blob: d681ea637a8b04998c6b3bce16a73b07b5993684 [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
90#define genl_ctrl_resolve android_genl_ctrl_resolve
Dmitry Shmidt54605472013-11-08 11:10:19 -080091#endif /* ANDROID */
92
93
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080094static struct nl_handle * nl_create_handle(struct nl_cb *cb, const char *dbg)
95{
96 struct nl_handle *handle;
97
98 handle = nl80211_handle_alloc(cb);
99 if (handle == NULL) {
100 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
101 "callbacks (%s)", dbg);
102 return NULL;
103 }
104
105 if (genl_connect(handle)) {
106 wpa_printf(MSG_ERROR, "nl80211: Failed to connect to generic "
107 "netlink (%s)", dbg);
108 nl80211_handle_destroy(handle);
109 return NULL;
110 }
111
112 return handle;
113}
114
115
116static void nl_destroy_handles(struct nl_handle **handle)
117{
118 if (*handle == NULL)
119 return;
120 nl80211_handle_destroy(*handle);
121 *handle = NULL;
122}
123
124
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700125#if __WORDSIZE == 64
126#define ELOOP_SOCKET_INVALID (intptr_t) 0x8888888888888889ULL
127#else
128#define ELOOP_SOCKET_INVALID (intptr_t) 0x88888889ULL
129#endif
130
131static void nl80211_register_eloop_read(struct nl_handle **handle,
132 eloop_sock_handler handler,
133 void *eloop_data)
134{
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800135#ifdef CONFIG_LIBNL20
136 /*
137 * libnl uses a pretty small buffer (32 kB that gets converted to 64 kB)
138 * by default. It is possible to hit that limit in some cases where
139 * operations are blocked, e.g., with a burst of Deauthentication frames
140 * to hostapd and STA entry deletion. Try to increase the buffer to make
141 * this less likely to occur.
142 */
143 if (nl_socket_set_buffer_size(*handle, 262144, 0) < 0) {
144 wpa_printf(MSG_DEBUG,
145 "nl80211: Could not set nl_socket RX buffer size: %s",
146 strerror(errno));
147 /* continue anyway with the default (smaller) buffer */
148 }
149#endif /* CONFIG_LIBNL20 */
150
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700151 nl_socket_set_nonblocking(*handle);
152 eloop_register_read_sock(nl_socket_get_fd(*handle), handler,
153 eloop_data, *handle);
154 *handle = (void *) (((intptr_t) *handle) ^ ELOOP_SOCKET_INVALID);
155}
156
157
158static void nl80211_destroy_eloop_handle(struct nl_handle **handle)
159{
160 *handle = (void *) (((intptr_t) *handle) ^ ELOOP_SOCKET_INVALID);
161 eloop_unregister_read_sock(nl_socket_get_fd(*handle));
162 nl_destroy_handles(handle);
163}
164
165
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800166static void nl80211_global_deinit(void *priv);
Dmitry 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,
179 int no_cck, int no_ack, int offchanok);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800180static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss,
181 int report);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700182
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700183static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
184static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
185static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
Dmitry Shmidt738a26e2011-07-07 14:22:14 -0700186
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700187static int nl80211_set_channel(struct i802_bss *bss,
188 struct hostapd_freq_params *freq, int set_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700189static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
190 int ifindex, int disabled);
191
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800192static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv,
193 int reset_mode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800194
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700195static int i802_set_iface_flags(struct i802_bss *bss, int up);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800196static int nl80211_set_param(void *priv, const char *param);
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700197
198
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800199/* Converts nl80211_chan_width to a common format */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800200enum chan_width convert2width(int width)
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800201{
202 switch (width) {
203 case NL80211_CHAN_WIDTH_20_NOHT:
204 return CHAN_WIDTH_20_NOHT;
205 case NL80211_CHAN_WIDTH_20:
206 return CHAN_WIDTH_20;
207 case NL80211_CHAN_WIDTH_40:
208 return CHAN_WIDTH_40;
209 case NL80211_CHAN_WIDTH_80:
210 return CHAN_WIDTH_80;
211 case NL80211_CHAN_WIDTH_80P80:
212 return CHAN_WIDTH_80P80;
213 case NL80211_CHAN_WIDTH_160:
214 return CHAN_WIDTH_160;
215 }
216 return CHAN_WIDTH_UNKNOWN;
217}
218
219
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800220int is_ap_interface(enum nl80211_iftype nlmode)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800221{
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700222 return nlmode == NL80211_IFTYPE_AP ||
223 nlmode == NL80211_IFTYPE_P2P_GO;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800224}
225
226
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800227int is_sta_interface(enum nl80211_iftype nlmode)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800228{
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700229 return nlmode == NL80211_IFTYPE_STATION ||
230 nlmode == NL80211_IFTYPE_P2P_CLIENT;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800231}
232
233
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700234static int is_p2p_net_interface(enum nl80211_iftype nlmode)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800235{
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700236 return nlmode == NL80211_IFTYPE_P2P_CLIENT ||
237 nlmode == NL80211_IFTYPE_P2P_GO;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800238}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700239
240
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800241struct i802_bss * get_bss_ifindex(struct wpa_driver_nl80211_data *drv,
242 int ifindex)
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -0700243{
244 struct i802_bss *bss;
245
246 for (bss = drv->first_bss; bss; bss = bss->next) {
247 if (bss->ifindex == ifindex)
248 return bss;
249 }
250
251 return NULL;
252}
253
254
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800255static int is_mesh_interface(enum nl80211_iftype nlmode)
256{
257 return nlmode == NL80211_IFTYPE_MESH_POINT;
258}
259
260
261void nl80211_mark_disconnected(struct wpa_driver_nl80211_data *drv)
Dmitry Shmidt8bae4132013-06-06 11:25:10 -0700262{
263 if (drv->associated)
264 os_memcpy(drv->prev_bssid, drv->bssid, ETH_ALEN);
265 drv->associated = 0;
266 os_memset(drv->bssid, 0, ETH_ALEN);
267}
268
269
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700270/* nl80211 code */
271static int ack_handler(struct nl_msg *msg, void *arg)
272{
273 int *err = arg;
274 *err = 0;
275 return NL_STOP;
276}
277
278static int finish_handler(struct nl_msg *msg, void *arg)
279{
280 int *ret = arg;
281 *ret = 0;
282 return NL_SKIP;
283}
284
285static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
286 void *arg)
287{
288 int *ret = arg;
289 *ret = err->error;
290 return NL_SKIP;
291}
292
293
294static int no_seq_check(struct nl_msg *msg, void *arg)
295{
296 return NL_OK;
297}
298
299
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800300static void nl80211_nlmsg_clear(struct nl_msg *msg)
301{
302 /*
303 * Clear nlmsg data, e.g., to make sure key material is not left in
304 * heap memory for unnecessarily long time.
305 */
306 if (msg) {
307 struct nlmsghdr *hdr = nlmsg_hdr(msg);
308 void *data = nlmsg_data(hdr);
309 /*
310 * This would use nlmsg_datalen() or the older nlmsg_len() if
311 * only libnl were to maintain a stable API.. Neither will work
312 * with all released versions, so just calculate the length
313 * here.
314 */
315 int len = hdr->nlmsg_len - NLMSG_HDRLEN;
316
317 os_memset(data, 0, len);
318 }
319}
320
321
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800322static int send_and_recv(struct nl80211_global *global,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700323 struct nl_handle *nl_handle, struct nl_msg *msg,
324 int (*valid_handler)(struct nl_msg *, void *),
325 void *valid_data)
326{
327 struct nl_cb *cb;
328 int err = -ENOMEM;
329
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800330 if (!msg)
331 return -ENOMEM;
332
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800333 cb = nl_cb_clone(global->nl_cb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700334 if (!cb)
335 goto out;
336
337 err = nl_send_auto_complete(nl_handle, msg);
338 if (err < 0)
339 goto out;
340
341 err = 1;
342
343 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
344 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
345 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
346
347 if (valid_handler)
348 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM,
349 valid_handler, valid_data);
350
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700351 while (err > 0) {
352 int res = nl_recvmsgs(nl_handle, cb);
Dmitry Shmidt71757432014-06-02 13:50:35 -0700353 if (res < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700354 wpa_printf(MSG_INFO,
355 "nl80211: %s->nl_recvmsgs failed: %d",
356 __func__, res);
357 }
358 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700359 out:
360 nl_cb_put(cb);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800361 if (!valid_handler && valid_data == (void *) -1)
362 nl80211_nlmsg_clear(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700363 nlmsg_free(msg);
364 return err;
365}
366
367
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800368int send_and_recv_msgs(struct wpa_driver_nl80211_data *drv,
369 struct nl_msg *msg,
370 int (*valid_handler)(struct nl_msg *, void *),
371 void *valid_data)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700372{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800373 return send_and_recv(drv->global, drv->global->nl, msg,
374 valid_handler, valid_data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700375}
376
377
378struct family_data {
379 const char *group;
380 int id;
381};
382
383
384static int family_handler(struct nl_msg *msg, void *arg)
385{
386 struct family_data *res = arg;
387 struct nlattr *tb[CTRL_ATTR_MAX + 1];
388 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
389 struct nlattr *mcgrp;
390 int i;
391
392 nla_parse(tb, CTRL_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
393 genlmsg_attrlen(gnlh, 0), NULL);
394 if (!tb[CTRL_ATTR_MCAST_GROUPS])
395 return NL_SKIP;
396
397 nla_for_each_nested(mcgrp, tb[CTRL_ATTR_MCAST_GROUPS], i) {
398 struct nlattr *tb2[CTRL_ATTR_MCAST_GRP_MAX + 1];
399 nla_parse(tb2, CTRL_ATTR_MCAST_GRP_MAX, nla_data(mcgrp),
400 nla_len(mcgrp), NULL);
401 if (!tb2[CTRL_ATTR_MCAST_GRP_NAME] ||
402 !tb2[CTRL_ATTR_MCAST_GRP_ID] ||
403 os_strncmp(nla_data(tb2[CTRL_ATTR_MCAST_GRP_NAME]),
404 res->group,
405 nla_len(tb2[CTRL_ATTR_MCAST_GRP_NAME])) != 0)
406 continue;
407 res->id = nla_get_u32(tb2[CTRL_ATTR_MCAST_GRP_ID]);
408 break;
409 };
410
411 return NL_SKIP;
412}
413
414
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800415static int nl_get_multicast_id(struct nl80211_global *global,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700416 const char *family, const char *group)
417{
418 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800419 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700420 struct family_data res = { group, -ENOENT };
421
422 msg = nlmsg_alloc();
423 if (!msg)
424 return -ENOMEM;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800425 if (!genlmsg_put(msg, 0, 0, genl_ctrl_resolve(global->nl, "nlctrl"),
426 0, 0, CTRL_CMD_GETFAMILY, 0) ||
427 nla_put_string(msg, CTRL_ATTR_FAMILY_NAME, family)) {
428 nlmsg_free(msg);
429 return -1;
430 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700431
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800432 ret = send_and_recv(global, global->nl, msg, family_handler, &res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700433 if (ret == 0)
434 ret = res.id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700435 return ret;
436}
437
438
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800439void * nl80211_cmd(struct wpa_driver_nl80211_data *drv,
440 struct nl_msg *msg, int flags, uint8_t cmd)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800441{
442 return genlmsg_put(msg, 0, 0, drv->global->nl80211_id,
443 0, flags, cmd, 0);
444}
445
446
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800447static int nl80211_set_iface_id(struct nl_msg *msg, struct i802_bss *bss)
448{
449 if (bss->wdev_id_set)
450 return nla_put_u64(msg, NL80211_ATTR_WDEV, bss->wdev_id);
451 return nla_put_u32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
452}
453
454
455struct nl_msg * nl80211_cmd_msg(struct i802_bss *bss, int flags, uint8_t cmd)
456{
457 struct nl_msg *msg;
458
459 msg = nlmsg_alloc();
460 if (!msg)
461 return NULL;
462
463 if (!nl80211_cmd(bss->drv, msg, flags, cmd) ||
464 nl80211_set_iface_id(msg, bss) < 0) {
465 nlmsg_free(msg);
466 return NULL;
467 }
468
469 return msg;
470}
471
472
473static struct nl_msg *
474nl80211_ifindex_msg(struct wpa_driver_nl80211_data *drv, int ifindex,
475 int flags, uint8_t cmd)
476{
477 struct nl_msg *msg;
478
479 msg = nlmsg_alloc();
480 if (!msg)
481 return NULL;
482
483 if (!nl80211_cmd(drv, msg, flags, cmd) ||
484 nla_put_u32(msg, NL80211_ATTR_IFINDEX, ifindex)) {
485 nlmsg_free(msg);
486 return NULL;
487 }
488
489 return msg;
490}
491
492
493struct nl_msg * nl80211_drv_msg(struct wpa_driver_nl80211_data *drv, int flags,
494 uint8_t cmd)
495{
496 return nl80211_ifindex_msg(drv, drv->ifindex, flags, cmd);
497}
498
499
500struct nl_msg * nl80211_bss_msg(struct i802_bss *bss, int flags, uint8_t cmd)
501{
502 return nl80211_ifindex_msg(bss->drv, bss->ifindex, flags, cmd);
503}
504
505
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800506struct wiphy_idx_data {
507 int wiphy_idx;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700508 enum nl80211_iftype nlmode;
509 u8 *macaddr;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800510};
511
512
513static int netdev_info_handler(struct nl_msg *msg, void *arg)
514{
515 struct nlattr *tb[NL80211_ATTR_MAX + 1];
516 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
517 struct wiphy_idx_data *info = arg;
518
519 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
520 genlmsg_attrlen(gnlh, 0), NULL);
521
522 if (tb[NL80211_ATTR_WIPHY])
523 info->wiphy_idx = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
524
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700525 if (tb[NL80211_ATTR_IFTYPE])
526 info->nlmode = nla_get_u32(tb[NL80211_ATTR_IFTYPE]);
527
528 if (tb[NL80211_ATTR_MAC] && info->macaddr)
529 os_memcpy(info->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
530 ETH_ALEN);
531
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800532 return NL_SKIP;
533}
534
535
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800536int nl80211_get_wiphy_index(struct i802_bss *bss)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800537{
538 struct nl_msg *msg;
539 struct wiphy_idx_data data = {
540 .wiphy_idx = -1,
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700541 .macaddr = NULL,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800542 };
543
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800544 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_GET_INTERFACE)))
545 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800546
547 if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0)
548 return data.wiphy_idx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800549 return -1;
550}
551
552
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700553static enum nl80211_iftype nl80211_get_ifmode(struct i802_bss *bss)
554{
555 struct nl_msg *msg;
556 struct wiphy_idx_data data = {
557 .nlmode = NL80211_IFTYPE_UNSPECIFIED,
558 .macaddr = NULL,
559 };
560
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800561 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_GET_INTERFACE)))
562 return NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700563
564 if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0)
565 return data.nlmode;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700566 return NL80211_IFTYPE_UNSPECIFIED;
567}
568
569
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700570static int nl80211_get_macaddr(struct i802_bss *bss)
571{
572 struct nl_msg *msg;
573 struct wiphy_idx_data data = {
574 .macaddr = bss->addr,
575 };
576
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800577 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_GET_INTERFACE)))
578 return -1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700579
580 return send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data);
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700581}
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700582
583
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800584static int nl80211_register_beacons(struct wpa_driver_nl80211_data *drv,
585 struct nl80211_wiphy_data *w)
586{
587 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800588 int ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800589
590 msg = nlmsg_alloc();
591 if (!msg)
592 return -1;
593
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800594 if (!nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_BEACONS) ||
595 nla_put_u32(msg, NL80211_ATTR_WIPHY, w->wiphy_idx)) {
596 nlmsg_free(msg);
597 return -1;
598 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800599
600 ret = send_and_recv(drv->global, w->nl_beacons, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800601 if (ret) {
602 wpa_printf(MSG_DEBUG, "nl80211: Register beacons command "
603 "failed: ret=%d (%s)",
604 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800605 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800606 return ret;
607}
608
609
610static void nl80211_recv_beacons(int sock, void *eloop_ctx, void *handle)
611{
612 struct nl80211_wiphy_data *w = eloop_ctx;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700613 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800614
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800615 wpa_printf(MSG_EXCESSIVE, "nl80211: Beacon event message available");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800616
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700617 res = nl_recvmsgs(handle, w->nl_cb);
Dmitry Shmidt71757432014-06-02 13:50:35 -0700618 if (res < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700619 wpa_printf(MSG_INFO, "nl80211: %s->nl_recvmsgs failed: %d",
620 __func__, res);
621 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800622}
623
624
625static int process_beacon_event(struct nl_msg *msg, void *arg)
626{
627 struct nl80211_wiphy_data *w = arg;
628 struct wpa_driver_nl80211_data *drv;
629 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
630 struct nlattr *tb[NL80211_ATTR_MAX + 1];
631 union wpa_event_data event;
632
633 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
634 genlmsg_attrlen(gnlh, 0), NULL);
635
636 if (gnlh->cmd != NL80211_CMD_FRAME) {
637 wpa_printf(MSG_DEBUG, "nl80211: Unexpected beacon event? (%d)",
638 gnlh->cmd);
639 return NL_SKIP;
640 }
641
642 if (!tb[NL80211_ATTR_FRAME])
643 return NL_SKIP;
644
645 dl_list_for_each(drv, &w->drvs, struct wpa_driver_nl80211_data,
646 wiphy_list) {
647 os_memset(&event, 0, sizeof(event));
648 event.rx_mgmt.frame = nla_data(tb[NL80211_ATTR_FRAME]);
649 event.rx_mgmt.frame_len = nla_len(tb[NL80211_ATTR_FRAME]);
650 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
651 }
652
653 return NL_SKIP;
654}
655
656
657static struct nl80211_wiphy_data *
658nl80211_get_wiphy_data_ap(struct i802_bss *bss)
659{
660 static DEFINE_DL_LIST(nl80211_wiphys);
661 struct nl80211_wiphy_data *w;
662 int wiphy_idx, found = 0;
663 struct i802_bss *tmp_bss;
664
665 if (bss->wiphy_data != NULL)
666 return bss->wiphy_data;
667
668 wiphy_idx = nl80211_get_wiphy_index(bss);
669
670 dl_list_for_each(w, &nl80211_wiphys, struct nl80211_wiphy_data, list) {
671 if (w->wiphy_idx == wiphy_idx)
672 goto add;
673 }
674
675 /* alloc new one */
676 w = os_zalloc(sizeof(*w));
677 if (w == NULL)
678 return NULL;
679 w->wiphy_idx = wiphy_idx;
680 dl_list_init(&w->bsss);
681 dl_list_init(&w->drvs);
682
683 w->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
684 if (!w->nl_cb) {
685 os_free(w);
686 return NULL;
687 }
688 nl_cb_set(w->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL);
689 nl_cb_set(w->nl_cb, NL_CB_VALID, NL_CB_CUSTOM, process_beacon_event,
690 w);
691
692 w->nl_beacons = nl_create_handle(bss->drv->global->nl_cb,
693 "wiphy beacons");
694 if (w->nl_beacons == NULL) {
695 os_free(w);
696 return NULL;
697 }
698
699 if (nl80211_register_beacons(bss->drv, w)) {
700 nl_destroy_handles(&w->nl_beacons);
701 os_free(w);
702 return NULL;
703 }
704
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700705 nl80211_register_eloop_read(&w->nl_beacons, nl80211_recv_beacons, w);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800706
707 dl_list_add(&nl80211_wiphys, &w->list);
708
709add:
710 /* drv entry for this bss already there? */
711 dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
712 if (tmp_bss->drv == bss->drv) {
713 found = 1;
714 break;
715 }
716 }
717 /* if not add it */
718 if (!found)
719 dl_list_add(&w->drvs, &bss->drv->wiphy_list);
720
721 dl_list_add(&w->bsss, &bss->wiphy_list);
722 bss->wiphy_data = w;
723 return w;
724}
725
726
727static void nl80211_put_wiphy_data_ap(struct i802_bss *bss)
728{
729 struct nl80211_wiphy_data *w = bss->wiphy_data;
730 struct i802_bss *tmp_bss;
731 int found = 0;
732
733 if (w == NULL)
734 return;
735 bss->wiphy_data = NULL;
736 dl_list_del(&bss->wiphy_list);
737
738 /* still any for this drv present? */
739 dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
740 if (tmp_bss->drv == bss->drv) {
741 found = 1;
742 break;
743 }
744 }
745 /* if not remove it */
746 if (!found)
747 dl_list_del(&bss->drv->wiphy_list);
748
749 if (!dl_list_empty(&w->bsss))
750 return;
751
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700752 nl80211_destroy_eloop_handle(&w->nl_beacons);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800753
754 nl_cb_put(w->nl_cb);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800755 dl_list_del(&w->list);
756 os_free(w);
757}
758
759
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700760static int wpa_driver_nl80211_get_bssid(void *priv, u8 *bssid)
761{
762 struct i802_bss *bss = priv;
763 struct wpa_driver_nl80211_data *drv = bss->drv;
764 if (!drv->associated)
765 return -1;
766 os_memcpy(bssid, drv->bssid, ETH_ALEN);
767 return 0;
768}
769
770
771static int wpa_driver_nl80211_get_ssid(void *priv, u8 *ssid)
772{
773 struct i802_bss *bss = priv;
774 struct wpa_driver_nl80211_data *drv = bss->drv;
775 if (!drv->associated)
776 return -1;
777 os_memcpy(ssid, drv->ssid, drv->ssid_len);
778 return drv->ssid_len;
779}
780
781
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800782static void wpa_driver_nl80211_event_newlink(
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800783 struct wpa_driver_nl80211_data *drv, const char *ifname)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700784{
785 union wpa_event_data event;
786
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800787 if (os_strcmp(drv->first_bss->ifname, ifname) == 0) {
788 if (if_nametoindex(drv->first_bss->ifname) == 0) {
789 wpa_printf(MSG_DEBUG, "nl80211: Interface %s does not exist - ignore RTM_NEWLINK",
790 drv->first_bss->ifname);
791 return;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700792 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800793 if (!drv->if_removed)
794 return;
795 wpa_printf(MSG_DEBUG, "nl80211: Mark if_removed=0 for %s based on RTM_NEWLINK event",
796 drv->first_bss->ifname);
797 drv->if_removed = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700798 }
799
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800800 os_memset(&event, 0, sizeof(event));
801 os_strlcpy(event.interface_status.ifname, ifname,
802 sizeof(event.interface_status.ifname));
803 event.interface_status.ievent = EVENT_INTERFACE_ADDED;
804 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
805}
806
807
808static void wpa_driver_nl80211_event_dellink(
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800809 struct wpa_driver_nl80211_data *drv, const char *ifname)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800810{
811 union wpa_event_data event;
812
813 if (os_strcmp(drv->first_bss->ifname, ifname) == 0) {
814 if (drv->if_removed) {
815 wpa_printf(MSG_DEBUG, "nl80211: if_removed already set - ignore RTM_DELLINK event for %s",
816 ifname);
817 return;
818 }
819 wpa_printf(MSG_DEBUG, "RTM_DELLINK: Interface '%s' removed - mark if_removed=1",
820 ifname);
821 drv->if_removed = 1;
822 } else {
823 wpa_printf(MSG_DEBUG, "RTM_DELLINK: Interface '%s' removed",
824 ifname);
825 }
826
827 os_memset(&event, 0, sizeof(event));
828 os_strlcpy(event.interface_status.ifname, ifname,
829 sizeof(event.interface_status.ifname));
830 event.interface_status.ievent = EVENT_INTERFACE_REMOVED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700831 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
832}
833
834
835static int wpa_driver_nl80211_own_ifname(struct wpa_driver_nl80211_data *drv,
836 u8 *buf, size_t len)
837{
838 int attrlen, rta_len;
839 struct rtattr *attr;
840
841 attrlen = len;
842 attr = (struct rtattr *) buf;
843
844 rta_len = RTA_ALIGN(sizeof(struct rtattr));
845 while (RTA_OK(attr, attrlen)) {
846 if (attr->rta_type == IFLA_IFNAME) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800847 if (os_strcmp(((char *) attr) + rta_len,
848 drv->first_bss->ifname) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700849 return 1;
850 else
851 break;
852 }
853 attr = RTA_NEXT(attr, attrlen);
854 }
855
856 return 0;
857}
858
859
860static int wpa_driver_nl80211_own_ifindex(struct wpa_driver_nl80211_data *drv,
861 int ifindex, u8 *buf, size_t len)
862{
863 if (drv->ifindex == ifindex)
864 return 1;
865
866 if (drv->if_removed && wpa_driver_nl80211_own_ifname(drv, buf, len)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700867 wpa_printf(MSG_DEBUG, "nl80211: Update ifindex for a removed "
868 "interface");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800869 wpa_driver_nl80211_finish_drv_init(drv, NULL, 0, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700870 return 1;
871 }
872
873 return 0;
874}
875
876
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800877static struct wpa_driver_nl80211_data *
878nl80211_find_drv(struct nl80211_global *global, int idx, u8 *buf, size_t len)
879{
880 struct wpa_driver_nl80211_data *drv;
881 dl_list_for_each(drv, &global->interfaces,
882 struct wpa_driver_nl80211_data, list) {
883 if (wpa_driver_nl80211_own_ifindex(drv, idx, buf, len) ||
884 have_ifidx(drv, idx))
885 return drv;
886 }
887 return NULL;
888}
889
890
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700891static void wpa_driver_nl80211_event_rtm_newlink(void *ctx,
892 struct ifinfomsg *ifi,
893 u8 *buf, size_t len)
894{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800895 struct nl80211_global *global = ctx;
896 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800897 int attrlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700898 struct rtattr *attr;
899 u32 brid = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800900 char namebuf[IFNAMSIZ];
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800901 char ifname[IFNAMSIZ + 1];
902 char extra[100], *pos, *end;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700903
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800904 drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
905 if (!drv) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800906 wpa_printf(MSG_DEBUG, "nl80211: Ignore RTM_NEWLINK event for foreign ifindex %d",
907 ifi->ifi_index);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700908 return;
909 }
910
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800911 extra[0] = '\0';
912 pos = extra;
913 end = pos + sizeof(extra);
914 ifname[0] = '\0';
915
916 attrlen = len;
917 attr = (struct rtattr *) buf;
918 while (RTA_OK(attr, attrlen)) {
919 switch (attr->rta_type) {
920 case IFLA_IFNAME:
921 if (RTA_PAYLOAD(attr) >= IFNAMSIZ)
922 break;
923 os_memcpy(ifname, RTA_DATA(attr), RTA_PAYLOAD(attr));
924 ifname[RTA_PAYLOAD(attr)] = '\0';
925 break;
926 case IFLA_MASTER:
927 brid = nla_get_u32((struct nlattr *) attr);
928 pos += os_snprintf(pos, end - pos, " master=%u", brid);
929 break;
930 case IFLA_WIRELESS:
931 pos += os_snprintf(pos, end - pos, " wext");
932 break;
933 case IFLA_OPERSTATE:
934 pos += os_snprintf(pos, end - pos, " operstate=%u",
935 nla_get_u32((struct nlattr *) attr));
936 break;
937 case IFLA_LINKMODE:
938 pos += os_snprintf(pos, end - pos, " linkmode=%u",
939 nla_get_u32((struct nlattr *) attr));
940 break;
941 }
942 attr = RTA_NEXT(attr, attrlen);
943 }
944 extra[sizeof(extra) - 1] = '\0';
945
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700946 wpa_printf(MSG_DEBUG, "RTM_NEWLINK: ifi_index=%d ifname=%s%s ifi_family=%d ifi_flags=0x%x (%s%s%s%s)",
947 ifi->ifi_index, ifname, extra, ifi->ifi_family,
948 ifi->ifi_flags,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700949 (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
950 (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
951 (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
952 (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
953
954 if (!drv->if_disabled && !(ifi->ifi_flags & IFF_UP)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800955 if (if_indextoname(ifi->ifi_index, namebuf) &&
956 linux_iface_up(drv->global->ioctl_sock,
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800957 drv->first_bss->ifname) > 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800958 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
959 "event since interface %s is up", namebuf);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800960 drv->ignore_if_down_event = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800961 return;
962 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700963 wpa_printf(MSG_DEBUG, "nl80211: Interface down");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800964 if (drv->ignore_if_down_event) {
965 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
966 "event generated by mode change");
967 drv->ignore_if_down_event = 0;
968 } else {
969 drv->if_disabled = 1;
970 wpa_supplicant_event(drv->ctx,
971 EVENT_INTERFACE_DISABLED, NULL);
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800972
973 /*
974 * Try to get drv again, since it may be removed as
975 * part of the EVENT_INTERFACE_DISABLED handling for
976 * dynamic interfaces
977 */
978 drv = nl80211_find_drv(global, ifi->ifi_index,
979 buf, len);
980 if (!drv)
981 return;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800982 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700983 }
984
985 if (drv->if_disabled && (ifi->ifi_flags & IFF_UP)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800986 if (if_indextoname(ifi->ifi_index, namebuf) &&
987 linux_iface_up(drv->global->ioctl_sock,
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800988 drv->first_bss->ifname) == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800989 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
990 "event since interface %s is down",
991 namebuf);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800992 } else if (if_nametoindex(drv->first_bss->ifname) == 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700993 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
994 "event since interface %s does not exist",
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800995 drv->first_bss->ifname);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700996 } else if (drv->if_removed) {
997 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
998 "event since interface %s is marked "
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800999 "removed", drv->first_bss->ifname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001000 } else {
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07001001 struct i802_bss *bss;
1002 u8 addr[ETH_ALEN];
1003
1004 /* Re-read MAC address as it may have changed */
1005 bss = get_bss_ifindex(drv, ifi->ifi_index);
1006 if (bss &&
1007 linux_get_ifhwaddr(drv->global->ioctl_sock,
1008 bss->ifname, addr) < 0) {
1009 wpa_printf(MSG_DEBUG,
1010 "nl80211: %s: failed to re-read MAC address",
1011 bss->ifname);
1012 } else if (bss &&
1013 os_memcmp(addr, bss->addr, ETH_ALEN) != 0) {
1014 wpa_printf(MSG_DEBUG,
1015 "nl80211: Own MAC address on ifindex %d (%s) changed from "
1016 MACSTR " to " MACSTR,
1017 ifi->ifi_index, bss->ifname,
1018 MAC2STR(bss->addr),
1019 MAC2STR(addr));
1020 os_memcpy(bss->addr, addr, ETH_ALEN);
1021 }
1022
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001023 wpa_printf(MSG_DEBUG, "nl80211: Interface up");
1024 drv->if_disabled = 0;
1025 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_ENABLED,
1026 NULL);
1027 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001028 }
1029
1030 /*
1031 * Some drivers send the association event before the operup event--in
1032 * this case, lifting operstate in wpa_driver_nl80211_set_operstate()
1033 * fails. This will hit us when wpa_supplicant does not need to do
1034 * IEEE 802.1X authentication
1035 */
1036 if (drv->operstate == 1 &&
1037 (ifi->ifi_flags & (IFF_LOWER_UP | IFF_DORMANT)) == IFF_LOWER_UP &&
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001038 !(ifi->ifi_flags & IFF_RUNNING)) {
1039 wpa_printf(MSG_DEBUG, "nl80211: Set IF_OPER_UP again based on ifi_flags and expected operstate");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001040 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001041 -1, IF_OPER_UP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001042 }
1043
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001044 if (ifname[0])
1045 wpa_driver_nl80211_event_newlink(drv, ifname);
1046
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001047 if (ifi->ifi_family == AF_BRIDGE && brid) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001048 struct i802_bss *bss;
1049
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001050 /* device has been added to bridge */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001051 if (!if_indextoname(brid, namebuf)) {
1052 wpa_printf(MSG_DEBUG,
1053 "nl80211: Could not find bridge ifname for ifindex %u",
1054 brid);
1055 return;
1056 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001057 wpa_printf(MSG_DEBUG, "nl80211: Add ifindex %u for bridge %s",
1058 brid, namebuf);
1059 add_ifidx(drv, brid);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001060
1061 for (bss = drv->first_bss; bss; bss = bss->next) {
1062 if (os_strcmp(ifname, bss->ifname) == 0) {
1063 os_strlcpy(bss->brname, namebuf, IFNAMSIZ);
1064 break;
1065 }
1066 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001067 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001068}
1069
1070
1071static void wpa_driver_nl80211_event_rtm_dellink(void *ctx,
1072 struct ifinfomsg *ifi,
1073 u8 *buf, size_t len)
1074{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001075 struct nl80211_global *global = ctx;
1076 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001077 int attrlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001078 struct rtattr *attr;
1079 u32 brid = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001080 char ifname[IFNAMSIZ + 1];
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001081 char extra[100], *pos, *end;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001082
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001083 drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
1084 if (!drv) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001085 wpa_printf(MSG_DEBUG, "nl80211: Ignore RTM_DELLINK event for foreign ifindex %d",
1086 ifi->ifi_index);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001087 return;
1088 }
1089
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001090 extra[0] = '\0';
1091 pos = extra;
1092 end = pos + sizeof(extra);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001093 ifname[0] = '\0';
1094
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001095 attrlen = len;
1096 attr = (struct rtattr *) buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001097 while (RTA_OK(attr, attrlen)) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001098 switch (attr->rta_type) {
1099 case IFLA_IFNAME:
1100 if (RTA_PAYLOAD(attr) >= IFNAMSIZ)
1101 break;
1102 os_memcpy(ifname, RTA_DATA(attr), RTA_PAYLOAD(attr));
1103 ifname[RTA_PAYLOAD(attr)] = '\0';
1104 break;
1105 case IFLA_MASTER:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001106 brid = nla_get_u32((struct nlattr *) attr);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001107 pos += os_snprintf(pos, end - pos, " master=%u", brid);
1108 break;
1109 case IFLA_OPERSTATE:
1110 pos += os_snprintf(pos, end - pos, " operstate=%u",
1111 nla_get_u32((struct nlattr *) attr));
1112 break;
1113 case IFLA_LINKMODE:
1114 pos += os_snprintf(pos, end - pos, " linkmode=%u",
1115 nla_get_u32((struct nlattr *) attr));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001116 break;
1117 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001118 attr = RTA_NEXT(attr, attrlen);
1119 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001120 extra[sizeof(extra) - 1] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001121
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001122 wpa_printf(MSG_DEBUG, "RTM_DELLINK: ifi_index=%d ifname=%s%s ifi_family=%d ifi_flags=0x%x (%s%s%s%s)",
1123 ifi->ifi_index, ifname, extra, ifi->ifi_family,
1124 ifi->ifi_flags,
1125 (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
1126 (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
1127 (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
1128 (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
1129
1130 if (ifname[0] && (ifi->ifi_family != AF_BRIDGE || !brid))
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001131 wpa_driver_nl80211_event_dellink(drv, ifname);
1132
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001133 if (ifi->ifi_family == AF_BRIDGE && brid) {
1134 /* device has been removed from bridge */
1135 char namebuf[IFNAMSIZ];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001136
1137 if (!if_indextoname(brid, namebuf)) {
1138 wpa_printf(MSG_DEBUG,
1139 "nl80211: Could not find bridge ifname for ifindex %u",
1140 brid);
1141 } else {
1142 wpa_printf(MSG_DEBUG,
1143 "nl80211: Remove ifindex %u for bridge %s",
1144 brid, namebuf);
1145 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001146 del_ifidx(drv, brid);
1147 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001148}
1149
1150
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001151unsigned int nl80211_get_assoc_freq(struct wpa_driver_nl80211_data *drv)
Jouni Malinen87fd2792011-05-16 18:35:42 +03001152{
1153 struct nl_msg *msg;
1154 int ret;
1155 struct nl80211_bss_info_arg arg;
1156
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001157 msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
Jouni Malinen87fd2792011-05-16 18:35:42 +03001158 os_memset(&arg, 0, sizeof(arg));
Jouni Malinen87fd2792011-05-16 18:35:42 +03001159 arg.drv = drv;
1160 ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
Jouni Malinen87fd2792011-05-16 18:35:42 +03001161 if (ret == 0) {
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07001162 unsigned int freq = drv->nlmode == NL80211_IFTYPE_ADHOC ?
1163 arg.ibss_freq : arg.assoc_freq;
Jouni Malinen87fd2792011-05-16 18:35:42 +03001164 wpa_printf(MSG_DEBUG, "nl80211: Operating frequency for the "
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07001165 "associated BSS from scan results: %u MHz", freq);
1166 if (freq)
1167 drv->assoc_freq = freq;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001168 return drv->assoc_freq;
Jouni Malinen87fd2792011-05-16 18:35:42 +03001169 }
1170 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
1171 "(%s)", ret, strerror(-ret));
Jouni Malinen87fd2792011-05-16 18:35:42 +03001172 return drv->assoc_freq;
1173}
1174
1175
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001176static int get_link_signal(struct nl_msg *msg, void *arg)
1177{
1178 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1179 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1180 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
1181 static struct nla_policy policy[NL80211_STA_INFO_MAX + 1] = {
1182 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001183 [NL80211_STA_INFO_SIGNAL_AVG] = { .type = NLA_U8 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001184 };
1185 struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
1186 static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
1187 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
1188 [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
1189 [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
1190 [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
1191 };
1192 struct wpa_signal_info *sig_change = arg;
1193
1194 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1195 genlmsg_attrlen(gnlh, 0), NULL);
1196 if (!tb[NL80211_ATTR_STA_INFO] ||
1197 nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
1198 tb[NL80211_ATTR_STA_INFO], policy))
1199 return NL_SKIP;
1200 if (!sinfo[NL80211_STA_INFO_SIGNAL])
1201 return NL_SKIP;
1202
1203 sig_change->current_signal =
1204 (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
1205
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001206 if (sinfo[NL80211_STA_INFO_SIGNAL_AVG])
1207 sig_change->avg_signal =
1208 (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL_AVG]);
1209 else
1210 sig_change->avg_signal = 0;
1211
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001212 if (sinfo[NL80211_STA_INFO_TX_BITRATE]) {
1213 if (nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
1214 sinfo[NL80211_STA_INFO_TX_BITRATE],
1215 rate_policy)) {
1216 sig_change->current_txrate = 0;
1217 } else {
1218 if (rinfo[NL80211_RATE_INFO_BITRATE]) {
1219 sig_change->current_txrate =
1220 nla_get_u16(rinfo[
1221 NL80211_RATE_INFO_BITRATE]) * 100;
1222 }
1223 }
1224 }
1225
1226 return NL_SKIP;
1227}
1228
1229
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001230int nl80211_get_link_signal(struct wpa_driver_nl80211_data *drv,
1231 struct wpa_signal_info *sig)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001232{
1233 struct nl_msg *msg;
1234
1235 sig->current_signal = -9999;
1236 sig->current_txrate = 0;
1237
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001238 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_GET_STATION)) ||
1239 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid)) {
1240 nlmsg_free(msg);
1241 return -ENOBUFS;
1242 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001243
1244 return send_and_recv_msgs(drv, msg, get_link_signal, sig);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001245}
1246
1247
1248static int get_link_noise(struct nl_msg *msg, void *arg)
1249{
1250 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1251 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1252 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
1253 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
1254 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
1255 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
1256 };
1257 struct wpa_signal_info *sig_change = arg;
1258
1259 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1260 genlmsg_attrlen(gnlh, 0), NULL);
1261
1262 if (!tb[NL80211_ATTR_SURVEY_INFO]) {
1263 wpa_printf(MSG_DEBUG, "nl80211: survey data missing!");
1264 return NL_SKIP;
1265 }
1266
1267 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
1268 tb[NL80211_ATTR_SURVEY_INFO],
1269 survey_policy)) {
1270 wpa_printf(MSG_DEBUG, "nl80211: failed to parse nested "
1271 "attributes!");
1272 return NL_SKIP;
1273 }
1274
1275 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
1276 return NL_SKIP;
1277
1278 if (nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
1279 sig_change->frequency)
1280 return NL_SKIP;
1281
1282 if (!sinfo[NL80211_SURVEY_INFO_NOISE])
1283 return NL_SKIP;
1284
1285 sig_change->current_noise =
1286 (s8) nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
1287
1288 return NL_SKIP;
1289}
1290
1291
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001292int nl80211_get_link_noise(struct wpa_driver_nl80211_data *drv,
1293 struct wpa_signal_info *sig_change)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001294{
1295 struct nl_msg *msg;
1296
1297 sig_change->current_noise = 9999;
1298 sig_change->frequency = drv->assoc_freq;
1299
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001300 msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001301 return send_and_recv_msgs(drv, msg, get_link_noise, sig_change);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001302}
1303
1304
1305static void wpa_driver_nl80211_event_receive(int sock, void *eloop_ctx,
1306 void *handle)
1307{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001308 struct nl_cb *cb = eloop_ctx;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001309 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001310
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001311 wpa_printf(MSG_MSGDUMP, "nl80211: Event message available");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001312
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001313 res = nl_recvmsgs(handle, cb);
Dmitry Shmidt71757432014-06-02 13:50:35 -07001314 if (res < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001315 wpa_printf(MSG_INFO, "nl80211: %s->nl_recvmsgs failed: %d",
1316 __func__, res);
1317 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001318}
1319
1320
1321/**
1322 * wpa_driver_nl80211_set_country - ask nl80211 to set the regulatory domain
1323 * @priv: driver_nl80211 private data
1324 * @alpha2_arg: country to which to switch to
1325 * Returns: 0 on success, -1 on failure
1326 *
1327 * This asks nl80211 to set the regulatory domain for given
1328 * country ISO / IEC alpha2.
1329 */
1330static int wpa_driver_nl80211_set_country(void *priv, const char *alpha2_arg)
1331{
1332 struct i802_bss *bss = priv;
1333 struct wpa_driver_nl80211_data *drv = bss->drv;
1334 char alpha2[3];
1335 struct nl_msg *msg;
1336
1337 msg = nlmsg_alloc();
1338 if (!msg)
1339 return -ENOMEM;
1340
1341 alpha2[0] = alpha2_arg[0];
1342 alpha2[1] = alpha2_arg[1];
1343 alpha2[2] = '\0';
1344
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001345 if (!nl80211_cmd(drv, msg, 0, NL80211_CMD_REQ_SET_REG) ||
1346 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, alpha2)) {
1347 nlmsg_free(msg);
1348 return -EINVAL;
1349 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001350 if (send_and_recv_msgs(drv, msg, NULL, NULL))
1351 return -EINVAL;
1352 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001353}
1354
1355
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001356static int nl80211_get_country(struct nl_msg *msg, void *arg)
1357{
1358 char *alpha2 = arg;
1359 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
1360 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1361
1362 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1363 genlmsg_attrlen(gnlh, 0), NULL);
1364 if (!tb_msg[NL80211_ATTR_REG_ALPHA2]) {
1365 wpa_printf(MSG_DEBUG, "nl80211: No country information available");
1366 return NL_SKIP;
1367 }
1368 os_strlcpy(alpha2, nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]), 3);
1369 return NL_SKIP;
1370}
1371
1372
1373static int wpa_driver_nl80211_get_country(void *priv, char *alpha2)
1374{
1375 struct i802_bss *bss = priv;
1376 struct wpa_driver_nl80211_data *drv = bss->drv;
1377 struct nl_msg *msg;
1378 int ret;
1379
1380 msg = nlmsg_alloc();
1381 if (!msg)
1382 return -ENOMEM;
1383
1384 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG);
1385 alpha2[0] = '\0';
1386 ret = send_and_recv_msgs(drv, msg, nl80211_get_country, alpha2);
1387 if (!alpha2[0])
1388 ret = -1;
1389
1390 return ret;
1391}
1392
1393
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001394static int wpa_driver_nl80211_init_nl_global(struct nl80211_global *global)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001395{
1396 int ret;
1397
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001398 global->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
1399 if (global->nl_cb == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001400 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
1401 "callbacks");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001402 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001403 }
1404
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001405 global->nl = nl_create_handle(global->nl_cb, "nl");
1406 if (global->nl == NULL)
1407 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001408
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001409 global->nl80211_id = genl_ctrl_resolve(global->nl, "nl80211");
1410 if (global->nl80211_id < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001411 wpa_printf(MSG_ERROR, "nl80211: 'nl80211' generic netlink not "
1412 "found");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001413 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001414 }
1415
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001416 global->nl_event = nl_create_handle(global->nl_cb, "event");
1417 if (global->nl_event == NULL)
1418 goto err;
1419
1420 ret = nl_get_multicast_id(global, "nl80211", "scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001421 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001422 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001423 if (ret < 0) {
1424 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
1425 "membership for scan events: %d (%s)",
1426 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001427 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001428 }
1429
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001430 ret = nl_get_multicast_id(global, "nl80211", "mlme");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001431 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001432 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001433 if (ret < 0) {
1434 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
1435 "membership for mlme events: %d (%s)",
1436 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001437 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001438 }
1439
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001440 ret = nl_get_multicast_id(global, "nl80211", "regulatory");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001441 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001442 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001443 if (ret < 0) {
1444 wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
1445 "membership for regulatory events: %d (%s)",
1446 ret, strerror(-ret));
1447 /* Continue without regulatory events */
1448 }
1449
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001450 ret = nl_get_multicast_id(global, "nl80211", "vendor");
1451 if (ret >= 0)
1452 ret = nl_socket_add_membership(global->nl_event, ret);
1453 if (ret < 0) {
1454 wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
1455 "membership for vendor events: %d (%s)",
1456 ret, strerror(-ret));
1457 /* Continue without vendor events */
1458 }
1459
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001460 nl_cb_set(global->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
1461 no_seq_check, NULL);
1462 nl_cb_set(global->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
1463 process_global_event, global);
1464
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001465 nl80211_register_eloop_read(&global->nl_event,
1466 wpa_driver_nl80211_event_receive,
1467 global->nl_cb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001468
1469 return 0;
1470
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001471err:
1472 nl_destroy_handles(&global->nl_event);
1473 nl_destroy_handles(&global->nl);
1474 nl_cb_put(global->nl_cb);
1475 global->nl_cb = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001476 return -1;
1477}
1478
1479
1480static void wpa_driver_nl80211_rfkill_blocked(void *ctx)
1481{
1482 wpa_printf(MSG_DEBUG, "nl80211: RFKILL blocked");
1483 /*
1484 * This may be for any interface; use ifdown event to disable
1485 * interface.
1486 */
1487}
1488
1489
1490static void wpa_driver_nl80211_rfkill_unblocked(void *ctx)
1491{
1492 struct wpa_driver_nl80211_data *drv = ctx;
1493 wpa_printf(MSG_DEBUG, "nl80211: RFKILL unblocked");
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001494 if (i802_set_iface_flags(drv->first_bss, 1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001495 wpa_printf(MSG_DEBUG, "nl80211: Could not set interface UP "
1496 "after rfkill unblock");
1497 return;
1498 }
1499 /* rtnetlink ifup handler will report interface as enabled */
1500}
1501
1502
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001503static void wpa_driver_nl80211_handle_eapol_tx_status(int sock,
1504 void *eloop_ctx,
1505 void *handle)
1506{
1507 struct wpa_driver_nl80211_data *drv = eloop_ctx;
1508 u8 data[2048];
1509 struct msghdr msg;
1510 struct iovec entry;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001511 u8 control[512];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001512 struct cmsghdr *cmsg;
1513 int res, found_ee = 0, found_wifi = 0, acked = 0;
1514 union wpa_event_data event;
1515
1516 memset(&msg, 0, sizeof(msg));
1517 msg.msg_iov = &entry;
1518 msg.msg_iovlen = 1;
1519 entry.iov_base = data;
1520 entry.iov_len = sizeof(data);
1521 msg.msg_control = &control;
1522 msg.msg_controllen = sizeof(control);
1523
1524 res = recvmsg(sock, &msg, MSG_ERRQUEUE);
1525 /* if error or not fitting 802.3 header, return */
1526 if (res < 14)
1527 return;
1528
1529 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg))
1530 {
1531 if (cmsg->cmsg_level == SOL_SOCKET &&
1532 cmsg->cmsg_type == SCM_WIFI_STATUS) {
1533 int *ack;
1534
1535 found_wifi = 1;
1536 ack = (void *)CMSG_DATA(cmsg);
1537 acked = *ack;
1538 }
1539
1540 if (cmsg->cmsg_level == SOL_PACKET &&
1541 cmsg->cmsg_type == PACKET_TX_TIMESTAMP) {
1542 struct sock_extended_err *err =
1543 (struct sock_extended_err *)CMSG_DATA(cmsg);
1544
1545 if (err->ee_origin == SO_EE_ORIGIN_TXSTATUS)
1546 found_ee = 1;
1547 }
1548 }
1549
1550 if (!found_ee || !found_wifi)
1551 return;
1552
1553 memset(&event, 0, sizeof(event));
1554 event.eapol_tx_status.dst = data;
1555 event.eapol_tx_status.data = data + 14;
1556 event.eapol_tx_status.data_len = res - 14;
1557 event.eapol_tx_status.ack = acked;
1558 wpa_supplicant_event(drv->ctx, EVENT_EAPOL_TX_STATUS, &event);
1559}
1560
1561
1562static int nl80211_init_bss(struct i802_bss *bss)
1563{
1564 bss->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
1565 if (!bss->nl_cb)
1566 return -1;
1567
1568 nl_cb_set(bss->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
1569 no_seq_check, NULL);
1570 nl_cb_set(bss->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
1571 process_bss_event, bss);
1572
1573 return 0;
1574}
1575
1576
1577static void nl80211_destroy_bss(struct i802_bss *bss)
1578{
1579 nl_cb_put(bss->nl_cb);
1580 bss->nl_cb = NULL;
1581}
1582
1583
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001584static void * wpa_driver_nl80211_drv_init(void *ctx, const char *ifname,
1585 void *global_priv, int hostapd,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001586 const u8 *set_addr,
1587 const char *driver_params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001588{
1589 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001590 struct rfkill_config *rcfg;
1591 struct i802_bss *bss;
1592
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001593 if (global_priv == NULL)
1594 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001595 drv = os_zalloc(sizeof(*drv));
1596 if (drv == NULL)
1597 return NULL;
1598 drv->global = global_priv;
1599 drv->ctx = ctx;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001600 drv->hostapd = !!hostapd;
1601 drv->eapol_sock = -1;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001602
1603 /*
1604 * There is no driver capability flag for this, so assume it is
1605 * supported and disable this on first attempt to use if the driver
1606 * rejects the command due to missing support.
1607 */
1608 drv->set_rekey_offload = 1;
1609
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001610 drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int);
1611 drv->if_indices = drv->default_if_indices;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001612
1613 drv->first_bss = os_zalloc(sizeof(*drv->first_bss));
1614 if (!drv->first_bss) {
1615 os_free(drv);
1616 return NULL;
1617 }
1618 bss = drv->first_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001619 bss->drv = drv;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001620 bss->ctx = ctx;
1621
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001622 os_strlcpy(bss->ifname, ifname, sizeof(bss->ifname));
1623 drv->monitor_ifidx = -1;
1624 drv->monitor_sock = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001625 drv->eapol_tx_sock = -1;
1626 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001627
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001628 if (nl80211_init_bss(bss))
1629 goto failed;
1630
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001631 rcfg = os_zalloc(sizeof(*rcfg));
1632 if (rcfg == NULL)
1633 goto failed;
1634 rcfg->ctx = drv;
1635 os_strlcpy(rcfg->ifname, ifname, sizeof(rcfg->ifname));
1636 rcfg->blocked_cb = wpa_driver_nl80211_rfkill_blocked;
1637 rcfg->unblocked_cb = wpa_driver_nl80211_rfkill_unblocked;
1638 drv->rfkill = rfkill_init(rcfg);
1639 if (drv->rfkill == NULL) {
1640 wpa_printf(MSG_DEBUG, "nl80211: RFKILL status not available");
1641 os_free(rcfg);
1642 }
1643
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001644 if (linux_iface_up(drv->global->ioctl_sock, ifname) > 0)
1645 drv->start_iface_up = 1;
1646
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001647 if (wpa_driver_nl80211_finish_drv_init(drv, set_addr, 1, driver_params))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001648 goto failed;
1649
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001650 drv->eapol_tx_sock = socket(PF_PACKET, SOCK_DGRAM, 0);
1651 if (drv->eapol_tx_sock < 0)
1652 goto failed;
1653
1654 if (drv->data_tx_status) {
1655 int enabled = 1;
1656
1657 if (setsockopt(drv->eapol_tx_sock, SOL_SOCKET, SO_WIFI_STATUS,
1658 &enabled, sizeof(enabled)) < 0) {
1659 wpa_printf(MSG_DEBUG,
1660 "nl80211: wifi status sockopt failed\n");
1661 drv->data_tx_status = 0;
1662 if (!drv->use_monitor)
1663 drv->capa.flags &=
1664 ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
1665 } else {
1666 eloop_register_read_sock(drv->eapol_tx_sock,
1667 wpa_driver_nl80211_handle_eapol_tx_status,
1668 drv, NULL);
1669 }
1670 }
1671
1672 if (drv->global) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001673 dl_list_add(&drv->global->interfaces, &drv->list);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001674 drv->in_interface_list = 1;
1675 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001676
1677 return bss;
1678
1679failed:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001680 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001681 return NULL;
1682}
1683
1684
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001685/**
1686 * wpa_driver_nl80211_init - Initialize nl80211 driver interface
1687 * @ctx: context to be used when calling wpa_supplicant functions,
1688 * e.g., wpa_supplicant_event()
1689 * @ifname: interface name, e.g., wlan0
1690 * @global_priv: private driver global data from global_init()
1691 * Returns: Pointer to private data, %NULL on failure
1692 */
1693static void * wpa_driver_nl80211_init(void *ctx, const char *ifname,
1694 void *global_priv)
1695{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001696 return wpa_driver_nl80211_drv_init(ctx, ifname, global_priv, 0, NULL,
1697 NULL);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001698}
1699
1700
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001701static int nl80211_register_frame(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001702 struct nl_handle *nl_handle,
1703 u16 type, const u8 *match, size_t match_len)
1704{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001705 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001706 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001707 int ret;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001708 char buf[30];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001709
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001710 buf[0] = '\0';
1711 wpa_snprintf_hex(buf, sizeof(buf), match, match_len);
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07001712 wpa_printf(MSG_DEBUG, "nl80211: Register frame type=0x%x (%s) nl_handle=%p match=%s",
1713 type, fc2str(type), nl_handle, buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001714
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001715 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_REGISTER_ACTION)) ||
1716 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE, type) ||
1717 nla_put(msg, NL80211_ATTR_FRAME_MATCH, match_len, match)) {
1718 nlmsg_free(msg);
1719 return -1;
1720 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001721
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001722 ret = send_and_recv(drv->global, nl_handle, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001723 if (ret) {
1724 wpa_printf(MSG_DEBUG, "nl80211: Register frame command "
1725 "failed (type=%u): ret=%d (%s)",
1726 type, ret, strerror(-ret));
1727 wpa_hexdump(MSG_DEBUG, "nl80211: Register frame match",
1728 match, match_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001729 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001730 return ret;
1731}
1732
1733
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001734static int nl80211_alloc_mgmt_handle(struct i802_bss *bss)
1735{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001736 if (bss->nl_mgmt) {
1737 wpa_printf(MSG_DEBUG, "nl80211: Mgmt reporting "
1738 "already on! (nl_mgmt=%p)", bss->nl_mgmt);
1739 return -1;
1740 }
1741
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001742 bss->nl_mgmt = nl_create_handle(bss->nl_cb, "mgmt");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001743 if (bss->nl_mgmt == NULL)
1744 return -1;
1745
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001746 return 0;
1747}
1748
1749
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001750static void nl80211_mgmt_handle_register_eloop(struct i802_bss *bss)
1751{
1752 nl80211_register_eloop_read(&bss->nl_mgmt,
1753 wpa_driver_nl80211_event_receive,
1754 bss->nl_cb);
1755}
1756
1757
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001758static int nl80211_register_action_frame(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001759 const u8 *match, size_t match_len)
1760{
1761 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_ACTION << 4);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001762 return nl80211_register_frame(bss, bss->nl_mgmt,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001763 type, match, match_len);
1764}
1765
1766
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001767static int nl80211_mgmt_subscribe_non_ap(struct i802_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001768{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001769 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001770 int ret = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001771
1772 if (nl80211_alloc_mgmt_handle(bss))
1773 return -1;
1774 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with non-AP "
1775 "handle %p", bss->nl_mgmt);
1776
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001777 if (drv->nlmode == NL80211_IFTYPE_ADHOC) {
1778 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_AUTH << 4);
1779
1780 /* register for any AUTH message */
1781 nl80211_register_frame(bss, bss->nl_mgmt, type, NULL, 0);
1782 }
1783
Dmitry Shmidt051af732013-10-22 13:52:46 -07001784#ifdef CONFIG_INTERWORKING
1785 /* QoS Map Configure */
1786 if (nl80211_register_action_frame(bss, (u8 *) "\x01\x04", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001787 ret = -1;
Dmitry Shmidt051af732013-10-22 13:52:46 -07001788#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001789#if defined(CONFIG_P2P) || defined(CONFIG_INTERWORKING)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001790 /* GAS Initial Request */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001791 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0a", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001792 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001793 /* GAS Initial Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001794 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0b", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001795 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001796 /* GAS Comeback Request */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001797 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0c", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001798 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001799 /* GAS Comeback Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001800 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0d", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001801 ret = -1;
Dmitry Shmidt18463232014-01-24 12:29:41 -08001802 /* Protected GAS Initial Request */
1803 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0a", 2) < 0)
1804 ret = -1;
1805 /* Protected GAS Initial Response */
1806 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0b", 2) < 0)
1807 ret = -1;
1808 /* Protected GAS Comeback Request */
1809 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0c", 2) < 0)
1810 ret = -1;
1811 /* Protected GAS Comeback Response */
1812 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0d", 2) < 0)
1813 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001814#endif /* CONFIG_P2P || CONFIG_INTERWORKING */
1815#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001816 /* P2P Public Action */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001817 if (nl80211_register_action_frame(bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001818 (u8 *) "\x04\x09\x50\x6f\x9a\x09",
1819 6) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001820 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001821 /* P2P Action */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001822 if (nl80211_register_action_frame(bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001823 (u8 *) "\x7f\x50\x6f\x9a\x09",
1824 5) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001825 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001826#endif /* CONFIG_P2P */
1827#ifdef CONFIG_IEEE80211W
1828 /* SA Query Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001829 if (nl80211_register_action_frame(bss, (u8 *) "\x08\x01", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001830 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001831#endif /* CONFIG_IEEE80211W */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001832#ifdef CONFIG_TDLS
1833 if ((drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT)) {
1834 /* TDLS Discovery Response */
1835 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0e", 2) <
1836 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001837 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001838 }
1839#endif /* CONFIG_TDLS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001840
1841 /* FT Action frames */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001842 if (nl80211_register_action_frame(bss, (u8 *) "\x06", 1) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001843 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001844 else
1845 drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_FT |
1846 WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK;
1847
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001848 /* WNM - BSS Transition Management Request */
1849 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x07", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001850 ret = -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001851 /* WNM-Sleep Mode Response */
1852 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x11", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001853 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001854
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001855#ifdef CONFIG_HS20
1856 /* WNM-Notification */
1857 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x1a", 2) < 0)
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001858 ret = -1;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001859#endif /* CONFIG_HS20 */
1860
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001861 /* WMM-AC ADDTS Response */
1862 if (nl80211_register_action_frame(bss, (u8 *) "\x11\x01", 2) < 0)
1863 ret = -1;
1864
1865 /* WMM-AC DELTS */
1866 if (nl80211_register_action_frame(bss, (u8 *) "\x11\x02", 2) < 0)
1867 ret = -1;
1868
1869 /* Radio Measurement - Neighbor Report Response */
1870 if (nl80211_register_action_frame(bss, (u8 *) "\x05\x05", 2) < 0)
1871 ret = -1;
1872
1873 /* Radio Measurement - Link Measurement Request */
1874 if ((drv->capa.rrm_flags & WPA_DRIVER_FLAGS_TX_POWER_INSERTION) &&
1875 (nl80211_register_action_frame(bss, (u8 *) "\x05\x02", 2) < 0))
1876 ret = -1;
1877
1878 nl80211_mgmt_handle_register_eloop(bss);
1879
1880 return ret;
1881}
1882
1883
1884static int nl80211_mgmt_subscribe_mesh(struct i802_bss *bss)
1885{
1886 int ret = 0;
1887
1888 if (nl80211_alloc_mgmt_handle(bss))
1889 return -1;
1890
1891 wpa_printf(MSG_DEBUG,
1892 "nl80211: Subscribe to mgmt frames with mesh handle %p",
1893 bss->nl_mgmt);
1894
1895 /* Auth frames for mesh SAE */
1896 if (nl80211_register_frame(bss, bss->nl_mgmt,
1897 (WLAN_FC_TYPE_MGMT << 2) |
1898 (WLAN_FC_STYPE_AUTH << 4),
1899 NULL, 0) < 0)
1900 ret = -1;
1901
1902 /* Mesh peering open */
1903 if (nl80211_register_action_frame(bss, (u8 *) "\x0f\x01", 2) < 0)
1904 ret = -1;
1905 /* Mesh peering confirm */
1906 if (nl80211_register_action_frame(bss, (u8 *) "\x0f\x02", 2) < 0)
1907 ret = -1;
1908 /* Mesh peering close */
1909 if (nl80211_register_action_frame(bss, (u8 *) "\x0f\x03", 2) < 0)
1910 ret = -1;
1911
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001912 nl80211_mgmt_handle_register_eloop(bss);
1913
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001914 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001915}
1916
1917
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001918static int nl80211_register_spurious_class3(struct i802_bss *bss)
1919{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001920 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001921 int ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001922
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001923 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_UNEXPECTED_FRAME);
1924 ret = send_and_recv(bss->drv->global, bss->nl_mgmt, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001925 if (ret) {
1926 wpa_printf(MSG_DEBUG, "nl80211: Register spurious class3 "
1927 "failed: ret=%d (%s)",
1928 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001929 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001930 return ret;
1931}
1932
1933
1934static int nl80211_mgmt_subscribe_ap(struct i802_bss *bss)
1935{
1936 static const int stypes[] = {
1937 WLAN_FC_STYPE_AUTH,
1938 WLAN_FC_STYPE_ASSOC_REQ,
1939 WLAN_FC_STYPE_REASSOC_REQ,
1940 WLAN_FC_STYPE_DISASSOC,
1941 WLAN_FC_STYPE_DEAUTH,
1942 WLAN_FC_STYPE_ACTION,
1943 WLAN_FC_STYPE_PROBE_REQ,
1944/* Beacon doesn't work as mac80211 doesn't currently allow
1945 * it, but it wouldn't really be the right thing anyway as
1946 * it isn't per interface ... maybe just dump the scan
1947 * results periodically for OLBC?
1948 */
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07001949 /* WLAN_FC_STYPE_BEACON, */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001950 };
1951 unsigned int i;
1952
1953 if (nl80211_alloc_mgmt_handle(bss))
1954 return -1;
1955 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
1956 "handle %p", bss->nl_mgmt);
1957
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001958 for (i = 0; i < ARRAY_SIZE(stypes); i++) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001959 if (nl80211_register_frame(bss, bss->nl_mgmt,
1960 (WLAN_FC_TYPE_MGMT << 2) |
1961 (stypes[i] << 4),
1962 NULL, 0) < 0) {
1963 goto out_err;
1964 }
1965 }
1966
1967 if (nl80211_register_spurious_class3(bss))
1968 goto out_err;
1969
1970 if (nl80211_get_wiphy_data_ap(bss) == NULL)
1971 goto out_err;
1972
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001973 nl80211_mgmt_handle_register_eloop(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001974 return 0;
1975
1976out_err:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001977 nl_destroy_handles(&bss->nl_mgmt);
1978 return -1;
1979}
1980
1981
1982static int nl80211_mgmt_subscribe_ap_dev_sme(struct i802_bss *bss)
1983{
1984 if (nl80211_alloc_mgmt_handle(bss))
1985 return -1;
1986 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
1987 "handle %p (device SME)", bss->nl_mgmt);
1988
1989 if (nl80211_register_frame(bss, bss->nl_mgmt,
1990 (WLAN_FC_TYPE_MGMT << 2) |
1991 (WLAN_FC_STYPE_ACTION << 4),
1992 NULL, 0) < 0)
1993 goto out_err;
1994
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001995 nl80211_mgmt_handle_register_eloop(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001996 return 0;
1997
1998out_err:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001999 nl_destroy_handles(&bss->nl_mgmt);
2000 return -1;
2001}
2002
2003
2004static void nl80211_mgmt_unsubscribe(struct i802_bss *bss, const char *reason)
2005{
2006 if (bss->nl_mgmt == NULL)
2007 return;
2008 wpa_printf(MSG_DEBUG, "nl80211: Unsubscribe mgmt frames handle %p "
2009 "(%s)", bss->nl_mgmt, reason);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002010 nl80211_destroy_eloop_handle(&bss->nl_mgmt);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002011
2012 nl80211_put_wiphy_data_ap(bss);
2013}
2014
2015
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002016static void wpa_driver_nl80211_send_rfkill(void *eloop_ctx, void *timeout_ctx)
2017{
2018 wpa_supplicant_event(timeout_ctx, EVENT_INTERFACE_DISABLED, NULL);
2019}
2020
2021
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002022static void nl80211_del_p2pdev(struct i802_bss *bss)
2023{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002024 struct nl_msg *msg;
2025 int ret;
2026
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002027 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_DEL_INTERFACE);
2028 ret = send_and_recv_msgs(bss->drv, msg, NULL, NULL);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002029
2030 wpa_printf(MSG_DEBUG, "nl80211: Delete P2P Device %s (0x%llx): %s",
2031 bss->ifname, (long long unsigned int) bss->wdev_id,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002032 strerror(-ret));
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002033}
2034
2035
2036static int nl80211_set_p2pdev(struct i802_bss *bss, int start)
2037{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002038 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002039 int ret;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002040
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002041 msg = nl80211_cmd_msg(bss, 0, start ? NL80211_CMD_START_P2P_DEVICE :
2042 NL80211_CMD_STOP_P2P_DEVICE);
2043 ret = send_and_recv_msgs(bss->drv, msg, NULL, NULL);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002044
2045 wpa_printf(MSG_DEBUG, "nl80211: %s P2P Device %s (0x%llx): %s",
2046 start ? "Start" : "Stop",
2047 bss->ifname, (long long unsigned int) bss->wdev_id,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002048 strerror(-ret));
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002049 return ret;
2050}
2051
2052
2053static int i802_set_iface_flags(struct i802_bss *bss, int up)
2054{
2055 enum nl80211_iftype nlmode;
2056
2057 nlmode = nl80211_get_ifmode(bss);
2058 if (nlmode != NL80211_IFTYPE_P2P_DEVICE) {
2059 return linux_set_iface_flags(bss->drv->global->ioctl_sock,
2060 bss->ifname, up);
2061 }
2062
2063 /* P2P Device has start/stop which is equivalent */
2064 return nl80211_set_p2pdev(bss, up);
2065}
2066
2067
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002068static int
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002069wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002070 const u8 *set_addr, int first,
2071 const char *driver_params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002072{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002073 struct i802_bss *bss = drv->first_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002074 int send_rfkill_event = 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002075 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002076
2077 drv->ifindex = if_nametoindex(bss->ifname);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002078 bss->ifindex = drv->ifindex;
2079 bss->wdev_id = drv->global->if_add_wdevid;
2080 bss->wdev_id_set = drv->global->if_add_wdevid_set;
2081
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002082 bss->if_dynamic = drv->ifindex == drv->global->if_add_ifindex;
2083 bss->if_dynamic = bss->if_dynamic || drv->global->if_add_wdevid_set;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002084 drv->global->if_add_wdevid_set = 0;
2085
Dmitry Shmidt03658832014-08-13 11:03:49 -07002086 if (!bss->if_dynamic && nl80211_get_ifmode(bss) == NL80211_IFTYPE_AP)
2087 bss->static_ap = 1;
2088
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002089 if (wpa_driver_nl80211_capa(drv))
2090 return -1;
2091
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002092 if (driver_params && nl80211_set_param(bss, driver_params) < 0)
2093 return -1;
2094
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002095 wpa_printf(MSG_DEBUG, "nl80211: interface %s in phy %s",
2096 bss->ifname, drv->phyname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002097
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002098 if (set_addr &&
2099 (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0) ||
2100 linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
2101 set_addr)))
2102 return -1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002103
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002104 if (first && nl80211_get_ifmode(bss) == NL80211_IFTYPE_AP)
2105 drv->start_mode_ap = 1;
2106
Dmitry Shmidt03658832014-08-13 11:03:49 -07002107 if (drv->hostapd || bss->static_ap)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002108 nlmode = NL80211_IFTYPE_AP;
2109 else if (bss->if_dynamic)
2110 nlmode = nl80211_get_ifmode(bss);
2111 else
2112 nlmode = NL80211_IFTYPE_STATION;
2113
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002114 if (wpa_driver_nl80211_set_mode(bss, nlmode) < 0) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002115 wpa_printf(MSG_ERROR, "nl80211: Could not configure driver mode");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002116 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002117 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002118
Dmitry Shmidt98660862014-03-11 17:26:21 -07002119 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002120 nl80211_get_macaddr(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002121
Dmitry Shmidt98660862014-03-11 17:26:21 -07002122 if (!rfkill_is_blocked(drv->rfkill)) {
2123 int ret = i802_set_iface_flags(bss, 1);
2124 if (ret) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002125 wpa_printf(MSG_ERROR, "nl80211: Could not set "
2126 "interface '%s' UP", bss->ifname);
Dmitry Shmidt98660862014-03-11 17:26:21 -07002127 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002128 }
Dmitry Shmidt98660862014-03-11 17:26:21 -07002129 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
2130 return ret;
2131 } else {
2132 wpa_printf(MSG_DEBUG, "nl80211: Could not yet enable "
2133 "interface '%s' due to rfkill", bss->ifname);
2134 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
2135 return 0;
2136 drv->if_disabled = 1;
2137 send_rfkill_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002138 }
2139
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002140 if (!drv->hostapd)
2141 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
2142 1, IF_OPER_DORMANT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002143
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002144 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
2145 bss->addr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002146 return -1;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002147 os_memcpy(drv->perm_addr, bss->addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002148
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002149 if (send_rfkill_event) {
2150 eloop_register_timeout(0, 0, wpa_driver_nl80211_send_rfkill,
2151 drv, drv->ctx);
2152 }
2153
2154 return 0;
2155}
2156
2157
2158static int wpa_driver_nl80211_del_beacon(struct wpa_driver_nl80211_data *drv)
2159{
2160 struct nl_msg *msg;
2161
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002162 wpa_printf(MSG_DEBUG, "nl80211: Remove beacon (ifindex=%d)",
2163 drv->ifindex);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002164 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_DEL_BEACON);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002165 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002166}
2167
2168
2169/**
2170 * wpa_driver_nl80211_deinit - Deinitialize nl80211 driver interface
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002171 * @bss: Pointer to private nl80211 data from wpa_driver_nl80211_init()
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002172 *
2173 * Shut down driver interface and processing of driver events. Free
2174 * private data buffer if one was allocated in wpa_driver_nl80211_init().
2175 */
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002176static void wpa_driver_nl80211_deinit(struct i802_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002177{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002178 struct wpa_driver_nl80211_data *drv = bss->drv;
2179
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002180 wpa_printf(MSG_INFO, "nl80211: deinit ifname=%s disabled_11b_rates=%d",
2181 bss->ifname, drv->disabled_11b_rates);
2182
Dmitry Shmidt04949592012-07-19 12:16:46 -07002183 bss->in_deinit = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002184 if (drv->data_tx_status)
2185 eloop_unregister_read_sock(drv->eapol_tx_sock);
2186 if (drv->eapol_tx_sock >= 0)
2187 close(drv->eapol_tx_sock);
2188
2189 if (bss->nl_preq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002190 wpa_driver_nl80211_probe_req_report(bss, 0);
2191 if (bss->added_if_into_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002192 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
2193 bss->ifname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002194 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
2195 "interface %s from bridge %s: %s",
2196 bss->ifname, bss->brname, strerror(errno));
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002197 if (drv->rtnl_sk)
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07002198 nl80211_handle_destroy(drv->rtnl_sk);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002199 }
2200 if (bss->added_bridge) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002201 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->brname,
2202 0) < 0)
2203 wpa_printf(MSG_INFO,
2204 "nl80211: Could not set bridge %s down",
2205 bss->brname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002206 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002207 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
2208 "bridge %s: %s",
2209 bss->brname, strerror(errno));
2210 }
2211
2212 nl80211_remove_monitor_interface(drv);
2213
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002214 if (is_ap_interface(drv->nlmode))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002215 wpa_driver_nl80211_del_beacon(drv);
2216
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002217 if (drv->eapol_sock >= 0) {
2218 eloop_unregister_read_sock(drv->eapol_sock);
2219 close(drv->eapol_sock);
2220 }
2221
2222 if (drv->if_indices != drv->default_if_indices)
2223 os_free(drv->if_indices);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002224
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002225 if (drv->disabled_11b_rates)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002226 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
2227
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002228 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, 0,
2229 IF_OPER_UP);
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07002230 eloop_cancel_timeout(wpa_driver_nl80211_send_rfkill, drv, drv->ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002231 rfkill_deinit(drv->rfkill);
2232
2233 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
2234
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002235 if (!drv->start_iface_up)
2236 (void) i802_set_iface_flags(bss, 0);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002237
2238 if (drv->addr_changed) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002239 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname,
2240 0) < 0) {
2241 wpa_printf(MSG_DEBUG,
2242 "nl80211: Could not set interface down to restore permanent MAC address");
2243 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002244 if (linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
2245 drv->perm_addr) < 0) {
2246 wpa_printf(MSG_DEBUG,
2247 "nl80211: Could not restore permanent MAC address");
2248 }
2249 }
2250
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002251 if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002252 if (!drv->hostapd || !drv->start_mode_ap)
2253 wpa_driver_nl80211_set_mode(bss,
2254 NL80211_IFTYPE_STATION);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002255 nl80211_mgmt_unsubscribe(bss, "deinit");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002256 } else {
2257 nl80211_mgmt_unsubscribe(bss, "deinit");
2258 nl80211_del_p2pdev(bss);
2259 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002260
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002261 nl80211_destroy_bss(drv->first_bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002262
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002263 os_free(drv->filter_ssids);
2264
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002265 os_free(drv->auth_ie);
2266
2267 if (drv->in_interface_list)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002268 dl_list_del(&drv->list);
2269
Dmitry Shmidt444d5672013-04-01 13:08:44 -07002270 os_free(drv->extended_capa);
2271 os_free(drv->extended_capa_mask);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002272 os_free(drv->first_bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002273 os_free(drv);
2274}
2275
2276
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002277static u32 wpa_alg_to_cipher_suite(enum wpa_alg alg, size_t key_len)
2278{
2279 switch (alg) {
2280 case WPA_ALG_WEP:
2281 if (key_len == 5)
2282 return WLAN_CIPHER_SUITE_WEP40;
2283 return WLAN_CIPHER_SUITE_WEP104;
2284 case WPA_ALG_TKIP:
2285 return WLAN_CIPHER_SUITE_TKIP;
2286 case WPA_ALG_CCMP:
2287 return WLAN_CIPHER_SUITE_CCMP;
2288 case WPA_ALG_GCMP:
2289 return WLAN_CIPHER_SUITE_GCMP;
2290 case WPA_ALG_CCMP_256:
2291 return WLAN_CIPHER_SUITE_CCMP_256;
2292 case WPA_ALG_GCMP_256:
2293 return WLAN_CIPHER_SUITE_GCMP_256;
2294 case WPA_ALG_IGTK:
2295 return WLAN_CIPHER_SUITE_AES_CMAC;
2296 case WPA_ALG_BIP_GMAC_128:
2297 return WLAN_CIPHER_SUITE_BIP_GMAC_128;
2298 case WPA_ALG_BIP_GMAC_256:
2299 return WLAN_CIPHER_SUITE_BIP_GMAC_256;
2300 case WPA_ALG_BIP_CMAC_256:
2301 return WLAN_CIPHER_SUITE_BIP_CMAC_256;
2302 case WPA_ALG_SMS4:
2303 return WLAN_CIPHER_SUITE_SMS4;
2304 case WPA_ALG_KRK:
2305 return WLAN_CIPHER_SUITE_KRK;
2306 case WPA_ALG_NONE:
2307 case WPA_ALG_PMK:
2308 wpa_printf(MSG_ERROR, "nl80211: Unexpected encryption algorithm %d",
2309 alg);
2310 return 0;
2311 }
2312
2313 wpa_printf(MSG_ERROR, "nl80211: Unsupported encryption algorithm %d",
2314 alg);
2315 return 0;
2316}
2317
2318
2319static u32 wpa_cipher_to_cipher_suite(unsigned int cipher)
2320{
2321 switch (cipher) {
2322 case WPA_CIPHER_CCMP_256:
2323 return WLAN_CIPHER_SUITE_CCMP_256;
2324 case WPA_CIPHER_GCMP_256:
2325 return WLAN_CIPHER_SUITE_GCMP_256;
2326 case WPA_CIPHER_CCMP:
2327 return WLAN_CIPHER_SUITE_CCMP;
2328 case WPA_CIPHER_GCMP:
2329 return WLAN_CIPHER_SUITE_GCMP;
2330 case WPA_CIPHER_TKIP:
2331 return WLAN_CIPHER_SUITE_TKIP;
2332 case WPA_CIPHER_WEP104:
2333 return WLAN_CIPHER_SUITE_WEP104;
2334 case WPA_CIPHER_WEP40:
2335 return WLAN_CIPHER_SUITE_WEP40;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002336 case WPA_CIPHER_GTK_NOT_USED:
2337 return WLAN_CIPHER_SUITE_NO_GROUP_ADDR;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002338 }
2339
2340 return 0;
2341}
2342
2343
2344static int wpa_cipher_to_cipher_suites(unsigned int ciphers, u32 suites[],
2345 int max_suites)
2346{
2347 int num_suites = 0;
2348
2349 if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP_256)
2350 suites[num_suites++] = WLAN_CIPHER_SUITE_CCMP_256;
2351 if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP_256)
2352 suites[num_suites++] = WLAN_CIPHER_SUITE_GCMP_256;
2353 if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP)
2354 suites[num_suites++] = WLAN_CIPHER_SUITE_CCMP;
2355 if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP)
2356 suites[num_suites++] = WLAN_CIPHER_SUITE_GCMP;
2357 if (num_suites < max_suites && ciphers & WPA_CIPHER_TKIP)
2358 suites[num_suites++] = WLAN_CIPHER_SUITE_TKIP;
2359 if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP104)
2360 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP104;
2361 if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP40)
2362 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP40;
2363
2364 return num_suites;
2365}
2366
2367
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002368static int issue_key_mgmt_set_key(struct wpa_driver_nl80211_data *drv,
2369 const u8 *key, size_t key_len)
2370{
2371 struct nl_msg *msg;
2372 int ret;
2373
2374 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_KEY_MGMT_OFFLOAD))
2375 return 0;
2376
2377 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
2378 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
2379 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
2380 QCA_NL80211_VENDOR_SUBCMD_KEY_MGMT_SET_KEY) ||
2381 nla_put(msg, NL80211_ATTR_VENDOR_DATA, key_len, key)) {
2382 nl80211_nlmsg_clear(msg);
2383 nlmsg_free(msg);
2384 return -1;
2385 }
2386 ret = send_and_recv_msgs(drv, msg, NULL, (void *) -1);
2387 if (ret) {
2388 wpa_printf(MSG_DEBUG,
2389 "nl80211: Key management set key failed: ret=%d (%s)",
2390 ret, strerror(-ret));
2391 }
2392
2393 return ret;
2394}
2395
2396
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002397static int wpa_driver_nl80211_set_key(const char *ifname, struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002398 enum wpa_alg alg, const u8 *addr,
2399 int key_idx, int set_tx,
2400 const u8 *seq, size_t seq_len,
2401 const u8 *key, size_t key_len)
2402{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002403 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002404 int ifindex;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002405 struct nl_msg *msg;
2406 int ret;
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07002407 int tdls = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002408
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002409 /* Ignore for P2P Device */
2410 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
2411 return 0;
2412
2413 ifindex = if_nametoindex(ifname);
2414 wpa_printf(MSG_DEBUG, "%s: ifindex=%d (%s) alg=%d addr=%p key_idx=%d "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002415 "set_tx=%d seq_len=%lu key_len=%lu",
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002416 __func__, ifindex, ifname, alg, addr, key_idx, set_tx,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002417 (unsigned long) seq_len, (unsigned long) key_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002418#ifdef CONFIG_TDLS
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07002419 if (key_idx == -1) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002420 key_idx = 0;
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07002421 tdls = 1;
2422 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002423#endif /* CONFIG_TDLS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002424
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002425 if (alg == WPA_ALG_PMK &&
2426 (drv->capa.flags & WPA_DRIVER_FLAGS_KEY_MGMT_OFFLOAD)) {
2427 wpa_printf(MSG_DEBUG, "%s: calling issue_key_mgmt_set_key",
2428 __func__);
2429 ret = issue_key_mgmt_set_key(drv, key, key_len);
2430 return ret;
2431 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002432
2433 if (alg == WPA_ALG_NONE) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002434 msg = nl80211_ifindex_msg(drv, ifindex, 0, NL80211_CMD_DEL_KEY);
2435 if (!msg)
2436 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002437 } else {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002438 msg = nl80211_ifindex_msg(drv, ifindex, 0, NL80211_CMD_NEW_KEY);
2439 if (!msg ||
2440 nla_put(msg, NL80211_ATTR_KEY_DATA, key_len, key) ||
2441 nla_put_u32(msg, NL80211_ATTR_KEY_CIPHER,
2442 wpa_alg_to_cipher_suite(alg, key_len)))
2443 goto fail;
Dmitry Shmidt98660862014-03-11 17:26:21 -07002444 wpa_hexdump_key(MSG_DEBUG, "nl80211: KEY_DATA", key, key_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002445 }
2446
Dmitry Shmidt98660862014-03-11 17:26:21 -07002447 if (seq && seq_len) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002448 if (nla_put(msg, NL80211_ATTR_KEY_SEQ, seq_len, seq))
2449 goto fail;
Dmitry Shmidt98660862014-03-11 17:26:21 -07002450 wpa_hexdump(MSG_DEBUG, "nl80211: KEY_SEQ", seq, seq_len);
2451 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002452
2453 if (addr && !is_broadcast_ether_addr(addr)) {
2454 wpa_printf(MSG_DEBUG, " addr=" MACSTR, MAC2STR(addr));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002455 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
2456 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002457
2458 if (alg != WPA_ALG_WEP && key_idx && !set_tx) {
2459 wpa_printf(MSG_DEBUG, " RSN IBSS RX GTK");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002460 if (nla_put_u32(msg, NL80211_ATTR_KEY_TYPE,
2461 NL80211_KEYTYPE_GROUP))
2462 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002463 }
2464 } else if (addr && is_broadcast_ether_addr(addr)) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002465 struct nlattr *types;
2466
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002467 wpa_printf(MSG_DEBUG, " broadcast key");
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002468
2469 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002470 if (!types ||
2471 nla_put_flag(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST))
2472 goto fail;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002473 nla_nest_end(msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002474 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002475 if (nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
2476 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002477
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002478 ret = send_and_recv_msgs(drv, msg, NULL, key ? (void *) -1 : NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002479 if ((ret == -ENOENT || ret == -ENOLINK) && alg == WPA_ALG_NONE)
2480 ret = 0;
2481 if (ret)
2482 wpa_printf(MSG_DEBUG, "nl80211: set_key failed; err=%d %s)",
2483 ret, strerror(-ret));
2484
2485 /*
2486 * If we failed or don't need to set the default TX key (below),
2487 * we're done here.
2488 */
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07002489 if (ret || !set_tx || alg == WPA_ALG_NONE || tdls)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002490 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002491 if (is_ap_interface(drv->nlmode) && addr &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002492 !is_broadcast_ether_addr(addr))
2493 return ret;
2494
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002495 msg = nl80211_ifindex_msg(drv, ifindex, 0, NL80211_CMD_SET_KEY);
2496 if (!msg ||
2497 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx) ||
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002498 nla_put_flag(msg, (alg == WPA_ALG_IGTK ||
2499 alg == WPA_ALG_BIP_GMAC_128 ||
2500 alg == WPA_ALG_BIP_GMAC_256 ||
2501 alg == WPA_ALG_BIP_CMAC_256) ?
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002502 NL80211_ATTR_KEY_DEFAULT_MGMT :
2503 NL80211_ATTR_KEY_DEFAULT))
2504 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002505 if (addr && is_broadcast_ether_addr(addr)) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002506 struct nlattr *types;
2507
2508 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002509 if (!types ||
2510 nla_put_flag(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST))
2511 goto fail;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002512 nla_nest_end(msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002513 } else if (addr) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002514 struct nlattr *types;
2515
2516 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002517 if (!types ||
2518 nla_put_flag(msg, NL80211_KEY_DEFAULT_TYPE_UNICAST))
2519 goto fail;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002520 nla_nest_end(msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002521 }
2522
2523 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
2524 if (ret == -ENOENT)
2525 ret = 0;
2526 if (ret)
2527 wpa_printf(MSG_DEBUG, "nl80211: set_key default failed; "
2528 "err=%d %s)", ret, strerror(-ret));
2529 return ret;
2530
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002531fail:
2532 nl80211_nlmsg_clear(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002533 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002534 return -ENOBUFS;
2535}
2536
2537
2538static int nl_add_key(struct nl_msg *msg, enum wpa_alg alg,
2539 int key_idx, int defkey,
2540 const u8 *seq, size_t seq_len,
2541 const u8 *key, size_t key_len)
2542{
2543 struct nlattr *key_attr = nla_nest_start(msg, NL80211_ATTR_KEY);
2544 if (!key_attr)
2545 return -1;
2546
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002547 if (defkey && alg == WPA_ALG_IGTK) {
2548 if (nla_put_flag(msg, NL80211_KEY_DEFAULT_MGMT))
2549 return -1;
2550 } else if (defkey) {
2551 if (nla_put_flag(msg, NL80211_KEY_DEFAULT))
2552 return -1;
2553 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002554
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002555 if (nla_put_u8(msg, NL80211_KEY_IDX, key_idx) ||
2556 nla_put_u32(msg, NL80211_KEY_CIPHER,
2557 wpa_alg_to_cipher_suite(alg, key_len)) ||
2558 (seq && seq_len &&
2559 nla_put(msg, NL80211_KEY_SEQ, seq_len, seq)) ||
2560 nla_put(msg, NL80211_KEY_DATA, key_len, key))
2561 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002562
2563 nla_nest_end(msg, key_attr);
2564
2565 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002566}
2567
2568
2569static int nl80211_set_conn_keys(struct wpa_driver_associate_params *params,
2570 struct nl_msg *msg)
2571{
2572 int i, privacy = 0;
2573 struct nlattr *nl_keys, *nl_key;
2574
2575 for (i = 0; i < 4; i++) {
2576 if (!params->wep_key[i])
2577 continue;
2578 privacy = 1;
2579 break;
2580 }
2581 if (params->wps == WPS_MODE_PRIVACY)
2582 privacy = 1;
2583 if (params->pairwise_suite &&
2584 params->pairwise_suite != WPA_CIPHER_NONE)
2585 privacy = 1;
2586
2587 if (!privacy)
2588 return 0;
2589
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002590 if (nla_put_flag(msg, NL80211_ATTR_PRIVACY))
2591 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002592
2593 nl_keys = nla_nest_start(msg, NL80211_ATTR_KEYS);
2594 if (!nl_keys)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002595 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002596
2597 for (i = 0; i < 4; i++) {
2598 if (!params->wep_key[i])
2599 continue;
2600
2601 nl_key = nla_nest_start(msg, i);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002602 if (!nl_key ||
2603 nla_put(msg, NL80211_KEY_DATA, params->wep_key_len[i],
2604 params->wep_key[i]) ||
2605 nla_put_u32(msg, NL80211_KEY_CIPHER,
2606 params->wep_key_len[i] == 5 ?
2607 WLAN_CIPHER_SUITE_WEP40 :
2608 WLAN_CIPHER_SUITE_WEP104) ||
2609 nla_put_u8(msg, NL80211_KEY_IDX, i) ||
2610 (i == params->wep_tx_keyidx &&
2611 nla_put_flag(msg, NL80211_KEY_DEFAULT)))
2612 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002613
2614 nla_nest_end(msg, nl_key);
2615 }
2616 nla_nest_end(msg, nl_keys);
2617
2618 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002619}
2620
2621
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002622int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
2623 const u8 *addr, int cmd, u16 reason_code,
2624 int local_state_change)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002625{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002626 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002627 struct nl_msg *msg;
2628
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002629 if (!(msg = nl80211_drv_msg(drv, 0, cmd)) ||
2630 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code) ||
2631 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
2632 (local_state_change &&
2633 nla_put_flag(msg, NL80211_ATTR_LOCAL_STATE_CHANGE))) {
2634 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002635 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002636 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002637
2638 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002639 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002640 wpa_dbg(drv->ctx, MSG_DEBUG,
2641 "nl80211: MLME command failed: reason=%u ret=%d (%s)",
2642 reason_code, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002643 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002644 return ret;
2645}
2646
2647
2648static int wpa_driver_nl80211_disconnect(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002649 int reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002650{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002651 int ret;
2652
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002653 wpa_printf(MSG_DEBUG, "%s(reason_code=%d)", __func__, reason_code);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07002654 nl80211_mark_disconnected(drv);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002655 /* Disconnect command doesn't need BSSID - it uses cached value */
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002656 ret = wpa_driver_nl80211_mlme(drv, NULL, NL80211_CMD_DISCONNECT,
2657 reason_code, 0);
2658 /*
2659 * For locally generated disconnect, supplicant already generates a
2660 * DEAUTH event, so ignore the event from NL80211.
2661 */
2662 drv->ignore_next_local_disconnect = ret == 0;
2663
2664 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002665}
2666
2667
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002668static int wpa_driver_nl80211_deauthenticate(struct i802_bss *bss,
2669 const u8 *addr, int reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002670{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002671 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07002672 int ret;
Dmitry Shmidt413dde72014-04-11 10:23:22 -07002673
2674 if (drv->nlmode == NL80211_IFTYPE_ADHOC) {
2675 nl80211_mark_disconnected(drv);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002676 return nl80211_leave_ibss(drv, 1);
Dmitry Shmidt413dde72014-04-11 10:23:22 -07002677 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002678 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME))
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002679 return wpa_driver_nl80211_disconnect(drv, reason_code);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002680 wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " reason_code=%d)",
2681 __func__, MAC2STR(addr), reason_code);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07002682 nl80211_mark_disconnected(drv);
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07002683 ret = wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DEAUTHENTICATE,
2684 reason_code, 0);
2685 /*
2686 * For locally generated deauthenticate, supplicant already generates a
2687 * DEAUTH event, so ignore the event from NL80211.
2688 */
2689 drv->ignore_next_local_deauth = ret == 0;
2690 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002691}
2692
2693
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002694static void nl80211_copy_auth_params(struct wpa_driver_nl80211_data *drv,
2695 struct wpa_driver_auth_params *params)
2696{
2697 int i;
2698
2699 drv->auth_freq = params->freq;
2700 drv->auth_alg = params->auth_alg;
2701 drv->auth_wep_tx_keyidx = params->wep_tx_keyidx;
2702 drv->auth_local_state_change = params->local_state_change;
2703 drv->auth_p2p = params->p2p;
2704
2705 if (params->bssid)
2706 os_memcpy(drv->auth_bssid_, params->bssid, ETH_ALEN);
2707 else
2708 os_memset(drv->auth_bssid_, 0, ETH_ALEN);
2709
2710 if (params->ssid) {
2711 os_memcpy(drv->auth_ssid, params->ssid, params->ssid_len);
2712 drv->auth_ssid_len = params->ssid_len;
2713 } else
2714 drv->auth_ssid_len = 0;
2715
2716
2717 os_free(drv->auth_ie);
2718 drv->auth_ie = NULL;
2719 drv->auth_ie_len = 0;
2720 if (params->ie) {
2721 drv->auth_ie = os_malloc(params->ie_len);
2722 if (drv->auth_ie) {
2723 os_memcpy(drv->auth_ie, params->ie, params->ie_len);
2724 drv->auth_ie_len = params->ie_len;
2725 }
2726 }
2727
2728 for (i = 0; i < 4; i++) {
2729 if (params->wep_key[i] && params->wep_key_len[i] &&
2730 params->wep_key_len[i] <= 16) {
2731 os_memcpy(drv->auth_wep_key[i], params->wep_key[i],
2732 params->wep_key_len[i]);
2733 drv->auth_wep_key_len[i] = params->wep_key_len[i];
2734 } else
2735 drv->auth_wep_key_len[i] = 0;
2736 }
2737}
2738
2739
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002740static void nl80211_unmask_11b_rates(struct i802_bss *bss)
2741{
2742 struct wpa_driver_nl80211_data *drv = bss->drv;
2743
2744 if (is_p2p_net_interface(drv->nlmode) || !drv->disabled_11b_rates)
2745 return;
2746
2747 /*
2748 * Looks like we failed to unmask 11b rates previously. This could
2749 * happen, e.g., if the interface was down at the point in time when a
2750 * P2P group was terminated.
2751 */
2752 wpa_printf(MSG_DEBUG,
2753 "nl80211: Interface %s mode is for non-P2P, but 11b rates were disabled - re-enable them",
2754 bss->ifname);
2755 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
2756}
2757
2758
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002759static int wpa_driver_nl80211_authenticate(
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002760 struct i802_bss *bss, struct wpa_driver_auth_params *params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002761{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002762 struct wpa_driver_nl80211_data *drv = bss->drv;
2763 int ret = -1, i;
2764 struct nl_msg *msg;
2765 enum nl80211_auth_type type;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002766 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002767 int count = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002768 int is_retry;
2769
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002770 nl80211_unmask_11b_rates(bss);
2771
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002772 is_retry = drv->retry_auth;
2773 drv->retry_auth = 0;
Dmitry Shmidt98660862014-03-11 17:26:21 -07002774 drv->ignore_deauth_event = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002775
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07002776 nl80211_mark_disconnected(drv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002777 os_memset(drv->auth_bssid, 0, ETH_ALEN);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07002778 if (params->bssid)
2779 os_memcpy(drv->auth_attempt_bssid, params->bssid, ETH_ALEN);
2780 else
2781 os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002782 /* FIX: IBSS mode */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002783 nlmode = params->p2p ?
2784 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
2785 if (drv->nlmode != nlmode &&
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002786 wpa_driver_nl80211_set_mode(bss, nlmode) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002787 return -1;
2788
2789retry:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002790 wpa_printf(MSG_DEBUG, "nl80211: Authenticate (ifindex=%d)",
2791 drv->ifindex);
2792
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002793 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_AUTHENTICATE);
2794 if (!msg)
2795 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002796
2797 for (i = 0; i < 4; i++) {
2798 if (!params->wep_key[i])
2799 continue;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002800 wpa_driver_nl80211_set_key(bss->ifname, bss, WPA_ALG_WEP,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002801 NULL, i,
2802 i == params->wep_tx_keyidx, NULL, 0,
2803 params->wep_key[i],
2804 params->wep_key_len[i]);
2805 if (params->wep_tx_keyidx != i)
2806 continue;
2807 if (nl_add_key(msg, WPA_ALG_WEP, i, 1, NULL, 0,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002808 params->wep_key[i], params->wep_key_len[i]))
2809 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002810 }
2811
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002812 if (params->bssid) {
2813 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
2814 MAC2STR(params->bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002815 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid))
2816 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002817 }
2818 if (params->freq) {
2819 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002820 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq))
2821 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002822 }
2823 if (params->ssid) {
2824 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
2825 params->ssid, params->ssid_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002826 if (nla_put(msg, NL80211_ATTR_SSID, params->ssid_len,
2827 params->ssid))
2828 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002829 }
2830 wpa_hexdump(MSG_DEBUG, " * IEs", params->ie, params->ie_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002831 if (params->ie &&
2832 nla_put(msg, NL80211_ATTR_IE, params->ie_len, params->ie))
2833 goto fail;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002834 if (params->sae_data) {
2835 wpa_hexdump(MSG_DEBUG, " * SAE data", params->sae_data,
2836 params->sae_data_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002837 if (nla_put(msg, NL80211_ATTR_SAE_DATA, params->sae_data_len,
2838 params->sae_data))
2839 goto fail;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002840 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002841 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
2842 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
2843 else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
2844 type = NL80211_AUTHTYPE_SHARED_KEY;
2845 else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
2846 type = NL80211_AUTHTYPE_NETWORK_EAP;
2847 else if (params->auth_alg & WPA_AUTH_ALG_FT)
2848 type = NL80211_AUTHTYPE_FT;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002849 else if (params->auth_alg & WPA_AUTH_ALG_SAE)
2850 type = NL80211_AUTHTYPE_SAE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002851 else
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002852 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002853 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002854 if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE, type))
2855 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002856 if (params->local_state_change) {
2857 wpa_printf(MSG_DEBUG, " * Local state change only");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002858 if (nla_put_flag(msg, NL80211_ATTR_LOCAL_STATE_CHANGE))
2859 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002860 }
2861
2862 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
2863 msg = NULL;
2864 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002865 wpa_dbg(drv->ctx, MSG_DEBUG,
2866 "nl80211: MLME command failed (auth): ret=%d (%s)",
2867 ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002868 count++;
2869 if (ret == -EALREADY && count == 1 && params->bssid &&
2870 !params->local_state_change) {
2871 /*
2872 * mac80211 does not currently accept new
2873 * authentication if we are already authenticated. As a
2874 * workaround, force deauthentication and try again.
2875 */
2876 wpa_printf(MSG_DEBUG, "nl80211: Retry authentication "
2877 "after forced deauthentication");
Dmitry Shmidt98660862014-03-11 17:26:21 -07002878 drv->ignore_deauth_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002879 wpa_driver_nl80211_deauthenticate(
2880 bss, params->bssid,
2881 WLAN_REASON_PREV_AUTH_NOT_VALID);
2882 nlmsg_free(msg);
2883 goto retry;
2884 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002885
2886 if (ret == -ENOENT && params->freq && !is_retry) {
2887 /*
2888 * cfg80211 has likely expired the BSS entry even
2889 * though it was previously available in our internal
2890 * BSS table. To recover quickly, start a single
2891 * channel scan on the specified channel.
2892 */
2893 struct wpa_driver_scan_params scan;
2894 int freqs[2];
2895
2896 os_memset(&scan, 0, sizeof(scan));
2897 scan.num_ssids = 1;
2898 if (params->ssid) {
2899 scan.ssids[0].ssid = params->ssid;
2900 scan.ssids[0].ssid_len = params->ssid_len;
2901 }
2902 freqs[0] = params->freq;
2903 freqs[1] = 0;
2904 scan.freqs = freqs;
2905 wpa_printf(MSG_DEBUG, "nl80211: Trigger single "
2906 "channel scan to refresh cfg80211 BSS "
2907 "entry");
2908 ret = wpa_driver_nl80211_scan(bss, &scan);
2909 if (ret == 0) {
2910 nl80211_copy_auth_params(drv, params);
2911 drv->scan_for_auth = 1;
2912 }
2913 } else if (is_retry) {
2914 /*
2915 * Need to indicate this with an event since the return
2916 * value from the retry is not delivered to core code.
2917 */
2918 union wpa_event_data event;
2919 wpa_printf(MSG_DEBUG, "nl80211: Authentication retry "
2920 "failed");
2921 os_memset(&event, 0, sizeof(event));
2922 os_memcpy(event.timeout_event.addr, drv->auth_bssid_,
2923 ETH_ALEN);
2924 wpa_supplicant_event(drv->ctx, EVENT_AUTH_TIMED_OUT,
2925 &event);
2926 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002927 } else {
2928 wpa_printf(MSG_DEBUG,
2929 "nl80211: Authentication request send successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002930 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002931
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002932fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002933 nlmsg_free(msg);
2934 return ret;
2935}
2936
2937
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002938int wpa_driver_nl80211_authenticate_retry(struct wpa_driver_nl80211_data *drv)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002939{
2940 struct wpa_driver_auth_params params;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002941 struct i802_bss *bss = drv->first_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002942 int i;
2943
2944 wpa_printf(MSG_DEBUG, "nl80211: Try to authenticate again");
2945
2946 os_memset(&params, 0, sizeof(params));
2947 params.freq = drv->auth_freq;
2948 params.auth_alg = drv->auth_alg;
2949 params.wep_tx_keyidx = drv->auth_wep_tx_keyidx;
2950 params.local_state_change = drv->auth_local_state_change;
2951 params.p2p = drv->auth_p2p;
2952
2953 if (!is_zero_ether_addr(drv->auth_bssid_))
2954 params.bssid = drv->auth_bssid_;
2955
2956 if (drv->auth_ssid_len) {
2957 params.ssid = drv->auth_ssid;
2958 params.ssid_len = drv->auth_ssid_len;
2959 }
2960
2961 params.ie = drv->auth_ie;
2962 params.ie_len = drv->auth_ie_len;
2963
2964 for (i = 0; i < 4; i++) {
2965 if (drv->auth_wep_key_len[i]) {
2966 params.wep_key[i] = drv->auth_wep_key[i];
2967 params.wep_key_len[i] = drv->auth_wep_key_len[i];
2968 }
2969 }
2970
2971 drv->retry_auth = 1;
2972 return wpa_driver_nl80211_authenticate(bss, &params);
2973}
2974
2975
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002976static int wpa_driver_nl80211_send_frame(struct i802_bss *bss,
2977 const void *data, size_t len,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002978 int encrypt, int noack,
2979 unsigned int freq, int no_cck,
2980 int offchanok, unsigned int wait_time)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002981{
2982 struct wpa_driver_nl80211_data *drv = bss->drv;
2983 u64 cookie;
Dmitry Shmidt051af732013-10-22 13:52:46 -07002984 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002985
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07002986 if (freq == 0 && drv->nlmode == NL80211_IFTYPE_ADHOC) {
2987 freq = nl80211_get_assoc_freq(drv);
2988 wpa_printf(MSG_DEBUG,
2989 "nl80211: send_frame - Use assoc_freq=%u for IBSS",
2990 freq);
2991 }
Dmitry Shmidt56052862013-10-04 10:23:25 -07002992 if (freq == 0) {
2993 wpa_printf(MSG_DEBUG, "nl80211: send_frame - Use bss->freq=%u",
2994 bss->freq);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002995 freq = bss->freq;
Dmitry Shmidt56052862013-10-04 10:23:25 -07002996 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002997
Dmitry Shmidt56052862013-10-04 10:23:25 -07002998 if (drv->use_monitor) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002999 wpa_printf(MSG_DEBUG, "nl80211: send_frame(freq=%u bss->freq=%u) -> send_monitor",
Dmitry Shmidt56052862013-10-04 10:23:25 -07003000 freq, bss->freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003001 return nl80211_send_monitor(drv, data, len, encrypt, noack);
Dmitry Shmidt56052862013-10-04 10:23:25 -07003002 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003003
Dmitry Shmidt56052862013-10-04 10:23:25 -07003004 wpa_printf(MSG_DEBUG, "nl80211: send_frame -> send_frame_cmd");
Dmitry Shmidt051af732013-10-22 13:52:46 -07003005 res = nl80211_send_frame_cmd(bss, freq, wait_time, data, len,
3006 &cookie, no_cck, noack, offchanok);
3007 if (res == 0 && !noack) {
3008 const struct ieee80211_mgmt *mgmt;
3009 u16 fc;
3010
3011 mgmt = (const struct ieee80211_mgmt *) data;
3012 fc = le_to_host16(mgmt->frame_control);
3013 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
3014 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_ACTION) {
3015 wpa_printf(MSG_MSGDUMP,
3016 "nl80211: Update send_action_cookie from 0x%llx to 0x%llx",
3017 (long long unsigned int)
3018 drv->send_action_cookie,
3019 (long long unsigned int) cookie);
3020 drv->send_action_cookie = cookie;
3021 }
3022 }
3023
3024 return res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003025}
3026
3027
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003028static int wpa_driver_nl80211_send_mlme(struct i802_bss *bss, const u8 *data,
3029 size_t data_len, int noack,
3030 unsigned int freq, int no_cck,
3031 int offchanok,
3032 unsigned int wait_time)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003033{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003034 struct wpa_driver_nl80211_data *drv = bss->drv;
3035 struct ieee80211_mgmt *mgmt;
3036 int encrypt = 1;
3037 u16 fc;
3038
3039 mgmt = (struct ieee80211_mgmt *) data;
3040 fc = le_to_host16(mgmt->frame_control);
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07003041 wpa_printf(MSG_DEBUG, "nl80211: send_mlme - da= " MACSTR
3042 " noack=%d freq=%u no_cck=%d offchanok=%d wait_time=%u fc=0x%x (%s) nlmode=%d",
3043 MAC2STR(mgmt->da), noack, freq, no_cck, offchanok, wait_time,
3044 fc, fc2str(fc), drv->nlmode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003045
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003046 if ((is_sta_interface(drv->nlmode) ||
3047 drv->nlmode == NL80211_IFTYPE_P2P_DEVICE) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003048 WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
3049 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_RESP) {
3050 /*
3051 * The use of last_mgmt_freq is a bit of a hack,
3052 * but it works due to the single-threaded nature
3053 * of wpa_supplicant.
3054 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07003055 if (freq == 0) {
3056 wpa_printf(MSG_DEBUG, "nl80211: Use last_mgmt_freq=%d",
3057 drv->last_mgmt_freq);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003058 freq = drv->last_mgmt_freq;
Dmitry Shmidt56052862013-10-04 10:23:25 -07003059 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003060 return nl80211_send_frame_cmd(bss, freq, 0,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003061 data, data_len, NULL, 1, noack,
3062 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003063 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003064
3065 if (drv->device_ap_sme && is_ap_interface(drv->nlmode)) {
Dmitry Shmidt56052862013-10-04 10:23:25 -07003066 if (freq == 0) {
3067 wpa_printf(MSG_DEBUG, "nl80211: Use bss->freq=%d",
3068 bss->freq);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003069 freq = bss->freq;
Dmitry Shmidt56052862013-10-04 10:23:25 -07003070 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003071 return nl80211_send_frame_cmd(bss, freq,
3072 (int) freq == bss->freq ? 0 :
3073 wait_time,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003074 data, data_len,
3075 &drv->send_action_cookie,
3076 no_cck, noack, offchanok);
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07003077 }
Dmitry Shmidtb638fe72012-03-20 12:51:25 -07003078
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003079 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
3080 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) {
3081 /*
3082 * Only one of the authentication frame types is encrypted.
3083 * In order for static WEP encryption to work properly (i.e.,
3084 * to not encrypt the frame), we need to tell mac80211 about
3085 * the frames that must not be encrypted.
3086 */
3087 u16 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
3088 u16 auth_trans = le_to_host16(mgmt->u.auth.auth_transaction);
3089 if (auth_alg != WLAN_AUTH_SHARED_KEY || auth_trans != 3)
3090 encrypt = 0;
3091 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003092
Dmitry Shmidt56052862013-10-04 10:23:25 -07003093 wpa_printf(MSG_DEBUG, "nl80211: send_mlme -> send_frame");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003094 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003095 noack, freq, no_cck, offchanok,
3096 wait_time);
3097}
3098
3099
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003100static int nl80211_put_basic_rates(struct nl_msg *msg, const int *basic_rates)
3101{
3102 u8 rates[NL80211_MAX_SUPP_RATES];
3103 u8 rates_len = 0;
3104 int i;
3105
3106 if (!basic_rates)
3107 return 0;
3108
3109 for (i = 0; i < NL80211_MAX_SUPP_RATES && basic_rates[i] >= 0; i++)
3110 rates[rates_len++] = basic_rates[i] / 5;
3111
3112 return nla_put(msg, NL80211_ATTR_BSS_BASIC_RATES, rates_len, rates);
3113}
3114
3115
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003116static int nl80211_set_bss(struct i802_bss *bss, int cts, int preamble,
3117 int slot, int ht_opmode, int ap_isolate,
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003118 const int *basic_rates)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003119{
3120 struct wpa_driver_nl80211_data *drv = bss->drv;
3121 struct nl_msg *msg;
3122
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003123 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_BSS)) ||
3124 (cts >= 0 &&
3125 nla_put_u8(msg, NL80211_ATTR_BSS_CTS_PROT, cts)) ||
3126 (preamble >= 0 &&
3127 nla_put_u8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble)) ||
3128 (slot >= 0 &&
3129 nla_put_u8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot)) ||
3130 (ht_opmode >= 0 &&
3131 nla_put_u16(msg, NL80211_ATTR_BSS_HT_OPMODE, ht_opmode)) ||
3132 (ap_isolate >= 0 &&
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003133 nla_put_u8(msg, NL80211_ATTR_AP_ISOLATE, ap_isolate)) ||
3134 nl80211_put_basic_rates(msg, basic_rates)) {
3135 nlmsg_free(msg);
3136 return -ENOBUFS;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003137 }
3138
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003139 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003140}
3141
3142
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003143static int wpa_driver_nl80211_set_acl(void *priv,
3144 struct hostapd_acl_params *params)
3145{
3146 struct i802_bss *bss = priv;
3147 struct wpa_driver_nl80211_data *drv = bss->drv;
3148 struct nl_msg *msg;
3149 struct nlattr *acl;
3150 unsigned int i;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003151 int ret;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003152
3153 if (!(drv->capa.max_acl_mac_addrs))
3154 return -ENOTSUP;
3155
3156 if (params->num_mac_acl > drv->capa.max_acl_mac_addrs)
3157 return -ENOTSUP;
3158
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003159 wpa_printf(MSG_DEBUG, "nl80211: Set %s ACL (num_mac_acl=%u)",
3160 params->acl_policy ? "Accept" : "Deny", params->num_mac_acl);
3161
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003162 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_MAC_ACL)) ||
3163 nla_put_u32(msg, NL80211_ATTR_ACL_POLICY, params->acl_policy ?
3164 NL80211_ACL_POLICY_DENY_UNLESS_LISTED :
3165 NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED) ||
3166 (acl = nla_nest_start(msg, NL80211_ATTR_MAC_ADDRS)) == NULL) {
3167 nlmsg_free(msg);
3168 return -ENOMEM;
3169 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003170
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003171 for (i = 0; i < params->num_mac_acl; i++) {
3172 if (nla_put(msg, i + 1, ETH_ALEN, params->mac_acl[i].addr)) {
3173 nlmsg_free(msg);
3174 return -ENOMEM;
3175 }
3176 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003177
3178 nla_nest_end(msg, acl);
3179
3180 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003181 if (ret) {
3182 wpa_printf(MSG_DEBUG, "nl80211: Failed to set MAC ACL: %d (%s)",
3183 ret, strerror(-ret));
3184 }
3185
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003186 return ret;
3187}
3188
3189
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003190static int nl80211_put_beacon_int(struct nl_msg *msg, int beacon_int)
3191{
3192 if (beacon_int > 0) {
3193 wpa_printf(MSG_DEBUG, " * beacon_int=%d", beacon_int);
3194 return nla_put_u32(msg, NL80211_ATTR_BEACON_INTERVAL,
3195 beacon_int);
3196 }
3197
3198 return 0;
3199}
3200
3201
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003202static int wpa_driver_nl80211_set_ap(void *priv,
3203 struct wpa_driver_ap_params *params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003204{
3205 struct i802_bss *bss = priv;
3206 struct wpa_driver_nl80211_data *drv = bss->drv;
3207 struct nl_msg *msg;
3208 u8 cmd = NL80211_CMD_NEW_BEACON;
3209 int ret;
3210 int beacon_set;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003211 int num_suites;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003212 int smps_mode;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003213 u32 suites[10], suite;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003214 u32 ver;
3215
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07003216 beacon_set = bss->beacon_set;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003217
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003218 wpa_printf(MSG_DEBUG, "nl80211: Set beacon (beacon_set=%d)",
3219 beacon_set);
3220 if (beacon_set)
3221 cmd = NL80211_CMD_SET_BEACON;
3222
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003223 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon head",
3224 params->head, params->head_len);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003225 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon tail",
3226 params->tail, params->tail_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003227 wpa_printf(MSG_DEBUG, "nl80211: ifindex=%d", bss->ifindex);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003228 wpa_printf(MSG_DEBUG, "nl80211: beacon_int=%d", params->beacon_int);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003229 wpa_printf(MSG_DEBUG, "nl80211: dtim_period=%d", params->dtim_period);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003230 wpa_hexdump_ascii(MSG_DEBUG, "nl80211: ssid",
3231 params->ssid, params->ssid_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003232 if (!(msg = nl80211_bss_msg(bss, 0, cmd)) ||
3233 nla_put(msg, NL80211_ATTR_BEACON_HEAD, params->head_len,
3234 params->head) ||
3235 nla_put(msg, NL80211_ATTR_BEACON_TAIL, params->tail_len,
3236 params->tail) ||
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003237 nl80211_put_beacon_int(msg, params->beacon_int) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003238 nla_put_u32(msg, NL80211_ATTR_DTIM_PERIOD, params->dtim_period) ||
3239 nla_put(msg, NL80211_ATTR_SSID, params->ssid_len, params->ssid))
3240 goto fail;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003241 if (params->proberesp && params->proberesp_len) {
3242 wpa_hexdump(MSG_DEBUG, "nl80211: proberesp (offload)",
3243 params->proberesp, params->proberesp_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003244 if (nla_put(msg, NL80211_ATTR_PROBE_RESP, params->proberesp_len,
3245 params->proberesp))
3246 goto fail;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003247 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003248 switch (params->hide_ssid) {
3249 case NO_SSID_HIDING:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003250 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID not in use");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003251 if (nla_put_u32(msg, NL80211_ATTR_HIDDEN_SSID,
3252 NL80211_HIDDEN_SSID_NOT_IN_USE))
3253 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003254 break;
3255 case HIDDEN_SSID_ZERO_LEN:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003256 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero len");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003257 if (nla_put_u32(msg, NL80211_ATTR_HIDDEN_SSID,
3258 NL80211_HIDDEN_SSID_ZERO_LEN))
3259 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003260 break;
3261 case HIDDEN_SSID_ZERO_CONTENTS:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003262 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero contents");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003263 if (nla_put_u32(msg, NL80211_ATTR_HIDDEN_SSID,
3264 NL80211_HIDDEN_SSID_ZERO_CONTENTS))
3265 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003266 break;
3267 }
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003268 wpa_printf(MSG_DEBUG, "nl80211: privacy=%d", params->privacy);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003269 if (params->privacy &&
3270 nla_put_flag(msg, NL80211_ATTR_PRIVACY))
3271 goto fail;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003272 wpa_printf(MSG_DEBUG, "nl80211: auth_algs=0x%x", params->auth_algs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003273 if ((params->auth_algs & (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) ==
3274 (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) {
3275 /* Leave out the attribute */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003276 } else if (params->auth_algs & WPA_AUTH_ALG_SHARED) {
3277 if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE,
3278 NL80211_AUTHTYPE_SHARED_KEY))
3279 goto fail;
3280 } else {
3281 if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE,
3282 NL80211_AUTHTYPE_OPEN_SYSTEM))
3283 goto fail;
3284 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003285
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003286 wpa_printf(MSG_DEBUG, "nl80211: wpa_version=0x%x", params->wpa_version);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003287 ver = 0;
3288 if (params->wpa_version & WPA_PROTO_WPA)
3289 ver |= NL80211_WPA_VERSION_1;
3290 if (params->wpa_version & WPA_PROTO_RSN)
3291 ver |= NL80211_WPA_VERSION_2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003292 if (ver &&
3293 nla_put_u32(msg, NL80211_ATTR_WPA_VERSIONS, ver))
3294 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003295
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003296 wpa_printf(MSG_DEBUG, "nl80211: key_mgmt_suites=0x%x",
3297 params->key_mgmt_suites);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003298 num_suites = 0;
3299 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X)
3300 suites[num_suites++] = WLAN_AKM_SUITE_8021X;
3301 if (params->key_mgmt_suites & WPA_KEY_MGMT_PSK)
3302 suites[num_suites++] = WLAN_AKM_SUITE_PSK;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003303 if (num_suites &&
3304 nla_put(msg, NL80211_ATTR_AKM_SUITES, num_suites * sizeof(u32),
3305 suites))
3306 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003307
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003308 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
3309 params->pairwise_ciphers & (WPA_CIPHER_WEP104 | WPA_CIPHER_WEP40) &&
3310 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT))
3311 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003312
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003313 wpa_printf(MSG_DEBUG, "nl80211: pairwise_ciphers=0x%x",
3314 params->pairwise_ciphers);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003315 num_suites = wpa_cipher_to_cipher_suites(params->pairwise_ciphers,
3316 suites, ARRAY_SIZE(suites));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003317 if (num_suites &&
3318 nla_put(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
3319 num_suites * sizeof(u32), suites))
3320 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003321
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003322 wpa_printf(MSG_DEBUG, "nl80211: group_cipher=0x%x",
3323 params->group_cipher);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003324 suite = wpa_cipher_to_cipher_suite(params->group_cipher);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003325 if (suite &&
3326 nla_put_u32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, suite))
3327 goto fail;
3328
3329 switch (params->smps_mode) {
3330 case HT_CAP_INFO_SMPS_DYNAMIC:
3331 wpa_printf(MSG_DEBUG, "nl80211: SMPS mode - dynamic");
3332 smps_mode = NL80211_SMPS_DYNAMIC;
3333 break;
3334 case HT_CAP_INFO_SMPS_STATIC:
3335 wpa_printf(MSG_DEBUG, "nl80211: SMPS mode - static");
3336 smps_mode = NL80211_SMPS_STATIC;
3337 break;
3338 default:
3339 /* invalid - fallback to smps off */
3340 case HT_CAP_INFO_SMPS_DISABLED:
3341 wpa_printf(MSG_DEBUG, "nl80211: SMPS mode - off");
3342 smps_mode = NL80211_SMPS_OFF;
3343 break;
3344 }
3345 if (nla_put_u32(msg, NL80211_ATTR_SMPS_MODE, smps_mode))
3346 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003347
3348 if (params->beacon_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003349 wpa_hexdump_buf(MSG_DEBUG, "nl80211: beacon_ies",
3350 params->beacon_ies);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003351 if (nla_put(msg, NL80211_ATTR_IE,
3352 wpabuf_len(params->beacon_ies),
3353 wpabuf_head(params->beacon_ies)))
3354 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003355 }
3356 if (params->proberesp_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003357 wpa_hexdump_buf(MSG_DEBUG, "nl80211: proberesp_ies",
3358 params->proberesp_ies);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003359 if (nla_put(msg, NL80211_ATTR_IE_PROBE_RESP,
3360 wpabuf_len(params->proberesp_ies),
3361 wpabuf_head(params->proberesp_ies)))
3362 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003363 }
3364 if (params->assocresp_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003365 wpa_hexdump_buf(MSG_DEBUG, "nl80211: assocresp_ies",
3366 params->assocresp_ies);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003367 if (nla_put(msg, NL80211_ATTR_IE_ASSOC_RESP,
3368 wpabuf_len(params->assocresp_ies),
3369 wpabuf_head(params->assocresp_ies)))
3370 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003371 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003372
Dmitry Shmidt04949592012-07-19 12:16:46 -07003373 if (drv->capa.flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003374 wpa_printf(MSG_DEBUG, "nl80211: ap_max_inactivity=%d",
3375 params->ap_max_inactivity);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003376 if (nla_put_u16(msg, NL80211_ATTR_INACTIVITY_TIMEOUT,
3377 params->ap_max_inactivity))
3378 goto fail;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003379 }
3380
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003381 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3382 if (ret) {
3383 wpa_printf(MSG_DEBUG, "nl80211: Beacon set failed: %d (%s)",
3384 ret, strerror(-ret));
3385 } else {
3386 bss->beacon_set = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003387 nl80211_set_bss(bss, params->cts_protect, params->preamble,
3388 params->short_slot_time, params->ht_opmode,
3389 params->isolate, params->basic_rates);
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07003390 if (beacon_set && params->freq &&
3391 params->freq->bandwidth != bss->bandwidth) {
3392 wpa_printf(MSG_DEBUG,
3393 "nl80211: Update BSS %s bandwidth: %d -> %d",
3394 bss->ifname, bss->bandwidth,
3395 params->freq->bandwidth);
3396 ret = nl80211_set_channel(bss, params->freq, 1);
3397 if (ret) {
3398 wpa_printf(MSG_DEBUG,
3399 "nl80211: Frequency set failed: %d (%s)",
3400 ret, strerror(-ret));
3401 } else {
3402 wpa_printf(MSG_DEBUG,
3403 "nl80211: Frequency set succeeded for ht2040 coex");
3404 bss->bandwidth = params->freq->bandwidth;
3405 }
3406 } else if (!beacon_set) {
3407 /*
3408 * cfg80211 updates the driver on frequence change in AP
3409 * mode only at the point when beaconing is started, so
3410 * set the initial value here.
3411 */
3412 bss->bandwidth = params->freq->bandwidth;
3413 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003414 }
3415 return ret;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003416fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003417 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003418 return -ENOBUFS;
3419}
3420
3421
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003422static int nl80211_put_freq_params(struct nl_msg *msg,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003423 const struct hostapd_freq_params *freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003424{
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003425 wpa_printf(MSG_DEBUG, " * freq=%d", freq->freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003426 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq->freq))
3427 return -ENOBUFS;
3428
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003429 wpa_printf(MSG_DEBUG, " * vht_enabled=%d", freq->vht_enabled);
3430 wpa_printf(MSG_DEBUG, " * ht_enabled=%d", freq->ht_enabled);
3431
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003432 if (freq->vht_enabled) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003433 enum nl80211_chan_width cw;
3434
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003435 wpa_printf(MSG_DEBUG, " * bandwidth=%d", freq->bandwidth);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003436 switch (freq->bandwidth) {
3437 case 20:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003438 cw = NL80211_CHAN_WIDTH_20;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003439 break;
3440 case 40:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003441 cw = NL80211_CHAN_WIDTH_40;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003442 break;
3443 case 80:
3444 if (freq->center_freq2)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003445 cw = NL80211_CHAN_WIDTH_80P80;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003446 else
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003447 cw = NL80211_CHAN_WIDTH_80;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003448 break;
3449 case 160:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003450 cw = NL80211_CHAN_WIDTH_160;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003451 break;
3452 default:
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003453 return -EINVAL;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003454 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003455
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003456 wpa_printf(MSG_DEBUG, " * channel_width=%d", cw);
3457 wpa_printf(MSG_DEBUG, " * center_freq1=%d",
3458 freq->center_freq1);
3459 wpa_printf(MSG_DEBUG, " * center_freq2=%d",
3460 freq->center_freq2);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003461 if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, cw) ||
3462 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1,
3463 freq->center_freq1) ||
3464 (freq->center_freq2 &&
3465 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2,
3466 freq->center_freq2)))
3467 return -ENOBUFS;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003468 } else if (freq->ht_enabled) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003469 enum nl80211_channel_type ct;
3470
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003471 wpa_printf(MSG_DEBUG, " * sec_channel_offset=%d",
3472 freq->sec_channel_offset);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003473 switch (freq->sec_channel_offset) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003474 case -1:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003475 ct = NL80211_CHAN_HT40MINUS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003476 break;
3477 case 1:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003478 ct = NL80211_CHAN_HT40PLUS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003479 break;
3480 default:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003481 ct = NL80211_CHAN_HT20;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003482 break;
3483 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003484
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003485 wpa_printf(MSG_DEBUG, " * channel_type=%d", ct);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003486 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, ct))
3487 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003488 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003489 return 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003490}
3491
3492
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07003493static int nl80211_set_channel(struct i802_bss *bss,
3494 struct hostapd_freq_params *freq, int set_chan)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003495{
3496 struct wpa_driver_nl80211_data *drv = bss->drv;
3497 struct nl_msg *msg;
3498 int ret;
3499
3500 wpa_printf(MSG_DEBUG,
3501 "nl80211: Set freq %d (ht_enabled=%d, vht_enabled=%d, bandwidth=%d MHz, cf1=%d MHz, cf2=%d MHz)",
3502 freq->freq, freq->ht_enabled, freq->vht_enabled,
3503 freq->bandwidth, freq->center_freq1, freq->center_freq2);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003504
3505 msg = nl80211_drv_msg(drv, 0, set_chan ? NL80211_CMD_SET_CHANNEL :
3506 NL80211_CMD_SET_WIPHY);
3507 if (!msg || nl80211_put_freq_params(msg, freq) < 0) {
3508 nlmsg_free(msg);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003509 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003510 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003511
3512 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003513 if (ret == 0) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003514 bss->freq = freq->freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003515 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003516 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003517 wpa_printf(MSG_DEBUG, "nl80211: Failed to set channel (freq=%d): "
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003518 "%d (%s)", freq->freq, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003519 return -1;
3520}
3521
3522
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003523static u32 sta_flags_nl80211(int flags)
3524{
3525 u32 f = 0;
3526
3527 if (flags & WPA_STA_AUTHORIZED)
3528 f |= BIT(NL80211_STA_FLAG_AUTHORIZED);
3529 if (flags & WPA_STA_WMM)
3530 f |= BIT(NL80211_STA_FLAG_WME);
3531 if (flags & WPA_STA_SHORT_PREAMBLE)
3532 f |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
3533 if (flags & WPA_STA_MFP)
3534 f |= BIT(NL80211_STA_FLAG_MFP);
3535 if (flags & WPA_STA_TDLS_PEER)
3536 f |= BIT(NL80211_STA_FLAG_TDLS_PEER);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003537 if (flags & WPA_STA_AUTHENTICATED)
3538 f |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003539
3540 return f;
3541}
3542
3543
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003544#ifdef CONFIG_MESH
3545static u32 sta_plink_state_nl80211(enum mesh_plink_state state)
3546{
3547 switch (state) {
3548 case PLINK_LISTEN:
3549 return NL80211_PLINK_LISTEN;
3550 case PLINK_OPEN_SENT:
3551 return NL80211_PLINK_OPN_SNT;
3552 case PLINK_OPEN_RCVD:
3553 return NL80211_PLINK_OPN_RCVD;
3554 case PLINK_CNF_RCVD:
3555 return NL80211_PLINK_CNF_RCVD;
3556 case PLINK_ESTAB:
3557 return NL80211_PLINK_ESTAB;
3558 case PLINK_HOLDING:
3559 return NL80211_PLINK_HOLDING;
3560 case PLINK_BLOCKED:
3561 return NL80211_PLINK_BLOCKED;
3562 default:
3563 wpa_printf(MSG_ERROR, "nl80211: Invalid mesh plink state %d",
3564 state);
3565 }
3566 return -1;
3567}
3568#endif /* CONFIG_MESH */
3569
3570
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003571static int wpa_driver_nl80211_sta_add(void *priv,
3572 struct hostapd_sta_add_params *params)
3573{
3574 struct i802_bss *bss = priv;
3575 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003576 struct nl_msg *msg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003577 struct nl80211_sta_flag_update upd;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003578 int ret = -ENOBUFS;
3579
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003580 if ((params->flags & WPA_STA_TDLS_PEER) &&
3581 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
3582 return -EOPNOTSUPP;
3583
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003584 wpa_printf(MSG_DEBUG, "nl80211: %s STA " MACSTR,
3585 params->set ? "Set" : "Add", MAC2STR(params->addr));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003586 msg = nl80211_bss_msg(bss, 0, params->set ? NL80211_CMD_SET_STATION :
3587 NL80211_CMD_NEW_STATION);
3588 if (!msg || nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr))
3589 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003590
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003591 if (!params->set || (params->flags & WPA_STA_TDLS_PEER)) {
3592 wpa_hexdump(MSG_DEBUG, " * supported rates",
3593 params->supp_rates, params->supp_rates_len);
3594 wpa_printf(MSG_DEBUG, " * capability=0x%x",
3595 params->capability);
3596 if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_RATES,
3597 params->supp_rates_len, params->supp_rates) ||
3598 nla_put_u16(msg, NL80211_ATTR_STA_CAPABILITY,
3599 params->capability))
3600 goto fail;
3601
3602 if (params->ht_capabilities) {
3603 wpa_hexdump(MSG_DEBUG, " * ht_capabilities",
3604 (u8 *) params->ht_capabilities,
3605 sizeof(*params->ht_capabilities));
3606 if (nla_put(msg, NL80211_ATTR_HT_CAPABILITY,
3607 sizeof(*params->ht_capabilities),
3608 params->ht_capabilities))
3609 goto fail;
3610 }
3611
3612 if (params->vht_capabilities) {
3613 wpa_hexdump(MSG_DEBUG, " * vht_capabilities",
3614 (u8 *) params->vht_capabilities,
3615 sizeof(*params->vht_capabilities));
3616 if (nla_put(msg, NL80211_ATTR_VHT_CAPABILITY,
3617 sizeof(*params->vht_capabilities),
3618 params->vht_capabilities))
3619 goto fail;
3620 }
3621
3622 if (params->ext_capab) {
3623 wpa_hexdump(MSG_DEBUG, " * ext_capab",
3624 params->ext_capab, params->ext_capab_len);
3625 if (nla_put(msg, NL80211_ATTR_STA_EXT_CAPABILITY,
3626 params->ext_capab_len, params->ext_capab))
3627 goto fail;
3628 }
3629 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003630 if (!params->set) {
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07003631 if (params->aid) {
3632 wpa_printf(MSG_DEBUG, " * aid=%u", params->aid);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003633 if (nla_put_u16(msg, NL80211_ATTR_STA_AID, params->aid))
3634 goto fail;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07003635 } else {
3636 /*
3637 * cfg80211 validates that AID is non-zero, so we have
3638 * to make this a non-zero value for the TDLS case where
3639 * a dummy STA entry is used for now.
3640 */
3641 wpa_printf(MSG_DEBUG, " * aid=1 (TDLS workaround)");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003642 if (nla_put_u16(msg, NL80211_ATTR_STA_AID, 1))
3643 goto fail;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07003644 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003645 wpa_printf(MSG_DEBUG, " * listen_interval=%u",
3646 params->listen_interval);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003647 if (nla_put_u16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
3648 params->listen_interval))
3649 goto fail;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003650 } else if (params->aid && (params->flags & WPA_STA_TDLS_PEER)) {
3651 wpa_printf(MSG_DEBUG, " * peer_aid=%u", params->aid);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003652 if (nla_put_u16(msg, NL80211_ATTR_PEER_AID, params->aid))
3653 goto fail;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003654 }
3655
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08003656 if (params->vht_opmode_enabled) {
3657 wpa_printf(MSG_DEBUG, " * opmode=%u", params->vht_opmode);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003658 if (nla_put_u8(msg, NL80211_ATTR_OPMODE_NOTIF,
3659 params->vht_opmode))
3660 goto fail;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003661 }
3662
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003663 if (params->supp_channels) {
3664 wpa_hexdump(MSG_DEBUG, " * supported channels",
3665 params->supp_channels, params->supp_channels_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003666 if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_CHANNELS,
3667 params->supp_channels_len, params->supp_channels))
3668 goto fail;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003669 }
3670
3671 if (params->supp_oper_classes) {
3672 wpa_hexdump(MSG_DEBUG, " * supported operating classes",
3673 params->supp_oper_classes,
3674 params->supp_oper_classes_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003675 if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES,
3676 params->supp_oper_classes_len,
3677 params->supp_oper_classes))
3678 goto fail;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003679 }
3680
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003681 os_memset(&upd, 0, sizeof(upd));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003682 upd.set = sta_flags_nl80211(params->flags);
3683 upd.mask = upd.set | sta_flags_nl80211(params->flags_mask);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003684 wpa_printf(MSG_DEBUG, " * flags set=0x%x mask=0x%x",
3685 upd.set, upd.mask);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003686 if (nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd))
3687 goto fail;
3688
3689#ifdef CONFIG_MESH
3690 if (params->plink_state &&
3691 nla_put_u8(msg, NL80211_ATTR_STA_PLINK_STATE,
3692 sta_plink_state_nl80211(params->plink_state)))
3693 goto fail;
3694#endif /* CONFIG_MESH */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003695
3696 if (params->flags & WPA_STA_WMM) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003697 struct nlattr *wme = nla_nest_start(msg, NL80211_ATTR_STA_WME);
3698
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003699 wpa_printf(MSG_DEBUG, " * qosinfo=0x%x", params->qosinfo);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003700 if (!wme ||
3701 nla_put_u8(msg, NL80211_STA_WME_UAPSD_QUEUES,
3702 params->qosinfo & WMM_QOSINFO_STA_AC_MASK) ||
3703 nla_put_u8(msg, NL80211_STA_WME_MAX_SP,
3704 (params->qosinfo >> WMM_QOSINFO_STA_SP_SHIFT) &
3705 WMM_QOSINFO_STA_SP_MASK))
3706 goto fail;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003707 nla_nest_end(msg, wme);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003708 }
3709
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003710 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003711 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003712 if (ret)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003713 wpa_printf(MSG_DEBUG, "nl80211: NL80211_CMD_%s_STATION "
3714 "result: %d (%s)", params->set ? "SET" : "NEW", ret,
3715 strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003716 if (ret == -EEXIST)
3717 ret = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003718fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003719 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003720 return ret;
3721}
3722
3723
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003724static void rtnl_neigh_delete_fdb_entry(struct i802_bss *bss, const u8 *addr)
3725{
3726#ifdef CONFIG_LIBNL3_ROUTE
3727 struct wpa_driver_nl80211_data *drv = bss->drv;
3728 struct rtnl_neigh *rn;
3729 struct nl_addr *nl_addr;
3730 int err;
3731
3732 rn = rtnl_neigh_alloc();
3733 if (!rn)
3734 return;
3735
3736 rtnl_neigh_set_family(rn, AF_BRIDGE);
3737 rtnl_neigh_set_ifindex(rn, bss->ifindex);
3738 nl_addr = nl_addr_build(AF_BRIDGE, (void *) addr, ETH_ALEN);
3739 if (!nl_addr) {
3740 rtnl_neigh_put(rn);
3741 return;
3742 }
3743 rtnl_neigh_set_lladdr(rn, nl_addr);
3744
3745 err = rtnl_neigh_delete(drv->rtnl_sk, rn, 0);
3746 if (err < 0) {
3747 wpa_printf(MSG_DEBUG, "nl80211: bridge FDB entry delete for "
3748 MACSTR " ifindex=%d failed: %s", MAC2STR(addr),
3749 bss->ifindex, nl_geterror(err));
3750 } else {
3751 wpa_printf(MSG_DEBUG, "nl80211: deleted bridge FDB entry for "
3752 MACSTR, MAC2STR(addr));
3753 }
3754
3755 nl_addr_put(nl_addr);
3756 rtnl_neigh_put(rn);
3757#endif /* CONFIG_LIBNL3_ROUTE */
3758}
3759
3760
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003761static int wpa_driver_nl80211_sta_remove(struct i802_bss *bss, const u8 *addr,
3762 int deauth, u16 reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003763{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003764 struct wpa_driver_nl80211_data *drv = bss->drv;
3765 struct nl_msg *msg;
3766 int ret;
3767
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003768 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_DEL_STATION)) ||
3769 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
3770 (deauth == 0 &&
3771 nla_put_u8(msg, NL80211_ATTR_MGMT_SUBTYPE,
3772 WLAN_FC_STYPE_DISASSOC)) ||
3773 (deauth == 1 &&
3774 nla_put_u8(msg, NL80211_ATTR_MGMT_SUBTYPE,
3775 WLAN_FC_STYPE_DEAUTH)) ||
3776 (reason_code &&
3777 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code))) {
3778 nlmsg_free(msg);
3779 return -ENOBUFS;
3780 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003781
3782 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07003783 wpa_printf(MSG_DEBUG, "nl80211: sta_remove -> DEL_STATION %s " MACSTR
3784 " --> %d (%s)",
3785 bss->ifname, MAC2STR(addr), ret, strerror(-ret));
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003786
3787 if (drv->rtnl_sk)
3788 rtnl_neigh_delete_fdb_entry(bss, addr);
3789
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003790 if (ret == -ENOENT)
3791 return 0;
3792 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003793}
3794
3795
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003796void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv, int ifidx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003797{
3798 struct nl_msg *msg;
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07003799 struct wpa_driver_nl80211_data *drv2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003800
3801 wpa_printf(MSG_DEBUG, "nl80211: Remove interface ifindex=%d", ifidx);
3802
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003803 /* stop listening for EAPOL on this interface */
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07003804 dl_list_for_each(drv2, &drv->global->interfaces,
3805 struct wpa_driver_nl80211_data, list)
3806 del_ifidx(drv2, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003807
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003808 msg = nl80211_ifindex_msg(drv, ifidx, 0, NL80211_CMD_DEL_INTERFACE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003809 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
3810 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003811 wpa_printf(MSG_ERROR, "Failed to remove interface (ifidx=%d)", ifidx);
3812}
3813
3814
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003815static const char * nl80211_iftype_str(enum nl80211_iftype mode)
3816{
3817 switch (mode) {
3818 case NL80211_IFTYPE_ADHOC:
3819 return "ADHOC";
3820 case NL80211_IFTYPE_STATION:
3821 return "STATION";
3822 case NL80211_IFTYPE_AP:
3823 return "AP";
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003824 case NL80211_IFTYPE_AP_VLAN:
3825 return "AP_VLAN";
3826 case NL80211_IFTYPE_WDS:
3827 return "WDS";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003828 case NL80211_IFTYPE_MONITOR:
3829 return "MONITOR";
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003830 case NL80211_IFTYPE_MESH_POINT:
3831 return "MESH_POINT";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003832 case NL80211_IFTYPE_P2P_CLIENT:
3833 return "P2P_CLIENT";
3834 case NL80211_IFTYPE_P2P_GO:
3835 return "P2P_GO";
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003836 case NL80211_IFTYPE_P2P_DEVICE:
3837 return "P2P_DEVICE";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003838 default:
3839 return "unknown";
3840 }
3841}
3842
3843
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003844static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv,
3845 const char *ifname,
3846 enum nl80211_iftype iftype,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003847 const u8 *addr, int wds,
3848 int (*handler)(struct nl_msg *, void *),
3849 void *arg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003850{
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003851 struct nl_msg *msg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003852 int ifidx;
3853 int ret = -ENOBUFS;
3854
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003855 wpa_printf(MSG_DEBUG, "nl80211: Create interface iftype %d (%s)",
3856 iftype, nl80211_iftype_str(iftype));
3857
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003858 msg = nl80211_cmd_msg(drv->first_bss, 0, NL80211_CMD_NEW_INTERFACE);
3859 if (!msg ||
3860 nla_put_string(msg, NL80211_ATTR_IFNAME, ifname) ||
3861 nla_put_u32(msg, NL80211_ATTR_IFTYPE, iftype))
3862 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003863
3864 if (iftype == NL80211_IFTYPE_MONITOR) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003865 struct nlattr *flags;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003866
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003867 flags = nla_nest_start(msg, NL80211_ATTR_MNTR_FLAGS);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003868 if (!flags ||
3869 nla_put_flag(msg, NL80211_MNTR_FLAG_COOK_FRAMES))
3870 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003871
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003872 nla_nest_end(msg, flags);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003873 } else if (wds) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003874 if (nla_put_u8(msg, NL80211_ATTR_4ADDR, wds))
3875 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003876 }
3877
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07003878 /*
3879 * Tell cfg80211 that the interface belongs to the socket that created
3880 * it, and the interface should be deleted when the socket is closed.
3881 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003882 if (nla_put_flag(msg, NL80211_ATTR_IFACE_SOCKET_OWNER))
3883 goto fail;
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07003884
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003885 ret = send_and_recv_msgs(drv, msg, handler, arg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003886 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003887 if (ret) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003888 fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003889 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003890 wpa_printf(MSG_ERROR, "Failed to create interface %s: %d (%s)",
3891 ifname, ret, strerror(-ret));
3892 return ret;
3893 }
3894
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003895 if (iftype == NL80211_IFTYPE_P2P_DEVICE)
3896 return 0;
3897
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003898 ifidx = if_nametoindex(ifname);
3899 wpa_printf(MSG_DEBUG, "nl80211: New interface %s created: ifindex=%d",
3900 ifname, ifidx);
3901
3902 if (ifidx <= 0)
3903 return -1;
3904
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07003905 /*
3906 * Some virtual interfaces need to process EAPOL packets and events on
3907 * the parent interface. This is used mainly with hostapd.
3908 */
3909 if (drv->hostapd ||
3910 iftype == NL80211_IFTYPE_AP_VLAN ||
3911 iftype == NL80211_IFTYPE_WDS ||
3912 iftype == NL80211_IFTYPE_MONITOR) {
3913 /* start listening for EAPOL on this interface */
3914 add_ifidx(drv, ifidx);
3915 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003916
3917 if (addr && iftype != NL80211_IFTYPE_MONITOR &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003918 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname, addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003919 nl80211_remove_iface(drv, ifidx);
3920 return -1;
3921 }
3922
3923 return ifidx;
3924}
3925
3926
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003927int nl80211_create_iface(struct wpa_driver_nl80211_data *drv,
3928 const char *ifname, enum nl80211_iftype iftype,
3929 const u8 *addr, int wds,
3930 int (*handler)(struct nl_msg *, void *),
3931 void *arg, int use_existing)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003932{
3933 int ret;
3934
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003935 ret = nl80211_create_iface_once(drv, ifname, iftype, addr, wds, handler,
3936 arg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003937
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003938 /* if error occurred and interface exists already */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003939 if (ret == -ENFILE && if_nametoindex(ifname)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003940 if (use_existing) {
3941 wpa_printf(MSG_DEBUG, "nl80211: Continue using existing interface %s",
3942 ifname);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07003943 if (addr && iftype != NL80211_IFTYPE_MONITOR &&
3944 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
3945 addr) < 0 &&
3946 (linux_set_iface_flags(drv->global->ioctl_sock,
3947 ifname, 0) < 0 ||
3948 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
3949 addr) < 0 ||
3950 linux_set_iface_flags(drv->global->ioctl_sock,
3951 ifname, 1) < 0))
3952 return -1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003953 return -ENFILE;
3954 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003955 wpa_printf(MSG_INFO, "Try to remove and re-create %s", ifname);
3956
3957 /* Try to remove the interface that was already there. */
3958 nl80211_remove_iface(drv, if_nametoindex(ifname));
3959
3960 /* Try to create the interface again */
3961 ret = nl80211_create_iface_once(drv, ifname, iftype, addr,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003962 wds, handler, arg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003963 }
3964
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003965 if (ret >= 0 && is_p2p_net_interface(iftype)) {
3966 wpa_printf(MSG_DEBUG,
3967 "nl80211: Interface %s created for P2P - disable 11b rates",
3968 ifname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003969 nl80211_disable_11b_rates(drv, ret, 1);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003970 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003971
3972 return ret;
3973}
3974
3975
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003976static int nl80211_setup_ap(struct i802_bss *bss)
3977{
3978 struct wpa_driver_nl80211_data *drv = bss->drv;
3979
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003980 wpa_printf(MSG_DEBUG, "nl80211: Setup AP(%s) - device_ap_sme=%d use_monitor=%d",
3981 bss->ifname, drv->device_ap_sme, drv->use_monitor);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003982
3983 /*
3984 * Disable Probe Request reporting unless we need it in this way for
3985 * devices that include the AP SME, in the other case (unless using
3986 * monitor iface) we'll get it through the nl_mgmt socket instead.
3987 */
3988 if (!drv->device_ap_sme)
3989 wpa_driver_nl80211_probe_req_report(bss, 0);
3990
3991 if (!drv->device_ap_sme && !drv->use_monitor)
3992 if (nl80211_mgmt_subscribe_ap(bss))
3993 return -1;
3994
3995 if (drv->device_ap_sme && !drv->use_monitor)
3996 if (nl80211_mgmt_subscribe_ap_dev_sme(bss))
3997 return -1;
3998
3999 if (!drv->device_ap_sme && drv->use_monitor &&
4000 nl80211_create_monitor_interface(drv) &&
4001 !drv->device_ap_sme)
Dmitry Shmidt04949592012-07-19 12:16:46 -07004002 return -1;
4003
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004004 if (drv->device_ap_sme &&
4005 wpa_driver_nl80211_probe_req_report(bss, 1) < 0) {
4006 wpa_printf(MSG_DEBUG, "nl80211: Failed to enable "
4007 "Probe Request frame reporting in AP mode");
4008 /* Try to survive without this */
4009 }
4010
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004011 return 0;
4012}
4013
4014
4015static void nl80211_teardown_ap(struct i802_bss *bss)
4016{
4017 struct wpa_driver_nl80211_data *drv = bss->drv;
4018
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004019 wpa_printf(MSG_DEBUG, "nl80211: Teardown AP(%s) - device_ap_sme=%d use_monitor=%d",
4020 bss->ifname, drv->device_ap_sme, drv->use_monitor);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004021 if (drv->device_ap_sme) {
4022 wpa_driver_nl80211_probe_req_report(bss, 0);
4023 if (!drv->use_monitor)
4024 nl80211_mgmt_unsubscribe(bss, "AP teardown (dev SME)");
4025 } else if (drv->use_monitor)
4026 nl80211_remove_monitor_interface(drv);
4027 else
4028 nl80211_mgmt_unsubscribe(bss, "AP teardown");
4029
4030 bss->beacon_set = 0;
4031}
4032
4033
4034static int nl80211_send_eapol_data(struct i802_bss *bss,
4035 const u8 *addr, const u8 *data,
4036 size_t data_len)
4037{
4038 struct sockaddr_ll ll;
4039 int ret;
4040
4041 if (bss->drv->eapol_tx_sock < 0) {
4042 wpa_printf(MSG_DEBUG, "nl80211: No socket to send EAPOL");
4043 return -1;
4044 }
4045
4046 os_memset(&ll, 0, sizeof(ll));
4047 ll.sll_family = AF_PACKET;
4048 ll.sll_ifindex = bss->ifindex;
4049 ll.sll_protocol = htons(ETH_P_PAE);
4050 ll.sll_halen = ETH_ALEN;
4051 os_memcpy(ll.sll_addr, addr, ETH_ALEN);
4052 ret = sendto(bss->drv->eapol_tx_sock, data, data_len, 0,
4053 (struct sockaddr *) &ll, sizeof(ll));
4054 if (ret < 0)
4055 wpa_printf(MSG_ERROR, "nl80211: EAPOL TX: %s",
4056 strerror(errno));
4057
4058 return ret;
4059}
4060
4061
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004062static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
4063
4064static int wpa_driver_nl80211_hapd_send_eapol(
4065 void *priv, const u8 *addr, const u8 *data,
4066 size_t data_len, int encrypt, const u8 *own_addr, u32 flags)
4067{
4068 struct i802_bss *bss = priv;
4069 struct wpa_driver_nl80211_data *drv = bss->drv;
4070 struct ieee80211_hdr *hdr;
4071 size_t len;
4072 u8 *pos;
4073 int res;
4074 int qos = flags & WPA_STA_WMM;
Dmitry Shmidt641185e2013-11-06 15:17:13 -08004075
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004076 if (drv->device_ap_sme || !drv->use_monitor)
4077 return nl80211_send_eapol_data(bss, addr, data, data_len);
4078
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004079 len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
4080 data_len;
4081 hdr = os_zalloc(len);
4082 if (hdr == NULL) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004083 wpa_printf(MSG_INFO, "nl80211: Failed to allocate EAPOL buffer(len=%lu)",
4084 (unsigned long) len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004085 return -1;
4086 }
4087
4088 hdr->frame_control =
4089 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
4090 hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
4091 if (encrypt)
4092 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
4093 if (qos) {
4094 hdr->frame_control |=
4095 host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
4096 }
4097
4098 memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
4099 memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
4100 memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
4101 pos = (u8 *) (hdr + 1);
4102
4103 if (qos) {
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004104 /* Set highest priority in QoS header */
4105 pos[0] = 7;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004106 pos[1] = 0;
4107 pos += 2;
4108 }
4109
4110 memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
4111 pos += sizeof(rfc1042_header);
4112 WPA_PUT_BE16(pos, ETH_P_PAE);
4113 pos += 2;
4114 memcpy(pos, data, data_len);
4115
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004116 res = wpa_driver_nl80211_send_frame(bss, (u8 *) hdr, len, encrypt, 0,
4117 0, 0, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004118 if (res < 0) {
4119 wpa_printf(MSG_ERROR, "i802_send_eapol - packet len: %lu - "
4120 "failed: %d (%s)",
4121 (unsigned long) len, errno, strerror(errno));
4122 }
4123 os_free(hdr);
4124
4125 return res;
4126}
4127
4128
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004129static int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr,
4130 int total_flags,
4131 int flags_or, int flags_and)
4132{
4133 struct i802_bss *bss = priv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004134 struct nl_msg *msg;
4135 struct nlattr *flags;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004136 struct nl80211_sta_flag_update upd;
4137
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004138 wpa_printf(MSG_DEBUG, "nl80211: Set STA flags - ifname=%s addr=" MACSTR
4139 " total_flags=0x%x flags_or=0x%x flags_and=0x%x authorized=%d",
4140 bss->ifname, MAC2STR(addr), total_flags, flags_or, flags_and,
4141 !!(total_flags & WPA_STA_AUTHORIZED));
4142
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004143 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
4144 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
4145 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004146
4147 /*
4148 * Backwards compatibility version using NL80211_ATTR_STA_FLAGS. This
4149 * can be removed eventually.
4150 */
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004151 flags = nla_nest_start(msg, NL80211_ATTR_STA_FLAGS);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004152 if (!flags ||
4153 ((total_flags & WPA_STA_AUTHORIZED) &&
4154 nla_put_flag(msg, NL80211_STA_FLAG_AUTHORIZED)) ||
4155 ((total_flags & WPA_STA_WMM) &&
4156 nla_put_flag(msg, NL80211_STA_FLAG_WME)) ||
4157 ((total_flags & WPA_STA_SHORT_PREAMBLE) &&
4158 nla_put_flag(msg, NL80211_STA_FLAG_SHORT_PREAMBLE)) ||
4159 ((total_flags & WPA_STA_MFP) &&
4160 nla_put_flag(msg, NL80211_STA_FLAG_MFP)) ||
4161 ((total_flags & WPA_STA_TDLS_PEER) &&
4162 nla_put_flag(msg, NL80211_STA_FLAG_TDLS_PEER)))
4163 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004164
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004165 nla_nest_end(msg, flags);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004166
4167 os_memset(&upd, 0, sizeof(upd));
4168 upd.mask = sta_flags_nl80211(flags_or | ~flags_and);
4169 upd.set = sta_flags_nl80211(flags_or);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004170 if (nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd))
4171 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004172
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004173 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
4174fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004175 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004176 return -ENOBUFS;
4177}
4178
4179
4180static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv,
4181 struct wpa_driver_associate_params *params)
4182{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004183 enum nl80211_iftype nlmode, old_mode;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004184
4185 if (params->p2p) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004186 wpa_printf(MSG_DEBUG, "nl80211: Setup AP operations for P2P "
4187 "group (GO)");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004188 nlmode = NL80211_IFTYPE_P2P_GO;
4189 } else
4190 nlmode = NL80211_IFTYPE_AP;
4191
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004192 old_mode = drv->nlmode;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004193 if (wpa_driver_nl80211_set_mode(drv->first_bss, nlmode)) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004194 nl80211_remove_monitor_interface(drv);
4195 return -1;
4196 }
4197
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004198 if (nl80211_set_channel(drv->first_bss, &params->freq, 0)) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004199 if (old_mode != nlmode)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004200 wpa_driver_nl80211_set_mode(drv->first_bss, old_mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004201 nl80211_remove_monitor_interface(drv);
4202 return -1;
4203 }
4204
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004205 return 0;
4206}
4207
4208
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004209static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv,
4210 int reset_mode)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004211{
4212 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004213 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004214
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004215 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_LEAVE_IBSS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004216 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004217 if (ret) {
4218 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS failed: ret=%d "
4219 "(%s)", ret, strerror(-ret));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004220 } else {
4221 wpa_printf(MSG_DEBUG,
4222 "nl80211: Leave IBSS request sent successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004223 }
4224
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004225 if (reset_mode &&
4226 wpa_driver_nl80211_set_mode(drv->first_bss,
Dmitry Shmidt56052862013-10-04 10:23:25 -07004227 NL80211_IFTYPE_STATION)) {
4228 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
4229 "station mode");
4230 }
4231
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004232 return ret;
4233}
4234
4235
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08004236static int nl80211_ht_vht_overrides(struct nl_msg *msg,
4237 struct wpa_driver_associate_params *params)
4238{
4239 if (params->disable_ht && nla_put_flag(msg, NL80211_ATTR_DISABLE_HT))
4240 return -1;
4241
4242 if (params->htcaps && params->htcaps_mask) {
4243 int sz = sizeof(struct ieee80211_ht_capabilities);
4244 wpa_hexdump(MSG_DEBUG, " * htcaps", params->htcaps, sz);
4245 wpa_hexdump(MSG_DEBUG, " * htcaps_mask",
4246 params->htcaps_mask, sz);
4247 if (nla_put(msg, NL80211_ATTR_HT_CAPABILITY, sz,
4248 params->htcaps) ||
4249 nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK, sz,
4250 params->htcaps_mask))
4251 return -1;
4252 }
4253
4254#ifdef CONFIG_VHT_OVERRIDES
4255 if (params->disable_vht) {
4256 wpa_printf(MSG_DEBUG, " * VHT disabled");
4257 if (nla_put_flag(msg, NL80211_ATTR_DISABLE_VHT))
4258 return -1;
4259 }
4260
4261 if (params->vhtcaps && params->vhtcaps_mask) {
4262 int sz = sizeof(struct ieee80211_vht_capabilities);
4263 wpa_hexdump(MSG_DEBUG, " * vhtcaps", params->vhtcaps, sz);
4264 wpa_hexdump(MSG_DEBUG, " * vhtcaps_mask",
4265 params->vhtcaps_mask, sz);
4266 if (nla_put(msg, NL80211_ATTR_VHT_CAPABILITY, sz,
4267 params->vhtcaps) ||
4268 nla_put(msg, NL80211_ATTR_VHT_CAPABILITY_MASK, sz,
4269 params->vhtcaps_mask))
4270 return -1;
4271 }
4272#endif /* CONFIG_VHT_OVERRIDES */
4273
4274 return 0;
4275}
4276
4277
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004278static int wpa_driver_nl80211_ibss(struct wpa_driver_nl80211_data *drv,
4279 struct wpa_driver_associate_params *params)
4280{
4281 struct nl_msg *msg;
4282 int ret = -1;
4283 int count = 0;
4284
4285 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS (ifindex=%d)", drv->ifindex);
4286
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004287 if (wpa_driver_nl80211_set_mode_ibss(drv->first_bss, &params->freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004288 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
4289 "IBSS mode");
4290 return -1;
4291 }
4292
4293retry:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004294 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_JOIN_IBSS)) ||
4295 params->ssid == NULL || params->ssid_len > sizeof(drv->ssid))
4296 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004297
4298 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
4299 params->ssid, params->ssid_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004300 if (nla_put(msg, NL80211_ATTR_SSID, params->ssid_len, params->ssid))
4301 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004302 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
4303 drv->ssid_len = params->ssid_len;
4304
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004305 if (nl80211_put_freq_params(msg, &params->freq) < 0 ||
4306 nl80211_put_beacon_int(msg, params->beacon_int))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004307 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004308
4309 ret = nl80211_set_conn_keys(params, msg);
4310 if (ret)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004311 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004312
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004313 if (params->bssid && params->fixed_bssid) {
4314 wpa_printf(MSG_DEBUG, " * BSSID=" MACSTR,
4315 MAC2STR(params->bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004316 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid))
4317 goto fail;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004318 }
4319
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004320 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
4321 params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
4322 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
4323 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004324 wpa_printf(MSG_DEBUG, " * control port");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004325 if (nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT))
4326 goto fail;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004327 }
4328
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004329 if (params->wpa_ie) {
4330 wpa_hexdump(MSG_DEBUG,
4331 " * Extra IEs for Beacon/Probe Response frames",
4332 params->wpa_ie, params->wpa_ie_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004333 if (nla_put(msg, NL80211_ATTR_IE, params->wpa_ie_len,
4334 params->wpa_ie))
4335 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004336 }
4337
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08004338 if (nl80211_ht_vht_overrides(msg, params) < 0)
4339 return -1;
4340
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004341 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4342 msg = NULL;
4343 if (ret) {
4344 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS failed: ret=%d (%s)",
4345 ret, strerror(-ret));
4346 count++;
4347 if (ret == -EALREADY && count == 1) {
4348 wpa_printf(MSG_DEBUG, "nl80211: Retry IBSS join after "
4349 "forced leave");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004350 nl80211_leave_ibss(drv, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004351 nlmsg_free(msg);
4352 goto retry;
4353 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004354 } else {
4355 wpa_printf(MSG_DEBUG,
4356 "nl80211: Join IBSS request sent successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004357 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004358
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004359fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004360 nlmsg_free(msg);
4361 return ret;
4362}
4363
4364
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004365static int nl80211_connect_common(struct wpa_driver_nl80211_data *drv,
4366 struct wpa_driver_associate_params *params,
4367 struct nl_msg *msg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004368{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004369 if (params->bssid) {
4370 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
4371 MAC2STR(params->bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004372 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid))
4373 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004374 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004375
Dmitry Shmidt96be6222014-02-13 10:16:51 -08004376 if (params->bssid_hint) {
4377 wpa_printf(MSG_DEBUG, " * bssid_hint=" MACSTR,
4378 MAC2STR(params->bssid_hint));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004379 if (nla_put(msg, NL80211_ATTR_MAC_HINT, ETH_ALEN,
4380 params->bssid_hint))
4381 return -1;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08004382 }
4383
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004384 if (params->freq.freq) {
4385 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq.freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004386 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
4387 params->freq.freq))
4388 return -1;
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004389 drv->assoc_freq = params->freq.freq;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004390 } else
4391 drv->assoc_freq = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004392
Dmitry Shmidt96be6222014-02-13 10:16:51 -08004393 if (params->freq_hint) {
4394 wpa_printf(MSG_DEBUG, " * freq_hint=%d", params->freq_hint);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004395 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ_HINT,
4396 params->freq_hint))
4397 return -1;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08004398 }
4399
Dmitry Shmidt04949592012-07-19 12:16:46 -07004400 if (params->bg_scan_period >= 0) {
4401 wpa_printf(MSG_DEBUG, " * bg scan period=%d",
4402 params->bg_scan_period);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004403 if (nla_put_u16(msg, NL80211_ATTR_BG_SCAN_PERIOD,
4404 params->bg_scan_period))
4405 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004406 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004407
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004408 if (params->ssid) {
4409 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
4410 params->ssid, params->ssid_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004411 if (nla_put(msg, NL80211_ATTR_SSID, params->ssid_len,
4412 params->ssid))
4413 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004414 if (params->ssid_len > sizeof(drv->ssid))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004415 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004416 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
4417 drv->ssid_len = params->ssid_len;
4418 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004419
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004420 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004421 if (params->wpa_ie &&
4422 nla_put(msg, NL80211_ATTR_IE, params->wpa_ie_len, params->wpa_ie))
4423 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004424
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004425 if (params->wpa_proto) {
4426 enum nl80211_wpa_versions ver = 0;
4427
4428 if (params->wpa_proto & WPA_PROTO_WPA)
4429 ver |= NL80211_WPA_VERSION_1;
4430 if (params->wpa_proto & WPA_PROTO_RSN)
4431 ver |= NL80211_WPA_VERSION_2;
4432
4433 wpa_printf(MSG_DEBUG, " * WPA Versions 0x%x", ver);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004434 if (nla_put_u32(msg, NL80211_ATTR_WPA_VERSIONS, ver))
4435 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004436 }
4437
4438 if (params->pairwise_suite != WPA_CIPHER_NONE) {
4439 u32 cipher = wpa_cipher_to_cipher_suite(params->pairwise_suite);
4440 wpa_printf(MSG_DEBUG, " * pairwise=0x%x", cipher);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004441 if (nla_put_u32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
4442 cipher))
4443 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004444 }
4445
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08004446 if (params->group_suite == WPA_CIPHER_GTK_NOT_USED &&
4447 !(drv->capa.enc & WPA_DRIVER_CAPA_ENC_GTK_NOT_USED)) {
4448 /*
4449 * This is likely to work even though many drivers do not
4450 * advertise support for operations without GTK.
4451 */
4452 wpa_printf(MSG_DEBUG, " * skip group cipher configuration for GTK_NOT_USED due to missing driver support advertisement");
4453 } else if (params->group_suite != WPA_CIPHER_NONE) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004454 u32 cipher = wpa_cipher_to_cipher_suite(params->group_suite);
4455 wpa_printf(MSG_DEBUG, " * group=0x%x", cipher);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004456 if (nla_put_u32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher))
4457 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004458 }
4459
4460 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
4461 params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
4462 params->key_mgmt_suite == WPA_KEY_MGMT_FT_IEEE8021X ||
4463 params->key_mgmt_suite == WPA_KEY_MGMT_FT_PSK ||
Dmitry Shmidt15907092014-03-25 10:42:57 -07004464 params->key_mgmt_suite == WPA_KEY_MGMT_CCKM ||
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -07004465 params->key_mgmt_suite == WPA_KEY_MGMT_OSEN ||
4466 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004467 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256 ||
Dmitry Shmidt807291d2015-01-27 13:40:23 -08004468 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SUITE_B ||
4469 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004470 int mgmt = WLAN_AKM_SUITE_PSK;
4471
4472 switch (params->key_mgmt_suite) {
4473 case WPA_KEY_MGMT_CCKM:
4474 mgmt = WLAN_AKM_SUITE_CCKM;
4475 break;
4476 case WPA_KEY_MGMT_IEEE8021X:
4477 mgmt = WLAN_AKM_SUITE_8021X;
4478 break;
4479 case WPA_KEY_MGMT_FT_IEEE8021X:
4480 mgmt = WLAN_AKM_SUITE_FT_8021X;
4481 break;
4482 case WPA_KEY_MGMT_FT_PSK:
4483 mgmt = WLAN_AKM_SUITE_FT_PSK;
4484 break;
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -07004485 case WPA_KEY_MGMT_IEEE8021X_SHA256:
4486 mgmt = WLAN_AKM_SUITE_8021X_SHA256;
4487 break;
4488 case WPA_KEY_MGMT_PSK_SHA256:
4489 mgmt = WLAN_AKM_SUITE_PSK_SHA256;
4490 break;
Dmitry Shmidt15907092014-03-25 10:42:57 -07004491 case WPA_KEY_MGMT_OSEN:
4492 mgmt = WLAN_AKM_SUITE_OSEN;
4493 break;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004494 case WPA_KEY_MGMT_IEEE8021X_SUITE_B:
4495 mgmt = WLAN_AKM_SUITE_8021X_SUITE_B;
4496 break;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08004497 case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192:
4498 mgmt = WLAN_AKM_SUITE_8021X_SUITE_B_192;
4499 break;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004500 case WPA_KEY_MGMT_PSK:
4501 default:
4502 mgmt = WLAN_AKM_SUITE_PSK;
4503 break;
4504 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07004505 wpa_printf(MSG_DEBUG, " * akm=0x%x", mgmt);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004506 if (nla_put_u32(msg, NL80211_ATTR_AKM_SUITES, mgmt))
4507 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004508 }
4509
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004510 if (nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT))
4511 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004512
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004513 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED &&
4514 nla_put_u32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED))
4515 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004516
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004517 if (params->rrm_used) {
4518 u32 drv_rrm_flags = drv->capa.rrm_flags;
4519 if (!(drv_rrm_flags &
4520 WPA_DRIVER_FLAGS_DS_PARAM_SET_IE_IN_PROBES) ||
4521 !(drv_rrm_flags & WPA_DRIVER_FLAGS_QUIET) ||
4522 nla_put_flag(msg, NL80211_ATTR_USE_RRM))
4523 return -1;
4524 }
4525
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08004526 if (nl80211_ht_vht_overrides(msg, params) < 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004527 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004528
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004529 if (params->p2p)
4530 wpa_printf(MSG_DEBUG, " * P2P group");
4531
4532 return 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004533}
4534
4535
4536static int wpa_driver_nl80211_try_connect(
4537 struct wpa_driver_nl80211_data *drv,
4538 struct wpa_driver_associate_params *params)
4539{
4540 struct nl_msg *msg;
4541 enum nl80211_auth_type type;
4542 int ret;
4543 int algs;
4544
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004545 if (params->req_key_mgmt_offload && params->psk &&
4546 (params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
4547 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256 ||
4548 params->key_mgmt_suite == WPA_KEY_MGMT_FT_PSK)) {
4549 wpa_printf(MSG_DEBUG, "nl80211: Key management set PSK");
4550 ret = issue_key_mgmt_set_key(drv, params->psk, 32);
4551 if (ret)
4552 return ret;
4553 }
4554
4555 wpa_printf(MSG_DEBUG, "nl80211: Connect (ifindex=%d)", drv->ifindex);
4556 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_CONNECT);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004557 if (!msg)
4558 return -1;
4559
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004560 ret = nl80211_connect_common(drv, params, msg);
4561 if (ret)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004562 goto fail;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004563
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004564 algs = 0;
4565 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
4566 algs++;
4567 if (params->auth_alg & WPA_AUTH_ALG_SHARED)
4568 algs++;
4569 if (params->auth_alg & WPA_AUTH_ALG_LEAP)
4570 algs++;
4571 if (algs > 1) {
4572 wpa_printf(MSG_DEBUG, " * Leave out Auth Type for automatic "
4573 "selection");
4574 goto skip_auth_type;
4575 }
4576
4577 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
4578 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
4579 else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
4580 type = NL80211_AUTHTYPE_SHARED_KEY;
4581 else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
4582 type = NL80211_AUTHTYPE_NETWORK_EAP;
4583 else if (params->auth_alg & WPA_AUTH_ALG_FT)
4584 type = NL80211_AUTHTYPE_FT;
4585 else
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004586 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004587
4588 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004589 if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE, type))
4590 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004591
4592skip_auth_type:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004593 ret = nl80211_set_conn_keys(params, msg);
4594 if (ret)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004595 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004596
4597 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4598 msg = NULL;
4599 if (ret) {
4600 wpa_printf(MSG_DEBUG, "nl80211: MLME connect failed: ret=%d "
4601 "(%s)", ret, strerror(-ret));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004602 } else {
4603 wpa_printf(MSG_DEBUG,
4604 "nl80211: Connect request send successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004605 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004606
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004607fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004608 nlmsg_free(msg);
4609 return ret;
4610
4611}
4612
4613
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004614static int wpa_driver_nl80211_connect(
4615 struct wpa_driver_nl80211_data *drv,
4616 struct wpa_driver_associate_params *params)
4617{
Jithu Jancea7c60b42014-12-03 18:54:40 +05304618 int ret;
4619
4620 /* Store the connection attempted bssid for future use */
4621 if (params->bssid)
4622 os_memcpy(drv->auth_attempt_bssid, params->bssid, ETH_ALEN);
4623 else
4624 os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
4625
4626 ret = wpa_driver_nl80211_try_connect(drv, params);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004627 if (ret == -EALREADY) {
4628 /*
4629 * cfg80211 does not currently accept new connections if
4630 * we are already connected. As a workaround, force
4631 * disconnection and try again.
4632 */
4633 wpa_printf(MSG_DEBUG, "nl80211: Explicitly "
4634 "disconnecting before reassociation "
4635 "attempt");
4636 if (wpa_driver_nl80211_disconnect(
4637 drv, WLAN_REASON_PREV_AUTH_NOT_VALID))
4638 return -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004639 ret = wpa_driver_nl80211_try_connect(drv, params);
4640 }
4641 return ret;
4642}
4643
4644
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004645static int wpa_driver_nl80211_associate(
4646 void *priv, struct wpa_driver_associate_params *params)
4647{
4648 struct i802_bss *bss = priv;
4649 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004650 int ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004651 struct nl_msg *msg;
4652
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004653 nl80211_unmask_11b_rates(bss);
4654
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004655 if (params->mode == IEEE80211_MODE_AP)
4656 return wpa_driver_nl80211_ap(drv, params);
4657
4658 if (params->mode == IEEE80211_MODE_IBSS)
4659 return wpa_driver_nl80211_ibss(drv, params);
4660
4661 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004662 enum nl80211_iftype nlmode = params->p2p ?
4663 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
4664
4665 if (wpa_driver_nl80211_set_mode(priv, nlmode) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004666 return -1;
4667 return wpa_driver_nl80211_connect(drv, params);
4668 }
4669
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004670 nl80211_mark_disconnected(drv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004671
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004672 wpa_printf(MSG_DEBUG, "nl80211: Associate (ifindex=%d)",
4673 drv->ifindex);
4674 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_ASSOCIATE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004675 if (!msg)
4676 return -1;
4677
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004678 ret = nl80211_connect_common(drv, params, msg);
4679 if (ret)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004680 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004681
4682 if (params->prev_bssid) {
4683 wpa_printf(MSG_DEBUG, " * prev_bssid=" MACSTR,
4684 MAC2STR(params->prev_bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004685 if (nla_put(msg, NL80211_ATTR_PREV_BSSID, ETH_ALEN,
4686 params->prev_bssid))
4687 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004688 }
4689
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004690 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4691 msg = NULL;
4692 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004693 wpa_dbg(drv->ctx, MSG_DEBUG,
4694 "nl80211: MLME command failed (assoc): ret=%d (%s)",
4695 ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004696 nl80211_dump_scan(drv);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004697 } else {
4698 wpa_printf(MSG_DEBUG,
4699 "nl80211: Association request send successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004700 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004701
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004702fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004703 nlmsg_free(msg);
4704 return ret;
4705}
4706
4707
4708static int nl80211_set_mode(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004709 int ifindex, enum nl80211_iftype mode)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004710{
4711 struct nl_msg *msg;
4712 int ret = -ENOBUFS;
4713
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004714 wpa_printf(MSG_DEBUG, "nl80211: Set mode ifindex %d iftype %d (%s)",
4715 ifindex, mode, nl80211_iftype_str(mode));
4716
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004717 msg = nl80211_cmd_msg(drv->first_bss, 0, NL80211_CMD_SET_INTERFACE);
4718 if (!msg || nla_put_u32(msg, NL80211_ATTR_IFTYPE, mode))
4719 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004720
4721 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004722 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004723 if (!ret)
4724 return 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004725fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004726 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004727 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface %d to mode %d:"
4728 " %d (%s)", ifindex, mode, ret, strerror(-ret));
4729 return ret;
4730}
4731
4732
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004733static int wpa_driver_nl80211_set_mode_impl(
4734 struct i802_bss *bss,
4735 enum nl80211_iftype nlmode,
4736 struct hostapd_freq_params *desired_freq_params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004737{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004738 struct wpa_driver_nl80211_data *drv = bss->drv;
4739 int ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004740 int i;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004741 int was_ap = is_ap_interface(drv->nlmode);
4742 int res;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004743 int mode_switch_res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004744
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004745 mode_switch_res = nl80211_set_mode(drv, drv->ifindex, nlmode);
4746 if (mode_switch_res && nlmode == nl80211_get_ifmode(bss))
4747 mode_switch_res = 0;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004748
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004749 if (mode_switch_res == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004750 drv->nlmode = nlmode;
4751 ret = 0;
4752 goto done;
4753 }
4754
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004755 if (mode_switch_res == -ENODEV)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004756 return -1;
4757
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004758 if (nlmode == drv->nlmode) {
4759 wpa_printf(MSG_DEBUG, "nl80211: Interface already in "
4760 "requested mode - ignore error");
4761 ret = 0;
4762 goto done; /* Already in the requested mode */
4763 }
4764
4765 /* mac80211 doesn't allow mode changes while the device is up, so
4766 * take the device down, try to set the mode again, and bring the
4767 * device back up.
4768 */
4769 wpa_printf(MSG_DEBUG, "nl80211: Try mode change after setting "
4770 "interface down");
4771 for (i = 0; i < 10; i++) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004772 res = i802_set_iface_flags(bss, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004773 if (res == -EACCES || res == -ENODEV)
4774 break;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004775 if (res != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004776 wpa_printf(MSG_DEBUG, "nl80211: Failed to set "
4777 "interface down");
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004778 os_sleep(0, 100000);
4779 continue;
4780 }
4781
4782 /*
4783 * Setting the mode will fail for some drivers if the phy is
4784 * on a frequency that the mode is disallowed in.
4785 */
4786 if (desired_freq_params) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004787 res = nl80211_set_channel(bss, desired_freq_params, 0);
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004788 if (res) {
4789 wpa_printf(MSG_DEBUG,
4790 "nl80211: Failed to set frequency on interface");
4791 }
4792 }
4793
4794 /* Try to set the mode again while the interface is down */
4795 mode_switch_res = nl80211_set_mode(drv, drv->ifindex, nlmode);
4796 if (mode_switch_res == -EBUSY) {
4797 wpa_printf(MSG_DEBUG,
4798 "nl80211: Delaying mode set while interface going down");
4799 os_sleep(0, 100000);
4800 continue;
4801 }
4802 ret = mode_switch_res;
4803 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004804 }
4805
4806 if (!ret) {
4807 wpa_printf(MSG_DEBUG, "nl80211: Mode change succeeded while "
4808 "interface is down");
4809 drv->nlmode = nlmode;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004810 drv->ignore_if_down_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004811 }
4812
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004813 /* Bring the interface back up */
4814 res = linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1);
4815 if (res != 0) {
4816 wpa_printf(MSG_DEBUG,
4817 "nl80211: Failed to set interface up after switching mode");
4818 ret = -1;
4819 }
4820
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004821done:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004822 if (ret) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004823 wpa_printf(MSG_DEBUG, "nl80211: Interface mode change to %d "
4824 "from %d failed", nlmode, drv->nlmode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004825 return ret;
4826 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004827
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004828 if (is_p2p_net_interface(nlmode)) {
4829 wpa_printf(MSG_DEBUG,
4830 "nl80211: Interface %s mode change to P2P - disable 11b rates",
4831 bss->ifname);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004832 nl80211_disable_11b_rates(drv, drv->ifindex, 1);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004833 } else if (drv->disabled_11b_rates) {
4834 wpa_printf(MSG_DEBUG,
4835 "nl80211: Interface %s mode changed to non-P2P - re-enable 11b rates",
4836 bss->ifname);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004837 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004838 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004839
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004840 if (is_ap_interface(nlmode)) {
4841 nl80211_mgmt_unsubscribe(bss, "start AP");
4842 /* Setup additional AP mode functionality if needed */
4843 if (nl80211_setup_ap(bss))
4844 return -1;
4845 } else if (was_ap) {
4846 /* Remove additional AP mode functionality */
4847 nl80211_teardown_ap(bss);
4848 } else {
4849 nl80211_mgmt_unsubscribe(bss, "mode change");
4850 }
4851
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004852 if (is_mesh_interface(nlmode) &&
4853 nl80211_mgmt_subscribe_mesh(bss))
4854 return -1;
4855
Dmitry Shmidt04949592012-07-19 12:16:46 -07004856 if (!bss->in_deinit && !is_ap_interface(nlmode) &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004857 !is_mesh_interface(nlmode) &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004858 nl80211_mgmt_subscribe_non_ap(bss) < 0)
4859 wpa_printf(MSG_DEBUG, "nl80211: Failed to register Action "
4860 "frame processing - ignore for now");
4861
4862 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004863}
4864
4865
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004866int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
4867 enum nl80211_iftype nlmode)
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004868{
4869 return wpa_driver_nl80211_set_mode_impl(bss, nlmode, NULL);
4870}
4871
4872
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004873static int wpa_driver_nl80211_set_mode_ibss(struct i802_bss *bss,
4874 struct hostapd_freq_params *freq)
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004875{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004876 return wpa_driver_nl80211_set_mode_impl(bss, NL80211_IFTYPE_ADHOC,
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004877 freq);
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004878}
4879
4880
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004881static int wpa_driver_nl80211_get_capa(void *priv,
4882 struct wpa_driver_capa *capa)
4883{
4884 struct i802_bss *bss = priv;
4885 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07004886
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004887 if (!drv->has_capability)
4888 return -1;
4889 os_memcpy(capa, &drv->capa, sizeof(*capa));
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004890 if (drv->extended_capa && drv->extended_capa_mask) {
4891 capa->extended_capa = drv->extended_capa;
4892 capa->extended_capa_mask = drv->extended_capa_mask;
4893 capa->extended_capa_len = drv->extended_capa_len;
4894 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004895
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004896 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004897}
4898
4899
4900static int wpa_driver_nl80211_set_operstate(void *priv, int state)
4901{
4902 struct i802_bss *bss = priv;
4903 struct wpa_driver_nl80211_data *drv = bss->drv;
4904
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004905 wpa_printf(MSG_DEBUG, "nl80211: Set %s operstate %d->%d (%s)",
4906 bss->ifname, drv->operstate, state,
4907 state ? "UP" : "DORMANT");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004908 drv->operstate = state;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004909 return netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, -1,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004910 state ? IF_OPER_UP : IF_OPER_DORMANT);
4911}
4912
4913
4914static int wpa_driver_nl80211_set_supp_port(void *priv, int authorized)
4915{
4916 struct i802_bss *bss = priv;
4917 struct wpa_driver_nl80211_data *drv = bss->drv;
4918 struct nl_msg *msg;
4919 struct nl80211_sta_flag_update upd;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004920 int ret;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004921
4922 if (!drv->associated && is_zero_ether_addr(drv->bssid) && !authorized) {
4923 wpa_printf(MSG_DEBUG, "nl80211: Skip set_supp_port(unauthorized) while not associated");
4924 return 0;
4925 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004926
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004927 wpa_printf(MSG_DEBUG, "nl80211: Set supplicant port %sauthorized for "
4928 MACSTR, authorized ? "" : "un", MAC2STR(drv->bssid));
4929
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004930 os_memset(&upd, 0, sizeof(upd));
4931 upd.mask = BIT(NL80211_STA_FLAG_AUTHORIZED);
4932 if (authorized)
4933 upd.set = BIT(NL80211_STA_FLAG_AUTHORIZED);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004934
4935 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
4936 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid) ||
4937 nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd)) {
4938 nlmsg_free(msg);
4939 return -ENOBUFS;
4940 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004941
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004942 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004943 if (!ret)
4944 return 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004945 wpa_printf(MSG_DEBUG, "nl80211: Failed to set STA flag: %d (%s)",
4946 ret, strerror(-ret));
4947 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004948}
4949
4950
Jouni Malinen75ecf522011-06-27 15:19:46 -07004951/* Set kernel driver on given frequency (MHz) */
4952static int i802_set_freq(void *priv, struct hostapd_freq_params *freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004953{
Jouni Malinen75ecf522011-06-27 15:19:46 -07004954 struct i802_bss *bss = priv;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07004955 return nl80211_set_channel(bss, freq, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004956}
4957
4958
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004959static inline int min_int(int a, int b)
4960{
4961 if (a < b)
4962 return a;
4963 return b;
4964}
4965
4966
4967static int get_key_handler(struct nl_msg *msg, void *arg)
4968{
4969 struct nlattr *tb[NL80211_ATTR_MAX + 1];
4970 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
4971
4972 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
4973 genlmsg_attrlen(gnlh, 0), NULL);
4974
4975 /*
4976 * TODO: validate the key index and mac address!
4977 * Otherwise, there's a race condition as soon as
4978 * the kernel starts sending key notifications.
4979 */
4980
4981 if (tb[NL80211_ATTR_KEY_SEQ])
4982 memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]),
4983 min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6));
4984 return NL_SKIP;
4985}
4986
4987
4988static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
4989 int idx, u8 *seq)
4990{
4991 struct i802_bss *bss = priv;
4992 struct wpa_driver_nl80211_data *drv = bss->drv;
4993 struct nl_msg *msg;
4994
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004995 msg = nl80211_ifindex_msg(drv, if_nametoindex(iface), 0,
4996 NL80211_CMD_GET_KEY);
4997 if (!msg ||
4998 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
4999 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, idx)) {
5000 nlmsg_free(msg);
5001 return -ENOBUFS;
5002 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005003
5004 memset(seq, 0, 6);
5005
5006 return send_and_recv_msgs(drv, msg, get_key_handler, seq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005007}
5008
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005009
5010static int i802_set_rts(void *priv, int rts)
5011{
5012 struct i802_bss *bss = priv;
5013 struct wpa_driver_nl80211_data *drv = bss->drv;
5014 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005015 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005016 u32 val;
5017
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005018 if (rts >= 2347)
5019 val = (u32) -1;
5020 else
5021 val = rts;
5022
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005023 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_WIPHY)) ||
5024 nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD, val)) {
5025 nlmsg_free(msg);
5026 return -ENOBUFS;
5027 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005028
5029 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5030 if (!ret)
5031 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005032 wpa_printf(MSG_DEBUG, "nl80211: Failed to set RTS threshold %d: "
5033 "%d (%s)", rts, ret, strerror(-ret));
5034 return ret;
5035}
5036
5037
5038static int i802_set_frag(void *priv, int frag)
5039{
5040 struct i802_bss *bss = priv;
5041 struct wpa_driver_nl80211_data *drv = bss->drv;
5042 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005043 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005044 u32 val;
5045
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005046 if (frag >= 2346)
5047 val = (u32) -1;
5048 else
5049 val = frag;
5050
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005051 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_WIPHY)) ||
5052 nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD, val)) {
5053 nlmsg_free(msg);
5054 return -ENOBUFS;
5055 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005056
5057 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5058 if (!ret)
5059 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005060 wpa_printf(MSG_DEBUG, "nl80211: Failed to set fragmentation threshold "
5061 "%d: %d (%s)", frag, ret, strerror(-ret));
5062 return ret;
5063}
5064
5065
5066static int i802_flush(void *priv)
5067{
5068 struct i802_bss *bss = priv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005069 struct nl_msg *msg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005070 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005071
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005072 wpa_printf(MSG_DEBUG, "nl80211: flush -> DEL_STATION %s (all)",
5073 bss->ifname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005074
5075 /*
5076 * XXX: FIX! this needs to flush all VLANs too
5077 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005078 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_DEL_STATION);
5079 res = send_and_recv_msgs(bss->drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005080 if (res) {
5081 wpa_printf(MSG_DEBUG, "nl80211: Station flush failed: ret=%d "
5082 "(%s)", res, strerror(-res));
5083 }
5084 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005085}
5086
5087
5088static int get_sta_handler(struct nl_msg *msg, void *arg)
5089{
5090 struct nlattr *tb[NL80211_ATTR_MAX + 1];
5091 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
5092 struct hostap_sta_driver_data *data = arg;
5093 struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
5094 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
5095 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
5096 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
5097 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
5098 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
5099 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03005100 [NL80211_STA_INFO_TX_FAILED] = { .type = NLA_U32 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005101 };
5102
5103 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
5104 genlmsg_attrlen(gnlh, 0), NULL);
5105
5106 /*
5107 * TODO: validate the interface and mac address!
5108 * Otherwise, there's a race condition as soon as
5109 * the kernel starts sending station notifications.
5110 */
5111
5112 if (!tb[NL80211_ATTR_STA_INFO]) {
5113 wpa_printf(MSG_DEBUG, "sta stats missing!");
5114 return NL_SKIP;
5115 }
5116 if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
5117 tb[NL80211_ATTR_STA_INFO],
5118 stats_policy)) {
5119 wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
5120 return NL_SKIP;
5121 }
5122
5123 if (stats[NL80211_STA_INFO_INACTIVE_TIME])
5124 data->inactive_msec =
5125 nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
5126 if (stats[NL80211_STA_INFO_RX_BYTES])
5127 data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
5128 if (stats[NL80211_STA_INFO_TX_BYTES])
5129 data->tx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
5130 if (stats[NL80211_STA_INFO_RX_PACKETS])
5131 data->rx_packets =
5132 nla_get_u32(stats[NL80211_STA_INFO_RX_PACKETS]);
5133 if (stats[NL80211_STA_INFO_TX_PACKETS])
5134 data->tx_packets =
5135 nla_get_u32(stats[NL80211_STA_INFO_TX_PACKETS]);
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03005136 if (stats[NL80211_STA_INFO_TX_FAILED])
5137 data->tx_retry_failed =
5138 nla_get_u32(stats[NL80211_STA_INFO_TX_FAILED]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005139
5140 return NL_SKIP;
5141}
5142
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005143static int i802_read_sta_data(struct i802_bss *bss,
5144 struct hostap_sta_driver_data *data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005145 const u8 *addr)
5146{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005147 struct nl_msg *msg;
5148
5149 os_memset(data, 0, sizeof(*data));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005150
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005151 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_GET_STATION)) ||
5152 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
5153 nlmsg_free(msg);
5154 return -ENOBUFS;
5155 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005156
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005157 return send_and_recv_msgs(bss->drv, msg, get_sta_handler, data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005158}
5159
5160
5161static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
5162 int cw_min, int cw_max, int burst_time)
5163{
5164 struct i802_bss *bss = priv;
5165 struct wpa_driver_nl80211_data *drv = bss->drv;
5166 struct nl_msg *msg;
5167 struct nlattr *txq, *params;
5168
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005169 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005170 if (!msg)
5171 return -1;
5172
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005173 txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS);
5174 if (!txq)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005175 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005176
5177 /* We are only sending parameters for a single TXQ at a time */
5178 params = nla_nest_start(msg, 1);
5179 if (!params)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005180 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005181
5182 switch (queue) {
5183 case 0:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005184 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VO))
5185 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005186 break;
5187 case 1:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005188 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VI))
5189 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005190 break;
5191 case 2:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005192 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BE))
5193 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005194 break;
5195 case 3:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005196 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BK))
5197 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005198 break;
5199 }
5200 /* Burst time is configured in units of 0.1 msec and TXOP parameter in
5201 * 32 usec, so need to convert the value here. */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005202 if (nla_put_u16(msg, NL80211_TXQ_ATTR_TXOP,
5203 (burst_time * 100 + 16) / 32) ||
5204 nla_put_u16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min) ||
5205 nla_put_u16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max) ||
5206 nla_put_u8(msg, NL80211_TXQ_ATTR_AIFS, aifs))
5207 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005208
5209 nla_nest_end(msg, params);
5210
5211 nla_nest_end(msg, txq);
5212
5213 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
5214 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005215 msg = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005216fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005217 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005218 return -1;
5219}
5220
5221
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005222static int i802_set_sta_vlan(struct i802_bss *bss, const u8 *addr,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005223 const char *ifname, int vlan_id)
5224{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005225 struct wpa_driver_nl80211_data *drv = bss->drv;
5226 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005227 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005228
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005229 wpa_printf(MSG_DEBUG, "nl80211: %s[%d]: set_sta_vlan(" MACSTR
5230 ", ifname=%s[%d], vlan_id=%d)",
5231 bss->ifname, if_nametoindex(bss->ifname),
5232 MAC2STR(addr), ifname, if_nametoindex(ifname), vlan_id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005233 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
5234 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
5235 nla_put_u32(msg, NL80211_ATTR_STA_VLAN, if_nametoindex(ifname))) {
5236 nlmsg_free(msg);
5237 return -ENOBUFS;
5238 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005239
5240 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5241 if (ret < 0) {
5242 wpa_printf(MSG_ERROR, "nl80211: NL80211_ATTR_STA_VLAN (addr="
5243 MACSTR " ifname=%s vlan_id=%d) failed: %d (%s)",
5244 MAC2STR(addr), ifname, vlan_id, ret,
5245 strerror(-ret));
5246 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005247 return ret;
5248}
5249
5250
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005251static int i802_get_inact_sec(void *priv, const u8 *addr)
5252{
5253 struct hostap_sta_driver_data data;
5254 int ret;
5255
5256 data.inactive_msec = (unsigned long) -1;
5257 ret = i802_read_sta_data(priv, &data, addr);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08005258 if (ret == -ENOENT)
5259 return -ENOENT;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005260 if (ret || data.inactive_msec == (unsigned long) -1)
5261 return -1;
5262 return data.inactive_msec / 1000;
5263}
5264
5265
5266static int i802_sta_clear_stats(void *priv, const u8 *addr)
5267{
5268#if 0
5269 /* TODO */
5270#endif
5271 return 0;
5272}
5273
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005274
5275static int i802_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
5276 int reason)
5277{
5278 struct i802_bss *bss = priv;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005279 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005280 struct ieee80211_mgmt mgmt;
5281
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005282 if (is_mesh_interface(drv->nlmode))
5283 return -1;
5284
Dmitry Shmidt04949592012-07-19 12:16:46 -07005285 if (drv->device_ap_sme)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005286 return wpa_driver_nl80211_sta_remove(bss, addr, 1, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005287
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005288 memset(&mgmt, 0, sizeof(mgmt));
5289 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
5290 WLAN_FC_STYPE_DEAUTH);
5291 memcpy(mgmt.da, addr, ETH_ALEN);
5292 memcpy(mgmt.sa, own_addr, ETH_ALEN);
5293 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
5294 mgmt.u.deauth.reason_code = host_to_le16(reason);
5295 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
5296 IEEE80211_HDRLEN +
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005297 sizeof(mgmt.u.deauth), 0, 0, 0, 0,
5298 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005299}
5300
5301
5302static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
5303 int reason)
5304{
5305 struct i802_bss *bss = priv;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005306 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005307 struct ieee80211_mgmt mgmt;
5308
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005309 if (is_mesh_interface(drv->nlmode))
5310 return -1;
5311
Dmitry Shmidt04949592012-07-19 12:16:46 -07005312 if (drv->device_ap_sme)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005313 return wpa_driver_nl80211_sta_remove(bss, addr, 0, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005314
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005315 memset(&mgmt, 0, sizeof(mgmt));
5316 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
5317 WLAN_FC_STYPE_DISASSOC);
5318 memcpy(mgmt.da, addr, ETH_ALEN);
5319 memcpy(mgmt.sa, own_addr, ETH_ALEN);
5320 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
5321 mgmt.u.disassoc.reason_code = host_to_le16(reason);
5322 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
5323 IEEE80211_HDRLEN +
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005324 sizeof(mgmt.u.disassoc), 0, 0, 0, 0,
5325 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005326}
5327
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005328
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07005329static void dump_ifidx(struct wpa_driver_nl80211_data *drv)
5330{
5331 char buf[200], *pos, *end;
5332 int i, res;
5333
5334 pos = buf;
5335 end = pos + sizeof(buf);
5336
5337 for (i = 0; i < drv->num_if_indices; i++) {
5338 if (!drv->if_indices[i])
5339 continue;
5340 res = os_snprintf(pos, end - pos, " %d", drv->if_indices[i]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005341 if (os_snprintf_error(end - pos, res))
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07005342 break;
5343 pos += res;
5344 }
5345 *pos = '\0';
5346
5347 wpa_printf(MSG_DEBUG, "nl80211: if_indices[%d]:%s",
5348 drv->num_if_indices, buf);
5349}
5350
5351
Jouni Malinen75ecf522011-06-27 15:19:46 -07005352static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
5353{
5354 int i;
5355 int *old;
5356
5357 wpa_printf(MSG_DEBUG, "nl80211: Add own interface ifindex %d",
5358 ifidx);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07005359 if (have_ifidx(drv, ifidx)) {
5360 wpa_printf(MSG_DEBUG, "nl80211: ifindex %d already in the list",
5361 ifidx);
5362 return;
5363 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07005364 for (i = 0; i < drv->num_if_indices; i++) {
5365 if (drv->if_indices[i] == 0) {
5366 drv->if_indices[i] = ifidx;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07005367 dump_ifidx(drv);
Jouni Malinen75ecf522011-06-27 15:19:46 -07005368 return;
5369 }
5370 }
5371
5372 if (drv->if_indices != drv->default_if_indices)
5373 old = drv->if_indices;
5374 else
5375 old = NULL;
5376
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005377 drv->if_indices = os_realloc_array(old, drv->num_if_indices + 1,
5378 sizeof(int));
Jouni Malinen75ecf522011-06-27 15:19:46 -07005379 if (!drv->if_indices) {
5380 if (!old)
5381 drv->if_indices = drv->default_if_indices;
5382 else
5383 drv->if_indices = old;
5384 wpa_printf(MSG_ERROR, "Failed to reallocate memory for "
5385 "interfaces");
5386 wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx);
5387 return;
5388 } else if (!old)
5389 os_memcpy(drv->if_indices, drv->default_if_indices,
5390 sizeof(drv->default_if_indices));
5391 drv->if_indices[drv->num_if_indices] = ifidx;
5392 drv->num_if_indices++;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07005393 dump_ifidx(drv);
Jouni Malinen75ecf522011-06-27 15:19:46 -07005394}
5395
5396
5397static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
5398{
5399 int i;
5400
5401 for (i = 0; i < drv->num_if_indices; i++) {
5402 if (drv->if_indices[i] == ifidx) {
5403 drv->if_indices[i] = 0;
5404 break;
5405 }
5406 }
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07005407 dump_ifidx(drv);
Jouni Malinen75ecf522011-06-27 15:19:46 -07005408}
5409
5410
5411static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
5412{
5413 int i;
5414
5415 for (i = 0; i < drv->num_if_indices; i++)
5416 if (drv->if_indices[i] == ifidx)
5417 return 1;
5418
5419 return 0;
5420}
5421
5422
5423static int i802_set_wds_sta(void *priv, const u8 *addr, int aid, int val,
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07005424 const char *bridge_ifname, char *ifname_wds)
Jouni Malinen75ecf522011-06-27 15:19:46 -07005425{
5426 struct i802_bss *bss = priv;
5427 struct wpa_driver_nl80211_data *drv = bss->drv;
5428 char name[IFNAMSIZ + 1];
5429
5430 os_snprintf(name, sizeof(name), "%s.sta%d", bss->ifname, aid);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005431 if (ifname_wds)
5432 os_strlcpy(ifname_wds, name, IFNAMSIZ + 1);
5433
Jouni Malinen75ecf522011-06-27 15:19:46 -07005434 wpa_printf(MSG_DEBUG, "nl80211: Set WDS STA addr=" MACSTR
5435 " aid=%d val=%d name=%s", MAC2STR(addr), aid, val, name);
5436 if (val) {
5437 if (!if_nametoindex(name)) {
5438 if (nl80211_create_iface(drv, name,
5439 NL80211_IFTYPE_AP_VLAN,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005440 bss->addr, 1, NULL, NULL, 0) <
5441 0)
Jouni Malinen75ecf522011-06-27 15:19:46 -07005442 return -1;
5443 if (bridge_ifname &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005444 linux_br_add_if(drv->global->ioctl_sock,
5445 bridge_ifname, name) < 0)
Jouni Malinen75ecf522011-06-27 15:19:46 -07005446 return -1;
5447 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005448 if (linux_set_iface_flags(drv->global->ioctl_sock, name, 1)) {
5449 wpa_printf(MSG_ERROR, "nl80211: Failed to set WDS STA "
5450 "interface %s up", name);
5451 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07005452 return i802_set_sta_vlan(priv, addr, name, 0);
5453 } else {
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005454 if (bridge_ifname)
5455 linux_br_del_if(drv->global->ioctl_sock, bridge_ifname,
5456 name);
5457
Jouni Malinen75ecf522011-06-27 15:19:46 -07005458 i802_set_sta_vlan(priv, addr, bss->ifname, 0);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08005459 nl80211_remove_iface(drv, if_nametoindex(name));
5460 return 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07005461 }
5462}
5463
5464
5465static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
5466{
5467 struct wpa_driver_nl80211_data *drv = eloop_ctx;
5468 struct sockaddr_ll lladdr;
5469 unsigned char buf[3000];
5470 int len;
5471 socklen_t fromlen = sizeof(lladdr);
5472
5473 len = recvfrom(sock, buf, sizeof(buf), 0,
5474 (struct sockaddr *)&lladdr, &fromlen);
5475 if (len < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005476 wpa_printf(MSG_ERROR, "nl80211: EAPOL recv failed: %s",
5477 strerror(errno));
Jouni Malinen75ecf522011-06-27 15:19:46 -07005478 return;
5479 }
5480
5481 if (have_ifidx(drv, lladdr.sll_ifindex))
5482 drv_event_eapol_rx(drv->ctx, lladdr.sll_addr, buf, len);
5483}
5484
5485
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005486static int i802_check_bridge(struct wpa_driver_nl80211_data *drv,
5487 struct i802_bss *bss,
5488 const char *brname, const char *ifname)
5489{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005490 int br_ifindex;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005491 char in_br[IFNAMSIZ];
5492
5493 os_strlcpy(bss->brname, brname, IFNAMSIZ);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005494 br_ifindex = if_nametoindex(brname);
5495 if (br_ifindex == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005496 /*
5497 * Bridge was configured, but the bridge device does
5498 * not exist. Try to add it now.
5499 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005500 if (linux_br_add(drv->global->ioctl_sock, brname) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005501 wpa_printf(MSG_ERROR, "nl80211: Failed to add the "
5502 "bridge interface %s: %s",
5503 brname, strerror(errno));
5504 return -1;
5505 }
5506 bss->added_bridge = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005507 br_ifindex = if_nametoindex(brname);
5508 add_ifidx(drv, br_ifindex);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005509 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005510 bss->br_ifindex = br_ifindex;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005511
5512 if (linux_br_get(in_br, ifname) == 0) {
5513 if (os_strcmp(in_br, brname) == 0)
5514 return 0; /* already in the bridge */
5515
5516 wpa_printf(MSG_DEBUG, "nl80211: Removing interface %s from "
5517 "bridge %s", ifname, in_br);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005518 if (linux_br_del_if(drv->global->ioctl_sock, in_br, ifname) <
5519 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005520 wpa_printf(MSG_ERROR, "nl80211: Failed to "
5521 "remove interface %s from bridge "
5522 "%s: %s",
5523 ifname, brname, strerror(errno));
5524 return -1;
5525 }
5526 }
5527
5528 wpa_printf(MSG_DEBUG, "nl80211: Adding interface %s into bridge %s",
5529 ifname, brname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005530 if (linux_br_add_if(drv->global->ioctl_sock, brname, ifname) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005531 wpa_printf(MSG_ERROR, "nl80211: Failed to add interface %s "
5532 "into bridge %s: %s",
5533 ifname, brname, strerror(errno));
5534 return -1;
5535 }
5536 bss->added_if_into_bridge = 1;
5537
5538 return 0;
5539}
5540
5541
5542static void *i802_init(struct hostapd_data *hapd,
5543 struct wpa_init_params *params)
5544{
5545 struct wpa_driver_nl80211_data *drv;
5546 struct i802_bss *bss;
5547 size_t i;
5548 char brname[IFNAMSIZ];
5549 int ifindex, br_ifindex;
5550 int br_added = 0;
5551
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005552 bss = wpa_driver_nl80211_drv_init(hapd, params->ifname,
5553 params->global_priv, 1,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005554 params->bssid, params->driver_params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005555 if (bss == NULL)
5556 return NULL;
5557
5558 drv = bss->drv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005559
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005560 if (linux_br_get(brname, params->ifname) == 0) {
5561 wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in bridge %s",
5562 params->ifname, brname);
5563 br_ifindex = if_nametoindex(brname);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005564 os_strlcpy(bss->brname, brname, IFNAMSIZ);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005565 } else {
5566 brname[0] = '\0';
5567 br_ifindex = 0;
5568 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005569 bss->br_ifindex = br_ifindex;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005570
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005571 for (i = 0; i < params->num_bridge; i++) {
5572 if (params->bridge[i]) {
5573 ifindex = if_nametoindex(params->bridge[i]);
5574 if (ifindex)
5575 add_ifidx(drv, ifindex);
5576 if (ifindex == br_ifindex)
5577 br_added = 1;
5578 }
5579 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005580
5581 /* start listening for EAPOL on the default AP interface */
5582 add_ifidx(drv, drv->ifindex);
5583
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005584 if (params->num_bridge && params->bridge[0]) {
5585 if (i802_check_bridge(drv, bss, params->bridge[0],
5586 params->ifname) < 0)
5587 goto failed;
5588 if (os_strcmp(params->bridge[0], brname) != 0)
5589 br_added = 1;
5590 }
5591
5592 if (!br_added && br_ifindex &&
5593 (params->num_bridge == 0 || !params->bridge[0]))
5594 add_ifidx(drv, br_ifindex);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005595
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005596#ifdef CONFIG_LIBNL3_ROUTE
5597 if (bss->added_if_into_bridge) {
5598 drv->rtnl_sk = nl_socket_alloc();
5599 if (drv->rtnl_sk == NULL) {
5600 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate nl_sock");
5601 goto failed;
5602 }
5603
5604 if (nl_connect(drv->rtnl_sk, NETLINK_ROUTE)) {
5605 wpa_printf(MSG_ERROR, "nl80211: Failed to connect nl_sock to NETLINK_ROUTE: %s",
5606 strerror(errno));
5607 goto failed;
5608 }
5609 }
5610#endif /* CONFIG_LIBNL3_ROUTE */
5611
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005612 drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
5613 if (drv->eapol_sock < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005614 wpa_printf(MSG_ERROR, "nl80211: socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE) failed: %s",
5615 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005616 goto failed;
5617 }
5618
5619 if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
5620 {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005621 wpa_printf(MSG_INFO, "nl80211: Could not register read socket for eapol");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005622 goto failed;
5623 }
5624
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005625 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
5626 params->own_addr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005627 goto failed;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005628 os_memcpy(drv->perm_addr, params->own_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005629
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005630 memcpy(bss->addr, params->own_addr, ETH_ALEN);
5631
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005632 return bss;
5633
5634failed:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005635 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005636 return NULL;
5637}
5638
5639
5640static void i802_deinit(void *priv)
5641{
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005642 struct i802_bss *bss = priv;
5643 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005644}
5645
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005646
5647static enum nl80211_iftype wpa_driver_nl80211_if_type(
5648 enum wpa_driver_if_type type)
5649{
5650 switch (type) {
5651 case WPA_IF_STATION:
5652 return NL80211_IFTYPE_STATION;
5653 case WPA_IF_P2P_CLIENT:
5654 case WPA_IF_P2P_GROUP:
5655 return NL80211_IFTYPE_P2P_CLIENT;
5656 case WPA_IF_AP_VLAN:
5657 return NL80211_IFTYPE_AP_VLAN;
5658 case WPA_IF_AP_BSS:
5659 return NL80211_IFTYPE_AP;
5660 case WPA_IF_P2P_GO:
5661 return NL80211_IFTYPE_P2P_GO;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005662 case WPA_IF_P2P_DEVICE:
5663 return NL80211_IFTYPE_P2P_DEVICE;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005664 case WPA_IF_MESH:
5665 return NL80211_IFTYPE_MESH_POINT;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005666 }
5667 return -1;
5668}
5669
5670
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005671#if defined(CONFIG_P2P) || defined(CONFIG_MESH)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005672
5673static int nl80211_addr_in_use(struct nl80211_global *global, const u8 *addr)
5674{
5675 struct wpa_driver_nl80211_data *drv;
5676 dl_list_for_each(drv, &global->interfaces,
5677 struct wpa_driver_nl80211_data, list) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005678 if (os_memcmp(addr, drv->first_bss->addr, ETH_ALEN) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005679 return 1;
5680 }
5681 return 0;
5682}
5683
5684
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005685static int nl80211_vif_addr(struct wpa_driver_nl80211_data *drv, u8 *new_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005686{
5687 unsigned int idx;
5688
5689 if (!drv->global)
5690 return -1;
5691
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005692 os_memcpy(new_addr, drv->first_bss->addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005693 for (idx = 0; idx < 64; idx++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005694 new_addr[0] = drv->first_bss->addr[0] | 0x02;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005695 new_addr[0] ^= idx << 2;
5696 if (!nl80211_addr_in_use(drv->global, new_addr))
5697 break;
5698 }
5699 if (idx == 64)
5700 return -1;
5701
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005702 wpa_printf(MSG_DEBUG, "nl80211: Assigned new virtual interface address "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005703 MACSTR, MAC2STR(new_addr));
5704
5705 return 0;
5706}
5707
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005708#endif /* CONFIG_P2P || CONFIG_MESH */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005709
5710
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005711struct wdev_info {
5712 u64 wdev_id;
5713 int wdev_id_set;
5714 u8 macaddr[ETH_ALEN];
5715};
5716
5717static int nl80211_wdev_handler(struct nl_msg *msg, void *arg)
5718{
5719 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
5720 struct nlattr *tb[NL80211_ATTR_MAX + 1];
5721 struct wdev_info *wi = arg;
5722
5723 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
5724 genlmsg_attrlen(gnlh, 0), NULL);
5725 if (tb[NL80211_ATTR_WDEV]) {
5726 wi->wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
5727 wi->wdev_id_set = 1;
5728 }
5729
5730 if (tb[NL80211_ATTR_MAC])
5731 os_memcpy(wi->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
5732 ETH_ALEN);
5733
5734 return NL_SKIP;
5735}
5736
5737
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005738static int wpa_driver_nl80211_if_add(void *priv, enum wpa_driver_if_type type,
5739 const char *ifname, const u8 *addr,
5740 void *bss_ctx, void **drv_priv,
5741 char *force_ifname, u8 *if_addr,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005742 const char *bridge, int use_existing)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005743{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005744 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005745 struct i802_bss *bss = priv;
5746 struct wpa_driver_nl80211_data *drv = bss->drv;
5747 int ifidx;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005748 int added = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005749
5750 if (addr)
5751 os_memcpy(if_addr, addr, ETH_ALEN);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005752 nlmode = wpa_driver_nl80211_if_type(type);
5753 if (nlmode == NL80211_IFTYPE_P2P_DEVICE) {
5754 struct wdev_info p2pdev_info;
5755
5756 os_memset(&p2pdev_info, 0, sizeof(p2pdev_info));
5757 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
5758 0, nl80211_wdev_handler,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005759 &p2pdev_info, use_existing);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005760 if (!p2pdev_info.wdev_id_set || ifidx != 0) {
5761 wpa_printf(MSG_ERROR, "nl80211: Failed to create a P2P Device interface %s",
5762 ifname);
5763 return -1;
5764 }
5765
5766 drv->global->if_add_wdevid = p2pdev_info.wdev_id;
5767 drv->global->if_add_wdevid_set = p2pdev_info.wdev_id_set;
5768 if (!is_zero_ether_addr(p2pdev_info.macaddr))
5769 os_memcpy(if_addr, p2pdev_info.macaddr, ETH_ALEN);
5770 wpa_printf(MSG_DEBUG, "nl80211: New P2P Device interface %s (0x%llx) created",
5771 ifname,
5772 (long long unsigned int) p2pdev_info.wdev_id);
5773 } else {
5774 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005775 0, NULL, NULL, use_existing);
5776 if (use_existing && ifidx == -ENFILE) {
5777 added = 0;
5778 ifidx = if_nametoindex(ifname);
5779 } else if (ifidx < 0) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005780 return -1;
5781 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005782 }
5783
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005784 if (!addr) {
5785 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
5786 os_memcpy(if_addr, bss->addr, ETH_ALEN);
5787 else if (linux_get_ifhwaddr(drv->global->ioctl_sock,
5788 bss->ifname, if_addr) < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005789 if (added)
5790 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005791 return -1;
5792 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005793 }
5794
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005795#if defined(CONFIG_P2P) || defined(CONFIG_MESH)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005796 if (!addr &&
5797 (type == WPA_IF_P2P_CLIENT || type == WPA_IF_P2P_GROUP ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005798 type == WPA_IF_P2P_GO || type == WPA_IF_MESH)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005799 /* Enforce unique P2P Interface Address */
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005800 u8 new_addr[ETH_ALEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005801
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005802 if (linux_get_ifhwaddr(drv->global->ioctl_sock, ifname,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005803 new_addr) < 0) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07005804 if (added)
5805 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005806 return -1;
5807 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005808 if (nl80211_addr_in_use(drv->global, new_addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005809 wpa_printf(MSG_DEBUG, "nl80211: Allocate new address "
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005810 "for %s interface", type == WPA_IF_MESH ?
5811 "mesh" : "P2P group");
5812 if (nl80211_vif_addr(drv, new_addr) < 0) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07005813 if (added)
5814 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005815 return -1;
5816 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005817 if (linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005818 new_addr) < 0) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07005819 if (added)
5820 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005821 return -1;
5822 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005823 }
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07005824 os_memcpy(if_addr, new_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005825 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005826#endif /* CONFIG_P2P || CONFIG_MESH */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005827
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005828 if (type == WPA_IF_AP_BSS) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005829 struct i802_bss *new_bss = os_zalloc(sizeof(*new_bss));
5830 if (new_bss == NULL) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005831 if (added)
5832 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005833 return -1;
5834 }
5835
5836 if (bridge &&
5837 i802_check_bridge(drv, new_bss, bridge, ifname) < 0) {
5838 wpa_printf(MSG_ERROR, "nl80211: Failed to add the new "
5839 "interface %s to a bridge %s",
5840 ifname, bridge);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005841 if (added)
5842 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005843 os_free(new_bss);
5844 return -1;
5845 }
5846
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005847 if (linux_set_iface_flags(drv->global->ioctl_sock, ifname, 1))
5848 {
Dmitry Shmidt71757432014-06-02 13:50:35 -07005849 if (added)
5850 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005851 os_free(new_bss);
5852 return -1;
5853 }
5854 os_strlcpy(new_bss->ifname, ifname, IFNAMSIZ);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005855 os_memcpy(new_bss->addr, if_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005856 new_bss->ifindex = ifidx;
5857 new_bss->drv = drv;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005858 new_bss->next = drv->first_bss->next;
5859 new_bss->freq = drv->first_bss->freq;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005860 new_bss->ctx = bss_ctx;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005861 new_bss->added_if = added;
5862 drv->first_bss->next = new_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005863 if (drv_priv)
5864 *drv_priv = new_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005865 nl80211_init_bss(new_bss);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005866
5867 /* Subscribe management frames for this WPA_IF_AP_BSS */
5868 if (nl80211_setup_ap(new_bss))
5869 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005870 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005871
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005872 if (drv->global)
5873 drv->global->if_add_ifindex = ifidx;
5874
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005875 /*
5876 * Some virtual interfaces need to process EAPOL packets and events on
5877 * the parent interface. This is used mainly with hostapd.
5878 */
5879 if (ifidx > 0 &&
5880 (drv->hostapd ||
5881 nlmode == NL80211_IFTYPE_AP_VLAN ||
5882 nlmode == NL80211_IFTYPE_WDS ||
5883 nlmode == NL80211_IFTYPE_MONITOR))
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07005884 add_ifidx(drv, ifidx);
5885
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005886 return 0;
5887}
5888
5889
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005890static int wpa_driver_nl80211_if_remove(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005891 enum wpa_driver_if_type type,
5892 const char *ifname)
5893{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005894 struct wpa_driver_nl80211_data *drv = bss->drv;
5895 int ifindex = if_nametoindex(ifname);
5896
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005897 wpa_printf(MSG_DEBUG, "nl80211: %s(type=%d ifname=%s) ifindex=%d added_if=%d",
5898 __func__, type, ifname, ifindex, bss->added_if);
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08005899 if (ifindex > 0 && (bss->added_if || bss->ifindex != ifindex))
Dmitry Shmidt051af732013-10-22 13:52:46 -07005900 nl80211_remove_iface(drv, ifindex);
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07005901 else if (ifindex > 0 && !bss->added_if) {
5902 struct wpa_driver_nl80211_data *drv2;
5903 dl_list_for_each(drv2, &drv->global->interfaces,
5904 struct wpa_driver_nl80211_data, list)
5905 del_ifidx(drv2, ifindex);
5906 }
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005907
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005908 if (type != WPA_IF_AP_BSS)
5909 return 0;
5910
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005911 if (bss->added_if_into_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005912 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
5913 bss->ifname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005914 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
5915 "interface %s from bridge %s: %s",
5916 bss->ifname, bss->brname, strerror(errno));
5917 }
5918 if (bss->added_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005919 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005920 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
5921 "bridge %s: %s",
5922 bss->brname, strerror(errno));
5923 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005924
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005925 if (bss != drv->first_bss) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005926 struct i802_bss *tbss;
5927
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005928 wpa_printf(MSG_DEBUG, "nl80211: Not the first BSS - remove it");
5929 for (tbss = drv->first_bss; tbss; tbss = tbss->next) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005930 if (tbss->next == bss) {
5931 tbss->next = bss->next;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005932 /* Unsubscribe management frames */
5933 nl80211_teardown_ap(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005934 nl80211_destroy_bss(bss);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07005935 if (!bss->added_if)
5936 i802_set_iface_flags(bss, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005937 os_free(bss);
5938 bss = NULL;
5939 break;
5940 }
5941 }
5942 if (bss)
5943 wpa_printf(MSG_INFO, "nl80211: %s - could not find "
5944 "BSS %p in the list", __func__, bss);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005945 } else {
5946 wpa_printf(MSG_DEBUG, "nl80211: First BSS - reassign context");
5947 nl80211_teardown_ap(bss);
5948 if (!bss->added_if && !drv->first_bss->next)
5949 wpa_driver_nl80211_del_beacon(drv);
5950 nl80211_destroy_bss(bss);
5951 if (!bss->added_if)
5952 i802_set_iface_flags(bss, 0);
5953 if (drv->first_bss->next) {
5954 drv->first_bss = drv->first_bss->next;
5955 drv->ctx = drv->first_bss->ctx;
5956 os_free(bss);
5957 } else {
5958 wpa_printf(MSG_DEBUG, "nl80211: No second BSS to reassign context to");
5959 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005960 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005961
5962 return 0;
5963}
5964
5965
5966static int cookie_handler(struct nl_msg *msg, void *arg)
5967{
5968 struct nlattr *tb[NL80211_ATTR_MAX + 1];
5969 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
5970 u64 *cookie = arg;
5971 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
5972 genlmsg_attrlen(gnlh, 0), NULL);
5973 if (tb[NL80211_ATTR_COOKIE])
5974 *cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
5975 return NL_SKIP;
5976}
5977
5978
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005979static int nl80211_send_frame_cmd(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005980 unsigned int freq, unsigned int wait,
5981 const u8 *buf, size_t buf_len,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005982 u64 *cookie_out, int no_cck, int no_ack,
5983 int offchanok)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005984{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005985 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005986 struct nl_msg *msg;
5987 u64 cookie;
5988 int ret = -1;
5989
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005990 wpa_printf(MSG_MSGDUMP, "nl80211: CMD_FRAME freq=%u wait=%u no_cck=%d "
Dmitry Shmidt04949592012-07-19 12:16:46 -07005991 "no_ack=%d offchanok=%d",
5992 freq, wait, no_cck, no_ack, offchanok);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005993 wpa_hexdump(MSG_MSGDUMP, "CMD_FRAME", buf, buf_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005994
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005995 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_FRAME)) ||
5996 (freq && nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
5997 (wait && nla_put_u32(msg, NL80211_ATTR_DURATION, wait)) ||
5998 (offchanok && ((drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
5999 drv->test_use_roc_tx) &&
6000 nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK)) ||
6001 (no_cck && nla_put_flag(msg, NL80211_ATTR_TX_NO_CCK_RATE)) ||
6002 (no_ack && nla_put_flag(msg, NL80211_ATTR_DONT_WAIT_FOR_ACK)) ||
6003 nla_put(msg, NL80211_ATTR_FRAME, buf_len, buf))
6004 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006005
6006 cookie = 0;
6007 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
6008 msg = NULL;
6009 if (ret) {
6010 wpa_printf(MSG_DEBUG, "nl80211: Frame command failed: ret=%d "
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07006011 "(%s) (freq=%u wait=%u)", ret, strerror(-ret),
6012 freq, wait);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006013 } else {
6014 wpa_printf(MSG_MSGDUMP, "nl80211: Frame TX command accepted%s; "
6015 "cookie 0x%llx", no_ack ? " (no ACK)" : "",
6016 (long long unsigned int) cookie);
6017
6018 if (cookie_out)
6019 *cookie_out = no_ack ? (u64) -1 : cookie;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006020 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006021
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006022fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006023 nlmsg_free(msg);
6024 return ret;
6025}
6026
6027
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006028static int wpa_driver_nl80211_send_action(struct i802_bss *bss,
6029 unsigned int freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006030 unsigned int wait_time,
6031 const u8 *dst, const u8 *src,
6032 const u8 *bssid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006033 const u8 *data, size_t data_len,
6034 int no_cck)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006035{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006036 struct wpa_driver_nl80211_data *drv = bss->drv;
6037 int ret = -1;
6038 u8 *buf;
6039 struct ieee80211_hdr *hdr;
6040
6041 wpa_printf(MSG_DEBUG, "nl80211: Send Action frame (ifindex=%d, "
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006042 "freq=%u MHz wait=%d ms no_cck=%d)",
6043 drv->ifindex, freq, wait_time, no_cck);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006044
6045 buf = os_zalloc(24 + data_len);
6046 if (buf == NULL)
6047 return ret;
6048 os_memcpy(buf + 24, data, data_len);
6049 hdr = (struct ieee80211_hdr *) buf;
6050 hdr->frame_control =
6051 IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_ACTION);
6052 os_memcpy(hdr->addr1, dst, ETH_ALEN);
6053 os_memcpy(hdr->addr2, src, ETH_ALEN);
6054 os_memcpy(hdr->addr3, bssid, ETH_ALEN);
6055
Dmitry Shmidt56052862013-10-04 10:23:25 -07006056 if (is_ap_interface(drv->nlmode) &&
6057 (!(drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
6058 (int) freq == bss->freq || drv->device_ap_sme ||
6059 !drv->use_monitor))
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006060 ret = wpa_driver_nl80211_send_mlme(bss, buf, 24 + data_len,
6061 0, freq, no_cck, 1,
6062 wait_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006063 else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006064 ret = nl80211_send_frame_cmd(bss, freq, wait_time, buf,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006065 24 + data_len,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006066 &drv->send_action_cookie,
6067 no_cck, 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006068
6069 os_free(buf);
6070 return ret;
6071}
6072
6073
6074static void wpa_driver_nl80211_send_action_cancel_wait(void *priv)
6075{
6076 struct i802_bss *bss = priv;
6077 struct wpa_driver_nl80211_data *drv = bss->drv;
6078 struct nl_msg *msg;
6079 int ret;
6080
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08006081 wpa_printf(MSG_DEBUG, "nl80211: Cancel TX frame wait: cookie=0x%llx",
6082 (long long unsigned int) drv->send_action_cookie);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006083 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_FRAME_WAIT_CANCEL)) ||
6084 nla_put_u64(msg, NL80211_ATTR_COOKIE, drv->send_action_cookie)) {
6085 nlmsg_free(msg);
6086 return;
6087 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006088
6089 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006090 if (ret)
6091 wpa_printf(MSG_DEBUG, "nl80211: wait cancel failed: ret=%d "
6092 "(%s)", ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006093}
6094
6095
6096static int wpa_driver_nl80211_remain_on_channel(void *priv, unsigned int freq,
6097 unsigned int duration)
6098{
6099 struct i802_bss *bss = priv;
6100 struct wpa_driver_nl80211_data *drv = bss->drv;
6101 struct nl_msg *msg;
6102 int ret;
6103 u64 cookie;
6104
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006105 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_REMAIN_ON_CHANNEL)) ||
6106 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
6107 nla_put_u32(msg, NL80211_ATTR_DURATION, duration)) {
6108 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006109 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006110 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006111
6112 cookie = 0;
6113 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
6114 if (ret == 0) {
6115 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel cookie "
6116 "0x%llx for freq=%u MHz duration=%u",
6117 (long long unsigned int) cookie, freq, duration);
6118 drv->remain_on_chan_cookie = cookie;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006119 drv->pending_remain_on_chan = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006120 return 0;
6121 }
6122 wpa_printf(MSG_DEBUG, "nl80211: Failed to request remain-on-channel "
6123 "(freq=%d duration=%u): %d (%s)",
6124 freq, duration, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006125 return -1;
6126}
6127
6128
6129static int wpa_driver_nl80211_cancel_remain_on_channel(void *priv)
6130{
6131 struct i802_bss *bss = priv;
6132 struct wpa_driver_nl80211_data *drv = bss->drv;
6133 struct nl_msg *msg;
6134 int ret;
6135
6136 if (!drv->pending_remain_on_chan) {
6137 wpa_printf(MSG_DEBUG, "nl80211: No pending remain-on-channel "
6138 "to cancel");
6139 return -1;
6140 }
6141
6142 wpa_printf(MSG_DEBUG, "nl80211: Cancel remain-on-channel with cookie "
6143 "0x%llx",
6144 (long long unsigned int) drv->remain_on_chan_cookie);
6145
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006146 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL);
6147 if (!msg ||
6148 nla_put_u64(msg, NL80211_ATTR_COOKIE, drv->remain_on_chan_cookie)) {
6149 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006150 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006151 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006152
6153 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6154 if (ret == 0)
6155 return 0;
6156 wpa_printf(MSG_DEBUG, "nl80211: Failed to cancel remain-on-channel: "
6157 "%d (%s)", ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006158 return -1;
6159}
6160
6161
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006162static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss, int report)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006163{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006164 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07006165
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006166 if (!report) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006167 if (bss->nl_preq && drv->device_ap_sme &&
Dmitry Shmidt03658832014-08-13 11:03:49 -07006168 is_ap_interface(drv->nlmode) && !bss->in_deinit &&
6169 !bss->static_ap) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006170 /*
6171 * Do not disable Probe Request reporting that was
6172 * enabled in nl80211_setup_ap().
6173 */
6174 wpa_printf(MSG_DEBUG, "nl80211: Skip disabling of "
6175 "Probe Request reporting nl_preq=%p while "
6176 "in AP mode", bss->nl_preq);
6177 } else if (bss->nl_preq) {
6178 wpa_printf(MSG_DEBUG, "nl80211: Disable Probe Request "
6179 "reporting nl_preq=%p", bss->nl_preq);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006180 nl80211_destroy_eloop_handle(&bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006181 }
6182 return 0;
6183 }
6184
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006185 if (bss->nl_preq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006186 wpa_printf(MSG_DEBUG, "nl80211: Probe Request reporting "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006187 "already on! nl_preq=%p", bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006188 return 0;
6189 }
6190
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006191 bss->nl_preq = nl_create_handle(drv->global->nl_cb, "preq");
6192 if (bss->nl_preq == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006193 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006194 wpa_printf(MSG_DEBUG, "nl80211: Enable Probe Request "
6195 "reporting nl_preq=%p", bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006196
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006197 if (nl80211_register_frame(bss, bss->nl_preq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006198 (WLAN_FC_TYPE_MGMT << 2) |
6199 (WLAN_FC_STYPE_PROBE_REQ << 4),
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006200 NULL, 0) < 0)
6201 goto out_err;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07006202
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006203 nl80211_register_eloop_read(&bss->nl_preq,
6204 wpa_driver_nl80211_event_receive,
6205 bss->nl_cb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006206
6207 return 0;
6208
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006209 out_err:
6210 nl_destroy_handles(&bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006211 return -1;
6212}
6213
6214
6215static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
6216 int ifindex, int disabled)
6217{
6218 struct nl_msg *msg;
6219 struct nlattr *bands, *band;
6220 int ret;
6221
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006222 wpa_printf(MSG_DEBUG,
6223 "nl80211: NL80211_CMD_SET_TX_BITRATE_MASK (ifindex=%d %s)",
6224 ifindex, disabled ? "NL80211_TXRATE_LEGACY=OFDM-only" :
6225 "no NL80211_TXRATE_LEGACY constraint");
6226
6227 msg = nl80211_ifindex_msg(drv, ifindex, 0,
6228 NL80211_CMD_SET_TX_BITRATE_MASK);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006229 if (!msg)
6230 return -1;
6231
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006232 bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES);
6233 if (!bands)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006234 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006235
6236 /*
6237 * Disable 2 GHz rates 1, 2, 5.5, 11 Mbps by masking out everything
6238 * else apart from 6, 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS
6239 * rates. All 5 GHz rates are left enabled.
6240 */
6241 band = nla_nest_start(msg, NL80211_BAND_2GHZ);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006242 if (!band ||
6243 (disabled && nla_put(msg, NL80211_TXRATE_LEGACY, 8,
6244 "\x0c\x12\x18\x24\x30\x48\x60\x6c")))
6245 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006246 nla_nest_end(msg, band);
6247
6248 nla_nest_end(msg, bands);
6249
6250 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006251 if (ret) {
6252 wpa_printf(MSG_DEBUG, "nl80211: Set TX rates failed: ret=%d "
6253 "(%s)", ret, strerror(-ret));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006254 } else
6255 drv->disabled_11b_rates = disabled;
6256
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006257 return ret;
6258
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006259fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006260 nlmsg_free(msg);
6261 return -1;
6262}
6263
6264
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006265static int wpa_driver_nl80211_deinit_ap(void *priv)
6266{
6267 struct i802_bss *bss = priv;
6268 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006269 if (!is_ap_interface(drv->nlmode))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006270 return -1;
6271 wpa_driver_nl80211_del_beacon(drv);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006272 bss->beacon_set = 0;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006273
6274 /*
6275 * If the P2P GO interface was dynamically added, then it is
6276 * possible that the interface change to station is not possible.
6277 */
6278 if (drv->nlmode == NL80211_IFTYPE_P2P_GO && bss->if_dynamic)
6279 return 0;
6280
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006281 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006282}
6283
6284
Dmitry Shmidtea69e842013-05-13 14:52:28 -07006285static int wpa_driver_nl80211_stop_ap(void *priv)
6286{
6287 struct i802_bss *bss = priv;
6288 struct wpa_driver_nl80211_data *drv = bss->drv;
6289 if (!is_ap_interface(drv->nlmode))
6290 return -1;
6291 wpa_driver_nl80211_del_beacon(drv);
6292 bss->beacon_set = 0;
6293 return 0;
6294}
6295
6296
Dmitry Shmidt04949592012-07-19 12:16:46 -07006297static int wpa_driver_nl80211_deinit_p2p_cli(void *priv)
6298{
6299 struct i802_bss *bss = priv;
6300 struct wpa_driver_nl80211_data *drv = bss->drv;
6301 if (drv->nlmode != NL80211_IFTYPE_P2P_CLIENT)
6302 return -1;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006303
6304 /*
6305 * If the P2P Client interface was dynamically added, then it is
6306 * possible that the interface change to station is not possible.
6307 */
6308 if (bss->if_dynamic)
6309 return 0;
6310
Dmitry Shmidt04949592012-07-19 12:16:46 -07006311 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
6312}
6313
6314
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006315static void wpa_driver_nl80211_resume(void *priv)
6316{
6317 struct i802_bss *bss = priv;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006318
6319 if (i802_set_iface_flags(bss, 1))
6320 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface up on resume event");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006321}
6322
6323
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006324static int nl80211_signal_monitor(void *priv, int threshold, int hysteresis)
6325{
6326 struct i802_bss *bss = priv;
6327 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006328 struct nl_msg *msg;
6329 struct nlattr *cqm;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006330
6331 wpa_printf(MSG_DEBUG, "nl80211: Signal monitor threshold=%d "
6332 "hysteresis=%d", threshold, hysteresis);
6333
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006334 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_CQM)) ||
6335 !(cqm = nla_nest_start(msg, NL80211_ATTR_CQM)) ||
6336 nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THOLD, threshold) ||
6337 nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_HYST, hysteresis)) {
6338 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006339 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006340 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006341 nla_nest_end(msg, cqm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006342
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006343 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006344}
6345
6346
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006347static int get_channel_width(struct nl_msg *msg, void *arg)
6348{
6349 struct nlattr *tb[NL80211_ATTR_MAX + 1];
6350 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6351 struct wpa_signal_info *sig_change = arg;
6352
6353 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6354 genlmsg_attrlen(gnlh, 0), NULL);
6355
6356 sig_change->center_frq1 = -1;
6357 sig_change->center_frq2 = -1;
6358 sig_change->chanwidth = CHAN_WIDTH_UNKNOWN;
6359
6360 if (tb[NL80211_ATTR_CHANNEL_WIDTH]) {
6361 sig_change->chanwidth = convert2width(
6362 nla_get_u32(tb[NL80211_ATTR_CHANNEL_WIDTH]));
6363 if (tb[NL80211_ATTR_CENTER_FREQ1])
6364 sig_change->center_frq1 =
6365 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]);
6366 if (tb[NL80211_ATTR_CENTER_FREQ2])
6367 sig_change->center_frq2 =
6368 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]);
6369 }
6370
6371 return NL_SKIP;
6372}
6373
6374
6375static int nl80211_get_channel_width(struct wpa_driver_nl80211_data *drv,
6376 struct wpa_signal_info *sig)
6377{
6378 struct nl_msg *msg;
6379
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006380 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_GET_INTERFACE);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006381 return send_and_recv_msgs(drv, msg, get_channel_width, sig);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006382}
6383
6384
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006385static int nl80211_signal_poll(void *priv, struct wpa_signal_info *si)
6386{
6387 struct i802_bss *bss = priv;
6388 struct wpa_driver_nl80211_data *drv = bss->drv;
6389 int res;
6390
6391 os_memset(si, 0, sizeof(*si));
6392 res = nl80211_get_link_signal(drv, si);
6393 if (res != 0)
6394 return res;
6395
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006396 res = nl80211_get_channel_width(drv, si);
6397 if (res != 0)
6398 return res;
6399
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006400 return nl80211_get_link_noise(drv, si);
6401}
6402
6403
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006404static int wpa_driver_nl80211_shared_freq(void *priv)
6405{
6406 struct i802_bss *bss = priv;
6407 struct wpa_driver_nl80211_data *drv = bss->drv;
6408 struct wpa_driver_nl80211_data *driver;
6409 int freq = 0;
6410
6411 /*
6412 * If the same PHY is in connected state with some other interface,
6413 * then retrieve the assoc freq.
6414 */
6415 wpa_printf(MSG_DEBUG, "nl80211: Get shared freq for PHY %s",
6416 drv->phyname);
6417
6418 dl_list_for_each(driver, &drv->global->interfaces,
6419 struct wpa_driver_nl80211_data, list) {
6420 if (drv == driver ||
6421 os_strcmp(drv->phyname, driver->phyname) != 0 ||
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006422 !driver->associated)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006423 continue;
6424
6425 wpa_printf(MSG_DEBUG, "nl80211: Found a match for PHY %s - %s "
6426 MACSTR,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006427 driver->phyname, driver->first_bss->ifname,
6428 MAC2STR(driver->first_bss->addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07006429 if (is_ap_interface(driver->nlmode))
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006430 freq = driver->first_bss->freq;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006431 else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006432 freq = nl80211_get_assoc_freq(driver);
6433 wpa_printf(MSG_DEBUG, "nl80211: Shared freq for PHY %s: %d",
6434 drv->phyname, freq);
6435 }
6436
6437 if (!freq)
6438 wpa_printf(MSG_DEBUG, "nl80211: No shared interface for "
6439 "PHY (%s) in associated state", drv->phyname);
6440
6441 return freq;
6442}
6443
6444
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006445static int nl80211_send_frame(void *priv, const u8 *data, size_t data_len,
6446 int encrypt)
6447{
6448 struct i802_bss *bss = priv;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006449 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt, 0,
6450 0, 0, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006451}
6452
6453
6454static int nl80211_set_param(void *priv, const char *param)
6455{
6456 wpa_printf(MSG_DEBUG, "nl80211: driver param='%s'", param);
6457 if (param == NULL)
6458 return 0;
6459
6460#ifdef CONFIG_P2P
6461 if (os_strstr(param, "use_p2p_group_interface=1")) {
6462 struct i802_bss *bss = priv;
6463 struct wpa_driver_nl80211_data *drv = bss->drv;
6464
6465 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
6466 "interface");
6467 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
6468 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
6469 }
6470#endif /* CONFIG_P2P */
6471
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006472 if (os_strstr(param, "use_monitor=1")) {
6473 struct i802_bss *bss = priv;
6474 struct wpa_driver_nl80211_data *drv = bss->drv;
6475 drv->use_monitor = 1;
6476 }
6477
6478 if (os_strstr(param, "force_connect_cmd=1")) {
6479 struct i802_bss *bss = priv;
6480 struct wpa_driver_nl80211_data *drv = bss->drv;
6481 drv->capa.flags &= ~WPA_DRIVER_FLAGS_SME;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006482 drv->force_connect_cmd = 1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006483 }
6484
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08006485 if (os_strstr(param, "no_offchannel_tx=1")) {
6486 struct i802_bss *bss = priv;
6487 struct wpa_driver_nl80211_data *drv = bss->drv;
6488 drv->capa.flags &= ~WPA_DRIVER_FLAGS_OFFCHANNEL_TX;
6489 drv->test_use_roc_tx = 1;
6490 }
6491
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006492 return 0;
6493}
6494
6495
6496static void * nl80211_global_init(void)
6497{
6498 struct nl80211_global *global;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006499 struct netlink_config *cfg;
6500
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006501 global = os_zalloc(sizeof(*global));
6502 if (global == NULL)
6503 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006504 global->ioctl_sock = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006505 dl_list_init(&global->interfaces);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006506 global->if_add_ifindex = -1;
6507
6508 cfg = os_zalloc(sizeof(*cfg));
6509 if (cfg == NULL)
6510 goto err;
6511
6512 cfg->ctx = global;
6513 cfg->newlink_cb = wpa_driver_nl80211_event_rtm_newlink;
6514 cfg->dellink_cb = wpa_driver_nl80211_event_rtm_dellink;
6515 global->netlink = netlink_init(cfg);
6516 if (global->netlink == NULL) {
6517 os_free(cfg);
6518 goto err;
6519 }
6520
6521 if (wpa_driver_nl80211_init_nl_global(global) < 0)
6522 goto err;
6523
6524 global->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
6525 if (global->ioctl_sock < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006526 wpa_printf(MSG_ERROR, "nl80211: socket(PF_INET,SOCK_DGRAM) failed: %s",
6527 strerror(errno));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006528 goto err;
6529 }
6530
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006531 return global;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006532
6533err:
6534 nl80211_global_deinit(global);
6535 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006536}
6537
6538
6539static void nl80211_global_deinit(void *priv)
6540{
6541 struct nl80211_global *global = priv;
6542 if (global == NULL)
6543 return;
6544 if (!dl_list_empty(&global->interfaces)) {
6545 wpa_printf(MSG_ERROR, "nl80211: %u interface(s) remain at "
6546 "nl80211_global_deinit",
6547 dl_list_len(&global->interfaces));
6548 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006549
6550 if (global->netlink)
6551 netlink_deinit(global->netlink);
6552
6553 nl_destroy_handles(&global->nl);
6554
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006555 if (global->nl_event)
6556 nl80211_destroy_eloop_handle(&global->nl_event);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006557
6558 nl_cb_put(global->nl_cb);
6559
6560 if (global->ioctl_sock >= 0)
6561 close(global->ioctl_sock);
6562
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006563 os_free(global);
6564}
6565
6566
6567static const char * nl80211_get_radio_name(void *priv)
6568{
6569 struct i802_bss *bss = priv;
6570 struct wpa_driver_nl80211_data *drv = bss->drv;
6571 return drv->phyname;
6572}
6573
6574
Jouni Malinen75ecf522011-06-27 15:19:46 -07006575static int nl80211_pmkid(struct i802_bss *bss, int cmd, const u8 *bssid,
6576 const u8 *pmkid)
6577{
6578 struct nl_msg *msg;
6579
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006580 if (!(msg = nl80211_bss_msg(bss, 0, cmd)) ||
6581 (pmkid && nla_put(msg, NL80211_ATTR_PMKID, 16, pmkid)) ||
6582 (bssid && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))) {
6583 nlmsg_free(msg);
6584 return -ENOBUFS;
6585 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07006586
6587 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
Jouni Malinen75ecf522011-06-27 15:19:46 -07006588}
6589
6590
6591static int nl80211_add_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
6592{
6593 struct i802_bss *bss = priv;
6594 wpa_printf(MSG_DEBUG, "nl80211: Add PMKID for " MACSTR, MAC2STR(bssid));
6595 return nl80211_pmkid(bss, NL80211_CMD_SET_PMKSA, bssid, pmkid);
6596}
6597
6598
6599static int nl80211_remove_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
6600{
6601 struct i802_bss *bss = priv;
6602 wpa_printf(MSG_DEBUG, "nl80211: Delete PMKID for " MACSTR,
6603 MAC2STR(bssid));
6604 return nl80211_pmkid(bss, NL80211_CMD_DEL_PMKSA, bssid, pmkid);
6605}
6606
6607
6608static int nl80211_flush_pmkid(void *priv)
6609{
6610 struct i802_bss *bss = priv;
6611 wpa_printf(MSG_DEBUG, "nl80211: Flush PMKIDs");
6612 return nl80211_pmkid(bss, NL80211_CMD_FLUSH_PMKSA, NULL, NULL);
6613}
6614
6615
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006616static void clean_survey_results(struct survey_results *survey_results)
6617{
6618 struct freq_survey *survey, *tmp;
6619
6620 if (dl_list_empty(&survey_results->survey_list))
6621 return;
6622
6623 dl_list_for_each_safe(survey, tmp, &survey_results->survey_list,
6624 struct freq_survey, list) {
6625 dl_list_del(&survey->list);
6626 os_free(survey);
6627 }
6628}
6629
6630
6631static void add_survey(struct nlattr **sinfo, u32 ifidx,
6632 struct dl_list *survey_list)
6633{
6634 struct freq_survey *survey;
6635
6636 survey = os_zalloc(sizeof(struct freq_survey));
6637 if (!survey)
6638 return;
6639
6640 survey->ifidx = ifidx;
6641 survey->freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
6642 survey->filled = 0;
6643
6644 if (sinfo[NL80211_SURVEY_INFO_NOISE]) {
6645 survey->nf = (int8_t)
6646 nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
6647 survey->filled |= SURVEY_HAS_NF;
6648 }
6649
6650 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]) {
6651 survey->channel_time =
6652 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]);
6653 survey->filled |= SURVEY_HAS_CHAN_TIME;
6654 }
6655
6656 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]) {
6657 survey->channel_time_busy =
6658 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]);
6659 survey->filled |= SURVEY_HAS_CHAN_TIME_BUSY;
6660 }
6661
6662 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]) {
6663 survey->channel_time_rx =
6664 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]);
6665 survey->filled |= SURVEY_HAS_CHAN_TIME_RX;
6666 }
6667
6668 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]) {
6669 survey->channel_time_tx =
6670 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]);
6671 survey->filled |= SURVEY_HAS_CHAN_TIME_TX;
6672 }
6673
6674 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)",
6675 survey->freq,
6676 survey->nf,
6677 (unsigned long int) survey->channel_time,
6678 (unsigned long int) survey->channel_time_busy,
6679 (unsigned long int) survey->channel_time_tx,
6680 (unsigned long int) survey->channel_time_rx,
6681 survey->filled);
6682
6683 dl_list_add_tail(survey_list, &survey->list);
6684}
6685
6686
6687static int check_survey_ok(struct nlattr **sinfo, u32 surveyed_freq,
6688 unsigned int freq_filter)
6689{
6690 if (!freq_filter)
6691 return 1;
6692
6693 return freq_filter == surveyed_freq;
6694}
6695
6696
6697static int survey_handler(struct nl_msg *msg, void *arg)
6698{
6699 struct nlattr *tb[NL80211_ATTR_MAX + 1];
6700 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6701 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
6702 struct survey_results *survey_results;
6703 u32 surveyed_freq = 0;
6704 u32 ifidx;
6705
6706 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
6707 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
6708 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
6709 };
6710
6711 survey_results = (struct survey_results *) arg;
6712
6713 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6714 genlmsg_attrlen(gnlh, 0), NULL);
6715
Dmitry Shmidt97672262014-02-03 13:02:54 -08006716 if (!tb[NL80211_ATTR_IFINDEX])
6717 return NL_SKIP;
6718
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006719 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
6720
6721 if (!tb[NL80211_ATTR_SURVEY_INFO])
6722 return NL_SKIP;
6723
6724 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
6725 tb[NL80211_ATTR_SURVEY_INFO],
6726 survey_policy))
6727 return NL_SKIP;
6728
6729 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY]) {
6730 wpa_printf(MSG_ERROR, "nl80211: Invalid survey data");
6731 return NL_SKIP;
6732 }
6733
6734 surveyed_freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
6735
6736 if (!check_survey_ok(sinfo, surveyed_freq,
6737 survey_results->freq_filter))
6738 return NL_SKIP;
6739
6740 if (survey_results->freq_filter &&
6741 survey_results->freq_filter != surveyed_freq) {
6742 wpa_printf(MSG_EXCESSIVE, "nl80211: Ignoring survey data for freq %d MHz",
6743 surveyed_freq);
6744 return NL_SKIP;
6745 }
6746
6747 add_survey(sinfo, ifidx, &survey_results->survey_list);
6748
6749 return NL_SKIP;
6750}
6751
6752
6753static int wpa_driver_nl80211_get_survey(void *priv, unsigned int freq)
6754{
6755 struct i802_bss *bss = priv;
6756 struct wpa_driver_nl80211_data *drv = bss->drv;
6757 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006758 int err;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006759 union wpa_event_data data;
6760 struct survey_results *survey_results;
6761
6762 os_memset(&data, 0, sizeof(data));
6763 survey_results = &data.survey_results;
6764
6765 dl_list_init(&survey_results->survey_list);
6766
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006767 msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006768 if (!msg)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006769 return -ENOBUFS;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006770
6771 if (freq)
6772 data.survey_results.freq_filter = freq;
6773
6774 do {
6775 wpa_printf(MSG_DEBUG, "nl80211: Fetch survey data");
6776 err = send_and_recv_msgs(drv, msg, survey_handler,
6777 survey_results);
6778 } while (err > 0);
6779
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006780 if (err)
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006781 wpa_printf(MSG_ERROR, "nl80211: Failed to process survey data");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006782 else
6783 wpa_supplicant_event(drv->ctx, EVENT_SURVEY, &data);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006784
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006785 clean_survey_results(survey_results);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006786 return err;
6787}
6788
6789
Dmitry Shmidt807291d2015-01-27 13:40:23 -08006790static void nl80211_set_rekey_info(void *priv, const u8 *kek, size_t kek_len,
6791 const u8 *kck, size_t kck_len,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006792 const u8 *replay_ctr)
6793{
6794 struct i802_bss *bss = priv;
6795 struct wpa_driver_nl80211_data *drv = bss->drv;
6796 struct nlattr *replay_nested;
6797 struct nl_msg *msg;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08006798 int ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006799
Dmitry Shmidtff787d52015-01-12 13:01:47 -08006800 if (!drv->set_rekey_offload)
6801 return;
6802
6803 wpa_printf(MSG_DEBUG, "nl80211: Set rekey offload");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006804 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_REKEY_OFFLOAD)) ||
6805 !(replay_nested = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA)) ||
Dmitry Shmidt807291d2015-01-27 13:40:23 -08006806 nla_put(msg, NL80211_REKEY_DATA_KEK, kek_len, kek) ||
6807 nla_put(msg, NL80211_REKEY_DATA_KCK, kck_len, kck) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006808 nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR, NL80211_REPLAY_CTR_LEN,
6809 replay_ctr)) {
6810 nl80211_nlmsg_clear(msg);
6811 nlmsg_free(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006812 return;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006813 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006814
6815 nla_nest_end(msg, replay_nested);
6816
Dmitry Shmidtff787d52015-01-12 13:01:47 -08006817 ret = send_and_recv_msgs(drv, msg, NULL, (void *) -1);
6818 if (ret == -EOPNOTSUPP) {
6819 wpa_printf(MSG_DEBUG,
6820 "nl80211: Driver does not support rekey offload");
6821 drv->set_rekey_offload = 0;
6822 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006823}
6824
6825
6826static void nl80211_send_null_frame(struct i802_bss *bss, const u8 *own_addr,
6827 const u8 *addr, int qos)
6828{
6829 /* send data frame to poll STA and check whether
6830 * this frame is ACKed */
6831 struct {
6832 struct ieee80211_hdr hdr;
6833 u16 qos_ctl;
6834 } STRUCT_PACKED nulldata;
6835 size_t size;
6836
6837 /* Send data frame to poll STA and check whether this frame is ACKed */
6838
6839 os_memset(&nulldata, 0, sizeof(nulldata));
6840
6841 if (qos) {
6842 nulldata.hdr.frame_control =
6843 IEEE80211_FC(WLAN_FC_TYPE_DATA,
6844 WLAN_FC_STYPE_QOS_NULL);
6845 size = sizeof(nulldata);
6846 } else {
6847 nulldata.hdr.frame_control =
6848 IEEE80211_FC(WLAN_FC_TYPE_DATA,
6849 WLAN_FC_STYPE_NULLFUNC);
6850 size = sizeof(struct ieee80211_hdr);
6851 }
6852
6853 nulldata.hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS);
6854 os_memcpy(nulldata.hdr.IEEE80211_DA_FROMDS, addr, ETH_ALEN);
6855 os_memcpy(nulldata.hdr.IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
6856 os_memcpy(nulldata.hdr.IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
6857
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006858 if (wpa_driver_nl80211_send_mlme(bss, (u8 *) &nulldata, size, 0, 0, 0,
6859 0, 0) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006860 wpa_printf(MSG_DEBUG, "nl80211_send_null_frame: Failed to "
6861 "send poll frame");
6862}
6863
6864static void nl80211_poll_client(void *priv, const u8 *own_addr, const u8 *addr,
6865 int qos)
6866{
6867 struct i802_bss *bss = priv;
6868 struct wpa_driver_nl80211_data *drv = bss->drv;
6869 struct nl_msg *msg;
6870
6871 if (!drv->poll_command_supported) {
6872 nl80211_send_null_frame(bss, own_addr, addr, qos);
6873 return;
6874 }
6875
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006876 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_PROBE_CLIENT)) ||
6877 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
6878 nlmsg_free(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006879 return;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006880 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006881
6882 send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006883}
6884
6885
6886static int nl80211_set_power_save(struct i802_bss *bss, int enabled)
6887{
6888 struct nl_msg *msg;
6889
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006890 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_POWER_SAVE)) ||
6891 nla_put_u32(msg, NL80211_ATTR_PS_STATE,
6892 enabled ? NL80211_PS_ENABLED : NL80211_PS_DISABLED)) {
6893 nlmsg_free(msg);
6894 return -ENOBUFS;
6895 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006896 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006897}
6898
6899
6900static int nl80211_set_p2p_powersave(void *priv, int legacy_ps, int opp_ps,
6901 int ctwindow)
6902{
6903 struct i802_bss *bss = priv;
6904
6905 wpa_printf(MSG_DEBUG, "nl80211: set_p2p_powersave (legacy_ps=%d "
6906 "opp_ps=%d ctwindow=%d)", legacy_ps, opp_ps, ctwindow);
6907
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08006908 if (opp_ps != -1 || ctwindow != -1) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006909#ifdef ANDROID_P2P
6910 wpa_driver_set_p2p_ps(priv, legacy_ps, opp_ps, ctwindow);
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08006911#else /* ANDROID_P2P */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006912 return -1; /* Not yet supported */
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08006913#endif /* ANDROID_P2P */
6914 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006915
6916 if (legacy_ps == -1)
6917 return 0;
6918 if (legacy_ps != 0 && legacy_ps != 1)
6919 return -1; /* Not yet supported */
6920
6921 return nl80211_set_power_save(bss, legacy_ps);
6922}
6923
6924
Dmitry Shmidt051af732013-10-22 13:52:46 -07006925static int nl80211_start_radar_detection(void *priv,
6926 struct hostapd_freq_params *freq)
Dmitry Shmidtea69e842013-05-13 14:52:28 -07006927{
6928 struct i802_bss *bss = priv;
6929 struct wpa_driver_nl80211_data *drv = bss->drv;
6930 struct nl_msg *msg;
6931 int ret;
6932
Dmitry Shmidt051af732013-10-22 13:52:46 -07006933 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)",
6934 freq->freq, freq->ht_enabled, freq->vht_enabled,
6935 freq->bandwidth, freq->center_freq1, freq->center_freq2);
6936
Dmitry Shmidtea69e842013-05-13 14:52:28 -07006937 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_RADAR)) {
6938 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support radar "
6939 "detection");
6940 return -1;
6941 }
6942
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006943 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_RADAR_DETECT)) ||
6944 nl80211_put_freq_params(msg, freq) < 0) {
6945 nlmsg_free(msg);
Dmitry Shmidtea69e842013-05-13 14:52:28 -07006946 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006947 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -07006948
6949 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6950 if (ret == 0)
6951 return 0;
6952 wpa_printf(MSG_DEBUG, "nl80211: Failed to start radar detection: "
6953 "%d (%s)", ret, strerror(-ret));
Dmitry Shmidtea69e842013-05-13 14:52:28 -07006954 return -1;
6955}
6956
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006957#ifdef CONFIG_TDLS
6958
6959static int nl80211_send_tdls_mgmt(void *priv, const u8 *dst, u8 action_code,
6960 u8 dialog_token, u16 status_code,
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07006961 u32 peer_capab, int initiator, const u8 *buf,
6962 size_t len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006963{
6964 struct i802_bss *bss = priv;
6965 struct wpa_driver_nl80211_data *drv = bss->drv;
6966 struct nl_msg *msg;
6967
6968 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
6969 return -EOPNOTSUPP;
6970
6971 if (!dst)
6972 return -EINVAL;
6973
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006974 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_TDLS_MGMT)) ||
6975 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
6976 nla_put_u8(msg, NL80211_ATTR_TDLS_ACTION, action_code) ||
6977 nla_put_u8(msg, NL80211_ATTR_TDLS_DIALOG_TOKEN, dialog_token) ||
6978 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status_code))
6979 goto fail;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006980 if (peer_capab) {
6981 /*
6982 * The internal enum tdls_peer_capability definition is
6983 * currently identical with the nl80211 enum
6984 * nl80211_tdls_peer_capability, so no conversion is needed
6985 * here.
6986 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006987 if (nla_put_u32(msg, NL80211_ATTR_TDLS_PEER_CAPABILITY,
6988 peer_capab))
6989 goto fail;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006990 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006991 if ((initiator &&
6992 nla_put_flag(msg, NL80211_ATTR_TDLS_INITIATOR)) ||
6993 nla_put(msg, NL80211_ATTR_IE, len, buf))
6994 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006995
6996 return send_and_recv_msgs(drv, msg, NULL, NULL);
6997
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006998fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006999 nlmsg_free(msg);
7000 return -ENOBUFS;
7001}
7002
7003
7004static int nl80211_tdls_oper(void *priv, enum tdls_oper oper, const u8 *peer)
7005{
7006 struct i802_bss *bss = priv;
7007 struct wpa_driver_nl80211_data *drv = bss->drv;
7008 struct nl_msg *msg;
7009 enum nl80211_tdls_operation nl80211_oper;
7010
7011 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
7012 return -EOPNOTSUPP;
7013
7014 switch (oper) {
7015 case TDLS_DISCOVERY_REQ:
7016 nl80211_oper = NL80211_TDLS_DISCOVERY_REQ;
7017 break;
7018 case TDLS_SETUP:
7019 nl80211_oper = NL80211_TDLS_SETUP;
7020 break;
7021 case TDLS_TEARDOWN:
7022 nl80211_oper = NL80211_TDLS_TEARDOWN;
7023 break;
7024 case TDLS_ENABLE_LINK:
7025 nl80211_oper = NL80211_TDLS_ENABLE_LINK;
7026 break;
7027 case TDLS_DISABLE_LINK:
7028 nl80211_oper = NL80211_TDLS_DISABLE_LINK;
7029 break;
7030 case TDLS_ENABLE:
7031 return 0;
7032 case TDLS_DISABLE:
7033 return 0;
7034 default:
7035 return -EINVAL;
7036 }
7037
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007038 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_TDLS_OPER)) ||
7039 nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, nl80211_oper) ||
7040 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer)) {
7041 nlmsg_free(msg);
7042 return -ENOBUFS;
7043 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007044
7045 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007046}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007047
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007048
7049static int
7050nl80211_tdls_enable_channel_switch(void *priv, const u8 *addr, u8 oper_class,
7051 const struct hostapd_freq_params *params)
7052{
7053 struct i802_bss *bss = priv;
7054 struct wpa_driver_nl80211_data *drv = bss->drv;
7055 struct nl_msg *msg;
7056 int ret = -ENOBUFS;
7057
7058 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT) ||
7059 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_CHANNEL_SWITCH))
7060 return -EOPNOTSUPP;
7061
7062 wpa_printf(MSG_DEBUG, "nl80211: Enable TDLS channel switch " MACSTR
7063 " oper_class=%u freq=%u",
7064 MAC2STR(addr), oper_class, params->freq);
7065 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_TDLS_CHANNEL_SWITCH);
7066 if (!msg ||
7067 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
7068 nla_put_u8(msg, NL80211_ATTR_OPER_CLASS, oper_class) ||
7069 (ret = nl80211_put_freq_params(msg, params))) {
7070 nlmsg_free(msg);
7071 wpa_printf(MSG_DEBUG, "nl80211: Could not build TDLS chan switch");
7072 return ret;
7073 }
7074
7075 return send_and_recv_msgs(drv, msg, NULL, NULL);
7076}
7077
7078
7079static int
7080nl80211_tdls_disable_channel_switch(void *priv, const u8 *addr)
7081{
7082 struct i802_bss *bss = priv;
7083 struct wpa_driver_nl80211_data *drv = bss->drv;
7084 struct nl_msg *msg;
7085
7086 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT) ||
7087 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_CHANNEL_SWITCH))
7088 return -EOPNOTSUPP;
7089
7090 wpa_printf(MSG_DEBUG, "nl80211: Disable TDLS channel switch " MACSTR,
7091 MAC2STR(addr));
7092 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_TDLS_CANCEL_CHANNEL_SWITCH);
7093 if (!msg ||
7094 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
7095 nlmsg_free(msg);
7096 wpa_printf(MSG_DEBUG,
7097 "nl80211: Could not build TDLS cancel chan switch");
7098 return -ENOBUFS;
7099 }
7100
7101 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007102}
7103
7104#endif /* CONFIG TDLS */
7105
7106
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007107static int driver_nl80211_set_key(const char *ifname, void *priv,
7108 enum wpa_alg alg, const u8 *addr,
7109 int key_idx, int set_tx,
7110 const u8 *seq, size_t seq_len,
7111 const u8 *key, size_t key_len)
7112{
7113 struct i802_bss *bss = priv;
7114 return wpa_driver_nl80211_set_key(ifname, bss, alg, addr, key_idx,
7115 set_tx, seq, seq_len, key, key_len);
7116}
7117
7118
7119static int driver_nl80211_scan2(void *priv,
7120 struct wpa_driver_scan_params *params)
7121{
7122 struct i802_bss *bss = priv;
7123 return wpa_driver_nl80211_scan(bss, params);
7124}
7125
7126
7127static int driver_nl80211_deauthenticate(void *priv, const u8 *addr,
7128 int reason_code)
7129{
7130 struct i802_bss *bss = priv;
7131 return wpa_driver_nl80211_deauthenticate(bss, addr, reason_code);
7132}
7133
7134
7135static int driver_nl80211_authenticate(void *priv,
7136 struct wpa_driver_auth_params *params)
7137{
7138 struct i802_bss *bss = priv;
7139 return wpa_driver_nl80211_authenticate(bss, params);
7140}
7141
7142
7143static void driver_nl80211_deinit(void *priv)
7144{
7145 struct i802_bss *bss = priv;
7146 wpa_driver_nl80211_deinit(bss);
7147}
7148
7149
7150static int driver_nl80211_if_remove(void *priv, enum wpa_driver_if_type type,
7151 const char *ifname)
7152{
7153 struct i802_bss *bss = priv;
7154 return wpa_driver_nl80211_if_remove(bss, type, ifname);
7155}
7156
7157
7158static int driver_nl80211_send_mlme(void *priv, const u8 *data,
7159 size_t data_len, int noack)
7160{
7161 struct i802_bss *bss = priv;
7162 return wpa_driver_nl80211_send_mlme(bss, data, data_len, noack,
7163 0, 0, 0, 0);
7164}
7165
7166
7167static int driver_nl80211_sta_remove(void *priv, const u8 *addr)
7168{
7169 struct i802_bss *bss = priv;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007170 return wpa_driver_nl80211_sta_remove(bss, addr, -1, 0);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007171}
7172
7173
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007174static int driver_nl80211_set_sta_vlan(void *priv, const u8 *addr,
7175 const char *ifname, int vlan_id)
7176{
7177 struct i802_bss *bss = priv;
7178 return i802_set_sta_vlan(bss, addr, ifname, vlan_id);
7179}
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007180
7181
7182static int driver_nl80211_read_sta_data(void *priv,
7183 struct hostap_sta_driver_data *data,
7184 const u8 *addr)
7185{
7186 struct i802_bss *bss = priv;
7187 return i802_read_sta_data(bss, data, addr);
7188}
7189
7190
7191static int driver_nl80211_send_action(void *priv, unsigned int freq,
7192 unsigned int wait_time,
7193 const u8 *dst, const u8 *src,
7194 const u8 *bssid,
7195 const u8 *data, size_t data_len,
7196 int no_cck)
7197{
7198 struct i802_bss *bss = priv;
7199 return wpa_driver_nl80211_send_action(bss, freq, wait_time, dst, src,
7200 bssid, data, data_len, no_cck);
7201}
7202
7203
7204static int driver_nl80211_probe_req_report(void *priv, int report)
7205{
7206 struct i802_bss *bss = priv;
7207 return wpa_driver_nl80211_probe_req_report(bss, report);
7208}
7209
7210
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007211static int wpa_driver_nl80211_update_ft_ies(void *priv, const u8 *md,
7212 const u8 *ies, size_t ies_len)
7213{
7214 int ret;
7215 struct nl_msg *msg;
7216 struct i802_bss *bss = priv;
7217 struct wpa_driver_nl80211_data *drv = bss->drv;
7218 u16 mdid = WPA_GET_LE16(md);
7219
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007220 wpa_printf(MSG_DEBUG, "nl80211: Updating FT IEs");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007221 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_UPDATE_FT_IES)) ||
7222 nla_put(msg, NL80211_ATTR_IE, ies_len, ies) ||
7223 nla_put_u16(msg, NL80211_ATTR_MDID, mdid)) {
7224 nlmsg_free(msg);
7225 return -ENOBUFS;
7226 }
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007227
7228 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7229 if (ret) {
7230 wpa_printf(MSG_DEBUG, "nl80211: update_ft_ies failed "
7231 "err=%d (%s)", ret, strerror(-ret));
7232 }
7233
7234 return ret;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007235}
7236
7237
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007238const u8 * wpa_driver_nl80211_get_macaddr(void *priv)
7239{
7240 struct i802_bss *bss = priv;
7241 struct wpa_driver_nl80211_data *drv = bss->drv;
7242
7243 if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE)
7244 return NULL;
7245
7246 return bss->addr;
7247}
7248
7249
Dmitry Shmidt56052862013-10-04 10:23:25 -07007250static const char * scan_state_str(enum scan_states scan_state)
7251{
7252 switch (scan_state) {
7253 case NO_SCAN:
7254 return "NO_SCAN";
7255 case SCAN_REQUESTED:
7256 return "SCAN_REQUESTED";
7257 case SCAN_STARTED:
7258 return "SCAN_STARTED";
7259 case SCAN_COMPLETED:
7260 return "SCAN_COMPLETED";
7261 case SCAN_ABORTED:
7262 return "SCAN_ABORTED";
7263 case SCHED_SCAN_STARTED:
7264 return "SCHED_SCAN_STARTED";
7265 case SCHED_SCAN_STOPPED:
7266 return "SCHED_SCAN_STOPPED";
7267 case SCHED_SCAN_RESULTS:
7268 return "SCHED_SCAN_RESULTS";
7269 }
7270
7271 return "??";
7272}
7273
7274
7275static int wpa_driver_nl80211_status(void *priv, char *buf, size_t buflen)
7276{
7277 struct i802_bss *bss = priv;
7278 struct wpa_driver_nl80211_data *drv = bss->drv;
7279 int res;
7280 char *pos, *end;
7281
7282 pos = buf;
7283 end = buf + buflen;
7284
7285 res = os_snprintf(pos, end - pos,
7286 "ifindex=%d\n"
7287 "ifname=%s\n"
7288 "brname=%s\n"
7289 "addr=" MACSTR "\n"
7290 "freq=%d\n"
7291 "%s%s%s%s%s",
7292 bss->ifindex,
7293 bss->ifname,
7294 bss->brname,
7295 MAC2STR(bss->addr),
7296 bss->freq,
7297 bss->beacon_set ? "beacon_set=1\n" : "",
7298 bss->added_if_into_bridge ?
7299 "added_if_into_bridge=1\n" : "",
7300 bss->added_bridge ? "added_bridge=1\n" : "",
7301 bss->in_deinit ? "in_deinit=1\n" : "",
7302 bss->if_dynamic ? "if_dynamic=1\n" : "");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007303 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt56052862013-10-04 10:23:25 -07007304 return pos - buf;
7305 pos += res;
7306
7307 if (bss->wdev_id_set) {
7308 res = os_snprintf(pos, end - pos, "wdev_id=%llu\n",
7309 (unsigned long long) bss->wdev_id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007310 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt56052862013-10-04 10:23:25 -07007311 return pos - buf;
7312 pos += res;
7313 }
7314
7315 res = os_snprintf(pos, end - pos,
7316 "phyname=%s\n"
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007317 "perm_addr=" MACSTR "\n"
Dmitry Shmidt56052862013-10-04 10:23:25 -07007318 "drv_ifindex=%d\n"
7319 "operstate=%d\n"
7320 "scan_state=%s\n"
7321 "auth_bssid=" MACSTR "\n"
7322 "auth_attempt_bssid=" MACSTR "\n"
7323 "bssid=" MACSTR "\n"
7324 "prev_bssid=" MACSTR "\n"
7325 "associated=%d\n"
7326 "assoc_freq=%u\n"
7327 "monitor_sock=%d\n"
7328 "monitor_ifidx=%d\n"
7329 "monitor_refcount=%d\n"
7330 "last_mgmt_freq=%u\n"
7331 "eapol_tx_sock=%d\n"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007332 "%s%s%s%s%s%s%s%s%s%s%s%s%s",
Dmitry Shmidt56052862013-10-04 10:23:25 -07007333 drv->phyname,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007334 MAC2STR(drv->perm_addr),
Dmitry Shmidt56052862013-10-04 10:23:25 -07007335 drv->ifindex,
7336 drv->operstate,
7337 scan_state_str(drv->scan_state),
7338 MAC2STR(drv->auth_bssid),
7339 MAC2STR(drv->auth_attempt_bssid),
7340 MAC2STR(drv->bssid),
7341 MAC2STR(drv->prev_bssid),
7342 drv->associated,
7343 drv->assoc_freq,
7344 drv->monitor_sock,
7345 drv->monitor_ifidx,
7346 drv->monitor_refcount,
7347 drv->last_mgmt_freq,
7348 drv->eapol_tx_sock,
7349 drv->ignore_if_down_event ?
7350 "ignore_if_down_event=1\n" : "",
7351 drv->scan_complete_events ?
7352 "scan_complete_events=1\n" : "",
7353 drv->disabled_11b_rates ?
7354 "disabled_11b_rates=1\n" : "",
7355 drv->pending_remain_on_chan ?
7356 "pending_remain_on_chan=1\n" : "",
7357 drv->in_interface_list ? "in_interface_list=1\n" : "",
7358 drv->device_ap_sme ? "device_ap_sme=1\n" : "",
7359 drv->poll_command_supported ?
7360 "poll_command_supported=1\n" : "",
7361 drv->data_tx_status ? "data_tx_status=1\n" : "",
7362 drv->scan_for_auth ? "scan_for_auth=1\n" : "",
7363 drv->retry_auth ? "retry_auth=1\n" : "",
7364 drv->use_monitor ? "use_monitor=1\n" : "",
7365 drv->ignore_next_local_disconnect ?
7366 "ignore_next_local_disconnect=1\n" : "",
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07007367 drv->ignore_next_local_deauth ?
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007368 "ignore_next_local_deauth=1\n" : "");
7369 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt56052862013-10-04 10:23:25 -07007370 return pos - buf;
7371 pos += res;
7372
7373 if (drv->has_capability) {
7374 res = os_snprintf(pos, end - pos,
7375 "capa.key_mgmt=0x%x\n"
7376 "capa.enc=0x%x\n"
7377 "capa.auth=0x%x\n"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007378 "capa.flags=0x%llx\n"
7379 "capa.rrm_flags=0x%x\n"
Dmitry Shmidt56052862013-10-04 10:23:25 -07007380 "capa.max_scan_ssids=%d\n"
7381 "capa.max_sched_scan_ssids=%d\n"
7382 "capa.sched_scan_supported=%d\n"
7383 "capa.max_match_sets=%d\n"
7384 "capa.max_remain_on_chan=%u\n"
7385 "capa.max_stations=%u\n"
7386 "capa.probe_resp_offloads=0x%x\n"
7387 "capa.max_acl_mac_addrs=%u\n"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007388 "capa.num_multichan_concurrent=%u\n"
7389 "capa.mac_addr_rand_sched_scan_supported=%d\n"
7390 "capa.mac_addr_rand_scan_supported=%d\n",
Dmitry Shmidt56052862013-10-04 10:23:25 -07007391 drv->capa.key_mgmt,
7392 drv->capa.enc,
7393 drv->capa.auth,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007394 (unsigned long long) drv->capa.flags,
7395 drv->capa.rrm_flags,
Dmitry Shmidt56052862013-10-04 10:23:25 -07007396 drv->capa.max_scan_ssids,
7397 drv->capa.max_sched_scan_ssids,
7398 drv->capa.sched_scan_supported,
7399 drv->capa.max_match_sets,
7400 drv->capa.max_remain_on_chan,
7401 drv->capa.max_stations,
7402 drv->capa.probe_resp_offloads,
7403 drv->capa.max_acl_mac_addrs,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007404 drv->capa.num_multichan_concurrent,
7405 drv->capa.mac_addr_rand_sched_scan_supported,
7406 drv->capa.mac_addr_rand_scan_supported);
7407 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt56052862013-10-04 10:23:25 -07007408 return pos - buf;
7409 pos += res;
7410 }
7411
7412 return pos - buf;
7413}
7414
7415
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007416static int set_beacon_data(struct nl_msg *msg, struct beacon_data *settings)
7417{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007418 if ((settings->head &&
7419 nla_put(msg, NL80211_ATTR_BEACON_HEAD,
7420 settings->head_len, settings->head)) ||
7421 (settings->tail &&
7422 nla_put(msg, NL80211_ATTR_BEACON_TAIL,
7423 settings->tail_len, settings->tail)) ||
7424 (settings->beacon_ies &&
7425 nla_put(msg, NL80211_ATTR_IE,
7426 settings->beacon_ies_len, settings->beacon_ies)) ||
7427 (settings->proberesp_ies &&
7428 nla_put(msg, NL80211_ATTR_IE_PROBE_RESP,
7429 settings->proberesp_ies_len, settings->proberesp_ies)) ||
7430 (settings->assocresp_ies &&
7431 nla_put(msg, NL80211_ATTR_IE_ASSOC_RESP,
7432 settings->assocresp_ies_len, settings->assocresp_ies)) ||
7433 (settings->probe_resp &&
7434 nla_put(msg, NL80211_ATTR_PROBE_RESP,
7435 settings->probe_resp_len, settings->probe_resp)))
7436 return -ENOBUFS;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007437
7438 return 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007439}
7440
7441
7442static int nl80211_switch_channel(void *priv, struct csa_settings *settings)
7443{
7444 struct nl_msg *msg;
7445 struct i802_bss *bss = priv;
7446 struct wpa_driver_nl80211_data *drv = bss->drv;
7447 struct nlattr *beacon_csa;
7448 int ret = -ENOBUFS;
7449
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08007450 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 -08007451 settings->cs_count, settings->block_tx,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08007452 settings->freq_params.freq, settings->freq_params.bandwidth,
7453 settings->freq_params.center_freq1,
7454 settings->freq_params.center_freq2);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007455
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007456 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_AP_CSA)) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007457 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support channel switch command");
7458 return -EOPNOTSUPP;
7459 }
7460
7461 if ((drv->nlmode != NL80211_IFTYPE_AP) &&
7462 (drv->nlmode != NL80211_IFTYPE_P2P_GO))
7463 return -EOPNOTSUPP;
7464
7465 /* check settings validity */
7466 if (!settings->beacon_csa.tail ||
7467 ((settings->beacon_csa.tail_len <=
7468 settings->counter_offset_beacon) ||
7469 (settings->beacon_csa.tail[settings->counter_offset_beacon] !=
7470 settings->cs_count)))
7471 return -EINVAL;
7472
7473 if (settings->beacon_csa.probe_resp &&
7474 ((settings->beacon_csa.probe_resp_len <=
7475 settings->counter_offset_presp) ||
7476 (settings->beacon_csa.probe_resp[settings->counter_offset_presp] !=
7477 settings->cs_count)))
7478 return -EINVAL;
7479
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007480 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_CHANNEL_SWITCH)) ||
7481 nla_put_u32(msg, NL80211_ATTR_CH_SWITCH_COUNT,
7482 settings->cs_count) ||
7483 (ret = nl80211_put_freq_params(msg, &settings->freq_params)) ||
7484 (settings->block_tx &&
7485 nla_put_flag(msg, NL80211_ATTR_CH_SWITCH_BLOCK_TX)))
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007486 goto error;
7487
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007488 /* beacon_after params */
7489 ret = set_beacon_data(msg, &settings->beacon_after);
7490 if (ret)
7491 goto error;
7492
7493 /* beacon_csa params */
7494 beacon_csa = nla_nest_start(msg, NL80211_ATTR_CSA_IES);
7495 if (!beacon_csa)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007496 goto fail;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007497
7498 ret = set_beacon_data(msg, &settings->beacon_csa);
7499 if (ret)
7500 goto error;
7501
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007502 if (nla_put_u16(msg, NL80211_ATTR_CSA_C_OFF_BEACON,
7503 settings->counter_offset_beacon) ||
7504 (settings->beacon_csa.probe_resp &&
7505 nla_put_u16(msg, NL80211_ATTR_CSA_C_OFF_PRESP,
7506 settings->counter_offset_presp)))
7507 goto fail;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007508
7509 nla_nest_end(msg, beacon_csa);
7510 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7511 if (ret) {
7512 wpa_printf(MSG_DEBUG, "nl80211: switch_channel failed err=%d (%s)",
7513 ret, strerror(-ret));
7514 }
7515 return ret;
7516
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007517fail:
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007518 ret = -ENOBUFS;
7519error:
7520 nlmsg_free(msg);
7521 wpa_printf(MSG_DEBUG, "nl80211: Could not build channel switch request");
7522 return ret;
7523}
7524
7525
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007526static int nl80211_add_ts(void *priv, u8 tsid, const u8 *addr,
7527 u8 user_priority, u16 admitted_time)
7528{
7529 struct i802_bss *bss = priv;
7530 struct wpa_driver_nl80211_data *drv = bss->drv;
7531 struct nl_msg *msg;
7532 int ret;
7533
7534 wpa_printf(MSG_DEBUG,
7535 "nl80211: add_ts request: tsid=%u admitted_time=%u up=%d",
7536 tsid, admitted_time, user_priority);
7537
7538 if (!is_sta_interface(drv->nlmode))
7539 return -ENOTSUP;
7540
7541 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_ADD_TX_TS);
7542 if (!msg ||
7543 nla_put_u8(msg, NL80211_ATTR_TSID, tsid) ||
7544 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
7545 nla_put_u8(msg, NL80211_ATTR_USER_PRIO, user_priority) ||
7546 nla_put_u16(msg, NL80211_ATTR_ADMITTED_TIME, admitted_time)) {
7547 nlmsg_free(msg);
7548 return -ENOBUFS;
7549 }
7550
7551 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7552 if (ret)
7553 wpa_printf(MSG_DEBUG, "nl80211: add_ts failed err=%d (%s)",
7554 ret, strerror(-ret));
7555 return ret;
7556}
7557
7558
7559static int nl80211_del_ts(void *priv, u8 tsid, const u8 *addr)
7560{
7561 struct i802_bss *bss = priv;
7562 struct wpa_driver_nl80211_data *drv = bss->drv;
7563 struct nl_msg *msg;
7564 int ret;
7565
7566 wpa_printf(MSG_DEBUG, "nl80211: del_ts request: tsid=%u", tsid);
7567
7568 if (!is_sta_interface(drv->nlmode))
7569 return -ENOTSUP;
7570
7571 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_DEL_TX_TS)) ||
7572 nla_put_u8(msg, NL80211_ATTR_TSID, tsid) ||
7573 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
7574 nlmsg_free(msg);
7575 return -ENOBUFS;
7576 }
7577
7578 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7579 if (ret)
7580 wpa_printf(MSG_DEBUG, "nl80211: del_ts failed err=%d (%s)",
7581 ret, strerror(-ret));
7582 return ret;
7583}
7584
7585
Dmitry Shmidta38abf92014-03-06 13:38:44 -08007586#ifdef CONFIG_TESTING_OPTIONS
7587static int cmd_reply_handler(struct nl_msg *msg, void *arg)
7588{
7589 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
7590 struct wpabuf *buf = arg;
7591
7592 if (!buf)
7593 return NL_SKIP;
7594
7595 if ((size_t) genlmsg_attrlen(gnlh, 0) > wpabuf_tailroom(buf)) {
7596 wpa_printf(MSG_INFO, "nl80211: insufficient buffer space for reply");
7597 return NL_SKIP;
7598 }
7599
7600 wpabuf_put_data(buf, genlmsg_attrdata(gnlh, 0),
7601 genlmsg_attrlen(gnlh, 0));
7602
7603 return NL_SKIP;
7604}
7605#endif /* CONFIG_TESTING_OPTIONS */
7606
7607
7608static int vendor_reply_handler(struct nl_msg *msg, void *arg)
7609{
7610 struct nlattr *tb[NL80211_ATTR_MAX + 1];
7611 struct nlattr *nl_vendor_reply, *nl;
7612 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
7613 struct wpabuf *buf = arg;
7614 int rem;
7615
7616 if (!buf)
7617 return NL_SKIP;
7618
7619 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
7620 genlmsg_attrlen(gnlh, 0), NULL);
7621 nl_vendor_reply = tb[NL80211_ATTR_VENDOR_DATA];
7622
7623 if (!nl_vendor_reply)
7624 return NL_SKIP;
7625
7626 if ((size_t) nla_len(nl_vendor_reply) > wpabuf_tailroom(buf)) {
7627 wpa_printf(MSG_INFO, "nl80211: Vendor command: insufficient buffer space for reply");
7628 return NL_SKIP;
7629 }
7630
7631 nla_for_each_nested(nl, nl_vendor_reply, rem) {
7632 wpabuf_put_data(buf, nla_data(nl), nla_len(nl));
7633 }
7634
7635 return NL_SKIP;
7636}
7637
7638
7639static int nl80211_vendor_cmd(void *priv, unsigned int vendor_id,
7640 unsigned int subcmd, const u8 *data,
7641 size_t data_len, struct wpabuf *buf)
7642{
7643 struct i802_bss *bss = priv;
7644 struct wpa_driver_nl80211_data *drv = bss->drv;
7645 struct nl_msg *msg;
7646 int ret;
7647
Dmitry Shmidta38abf92014-03-06 13:38:44 -08007648#ifdef CONFIG_TESTING_OPTIONS
7649 if (vendor_id == 0xffffffff) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007650 msg = nlmsg_alloc();
7651 if (!msg)
7652 return -ENOMEM;
7653
Dmitry Shmidta38abf92014-03-06 13:38:44 -08007654 nl80211_cmd(drv, msg, 0, subcmd);
7655 if (nlmsg_append(msg, (void *) data, data_len, NLMSG_ALIGNTO) <
7656 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007657 goto fail;
Dmitry Shmidta38abf92014-03-06 13:38:44 -08007658 ret = send_and_recv_msgs(drv, msg, cmd_reply_handler, buf);
7659 if (ret)
7660 wpa_printf(MSG_DEBUG, "nl80211: command failed err=%d",
7661 ret);
7662 return ret;
7663 }
7664#endif /* CONFIG_TESTING_OPTIONS */
7665
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007666 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_VENDOR)) ||
7667 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, vendor_id) ||
7668 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD, subcmd) ||
7669 (data &&
7670 nla_put(msg, NL80211_ATTR_VENDOR_DATA, data_len, data)))
7671 goto fail;
Dmitry Shmidta38abf92014-03-06 13:38:44 -08007672
7673 ret = send_and_recv_msgs(drv, msg, vendor_reply_handler, buf);
7674 if (ret)
7675 wpa_printf(MSG_DEBUG, "nl80211: vendor command failed err=%d",
7676 ret);
7677 return ret;
7678
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007679fail:
Dmitry Shmidta38abf92014-03-06 13:38:44 -08007680 nlmsg_free(msg);
7681 return -ENOBUFS;
7682}
7683
7684
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007685static int nl80211_set_qos_map(void *priv, const u8 *qos_map_set,
7686 u8 qos_map_set_len)
7687{
7688 struct i802_bss *bss = priv;
7689 struct wpa_driver_nl80211_data *drv = bss->drv;
7690 struct nl_msg *msg;
7691 int ret;
7692
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007693 wpa_hexdump(MSG_DEBUG, "nl80211: Setting QoS Map",
7694 qos_map_set, qos_map_set_len);
7695
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007696 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_QOS_MAP)) ||
7697 nla_put(msg, NL80211_ATTR_QOS_MAP, qos_map_set_len, qos_map_set)) {
7698 nlmsg_free(msg);
7699 return -ENOBUFS;
7700 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007701
7702 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7703 if (ret)
7704 wpa_printf(MSG_DEBUG, "nl80211: Setting QoS Map failed");
7705
7706 return ret;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007707}
7708
7709
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07007710static int nl80211_set_wowlan(void *priv,
7711 const struct wowlan_triggers *triggers)
7712{
7713 struct i802_bss *bss = priv;
7714 struct wpa_driver_nl80211_data *drv = bss->drv;
7715 struct nl_msg *msg;
7716 struct nlattr *wowlan_triggers;
7717 int ret;
7718
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07007719 wpa_printf(MSG_DEBUG, "nl80211: Setting wowlan");
7720
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007721 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_WOWLAN)) ||
7722 !(wowlan_triggers = nla_nest_start(msg,
7723 NL80211_ATTR_WOWLAN_TRIGGERS)) ||
7724 (triggers->any &&
7725 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
7726 (triggers->disconnect &&
7727 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
7728 (triggers->magic_pkt &&
7729 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
7730 (triggers->gtk_rekey_failure &&
7731 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
7732 (triggers->eap_identity_req &&
7733 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
7734 (triggers->four_way_handshake &&
7735 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
7736 (triggers->rfkill_release &&
7737 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))) {
7738 nlmsg_free(msg);
7739 return -ENOBUFS;
7740 }
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07007741
7742 nla_nest_end(msg, wowlan_triggers);
7743
7744 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7745 if (ret)
7746 wpa_printf(MSG_DEBUG, "nl80211: Setting wowlan failed");
7747
7748 return ret;
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07007749}
7750
7751
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007752static int nl80211_roaming(void *priv, int allowed, const u8 *bssid)
7753{
7754 struct i802_bss *bss = priv;
7755 struct wpa_driver_nl80211_data *drv = bss->drv;
7756 struct nl_msg *msg;
7757 struct nlattr *params;
7758
7759 wpa_printf(MSG_DEBUG, "nl80211: Roaming policy: allowed=%d", allowed);
7760
7761 if (!drv->roaming_vendor_cmd_avail) {
7762 wpa_printf(MSG_DEBUG,
7763 "nl80211: Ignore roaming policy change since driver does not provide command for setting it");
7764 return -1;
7765 }
7766
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007767 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
7768 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
7769 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
7770 QCA_NL80211_VENDOR_SUBCMD_ROAMING) ||
7771 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
7772 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_ROAMING_POLICY,
7773 allowed ? QCA_ROAMING_ALLOWED_WITHIN_ESS :
7774 QCA_ROAMING_NOT_ALLOWED) ||
7775 (bssid &&
7776 nla_put(msg, QCA_WLAN_VENDOR_ATTR_MAC_ADDR, ETH_ALEN, bssid))) {
7777 nlmsg_free(msg);
7778 return -1;
7779 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007780 nla_nest_end(msg, params);
7781
7782 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007783}
7784
7785
7786static int nl80211_set_mac_addr(void *priv, const u8 *addr)
7787{
7788 struct i802_bss *bss = priv;
7789 struct wpa_driver_nl80211_data *drv = bss->drv;
7790 int new_addr = addr != NULL;
7791
7792 if (!addr)
7793 addr = drv->perm_addr;
7794
7795 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0) < 0)
7796 return -1;
7797
7798 if (linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname, addr) < 0)
7799 {
7800 wpa_printf(MSG_DEBUG,
7801 "nl80211: failed to set_mac_addr for %s to " MACSTR,
7802 bss->ifname, MAC2STR(addr));
7803 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname,
7804 1) < 0) {
7805 wpa_printf(MSG_DEBUG,
7806 "nl80211: Could not restore interface UP after failed set_mac_addr");
7807 }
7808 return -1;
7809 }
7810
7811 wpa_printf(MSG_DEBUG, "nl80211: set_mac_addr for %s to " MACSTR,
7812 bss->ifname, MAC2STR(addr));
7813 drv->addr_changed = new_addr;
7814 os_memcpy(bss->addr, addr, ETH_ALEN);
7815
7816 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1) < 0)
7817 {
7818 wpa_printf(MSG_DEBUG,
7819 "nl80211: Could not restore interface UP after set_mac_addr");
7820 }
7821
7822 return 0;
7823}
7824
7825
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007826#ifdef CONFIG_MESH
7827
7828static int wpa_driver_nl80211_init_mesh(void *priv)
7829{
7830 if (wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_MESH_POINT)) {
7831 wpa_printf(MSG_INFO,
7832 "nl80211: Failed to set interface into mesh mode");
7833 return -1;
7834 }
7835 return 0;
7836}
7837
7838
Dmitry Shmidtff787d52015-01-12 13:01:47 -08007839static int nl80211_put_mesh_id(struct nl_msg *msg, const u8 *mesh_id,
7840 size_t mesh_id_len)
7841{
7842 if (mesh_id) {
7843 wpa_hexdump_ascii(MSG_DEBUG, " * Mesh ID (SSID)",
7844 mesh_id, mesh_id_len);
7845 return nla_put(msg, NL80211_ATTR_MESH_ID, mesh_id_len, mesh_id);
7846 }
7847
7848 return 0;
7849}
7850
7851
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007852static int
7853wpa_driver_nl80211_join_mesh(void *priv,
7854 struct wpa_driver_mesh_join_params *params)
7855{
7856 struct i802_bss *bss = priv;
7857 struct wpa_driver_nl80211_data *drv = bss->drv;
7858 struct nl_msg *msg;
7859 struct nlattr *container;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08007860 int ret = -1;
7861 u32 timeout;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007862
7863 wpa_printf(MSG_DEBUG, "nl80211: mesh join (ifindex=%d)", drv->ifindex);
7864 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_JOIN_MESH);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08007865 if (!msg ||
7866 nl80211_put_freq_params(msg, &params->freq) ||
7867 nl80211_put_basic_rates(msg, params->basic_rates) ||
7868 nl80211_put_mesh_id(msg, params->meshid, params->meshid_len) ||
7869 nl80211_put_beacon_int(msg, params->beacon_int))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007870 goto fail;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007871
7872 wpa_printf(MSG_DEBUG, " * flags=%08X", params->flags);
7873
7874 container = nla_nest_start(msg, NL80211_ATTR_MESH_SETUP);
7875 if (!container)
7876 goto fail;
7877
7878 if (params->ies) {
7879 wpa_hexdump(MSG_DEBUG, " * IEs", params->ies, params->ie_len);
7880 if (nla_put(msg, NL80211_MESH_SETUP_IE, params->ie_len,
7881 params->ies))
7882 goto fail;
7883 }
7884 /* WPA_DRIVER_MESH_FLAG_OPEN_AUTH is treated as default by nl80211 */
7885 if (params->flags & WPA_DRIVER_MESH_FLAG_SAE_AUTH) {
7886 if (nla_put_u8(msg, NL80211_MESH_SETUP_AUTH_PROTOCOL, 0x1) ||
7887 nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_AUTH))
7888 goto fail;
7889 }
7890 if ((params->flags & WPA_DRIVER_MESH_FLAG_AMPE) &&
7891 nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_AMPE))
7892 goto fail;
7893 if ((params->flags & WPA_DRIVER_MESH_FLAG_USER_MPM) &&
7894 nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_MPM))
7895 goto fail;
7896 nla_nest_end(msg, container);
7897
7898 container = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
7899 if (!container)
7900 goto fail;
7901
7902 if (!(params->conf.flags & WPA_DRIVER_MESH_CONF_FLAG_AUTO_PLINKS) &&
7903 nla_put_u32(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS, 0))
7904 goto fail;
7905 if ((params->conf.flags & WPA_DRIVER_MESH_FLAG_DRIVER_MPM) &&
7906 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
7907 params->max_peer_links))
7908 goto fail;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08007909
7910 /*
7911 * Set NL80211_MESHCONF_PLINK_TIMEOUT even if user mpm is used because
7912 * the timer could disconnect stations even in that case.
7913 *
7914 * Set 0xffffffff instead of 0 because NL80211_MESHCONF_PLINK_TIMEOUT
7915 * does not allow 0.
7916 */
7917 timeout = params->conf.peer_link_timeout;
7918 if ((params->flags & WPA_DRIVER_MESH_FLAG_USER_MPM) || timeout == 0)
7919 timeout = 0xffffffff;
7920 if (nla_put_u32(msg, NL80211_MESHCONF_PLINK_TIMEOUT, timeout)) {
7921 wpa_printf(MSG_ERROR, "nl80211: Failed to set PLINK_TIMEOUT");
7922 goto fail;
7923 }
7924
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007925 nla_nest_end(msg, container);
7926
7927 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7928 msg = NULL;
7929 if (ret) {
7930 wpa_printf(MSG_DEBUG, "nl80211: mesh join failed: ret=%d (%s)",
7931 ret, strerror(-ret));
7932 goto fail;
7933 }
7934 ret = 0;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08007935 bss->freq = params->freq.freq;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007936 wpa_printf(MSG_DEBUG, "nl80211: mesh join request send successfully");
7937
7938fail:
7939 nlmsg_free(msg);
7940 return ret;
7941}
7942
7943
7944static int wpa_driver_nl80211_leave_mesh(void *priv)
7945{
7946 struct i802_bss *bss = priv;
7947 struct wpa_driver_nl80211_data *drv = bss->drv;
7948 struct nl_msg *msg;
7949 int ret;
7950
7951 wpa_printf(MSG_DEBUG, "nl80211: mesh leave (ifindex=%d)", drv->ifindex);
7952 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_LEAVE_MESH);
7953 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7954 if (ret) {
7955 wpa_printf(MSG_DEBUG, "nl80211: mesh leave failed: ret=%d (%s)",
7956 ret, strerror(-ret));
7957 } else {
7958 wpa_printf(MSG_DEBUG,
7959 "nl80211: mesh leave request send successfully");
7960 }
7961
7962 if (wpa_driver_nl80211_set_mode(drv->first_bss,
7963 NL80211_IFTYPE_STATION)) {
7964 wpa_printf(MSG_INFO,
7965 "nl80211: Failed to set interface into station mode");
7966 }
7967 return ret;
7968}
7969
7970#endif /* CONFIG_MESH */
7971
7972
7973static int wpa_driver_br_add_ip_neigh(void *priv, u8 version,
7974 const u8 *ipaddr, int prefixlen,
7975 const u8 *addr)
7976{
7977#ifdef CONFIG_LIBNL3_ROUTE
7978 struct i802_bss *bss = priv;
7979 struct wpa_driver_nl80211_data *drv = bss->drv;
7980 struct rtnl_neigh *rn;
7981 struct nl_addr *nl_ipaddr = NULL;
7982 struct nl_addr *nl_lladdr = NULL;
7983 int family, addrsize;
7984 int res;
7985
7986 if (!ipaddr || prefixlen == 0 || !addr)
7987 return -EINVAL;
7988
7989 if (bss->br_ifindex == 0) {
7990 wpa_printf(MSG_DEBUG,
7991 "nl80211: bridge must be set before adding an ip neigh to it");
7992 return -1;
7993 }
7994
7995 if (!drv->rtnl_sk) {
7996 wpa_printf(MSG_DEBUG,
7997 "nl80211: nl_sock for NETLINK_ROUTE is not initialized");
7998 return -1;
7999 }
8000
8001 if (version == 4) {
8002 family = AF_INET;
8003 addrsize = 4;
8004 } else if (version == 6) {
8005 family = AF_INET6;
8006 addrsize = 16;
8007 } else {
8008 return -EINVAL;
8009 }
8010
8011 rn = rtnl_neigh_alloc();
8012 if (rn == NULL)
8013 return -ENOMEM;
8014
8015 /* set the destination ip address for neigh */
8016 nl_ipaddr = nl_addr_build(family, (void *) ipaddr, addrsize);
8017 if (nl_ipaddr == NULL) {
8018 wpa_printf(MSG_DEBUG, "nl80211: nl_ipaddr build failed");
8019 res = -ENOMEM;
8020 goto errout;
8021 }
8022 nl_addr_set_prefixlen(nl_ipaddr, prefixlen);
8023 res = rtnl_neigh_set_dst(rn, nl_ipaddr);
8024 if (res) {
8025 wpa_printf(MSG_DEBUG,
8026 "nl80211: neigh set destination addr failed");
8027 goto errout;
8028 }
8029
8030 /* set the corresponding lladdr for neigh */
8031 nl_lladdr = nl_addr_build(AF_BRIDGE, (u8 *) addr, ETH_ALEN);
8032 if (nl_lladdr == NULL) {
8033 wpa_printf(MSG_DEBUG, "nl80211: neigh set lladdr failed");
8034 res = -ENOMEM;
8035 goto errout;
8036 }
8037 rtnl_neigh_set_lladdr(rn, nl_lladdr);
8038
8039 rtnl_neigh_set_ifindex(rn, bss->br_ifindex);
8040 rtnl_neigh_set_state(rn, NUD_PERMANENT);
8041
8042 res = rtnl_neigh_add(drv->rtnl_sk, rn, NLM_F_CREATE);
8043 if (res) {
8044 wpa_printf(MSG_DEBUG,
8045 "nl80211: Adding bridge ip neigh failed: %s",
8046 strerror(errno));
8047 }
8048errout:
8049 if (nl_lladdr)
8050 nl_addr_put(nl_lladdr);
8051 if (nl_ipaddr)
8052 nl_addr_put(nl_ipaddr);
8053 if (rn)
8054 rtnl_neigh_put(rn);
8055 return res;
8056#else /* CONFIG_LIBNL3_ROUTE */
8057 return -1;
8058#endif /* CONFIG_LIBNL3_ROUTE */
8059}
8060
8061
8062static int wpa_driver_br_delete_ip_neigh(void *priv, u8 version,
8063 const u8 *ipaddr)
8064{
8065#ifdef CONFIG_LIBNL3_ROUTE
8066 struct i802_bss *bss = priv;
8067 struct wpa_driver_nl80211_data *drv = bss->drv;
8068 struct rtnl_neigh *rn;
8069 struct nl_addr *nl_ipaddr;
8070 int family, addrsize;
8071 int res;
8072
8073 if (!ipaddr)
8074 return -EINVAL;
8075
8076 if (version == 4) {
8077 family = AF_INET;
8078 addrsize = 4;
8079 } else if (version == 6) {
8080 family = AF_INET6;
8081 addrsize = 16;
8082 } else {
8083 return -EINVAL;
8084 }
8085
8086 if (bss->br_ifindex == 0) {
8087 wpa_printf(MSG_DEBUG,
8088 "nl80211: bridge must be set to delete an ip neigh");
8089 return -1;
8090 }
8091
8092 if (!drv->rtnl_sk) {
8093 wpa_printf(MSG_DEBUG,
8094 "nl80211: nl_sock for NETLINK_ROUTE is not initialized");
8095 return -1;
8096 }
8097
8098 rn = rtnl_neigh_alloc();
8099 if (rn == NULL)
8100 return -ENOMEM;
8101
8102 /* set the destination ip address for neigh */
8103 nl_ipaddr = nl_addr_build(family, (void *) ipaddr, addrsize);
8104 if (nl_ipaddr == NULL) {
8105 wpa_printf(MSG_DEBUG, "nl80211: nl_ipaddr build failed");
8106 res = -ENOMEM;
8107 goto errout;
8108 }
8109 res = rtnl_neigh_set_dst(rn, nl_ipaddr);
8110 if (res) {
8111 wpa_printf(MSG_DEBUG,
8112 "nl80211: neigh set destination addr failed");
8113 goto errout;
8114 }
8115
8116 rtnl_neigh_set_ifindex(rn, bss->br_ifindex);
8117
8118 res = rtnl_neigh_delete(drv->rtnl_sk, rn, 0);
8119 if (res) {
8120 wpa_printf(MSG_DEBUG,
8121 "nl80211: Deleting bridge ip neigh failed: %s",
8122 strerror(errno));
8123 }
8124errout:
8125 if (nl_ipaddr)
8126 nl_addr_put(nl_ipaddr);
8127 if (rn)
8128 rtnl_neigh_put(rn);
8129 return res;
8130#else /* CONFIG_LIBNL3_ROUTE */
8131 return -1;
8132#endif /* CONFIG_LIBNL3_ROUTE */
8133}
8134
8135
8136static int linux_write_system_file(const char *path, unsigned int val)
8137{
8138 char buf[50];
8139 int fd, len;
8140
8141 len = os_snprintf(buf, sizeof(buf), "%u\n", val);
8142 if (os_snprintf_error(sizeof(buf), len))
8143 return -1;
8144
8145 fd = open(path, O_WRONLY);
8146 if (fd < 0)
8147 return -1;
8148
8149 if (write(fd, buf, len) < 0) {
8150 wpa_printf(MSG_DEBUG,
8151 "nl80211: Failed to write Linux system file: %s with the value of %d",
8152 path, val);
8153 close(fd);
8154 return -1;
8155 }
8156 close(fd);
8157
8158 return 0;
8159}
8160
8161
8162static const char * drv_br_port_attr_str(enum drv_br_port_attr attr)
8163{
8164 switch (attr) {
8165 case DRV_BR_PORT_ATTR_PROXYARP:
8166 return "proxyarp";
8167 case DRV_BR_PORT_ATTR_HAIRPIN_MODE:
8168 return "hairpin_mode";
8169 }
8170
8171 return NULL;
8172}
8173
8174
8175static int wpa_driver_br_port_set_attr(void *priv, enum drv_br_port_attr attr,
8176 unsigned int val)
8177{
8178 struct i802_bss *bss = priv;
8179 char path[128];
8180 const char *attr_txt;
8181
8182 attr_txt = drv_br_port_attr_str(attr);
8183 if (attr_txt == NULL)
8184 return -EINVAL;
8185
8186 os_snprintf(path, sizeof(path), "/sys/class/net/%s/brport/%s",
8187 bss->ifname, attr_txt);
8188
8189 if (linux_write_system_file(path, val))
8190 return -1;
8191
8192 return 0;
8193}
8194
8195
8196static const char * drv_br_net_param_str(enum drv_br_net_param param)
8197{
8198 switch (param) {
8199 case DRV_BR_NET_PARAM_GARP_ACCEPT:
8200 return "arp_accept";
8201 }
8202
8203 return NULL;
8204}
8205
8206
8207static int wpa_driver_br_set_net_param(void *priv, enum drv_br_net_param param,
8208 unsigned int val)
8209{
8210 struct i802_bss *bss = priv;
8211 char path[128];
8212 const char *param_txt;
8213 int ip_version = 4;
8214
8215 param_txt = drv_br_net_param_str(param);
8216 if (param_txt == NULL)
8217 return -EINVAL;
8218
8219 switch (param) {
8220 case DRV_BR_NET_PARAM_GARP_ACCEPT:
8221 ip_version = 4;
8222 break;
8223 default:
8224 return -EINVAL;
8225 }
8226
8227 os_snprintf(path, sizeof(path), "/proc/sys/net/ipv%d/conf/%s/%s",
8228 ip_version, bss->brname, param_txt);
8229
8230 if (linux_write_system_file(path, val))
8231 return -1;
8232
8233 return 0;
8234}
8235
8236
8237static int hw_mode_to_qca_acs(enum hostapd_hw_mode hw_mode)
8238{
8239 switch (hw_mode) {
8240 case HOSTAPD_MODE_IEEE80211B:
8241 return QCA_ACS_MODE_IEEE80211B;
8242 case HOSTAPD_MODE_IEEE80211G:
8243 return QCA_ACS_MODE_IEEE80211G;
8244 case HOSTAPD_MODE_IEEE80211A:
8245 return QCA_ACS_MODE_IEEE80211A;
8246 case HOSTAPD_MODE_IEEE80211AD:
8247 return QCA_ACS_MODE_IEEE80211AD;
8248 default:
8249 return -1;
8250 }
8251}
8252
8253
8254static int wpa_driver_do_acs(void *priv, struct drv_acs_params *params)
8255{
8256 struct i802_bss *bss = priv;
8257 struct wpa_driver_nl80211_data *drv = bss->drv;
8258 struct nl_msg *msg;
8259 struct nlattr *data;
8260 int ret;
8261 int mode;
8262
8263 mode = hw_mode_to_qca_acs(params->hw_mode);
8264 if (mode < 0)
8265 return -1;
8266
8267 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
8268 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
8269 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
8270 QCA_NL80211_VENDOR_SUBCMD_DO_ACS) ||
8271 !(data = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
8272 nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_ACS_HW_MODE, mode) ||
8273 (params->ht_enabled &&
8274 nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_HT_ENABLED)) ||
8275 (params->ht40_enabled &&
8276 nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_HT40_ENABLED))) {
8277 nlmsg_free(msg);
8278 return -ENOBUFS;
8279 }
8280 nla_nest_end(msg, data);
8281
8282 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8283 if (ret) {
8284 wpa_printf(MSG_DEBUG,
8285 "nl80211: Failed to invoke driver ACS function: %s",
8286 strerror(errno));
8287 }
8288 return ret;
8289}
8290
8291
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008292const struct wpa_driver_ops wpa_driver_nl80211_ops = {
8293 .name = "nl80211",
8294 .desc = "Linux nl80211/cfg80211",
8295 .get_bssid = wpa_driver_nl80211_get_bssid,
8296 .get_ssid = wpa_driver_nl80211_get_ssid,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008297 .set_key = driver_nl80211_set_key,
8298 .scan2 = driver_nl80211_scan2,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008299 .sched_scan = wpa_driver_nl80211_sched_scan,
8300 .stop_sched_scan = wpa_driver_nl80211_stop_sched_scan,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008301 .get_scan_results2 = wpa_driver_nl80211_get_scan_results,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008302 .deauthenticate = driver_nl80211_deauthenticate,
8303 .authenticate = driver_nl80211_authenticate,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008304 .associate = wpa_driver_nl80211_associate,
8305 .global_init = nl80211_global_init,
8306 .global_deinit = nl80211_global_deinit,
8307 .init2 = wpa_driver_nl80211_init,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008308 .deinit = driver_nl80211_deinit,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008309 .get_capa = wpa_driver_nl80211_get_capa,
8310 .set_operstate = wpa_driver_nl80211_set_operstate,
8311 .set_supp_port = wpa_driver_nl80211_set_supp_port,
8312 .set_country = wpa_driver_nl80211_set_country,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008313 .get_country = wpa_driver_nl80211_get_country,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008314 .set_ap = wpa_driver_nl80211_set_ap,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07008315 .set_acl = wpa_driver_nl80211_set_acl,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008316 .if_add = wpa_driver_nl80211_if_add,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008317 .if_remove = driver_nl80211_if_remove,
8318 .send_mlme = driver_nl80211_send_mlme,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008319 .get_hw_feature_data = nl80211_get_hw_feature_data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008320 .sta_add = wpa_driver_nl80211_sta_add,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008321 .sta_remove = driver_nl80211_sta_remove,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008322 .hapd_send_eapol = wpa_driver_nl80211_hapd_send_eapol,
8323 .sta_set_flags = wpa_driver_nl80211_sta_set_flags,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008324 .hapd_init = i802_init,
8325 .hapd_deinit = i802_deinit,
Jouni Malinen75ecf522011-06-27 15:19:46 -07008326 .set_wds_sta = i802_set_wds_sta,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008327 .get_seqnum = i802_get_seqnum,
8328 .flush = i802_flush,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008329 .get_inact_sec = i802_get_inact_sec,
8330 .sta_clear_stats = i802_sta_clear_stats,
8331 .set_rts = i802_set_rts,
8332 .set_frag = i802_set_frag,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008333 .set_tx_queue_params = i802_set_tx_queue_params,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008334 .set_sta_vlan = driver_nl80211_set_sta_vlan,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008335 .sta_deauth = i802_sta_deauth,
8336 .sta_disassoc = i802_sta_disassoc,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008337 .read_sta_data = driver_nl80211_read_sta_data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008338 .set_freq = i802_set_freq,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008339 .send_action = driver_nl80211_send_action,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008340 .send_action_cancel_wait = wpa_driver_nl80211_send_action_cancel_wait,
8341 .remain_on_channel = wpa_driver_nl80211_remain_on_channel,
8342 .cancel_remain_on_channel =
8343 wpa_driver_nl80211_cancel_remain_on_channel,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008344 .probe_req_report = driver_nl80211_probe_req_report,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008345 .deinit_ap = wpa_driver_nl80211_deinit_ap,
Dmitry Shmidt04949592012-07-19 12:16:46 -07008346 .deinit_p2p_cli = wpa_driver_nl80211_deinit_p2p_cli,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008347 .resume = wpa_driver_nl80211_resume,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008348 .signal_monitor = nl80211_signal_monitor,
8349 .signal_poll = nl80211_signal_poll,
8350 .send_frame = nl80211_send_frame,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008351 .shared_freq = wpa_driver_nl80211_shared_freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008352 .set_param = nl80211_set_param,
8353 .get_radio_name = nl80211_get_radio_name,
Jouni Malinen75ecf522011-06-27 15:19:46 -07008354 .add_pmkid = nl80211_add_pmkid,
8355 .remove_pmkid = nl80211_remove_pmkid,
8356 .flush_pmkid = nl80211_flush_pmkid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008357 .set_rekey_info = nl80211_set_rekey_info,
8358 .poll_client = nl80211_poll_client,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008359 .set_p2p_powersave = nl80211_set_p2p_powersave,
Dmitry Shmidtea69e842013-05-13 14:52:28 -07008360 .start_dfs_cac = nl80211_start_radar_detection,
8361 .stop_ap = wpa_driver_nl80211_stop_ap,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008362#ifdef CONFIG_TDLS
8363 .send_tdls_mgmt = nl80211_send_tdls_mgmt,
8364 .tdls_oper = nl80211_tdls_oper,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008365 .tdls_enable_channel_switch = nl80211_tdls_enable_channel_switch,
8366 .tdls_disable_channel_switch = nl80211_tdls_disable_channel_switch,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008367#endif /* CONFIG_TDLS */
Dmitry Shmidt700a1372013-03-15 14:14:44 -07008368 .update_ft_ies = wpa_driver_nl80211_update_ft_ies,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008369 .get_mac_addr = wpa_driver_nl80211_get_macaddr,
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008370 .get_survey = wpa_driver_nl80211_get_survey,
Dmitry Shmidt56052862013-10-04 10:23:25 -07008371 .status = wpa_driver_nl80211_status,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008372 .switch_channel = nl80211_switch_channel,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008373#ifdef ANDROID_P2P
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07008374 .set_noa = wpa_driver_set_p2p_noa,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008375 .get_noa = wpa_driver_get_p2p_noa,
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07008376 .set_ap_wps_ie = wpa_driver_set_ap_wps_p2p_ie,
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08008377#endif /* ANDROID_P2P */
Dmitry Shmidt738a26e2011-07-07 14:22:14 -07008378#ifdef ANDROID
8379 .driver_cmd = wpa_driver_nl80211_driver_cmd,
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08008380#endif /* ANDROID */
Dmitry Shmidta38abf92014-03-06 13:38:44 -08008381 .vendor_cmd = nl80211_vendor_cmd,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008382 .set_qos_map = nl80211_set_qos_map,
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07008383 .set_wowlan = nl80211_set_wowlan,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008384 .roaming = nl80211_roaming,
8385 .set_mac_addr = nl80211_set_mac_addr,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008386#ifdef CONFIG_MESH
8387 .init_mesh = wpa_driver_nl80211_init_mesh,
8388 .join_mesh = wpa_driver_nl80211_join_mesh,
8389 .leave_mesh = wpa_driver_nl80211_leave_mesh,
8390#endif /* CONFIG_MESH */
8391 .br_add_ip_neigh = wpa_driver_br_add_ip_neigh,
8392 .br_delete_ip_neigh = wpa_driver_br_delete_ip_neigh,
8393 .br_port_set_attr = wpa_driver_br_port_set_attr,
8394 .br_set_net_param = wpa_driver_br_set_net_param,
8395 .add_tx_ts = nl80211_add_ts,
8396 .del_tx_ts = nl80211_del_ts,
8397 .do_acs = wpa_driver_do_acs,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008398};