blob: 3391012047df44c767c8abdee2bd19f7a3b9f80b [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 Shmidt34af3062013-07-11 10:46:32 -0700203
204
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800205/* Converts nl80211_chan_width to a common format */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800206enum chan_width convert2width(int width)
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800207{
208 switch (width) {
209 case NL80211_CHAN_WIDTH_20_NOHT:
210 return CHAN_WIDTH_20_NOHT;
211 case NL80211_CHAN_WIDTH_20:
212 return CHAN_WIDTH_20;
213 case NL80211_CHAN_WIDTH_40:
214 return CHAN_WIDTH_40;
215 case NL80211_CHAN_WIDTH_80:
216 return CHAN_WIDTH_80;
217 case NL80211_CHAN_WIDTH_80P80:
218 return CHAN_WIDTH_80P80;
219 case NL80211_CHAN_WIDTH_160:
220 return CHAN_WIDTH_160;
221 }
222 return CHAN_WIDTH_UNKNOWN;
223}
224
225
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800226int is_ap_interface(enum nl80211_iftype nlmode)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800227{
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700228 return nlmode == NL80211_IFTYPE_AP ||
229 nlmode == NL80211_IFTYPE_P2P_GO;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800230}
231
232
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800233int is_sta_interface(enum nl80211_iftype nlmode)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800234{
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700235 return nlmode == NL80211_IFTYPE_STATION ||
236 nlmode == NL80211_IFTYPE_P2P_CLIENT;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800237}
238
239
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700240static int is_p2p_net_interface(enum nl80211_iftype nlmode)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800241{
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700242 return nlmode == NL80211_IFTYPE_P2P_CLIENT ||
243 nlmode == NL80211_IFTYPE_P2P_GO;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800244}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700245
246
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800247struct i802_bss * get_bss_ifindex(struct wpa_driver_nl80211_data *drv,
248 int ifindex)
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -0700249{
250 struct i802_bss *bss;
251
252 for (bss = drv->first_bss; bss; bss = bss->next) {
253 if (bss->ifindex == ifindex)
254 return bss;
255 }
256
257 return NULL;
258}
259
260
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800261static int is_mesh_interface(enum nl80211_iftype nlmode)
262{
263 return nlmode == NL80211_IFTYPE_MESH_POINT;
264}
265
266
267void nl80211_mark_disconnected(struct wpa_driver_nl80211_data *drv)
Dmitry Shmidt8bae4132013-06-06 11:25:10 -0700268{
269 if (drv->associated)
270 os_memcpy(drv->prev_bssid, drv->bssid, ETH_ALEN);
271 drv->associated = 0;
272 os_memset(drv->bssid, 0, ETH_ALEN);
273}
274
275
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700276/* nl80211 code */
277static int ack_handler(struct nl_msg *msg, void *arg)
278{
279 int *err = arg;
280 *err = 0;
281 return NL_STOP;
282}
283
284static int finish_handler(struct nl_msg *msg, void *arg)
285{
286 int *ret = arg;
287 *ret = 0;
288 return NL_SKIP;
289}
290
291static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
292 void *arg)
293{
294 int *ret = arg;
295 *ret = err->error;
296 return NL_SKIP;
297}
298
299
300static int no_seq_check(struct nl_msg *msg, void *arg)
301{
302 return NL_OK;
303}
304
305
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800306static void nl80211_nlmsg_clear(struct nl_msg *msg)
307{
308 /*
309 * Clear nlmsg data, e.g., to make sure key material is not left in
310 * heap memory for unnecessarily long time.
311 */
312 if (msg) {
313 struct nlmsghdr *hdr = nlmsg_hdr(msg);
314 void *data = nlmsg_data(hdr);
315 /*
316 * This would use nlmsg_datalen() or the older nlmsg_len() if
317 * only libnl were to maintain a stable API.. Neither will work
318 * with all released versions, so just calculate the length
319 * here.
320 */
321 int len = hdr->nlmsg_len - NLMSG_HDRLEN;
322
323 os_memset(data, 0, len);
324 }
325}
326
327
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800328static int send_and_recv(struct nl80211_global *global,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700329 struct nl_handle *nl_handle, struct nl_msg *msg,
330 int (*valid_handler)(struct nl_msg *, void *),
331 void *valid_data)
332{
333 struct nl_cb *cb;
334 int err = -ENOMEM;
335
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800336 if (!msg)
337 return -ENOMEM;
338
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800339 cb = nl_cb_clone(global->nl_cb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700340 if (!cb)
341 goto out;
342
343 err = nl_send_auto_complete(nl_handle, msg);
344 if (err < 0)
345 goto out;
346
347 err = 1;
348
349 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
350 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
351 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
352
353 if (valid_handler)
354 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM,
355 valid_handler, valid_data);
356
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700357 while (err > 0) {
358 int res = nl_recvmsgs(nl_handle, cb);
Dmitry Shmidt71757432014-06-02 13:50:35 -0700359 if (res < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700360 wpa_printf(MSG_INFO,
361 "nl80211: %s->nl_recvmsgs failed: %d",
362 __func__, res);
363 }
364 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700365 out:
366 nl_cb_put(cb);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800367 if (!valid_handler && valid_data == (void *) -1)
368 nl80211_nlmsg_clear(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700369 nlmsg_free(msg);
370 return err;
371}
372
373
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800374int send_and_recv_msgs(struct wpa_driver_nl80211_data *drv,
375 struct nl_msg *msg,
376 int (*valid_handler)(struct nl_msg *, void *),
377 void *valid_data)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700378{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800379 return send_and_recv(drv->global, drv->global->nl, msg,
380 valid_handler, valid_data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700381}
382
383
384struct family_data {
385 const char *group;
386 int id;
387};
388
389
390static int family_handler(struct nl_msg *msg, void *arg)
391{
392 struct family_data *res = arg;
393 struct nlattr *tb[CTRL_ATTR_MAX + 1];
394 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
395 struct nlattr *mcgrp;
396 int i;
397
398 nla_parse(tb, CTRL_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
399 genlmsg_attrlen(gnlh, 0), NULL);
400 if (!tb[CTRL_ATTR_MCAST_GROUPS])
401 return NL_SKIP;
402
403 nla_for_each_nested(mcgrp, tb[CTRL_ATTR_MCAST_GROUPS], i) {
404 struct nlattr *tb2[CTRL_ATTR_MCAST_GRP_MAX + 1];
405 nla_parse(tb2, CTRL_ATTR_MCAST_GRP_MAX, nla_data(mcgrp),
406 nla_len(mcgrp), NULL);
407 if (!tb2[CTRL_ATTR_MCAST_GRP_NAME] ||
408 !tb2[CTRL_ATTR_MCAST_GRP_ID] ||
409 os_strncmp(nla_data(tb2[CTRL_ATTR_MCAST_GRP_NAME]),
410 res->group,
411 nla_len(tb2[CTRL_ATTR_MCAST_GRP_NAME])) != 0)
412 continue;
413 res->id = nla_get_u32(tb2[CTRL_ATTR_MCAST_GRP_ID]);
414 break;
415 };
416
417 return NL_SKIP;
418}
419
420
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800421static int nl_get_multicast_id(struct nl80211_global *global,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700422 const char *family, const char *group)
423{
424 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800425 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700426 struct family_data res = { group, -ENOENT };
427
428 msg = nlmsg_alloc();
429 if (!msg)
430 return -ENOMEM;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800431 if (!genlmsg_put(msg, 0, 0, genl_ctrl_resolve(global->nl, "nlctrl"),
432 0, 0, CTRL_CMD_GETFAMILY, 0) ||
433 nla_put_string(msg, CTRL_ATTR_FAMILY_NAME, family)) {
434 nlmsg_free(msg);
435 return -1;
436 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700437
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800438 ret = send_and_recv(global, global->nl, msg, family_handler, &res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700439 if (ret == 0)
440 ret = res.id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700441 return ret;
442}
443
444
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800445void * nl80211_cmd(struct wpa_driver_nl80211_data *drv,
446 struct nl_msg *msg, int flags, uint8_t cmd)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800447{
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700448 if (TEST_FAIL())
449 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800450 return genlmsg_put(msg, 0, 0, drv->global->nl80211_id,
451 0, flags, cmd, 0);
452}
453
454
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800455static int nl80211_set_iface_id(struct nl_msg *msg, struct i802_bss *bss)
456{
457 if (bss->wdev_id_set)
458 return nla_put_u64(msg, NL80211_ATTR_WDEV, bss->wdev_id);
459 return nla_put_u32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
460}
461
462
463struct nl_msg * nl80211_cmd_msg(struct i802_bss *bss, int flags, uint8_t cmd)
464{
465 struct nl_msg *msg;
466
467 msg = nlmsg_alloc();
468 if (!msg)
469 return NULL;
470
471 if (!nl80211_cmd(bss->drv, msg, flags, cmd) ||
472 nl80211_set_iface_id(msg, bss) < 0) {
473 nlmsg_free(msg);
474 return NULL;
475 }
476
477 return msg;
478}
479
480
481static struct nl_msg *
482nl80211_ifindex_msg(struct wpa_driver_nl80211_data *drv, int ifindex,
483 int flags, uint8_t cmd)
484{
485 struct nl_msg *msg;
486
487 msg = nlmsg_alloc();
488 if (!msg)
489 return NULL;
490
491 if (!nl80211_cmd(drv, msg, flags, cmd) ||
492 nla_put_u32(msg, NL80211_ATTR_IFINDEX, ifindex)) {
493 nlmsg_free(msg);
494 return NULL;
495 }
496
497 return msg;
498}
499
500
501struct nl_msg * nl80211_drv_msg(struct wpa_driver_nl80211_data *drv, int flags,
502 uint8_t cmd)
503{
504 return nl80211_ifindex_msg(drv, drv->ifindex, flags, cmd);
505}
506
507
508struct nl_msg * nl80211_bss_msg(struct i802_bss *bss, int flags, uint8_t cmd)
509{
510 return nl80211_ifindex_msg(bss->drv, bss->ifindex, flags, cmd);
511}
512
513
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800514struct wiphy_idx_data {
515 int wiphy_idx;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700516 enum nl80211_iftype nlmode;
517 u8 *macaddr;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800518};
519
520
521static int netdev_info_handler(struct nl_msg *msg, void *arg)
522{
523 struct nlattr *tb[NL80211_ATTR_MAX + 1];
524 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
525 struct wiphy_idx_data *info = arg;
526
527 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
528 genlmsg_attrlen(gnlh, 0), NULL);
529
530 if (tb[NL80211_ATTR_WIPHY])
531 info->wiphy_idx = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
532
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700533 if (tb[NL80211_ATTR_IFTYPE])
534 info->nlmode = nla_get_u32(tb[NL80211_ATTR_IFTYPE]);
535
536 if (tb[NL80211_ATTR_MAC] && info->macaddr)
537 os_memcpy(info->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
538 ETH_ALEN);
539
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800540 return NL_SKIP;
541}
542
543
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800544int nl80211_get_wiphy_index(struct i802_bss *bss)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800545{
546 struct nl_msg *msg;
547 struct wiphy_idx_data data = {
548 .wiphy_idx = -1,
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700549 .macaddr = NULL,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800550 };
551
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800552 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_GET_INTERFACE)))
553 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800554
555 if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0)
556 return data.wiphy_idx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800557 return -1;
558}
559
560
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700561static enum nl80211_iftype nl80211_get_ifmode(struct i802_bss *bss)
562{
563 struct nl_msg *msg;
564 struct wiphy_idx_data data = {
565 .nlmode = NL80211_IFTYPE_UNSPECIFIED,
566 .macaddr = NULL,
567 };
568
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800569 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_GET_INTERFACE)))
570 return NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700571
572 if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0)
573 return data.nlmode;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700574 return NL80211_IFTYPE_UNSPECIFIED;
575}
576
577
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700578static int nl80211_get_macaddr(struct i802_bss *bss)
579{
580 struct nl_msg *msg;
581 struct wiphy_idx_data data = {
582 .macaddr = bss->addr,
583 };
584
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800585 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_GET_INTERFACE)))
586 return -1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700587
588 return send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data);
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700589}
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700590
591
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800592static int nl80211_register_beacons(struct wpa_driver_nl80211_data *drv,
593 struct nl80211_wiphy_data *w)
594{
595 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800596 int ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800597
598 msg = nlmsg_alloc();
599 if (!msg)
600 return -1;
601
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800602 if (!nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_BEACONS) ||
603 nla_put_u32(msg, NL80211_ATTR_WIPHY, w->wiphy_idx)) {
604 nlmsg_free(msg);
605 return -1;
606 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800607
608 ret = send_and_recv(drv->global, w->nl_beacons, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800609 if (ret) {
610 wpa_printf(MSG_DEBUG, "nl80211: Register beacons command "
611 "failed: ret=%d (%s)",
612 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800613 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800614 return ret;
615}
616
617
618static void nl80211_recv_beacons(int sock, void *eloop_ctx, void *handle)
619{
620 struct nl80211_wiphy_data *w = eloop_ctx;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700621 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800622
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800623 wpa_printf(MSG_EXCESSIVE, "nl80211: Beacon event message available");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800624
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700625 res = nl_recvmsgs(handle, w->nl_cb);
Dmitry Shmidt71757432014-06-02 13:50:35 -0700626 if (res < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700627 wpa_printf(MSG_INFO, "nl80211: %s->nl_recvmsgs failed: %d",
628 __func__, res);
629 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800630}
631
632
633static int process_beacon_event(struct nl_msg *msg, void *arg)
634{
635 struct nl80211_wiphy_data *w = arg;
636 struct wpa_driver_nl80211_data *drv;
637 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
638 struct nlattr *tb[NL80211_ATTR_MAX + 1];
639 union wpa_event_data event;
640
641 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
642 genlmsg_attrlen(gnlh, 0), NULL);
643
644 if (gnlh->cmd != NL80211_CMD_FRAME) {
645 wpa_printf(MSG_DEBUG, "nl80211: Unexpected beacon event? (%d)",
646 gnlh->cmd);
647 return NL_SKIP;
648 }
649
650 if (!tb[NL80211_ATTR_FRAME])
651 return NL_SKIP;
652
653 dl_list_for_each(drv, &w->drvs, struct wpa_driver_nl80211_data,
654 wiphy_list) {
655 os_memset(&event, 0, sizeof(event));
656 event.rx_mgmt.frame = nla_data(tb[NL80211_ATTR_FRAME]);
657 event.rx_mgmt.frame_len = nla_len(tb[NL80211_ATTR_FRAME]);
658 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
659 }
660
661 return NL_SKIP;
662}
663
664
665static struct nl80211_wiphy_data *
666nl80211_get_wiphy_data_ap(struct i802_bss *bss)
667{
668 static DEFINE_DL_LIST(nl80211_wiphys);
669 struct nl80211_wiphy_data *w;
670 int wiphy_idx, found = 0;
671 struct i802_bss *tmp_bss;
672
673 if (bss->wiphy_data != NULL)
674 return bss->wiphy_data;
675
676 wiphy_idx = nl80211_get_wiphy_index(bss);
677
678 dl_list_for_each(w, &nl80211_wiphys, struct nl80211_wiphy_data, list) {
679 if (w->wiphy_idx == wiphy_idx)
680 goto add;
681 }
682
683 /* alloc new one */
684 w = os_zalloc(sizeof(*w));
685 if (w == NULL)
686 return NULL;
687 w->wiphy_idx = wiphy_idx;
688 dl_list_init(&w->bsss);
689 dl_list_init(&w->drvs);
690
691 w->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
692 if (!w->nl_cb) {
693 os_free(w);
694 return NULL;
695 }
696 nl_cb_set(w->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL);
697 nl_cb_set(w->nl_cb, NL_CB_VALID, NL_CB_CUSTOM, process_beacon_event,
698 w);
699
700 w->nl_beacons = nl_create_handle(bss->drv->global->nl_cb,
701 "wiphy beacons");
702 if (w->nl_beacons == NULL) {
703 os_free(w);
704 return NULL;
705 }
706
707 if (nl80211_register_beacons(bss->drv, w)) {
708 nl_destroy_handles(&w->nl_beacons);
709 os_free(w);
710 return NULL;
711 }
712
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700713 nl80211_register_eloop_read(&w->nl_beacons, nl80211_recv_beacons, w);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800714
715 dl_list_add(&nl80211_wiphys, &w->list);
716
717add:
718 /* drv entry for this bss already there? */
719 dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
720 if (tmp_bss->drv == bss->drv) {
721 found = 1;
722 break;
723 }
724 }
725 /* if not add it */
726 if (!found)
727 dl_list_add(&w->drvs, &bss->drv->wiphy_list);
728
729 dl_list_add(&w->bsss, &bss->wiphy_list);
730 bss->wiphy_data = w;
731 return w;
732}
733
734
735static void nl80211_put_wiphy_data_ap(struct i802_bss *bss)
736{
737 struct nl80211_wiphy_data *w = bss->wiphy_data;
738 struct i802_bss *tmp_bss;
739 int found = 0;
740
741 if (w == NULL)
742 return;
743 bss->wiphy_data = NULL;
744 dl_list_del(&bss->wiphy_list);
745
746 /* still any for this drv present? */
747 dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
748 if (tmp_bss->drv == bss->drv) {
749 found = 1;
750 break;
751 }
752 }
753 /* if not remove it */
754 if (!found)
755 dl_list_del(&bss->drv->wiphy_list);
756
757 if (!dl_list_empty(&w->bsss))
758 return;
759
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700760 nl80211_destroy_eloop_handle(&w->nl_beacons);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800761
762 nl_cb_put(w->nl_cb);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800763 dl_list_del(&w->list);
764 os_free(w);
765}
766
767
Dmitry Shmidte4663042016-04-04 10:07:49 -0700768static unsigned int nl80211_get_ifindex(void *priv)
769{
770 struct i802_bss *bss = priv;
771 struct wpa_driver_nl80211_data *drv = bss->drv;
772
773 return drv->ifindex;
774}
775
776
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700777static int wpa_driver_nl80211_get_bssid(void *priv, u8 *bssid)
778{
779 struct i802_bss *bss = priv;
780 struct wpa_driver_nl80211_data *drv = bss->drv;
781 if (!drv->associated)
782 return -1;
783 os_memcpy(bssid, drv->bssid, ETH_ALEN);
784 return 0;
785}
786
787
788static int wpa_driver_nl80211_get_ssid(void *priv, u8 *ssid)
789{
790 struct i802_bss *bss = priv;
791 struct wpa_driver_nl80211_data *drv = bss->drv;
792 if (!drv->associated)
793 return -1;
794 os_memcpy(ssid, drv->ssid, drv->ssid_len);
795 return drv->ssid_len;
796}
797
798
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800799static void wpa_driver_nl80211_event_newlink(
Dmitry Shmidte4663042016-04-04 10:07:49 -0700800 struct nl80211_global *global, struct wpa_driver_nl80211_data *drv,
801 int ifindex, const char *ifname)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700802{
803 union wpa_event_data event;
804
Dmitry Shmidte4663042016-04-04 10:07:49 -0700805 if (drv && os_strcmp(drv->first_bss->ifname, ifname) == 0) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800806 if (if_nametoindex(drv->first_bss->ifname) == 0) {
807 wpa_printf(MSG_DEBUG, "nl80211: Interface %s does not exist - ignore RTM_NEWLINK",
808 drv->first_bss->ifname);
809 return;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700810 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800811 if (!drv->if_removed)
812 return;
813 wpa_printf(MSG_DEBUG, "nl80211: Mark if_removed=0 for %s based on RTM_NEWLINK event",
814 drv->first_bss->ifname);
815 drv->if_removed = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700816 }
817
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800818 os_memset(&event, 0, sizeof(event));
Dmitry Shmidte4663042016-04-04 10:07:49 -0700819 event.interface_status.ifindex = ifindex;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800820 os_strlcpy(event.interface_status.ifname, ifname,
821 sizeof(event.interface_status.ifname));
822 event.interface_status.ievent = EVENT_INTERFACE_ADDED;
Dmitry Shmidte4663042016-04-04 10:07:49 -0700823 if (drv)
824 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
825 else
826 wpa_supplicant_event_global(global->ctx, EVENT_INTERFACE_STATUS,
827 &event);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800828}
829
830
831static void wpa_driver_nl80211_event_dellink(
Dmitry Shmidte4663042016-04-04 10:07:49 -0700832 struct nl80211_global *global, struct wpa_driver_nl80211_data *drv,
833 int ifindex, const char *ifname)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800834{
835 union wpa_event_data event;
836
Dmitry Shmidte4663042016-04-04 10:07:49 -0700837 if (drv && os_strcmp(drv->first_bss->ifname, ifname) == 0) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800838 if (drv->if_removed) {
839 wpa_printf(MSG_DEBUG, "nl80211: if_removed already set - ignore RTM_DELLINK event for %s",
840 ifname);
841 return;
842 }
843 wpa_printf(MSG_DEBUG, "RTM_DELLINK: Interface '%s' removed - mark if_removed=1",
844 ifname);
845 drv->if_removed = 1;
846 } else {
847 wpa_printf(MSG_DEBUG, "RTM_DELLINK: Interface '%s' removed",
848 ifname);
849 }
850
851 os_memset(&event, 0, sizeof(event));
Dmitry Shmidte4663042016-04-04 10:07:49 -0700852 event.interface_status.ifindex = ifindex;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800853 os_strlcpy(event.interface_status.ifname, ifname,
854 sizeof(event.interface_status.ifname));
855 event.interface_status.ievent = EVENT_INTERFACE_REMOVED;
Dmitry Shmidte4663042016-04-04 10:07:49 -0700856 if (drv)
857 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
858 else
859 wpa_supplicant_event_global(global->ctx, EVENT_INTERFACE_STATUS,
860 &event);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700861}
862
863
864static int wpa_driver_nl80211_own_ifname(struct wpa_driver_nl80211_data *drv,
865 u8 *buf, size_t len)
866{
867 int attrlen, rta_len;
868 struct rtattr *attr;
869
870 attrlen = len;
871 attr = (struct rtattr *) buf;
872
873 rta_len = RTA_ALIGN(sizeof(struct rtattr));
874 while (RTA_OK(attr, attrlen)) {
875 if (attr->rta_type == IFLA_IFNAME) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800876 if (os_strcmp(((char *) attr) + rta_len,
877 drv->first_bss->ifname) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700878 return 1;
879 else
880 break;
881 }
882 attr = RTA_NEXT(attr, attrlen);
883 }
884
885 return 0;
886}
887
888
889static int wpa_driver_nl80211_own_ifindex(struct wpa_driver_nl80211_data *drv,
890 int ifindex, u8 *buf, size_t len)
891{
892 if (drv->ifindex == ifindex)
893 return 1;
894
895 if (drv->if_removed && wpa_driver_nl80211_own_ifname(drv, buf, len)) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800896 nl80211_check_global(drv->global);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700897 wpa_printf(MSG_DEBUG, "nl80211: Update ifindex for a removed "
898 "interface");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800899 wpa_driver_nl80211_finish_drv_init(drv, NULL, 0, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700900 return 1;
901 }
902
903 return 0;
904}
905
906
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800907static struct wpa_driver_nl80211_data *
908nl80211_find_drv(struct nl80211_global *global, int idx, u8 *buf, size_t len)
909{
910 struct wpa_driver_nl80211_data *drv;
911 dl_list_for_each(drv, &global->interfaces,
912 struct wpa_driver_nl80211_data, list) {
913 if (wpa_driver_nl80211_own_ifindex(drv, idx, buf, len) ||
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800914 have_ifidx(drv, idx, IFIDX_ANY))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800915 return drv;
916 }
917 return NULL;
918}
919
920
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700921static void wpa_driver_nl80211_event_rtm_newlink(void *ctx,
922 struct ifinfomsg *ifi,
923 u8 *buf, size_t len)
924{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800925 struct nl80211_global *global = ctx;
926 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800927 int attrlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700928 struct rtattr *attr;
929 u32 brid = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800930 char namebuf[IFNAMSIZ];
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800931 char ifname[IFNAMSIZ + 1];
932 char extra[100], *pos, *end;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700933
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800934 extra[0] = '\0';
935 pos = extra;
936 end = pos + sizeof(extra);
937 ifname[0] = '\0';
938
939 attrlen = len;
940 attr = (struct rtattr *) buf;
941 while (RTA_OK(attr, attrlen)) {
942 switch (attr->rta_type) {
943 case IFLA_IFNAME:
944 if (RTA_PAYLOAD(attr) >= IFNAMSIZ)
945 break;
946 os_memcpy(ifname, RTA_DATA(attr), RTA_PAYLOAD(attr));
947 ifname[RTA_PAYLOAD(attr)] = '\0';
948 break;
949 case IFLA_MASTER:
950 brid = nla_get_u32((struct nlattr *) attr);
951 pos += os_snprintf(pos, end - pos, " master=%u", brid);
952 break;
953 case IFLA_WIRELESS:
954 pos += os_snprintf(pos, end - pos, " wext");
955 break;
956 case IFLA_OPERSTATE:
957 pos += os_snprintf(pos, end - pos, " operstate=%u",
958 nla_get_u32((struct nlattr *) attr));
959 break;
960 case IFLA_LINKMODE:
961 pos += os_snprintf(pos, end - pos, " linkmode=%u",
962 nla_get_u32((struct nlattr *) attr));
963 break;
964 }
965 attr = RTA_NEXT(attr, attrlen);
966 }
967 extra[sizeof(extra) - 1] = '\0';
968
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700969 wpa_printf(MSG_DEBUG, "RTM_NEWLINK: ifi_index=%d ifname=%s%s ifi_family=%d ifi_flags=0x%x (%s%s%s%s)",
970 ifi->ifi_index, ifname, extra, ifi->ifi_family,
971 ifi->ifi_flags,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700972 (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
973 (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
974 (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
975 (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
976
Dmitry Shmidte4663042016-04-04 10:07:49 -0700977 drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
978 if (!drv)
979 goto event_newlink;
980
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700981 if (!drv->if_disabled && !(ifi->ifi_flags & IFF_UP)) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800982 namebuf[0] = '\0';
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800983 if (if_indextoname(ifi->ifi_index, namebuf) &&
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800984 linux_iface_up(drv->global->ioctl_sock, namebuf) > 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800985 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
986 "event since interface %s is up", namebuf);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800987 drv->ignore_if_down_event = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800988 return;
989 }
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800990 wpa_printf(MSG_DEBUG, "nl80211: Interface down (%s/%s)",
991 namebuf, ifname);
992 if (os_strcmp(drv->first_bss->ifname, ifname) != 0) {
993 wpa_printf(MSG_DEBUG,
994 "nl80211: Not the main interface (%s) - do not indicate interface down",
995 drv->first_bss->ifname);
996 } else if (drv->ignore_if_down_event) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800997 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
998 "event generated by mode change");
999 drv->ignore_if_down_event = 0;
1000 } else {
1001 drv->if_disabled = 1;
1002 wpa_supplicant_event(drv->ctx,
1003 EVENT_INTERFACE_DISABLED, NULL);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001004
1005 /*
1006 * Try to get drv again, since it may be removed as
1007 * part of the EVENT_INTERFACE_DISABLED handling for
1008 * dynamic interfaces
1009 */
1010 drv = nl80211_find_drv(global, ifi->ifi_index,
1011 buf, len);
1012 if (!drv)
1013 return;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001014 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001015 }
1016
1017 if (drv->if_disabled && (ifi->ifi_flags & IFF_UP)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001018 if (if_indextoname(ifi->ifi_index, namebuf) &&
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001019 linux_iface_up(drv->global->ioctl_sock, namebuf) == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001020 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
1021 "event since interface %s is down",
1022 namebuf);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001023 } else if (if_nametoindex(drv->first_bss->ifname) == 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001024 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
1025 "event since interface %s does not exist",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001026 drv->first_bss->ifname);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001027 } else if (drv->if_removed) {
1028 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
1029 "event since interface %s is marked "
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001030 "removed", drv->first_bss->ifname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001031 } else {
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07001032 struct i802_bss *bss;
1033 u8 addr[ETH_ALEN];
1034
1035 /* Re-read MAC address as it may have changed */
1036 bss = get_bss_ifindex(drv, ifi->ifi_index);
1037 if (bss &&
1038 linux_get_ifhwaddr(drv->global->ioctl_sock,
1039 bss->ifname, addr) < 0) {
1040 wpa_printf(MSG_DEBUG,
1041 "nl80211: %s: failed to re-read MAC address",
1042 bss->ifname);
1043 } else if (bss &&
1044 os_memcmp(addr, bss->addr, ETH_ALEN) != 0) {
1045 wpa_printf(MSG_DEBUG,
1046 "nl80211: Own MAC address on ifindex %d (%s) changed from "
1047 MACSTR " to " MACSTR,
1048 ifi->ifi_index, bss->ifname,
1049 MAC2STR(bss->addr),
1050 MAC2STR(addr));
1051 os_memcpy(bss->addr, addr, ETH_ALEN);
1052 }
1053
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001054 wpa_printf(MSG_DEBUG, "nl80211: Interface up");
1055 drv->if_disabled = 0;
1056 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_ENABLED,
1057 NULL);
1058 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001059 }
1060
1061 /*
1062 * Some drivers send the association event before the operup event--in
1063 * this case, lifting operstate in wpa_driver_nl80211_set_operstate()
1064 * fails. This will hit us when wpa_supplicant does not need to do
1065 * IEEE 802.1X authentication
1066 */
1067 if (drv->operstate == 1 &&
1068 (ifi->ifi_flags & (IFF_LOWER_UP | IFF_DORMANT)) == IFF_LOWER_UP &&
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001069 !(ifi->ifi_flags & IFF_RUNNING)) {
1070 wpa_printf(MSG_DEBUG, "nl80211: Set IF_OPER_UP again based on ifi_flags and expected operstate");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001071 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001072 -1, IF_OPER_UP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001073 }
1074
Dmitry Shmidte4663042016-04-04 10:07:49 -07001075event_newlink:
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001076 if (ifname[0])
Dmitry Shmidte4663042016-04-04 10:07:49 -07001077 wpa_driver_nl80211_event_newlink(global, drv, ifi->ifi_index,
1078 ifname);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001079
Dmitry Shmidte4663042016-04-04 10:07:49 -07001080 if (ifi->ifi_family == AF_BRIDGE && brid && drv) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001081 struct i802_bss *bss;
1082
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001083 /* device has been added to bridge */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001084 if (!if_indextoname(brid, namebuf)) {
1085 wpa_printf(MSG_DEBUG,
1086 "nl80211: Could not find bridge ifname for ifindex %u",
1087 brid);
1088 return;
1089 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001090 wpa_printf(MSG_DEBUG, "nl80211: Add ifindex %u for bridge %s",
1091 brid, namebuf);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001092 add_ifidx(drv, brid, ifi->ifi_index);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001093
1094 for (bss = drv->first_bss; bss; bss = bss->next) {
1095 if (os_strcmp(ifname, bss->ifname) == 0) {
1096 os_strlcpy(bss->brname, namebuf, IFNAMSIZ);
1097 break;
1098 }
1099 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001100 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001101}
1102
1103
1104static void wpa_driver_nl80211_event_rtm_dellink(void *ctx,
1105 struct ifinfomsg *ifi,
1106 u8 *buf, size_t len)
1107{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001108 struct nl80211_global *global = ctx;
1109 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001110 int attrlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001111 struct rtattr *attr;
1112 u32 brid = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001113 char ifname[IFNAMSIZ + 1];
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001114 char extra[100], *pos, *end;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001115
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001116 extra[0] = '\0';
1117 pos = extra;
1118 end = pos + sizeof(extra);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001119 ifname[0] = '\0';
1120
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001121 attrlen = len;
1122 attr = (struct rtattr *) buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001123 while (RTA_OK(attr, attrlen)) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001124 switch (attr->rta_type) {
1125 case IFLA_IFNAME:
1126 if (RTA_PAYLOAD(attr) >= IFNAMSIZ)
1127 break;
1128 os_memcpy(ifname, RTA_DATA(attr), RTA_PAYLOAD(attr));
1129 ifname[RTA_PAYLOAD(attr)] = '\0';
1130 break;
1131 case IFLA_MASTER:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001132 brid = nla_get_u32((struct nlattr *) attr);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001133 pos += os_snprintf(pos, end - pos, " master=%u", brid);
1134 break;
1135 case IFLA_OPERSTATE:
1136 pos += os_snprintf(pos, end - pos, " operstate=%u",
1137 nla_get_u32((struct nlattr *) attr));
1138 break;
1139 case IFLA_LINKMODE:
1140 pos += os_snprintf(pos, end - pos, " linkmode=%u",
1141 nla_get_u32((struct nlattr *) attr));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001142 break;
1143 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001144 attr = RTA_NEXT(attr, attrlen);
1145 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001146 extra[sizeof(extra) - 1] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001147
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001148 wpa_printf(MSG_DEBUG, "RTM_DELLINK: ifi_index=%d ifname=%s%s ifi_family=%d ifi_flags=0x%x (%s%s%s%s)",
1149 ifi->ifi_index, ifname, extra, ifi->ifi_family,
1150 ifi->ifi_flags,
1151 (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
1152 (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
1153 (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
1154 (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
1155
Dmitry Shmidte4663042016-04-04 10:07:49 -07001156 drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001157
Dmitry Shmidte4663042016-04-04 10:07:49 -07001158 if (ifi->ifi_family == AF_BRIDGE && brid && drv) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001159 /* device has been removed from bridge */
1160 char namebuf[IFNAMSIZ];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001161
1162 if (!if_indextoname(brid, namebuf)) {
1163 wpa_printf(MSG_DEBUG,
1164 "nl80211: Could not find bridge ifname for ifindex %u",
1165 brid);
1166 } else {
1167 wpa_printf(MSG_DEBUG,
1168 "nl80211: Remove ifindex %u for bridge %s",
1169 brid, namebuf);
1170 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001171 del_ifidx(drv, brid, ifi->ifi_index);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001172 }
Dmitry Shmidte4663042016-04-04 10:07:49 -07001173
1174 if (ifi->ifi_family != AF_BRIDGE || !brid)
1175 wpa_driver_nl80211_event_dellink(global, drv, ifi->ifi_index,
1176 ifname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001177}
1178
1179
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001180unsigned int nl80211_get_assoc_freq(struct wpa_driver_nl80211_data *drv)
Jouni Malinen87fd2792011-05-16 18:35:42 +03001181{
1182 struct nl_msg *msg;
1183 int ret;
1184 struct nl80211_bss_info_arg arg;
1185
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001186 msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
Jouni Malinen87fd2792011-05-16 18:35:42 +03001187 os_memset(&arg, 0, sizeof(arg));
Jouni Malinen87fd2792011-05-16 18:35:42 +03001188 arg.drv = drv;
1189 ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
Jouni Malinen87fd2792011-05-16 18:35:42 +03001190 if (ret == 0) {
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07001191 unsigned int freq = drv->nlmode == NL80211_IFTYPE_ADHOC ?
1192 arg.ibss_freq : arg.assoc_freq;
Jouni Malinen87fd2792011-05-16 18:35:42 +03001193 wpa_printf(MSG_DEBUG, "nl80211: Operating frequency for the "
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07001194 "associated BSS from scan results: %u MHz", freq);
1195 if (freq)
1196 drv->assoc_freq = freq;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001197 return drv->assoc_freq;
Jouni Malinen87fd2792011-05-16 18:35:42 +03001198 }
1199 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
1200 "(%s)", ret, strerror(-ret));
Jouni Malinen87fd2792011-05-16 18:35:42 +03001201 return drv->assoc_freq;
1202}
1203
1204
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001205static int get_link_signal(struct nl_msg *msg, void *arg)
1206{
1207 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1208 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1209 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
1210 static struct nla_policy policy[NL80211_STA_INFO_MAX + 1] = {
1211 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001212 [NL80211_STA_INFO_SIGNAL_AVG] = { .type = NLA_U8 },
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07001213 [NL80211_STA_INFO_BEACON_SIGNAL_AVG] = { .type = NLA_U8 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001214 };
1215 struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
1216 static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
1217 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
1218 [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
1219 [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
1220 [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
1221 };
1222 struct wpa_signal_info *sig_change = arg;
1223
1224 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1225 genlmsg_attrlen(gnlh, 0), NULL);
1226 if (!tb[NL80211_ATTR_STA_INFO] ||
1227 nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
1228 tb[NL80211_ATTR_STA_INFO], policy))
1229 return NL_SKIP;
1230 if (!sinfo[NL80211_STA_INFO_SIGNAL])
1231 return NL_SKIP;
1232
1233 sig_change->current_signal =
1234 (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
1235
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001236 if (sinfo[NL80211_STA_INFO_SIGNAL_AVG])
1237 sig_change->avg_signal =
1238 (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL_AVG]);
1239 else
1240 sig_change->avg_signal = 0;
1241
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07001242 if (sinfo[NL80211_STA_INFO_BEACON_SIGNAL_AVG])
1243 sig_change->avg_beacon_signal =
1244 (s8)
1245 nla_get_u8(sinfo[NL80211_STA_INFO_BEACON_SIGNAL_AVG]);
1246 else
1247 sig_change->avg_beacon_signal = 0;
1248
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001249 if (sinfo[NL80211_STA_INFO_TX_BITRATE]) {
1250 if (nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
1251 sinfo[NL80211_STA_INFO_TX_BITRATE],
1252 rate_policy)) {
1253 sig_change->current_txrate = 0;
1254 } else {
1255 if (rinfo[NL80211_RATE_INFO_BITRATE]) {
1256 sig_change->current_txrate =
1257 nla_get_u16(rinfo[
1258 NL80211_RATE_INFO_BITRATE]) * 100;
1259 }
1260 }
1261 }
1262
1263 return NL_SKIP;
1264}
1265
1266
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001267int nl80211_get_link_signal(struct wpa_driver_nl80211_data *drv,
1268 struct wpa_signal_info *sig)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001269{
1270 struct nl_msg *msg;
1271
1272 sig->current_signal = -9999;
1273 sig->current_txrate = 0;
1274
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001275 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_GET_STATION)) ||
1276 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid)) {
1277 nlmsg_free(msg);
1278 return -ENOBUFS;
1279 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001280
1281 return send_and_recv_msgs(drv, msg, get_link_signal, sig);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001282}
1283
1284
1285static int get_link_noise(struct nl_msg *msg, void *arg)
1286{
1287 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1288 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1289 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
1290 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
1291 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
1292 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
1293 };
1294 struct wpa_signal_info *sig_change = arg;
1295
1296 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1297 genlmsg_attrlen(gnlh, 0), NULL);
1298
1299 if (!tb[NL80211_ATTR_SURVEY_INFO]) {
1300 wpa_printf(MSG_DEBUG, "nl80211: survey data missing!");
1301 return NL_SKIP;
1302 }
1303
1304 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
1305 tb[NL80211_ATTR_SURVEY_INFO],
1306 survey_policy)) {
1307 wpa_printf(MSG_DEBUG, "nl80211: failed to parse nested "
1308 "attributes!");
1309 return NL_SKIP;
1310 }
1311
1312 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
1313 return NL_SKIP;
1314
1315 if (nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
1316 sig_change->frequency)
1317 return NL_SKIP;
1318
1319 if (!sinfo[NL80211_SURVEY_INFO_NOISE])
1320 return NL_SKIP;
1321
1322 sig_change->current_noise =
1323 (s8) nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
1324
1325 return NL_SKIP;
1326}
1327
1328
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001329int nl80211_get_link_noise(struct wpa_driver_nl80211_data *drv,
1330 struct wpa_signal_info *sig_change)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001331{
1332 struct nl_msg *msg;
1333
1334 sig_change->current_noise = 9999;
1335 sig_change->frequency = drv->assoc_freq;
1336
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001337 msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001338 return send_and_recv_msgs(drv, msg, get_link_noise, sig_change);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001339}
1340
1341
1342static void wpa_driver_nl80211_event_receive(int sock, void *eloop_ctx,
1343 void *handle)
1344{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001345 struct nl_cb *cb = eloop_ctx;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001346 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001347
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001348 wpa_printf(MSG_MSGDUMP, "nl80211: Event message available");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001349
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001350 res = nl_recvmsgs(handle, cb);
Dmitry Shmidt71757432014-06-02 13:50:35 -07001351 if (res < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001352 wpa_printf(MSG_INFO, "nl80211: %s->nl_recvmsgs failed: %d",
1353 __func__, res);
1354 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001355}
1356
1357
1358/**
1359 * wpa_driver_nl80211_set_country - ask nl80211 to set the regulatory domain
1360 * @priv: driver_nl80211 private data
1361 * @alpha2_arg: country to which to switch to
1362 * Returns: 0 on success, -1 on failure
1363 *
1364 * This asks nl80211 to set the regulatory domain for given
1365 * country ISO / IEC alpha2.
1366 */
1367static int wpa_driver_nl80211_set_country(void *priv, const char *alpha2_arg)
1368{
1369 struct i802_bss *bss = priv;
1370 struct wpa_driver_nl80211_data *drv = bss->drv;
1371 char alpha2[3];
1372 struct nl_msg *msg;
1373
1374 msg = nlmsg_alloc();
1375 if (!msg)
1376 return -ENOMEM;
1377
1378 alpha2[0] = alpha2_arg[0];
1379 alpha2[1] = alpha2_arg[1];
1380 alpha2[2] = '\0';
1381
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001382 if (!nl80211_cmd(drv, msg, 0, NL80211_CMD_REQ_SET_REG) ||
1383 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, alpha2)) {
1384 nlmsg_free(msg);
1385 return -EINVAL;
1386 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001387 if (send_and_recv_msgs(drv, msg, NULL, NULL))
1388 return -EINVAL;
1389 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001390}
1391
1392
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001393static int nl80211_get_country(struct nl_msg *msg, void *arg)
1394{
1395 char *alpha2 = arg;
1396 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
1397 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1398
1399 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1400 genlmsg_attrlen(gnlh, 0), NULL);
1401 if (!tb_msg[NL80211_ATTR_REG_ALPHA2]) {
1402 wpa_printf(MSG_DEBUG, "nl80211: No country information available");
1403 return NL_SKIP;
1404 }
1405 os_strlcpy(alpha2, nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]), 3);
1406 return NL_SKIP;
1407}
1408
1409
1410static int wpa_driver_nl80211_get_country(void *priv, char *alpha2)
1411{
1412 struct i802_bss *bss = priv;
1413 struct wpa_driver_nl80211_data *drv = bss->drv;
1414 struct nl_msg *msg;
1415 int ret;
1416
1417 msg = nlmsg_alloc();
1418 if (!msg)
1419 return -ENOMEM;
1420
1421 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG);
1422 alpha2[0] = '\0';
1423 ret = send_and_recv_msgs(drv, msg, nl80211_get_country, alpha2);
1424 if (!alpha2[0])
1425 ret = -1;
1426
1427 return ret;
1428}
1429
1430
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001431static int wpa_driver_nl80211_init_nl_global(struct nl80211_global *global)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001432{
1433 int ret;
1434
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001435 global->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
1436 if (global->nl_cb == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001437 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
1438 "callbacks");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001439 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001440 }
1441
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001442 global->nl = nl_create_handle(global->nl_cb, "nl");
1443 if (global->nl == NULL)
1444 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001445
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001446 global->nl80211_id = genl_ctrl_resolve(global->nl, "nl80211");
1447 if (global->nl80211_id < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001448 wpa_printf(MSG_ERROR, "nl80211: 'nl80211' generic netlink not "
1449 "found");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001450 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001451 }
1452
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001453 global->nl_event = nl_create_handle(global->nl_cb, "event");
1454 if (global->nl_event == NULL)
1455 goto err;
1456
1457 ret = nl_get_multicast_id(global, "nl80211", "scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001458 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001459 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001460 if (ret < 0) {
1461 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
1462 "membership for scan events: %d (%s)",
1463 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001464 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001465 }
1466
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001467 ret = nl_get_multicast_id(global, "nl80211", "mlme");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001468 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001469 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001470 if (ret < 0) {
1471 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
1472 "membership for mlme events: %d (%s)",
1473 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001474 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001475 }
1476
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001477 ret = nl_get_multicast_id(global, "nl80211", "regulatory");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001478 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001479 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001480 if (ret < 0) {
1481 wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
1482 "membership for regulatory events: %d (%s)",
1483 ret, strerror(-ret));
1484 /* Continue without regulatory events */
1485 }
1486
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001487 ret = nl_get_multicast_id(global, "nl80211", "vendor");
1488 if (ret >= 0)
1489 ret = nl_socket_add_membership(global->nl_event, ret);
1490 if (ret < 0) {
1491 wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
1492 "membership for vendor events: %d (%s)",
1493 ret, strerror(-ret));
1494 /* Continue without vendor events */
1495 }
1496
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001497 nl_cb_set(global->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
1498 no_seq_check, NULL);
1499 nl_cb_set(global->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
1500 process_global_event, global);
1501
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001502 nl80211_register_eloop_read(&global->nl_event,
1503 wpa_driver_nl80211_event_receive,
1504 global->nl_cb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001505
1506 return 0;
1507
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001508err:
1509 nl_destroy_handles(&global->nl_event);
1510 nl_destroy_handles(&global->nl);
1511 nl_cb_put(global->nl_cb);
1512 global->nl_cb = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001513 return -1;
1514}
1515
1516
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001517static void nl80211_check_global(struct nl80211_global *global)
1518{
1519 struct nl_handle *handle;
1520 const char *groups[] = { "scan", "mlme", "regulatory", "vendor", NULL };
1521 int ret;
1522 unsigned int i;
1523
1524 /*
1525 * Try to re-add memberships to handle case of cfg80211 getting reloaded
1526 * and all registration having been cleared.
1527 */
1528 handle = (void *) (((intptr_t) global->nl_event) ^
1529 ELOOP_SOCKET_INVALID);
1530
1531 for (i = 0; groups[i]; i++) {
1532 ret = nl_get_multicast_id(global, "nl80211", groups[i]);
1533 if (ret >= 0)
1534 ret = nl_socket_add_membership(handle, ret);
1535 if (ret < 0) {
1536 wpa_printf(MSG_INFO,
1537 "nl80211: Could not re-add multicast membership for %s events: %d (%s)",
1538 groups[i], ret, strerror(-ret));
1539 }
1540 }
1541}
1542
1543
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001544static void wpa_driver_nl80211_rfkill_blocked(void *ctx)
1545{
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001546 struct wpa_driver_nl80211_data *drv = ctx;
1547
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001548 wpa_printf(MSG_DEBUG, "nl80211: RFKILL blocked");
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001549
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001550 /*
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001551 * rtnetlink ifdown handler will report interfaces other than the P2P
1552 * Device interface as disabled.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001553 */
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001554 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
1555 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_DISABLED, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001556}
1557
1558
1559static void wpa_driver_nl80211_rfkill_unblocked(void *ctx)
1560{
1561 struct wpa_driver_nl80211_data *drv = ctx;
1562 wpa_printf(MSG_DEBUG, "nl80211: RFKILL unblocked");
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001563 if (i802_set_iface_flags(drv->first_bss, 1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001564 wpa_printf(MSG_DEBUG, "nl80211: Could not set interface UP "
1565 "after rfkill unblock");
1566 return;
1567 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001568
1569 if (is_p2p_net_interface(drv->nlmode))
1570 nl80211_disable_11b_rates(drv, drv->ifindex, 1);
1571
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001572 /*
1573 * rtnetlink ifup handler will report interfaces other than the P2P
1574 * Device interface as enabled.
1575 */
1576 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
1577 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_ENABLED, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001578}
1579
1580
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001581static void wpa_driver_nl80211_handle_eapol_tx_status(int sock,
1582 void *eloop_ctx,
1583 void *handle)
1584{
1585 struct wpa_driver_nl80211_data *drv = eloop_ctx;
1586 u8 data[2048];
1587 struct msghdr msg;
1588 struct iovec entry;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001589 u8 control[512];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001590 struct cmsghdr *cmsg;
1591 int res, found_ee = 0, found_wifi = 0, acked = 0;
1592 union wpa_event_data event;
1593
1594 memset(&msg, 0, sizeof(msg));
1595 msg.msg_iov = &entry;
1596 msg.msg_iovlen = 1;
1597 entry.iov_base = data;
1598 entry.iov_len = sizeof(data);
1599 msg.msg_control = &control;
1600 msg.msg_controllen = sizeof(control);
1601
1602 res = recvmsg(sock, &msg, MSG_ERRQUEUE);
1603 /* if error or not fitting 802.3 header, return */
1604 if (res < 14)
1605 return;
1606
1607 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg))
1608 {
1609 if (cmsg->cmsg_level == SOL_SOCKET &&
1610 cmsg->cmsg_type == SCM_WIFI_STATUS) {
1611 int *ack;
1612
1613 found_wifi = 1;
1614 ack = (void *)CMSG_DATA(cmsg);
1615 acked = *ack;
1616 }
1617
1618 if (cmsg->cmsg_level == SOL_PACKET &&
1619 cmsg->cmsg_type == PACKET_TX_TIMESTAMP) {
1620 struct sock_extended_err *err =
1621 (struct sock_extended_err *)CMSG_DATA(cmsg);
1622
1623 if (err->ee_origin == SO_EE_ORIGIN_TXSTATUS)
1624 found_ee = 1;
1625 }
1626 }
1627
1628 if (!found_ee || !found_wifi)
1629 return;
1630
1631 memset(&event, 0, sizeof(event));
1632 event.eapol_tx_status.dst = data;
1633 event.eapol_tx_status.data = data + 14;
1634 event.eapol_tx_status.data_len = res - 14;
1635 event.eapol_tx_status.ack = acked;
1636 wpa_supplicant_event(drv->ctx, EVENT_EAPOL_TX_STATUS, &event);
1637}
1638
1639
1640static int nl80211_init_bss(struct i802_bss *bss)
1641{
1642 bss->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
1643 if (!bss->nl_cb)
1644 return -1;
1645
1646 nl_cb_set(bss->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
1647 no_seq_check, NULL);
1648 nl_cb_set(bss->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
1649 process_bss_event, bss);
1650
1651 return 0;
1652}
1653
1654
1655static void nl80211_destroy_bss(struct i802_bss *bss)
1656{
1657 nl_cb_put(bss->nl_cb);
1658 bss->nl_cb = NULL;
1659}
1660
1661
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001662static void
1663wpa_driver_nl80211_drv_init_rfkill(struct wpa_driver_nl80211_data *drv)
1664{
1665 struct rfkill_config *rcfg;
1666
1667 if (drv->rfkill)
1668 return;
1669
1670 rcfg = os_zalloc(sizeof(*rcfg));
1671 if (!rcfg)
1672 return;
1673
1674 rcfg->ctx = drv;
1675
1676 /* rfkill uses netdev sysfs for initialization. However, P2P Device is
1677 * not associated with a netdev, so use the name of some other interface
1678 * sharing the same wiphy as the P2P Device interface.
1679 *
1680 * Note: This is valid, as a P2P Device interface is always dynamically
1681 * created and is created only once another wpa_s interface was added.
1682 */
1683 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE) {
1684 struct nl80211_global *global = drv->global;
1685 struct wpa_driver_nl80211_data *tmp1;
1686
1687 dl_list_for_each(tmp1, &global->interfaces,
1688 struct wpa_driver_nl80211_data, list) {
1689 if (drv == tmp1 || drv->wiphy_idx != tmp1->wiphy_idx ||
1690 !tmp1->rfkill)
1691 continue;
1692
1693 wpa_printf(MSG_DEBUG,
1694 "nl80211: Use (%s) to initialize P2P Device rfkill",
1695 tmp1->first_bss->ifname);
1696 os_strlcpy(rcfg->ifname, tmp1->first_bss->ifname,
1697 sizeof(rcfg->ifname));
1698 break;
1699 }
1700 } else {
1701 os_strlcpy(rcfg->ifname, drv->first_bss->ifname,
1702 sizeof(rcfg->ifname));
1703 }
1704
1705 rcfg->blocked_cb = wpa_driver_nl80211_rfkill_blocked;
1706 rcfg->unblocked_cb = wpa_driver_nl80211_rfkill_unblocked;
1707 drv->rfkill = rfkill_init(rcfg);
1708 if (!drv->rfkill) {
1709 wpa_printf(MSG_DEBUG, "nl80211: RFKILL status not available");
1710 os_free(rcfg);
1711 }
1712}
1713
1714
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001715static void * wpa_driver_nl80211_drv_init(void *ctx, const char *ifname,
1716 void *global_priv, int hostapd,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001717 const u8 *set_addr,
1718 const char *driver_params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001719{
1720 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001721 struct i802_bss *bss;
1722
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001723 if (global_priv == NULL)
1724 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001725 drv = os_zalloc(sizeof(*drv));
1726 if (drv == NULL)
1727 return NULL;
1728 drv->global = global_priv;
1729 drv->ctx = ctx;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001730 drv->hostapd = !!hostapd;
1731 drv->eapol_sock = -1;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001732
1733 /*
1734 * There is no driver capability flag for this, so assume it is
1735 * supported and disable this on first attempt to use if the driver
1736 * rejects the command due to missing support.
1737 */
1738 drv->set_rekey_offload = 1;
1739
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001740 drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int);
1741 drv->if_indices = drv->default_if_indices;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001742 drv->if_indices_reason = drv->default_if_indices_reason;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001743
1744 drv->first_bss = os_zalloc(sizeof(*drv->first_bss));
1745 if (!drv->first_bss) {
1746 os_free(drv);
1747 return NULL;
1748 }
1749 bss = drv->first_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001750 bss->drv = drv;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001751 bss->ctx = ctx;
1752
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001753 os_strlcpy(bss->ifname, ifname, sizeof(bss->ifname));
1754 drv->monitor_ifidx = -1;
1755 drv->monitor_sock = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001756 drv->eapol_tx_sock = -1;
1757 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001758
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001759 if (nl80211_init_bss(bss))
1760 goto failed;
1761
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001762 if (wpa_driver_nl80211_finish_drv_init(drv, set_addr, 1, driver_params))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001763 goto failed;
1764
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001765 drv->eapol_tx_sock = socket(PF_PACKET, SOCK_DGRAM, 0);
1766 if (drv->eapol_tx_sock < 0)
1767 goto failed;
1768
1769 if (drv->data_tx_status) {
1770 int enabled = 1;
1771
1772 if (setsockopt(drv->eapol_tx_sock, SOL_SOCKET, SO_WIFI_STATUS,
1773 &enabled, sizeof(enabled)) < 0) {
1774 wpa_printf(MSG_DEBUG,
1775 "nl80211: wifi status sockopt failed\n");
1776 drv->data_tx_status = 0;
1777 if (!drv->use_monitor)
1778 drv->capa.flags &=
1779 ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
1780 } else {
1781 eloop_register_read_sock(drv->eapol_tx_sock,
1782 wpa_driver_nl80211_handle_eapol_tx_status,
1783 drv, NULL);
1784 }
1785 }
1786
1787 if (drv->global) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001788 nl80211_check_global(drv->global);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001789 dl_list_add(&drv->global->interfaces, &drv->list);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001790 drv->in_interface_list = 1;
1791 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001792
1793 return bss;
1794
1795failed:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001796 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001797 return NULL;
1798}
1799
1800
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001801/**
1802 * wpa_driver_nl80211_init - Initialize nl80211 driver interface
1803 * @ctx: context to be used when calling wpa_supplicant functions,
1804 * e.g., wpa_supplicant_event()
1805 * @ifname: interface name, e.g., wlan0
1806 * @global_priv: private driver global data from global_init()
1807 * Returns: Pointer to private data, %NULL on failure
1808 */
1809static void * wpa_driver_nl80211_init(void *ctx, const char *ifname,
1810 void *global_priv)
1811{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001812 return wpa_driver_nl80211_drv_init(ctx, ifname, global_priv, 0, NULL,
1813 NULL);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001814}
1815
1816
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001817static int nl80211_register_frame(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001818 struct nl_handle *nl_handle,
1819 u16 type, const u8 *match, size_t match_len)
1820{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001821 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001822 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001823 int ret;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001824 char buf[30];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001825
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001826 buf[0] = '\0';
1827 wpa_snprintf_hex(buf, sizeof(buf), match, match_len);
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07001828 wpa_printf(MSG_DEBUG, "nl80211: Register frame type=0x%x (%s) nl_handle=%p match=%s",
1829 type, fc2str(type), nl_handle, buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001830
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001831 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_REGISTER_ACTION)) ||
1832 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE, type) ||
1833 nla_put(msg, NL80211_ATTR_FRAME_MATCH, match_len, match)) {
1834 nlmsg_free(msg);
1835 return -1;
1836 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001837
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001838 ret = send_and_recv(drv->global, nl_handle, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001839 if (ret) {
1840 wpa_printf(MSG_DEBUG, "nl80211: Register frame command "
1841 "failed (type=%u): ret=%d (%s)",
1842 type, ret, strerror(-ret));
1843 wpa_hexdump(MSG_DEBUG, "nl80211: Register frame match",
1844 match, match_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001845 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001846 return ret;
1847}
1848
1849
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001850static int nl80211_alloc_mgmt_handle(struct i802_bss *bss)
1851{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001852 if (bss->nl_mgmt) {
1853 wpa_printf(MSG_DEBUG, "nl80211: Mgmt reporting "
1854 "already on! (nl_mgmt=%p)", bss->nl_mgmt);
1855 return -1;
1856 }
1857
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001858 bss->nl_mgmt = nl_create_handle(bss->nl_cb, "mgmt");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001859 if (bss->nl_mgmt == NULL)
1860 return -1;
1861
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001862 return 0;
1863}
1864
1865
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001866static void nl80211_mgmt_handle_register_eloop(struct i802_bss *bss)
1867{
1868 nl80211_register_eloop_read(&bss->nl_mgmt,
1869 wpa_driver_nl80211_event_receive,
1870 bss->nl_cb);
1871}
1872
1873
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001874static int nl80211_register_action_frame(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001875 const u8 *match, size_t match_len)
1876{
1877 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_ACTION << 4);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001878 return nl80211_register_frame(bss, bss->nl_mgmt,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001879 type, match, match_len);
1880}
1881
1882
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001883static int nl80211_mgmt_subscribe_non_ap(struct i802_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001884{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001885 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001886 int ret = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001887
1888 if (nl80211_alloc_mgmt_handle(bss))
1889 return -1;
1890 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with non-AP "
1891 "handle %p", bss->nl_mgmt);
1892
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001893 if (drv->nlmode == NL80211_IFTYPE_ADHOC) {
1894 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_AUTH << 4);
1895
1896 /* register for any AUTH message */
1897 nl80211_register_frame(bss, bss->nl_mgmt, type, NULL, 0);
1898 }
1899
Dmitry Shmidt051af732013-10-22 13:52:46 -07001900#ifdef CONFIG_INTERWORKING
1901 /* QoS Map Configure */
1902 if (nl80211_register_action_frame(bss, (u8 *) "\x01\x04", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001903 ret = -1;
Dmitry Shmidt051af732013-10-22 13:52:46 -07001904#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001905#if defined(CONFIG_P2P) || defined(CONFIG_INTERWORKING)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001906 /* GAS Initial Request */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001907 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0a", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001908 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001909 /* GAS Initial Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001910 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0b", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001911 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001912 /* GAS Comeback Request */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001913 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0c", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001914 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001915 /* GAS Comeback Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001916 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0d", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001917 ret = -1;
Dmitry Shmidt18463232014-01-24 12:29:41 -08001918 /* Protected GAS Initial Request */
1919 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0a", 2) < 0)
1920 ret = -1;
1921 /* Protected GAS Initial Response */
1922 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0b", 2) < 0)
1923 ret = -1;
1924 /* Protected GAS Comeback Request */
1925 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0c", 2) < 0)
1926 ret = -1;
1927 /* Protected GAS Comeback Response */
1928 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0d", 2) < 0)
1929 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001930#endif /* CONFIG_P2P || CONFIG_INTERWORKING */
1931#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001932 /* P2P Public Action */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001933 if (nl80211_register_action_frame(bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001934 (u8 *) "\x04\x09\x50\x6f\x9a\x09",
1935 6) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001936 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001937 /* P2P Action */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001938 if (nl80211_register_action_frame(bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001939 (u8 *) "\x7f\x50\x6f\x9a\x09",
1940 5) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001941 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001942#endif /* CONFIG_P2P */
1943#ifdef CONFIG_IEEE80211W
1944 /* SA Query Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001945 if (nl80211_register_action_frame(bss, (u8 *) "\x08\x01", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001946 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001947#endif /* CONFIG_IEEE80211W */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001948#ifdef CONFIG_TDLS
1949 if ((drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT)) {
1950 /* TDLS Discovery Response */
1951 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0e", 2) <
1952 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001953 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001954 }
1955#endif /* CONFIG_TDLS */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001956#ifdef CONFIG_FST
1957 /* FST Action frames */
1958 if (nl80211_register_action_frame(bss, (u8 *) "\x12", 1) < 0)
1959 ret = -1;
1960#endif /* CONFIG_FST */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001961
1962 /* FT Action frames */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001963 if (nl80211_register_action_frame(bss, (u8 *) "\x06", 1) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001964 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001965 else
1966 drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_FT |
1967 WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK;
1968
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001969 /* WNM - BSS Transition Management Request */
1970 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x07", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001971 ret = -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001972 /* WNM-Sleep Mode Response */
1973 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x11", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001974 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001975
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001976#ifdef CONFIG_HS20
1977 /* WNM-Notification */
1978 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x1a", 2) < 0)
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001979 ret = -1;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001980#endif /* CONFIG_HS20 */
1981
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001982 /* WMM-AC ADDTS Response */
1983 if (nl80211_register_action_frame(bss, (u8 *) "\x11\x01", 2) < 0)
1984 ret = -1;
1985
1986 /* WMM-AC DELTS */
1987 if (nl80211_register_action_frame(bss, (u8 *) "\x11\x02", 2) < 0)
1988 ret = -1;
1989
1990 /* Radio Measurement - Neighbor Report Response */
1991 if (nl80211_register_action_frame(bss, (u8 *) "\x05\x05", 2) < 0)
1992 ret = -1;
1993
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001994 /* Radio Measurement - Radio Measurement Request */
1995 if (nl80211_register_action_frame(bss, (u8 *) "\x05\x00", 2) < 0)
1996 ret = -1;
1997
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001998 /* Radio Measurement - Link Measurement Request */
1999 if ((drv->capa.rrm_flags & WPA_DRIVER_FLAGS_TX_POWER_INSERTION) &&
2000 (nl80211_register_action_frame(bss, (u8 *) "\x05\x02", 2) < 0))
2001 ret = -1;
2002
2003 nl80211_mgmt_handle_register_eloop(bss);
2004
2005 return ret;
2006}
2007
2008
2009static int nl80211_mgmt_subscribe_mesh(struct i802_bss *bss)
2010{
2011 int ret = 0;
2012
2013 if (nl80211_alloc_mgmt_handle(bss))
2014 return -1;
2015
2016 wpa_printf(MSG_DEBUG,
2017 "nl80211: Subscribe to mgmt frames with mesh handle %p",
2018 bss->nl_mgmt);
2019
2020 /* Auth frames for mesh SAE */
2021 if (nl80211_register_frame(bss, bss->nl_mgmt,
2022 (WLAN_FC_TYPE_MGMT << 2) |
2023 (WLAN_FC_STYPE_AUTH << 4),
2024 NULL, 0) < 0)
2025 ret = -1;
2026
2027 /* Mesh peering open */
2028 if (nl80211_register_action_frame(bss, (u8 *) "\x0f\x01", 2) < 0)
2029 ret = -1;
2030 /* Mesh peering confirm */
2031 if (nl80211_register_action_frame(bss, (u8 *) "\x0f\x02", 2) < 0)
2032 ret = -1;
2033 /* Mesh peering close */
2034 if (nl80211_register_action_frame(bss, (u8 *) "\x0f\x03", 2) < 0)
2035 ret = -1;
2036
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002037 nl80211_mgmt_handle_register_eloop(bss);
2038
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002039 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002040}
2041
2042
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002043static int nl80211_register_spurious_class3(struct i802_bss *bss)
2044{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002045 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002046 int ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002047
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002048 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_UNEXPECTED_FRAME);
2049 ret = send_and_recv(bss->drv->global, bss->nl_mgmt, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002050 if (ret) {
2051 wpa_printf(MSG_DEBUG, "nl80211: Register spurious class3 "
2052 "failed: ret=%d (%s)",
2053 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002054 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002055 return ret;
2056}
2057
2058
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002059static int nl80211_action_subscribe_ap(struct i802_bss *bss)
2060{
2061 int ret = 0;
2062
2063 /* Public Action frames */
2064 if (nl80211_register_action_frame(bss, (u8 *) "\x04", 1) < 0)
2065 ret = -1;
2066 /* RRM Measurement Report */
2067 if (nl80211_register_action_frame(bss, (u8 *) "\x05\x01", 2) < 0)
2068 ret = -1;
2069 /* RRM Neighbor Report Request */
2070 if (nl80211_register_action_frame(bss, (u8 *) "\x05\x04", 2) < 0)
2071 ret = -1;
2072 /* FT Action frames */
2073 if (nl80211_register_action_frame(bss, (u8 *) "\x06", 1) < 0)
2074 ret = -1;
2075#ifdef CONFIG_IEEE80211W
2076 /* SA Query */
2077 if (nl80211_register_action_frame(bss, (u8 *) "\x08", 1) < 0)
2078 ret = -1;
2079#endif /* CONFIG_IEEE80211W */
2080 /* Protected Dual of Public Action */
2081 if (nl80211_register_action_frame(bss, (u8 *) "\x09", 1) < 0)
2082 ret = -1;
2083 /* WNM */
2084 if (nl80211_register_action_frame(bss, (u8 *) "\x0a", 1) < 0)
2085 ret = -1;
2086 /* WMM */
2087 if (nl80211_register_action_frame(bss, (u8 *) "\x11", 1) < 0)
2088 ret = -1;
2089#ifdef CONFIG_FST
2090 /* FST Action frames */
2091 if (nl80211_register_action_frame(bss, (u8 *) "\x12", 1) < 0)
2092 ret = -1;
2093#endif /* CONFIG_FST */
2094 /* Vendor-specific */
2095 if (nl80211_register_action_frame(bss, (u8 *) "\x7f", 1) < 0)
2096 ret = -1;
2097
2098 return ret;
2099}
2100
2101
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002102static int nl80211_mgmt_subscribe_ap(struct i802_bss *bss)
2103{
2104 static const int stypes[] = {
2105 WLAN_FC_STYPE_AUTH,
2106 WLAN_FC_STYPE_ASSOC_REQ,
2107 WLAN_FC_STYPE_REASSOC_REQ,
2108 WLAN_FC_STYPE_DISASSOC,
2109 WLAN_FC_STYPE_DEAUTH,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002110 WLAN_FC_STYPE_PROBE_REQ,
2111/* Beacon doesn't work as mac80211 doesn't currently allow
2112 * it, but it wouldn't really be the right thing anyway as
2113 * it isn't per interface ... maybe just dump the scan
2114 * results periodically for OLBC?
2115 */
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07002116 /* WLAN_FC_STYPE_BEACON, */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002117 };
2118 unsigned int i;
2119
2120 if (nl80211_alloc_mgmt_handle(bss))
2121 return -1;
2122 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
2123 "handle %p", bss->nl_mgmt);
2124
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002125 for (i = 0; i < ARRAY_SIZE(stypes); i++) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002126 if (nl80211_register_frame(bss, bss->nl_mgmt,
2127 (WLAN_FC_TYPE_MGMT << 2) |
2128 (stypes[i] << 4),
2129 NULL, 0) < 0) {
2130 goto out_err;
2131 }
2132 }
2133
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002134 if (nl80211_action_subscribe_ap(bss))
2135 goto out_err;
2136
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002137 if (nl80211_register_spurious_class3(bss))
2138 goto out_err;
2139
2140 if (nl80211_get_wiphy_data_ap(bss) == NULL)
2141 goto out_err;
2142
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002143 nl80211_mgmt_handle_register_eloop(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002144 return 0;
2145
2146out_err:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002147 nl_destroy_handles(&bss->nl_mgmt);
2148 return -1;
2149}
2150
2151
2152static int nl80211_mgmt_subscribe_ap_dev_sme(struct i802_bss *bss)
2153{
2154 if (nl80211_alloc_mgmt_handle(bss))
2155 return -1;
2156 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
2157 "handle %p (device SME)", bss->nl_mgmt);
2158
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002159 if (nl80211_action_subscribe_ap(bss))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002160 goto out_err;
2161
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002162 nl80211_mgmt_handle_register_eloop(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002163 return 0;
2164
2165out_err:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002166 nl_destroy_handles(&bss->nl_mgmt);
2167 return -1;
2168}
2169
2170
2171static void nl80211_mgmt_unsubscribe(struct i802_bss *bss, const char *reason)
2172{
2173 if (bss->nl_mgmt == NULL)
2174 return;
2175 wpa_printf(MSG_DEBUG, "nl80211: Unsubscribe mgmt frames handle %p "
2176 "(%s)", bss->nl_mgmt, reason);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002177 nl80211_destroy_eloop_handle(&bss->nl_mgmt);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002178
2179 nl80211_put_wiphy_data_ap(bss);
2180}
2181
2182
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002183static void wpa_driver_nl80211_send_rfkill(void *eloop_ctx, void *timeout_ctx)
2184{
2185 wpa_supplicant_event(timeout_ctx, EVENT_INTERFACE_DISABLED, NULL);
2186}
2187
2188
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002189static void nl80211_del_p2pdev(struct i802_bss *bss)
2190{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002191 struct nl_msg *msg;
2192 int ret;
2193
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002194 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_DEL_INTERFACE);
2195 ret = send_and_recv_msgs(bss->drv, msg, NULL, NULL);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002196
2197 wpa_printf(MSG_DEBUG, "nl80211: Delete P2P Device %s (0x%llx): %s",
2198 bss->ifname, (long long unsigned int) bss->wdev_id,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002199 strerror(-ret));
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002200}
2201
2202
2203static int nl80211_set_p2pdev(struct i802_bss *bss, int start)
2204{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002205 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002206 int ret;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002207
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002208 msg = nl80211_cmd_msg(bss, 0, start ? NL80211_CMD_START_P2P_DEVICE :
2209 NL80211_CMD_STOP_P2P_DEVICE);
2210 ret = send_and_recv_msgs(bss->drv, msg, NULL, NULL);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002211
2212 wpa_printf(MSG_DEBUG, "nl80211: %s P2P Device %s (0x%llx): %s",
2213 start ? "Start" : "Stop",
2214 bss->ifname, (long long unsigned int) bss->wdev_id,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002215 strerror(-ret));
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002216 return ret;
2217}
2218
2219
2220static int i802_set_iface_flags(struct i802_bss *bss, int up)
2221{
2222 enum nl80211_iftype nlmode;
2223
2224 nlmode = nl80211_get_ifmode(bss);
2225 if (nlmode != NL80211_IFTYPE_P2P_DEVICE) {
2226 return linux_set_iface_flags(bss->drv->global->ioctl_sock,
2227 bss->ifname, up);
2228 }
2229
2230 /* P2P Device has start/stop which is equivalent */
2231 return nl80211_set_p2pdev(bss, up);
2232}
2233
2234
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002235#ifdef CONFIG_TESTING_OPTIONS
2236static int qca_vendor_test_cmd_handler(struct nl_msg *msg, void *arg)
2237{
2238 /* struct wpa_driver_nl80211_data *drv = arg; */
2239 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2240 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2241
2242
2243 wpa_printf(MSG_DEBUG,
2244 "nl80211: QCA vendor test command response received");
2245
2246 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2247 genlmsg_attrlen(gnlh, 0), NULL);
2248 if (!tb[NL80211_ATTR_VENDOR_DATA]) {
2249 wpa_printf(MSG_DEBUG, "nl80211: No vendor data attribute");
2250 return NL_SKIP;
2251 }
2252
2253 wpa_hexdump(MSG_DEBUG,
2254 "nl80211: Received QCA vendor test command response",
2255 nla_data(tb[NL80211_ATTR_VENDOR_DATA]),
2256 nla_len(tb[NL80211_ATTR_VENDOR_DATA]));
2257
2258 return NL_SKIP;
2259}
2260#endif /* CONFIG_TESTING_OPTIONS */
2261
2262
2263static void qca_vendor_test(struct wpa_driver_nl80211_data *drv)
2264{
2265#ifdef CONFIG_TESTING_OPTIONS
2266 struct nl_msg *msg;
2267 struct nlattr *params;
2268 int ret;
2269
2270 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
2271 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
2272 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
2273 QCA_NL80211_VENDOR_SUBCMD_TEST) ||
2274 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
2275 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_TEST, 123)) {
2276 nlmsg_free(msg);
2277 return;
2278 }
2279 nla_nest_end(msg, params);
2280
2281 ret = send_and_recv_msgs(drv, msg, qca_vendor_test_cmd_handler, drv);
2282 wpa_printf(MSG_DEBUG,
2283 "nl80211: QCA vendor test command returned %d (%s)",
2284 ret, strerror(-ret));
2285#endif /* CONFIG_TESTING_OPTIONS */
2286}
2287
2288
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002289static int
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002290wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002291 const u8 *set_addr, int first,
2292 const char *driver_params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002293{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002294 struct i802_bss *bss = drv->first_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002295 int send_rfkill_event = 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002296 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002297
2298 drv->ifindex = if_nametoindex(bss->ifname);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002299 bss->ifindex = drv->ifindex;
2300 bss->wdev_id = drv->global->if_add_wdevid;
2301 bss->wdev_id_set = drv->global->if_add_wdevid_set;
2302
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002303 bss->if_dynamic = drv->ifindex == drv->global->if_add_ifindex;
2304 bss->if_dynamic = bss->if_dynamic || drv->global->if_add_wdevid_set;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002305 drv->global->if_add_wdevid_set = 0;
2306
Dmitry Shmidt03658832014-08-13 11:03:49 -07002307 if (!bss->if_dynamic && nl80211_get_ifmode(bss) == NL80211_IFTYPE_AP)
2308 bss->static_ap = 1;
2309
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -08002310 if (first &&
2311 nl80211_get_ifmode(bss) != NL80211_IFTYPE_P2P_DEVICE &&
2312 linux_iface_up(drv->global->ioctl_sock, bss->ifname) > 0)
2313 drv->start_iface_up = 1;
2314
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002315 if (wpa_driver_nl80211_capa(drv))
2316 return -1;
2317
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002318 if (driver_params && nl80211_set_param(bss, driver_params) < 0)
2319 return -1;
2320
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002321 wpa_printf(MSG_DEBUG, "nl80211: interface %s in phy %s",
2322 bss->ifname, drv->phyname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002323
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002324 if (set_addr &&
2325 (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0) ||
2326 linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
2327 set_addr)))
2328 return -1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002329
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002330 if (first && nl80211_get_ifmode(bss) == NL80211_IFTYPE_AP)
2331 drv->start_mode_ap = 1;
2332
Dmitry Shmidt03658832014-08-13 11:03:49 -07002333 if (drv->hostapd || bss->static_ap)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002334 nlmode = NL80211_IFTYPE_AP;
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07002335 else if (bss->if_dynamic ||
2336 nl80211_get_ifmode(bss) == NL80211_IFTYPE_MESH_POINT)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002337 nlmode = nl80211_get_ifmode(bss);
2338 else
2339 nlmode = NL80211_IFTYPE_STATION;
2340
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002341 if (wpa_driver_nl80211_set_mode(bss, nlmode) < 0) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002342 wpa_printf(MSG_ERROR, "nl80211: Could not configure driver mode");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002343 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002344 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002345
Dmitry Shmidt98660862014-03-11 17:26:21 -07002346 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002347 nl80211_get_macaddr(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002348
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08002349 wpa_driver_nl80211_drv_init_rfkill(drv);
2350
Dmitry Shmidt98660862014-03-11 17:26:21 -07002351 if (!rfkill_is_blocked(drv->rfkill)) {
2352 int ret = i802_set_iface_flags(bss, 1);
2353 if (ret) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002354 wpa_printf(MSG_ERROR, "nl80211: Could not set "
2355 "interface '%s' UP", bss->ifname);
Dmitry Shmidt98660862014-03-11 17:26:21 -07002356 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002357 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002358
2359 if (is_p2p_net_interface(nlmode))
2360 nl80211_disable_11b_rates(bss->drv,
2361 bss->drv->ifindex, 1);
2362
Dmitry Shmidt98660862014-03-11 17:26:21 -07002363 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
2364 return ret;
2365 } else {
2366 wpa_printf(MSG_DEBUG, "nl80211: Could not yet enable "
2367 "interface '%s' due to rfkill", bss->ifname);
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08002368 if (nlmode != NL80211_IFTYPE_P2P_DEVICE)
2369 drv->if_disabled = 1;
2370
Dmitry Shmidt98660862014-03-11 17:26:21 -07002371 send_rfkill_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002372 }
2373
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08002374 if (!drv->hostapd && nlmode != NL80211_IFTYPE_P2P_DEVICE)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002375 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
2376 1, IF_OPER_DORMANT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002377
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08002378 if (nlmode != NL80211_IFTYPE_P2P_DEVICE) {
2379 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
2380 bss->addr))
2381 return -1;
2382 os_memcpy(drv->perm_addr, bss->addr, ETH_ALEN);
2383 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002384
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002385 if (send_rfkill_event) {
2386 eloop_register_timeout(0, 0, wpa_driver_nl80211_send_rfkill,
2387 drv, drv->ctx);
2388 }
2389
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002390 if (drv->vendor_cmd_test_avail)
2391 qca_vendor_test(drv);
2392
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002393 return 0;
2394}
2395
2396
2397static int wpa_driver_nl80211_del_beacon(struct wpa_driver_nl80211_data *drv)
2398{
2399 struct nl_msg *msg;
2400
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002401 wpa_printf(MSG_DEBUG, "nl80211: Remove beacon (ifindex=%d)",
2402 drv->ifindex);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002403 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_DEL_BEACON);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002404 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002405}
2406
2407
2408/**
2409 * wpa_driver_nl80211_deinit - Deinitialize nl80211 driver interface
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002410 * @bss: Pointer to private nl80211 data from wpa_driver_nl80211_init()
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002411 *
2412 * Shut down driver interface and processing of driver events. Free
2413 * private data buffer if one was allocated in wpa_driver_nl80211_init().
2414 */
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002415static void wpa_driver_nl80211_deinit(struct i802_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002416{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002417 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002418 unsigned int i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002419
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002420 wpa_printf(MSG_INFO, "nl80211: deinit ifname=%s disabled_11b_rates=%d",
2421 bss->ifname, drv->disabled_11b_rates);
2422
Dmitry Shmidt04949592012-07-19 12:16:46 -07002423 bss->in_deinit = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002424 if (drv->data_tx_status)
2425 eloop_unregister_read_sock(drv->eapol_tx_sock);
2426 if (drv->eapol_tx_sock >= 0)
2427 close(drv->eapol_tx_sock);
2428
2429 if (bss->nl_preq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002430 wpa_driver_nl80211_probe_req_report(bss, 0);
2431 if (bss->added_if_into_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002432 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
2433 bss->ifname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002434 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
2435 "interface %s from bridge %s: %s",
2436 bss->ifname, bss->brname, strerror(errno));
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002437 if (drv->rtnl_sk)
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07002438 nl80211_handle_destroy(drv->rtnl_sk);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002439 }
2440 if (bss->added_bridge) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002441 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->brname,
2442 0) < 0)
2443 wpa_printf(MSG_INFO,
2444 "nl80211: Could not set bridge %s down",
2445 bss->brname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002446 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002447 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
2448 "bridge %s: %s",
2449 bss->brname, strerror(errno));
2450 }
2451
2452 nl80211_remove_monitor_interface(drv);
2453
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002454 if (is_ap_interface(drv->nlmode))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002455 wpa_driver_nl80211_del_beacon(drv);
2456
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002457 if (drv->eapol_sock >= 0) {
2458 eloop_unregister_read_sock(drv->eapol_sock);
2459 close(drv->eapol_sock);
2460 }
2461
2462 if (drv->if_indices != drv->default_if_indices)
2463 os_free(drv->if_indices);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002464
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002465 if (drv->if_indices_reason != drv->default_if_indices_reason)
2466 os_free(drv->if_indices_reason);
2467
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002468 if (drv->disabled_11b_rates)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002469 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
2470
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002471 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, 0,
2472 IF_OPER_UP);
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07002473 eloop_cancel_timeout(wpa_driver_nl80211_send_rfkill, drv, drv->ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002474 rfkill_deinit(drv->rfkill);
2475
2476 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
2477
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002478 if (!drv->start_iface_up)
2479 (void) i802_set_iface_flags(bss, 0);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002480
2481 if (drv->addr_changed) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002482 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname,
2483 0) < 0) {
2484 wpa_printf(MSG_DEBUG,
2485 "nl80211: Could not set interface down to restore permanent MAC address");
2486 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002487 if (linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
2488 drv->perm_addr) < 0) {
2489 wpa_printf(MSG_DEBUG,
2490 "nl80211: Could not restore permanent MAC address");
2491 }
2492 }
2493
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002494 if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002495 if (!drv->hostapd || !drv->start_mode_ap)
2496 wpa_driver_nl80211_set_mode(bss,
2497 NL80211_IFTYPE_STATION);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002498 nl80211_mgmt_unsubscribe(bss, "deinit");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002499 } else {
2500 nl80211_mgmt_unsubscribe(bss, "deinit");
2501 nl80211_del_p2pdev(bss);
2502 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002503
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002504 nl80211_destroy_bss(drv->first_bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002505
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002506 os_free(drv->filter_ssids);
2507
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002508 os_free(drv->auth_ie);
2509
2510 if (drv->in_interface_list)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002511 dl_list_del(&drv->list);
2512
Dmitry Shmidt444d5672013-04-01 13:08:44 -07002513 os_free(drv->extended_capa);
2514 os_free(drv->extended_capa_mask);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002515 for (i = 0; i < drv->num_iface_ext_capa; i++) {
2516 os_free(drv->iface_ext_capa[i].ext_capa);
2517 os_free(drv->iface_ext_capa[i].ext_capa_mask);
2518 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002519 os_free(drv->first_bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002520 os_free(drv);
2521}
2522
2523
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002524static u32 wpa_alg_to_cipher_suite(enum wpa_alg alg, size_t key_len)
2525{
2526 switch (alg) {
2527 case WPA_ALG_WEP:
2528 if (key_len == 5)
2529 return WLAN_CIPHER_SUITE_WEP40;
2530 return WLAN_CIPHER_SUITE_WEP104;
2531 case WPA_ALG_TKIP:
2532 return WLAN_CIPHER_SUITE_TKIP;
2533 case WPA_ALG_CCMP:
2534 return WLAN_CIPHER_SUITE_CCMP;
2535 case WPA_ALG_GCMP:
2536 return WLAN_CIPHER_SUITE_GCMP;
2537 case WPA_ALG_CCMP_256:
2538 return WLAN_CIPHER_SUITE_CCMP_256;
2539 case WPA_ALG_GCMP_256:
2540 return WLAN_CIPHER_SUITE_GCMP_256;
2541 case WPA_ALG_IGTK:
2542 return WLAN_CIPHER_SUITE_AES_CMAC;
2543 case WPA_ALG_BIP_GMAC_128:
2544 return WLAN_CIPHER_SUITE_BIP_GMAC_128;
2545 case WPA_ALG_BIP_GMAC_256:
2546 return WLAN_CIPHER_SUITE_BIP_GMAC_256;
2547 case WPA_ALG_BIP_CMAC_256:
2548 return WLAN_CIPHER_SUITE_BIP_CMAC_256;
2549 case WPA_ALG_SMS4:
2550 return WLAN_CIPHER_SUITE_SMS4;
2551 case WPA_ALG_KRK:
2552 return WLAN_CIPHER_SUITE_KRK;
2553 case WPA_ALG_NONE:
2554 case WPA_ALG_PMK:
2555 wpa_printf(MSG_ERROR, "nl80211: Unexpected encryption algorithm %d",
2556 alg);
2557 return 0;
2558 }
2559
2560 wpa_printf(MSG_ERROR, "nl80211: Unsupported encryption algorithm %d",
2561 alg);
2562 return 0;
2563}
2564
2565
2566static u32 wpa_cipher_to_cipher_suite(unsigned int cipher)
2567{
2568 switch (cipher) {
2569 case WPA_CIPHER_CCMP_256:
2570 return WLAN_CIPHER_SUITE_CCMP_256;
2571 case WPA_CIPHER_GCMP_256:
2572 return WLAN_CIPHER_SUITE_GCMP_256;
2573 case WPA_CIPHER_CCMP:
2574 return WLAN_CIPHER_SUITE_CCMP;
2575 case WPA_CIPHER_GCMP:
2576 return WLAN_CIPHER_SUITE_GCMP;
2577 case WPA_CIPHER_TKIP:
2578 return WLAN_CIPHER_SUITE_TKIP;
2579 case WPA_CIPHER_WEP104:
2580 return WLAN_CIPHER_SUITE_WEP104;
2581 case WPA_CIPHER_WEP40:
2582 return WLAN_CIPHER_SUITE_WEP40;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002583 case WPA_CIPHER_GTK_NOT_USED:
2584 return WLAN_CIPHER_SUITE_NO_GROUP_ADDR;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002585 }
2586
2587 return 0;
2588}
2589
2590
2591static int wpa_cipher_to_cipher_suites(unsigned int ciphers, u32 suites[],
2592 int max_suites)
2593{
2594 int num_suites = 0;
2595
2596 if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP_256)
2597 suites[num_suites++] = WLAN_CIPHER_SUITE_CCMP_256;
2598 if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP_256)
2599 suites[num_suites++] = WLAN_CIPHER_SUITE_GCMP_256;
2600 if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP)
2601 suites[num_suites++] = WLAN_CIPHER_SUITE_CCMP;
2602 if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP)
2603 suites[num_suites++] = WLAN_CIPHER_SUITE_GCMP;
2604 if (num_suites < max_suites && ciphers & WPA_CIPHER_TKIP)
2605 suites[num_suites++] = WLAN_CIPHER_SUITE_TKIP;
2606 if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP104)
2607 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP104;
2608 if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP40)
2609 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP40;
2610
2611 return num_suites;
2612}
2613
2614
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002615#ifdef CONFIG_DRIVER_NL80211_QCA
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002616static int issue_key_mgmt_set_key(struct wpa_driver_nl80211_data *drv,
2617 const u8 *key, size_t key_len)
2618{
2619 struct nl_msg *msg;
2620 int ret;
2621
2622 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_KEY_MGMT_OFFLOAD))
2623 return 0;
2624
2625 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
2626 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
2627 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
2628 QCA_NL80211_VENDOR_SUBCMD_KEY_MGMT_SET_KEY) ||
2629 nla_put(msg, NL80211_ATTR_VENDOR_DATA, key_len, key)) {
2630 nl80211_nlmsg_clear(msg);
2631 nlmsg_free(msg);
2632 return -1;
2633 }
2634 ret = send_and_recv_msgs(drv, msg, NULL, (void *) -1);
2635 if (ret) {
2636 wpa_printf(MSG_DEBUG,
2637 "nl80211: Key management set key failed: ret=%d (%s)",
2638 ret, strerror(-ret));
2639 }
2640
2641 return ret;
2642}
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002643#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002644
2645
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002646static int wpa_driver_nl80211_set_key(const char *ifname, struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002647 enum wpa_alg alg, const u8 *addr,
2648 int key_idx, int set_tx,
2649 const u8 *seq, size_t seq_len,
2650 const u8 *key, size_t key_len)
2651{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002652 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002653 int ifindex;
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002654 struct nl_msg *msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002655 int ret;
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07002656 int tdls = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002657
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002658 /* Ignore for P2P Device */
2659 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
2660 return 0;
2661
2662 ifindex = if_nametoindex(ifname);
2663 wpa_printf(MSG_DEBUG, "%s: ifindex=%d (%s) alg=%d addr=%p key_idx=%d "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002664 "set_tx=%d seq_len=%lu key_len=%lu",
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002665 __func__, ifindex, ifname, alg, addr, key_idx, set_tx,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002666 (unsigned long) seq_len, (unsigned long) key_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002667#ifdef CONFIG_TDLS
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07002668 if (key_idx == -1) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002669 key_idx = 0;
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07002670 tdls = 1;
2671 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002672#endif /* CONFIG_TDLS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002673
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002674#ifdef CONFIG_DRIVER_NL80211_QCA
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002675 if (alg == WPA_ALG_PMK &&
2676 (drv->capa.flags & WPA_DRIVER_FLAGS_KEY_MGMT_OFFLOAD)) {
2677 wpa_printf(MSG_DEBUG, "%s: calling issue_key_mgmt_set_key",
2678 __func__);
2679 ret = issue_key_mgmt_set_key(drv, key, key_len);
2680 return ret;
2681 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002682#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002683
2684 if (alg == WPA_ALG_NONE) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002685 msg = nl80211_ifindex_msg(drv, ifindex, 0, NL80211_CMD_DEL_KEY);
2686 if (!msg)
2687 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002688 } else {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002689 u32 suite;
2690
2691 suite = wpa_alg_to_cipher_suite(alg, key_len);
2692 if (!suite)
2693 goto fail;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002694 msg = nl80211_ifindex_msg(drv, ifindex, 0, NL80211_CMD_NEW_KEY);
2695 if (!msg ||
2696 nla_put(msg, NL80211_ATTR_KEY_DATA, key_len, key) ||
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002697 nla_put_u32(msg, NL80211_ATTR_KEY_CIPHER, suite))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002698 goto fail;
Dmitry Shmidt98660862014-03-11 17:26:21 -07002699 wpa_hexdump_key(MSG_DEBUG, "nl80211: KEY_DATA", key, key_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002700 }
2701
Dmitry Shmidt98660862014-03-11 17:26:21 -07002702 if (seq && seq_len) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002703 if (nla_put(msg, NL80211_ATTR_KEY_SEQ, seq_len, seq))
2704 goto fail;
Dmitry Shmidt98660862014-03-11 17:26:21 -07002705 wpa_hexdump(MSG_DEBUG, "nl80211: KEY_SEQ", seq, seq_len);
2706 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002707
2708 if (addr && !is_broadcast_ether_addr(addr)) {
2709 wpa_printf(MSG_DEBUG, " addr=" MACSTR, MAC2STR(addr));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002710 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
2711 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002712
2713 if (alg != WPA_ALG_WEP && key_idx && !set_tx) {
2714 wpa_printf(MSG_DEBUG, " RSN IBSS RX GTK");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002715 if (nla_put_u32(msg, NL80211_ATTR_KEY_TYPE,
2716 NL80211_KEYTYPE_GROUP))
2717 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002718 }
2719 } else if (addr && is_broadcast_ether_addr(addr)) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002720 struct nlattr *types;
2721
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002722 wpa_printf(MSG_DEBUG, " broadcast key");
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002723
2724 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002725 if (!types ||
2726 nla_put_flag(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST))
2727 goto fail;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002728 nla_nest_end(msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002729 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002730 if (nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
2731 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002732
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002733 ret = send_and_recv_msgs(drv, msg, NULL, key ? (void *) -1 : NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002734 if ((ret == -ENOENT || ret == -ENOLINK) && alg == WPA_ALG_NONE)
2735 ret = 0;
2736 if (ret)
2737 wpa_printf(MSG_DEBUG, "nl80211: set_key failed; err=%d %s)",
2738 ret, strerror(-ret));
2739
2740 /*
2741 * If we failed or don't need to set the default TX key (below),
2742 * we're done here.
2743 */
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07002744 if (ret || !set_tx || alg == WPA_ALG_NONE || tdls)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002745 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002746 if (is_ap_interface(drv->nlmode) && addr &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002747 !is_broadcast_ether_addr(addr))
2748 return ret;
2749
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002750 msg = nl80211_ifindex_msg(drv, ifindex, 0, NL80211_CMD_SET_KEY);
2751 if (!msg ||
2752 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx) ||
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002753 nla_put_flag(msg, (alg == WPA_ALG_IGTK ||
2754 alg == WPA_ALG_BIP_GMAC_128 ||
2755 alg == WPA_ALG_BIP_GMAC_256 ||
2756 alg == WPA_ALG_BIP_CMAC_256) ?
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002757 NL80211_ATTR_KEY_DEFAULT_MGMT :
2758 NL80211_ATTR_KEY_DEFAULT))
2759 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002760 if (addr && is_broadcast_ether_addr(addr)) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002761 struct nlattr *types;
2762
2763 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002764 if (!types ||
2765 nla_put_flag(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST))
2766 goto fail;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002767 nla_nest_end(msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002768 } else if (addr) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002769 struct nlattr *types;
2770
2771 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002772 if (!types ||
2773 nla_put_flag(msg, NL80211_KEY_DEFAULT_TYPE_UNICAST))
2774 goto fail;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002775 nla_nest_end(msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002776 }
2777
2778 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
2779 if (ret == -ENOENT)
2780 ret = 0;
2781 if (ret)
2782 wpa_printf(MSG_DEBUG, "nl80211: set_key default failed; "
2783 "err=%d %s)", ret, strerror(-ret));
2784 return ret;
2785
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002786fail:
2787 nl80211_nlmsg_clear(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002788 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002789 return -ENOBUFS;
2790}
2791
2792
2793static int nl_add_key(struct nl_msg *msg, enum wpa_alg alg,
2794 int key_idx, int defkey,
2795 const u8 *seq, size_t seq_len,
2796 const u8 *key, size_t key_len)
2797{
2798 struct nlattr *key_attr = nla_nest_start(msg, NL80211_ATTR_KEY);
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002799 u32 suite;
2800
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002801 if (!key_attr)
2802 return -1;
2803
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002804 suite = wpa_alg_to_cipher_suite(alg, key_len);
2805 if (!suite)
2806 return -1;
2807
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002808 if (defkey && alg == WPA_ALG_IGTK) {
2809 if (nla_put_flag(msg, NL80211_KEY_DEFAULT_MGMT))
2810 return -1;
2811 } else if (defkey) {
2812 if (nla_put_flag(msg, NL80211_KEY_DEFAULT))
2813 return -1;
2814 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002815
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002816 if (nla_put_u8(msg, NL80211_KEY_IDX, key_idx) ||
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002817 nla_put_u32(msg, NL80211_KEY_CIPHER, suite) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002818 (seq && seq_len &&
2819 nla_put(msg, NL80211_KEY_SEQ, seq_len, seq)) ||
2820 nla_put(msg, NL80211_KEY_DATA, key_len, key))
2821 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002822
2823 nla_nest_end(msg, key_attr);
2824
2825 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002826}
2827
2828
2829static int nl80211_set_conn_keys(struct wpa_driver_associate_params *params,
2830 struct nl_msg *msg)
2831{
2832 int i, privacy = 0;
2833 struct nlattr *nl_keys, *nl_key;
2834
2835 for (i = 0; i < 4; i++) {
2836 if (!params->wep_key[i])
2837 continue;
2838 privacy = 1;
2839 break;
2840 }
2841 if (params->wps == WPS_MODE_PRIVACY)
2842 privacy = 1;
2843 if (params->pairwise_suite &&
2844 params->pairwise_suite != WPA_CIPHER_NONE)
2845 privacy = 1;
2846
2847 if (!privacy)
2848 return 0;
2849
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002850 if (nla_put_flag(msg, NL80211_ATTR_PRIVACY))
2851 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002852
2853 nl_keys = nla_nest_start(msg, NL80211_ATTR_KEYS);
2854 if (!nl_keys)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002855 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002856
2857 for (i = 0; i < 4; i++) {
2858 if (!params->wep_key[i])
2859 continue;
2860
2861 nl_key = nla_nest_start(msg, i);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002862 if (!nl_key ||
2863 nla_put(msg, NL80211_KEY_DATA, params->wep_key_len[i],
2864 params->wep_key[i]) ||
2865 nla_put_u32(msg, NL80211_KEY_CIPHER,
2866 params->wep_key_len[i] == 5 ?
2867 WLAN_CIPHER_SUITE_WEP40 :
2868 WLAN_CIPHER_SUITE_WEP104) ||
2869 nla_put_u8(msg, NL80211_KEY_IDX, i) ||
2870 (i == params->wep_tx_keyidx &&
2871 nla_put_flag(msg, NL80211_KEY_DEFAULT)))
2872 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002873
2874 nla_nest_end(msg, nl_key);
2875 }
2876 nla_nest_end(msg, nl_keys);
2877
2878 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002879}
2880
2881
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002882int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
2883 const u8 *addr, int cmd, u16 reason_code,
2884 int local_state_change)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002885{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002886 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002887 struct nl_msg *msg;
2888
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002889 if (!(msg = nl80211_drv_msg(drv, 0, cmd)) ||
2890 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code) ||
2891 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
2892 (local_state_change &&
2893 nla_put_flag(msg, NL80211_ATTR_LOCAL_STATE_CHANGE))) {
2894 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002895 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002896 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002897
2898 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002899 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002900 wpa_dbg(drv->ctx, MSG_DEBUG,
2901 "nl80211: MLME command failed: reason=%u ret=%d (%s)",
2902 reason_code, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002903 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002904 return ret;
2905}
2906
2907
2908static int wpa_driver_nl80211_disconnect(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002909 int reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002910{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002911 int ret;
2912
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002913 wpa_printf(MSG_DEBUG, "%s(reason_code=%d)", __func__, reason_code);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07002914 nl80211_mark_disconnected(drv);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002915 /* Disconnect command doesn't need BSSID - it uses cached value */
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002916 ret = wpa_driver_nl80211_mlme(drv, NULL, NL80211_CMD_DISCONNECT,
2917 reason_code, 0);
2918 /*
2919 * For locally generated disconnect, supplicant already generates a
2920 * DEAUTH event, so ignore the event from NL80211.
2921 */
2922 drv->ignore_next_local_disconnect = ret == 0;
2923
2924 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002925}
2926
2927
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002928static int wpa_driver_nl80211_deauthenticate(struct i802_bss *bss,
2929 const u8 *addr, int reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002930{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002931 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07002932 int ret;
Dmitry Shmidt413dde72014-04-11 10:23:22 -07002933
2934 if (drv->nlmode == NL80211_IFTYPE_ADHOC) {
2935 nl80211_mark_disconnected(drv);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002936 return nl80211_leave_ibss(drv, 1);
Dmitry Shmidt413dde72014-04-11 10:23:22 -07002937 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002938 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME))
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002939 return wpa_driver_nl80211_disconnect(drv, reason_code);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002940 wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " reason_code=%d)",
2941 __func__, MAC2STR(addr), reason_code);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07002942 nl80211_mark_disconnected(drv);
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07002943 ret = wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DEAUTHENTICATE,
2944 reason_code, 0);
2945 /*
2946 * For locally generated deauthenticate, supplicant already generates a
2947 * DEAUTH event, so ignore the event from NL80211.
2948 */
2949 drv->ignore_next_local_deauth = ret == 0;
2950 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002951}
2952
2953
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002954static void nl80211_copy_auth_params(struct wpa_driver_nl80211_data *drv,
2955 struct wpa_driver_auth_params *params)
2956{
2957 int i;
2958
2959 drv->auth_freq = params->freq;
2960 drv->auth_alg = params->auth_alg;
2961 drv->auth_wep_tx_keyidx = params->wep_tx_keyidx;
2962 drv->auth_local_state_change = params->local_state_change;
2963 drv->auth_p2p = params->p2p;
2964
2965 if (params->bssid)
2966 os_memcpy(drv->auth_bssid_, params->bssid, ETH_ALEN);
2967 else
2968 os_memset(drv->auth_bssid_, 0, ETH_ALEN);
2969
2970 if (params->ssid) {
2971 os_memcpy(drv->auth_ssid, params->ssid, params->ssid_len);
2972 drv->auth_ssid_len = params->ssid_len;
2973 } else
2974 drv->auth_ssid_len = 0;
2975
2976
2977 os_free(drv->auth_ie);
2978 drv->auth_ie = NULL;
2979 drv->auth_ie_len = 0;
2980 if (params->ie) {
2981 drv->auth_ie = os_malloc(params->ie_len);
2982 if (drv->auth_ie) {
2983 os_memcpy(drv->auth_ie, params->ie, params->ie_len);
2984 drv->auth_ie_len = params->ie_len;
2985 }
2986 }
2987
2988 for (i = 0; i < 4; i++) {
2989 if (params->wep_key[i] && params->wep_key_len[i] &&
2990 params->wep_key_len[i] <= 16) {
2991 os_memcpy(drv->auth_wep_key[i], params->wep_key[i],
2992 params->wep_key_len[i]);
2993 drv->auth_wep_key_len[i] = params->wep_key_len[i];
2994 } else
2995 drv->auth_wep_key_len[i] = 0;
2996 }
2997}
2998
2999
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003000static void nl80211_unmask_11b_rates(struct i802_bss *bss)
3001{
3002 struct wpa_driver_nl80211_data *drv = bss->drv;
3003
3004 if (is_p2p_net_interface(drv->nlmode) || !drv->disabled_11b_rates)
3005 return;
3006
3007 /*
3008 * Looks like we failed to unmask 11b rates previously. This could
3009 * happen, e.g., if the interface was down at the point in time when a
3010 * P2P group was terminated.
3011 */
3012 wpa_printf(MSG_DEBUG,
3013 "nl80211: Interface %s mode is for non-P2P, but 11b rates were disabled - re-enable them",
3014 bss->ifname);
3015 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
3016}
3017
3018
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003019static int wpa_driver_nl80211_authenticate(
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003020 struct i802_bss *bss, struct wpa_driver_auth_params *params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003021{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003022 struct wpa_driver_nl80211_data *drv = bss->drv;
3023 int ret = -1, i;
3024 struct nl_msg *msg;
3025 enum nl80211_auth_type type;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003026 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003027 int count = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003028 int is_retry;
3029
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003030 nl80211_unmask_11b_rates(bss);
3031
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003032 is_retry = drv->retry_auth;
3033 drv->retry_auth = 0;
Dmitry Shmidt98660862014-03-11 17:26:21 -07003034 drv->ignore_deauth_event = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003035
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003036 nl80211_mark_disconnected(drv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003037 os_memset(drv->auth_bssid, 0, ETH_ALEN);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003038 if (params->bssid)
3039 os_memcpy(drv->auth_attempt_bssid, params->bssid, ETH_ALEN);
3040 else
3041 os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003042 /* FIX: IBSS mode */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003043 nlmode = params->p2p ?
3044 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
3045 if (drv->nlmode != nlmode &&
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003046 wpa_driver_nl80211_set_mode(bss, nlmode) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003047 return -1;
3048
3049retry:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003050 wpa_printf(MSG_DEBUG, "nl80211: Authenticate (ifindex=%d)",
3051 drv->ifindex);
3052
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003053 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_AUTHENTICATE);
3054 if (!msg)
3055 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003056
3057 for (i = 0; i < 4; i++) {
3058 if (!params->wep_key[i])
3059 continue;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003060 wpa_driver_nl80211_set_key(bss->ifname, bss, WPA_ALG_WEP,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003061 NULL, i,
3062 i == params->wep_tx_keyidx, NULL, 0,
3063 params->wep_key[i],
3064 params->wep_key_len[i]);
3065 if (params->wep_tx_keyidx != i)
3066 continue;
3067 if (nl_add_key(msg, WPA_ALG_WEP, i, 1, NULL, 0,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003068 params->wep_key[i], params->wep_key_len[i]))
3069 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003070 }
3071
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003072 if (params->bssid) {
3073 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
3074 MAC2STR(params->bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003075 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid))
3076 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003077 }
3078 if (params->freq) {
3079 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003080 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq))
3081 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003082 }
3083 if (params->ssid) {
3084 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
3085 params->ssid, params->ssid_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003086 if (nla_put(msg, NL80211_ATTR_SSID, params->ssid_len,
3087 params->ssid))
3088 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003089 }
3090 wpa_hexdump(MSG_DEBUG, " * IEs", params->ie, params->ie_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003091 if (params->ie &&
3092 nla_put(msg, NL80211_ATTR_IE, params->ie_len, params->ie))
3093 goto fail;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003094 if (params->sae_data) {
3095 wpa_hexdump(MSG_DEBUG, " * SAE data", params->sae_data,
3096 params->sae_data_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003097 if (nla_put(msg, NL80211_ATTR_SAE_DATA, params->sae_data_len,
3098 params->sae_data))
3099 goto fail;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003100 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003101 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
3102 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
3103 else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
3104 type = NL80211_AUTHTYPE_SHARED_KEY;
3105 else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
3106 type = NL80211_AUTHTYPE_NETWORK_EAP;
3107 else if (params->auth_alg & WPA_AUTH_ALG_FT)
3108 type = NL80211_AUTHTYPE_FT;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003109 else if (params->auth_alg & WPA_AUTH_ALG_SAE)
3110 type = NL80211_AUTHTYPE_SAE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003111 else
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003112 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003113 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003114 if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE, type))
3115 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003116 if (params->local_state_change) {
3117 wpa_printf(MSG_DEBUG, " * Local state change only");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003118 if (nla_put_flag(msg, NL80211_ATTR_LOCAL_STATE_CHANGE))
3119 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003120 }
3121
3122 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3123 msg = NULL;
3124 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003125 wpa_dbg(drv->ctx, MSG_DEBUG,
3126 "nl80211: MLME command failed (auth): ret=%d (%s)",
3127 ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003128 count++;
3129 if (ret == -EALREADY && count == 1 && params->bssid &&
3130 !params->local_state_change) {
3131 /*
3132 * mac80211 does not currently accept new
3133 * authentication if we are already authenticated. As a
3134 * workaround, force deauthentication and try again.
3135 */
3136 wpa_printf(MSG_DEBUG, "nl80211: Retry authentication "
3137 "after forced deauthentication");
Dmitry Shmidt98660862014-03-11 17:26:21 -07003138 drv->ignore_deauth_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003139 wpa_driver_nl80211_deauthenticate(
3140 bss, params->bssid,
3141 WLAN_REASON_PREV_AUTH_NOT_VALID);
3142 nlmsg_free(msg);
3143 goto retry;
3144 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003145
3146 if (ret == -ENOENT && params->freq && !is_retry) {
3147 /*
3148 * cfg80211 has likely expired the BSS entry even
3149 * though it was previously available in our internal
3150 * BSS table. To recover quickly, start a single
3151 * channel scan on the specified channel.
3152 */
3153 struct wpa_driver_scan_params scan;
3154 int freqs[2];
3155
3156 os_memset(&scan, 0, sizeof(scan));
3157 scan.num_ssids = 1;
3158 if (params->ssid) {
3159 scan.ssids[0].ssid = params->ssid;
3160 scan.ssids[0].ssid_len = params->ssid_len;
3161 }
3162 freqs[0] = params->freq;
3163 freqs[1] = 0;
3164 scan.freqs = freqs;
3165 wpa_printf(MSG_DEBUG, "nl80211: Trigger single "
3166 "channel scan to refresh cfg80211 BSS "
3167 "entry");
3168 ret = wpa_driver_nl80211_scan(bss, &scan);
3169 if (ret == 0) {
3170 nl80211_copy_auth_params(drv, params);
3171 drv->scan_for_auth = 1;
3172 }
3173 } else if (is_retry) {
3174 /*
3175 * Need to indicate this with an event since the return
3176 * value from the retry is not delivered to core code.
3177 */
3178 union wpa_event_data event;
3179 wpa_printf(MSG_DEBUG, "nl80211: Authentication retry "
3180 "failed");
3181 os_memset(&event, 0, sizeof(event));
3182 os_memcpy(event.timeout_event.addr, drv->auth_bssid_,
3183 ETH_ALEN);
3184 wpa_supplicant_event(drv->ctx, EVENT_AUTH_TIMED_OUT,
3185 &event);
3186 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003187 } else {
3188 wpa_printf(MSG_DEBUG,
3189 "nl80211: Authentication request send successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003190 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003191
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003192fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003193 nlmsg_free(msg);
3194 return ret;
3195}
3196
3197
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003198int wpa_driver_nl80211_authenticate_retry(struct wpa_driver_nl80211_data *drv)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003199{
3200 struct wpa_driver_auth_params params;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003201 struct i802_bss *bss = drv->first_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003202 int i;
3203
3204 wpa_printf(MSG_DEBUG, "nl80211: Try to authenticate again");
3205
3206 os_memset(&params, 0, sizeof(params));
3207 params.freq = drv->auth_freq;
3208 params.auth_alg = drv->auth_alg;
3209 params.wep_tx_keyidx = drv->auth_wep_tx_keyidx;
3210 params.local_state_change = drv->auth_local_state_change;
3211 params.p2p = drv->auth_p2p;
3212
3213 if (!is_zero_ether_addr(drv->auth_bssid_))
3214 params.bssid = drv->auth_bssid_;
3215
3216 if (drv->auth_ssid_len) {
3217 params.ssid = drv->auth_ssid;
3218 params.ssid_len = drv->auth_ssid_len;
3219 }
3220
3221 params.ie = drv->auth_ie;
3222 params.ie_len = drv->auth_ie_len;
3223
3224 for (i = 0; i < 4; i++) {
3225 if (drv->auth_wep_key_len[i]) {
3226 params.wep_key[i] = drv->auth_wep_key[i];
3227 params.wep_key_len[i] = drv->auth_wep_key_len[i];
3228 }
3229 }
3230
3231 drv->retry_auth = 1;
3232 return wpa_driver_nl80211_authenticate(bss, &params);
3233}
3234
3235
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003236static int wpa_driver_nl80211_send_frame(struct i802_bss *bss,
3237 const void *data, size_t len,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003238 int encrypt, int noack,
3239 unsigned int freq, int no_cck,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003240 int offchanok, unsigned int wait_time,
3241 const u16 *csa_offs,
3242 size_t csa_offs_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003243{
3244 struct wpa_driver_nl80211_data *drv = bss->drv;
3245 u64 cookie;
Dmitry Shmidt051af732013-10-22 13:52:46 -07003246 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003247
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07003248 if (freq == 0 && drv->nlmode == NL80211_IFTYPE_ADHOC) {
3249 freq = nl80211_get_assoc_freq(drv);
3250 wpa_printf(MSG_DEBUG,
3251 "nl80211: send_frame - Use assoc_freq=%u for IBSS",
3252 freq);
3253 }
Dmitry Shmidt56052862013-10-04 10:23:25 -07003254 if (freq == 0) {
3255 wpa_printf(MSG_DEBUG, "nl80211: send_frame - Use bss->freq=%u",
3256 bss->freq);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003257 freq = bss->freq;
Dmitry Shmidt56052862013-10-04 10:23:25 -07003258 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003259
Dmitry Shmidt56052862013-10-04 10:23:25 -07003260 if (drv->use_monitor) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003261 wpa_printf(MSG_DEBUG, "nl80211: send_frame(freq=%u bss->freq=%u) -> send_monitor",
Dmitry Shmidt56052862013-10-04 10:23:25 -07003262 freq, bss->freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003263 return nl80211_send_monitor(drv, data, len, encrypt, noack);
Dmitry Shmidt56052862013-10-04 10:23:25 -07003264 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003265
Dmitry Shmidt56052862013-10-04 10:23:25 -07003266 wpa_printf(MSG_DEBUG, "nl80211: send_frame -> send_frame_cmd");
Dmitry Shmidt051af732013-10-22 13:52:46 -07003267 res = nl80211_send_frame_cmd(bss, freq, wait_time, data, len,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003268 &cookie, no_cck, noack, offchanok,
3269 csa_offs, csa_offs_len);
Dmitry Shmidt051af732013-10-22 13:52:46 -07003270 if (res == 0 && !noack) {
3271 const struct ieee80211_mgmt *mgmt;
3272 u16 fc;
3273
3274 mgmt = (const struct ieee80211_mgmt *) data;
3275 fc = le_to_host16(mgmt->frame_control);
3276 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
3277 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_ACTION) {
3278 wpa_printf(MSG_MSGDUMP,
3279 "nl80211: Update send_action_cookie from 0x%llx to 0x%llx",
3280 (long long unsigned int)
3281 drv->send_action_cookie,
3282 (long long unsigned int) cookie);
3283 drv->send_action_cookie = cookie;
3284 }
3285 }
3286
3287 return res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003288}
3289
3290
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003291static int wpa_driver_nl80211_send_mlme(struct i802_bss *bss, const u8 *data,
3292 size_t data_len, int noack,
3293 unsigned int freq, int no_cck,
3294 int offchanok,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003295 unsigned int wait_time,
3296 const u16 *csa_offs,
3297 size_t csa_offs_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003298{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003299 struct wpa_driver_nl80211_data *drv = bss->drv;
3300 struct ieee80211_mgmt *mgmt;
3301 int encrypt = 1;
3302 u16 fc;
3303
3304 mgmt = (struct ieee80211_mgmt *) data;
3305 fc = le_to_host16(mgmt->frame_control);
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07003306 wpa_printf(MSG_DEBUG, "nl80211: send_mlme - da= " MACSTR
3307 " noack=%d freq=%u no_cck=%d offchanok=%d wait_time=%u fc=0x%x (%s) nlmode=%d",
3308 MAC2STR(mgmt->da), noack, freq, no_cck, offchanok, wait_time,
3309 fc, fc2str(fc), drv->nlmode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003310
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003311 if ((is_sta_interface(drv->nlmode) ||
3312 drv->nlmode == NL80211_IFTYPE_P2P_DEVICE) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003313 WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
3314 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_RESP) {
3315 /*
3316 * The use of last_mgmt_freq is a bit of a hack,
3317 * but it works due to the single-threaded nature
3318 * of wpa_supplicant.
3319 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07003320 if (freq == 0) {
3321 wpa_printf(MSG_DEBUG, "nl80211: Use last_mgmt_freq=%d",
3322 drv->last_mgmt_freq);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003323 freq = drv->last_mgmt_freq;
Dmitry Shmidt56052862013-10-04 10:23:25 -07003324 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003325 return nl80211_send_frame_cmd(bss, freq, 0,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003326 data, data_len, NULL, 1, noack,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003327 1, csa_offs, csa_offs_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003328 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003329
3330 if (drv->device_ap_sme && is_ap_interface(drv->nlmode)) {
Dmitry Shmidt56052862013-10-04 10:23:25 -07003331 if (freq == 0) {
3332 wpa_printf(MSG_DEBUG, "nl80211: Use bss->freq=%d",
3333 bss->freq);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003334 freq = bss->freq;
Dmitry Shmidt56052862013-10-04 10:23:25 -07003335 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003336 return nl80211_send_frame_cmd(bss, freq,
3337 (int) freq == bss->freq ? 0 :
3338 wait_time,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003339 data, data_len,
3340 &drv->send_action_cookie,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003341 no_cck, noack, offchanok,
3342 csa_offs, csa_offs_len);
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07003343 }
Dmitry Shmidtb638fe72012-03-20 12:51:25 -07003344
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003345 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
3346 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) {
3347 /*
3348 * Only one of the authentication frame types is encrypted.
3349 * In order for static WEP encryption to work properly (i.e.,
3350 * to not encrypt the frame), we need to tell mac80211 about
3351 * the frames that must not be encrypted.
3352 */
3353 u16 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
3354 u16 auth_trans = le_to_host16(mgmt->u.auth.auth_transaction);
3355 if (auth_alg != WLAN_AUTH_SHARED_KEY || auth_trans != 3)
3356 encrypt = 0;
3357 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003358
Dmitry Shmidt56052862013-10-04 10:23:25 -07003359 wpa_printf(MSG_DEBUG, "nl80211: send_mlme -> send_frame");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003360 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003361 noack, freq, no_cck, offchanok,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003362 wait_time, csa_offs,
3363 csa_offs_len);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003364}
3365
3366
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003367static int nl80211_put_basic_rates(struct nl_msg *msg, const int *basic_rates)
3368{
3369 u8 rates[NL80211_MAX_SUPP_RATES];
3370 u8 rates_len = 0;
3371 int i;
3372
3373 if (!basic_rates)
3374 return 0;
3375
3376 for (i = 0; i < NL80211_MAX_SUPP_RATES && basic_rates[i] >= 0; i++)
3377 rates[rates_len++] = basic_rates[i] / 5;
3378
3379 return nla_put(msg, NL80211_ATTR_BSS_BASIC_RATES, rates_len, rates);
3380}
3381
3382
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003383static int nl80211_set_bss(struct i802_bss *bss, int cts, int preamble,
3384 int slot, int ht_opmode, int ap_isolate,
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003385 const int *basic_rates)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003386{
3387 struct wpa_driver_nl80211_data *drv = bss->drv;
3388 struct nl_msg *msg;
3389
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003390 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_BSS)) ||
3391 (cts >= 0 &&
3392 nla_put_u8(msg, NL80211_ATTR_BSS_CTS_PROT, cts)) ||
3393 (preamble >= 0 &&
3394 nla_put_u8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble)) ||
3395 (slot >= 0 &&
3396 nla_put_u8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot)) ||
3397 (ht_opmode >= 0 &&
3398 nla_put_u16(msg, NL80211_ATTR_BSS_HT_OPMODE, ht_opmode)) ||
3399 (ap_isolate >= 0 &&
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003400 nla_put_u8(msg, NL80211_ATTR_AP_ISOLATE, ap_isolate)) ||
3401 nl80211_put_basic_rates(msg, basic_rates)) {
3402 nlmsg_free(msg);
3403 return -ENOBUFS;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003404 }
3405
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003406 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003407}
3408
3409
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003410static int wpa_driver_nl80211_set_acl(void *priv,
3411 struct hostapd_acl_params *params)
3412{
3413 struct i802_bss *bss = priv;
3414 struct wpa_driver_nl80211_data *drv = bss->drv;
3415 struct nl_msg *msg;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003416 struct nl_msg *acl;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003417 unsigned int i;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003418 int ret;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003419
3420 if (!(drv->capa.max_acl_mac_addrs))
3421 return -ENOTSUP;
3422
3423 if (params->num_mac_acl > drv->capa.max_acl_mac_addrs)
3424 return -ENOTSUP;
3425
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003426 wpa_printf(MSG_DEBUG, "nl80211: Set %s ACL (num_mac_acl=%u)",
3427 params->acl_policy ? "Accept" : "Deny", params->num_mac_acl);
3428
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003429 acl = nlmsg_alloc();
3430 if (!acl)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003431 return -ENOMEM;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003432 for (i = 0; i < params->num_mac_acl; i++) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003433 if (nla_put(acl, i + 1, ETH_ALEN, params->mac_acl[i].addr)) {
3434 nlmsg_free(acl);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003435 return -ENOMEM;
3436 }
3437 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003438
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003439 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_MAC_ACL)) ||
3440 nla_put_u32(msg, NL80211_ATTR_ACL_POLICY, params->acl_policy ?
3441 NL80211_ACL_POLICY_DENY_UNLESS_LISTED :
3442 NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED) ||
3443 nla_put_nested(msg, NL80211_ATTR_MAC_ADDRS, acl)) {
3444 nlmsg_free(msg);
3445 nlmsg_free(acl);
3446 return -ENOMEM;
3447 }
3448 nlmsg_free(acl);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003449
3450 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003451 if (ret) {
3452 wpa_printf(MSG_DEBUG, "nl80211: Failed to set MAC ACL: %d (%s)",
3453 ret, strerror(-ret));
3454 }
3455
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003456 return ret;
3457}
3458
3459
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003460static int nl80211_put_beacon_int(struct nl_msg *msg, int beacon_int)
3461{
3462 if (beacon_int > 0) {
3463 wpa_printf(MSG_DEBUG, " * beacon_int=%d", beacon_int);
3464 return nla_put_u32(msg, NL80211_ATTR_BEACON_INTERVAL,
3465 beacon_int);
3466 }
3467
3468 return 0;
3469}
3470
3471
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07003472static int nl80211_put_dtim_period(struct nl_msg *msg, int dtim_period)
3473{
3474 if (dtim_period > 0) {
3475 wpa_printf(MSG_DEBUG, " * dtim_period=%d", dtim_period);
3476 return nla_put_u32(msg, NL80211_ATTR_DTIM_PERIOD, dtim_period);
3477 }
3478
3479 return 0;
3480}
3481
3482
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003483static int wpa_driver_nl80211_set_ap(void *priv,
3484 struct wpa_driver_ap_params *params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003485{
3486 struct i802_bss *bss = priv;
3487 struct wpa_driver_nl80211_data *drv = bss->drv;
3488 struct nl_msg *msg;
3489 u8 cmd = NL80211_CMD_NEW_BEACON;
3490 int ret;
3491 int beacon_set;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003492 int num_suites;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003493 int smps_mode;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003494 u32 suites[10], suite;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003495 u32 ver;
3496
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003497 beacon_set = params->reenable ? 0 : bss->beacon_set;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003498
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003499 wpa_printf(MSG_DEBUG, "nl80211: Set beacon (beacon_set=%d)",
3500 beacon_set);
3501 if (beacon_set)
3502 cmd = NL80211_CMD_SET_BEACON;
3503
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003504 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon head",
3505 params->head, params->head_len);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003506 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon tail",
3507 params->tail, params->tail_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003508 wpa_printf(MSG_DEBUG, "nl80211: ifindex=%d", bss->ifindex);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003509 wpa_printf(MSG_DEBUG, "nl80211: beacon_int=%d", params->beacon_int);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003510 wpa_printf(MSG_DEBUG, "nl80211: dtim_period=%d", params->dtim_period);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003511 wpa_hexdump_ascii(MSG_DEBUG, "nl80211: ssid",
3512 params->ssid, params->ssid_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003513 if (!(msg = nl80211_bss_msg(bss, 0, cmd)) ||
3514 nla_put(msg, NL80211_ATTR_BEACON_HEAD, params->head_len,
3515 params->head) ||
3516 nla_put(msg, NL80211_ATTR_BEACON_TAIL, params->tail_len,
3517 params->tail) ||
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003518 nl80211_put_beacon_int(msg, params->beacon_int) ||
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07003519 nl80211_put_dtim_period(msg, params->dtim_period) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003520 nla_put(msg, NL80211_ATTR_SSID, params->ssid_len, params->ssid))
3521 goto fail;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003522 if (params->proberesp && params->proberesp_len) {
3523 wpa_hexdump(MSG_DEBUG, "nl80211: proberesp (offload)",
3524 params->proberesp, params->proberesp_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003525 if (nla_put(msg, NL80211_ATTR_PROBE_RESP, params->proberesp_len,
3526 params->proberesp))
3527 goto fail;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003528 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003529 switch (params->hide_ssid) {
3530 case NO_SSID_HIDING:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003531 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID not in use");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003532 if (nla_put_u32(msg, NL80211_ATTR_HIDDEN_SSID,
3533 NL80211_HIDDEN_SSID_NOT_IN_USE))
3534 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003535 break;
3536 case HIDDEN_SSID_ZERO_LEN:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003537 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero len");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003538 if (nla_put_u32(msg, NL80211_ATTR_HIDDEN_SSID,
3539 NL80211_HIDDEN_SSID_ZERO_LEN))
3540 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003541 break;
3542 case HIDDEN_SSID_ZERO_CONTENTS:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003543 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero contents");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003544 if (nla_put_u32(msg, NL80211_ATTR_HIDDEN_SSID,
3545 NL80211_HIDDEN_SSID_ZERO_CONTENTS))
3546 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003547 break;
3548 }
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003549 wpa_printf(MSG_DEBUG, "nl80211: privacy=%d", params->privacy);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003550 if (params->privacy &&
3551 nla_put_flag(msg, NL80211_ATTR_PRIVACY))
3552 goto fail;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003553 wpa_printf(MSG_DEBUG, "nl80211: auth_algs=0x%x", params->auth_algs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003554 if ((params->auth_algs & (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) ==
3555 (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) {
3556 /* Leave out the attribute */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003557 } else if (params->auth_algs & WPA_AUTH_ALG_SHARED) {
3558 if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE,
3559 NL80211_AUTHTYPE_SHARED_KEY))
3560 goto fail;
3561 } else {
3562 if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE,
3563 NL80211_AUTHTYPE_OPEN_SYSTEM))
3564 goto fail;
3565 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003566
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003567 wpa_printf(MSG_DEBUG, "nl80211: wpa_version=0x%x", params->wpa_version);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003568 ver = 0;
3569 if (params->wpa_version & WPA_PROTO_WPA)
3570 ver |= NL80211_WPA_VERSION_1;
3571 if (params->wpa_version & WPA_PROTO_RSN)
3572 ver |= NL80211_WPA_VERSION_2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003573 if (ver &&
3574 nla_put_u32(msg, NL80211_ATTR_WPA_VERSIONS, ver))
3575 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003576
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003577 wpa_printf(MSG_DEBUG, "nl80211: key_mgmt_suites=0x%x",
3578 params->key_mgmt_suites);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003579 num_suites = 0;
3580 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X)
3581 suites[num_suites++] = WLAN_AKM_SUITE_8021X;
3582 if (params->key_mgmt_suites & WPA_KEY_MGMT_PSK)
3583 suites[num_suites++] = WLAN_AKM_SUITE_PSK;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003584 if (num_suites &&
3585 nla_put(msg, NL80211_ATTR_AKM_SUITES, num_suites * sizeof(u32),
3586 suites))
3587 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003588
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003589 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
3590 params->pairwise_ciphers & (WPA_CIPHER_WEP104 | WPA_CIPHER_WEP40) &&
3591 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT))
3592 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003593
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003594 wpa_printf(MSG_DEBUG, "nl80211: pairwise_ciphers=0x%x",
3595 params->pairwise_ciphers);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003596 num_suites = wpa_cipher_to_cipher_suites(params->pairwise_ciphers,
3597 suites, ARRAY_SIZE(suites));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003598 if (num_suites &&
3599 nla_put(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
3600 num_suites * sizeof(u32), suites))
3601 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003602
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003603 wpa_printf(MSG_DEBUG, "nl80211: group_cipher=0x%x",
3604 params->group_cipher);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003605 suite = wpa_cipher_to_cipher_suite(params->group_cipher);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003606 if (suite &&
3607 nla_put_u32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, suite))
3608 goto fail;
3609
Dmitry Shmidte4663042016-04-04 10:07:49 -07003610 if (params->ht_opmode != -1) {
3611 switch (params->smps_mode) {
3612 case HT_CAP_INFO_SMPS_DYNAMIC:
3613 wpa_printf(MSG_DEBUG, "nl80211: SMPS mode - dynamic");
3614 smps_mode = NL80211_SMPS_DYNAMIC;
3615 break;
3616 case HT_CAP_INFO_SMPS_STATIC:
3617 wpa_printf(MSG_DEBUG, "nl80211: SMPS mode - static");
3618 smps_mode = NL80211_SMPS_STATIC;
3619 break;
3620 default:
3621 /* invalid - fallback to smps off */
3622 case HT_CAP_INFO_SMPS_DISABLED:
3623 wpa_printf(MSG_DEBUG, "nl80211: SMPS mode - off");
3624 smps_mode = NL80211_SMPS_OFF;
3625 break;
3626 }
3627 if (nla_put_u32(msg, NL80211_ATTR_SMPS_MODE, smps_mode))
3628 goto fail;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003629 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003630
3631 if (params->beacon_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003632 wpa_hexdump_buf(MSG_DEBUG, "nl80211: beacon_ies",
3633 params->beacon_ies);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003634 if (nla_put(msg, NL80211_ATTR_IE,
3635 wpabuf_len(params->beacon_ies),
3636 wpabuf_head(params->beacon_ies)))
3637 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003638 }
3639 if (params->proberesp_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003640 wpa_hexdump_buf(MSG_DEBUG, "nl80211: proberesp_ies",
3641 params->proberesp_ies);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003642 if (nla_put(msg, NL80211_ATTR_IE_PROBE_RESP,
3643 wpabuf_len(params->proberesp_ies),
3644 wpabuf_head(params->proberesp_ies)))
3645 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003646 }
3647 if (params->assocresp_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003648 wpa_hexdump_buf(MSG_DEBUG, "nl80211: assocresp_ies",
3649 params->assocresp_ies);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003650 if (nla_put(msg, NL80211_ATTR_IE_ASSOC_RESP,
3651 wpabuf_len(params->assocresp_ies),
3652 wpabuf_head(params->assocresp_ies)))
3653 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003654 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003655
Dmitry Shmidt04949592012-07-19 12:16:46 -07003656 if (drv->capa.flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003657 wpa_printf(MSG_DEBUG, "nl80211: ap_max_inactivity=%d",
3658 params->ap_max_inactivity);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003659 if (nla_put_u16(msg, NL80211_ATTR_INACTIVITY_TIMEOUT,
3660 params->ap_max_inactivity))
3661 goto fail;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003662 }
3663
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003664#ifdef CONFIG_P2P
3665 if (params->p2p_go_ctwindow > 0) {
3666 if (drv->p2p_go_ctwindow_supported) {
3667 wpa_printf(MSG_DEBUG, "nl80211: P2P GO ctwindow=%d",
3668 params->p2p_go_ctwindow);
3669 if (nla_put_u8(msg, NL80211_ATTR_P2P_CTWINDOW,
3670 params->p2p_go_ctwindow))
3671 goto fail;
3672 } else {
3673 wpa_printf(MSG_INFO,
3674 "nl80211: Driver does not support CTWindow configuration - ignore this parameter");
3675 }
3676 }
3677#endif /* CONFIG_P2P */
3678
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003679 if (params->pbss) {
3680 wpa_printf(MSG_DEBUG, "nl80211: PBSS");
3681 if (nla_put_flag(msg, NL80211_ATTR_PBSS))
3682 goto fail;
3683 }
3684
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003685 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3686 if (ret) {
3687 wpa_printf(MSG_DEBUG, "nl80211: Beacon set failed: %d (%s)",
3688 ret, strerror(-ret));
3689 } else {
3690 bss->beacon_set = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003691 nl80211_set_bss(bss, params->cts_protect, params->preamble,
3692 params->short_slot_time, params->ht_opmode,
3693 params->isolate, params->basic_rates);
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07003694 if (beacon_set && params->freq &&
3695 params->freq->bandwidth != bss->bandwidth) {
3696 wpa_printf(MSG_DEBUG,
3697 "nl80211: Update BSS %s bandwidth: %d -> %d",
3698 bss->ifname, bss->bandwidth,
3699 params->freq->bandwidth);
3700 ret = nl80211_set_channel(bss, params->freq, 1);
3701 if (ret) {
3702 wpa_printf(MSG_DEBUG,
3703 "nl80211: Frequency set failed: %d (%s)",
3704 ret, strerror(-ret));
3705 } else {
3706 wpa_printf(MSG_DEBUG,
3707 "nl80211: Frequency set succeeded for ht2040 coex");
3708 bss->bandwidth = params->freq->bandwidth;
3709 }
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07003710 } else if (!beacon_set && params->freq) {
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07003711 /*
3712 * cfg80211 updates the driver on frequence change in AP
3713 * mode only at the point when beaconing is started, so
3714 * set the initial value here.
3715 */
3716 bss->bandwidth = params->freq->bandwidth;
3717 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003718 }
3719 return ret;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003720fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003721 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003722 return -ENOBUFS;
3723}
3724
3725
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003726static int nl80211_put_freq_params(struct nl_msg *msg,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003727 const struct hostapd_freq_params *freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003728{
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003729 wpa_printf(MSG_DEBUG, " * freq=%d", freq->freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003730 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq->freq))
3731 return -ENOBUFS;
3732
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003733 wpa_printf(MSG_DEBUG, " * vht_enabled=%d", freq->vht_enabled);
3734 wpa_printf(MSG_DEBUG, " * ht_enabled=%d", freq->ht_enabled);
3735
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003736 if (freq->vht_enabled) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003737 enum nl80211_chan_width cw;
3738
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003739 wpa_printf(MSG_DEBUG, " * bandwidth=%d", freq->bandwidth);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003740 switch (freq->bandwidth) {
3741 case 20:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003742 cw = NL80211_CHAN_WIDTH_20;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003743 break;
3744 case 40:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003745 cw = NL80211_CHAN_WIDTH_40;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003746 break;
3747 case 80:
3748 if (freq->center_freq2)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003749 cw = NL80211_CHAN_WIDTH_80P80;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003750 else
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003751 cw = NL80211_CHAN_WIDTH_80;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003752 break;
3753 case 160:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003754 cw = NL80211_CHAN_WIDTH_160;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003755 break;
3756 default:
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003757 return -EINVAL;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003758 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003759
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003760 wpa_printf(MSG_DEBUG, " * channel_width=%d", cw);
3761 wpa_printf(MSG_DEBUG, " * center_freq1=%d",
3762 freq->center_freq1);
3763 wpa_printf(MSG_DEBUG, " * center_freq2=%d",
3764 freq->center_freq2);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003765 if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, cw) ||
3766 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1,
3767 freq->center_freq1) ||
3768 (freq->center_freq2 &&
3769 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2,
3770 freq->center_freq2)))
3771 return -ENOBUFS;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003772 } else if (freq->ht_enabled) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003773 enum nl80211_channel_type ct;
3774
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003775 wpa_printf(MSG_DEBUG, " * sec_channel_offset=%d",
3776 freq->sec_channel_offset);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003777 switch (freq->sec_channel_offset) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003778 case -1:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003779 ct = NL80211_CHAN_HT40MINUS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003780 break;
3781 case 1:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003782 ct = NL80211_CHAN_HT40PLUS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003783 break;
3784 default:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003785 ct = NL80211_CHAN_HT20;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003786 break;
3787 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003788
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003789 wpa_printf(MSG_DEBUG, " * channel_type=%d", ct);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003790 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, ct))
3791 return -ENOBUFS;
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07003792 } else {
3793 wpa_printf(MSG_DEBUG, " * channel_type=%d",
3794 NL80211_CHAN_NO_HT);
3795 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
3796 NL80211_CHAN_NO_HT))
3797 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003798 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003799 return 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003800}
3801
3802
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07003803static int nl80211_set_channel(struct i802_bss *bss,
3804 struct hostapd_freq_params *freq, int set_chan)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003805{
3806 struct wpa_driver_nl80211_data *drv = bss->drv;
3807 struct nl_msg *msg;
3808 int ret;
3809
3810 wpa_printf(MSG_DEBUG,
3811 "nl80211: Set freq %d (ht_enabled=%d, vht_enabled=%d, bandwidth=%d MHz, cf1=%d MHz, cf2=%d MHz)",
3812 freq->freq, freq->ht_enabled, freq->vht_enabled,
3813 freq->bandwidth, freq->center_freq1, freq->center_freq2);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003814
3815 msg = nl80211_drv_msg(drv, 0, set_chan ? NL80211_CMD_SET_CHANNEL :
3816 NL80211_CMD_SET_WIPHY);
3817 if (!msg || nl80211_put_freq_params(msg, freq) < 0) {
3818 nlmsg_free(msg);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003819 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003820 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003821
3822 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003823 if (ret == 0) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003824 bss->freq = freq->freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003825 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003826 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003827 wpa_printf(MSG_DEBUG, "nl80211: Failed to set channel (freq=%d): "
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003828 "%d (%s)", freq->freq, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003829 return -1;
3830}
3831
3832
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003833static u32 sta_flags_nl80211(int flags)
3834{
3835 u32 f = 0;
3836
3837 if (flags & WPA_STA_AUTHORIZED)
3838 f |= BIT(NL80211_STA_FLAG_AUTHORIZED);
3839 if (flags & WPA_STA_WMM)
3840 f |= BIT(NL80211_STA_FLAG_WME);
3841 if (flags & WPA_STA_SHORT_PREAMBLE)
3842 f |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
3843 if (flags & WPA_STA_MFP)
3844 f |= BIT(NL80211_STA_FLAG_MFP);
3845 if (flags & WPA_STA_TDLS_PEER)
3846 f |= BIT(NL80211_STA_FLAG_TDLS_PEER);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003847 if (flags & WPA_STA_AUTHENTICATED)
3848 f |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003849 if (flags & WPA_STA_ASSOCIATED)
3850 f |= BIT(NL80211_STA_FLAG_ASSOCIATED);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003851
3852 return f;
3853}
3854
3855
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003856#ifdef CONFIG_MESH
3857static u32 sta_plink_state_nl80211(enum mesh_plink_state state)
3858{
3859 switch (state) {
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07003860 case PLINK_IDLE:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003861 return NL80211_PLINK_LISTEN;
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07003862 case PLINK_OPN_SNT:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003863 return NL80211_PLINK_OPN_SNT;
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07003864 case PLINK_OPN_RCVD:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003865 return NL80211_PLINK_OPN_RCVD;
3866 case PLINK_CNF_RCVD:
3867 return NL80211_PLINK_CNF_RCVD;
3868 case PLINK_ESTAB:
3869 return NL80211_PLINK_ESTAB;
3870 case PLINK_HOLDING:
3871 return NL80211_PLINK_HOLDING;
3872 case PLINK_BLOCKED:
3873 return NL80211_PLINK_BLOCKED;
3874 default:
3875 wpa_printf(MSG_ERROR, "nl80211: Invalid mesh plink state %d",
3876 state);
3877 }
3878 return -1;
3879}
3880#endif /* CONFIG_MESH */
3881
3882
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003883static int wpa_driver_nl80211_sta_add(void *priv,
3884 struct hostapd_sta_add_params *params)
3885{
3886 struct i802_bss *bss = priv;
3887 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003888 struct nl_msg *msg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003889 struct nl80211_sta_flag_update upd;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003890 int ret = -ENOBUFS;
3891
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003892 if ((params->flags & WPA_STA_TDLS_PEER) &&
3893 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
3894 return -EOPNOTSUPP;
3895
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003896 wpa_printf(MSG_DEBUG, "nl80211: %s STA " MACSTR,
3897 params->set ? "Set" : "Add", MAC2STR(params->addr));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003898 msg = nl80211_bss_msg(bss, 0, params->set ? NL80211_CMD_SET_STATION :
3899 NL80211_CMD_NEW_STATION);
3900 if (!msg || nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr))
3901 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003902
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003903 /*
3904 * Set the below properties only in one of the following cases:
3905 * 1. New station is added, already associated.
3906 * 2. Set WPA_STA_TDLS_PEER station.
3907 * 3. Set an already added unassociated station, if driver supports
3908 * full AP client state. (Set these properties after station became
3909 * associated will be rejected by the driver).
3910 */
3911 if (!params->set || (params->flags & WPA_STA_TDLS_PEER) ||
3912 (params->set && FULL_AP_CLIENT_STATE_SUPP(drv->capa.flags) &&
3913 (params->flags & WPA_STA_ASSOCIATED))) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003914 wpa_hexdump(MSG_DEBUG, " * supported rates",
3915 params->supp_rates, params->supp_rates_len);
3916 wpa_printf(MSG_DEBUG, " * capability=0x%x",
3917 params->capability);
3918 if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_RATES,
3919 params->supp_rates_len, params->supp_rates) ||
3920 nla_put_u16(msg, NL80211_ATTR_STA_CAPABILITY,
3921 params->capability))
3922 goto fail;
3923
3924 if (params->ht_capabilities) {
3925 wpa_hexdump(MSG_DEBUG, " * ht_capabilities",
3926 (u8 *) params->ht_capabilities,
3927 sizeof(*params->ht_capabilities));
3928 if (nla_put(msg, NL80211_ATTR_HT_CAPABILITY,
3929 sizeof(*params->ht_capabilities),
3930 params->ht_capabilities))
3931 goto fail;
3932 }
3933
3934 if (params->vht_capabilities) {
3935 wpa_hexdump(MSG_DEBUG, " * vht_capabilities",
3936 (u8 *) params->vht_capabilities,
3937 sizeof(*params->vht_capabilities));
3938 if (nla_put(msg, NL80211_ATTR_VHT_CAPABILITY,
3939 sizeof(*params->vht_capabilities),
3940 params->vht_capabilities))
3941 goto fail;
3942 }
3943
3944 if (params->ext_capab) {
3945 wpa_hexdump(MSG_DEBUG, " * ext_capab",
3946 params->ext_capab, params->ext_capab_len);
3947 if (nla_put(msg, NL80211_ATTR_STA_EXT_CAPABILITY,
3948 params->ext_capab_len, params->ext_capab))
3949 goto fail;
3950 }
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003951
3952 if (is_ap_interface(drv->nlmode) &&
3953 nla_put_u8(msg, NL80211_ATTR_STA_SUPPORT_P2P_PS,
3954 params->support_p2p_ps ?
3955 NL80211_P2P_PS_SUPPORTED :
3956 NL80211_P2P_PS_UNSUPPORTED))
3957 goto fail;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003958 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003959 if (!params->set) {
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07003960 if (params->aid) {
3961 wpa_printf(MSG_DEBUG, " * aid=%u", params->aid);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003962 if (nla_put_u16(msg, NL80211_ATTR_STA_AID, params->aid))
3963 goto fail;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07003964 } else {
3965 /*
3966 * cfg80211 validates that AID is non-zero, so we have
3967 * to make this a non-zero value for the TDLS case where
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003968 * a dummy STA entry is used for now and for a station
3969 * that is still not associated.
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07003970 */
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003971 wpa_printf(MSG_DEBUG, " * aid=1 (%s workaround)",
3972 (params->flags & WPA_STA_TDLS_PEER) ?
3973 "TDLS" : "UNASSOC_STA");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003974 if (nla_put_u16(msg, NL80211_ATTR_STA_AID, 1))
3975 goto fail;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07003976 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003977 wpa_printf(MSG_DEBUG, " * listen_interval=%u",
3978 params->listen_interval);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003979 if (nla_put_u16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
3980 params->listen_interval))
3981 goto fail;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003982 } else if (params->aid && (params->flags & WPA_STA_TDLS_PEER)) {
3983 wpa_printf(MSG_DEBUG, " * peer_aid=%u", params->aid);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003984 if (nla_put_u16(msg, NL80211_ATTR_PEER_AID, params->aid))
3985 goto fail;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003986 } else if (FULL_AP_CLIENT_STATE_SUPP(drv->capa.flags) &&
3987 (params->flags & WPA_STA_ASSOCIATED)) {
3988 wpa_printf(MSG_DEBUG, " * aid=%u", params->aid);
3989 wpa_printf(MSG_DEBUG, " * listen_interval=%u",
3990 params->listen_interval);
3991 if (nla_put_u16(msg, NL80211_ATTR_STA_AID, params->aid) ||
3992 nla_put_u16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
3993 params->listen_interval))
3994 goto fail;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003995 }
3996
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08003997 if (params->vht_opmode_enabled) {
3998 wpa_printf(MSG_DEBUG, " * opmode=%u", params->vht_opmode);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003999 if (nla_put_u8(msg, NL80211_ATTR_OPMODE_NOTIF,
4000 params->vht_opmode))
4001 goto fail;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08004002 }
4003
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004004 if (params->supp_channels) {
4005 wpa_hexdump(MSG_DEBUG, " * supported channels",
4006 params->supp_channels, params->supp_channels_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004007 if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_CHANNELS,
4008 params->supp_channels_len, params->supp_channels))
4009 goto fail;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004010 }
4011
4012 if (params->supp_oper_classes) {
4013 wpa_hexdump(MSG_DEBUG, " * supported operating classes",
4014 params->supp_oper_classes,
4015 params->supp_oper_classes_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004016 if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES,
4017 params->supp_oper_classes_len,
4018 params->supp_oper_classes))
4019 goto fail;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004020 }
4021
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004022 os_memset(&upd, 0, sizeof(upd));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004023 upd.set = sta_flags_nl80211(params->flags);
4024 upd.mask = upd.set | sta_flags_nl80211(params->flags_mask);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004025
4026 /*
4027 * If the driver doesn't support full AP client state, ignore ASSOC/AUTH
4028 * flags, as nl80211 driver moves a new station, by default, into
4029 * associated state.
4030 *
4031 * On the other hand, if the driver supports that feature and the
4032 * station is added in unauthenticated state, set the
4033 * authenticated/associated bits in the mask to prevent moving this
4034 * station to associated state before it is actually associated.
4035 *
4036 * This is irrelevant for mesh mode where the station is added to the
4037 * driver as authenticated already, and ASSOCIATED isn't part of the
4038 * nl80211 API.
4039 */
4040 if (!is_mesh_interface(drv->nlmode)) {
4041 if (!FULL_AP_CLIENT_STATE_SUPP(drv->capa.flags)) {
4042 wpa_printf(MSG_DEBUG,
4043 "nl80211: Ignore ASSOC/AUTH flags since driver doesn't support full AP client state");
4044 upd.mask &= ~(BIT(NL80211_STA_FLAG_ASSOCIATED) |
4045 BIT(NL80211_STA_FLAG_AUTHENTICATED));
4046 } else if (!params->set &&
4047 !(params->flags & WPA_STA_TDLS_PEER)) {
4048 if (!(params->flags & WPA_STA_AUTHENTICATED))
4049 upd.mask |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
4050 if (!(params->flags & WPA_STA_ASSOCIATED))
4051 upd.mask |= BIT(NL80211_STA_FLAG_ASSOCIATED);
4052 }
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07004053#ifdef CONFIG_MESH
4054 } else {
4055 if (params->plink_state == PLINK_ESTAB && params->peer_aid) {
4056 ret = nla_put_u16(msg, NL80211_ATTR_MESH_PEER_AID,
4057 params->peer_aid);
4058 if (ret)
4059 goto fail;
4060 }
4061#endif /* CONFIG_MESH */
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004062 }
4063
Dmitry Shmidtf8623282013-02-20 14:34:59 -08004064 wpa_printf(MSG_DEBUG, " * flags set=0x%x mask=0x%x",
4065 upd.set, upd.mask);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004066 if (nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd))
4067 goto fail;
4068
4069#ifdef CONFIG_MESH
4070 if (params->plink_state &&
4071 nla_put_u8(msg, NL80211_ATTR_STA_PLINK_STATE,
4072 sta_plink_state_nl80211(params->plink_state)))
4073 goto fail;
4074#endif /* CONFIG_MESH */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004075
4076 if (params->flags & WPA_STA_WMM) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004077 struct nlattr *wme = nla_nest_start(msg, NL80211_ATTR_STA_WME);
4078
Dmitry Shmidtf8623282013-02-20 14:34:59 -08004079 wpa_printf(MSG_DEBUG, " * qosinfo=0x%x", params->qosinfo);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004080 if (!wme ||
4081 nla_put_u8(msg, NL80211_STA_WME_UAPSD_QUEUES,
4082 params->qosinfo & WMM_QOSINFO_STA_AC_MASK) ||
4083 nla_put_u8(msg, NL80211_STA_WME_MAX_SP,
4084 (params->qosinfo >> WMM_QOSINFO_STA_SP_SHIFT) &
4085 WMM_QOSINFO_STA_SP_MASK))
4086 goto fail;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004087 nla_nest_end(msg, wme);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004088 }
4089
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004090 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004091 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004092 if (ret)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004093 wpa_printf(MSG_DEBUG, "nl80211: NL80211_CMD_%s_STATION "
4094 "result: %d (%s)", params->set ? "SET" : "NEW", ret,
4095 strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004096 if (ret == -EEXIST)
4097 ret = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004098fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004099 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004100 return ret;
4101}
4102
4103
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004104static void rtnl_neigh_delete_fdb_entry(struct i802_bss *bss, const u8 *addr)
4105{
4106#ifdef CONFIG_LIBNL3_ROUTE
4107 struct wpa_driver_nl80211_data *drv = bss->drv;
4108 struct rtnl_neigh *rn;
4109 struct nl_addr *nl_addr;
4110 int err;
4111
4112 rn = rtnl_neigh_alloc();
4113 if (!rn)
4114 return;
4115
4116 rtnl_neigh_set_family(rn, AF_BRIDGE);
4117 rtnl_neigh_set_ifindex(rn, bss->ifindex);
4118 nl_addr = nl_addr_build(AF_BRIDGE, (void *) addr, ETH_ALEN);
4119 if (!nl_addr) {
4120 rtnl_neigh_put(rn);
4121 return;
4122 }
4123 rtnl_neigh_set_lladdr(rn, nl_addr);
4124
4125 err = rtnl_neigh_delete(drv->rtnl_sk, rn, 0);
4126 if (err < 0) {
4127 wpa_printf(MSG_DEBUG, "nl80211: bridge FDB entry delete for "
4128 MACSTR " ifindex=%d failed: %s", MAC2STR(addr),
4129 bss->ifindex, nl_geterror(err));
4130 } else {
4131 wpa_printf(MSG_DEBUG, "nl80211: deleted bridge FDB entry for "
4132 MACSTR, MAC2STR(addr));
4133 }
4134
4135 nl_addr_put(nl_addr);
4136 rtnl_neigh_put(rn);
4137#endif /* CONFIG_LIBNL3_ROUTE */
4138}
4139
4140
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004141static int wpa_driver_nl80211_sta_remove(struct i802_bss *bss, const u8 *addr,
4142 int deauth, u16 reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004143{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004144 struct wpa_driver_nl80211_data *drv = bss->drv;
4145 struct nl_msg *msg;
4146 int ret;
4147
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004148 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_DEL_STATION)) ||
4149 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
4150 (deauth == 0 &&
4151 nla_put_u8(msg, NL80211_ATTR_MGMT_SUBTYPE,
4152 WLAN_FC_STYPE_DISASSOC)) ||
4153 (deauth == 1 &&
4154 nla_put_u8(msg, NL80211_ATTR_MGMT_SUBTYPE,
4155 WLAN_FC_STYPE_DEAUTH)) ||
4156 (reason_code &&
4157 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code))) {
4158 nlmsg_free(msg);
4159 return -ENOBUFS;
4160 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004161
4162 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004163 wpa_printf(MSG_DEBUG, "nl80211: sta_remove -> DEL_STATION %s " MACSTR
4164 " --> %d (%s)",
4165 bss->ifname, MAC2STR(addr), ret, strerror(-ret));
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004166
4167 if (drv->rtnl_sk)
4168 rtnl_neigh_delete_fdb_entry(bss, addr);
4169
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004170 if (ret == -ENOENT)
4171 return 0;
4172 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004173}
4174
4175
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004176void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv, int ifidx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004177{
4178 struct nl_msg *msg;
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07004179 struct wpa_driver_nl80211_data *drv2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004180
4181 wpa_printf(MSG_DEBUG, "nl80211: Remove interface ifindex=%d", ifidx);
4182
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004183 /* stop listening for EAPOL on this interface */
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07004184 dl_list_for_each(drv2, &drv->global->interfaces,
4185 struct wpa_driver_nl80211_data, list)
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004186 {
4187 del_ifidx(drv2, ifidx, IFIDX_ANY);
4188 /* Remove all bridges learned for this iface */
4189 del_ifidx(drv2, IFIDX_ANY, ifidx);
4190 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004191
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004192 msg = nl80211_ifindex_msg(drv, ifidx, 0, NL80211_CMD_DEL_INTERFACE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004193 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
4194 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004195 wpa_printf(MSG_ERROR, "Failed to remove interface (ifidx=%d)", ifidx);
4196}
4197
4198
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07004199const char * nl80211_iftype_str(enum nl80211_iftype mode)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004200{
4201 switch (mode) {
4202 case NL80211_IFTYPE_ADHOC:
4203 return "ADHOC";
4204 case NL80211_IFTYPE_STATION:
4205 return "STATION";
4206 case NL80211_IFTYPE_AP:
4207 return "AP";
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07004208 case NL80211_IFTYPE_AP_VLAN:
4209 return "AP_VLAN";
4210 case NL80211_IFTYPE_WDS:
4211 return "WDS";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004212 case NL80211_IFTYPE_MONITOR:
4213 return "MONITOR";
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07004214 case NL80211_IFTYPE_MESH_POINT:
4215 return "MESH_POINT";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004216 case NL80211_IFTYPE_P2P_CLIENT:
4217 return "P2P_CLIENT";
4218 case NL80211_IFTYPE_P2P_GO:
4219 return "P2P_GO";
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004220 case NL80211_IFTYPE_P2P_DEVICE:
4221 return "P2P_DEVICE";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004222 default:
4223 return "unknown";
4224 }
4225}
4226
4227
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004228static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv,
4229 const char *ifname,
4230 enum nl80211_iftype iftype,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004231 const u8 *addr, int wds,
4232 int (*handler)(struct nl_msg *, void *),
4233 void *arg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004234{
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004235 struct nl_msg *msg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004236 int ifidx;
4237 int ret = -ENOBUFS;
4238
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004239 wpa_printf(MSG_DEBUG, "nl80211: Create interface iftype %d (%s)",
4240 iftype, nl80211_iftype_str(iftype));
4241
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004242 msg = nl80211_cmd_msg(drv->first_bss, 0, NL80211_CMD_NEW_INTERFACE);
4243 if (!msg ||
4244 nla_put_string(msg, NL80211_ATTR_IFNAME, ifname) ||
4245 nla_put_u32(msg, NL80211_ATTR_IFTYPE, iftype))
4246 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004247
4248 if (iftype == NL80211_IFTYPE_MONITOR) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004249 struct nlattr *flags;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004250
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004251 flags = nla_nest_start(msg, NL80211_ATTR_MNTR_FLAGS);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004252 if (!flags ||
4253 nla_put_flag(msg, NL80211_MNTR_FLAG_COOK_FRAMES))
4254 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004255
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004256 nla_nest_end(msg, flags);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004257 } else if (wds) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004258 if (nla_put_u8(msg, NL80211_ATTR_4ADDR, wds))
4259 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004260 }
4261
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07004262 /*
4263 * Tell cfg80211 that the interface belongs to the socket that created
4264 * it, and the interface should be deleted when the socket is closed.
4265 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004266 if (nla_put_flag(msg, NL80211_ATTR_IFACE_SOCKET_OWNER))
4267 goto fail;
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07004268
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004269 ret = send_and_recv_msgs(drv, msg, handler, arg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004270 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004271 if (ret) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004272 fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004273 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004274 wpa_printf(MSG_ERROR, "Failed to create interface %s: %d (%s)",
4275 ifname, ret, strerror(-ret));
4276 return ret;
4277 }
4278
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004279 if (iftype == NL80211_IFTYPE_P2P_DEVICE)
4280 return 0;
4281
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004282 ifidx = if_nametoindex(ifname);
4283 wpa_printf(MSG_DEBUG, "nl80211: New interface %s created: ifindex=%d",
4284 ifname, ifidx);
4285
4286 if (ifidx <= 0)
4287 return -1;
4288
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07004289 /*
4290 * Some virtual interfaces need to process EAPOL packets and events on
4291 * the parent interface. This is used mainly with hostapd.
4292 */
4293 if (drv->hostapd ||
4294 iftype == NL80211_IFTYPE_AP_VLAN ||
4295 iftype == NL80211_IFTYPE_WDS ||
4296 iftype == NL80211_IFTYPE_MONITOR) {
4297 /* start listening for EAPOL on this interface */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004298 add_ifidx(drv, ifidx, IFIDX_ANY);
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07004299 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004300
4301 if (addr && iftype != NL80211_IFTYPE_MONITOR &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004302 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname, addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004303 nl80211_remove_iface(drv, ifidx);
4304 return -1;
4305 }
4306
4307 return ifidx;
4308}
4309
4310
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004311int nl80211_create_iface(struct wpa_driver_nl80211_data *drv,
4312 const char *ifname, enum nl80211_iftype iftype,
4313 const u8 *addr, int wds,
4314 int (*handler)(struct nl_msg *, void *),
4315 void *arg, int use_existing)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004316{
4317 int ret;
4318
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004319 ret = nl80211_create_iface_once(drv, ifname, iftype, addr, wds, handler,
4320 arg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004321
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004322 /* if error occurred and interface exists already */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004323 if (ret == -ENFILE && if_nametoindex(ifname)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004324 if (use_existing) {
4325 wpa_printf(MSG_DEBUG, "nl80211: Continue using existing interface %s",
4326 ifname);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07004327 if (addr && iftype != NL80211_IFTYPE_MONITOR &&
4328 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
4329 addr) < 0 &&
4330 (linux_set_iface_flags(drv->global->ioctl_sock,
4331 ifname, 0) < 0 ||
4332 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
4333 addr) < 0 ||
4334 linux_set_iface_flags(drv->global->ioctl_sock,
4335 ifname, 1) < 0))
4336 return -1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004337 return -ENFILE;
4338 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004339 wpa_printf(MSG_INFO, "Try to remove and re-create %s", ifname);
4340
4341 /* Try to remove the interface that was already there. */
4342 nl80211_remove_iface(drv, if_nametoindex(ifname));
4343
4344 /* Try to create the interface again */
4345 ret = nl80211_create_iface_once(drv, ifname, iftype, addr,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004346 wds, handler, arg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004347 }
4348
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004349 if (ret >= 0 && is_p2p_net_interface(iftype)) {
4350 wpa_printf(MSG_DEBUG,
4351 "nl80211: Interface %s created for P2P - disable 11b rates",
4352 ifname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004353 nl80211_disable_11b_rates(drv, ret, 1);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004354 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004355
4356 return ret;
4357}
4358
4359
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004360static int nl80211_setup_ap(struct i802_bss *bss)
4361{
4362 struct wpa_driver_nl80211_data *drv = bss->drv;
4363
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004364 wpa_printf(MSG_DEBUG, "nl80211: Setup AP(%s) - device_ap_sme=%d use_monitor=%d",
4365 bss->ifname, drv->device_ap_sme, drv->use_monitor);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004366
4367 /*
4368 * Disable Probe Request reporting unless we need it in this way for
4369 * devices that include the AP SME, in the other case (unless using
4370 * monitor iface) we'll get it through the nl_mgmt socket instead.
4371 */
4372 if (!drv->device_ap_sme)
4373 wpa_driver_nl80211_probe_req_report(bss, 0);
4374
4375 if (!drv->device_ap_sme && !drv->use_monitor)
4376 if (nl80211_mgmt_subscribe_ap(bss))
4377 return -1;
4378
4379 if (drv->device_ap_sme && !drv->use_monitor)
4380 if (nl80211_mgmt_subscribe_ap_dev_sme(bss))
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004381 wpa_printf(MSG_DEBUG,
4382 "nl80211: Failed to subscribe for mgmt frames from SME driver - trying to run without it");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004383
4384 if (!drv->device_ap_sme && drv->use_monitor &&
4385 nl80211_create_monitor_interface(drv) &&
4386 !drv->device_ap_sme)
Dmitry Shmidt04949592012-07-19 12:16:46 -07004387 return -1;
4388
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004389 if (drv->device_ap_sme &&
4390 wpa_driver_nl80211_probe_req_report(bss, 1) < 0) {
4391 wpa_printf(MSG_DEBUG, "nl80211: Failed to enable "
4392 "Probe Request frame reporting in AP mode");
4393 /* Try to survive without this */
4394 }
4395
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004396 return 0;
4397}
4398
4399
4400static void nl80211_teardown_ap(struct i802_bss *bss)
4401{
4402 struct wpa_driver_nl80211_data *drv = bss->drv;
4403
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004404 wpa_printf(MSG_DEBUG, "nl80211: Teardown AP(%s) - device_ap_sme=%d use_monitor=%d",
4405 bss->ifname, drv->device_ap_sme, drv->use_monitor);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004406 if (drv->device_ap_sme) {
4407 wpa_driver_nl80211_probe_req_report(bss, 0);
4408 if (!drv->use_monitor)
4409 nl80211_mgmt_unsubscribe(bss, "AP teardown (dev SME)");
4410 } else if (drv->use_monitor)
4411 nl80211_remove_monitor_interface(drv);
4412 else
4413 nl80211_mgmt_unsubscribe(bss, "AP teardown");
4414
4415 bss->beacon_set = 0;
4416}
4417
4418
4419static int nl80211_send_eapol_data(struct i802_bss *bss,
4420 const u8 *addr, const u8 *data,
4421 size_t data_len)
4422{
4423 struct sockaddr_ll ll;
4424 int ret;
4425
4426 if (bss->drv->eapol_tx_sock < 0) {
4427 wpa_printf(MSG_DEBUG, "nl80211: No socket to send EAPOL");
4428 return -1;
4429 }
4430
4431 os_memset(&ll, 0, sizeof(ll));
4432 ll.sll_family = AF_PACKET;
4433 ll.sll_ifindex = bss->ifindex;
4434 ll.sll_protocol = htons(ETH_P_PAE);
4435 ll.sll_halen = ETH_ALEN;
4436 os_memcpy(ll.sll_addr, addr, ETH_ALEN);
4437 ret = sendto(bss->drv->eapol_tx_sock, data, data_len, 0,
4438 (struct sockaddr *) &ll, sizeof(ll));
4439 if (ret < 0)
4440 wpa_printf(MSG_ERROR, "nl80211: EAPOL TX: %s",
4441 strerror(errno));
4442
4443 return ret;
4444}
4445
4446
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004447static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
4448
4449static int wpa_driver_nl80211_hapd_send_eapol(
4450 void *priv, const u8 *addr, const u8 *data,
4451 size_t data_len, int encrypt, const u8 *own_addr, u32 flags)
4452{
4453 struct i802_bss *bss = priv;
4454 struct wpa_driver_nl80211_data *drv = bss->drv;
4455 struct ieee80211_hdr *hdr;
4456 size_t len;
4457 u8 *pos;
4458 int res;
4459 int qos = flags & WPA_STA_WMM;
Dmitry Shmidt641185e2013-11-06 15:17:13 -08004460
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004461 if (drv->device_ap_sme || !drv->use_monitor)
4462 return nl80211_send_eapol_data(bss, addr, data, data_len);
4463
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004464 len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
4465 data_len;
4466 hdr = os_zalloc(len);
4467 if (hdr == NULL) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004468 wpa_printf(MSG_INFO, "nl80211: Failed to allocate EAPOL buffer(len=%lu)",
4469 (unsigned long) len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004470 return -1;
4471 }
4472
4473 hdr->frame_control =
4474 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
4475 hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
4476 if (encrypt)
4477 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
4478 if (qos) {
4479 hdr->frame_control |=
4480 host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
4481 }
4482
4483 memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
4484 memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
4485 memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
4486 pos = (u8 *) (hdr + 1);
4487
4488 if (qos) {
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004489 /* Set highest priority in QoS header */
4490 pos[0] = 7;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004491 pos[1] = 0;
4492 pos += 2;
4493 }
4494
4495 memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
4496 pos += sizeof(rfc1042_header);
4497 WPA_PUT_BE16(pos, ETH_P_PAE);
4498 pos += 2;
4499 memcpy(pos, data, data_len);
4500
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004501 res = wpa_driver_nl80211_send_frame(bss, (u8 *) hdr, len, encrypt, 0,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004502 0, 0, 0, 0, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004503 if (res < 0) {
4504 wpa_printf(MSG_ERROR, "i802_send_eapol - packet len: %lu - "
4505 "failed: %d (%s)",
4506 (unsigned long) len, errno, strerror(errno));
4507 }
4508 os_free(hdr);
4509
4510 return res;
4511}
4512
4513
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004514static int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004515 unsigned int total_flags,
4516 unsigned int flags_or,
4517 unsigned int flags_and)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004518{
4519 struct i802_bss *bss = priv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004520 struct nl_msg *msg;
4521 struct nlattr *flags;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004522 struct nl80211_sta_flag_update upd;
4523
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004524 wpa_printf(MSG_DEBUG, "nl80211: Set STA flags - ifname=%s addr=" MACSTR
4525 " total_flags=0x%x flags_or=0x%x flags_and=0x%x authorized=%d",
4526 bss->ifname, MAC2STR(addr), total_flags, flags_or, flags_and,
4527 !!(total_flags & WPA_STA_AUTHORIZED));
4528
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004529 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
4530 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
4531 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004532
4533 /*
4534 * Backwards compatibility version using NL80211_ATTR_STA_FLAGS. This
4535 * can be removed eventually.
4536 */
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004537 flags = nla_nest_start(msg, NL80211_ATTR_STA_FLAGS);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004538 if (!flags ||
4539 ((total_flags & WPA_STA_AUTHORIZED) &&
4540 nla_put_flag(msg, NL80211_STA_FLAG_AUTHORIZED)) ||
4541 ((total_flags & WPA_STA_WMM) &&
4542 nla_put_flag(msg, NL80211_STA_FLAG_WME)) ||
4543 ((total_flags & WPA_STA_SHORT_PREAMBLE) &&
4544 nla_put_flag(msg, NL80211_STA_FLAG_SHORT_PREAMBLE)) ||
4545 ((total_flags & WPA_STA_MFP) &&
4546 nla_put_flag(msg, NL80211_STA_FLAG_MFP)) ||
4547 ((total_flags & WPA_STA_TDLS_PEER) &&
4548 nla_put_flag(msg, NL80211_STA_FLAG_TDLS_PEER)))
4549 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004550
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004551 nla_nest_end(msg, flags);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004552
4553 os_memset(&upd, 0, sizeof(upd));
4554 upd.mask = sta_flags_nl80211(flags_or | ~flags_and);
4555 upd.set = sta_flags_nl80211(flags_or);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004556 if (nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd))
4557 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004558
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004559 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
4560fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004561 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004562 return -ENOBUFS;
4563}
4564
4565
4566static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv,
4567 struct wpa_driver_associate_params *params)
4568{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004569 enum nl80211_iftype nlmode, old_mode;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004570
4571 if (params->p2p) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004572 wpa_printf(MSG_DEBUG, "nl80211: Setup AP operations for P2P "
4573 "group (GO)");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004574 nlmode = NL80211_IFTYPE_P2P_GO;
4575 } else
4576 nlmode = NL80211_IFTYPE_AP;
4577
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004578 old_mode = drv->nlmode;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004579 if (wpa_driver_nl80211_set_mode(drv->first_bss, nlmode)) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004580 nl80211_remove_monitor_interface(drv);
4581 return -1;
4582 }
4583
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08004584 if (params->freq.freq &&
4585 nl80211_set_channel(drv->first_bss, &params->freq, 0)) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004586 if (old_mode != nlmode)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004587 wpa_driver_nl80211_set_mode(drv->first_bss, old_mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004588 nl80211_remove_monitor_interface(drv);
4589 return -1;
4590 }
4591
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004592 return 0;
4593}
4594
4595
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004596static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv,
4597 int reset_mode)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004598{
4599 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004600 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004601
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004602 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_LEAVE_IBSS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004603 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004604 if (ret) {
4605 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS failed: ret=%d "
4606 "(%s)", ret, strerror(-ret));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004607 } else {
4608 wpa_printf(MSG_DEBUG,
4609 "nl80211: Leave IBSS request sent successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004610 }
4611
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004612 if (reset_mode &&
4613 wpa_driver_nl80211_set_mode(drv->first_bss,
Dmitry Shmidt56052862013-10-04 10:23:25 -07004614 NL80211_IFTYPE_STATION)) {
4615 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
4616 "station mode");
4617 }
4618
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004619 return ret;
4620}
4621
4622
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08004623static int nl80211_ht_vht_overrides(struct nl_msg *msg,
4624 struct wpa_driver_associate_params *params)
4625{
4626 if (params->disable_ht && nla_put_flag(msg, NL80211_ATTR_DISABLE_HT))
4627 return -1;
4628
4629 if (params->htcaps && params->htcaps_mask) {
4630 int sz = sizeof(struct ieee80211_ht_capabilities);
4631 wpa_hexdump(MSG_DEBUG, " * htcaps", params->htcaps, sz);
4632 wpa_hexdump(MSG_DEBUG, " * htcaps_mask",
4633 params->htcaps_mask, sz);
4634 if (nla_put(msg, NL80211_ATTR_HT_CAPABILITY, sz,
4635 params->htcaps) ||
4636 nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK, sz,
4637 params->htcaps_mask))
4638 return -1;
4639 }
4640
4641#ifdef CONFIG_VHT_OVERRIDES
4642 if (params->disable_vht) {
4643 wpa_printf(MSG_DEBUG, " * VHT disabled");
4644 if (nla_put_flag(msg, NL80211_ATTR_DISABLE_VHT))
4645 return -1;
4646 }
4647
4648 if (params->vhtcaps && params->vhtcaps_mask) {
4649 int sz = sizeof(struct ieee80211_vht_capabilities);
4650 wpa_hexdump(MSG_DEBUG, " * vhtcaps", params->vhtcaps, sz);
4651 wpa_hexdump(MSG_DEBUG, " * vhtcaps_mask",
4652 params->vhtcaps_mask, sz);
4653 if (nla_put(msg, NL80211_ATTR_VHT_CAPABILITY, sz,
4654 params->vhtcaps) ||
4655 nla_put(msg, NL80211_ATTR_VHT_CAPABILITY_MASK, sz,
4656 params->vhtcaps_mask))
4657 return -1;
4658 }
4659#endif /* CONFIG_VHT_OVERRIDES */
4660
4661 return 0;
4662}
4663
4664
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004665static int wpa_driver_nl80211_ibss(struct wpa_driver_nl80211_data *drv,
4666 struct wpa_driver_associate_params *params)
4667{
4668 struct nl_msg *msg;
4669 int ret = -1;
4670 int count = 0;
4671
4672 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS (ifindex=%d)", drv->ifindex);
4673
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004674 if (wpa_driver_nl80211_set_mode_ibss(drv->first_bss, &params->freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004675 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
4676 "IBSS mode");
4677 return -1;
4678 }
4679
4680retry:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004681 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_JOIN_IBSS)) ||
4682 params->ssid == NULL || params->ssid_len > sizeof(drv->ssid))
4683 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004684
4685 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
4686 params->ssid, params->ssid_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004687 if (nla_put(msg, NL80211_ATTR_SSID, params->ssid_len, params->ssid))
4688 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004689 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
4690 drv->ssid_len = params->ssid_len;
4691
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004692 if (nl80211_put_freq_params(msg, &params->freq) < 0 ||
4693 nl80211_put_beacon_int(msg, params->beacon_int))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004694 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004695
4696 ret = nl80211_set_conn_keys(params, msg);
4697 if (ret)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004698 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004699
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004700 if (params->bssid && params->fixed_bssid) {
4701 wpa_printf(MSG_DEBUG, " * BSSID=" MACSTR,
4702 MAC2STR(params->bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004703 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid))
4704 goto fail;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004705 }
4706
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004707 if (params->fixed_freq) {
4708 wpa_printf(MSG_DEBUG, " * fixed_freq");
4709 if (nla_put_flag(msg, NL80211_ATTR_FREQ_FIXED))
4710 goto fail;
4711 }
4712
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004713 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
4714 params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
4715 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
4716 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004717 wpa_printf(MSG_DEBUG, " * control port");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004718 if (nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT))
4719 goto fail;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004720 }
4721
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004722 if (params->wpa_ie) {
4723 wpa_hexdump(MSG_DEBUG,
4724 " * Extra IEs for Beacon/Probe Response frames",
4725 params->wpa_ie, params->wpa_ie_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004726 if (nla_put(msg, NL80211_ATTR_IE, params->wpa_ie_len,
4727 params->wpa_ie))
4728 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004729 }
4730
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08004731 ret = nl80211_ht_vht_overrides(msg, params);
4732 if (ret < 0)
4733 goto fail;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08004734
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004735 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4736 msg = NULL;
4737 if (ret) {
4738 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS failed: ret=%d (%s)",
4739 ret, strerror(-ret));
4740 count++;
4741 if (ret == -EALREADY && count == 1) {
4742 wpa_printf(MSG_DEBUG, "nl80211: Retry IBSS join after "
4743 "forced leave");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004744 nl80211_leave_ibss(drv, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004745 nlmsg_free(msg);
4746 goto retry;
4747 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004748 } else {
4749 wpa_printf(MSG_DEBUG,
4750 "nl80211: Join IBSS request sent successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004751 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004752
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004753fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004754 nlmsg_free(msg);
4755 return ret;
4756}
4757
4758
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004759static int nl80211_connect_common(struct wpa_driver_nl80211_data *drv,
4760 struct wpa_driver_associate_params *params,
4761 struct nl_msg *msg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004762{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004763 if (params->bssid) {
4764 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
4765 MAC2STR(params->bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004766 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid))
4767 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004768 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004769
Dmitry Shmidt96be6222014-02-13 10:16:51 -08004770 if (params->bssid_hint) {
4771 wpa_printf(MSG_DEBUG, " * bssid_hint=" MACSTR,
4772 MAC2STR(params->bssid_hint));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004773 if (nla_put(msg, NL80211_ATTR_MAC_HINT, ETH_ALEN,
4774 params->bssid_hint))
4775 return -1;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08004776 }
4777
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004778 if (params->freq.freq) {
4779 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq.freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004780 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
4781 params->freq.freq))
4782 return -1;
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004783 drv->assoc_freq = params->freq.freq;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004784 } else
4785 drv->assoc_freq = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004786
Dmitry Shmidt96be6222014-02-13 10:16:51 -08004787 if (params->freq_hint) {
4788 wpa_printf(MSG_DEBUG, " * freq_hint=%d", params->freq_hint);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004789 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ_HINT,
4790 params->freq_hint))
4791 return -1;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08004792 }
4793
Dmitry Shmidt04949592012-07-19 12:16:46 -07004794 if (params->bg_scan_period >= 0) {
4795 wpa_printf(MSG_DEBUG, " * bg scan period=%d",
4796 params->bg_scan_period);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004797 if (nla_put_u16(msg, NL80211_ATTR_BG_SCAN_PERIOD,
4798 params->bg_scan_period))
4799 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004800 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004801
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004802 if (params->ssid) {
4803 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
4804 params->ssid, params->ssid_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004805 if (nla_put(msg, NL80211_ATTR_SSID, params->ssid_len,
4806 params->ssid))
4807 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004808 if (params->ssid_len > sizeof(drv->ssid))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004809 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004810 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
4811 drv->ssid_len = params->ssid_len;
4812 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004813
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004814 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004815 if (params->wpa_ie &&
4816 nla_put(msg, NL80211_ATTR_IE, params->wpa_ie_len, params->wpa_ie))
4817 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004818
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004819 if (params->wpa_proto) {
4820 enum nl80211_wpa_versions ver = 0;
4821
4822 if (params->wpa_proto & WPA_PROTO_WPA)
4823 ver |= NL80211_WPA_VERSION_1;
4824 if (params->wpa_proto & WPA_PROTO_RSN)
4825 ver |= NL80211_WPA_VERSION_2;
4826
4827 wpa_printf(MSG_DEBUG, " * WPA Versions 0x%x", ver);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004828 if (nla_put_u32(msg, NL80211_ATTR_WPA_VERSIONS, ver))
4829 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004830 }
4831
4832 if (params->pairwise_suite != WPA_CIPHER_NONE) {
4833 u32 cipher = wpa_cipher_to_cipher_suite(params->pairwise_suite);
4834 wpa_printf(MSG_DEBUG, " * pairwise=0x%x", cipher);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004835 if (nla_put_u32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
4836 cipher))
4837 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004838 }
4839
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08004840 if (params->group_suite == WPA_CIPHER_GTK_NOT_USED &&
4841 !(drv->capa.enc & WPA_DRIVER_CAPA_ENC_GTK_NOT_USED)) {
4842 /*
4843 * This is likely to work even though many drivers do not
4844 * advertise support for operations without GTK.
4845 */
4846 wpa_printf(MSG_DEBUG, " * skip group cipher configuration for GTK_NOT_USED due to missing driver support advertisement");
4847 } else if (params->group_suite != WPA_CIPHER_NONE) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004848 u32 cipher = wpa_cipher_to_cipher_suite(params->group_suite);
4849 wpa_printf(MSG_DEBUG, " * group=0x%x", cipher);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004850 if (nla_put_u32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher))
4851 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004852 }
4853
4854 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
4855 params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
4856 params->key_mgmt_suite == WPA_KEY_MGMT_FT_IEEE8021X ||
4857 params->key_mgmt_suite == WPA_KEY_MGMT_FT_PSK ||
Dmitry Shmidt15907092014-03-25 10:42:57 -07004858 params->key_mgmt_suite == WPA_KEY_MGMT_CCKM ||
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -07004859 params->key_mgmt_suite == WPA_KEY_MGMT_OSEN ||
4860 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004861 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256 ||
Dmitry Shmidt807291d2015-01-27 13:40:23 -08004862 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SUITE_B ||
4863 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004864 int mgmt = WLAN_AKM_SUITE_PSK;
4865
4866 switch (params->key_mgmt_suite) {
4867 case WPA_KEY_MGMT_CCKM:
4868 mgmt = WLAN_AKM_SUITE_CCKM;
4869 break;
4870 case WPA_KEY_MGMT_IEEE8021X:
4871 mgmt = WLAN_AKM_SUITE_8021X;
4872 break;
4873 case WPA_KEY_MGMT_FT_IEEE8021X:
4874 mgmt = WLAN_AKM_SUITE_FT_8021X;
4875 break;
4876 case WPA_KEY_MGMT_FT_PSK:
4877 mgmt = WLAN_AKM_SUITE_FT_PSK;
4878 break;
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -07004879 case WPA_KEY_MGMT_IEEE8021X_SHA256:
4880 mgmt = WLAN_AKM_SUITE_8021X_SHA256;
4881 break;
4882 case WPA_KEY_MGMT_PSK_SHA256:
4883 mgmt = WLAN_AKM_SUITE_PSK_SHA256;
4884 break;
Dmitry Shmidt15907092014-03-25 10:42:57 -07004885 case WPA_KEY_MGMT_OSEN:
4886 mgmt = WLAN_AKM_SUITE_OSEN;
4887 break;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004888 case WPA_KEY_MGMT_IEEE8021X_SUITE_B:
4889 mgmt = WLAN_AKM_SUITE_8021X_SUITE_B;
4890 break;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08004891 case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192:
4892 mgmt = WLAN_AKM_SUITE_8021X_SUITE_B_192;
4893 break;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004894 case WPA_KEY_MGMT_PSK:
4895 default:
4896 mgmt = WLAN_AKM_SUITE_PSK;
4897 break;
4898 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07004899 wpa_printf(MSG_DEBUG, " * akm=0x%x", mgmt);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004900 if (nla_put_u32(msg, NL80211_ATTR_AKM_SUITES, mgmt))
4901 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004902 }
4903
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004904 if (nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT))
4905 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004906
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004907 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED &&
4908 nla_put_u32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED))
4909 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004910
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004911 if (params->rrm_used) {
4912 u32 drv_rrm_flags = drv->capa.rrm_flags;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004913 if ((!((drv_rrm_flags &
4914 WPA_DRIVER_FLAGS_DS_PARAM_SET_IE_IN_PROBES) &&
4915 (drv_rrm_flags & WPA_DRIVER_FLAGS_QUIET)) &&
4916 !(drv_rrm_flags & WPA_DRIVER_FLAGS_SUPPORT_RRM)) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004917 nla_put_flag(msg, NL80211_ATTR_USE_RRM))
4918 return -1;
4919 }
4920
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08004921 if (nl80211_ht_vht_overrides(msg, params) < 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004922 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004923
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004924 if (params->p2p)
4925 wpa_printf(MSG_DEBUG, " * P2P group");
4926
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004927 if (params->pbss) {
4928 wpa_printf(MSG_DEBUG, " * PBSS");
4929 if (nla_put_flag(msg, NL80211_ATTR_PBSS))
4930 return -1;
4931 }
4932
Dmitry Shmidte4663042016-04-04 10:07:49 -07004933 drv->connect_reassoc = 0;
4934 if (params->prev_bssid) {
4935 wpa_printf(MSG_DEBUG, " * prev_bssid=" MACSTR,
4936 MAC2STR(params->prev_bssid));
4937 if (nla_put(msg, NL80211_ATTR_PREV_BSSID, ETH_ALEN,
4938 params->prev_bssid))
4939 return -1;
4940 drv->connect_reassoc = 1;
4941 }
4942
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004943 return 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004944}
4945
4946
4947static int wpa_driver_nl80211_try_connect(
4948 struct wpa_driver_nl80211_data *drv,
4949 struct wpa_driver_associate_params *params)
4950{
4951 struct nl_msg *msg;
4952 enum nl80211_auth_type type;
4953 int ret;
4954 int algs;
4955
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004956#ifdef CONFIG_DRIVER_NL80211_QCA
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004957 if (params->req_key_mgmt_offload && params->psk &&
4958 (params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
4959 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256 ||
4960 params->key_mgmt_suite == WPA_KEY_MGMT_FT_PSK)) {
4961 wpa_printf(MSG_DEBUG, "nl80211: Key management set PSK");
4962 ret = issue_key_mgmt_set_key(drv, params->psk, 32);
4963 if (ret)
4964 return ret;
4965 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004966#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004967
4968 wpa_printf(MSG_DEBUG, "nl80211: Connect (ifindex=%d)", drv->ifindex);
4969 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_CONNECT);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004970 if (!msg)
4971 return -1;
4972
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004973 ret = nl80211_connect_common(drv, params, msg);
4974 if (ret)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004975 goto fail;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004976
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004977 algs = 0;
4978 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
4979 algs++;
4980 if (params->auth_alg & WPA_AUTH_ALG_SHARED)
4981 algs++;
4982 if (params->auth_alg & WPA_AUTH_ALG_LEAP)
4983 algs++;
4984 if (algs > 1) {
4985 wpa_printf(MSG_DEBUG, " * Leave out Auth Type for automatic "
4986 "selection");
4987 goto skip_auth_type;
4988 }
4989
4990 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
4991 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
4992 else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
4993 type = NL80211_AUTHTYPE_SHARED_KEY;
4994 else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
4995 type = NL80211_AUTHTYPE_NETWORK_EAP;
4996 else if (params->auth_alg & WPA_AUTH_ALG_FT)
4997 type = NL80211_AUTHTYPE_FT;
4998 else
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004999 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005000
5001 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005002 if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE, type))
5003 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005004
5005skip_auth_type:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005006 ret = nl80211_set_conn_keys(params, msg);
5007 if (ret)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005008 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005009
5010 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5011 msg = NULL;
5012 if (ret) {
5013 wpa_printf(MSG_DEBUG, "nl80211: MLME connect failed: ret=%d "
5014 "(%s)", ret, strerror(-ret));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005015 } else {
5016 wpa_printf(MSG_DEBUG,
5017 "nl80211: Connect request send successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005018 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005019
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005020fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005021 nlmsg_free(msg);
5022 return ret;
5023
5024}
5025
5026
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005027static int wpa_driver_nl80211_connect(
5028 struct wpa_driver_nl80211_data *drv,
5029 struct wpa_driver_associate_params *params)
5030{
Jithu Jancea7c60b42014-12-03 18:54:40 +05305031 int ret;
5032
5033 /* Store the connection attempted bssid for future use */
5034 if (params->bssid)
5035 os_memcpy(drv->auth_attempt_bssid, params->bssid, ETH_ALEN);
5036 else
5037 os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
5038
5039 ret = wpa_driver_nl80211_try_connect(drv, params);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005040 if (ret == -EALREADY) {
5041 /*
5042 * cfg80211 does not currently accept new connections if
5043 * we are already connected. As a workaround, force
5044 * disconnection and try again.
5045 */
5046 wpa_printf(MSG_DEBUG, "nl80211: Explicitly "
5047 "disconnecting before reassociation "
5048 "attempt");
5049 if (wpa_driver_nl80211_disconnect(
5050 drv, WLAN_REASON_PREV_AUTH_NOT_VALID))
5051 return -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005052 ret = wpa_driver_nl80211_try_connect(drv, params);
5053 }
5054 return ret;
5055}
5056
5057
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005058static int wpa_driver_nl80211_associate(
5059 void *priv, struct wpa_driver_associate_params *params)
5060{
5061 struct i802_bss *bss = priv;
5062 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005063 int ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005064 struct nl_msg *msg;
5065
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005066 nl80211_unmask_11b_rates(bss);
5067
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005068 if (params->mode == IEEE80211_MODE_AP)
5069 return wpa_driver_nl80211_ap(drv, params);
5070
5071 if (params->mode == IEEE80211_MODE_IBSS)
5072 return wpa_driver_nl80211_ibss(drv, params);
5073
5074 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005075 enum nl80211_iftype nlmode = params->p2p ?
5076 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
5077
5078 if (wpa_driver_nl80211_set_mode(priv, nlmode) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005079 return -1;
5080 return wpa_driver_nl80211_connect(drv, params);
5081 }
5082
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005083 nl80211_mark_disconnected(drv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005084
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005085 wpa_printf(MSG_DEBUG, "nl80211: Associate (ifindex=%d)",
5086 drv->ifindex);
5087 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_ASSOCIATE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005088 if (!msg)
5089 return -1;
5090
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005091 ret = nl80211_connect_common(drv, params, msg);
5092 if (ret)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005093 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005094
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005095 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5096 msg = NULL;
5097 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005098 wpa_dbg(drv->ctx, MSG_DEBUG,
5099 "nl80211: MLME command failed (assoc): ret=%d (%s)",
5100 ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005101 nl80211_dump_scan(drv);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005102 } else {
5103 wpa_printf(MSG_DEBUG,
5104 "nl80211: Association request send successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005105 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005106
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005107fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005108 nlmsg_free(msg);
5109 return ret;
5110}
5111
5112
5113static int nl80211_set_mode(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005114 int ifindex, enum nl80211_iftype mode)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005115{
5116 struct nl_msg *msg;
5117 int ret = -ENOBUFS;
5118
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005119 wpa_printf(MSG_DEBUG, "nl80211: Set mode ifindex %d iftype %d (%s)",
5120 ifindex, mode, nl80211_iftype_str(mode));
5121
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005122 msg = nl80211_cmd_msg(drv->first_bss, 0, NL80211_CMD_SET_INTERFACE);
5123 if (!msg || nla_put_u32(msg, NL80211_ATTR_IFTYPE, mode))
5124 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005125
5126 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005127 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005128 if (!ret)
5129 return 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005130fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005131 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005132 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface %d to mode %d:"
5133 " %d (%s)", ifindex, mode, ret, strerror(-ret));
5134 return ret;
5135}
5136
5137
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005138static int wpa_driver_nl80211_set_mode_impl(
5139 struct i802_bss *bss,
5140 enum nl80211_iftype nlmode,
5141 struct hostapd_freq_params *desired_freq_params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005142{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005143 struct wpa_driver_nl80211_data *drv = bss->drv;
5144 int ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005145 int i;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005146 int was_ap = is_ap_interface(drv->nlmode);
5147 int res;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005148 int mode_switch_res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005149
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07005150 if (TEST_FAIL())
5151 return -1;
5152
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005153 mode_switch_res = nl80211_set_mode(drv, drv->ifindex, nlmode);
5154 if (mode_switch_res && nlmode == nl80211_get_ifmode(bss))
5155 mode_switch_res = 0;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005156
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005157 if (mode_switch_res == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005158 drv->nlmode = nlmode;
5159 ret = 0;
5160 goto done;
5161 }
5162
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005163 if (mode_switch_res == -ENODEV)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005164 return -1;
5165
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005166 if (nlmode == drv->nlmode) {
5167 wpa_printf(MSG_DEBUG, "nl80211: Interface already in "
5168 "requested mode - ignore error");
5169 ret = 0;
5170 goto done; /* Already in the requested mode */
5171 }
5172
5173 /* mac80211 doesn't allow mode changes while the device is up, so
5174 * take the device down, try to set the mode again, and bring the
5175 * device back up.
5176 */
5177 wpa_printf(MSG_DEBUG, "nl80211: Try mode change after setting "
5178 "interface down");
5179 for (i = 0; i < 10; i++) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005180 res = i802_set_iface_flags(bss, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005181 if (res == -EACCES || res == -ENODEV)
5182 break;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005183 if (res != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005184 wpa_printf(MSG_DEBUG, "nl80211: Failed to set "
5185 "interface down");
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005186 os_sleep(0, 100000);
5187 continue;
5188 }
5189
5190 /*
5191 * Setting the mode will fail for some drivers if the phy is
5192 * on a frequency that the mode is disallowed in.
5193 */
5194 if (desired_freq_params) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005195 res = nl80211_set_channel(bss, desired_freq_params, 0);
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005196 if (res) {
5197 wpa_printf(MSG_DEBUG,
5198 "nl80211: Failed to set frequency on interface");
5199 }
5200 }
5201
5202 /* Try to set the mode again while the interface is down */
5203 mode_switch_res = nl80211_set_mode(drv, drv->ifindex, nlmode);
5204 if (mode_switch_res == -EBUSY) {
5205 wpa_printf(MSG_DEBUG,
5206 "nl80211: Delaying mode set while interface going down");
5207 os_sleep(0, 100000);
5208 continue;
5209 }
5210 ret = mode_switch_res;
5211 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005212 }
5213
5214 if (!ret) {
5215 wpa_printf(MSG_DEBUG, "nl80211: Mode change succeeded while "
5216 "interface is down");
5217 drv->nlmode = nlmode;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005218 drv->ignore_if_down_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005219 }
5220
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005221 /* Bring the interface back up */
5222 res = linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1);
5223 if (res != 0) {
5224 wpa_printf(MSG_DEBUG,
5225 "nl80211: Failed to set interface up after switching mode");
5226 ret = -1;
5227 }
5228
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005229done:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005230 if (ret) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005231 wpa_printf(MSG_DEBUG, "nl80211: Interface mode change to %d "
5232 "from %d failed", nlmode, drv->nlmode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005233 return ret;
5234 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005235
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005236 if (is_p2p_net_interface(nlmode)) {
5237 wpa_printf(MSG_DEBUG,
5238 "nl80211: Interface %s mode change to P2P - disable 11b rates",
5239 bss->ifname);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005240 nl80211_disable_11b_rates(drv, drv->ifindex, 1);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005241 } else if (drv->disabled_11b_rates) {
5242 wpa_printf(MSG_DEBUG,
5243 "nl80211: Interface %s mode changed to non-P2P - re-enable 11b rates",
5244 bss->ifname);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005245 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005246 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005247
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005248 if (is_ap_interface(nlmode)) {
5249 nl80211_mgmt_unsubscribe(bss, "start AP");
5250 /* Setup additional AP mode functionality if needed */
5251 if (nl80211_setup_ap(bss))
5252 return -1;
5253 } else if (was_ap) {
5254 /* Remove additional AP mode functionality */
5255 nl80211_teardown_ap(bss);
5256 } else {
5257 nl80211_mgmt_unsubscribe(bss, "mode change");
5258 }
5259
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005260 if (is_mesh_interface(nlmode) &&
5261 nl80211_mgmt_subscribe_mesh(bss))
5262 return -1;
5263
Dmitry Shmidt04949592012-07-19 12:16:46 -07005264 if (!bss->in_deinit && !is_ap_interface(nlmode) &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005265 !is_mesh_interface(nlmode) &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005266 nl80211_mgmt_subscribe_non_ap(bss) < 0)
5267 wpa_printf(MSG_DEBUG, "nl80211: Failed to register Action "
5268 "frame processing - ignore for now");
5269
5270 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005271}
5272
5273
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005274int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
5275 enum nl80211_iftype nlmode)
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005276{
5277 return wpa_driver_nl80211_set_mode_impl(bss, nlmode, NULL);
5278}
5279
5280
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07005281static int wpa_driver_nl80211_set_mode_ibss(struct i802_bss *bss,
5282 struct hostapd_freq_params *freq)
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005283{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005284 return wpa_driver_nl80211_set_mode_impl(bss, NL80211_IFTYPE_ADHOC,
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07005285 freq);
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005286}
5287
5288
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005289static int wpa_driver_nl80211_get_capa(void *priv,
5290 struct wpa_driver_capa *capa)
5291{
5292 struct i802_bss *bss = priv;
5293 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07005294
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005295 if (!drv->has_capability)
5296 return -1;
5297 os_memcpy(capa, &drv->capa, sizeof(*capa));
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005298 if (drv->extended_capa && drv->extended_capa_mask) {
5299 capa->extended_capa = drv->extended_capa;
5300 capa->extended_capa_mask = drv->extended_capa_mask;
5301 capa->extended_capa_len = drv->extended_capa_len;
5302 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005303
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005304 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005305}
5306
5307
5308static int wpa_driver_nl80211_set_operstate(void *priv, int state)
5309{
5310 struct i802_bss *bss = priv;
5311 struct wpa_driver_nl80211_data *drv = bss->drv;
5312
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005313 wpa_printf(MSG_DEBUG, "nl80211: Set %s operstate %d->%d (%s)",
5314 bss->ifname, drv->operstate, state,
5315 state ? "UP" : "DORMANT");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005316 drv->operstate = state;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005317 return netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, -1,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005318 state ? IF_OPER_UP : IF_OPER_DORMANT);
5319}
5320
5321
5322static int wpa_driver_nl80211_set_supp_port(void *priv, int authorized)
5323{
5324 struct i802_bss *bss = priv;
5325 struct wpa_driver_nl80211_data *drv = bss->drv;
5326 struct nl_msg *msg;
5327 struct nl80211_sta_flag_update upd;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005328 int ret;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005329
5330 if (!drv->associated && is_zero_ether_addr(drv->bssid) && !authorized) {
5331 wpa_printf(MSG_DEBUG, "nl80211: Skip set_supp_port(unauthorized) while not associated");
5332 return 0;
5333 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005334
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005335 wpa_printf(MSG_DEBUG, "nl80211: Set supplicant port %sauthorized for "
5336 MACSTR, authorized ? "" : "un", MAC2STR(drv->bssid));
5337
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005338 os_memset(&upd, 0, sizeof(upd));
5339 upd.mask = BIT(NL80211_STA_FLAG_AUTHORIZED);
5340 if (authorized)
5341 upd.set = BIT(NL80211_STA_FLAG_AUTHORIZED);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005342
5343 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
5344 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid) ||
5345 nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd)) {
5346 nlmsg_free(msg);
5347 return -ENOBUFS;
5348 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005349
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005350 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005351 if (!ret)
5352 return 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005353 wpa_printf(MSG_DEBUG, "nl80211: Failed to set STA flag: %d (%s)",
5354 ret, strerror(-ret));
5355 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005356}
5357
5358
Jouni Malinen75ecf522011-06-27 15:19:46 -07005359/* Set kernel driver on given frequency (MHz) */
5360static int i802_set_freq(void *priv, struct hostapd_freq_params *freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005361{
Jouni Malinen75ecf522011-06-27 15:19:46 -07005362 struct i802_bss *bss = priv;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07005363 return nl80211_set_channel(bss, freq, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005364}
5365
5366
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005367static inline int min_int(int a, int b)
5368{
5369 if (a < b)
5370 return a;
5371 return b;
5372}
5373
5374
5375static int get_key_handler(struct nl_msg *msg, void *arg)
5376{
5377 struct nlattr *tb[NL80211_ATTR_MAX + 1];
5378 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
5379
5380 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
5381 genlmsg_attrlen(gnlh, 0), NULL);
5382
5383 /*
5384 * TODO: validate the key index and mac address!
5385 * Otherwise, there's a race condition as soon as
5386 * the kernel starts sending key notifications.
5387 */
5388
5389 if (tb[NL80211_ATTR_KEY_SEQ])
5390 memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]),
5391 min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6));
5392 return NL_SKIP;
5393}
5394
5395
5396static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
5397 int idx, u8 *seq)
5398{
5399 struct i802_bss *bss = priv;
5400 struct wpa_driver_nl80211_data *drv = bss->drv;
5401 struct nl_msg *msg;
5402
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005403 msg = nl80211_ifindex_msg(drv, if_nametoindex(iface), 0,
5404 NL80211_CMD_GET_KEY);
5405 if (!msg ||
5406 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
5407 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, idx)) {
5408 nlmsg_free(msg);
5409 return -ENOBUFS;
5410 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005411
5412 memset(seq, 0, 6);
5413
5414 return send_and_recv_msgs(drv, msg, get_key_handler, seq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005415}
5416
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005417
5418static int i802_set_rts(void *priv, int rts)
5419{
5420 struct i802_bss *bss = priv;
5421 struct wpa_driver_nl80211_data *drv = bss->drv;
5422 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005423 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005424 u32 val;
5425
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005426 if (rts >= 2347)
5427 val = (u32) -1;
5428 else
5429 val = rts;
5430
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005431 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_WIPHY)) ||
5432 nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD, val)) {
5433 nlmsg_free(msg);
5434 return -ENOBUFS;
5435 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005436
5437 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5438 if (!ret)
5439 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005440 wpa_printf(MSG_DEBUG, "nl80211: Failed to set RTS threshold %d: "
5441 "%d (%s)", rts, ret, strerror(-ret));
5442 return ret;
5443}
5444
5445
5446static int i802_set_frag(void *priv, int frag)
5447{
5448 struct i802_bss *bss = priv;
5449 struct wpa_driver_nl80211_data *drv = bss->drv;
5450 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005451 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005452 u32 val;
5453
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005454 if (frag >= 2346)
5455 val = (u32) -1;
5456 else
5457 val = frag;
5458
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005459 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_WIPHY)) ||
5460 nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD, val)) {
5461 nlmsg_free(msg);
5462 return -ENOBUFS;
5463 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005464
5465 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5466 if (!ret)
5467 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005468 wpa_printf(MSG_DEBUG, "nl80211: Failed to set fragmentation threshold "
5469 "%d: %d (%s)", frag, ret, strerror(-ret));
5470 return ret;
5471}
5472
5473
5474static int i802_flush(void *priv)
5475{
5476 struct i802_bss *bss = priv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005477 struct nl_msg *msg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005478 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005479
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005480 wpa_printf(MSG_DEBUG, "nl80211: flush -> DEL_STATION %s (all)",
5481 bss->ifname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005482
5483 /*
5484 * XXX: FIX! this needs to flush all VLANs too
5485 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005486 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_DEL_STATION);
5487 res = send_and_recv_msgs(bss->drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005488 if (res) {
5489 wpa_printf(MSG_DEBUG, "nl80211: Station flush failed: ret=%d "
5490 "(%s)", res, strerror(-res));
5491 }
5492 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005493}
5494
5495
5496static int get_sta_handler(struct nl_msg *msg, void *arg)
5497{
5498 struct nlattr *tb[NL80211_ATTR_MAX + 1];
5499 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
5500 struct hostap_sta_driver_data *data = arg;
5501 struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
5502 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
5503 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
5504 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
5505 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
5506 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
5507 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03005508 [NL80211_STA_INFO_TX_FAILED] = { .type = NLA_U32 },
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005509 [NL80211_STA_INFO_RX_BYTES64] = { .type = NLA_U64 },
5510 [NL80211_STA_INFO_TX_BYTES64] = { .type = NLA_U64 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005511 };
5512
5513 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
5514 genlmsg_attrlen(gnlh, 0), NULL);
5515
5516 /*
5517 * TODO: validate the interface and mac address!
5518 * Otherwise, there's a race condition as soon as
5519 * the kernel starts sending station notifications.
5520 */
5521
5522 if (!tb[NL80211_ATTR_STA_INFO]) {
5523 wpa_printf(MSG_DEBUG, "sta stats missing!");
5524 return NL_SKIP;
5525 }
5526 if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
5527 tb[NL80211_ATTR_STA_INFO],
5528 stats_policy)) {
5529 wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
5530 return NL_SKIP;
5531 }
5532
5533 if (stats[NL80211_STA_INFO_INACTIVE_TIME])
5534 data->inactive_msec =
5535 nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005536 /* For backwards compatibility, fetch the 32-bit counters first. */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005537 if (stats[NL80211_STA_INFO_RX_BYTES])
5538 data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
5539 if (stats[NL80211_STA_INFO_TX_BYTES])
5540 data->tx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005541 if (stats[NL80211_STA_INFO_RX_BYTES64] &&
5542 stats[NL80211_STA_INFO_TX_BYTES64]) {
5543 /*
5544 * The driver supports 64-bit counters, so use them to override
5545 * the 32-bit values.
5546 */
5547 data->rx_bytes =
5548 nla_get_u64(stats[NL80211_STA_INFO_RX_BYTES64]);
5549 data->tx_bytes =
5550 nla_get_u64(stats[NL80211_STA_INFO_TX_BYTES64]);
5551 data->bytes_64bit = 1;
5552 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005553 if (stats[NL80211_STA_INFO_RX_PACKETS])
5554 data->rx_packets =
5555 nla_get_u32(stats[NL80211_STA_INFO_RX_PACKETS]);
5556 if (stats[NL80211_STA_INFO_TX_PACKETS])
5557 data->tx_packets =
5558 nla_get_u32(stats[NL80211_STA_INFO_TX_PACKETS]);
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03005559 if (stats[NL80211_STA_INFO_TX_FAILED])
5560 data->tx_retry_failed =
5561 nla_get_u32(stats[NL80211_STA_INFO_TX_FAILED]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005562
5563 return NL_SKIP;
5564}
5565
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005566static int i802_read_sta_data(struct i802_bss *bss,
5567 struct hostap_sta_driver_data *data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005568 const u8 *addr)
5569{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005570 struct nl_msg *msg;
5571
5572 os_memset(data, 0, sizeof(*data));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005573
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005574 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_GET_STATION)) ||
5575 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
5576 nlmsg_free(msg);
5577 return -ENOBUFS;
5578 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005579
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005580 return send_and_recv_msgs(bss->drv, msg, get_sta_handler, data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005581}
5582
5583
5584static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
5585 int cw_min, int cw_max, int burst_time)
5586{
5587 struct i802_bss *bss = priv;
5588 struct wpa_driver_nl80211_data *drv = bss->drv;
5589 struct nl_msg *msg;
5590 struct nlattr *txq, *params;
5591
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005592 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005593 if (!msg)
5594 return -1;
5595
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005596 txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS);
5597 if (!txq)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005598 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005599
5600 /* We are only sending parameters for a single TXQ at a time */
5601 params = nla_nest_start(msg, 1);
5602 if (!params)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005603 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005604
5605 switch (queue) {
5606 case 0:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005607 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VO))
5608 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005609 break;
5610 case 1:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005611 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VI))
5612 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005613 break;
5614 case 2:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005615 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BE))
5616 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005617 break;
5618 case 3:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005619 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BK))
5620 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005621 break;
5622 }
5623 /* Burst time is configured in units of 0.1 msec and TXOP parameter in
5624 * 32 usec, so need to convert the value here. */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005625 if (nla_put_u16(msg, NL80211_TXQ_ATTR_TXOP,
5626 (burst_time * 100 + 16) / 32) ||
5627 nla_put_u16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min) ||
5628 nla_put_u16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max) ||
5629 nla_put_u8(msg, NL80211_TXQ_ATTR_AIFS, aifs))
5630 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005631
5632 nla_nest_end(msg, params);
5633
5634 nla_nest_end(msg, txq);
5635
5636 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
5637 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005638 msg = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005639fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005640 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005641 return -1;
5642}
5643
5644
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005645static int i802_set_sta_vlan(struct i802_bss *bss, const u8 *addr,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005646 const char *ifname, int vlan_id)
5647{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005648 struct wpa_driver_nl80211_data *drv = bss->drv;
5649 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005650 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005651
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005652 wpa_printf(MSG_DEBUG, "nl80211: %s[%d]: set_sta_vlan(" MACSTR
5653 ", ifname=%s[%d], vlan_id=%d)",
5654 bss->ifname, if_nametoindex(bss->ifname),
5655 MAC2STR(addr), ifname, if_nametoindex(ifname), vlan_id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005656 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
5657 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
5658 nla_put_u32(msg, NL80211_ATTR_STA_VLAN, if_nametoindex(ifname))) {
5659 nlmsg_free(msg);
5660 return -ENOBUFS;
5661 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005662
5663 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5664 if (ret < 0) {
5665 wpa_printf(MSG_ERROR, "nl80211: NL80211_ATTR_STA_VLAN (addr="
5666 MACSTR " ifname=%s vlan_id=%d) failed: %d (%s)",
5667 MAC2STR(addr), ifname, vlan_id, ret,
5668 strerror(-ret));
5669 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005670 return ret;
5671}
5672
5673
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005674static int i802_get_inact_sec(void *priv, const u8 *addr)
5675{
5676 struct hostap_sta_driver_data data;
5677 int ret;
5678
5679 data.inactive_msec = (unsigned long) -1;
5680 ret = i802_read_sta_data(priv, &data, addr);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08005681 if (ret == -ENOENT)
5682 return -ENOENT;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005683 if (ret || data.inactive_msec == (unsigned long) -1)
5684 return -1;
5685 return data.inactive_msec / 1000;
5686}
5687
5688
5689static int i802_sta_clear_stats(void *priv, const u8 *addr)
5690{
5691#if 0
5692 /* TODO */
5693#endif
5694 return 0;
5695}
5696
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005697
5698static int i802_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
5699 int reason)
5700{
5701 struct i802_bss *bss = priv;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005702 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005703 struct ieee80211_mgmt mgmt;
5704
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005705 if (is_mesh_interface(drv->nlmode))
5706 return -1;
5707
Dmitry Shmidt04949592012-07-19 12:16:46 -07005708 if (drv->device_ap_sme)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005709 return wpa_driver_nl80211_sta_remove(bss, addr, 1, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005710
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005711 memset(&mgmt, 0, sizeof(mgmt));
5712 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
5713 WLAN_FC_STYPE_DEAUTH);
5714 memcpy(mgmt.da, addr, ETH_ALEN);
5715 memcpy(mgmt.sa, own_addr, ETH_ALEN);
5716 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
5717 mgmt.u.deauth.reason_code = host_to_le16(reason);
5718 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
5719 IEEE80211_HDRLEN +
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005720 sizeof(mgmt.u.deauth), 0, 0, 0, 0,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005721 0, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005722}
5723
5724
5725static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
5726 int reason)
5727{
5728 struct i802_bss *bss = priv;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005729 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005730 struct ieee80211_mgmt mgmt;
5731
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005732 if (is_mesh_interface(drv->nlmode))
5733 return -1;
5734
Dmitry Shmidt04949592012-07-19 12:16:46 -07005735 if (drv->device_ap_sme)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005736 return wpa_driver_nl80211_sta_remove(bss, addr, 0, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005737
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005738 memset(&mgmt, 0, sizeof(mgmt));
5739 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
5740 WLAN_FC_STYPE_DISASSOC);
5741 memcpy(mgmt.da, addr, ETH_ALEN);
5742 memcpy(mgmt.sa, own_addr, ETH_ALEN);
5743 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
5744 mgmt.u.disassoc.reason_code = host_to_le16(reason);
5745 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
5746 IEEE80211_HDRLEN +
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005747 sizeof(mgmt.u.disassoc), 0, 0, 0, 0,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005748 0, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005749}
5750
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005751
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07005752static void dump_ifidx(struct wpa_driver_nl80211_data *drv)
5753{
5754 char buf[200], *pos, *end;
5755 int i, res;
5756
5757 pos = buf;
5758 end = pos + sizeof(buf);
5759
5760 for (i = 0; i < drv->num_if_indices; i++) {
5761 if (!drv->if_indices[i])
5762 continue;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005763 res = os_snprintf(pos, end - pos, " %d(%d)",
5764 drv->if_indices[i],
5765 drv->if_indices_reason[i]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005766 if (os_snprintf_error(end - pos, res))
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07005767 break;
5768 pos += res;
5769 }
5770 *pos = '\0';
5771
5772 wpa_printf(MSG_DEBUG, "nl80211: if_indices[%d]:%s",
5773 drv->num_if_indices, buf);
5774}
5775
5776
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005777static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
5778 int ifidx_reason)
Jouni Malinen75ecf522011-06-27 15:19:46 -07005779{
5780 int i;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005781 int *old, *old_reason;
Jouni Malinen75ecf522011-06-27 15:19:46 -07005782
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005783 wpa_printf(MSG_DEBUG,
5784 "nl80211: Add own interface ifindex %d (ifidx_reason %d)",
5785 ifidx, ifidx_reason);
5786 if (have_ifidx(drv, ifidx, ifidx_reason)) {
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07005787 wpa_printf(MSG_DEBUG, "nl80211: ifindex %d already in the list",
5788 ifidx);
5789 return;
5790 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07005791 for (i = 0; i < drv->num_if_indices; i++) {
5792 if (drv->if_indices[i] == 0) {
5793 drv->if_indices[i] = ifidx;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005794 drv->if_indices_reason[i] = ifidx_reason;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07005795 dump_ifidx(drv);
Jouni Malinen75ecf522011-06-27 15:19:46 -07005796 return;
5797 }
5798 }
5799
5800 if (drv->if_indices != drv->default_if_indices)
5801 old = drv->if_indices;
5802 else
5803 old = NULL;
5804
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005805 if (drv->if_indices_reason != drv->default_if_indices_reason)
5806 old_reason = drv->if_indices_reason;
5807 else
5808 old_reason = NULL;
5809
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005810 drv->if_indices = os_realloc_array(old, drv->num_if_indices + 1,
5811 sizeof(int));
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005812 drv->if_indices_reason = os_realloc_array(old_reason,
5813 drv->num_if_indices + 1,
5814 sizeof(int));
Jouni Malinen75ecf522011-06-27 15:19:46 -07005815 if (!drv->if_indices) {
5816 if (!old)
5817 drv->if_indices = drv->default_if_indices;
5818 else
5819 drv->if_indices = old;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005820 }
5821 if (!drv->if_indices_reason) {
5822 if (!old_reason)
5823 drv->if_indices_reason = drv->default_if_indices_reason;
5824 else
Dmitry Shmidte4663042016-04-04 10:07:49 -07005825 drv->if_indices_reason = old_reason;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005826 }
5827 if (!drv->if_indices || !drv->if_indices_reason) {
Jouni Malinen75ecf522011-06-27 15:19:46 -07005828 wpa_printf(MSG_ERROR, "Failed to reallocate memory for "
5829 "interfaces");
5830 wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx);
5831 return;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005832 }
5833 if (!old)
Jouni Malinen75ecf522011-06-27 15:19:46 -07005834 os_memcpy(drv->if_indices, drv->default_if_indices,
5835 sizeof(drv->default_if_indices));
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005836 if (!old_reason)
5837 os_memcpy(drv->if_indices_reason,
5838 drv->default_if_indices_reason,
5839 sizeof(drv->default_if_indices_reason));
Jouni Malinen75ecf522011-06-27 15:19:46 -07005840 drv->if_indices[drv->num_if_indices] = ifidx;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005841 drv->if_indices_reason[drv->num_if_indices] = ifidx_reason;
Jouni Malinen75ecf522011-06-27 15:19:46 -07005842 drv->num_if_indices++;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07005843 dump_ifidx(drv);
Jouni Malinen75ecf522011-06-27 15:19:46 -07005844}
5845
5846
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005847static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
5848 int ifidx_reason)
Jouni Malinen75ecf522011-06-27 15:19:46 -07005849{
5850 int i;
5851
5852 for (i = 0; i < drv->num_if_indices; i++) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005853 if ((drv->if_indices[i] == ifidx || ifidx == IFIDX_ANY) &&
5854 (drv->if_indices_reason[i] == ifidx_reason ||
5855 ifidx_reason == IFIDX_ANY)) {
Jouni Malinen75ecf522011-06-27 15:19:46 -07005856 drv->if_indices[i] = 0;
5857 break;
5858 }
5859 }
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07005860 dump_ifidx(drv);
Jouni Malinen75ecf522011-06-27 15:19:46 -07005861}
5862
5863
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005864static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
5865 int ifidx_reason)
Jouni Malinen75ecf522011-06-27 15:19:46 -07005866{
5867 int i;
5868
5869 for (i = 0; i < drv->num_if_indices; i++)
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005870 if (drv->if_indices[i] == ifidx &&
5871 (drv->if_indices_reason[i] == ifidx_reason ||
5872 ifidx_reason == IFIDX_ANY))
Jouni Malinen75ecf522011-06-27 15:19:46 -07005873 return 1;
5874
5875 return 0;
5876}
5877
5878
5879static int i802_set_wds_sta(void *priv, const u8 *addr, int aid, int val,
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07005880 const char *bridge_ifname, char *ifname_wds)
Jouni Malinen75ecf522011-06-27 15:19:46 -07005881{
5882 struct i802_bss *bss = priv;
5883 struct wpa_driver_nl80211_data *drv = bss->drv;
5884 char name[IFNAMSIZ + 1];
5885
5886 os_snprintf(name, sizeof(name), "%s.sta%d", bss->ifname, aid);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005887 if (ifname_wds)
5888 os_strlcpy(ifname_wds, name, IFNAMSIZ + 1);
5889
Jouni Malinen75ecf522011-06-27 15:19:46 -07005890 wpa_printf(MSG_DEBUG, "nl80211: Set WDS STA addr=" MACSTR
5891 " aid=%d val=%d name=%s", MAC2STR(addr), aid, val, name);
5892 if (val) {
5893 if (!if_nametoindex(name)) {
5894 if (nl80211_create_iface(drv, name,
5895 NL80211_IFTYPE_AP_VLAN,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005896 bss->addr, 1, NULL, NULL, 0) <
5897 0)
Jouni Malinen75ecf522011-06-27 15:19:46 -07005898 return -1;
5899 if (bridge_ifname &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005900 linux_br_add_if(drv->global->ioctl_sock,
5901 bridge_ifname, name) < 0)
Jouni Malinen75ecf522011-06-27 15:19:46 -07005902 return -1;
5903 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005904 if (linux_set_iface_flags(drv->global->ioctl_sock, name, 1)) {
5905 wpa_printf(MSG_ERROR, "nl80211: Failed to set WDS STA "
5906 "interface %s up", name);
5907 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07005908 return i802_set_sta_vlan(priv, addr, name, 0);
5909 } else {
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005910 if (bridge_ifname)
5911 linux_br_del_if(drv->global->ioctl_sock, bridge_ifname,
5912 name);
5913
Jouni Malinen75ecf522011-06-27 15:19:46 -07005914 i802_set_sta_vlan(priv, addr, bss->ifname, 0);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08005915 nl80211_remove_iface(drv, if_nametoindex(name));
5916 return 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07005917 }
5918}
5919
5920
5921static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
5922{
5923 struct wpa_driver_nl80211_data *drv = eloop_ctx;
5924 struct sockaddr_ll lladdr;
5925 unsigned char buf[3000];
5926 int len;
5927 socklen_t fromlen = sizeof(lladdr);
5928
5929 len = recvfrom(sock, buf, sizeof(buf), 0,
5930 (struct sockaddr *)&lladdr, &fromlen);
5931 if (len < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005932 wpa_printf(MSG_ERROR, "nl80211: EAPOL recv failed: %s",
5933 strerror(errno));
Jouni Malinen75ecf522011-06-27 15:19:46 -07005934 return;
5935 }
5936
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005937 if (have_ifidx(drv, lladdr.sll_ifindex, IFIDX_ANY))
Jouni Malinen75ecf522011-06-27 15:19:46 -07005938 drv_event_eapol_rx(drv->ctx, lladdr.sll_addr, buf, len);
5939}
5940
5941
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005942static int i802_check_bridge(struct wpa_driver_nl80211_data *drv,
5943 struct i802_bss *bss,
5944 const char *brname, const char *ifname)
5945{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005946 int br_ifindex;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005947 char in_br[IFNAMSIZ];
5948
5949 os_strlcpy(bss->brname, brname, IFNAMSIZ);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005950 br_ifindex = if_nametoindex(brname);
5951 if (br_ifindex == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005952 /*
5953 * Bridge was configured, but the bridge device does
5954 * not exist. Try to add it now.
5955 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005956 if (linux_br_add(drv->global->ioctl_sock, brname) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005957 wpa_printf(MSG_ERROR, "nl80211: Failed to add the "
5958 "bridge interface %s: %s",
5959 brname, strerror(errno));
5960 return -1;
5961 }
5962 bss->added_bridge = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005963 br_ifindex = if_nametoindex(brname);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005964 add_ifidx(drv, br_ifindex, drv->ifindex);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005965 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005966 bss->br_ifindex = br_ifindex;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005967
5968 if (linux_br_get(in_br, ifname) == 0) {
5969 if (os_strcmp(in_br, brname) == 0)
5970 return 0; /* already in the bridge */
5971
5972 wpa_printf(MSG_DEBUG, "nl80211: Removing interface %s from "
5973 "bridge %s", ifname, in_br);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005974 if (linux_br_del_if(drv->global->ioctl_sock, in_br, ifname) <
5975 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005976 wpa_printf(MSG_ERROR, "nl80211: Failed to "
5977 "remove interface %s from bridge "
5978 "%s: %s",
5979 ifname, brname, strerror(errno));
5980 return -1;
5981 }
5982 }
5983
5984 wpa_printf(MSG_DEBUG, "nl80211: Adding interface %s into bridge %s",
5985 ifname, brname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005986 if (linux_br_add_if(drv->global->ioctl_sock, brname, ifname) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005987 wpa_printf(MSG_ERROR, "nl80211: Failed to add interface %s "
5988 "into bridge %s: %s",
5989 ifname, brname, strerror(errno));
5990 return -1;
5991 }
5992 bss->added_if_into_bridge = 1;
5993
5994 return 0;
5995}
5996
5997
5998static void *i802_init(struct hostapd_data *hapd,
5999 struct wpa_init_params *params)
6000{
6001 struct wpa_driver_nl80211_data *drv;
6002 struct i802_bss *bss;
6003 size_t i;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006004 char master_ifname[IFNAMSIZ];
6005 int ifindex, br_ifindex = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006006 int br_added = 0;
6007
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08006008 bss = wpa_driver_nl80211_drv_init(hapd, params->ifname,
6009 params->global_priv, 1,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006010 params->bssid, params->driver_params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006011 if (bss == NULL)
6012 return NULL;
6013
6014 drv = bss->drv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006015
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006016 if (linux_br_get(master_ifname, params->ifname) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006017 wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in bridge %s",
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006018 params->ifname, master_ifname);
6019 br_ifindex = if_nametoindex(master_ifname);
6020 os_strlcpy(bss->brname, master_ifname, IFNAMSIZ);
6021 } else if ((params->num_bridge == 0 || !params->bridge[0]) &&
6022 linux_master_get(master_ifname, params->ifname) == 0) {
6023 wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in master %s",
6024 params->ifname, master_ifname);
6025 /* start listening for EAPOL on the master interface */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006026 add_ifidx(drv, if_nametoindex(master_ifname), drv->ifindex);
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006027
6028 /* check if master itself is under bridge */
6029 if (linux_br_get(master_ifname, master_ifname) == 0) {
6030 wpa_printf(MSG_DEBUG, "nl80211: which is in bridge %s",
6031 master_ifname);
6032 br_ifindex = if_nametoindex(master_ifname);
6033 os_strlcpy(bss->brname, master_ifname, IFNAMSIZ);
6034 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006035 } else {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006036 master_ifname[0] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006037 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006038
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006039 bss->br_ifindex = br_ifindex;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006040
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006041 for (i = 0; i < params->num_bridge; i++) {
6042 if (params->bridge[i]) {
6043 ifindex = if_nametoindex(params->bridge[i]);
6044 if (ifindex)
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006045 add_ifidx(drv, ifindex, drv->ifindex);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006046 if (ifindex == br_ifindex)
6047 br_added = 1;
6048 }
6049 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006050
6051 /* start listening for EAPOL on the default AP interface */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006052 add_ifidx(drv, drv->ifindex, IFIDX_ANY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006053
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006054 if (params->num_bridge && params->bridge[0]) {
6055 if (i802_check_bridge(drv, bss, params->bridge[0],
6056 params->ifname) < 0)
6057 goto failed;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006058 if (os_strcmp(params->bridge[0], master_ifname) != 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006059 br_added = 1;
6060 }
6061
6062 if (!br_added && br_ifindex &&
6063 (params->num_bridge == 0 || !params->bridge[0]))
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006064 add_ifidx(drv, br_ifindex, drv->ifindex);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006065
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006066#ifdef CONFIG_LIBNL3_ROUTE
6067 if (bss->added_if_into_bridge) {
6068 drv->rtnl_sk = nl_socket_alloc();
6069 if (drv->rtnl_sk == NULL) {
6070 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate nl_sock");
6071 goto failed;
6072 }
6073
6074 if (nl_connect(drv->rtnl_sk, NETLINK_ROUTE)) {
6075 wpa_printf(MSG_ERROR, "nl80211: Failed to connect nl_sock to NETLINK_ROUTE: %s",
6076 strerror(errno));
6077 goto failed;
6078 }
6079 }
6080#endif /* CONFIG_LIBNL3_ROUTE */
6081
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006082 drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
6083 if (drv->eapol_sock < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006084 wpa_printf(MSG_ERROR, "nl80211: socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE) failed: %s",
6085 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006086 goto failed;
6087 }
6088
6089 if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
6090 {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006091 wpa_printf(MSG_INFO, "nl80211: Could not register read socket for eapol");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006092 goto failed;
6093 }
6094
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006095 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
6096 params->own_addr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006097 goto failed;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006098 os_memcpy(drv->perm_addr, params->own_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006099
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006100 memcpy(bss->addr, params->own_addr, ETH_ALEN);
6101
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006102 return bss;
6103
6104failed:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006105 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006106 return NULL;
6107}
6108
6109
6110static void i802_deinit(void *priv)
6111{
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006112 struct i802_bss *bss = priv;
6113 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006114}
6115
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006116
6117static enum nl80211_iftype wpa_driver_nl80211_if_type(
6118 enum wpa_driver_if_type type)
6119{
6120 switch (type) {
6121 case WPA_IF_STATION:
6122 return NL80211_IFTYPE_STATION;
6123 case WPA_IF_P2P_CLIENT:
6124 case WPA_IF_P2P_GROUP:
6125 return NL80211_IFTYPE_P2P_CLIENT;
6126 case WPA_IF_AP_VLAN:
6127 return NL80211_IFTYPE_AP_VLAN;
6128 case WPA_IF_AP_BSS:
6129 return NL80211_IFTYPE_AP;
6130 case WPA_IF_P2P_GO:
6131 return NL80211_IFTYPE_P2P_GO;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006132 case WPA_IF_P2P_DEVICE:
6133 return NL80211_IFTYPE_P2P_DEVICE;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006134 case WPA_IF_MESH:
6135 return NL80211_IFTYPE_MESH_POINT;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006136 default:
6137 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006138 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006139}
6140
6141
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006142static int nl80211_addr_in_use(struct nl80211_global *global, const u8 *addr)
6143{
6144 struct wpa_driver_nl80211_data *drv;
6145 dl_list_for_each(drv, &global->interfaces,
6146 struct wpa_driver_nl80211_data, list) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006147 if (os_memcmp(addr, drv->first_bss->addr, ETH_ALEN) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006148 return 1;
6149 }
6150 return 0;
6151}
6152
6153
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006154static int nl80211_vif_addr(struct wpa_driver_nl80211_data *drv, u8 *new_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006155{
6156 unsigned int idx;
6157
6158 if (!drv->global)
6159 return -1;
6160
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006161 os_memcpy(new_addr, drv->first_bss->addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006162 for (idx = 0; idx < 64; idx++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006163 new_addr[0] = drv->first_bss->addr[0] | 0x02;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006164 new_addr[0] ^= idx << 2;
6165 if (!nl80211_addr_in_use(drv->global, new_addr))
6166 break;
6167 }
6168 if (idx == 64)
6169 return -1;
6170
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006171 wpa_printf(MSG_DEBUG, "nl80211: Assigned new virtual interface address "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006172 MACSTR, MAC2STR(new_addr));
6173
6174 return 0;
6175}
6176
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006177
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006178struct wdev_info {
6179 u64 wdev_id;
6180 int wdev_id_set;
6181 u8 macaddr[ETH_ALEN];
6182};
6183
6184static int nl80211_wdev_handler(struct nl_msg *msg, void *arg)
6185{
6186 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6187 struct nlattr *tb[NL80211_ATTR_MAX + 1];
6188 struct wdev_info *wi = arg;
6189
6190 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6191 genlmsg_attrlen(gnlh, 0), NULL);
6192 if (tb[NL80211_ATTR_WDEV]) {
6193 wi->wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
6194 wi->wdev_id_set = 1;
6195 }
6196
6197 if (tb[NL80211_ATTR_MAC])
6198 os_memcpy(wi->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
6199 ETH_ALEN);
6200
6201 return NL_SKIP;
6202}
6203
6204
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006205static int wpa_driver_nl80211_if_add(void *priv, enum wpa_driver_if_type type,
6206 const char *ifname, const u8 *addr,
6207 void *bss_ctx, void **drv_priv,
6208 char *force_ifname, u8 *if_addr,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006209 const char *bridge, int use_existing,
6210 int setup_ap)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006211{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006212 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006213 struct i802_bss *bss = priv;
6214 struct wpa_driver_nl80211_data *drv = bss->drv;
6215 int ifidx;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006216 int added = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006217
6218 if (addr)
6219 os_memcpy(if_addr, addr, ETH_ALEN);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006220 nlmode = wpa_driver_nl80211_if_type(type);
6221 if (nlmode == NL80211_IFTYPE_P2P_DEVICE) {
6222 struct wdev_info p2pdev_info;
6223
6224 os_memset(&p2pdev_info, 0, sizeof(p2pdev_info));
6225 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
6226 0, nl80211_wdev_handler,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006227 &p2pdev_info, use_existing);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006228 if (!p2pdev_info.wdev_id_set || ifidx != 0) {
6229 wpa_printf(MSG_ERROR, "nl80211: Failed to create a P2P Device interface %s",
6230 ifname);
6231 return -1;
6232 }
6233
6234 drv->global->if_add_wdevid = p2pdev_info.wdev_id;
6235 drv->global->if_add_wdevid_set = p2pdev_info.wdev_id_set;
6236 if (!is_zero_ether_addr(p2pdev_info.macaddr))
6237 os_memcpy(if_addr, p2pdev_info.macaddr, ETH_ALEN);
6238 wpa_printf(MSG_DEBUG, "nl80211: New P2P Device interface %s (0x%llx) created",
6239 ifname,
6240 (long long unsigned int) p2pdev_info.wdev_id);
6241 } else {
6242 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006243 0, NULL, NULL, use_existing);
6244 if (use_existing && ifidx == -ENFILE) {
6245 added = 0;
6246 ifidx = if_nametoindex(ifname);
6247 } else if (ifidx < 0) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006248 return -1;
6249 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006250 }
6251
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006252 if (!addr) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07006253 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006254 os_memcpy(if_addr, bss->addr, ETH_ALEN);
6255 else if (linux_get_ifhwaddr(drv->global->ioctl_sock,
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07006256 ifname, if_addr) < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006257 if (added)
6258 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006259 return -1;
6260 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006261 }
6262
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006263 if (!addr &&
6264 (type == WPA_IF_P2P_CLIENT || type == WPA_IF_P2P_GROUP ||
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07006265 type == WPA_IF_P2P_GO || type == WPA_IF_MESH ||
6266 type == WPA_IF_STATION)) {
6267 /* Enforce unique address */
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006268 u8 new_addr[ETH_ALEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006269
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006270 if (linux_get_ifhwaddr(drv->global->ioctl_sock, ifname,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006271 new_addr) < 0) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07006272 if (added)
6273 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006274 return -1;
6275 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006276 if (nl80211_addr_in_use(drv->global, new_addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006277 wpa_printf(MSG_DEBUG, "nl80211: Allocate new address "
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07006278 "for interface %s type %d", ifname, type);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006279 if (nl80211_vif_addr(drv, new_addr) < 0) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07006280 if (added)
6281 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006282 return -1;
6283 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006284 if (linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006285 new_addr) < 0) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07006286 if (added)
6287 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006288 return -1;
6289 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006290 }
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07006291 os_memcpy(if_addr, new_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006292 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006293
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006294 if (type == WPA_IF_AP_BSS && setup_ap) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006295 struct i802_bss *new_bss = os_zalloc(sizeof(*new_bss));
6296 if (new_bss == NULL) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006297 if (added)
6298 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006299 return -1;
6300 }
6301
6302 if (bridge &&
6303 i802_check_bridge(drv, new_bss, bridge, ifname) < 0) {
6304 wpa_printf(MSG_ERROR, "nl80211: Failed to add the new "
6305 "interface %s to a bridge %s",
6306 ifname, bridge);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006307 if (added)
6308 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006309 os_free(new_bss);
6310 return -1;
6311 }
6312
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006313 if (linux_set_iface_flags(drv->global->ioctl_sock, ifname, 1))
6314 {
Dmitry Shmidt71757432014-06-02 13:50:35 -07006315 if (added)
6316 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006317 os_free(new_bss);
6318 return -1;
6319 }
6320 os_strlcpy(new_bss->ifname, ifname, IFNAMSIZ);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006321 os_memcpy(new_bss->addr, if_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006322 new_bss->ifindex = ifidx;
6323 new_bss->drv = drv;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006324 new_bss->next = drv->first_bss->next;
6325 new_bss->freq = drv->first_bss->freq;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006326 new_bss->ctx = bss_ctx;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006327 new_bss->added_if = added;
6328 drv->first_bss->next = new_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006329 if (drv_priv)
6330 *drv_priv = new_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006331 nl80211_init_bss(new_bss);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006332
6333 /* Subscribe management frames for this WPA_IF_AP_BSS */
6334 if (nl80211_setup_ap(new_bss))
6335 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006336 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006337
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006338 if (drv->global)
6339 drv->global->if_add_ifindex = ifidx;
6340
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006341 /*
6342 * Some virtual interfaces need to process EAPOL packets and events on
6343 * the parent interface. This is used mainly with hostapd.
6344 */
6345 if (ifidx > 0 &&
6346 (drv->hostapd ||
6347 nlmode == NL80211_IFTYPE_AP_VLAN ||
6348 nlmode == NL80211_IFTYPE_WDS ||
6349 nlmode == NL80211_IFTYPE_MONITOR))
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006350 add_ifidx(drv, ifidx, IFIDX_ANY);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006351
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006352 return 0;
6353}
6354
6355
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006356static int wpa_driver_nl80211_if_remove(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006357 enum wpa_driver_if_type type,
6358 const char *ifname)
6359{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006360 struct wpa_driver_nl80211_data *drv = bss->drv;
6361 int ifindex = if_nametoindex(ifname);
6362
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006363 wpa_printf(MSG_DEBUG, "nl80211: %s(type=%d ifname=%s) ifindex=%d added_if=%d",
6364 __func__, type, ifname, ifindex, bss->added_if);
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08006365 if (ifindex > 0 && (bss->added_if || bss->ifindex != ifindex))
Dmitry Shmidt051af732013-10-22 13:52:46 -07006366 nl80211_remove_iface(drv, ifindex);
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07006367 else if (ifindex > 0 && !bss->added_if) {
6368 struct wpa_driver_nl80211_data *drv2;
6369 dl_list_for_each(drv2, &drv->global->interfaces,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006370 struct wpa_driver_nl80211_data, list) {
6371 del_ifidx(drv2, ifindex, IFIDX_ANY);
6372 del_ifidx(drv2, IFIDX_ANY, ifindex);
6373 }
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07006374 }
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006375
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006376 if (type != WPA_IF_AP_BSS)
6377 return 0;
6378
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006379 if (bss->added_if_into_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006380 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
6381 bss->ifname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006382 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
6383 "interface %s from bridge %s: %s",
6384 bss->ifname, bss->brname, strerror(errno));
6385 }
6386 if (bss->added_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006387 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006388 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
6389 "bridge %s: %s",
6390 bss->brname, strerror(errno));
6391 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006392
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006393 if (bss != drv->first_bss) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006394 struct i802_bss *tbss;
6395
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006396 wpa_printf(MSG_DEBUG, "nl80211: Not the first BSS - remove it");
6397 for (tbss = drv->first_bss; tbss; tbss = tbss->next) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006398 if (tbss->next == bss) {
6399 tbss->next = bss->next;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006400 /* Unsubscribe management frames */
6401 nl80211_teardown_ap(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006402 nl80211_destroy_bss(bss);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006403 if (!bss->added_if)
6404 i802_set_iface_flags(bss, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006405 os_free(bss);
6406 bss = NULL;
6407 break;
6408 }
6409 }
6410 if (bss)
6411 wpa_printf(MSG_INFO, "nl80211: %s - could not find "
6412 "BSS %p in the list", __func__, bss);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006413 } else {
6414 wpa_printf(MSG_DEBUG, "nl80211: First BSS - reassign context");
6415 nl80211_teardown_ap(bss);
6416 if (!bss->added_if && !drv->first_bss->next)
6417 wpa_driver_nl80211_del_beacon(drv);
6418 nl80211_destroy_bss(bss);
6419 if (!bss->added_if)
6420 i802_set_iface_flags(bss, 0);
6421 if (drv->first_bss->next) {
6422 drv->first_bss = drv->first_bss->next;
6423 drv->ctx = drv->first_bss->ctx;
6424 os_free(bss);
6425 } else {
6426 wpa_printf(MSG_DEBUG, "nl80211: No second BSS to reassign context to");
6427 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006428 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006429
6430 return 0;
6431}
6432
6433
6434static int cookie_handler(struct nl_msg *msg, void *arg)
6435{
6436 struct nlattr *tb[NL80211_ATTR_MAX + 1];
6437 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6438 u64 *cookie = arg;
6439 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6440 genlmsg_attrlen(gnlh, 0), NULL);
6441 if (tb[NL80211_ATTR_COOKIE])
6442 *cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
6443 return NL_SKIP;
6444}
6445
6446
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006447static int nl80211_send_frame_cmd(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006448 unsigned int freq, unsigned int wait,
6449 const u8 *buf, size_t buf_len,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006450 u64 *cookie_out, int no_cck, int no_ack,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006451 int offchanok, const u16 *csa_offs,
6452 size_t csa_offs_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006453{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006454 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006455 struct nl_msg *msg;
6456 u64 cookie;
6457 int ret = -1;
6458
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006459 wpa_printf(MSG_MSGDUMP, "nl80211: CMD_FRAME freq=%u wait=%u no_cck=%d "
Dmitry Shmidt04949592012-07-19 12:16:46 -07006460 "no_ack=%d offchanok=%d",
6461 freq, wait, no_cck, no_ack, offchanok);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006462 wpa_hexdump(MSG_MSGDUMP, "CMD_FRAME", buf, buf_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006463
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006464 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_FRAME)) ||
6465 (freq && nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
6466 (wait && nla_put_u32(msg, NL80211_ATTR_DURATION, wait)) ||
6467 (offchanok && ((drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
6468 drv->test_use_roc_tx) &&
6469 nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK)) ||
6470 (no_cck && nla_put_flag(msg, NL80211_ATTR_TX_NO_CCK_RATE)) ||
6471 (no_ack && nla_put_flag(msg, NL80211_ATTR_DONT_WAIT_FOR_ACK)) ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006472 (csa_offs && nla_put(msg, NL80211_ATTR_CSA_C_OFFSETS_TX,
6473 csa_offs_len * sizeof(u16), csa_offs)) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006474 nla_put(msg, NL80211_ATTR_FRAME, buf_len, buf))
6475 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006476
6477 cookie = 0;
6478 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
6479 msg = NULL;
6480 if (ret) {
6481 wpa_printf(MSG_DEBUG, "nl80211: Frame command failed: ret=%d "
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07006482 "(%s) (freq=%u wait=%u)", ret, strerror(-ret),
6483 freq, wait);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006484 } else {
6485 wpa_printf(MSG_MSGDUMP, "nl80211: Frame TX command accepted%s; "
6486 "cookie 0x%llx", no_ack ? " (no ACK)" : "",
6487 (long long unsigned int) cookie);
6488
6489 if (cookie_out)
6490 *cookie_out = no_ack ? (u64) -1 : cookie;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006491
6492 if (drv->num_send_action_cookies == MAX_SEND_ACTION_COOKIES) {
6493 wpa_printf(MSG_DEBUG,
6494 "nl80211: Drop oldest pending send action cookie 0x%llx",
6495 (long long unsigned int)
6496 drv->send_action_cookies[0]);
6497 os_memmove(&drv->send_action_cookies[0],
6498 &drv->send_action_cookies[1],
6499 (MAX_SEND_ACTION_COOKIES - 1) *
6500 sizeof(u64));
6501 drv->num_send_action_cookies--;
6502 }
6503 drv->send_action_cookies[drv->num_send_action_cookies] = cookie;
6504 drv->num_send_action_cookies++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006505 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006506
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006507fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006508 nlmsg_free(msg);
6509 return ret;
6510}
6511
6512
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006513static int wpa_driver_nl80211_send_action(struct i802_bss *bss,
6514 unsigned int freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006515 unsigned int wait_time,
6516 const u8 *dst, const u8 *src,
6517 const u8 *bssid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006518 const u8 *data, size_t data_len,
6519 int no_cck)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006520{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006521 struct wpa_driver_nl80211_data *drv = bss->drv;
6522 int ret = -1;
6523 u8 *buf;
6524 struct ieee80211_hdr *hdr;
6525
6526 wpa_printf(MSG_DEBUG, "nl80211: Send Action frame (ifindex=%d, "
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006527 "freq=%u MHz wait=%d ms no_cck=%d)",
6528 drv->ifindex, freq, wait_time, no_cck);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006529
6530 buf = os_zalloc(24 + data_len);
6531 if (buf == NULL)
6532 return ret;
6533 os_memcpy(buf + 24, data, data_len);
6534 hdr = (struct ieee80211_hdr *) buf;
6535 hdr->frame_control =
6536 IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_ACTION);
6537 os_memcpy(hdr->addr1, dst, ETH_ALEN);
6538 os_memcpy(hdr->addr2, src, ETH_ALEN);
6539 os_memcpy(hdr->addr3, bssid, ETH_ALEN);
6540
Dmitry Shmidt56052862013-10-04 10:23:25 -07006541 if (is_ap_interface(drv->nlmode) &&
6542 (!(drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
6543 (int) freq == bss->freq || drv->device_ap_sme ||
6544 !drv->use_monitor))
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006545 ret = wpa_driver_nl80211_send_mlme(bss, buf, 24 + data_len,
6546 0, freq, no_cck, 1,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006547 wait_time, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006548 else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006549 ret = nl80211_send_frame_cmd(bss, freq, wait_time, buf,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006550 24 + data_len,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006551 &drv->send_action_cookie,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006552 no_cck, 0, 1, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006553
6554 os_free(buf);
6555 return ret;
6556}
6557
6558
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006559static void nl80211_frame_wait_cancel(struct i802_bss *bss, u64 cookie)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006560{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006561 struct wpa_driver_nl80211_data *drv = bss->drv;
6562 struct nl_msg *msg;
6563 int ret;
6564
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08006565 wpa_printf(MSG_DEBUG, "nl80211: Cancel TX frame wait: cookie=0x%llx",
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006566 (long long unsigned int) cookie);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006567 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_FRAME_WAIT_CANCEL)) ||
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006568 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006569 nlmsg_free(msg);
6570 return;
6571 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006572
6573 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006574 if (ret)
6575 wpa_printf(MSG_DEBUG, "nl80211: wait cancel failed: ret=%d "
6576 "(%s)", ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006577}
6578
6579
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006580static void wpa_driver_nl80211_send_action_cancel_wait(void *priv)
6581{
6582 struct i802_bss *bss = priv;
6583 struct wpa_driver_nl80211_data *drv = bss->drv;
6584 unsigned int i;
6585 u64 cookie;
6586
6587 /* Cancel the last pending TX cookie */
6588 nl80211_frame_wait_cancel(bss, drv->send_action_cookie);
6589
6590 /*
6591 * Cancel the other pending TX cookies, if any. This is needed since
6592 * the driver may keep a list of all pending offchannel TX operations
6593 * and free up the radio only once they have expired or cancelled.
6594 */
6595 for (i = drv->num_send_action_cookies; i > 0; i--) {
6596 cookie = drv->send_action_cookies[i - 1];
6597 if (cookie != drv->send_action_cookie)
6598 nl80211_frame_wait_cancel(bss, cookie);
6599 }
6600 drv->num_send_action_cookies = 0;
6601}
6602
6603
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006604static int wpa_driver_nl80211_remain_on_channel(void *priv, unsigned int freq,
6605 unsigned int duration)
6606{
6607 struct i802_bss *bss = priv;
6608 struct wpa_driver_nl80211_data *drv = bss->drv;
6609 struct nl_msg *msg;
6610 int ret;
6611 u64 cookie;
6612
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006613 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_REMAIN_ON_CHANNEL)) ||
6614 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
6615 nla_put_u32(msg, NL80211_ATTR_DURATION, duration)) {
6616 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006617 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006618 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006619
6620 cookie = 0;
6621 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
6622 if (ret == 0) {
6623 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel cookie "
6624 "0x%llx for freq=%u MHz duration=%u",
6625 (long long unsigned int) cookie, freq, duration);
6626 drv->remain_on_chan_cookie = cookie;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006627 drv->pending_remain_on_chan = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006628 return 0;
6629 }
6630 wpa_printf(MSG_DEBUG, "nl80211: Failed to request remain-on-channel "
6631 "(freq=%d duration=%u): %d (%s)",
6632 freq, duration, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006633 return -1;
6634}
6635
6636
6637static int wpa_driver_nl80211_cancel_remain_on_channel(void *priv)
6638{
6639 struct i802_bss *bss = priv;
6640 struct wpa_driver_nl80211_data *drv = bss->drv;
6641 struct nl_msg *msg;
6642 int ret;
6643
6644 if (!drv->pending_remain_on_chan) {
6645 wpa_printf(MSG_DEBUG, "nl80211: No pending remain-on-channel "
6646 "to cancel");
6647 return -1;
6648 }
6649
6650 wpa_printf(MSG_DEBUG, "nl80211: Cancel remain-on-channel with cookie "
6651 "0x%llx",
6652 (long long unsigned int) drv->remain_on_chan_cookie);
6653
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006654 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL);
6655 if (!msg ||
6656 nla_put_u64(msg, NL80211_ATTR_COOKIE, drv->remain_on_chan_cookie)) {
6657 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006658 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006659 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006660
6661 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6662 if (ret == 0)
6663 return 0;
6664 wpa_printf(MSG_DEBUG, "nl80211: Failed to cancel remain-on-channel: "
6665 "%d (%s)", ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006666 return -1;
6667}
6668
6669
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006670static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss, int report)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006671{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006672 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07006673
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006674 if (!report) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006675 if (bss->nl_preq && drv->device_ap_sme &&
Dmitry Shmidt03658832014-08-13 11:03:49 -07006676 is_ap_interface(drv->nlmode) && !bss->in_deinit &&
6677 !bss->static_ap) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006678 /*
6679 * Do not disable Probe Request reporting that was
6680 * enabled in nl80211_setup_ap().
6681 */
6682 wpa_printf(MSG_DEBUG, "nl80211: Skip disabling of "
6683 "Probe Request reporting nl_preq=%p while "
6684 "in AP mode", bss->nl_preq);
6685 } else if (bss->nl_preq) {
6686 wpa_printf(MSG_DEBUG, "nl80211: Disable Probe Request "
6687 "reporting nl_preq=%p", bss->nl_preq);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006688 nl80211_destroy_eloop_handle(&bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006689 }
6690 return 0;
6691 }
6692
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006693 if (bss->nl_preq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006694 wpa_printf(MSG_DEBUG, "nl80211: Probe Request reporting "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006695 "already on! nl_preq=%p", bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006696 return 0;
6697 }
6698
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006699 bss->nl_preq = nl_create_handle(drv->global->nl_cb, "preq");
6700 if (bss->nl_preq == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006701 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006702 wpa_printf(MSG_DEBUG, "nl80211: Enable Probe Request "
6703 "reporting nl_preq=%p", bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006704
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006705 if (nl80211_register_frame(bss, bss->nl_preq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006706 (WLAN_FC_TYPE_MGMT << 2) |
6707 (WLAN_FC_STYPE_PROBE_REQ << 4),
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006708 NULL, 0) < 0)
6709 goto out_err;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07006710
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006711 nl80211_register_eloop_read(&bss->nl_preq,
6712 wpa_driver_nl80211_event_receive,
6713 bss->nl_cb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006714
6715 return 0;
6716
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006717 out_err:
6718 nl_destroy_handles(&bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006719 return -1;
6720}
6721
6722
6723static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
6724 int ifindex, int disabled)
6725{
6726 struct nl_msg *msg;
6727 struct nlattr *bands, *band;
6728 int ret;
6729
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006730 wpa_printf(MSG_DEBUG,
6731 "nl80211: NL80211_CMD_SET_TX_BITRATE_MASK (ifindex=%d %s)",
6732 ifindex, disabled ? "NL80211_TXRATE_LEGACY=OFDM-only" :
6733 "no NL80211_TXRATE_LEGACY constraint");
6734
6735 msg = nl80211_ifindex_msg(drv, ifindex, 0,
6736 NL80211_CMD_SET_TX_BITRATE_MASK);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006737 if (!msg)
6738 return -1;
6739
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006740 bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES);
6741 if (!bands)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006742 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006743
6744 /*
6745 * Disable 2 GHz rates 1, 2, 5.5, 11 Mbps by masking out everything
6746 * else apart from 6, 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS
6747 * rates. All 5 GHz rates are left enabled.
6748 */
6749 band = nla_nest_start(msg, NL80211_BAND_2GHZ);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006750 if (!band ||
6751 (disabled && nla_put(msg, NL80211_TXRATE_LEGACY, 8,
6752 "\x0c\x12\x18\x24\x30\x48\x60\x6c")))
6753 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006754 nla_nest_end(msg, band);
6755
6756 nla_nest_end(msg, bands);
6757
6758 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006759 if (ret) {
6760 wpa_printf(MSG_DEBUG, "nl80211: Set TX rates failed: ret=%d "
6761 "(%s)", ret, strerror(-ret));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006762 } else
6763 drv->disabled_11b_rates = disabled;
6764
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006765 return ret;
6766
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006767fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006768 nlmsg_free(msg);
6769 return -1;
6770}
6771
6772
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006773static int wpa_driver_nl80211_deinit_ap(void *priv)
6774{
6775 struct i802_bss *bss = priv;
6776 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006777 if (!is_ap_interface(drv->nlmode))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006778 return -1;
6779 wpa_driver_nl80211_del_beacon(drv);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006780 bss->beacon_set = 0;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006781
6782 /*
6783 * If the P2P GO interface was dynamically added, then it is
6784 * possible that the interface change to station is not possible.
6785 */
6786 if (drv->nlmode == NL80211_IFTYPE_P2P_GO && bss->if_dynamic)
6787 return 0;
6788
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006789 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006790}
6791
6792
Dmitry Shmidtea69e842013-05-13 14:52:28 -07006793static int wpa_driver_nl80211_stop_ap(void *priv)
6794{
6795 struct i802_bss *bss = priv;
6796 struct wpa_driver_nl80211_data *drv = bss->drv;
6797 if (!is_ap_interface(drv->nlmode))
6798 return -1;
6799 wpa_driver_nl80211_del_beacon(drv);
6800 bss->beacon_set = 0;
6801 return 0;
6802}
6803
6804
Dmitry Shmidt04949592012-07-19 12:16:46 -07006805static int wpa_driver_nl80211_deinit_p2p_cli(void *priv)
6806{
6807 struct i802_bss *bss = priv;
6808 struct wpa_driver_nl80211_data *drv = bss->drv;
6809 if (drv->nlmode != NL80211_IFTYPE_P2P_CLIENT)
6810 return -1;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006811
6812 /*
6813 * If the P2P Client interface was dynamically added, then it is
6814 * possible that the interface change to station is not possible.
6815 */
6816 if (bss->if_dynamic)
6817 return 0;
6818
Dmitry Shmidt04949592012-07-19 12:16:46 -07006819 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
6820}
6821
6822
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006823static void wpa_driver_nl80211_resume(void *priv)
6824{
6825 struct i802_bss *bss = priv;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006826 enum nl80211_iftype nlmode = nl80211_get_ifmode(bss);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006827
6828 if (i802_set_iface_flags(bss, 1))
6829 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface up on resume event");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006830
6831 if (is_p2p_net_interface(nlmode))
6832 nl80211_disable_11b_rates(bss->drv, bss->drv->ifindex, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006833}
6834
6835
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006836static int nl80211_signal_monitor(void *priv, int threshold, int hysteresis)
6837{
6838 struct i802_bss *bss = priv;
6839 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006840 struct nl_msg *msg;
6841 struct nlattr *cqm;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006842
6843 wpa_printf(MSG_DEBUG, "nl80211: Signal monitor threshold=%d "
6844 "hysteresis=%d", threshold, hysteresis);
6845
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006846 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_CQM)) ||
6847 !(cqm = nla_nest_start(msg, NL80211_ATTR_CQM)) ||
6848 nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THOLD, threshold) ||
6849 nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_HYST, hysteresis)) {
6850 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006851 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006852 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006853 nla_nest_end(msg, cqm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006854
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006855 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006856}
6857
6858
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006859static int get_channel_width(struct nl_msg *msg, void *arg)
6860{
6861 struct nlattr *tb[NL80211_ATTR_MAX + 1];
6862 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6863 struct wpa_signal_info *sig_change = arg;
6864
6865 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6866 genlmsg_attrlen(gnlh, 0), NULL);
6867
6868 sig_change->center_frq1 = -1;
6869 sig_change->center_frq2 = -1;
6870 sig_change->chanwidth = CHAN_WIDTH_UNKNOWN;
6871
6872 if (tb[NL80211_ATTR_CHANNEL_WIDTH]) {
6873 sig_change->chanwidth = convert2width(
6874 nla_get_u32(tb[NL80211_ATTR_CHANNEL_WIDTH]));
6875 if (tb[NL80211_ATTR_CENTER_FREQ1])
6876 sig_change->center_frq1 =
6877 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]);
6878 if (tb[NL80211_ATTR_CENTER_FREQ2])
6879 sig_change->center_frq2 =
6880 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]);
6881 }
6882
6883 return NL_SKIP;
6884}
6885
6886
6887static int nl80211_get_channel_width(struct wpa_driver_nl80211_data *drv,
6888 struct wpa_signal_info *sig)
6889{
6890 struct nl_msg *msg;
6891
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006892 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_GET_INTERFACE);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006893 return send_and_recv_msgs(drv, msg, get_channel_width, sig);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006894}
6895
6896
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006897static int nl80211_signal_poll(void *priv, struct wpa_signal_info *si)
6898{
6899 struct i802_bss *bss = priv;
6900 struct wpa_driver_nl80211_data *drv = bss->drv;
6901 int res;
6902
6903 os_memset(si, 0, sizeof(*si));
6904 res = nl80211_get_link_signal(drv, si);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006905 if (res) {
6906 if (drv->nlmode != NL80211_IFTYPE_ADHOC &&
6907 drv->nlmode != NL80211_IFTYPE_MESH_POINT)
6908 return res;
6909 si->current_signal = 0;
6910 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006911
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006912 res = nl80211_get_channel_width(drv, si);
6913 if (res != 0)
6914 return res;
6915
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006916 return nl80211_get_link_noise(drv, si);
6917}
6918
6919
6920static int nl80211_send_frame(void *priv, const u8 *data, size_t data_len,
6921 int encrypt)
6922{
6923 struct i802_bss *bss = priv;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006924 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt, 0,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006925 0, 0, 0, 0, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006926}
6927
6928
6929static int nl80211_set_param(void *priv, const char *param)
6930{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006931 if (param == NULL)
6932 return 0;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08006933 wpa_printf(MSG_DEBUG, "nl80211: driver param='%s'", param);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006934
6935#ifdef CONFIG_P2P
6936 if (os_strstr(param, "use_p2p_group_interface=1")) {
6937 struct i802_bss *bss = priv;
6938 struct wpa_driver_nl80211_data *drv = bss->drv;
6939
6940 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
6941 "interface");
6942 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
6943 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
6944 }
6945#endif /* CONFIG_P2P */
6946
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006947 if (os_strstr(param, "use_monitor=1")) {
6948 struct i802_bss *bss = priv;
6949 struct wpa_driver_nl80211_data *drv = bss->drv;
6950 drv->use_monitor = 1;
6951 }
6952
6953 if (os_strstr(param, "force_connect_cmd=1")) {
6954 struct i802_bss *bss = priv;
6955 struct wpa_driver_nl80211_data *drv = bss->drv;
6956 drv->capa.flags &= ~WPA_DRIVER_FLAGS_SME;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006957 drv->force_connect_cmd = 1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006958 }
6959
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08006960 if (os_strstr(param, "no_offchannel_tx=1")) {
6961 struct i802_bss *bss = priv;
6962 struct wpa_driver_nl80211_data *drv = bss->drv;
6963 drv->capa.flags &= ~WPA_DRIVER_FLAGS_OFFCHANNEL_TX;
6964 drv->test_use_roc_tx = 1;
6965 }
6966
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006967 return 0;
6968}
6969
6970
Dmitry Shmidte4663042016-04-04 10:07:49 -07006971static void * nl80211_global_init(void *ctx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006972{
6973 struct nl80211_global *global;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006974 struct netlink_config *cfg;
6975
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006976 global = os_zalloc(sizeof(*global));
6977 if (global == NULL)
6978 return NULL;
Dmitry Shmidte4663042016-04-04 10:07:49 -07006979 global->ctx = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006980 global->ioctl_sock = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006981 dl_list_init(&global->interfaces);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006982 global->if_add_ifindex = -1;
6983
6984 cfg = os_zalloc(sizeof(*cfg));
6985 if (cfg == NULL)
6986 goto err;
6987
6988 cfg->ctx = global;
6989 cfg->newlink_cb = wpa_driver_nl80211_event_rtm_newlink;
6990 cfg->dellink_cb = wpa_driver_nl80211_event_rtm_dellink;
6991 global->netlink = netlink_init(cfg);
6992 if (global->netlink == NULL) {
6993 os_free(cfg);
6994 goto err;
6995 }
6996
6997 if (wpa_driver_nl80211_init_nl_global(global) < 0)
6998 goto err;
6999
7000 global->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
7001 if (global->ioctl_sock < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007002 wpa_printf(MSG_ERROR, "nl80211: socket(PF_INET,SOCK_DGRAM) failed: %s",
7003 strerror(errno));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007004 goto err;
7005 }
7006
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007007 return global;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007008
7009err:
7010 nl80211_global_deinit(global);
7011 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007012}
7013
7014
7015static void nl80211_global_deinit(void *priv)
7016{
7017 struct nl80211_global *global = priv;
7018 if (global == NULL)
7019 return;
7020 if (!dl_list_empty(&global->interfaces)) {
7021 wpa_printf(MSG_ERROR, "nl80211: %u interface(s) remain at "
7022 "nl80211_global_deinit",
7023 dl_list_len(&global->interfaces));
7024 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007025
7026 if (global->netlink)
7027 netlink_deinit(global->netlink);
7028
7029 nl_destroy_handles(&global->nl);
7030
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007031 if (global->nl_event)
7032 nl80211_destroy_eloop_handle(&global->nl_event);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007033
7034 nl_cb_put(global->nl_cb);
7035
7036 if (global->ioctl_sock >= 0)
7037 close(global->ioctl_sock);
7038
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007039 os_free(global);
7040}
7041
7042
7043static const char * nl80211_get_radio_name(void *priv)
7044{
7045 struct i802_bss *bss = priv;
7046 struct wpa_driver_nl80211_data *drv = bss->drv;
7047 return drv->phyname;
7048}
7049
7050
Jouni Malinen75ecf522011-06-27 15:19:46 -07007051static int nl80211_pmkid(struct i802_bss *bss, int cmd, const u8 *bssid,
7052 const u8 *pmkid)
7053{
7054 struct nl_msg *msg;
7055
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007056 if (!(msg = nl80211_bss_msg(bss, 0, cmd)) ||
7057 (pmkid && nla_put(msg, NL80211_ATTR_PMKID, 16, pmkid)) ||
7058 (bssid && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))) {
7059 nlmsg_free(msg);
7060 return -ENOBUFS;
7061 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07007062
7063 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
Jouni Malinen75ecf522011-06-27 15:19:46 -07007064}
7065
7066
7067static int nl80211_add_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
7068{
7069 struct i802_bss *bss = priv;
7070 wpa_printf(MSG_DEBUG, "nl80211: Add PMKID for " MACSTR, MAC2STR(bssid));
7071 return nl80211_pmkid(bss, NL80211_CMD_SET_PMKSA, bssid, pmkid);
7072}
7073
7074
7075static int nl80211_remove_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
7076{
7077 struct i802_bss *bss = priv;
7078 wpa_printf(MSG_DEBUG, "nl80211: Delete PMKID for " MACSTR,
7079 MAC2STR(bssid));
7080 return nl80211_pmkid(bss, NL80211_CMD_DEL_PMKSA, bssid, pmkid);
7081}
7082
7083
7084static int nl80211_flush_pmkid(void *priv)
7085{
7086 struct i802_bss *bss = priv;
7087 wpa_printf(MSG_DEBUG, "nl80211: Flush PMKIDs");
7088 return nl80211_pmkid(bss, NL80211_CMD_FLUSH_PMKSA, NULL, NULL);
7089}
7090
7091
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007092static void clean_survey_results(struct survey_results *survey_results)
7093{
7094 struct freq_survey *survey, *tmp;
7095
7096 if (dl_list_empty(&survey_results->survey_list))
7097 return;
7098
7099 dl_list_for_each_safe(survey, tmp, &survey_results->survey_list,
7100 struct freq_survey, list) {
7101 dl_list_del(&survey->list);
7102 os_free(survey);
7103 }
7104}
7105
7106
7107static void add_survey(struct nlattr **sinfo, u32 ifidx,
7108 struct dl_list *survey_list)
7109{
7110 struct freq_survey *survey;
7111
7112 survey = os_zalloc(sizeof(struct freq_survey));
7113 if (!survey)
7114 return;
7115
7116 survey->ifidx = ifidx;
7117 survey->freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
7118 survey->filled = 0;
7119
7120 if (sinfo[NL80211_SURVEY_INFO_NOISE]) {
7121 survey->nf = (int8_t)
7122 nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
7123 survey->filled |= SURVEY_HAS_NF;
7124 }
7125
7126 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]) {
7127 survey->channel_time =
7128 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]);
7129 survey->filled |= SURVEY_HAS_CHAN_TIME;
7130 }
7131
7132 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]) {
7133 survey->channel_time_busy =
7134 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]);
7135 survey->filled |= SURVEY_HAS_CHAN_TIME_BUSY;
7136 }
7137
7138 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]) {
7139 survey->channel_time_rx =
7140 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]);
7141 survey->filled |= SURVEY_HAS_CHAN_TIME_RX;
7142 }
7143
7144 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]) {
7145 survey->channel_time_tx =
7146 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]);
7147 survey->filled |= SURVEY_HAS_CHAN_TIME_TX;
7148 }
7149
7150 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)",
7151 survey->freq,
7152 survey->nf,
7153 (unsigned long int) survey->channel_time,
7154 (unsigned long int) survey->channel_time_busy,
7155 (unsigned long int) survey->channel_time_tx,
7156 (unsigned long int) survey->channel_time_rx,
7157 survey->filled);
7158
7159 dl_list_add_tail(survey_list, &survey->list);
7160}
7161
7162
7163static int check_survey_ok(struct nlattr **sinfo, u32 surveyed_freq,
7164 unsigned int freq_filter)
7165{
7166 if (!freq_filter)
7167 return 1;
7168
7169 return freq_filter == surveyed_freq;
7170}
7171
7172
7173static int survey_handler(struct nl_msg *msg, void *arg)
7174{
7175 struct nlattr *tb[NL80211_ATTR_MAX + 1];
7176 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
7177 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
7178 struct survey_results *survey_results;
7179 u32 surveyed_freq = 0;
7180 u32 ifidx;
7181
7182 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
7183 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
7184 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
7185 };
7186
7187 survey_results = (struct survey_results *) arg;
7188
7189 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
7190 genlmsg_attrlen(gnlh, 0), NULL);
7191
Dmitry Shmidt97672262014-02-03 13:02:54 -08007192 if (!tb[NL80211_ATTR_IFINDEX])
7193 return NL_SKIP;
7194
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007195 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
7196
7197 if (!tb[NL80211_ATTR_SURVEY_INFO])
7198 return NL_SKIP;
7199
7200 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
7201 tb[NL80211_ATTR_SURVEY_INFO],
7202 survey_policy))
7203 return NL_SKIP;
7204
7205 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY]) {
7206 wpa_printf(MSG_ERROR, "nl80211: Invalid survey data");
7207 return NL_SKIP;
7208 }
7209
7210 surveyed_freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
7211
7212 if (!check_survey_ok(sinfo, surveyed_freq,
7213 survey_results->freq_filter))
7214 return NL_SKIP;
7215
7216 if (survey_results->freq_filter &&
7217 survey_results->freq_filter != surveyed_freq) {
7218 wpa_printf(MSG_EXCESSIVE, "nl80211: Ignoring survey data for freq %d MHz",
7219 surveyed_freq);
7220 return NL_SKIP;
7221 }
7222
7223 add_survey(sinfo, ifidx, &survey_results->survey_list);
7224
7225 return NL_SKIP;
7226}
7227
7228
7229static int wpa_driver_nl80211_get_survey(void *priv, unsigned int freq)
7230{
7231 struct i802_bss *bss = priv;
7232 struct wpa_driver_nl80211_data *drv = bss->drv;
7233 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007234 int err;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007235 union wpa_event_data data;
7236 struct survey_results *survey_results;
7237
7238 os_memset(&data, 0, sizeof(data));
7239 survey_results = &data.survey_results;
7240
7241 dl_list_init(&survey_results->survey_list);
7242
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007243 msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007244 if (!msg)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007245 return -ENOBUFS;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007246
7247 if (freq)
7248 data.survey_results.freq_filter = freq;
7249
7250 do {
7251 wpa_printf(MSG_DEBUG, "nl80211: Fetch survey data");
7252 err = send_and_recv_msgs(drv, msg, survey_handler,
7253 survey_results);
7254 } while (err > 0);
7255
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007256 if (err)
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007257 wpa_printf(MSG_ERROR, "nl80211: Failed to process survey data");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007258 else
7259 wpa_supplicant_event(drv->ctx, EVENT_SURVEY, &data);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007260
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007261 clean_survey_results(survey_results);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007262 return err;
7263}
7264
7265
Dmitry Shmidt807291d2015-01-27 13:40:23 -08007266static void nl80211_set_rekey_info(void *priv, const u8 *kek, size_t kek_len,
7267 const u8 *kck, size_t kck_len,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007268 const u8 *replay_ctr)
7269{
7270 struct i802_bss *bss = priv;
7271 struct wpa_driver_nl80211_data *drv = bss->drv;
7272 struct nlattr *replay_nested;
7273 struct nl_msg *msg;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08007274 int ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007275
Dmitry Shmidtff787d52015-01-12 13:01:47 -08007276 if (!drv->set_rekey_offload)
7277 return;
7278
7279 wpa_printf(MSG_DEBUG, "nl80211: Set rekey offload");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007280 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_REKEY_OFFLOAD)) ||
7281 !(replay_nested = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA)) ||
Dmitry Shmidt807291d2015-01-27 13:40:23 -08007282 nla_put(msg, NL80211_REKEY_DATA_KEK, kek_len, kek) ||
7283 nla_put(msg, NL80211_REKEY_DATA_KCK, kck_len, kck) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007284 nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR, NL80211_REPLAY_CTR_LEN,
7285 replay_ctr)) {
7286 nl80211_nlmsg_clear(msg);
7287 nlmsg_free(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007288 return;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007289 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007290
7291 nla_nest_end(msg, replay_nested);
7292
Dmitry Shmidtff787d52015-01-12 13:01:47 -08007293 ret = send_and_recv_msgs(drv, msg, NULL, (void *) -1);
7294 if (ret == -EOPNOTSUPP) {
7295 wpa_printf(MSG_DEBUG,
7296 "nl80211: Driver does not support rekey offload");
7297 drv->set_rekey_offload = 0;
7298 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007299}
7300
7301
7302static void nl80211_send_null_frame(struct i802_bss *bss, const u8 *own_addr,
7303 const u8 *addr, int qos)
7304{
7305 /* send data frame to poll STA and check whether
7306 * this frame is ACKed */
7307 struct {
7308 struct ieee80211_hdr hdr;
7309 u16 qos_ctl;
7310 } STRUCT_PACKED nulldata;
7311 size_t size;
7312
7313 /* Send data frame to poll STA and check whether this frame is ACKed */
7314
7315 os_memset(&nulldata, 0, sizeof(nulldata));
7316
7317 if (qos) {
7318 nulldata.hdr.frame_control =
7319 IEEE80211_FC(WLAN_FC_TYPE_DATA,
7320 WLAN_FC_STYPE_QOS_NULL);
7321 size = sizeof(nulldata);
7322 } else {
7323 nulldata.hdr.frame_control =
7324 IEEE80211_FC(WLAN_FC_TYPE_DATA,
7325 WLAN_FC_STYPE_NULLFUNC);
7326 size = sizeof(struct ieee80211_hdr);
7327 }
7328
7329 nulldata.hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS);
7330 os_memcpy(nulldata.hdr.IEEE80211_DA_FROMDS, addr, ETH_ALEN);
7331 os_memcpy(nulldata.hdr.IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
7332 os_memcpy(nulldata.hdr.IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
7333
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007334 if (wpa_driver_nl80211_send_mlme(bss, (u8 *) &nulldata, size, 0, 0, 0,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007335 0, 0, NULL, 0) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007336 wpa_printf(MSG_DEBUG, "nl80211_send_null_frame: Failed to "
7337 "send poll frame");
7338}
7339
7340static void nl80211_poll_client(void *priv, const u8 *own_addr, const u8 *addr,
7341 int qos)
7342{
7343 struct i802_bss *bss = priv;
7344 struct wpa_driver_nl80211_data *drv = bss->drv;
7345 struct nl_msg *msg;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007346 int ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007347
7348 if (!drv->poll_command_supported) {
7349 nl80211_send_null_frame(bss, own_addr, addr, qos);
7350 return;
7351 }
7352
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007353 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_PROBE_CLIENT)) ||
7354 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
7355 nlmsg_free(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007356 return;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007357 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007358
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007359 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7360 if (ret < 0) {
7361 wpa_printf(MSG_DEBUG, "nl80211: Client probe request for "
7362 MACSTR " failed: ret=%d (%s)",
7363 MAC2STR(addr), ret, strerror(-ret));
7364 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007365}
7366
7367
7368static int nl80211_set_power_save(struct i802_bss *bss, int enabled)
7369{
7370 struct nl_msg *msg;
7371
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007372 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_POWER_SAVE)) ||
7373 nla_put_u32(msg, NL80211_ATTR_PS_STATE,
7374 enabled ? NL80211_PS_ENABLED : NL80211_PS_DISABLED)) {
7375 nlmsg_free(msg);
7376 return -ENOBUFS;
7377 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007378 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007379}
7380
7381
7382static int nl80211_set_p2p_powersave(void *priv, int legacy_ps, int opp_ps,
7383 int ctwindow)
7384{
7385 struct i802_bss *bss = priv;
7386
7387 wpa_printf(MSG_DEBUG, "nl80211: set_p2p_powersave (legacy_ps=%d "
7388 "opp_ps=%d ctwindow=%d)", legacy_ps, opp_ps, ctwindow);
7389
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08007390 if (opp_ps != -1 || ctwindow != -1) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007391#ifdef ANDROID_P2P
7392 wpa_driver_set_p2p_ps(priv, legacy_ps, opp_ps, ctwindow);
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08007393#else /* ANDROID_P2P */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007394 return -1; /* Not yet supported */
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08007395#endif /* ANDROID_P2P */
7396 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007397
7398 if (legacy_ps == -1)
7399 return 0;
7400 if (legacy_ps != 0 && legacy_ps != 1)
7401 return -1; /* Not yet supported */
7402
7403 return nl80211_set_power_save(bss, legacy_ps);
7404}
7405
7406
Dmitry Shmidt051af732013-10-22 13:52:46 -07007407static int nl80211_start_radar_detection(void *priv,
7408 struct hostapd_freq_params *freq)
Dmitry Shmidtea69e842013-05-13 14:52:28 -07007409{
7410 struct i802_bss *bss = priv;
7411 struct wpa_driver_nl80211_data *drv = bss->drv;
7412 struct nl_msg *msg;
7413 int ret;
7414
Dmitry Shmidt051af732013-10-22 13:52:46 -07007415 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)",
7416 freq->freq, freq->ht_enabled, freq->vht_enabled,
7417 freq->bandwidth, freq->center_freq1, freq->center_freq2);
7418
Dmitry Shmidtea69e842013-05-13 14:52:28 -07007419 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_RADAR)) {
7420 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support radar "
7421 "detection");
7422 return -1;
7423 }
7424
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007425 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_RADAR_DETECT)) ||
7426 nl80211_put_freq_params(msg, freq) < 0) {
7427 nlmsg_free(msg);
Dmitry Shmidtea69e842013-05-13 14:52:28 -07007428 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007429 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -07007430
7431 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7432 if (ret == 0)
7433 return 0;
7434 wpa_printf(MSG_DEBUG, "nl80211: Failed to start radar detection: "
7435 "%d (%s)", ret, strerror(-ret));
Dmitry Shmidtea69e842013-05-13 14:52:28 -07007436 return -1;
7437}
7438
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007439#ifdef CONFIG_TDLS
7440
7441static int nl80211_send_tdls_mgmt(void *priv, const u8 *dst, u8 action_code,
7442 u8 dialog_token, u16 status_code,
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07007443 u32 peer_capab, int initiator, const u8 *buf,
7444 size_t len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007445{
7446 struct i802_bss *bss = priv;
7447 struct wpa_driver_nl80211_data *drv = bss->drv;
7448 struct nl_msg *msg;
7449
7450 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
7451 return -EOPNOTSUPP;
7452
7453 if (!dst)
7454 return -EINVAL;
7455
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007456 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_TDLS_MGMT)) ||
7457 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
7458 nla_put_u8(msg, NL80211_ATTR_TDLS_ACTION, action_code) ||
7459 nla_put_u8(msg, NL80211_ATTR_TDLS_DIALOG_TOKEN, dialog_token) ||
7460 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status_code))
7461 goto fail;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07007462 if (peer_capab) {
7463 /*
7464 * The internal enum tdls_peer_capability definition is
7465 * currently identical with the nl80211 enum
7466 * nl80211_tdls_peer_capability, so no conversion is needed
7467 * here.
7468 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007469 if (nla_put_u32(msg, NL80211_ATTR_TDLS_PEER_CAPABILITY,
7470 peer_capab))
7471 goto fail;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07007472 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007473 if ((initiator &&
7474 nla_put_flag(msg, NL80211_ATTR_TDLS_INITIATOR)) ||
7475 nla_put(msg, NL80211_ATTR_IE, len, buf))
7476 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007477
7478 return send_and_recv_msgs(drv, msg, NULL, NULL);
7479
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007480fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007481 nlmsg_free(msg);
7482 return -ENOBUFS;
7483}
7484
7485
7486static int nl80211_tdls_oper(void *priv, enum tdls_oper oper, const u8 *peer)
7487{
7488 struct i802_bss *bss = priv;
7489 struct wpa_driver_nl80211_data *drv = bss->drv;
7490 struct nl_msg *msg;
7491 enum nl80211_tdls_operation nl80211_oper;
7492
7493 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
7494 return -EOPNOTSUPP;
7495
7496 switch (oper) {
7497 case TDLS_DISCOVERY_REQ:
7498 nl80211_oper = NL80211_TDLS_DISCOVERY_REQ;
7499 break;
7500 case TDLS_SETUP:
7501 nl80211_oper = NL80211_TDLS_SETUP;
7502 break;
7503 case TDLS_TEARDOWN:
7504 nl80211_oper = NL80211_TDLS_TEARDOWN;
7505 break;
7506 case TDLS_ENABLE_LINK:
7507 nl80211_oper = NL80211_TDLS_ENABLE_LINK;
7508 break;
7509 case TDLS_DISABLE_LINK:
7510 nl80211_oper = NL80211_TDLS_DISABLE_LINK;
7511 break;
7512 case TDLS_ENABLE:
7513 return 0;
7514 case TDLS_DISABLE:
7515 return 0;
7516 default:
7517 return -EINVAL;
7518 }
7519
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007520 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_TDLS_OPER)) ||
7521 nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, nl80211_oper) ||
7522 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer)) {
7523 nlmsg_free(msg);
7524 return -ENOBUFS;
7525 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007526
7527 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007528}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007529
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007530
7531static int
7532nl80211_tdls_enable_channel_switch(void *priv, const u8 *addr, u8 oper_class,
7533 const struct hostapd_freq_params *params)
7534{
7535 struct i802_bss *bss = priv;
7536 struct wpa_driver_nl80211_data *drv = bss->drv;
7537 struct nl_msg *msg;
7538 int ret = -ENOBUFS;
7539
7540 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT) ||
7541 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_CHANNEL_SWITCH))
7542 return -EOPNOTSUPP;
7543
7544 wpa_printf(MSG_DEBUG, "nl80211: Enable TDLS channel switch " MACSTR
7545 " oper_class=%u freq=%u",
7546 MAC2STR(addr), oper_class, params->freq);
7547 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_TDLS_CHANNEL_SWITCH);
7548 if (!msg ||
7549 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
7550 nla_put_u8(msg, NL80211_ATTR_OPER_CLASS, oper_class) ||
7551 (ret = nl80211_put_freq_params(msg, params))) {
7552 nlmsg_free(msg);
7553 wpa_printf(MSG_DEBUG, "nl80211: Could not build TDLS chan switch");
7554 return ret;
7555 }
7556
7557 return send_and_recv_msgs(drv, msg, NULL, NULL);
7558}
7559
7560
7561static int
7562nl80211_tdls_disable_channel_switch(void *priv, const u8 *addr)
7563{
7564 struct i802_bss *bss = priv;
7565 struct wpa_driver_nl80211_data *drv = bss->drv;
7566 struct nl_msg *msg;
7567
7568 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT) ||
7569 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_CHANNEL_SWITCH))
7570 return -EOPNOTSUPP;
7571
7572 wpa_printf(MSG_DEBUG, "nl80211: Disable TDLS channel switch " MACSTR,
7573 MAC2STR(addr));
7574 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_TDLS_CANCEL_CHANNEL_SWITCH);
7575 if (!msg ||
7576 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
7577 nlmsg_free(msg);
7578 wpa_printf(MSG_DEBUG,
7579 "nl80211: Could not build TDLS cancel chan switch");
7580 return -ENOBUFS;
7581 }
7582
7583 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007584}
7585
7586#endif /* CONFIG TDLS */
7587
7588
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007589static int driver_nl80211_set_key(const char *ifname, void *priv,
7590 enum wpa_alg alg, const u8 *addr,
7591 int key_idx, int set_tx,
7592 const u8 *seq, size_t seq_len,
7593 const u8 *key, size_t key_len)
7594{
7595 struct i802_bss *bss = priv;
7596 return wpa_driver_nl80211_set_key(ifname, bss, alg, addr, key_idx,
7597 set_tx, seq, seq_len, key, key_len);
7598}
7599
7600
7601static int driver_nl80211_scan2(void *priv,
7602 struct wpa_driver_scan_params *params)
7603{
7604 struct i802_bss *bss = priv;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007605#ifdef CONFIG_DRIVER_NL80211_QCA
7606 struct wpa_driver_nl80211_data *drv = bss->drv;
7607
7608 /*
7609 * Do a vendor specific scan if possible. If only_new_results is
7610 * set, do a normal scan since a kernel (cfg80211) BSS cache flush
7611 * cannot be achieved through a vendor scan. The below condition may
7612 * need to be modified if new scan flags are added in the future whose
7613 * functionality can only be achieved through a normal scan.
7614 */
7615 if (drv->scan_vendor_cmd_avail && !params->only_new_results)
7616 return wpa_driver_nl80211_vendor_scan(bss, params);
7617#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007618 return wpa_driver_nl80211_scan(bss, params);
7619}
7620
7621
7622static int driver_nl80211_deauthenticate(void *priv, const u8 *addr,
7623 int reason_code)
7624{
7625 struct i802_bss *bss = priv;
7626 return wpa_driver_nl80211_deauthenticate(bss, addr, reason_code);
7627}
7628
7629
7630static int driver_nl80211_authenticate(void *priv,
7631 struct wpa_driver_auth_params *params)
7632{
7633 struct i802_bss *bss = priv;
7634 return wpa_driver_nl80211_authenticate(bss, params);
7635}
7636
7637
7638static void driver_nl80211_deinit(void *priv)
7639{
7640 struct i802_bss *bss = priv;
7641 wpa_driver_nl80211_deinit(bss);
7642}
7643
7644
7645static int driver_nl80211_if_remove(void *priv, enum wpa_driver_if_type type,
7646 const char *ifname)
7647{
7648 struct i802_bss *bss = priv;
7649 return wpa_driver_nl80211_if_remove(bss, type, ifname);
7650}
7651
7652
7653static int driver_nl80211_send_mlme(void *priv, const u8 *data,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007654 size_t data_len, int noack,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007655 unsigned int freq,
7656 const u16 *csa_offs, size_t csa_offs_len)
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007657{
7658 struct i802_bss *bss = priv;
7659 return wpa_driver_nl80211_send_mlme(bss, data, data_len, noack,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007660 freq, 0, 0, 0, csa_offs,
7661 csa_offs_len);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007662}
7663
7664
7665static int driver_nl80211_sta_remove(void *priv, const u8 *addr)
7666{
7667 struct i802_bss *bss = priv;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007668 return wpa_driver_nl80211_sta_remove(bss, addr, -1, 0);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007669}
7670
7671
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007672static int driver_nl80211_set_sta_vlan(void *priv, const u8 *addr,
7673 const char *ifname, int vlan_id)
7674{
7675 struct i802_bss *bss = priv;
7676 return i802_set_sta_vlan(bss, addr, ifname, vlan_id);
7677}
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007678
7679
7680static int driver_nl80211_read_sta_data(void *priv,
7681 struct hostap_sta_driver_data *data,
7682 const u8 *addr)
7683{
7684 struct i802_bss *bss = priv;
7685 return i802_read_sta_data(bss, data, addr);
7686}
7687
7688
7689static int driver_nl80211_send_action(void *priv, unsigned int freq,
7690 unsigned int wait_time,
7691 const u8 *dst, const u8 *src,
7692 const u8 *bssid,
7693 const u8 *data, size_t data_len,
7694 int no_cck)
7695{
7696 struct i802_bss *bss = priv;
7697 return wpa_driver_nl80211_send_action(bss, freq, wait_time, dst, src,
7698 bssid, data, data_len, no_cck);
7699}
7700
7701
7702static int driver_nl80211_probe_req_report(void *priv, int report)
7703{
7704 struct i802_bss *bss = priv;
7705 return wpa_driver_nl80211_probe_req_report(bss, report);
7706}
7707
7708
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007709static int wpa_driver_nl80211_update_ft_ies(void *priv, const u8 *md,
7710 const u8 *ies, size_t ies_len)
7711{
7712 int ret;
7713 struct nl_msg *msg;
7714 struct i802_bss *bss = priv;
7715 struct wpa_driver_nl80211_data *drv = bss->drv;
7716 u16 mdid = WPA_GET_LE16(md);
7717
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007718 wpa_printf(MSG_DEBUG, "nl80211: Updating FT IEs");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007719 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_UPDATE_FT_IES)) ||
7720 nla_put(msg, NL80211_ATTR_IE, ies_len, ies) ||
7721 nla_put_u16(msg, NL80211_ATTR_MDID, mdid)) {
7722 nlmsg_free(msg);
7723 return -ENOBUFS;
7724 }
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007725
7726 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7727 if (ret) {
7728 wpa_printf(MSG_DEBUG, "nl80211: update_ft_ies failed "
7729 "err=%d (%s)", ret, strerror(-ret));
7730 }
7731
7732 return ret;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007733}
7734
7735
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -07007736static const u8 * wpa_driver_nl80211_get_macaddr(void *priv)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007737{
7738 struct i802_bss *bss = priv;
7739 struct wpa_driver_nl80211_data *drv = bss->drv;
7740
7741 if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE)
7742 return NULL;
7743
7744 return bss->addr;
7745}
7746
7747
Dmitry Shmidt56052862013-10-04 10:23:25 -07007748static const char * scan_state_str(enum scan_states scan_state)
7749{
7750 switch (scan_state) {
7751 case NO_SCAN:
7752 return "NO_SCAN";
7753 case SCAN_REQUESTED:
7754 return "SCAN_REQUESTED";
7755 case SCAN_STARTED:
7756 return "SCAN_STARTED";
7757 case SCAN_COMPLETED:
7758 return "SCAN_COMPLETED";
7759 case SCAN_ABORTED:
7760 return "SCAN_ABORTED";
7761 case SCHED_SCAN_STARTED:
7762 return "SCHED_SCAN_STARTED";
7763 case SCHED_SCAN_STOPPED:
7764 return "SCHED_SCAN_STOPPED";
7765 case SCHED_SCAN_RESULTS:
7766 return "SCHED_SCAN_RESULTS";
7767 }
7768
7769 return "??";
7770}
7771
7772
7773static int wpa_driver_nl80211_status(void *priv, char *buf, size_t buflen)
7774{
7775 struct i802_bss *bss = priv;
7776 struct wpa_driver_nl80211_data *drv = bss->drv;
7777 int res;
7778 char *pos, *end;
7779
7780 pos = buf;
7781 end = buf + buflen;
7782
7783 res = os_snprintf(pos, end - pos,
7784 "ifindex=%d\n"
7785 "ifname=%s\n"
7786 "brname=%s\n"
7787 "addr=" MACSTR "\n"
7788 "freq=%d\n"
7789 "%s%s%s%s%s",
7790 bss->ifindex,
7791 bss->ifname,
7792 bss->brname,
7793 MAC2STR(bss->addr),
7794 bss->freq,
7795 bss->beacon_set ? "beacon_set=1\n" : "",
7796 bss->added_if_into_bridge ?
7797 "added_if_into_bridge=1\n" : "",
7798 bss->added_bridge ? "added_bridge=1\n" : "",
7799 bss->in_deinit ? "in_deinit=1\n" : "",
7800 bss->if_dynamic ? "if_dynamic=1\n" : "");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007801 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt56052862013-10-04 10:23:25 -07007802 return pos - buf;
7803 pos += res;
7804
7805 if (bss->wdev_id_set) {
7806 res = os_snprintf(pos, end - pos, "wdev_id=%llu\n",
7807 (unsigned long long) bss->wdev_id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007808 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt56052862013-10-04 10:23:25 -07007809 return pos - buf;
7810 pos += res;
7811 }
7812
7813 res = os_snprintf(pos, end - pos,
7814 "phyname=%s\n"
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007815 "perm_addr=" MACSTR "\n"
Dmitry Shmidt56052862013-10-04 10:23:25 -07007816 "drv_ifindex=%d\n"
7817 "operstate=%d\n"
7818 "scan_state=%s\n"
7819 "auth_bssid=" MACSTR "\n"
7820 "auth_attempt_bssid=" MACSTR "\n"
7821 "bssid=" MACSTR "\n"
7822 "prev_bssid=" MACSTR "\n"
7823 "associated=%d\n"
7824 "assoc_freq=%u\n"
7825 "monitor_sock=%d\n"
7826 "monitor_ifidx=%d\n"
7827 "monitor_refcount=%d\n"
7828 "last_mgmt_freq=%u\n"
7829 "eapol_tx_sock=%d\n"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007830 "%s%s%s%s%s%s%s%s%s%s%s%s%s",
Dmitry Shmidt56052862013-10-04 10:23:25 -07007831 drv->phyname,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007832 MAC2STR(drv->perm_addr),
Dmitry Shmidt56052862013-10-04 10:23:25 -07007833 drv->ifindex,
7834 drv->operstate,
7835 scan_state_str(drv->scan_state),
7836 MAC2STR(drv->auth_bssid),
7837 MAC2STR(drv->auth_attempt_bssid),
7838 MAC2STR(drv->bssid),
7839 MAC2STR(drv->prev_bssid),
7840 drv->associated,
7841 drv->assoc_freq,
7842 drv->monitor_sock,
7843 drv->monitor_ifidx,
7844 drv->monitor_refcount,
7845 drv->last_mgmt_freq,
7846 drv->eapol_tx_sock,
7847 drv->ignore_if_down_event ?
7848 "ignore_if_down_event=1\n" : "",
7849 drv->scan_complete_events ?
7850 "scan_complete_events=1\n" : "",
7851 drv->disabled_11b_rates ?
7852 "disabled_11b_rates=1\n" : "",
7853 drv->pending_remain_on_chan ?
7854 "pending_remain_on_chan=1\n" : "",
7855 drv->in_interface_list ? "in_interface_list=1\n" : "",
7856 drv->device_ap_sme ? "device_ap_sme=1\n" : "",
7857 drv->poll_command_supported ?
7858 "poll_command_supported=1\n" : "",
7859 drv->data_tx_status ? "data_tx_status=1\n" : "",
7860 drv->scan_for_auth ? "scan_for_auth=1\n" : "",
7861 drv->retry_auth ? "retry_auth=1\n" : "",
7862 drv->use_monitor ? "use_monitor=1\n" : "",
7863 drv->ignore_next_local_disconnect ?
7864 "ignore_next_local_disconnect=1\n" : "",
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07007865 drv->ignore_next_local_deauth ?
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007866 "ignore_next_local_deauth=1\n" : "");
7867 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt56052862013-10-04 10:23:25 -07007868 return pos - buf;
7869 pos += res;
7870
7871 if (drv->has_capability) {
7872 res = os_snprintf(pos, end - pos,
7873 "capa.key_mgmt=0x%x\n"
7874 "capa.enc=0x%x\n"
7875 "capa.auth=0x%x\n"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007876 "capa.flags=0x%llx\n"
7877 "capa.rrm_flags=0x%x\n"
Dmitry Shmidt56052862013-10-04 10:23:25 -07007878 "capa.max_scan_ssids=%d\n"
7879 "capa.max_sched_scan_ssids=%d\n"
7880 "capa.sched_scan_supported=%d\n"
7881 "capa.max_match_sets=%d\n"
7882 "capa.max_remain_on_chan=%u\n"
7883 "capa.max_stations=%u\n"
7884 "capa.probe_resp_offloads=0x%x\n"
7885 "capa.max_acl_mac_addrs=%u\n"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007886 "capa.num_multichan_concurrent=%u\n"
7887 "capa.mac_addr_rand_sched_scan_supported=%d\n"
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007888 "capa.mac_addr_rand_scan_supported=%d\n"
7889 "capa.conc_capab=%u\n"
7890 "capa.max_conc_chan_2_4=%u\n"
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007891 "capa.max_conc_chan_5_0=%u\n"
7892 "capa.max_sched_scan_plans=%u\n"
7893 "capa.max_sched_scan_plan_interval=%u\n"
7894 "capa.max_sched_scan_plan_iterations=%u\n",
Dmitry Shmidt56052862013-10-04 10:23:25 -07007895 drv->capa.key_mgmt,
7896 drv->capa.enc,
7897 drv->capa.auth,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007898 (unsigned long long) drv->capa.flags,
7899 drv->capa.rrm_flags,
Dmitry Shmidt56052862013-10-04 10:23:25 -07007900 drv->capa.max_scan_ssids,
7901 drv->capa.max_sched_scan_ssids,
7902 drv->capa.sched_scan_supported,
7903 drv->capa.max_match_sets,
7904 drv->capa.max_remain_on_chan,
7905 drv->capa.max_stations,
7906 drv->capa.probe_resp_offloads,
7907 drv->capa.max_acl_mac_addrs,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007908 drv->capa.num_multichan_concurrent,
7909 drv->capa.mac_addr_rand_sched_scan_supported,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007910 drv->capa.mac_addr_rand_scan_supported,
7911 drv->capa.conc_capab,
7912 drv->capa.max_conc_chan_2_4,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007913 drv->capa.max_conc_chan_5_0,
7914 drv->capa.max_sched_scan_plans,
7915 drv->capa.max_sched_scan_plan_interval,
7916 drv->capa.max_sched_scan_plan_iterations);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007917 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt56052862013-10-04 10:23:25 -07007918 return pos - buf;
7919 pos += res;
7920 }
7921
7922 return pos - buf;
7923}
7924
7925
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007926static int set_beacon_data(struct nl_msg *msg, struct beacon_data *settings)
7927{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007928 if ((settings->head &&
7929 nla_put(msg, NL80211_ATTR_BEACON_HEAD,
7930 settings->head_len, settings->head)) ||
7931 (settings->tail &&
7932 nla_put(msg, NL80211_ATTR_BEACON_TAIL,
7933 settings->tail_len, settings->tail)) ||
7934 (settings->beacon_ies &&
7935 nla_put(msg, NL80211_ATTR_IE,
7936 settings->beacon_ies_len, settings->beacon_ies)) ||
7937 (settings->proberesp_ies &&
7938 nla_put(msg, NL80211_ATTR_IE_PROBE_RESP,
7939 settings->proberesp_ies_len, settings->proberesp_ies)) ||
7940 (settings->assocresp_ies &&
7941 nla_put(msg, NL80211_ATTR_IE_ASSOC_RESP,
7942 settings->assocresp_ies_len, settings->assocresp_ies)) ||
7943 (settings->probe_resp &&
7944 nla_put(msg, NL80211_ATTR_PROBE_RESP,
7945 settings->probe_resp_len, settings->probe_resp)))
7946 return -ENOBUFS;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007947
7948 return 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007949}
7950
7951
7952static int nl80211_switch_channel(void *priv, struct csa_settings *settings)
7953{
7954 struct nl_msg *msg;
7955 struct i802_bss *bss = priv;
7956 struct wpa_driver_nl80211_data *drv = bss->drv;
7957 struct nlattr *beacon_csa;
7958 int ret = -ENOBUFS;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007959 int csa_off_len = 0;
7960 int i;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007961
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08007962 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 -08007963 settings->cs_count, settings->block_tx,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08007964 settings->freq_params.freq, settings->freq_params.bandwidth,
7965 settings->freq_params.center_freq1,
7966 settings->freq_params.center_freq2);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007967
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007968 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_AP_CSA)) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007969 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support channel switch command");
7970 return -EOPNOTSUPP;
7971 }
7972
7973 if ((drv->nlmode != NL80211_IFTYPE_AP) &&
7974 (drv->nlmode != NL80211_IFTYPE_P2P_GO))
7975 return -EOPNOTSUPP;
7976
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007977 /*
7978 * Remove empty counters, assuming Probe Response and Beacon frame
7979 * counters match. This implementation assumes that there are only two
7980 * counters.
7981 */
7982 if (settings->counter_offset_beacon[0] &&
7983 !settings->counter_offset_beacon[1]) {
7984 csa_off_len = 1;
7985 } else if (settings->counter_offset_beacon[1] &&
7986 !settings->counter_offset_beacon[0]) {
7987 csa_off_len = 1;
7988 settings->counter_offset_beacon[0] =
7989 settings->counter_offset_beacon[1];
7990 settings->counter_offset_presp[0] =
7991 settings->counter_offset_presp[1];
7992 } else if (settings->counter_offset_beacon[1] &&
7993 settings->counter_offset_beacon[0]) {
7994 csa_off_len = 2;
7995 } else {
7996 wpa_printf(MSG_ERROR, "nl80211: No CSA counters provided");
7997 return -EINVAL;
7998 }
7999
8000 /* Check CSA counters validity */
8001 if (drv->capa.max_csa_counters &&
8002 csa_off_len > drv->capa.max_csa_counters) {
8003 wpa_printf(MSG_ERROR,
8004 "nl80211: Too many CSA counters provided");
8005 return -EINVAL;
8006 }
8007
8008 if (!settings->beacon_csa.tail)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008009 return -EINVAL;
8010
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008011 for (i = 0; i < csa_off_len; i++) {
8012 u16 csa_c_off_bcn = settings->counter_offset_beacon[i];
8013 u16 csa_c_off_presp = settings->counter_offset_presp[i];
8014
8015 if ((settings->beacon_csa.tail_len <= csa_c_off_bcn) ||
8016 (settings->beacon_csa.tail[csa_c_off_bcn] !=
8017 settings->cs_count))
8018 return -EINVAL;
8019
8020 if (settings->beacon_csa.probe_resp &&
8021 ((settings->beacon_csa.probe_resp_len <=
8022 csa_c_off_presp) ||
8023 (settings->beacon_csa.probe_resp[csa_c_off_presp] !=
8024 settings->cs_count)))
8025 return -EINVAL;
8026 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008027
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008028 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_CHANNEL_SWITCH)) ||
8029 nla_put_u32(msg, NL80211_ATTR_CH_SWITCH_COUNT,
8030 settings->cs_count) ||
8031 (ret = nl80211_put_freq_params(msg, &settings->freq_params)) ||
8032 (settings->block_tx &&
8033 nla_put_flag(msg, NL80211_ATTR_CH_SWITCH_BLOCK_TX)))
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008034 goto error;
8035
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008036 /* beacon_after params */
8037 ret = set_beacon_data(msg, &settings->beacon_after);
8038 if (ret)
8039 goto error;
8040
8041 /* beacon_csa params */
8042 beacon_csa = nla_nest_start(msg, NL80211_ATTR_CSA_IES);
8043 if (!beacon_csa)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008044 goto fail;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008045
8046 ret = set_beacon_data(msg, &settings->beacon_csa);
8047 if (ret)
8048 goto error;
8049
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008050 if (nla_put(msg, NL80211_ATTR_CSA_C_OFF_BEACON,
8051 csa_off_len * sizeof(u16),
8052 settings->counter_offset_beacon) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008053 (settings->beacon_csa.probe_resp &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008054 nla_put(msg, NL80211_ATTR_CSA_C_OFF_PRESP,
8055 csa_off_len * sizeof(u16),
8056 settings->counter_offset_presp)))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008057 goto fail;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008058
8059 nla_nest_end(msg, beacon_csa);
8060 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8061 if (ret) {
8062 wpa_printf(MSG_DEBUG, "nl80211: switch_channel failed err=%d (%s)",
8063 ret, strerror(-ret));
8064 }
8065 return ret;
8066
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008067fail:
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008068 ret = -ENOBUFS;
8069error:
8070 nlmsg_free(msg);
8071 wpa_printf(MSG_DEBUG, "nl80211: Could not build channel switch request");
8072 return ret;
8073}
8074
8075
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008076static int nl80211_add_ts(void *priv, u8 tsid, const u8 *addr,
8077 u8 user_priority, u16 admitted_time)
8078{
8079 struct i802_bss *bss = priv;
8080 struct wpa_driver_nl80211_data *drv = bss->drv;
8081 struct nl_msg *msg;
8082 int ret;
8083
8084 wpa_printf(MSG_DEBUG,
8085 "nl80211: add_ts request: tsid=%u admitted_time=%u up=%d",
8086 tsid, admitted_time, user_priority);
8087
8088 if (!is_sta_interface(drv->nlmode))
8089 return -ENOTSUP;
8090
8091 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_ADD_TX_TS);
8092 if (!msg ||
8093 nla_put_u8(msg, NL80211_ATTR_TSID, tsid) ||
8094 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
8095 nla_put_u8(msg, NL80211_ATTR_USER_PRIO, user_priority) ||
8096 nla_put_u16(msg, NL80211_ATTR_ADMITTED_TIME, admitted_time)) {
8097 nlmsg_free(msg);
8098 return -ENOBUFS;
8099 }
8100
8101 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8102 if (ret)
8103 wpa_printf(MSG_DEBUG, "nl80211: add_ts failed err=%d (%s)",
8104 ret, strerror(-ret));
8105 return ret;
8106}
8107
8108
8109static int nl80211_del_ts(void *priv, u8 tsid, const u8 *addr)
8110{
8111 struct i802_bss *bss = priv;
8112 struct wpa_driver_nl80211_data *drv = bss->drv;
8113 struct nl_msg *msg;
8114 int ret;
8115
8116 wpa_printf(MSG_DEBUG, "nl80211: del_ts request: tsid=%u", tsid);
8117
8118 if (!is_sta_interface(drv->nlmode))
8119 return -ENOTSUP;
8120
8121 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_DEL_TX_TS)) ||
8122 nla_put_u8(msg, NL80211_ATTR_TSID, tsid) ||
8123 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
8124 nlmsg_free(msg);
8125 return -ENOBUFS;
8126 }
8127
8128 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8129 if (ret)
8130 wpa_printf(MSG_DEBUG, "nl80211: del_ts failed err=%d (%s)",
8131 ret, strerror(-ret));
8132 return ret;
8133}
8134
8135
Dmitry Shmidta38abf92014-03-06 13:38:44 -08008136#ifdef CONFIG_TESTING_OPTIONS
8137static int cmd_reply_handler(struct nl_msg *msg, void *arg)
8138{
8139 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
8140 struct wpabuf *buf = arg;
8141
8142 if (!buf)
8143 return NL_SKIP;
8144
8145 if ((size_t) genlmsg_attrlen(gnlh, 0) > wpabuf_tailroom(buf)) {
8146 wpa_printf(MSG_INFO, "nl80211: insufficient buffer space for reply");
8147 return NL_SKIP;
8148 }
8149
8150 wpabuf_put_data(buf, genlmsg_attrdata(gnlh, 0),
8151 genlmsg_attrlen(gnlh, 0));
8152
8153 return NL_SKIP;
8154}
8155#endif /* CONFIG_TESTING_OPTIONS */
8156
8157
8158static int vendor_reply_handler(struct nl_msg *msg, void *arg)
8159{
8160 struct nlattr *tb[NL80211_ATTR_MAX + 1];
8161 struct nlattr *nl_vendor_reply, *nl;
8162 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
8163 struct wpabuf *buf = arg;
8164 int rem;
8165
8166 if (!buf)
8167 return NL_SKIP;
8168
8169 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
8170 genlmsg_attrlen(gnlh, 0), NULL);
8171 nl_vendor_reply = tb[NL80211_ATTR_VENDOR_DATA];
8172
8173 if (!nl_vendor_reply)
8174 return NL_SKIP;
8175
8176 if ((size_t) nla_len(nl_vendor_reply) > wpabuf_tailroom(buf)) {
8177 wpa_printf(MSG_INFO, "nl80211: Vendor command: insufficient buffer space for reply");
8178 return NL_SKIP;
8179 }
8180
8181 nla_for_each_nested(nl, nl_vendor_reply, rem) {
8182 wpabuf_put_data(buf, nla_data(nl), nla_len(nl));
8183 }
8184
8185 return NL_SKIP;
8186}
8187
8188
8189static int nl80211_vendor_cmd(void *priv, unsigned int vendor_id,
8190 unsigned int subcmd, const u8 *data,
8191 size_t data_len, struct wpabuf *buf)
8192{
8193 struct i802_bss *bss = priv;
8194 struct wpa_driver_nl80211_data *drv = bss->drv;
8195 struct nl_msg *msg;
8196 int ret;
8197
Dmitry Shmidta38abf92014-03-06 13:38:44 -08008198#ifdef CONFIG_TESTING_OPTIONS
8199 if (vendor_id == 0xffffffff) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008200 msg = nlmsg_alloc();
8201 if (!msg)
8202 return -ENOMEM;
8203
Dmitry Shmidta38abf92014-03-06 13:38:44 -08008204 nl80211_cmd(drv, msg, 0, subcmd);
8205 if (nlmsg_append(msg, (void *) data, data_len, NLMSG_ALIGNTO) <
8206 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008207 goto fail;
Dmitry Shmidta38abf92014-03-06 13:38:44 -08008208 ret = send_and_recv_msgs(drv, msg, cmd_reply_handler, buf);
8209 if (ret)
8210 wpa_printf(MSG_DEBUG, "nl80211: command failed err=%d",
8211 ret);
8212 return ret;
8213 }
8214#endif /* CONFIG_TESTING_OPTIONS */
8215
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008216 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_VENDOR)) ||
8217 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, vendor_id) ||
8218 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD, subcmd) ||
8219 (data &&
8220 nla_put(msg, NL80211_ATTR_VENDOR_DATA, data_len, data)))
8221 goto fail;
Dmitry Shmidta38abf92014-03-06 13:38:44 -08008222
8223 ret = send_and_recv_msgs(drv, msg, vendor_reply_handler, buf);
8224 if (ret)
8225 wpa_printf(MSG_DEBUG, "nl80211: vendor command failed err=%d",
8226 ret);
8227 return ret;
8228
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008229fail:
Dmitry Shmidta38abf92014-03-06 13:38:44 -08008230 nlmsg_free(msg);
8231 return -ENOBUFS;
8232}
8233
8234
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008235static int nl80211_set_qos_map(void *priv, const u8 *qos_map_set,
8236 u8 qos_map_set_len)
8237{
8238 struct i802_bss *bss = priv;
8239 struct wpa_driver_nl80211_data *drv = bss->drv;
8240 struct nl_msg *msg;
8241 int ret;
8242
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008243 wpa_hexdump(MSG_DEBUG, "nl80211: Setting QoS Map",
8244 qos_map_set, qos_map_set_len);
8245
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008246 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_QOS_MAP)) ||
8247 nla_put(msg, NL80211_ATTR_QOS_MAP, qos_map_set_len, qos_map_set)) {
8248 nlmsg_free(msg);
8249 return -ENOBUFS;
8250 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008251
8252 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8253 if (ret)
8254 wpa_printf(MSG_DEBUG, "nl80211: Setting QoS Map failed");
8255
8256 return ret;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008257}
8258
8259
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07008260static int nl80211_set_wowlan(void *priv,
8261 const struct wowlan_triggers *triggers)
8262{
8263 struct i802_bss *bss = priv;
8264 struct wpa_driver_nl80211_data *drv = bss->drv;
8265 struct nl_msg *msg;
8266 struct nlattr *wowlan_triggers;
8267 int ret;
8268
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07008269 wpa_printf(MSG_DEBUG, "nl80211: Setting wowlan");
8270
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008271 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_SET_WOWLAN)) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008272 !(wowlan_triggers = nla_nest_start(msg,
8273 NL80211_ATTR_WOWLAN_TRIGGERS)) ||
8274 (triggers->any &&
8275 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
8276 (triggers->disconnect &&
8277 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
8278 (triggers->magic_pkt &&
8279 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
8280 (triggers->gtk_rekey_failure &&
8281 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
8282 (triggers->eap_identity_req &&
8283 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
8284 (triggers->four_way_handshake &&
8285 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
8286 (triggers->rfkill_release &&
8287 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))) {
8288 nlmsg_free(msg);
8289 return -ENOBUFS;
8290 }
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07008291
8292 nla_nest_end(msg, wowlan_triggers);
8293
8294 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8295 if (ret)
8296 wpa_printf(MSG_DEBUG, "nl80211: Setting wowlan failed");
8297
8298 return ret;
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07008299}
8300
8301
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008302#ifdef CONFIG_DRIVER_NL80211_QCA
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008303static int nl80211_roaming(void *priv, int allowed, const u8 *bssid)
8304{
8305 struct i802_bss *bss = priv;
8306 struct wpa_driver_nl80211_data *drv = bss->drv;
8307 struct nl_msg *msg;
8308 struct nlattr *params;
8309
8310 wpa_printf(MSG_DEBUG, "nl80211: Roaming policy: allowed=%d", allowed);
8311
8312 if (!drv->roaming_vendor_cmd_avail) {
8313 wpa_printf(MSG_DEBUG,
8314 "nl80211: Ignore roaming policy change since driver does not provide command for setting it");
8315 return -1;
8316 }
8317
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008318 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
8319 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
8320 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
8321 QCA_NL80211_VENDOR_SUBCMD_ROAMING) ||
8322 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
8323 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_ROAMING_POLICY,
8324 allowed ? QCA_ROAMING_ALLOWED_WITHIN_ESS :
8325 QCA_ROAMING_NOT_ALLOWED) ||
8326 (bssid &&
8327 nla_put(msg, QCA_WLAN_VENDOR_ATTR_MAC_ADDR, ETH_ALEN, bssid))) {
8328 nlmsg_free(msg);
8329 return -1;
8330 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008331 nla_nest_end(msg, params);
8332
8333 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008334}
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008335#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008336
8337
8338static int nl80211_set_mac_addr(void *priv, const u8 *addr)
8339{
8340 struct i802_bss *bss = priv;
8341 struct wpa_driver_nl80211_data *drv = bss->drv;
8342 int new_addr = addr != NULL;
8343
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008344 if (TEST_FAIL())
8345 return -1;
8346
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008347 if (!addr)
8348 addr = drv->perm_addr;
8349
8350 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0) < 0)
8351 return -1;
8352
8353 if (linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname, addr) < 0)
8354 {
8355 wpa_printf(MSG_DEBUG,
8356 "nl80211: failed to set_mac_addr for %s to " MACSTR,
8357 bss->ifname, MAC2STR(addr));
8358 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname,
8359 1) < 0) {
8360 wpa_printf(MSG_DEBUG,
8361 "nl80211: Could not restore interface UP after failed set_mac_addr");
8362 }
8363 return -1;
8364 }
8365
8366 wpa_printf(MSG_DEBUG, "nl80211: set_mac_addr for %s to " MACSTR,
8367 bss->ifname, MAC2STR(addr));
8368 drv->addr_changed = new_addr;
8369 os_memcpy(bss->addr, addr, ETH_ALEN);
8370
8371 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1) < 0)
8372 {
8373 wpa_printf(MSG_DEBUG,
8374 "nl80211: Could not restore interface UP after set_mac_addr");
8375 }
8376
8377 return 0;
8378}
8379
8380
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008381#ifdef CONFIG_MESH
8382
8383static int wpa_driver_nl80211_init_mesh(void *priv)
8384{
8385 if (wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_MESH_POINT)) {
8386 wpa_printf(MSG_INFO,
8387 "nl80211: Failed to set interface into mesh mode");
8388 return -1;
8389 }
8390 return 0;
8391}
8392
8393
Dmitry Shmidtff787d52015-01-12 13:01:47 -08008394static int nl80211_put_mesh_id(struct nl_msg *msg, const u8 *mesh_id,
8395 size_t mesh_id_len)
8396{
8397 if (mesh_id) {
8398 wpa_hexdump_ascii(MSG_DEBUG, " * Mesh ID (SSID)",
8399 mesh_id, mesh_id_len);
8400 return nla_put(msg, NL80211_ATTR_MESH_ID, mesh_id_len, mesh_id);
8401 }
8402
8403 return 0;
8404}
8405
8406
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008407static int nl80211_join_mesh(struct i802_bss *bss,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008408 struct wpa_driver_mesh_join_params *params)
8409{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008410 struct wpa_driver_nl80211_data *drv = bss->drv;
8411 struct nl_msg *msg;
8412 struct nlattr *container;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08008413 int ret = -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008414
8415 wpa_printf(MSG_DEBUG, "nl80211: mesh join (ifindex=%d)", drv->ifindex);
8416 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_JOIN_MESH);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08008417 if (!msg ||
8418 nl80211_put_freq_params(msg, &params->freq) ||
8419 nl80211_put_basic_rates(msg, params->basic_rates) ||
8420 nl80211_put_mesh_id(msg, params->meshid, params->meshid_len) ||
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07008421 nl80211_put_beacon_int(msg, params->beacon_int) ||
8422 nl80211_put_dtim_period(msg, params->dtim_period))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008423 goto fail;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008424
8425 wpa_printf(MSG_DEBUG, " * flags=%08X", params->flags);
8426
8427 container = nla_nest_start(msg, NL80211_ATTR_MESH_SETUP);
8428 if (!container)
8429 goto fail;
8430
8431 if (params->ies) {
8432 wpa_hexdump(MSG_DEBUG, " * IEs", params->ies, params->ie_len);
8433 if (nla_put(msg, NL80211_MESH_SETUP_IE, params->ie_len,
8434 params->ies))
8435 goto fail;
8436 }
8437 /* WPA_DRIVER_MESH_FLAG_OPEN_AUTH is treated as default by nl80211 */
8438 if (params->flags & WPA_DRIVER_MESH_FLAG_SAE_AUTH) {
8439 if (nla_put_u8(msg, NL80211_MESH_SETUP_AUTH_PROTOCOL, 0x1) ||
8440 nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_AUTH))
8441 goto fail;
8442 }
8443 if ((params->flags & WPA_DRIVER_MESH_FLAG_AMPE) &&
8444 nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_AMPE))
8445 goto fail;
8446 if ((params->flags & WPA_DRIVER_MESH_FLAG_USER_MPM) &&
8447 nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_MPM))
8448 goto fail;
8449 nla_nest_end(msg, container);
8450
8451 container = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
8452 if (!container)
8453 goto fail;
8454
8455 if (!(params->conf.flags & WPA_DRIVER_MESH_CONF_FLAG_AUTO_PLINKS) &&
8456 nla_put_u32(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS, 0))
8457 goto fail;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07008458 if (nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008459 params->max_peer_links))
8460 goto fail;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08008461
8462 /*
8463 * Set NL80211_MESHCONF_PLINK_TIMEOUT even if user mpm is used because
8464 * the timer could disconnect stations even in that case.
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08008465 */
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008466 if (nla_put_u32(msg, NL80211_MESHCONF_PLINK_TIMEOUT,
8467 params->conf.peer_link_timeout)) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08008468 wpa_printf(MSG_ERROR, "nl80211: Failed to set PLINK_TIMEOUT");
8469 goto fail;
8470 }
8471
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008472 nla_nest_end(msg, container);
8473
8474 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8475 msg = NULL;
8476 if (ret) {
8477 wpa_printf(MSG_DEBUG, "nl80211: mesh join failed: ret=%d (%s)",
8478 ret, strerror(-ret));
8479 goto fail;
8480 }
8481 ret = 0;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07008482 drv->assoc_freq = bss->freq = params->freq.freq;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008483 wpa_printf(MSG_DEBUG, "nl80211: mesh join request send successfully");
8484
8485fail:
8486 nlmsg_free(msg);
8487 return ret;
8488}
8489
8490
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008491static int
8492wpa_driver_nl80211_join_mesh(void *priv,
8493 struct wpa_driver_mesh_join_params *params)
8494{
8495 struct i802_bss *bss = priv;
8496 int ret, timeout;
8497
8498 timeout = params->conf.peer_link_timeout;
8499
8500 /* Disable kernel inactivity timer */
8501 if (params->flags & WPA_DRIVER_MESH_FLAG_USER_MPM)
8502 params->conf.peer_link_timeout = 0;
8503
8504 ret = nl80211_join_mesh(bss, params);
8505 if (ret == -EINVAL && params->conf.peer_link_timeout == 0) {
8506 wpa_printf(MSG_DEBUG,
8507 "nl80211: Mesh join retry for peer_link_timeout");
8508 /*
8509 * Old kernel does not support setting
8510 * NL80211_MESHCONF_PLINK_TIMEOUT to zero, so set 60 seconds
8511 * into future from peer_link_timeout.
8512 */
8513 params->conf.peer_link_timeout = timeout + 60;
8514 ret = nl80211_join_mesh(priv, params);
8515 }
8516
8517 params->conf.peer_link_timeout = timeout;
8518 return ret;
8519}
8520
8521
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008522static int wpa_driver_nl80211_leave_mesh(void *priv)
8523{
8524 struct i802_bss *bss = priv;
8525 struct wpa_driver_nl80211_data *drv = bss->drv;
8526 struct nl_msg *msg;
8527 int ret;
8528
8529 wpa_printf(MSG_DEBUG, "nl80211: mesh leave (ifindex=%d)", drv->ifindex);
8530 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_LEAVE_MESH);
8531 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8532 if (ret) {
8533 wpa_printf(MSG_DEBUG, "nl80211: mesh leave failed: ret=%d (%s)",
8534 ret, strerror(-ret));
8535 } else {
8536 wpa_printf(MSG_DEBUG,
8537 "nl80211: mesh leave request send successfully");
8538 }
8539
8540 if (wpa_driver_nl80211_set_mode(drv->first_bss,
8541 NL80211_IFTYPE_STATION)) {
8542 wpa_printf(MSG_INFO,
8543 "nl80211: Failed to set interface into station mode");
8544 }
8545 return ret;
8546}
8547
8548#endif /* CONFIG_MESH */
8549
8550
8551static int wpa_driver_br_add_ip_neigh(void *priv, u8 version,
8552 const u8 *ipaddr, int prefixlen,
8553 const u8 *addr)
8554{
8555#ifdef CONFIG_LIBNL3_ROUTE
8556 struct i802_bss *bss = priv;
8557 struct wpa_driver_nl80211_data *drv = bss->drv;
8558 struct rtnl_neigh *rn;
8559 struct nl_addr *nl_ipaddr = NULL;
8560 struct nl_addr *nl_lladdr = NULL;
8561 int family, addrsize;
8562 int res;
8563
8564 if (!ipaddr || prefixlen == 0 || !addr)
8565 return -EINVAL;
8566
8567 if (bss->br_ifindex == 0) {
8568 wpa_printf(MSG_DEBUG,
8569 "nl80211: bridge must be set before adding an ip neigh to it");
8570 return -1;
8571 }
8572
8573 if (!drv->rtnl_sk) {
8574 wpa_printf(MSG_DEBUG,
8575 "nl80211: nl_sock for NETLINK_ROUTE is not initialized");
8576 return -1;
8577 }
8578
8579 if (version == 4) {
8580 family = AF_INET;
8581 addrsize = 4;
8582 } else if (version == 6) {
8583 family = AF_INET6;
8584 addrsize = 16;
8585 } else {
8586 return -EINVAL;
8587 }
8588
8589 rn = rtnl_neigh_alloc();
8590 if (rn == NULL)
8591 return -ENOMEM;
8592
8593 /* set the destination ip address for neigh */
8594 nl_ipaddr = nl_addr_build(family, (void *) ipaddr, addrsize);
8595 if (nl_ipaddr == NULL) {
8596 wpa_printf(MSG_DEBUG, "nl80211: nl_ipaddr build failed");
8597 res = -ENOMEM;
8598 goto errout;
8599 }
8600 nl_addr_set_prefixlen(nl_ipaddr, prefixlen);
8601 res = rtnl_neigh_set_dst(rn, nl_ipaddr);
8602 if (res) {
8603 wpa_printf(MSG_DEBUG,
8604 "nl80211: neigh set destination addr failed");
8605 goto errout;
8606 }
8607
8608 /* set the corresponding lladdr for neigh */
8609 nl_lladdr = nl_addr_build(AF_BRIDGE, (u8 *) addr, ETH_ALEN);
8610 if (nl_lladdr == NULL) {
8611 wpa_printf(MSG_DEBUG, "nl80211: neigh set lladdr failed");
8612 res = -ENOMEM;
8613 goto errout;
8614 }
8615 rtnl_neigh_set_lladdr(rn, nl_lladdr);
8616
8617 rtnl_neigh_set_ifindex(rn, bss->br_ifindex);
8618 rtnl_neigh_set_state(rn, NUD_PERMANENT);
8619
8620 res = rtnl_neigh_add(drv->rtnl_sk, rn, NLM_F_CREATE);
8621 if (res) {
8622 wpa_printf(MSG_DEBUG,
8623 "nl80211: Adding bridge ip neigh failed: %s",
8624 strerror(errno));
8625 }
8626errout:
8627 if (nl_lladdr)
8628 nl_addr_put(nl_lladdr);
8629 if (nl_ipaddr)
8630 nl_addr_put(nl_ipaddr);
8631 if (rn)
8632 rtnl_neigh_put(rn);
8633 return res;
8634#else /* CONFIG_LIBNL3_ROUTE */
8635 return -1;
8636#endif /* CONFIG_LIBNL3_ROUTE */
8637}
8638
8639
8640static int wpa_driver_br_delete_ip_neigh(void *priv, u8 version,
8641 const u8 *ipaddr)
8642{
8643#ifdef CONFIG_LIBNL3_ROUTE
8644 struct i802_bss *bss = priv;
8645 struct wpa_driver_nl80211_data *drv = bss->drv;
8646 struct rtnl_neigh *rn;
8647 struct nl_addr *nl_ipaddr;
8648 int family, addrsize;
8649 int res;
8650
8651 if (!ipaddr)
8652 return -EINVAL;
8653
8654 if (version == 4) {
8655 family = AF_INET;
8656 addrsize = 4;
8657 } else if (version == 6) {
8658 family = AF_INET6;
8659 addrsize = 16;
8660 } else {
8661 return -EINVAL;
8662 }
8663
8664 if (bss->br_ifindex == 0) {
8665 wpa_printf(MSG_DEBUG,
8666 "nl80211: bridge must be set to delete an ip neigh");
8667 return -1;
8668 }
8669
8670 if (!drv->rtnl_sk) {
8671 wpa_printf(MSG_DEBUG,
8672 "nl80211: nl_sock for NETLINK_ROUTE is not initialized");
8673 return -1;
8674 }
8675
8676 rn = rtnl_neigh_alloc();
8677 if (rn == NULL)
8678 return -ENOMEM;
8679
8680 /* set the destination ip address for neigh */
8681 nl_ipaddr = nl_addr_build(family, (void *) ipaddr, addrsize);
8682 if (nl_ipaddr == NULL) {
8683 wpa_printf(MSG_DEBUG, "nl80211: nl_ipaddr build failed");
8684 res = -ENOMEM;
8685 goto errout;
8686 }
8687 res = rtnl_neigh_set_dst(rn, nl_ipaddr);
8688 if (res) {
8689 wpa_printf(MSG_DEBUG,
8690 "nl80211: neigh set destination addr failed");
8691 goto errout;
8692 }
8693
8694 rtnl_neigh_set_ifindex(rn, bss->br_ifindex);
8695
8696 res = rtnl_neigh_delete(drv->rtnl_sk, rn, 0);
8697 if (res) {
8698 wpa_printf(MSG_DEBUG,
8699 "nl80211: Deleting bridge ip neigh failed: %s",
8700 strerror(errno));
8701 }
8702errout:
8703 if (nl_ipaddr)
8704 nl_addr_put(nl_ipaddr);
8705 if (rn)
8706 rtnl_neigh_put(rn);
8707 return res;
8708#else /* CONFIG_LIBNL3_ROUTE */
8709 return -1;
8710#endif /* CONFIG_LIBNL3_ROUTE */
8711}
8712
8713
8714static int linux_write_system_file(const char *path, unsigned int val)
8715{
8716 char buf[50];
8717 int fd, len;
8718
8719 len = os_snprintf(buf, sizeof(buf), "%u\n", val);
8720 if (os_snprintf_error(sizeof(buf), len))
8721 return -1;
8722
8723 fd = open(path, O_WRONLY);
8724 if (fd < 0)
8725 return -1;
8726
8727 if (write(fd, buf, len) < 0) {
8728 wpa_printf(MSG_DEBUG,
8729 "nl80211: Failed to write Linux system file: %s with the value of %d",
8730 path, val);
8731 close(fd);
8732 return -1;
8733 }
8734 close(fd);
8735
8736 return 0;
8737}
8738
8739
8740static const char * drv_br_port_attr_str(enum drv_br_port_attr attr)
8741{
8742 switch (attr) {
8743 case DRV_BR_PORT_ATTR_PROXYARP:
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07008744 return "proxyarp_wifi";
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008745 case DRV_BR_PORT_ATTR_HAIRPIN_MODE:
8746 return "hairpin_mode";
8747 }
8748
8749 return NULL;
8750}
8751
8752
8753static int wpa_driver_br_port_set_attr(void *priv, enum drv_br_port_attr attr,
8754 unsigned int val)
8755{
8756 struct i802_bss *bss = priv;
8757 char path[128];
8758 const char *attr_txt;
8759
8760 attr_txt = drv_br_port_attr_str(attr);
8761 if (attr_txt == NULL)
8762 return -EINVAL;
8763
8764 os_snprintf(path, sizeof(path), "/sys/class/net/%s/brport/%s",
8765 bss->ifname, attr_txt);
8766
8767 if (linux_write_system_file(path, val))
8768 return -1;
8769
8770 return 0;
8771}
8772
8773
8774static const char * drv_br_net_param_str(enum drv_br_net_param param)
8775{
8776 switch (param) {
8777 case DRV_BR_NET_PARAM_GARP_ACCEPT:
8778 return "arp_accept";
Dmitry Shmidt83474442015-04-15 13:47:09 -07008779 default:
8780 return NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008781 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008782}
8783
8784
8785static int wpa_driver_br_set_net_param(void *priv, enum drv_br_net_param param,
8786 unsigned int val)
8787{
8788 struct i802_bss *bss = priv;
8789 char path[128];
8790 const char *param_txt;
8791 int ip_version = 4;
8792
Dmitry Shmidt83474442015-04-15 13:47:09 -07008793 if (param == DRV_BR_MULTICAST_SNOOPING) {
8794 os_snprintf(path, sizeof(path),
8795 "/sys/devices/virtual/net/%s/bridge/multicast_snooping",
8796 bss->brname);
8797 goto set_val;
8798 }
8799
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008800 param_txt = drv_br_net_param_str(param);
8801 if (param_txt == NULL)
8802 return -EINVAL;
8803
8804 switch (param) {
8805 case DRV_BR_NET_PARAM_GARP_ACCEPT:
8806 ip_version = 4;
8807 break;
8808 default:
8809 return -EINVAL;
8810 }
8811
8812 os_snprintf(path, sizeof(path), "/proc/sys/net/ipv%d/conf/%s/%s",
8813 ip_version, bss->brname, param_txt);
8814
Dmitry Shmidt83474442015-04-15 13:47:09 -07008815set_val:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008816 if (linux_write_system_file(path, val))
8817 return -1;
8818
8819 return 0;
8820}
8821
8822
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008823#ifdef CONFIG_DRIVER_NL80211_QCA
8824
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008825static int hw_mode_to_qca_acs(enum hostapd_hw_mode hw_mode)
8826{
8827 switch (hw_mode) {
8828 case HOSTAPD_MODE_IEEE80211B:
8829 return QCA_ACS_MODE_IEEE80211B;
8830 case HOSTAPD_MODE_IEEE80211G:
8831 return QCA_ACS_MODE_IEEE80211G;
8832 case HOSTAPD_MODE_IEEE80211A:
8833 return QCA_ACS_MODE_IEEE80211A;
8834 case HOSTAPD_MODE_IEEE80211AD:
8835 return QCA_ACS_MODE_IEEE80211AD;
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07008836 case HOSTAPD_MODE_IEEE80211ANY:
8837 return QCA_ACS_MODE_IEEE80211ANY;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008838 default:
8839 return -1;
8840 }
8841}
8842
8843
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008844static int add_acs_freq_list(struct nl_msg *msg, const int *freq_list)
8845{
8846 int i, len, ret;
8847 u32 *freqs;
8848
8849 if (!freq_list)
8850 return 0;
8851 len = int_array_len(freq_list);
8852 freqs = os_malloc(sizeof(u32) * len);
8853 if (!freqs)
8854 return -1;
8855 for (i = 0; i < len; i++)
8856 freqs[i] = freq_list[i];
8857 ret = nla_put(msg, QCA_WLAN_VENDOR_ATTR_ACS_FREQ_LIST,
8858 sizeof(u32) * len, freqs);
8859 os_free(freqs);
8860 return ret;
8861}
8862
8863
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008864static int wpa_driver_do_acs(void *priv, struct drv_acs_params *params)
8865{
8866 struct i802_bss *bss = priv;
8867 struct wpa_driver_nl80211_data *drv = bss->drv;
8868 struct nl_msg *msg;
8869 struct nlattr *data;
8870 int ret;
8871 int mode;
8872
8873 mode = hw_mode_to_qca_acs(params->hw_mode);
8874 if (mode < 0)
8875 return -1;
8876
8877 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
8878 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
8879 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
8880 QCA_NL80211_VENDOR_SUBCMD_DO_ACS) ||
8881 !(data = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
8882 nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_ACS_HW_MODE, mode) ||
8883 (params->ht_enabled &&
8884 nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_HT_ENABLED)) ||
8885 (params->ht40_enabled &&
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07008886 nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_HT40_ENABLED)) ||
8887 (params->vht_enabled &&
8888 nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_VHT_ENABLED)) ||
8889 nla_put_u16(msg, QCA_WLAN_VENDOR_ATTR_ACS_CHWIDTH,
8890 params->ch_width) ||
8891 (params->ch_list_len &&
8892 nla_put(msg, QCA_WLAN_VENDOR_ATTR_ACS_CH_LIST, params->ch_list_len,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008893 params->ch_list)) ||
8894 add_acs_freq_list(msg, params->freq_list)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008895 nlmsg_free(msg);
8896 return -ENOBUFS;
8897 }
8898 nla_nest_end(msg, data);
8899
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07008900 wpa_printf(MSG_DEBUG,
8901 "nl80211: ACS Params: HW_MODE: %d HT: %d HT40: %d VHT: %d BW: %d CH_LIST_LEN: %u",
8902 params->hw_mode, params->ht_enabled, params->ht40_enabled,
8903 params->vht_enabled, params->ch_width, params->ch_list_len);
8904
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008905 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8906 if (ret) {
8907 wpa_printf(MSG_DEBUG,
8908 "nl80211: Failed to invoke driver ACS function: %s",
8909 strerror(errno));
8910 }
8911 return ret;
8912}
8913
8914
Ravi Joshie6ccb162015-07-16 17:45:41 -07008915static int nl80211_set_band(void *priv, enum set_band band)
8916{
8917 struct i802_bss *bss = priv;
8918 struct wpa_driver_nl80211_data *drv = bss->drv;
8919 struct nl_msg *msg;
8920 struct nlattr *data;
8921 int ret;
8922 enum qca_set_band qca_band;
8923
8924 if (!drv->setband_vendor_cmd_avail)
8925 return -1;
8926
8927 switch (band) {
8928 case WPA_SETBAND_AUTO:
8929 qca_band = QCA_SETBAND_AUTO;
8930 break;
8931 case WPA_SETBAND_5G:
8932 qca_band = QCA_SETBAND_5G;
8933 break;
8934 case WPA_SETBAND_2G:
8935 qca_band = QCA_SETBAND_2G;
8936 break;
8937 default:
8938 return -1;
8939 }
8940
8941 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
8942 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
8943 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
8944 QCA_NL80211_VENDOR_SUBCMD_SETBAND) ||
8945 !(data = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
8946 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_SETBAND_VALUE, qca_band)) {
8947 nlmsg_free(msg);
8948 return -ENOBUFS;
8949 }
8950 nla_nest_end(msg, data);
8951
8952 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8953 if (ret) {
8954 wpa_printf(MSG_DEBUG,
8955 "nl80211: Driver setband function failed: %s",
8956 strerror(errno));
8957 }
8958 return ret;
8959}
8960
8961
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008962struct nl80211_pcl {
8963 unsigned int num;
8964 unsigned int *freq_list;
8965};
8966
8967static int preferred_freq_info_handler(struct nl_msg *msg, void *arg)
8968{
8969 struct nlattr *tb[NL80211_ATTR_MAX + 1];
8970 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
8971 struct nl80211_pcl *param = arg;
8972 struct nlattr *nl_vend, *attr;
8973 enum qca_iface_type iface_type;
8974 struct nlattr *tb_vendor[QCA_WLAN_VENDOR_ATTR_MAX + 1];
8975 unsigned int num, max_num;
8976 u32 *freqs;
8977
8978 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
8979 genlmsg_attrlen(gnlh, 0), NULL);
8980
8981 nl_vend = tb[NL80211_ATTR_VENDOR_DATA];
8982 if (!nl_vend)
8983 return NL_SKIP;
8984
8985 nla_parse(tb_vendor, QCA_WLAN_VENDOR_ATTR_MAX,
8986 nla_data(nl_vend), nla_len(nl_vend), NULL);
8987
8988 attr = tb_vendor[
8989 QCA_WLAN_VENDOR_ATTR_GET_PREFERRED_FREQ_LIST_IFACE_TYPE];
8990 if (!attr) {
8991 wpa_printf(MSG_ERROR, "nl80211: iface_type couldn't be found");
8992 param->num = 0;
8993 return NL_SKIP;
8994 }
8995
8996 iface_type = (enum qca_iface_type) nla_get_u32(attr);
8997 wpa_printf(MSG_DEBUG, "nl80211: Driver returned iface_type=%d",
8998 iface_type);
8999
9000 attr = tb_vendor[QCA_WLAN_VENDOR_ATTR_GET_PREFERRED_FREQ_LIST];
9001 if (!attr) {
9002 wpa_printf(MSG_ERROR,
9003 "nl80211: preferred_freq_list couldn't be found");
9004 param->num = 0;
9005 return NL_SKIP;
9006 }
9007
9008 /*
9009 * param->num has the maximum number of entries for which there
9010 * is room in the freq_list provided by the caller.
9011 */
9012 freqs = nla_data(attr);
9013 max_num = nla_len(attr) / sizeof(u32);
9014 if (max_num > param->num)
9015 max_num = param->num;
9016 for (num = 0; num < max_num; num++)
9017 param->freq_list[num] = freqs[num];
9018 param->num = num;
9019
9020 return NL_SKIP;
9021}
9022
9023
9024static int nl80211_get_pref_freq_list(void *priv,
9025 enum wpa_driver_if_type if_type,
9026 unsigned int *num,
9027 unsigned int *freq_list)
9028{
9029 struct i802_bss *bss = priv;
9030 struct wpa_driver_nl80211_data *drv = bss->drv;
9031 struct nl_msg *msg;
9032 int ret;
9033 unsigned int i;
9034 struct nlattr *params;
9035 struct nl80211_pcl param;
9036 enum qca_iface_type iface_type;
9037
9038 if (!drv->get_pref_freq_list)
9039 return -1;
9040
9041 switch (if_type) {
9042 case WPA_IF_STATION:
9043 iface_type = QCA_IFACE_TYPE_STA;
9044 break;
9045 case WPA_IF_AP_BSS:
9046 iface_type = QCA_IFACE_TYPE_AP;
9047 break;
9048 case WPA_IF_P2P_GO:
9049 iface_type = QCA_IFACE_TYPE_P2P_GO;
9050 break;
9051 case WPA_IF_P2P_CLIENT:
9052 iface_type = QCA_IFACE_TYPE_P2P_CLIENT;
9053 break;
9054 case WPA_IF_IBSS:
9055 iface_type = QCA_IFACE_TYPE_IBSS;
9056 break;
9057 case WPA_IF_TDLS:
9058 iface_type = QCA_IFACE_TYPE_TDLS;
9059 break;
9060 default:
9061 return -1;
9062 }
9063
9064 param.num = *num;
9065 param.freq_list = freq_list;
9066
9067 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
9068 nla_put_u32(msg, NL80211_ATTR_IFINDEX, drv->ifindex) ||
9069 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
9070 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9071 QCA_NL80211_VENDOR_SUBCMD_GET_PREFERRED_FREQ_LIST) ||
9072 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
9073 nla_put_u32(msg,
9074 QCA_WLAN_VENDOR_ATTR_GET_PREFERRED_FREQ_LIST_IFACE_TYPE,
9075 iface_type)) {
9076 wpa_printf(MSG_ERROR,
9077 "%s: err in adding vendor_cmd and vendor_data",
9078 __func__);
9079 nlmsg_free(msg);
9080 return -1;
9081 }
9082 nla_nest_end(msg, params);
9083
9084 os_memset(freq_list, 0, *num * sizeof(freq_list[0]));
9085 ret = send_and_recv_msgs(drv, msg, preferred_freq_info_handler, &param);
9086 if (ret) {
9087 wpa_printf(MSG_ERROR,
9088 "%s: err in send_and_recv_msgs", __func__);
9089 return ret;
9090 }
9091
9092 *num = param.num;
9093
9094 for (i = 0; i < *num; i++) {
9095 wpa_printf(MSG_DEBUG, "nl80211: preferred_channel_list[%d]=%d",
9096 i, freq_list[i]);
9097 }
9098
9099 return 0;
9100}
9101
9102
9103static int nl80211_set_prob_oper_freq(void *priv, unsigned int freq)
9104{
9105 struct i802_bss *bss = priv;
9106 struct wpa_driver_nl80211_data *drv = bss->drv;
9107 struct nl_msg *msg;
9108 int ret;
9109 struct nlattr *params;
9110
9111 if (!drv->set_prob_oper_freq)
9112 return -1;
9113
9114 wpa_printf(MSG_DEBUG,
9115 "nl80211: Set P2P probable operating freq %u for ifindex %d",
9116 freq, bss->ifindex);
9117
9118 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
9119 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
9120 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9121 QCA_NL80211_VENDOR_SUBCMD_SET_PROBABLE_OPER_CHANNEL) ||
9122 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
9123 nla_put_u32(msg,
9124 QCA_WLAN_VENDOR_ATTR_PROBABLE_OPER_CHANNEL_IFACE_TYPE,
9125 QCA_IFACE_TYPE_P2P_CLIENT) ||
9126 nla_put_u32(msg,
9127 QCA_WLAN_VENDOR_ATTR_PROBABLE_OPER_CHANNEL_FREQ,
9128 freq)) {
9129 wpa_printf(MSG_ERROR,
9130 "%s: err in adding vendor_cmd and vendor_data",
9131 __func__);
9132 nlmsg_free(msg);
9133 return -1;
9134 }
9135 nla_nest_end(msg, params);
9136
9137 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9138 msg = NULL;
9139 if (ret) {
9140 wpa_printf(MSG_ERROR, "%s: err in send_and_recv_msgs",
9141 __func__);
9142 return ret;
9143 }
9144 nlmsg_free(msg);
9145 return 0;
9146}
9147
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07009148
9149static int nl80211_p2p_lo_start(void *priv, unsigned int freq,
9150 unsigned int period, unsigned int interval,
9151 unsigned int count, const u8 *device_types,
9152 size_t dev_types_len,
9153 const u8 *ies, size_t ies_len)
9154{
9155 struct i802_bss *bss = priv;
9156 struct wpa_driver_nl80211_data *drv = bss->drv;
9157 struct nl_msg *msg;
9158 struct nlattr *container;
9159 int ret;
9160
9161 wpa_printf(MSG_DEBUG,
9162 "nl80211: Start P2P Listen offload: freq=%u, period=%u, interval=%u, count=%u",
9163 freq, period, interval, count);
9164
9165 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_P2P_LISTEN_OFFLOAD))
9166 return -1;
9167
9168 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
9169 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
9170 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9171 QCA_NL80211_VENDOR_SUBCMD_P2P_LISTEN_OFFLOAD_START))
9172 goto fail;
9173
9174 container = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
9175 if (!container)
9176 goto fail;
9177
9178 if (nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_CHANNEL,
9179 freq) ||
9180 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_PERIOD,
9181 period) ||
9182 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_INTERVAL,
9183 interval) ||
9184 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_COUNT,
9185 count) ||
9186 nla_put(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_DEVICE_TYPES,
9187 dev_types_len, device_types) ||
9188 nla_put(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_VENDOR_IE,
9189 ies_len, ies))
9190 goto fail;
9191
9192 nla_nest_end(msg, container);
9193 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9194 msg = NULL;
9195 if (ret) {
9196 wpa_printf(MSG_DEBUG,
9197 "nl80211: Failed to send P2P Listen offload vendor command");
9198 goto fail;
9199 }
9200
9201 return 0;
9202
9203fail:
9204 nlmsg_free(msg);
9205 return -1;
9206}
9207
9208
9209static int nl80211_p2p_lo_stop(void *priv)
9210{
9211 struct i802_bss *bss = priv;
9212 struct wpa_driver_nl80211_data *drv = bss->drv;
9213 struct nl_msg *msg;
9214
9215 wpa_printf(MSG_DEBUG, "nl80211: Stop P2P Listen offload");
9216
9217 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_P2P_LISTEN_OFFLOAD))
9218 return -1;
9219
9220 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
9221 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
9222 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9223 QCA_NL80211_VENDOR_SUBCMD_P2P_LISTEN_OFFLOAD_STOP)) {
9224 nlmsg_free(msg);
9225 return -1;
9226 }
9227
9228 return send_and_recv_msgs(drv, msg, NULL, NULL);
9229}
9230
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009231#endif /* CONFIG_DRIVER_NL80211_QCA */
9232
9233
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009234static int nl80211_write_to_file(const char *name, unsigned int val)
9235{
9236 int fd, len;
9237 char tmp[128];
9238
9239 fd = open(name, O_RDWR);
9240 if (fd < 0) {
9241 wpa_printf(MSG_ERROR, "nl80211: Failed to open %s: %s",
9242 name, strerror(errno));
9243 return fd;
9244 }
9245
9246 len = os_snprintf(tmp, sizeof(tmp), "%u\n", val);
9247 len = write(fd, tmp, len);
9248 if (len < 0)
9249 wpa_printf(MSG_ERROR, "nl80211: Failed to write to %s: %s",
9250 name, strerror(errno));
9251 close(fd);
9252
9253 return 0;
9254}
9255
9256
9257static int nl80211_configure_data_frame_filters(void *priv, u32 filter_flags)
9258{
9259 struct i802_bss *bss = priv;
9260 char path[128];
9261 int ret;
9262
9263 wpa_printf(MSG_DEBUG, "nl80211: Data frame filter flags=0x%x",
9264 filter_flags);
9265
9266 /* Configure filtering of unicast frame encrypted using GTK */
9267 ret = os_snprintf(path, sizeof(path),
9268 "/proc/sys/net/ipv4/conf/%s/drop_unicast_in_l2_multicast",
9269 bss->ifname);
9270 if (os_snprintf_error(sizeof(path), ret))
9271 return -1;
9272
9273 ret = nl80211_write_to_file(path,
9274 !!(filter_flags &
9275 WPA_DATA_FRAME_FILTER_FLAG_GTK));
9276 if (ret) {
9277 wpa_printf(MSG_ERROR,
9278 "nl80211: Failed to set IPv4 unicast in multicast filter");
9279 return ret;
9280 }
9281
9282 os_snprintf(path, sizeof(path),
9283 "/proc/sys/net/ipv6/conf/%s/drop_unicast_in_l2_multicast",
9284 bss->ifname);
9285 ret = nl80211_write_to_file(path,
9286 !!(filter_flags &
9287 WPA_DATA_FRAME_FILTER_FLAG_GTK));
9288
9289 if (ret) {
9290 wpa_printf(MSG_ERROR,
9291 "nl80211: Failed to set IPv6 unicast in multicast filter");
9292 return ret;
9293 }
9294
9295 /* Configure filtering of unicast frame encrypted using GTK */
9296 os_snprintf(path, sizeof(path),
9297 "/proc/sys/net/ipv4/conf/%s/drop_gratuitous_arp",
9298 bss->ifname);
9299 ret = nl80211_write_to_file(path,
9300 !!(filter_flags &
9301 WPA_DATA_FRAME_FILTER_FLAG_ARP));
9302 if (ret) {
9303 wpa_printf(MSG_ERROR,
9304 "nl80211: Failed set gratuitous ARP filter");
9305 return ret;
9306 }
9307
9308 /* Configure filtering of IPv6 NA frames */
9309 os_snprintf(path, sizeof(path),
9310 "/proc/sys/net/ipv6/conf/%s/drop_unsolicited_na",
9311 bss->ifname);
9312 ret = nl80211_write_to_file(path,
9313 !!(filter_flags &
9314 WPA_DATA_FRAME_FILTER_FLAG_NA));
9315 if (ret) {
9316 wpa_printf(MSG_ERROR,
9317 "nl80211: Failed to set unsolicited NA filter");
9318 return ret;
9319 }
9320
9321 return 0;
9322}
9323
9324
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07009325static int nl80211_get_ext_capab(void *priv, enum wpa_driver_if_type type,
9326 const u8 **ext_capa, const u8 **ext_capa_mask,
9327 unsigned int *ext_capa_len)
9328{
9329 struct i802_bss *bss = priv;
9330 struct wpa_driver_nl80211_data *drv = bss->drv;
9331 enum nl80211_iftype nlmode;
9332 unsigned int i;
9333
9334 if (!ext_capa || !ext_capa_mask || !ext_capa_len)
9335 return -1;
9336
9337 nlmode = wpa_driver_nl80211_if_type(type);
9338
9339 /* By default, use the per-radio values */
9340 *ext_capa = drv->extended_capa;
9341 *ext_capa_mask = drv->extended_capa_mask;
9342 *ext_capa_len = drv->extended_capa_len;
9343
9344 /* Replace the default value if a per-interface type value exists */
9345 for (i = 0; i < drv->num_iface_ext_capa; i++) {
9346 if (nlmode == drv->iface_ext_capa[i].iftype) {
9347 *ext_capa = drv->iface_ext_capa[i].ext_capa;
9348 *ext_capa_mask = drv->iface_ext_capa[i].ext_capa_mask;
9349 *ext_capa_len = drv->iface_ext_capa[i].ext_capa_len;
9350 break;
9351 }
9352 }
9353
9354 return 0;
9355}
9356
9357
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009358const struct wpa_driver_ops wpa_driver_nl80211_ops = {
9359 .name = "nl80211",
9360 .desc = "Linux nl80211/cfg80211",
9361 .get_bssid = wpa_driver_nl80211_get_bssid,
9362 .get_ssid = wpa_driver_nl80211_get_ssid,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009363 .set_key = driver_nl80211_set_key,
9364 .scan2 = driver_nl80211_scan2,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009365 .sched_scan = wpa_driver_nl80211_sched_scan,
9366 .stop_sched_scan = wpa_driver_nl80211_stop_sched_scan,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009367 .get_scan_results2 = wpa_driver_nl80211_get_scan_results,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009368 .abort_scan = wpa_driver_nl80211_abort_scan,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009369 .deauthenticate = driver_nl80211_deauthenticate,
9370 .authenticate = driver_nl80211_authenticate,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009371 .associate = wpa_driver_nl80211_associate,
9372 .global_init = nl80211_global_init,
9373 .global_deinit = nl80211_global_deinit,
9374 .init2 = wpa_driver_nl80211_init,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009375 .deinit = driver_nl80211_deinit,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009376 .get_capa = wpa_driver_nl80211_get_capa,
9377 .set_operstate = wpa_driver_nl80211_set_operstate,
9378 .set_supp_port = wpa_driver_nl80211_set_supp_port,
9379 .set_country = wpa_driver_nl80211_set_country,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009380 .get_country = wpa_driver_nl80211_get_country,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009381 .set_ap = wpa_driver_nl80211_set_ap,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07009382 .set_acl = wpa_driver_nl80211_set_acl,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009383 .if_add = wpa_driver_nl80211_if_add,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009384 .if_remove = driver_nl80211_if_remove,
9385 .send_mlme = driver_nl80211_send_mlme,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009386 .get_hw_feature_data = nl80211_get_hw_feature_data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009387 .sta_add = wpa_driver_nl80211_sta_add,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009388 .sta_remove = driver_nl80211_sta_remove,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009389 .hapd_send_eapol = wpa_driver_nl80211_hapd_send_eapol,
9390 .sta_set_flags = wpa_driver_nl80211_sta_set_flags,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009391 .hapd_init = i802_init,
9392 .hapd_deinit = i802_deinit,
Jouni Malinen75ecf522011-06-27 15:19:46 -07009393 .set_wds_sta = i802_set_wds_sta,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009394 .get_seqnum = i802_get_seqnum,
9395 .flush = i802_flush,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009396 .get_inact_sec = i802_get_inact_sec,
9397 .sta_clear_stats = i802_sta_clear_stats,
9398 .set_rts = i802_set_rts,
9399 .set_frag = i802_set_frag,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009400 .set_tx_queue_params = i802_set_tx_queue_params,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009401 .set_sta_vlan = driver_nl80211_set_sta_vlan,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009402 .sta_deauth = i802_sta_deauth,
9403 .sta_disassoc = i802_sta_disassoc,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009404 .read_sta_data = driver_nl80211_read_sta_data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009405 .set_freq = i802_set_freq,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009406 .send_action = driver_nl80211_send_action,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009407 .send_action_cancel_wait = wpa_driver_nl80211_send_action_cancel_wait,
9408 .remain_on_channel = wpa_driver_nl80211_remain_on_channel,
9409 .cancel_remain_on_channel =
9410 wpa_driver_nl80211_cancel_remain_on_channel,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009411 .probe_req_report = driver_nl80211_probe_req_report,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009412 .deinit_ap = wpa_driver_nl80211_deinit_ap,
Dmitry Shmidt04949592012-07-19 12:16:46 -07009413 .deinit_p2p_cli = wpa_driver_nl80211_deinit_p2p_cli,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009414 .resume = wpa_driver_nl80211_resume,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009415 .signal_monitor = nl80211_signal_monitor,
9416 .signal_poll = nl80211_signal_poll,
9417 .send_frame = nl80211_send_frame,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009418 .set_param = nl80211_set_param,
9419 .get_radio_name = nl80211_get_radio_name,
Jouni Malinen75ecf522011-06-27 15:19:46 -07009420 .add_pmkid = nl80211_add_pmkid,
9421 .remove_pmkid = nl80211_remove_pmkid,
9422 .flush_pmkid = nl80211_flush_pmkid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009423 .set_rekey_info = nl80211_set_rekey_info,
9424 .poll_client = nl80211_poll_client,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009425 .set_p2p_powersave = nl80211_set_p2p_powersave,
Dmitry Shmidtea69e842013-05-13 14:52:28 -07009426 .start_dfs_cac = nl80211_start_radar_detection,
9427 .stop_ap = wpa_driver_nl80211_stop_ap,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009428#ifdef CONFIG_TDLS
9429 .send_tdls_mgmt = nl80211_send_tdls_mgmt,
9430 .tdls_oper = nl80211_tdls_oper,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009431 .tdls_enable_channel_switch = nl80211_tdls_enable_channel_switch,
9432 .tdls_disable_channel_switch = nl80211_tdls_disable_channel_switch,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009433#endif /* CONFIG_TDLS */
Dmitry Shmidt700a1372013-03-15 14:14:44 -07009434 .update_ft_ies = wpa_driver_nl80211_update_ft_ies,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009435 .get_mac_addr = wpa_driver_nl80211_get_macaddr,
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07009436 .get_survey = wpa_driver_nl80211_get_survey,
Dmitry Shmidt56052862013-10-04 10:23:25 -07009437 .status = wpa_driver_nl80211_status,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08009438 .switch_channel = nl80211_switch_channel,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009439#ifdef ANDROID_P2P
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07009440 .set_noa = wpa_driver_set_p2p_noa,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009441 .get_noa = wpa_driver_get_p2p_noa,
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07009442 .set_ap_wps_ie = wpa_driver_set_ap_wps_p2p_ie,
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08009443#endif /* ANDROID_P2P */
Dmitry Shmidt738a26e2011-07-07 14:22:14 -07009444#ifdef ANDROID
Dmitry Shmidt41712582015-06-29 11:02:15 -07009445#ifndef ANDROID_LIB_STUB
Dmitry Shmidt738a26e2011-07-07 14:22:14 -07009446 .driver_cmd = wpa_driver_nl80211_driver_cmd,
Dmitry Shmidt41712582015-06-29 11:02:15 -07009447#endif /* !ANDROID_LIB_STUB */
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08009448#endif /* ANDROID */
Dmitry Shmidta38abf92014-03-06 13:38:44 -08009449 .vendor_cmd = nl80211_vendor_cmd,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08009450 .set_qos_map = nl80211_set_qos_map,
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07009451 .set_wowlan = nl80211_set_wowlan,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07009452 .set_mac_addr = nl80211_set_mac_addr,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009453#ifdef CONFIG_MESH
9454 .init_mesh = wpa_driver_nl80211_init_mesh,
9455 .join_mesh = wpa_driver_nl80211_join_mesh,
9456 .leave_mesh = wpa_driver_nl80211_leave_mesh,
9457#endif /* CONFIG_MESH */
9458 .br_add_ip_neigh = wpa_driver_br_add_ip_neigh,
9459 .br_delete_ip_neigh = wpa_driver_br_delete_ip_neigh,
9460 .br_port_set_attr = wpa_driver_br_port_set_attr,
9461 .br_set_net_param = wpa_driver_br_set_net_param,
9462 .add_tx_ts = nl80211_add_ts,
9463 .del_tx_ts = nl80211_del_ts,
Dmitry Shmidte4663042016-04-04 10:07:49 -07009464 .get_ifindex = nl80211_get_ifindex,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009465#ifdef CONFIG_DRIVER_NL80211_QCA
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07009466 .roaming = nl80211_roaming,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009467 .do_acs = wpa_driver_do_acs,
Ravi Joshie6ccb162015-07-16 17:45:41 -07009468 .set_band = nl80211_set_band,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009469 .get_pref_freq_list = nl80211_get_pref_freq_list,
9470 .set_prob_oper_freq = nl80211_set_prob_oper_freq,
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07009471 .p2p_lo_start = nl80211_p2p_lo_start,
9472 .p2p_lo_stop = nl80211_p2p_lo_stop,
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07009473 .set_default_scan_ies = nl80211_set_default_scan_ies,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009474#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009475 .configure_data_frame_filters = nl80211_configure_data_frame_filters,
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07009476 .get_ext_capab = nl80211_get_ext_capab,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009477};