blob: 5fb6652a031ed8860ecb388598a76630f7d49c6f [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * Driver interaction with Linux nl80211/cfg80211
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003 * Copyright (c) 2002-2015, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 * Copyright (c) 2003-2004, Instant802 Networks, Inc.
5 * Copyright (c) 2005-2006, Devicescape Software, Inc.
6 * Copyright (c) 2007, Johannes Berg <johannes@sipsolutions.net>
7 * Copyright (c) 2009-2010, Atheros Communications
8 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08009 * This software may be distributed under the terms of the BSD license.
10 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011 */
12
13#include "includes.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014#include <sys/types.h>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070015#include <fcntl.h>
16#include <net/if.h>
17#include <netlink/genl/genl.h>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070018#include <netlink/genl/ctrl.h>
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070019#ifdef CONFIG_LIBNL3_ROUTE
20#include <netlink/route/neighbour.h>
21#endif /* CONFIG_LIBNL3_ROUTE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070022#include <linux/rtnetlink.h>
23#include <netpacket/packet.h>
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080024#include <linux/errqueue.h>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070025
26#include "common.h"
27#include "eloop.h"
Dmitry Shmidtcf32e602014-01-28 10:57:39 -080028#include "common/qca-vendor.h"
Dmitry Shmidt7832adb2014-04-29 10:53:02 -070029#include "common/qca-vendor-attr.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070030#include "common/ieee802_11_defs.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080031#include "common/ieee802_11_common.h"
32#include "l2_packet/l2_packet.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070033#include "netlink.h"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080034#include "linux_defines.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070035#include "linux_ioctl.h"
36#include "radiotap.h"
37#include "radiotap_iter.h"
38#include "rfkill.h"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080039#include "driver_nl80211.h"
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080040
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080041
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080042#ifndef CONFIG_LIBNL20
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070043/*
44 * libnl 1.1 has a bug, it tries to allocate socket numbers densely
45 * but when you free a socket again it will mess up its bitmap and
46 * and use the wrong number the next time it needs a socket ID.
47 * Therefore, we wrap the handle alloc/destroy and add our own pid
48 * accounting.
49 */
50static uint32_t port_bitmap[32] = { 0 };
51
52static struct nl_handle *nl80211_handle_alloc(void *cb)
53{
54 struct nl_handle *handle;
55 uint32_t pid = getpid() & 0x3FFFFF;
56 int i;
57
58 handle = nl_handle_alloc_cb(cb);
59
60 for (i = 0; i < 1024; i++) {
61 if (port_bitmap[i / 32] & (1 << (i % 32)))
62 continue;
63 port_bitmap[i / 32] |= 1 << (i % 32);
64 pid += i << 22;
65 break;
66 }
67
68 nl_socket_set_local_port(handle, pid);
69
70 return handle;
71}
72
73static void nl80211_handle_destroy(struct nl_handle *handle)
74{
75 uint32_t port = nl_socket_get_local_port(handle);
76
77 port >>= 22;
78 port_bitmap[port / 32] &= ~(1 << (port % 32));
79
80 nl_handle_destroy(handle);
81}
82#endif /* CONFIG_LIBNL20 */
83
84
Dmitry Shmidt54605472013-11-08 11:10:19 -080085#ifdef ANDROID
86/* system/core/libnl_2 does not include nl_socket_set_nonblocking() */
Dmitry Shmidt54605472013-11-08 11:10:19 -080087#undef nl_socket_set_nonblocking
88#define nl_socket_set_nonblocking(h) android_nl_socket_set_nonblocking(h)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080089
Dmitry Shmidt54605472013-11-08 11:10:19 -080090#endif /* ANDROID */
91
92
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080093static struct nl_handle * nl_create_handle(struct nl_cb *cb, const char *dbg)
94{
95 struct nl_handle *handle;
96
97 handle = nl80211_handle_alloc(cb);
98 if (handle == NULL) {
99 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
100 "callbacks (%s)", dbg);
101 return NULL;
102 }
103
104 if (genl_connect(handle)) {
105 wpa_printf(MSG_ERROR, "nl80211: Failed to connect to generic "
106 "netlink (%s)", dbg);
107 nl80211_handle_destroy(handle);
108 return NULL;
109 }
110
111 return handle;
112}
113
114
115static void nl_destroy_handles(struct nl_handle **handle)
116{
117 if (*handle == NULL)
118 return;
119 nl80211_handle_destroy(*handle);
120 *handle = NULL;
121}
122
123
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700124#if __WORDSIZE == 64
125#define ELOOP_SOCKET_INVALID (intptr_t) 0x8888888888888889ULL
126#else
127#define ELOOP_SOCKET_INVALID (intptr_t) 0x88888889ULL
128#endif
129
130static void nl80211_register_eloop_read(struct nl_handle **handle,
131 eloop_sock_handler handler,
132 void *eloop_data)
133{
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800134#ifdef CONFIG_LIBNL20
135 /*
136 * libnl uses a pretty small buffer (32 kB that gets converted to 64 kB)
137 * by default. It is possible to hit that limit in some cases where
138 * operations are blocked, e.g., with a burst of Deauthentication frames
139 * to hostapd and STA entry deletion. Try to increase the buffer to make
140 * this less likely to occur.
141 */
142 if (nl_socket_set_buffer_size(*handle, 262144, 0) < 0) {
143 wpa_printf(MSG_DEBUG,
144 "nl80211: Could not set nl_socket RX buffer size: %s",
145 strerror(errno));
146 /* continue anyway with the default (smaller) buffer */
147 }
148#endif /* CONFIG_LIBNL20 */
149
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700150 nl_socket_set_nonblocking(*handle);
151 eloop_register_read_sock(nl_socket_get_fd(*handle), handler,
152 eloop_data, *handle);
153 *handle = (void *) (((intptr_t) *handle) ^ ELOOP_SOCKET_INVALID);
154}
155
156
157static void nl80211_destroy_eloop_handle(struct nl_handle **handle)
158{
159 *handle = (void *) (((intptr_t) *handle) ^ ELOOP_SOCKET_INVALID);
160 eloop_unregister_read_sock(nl_socket_get_fd(*handle));
161 nl_destroy_handles(handle);
162}
163
164
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800165static void nl80211_global_deinit(void *priv);
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800166static void nl80211_check_global(struct nl80211_global *global);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800167
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800168static void wpa_driver_nl80211_deinit(struct i802_bss *bss);
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -0700169static int wpa_driver_nl80211_set_mode_ibss(struct i802_bss *bss,
170 struct hostapd_freq_params *freq);
Dmitry Shmidtd30ac602014-06-30 09:54:22 -0700171
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700172static int
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800173wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800174 const u8 *set_addr, int first,
175 const char *driver_params);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800176static int nl80211_send_frame_cmd(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700177 unsigned int freq, unsigned int wait,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800178 const u8 *buf, size_t buf_len, u64 *cookie,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800179 int no_cck, int no_ack, int offchanok,
180 const u16 *csa_offs, size_t csa_offs_len);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800181static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss,
182 int report);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700183
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800184#define IFIDX_ANY -1
185
186static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
187 int ifidx_reason);
188static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
189 int ifidx_reason);
190static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
191 int ifidx_reason);
Dmitry Shmidt738a26e2011-07-07 14:22:14 -0700192
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700193static int nl80211_set_channel(struct i802_bss *bss,
194 struct hostapd_freq_params *freq, int set_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700195static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
196 int ifindex, int disabled);
197
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800198static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv,
199 int reset_mode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800200
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700201static int i802_set_iface_flags(struct i802_bss *bss, int up);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800202static int nl80211_set_param(void *priv, const char *param);
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700203
204
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800205/* Converts nl80211_chan_width to a common format */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800206enum chan_width convert2width(int width)
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800207{
208 switch (width) {
209 case NL80211_CHAN_WIDTH_20_NOHT:
210 return CHAN_WIDTH_20_NOHT;
211 case NL80211_CHAN_WIDTH_20:
212 return CHAN_WIDTH_20;
213 case NL80211_CHAN_WIDTH_40:
214 return CHAN_WIDTH_40;
215 case NL80211_CHAN_WIDTH_80:
216 return CHAN_WIDTH_80;
217 case NL80211_CHAN_WIDTH_80P80:
218 return CHAN_WIDTH_80P80;
219 case NL80211_CHAN_WIDTH_160:
220 return CHAN_WIDTH_160;
221 }
222 return CHAN_WIDTH_UNKNOWN;
223}
224
225
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800226int is_ap_interface(enum nl80211_iftype nlmode)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800227{
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700228 return nlmode == NL80211_IFTYPE_AP ||
229 nlmode == NL80211_IFTYPE_P2P_GO;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800230}
231
232
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800233int is_sta_interface(enum nl80211_iftype nlmode)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800234{
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700235 return nlmode == NL80211_IFTYPE_STATION ||
236 nlmode == NL80211_IFTYPE_P2P_CLIENT;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800237}
238
239
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700240static int is_p2p_net_interface(enum nl80211_iftype nlmode)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800241{
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700242 return nlmode == NL80211_IFTYPE_P2P_CLIENT ||
243 nlmode == NL80211_IFTYPE_P2P_GO;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800244}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700245
246
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800247struct i802_bss * get_bss_ifindex(struct wpa_driver_nl80211_data *drv,
248 int ifindex)
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -0700249{
250 struct i802_bss *bss;
251
252 for (bss = drv->first_bss; bss; bss = bss->next) {
253 if (bss->ifindex == ifindex)
254 return bss;
255 }
256
257 return NULL;
258}
259
260
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800261static int is_mesh_interface(enum nl80211_iftype nlmode)
262{
263 return nlmode == NL80211_IFTYPE_MESH_POINT;
264}
265
266
267void nl80211_mark_disconnected(struct wpa_driver_nl80211_data *drv)
Dmitry Shmidt8bae4132013-06-06 11:25:10 -0700268{
269 if (drv->associated)
270 os_memcpy(drv->prev_bssid, drv->bssid, ETH_ALEN);
271 drv->associated = 0;
272 os_memset(drv->bssid, 0, ETH_ALEN);
273}
274
275
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700276/* nl80211 code */
277static int ack_handler(struct nl_msg *msg, void *arg)
278{
279 int *err = arg;
280 *err = 0;
281 return NL_STOP;
282}
283
284static int finish_handler(struct nl_msg *msg, void *arg)
285{
286 int *ret = arg;
287 *ret = 0;
288 return NL_SKIP;
289}
290
291static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
292 void *arg)
293{
294 int *ret = arg;
295 *ret = err->error;
296 return NL_SKIP;
297}
298
299
300static int no_seq_check(struct nl_msg *msg, void *arg)
301{
302 return NL_OK;
303}
304
305
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800306static void nl80211_nlmsg_clear(struct nl_msg *msg)
307{
308 /*
309 * Clear nlmsg data, e.g., to make sure key material is not left in
310 * heap memory for unnecessarily long time.
311 */
312 if (msg) {
313 struct nlmsghdr *hdr = nlmsg_hdr(msg);
314 void *data = nlmsg_data(hdr);
315 /*
316 * This would use nlmsg_datalen() or the older nlmsg_len() if
317 * only libnl were to maintain a stable API.. Neither will work
318 * with all released versions, so just calculate the length
319 * here.
320 */
321 int len = hdr->nlmsg_len - NLMSG_HDRLEN;
322
323 os_memset(data, 0, len);
324 }
325}
326
327
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800328static int send_and_recv(struct nl80211_global *global,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700329 struct nl_handle *nl_handle, struct nl_msg *msg,
330 int (*valid_handler)(struct nl_msg *, void *),
331 void *valid_data)
332{
333 struct nl_cb *cb;
334 int err = -ENOMEM;
335
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800336 if (!msg)
337 return -ENOMEM;
338
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800339 cb = nl_cb_clone(global->nl_cb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700340 if (!cb)
341 goto out;
342
343 err = nl_send_auto_complete(nl_handle, msg);
344 if (err < 0)
345 goto out;
346
347 err = 1;
348
349 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
350 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
351 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
352
353 if (valid_handler)
354 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM,
355 valid_handler, valid_data);
356
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700357 while (err > 0) {
358 int res = nl_recvmsgs(nl_handle, cb);
Dmitry Shmidt71757432014-06-02 13:50:35 -0700359 if (res < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700360 wpa_printf(MSG_INFO,
361 "nl80211: %s->nl_recvmsgs failed: %d",
362 __func__, res);
363 }
364 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700365 out:
366 nl_cb_put(cb);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800367 if (!valid_handler && valid_data == (void *) -1)
368 nl80211_nlmsg_clear(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700369 nlmsg_free(msg);
370 return err;
371}
372
373
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800374int send_and_recv_msgs(struct wpa_driver_nl80211_data *drv,
375 struct nl_msg *msg,
376 int (*valid_handler)(struct nl_msg *, void *),
377 void *valid_data)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700378{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800379 return send_and_recv(drv->global, drv->global->nl, msg,
380 valid_handler, valid_data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700381}
382
383
384struct family_data {
385 const char *group;
386 int id;
387};
388
389
390static int family_handler(struct nl_msg *msg, void *arg)
391{
392 struct family_data *res = arg;
393 struct nlattr *tb[CTRL_ATTR_MAX + 1];
394 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
395 struct nlattr *mcgrp;
396 int i;
397
398 nla_parse(tb, CTRL_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
399 genlmsg_attrlen(gnlh, 0), NULL);
400 if (!tb[CTRL_ATTR_MCAST_GROUPS])
401 return NL_SKIP;
402
403 nla_for_each_nested(mcgrp, tb[CTRL_ATTR_MCAST_GROUPS], i) {
404 struct nlattr *tb2[CTRL_ATTR_MCAST_GRP_MAX + 1];
405 nla_parse(tb2, CTRL_ATTR_MCAST_GRP_MAX, nla_data(mcgrp),
406 nla_len(mcgrp), NULL);
407 if (!tb2[CTRL_ATTR_MCAST_GRP_NAME] ||
408 !tb2[CTRL_ATTR_MCAST_GRP_ID] ||
409 os_strncmp(nla_data(tb2[CTRL_ATTR_MCAST_GRP_NAME]),
410 res->group,
411 nla_len(tb2[CTRL_ATTR_MCAST_GRP_NAME])) != 0)
412 continue;
413 res->id = nla_get_u32(tb2[CTRL_ATTR_MCAST_GRP_ID]);
414 break;
415 };
416
417 return NL_SKIP;
418}
419
420
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800421static int nl_get_multicast_id(struct nl80211_global *global,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700422 const char *family, const char *group)
423{
424 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800425 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700426 struct family_data res = { group, -ENOENT };
427
428 msg = nlmsg_alloc();
429 if (!msg)
430 return -ENOMEM;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800431 if (!genlmsg_put(msg, 0, 0, genl_ctrl_resolve(global->nl, "nlctrl"),
432 0, 0, CTRL_CMD_GETFAMILY, 0) ||
433 nla_put_string(msg, CTRL_ATTR_FAMILY_NAME, family)) {
434 nlmsg_free(msg);
435 return -1;
436 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700437
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800438 ret = send_and_recv(global, global->nl, msg, family_handler, &res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700439 if (ret == 0)
440 ret = res.id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700441 return ret;
442}
443
444
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800445void * nl80211_cmd(struct wpa_driver_nl80211_data *drv,
446 struct nl_msg *msg, int flags, uint8_t cmd)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800447{
448 return genlmsg_put(msg, 0, 0, drv->global->nl80211_id,
449 0, flags, cmd, 0);
450}
451
452
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800453static int nl80211_set_iface_id(struct nl_msg *msg, struct i802_bss *bss)
454{
455 if (bss->wdev_id_set)
456 return nla_put_u64(msg, NL80211_ATTR_WDEV, bss->wdev_id);
457 return nla_put_u32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
458}
459
460
461struct nl_msg * nl80211_cmd_msg(struct i802_bss *bss, int flags, uint8_t cmd)
462{
463 struct nl_msg *msg;
464
465 msg = nlmsg_alloc();
466 if (!msg)
467 return NULL;
468
469 if (!nl80211_cmd(bss->drv, msg, flags, cmd) ||
470 nl80211_set_iface_id(msg, bss) < 0) {
471 nlmsg_free(msg);
472 return NULL;
473 }
474
475 return msg;
476}
477
478
479static struct nl_msg *
480nl80211_ifindex_msg(struct wpa_driver_nl80211_data *drv, int ifindex,
481 int flags, uint8_t cmd)
482{
483 struct nl_msg *msg;
484
485 msg = nlmsg_alloc();
486 if (!msg)
487 return NULL;
488
489 if (!nl80211_cmd(drv, msg, flags, cmd) ||
490 nla_put_u32(msg, NL80211_ATTR_IFINDEX, ifindex)) {
491 nlmsg_free(msg);
492 return NULL;
493 }
494
495 return msg;
496}
497
498
499struct nl_msg * nl80211_drv_msg(struct wpa_driver_nl80211_data *drv, int flags,
500 uint8_t cmd)
501{
502 return nl80211_ifindex_msg(drv, drv->ifindex, flags, cmd);
503}
504
505
506struct nl_msg * nl80211_bss_msg(struct i802_bss *bss, int flags, uint8_t cmd)
507{
508 return nl80211_ifindex_msg(bss->drv, bss->ifindex, flags, cmd);
509}
510
511
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800512struct wiphy_idx_data {
513 int wiphy_idx;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700514 enum nl80211_iftype nlmode;
515 u8 *macaddr;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800516};
517
518
519static int netdev_info_handler(struct nl_msg *msg, void *arg)
520{
521 struct nlattr *tb[NL80211_ATTR_MAX + 1];
522 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
523 struct wiphy_idx_data *info = arg;
524
525 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
526 genlmsg_attrlen(gnlh, 0), NULL);
527
528 if (tb[NL80211_ATTR_WIPHY])
529 info->wiphy_idx = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
530
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700531 if (tb[NL80211_ATTR_IFTYPE])
532 info->nlmode = nla_get_u32(tb[NL80211_ATTR_IFTYPE]);
533
534 if (tb[NL80211_ATTR_MAC] && info->macaddr)
535 os_memcpy(info->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
536 ETH_ALEN);
537
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800538 return NL_SKIP;
539}
540
541
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800542int nl80211_get_wiphy_index(struct i802_bss *bss)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800543{
544 struct nl_msg *msg;
545 struct wiphy_idx_data data = {
546 .wiphy_idx = -1,
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700547 .macaddr = NULL,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800548 };
549
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800550 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_GET_INTERFACE)))
551 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800552
553 if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0)
554 return data.wiphy_idx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800555 return -1;
556}
557
558
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700559static enum nl80211_iftype nl80211_get_ifmode(struct i802_bss *bss)
560{
561 struct nl_msg *msg;
562 struct wiphy_idx_data data = {
563 .nlmode = NL80211_IFTYPE_UNSPECIFIED,
564 .macaddr = NULL,
565 };
566
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800567 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_GET_INTERFACE)))
568 return NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700569
570 if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0)
571 return data.nlmode;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700572 return NL80211_IFTYPE_UNSPECIFIED;
573}
574
575
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700576static int nl80211_get_macaddr(struct i802_bss *bss)
577{
578 struct nl_msg *msg;
579 struct wiphy_idx_data data = {
580 .macaddr = bss->addr,
581 };
582
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800583 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_GET_INTERFACE)))
584 return -1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700585
586 return send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data);
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700587}
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700588
589
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800590static int nl80211_register_beacons(struct wpa_driver_nl80211_data *drv,
591 struct nl80211_wiphy_data *w)
592{
593 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800594 int ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800595
596 msg = nlmsg_alloc();
597 if (!msg)
598 return -1;
599
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800600 if (!nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_BEACONS) ||
601 nla_put_u32(msg, NL80211_ATTR_WIPHY, w->wiphy_idx)) {
602 nlmsg_free(msg);
603 return -1;
604 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800605
606 ret = send_and_recv(drv->global, w->nl_beacons, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800607 if (ret) {
608 wpa_printf(MSG_DEBUG, "nl80211: Register beacons command "
609 "failed: ret=%d (%s)",
610 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800611 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800612 return ret;
613}
614
615
616static void nl80211_recv_beacons(int sock, void *eloop_ctx, void *handle)
617{
618 struct nl80211_wiphy_data *w = eloop_ctx;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700619 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800620
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800621 wpa_printf(MSG_EXCESSIVE, "nl80211: Beacon event message available");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800622
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700623 res = nl_recvmsgs(handle, w->nl_cb);
Dmitry Shmidt71757432014-06-02 13:50:35 -0700624 if (res < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700625 wpa_printf(MSG_INFO, "nl80211: %s->nl_recvmsgs failed: %d",
626 __func__, res);
627 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800628}
629
630
631static int process_beacon_event(struct nl_msg *msg, void *arg)
632{
633 struct nl80211_wiphy_data *w = arg;
634 struct wpa_driver_nl80211_data *drv;
635 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
636 struct nlattr *tb[NL80211_ATTR_MAX + 1];
637 union wpa_event_data event;
638
639 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
640 genlmsg_attrlen(gnlh, 0), NULL);
641
642 if (gnlh->cmd != NL80211_CMD_FRAME) {
643 wpa_printf(MSG_DEBUG, "nl80211: Unexpected beacon event? (%d)",
644 gnlh->cmd);
645 return NL_SKIP;
646 }
647
648 if (!tb[NL80211_ATTR_FRAME])
649 return NL_SKIP;
650
651 dl_list_for_each(drv, &w->drvs, struct wpa_driver_nl80211_data,
652 wiphy_list) {
653 os_memset(&event, 0, sizeof(event));
654 event.rx_mgmt.frame = nla_data(tb[NL80211_ATTR_FRAME]);
655 event.rx_mgmt.frame_len = nla_len(tb[NL80211_ATTR_FRAME]);
656 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
657 }
658
659 return NL_SKIP;
660}
661
662
663static struct nl80211_wiphy_data *
664nl80211_get_wiphy_data_ap(struct i802_bss *bss)
665{
666 static DEFINE_DL_LIST(nl80211_wiphys);
667 struct nl80211_wiphy_data *w;
668 int wiphy_idx, found = 0;
669 struct i802_bss *tmp_bss;
670
671 if (bss->wiphy_data != NULL)
672 return bss->wiphy_data;
673
674 wiphy_idx = nl80211_get_wiphy_index(bss);
675
676 dl_list_for_each(w, &nl80211_wiphys, struct nl80211_wiphy_data, list) {
677 if (w->wiphy_idx == wiphy_idx)
678 goto add;
679 }
680
681 /* alloc new one */
682 w = os_zalloc(sizeof(*w));
683 if (w == NULL)
684 return NULL;
685 w->wiphy_idx = wiphy_idx;
686 dl_list_init(&w->bsss);
687 dl_list_init(&w->drvs);
688
689 w->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
690 if (!w->nl_cb) {
691 os_free(w);
692 return NULL;
693 }
694 nl_cb_set(w->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL);
695 nl_cb_set(w->nl_cb, NL_CB_VALID, NL_CB_CUSTOM, process_beacon_event,
696 w);
697
698 w->nl_beacons = nl_create_handle(bss->drv->global->nl_cb,
699 "wiphy beacons");
700 if (w->nl_beacons == NULL) {
701 os_free(w);
702 return NULL;
703 }
704
705 if (nl80211_register_beacons(bss->drv, w)) {
706 nl_destroy_handles(&w->nl_beacons);
707 os_free(w);
708 return NULL;
709 }
710
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700711 nl80211_register_eloop_read(&w->nl_beacons, nl80211_recv_beacons, w);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800712
713 dl_list_add(&nl80211_wiphys, &w->list);
714
715add:
716 /* drv entry for this bss already there? */
717 dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
718 if (tmp_bss->drv == bss->drv) {
719 found = 1;
720 break;
721 }
722 }
723 /* if not add it */
724 if (!found)
725 dl_list_add(&w->drvs, &bss->drv->wiphy_list);
726
727 dl_list_add(&w->bsss, &bss->wiphy_list);
728 bss->wiphy_data = w;
729 return w;
730}
731
732
733static void nl80211_put_wiphy_data_ap(struct i802_bss *bss)
734{
735 struct nl80211_wiphy_data *w = bss->wiphy_data;
736 struct i802_bss *tmp_bss;
737 int found = 0;
738
739 if (w == NULL)
740 return;
741 bss->wiphy_data = NULL;
742 dl_list_del(&bss->wiphy_list);
743
744 /* still any for this drv present? */
745 dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
746 if (tmp_bss->drv == bss->drv) {
747 found = 1;
748 break;
749 }
750 }
751 /* if not remove it */
752 if (!found)
753 dl_list_del(&bss->drv->wiphy_list);
754
755 if (!dl_list_empty(&w->bsss))
756 return;
757
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700758 nl80211_destroy_eloop_handle(&w->nl_beacons);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800759
760 nl_cb_put(w->nl_cb);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800761 dl_list_del(&w->list);
762 os_free(w);
763}
764
765
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700766static int wpa_driver_nl80211_get_bssid(void *priv, u8 *bssid)
767{
768 struct i802_bss *bss = priv;
769 struct wpa_driver_nl80211_data *drv = bss->drv;
770 if (!drv->associated)
771 return -1;
772 os_memcpy(bssid, drv->bssid, ETH_ALEN);
773 return 0;
774}
775
776
777static int wpa_driver_nl80211_get_ssid(void *priv, u8 *ssid)
778{
779 struct i802_bss *bss = priv;
780 struct wpa_driver_nl80211_data *drv = bss->drv;
781 if (!drv->associated)
782 return -1;
783 os_memcpy(ssid, drv->ssid, drv->ssid_len);
784 return drv->ssid_len;
785}
786
787
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800788static void wpa_driver_nl80211_event_newlink(
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800789 struct wpa_driver_nl80211_data *drv, const char *ifname)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700790{
791 union wpa_event_data event;
792
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800793 if (os_strcmp(drv->first_bss->ifname, ifname) == 0) {
794 if (if_nametoindex(drv->first_bss->ifname) == 0) {
795 wpa_printf(MSG_DEBUG, "nl80211: Interface %s does not exist - ignore RTM_NEWLINK",
796 drv->first_bss->ifname);
797 return;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700798 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800799 if (!drv->if_removed)
800 return;
801 wpa_printf(MSG_DEBUG, "nl80211: Mark if_removed=0 for %s based on RTM_NEWLINK event",
802 drv->first_bss->ifname);
803 drv->if_removed = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700804 }
805
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800806 os_memset(&event, 0, sizeof(event));
807 os_strlcpy(event.interface_status.ifname, ifname,
808 sizeof(event.interface_status.ifname));
809 event.interface_status.ievent = EVENT_INTERFACE_ADDED;
810 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
811}
812
813
814static void wpa_driver_nl80211_event_dellink(
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800815 struct wpa_driver_nl80211_data *drv, const char *ifname)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800816{
817 union wpa_event_data event;
818
819 if (os_strcmp(drv->first_bss->ifname, ifname) == 0) {
820 if (drv->if_removed) {
821 wpa_printf(MSG_DEBUG, "nl80211: if_removed already set - ignore RTM_DELLINK event for %s",
822 ifname);
823 return;
824 }
825 wpa_printf(MSG_DEBUG, "RTM_DELLINK: Interface '%s' removed - mark if_removed=1",
826 ifname);
827 drv->if_removed = 1;
828 } else {
829 wpa_printf(MSG_DEBUG, "RTM_DELLINK: Interface '%s' removed",
830 ifname);
831 }
832
833 os_memset(&event, 0, sizeof(event));
834 os_strlcpy(event.interface_status.ifname, ifname,
835 sizeof(event.interface_status.ifname));
836 event.interface_status.ievent = EVENT_INTERFACE_REMOVED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700837 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
838}
839
840
841static int wpa_driver_nl80211_own_ifname(struct wpa_driver_nl80211_data *drv,
842 u8 *buf, size_t len)
843{
844 int attrlen, rta_len;
845 struct rtattr *attr;
846
847 attrlen = len;
848 attr = (struct rtattr *) buf;
849
850 rta_len = RTA_ALIGN(sizeof(struct rtattr));
851 while (RTA_OK(attr, attrlen)) {
852 if (attr->rta_type == IFLA_IFNAME) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800853 if (os_strcmp(((char *) attr) + rta_len,
854 drv->first_bss->ifname) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700855 return 1;
856 else
857 break;
858 }
859 attr = RTA_NEXT(attr, attrlen);
860 }
861
862 return 0;
863}
864
865
866static int wpa_driver_nl80211_own_ifindex(struct wpa_driver_nl80211_data *drv,
867 int ifindex, u8 *buf, size_t len)
868{
869 if (drv->ifindex == ifindex)
870 return 1;
871
872 if (drv->if_removed && wpa_driver_nl80211_own_ifname(drv, buf, len)) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800873 nl80211_check_global(drv->global);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700874 wpa_printf(MSG_DEBUG, "nl80211: Update ifindex for a removed "
875 "interface");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800876 wpa_driver_nl80211_finish_drv_init(drv, NULL, 0, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700877 return 1;
878 }
879
880 return 0;
881}
882
883
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800884static struct wpa_driver_nl80211_data *
885nl80211_find_drv(struct nl80211_global *global, int idx, u8 *buf, size_t len)
886{
887 struct wpa_driver_nl80211_data *drv;
888 dl_list_for_each(drv, &global->interfaces,
889 struct wpa_driver_nl80211_data, list) {
890 if (wpa_driver_nl80211_own_ifindex(drv, idx, buf, len) ||
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800891 have_ifidx(drv, idx, IFIDX_ANY))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800892 return drv;
893 }
894 return NULL;
895}
896
897
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700898static void wpa_driver_nl80211_event_rtm_newlink(void *ctx,
899 struct ifinfomsg *ifi,
900 u8 *buf, size_t len)
901{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800902 struct nl80211_global *global = ctx;
903 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800904 int attrlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700905 struct rtattr *attr;
906 u32 brid = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800907 char namebuf[IFNAMSIZ];
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800908 char ifname[IFNAMSIZ + 1];
909 char extra[100], *pos, *end;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700910
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800911 drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
912 if (!drv) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800913 wpa_printf(MSG_DEBUG, "nl80211: Ignore RTM_NEWLINK event for foreign ifindex %d",
914 ifi->ifi_index);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700915 return;
916 }
917
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800918 extra[0] = '\0';
919 pos = extra;
920 end = pos + sizeof(extra);
921 ifname[0] = '\0';
922
923 attrlen = len;
924 attr = (struct rtattr *) buf;
925 while (RTA_OK(attr, attrlen)) {
926 switch (attr->rta_type) {
927 case IFLA_IFNAME:
928 if (RTA_PAYLOAD(attr) >= IFNAMSIZ)
929 break;
930 os_memcpy(ifname, RTA_DATA(attr), RTA_PAYLOAD(attr));
931 ifname[RTA_PAYLOAD(attr)] = '\0';
932 break;
933 case IFLA_MASTER:
934 brid = nla_get_u32((struct nlattr *) attr);
935 pos += os_snprintf(pos, end - pos, " master=%u", brid);
936 break;
937 case IFLA_WIRELESS:
938 pos += os_snprintf(pos, end - pos, " wext");
939 break;
940 case IFLA_OPERSTATE:
941 pos += os_snprintf(pos, end - pos, " operstate=%u",
942 nla_get_u32((struct nlattr *) attr));
943 break;
944 case IFLA_LINKMODE:
945 pos += os_snprintf(pos, end - pos, " linkmode=%u",
946 nla_get_u32((struct nlattr *) attr));
947 break;
948 }
949 attr = RTA_NEXT(attr, attrlen);
950 }
951 extra[sizeof(extra) - 1] = '\0';
952
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700953 wpa_printf(MSG_DEBUG, "RTM_NEWLINK: ifi_index=%d ifname=%s%s ifi_family=%d ifi_flags=0x%x (%s%s%s%s)",
954 ifi->ifi_index, ifname, extra, ifi->ifi_family,
955 ifi->ifi_flags,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700956 (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
957 (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
958 (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
959 (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
960
961 if (!drv->if_disabled && !(ifi->ifi_flags & IFF_UP)) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800962 namebuf[0] = '\0';
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800963 if (if_indextoname(ifi->ifi_index, namebuf) &&
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800964 linux_iface_up(drv->global->ioctl_sock, namebuf) > 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800965 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
966 "event since interface %s is up", namebuf);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800967 drv->ignore_if_down_event = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800968 return;
969 }
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800970 wpa_printf(MSG_DEBUG, "nl80211: Interface down (%s/%s)",
971 namebuf, ifname);
972 if (os_strcmp(drv->first_bss->ifname, ifname) != 0) {
973 wpa_printf(MSG_DEBUG,
974 "nl80211: Not the main interface (%s) - do not indicate interface down",
975 drv->first_bss->ifname);
976 } else if (drv->ignore_if_down_event) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800977 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
978 "event generated by mode change");
979 drv->ignore_if_down_event = 0;
980 } else {
981 drv->if_disabled = 1;
982 wpa_supplicant_event(drv->ctx,
983 EVENT_INTERFACE_DISABLED, NULL);
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800984
985 /*
986 * Try to get drv again, since it may be removed as
987 * part of the EVENT_INTERFACE_DISABLED handling for
988 * dynamic interfaces
989 */
990 drv = nl80211_find_drv(global, ifi->ifi_index,
991 buf, len);
992 if (!drv)
993 return;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800994 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700995 }
996
997 if (drv->if_disabled && (ifi->ifi_flags & IFF_UP)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800998 if (if_indextoname(ifi->ifi_index, namebuf) &&
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800999 linux_iface_up(drv->global->ioctl_sock, namebuf) == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001000 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
1001 "event since interface %s is down",
1002 namebuf);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001003 } else if (if_nametoindex(drv->first_bss->ifname) == 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001004 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
1005 "event since interface %s does not exist",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001006 drv->first_bss->ifname);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001007 } else if (drv->if_removed) {
1008 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
1009 "event since interface %s is marked "
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001010 "removed", drv->first_bss->ifname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001011 } else {
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07001012 struct i802_bss *bss;
1013 u8 addr[ETH_ALEN];
1014
1015 /* Re-read MAC address as it may have changed */
1016 bss = get_bss_ifindex(drv, ifi->ifi_index);
1017 if (bss &&
1018 linux_get_ifhwaddr(drv->global->ioctl_sock,
1019 bss->ifname, addr) < 0) {
1020 wpa_printf(MSG_DEBUG,
1021 "nl80211: %s: failed to re-read MAC address",
1022 bss->ifname);
1023 } else if (bss &&
1024 os_memcmp(addr, bss->addr, ETH_ALEN) != 0) {
1025 wpa_printf(MSG_DEBUG,
1026 "nl80211: Own MAC address on ifindex %d (%s) changed from "
1027 MACSTR " to " MACSTR,
1028 ifi->ifi_index, bss->ifname,
1029 MAC2STR(bss->addr),
1030 MAC2STR(addr));
1031 os_memcpy(bss->addr, addr, ETH_ALEN);
1032 }
1033
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001034 wpa_printf(MSG_DEBUG, "nl80211: Interface up");
1035 drv->if_disabled = 0;
1036 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_ENABLED,
1037 NULL);
1038 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001039 }
1040
1041 /*
1042 * Some drivers send the association event before the operup event--in
1043 * this case, lifting operstate in wpa_driver_nl80211_set_operstate()
1044 * fails. This will hit us when wpa_supplicant does not need to do
1045 * IEEE 802.1X authentication
1046 */
1047 if (drv->operstate == 1 &&
1048 (ifi->ifi_flags & (IFF_LOWER_UP | IFF_DORMANT)) == IFF_LOWER_UP &&
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001049 !(ifi->ifi_flags & IFF_RUNNING)) {
1050 wpa_printf(MSG_DEBUG, "nl80211: Set IF_OPER_UP again based on ifi_flags and expected operstate");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001051 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001052 -1, IF_OPER_UP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001053 }
1054
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001055 if (ifname[0])
1056 wpa_driver_nl80211_event_newlink(drv, ifname);
1057
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001058 if (ifi->ifi_family == AF_BRIDGE && brid) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001059 struct i802_bss *bss;
1060
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001061 /* device has been added to bridge */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001062 if (!if_indextoname(brid, namebuf)) {
1063 wpa_printf(MSG_DEBUG,
1064 "nl80211: Could not find bridge ifname for ifindex %u",
1065 brid);
1066 return;
1067 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001068 wpa_printf(MSG_DEBUG, "nl80211: Add ifindex %u for bridge %s",
1069 brid, namebuf);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001070 add_ifidx(drv, brid, ifi->ifi_index);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001071
1072 for (bss = drv->first_bss; bss; bss = bss->next) {
1073 if (os_strcmp(ifname, bss->ifname) == 0) {
1074 os_strlcpy(bss->brname, namebuf, IFNAMSIZ);
1075 break;
1076 }
1077 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001078 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001079}
1080
1081
1082static void wpa_driver_nl80211_event_rtm_dellink(void *ctx,
1083 struct ifinfomsg *ifi,
1084 u8 *buf, size_t len)
1085{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001086 struct nl80211_global *global = ctx;
1087 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001088 int attrlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001089 struct rtattr *attr;
1090 u32 brid = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001091 char ifname[IFNAMSIZ + 1];
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001092 char extra[100], *pos, *end;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001093
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001094 drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
1095 if (!drv) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001096 wpa_printf(MSG_DEBUG, "nl80211: Ignore RTM_DELLINK event for foreign ifindex %d",
1097 ifi->ifi_index);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001098 return;
1099 }
1100
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001101 extra[0] = '\0';
1102 pos = extra;
1103 end = pos + sizeof(extra);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001104 ifname[0] = '\0';
1105
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001106 attrlen = len;
1107 attr = (struct rtattr *) buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001108 while (RTA_OK(attr, attrlen)) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001109 switch (attr->rta_type) {
1110 case IFLA_IFNAME:
1111 if (RTA_PAYLOAD(attr) >= IFNAMSIZ)
1112 break;
1113 os_memcpy(ifname, RTA_DATA(attr), RTA_PAYLOAD(attr));
1114 ifname[RTA_PAYLOAD(attr)] = '\0';
1115 break;
1116 case IFLA_MASTER:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001117 brid = nla_get_u32((struct nlattr *) attr);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001118 pos += os_snprintf(pos, end - pos, " master=%u", brid);
1119 break;
1120 case IFLA_OPERSTATE:
1121 pos += os_snprintf(pos, end - pos, " operstate=%u",
1122 nla_get_u32((struct nlattr *) attr));
1123 break;
1124 case IFLA_LINKMODE:
1125 pos += os_snprintf(pos, end - pos, " linkmode=%u",
1126 nla_get_u32((struct nlattr *) attr));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001127 break;
1128 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001129 attr = RTA_NEXT(attr, attrlen);
1130 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001131 extra[sizeof(extra) - 1] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001132
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001133 wpa_printf(MSG_DEBUG, "RTM_DELLINK: ifi_index=%d ifname=%s%s ifi_family=%d ifi_flags=0x%x (%s%s%s%s)",
1134 ifi->ifi_index, ifname, extra, ifi->ifi_family,
1135 ifi->ifi_flags,
1136 (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
1137 (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
1138 (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
1139 (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
1140
1141 if (ifname[0] && (ifi->ifi_family != AF_BRIDGE || !brid))
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001142 wpa_driver_nl80211_event_dellink(drv, ifname);
1143
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001144 if (ifi->ifi_family == AF_BRIDGE && brid) {
1145 /* device has been removed from bridge */
1146 char namebuf[IFNAMSIZ];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001147
1148 if (!if_indextoname(brid, namebuf)) {
1149 wpa_printf(MSG_DEBUG,
1150 "nl80211: Could not find bridge ifname for ifindex %u",
1151 brid);
1152 } else {
1153 wpa_printf(MSG_DEBUG,
1154 "nl80211: Remove ifindex %u for bridge %s",
1155 brid, namebuf);
1156 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001157 del_ifidx(drv, brid, ifi->ifi_index);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001158 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001159}
1160
1161
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001162unsigned int nl80211_get_assoc_freq(struct wpa_driver_nl80211_data *drv)
Jouni Malinen87fd2792011-05-16 18:35:42 +03001163{
1164 struct nl_msg *msg;
1165 int ret;
1166 struct nl80211_bss_info_arg arg;
1167
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001168 msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
Jouni Malinen87fd2792011-05-16 18:35:42 +03001169 os_memset(&arg, 0, sizeof(arg));
Jouni Malinen87fd2792011-05-16 18:35:42 +03001170 arg.drv = drv;
1171 ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
Jouni Malinen87fd2792011-05-16 18:35:42 +03001172 if (ret == 0) {
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07001173 unsigned int freq = drv->nlmode == NL80211_IFTYPE_ADHOC ?
1174 arg.ibss_freq : arg.assoc_freq;
Jouni Malinen87fd2792011-05-16 18:35:42 +03001175 wpa_printf(MSG_DEBUG, "nl80211: Operating frequency for the "
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07001176 "associated BSS from scan results: %u MHz", freq);
1177 if (freq)
1178 drv->assoc_freq = freq;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001179 return drv->assoc_freq;
Jouni Malinen87fd2792011-05-16 18:35:42 +03001180 }
1181 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
1182 "(%s)", ret, strerror(-ret));
Jouni Malinen87fd2792011-05-16 18:35:42 +03001183 return drv->assoc_freq;
1184}
1185
1186
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001187static int get_link_signal(struct nl_msg *msg, void *arg)
1188{
1189 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1190 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1191 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
1192 static struct nla_policy policy[NL80211_STA_INFO_MAX + 1] = {
1193 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001194 [NL80211_STA_INFO_SIGNAL_AVG] = { .type = NLA_U8 },
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07001195 [NL80211_STA_INFO_BEACON_SIGNAL_AVG] = { .type = NLA_U8 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001196 };
1197 struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
1198 static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
1199 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
1200 [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
1201 [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
1202 [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
1203 };
1204 struct wpa_signal_info *sig_change = arg;
1205
1206 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1207 genlmsg_attrlen(gnlh, 0), NULL);
1208 if (!tb[NL80211_ATTR_STA_INFO] ||
1209 nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
1210 tb[NL80211_ATTR_STA_INFO], policy))
1211 return NL_SKIP;
1212 if (!sinfo[NL80211_STA_INFO_SIGNAL])
1213 return NL_SKIP;
1214
1215 sig_change->current_signal =
1216 (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
1217
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001218 if (sinfo[NL80211_STA_INFO_SIGNAL_AVG])
1219 sig_change->avg_signal =
1220 (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL_AVG]);
1221 else
1222 sig_change->avg_signal = 0;
1223
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07001224 if (sinfo[NL80211_STA_INFO_BEACON_SIGNAL_AVG])
1225 sig_change->avg_beacon_signal =
1226 (s8)
1227 nla_get_u8(sinfo[NL80211_STA_INFO_BEACON_SIGNAL_AVG]);
1228 else
1229 sig_change->avg_beacon_signal = 0;
1230
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001231 if (sinfo[NL80211_STA_INFO_TX_BITRATE]) {
1232 if (nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
1233 sinfo[NL80211_STA_INFO_TX_BITRATE],
1234 rate_policy)) {
1235 sig_change->current_txrate = 0;
1236 } else {
1237 if (rinfo[NL80211_RATE_INFO_BITRATE]) {
1238 sig_change->current_txrate =
1239 nla_get_u16(rinfo[
1240 NL80211_RATE_INFO_BITRATE]) * 100;
1241 }
1242 }
1243 }
1244
1245 return NL_SKIP;
1246}
1247
1248
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001249int nl80211_get_link_signal(struct wpa_driver_nl80211_data *drv,
1250 struct wpa_signal_info *sig)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001251{
1252 struct nl_msg *msg;
1253
1254 sig->current_signal = -9999;
1255 sig->current_txrate = 0;
1256
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001257 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_GET_STATION)) ||
1258 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid)) {
1259 nlmsg_free(msg);
1260 return -ENOBUFS;
1261 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001262
1263 return send_and_recv_msgs(drv, msg, get_link_signal, sig);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001264}
1265
1266
1267static int get_link_noise(struct nl_msg *msg, void *arg)
1268{
1269 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1270 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1271 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
1272 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
1273 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
1274 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
1275 };
1276 struct wpa_signal_info *sig_change = arg;
1277
1278 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1279 genlmsg_attrlen(gnlh, 0), NULL);
1280
1281 if (!tb[NL80211_ATTR_SURVEY_INFO]) {
1282 wpa_printf(MSG_DEBUG, "nl80211: survey data missing!");
1283 return NL_SKIP;
1284 }
1285
1286 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
1287 tb[NL80211_ATTR_SURVEY_INFO],
1288 survey_policy)) {
1289 wpa_printf(MSG_DEBUG, "nl80211: failed to parse nested "
1290 "attributes!");
1291 return NL_SKIP;
1292 }
1293
1294 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
1295 return NL_SKIP;
1296
1297 if (nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
1298 sig_change->frequency)
1299 return NL_SKIP;
1300
1301 if (!sinfo[NL80211_SURVEY_INFO_NOISE])
1302 return NL_SKIP;
1303
1304 sig_change->current_noise =
1305 (s8) nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
1306
1307 return NL_SKIP;
1308}
1309
1310
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001311int nl80211_get_link_noise(struct wpa_driver_nl80211_data *drv,
1312 struct wpa_signal_info *sig_change)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001313{
1314 struct nl_msg *msg;
1315
1316 sig_change->current_noise = 9999;
1317 sig_change->frequency = drv->assoc_freq;
1318
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001319 msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001320 return send_and_recv_msgs(drv, msg, get_link_noise, sig_change);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001321}
1322
1323
1324static void wpa_driver_nl80211_event_receive(int sock, void *eloop_ctx,
1325 void *handle)
1326{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001327 struct nl_cb *cb = eloop_ctx;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001328 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001329
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001330 wpa_printf(MSG_MSGDUMP, "nl80211: Event message available");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001331
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001332 res = nl_recvmsgs(handle, cb);
Dmitry Shmidt71757432014-06-02 13:50:35 -07001333 if (res < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001334 wpa_printf(MSG_INFO, "nl80211: %s->nl_recvmsgs failed: %d",
1335 __func__, res);
1336 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001337}
1338
1339
1340/**
1341 * wpa_driver_nl80211_set_country - ask nl80211 to set the regulatory domain
1342 * @priv: driver_nl80211 private data
1343 * @alpha2_arg: country to which to switch to
1344 * Returns: 0 on success, -1 on failure
1345 *
1346 * This asks nl80211 to set the regulatory domain for given
1347 * country ISO / IEC alpha2.
1348 */
1349static int wpa_driver_nl80211_set_country(void *priv, const char *alpha2_arg)
1350{
1351 struct i802_bss *bss = priv;
1352 struct wpa_driver_nl80211_data *drv = bss->drv;
1353 char alpha2[3];
1354 struct nl_msg *msg;
1355
1356 msg = nlmsg_alloc();
1357 if (!msg)
1358 return -ENOMEM;
1359
1360 alpha2[0] = alpha2_arg[0];
1361 alpha2[1] = alpha2_arg[1];
1362 alpha2[2] = '\0';
1363
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001364 if (!nl80211_cmd(drv, msg, 0, NL80211_CMD_REQ_SET_REG) ||
1365 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, alpha2)) {
1366 nlmsg_free(msg);
1367 return -EINVAL;
1368 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001369 if (send_and_recv_msgs(drv, msg, NULL, NULL))
1370 return -EINVAL;
1371 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001372}
1373
1374
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001375static int nl80211_get_country(struct nl_msg *msg, void *arg)
1376{
1377 char *alpha2 = arg;
1378 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
1379 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1380
1381 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1382 genlmsg_attrlen(gnlh, 0), NULL);
1383 if (!tb_msg[NL80211_ATTR_REG_ALPHA2]) {
1384 wpa_printf(MSG_DEBUG, "nl80211: No country information available");
1385 return NL_SKIP;
1386 }
1387 os_strlcpy(alpha2, nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]), 3);
1388 return NL_SKIP;
1389}
1390
1391
1392static int wpa_driver_nl80211_get_country(void *priv, char *alpha2)
1393{
1394 struct i802_bss *bss = priv;
1395 struct wpa_driver_nl80211_data *drv = bss->drv;
1396 struct nl_msg *msg;
1397 int ret;
1398
1399 msg = nlmsg_alloc();
1400 if (!msg)
1401 return -ENOMEM;
1402
1403 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG);
1404 alpha2[0] = '\0';
1405 ret = send_and_recv_msgs(drv, msg, nl80211_get_country, alpha2);
1406 if (!alpha2[0])
1407 ret = -1;
1408
1409 return ret;
1410}
1411
1412
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001413static int wpa_driver_nl80211_init_nl_global(struct nl80211_global *global)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001414{
1415 int ret;
1416
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001417 global->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
1418 if (global->nl_cb == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001419 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
1420 "callbacks");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001421 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001422 }
1423
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001424 global->nl = nl_create_handle(global->nl_cb, "nl");
1425 if (global->nl == NULL)
1426 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001427
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001428 global->nl80211_id = genl_ctrl_resolve(global->nl, "nl80211");
1429 if (global->nl80211_id < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001430 wpa_printf(MSG_ERROR, "nl80211: 'nl80211' generic netlink not "
1431 "found");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001432 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001433 }
1434
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001435 global->nl_event = nl_create_handle(global->nl_cb, "event");
1436 if (global->nl_event == NULL)
1437 goto err;
1438
1439 ret = nl_get_multicast_id(global, "nl80211", "scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001440 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001441 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001442 if (ret < 0) {
1443 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
1444 "membership for scan events: %d (%s)",
1445 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001446 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001447 }
1448
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001449 ret = nl_get_multicast_id(global, "nl80211", "mlme");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001450 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001451 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001452 if (ret < 0) {
1453 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
1454 "membership for mlme events: %d (%s)",
1455 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001456 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001457 }
1458
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001459 ret = nl_get_multicast_id(global, "nl80211", "regulatory");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001460 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001461 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001462 if (ret < 0) {
1463 wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
1464 "membership for regulatory events: %d (%s)",
1465 ret, strerror(-ret));
1466 /* Continue without regulatory events */
1467 }
1468
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001469 ret = nl_get_multicast_id(global, "nl80211", "vendor");
1470 if (ret >= 0)
1471 ret = nl_socket_add_membership(global->nl_event, ret);
1472 if (ret < 0) {
1473 wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
1474 "membership for vendor events: %d (%s)",
1475 ret, strerror(-ret));
1476 /* Continue without vendor events */
1477 }
1478
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001479 nl_cb_set(global->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
1480 no_seq_check, NULL);
1481 nl_cb_set(global->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
1482 process_global_event, global);
1483
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001484 nl80211_register_eloop_read(&global->nl_event,
1485 wpa_driver_nl80211_event_receive,
1486 global->nl_cb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001487
1488 return 0;
1489
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001490err:
1491 nl_destroy_handles(&global->nl_event);
1492 nl_destroy_handles(&global->nl);
1493 nl_cb_put(global->nl_cb);
1494 global->nl_cb = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001495 return -1;
1496}
1497
1498
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001499static void nl80211_check_global(struct nl80211_global *global)
1500{
1501 struct nl_handle *handle;
1502 const char *groups[] = { "scan", "mlme", "regulatory", "vendor", NULL };
1503 int ret;
1504 unsigned int i;
1505
1506 /*
1507 * Try to re-add memberships to handle case of cfg80211 getting reloaded
1508 * and all registration having been cleared.
1509 */
1510 handle = (void *) (((intptr_t) global->nl_event) ^
1511 ELOOP_SOCKET_INVALID);
1512
1513 for (i = 0; groups[i]; i++) {
1514 ret = nl_get_multicast_id(global, "nl80211", groups[i]);
1515 if (ret >= 0)
1516 ret = nl_socket_add_membership(handle, ret);
1517 if (ret < 0) {
1518 wpa_printf(MSG_INFO,
1519 "nl80211: Could not re-add multicast membership for %s events: %d (%s)",
1520 groups[i], ret, strerror(-ret));
1521 }
1522 }
1523}
1524
1525
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001526static void wpa_driver_nl80211_rfkill_blocked(void *ctx)
1527{
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001528 struct wpa_driver_nl80211_data *drv = ctx;
1529
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001530 wpa_printf(MSG_DEBUG, "nl80211: RFKILL blocked");
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001531
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001532 /*
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001533 * rtnetlink ifdown handler will report interfaces other than the P2P
1534 * Device interface as disabled.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001535 */
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001536 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
1537 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_DISABLED, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001538}
1539
1540
1541static void wpa_driver_nl80211_rfkill_unblocked(void *ctx)
1542{
1543 struct wpa_driver_nl80211_data *drv = ctx;
1544 wpa_printf(MSG_DEBUG, "nl80211: RFKILL unblocked");
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001545 if (i802_set_iface_flags(drv->first_bss, 1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001546 wpa_printf(MSG_DEBUG, "nl80211: Could not set interface UP "
1547 "after rfkill unblock");
1548 return;
1549 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001550
1551 if (is_p2p_net_interface(drv->nlmode))
1552 nl80211_disable_11b_rates(drv, drv->ifindex, 1);
1553
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001554 /*
1555 * rtnetlink ifup handler will report interfaces other than the P2P
1556 * Device interface as enabled.
1557 */
1558 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
1559 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_ENABLED, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001560}
1561
1562
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001563static void wpa_driver_nl80211_handle_eapol_tx_status(int sock,
1564 void *eloop_ctx,
1565 void *handle)
1566{
1567 struct wpa_driver_nl80211_data *drv = eloop_ctx;
1568 u8 data[2048];
1569 struct msghdr msg;
1570 struct iovec entry;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001571 u8 control[512];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001572 struct cmsghdr *cmsg;
1573 int res, found_ee = 0, found_wifi = 0, acked = 0;
1574 union wpa_event_data event;
1575
1576 memset(&msg, 0, sizeof(msg));
1577 msg.msg_iov = &entry;
1578 msg.msg_iovlen = 1;
1579 entry.iov_base = data;
1580 entry.iov_len = sizeof(data);
1581 msg.msg_control = &control;
1582 msg.msg_controllen = sizeof(control);
1583
1584 res = recvmsg(sock, &msg, MSG_ERRQUEUE);
1585 /* if error or not fitting 802.3 header, return */
1586 if (res < 14)
1587 return;
1588
1589 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg))
1590 {
1591 if (cmsg->cmsg_level == SOL_SOCKET &&
1592 cmsg->cmsg_type == SCM_WIFI_STATUS) {
1593 int *ack;
1594
1595 found_wifi = 1;
1596 ack = (void *)CMSG_DATA(cmsg);
1597 acked = *ack;
1598 }
1599
1600 if (cmsg->cmsg_level == SOL_PACKET &&
1601 cmsg->cmsg_type == PACKET_TX_TIMESTAMP) {
1602 struct sock_extended_err *err =
1603 (struct sock_extended_err *)CMSG_DATA(cmsg);
1604
1605 if (err->ee_origin == SO_EE_ORIGIN_TXSTATUS)
1606 found_ee = 1;
1607 }
1608 }
1609
1610 if (!found_ee || !found_wifi)
1611 return;
1612
1613 memset(&event, 0, sizeof(event));
1614 event.eapol_tx_status.dst = data;
1615 event.eapol_tx_status.data = data + 14;
1616 event.eapol_tx_status.data_len = res - 14;
1617 event.eapol_tx_status.ack = acked;
1618 wpa_supplicant_event(drv->ctx, EVENT_EAPOL_TX_STATUS, &event);
1619}
1620
1621
1622static int nl80211_init_bss(struct i802_bss *bss)
1623{
1624 bss->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
1625 if (!bss->nl_cb)
1626 return -1;
1627
1628 nl_cb_set(bss->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
1629 no_seq_check, NULL);
1630 nl_cb_set(bss->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
1631 process_bss_event, bss);
1632
1633 return 0;
1634}
1635
1636
1637static void nl80211_destroy_bss(struct i802_bss *bss)
1638{
1639 nl_cb_put(bss->nl_cb);
1640 bss->nl_cb = NULL;
1641}
1642
1643
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001644static void
1645wpa_driver_nl80211_drv_init_rfkill(struct wpa_driver_nl80211_data *drv)
1646{
1647 struct rfkill_config *rcfg;
1648
1649 if (drv->rfkill)
1650 return;
1651
1652 rcfg = os_zalloc(sizeof(*rcfg));
1653 if (!rcfg)
1654 return;
1655
1656 rcfg->ctx = drv;
1657
1658 /* rfkill uses netdev sysfs for initialization. However, P2P Device is
1659 * not associated with a netdev, so use the name of some other interface
1660 * sharing the same wiphy as the P2P Device interface.
1661 *
1662 * Note: This is valid, as a P2P Device interface is always dynamically
1663 * created and is created only once another wpa_s interface was added.
1664 */
1665 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE) {
1666 struct nl80211_global *global = drv->global;
1667 struct wpa_driver_nl80211_data *tmp1;
1668
1669 dl_list_for_each(tmp1, &global->interfaces,
1670 struct wpa_driver_nl80211_data, list) {
1671 if (drv == tmp1 || drv->wiphy_idx != tmp1->wiphy_idx ||
1672 !tmp1->rfkill)
1673 continue;
1674
1675 wpa_printf(MSG_DEBUG,
1676 "nl80211: Use (%s) to initialize P2P Device rfkill",
1677 tmp1->first_bss->ifname);
1678 os_strlcpy(rcfg->ifname, tmp1->first_bss->ifname,
1679 sizeof(rcfg->ifname));
1680 break;
1681 }
1682 } else {
1683 os_strlcpy(rcfg->ifname, drv->first_bss->ifname,
1684 sizeof(rcfg->ifname));
1685 }
1686
1687 rcfg->blocked_cb = wpa_driver_nl80211_rfkill_blocked;
1688 rcfg->unblocked_cb = wpa_driver_nl80211_rfkill_unblocked;
1689 drv->rfkill = rfkill_init(rcfg);
1690 if (!drv->rfkill) {
1691 wpa_printf(MSG_DEBUG, "nl80211: RFKILL status not available");
1692 os_free(rcfg);
1693 }
1694}
1695
1696
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001697static void * wpa_driver_nl80211_drv_init(void *ctx, const char *ifname,
1698 void *global_priv, int hostapd,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001699 const u8 *set_addr,
1700 const char *driver_params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001701{
1702 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001703 struct i802_bss *bss;
1704
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001705 if (global_priv == NULL)
1706 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001707 drv = os_zalloc(sizeof(*drv));
1708 if (drv == NULL)
1709 return NULL;
1710 drv->global = global_priv;
1711 drv->ctx = ctx;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001712 drv->hostapd = !!hostapd;
1713 drv->eapol_sock = -1;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001714
1715 /*
1716 * There is no driver capability flag for this, so assume it is
1717 * supported and disable this on first attempt to use if the driver
1718 * rejects the command due to missing support.
1719 */
1720 drv->set_rekey_offload = 1;
1721
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001722 drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int);
1723 drv->if_indices = drv->default_if_indices;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001724 drv->if_indices_reason = drv->default_if_indices_reason;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001725
1726 drv->first_bss = os_zalloc(sizeof(*drv->first_bss));
1727 if (!drv->first_bss) {
1728 os_free(drv);
1729 return NULL;
1730 }
1731 bss = drv->first_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001732 bss->drv = drv;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001733 bss->ctx = ctx;
1734
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001735 os_strlcpy(bss->ifname, ifname, sizeof(bss->ifname));
1736 drv->monitor_ifidx = -1;
1737 drv->monitor_sock = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001738 drv->eapol_tx_sock = -1;
1739 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001740
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001741 if (nl80211_init_bss(bss))
1742 goto failed;
1743
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001744 if (wpa_driver_nl80211_finish_drv_init(drv, set_addr, 1, driver_params))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001745 goto failed;
1746
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001747 drv->eapol_tx_sock = socket(PF_PACKET, SOCK_DGRAM, 0);
1748 if (drv->eapol_tx_sock < 0)
1749 goto failed;
1750
1751 if (drv->data_tx_status) {
1752 int enabled = 1;
1753
1754 if (setsockopt(drv->eapol_tx_sock, SOL_SOCKET, SO_WIFI_STATUS,
1755 &enabled, sizeof(enabled)) < 0) {
1756 wpa_printf(MSG_DEBUG,
1757 "nl80211: wifi status sockopt failed\n");
1758 drv->data_tx_status = 0;
1759 if (!drv->use_monitor)
1760 drv->capa.flags &=
1761 ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
1762 } else {
1763 eloop_register_read_sock(drv->eapol_tx_sock,
1764 wpa_driver_nl80211_handle_eapol_tx_status,
1765 drv, NULL);
1766 }
1767 }
1768
1769 if (drv->global) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001770 nl80211_check_global(drv->global);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001771 dl_list_add(&drv->global->interfaces, &drv->list);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001772 drv->in_interface_list = 1;
1773 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001774
1775 return bss;
1776
1777failed:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001778 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001779 return NULL;
1780}
1781
1782
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001783/**
1784 * wpa_driver_nl80211_init - Initialize nl80211 driver interface
1785 * @ctx: context to be used when calling wpa_supplicant functions,
1786 * e.g., wpa_supplicant_event()
1787 * @ifname: interface name, e.g., wlan0
1788 * @global_priv: private driver global data from global_init()
1789 * Returns: Pointer to private data, %NULL on failure
1790 */
1791static void * wpa_driver_nl80211_init(void *ctx, const char *ifname,
1792 void *global_priv)
1793{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001794 return wpa_driver_nl80211_drv_init(ctx, ifname, global_priv, 0, NULL,
1795 NULL);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001796}
1797
1798
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001799static int nl80211_register_frame(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001800 struct nl_handle *nl_handle,
1801 u16 type, const u8 *match, size_t match_len)
1802{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001803 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001804 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001805 int ret;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001806 char buf[30];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001807
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001808 buf[0] = '\0';
1809 wpa_snprintf_hex(buf, sizeof(buf), match, match_len);
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07001810 wpa_printf(MSG_DEBUG, "nl80211: Register frame type=0x%x (%s) nl_handle=%p match=%s",
1811 type, fc2str(type), nl_handle, buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001812
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001813 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_REGISTER_ACTION)) ||
1814 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE, type) ||
1815 nla_put(msg, NL80211_ATTR_FRAME_MATCH, match_len, match)) {
1816 nlmsg_free(msg);
1817 return -1;
1818 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001819
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001820 ret = send_and_recv(drv->global, nl_handle, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001821 if (ret) {
1822 wpa_printf(MSG_DEBUG, "nl80211: Register frame command "
1823 "failed (type=%u): ret=%d (%s)",
1824 type, ret, strerror(-ret));
1825 wpa_hexdump(MSG_DEBUG, "nl80211: Register frame match",
1826 match, match_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001827 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001828 return ret;
1829}
1830
1831
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001832static int nl80211_alloc_mgmt_handle(struct i802_bss *bss)
1833{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001834 if (bss->nl_mgmt) {
1835 wpa_printf(MSG_DEBUG, "nl80211: Mgmt reporting "
1836 "already on! (nl_mgmt=%p)", bss->nl_mgmt);
1837 return -1;
1838 }
1839
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001840 bss->nl_mgmt = nl_create_handle(bss->nl_cb, "mgmt");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001841 if (bss->nl_mgmt == NULL)
1842 return -1;
1843
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001844 return 0;
1845}
1846
1847
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001848static void nl80211_mgmt_handle_register_eloop(struct i802_bss *bss)
1849{
1850 nl80211_register_eloop_read(&bss->nl_mgmt,
1851 wpa_driver_nl80211_event_receive,
1852 bss->nl_cb);
1853}
1854
1855
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001856static int nl80211_register_action_frame(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001857 const u8 *match, size_t match_len)
1858{
1859 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_ACTION << 4);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001860 return nl80211_register_frame(bss, bss->nl_mgmt,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001861 type, match, match_len);
1862}
1863
1864
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001865static int nl80211_mgmt_subscribe_non_ap(struct i802_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001866{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001867 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001868 int ret = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001869
1870 if (nl80211_alloc_mgmt_handle(bss))
1871 return -1;
1872 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with non-AP "
1873 "handle %p", bss->nl_mgmt);
1874
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001875 if (drv->nlmode == NL80211_IFTYPE_ADHOC) {
1876 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_AUTH << 4);
1877
1878 /* register for any AUTH message */
1879 nl80211_register_frame(bss, bss->nl_mgmt, type, NULL, 0);
1880 }
1881
Dmitry Shmidt051af732013-10-22 13:52:46 -07001882#ifdef CONFIG_INTERWORKING
1883 /* QoS Map Configure */
1884 if (nl80211_register_action_frame(bss, (u8 *) "\x01\x04", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001885 ret = -1;
Dmitry Shmidt051af732013-10-22 13:52:46 -07001886#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001887#if defined(CONFIG_P2P) || defined(CONFIG_INTERWORKING)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001888 /* GAS Initial Request */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001889 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0a", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001890 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001891 /* GAS Initial Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001892 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0b", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001893 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001894 /* GAS Comeback Request */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001895 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0c", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001896 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001897 /* GAS Comeback Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001898 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0d", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001899 ret = -1;
Dmitry Shmidt18463232014-01-24 12:29:41 -08001900 /* Protected GAS Initial Request */
1901 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0a", 2) < 0)
1902 ret = -1;
1903 /* Protected GAS Initial Response */
1904 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0b", 2) < 0)
1905 ret = -1;
1906 /* Protected GAS Comeback Request */
1907 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0c", 2) < 0)
1908 ret = -1;
1909 /* Protected GAS Comeback Response */
1910 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0d", 2) < 0)
1911 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001912#endif /* CONFIG_P2P || CONFIG_INTERWORKING */
1913#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001914 /* P2P Public Action */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001915 if (nl80211_register_action_frame(bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001916 (u8 *) "\x04\x09\x50\x6f\x9a\x09",
1917 6) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001918 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001919 /* P2P Action */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001920 if (nl80211_register_action_frame(bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001921 (u8 *) "\x7f\x50\x6f\x9a\x09",
1922 5) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001923 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001924#endif /* CONFIG_P2P */
1925#ifdef CONFIG_IEEE80211W
1926 /* SA Query Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001927 if (nl80211_register_action_frame(bss, (u8 *) "\x08\x01", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001928 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001929#endif /* CONFIG_IEEE80211W */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001930#ifdef CONFIG_TDLS
1931 if ((drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT)) {
1932 /* TDLS Discovery Response */
1933 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0e", 2) <
1934 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001935 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001936 }
1937#endif /* CONFIG_TDLS */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001938#ifdef CONFIG_FST
1939 /* FST Action frames */
1940 if (nl80211_register_action_frame(bss, (u8 *) "\x12", 1) < 0)
1941 ret = -1;
1942#endif /* CONFIG_FST */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001943
1944 /* FT Action frames */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001945 if (nl80211_register_action_frame(bss, (u8 *) "\x06", 1) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001946 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001947 else
1948 drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_FT |
1949 WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK;
1950
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001951 /* WNM - BSS Transition Management Request */
1952 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x07", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001953 ret = -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001954 /* WNM-Sleep Mode Response */
1955 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x11", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001956 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001957
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001958#ifdef CONFIG_HS20
1959 /* WNM-Notification */
1960 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x1a", 2) < 0)
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001961 ret = -1;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001962#endif /* CONFIG_HS20 */
1963
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001964 /* WMM-AC ADDTS Response */
1965 if (nl80211_register_action_frame(bss, (u8 *) "\x11\x01", 2) < 0)
1966 ret = -1;
1967
1968 /* WMM-AC DELTS */
1969 if (nl80211_register_action_frame(bss, (u8 *) "\x11\x02", 2) < 0)
1970 ret = -1;
1971
1972 /* Radio Measurement - Neighbor Report Response */
1973 if (nl80211_register_action_frame(bss, (u8 *) "\x05\x05", 2) < 0)
1974 ret = -1;
1975
1976 /* Radio Measurement - Link Measurement Request */
1977 if ((drv->capa.rrm_flags & WPA_DRIVER_FLAGS_TX_POWER_INSERTION) &&
1978 (nl80211_register_action_frame(bss, (u8 *) "\x05\x02", 2) < 0))
1979 ret = -1;
1980
1981 nl80211_mgmt_handle_register_eloop(bss);
1982
1983 return ret;
1984}
1985
1986
1987static int nl80211_mgmt_subscribe_mesh(struct i802_bss *bss)
1988{
1989 int ret = 0;
1990
1991 if (nl80211_alloc_mgmt_handle(bss))
1992 return -1;
1993
1994 wpa_printf(MSG_DEBUG,
1995 "nl80211: Subscribe to mgmt frames with mesh handle %p",
1996 bss->nl_mgmt);
1997
1998 /* Auth frames for mesh SAE */
1999 if (nl80211_register_frame(bss, bss->nl_mgmt,
2000 (WLAN_FC_TYPE_MGMT << 2) |
2001 (WLAN_FC_STYPE_AUTH << 4),
2002 NULL, 0) < 0)
2003 ret = -1;
2004
2005 /* Mesh peering open */
2006 if (nl80211_register_action_frame(bss, (u8 *) "\x0f\x01", 2) < 0)
2007 ret = -1;
2008 /* Mesh peering confirm */
2009 if (nl80211_register_action_frame(bss, (u8 *) "\x0f\x02", 2) < 0)
2010 ret = -1;
2011 /* Mesh peering close */
2012 if (nl80211_register_action_frame(bss, (u8 *) "\x0f\x03", 2) < 0)
2013 ret = -1;
2014
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002015 nl80211_mgmt_handle_register_eloop(bss);
2016
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002017 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002018}
2019
2020
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002021static int nl80211_register_spurious_class3(struct i802_bss *bss)
2022{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002023 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002024 int ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002025
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002026 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_UNEXPECTED_FRAME);
2027 ret = send_and_recv(bss->drv->global, bss->nl_mgmt, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002028 if (ret) {
2029 wpa_printf(MSG_DEBUG, "nl80211: Register spurious class3 "
2030 "failed: ret=%d (%s)",
2031 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002032 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002033 return ret;
2034}
2035
2036
2037static int nl80211_mgmt_subscribe_ap(struct i802_bss *bss)
2038{
2039 static const int stypes[] = {
2040 WLAN_FC_STYPE_AUTH,
2041 WLAN_FC_STYPE_ASSOC_REQ,
2042 WLAN_FC_STYPE_REASSOC_REQ,
2043 WLAN_FC_STYPE_DISASSOC,
2044 WLAN_FC_STYPE_DEAUTH,
2045 WLAN_FC_STYPE_ACTION,
2046 WLAN_FC_STYPE_PROBE_REQ,
2047/* Beacon doesn't work as mac80211 doesn't currently allow
2048 * it, but it wouldn't really be the right thing anyway as
2049 * it isn't per interface ... maybe just dump the scan
2050 * results periodically for OLBC?
2051 */
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07002052 /* WLAN_FC_STYPE_BEACON, */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002053 };
2054 unsigned int i;
2055
2056 if (nl80211_alloc_mgmt_handle(bss))
2057 return -1;
2058 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
2059 "handle %p", bss->nl_mgmt);
2060
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002061 for (i = 0; i < ARRAY_SIZE(stypes); i++) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002062 if (nl80211_register_frame(bss, bss->nl_mgmt,
2063 (WLAN_FC_TYPE_MGMT << 2) |
2064 (stypes[i] << 4),
2065 NULL, 0) < 0) {
2066 goto out_err;
2067 }
2068 }
2069
2070 if (nl80211_register_spurious_class3(bss))
2071 goto out_err;
2072
2073 if (nl80211_get_wiphy_data_ap(bss) == NULL)
2074 goto out_err;
2075
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002076 nl80211_mgmt_handle_register_eloop(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002077 return 0;
2078
2079out_err:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002080 nl_destroy_handles(&bss->nl_mgmt);
2081 return -1;
2082}
2083
2084
2085static int nl80211_mgmt_subscribe_ap_dev_sme(struct i802_bss *bss)
2086{
2087 if (nl80211_alloc_mgmt_handle(bss))
2088 return -1;
2089 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
2090 "handle %p (device SME)", bss->nl_mgmt);
2091
2092 if (nl80211_register_frame(bss, bss->nl_mgmt,
2093 (WLAN_FC_TYPE_MGMT << 2) |
2094 (WLAN_FC_STYPE_ACTION << 4),
2095 NULL, 0) < 0)
2096 goto out_err;
2097
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002098 nl80211_mgmt_handle_register_eloop(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002099 return 0;
2100
2101out_err:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002102 nl_destroy_handles(&bss->nl_mgmt);
2103 return -1;
2104}
2105
2106
2107static void nl80211_mgmt_unsubscribe(struct i802_bss *bss, const char *reason)
2108{
2109 if (bss->nl_mgmt == NULL)
2110 return;
2111 wpa_printf(MSG_DEBUG, "nl80211: Unsubscribe mgmt frames handle %p "
2112 "(%s)", bss->nl_mgmt, reason);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002113 nl80211_destroy_eloop_handle(&bss->nl_mgmt);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002114
2115 nl80211_put_wiphy_data_ap(bss);
2116}
2117
2118
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002119static void wpa_driver_nl80211_send_rfkill(void *eloop_ctx, void *timeout_ctx)
2120{
2121 wpa_supplicant_event(timeout_ctx, EVENT_INTERFACE_DISABLED, NULL);
2122}
2123
2124
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002125static void nl80211_del_p2pdev(struct i802_bss *bss)
2126{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002127 struct nl_msg *msg;
2128 int ret;
2129
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002130 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_DEL_INTERFACE);
2131 ret = send_and_recv_msgs(bss->drv, msg, NULL, NULL);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002132
2133 wpa_printf(MSG_DEBUG, "nl80211: Delete P2P Device %s (0x%llx): %s",
2134 bss->ifname, (long long unsigned int) bss->wdev_id,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002135 strerror(-ret));
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002136}
2137
2138
2139static int nl80211_set_p2pdev(struct i802_bss *bss, int start)
2140{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002141 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002142 int ret;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002143
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002144 msg = nl80211_cmd_msg(bss, 0, start ? NL80211_CMD_START_P2P_DEVICE :
2145 NL80211_CMD_STOP_P2P_DEVICE);
2146 ret = send_and_recv_msgs(bss->drv, msg, NULL, NULL);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002147
2148 wpa_printf(MSG_DEBUG, "nl80211: %s P2P Device %s (0x%llx): %s",
2149 start ? "Start" : "Stop",
2150 bss->ifname, (long long unsigned int) bss->wdev_id,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002151 strerror(-ret));
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002152 return ret;
2153}
2154
2155
2156static int i802_set_iface_flags(struct i802_bss *bss, int up)
2157{
2158 enum nl80211_iftype nlmode;
2159
2160 nlmode = nl80211_get_ifmode(bss);
2161 if (nlmode != NL80211_IFTYPE_P2P_DEVICE) {
2162 return linux_set_iface_flags(bss->drv->global->ioctl_sock,
2163 bss->ifname, up);
2164 }
2165
2166 /* P2P Device has start/stop which is equivalent */
2167 return nl80211_set_p2pdev(bss, up);
2168}
2169
2170
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002171#ifdef CONFIG_TESTING_OPTIONS
2172static int qca_vendor_test_cmd_handler(struct nl_msg *msg, void *arg)
2173{
2174 /* struct wpa_driver_nl80211_data *drv = arg; */
2175 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2176 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2177
2178
2179 wpa_printf(MSG_DEBUG,
2180 "nl80211: QCA vendor test command response received");
2181
2182 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2183 genlmsg_attrlen(gnlh, 0), NULL);
2184 if (!tb[NL80211_ATTR_VENDOR_DATA]) {
2185 wpa_printf(MSG_DEBUG, "nl80211: No vendor data attribute");
2186 return NL_SKIP;
2187 }
2188
2189 wpa_hexdump(MSG_DEBUG,
2190 "nl80211: Received QCA vendor test command response",
2191 nla_data(tb[NL80211_ATTR_VENDOR_DATA]),
2192 nla_len(tb[NL80211_ATTR_VENDOR_DATA]));
2193
2194 return NL_SKIP;
2195}
2196#endif /* CONFIG_TESTING_OPTIONS */
2197
2198
2199static void qca_vendor_test(struct wpa_driver_nl80211_data *drv)
2200{
2201#ifdef CONFIG_TESTING_OPTIONS
2202 struct nl_msg *msg;
2203 struct nlattr *params;
2204 int ret;
2205
2206 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
2207 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
2208 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
2209 QCA_NL80211_VENDOR_SUBCMD_TEST) ||
2210 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
2211 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_TEST, 123)) {
2212 nlmsg_free(msg);
2213 return;
2214 }
2215 nla_nest_end(msg, params);
2216
2217 ret = send_and_recv_msgs(drv, msg, qca_vendor_test_cmd_handler, drv);
2218 wpa_printf(MSG_DEBUG,
2219 "nl80211: QCA vendor test command returned %d (%s)",
2220 ret, strerror(-ret));
2221#endif /* CONFIG_TESTING_OPTIONS */
2222}
2223
2224
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002225static int
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002226wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002227 const u8 *set_addr, int first,
2228 const char *driver_params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002229{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002230 struct i802_bss *bss = drv->first_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002231 int send_rfkill_event = 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002232 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002233
2234 drv->ifindex = if_nametoindex(bss->ifname);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002235 bss->ifindex = drv->ifindex;
2236 bss->wdev_id = drv->global->if_add_wdevid;
2237 bss->wdev_id_set = drv->global->if_add_wdevid_set;
2238
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002239 bss->if_dynamic = drv->ifindex == drv->global->if_add_ifindex;
2240 bss->if_dynamic = bss->if_dynamic || drv->global->if_add_wdevid_set;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002241 drv->global->if_add_wdevid_set = 0;
2242
Dmitry Shmidt03658832014-08-13 11:03:49 -07002243 if (!bss->if_dynamic && nl80211_get_ifmode(bss) == NL80211_IFTYPE_AP)
2244 bss->static_ap = 1;
2245
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -08002246 if (first &&
2247 nl80211_get_ifmode(bss) != NL80211_IFTYPE_P2P_DEVICE &&
2248 linux_iface_up(drv->global->ioctl_sock, bss->ifname) > 0)
2249 drv->start_iface_up = 1;
2250
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002251 if (wpa_driver_nl80211_capa(drv))
2252 return -1;
2253
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002254 if (driver_params && nl80211_set_param(bss, driver_params) < 0)
2255 return -1;
2256
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002257 wpa_printf(MSG_DEBUG, "nl80211: interface %s in phy %s",
2258 bss->ifname, drv->phyname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002259
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002260 if (set_addr &&
2261 (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0) ||
2262 linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
2263 set_addr)))
2264 return -1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002265
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002266 if (first && nl80211_get_ifmode(bss) == NL80211_IFTYPE_AP)
2267 drv->start_mode_ap = 1;
2268
Dmitry Shmidt03658832014-08-13 11:03:49 -07002269 if (drv->hostapd || bss->static_ap)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002270 nlmode = NL80211_IFTYPE_AP;
2271 else if (bss->if_dynamic)
2272 nlmode = nl80211_get_ifmode(bss);
2273 else
2274 nlmode = NL80211_IFTYPE_STATION;
2275
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002276 if (wpa_driver_nl80211_set_mode(bss, nlmode) < 0) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002277 wpa_printf(MSG_ERROR, "nl80211: Could not configure driver mode");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002278 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002279 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002280
Dmitry Shmidt98660862014-03-11 17:26:21 -07002281 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002282 nl80211_get_macaddr(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002283
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08002284 wpa_driver_nl80211_drv_init_rfkill(drv);
2285
Dmitry Shmidt98660862014-03-11 17:26:21 -07002286 if (!rfkill_is_blocked(drv->rfkill)) {
2287 int ret = i802_set_iface_flags(bss, 1);
2288 if (ret) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002289 wpa_printf(MSG_ERROR, "nl80211: Could not set "
2290 "interface '%s' UP", bss->ifname);
Dmitry Shmidt98660862014-03-11 17:26:21 -07002291 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002292 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002293
2294 if (is_p2p_net_interface(nlmode))
2295 nl80211_disable_11b_rates(bss->drv,
2296 bss->drv->ifindex, 1);
2297
Dmitry Shmidt98660862014-03-11 17:26:21 -07002298 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
2299 return ret;
2300 } else {
2301 wpa_printf(MSG_DEBUG, "nl80211: Could not yet enable "
2302 "interface '%s' due to rfkill", bss->ifname);
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08002303 if (nlmode != NL80211_IFTYPE_P2P_DEVICE)
2304 drv->if_disabled = 1;
2305
Dmitry Shmidt98660862014-03-11 17:26:21 -07002306 send_rfkill_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002307 }
2308
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08002309 if (!drv->hostapd && nlmode != NL80211_IFTYPE_P2P_DEVICE)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002310 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
2311 1, IF_OPER_DORMANT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002312
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08002313 if (nlmode != NL80211_IFTYPE_P2P_DEVICE) {
2314 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
2315 bss->addr))
2316 return -1;
2317 os_memcpy(drv->perm_addr, bss->addr, ETH_ALEN);
2318 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002319
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002320 if (send_rfkill_event) {
2321 eloop_register_timeout(0, 0, wpa_driver_nl80211_send_rfkill,
2322 drv, drv->ctx);
2323 }
2324
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002325 if (drv->vendor_cmd_test_avail)
2326 qca_vendor_test(drv);
2327
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002328 return 0;
2329}
2330
2331
2332static int wpa_driver_nl80211_del_beacon(struct wpa_driver_nl80211_data *drv)
2333{
2334 struct nl_msg *msg;
2335
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002336 wpa_printf(MSG_DEBUG, "nl80211: Remove beacon (ifindex=%d)",
2337 drv->ifindex);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002338 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_DEL_BEACON);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002339 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002340}
2341
2342
2343/**
2344 * wpa_driver_nl80211_deinit - Deinitialize nl80211 driver interface
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002345 * @bss: Pointer to private nl80211 data from wpa_driver_nl80211_init()
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002346 *
2347 * Shut down driver interface and processing of driver events. Free
2348 * private data buffer if one was allocated in wpa_driver_nl80211_init().
2349 */
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002350static void wpa_driver_nl80211_deinit(struct i802_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002351{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002352 struct wpa_driver_nl80211_data *drv = bss->drv;
2353
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002354 wpa_printf(MSG_INFO, "nl80211: deinit ifname=%s disabled_11b_rates=%d",
2355 bss->ifname, drv->disabled_11b_rates);
2356
Dmitry Shmidt04949592012-07-19 12:16:46 -07002357 bss->in_deinit = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002358 if (drv->data_tx_status)
2359 eloop_unregister_read_sock(drv->eapol_tx_sock);
2360 if (drv->eapol_tx_sock >= 0)
2361 close(drv->eapol_tx_sock);
2362
2363 if (bss->nl_preq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002364 wpa_driver_nl80211_probe_req_report(bss, 0);
2365 if (bss->added_if_into_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002366 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
2367 bss->ifname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002368 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
2369 "interface %s from bridge %s: %s",
2370 bss->ifname, bss->brname, strerror(errno));
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002371 if (drv->rtnl_sk)
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07002372 nl80211_handle_destroy(drv->rtnl_sk);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002373 }
2374 if (bss->added_bridge) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002375 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->brname,
2376 0) < 0)
2377 wpa_printf(MSG_INFO,
2378 "nl80211: Could not set bridge %s down",
2379 bss->brname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002380 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002381 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
2382 "bridge %s: %s",
2383 bss->brname, strerror(errno));
2384 }
2385
2386 nl80211_remove_monitor_interface(drv);
2387
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002388 if (is_ap_interface(drv->nlmode))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002389 wpa_driver_nl80211_del_beacon(drv);
2390
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002391 if (drv->eapol_sock >= 0) {
2392 eloop_unregister_read_sock(drv->eapol_sock);
2393 close(drv->eapol_sock);
2394 }
2395
2396 if (drv->if_indices != drv->default_if_indices)
2397 os_free(drv->if_indices);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002398
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002399 if (drv->if_indices_reason != drv->default_if_indices_reason)
2400 os_free(drv->if_indices_reason);
2401
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002402 if (drv->disabled_11b_rates)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002403 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
2404
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002405 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, 0,
2406 IF_OPER_UP);
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07002407 eloop_cancel_timeout(wpa_driver_nl80211_send_rfkill, drv, drv->ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002408 rfkill_deinit(drv->rfkill);
2409
2410 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
2411
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002412 if (!drv->start_iface_up)
2413 (void) i802_set_iface_flags(bss, 0);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002414
2415 if (drv->addr_changed) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002416 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname,
2417 0) < 0) {
2418 wpa_printf(MSG_DEBUG,
2419 "nl80211: Could not set interface down to restore permanent MAC address");
2420 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002421 if (linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
2422 drv->perm_addr) < 0) {
2423 wpa_printf(MSG_DEBUG,
2424 "nl80211: Could not restore permanent MAC address");
2425 }
2426 }
2427
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002428 if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002429 if (!drv->hostapd || !drv->start_mode_ap)
2430 wpa_driver_nl80211_set_mode(bss,
2431 NL80211_IFTYPE_STATION);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002432 nl80211_mgmt_unsubscribe(bss, "deinit");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002433 } else {
2434 nl80211_mgmt_unsubscribe(bss, "deinit");
2435 nl80211_del_p2pdev(bss);
2436 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002437
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002438 nl80211_destroy_bss(drv->first_bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002439
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002440 os_free(drv->filter_ssids);
2441
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002442 os_free(drv->auth_ie);
2443
2444 if (drv->in_interface_list)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002445 dl_list_del(&drv->list);
2446
Dmitry Shmidt444d5672013-04-01 13:08:44 -07002447 os_free(drv->extended_capa);
2448 os_free(drv->extended_capa_mask);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002449 os_free(drv->first_bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002450 os_free(drv);
2451}
2452
2453
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002454static u32 wpa_alg_to_cipher_suite(enum wpa_alg alg, size_t key_len)
2455{
2456 switch (alg) {
2457 case WPA_ALG_WEP:
2458 if (key_len == 5)
2459 return WLAN_CIPHER_SUITE_WEP40;
2460 return WLAN_CIPHER_SUITE_WEP104;
2461 case WPA_ALG_TKIP:
2462 return WLAN_CIPHER_SUITE_TKIP;
2463 case WPA_ALG_CCMP:
2464 return WLAN_CIPHER_SUITE_CCMP;
2465 case WPA_ALG_GCMP:
2466 return WLAN_CIPHER_SUITE_GCMP;
2467 case WPA_ALG_CCMP_256:
2468 return WLAN_CIPHER_SUITE_CCMP_256;
2469 case WPA_ALG_GCMP_256:
2470 return WLAN_CIPHER_SUITE_GCMP_256;
2471 case WPA_ALG_IGTK:
2472 return WLAN_CIPHER_SUITE_AES_CMAC;
2473 case WPA_ALG_BIP_GMAC_128:
2474 return WLAN_CIPHER_SUITE_BIP_GMAC_128;
2475 case WPA_ALG_BIP_GMAC_256:
2476 return WLAN_CIPHER_SUITE_BIP_GMAC_256;
2477 case WPA_ALG_BIP_CMAC_256:
2478 return WLAN_CIPHER_SUITE_BIP_CMAC_256;
2479 case WPA_ALG_SMS4:
2480 return WLAN_CIPHER_SUITE_SMS4;
2481 case WPA_ALG_KRK:
2482 return WLAN_CIPHER_SUITE_KRK;
2483 case WPA_ALG_NONE:
2484 case WPA_ALG_PMK:
2485 wpa_printf(MSG_ERROR, "nl80211: Unexpected encryption algorithm %d",
2486 alg);
2487 return 0;
2488 }
2489
2490 wpa_printf(MSG_ERROR, "nl80211: Unsupported encryption algorithm %d",
2491 alg);
2492 return 0;
2493}
2494
2495
2496static u32 wpa_cipher_to_cipher_suite(unsigned int cipher)
2497{
2498 switch (cipher) {
2499 case WPA_CIPHER_CCMP_256:
2500 return WLAN_CIPHER_SUITE_CCMP_256;
2501 case WPA_CIPHER_GCMP_256:
2502 return WLAN_CIPHER_SUITE_GCMP_256;
2503 case WPA_CIPHER_CCMP:
2504 return WLAN_CIPHER_SUITE_CCMP;
2505 case WPA_CIPHER_GCMP:
2506 return WLAN_CIPHER_SUITE_GCMP;
2507 case WPA_CIPHER_TKIP:
2508 return WLAN_CIPHER_SUITE_TKIP;
2509 case WPA_CIPHER_WEP104:
2510 return WLAN_CIPHER_SUITE_WEP104;
2511 case WPA_CIPHER_WEP40:
2512 return WLAN_CIPHER_SUITE_WEP40;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002513 case WPA_CIPHER_GTK_NOT_USED:
2514 return WLAN_CIPHER_SUITE_NO_GROUP_ADDR;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002515 }
2516
2517 return 0;
2518}
2519
2520
2521static int wpa_cipher_to_cipher_suites(unsigned int ciphers, u32 suites[],
2522 int max_suites)
2523{
2524 int num_suites = 0;
2525
2526 if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP_256)
2527 suites[num_suites++] = WLAN_CIPHER_SUITE_CCMP_256;
2528 if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP_256)
2529 suites[num_suites++] = WLAN_CIPHER_SUITE_GCMP_256;
2530 if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP)
2531 suites[num_suites++] = WLAN_CIPHER_SUITE_CCMP;
2532 if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP)
2533 suites[num_suites++] = WLAN_CIPHER_SUITE_GCMP;
2534 if (num_suites < max_suites && ciphers & WPA_CIPHER_TKIP)
2535 suites[num_suites++] = WLAN_CIPHER_SUITE_TKIP;
2536 if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP104)
2537 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP104;
2538 if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP40)
2539 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP40;
2540
2541 return num_suites;
2542}
2543
2544
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002545#ifdef CONFIG_DRIVER_NL80211_QCA
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002546static int issue_key_mgmt_set_key(struct wpa_driver_nl80211_data *drv,
2547 const u8 *key, size_t key_len)
2548{
2549 struct nl_msg *msg;
2550 int ret;
2551
2552 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_KEY_MGMT_OFFLOAD))
2553 return 0;
2554
2555 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
2556 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
2557 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
2558 QCA_NL80211_VENDOR_SUBCMD_KEY_MGMT_SET_KEY) ||
2559 nla_put(msg, NL80211_ATTR_VENDOR_DATA, key_len, key)) {
2560 nl80211_nlmsg_clear(msg);
2561 nlmsg_free(msg);
2562 return -1;
2563 }
2564 ret = send_and_recv_msgs(drv, msg, NULL, (void *) -1);
2565 if (ret) {
2566 wpa_printf(MSG_DEBUG,
2567 "nl80211: Key management set key failed: ret=%d (%s)",
2568 ret, strerror(-ret));
2569 }
2570
2571 return ret;
2572}
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002573#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002574
2575
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002576static int wpa_driver_nl80211_set_key(const char *ifname, struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002577 enum wpa_alg alg, const u8 *addr,
2578 int key_idx, int set_tx,
2579 const u8 *seq, size_t seq_len,
2580 const u8 *key, size_t key_len)
2581{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002582 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002583 int ifindex;
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002584 struct nl_msg *msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002585 int ret;
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07002586 int tdls = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002587
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002588 /* Ignore for P2P Device */
2589 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
2590 return 0;
2591
2592 ifindex = if_nametoindex(ifname);
2593 wpa_printf(MSG_DEBUG, "%s: ifindex=%d (%s) alg=%d addr=%p key_idx=%d "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002594 "set_tx=%d seq_len=%lu key_len=%lu",
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002595 __func__, ifindex, ifname, alg, addr, key_idx, set_tx,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002596 (unsigned long) seq_len, (unsigned long) key_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002597#ifdef CONFIG_TDLS
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07002598 if (key_idx == -1) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002599 key_idx = 0;
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07002600 tdls = 1;
2601 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002602#endif /* CONFIG_TDLS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002603
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002604#ifdef CONFIG_DRIVER_NL80211_QCA
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002605 if (alg == WPA_ALG_PMK &&
2606 (drv->capa.flags & WPA_DRIVER_FLAGS_KEY_MGMT_OFFLOAD)) {
2607 wpa_printf(MSG_DEBUG, "%s: calling issue_key_mgmt_set_key",
2608 __func__);
2609 ret = issue_key_mgmt_set_key(drv, key, key_len);
2610 return ret;
2611 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002612#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002613
2614 if (alg == WPA_ALG_NONE) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002615 msg = nl80211_ifindex_msg(drv, ifindex, 0, NL80211_CMD_DEL_KEY);
2616 if (!msg)
2617 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002618 } else {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002619 u32 suite;
2620
2621 suite = wpa_alg_to_cipher_suite(alg, key_len);
2622 if (!suite)
2623 goto fail;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002624 msg = nl80211_ifindex_msg(drv, ifindex, 0, NL80211_CMD_NEW_KEY);
2625 if (!msg ||
2626 nla_put(msg, NL80211_ATTR_KEY_DATA, key_len, key) ||
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002627 nla_put_u32(msg, NL80211_ATTR_KEY_CIPHER, suite))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002628 goto fail;
Dmitry Shmidt98660862014-03-11 17:26:21 -07002629 wpa_hexdump_key(MSG_DEBUG, "nl80211: KEY_DATA", key, key_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002630 }
2631
Dmitry Shmidt98660862014-03-11 17:26:21 -07002632 if (seq && seq_len) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002633 if (nla_put(msg, NL80211_ATTR_KEY_SEQ, seq_len, seq))
2634 goto fail;
Dmitry Shmidt98660862014-03-11 17:26:21 -07002635 wpa_hexdump(MSG_DEBUG, "nl80211: KEY_SEQ", seq, seq_len);
2636 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002637
2638 if (addr && !is_broadcast_ether_addr(addr)) {
2639 wpa_printf(MSG_DEBUG, " addr=" MACSTR, MAC2STR(addr));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002640 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
2641 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002642
2643 if (alg != WPA_ALG_WEP && key_idx && !set_tx) {
2644 wpa_printf(MSG_DEBUG, " RSN IBSS RX GTK");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002645 if (nla_put_u32(msg, NL80211_ATTR_KEY_TYPE,
2646 NL80211_KEYTYPE_GROUP))
2647 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002648 }
2649 } else if (addr && is_broadcast_ether_addr(addr)) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002650 struct nlattr *types;
2651
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002652 wpa_printf(MSG_DEBUG, " broadcast key");
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002653
2654 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002655 if (!types ||
2656 nla_put_flag(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST))
2657 goto fail;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002658 nla_nest_end(msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002659 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002660 if (nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
2661 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002662
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002663 ret = send_and_recv_msgs(drv, msg, NULL, key ? (void *) -1 : NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002664 if ((ret == -ENOENT || ret == -ENOLINK) && alg == WPA_ALG_NONE)
2665 ret = 0;
2666 if (ret)
2667 wpa_printf(MSG_DEBUG, "nl80211: set_key failed; err=%d %s)",
2668 ret, strerror(-ret));
2669
2670 /*
2671 * If we failed or don't need to set the default TX key (below),
2672 * we're done here.
2673 */
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07002674 if (ret || !set_tx || alg == WPA_ALG_NONE || tdls)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002675 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002676 if (is_ap_interface(drv->nlmode) && addr &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002677 !is_broadcast_ether_addr(addr))
2678 return ret;
2679
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002680 msg = nl80211_ifindex_msg(drv, ifindex, 0, NL80211_CMD_SET_KEY);
2681 if (!msg ||
2682 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx) ||
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002683 nla_put_flag(msg, (alg == WPA_ALG_IGTK ||
2684 alg == WPA_ALG_BIP_GMAC_128 ||
2685 alg == WPA_ALG_BIP_GMAC_256 ||
2686 alg == WPA_ALG_BIP_CMAC_256) ?
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002687 NL80211_ATTR_KEY_DEFAULT_MGMT :
2688 NL80211_ATTR_KEY_DEFAULT))
2689 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002690 if (addr && is_broadcast_ether_addr(addr)) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002691 struct nlattr *types;
2692
2693 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002694 if (!types ||
2695 nla_put_flag(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST))
2696 goto fail;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002697 nla_nest_end(msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002698 } else if (addr) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002699 struct nlattr *types;
2700
2701 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002702 if (!types ||
2703 nla_put_flag(msg, NL80211_KEY_DEFAULT_TYPE_UNICAST))
2704 goto fail;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002705 nla_nest_end(msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002706 }
2707
2708 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
2709 if (ret == -ENOENT)
2710 ret = 0;
2711 if (ret)
2712 wpa_printf(MSG_DEBUG, "nl80211: set_key default failed; "
2713 "err=%d %s)", ret, strerror(-ret));
2714 return ret;
2715
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002716fail:
2717 nl80211_nlmsg_clear(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002718 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002719 return -ENOBUFS;
2720}
2721
2722
2723static int nl_add_key(struct nl_msg *msg, enum wpa_alg alg,
2724 int key_idx, int defkey,
2725 const u8 *seq, size_t seq_len,
2726 const u8 *key, size_t key_len)
2727{
2728 struct nlattr *key_attr = nla_nest_start(msg, NL80211_ATTR_KEY);
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002729 u32 suite;
2730
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002731 if (!key_attr)
2732 return -1;
2733
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002734 suite = wpa_alg_to_cipher_suite(alg, key_len);
2735 if (!suite)
2736 return -1;
2737
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002738 if (defkey && alg == WPA_ALG_IGTK) {
2739 if (nla_put_flag(msg, NL80211_KEY_DEFAULT_MGMT))
2740 return -1;
2741 } else if (defkey) {
2742 if (nla_put_flag(msg, NL80211_KEY_DEFAULT))
2743 return -1;
2744 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002745
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002746 if (nla_put_u8(msg, NL80211_KEY_IDX, key_idx) ||
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002747 nla_put_u32(msg, NL80211_KEY_CIPHER, suite) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002748 (seq && seq_len &&
2749 nla_put(msg, NL80211_KEY_SEQ, seq_len, seq)) ||
2750 nla_put(msg, NL80211_KEY_DATA, key_len, key))
2751 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002752
2753 nla_nest_end(msg, key_attr);
2754
2755 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002756}
2757
2758
2759static int nl80211_set_conn_keys(struct wpa_driver_associate_params *params,
2760 struct nl_msg *msg)
2761{
2762 int i, privacy = 0;
2763 struct nlattr *nl_keys, *nl_key;
2764
2765 for (i = 0; i < 4; i++) {
2766 if (!params->wep_key[i])
2767 continue;
2768 privacy = 1;
2769 break;
2770 }
2771 if (params->wps == WPS_MODE_PRIVACY)
2772 privacy = 1;
2773 if (params->pairwise_suite &&
2774 params->pairwise_suite != WPA_CIPHER_NONE)
2775 privacy = 1;
2776
2777 if (!privacy)
2778 return 0;
2779
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002780 if (nla_put_flag(msg, NL80211_ATTR_PRIVACY))
2781 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002782
2783 nl_keys = nla_nest_start(msg, NL80211_ATTR_KEYS);
2784 if (!nl_keys)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002785 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002786
2787 for (i = 0; i < 4; i++) {
2788 if (!params->wep_key[i])
2789 continue;
2790
2791 nl_key = nla_nest_start(msg, i);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002792 if (!nl_key ||
2793 nla_put(msg, NL80211_KEY_DATA, params->wep_key_len[i],
2794 params->wep_key[i]) ||
2795 nla_put_u32(msg, NL80211_KEY_CIPHER,
2796 params->wep_key_len[i] == 5 ?
2797 WLAN_CIPHER_SUITE_WEP40 :
2798 WLAN_CIPHER_SUITE_WEP104) ||
2799 nla_put_u8(msg, NL80211_KEY_IDX, i) ||
2800 (i == params->wep_tx_keyidx &&
2801 nla_put_flag(msg, NL80211_KEY_DEFAULT)))
2802 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002803
2804 nla_nest_end(msg, nl_key);
2805 }
2806 nla_nest_end(msg, nl_keys);
2807
2808 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002809}
2810
2811
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002812int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
2813 const u8 *addr, int cmd, u16 reason_code,
2814 int local_state_change)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002815{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002816 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002817 struct nl_msg *msg;
2818
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002819 if (!(msg = nl80211_drv_msg(drv, 0, cmd)) ||
2820 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code) ||
2821 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
2822 (local_state_change &&
2823 nla_put_flag(msg, NL80211_ATTR_LOCAL_STATE_CHANGE))) {
2824 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002825 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002826 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002827
2828 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002829 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002830 wpa_dbg(drv->ctx, MSG_DEBUG,
2831 "nl80211: MLME command failed: reason=%u ret=%d (%s)",
2832 reason_code, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002833 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002834 return ret;
2835}
2836
2837
2838static int wpa_driver_nl80211_disconnect(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002839 int reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002840{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002841 int ret;
2842
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002843 wpa_printf(MSG_DEBUG, "%s(reason_code=%d)", __func__, reason_code);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07002844 nl80211_mark_disconnected(drv);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002845 /* Disconnect command doesn't need BSSID - it uses cached value */
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002846 ret = wpa_driver_nl80211_mlme(drv, NULL, NL80211_CMD_DISCONNECT,
2847 reason_code, 0);
2848 /*
2849 * For locally generated disconnect, supplicant already generates a
2850 * DEAUTH event, so ignore the event from NL80211.
2851 */
2852 drv->ignore_next_local_disconnect = ret == 0;
2853
2854 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002855}
2856
2857
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002858static int wpa_driver_nl80211_deauthenticate(struct i802_bss *bss,
2859 const u8 *addr, int reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002860{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002861 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07002862 int ret;
Dmitry Shmidt413dde72014-04-11 10:23:22 -07002863
2864 if (drv->nlmode == NL80211_IFTYPE_ADHOC) {
2865 nl80211_mark_disconnected(drv);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002866 return nl80211_leave_ibss(drv, 1);
Dmitry Shmidt413dde72014-04-11 10:23:22 -07002867 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002868 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME))
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002869 return wpa_driver_nl80211_disconnect(drv, reason_code);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002870 wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " reason_code=%d)",
2871 __func__, MAC2STR(addr), reason_code);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07002872 nl80211_mark_disconnected(drv);
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07002873 ret = wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DEAUTHENTICATE,
2874 reason_code, 0);
2875 /*
2876 * For locally generated deauthenticate, supplicant already generates a
2877 * DEAUTH event, so ignore the event from NL80211.
2878 */
2879 drv->ignore_next_local_deauth = ret == 0;
2880 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002881}
2882
2883
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002884static void nl80211_copy_auth_params(struct wpa_driver_nl80211_data *drv,
2885 struct wpa_driver_auth_params *params)
2886{
2887 int i;
2888
2889 drv->auth_freq = params->freq;
2890 drv->auth_alg = params->auth_alg;
2891 drv->auth_wep_tx_keyidx = params->wep_tx_keyidx;
2892 drv->auth_local_state_change = params->local_state_change;
2893 drv->auth_p2p = params->p2p;
2894
2895 if (params->bssid)
2896 os_memcpy(drv->auth_bssid_, params->bssid, ETH_ALEN);
2897 else
2898 os_memset(drv->auth_bssid_, 0, ETH_ALEN);
2899
2900 if (params->ssid) {
2901 os_memcpy(drv->auth_ssid, params->ssid, params->ssid_len);
2902 drv->auth_ssid_len = params->ssid_len;
2903 } else
2904 drv->auth_ssid_len = 0;
2905
2906
2907 os_free(drv->auth_ie);
2908 drv->auth_ie = NULL;
2909 drv->auth_ie_len = 0;
2910 if (params->ie) {
2911 drv->auth_ie = os_malloc(params->ie_len);
2912 if (drv->auth_ie) {
2913 os_memcpy(drv->auth_ie, params->ie, params->ie_len);
2914 drv->auth_ie_len = params->ie_len;
2915 }
2916 }
2917
2918 for (i = 0; i < 4; i++) {
2919 if (params->wep_key[i] && params->wep_key_len[i] &&
2920 params->wep_key_len[i] <= 16) {
2921 os_memcpy(drv->auth_wep_key[i], params->wep_key[i],
2922 params->wep_key_len[i]);
2923 drv->auth_wep_key_len[i] = params->wep_key_len[i];
2924 } else
2925 drv->auth_wep_key_len[i] = 0;
2926 }
2927}
2928
2929
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002930static void nl80211_unmask_11b_rates(struct i802_bss *bss)
2931{
2932 struct wpa_driver_nl80211_data *drv = bss->drv;
2933
2934 if (is_p2p_net_interface(drv->nlmode) || !drv->disabled_11b_rates)
2935 return;
2936
2937 /*
2938 * Looks like we failed to unmask 11b rates previously. This could
2939 * happen, e.g., if the interface was down at the point in time when a
2940 * P2P group was terminated.
2941 */
2942 wpa_printf(MSG_DEBUG,
2943 "nl80211: Interface %s mode is for non-P2P, but 11b rates were disabled - re-enable them",
2944 bss->ifname);
2945 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
2946}
2947
2948
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002949static int wpa_driver_nl80211_authenticate(
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002950 struct i802_bss *bss, struct wpa_driver_auth_params *params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002951{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002952 struct wpa_driver_nl80211_data *drv = bss->drv;
2953 int ret = -1, i;
2954 struct nl_msg *msg;
2955 enum nl80211_auth_type type;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002956 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002957 int count = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002958 int is_retry;
2959
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002960 nl80211_unmask_11b_rates(bss);
2961
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002962 is_retry = drv->retry_auth;
2963 drv->retry_auth = 0;
Dmitry Shmidt98660862014-03-11 17:26:21 -07002964 drv->ignore_deauth_event = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002965
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07002966 nl80211_mark_disconnected(drv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002967 os_memset(drv->auth_bssid, 0, ETH_ALEN);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07002968 if (params->bssid)
2969 os_memcpy(drv->auth_attempt_bssid, params->bssid, ETH_ALEN);
2970 else
2971 os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002972 /* FIX: IBSS mode */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002973 nlmode = params->p2p ?
2974 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
2975 if (drv->nlmode != nlmode &&
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002976 wpa_driver_nl80211_set_mode(bss, nlmode) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002977 return -1;
2978
2979retry:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002980 wpa_printf(MSG_DEBUG, "nl80211: Authenticate (ifindex=%d)",
2981 drv->ifindex);
2982
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002983 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_AUTHENTICATE);
2984 if (!msg)
2985 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002986
2987 for (i = 0; i < 4; i++) {
2988 if (!params->wep_key[i])
2989 continue;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002990 wpa_driver_nl80211_set_key(bss->ifname, bss, WPA_ALG_WEP,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002991 NULL, i,
2992 i == params->wep_tx_keyidx, NULL, 0,
2993 params->wep_key[i],
2994 params->wep_key_len[i]);
2995 if (params->wep_tx_keyidx != i)
2996 continue;
2997 if (nl_add_key(msg, WPA_ALG_WEP, i, 1, NULL, 0,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002998 params->wep_key[i], params->wep_key_len[i]))
2999 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003000 }
3001
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003002 if (params->bssid) {
3003 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
3004 MAC2STR(params->bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003005 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid))
3006 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003007 }
3008 if (params->freq) {
3009 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003010 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq))
3011 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003012 }
3013 if (params->ssid) {
3014 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
3015 params->ssid, params->ssid_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003016 if (nla_put(msg, NL80211_ATTR_SSID, params->ssid_len,
3017 params->ssid))
3018 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003019 }
3020 wpa_hexdump(MSG_DEBUG, " * IEs", params->ie, params->ie_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003021 if (params->ie &&
3022 nla_put(msg, NL80211_ATTR_IE, params->ie_len, params->ie))
3023 goto fail;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003024 if (params->sae_data) {
3025 wpa_hexdump(MSG_DEBUG, " * SAE data", params->sae_data,
3026 params->sae_data_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003027 if (nla_put(msg, NL80211_ATTR_SAE_DATA, params->sae_data_len,
3028 params->sae_data))
3029 goto fail;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003030 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003031 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
3032 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
3033 else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
3034 type = NL80211_AUTHTYPE_SHARED_KEY;
3035 else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
3036 type = NL80211_AUTHTYPE_NETWORK_EAP;
3037 else if (params->auth_alg & WPA_AUTH_ALG_FT)
3038 type = NL80211_AUTHTYPE_FT;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003039 else if (params->auth_alg & WPA_AUTH_ALG_SAE)
3040 type = NL80211_AUTHTYPE_SAE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003041 else
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003042 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003043 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003044 if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE, type))
3045 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003046 if (params->local_state_change) {
3047 wpa_printf(MSG_DEBUG, " * Local state change only");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003048 if (nla_put_flag(msg, NL80211_ATTR_LOCAL_STATE_CHANGE))
3049 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003050 }
3051
3052 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3053 msg = NULL;
3054 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003055 wpa_dbg(drv->ctx, MSG_DEBUG,
3056 "nl80211: MLME command failed (auth): ret=%d (%s)",
3057 ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003058 count++;
3059 if (ret == -EALREADY && count == 1 && params->bssid &&
3060 !params->local_state_change) {
3061 /*
3062 * mac80211 does not currently accept new
3063 * authentication if we are already authenticated. As a
3064 * workaround, force deauthentication and try again.
3065 */
3066 wpa_printf(MSG_DEBUG, "nl80211: Retry authentication "
3067 "after forced deauthentication");
Dmitry Shmidt98660862014-03-11 17:26:21 -07003068 drv->ignore_deauth_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003069 wpa_driver_nl80211_deauthenticate(
3070 bss, params->bssid,
3071 WLAN_REASON_PREV_AUTH_NOT_VALID);
3072 nlmsg_free(msg);
3073 goto retry;
3074 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003075
3076 if (ret == -ENOENT && params->freq && !is_retry) {
3077 /*
3078 * cfg80211 has likely expired the BSS entry even
3079 * though it was previously available in our internal
3080 * BSS table. To recover quickly, start a single
3081 * channel scan on the specified channel.
3082 */
3083 struct wpa_driver_scan_params scan;
3084 int freqs[2];
3085
3086 os_memset(&scan, 0, sizeof(scan));
3087 scan.num_ssids = 1;
3088 if (params->ssid) {
3089 scan.ssids[0].ssid = params->ssid;
3090 scan.ssids[0].ssid_len = params->ssid_len;
3091 }
3092 freqs[0] = params->freq;
3093 freqs[1] = 0;
3094 scan.freqs = freqs;
3095 wpa_printf(MSG_DEBUG, "nl80211: Trigger single "
3096 "channel scan to refresh cfg80211 BSS "
3097 "entry");
3098 ret = wpa_driver_nl80211_scan(bss, &scan);
3099 if (ret == 0) {
3100 nl80211_copy_auth_params(drv, params);
3101 drv->scan_for_auth = 1;
3102 }
3103 } else if (is_retry) {
3104 /*
3105 * Need to indicate this with an event since the return
3106 * value from the retry is not delivered to core code.
3107 */
3108 union wpa_event_data event;
3109 wpa_printf(MSG_DEBUG, "nl80211: Authentication retry "
3110 "failed");
3111 os_memset(&event, 0, sizeof(event));
3112 os_memcpy(event.timeout_event.addr, drv->auth_bssid_,
3113 ETH_ALEN);
3114 wpa_supplicant_event(drv->ctx, EVENT_AUTH_TIMED_OUT,
3115 &event);
3116 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003117 } else {
3118 wpa_printf(MSG_DEBUG,
3119 "nl80211: Authentication request send successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003120 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003121
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003122fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003123 nlmsg_free(msg);
3124 return ret;
3125}
3126
3127
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003128int wpa_driver_nl80211_authenticate_retry(struct wpa_driver_nl80211_data *drv)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003129{
3130 struct wpa_driver_auth_params params;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003131 struct i802_bss *bss = drv->first_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003132 int i;
3133
3134 wpa_printf(MSG_DEBUG, "nl80211: Try to authenticate again");
3135
3136 os_memset(&params, 0, sizeof(params));
3137 params.freq = drv->auth_freq;
3138 params.auth_alg = drv->auth_alg;
3139 params.wep_tx_keyidx = drv->auth_wep_tx_keyidx;
3140 params.local_state_change = drv->auth_local_state_change;
3141 params.p2p = drv->auth_p2p;
3142
3143 if (!is_zero_ether_addr(drv->auth_bssid_))
3144 params.bssid = drv->auth_bssid_;
3145
3146 if (drv->auth_ssid_len) {
3147 params.ssid = drv->auth_ssid;
3148 params.ssid_len = drv->auth_ssid_len;
3149 }
3150
3151 params.ie = drv->auth_ie;
3152 params.ie_len = drv->auth_ie_len;
3153
3154 for (i = 0; i < 4; i++) {
3155 if (drv->auth_wep_key_len[i]) {
3156 params.wep_key[i] = drv->auth_wep_key[i];
3157 params.wep_key_len[i] = drv->auth_wep_key_len[i];
3158 }
3159 }
3160
3161 drv->retry_auth = 1;
3162 return wpa_driver_nl80211_authenticate(bss, &params);
3163}
3164
3165
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003166static int wpa_driver_nl80211_send_frame(struct i802_bss *bss,
3167 const void *data, size_t len,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003168 int encrypt, int noack,
3169 unsigned int freq, int no_cck,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003170 int offchanok, unsigned int wait_time,
3171 const u16 *csa_offs,
3172 size_t csa_offs_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003173{
3174 struct wpa_driver_nl80211_data *drv = bss->drv;
3175 u64 cookie;
Dmitry Shmidt051af732013-10-22 13:52:46 -07003176 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003177
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07003178 if (freq == 0 && drv->nlmode == NL80211_IFTYPE_ADHOC) {
3179 freq = nl80211_get_assoc_freq(drv);
3180 wpa_printf(MSG_DEBUG,
3181 "nl80211: send_frame - Use assoc_freq=%u for IBSS",
3182 freq);
3183 }
Dmitry Shmidt56052862013-10-04 10:23:25 -07003184 if (freq == 0) {
3185 wpa_printf(MSG_DEBUG, "nl80211: send_frame - Use bss->freq=%u",
3186 bss->freq);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003187 freq = bss->freq;
Dmitry Shmidt56052862013-10-04 10:23:25 -07003188 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003189
Dmitry Shmidt56052862013-10-04 10:23:25 -07003190 if (drv->use_monitor) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003191 wpa_printf(MSG_DEBUG, "nl80211: send_frame(freq=%u bss->freq=%u) -> send_monitor",
Dmitry Shmidt56052862013-10-04 10:23:25 -07003192 freq, bss->freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003193 return nl80211_send_monitor(drv, data, len, encrypt, noack);
Dmitry Shmidt56052862013-10-04 10:23:25 -07003194 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003195
Dmitry Shmidt56052862013-10-04 10:23:25 -07003196 wpa_printf(MSG_DEBUG, "nl80211: send_frame -> send_frame_cmd");
Dmitry Shmidt051af732013-10-22 13:52:46 -07003197 res = nl80211_send_frame_cmd(bss, freq, wait_time, data, len,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003198 &cookie, no_cck, noack, offchanok,
3199 csa_offs, csa_offs_len);
Dmitry Shmidt051af732013-10-22 13:52:46 -07003200 if (res == 0 && !noack) {
3201 const struct ieee80211_mgmt *mgmt;
3202 u16 fc;
3203
3204 mgmt = (const struct ieee80211_mgmt *) data;
3205 fc = le_to_host16(mgmt->frame_control);
3206 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
3207 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_ACTION) {
3208 wpa_printf(MSG_MSGDUMP,
3209 "nl80211: Update send_action_cookie from 0x%llx to 0x%llx",
3210 (long long unsigned int)
3211 drv->send_action_cookie,
3212 (long long unsigned int) cookie);
3213 drv->send_action_cookie = cookie;
3214 }
3215 }
3216
3217 return res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003218}
3219
3220
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003221static int wpa_driver_nl80211_send_mlme(struct i802_bss *bss, const u8 *data,
3222 size_t data_len, int noack,
3223 unsigned int freq, int no_cck,
3224 int offchanok,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003225 unsigned int wait_time,
3226 const u16 *csa_offs,
3227 size_t csa_offs_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003228{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003229 struct wpa_driver_nl80211_data *drv = bss->drv;
3230 struct ieee80211_mgmt *mgmt;
3231 int encrypt = 1;
3232 u16 fc;
3233
3234 mgmt = (struct ieee80211_mgmt *) data;
3235 fc = le_to_host16(mgmt->frame_control);
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07003236 wpa_printf(MSG_DEBUG, "nl80211: send_mlme - da= " MACSTR
3237 " noack=%d freq=%u no_cck=%d offchanok=%d wait_time=%u fc=0x%x (%s) nlmode=%d",
3238 MAC2STR(mgmt->da), noack, freq, no_cck, offchanok, wait_time,
3239 fc, fc2str(fc), drv->nlmode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003240
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003241 if ((is_sta_interface(drv->nlmode) ||
3242 drv->nlmode == NL80211_IFTYPE_P2P_DEVICE) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003243 WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
3244 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_RESP) {
3245 /*
3246 * The use of last_mgmt_freq is a bit of a hack,
3247 * but it works due to the single-threaded nature
3248 * of wpa_supplicant.
3249 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07003250 if (freq == 0) {
3251 wpa_printf(MSG_DEBUG, "nl80211: Use last_mgmt_freq=%d",
3252 drv->last_mgmt_freq);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003253 freq = drv->last_mgmt_freq;
Dmitry Shmidt56052862013-10-04 10:23:25 -07003254 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003255 return nl80211_send_frame_cmd(bss, freq, 0,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003256 data, data_len, NULL, 1, noack,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003257 1, csa_offs, csa_offs_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003258 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003259
3260 if (drv->device_ap_sme && is_ap_interface(drv->nlmode)) {
Dmitry Shmidt56052862013-10-04 10:23:25 -07003261 if (freq == 0) {
3262 wpa_printf(MSG_DEBUG, "nl80211: Use bss->freq=%d",
3263 bss->freq);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003264 freq = bss->freq;
Dmitry Shmidt56052862013-10-04 10:23:25 -07003265 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003266 return nl80211_send_frame_cmd(bss, freq,
3267 (int) freq == bss->freq ? 0 :
3268 wait_time,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003269 data, data_len,
3270 &drv->send_action_cookie,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003271 no_cck, noack, offchanok,
3272 csa_offs, csa_offs_len);
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07003273 }
Dmitry Shmidtb638fe72012-03-20 12:51:25 -07003274
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003275 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
3276 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) {
3277 /*
3278 * Only one of the authentication frame types is encrypted.
3279 * In order for static WEP encryption to work properly (i.e.,
3280 * to not encrypt the frame), we need to tell mac80211 about
3281 * the frames that must not be encrypted.
3282 */
3283 u16 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
3284 u16 auth_trans = le_to_host16(mgmt->u.auth.auth_transaction);
3285 if (auth_alg != WLAN_AUTH_SHARED_KEY || auth_trans != 3)
3286 encrypt = 0;
3287 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003288
Dmitry Shmidt56052862013-10-04 10:23:25 -07003289 wpa_printf(MSG_DEBUG, "nl80211: send_mlme -> send_frame");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003290 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003291 noack, freq, no_cck, offchanok,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003292 wait_time, csa_offs,
3293 csa_offs_len);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003294}
3295
3296
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003297static int nl80211_put_basic_rates(struct nl_msg *msg, const int *basic_rates)
3298{
3299 u8 rates[NL80211_MAX_SUPP_RATES];
3300 u8 rates_len = 0;
3301 int i;
3302
3303 if (!basic_rates)
3304 return 0;
3305
3306 for (i = 0; i < NL80211_MAX_SUPP_RATES && basic_rates[i] >= 0; i++)
3307 rates[rates_len++] = basic_rates[i] / 5;
3308
3309 return nla_put(msg, NL80211_ATTR_BSS_BASIC_RATES, rates_len, rates);
3310}
3311
3312
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003313static int nl80211_set_bss(struct i802_bss *bss, int cts, int preamble,
3314 int slot, int ht_opmode, int ap_isolate,
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003315 const int *basic_rates)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003316{
3317 struct wpa_driver_nl80211_data *drv = bss->drv;
3318 struct nl_msg *msg;
3319
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003320 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_BSS)) ||
3321 (cts >= 0 &&
3322 nla_put_u8(msg, NL80211_ATTR_BSS_CTS_PROT, cts)) ||
3323 (preamble >= 0 &&
3324 nla_put_u8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble)) ||
3325 (slot >= 0 &&
3326 nla_put_u8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot)) ||
3327 (ht_opmode >= 0 &&
3328 nla_put_u16(msg, NL80211_ATTR_BSS_HT_OPMODE, ht_opmode)) ||
3329 (ap_isolate >= 0 &&
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003330 nla_put_u8(msg, NL80211_ATTR_AP_ISOLATE, ap_isolate)) ||
3331 nl80211_put_basic_rates(msg, basic_rates)) {
3332 nlmsg_free(msg);
3333 return -ENOBUFS;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003334 }
3335
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003336 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003337}
3338
3339
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003340static int wpa_driver_nl80211_set_acl(void *priv,
3341 struct hostapd_acl_params *params)
3342{
3343 struct i802_bss *bss = priv;
3344 struct wpa_driver_nl80211_data *drv = bss->drv;
3345 struct nl_msg *msg;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003346 struct nl_msg *acl;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003347 unsigned int i;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003348 int ret;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003349
3350 if (!(drv->capa.max_acl_mac_addrs))
3351 return -ENOTSUP;
3352
3353 if (params->num_mac_acl > drv->capa.max_acl_mac_addrs)
3354 return -ENOTSUP;
3355
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003356 wpa_printf(MSG_DEBUG, "nl80211: Set %s ACL (num_mac_acl=%u)",
3357 params->acl_policy ? "Accept" : "Deny", params->num_mac_acl);
3358
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003359 acl = nlmsg_alloc();
3360 if (!acl)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003361 return -ENOMEM;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003362 for (i = 0; i < params->num_mac_acl; i++) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003363 if (nla_put(acl, i + 1, ETH_ALEN, params->mac_acl[i].addr)) {
3364 nlmsg_free(acl);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003365 return -ENOMEM;
3366 }
3367 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003368
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003369 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_MAC_ACL)) ||
3370 nla_put_u32(msg, NL80211_ATTR_ACL_POLICY, params->acl_policy ?
3371 NL80211_ACL_POLICY_DENY_UNLESS_LISTED :
3372 NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED) ||
3373 nla_put_nested(msg, NL80211_ATTR_MAC_ADDRS, acl)) {
3374 nlmsg_free(msg);
3375 nlmsg_free(acl);
3376 return -ENOMEM;
3377 }
3378 nlmsg_free(acl);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003379
3380 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003381 if (ret) {
3382 wpa_printf(MSG_DEBUG, "nl80211: Failed to set MAC ACL: %d (%s)",
3383 ret, strerror(-ret));
3384 }
3385
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003386 return ret;
3387}
3388
3389
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003390static int nl80211_put_beacon_int(struct nl_msg *msg, int beacon_int)
3391{
3392 if (beacon_int > 0) {
3393 wpa_printf(MSG_DEBUG, " * beacon_int=%d", beacon_int);
3394 return nla_put_u32(msg, NL80211_ATTR_BEACON_INTERVAL,
3395 beacon_int);
3396 }
3397
3398 return 0;
3399}
3400
3401
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003402static int wpa_driver_nl80211_set_ap(void *priv,
3403 struct wpa_driver_ap_params *params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003404{
3405 struct i802_bss *bss = priv;
3406 struct wpa_driver_nl80211_data *drv = bss->drv;
3407 struct nl_msg *msg;
3408 u8 cmd = NL80211_CMD_NEW_BEACON;
3409 int ret;
3410 int beacon_set;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003411 int num_suites;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003412 int smps_mode;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003413 u32 suites[10], suite;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003414 u32 ver;
3415
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003416 beacon_set = params->reenable ? 0 : bss->beacon_set;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003417
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003418 wpa_printf(MSG_DEBUG, "nl80211: Set beacon (beacon_set=%d)",
3419 beacon_set);
3420 if (beacon_set)
3421 cmd = NL80211_CMD_SET_BEACON;
3422
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003423 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon head",
3424 params->head, params->head_len);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003425 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon tail",
3426 params->tail, params->tail_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003427 wpa_printf(MSG_DEBUG, "nl80211: ifindex=%d", bss->ifindex);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003428 wpa_printf(MSG_DEBUG, "nl80211: beacon_int=%d", params->beacon_int);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003429 wpa_printf(MSG_DEBUG, "nl80211: dtim_period=%d", params->dtim_period);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003430 wpa_hexdump_ascii(MSG_DEBUG, "nl80211: ssid",
3431 params->ssid, params->ssid_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003432 if (!(msg = nl80211_bss_msg(bss, 0, cmd)) ||
3433 nla_put(msg, NL80211_ATTR_BEACON_HEAD, params->head_len,
3434 params->head) ||
3435 nla_put(msg, NL80211_ATTR_BEACON_TAIL, params->tail_len,
3436 params->tail) ||
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003437 nl80211_put_beacon_int(msg, params->beacon_int) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003438 nla_put_u32(msg, NL80211_ATTR_DTIM_PERIOD, params->dtim_period) ||
3439 nla_put(msg, NL80211_ATTR_SSID, params->ssid_len, params->ssid))
3440 goto fail;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003441 if (params->proberesp && params->proberesp_len) {
3442 wpa_hexdump(MSG_DEBUG, "nl80211: proberesp (offload)",
3443 params->proberesp, params->proberesp_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003444 if (nla_put(msg, NL80211_ATTR_PROBE_RESP, params->proberesp_len,
3445 params->proberesp))
3446 goto fail;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003447 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003448 switch (params->hide_ssid) {
3449 case NO_SSID_HIDING:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003450 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID not in use");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003451 if (nla_put_u32(msg, NL80211_ATTR_HIDDEN_SSID,
3452 NL80211_HIDDEN_SSID_NOT_IN_USE))
3453 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003454 break;
3455 case HIDDEN_SSID_ZERO_LEN:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003456 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero len");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003457 if (nla_put_u32(msg, NL80211_ATTR_HIDDEN_SSID,
3458 NL80211_HIDDEN_SSID_ZERO_LEN))
3459 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003460 break;
3461 case HIDDEN_SSID_ZERO_CONTENTS:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003462 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero contents");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003463 if (nla_put_u32(msg, NL80211_ATTR_HIDDEN_SSID,
3464 NL80211_HIDDEN_SSID_ZERO_CONTENTS))
3465 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003466 break;
3467 }
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003468 wpa_printf(MSG_DEBUG, "nl80211: privacy=%d", params->privacy);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003469 if (params->privacy &&
3470 nla_put_flag(msg, NL80211_ATTR_PRIVACY))
3471 goto fail;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003472 wpa_printf(MSG_DEBUG, "nl80211: auth_algs=0x%x", params->auth_algs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003473 if ((params->auth_algs & (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) ==
3474 (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) {
3475 /* Leave out the attribute */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003476 } else if (params->auth_algs & WPA_AUTH_ALG_SHARED) {
3477 if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE,
3478 NL80211_AUTHTYPE_SHARED_KEY))
3479 goto fail;
3480 } else {
3481 if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE,
3482 NL80211_AUTHTYPE_OPEN_SYSTEM))
3483 goto fail;
3484 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003485
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003486 wpa_printf(MSG_DEBUG, "nl80211: wpa_version=0x%x", params->wpa_version);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003487 ver = 0;
3488 if (params->wpa_version & WPA_PROTO_WPA)
3489 ver |= NL80211_WPA_VERSION_1;
3490 if (params->wpa_version & WPA_PROTO_RSN)
3491 ver |= NL80211_WPA_VERSION_2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003492 if (ver &&
3493 nla_put_u32(msg, NL80211_ATTR_WPA_VERSIONS, ver))
3494 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003495
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003496 wpa_printf(MSG_DEBUG, "nl80211: key_mgmt_suites=0x%x",
3497 params->key_mgmt_suites);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003498 num_suites = 0;
3499 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X)
3500 suites[num_suites++] = WLAN_AKM_SUITE_8021X;
3501 if (params->key_mgmt_suites & WPA_KEY_MGMT_PSK)
3502 suites[num_suites++] = WLAN_AKM_SUITE_PSK;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003503 if (num_suites &&
3504 nla_put(msg, NL80211_ATTR_AKM_SUITES, num_suites * sizeof(u32),
3505 suites))
3506 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003507
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003508 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
3509 params->pairwise_ciphers & (WPA_CIPHER_WEP104 | WPA_CIPHER_WEP40) &&
3510 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT))
3511 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003512
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003513 wpa_printf(MSG_DEBUG, "nl80211: pairwise_ciphers=0x%x",
3514 params->pairwise_ciphers);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003515 num_suites = wpa_cipher_to_cipher_suites(params->pairwise_ciphers,
3516 suites, ARRAY_SIZE(suites));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003517 if (num_suites &&
3518 nla_put(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
3519 num_suites * sizeof(u32), suites))
3520 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003521
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003522 wpa_printf(MSG_DEBUG, "nl80211: group_cipher=0x%x",
3523 params->group_cipher);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003524 suite = wpa_cipher_to_cipher_suite(params->group_cipher);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003525 if (suite &&
3526 nla_put_u32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, suite))
3527 goto fail;
3528
3529 switch (params->smps_mode) {
3530 case HT_CAP_INFO_SMPS_DYNAMIC:
3531 wpa_printf(MSG_DEBUG, "nl80211: SMPS mode - dynamic");
3532 smps_mode = NL80211_SMPS_DYNAMIC;
3533 break;
3534 case HT_CAP_INFO_SMPS_STATIC:
3535 wpa_printf(MSG_DEBUG, "nl80211: SMPS mode - static");
3536 smps_mode = NL80211_SMPS_STATIC;
3537 break;
3538 default:
3539 /* invalid - fallback to smps off */
3540 case HT_CAP_INFO_SMPS_DISABLED:
3541 wpa_printf(MSG_DEBUG, "nl80211: SMPS mode - off");
3542 smps_mode = NL80211_SMPS_OFF;
3543 break;
3544 }
3545 if (nla_put_u32(msg, NL80211_ATTR_SMPS_MODE, smps_mode))
3546 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003547
3548 if (params->beacon_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003549 wpa_hexdump_buf(MSG_DEBUG, "nl80211: beacon_ies",
3550 params->beacon_ies);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003551 if (nla_put(msg, NL80211_ATTR_IE,
3552 wpabuf_len(params->beacon_ies),
3553 wpabuf_head(params->beacon_ies)))
3554 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003555 }
3556 if (params->proberesp_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003557 wpa_hexdump_buf(MSG_DEBUG, "nl80211: proberesp_ies",
3558 params->proberesp_ies);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003559 if (nla_put(msg, NL80211_ATTR_IE_PROBE_RESP,
3560 wpabuf_len(params->proberesp_ies),
3561 wpabuf_head(params->proberesp_ies)))
3562 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003563 }
3564 if (params->assocresp_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003565 wpa_hexdump_buf(MSG_DEBUG, "nl80211: assocresp_ies",
3566 params->assocresp_ies);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003567 if (nla_put(msg, NL80211_ATTR_IE_ASSOC_RESP,
3568 wpabuf_len(params->assocresp_ies),
3569 wpabuf_head(params->assocresp_ies)))
3570 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003571 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003572
Dmitry Shmidt04949592012-07-19 12:16:46 -07003573 if (drv->capa.flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003574 wpa_printf(MSG_DEBUG, "nl80211: ap_max_inactivity=%d",
3575 params->ap_max_inactivity);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003576 if (nla_put_u16(msg, NL80211_ATTR_INACTIVITY_TIMEOUT,
3577 params->ap_max_inactivity))
3578 goto fail;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003579 }
3580
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003581#ifdef CONFIG_P2P
3582 if (params->p2p_go_ctwindow > 0) {
3583 if (drv->p2p_go_ctwindow_supported) {
3584 wpa_printf(MSG_DEBUG, "nl80211: P2P GO ctwindow=%d",
3585 params->p2p_go_ctwindow);
3586 if (nla_put_u8(msg, NL80211_ATTR_P2P_CTWINDOW,
3587 params->p2p_go_ctwindow))
3588 goto fail;
3589 } else {
3590 wpa_printf(MSG_INFO,
3591 "nl80211: Driver does not support CTWindow configuration - ignore this parameter");
3592 }
3593 }
3594#endif /* CONFIG_P2P */
3595
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003596 if (params->pbss) {
3597 wpa_printf(MSG_DEBUG, "nl80211: PBSS");
3598 if (nla_put_flag(msg, NL80211_ATTR_PBSS))
3599 goto fail;
3600 }
3601
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003602 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3603 if (ret) {
3604 wpa_printf(MSG_DEBUG, "nl80211: Beacon set failed: %d (%s)",
3605 ret, strerror(-ret));
3606 } else {
3607 bss->beacon_set = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003608 nl80211_set_bss(bss, params->cts_protect, params->preamble,
3609 params->short_slot_time, params->ht_opmode,
3610 params->isolate, params->basic_rates);
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07003611 if (beacon_set && params->freq &&
3612 params->freq->bandwidth != bss->bandwidth) {
3613 wpa_printf(MSG_DEBUG,
3614 "nl80211: Update BSS %s bandwidth: %d -> %d",
3615 bss->ifname, bss->bandwidth,
3616 params->freq->bandwidth);
3617 ret = nl80211_set_channel(bss, params->freq, 1);
3618 if (ret) {
3619 wpa_printf(MSG_DEBUG,
3620 "nl80211: Frequency set failed: %d (%s)",
3621 ret, strerror(-ret));
3622 } else {
3623 wpa_printf(MSG_DEBUG,
3624 "nl80211: Frequency set succeeded for ht2040 coex");
3625 bss->bandwidth = params->freq->bandwidth;
3626 }
3627 } else if (!beacon_set) {
3628 /*
3629 * cfg80211 updates the driver on frequence change in AP
3630 * mode only at the point when beaconing is started, so
3631 * set the initial value here.
3632 */
3633 bss->bandwidth = params->freq->bandwidth;
3634 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003635 }
3636 return ret;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003637fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003638 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003639 return -ENOBUFS;
3640}
3641
3642
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003643static int nl80211_put_freq_params(struct nl_msg *msg,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003644 const struct hostapd_freq_params *freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003645{
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003646 wpa_printf(MSG_DEBUG, " * freq=%d", freq->freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003647 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq->freq))
3648 return -ENOBUFS;
3649
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003650 wpa_printf(MSG_DEBUG, " * vht_enabled=%d", freq->vht_enabled);
3651 wpa_printf(MSG_DEBUG, " * ht_enabled=%d", freq->ht_enabled);
3652
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003653 if (freq->vht_enabled) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003654 enum nl80211_chan_width cw;
3655
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003656 wpa_printf(MSG_DEBUG, " * bandwidth=%d", freq->bandwidth);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003657 switch (freq->bandwidth) {
3658 case 20:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003659 cw = NL80211_CHAN_WIDTH_20;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003660 break;
3661 case 40:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003662 cw = NL80211_CHAN_WIDTH_40;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003663 break;
3664 case 80:
3665 if (freq->center_freq2)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003666 cw = NL80211_CHAN_WIDTH_80P80;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003667 else
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003668 cw = NL80211_CHAN_WIDTH_80;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003669 break;
3670 case 160:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003671 cw = NL80211_CHAN_WIDTH_160;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003672 break;
3673 default:
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003674 return -EINVAL;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003675 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003676
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003677 wpa_printf(MSG_DEBUG, " * channel_width=%d", cw);
3678 wpa_printf(MSG_DEBUG, " * center_freq1=%d",
3679 freq->center_freq1);
3680 wpa_printf(MSG_DEBUG, " * center_freq2=%d",
3681 freq->center_freq2);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003682 if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, cw) ||
3683 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1,
3684 freq->center_freq1) ||
3685 (freq->center_freq2 &&
3686 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2,
3687 freq->center_freq2)))
3688 return -ENOBUFS;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003689 } else if (freq->ht_enabled) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003690 enum nl80211_channel_type ct;
3691
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003692 wpa_printf(MSG_DEBUG, " * sec_channel_offset=%d",
3693 freq->sec_channel_offset);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003694 switch (freq->sec_channel_offset) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003695 case -1:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003696 ct = NL80211_CHAN_HT40MINUS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003697 break;
3698 case 1:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003699 ct = NL80211_CHAN_HT40PLUS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003700 break;
3701 default:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003702 ct = NL80211_CHAN_HT20;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003703 break;
3704 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003705
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003706 wpa_printf(MSG_DEBUG, " * channel_type=%d", ct);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003707 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, ct))
3708 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003709 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003710 return 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003711}
3712
3713
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07003714static int nl80211_set_channel(struct i802_bss *bss,
3715 struct hostapd_freq_params *freq, int set_chan)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003716{
3717 struct wpa_driver_nl80211_data *drv = bss->drv;
3718 struct nl_msg *msg;
3719 int ret;
3720
3721 wpa_printf(MSG_DEBUG,
3722 "nl80211: Set freq %d (ht_enabled=%d, vht_enabled=%d, bandwidth=%d MHz, cf1=%d MHz, cf2=%d MHz)",
3723 freq->freq, freq->ht_enabled, freq->vht_enabled,
3724 freq->bandwidth, freq->center_freq1, freq->center_freq2);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003725
3726 msg = nl80211_drv_msg(drv, 0, set_chan ? NL80211_CMD_SET_CHANNEL :
3727 NL80211_CMD_SET_WIPHY);
3728 if (!msg || nl80211_put_freq_params(msg, freq) < 0) {
3729 nlmsg_free(msg);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003730 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003731 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003732
3733 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003734 if (ret == 0) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003735 bss->freq = freq->freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003736 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003737 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003738 wpa_printf(MSG_DEBUG, "nl80211: Failed to set channel (freq=%d): "
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003739 "%d (%s)", freq->freq, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003740 return -1;
3741}
3742
3743
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003744static u32 sta_flags_nl80211(int flags)
3745{
3746 u32 f = 0;
3747
3748 if (flags & WPA_STA_AUTHORIZED)
3749 f |= BIT(NL80211_STA_FLAG_AUTHORIZED);
3750 if (flags & WPA_STA_WMM)
3751 f |= BIT(NL80211_STA_FLAG_WME);
3752 if (flags & WPA_STA_SHORT_PREAMBLE)
3753 f |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
3754 if (flags & WPA_STA_MFP)
3755 f |= BIT(NL80211_STA_FLAG_MFP);
3756 if (flags & WPA_STA_TDLS_PEER)
3757 f |= BIT(NL80211_STA_FLAG_TDLS_PEER);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003758 if (flags & WPA_STA_AUTHENTICATED)
3759 f |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003760 if (flags & WPA_STA_ASSOCIATED)
3761 f |= BIT(NL80211_STA_FLAG_ASSOCIATED);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003762
3763 return f;
3764}
3765
3766
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003767#ifdef CONFIG_MESH
3768static u32 sta_plink_state_nl80211(enum mesh_plink_state state)
3769{
3770 switch (state) {
3771 case PLINK_LISTEN:
3772 return NL80211_PLINK_LISTEN;
3773 case PLINK_OPEN_SENT:
3774 return NL80211_PLINK_OPN_SNT;
3775 case PLINK_OPEN_RCVD:
3776 return NL80211_PLINK_OPN_RCVD;
3777 case PLINK_CNF_RCVD:
3778 return NL80211_PLINK_CNF_RCVD;
3779 case PLINK_ESTAB:
3780 return NL80211_PLINK_ESTAB;
3781 case PLINK_HOLDING:
3782 return NL80211_PLINK_HOLDING;
3783 case PLINK_BLOCKED:
3784 return NL80211_PLINK_BLOCKED;
3785 default:
3786 wpa_printf(MSG_ERROR, "nl80211: Invalid mesh plink state %d",
3787 state);
3788 }
3789 return -1;
3790}
3791#endif /* CONFIG_MESH */
3792
3793
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003794static int wpa_driver_nl80211_sta_add(void *priv,
3795 struct hostapd_sta_add_params *params)
3796{
3797 struct i802_bss *bss = priv;
3798 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003799 struct nl_msg *msg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003800 struct nl80211_sta_flag_update upd;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003801 int ret = -ENOBUFS;
3802
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003803 if ((params->flags & WPA_STA_TDLS_PEER) &&
3804 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
3805 return -EOPNOTSUPP;
3806
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003807 wpa_printf(MSG_DEBUG, "nl80211: %s STA " MACSTR,
3808 params->set ? "Set" : "Add", MAC2STR(params->addr));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003809 msg = nl80211_bss_msg(bss, 0, params->set ? NL80211_CMD_SET_STATION :
3810 NL80211_CMD_NEW_STATION);
3811 if (!msg || nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr))
3812 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003813
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003814 /*
3815 * Set the below properties only in one of the following cases:
3816 * 1. New station is added, already associated.
3817 * 2. Set WPA_STA_TDLS_PEER station.
3818 * 3. Set an already added unassociated station, if driver supports
3819 * full AP client state. (Set these properties after station became
3820 * associated will be rejected by the driver).
3821 */
3822 if (!params->set || (params->flags & WPA_STA_TDLS_PEER) ||
3823 (params->set && FULL_AP_CLIENT_STATE_SUPP(drv->capa.flags) &&
3824 (params->flags & WPA_STA_ASSOCIATED))) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003825 wpa_hexdump(MSG_DEBUG, " * supported rates",
3826 params->supp_rates, params->supp_rates_len);
3827 wpa_printf(MSG_DEBUG, " * capability=0x%x",
3828 params->capability);
3829 if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_RATES,
3830 params->supp_rates_len, params->supp_rates) ||
3831 nla_put_u16(msg, NL80211_ATTR_STA_CAPABILITY,
3832 params->capability))
3833 goto fail;
3834
3835 if (params->ht_capabilities) {
3836 wpa_hexdump(MSG_DEBUG, " * ht_capabilities",
3837 (u8 *) params->ht_capabilities,
3838 sizeof(*params->ht_capabilities));
3839 if (nla_put(msg, NL80211_ATTR_HT_CAPABILITY,
3840 sizeof(*params->ht_capabilities),
3841 params->ht_capabilities))
3842 goto fail;
3843 }
3844
3845 if (params->vht_capabilities) {
3846 wpa_hexdump(MSG_DEBUG, " * vht_capabilities",
3847 (u8 *) params->vht_capabilities,
3848 sizeof(*params->vht_capabilities));
3849 if (nla_put(msg, NL80211_ATTR_VHT_CAPABILITY,
3850 sizeof(*params->vht_capabilities),
3851 params->vht_capabilities))
3852 goto fail;
3853 }
3854
3855 if (params->ext_capab) {
3856 wpa_hexdump(MSG_DEBUG, " * ext_capab",
3857 params->ext_capab, params->ext_capab_len);
3858 if (nla_put(msg, NL80211_ATTR_STA_EXT_CAPABILITY,
3859 params->ext_capab_len, params->ext_capab))
3860 goto fail;
3861 }
3862 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003863 if (!params->set) {
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07003864 if (params->aid) {
3865 wpa_printf(MSG_DEBUG, " * aid=%u", params->aid);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003866 if (nla_put_u16(msg, NL80211_ATTR_STA_AID, params->aid))
3867 goto fail;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07003868 } else {
3869 /*
3870 * cfg80211 validates that AID is non-zero, so we have
3871 * to make this a non-zero value for the TDLS case where
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003872 * a dummy STA entry is used for now and for a station
3873 * that is still not associated.
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07003874 */
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003875 wpa_printf(MSG_DEBUG, " * aid=1 (%s workaround)",
3876 (params->flags & WPA_STA_TDLS_PEER) ?
3877 "TDLS" : "UNASSOC_STA");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003878 if (nla_put_u16(msg, NL80211_ATTR_STA_AID, 1))
3879 goto fail;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07003880 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003881 wpa_printf(MSG_DEBUG, " * listen_interval=%u",
3882 params->listen_interval);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003883 if (nla_put_u16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
3884 params->listen_interval))
3885 goto fail;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003886 } else if (params->aid && (params->flags & WPA_STA_TDLS_PEER)) {
3887 wpa_printf(MSG_DEBUG, " * peer_aid=%u", params->aid);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003888 if (nla_put_u16(msg, NL80211_ATTR_PEER_AID, params->aid))
3889 goto fail;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003890 } else if (FULL_AP_CLIENT_STATE_SUPP(drv->capa.flags) &&
3891 (params->flags & WPA_STA_ASSOCIATED)) {
3892 wpa_printf(MSG_DEBUG, " * aid=%u", params->aid);
3893 wpa_printf(MSG_DEBUG, " * listen_interval=%u",
3894 params->listen_interval);
3895 if (nla_put_u16(msg, NL80211_ATTR_STA_AID, params->aid) ||
3896 nla_put_u16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
3897 params->listen_interval))
3898 goto fail;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003899 }
3900
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08003901 if (params->vht_opmode_enabled) {
3902 wpa_printf(MSG_DEBUG, " * opmode=%u", params->vht_opmode);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003903 if (nla_put_u8(msg, NL80211_ATTR_OPMODE_NOTIF,
3904 params->vht_opmode))
3905 goto fail;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003906 }
3907
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003908 if (params->supp_channels) {
3909 wpa_hexdump(MSG_DEBUG, " * supported channels",
3910 params->supp_channels, params->supp_channels_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003911 if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_CHANNELS,
3912 params->supp_channels_len, params->supp_channels))
3913 goto fail;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003914 }
3915
3916 if (params->supp_oper_classes) {
3917 wpa_hexdump(MSG_DEBUG, " * supported operating classes",
3918 params->supp_oper_classes,
3919 params->supp_oper_classes_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003920 if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES,
3921 params->supp_oper_classes_len,
3922 params->supp_oper_classes))
3923 goto fail;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003924 }
3925
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003926 os_memset(&upd, 0, sizeof(upd));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003927 upd.set = sta_flags_nl80211(params->flags);
3928 upd.mask = upd.set | sta_flags_nl80211(params->flags_mask);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003929
3930 /*
3931 * If the driver doesn't support full AP client state, ignore ASSOC/AUTH
3932 * flags, as nl80211 driver moves a new station, by default, into
3933 * associated state.
3934 *
3935 * On the other hand, if the driver supports that feature and the
3936 * station is added in unauthenticated state, set the
3937 * authenticated/associated bits in the mask to prevent moving this
3938 * station to associated state before it is actually associated.
3939 *
3940 * This is irrelevant for mesh mode where the station is added to the
3941 * driver as authenticated already, and ASSOCIATED isn't part of the
3942 * nl80211 API.
3943 */
3944 if (!is_mesh_interface(drv->nlmode)) {
3945 if (!FULL_AP_CLIENT_STATE_SUPP(drv->capa.flags)) {
3946 wpa_printf(MSG_DEBUG,
3947 "nl80211: Ignore ASSOC/AUTH flags since driver doesn't support full AP client state");
3948 upd.mask &= ~(BIT(NL80211_STA_FLAG_ASSOCIATED) |
3949 BIT(NL80211_STA_FLAG_AUTHENTICATED));
3950 } else if (!params->set &&
3951 !(params->flags & WPA_STA_TDLS_PEER)) {
3952 if (!(params->flags & WPA_STA_AUTHENTICATED))
3953 upd.mask |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
3954 if (!(params->flags & WPA_STA_ASSOCIATED))
3955 upd.mask |= BIT(NL80211_STA_FLAG_ASSOCIATED);
3956 }
3957 }
3958
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003959 wpa_printf(MSG_DEBUG, " * flags set=0x%x mask=0x%x",
3960 upd.set, upd.mask);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003961 if (nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd))
3962 goto fail;
3963
3964#ifdef CONFIG_MESH
3965 if (params->plink_state &&
3966 nla_put_u8(msg, NL80211_ATTR_STA_PLINK_STATE,
3967 sta_plink_state_nl80211(params->plink_state)))
3968 goto fail;
3969#endif /* CONFIG_MESH */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003970
3971 if (params->flags & WPA_STA_WMM) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003972 struct nlattr *wme = nla_nest_start(msg, NL80211_ATTR_STA_WME);
3973
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003974 wpa_printf(MSG_DEBUG, " * qosinfo=0x%x", params->qosinfo);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003975 if (!wme ||
3976 nla_put_u8(msg, NL80211_STA_WME_UAPSD_QUEUES,
3977 params->qosinfo & WMM_QOSINFO_STA_AC_MASK) ||
3978 nla_put_u8(msg, NL80211_STA_WME_MAX_SP,
3979 (params->qosinfo >> WMM_QOSINFO_STA_SP_SHIFT) &
3980 WMM_QOSINFO_STA_SP_MASK))
3981 goto fail;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003982 nla_nest_end(msg, wme);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003983 }
3984
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003985 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003986 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003987 if (ret)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003988 wpa_printf(MSG_DEBUG, "nl80211: NL80211_CMD_%s_STATION "
3989 "result: %d (%s)", params->set ? "SET" : "NEW", ret,
3990 strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003991 if (ret == -EEXIST)
3992 ret = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003993fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003994 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003995 return ret;
3996}
3997
3998
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003999static void rtnl_neigh_delete_fdb_entry(struct i802_bss *bss, const u8 *addr)
4000{
4001#ifdef CONFIG_LIBNL3_ROUTE
4002 struct wpa_driver_nl80211_data *drv = bss->drv;
4003 struct rtnl_neigh *rn;
4004 struct nl_addr *nl_addr;
4005 int err;
4006
4007 rn = rtnl_neigh_alloc();
4008 if (!rn)
4009 return;
4010
4011 rtnl_neigh_set_family(rn, AF_BRIDGE);
4012 rtnl_neigh_set_ifindex(rn, bss->ifindex);
4013 nl_addr = nl_addr_build(AF_BRIDGE, (void *) addr, ETH_ALEN);
4014 if (!nl_addr) {
4015 rtnl_neigh_put(rn);
4016 return;
4017 }
4018 rtnl_neigh_set_lladdr(rn, nl_addr);
4019
4020 err = rtnl_neigh_delete(drv->rtnl_sk, rn, 0);
4021 if (err < 0) {
4022 wpa_printf(MSG_DEBUG, "nl80211: bridge FDB entry delete for "
4023 MACSTR " ifindex=%d failed: %s", MAC2STR(addr),
4024 bss->ifindex, nl_geterror(err));
4025 } else {
4026 wpa_printf(MSG_DEBUG, "nl80211: deleted bridge FDB entry for "
4027 MACSTR, MAC2STR(addr));
4028 }
4029
4030 nl_addr_put(nl_addr);
4031 rtnl_neigh_put(rn);
4032#endif /* CONFIG_LIBNL3_ROUTE */
4033}
4034
4035
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004036static int wpa_driver_nl80211_sta_remove(struct i802_bss *bss, const u8 *addr,
4037 int deauth, u16 reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004038{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004039 struct wpa_driver_nl80211_data *drv = bss->drv;
4040 struct nl_msg *msg;
4041 int ret;
4042
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004043 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_DEL_STATION)) ||
4044 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
4045 (deauth == 0 &&
4046 nla_put_u8(msg, NL80211_ATTR_MGMT_SUBTYPE,
4047 WLAN_FC_STYPE_DISASSOC)) ||
4048 (deauth == 1 &&
4049 nla_put_u8(msg, NL80211_ATTR_MGMT_SUBTYPE,
4050 WLAN_FC_STYPE_DEAUTH)) ||
4051 (reason_code &&
4052 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code))) {
4053 nlmsg_free(msg);
4054 return -ENOBUFS;
4055 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004056
4057 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004058 wpa_printf(MSG_DEBUG, "nl80211: sta_remove -> DEL_STATION %s " MACSTR
4059 " --> %d (%s)",
4060 bss->ifname, MAC2STR(addr), ret, strerror(-ret));
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004061
4062 if (drv->rtnl_sk)
4063 rtnl_neigh_delete_fdb_entry(bss, addr);
4064
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004065 if (ret == -ENOENT)
4066 return 0;
4067 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004068}
4069
4070
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004071void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv, int ifidx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004072{
4073 struct nl_msg *msg;
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07004074 struct wpa_driver_nl80211_data *drv2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004075
4076 wpa_printf(MSG_DEBUG, "nl80211: Remove interface ifindex=%d", ifidx);
4077
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004078 /* stop listening for EAPOL on this interface */
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07004079 dl_list_for_each(drv2, &drv->global->interfaces,
4080 struct wpa_driver_nl80211_data, list)
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004081 {
4082 del_ifidx(drv2, ifidx, IFIDX_ANY);
4083 /* Remove all bridges learned for this iface */
4084 del_ifidx(drv2, IFIDX_ANY, ifidx);
4085 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004086
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004087 msg = nl80211_ifindex_msg(drv, ifidx, 0, NL80211_CMD_DEL_INTERFACE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004088 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
4089 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004090 wpa_printf(MSG_ERROR, "Failed to remove interface (ifidx=%d)", ifidx);
4091}
4092
4093
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004094static const char * nl80211_iftype_str(enum nl80211_iftype mode)
4095{
4096 switch (mode) {
4097 case NL80211_IFTYPE_ADHOC:
4098 return "ADHOC";
4099 case NL80211_IFTYPE_STATION:
4100 return "STATION";
4101 case NL80211_IFTYPE_AP:
4102 return "AP";
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07004103 case NL80211_IFTYPE_AP_VLAN:
4104 return "AP_VLAN";
4105 case NL80211_IFTYPE_WDS:
4106 return "WDS";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004107 case NL80211_IFTYPE_MONITOR:
4108 return "MONITOR";
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07004109 case NL80211_IFTYPE_MESH_POINT:
4110 return "MESH_POINT";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004111 case NL80211_IFTYPE_P2P_CLIENT:
4112 return "P2P_CLIENT";
4113 case NL80211_IFTYPE_P2P_GO:
4114 return "P2P_GO";
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004115 case NL80211_IFTYPE_P2P_DEVICE:
4116 return "P2P_DEVICE";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004117 default:
4118 return "unknown";
4119 }
4120}
4121
4122
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004123static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv,
4124 const char *ifname,
4125 enum nl80211_iftype iftype,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004126 const u8 *addr, int wds,
4127 int (*handler)(struct nl_msg *, void *),
4128 void *arg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004129{
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004130 struct nl_msg *msg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004131 int ifidx;
4132 int ret = -ENOBUFS;
4133
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004134 wpa_printf(MSG_DEBUG, "nl80211: Create interface iftype %d (%s)",
4135 iftype, nl80211_iftype_str(iftype));
4136
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004137 msg = nl80211_cmd_msg(drv->first_bss, 0, NL80211_CMD_NEW_INTERFACE);
4138 if (!msg ||
4139 nla_put_string(msg, NL80211_ATTR_IFNAME, ifname) ||
4140 nla_put_u32(msg, NL80211_ATTR_IFTYPE, iftype))
4141 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004142
4143 if (iftype == NL80211_IFTYPE_MONITOR) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004144 struct nlattr *flags;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004145
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004146 flags = nla_nest_start(msg, NL80211_ATTR_MNTR_FLAGS);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004147 if (!flags ||
4148 nla_put_flag(msg, NL80211_MNTR_FLAG_COOK_FRAMES))
4149 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004150
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004151 nla_nest_end(msg, flags);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004152 } else if (wds) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004153 if (nla_put_u8(msg, NL80211_ATTR_4ADDR, wds))
4154 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004155 }
4156
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07004157 /*
4158 * Tell cfg80211 that the interface belongs to the socket that created
4159 * it, and the interface should be deleted when the socket is closed.
4160 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004161 if (nla_put_flag(msg, NL80211_ATTR_IFACE_SOCKET_OWNER))
4162 goto fail;
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07004163
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004164 ret = send_and_recv_msgs(drv, msg, handler, arg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004165 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004166 if (ret) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004167 fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004168 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004169 wpa_printf(MSG_ERROR, "Failed to create interface %s: %d (%s)",
4170 ifname, ret, strerror(-ret));
4171 return ret;
4172 }
4173
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004174 if (iftype == NL80211_IFTYPE_P2P_DEVICE)
4175 return 0;
4176
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004177 ifidx = if_nametoindex(ifname);
4178 wpa_printf(MSG_DEBUG, "nl80211: New interface %s created: ifindex=%d",
4179 ifname, ifidx);
4180
4181 if (ifidx <= 0)
4182 return -1;
4183
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07004184 /*
4185 * Some virtual interfaces need to process EAPOL packets and events on
4186 * the parent interface. This is used mainly with hostapd.
4187 */
4188 if (drv->hostapd ||
4189 iftype == NL80211_IFTYPE_AP_VLAN ||
4190 iftype == NL80211_IFTYPE_WDS ||
4191 iftype == NL80211_IFTYPE_MONITOR) {
4192 /* start listening for EAPOL on this interface */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004193 add_ifidx(drv, ifidx, IFIDX_ANY);
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07004194 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004195
4196 if (addr && iftype != NL80211_IFTYPE_MONITOR &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004197 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname, addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004198 nl80211_remove_iface(drv, ifidx);
4199 return -1;
4200 }
4201
4202 return ifidx;
4203}
4204
4205
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004206int nl80211_create_iface(struct wpa_driver_nl80211_data *drv,
4207 const char *ifname, enum nl80211_iftype iftype,
4208 const u8 *addr, int wds,
4209 int (*handler)(struct nl_msg *, void *),
4210 void *arg, int use_existing)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004211{
4212 int ret;
4213
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004214 ret = nl80211_create_iface_once(drv, ifname, iftype, addr, wds, handler,
4215 arg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004216
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004217 /* if error occurred and interface exists already */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004218 if (ret == -ENFILE && if_nametoindex(ifname)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004219 if (use_existing) {
4220 wpa_printf(MSG_DEBUG, "nl80211: Continue using existing interface %s",
4221 ifname);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07004222 if (addr && iftype != NL80211_IFTYPE_MONITOR &&
4223 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
4224 addr) < 0 &&
4225 (linux_set_iface_flags(drv->global->ioctl_sock,
4226 ifname, 0) < 0 ||
4227 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
4228 addr) < 0 ||
4229 linux_set_iface_flags(drv->global->ioctl_sock,
4230 ifname, 1) < 0))
4231 return -1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004232 return -ENFILE;
4233 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004234 wpa_printf(MSG_INFO, "Try to remove and re-create %s", ifname);
4235
4236 /* Try to remove the interface that was already there. */
4237 nl80211_remove_iface(drv, if_nametoindex(ifname));
4238
4239 /* Try to create the interface again */
4240 ret = nl80211_create_iface_once(drv, ifname, iftype, addr,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004241 wds, handler, arg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004242 }
4243
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004244 if (ret >= 0 && is_p2p_net_interface(iftype)) {
4245 wpa_printf(MSG_DEBUG,
4246 "nl80211: Interface %s created for P2P - disable 11b rates",
4247 ifname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004248 nl80211_disable_11b_rates(drv, ret, 1);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004249 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004250
4251 return ret;
4252}
4253
4254
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004255static int nl80211_setup_ap(struct i802_bss *bss)
4256{
4257 struct wpa_driver_nl80211_data *drv = bss->drv;
4258
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004259 wpa_printf(MSG_DEBUG, "nl80211: Setup AP(%s) - device_ap_sme=%d use_monitor=%d",
4260 bss->ifname, drv->device_ap_sme, drv->use_monitor);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004261
4262 /*
4263 * Disable Probe Request reporting unless we need it in this way for
4264 * devices that include the AP SME, in the other case (unless using
4265 * monitor iface) we'll get it through the nl_mgmt socket instead.
4266 */
4267 if (!drv->device_ap_sme)
4268 wpa_driver_nl80211_probe_req_report(bss, 0);
4269
4270 if (!drv->device_ap_sme && !drv->use_monitor)
4271 if (nl80211_mgmt_subscribe_ap(bss))
4272 return -1;
4273
4274 if (drv->device_ap_sme && !drv->use_monitor)
4275 if (nl80211_mgmt_subscribe_ap_dev_sme(bss))
4276 return -1;
4277
4278 if (!drv->device_ap_sme && drv->use_monitor &&
4279 nl80211_create_monitor_interface(drv) &&
4280 !drv->device_ap_sme)
Dmitry Shmidt04949592012-07-19 12:16:46 -07004281 return -1;
4282
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004283 if (drv->device_ap_sme &&
4284 wpa_driver_nl80211_probe_req_report(bss, 1) < 0) {
4285 wpa_printf(MSG_DEBUG, "nl80211: Failed to enable "
4286 "Probe Request frame reporting in AP mode");
4287 /* Try to survive without this */
4288 }
4289
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004290 return 0;
4291}
4292
4293
4294static void nl80211_teardown_ap(struct i802_bss *bss)
4295{
4296 struct wpa_driver_nl80211_data *drv = bss->drv;
4297
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004298 wpa_printf(MSG_DEBUG, "nl80211: Teardown AP(%s) - device_ap_sme=%d use_monitor=%d",
4299 bss->ifname, drv->device_ap_sme, drv->use_monitor);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004300 if (drv->device_ap_sme) {
4301 wpa_driver_nl80211_probe_req_report(bss, 0);
4302 if (!drv->use_monitor)
4303 nl80211_mgmt_unsubscribe(bss, "AP teardown (dev SME)");
4304 } else if (drv->use_monitor)
4305 nl80211_remove_monitor_interface(drv);
4306 else
4307 nl80211_mgmt_unsubscribe(bss, "AP teardown");
4308
4309 bss->beacon_set = 0;
4310}
4311
4312
4313static int nl80211_send_eapol_data(struct i802_bss *bss,
4314 const u8 *addr, const u8 *data,
4315 size_t data_len)
4316{
4317 struct sockaddr_ll ll;
4318 int ret;
4319
4320 if (bss->drv->eapol_tx_sock < 0) {
4321 wpa_printf(MSG_DEBUG, "nl80211: No socket to send EAPOL");
4322 return -1;
4323 }
4324
4325 os_memset(&ll, 0, sizeof(ll));
4326 ll.sll_family = AF_PACKET;
4327 ll.sll_ifindex = bss->ifindex;
4328 ll.sll_protocol = htons(ETH_P_PAE);
4329 ll.sll_halen = ETH_ALEN;
4330 os_memcpy(ll.sll_addr, addr, ETH_ALEN);
4331 ret = sendto(bss->drv->eapol_tx_sock, data, data_len, 0,
4332 (struct sockaddr *) &ll, sizeof(ll));
4333 if (ret < 0)
4334 wpa_printf(MSG_ERROR, "nl80211: EAPOL TX: %s",
4335 strerror(errno));
4336
4337 return ret;
4338}
4339
4340
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004341static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
4342
4343static int wpa_driver_nl80211_hapd_send_eapol(
4344 void *priv, const u8 *addr, const u8 *data,
4345 size_t data_len, int encrypt, const u8 *own_addr, u32 flags)
4346{
4347 struct i802_bss *bss = priv;
4348 struct wpa_driver_nl80211_data *drv = bss->drv;
4349 struct ieee80211_hdr *hdr;
4350 size_t len;
4351 u8 *pos;
4352 int res;
4353 int qos = flags & WPA_STA_WMM;
Dmitry Shmidt641185e2013-11-06 15:17:13 -08004354
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004355 if (drv->device_ap_sme || !drv->use_monitor)
4356 return nl80211_send_eapol_data(bss, addr, data, data_len);
4357
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004358 len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
4359 data_len;
4360 hdr = os_zalloc(len);
4361 if (hdr == NULL) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004362 wpa_printf(MSG_INFO, "nl80211: Failed to allocate EAPOL buffer(len=%lu)",
4363 (unsigned long) len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004364 return -1;
4365 }
4366
4367 hdr->frame_control =
4368 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
4369 hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
4370 if (encrypt)
4371 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
4372 if (qos) {
4373 hdr->frame_control |=
4374 host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
4375 }
4376
4377 memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
4378 memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
4379 memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
4380 pos = (u8 *) (hdr + 1);
4381
4382 if (qos) {
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004383 /* Set highest priority in QoS header */
4384 pos[0] = 7;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004385 pos[1] = 0;
4386 pos += 2;
4387 }
4388
4389 memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
4390 pos += sizeof(rfc1042_header);
4391 WPA_PUT_BE16(pos, ETH_P_PAE);
4392 pos += 2;
4393 memcpy(pos, data, data_len);
4394
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004395 res = wpa_driver_nl80211_send_frame(bss, (u8 *) hdr, len, encrypt, 0,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004396 0, 0, 0, 0, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004397 if (res < 0) {
4398 wpa_printf(MSG_ERROR, "i802_send_eapol - packet len: %lu - "
4399 "failed: %d (%s)",
4400 (unsigned long) len, errno, strerror(errno));
4401 }
4402 os_free(hdr);
4403
4404 return res;
4405}
4406
4407
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004408static int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004409 unsigned int total_flags,
4410 unsigned int flags_or,
4411 unsigned int flags_and)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004412{
4413 struct i802_bss *bss = priv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004414 struct nl_msg *msg;
4415 struct nlattr *flags;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004416 struct nl80211_sta_flag_update upd;
4417
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004418 wpa_printf(MSG_DEBUG, "nl80211: Set STA flags - ifname=%s addr=" MACSTR
4419 " total_flags=0x%x flags_or=0x%x flags_and=0x%x authorized=%d",
4420 bss->ifname, MAC2STR(addr), total_flags, flags_or, flags_and,
4421 !!(total_flags & WPA_STA_AUTHORIZED));
4422
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004423 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
4424 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
4425 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004426
4427 /*
4428 * Backwards compatibility version using NL80211_ATTR_STA_FLAGS. This
4429 * can be removed eventually.
4430 */
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004431 flags = nla_nest_start(msg, NL80211_ATTR_STA_FLAGS);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004432 if (!flags ||
4433 ((total_flags & WPA_STA_AUTHORIZED) &&
4434 nla_put_flag(msg, NL80211_STA_FLAG_AUTHORIZED)) ||
4435 ((total_flags & WPA_STA_WMM) &&
4436 nla_put_flag(msg, NL80211_STA_FLAG_WME)) ||
4437 ((total_flags & WPA_STA_SHORT_PREAMBLE) &&
4438 nla_put_flag(msg, NL80211_STA_FLAG_SHORT_PREAMBLE)) ||
4439 ((total_flags & WPA_STA_MFP) &&
4440 nla_put_flag(msg, NL80211_STA_FLAG_MFP)) ||
4441 ((total_flags & WPA_STA_TDLS_PEER) &&
4442 nla_put_flag(msg, NL80211_STA_FLAG_TDLS_PEER)))
4443 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004444
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004445 nla_nest_end(msg, flags);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004446
4447 os_memset(&upd, 0, sizeof(upd));
4448 upd.mask = sta_flags_nl80211(flags_or | ~flags_and);
4449 upd.set = sta_flags_nl80211(flags_or);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004450 if (nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd))
4451 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004452
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004453 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
4454fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004455 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004456 return -ENOBUFS;
4457}
4458
4459
4460static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv,
4461 struct wpa_driver_associate_params *params)
4462{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004463 enum nl80211_iftype nlmode, old_mode;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004464
4465 if (params->p2p) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004466 wpa_printf(MSG_DEBUG, "nl80211: Setup AP operations for P2P "
4467 "group (GO)");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004468 nlmode = NL80211_IFTYPE_P2P_GO;
4469 } else
4470 nlmode = NL80211_IFTYPE_AP;
4471
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004472 old_mode = drv->nlmode;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004473 if (wpa_driver_nl80211_set_mode(drv->first_bss, nlmode)) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004474 nl80211_remove_monitor_interface(drv);
4475 return -1;
4476 }
4477
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08004478 if (params->freq.freq &&
4479 nl80211_set_channel(drv->first_bss, &params->freq, 0)) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004480 if (old_mode != nlmode)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004481 wpa_driver_nl80211_set_mode(drv->first_bss, old_mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004482 nl80211_remove_monitor_interface(drv);
4483 return -1;
4484 }
4485
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004486 return 0;
4487}
4488
4489
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004490static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv,
4491 int reset_mode)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004492{
4493 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004494 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004495
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004496 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_LEAVE_IBSS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004497 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004498 if (ret) {
4499 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS failed: ret=%d "
4500 "(%s)", ret, strerror(-ret));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004501 } else {
4502 wpa_printf(MSG_DEBUG,
4503 "nl80211: Leave IBSS request sent successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004504 }
4505
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004506 if (reset_mode &&
4507 wpa_driver_nl80211_set_mode(drv->first_bss,
Dmitry Shmidt56052862013-10-04 10:23:25 -07004508 NL80211_IFTYPE_STATION)) {
4509 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
4510 "station mode");
4511 }
4512
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004513 return ret;
4514}
4515
4516
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08004517static int nl80211_ht_vht_overrides(struct nl_msg *msg,
4518 struct wpa_driver_associate_params *params)
4519{
4520 if (params->disable_ht && nla_put_flag(msg, NL80211_ATTR_DISABLE_HT))
4521 return -1;
4522
4523 if (params->htcaps && params->htcaps_mask) {
4524 int sz = sizeof(struct ieee80211_ht_capabilities);
4525 wpa_hexdump(MSG_DEBUG, " * htcaps", params->htcaps, sz);
4526 wpa_hexdump(MSG_DEBUG, " * htcaps_mask",
4527 params->htcaps_mask, sz);
4528 if (nla_put(msg, NL80211_ATTR_HT_CAPABILITY, sz,
4529 params->htcaps) ||
4530 nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK, sz,
4531 params->htcaps_mask))
4532 return -1;
4533 }
4534
4535#ifdef CONFIG_VHT_OVERRIDES
4536 if (params->disable_vht) {
4537 wpa_printf(MSG_DEBUG, " * VHT disabled");
4538 if (nla_put_flag(msg, NL80211_ATTR_DISABLE_VHT))
4539 return -1;
4540 }
4541
4542 if (params->vhtcaps && params->vhtcaps_mask) {
4543 int sz = sizeof(struct ieee80211_vht_capabilities);
4544 wpa_hexdump(MSG_DEBUG, " * vhtcaps", params->vhtcaps, sz);
4545 wpa_hexdump(MSG_DEBUG, " * vhtcaps_mask",
4546 params->vhtcaps_mask, sz);
4547 if (nla_put(msg, NL80211_ATTR_VHT_CAPABILITY, sz,
4548 params->vhtcaps) ||
4549 nla_put(msg, NL80211_ATTR_VHT_CAPABILITY_MASK, sz,
4550 params->vhtcaps_mask))
4551 return -1;
4552 }
4553#endif /* CONFIG_VHT_OVERRIDES */
4554
4555 return 0;
4556}
4557
4558
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004559static int wpa_driver_nl80211_ibss(struct wpa_driver_nl80211_data *drv,
4560 struct wpa_driver_associate_params *params)
4561{
4562 struct nl_msg *msg;
4563 int ret = -1;
4564 int count = 0;
4565
4566 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS (ifindex=%d)", drv->ifindex);
4567
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004568 if (wpa_driver_nl80211_set_mode_ibss(drv->first_bss, &params->freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004569 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
4570 "IBSS mode");
4571 return -1;
4572 }
4573
4574retry:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004575 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_JOIN_IBSS)) ||
4576 params->ssid == NULL || params->ssid_len > sizeof(drv->ssid))
4577 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004578
4579 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
4580 params->ssid, params->ssid_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004581 if (nla_put(msg, NL80211_ATTR_SSID, params->ssid_len, params->ssid))
4582 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004583 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
4584 drv->ssid_len = params->ssid_len;
4585
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004586 if (nl80211_put_freq_params(msg, &params->freq) < 0 ||
4587 nl80211_put_beacon_int(msg, params->beacon_int))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004588 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004589
4590 ret = nl80211_set_conn_keys(params, msg);
4591 if (ret)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004592 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004593
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004594 if (params->bssid && params->fixed_bssid) {
4595 wpa_printf(MSG_DEBUG, " * BSSID=" MACSTR,
4596 MAC2STR(params->bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004597 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid))
4598 goto fail;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004599 }
4600
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004601 if (params->fixed_freq) {
4602 wpa_printf(MSG_DEBUG, " * fixed_freq");
4603 if (nla_put_flag(msg, NL80211_ATTR_FREQ_FIXED))
4604 goto fail;
4605 }
4606
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004607 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
4608 params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
4609 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
4610 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004611 wpa_printf(MSG_DEBUG, " * control port");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004612 if (nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT))
4613 goto fail;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004614 }
4615
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004616 if (params->wpa_ie) {
4617 wpa_hexdump(MSG_DEBUG,
4618 " * Extra IEs for Beacon/Probe Response frames",
4619 params->wpa_ie, params->wpa_ie_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004620 if (nla_put(msg, NL80211_ATTR_IE, params->wpa_ie_len,
4621 params->wpa_ie))
4622 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004623 }
4624
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08004625 ret = nl80211_ht_vht_overrides(msg, params);
4626 if (ret < 0)
4627 goto fail;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08004628
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004629 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4630 msg = NULL;
4631 if (ret) {
4632 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS failed: ret=%d (%s)",
4633 ret, strerror(-ret));
4634 count++;
4635 if (ret == -EALREADY && count == 1) {
4636 wpa_printf(MSG_DEBUG, "nl80211: Retry IBSS join after "
4637 "forced leave");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004638 nl80211_leave_ibss(drv, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004639 nlmsg_free(msg);
4640 goto retry;
4641 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004642 } else {
4643 wpa_printf(MSG_DEBUG,
4644 "nl80211: Join IBSS request sent successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004645 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004646
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004647fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004648 nlmsg_free(msg);
4649 return ret;
4650}
4651
4652
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004653static int nl80211_connect_common(struct wpa_driver_nl80211_data *drv,
4654 struct wpa_driver_associate_params *params,
4655 struct nl_msg *msg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004656{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004657 if (params->bssid) {
4658 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
4659 MAC2STR(params->bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004660 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid))
4661 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004662 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004663
Dmitry Shmidt96be6222014-02-13 10:16:51 -08004664 if (params->bssid_hint) {
4665 wpa_printf(MSG_DEBUG, " * bssid_hint=" MACSTR,
4666 MAC2STR(params->bssid_hint));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004667 if (nla_put(msg, NL80211_ATTR_MAC_HINT, ETH_ALEN,
4668 params->bssid_hint))
4669 return -1;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08004670 }
4671
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004672 if (params->freq.freq) {
4673 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq.freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004674 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
4675 params->freq.freq))
4676 return -1;
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004677 drv->assoc_freq = params->freq.freq;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004678 } else
4679 drv->assoc_freq = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004680
Dmitry Shmidt96be6222014-02-13 10:16:51 -08004681 if (params->freq_hint) {
4682 wpa_printf(MSG_DEBUG, " * freq_hint=%d", params->freq_hint);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004683 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ_HINT,
4684 params->freq_hint))
4685 return -1;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08004686 }
4687
Dmitry Shmidt04949592012-07-19 12:16:46 -07004688 if (params->bg_scan_period >= 0) {
4689 wpa_printf(MSG_DEBUG, " * bg scan period=%d",
4690 params->bg_scan_period);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004691 if (nla_put_u16(msg, NL80211_ATTR_BG_SCAN_PERIOD,
4692 params->bg_scan_period))
4693 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004694 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004695
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004696 if (params->ssid) {
4697 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
4698 params->ssid, params->ssid_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004699 if (nla_put(msg, NL80211_ATTR_SSID, params->ssid_len,
4700 params->ssid))
4701 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004702 if (params->ssid_len > sizeof(drv->ssid))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004703 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004704 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
4705 drv->ssid_len = params->ssid_len;
4706 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004707
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004708 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004709 if (params->wpa_ie &&
4710 nla_put(msg, NL80211_ATTR_IE, params->wpa_ie_len, params->wpa_ie))
4711 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004712
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004713 if (params->wpa_proto) {
4714 enum nl80211_wpa_versions ver = 0;
4715
4716 if (params->wpa_proto & WPA_PROTO_WPA)
4717 ver |= NL80211_WPA_VERSION_1;
4718 if (params->wpa_proto & WPA_PROTO_RSN)
4719 ver |= NL80211_WPA_VERSION_2;
4720
4721 wpa_printf(MSG_DEBUG, " * WPA Versions 0x%x", ver);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004722 if (nla_put_u32(msg, NL80211_ATTR_WPA_VERSIONS, ver))
4723 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004724 }
4725
4726 if (params->pairwise_suite != WPA_CIPHER_NONE) {
4727 u32 cipher = wpa_cipher_to_cipher_suite(params->pairwise_suite);
4728 wpa_printf(MSG_DEBUG, " * pairwise=0x%x", cipher);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004729 if (nla_put_u32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
4730 cipher))
4731 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004732 }
4733
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08004734 if (params->group_suite == WPA_CIPHER_GTK_NOT_USED &&
4735 !(drv->capa.enc & WPA_DRIVER_CAPA_ENC_GTK_NOT_USED)) {
4736 /*
4737 * This is likely to work even though many drivers do not
4738 * advertise support for operations without GTK.
4739 */
4740 wpa_printf(MSG_DEBUG, " * skip group cipher configuration for GTK_NOT_USED due to missing driver support advertisement");
4741 } else if (params->group_suite != WPA_CIPHER_NONE) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004742 u32 cipher = wpa_cipher_to_cipher_suite(params->group_suite);
4743 wpa_printf(MSG_DEBUG, " * group=0x%x", cipher);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004744 if (nla_put_u32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher))
4745 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004746 }
4747
4748 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
4749 params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
4750 params->key_mgmt_suite == WPA_KEY_MGMT_FT_IEEE8021X ||
4751 params->key_mgmt_suite == WPA_KEY_MGMT_FT_PSK ||
Dmitry Shmidt15907092014-03-25 10:42:57 -07004752 params->key_mgmt_suite == WPA_KEY_MGMT_CCKM ||
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -07004753 params->key_mgmt_suite == WPA_KEY_MGMT_OSEN ||
4754 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004755 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256 ||
Dmitry Shmidt807291d2015-01-27 13:40:23 -08004756 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SUITE_B ||
4757 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004758 int mgmt = WLAN_AKM_SUITE_PSK;
4759
4760 switch (params->key_mgmt_suite) {
4761 case WPA_KEY_MGMT_CCKM:
4762 mgmt = WLAN_AKM_SUITE_CCKM;
4763 break;
4764 case WPA_KEY_MGMT_IEEE8021X:
4765 mgmt = WLAN_AKM_SUITE_8021X;
4766 break;
4767 case WPA_KEY_MGMT_FT_IEEE8021X:
4768 mgmt = WLAN_AKM_SUITE_FT_8021X;
4769 break;
4770 case WPA_KEY_MGMT_FT_PSK:
4771 mgmt = WLAN_AKM_SUITE_FT_PSK;
4772 break;
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -07004773 case WPA_KEY_MGMT_IEEE8021X_SHA256:
4774 mgmt = WLAN_AKM_SUITE_8021X_SHA256;
4775 break;
4776 case WPA_KEY_MGMT_PSK_SHA256:
4777 mgmt = WLAN_AKM_SUITE_PSK_SHA256;
4778 break;
Dmitry Shmidt15907092014-03-25 10:42:57 -07004779 case WPA_KEY_MGMT_OSEN:
4780 mgmt = WLAN_AKM_SUITE_OSEN;
4781 break;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004782 case WPA_KEY_MGMT_IEEE8021X_SUITE_B:
4783 mgmt = WLAN_AKM_SUITE_8021X_SUITE_B;
4784 break;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08004785 case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192:
4786 mgmt = WLAN_AKM_SUITE_8021X_SUITE_B_192;
4787 break;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004788 case WPA_KEY_MGMT_PSK:
4789 default:
4790 mgmt = WLAN_AKM_SUITE_PSK;
4791 break;
4792 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07004793 wpa_printf(MSG_DEBUG, " * akm=0x%x", mgmt);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004794 if (nla_put_u32(msg, NL80211_ATTR_AKM_SUITES, mgmt))
4795 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004796 }
4797
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004798 if (nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT))
4799 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004800
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004801 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED &&
4802 nla_put_u32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED))
4803 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004804
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004805 if (params->rrm_used) {
4806 u32 drv_rrm_flags = drv->capa.rrm_flags;
4807 if (!(drv_rrm_flags &
4808 WPA_DRIVER_FLAGS_DS_PARAM_SET_IE_IN_PROBES) ||
4809 !(drv_rrm_flags & WPA_DRIVER_FLAGS_QUIET) ||
4810 nla_put_flag(msg, NL80211_ATTR_USE_RRM))
4811 return -1;
4812 }
4813
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08004814 if (nl80211_ht_vht_overrides(msg, params) < 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004815 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004816
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004817 if (params->p2p)
4818 wpa_printf(MSG_DEBUG, " * P2P group");
4819
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004820 if (params->pbss) {
4821 wpa_printf(MSG_DEBUG, " * PBSS");
4822 if (nla_put_flag(msg, NL80211_ATTR_PBSS))
4823 return -1;
4824 }
4825
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004826 return 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004827}
4828
4829
4830static int wpa_driver_nl80211_try_connect(
4831 struct wpa_driver_nl80211_data *drv,
4832 struct wpa_driver_associate_params *params)
4833{
4834 struct nl_msg *msg;
4835 enum nl80211_auth_type type;
4836 int ret;
4837 int algs;
4838
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004839#ifdef CONFIG_DRIVER_NL80211_QCA
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004840 if (params->req_key_mgmt_offload && params->psk &&
4841 (params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
4842 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256 ||
4843 params->key_mgmt_suite == WPA_KEY_MGMT_FT_PSK)) {
4844 wpa_printf(MSG_DEBUG, "nl80211: Key management set PSK");
4845 ret = issue_key_mgmt_set_key(drv, params->psk, 32);
4846 if (ret)
4847 return ret;
4848 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004849#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004850
4851 wpa_printf(MSG_DEBUG, "nl80211: Connect (ifindex=%d)", drv->ifindex);
4852 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_CONNECT);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004853 if (!msg)
4854 return -1;
4855
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004856 ret = nl80211_connect_common(drv, params, msg);
4857 if (ret)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004858 goto fail;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004859
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004860 algs = 0;
4861 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
4862 algs++;
4863 if (params->auth_alg & WPA_AUTH_ALG_SHARED)
4864 algs++;
4865 if (params->auth_alg & WPA_AUTH_ALG_LEAP)
4866 algs++;
4867 if (algs > 1) {
4868 wpa_printf(MSG_DEBUG, " * Leave out Auth Type for automatic "
4869 "selection");
4870 goto skip_auth_type;
4871 }
4872
4873 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
4874 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
4875 else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
4876 type = NL80211_AUTHTYPE_SHARED_KEY;
4877 else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
4878 type = NL80211_AUTHTYPE_NETWORK_EAP;
4879 else if (params->auth_alg & WPA_AUTH_ALG_FT)
4880 type = NL80211_AUTHTYPE_FT;
4881 else
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004882 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004883
4884 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004885 if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE, type))
4886 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004887
4888skip_auth_type:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004889 ret = nl80211_set_conn_keys(params, msg);
4890 if (ret)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004891 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004892
4893 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4894 msg = NULL;
4895 if (ret) {
4896 wpa_printf(MSG_DEBUG, "nl80211: MLME connect failed: ret=%d "
4897 "(%s)", ret, strerror(-ret));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004898 } else {
4899 wpa_printf(MSG_DEBUG,
4900 "nl80211: Connect request send successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004901 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004902
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004903fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004904 nlmsg_free(msg);
4905 return ret;
4906
4907}
4908
4909
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004910static int wpa_driver_nl80211_connect(
4911 struct wpa_driver_nl80211_data *drv,
4912 struct wpa_driver_associate_params *params)
4913{
Jithu Jancea7c60b42014-12-03 18:54:40 +05304914 int ret;
4915
4916 /* Store the connection attempted bssid for future use */
4917 if (params->bssid)
4918 os_memcpy(drv->auth_attempt_bssid, params->bssid, ETH_ALEN);
4919 else
4920 os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
4921
4922 ret = wpa_driver_nl80211_try_connect(drv, params);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004923 if (ret == -EALREADY) {
4924 /*
4925 * cfg80211 does not currently accept new connections if
4926 * we are already connected. As a workaround, force
4927 * disconnection and try again.
4928 */
4929 wpa_printf(MSG_DEBUG, "nl80211: Explicitly "
4930 "disconnecting before reassociation "
4931 "attempt");
4932 if (wpa_driver_nl80211_disconnect(
4933 drv, WLAN_REASON_PREV_AUTH_NOT_VALID))
4934 return -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004935 ret = wpa_driver_nl80211_try_connect(drv, params);
4936 }
4937 return ret;
4938}
4939
4940
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004941static int wpa_driver_nl80211_associate(
4942 void *priv, struct wpa_driver_associate_params *params)
4943{
4944 struct i802_bss *bss = priv;
4945 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004946 int ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004947 struct nl_msg *msg;
4948
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004949 nl80211_unmask_11b_rates(bss);
4950
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004951 if (params->mode == IEEE80211_MODE_AP)
4952 return wpa_driver_nl80211_ap(drv, params);
4953
4954 if (params->mode == IEEE80211_MODE_IBSS)
4955 return wpa_driver_nl80211_ibss(drv, params);
4956
4957 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004958 enum nl80211_iftype nlmode = params->p2p ?
4959 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
4960
4961 if (wpa_driver_nl80211_set_mode(priv, nlmode) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004962 return -1;
4963 return wpa_driver_nl80211_connect(drv, params);
4964 }
4965
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004966 nl80211_mark_disconnected(drv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004967
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004968 wpa_printf(MSG_DEBUG, "nl80211: Associate (ifindex=%d)",
4969 drv->ifindex);
4970 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_ASSOCIATE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004971 if (!msg)
4972 return -1;
4973
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004974 ret = nl80211_connect_common(drv, params, msg);
4975 if (ret)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004976 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004977
4978 if (params->prev_bssid) {
4979 wpa_printf(MSG_DEBUG, " * prev_bssid=" MACSTR,
4980 MAC2STR(params->prev_bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004981 if (nla_put(msg, NL80211_ATTR_PREV_BSSID, ETH_ALEN,
4982 params->prev_bssid))
4983 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004984 }
4985
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004986 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4987 msg = NULL;
4988 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004989 wpa_dbg(drv->ctx, MSG_DEBUG,
4990 "nl80211: MLME command failed (assoc): ret=%d (%s)",
4991 ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004992 nl80211_dump_scan(drv);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004993 } else {
4994 wpa_printf(MSG_DEBUG,
4995 "nl80211: Association request send successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004996 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004997
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004998fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004999 nlmsg_free(msg);
5000 return ret;
5001}
5002
5003
5004static int nl80211_set_mode(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005005 int ifindex, enum nl80211_iftype mode)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005006{
5007 struct nl_msg *msg;
5008 int ret = -ENOBUFS;
5009
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005010 wpa_printf(MSG_DEBUG, "nl80211: Set mode ifindex %d iftype %d (%s)",
5011 ifindex, mode, nl80211_iftype_str(mode));
5012
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005013 msg = nl80211_cmd_msg(drv->first_bss, 0, NL80211_CMD_SET_INTERFACE);
5014 if (!msg || nla_put_u32(msg, NL80211_ATTR_IFTYPE, mode))
5015 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005016
5017 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005018 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005019 if (!ret)
5020 return 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005021fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005022 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005023 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface %d to mode %d:"
5024 " %d (%s)", ifindex, mode, ret, strerror(-ret));
5025 return ret;
5026}
5027
5028
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005029static int wpa_driver_nl80211_set_mode_impl(
5030 struct i802_bss *bss,
5031 enum nl80211_iftype nlmode,
5032 struct hostapd_freq_params *desired_freq_params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005033{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005034 struct wpa_driver_nl80211_data *drv = bss->drv;
5035 int ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005036 int i;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005037 int was_ap = is_ap_interface(drv->nlmode);
5038 int res;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005039 int mode_switch_res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005040
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005041 mode_switch_res = nl80211_set_mode(drv, drv->ifindex, nlmode);
5042 if (mode_switch_res && nlmode == nl80211_get_ifmode(bss))
5043 mode_switch_res = 0;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005044
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005045 if (mode_switch_res == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005046 drv->nlmode = nlmode;
5047 ret = 0;
5048 goto done;
5049 }
5050
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005051 if (mode_switch_res == -ENODEV)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005052 return -1;
5053
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005054 if (nlmode == drv->nlmode) {
5055 wpa_printf(MSG_DEBUG, "nl80211: Interface already in "
5056 "requested mode - ignore error");
5057 ret = 0;
5058 goto done; /* Already in the requested mode */
5059 }
5060
5061 /* mac80211 doesn't allow mode changes while the device is up, so
5062 * take the device down, try to set the mode again, and bring the
5063 * device back up.
5064 */
5065 wpa_printf(MSG_DEBUG, "nl80211: Try mode change after setting "
5066 "interface down");
5067 for (i = 0; i < 10; i++) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005068 res = i802_set_iface_flags(bss, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005069 if (res == -EACCES || res == -ENODEV)
5070 break;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005071 if (res != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005072 wpa_printf(MSG_DEBUG, "nl80211: Failed to set "
5073 "interface down");
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005074 os_sleep(0, 100000);
5075 continue;
5076 }
5077
5078 /*
5079 * Setting the mode will fail for some drivers if the phy is
5080 * on a frequency that the mode is disallowed in.
5081 */
5082 if (desired_freq_params) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005083 res = nl80211_set_channel(bss, desired_freq_params, 0);
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005084 if (res) {
5085 wpa_printf(MSG_DEBUG,
5086 "nl80211: Failed to set frequency on interface");
5087 }
5088 }
5089
5090 /* Try to set the mode again while the interface is down */
5091 mode_switch_res = nl80211_set_mode(drv, drv->ifindex, nlmode);
5092 if (mode_switch_res == -EBUSY) {
5093 wpa_printf(MSG_DEBUG,
5094 "nl80211: Delaying mode set while interface going down");
5095 os_sleep(0, 100000);
5096 continue;
5097 }
5098 ret = mode_switch_res;
5099 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005100 }
5101
5102 if (!ret) {
5103 wpa_printf(MSG_DEBUG, "nl80211: Mode change succeeded while "
5104 "interface is down");
5105 drv->nlmode = nlmode;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005106 drv->ignore_if_down_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005107 }
5108
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005109 /* Bring the interface back up */
5110 res = linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1);
5111 if (res != 0) {
5112 wpa_printf(MSG_DEBUG,
5113 "nl80211: Failed to set interface up after switching mode");
5114 ret = -1;
5115 }
5116
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005117done:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005118 if (ret) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005119 wpa_printf(MSG_DEBUG, "nl80211: Interface mode change to %d "
5120 "from %d failed", nlmode, drv->nlmode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005121 return ret;
5122 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005123
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005124 if (is_p2p_net_interface(nlmode)) {
5125 wpa_printf(MSG_DEBUG,
5126 "nl80211: Interface %s mode change to P2P - disable 11b rates",
5127 bss->ifname);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005128 nl80211_disable_11b_rates(drv, drv->ifindex, 1);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005129 } else if (drv->disabled_11b_rates) {
5130 wpa_printf(MSG_DEBUG,
5131 "nl80211: Interface %s mode changed to non-P2P - re-enable 11b rates",
5132 bss->ifname);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005133 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005134 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005135
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005136 if (is_ap_interface(nlmode)) {
5137 nl80211_mgmt_unsubscribe(bss, "start AP");
5138 /* Setup additional AP mode functionality if needed */
5139 if (nl80211_setup_ap(bss))
5140 return -1;
5141 } else if (was_ap) {
5142 /* Remove additional AP mode functionality */
5143 nl80211_teardown_ap(bss);
5144 } else {
5145 nl80211_mgmt_unsubscribe(bss, "mode change");
5146 }
5147
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005148 if (is_mesh_interface(nlmode) &&
5149 nl80211_mgmt_subscribe_mesh(bss))
5150 return -1;
5151
Dmitry Shmidt04949592012-07-19 12:16:46 -07005152 if (!bss->in_deinit && !is_ap_interface(nlmode) &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005153 !is_mesh_interface(nlmode) &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005154 nl80211_mgmt_subscribe_non_ap(bss) < 0)
5155 wpa_printf(MSG_DEBUG, "nl80211: Failed to register Action "
5156 "frame processing - ignore for now");
5157
5158 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005159}
5160
5161
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005162int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
5163 enum nl80211_iftype nlmode)
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005164{
5165 return wpa_driver_nl80211_set_mode_impl(bss, nlmode, NULL);
5166}
5167
5168
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07005169static int wpa_driver_nl80211_set_mode_ibss(struct i802_bss *bss,
5170 struct hostapd_freq_params *freq)
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005171{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005172 return wpa_driver_nl80211_set_mode_impl(bss, NL80211_IFTYPE_ADHOC,
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07005173 freq);
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005174}
5175
5176
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005177static int wpa_driver_nl80211_get_capa(void *priv,
5178 struct wpa_driver_capa *capa)
5179{
5180 struct i802_bss *bss = priv;
5181 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07005182
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005183 if (!drv->has_capability)
5184 return -1;
5185 os_memcpy(capa, &drv->capa, sizeof(*capa));
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005186 if (drv->extended_capa && drv->extended_capa_mask) {
5187 capa->extended_capa = drv->extended_capa;
5188 capa->extended_capa_mask = drv->extended_capa_mask;
5189 capa->extended_capa_len = drv->extended_capa_len;
5190 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005191
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005192 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005193}
5194
5195
5196static int wpa_driver_nl80211_set_operstate(void *priv, int state)
5197{
5198 struct i802_bss *bss = priv;
5199 struct wpa_driver_nl80211_data *drv = bss->drv;
5200
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005201 wpa_printf(MSG_DEBUG, "nl80211: Set %s operstate %d->%d (%s)",
5202 bss->ifname, drv->operstate, state,
5203 state ? "UP" : "DORMANT");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005204 drv->operstate = state;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005205 return netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, -1,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005206 state ? IF_OPER_UP : IF_OPER_DORMANT);
5207}
5208
5209
5210static int wpa_driver_nl80211_set_supp_port(void *priv, int authorized)
5211{
5212 struct i802_bss *bss = priv;
5213 struct wpa_driver_nl80211_data *drv = bss->drv;
5214 struct nl_msg *msg;
5215 struct nl80211_sta_flag_update upd;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005216 int ret;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005217
5218 if (!drv->associated && is_zero_ether_addr(drv->bssid) && !authorized) {
5219 wpa_printf(MSG_DEBUG, "nl80211: Skip set_supp_port(unauthorized) while not associated");
5220 return 0;
5221 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005222
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005223 wpa_printf(MSG_DEBUG, "nl80211: Set supplicant port %sauthorized for "
5224 MACSTR, authorized ? "" : "un", MAC2STR(drv->bssid));
5225
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005226 os_memset(&upd, 0, sizeof(upd));
5227 upd.mask = BIT(NL80211_STA_FLAG_AUTHORIZED);
5228 if (authorized)
5229 upd.set = BIT(NL80211_STA_FLAG_AUTHORIZED);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005230
5231 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
5232 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid) ||
5233 nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd)) {
5234 nlmsg_free(msg);
5235 return -ENOBUFS;
5236 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005237
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005238 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005239 if (!ret)
5240 return 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005241 wpa_printf(MSG_DEBUG, "nl80211: Failed to set STA flag: %d (%s)",
5242 ret, strerror(-ret));
5243 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005244}
5245
5246
Jouni Malinen75ecf522011-06-27 15:19:46 -07005247/* Set kernel driver on given frequency (MHz) */
5248static int i802_set_freq(void *priv, struct hostapd_freq_params *freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005249{
Jouni Malinen75ecf522011-06-27 15:19:46 -07005250 struct i802_bss *bss = priv;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07005251 return nl80211_set_channel(bss, freq, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005252}
5253
5254
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005255static inline int min_int(int a, int b)
5256{
5257 if (a < b)
5258 return a;
5259 return b;
5260}
5261
5262
5263static int get_key_handler(struct nl_msg *msg, void *arg)
5264{
5265 struct nlattr *tb[NL80211_ATTR_MAX + 1];
5266 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
5267
5268 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
5269 genlmsg_attrlen(gnlh, 0), NULL);
5270
5271 /*
5272 * TODO: validate the key index and mac address!
5273 * Otherwise, there's a race condition as soon as
5274 * the kernel starts sending key notifications.
5275 */
5276
5277 if (tb[NL80211_ATTR_KEY_SEQ])
5278 memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]),
5279 min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6));
5280 return NL_SKIP;
5281}
5282
5283
5284static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
5285 int idx, u8 *seq)
5286{
5287 struct i802_bss *bss = priv;
5288 struct wpa_driver_nl80211_data *drv = bss->drv;
5289 struct nl_msg *msg;
5290
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005291 msg = nl80211_ifindex_msg(drv, if_nametoindex(iface), 0,
5292 NL80211_CMD_GET_KEY);
5293 if (!msg ||
5294 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
5295 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, idx)) {
5296 nlmsg_free(msg);
5297 return -ENOBUFS;
5298 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005299
5300 memset(seq, 0, 6);
5301
5302 return send_and_recv_msgs(drv, msg, get_key_handler, seq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005303}
5304
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005305
5306static int i802_set_rts(void *priv, int rts)
5307{
5308 struct i802_bss *bss = priv;
5309 struct wpa_driver_nl80211_data *drv = bss->drv;
5310 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005311 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005312 u32 val;
5313
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005314 if (rts >= 2347)
5315 val = (u32) -1;
5316 else
5317 val = rts;
5318
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005319 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_WIPHY)) ||
5320 nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD, val)) {
5321 nlmsg_free(msg);
5322 return -ENOBUFS;
5323 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005324
5325 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5326 if (!ret)
5327 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005328 wpa_printf(MSG_DEBUG, "nl80211: Failed to set RTS threshold %d: "
5329 "%d (%s)", rts, ret, strerror(-ret));
5330 return ret;
5331}
5332
5333
5334static int i802_set_frag(void *priv, int frag)
5335{
5336 struct i802_bss *bss = priv;
5337 struct wpa_driver_nl80211_data *drv = bss->drv;
5338 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005339 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005340 u32 val;
5341
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005342 if (frag >= 2346)
5343 val = (u32) -1;
5344 else
5345 val = frag;
5346
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005347 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_WIPHY)) ||
5348 nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD, val)) {
5349 nlmsg_free(msg);
5350 return -ENOBUFS;
5351 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005352
5353 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5354 if (!ret)
5355 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005356 wpa_printf(MSG_DEBUG, "nl80211: Failed to set fragmentation threshold "
5357 "%d: %d (%s)", frag, ret, strerror(-ret));
5358 return ret;
5359}
5360
5361
5362static int i802_flush(void *priv)
5363{
5364 struct i802_bss *bss = priv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005365 struct nl_msg *msg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005366 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005367
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005368 wpa_printf(MSG_DEBUG, "nl80211: flush -> DEL_STATION %s (all)",
5369 bss->ifname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005370
5371 /*
5372 * XXX: FIX! this needs to flush all VLANs too
5373 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005374 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_DEL_STATION);
5375 res = send_and_recv_msgs(bss->drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005376 if (res) {
5377 wpa_printf(MSG_DEBUG, "nl80211: Station flush failed: ret=%d "
5378 "(%s)", res, strerror(-res));
5379 }
5380 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005381}
5382
5383
5384static int get_sta_handler(struct nl_msg *msg, void *arg)
5385{
5386 struct nlattr *tb[NL80211_ATTR_MAX + 1];
5387 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
5388 struct hostap_sta_driver_data *data = arg;
5389 struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
5390 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
5391 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
5392 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
5393 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
5394 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
5395 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03005396 [NL80211_STA_INFO_TX_FAILED] = { .type = NLA_U32 },
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005397 [NL80211_STA_INFO_RX_BYTES64] = { .type = NLA_U64 },
5398 [NL80211_STA_INFO_TX_BYTES64] = { .type = NLA_U64 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005399 };
5400
5401 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
5402 genlmsg_attrlen(gnlh, 0), NULL);
5403
5404 /*
5405 * TODO: validate the interface and mac address!
5406 * Otherwise, there's a race condition as soon as
5407 * the kernel starts sending station notifications.
5408 */
5409
5410 if (!tb[NL80211_ATTR_STA_INFO]) {
5411 wpa_printf(MSG_DEBUG, "sta stats missing!");
5412 return NL_SKIP;
5413 }
5414 if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
5415 tb[NL80211_ATTR_STA_INFO],
5416 stats_policy)) {
5417 wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
5418 return NL_SKIP;
5419 }
5420
5421 if (stats[NL80211_STA_INFO_INACTIVE_TIME])
5422 data->inactive_msec =
5423 nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005424 /* For backwards compatibility, fetch the 32-bit counters first. */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005425 if (stats[NL80211_STA_INFO_RX_BYTES])
5426 data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
5427 if (stats[NL80211_STA_INFO_TX_BYTES])
5428 data->tx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005429 if (stats[NL80211_STA_INFO_RX_BYTES64] &&
5430 stats[NL80211_STA_INFO_TX_BYTES64]) {
5431 /*
5432 * The driver supports 64-bit counters, so use them to override
5433 * the 32-bit values.
5434 */
5435 data->rx_bytes =
5436 nla_get_u64(stats[NL80211_STA_INFO_RX_BYTES64]);
5437 data->tx_bytes =
5438 nla_get_u64(stats[NL80211_STA_INFO_TX_BYTES64]);
5439 data->bytes_64bit = 1;
5440 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005441 if (stats[NL80211_STA_INFO_RX_PACKETS])
5442 data->rx_packets =
5443 nla_get_u32(stats[NL80211_STA_INFO_RX_PACKETS]);
5444 if (stats[NL80211_STA_INFO_TX_PACKETS])
5445 data->tx_packets =
5446 nla_get_u32(stats[NL80211_STA_INFO_TX_PACKETS]);
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03005447 if (stats[NL80211_STA_INFO_TX_FAILED])
5448 data->tx_retry_failed =
5449 nla_get_u32(stats[NL80211_STA_INFO_TX_FAILED]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005450
5451 return NL_SKIP;
5452}
5453
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005454static int i802_read_sta_data(struct i802_bss *bss,
5455 struct hostap_sta_driver_data *data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005456 const u8 *addr)
5457{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005458 struct nl_msg *msg;
5459
5460 os_memset(data, 0, sizeof(*data));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005461
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005462 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_GET_STATION)) ||
5463 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
5464 nlmsg_free(msg);
5465 return -ENOBUFS;
5466 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005467
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005468 return send_and_recv_msgs(bss->drv, msg, get_sta_handler, data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005469}
5470
5471
5472static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
5473 int cw_min, int cw_max, int burst_time)
5474{
5475 struct i802_bss *bss = priv;
5476 struct wpa_driver_nl80211_data *drv = bss->drv;
5477 struct nl_msg *msg;
5478 struct nlattr *txq, *params;
5479
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005480 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005481 if (!msg)
5482 return -1;
5483
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005484 txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS);
5485 if (!txq)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005486 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005487
5488 /* We are only sending parameters for a single TXQ at a time */
5489 params = nla_nest_start(msg, 1);
5490 if (!params)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005491 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005492
5493 switch (queue) {
5494 case 0:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005495 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VO))
5496 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005497 break;
5498 case 1:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005499 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VI))
5500 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005501 break;
5502 case 2:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005503 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BE))
5504 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005505 break;
5506 case 3:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005507 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BK))
5508 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005509 break;
5510 }
5511 /* Burst time is configured in units of 0.1 msec and TXOP parameter in
5512 * 32 usec, so need to convert the value here. */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005513 if (nla_put_u16(msg, NL80211_TXQ_ATTR_TXOP,
5514 (burst_time * 100 + 16) / 32) ||
5515 nla_put_u16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min) ||
5516 nla_put_u16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max) ||
5517 nla_put_u8(msg, NL80211_TXQ_ATTR_AIFS, aifs))
5518 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005519
5520 nla_nest_end(msg, params);
5521
5522 nla_nest_end(msg, txq);
5523
5524 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
5525 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005526 msg = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005527fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005528 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005529 return -1;
5530}
5531
5532
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005533static int i802_set_sta_vlan(struct i802_bss *bss, const u8 *addr,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005534 const char *ifname, int vlan_id)
5535{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005536 struct wpa_driver_nl80211_data *drv = bss->drv;
5537 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005538 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005539
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005540 wpa_printf(MSG_DEBUG, "nl80211: %s[%d]: set_sta_vlan(" MACSTR
5541 ", ifname=%s[%d], vlan_id=%d)",
5542 bss->ifname, if_nametoindex(bss->ifname),
5543 MAC2STR(addr), ifname, if_nametoindex(ifname), vlan_id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005544 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
5545 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
5546 nla_put_u32(msg, NL80211_ATTR_STA_VLAN, if_nametoindex(ifname))) {
5547 nlmsg_free(msg);
5548 return -ENOBUFS;
5549 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005550
5551 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5552 if (ret < 0) {
5553 wpa_printf(MSG_ERROR, "nl80211: NL80211_ATTR_STA_VLAN (addr="
5554 MACSTR " ifname=%s vlan_id=%d) failed: %d (%s)",
5555 MAC2STR(addr), ifname, vlan_id, ret,
5556 strerror(-ret));
5557 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005558 return ret;
5559}
5560
5561
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005562static int i802_get_inact_sec(void *priv, const u8 *addr)
5563{
5564 struct hostap_sta_driver_data data;
5565 int ret;
5566
5567 data.inactive_msec = (unsigned long) -1;
5568 ret = i802_read_sta_data(priv, &data, addr);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08005569 if (ret == -ENOENT)
5570 return -ENOENT;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005571 if (ret || data.inactive_msec == (unsigned long) -1)
5572 return -1;
5573 return data.inactive_msec / 1000;
5574}
5575
5576
5577static int i802_sta_clear_stats(void *priv, const u8 *addr)
5578{
5579#if 0
5580 /* TODO */
5581#endif
5582 return 0;
5583}
5584
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005585
5586static int i802_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
5587 int reason)
5588{
5589 struct i802_bss *bss = priv;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005590 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005591 struct ieee80211_mgmt mgmt;
5592
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005593 if (is_mesh_interface(drv->nlmode))
5594 return -1;
5595
Dmitry Shmidt04949592012-07-19 12:16:46 -07005596 if (drv->device_ap_sme)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005597 return wpa_driver_nl80211_sta_remove(bss, addr, 1, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005598
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005599 memset(&mgmt, 0, sizeof(mgmt));
5600 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
5601 WLAN_FC_STYPE_DEAUTH);
5602 memcpy(mgmt.da, addr, ETH_ALEN);
5603 memcpy(mgmt.sa, own_addr, ETH_ALEN);
5604 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
5605 mgmt.u.deauth.reason_code = host_to_le16(reason);
5606 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
5607 IEEE80211_HDRLEN +
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005608 sizeof(mgmt.u.deauth), 0, 0, 0, 0,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005609 0, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005610}
5611
5612
5613static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
5614 int reason)
5615{
5616 struct i802_bss *bss = priv;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005617 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005618 struct ieee80211_mgmt mgmt;
5619
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005620 if (is_mesh_interface(drv->nlmode))
5621 return -1;
5622
Dmitry Shmidt04949592012-07-19 12:16:46 -07005623 if (drv->device_ap_sme)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005624 return wpa_driver_nl80211_sta_remove(bss, addr, 0, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005625
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005626 memset(&mgmt, 0, sizeof(mgmt));
5627 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
5628 WLAN_FC_STYPE_DISASSOC);
5629 memcpy(mgmt.da, addr, ETH_ALEN);
5630 memcpy(mgmt.sa, own_addr, ETH_ALEN);
5631 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
5632 mgmt.u.disassoc.reason_code = host_to_le16(reason);
5633 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
5634 IEEE80211_HDRLEN +
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005635 sizeof(mgmt.u.disassoc), 0, 0, 0, 0,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005636 0, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005637}
5638
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005639
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07005640static void dump_ifidx(struct wpa_driver_nl80211_data *drv)
5641{
5642 char buf[200], *pos, *end;
5643 int i, res;
5644
5645 pos = buf;
5646 end = pos + sizeof(buf);
5647
5648 for (i = 0; i < drv->num_if_indices; i++) {
5649 if (!drv->if_indices[i])
5650 continue;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005651 res = os_snprintf(pos, end - pos, " %d(%d)",
5652 drv->if_indices[i],
5653 drv->if_indices_reason[i]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005654 if (os_snprintf_error(end - pos, res))
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07005655 break;
5656 pos += res;
5657 }
5658 *pos = '\0';
5659
5660 wpa_printf(MSG_DEBUG, "nl80211: if_indices[%d]:%s",
5661 drv->num_if_indices, buf);
5662}
5663
5664
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005665static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
5666 int ifidx_reason)
Jouni Malinen75ecf522011-06-27 15:19:46 -07005667{
5668 int i;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005669 int *old, *old_reason;
Jouni Malinen75ecf522011-06-27 15:19:46 -07005670
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005671 wpa_printf(MSG_DEBUG,
5672 "nl80211: Add own interface ifindex %d (ifidx_reason %d)",
5673 ifidx, ifidx_reason);
5674 if (have_ifidx(drv, ifidx, ifidx_reason)) {
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07005675 wpa_printf(MSG_DEBUG, "nl80211: ifindex %d already in the list",
5676 ifidx);
5677 return;
5678 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07005679 for (i = 0; i < drv->num_if_indices; i++) {
5680 if (drv->if_indices[i] == 0) {
5681 drv->if_indices[i] = ifidx;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005682 drv->if_indices_reason[i] = ifidx_reason;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07005683 dump_ifidx(drv);
Jouni Malinen75ecf522011-06-27 15:19:46 -07005684 return;
5685 }
5686 }
5687
5688 if (drv->if_indices != drv->default_if_indices)
5689 old = drv->if_indices;
5690 else
5691 old = NULL;
5692
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005693 if (drv->if_indices_reason != drv->default_if_indices_reason)
5694 old_reason = drv->if_indices_reason;
5695 else
5696 old_reason = NULL;
5697
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005698 drv->if_indices = os_realloc_array(old, drv->num_if_indices + 1,
5699 sizeof(int));
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005700 drv->if_indices_reason = os_realloc_array(old_reason,
5701 drv->num_if_indices + 1,
5702 sizeof(int));
Jouni Malinen75ecf522011-06-27 15:19:46 -07005703 if (!drv->if_indices) {
5704 if (!old)
5705 drv->if_indices = drv->default_if_indices;
5706 else
5707 drv->if_indices = old;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005708 }
5709 if (!drv->if_indices_reason) {
5710 if (!old_reason)
5711 drv->if_indices_reason = drv->default_if_indices_reason;
5712 else
5713 drv->if_indices = old_reason;
5714 }
5715 if (!drv->if_indices || !drv->if_indices_reason) {
Jouni Malinen75ecf522011-06-27 15:19:46 -07005716 wpa_printf(MSG_ERROR, "Failed to reallocate memory for "
5717 "interfaces");
5718 wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx);
5719 return;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005720 }
5721 if (!old)
Jouni Malinen75ecf522011-06-27 15:19:46 -07005722 os_memcpy(drv->if_indices, drv->default_if_indices,
5723 sizeof(drv->default_if_indices));
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005724 if (!old_reason)
5725 os_memcpy(drv->if_indices_reason,
5726 drv->default_if_indices_reason,
5727 sizeof(drv->default_if_indices_reason));
Jouni Malinen75ecf522011-06-27 15:19:46 -07005728 drv->if_indices[drv->num_if_indices] = ifidx;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005729 drv->if_indices_reason[drv->num_if_indices] = ifidx_reason;
Jouni Malinen75ecf522011-06-27 15:19:46 -07005730 drv->num_if_indices++;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07005731 dump_ifidx(drv);
Jouni Malinen75ecf522011-06-27 15:19:46 -07005732}
5733
5734
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005735static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
5736 int ifidx_reason)
Jouni Malinen75ecf522011-06-27 15:19:46 -07005737{
5738 int i;
5739
5740 for (i = 0; i < drv->num_if_indices; i++) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005741 if ((drv->if_indices[i] == ifidx || ifidx == IFIDX_ANY) &&
5742 (drv->if_indices_reason[i] == ifidx_reason ||
5743 ifidx_reason == IFIDX_ANY)) {
Jouni Malinen75ecf522011-06-27 15:19:46 -07005744 drv->if_indices[i] = 0;
5745 break;
5746 }
5747 }
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07005748 dump_ifidx(drv);
Jouni Malinen75ecf522011-06-27 15:19:46 -07005749}
5750
5751
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005752static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
5753 int ifidx_reason)
Jouni Malinen75ecf522011-06-27 15:19:46 -07005754{
5755 int i;
5756
5757 for (i = 0; i < drv->num_if_indices; i++)
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005758 if (drv->if_indices[i] == ifidx &&
5759 (drv->if_indices_reason[i] == ifidx_reason ||
5760 ifidx_reason == IFIDX_ANY))
Jouni Malinen75ecf522011-06-27 15:19:46 -07005761 return 1;
5762
5763 return 0;
5764}
5765
5766
5767static int i802_set_wds_sta(void *priv, const u8 *addr, int aid, int val,
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07005768 const char *bridge_ifname, char *ifname_wds)
Jouni Malinen75ecf522011-06-27 15:19:46 -07005769{
5770 struct i802_bss *bss = priv;
5771 struct wpa_driver_nl80211_data *drv = bss->drv;
5772 char name[IFNAMSIZ + 1];
5773
5774 os_snprintf(name, sizeof(name), "%s.sta%d", bss->ifname, aid);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005775 if (ifname_wds)
5776 os_strlcpy(ifname_wds, name, IFNAMSIZ + 1);
5777
Jouni Malinen75ecf522011-06-27 15:19:46 -07005778 wpa_printf(MSG_DEBUG, "nl80211: Set WDS STA addr=" MACSTR
5779 " aid=%d val=%d name=%s", MAC2STR(addr), aid, val, name);
5780 if (val) {
5781 if (!if_nametoindex(name)) {
5782 if (nl80211_create_iface(drv, name,
5783 NL80211_IFTYPE_AP_VLAN,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005784 bss->addr, 1, NULL, NULL, 0) <
5785 0)
Jouni Malinen75ecf522011-06-27 15:19:46 -07005786 return -1;
5787 if (bridge_ifname &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005788 linux_br_add_if(drv->global->ioctl_sock,
5789 bridge_ifname, name) < 0)
Jouni Malinen75ecf522011-06-27 15:19:46 -07005790 return -1;
5791 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005792 if (linux_set_iface_flags(drv->global->ioctl_sock, name, 1)) {
5793 wpa_printf(MSG_ERROR, "nl80211: Failed to set WDS STA "
5794 "interface %s up", name);
5795 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07005796 return i802_set_sta_vlan(priv, addr, name, 0);
5797 } else {
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005798 if (bridge_ifname)
5799 linux_br_del_if(drv->global->ioctl_sock, bridge_ifname,
5800 name);
5801
Jouni Malinen75ecf522011-06-27 15:19:46 -07005802 i802_set_sta_vlan(priv, addr, bss->ifname, 0);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08005803 nl80211_remove_iface(drv, if_nametoindex(name));
5804 return 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07005805 }
5806}
5807
5808
5809static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
5810{
5811 struct wpa_driver_nl80211_data *drv = eloop_ctx;
5812 struct sockaddr_ll lladdr;
5813 unsigned char buf[3000];
5814 int len;
5815 socklen_t fromlen = sizeof(lladdr);
5816
5817 len = recvfrom(sock, buf, sizeof(buf), 0,
5818 (struct sockaddr *)&lladdr, &fromlen);
5819 if (len < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005820 wpa_printf(MSG_ERROR, "nl80211: EAPOL recv failed: %s",
5821 strerror(errno));
Jouni Malinen75ecf522011-06-27 15:19:46 -07005822 return;
5823 }
5824
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005825 if (have_ifidx(drv, lladdr.sll_ifindex, IFIDX_ANY))
Jouni Malinen75ecf522011-06-27 15:19:46 -07005826 drv_event_eapol_rx(drv->ctx, lladdr.sll_addr, buf, len);
5827}
5828
5829
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005830static int i802_check_bridge(struct wpa_driver_nl80211_data *drv,
5831 struct i802_bss *bss,
5832 const char *brname, const char *ifname)
5833{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005834 int br_ifindex;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005835 char in_br[IFNAMSIZ];
5836
5837 os_strlcpy(bss->brname, brname, IFNAMSIZ);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005838 br_ifindex = if_nametoindex(brname);
5839 if (br_ifindex == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005840 /*
5841 * Bridge was configured, but the bridge device does
5842 * not exist. Try to add it now.
5843 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005844 if (linux_br_add(drv->global->ioctl_sock, brname) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005845 wpa_printf(MSG_ERROR, "nl80211: Failed to add the "
5846 "bridge interface %s: %s",
5847 brname, strerror(errno));
5848 return -1;
5849 }
5850 bss->added_bridge = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005851 br_ifindex = if_nametoindex(brname);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005852 add_ifidx(drv, br_ifindex, drv->ifindex);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005853 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005854 bss->br_ifindex = br_ifindex;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005855
5856 if (linux_br_get(in_br, ifname) == 0) {
5857 if (os_strcmp(in_br, brname) == 0)
5858 return 0; /* already in the bridge */
5859
5860 wpa_printf(MSG_DEBUG, "nl80211: Removing interface %s from "
5861 "bridge %s", ifname, in_br);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005862 if (linux_br_del_if(drv->global->ioctl_sock, in_br, ifname) <
5863 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005864 wpa_printf(MSG_ERROR, "nl80211: Failed to "
5865 "remove interface %s from bridge "
5866 "%s: %s",
5867 ifname, brname, strerror(errno));
5868 return -1;
5869 }
5870 }
5871
5872 wpa_printf(MSG_DEBUG, "nl80211: Adding interface %s into bridge %s",
5873 ifname, brname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005874 if (linux_br_add_if(drv->global->ioctl_sock, brname, ifname) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005875 wpa_printf(MSG_ERROR, "nl80211: Failed to add interface %s "
5876 "into bridge %s: %s",
5877 ifname, brname, strerror(errno));
5878 return -1;
5879 }
5880 bss->added_if_into_bridge = 1;
5881
5882 return 0;
5883}
5884
5885
5886static void *i802_init(struct hostapd_data *hapd,
5887 struct wpa_init_params *params)
5888{
5889 struct wpa_driver_nl80211_data *drv;
5890 struct i802_bss *bss;
5891 size_t i;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005892 char master_ifname[IFNAMSIZ];
5893 int ifindex, br_ifindex = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005894 int br_added = 0;
5895
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005896 bss = wpa_driver_nl80211_drv_init(hapd, params->ifname,
5897 params->global_priv, 1,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005898 params->bssid, params->driver_params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005899 if (bss == NULL)
5900 return NULL;
5901
5902 drv = bss->drv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005903
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005904 if (linux_br_get(master_ifname, params->ifname) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005905 wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in bridge %s",
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005906 params->ifname, master_ifname);
5907 br_ifindex = if_nametoindex(master_ifname);
5908 os_strlcpy(bss->brname, master_ifname, IFNAMSIZ);
5909 } else if ((params->num_bridge == 0 || !params->bridge[0]) &&
5910 linux_master_get(master_ifname, params->ifname) == 0) {
5911 wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in master %s",
5912 params->ifname, master_ifname);
5913 /* start listening for EAPOL on the master interface */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005914 add_ifidx(drv, if_nametoindex(master_ifname), drv->ifindex);
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005915
5916 /* check if master itself is under bridge */
5917 if (linux_br_get(master_ifname, master_ifname) == 0) {
5918 wpa_printf(MSG_DEBUG, "nl80211: which is in bridge %s",
5919 master_ifname);
5920 br_ifindex = if_nametoindex(master_ifname);
5921 os_strlcpy(bss->brname, master_ifname, IFNAMSIZ);
5922 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005923 } else {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005924 master_ifname[0] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005925 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005926
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005927 bss->br_ifindex = br_ifindex;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005928
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005929 for (i = 0; i < params->num_bridge; i++) {
5930 if (params->bridge[i]) {
5931 ifindex = if_nametoindex(params->bridge[i]);
5932 if (ifindex)
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005933 add_ifidx(drv, ifindex, drv->ifindex);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005934 if (ifindex == br_ifindex)
5935 br_added = 1;
5936 }
5937 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005938
5939 /* start listening for EAPOL on the default AP interface */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005940 add_ifidx(drv, drv->ifindex, IFIDX_ANY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005941
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005942 if (params->num_bridge && params->bridge[0]) {
5943 if (i802_check_bridge(drv, bss, params->bridge[0],
5944 params->ifname) < 0)
5945 goto failed;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005946 if (os_strcmp(params->bridge[0], master_ifname) != 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005947 br_added = 1;
5948 }
5949
5950 if (!br_added && br_ifindex &&
5951 (params->num_bridge == 0 || !params->bridge[0]))
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005952 add_ifidx(drv, br_ifindex, drv->ifindex);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005953
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005954#ifdef CONFIG_LIBNL3_ROUTE
5955 if (bss->added_if_into_bridge) {
5956 drv->rtnl_sk = nl_socket_alloc();
5957 if (drv->rtnl_sk == NULL) {
5958 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate nl_sock");
5959 goto failed;
5960 }
5961
5962 if (nl_connect(drv->rtnl_sk, NETLINK_ROUTE)) {
5963 wpa_printf(MSG_ERROR, "nl80211: Failed to connect nl_sock to NETLINK_ROUTE: %s",
5964 strerror(errno));
5965 goto failed;
5966 }
5967 }
5968#endif /* CONFIG_LIBNL3_ROUTE */
5969
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005970 drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
5971 if (drv->eapol_sock < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005972 wpa_printf(MSG_ERROR, "nl80211: socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE) failed: %s",
5973 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005974 goto failed;
5975 }
5976
5977 if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
5978 {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005979 wpa_printf(MSG_INFO, "nl80211: Could not register read socket for eapol");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005980 goto failed;
5981 }
5982
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005983 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
5984 params->own_addr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005985 goto failed;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005986 os_memcpy(drv->perm_addr, params->own_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005987
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005988 memcpy(bss->addr, params->own_addr, ETH_ALEN);
5989
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005990 return bss;
5991
5992failed:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005993 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005994 return NULL;
5995}
5996
5997
5998static void i802_deinit(void *priv)
5999{
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006000 struct i802_bss *bss = priv;
6001 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006002}
6003
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006004
6005static enum nl80211_iftype wpa_driver_nl80211_if_type(
6006 enum wpa_driver_if_type type)
6007{
6008 switch (type) {
6009 case WPA_IF_STATION:
6010 return NL80211_IFTYPE_STATION;
6011 case WPA_IF_P2P_CLIENT:
6012 case WPA_IF_P2P_GROUP:
6013 return NL80211_IFTYPE_P2P_CLIENT;
6014 case WPA_IF_AP_VLAN:
6015 return NL80211_IFTYPE_AP_VLAN;
6016 case WPA_IF_AP_BSS:
6017 return NL80211_IFTYPE_AP;
6018 case WPA_IF_P2P_GO:
6019 return NL80211_IFTYPE_P2P_GO;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006020 case WPA_IF_P2P_DEVICE:
6021 return NL80211_IFTYPE_P2P_DEVICE;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006022 case WPA_IF_MESH:
6023 return NL80211_IFTYPE_MESH_POINT;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006024 default:
6025 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006026 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006027}
6028
6029
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006030static int nl80211_addr_in_use(struct nl80211_global *global, const u8 *addr)
6031{
6032 struct wpa_driver_nl80211_data *drv;
6033 dl_list_for_each(drv, &global->interfaces,
6034 struct wpa_driver_nl80211_data, list) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006035 if (os_memcmp(addr, drv->first_bss->addr, ETH_ALEN) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006036 return 1;
6037 }
6038 return 0;
6039}
6040
6041
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006042static int nl80211_vif_addr(struct wpa_driver_nl80211_data *drv, u8 *new_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006043{
6044 unsigned int idx;
6045
6046 if (!drv->global)
6047 return -1;
6048
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006049 os_memcpy(new_addr, drv->first_bss->addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006050 for (idx = 0; idx < 64; idx++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006051 new_addr[0] = drv->first_bss->addr[0] | 0x02;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006052 new_addr[0] ^= idx << 2;
6053 if (!nl80211_addr_in_use(drv->global, new_addr))
6054 break;
6055 }
6056 if (idx == 64)
6057 return -1;
6058
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006059 wpa_printf(MSG_DEBUG, "nl80211: Assigned new virtual interface address "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006060 MACSTR, MAC2STR(new_addr));
6061
6062 return 0;
6063}
6064
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006065
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006066struct wdev_info {
6067 u64 wdev_id;
6068 int wdev_id_set;
6069 u8 macaddr[ETH_ALEN];
6070};
6071
6072static int nl80211_wdev_handler(struct nl_msg *msg, void *arg)
6073{
6074 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6075 struct nlattr *tb[NL80211_ATTR_MAX + 1];
6076 struct wdev_info *wi = arg;
6077
6078 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6079 genlmsg_attrlen(gnlh, 0), NULL);
6080 if (tb[NL80211_ATTR_WDEV]) {
6081 wi->wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
6082 wi->wdev_id_set = 1;
6083 }
6084
6085 if (tb[NL80211_ATTR_MAC])
6086 os_memcpy(wi->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
6087 ETH_ALEN);
6088
6089 return NL_SKIP;
6090}
6091
6092
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006093static int wpa_driver_nl80211_if_add(void *priv, enum wpa_driver_if_type type,
6094 const char *ifname, const u8 *addr,
6095 void *bss_ctx, void **drv_priv,
6096 char *force_ifname, u8 *if_addr,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006097 const char *bridge, int use_existing,
6098 int setup_ap)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006099{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006100 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006101 struct i802_bss *bss = priv;
6102 struct wpa_driver_nl80211_data *drv = bss->drv;
6103 int ifidx;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006104 int added = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006105
6106 if (addr)
6107 os_memcpy(if_addr, addr, ETH_ALEN);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006108 nlmode = wpa_driver_nl80211_if_type(type);
6109 if (nlmode == NL80211_IFTYPE_P2P_DEVICE) {
6110 struct wdev_info p2pdev_info;
6111
6112 os_memset(&p2pdev_info, 0, sizeof(p2pdev_info));
6113 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
6114 0, nl80211_wdev_handler,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006115 &p2pdev_info, use_existing);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006116 if (!p2pdev_info.wdev_id_set || ifidx != 0) {
6117 wpa_printf(MSG_ERROR, "nl80211: Failed to create a P2P Device interface %s",
6118 ifname);
6119 return -1;
6120 }
6121
6122 drv->global->if_add_wdevid = p2pdev_info.wdev_id;
6123 drv->global->if_add_wdevid_set = p2pdev_info.wdev_id_set;
6124 if (!is_zero_ether_addr(p2pdev_info.macaddr))
6125 os_memcpy(if_addr, p2pdev_info.macaddr, ETH_ALEN);
6126 wpa_printf(MSG_DEBUG, "nl80211: New P2P Device interface %s (0x%llx) created",
6127 ifname,
6128 (long long unsigned int) p2pdev_info.wdev_id);
6129 } else {
6130 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006131 0, NULL, NULL, use_existing);
6132 if (use_existing && ifidx == -ENFILE) {
6133 added = 0;
6134 ifidx = if_nametoindex(ifname);
6135 } else if (ifidx < 0) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006136 return -1;
6137 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006138 }
6139
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006140 if (!addr) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07006141 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006142 os_memcpy(if_addr, bss->addr, ETH_ALEN);
6143 else if (linux_get_ifhwaddr(drv->global->ioctl_sock,
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07006144 ifname, if_addr) < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006145 if (added)
6146 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006147 return -1;
6148 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006149 }
6150
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006151 if (!addr &&
6152 (type == WPA_IF_P2P_CLIENT || type == WPA_IF_P2P_GROUP ||
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07006153 type == WPA_IF_P2P_GO || type == WPA_IF_MESH ||
6154 type == WPA_IF_STATION)) {
6155 /* Enforce unique address */
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006156 u8 new_addr[ETH_ALEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006157
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006158 if (linux_get_ifhwaddr(drv->global->ioctl_sock, ifname,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006159 new_addr) < 0) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07006160 if (added)
6161 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006162 return -1;
6163 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006164 if (nl80211_addr_in_use(drv->global, new_addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006165 wpa_printf(MSG_DEBUG, "nl80211: Allocate new address "
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07006166 "for interface %s type %d", ifname, type);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006167 if (nl80211_vif_addr(drv, new_addr) < 0) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07006168 if (added)
6169 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006170 return -1;
6171 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006172 if (linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006173 new_addr) < 0) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07006174 if (added)
6175 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006176 return -1;
6177 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006178 }
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07006179 os_memcpy(if_addr, new_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006180 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006181
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006182 if (type == WPA_IF_AP_BSS && setup_ap) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006183 struct i802_bss *new_bss = os_zalloc(sizeof(*new_bss));
6184 if (new_bss == NULL) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006185 if (added)
6186 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006187 return -1;
6188 }
6189
6190 if (bridge &&
6191 i802_check_bridge(drv, new_bss, bridge, ifname) < 0) {
6192 wpa_printf(MSG_ERROR, "nl80211: Failed to add the new "
6193 "interface %s to a bridge %s",
6194 ifname, bridge);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006195 if (added)
6196 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006197 os_free(new_bss);
6198 return -1;
6199 }
6200
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006201 if (linux_set_iface_flags(drv->global->ioctl_sock, ifname, 1))
6202 {
Dmitry Shmidt71757432014-06-02 13:50:35 -07006203 if (added)
6204 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006205 os_free(new_bss);
6206 return -1;
6207 }
6208 os_strlcpy(new_bss->ifname, ifname, IFNAMSIZ);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006209 os_memcpy(new_bss->addr, if_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006210 new_bss->ifindex = ifidx;
6211 new_bss->drv = drv;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006212 new_bss->next = drv->first_bss->next;
6213 new_bss->freq = drv->first_bss->freq;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006214 new_bss->ctx = bss_ctx;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006215 new_bss->added_if = added;
6216 drv->first_bss->next = new_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006217 if (drv_priv)
6218 *drv_priv = new_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006219 nl80211_init_bss(new_bss);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006220
6221 /* Subscribe management frames for this WPA_IF_AP_BSS */
6222 if (nl80211_setup_ap(new_bss))
6223 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006224 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006225
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006226 if (drv->global)
6227 drv->global->if_add_ifindex = ifidx;
6228
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006229 /*
6230 * Some virtual interfaces need to process EAPOL packets and events on
6231 * the parent interface. This is used mainly with hostapd.
6232 */
6233 if (ifidx > 0 &&
6234 (drv->hostapd ||
6235 nlmode == NL80211_IFTYPE_AP_VLAN ||
6236 nlmode == NL80211_IFTYPE_WDS ||
6237 nlmode == NL80211_IFTYPE_MONITOR))
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006238 add_ifidx(drv, ifidx, IFIDX_ANY);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006239
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006240 return 0;
6241}
6242
6243
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006244static int wpa_driver_nl80211_if_remove(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006245 enum wpa_driver_if_type type,
6246 const char *ifname)
6247{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006248 struct wpa_driver_nl80211_data *drv = bss->drv;
6249 int ifindex = if_nametoindex(ifname);
6250
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006251 wpa_printf(MSG_DEBUG, "nl80211: %s(type=%d ifname=%s) ifindex=%d added_if=%d",
6252 __func__, type, ifname, ifindex, bss->added_if);
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08006253 if (ifindex > 0 && (bss->added_if || bss->ifindex != ifindex))
Dmitry Shmidt051af732013-10-22 13:52:46 -07006254 nl80211_remove_iface(drv, ifindex);
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07006255 else if (ifindex > 0 && !bss->added_if) {
6256 struct wpa_driver_nl80211_data *drv2;
6257 dl_list_for_each(drv2, &drv->global->interfaces,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006258 struct wpa_driver_nl80211_data, list) {
6259 del_ifidx(drv2, ifindex, IFIDX_ANY);
6260 del_ifidx(drv2, IFIDX_ANY, ifindex);
6261 }
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07006262 }
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006263
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006264 if (type != WPA_IF_AP_BSS)
6265 return 0;
6266
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006267 if (bss->added_if_into_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006268 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
6269 bss->ifname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006270 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
6271 "interface %s from bridge %s: %s",
6272 bss->ifname, bss->brname, strerror(errno));
6273 }
6274 if (bss->added_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006275 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006276 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
6277 "bridge %s: %s",
6278 bss->brname, strerror(errno));
6279 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006280
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006281 if (bss != drv->first_bss) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006282 struct i802_bss *tbss;
6283
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006284 wpa_printf(MSG_DEBUG, "nl80211: Not the first BSS - remove it");
6285 for (tbss = drv->first_bss; tbss; tbss = tbss->next) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006286 if (tbss->next == bss) {
6287 tbss->next = bss->next;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006288 /* Unsubscribe management frames */
6289 nl80211_teardown_ap(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006290 nl80211_destroy_bss(bss);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006291 if (!bss->added_if)
6292 i802_set_iface_flags(bss, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006293 os_free(bss);
6294 bss = NULL;
6295 break;
6296 }
6297 }
6298 if (bss)
6299 wpa_printf(MSG_INFO, "nl80211: %s - could not find "
6300 "BSS %p in the list", __func__, bss);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006301 } else {
6302 wpa_printf(MSG_DEBUG, "nl80211: First BSS - reassign context");
6303 nl80211_teardown_ap(bss);
6304 if (!bss->added_if && !drv->first_bss->next)
6305 wpa_driver_nl80211_del_beacon(drv);
6306 nl80211_destroy_bss(bss);
6307 if (!bss->added_if)
6308 i802_set_iface_flags(bss, 0);
6309 if (drv->first_bss->next) {
6310 drv->first_bss = drv->first_bss->next;
6311 drv->ctx = drv->first_bss->ctx;
6312 os_free(bss);
6313 } else {
6314 wpa_printf(MSG_DEBUG, "nl80211: No second BSS to reassign context to");
6315 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006316 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006317
6318 return 0;
6319}
6320
6321
6322static int cookie_handler(struct nl_msg *msg, void *arg)
6323{
6324 struct nlattr *tb[NL80211_ATTR_MAX + 1];
6325 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6326 u64 *cookie = arg;
6327 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6328 genlmsg_attrlen(gnlh, 0), NULL);
6329 if (tb[NL80211_ATTR_COOKIE])
6330 *cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
6331 return NL_SKIP;
6332}
6333
6334
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006335static int nl80211_send_frame_cmd(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006336 unsigned int freq, unsigned int wait,
6337 const u8 *buf, size_t buf_len,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006338 u64 *cookie_out, int no_cck, int no_ack,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006339 int offchanok, const u16 *csa_offs,
6340 size_t csa_offs_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006341{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006342 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006343 struct nl_msg *msg;
6344 u64 cookie;
6345 int ret = -1;
6346
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006347 wpa_printf(MSG_MSGDUMP, "nl80211: CMD_FRAME freq=%u wait=%u no_cck=%d "
Dmitry Shmidt04949592012-07-19 12:16:46 -07006348 "no_ack=%d offchanok=%d",
6349 freq, wait, no_cck, no_ack, offchanok);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006350 wpa_hexdump(MSG_MSGDUMP, "CMD_FRAME", buf, buf_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006351
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006352 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_FRAME)) ||
6353 (freq && nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
6354 (wait && nla_put_u32(msg, NL80211_ATTR_DURATION, wait)) ||
6355 (offchanok && ((drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
6356 drv->test_use_roc_tx) &&
6357 nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK)) ||
6358 (no_cck && nla_put_flag(msg, NL80211_ATTR_TX_NO_CCK_RATE)) ||
6359 (no_ack && nla_put_flag(msg, NL80211_ATTR_DONT_WAIT_FOR_ACK)) ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006360 (csa_offs && nla_put(msg, NL80211_ATTR_CSA_C_OFFSETS_TX,
6361 csa_offs_len * sizeof(u16), csa_offs)) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006362 nla_put(msg, NL80211_ATTR_FRAME, buf_len, buf))
6363 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006364
6365 cookie = 0;
6366 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
6367 msg = NULL;
6368 if (ret) {
6369 wpa_printf(MSG_DEBUG, "nl80211: Frame command failed: ret=%d "
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07006370 "(%s) (freq=%u wait=%u)", ret, strerror(-ret),
6371 freq, wait);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006372 } else {
6373 wpa_printf(MSG_MSGDUMP, "nl80211: Frame TX command accepted%s; "
6374 "cookie 0x%llx", no_ack ? " (no ACK)" : "",
6375 (long long unsigned int) cookie);
6376
6377 if (cookie_out)
6378 *cookie_out = no_ack ? (u64) -1 : cookie;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006379
6380 if (drv->num_send_action_cookies == MAX_SEND_ACTION_COOKIES) {
6381 wpa_printf(MSG_DEBUG,
6382 "nl80211: Drop oldest pending send action cookie 0x%llx",
6383 (long long unsigned int)
6384 drv->send_action_cookies[0]);
6385 os_memmove(&drv->send_action_cookies[0],
6386 &drv->send_action_cookies[1],
6387 (MAX_SEND_ACTION_COOKIES - 1) *
6388 sizeof(u64));
6389 drv->num_send_action_cookies--;
6390 }
6391 drv->send_action_cookies[drv->num_send_action_cookies] = cookie;
6392 drv->num_send_action_cookies++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006393 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006394
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006395fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006396 nlmsg_free(msg);
6397 return ret;
6398}
6399
6400
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006401static int wpa_driver_nl80211_send_action(struct i802_bss *bss,
6402 unsigned int freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006403 unsigned int wait_time,
6404 const u8 *dst, const u8 *src,
6405 const u8 *bssid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006406 const u8 *data, size_t data_len,
6407 int no_cck)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006408{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006409 struct wpa_driver_nl80211_data *drv = bss->drv;
6410 int ret = -1;
6411 u8 *buf;
6412 struct ieee80211_hdr *hdr;
6413
6414 wpa_printf(MSG_DEBUG, "nl80211: Send Action frame (ifindex=%d, "
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006415 "freq=%u MHz wait=%d ms no_cck=%d)",
6416 drv->ifindex, freq, wait_time, no_cck);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006417
6418 buf = os_zalloc(24 + data_len);
6419 if (buf == NULL)
6420 return ret;
6421 os_memcpy(buf + 24, data, data_len);
6422 hdr = (struct ieee80211_hdr *) buf;
6423 hdr->frame_control =
6424 IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_ACTION);
6425 os_memcpy(hdr->addr1, dst, ETH_ALEN);
6426 os_memcpy(hdr->addr2, src, ETH_ALEN);
6427 os_memcpy(hdr->addr3, bssid, ETH_ALEN);
6428
Dmitry Shmidt56052862013-10-04 10:23:25 -07006429 if (is_ap_interface(drv->nlmode) &&
6430 (!(drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
6431 (int) freq == bss->freq || drv->device_ap_sme ||
6432 !drv->use_monitor))
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006433 ret = wpa_driver_nl80211_send_mlme(bss, buf, 24 + data_len,
6434 0, freq, no_cck, 1,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006435 wait_time, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006436 else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006437 ret = nl80211_send_frame_cmd(bss, freq, wait_time, buf,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006438 24 + data_len,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006439 &drv->send_action_cookie,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006440 no_cck, 0, 1, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006441
6442 os_free(buf);
6443 return ret;
6444}
6445
6446
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006447static void nl80211_frame_wait_cancel(struct i802_bss *bss, u64 cookie)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006448{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006449 struct wpa_driver_nl80211_data *drv = bss->drv;
6450 struct nl_msg *msg;
6451 int ret;
6452
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08006453 wpa_printf(MSG_DEBUG, "nl80211: Cancel TX frame wait: cookie=0x%llx",
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006454 (long long unsigned int) cookie);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006455 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_FRAME_WAIT_CANCEL)) ||
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006456 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006457 nlmsg_free(msg);
6458 return;
6459 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006460
6461 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006462 if (ret)
6463 wpa_printf(MSG_DEBUG, "nl80211: wait cancel failed: ret=%d "
6464 "(%s)", ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006465}
6466
6467
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006468static void wpa_driver_nl80211_send_action_cancel_wait(void *priv)
6469{
6470 struct i802_bss *bss = priv;
6471 struct wpa_driver_nl80211_data *drv = bss->drv;
6472 unsigned int i;
6473 u64 cookie;
6474
6475 /* Cancel the last pending TX cookie */
6476 nl80211_frame_wait_cancel(bss, drv->send_action_cookie);
6477
6478 /*
6479 * Cancel the other pending TX cookies, if any. This is needed since
6480 * the driver may keep a list of all pending offchannel TX operations
6481 * and free up the radio only once they have expired or cancelled.
6482 */
6483 for (i = drv->num_send_action_cookies; i > 0; i--) {
6484 cookie = drv->send_action_cookies[i - 1];
6485 if (cookie != drv->send_action_cookie)
6486 nl80211_frame_wait_cancel(bss, cookie);
6487 }
6488 drv->num_send_action_cookies = 0;
6489}
6490
6491
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006492static int wpa_driver_nl80211_remain_on_channel(void *priv, unsigned int freq,
6493 unsigned int duration)
6494{
6495 struct i802_bss *bss = priv;
6496 struct wpa_driver_nl80211_data *drv = bss->drv;
6497 struct nl_msg *msg;
6498 int ret;
6499 u64 cookie;
6500
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006501 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_REMAIN_ON_CHANNEL)) ||
6502 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
6503 nla_put_u32(msg, NL80211_ATTR_DURATION, duration)) {
6504 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006505 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006506 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006507
6508 cookie = 0;
6509 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
6510 if (ret == 0) {
6511 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel cookie "
6512 "0x%llx for freq=%u MHz duration=%u",
6513 (long long unsigned int) cookie, freq, duration);
6514 drv->remain_on_chan_cookie = cookie;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006515 drv->pending_remain_on_chan = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006516 return 0;
6517 }
6518 wpa_printf(MSG_DEBUG, "nl80211: Failed to request remain-on-channel "
6519 "(freq=%d duration=%u): %d (%s)",
6520 freq, duration, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006521 return -1;
6522}
6523
6524
6525static int wpa_driver_nl80211_cancel_remain_on_channel(void *priv)
6526{
6527 struct i802_bss *bss = priv;
6528 struct wpa_driver_nl80211_data *drv = bss->drv;
6529 struct nl_msg *msg;
6530 int ret;
6531
6532 if (!drv->pending_remain_on_chan) {
6533 wpa_printf(MSG_DEBUG, "nl80211: No pending remain-on-channel "
6534 "to cancel");
6535 return -1;
6536 }
6537
6538 wpa_printf(MSG_DEBUG, "nl80211: Cancel remain-on-channel with cookie "
6539 "0x%llx",
6540 (long long unsigned int) drv->remain_on_chan_cookie);
6541
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006542 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL);
6543 if (!msg ||
6544 nla_put_u64(msg, NL80211_ATTR_COOKIE, drv->remain_on_chan_cookie)) {
6545 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006546 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006547 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006548
6549 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6550 if (ret == 0)
6551 return 0;
6552 wpa_printf(MSG_DEBUG, "nl80211: Failed to cancel remain-on-channel: "
6553 "%d (%s)", ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006554 return -1;
6555}
6556
6557
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006558static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss, int report)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006559{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006560 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07006561
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006562 if (!report) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006563 if (bss->nl_preq && drv->device_ap_sme &&
Dmitry Shmidt03658832014-08-13 11:03:49 -07006564 is_ap_interface(drv->nlmode) && !bss->in_deinit &&
6565 !bss->static_ap) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006566 /*
6567 * Do not disable Probe Request reporting that was
6568 * enabled in nl80211_setup_ap().
6569 */
6570 wpa_printf(MSG_DEBUG, "nl80211: Skip disabling of "
6571 "Probe Request reporting nl_preq=%p while "
6572 "in AP mode", bss->nl_preq);
6573 } else if (bss->nl_preq) {
6574 wpa_printf(MSG_DEBUG, "nl80211: Disable Probe Request "
6575 "reporting nl_preq=%p", bss->nl_preq);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006576 nl80211_destroy_eloop_handle(&bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006577 }
6578 return 0;
6579 }
6580
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006581 if (bss->nl_preq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006582 wpa_printf(MSG_DEBUG, "nl80211: Probe Request reporting "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006583 "already on! nl_preq=%p", bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006584 return 0;
6585 }
6586
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006587 bss->nl_preq = nl_create_handle(drv->global->nl_cb, "preq");
6588 if (bss->nl_preq == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006589 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006590 wpa_printf(MSG_DEBUG, "nl80211: Enable Probe Request "
6591 "reporting nl_preq=%p", bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006592
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006593 if (nl80211_register_frame(bss, bss->nl_preq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006594 (WLAN_FC_TYPE_MGMT << 2) |
6595 (WLAN_FC_STYPE_PROBE_REQ << 4),
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006596 NULL, 0) < 0)
6597 goto out_err;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07006598
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006599 nl80211_register_eloop_read(&bss->nl_preq,
6600 wpa_driver_nl80211_event_receive,
6601 bss->nl_cb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006602
6603 return 0;
6604
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006605 out_err:
6606 nl_destroy_handles(&bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006607 return -1;
6608}
6609
6610
6611static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
6612 int ifindex, int disabled)
6613{
6614 struct nl_msg *msg;
6615 struct nlattr *bands, *band;
6616 int ret;
6617
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006618 wpa_printf(MSG_DEBUG,
6619 "nl80211: NL80211_CMD_SET_TX_BITRATE_MASK (ifindex=%d %s)",
6620 ifindex, disabled ? "NL80211_TXRATE_LEGACY=OFDM-only" :
6621 "no NL80211_TXRATE_LEGACY constraint");
6622
6623 msg = nl80211_ifindex_msg(drv, ifindex, 0,
6624 NL80211_CMD_SET_TX_BITRATE_MASK);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006625 if (!msg)
6626 return -1;
6627
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006628 bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES);
6629 if (!bands)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006630 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006631
6632 /*
6633 * Disable 2 GHz rates 1, 2, 5.5, 11 Mbps by masking out everything
6634 * else apart from 6, 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS
6635 * rates. All 5 GHz rates are left enabled.
6636 */
6637 band = nla_nest_start(msg, NL80211_BAND_2GHZ);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006638 if (!band ||
6639 (disabled && nla_put(msg, NL80211_TXRATE_LEGACY, 8,
6640 "\x0c\x12\x18\x24\x30\x48\x60\x6c")))
6641 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006642 nla_nest_end(msg, band);
6643
6644 nla_nest_end(msg, bands);
6645
6646 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006647 if (ret) {
6648 wpa_printf(MSG_DEBUG, "nl80211: Set TX rates failed: ret=%d "
6649 "(%s)", ret, strerror(-ret));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006650 } else
6651 drv->disabled_11b_rates = disabled;
6652
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006653 return ret;
6654
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006655fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006656 nlmsg_free(msg);
6657 return -1;
6658}
6659
6660
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006661static int wpa_driver_nl80211_deinit_ap(void *priv)
6662{
6663 struct i802_bss *bss = priv;
6664 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006665 if (!is_ap_interface(drv->nlmode))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006666 return -1;
6667 wpa_driver_nl80211_del_beacon(drv);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006668 bss->beacon_set = 0;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006669
6670 /*
6671 * If the P2P GO interface was dynamically added, then it is
6672 * possible that the interface change to station is not possible.
6673 */
6674 if (drv->nlmode == NL80211_IFTYPE_P2P_GO && bss->if_dynamic)
6675 return 0;
6676
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006677 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006678}
6679
6680
Dmitry Shmidtea69e842013-05-13 14:52:28 -07006681static int wpa_driver_nl80211_stop_ap(void *priv)
6682{
6683 struct i802_bss *bss = priv;
6684 struct wpa_driver_nl80211_data *drv = bss->drv;
6685 if (!is_ap_interface(drv->nlmode))
6686 return -1;
6687 wpa_driver_nl80211_del_beacon(drv);
6688 bss->beacon_set = 0;
6689 return 0;
6690}
6691
6692
Dmitry Shmidt04949592012-07-19 12:16:46 -07006693static int wpa_driver_nl80211_deinit_p2p_cli(void *priv)
6694{
6695 struct i802_bss *bss = priv;
6696 struct wpa_driver_nl80211_data *drv = bss->drv;
6697 if (drv->nlmode != NL80211_IFTYPE_P2P_CLIENT)
6698 return -1;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006699
6700 /*
6701 * If the P2P Client interface was dynamically added, then it is
6702 * possible that the interface change to station is not possible.
6703 */
6704 if (bss->if_dynamic)
6705 return 0;
6706
Dmitry Shmidt04949592012-07-19 12:16:46 -07006707 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
6708}
6709
6710
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006711static void wpa_driver_nl80211_resume(void *priv)
6712{
6713 struct i802_bss *bss = priv;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006714 enum nl80211_iftype nlmode = nl80211_get_ifmode(bss);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006715
6716 if (i802_set_iface_flags(bss, 1))
6717 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface up on resume event");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006718
6719 if (is_p2p_net_interface(nlmode))
6720 nl80211_disable_11b_rates(bss->drv, bss->drv->ifindex, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006721}
6722
6723
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006724static int nl80211_signal_monitor(void *priv, int threshold, int hysteresis)
6725{
6726 struct i802_bss *bss = priv;
6727 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006728 struct nl_msg *msg;
6729 struct nlattr *cqm;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006730
6731 wpa_printf(MSG_DEBUG, "nl80211: Signal monitor threshold=%d "
6732 "hysteresis=%d", threshold, hysteresis);
6733
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006734 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_CQM)) ||
6735 !(cqm = nla_nest_start(msg, NL80211_ATTR_CQM)) ||
6736 nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THOLD, threshold) ||
6737 nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_HYST, hysteresis)) {
6738 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006739 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006740 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006741 nla_nest_end(msg, cqm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006742
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006743 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006744}
6745
6746
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006747static int get_channel_width(struct nl_msg *msg, void *arg)
6748{
6749 struct nlattr *tb[NL80211_ATTR_MAX + 1];
6750 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6751 struct wpa_signal_info *sig_change = arg;
6752
6753 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6754 genlmsg_attrlen(gnlh, 0), NULL);
6755
6756 sig_change->center_frq1 = -1;
6757 sig_change->center_frq2 = -1;
6758 sig_change->chanwidth = CHAN_WIDTH_UNKNOWN;
6759
6760 if (tb[NL80211_ATTR_CHANNEL_WIDTH]) {
6761 sig_change->chanwidth = convert2width(
6762 nla_get_u32(tb[NL80211_ATTR_CHANNEL_WIDTH]));
6763 if (tb[NL80211_ATTR_CENTER_FREQ1])
6764 sig_change->center_frq1 =
6765 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]);
6766 if (tb[NL80211_ATTR_CENTER_FREQ2])
6767 sig_change->center_frq2 =
6768 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]);
6769 }
6770
6771 return NL_SKIP;
6772}
6773
6774
6775static int nl80211_get_channel_width(struct wpa_driver_nl80211_data *drv,
6776 struct wpa_signal_info *sig)
6777{
6778 struct nl_msg *msg;
6779
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006780 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_GET_INTERFACE);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006781 return send_and_recv_msgs(drv, msg, get_channel_width, sig);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006782}
6783
6784
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006785static int nl80211_signal_poll(void *priv, struct wpa_signal_info *si)
6786{
6787 struct i802_bss *bss = priv;
6788 struct wpa_driver_nl80211_data *drv = bss->drv;
6789 int res;
6790
6791 os_memset(si, 0, sizeof(*si));
6792 res = nl80211_get_link_signal(drv, si);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006793 if (res) {
6794 if (drv->nlmode != NL80211_IFTYPE_ADHOC &&
6795 drv->nlmode != NL80211_IFTYPE_MESH_POINT)
6796 return res;
6797 si->current_signal = 0;
6798 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006799
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006800 res = nl80211_get_channel_width(drv, si);
6801 if (res != 0)
6802 return res;
6803
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006804 return nl80211_get_link_noise(drv, si);
6805}
6806
6807
6808static int nl80211_send_frame(void *priv, const u8 *data, size_t data_len,
6809 int encrypt)
6810{
6811 struct i802_bss *bss = priv;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006812 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt, 0,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006813 0, 0, 0, 0, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006814}
6815
6816
6817static int nl80211_set_param(void *priv, const char *param)
6818{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006819 if (param == NULL)
6820 return 0;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08006821 wpa_printf(MSG_DEBUG, "nl80211: driver param='%s'", param);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006822
6823#ifdef CONFIG_P2P
6824 if (os_strstr(param, "use_p2p_group_interface=1")) {
6825 struct i802_bss *bss = priv;
6826 struct wpa_driver_nl80211_data *drv = bss->drv;
6827
6828 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
6829 "interface");
6830 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
6831 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
6832 }
6833#endif /* CONFIG_P2P */
6834
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006835 if (os_strstr(param, "use_monitor=1")) {
6836 struct i802_bss *bss = priv;
6837 struct wpa_driver_nl80211_data *drv = bss->drv;
6838 drv->use_monitor = 1;
6839 }
6840
6841 if (os_strstr(param, "force_connect_cmd=1")) {
6842 struct i802_bss *bss = priv;
6843 struct wpa_driver_nl80211_data *drv = bss->drv;
6844 drv->capa.flags &= ~WPA_DRIVER_FLAGS_SME;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006845 drv->force_connect_cmd = 1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006846 }
6847
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08006848 if (os_strstr(param, "no_offchannel_tx=1")) {
6849 struct i802_bss *bss = priv;
6850 struct wpa_driver_nl80211_data *drv = bss->drv;
6851 drv->capa.flags &= ~WPA_DRIVER_FLAGS_OFFCHANNEL_TX;
6852 drv->test_use_roc_tx = 1;
6853 }
6854
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006855 return 0;
6856}
6857
6858
6859static void * nl80211_global_init(void)
6860{
6861 struct nl80211_global *global;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006862 struct netlink_config *cfg;
6863
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006864 global = os_zalloc(sizeof(*global));
6865 if (global == NULL)
6866 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006867 global->ioctl_sock = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006868 dl_list_init(&global->interfaces);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006869 global->if_add_ifindex = -1;
6870
6871 cfg = os_zalloc(sizeof(*cfg));
6872 if (cfg == NULL)
6873 goto err;
6874
6875 cfg->ctx = global;
6876 cfg->newlink_cb = wpa_driver_nl80211_event_rtm_newlink;
6877 cfg->dellink_cb = wpa_driver_nl80211_event_rtm_dellink;
6878 global->netlink = netlink_init(cfg);
6879 if (global->netlink == NULL) {
6880 os_free(cfg);
6881 goto err;
6882 }
6883
6884 if (wpa_driver_nl80211_init_nl_global(global) < 0)
6885 goto err;
6886
6887 global->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
6888 if (global->ioctl_sock < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006889 wpa_printf(MSG_ERROR, "nl80211: socket(PF_INET,SOCK_DGRAM) failed: %s",
6890 strerror(errno));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006891 goto err;
6892 }
6893
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006894 return global;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006895
6896err:
6897 nl80211_global_deinit(global);
6898 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006899}
6900
6901
6902static void nl80211_global_deinit(void *priv)
6903{
6904 struct nl80211_global *global = priv;
6905 if (global == NULL)
6906 return;
6907 if (!dl_list_empty(&global->interfaces)) {
6908 wpa_printf(MSG_ERROR, "nl80211: %u interface(s) remain at "
6909 "nl80211_global_deinit",
6910 dl_list_len(&global->interfaces));
6911 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006912
6913 if (global->netlink)
6914 netlink_deinit(global->netlink);
6915
6916 nl_destroy_handles(&global->nl);
6917
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006918 if (global->nl_event)
6919 nl80211_destroy_eloop_handle(&global->nl_event);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006920
6921 nl_cb_put(global->nl_cb);
6922
6923 if (global->ioctl_sock >= 0)
6924 close(global->ioctl_sock);
6925
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006926 os_free(global);
6927}
6928
6929
6930static const char * nl80211_get_radio_name(void *priv)
6931{
6932 struct i802_bss *bss = priv;
6933 struct wpa_driver_nl80211_data *drv = bss->drv;
6934 return drv->phyname;
6935}
6936
6937
Jouni Malinen75ecf522011-06-27 15:19:46 -07006938static int nl80211_pmkid(struct i802_bss *bss, int cmd, const u8 *bssid,
6939 const u8 *pmkid)
6940{
6941 struct nl_msg *msg;
6942
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006943 if (!(msg = nl80211_bss_msg(bss, 0, cmd)) ||
6944 (pmkid && nla_put(msg, NL80211_ATTR_PMKID, 16, pmkid)) ||
6945 (bssid && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))) {
6946 nlmsg_free(msg);
6947 return -ENOBUFS;
6948 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07006949
6950 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
Jouni Malinen75ecf522011-06-27 15:19:46 -07006951}
6952
6953
6954static int nl80211_add_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
6955{
6956 struct i802_bss *bss = priv;
6957 wpa_printf(MSG_DEBUG, "nl80211: Add PMKID for " MACSTR, MAC2STR(bssid));
6958 return nl80211_pmkid(bss, NL80211_CMD_SET_PMKSA, bssid, pmkid);
6959}
6960
6961
6962static int nl80211_remove_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
6963{
6964 struct i802_bss *bss = priv;
6965 wpa_printf(MSG_DEBUG, "nl80211: Delete PMKID for " MACSTR,
6966 MAC2STR(bssid));
6967 return nl80211_pmkid(bss, NL80211_CMD_DEL_PMKSA, bssid, pmkid);
6968}
6969
6970
6971static int nl80211_flush_pmkid(void *priv)
6972{
6973 struct i802_bss *bss = priv;
6974 wpa_printf(MSG_DEBUG, "nl80211: Flush PMKIDs");
6975 return nl80211_pmkid(bss, NL80211_CMD_FLUSH_PMKSA, NULL, NULL);
6976}
6977
6978
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006979static void clean_survey_results(struct survey_results *survey_results)
6980{
6981 struct freq_survey *survey, *tmp;
6982
6983 if (dl_list_empty(&survey_results->survey_list))
6984 return;
6985
6986 dl_list_for_each_safe(survey, tmp, &survey_results->survey_list,
6987 struct freq_survey, list) {
6988 dl_list_del(&survey->list);
6989 os_free(survey);
6990 }
6991}
6992
6993
6994static void add_survey(struct nlattr **sinfo, u32 ifidx,
6995 struct dl_list *survey_list)
6996{
6997 struct freq_survey *survey;
6998
6999 survey = os_zalloc(sizeof(struct freq_survey));
7000 if (!survey)
7001 return;
7002
7003 survey->ifidx = ifidx;
7004 survey->freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
7005 survey->filled = 0;
7006
7007 if (sinfo[NL80211_SURVEY_INFO_NOISE]) {
7008 survey->nf = (int8_t)
7009 nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
7010 survey->filled |= SURVEY_HAS_NF;
7011 }
7012
7013 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]) {
7014 survey->channel_time =
7015 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]);
7016 survey->filled |= SURVEY_HAS_CHAN_TIME;
7017 }
7018
7019 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]) {
7020 survey->channel_time_busy =
7021 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]);
7022 survey->filled |= SURVEY_HAS_CHAN_TIME_BUSY;
7023 }
7024
7025 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]) {
7026 survey->channel_time_rx =
7027 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]);
7028 survey->filled |= SURVEY_HAS_CHAN_TIME_RX;
7029 }
7030
7031 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]) {
7032 survey->channel_time_tx =
7033 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]);
7034 survey->filled |= SURVEY_HAS_CHAN_TIME_TX;
7035 }
7036
7037 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)",
7038 survey->freq,
7039 survey->nf,
7040 (unsigned long int) survey->channel_time,
7041 (unsigned long int) survey->channel_time_busy,
7042 (unsigned long int) survey->channel_time_tx,
7043 (unsigned long int) survey->channel_time_rx,
7044 survey->filled);
7045
7046 dl_list_add_tail(survey_list, &survey->list);
7047}
7048
7049
7050static int check_survey_ok(struct nlattr **sinfo, u32 surveyed_freq,
7051 unsigned int freq_filter)
7052{
7053 if (!freq_filter)
7054 return 1;
7055
7056 return freq_filter == surveyed_freq;
7057}
7058
7059
7060static int survey_handler(struct nl_msg *msg, void *arg)
7061{
7062 struct nlattr *tb[NL80211_ATTR_MAX + 1];
7063 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
7064 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
7065 struct survey_results *survey_results;
7066 u32 surveyed_freq = 0;
7067 u32 ifidx;
7068
7069 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
7070 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
7071 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
7072 };
7073
7074 survey_results = (struct survey_results *) arg;
7075
7076 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
7077 genlmsg_attrlen(gnlh, 0), NULL);
7078
Dmitry Shmidt97672262014-02-03 13:02:54 -08007079 if (!tb[NL80211_ATTR_IFINDEX])
7080 return NL_SKIP;
7081
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007082 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
7083
7084 if (!tb[NL80211_ATTR_SURVEY_INFO])
7085 return NL_SKIP;
7086
7087 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
7088 tb[NL80211_ATTR_SURVEY_INFO],
7089 survey_policy))
7090 return NL_SKIP;
7091
7092 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY]) {
7093 wpa_printf(MSG_ERROR, "nl80211: Invalid survey data");
7094 return NL_SKIP;
7095 }
7096
7097 surveyed_freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
7098
7099 if (!check_survey_ok(sinfo, surveyed_freq,
7100 survey_results->freq_filter))
7101 return NL_SKIP;
7102
7103 if (survey_results->freq_filter &&
7104 survey_results->freq_filter != surveyed_freq) {
7105 wpa_printf(MSG_EXCESSIVE, "nl80211: Ignoring survey data for freq %d MHz",
7106 surveyed_freq);
7107 return NL_SKIP;
7108 }
7109
7110 add_survey(sinfo, ifidx, &survey_results->survey_list);
7111
7112 return NL_SKIP;
7113}
7114
7115
7116static int wpa_driver_nl80211_get_survey(void *priv, unsigned int freq)
7117{
7118 struct i802_bss *bss = priv;
7119 struct wpa_driver_nl80211_data *drv = bss->drv;
7120 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007121 int err;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007122 union wpa_event_data data;
7123 struct survey_results *survey_results;
7124
7125 os_memset(&data, 0, sizeof(data));
7126 survey_results = &data.survey_results;
7127
7128 dl_list_init(&survey_results->survey_list);
7129
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007130 msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007131 if (!msg)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007132 return -ENOBUFS;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007133
7134 if (freq)
7135 data.survey_results.freq_filter = freq;
7136
7137 do {
7138 wpa_printf(MSG_DEBUG, "nl80211: Fetch survey data");
7139 err = send_and_recv_msgs(drv, msg, survey_handler,
7140 survey_results);
7141 } while (err > 0);
7142
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007143 if (err)
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007144 wpa_printf(MSG_ERROR, "nl80211: Failed to process survey data");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007145 else
7146 wpa_supplicant_event(drv->ctx, EVENT_SURVEY, &data);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007147
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007148 clean_survey_results(survey_results);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007149 return err;
7150}
7151
7152
Dmitry Shmidt807291d2015-01-27 13:40:23 -08007153static void nl80211_set_rekey_info(void *priv, const u8 *kek, size_t kek_len,
7154 const u8 *kck, size_t kck_len,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007155 const u8 *replay_ctr)
7156{
7157 struct i802_bss *bss = priv;
7158 struct wpa_driver_nl80211_data *drv = bss->drv;
7159 struct nlattr *replay_nested;
7160 struct nl_msg *msg;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08007161 int ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007162
Dmitry Shmidtff787d52015-01-12 13:01:47 -08007163 if (!drv->set_rekey_offload)
7164 return;
7165
7166 wpa_printf(MSG_DEBUG, "nl80211: Set rekey offload");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007167 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_REKEY_OFFLOAD)) ||
7168 !(replay_nested = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA)) ||
Dmitry Shmidt807291d2015-01-27 13:40:23 -08007169 nla_put(msg, NL80211_REKEY_DATA_KEK, kek_len, kek) ||
7170 nla_put(msg, NL80211_REKEY_DATA_KCK, kck_len, kck) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007171 nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR, NL80211_REPLAY_CTR_LEN,
7172 replay_ctr)) {
7173 nl80211_nlmsg_clear(msg);
7174 nlmsg_free(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007175 return;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007176 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007177
7178 nla_nest_end(msg, replay_nested);
7179
Dmitry Shmidtff787d52015-01-12 13:01:47 -08007180 ret = send_and_recv_msgs(drv, msg, NULL, (void *) -1);
7181 if (ret == -EOPNOTSUPP) {
7182 wpa_printf(MSG_DEBUG,
7183 "nl80211: Driver does not support rekey offload");
7184 drv->set_rekey_offload = 0;
7185 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007186}
7187
7188
7189static void nl80211_send_null_frame(struct i802_bss *bss, const u8 *own_addr,
7190 const u8 *addr, int qos)
7191{
7192 /* send data frame to poll STA and check whether
7193 * this frame is ACKed */
7194 struct {
7195 struct ieee80211_hdr hdr;
7196 u16 qos_ctl;
7197 } STRUCT_PACKED nulldata;
7198 size_t size;
7199
7200 /* Send data frame to poll STA and check whether this frame is ACKed */
7201
7202 os_memset(&nulldata, 0, sizeof(nulldata));
7203
7204 if (qos) {
7205 nulldata.hdr.frame_control =
7206 IEEE80211_FC(WLAN_FC_TYPE_DATA,
7207 WLAN_FC_STYPE_QOS_NULL);
7208 size = sizeof(nulldata);
7209 } else {
7210 nulldata.hdr.frame_control =
7211 IEEE80211_FC(WLAN_FC_TYPE_DATA,
7212 WLAN_FC_STYPE_NULLFUNC);
7213 size = sizeof(struct ieee80211_hdr);
7214 }
7215
7216 nulldata.hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS);
7217 os_memcpy(nulldata.hdr.IEEE80211_DA_FROMDS, addr, ETH_ALEN);
7218 os_memcpy(nulldata.hdr.IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
7219 os_memcpy(nulldata.hdr.IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
7220
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007221 if (wpa_driver_nl80211_send_mlme(bss, (u8 *) &nulldata, size, 0, 0, 0,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007222 0, 0, NULL, 0) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007223 wpa_printf(MSG_DEBUG, "nl80211_send_null_frame: Failed to "
7224 "send poll frame");
7225}
7226
7227static void nl80211_poll_client(void *priv, const u8 *own_addr, const u8 *addr,
7228 int qos)
7229{
7230 struct i802_bss *bss = priv;
7231 struct wpa_driver_nl80211_data *drv = bss->drv;
7232 struct nl_msg *msg;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007233 int ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007234
7235 if (!drv->poll_command_supported) {
7236 nl80211_send_null_frame(bss, own_addr, addr, qos);
7237 return;
7238 }
7239
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007240 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_PROBE_CLIENT)) ||
7241 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
7242 nlmsg_free(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007243 return;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007244 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007245
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007246 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7247 if (ret < 0) {
7248 wpa_printf(MSG_DEBUG, "nl80211: Client probe request for "
7249 MACSTR " failed: ret=%d (%s)",
7250 MAC2STR(addr), ret, strerror(-ret));
7251 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007252}
7253
7254
7255static int nl80211_set_power_save(struct i802_bss *bss, int enabled)
7256{
7257 struct nl_msg *msg;
7258
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007259 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_POWER_SAVE)) ||
7260 nla_put_u32(msg, NL80211_ATTR_PS_STATE,
7261 enabled ? NL80211_PS_ENABLED : NL80211_PS_DISABLED)) {
7262 nlmsg_free(msg);
7263 return -ENOBUFS;
7264 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007265 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007266}
7267
7268
7269static int nl80211_set_p2p_powersave(void *priv, int legacy_ps, int opp_ps,
7270 int ctwindow)
7271{
7272 struct i802_bss *bss = priv;
7273
7274 wpa_printf(MSG_DEBUG, "nl80211: set_p2p_powersave (legacy_ps=%d "
7275 "opp_ps=%d ctwindow=%d)", legacy_ps, opp_ps, ctwindow);
7276
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08007277 if (opp_ps != -1 || ctwindow != -1) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007278#ifdef ANDROID_P2P
7279 wpa_driver_set_p2p_ps(priv, legacy_ps, opp_ps, ctwindow);
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08007280#else /* ANDROID_P2P */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007281 return -1; /* Not yet supported */
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08007282#endif /* ANDROID_P2P */
7283 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007284
7285 if (legacy_ps == -1)
7286 return 0;
7287 if (legacy_ps != 0 && legacy_ps != 1)
7288 return -1; /* Not yet supported */
7289
7290 return nl80211_set_power_save(bss, legacy_ps);
7291}
7292
7293
Dmitry Shmidt051af732013-10-22 13:52:46 -07007294static int nl80211_start_radar_detection(void *priv,
7295 struct hostapd_freq_params *freq)
Dmitry Shmidtea69e842013-05-13 14:52:28 -07007296{
7297 struct i802_bss *bss = priv;
7298 struct wpa_driver_nl80211_data *drv = bss->drv;
7299 struct nl_msg *msg;
7300 int ret;
7301
Dmitry Shmidt051af732013-10-22 13:52:46 -07007302 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)",
7303 freq->freq, freq->ht_enabled, freq->vht_enabled,
7304 freq->bandwidth, freq->center_freq1, freq->center_freq2);
7305
Dmitry Shmidtea69e842013-05-13 14:52:28 -07007306 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_RADAR)) {
7307 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support radar "
7308 "detection");
7309 return -1;
7310 }
7311
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007312 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_RADAR_DETECT)) ||
7313 nl80211_put_freq_params(msg, freq) < 0) {
7314 nlmsg_free(msg);
Dmitry Shmidtea69e842013-05-13 14:52:28 -07007315 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007316 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -07007317
7318 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7319 if (ret == 0)
7320 return 0;
7321 wpa_printf(MSG_DEBUG, "nl80211: Failed to start radar detection: "
7322 "%d (%s)", ret, strerror(-ret));
Dmitry Shmidtea69e842013-05-13 14:52:28 -07007323 return -1;
7324}
7325
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007326#ifdef CONFIG_TDLS
7327
7328static int nl80211_send_tdls_mgmt(void *priv, const u8 *dst, u8 action_code,
7329 u8 dialog_token, u16 status_code,
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07007330 u32 peer_capab, int initiator, const u8 *buf,
7331 size_t len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007332{
7333 struct i802_bss *bss = priv;
7334 struct wpa_driver_nl80211_data *drv = bss->drv;
7335 struct nl_msg *msg;
7336
7337 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
7338 return -EOPNOTSUPP;
7339
7340 if (!dst)
7341 return -EINVAL;
7342
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007343 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_TDLS_MGMT)) ||
7344 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
7345 nla_put_u8(msg, NL80211_ATTR_TDLS_ACTION, action_code) ||
7346 nla_put_u8(msg, NL80211_ATTR_TDLS_DIALOG_TOKEN, dialog_token) ||
7347 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status_code))
7348 goto fail;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07007349 if (peer_capab) {
7350 /*
7351 * The internal enum tdls_peer_capability definition is
7352 * currently identical with the nl80211 enum
7353 * nl80211_tdls_peer_capability, so no conversion is needed
7354 * here.
7355 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007356 if (nla_put_u32(msg, NL80211_ATTR_TDLS_PEER_CAPABILITY,
7357 peer_capab))
7358 goto fail;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07007359 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007360 if ((initiator &&
7361 nla_put_flag(msg, NL80211_ATTR_TDLS_INITIATOR)) ||
7362 nla_put(msg, NL80211_ATTR_IE, len, buf))
7363 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007364
7365 return send_and_recv_msgs(drv, msg, NULL, NULL);
7366
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007367fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007368 nlmsg_free(msg);
7369 return -ENOBUFS;
7370}
7371
7372
7373static int nl80211_tdls_oper(void *priv, enum tdls_oper oper, const u8 *peer)
7374{
7375 struct i802_bss *bss = priv;
7376 struct wpa_driver_nl80211_data *drv = bss->drv;
7377 struct nl_msg *msg;
7378 enum nl80211_tdls_operation nl80211_oper;
7379
7380 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
7381 return -EOPNOTSUPP;
7382
7383 switch (oper) {
7384 case TDLS_DISCOVERY_REQ:
7385 nl80211_oper = NL80211_TDLS_DISCOVERY_REQ;
7386 break;
7387 case TDLS_SETUP:
7388 nl80211_oper = NL80211_TDLS_SETUP;
7389 break;
7390 case TDLS_TEARDOWN:
7391 nl80211_oper = NL80211_TDLS_TEARDOWN;
7392 break;
7393 case TDLS_ENABLE_LINK:
7394 nl80211_oper = NL80211_TDLS_ENABLE_LINK;
7395 break;
7396 case TDLS_DISABLE_LINK:
7397 nl80211_oper = NL80211_TDLS_DISABLE_LINK;
7398 break;
7399 case TDLS_ENABLE:
7400 return 0;
7401 case TDLS_DISABLE:
7402 return 0;
7403 default:
7404 return -EINVAL;
7405 }
7406
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007407 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_TDLS_OPER)) ||
7408 nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, nl80211_oper) ||
7409 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer)) {
7410 nlmsg_free(msg);
7411 return -ENOBUFS;
7412 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007413
7414 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007415}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007416
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007417
7418static int
7419nl80211_tdls_enable_channel_switch(void *priv, const u8 *addr, u8 oper_class,
7420 const struct hostapd_freq_params *params)
7421{
7422 struct i802_bss *bss = priv;
7423 struct wpa_driver_nl80211_data *drv = bss->drv;
7424 struct nl_msg *msg;
7425 int ret = -ENOBUFS;
7426
7427 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT) ||
7428 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_CHANNEL_SWITCH))
7429 return -EOPNOTSUPP;
7430
7431 wpa_printf(MSG_DEBUG, "nl80211: Enable TDLS channel switch " MACSTR
7432 " oper_class=%u freq=%u",
7433 MAC2STR(addr), oper_class, params->freq);
7434 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_TDLS_CHANNEL_SWITCH);
7435 if (!msg ||
7436 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
7437 nla_put_u8(msg, NL80211_ATTR_OPER_CLASS, oper_class) ||
7438 (ret = nl80211_put_freq_params(msg, params))) {
7439 nlmsg_free(msg);
7440 wpa_printf(MSG_DEBUG, "nl80211: Could not build TDLS chan switch");
7441 return ret;
7442 }
7443
7444 return send_and_recv_msgs(drv, msg, NULL, NULL);
7445}
7446
7447
7448static int
7449nl80211_tdls_disable_channel_switch(void *priv, const u8 *addr)
7450{
7451 struct i802_bss *bss = priv;
7452 struct wpa_driver_nl80211_data *drv = bss->drv;
7453 struct nl_msg *msg;
7454
7455 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT) ||
7456 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_CHANNEL_SWITCH))
7457 return -EOPNOTSUPP;
7458
7459 wpa_printf(MSG_DEBUG, "nl80211: Disable TDLS channel switch " MACSTR,
7460 MAC2STR(addr));
7461 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_TDLS_CANCEL_CHANNEL_SWITCH);
7462 if (!msg ||
7463 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
7464 nlmsg_free(msg);
7465 wpa_printf(MSG_DEBUG,
7466 "nl80211: Could not build TDLS cancel chan switch");
7467 return -ENOBUFS;
7468 }
7469
7470 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007471}
7472
7473#endif /* CONFIG TDLS */
7474
7475
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007476static int driver_nl80211_set_key(const char *ifname, void *priv,
7477 enum wpa_alg alg, const u8 *addr,
7478 int key_idx, int set_tx,
7479 const u8 *seq, size_t seq_len,
7480 const u8 *key, size_t key_len)
7481{
7482 struct i802_bss *bss = priv;
7483 return wpa_driver_nl80211_set_key(ifname, bss, alg, addr, key_idx,
7484 set_tx, seq, seq_len, key, key_len);
7485}
7486
7487
7488static int driver_nl80211_scan2(void *priv,
7489 struct wpa_driver_scan_params *params)
7490{
7491 struct i802_bss *bss = priv;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007492#ifdef CONFIG_DRIVER_NL80211_QCA
7493 struct wpa_driver_nl80211_data *drv = bss->drv;
7494
7495 /*
7496 * Do a vendor specific scan if possible. If only_new_results is
7497 * set, do a normal scan since a kernel (cfg80211) BSS cache flush
7498 * cannot be achieved through a vendor scan. The below condition may
7499 * need to be modified if new scan flags are added in the future whose
7500 * functionality can only be achieved through a normal scan.
7501 */
7502 if (drv->scan_vendor_cmd_avail && !params->only_new_results)
7503 return wpa_driver_nl80211_vendor_scan(bss, params);
7504#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007505 return wpa_driver_nl80211_scan(bss, params);
7506}
7507
7508
7509static int driver_nl80211_deauthenticate(void *priv, const u8 *addr,
7510 int reason_code)
7511{
7512 struct i802_bss *bss = priv;
7513 return wpa_driver_nl80211_deauthenticate(bss, addr, reason_code);
7514}
7515
7516
7517static int driver_nl80211_authenticate(void *priv,
7518 struct wpa_driver_auth_params *params)
7519{
7520 struct i802_bss *bss = priv;
7521 return wpa_driver_nl80211_authenticate(bss, params);
7522}
7523
7524
7525static void driver_nl80211_deinit(void *priv)
7526{
7527 struct i802_bss *bss = priv;
7528 wpa_driver_nl80211_deinit(bss);
7529}
7530
7531
7532static int driver_nl80211_if_remove(void *priv, enum wpa_driver_if_type type,
7533 const char *ifname)
7534{
7535 struct i802_bss *bss = priv;
7536 return wpa_driver_nl80211_if_remove(bss, type, ifname);
7537}
7538
7539
7540static int driver_nl80211_send_mlme(void *priv, const u8 *data,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007541 size_t data_len, int noack,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007542 unsigned int freq,
7543 const u16 *csa_offs, size_t csa_offs_len)
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007544{
7545 struct i802_bss *bss = priv;
7546 return wpa_driver_nl80211_send_mlme(bss, data, data_len, noack,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007547 freq, 0, 0, 0, csa_offs,
7548 csa_offs_len);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007549}
7550
7551
7552static int driver_nl80211_sta_remove(void *priv, const u8 *addr)
7553{
7554 struct i802_bss *bss = priv;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007555 return wpa_driver_nl80211_sta_remove(bss, addr, -1, 0);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007556}
7557
7558
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007559static int driver_nl80211_set_sta_vlan(void *priv, const u8 *addr,
7560 const char *ifname, int vlan_id)
7561{
7562 struct i802_bss *bss = priv;
7563 return i802_set_sta_vlan(bss, addr, ifname, vlan_id);
7564}
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007565
7566
7567static int driver_nl80211_read_sta_data(void *priv,
7568 struct hostap_sta_driver_data *data,
7569 const u8 *addr)
7570{
7571 struct i802_bss *bss = priv;
7572 return i802_read_sta_data(bss, data, addr);
7573}
7574
7575
7576static int driver_nl80211_send_action(void *priv, unsigned int freq,
7577 unsigned int wait_time,
7578 const u8 *dst, const u8 *src,
7579 const u8 *bssid,
7580 const u8 *data, size_t data_len,
7581 int no_cck)
7582{
7583 struct i802_bss *bss = priv;
7584 return wpa_driver_nl80211_send_action(bss, freq, wait_time, dst, src,
7585 bssid, data, data_len, no_cck);
7586}
7587
7588
7589static int driver_nl80211_probe_req_report(void *priv, int report)
7590{
7591 struct i802_bss *bss = priv;
7592 return wpa_driver_nl80211_probe_req_report(bss, report);
7593}
7594
7595
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007596static int wpa_driver_nl80211_update_ft_ies(void *priv, const u8 *md,
7597 const u8 *ies, size_t ies_len)
7598{
7599 int ret;
7600 struct nl_msg *msg;
7601 struct i802_bss *bss = priv;
7602 struct wpa_driver_nl80211_data *drv = bss->drv;
7603 u16 mdid = WPA_GET_LE16(md);
7604
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007605 wpa_printf(MSG_DEBUG, "nl80211: Updating FT IEs");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007606 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_UPDATE_FT_IES)) ||
7607 nla_put(msg, NL80211_ATTR_IE, ies_len, ies) ||
7608 nla_put_u16(msg, NL80211_ATTR_MDID, mdid)) {
7609 nlmsg_free(msg);
7610 return -ENOBUFS;
7611 }
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007612
7613 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7614 if (ret) {
7615 wpa_printf(MSG_DEBUG, "nl80211: update_ft_ies failed "
7616 "err=%d (%s)", ret, strerror(-ret));
7617 }
7618
7619 return ret;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007620}
7621
7622
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007623const u8 * wpa_driver_nl80211_get_macaddr(void *priv)
7624{
7625 struct i802_bss *bss = priv;
7626 struct wpa_driver_nl80211_data *drv = bss->drv;
7627
7628 if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE)
7629 return NULL;
7630
7631 return bss->addr;
7632}
7633
7634
Dmitry Shmidt56052862013-10-04 10:23:25 -07007635static const char * scan_state_str(enum scan_states scan_state)
7636{
7637 switch (scan_state) {
7638 case NO_SCAN:
7639 return "NO_SCAN";
7640 case SCAN_REQUESTED:
7641 return "SCAN_REQUESTED";
7642 case SCAN_STARTED:
7643 return "SCAN_STARTED";
7644 case SCAN_COMPLETED:
7645 return "SCAN_COMPLETED";
7646 case SCAN_ABORTED:
7647 return "SCAN_ABORTED";
7648 case SCHED_SCAN_STARTED:
7649 return "SCHED_SCAN_STARTED";
7650 case SCHED_SCAN_STOPPED:
7651 return "SCHED_SCAN_STOPPED";
7652 case SCHED_SCAN_RESULTS:
7653 return "SCHED_SCAN_RESULTS";
7654 }
7655
7656 return "??";
7657}
7658
7659
7660static int wpa_driver_nl80211_status(void *priv, char *buf, size_t buflen)
7661{
7662 struct i802_bss *bss = priv;
7663 struct wpa_driver_nl80211_data *drv = bss->drv;
7664 int res;
7665 char *pos, *end;
7666
7667 pos = buf;
7668 end = buf + buflen;
7669
7670 res = os_snprintf(pos, end - pos,
7671 "ifindex=%d\n"
7672 "ifname=%s\n"
7673 "brname=%s\n"
7674 "addr=" MACSTR "\n"
7675 "freq=%d\n"
7676 "%s%s%s%s%s",
7677 bss->ifindex,
7678 bss->ifname,
7679 bss->brname,
7680 MAC2STR(bss->addr),
7681 bss->freq,
7682 bss->beacon_set ? "beacon_set=1\n" : "",
7683 bss->added_if_into_bridge ?
7684 "added_if_into_bridge=1\n" : "",
7685 bss->added_bridge ? "added_bridge=1\n" : "",
7686 bss->in_deinit ? "in_deinit=1\n" : "",
7687 bss->if_dynamic ? "if_dynamic=1\n" : "");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007688 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt56052862013-10-04 10:23:25 -07007689 return pos - buf;
7690 pos += res;
7691
7692 if (bss->wdev_id_set) {
7693 res = os_snprintf(pos, end - pos, "wdev_id=%llu\n",
7694 (unsigned long long) bss->wdev_id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007695 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt56052862013-10-04 10:23:25 -07007696 return pos - buf;
7697 pos += res;
7698 }
7699
7700 res = os_snprintf(pos, end - pos,
7701 "phyname=%s\n"
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007702 "perm_addr=" MACSTR "\n"
Dmitry Shmidt56052862013-10-04 10:23:25 -07007703 "drv_ifindex=%d\n"
7704 "operstate=%d\n"
7705 "scan_state=%s\n"
7706 "auth_bssid=" MACSTR "\n"
7707 "auth_attempt_bssid=" MACSTR "\n"
7708 "bssid=" MACSTR "\n"
7709 "prev_bssid=" MACSTR "\n"
7710 "associated=%d\n"
7711 "assoc_freq=%u\n"
7712 "monitor_sock=%d\n"
7713 "monitor_ifidx=%d\n"
7714 "monitor_refcount=%d\n"
7715 "last_mgmt_freq=%u\n"
7716 "eapol_tx_sock=%d\n"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007717 "%s%s%s%s%s%s%s%s%s%s%s%s%s",
Dmitry Shmidt56052862013-10-04 10:23:25 -07007718 drv->phyname,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007719 MAC2STR(drv->perm_addr),
Dmitry Shmidt56052862013-10-04 10:23:25 -07007720 drv->ifindex,
7721 drv->operstate,
7722 scan_state_str(drv->scan_state),
7723 MAC2STR(drv->auth_bssid),
7724 MAC2STR(drv->auth_attempt_bssid),
7725 MAC2STR(drv->bssid),
7726 MAC2STR(drv->prev_bssid),
7727 drv->associated,
7728 drv->assoc_freq,
7729 drv->monitor_sock,
7730 drv->monitor_ifidx,
7731 drv->monitor_refcount,
7732 drv->last_mgmt_freq,
7733 drv->eapol_tx_sock,
7734 drv->ignore_if_down_event ?
7735 "ignore_if_down_event=1\n" : "",
7736 drv->scan_complete_events ?
7737 "scan_complete_events=1\n" : "",
7738 drv->disabled_11b_rates ?
7739 "disabled_11b_rates=1\n" : "",
7740 drv->pending_remain_on_chan ?
7741 "pending_remain_on_chan=1\n" : "",
7742 drv->in_interface_list ? "in_interface_list=1\n" : "",
7743 drv->device_ap_sme ? "device_ap_sme=1\n" : "",
7744 drv->poll_command_supported ?
7745 "poll_command_supported=1\n" : "",
7746 drv->data_tx_status ? "data_tx_status=1\n" : "",
7747 drv->scan_for_auth ? "scan_for_auth=1\n" : "",
7748 drv->retry_auth ? "retry_auth=1\n" : "",
7749 drv->use_monitor ? "use_monitor=1\n" : "",
7750 drv->ignore_next_local_disconnect ?
7751 "ignore_next_local_disconnect=1\n" : "",
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07007752 drv->ignore_next_local_deauth ?
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007753 "ignore_next_local_deauth=1\n" : "");
7754 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt56052862013-10-04 10:23:25 -07007755 return pos - buf;
7756 pos += res;
7757
7758 if (drv->has_capability) {
7759 res = os_snprintf(pos, end - pos,
7760 "capa.key_mgmt=0x%x\n"
7761 "capa.enc=0x%x\n"
7762 "capa.auth=0x%x\n"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007763 "capa.flags=0x%llx\n"
7764 "capa.rrm_flags=0x%x\n"
Dmitry Shmidt56052862013-10-04 10:23:25 -07007765 "capa.max_scan_ssids=%d\n"
7766 "capa.max_sched_scan_ssids=%d\n"
7767 "capa.sched_scan_supported=%d\n"
7768 "capa.max_match_sets=%d\n"
7769 "capa.max_remain_on_chan=%u\n"
7770 "capa.max_stations=%u\n"
7771 "capa.probe_resp_offloads=0x%x\n"
7772 "capa.max_acl_mac_addrs=%u\n"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007773 "capa.num_multichan_concurrent=%u\n"
7774 "capa.mac_addr_rand_sched_scan_supported=%d\n"
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007775 "capa.mac_addr_rand_scan_supported=%d\n"
7776 "capa.conc_capab=%u\n"
7777 "capa.max_conc_chan_2_4=%u\n"
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007778 "capa.max_conc_chan_5_0=%u\n"
7779 "capa.max_sched_scan_plans=%u\n"
7780 "capa.max_sched_scan_plan_interval=%u\n"
7781 "capa.max_sched_scan_plan_iterations=%u\n",
Dmitry Shmidt56052862013-10-04 10:23:25 -07007782 drv->capa.key_mgmt,
7783 drv->capa.enc,
7784 drv->capa.auth,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007785 (unsigned long long) drv->capa.flags,
7786 drv->capa.rrm_flags,
Dmitry Shmidt56052862013-10-04 10:23:25 -07007787 drv->capa.max_scan_ssids,
7788 drv->capa.max_sched_scan_ssids,
7789 drv->capa.sched_scan_supported,
7790 drv->capa.max_match_sets,
7791 drv->capa.max_remain_on_chan,
7792 drv->capa.max_stations,
7793 drv->capa.probe_resp_offloads,
7794 drv->capa.max_acl_mac_addrs,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007795 drv->capa.num_multichan_concurrent,
7796 drv->capa.mac_addr_rand_sched_scan_supported,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007797 drv->capa.mac_addr_rand_scan_supported,
7798 drv->capa.conc_capab,
7799 drv->capa.max_conc_chan_2_4,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007800 drv->capa.max_conc_chan_5_0,
7801 drv->capa.max_sched_scan_plans,
7802 drv->capa.max_sched_scan_plan_interval,
7803 drv->capa.max_sched_scan_plan_iterations);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007804 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt56052862013-10-04 10:23:25 -07007805 return pos - buf;
7806 pos += res;
7807 }
7808
7809 return pos - buf;
7810}
7811
7812
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007813static int set_beacon_data(struct nl_msg *msg, struct beacon_data *settings)
7814{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007815 if ((settings->head &&
7816 nla_put(msg, NL80211_ATTR_BEACON_HEAD,
7817 settings->head_len, settings->head)) ||
7818 (settings->tail &&
7819 nla_put(msg, NL80211_ATTR_BEACON_TAIL,
7820 settings->tail_len, settings->tail)) ||
7821 (settings->beacon_ies &&
7822 nla_put(msg, NL80211_ATTR_IE,
7823 settings->beacon_ies_len, settings->beacon_ies)) ||
7824 (settings->proberesp_ies &&
7825 nla_put(msg, NL80211_ATTR_IE_PROBE_RESP,
7826 settings->proberesp_ies_len, settings->proberesp_ies)) ||
7827 (settings->assocresp_ies &&
7828 nla_put(msg, NL80211_ATTR_IE_ASSOC_RESP,
7829 settings->assocresp_ies_len, settings->assocresp_ies)) ||
7830 (settings->probe_resp &&
7831 nla_put(msg, NL80211_ATTR_PROBE_RESP,
7832 settings->probe_resp_len, settings->probe_resp)))
7833 return -ENOBUFS;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007834
7835 return 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007836}
7837
7838
7839static int nl80211_switch_channel(void *priv, struct csa_settings *settings)
7840{
7841 struct nl_msg *msg;
7842 struct i802_bss *bss = priv;
7843 struct wpa_driver_nl80211_data *drv = bss->drv;
7844 struct nlattr *beacon_csa;
7845 int ret = -ENOBUFS;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007846 int csa_off_len = 0;
7847 int i;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007848
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08007849 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 -08007850 settings->cs_count, settings->block_tx,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08007851 settings->freq_params.freq, settings->freq_params.bandwidth,
7852 settings->freq_params.center_freq1,
7853 settings->freq_params.center_freq2);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007854
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007855 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_AP_CSA)) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007856 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support channel switch command");
7857 return -EOPNOTSUPP;
7858 }
7859
7860 if ((drv->nlmode != NL80211_IFTYPE_AP) &&
7861 (drv->nlmode != NL80211_IFTYPE_P2P_GO))
7862 return -EOPNOTSUPP;
7863
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007864 /*
7865 * Remove empty counters, assuming Probe Response and Beacon frame
7866 * counters match. This implementation assumes that there are only two
7867 * counters.
7868 */
7869 if (settings->counter_offset_beacon[0] &&
7870 !settings->counter_offset_beacon[1]) {
7871 csa_off_len = 1;
7872 } else if (settings->counter_offset_beacon[1] &&
7873 !settings->counter_offset_beacon[0]) {
7874 csa_off_len = 1;
7875 settings->counter_offset_beacon[0] =
7876 settings->counter_offset_beacon[1];
7877 settings->counter_offset_presp[0] =
7878 settings->counter_offset_presp[1];
7879 } else if (settings->counter_offset_beacon[1] &&
7880 settings->counter_offset_beacon[0]) {
7881 csa_off_len = 2;
7882 } else {
7883 wpa_printf(MSG_ERROR, "nl80211: No CSA counters provided");
7884 return -EINVAL;
7885 }
7886
7887 /* Check CSA counters validity */
7888 if (drv->capa.max_csa_counters &&
7889 csa_off_len > drv->capa.max_csa_counters) {
7890 wpa_printf(MSG_ERROR,
7891 "nl80211: Too many CSA counters provided");
7892 return -EINVAL;
7893 }
7894
7895 if (!settings->beacon_csa.tail)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007896 return -EINVAL;
7897
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007898 for (i = 0; i < csa_off_len; i++) {
7899 u16 csa_c_off_bcn = settings->counter_offset_beacon[i];
7900 u16 csa_c_off_presp = settings->counter_offset_presp[i];
7901
7902 if ((settings->beacon_csa.tail_len <= csa_c_off_bcn) ||
7903 (settings->beacon_csa.tail[csa_c_off_bcn] !=
7904 settings->cs_count))
7905 return -EINVAL;
7906
7907 if (settings->beacon_csa.probe_resp &&
7908 ((settings->beacon_csa.probe_resp_len <=
7909 csa_c_off_presp) ||
7910 (settings->beacon_csa.probe_resp[csa_c_off_presp] !=
7911 settings->cs_count)))
7912 return -EINVAL;
7913 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007914
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007915 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_CHANNEL_SWITCH)) ||
7916 nla_put_u32(msg, NL80211_ATTR_CH_SWITCH_COUNT,
7917 settings->cs_count) ||
7918 (ret = nl80211_put_freq_params(msg, &settings->freq_params)) ||
7919 (settings->block_tx &&
7920 nla_put_flag(msg, NL80211_ATTR_CH_SWITCH_BLOCK_TX)))
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007921 goto error;
7922
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007923 /* beacon_after params */
7924 ret = set_beacon_data(msg, &settings->beacon_after);
7925 if (ret)
7926 goto error;
7927
7928 /* beacon_csa params */
7929 beacon_csa = nla_nest_start(msg, NL80211_ATTR_CSA_IES);
7930 if (!beacon_csa)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007931 goto fail;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007932
7933 ret = set_beacon_data(msg, &settings->beacon_csa);
7934 if (ret)
7935 goto error;
7936
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007937 if (nla_put(msg, NL80211_ATTR_CSA_C_OFF_BEACON,
7938 csa_off_len * sizeof(u16),
7939 settings->counter_offset_beacon) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007940 (settings->beacon_csa.probe_resp &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007941 nla_put(msg, NL80211_ATTR_CSA_C_OFF_PRESP,
7942 csa_off_len * sizeof(u16),
7943 settings->counter_offset_presp)))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007944 goto fail;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007945
7946 nla_nest_end(msg, beacon_csa);
7947 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7948 if (ret) {
7949 wpa_printf(MSG_DEBUG, "nl80211: switch_channel failed err=%d (%s)",
7950 ret, strerror(-ret));
7951 }
7952 return ret;
7953
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007954fail:
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007955 ret = -ENOBUFS;
7956error:
7957 nlmsg_free(msg);
7958 wpa_printf(MSG_DEBUG, "nl80211: Could not build channel switch request");
7959 return ret;
7960}
7961
7962
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007963static int nl80211_add_ts(void *priv, u8 tsid, const u8 *addr,
7964 u8 user_priority, u16 admitted_time)
7965{
7966 struct i802_bss *bss = priv;
7967 struct wpa_driver_nl80211_data *drv = bss->drv;
7968 struct nl_msg *msg;
7969 int ret;
7970
7971 wpa_printf(MSG_DEBUG,
7972 "nl80211: add_ts request: tsid=%u admitted_time=%u up=%d",
7973 tsid, admitted_time, user_priority);
7974
7975 if (!is_sta_interface(drv->nlmode))
7976 return -ENOTSUP;
7977
7978 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_ADD_TX_TS);
7979 if (!msg ||
7980 nla_put_u8(msg, NL80211_ATTR_TSID, tsid) ||
7981 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
7982 nla_put_u8(msg, NL80211_ATTR_USER_PRIO, user_priority) ||
7983 nla_put_u16(msg, NL80211_ATTR_ADMITTED_TIME, admitted_time)) {
7984 nlmsg_free(msg);
7985 return -ENOBUFS;
7986 }
7987
7988 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7989 if (ret)
7990 wpa_printf(MSG_DEBUG, "nl80211: add_ts failed err=%d (%s)",
7991 ret, strerror(-ret));
7992 return ret;
7993}
7994
7995
7996static int nl80211_del_ts(void *priv, u8 tsid, const u8 *addr)
7997{
7998 struct i802_bss *bss = priv;
7999 struct wpa_driver_nl80211_data *drv = bss->drv;
8000 struct nl_msg *msg;
8001 int ret;
8002
8003 wpa_printf(MSG_DEBUG, "nl80211: del_ts request: tsid=%u", tsid);
8004
8005 if (!is_sta_interface(drv->nlmode))
8006 return -ENOTSUP;
8007
8008 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_DEL_TX_TS)) ||
8009 nla_put_u8(msg, NL80211_ATTR_TSID, tsid) ||
8010 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
8011 nlmsg_free(msg);
8012 return -ENOBUFS;
8013 }
8014
8015 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8016 if (ret)
8017 wpa_printf(MSG_DEBUG, "nl80211: del_ts failed err=%d (%s)",
8018 ret, strerror(-ret));
8019 return ret;
8020}
8021
8022
Dmitry Shmidta38abf92014-03-06 13:38:44 -08008023#ifdef CONFIG_TESTING_OPTIONS
8024static int cmd_reply_handler(struct nl_msg *msg, void *arg)
8025{
8026 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
8027 struct wpabuf *buf = arg;
8028
8029 if (!buf)
8030 return NL_SKIP;
8031
8032 if ((size_t) genlmsg_attrlen(gnlh, 0) > wpabuf_tailroom(buf)) {
8033 wpa_printf(MSG_INFO, "nl80211: insufficient buffer space for reply");
8034 return NL_SKIP;
8035 }
8036
8037 wpabuf_put_data(buf, genlmsg_attrdata(gnlh, 0),
8038 genlmsg_attrlen(gnlh, 0));
8039
8040 return NL_SKIP;
8041}
8042#endif /* CONFIG_TESTING_OPTIONS */
8043
8044
8045static int vendor_reply_handler(struct nl_msg *msg, void *arg)
8046{
8047 struct nlattr *tb[NL80211_ATTR_MAX + 1];
8048 struct nlattr *nl_vendor_reply, *nl;
8049 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
8050 struct wpabuf *buf = arg;
8051 int rem;
8052
8053 if (!buf)
8054 return NL_SKIP;
8055
8056 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
8057 genlmsg_attrlen(gnlh, 0), NULL);
8058 nl_vendor_reply = tb[NL80211_ATTR_VENDOR_DATA];
8059
8060 if (!nl_vendor_reply)
8061 return NL_SKIP;
8062
8063 if ((size_t) nla_len(nl_vendor_reply) > wpabuf_tailroom(buf)) {
8064 wpa_printf(MSG_INFO, "nl80211: Vendor command: insufficient buffer space for reply");
8065 return NL_SKIP;
8066 }
8067
8068 nla_for_each_nested(nl, nl_vendor_reply, rem) {
8069 wpabuf_put_data(buf, nla_data(nl), nla_len(nl));
8070 }
8071
8072 return NL_SKIP;
8073}
8074
8075
8076static int nl80211_vendor_cmd(void *priv, unsigned int vendor_id,
8077 unsigned int subcmd, const u8 *data,
8078 size_t data_len, struct wpabuf *buf)
8079{
8080 struct i802_bss *bss = priv;
8081 struct wpa_driver_nl80211_data *drv = bss->drv;
8082 struct nl_msg *msg;
8083 int ret;
8084
Dmitry Shmidta38abf92014-03-06 13:38:44 -08008085#ifdef CONFIG_TESTING_OPTIONS
8086 if (vendor_id == 0xffffffff) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008087 msg = nlmsg_alloc();
8088 if (!msg)
8089 return -ENOMEM;
8090
Dmitry Shmidta38abf92014-03-06 13:38:44 -08008091 nl80211_cmd(drv, msg, 0, subcmd);
8092 if (nlmsg_append(msg, (void *) data, data_len, NLMSG_ALIGNTO) <
8093 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008094 goto fail;
Dmitry Shmidta38abf92014-03-06 13:38:44 -08008095 ret = send_and_recv_msgs(drv, msg, cmd_reply_handler, buf);
8096 if (ret)
8097 wpa_printf(MSG_DEBUG, "nl80211: command failed err=%d",
8098 ret);
8099 return ret;
8100 }
8101#endif /* CONFIG_TESTING_OPTIONS */
8102
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008103 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_VENDOR)) ||
8104 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, vendor_id) ||
8105 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD, subcmd) ||
8106 (data &&
8107 nla_put(msg, NL80211_ATTR_VENDOR_DATA, data_len, data)))
8108 goto fail;
Dmitry Shmidta38abf92014-03-06 13:38:44 -08008109
8110 ret = send_and_recv_msgs(drv, msg, vendor_reply_handler, buf);
8111 if (ret)
8112 wpa_printf(MSG_DEBUG, "nl80211: vendor command failed err=%d",
8113 ret);
8114 return ret;
8115
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008116fail:
Dmitry Shmidta38abf92014-03-06 13:38:44 -08008117 nlmsg_free(msg);
8118 return -ENOBUFS;
8119}
8120
8121
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008122static int nl80211_set_qos_map(void *priv, const u8 *qos_map_set,
8123 u8 qos_map_set_len)
8124{
8125 struct i802_bss *bss = priv;
8126 struct wpa_driver_nl80211_data *drv = bss->drv;
8127 struct nl_msg *msg;
8128 int ret;
8129
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008130 wpa_hexdump(MSG_DEBUG, "nl80211: Setting QoS Map",
8131 qos_map_set, qos_map_set_len);
8132
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008133 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_QOS_MAP)) ||
8134 nla_put(msg, NL80211_ATTR_QOS_MAP, qos_map_set_len, qos_map_set)) {
8135 nlmsg_free(msg);
8136 return -ENOBUFS;
8137 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008138
8139 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8140 if (ret)
8141 wpa_printf(MSG_DEBUG, "nl80211: Setting QoS Map failed");
8142
8143 return ret;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008144}
8145
8146
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07008147static int nl80211_set_wowlan(void *priv,
8148 const struct wowlan_triggers *triggers)
8149{
8150 struct i802_bss *bss = priv;
8151 struct wpa_driver_nl80211_data *drv = bss->drv;
8152 struct nl_msg *msg;
8153 struct nlattr *wowlan_triggers;
8154 int ret;
8155
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07008156 wpa_printf(MSG_DEBUG, "nl80211: Setting wowlan");
8157
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008158 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_SET_WOWLAN)) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008159 !(wowlan_triggers = nla_nest_start(msg,
8160 NL80211_ATTR_WOWLAN_TRIGGERS)) ||
8161 (triggers->any &&
8162 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
8163 (triggers->disconnect &&
8164 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
8165 (triggers->magic_pkt &&
8166 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
8167 (triggers->gtk_rekey_failure &&
8168 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
8169 (triggers->eap_identity_req &&
8170 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
8171 (triggers->four_way_handshake &&
8172 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
8173 (triggers->rfkill_release &&
8174 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))) {
8175 nlmsg_free(msg);
8176 return -ENOBUFS;
8177 }
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07008178
8179 nla_nest_end(msg, wowlan_triggers);
8180
8181 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8182 if (ret)
8183 wpa_printf(MSG_DEBUG, "nl80211: Setting wowlan failed");
8184
8185 return ret;
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07008186}
8187
8188
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008189#ifdef CONFIG_DRIVER_NL80211_QCA
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008190static int nl80211_roaming(void *priv, int allowed, const u8 *bssid)
8191{
8192 struct i802_bss *bss = priv;
8193 struct wpa_driver_nl80211_data *drv = bss->drv;
8194 struct nl_msg *msg;
8195 struct nlattr *params;
8196
8197 wpa_printf(MSG_DEBUG, "nl80211: Roaming policy: allowed=%d", allowed);
8198
8199 if (!drv->roaming_vendor_cmd_avail) {
8200 wpa_printf(MSG_DEBUG,
8201 "nl80211: Ignore roaming policy change since driver does not provide command for setting it");
8202 return -1;
8203 }
8204
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008205 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
8206 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
8207 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
8208 QCA_NL80211_VENDOR_SUBCMD_ROAMING) ||
8209 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
8210 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_ROAMING_POLICY,
8211 allowed ? QCA_ROAMING_ALLOWED_WITHIN_ESS :
8212 QCA_ROAMING_NOT_ALLOWED) ||
8213 (bssid &&
8214 nla_put(msg, QCA_WLAN_VENDOR_ATTR_MAC_ADDR, ETH_ALEN, bssid))) {
8215 nlmsg_free(msg);
8216 return -1;
8217 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008218 nla_nest_end(msg, params);
8219
8220 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008221}
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008222#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008223
8224
8225static int nl80211_set_mac_addr(void *priv, const u8 *addr)
8226{
8227 struct i802_bss *bss = priv;
8228 struct wpa_driver_nl80211_data *drv = bss->drv;
8229 int new_addr = addr != NULL;
8230
8231 if (!addr)
8232 addr = drv->perm_addr;
8233
8234 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0) < 0)
8235 return -1;
8236
8237 if (linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname, addr) < 0)
8238 {
8239 wpa_printf(MSG_DEBUG,
8240 "nl80211: failed to set_mac_addr for %s to " MACSTR,
8241 bss->ifname, MAC2STR(addr));
8242 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname,
8243 1) < 0) {
8244 wpa_printf(MSG_DEBUG,
8245 "nl80211: Could not restore interface UP after failed set_mac_addr");
8246 }
8247 return -1;
8248 }
8249
8250 wpa_printf(MSG_DEBUG, "nl80211: set_mac_addr for %s to " MACSTR,
8251 bss->ifname, MAC2STR(addr));
8252 drv->addr_changed = new_addr;
8253 os_memcpy(bss->addr, addr, ETH_ALEN);
8254
8255 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1) < 0)
8256 {
8257 wpa_printf(MSG_DEBUG,
8258 "nl80211: Could not restore interface UP after set_mac_addr");
8259 }
8260
8261 return 0;
8262}
8263
8264
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008265#ifdef CONFIG_MESH
8266
8267static int wpa_driver_nl80211_init_mesh(void *priv)
8268{
8269 if (wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_MESH_POINT)) {
8270 wpa_printf(MSG_INFO,
8271 "nl80211: Failed to set interface into mesh mode");
8272 return -1;
8273 }
8274 return 0;
8275}
8276
8277
Dmitry Shmidtff787d52015-01-12 13:01:47 -08008278static int nl80211_put_mesh_id(struct nl_msg *msg, const u8 *mesh_id,
8279 size_t mesh_id_len)
8280{
8281 if (mesh_id) {
8282 wpa_hexdump_ascii(MSG_DEBUG, " * Mesh ID (SSID)",
8283 mesh_id, mesh_id_len);
8284 return nla_put(msg, NL80211_ATTR_MESH_ID, mesh_id_len, mesh_id);
8285 }
8286
8287 return 0;
8288}
8289
8290
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008291static int nl80211_join_mesh(struct i802_bss *bss,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008292 struct wpa_driver_mesh_join_params *params)
8293{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008294 struct wpa_driver_nl80211_data *drv = bss->drv;
8295 struct nl_msg *msg;
8296 struct nlattr *container;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08008297 int ret = -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008298
8299 wpa_printf(MSG_DEBUG, "nl80211: mesh join (ifindex=%d)", drv->ifindex);
8300 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_JOIN_MESH);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08008301 if (!msg ||
8302 nl80211_put_freq_params(msg, &params->freq) ||
8303 nl80211_put_basic_rates(msg, params->basic_rates) ||
8304 nl80211_put_mesh_id(msg, params->meshid, params->meshid_len) ||
8305 nl80211_put_beacon_int(msg, params->beacon_int))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008306 goto fail;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008307
8308 wpa_printf(MSG_DEBUG, " * flags=%08X", params->flags);
8309
8310 container = nla_nest_start(msg, NL80211_ATTR_MESH_SETUP);
8311 if (!container)
8312 goto fail;
8313
8314 if (params->ies) {
8315 wpa_hexdump(MSG_DEBUG, " * IEs", params->ies, params->ie_len);
8316 if (nla_put(msg, NL80211_MESH_SETUP_IE, params->ie_len,
8317 params->ies))
8318 goto fail;
8319 }
8320 /* WPA_DRIVER_MESH_FLAG_OPEN_AUTH is treated as default by nl80211 */
8321 if (params->flags & WPA_DRIVER_MESH_FLAG_SAE_AUTH) {
8322 if (nla_put_u8(msg, NL80211_MESH_SETUP_AUTH_PROTOCOL, 0x1) ||
8323 nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_AUTH))
8324 goto fail;
8325 }
8326 if ((params->flags & WPA_DRIVER_MESH_FLAG_AMPE) &&
8327 nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_AMPE))
8328 goto fail;
8329 if ((params->flags & WPA_DRIVER_MESH_FLAG_USER_MPM) &&
8330 nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_MPM))
8331 goto fail;
8332 nla_nest_end(msg, container);
8333
8334 container = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
8335 if (!container)
8336 goto fail;
8337
8338 if (!(params->conf.flags & WPA_DRIVER_MESH_CONF_FLAG_AUTO_PLINKS) &&
8339 nla_put_u32(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS, 0))
8340 goto fail;
8341 if ((params->conf.flags & WPA_DRIVER_MESH_FLAG_DRIVER_MPM) &&
8342 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
8343 params->max_peer_links))
8344 goto fail;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08008345
8346 /*
8347 * Set NL80211_MESHCONF_PLINK_TIMEOUT even if user mpm is used because
8348 * the timer could disconnect stations even in that case.
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08008349 */
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008350 if (nla_put_u32(msg, NL80211_MESHCONF_PLINK_TIMEOUT,
8351 params->conf.peer_link_timeout)) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08008352 wpa_printf(MSG_ERROR, "nl80211: Failed to set PLINK_TIMEOUT");
8353 goto fail;
8354 }
8355
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008356 nla_nest_end(msg, container);
8357
8358 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8359 msg = NULL;
8360 if (ret) {
8361 wpa_printf(MSG_DEBUG, "nl80211: mesh join failed: ret=%d (%s)",
8362 ret, strerror(-ret));
8363 goto fail;
8364 }
8365 ret = 0;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08008366 bss->freq = params->freq.freq;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008367 wpa_printf(MSG_DEBUG, "nl80211: mesh join request send successfully");
8368
8369fail:
8370 nlmsg_free(msg);
8371 return ret;
8372}
8373
8374
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008375static int
8376wpa_driver_nl80211_join_mesh(void *priv,
8377 struct wpa_driver_mesh_join_params *params)
8378{
8379 struct i802_bss *bss = priv;
8380 int ret, timeout;
8381
8382 timeout = params->conf.peer_link_timeout;
8383
8384 /* Disable kernel inactivity timer */
8385 if (params->flags & WPA_DRIVER_MESH_FLAG_USER_MPM)
8386 params->conf.peer_link_timeout = 0;
8387
8388 ret = nl80211_join_mesh(bss, params);
8389 if (ret == -EINVAL && params->conf.peer_link_timeout == 0) {
8390 wpa_printf(MSG_DEBUG,
8391 "nl80211: Mesh join retry for peer_link_timeout");
8392 /*
8393 * Old kernel does not support setting
8394 * NL80211_MESHCONF_PLINK_TIMEOUT to zero, so set 60 seconds
8395 * into future from peer_link_timeout.
8396 */
8397 params->conf.peer_link_timeout = timeout + 60;
8398 ret = nl80211_join_mesh(priv, params);
8399 }
8400
8401 params->conf.peer_link_timeout = timeout;
8402 return ret;
8403}
8404
8405
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008406static int wpa_driver_nl80211_leave_mesh(void *priv)
8407{
8408 struct i802_bss *bss = priv;
8409 struct wpa_driver_nl80211_data *drv = bss->drv;
8410 struct nl_msg *msg;
8411 int ret;
8412
8413 wpa_printf(MSG_DEBUG, "nl80211: mesh leave (ifindex=%d)", drv->ifindex);
8414 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_LEAVE_MESH);
8415 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8416 if (ret) {
8417 wpa_printf(MSG_DEBUG, "nl80211: mesh leave failed: ret=%d (%s)",
8418 ret, strerror(-ret));
8419 } else {
8420 wpa_printf(MSG_DEBUG,
8421 "nl80211: mesh leave request send successfully");
8422 }
8423
8424 if (wpa_driver_nl80211_set_mode(drv->first_bss,
8425 NL80211_IFTYPE_STATION)) {
8426 wpa_printf(MSG_INFO,
8427 "nl80211: Failed to set interface into station mode");
8428 }
8429 return ret;
8430}
8431
8432#endif /* CONFIG_MESH */
8433
8434
8435static int wpa_driver_br_add_ip_neigh(void *priv, u8 version,
8436 const u8 *ipaddr, int prefixlen,
8437 const u8 *addr)
8438{
8439#ifdef CONFIG_LIBNL3_ROUTE
8440 struct i802_bss *bss = priv;
8441 struct wpa_driver_nl80211_data *drv = bss->drv;
8442 struct rtnl_neigh *rn;
8443 struct nl_addr *nl_ipaddr = NULL;
8444 struct nl_addr *nl_lladdr = NULL;
8445 int family, addrsize;
8446 int res;
8447
8448 if (!ipaddr || prefixlen == 0 || !addr)
8449 return -EINVAL;
8450
8451 if (bss->br_ifindex == 0) {
8452 wpa_printf(MSG_DEBUG,
8453 "nl80211: bridge must be set before adding an ip neigh to it");
8454 return -1;
8455 }
8456
8457 if (!drv->rtnl_sk) {
8458 wpa_printf(MSG_DEBUG,
8459 "nl80211: nl_sock for NETLINK_ROUTE is not initialized");
8460 return -1;
8461 }
8462
8463 if (version == 4) {
8464 family = AF_INET;
8465 addrsize = 4;
8466 } else if (version == 6) {
8467 family = AF_INET6;
8468 addrsize = 16;
8469 } else {
8470 return -EINVAL;
8471 }
8472
8473 rn = rtnl_neigh_alloc();
8474 if (rn == NULL)
8475 return -ENOMEM;
8476
8477 /* set the destination ip address for neigh */
8478 nl_ipaddr = nl_addr_build(family, (void *) ipaddr, addrsize);
8479 if (nl_ipaddr == NULL) {
8480 wpa_printf(MSG_DEBUG, "nl80211: nl_ipaddr build failed");
8481 res = -ENOMEM;
8482 goto errout;
8483 }
8484 nl_addr_set_prefixlen(nl_ipaddr, prefixlen);
8485 res = rtnl_neigh_set_dst(rn, nl_ipaddr);
8486 if (res) {
8487 wpa_printf(MSG_DEBUG,
8488 "nl80211: neigh set destination addr failed");
8489 goto errout;
8490 }
8491
8492 /* set the corresponding lladdr for neigh */
8493 nl_lladdr = nl_addr_build(AF_BRIDGE, (u8 *) addr, ETH_ALEN);
8494 if (nl_lladdr == NULL) {
8495 wpa_printf(MSG_DEBUG, "nl80211: neigh set lladdr failed");
8496 res = -ENOMEM;
8497 goto errout;
8498 }
8499 rtnl_neigh_set_lladdr(rn, nl_lladdr);
8500
8501 rtnl_neigh_set_ifindex(rn, bss->br_ifindex);
8502 rtnl_neigh_set_state(rn, NUD_PERMANENT);
8503
8504 res = rtnl_neigh_add(drv->rtnl_sk, rn, NLM_F_CREATE);
8505 if (res) {
8506 wpa_printf(MSG_DEBUG,
8507 "nl80211: Adding bridge ip neigh failed: %s",
8508 strerror(errno));
8509 }
8510errout:
8511 if (nl_lladdr)
8512 nl_addr_put(nl_lladdr);
8513 if (nl_ipaddr)
8514 nl_addr_put(nl_ipaddr);
8515 if (rn)
8516 rtnl_neigh_put(rn);
8517 return res;
8518#else /* CONFIG_LIBNL3_ROUTE */
8519 return -1;
8520#endif /* CONFIG_LIBNL3_ROUTE */
8521}
8522
8523
8524static int wpa_driver_br_delete_ip_neigh(void *priv, u8 version,
8525 const u8 *ipaddr)
8526{
8527#ifdef CONFIG_LIBNL3_ROUTE
8528 struct i802_bss *bss = priv;
8529 struct wpa_driver_nl80211_data *drv = bss->drv;
8530 struct rtnl_neigh *rn;
8531 struct nl_addr *nl_ipaddr;
8532 int family, addrsize;
8533 int res;
8534
8535 if (!ipaddr)
8536 return -EINVAL;
8537
8538 if (version == 4) {
8539 family = AF_INET;
8540 addrsize = 4;
8541 } else if (version == 6) {
8542 family = AF_INET6;
8543 addrsize = 16;
8544 } else {
8545 return -EINVAL;
8546 }
8547
8548 if (bss->br_ifindex == 0) {
8549 wpa_printf(MSG_DEBUG,
8550 "nl80211: bridge must be set to delete an ip neigh");
8551 return -1;
8552 }
8553
8554 if (!drv->rtnl_sk) {
8555 wpa_printf(MSG_DEBUG,
8556 "nl80211: nl_sock for NETLINK_ROUTE is not initialized");
8557 return -1;
8558 }
8559
8560 rn = rtnl_neigh_alloc();
8561 if (rn == NULL)
8562 return -ENOMEM;
8563
8564 /* set the destination ip address for neigh */
8565 nl_ipaddr = nl_addr_build(family, (void *) ipaddr, addrsize);
8566 if (nl_ipaddr == NULL) {
8567 wpa_printf(MSG_DEBUG, "nl80211: nl_ipaddr build failed");
8568 res = -ENOMEM;
8569 goto errout;
8570 }
8571 res = rtnl_neigh_set_dst(rn, nl_ipaddr);
8572 if (res) {
8573 wpa_printf(MSG_DEBUG,
8574 "nl80211: neigh set destination addr failed");
8575 goto errout;
8576 }
8577
8578 rtnl_neigh_set_ifindex(rn, bss->br_ifindex);
8579
8580 res = rtnl_neigh_delete(drv->rtnl_sk, rn, 0);
8581 if (res) {
8582 wpa_printf(MSG_DEBUG,
8583 "nl80211: Deleting bridge ip neigh failed: %s",
8584 strerror(errno));
8585 }
8586errout:
8587 if (nl_ipaddr)
8588 nl_addr_put(nl_ipaddr);
8589 if (rn)
8590 rtnl_neigh_put(rn);
8591 return res;
8592#else /* CONFIG_LIBNL3_ROUTE */
8593 return -1;
8594#endif /* CONFIG_LIBNL3_ROUTE */
8595}
8596
8597
8598static int linux_write_system_file(const char *path, unsigned int val)
8599{
8600 char buf[50];
8601 int fd, len;
8602
8603 len = os_snprintf(buf, sizeof(buf), "%u\n", val);
8604 if (os_snprintf_error(sizeof(buf), len))
8605 return -1;
8606
8607 fd = open(path, O_WRONLY);
8608 if (fd < 0)
8609 return -1;
8610
8611 if (write(fd, buf, len) < 0) {
8612 wpa_printf(MSG_DEBUG,
8613 "nl80211: Failed to write Linux system file: %s with the value of %d",
8614 path, val);
8615 close(fd);
8616 return -1;
8617 }
8618 close(fd);
8619
8620 return 0;
8621}
8622
8623
8624static const char * drv_br_port_attr_str(enum drv_br_port_attr attr)
8625{
8626 switch (attr) {
8627 case DRV_BR_PORT_ATTR_PROXYARP:
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07008628 return "proxyarp_wifi";
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008629 case DRV_BR_PORT_ATTR_HAIRPIN_MODE:
8630 return "hairpin_mode";
8631 }
8632
8633 return NULL;
8634}
8635
8636
8637static int wpa_driver_br_port_set_attr(void *priv, enum drv_br_port_attr attr,
8638 unsigned int val)
8639{
8640 struct i802_bss *bss = priv;
8641 char path[128];
8642 const char *attr_txt;
8643
8644 attr_txt = drv_br_port_attr_str(attr);
8645 if (attr_txt == NULL)
8646 return -EINVAL;
8647
8648 os_snprintf(path, sizeof(path), "/sys/class/net/%s/brport/%s",
8649 bss->ifname, attr_txt);
8650
8651 if (linux_write_system_file(path, val))
8652 return -1;
8653
8654 return 0;
8655}
8656
8657
8658static const char * drv_br_net_param_str(enum drv_br_net_param param)
8659{
8660 switch (param) {
8661 case DRV_BR_NET_PARAM_GARP_ACCEPT:
8662 return "arp_accept";
Dmitry Shmidt83474442015-04-15 13:47:09 -07008663 default:
8664 return NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008665 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008666}
8667
8668
8669static int wpa_driver_br_set_net_param(void *priv, enum drv_br_net_param param,
8670 unsigned int val)
8671{
8672 struct i802_bss *bss = priv;
8673 char path[128];
8674 const char *param_txt;
8675 int ip_version = 4;
8676
Dmitry Shmidt83474442015-04-15 13:47:09 -07008677 if (param == DRV_BR_MULTICAST_SNOOPING) {
8678 os_snprintf(path, sizeof(path),
8679 "/sys/devices/virtual/net/%s/bridge/multicast_snooping",
8680 bss->brname);
8681 goto set_val;
8682 }
8683
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008684 param_txt = drv_br_net_param_str(param);
8685 if (param_txt == NULL)
8686 return -EINVAL;
8687
8688 switch (param) {
8689 case DRV_BR_NET_PARAM_GARP_ACCEPT:
8690 ip_version = 4;
8691 break;
8692 default:
8693 return -EINVAL;
8694 }
8695
8696 os_snprintf(path, sizeof(path), "/proc/sys/net/ipv%d/conf/%s/%s",
8697 ip_version, bss->brname, param_txt);
8698
Dmitry Shmidt83474442015-04-15 13:47:09 -07008699set_val:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008700 if (linux_write_system_file(path, val))
8701 return -1;
8702
8703 return 0;
8704}
8705
8706
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008707#ifdef CONFIG_DRIVER_NL80211_QCA
8708
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008709static int hw_mode_to_qca_acs(enum hostapd_hw_mode hw_mode)
8710{
8711 switch (hw_mode) {
8712 case HOSTAPD_MODE_IEEE80211B:
8713 return QCA_ACS_MODE_IEEE80211B;
8714 case HOSTAPD_MODE_IEEE80211G:
8715 return QCA_ACS_MODE_IEEE80211G;
8716 case HOSTAPD_MODE_IEEE80211A:
8717 return QCA_ACS_MODE_IEEE80211A;
8718 case HOSTAPD_MODE_IEEE80211AD:
8719 return QCA_ACS_MODE_IEEE80211AD;
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07008720 case HOSTAPD_MODE_IEEE80211ANY:
8721 return QCA_ACS_MODE_IEEE80211ANY;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008722 default:
8723 return -1;
8724 }
8725}
8726
8727
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008728static int add_acs_freq_list(struct nl_msg *msg, const int *freq_list)
8729{
8730 int i, len, ret;
8731 u32 *freqs;
8732
8733 if (!freq_list)
8734 return 0;
8735 len = int_array_len(freq_list);
8736 freqs = os_malloc(sizeof(u32) * len);
8737 if (!freqs)
8738 return -1;
8739 for (i = 0; i < len; i++)
8740 freqs[i] = freq_list[i];
8741 ret = nla_put(msg, QCA_WLAN_VENDOR_ATTR_ACS_FREQ_LIST,
8742 sizeof(u32) * len, freqs);
8743 os_free(freqs);
8744 return ret;
8745}
8746
8747
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008748static int wpa_driver_do_acs(void *priv, struct drv_acs_params *params)
8749{
8750 struct i802_bss *bss = priv;
8751 struct wpa_driver_nl80211_data *drv = bss->drv;
8752 struct nl_msg *msg;
8753 struct nlattr *data;
8754 int ret;
8755 int mode;
8756
8757 mode = hw_mode_to_qca_acs(params->hw_mode);
8758 if (mode < 0)
8759 return -1;
8760
8761 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
8762 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
8763 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
8764 QCA_NL80211_VENDOR_SUBCMD_DO_ACS) ||
8765 !(data = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
8766 nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_ACS_HW_MODE, mode) ||
8767 (params->ht_enabled &&
8768 nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_HT_ENABLED)) ||
8769 (params->ht40_enabled &&
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07008770 nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_HT40_ENABLED)) ||
8771 (params->vht_enabled &&
8772 nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_VHT_ENABLED)) ||
8773 nla_put_u16(msg, QCA_WLAN_VENDOR_ATTR_ACS_CHWIDTH,
8774 params->ch_width) ||
8775 (params->ch_list_len &&
8776 nla_put(msg, QCA_WLAN_VENDOR_ATTR_ACS_CH_LIST, params->ch_list_len,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008777 params->ch_list)) ||
8778 add_acs_freq_list(msg, params->freq_list)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008779 nlmsg_free(msg);
8780 return -ENOBUFS;
8781 }
8782 nla_nest_end(msg, data);
8783
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07008784 wpa_printf(MSG_DEBUG,
8785 "nl80211: ACS Params: HW_MODE: %d HT: %d HT40: %d VHT: %d BW: %d CH_LIST_LEN: %u",
8786 params->hw_mode, params->ht_enabled, params->ht40_enabled,
8787 params->vht_enabled, params->ch_width, params->ch_list_len);
8788
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008789 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8790 if (ret) {
8791 wpa_printf(MSG_DEBUG,
8792 "nl80211: Failed to invoke driver ACS function: %s",
8793 strerror(errno));
8794 }
8795 return ret;
8796}
8797
8798
Ravi Joshie6ccb162015-07-16 17:45:41 -07008799static int nl80211_set_band(void *priv, enum set_band band)
8800{
8801 struct i802_bss *bss = priv;
8802 struct wpa_driver_nl80211_data *drv = bss->drv;
8803 struct nl_msg *msg;
8804 struct nlattr *data;
8805 int ret;
8806 enum qca_set_band qca_band;
8807
8808 if (!drv->setband_vendor_cmd_avail)
8809 return -1;
8810
8811 switch (band) {
8812 case WPA_SETBAND_AUTO:
8813 qca_band = QCA_SETBAND_AUTO;
8814 break;
8815 case WPA_SETBAND_5G:
8816 qca_band = QCA_SETBAND_5G;
8817 break;
8818 case WPA_SETBAND_2G:
8819 qca_band = QCA_SETBAND_2G;
8820 break;
8821 default:
8822 return -1;
8823 }
8824
8825 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
8826 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
8827 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
8828 QCA_NL80211_VENDOR_SUBCMD_SETBAND) ||
8829 !(data = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
8830 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_SETBAND_VALUE, qca_band)) {
8831 nlmsg_free(msg);
8832 return -ENOBUFS;
8833 }
8834 nla_nest_end(msg, data);
8835
8836 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8837 if (ret) {
8838 wpa_printf(MSG_DEBUG,
8839 "nl80211: Driver setband function failed: %s",
8840 strerror(errno));
8841 }
8842 return ret;
8843}
8844
8845
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008846struct nl80211_pcl {
8847 unsigned int num;
8848 unsigned int *freq_list;
8849};
8850
8851static int preferred_freq_info_handler(struct nl_msg *msg, void *arg)
8852{
8853 struct nlattr *tb[NL80211_ATTR_MAX + 1];
8854 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
8855 struct nl80211_pcl *param = arg;
8856 struct nlattr *nl_vend, *attr;
8857 enum qca_iface_type iface_type;
8858 struct nlattr *tb_vendor[QCA_WLAN_VENDOR_ATTR_MAX + 1];
8859 unsigned int num, max_num;
8860 u32 *freqs;
8861
8862 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
8863 genlmsg_attrlen(gnlh, 0), NULL);
8864
8865 nl_vend = tb[NL80211_ATTR_VENDOR_DATA];
8866 if (!nl_vend)
8867 return NL_SKIP;
8868
8869 nla_parse(tb_vendor, QCA_WLAN_VENDOR_ATTR_MAX,
8870 nla_data(nl_vend), nla_len(nl_vend), NULL);
8871
8872 attr = tb_vendor[
8873 QCA_WLAN_VENDOR_ATTR_GET_PREFERRED_FREQ_LIST_IFACE_TYPE];
8874 if (!attr) {
8875 wpa_printf(MSG_ERROR, "nl80211: iface_type couldn't be found");
8876 param->num = 0;
8877 return NL_SKIP;
8878 }
8879
8880 iface_type = (enum qca_iface_type) nla_get_u32(attr);
8881 wpa_printf(MSG_DEBUG, "nl80211: Driver returned iface_type=%d",
8882 iface_type);
8883
8884 attr = tb_vendor[QCA_WLAN_VENDOR_ATTR_GET_PREFERRED_FREQ_LIST];
8885 if (!attr) {
8886 wpa_printf(MSG_ERROR,
8887 "nl80211: preferred_freq_list couldn't be found");
8888 param->num = 0;
8889 return NL_SKIP;
8890 }
8891
8892 /*
8893 * param->num has the maximum number of entries for which there
8894 * is room in the freq_list provided by the caller.
8895 */
8896 freqs = nla_data(attr);
8897 max_num = nla_len(attr) / sizeof(u32);
8898 if (max_num > param->num)
8899 max_num = param->num;
8900 for (num = 0; num < max_num; num++)
8901 param->freq_list[num] = freqs[num];
8902 param->num = num;
8903
8904 return NL_SKIP;
8905}
8906
8907
8908static int nl80211_get_pref_freq_list(void *priv,
8909 enum wpa_driver_if_type if_type,
8910 unsigned int *num,
8911 unsigned int *freq_list)
8912{
8913 struct i802_bss *bss = priv;
8914 struct wpa_driver_nl80211_data *drv = bss->drv;
8915 struct nl_msg *msg;
8916 int ret;
8917 unsigned int i;
8918 struct nlattr *params;
8919 struct nl80211_pcl param;
8920 enum qca_iface_type iface_type;
8921
8922 if (!drv->get_pref_freq_list)
8923 return -1;
8924
8925 switch (if_type) {
8926 case WPA_IF_STATION:
8927 iface_type = QCA_IFACE_TYPE_STA;
8928 break;
8929 case WPA_IF_AP_BSS:
8930 iface_type = QCA_IFACE_TYPE_AP;
8931 break;
8932 case WPA_IF_P2P_GO:
8933 iface_type = QCA_IFACE_TYPE_P2P_GO;
8934 break;
8935 case WPA_IF_P2P_CLIENT:
8936 iface_type = QCA_IFACE_TYPE_P2P_CLIENT;
8937 break;
8938 case WPA_IF_IBSS:
8939 iface_type = QCA_IFACE_TYPE_IBSS;
8940 break;
8941 case WPA_IF_TDLS:
8942 iface_type = QCA_IFACE_TYPE_TDLS;
8943 break;
8944 default:
8945 return -1;
8946 }
8947
8948 param.num = *num;
8949 param.freq_list = freq_list;
8950
8951 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
8952 nla_put_u32(msg, NL80211_ATTR_IFINDEX, drv->ifindex) ||
8953 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
8954 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
8955 QCA_NL80211_VENDOR_SUBCMD_GET_PREFERRED_FREQ_LIST) ||
8956 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
8957 nla_put_u32(msg,
8958 QCA_WLAN_VENDOR_ATTR_GET_PREFERRED_FREQ_LIST_IFACE_TYPE,
8959 iface_type)) {
8960 wpa_printf(MSG_ERROR,
8961 "%s: err in adding vendor_cmd and vendor_data",
8962 __func__);
8963 nlmsg_free(msg);
8964 return -1;
8965 }
8966 nla_nest_end(msg, params);
8967
8968 os_memset(freq_list, 0, *num * sizeof(freq_list[0]));
8969 ret = send_and_recv_msgs(drv, msg, preferred_freq_info_handler, &param);
8970 if (ret) {
8971 wpa_printf(MSG_ERROR,
8972 "%s: err in send_and_recv_msgs", __func__);
8973 return ret;
8974 }
8975
8976 *num = param.num;
8977
8978 for (i = 0; i < *num; i++) {
8979 wpa_printf(MSG_DEBUG, "nl80211: preferred_channel_list[%d]=%d",
8980 i, freq_list[i]);
8981 }
8982
8983 return 0;
8984}
8985
8986
8987static int nl80211_set_prob_oper_freq(void *priv, unsigned int freq)
8988{
8989 struct i802_bss *bss = priv;
8990 struct wpa_driver_nl80211_data *drv = bss->drv;
8991 struct nl_msg *msg;
8992 int ret;
8993 struct nlattr *params;
8994
8995 if (!drv->set_prob_oper_freq)
8996 return -1;
8997
8998 wpa_printf(MSG_DEBUG,
8999 "nl80211: Set P2P probable operating freq %u for ifindex %d",
9000 freq, bss->ifindex);
9001
9002 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
9003 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
9004 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9005 QCA_NL80211_VENDOR_SUBCMD_SET_PROBABLE_OPER_CHANNEL) ||
9006 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
9007 nla_put_u32(msg,
9008 QCA_WLAN_VENDOR_ATTR_PROBABLE_OPER_CHANNEL_IFACE_TYPE,
9009 QCA_IFACE_TYPE_P2P_CLIENT) ||
9010 nla_put_u32(msg,
9011 QCA_WLAN_VENDOR_ATTR_PROBABLE_OPER_CHANNEL_FREQ,
9012 freq)) {
9013 wpa_printf(MSG_ERROR,
9014 "%s: err in adding vendor_cmd and vendor_data",
9015 __func__);
9016 nlmsg_free(msg);
9017 return -1;
9018 }
9019 nla_nest_end(msg, params);
9020
9021 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9022 msg = NULL;
9023 if (ret) {
9024 wpa_printf(MSG_ERROR, "%s: err in send_and_recv_msgs",
9025 __func__);
9026 return ret;
9027 }
9028 nlmsg_free(msg);
9029 return 0;
9030}
9031
9032#endif /* CONFIG_DRIVER_NL80211_QCA */
9033
9034
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009035const struct wpa_driver_ops wpa_driver_nl80211_ops = {
9036 .name = "nl80211",
9037 .desc = "Linux nl80211/cfg80211",
9038 .get_bssid = wpa_driver_nl80211_get_bssid,
9039 .get_ssid = wpa_driver_nl80211_get_ssid,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009040 .set_key = driver_nl80211_set_key,
9041 .scan2 = driver_nl80211_scan2,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009042 .sched_scan = wpa_driver_nl80211_sched_scan,
9043 .stop_sched_scan = wpa_driver_nl80211_stop_sched_scan,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009044 .get_scan_results2 = wpa_driver_nl80211_get_scan_results,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009045 .abort_scan = wpa_driver_nl80211_abort_scan,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009046 .deauthenticate = driver_nl80211_deauthenticate,
9047 .authenticate = driver_nl80211_authenticate,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009048 .associate = wpa_driver_nl80211_associate,
9049 .global_init = nl80211_global_init,
9050 .global_deinit = nl80211_global_deinit,
9051 .init2 = wpa_driver_nl80211_init,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009052 .deinit = driver_nl80211_deinit,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009053 .get_capa = wpa_driver_nl80211_get_capa,
9054 .set_operstate = wpa_driver_nl80211_set_operstate,
9055 .set_supp_port = wpa_driver_nl80211_set_supp_port,
9056 .set_country = wpa_driver_nl80211_set_country,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009057 .get_country = wpa_driver_nl80211_get_country,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009058 .set_ap = wpa_driver_nl80211_set_ap,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07009059 .set_acl = wpa_driver_nl80211_set_acl,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009060 .if_add = wpa_driver_nl80211_if_add,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009061 .if_remove = driver_nl80211_if_remove,
9062 .send_mlme = driver_nl80211_send_mlme,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009063 .get_hw_feature_data = nl80211_get_hw_feature_data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009064 .sta_add = wpa_driver_nl80211_sta_add,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009065 .sta_remove = driver_nl80211_sta_remove,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009066 .hapd_send_eapol = wpa_driver_nl80211_hapd_send_eapol,
9067 .sta_set_flags = wpa_driver_nl80211_sta_set_flags,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009068 .hapd_init = i802_init,
9069 .hapd_deinit = i802_deinit,
Jouni Malinen75ecf522011-06-27 15:19:46 -07009070 .set_wds_sta = i802_set_wds_sta,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009071 .get_seqnum = i802_get_seqnum,
9072 .flush = i802_flush,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009073 .get_inact_sec = i802_get_inact_sec,
9074 .sta_clear_stats = i802_sta_clear_stats,
9075 .set_rts = i802_set_rts,
9076 .set_frag = i802_set_frag,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009077 .set_tx_queue_params = i802_set_tx_queue_params,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009078 .set_sta_vlan = driver_nl80211_set_sta_vlan,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009079 .sta_deauth = i802_sta_deauth,
9080 .sta_disassoc = i802_sta_disassoc,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009081 .read_sta_data = driver_nl80211_read_sta_data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009082 .set_freq = i802_set_freq,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009083 .send_action = driver_nl80211_send_action,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009084 .send_action_cancel_wait = wpa_driver_nl80211_send_action_cancel_wait,
9085 .remain_on_channel = wpa_driver_nl80211_remain_on_channel,
9086 .cancel_remain_on_channel =
9087 wpa_driver_nl80211_cancel_remain_on_channel,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009088 .probe_req_report = driver_nl80211_probe_req_report,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009089 .deinit_ap = wpa_driver_nl80211_deinit_ap,
Dmitry Shmidt04949592012-07-19 12:16:46 -07009090 .deinit_p2p_cli = wpa_driver_nl80211_deinit_p2p_cli,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009091 .resume = wpa_driver_nl80211_resume,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009092 .signal_monitor = nl80211_signal_monitor,
9093 .signal_poll = nl80211_signal_poll,
9094 .send_frame = nl80211_send_frame,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009095 .set_param = nl80211_set_param,
9096 .get_radio_name = nl80211_get_radio_name,
Jouni Malinen75ecf522011-06-27 15:19:46 -07009097 .add_pmkid = nl80211_add_pmkid,
9098 .remove_pmkid = nl80211_remove_pmkid,
9099 .flush_pmkid = nl80211_flush_pmkid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009100 .set_rekey_info = nl80211_set_rekey_info,
9101 .poll_client = nl80211_poll_client,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009102 .set_p2p_powersave = nl80211_set_p2p_powersave,
Dmitry Shmidtea69e842013-05-13 14:52:28 -07009103 .start_dfs_cac = nl80211_start_radar_detection,
9104 .stop_ap = wpa_driver_nl80211_stop_ap,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009105#ifdef CONFIG_TDLS
9106 .send_tdls_mgmt = nl80211_send_tdls_mgmt,
9107 .tdls_oper = nl80211_tdls_oper,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009108 .tdls_enable_channel_switch = nl80211_tdls_enable_channel_switch,
9109 .tdls_disable_channel_switch = nl80211_tdls_disable_channel_switch,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009110#endif /* CONFIG_TDLS */
Dmitry Shmidt700a1372013-03-15 14:14:44 -07009111 .update_ft_ies = wpa_driver_nl80211_update_ft_ies,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009112 .get_mac_addr = wpa_driver_nl80211_get_macaddr,
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07009113 .get_survey = wpa_driver_nl80211_get_survey,
Dmitry Shmidt56052862013-10-04 10:23:25 -07009114 .status = wpa_driver_nl80211_status,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08009115 .switch_channel = nl80211_switch_channel,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009116#ifdef ANDROID_P2P
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07009117 .set_noa = wpa_driver_set_p2p_noa,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009118 .get_noa = wpa_driver_get_p2p_noa,
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07009119 .set_ap_wps_ie = wpa_driver_set_ap_wps_p2p_ie,
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08009120#endif /* ANDROID_P2P */
Dmitry Shmidt738a26e2011-07-07 14:22:14 -07009121#ifdef ANDROID
Dmitry Shmidt41712582015-06-29 11:02:15 -07009122#ifndef ANDROID_LIB_STUB
Dmitry Shmidt738a26e2011-07-07 14:22:14 -07009123 .driver_cmd = wpa_driver_nl80211_driver_cmd,
Dmitry Shmidt41712582015-06-29 11:02:15 -07009124#endif /* !ANDROID_LIB_STUB */
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08009125#endif /* ANDROID */
Dmitry Shmidta38abf92014-03-06 13:38:44 -08009126 .vendor_cmd = nl80211_vendor_cmd,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08009127 .set_qos_map = nl80211_set_qos_map,
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07009128 .set_wowlan = nl80211_set_wowlan,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009129#ifdef CONFIG_DRIVER_NL80211_QCA
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07009130 .roaming = nl80211_roaming,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009131#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07009132 .set_mac_addr = nl80211_set_mac_addr,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009133#ifdef CONFIG_MESH
9134 .init_mesh = wpa_driver_nl80211_init_mesh,
9135 .join_mesh = wpa_driver_nl80211_join_mesh,
9136 .leave_mesh = wpa_driver_nl80211_leave_mesh,
9137#endif /* CONFIG_MESH */
9138 .br_add_ip_neigh = wpa_driver_br_add_ip_neigh,
9139 .br_delete_ip_neigh = wpa_driver_br_delete_ip_neigh,
9140 .br_port_set_attr = wpa_driver_br_port_set_attr,
9141 .br_set_net_param = wpa_driver_br_set_net_param,
9142 .add_tx_ts = nl80211_add_ts,
9143 .del_tx_ts = nl80211_del_ts,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009144#ifdef CONFIG_DRIVER_NL80211_QCA
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009145 .do_acs = wpa_driver_do_acs,
Ravi Joshie6ccb162015-07-16 17:45:41 -07009146 .set_band = nl80211_set_band,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009147 .get_pref_freq_list = nl80211_get_pref_freq_list,
9148 .set_prob_oper_freq = nl80211_set_prob_oper_freq,
9149#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009150};