blob: 2a05a3b806f326829126bc126047196513066136 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * Driver interaction with Linux nl80211/cfg80211
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003 * Copyright (c) 2002-2015, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 * Copyright (c) 2003-2004, Instant802 Networks, Inc.
5 * Copyright (c) 2005-2006, Devicescape Software, Inc.
6 * Copyright (c) 2007, Johannes Berg <johannes@sipsolutions.net>
7 * Copyright (c) 2009-2010, Atheros Communications
8 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08009 * This software may be distributed under the terms of the BSD license.
10 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011 */
12
13#include "includes.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014#include <sys/types.h>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070015#include <fcntl.h>
16#include <net/if.h>
17#include <netlink/genl/genl.h>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070018#include <netlink/genl/ctrl.h>
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070019#ifdef CONFIG_LIBNL3_ROUTE
20#include <netlink/route/neighbour.h>
21#endif /* CONFIG_LIBNL3_ROUTE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070022#include <linux/rtnetlink.h>
23#include <netpacket/packet.h>
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080024#include <linux/errqueue.h>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070025
26#include "common.h"
27#include "eloop.h"
Dmitry Shmidtcf32e602014-01-28 10:57:39 -080028#include "common/qca-vendor.h"
Dmitry Shmidt7832adb2014-04-29 10:53:02 -070029#include "common/qca-vendor-attr.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070030#include "common/ieee802_11_defs.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080031#include "common/ieee802_11_common.h"
32#include "l2_packet/l2_packet.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070033#include "netlink.h"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080034#include "linux_defines.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070035#include "linux_ioctl.h"
36#include "radiotap.h"
37#include "radiotap_iter.h"
38#include "rfkill.h"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080039#include "driver_nl80211.h"
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080040
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080041
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080042#ifndef CONFIG_LIBNL20
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070043/*
44 * libnl 1.1 has a bug, it tries to allocate socket numbers densely
45 * but when you free a socket again it will mess up its bitmap and
46 * and use the wrong number the next time it needs a socket ID.
47 * Therefore, we wrap the handle alloc/destroy and add our own pid
48 * accounting.
49 */
50static uint32_t port_bitmap[32] = { 0 };
51
52static struct nl_handle *nl80211_handle_alloc(void *cb)
53{
54 struct nl_handle *handle;
55 uint32_t pid = getpid() & 0x3FFFFF;
56 int i;
57
58 handle = nl_handle_alloc_cb(cb);
59
60 for (i = 0; i < 1024; i++) {
61 if (port_bitmap[i / 32] & (1 << (i % 32)))
62 continue;
63 port_bitmap[i / 32] |= 1 << (i % 32);
64 pid += i << 22;
65 break;
66 }
67
68 nl_socket_set_local_port(handle, pid);
69
70 return handle;
71}
72
73static void nl80211_handle_destroy(struct nl_handle *handle)
74{
75 uint32_t port = nl_socket_get_local_port(handle);
76
77 port >>= 22;
78 port_bitmap[port / 32] &= ~(1 << (port % 32));
79
80 nl_handle_destroy(handle);
81}
82#endif /* CONFIG_LIBNL20 */
83
84
Dmitry Shmidt54605472013-11-08 11:10:19 -080085#ifdef ANDROID
86/* system/core/libnl_2 does not include nl_socket_set_nonblocking() */
Dmitry Shmidt54605472013-11-08 11:10:19 -080087#undef nl_socket_set_nonblocking
88#define nl_socket_set_nonblocking(h) android_nl_socket_set_nonblocking(h)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080089
Dmitry Shmidt54605472013-11-08 11:10:19 -080090#endif /* ANDROID */
91
92
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080093static struct nl_handle * nl_create_handle(struct nl_cb *cb, const char *dbg)
94{
95 struct nl_handle *handle;
96
97 handle = nl80211_handle_alloc(cb);
98 if (handle == NULL) {
99 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
100 "callbacks (%s)", dbg);
101 return NULL;
102 }
103
104 if (genl_connect(handle)) {
105 wpa_printf(MSG_ERROR, "nl80211: Failed to connect to generic "
106 "netlink (%s)", dbg);
107 nl80211_handle_destroy(handle);
108 return NULL;
109 }
110
111 return handle;
112}
113
114
115static void nl_destroy_handles(struct nl_handle **handle)
116{
117 if (*handle == NULL)
118 return;
119 nl80211_handle_destroy(*handle);
120 *handle = NULL;
121}
122
123
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700124#if __WORDSIZE == 64
125#define ELOOP_SOCKET_INVALID (intptr_t) 0x8888888888888889ULL
126#else
127#define ELOOP_SOCKET_INVALID (intptr_t) 0x88888889ULL
128#endif
129
130static void nl80211_register_eloop_read(struct nl_handle **handle,
131 eloop_sock_handler handler,
132 void *eloop_data)
133{
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800134#ifdef CONFIG_LIBNL20
135 /*
136 * libnl uses a pretty small buffer (32 kB that gets converted to 64 kB)
137 * by default. It is possible to hit that limit in some cases where
138 * operations are blocked, e.g., with a burst of Deauthentication frames
139 * to hostapd and STA entry deletion. Try to increase the buffer to make
140 * this less likely to occur.
141 */
142 if (nl_socket_set_buffer_size(*handle, 262144, 0) < 0) {
143 wpa_printf(MSG_DEBUG,
144 "nl80211: Could not set nl_socket RX buffer size: %s",
145 strerror(errno));
146 /* continue anyway with the default (smaller) buffer */
147 }
148#endif /* CONFIG_LIBNL20 */
149
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700150 nl_socket_set_nonblocking(*handle);
151 eloop_register_read_sock(nl_socket_get_fd(*handle), handler,
152 eloop_data, *handle);
153 *handle = (void *) (((intptr_t) *handle) ^ ELOOP_SOCKET_INVALID);
154}
155
156
157static void nl80211_destroy_eloop_handle(struct nl_handle **handle)
158{
159 *handle = (void *) (((intptr_t) *handle) ^ ELOOP_SOCKET_INVALID);
160 eloop_unregister_read_sock(nl_socket_get_fd(*handle));
161 nl_destroy_handles(handle);
162}
163
164
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800165static void nl80211_global_deinit(void *priv);
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800166static void nl80211_check_global(struct nl80211_global *global);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800167
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800168static void wpa_driver_nl80211_deinit(struct i802_bss *bss);
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -0700169static int wpa_driver_nl80211_set_mode_ibss(struct i802_bss *bss,
170 struct hostapd_freq_params *freq);
Dmitry Shmidtd30ac602014-06-30 09:54:22 -0700171
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700172static int
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800173wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800174 const u8 *set_addr, int first,
175 const char *driver_params);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800176static int nl80211_send_frame_cmd(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700177 unsigned int freq, unsigned int wait,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800178 const u8 *buf, size_t buf_len, u64 *cookie,
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 Shmidt7f656022015-02-25 14:36:37 -0800867 nl80211_check_global(drv->global);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700868 wpa_printf(MSG_DEBUG, "nl80211: Update ifindex for a removed "
869 "interface");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800870 wpa_driver_nl80211_finish_drv_init(drv, NULL, 0, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700871 return 1;
872 }
873
874 return 0;
875}
876
877
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800878static struct wpa_driver_nl80211_data *
879nl80211_find_drv(struct nl80211_global *global, int idx, u8 *buf, size_t len)
880{
881 struct wpa_driver_nl80211_data *drv;
882 dl_list_for_each(drv, &global->interfaces,
883 struct wpa_driver_nl80211_data, list) {
884 if (wpa_driver_nl80211_own_ifindex(drv, idx, buf, len) ||
885 have_ifidx(drv, idx))
886 return drv;
887 }
888 return NULL;
889}
890
891
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700892static void wpa_driver_nl80211_event_rtm_newlink(void *ctx,
893 struct ifinfomsg *ifi,
894 u8 *buf, size_t len)
895{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800896 struct nl80211_global *global = ctx;
897 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800898 int attrlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700899 struct rtattr *attr;
900 u32 brid = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800901 char namebuf[IFNAMSIZ];
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800902 char ifname[IFNAMSIZ + 1];
903 char extra[100], *pos, *end;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700904
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800905 drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
906 if (!drv) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800907 wpa_printf(MSG_DEBUG, "nl80211: Ignore RTM_NEWLINK event for foreign ifindex %d",
908 ifi->ifi_index);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700909 return;
910 }
911
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800912 extra[0] = '\0';
913 pos = extra;
914 end = pos + sizeof(extra);
915 ifname[0] = '\0';
916
917 attrlen = len;
918 attr = (struct rtattr *) buf;
919 while (RTA_OK(attr, attrlen)) {
920 switch (attr->rta_type) {
921 case IFLA_IFNAME:
922 if (RTA_PAYLOAD(attr) >= IFNAMSIZ)
923 break;
924 os_memcpy(ifname, RTA_DATA(attr), RTA_PAYLOAD(attr));
925 ifname[RTA_PAYLOAD(attr)] = '\0';
926 break;
927 case IFLA_MASTER:
928 brid = nla_get_u32((struct nlattr *) attr);
929 pos += os_snprintf(pos, end - pos, " master=%u", brid);
930 break;
931 case IFLA_WIRELESS:
932 pos += os_snprintf(pos, end - pos, " wext");
933 break;
934 case IFLA_OPERSTATE:
935 pos += os_snprintf(pos, end - pos, " operstate=%u",
936 nla_get_u32((struct nlattr *) attr));
937 break;
938 case IFLA_LINKMODE:
939 pos += os_snprintf(pos, end - pos, " linkmode=%u",
940 nla_get_u32((struct nlattr *) attr));
941 break;
942 }
943 attr = RTA_NEXT(attr, attrlen);
944 }
945 extra[sizeof(extra) - 1] = '\0';
946
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700947 wpa_printf(MSG_DEBUG, "RTM_NEWLINK: ifi_index=%d ifname=%s%s ifi_family=%d ifi_flags=0x%x (%s%s%s%s)",
948 ifi->ifi_index, ifname, extra, ifi->ifi_family,
949 ifi->ifi_flags,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700950 (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
951 (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
952 (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
953 (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
954
955 if (!drv->if_disabled && !(ifi->ifi_flags & IFF_UP)) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800956 namebuf[0] = '\0';
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800957 if (if_indextoname(ifi->ifi_index, namebuf) &&
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800958 linux_iface_up(drv->global->ioctl_sock, namebuf) > 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800959 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
960 "event since interface %s is up", namebuf);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800961 drv->ignore_if_down_event = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800962 return;
963 }
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800964 wpa_printf(MSG_DEBUG, "nl80211: Interface down (%s/%s)",
965 namebuf, ifname);
966 if (os_strcmp(drv->first_bss->ifname, ifname) != 0) {
967 wpa_printf(MSG_DEBUG,
968 "nl80211: Not the main interface (%s) - do not indicate interface down",
969 drv->first_bss->ifname);
970 } else if (drv->ignore_if_down_event) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800971 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
972 "event generated by mode change");
973 drv->ignore_if_down_event = 0;
974 } else {
975 drv->if_disabled = 1;
976 wpa_supplicant_event(drv->ctx,
977 EVENT_INTERFACE_DISABLED, NULL);
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800978
979 /*
980 * Try to get drv again, since it may be removed as
981 * part of the EVENT_INTERFACE_DISABLED handling for
982 * dynamic interfaces
983 */
984 drv = nl80211_find_drv(global, ifi->ifi_index,
985 buf, len);
986 if (!drv)
987 return;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800988 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700989 }
990
991 if (drv->if_disabled && (ifi->ifi_flags & IFF_UP)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800992 if (if_indextoname(ifi->ifi_index, namebuf) &&
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800993 linux_iface_up(drv->global->ioctl_sock, namebuf) == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800994 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
995 "event since interface %s is down",
996 namebuf);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800997 } else if (if_nametoindex(drv->first_bss->ifname) == 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700998 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
999 "event since interface %s does not exist",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001000 drv->first_bss->ifname);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001001 } else if (drv->if_removed) {
1002 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
1003 "event since interface %s is marked "
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001004 "removed", drv->first_bss->ifname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001005 } else {
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07001006 struct i802_bss *bss;
1007 u8 addr[ETH_ALEN];
1008
1009 /* Re-read MAC address as it may have changed */
1010 bss = get_bss_ifindex(drv, ifi->ifi_index);
1011 if (bss &&
1012 linux_get_ifhwaddr(drv->global->ioctl_sock,
1013 bss->ifname, addr) < 0) {
1014 wpa_printf(MSG_DEBUG,
1015 "nl80211: %s: failed to re-read MAC address",
1016 bss->ifname);
1017 } else if (bss &&
1018 os_memcmp(addr, bss->addr, ETH_ALEN) != 0) {
1019 wpa_printf(MSG_DEBUG,
1020 "nl80211: Own MAC address on ifindex %d (%s) changed from "
1021 MACSTR " to " MACSTR,
1022 ifi->ifi_index, bss->ifname,
1023 MAC2STR(bss->addr),
1024 MAC2STR(addr));
1025 os_memcpy(bss->addr, addr, ETH_ALEN);
1026 }
1027
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001028 wpa_printf(MSG_DEBUG, "nl80211: Interface up");
1029 drv->if_disabled = 0;
1030 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_ENABLED,
1031 NULL);
1032 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001033 }
1034
1035 /*
1036 * Some drivers send the association event before the operup event--in
1037 * this case, lifting operstate in wpa_driver_nl80211_set_operstate()
1038 * fails. This will hit us when wpa_supplicant does not need to do
1039 * IEEE 802.1X authentication
1040 */
1041 if (drv->operstate == 1 &&
1042 (ifi->ifi_flags & (IFF_LOWER_UP | IFF_DORMANT)) == IFF_LOWER_UP &&
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001043 !(ifi->ifi_flags & IFF_RUNNING)) {
1044 wpa_printf(MSG_DEBUG, "nl80211: Set IF_OPER_UP again based on ifi_flags and expected operstate");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001045 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001046 -1, IF_OPER_UP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001047 }
1048
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001049 if (ifname[0])
1050 wpa_driver_nl80211_event_newlink(drv, ifname);
1051
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001052 if (ifi->ifi_family == AF_BRIDGE && brid) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001053 struct i802_bss *bss;
1054
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001055 /* device has been added to bridge */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001056 if (!if_indextoname(brid, namebuf)) {
1057 wpa_printf(MSG_DEBUG,
1058 "nl80211: Could not find bridge ifname for ifindex %u",
1059 brid);
1060 return;
1061 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001062 wpa_printf(MSG_DEBUG, "nl80211: Add ifindex %u for bridge %s",
1063 brid, namebuf);
1064 add_ifidx(drv, brid);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001065
1066 for (bss = drv->first_bss; bss; bss = bss->next) {
1067 if (os_strcmp(ifname, bss->ifname) == 0) {
1068 os_strlcpy(bss->brname, namebuf, IFNAMSIZ);
1069 break;
1070 }
1071 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001072 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001073}
1074
1075
1076static void wpa_driver_nl80211_event_rtm_dellink(void *ctx,
1077 struct ifinfomsg *ifi,
1078 u8 *buf, size_t len)
1079{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001080 struct nl80211_global *global = ctx;
1081 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001082 int attrlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001083 struct rtattr *attr;
1084 u32 brid = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001085 char ifname[IFNAMSIZ + 1];
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001086 char extra[100], *pos, *end;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001087
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001088 drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
1089 if (!drv) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001090 wpa_printf(MSG_DEBUG, "nl80211: Ignore RTM_DELLINK event for foreign ifindex %d",
1091 ifi->ifi_index);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001092 return;
1093 }
1094
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001095 extra[0] = '\0';
1096 pos = extra;
1097 end = pos + sizeof(extra);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001098 ifname[0] = '\0';
1099
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001100 attrlen = len;
1101 attr = (struct rtattr *) buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001102 while (RTA_OK(attr, attrlen)) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001103 switch (attr->rta_type) {
1104 case IFLA_IFNAME:
1105 if (RTA_PAYLOAD(attr) >= IFNAMSIZ)
1106 break;
1107 os_memcpy(ifname, RTA_DATA(attr), RTA_PAYLOAD(attr));
1108 ifname[RTA_PAYLOAD(attr)] = '\0';
1109 break;
1110 case IFLA_MASTER:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001111 brid = nla_get_u32((struct nlattr *) attr);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001112 pos += os_snprintf(pos, end - pos, " master=%u", brid);
1113 break;
1114 case IFLA_OPERSTATE:
1115 pos += os_snprintf(pos, end - pos, " operstate=%u",
1116 nla_get_u32((struct nlattr *) attr));
1117 break;
1118 case IFLA_LINKMODE:
1119 pos += os_snprintf(pos, end - pos, " linkmode=%u",
1120 nla_get_u32((struct nlattr *) attr));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001121 break;
1122 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001123 attr = RTA_NEXT(attr, attrlen);
1124 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001125 extra[sizeof(extra) - 1] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001126
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001127 wpa_printf(MSG_DEBUG, "RTM_DELLINK: ifi_index=%d ifname=%s%s ifi_family=%d ifi_flags=0x%x (%s%s%s%s)",
1128 ifi->ifi_index, ifname, extra, ifi->ifi_family,
1129 ifi->ifi_flags,
1130 (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
1131 (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
1132 (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
1133 (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
1134
1135 if (ifname[0] && (ifi->ifi_family != AF_BRIDGE || !brid))
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001136 wpa_driver_nl80211_event_dellink(drv, ifname);
1137
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001138 if (ifi->ifi_family == AF_BRIDGE && brid) {
1139 /* device has been removed from bridge */
1140 char namebuf[IFNAMSIZ];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001141
1142 if (!if_indextoname(brid, namebuf)) {
1143 wpa_printf(MSG_DEBUG,
1144 "nl80211: Could not find bridge ifname for ifindex %u",
1145 brid);
1146 } else {
1147 wpa_printf(MSG_DEBUG,
1148 "nl80211: Remove ifindex %u for bridge %s",
1149 brid, namebuf);
1150 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001151 del_ifidx(drv, brid);
1152 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001153}
1154
1155
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001156unsigned int nl80211_get_assoc_freq(struct wpa_driver_nl80211_data *drv)
Jouni Malinen87fd2792011-05-16 18:35:42 +03001157{
1158 struct nl_msg *msg;
1159 int ret;
1160 struct nl80211_bss_info_arg arg;
1161
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001162 msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
Jouni Malinen87fd2792011-05-16 18:35:42 +03001163 os_memset(&arg, 0, sizeof(arg));
Jouni Malinen87fd2792011-05-16 18:35:42 +03001164 arg.drv = drv;
1165 ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
Jouni Malinen87fd2792011-05-16 18:35:42 +03001166 if (ret == 0) {
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07001167 unsigned int freq = drv->nlmode == NL80211_IFTYPE_ADHOC ?
1168 arg.ibss_freq : arg.assoc_freq;
Jouni Malinen87fd2792011-05-16 18:35:42 +03001169 wpa_printf(MSG_DEBUG, "nl80211: Operating frequency for the "
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07001170 "associated BSS from scan results: %u MHz", freq);
1171 if (freq)
1172 drv->assoc_freq = freq;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001173 return drv->assoc_freq;
Jouni Malinen87fd2792011-05-16 18:35:42 +03001174 }
1175 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
1176 "(%s)", ret, strerror(-ret));
Jouni Malinen87fd2792011-05-16 18:35:42 +03001177 return drv->assoc_freq;
1178}
1179
1180
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001181static int get_link_signal(struct nl_msg *msg, void *arg)
1182{
1183 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1184 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1185 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
1186 static struct nla_policy policy[NL80211_STA_INFO_MAX + 1] = {
1187 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001188 [NL80211_STA_INFO_SIGNAL_AVG] = { .type = NLA_U8 },
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07001189 [NL80211_STA_INFO_BEACON_SIGNAL_AVG] = { .type = NLA_U8 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001190 };
1191 struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
1192 static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
1193 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
1194 [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
1195 [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
1196 [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
1197 };
1198 struct wpa_signal_info *sig_change = arg;
1199
1200 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1201 genlmsg_attrlen(gnlh, 0), NULL);
1202 if (!tb[NL80211_ATTR_STA_INFO] ||
1203 nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
1204 tb[NL80211_ATTR_STA_INFO], policy))
1205 return NL_SKIP;
1206 if (!sinfo[NL80211_STA_INFO_SIGNAL])
1207 return NL_SKIP;
1208
1209 sig_change->current_signal =
1210 (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
1211
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001212 if (sinfo[NL80211_STA_INFO_SIGNAL_AVG])
1213 sig_change->avg_signal =
1214 (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL_AVG]);
1215 else
1216 sig_change->avg_signal = 0;
1217
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07001218 if (sinfo[NL80211_STA_INFO_BEACON_SIGNAL_AVG])
1219 sig_change->avg_beacon_signal =
1220 (s8)
1221 nla_get_u8(sinfo[NL80211_STA_INFO_BEACON_SIGNAL_AVG]);
1222 else
1223 sig_change->avg_beacon_signal = 0;
1224
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001225 if (sinfo[NL80211_STA_INFO_TX_BITRATE]) {
1226 if (nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
1227 sinfo[NL80211_STA_INFO_TX_BITRATE],
1228 rate_policy)) {
1229 sig_change->current_txrate = 0;
1230 } else {
1231 if (rinfo[NL80211_RATE_INFO_BITRATE]) {
1232 sig_change->current_txrate =
1233 nla_get_u16(rinfo[
1234 NL80211_RATE_INFO_BITRATE]) * 100;
1235 }
1236 }
1237 }
1238
1239 return NL_SKIP;
1240}
1241
1242
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001243int nl80211_get_link_signal(struct wpa_driver_nl80211_data *drv,
1244 struct wpa_signal_info *sig)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001245{
1246 struct nl_msg *msg;
1247
1248 sig->current_signal = -9999;
1249 sig->current_txrate = 0;
1250
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001251 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_GET_STATION)) ||
1252 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid)) {
1253 nlmsg_free(msg);
1254 return -ENOBUFS;
1255 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001256
1257 return send_and_recv_msgs(drv, msg, get_link_signal, sig);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001258}
1259
1260
1261static int get_link_noise(struct nl_msg *msg, void *arg)
1262{
1263 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1264 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1265 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
1266 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
1267 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
1268 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
1269 };
1270 struct wpa_signal_info *sig_change = arg;
1271
1272 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1273 genlmsg_attrlen(gnlh, 0), NULL);
1274
1275 if (!tb[NL80211_ATTR_SURVEY_INFO]) {
1276 wpa_printf(MSG_DEBUG, "nl80211: survey data missing!");
1277 return NL_SKIP;
1278 }
1279
1280 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
1281 tb[NL80211_ATTR_SURVEY_INFO],
1282 survey_policy)) {
1283 wpa_printf(MSG_DEBUG, "nl80211: failed to parse nested "
1284 "attributes!");
1285 return NL_SKIP;
1286 }
1287
1288 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
1289 return NL_SKIP;
1290
1291 if (nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
1292 sig_change->frequency)
1293 return NL_SKIP;
1294
1295 if (!sinfo[NL80211_SURVEY_INFO_NOISE])
1296 return NL_SKIP;
1297
1298 sig_change->current_noise =
1299 (s8) nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
1300
1301 return NL_SKIP;
1302}
1303
1304
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001305int nl80211_get_link_noise(struct wpa_driver_nl80211_data *drv,
1306 struct wpa_signal_info *sig_change)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001307{
1308 struct nl_msg *msg;
1309
1310 sig_change->current_noise = 9999;
1311 sig_change->frequency = drv->assoc_freq;
1312
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001313 msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001314 return send_and_recv_msgs(drv, msg, get_link_noise, sig_change);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001315}
1316
1317
1318static void wpa_driver_nl80211_event_receive(int sock, void *eloop_ctx,
1319 void *handle)
1320{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001321 struct nl_cb *cb = eloop_ctx;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001322 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001323
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001324 wpa_printf(MSG_MSGDUMP, "nl80211: Event message available");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001325
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001326 res = nl_recvmsgs(handle, cb);
Dmitry Shmidt71757432014-06-02 13:50:35 -07001327 if (res < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001328 wpa_printf(MSG_INFO, "nl80211: %s->nl_recvmsgs failed: %d",
1329 __func__, res);
1330 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001331}
1332
1333
1334/**
1335 * wpa_driver_nl80211_set_country - ask nl80211 to set the regulatory domain
1336 * @priv: driver_nl80211 private data
1337 * @alpha2_arg: country to which to switch to
1338 * Returns: 0 on success, -1 on failure
1339 *
1340 * This asks nl80211 to set the regulatory domain for given
1341 * country ISO / IEC alpha2.
1342 */
1343static int wpa_driver_nl80211_set_country(void *priv, const char *alpha2_arg)
1344{
1345 struct i802_bss *bss = priv;
1346 struct wpa_driver_nl80211_data *drv = bss->drv;
1347 char alpha2[3];
1348 struct nl_msg *msg;
1349
1350 msg = nlmsg_alloc();
1351 if (!msg)
1352 return -ENOMEM;
1353
1354 alpha2[0] = alpha2_arg[0];
1355 alpha2[1] = alpha2_arg[1];
1356 alpha2[2] = '\0';
1357
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001358 if (!nl80211_cmd(drv, msg, 0, NL80211_CMD_REQ_SET_REG) ||
1359 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, alpha2)) {
1360 nlmsg_free(msg);
1361 return -EINVAL;
1362 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001363 if (send_and_recv_msgs(drv, msg, NULL, NULL))
1364 return -EINVAL;
1365 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001366}
1367
1368
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001369static int nl80211_get_country(struct nl_msg *msg, void *arg)
1370{
1371 char *alpha2 = arg;
1372 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
1373 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1374
1375 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1376 genlmsg_attrlen(gnlh, 0), NULL);
1377 if (!tb_msg[NL80211_ATTR_REG_ALPHA2]) {
1378 wpa_printf(MSG_DEBUG, "nl80211: No country information available");
1379 return NL_SKIP;
1380 }
1381 os_strlcpy(alpha2, nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]), 3);
1382 return NL_SKIP;
1383}
1384
1385
1386static int wpa_driver_nl80211_get_country(void *priv, char *alpha2)
1387{
1388 struct i802_bss *bss = priv;
1389 struct wpa_driver_nl80211_data *drv = bss->drv;
1390 struct nl_msg *msg;
1391 int ret;
1392
1393 msg = nlmsg_alloc();
1394 if (!msg)
1395 return -ENOMEM;
1396
1397 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG);
1398 alpha2[0] = '\0';
1399 ret = send_and_recv_msgs(drv, msg, nl80211_get_country, alpha2);
1400 if (!alpha2[0])
1401 ret = -1;
1402
1403 return ret;
1404}
1405
1406
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001407static int wpa_driver_nl80211_init_nl_global(struct nl80211_global *global)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001408{
1409 int ret;
1410
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001411 global->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
1412 if (global->nl_cb == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001413 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
1414 "callbacks");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001415 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001416 }
1417
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001418 global->nl = nl_create_handle(global->nl_cb, "nl");
1419 if (global->nl == NULL)
1420 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001421
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001422 global->nl80211_id = genl_ctrl_resolve(global->nl, "nl80211");
1423 if (global->nl80211_id < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001424 wpa_printf(MSG_ERROR, "nl80211: 'nl80211' generic netlink not "
1425 "found");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001426 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001427 }
1428
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001429 global->nl_event = nl_create_handle(global->nl_cb, "event");
1430 if (global->nl_event == NULL)
1431 goto err;
1432
1433 ret = nl_get_multicast_id(global, "nl80211", "scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001434 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001435 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001436 if (ret < 0) {
1437 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
1438 "membership for scan events: %d (%s)",
1439 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001440 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001441 }
1442
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001443 ret = nl_get_multicast_id(global, "nl80211", "mlme");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001444 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001445 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001446 if (ret < 0) {
1447 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
1448 "membership for mlme events: %d (%s)",
1449 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001450 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001451 }
1452
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001453 ret = nl_get_multicast_id(global, "nl80211", "regulatory");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001454 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001455 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001456 if (ret < 0) {
1457 wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
1458 "membership for regulatory events: %d (%s)",
1459 ret, strerror(-ret));
1460 /* Continue without regulatory events */
1461 }
1462
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001463 ret = nl_get_multicast_id(global, "nl80211", "vendor");
1464 if (ret >= 0)
1465 ret = nl_socket_add_membership(global->nl_event, ret);
1466 if (ret < 0) {
1467 wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
1468 "membership for vendor events: %d (%s)",
1469 ret, strerror(-ret));
1470 /* Continue without vendor events */
1471 }
1472
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001473 nl_cb_set(global->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
1474 no_seq_check, NULL);
1475 nl_cb_set(global->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
1476 process_global_event, global);
1477
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001478 nl80211_register_eloop_read(&global->nl_event,
1479 wpa_driver_nl80211_event_receive,
1480 global->nl_cb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001481
1482 return 0;
1483
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001484err:
1485 nl_destroy_handles(&global->nl_event);
1486 nl_destroy_handles(&global->nl);
1487 nl_cb_put(global->nl_cb);
1488 global->nl_cb = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001489 return -1;
1490}
1491
1492
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001493static void nl80211_check_global(struct nl80211_global *global)
1494{
1495 struct nl_handle *handle;
1496 const char *groups[] = { "scan", "mlme", "regulatory", "vendor", NULL };
1497 int ret;
1498 unsigned int i;
1499
1500 /*
1501 * Try to re-add memberships to handle case of cfg80211 getting reloaded
1502 * and all registration having been cleared.
1503 */
1504 handle = (void *) (((intptr_t) global->nl_event) ^
1505 ELOOP_SOCKET_INVALID);
1506
1507 for (i = 0; groups[i]; i++) {
1508 ret = nl_get_multicast_id(global, "nl80211", groups[i]);
1509 if (ret >= 0)
1510 ret = nl_socket_add_membership(handle, ret);
1511 if (ret < 0) {
1512 wpa_printf(MSG_INFO,
1513 "nl80211: Could not re-add multicast membership for %s events: %d (%s)",
1514 groups[i], ret, strerror(-ret));
1515 }
1516 }
1517}
1518
1519
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001520static void wpa_driver_nl80211_rfkill_blocked(void *ctx)
1521{
1522 wpa_printf(MSG_DEBUG, "nl80211: RFKILL blocked");
1523 /*
1524 * This may be for any interface; use ifdown event to disable
1525 * interface.
1526 */
1527}
1528
1529
1530static void wpa_driver_nl80211_rfkill_unblocked(void *ctx)
1531{
1532 struct wpa_driver_nl80211_data *drv = ctx;
1533 wpa_printf(MSG_DEBUG, "nl80211: RFKILL unblocked");
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001534 if (i802_set_iface_flags(drv->first_bss, 1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001535 wpa_printf(MSG_DEBUG, "nl80211: Could not set interface UP "
1536 "after rfkill unblock");
1537 return;
1538 }
1539 /* rtnetlink ifup handler will report interface as enabled */
1540}
1541
1542
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001543static void wpa_driver_nl80211_handle_eapol_tx_status(int sock,
1544 void *eloop_ctx,
1545 void *handle)
1546{
1547 struct wpa_driver_nl80211_data *drv = eloop_ctx;
1548 u8 data[2048];
1549 struct msghdr msg;
1550 struct iovec entry;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001551 u8 control[512];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001552 struct cmsghdr *cmsg;
1553 int res, found_ee = 0, found_wifi = 0, acked = 0;
1554 union wpa_event_data event;
1555
1556 memset(&msg, 0, sizeof(msg));
1557 msg.msg_iov = &entry;
1558 msg.msg_iovlen = 1;
1559 entry.iov_base = data;
1560 entry.iov_len = sizeof(data);
1561 msg.msg_control = &control;
1562 msg.msg_controllen = sizeof(control);
1563
1564 res = recvmsg(sock, &msg, MSG_ERRQUEUE);
1565 /* if error or not fitting 802.3 header, return */
1566 if (res < 14)
1567 return;
1568
1569 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg))
1570 {
1571 if (cmsg->cmsg_level == SOL_SOCKET &&
1572 cmsg->cmsg_type == SCM_WIFI_STATUS) {
1573 int *ack;
1574
1575 found_wifi = 1;
1576 ack = (void *)CMSG_DATA(cmsg);
1577 acked = *ack;
1578 }
1579
1580 if (cmsg->cmsg_level == SOL_PACKET &&
1581 cmsg->cmsg_type == PACKET_TX_TIMESTAMP) {
1582 struct sock_extended_err *err =
1583 (struct sock_extended_err *)CMSG_DATA(cmsg);
1584
1585 if (err->ee_origin == SO_EE_ORIGIN_TXSTATUS)
1586 found_ee = 1;
1587 }
1588 }
1589
1590 if (!found_ee || !found_wifi)
1591 return;
1592
1593 memset(&event, 0, sizeof(event));
1594 event.eapol_tx_status.dst = data;
1595 event.eapol_tx_status.data = data + 14;
1596 event.eapol_tx_status.data_len = res - 14;
1597 event.eapol_tx_status.ack = acked;
1598 wpa_supplicant_event(drv->ctx, EVENT_EAPOL_TX_STATUS, &event);
1599}
1600
1601
1602static int nl80211_init_bss(struct i802_bss *bss)
1603{
1604 bss->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
1605 if (!bss->nl_cb)
1606 return -1;
1607
1608 nl_cb_set(bss->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
1609 no_seq_check, NULL);
1610 nl_cb_set(bss->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
1611 process_bss_event, bss);
1612
1613 return 0;
1614}
1615
1616
1617static void nl80211_destroy_bss(struct i802_bss *bss)
1618{
1619 nl_cb_put(bss->nl_cb);
1620 bss->nl_cb = NULL;
1621}
1622
1623
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001624static void * wpa_driver_nl80211_drv_init(void *ctx, const char *ifname,
1625 void *global_priv, int hostapd,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001626 const u8 *set_addr,
1627 const char *driver_params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001628{
1629 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001630 struct rfkill_config *rcfg;
1631 struct i802_bss *bss;
1632
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001633 if (global_priv == NULL)
1634 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001635 drv = os_zalloc(sizeof(*drv));
1636 if (drv == NULL)
1637 return NULL;
1638 drv->global = global_priv;
1639 drv->ctx = ctx;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001640 drv->hostapd = !!hostapd;
1641 drv->eapol_sock = -1;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001642
1643 /*
1644 * There is no driver capability flag for this, so assume it is
1645 * supported and disable this on first attempt to use if the driver
1646 * rejects the command due to missing support.
1647 */
1648 drv->set_rekey_offload = 1;
1649
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001650 drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int);
1651 drv->if_indices = drv->default_if_indices;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001652
1653 drv->first_bss = os_zalloc(sizeof(*drv->first_bss));
1654 if (!drv->first_bss) {
1655 os_free(drv);
1656 return NULL;
1657 }
1658 bss = drv->first_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001659 bss->drv = drv;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001660 bss->ctx = ctx;
1661
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001662 os_strlcpy(bss->ifname, ifname, sizeof(bss->ifname));
1663 drv->monitor_ifidx = -1;
1664 drv->monitor_sock = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001665 drv->eapol_tx_sock = -1;
1666 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001667
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001668 if (nl80211_init_bss(bss))
1669 goto failed;
1670
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001671 rcfg = os_zalloc(sizeof(*rcfg));
1672 if (rcfg == NULL)
1673 goto failed;
1674 rcfg->ctx = drv;
1675 os_strlcpy(rcfg->ifname, ifname, sizeof(rcfg->ifname));
1676 rcfg->blocked_cb = wpa_driver_nl80211_rfkill_blocked;
1677 rcfg->unblocked_cb = wpa_driver_nl80211_rfkill_unblocked;
1678 drv->rfkill = rfkill_init(rcfg);
1679 if (drv->rfkill == NULL) {
1680 wpa_printf(MSG_DEBUG, "nl80211: RFKILL status not available");
1681 os_free(rcfg);
1682 }
1683
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001684 if (linux_iface_up(drv->global->ioctl_sock, ifname) > 0)
1685 drv->start_iface_up = 1;
1686
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001687 if (wpa_driver_nl80211_finish_drv_init(drv, set_addr, 1, driver_params))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001688 goto failed;
1689
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001690 drv->eapol_tx_sock = socket(PF_PACKET, SOCK_DGRAM, 0);
1691 if (drv->eapol_tx_sock < 0)
1692 goto failed;
1693
1694 if (drv->data_tx_status) {
1695 int enabled = 1;
1696
1697 if (setsockopt(drv->eapol_tx_sock, SOL_SOCKET, SO_WIFI_STATUS,
1698 &enabled, sizeof(enabled)) < 0) {
1699 wpa_printf(MSG_DEBUG,
1700 "nl80211: wifi status sockopt failed\n");
1701 drv->data_tx_status = 0;
1702 if (!drv->use_monitor)
1703 drv->capa.flags &=
1704 ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
1705 } else {
1706 eloop_register_read_sock(drv->eapol_tx_sock,
1707 wpa_driver_nl80211_handle_eapol_tx_status,
1708 drv, NULL);
1709 }
1710 }
1711
1712 if (drv->global) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001713 nl80211_check_global(drv->global);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001714 dl_list_add(&drv->global->interfaces, &drv->list);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001715 drv->in_interface_list = 1;
1716 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001717
1718 return bss;
1719
1720failed:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001721 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001722 return NULL;
1723}
1724
1725
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001726/**
1727 * wpa_driver_nl80211_init - Initialize nl80211 driver interface
1728 * @ctx: context to be used when calling wpa_supplicant functions,
1729 * e.g., wpa_supplicant_event()
1730 * @ifname: interface name, e.g., wlan0
1731 * @global_priv: private driver global data from global_init()
1732 * Returns: Pointer to private data, %NULL on failure
1733 */
1734static void * wpa_driver_nl80211_init(void *ctx, const char *ifname,
1735 void *global_priv)
1736{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001737 return wpa_driver_nl80211_drv_init(ctx, ifname, global_priv, 0, NULL,
1738 NULL);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001739}
1740
1741
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001742static int nl80211_register_frame(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001743 struct nl_handle *nl_handle,
1744 u16 type, const u8 *match, size_t match_len)
1745{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001746 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001747 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001748 int ret;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001749 char buf[30];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001750
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001751 buf[0] = '\0';
1752 wpa_snprintf_hex(buf, sizeof(buf), match, match_len);
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07001753 wpa_printf(MSG_DEBUG, "nl80211: Register frame type=0x%x (%s) nl_handle=%p match=%s",
1754 type, fc2str(type), nl_handle, buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001755
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001756 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_REGISTER_ACTION)) ||
1757 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE, type) ||
1758 nla_put(msg, NL80211_ATTR_FRAME_MATCH, match_len, match)) {
1759 nlmsg_free(msg);
1760 return -1;
1761 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001762
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001763 ret = send_and_recv(drv->global, nl_handle, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001764 if (ret) {
1765 wpa_printf(MSG_DEBUG, "nl80211: Register frame command "
1766 "failed (type=%u): ret=%d (%s)",
1767 type, ret, strerror(-ret));
1768 wpa_hexdump(MSG_DEBUG, "nl80211: Register frame match",
1769 match, match_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001770 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001771 return ret;
1772}
1773
1774
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001775static int nl80211_alloc_mgmt_handle(struct i802_bss *bss)
1776{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001777 if (bss->nl_mgmt) {
1778 wpa_printf(MSG_DEBUG, "nl80211: Mgmt reporting "
1779 "already on! (nl_mgmt=%p)", bss->nl_mgmt);
1780 return -1;
1781 }
1782
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001783 bss->nl_mgmt = nl_create_handle(bss->nl_cb, "mgmt");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001784 if (bss->nl_mgmt == NULL)
1785 return -1;
1786
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001787 return 0;
1788}
1789
1790
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001791static void nl80211_mgmt_handle_register_eloop(struct i802_bss *bss)
1792{
1793 nl80211_register_eloop_read(&bss->nl_mgmt,
1794 wpa_driver_nl80211_event_receive,
1795 bss->nl_cb);
1796}
1797
1798
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001799static int nl80211_register_action_frame(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001800 const u8 *match, size_t match_len)
1801{
1802 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_ACTION << 4);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001803 return nl80211_register_frame(bss, bss->nl_mgmt,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001804 type, match, match_len);
1805}
1806
1807
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001808static int nl80211_mgmt_subscribe_non_ap(struct i802_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001809{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001810 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001811 int ret = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001812
1813 if (nl80211_alloc_mgmt_handle(bss))
1814 return -1;
1815 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with non-AP "
1816 "handle %p", bss->nl_mgmt);
1817
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001818 if (drv->nlmode == NL80211_IFTYPE_ADHOC) {
1819 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_AUTH << 4);
1820
1821 /* register for any AUTH message */
1822 nl80211_register_frame(bss, bss->nl_mgmt, type, NULL, 0);
1823 }
1824
Dmitry Shmidt051af732013-10-22 13:52:46 -07001825#ifdef CONFIG_INTERWORKING
1826 /* QoS Map Configure */
1827 if (nl80211_register_action_frame(bss, (u8 *) "\x01\x04", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001828 ret = -1;
Dmitry Shmidt051af732013-10-22 13:52:46 -07001829#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001830#if defined(CONFIG_P2P) || defined(CONFIG_INTERWORKING)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001831 /* GAS Initial Request */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001832 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0a", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001833 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001834 /* GAS Initial Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001835 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0b", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001836 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001837 /* GAS Comeback Request */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001838 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0c", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001839 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001840 /* GAS Comeback Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001841 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0d", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001842 ret = -1;
Dmitry Shmidt18463232014-01-24 12:29:41 -08001843 /* Protected GAS Initial Request */
1844 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0a", 2) < 0)
1845 ret = -1;
1846 /* Protected GAS Initial Response */
1847 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0b", 2) < 0)
1848 ret = -1;
1849 /* Protected GAS Comeback Request */
1850 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0c", 2) < 0)
1851 ret = -1;
1852 /* Protected GAS Comeback Response */
1853 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0d", 2) < 0)
1854 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001855#endif /* CONFIG_P2P || CONFIG_INTERWORKING */
1856#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001857 /* P2P Public Action */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001858 if (nl80211_register_action_frame(bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001859 (u8 *) "\x04\x09\x50\x6f\x9a\x09",
1860 6) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001861 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001862 /* P2P Action */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001863 if (nl80211_register_action_frame(bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001864 (u8 *) "\x7f\x50\x6f\x9a\x09",
1865 5) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001866 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001867#endif /* CONFIG_P2P */
1868#ifdef CONFIG_IEEE80211W
1869 /* SA Query Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001870 if (nl80211_register_action_frame(bss, (u8 *) "\x08\x01", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001871 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001872#endif /* CONFIG_IEEE80211W */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001873#ifdef CONFIG_TDLS
1874 if ((drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT)) {
1875 /* TDLS Discovery Response */
1876 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0e", 2) <
1877 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001878 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001879 }
1880#endif /* CONFIG_TDLS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001881
1882 /* FT Action frames */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001883 if (nl80211_register_action_frame(bss, (u8 *) "\x06", 1) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001884 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001885 else
1886 drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_FT |
1887 WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK;
1888
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001889 /* WNM - BSS Transition Management Request */
1890 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x07", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001891 ret = -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001892 /* WNM-Sleep Mode Response */
1893 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x11", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001894 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001895
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001896#ifdef CONFIG_HS20
1897 /* WNM-Notification */
1898 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x1a", 2) < 0)
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001899 ret = -1;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001900#endif /* CONFIG_HS20 */
1901
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001902 /* WMM-AC ADDTS Response */
1903 if (nl80211_register_action_frame(bss, (u8 *) "\x11\x01", 2) < 0)
1904 ret = -1;
1905
1906 /* WMM-AC DELTS */
1907 if (nl80211_register_action_frame(bss, (u8 *) "\x11\x02", 2) < 0)
1908 ret = -1;
1909
1910 /* Radio Measurement - Neighbor Report Response */
1911 if (nl80211_register_action_frame(bss, (u8 *) "\x05\x05", 2) < 0)
1912 ret = -1;
1913
1914 /* Radio Measurement - Link Measurement Request */
1915 if ((drv->capa.rrm_flags & WPA_DRIVER_FLAGS_TX_POWER_INSERTION) &&
1916 (nl80211_register_action_frame(bss, (u8 *) "\x05\x02", 2) < 0))
1917 ret = -1;
1918
1919 nl80211_mgmt_handle_register_eloop(bss);
1920
1921 return ret;
1922}
1923
1924
1925static int nl80211_mgmt_subscribe_mesh(struct i802_bss *bss)
1926{
1927 int ret = 0;
1928
1929 if (nl80211_alloc_mgmt_handle(bss))
1930 return -1;
1931
1932 wpa_printf(MSG_DEBUG,
1933 "nl80211: Subscribe to mgmt frames with mesh handle %p",
1934 bss->nl_mgmt);
1935
1936 /* Auth frames for mesh SAE */
1937 if (nl80211_register_frame(bss, bss->nl_mgmt,
1938 (WLAN_FC_TYPE_MGMT << 2) |
1939 (WLAN_FC_STYPE_AUTH << 4),
1940 NULL, 0) < 0)
1941 ret = -1;
1942
1943 /* Mesh peering open */
1944 if (nl80211_register_action_frame(bss, (u8 *) "\x0f\x01", 2) < 0)
1945 ret = -1;
1946 /* Mesh peering confirm */
1947 if (nl80211_register_action_frame(bss, (u8 *) "\x0f\x02", 2) < 0)
1948 ret = -1;
1949 /* Mesh peering close */
1950 if (nl80211_register_action_frame(bss, (u8 *) "\x0f\x03", 2) < 0)
1951 ret = -1;
1952
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001953 nl80211_mgmt_handle_register_eloop(bss);
1954
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001955 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001956}
1957
1958
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001959static int nl80211_register_spurious_class3(struct i802_bss *bss)
1960{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001961 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001962 int ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001963
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001964 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_UNEXPECTED_FRAME);
1965 ret = send_and_recv(bss->drv->global, bss->nl_mgmt, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001966 if (ret) {
1967 wpa_printf(MSG_DEBUG, "nl80211: Register spurious class3 "
1968 "failed: ret=%d (%s)",
1969 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001970 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001971 return ret;
1972}
1973
1974
1975static int nl80211_mgmt_subscribe_ap(struct i802_bss *bss)
1976{
1977 static const int stypes[] = {
1978 WLAN_FC_STYPE_AUTH,
1979 WLAN_FC_STYPE_ASSOC_REQ,
1980 WLAN_FC_STYPE_REASSOC_REQ,
1981 WLAN_FC_STYPE_DISASSOC,
1982 WLAN_FC_STYPE_DEAUTH,
1983 WLAN_FC_STYPE_ACTION,
1984 WLAN_FC_STYPE_PROBE_REQ,
1985/* Beacon doesn't work as mac80211 doesn't currently allow
1986 * it, but it wouldn't really be the right thing anyway as
1987 * it isn't per interface ... maybe just dump the scan
1988 * results periodically for OLBC?
1989 */
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07001990 /* WLAN_FC_STYPE_BEACON, */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001991 };
1992 unsigned int i;
1993
1994 if (nl80211_alloc_mgmt_handle(bss))
1995 return -1;
1996 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
1997 "handle %p", bss->nl_mgmt);
1998
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001999 for (i = 0; i < ARRAY_SIZE(stypes); i++) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002000 if (nl80211_register_frame(bss, bss->nl_mgmt,
2001 (WLAN_FC_TYPE_MGMT << 2) |
2002 (stypes[i] << 4),
2003 NULL, 0) < 0) {
2004 goto out_err;
2005 }
2006 }
2007
2008 if (nl80211_register_spurious_class3(bss))
2009 goto out_err;
2010
2011 if (nl80211_get_wiphy_data_ap(bss) == NULL)
2012 goto out_err;
2013
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002014 nl80211_mgmt_handle_register_eloop(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002015 return 0;
2016
2017out_err:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002018 nl_destroy_handles(&bss->nl_mgmt);
2019 return -1;
2020}
2021
2022
2023static int nl80211_mgmt_subscribe_ap_dev_sme(struct i802_bss *bss)
2024{
2025 if (nl80211_alloc_mgmt_handle(bss))
2026 return -1;
2027 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
2028 "handle %p (device SME)", bss->nl_mgmt);
2029
2030 if (nl80211_register_frame(bss, bss->nl_mgmt,
2031 (WLAN_FC_TYPE_MGMT << 2) |
2032 (WLAN_FC_STYPE_ACTION << 4),
2033 NULL, 0) < 0)
2034 goto out_err;
2035
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002036 nl80211_mgmt_handle_register_eloop(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002037 return 0;
2038
2039out_err:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002040 nl_destroy_handles(&bss->nl_mgmt);
2041 return -1;
2042}
2043
2044
2045static void nl80211_mgmt_unsubscribe(struct i802_bss *bss, const char *reason)
2046{
2047 if (bss->nl_mgmt == NULL)
2048 return;
2049 wpa_printf(MSG_DEBUG, "nl80211: Unsubscribe mgmt frames handle %p "
2050 "(%s)", bss->nl_mgmt, reason);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002051 nl80211_destroy_eloop_handle(&bss->nl_mgmt);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002052
2053 nl80211_put_wiphy_data_ap(bss);
2054}
2055
2056
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002057static void wpa_driver_nl80211_send_rfkill(void *eloop_ctx, void *timeout_ctx)
2058{
2059 wpa_supplicant_event(timeout_ctx, EVENT_INTERFACE_DISABLED, NULL);
2060}
2061
2062
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002063static void nl80211_del_p2pdev(struct i802_bss *bss)
2064{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002065 struct nl_msg *msg;
2066 int ret;
2067
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002068 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_DEL_INTERFACE);
2069 ret = send_and_recv_msgs(bss->drv, msg, NULL, NULL);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002070
2071 wpa_printf(MSG_DEBUG, "nl80211: Delete P2P Device %s (0x%llx): %s",
2072 bss->ifname, (long long unsigned int) bss->wdev_id,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002073 strerror(-ret));
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002074}
2075
2076
2077static int nl80211_set_p2pdev(struct i802_bss *bss, int start)
2078{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002079 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002080 int ret;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002081
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002082 msg = nl80211_cmd_msg(bss, 0, start ? NL80211_CMD_START_P2P_DEVICE :
2083 NL80211_CMD_STOP_P2P_DEVICE);
2084 ret = send_and_recv_msgs(bss->drv, msg, NULL, NULL);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002085
2086 wpa_printf(MSG_DEBUG, "nl80211: %s P2P Device %s (0x%llx): %s",
2087 start ? "Start" : "Stop",
2088 bss->ifname, (long long unsigned int) bss->wdev_id,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002089 strerror(-ret));
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002090 return ret;
2091}
2092
2093
2094static int i802_set_iface_flags(struct i802_bss *bss, int up)
2095{
2096 enum nl80211_iftype nlmode;
2097
2098 nlmode = nl80211_get_ifmode(bss);
2099 if (nlmode != NL80211_IFTYPE_P2P_DEVICE) {
2100 return linux_set_iface_flags(bss->drv->global->ioctl_sock,
2101 bss->ifname, up);
2102 }
2103
2104 /* P2P Device has start/stop which is equivalent */
2105 return nl80211_set_p2pdev(bss, up);
2106}
2107
2108
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002109#ifdef CONFIG_TESTING_OPTIONS
2110static int qca_vendor_test_cmd_handler(struct nl_msg *msg, void *arg)
2111{
2112 /* struct wpa_driver_nl80211_data *drv = arg; */
2113 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2114 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2115
2116
2117 wpa_printf(MSG_DEBUG,
2118 "nl80211: QCA vendor test command response received");
2119
2120 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2121 genlmsg_attrlen(gnlh, 0), NULL);
2122 if (!tb[NL80211_ATTR_VENDOR_DATA]) {
2123 wpa_printf(MSG_DEBUG, "nl80211: No vendor data attribute");
2124 return NL_SKIP;
2125 }
2126
2127 wpa_hexdump(MSG_DEBUG,
2128 "nl80211: Received QCA vendor test command response",
2129 nla_data(tb[NL80211_ATTR_VENDOR_DATA]),
2130 nla_len(tb[NL80211_ATTR_VENDOR_DATA]));
2131
2132 return NL_SKIP;
2133}
2134#endif /* CONFIG_TESTING_OPTIONS */
2135
2136
2137static void qca_vendor_test(struct wpa_driver_nl80211_data *drv)
2138{
2139#ifdef CONFIG_TESTING_OPTIONS
2140 struct nl_msg *msg;
2141 struct nlattr *params;
2142 int ret;
2143
2144 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
2145 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
2146 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
2147 QCA_NL80211_VENDOR_SUBCMD_TEST) ||
2148 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
2149 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_TEST, 123)) {
2150 nlmsg_free(msg);
2151 return;
2152 }
2153 nla_nest_end(msg, params);
2154
2155 ret = send_and_recv_msgs(drv, msg, qca_vendor_test_cmd_handler, drv);
2156 wpa_printf(MSG_DEBUG,
2157 "nl80211: QCA vendor test command returned %d (%s)",
2158 ret, strerror(-ret));
2159#endif /* CONFIG_TESTING_OPTIONS */
2160}
2161
2162
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002163static int
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002164wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002165 const u8 *set_addr, int first,
2166 const char *driver_params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002167{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002168 struct i802_bss *bss = drv->first_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002169 int send_rfkill_event = 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002170 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002171
2172 drv->ifindex = if_nametoindex(bss->ifname);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002173 bss->ifindex = drv->ifindex;
2174 bss->wdev_id = drv->global->if_add_wdevid;
2175 bss->wdev_id_set = drv->global->if_add_wdevid_set;
2176
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002177 bss->if_dynamic = drv->ifindex == drv->global->if_add_ifindex;
2178 bss->if_dynamic = bss->if_dynamic || drv->global->if_add_wdevid_set;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002179 drv->global->if_add_wdevid_set = 0;
2180
Dmitry Shmidt03658832014-08-13 11:03:49 -07002181 if (!bss->if_dynamic && nl80211_get_ifmode(bss) == NL80211_IFTYPE_AP)
2182 bss->static_ap = 1;
2183
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002184 if (wpa_driver_nl80211_capa(drv))
2185 return -1;
2186
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002187 if (driver_params && nl80211_set_param(bss, driver_params) < 0)
2188 return -1;
2189
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002190 wpa_printf(MSG_DEBUG, "nl80211: interface %s in phy %s",
2191 bss->ifname, drv->phyname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002192
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002193 if (set_addr &&
2194 (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0) ||
2195 linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
2196 set_addr)))
2197 return -1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002198
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002199 if (first && nl80211_get_ifmode(bss) == NL80211_IFTYPE_AP)
2200 drv->start_mode_ap = 1;
2201
Dmitry Shmidt03658832014-08-13 11:03:49 -07002202 if (drv->hostapd || bss->static_ap)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002203 nlmode = NL80211_IFTYPE_AP;
2204 else if (bss->if_dynamic)
2205 nlmode = nl80211_get_ifmode(bss);
2206 else
2207 nlmode = NL80211_IFTYPE_STATION;
2208
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002209 if (wpa_driver_nl80211_set_mode(bss, nlmode) < 0) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002210 wpa_printf(MSG_ERROR, "nl80211: Could not configure driver mode");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002211 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002212 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002213
Dmitry Shmidt98660862014-03-11 17:26:21 -07002214 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002215 nl80211_get_macaddr(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002216
Dmitry Shmidt98660862014-03-11 17:26:21 -07002217 if (!rfkill_is_blocked(drv->rfkill)) {
2218 int ret = i802_set_iface_flags(bss, 1);
2219 if (ret) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002220 wpa_printf(MSG_ERROR, "nl80211: Could not set "
2221 "interface '%s' UP", bss->ifname);
Dmitry Shmidt98660862014-03-11 17:26:21 -07002222 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002223 }
Dmitry Shmidt98660862014-03-11 17:26:21 -07002224 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
2225 return ret;
2226 } else {
2227 wpa_printf(MSG_DEBUG, "nl80211: Could not yet enable "
2228 "interface '%s' due to rfkill", bss->ifname);
2229 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
2230 return 0;
2231 drv->if_disabled = 1;
2232 send_rfkill_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002233 }
2234
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002235 if (!drv->hostapd)
2236 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
2237 1, IF_OPER_DORMANT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002238
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002239 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
2240 bss->addr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002241 return -1;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002242 os_memcpy(drv->perm_addr, bss->addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002243
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002244 if (send_rfkill_event) {
2245 eloop_register_timeout(0, 0, wpa_driver_nl80211_send_rfkill,
2246 drv, drv->ctx);
2247 }
2248
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002249 if (drv->vendor_cmd_test_avail)
2250 qca_vendor_test(drv);
2251
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002252 return 0;
2253}
2254
2255
2256static int wpa_driver_nl80211_del_beacon(struct wpa_driver_nl80211_data *drv)
2257{
2258 struct nl_msg *msg;
2259
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002260 wpa_printf(MSG_DEBUG, "nl80211: Remove beacon (ifindex=%d)",
2261 drv->ifindex);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002262 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_DEL_BEACON);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002263 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002264}
2265
2266
2267/**
2268 * wpa_driver_nl80211_deinit - Deinitialize nl80211 driver interface
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002269 * @bss: Pointer to private nl80211 data from wpa_driver_nl80211_init()
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002270 *
2271 * Shut down driver interface and processing of driver events. Free
2272 * private data buffer if one was allocated in wpa_driver_nl80211_init().
2273 */
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002274static void wpa_driver_nl80211_deinit(struct i802_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002275{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002276 struct wpa_driver_nl80211_data *drv = bss->drv;
2277
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002278 wpa_printf(MSG_INFO, "nl80211: deinit ifname=%s disabled_11b_rates=%d",
2279 bss->ifname, drv->disabled_11b_rates);
2280
Dmitry Shmidt04949592012-07-19 12:16:46 -07002281 bss->in_deinit = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002282 if (drv->data_tx_status)
2283 eloop_unregister_read_sock(drv->eapol_tx_sock);
2284 if (drv->eapol_tx_sock >= 0)
2285 close(drv->eapol_tx_sock);
2286
2287 if (bss->nl_preq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002288 wpa_driver_nl80211_probe_req_report(bss, 0);
2289 if (bss->added_if_into_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002290 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
2291 bss->ifname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002292 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
2293 "interface %s from bridge %s: %s",
2294 bss->ifname, bss->brname, strerror(errno));
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002295 if (drv->rtnl_sk)
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07002296 nl80211_handle_destroy(drv->rtnl_sk);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002297 }
2298 if (bss->added_bridge) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002299 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->brname,
2300 0) < 0)
2301 wpa_printf(MSG_INFO,
2302 "nl80211: Could not set bridge %s down",
2303 bss->brname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002304 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002305 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
2306 "bridge %s: %s",
2307 bss->brname, strerror(errno));
2308 }
2309
2310 nl80211_remove_monitor_interface(drv);
2311
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002312 if (is_ap_interface(drv->nlmode))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002313 wpa_driver_nl80211_del_beacon(drv);
2314
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002315 if (drv->eapol_sock >= 0) {
2316 eloop_unregister_read_sock(drv->eapol_sock);
2317 close(drv->eapol_sock);
2318 }
2319
2320 if (drv->if_indices != drv->default_if_indices)
2321 os_free(drv->if_indices);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002322
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002323 if (drv->disabled_11b_rates)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002324 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
2325
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002326 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, 0,
2327 IF_OPER_UP);
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07002328 eloop_cancel_timeout(wpa_driver_nl80211_send_rfkill, drv, drv->ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002329 rfkill_deinit(drv->rfkill);
2330
2331 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
2332
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002333 if (!drv->start_iface_up)
2334 (void) i802_set_iface_flags(bss, 0);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002335
2336 if (drv->addr_changed) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002337 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname,
2338 0) < 0) {
2339 wpa_printf(MSG_DEBUG,
2340 "nl80211: Could not set interface down to restore permanent MAC address");
2341 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002342 if (linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
2343 drv->perm_addr) < 0) {
2344 wpa_printf(MSG_DEBUG,
2345 "nl80211: Could not restore permanent MAC address");
2346 }
2347 }
2348
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002349 if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002350 if (!drv->hostapd || !drv->start_mode_ap)
2351 wpa_driver_nl80211_set_mode(bss,
2352 NL80211_IFTYPE_STATION);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002353 nl80211_mgmt_unsubscribe(bss, "deinit");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002354 } else {
2355 nl80211_mgmt_unsubscribe(bss, "deinit");
2356 nl80211_del_p2pdev(bss);
2357 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002358
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002359 nl80211_destroy_bss(drv->first_bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002360
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002361 os_free(drv->filter_ssids);
2362
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002363 os_free(drv->auth_ie);
2364
2365 if (drv->in_interface_list)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002366 dl_list_del(&drv->list);
2367
Dmitry Shmidt444d5672013-04-01 13:08:44 -07002368 os_free(drv->extended_capa);
2369 os_free(drv->extended_capa_mask);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002370 os_free(drv->first_bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002371 os_free(drv);
2372}
2373
2374
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002375static u32 wpa_alg_to_cipher_suite(enum wpa_alg alg, size_t key_len)
2376{
2377 switch (alg) {
2378 case WPA_ALG_WEP:
2379 if (key_len == 5)
2380 return WLAN_CIPHER_SUITE_WEP40;
2381 return WLAN_CIPHER_SUITE_WEP104;
2382 case WPA_ALG_TKIP:
2383 return WLAN_CIPHER_SUITE_TKIP;
2384 case WPA_ALG_CCMP:
2385 return WLAN_CIPHER_SUITE_CCMP;
2386 case WPA_ALG_GCMP:
2387 return WLAN_CIPHER_SUITE_GCMP;
2388 case WPA_ALG_CCMP_256:
2389 return WLAN_CIPHER_SUITE_CCMP_256;
2390 case WPA_ALG_GCMP_256:
2391 return WLAN_CIPHER_SUITE_GCMP_256;
2392 case WPA_ALG_IGTK:
2393 return WLAN_CIPHER_SUITE_AES_CMAC;
2394 case WPA_ALG_BIP_GMAC_128:
2395 return WLAN_CIPHER_SUITE_BIP_GMAC_128;
2396 case WPA_ALG_BIP_GMAC_256:
2397 return WLAN_CIPHER_SUITE_BIP_GMAC_256;
2398 case WPA_ALG_BIP_CMAC_256:
2399 return WLAN_CIPHER_SUITE_BIP_CMAC_256;
2400 case WPA_ALG_SMS4:
2401 return WLAN_CIPHER_SUITE_SMS4;
2402 case WPA_ALG_KRK:
2403 return WLAN_CIPHER_SUITE_KRK;
2404 case WPA_ALG_NONE:
2405 case WPA_ALG_PMK:
2406 wpa_printf(MSG_ERROR, "nl80211: Unexpected encryption algorithm %d",
2407 alg);
2408 return 0;
2409 }
2410
2411 wpa_printf(MSG_ERROR, "nl80211: Unsupported encryption algorithm %d",
2412 alg);
2413 return 0;
2414}
2415
2416
2417static u32 wpa_cipher_to_cipher_suite(unsigned int cipher)
2418{
2419 switch (cipher) {
2420 case WPA_CIPHER_CCMP_256:
2421 return WLAN_CIPHER_SUITE_CCMP_256;
2422 case WPA_CIPHER_GCMP_256:
2423 return WLAN_CIPHER_SUITE_GCMP_256;
2424 case WPA_CIPHER_CCMP:
2425 return WLAN_CIPHER_SUITE_CCMP;
2426 case WPA_CIPHER_GCMP:
2427 return WLAN_CIPHER_SUITE_GCMP;
2428 case WPA_CIPHER_TKIP:
2429 return WLAN_CIPHER_SUITE_TKIP;
2430 case WPA_CIPHER_WEP104:
2431 return WLAN_CIPHER_SUITE_WEP104;
2432 case WPA_CIPHER_WEP40:
2433 return WLAN_CIPHER_SUITE_WEP40;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002434 case WPA_CIPHER_GTK_NOT_USED:
2435 return WLAN_CIPHER_SUITE_NO_GROUP_ADDR;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002436 }
2437
2438 return 0;
2439}
2440
2441
2442static int wpa_cipher_to_cipher_suites(unsigned int ciphers, u32 suites[],
2443 int max_suites)
2444{
2445 int num_suites = 0;
2446
2447 if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP_256)
2448 suites[num_suites++] = WLAN_CIPHER_SUITE_CCMP_256;
2449 if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP_256)
2450 suites[num_suites++] = WLAN_CIPHER_SUITE_GCMP_256;
2451 if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP)
2452 suites[num_suites++] = WLAN_CIPHER_SUITE_CCMP;
2453 if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP)
2454 suites[num_suites++] = WLAN_CIPHER_SUITE_GCMP;
2455 if (num_suites < max_suites && ciphers & WPA_CIPHER_TKIP)
2456 suites[num_suites++] = WLAN_CIPHER_SUITE_TKIP;
2457 if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP104)
2458 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP104;
2459 if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP40)
2460 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP40;
2461
2462 return num_suites;
2463}
2464
2465
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002466static int issue_key_mgmt_set_key(struct wpa_driver_nl80211_data *drv,
2467 const u8 *key, size_t key_len)
2468{
2469 struct nl_msg *msg;
2470 int ret;
2471
2472 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_KEY_MGMT_OFFLOAD))
2473 return 0;
2474
2475 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
2476 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
2477 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
2478 QCA_NL80211_VENDOR_SUBCMD_KEY_MGMT_SET_KEY) ||
2479 nla_put(msg, NL80211_ATTR_VENDOR_DATA, key_len, key)) {
2480 nl80211_nlmsg_clear(msg);
2481 nlmsg_free(msg);
2482 return -1;
2483 }
2484 ret = send_and_recv_msgs(drv, msg, NULL, (void *) -1);
2485 if (ret) {
2486 wpa_printf(MSG_DEBUG,
2487 "nl80211: Key management set key failed: ret=%d (%s)",
2488 ret, strerror(-ret));
2489 }
2490
2491 return ret;
2492}
2493
2494
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002495static int wpa_driver_nl80211_set_key(const char *ifname, struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002496 enum wpa_alg alg, const u8 *addr,
2497 int key_idx, int set_tx,
2498 const u8 *seq, size_t seq_len,
2499 const u8 *key, size_t key_len)
2500{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002501 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002502 int ifindex;
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002503 struct nl_msg *msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002504 int ret;
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07002505 int tdls = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002506
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002507 /* Ignore for P2P Device */
2508 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
2509 return 0;
2510
2511 ifindex = if_nametoindex(ifname);
2512 wpa_printf(MSG_DEBUG, "%s: ifindex=%d (%s) alg=%d addr=%p key_idx=%d "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002513 "set_tx=%d seq_len=%lu key_len=%lu",
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002514 __func__, ifindex, ifname, alg, addr, key_idx, set_tx,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002515 (unsigned long) seq_len, (unsigned long) key_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002516#ifdef CONFIG_TDLS
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07002517 if (key_idx == -1) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002518 key_idx = 0;
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07002519 tdls = 1;
2520 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002521#endif /* CONFIG_TDLS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002522
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002523 if (alg == WPA_ALG_PMK &&
2524 (drv->capa.flags & WPA_DRIVER_FLAGS_KEY_MGMT_OFFLOAD)) {
2525 wpa_printf(MSG_DEBUG, "%s: calling issue_key_mgmt_set_key",
2526 __func__);
2527 ret = issue_key_mgmt_set_key(drv, key, key_len);
2528 return ret;
2529 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002530
2531 if (alg == WPA_ALG_NONE) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002532 msg = nl80211_ifindex_msg(drv, ifindex, 0, NL80211_CMD_DEL_KEY);
2533 if (!msg)
2534 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002535 } else {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002536 u32 suite;
2537
2538 suite = wpa_alg_to_cipher_suite(alg, key_len);
2539 if (!suite)
2540 goto fail;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002541 msg = nl80211_ifindex_msg(drv, ifindex, 0, NL80211_CMD_NEW_KEY);
2542 if (!msg ||
2543 nla_put(msg, NL80211_ATTR_KEY_DATA, key_len, key) ||
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002544 nla_put_u32(msg, NL80211_ATTR_KEY_CIPHER, suite))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002545 goto fail;
Dmitry Shmidt98660862014-03-11 17:26:21 -07002546 wpa_hexdump_key(MSG_DEBUG, "nl80211: KEY_DATA", key, key_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002547 }
2548
Dmitry Shmidt98660862014-03-11 17:26:21 -07002549 if (seq && seq_len) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002550 if (nla_put(msg, NL80211_ATTR_KEY_SEQ, seq_len, seq))
2551 goto fail;
Dmitry Shmidt98660862014-03-11 17:26:21 -07002552 wpa_hexdump(MSG_DEBUG, "nl80211: KEY_SEQ", seq, seq_len);
2553 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002554
2555 if (addr && !is_broadcast_ether_addr(addr)) {
2556 wpa_printf(MSG_DEBUG, " addr=" MACSTR, MAC2STR(addr));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002557 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
2558 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002559
2560 if (alg != WPA_ALG_WEP && key_idx && !set_tx) {
2561 wpa_printf(MSG_DEBUG, " RSN IBSS RX GTK");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002562 if (nla_put_u32(msg, NL80211_ATTR_KEY_TYPE,
2563 NL80211_KEYTYPE_GROUP))
2564 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002565 }
2566 } else if (addr && is_broadcast_ether_addr(addr)) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002567 struct nlattr *types;
2568
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002569 wpa_printf(MSG_DEBUG, " broadcast key");
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002570
2571 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002572 if (!types ||
2573 nla_put_flag(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST))
2574 goto fail;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002575 nla_nest_end(msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002576 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002577 if (nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
2578 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002579
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002580 ret = send_and_recv_msgs(drv, msg, NULL, key ? (void *) -1 : NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002581 if ((ret == -ENOENT || ret == -ENOLINK) && alg == WPA_ALG_NONE)
2582 ret = 0;
2583 if (ret)
2584 wpa_printf(MSG_DEBUG, "nl80211: set_key failed; err=%d %s)",
2585 ret, strerror(-ret));
2586
2587 /*
2588 * If we failed or don't need to set the default TX key (below),
2589 * we're done here.
2590 */
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07002591 if (ret || !set_tx || alg == WPA_ALG_NONE || tdls)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002592 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002593 if (is_ap_interface(drv->nlmode) && addr &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002594 !is_broadcast_ether_addr(addr))
2595 return ret;
2596
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002597 msg = nl80211_ifindex_msg(drv, ifindex, 0, NL80211_CMD_SET_KEY);
2598 if (!msg ||
2599 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx) ||
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002600 nla_put_flag(msg, (alg == WPA_ALG_IGTK ||
2601 alg == WPA_ALG_BIP_GMAC_128 ||
2602 alg == WPA_ALG_BIP_GMAC_256 ||
2603 alg == WPA_ALG_BIP_CMAC_256) ?
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002604 NL80211_ATTR_KEY_DEFAULT_MGMT :
2605 NL80211_ATTR_KEY_DEFAULT))
2606 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002607 if (addr && is_broadcast_ether_addr(addr)) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002608 struct nlattr *types;
2609
2610 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002611 if (!types ||
2612 nla_put_flag(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST))
2613 goto fail;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002614 nla_nest_end(msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002615 } else if (addr) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002616 struct nlattr *types;
2617
2618 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002619 if (!types ||
2620 nla_put_flag(msg, NL80211_KEY_DEFAULT_TYPE_UNICAST))
2621 goto fail;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002622 nla_nest_end(msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002623 }
2624
2625 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
2626 if (ret == -ENOENT)
2627 ret = 0;
2628 if (ret)
2629 wpa_printf(MSG_DEBUG, "nl80211: set_key default failed; "
2630 "err=%d %s)", ret, strerror(-ret));
2631 return ret;
2632
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002633fail:
2634 nl80211_nlmsg_clear(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002635 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002636 return -ENOBUFS;
2637}
2638
2639
2640static int nl_add_key(struct nl_msg *msg, enum wpa_alg alg,
2641 int key_idx, int defkey,
2642 const u8 *seq, size_t seq_len,
2643 const u8 *key, size_t key_len)
2644{
2645 struct nlattr *key_attr = nla_nest_start(msg, NL80211_ATTR_KEY);
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002646 u32 suite;
2647
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002648 if (!key_attr)
2649 return -1;
2650
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002651 suite = wpa_alg_to_cipher_suite(alg, key_len);
2652 if (!suite)
2653 return -1;
2654
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002655 if (defkey && alg == WPA_ALG_IGTK) {
2656 if (nla_put_flag(msg, NL80211_KEY_DEFAULT_MGMT))
2657 return -1;
2658 } else if (defkey) {
2659 if (nla_put_flag(msg, NL80211_KEY_DEFAULT))
2660 return -1;
2661 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002662
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002663 if (nla_put_u8(msg, NL80211_KEY_IDX, key_idx) ||
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002664 nla_put_u32(msg, NL80211_KEY_CIPHER, suite) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002665 (seq && seq_len &&
2666 nla_put(msg, NL80211_KEY_SEQ, seq_len, seq)) ||
2667 nla_put(msg, NL80211_KEY_DATA, key_len, key))
2668 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002669
2670 nla_nest_end(msg, key_attr);
2671
2672 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002673}
2674
2675
2676static int nl80211_set_conn_keys(struct wpa_driver_associate_params *params,
2677 struct nl_msg *msg)
2678{
2679 int i, privacy = 0;
2680 struct nlattr *nl_keys, *nl_key;
2681
2682 for (i = 0; i < 4; i++) {
2683 if (!params->wep_key[i])
2684 continue;
2685 privacy = 1;
2686 break;
2687 }
2688 if (params->wps == WPS_MODE_PRIVACY)
2689 privacy = 1;
2690 if (params->pairwise_suite &&
2691 params->pairwise_suite != WPA_CIPHER_NONE)
2692 privacy = 1;
2693
2694 if (!privacy)
2695 return 0;
2696
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002697 if (nla_put_flag(msg, NL80211_ATTR_PRIVACY))
2698 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002699
2700 nl_keys = nla_nest_start(msg, NL80211_ATTR_KEYS);
2701 if (!nl_keys)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002702 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002703
2704 for (i = 0; i < 4; i++) {
2705 if (!params->wep_key[i])
2706 continue;
2707
2708 nl_key = nla_nest_start(msg, i);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002709 if (!nl_key ||
2710 nla_put(msg, NL80211_KEY_DATA, params->wep_key_len[i],
2711 params->wep_key[i]) ||
2712 nla_put_u32(msg, NL80211_KEY_CIPHER,
2713 params->wep_key_len[i] == 5 ?
2714 WLAN_CIPHER_SUITE_WEP40 :
2715 WLAN_CIPHER_SUITE_WEP104) ||
2716 nla_put_u8(msg, NL80211_KEY_IDX, i) ||
2717 (i == params->wep_tx_keyidx &&
2718 nla_put_flag(msg, NL80211_KEY_DEFAULT)))
2719 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002720
2721 nla_nest_end(msg, nl_key);
2722 }
2723 nla_nest_end(msg, nl_keys);
2724
2725 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002726}
2727
2728
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002729int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
2730 const u8 *addr, int cmd, u16 reason_code,
2731 int local_state_change)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002732{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002733 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002734 struct nl_msg *msg;
2735
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002736 if (!(msg = nl80211_drv_msg(drv, 0, cmd)) ||
2737 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code) ||
2738 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
2739 (local_state_change &&
2740 nla_put_flag(msg, NL80211_ATTR_LOCAL_STATE_CHANGE))) {
2741 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002742 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002743 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002744
2745 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002746 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002747 wpa_dbg(drv->ctx, MSG_DEBUG,
2748 "nl80211: MLME command failed: reason=%u ret=%d (%s)",
2749 reason_code, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002750 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002751 return ret;
2752}
2753
2754
2755static int wpa_driver_nl80211_disconnect(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002756 int reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002757{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002758 int ret;
2759
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002760 wpa_printf(MSG_DEBUG, "%s(reason_code=%d)", __func__, reason_code);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07002761 nl80211_mark_disconnected(drv);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002762 /* Disconnect command doesn't need BSSID - it uses cached value */
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002763 ret = wpa_driver_nl80211_mlme(drv, NULL, NL80211_CMD_DISCONNECT,
2764 reason_code, 0);
2765 /*
2766 * For locally generated disconnect, supplicant already generates a
2767 * DEAUTH event, so ignore the event from NL80211.
2768 */
2769 drv->ignore_next_local_disconnect = ret == 0;
2770
2771 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002772}
2773
2774
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002775static int wpa_driver_nl80211_deauthenticate(struct i802_bss *bss,
2776 const u8 *addr, int reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002777{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002778 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07002779 int ret;
Dmitry Shmidt413dde72014-04-11 10:23:22 -07002780
2781 if (drv->nlmode == NL80211_IFTYPE_ADHOC) {
2782 nl80211_mark_disconnected(drv);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002783 return nl80211_leave_ibss(drv, 1);
Dmitry Shmidt413dde72014-04-11 10:23:22 -07002784 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002785 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME))
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002786 return wpa_driver_nl80211_disconnect(drv, reason_code);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002787 wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " reason_code=%d)",
2788 __func__, MAC2STR(addr), reason_code);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07002789 nl80211_mark_disconnected(drv);
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07002790 ret = wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DEAUTHENTICATE,
2791 reason_code, 0);
2792 /*
2793 * For locally generated deauthenticate, supplicant already generates a
2794 * DEAUTH event, so ignore the event from NL80211.
2795 */
2796 drv->ignore_next_local_deauth = ret == 0;
2797 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002798}
2799
2800
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002801static void nl80211_copy_auth_params(struct wpa_driver_nl80211_data *drv,
2802 struct wpa_driver_auth_params *params)
2803{
2804 int i;
2805
2806 drv->auth_freq = params->freq;
2807 drv->auth_alg = params->auth_alg;
2808 drv->auth_wep_tx_keyidx = params->wep_tx_keyidx;
2809 drv->auth_local_state_change = params->local_state_change;
2810 drv->auth_p2p = params->p2p;
2811
2812 if (params->bssid)
2813 os_memcpy(drv->auth_bssid_, params->bssid, ETH_ALEN);
2814 else
2815 os_memset(drv->auth_bssid_, 0, ETH_ALEN);
2816
2817 if (params->ssid) {
2818 os_memcpy(drv->auth_ssid, params->ssid, params->ssid_len);
2819 drv->auth_ssid_len = params->ssid_len;
2820 } else
2821 drv->auth_ssid_len = 0;
2822
2823
2824 os_free(drv->auth_ie);
2825 drv->auth_ie = NULL;
2826 drv->auth_ie_len = 0;
2827 if (params->ie) {
2828 drv->auth_ie = os_malloc(params->ie_len);
2829 if (drv->auth_ie) {
2830 os_memcpy(drv->auth_ie, params->ie, params->ie_len);
2831 drv->auth_ie_len = params->ie_len;
2832 }
2833 }
2834
2835 for (i = 0; i < 4; i++) {
2836 if (params->wep_key[i] && params->wep_key_len[i] &&
2837 params->wep_key_len[i] <= 16) {
2838 os_memcpy(drv->auth_wep_key[i], params->wep_key[i],
2839 params->wep_key_len[i]);
2840 drv->auth_wep_key_len[i] = params->wep_key_len[i];
2841 } else
2842 drv->auth_wep_key_len[i] = 0;
2843 }
2844}
2845
2846
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002847static void nl80211_unmask_11b_rates(struct i802_bss *bss)
2848{
2849 struct wpa_driver_nl80211_data *drv = bss->drv;
2850
2851 if (is_p2p_net_interface(drv->nlmode) || !drv->disabled_11b_rates)
2852 return;
2853
2854 /*
2855 * Looks like we failed to unmask 11b rates previously. This could
2856 * happen, e.g., if the interface was down at the point in time when a
2857 * P2P group was terminated.
2858 */
2859 wpa_printf(MSG_DEBUG,
2860 "nl80211: Interface %s mode is for non-P2P, but 11b rates were disabled - re-enable them",
2861 bss->ifname);
2862 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
2863}
2864
2865
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002866static int wpa_driver_nl80211_authenticate(
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002867 struct i802_bss *bss, struct wpa_driver_auth_params *params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002868{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002869 struct wpa_driver_nl80211_data *drv = bss->drv;
2870 int ret = -1, i;
2871 struct nl_msg *msg;
2872 enum nl80211_auth_type type;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002873 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002874 int count = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002875 int is_retry;
2876
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002877 nl80211_unmask_11b_rates(bss);
2878
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002879 is_retry = drv->retry_auth;
2880 drv->retry_auth = 0;
Dmitry Shmidt98660862014-03-11 17:26:21 -07002881 drv->ignore_deauth_event = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002882
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07002883 nl80211_mark_disconnected(drv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002884 os_memset(drv->auth_bssid, 0, ETH_ALEN);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07002885 if (params->bssid)
2886 os_memcpy(drv->auth_attempt_bssid, params->bssid, ETH_ALEN);
2887 else
2888 os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002889 /* FIX: IBSS mode */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002890 nlmode = params->p2p ?
2891 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
2892 if (drv->nlmode != nlmode &&
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002893 wpa_driver_nl80211_set_mode(bss, nlmode) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002894 return -1;
2895
2896retry:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002897 wpa_printf(MSG_DEBUG, "nl80211: Authenticate (ifindex=%d)",
2898 drv->ifindex);
2899
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002900 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_AUTHENTICATE);
2901 if (!msg)
2902 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002903
2904 for (i = 0; i < 4; i++) {
2905 if (!params->wep_key[i])
2906 continue;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002907 wpa_driver_nl80211_set_key(bss->ifname, bss, WPA_ALG_WEP,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002908 NULL, i,
2909 i == params->wep_tx_keyidx, NULL, 0,
2910 params->wep_key[i],
2911 params->wep_key_len[i]);
2912 if (params->wep_tx_keyidx != i)
2913 continue;
2914 if (nl_add_key(msg, WPA_ALG_WEP, i, 1, NULL, 0,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002915 params->wep_key[i], params->wep_key_len[i]))
2916 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002917 }
2918
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002919 if (params->bssid) {
2920 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
2921 MAC2STR(params->bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002922 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid))
2923 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002924 }
2925 if (params->freq) {
2926 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002927 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq))
2928 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002929 }
2930 if (params->ssid) {
2931 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
2932 params->ssid, params->ssid_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002933 if (nla_put(msg, NL80211_ATTR_SSID, params->ssid_len,
2934 params->ssid))
2935 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002936 }
2937 wpa_hexdump(MSG_DEBUG, " * IEs", params->ie, params->ie_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002938 if (params->ie &&
2939 nla_put(msg, NL80211_ATTR_IE, params->ie_len, params->ie))
2940 goto fail;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002941 if (params->sae_data) {
2942 wpa_hexdump(MSG_DEBUG, " * SAE data", params->sae_data,
2943 params->sae_data_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002944 if (nla_put(msg, NL80211_ATTR_SAE_DATA, params->sae_data_len,
2945 params->sae_data))
2946 goto fail;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002947 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002948 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
2949 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
2950 else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
2951 type = NL80211_AUTHTYPE_SHARED_KEY;
2952 else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
2953 type = NL80211_AUTHTYPE_NETWORK_EAP;
2954 else if (params->auth_alg & WPA_AUTH_ALG_FT)
2955 type = NL80211_AUTHTYPE_FT;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002956 else if (params->auth_alg & WPA_AUTH_ALG_SAE)
2957 type = NL80211_AUTHTYPE_SAE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002958 else
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002959 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002960 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002961 if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE, type))
2962 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002963 if (params->local_state_change) {
2964 wpa_printf(MSG_DEBUG, " * Local state change only");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002965 if (nla_put_flag(msg, NL80211_ATTR_LOCAL_STATE_CHANGE))
2966 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002967 }
2968
2969 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
2970 msg = NULL;
2971 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002972 wpa_dbg(drv->ctx, MSG_DEBUG,
2973 "nl80211: MLME command failed (auth): ret=%d (%s)",
2974 ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002975 count++;
2976 if (ret == -EALREADY && count == 1 && params->bssid &&
2977 !params->local_state_change) {
2978 /*
2979 * mac80211 does not currently accept new
2980 * authentication if we are already authenticated. As a
2981 * workaround, force deauthentication and try again.
2982 */
2983 wpa_printf(MSG_DEBUG, "nl80211: Retry authentication "
2984 "after forced deauthentication");
Dmitry Shmidt98660862014-03-11 17:26:21 -07002985 drv->ignore_deauth_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002986 wpa_driver_nl80211_deauthenticate(
2987 bss, params->bssid,
2988 WLAN_REASON_PREV_AUTH_NOT_VALID);
2989 nlmsg_free(msg);
2990 goto retry;
2991 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002992
2993 if (ret == -ENOENT && params->freq && !is_retry) {
2994 /*
2995 * cfg80211 has likely expired the BSS entry even
2996 * though it was previously available in our internal
2997 * BSS table. To recover quickly, start a single
2998 * channel scan on the specified channel.
2999 */
3000 struct wpa_driver_scan_params scan;
3001 int freqs[2];
3002
3003 os_memset(&scan, 0, sizeof(scan));
3004 scan.num_ssids = 1;
3005 if (params->ssid) {
3006 scan.ssids[0].ssid = params->ssid;
3007 scan.ssids[0].ssid_len = params->ssid_len;
3008 }
3009 freqs[0] = params->freq;
3010 freqs[1] = 0;
3011 scan.freqs = freqs;
3012 wpa_printf(MSG_DEBUG, "nl80211: Trigger single "
3013 "channel scan to refresh cfg80211 BSS "
3014 "entry");
3015 ret = wpa_driver_nl80211_scan(bss, &scan);
3016 if (ret == 0) {
3017 nl80211_copy_auth_params(drv, params);
3018 drv->scan_for_auth = 1;
3019 }
3020 } else if (is_retry) {
3021 /*
3022 * Need to indicate this with an event since the return
3023 * value from the retry is not delivered to core code.
3024 */
3025 union wpa_event_data event;
3026 wpa_printf(MSG_DEBUG, "nl80211: Authentication retry "
3027 "failed");
3028 os_memset(&event, 0, sizeof(event));
3029 os_memcpy(event.timeout_event.addr, drv->auth_bssid_,
3030 ETH_ALEN);
3031 wpa_supplicant_event(drv->ctx, EVENT_AUTH_TIMED_OUT,
3032 &event);
3033 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003034 } else {
3035 wpa_printf(MSG_DEBUG,
3036 "nl80211: Authentication request send successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003037 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003038
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003039fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003040 nlmsg_free(msg);
3041 return ret;
3042}
3043
3044
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003045int wpa_driver_nl80211_authenticate_retry(struct wpa_driver_nl80211_data *drv)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003046{
3047 struct wpa_driver_auth_params params;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003048 struct i802_bss *bss = drv->first_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003049 int i;
3050
3051 wpa_printf(MSG_DEBUG, "nl80211: Try to authenticate again");
3052
3053 os_memset(&params, 0, sizeof(params));
3054 params.freq = drv->auth_freq;
3055 params.auth_alg = drv->auth_alg;
3056 params.wep_tx_keyidx = drv->auth_wep_tx_keyidx;
3057 params.local_state_change = drv->auth_local_state_change;
3058 params.p2p = drv->auth_p2p;
3059
3060 if (!is_zero_ether_addr(drv->auth_bssid_))
3061 params.bssid = drv->auth_bssid_;
3062
3063 if (drv->auth_ssid_len) {
3064 params.ssid = drv->auth_ssid;
3065 params.ssid_len = drv->auth_ssid_len;
3066 }
3067
3068 params.ie = drv->auth_ie;
3069 params.ie_len = drv->auth_ie_len;
3070
3071 for (i = 0; i < 4; i++) {
3072 if (drv->auth_wep_key_len[i]) {
3073 params.wep_key[i] = drv->auth_wep_key[i];
3074 params.wep_key_len[i] = drv->auth_wep_key_len[i];
3075 }
3076 }
3077
3078 drv->retry_auth = 1;
3079 return wpa_driver_nl80211_authenticate(bss, &params);
3080}
3081
3082
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003083static int wpa_driver_nl80211_send_frame(struct i802_bss *bss,
3084 const void *data, size_t len,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003085 int encrypt, int noack,
3086 unsigned int freq, int no_cck,
3087 int offchanok, unsigned int wait_time)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003088{
3089 struct wpa_driver_nl80211_data *drv = bss->drv;
3090 u64 cookie;
Dmitry Shmidt051af732013-10-22 13:52:46 -07003091 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003092
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07003093 if (freq == 0 && drv->nlmode == NL80211_IFTYPE_ADHOC) {
3094 freq = nl80211_get_assoc_freq(drv);
3095 wpa_printf(MSG_DEBUG,
3096 "nl80211: send_frame - Use assoc_freq=%u for IBSS",
3097 freq);
3098 }
Dmitry Shmidt56052862013-10-04 10:23:25 -07003099 if (freq == 0) {
3100 wpa_printf(MSG_DEBUG, "nl80211: send_frame - Use bss->freq=%u",
3101 bss->freq);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003102 freq = bss->freq;
Dmitry Shmidt56052862013-10-04 10:23:25 -07003103 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003104
Dmitry Shmidt56052862013-10-04 10:23:25 -07003105 if (drv->use_monitor) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003106 wpa_printf(MSG_DEBUG, "nl80211: send_frame(freq=%u bss->freq=%u) -> send_monitor",
Dmitry Shmidt56052862013-10-04 10:23:25 -07003107 freq, bss->freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003108 return nl80211_send_monitor(drv, data, len, encrypt, noack);
Dmitry Shmidt56052862013-10-04 10:23:25 -07003109 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003110
Dmitry Shmidt56052862013-10-04 10:23:25 -07003111 wpa_printf(MSG_DEBUG, "nl80211: send_frame -> send_frame_cmd");
Dmitry Shmidt051af732013-10-22 13:52:46 -07003112 res = nl80211_send_frame_cmd(bss, freq, wait_time, data, len,
3113 &cookie, no_cck, noack, offchanok);
3114 if (res == 0 && !noack) {
3115 const struct ieee80211_mgmt *mgmt;
3116 u16 fc;
3117
3118 mgmt = (const struct ieee80211_mgmt *) data;
3119 fc = le_to_host16(mgmt->frame_control);
3120 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
3121 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_ACTION) {
3122 wpa_printf(MSG_MSGDUMP,
3123 "nl80211: Update send_action_cookie from 0x%llx to 0x%llx",
3124 (long long unsigned int)
3125 drv->send_action_cookie,
3126 (long long unsigned int) cookie);
3127 drv->send_action_cookie = cookie;
3128 }
3129 }
3130
3131 return res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003132}
3133
3134
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003135static int wpa_driver_nl80211_send_mlme(struct i802_bss *bss, const u8 *data,
3136 size_t data_len, int noack,
3137 unsigned int freq, int no_cck,
3138 int offchanok,
3139 unsigned int wait_time)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003140{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003141 struct wpa_driver_nl80211_data *drv = bss->drv;
3142 struct ieee80211_mgmt *mgmt;
3143 int encrypt = 1;
3144 u16 fc;
3145
3146 mgmt = (struct ieee80211_mgmt *) data;
3147 fc = le_to_host16(mgmt->frame_control);
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07003148 wpa_printf(MSG_DEBUG, "nl80211: send_mlme - da= " MACSTR
3149 " noack=%d freq=%u no_cck=%d offchanok=%d wait_time=%u fc=0x%x (%s) nlmode=%d",
3150 MAC2STR(mgmt->da), noack, freq, no_cck, offchanok, wait_time,
3151 fc, fc2str(fc), drv->nlmode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003152
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003153 if ((is_sta_interface(drv->nlmode) ||
3154 drv->nlmode == NL80211_IFTYPE_P2P_DEVICE) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003155 WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
3156 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_RESP) {
3157 /*
3158 * The use of last_mgmt_freq is a bit of a hack,
3159 * but it works due to the single-threaded nature
3160 * of wpa_supplicant.
3161 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07003162 if (freq == 0) {
3163 wpa_printf(MSG_DEBUG, "nl80211: Use last_mgmt_freq=%d",
3164 drv->last_mgmt_freq);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003165 freq = drv->last_mgmt_freq;
Dmitry Shmidt56052862013-10-04 10:23:25 -07003166 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003167 return nl80211_send_frame_cmd(bss, freq, 0,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003168 data, data_len, NULL, 1, noack,
3169 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003170 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003171
3172 if (drv->device_ap_sme && is_ap_interface(drv->nlmode)) {
Dmitry Shmidt56052862013-10-04 10:23:25 -07003173 if (freq == 0) {
3174 wpa_printf(MSG_DEBUG, "nl80211: Use bss->freq=%d",
3175 bss->freq);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003176 freq = bss->freq;
Dmitry Shmidt56052862013-10-04 10:23:25 -07003177 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003178 return nl80211_send_frame_cmd(bss, freq,
3179 (int) freq == bss->freq ? 0 :
3180 wait_time,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003181 data, data_len,
3182 &drv->send_action_cookie,
3183 no_cck, noack, offchanok);
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07003184 }
Dmitry Shmidtb638fe72012-03-20 12:51:25 -07003185
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003186 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
3187 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) {
3188 /*
3189 * Only one of the authentication frame types is encrypted.
3190 * In order for static WEP encryption to work properly (i.e.,
3191 * to not encrypt the frame), we need to tell mac80211 about
3192 * the frames that must not be encrypted.
3193 */
3194 u16 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
3195 u16 auth_trans = le_to_host16(mgmt->u.auth.auth_transaction);
3196 if (auth_alg != WLAN_AUTH_SHARED_KEY || auth_trans != 3)
3197 encrypt = 0;
3198 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003199
Dmitry Shmidt56052862013-10-04 10:23:25 -07003200 wpa_printf(MSG_DEBUG, "nl80211: send_mlme -> send_frame");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003201 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003202 noack, freq, no_cck, offchanok,
3203 wait_time);
3204}
3205
3206
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003207static int nl80211_put_basic_rates(struct nl_msg *msg, const int *basic_rates)
3208{
3209 u8 rates[NL80211_MAX_SUPP_RATES];
3210 u8 rates_len = 0;
3211 int i;
3212
3213 if (!basic_rates)
3214 return 0;
3215
3216 for (i = 0; i < NL80211_MAX_SUPP_RATES && basic_rates[i] >= 0; i++)
3217 rates[rates_len++] = basic_rates[i] / 5;
3218
3219 return nla_put(msg, NL80211_ATTR_BSS_BASIC_RATES, rates_len, rates);
3220}
3221
3222
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003223static int nl80211_set_bss(struct i802_bss *bss, int cts, int preamble,
3224 int slot, int ht_opmode, int ap_isolate,
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003225 const int *basic_rates)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003226{
3227 struct wpa_driver_nl80211_data *drv = bss->drv;
3228 struct nl_msg *msg;
3229
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003230 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_BSS)) ||
3231 (cts >= 0 &&
3232 nla_put_u8(msg, NL80211_ATTR_BSS_CTS_PROT, cts)) ||
3233 (preamble >= 0 &&
3234 nla_put_u8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble)) ||
3235 (slot >= 0 &&
3236 nla_put_u8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot)) ||
3237 (ht_opmode >= 0 &&
3238 nla_put_u16(msg, NL80211_ATTR_BSS_HT_OPMODE, ht_opmode)) ||
3239 (ap_isolate >= 0 &&
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003240 nla_put_u8(msg, NL80211_ATTR_AP_ISOLATE, ap_isolate)) ||
3241 nl80211_put_basic_rates(msg, basic_rates)) {
3242 nlmsg_free(msg);
3243 return -ENOBUFS;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003244 }
3245
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003246 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003247}
3248
3249
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003250static int wpa_driver_nl80211_set_acl(void *priv,
3251 struct hostapd_acl_params *params)
3252{
3253 struct i802_bss *bss = priv;
3254 struct wpa_driver_nl80211_data *drv = bss->drv;
3255 struct nl_msg *msg;
3256 struct nlattr *acl;
3257 unsigned int i;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003258 int ret;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003259
3260 if (!(drv->capa.max_acl_mac_addrs))
3261 return -ENOTSUP;
3262
3263 if (params->num_mac_acl > drv->capa.max_acl_mac_addrs)
3264 return -ENOTSUP;
3265
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003266 wpa_printf(MSG_DEBUG, "nl80211: Set %s ACL (num_mac_acl=%u)",
3267 params->acl_policy ? "Accept" : "Deny", params->num_mac_acl);
3268
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003269 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_MAC_ACL)) ||
3270 nla_put_u32(msg, NL80211_ATTR_ACL_POLICY, params->acl_policy ?
3271 NL80211_ACL_POLICY_DENY_UNLESS_LISTED :
3272 NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED) ||
3273 (acl = nla_nest_start(msg, NL80211_ATTR_MAC_ADDRS)) == NULL) {
3274 nlmsg_free(msg);
3275 return -ENOMEM;
3276 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003277
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003278 for (i = 0; i < params->num_mac_acl; i++) {
3279 if (nla_put(msg, i + 1, ETH_ALEN, params->mac_acl[i].addr)) {
3280 nlmsg_free(msg);
3281 return -ENOMEM;
3282 }
3283 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003284
3285 nla_nest_end(msg, acl);
3286
3287 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003288 if (ret) {
3289 wpa_printf(MSG_DEBUG, "nl80211: Failed to set MAC ACL: %d (%s)",
3290 ret, strerror(-ret));
3291 }
3292
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003293 return ret;
3294}
3295
3296
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003297static int nl80211_put_beacon_int(struct nl_msg *msg, int beacon_int)
3298{
3299 if (beacon_int > 0) {
3300 wpa_printf(MSG_DEBUG, " * beacon_int=%d", beacon_int);
3301 return nla_put_u32(msg, NL80211_ATTR_BEACON_INTERVAL,
3302 beacon_int);
3303 }
3304
3305 return 0;
3306}
3307
3308
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003309static int wpa_driver_nl80211_set_ap(void *priv,
3310 struct wpa_driver_ap_params *params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003311{
3312 struct i802_bss *bss = priv;
3313 struct wpa_driver_nl80211_data *drv = bss->drv;
3314 struct nl_msg *msg;
3315 u8 cmd = NL80211_CMD_NEW_BEACON;
3316 int ret;
3317 int beacon_set;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003318 int num_suites;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003319 int smps_mode;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003320 u32 suites[10], suite;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003321 u32 ver;
3322
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003323 beacon_set = params->reenable ? 0 : bss->beacon_set;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003324
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003325 wpa_printf(MSG_DEBUG, "nl80211: Set beacon (beacon_set=%d)",
3326 beacon_set);
3327 if (beacon_set)
3328 cmd = NL80211_CMD_SET_BEACON;
3329
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003330 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon head",
3331 params->head, params->head_len);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003332 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon tail",
3333 params->tail, params->tail_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003334 wpa_printf(MSG_DEBUG, "nl80211: ifindex=%d", bss->ifindex);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003335 wpa_printf(MSG_DEBUG, "nl80211: beacon_int=%d", params->beacon_int);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003336 wpa_printf(MSG_DEBUG, "nl80211: dtim_period=%d", params->dtim_period);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003337 wpa_hexdump_ascii(MSG_DEBUG, "nl80211: ssid",
3338 params->ssid, params->ssid_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003339 if (!(msg = nl80211_bss_msg(bss, 0, cmd)) ||
3340 nla_put(msg, NL80211_ATTR_BEACON_HEAD, params->head_len,
3341 params->head) ||
3342 nla_put(msg, NL80211_ATTR_BEACON_TAIL, params->tail_len,
3343 params->tail) ||
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003344 nl80211_put_beacon_int(msg, params->beacon_int) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003345 nla_put_u32(msg, NL80211_ATTR_DTIM_PERIOD, params->dtim_period) ||
3346 nla_put(msg, NL80211_ATTR_SSID, params->ssid_len, params->ssid))
3347 goto fail;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003348 if (params->proberesp && params->proberesp_len) {
3349 wpa_hexdump(MSG_DEBUG, "nl80211: proberesp (offload)",
3350 params->proberesp, params->proberesp_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003351 if (nla_put(msg, NL80211_ATTR_PROBE_RESP, params->proberesp_len,
3352 params->proberesp))
3353 goto fail;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003354 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003355 switch (params->hide_ssid) {
3356 case NO_SSID_HIDING:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003357 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID not in use");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003358 if (nla_put_u32(msg, NL80211_ATTR_HIDDEN_SSID,
3359 NL80211_HIDDEN_SSID_NOT_IN_USE))
3360 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003361 break;
3362 case HIDDEN_SSID_ZERO_LEN:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003363 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero len");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003364 if (nla_put_u32(msg, NL80211_ATTR_HIDDEN_SSID,
3365 NL80211_HIDDEN_SSID_ZERO_LEN))
3366 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003367 break;
3368 case HIDDEN_SSID_ZERO_CONTENTS:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003369 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero contents");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003370 if (nla_put_u32(msg, NL80211_ATTR_HIDDEN_SSID,
3371 NL80211_HIDDEN_SSID_ZERO_CONTENTS))
3372 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003373 break;
3374 }
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003375 wpa_printf(MSG_DEBUG, "nl80211: privacy=%d", params->privacy);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003376 if (params->privacy &&
3377 nla_put_flag(msg, NL80211_ATTR_PRIVACY))
3378 goto fail;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003379 wpa_printf(MSG_DEBUG, "nl80211: auth_algs=0x%x", params->auth_algs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003380 if ((params->auth_algs & (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) ==
3381 (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) {
3382 /* Leave out the attribute */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003383 } else if (params->auth_algs & WPA_AUTH_ALG_SHARED) {
3384 if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE,
3385 NL80211_AUTHTYPE_SHARED_KEY))
3386 goto fail;
3387 } else {
3388 if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE,
3389 NL80211_AUTHTYPE_OPEN_SYSTEM))
3390 goto fail;
3391 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003392
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003393 wpa_printf(MSG_DEBUG, "nl80211: wpa_version=0x%x", params->wpa_version);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003394 ver = 0;
3395 if (params->wpa_version & WPA_PROTO_WPA)
3396 ver |= NL80211_WPA_VERSION_1;
3397 if (params->wpa_version & WPA_PROTO_RSN)
3398 ver |= NL80211_WPA_VERSION_2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003399 if (ver &&
3400 nla_put_u32(msg, NL80211_ATTR_WPA_VERSIONS, ver))
3401 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003402
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003403 wpa_printf(MSG_DEBUG, "nl80211: key_mgmt_suites=0x%x",
3404 params->key_mgmt_suites);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003405 num_suites = 0;
3406 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X)
3407 suites[num_suites++] = WLAN_AKM_SUITE_8021X;
3408 if (params->key_mgmt_suites & WPA_KEY_MGMT_PSK)
3409 suites[num_suites++] = WLAN_AKM_SUITE_PSK;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003410 if (num_suites &&
3411 nla_put(msg, NL80211_ATTR_AKM_SUITES, num_suites * sizeof(u32),
3412 suites))
3413 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003414
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003415 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
3416 params->pairwise_ciphers & (WPA_CIPHER_WEP104 | WPA_CIPHER_WEP40) &&
3417 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT))
3418 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003419
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003420 wpa_printf(MSG_DEBUG, "nl80211: pairwise_ciphers=0x%x",
3421 params->pairwise_ciphers);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003422 num_suites = wpa_cipher_to_cipher_suites(params->pairwise_ciphers,
3423 suites, ARRAY_SIZE(suites));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003424 if (num_suites &&
3425 nla_put(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
3426 num_suites * sizeof(u32), suites))
3427 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003428
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003429 wpa_printf(MSG_DEBUG, "nl80211: group_cipher=0x%x",
3430 params->group_cipher);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003431 suite = wpa_cipher_to_cipher_suite(params->group_cipher);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003432 if (suite &&
3433 nla_put_u32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, suite))
3434 goto fail;
3435
3436 switch (params->smps_mode) {
3437 case HT_CAP_INFO_SMPS_DYNAMIC:
3438 wpa_printf(MSG_DEBUG, "nl80211: SMPS mode - dynamic");
3439 smps_mode = NL80211_SMPS_DYNAMIC;
3440 break;
3441 case HT_CAP_INFO_SMPS_STATIC:
3442 wpa_printf(MSG_DEBUG, "nl80211: SMPS mode - static");
3443 smps_mode = NL80211_SMPS_STATIC;
3444 break;
3445 default:
3446 /* invalid - fallback to smps off */
3447 case HT_CAP_INFO_SMPS_DISABLED:
3448 wpa_printf(MSG_DEBUG, "nl80211: SMPS mode - off");
3449 smps_mode = NL80211_SMPS_OFF;
3450 break;
3451 }
3452 if (nla_put_u32(msg, NL80211_ATTR_SMPS_MODE, smps_mode))
3453 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003454
3455 if (params->beacon_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003456 wpa_hexdump_buf(MSG_DEBUG, "nl80211: beacon_ies",
3457 params->beacon_ies);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003458 if (nla_put(msg, NL80211_ATTR_IE,
3459 wpabuf_len(params->beacon_ies),
3460 wpabuf_head(params->beacon_ies)))
3461 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003462 }
3463 if (params->proberesp_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003464 wpa_hexdump_buf(MSG_DEBUG, "nl80211: proberesp_ies",
3465 params->proberesp_ies);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003466 if (nla_put(msg, NL80211_ATTR_IE_PROBE_RESP,
3467 wpabuf_len(params->proberesp_ies),
3468 wpabuf_head(params->proberesp_ies)))
3469 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003470 }
3471 if (params->assocresp_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003472 wpa_hexdump_buf(MSG_DEBUG, "nl80211: assocresp_ies",
3473 params->assocresp_ies);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003474 if (nla_put(msg, NL80211_ATTR_IE_ASSOC_RESP,
3475 wpabuf_len(params->assocresp_ies),
3476 wpabuf_head(params->assocresp_ies)))
3477 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003478 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003479
Dmitry Shmidt04949592012-07-19 12:16:46 -07003480 if (drv->capa.flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003481 wpa_printf(MSG_DEBUG, "nl80211: ap_max_inactivity=%d",
3482 params->ap_max_inactivity);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003483 if (nla_put_u16(msg, NL80211_ATTR_INACTIVITY_TIMEOUT,
3484 params->ap_max_inactivity))
3485 goto fail;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003486 }
3487
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003488#ifdef CONFIG_P2P
3489 if (params->p2p_go_ctwindow > 0) {
3490 if (drv->p2p_go_ctwindow_supported) {
3491 wpa_printf(MSG_DEBUG, "nl80211: P2P GO ctwindow=%d",
3492 params->p2p_go_ctwindow);
3493 if (nla_put_u8(msg, NL80211_ATTR_P2P_CTWINDOW,
3494 params->p2p_go_ctwindow))
3495 goto fail;
3496 } else {
3497 wpa_printf(MSG_INFO,
3498 "nl80211: Driver does not support CTWindow configuration - ignore this parameter");
3499 }
3500 }
3501#endif /* CONFIG_P2P */
3502
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003503 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3504 if (ret) {
3505 wpa_printf(MSG_DEBUG, "nl80211: Beacon set failed: %d (%s)",
3506 ret, strerror(-ret));
3507 } else {
3508 bss->beacon_set = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003509 nl80211_set_bss(bss, params->cts_protect, params->preamble,
3510 params->short_slot_time, params->ht_opmode,
3511 params->isolate, params->basic_rates);
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07003512 if (beacon_set && params->freq &&
3513 params->freq->bandwidth != bss->bandwidth) {
3514 wpa_printf(MSG_DEBUG,
3515 "nl80211: Update BSS %s bandwidth: %d -> %d",
3516 bss->ifname, bss->bandwidth,
3517 params->freq->bandwidth);
3518 ret = nl80211_set_channel(bss, params->freq, 1);
3519 if (ret) {
3520 wpa_printf(MSG_DEBUG,
3521 "nl80211: Frequency set failed: %d (%s)",
3522 ret, strerror(-ret));
3523 } else {
3524 wpa_printf(MSG_DEBUG,
3525 "nl80211: Frequency set succeeded for ht2040 coex");
3526 bss->bandwidth = params->freq->bandwidth;
3527 }
3528 } else if (!beacon_set) {
3529 /*
3530 * cfg80211 updates the driver on frequence change in AP
3531 * mode only at the point when beaconing is started, so
3532 * set the initial value here.
3533 */
3534 bss->bandwidth = params->freq->bandwidth;
3535 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003536 }
3537 return ret;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003538fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003539 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003540 return -ENOBUFS;
3541}
3542
3543
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003544static int nl80211_put_freq_params(struct nl_msg *msg,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003545 const struct hostapd_freq_params *freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003546{
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003547 wpa_printf(MSG_DEBUG, " * freq=%d", freq->freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003548 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq->freq))
3549 return -ENOBUFS;
3550
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003551 wpa_printf(MSG_DEBUG, " * vht_enabled=%d", freq->vht_enabled);
3552 wpa_printf(MSG_DEBUG, " * ht_enabled=%d", freq->ht_enabled);
3553
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003554 if (freq->vht_enabled) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003555 enum nl80211_chan_width cw;
3556
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003557 wpa_printf(MSG_DEBUG, " * bandwidth=%d", freq->bandwidth);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003558 switch (freq->bandwidth) {
3559 case 20:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003560 cw = NL80211_CHAN_WIDTH_20;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003561 break;
3562 case 40:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003563 cw = NL80211_CHAN_WIDTH_40;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003564 break;
3565 case 80:
3566 if (freq->center_freq2)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003567 cw = NL80211_CHAN_WIDTH_80P80;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003568 else
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003569 cw = NL80211_CHAN_WIDTH_80;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003570 break;
3571 case 160:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003572 cw = NL80211_CHAN_WIDTH_160;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003573 break;
3574 default:
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003575 return -EINVAL;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003576 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003577
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003578 wpa_printf(MSG_DEBUG, " * channel_width=%d", cw);
3579 wpa_printf(MSG_DEBUG, " * center_freq1=%d",
3580 freq->center_freq1);
3581 wpa_printf(MSG_DEBUG, " * center_freq2=%d",
3582 freq->center_freq2);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003583 if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, cw) ||
3584 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1,
3585 freq->center_freq1) ||
3586 (freq->center_freq2 &&
3587 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2,
3588 freq->center_freq2)))
3589 return -ENOBUFS;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003590 } else if (freq->ht_enabled) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003591 enum nl80211_channel_type ct;
3592
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003593 wpa_printf(MSG_DEBUG, " * sec_channel_offset=%d",
3594 freq->sec_channel_offset);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003595 switch (freq->sec_channel_offset) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003596 case -1:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003597 ct = NL80211_CHAN_HT40MINUS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003598 break;
3599 case 1:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003600 ct = NL80211_CHAN_HT40PLUS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003601 break;
3602 default:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003603 ct = NL80211_CHAN_HT20;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003604 break;
3605 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003606
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003607 wpa_printf(MSG_DEBUG, " * channel_type=%d", ct);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003608 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, ct))
3609 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003610 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003611 return 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003612}
3613
3614
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07003615static int nl80211_set_channel(struct i802_bss *bss,
3616 struct hostapd_freq_params *freq, int set_chan)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003617{
3618 struct wpa_driver_nl80211_data *drv = bss->drv;
3619 struct nl_msg *msg;
3620 int ret;
3621
3622 wpa_printf(MSG_DEBUG,
3623 "nl80211: Set freq %d (ht_enabled=%d, vht_enabled=%d, bandwidth=%d MHz, cf1=%d MHz, cf2=%d MHz)",
3624 freq->freq, freq->ht_enabled, freq->vht_enabled,
3625 freq->bandwidth, freq->center_freq1, freq->center_freq2);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003626
3627 msg = nl80211_drv_msg(drv, 0, set_chan ? NL80211_CMD_SET_CHANNEL :
3628 NL80211_CMD_SET_WIPHY);
3629 if (!msg || nl80211_put_freq_params(msg, freq) < 0) {
3630 nlmsg_free(msg);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003631 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003632 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003633
3634 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003635 if (ret == 0) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003636 bss->freq = freq->freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003637 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003638 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003639 wpa_printf(MSG_DEBUG, "nl80211: Failed to set channel (freq=%d): "
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003640 "%d (%s)", freq->freq, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003641 return -1;
3642}
3643
3644
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003645static u32 sta_flags_nl80211(int flags)
3646{
3647 u32 f = 0;
3648
3649 if (flags & WPA_STA_AUTHORIZED)
3650 f |= BIT(NL80211_STA_FLAG_AUTHORIZED);
3651 if (flags & WPA_STA_WMM)
3652 f |= BIT(NL80211_STA_FLAG_WME);
3653 if (flags & WPA_STA_SHORT_PREAMBLE)
3654 f |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
3655 if (flags & WPA_STA_MFP)
3656 f |= BIT(NL80211_STA_FLAG_MFP);
3657 if (flags & WPA_STA_TDLS_PEER)
3658 f |= BIT(NL80211_STA_FLAG_TDLS_PEER);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003659 if (flags & WPA_STA_AUTHENTICATED)
3660 f |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003661
3662 return f;
3663}
3664
3665
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003666#ifdef CONFIG_MESH
3667static u32 sta_plink_state_nl80211(enum mesh_plink_state state)
3668{
3669 switch (state) {
3670 case PLINK_LISTEN:
3671 return NL80211_PLINK_LISTEN;
3672 case PLINK_OPEN_SENT:
3673 return NL80211_PLINK_OPN_SNT;
3674 case PLINK_OPEN_RCVD:
3675 return NL80211_PLINK_OPN_RCVD;
3676 case PLINK_CNF_RCVD:
3677 return NL80211_PLINK_CNF_RCVD;
3678 case PLINK_ESTAB:
3679 return NL80211_PLINK_ESTAB;
3680 case PLINK_HOLDING:
3681 return NL80211_PLINK_HOLDING;
3682 case PLINK_BLOCKED:
3683 return NL80211_PLINK_BLOCKED;
3684 default:
3685 wpa_printf(MSG_ERROR, "nl80211: Invalid mesh plink state %d",
3686 state);
3687 }
3688 return -1;
3689}
3690#endif /* CONFIG_MESH */
3691
3692
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003693static int wpa_driver_nl80211_sta_add(void *priv,
3694 struct hostapd_sta_add_params *params)
3695{
3696 struct i802_bss *bss = priv;
3697 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003698 struct nl_msg *msg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003699 struct nl80211_sta_flag_update upd;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003700 int ret = -ENOBUFS;
3701
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003702 if ((params->flags & WPA_STA_TDLS_PEER) &&
3703 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
3704 return -EOPNOTSUPP;
3705
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003706 wpa_printf(MSG_DEBUG, "nl80211: %s STA " MACSTR,
3707 params->set ? "Set" : "Add", MAC2STR(params->addr));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003708 msg = nl80211_bss_msg(bss, 0, params->set ? NL80211_CMD_SET_STATION :
3709 NL80211_CMD_NEW_STATION);
3710 if (!msg || nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr))
3711 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003712
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003713 if (!params->set || (params->flags & WPA_STA_TDLS_PEER)) {
3714 wpa_hexdump(MSG_DEBUG, " * supported rates",
3715 params->supp_rates, params->supp_rates_len);
3716 wpa_printf(MSG_DEBUG, " * capability=0x%x",
3717 params->capability);
3718 if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_RATES,
3719 params->supp_rates_len, params->supp_rates) ||
3720 nla_put_u16(msg, NL80211_ATTR_STA_CAPABILITY,
3721 params->capability))
3722 goto fail;
3723
3724 if (params->ht_capabilities) {
3725 wpa_hexdump(MSG_DEBUG, " * ht_capabilities",
3726 (u8 *) params->ht_capabilities,
3727 sizeof(*params->ht_capabilities));
3728 if (nla_put(msg, NL80211_ATTR_HT_CAPABILITY,
3729 sizeof(*params->ht_capabilities),
3730 params->ht_capabilities))
3731 goto fail;
3732 }
3733
3734 if (params->vht_capabilities) {
3735 wpa_hexdump(MSG_DEBUG, " * vht_capabilities",
3736 (u8 *) params->vht_capabilities,
3737 sizeof(*params->vht_capabilities));
3738 if (nla_put(msg, NL80211_ATTR_VHT_CAPABILITY,
3739 sizeof(*params->vht_capabilities),
3740 params->vht_capabilities))
3741 goto fail;
3742 }
3743
3744 if (params->ext_capab) {
3745 wpa_hexdump(MSG_DEBUG, " * ext_capab",
3746 params->ext_capab, params->ext_capab_len);
3747 if (nla_put(msg, NL80211_ATTR_STA_EXT_CAPABILITY,
3748 params->ext_capab_len, params->ext_capab))
3749 goto fail;
3750 }
3751 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003752 if (!params->set) {
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07003753 if (params->aid) {
3754 wpa_printf(MSG_DEBUG, " * aid=%u", params->aid);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003755 if (nla_put_u16(msg, NL80211_ATTR_STA_AID, params->aid))
3756 goto fail;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07003757 } else {
3758 /*
3759 * cfg80211 validates that AID is non-zero, so we have
3760 * to make this a non-zero value for the TDLS case where
3761 * a dummy STA entry is used for now.
3762 */
3763 wpa_printf(MSG_DEBUG, " * aid=1 (TDLS workaround)");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003764 if (nla_put_u16(msg, NL80211_ATTR_STA_AID, 1))
3765 goto fail;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07003766 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003767 wpa_printf(MSG_DEBUG, " * listen_interval=%u",
3768 params->listen_interval);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003769 if (nla_put_u16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
3770 params->listen_interval))
3771 goto fail;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003772 } else if (params->aid && (params->flags & WPA_STA_TDLS_PEER)) {
3773 wpa_printf(MSG_DEBUG, " * peer_aid=%u", params->aid);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003774 if (nla_put_u16(msg, NL80211_ATTR_PEER_AID, params->aid))
3775 goto fail;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003776 }
3777
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08003778 if (params->vht_opmode_enabled) {
3779 wpa_printf(MSG_DEBUG, " * opmode=%u", params->vht_opmode);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003780 if (nla_put_u8(msg, NL80211_ATTR_OPMODE_NOTIF,
3781 params->vht_opmode))
3782 goto fail;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003783 }
3784
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003785 if (params->supp_channels) {
3786 wpa_hexdump(MSG_DEBUG, " * supported channels",
3787 params->supp_channels, params->supp_channels_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003788 if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_CHANNELS,
3789 params->supp_channels_len, params->supp_channels))
3790 goto fail;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003791 }
3792
3793 if (params->supp_oper_classes) {
3794 wpa_hexdump(MSG_DEBUG, " * supported operating classes",
3795 params->supp_oper_classes,
3796 params->supp_oper_classes_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003797 if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES,
3798 params->supp_oper_classes_len,
3799 params->supp_oper_classes))
3800 goto fail;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003801 }
3802
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003803 os_memset(&upd, 0, sizeof(upd));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003804 upd.set = sta_flags_nl80211(params->flags);
3805 upd.mask = upd.set | sta_flags_nl80211(params->flags_mask);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003806 wpa_printf(MSG_DEBUG, " * flags set=0x%x mask=0x%x",
3807 upd.set, upd.mask);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003808 if (nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd))
3809 goto fail;
3810
3811#ifdef CONFIG_MESH
3812 if (params->plink_state &&
3813 nla_put_u8(msg, NL80211_ATTR_STA_PLINK_STATE,
3814 sta_plink_state_nl80211(params->plink_state)))
3815 goto fail;
3816#endif /* CONFIG_MESH */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003817
3818 if (params->flags & WPA_STA_WMM) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003819 struct nlattr *wme = nla_nest_start(msg, NL80211_ATTR_STA_WME);
3820
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003821 wpa_printf(MSG_DEBUG, " * qosinfo=0x%x", params->qosinfo);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003822 if (!wme ||
3823 nla_put_u8(msg, NL80211_STA_WME_UAPSD_QUEUES,
3824 params->qosinfo & WMM_QOSINFO_STA_AC_MASK) ||
3825 nla_put_u8(msg, NL80211_STA_WME_MAX_SP,
3826 (params->qosinfo >> WMM_QOSINFO_STA_SP_SHIFT) &
3827 WMM_QOSINFO_STA_SP_MASK))
3828 goto fail;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003829 nla_nest_end(msg, wme);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003830 }
3831
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003832 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003833 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003834 if (ret)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003835 wpa_printf(MSG_DEBUG, "nl80211: NL80211_CMD_%s_STATION "
3836 "result: %d (%s)", params->set ? "SET" : "NEW", ret,
3837 strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003838 if (ret == -EEXIST)
3839 ret = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003840fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003841 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003842 return ret;
3843}
3844
3845
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003846static void rtnl_neigh_delete_fdb_entry(struct i802_bss *bss, const u8 *addr)
3847{
3848#ifdef CONFIG_LIBNL3_ROUTE
3849 struct wpa_driver_nl80211_data *drv = bss->drv;
3850 struct rtnl_neigh *rn;
3851 struct nl_addr *nl_addr;
3852 int err;
3853
3854 rn = rtnl_neigh_alloc();
3855 if (!rn)
3856 return;
3857
3858 rtnl_neigh_set_family(rn, AF_BRIDGE);
3859 rtnl_neigh_set_ifindex(rn, bss->ifindex);
3860 nl_addr = nl_addr_build(AF_BRIDGE, (void *) addr, ETH_ALEN);
3861 if (!nl_addr) {
3862 rtnl_neigh_put(rn);
3863 return;
3864 }
3865 rtnl_neigh_set_lladdr(rn, nl_addr);
3866
3867 err = rtnl_neigh_delete(drv->rtnl_sk, rn, 0);
3868 if (err < 0) {
3869 wpa_printf(MSG_DEBUG, "nl80211: bridge FDB entry delete for "
3870 MACSTR " ifindex=%d failed: %s", MAC2STR(addr),
3871 bss->ifindex, nl_geterror(err));
3872 } else {
3873 wpa_printf(MSG_DEBUG, "nl80211: deleted bridge FDB entry for "
3874 MACSTR, MAC2STR(addr));
3875 }
3876
3877 nl_addr_put(nl_addr);
3878 rtnl_neigh_put(rn);
3879#endif /* CONFIG_LIBNL3_ROUTE */
3880}
3881
3882
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003883static int wpa_driver_nl80211_sta_remove(struct i802_bss *bss, const u8 *addr,
3884 int deauth, u16 reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003885{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003886 struct wpa_driver_nl80211_data *drv = bss->drv;
3887 struct nl_msg *msg;
3888 int ret;
3889
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003890 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_DEL_STATION)) ||
3891 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
3892 (deauth == 0 &&
3893 nla_put_u8(msg, NL80211_ATTR_MGMT_SUBTYPE,
3894 WLAN_FC_STYPE_DISASSOC)) ||
3895 (deauth == 1 &&
3896 nla_put_u8(msg, NL80211_ATTR_MGMT_SUBTYPE,
3897 WLAN_FC_STYPE_DEAUTH)) ||
3898 (reason_code &&
3899 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code))) {
3900 nlmsg_free(msg);
3901 return -ENOBUFS;
3902 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003903
3904 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07003905 wpa_printf(MSG_DEBUG, "nl80211: sta_remove -> DEL_STATION %s " MACSTR
3906 " --> %d (%s)",
3907 bss->ifname, MAC2STR(addr), ret, strerror(-ret));
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003908
3909 if (drv->rtnl_sk)
3910 rtnl_neigh_delete_fdb_entry(bss, addr);
3911
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003912 if (ret == -ENOENT)
3913 return 0;
3914 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003915}
3916
3917
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003918void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv, int ifidx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003919{
3920 struct nl_msg *msg;
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07003921 struct wpa_driver_nl80211_data *drv2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003922
3923 wpa_printf(MSG_DEBUG, "nl80211: Remove interface ifindex=%d", ifidx);
3924
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003925 /* stop listening for EAPOL on this interface */
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07003926 dl_list_for_each(drv2, &drv->global->interfaces,
3927 struct wpa_driver_nl80211_data, list)
3928 del_ifidx(drv2, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003929
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003930 msg = nl80211_ifindex_msg(drv, ifidx, 0, NL80211_CMD_DEL_INTERFACE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003931 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
3932 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003933 wpa_printf(MSG_ERROR, "Failed to remove interface (ifidx=%d)", ifidx);
3934}
3935
3936
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003937static const char * nl80211_iftype_str(enum nl80211_iftype mode)
3938{
3939 switch (mode) {
3940 case NL80211_IFTYPE_ADHOC:
3941 return "ADHOC";
3942 case NL80211_IFTYPE_STATION:
3943 return "STATION";
3944 case NL80211_IFTYPE_AP:
3945 return "AP";
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003946 case NL80211_IFTYPE_AP_VLAN:
3947 return "AP_VLAN";
3948 case NL80211_IFTYPE_WDS:
3949 return "WDS";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003950 case NL80211_IFTYPE_MONITOR:
3951 return "MONITOR";
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003952 case NL80211_IFTYPE_MESH_POINT:
3953 return "MESH_POINT";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003954 case NL80211_IFTYPE_P2P_CLIENT:
3955 return "P2P_CLIENT";
3956 case NL80211_IFTYPE_P2P_GO:
3957 return "P2P_GO";
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003958 case NL80211_IFTYPE_P2P_DEVICE:
3959 return "P2P_DEVICE";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003960 default:
3961 return "unknown";
3962 }
3963}
3964
3965
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003966static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv,
3967 const char *ifname,
3968 enum nl80211_iftype iftype,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003969 const u8 *addr, int wds,
3970 int (*handler)(struct nl_msg *, void *),
3971 void *arg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003972{
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003973 struct nl_msg *msg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003974 int ifidx;
3975 int ret = -ENOBUFS;
3976
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003977 wpa_printf(MSG_DEBUG, "nl80211: Create interface iftype %d (%s)",
3978 iftype, nl80211_iftype_str(iftype));
3979
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003980 msg = nl80211_cmd_msg(drv->first_bss, 0, NL80211_CMD_NEW_INTERFACE);
3981 if (!msg ||
3982 nla_put_string(msg, NL80211_ATTR_IFNAME, ifname) ||
3983 nla_put_u32(msg, NL80211_ATTR_IFTYPE, iftype))
3984 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003985
3986 if (iftype == NL80211_IFTYPE_MONITOR) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003987 struct nlattr *flags;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003988
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003989 flags = nla_nest_start(msg, NL80211_ATTR_MNTR_FLAGS);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003990 if (!flags ||
3991 nla_put_flag(msg, NL80211_MNTR_FLAG_COOK_FRAMES))
3992 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003993
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003994 nla_nest_end(msg, flags);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003995 } else if (wds) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003996 if (nla_put_u8(msg, NL80211_ATTR_4ADDR, wds))
3997 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003998 }
3999
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07004000 /*
4001 * Tell cfg80211 that the interface belongs to the socket that created
4002 * it, and the interface should be deleted when the socket is closed.
4003 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004004 if (nla_put_flag(msg, NL80211_ATTR_IFACE_SOCKET_OWNER))
4005 goto fail;
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07004006
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004007 ret = send_and_recv_msgs(drv, msg, handler, arg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004008 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004009 if (ret) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004010 fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004011 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004012 wpa_printf(MSG_ERROR, "Failed to create interface %s: %d (%s)",
4013 ifname, ret, strerror(-ret));
4014 return ret;
4015 }
4016
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004017 if (iftype == NL80211_IFTYPE_P2P_DEVICE)
4018 return 0;
4019
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004020 ifidx = if_nametoindex(ifname);
4021 wpa_printf(MSG_DEBUG, "nl80211: New interface %s created: ifindex=%d",
4022 ifname, ifidx);
4023
4024 if (ifidx <= 0)
4025 return -1;
4026
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07004027 /*
4028 * Some virtual interfaces need to process EAPOL packets and events on
4029 * the parent interface. This is used mainly with hostapd.
4030 */
4031 if (drv->hostapd ||
4032 iftype == NL80211_IFTYPE_AP_VLAN ||
4033 iftype == NL80211_IFTYPE_WDS ||
4034 iftype == NL80211_IFTYPE_MONITOR) {
4035 /* start listening for EAPOL on this interface */
4036 add_ifidx(drv, ifidx);
4037 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004038
4039 if (addr && iftype != NL80211_IFTYPE_MONITOR &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004040 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname, addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004041 nl80211_remove_iface(drv, ifidx);
4042 return -1;
4043 }
4044
4045 return ifidx;
4046}
4047
4048
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004049int nl80211_create_iface(struct wpa_driver_nl80211_data *drv,
4050 const char *ifname, enum nl80211_iftype iftype,
4051 const u8 *addr, int wds,
4052 int (*handler)(struct nl_msg *, void *),
4053 void *arg, int use_existing)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004054{
4055 int ret;
4056
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004057 ret = nl80211_create_iface_once(drv, ifname, iftype, addr, wds, handler,
4058 arg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004059
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004060 /* if error occurred and interface exists already */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004061 if (ret == -ENFILE && if_nametoindex(ifname)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004062 if (use_existing) {
4063 wpa_printf(MSG_DEBUG, "nl80211: Continue using existing interface %s",
4064 ifname);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07004065 if (addr && iftype != NL80211_IFTYPE_MONITOR &&
4066 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
4067 addr) < 0 &&
4068 (linux_set_iface_flags(drv->global->ioctl_sock,
4069 ifname, 0) < 0 ||
4070 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
4071 addr) < 0 ||
4072 linux_set_iface_flags(drv->global->ioctl_sock,
4073 ifname, 1) < 0))
4074 return -1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004075 return -ENFILE;
4076 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004077 wpa_printf(MSG_INFO, "Try to remove and re-create %s", ifname);
4078
4079 /* Try to remove the interface that was already there. */
4080 nl80211_remove_iface(drv, if_nametoindex(ifname));
4081
4082 /* Try to create the interface again */
4083 ret = nl80211_create_iface_once(drv, ifname, iftype, addr,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004084 wds, handler, arg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004085 }
4086
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004087 if (ret >= 0 && is_p2p_net_interface(iftype)) {
4088 wpa_printf(MSG_DEBUG,
4089 "nl80211: Interface %s created for P2P - disable 11b rates",
4090 ifname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004091 nl80211_disable_11b_rates(drv, ret, 1);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004092 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004093
4094 return ret;
4095}
4096
4097
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004098static int nl80211_setup_ap(struct i802_bss *bss)
4099{
4100 struct wpa_driver_nl80211_data *drv = bss->drv;
4101
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004102 wpa_printf(MSG_DEBUG, "nl80211: Setup AP(%s) - device_ap_sme=%d use_monitor=%d",
4103 bss->ifname, drv->device_ap_sme, drv->use_monitor);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004104
4105 /*
4106 * Disable Probe Request reporting unless we need it in this way for
4107 * devices that include the AP SME, in the other case (unless using
4108 * monitor iface) we'll get it through the nl_mgmt socket instead.
4109 */
4110 if (!drv->device_ap_sme)
4111 wpa_driver_nl80211_probe_req_report(bss, 0);
4112
4113 if (!drv->device_ap_sme && !drv->use_monitor)
4114 if (nl80211_mgmt_subscribe_ap(bss))
4115 return -1;
4116
4117 if (drv->device_ap_sme && !drv->use_monitor)
4118 if (nl80211_mgmt_subscribe_ap_dev_sme(bss))
4119 return -1;
4120
4121 if (!drv->device_ap_sme && drv->use_monitor &&
4122 nl80211_create_monitor_interface(drv) &&
4123 !drv->device_ap_sme)
Dmitry Shmidt04949592012-07-19 12:16:46 -07004124 return -1;
4125
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004126 if (drv->device_ap_sme &&
4127 wpa_driver_nl80211_probe_req_report(bss, 1) < 0) {
4128 wpa_printf(MSG_DEBUG, "nl80211: Failed to enable "
4129 "Probe Request frame reporting in AP mode");
4130 /* Try to survive without this */
4131 }
4132
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004133 return 0;
4134}
4135
4136
4137static void nl80211_teardown_ap(struct i802_bss *bss)
4138{
4139 struct wpa_driver_nl80211_data *drv = bss->drv;
4140
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004141 wpa_printf(MSG_DEBUG, "nl80211: Teardown AP(%s) - device_ap_sme=%d use_monitor=%d",
4142 bss->ifname, drv->device_ap_sme, drv->use_monitor);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004143 if (drv->device_ap_sme) {
4144 wpa_driver_nl80211_probe_req_report(bss, 0);
4145 if (!drv->use_monitor)
4146 nl80211_mgmt_unsubscribe(bss, "AP teardown (dev SME)");
4147 } else if (drv->use_monitor)
4148 nl80211_remove_monitor_interface(drv);
4149 else
4150 nl80211_mgmt_unsubscribe(bss, "AP teardown");
4151
4152 bss->beacon_set = 0;
4153}
4154
4155
4156static int nl80211_send_eapol_data(struct i802_bss *bss,
4157 const u8 *addr, const u8 *data,
4158 size_t data_len)
4159{
4160 struct sockaddr_ll ll;
4161 int ret;
4162
4163 if (bss->drv->eapol_tx_sock < 0) {
4164 wpa_printf(MSG_DEBUG, "nl80211: No socket to send EAPOL");
4165 return -1;
4166 }
4167
4168 os_memset(&ll, 0, sizeof(ll));
4169 ll.sll_family = AF_PACKET;
4170 ll.sll_ifindex = bss->ifindex;
4171 ll.sll_protocol = htons(ETH_P_PAE);
4172 ll.sll_halen = ETH_ALEN;
4173 os_memcpy(ll.sll_addr, addr, ETH_ALEN);
4174 ret = sendto(bss->drv->eapol_tx_sock, data, data_len, 0,
4175 (struct sockaddr *) &ll, sizeof(ll));
4176 if (ret < 0)
4177 wpa_printf(MSG_ERROR, "nl80211: EAPOL TX: %s",
4178 strerror(errno));
4179
4180 return ret;
4181}
4182
4183
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004184static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
4185
4186static int wpa_driver_nl80211_hapd_send_eapol(
4187 void *priv, const u8 *addr, const u8 *data,
4188 size_t data_len, int encrypt, const u8 *own_addr, u32 flags)
4189{
4190 struct i802_bss *bss = priv;
4191 struct wpa_driver_nl80211_data *drv = bss->drv;
4192 struct ieee80211_hdr *hdr;
4193 size_t len;
4194 u8 *pos;
4195 int res;
4196 int qos = flags & WPA_STA_WMM;
Dmitry Shmidt641185e2013-11-06 15:17:13 -08004197
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004198 if (drv->device_ap_sme || !drv->use_monitor)
4199 return nl80211_send_eapol_data(bss, addr, data, data_len);
4200
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004201 len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
4202 data_len;
4203 hdr = os_zalloc(len);
4204 if (hdr == NULL) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004205 wpa_printf(MSG_INFO, "nl80211: Failed to allocate EAPOL buffer(len=%lu)",
4206 (unsigned long) len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004207 return -1;
4208 }
4209
4210 hdr->frame_control =
4211 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
4212 hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
4213 if (encrypt)
4214 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
4215 if (qos) {
4216 hdr->frame_control |=
4217 host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
4218 }
4219
4220 memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
4221 memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
4222 memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
4223 pos = (u8 *) (hdr + 1);
4224
4225 if (qos) {
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004226 /* Set highest priority in QoS header */
4227 pos[0] = 7;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004228 pos[1] = 0;
4229 pos += 2;
4230 }
4231
4232 memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
4233 pos += sizeof(rfc1042_header);
4234 WPA_PUT_BE16(pos, ETH_P_PAE);
4235 pos += 2;
4236 memcpy(pos, data, data_len);
4237
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004238 res = wpa_driver_nl80211_send_frame(bss, (u8 *) hdr, len, encrypt, 0,
4239 0, 0, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004240 if (res < 0) {
4241 wpa_printf(MSG_ERROR, "i802_send_eapol - packet len: %lu - "
4242 "failed: %d (%s)",
4243 (unsigned long) len, errno, strerror(errno));
4244 }
4245 os_free(hdr);
4246
4247 return res;
4248}
4249
4250
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004251static int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr,
4252 int total_flags,
4253 int flags_or, int flags_and)
4254{
4255 struct i802_bss *bss = priv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004256 struct nl_msg *msg;
4257 struct nlattr *flags;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004258 struct nl80211_sta_flag_update upd;
4259
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004260 wpa_printf(MSG_DEBUG, "nl80211: Set STA flags - ifname=%s addr=" MACSTR
4261 " total_flags=0x%x flags_or=0x%x flags_and=0x%x authorized=%d",
4262 bss->ifname, MAC2STR(addr), total_flags, flags_or, flags_and,
4263 !!(total_flags & WPA_STA_AUTHORIZED));
4264
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004265 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
4266 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
4267 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004268
4269 /*
4270 * Backwards compatibility version using NL80211_ATTR_STA_FLAGS. This
4271 * can be removed eventually.
4272 */
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004273 flags = nla_nest_start(msg, NL80211_ATTR_STA_FLAGS);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004274 if (!flags ||
4275 ((total_flags & WPA_STA_AUTHORIZED) &&
4276 nla_put_flag(msg, NL80211_STA_FLAG_AUTHORIZED)) ||
4277 ((total_flags & WPA_STA_WMM) &&
4278 nla_put_flag(msg, NL80211_STA_FLAG_WME)) ||
4279 ((total_flags & WPA_STA_SHORT_PREAMBLE) &&
4280 nla_put_flag(msg, NL80211_STA_FLAG_SHORT_PREAMBLE)) ||
4281 ((total_flags & WPA_STA_MFP) &&
4282 nla_put_flag(msg, NL80211_STA_FLAG_MFP)) ||
4283 ((total_flags & WPA_STA_TDLS_PEER) &&
4284 nla_put_flag(msg, NL80211_STA_FLAG_TDLS_PEER)))
4285 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004286
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004287 nla_nest_end(msg, flags);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004288
4289 os_memset(&upd, 0, sizeof(upd));
4290 upd.mask = sta_flags_nl80211(flags_or | ~flags_and);
4291 upd.set = sta_flags_nl80211(flags_or);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004292 if (nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd))
4293 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004294
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004295 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
4296fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004297 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004298 return -ENOBUFS;
4299}
4300
4301
4302static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv,
4303 struct wpa_driver_associate_params *params)
4304{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004305 enum nl80211_iftype nlmode, old_mode;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004306
4307 if (params->p2p) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004308 wpa_printf(MSG_DEBUG, "nl80211: Setup AP operations for P2P "
4309 "group (GO)");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004310 nlmode = NL80211_IFTYPE_P2P_GO;
4311 } else
4312 nlmode = NL80211_IFTYPE_AP;
4313
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004314 old_mode = drv->nlmode;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004315 if (wpa_driver_nl80211_set_mode(drv->first_bss, nlmode)) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004316 nl80211_remove_monitor_interface(drv);
4317 return -1;
4318 }
4319
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08004320 if (params->freq.freq &&
4321 nl80211_set_channel(drv->first_bss, &params->freq, 0)) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004322 if (old_mode != nlmode)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004323 wpa_driver_nl80211_set_mode(drv->first_bss, old_mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004324 nl80211_remove_monitor_interface(drv);
4325 return -1;
4326 }
4327
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004328 return 0;
4329}
4330
4331
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004332static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv,
4333 int reset_mode)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004334{
4335 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004336 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004337
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004338 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_LEAVE_IBSS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004339 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004340 if (ret) {
4341 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS failed: ret=%d "
4342 "(%s)", ret, strerror(-ret));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004343 } else {
4344 wpa_printf(MSG_DEBUG,
4345 "nl80211: Leave IBSS request sent successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004346 }
4347
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004348 if (reset_mode &&
4349 wpa_driver_nl80211_set_mode(drv->first_bss,
Dmitry Shmidt56052862013-10-04 10:23:25 -07004350 NL80211_IFTYPE_STATION)) {
4351 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
4352 "station mode");
4353 }
4354
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004355 return ret;
4356}
4357
4358
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08004359static int nl80211_ht_vht_overrides(struct nl_msg *msg,
4360 struct wpa_driver_associate_params *params)
4361{
4362 if (params->disable_ht && nla_put_flag(msg, NL80211_ATTR_DISABLE_HT))
4363 return -1;
4364
4365 if (params->htcaps && params->htcaps_mask) {
4366 int sz = sizeof(struct ieee80211_ht_capabilities);
4367 wpa_hexdump(MSG_DEBUG, " * htcaps", params->htcaps, sz);
4368 wpa_hexdump(MSG_DEBUG, " * htcaps_mask",
4369 params->htcaps_mask, sz);
4370 if (nla_put(msg, NL80211_ATTR_HT_CAPABILITY, sz,
4371 params->htcaps) ||
4372 nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK, sz,
4373 params->htcaps_mask))
4374 return -1;
4375 }
4376
4377#ifdef CONFIG_VHT_OVERRIDES
4378 if (params->disable_vht) {
4379 wpa_printf(MSG_DEBUG, " * VHT disabled");
4380 if (nla_put_flag(msg, NL80211_ATTR_DISABLE_VHT))
4381 return -1;
4382 }
4383
4384 if (params->vhtcaps && params->vhtcaps_mask) {
4385 int sz = sizeof(struct ieee80211_vht_capabilities);
4386 wpa_hexdump(MSG_DEBUG, " * vhtcaps", params->vhtcaps, sz);
4387 wpa_hexdump(MSG_DEBUG, " * vhtcaps_mask",
4388 params->vhtcaps_mask, sz);
4389 if (nla_put(msg, NL80211_ATTR_VHT_CAPABILITY, sz,
4390 params->vhtcaps) ||
4391 nla_put(msg, NL80211_ATTR_VHT_CAPABILITY_MASK, sz,
4392 params->vhtcaps_mask))
4393 return -1;
4394 }
4395#endif /* CONFIG_VHT_OVERRIDES */
4396
4397 return 0;
4398}
4399
4400
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004401static int wpa_driver_nl80211_ibss(struct wpa_driver_nl80211_data *drv,
4402 struct wpa_driver_associate_params *params)
4403{
4404 struct nl_msg *msg;
4405 int ret = -1;
4406 int count = 0;
4407
4408 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS (ifindex=%d)", drv->ifindex);
4409
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004410 if (wpa_driver_nl80211_set_mode_ibss(drv->first_bss, &params->freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004411 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
4412 "IBSS mode");
4413 return -1;
4414 }
4415
4416retry:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004417 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_JOIN_IBSS)) ||
4418 params->ssid == NULL || params->ssid_len > sizeof(drv->ssid))
4419 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004420
4421 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
4422 params->ssid, params->ssid_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004423 if (nla_put(msg, NL80211_ATTR_SSID, params->ssid_len, params->ssid))
4424 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004425 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
4426 drv->ssid_len = params->ssid_len;
4427
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004428 if (nl80211_put_freq_params(msg, &params->freq) < 0 ||
4429 nl80211_put_beacon_int(msg, params->beacon_int))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004430 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004431
4432 ret = nl80211_set_conn_keys(params, msg);
4433 if (ret)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004434 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004435
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004436 if (params->bssid && params->fixed_bssid) {
4437 wpa_printf(MSG_DEBUG, " * BSSID=" MACSTR,
4438 MAC2STR(params->bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004439 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid))
4440 goto fail;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004441 }
4442
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004443 if (params->fixed_freq) {
4444 wpa_printf(MSG_DEBUG, " * fixed_freq");
4445 if (nla_put_flag(msg, NL80211_ATTR_FREQ_FIXED))
4446 goto fail;
4447 }
4448
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004449 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
4450 params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
4451 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
4452 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004453 wpa_printf(MSG_DEBUG, " * control port");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004454 if (nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT))
4455 goto fail;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004456 }
4457
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004458 if (params->wpa_ie) {
4459 wpa_hexdump(MSG_DEBUG,
4460 " * Extra IEs for Beacon/Probe Response frames",
4461 params->wpa_ie, params->wpa_ie_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004462 if (nla_put(msg, NL80211_ATTR_IE, params->wpa_ie_len,
4463 params->wpa_ie))
4464 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004465 }
4466
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08004467 if (nl80211_ht_vht_overrides(msg, params) < 0)
4468 return -1;
4469
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004470 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4471 msg = NULL;
4472 if (ret) {
4473 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS failed: ret=%d (%s)",
4474 ret, strerror(-ret));
4475 count++;
4476 if (ret == -EALREADY && count == 1) {
4477 wpa_printf(MSG_DEBUG, "nl80211: Retry IBSS join after "
4478 "forced leave");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004479 nl80211_leave_ibss(drv, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004480 nlmsg_free(msg);
4481 goto retry;
4482 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004483 } else {
4484 wpa_printf(MSG_DEBUG,
4485 "nl80211: Join IBSS request sent successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004486 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004487
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004488fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004489 nlmsg_free(msg);
4490 return ret;
4491}
4492
4493
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004494static int nl80211_connect_common(struct wpa_driver_nl80211_data *drv,
4495 struct wpa_driver_associate_params *params,
4496 struct nl_msg *msg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004497{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004498 if (params->bssid) {
4499 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
4500 MAC2STR(params->bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004501 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid))
4502 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004503 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004504
Dmitry Shmidt96be6222014-02-13 10:16:51 -08004505 if (params->bssid_hint) {
4506 wpa_printf(MSG_DEBUG, " * bssid_hint=" MACSTR,
4507 MAC2STR(params->bssid_hint));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004508 if (nla_put(msg, NL80211_ATTR_MAC_HINT, ETH_ALEN,
4509 params->bssid_hint))
4510 return -1;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08004511 }
4512
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004513 if (params->freq.freq) {
4514 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq.freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004515 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
4516 params->freq.freq))
4517 return -1;
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004518 drv->assoc_freq = params->freq.freq;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004519 } else
4520 drv->assoc_freq = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004521
Dmitry Shmidt96be6222014-02-13 10:16:51 -08004522 if (params->freq_hint) {
4523 wpa_printf(MSG_DEBUG, " * freq_hint=%d", params->freq_hint);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004524 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ_HINT,
4525 params->freq_hint))
4526 return -1;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08004527 }
4528
Dmitry Shmidt04949592012-07-19 12:16:46 -07004529 if (params->bg_scan_period >= 0) {
4530 wpa_printf(MSG_DEBUG, " * bg scan period=%d",
4531 params->bg_scan_period);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004532 if (nla_put_u16(msg, NL80211_ATTR_BG_SCAN_PERIOD,
4533 params->bg_scan_period))
4534 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004535 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004536
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004537 if (params->ssid) {
4538 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
4539 params->ssid, params->ssid_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004540 if (nla_put(msg, NL80211_ATTR_SSID, params->ssid_len,
4541 params->ssid))
4542 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004543 if (params->ssid_len > sizeof(drv->ssid))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004544 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004545 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
4546 drv->ssid_len = params->ssid_len;
4547 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004548
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004549 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004550 if (params->wpa_ie &&
4551 nla_put(msg, NL80211_ATTR_IE, params->wpa_ie_len, params->wpa_ie))
4552 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004553
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004554 if (params->wpa_proto) {
4555 enum nl80211_wpa_versions ver = 0;
4556
4557 if (params->wpa_proto & WPA_PROTO_WPA)
4558 ver |= NL80211_WPA_VERSION_1;
4559 if (params->wpa_proto & WPA_PROTO_RSN)
4560 ver |= NL80211_WPA_VERSION_2;
4561
4562 wpa_printf(MSG_DEBUG, " * WPA Versions 0x%x", ver);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004563 if (nla_put_u32(msg, NL80211_ATTR_WPA_VERSIONS, ver))
4564 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004565 }
4566
4567 if (params->pairwise_suite != WPA_CIPHER_NONE) {
4568 u32 cipher = wpa_cipher_to_cipher_suite(params->pairwise_suite);
4569 wpa_printf(MSG_DEBUG, " * pairwise=0x%x", cipher);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004570 if (nla_put_u32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
4571 cipher))
4572 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004573 }
4574
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08004575 if (params->group_suite == WPA_CIPHER_GTK_NOT_USED &&
4576 !(drv->capa.enc & WPA_DRIVER_CAPA_ENC_GTK_NOT_USED)) {
4577 /*
4578 * This is likely to work even though many drivers do not
4579 * advertise support for operations without GTK.
4580 */
4581 wpa_printf(MSG_DEBUG, " * skip group cipher configuration for GTK_NOT_USED due to missing driver support advertisement");
4582 } else if (params->group_suite != WPA_CIPHER_NONE) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004583 u32 cipher = wpa_cipher_to_cipher_suite(params->group_suite);
4584 wpa_printf(MSG_DEBUG, " * group=0x%x", cipher);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004585 if (nla_put_u32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher))
4586 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004587 }
4588
4589 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
4590 params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
4591 params->key_mgmt_suite == WPA_KEY_MGMT_FT_IEEE8021X ||
4592 params->key_mgmt_suite == WPA_KEY_MGMT_FT_PSK ||
Dmitry Shmidt15907092014-03-25 10:42:57 -07004593 params->key_mgmt_suite == WPA_KEY_MGMT_CCKM ||
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -07004594 params->key_mgmt_suite == WPA_KEY_MGMT_OSEN ||
4595 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004596 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256 ||
Dmitry Shmidt807291d2015-01-27 13:40:23 -08004597 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SUITE_B ||
4598 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004599 int mgmt = WLAN_AKM_SUITE_PSK;
4600
4601 switch (params->key_mgmt_suite) {
4602 case WPA_KEY_MGMT_CCKM:
4603 mgmt = WLAN_AKM_SUITE_CCKM;
4604 break;
4605 case WPA_KEY_MGMT_IEEE8021X:
4606 mgmt = WLAN_AKM_SUITE_8021X;
4607 break;
4608 case WPA_KEY_MGMT_FT_IEEE8021X:
4609 mgmt = WLAN_AKM_SUITE_FT_8021X;
4610 break;
4611 case WPA_KEY_MGMT_FT_PSK:
4612 mgmt = WLAN_AKM_SUITE_FT_PSK;
4613 break;
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -07004614 case WPA_KEY_MGMT_IEEE8021X_SHA256:
4615 mgmt = WLAN_AKM_SUITE_8021X_SHA256;
4616 break;
4617 case WPA_KEY_MGMT_PSK_SHA256:
4618 mgmt = WLAN_AKM_SUITE_PSK_SHA256;
4619 break;
Dmitry Shmidt15907092014-03-25 10:42:57 -07004620 case WPA_KEY_MGMT_OSEN:
4621 mgmt = WLAN_AKM_SUITE_OSEN;
4622 break;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004623 case WPA_KEY_MGMT_IEEE8021X_SUITE_B:
4624 mgmt = WLAN_AKM_SUITE_8021X_SUITE_B;
4625 break;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08004626 case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192:
4627 mgmt = WLAN_AKM_SUITE_8021X_SUITE_B_192;
4628 break;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004629 case WPA_KEY_MGMT_PSK:
4630 default:
4631 mgmt = WLAN_AKM_SUITE_PSK;
4632 break;
4633 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07004634 wpa_printf(MSG_DEBUG, " * akm=0x%x", mgmt);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004635 if (nla_put_u32(msg, NL80211_ATTR_AKM_SUITES, mgmt))
4636 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004637 }
4638
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004639 if (nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT))
4640 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004641
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004642 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED &&
4643 nla_put_u32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED))
4644 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004645
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004646 if (params->rrm_used) {
4647 u32 drv_rrm_flags = drv->capa.rrm_flags;
4648 if (!(drv_rrm_flags &
4649 WPA_DRIVER_FLAGS_DS_PARAM_SET_IE_IN_PROBES) ||
4650 !(drv_rrm_flags & WPA_DRIVER_FLAGS_QUIET) ||
4651 nla_put_flag(msg, NL80211_ATTR_USE_RRM))
4652 return -1;
4653 }
4654
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08004655 if (nl80211_ht_vht_overrides(msg, params) < 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004656 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004657
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004658 if (params->p2p)
4659 wpa_printf(MSG_DEBUG, " * P2P group");
4660
4661 return 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004662}
4663
4664
4665static int wpa_driver_nl80211_try_connect(
4666 struct wpa_driver_nl80211_data *drv,
4667 struct wpa_driver_associate_params *params)
4668{
4669 struct nl_msg *msg;
4670 enum nl80211_auth_type type;
4671 int ret;
4672 int algs;
4673
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004674 if (params->req_key_mgmt_offload && params->psk &&
4675 (params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
4676 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256 ||
4677 params->key_mgmt_suite == WPA_KEY_MGMT_FT_PSK)) {
4678 wpa_printf(MSG_DEBUG, "nl80211: Key management set PSK");
4679 ret = issue_key_mgmt_set_key(drv, params->psk, 32);
4680 if (ret)
4681 return ret;
4682 }
4683
4684 wpa_printf(MSG_DEBUG, "nl80211: Connect (ifindex=%d)", drv->ifindex);
4685 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_CONNECT);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004686 if (!msg)
4687 return -1;
4688
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004689 ret = nl80211_connect_common(drv, params, msg);
4690 if (ret)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004691 goto fail;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004692
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004693 algs = 0;
4694 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
4695 algs++;
4696 if (params->auth_alg & WPA_AUTH_ALG_SHARED)
4697 algs++;
4698 if (params->auth_alg & WPA_AUTH_ALG_LEAP)
4699 algs++;
4700 if (algs > 1) {
4701 wpa_printf(MSG_DEBUG, " * Leave out Auth Type for automatic "
4702 "selection");
4703 goto skip_auth_type;
4704 }
4705
4706 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
4707 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
4708 else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
4709 type = NL80211_AUTHTYPE_SHARED_KEY;
4710 else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
4711 type = NL80211_AUTHTYPE_NETWORK_EAP;
4712 else if (params->auth_alg & WPA_AUTH_ALG_FT)
4713 type = NL80211_AUTHTYPE_FT;
4714 else
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004715 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004716
4717 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004718 if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE, type))
4719 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004720
4721skip_auth_type:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004722 ret = nl80211_set_conn_keys(params, msg);
4723 if (ret)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004724 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004725
4726 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4727 msg = NULL;
4728 if (ret) {
4729 wpa_printf(MSG_DEBUG, "nl80211: MLME connect failed: ret=%d "
4730 "(%s)", ret, strerror(-ret));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004731 } else {
4732 wpa_printf(MSG_DEBUG,
4733 "nl80211: Connect request send successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004734 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004735
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004736fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004737 nlmsg_free(msg);
4738 return ret;
4739
4740}
4741
4742
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004743static int wpa_driver_nl80211_connect(
4744 struct wpa_driver_nl80211_data *drv,
4745 struct wpa_driver_associate_params *params)
4746{
Jithu Jancea7c60b42014-12-03 18:54:40 +05304747 int ret;
4748
4749 /* Store the connection attempted bssid for future use */
4750 if (params->bssid)
4751 os_memcpy(drv->auth_attempt_bssid, params->bssid, ETH_ALEN);
4752 else
4753 os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
4754
4755 ret = wpa_driver_nl80211_try_connect(drv, params);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004756 if (ret == -EALREADY) {
4757 /*
4758 * cfg80211 does not currently accept new connections if
4759 * we are already connected. As a workaround, force
4760 * disconnection and try again.
4761 */
4762 wpa_printf(MSG_DEBUG, "nl80211: Explicitly "
4763 "disconnecting before reassociation "
4764 "attempt");
4765 if (wpa_driver_nl80211_disconnect(
4766 drv, WLAN_REASON_PREV_AUTH_NOT_VALID))
4767 return -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004768 ret = wpa_driver_nl80211_try_connect(drv, params);
4769 }
4770 return ret;
4771}
4772
4773
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004774static int wpa_driver_nl80211_associate(
4775 void *priv, struct wpa_driver_associate_params *params)
4776{
4777 struct i802_bss *bss = priv;
4778 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004779 int ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004780 struct nl_msg *msg;
4781
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004782 nl80211_unmask_11b_rates(bss);
4783
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004784 if (params->mode == IEEE80211_MODE_AP)
4785 return wpa_driver_nl80211_ap(drv, params);
4786
4787 if (params->mode == IEEE80211_MODE_IBSS)
4788 return wpa_driver_nl80211_ibss(drv, params);
4789
4790 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004791 enum nl80211_iftype nlmode = params->p2p ?
4792 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
4793
4794 if (wpa_driver_nl80211_set_mode(priv, nlmode) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004795 return -1;
4796 return wpa_driver_nl80211_connect(drv, params);
4797 }
4798
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004799 nl80211_mark_disconnected(drv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004800
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004801 wpa_printf(MSG_DEBUG, "nl80211: Associate (ifindex=%d)",
4802 drv->ifindex);
4803 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_ASSOCIATE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004804 if (!msg)
4805 return -1;
4806
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004807 ret = nl80211_connect_common(drv, params, msg);
4808 if (ret)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004809 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004810
4811 if (params->prev_bssid) {
4812 wpa_printf(MSG_DEBUG, " * prev_bssid=" MACSTR,
4813 MAC2STR(params->prev_bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004814 if (nla_put(msg, NL80211_ATTR_PREV_BSSID, ETH_ALEN,
4815 params->prev_bssid))
4816 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004817 }
4818
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004819 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4820 msg = NULL;
4821 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004822 wpa_dbg(drv->ctx, MSG_DEBUG,
4823 "nl80211: MLME command failed (assoc): ret=%d (%s)",
4824 ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004825 nl80211_dump_scan(drv);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004826 } else {
4827 wpa_printf(MSG_DEBUG,
4828 "nl80211: Association request send successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004829 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004830
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004831fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004832 nlmsg_free(msg);
4833 return ret;
4834}
4835
4836
4837static int nl80211_set_mode(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004838 int ifindex, enum nl80211_iftype mode)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004839{
4840 struct nl_msg *msg;
4841 int ret = -ENOBUFS;
4842
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004843 wpa_printf(MSG_DEBUG, "nl80211: Set mode ifindex %d iftype %d (%s)",
4844 ifindex, mode, nl80211_iftype_str(mode));
4845
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004846 msg = nl80211_cmd_msg(drv->first_bss, 0, NL80211_CMD_SET_INTERFACE);
4847 if (!msg || nla_put_u32(msg, NL80211_ATTR_IFTYPE, mode))
4848 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004849
4850 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004851 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004852 if (!ret)
4853 return 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004854fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004855 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004856 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface %d to mode %d:"
4857 " %d (%s)", ifindex, mode, ret, strerror(-ret));
4858 return ret;
4859}
4860
4861
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004862static int wpa_driver_nl80211_set_mode_impl(
4863 struct i802_bss *bss,
4864 enum nl80211_iftype nlmode,
4865 struct hostapd_freq_params *desired_freq_params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004866{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004867 struct wpa_driver_nl80211_data *drv = bss->drv;
4868 int ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004869 int i;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004870 int was_ap = is_ap_interface(drv->nlmode);
4871 int res;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004872 int mode_switch_res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004873
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004874 mode_switch_res = nl80211_set_mode(drv, drv->ifindex, nlmode);
4875 if (mode_switch_res && nlmode == nl80211_get_ifmode(bss))
4876 mode_switch_res = 0;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004877
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004878 if (mode_switch_res == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004879 drv->nlmode = nlmode;
4880 ret = 0;
4881 goto done;
4882 }
4883
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004884 if (mode_switch_res == -ENODEV)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004885 return -1;
4886
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004887 if (nlmode == drv->nlmode) {
4888 wpa_printf(MSG_DEBUG, "nl80211: Interface already in "
4889 "requested mode - ignore error");
4890 ret = 0;
4891 goto done; /* Already in the requested mode */
4892 }
4893
4894 /* mac80211 doesn't allow mode changes while the device is up, so
4895 * take the device down, try to set the mode again, and bring the
4896 * device back up.
4897 */
4898 wpa_printf(MSG_DEBUG, "nl80211: Try mode change after setting "
4899 "interface down");
4900 for (i = 0; i < 10; i++) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004901 res = i802_set_iface_flags(bss, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004902 if (res == -EACCES || res == -ENODEV)
4903 break;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004904 if (res != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004905 wpa_printf(MSG_DEBUG, "nl80211: Failed to set "
4906 "interface down");
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004907 os_sleep(0, 100000);
4908 continue;
4909 }
4910
4911 /*
4912 * Setting the mode will fail for some drivers if the phy is
4913 * on a frequency that the mode is disallowed in.
4914 */
4915 if (desired_freq_params) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004916 res = nl80211_set_channel(bss, desired_freq_params, 0);
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004917 if (res) {
4918 wpa_printf(MSG_DEBUG,
4919 "nl80211: Failed to set frequency on interface");
4920 }
4921 }
4922
4923 /* Try to set the mode again while the interface is down */
4924 mode_switch_res = nl80211_set_mode(drv, drv->ifindex, nlmode);
4925 if (mode_switch_res == -EBUSY) {
4926 wpa_printf(MSG_DEBUG,
4927 "nl80211: Delaying mode set while interface going down");
4928 os_sleep(0, 100000);
4929 continue;
4930 }
4931 ret = mode_switch_res;
4932 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004933 }
4934
4935 if (!ret) {
4936 wpa_printf(MSG_DEBUG, "nl80211: Mode change succeeded while "
4937 "interface is down");
4938 drv->nlmode = nlmode;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004939 drv->ignore_if_down_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004940 }
4941
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004942 /* Bring the interface back up */
4943 res = linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1);
4944 if (res != 0) {
4945 wpa_printf(MSG_DEBUG,
4946 "nl80211: Failed to set interface up after switching mode");
4947 ret = -1;
4948 }
4949
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004950done:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004951 if (ret) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004952 wpa_printf(MSG_DEBUG, "nl80211: Interface mode change to %d "
4953 "from %d failed", nlmode, drv->nlmode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004954 return ret;
4955 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004956
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004957 if (is_p2p_net_interface(nlmode)) {
4958 wpa_printf(MSG_DEBUG,
4959 "nl80211: Interface %s mode change to P2P - disable 11b rates",
4960 bss->ifname);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004961 nl80211_disable_11b_rates(drv, drv->ifindex, 1);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004962 } else if (drv->disabled_11b_rates) {
4963 wpa_printf(MSG_DEBUG,
4964 "nl80211: Interface %s mode changed to non-P2P - re-enable 11b rates",
4965 bss->ifname);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004966 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004967 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004968
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004969 if (is_ap_interface(nlmode)) {
4970 nl80211_mgmt_unsubscribe(bss, "start AP");
4971 /* Setup additional AP mode functionality if needed */
4972 if (nl80211_setup_ap(bss))
4973 return -1;
4974 } else if (was_ap) {
4975 /* Remove additional AP mode functionality */
4976 nl80211_teardown_ap(bss);
4977 } else {
4978 nl80211_mgmt_unsubscribe(bss, "mode change");
4979 }
4980
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004981 if (is_mesh_interface(nlmode) &&
4982 nl80211_mgmt_subscribe_mesh(bss))
4983 return -1;
4984
Dmitry Shmidt04949592012-07-19 12:16:46 -07004985 if (!bss->in_deinit && !is_ap_interface(nlmode) &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004986 !is_mesh_interface(nlmode) &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004987 nl80211_mgmt_subscribe_non_ap(bss) < 0)
4988 wpa_printf(MSG_DEBUG, "nl80211: Failed to register Action "
4989 "frame processing - ignore for now");
4990
4991 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004992}
4993
4994
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004995int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
4996 enum nl80211_iftype nlmode)
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004997{
4998 return wpa_driver_nl80211_set_mode_impl(bss, nlmode, NULL);
4999}
5000
5001
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07005002static int wpa_driver_nl80211_set_mode_ibss(struct i802_bss *bss,
5003 struct hostapd_freq_params *freq)
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005004{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005005 return wpa_driver_nl80211_set_mode_impl(bss, NL80211_IFTYPE_ADHOC,
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07005006 freq);
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005007}
5008
5009
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005010static int wpa_driver_nl80211_get_capa(void *priv,
5011 struct wpa_driver_capa *capa)
5012{
5013 struct i802_bss *bss = priv;
5014 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07005015
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005016 if (!drv->has_capability)
5017 return -1;
5018 os_memcpy(capa, &drv->capa, sizeof(*capa));
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005019 if (drv->extended_capa && drv->extended_capa_mask) {
5020 capa->extended_capa = drv->extended_capa;
5021 capa->extended_capa_mask = drv->extended_capa_mask;
5022 capa->extended_capa_len = drv->extended_capa_len;
5023 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005024
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005025 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005026}
5027
5028
5029static int wpa_driver_nl80211_set_operstate(void *priv, int state)
5030{
5031 struct i802_bss *bss = priv;
5032 struct wpa_driver_nl80211_data *drv = bss->drv;
5033
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005034 wpa_printf(MSG_DEBUG, "nl80211: Set %s operstate %d->%d (%s)",
5035 bss->ifname, drv->operstate, state,
5036 state ? "UP" : "DORMANT");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005037 drv->operstate = state;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005038 return netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, -1,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005039 state ? IF_OPER_UP : IF_OPER_DORMANT);
5040}
5041
5042
5043static int wpa_driver_nl80211_set_supp_port(void *priv, int authorized)
5044{
5045 struct i802_bss *bss = priv;
5046 struct wpa_driver_nl80211_data *drv = bss->drv;
5047 struct nl_msg *msg;
5048 struct nl80211_sta_flag_update upd;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005049 int ret;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005050
5051 if (!drv->associated && is_zero_ether_addr(drv->bssid) && !authorized) {
5052 wpa_printf(MSG_DEBUG, "nl80211: Skip set_supp_port(unauthorized) while not associated");
5053 return 0;
5054 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005055
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005056 wpa_printf(MSG_DEBUG, "nl80211: Set supplicant port %sauthorized for "
5057 MACSTR, authorized ? "" : "un", MAC2STR(drv->bssid));
5058
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005059 os_memset(&upd, 0, sizeof(upd));
5060 upd.mask = BIT(NL80211_STA_FLAG_AUTHORIZED);
5061 if (authorized)
5062 upd.set = BIT(NL80211_STA_FLAG_AUTHORIZED);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005063
5064 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
5065 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid) ||
5066 nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd)) {
5067 nlmsg_free(msg);
5068 return -ENOBUFS;
5069 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005070
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005071 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005072 if (!ret)
5073 return 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005074 wpa_printf(MSG_DEBUG, "nl80211: Failed to set STA flag: %d (%s)",
5075 ret, strerror(-ret));
5076 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005077}
5078
5079
Jouni Malinen75ecf522011-06-27 15:19:46 -07005080/* Set kernel driver on given frequency (MHz) */
5081static int i802_set_freq(void *priv, struct hostapd_freq_params *freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005082{
Jouni Malinen75ecf522011-06-27 15:19:46 -07005083 struct i802_bss *bss = priv;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07005084 return nl80211_set_channel(bss, freq, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005085}
5086
5087
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005088static inline int min_int(int a, int b)
5089{
5090 if (a < b)
5091 return a;
5092 return b;
5093}
5094
5095
5096static int get_key_handler(struct nl_msg *msg, void *arg)
5097{
5098 struct nlattr *tb[NL80211_ATTR_MAX + 1];
5099 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
5100
5101 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
5102 genlmsg_attrlen(gnlh, 0), NULL);
5103
5104 /*
5105 * TODO: validate the key index and mac address!
5106 * Otherwise, there's a race condition as soon as
5107 * the kernel starts sending key notifications.
5108 */
5109
5110 if (tb[NL80211_ATTR_KEY_SEQ])
5111 memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]),
5112 min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6));
5113 return NL_SKIP;
5114}
5115
5116
5117static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
5118 int idx, u8 *seq)
5119{
5120 struct i802_bss *bss = priv;
5121 struct wpa_driver_nl80211_data *drv = bss->drv;
5122 struct nl_msg *msg;
5123
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005124 msg = nl80211_ifindex_msg(drv, if_nametoindex(iface), 0,
5125 NL80211_CMD_GET_KEY);
5126 if (!msg ||
5127 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
5128 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, idx)) {
5129 nlmsg_free(msg);
5130 return -ENOBUFS;
5131 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005132
5133 memset(seq, 0, 6);
5134
5135 return send_and_recv_msgs(drv, msg, get_key_handler, seq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005136}
5137
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005138
5139static int i802_set_rts(void *priv, int rts)
5140{
5141 struct i802_bss *bss = priv;
5142 struct wpa_driver_nl80211_data *drv = bss->drv;
5143 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005144 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005145 u32 val;
5146
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005147 if (rts >= 2347)
5148 val = (u32) -1;
5149 else
5150 val = rts;
5151
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005152 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_WIPHY)) ||
5153 nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD, val)) {
5154 nlmsg_free(msg);
5155 return -ENOBUFS;
5156 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005157
5158 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5159 if (!ret)
5160 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005161 wpa_printf(MSG_DEBUG, "nl80211: Failed to set RTS threshold %d: "
5162 "%d (%s)", rts, ret, strerror(-ret));
5163 return ret;
5164}
5165
5166
5167static int i802_set_frag(void *priv, int frag)
5168{
5169 struct i802_bss *bss = priv;
5170 struct wpa_driver_nl80211_data *drv = bss->drv;
5171 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005172 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005173 u32 val;
5174
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005175 if (frag >= 2346)
5176 val = (u32) -1;
5177 else
5178 val = frag;
5179
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005180 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_WIPHY)) ||
5181 nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD, val)) {
5182 nlmsg_free(msg);
5183 return -ENOBUFS;
5184 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005185
5186 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5187 if (!ret)
5188 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005189 wpa_printf(MSG_DEBUG, "nl80211: Failed to set fragmentation threshold "
5190 "%d: %d (%s)", frag, ret, strerror(-ret));
5191 return ret;
5192}
5193
5194
5195static int i802_flush(void *priv)
5196{
5197 struct i802_bss *bss = priv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005198 struct nl_msg *msg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005199 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005200
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005201 wpa_printf(MSG_DEBUG, "nl80211: flush -> DEL_STATION %s (all)",
5202 bss->ifname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005203
5204 /*
5205 * XXX: FIX! this needs to flush all VLANs too
5206 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005207 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_DEL_STATION);
5208 res = send_and_recv_msgs(bss->drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005209 if (res) {
5210 wpa_printf(MSG_DEBUG, "nl80211: Station flush failed: ret=%d "
5211 "(%s)", res, strerror(-res));
5212 }
5213 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005214}
5215
5216
5217static int get_sta_handler(struct nl_msg *msg, void *arg)
5218{
5219 struct nlattr *tb[NL80211_ATTR_MAX + 1];
5220 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
5221 struct hostap_sta_driver_data *data = arg;
5222 struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
5223 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
5224 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
5225 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
5226 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
5227 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
5228 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03005229 [NL80211_STA_INFO_TX_FAILED] = { .type = NLA_U32 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005230 };
5231
5232 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
5233 genlmsg_attrlen(gnlh, 0), NULL);
5234
5235 /*
5236 * TODO: validate the interface and mac address!
5237 * Otherwise, there's a race condition as soon as
5238 * the kernel starts sending station notifications.
5239 */
5240
5241 if (!tb[NL80211_ATTR_STA_INFO]) {
5242 wpa_printf(MSG_DEBUG, "sta stats missing!");
5243 return NL_SKIP;
5244 }
5245 if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
5246 tb[NL80211_ATTR_STA_INFO],
5247 stats_policy)) {
5248 wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
5249 return NL_SKIP;
5250 }
5251
5252 if (stats[NL80211_STA_INFO_INACTIVE_TIME])
5253 data->inactive_msec =
5254 nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
5255 if (stats[NL80211_STA_INFO_RX_BYTES])
5256 data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
5257 if (stats[NL80211_STA_INFO_TX_BYTES])
5258 data->tx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
5259 if (stats[NL80211_STA_INFO_RX_PACKETS])
5260 data->rx_packets =
5261 nla_get_u32(stats[NL80211_STA_INFO_RX_PACKETS]);
5262 if (stats[NL80211_STA_INFO_TX_PACKETS])
5263 data->tx_packets =
5264 nla_get_u32(stats[NL80211_STA_INFO_TX_PACKETS]);
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03005265 if (stats[NL80211_STA_INFO_TX_FAILED])
5266 data->tx_retry_failed =
5267 nla_get_u32(stats[NL80211_STA_INFO_TX_FAILED]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005268
5269 return NL_SKIP;
5270}
5271
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005272static int i802_read_sta_data(struct i802_bss *bss,
5273 struct hostap_sta_driver_data *data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005274 const u8 *addr)
5275{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005276 struct nl_msg *msg;
5277
5278 os_memset(data, 0, sizeof(*data));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005279
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005280 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_GET_STATION)) ||
5281 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
5282 nlmsg_free(msg);
5283 return -ENOBUFS;
5284 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005285
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005286 return send_and_recv_msgs(bss->drv, msg, get_sta_handler, data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005287}
5288
5289
5290static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
5291 int cw_min, int cw_max, int burst_time)
5292{
5293 struct i802_bss *bss = priv;
5294 struct wpa_driver_nl80211_data *drv = bss->drv;
5295 struct nl_msg *msg;
5296 struct nlattr *txq, *params;
5297
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005298 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005299 if (!msg)
5300 return -1;
5301
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005302 txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS);
5303 if (!txq)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005304 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005305
5306 /* We are only sending parameters for a single TXQ at a time */
5307 params = nla_nest_start(msg, 1);
5308 if (!params)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005309 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005310
5311 switch (queue) {
5312 case 0:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005313 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VO))
5314 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005315 break;
5316 case 1:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005317 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VI))
5318 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005319 break;
5320 case 2:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005321 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BE))
5322 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005323 break;
5324 case 3:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005325 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BK))
5326 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005327 break;
5328 }
5329 /* Burst time is configured in units of 0.1 msec and TXOP parameter in
5330 * 32 usec, so need to convert the value here. */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005331 if (nla_put_u16(msg, NL80211_TXQ_ATTR_TXOP,
5332 (burst_time * 100 + 16) / 32) ||
5333 nla_put_u16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min) ||
5334 nla_put_u16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max) ||
5335 nla_put_u8(msg, NL80211_TXQ_ATTR_AIFS, aifs))
5336 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005337
5338 nla_nest_end(msg, params);
5339
5340 nla_nest_end(msg, txq);
5341
5342 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
5343 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005344 msg = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005345fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005346 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005347 return -1;
5348}
5349
5350
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005351static int i802_set_sta_vlan(struct i802_bss *bss, const u8 *addr,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005352 const char *ifname, int vlan_id)
5353{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005354 struct wpa_driver_nl80211_data *drv = bss->drv;
5355 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005356 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005357
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005358 wpa_printf(MSG_DEBUG, "nl80211: %s[%d]: set_sta_vlan(" MACSTR
5359 ", ifname=%s[%d], vlan_id=%d)",
5360 bss->ifname, if_nametoindex(bss->ifname),
5361 MAC2STR(addr), ifname, if_nametoindex(ifname), vlan_id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005362 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
5363 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
5364 nla_put_u32(msg, NL80211_ATTR_STA_VLAN, if_nametoindex(ifname))) {
5365 nlmsg_free(msg);
5366 return -ENOBUFS;
5367 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005368
5369 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5370 if (ret < 0) {
5371 wpa_printf(MSG_ERROR, "nl80211: NL80211_ATTR_STA_VLAN (addr="
5372 MACSTR " ifname=%s vlan_id=%d) failed: %d (%s)",
5373 MAC2STR(addr), ifname, vlan_id, ret,
5374 strerror(-ret));
5375 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005376 return ret;
5377}
5378
5379
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005380static int i802_get_inact_sec(void *priv, const u8 *addr)
5381{
5382 struct hostap_sta_driver_data data;
5383 int ret;
5384
5385 data.inactive_msec = (unsigned long) -1;
5386 ret = i802_read_sta_data(priv, &data, addr);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08005387 if (ret == -ENOENT)
5388 return -ENOENT;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005389 if (ret || data.inactive_msec == (unsigned long) -1)
5390 return -1;
5391 return data.inactive_msec / 1000;
5392}
5393
5394
5395static int i802_sta_clear_stats(void *priv, const u8 *addr)
5396{
5397#if 0
5398 /* TODO */
5399#endif
5400 return 0;
5401}
5402
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005403
5404static int i802_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
5405 int reason)
5406{
5407 struct i802_bss *bss = priv;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005408 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005409 struct ieee80211_mgmt mgmt;
5410
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005411 if (is_mesh_interface(drv->nlmode))
5412 return -1;
5413
Dmitry Shmidt04949592012-07-19 12:16:46 -07005414 if (drv->device_ap_sme)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005415 return wpa_driver_nl80211_sta_remove(bss, addr, 1, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005416
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005417 memset(&mgmt, 0, sizeof(mgmt));
5418 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
5419 WLAN_FC_STYPE_DEAUTH);
5420 memcpy(mgmt.da, addr, ETH_ALEN);
5421 memcpy(mgmt.sa, own_addr, ETH_ALEN);
5422 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
5423 mgmt.u.deauth.reason_code = host_to_le16(reason);
5424 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
5425 IEEE80211_HDRLEN +
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005426 sizeof(mgmt.u.deauth), 0, 0, 0, 0,
5427 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005428}
5429
5430
5431static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
5432 int reason)
5433{
5434 struct i802_bss *bss = priv;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005435 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005436 struct ieee80211_mgmt mgmt;
5437
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005438 if (is_mesh_interface(drv->nlmode))
5439 return -1;
5440
Dmitry Shmidt04949592012-07-19 12:16:46 -07005441 if (drv->device_ap_sme)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005442 return wpa_driver_nl80211_sta_remove(bss, addr, 0, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005443
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005444 memset(&mgmt, 0, sizeof(mgmt));
5445 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
5446 WLAN_FC_STYPE_DISASSOC);
5447 memcpy(mgmt.da, addr, ETH_ALEN);
5448 memcpy(mgmt.sa, own_addr, ETH_ALEN);
5449 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
5450 mgmt.u.disassoc.reason_code = host_to_le16(reason);
5451 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
5452 IEEE80211_HDRLEN +
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005453 sizeof(mgmt.u.disassoc), 0, 0, 0, 0,
5454 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005455}
5456
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005457
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07005458static void dump_ifidx(struct wpa_driver_nl80211_data *drv)
5459{
5460 char buf[200], *pos, *end;
5461 int i, res;
5462
5463 pos = buf;
5464 end = pos + sizeof(buf);
5465
5466 for (i = 0; i < drv->num_if_indices; i++) {
5467 if (!drv->if_indices[i])
5468 continue;
5469 res = os_snprintf(pos, end - pos, " %d", drv->if_indices[i]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005470 if (os_snprintf_error(end - pos, res))
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07005471 break;
5472 pos += res;
5473 }
5474 *pos = '\0';
5475
5476 wpa_printf(MSG_DEBUG, "nl80211: if_indices[%d]:%s",
5477 drv->num_if_indices, buf);
5478}
5479
5480
Jouni Malinen75ecf522011-06-27 15:19:46 -07005481static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
5482{
5483 int i;
5484 int *old;
5485
5486 wpa_printf(MSG_DEBUG, "nl80211: Add own interface ifindex %d",
5487 ifidx);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07005488 if (have_ifidx(drv, ifidx)) {
5489 wpa_printf(MSG_DEBUG, "nl80211: ifindex %d already in the list",
5490 ifidx);
5491 return;
5492 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07005493 for (i = 0; i < drv->num_if_indices; i++) {
5494 if (drv->if_indices[i] == 0) {
5495 drv->if_indices[i] = ifidx;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07005496 dump_ifidx(drv);
Jouni Malinen75ecf522011-06-27 15:19:46 -07005497 return;
5498 }
5499 }
5500
5501 if (drv->if_indices != drv->default_if_indices)
5502 old = drv->if_indices;
5503 else
5504 old = NULL;
5505
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005506 drv->if_indices = os_realloc_array(old, drv->num_if_indices + 1,
5507 sizeof(int));
Jouni Malinen75ecf522011-06-27 15:19:46 -07005508 if (!drv->if_indices) {
5509 if (!old)
5510 drv->if_indices = drv->default_if_indices;
5511 else
5512 drv->if_indices = old;
5513 wpa_printf(MSG_ERROR, "Failed to reallocate memory for "
5514 "interfaces");
5515 wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx);
5516 return;
5517 } else if (!old)
5518 os_memcpy(drv->if_indices, drv->default_if_indices,
5519 sizeof(drv->default_if_indices));
5520 drv->if_indices[drv->num_if_indices] = ifidx;
5521 drv->num_if_indices++;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07005522 dump_ifidx(drv);
Jouni Malinen75ecf522011-06-27 15:19:46 -07005523}
5524
5525
5526static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
5527{
5528 int i;
5529
5530 for (i = 0; i < drv->num_if_indices; i++) {
5531 if (drv->if_indices[i] == ifidx) {
5532 drv->if_indices[i] = 0;
5533 break;
5534 }
5535 }
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07005536 dump_ifidx(drv);
Jouni Malinen75ecf522011-06-27 15:19:46 -07005537}
5538
5539
5540static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
5541{
5542 int i;
5543
5544 for (i = 0; i < drv->num_if_indices; i++)
5545 if (drv->if_indices[i] == ifidx)
5546 return 1;
5547
5548 return 0;
5549}
5550
5551
5552static int i802_set_wds_sta(void *priv, const u8 *addr, int aid, int val,
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07005553 const char *bridge_ifname, char *ifname_wds)
Jouni Malinen75ecf522011-06-27 15:19:46 -07005554{
5555 struct i802_bss *bss = priv;
5556 struct wpa_driver_nl80211_data *drv = bss->drv;
5557 char name[IFNAMSIZ + 1];
5558
5559 os_snprintf(name, sizeof(name), "%s.sta%d", bss->ifname, aid);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005560 if (ifname_wds)
5561 os_strlcpy(ifname_wds, name, IFNAMSIZ + 1);
5562
Jouni Malinen75ecf522011-06-27 15:19:46 -07005563 wpa_printf(MSG_DEBUG, "nl80211: Set WDS STA addr=" MACSTR
5564 " aid=%d val=%d name=%s", MAC2STR(addr), aid, val, name);
5565 if (val) {
5566 if (!if_nametoindex(name)) {
5567 if (nl80211_create_iface(drv, name,
5568 NL80211_IFTYPE_AP_VLAN,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005569 bss->addr, 1, NULL, NULL, 0) <
5570 0)
Jouni Malinen75ecf522011-06-27 15:19:46 -07005571 return -1;
5572 if (bridge_ifname &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005573 linux_br_add_if(drv->global->ioctl_sock,
5574 bridge_ifname, name) < 0)
Jouni Malinen75ecf522011-06-27 15:19:46 -07005575 return -1;
5576 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005577 if (linux_set_iface_flags(drv->global->ioctl_sock, name, 1)) {
5578 wpa_printf(MSG_ERROR, "nl80211: Failed to set WDS STA "
5579 "interface %s up", name);
5580 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07005581 return i802_set_sta_vlan(priv, addr, name, 0);
5582 } else {
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005583 if (bridge_ifname)
5584 linux_br_del_if(drv->global->ioctl_sock, bridge_ifname,
5585 name);
5586
Jouni Malinen75ecf522011-06-27 15:19:46 -07005587 i802_set_sta_vlan(priv, addr, bss->ifname, 0);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08005588 nl80211_remove_iface(drv, if_nametoindex(name));
5589 return 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07005590 }
5591}
5592
5593
5594static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
5595{
5596 struct wpa_driver_nl80211_data *drv = eloop_ctx;
5597 struct sockaddr_ll lladdr;
5598 unsigned char buf[3000];
5599 int len;
5600 socklen_t fromlen = sizeof(lladdr);
5601
5602 len = recvfrom(sock, buf, sizeof(buf), 0,
5603 (struct sockaddr *)&lladdr, &fromlen);
5604 if (len < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005605 wpa_printf(MSG_ERROR, "nl80211: EAPOL recv failed: %s",
5606 strerror(errno));
Jouni Malinen75ecf522011-06-27 15:19:46 -07005607 return;
5608 }
5609
5610 if (have_ifidx(drv, lladdr.sll_ifindex))
5611 drv_event_eapol_rx(drv->ctx, lladdr.sll_addr, buf, len);
5612}
5613
5614
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005615static int i802_check_bridge(struct wpa_driver_nl80211_data *drv,
5616 struct i802_bss *bss,
5617 const char *brname, const char *ifname)
5618{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005619 int br_ifindex;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005620 char in_br[IFNAMSIZ];
5621
5622 os_strlcpy(bss->brname, brname, IFNAMSIZ);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005623 br_ifindex = if_nametoindex(brname);
5624 if (br_ifindex == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005625 /*
5626 * Bridge was configured, but the bridge device does
5627 * not exist. Try to add it now.
5628 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005629 if (linux_br_add(drv->global->ioctl_sock, brname) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005630 wpa_printf(MSG_ERROR, "nl80211: Failed to add the "
5631 "bridge interface %s: %s",
5632 brname, strerror(errno));
5633 return -1;
5634 }
5635 bss->added_bridge = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005636 br_ifindex = if_nametoindex(brname);
5637 add_ifidx(drv, br_ifindex);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005638 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005639 bss->br_ifindex = br_ifindex;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005640
5641 if (linux_br_get(in_br, ifname) == 0) {
5642 if (os_strcmp(in_br, brname) == 0)
5643 return 0; /* already in the bridge */
5644
5645 wpa_printf(MSG_DEBUG, "nl80211: Removing interface %s from "
5646 "bridge %s", ifname, in_br);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005647 if (linux_br_del_if(drv->global->ioctl_sock, in_br, ifname) <
5648 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005649 wpa_printf(MSG_ERROR, "nl80211: Failed to "
5650 "remove interface %s from bridge "
5651 "%s: %s",
5652 ifname, brname, strerror(errno));
5653 return -1;
5654 }
5655 }
5656
5657 wpa_printf(MSG_DEBUG, "nl80211: Adding interface %s into bridge %s",
5658 ifname, brname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005659 if (linux_br_add_if(drv->global->ioctl_sock, brname, ifname) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005660 wpa_printf(MSG_ERROR, "nl80211: Failed to add interface %s "
5661 "into bridge %s: %s",
5662 ifname, brname, strerror(errno));
5663 return -1;
5664 }
5665 bss->added_if_into_bridge = 1;
5666
5667 return 0;
5668}
5669
5670
5671static void *i802_init(struct hostapd_data *hapd,
5672 struct wpa_init_params *params)
5673{
5674 struct wpa_driver_nl80211_data *drv;
5675 struct i802_bss *bss;
5676 size_t i;
5677 char brname[IFNAMSIZ];
5678 int ifindex, br_ifindex;
5679 int br_added = 0;
5680
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005681 bss = wpa_driver_nl80211_drv_init(hapd, params->ifname,
5682 params->global_priv, 1,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005683 params->bssid, params->driver_params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005684 if (bss == NULL)
5685 return NULL;
5686
5687 drv = bss->drv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005688
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005689 if (linux_br_get(brname, params->ifname) == 0) {
5690 wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in bridge %s",
5691 params->ifname, brname);
5692 br_ifindex = if_nametoindex(brname);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005693 os_strlcpy(bss->brname, brname, IFNAMSIZ);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005694 } else {
5695 brname[0] = '\0';
5696 br_ifindex = 0;
5697 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005698 bss->br_ifindex = br_ifindex;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005699
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005700 for (i = 0; i < params->num_bridge; i++) {
5701 if (params->bridge[i]) {
5702 ifindex = if_nametoindex(params->bridge[i]);
5703 if (ifindex)
5704 add_ifidx(drv, ifindex);
5705 if (ifindex == br_ifindex)
5706 br_added = 1;
5707 }
5708 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005709
5710 /* start listening for EAPOL on the default AP interface */
5711 add_ifidx(drv, drv->ifindex);
5712
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005713 if (params->num_bridge && params->bridge[0]) {
5714 if (i802_check_bridge(drv, bss, params->bridge[0],
5715 params->ifname) < 0)
5716 goto failed;
5717 if (os_strcmp(params->bridge[0], brname) != 0)
5718 br_added = 1;
5719 }
5720
5721 if (!br_added && br_ifindex &&
5722 (params->num_bridge == 0 || !params->bridge[0]))
5723 add_ifidx(drv, br_ifindex);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005724
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005725#ifdef CONFIG_LIBNL3_ROUTE
5726 if (bss->added_if_into_bridge) {
5727 drv->rtnl_sk = nl_socket_alloc();
5728 if (drv->rtnl_sk == NULL) {
5729 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate nl_sock");
5730 goto failed;
5731 }
5732
5733 if (nl_connect(drv->rtnl_sk, NETLINK_ROUTE)) {
5734 wpa_printf(MSG_ERROR, "nl80211: Failed to connect nl_sock to NETLINK_ROUTE: %s",
5735 strerror(errno));
5736 goto failed;
5737 }
5738 }
5739#endif /* CONFIG_LIBNL3_ROUTE */
5740
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005741 drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
5742 if (drv->eapol_sock < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005743 wpa_printf(MSG_ERROR, "nl80211: socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE) failed: %s",
5744 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005745 goto failed;
5746 }
5747
5748 if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
5749 {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005750 wpa_printf(MSG_INFO, "nl80211: Could not register read socket for eapol");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005751 goto failed;
5752 }
5753
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005754 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
5755 params->own_addr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005756 goto failed;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005757 os_memcpy(drv->perm_addr, params->own_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005758
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005759 memcpy(bss->addr, params->own_addr, ETH_ALEN);
5760
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005761 return bss;
5762
5763failed:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005764 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005765 return NULL;
5766}
5767
5768
5769static void i802_deinit(void *priv)
5770{
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005771 struct i802_bss *bss = priv;
5772 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005773}
5774
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005775
5776static enum nl80211_iftype wpa_driver_nl80211_if_type(
5777 enum wpa_driver_if_type type)
5778{
5779 switch (type) {
5780 case WPA_IF_STATION:
5781 return NL80211_IFTYPE_STATION;
5782 case WPA_IF_P2P_CLIENT:
5783 case WPA_IF_P2P_GROUP:
5784 return NL80211_IFTYPE_P2P_CLIENT;
5785 case WPA_IF_AP_VLAN:
5786 return NL80211_IFTYPE_AP_VLAN;
5787 case WPA_IF_AP_BSS:
5788 return NL80211_IFTYPE_AP;
5789 case WPA_IF_P2P_GO:
5790 return NL80211_IFTYPE_P2P_GO;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005791 case WPA_IF_P2P_DEVICE:
5792 return NL80211_IFTYPE_P2P_DEVICE;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005793 case WPA_IF_MESH:
5794 return NL80211_IFTYPE_MESH_POINT;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005795 }
5796 return -1;
5797}
5798
5799
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005800static int nl80211_addr_in_use(struct nl80211_global *global, const u8 *addr)
5801{
5802 struct wpa_driver_nl80211_data *drv;
5803 dl_list_for_each(drv, &global->interfaces,
5804 struct wpa_driver_nl80211_data, list) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005805 if (os_memcmp(addr, drv->first_bss->addr, ETH_ALEN) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005806 return 1;
5807 }
5808 return 0;
5809}
5810
5811
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005812static int nl80211_vif_addr(struct wpa_driver_nl80211_data *drv, u8 *new_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005813{
5814 unsigned int idx;
5815
5816 if (!drv->global)
5817 return -1;
5818
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005819 os_memcpy(new_addr, drv->first_bss->addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005820 for (idx = 0; idx < 64; idx++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005821 new_addr[0] = drv->first_bss->addr[0] | 0x02;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005822 new_addr[0] ^= idx << 2;
5823 if (!nl80211_addr_in_use(drv->global, new_addr))
5824 break;
5825 }
5826 if (idx == 64)
5827 return -1;
5828
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005829 wpa_printf(MSG_DEBUG, "nl80211: Assigned new virtual interface address "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005830 MACSTR, MAC2STR(new_addr));
5831
5832 return 0;
5833}
5834
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005835
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005836struct wdev_info {
5837 u64 wdev_id;
5838 int wdev_id_set;
5839 u8 macaddr[ETH_ALEN];
5840};
5841
5842static int nl80211_wdev_handler(struct nl_msg *msg, void *arg)
5843{
5844 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
5845 struct nlattr *tb[NL80211_ATTR_MAX + 1];
5846 struct wdev_info *wi = arg;
5847
5848 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
5849 genlmsg_attrlen(gnlh, 0), NULL);
5850 if (tb[NL80211_ATTR_WDEV]) {
5851 wi->wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
5852 wi->wdev_id_set = 1;
5853 }
5854
5855 if (tb[NL80211_ATTR_MAC])
5856 os_memcpy(wi->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
5857 ETH_ALEN);
5858
5859 return NL_SKIP;
5860}
5861
5862
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005863static int wpa_driver_nl80211_if_add(void *priv, enum wpa_driver_if_type type,
5864 const char *ifname, const u8 *addr,
5865 void *bss_ctx, void **drv_priv,
5866 char *force_ifname, u8 *if_addr,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005867 const char *bridge, int use_existing)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005868{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005869 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005870 struct i802_bss *bss = priv;
5871 struct wpa_driver_nl80211_data *drv = bss->drv;
5872 int ifidx;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005873 int added = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005874
5875 if (addr)
5876 os_memcpy(if_addr, addr, ETH_ALEN);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005877 nlmode = wpa_driver_nl80211_if_type(type);
5878 if (nlmode == NL80211_IFTYPE_P2P_DEVICE) {
5879 struct wdev_info p2pdev_info;
5880
5881 os_memset(&p2pdev_info, 0, sizeof(p2pdev_info));
5882 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
5883 0, nl80211_wdev_handler,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005884 &p2pdev_info, use_existing);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005885 if (!p2pdev_info.wdev_id_set || ifidx != 0) {
5886 wpa_printf(MSG_ERROR, "nl80211: Failed to create a P2P Device interface %s",
5887 ifname);
5888 return -1;
5889 }
5890
5891 drv->global->if_add_wdevid = p2pdev_info.wdev_id;
5892 drv->global->if_add_wdevid_set = p2pdev_info.wdev_id_set;
5893 if (!is_zero_ether_addr(p2pdev_info.macaddr))
5894 os_memcpy(if_addr, p2pdev_info.macaddr, ETH_ALEN);
5895 wpa_printf(MSG_DEBUG, "nl80211: New P2P Device interface %s (0x%llx) created",
5896 ifname,
5897 (long long unsigned int) p2pdev_info.wdev_id);
5898 } else {
5899 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005900 0, NULL, NULL, use_existing);
5901 if (use_existing && ifidx == -ENFILE) {
5902 added = 0;
5903 ifidx = if_nametoindex(ifname);
5904 } else if (ifidx < 0) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005905 return -1;
5906 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005907 }
5908
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005909 if (!addr) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07005910 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005911 os_memcpy(if_addr, bss->addr, ETH_ALEN);
5912 else if (linux_get_ifhwaddr(drv->global->ioctl_sock,
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07005913 ifname, if_addr) < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005914 if (added)
5915 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005916 return -1;
5917 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005918 }
5919
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005920 if (!addr &&
5921 (type == WPA_IF_P2P_CLIENT || type == WPA_IF_P2P_GROUP ||
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07005922 type == WPA_IF_P2P_GO || type == WPA_IF_MESH ||
5923 type == WPA_IF_STATION)) {
5924 /* Enforce unique address */
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005925 u8 new_addr[ETH_ALEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005926
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005927 if (linux_get_ifhwaddr(drv->global->ioctl_sock, ifname,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005928 new_addr) < 0) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07005929 if (added)
5930 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005931 return -1;
5932 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005933 if (nl80211_addr_in_use(drv->global, new_addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005934 wpa_printf(MSG_DEBUG, "nl80211: Allocate new address "
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07005935 "for interface %s type %d", ifname, type);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005936 if (nl80211_vif_addr(drv, new_addr) < 0) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07005937 if (added)
5938 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005939 return -1;
5940 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005941 if (linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005942 new_addr) < 0) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07005943 if (added)
5944 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005945 return -1;
5946 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005947 }
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07005948 os_memcpy(if_addr, new_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005949 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005950
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005951 if (type == WPA_IF_AP_BSS) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005952 struct i802_bss *new_bss = os_zalloc(sizeof(*new_bss));
5953 if (new_bss == NULL) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005954 if (added)
5955 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005956 return -1;
5957 }
5958
5959 if (bridge &&
5960 i802_check_bridge(drv, new_bss, bridge, ifname) < 0) {
5961 wpa_printf(MSG_ERROR, "nl80211: Failed to add the new "
5962 "interface %s to a bridge %s",
5963 ifname, bridge);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005964 if (added)
5965 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005966 os_free(new_bss);
5967 return -1;
5968 }
5969
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005970 if (linux_set_iface_flags(drv->global->ioctl_sock, ifname, 1))
5971 {
Dmitry Shmidt71757432014-06-02 13:50:35 -07005972 if (added)
5973 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005974 os_free(new_bss);
5975 return -1;
5976 }
5977 os_strlcpy(new_bss->ifname, ifname, IFNAMSIZ);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005978 os_memcpy(new_bss->addr, if_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005979 new_bss->ifindex = ifidx;
5980 new_bss->drv = drv;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005981 new_bss->next = drv->first_bss->next;
5982 new_bss->freq = drv->first_bss->freq;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005983 new_bss->ctx = bss_ctx;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005984 new_bss->added_if = added;
5985 drv->first_bss->next = new_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005986 if (drv_priv)
5987 *drv_priv = new_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005988 nl80211_init_bss(new_bss);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005989
5990 /* Subscribe management frames for this WPA_IF_AP_BSS */
5991 if (nl80211_setup_ap(new_bss))
5992 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005993 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005994
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005995 if (drv->global)
5996 drv->global->if_add_ifindex = ifidx;
5997
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005998 /*
5999 * Some virtual interfaces need to process EAPOL packets and events on
6000 * the parent interface. This is used mainly with hostapd.
6001 */
6002 if (ifidx > 0 &&
6003 (drv->hostapd ||
6004 nlmode == NL80211_IFTYPE_AP_VLAN ||
6005 nlmode == NL80211_IFTYPE_WDS ||
6006 nlmode == NL80211_IFTYPE_MONITOR))
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006007 add_ifidx(drv, ifidx);
6008
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006009 return 0;
6010}
6011
6012
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006013static int wpa_driver_nl80211_if_remove(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006014 enum wpa_driver_if_type type,
6015 const char *ifname)
6016{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006017 struct wpa_driver_nl80211_data *drv = bss->drv;
6018 int ifindex = if_nametoindex(ifname);
6019
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006020 wpa_printf(MSG_DEBUG, "nl80211: %s(type=%d ifname=%s) ifindex=%d added_if=%d",
6021 __func__, type, ifname, ifindex, bss->added_if);
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08006022 if (ifindex > 0 && (bss->added_if || bss->ifindex != ifindex))
Dmitry Shmidt051af732013-10-22 13:52:46 -07006023 nl80211_remove_iface(drv, ifindex);
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07006024 else if (ifindex > 0 && !bss->added_if) {
6025 struct wpa_driver_nl80211_data *drv2;
6026 dl_list_for_each(drv2, &drv->global->interfaces,
6027 struct wpa_driver_nl80211_data, list)
6028 del_ifidx(drv2, ifindex);
6029 }
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006030
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006031 if (type != WPA_IF_AP_BSS)
6032 return 0;
6033
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006034 if (bss->added_if_into_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006035 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
6036 bss->ifname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006037 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
6038 "interface %s from bridge %s: %s",
6039 bss->ifname, bss->brname, strerror(errno));
6040 }
6041 if (bss->added_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006042 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006043 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
6044 "bridge %s: %s",
6045 bss->brname, strerror(errno));
6046 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006047
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006048 if (bss != drv->first_bss) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006049 struct i802_bss *tbss;
6050
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006051 wpa_printf(MSG_DEBUG, "nl80211: Not the first BSS - remove it");
6052 for (tbss = drv->first_bss; tbss; tbss = tbss->next) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006053 if (tbss->next == bss) {
6054 tbss->next = bss->next;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006055 /* Unsubscribe management frames */
6056 nl80211_teardown_ap(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006057 nl80211_destroy_bss(bss);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006058 if (!bss->added_if)
6059 i802_set_iface_flags(bss, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006060 os_free(bss);
6061 bss = NULL;
6062 break;
6063 }
6064 }
6065 if (bss)
6066 wpa_printf(MSG_INFO, "nl80211: %s - could not find "
6067 "BSS %p in the list", __func__, bss);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006068 } else {
6069 wpa_printf(MSG_DEBUG, "nl80211: First BSS - reassign context");
6070 nl80211_teardown_ap(bss);
6071 if (!bss->added_if && !drv->first_bss->next)
6072 wpa_driver_nl80211_del_beacon(drv);
6073 nl80211_destroy_bss(bss);
6074 if (!bss->added_if)
6075 i802_set_iface_flags(bss, 0);
6076 if (drv->first_bss->next) {
6077 drv->first_bss = drv->first_bss->next;
6078 drv->ctx = drv->first_bss->ctx;
6079 os_free(bss);
6080 } else {
6081 wpa_printf(MSG_DEBUG, "nl80211: No second BSS to reassign context to");
6082 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006083 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006084
6085 return 0;
6086}
6087
6088
6089static int cookie_handler(struct nl_msg *msg, void *arg)
6090{
6091 struct nlattr *tb[NL80211_ATTR_MAX + 1];
6092 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6093 u64 *cookie = arg;
6094 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6095 genlmsg_attrlen(gnlh, 0), NULL);
6096 if (tb[NL80211_ATTR_COOKIE])
6097 *cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
6098 return NL_SKIP;
6099}
6100
6101
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006102static int nl80211_send_frame_cmd(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006103 unsigned int freq, unsigned int wait,
6104 const u8 *buf, size_t buf_len,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006105 u64 *cookie_out, int no_cck, int no_ack,
6106 int offchanok)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006107{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006108 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006109 struct nl_msg *msg;
6110 u64 cookie;
6111 int ret = -1;
6112
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006113 wpa_printf(MSG_MSGDUMP, "nl80211: CMD_FRAME freq=%u wait=%u no_cck=%d "
Dmitry Shmidt04949592012-07-19 12:16:46 -07006114 "no_ack=%d offchanok=%d",
6115 freq, wait, no_cck, no_ack, offchanok);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006116 wpa_hexdump(MSG_MSGDUMP, "CMD_FRAME", buf, buf_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006117
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006118 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_FRAME)) ||
6119 (freq && nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
6120 (wait && nla_put_u32(msg, NL80211_ATTR_DURATION, wait)) ||
6121 (offchanok && ((drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
6122 drv->test_use_roc_tx) &&
6123 nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK)) ||
6124 (no_cck && nla_put_flag(msg, NL80211_ATTR_TX_NO_CCK_RATE)) ||
6125 (no_ack && nla_put_flag(msg, NL80211_ATTR_DONT_WAIT_FOR_ACK)) ||
6126 nla_put(msg, NL80211_ATTR_FRAME, buf_len, buf))
6127 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006128
6129 cookie = 0;
6130 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
6131 msg = NULL;
6132 if (ret) {
6133 wpa_printf(MSG_DEBUG, "nl80211: Frame command failed: ret=%d "
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07006134 "(%s) (freq=%u wait=%u)", ret, strerror(-ret),
6135 freq, wait);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006136 } else {
6137 wpa_printf(MSG_MSGDUMP, "nl80211: Frame TX command accepted%s; "
6138 "cookie 0x%llx", no_ack ? " (no ACK)" : "",
6139 (long long unsigned int) cookie);
6140
6141 if (cookie_out)
6142 *cookie_out = no_ack ? (u64) -1 : cookie;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006143 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006144
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006145fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006146 nlmsg_free(msg);
6147 return ret;
6148}
6149
6150
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006151static int wpa_driver_nl80211_send_action(struct i802_bss *bss,
6152 unsigned int freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006153 unsigned int wait_time,
6154 const u8 *dst, const u8 *src,
6155 const u8 *bssid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006156 const u8 *data, size_t data_len,
6157 int no_cck)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006158{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006159 struct wpa_driver_nl80211_data *drv = bss->drv;
6160 int ret = -1;
6161 u8 *buf;
6162 struct ieee80211_hdr *hdr;
6163
6164 wpa_printf(MSG_DEBUG, "nl80211: Send Action frame (ifindex=%d, "
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006165 "freq=%u MHz wait=%d ms no_cck=%d)",
6166 drv->ifindex, freq, wait_time, no_cck);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006167
6168 buf = os_zalloc(24 + data_len);
6169 if (buf == NULL)
6170 return ret;
6171 os_memcpy(buf + 24, data, data_len);
6172 hdr = (struct ieee80211_hdr *) buf;
6173 hdr->frame_control =
6174 IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_ACTION);
6175 os_memcpy(hdr->addr1, dst, ETH_ALEN);
6176 os_memcpy(hdr->addr2, src, ETH_ALEN);
6177 os_memcpy(hdr->addr3, bssid, ETH_ALEN);
6178
Dmitry Shmidt56052862013-10-04 10:23:25 -07006179 if (is_ap_interface(drv->nlmode) &&
6180 (!(drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
6181 (int) freq == bss->freq || drv->device_ap_sme ||
6182 !drv->use_monitor))
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006183 ret = wpa_driver_nl80211_send_mlme(bss, buf, 24 + data_len,
6184 0, freq, no_cck, 1,
6185 wait_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006186 else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006187 ret = nl80211_send_frame_cmd(bss, freq, wait_time, buf,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006188 24 + data_len,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006189 &drv->send_action_cookie,
6190 no_cck, 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006191
6192 os_free(buf);
6193 return ret;
6194}
6195
6196
6197static void wpa_driver_nl80211_send_action_cancel_wait(void *priv)
6198{
6199 struct i802_bss *bss = priv;
6200 struct wpa_driver_nl80211_data *drv = bss->drv;
6201 struct nl_msg *msg;
6202 int ret;
6203
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08006204 wpa_printf(MSG_DEBUG, "nl80211: Cancel TX frame wait: cookie=0x%llx",
6205 (long long unsigned int) drv->send_action_cookie);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006206 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_FRAME_WAIT_CANCEL)) ||
6207 nla_put_u64(msg, NL80211_ATTR_COOKIE, drv->send_action_cookie)) {
6208 nlmsg_free(msg);
6209 return;
6210 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006211
6212 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006213 if (ret)
6214 wpa_printf(MSG_DEBUG, "nl80211: wait cancel failed: ret=%d "
6215 "(%s)", ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006216}
6217
6218
6219static int wpa_driver_nl80211_remain_on_channel(void *priv, unsigned int freq,
6220 unsigned int duration)
6221{
6222 struct i802_bss *bss = priv;
6223 struct wpa_driver_nl80211_data *drv = bss->drv;
6224 struct nl_msg *msg;
6225 int ret;
6226 u64 cookie;
6227
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006228 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_REMAIN_ON_CHANNEL)) ||
6229 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
6230 nla_put_u32(msg, NL80211_ATTR_DURATION, duration)) {
6231 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006232 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006233 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006234
6235 cookie = 0;
6236 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
6237 if (ret == 0) {
6238 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel cookie "
6239 "0x%llx for freq=%u MHz duration=%u",
6240 (long long unsigned int) cookie, freq, duration);
6241 drv->remain_on_chan_cookie = cookie;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006242 drv->pending_remain_on_chan = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006243 return 0;
6244 }
6245 wpa_printf(MSG_DEBUG, "nl80211: Failed to request remain-on-channel "
6246 "(freq=%d duration=%u): %d (%s)",
6247 freq, duration, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006248 return -1;
6249}
6250
6251
6252static int wpa_driver_nl80211_cancel_remain_on_channel(void *priv)
6253{
6254 struct i802_bss *bss = priv;
6255 struct wpa_driver_nl80211_data *drv = bss->drv;
6256 struct nl_msg *msg;
6257 int ret;
6258
6259 if (!drv->pending_remain_on_chan) {
6260 wpa_printf(MSG_DEBUG, "nl80211: No pending remain-on-channel "
6261 "to cancel");
6262 return -1;
6263 }
6264
6265 wpa_printf(MSG_DEBUG, "nl80211: Cancel remain-on-channel with cookie "
6266 "0x%llx",
6267 (long long unsigned int) drv->remain_on_chan_cookie);
6268
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006269 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL);
6270 if (!msg ||
6271 nla_put_u64(msg, NL80211_ATTR_COOKIE, drv->remain_on_chan_cookie)) {
6272 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006273 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006274 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006275
6276 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6277 if (ret == 0)
6278 return 0;
6279 wpa_printf(MSG_DEBUG, "nl80211: Failed to cancel remain-on-channel: "
6280 "%d (%s)", ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006281 return -1;
6282}
6283
6284
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006285static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss, int report)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006286{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006287 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07006288
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006289 if (!report) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006290 if (bss->nl_preq && drv->device_ap_sme &&
Dmitry Shmidt03658832014-08-13 11:03:49 -07006291 is_ap_interface(drv->nlmode) && !bss->in_deinit &&
6292 !bss->static_ap) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006293 /*
6294 * Do not disable Probe Request reporting that was
6295 * enabled in nl80211_setup_ap().
6296 */
6297 wpa_printf(MSG_DEBUG, "nl80211: Skip disabling of "
6298 "Probe Request reporting nl_preq=%p while "
6299 "in AP mode", bss->nl_preq);
6300 } else if (bss->nl_preq) {
6301 wpa_printf(MSG_DEBUG, "nl80211: Disable Probe Request "
6302 "reporting nl_preq=%p", bss->nl_preq);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006303 nl80211_destroy_eloop_handle(&bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006304 }
6305 return 0;
6306 }
6307
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006308 if (bss->nl_preq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006309 wpa_printf(MSG_DEBUG, "nl80211: Probe Request reporting "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006310 "already on! nl_preq=%p", bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006311 return 0;
6312 }
6313
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006314 bss->nl_preq = nl_create_handle(drv->global->nl_cb, "preq");
6315 if (bss->nl_preq == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006316 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006317 wpa_printf(MSG_DEBUG, "nl80211: Enable Probe Request "
6318 "reporting nl_preq=%p", bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006319
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006320 if (nl80211_register_frame(bss, bss->nl_preq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006321 (WLAN_FC_TYPE_MGMT << 2) |
6322 (WLAN_FC_STYPE_PROBE_REQ << 4),
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006323 NULL, 0) < 0)
6324 goto out_err;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07006325
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006326 nl80211_register_eloop_read(&bss->nl_preq,
6327 wpa_driver_nl80211_event_receive,
6328 bss->nl_cb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006329
6330 return 0;
6331
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006332 out_err:
6333 nl_destroy_handles(&bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006334 return -1;
6335}
6336
6337
6338static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
6339 int ifindex, int disabled)
6340{
6341 struct nl_msg *msg;
6342 struct nlattr *bands, *band;
6343 int ret;
6344
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006345 wpa_printf(MSG_DEBUG,
6346 "nl80211: NL80211_CMD_SET_TX_BITRATE_MASK (ifindex=%d %s)",
6347 ifindex, disabled ? "NL80211_TXRATE_LEGACY=OFDM-only" :
6348 "no NL80211_TXRATE_LEGACY constraint");
6349
6350 msg = nl80211_ifindex_msg(drv, ifindex, 0,
6351 NL80211_CMD_SET_TX_BITRATE_MASK);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006352 if (!msg)
6353 return -1;
6354
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006355 bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES);
6356 if (!bands)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006357 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006358
6359 /*
6360 * Disable 2 GHz rates 1, 2, 5.5, 11 Mbps by masking out everything
6361 * else apart from 6, 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS
6362 * rates. All 5 GHz rates are left enabled.
6363 */
6364 band = nla_nest_start(msg, NL80211_BAND_2GHZ);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006365 if (!band ||
6366 (disabled && nla_put(msg, NL80211_TXRATE_LEGACY, 8,
6367 "\x0c\x12\x18\x24\x30\x48\x60\x6c")))
6368 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006369 nla_nest_end(msg, band);
6370
6371 nla_nest_end(msg, bands);
6372
6373 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006374 if (ret) {
6375 wpa_printf(MSG_DEBUG, "nl80211: Set TX rates failed: ret=%d "
6376 "(%s)", ret, strerror(-ret));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006377 } else
6378 drv->disabled_11b_rates = disabled;
6379
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006380 return ret;
6381
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006382fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006383 nlmsg_free(msg);
6384 return -1;
6385}
6386
6387
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006388static int wpa_driver_nl80211_deinit_ap(void *priv)
6389{
6390 struct i802_bss *bss = priv;
6391 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006392 if (!is_ap_interface(drv->nlmode))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006393 return -1;
6394 wpa_driver_nl80211_del_beacon(drv);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006395 bss->beacon_set = 0;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006396
6397 /*
6398 * If the P2P GO interface was dynamically added, then it is
6399 * possible that the interface change to station is not possible.
6400 */
6401 if (drv->nlmode == NL80211_IFTYPE_P2P_GO && bss->if_dynamic)
6402 return 0;
6403
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006404 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006405}
6406
6407
Dmitry Shmidtea69e842013-05-13 14:52:28 -07006408static int wpa_driver_nl80211_stop_ap(void *priv)
6409{
6410 struct i802_bss *bss = priv;
6411 struct wpa_driver_nl80211_data *drv = bss->drv;
6412 if (!is_ap_interface(drv->nlmode))
6413 return -1;
6414 wpa_driver_nl80211_del_beacon(drv);
6415 bss->beacon_set = 0;
6416 return 0;
6417}
6418
6419
Dmitry Shmidt04949592012-07-19 12:16:46 -07006420static int wpa_driver_nl80211_deinit_p2p_cli(void *priv)
6421{
6422 struct i802_bss *bss = priv;
6423 struct wpa_driver_nl80211_data *drv = bss->drv;
6424 if (drv->nlmode != NL80211_IFTYPE_P2P_CLIENT)
6425 return -1;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006426
6427 /*
6428 * If the P2P Client interface was dynamically added, then it is
6429 * possible that the interface change to station is not possible.
6430 */
6431 if (bss->if_dynamic)
6432 return 0;
6433
Dmitry Shmidt04949592012-07-19 12:16:46 -07006434 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
6435}
6436
6437
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006438static void wpa_driver_nl80211_resume(void *priv)
6439{
6440 struct i802_bss *bss = priv;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006441
6442 if (i802_set_iface_flags(bss, 1))
6443 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface up on resume event");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006444}
6445
6446
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006447static int nl80211_signal_monitor(void *priv, int threshold, int hysteresis)
6448{
6449 struct i802_bss *bss = priv;
6450 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006451 struct nl_msg *msg;
6452 struct nlattr *cqm;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006453
6454 wpa_printf(MSG_DEBUG, "nl80211: Signal monitor threshold=%d "
6455 "hysteresis=%d", threshold, hysteresis);
6456
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006457 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_CQM)) ||
6458 !(cqm = nla_nest_start(msg, NL80211_ATTR_CQM)) ||
6459 nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THOLD, threshold) ||
6460 nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_HYST, hysteresis)) {
6461 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006462 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006463 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006464 nla_nest_end(msg, cqm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006465
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006466 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006467}
6468
6469
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006470static int get_channel_width(struct nl_msg *msg, void *arg)
6471{
6472 struct nlattr *tb[NL80211_ATTR_MAX + 1];
6473 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6474 struct wpa_signal_info *sig_change = arg;
6475
6476 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6477 genlmsg_attrlen(gnlh, 0), NULL);
6478
6479 sig_change->center_frq1 = -1;
6480 sig_change->center_frq2 = -1;
6481 sig_change->chanwidth = CHAN_WIDTH_UNKNOWN;
6482
6483 if (tb[NL80211_ATTR_CHANNEL_WIDTH]) {
6484 sig_change->chanwidth = convert2width(
6485 nla_get_u32(tb[NL80211_ATTR_CHANNEL_WIDTH]));
6486 if (tb[NL80211_ATTR_CENTER_FREQ1])
6487 sig_change->center_frq1 =
6488 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]);
6489 if (tb[NL80211_ATTR_CENTER_FREQ2])
6490 sig_change->center_frq2 =
6491 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]);
6492 }
6493
6494 return NL_SKIP;
6495}
6496
6497
6498static int nl80211_get_channel_width(struct wpa_driver_nl80211_data *drv,
6499 struct wpa_signal_info *sig)
6500{
6501 struct nl_msg *msg;
6502
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006503 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_GET_INTERFACE);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006504 return send_and_recv_msgs(drv, msg, get_channel_width, sig);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006505}
6506
6507
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006508static int nl80211_signal_poll(void *priv, struct wpa_signal_info *si)
6509{
6510 struct i802_bss *bss = priv;
6511 struct wpa_driver_nl80211_data *drv = bss->drv;
6512 int res;
6513
6514 os_memset(si, 0, sizeof(*si));
6515 res = nl80211_get_link_signal(drv, si);
6516 if (res != 0)
6517 return res;
6518
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006519 res = nl80211_get_channel_width(drv, si);
6520 if (res != 0)
6521 return res;
6522
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006523 return nl80211_get_link_noise(drv, si);
6524}
6525
6526
6527static int nl80211_send_frame(void *priv, const u8 *data, size_t data_len,
6528 int encrypt)
6529{
6530 struct i802_bss *bss = priv;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006531 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt, 0,
6532 0, 0, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006533}
6534
6535
6536static int nl80211_set_param(void *priv, const char *param)
6537{
6538 wpa_printf(MSG_DEBUG, "nl80211: driver param='%s'", param);
6539 if (param == NULL)
6540 return 0;
6541
6542#ifdef CONFIG_P2P
6543 if (os_strstr(param, "use_p2p_group_interface=1")) {
6544 struct i802_bss *bss = priv;
6545 struct wpa_driver_nl80211_data *drv = bss->drv;
6546
6547 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
6548 "interface");
6549 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
6550 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
6551 }
6552#endif /* CONFIG_P2P */
6553
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006554 if (os_strstr(param, "use_monitor=1")) {
6555 struct i802_bss *bss = priv;
6556 struct wpa_driver_nl80211_data *drv = bss->drv;
6557 drv->use_monitor = 1;
6558 }
6559
6560 if (os_strstr(param, "force_connect_cmd=1")) {
6561 struct i802_bss *bss = priv;
6562 struct wpa_driver_nl80211_data *drv = bss->drv;
6563 drv->capa.flags &= ~WPA_DRIVER_FLAGS_SME;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006564 drv->force_connect_cmd = 1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006565 }
6566
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08006567 if (os_strstr(param, "no_offchannel_tx=1")) {
6568 struct i802_bss *bss = priv;
6569 struct wpa_driver_nl80211_data *drv = bss->drv;
6570 drv->capa.flags &= ~WPA_DRIVER_FLAGS_OFFCHANNEL_TX;
6571 drv->test_use_roc_tx = 1;
6572 }
6573
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006574 return 0;
6575}
6576
6577
6578static void * nl80211_global_init(void)
6579{
6580 struct nl80211_global *global;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006581 struct netlink_config *cfg;
6582
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006583 global = os_zalloc(sizeof(*global));
6584 if (global == NULL)
6585 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006586 global->ioctl_sock = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006587 dl_list_init(&global->interfaces);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006588 global->if_add_ifindex = -1;
6589
6590 cfg = os_zalloc(sizeof(*cfg));
6591 if (cfg == NULL)
6592 goto err;
6593
6594 cfg->ctx = global;
6595 cfg->newlink_cb = wpa_driver_nl80211_event_rtm_newlink;
6596 cfg->dellink_cb = wpa_driver_nl80211_event_rtm_dellink;
6597 global->netlink = netlink_init(cfg);
6598 if (global->netlink == NULL) {
6599 os_free(cfg);
6600 goto err;
6601 }
6602
6603 if (wpa_driver_nl80211_init_nl_global(global) < 0)
6604 goto err;
6605
6606 global->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
6607 if (global->ioctl_sock < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006608 wpa_printf(MSG_ERROR, "nl80211: socket(PF_INET,SOCK_DGRAM) failed: %s",
6609 strerror(errno));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006610 goto err;
6611 }
6612
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006613 return global;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006614
6615err:
6616 nl80211_global_deinit(global);
6617 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006618}
6619
6620
6621static void nl80211_global_deinit(void *priv)
6622{
6623 struct nl80211_global *global = priv;
6624 if (global == NULL)
6625 return;
6626 if (!dl_list_empty(&global->interfaces)) {
6627 wpa_printf(MSG_ERROR, "nl80211: %u interface(s) remain at "
6628 "nl80211_global_deinit",
6629 dl_list_len(&global->interfaces));
6630 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006631
6632 if (global->netlink)
6633 netlink_deinit(global->netlink);
6634
6635 nl_destroy_handles(&global->nl);
6636
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006637 if (global->nl_event)
6638 nl80211_destroy_eloop_handle(&global->nl_event);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006639
6640 nl_cb_put(global->nl_cb);
6641
6642 if (global->ioctl_sock >= 0)
6643 close(global->ioctl_sock);
6644
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006645 os_free(global);
6646}
6647
6648
6649static const char * nl80211_get_radio_name(void *priv)
6650{
6651 struct i802_bss *bss = priv;
6652 struct wpa_driver_nl80211_data *drv = bss->drv;
6653 return drv->phyname;
6654}
6655
6656
Jouni Malinen75ecf522011-06-27 15:19:46 -07006657static int nl80211_pmkid(struct i802_bss *bss, int cmd, const u8 *bssid,
6658 const u8 *pmkid)
6659{
6660 struct nl_msg *msg;
6661
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006662 if (!(msg = nl80211_bss_msg(bss, 0, cmd)) ||
6663 (pmkid && nla_put(msg, NL80211_ATTR_PMKID, 16, pmkid)) ||
6664 (bssid && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))) {
6665 nlmsg_free(msg);
6666 return -ENOBUFS;
6667 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07006668
6669 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
Jouni Malinen75ecf522011-06-27 15:19:46 -07006670}
6671
6672
6673static int nl80211_add_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
6674{
6675 struct i802_bss *bss = priv;
6676 wpa_printf(MSG_DEBUG, "nl80211: Add PMKID for " MACSTR, MAC2STR(bssid));
6677 return nl80211_pmkid(bss, NL80211_CMD_SET_PMKSA, bssid, pmkid);
6678}
6679
6680
6681static int nl80211_remove_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
6682{
6683 struct i802_bss *bss = priv;
6684 wpa_printf(MSG_DEBUG, "nl80211: Delete PMKID for " MACSTR,
6685 MAC2STR(bssid));
6686 return nl80211_pmkid(bss, NL80211_CMD_DEL_PMKSA, bssid, pmkid);
6687}
6688
6689
6690static int nl80211_flush_pmkid(void *priv)
6691{
6692 struct i802_bss *bss = priv;
6693 wpa_printf(MSG_DEBUG, "nl80211: Flush PMKIDs");
6694 return nl80211_pmkid(bss, NL80211_CMD_FLUSH_PMKSA, NULL, NULL);
6695}
6696
6697
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006698static void clean_survey_results(struct survey_results *survey_results)
6699{
6700 struct freq_survey *survey, *tmp;
6701
6702 if (dl_list_empty(&survey_results->survey_list))
6703 return;
6704
6705 dl_list_for_each_safe(survey, tmp, &survey_results->survey_list,
6706 struct freq_survey, list) {
6707 dl_list_del(&survey->list);
6708 os_free(survey);
6709 }
6710}
6711
6712
6713static void add_survey(struct nlattr **sinfo, u32 ifidx,
6714 struct dl_list *survey_list)
6715{
6716 struct freq_survey *survey;
6717
6718 survey = os_zalloc(sizeof(struct freq_survey));
6719 if (!survey)
6720 return;
6721
6722 survey->ifidx = ifidx;
6723 survey->freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
6724 survey->filled = 0;
6725
6726 if (sinfo[NL80211_SURVEY_INFO_NOISE]) {
6727 survey->nf = (int8_t)
6728 nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
6729 survey->filled |= SURVEY_HAS_NF;
6730 }
6731
6732 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]) {
6733 survey->channel_time =
6734 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]);
6735 survey->filled |= SURVEY_HAS_CHAN_TIME;
6736 }
6737
6738 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]) {
6739 survey->channel_time_busy =
6740 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]);
6741 survey->filled |= SURVEY_HAS_CHAN_TIME_BUSY;
6742 }
6743
6744 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]) {
6745 survey->channel_time_rx =
6746 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]);
6747 survey->filled |= SURVEY_HAS_CHAN_TIME_RX;
6748 }
6749
6750 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]) {
6751 survey->channel_time_tx =
6752 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]);
6753 survey->filled |= SURVEY_HAS_CHAN_TIME_TX;
6754 }
6755
6756 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)",
6757 survey->freq,
6758 survey->nf,
6759 (unsigned long int) survey->channel_time,
6760 (unsigned long int) survey->channel_time_busy,
6761 (unsigned long int) survey->channel_time_tx,
6762 (unsigned long int) survey->channel_time_rx,
6763 survey->filled);
6764
6765 dl_list_add_tail(survey_list, &survey->list);
6766}
6767
6768
6769static int check_survey_ok(struct nlattr **sinfo, u32 surveyed_freq,
6770 unsigned int freq_filter)
6771{
6772 if (!freq_filter)
6773 return 1;
6774
6775 return freq_filter == surveyed_freq;
6776}
6777
6778
6779static int survey_handler(struct nl_msg *msg, void *arg)
6780{
6781 struct nlattr *tb[NL80211_ATTR_MAX + 1];
6782 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6783 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
6784 struct survey_results *survey_results;
6785 u32 surveyed_freq = 0;
6786 u32 ifidx;
6787
6788 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
6789 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
6790 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
6791 };
6792
6793 survey_results = (struct survey_results *) arg;
6794
6795 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6796 genlmsg_attrlen(gnlh, 0), NULL);
6797
Dmitry Shmidt97672262014-02-03 13:02:54 -08006798 if (!tb[NL80211_ATTR_IFINDEX])
6799 return NL_SKIP;
6800
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006801 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
6802
6803 if (!tb[NL80211_ATTR_SURVEY_INFO])
6804 return NL_SKIP;
6805
6806 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
6807 tb[NL80211_ATTR_SURVEY_INFO],
6808 survey_policy))
6809 return NL_SKIP;
6810
6811 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY]) {
6812 wpa_printf(MSG_ERROR, "nl80211: Invalid survey data");
6813 return NL_SKIP;
6814 }
6815
6816 surveyed_freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
6817
6818 if (!check_survey_ok(sinfo, surveyed_freq,
6819 survey_results->freq_filter))
6820 return NL_SKIP;
6821
6822 if (survey_results->freq_filter &&
6823 survey_results->freq_filter != surveyed_freq) {
6824 wpa_printf(MSG_EXCESSIVE, "nl80211: Ignoring survey data for freq %d MHz",
6825 surveyed_freq);
6826 return NL_SKIP;
6827 }
6828
6829 add_survey(sinfo, ifidx, &survey_results->survey_list);
6830
6831 return NL_SKIP;
6832}
6833
6834
6835static int wpa_driver_nl80211_get_survey(void *priv, unsigned int freq)
6836{
6837 struct i802_bss *bss = priv;
6838 struct wpa_driver_nl80211_data *drv = bss->drv;
6839 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006840 int err;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006841 union wpa_event_data data;
6842 struct survey_results *survey_results;
6843
6844 os_memset(&data, 0, sizeof(data));
6845 survey_results = &data.survey_results;
6846
6847 dl_list_init(&survey_results->survey_list);
6848
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006849 msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006850 if (!msg)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006851 return -ENOBUFS;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006852
6853 if (freq)
6854 data.survey_results.freq_filter = freq;
6855
6856 do {
6857 wpa_printf(MSG_DEBUG, "nl80211: Fetch survey data");
6858 err = send_and_recv_msgs(drv, msg, survey_handler,
6859 survey_results);
6860 } while (err > 0);
6861
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006862 if (err)
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006863 wpa_printf(MSG_ERROR, "nl80211: Failed to process survey data");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006864 else
6865 wpa_supplicant_event(drv->ctx, EVENT_SURVEY, &data);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006866
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006867 clean_survey_results(survey_results);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006868 return err;
6869}
6870
6871
Dmitry Shmidt807291d2015-01-27 13:40:23 -08006872static void nl80211_set_rekey_info(void *priv, const u8 *kek, size_t kek_len,
6873 const u8 *kck, size_t kck_len,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006874 const u8 *replay_ctr)
6875{
6876 struct i802_bss *bss = priv;
6877 struct wpa_driver_nl80211_data *drv = bss->drv;
6878 struct nlattr *replay_nested;
6879 struct nl_msg *msg;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08006880 int ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006881
Dmitry Shmidtff787d52015-01-12 13:01:47 -08006882 if (!drv->set_rekey_offload)
6883 return;
6884
6885 wpa_printf(MSG_DEBUG, "nl80211: Set rekey offload");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006886 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_REKEY_OFFLOAD)) ||
6887 !(replay_nested = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA)) ||
Dmitry Shmidt807291d2015-01-27 13:40:23 -08006888 nla_put(msg, NL80211_REKEY_DATA_KEK, kek_len, kek) ||
6889 nla_put(msg, NL80211_REKEY_DATA_KCK, kck_len, kck) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006890 nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR, NL80211_REPLAY_CTR_LEN,
6891 replay_ctr)) {
6892 nl80211_nlmsg_clear(msg);
6893 nlmsg_free(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006894 return;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006895 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006896
6897 nla_nest_end(msg, replay_nested);
6898
Dmitry Shmidtff787d52015-01-12 13:01:47 -08006899 ret = send_and_recv_msgs(drv, msg, NULL, (void *) -1);
6900 if (ret == -EOPNOTSUPP) {
6901 wpa_printf(MSG_DEBUG,
6902 "nl80211: Driver does not support rekey offload");
6903 drv->set_rekey_offload = 0;
6904 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006905}
6906
6907
6908static void nl80211_send_null_frame(struct i802_bss *bss, const u8 *own_addr,
6909 const u8 *addr, int qos)
6910{
6911 /* send data frame to poll STA and check whether
6912 * this frame is ACKed */
6913 struct {
6914 struct ieee80211_hdr hdr;
6915 u16 qos_ctl;
6916 } STRUCT_PACKED nulldata;
6917 size_t size;
6918
6919 /* Send data frame to poll STA and check whether this frame is ACKed */
6920
6921 os_memset(&nulldata, 0, sizeof(nulldata));
6922
6923 if (qos) {
6924 nulldata.hdr.frame_control =
6925 IEEE80211_FC(WLAN_FC_TYPE_DATA,
6926 WLAN_FC_STYPE_QOS_NULL);
6927 size = sizeof(nulldata);
6928 } else {
6929 nulldata.hdr.frame_control =
6930 IEEE80211_FC(WLAN_FC_TYPE_DATA,
6931 WLAN_FC_STYPE_NULLFUNC);
6932 size = sizeof(struct ieee80211_hdr);
6933 }
6934
6935 nulldata.hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS);
6936 os_memcpy(nulldata.hdr.IEEE80211_DA_FROMDS, addr, ETH_ALEN);
6937 os_memcpy(nulldata.hdr.IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
6938 os_memcpy(nulldata.hdr.IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
6939
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006940 if (wpa_driver_nl80211_send_mlme(bss, (u8 *) &nulldata, size, 0, 0, 0,
6941 0, 0) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006942 wpa_printf(MSG_DEBUG, "nl80211_send_null_frame: Failed to "
6943 "send poll frame");
6944}
6945
6946static void nl80211_poll_client(void *priv, const u8 *own_addr, const u8 *addr,
6947 int qos)
6948{
6949 struct i802_bss *bss = priv;
6950 struct wpa_driver_nl80211_data *drv = bss->drv;
6951 struct nl_msg *msg;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08006952 int ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006953
6954 if (!drv->poll_command_supported) {
6955 nl80211_send_null_frame(bss, own_addr, addr, qos);
6956 return;
6957 }
6958
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006959 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_PROBE_CLIENT)) ||
6960 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
6961 nlmsg_free(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006962 return;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006963 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006964
Dmitry Shmidt7f656022015-02-25 14:36:37 -08006965 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6966 if (ret < 0) {
6967 wpa_printf(MSG_DEBUG, "nl80211: Client probe request for "
6968 MACSTR " failed: ret=%d (%s)",
6969 MAC2STR(addr), ret, strerror(-ret));
6970 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006971}
6972
6973
6974static int nl80211_set_power_save(struct i802_bss *bss, int enabled)
6975{
6976 struct nl_msg *msg;
6977
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006978 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_POWER_SAVE)) ||
6979 nla_put_u32(msg, NL80211_ATTR_PS_STATE,
6980 enabled ? NL80211_PS_ENABLED : NL80211_PS_DISABLED)) {
6981 nlmsg_free(msg);
6982 return -ENOBUFS;
6983 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006984 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006985}
6986
6987
6988static int nl80211_set_p2p_powersave(void *priv, int legacy_ps, int opp_ps,
6989 int ctwindow)
6990{
6991 struct i802_bss *bss = priv;
6992
6993 wpa_printf(MSG_DEBUG, "nl80211: set_p2p_powersave (legacy_ps=%d "
6994 "opp_ps=%d ctwindow=%d)", legacy_ps, opp_ps, ctwindow);
6995
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08006996 if (opp_ps != -1 || ctwindow != -1) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006997#ifdef ANDROID_P2P
6998 wpa_driver_set_p2p_ps(priv, legacy_ps, opp_ps, ctwindow);
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08006999#else /* ANDROID_P2P */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007000 return -1; /* Not yet supported */
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08007001#endif /* ANDROID_P2P */
7002 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007003
7004 if (legacy_ps == -1)
7005 return 0;
7006 if (legacy_ps != 0 && legacy_ps != 1)
7007 return -1; /* Not yet supported */
7008
7009 return nl80211_set_power_save(bss, legacy_ps);
7010}
7011
7012
Dmitry Shmidt051af732013-10-22 13:52:46 -07007013static int nl80211_start_radar_detection(void *priv,
7014 struct hostapd_freq_params *freq)
Dmitry Shmidtea69e842013-05-13 14:52:28 -07007015{
7016 struct i802_bss *bss = priv;
7017 struct wpa_driver_nl80211_data *drv = bss->drv;
7018 struct nl_msg *msg;
7019 int ret;
7020
Dmitry Shmidt051af732013-10-22 13:52:46 -07007021 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)",
7022 freq->freq, freq->ht_enabled, freq->vht_enabled,
7023 freq->bandwidth, freq->center_freq1, freq->center_freq2);
7024
Dmitry Shmidtea69e842013-05-13 14:52:28 -07007025 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_RADAR)) {
7026 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support radar "
7027 "detection");
7028 return -1;
7029 }
7030
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007031 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_RADAR_DETECT)) ||
7032 nl80211_put_freq_params(msg, freq) < 0) {
7033 nlmsg_free(msg);
Dmitry Shmidtea69e842013-05-13 14:52:28 -07007034 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007035 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -07007036
7037 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7038 if (ret == 0)
7039 return 0;
7040 wpa_printf(MSG_DEBUG, "nl80211: Failed to start radar detection: "
7041 "%d (%s)", ret, strerror(-ret));
Dmitry Shmidtea69e842013-05-13 14:52:28 -07007042 return -1;
7043}
7044
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007045#ifdef CONFIG_TDLS
7046
7047static int nl80211_send_tdls_mgmt(void *priv, const u8 *dst, u8 action_code,
7048 u8 dialog_token, u16 status_code,
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07007049 u32 peer_capab, int initiator, const u8 *buf,
7050 size_t len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007051{
7052 struct i802_bss *bss = priv;
7053 struct wpa_driver_nl80211_data *drv = bss->drv;
7054 struct nl_msg *msg;
7055
7056 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
7057 return -EOPNOTSUPP;
7058
7059 if (!dst)
7060 return -EINVAL;
7061
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007062 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_TDLS_MGMT)) ||
7063 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
7064 nla_put_u8(msg, NL80211_ATTR_TDLS_ACTION, action_code) ||
7065 nla_put_u8(msg, NL80211_ATTR_TDLS_DIALOG_TOKEN, dialog_token) ||
7066 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status_code))
7067 goto fail;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07007068 if (peer_capab) {
7069 /*
7070 * The internal enum tdls_peer_capability definition is
7071 * currently identical with the nl80211 enum
7072 * nl80211_tdls_peer_capability, so no conversion is needed
7073 * here.
7074 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007075 if (nla_put_u32(msg, NL80211_ATTR_TDLS_PEER_CAPABILITY,
7076 peer_capab))
7077 goto fail;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07007078 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007079 if ((initiator &&
7080 nla_put_flag(msg, NL80211_ATTR_TDLS_INITIATOR)) ||
7081 nla_put(msg, NL80211_ATTR_IE, len, buf))
7082 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007083
7084 return send_and_recv_msgs(drv, msg, NULL, NULL);
7085
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007086fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007087 nlmsg_free(msg);
7088 return -ENOBUFS;
7089}
7090
7091
7092static int nl80211_tdls_oper(void *priv, enum tdls_oper oper, const u8 *peer)
7093{
7094 struct i802_bss *bss = priv;
7095 struct wpa_driver_nl80211_data *drv = bss->drv;
7096 struct nl_msg *msg;
7097 enum nl80211_tdls_operation nl80211_oper;
7098
7099 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
7100 return -EOPNOTSUPP;
7101
7102 switch (oper) {
7103 case TDLS_DISCOVERY_REQ:
7104 nl80211_oper = NL80211_TDLS_DISCOVERY_REQ;
7105 break;
7106 case TDLS_SETUP:
7107 nl80211_oper = NL80211_TDLS_SETUP;
7108 break;
7109 case TDLS_TEARDOWN:
7110 nl80211_oper = NL80211_TDLS_TEARDOWN;
7111 break;
7112 case TDLS_ENABLE_LINK:
7113 nl80211_oper = NL80211_TDLS_ENABLE_LINK;
7114 break;
7115 case TDLS_DISABLE_LINK:
7116 nl80211_oper = NL80211_TDLS_DISABLE_LINK;
7117 break;
7118 case TDLS_ENABLE:
7119 return 0;
7120 case TDLS_DISABLE:
7121 return 0;
7122 default:
7123 return -EINVAL;
7124 }
7125
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007126 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_TDLS_OPER)) ||
7127 nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, nl80211_oper) ||
7128 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer)) {
7129 nlmsg_free(msg);
7130 return -ENOBUFS;
7131 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007132
7133 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007134}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007135
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007136
7137static int
7138nl80211_tdls_enable_channel_switch(void *priv, const u8 *addr, u8 oper_class,
7139 const struct hostapd_freq_params *params)
7140{
7141 struct i802_bss *bss = priv;
7142 struct wpa_driver_nl80211_data *drv = bss->drv;
7143 struct nl_msg *msg;
7144 int ret = -ENOBUFS;
7145
7146 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT) ||
7147 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_CHANNEL_SWITCH))
7148 return -EOPNOTSUPP;
7149
7150 wpa_printf(MSG_DEBUG, "nl80211: Enable TDLS channel switch " MACSTR
7151 " oper_class=%u freq=%u",
7152 MAC2STR(addr), oper_class, params->freq);
7153 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_TDLS_CHANNEL_SWITCH);
7154 if (!msg ||
7155 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
7156 nla_put_u8(msg, NL80211_ATTR_OPER_CLASS, oper_class) ||
7157 (ret = nl80211_put_freq_params(msg, params))) {
7158 nlmsg_free(msg);
7159 wpa_printf(MSG_DEBUG, "nl80211: Could not build TDLS chan switch");
7160 return ret;
7161 }
7162
7163 return send_and_recv_msgs(drv, msg, NULL, NULL);
7164}
7165
7166
7167static int
7168nl80211_tdls_disable_channel_switch(void *priv, const u8 *addr)
7169{
7170 struct i802_bss *bss = priv;
7171 struct wpa_driver_nl80211_data *drv = bss->drv;
7172 struct nl_msg *msg;
7173
7174 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT) ||
7175 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_CHANNEL_SWITCH))
7176 return -EOPNOTSUPP;
7177
7178 wpa_printf(MSG_DEBUG, "nl80211: Disable TDLS channel switch " MACSTR,
7179 MAC2STR(addr));
7180 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_TDLS_CANCEL_CHANNEL_SWITCH);
7181 if (!msg ||
7182 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
7183 nlmsg_free(msg);
7184 wpa_printf(MSG_DEBUG,
7185 "nl80211: Could not build TDLS cancel chan switch");
7186 return -ENOBUFS;
7187 }
7188
7189 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007190}
7191
7192#endif /* CONFIG TDLS */
7193
7194
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007195static int driver_nl80211_set_key(const char *ifname, void *priv,
7196 enum wpa_alg alg, const u8 *addr,
7197 int key_idx, int set_tx,
7198 const u8 *seq, size_t seq_len,
7199 const u8 *key, size_t key_len)
7200{
7201 struct i802_bss *bss = priv;
7202 return wpa_driver_nl80211_set_key(ifname, bss, alg, addr, key_idx,
7203 set_tx, seq, seq_len, key, key_len);
7204}
7205
7206
7207static int driver_nl80211_scan2(void *priv,
7208 struct wpa_driver_scan_params *params)
7209{
7210 struct i802_bss *bss = priv;
7211 return wpa_driver_nl80211_scan(bss, params);
7212}
7213
7214
7215static int driver_nl80211_deauthenticate(void *priv, const u8 *addr,
7216 int reason_code)
7217{
7218 struct i802_bss *bss = priv;
7219 return wpa_driver_nl80211_deauthenticate(bss, addr, reason_code);
7220}
7221
7222
7223static int driver_nl80211_authenticate(void *priv,
7224 struct wpa_driver_auth_params *params)
7225{
7226 struct i802_bss *bss = priv;
7227 return wpa_driver_nl80211_authenticate(bss, params);
7228}
7229
7230
7231static void driver_nl80211_deinit(void *priv)
7232{
7233 struct i802_bss *bss = priv;
7234 wpa_driver_nl80211_deinit(bss);
7235}
7236
7237
7238static int driver_nl80211_if_remove(void *priv, enum wpa_driver_if_type type,
7239 const char *ifname)
7240{
7241 struct i802_bss *bss = priv;
7242 return wpa_driver_nl80211_if_remove(bss, type, ifname);
7243}
7244
7245
7246static int driver_nl80211_send_mlme(void *priv, const u8 *data,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007247 size_t data_len, int noack,
7248 unsigned int freq)
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007249{
7250 struct i802_bss *bss = priv;
7251 return wpa_driver_nl80211_send_mlme(bss, data, data_len, noack,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007252 freq, 0, 0, 0);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007253}
7254
7255
7256static int driver_nl80211_sta_remove(void *priv, const u8 *addr)
7257{
7258 struct i802_bss *bss = priv;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007259 return wpa_driver_nl80211_sta_remove(bss, addr, -1, 0);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007260}
7261
7262
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007263static int driver_nl80211_set_sta_vlan(void *priv, const u8 *addr,
7264 const char *ifname, int vlan_id)
7265{
7266 struct i802_bss *bss = priv;
7267 return i802_set_sta_vlan(bss, addr, ifname, vlan_id);
7268}
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007269
7270
7271static int driver_nl80211_read_sta_data(void *priv,
7272 struct hostap_sta_driver_data *data,
7273 const u8 *addr)
7274{
7275 struct i802_bss *bss = priv;
7276 return i802_read_sta_data(bss, data, addr);
7277}
7278
7279
7280static int driver_nl80211_send_action(void *priv, unsigned int freq,
7281 unsigned int wait_time,
7282 const u8 *dst, const u8 *src,
7283 const u8 *bssid,
7284 const u8 *data, size_t data_len,
7285 int no_cck)
7286{
7287 struct i802_bss *bss = priv;
7288 return wpa_driver_nl80211_send_action(bss, freq, wait_time, dst, src,
7289 bssid, data, data_len, no_cck);
7290}
7291
7292
7293static int driver_nl80211_probe_req_report(void *priv, int report)
7294{
7295 struct i802_bss *bss = priv;
7296 return wpa_driver_nl80211_probe_req_report(bss, report);
7297}
7298
7299
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007300static int wpa_driver_nl80211_update_ft_ies(void *priv, const u8 *md,
7301 const u8 *ies, size_t ies_len)
7302{
7303 int ret;
7304 struct nl_msg *msg;
7305 struct i802_bss *bss = priv;
7306 struct wpa_driver_nl80211_data *drv = bss->drv;
7307 u16 mdid = WPA_GET_LE16(md);
7308
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007309 wpa_printf(MSG_DEBUG, "nl80211: Updating FT IEs");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007310 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_UPDATE_FT_IES)) ||
7311 nla_put(msg, NL80211_ATTR_IE, ies_len, ies) ||
7312 nla_put_u16(msg, NL80211_ATTR_MDID, mdid)) {
7313 nlmsg_free(msg);
7314 return -ENOBUFS;
7315 }
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007316
7317 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7318 if (ret) {
7319 wpa_printf(MSG_DEBUG, "nl80211: update_ft_ies failed "
7320 "err=%d (%s)", ret, strerror(-ret));
7321 }
7322
7323 return ret;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007324}
7325
7326
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007327const u8 * wpa_driver_nl80211_get_macaddr(void *priv)
7328{
7329 struct i802_bss *bss = priv;
7330 struct wpa_driver_nl80211_data *drv = bss->drv;
7331
7332 if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE)
7333 return NULL;
7334
7335 return bss->addr;
7336}
7337
7338
Dmitry Shmidt56052862013-10-04 10:23:25 -07007339static const char * scan_state_str(enum scan_states scan_state)
7340{
7341 switch (scan_state) {
7342 case NO_SCAN:
7343 return "NO_SCAN";
7344 case SCAN_REQUESTED:
7345 return "SCAN_REQUESTED";
7346 case SCAN_STARTED:
7347 return "SCAN_STARTED";
7348 case SCAN_COMPLETED:
7349 return "SCAN_COMPLETED";
7350 case SCAN_ABORTED:
7351 return "SCAN_ABORTED";
7352 case SCHED_SCAN_STARTED:
7353 return "SCHED_SCAN_STARTED";
7354 case SCHED_SCAN_STOPPED:
7355 return "SCHED_SCAN_STOPPED";
7356 case SCHED_SCAN_RESULTS:
7357 return "SCHED_SCAN_RESULTS";
7358 }
7359
7360 return "??";
7361}
7362
7363
7364static int wpa_driver_nl80211_status(void *priv, char *buf, size_t buflen)
7365{
7366 struct i802_bss *bss = priv;
7367 struct wpa_driver_nl80211_data *drv = bss->drv;
7368 int res;
7369 char *pos, *end;
7370
7371 pos = buf;
7372 end = buf + buflen;
7373
7374 res = os_snprintf(pos, end - pos,
7375 "ifindex=%d\n"
7376 "ifname=%s\n"
7377 "brname=%s\n"
7378 "addr=" MACSTR "\n"
7379 "freq=%d\n"
7380 "%s%s%s%s%s",
7381 bss->ifindex,
7382 bss->ifname,
7383 bss->brname,
7384 MAC2STR(bss->addr),
7385 bss->freq,
7386 bss->beacon_set ? "beacon_set=1\n" : "",
7387 bss->added_if_into_bridge ?
7388 "added_if_into_bridge=1\n" : "",
7389 bss->added_bridge ? "added_bridge=1\n" : "",
7390 bss->in_deinit ? "in_deinit=1\n" : "",
7391 bss->if_dynamic ? "if_dynamic=1\n" : "");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007392 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt56052862013-10-04 10:23:25 -07007393 return pos - buf;
7394 pos += res;
7395
7396 if (bss->wdev_id_set) {
7397 res = os_snprintf(pos, end - pos, "wdev_id=%llu\n",
7398 (unsigned long long) bss->wdev_id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007399 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt56052862013-10-04 10:23:25 -07007400 return pos - buf;
7401 pos += res;
7402 }
7403
7404 res = os_snprintf(pos, end - pos,
7405 "phyname=%s\n"
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007406 "perm_addr=" MACSTR "\n"
Dmitry Shmidt56052862013-10-04 10:23:25 -07007407 "drv_ifindex=%d\n"
7408 "operstate=%d\n"
7409 "scan_state=%s\n"
7410 "auth_bssid=" MACSTR "\n"
7411 "auth_attempt_bssid=" MACSTR "\n"
7412 "bssid=" MACSTR "\n"
7413 "prev_bssid=" MACSTR "\n"
7414 "associated=%d\n"
7415 "assoc_freq=%u\n"
7416 "monitor_sock=%d\n"
7417 "monitor_ifidx=%d\n"
7418 "monitor_refcount=%d\n"
7419 "last_mgmt_freq=%u\n"
7420 "eapol_tx_sock=%d\n"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007421 "%s%s%s%s%s%s%s%s%s%s%s%s%s",
Dmitry Shmidt56052862013-10-04 10:23:25 -07007422 drv->phyname,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007423 MAC2STR(drv->perm_addr),
Dmitry Shmidt56052862013-10-04 10:23:25 -07007424 drv->ifindex,
7425 drv->operstate,
7426 scan_state_str(drv->scan_state),
7427 MAC2STR(drv->auth_bssid),
7428 MAC2STR(drv->auth_attempt_bssid),
7429 MAC2STR(drv->bssid),
7430 MAC2STR(drv->prev_bssid),
7431 drv->associated,
7432 drv->assoc_freq,
7433 drv->monitor_sock,
7434 drv->monitor_ifidx,
7435 drv->monitor_refcount,
7436 drv->last_mgmt_freq,
7437 drv->eapol_tx_sock,
7438 drv->ignore_if_down_event ?
7439 "ignore_if_down_event=1\n" : "",
7440 drv->scan_complete_events ?
7441 "scan_complete_events=1\n" : "",
7442 drv->disabled_11b_rates ?
7443 "disabled_11b_rates=1\n" : "",
7444 drv->pending_remain_on_chan ?
7445 "pending_remain_on_chan=1\n" : "",
7446 drv->in_interface_list ? "in_interface_list=1\n" : "",
7447 drv->device_ap_sme ? "device_ap_sme=1\n" : "",
7448 drv->poll_command_supported ?
7449 "poll_command_supported=1\n" : "",
7450 drv->data_tx_status ? "data_tx_status=1\n" : "",
7451 drv->scan_for_auth ? "scan_for_auth=1\n" : "",
7452 drv->retry_auth ? "retry_auth=1\n" : "",
7453 drv->use_monitor ? "use_monitor=1\n" : "",
7454 drv->ignore_next_local_disconnect ?
7455 "ignore_next_local_disconnect=1\n" : "",
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07007456 drv->ignore_next_local_deauth ?
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007457 "ignore_next_local_deauth=1\n" : "");
7458 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt56052862013-10-04 10:23:25 -07007459 return pos - buf;
7460 pos += res;
7461
7462 if (drv->has_capability) {
7463 res = os_snprintf(pos, end - pos,
7464 "capa.key_mgmt=0x%x\n"
7465 "capa.enc=0x%x\n"
7466 "capa.auth=0x%x\n"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007467 "capa.flags=0x%llx\n"
7468 "capa.rrm_flags=0x%x\n"
Dmitry Shmidt56052862013-10-04 10:23:25 -07007469 "capa.max_scan_ssids=%d\n"
7470 "capa.max_sched_scan_ssids=%d\n"
7471 "capa.sched_scan_supported=%d\n"
7472 "capa.max_match_sets=%d\n"
7473 "capa.max_remain_on_chan=%u\n"
7474 "capa.max_stations=%u\n"
7475 "capa.probe_resp_offloads=0x%x\n"
7476 "capa.max_acl_mac_addrs=%u\n"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007477 "capa.num_multichan_concurrent=%u\n"
7478 "capa.mac_addr_rand_sched_scan_supported=%d\n"
7479 "capa.mac_addr_rand_scan_supported=%d\n",
Dmitry Shmidt56052862013-10-04 10:23:25 -07007480 drv->capa.key_mgmt,
7481 drv->capa.enc,
7482 drv->capa.auth,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007483 (unsigned long long) drv->capa.flags,
7484 drv->capa.rrm_flags,
Dmitry Shmidt56052862013-10-04 10:23:25 -07007485 drv->capa.max_scan_ssids,
7486 drv->capa.max_sched_scan_ssids,
7487 drv->capa.sched_scan_supported,
7488 drv->capa.max_match_sets,
7489 drv->capa.max_remain_on_chan,
7490 drv->capa.max_stations,
7491 drv->capa.probe_resp_offloads,
7492 drv->capa.max_acl_mac_addrs,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007493 drv->capa.num_multichan_concurrent,
7494 drv->capa.mac_addr_rand_sched_scan_supported,
7495 drv->capa.mac_addr_rand_scan_supported);
7496 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt56052862013-10-04 10:23:25 -07007497 return pos - buf;
7498 pos += res;
7499 }
7500
7501 return pos - buf;
7502}
7503
7504
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007505static int set_beacon_data(struct nl_msg *msg, struct beacon_data *settings)
7506{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007507 if ((settings->head &&
7508 nla_put(msg, NL80211_ATTR_BEACON_HEAD,
7509 settings->head_len, settings->head)) ||
7510 (settings->tail &&
7511 nla_put(msg, NL80211_ATTR_BEACON_TAIL,
7512 settings->tail_len, settings->tail)) ||
7513 (settings->beacon_ies &&
7514 nla_put(msg, NL80211_ATTR_IE,
7515 settings->beacon_ies_len, settings->beacon_ies)) ||
7516 (settings->proberesp_ies &&
7517 nla_put(msg, NL80211_ATTR_IE_PROBE_RESP,
7518 settings->proberesp_ies_len, settings->proberesp_ies)) ||
7519 (settings->assocresp_ies &&
7520 nla_put(msg, NL80211_ATTR_IE_ASSOC_RESP,
7521 settings->assocresp_ies_len, settings->assocresp_ies)) ||
7522 (settings->probe_resp &&
7523 nla_put(msg, NL80211_ATTR_PROBE_RESP,
7524 settings->probe_resp_len, settings->probe_resp)))
7525 return -ENOBUFS;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007526
7527 return 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007528}
7529
7530
7531static int nl80211_switch_channel(void *priv, struct csa_settings *settings)
7532{
7533 struct nl_msg *msg;
7534 struct i802_bss *bss = priv;
7535 struct wpa_driver_nl80211_data *drv = bss->drv;
7536 struct nlattr *beacon_csa;
7537 int ret = -ENOBUFS;
7538
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08007539 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 -08007540 settings->cs_count, settings->block_tx,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08007541 settings->freq_params.freq, settings->freq_params.bandwidth,
7542 settings->freq_params.center_freq1,
7543 settings->freq_params.center_freq2);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007544
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007545 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_AP_CSA)) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007546 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support channel switch command");
7547 return -EOPNOTSUPP;
7548 }
7549
7550 if ((drv->nlmode != NL80211_IFTYPE_AP) &&
7551 (drv->nlmode != NL80211_IFTYPE_P2P_GO))
7552 return -EOPNOTSUPP;
7553
7554 /* check settings validity */
7555 if (!settings->beacon_csa.tail ||
7556 ((settings->beacon_csa.tail_len <=
7557 settings->counter_offset_beacon) ||
7558 (settings->beacon_csa.tail[settings->counter_offset_beacon] !=
7559 settings->cs_count)))
7560 return -EINVAL;
7561
7562 if (settings->beacon_csa.probe_resp &&
7563 ((settings->beacon_csa.probe_resp_len <=
7564 settings->counter_offset_presp) ||
7565 (settings->beacon_csa.probe_resp[settings->counter_offset_presp] !=
7566 settings->cs_count)))
7567 return -EINVAL;
7568
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007569 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_CHANNEL_SWITCH)) ||
7570 nla_put_u32(msg, NL80211_ATTR_CH_SWITCH_COUNT,
7571 settings->cs_count) ||
7572 (ret = nl80211_put_freq_params(msg, &settings->freq_params)) ||
7573 (settings->block_tx &&
7574 nla_put_flag(msg, NL80211_ATTR_CH_SWITCH_BLOCK_TX)))
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007575 goto error;
7576
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007577 /* beacon_after params */
7578 ret = set_beacon_data(msg, &settings->beacon_after);
7579 if (ret)
7580 goto error;
7581
7582 /* beacon_csa params */
7583 beacon_csa = nla_nest_start(msg, NL80211_ATTR_CSA_IES);
7584 if (!beacon_csa)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007585 goto fail;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007586
7587 ret = set_beacon_data(msg, &settings->beacon_csa);
7588 if (ret)
7589 goto error;
7590
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007591 if (nla_put_u16(msg, NL80211_ATTR_CSA_C_OFF_BEACON,
7592 settings->counter_offset_beacon) ||
7593 (settings->beacon_csa.probe_resp &&
7594 nla_put_u16(msg, NL80211_ATTR_CSA_C_OFF_PRESP,
7595 settings->counter_offset_presp)))
7596 goto fail;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007597
7598 nla_nest_end(msg, beacon_csa);
7599 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7600 if (ret) {
7601 wpa_printf(MSG_DEBUG, "nl80211: switch_channel failed err=%d (%s)",
7602 ret, strerror(-ret));
7603 }
7604 return ret;
7605
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007606fail:
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007607 ret = -ENOBUFS;
7608error:
7609 nlmsg_free(msg);
7610 wpa_printf(MSG_DEBUG, "nl80211: Could not build channel switch request");
7611 return ret;
7612}
7613
7614
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007615static int nl80211_add_ts(void *priv, u8 tsid, const u8 *addr,
7616 u8 user_priority, u16 admitted_time)
7617{
7618 struct i802_bss *bss = priv;
7619 struct wpa_driver_nl80211_data *drv = bss->drv;
7620 struct nl_msg *msg;
7621 int ret;
7622
7623 wpa_printf(MSG_DEBUG,
7624 "nl80211: add_ts request: tsid=%u admitted_time=%u up=%d",
7625 tsid, admitted_time, user_priority);
7626
7627 if (!is_sta_interface(drv->nlmode))
7628 return -ENOTSUP;
7629
7630 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_ADD_TX_TS);
7631 if (!msg ||
7632 nla_put_u8(msg, NL80211_ATTR_TSID, tsid) ||
7633 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
7634 nla_put_u8(msg, NL80211_ATTR_USER_PRIO, user_priority) ||
7635 nla_put_u16(msg, NL80211_ATTR_ADMITTED_TIME, admitted_time)) {
7636 nlmsg_free(msg);
7637 return -ENOBUFS;
7638 }
7639
7640 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7641 if (ret)
7642 wpa_printf(MSG_DEBUG, "nl80211: add_ts failed err=%d (%s)",
7643 ret, strerror(-ret));
7644 return ret;
7645}
7646
7647
7648static int nl80211_del_ts(void *priv, u8 tsid, const u8 *addr)
7649{
7650 struct i802_bss *bss = priv;
7651 struct wpa_driver_nl80211_data *drv = bss->drv;
7652 struct nl_msg *msg;
7653 int ret;
7654
7655 wpa_printf(MSG_DEBUG, "nl80211: del_ts request: tsid=%u", tsid);
7656
7657 if (!is_sta_interface(drv->nlmode))
7658 return -ENOTSUP;
7659
7660 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_DEL_TX_TS)) ||
7661 nla_put_u8(msg, NL80211_ATTR_TSID, tsid) ||
7662 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
7663 nlmsg_free(msg);
7664 return -ENOBUFS;
7665 }
7666
7667 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7668 if (ret)
7669 wpa_printf(MSG_DEBUG, "nl80211: del_ts failed err=%d (%s)",
7670 ret, strerror(-ret));
7671 return ret;
7672}
7673
7674
Dmitry Shmidta38abf92014-03-06 13:38:44 -08007675#ifdef CONFIG_TESTING_OPTIONS
7676static int cmd_reply_handler(struct nl_msg *msg, void *arg)
7677{
7678 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
7679 struct wpabuf *buf = arg;
7680
7681 if (!buf)
7682 return NL_SKIP;
7683
7684 if ((size_t) genlmsg_attrlen(gnlh, 0) > wpabuf_tailroom(buf)) {
7685 wpa_printf(MSG_INFO, "nl80211: insufficient buffer space for reply");
7686 return NL_SKIP;
7687 }
7688
7689 wpabuf_put_data(buf, genlmsg_attrdata(gnlh, 0),
7690 genlmsg_attrlen(gnlh, 0));
7691
7692 return NL_SKIP;
7693}
7694#endif /* CONFIG_TESTING_OPTIONS */
7695
7696
7697static int vendor_reply_handler(struct nl_msg *msg, void *arg)
7698{
7699 struct nlattr *tb[NL80211_ATTR_MAX + 1];
7700 struct nlattr *nl_vendor_reply, *nl;
7701 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
7702 struct wpabuf *buf = arg;
7703 int rem;
7704
7705 if (!buf)
7706 return NL_SKIP;
7707
7708 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
7709 genlmsg_attrlen(gnlh, 0), NULL);
7710 nl_vendor_reply = tb[NL80211_ATTR_VENDOR_DATA];
7711
7712 if (!nl_vendor_reply)
7713 return NL_SKIP;
7714
7715 if ((size_t) nla_len(nl_vendor_reply) > wpabuf_tailroom(buf)) {
7716 wpa_printf(MSG_INFO, "nl80211: Vendor command: insufficient buffer space for reply");
7717 return NL_SKIP;
7718 }
7719
7720 nla_for_each_nested(nl, nl_vendor_reply, rem) {
7721 wpabuf_put_data(buf, nla_data(nl), nla_len(nl));
7722 }
7723
7724 return NL_SKIP;
7725}
7726
7727
7728static int nl80211_vendor_cmd(void *priv, unsigned int vendor_id,
7729 unsigned int subcmd, const u8 *data,
7730 size_t data_len, struct wpabuf *buf)
7731{
7732 struct i802_bss *bss = priv;
7733 struct wpa_driver_nl80211_data *drv = bss->drv;
7734 struct nl_msg *msg;
7735 int ret;
7736
Dmitry Shmidta38abf92014-03-06 13:38:44 -08007737#ifdef CONFIG_TESTING_OPTIONS
7738 if (vendor_id == 0xffffffff) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007739 msg = nlmsg_alloc();
7740 if (!msg)
7741 return -ENOMEM;
7742
Dmitry Shmidta38abf92014-03-06 13:38:44 -08007743 nl80211_cmd(drv, msg, 0, subcmd);
7744 if (nlmsg_append(msg, (void *) data, data_len, NLMSG_ALIGNTO) <
7745 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007746 goto fail;
Dmitry Shmidta38abf92014-03-06 13:38:44 -08007747 ret = send_and_recv_msgs(drv, msg, cmd_reply_handler, buf);
7748 if (ret)
7749 wpa_printf(MSG_DEBUG, "nl80211: command failed err=%d",
7750 ret);
7751 return ret;
7752 }
7753#endif /* CONFIG_TESTING_OPTIONS */
7754
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007755 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_VENDOR)) ||
7756 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, vendor_id) ||
7757 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD, subcmd) ||
7758 (data &&
7759 nla_put(msg, NL80211_ATTR_VENDOR_DATA, data_len, data)))
7760 goto fail;
Dmitry Shmidta38abf92014-03-06 13:38:44 -08007761
7762 ret = send_and_recv_msgs(drv, msg, vendor_reply_handler, buf);
7763 if (ret)
7764 wpa_printf(MSG_DEBUG, "nl80211: vendor command failed err=%d",
7765 ret);
7766 return ret;
7767
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007768fail:
Dmitry Shmidta38abf92014-03-06 13:38:44 -08007769 nlmsg_free(msg);
7770 return -ENOBUFS;
7771}
7772
7773
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007774static int nl80211_set_qos_map(void *priv, const u8 *qos_map_set,
7775 u8 qos_map_set_len)
7776{
7777 struct i802_bss *bss = priv;
7778 struct wpa_driver_nl80211_data *drv = bss->drv;
7779 struct nl_msg *msg;
7780 int ret;
7781
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007782 wpa_hexdump(MSG_DEBUG, "nl80211: Setting QoS Map",
7783 qos_map_set, qos_map_set_len);
7784
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007785 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_QOS_MAP)) ||
7786 nla_put(msg, NL80211_ATTR_QOS_MAP, qos_map_set_len, qos_map_set)) {
7787 nlmsg_free(msg);
7788 return -ENOBUFS;
7789 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007790
7791 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7792 if (ret)
7793 wpa_printf(MSG_DEBUG, "nl80211: Setting QoS Map failed");
7794
7795 return ret;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007796}
7797
7798
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07007799static int nl80211_set_wowlan(void *priv,
7800 const struct wowlan_triggers *triggers)
7801{
7802 struct i802_bss *bss = priv;
7803 struct wpa_driver_nl80211_data *drv = bss->drv;
7804 struct nl_msg *msg;
7805 struct nlattr *wowlan_triggers;
7806 int ret;
7807
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07007808 wpa_printf(MSG_DEBUG, "nl80211: Setting wowlan");
7809
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007810 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_SET_WOWLAN)) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007811 !(wowlan_triggers = nla_nest_start(msg,
7812 NL80211_ATTR_WOWLAN_TRIGGERS)) ||
7813 (triggers->any &&
7814 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
7815 (triggers->disconnect &&
7816 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
7817 (triggers->magic_pkt &&
7818 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
7819 (triggers->gtk_rekey_failure &&
7820 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
7821 (triggers->eap_identity_req &&
7822 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
7823 (triggers->four_way_handshake &&
7824 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
7825 (triggers->rfkill_release &&
7826 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))) {
7827 nlmsg_free(msg);
7828 return -ENOBUFS;
7829 }
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07007830
7831 nla_nest_end(msg, wowlan_triggers);
7832
7833 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7834 if (ret)
7835 wpa_printf(MSG_DEBUG, "nl80211: Setting wowlan failed");
7836
7837 return ret;
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07007838}
7839
7840
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007841static int nl80211_roaming(void *priv, int allowed, const u8 *bssid)
7842{
7843 struct i802_bss *bss = priv;
7844 struct wpa_driver_nl80211_data *drv = bss->drv;
7845 struct nl_msg *msg;
7846 struct nlattr *params;
7847
7848 wpa_printf(MSG_DEBUG, "nl80211: Roaming policy: allowed=%d", allowed);
7849
7850 if (!drv->roaming_vendor_cmd_avail) {
7851 wpa_printf(MSG_DEBUG,
7852 "nl80211: Ignore roaming policy change since driver does not provide command for setting it");
7853 return -1;
7854 }
7855
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007856 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
7857 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
7858 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
7859 QCA_NL80211_VENDOR_SUBCMD_ROAMING) ||
7860 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
7861 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_ROAMING_POLICY,
7862 allowed ? QCA_ROAMING_ALLOWED_WITHIN_ESS :
7863 QCA_ROAMING_NOT_ALLOWED) ||
7864 (bssid &&
7865 nla_put(msg, QCA_WLAN_VENDOR_ATTR_MAC_ADDR, ETH_ALEN, bssid))) {
7866 nlmsg_free(msg);
7867 return -1;
7868 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007869 nla_nest_end(msg, params);
7870
7871 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007872}
7873
7874
7875static int nl80211_set_mac_addr(void *priv, const u8 *addr)
7876{
7877 struct i802_bss *bss = priv;
7878 struct wpa_driver_nl80211_data *drv = bss->drv;
7879 int new_addr = addr != NULL;
7880
7881 if (!addr)
7882 addr = drv->perm_addr;
7883
7884 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0) < 0)
7885 return -1;
7886
7887 if (linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname, addr) < 0)
7888 {
7889 wpa_printf(MSG_DEBUG,
7890 "nl80211: failed to set_mac_addr for %s to " MACSTR,
7891 bss->ifname, MAC2STR(addr));
7892 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname,
7893 1) < 0) {
7894 wpa_printf(MSG_DEBUG,
7895 "nl80211: Could not restore interface UP after failed set_mac_addr");
7896 }
7897 return -1;
7898 }
7899
7900 wpa_printf(MSG_DEBUG, "nl80211: set_mac_addr for %s to " MACSTR,
7901 bss->ifname, MAC2STR(addr));
7902 drv->addr_changed = new_addr;
7903 os_memcpy(bss->addr, addr, ETH_ALEN);
7904
7905 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1) < 0)
7906 {
7907 wpa_printf(MSG_DEBUG,
7908 "nl80211: Could not restore interface UP after set_mac_addr");
7909 }
7910
7911 return 0;
7912}
7913
7914
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007915#ifdef CONFIG_MESH
7916
7917static int wpa_driver_nl80211_init_mesh(void *priv)
7918{
7919 if (wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_MESH_POINT)) {
7920 wpa_printf(MSG_INFO,
7921 "nl80211: Failed to set interface into mesh mode");
7922 return -1;
7923 }
7924 return 0;
7925}
7926
7927
Dmitry Shmidtff787d52015-01-12 13:01:47 -08007928static int nl80211_put_mesh_id(struct nl_msg *msg, const u8 *mesh_id,
7929 size_t mesh_id_len)
7930{
7931 if (mesh_id) {
7932 wpa_hexdump_ascii(MSG_DEBUG, " * Mesh ID (SSID)",
7933 mesh_id, mesh_id_len);
7934 return nla_put(msg, NL80211_ATTR_MESH_ID, mesh_id_len, mesh_id);
7935 }
7936
7937 return 0;
7938}
7939
7940
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007941static int nl80211_join_mesh(struct i802_bss *bss,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007942 struct wpa_driver_mesh_join_params *params)
7943{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007944 struct wpa_driver_nl80211_data *drv = bss->drv;
7945 struct nl_msg *msg;
7946 struct nlattr *container;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08007947 int ret = -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007948
7949 wpa_printf(MSG_DEBUG, "nl80211: mesh join (ifindex=%d)", drv->ifindex);
7950 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_JOIN_MESH);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08007951 if (!msg ||
7952 nl80211_put_freq_params(msg, &params->freq) ||
7953 nl80211_put_basic_rates(msg, params->basic_rates) ||
7954 nl80211_put_mesh_id(msg, params->meshid, params->meshid_len) ||
7955 nl80211_put_beacon_int(msg, params->beacon_int))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007956 goto fail;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007957
7958 wpa_printf(MSG_DEBUG, " * flags=%08X", params->flags);
7959
7960 container = nla_nest_start(msg, NL80211_ATTR_MESH_SETUP);
7961 if (!container)
7962 goto fail;
7963
7964 if (params->ies) {
7965 wpa_hexdump(MSG_DEBUG, " * IEs", params->ies, params->ie_len);
7966 if (nla_put(msg, NL80211_MESH_SETUP_IE, params->ie_len,
7967 params->ies))
7968 goto fail;
7969 }
7970 /* WPA_DRIVER_MESH_FLAG_OPEN_AUTH is treated as default by nl80211 */
7971 if (params->flags & WPA_DRIVER_MESH_FLAG_SAE_AUTH) {
7972 if (nla_put_u8(msg, NL80211_MESH_SETUP_AUTH_PROTOCOL, 0x1) ||
7973 nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_AUTH))
7974 goto fail;
7975 }
7976 if ((params->flags & WPA_DRIVER_MESH_FLAG_AMPE) &&
7977 nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_AMPE))
7978 goto fail;
7979 if ((params->flags & WPA_DRIVER_MESH_FLAG_USER_MPM) &&
7980 nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_MPM))
7981 goto fail;
7982 nla_nest_end(msg, container);
7983
7984 container = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
7985 if (!container)
7986 goto fail;
7987
7988 if (!(params->conf.flags & WPA_DRIVER_MESH_CONF_FLAG_AUTO_PLINKS) &&
7989 nla_put_u32(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS, 0))
7990 goto fail;
7991 if ((params->conf.flags & WPA_DRIVER_MESH_FLAG_DRIVER_MPM) &&
7992 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
7993 params->max_peer_links))
7994 goto fail;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08007995
7996 /*
7997 * Set NL80211_MESHCONF_PLINK_TIMEOUT even if user mpm is used because
7998 * the timer could disconnect stations even in that case.
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08007999 */
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008000 if (nla_put_u32(msg, NL80211_MESHCONF_PLINK_TIMEOUT,
8001 params->conf.peer_link_timeout)) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08008002 wpa_printf(MSG_ERROR, "nl80211: Failed to set PLINK_TIMEOUT");
8003 goto fail;
8004 }
8005
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008006 nla_nest_end(msg, container);
8007
8008 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8009 msg = NULL;
8010 if (ret) {
8011 wpa_printf(MSG_DEBUG, "nl80211: mesh join failed: ret=%d (%s)",
8012 ret, strerror(-ret));
8013 goto fail;
8014 }
8015 ret = 0;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08008016 bss->freq = params->freq.freq;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008017 wpa_printf(MSG_DEBUG, "nl80211: mesh join request send successfully");
8018
8019fail:
8020 nlmsg_free(msg);
8021 return ret;
8022}
8023
8024
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008025static int
8026wpa_driver_nl80211_join_mesh(void *priv,
8027 struct wpa_driver_mesh_join_params *params)
8028{
8029 struct i802_bss *bss = priv;
8030 int ret, timeout;
8031
8032 timeout = params->conf.peer_link_timeout;
8033
8034 /* Disable kernel inactivity timer */
8035 if (params->flags & WPA_DRIVER_MESH_FLAG_USER_MPM)
8036 params->conf.peer_link_timeout = 0;
8037
8038 ret = nl80211_join_mesh(bss, params);
8039 if (ret == -EINVAL && params->conf.peer_link_timeout == 0) {
8040 wpa_printf(MSG_DEBUG,
8041 "nl80211: Mesh join retry for peer_link_timeout");
8042 /*
8043 * Old kernel does not support setting
8044 * NL80211_MESHCONF_PLINK_TIMEOUT to zero, so set 60 seconds
8045 * into future from peer_link_timeout.
8046 */
8047 params->conf.peer_link_timeout = timeout + 60;
8048 ret = nl80211_join_mesh(priv, params);
8049 }
8050
8051 params->conf.peer_link_timeout = timeout;
8052 return ret;
8053}
8054
8055
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008056static int wpa_driver_nl80211_leave_mesh(void *priv)
8057{
8058 struct i802_bss *bss = priv;
8059 struct wpa_driver_nl80211_data *drv = bss->drv;
8060 struct nl_msg *msg;
8061 int ret;
8062
8063 wpa_printf(MSG_DEBUG, "nl80211: mesh leave (ifindex=%d)", drv->ifindex);
8064 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_LEAVE_MESH);
8065 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8066 if (ret) {
8067 wpa_printf(MSG_DEBUG, "nl80211: mesh leave failed: ret=%d (%s)",
8068 ret, strerror(-ret));
8069 } else {
8070 wpa_printf(MSG_DEBUG,
8071 "nl80211: mesh leave request send successfully");
8072 }
8073
8074 if (wpa_driver_nl80211_set_mode(drv->first_bss,
8075 NL80211_IFTYPE_STATION)) {
8076 wpa_printf(MSG_INFO,
8077 "nl80211: Failed to set interface into station mode");
8078 }
8079 return ret;
8080}
8081
8082#endif /* CONFIG_MESH */
8083
8084
8085static int wpa_driver_br_add_ip_neigh(void *priv, u8 version,
8086 const u8 *ipaddr, int prefixlen,
8087 const u8 *addr)
8088{
8089#ifdef CONFIG_LIBNL3_ROUTE
8090 struct i802_bss *bss = priv;
8091 struct wpa_driver_nl80211_data *drv = bss->drv;
8092 struct rtnl_neigh *rn;
8093 struct nl_addr *nl_ipaddr = NULL;
8094 struct nl_addr *nl_lladdr = NULL;
8095 int family, addrsize;
8096 int res;
8097
8098 if (!ipaddr || prefixlen == 0 || !addr)
8099 return -EINVAL;
8100
8101 if (bss->br_ifindex == 0) {
8102 wpa_printf(MSG_DEBUG,
8103 "nl80211: bridge must be set before adding an ip neigh to it");
8104 return -1;
8105 }
8106
8107 if (!drv->rtnl_sk) {
8108 wpa_printf(MSG_DEBUG,
8109 "nl80211: nl_sock for NETLINK_ROUTE is not initialized");
8110 return -1;
8111 }
8112
8113 if (version == 4) {
8114 family = AF_INET;
8115 addrsize = 4;
8116 } else if (version == 6) {
8117 family = AF_INET6;
8118 addrsize = 16;
8119 } else {
8120 return -EINVAL;
8121 }
8122
8123 rn = rtnl_neigh_alloc();
8124 if (rn == NULL)
8125 return -ENOMEM;
8126
8127 /* set the destination ip address for neigh */
8128 nl_ipaddr = nl_addr_build(family, (void *) ipaddr, addrsize);
8129 if (nl_ipaddr == NULL) {
8130 wpa_printf(MSG_DEBUG, "nl80211: nl_ipaddr build failed");
8131 res = -ENOMEM;
8132 goto errout;
8133 }
8134 nl_addr_set_prefixlen(nl_ipaddr, prefixlen);
8135 res = rtnl_neigh_set_dst(rn, nl_ipaddr);
8136 if (res) {
8137 wpa_printf(MSG_DEBUG,
8138 "nl80211: neigh set destination addr failed");
8139 goto errout;
8140 }
8141
8142 /* set the corresponding lladdr for neigh */
8143 nl_lladdr = nl_addr_build(AF_BRIDGE, (u8 *) addr, ETH_ALEN);
8144 if (nl_lladdr == NULL) {
8145 wpa_printf(MSG_DEBUG, "nl80211: neigh set lladdr failed");
8146 res = -ENOMEM;
8147 goto errout;
8148 }
8149 rtnl_neigh_set_lladdr(rn, nl_lladdr);
8150
8151 rtnl_neigh_set_ifindex(rn, bss->br_ifindex);
8152 rtnl_neigh_set_state(rn, NUD_PERMANENT);
8153
8154 res = rtnl_neigh_add(drv->rtnl_sk, rn, NLM_F_CREATE);
8155 if (res) {
8156 wpa_printf(MSG_DEBUG,
8157 "nl80211: Adding bridge ip neigh failed: %s",
8158 strerror(errno));
8159 }
8160errout:
8161 if (nl_lladdr)
8162 nl_addr_put(nl_lladdr);
8163 if (nl_ipaddr)
8164 nl_addr_put(nl_ipaddr);
8165 if (rn)
8166 rtnl_neigh_put(rn);
8167 return res;
8168#else /* CONFIG_LIBNL3_ROUTE */
8169 return -1;
8170#endif /* CONFIG_LIBNL3_ROUTE */
8171}
8172
8173
8174static int wpa_driver_br_delete_ip_neigh(void *priv, u8 version,
8175 const u8 *ipaddr)
8176{
8177#ifdef CONFIG_LIBNL3_ROUTE
8178 struct i802_bss *bss = priv;
8179 struct wpa_driver_nl80211_data *drv = bss->drv;
8180 struct rtnl_neigh *rn;
8181 struct nl_addr *nl_ipaddr;
8182 int family, addrsize;
8183 int res;
8184
8185 if (!ipaddr)
8186 return -EINVAL;
8187
8188 if (version == 4) {
8189 family = AF_INET;
8190 addrsize = 4;
8191 } else if (version == 6) {
8192 family = AF_INET6;
8193 addrsize = 16;
8194 } else {
8195 return -EINVAL;
8196 }
8197
8198 if (bss->br_ifindex == 0) {
8199 wpa_printf(MSG_DEBUG,
8200 "nl80211: bridge must be set to delete an ip neigh");
8201 return -1;
8202 }
8203
8204 if (!drv->rtnl_sk) {
8205 wpa_printf(MSG_DEBUG,
8206 "nl80211: nl_sock for NETLINK_ROUTE is not initialized");
8207 return -1;
8208 }
8209
8210 rn = rtnl_neigh_alloc();
8211 if (rn == NULL)
8212 return -ENOMEM;
8213
8214 /* set the destination ip address for neigh */
8215 nl_ipaddr = nl_addr_build(family, (void *) ipaddr, addrsize);
8216 if (nl_ipaddr == NULL) {
8217 wpa_printf(MSG_DEBUG, "nl80211: nl_ipaddr build failed");
8218 res = -ENOMEM;
8219 goto errout;
8220 }
8221 res = rtnl_neigh_set_dst(rn, nl_ipaddr);
8222 if (res) {
8223 wpa_printf(MSG_DEBUG,
8224 "nl80211: neigh set destination addr failed");
8225 goto errout;
8226 }
8227
8228 rtnl_neigh_set_ifindex(rn, bss->br_ifindex);
8229
8230 res = rtnl_neigh_delete(drv->rtnl_sk, rn, 0);
8231 if (res) {
8232 wpa_printf(MSG_DEBUG,
8233 "nl80211: Deleting bridge ip neigh failed: %s",
8234 strerror(errno));
8235 }
8236errout:
8237 if (nl_ipaddr)
8238 nl_addr_put(nl_ipaddr);
8239 if (rn)
8240 rtnl_neigh_put(rn);
8241 return res;
8242#else /* CONFIG_LIBNL3_ROUTE */
8243 return -1;
8244#endif /* CONFIG_LIBNL3_ROUTE */
8245}
8246
8247
8248static int linux_write_system_file(const char *path, unsigned int val)
8249{
8250 char buf[50];
8251 int fd, len;
8252
8253 len = os_snprintf(buf, sizeof(buf), "%u\n", val);
8254 if (os_snprintf_error(sizeof(buf), len))
8255 return -1;
8256
8257 fd = open(path, O_WRONLY);
8258 if (fd < 0)
8259 return -1;
8260
8261 if (write(fd, buf, len) < 0) {
8262 wpa_printf(MSG_DEBUG,
8263 "nl80211: Failed to write Linux system file: %s with the value of %d",
8264 path, val);
8265 close(fd);
8266 return -1;
8267 }
8268 close(fd);
8269
8270 return 0;
8271}
8272
8273
8274static const char * drv_br_port_attr_str(enum drv_br_port_attr attr)
8275{
8276 switch (attr) {
8277 case DRV_BR_PORT_ATTR_PROXYARP:
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07008278 return "proxyarp_wifi";
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008279 case DRV_BR_PORT_ATTR_HAIRPIN_MODE:
8280 return "hairpin_mode";
8281 }
8282
8283 return NULL;
8284}
8285
8286
8287static int wpa_driver_br_port_set_attr(void *priv, enum drv_br_port_attr attr,
8288 unsigned int val)
8289{
8290 struct i802_bss *bss = priv;
8291 char path[128];
8292 const char *attr_txt;
8293
8294 attr_txt = drv_br_port_attr_str(attr);
8295 if (attr_txt == NULL)
8296 return -EINVAL;
8297
8298 os_snprintf(path, sizeof(path), "/sys/class/net/%s/brport/%s",
8299 bss->ifname, attr_txt);
8300
8301 if (linux_write_system_file(path, val))
8302 return -1;
8303
8304 return 0;
8305}
8306
8307
8308static const char * drv_br_net_param_str(enum drv_br_net_param param)
8309{
8310 switch (param) {
8311 case DRV_BR_NET_PARAM_GARP_ACCEPT:
8312 return "arp_accept";
Dmitry Shmidt83474442015-04-15 13:47:09 -07008313 default:
8314 return NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008315 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008316}
8317
8318
8319static int wpa_driver_br_set_net_param(void *priv, enum drv_br_net_param param,
8320 unsigned int val)
8321{
8322 struct i802_bss *bss = priv;
8323 char path[128];
8324 const char *param_txt;
8325 int ip_version = 4;
8326
Dmitry Shmidt83474442015-04-15 13:47:09 -07008327 if (param == DRV_BR_MULTICAST_SNOOPING) {
8328 os_snprintf(path, sizeof(path),
8329 "/sys/devices/virtual/net/%s/bridge/multicast_snooping",
8330 bss->brname);
8331 goto set_val;
8332 }
8333
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008334 param_txt = drv_br_net_param_str(param);
8335 if (param_txt == NULL)
8336 return -EINVAL;
8337
8338 switch (param) {
8339 case DRV_BR_NET_PARAM_GARP_ACCEPT:
8340 ip_version = 4;
8341 break;
8342 default:
8343 return -EINVAL;
8344 }
8345
8346 os_snprintf(path, sizeof(path), "/proc/sys/net/ipv%d/conf/%s/%s",
8347 ip_version, bss->brname, param_txt);
8348
Dmitry Shmidt83474442015-04-15 13:47:09 -07008349set_val:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008350 if (linux_write_system_file(path, val))
8351 return -1;
8352
8353 return 0;
8354}
8355
8356
8357static int hw_mode_to_qca_acs(enum hostapd_hw_mode hw_mode)
8358{
8359 switch (hw_mode) {
8360 case HOSTAPD_MODE_IEEE80211B:
8361 return QCA_ACS_MODE_IEEE80211B;
8362 case HOSTAPD_MODE_IEEE80211G:
8363 return QCA_ACS_MODE_IEEE80211G;
8364 case HOSTAPD_MODE_IEEE80211A:
8365 return QCA_ACS_MODE_IEEE80211A;
8366 case HOSTAPD_MODE_IEEE80211AD:
8367 return QCA_ACS_MODE_IEEE80211AD;
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07008368 case HOSTAPD_MODE_IEEE80211ANY:
8369 return QCA_ACS_MODE_IEEE80211ANY;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008370 default:
8371 return -1;
8372 }
8373}
8374
8375
8376static int wpa_driver_do_acs(void *priv, struct drv_acs_params *params)
8377{
8378 struct i802_bss *bss = priv;
8379 struct wpa_driver_nl80211_data *drv = bss->drv;
8380 struct nl_msg *msg;
8381 struct nlattr *data;
8382 int ret;
8383 int mode;
8384
8385 mode = hw_mode_to_qca_acs(params->hw_mode);
8386 if (mode < 0)
8387 return -1;
8388
8389 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
8390 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
8391 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
8392 QCA_NL80211_VENDOR_SUBCMD_DO_ACS) ||
8393 !(data = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
8394 nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_ACS_HW_MODE, mode) ||
8395 (params->ht_enabled &&
8396 nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_HT_ENABLED)) ||
8397 (params->ht40_enabled &&
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07008398 nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_HT40_ENABLED)) ||
8399 (params->vht_enabled &&
8400 nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_VHT_ENABLED)) ||
8401 nla_put_u16(msg, QCA_WLAN_VENDOR_ATTR_ACS_CHWIDTH,
8402 params->ch_width) ||
8403 (params->ch_list_len &&
8404 nla_put(msg, QCA_WLAN_VENDOR_ATTR_ACS_CH_LIST, params->ch_list_len,
8405 params->ch_list))) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008406 nlmsg_free(msg);
8407 return -ENOBUFS;
8408 }
8409 nla_nest_end(msg, data);
8410
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07008411 wpa_printf(MSG_DEBUG,
8412 "nl80211: ACS Params: HW_MODE: %d HT: %d HT40: %d VHT: %d BW: %d CH_LIST_LEN: %u",
8413 params->hw_mode, params->ht_enabled, params->ht40_enabled,
8414 params->vht_enabled, params->ch_width, params->ch_list_len);
8415
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008416 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8417 if (ret) {
8418 wpa_printf(MSG_DEBUG,
8419 "nl80211: Failed to invoke driver ACS function: %s",
8420 strerror(errno));
8421 }
8422 return ret;
8423}
8424
8425
Ravi Joshie6ccb162015-07-16 17:45:41 -07008426static int nl80211_set_band(void *priv, enum set_band band)
8427{
8428 struct i802_bss *bss = priv;
8429 struct wpa_driver_nl80211_data *drv = bss->drv;
8430 struct nl_msg *msg;
8431 struct nlattr *data;
8432 int ret;
8433 enum qca_set_band qca_band;
8434
8435 if (!drv->setband_vendor_cmd_avail)
8436 return -1;
8437
8438 switch (band) {
8439 case WPA_SETBAND_AUTO:
8440 qca_band = QCA_SETBAND_AUTO;
8441 break;
8442 case WPA_SETBAND_5G:
8443 qca_band = QCA_SETBAND_5G;
8444 break;
8445 case WPA_SETBAND_2G:
8446 qca_band = QCA_SETBAND_2G;
8447 break;
8448 default:
8449 return -1;
8450 }
8451
8452 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
8453 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
8454 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
8455 QCA_NL80211_VENDOR_SUBCMD_SETBAND) ||
8456 !(data = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
8457 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_SETBAND_VALUE, qca_band)) {
8458 nlmsg_free(msg);
8459 return -ENOBUFS;
8460 }
8461 nla_nest_end(msg, data);
8462
8463 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8464 if (ret) {
8465 wpa_printf(MSG_DEBUG,
8466 "nl80211: Driver setband function failed: %s",
8467 strerror(errno));
8468 }
8469 return ret;
8470}
8471
8472
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008473const struct wpa_driver_ops wpa_driver_nl80211_ops = {
8474 .name = "nl80211",
8475 .desc = "Linux nl80211/cfg80211",
8476 .get_bssid = wpa_driver_nl80211_get_bssid,
8477 .get_ssid = wpa_driver_nl80211_get_ssid,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008478 .set_key = driver_nl80211_set_key,
8479 .scan2 = driver_nl80211_scan2,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008480 .sched_scan = wpa_driver_nl80211_sched_scan,
8481 .stop_sched_scan = wpa_driver_nl80211_stop_sched_scan,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008482 .get_scan_results2 = wpa_driver_nl80211_get_scan_results,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008483 .deauthenticate = driver_nl80211_deauthenticate,
8484 .authenticate = driver_nl80211_authenticate,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008485 .associate = wpa_driver_nl80211_associate,
8486 .global_init = nl80211_global_init,
8487 .global_deinit = nl80211_global_deinit,
8488 .init2 = wpa_driver_nl80211_init,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008489 .deinit = driver_nl80211_deinit,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008490 .get_capa = wpa_driver_nl80211_get_capa,
8491 .set_operstate = wpa_driver_nl80211_set_operstate,
8492 .set_supp_port = wpa_driver_nl80211_set_supp_port,
8493 .set_country = wpa_driver_nl80211_set_country,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008494 .get_country = wpa_driver_nl80211_get_country,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008495 .set_ap = wpa_driver_nl80211_set_ap,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07008496 .set_acl = wpa_driver_nl80211_set_acl,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008497 .if_add = wpa_driver_nl80211_if_add,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008498 .if_remove = driver_nl80211_if_remove,
8499 .send_mlme = driver_nl80211_send_mlme,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008500 .get_hw_feature_data = nl80211_get_hw_feature_data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008501 .sta_add = wpa_driver_nl80211_sta_add,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008502 .sta_remove = driver_nl80211_sta_remove,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008503 .hapd_send_eapol = wpa_driver_nl80211_hapd_send_eapol,
8504 .sta_set_flags = wpa_driver_nl80211_sta_set_flags,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008505 .hapd_init = i802_init,
8506 .hapd_deinit = i802_deinit,
Jouni Malinen75ecf522011-06-27 15:19:46 -07008507 .set_wds_sta = i802_set_wds_sta,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008508 .get_seqnum = i802_get_seqnum,
8509 .flush = i802_flush,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008510 .get_inact_sec = i802_get_inact_sec,
8511 .sta_clear_stats = i802_sta_clear_stats,
8512 .set_rts = i802_set_rts,
8513 .set_frag = i802_set_frag,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008514 .set_tx_queue_params = i802_set_tx_queue_params,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008515 .set_sta_vlan = driver_nl80211_set_sta_vlan,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008516 .sta_deauth = i802_sta_deauth,
8517 .sta_disassoc = i802_sta_disassoc,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008518 .read_sta_data = driver_nl80211_read_sta_data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008519 .set_freq = i802_set_freq,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008520 .send_action = driver_nl80211_send_action,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008521 .send_action_cancel_wait = wpa_driver_nl80211_send_action_cancel_wait,
8522 .remain_on_channel = wpa_driver_nl80211_remain_on_channel,
8523 .cancel_remain_on_channel =
8524 wpa_driver_nl80211_cancel_remain_on_channel,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008525 .probe_req_report = driver_nl80211_probe_req_report,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008526 .deinit_ap = wpa_driver_nl80211_deinit_ap,
Dmitry Shmidt04949592012-07-19 12:16:46 -07008527 .deinit_p2p_cli = wpa_driver_nl80211_deinit_p2p_cli,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008528 .resume = wpa_driver_nl80211_resume,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008529 .signal_monitor = nl80211_signal_monitor,
8530 .signal_poll = nl80211_signal_poll,
8531 .send_frame = nl80211_send_frame,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008532 .set_param = nl80211_set_param,
8533 .get_radio_name = nl80211_get_radio_name,
Jouni Malinen75ecf522011-06-27 15:19:46 -07008534 .add_pmkid = nl80211_add_pmkid,
8535 .remove_pmkid = nl80211_remove_pmkid,
8536 .flush_pmkid = nl80211_flush_pmkid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008537 .set_rekey_info = nl80211_set_rekey_info,
8538 .poll_client = nl80211_poll_client,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008539 .set_p2p_powersave = nl80211_set_p2p_powersave,
Dmitry Shmidtea69e842013-05-13 14:52:28 -07008540 .start_dfs_cac = nl80211_start_radar_detection,
8541 .stop_ap = wpa_driver_nl80211_stop_ap,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008542#ifdef CONFIG_TDLS
8543 .send_tdls_mgmt = nl80211_send_tdls_mgmt,
8544 .tdls_oper = nl80211_tdls_oper,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008545 .tdls_enable_channel_switch = nl80211_tdls_enable_channel_switch,
8546 .tdls_disable_channel_switch = nl80211_tdls_disable_channel_switch,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008547#endif /* CONFIG_TDLS */
Dmitry Shmidt700a1372013-03-15 14:14:44 -07008548 .update_ft_ies = wpa_driver_nl80211_update_ft_ies,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008549 .get_mac_addr = wpa_driver_nl80211_get_macaddr,
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008550 .get_survey = wpa_driver_nl80211_get_survey,
Dmitry Shmidt56052862013-10-04 10:23:25 -07008551 .status = wpa_driver_nl80211_status,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008552 .switch_channel = nl80211_switch_channel,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008553#ifdef ANDROID_P2P
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07008554 .set_noa = wpa_driver_set_p2p_noa,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008555 .get_noa = wpa_driver_get_p2p_noa,
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07008556 .set_ap_wps_ie = wpa_driver_set_ap_wps_p2p_ie,
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08008557#endif /* ANDROID_P2P */
Dmitry Shmidt738a26e2011-07-07 14:22:14 -07008558#ifdef ANDROID
Dmitry Shmidt41712582015-06-29 11:02:15 -07008559#ifndef ANDROID_LIB_STUB
Dmitry Shmidt738a26e2011-07-07 14:22:14 -07008560 .driver_cmd = wpa_driver_nl80211_driver_cmd,
Dmitry Shmidt41712582015-06-29 11:02:15 -07008561#endif /* !ANDROID_LIB_STUB */
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08008562#endif /* ANDROID */
Dmitry Shmidta38abf92014-03-06 13:38:44 -08008563 .vendor_cmd = nl80211_vendor_cmd,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008564 .set_qos_map = nl80211_set_qos_map,
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07008565 .set_wowlan = nl80211_set_wowlan,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008566 .roaming = nl80211_roaming,
8567 .set_mac_addr = nl80211_set_mac_addr,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008568#ifdef CONFIG_MESH
8569 .init_mesh = wpa_driver_nl80211_init_mesh,
8570 .join_mesh = wpa_driver_nl80211_join_mesh,
8571 .leave_mesh = wpa_driver_nl80211_leave_mesh,
8572#endif /* CONFIG_MESH */
8573 .br_add_ip_neigh = wpa_driver_br_add_ip_neigh,
8574 .br_delete_ip_neigh = wpa_driver_br_delete_ip_neigh,
8575 .br_port_set_attr = wpa_driver_br_port_set_attr,
8576 .br_set_net_param = wpa_driver_br_set_net_param,
8577 .add_tx_ts = nl80211_add_ts,
8578 .del_tx_ts = nl80211_del_ts,
8579 .do_acs = wpa_driver_do_acs,
Ravi Joshie6ccb162015-07-16 17:45:41 -07008580 .set_band = nl80211_set_band,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008581};