blob: 4cb92d0a4439be43564c1f601a5bd8fc73267787 [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 Shmidt8d520ff2011-05-09 14:06:53 -0700184static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
185static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
186static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
Dmitry Shmidt738a26e2011-07-07 14:22:14 -0700187
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700188static int nl80211_set_channel(struct i802_bss *bss,
189 struct hostapd_freq_params *freq, int set_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700190static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
191 int ifindex, int disabled);
192
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800193static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv,
194 int reset_mode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800195
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700196static int i802_set_iface_flags(struct i802_bss *bss, int up);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800197static int nl80211_set_param(void *priv, const char *param);
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700198
199
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800200/* Converts nl80211_chan_width to a common format */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800201enum chan_width convert2width(int width)
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800202{
203 switch (width) {
204 case NL80211_CHAN_WIDTH_20_NOHT:
205 return CHAN_WIDTH_20_NOHT;
206 case NL80211_CHAN_WIDTH_20:
207 return CHAN_WIDTH_20;
208 case NL80211_CHAN_WIDTH_40:
209 return CHAN_WIDTH_40;
210 case NL80211_CHAN_WIDTH_80:
211 return CHAN_WIDTH_80;
212 case NL80211_CHAN_WIDTH_80P80:
213 return CHAN_WIDTH_80P80;
214 case NL80211_CHAN_WIDTH_160:
215 return CHAN_WIDTH_160;
216 }
217 return CHAN_WIDTH_UNKNOWN;
218}
219
220
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800221int is_ap_interface(enum nl80211_iftype nlmode)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800222{
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700223 return nlmode == NL80211_IFTYPE_AP ||
224 nlmode == NL80211_IFTYPE_P2P_GO;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800225}
226
227
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800228int is_sta_interface(enum nl80211_iftype nlmode)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800229{
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700230 return nlmode == NL80211_IFTYPE_STATION ||
231 nlmode == NL80211_IFTYPE_P2P_CLIENT;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800232}
233
234
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700235static int is_p2p_net_interface(enum nl80211_iftype nlmode)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800236{
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700237 return nlmode == NL80211_IFTYPE_P2P_CLIENT ||
238 nlmode == NL80211_IFTYPE_P2P_GO;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800239}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700240
241
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800242struct i802_bss * get_bss_ifindex(struct wpa_driver_nl80211_data *drv,
243 int ifindex)
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -0700244{
245 struct i802_bss *bss;
246
247 for (bss = drv->first_bss; bss; bss = bss->next) {
248 if (bss->ifindex == ifindex)
249 return bss;
250 }
251
252 return NULL;
253}
254
255
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800256static int is_mesh_interface(enum nl80211_iftype nlmode)
257{
258 return nlmode == NL80211_IFTYPE_MESH_POINT;
259}
260
261
262void nl80211_mark_disconnected(struct wpa_driver_nl80211_data *drv)
Dmitry Shmidt8bae4132013-06-06 11:25:10 -0700263{
264 if (drv->associated)
265 os_memcpy(drv->prev_bssid, drv->bssid, ETH_ALEN);
266 drv->associated = 0;
267 os_memset(drv->bssid, 0, ETH_ALEN);
268}
269
270
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700271/* nl80211 code */
272static int ack_handler(struct nl_msg *msg, void *arg)
273{
274 int *err = arg;
275 *err = 0;
276 return NL_STOP;
277}
278
279static int finish_handler(struct nl_msg *msg, void *arg)
280{
281 int *ret = arg;
282 *ret = 0;
283 return NL_SKIP;
284}
285
286static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
287 void *arg)
288{
289 int *ret = arg;
290 *ret = err->error;
291 return NL_SKIP;
292}
293
294
295static int no_seq_check(struct nl_msg *msg, void *arg)
296{
297 return NL_OK;
298}
299
300
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800301static void nl80211_nlmsg_clear(struct nl_msg *msg)
302{
303 /*
304 * Clear nlmsg data, e.g., to make sure key material is not left in
305 * heap memory for unnecessarily long time.
306 */
307 if (msg) {
308 struct nlmsghdr *hdr = nlmsg_hdr(msg);
309 void *data = nlmsg_data(hdr);
310 /*
311 * This would use nlmsg_datalen() or the older nlmsg_len() if
312 * only libnl were to maintain a stable API.. Neither will work
313 * with all released versions, so just calculate the length
314 * here.
315 */
316 int len = hdr->nlmsg_len - NLMSG_HDRLEN;
317
318 os_memset(data, 0, len);
319 }
320}
321
322
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800323static int send_and_recv(struct nl80211_global *global,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700324 struct nl_handle *nl_handle, struct nl_msg *msg,
325 int (*valid_handler)(struct nl_msg *, void *),
326 void *valid_data)
327{
328 struct nl_cb *cb;
329 int err = -ENOMEM;
330
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800331 if (!msg)
332 return -ENOMEM;
333
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800334 cb = nl_cb_clone(global->nl_cb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700335 if (!cb)
336 goto out;
337
338 err = nl_send_auto_complete(nl_handle, msg);
339 if (err < 0)
340 goto out;
341
342 err = 1;
343
344 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
345 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
346 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
347
348 if (valid_handler)
349 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM,
350 valid_handler, valid_data);
351
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700352 while (err > 0) {
353 int res = nl_recvmsgs(nl_handle, cb);
Dmitry Shmidt71757432014-06-02 13:50:35 -0700354 if (res < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700355 wpa_printf(MSG_INFO,
356 "nl80211: %s->nl_recvmsgs failed: %d",
357 __func__, res);
358 }
359 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700360 out:
361 nl_cb_put(cb);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800362 if (!valid_handler && valid_data == (void *) -1)
363 nl80211_nlmsg_clear(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700364 nlmsg_free(msg);
365 return err;
366}
367
368
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800369int send_and_recv_msgs(struct wpa_driver_nl80211_data *drv,
370 struct nl_msg *msg,
371 int (*valid_handler)(struct nl_msg *, void *),
372 void *valid_data)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700373{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800374 return send_and_recv(drv->global, drv->global->nl, msg,
375 valid_handler, valid_data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700376}
377
378
379struct family_data {
380 const char *group;
381 int id;
382};
383
384
385static int family_handler(struct nl_msg *msg, void *arg)
386{
387 struct family_data *res = arg;
388 struct nlattr *tb[CTRL_ATTR_MAX + 1];
389 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
390 struct nlattr *mcgrp;
391 int i;
392
393 nla_parse(tb, CTRL_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
394 genlmsg_attrlen(gnlh, 0), NULL);
395 if (!tb[CTRL_ATTR_MCAST_GROUPS])
396 return NL_SKIP;
397
398 nla_for_each_nested(mcgrp, tb[CTRL_ATTR_MCAST_GROUPS], i) {
399 struct nlattr *tb2[CTRL_ATTR_MCAST_GRP_MAX + 1];
400 nla_parse(tb2, CTRL_ATTR_MCAST_GRP_MAX, nla_data(mcgrp),
401 nla_len(mcgrp), NULL);
402 if (!tb2[CTRL_ATTR_MCAST_GRP_NAME] ||
403 !tb2[CTRL_ATTR_MCAST_GRP_ID] ||
404 os_strncmp(nla_data(tb2[CTRL_ATTR_MCAST_GRP_NAME]),
405 res->group,
406 nla_len(tb2[CTRL_ATTR_MCAST_GRP_NAME])) != 0)
407 continue;
408 res->id = nla_get_u32(tb2[CTRL_ATTR_MCAST_GRP_ID]);
409 break;
410 };
411
412 return NL_SKIP;
413}
414
415
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800416static int nl_get_multicast_id(struct nl80211_global *global,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700417 const char *family, const char *group)
418{
419 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800420 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700421 struct family_data res = { group, -ENOENT };
422
423 msg = nlmsg_alloc();
424 if (!msg)
425 return -ENOMEM;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800426 if (!genlmsg_put(msg, 0, 0, genl_ctrl_resolve(global->nl, "nlctrl"),
427 0, 0, CTRL_CMD_GETFAMILY, 0) ||
428 nla_put_string(msg, CTRL_ATTR_FAMILY_NAME, family)) {
429 nlmsg_free(msg);
430 return -1;
431 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700432
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800433 ret = send_and_recv(global, global->nl, msg, family_handler, &res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700434 if (ret == 0)
435 ret = res.id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700436 return ret;
437}
438
439
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800440void * nl80211_cmd(struct wpa_driver_nl80211_data *drv,
441 struct nl_msg *msg, int flags, uint8_t cmd)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800442{
443 return genlmsg_put(msg, 0, 0, drv->global->nl80211_id,
444 0, flags, cmd, 0);
445}
446
447
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800448static int nl80211_set_iface_id(struct nl_msg *msg, struct i802_bss *bss)
449{
450 if (bss->wdev_id_set)
451 return nla_put_u64(msg, NL80211_ATTR_WDEV, bss->wdev_id);
452 return nla_put_u32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
453}
454
455
456struct nl_msg * nl80211_cmd_msg(struct i802_bss *bss, int flags, uint8_t cmd)
457{
458 struct nl_msg *msg;
459
460 msg = nlmsg_alloc();
461 if (!msg)
462 return NULL;
463
464 if (!nl80211_cmd(bss->drv, msg, flags, cmd) ||
465 nl80211_set_iface_id(msg, bss) < 0) {
466 nlmsg_free(msg);
467 return NULL;
468 }
469
470 return msg;
471}
472
473
474static struct nl_msg *
475nl80211_ifindex_msg(struct wpa_driver_nl80211_data *drv, int ifindex,
476 int flags, uint8_t cmd)
477{
478 struct nl_msg *msg;
479
480 msg = nlmsg_alloc();
481 if (!msg)
482 return NULL;
483
484 if (!nl80211_cmd(drv, msg, flags, cmd) ||
485 nla_put_u32(msg, NL80211_ATTR_IFINDEX, ifindex)) {
486 nlmsg_free(msg);
487 return NULL;
488 }
489
490 return msg;
491}
492
493
494struct nl_msg * nl80211_drv_msg(struct wpa_driver_nl80211_data *drv, int flags,
495 uint8_t cmd)
496{
497 return nl80211_ifindex_msg(drv, drv->ifindex, flags, cmd);
498}
499
500
501struct nl_msg * nl80211_bss_msg(struct i802_bss *bss, int flags, uint8_t cmd)
502{
503 return nl80211_ifindex_msg(bss->drv, bss->ifindex, flags, cmd);
504}
505
506
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800507struct wiphy_idx_data {
508 int wiphy_idx;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700509 enum nl80211_iftype nlmode;
510 u8 *macaddr;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800511};
512
513
514static int netdev_info_handler(struct nl_msg *msg, void *arg)
515{
516 struct nlattr *tb[NL80211_ATTR_MAX + 1];
517 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
518 struct wiphy_idx_data *info = arg;
519
520 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
521 genlmsg_attrlen(gnlh, 0), NULL);
522
523 if (tb[NL80211_ATTR_WIPHY])
524 info->wiphy_idx = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
525
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700526 if (tb[NL80211_ATTR_IFTYPE])
527 info->nlmode = nla_get_u32(tb[NL80211_ATTR_IFTYPE]);
528
529 if (tb[NL80211_ATTR_MAC] && info->macaddr)
530 os_memcpy(info->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
531 ETH_ALEN);
532
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800533 return NL_SKIP;
534}
535
536
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800537int nl80211_get_wiphy_index(struct i802_bss *bss)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800538{
539 struct nl_msg *msg;
540 struct wiphy_idx_data data = {
541 .wiphy_idx = -1,
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700542 .macaddr = NULL,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800543 };
544
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800545 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_GET_INTERFACE)))
546 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800547
548 if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0)
549 return data.wiphy_idx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800550 return -1;
551}
552
553
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700554static enum nl80211_iftype nl80211_get_ifmode(struct i802_bss *bss)
555{
556 struct nl_msg *msg;
557 struct wiphy_idx_data data = {
558 .nlmode = NL80211_IFTYPE_UNSPECIFIED,
559 .macaddr = NULL,
560 };
561
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800562 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_GET_INTERFACE)))
563 return NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700564
565 if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0)
566 return data.nlmode;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700567 return NL80211_IFTYPE_UNSPECIFIED;
568}
569
570
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700571static int nl80211_get_macaddr(struct i802_bss *bss)
572{
573 struct nl_msg *msg;
574 struct wiphy_idx_data data = {
575 .macaddr = bss->addr,
576 };
577
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800578 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_GET_INTERFACE)))
579 return -1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700580
581 return send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data);
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700582}
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700583
584
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800585static int nl80211_register_beacons(struct wpa_driver_nl80211_data *drv,
586 struct nl80211_wiphy_data *w)
587{
588 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800589 int ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800590
591 msg = nlmsg_alloc();
592 if (!msg)
593 return -1;
594
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800595 if (!nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_BEACONS) ||
596 nla_put_u32(msg, NL80211_ATTR_WIPHY, w->wiphy_idx)) {
597 nlmsg_free(msg);
598 return -1;
599 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800600
601 ret = send_and_recv(drv->global, w->nl_beacons, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800602 if (ret) {
603 wpa_printf(MSG_DEBUG, "nl80211: Register beacons command "
604 "failed: ret=%d (%s)",
605 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800606 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800607 return ret;
608}
609
610
611static void nl80211_recv_beacons(int sock, void *eloop_ctx, void *handle)
612{
613 struct nl80211_wiphy_data *w = eloop_ctx;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700614 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800615
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800616 wpa_printf(MSG_EXCESSIVE, "nl80211: Beacon event message available");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800617
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700618 res = nl_recvmsgs(handle, w->nl_cb);
Dmitry Shmidt71757432014-06-02 13:50:35 -0700619 if (res < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700620 wpa_printf(MSG_INFO, "nl80211: %s->nl_recvmsgs failed: %d",
621 __func__, res);
622 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800623}
624
625
626static int process_beacon_event(struct nl_msg *msg, void *arg)
627{
628 struct nl80211_wiphy_data *w = arg;
629 struct wpa_driver_nl80211_data *drv;
630 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
631 struct nlattr *tb[NL80211_ATTR_MAX + 1];
632 union wpa_event_data event;
633
634 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
635 genlmsg_attrlen(gnlh, 0), NULL);
636
637 if (gnlh->cmd != NL80211_CMD_FRAME) {
638 wpa_printf(MSG_DEBUG, "nl80211: Unexpected beacon event? (%d)",
639 gnlh->cmd);
640 return NL_SKIP;
641 }
642
643 if (!tb[NL80211_ATTR_FRAME])
644 return NL_SKIP;
645
646 dl_list_for_each(drv, &w->drvs, struct wpa_driver_nl80211_data,
647 wiphy_list) {
648 os_memset(&event, 0, sizeof(event));
649 event.rx_mgmt.frame = nla_data(tb[NL80211_ATTR_FRAME]);
650 event.rx_mgmt.frame_len = nla_len(tb[NL80211_ATTR_FRAME]);
651 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
652 }
653
654 return NL_SKIP;
655}
656
657
658static struct nl80211_wiphy_data *
659nl80211_get_wiphy_data_ap(struct i802_bss *bss)
660{
661 static DEFINE_DL_LIST(nl80211_wiphys);
662 struct nl80211_wiphy_data *w;
663 int wiphy_idx, found = 0;
664 struct i802_bss *tmp_bss;
665
666 if (bss->wiphy_data != NULL)
667 return bss->wiphy_data;
668
669 wiphy_idx = nl80211_get_wiphy_index(bss);
670
671 dl_list_for_each(w, &nl80211_wiphys, struct nl80211_wiphy_data, list) {
672 if (w->wiphy_idx == wiphy_idx)
673 goto add;
674 }
675
676 /* alloc new one */
677 w = os_zalloc(sizeof(*w));
678 if (w == NULL)
679 return NULL;
680 w->wiphy_idx = wiphy_idx;
681 dl_list_init(&w->bsss);
682 dl_list_init(&w->drvs);
683
684 w->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
685 if (!w->nl_cb) {
686 os_free(w);
687 return NULL;
688 }
689 nl_cb_set(w->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL);
690 nl_cb_set(w->nl_cb, NL_CB_VALID, NL_CB_CUSTOM, process_beacon_event,
691 w);
692
693 w->nl_beacons = nl_create_handle(bss->drv->global->nl_cb,
694 "wiphy beacons");
695 if (w->nl_beacons == NULL) {
696 os_free(w);
697 return NULL;
698 }
699
700 if (nl80211_register_beacons(bss->drv, w)) {
701 nl_destroy_handles(&w->nl_beacons);
702 os_free(w);
703 return NULL;
704 }
705
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700706 nl80211_register_eloop_read(&w->nl_beacons, nl80211_recv_beacons, w);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800707
708 dl_list_add(&nl80211_wiphys, &w->list);
709
710add:
711 /* drv entry for this bss already there? */
712 dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
713 if (tmp_bss->drv == bss->drv) {
714 found = 1;
715 break;
716 }
717 }
718 /* if not add it */
719 if (!found)
720 dl_list_add(&w->drvs, &bss->drv->wiphy_list);
721
722 dl_list_add(&w->bsss, &bss->wiphy_list);
723 bss->wiphy_data = w;
724 return w;
725}
726
727
728static void nl80211_put_wiphy_data_ap(struct i802_bss *bss)
729{
730 struct nl80211_wiphy_data *w = bss->wiphy_data;
731 struct i802_bss *tmp_bss;
732 int found = 0;
733
734 if (w == NULL)
735 return;
736 bss->wiphy_data = NULL;
737 dl_list_del(&bss->wiphy_list);
738
739 /* still any for this drv present? */
740 dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
741 if (tmp_bss->drv == bss->drv) {
742 found = 1;
743 break;
744 }
745 }
746 /* if not remove it */
747 if (!found)
748 dl_list_del(&bss->drv->wiphy_list);
749
750 if (!dl_list_empty(&w->bsss))
751 return;
752
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700753 nl80211_destroy_eloop_handle(&w->nl_beacons);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800754
755 nl_cb_put(w->nl_cb);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800756 dl_list_del(&w->list);
757 os_free(w);
758}
759
760
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700761static int wpa_driver_nl80211_get_bssid(void *priv, u8 *bssid)
762{
763 struct i802_bss *bss = priv;
764 struct wpa_driver_nl80211_data *drv = bss->drv;
765 if (!drv->associated)
766 return -1;
767 os_memcpy(bssid, drv->bssid, ETH_ALEN);
768 return 0;
769}
770
771
772static int wpa_driver_nl80211_get_ssid(void *priv, u8 *ssid)
773{
774 struct i802_bss *bss = priv;
775 struct wpa_driver_nl80211_data *drv = bss->drv;
776 if (!drv->associated)
777 return -1;
778 os_memcpy(ssid, drv->ssid, drv->ssid_len);
779 return drv->ssid_len;
780}
781
782
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800783static void wpa_driver_nl80211_event_newlink(
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800784 struct wpa_driver_nl80211_data *drv, const char *ifname)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700785{
786 union wpa_event_data event;
787
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800788 if (os_strcmp(drv->first_bss->ifname, ifname) == 0) {
789 if (if_nametoindex(drv->first_bss->ifname) == 0) {
790 wpa_printf(MSG_DEBUG, "nl80211: Interface %s does not exist - ignore RTM_NEWLINK",
791 drv->first_bss->ifname);
792 return;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700793 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800794 if (!drv->if_removed)
795 return;
796 wpa_printf(MSG_DEBUG, "nl80211: Mark if_removed=0 for %s based on RTM_NEWLINK event",
797 drv->first_bss->ifname);
798 drv->if_removed = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700799 }
800
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800801 os_memset(&event, 0, sizeof(event));
802 os_strlcpy(event.interface_status.ifname, ifname,
803 sizeof(event.interface_status.ifname));
804 event.interface_status.ievent = EVENT_INTERFACE_ADDED;
805 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
806}
807
808
809static void wpa_driver_nl80211_event_dellink(
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800810 struct wpa_driver_nl80211_data *drv, const char *ifname)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800811{
812 union wpa_event_data event;
813
814 if (os_strcmp(drv->first_bss->ifname, ifname) == 0) {
815 if (drv->if_removed) {
816 wpa_printf(MSG_DEBUG, "nl80211: if_removed already set - ignore RTM_DELLINK event for %s",
817 ifname);
818 return;
819 }
820 wpa_printf(MSG_DEBUG, "RTM_DELLINK: Interface '%s' removed - mark if_removed=1",
821 ifname);
822 drv->if_removed = 1;
823 } else {
824 wpa_printf(MSG_DEBUG, "RTM_DELLINK: Interface '%s' removed",
825 ifname);
826 }
827
828 os_memset(&event, 0, sizeof(event));
829 os_strlcpy(event.interface_status.ifname, ifname,
830 sizeof(event.interface_status.ifname));
831 event.interface_status.ievent = EVENT_INTERFACE_REMOVED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700832 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
833}
834
835
836static int wpa_driver_nl80211_own_ifname(struct wpa_driver_nl80211_data *drv,
837 u8 *buf, size_t len)
838{
839 int attrlen, rta_len;
840 struct rtattr *attr;
841
842 attrlen = len;
843 attr = (struct rtattr *) buf;
844
845 rta_len = RTA_ALIGN(sizeof(struct rtattr));
846 while (RTA_OK(attr, attrlen)) {
847 if (attr->rta_type == IFLA_IFNAME) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800848 if (os_strcmp(((char *) attr) + rta_len,
849 drv->first_bss->ifname) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700850 return 1;
851 else
852 break;
853 }
854 attr = RTA_NEXT(attr, attrlen);
855 }
856
857 return 0;
858}
859
860
861static int wpa_driver_nl80211_own_ifindex(struct wpa_driver_nl80211_data *drv,
862 int ifindex, u8 *buf, size_t len)
863{
864 if (drv->ifindex == ifindex)
865 return 1;
866
867 if (drv->if_removed && wpa_driver_nl80211_own_ifname(drv, buf, len)) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800868 nl80211_check_global(drv->global);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700869 wpa_printf(MSG_DEBUG, "nl80211: Update ifindex for a removed "
870 "interface");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800871 wpa_driver_nl80211_finish_drv_init(drv, NULL, 0, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700872 return 1;
873 }
874
875 return 0;
876}
877
878
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800879static struct wpa_driver_nl80211_data *
880nl80211_find_drv(struct nl80211_global *global, int idx, u8 *buf, size_t len)
881{
882 struct wpa_driver_nl80211_data *drv;
883 dl_list_for_each(drv, &global->interfaces,
884 struct wpa_driver_nl80211_data, list) {
885 if (wpa_driver_nl80211_own_ifindex(drv, idx, buf, len) ||
886 have_ifidx(drv, idx))
887 return drv;
888 }
889 return NULL;
890}
891
892
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700893static void wpa_driver_nl80211_event_rtm_newlink(void *ctx,
894 struct ifinfomsg *ifi,
895 u8 *buf, size_t len)
896{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800897 struct nl80211_global *global = ctx;
898 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800899 int attrlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700900 struct rtattr *attr;
901 u32 brid = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800902 char namebuf[IFNAMSIZ];
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800903 char ifname[IFNAMSIZ + 1];
904 char extra[100], *pos, *end;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700905
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800906 drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
907 if (!drv) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800908 wpa_printf(MSG_DEBUG, "nl80211: Ignore RTM_NEWLINK event for foreign ifindex %d",
909 ifi->ifi_index);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700910 return;
911 }
912
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800913 extra[0] = '\0';
914 pos = extra;
915 end = pos + sizeof(extra);
916 ifname[0] = '\0';
917
918 attrlen = len;
919 attr = (struct rtattr *) buf;
920 while (RTA_OK(attr, attrlen)) {
921 switch (attr->rta_type) {
922 case IFLA_IFNAME:
923 if (RTA_PAYLOAD(attr) >= IFNAMSIZ)
924 break;
925 os_memcpy(ifname, RTA_DATA(attr), RTA_PAYLOAD(attr));
926 ifname[RTA_PAYLOAD(attr)] = '\0';
927 break;
928 case IFLA_MASTER:
929 brid = nla_get_u32((struct nlattr *) attr);
930 pos += os_snprintf(pos, end - pos, " master=%u", brid);
931 break;
932 case IFLA_WIRELESS:
933 pos += os_snprintf(pos, end - pos, " wext");
934 break;
935 case IFLA_OPERSTATE:
936 pos += os_snprintf(pos, end - pos, " operstate=%u",
937 nla_get_u32((struct nlattr *) attr));
938 break;
939 case IFLA_LINKMODE:
940 pos += os_snprintf(pos, end - pos, " linkmode=%u",
941 nla_get_u32((struct nlattr *) attr));
942 break;
943 }
944 attr = RTA_NEXT(attr, attrlen);
945 }
946 extra[sizeof(extra) - 1] = '\0';
947
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700948 wpa_printf(MSG_DEBUG, "RTM_NEWLINK: ifi_index=%d ifname=%s%s ifi_family=%d ifi_flags=0x%x (%s%s%s%s)",
949 ifi->ifi_index, ifname, extra, ifi->ifi_family,
950 ifi->ifi_flags,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700951 (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
952 (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
953 (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
954 (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
955
956 if (!drv->if_disabled && !(ifi->ifi_flags & IFF_UP)) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800957 namebuf[0] = '\0';
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800958 if (if_indextoname(ifi->ifi_index, namebuf) &&
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800959 linux_iface_up(drv->global->ioctl_sock, namebuf) > 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800960 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
961 "event since interface %s is up", namebuf);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800962 drv->ignore_if_down_event = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800963 return;
964 }
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800965 wpa_printf(MSG_DEBUG, "nl80211: Interface down (%s/%s)",
966 namebuf, ifname);
967 if (os_strcmp(drv->first_bss->ifname, ifname) != 0) {
968 wpa_printf(MSG_DEBUG,
969 "nl80211: Not the main interface (%s) - do not indicate interface down",
970 drv->first_bss->ifname);
971 } else if (drv->ignore_if_down_event) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800972 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
973 "event generated by mode change");
974 drv->ignore_if_down_event = 0;
975 } else {
976 drv->if_disabled = 1;
977 wpa_supplicant_event(drv->ctx,
978 EVENT_INTERFACE_DISABLED, NULL);
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800979
980 /*
981 * Try to get drv again, since it may be removed as
982 * part of the EVENT_INTERFACE_DISABLED handling for
983 * dynamic interfaces
984 */
985 drv = nl80211_find_drv(global, ifi->ifi_index,
986 buf, len);
987 if (!drv)
988 return;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800989 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700990 }
991
992 if (drv->if_disabled && (ifi->ifi_flags & IFF_UP)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800993 if (if_indextoname(ifi->ifi_index, namebuf) &&
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800994 linux_iface_up(drv->global->ioctl_sock, namebuf) == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800995 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
996 "event since interface %s is down",
997 namebuf);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800998 } else if (if_nametoindex(drv->first_bss->ifname) == 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700999 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
1000 "event since interface %s does not exist",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001001 drv->first_bss->ifname);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001002 } else if (drv->if_removed) {
1003 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
1004 "event since interface %s is marked "
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001005 "removed", drv->first_bss->ifname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001006 } else {
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07001007 struct i802_bss *bss;
1008 u8 addr[ETH_ALEN];
1009
1010 /* Re-read MAC address as it may have changed */
1011 bss = get_bss_ifindex(drv, ifi->ifi_index);
1012 if (bss &&
1013 linux_get_ifhwaddr(drv->global->ioctl_sock,
1014 bss->ifname, addr) < 0) {
1015 wpa_printf(MSG_DEBUG,
1016 "nl80211: %s: failed to re-read MAC address",
1017 bss->ifname);
1018 } else if (bss &&
1019 os_memcmp(addr, bss->addr, ETH_ALEN) != 0) {
1020 wpa_printf(MSG_DEBUG,
1021 "nl80211: Own MAC address on ifindex %d (%s) changed from "
1022 MACSTR " to " MACSTR,
1023 ifi->ifi_index, bss->ifname,
1024 MAC2STR(bss->addr),
1025 MAC2STR(addr));
1026 os_memcpy(bss->addr, addr, ETH_ALEN);
1027 }
1028
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001029 wpa_printf(MSG_DEBUG, "nl80211: Interface up");
1030 drv->if_disabled = 0;
1031 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_ENABLED,
1032 NULL);
1033 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001034 }
1035
1036 /*
1037 * Some drivers send the association event before the operup event--in
1038 * this case, lifting operstate in wpa_driver_nl80211_set_operstate()
1039 * fails. This will hit us when wpa_supplicant does not need to do
1040 * IEEE 802.1X authentication
1041 */
1042 if (drv->operstate == 1 &&
1043 (ifi->ifi_flags & (IFF_LOWER_UP | IFF_DORMANT)) == IFF_LOWER_UP &&
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001044 !(ifi->ifi_flags & IFF_RUNNING)) {
1045 wpa_printf(MSG_DEBUG, "nl80211: Set IF_OPER_UP again based on ifi_flags and expected operstate");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001046 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001047 -1, IF_OPER_UP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001048 }
1049
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001050 if (ifname[0])
1051 wpa_driver_nl80211_event_newlink(drv, ifname);
1052
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001053 if (ifi->ifi_family == AF_BRIDGE && brid) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001054 struct i802_bss *bss;
1055
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001056 /* device has been added to bridge */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001057 if (!if_indextoname(brid, namebuf)) {
1058 wpa_printf(MSG_DEBUG,
1059 "nl80211: Could not find bridge ifname for ifindex %u",
1060 brid);
1061 return;
1062 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001063 wpa_printf(MSG_DEBUG, "nl80211: Add ifindex %u for bridge %s",
1064 brid, namebuf);
1065 add_ifidx(drv, brid);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001066
1067 for (bss = drv->first_bss; bss; bss = bss->next) {
1068 if (os_strcmp(ifname, bss->ifname) == 0) {
1069 os_strlcpy(bss->brname, namebuf, IFNAMSIZ);
1070 break;
1071 }
1072 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001073 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001074}
1075
1076
1077static void wpa_driver_nl80211_event_rtm_dellink(void *ctx,
1078 struct ifinfomsg *ifi,
1079 u8 *buf, size_t len)
1080{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001081 struct nl80211_global *global = ctx;
1082 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001083 int attrlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001084 struct rtattr *attr;
1085 u32 brid = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001086 char ifname[IFNAMSIZ + 1];
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001087 char extra[100], *pos, *end;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001088
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001089 drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
1090 if (!drv) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001091 wpa_printf(MSG_DEBUG, "nl80211: Ignore RTM_DELLINK event for foreign ifindex %d",
1092 ifi->ifi_index);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001093 return;
1094 }
1095
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001096 extra[0] = '\0';
1097 pos = extra;
1098 end = pos + sizeof(extra);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001099 ifname[0] = '\0';
1100
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001101 attrlen = len;
1102 attr = (struct rtattr *) buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001103 while (RTA_OK(attr, attrlen)) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001104 switch (attr->rta_type) {
1105 case IFLA_IFNAME:
1106 if (RTA_PAYLOAD(attr) >= IFNAMSIZ)
1107 break;
1108 os_memcpy(ifname, RTA_DATA(attr), RTA_PAYLOAD(attr));
1109 ifname[RTA_PAYLOAD(attr)] = '\0';
1110 break;
1111 case IFLA_MASTER:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001112 brid = nla_get_u32((struct nlattr *) attr);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001113 pos += os_snprintf(pos, end - pos, " master=%u", brid);
1114 break;
1115 case IFLA_OPERSTATE:
1116 pos += os_snprintf(pos, end - pos, " operstate=%u",
1117 nla_get_u32((struct nlattr *) attr));
1118 break;
1119 case IFLA_LINKMODE:
1120 pos += os_snprintf(pos, end - pos, " linkmode=%u",
1121 nla_get_u32((struct nlattr *) attr));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001122 break;
1123 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001124 attr = RTA_NEXT(attr, attrlen);
1125 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001126 extra[sizeof(extra) - 1] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001127
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001128 wpa_printf(MSG_DEBUG, "RTM_DELLINK: ifi_index=%d ifname=%s%s ifi_family=%d ifi_flags=0x%x (%s%s%s%s)",
1129 ifi->ifi_index, ifname, extra, ifi->ifi_family,
1130 ifi->ifi_flags,
1131 (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
1132 (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
1133 (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
1134 (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
1135
1136 if (ifname[0] && (ifi->ifi_family != AF_BRIDGE || !brid))
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001137 wpa_driver_nl80211_event_dellink(drv, ifname);
1138
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001139 if (ifi->ifi_family == AF_BRIDGE && brid) {
1140 /* device has been removed from bridge */
1141 char namebuf[IFNAMSIZ];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001142
1143 if (!if_indextoname(brid, namebuf)) {
1144 wpa_printf(MSG_DEBUG,
1145 "nl80211: Could not find bridge ifname for ifindex %u",
1146 brid);
1147 } else {
1148 wpa_printf(MSG_DEBUG,
1149 "nl80211: Remove ifindex %u for bridge %s",
1150 brid, namebuf);
1151 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001152 del_ifidx(drv, brid);
1153 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001154}
1155
1156
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001157unsigned int nl80211_get_assoc_freq(struct wpa_driver_nl80211_data *drv)
Jouni Malinen87fd2792011-05-16 18:35:42 +03001158{
1159 struct nl_msg *msg;
1160 int ret;
1161 struct nl80211_bss_info_arg arg;
1162
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001163 msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
Jouni Malinen87fd2792011-05-16 18:35:42 +03001164 os_memset(&arg, 0, sizeof(arg));
Jouni Malinen87fd2792011-05-16 18:35:42 +03001165 arg.drv = drv;
1166 ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
Jouni Malinen87fd2792011-05-16 18:35:42 +03001167 if (ret == 0) {
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07001168 unsigned int freq = drv->nlmode == NL80211_IFTYPE_ADHOC ?
1169 arg.ibss_freq : arg.assoc_freq;
Jouni Malinen87fd2792011-05-16 18:35:42 +03001170 wpa_printf(MSG_DEBUG, "nl80211: Operating frequency for the "
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07001171 "associated BSS from scan results: %u MHz", freq);
1172 if (freq)
1173 drv->assoc_freq = freq;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001174 return drv->assoc_freq;
Jouni Malinen87fd2792011-05-16 18:35:42 +03001175 }
1176 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
1177 "(%s)", ret, strerror(-ret));
Jouni Malinen87fd2792011-05-16 18:35:42 +03001178 return drv->assoc_freq;
1179}
1180
1181
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001182static int get_link_signal(struct nl_msg *msg, void *arg)
1183{
1184 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1185 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1186 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
1187 static struct nla_policy policy[NL80211_STA_INFO_MAX + 1] = {
1188 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001189 [NL80211_STA_INFO_SIGNAL_AVG] = { .type = NLA_U8 },
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07001190 [NL80211_STA_INFO_BEACON_SIGNAL_AVG] = { .type = NLA_U8 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001191 };
1192 struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
1193 static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
1194 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
1195 [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
1196 [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
1197 [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
1198 };
1199 struct wpa_signal_info *sig_change = arg;
1200
1201 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1202 genlmsg_attrlen(gnlh, 0), NULL);
1203 if (!tb[NL80211_ATTR_STA_INFO] ||
1204 nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
1205 tb[NL80211_ATTR_STA_INFO], policy))
1206 return NL_SKIP;
1207 if (!sinfo[NL80211_STA_INFO_SIGNAL])
1208 return NL_SKIP;
1209
1210 sig_change->current_signal =
1211 (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
1212
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001213 if (sinfo[NL80211_STA_INFO_SIGNAL_AVG])
1214 sig_change->avg_signal =
1215 (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL_AVG]);
1216 else
1217 sig_change->avg_signal = 0;
1218
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07001219 if (sinfo[NL80211_STA_INFO_BEACON_SIGNAL_AVG])
1220 sig_change->avg_beacon_signal =
1221 (s8)
1222 nla_get_u8(sinfo[NL80211_STA_INFO_BEACON_SIGNAL_AVG]);
1223 else
1224 sig_change->avg_beacon_signal = 0;
1225
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001226 if (sinfo[NL80211_STA_INFO_TX_BITRATE]) {
1227 if (nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
1228 sinfo[NL80211_STA_INFO_TX_BITRATE],
1229 rate_policy)) {
1230 sig_change->current_txrate = 0;
1231 } else {
1232 if (rinfo[NL80211_RATE_INFO_BITRATE]) {
1233 sig_change->current_txrate =
1234 nla_get_u16(rinfo[
1235 NL80211_RATE_INFO_BITRATE]) * 100;
1236 }
1237 }
1238 }
1239
1240 return NL_SKIP;
1241}
1242
1243
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001244int nl80211_get_link_signal(struct wpa_driver_nl80211_data *drv,
1245 struct wpa_signal_info *sig)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001246{
1247 struct nl_msg *msg;
1248
1249 sig->current_signal = -9999;
1250 sig->current_txrate = 0;
1251
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001252 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_GET_STATION)) ||
1253 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid)) {
1254 nlmsg_free(msg);
1255 return -ENOBUFS;
1256 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001257
1258 return send_and_recv_msgs(drv, msg, get_link_signal, sig);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001259}
1260
1261
1262static int get_link_noise(struct nl_msg *msg, void *arg)
1263{
1264 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1265 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1266 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
1267 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
1268 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
1269 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
1270 };
1271 struct wpa_signal_info *sig_change = arg;
1272
1273 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1274 genlmsg_attrlen(gnlh, 0), NULL);
1275
1276 if (!tb[NL80211_ATTR_SURVEY_INFO]) {
1277 wpa_printf(MSG_DEBUG, "nl80211: survey data missing!");
1278 return NL_SKIP;
1279 }
1280
1281 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
1282 tb[NL80211_ATTR_SURVEY_INFO],
1283 survey_policy)) {
1284 wpa_printf(MSG_DEBUG, "nl80211: failed to parse nested "
1285 "attributes!");
1286 return NL_SKIP;
1287 }
1288
1289 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
1290 return NL_SKIP;
1291
1292 if (nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
1293 sig_change->frequency)
1294 return NL_SKIP;
1295
1296 if (!sinfo[NL80211_SURVEY_INFO_NOISE])
1297 return NL_SKIP;
1298
1299 sig_change->current_noise =
1300 (s8) nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
1301
1302 return NL_SKIP;
1303}
1304
1305
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001306int nl80211_get_link_noise(struct wpa_driver_nl80211_data *drv,
1307 struct wpa_signal_info *sig_change)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001308{
1309 struct nl_msg *msg;
1310
1311 sig_change->current_noise = 9999;
1312 sig_change->frequency = drv->assoc_freq;
1313
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001314 msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001315 return send_and_recv_msgs(drv, msg, get_link_noise, sig_change);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001316}
1317
1318
1319static void wpa_driver_nl80211_event_receive(int sock, void *eloop_ctx,
1320 void *handle)
1321{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001322 struct nl_cb *cb = eloop_ctx;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001323 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001324
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001325 wpa_printf(MSG_MSGDUMP, "nl80211: Event message available");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001326
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001327 res = nl_recvmsgs(handle, cb);
Dmitry Shmidt71757432014-06-02 13:50:35 -07001328 if (res < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001329 wpa_printf(MSG_INFO, "nl80211: %s->nl_recvmsgs failed: %d",
1330 __func__, res);
1331 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001332}
1333
1334
1335/**
1336 * wpa_driver_nl80211_set_country - ask nl80211 to set the regulatory domain
1337 * @priv: driver_nl80211 private data
1338 * @alpha2_arg: country to which to switch to
1339 * Returns: 0 on success, -1 on failure
1340 *
1341 * This asks nl80211 to set the regulatory domain for given
1342 * country ISO / IEC alpha2.
1343 */
1344static int wpa_driver_nl80211_set_country(void *priv, const char *alpha2_arg)
1345{
1346 struct i802_bss *bss = priv;
1347 struct wpa_driver_nl80211_data *drv = bss->drv;
1348 char alpha2[3];
1349 struct nl_msg *msg;
1350
1351 msg = nlmsg_alloc();
1352 if (!msg)
1353 return -ENOMEM;
1354
1355 alpha2[0] = alpha2_arg[0];
1356 alpha2[1] = alpha2_arg[1];
1357 alpha2[2] = '\0';
1358
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001359 if (!nl80211_cmd(drv, msg, 0, NL80211_CMD_REQ_SET_REG) ||
1360 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, alpha2)) {
1361 nlmsg_free(msg);
1362 return -EINVAL;
1363 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001364 if (send_and_recv_msgs(drv, msg, NULL, NULL))
1365 return -EINVAL;
1366 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001367}
1368
1369
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001370static int nl80211_get_country(struct nl_msg *msg, void *arg)
1371{
1372 char *alpha2 = arg;
1373 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
1374 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1375
1376 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1377 genlmsg_attrlen(gnlh, 0), NULL);
1378 if (!tb_msg[NL80211_ATTR_REG_ALPHA2]) {
1379 wpa_printf(MSG_DEBUG, "nl80211: No country information available");
1380 return NL_SKIP;
1381 }
1382 os_strlcpy(alpha2, nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]), 3);
1383 return NL_SKIP;
1384}
1385
1386
1387static int wpa_driver_nl80211_get_country(void *priv, char *alpha2)
1388{
1389 struct i802_bss *bss = priv;
1390 struct wpa_driver_nl80211_data *drv = bss->drv;
1391 struct nl_msg *msg;
1392 int ret;
1393
1394 msg = nlmsg_alloc();
1395 if (!msg)
1396 return -ENOMEM;
1397
1398 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG);
1399 alpha2[0] = '\0';
1400 ret = send_and_recv_msgs(drv, msg, nl80211_get_country, alpha2);
1401 if (!alpha2[0])
1402 ret = -1;
1403
1404 return ret;
1405}
1406
1407
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001408static int wpa_driver_nl80211_init_nl_global(struct nl80211_global *global)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001409{
1410 int ret;
1411
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001412 global->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
1413 if (global->nl_cb == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001414 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
1415 "callbacks");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001416 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001417 }
1418
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001419 global->nl = nl_create_handle(global->nl_cb, "nl");
1420 if (global->nl == NULL)
1421 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001422
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001423 global->nl80211_id = genl_ctrl_resolve(global->nl, "nl80211");
1424 if (global->nl80211_id < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001425 wpa_printf(MSG_ERROR, "nl80211: 'nl80211' generic netlink not "
1426 "found");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001427 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001428 }
1429
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001430 global->nl_event = nl_create_handle(global->nl_cb, "event");
1431 if (global->nl_event == NULL)
1432 goto err;
1433
1434 ret = nl_get_multicast_id(global, "nl80211", "scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001435 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001436 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001437 if (ret < 0) {
1438 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
1439 "membership for scan events: %d (%s)",
1440 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001441 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001442 }
1443
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001444 ret = nl_get_multicast_id(global, "nl80211", "mlme");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001445 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001446 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001447 if (ret < 0) {
1448 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
1449 "membership for mlme events: %d (%s)",
1450 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001451 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001452 }
1453
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001454 ret = nl_get_multicast_id(global, "nl80211", "regulatory");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001455 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001456 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001457 if (ret < 0) {
1458 wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
1459 "membership for regulatory events: %d (%s)",
1460 ret, strerror(-ret));
1461 /* Continue without regulatory events */
1462 }
1463
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001464 ret = nl_get_multicast_id(global, "nl80211", "vendor");
1465 if (ret >= 0)
1466 ret = nl_socket_add_membership(global->nl_event, ret);
1467 if (ret < 0) {
1468 wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
1469 "membership for vendor events: %d (%s)",
1470 ret, strerror(-ret));
1471 /* Continue without vendor events */
1472 }
1473
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001474 nl_cb_set(global->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
1475 no_seq_check, NULL);
1476 nl_cb_set(global->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
1477 process_global_event, global);
1478
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001479 nl80211_register_eloop_read(&global->nl_event,
1480 wpa_driver_nl80211_event_receive,
1481 global->nl_cb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001482
1483 return 0;
1484
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001485err:
1486 nl_destroy_handles(&global->nl_event);
1487 nl_destroy_handles(&global->nl);
1488 nl_cb_put(global->nl_cb);
1489 global->nl_cb = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001490 return -1;
1491}
1492
1493
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001494static void nl80211_check_global(struct nl80211_global *global)
1495{
1496 struct nl_handle *handle;
1497 const char *groups[] = { "scan", "mlme", "regulatory", "vendor", NULL };
1498 int ret;
1499 unsigned int i;
1500
1501 /*
1502 * Try to re-add memberships to handle case of cfg80211 getting reloaded
1503 * and all registration having been cleared.
1504 */
1505 handle = (void *) (((intptr_t) global->nl_event) ^
1506 ELOOP_SOCKET_INVALID);
1507
1508 for (i = 0; groups[i]; i++) {
1509 ret = nl_get_multicast_id(global, "nl80211", groups[i]);
1510 if (ret >= 0)
1511 ret = nl_socket_add_membership(handle, ret);
1512 if (ret < 0) {
1513 wpa_printf(MSG_INFO,
1514 "nl80211: Could not re-add multicast membership for %s events: %d (%s)",
1515 groups[i], ret, strerror(-ret));
1516 }
1517 }
1518}
1519
1520
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001521static void wpa_driver_nl80211_rfkill_blocked(void *ctx)
1522{
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001523 struct wpa_driver_nl80211_data *drv = ctx;
1524
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001525 wpa_printf(MSG_DEBUG, "nl80211: RFKILL blocked");
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001526
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001527 /*
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001528 * rtnetlink ifdown handler will report interfaces other than the P2P
1529 * Device interface as disabled.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001530 */
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001531 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
1532 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_DISABLED, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001533}
1534
1535
1536static void wpa_driver_nl80211_rfkill_unblocked(void *ctx)
1537{
1538 struct wpa_driver_nl80211_data *drv = ctx;
1539 wpa_printf(MSG_DEBUG, "nl80211: RFKILL unblocked");
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001540 if (i802_set_iface_flags(drv->first_bss, 1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001541 wpa_printf(MSG_DEBUG, "nl80211: Could not set interface UP "
1542 "after rfkill unblock");
1543 return;
1544 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001545
1546 if (is_p2p_net_interface(drv->nlmode))
1547 nl80211_disable_11b_rates(drv, drv->ifindex, 1);
1548
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001549 /*
1550 * rtnetlink ifup handler will report interfaces other than the P2P
1551 * Device interface as enabled.
1552 */
1553 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
1554 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_ENABLED, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001555}
1556
1557
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001558static void wpa_driver_nl80211_handle_eapol_tx_status(int sock,
1559 void *eloop_ctx,
1560 void *handle)
1561{
1562 struct wpa_driver_nl80211_data *drv = eloop_ctx;
1563 u8 data[2048];
1564 struct msghdr msg;
1565 struct iovec entry;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001566 u8 control[512];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001567 struct cmsghdr *cmsg;
1568 int res, found_ee = 0, found_wifi = 0, acked = 0;
1569 union wpa_event_data event;
1570
1571 memset(&msg, 0, sizeof(msg));
1572 msg.msg_iov = &entry;
1573 msg.msg_iovlen = 1;
1574 entry.iov_base = data;
1575 entry.iov_len = sizeof(data);
1576 msg.msg_control = &control;
1577 msg.msg_controllen = sizeof(control);
1578
1579 res = recvmsg(sock, &msg, MSG_ERRQUEUE);
1580 /* if error or not fitting 802.3 header, return */
1581 if (res < 14)
1582 return;
1583
1584 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg))
1585 {
1586 if (cmsg->cmsg_level == SOL_SOCKET &&
1587 cmsg->cmsg_type == SCM_WIFI_STATUS) {
1588 int *ack;
1589
1590 found_wifi = 1;
1591 ack = (void *)CMSG_DATA(cmsg);
1592 acked = *ack;
1593 }
1594
1595 if (cmsg->cmsg_level == SOL_PACKET &&
1596 cmsg->cmsg_type == PACKET_TX_TIMESTAMP) {
1597 struct sock_extended_err *err =
1598 (struct sock_extended_err *)CMSG_DATA(cmsg);
1599
1600 if (err->ee_origin == SO_EE_ORIGIN_TXSTATUS)
1601 found_ee = 1;
1602 }
1603 }
1604
1605 if (!found_ee || !found_wifi)
1606 return;
1607
1608 memset(&event, 0, sizeof(event));
1609 event.eapol_tx_status.dst = data;
1610 event.eapol_tx_status.data = data + 14;
1611 event.eapol_tx_status.data_len = res - 14;
1612 event.eapol_tx_status.ack = acked;
1613 wpa_supplicant_event(drv->ctx, EVENT_EAPOL_TX_STATUS, &event);
1614}
1615
1616
1617static int nl80211_init_bss(struct i802_bss *bss)
1618{
1619 bss->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
1620 if (!bss->nl_cb)
1621 return -1;
1622
1623 nl_cb_set(bss->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
1624 no_seq_check, NULL);
1625 nl_cb_set(bss->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
1626 process_bss_event, bss);
1627
1628 return 0;
1629}
1630
1631
1632static void nl80211_destroy_bss(struct i802_bss *bss)
1633{
1634 nl_cb_put(bss->nl_cb);
1635 bss->nl_cb = NULL;
1636}
1637
1638
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001639static void
1640wpa_driver_nl80211_drv_init_rfkill(struct wpa_driver_nl80211_data *drv)
1641{
1642 struct rfkill_config *rcfg;
1643
1644 if (drv->rfkill)
1645 return;
1646
1647 rcfg = os_zalloc(sizeof(*rcfg));
1648 if (!rcfg)
1649 return;
1650
1651 rcfg->ctx = drv;
1652
1653 /* rfkill uses netdev sysfs for initialization. However, P2P Device is
1654 * not associated with a netdev, so use the name of some other interface
1655 * sharing the same wiphy as the P2P Device interface.
1656 *
1657 * Note: This is valid, as a P2P Device interface is always dynamically
1658 * created and is created only once another wpa_s interface was added.
1659 */
1660 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE) {
1661 struct nl80211_global *global = drv->global;
1662 struct wpa_driver_nl80211_data *tmp1;
1663
1664 dl_list_for_each(tmp1, &global->interfaces,
1665 struct wpa_driver_nl80211_data, list) {
1666 if (drv == tmp1 || drv->wiphy_idx != tmp1->wiphy_idx ||
1667 !tmp1->rfkill)
1668 continue;
1669
1670 wpa_printf(MSG_DEBUG,
1671 "nl80211: Use (%s) to initialize P2P Device rfkill",
1672 tmp1->first_bss->ifname);
1673 os_strlcpy(rcfg->ifname, tmp1->first_bss->ifname,
1674 sizeof(rcfg->ifname));
1675 break;
1676 }
1677 } else {
1678 os_strlcpy(rcfg->ifname, drv->first_bss->ifname,
1679 sizeof(rcfg->ifname));
1680 }
1681
1682 rcfg->blocked_cb = wpa_driver_nl80211_rfkill_blocked;
1683 rcfg->unblocked_cb = wpa_driver_nl80211_rfkill_unblocked;
1684 drv->rfkill = rfkill_init(rcfg);
1685 if (!drv->rfkill) {
1686 wpa_printf(MSG_DEBUG, "nl80211: RFKILL status not available");
1687 os_free(rcfg);
1688 }
1689}
1690
1691
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001692static void * wpa_driver_nl80211_drv_init(void *ctx, const char *ifname,
1693 void *global_priv, int hostapd,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001694 const u8 *set_addr,
1695 const char *driver_params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001696{
1697 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001698 struct i802_bss *bss;
1699
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001700 if (global_priv == NULL)
1701 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001702 drv = os_zalloc(sizeof(*drv));
1703 if (drv == NULL)
1704 return NULL;
1705 drv->global = global_priv;
1706 drv->ctx = ctx;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001707 drv->hostapd = !!hostapd;
1708 drv->eapol_sock = -1;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001709
1710 /*
1711 * There is no driver capability flag for this, so assume it is
1712 * supported and disable this on first attempt to use if the driver
1713 * rejects the command due to missing support.
1714 */
1715 drv->set_rekey_offload = 1;
1716
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001717 drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int);
1718 drv->if_indices = drv->default_if_indices;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001719
1720 drv->first_bss = os_zalloc(sizeof(*drv->first_bss));
1721 if (!drv->first_bss) {
1722 os_free(drv);
1723 return NULL;
1724 }
1725 bss = drv->first_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001726 bss->drv = drv;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001727 bss->ctx = ctx;
1728
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001729 os_strlcpy(bss->ifname, ifname, sizeof(bss->ifname));
1730 drv->monitor_ifidx = -1;
1731 drv->monitor_sock = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001732 drv->eapol_tx_sock = -1;
1733 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001734
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001735 if (nl80211_init_bss(bss))
1736 goto failed;
1737
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001738 if (wpa_driver_nl80211_finish_drv_init(drv, set_addr, 1, driver_params))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001739 goto failed;
1740
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001741 drv->eapol_tx_sock = socket(PF_PACKET, SOCK_DGRAM, 0);
1742 if (drv->eapol_tx_sock < 0)
1743 goto failed;
1744
1745 if (drv->data_tx_status) {
1746 int enabled = 1;
1747
1748 if (setsockopt(drv->eapol_tx_sock, SOL_SOCKET, SO_WIFI_STATUS,
1749 &enabled, sizeof(enabled)) < 0) {
1750 wpa_printf(MSG_DEBUG,
1751 "nl80211: wifi status sockopt failed\n");
1752 drv->data_tx_status = 0;
1753 if (!drv->use_monitor)
1754 drv->capa.flags &=
1755 ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
1756 } else {
1757 eloop_register_read_sock(drv->eapol_tx_sock,
1758 wpa_driver_nl80211_handle_eapol_tx_status,
1759 drv, NULL);
1760 }
1761 }
1762
1763 if (drv->global) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001764 nl80211_check_global(drv->global);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001765 dl_list_add(&drv->global->interfaces, &drv->list);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001766 drv->in_interface_list = 1;
1767 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001768
1769 return bss;
1770
1771failed:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001772 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001773 return NULL;
1774}
1775
1776
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001777/**
1778 * wpa_driver_nl80211_init - Initialize nl80211 driver interface
1779 * @ctx: context to be used when calling wpa_supplicant functions,
1780 * e.g., wpa_supplicant_event()
1781 * @ifname: interface name, e.g., wlan0
1782 * @global_priv: private driver global data from global_init()
1783 * Returns: Pointer to private data, %NULL on failure
1784 */
1785static void * wpa_driver_nl80211_init(void *ctx, const char *ifname,
1786 void *global_priv)
1787{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001788 return wpa_driver_nl80211_drv_init(ctx, ifname, global_priv, 0, NULL,
1789 NULL);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001790}
1791
1792
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001793static int nl80211_register_frame(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001794 struct nl_handle *nl_handle,
1795 u16 type, const u8 *match, size_t match_len)
1796{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001797 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001798 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001799 int ret;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001800 char buf[30];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001801
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001802 buf[0] = '\0';
1803 wpa_snprintf_hex(buf, sizeof(buf), match, match_len);
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07001804 wpa_printf(MSG_DEBUG, "nl80211: Register frame type=0x%x (%s) nl_handle=%p match=%s",
1805 type, fc2str(type), nl_handle, buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001806
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001807 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_REGISTER_ACTION)) ||
1808 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE, type) ||
1809 nla_put(msg, NL80211_ATTR_FRAME_MATCH, match_len, match)) {
1810 nlmsg_free(msg);
1811 return -1;
1812 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001813
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001814 ret = send_and_recv(drv->global, nl_handle, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001815 if (ret) {
1816 wpa_printf(MSG_DEBUG, "nl80211: Register frame command "
1817 "failed (type=%u): ret=%d (%s)",
1818 type, ret, strerror(-ret));
1819 wpa_hexdump(MSG_DEBUG, "nl80211: Register frame match",
1820 match, match_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001821 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001822 return ret;
1823}
1824
1825
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001826static int nl80211_alloc_mgmt_handle(struct i802_bss *bss)
1827{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001828 if (bss->nl_mgmt) {
1829 wpa_printf(MSG_DEBUG, "nl80211: Mgmt reporting "
1830 "already on! (nl_mgmt=%p)", bss->nl_mgmt);
1831 return -1;
1832 }
1833
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001834 bss->nl_mgmt = nl_create_handle(bss->nl_cb, "mgmt");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001835 if (bss->nl_mgmt == NULL)
1836 return -1;
1837
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001838 return 0;
1839}
1840
1841
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001842static void nl80211_mgmt_handle_register_eloop(struct i802_bss *bss)
1843{
1844 nl80211_register_eloop_read(&bss->nl_mgmt,
1845 wpa_driver_nl80211_event_receive,
1846 bss->nl_cb);
1847}
1848
1849
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001850static int nl80211_register_action_frame(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001851 const u8 *match, size_t match_len)
1852{
1853 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_ACTION << 4);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001854 return nl80211_register_frame(bss, bss->nl_mgmt,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001855 type, match, match_len);
1856}
1857
1858
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001859static int nl80211_mgmt_subscribe_non_ap(struct i802_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001860{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001861 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001862 int ret = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001863
1864 if (nl80211_alloc_mgmt_handle(bss))
1865 return -1;
1866 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with non-AP "
1867 "handle %p", bss->nl_mgmt);
1868
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001869 if (drv->nlmode == NL80211_IFTYPE_ADHOC) {
1870 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_AUTH << 4);
1871
1872 /* register for any AUTH message */
1873 nl80211_register_frame(bss, bss->nl_mgmt, type, NULL, 0);
1874 }
1875
Dmitry Shmidt051af732013-10-22 13:52:46 -07001876#ifdef CONFIG_INTERWORKING
1877 /* QoS Map Configure */
1878 if (nl80211_register_action_frame(bss, (u8 *) "\x01\x04", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001879 ret = -1;
Dmitry Shmidt051af732013-10-22 13:52:46 -07001880#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001881#if defined(CONFIG_P2P) || defined(CONFIG_INTERWORKING)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001882 /* GAS Initial Request */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001883 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0a", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001884 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001885 /* GAS Initial Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001886 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0b", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001887 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001888 /* GAS Comeback Request */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001889 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0c", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001890 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001891 /* GAS Comeback Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001892 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0d", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001893 ret = -1;
Dmitry Shmidt18463232014-01-24 12:29:41 -08001894 /* Protected GAS Initial Request */
1895 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0a", 2) < 0)
1896 ret = -1;
1897 /* Protected GAS Initial Response */
1898 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0b", 2) < 0)
1899 ret = -1;
1900 /* Protected GAS Comeback Request */
1901 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0c", 2) < 0)
1902 ret = -1;
1903 /* Protected GAS Comeback Response */
1904 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0d", 2) < 0)
1905 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001906#endif /* CONFIG_P2P || CONFIG_INTERWORKING */
1907#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001908 /* P2P Public Action */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001909 if (nl80211_register_action_frame(bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001910 (u8 *) "\x04\x09\x50\x6f\x9a\x09",
1911 6) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001912 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001913 /* P2P Action */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001914 if (nl80211_register_action_frame(bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001915 (u8 *) "\x7f\x50\x6f\x9a\x09",
1916 5) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001917 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001918#endif /* CONFIG_P2P */
1919#ifdef CONFIG_IEEE80211W
1920 /* SA Query Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001921 if (nl80211_register_action_frame(bss, (u8 *) "\x08\x01", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001922 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001923#endif /* CONFIG_IEEE80211W */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001924#ifdef CONFIG_TDLS
1925 if ((drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT)) {
1926 /* TDLS Discovery Response */
1927 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0e", 2) <
1928 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001929 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001930 }
1931#endif /* CONFIG_TDLS */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001932#ifdef CONFIG_FST
1933 /* FST Action frames */
1934 if (nl80211_register_action_frame(bss, (u8 *) "\x12", 1) < 0)
1935 ret = -1;
1936#endif /* CONFIG_FST */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001937
1938 /* FT Action frames */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001939 if (nl80211_register_action_frame(bss, (u8 *) "\x06", 1) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001940 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001941 else
1942 drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_FT |
1943 WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK;
1944
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001945 /* WNM - BSS Transition Management Request */
1946 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x07", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001947 ret = -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001948 /* WNM-Sleep Mode Response */
1949 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x11", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001950 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001951
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001952#ifdef CONFIG_HS20
1953 /* WNM-Notification */
1954 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x1a", 2) < 0)
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001955 ret = -1;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001956#endif /* CONFIG_HS20 */
1957
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001958 /* WMM-AC ADDTS Response */
1959 if (nl80211_register_action_frame(bss, (u8 *) "\x11\x01", 2) < 0)
1960 ret = -1;
1961
1962 /* WMM-AC DELTS */
1963 if (nl80211_register_action_frame(bss, (u8 *) "\x11\x02", 2) < 0)
1964 ret = -1;
1965
1966 /* Radio Measurement - Neighbor Report Response */
1967 if (nl80211_register_action_frame(bss, (u8 *) "\x05\x05", 2) < 0)
1968 ret = -1;
1969
1970 /* Radio Measurement - Link Measurement Request */
1971 if ((drv->capa.rrm_flags & WPA_DRIVER_FLAGS_TX_POWER_INSERTION) &&
1972 (nl80211_register_action_frame(bss, (u8 *) "\x05\x02", 2) < 0))
1973 ret = -1;
1974
1975 nl80211_mgmt_handle_register_eloop(bss);
1976
1977 return ret;
1978}
1979
1980
1981static int nl80211_mgmt_subscribe_mesh(struct i802_bss *bss)
1982{
1983 int ret = 0;
1984
1985 if (nl80211_alloc_mgmt_handle(bss))
1986 return -1;
1987
1988 wpa_printf(MSG_DEBUG,
1989 "nl80211: Subscribe to mgmt frames with mesh handle %p",
1990 bss->nl_mgmt);
1991
1992 /* Auth frames for mesh SAE */
1993 if (nl80211_register_frame(bss, bss->nl_mgmt,
1994 (WLAN_FC_TYPE_MGMT << 2) |
1995 (WLAN_FC_STYPE_AUTH << 4),
1996 NULL, 0) < 0)
1997 ret = -1;
1998
1999 /* Mesh peering open */
2000 if (nl80211_register_action_frame(bss, (u8 *) "\x0f\x01", 2) < 0)
2001 ret = -1;
2002 /* Mesh peering confirm */
2003 if (nl80211_register_action_frame(bss, (u8 *) "\x0f\x02", 2) < 0)
2004 ret = -1;
2005 /* Mesh peering close */
2006 if (nl80211_register_action_frame(bss, (u8 *) "\x0f\x03", 2) < 0)
2007 ret = -1;
2008
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002009 nl80211_mgmt_handle_register_eloop(bss);
2010
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002011 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002012}
2013
2014
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002015static int nl80211_register_spurious_class3(struct i802_bss *bss)
2016{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002017 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002018 int ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002019
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002020 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_UNEXPECTED_FRAME);
2021 ret = send_and_recv(bss->drv->global, bss->nl_mgmt, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002022 if (ret) {
2023 wpa_printf(MSG_DEBUG, "nl80211: Register spurious class3 "
2024 "failed: ret=%d (%s)",
2025 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002026 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002027 return ret;
2028}
2029
2030
2031static int nl80211_mgmt_subscribe_ap(struct i802_bss *bss)
2032{
2033 static const int stypes[] = {
2034 WLAN_FC_STYPE_AUTH,
2035 WLAN_FC_STYPE_ASSOC_REQ,
2036 WLAN_FC_STYPE_REASSOC_REQ,
2037 WLAN_FC_STYPE_DISASSOC,
2038 WLAN_FC_STYPE_DEAUTH,
2039 WLAN_FC_STYPE_ACTION,
2040 WLAN_FC_STYPE_PROBE_REQ,
2041/* Beacon doesn't work as mac80211 doesn't currently allow
2042 * it, but it wouldn't really be the right thing anyway as
2043 * it isn't per interface ... maybe just dump the scan
2044 * results periodically for OLBC?
2045 */
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07002046 /* WLAN_FC_STYPE_BEACON, */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002047 };
2048 unsigned int i;
2049
2050 if (nl80211_alloc_mgmt_handle(bss))
2051 return -1;
2052 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
2053 "handle %p", bss->nl_mgmt);
2054
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002055 for (i = 0; i < ARRAY_SIZE(stypes); i++) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002056 if (nl80211_register_frame(bss, bss->nl_mgmt,
2057 (WLAN_FC_TYPE_MGMT << 2) |
2058 (stypes[i] << 4),
2059 NULL, 0) < 0) {
2060 goto out_err;
2061 }
2062 }
2063
2064 if (nl80211_register_spurious_class3(bss))
2065 goto out_err;
2066
2067 if (nl80211_get_wiphy_data_ap(bss) == NULL)
2068 goto out_err;
2069
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002070 nl80211_mgmt_handle_register_eloop(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002071 return 0;
2072
2073out_err:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002074 nl_destroy_handles(&bss->nl_mgmt);
2075 return -1;
2076}
2077
2078
2079static int nl80211_mgmt_subscribe_ap_dev_sme(struct i802_bss *bss)
2080{
2081 if (nl80211_alloc_mgmt_handle(bss))
2082 return -1;
2083 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
2084 "handle %p (device SME)", bss->nl_mgmt);
2085
2086 if (nl80211_register_frame(bss, bss->nl_mgmt,
2087 (WLAN_FC_TYPE_MGMT << 2) |
2088 (WLAN_FC_STYPE_ACTION << 4),
2089 NULL, 0) < 0)
2090 goto out_err;
2091
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002092 nl80211_mgmt_handle_register_eloop(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002093 return 0;
2094
2095out_err:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002096 nl_destroy_handles(&bss->nl_mgmt);
2097 return -1;
2098}
2099
2100
2101static void nl80211_mgmt_unsubscribe(struct i802_bss *bss, const char *reason)
2102{
2103 if (bss->nl_mgmt == NULL)
2104 return;
2105 wpa_printf(MSG_DEBUG, "nl80211: Unsubscribe mgmt frames handle %p "
2106 "(%s)", bss->nl_mgmt, reason);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002107 nl80211_destroy_eloop_handle(&bss->nl_mgmt);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002108
2109 nl80211_put_wiphy_data_ap(bss);
2110}
2111
2112
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002113static void wpa_driver_nl80211_send_rfkill(void *eloop_ctx, void *timeout_ctx)
2114{
2115 wpa_supplicant_event(timeout_ctx, EVENT_INTERFACE_DISABLED, NULL);
2116}
2117
2118
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002119static void nl80211_del_p2pdev(struct i802_bss *bss)
2120{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002121 struct nl_msg *msg;
2122 int ret;
2123
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002124 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_DEL_INTERFACE);
2125 ret = send_and_recv_msgs(bss->drv, msg, NULL, NULL);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002126
2127 wpa_printf(MSG_DEBUG, "nl80211: Delete P2P Device %s (0x%llx): %s",
2128 bss->ifname, (long long unsigned int) bss->wdev_id,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002129 strerror(-ret));
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002130}
2131
2132
2133static int nl80211_set_p2pdev(struct i802_bss *bss, int start)
2134{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002135 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002136 int ret;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002137
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002138 msg = nl80211_cmd_msg(bss, 0, start ? NL80211_CMD_START_P2P_DEVICE :
2139 NL80211_CMD_STOP_P2P_DEVICE);
2140 ret = send_and_recv_msgs(bss->drv, msg, NULL, NULL);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002141
2142 wpa_printf(MSG_DEBUG, "nl80211: %s P2P Device %s (0x%llx): %s",
2143 start ? "Start" : "Stop",
2144 bss->ifname, (long long unsigned int) bss->wdev_id,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002145 strerror(-ret));
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002146 return ret;
2147}
2148
2149
2150static int i802_set_iface_flags(struct i802_bss *bss, int up)
2151{
2152 enum nl80211_iftype nlmode;
2153
2154 nlmode = nl80211_get_ifmode(bss);
2155 if (nlmode != NL80211_IFTYPE_P2P_DEVICE) {
2156 return linux_set_iface_flags(bss->drv->global->ioctl_sock,
2157 bss->ifname, up);
2158 }
2159
2160 /* P2P Device has start/stop which is equivalent */
2161 return nl80211_set_p2pdev(bss, up);
2162}
2163
2164
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002165#ifdef CONFIG_TESTING_OPTIONS
2166static int qca_vendor_test_cmd_handler(struct nl_msg *msg, void *arg)
2167{
2168 /* struct wpa_driver_nl80211_data *drv = arg; */
2169 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2170 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2171
2172
2173 wpa_printf(MSG_DEBUG,
2174 "nl80211: QCA vendor test command response received");
2175
2176 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2177 genlmsg_attrlen(gnlh, 0), NULL);
2178 if (!tb[NL80211_ATTR_VENDOR_DATA]) {
2179 wpa_printf(MSG_DEBUG, "nl80211: No vendor data attribute");
2180 return NL_SKIP;
2181 }
2182
2183 wpa_hexdump(MSG_DEBUG,
2184 "nl80211: Received QCA vendor test command response",
2185 nla_data(tb[NL80211_ATTR_VENDOR_DATA]),
2186 nla_len(tb[NL80211_ATTR_VENDOR_DATA]));
2187
2188 return NL_SKIP;
2189}
2190#endif /* CONFIG_TESTING_OPTIONS */
2191
2192
2193static void qca_vendor_test(struct wpa_driver_nl80211_data *drv)
2194{
2195#ifdef CONFIG_TESTING_OPTIONS
2196 struct nl_msg *msg;
2197 struct nlattr *params;
2198 int ret;
2199
2200 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
2201 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
2202 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
2203 QCA_NL80211_VENDOR_SUBCMD_TEST) ||
2204 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
2205 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_TEST, 123)) {
2206 nlmsg_free(msg);
2207 return;
2208 }
2209 nla_nest_end(msg, params);
2210
2211 ret = send_and_recv_msgs(drv, msg, qca_vendor_test_cmd_handler, drv);
2212 wpa_printf(MSG_DEBUG,
2213 "nl80211: QCA vendor test command returned %d (%s)",
2214 ret, strerror(-ret));
2215#endif /* CONFIG_TESTING_OPTIONS */
2216}
2217
2218
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002219static int
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002220wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002221 const u8 *set_addr, int first,
2222 const char *driver_params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002223{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002224 struct i802_bss *bss = drv->first_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002225 int send_rfkill_event = 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002226 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002227
2228 drv->ifindex = if_nametoindex(bss->ifname);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002229 bss->ifindex = drv->ifindex;
2230 bss->wdev_id = drv->global->if_add_wdevid;
2231 bss->wdev_id_set = drv->global->if_add_wdevid_set;
2232
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002233 bss->if_dynamic = drv->ifindex == drv->global->if_add_ifindex;
2234 bss->if_dynamic = bss->if_dynamic || drv->global->if_add_wdevid_set;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002235 drv->global->if_add_wdevid_set = 0;
2236
Dmitry Shmidt03658832014-08-13 11:03:49 -07002237 if (!bss->if_dynamic && nl80211_get_ifmode(bss) == NL80211_IFTYPE_AP)
2238 bss->static_ap = 1;
2239
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -08002240 if (first &&
2241 nl80211_get_ifmode(bss) != NL80211_IFTYPE_P2P_DEVICE &&
2242 linux_iface_up(drv->global->ioctl_sock, bss->ifname) > 0)
2243 drv->start_iface_up = 1;
2244
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002245 if (wpa_driver_nl80211_capa(drv))
2246 return -1;
2247
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002248 if (driver_params && nl80211_set_param(bss, driver_params) < 0)
2249 return -1;
2250
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002251 wpa_printf(MSG_DEBUG, "nl80211: interface %s in phy %s",
2252 bss->ifname, drv->phyname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002253
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002254 if (set_addr &&
2255 (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0) ||
2256 linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
2257 set_addr)))
2258 return -1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002259
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002260 if (first && nl80211_get_ifmode(bss) == NL80211_IFTYPE_AP)
2261 drv->start_mode_ap = 1;
2262
Dmitry Shmidt03658832014-08-13 11:03:49 -07002263 if (drv->hostapd || bss->static_ap)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002264 nlmode = NL80211_IFTYPE_AP;
2265 else if (bss->if_dynamic)
2266 nlmode = nl80211_get_ifmode(bss);
2267 else
2268 nlmode = NL80211_IFTYPE_STATION;
2269
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002270 if (wpa_driver_nl80211_set_mode(bss, nlmode) < 0) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002271 wpa_printf(MSG_ERROR, "nl80211: Could not configure driver mode");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002272 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002273 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002274
Dmitry Shmidt98660862014-03-11 17:26:21 -07002275 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002276 nl80211_get_macaddr(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002277
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08002278 wpa_driver_nl80211_drv_init_rfkill(drv);
2279
Dmitry Shmidt98660862014-03-11 17:26:21 -07002280 if (!rfkill_is_blocked(drv->rfkill)) {
2281 int ret = i802_set_iface_flags(bss, 1);
2282 if (ret) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002283 wpa_printf(MSG_ERROR, "nl80211: Could not set "
2284 "interface '%s' UP", bss->ifname);
Dmitry Shmidt98660862014-03-11 17:26:21 -07002285 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002286 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002287
2288 if (is_p2p_net_interface(nlmode))
2289 nl80211_disable_11b_rates(bss->drv,
2290 bss->drv->ifindex, 1);
2291
Dmitry Shmidt98660862014-03-11 17:26:21 -07002292 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
2293 return ret;
2294 } else {
2295 wpa_printf(MSG_DEBUG, "nl80211: Could not yet enable "
2296 "interface '%s' due to rfkill", bss->ifname);
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08002297 if (nlmode != NL80211_IFTYPE_P2P_DEVICE)
2298 drv->if_disabled = 1;
2299
Dmitry Shmidt98660862014-03-11 17:26:21 -07002300 send_rfkill_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002301 }
2302
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08002303 if (!drv->hostapd && nlmode != NL80211_IFTYPE_P2P_DEVICE)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002304 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
2305 1, IF_OPER_DORMANT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002306
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08002307 if (nlmode != NL80211_IFTYPE_P2P_DEVICE) {
2308 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
2309 bss->addr))
2310 return -1;
2311 os_memcpy(drv->perm_addr, bss->addr, ETH_ALEN);
2312 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002313
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002314 if (send_rfkill_event) {
2315 eloop_register_timeout(0, 0, wpa_driver_nl80211_send_rfkill,
2316 drv, drv->ctx);
2317 }
2318
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002319 if (drv->vendor_cmd_test_avail)
2320 qca_vendor_test(drv);
2321
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002322 return 0;
2323}
2324
2325
2326static int wpa_driver_nl80211_del_beacon(struct wpa_driver_nl80211_data *drv)
2327{
2328 struct nl_msg *msg;
2329
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002330 wpa_printf(MSG_DEBUG, "nl80211: Remove beacon (ifindex=%d)",
2331 drv->ifindex);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002332 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_DEL_BEACON);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002333 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002334}
2335
2336
2337/**
2338 * wpa_driver_nl80211_deinit - Deinitialize nl80211 driver interface
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002339 * @bss: Pointer to private nl80211 data from wpa_driver_nl80211_init()
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002340 *
2341 * Shut down driver interface and processing of driver events. Free
2342 * private data buffer if one was allocated in wpa_driver_nl80211_init().
2343 */
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002344static void wpa_driver_nl80211_deinit(struct i802_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002345{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002346 struct wpa_driver_nl80211_data *drv = bss->drv;
2347
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002348 wpa_printf(MSG_INFO, "nl80211: deinit ifname=%s disabled_11b_rates=%d",
2349 bss->ifname, drv->disabled_11b_rates);
2350
Dmitry Shmidt04949592012-07-19 12:16:46 -07002351 bss->in_deinit = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002352 if (drv->data_tx_status)
2353 eloop_unregister_read_sock(drv->eapol_tx_sock);
2354 if (drv->eapol_tx_sock >= 0)
2355 close(drv->eapol_tx_sock);
2356
2357 if (bss->nl_preq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002358 wpa_driver_nl80211_probe_req_report(bss, 0);
2359 if (bss->added_if_into_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002360 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
2361 bss->ifname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002362 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
2363 "interface %s from bridge %s: %s",
2364 bss->ifname, bss->brname, strerror(errno));
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002365 if (drv->rtnl_sk)
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07002366 nl80211_handle_destroy(drv->rtnl_sk);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002367 }
2368 if (bss->added_bridge) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002369 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->brname,
2370 0) < 0)
2371 wpa_printf(MSG_INFO,
2372 "nl80211: Could not set bridge %s down",
2373 bss->brname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002374 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002375 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
2376 "bridge %s: %s",
2377 bss->brname, strerror(errno));
2378 }
2379
2380 nl80211_remove_monitor_interface(drv);
2381
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002382 if (is_ap_interface(drv->nlmode))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002383 wpa_driver_nl80211_del_beacon(drv);
2384
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002385 if (drv->eapol_sock >= 0) {
2386 eloop_unregister_read_sock(drv->eapol_sock);
2387 close(drv->eapol_sock);
2388 }
2389
2390 if (drv->if_indices != drv->default_if_indices)
2391 os_free(drv->if_indices);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002392
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002393 if (drv->disabled_11b_rates)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002394 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
2395
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002396 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, 0,
2397 IF_OPER_UP);
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07002398 eloop_cancel_timeout(wpa_driver_nl80211_send_rfkill, drv, drv->ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002399 rfkill_deinit(drv->rfkill);
2400
2401 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
2402
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002403 if (!drv->start_iface_up)
2404 (void) i802_set_iface_flags(bss, 0);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002405
2406 if (drv->addr_changed) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002407 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname,
2408 0) < 0) {
2409 wpa_printf(MSG_DEBUG,
2410 "nl80211: Could not set interface down to restore permanent MAC address");
2411 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002412 if (linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
2413 drv->perm_addr) < 0) {
2414 wpa_printf(MSG_DEBUG,
2415 "nl80211: Could not restore permanent MAC address");
2416 }
2417 }
2418
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002419 if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002420 if (!drv->hostapd || !drv->start_mode_ap)
2421 wpa_driver_nl80211_set_mode(bss,
2422 NL80211_IFTYPE_STATION);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002423 nl80211_mgmt_unsubscribe(bss, "deinit");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002424 } else {
2425 nl80211_mgmt_unsubscribe(bss, "deinit");
2426 nl80211_del_p2pdev(bss);
2427 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002428
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002429 nl80211_destroy_bss(drv->first_bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002430
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002431 os_free(drv->filter_ssids);
2432
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002433 os_free(drv->auth_ie);
2434
2435 if (drv->in_interface_list)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002436 dl_list_del(&drv->list);
2437
Dmitry Shmidt444d5672013-04-01 13:08:44 -07002438 os_free(drv->extended_capa);
2439 os_free(drv->extended_capa_mask);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002440 os_free(drv->first_bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002441 os_free(drv);
2442}
2443
2444
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002445static u32 wpa_alg_to_cipher_suite(enum wpa_alg alg, size_t key_len)
2446{
2447 switch (alg) {
2448 case WPA_ALG_WEP:
2449 if (key_len == 5)
2450 return WLAN_CIPHER_SUITE_WEP40;
2451 return WLAN_CIPHER_SUITE_WEP104;
2452 case WPA_ALG_TKIP:
2453 return WLAN_CIPHER_SUITE_TKIP;
2454 case WPA_ALG_CCMP:
2455 return WLAN_CIPHER_SUITE_CCMP;
2456 case WPA_ALG_GCMP:
2457 return WLAN_CIPHER_SUITE_GCMP;
2458 case WPA_ALG_CCMP_256:
2459 return WLAN_CIPHER_SUITE_CCMP_256;
2460 case WPA_ALG_GCMP_256:
2461 return WLAN_CIPHER_SUITE_GCMP_256;
2462 case WPA_ALG_IGTK:
2463 return WLAN_CIPHER_SUITE_AES_CMAC;
2464 case WPA_ALG_BIP_GMAC_128:
2465 return WLAN_CIPHER_SUITE_BIP_GMAC_128;
2466 case WPA_ALG_BIP_GMAC_256:
2467 return WLAN_CIPHER_SUITE_BIP_GMAC_256;
2468 case WPA_ALG_BIP_CMAC_256:
2469 return WLAN_CIPHER_SUITE_BIP_CMAC_256;
2470 case WPA_ALG_SMS4:
2471 return WLAN_CIPHER_SUITE_SMS4;
2472 case WPA_ALG_KRK:
2473 return WLAN_CIPHER_SUITE_KRK;
2474 case WPA_ALG_NONE:
2475 case WPA_ALG_PMK:
2476 wpa_printf(MSG_ERROR, "nl80211: Unexpected encryption algorithm %d",
2477 alg);
2478 return 0;
2479 }
2480
2481 wpa_printf(MSG_ERROR, "nl80211: Unsupported encryption algorithm %d",
2482 alg);
2483 return 0;
2484}
2485
2486
2487static u32 wpa_cipher_to_cipher_suite(unsigned int cipher)
2488{
2489 switch (cipher) {
2490 case WPA_CIPHER_CCMP_256:
2491 return WLAN_CIPHER_SUITE_CCMP_256;
2492 case WPA_CIPHER_GCMP_256:
2493 return WLAN_CIPHER_SUITE_GCMP_256;
2494 case WPA_CIPHER_CCMP:
2495 return WLAN_CIPHER_SUITE_CCMP;
2496 case WPA_CIPHER_GCMP:
2497 return WLAN_CIPHER_SUITE_GCMP;
2498 case WPA_CIPHER_TKIP:
2499 return WLAN_CIPHER_SUITE_TKIP;
2500 case WPA_CIPHER_WEP104:
2501 return WLAN_CIPHER_SUITE_WEP104;
2502 case WPA_CIPHER_WEP40:
2503 return WLAN_CIPHER_SUITE_WEP40;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002504 case WPA_CIPHER_GTK_NOT_USED:
2505 return WLAN_CIPHER_SUITE_NO_GROUP_ADDR;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002506 }
2507
2508 return 0;
2509}
2510
2511
2512static int wpa_cipher_to_cipher_suites(unsigned int ciphers, u32 suites[],
2513 int max_suites)
2514{
2515 int num_suites = 0;
2516
2517 if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP_256)
2518 suites[num_suites++] = WLAN_CIPHER_SUITE_CCMP_256;
2519 if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP_256)
2520 suites[num_suites++] = WLAN_CIPHER_SUITE_GCMP_256;
2521 if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP)
2522 suites[num_suites++] = WLAN_CIPHER_SUITE_CCMP;
2523 if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP)
2524 suites[num_suites++] = WLAN_CIPHER_SUITE_GCMP;
2525 if (num_suites < max_suites && ciphers & WPA_CIPHER_TKIP)
2526 suites[num_suites++] = WLAN_CIPHER_SUITE_TKIP;
2527 if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP104)
2528 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP104;
2529 if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP40)
2530 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP40;
2531
2532 return num_suites;
2533}
2534
2535
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002536#ifdef CONFIG_DRIVER_NL80211_QCA
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002537static int issue_key_mgmt_set_key(struct wpa_driver_nl80211_data *drv,
2538 const u8 *key, size_t key_len)
2539{
2540 struct nl_msg *msg;
2541 int ret;
2542
2543 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_KEY_MGMT_OFFLOAD))
2544 return 0;
2545
2546 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
2547 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
2548 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
2549 QCA_NL80211_VENDOR_SUBCMD_KEY_MGMT_SET_KEY) ||
2550 nla_put(msg, NL80211_ATTR_VENDOR_DATA, key_len, key)) {
2551 nl80211_nlmsg_clear(msg);
2552 nlmsg_free(msg);
2553 return -1;
2554 }
2555 ret = send_and_recv_msgs(drv, msg, NULL, (void *) -1);
2556 if (ret) {
2557 wpa_printf(MSG_DEBUG,
2558 "nl80211: Key management set key failed: ret=%d (%s)",
2559 ret, strerror(-ret));
2560 }
2561
2562 return ret;
2563}
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002564#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002565
2566
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002567static int wpa_driver_nl80211_set_key(const char *ifname, struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002568 enum wpa_alg alg, const u8 *addr,
2569 int key_idx, int set_tx,
2570 const u8 *seq, size_t seq_len,
2571 const u8 *key, size_t key_len)
2572{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002573 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002574 int ifindex;
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002575 struct nl_msg *msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002576 int ret;
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07002577 int tdls = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002578
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002579 /* Ignore for P2P Device */
2580 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
2581 return 0;
2582
2583 ifindex = if_nametoindex(ifname);
2584 wpa_printf(MSG_DEBUG, "%s: ifindex=%d (%s) alg=%d addr=%p key_idx=%d "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002585 "set_tx=%d seq_len=%lu key_len=%lu",
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002586 __func__, ifindex, ifname, alg, addr, key_idx, set_tx,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002587 (unsigned long) seq_len, (unsigned long) key_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002588#ifdef CONFIG_TDLS
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07002589 if (key_idx == -1) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002590 key_idx = 0;
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07002591 tdls = 1;
2592 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002593#endif /* CONFIG_TDLS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002594
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002595#ifdef CONFIG_DRIVER_NL80211_QCA
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002596 if (alg == WPA_ALG_PMK &&
2597 (drv->capa.flags & WPA_DRIVER_FLAGS_KEY_MGMT_OFFLOAD)) {
2598 wpa_printf(MSG_DEBUG, "%s: calling issue_key_mgmt_set_key",
2599 __func__);
2600 ret = issue_key_mgmt_set_key(drv, key, key_len);
2601 return ret;
2602 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002603#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002604
2605 if (alg == WPA_ALG_NONE) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002606 msg = nl80211_ifindex_msg(drv, ifindex, 0, NL80211_CMD_DEL_KEY);
2607 if (!msg)
2608 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002609 } else {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002610 u32 suite;
2611
2612 suite = wpa_alg_to_cipher_suite(alg, key_len);
2613 if (!suite)
2614 goto fail;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002615 msg = nl80211_ifindex_msg(drv, ifindex, 0, NL80211_CMD_NEW_KEY);
2616 if (!msg ||
2617 nla_put(msg, NL80211_ATTR_KEY_DATA, key_len, key) ||
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002618 nla_put_u32(msg, NL80211_ATTR_KEY_CIPHER, suite))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002619 goto fail;
Dmitry Shmidt98660862014-03-11 17:26:21 -07002620 wpa_hexdump_key(MSG_DEBUG, "nl80211: KEY_DATA", key, key_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002621 }
2622
Dmitry Shmidt98660862014-03-11 17:26:21 -07002623 if (seq && seq_len) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002624 if (nla_put(msg, NL80211_ATTR_KEY_SEQ, seq_len, seq))
2625 goto fail;
Dmitry Shmidt98660862014-03-11 17:26:21 -07002626 wpa_hexdump(MSG_DEBUG, "nl80211: KEY_SEQ", seq, seq_len);
2627 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002628
2629 if (addr && !is_broadcast_ether_addr(addr)) {
2630 wpa_printf(MSG_DEBUG, " addr=" MACSTR, MAC2STR(addr));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002631 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
2632 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002633
2634 if (alg != WPA_ALG_WEP && key_idx && !set_tx) {
2635 wpa_printf(MSG_DEBUG, " RSN IBSS RX GTK");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002636 if (nla_put_u32(msg, NL80211_ATTR_KEY_TYPE,
2637 NL80211_KEYTYPE_GROUP))
2638 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002639 }
2640 } else if (addr && is_broadcast_ether_addr(addr)) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002641 struct nlattr *types;
2642
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002643 wpa_printf(MSG_DEBUG, " broadcast key");
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002644
2645 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002646 if (!types ||
2647 nla_put_flag(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST))
2648 goto fail;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002649 nla_nest_end(msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002650 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002651 if (nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
2652 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002653
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002654 ret = send_and_recv_msgs(drv, msg, NULL, key ? (void *) -1 : NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002655 if ((ret == -ENOENT || ret == -ENOLINK) && alg == WPA_ALG_NONE)
2656 ret = 0;
2657 if (ret)
2658 wpa_printf(MSG_DEBUG, "nl80211: set_key failed; err=%d %s)",
2659 ret, strerror(-ret));
2660
2661 /*
2662 * If we failed or don't need to set the default TX key (below),
2663 * we're done here.
2664 */
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07002665 if (ret || !set_tx || alg == WPA_ALG_NONE || tdls)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002666 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002667 if (is_ap_interface(drv->nlmode) && addr &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002668 !is_broadcast_ether_addr(addr))
2669 return ret;
2670
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002671 msg = nl80211_ifindex_msg(drv, ifindex, 0, NL80211_CMD_SET_KEY);
2672 if (!msg ||
2673 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx) ||
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002674 nla_put_flag(msg, (alg == WPA_ALG_IGTK ||
2675 alg == WPA_ALG_BIP_GMAC_128 ||
2676 alg == WPA_ALG_BIP_GMAC_256 ||
2677 alg == WPA_ALG_BIP_CMAC_256) ?
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002678 NL80211_ATTR_KEY_DEFAULT_MGMT :
2679 NL80211_ATTR_KEY_DEFAULT))
2680 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002681 if (addr && is_broadcast_ether_addr(addr)) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002682 struct nlattr *types;
2683
2684 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002685 if (!types ||
2686 nla_put_flag(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST))
2687 goto fail;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002688 nla_nest_end(msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002689 } else if (addr) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002690 struct nlattr *types;
2691
2692 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002693 if (!types ||
2694 nla_put_flag(msg, NL80211_KEY_DEFAULT_TYPE_UNICAST))
2695 goto fail;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002696 nla_nest_end(msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002697 }
2698
2699 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
2700 if (ret == -ENOENT)
2701 ret = 0;
2702 if (ret)
2703 wpa_printf(MSG_DEBUG, "nl80211: set_key default failed; "
2704 "err=%d %s)", ret, strerror(-ret));
2705 return ret;
2706
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002707fail:
2708 nl80211_nlmsg_clear(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002709 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002710 return -ENOBUFS;
2711}
2712
2713
2714static int nl_add_key(struct nl_msg *msg, enum wpa_alg alg,
2715 int key_idx, int defkey,
2716 const u8 *seq, size_t seq_len,
2717 const u8 *key, size_t key_len)
2718{
2719 struct nlattr *key_attr = nla_nest_start(msg, NL80211_ATTR_KEY);
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002720 u32 suite;
2721
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002722 if (!key_attr)
2723 return -1;
2724
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002725 suite = wpa_alg_to_cipher_suite(alg, key_len);
2726 if (!suite)
2727 return -1;
2728
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002729 if (defkey && alg == WPA_ALG_IGTK) {
2730 if (nla_put_flag(msg, NL80211_KEY_DEFAULT_MGMT))
2731 return -1;
2732 } else if (defkey) {
2733 if (nla_put_flag(msg, NL80211_KEY_DEFAULT))
2734 return -1;
2735 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002736
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002737 if (nla_put_u8(msg, NL80211_KEY_IDX, key_idx) ||
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002738 nla_put_u32(msg, NL80211_KEY_CIPHER, suite) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002739 (seq && seq_len &&
2740 nla_put(msg, NL80211_KEY_SEQ, seq_len, seq)) ||
2741 nla_put(msg, NL80211_KEY_DATA, key_len, key))
2742 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002743
2744 nla_nest_end(msg, key_attr);
2745
2746 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002747}
2748
2749
2750static int nl80211_set_conn_keys(struct wpa_driver_associate_params *params,
2751 struct nl_msg *msg)
2752{
2753 int i, privacy = 0;
2754 struct nlattr *nl_keys, *nl_key;
2755
2756 for (i = 0; i < 4; i++) {
2757 if (!params->wep_key[i])
2758 continue;
2759 privacy = 1;
2760 break;
2761 }
2762 if (params->wps == WPS_MODE_PRIVACY)
2763 privacy = 1;
2764 if (params->pairwise_suite &&
2765 params->pairwise_suite != WPA_CIPHER_NONE)
2766 privacy = 1;
2767
2768 if (!privacy)
2769 return 0;
2770
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002771 if (nla_put_flag(msg, NL80211_ATTR_PRIVACY))
2772 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002773
2774 nl_keys = nla_nest_start(msg, NL80211_ATTR_KEYS);
2775 if (!nl_keys)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002776 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002777
2778 for (i = 0; i < 4; i++) {
2779 if (!params->wep_key[i])
2780 continue;
2781
2782 nl_key = nla_nest_start(msg, i);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002783 if (!nl_key ||
2784 nla_put(msg, NL80211_KEY_DATA, params->wep_key_len[i],
2785 params->wep_key[i]) ||
2786 nla_put_u32(msg, NL80211_KEY_CIPHER,
2787 params->wep_key_len[i] == 5 ?
2788 WLAN_CIPHER_SUITE_WEP40 :
2789 WLAN_CIPHER_SUITE_WEP104) ||
2790 nla_put_u8(msg, NL80211_KEY_IDX, i) ||
2791 (i == params->wep_tx_keyidx &&
2792 nla_put_flag(msg, NL80211_KEY_DEFAULT)))
2793 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002794
2795 nla_nest_end(msg, nl_key);
2796 }
2797 nla_nest_end(msg, nl_keys);
2798
2799 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002800}
2801
2802
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002803int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
2804 const u8 *addr, int cmd, u16 reason_code,
2805 int local_state_change)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002806{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002807 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002808 struct nl_msg *msg;
2809
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002810 if (!(msg = nl80211_drv_msg(drv, 0, cmd)) ||
2811 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code) ||
2812 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
2813 (local_state_change &&
2814 nla_put_flag(msg, NL80211_ATTR_LOCAL_STATE_CHANGE))) {
2815 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002816 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002817 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002818
2819 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002820 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002821 wpa_dbg(drv->ctx, MSG_DEBUG,
2822 "nl80211: MLME command failed: reason=%u ret=%d (%s)",
2823 reason_code, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002824 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002825 return ret;
2826}
2827
2828
2829static int wpa_driver_nl80211_disconnect(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002830 int reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002831{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002832 int ret;
2833
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002834 wpa_printf(MSG_DEBUG, "%s(reason_code=%d)", __func__, reason_code);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07002835 nl80211_mark_disconnected(drv);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002836 /* Disconnect command doesn't need BSSID - it uses cached value */
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002837 ret = wpa_driver_nl80211_mlme(drv, NULL, NL80211_CMD_DISCONNECT,
2838 reason_code, 0);
2839 /*
2840 * For locally generated disconnect, supplicant already generates a
2841 * DEAUTH event, so ignore the event from NL80211.
2842 */
2843 drv->ignore_next_local_disconnect = ret == 0;
2844
2845 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002846}
2847
2848
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002849static int wpa_driver_nl80211_deauthenticate(struct i802_bss *bss,
2850 const u8 *addr, int reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002851{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002852 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07002853 int ret;
Dmitry Shmidt413dde72014-04-11 10:23:22 -07002854
2855 if (drv->nlmode == NL80211_IFTYPE_ADHOC) {
2856 nl80211_mark_disconnected(drv);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002857 return nl80211_leave_ibss(drv, 1);
Dmitry Shmidt413dde72014-04-11 10:23:22 -07002858 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002859 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME))
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002860 return wpa_driver_nl80211_disconnect(drv, reason_code);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002861 wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " reason_code=%d)",
2862 __func__, MAC2STR(addr), reason_code);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07002863 nl80211_mark_disconnected(drv);
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07002864 ret = wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DEAUTHENTICATE,
2865 reason_code, 0);
2866 /*
2867 * For locally generated deauthenticate, supplicant already generates a
2868 * DEAUTH event, so ignore the event from NL80211.
2869 */
2870 drv->ignore_next_local_deauth = ret == 0;
2871 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002872}
2873
2874
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002875static void nl80211_copy_auth_params(struct wpa_driver_nl80211_data *drv,
2876 struct wpa_driver_auth_params *params)
2877{
2878 int i;
2879
2880 drv->auth_freq = params->freq;
2881 drv->auth_alg = params->auth_alg;
2882 drv->auth_wep_tx_keyidx = params->wep_tx_keyidx;
2883 drv->auth_local_state_change = params->local_state_change;
2884 drv->auth_p2p = params->p2p;
2885
2886 if (params->bssid)
2887 os_memcpy(drv->auth_bssid_, params->bssid, ETH_ALEN);
2888 else
2889 os_memset(drv->auth_bssid_, 0, ETH_ALEN);
2890
2891 if (params->ssid) {
2892 os_memcpy(drv->auth_ssid, params->ssid, params->ssid_len);
2893 drv->auth_ssid_len = params->ssid_len;
2894 } else
2895 drv->auth_ssid_len = 0;
2896
2897
2898 os_free(drv->auth_ie);
2899 drv->auth_ie = NULL;
2900 drv->auth_ie_len = 0;
2901 if (params->ie) {
2902 drv->auth_ie = os_malloc(params->ie_len);
2903 if (drv->auth_ie) {
2904 os_memcpy(drv->auth_ie, params->ie, params->ie_len);
2905 drv->auth_ie_len = params->ie_len;
2906 }
2907 }
2908
2909 for (i = 0; i < 4; i++) {
2910 if (params->wep_key[i] && params->wep_key_len[i] &&
2911 params->wep_key_len[i] <= 16) {
2912 os_memcpy(drv->auth_wep_key[i], params->wep_key[i],
2913 params->wep_key_len[i]);
2914 drv->auth_wep_key_len[i] = params->wep_key_len[i];
2915 } else
2916 drv->auth_wep_key_len[i] = 0;
2917 }
2918}
2919
2920
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002921static void nl80211_unmask_11b_rates(struct i802_bss *bss)
2922{
2923 struct wpa_driver_nl80211_data *drv = bss->drv;
2924
2925 if (is_p2p_net_interface(drv->nlmode) || !drv->disabled_11b_rates)
2926 return;
2927
2928 /*
2929 * Looks like we failed to unmask 11b rates previously. This could
2930 * happen, e.g., if the interface was down at the point in time when a
2931 * P2P group was terminated.
2932 */
2933 wpa_printf(MSG_DEBUG,
2934 "nl80211: Interface %s mode is for non-P2P, but 11b rates were disabled - re-enable them",
2935 bss->ifname);
2936 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
2937}
2938
2939
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002940static int wpa_driver_nl80211_authenticate(
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002941 struct i802_bss *bss, struct wpa_driver_auth_params *params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002942{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002943 struct wpa_driver_nl80211_data *drv = bss->drv;
2944 int ret = -1, i;
2945 struct nl_msg *msg;
2946 enum nl80211_auth_type type;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002947 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002948 int count = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002949 int is_retry;
2950
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002951 nl80211_unmask_11b_rates(bss);
2952
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002953 is_retry = drv->retry_auth;
2954 drv->retry_auth = 0;
Dmitry Shmidt98660862014-03-11 17:26:21 -07002955 drv->ignore_deauth_event = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002956
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07002957 nl80211_mark_disconnected(drv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002958 os_memset(drv->auth_bssid, 0, ETH_ALEN);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07002959 if (params->bssid)
2960 os_memcpy(drv->auth_attempt_bssid, params->bssid, ETH_ALEN);
2961 else
2962 os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002963 /* FIX: IBSS mode */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002964 nlmode = params->p2p ?
2965 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
2966 if (drv->nlmode != nlmode &&
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002967 wpa_driver_nl80211_set_mode(bss, nlmode) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002968 return -1;
2969
2970retry:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002971 wpa_printf(MSG_DEBUG, "nl80211: Authenticate (ifindex=%d)",
2972 drv->ifindex);
2973
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002974 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_AUTHENTICATE);
2975 if (!msg)
2976 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002977
2978 for (i = 0; i < 4; i++) {
2979 if (!params->wep_key[i])
2980 continue;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002981 wpa_driver_nl80211_set_key(bss->ifname, bss, WPA_ALG_WEP,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002982 NULL, i,
2983 i == params->wep_tx_keyidx, NULL, 0,
2984 params->wep_key[i],
2985 params->wep_key_len[i]);
2986 if (params->wep_tx_keyidx != i)
2987 continue;
2988 if (nl_add_key(msg, WPA_ALG_WEP, i, 1, NULL, 0,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002989 params->wep_key[i], params->wep_key_len[i]))
2990 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002991 }
2992
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002993 if (params->bssid) {
2994 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
2995 MAC2STR(params->bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002996 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid))
2997 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002998 }
2999 if (params->freq) {
3000 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003001 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq))
3002 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003003 }
3004 if (params->ssid) {
3005 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
3006 params->ssid, params->ssid_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003007 if (nla_put(msg, NL80211_ATTR_SSID, params->ssid_len,
3008 params->ssid))
3009 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003010 }
3011 wpa_hexdump(MSG_DEBUG, " * IEs", params->ie, params->ie_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003012 if (params->ie &&
3013 nla_put(msg, NL80211_ATTR_IE, params->ie_len, params->ie))
3014 goto fail;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003015 if (params->sae_data) {
3016 wpa_hexdump(MSG_DEBUG, " * SAE data", params->sae_data,
3017 params->sae_data_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003018 if (nla_put(msg, NL80211_ATTR_SAE_DATA, params->sae_data_len,
3019 params->sae_data))
3020 goto fail;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003021 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003022 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
3023 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
3024 else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
3025 type = NL80211_AUTHTYPE_SHARED_KEY;
3026 else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
3027 type = NL80211_AUTHTYPE_NETWORK_EAP;
3028 else if (params->auth_alg & WPA_AUTH_ALG_FT)
3029 type = NL80211_AUTHTYPE_FT;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003030 else if (params->auth_alg & WPA_AUTH_ALG_SAE)
3031 type = NL80211_AUTHTYPE_SAE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003032 else
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003033 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003034 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003035 if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE, type))
3036 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003037 if (params->local_state_change) {
3038 wpa_printf(MSG_DEBUG, " * Local state change only");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003039 if (nla_put_flag(msg, NL80211_ATTR_LOCAL_STATE_CHANGE))
3040 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003041 }
3042
3043 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3044 msg = NULL;
3045 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003046 wpa_dbg(drv->ctx, MSG_DEBUG,
3047 "nl80211: MLME command failed (auth): ret=%d (%s)",
3048 ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003049 count++;
3050 if (ret == -EALREADY && count == 1 && params->bssid &&
3051 !params->local_state_change) {
3052 /*
3053 * mac80211 does not currently accept new
3054 * authentication if we are already authenticated. As a
3055 * workaround, force deauthentication and try again.
3056 */
3057 wpa_printf(MSG_DEBUG, "nl80211: Retry authentication "
3058 "after forced deauthentication");
Dmitry Shmidt98660862014-03-11 17:26:21 -07003059 drv->ignore_deauth_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003060 wpa_driver_nl80211_deauthenticate(
3061 bss, params->bssid,
3062 WLAN_REASON_PREV_AUTH_NOT_VALID);
3063 nlmsg_free(msg);
3064 goto retry;
3065 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003066
3067 if (ret == -ENOENT && params->freq && !is_retry) {
3068 /*
3069 * cfg80211 has likely expired the BSS entry even
3070 * though it was previously available in our internal
3071 * BSS table. To recover quickly, start a single
3072 * channel scan on the specified channel.
3073 */
3074 struct wpa_driver_scan_params scan;
3075 int freqs[2];
3076
3077 os_memset(&scan, 0, sizeof(scan));
3078 scan.num_ssids = 1;
3079 if (params->ssid) {
3080 scan.ssids[0].ssid = params->ssid;
3081 scan.ssids[0].ssid_len = params->ssid_len;
3082 }
3083 freqs[0] = params->freq;
3084 freqs[1] = 0;
3085 scan.freqs = freqs;
3086 wpa_printf(MSG_DEBUG, "nl80211: Trigger single "
3087 "channel scan to refresh cfg80211 BSS "
3088 "entry");
3089 ret = wpa_driver_nl80211_scan(bss, &scan);
3090 if (ret == 0) {
3091 nl80211_copy_auth_params(drv, params);
3092 drv->scan_for_auth = 1;
3093 }
3094 } else if (is_retry) {
3095 /*
3096 * Need to indicate this with an event since the return
3097 * value from the retry is not delivered to core code.
3098 */
3099 union wpa_event_data event;
3100 wpa_printf(MSG_DEBUG, "nl80211: Authentication retry "
3101 "failed");
3102 os_memset(&event, 0, sizeof(event));
3103 os_memcpy(event.timeout_event.addr, drv->auth_bssid_,
3104 ETH_ALEN);
3105 wpa_supplicant_event(drv->ctx, EVENT_AUTH_TIMED_OUT,
3106 &event);
3107 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003108 } else {
3109 wpa_printf(MSG_DEBUG,
3110 "nl80211: Authentication request send successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003111 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003112
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003113fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003114 nlmsg_free(msg);
3115 return ret;
3116}
3117
3118
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003119int wpa_driver_nl80211_authenticate_retry(struct wpa_driver_nl80211_data *drv)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003120{
3121 struct wpa_driver_auth_params params;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003122 struct i802_bss *bss = drv->first_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003123 int i;
3124
3125 wpa_printf(MSG_DEBUG, "nl80211: Try to authenticate again");
3126
3127 os_memset(&params, 0, sizeof(params));
3128 params.freq = drv->auth_freq;
3129 params.auth_alg = drv->auth_alg;
3130 params.wep_tx_keyidx = drv->auth_wep_tx_keyidx;
3131 params.local_state_change = drv->auth_local_state_change;
3132 params.p2p = drv->auth_p2p;
3133
3134 if (!is_zero_ether_addr(drv->auth_bssid_))
3135 params.bssid = drv->auth_bssid_;
3136
3137 if (drv->auth_ssid_len) {
3138 params.ssid = drv->auth_ssid;
3139 params.ssid_len = drv->auth_ssid_len;
3140 }
3141
3142 params.ie = drv->auth_ie;
3143 params.ie_len = drv->auth_ie_len;
3144
3145 for (i = 0; i < 4; i++) {
3146 if (drv->auth_wep_key_len[i]) {
3147 params.wep_key[i] = drv->auth_wep_key[i];
3148 params.wep_key_len[i] = drv->auth_wep_key_len[i];
3149 }
3150 }
3151
3152 drv->retry_auth = 1;
3153 return wpa_driver_nl80211_authenticate(bss, &params);
3154}
3155
3156
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003157static int wpa_driver_nl80211_send_frame(struct i802_bss *bss,
3158 const void *data, size_t len,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003159 int encrypt, int noack,
3160 unsigned int freq, int no_cck,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003161 int offchanok, unsigned int wait_time,
3162 const u16 *csa_offs,
3163 size_t csa_offs_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003164{
3165 struct wpa_driver_nl80211_data *drv = bss->drv;
3166 u64 cookie;
Dmitry Shmidt051af732013-10-22 13:52:46 -07003167 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003168
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07003169 if (freq == 0 && drv->nlmode == NL80211_IFTYPE_ADHOC) {
3170 freq = nl80211_get_assoc_freq(drv);
3171 wpa_printf(MSG_DEBUG,
3172 "nl80211: send_frame - Use assoc_freq=%u for IBSS",
3173 freq);
3174 }
Dmitry Shmidt56052862013-10-04 10:23:25 -07003175 if (freq == 0) {
3176 wpa_printf(MSG_DEBUG, "nl80211: send_frame - Use bss->freq=%u",
3177 bss->freq);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003178 freq = bss->freq;
Dmitry Shmidt56052862013-10-04 10:23:25 -07003179 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003180
Dmitry Shmidt56052862013-10-04 10:23:25 -07003181 if (drv->use_monitor) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003182 wpa_printf(MSG_DEBUG, "nl80211: send_frame(freq=%u bss->freq=%u) -> send_monitor",
Dmitry Shmidt56052862013-10-04 10:23:25 -07003183 freq, bss->freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003184 return nl80211_send_monitor(drv, data, len, encrypt, noack);
Dmitry Shmidt56052862013-10-04 10:23:25 -07003185 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003186
Dmitry Shmidt56052862013-10-04 10:23:25 -07003187 wpa_printf(MSG_DEBUG, "nl80211: send_frame -> send_frame_cmd");
Dmitry Shmidt051af732013-10-22 13:52:46 -07003188 res = nl80211_send_frame_cmd(bss, freq, wait_time, data, len,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003189 &cookie, no_cck, noack, offchanok,
3190 csa_offs, csa_offs_len);
Dmitry Shmidt051af732013-10-22 13:52:46 -07003191 if (res == 0 && !noack) {
3192 const struct ieee80211_mgmt *mgmt;
3193 u16 fc;
3194
3195 mgmt = (const struct ieee80211_mgmt *) data;
3196 fc = le_to_host16(mgmt->frame_control);
3197 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
3198 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_ACTION) {
3199 wpa_printf(MSG_MSGDUMP,
3200 "nl80211: Update send_action_cookie from 0x%llx to 0x%llx",
3201 (long long unsigned int)
3202 drv->send_action_cookie,
3203 (long long unsigned int) cookie);
3204 drv->send_action_cookie = cookie;
3205 }
3206 }
3207
3208 return res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003209}
3210
3211
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003212static int wpa_driver_nl80211_send_mlme(struct i802_bss *bss, const u8 *data,
3213 size_t data_len, int noack,
3214 unsigned int freq, int no_cck,
3215 int offchanok,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003216 unsigned int wait_time,
3217 const u16 *csa_offs,
3218 size_t csa_offs_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003219{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003220 struct wpa_driver_nl80211_data *drv = bss->drv;
3221 struct ieee80211_mgmt *mgmt;
3222 int encrypt = 1;
3223 u16 fc;
3224
3225 mgmt = (struct ieee80211_mgmt *) data;
3226 fc = le_to_host16(mgmt->frame_control);
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07003227 wpa_printf(MSG_DEBUG, "nl80211: send_mlme - da= " MACSTR
3228 " noack=%d freq=%u no_cck=%d offchanok=%d wait_time=%u fc=0x%x (%s) nlmode=%d",
3229 MAC2STR(mgmt->da), noack, freq, no_cck, offchanok, wait_time,
3230 fc, fc2str(fc), drv->nlmode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003231
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003232 if ((is_sta_interface(drv->nlmode) ||
3233 drv->nlmode == NL80211_IFTYPE_P2P_DEVICE) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003234 WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
3235 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_RESP) {
3236 /*
3237 * The use of last_mgmt_freq is a bit of a hack,
3238 * but it works due to the single-threaded nature
3239 * of wpa_supplicant.
3240 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07003241 if (freq == 0) {
3242 wpa_printf(MSG_DEBUG, "nl80211: Use last_mgmt_freq=%d",
3243 drv->last_mgmt_freq);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003244 freq = drv->last_mgmt_freq;
Dmitry Shmidt56052862013-10-04 10:23:25 -07003245 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003246 return nl80211_send_frame_cmd(bss, freq, 0,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003247 data, data_len, NULL, 1, noack,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003248 1, csa_offs, csa_offs_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003249 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003250
3251 if (drv->device_ap_sme && is_ap_interface(drv->nlmode)) {
Dmitry Shmidt56052862013-10-04 10:23:25 -07003252 if (freq == 0) {
3253 wpa_printf(MSG_DEBUG, "nl80211: Use bss->freq=%d",
3254 bss->freq);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003255 freq = bss->freq;
Dmitry Shmidt56052862013-10-04 10:23:25 -07003256 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003257 return nl80211_send_frame_cmd(bss, freq,
3258 (int) freq == bss->freq ? 0 :
3259 wait_time,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003260 data, data_len,
3261 &drv->send_action_cookie,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003262 no_cck, noack, offchanok,
3263 csa_offs, csa_offs_len);
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07003264 }
Dmitry Shmidtb638fe72012-03-20 12:51:25 -07003265
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003266 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
3267 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) {
3268 /*
3269 * Only one of the authentication frame types is encrypted.
3270 * In order for static WEP encryption to work properly (i.e.,
3271 * to not encrypt the frame), we need to tell mac80211 about
3272 * the frames that must not be encrypted.
3273 */
3274 u16 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
3275 u16 auth_trans = le_to_host16(mgmt->u.auth.auth_transaction);
3276 if (auth_alg != WLAN_AUTH_SHARED_KEY || auth_trans != 3)
3277 encrypt = 0;
3278 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003279
Dmitry Shmidt56052862013-10-04 10:23:25 -07003280 wpa_printf(MSG_DEBUG, "nl80211: send_mlme -> send_frame");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003281 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003282 noack, freq, no_cck, offchanok,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003283 wait_time, csa_offs,
3284 csa_offs_len);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003285}
3286
3287
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003288static int nl80211_put_basic_rates(struct nl_msg *msg, const int *basic_rates)
3289{
3290 u8 rates[NL80211_MAX_SUPP_RATES];
3291 u8 rates_len = 0;
3292 int i;
3293
3294 if (!basic_rates)
3295 return 0;
3296
3297 for (i = 0; i < NL80211_MAX_SUPP_RATES && basic_rates[i] >= 0; i++)
3298 rates[rates_len++] = basic_rates[i] / 5;
3299
3300 return nla_put(msg, NL80211_ATTR_BSS_BASIC_RATES, rates_len, rates);
3301}
3302
3303
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003304static int nl80211_set_bss(struct i802_bss *bss, int cts, int preamble,
3305 int slot, int ht_opmode, int ap_isolate,
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003306 const int *basic_rates)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003307{
3308 struct wpa_driver_nl80211_data *drv = bss->drv;
3309 struct nl_msg *msg;
3310
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003311 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_BSS)) ||
3312 (cts >= 0 &&
3313 nla_put_u8(msg, NL80211_ATTR_BSS_CTS_PROT, cts)) ||
3314 (preamble >= 0 &&
3315 nla_put_u8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble)) ||
3316 (slot >= 0 &&
3317 nla_put_u8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot)) ||
3318 (ht_opmode >= 0 &&
3319 nla_put_u16(msg, NL80211_ATTR_BSS_HT_OPMODE, ht_opmode)) ||
3320 (ap_isolate >= 0 &&
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003321 nla_put_u8(msg, NL80211_ATTR_AP_ISOLATE, ap_isolate)) ||
3322 nl80211_put_basic_rates(msg, basic_rates)) {
3323 nlmsg_free(msg);
3324 return -ENOBUFS;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003325 }
3326
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003327 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003328}
3329
3330
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003331static int wpa_driver_nl80211_set_acl(void *priv,
3332 struct hostapd_acl_params *params)
3333{
3334 struct i802_bss *bss = priv;
3335 struct wpa_driver_nl80211_data *drv = bss->drv;
3336 struct nl_msg *msg;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003337 struct nl_msg *acl;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003338 unsigned int i;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003339 int ret;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003340
3341 if (!(drv->capa.max_acl_mac_addrs))
3342 return -ENOTSUP;
3343
3344 if (params->num_mac_acl > drv->capa.max_acl_mac_addrs)
3345 return -ENOTSUP;
3346
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003347 wpa_printf(MSG_DEBUG, "nl80211: Set %s ACL (num_mac_acl=%u)",
3348 params->acl_policy ? "Accept" : "Deny", params->num_mac_acl);
3349
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003350 acl = nlmsg_alloc();
3351 if (!acl)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003352 return -ENOMEM;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003353 for (i = 0; i < params->num_mac_acl; i++) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003354 if (nla_put(acl, i + 1, ETH_ALEN, params->mac_acl[i].addr)) {
3355 nlmsg_free(acl);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003356 return -ENOMEM;
3357 }
3358 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003359
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003360 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_MAC_ACL)) ||
3361 nla_put_u32(msg, NL80211_ATTR_ACL_POLICY, params->acl_policy ?
3362 NL80211_ACL_POLICY_DENY_UNLESS_LISTED :
3363 NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED) ||
3364 nla_put_nested(msg, NL80211_ATTR_MAC_ADDRS, acl)) {
3365 nlmsg_free(msg);
3366 nlmsg_free(acl);
3367 return -ENOMEM;
3368 }
3369 nlmsg_free(acl);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003370
3371 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003372 if (ret) {
3373 wpa_printf(MSG_DEBUG, "nl80211: Failed to set MAC ACL: %d (%s)",
3374 ret, strerror(-ret));
3375 }
3376
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003377 return ret;
3378}
3379
3380
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003381static int nl80211_put_beacon_int(struct nl_msg *msg, int beacon_int)
3382{
3383 if (beacon_int > 0) {
3384 wpa_printf(MSG_DEBUG, " * beacon_int=%d", beacon_int);
3385 return nla_put_u32(msg, NL80211_ATTR_BEACON_INTERVAL,
3386 beacon_int);
3387 }
3388
3389 return 0;
3390}
3391
3392
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003393static int wpa_driver_nl80211_set_ap(void *priv,
3394 struct wpa_driver_ap_params *params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003395{
3396 struct i802_bss *bss = priv;
3397 struct wpa_driver_nl80211_data *drv = bss->drv;
3398 struct nl_msg *msg;
3399 u8 cmd = NL80211_CMD_NEW_BEACON;
3400 int ret;
3401 int beacon_set;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003402 int num_suites;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003403 int smps_mode;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003404 u32 suites[10], suite;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003405 u32 ver;
3406
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003407 beacon_set = params->reenable ? 0 : bss->beacon_set;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003408
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003409 wpa_printf(MSG_DEBUG, "nl80211: Set beacon (beacon_set=%d)",
3410 beacon_set);
3411 if (beacon_set)
3412 cmd = NL80211_CMD_SET_BEACON;
3413
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003414 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon head",
3415 params->head, params->head_len);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003416 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon tail",
3417 params->tail, params->tail_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003418 wpa_printf(MSG_DEBUG, "nl80211: ifindex=%d", bss->ifindex);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003419 wpa_printf(MSG_DEBUG, "nl80211: beacon_int=%d", params->beacon_int);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003420 wpa_printf(MSG_DEBUG, "nl80211: dtim_period=%d", params->dtim_period);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003421 wpa_hexdump_ascii(MSG_DEBUG, "nl80211: ssid",
3422 params->ssid, params->ssid_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003423 if (!(msg = nl80211_bss_msg(bss, 0, cmd)) ||
3424 nla_put(msg, NL80211_ATTR_BEACON_HEAD, params->head_len,
3425 params->head) ||
3426 nla_put(msg, NL80211_ATTR_BEACON_TAIL, params->tail_len,
3427 params->tail) ||
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003428 nl80211_put_beacon_int(msg, params->beacon_int) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003429 nla_put_u32(msg, NL80211_ATTR_DTIM_PERIOD, params->dtim_period) ||
3430 nla_put(msg, NL80211_ATTR_SSID, params->ssid_len, params->ssid))
3431 goto fail;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003432 if (params->proberesp && params->proberesp_len) {
3433 wpa_hexdump(MSG_DEBUG, "nl80211: proberesp (offload)",
3434 params->proberesp, params->proberesp_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003435 if (nla_put(msg, NL80211_ATTR_PROBE_RESP, params->proberesp_len,
3436 params->proberesp))
3437 goto fail;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003438 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003439 switch (params->hide_ssid) {
3440 case NO_SSID_HIDING:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003441 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID not in use");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003442 if (nla_put_u32(msg, NL80211_ATTR_HIDDEN_SSID,
3443 NL80211_HIDDEN_SSID_NOT_IN_USE))
3444 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003445 break;
3446 case HIDDEN_SSID_ZERO_LEN:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003447 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero len");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003448 if (nla_put_u32(msg, NL80211_ATTR_HIDDEN_SSID,
3449 NL80211_HIDDEN_SSID_ZERO_LEN))
3450 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003451 break;
3452 case HIDDEN_SSID_ZERO_CONTENTS:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003453 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero contents");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003454 if (nla_put_u32(msg, NL80211_ATTR_HIDDEN_SSID,
3455 NL80211_HIDDEN_SSID_ZERO_CONTENTS))
3456 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003457 break;
3458 }
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003459 wpa_printf(MSG_DEBUG, "nl80211: privacy=%d", params->privacy);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003460 if (params->privacy &&
3461 nla_put_flag(msg, NL80211_ATTR_PRIVACY))
3462 goto fail;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003463 wpa_printf(MSG_DEBUG, "nl80211: auth_algs=0x%x", params->auth_algs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003464 if ((params->auth_algs & (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) ==
3465 (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) {
3466 /* Leave out the attribute */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003467 } else if (params->auth_algs & WPA_AUTH_ALG_SHARED) {
3468 if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE,
3469 NL80211_AUTHTYPE_SHARED_KEY))
3470 goto fail;
3471 } else {
3472 if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE,
3473 NL80211_AUTHTYPE_OPEN_SYSTEM))
3474 goto fail;
3475 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003476
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003477 wpa_printf(MSG_DEBUG, "nl80211: wpa_version=0x%x", params->wpa_version);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003478 ver = 0;
3479 if (params->wpa_version & WPA_PROTO_WPA)
3480 ver |= NL80211_WPA_VERSION_1;
3481 if (params->wpa_version & WPA_PROTO_RSN)
3482 ver |= NL80211_WPA_VERSION_2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003483 if (ver &&
3484 nla_put_u32(msg, NL80211_ATTR_WPA_VERSIONS, ver))
3485 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003486
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003487 wpa_printf(MSG_DEBUG, "nl80211: key_mgmt_suites=0x%x",
3488 params->key_mgmt_suites);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003489 num_suites = 0;
3490 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X)
3491 suites[num_suites++] = WLAN_AKM_SUITE_8021X;
3492 if (params->key_mgmt_suites & WPA_KEY_MGMT_PSK)
3493 suites[num_suites++] = WLAN_AKM_SUITE_PSK;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003494 if (num_suites &&
3495 nla_put(msg, NL80211_ATTR_AKM_SUITES, num_suites * sizeof(u32),
3496 suites))
3497 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003498
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003499 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
3500 params->pairwise_ciphers & (WPA_CIPHER_WEP104 | WPA_CIPHER_WEP40) &&
3501 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT))
3502 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003503
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003504 wpa_printf(MSG_DEBUG, "nl80211: pairwise_ciphers=0x%x",
3505 params->pairwise_ciphers);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003506 num_suites = wpa_cipher_to_cipher_suites(params->pairwise_ciphers,
3507 suites, ARRAY_SIZE(suites));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003508 if (num_suites &&
3509 nla_put(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
3510 num_suites * sizeof(u32), suites))
3511 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003512
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003513 wpa_printf(MSG_DEBUG, "nl80211: group_cipher=0x%x",
3514 params->group_cipher);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003515 suite = wpa_cipher_to_cipher_suite(params->group_cipher);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003516 if (suite &&
3517 nla_put_u32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, suite))
3518 goto fail;
3519
3520 switch (params->smps_mode) {
3521 case HT_CAP_INFO_SMPS_DYNAMIC:
3522 wpa_printf(MSG_DEBUG, "nl80211: SMPS mode - dynamic");
3523 smps_mode = NL80211_SMPS_DYNAMIC;
3524 break;
3525 case HT_CAP_INFO_SMPS_STATIC:
3526 wpa_printf(MSG_DEBUG, "nl80211: SMPS mode - static");
3527 smps_mode = NL80211_SMPS_STATIC;
3528 break;
3529 default:
3530 /* invalid - fallback to smps off */
3531 case HT_CAP_INFO_SMPS_DISABLED:
3532 wpa_printf(MSG_DEBUG, "nl80211: SMPS mode - off");
3533 smps_mode = NL80211_SMPS_OFF;
3534 break;
3535 }
3536 if (nla_put_u32(msg, NL80211_ATTR_SMPS_MODE, smps_mode))
3537 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003538
3539 if (params->beacon_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003540 wpa_hexdump_buf(MSG_DEBUG, "nl80211: beacon_ies",
3541 params->beacon_ies);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003542 if (nla_put(msg, NL80211_ATTR_IE,
3543 wpabuf_len(params->beacon_ies),
3544 wpabuf_head(params->beacon_ies)))
3545 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003546 }
3547 if (params->proberesp_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003548 wpa_hexdump_buf(MSG_DEBUG, "nl80211: proberesp_ies",
3549 params->proberesp_ies);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003550 if (nla_put(msg, NL80211_ATTR_IE_PROBE_RESP,
3551 wpabuf_len(params->proberesp_ies),
3552 wpabuf_head(params->proberesp_ies)))
3553 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003554 }
3555 if (params->assocresp_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003556 wpa_hexdump_buf(MSG_DEBUG, "nl80211: assocresp_ies",
3557 params->assocresp_ies);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003558 if (nla_put(msg, NL80211_ATTR_IE_ASSOC_RESP,
3559 wpabuf_len(params->assocresp_ies),
3560 wpabuf_head(params->assocresp_ies)))
3561 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003562 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003563
Dmitry Shmidt04949592012-07-19 12:16:46 -07003564 if (drv->capa.flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003565 wpa_printf(MSG_DEBUG, "nl80211: ap_max_inactivity=%d",
3566 params->ap_max_inactivity);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003567 if (nla_put_u16(msg, NL80211_ATTR_INACTIVITY_TIMEOUT,
3568 params->ap_max_inactivity))
3569 goto fail;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003570 }
3571
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003572#ifdef CONFIG_P2P
3573 if (params->p2p_go_ctwindow > 0) {
3574 if (drv->p2p_go_ctwindow_supported) {
3575 wpa_printf(MSG_DEBUG, "nl80211: P2P GO ctwindow=%d",
3576 params->p2p_go_ctwindow);
3577 if (nla_put_u8(msg, NL80211_ATTR_P2P_CTWINDOW,
3578 params->p2p_go_ctwindow))
3579 goto fail;
3580 } else {
3581 wpa_printf(MSG_INFO,
3582 "nl80211: Driver does not support CTWindow configuration - ignore this parameter");
3583 }
3584 }
3585#endif /* CONFIG_P2P */
3586
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003587 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3588 if (ret) {
3589 wpa_printf(MSG_DEBUG, "nl80211: Beacon set failed: %d (%s)",
3590 ret, strerror(-ret));
3591 } else {
3592 bss->beacon_set = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003593 nl80211_set_bss(bss, params->cts_protect, params->preamble,
3594 params->short_slot_time, params->ht_opmode,
3595 params->isolate, params->basic_rates);
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07003596 if (beacon_set && params->freq &&
3597 params->freq->bandwidth != bss->bandwidth) {
3598 wpa_printf(MSG_DEBUG,
3599 "nl80211: Update BSS %s bandwidth: %d -> %d",
3600 bss->ifname, bss->bandwidth,
3601 params->freq->bandwidth);
3602 ret = nl80211_set_channel(bss, params->freq, 1);
3603 if (ret) {
3604 wpa_printf(MSG_DEBUG,
3605 "nl80211: Frequency set failed: %d (%s)",
3606 ret, strerror(-ret));
3607 } else {
3608 wpa_printf(MSG_DEBUG,
3609 "nl80211: Frequency set succeeded for ht2040 coex");
3610 bss->bandwidth = params->freq->bandwidth;
3611 }
3612 } else if (!beacon_set) {
3613 /*
3614 * cfg80211 updates the driver on frequence change in AP
3615 * mode only at the point when beaconing is started, so
3616 * set the initial value here.
3617 */
3618 bss->bandwidth = params->freq->bandwidth;
3619 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003620 }
3621 return ret;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003622fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003623 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003624 return -ENOBUFS;
3625}
3626
3627
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003628static int nl80211_put_freq_params(struct nl_msg *msg,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003629 const struct hostapd_freq_params *freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003630{
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003631 wpa_printf(MSG_DEBUG, " * freq=%d", freq->freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003632 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq->freq))
3633 return -ENOBUFS;
3634
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003635 wpa_printf(MSG_DEBUG, " * vht_enabled=%d", freq->vht_enabled);
3636 wpa_printf(MSG_DEBUG, " * ht_enabled=%d", freq->ht_enabled);
3637
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003638 if (freq->vht_enabled) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003639 enum nl80211_chan_width cw;
3640
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003641 wpa_printf(MSG_DEBUG, " * bandwidth=%d", freq->bandwidth);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003642 switch (freq->bandwidth) {
3643 case 20:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003644 cw = NL80211_CHAN_WIDTH_20;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003645 break;
3646 case 40:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003647 cw = NL80211_CHAN_WIDTH_40;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003648 break;
3649 case 80:
3650 if (freq->center_freq2)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003651 cw = NL80211_CHAN_WIDTH_80P80;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003652 else
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003653 cw = NL80211_CHAN_WIDTH_80;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003654 break;
3655 case 160:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003656 cw = NL80211_CHAN_WIDTH_160;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003657 break;
3658 default:
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003659 return -EINVAL;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003660 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003661
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003662 wpa_printf(MSG_DEBUG, " * channel_width=%d", cw);
3663 wpa_printf(MSG_DEBUG, " * center_freq1=%d",
3664 freq->center_freq1);
3665 wpa_printf(MSG_DEBUG, " * center_freq2=%d",
3666 freq->center_freq2);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003667 if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, cw) ||
3668 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1,
3669 freq->center_freq1) ||
3670 (freq->center_freq2 &&
3671 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2,
3672 freq->center_freq2)))
3673 return -ENOBUFS;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003674 } else if (freq->ht_enabled) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003675 enum nl80211_channel_type ct;
3676
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003677 wpa_printf(MSG_DEBUG, " * sec_channel_offset=%d",
3678 freq->sec_channel_offset);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003679 switch (freq->sec_channel_offset) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003680 case -1:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003681 ct = NL80211_CHAN_HT40MINUS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003682 break;
3683 case 1:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003684 ct = NL80211_CHAN_HT40PLUS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003685 break;
3686 default:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003687 ct = NL80211_CHAN_HT20;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003688 break;
3689 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003690
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003691 wpa_printf(MSG_DEBUG, " * channel_type=%d", ct);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003692 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, ct))
3693 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003694 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003695 return 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003696}
3697
3698
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07003699static int nl80211_set_channel(struct i802_bss *bss,
3700 struct hostapd_freq_params *freq, int set_chan)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003701{
3702 struct wpa_driver_nl80211_data *drv = bss->drv;
3703 struct nl_msg *msg;
3704 int ret;
3705
3706 wpa_printf(MSG_DEBUG,
3707 "nl80211: Set freq %d (ht_enabled=%d, vht_enabled=%d, bandwidth=%d MHz, cf1=%d MHz, cf2=%d MHz)",
3708 freq->freq, freq->ht_enabled, freq->vht_enabled,
3709 freq->bandwidth, freq->center_freq1, freq->center_freq2);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003710
3711 msg = nl80211_drv_msg(drv, 0, set_chan ? NL80211_CMD_SET_CHANNEL :
3712 NL80211_CMD_SET_WIPHY);
3713 if (!msg || nl80211_put_freq_params(msg, freq) < 0) {
3714 nlmsg_free(msg);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003715 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003716 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003717
3718 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003719 if (ret == 0) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003720 bss->freq = freq->freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003721 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003722 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003723 wpa_printf(MSG_DEBUG, "nl80211: Failed to set channel (freq=%d): "
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003724 "%d (%s)", freq->freq, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003725 return -1;
3726}
3727
3728
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003729static u32 sta_flags_nl80211(int flags)
3730{
3731 u32 f = 0;
3732
3733 if (flags & WPA_STA_AUTHORIZED)
3734 f |= BIT(NL80211_STA_FLAG_AUTHORIZED);
3735 if (flags & WPA_STA_WMM)
3736 f |= BIT(NL80211_STA_FLAG_WME);
3737 if (flags & WPA_STA_SHORT_PREAMBLE)
3738 f |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
3739 if (flags & WPA_STA_MFP)
3740 f |= BIT(NL80211_STA_FLAG_MFP);
3741 if (flags & WPA_STA_TDLS_PEER)
3742 f |= BIT(NL80211_STA_FLAG_TDLS_PEER);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003743 if (flags & WPA_STA_AUTHENTICATED)
3744 f |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003745
3746 return f;
3747}
3748
3749
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003750#ifdef CONFIG_MESH
3751static u32 sta_plink_state_nl80211(enum mesh_plink_state state)
3752{
3753 switch (state) {
3754 case PLINK_LISTEN:
3755 return NL80211_PLINK_LISTEN;
3756 case PLINK_OPEN_SENT:
3757 return NL80211_PLINK_OPN_SNT;
3758 case PLINK_OPEN_RCVD:
3759 return NL80211_PLINK_OPN_RCVD;
3760 case PLINK_CNF_RCVD:
3761 return NL80211_PLINK_CNF_RCVD;
3762 case PLINK_ESTAB:
3763 return NL80211_PLINK_ESTAB;
3764 case PLINK_HOLDING:
3765 return NL80211_PLINK_HOLDING;
3766 case PLINK_BLOCKED:
3767 return NL80211_PLINK_BLOCKED;
3768 default:
3769 wpa_printf(MSG_ERROR, "nl80211: Invalid mesh plink state %d",
3770 state);
3771 }
3772 return -1;
3773}
3774#endif /* CONFIG_MESH */
3775
3776
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003777static int wpa_driver_nl80211_sta_add(void *priv,
3778 struct hostapd_sta_add_params *params)
3779{
3780 struct i802_bss *bss = priv;
3781 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003782 struct nl_msg *msg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003783 struct nl80211_sta_flag_update upd;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003784 int ret = -ENOBUFS;
3785
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003786 if ((params->flags & WPA_STA_TDLS_PEER) &&
3787 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
3788 return -EOPNOTSUPP;
3789
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003790 wpa_printf(MSG_DEBUG, "nl80211: %s STA " MACSTR,
3791 params->set ? "Set" : "Add", MAC2STR(params->addr));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003792 msg = nl80211_bss_msg(bss, 0, params->set ? NL80211_CMD_SET_STATION :
3793 NL80211_CMD_NEW_STATION);
3794 if (!msg || nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr))
3795 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003796
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003797 if (!params->set || (params->flags & WPA_STA_TDLS_PEER)) {
3798 wpa_hexdump(MSG_DEBUG, " * supported rates",
3799 params->supp_rates, params->supp_rates_len);
3800 wpa_printf(MSG_DEBUG, " * capability=0x%x",
3801 params->capability);
3802 if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_RATES,
3803 params->supp_rates_len, params->supp_rates) ||
3804 nla_put_u16(msg, NL80211_ATTR_STA_CAPABILITY,
3805 params->capability))
3806 goto fail;
3807
3808 if (params->ht_capabilities) {
3809 wpa_hexdump(MSG_DEBUG, " * ht_capabilities",
3810 (u8 *) params->ht_capabilities,
3811 sizeof(*params->ht_capabilities));
3812 if (nla_put(msg, NL80211_ATTR_HT_CAPABILITY,
3813 sizeof(*params->ht_capabilities),
3814 params->ht_capabilities))
3815 goto fail;
3816 }
3817
3818 if (params->vht_capabilities) {
3819 wpa_hexdump(MSG_DEBUG, " * vht_capabilities",
3820 (u8 *) params->vht_capabilities,
3821 sizeof(*params->vht_capabilities));
3822 if (nla_put(msg, NL80211_ATTR_VHT_CAPABILITY,
3823 sizeof(*params->vht_capabilities),
3824 params->vht_capabilities))
3825 goto fail;
3826 }
3827
3828 if (params->ext_capab) {
3829 wpa_hexdump(MSG_DEBUG, " * ext_capab",
3830 params->ext_capab, params->ext_capab_len);
3831 if (nla_put(msg, NL80211_ATTR_STA_EXT_CAPABILITY,
3832 params->ext_capab_len, params->ext_capab))
3833 goto fail;
3834 }
3835 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003836 if (!params->set) {
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07003837 if (params->aid) {
3838 wpa_printf(MSG_DEBUG, " * aid=%u", params->aid);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003839 if (nla_put_u16(msg, NL80211_ATTR_STA_AID, params->aid))
3840 goto fail;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07003841 } else {
3842 /*
3843 * cfg80211 validates that AID is non-zero, so we have
3844 * to make this a non-zero value for the TDLS case where
3845 * a dummy STA entry is used for now.
3846 */
3847 wpa_printf(MSG_DEBUG, " * aid=1 (TDLS workaround)");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003848 if (nla_put_u16(msg, NL80211_ATTR_STA_AID, 1))
3849 goto fail;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07003850 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003851 wpa_printf(MSG_DEBUG, " * listen_interval=%u",
3852 params->listen_interval);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003853 if (nla_put_u16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
3854 params->listen_interval))
3855 goto fail;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003856 } else if (params->aid && (params->flags & WPA_STA_TDLS_PEER)) {
3857 wpa_printf(MSG_DEBUG, " * peer_aid=%u", params->aid);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003858 if (nla_put_u16(msg, NL80211_ATTR_PEER_AID, params->aid))
3859 goto fail;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003860 }
3861
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08003862 if (params->vht_opmode_enabled) {
3863 wpa_printf(MSG_DEBUG, " * opmode=%u", params->vht_opmode);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003864 if (nla_put_u8(msg, NL80211_ATTR_OPMODE_NOTIF,
3865 params->vht_opmode))
3866 goto fail;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003867 }
3868
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003869 if (params->supp_channels) {
3870 wpa_hexdump(MSG_DEBUG, " * supported channels",
3871 params->supp_channels, params->supp_channels_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003872 if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_CHANNELS,
3873 params->supp_channels_len, params->supp_channels))
3874 goto fail;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003875 }
3876
3877 if (params->supp_oper_classes) {
3878 wpa_hexdump(MSG_DEBUG, " * supported operating classes",
3879 params->supp_oper_classes,
3880 params->supp_oper_classes_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003881 if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES,
3882 params->supp_oper_classes_len,
3883 params->supp_oper_classes))
3884 goto fail;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003885 }
3886
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003887 os_memset(&upd, 0, sizeof(upd));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003888 upd.set = sta_flags_nl80211(params->flags);
3889 upd.mask = upd.set | sta_flags_nl80211(params->flags_mask);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003890 wpa_printf(MSG_DEBUG, " * flags set=0x%x mask=0x%x",
3891 upd.set, upd.mask);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003892 if (nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd))
3893 goto fail;
3894
3895#ifdef CONFIG_MESH
3896 if (params->plink_state &&
3897 nla_put_u8(msg, NL80211_ATTR_STA_PLINK_STATE,
3898 sta_plink_state_nl80211(params->plink_state)))
3899 goto fail;
3900#endif /* CONFIG_MESH */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003901
3902 if (params->flags & WPA_STA_WMM) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003903 struct nlattr *wme = nla_nest_start(msg, NL80211_ATTR_STA_WME);
3904
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003905 wpa_printf(MSG_DEBUG, " * qosinfo=0x%x", params->qosinfo);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003906 if (!wme ||
3907 nla_put_u8(msg, NL80211_STA_WME_UAPSD_QUEUES,
3908 params->qosinfo & WMM_QOSINFO_STA_AC_MASK) ||
3909 nla_put_u8(msg, NL80211_STA_WME_MAX_SP,
3910 (params->qosinfo >> WMM_QOSINFO_STA_SP_SHIFT) &
3911 WMM_QOSINFO_STA_SP_MASK))
3912 goto fail;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003913 nla_nest_end(msg, wme);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003914 }
3915
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003916 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003917 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003918 if (ret)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003919 wpa_printf(MSG_DEBUG, "nl80211: NL80211_CMD_%s_STATION "
3920 "result: %d (%s)", params->set ? "SET" : "NEW", ret,
3921 strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003922 if (ret == -EEXIST)
3923 ret = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003924fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003925 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003926 return ret;
3927}
3928
3929
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003930static void rtnl_neigh_delete_fdb_entry(struct i802_bss *bss, const u8 *addr)
3931{
3932#ifdef CONFIG_LIBNL3_ROUTE
3933 struct wpa_driver_nl80211_data *drv = bss->drv;
3934 struct rtnl_neigh *rn;
3935 struct nl_addr *nl_addr;
3936 int err;
3937
3938 rn = rtnl_neigh_alloc();
3939 if (!rn)
3940 return;
3941
3942 rtnl_neigh_set_family(rn, AF_BRIDGE);
3943 rtnl_neigh_set_ifindex(rn, bss->ifindex);
3944 nl_addr = nl_addr_build(AF_BRIDGE, (void *) addr, ETH_ALEN);
3945 if (!nl_addr) {
3946 rtnl_neigh_put(rn);
3947 return;
3948 }
3949 rtnl_neigh_set_lladdr(rn, nl_addr);
3950
3951 err = rtnl_neigh_delete(drv->rtnl_sk, rn, 0);
3952 if (err < 0) {
3953 wpa_printf(MSG_DEBUG, "nl80211: bridge FDB entry delete for "
3954 MACSTR " ifindex=%d failed: %s", MAC2STR(addr),
3955 bss->ifindex, nl_geterror(err));
3956 } else {
3957 wpa_printf(MSG_DEBUG, "nl80211: deleted bridge FDB entry for "
3958 MACSTR, MAC2STR(addr));
3959 }
3960
3961 nl_addr_put(nl_addr);
3962 rtnl_neigh_put(rn);
3963#endif /* CONFIG_LIBNL3_ROUTE */
3964}
3965
3966
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003967static int wpa_driver_nl80211_sta_remove(struct i802_bss *bss, const u8 *addr,
3968 int deauth, u16 reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003969{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003970 struct wpa_driver_nl80211_data *drv = bss->drv;
3971 struct nl_msg *msg;
3972 int ret;
3973
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003974 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_DEL_STATION)) ||
3975 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
3976 (deauth == 0 &&
3977 nla_put_u8(msg, NL80211_ATTR_MGMT_SUBTYPE,
3978 WLAN_FC_STYPE_DISASSOC)) ||
3979 (deauth == 1 &&
3980 nla_put_u8(msg, NL80211_ATTR_MGMT_SUBTYPE,
3981 WLAN_FC_STYPE_DEAUTH)) ||
3982 (reason_code &&
3983 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code))) {
3984 nlmsg_free(msg);
3985 return -ENOBUFS;
3986 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003987
3988 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07003989 wpa_printf(MSG_DEBUG, "nl80211: sta_remove -> DEL_STATION %s " MACSTR
3990 " --> %d (%s)",
3991 bss->ifname, MAC2STR(addr), ret, strerror(-ret));
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003992
3993 if (drv->rtnl_sk)
3994 rtnl_neigh_delete_fdb_entry(bss, addr);
3995
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003996 if (ret == -ENOENT)
3997 return 0;
3998 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003999}
4000
4001
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004002void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv, int ifidx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004003{
4004 struct nl_msg *msg;
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07004005 struct wpa_driver_nl80211_data *drv2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004006
4007 wpa_printf(MSG_DEBUG, "nl80211: Remove interface ifindex=%d", ifidx);
4008
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004009 /* stop listening for EAPOL on this interface */
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07004010 dl_list_for_each(drv2, &drv->global->interfaces,
4011 struct wpa_driver_nl80211_data, list)
4012 del_ifidx(drv2, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004013
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004014 msg = nl80211_ifindex_msg(drv, ifidx, 0, NL80211_CMD_DEL_INTERFACE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004015 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
4016 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004017 wpa_printf(MSG_ERROR, "Failed to remove interface (ifidx=%d)", ifidx);
4018}
4019
4020
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004021static const char * nl80211_iftype_str(enum nl80211_iftype mode)
4022{
4023 switch (mode) {
4024 case NL80211_IFTYPE_ADHOC:
4025 return "ADHOC";
4026 case NL80211_IFTYPE_STATION:
4027 return "STATION";
4028 case NL80211_IFTYPE_AP:
4029 return "AP";
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07004030 case NL80211_IFTYPE_AP_VLAN:
4031 return "AP_VLAN";
4032 case NL80211_IFTYPE_WDS:
4033 return "WDS";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004034 case NL80211_IFTYPE_MONITOR:
4035 return "MONITOR";
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07004036 case NL80211_IFTYPE_MESH_POINT:
4037 return "MESH_POINT";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004038 case NL80211_IFTYPE_P2P_CLIENT:
4039 return "P2P_CLIENT";
4040 case NL80211_IFTYPE_P2P_GO:
4041 return "P2P_GO";
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004042 case NL80211_IFTYPE_P2P_DEVICE:
4043 return "P2P_DEVICE";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004044 default:
4045 return "unknown";
4046 }
4047}
4048
4049
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004050static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv,
4051 const char *ifname,
4052 enum nl80211_iftype iftype,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004053 const u8 *addr, int wds,
4054 int (*handler)(struct nl_msg *, void *),
4055 void *arg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004056{
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004057 struct nl_msg *msg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004058 int ifidx;
4059 int ret = -ENOBUFS;
4060
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004061 wpa_printf(MSG_DEBUG, "nl80211: Create interface iftype %d (%s)",
4062 iftype, nl80211_iftype_str(iftype));
4063
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004064 msg = nl80211_cmd_msg(drv->first_bss, 0, NL80211_CMD_NEW_INTERFACE);
4065 if (!msg ||
4066 nla_put_string(msg, NL80211_ATTR_IFNAME, ifname) ||
4067 nla_put_u32(msg, NL80211_ATTR_IFTYPE, iftype))
4068 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004069
4070 if (iftype == NL80211_IFTYPE_MONITOR) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004071 struct nlattr *flags;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004072
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004073 flags = nla_nest_start(msg, NL80211_ATTR_MNTR_FLAGS);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004074 if (!flags ||
4075 nla_put_flag(msg, NL80211_MNTR_FLAG_COOK_FRAMES))
4076 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004077
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004078 nla_nest_end(msg, flags);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004079 } else if (wds) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004080 if (nla_put_u8(msg, NL80211_ATTR_4ADDR, wds))
4081 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004082 }
4083
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07004084 /*
4085 * Tell cfg80211 that the interface belongs to the socket that created
4086 * it, and the interface should be deleted when the socket is closed.
4087 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004088 if (nla_put_flag(msg, NL80211_ATTR_IFACE_SOCKET_OWNER))
4089 goto fail;
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07004090
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004091 ret = send_and_recv_msgs(drv, msg, handler, arg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004092 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004093 if (ret) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004094 fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004095 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004096 wpa_printf(MSG_ERROR, "Failed to create interface %s: %d (%s)",
4097 ifname, ret, strerror(-ret));
4098 return ret;
4099 }
4100
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004101 if (iftype == NL80211_IFTYPE_P2P_DEVICE)
4102 return 0;
4103
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004104 ifidx = if_nametoindex(ifname);
4105 wpa_printf(MSG_DEBUG, "nl80211: New interface %s created: ifindex=%d",
4106 ifname, ifidx);
4107
4108 if (ifidx <= 0)
4109 return -1;
4110
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07004111 /*
4112 * Some virtual interfaces need to process EAPOL packets and events on
4113 * the parent interface. This is used mainly with hostapd.
4114 */
4115 if (drv->hostapd ||
4116 iftype == NL80211_IFTYPE_AP_VLAN ||
4117 iftype == NL80211_IFTYPE_WDS ||
4118 iftype == NL80211_IFTYPE_MONITOR) {
4119 /* start listening for EAPOL on this interface */
4120 add_ifidx(drv, ifidx);
4121 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004122
4123 if (addr && iftype != NL80211_IFTYPE_MONITOR &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004124 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname, addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004125 nl80211_remove_iface(drv, ifidx);
4126 return -1;
4127 }
4128
4129 return ifidx;
4130}
4131
4132
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004133int nl80211_create_iface(struct wpa_driver_nl80211_data *drv,
4134 const char *ifname, enum nl80211_iftype iftype,
4135 const u8 *addr, int wds,
4136 int (*handler)(struct nl_msg *, void *),
4137 void *arg, int use_existing)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004138{
4139 int ret;
4140
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004141 ret = nl80211_create_iface_once(drv, ifname, iftype, addr, wds, handler,
4142 arg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004143
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004144 /* if error occurred and interface exists already */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004145 if (ret == -ENFILE && if_nametoindex(ifname)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004146 if (use_existing) {
4147 wpa_printf(MSG_DEBUG, "nl80211: Continue using existing interface %s",
4148 ifname);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07004149 if (addr && iftype != NL80211_IFTYPE_MONITOR &&
4150 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
4151 addr) < 0 &&
4152 (linux_set_iface_flags(drv->global->ioctl_sock,
4153 ifname, 0) < 0 ||
4154 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
4155 addr) < 0 ||
4156 linux_set_iface_flags(drv->global->ioctl_sock,
4157 ifname, 1) < 0))
4158 return -1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004159 return -ENFILE;
4160 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004161 wpa_printf(MSG_INFO, "Try to remove and re-create %s", ifname);
4162
4163 /* Try to remove the interface that was already there. */
4164 nl80211_remove_iface(drv, if_nametoindex(ifname));
4165
4166 /* Try to create the interface again */
4167 ret = nl80211_create_iface_once(drv, ifname, iftype, addr,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004168 wds, handler, arg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004169 }
4170
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004171 if (ret >= 0 && is_p2p_net_interface(iftype)) {
4172 wpa_printf(MSG_DEBUG,
4173 "nl80211: Interface %s created for P2P - disable 11b rates",
4174 ifname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004175 nl80211_disable_11b_rates(drv, ret, 1);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004176 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004177
4178 return ret;
4179}
4180
4181
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004182static int nl80211_setup_ap(struct i802_bss *bss)
4183{
4184 struct wpa_driver_nl80211_data *drv = bss->drv;
4185
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004186 wpa_printf(MSG_DEBUG, "nl80211: Setup AP(%s) - device_ap_sme=%d use_monitor=%d",
4187 bss->ifname, drv->device_ap_sme, drv->use_monitor);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004188
4189 /*
4190 * Disable Probe Request reporting unless we need it in this way for
4191 * devices that include the AP SME, in the other case (unless using
4192 * monitor iface) we'll get it through the nl_mgmt socket instead.
4193 */
4194 if (!drv->device_ap_sme)
4195 wpa_driver_nl80211_probe_req_report(bss, 0);
4196
4197 if (!drv->device_ap_sme && !drv->use_monitor)
4198 if (nl80211_mgmt_subscribe_ap(bss))
4199 return -1;
4200
4201 if (drv->device_ap_sme && !drv->use_monitor)
4202 if (nl80211_mgmt_subscribe_ap_dev_sme(bss))
4203 return -1;
4204
4205 if (!drv->device_ap_sme && drv->use_monitor &&
4206 nl80211_create_monitor_interface(drv) &&
4207 !drv->device_ap_sme)
Dmitry Shmidt04949592012-07-19 12:16:46 -07004208 return -1;
4209
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004210 if (drv->device_ap_sme &&
4211 wpa_driver_nl80211_probe_req_report(bss, 1) < 0) {
4212 wpa_printf(MSG_DEBUG, "nl80211: Failed to enable "
4213 "Probe Request frame reporting in AP mode");
4214 /* Try to survive without this */
4215 }
4216
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004217 return 0;
4218}
4219
4220
4221static void nl80211_teardown_ap(struct i802_bss *bss)
4222{
4223 struct wpa_driver_nl80211_data *drv = bss->drv;
4224
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004225 wpa_printf(MSG_DEBUG, "nl80211: Teardown AP(%s) - device_ap_sme=%d use_monitor=%d",
4226 bss->ifname, drv->device_ap_sme, drv->use_monitor);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004227 if (drv->device_ap_sme) {
4228 wpa_driver_nl80211_probe_req_report(bss, 0);
4229 if (!drv->use_monitor)
4230 nl80211_mgmt_unsubscribe(bss, "AP teardown (dev SME)");
4231 } else if (drv->use_monitor)
4232 nl80211_remove_monitor_interface(drv);
4233 else
4234 nl80211_mgmt_unsubscribe(bss, "AP teardown");
4235
4236 bss->beacon_set = 0;
4237}
4238
4239
4240static int nl80211_send_eapol_data(struct i802_bss *bss,
4241 const u8 *addr, const u8 *data,
4242 size_t data_len)
4243{
4244 struct sockaddr_ll ll;
4245 int ret;
4246
4247 if (bss->drv->eapol_tx_sock < 0) {
4248 wpa_printf(MSG_DEBUG, "nl80211: No socket to send EAPOL");
4249 return -1;
4250 }
4251
4252 os_memset(&ll, 0, sizeof(ll));
4253 ll.sll_family = AF_PACKET;
4254 ll.sll_ifindex = bss->ifindex;
4255 ll.sll_protocol = htons(ETH_P_PAE);
4256 ll.sll_halen = ETH_ALEN;
4257 os_memcpy(ll.sll_addr, addr, ETH_ALEN);
4258 ret = sendto(bss->drv->eapol_tx_sock, data, data_len, 0,
4259 (struct sockaddr *) &ll, sizeof(ll));
4260 if (ret < 0)
4261 wpa_printf(MSG_ERROR, "nl80211: EAPOL TX: %s",
4262 strerror(errno));
4263
4264 return ret;
4265}
4266
4267
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004268static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
4269
4270static int wpa_driver_nl80211_hapd_send_eapol(
4271 void *priv, const u8 *addr, const u8 *data,
4272 size_t data_len, int encrypt, const u8 *own_addr, u32 flags)
4273{
4274 struct i802_bss *bss = priv;
4275 struct wpa_driver_nl80211_data *drv = bss->drv;
4276 struct ieee80211_hdr *hdr;
4277 size_t len;
4278 u8 *pos;
4279 int res;
4280 int qos = flags & WPA_STA_WMM;
Dmitry Shmidt641185e2013-11-06 15:17:13 -08004281
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004282 if (drv->device_ap_sme || !drv->use_monitor)
4283 return nl80211_send_eapol_data(bss, addr, data, data_len);
4284
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004285 len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
4286 data_len;
4287 hdr = os_zalloc(len);
4288 if (hdr == NULL) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004289 wpa_printf(MSG_INFO, "nl80211: Failed to allocate EAPOL buffer(len=%lu)",
4290 (unsigned long) len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004291 return -1;
4292 }
4293
4294 hdr->frame_control =
4295 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
4296 hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
4297 if (encrypt)
4298 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
4299 if (qos) {
4300 hdr->frame_control |=
4301 host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
4302 }
4303
4304 memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
4305 memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
4306 memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
4307 pos = (u8 *) (hdr + 1);
4308
4309 if (qos) {
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004310 /* Set highest priority in QoS header */
4311 pos[0] = 7;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004312 pos[1] = 0;
4313 pos += 2;
4314 }
4315
4316 memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
4317 pos += sizeof(rfc1042_header);
4318 WPA_PUT_BE16(pos, ETH_P_PAE);
4319 pos += 2;
4320 memcpy(pos, data, data_len);
4321
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004322 res = wpa_driver_nl80211_send_frame(bss, (u8 *) hdr, len, encrypt, 0,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004323 0, 0, 0, 0, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004324 if (res < 0) {
4325 wpa_printf(MSG_ERROR, "i802_send_eapol - packet len: %lu - "
4326 "failed: %d (%s)",
4327 (unsigned long) len, errno, strerror(errno));
4328 }
4329 os_free(hdr);
4330
4331 return res;
4332}
4333
4334
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004335static int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004336 unsigned int total_flags,
4337 unsigned int flags_or,
4338 unsigned int flags_and)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004339{
4340 struct i802_bss *bss = priv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004341 struct nl_msg *msg;
4342 struct nlattr *flags;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004343 struct nl80211_sta_flag_update upd;
4344
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004345 wpa_printf(MSG_DEBUG, "nl80211: Set STA flags - ifname=%s addr=" MACSTR
4346 " total_flags=0x%x flags_or=0x%x flags_and=0x%x authorized=%d",
4347 bss->ifname, MAC2STR(addr), total_flags, flags_or, flags_and,
4348 !!(total_flags & WPA_STA_AUTHORIZED));
4349
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004350 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
4351 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
4352 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004353
4354 /*
4355 * Backwards compatibility version using NL80211_ATTR_STA_FLAGS. This
4356 * can be removed eventually.
4357 */
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004358 flags = nla_nest_start(msg, NL80211_ATTR_STA_FLAGS);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004359 if (!flags ||
4360 ((total_flags & WPA_STA_AUTHORIZED) &&
4361 nla_put_flag(msg, NL80211_STA_FLAG_AUTHORIZED)) ||
4362 ((total_flags & WPA_STA_WMM) &&
4363 nla_put_flag(msg, NL80211_STA_FLAG_WME)) ||
4364 ((total_flags & WPA_STA_SHORT_PREAMBLE) &&
4365 nla_put_flag(msg, NL80211_STA_FLAG_SHORT_PREAMBLE)) ||
4366 ((total_flags & WPA_STA_MFP) &&
4367 nla_put_flag(msg, NL80211_STA_FLAG_MFP)) ||
4368 ((total_flags & WPA_STA_TDLS_PEER) &&
4369 nla_put_flag(msg, NL80211_STA_FLAG_TDLS_PEER)))
4370 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004371
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004372 nla_nest_end(msg, flags);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004373
4374 os_memset(&upd, 0, sizeof(upd));
4375 upd.mask = sta_flags_nl80211(flags_or | ~flags_and);
4376 upd.set = sta_flags_nl80211(flags_or);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004377 if (nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd))
4378 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004379
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004380 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
4381fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004382 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004383 return -ENOBUFS;
4384}
4385
4386
4387static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv,
4388 struct wpa_driver_associate_params *params)
4389{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004390 enum nl80211_iftype nlmode, old_mode;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004391
4392 if (params->p2p) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004393 wpa_printf(MSG_DEBUG, "nl80211: Setup AP operations for P2P "
4394 "group (GO)");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004395 nlmode = NL80211_IFTYPE_P2P_GO;
4396 } else
4397 nlmode = NL80211_IFTYPE_AP;
4398
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004399 old_mode = drv->nlmode;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004400 if (wpa_driver_nl80211_set_mode(drv->first_bss, nlmode)) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004401 nl80211_remove_monitor_interface(drv);
4402 return -1;
4403 }
4404
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08004405 if (params->freq.freq &&
4406 nl80211_set_channel(drv->first_bss, &params->freq, 0)) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004407 if (old_mode != nlmode)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004408 wpa_driver_nl80211_set_mode(drv->first_bss, old_mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004409 nl80211_remove_monitor_interface(drv);
4410 return -1;
4411 }
4412
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004413 return 0;
4414}
4415
4416
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004417static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv,
4418 int reset_mode)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004419{
4420 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004421 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004422
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004423 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_LEAVE_IBSS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004424 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004425 if (ret) {
4426 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS failed: ret=%d "
4427 "(%s)", ret, strerror(-ret));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004428 } else {
4429 wpa_printf(MSG_DEBUG,
4430 "nl80211: Leave IBSS request sent successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004431 }
4432
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004433 if (reset_mode &&
4434 wpa_driver_nl80211_set_mode(drv->first_bss,
Dmitry Shmidt56052862013-10-04 10:23:25 -07004435 NL80211_IFTYPE_STATION)) {
4436 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
4437 "station mode");
4438 }
4439
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004440 return ret;
4441}
4442
4443
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08004444static int nl80211_ht_vht_overrides(struct nl_msg *msg,
4445 struct wpa_driver_associate_params *params)
4446{
4447 if (params->disable_ht && nla_put_flag(msg, NL80211_ATTR_DISABLE_HT))
4448 return -1;
4449
4450 if (params->htcaps && params->htcaps_mask) {
4451 int sz = sizeof(struct ieee80211_ht_capabilities);
4452 wpa_hexdump(MSG_DEBUG, " * htcaps", params->htcaps, sz);
4453 wpa_hexdump(MSG_DEBUG, " * htcaps_mask",
4454 params->htcaps_mask, sz);
4455 if (nla_put(msg, NL80211_ATTR_HT_CAPABILITY, sz,
4456 params->htcaps) ||
4457 nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK, sz,
4458 params->htcaps_mask))
4459 return -1;
4460 }
4461
4462#ifdef CONFIG_VHT_OVERRIDES
4463 if (params->disable_vht) {
4464 wpa_printf(MSG_DEBUG, " * VHT disabled");
4465 if (nla_put_flag(msg, NL80211_ATTR_DISABLE_VHT))
4466 return -1;
4467 }
4468
4469 if (params->vhtcaps && params->vhtcaps_mask) {
4470 int sz = sizeof(struct ieee80211_vht_capabilities);
4471 wpa_hexdump(MSG_DEBUG, " * vhtcaps", params->vhtcaps, sz);
4472 wpa_hexdump(MSG_DEBUG, " * vhtcaps_mask",
4473 params->vhtcaps_mask, sz);
4474 if (nla_put(msg, NL80211_ATTR_VHT_CAPABILITY, sz,
4475 params->vhtcaps) ||
4476 nla_put(msg, NL80211_ATTR_VHT_CAPABILITY_MASK, sz,
4477 params->vhtcaps_mask))
4478 return -1;
4479 }
4480#endif /* CONFIG_VHT_OVERRIDES */
4481
4482 return 0;
4483}
4484
4485
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004486static int wpa_driver_nl80211_ibss(struct wpa_driver_nl80211_data *drv,
4487 struct wpa_driver_associate_params *params)
4488{
4489 struct nl_msg *msg;
4490 int ret = -1;
4491 int count = 0;
4492
4493 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS (ifindex=%d)", drv->ifindex);
4494
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004495 if (wpa_driver_nl80211_set_mode_ibss(drv->first_bss, &params->freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004496 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
4497 "IBSS mode");
4498 return -1;
4499 }
4500
4501retry:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004502 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_JOIN_IBSS)) ||
4503 params->ssid == NULL || params->ssid_len > sizeof(drv->ssid))
4504 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004505
4506 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
4507 params->ssid, params->ssid_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004508 if (nla_put(msg, NL80211_ATTR_SSID, params->ssid_len, params->ssid))
4509 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004510 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
4511 drv->ssid_len = params->ssid_len;
4512
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004513 if (nl80211_put_freq_params(msg, &params->freq) < 0 ||
4514 nl80211_put_beacon_int(msg, params->beacon_int))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004515 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004516
4517 ret = nl80211_set_conn_keys(params, msg);
4518 if (ret)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004519 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004520
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004521 if (params->bssid && params->fixed_bssid) {
4522 wpa_printf(MSG_DEBUG, " * BSSID=" MACSTR,
4523 MAC2STR(params->bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004524 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid))
4525 goto fail;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004526 }
4527
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004528 if (params->fixed_freq) {
4529 wpa_printf(MSG_DEBUG, " * fixed_freq");
4530 if (nla_put_flag(msg, NL80211_ATTR_FREQ_FIXED))
4531 goto fail;
4532 }
4533
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004534 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
4535 params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
4536 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
4537 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004538 wpa_printf(MSG_DEBUG, " * control port");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004539 if (nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT))
4540 goto fail;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004541 }
4542
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004543 if (params->wpa_ie) {
4544 wpa_hexdump(MSG_DEBUG,
4545 " * Extra IEs for Beacon/Probe Response frames",
4546 params->wpa_ie, params->wpa_ie_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004547 if (nla_put(msg, NL80211_ATTR_IE, params->wpa_ie_len,
4548 params->wpa_ie))
4549 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004550 }
4551
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08004552 ret = nl80211_ht_vht_overrides(msg, params);
4553 if (ret < 0)
4554 goto fail;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08004555
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004556 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4557 msg = NULL;
4558 if (ret) {
4559 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS failed: ret=%d (%s)",
4560 ret, strerror(-ret));
4561 count++;
4562 if (ret == -EALREADY && count == 1) {
4563 wpa_printf(MSG_DEBUG, "nl80211: Retry IBSS join after "
4564 "forced leave");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004565 nl80211_leave_ibss(drv, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004566 nlmsg_free(msg);
4567 goto retry;
4568 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004569 } else {
4570 wpa_printf(MSG_DEBUG,
4571 "nl80211: Join IBSS request sent successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004572 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004573
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004574fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004575 nlmsg_free(msg);
4576 return ret;
4577}
4578
4579
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004580static int nl80211_connect_common(struct wpa_driver_nl80211_data *drv,
4581 struct wpa_driver_associate_params *params,
4582 struct nl_msg *msg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004583{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004584 if (params->bssid) {
4585 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
4586 MAC2STR(params->bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004587 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid))
4588 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004589 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004590
Dmitry Shmidt96be6222014-02-13 10:16:51 -08004591 if (params->bssid_hint) {
4592 wpa_printf(MSG_DEBUG, " * bssid_hint=" MACSTR,
4593 MAC2STR(params->bssid_hint));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004594 if (nla_put(msg, NL80211_ATTR_MAC_HINT, ETH_ALEN,
4595 params->bssid_hint))
4596 return -1;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08004597 }
4598
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004599 if (params->freq.freq) {
4600 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq.freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004601 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
4602 params->freq.freq))
4603 return -1;
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004604 drv->assoc_freq = params->freq.freq;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004605 } else
4606 drv->assoc_freq = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004607
Dmitry Shmidt96be6222014-02-13 10:16:51 -08004608 if (params->freq_hint) {
4609 wpa_printf(MSG_DEBUG, " * freq_hint=%d", params->freq_hint);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004610 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ_HINT,
4611 params->freq_hint))
4612 return -1;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08004613 }
4614
Dmitry Shmidt04949592012-07-19 12:16:46 -07004615 if (params->bg_scan_period >= 0) {
4616 wpa_printf(MSG_DEBUG, " * bg scan period=%d",
4617 params->bg_scan_period);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004618 if (nla_put_u16(msg, NL80211_ATTR_BG_SCAN_PERIOD,
4619 params->bg_scan_period))
4620 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004621 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004622
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004623 if (params->ssid) {
4624 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
4625 params->ssid, params->ssid_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004626 if (nla_put(msg, NL80211_ATTR_SSID, params->ssid_len,
4627 params->ssid))
4628 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004629 if (params->ssid_len > sizeof(drv->ssid))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004630 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004631 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
4632 drv->ssid_len = params->ssid_len;
4633 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004634
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004635 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004636 if (params->wpa_ie &&
4637 nla_put(msg, NL80211_ATTR_IE, params->wpa_ie_len, params->wpa_ie))
4638 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004639
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004640 if (params->wpa_proto) {
4641 enum nl80211_wpa_versions ver = 0;
4642
4643 if (params->wpa_proto & WPA_PROTO_WPA)
4644 ver |= NL80211_WPA_VERSION_1;
4645 if (params->wpa_proto & WPA_PROTO_RSN)
4646 ver |= NL80211_WPA_VERSION_2;
4647
4648 wpa_printf(MSG_DEBUG, " * WPA Versions 0x%x", ver);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004649 if (nla_put_u32(msg, NL80211_ATTR_WPA_VERSIONS, ver))
4650 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004651 }
4652
4653 if (params->pairwise_suite != WPA_CIPHER_NONE) {
4654 u32 cipher = wpa_cipher_to_cipher_suite(params->pairwise_suite);
4655 wpa_printf(MSG_DEBUG, " * pairwise=0x%x", cipher);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004656 if (nla_put_u32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
4657 cipher))
4658 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004659 }
4660
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08004661 if (params->group_suite == WPA_CIPHER_GTK_NOT_USED &&
4662 !(drv->capa.enc & WPA_DRIVER_CAPA_ENC_GTK_NOT_USED)) {
4663 /*
4664 * This is likely to work even though many drivers do not
4665 * advertise support for operations without GTK.
4666 */
4667 wpa_printf(MSG_DEBUG, " * skip group cipher configuration for GTK_NOT_USED due to missing driver support advertisement");
4668 } else if (params->group_suite != WPA_CIPHER_NONE) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004669 u32 cipher = wpa_cipher_to_cipher_suite(params->group_suite);
4670 wpa_printf(MSG_DEBUG, " * group=0x%x", cipher);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004671 if (nla_put_u32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher))
4672 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004673 }
4674
4675 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
4676 params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
4677 params->key_mgmt_suite == WPA_KEY_MGMT_FT_IEEE8021X ||
4678 params->key_mgmt_suite == WPA_KEY_MGMT_FT_PSK ||
Dmitry Shmidt15907092014-03-25 10:42:57 -07004679 params->key_mgmt_suite == WPA_KEY_MGMT_CCKM ||
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -07004680 params->key_mgmt_suite == WPA_KEY_MGMT_OSEN ||
4681 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004682 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256 ||
Dmitry Shmidt807291d2015-01-27 13:40:23 -08004683 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SUITE_B ||
4684 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004685 int mgmt = WLAN_AKM_SUITE_PSK;
4686
4687 switch (params->key_mgmt_suite) {
4688 case WPA_KEY_MGMT_CCKM:
4689 mgmt = WLAN_AKM_SUITE_CCKM;
4690 break;
4691 case WPA_KEY_MGMT_IEEE8021X:
4692 mgmt = WLAN_AKM_SUITE_8021X;
4693 break;
4694 case WPA_KEY_MGMT_FT_IEEE8021X:
4695 mgmt = WLAN_AKM_SUITE_FT_8021X;
4696 break;
4697 case WPA_KEY_MGMT_FT_PSK:
4698 mgmt = WLAN_AKM_SUITE_FT_PSK;
4699 break;
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -07004700 case WPA_KEY_MGMT_IEEE8021X_SHA256:
4701 mgmt = WLAN_AKM_SUITE_8021X_SHA256;
4702 break;
4703 case WPA_KEY_MGMT_PSK_SHA256:
4704 mgmt = WLAN_AKM_SUITE_PSK_SHA256;
4705 break;
Dmitry Shmidt15907092014-03-25 10:42:57 -07004706 case WPA_KEY_MGMT_OSEN:
4707 mgmt = WLAN_AKM_SUITE_OSEN;
4708 break;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004709 case WPA_KEY_MGMT_IEEE8021X_SUITE_B:
4710 mgmt = WLAN_AKM_SUITE_8021X_SUITE_B;
4711 break;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08004712 case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192:
4713 mgmt = WLAN_AKM_SUITE_8021X_SUITE_B_192;
4714 break;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004715 case WPA_KEY_MGMT_PSK:
4716 default:
4717 mgmt = WLAN_AKM_SUITE_PSK;
4718 break;
4719 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07004720 wpa_printf(MSG_DEBUG, " * akm=0x%x", mgmt);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004721 if (nla_put_u32(msg, NL80211_ATTR_AKM_SUITES, mgmt))
4722 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004723 }
4724
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004725 if (nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT))
4726 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004727
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004728 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED &&
4729 nla_put_u32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED))
4730 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004731
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004732 if (params->rrm_used) {
4733 u32 drv_rrm_flags = drv->capa.rrm_flags;
4734 if (!(drv_rrm_flags &
4735 WPA_DRIVER_FLAGS_DS_PARAM_SET_IE_IN_PROBES) ||
4736 !(drv_rrm_flags & WPA_DRIVER_FLAGS_QUIET) ||
4737 nla_put_flag(msg, NL80211_ATTR_USE_RRM))
4738 return -1;
4739 }
4740
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08004741 if (nl80211_ht_vht_overrides(msg, params) < 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004742 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004743
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004744 if (params->p2p)
4745 wpa_printf(MSG_DEBUG, " * P2P group");
4746
4747 return 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004748}
4749
4750
4751static int wpa_driver_nl80211_try_connect(
4752 struct wpa_driver_nl80211_data *drv,
4753 struct wpa_driver_associate_params *params)
4754{
4755 struct nl_msg *msg;
4756 enum nl80211_auth_type type;
4757 int ret;
4758 int algs;
4759
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004760#ifdef CONFIG_DRIVER_NL80211_QCA
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004761 if (params->req_key_mgmt_offload && params->psk &&
4762 (params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
4763 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256 ||
4764 params->key_mgmt_suite == WPA_KEY_MGMT_FT_PSK)) {
4765 wpa_printf(MSG_DEBUG, "nl80211: Key management set PSK");
4766 ret = issue_key_mgmt_set_key(drv, params->psk, 32);
4767 if (ret)
4768 return ret;
4769 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004770#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004771
4772 wpa_printf(MSG_DEBUG, "nl80211: Connect (ifindex=%d)", drv->ifindex);
4773 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_CONNECT);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004774 if (!msg)
4775 return -1;
4776
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004777 ret = nl80211_connect_common(drv, params, msg);
4778 if (ret)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004779 goto fail;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004780
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004781 algs = 0;
4782 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
4783 algs++;
4784 if (params->auth_alg & WPA_AUTH_ALG_SHARED)
4785 algs++;
4786 if (params->auth_alg & WPA_AUTH_ALG_LEAP)
4787 algs++;
4788 if (algs > 1) {
4789 wpa_printf(MSG_DEBUG, " * Leave out Auth Type for automatic "
4790 "selection");
4791 goto skip_auth_type;
4792 }
4793
4794 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
4795 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
4796 else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
4797 type = NL80211_AUTHTYPE_SHARED_KEY;
4798 else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
4799 type = NL80211_AUTHTYPE_NETWORK_EAP;
4800 else if (params->auth_alg & WPA_AUTH_ALG_FT)
4801 type = NL80211_AUTHTYPE_FT;
4802 else
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004803 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004804
4805 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004806 if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE, type))
4807 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004808
4809skip_auth_type:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004810 ret = nl80211_set_conn_keys(params, msg);
4811 if (ret)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004812 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004813
4814 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4815 msg = NULL;
4816 if (ret) {
4817 wpa_printf(MSG_DEBUG, "nl80211: MLME connect failed: ret=%d "
4818 "(%s)", ret, strerror(-ret));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004819 } else {
4820 wpa_printf(MSG_DEBUG,
4821 "nl80211: Connect request send successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004822 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004823
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004824fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004825 nlmsg_free(msg);
4826 return ret;
4827
4828}
4829
4830
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004831static int wpa_driver_nl80211_connect(
4832 struct wpa_driver_nl80211_data *drv,
4833 struct wpa_driver_associate_params *params)
4834{
Jithu Jancea7c60b42014-12-03 18:54:40 +05304835 int ret;
4836
4837 /* Store the connection attempted bssid for future use */
4838 if (params->bssid)
4839 os_memcpy(drv->auth_attempt_bssid, params->bssid, ETH_ALEN);
4840 else
4841 os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
4842
4843 ret = wpa_driver_nl80211_try_connect(drv, params);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004844 if (ret == -EALREADY) {
4845 /*
4846 * cfg80211 does not currently accept new connections if
4847 * we are already connected. As a workaround, force
4848 * disconnection and try again.
4849 */
4850 wpa_printf(MSG_DEBUG, "nl80211: Explicitly "
4851 "disconnecting before reassociation "
4852 "attempt");
4853 if (wpa_driver_nl80211_disconnect(
4854 drv, WLAN_REASON_PREV_AUTH_NOT_VALID))
4855 return -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004856 ret = wpa_driver_nl80211_try_connect(drv, params);
4857 }
4858 return ret;
4859}
4860
4861
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004862static int wpa_driver_nl80211_associate(
4863 void *priv, struct wpa_driver_associate_params *params)
4864{
4865 struct i802_bss *bss = priv;
4866 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004867 int ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004868 struct nl_msg *msg;
4869
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004870 nl80211_unmask_11b_rates(bss);
4871
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004872 if (params->mode == IEEE80211_MODE_AP)
4873 return wpa_driver_nl80211_ap(drv, params);
4874
4875 if (params->mode == IEEE80211_MODE_IBSS)
4876 return wpa_driver_nl80211_ibss(drv, params);
4877
4878 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004879 enum nl80211_iftype nlmode = params->p2p ?
4880 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
4881
4882 if (wpa_driver_nl80211_set_mode(priv, nlmode) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004883 return -1;
4884 return wpa_driver_nl80211_connect(drv, params);
4885 }
4886
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004887 nl80211_mark_disconnected(drv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004888
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004889 wpa_printf(MSG_DEBUG, "nl80211: Associate (ifindex=%d)",
4890 drv->ifindex);
4891 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_ASSOCIATE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004892 if (!msg)
4893 return -1;
4894
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004895 ret = nl80211_connect_common(drv, params, msg);
4896 if (ret)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004897 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004898
4899 if (params->prev_bssid) {
4900 wpa_printf(MSG_DEBUG, " * prev_bssid=" MACSTR,
4901 MAC2STR(params->prev_bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004902 if (nla_put(msg, NL80211_ATTR_PREV_BSSID, ETH_ALEN,
4903 params->prev_bssid))
4904 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004905 }
4906
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004907 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4908 msg = NULL;
4909 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004910 wpa_dbg(drv->ctx, MSG_DEBUG,
4911 "nl80211: MLME command failed (assoc): ret=%d (%s)",
4912 ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004913 nl80211_dump_scan(drv);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004914 } else {
4915 wpa_printf(MSG_DEBUG,
4916 "nl80211: Association request send successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004917 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004918
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004919fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004920 nlmsg_free(msg);
4921 return ret;
4922}
4923
4924
4925static int nl80211_set_mode(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004926 int ifindex, enum nl80211_iftype mode)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004927{
4928 struct nl_msg *msg;
4929 int ret = -ENOBUFS;
4930
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004931 wpa_printf(MSG_DEBUG, "nl80211: Set mode ifindex %d iftype %d (%s)",
4932 ifindex, mode, nl80211_iftype_str(mode));
4933
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004934 msg = nl80211_cmd_msg(drv->first_bss, 0, NL80211_CMD_SET_INTERFACE);
4935 if (!msg || nla_put_u32(msg, NL80211_ATTR_IFTYPE, mode))
4936 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004937
4938 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004939 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004940 if (!ret)
4941 return 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004942fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004943 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004944 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface %d to mode %d:"
4945 " %d (%s)", ifindex, mode, ret, strerror(-ret));
4946 return ret;
4947}
4948
4949
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004950static int wpa_driver_nl80211_set_mode_impl(
4951 struct i802_bss *bss,
4952 enum nl80211_iftype nlmode,
4953 struct hostapd_freq_params *desired_freq_params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004954{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004955 struct wpa_driver_nl80211_data *drv = bss->drv;
4956 int ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004957 int i;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004958 int was_ap = is_ap_interface(drv->nlmode);
4959 int res;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004960 int mode_switch_res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004961
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004962 mode_switch_res = nl80211_set_mode(drv, drv->ifindex, nlmode);
4963 if (mode_switch_res && nlmode == nl80211_get_ifmode(bss))
4964 mode_switch_res = 0;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004965
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004966 if (mode_switch_res == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004967 drv->nlmode = nlmode;
4968 ret = 0;
4969 goto done;
4970 }
4971
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004972 if (mode_switch_res == -ENODEV)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004973 return -1;
4974
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004975 if (nlmode == drv->nlmode) {
4976 wpa_printf(MSG_DEBUG, "nl80211: Interface already in "
4977 "requested mode - ignore error");
4978 ret = 0;
4979 goto done; /* Already in the requested mode */
4980 }
4981
4982 /* mac80211 doesn't allow mode changes while the device is up, so
4983 * take the device down, try to set the mode again, and bring the
4984 * device back up.
4985 */
4986 wpa_printf(MSG_DEBUG, "nl80211: Try mode change after setting "
4987 "interface down");
4988 for (i = 0; i < 10; i++) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004989 res = i802_set_iface_flags(bss, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004990 if (res == -EACCES || res == -ENODEV)
4991 break;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004992 if (res != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004993 wpa_printf(MSG_DEBUG, "nl80211: Failed to set "
4994 "interface down");
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004995 os_sleep(0, 100000);
4996 continue;
4997 }
4998
4999 /*
5000 * Setting the mode will fail for some drivers if the phy is
5001 * on a frequency that the mode is disallowed in.
5002 */
5003 if (desired_freq_params) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005004 res = nl80211_set_channel(bss, desired_freq_params, 0);
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005005 if (res) {
5006 wpa_printf(MSG_DEBUG,
5007 "nl80211: Failed to set frequency on interface");
5008 }
5009 }
5010
5011 /* Try to set the mode again while the interface is down */
5012 mode_switch_res = nl80211_set_mode(drv, drv->ifindex, nlmode);
5013 if (mode_switch_res == -EBUSY) {
5014 wpa_printf(MSG_DEBUG,
5015 "nl80211: Delaying mode set while interface going down");
5016 os_sleep(0, 100000);
5017 continue;
5018 }
5019 ret = mode_switch_res;
5020 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005021 }
5022
5023 if (!ret) {
5024 wpa_printf(MSG_DEBUG, "nl80211: Mode change succeeded while "
5025 "interface is down");
5026 drv->nlmode = nlmode;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005027 drv->ignore_if_down_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005028 }
5029
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005030 /* Bring the interface back up */
5031 res = linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1);
5032 if (res != 0) {
5033 wpa_printf(MSG_DEBUG,
5034 "nl80211: Failed to set interface up after switching mode");
5035 ret = -1;
5036 }
5037
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005038done:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005039 if (ret) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005040 wpa_printf(MSG_DEBUG, "nl80211: Interface mode change to %d "
5041 "from %d failed", nlmode, drv->nlmode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005042 return ret;
5043 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005044
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005045 if (is_p2p_net_interface(nlmode)) {
5046 wpa_printf(MSG_DEBUG,
5047 "nl80211: Interface %s mode change to P2P - disable 11b rates",
5048 bss->ifname);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005049 nl80211_disable_11b_rates(drv, drv->ifindex, 1);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005050 } else if (drv->disabled_11b_rates) {
5051 wpa_printf(MSG_DEBUG,
5052 "nl80211: Interface %s mode changed to non-P2P - re-enable 11b rates",
5053 bss->ifname);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005054 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005055 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005056
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005057 if (is_ap_interface(nlmode)) {
5058 nl80211_mgmt_unsubscribe(bss, "start AP");
5059 /* Setup additional AP mode functionality if needed */
5060 if (nl80211_setup_ap(bss))
5061 return -1;
5062 } else if (was_ap) {
5063 /* Remove additional AP mode functionality */
5064 nl80211_teardown_ap(bss);
5065 } else {
5066 nl80211_mgmt_unsubscribe(bss, "mode change");
5067 }
5068
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005069 if (is_mesh_interface(nlmode) &&
5070 nl80211_mgmt_subscribe_mesh(bss))
5071 return -1;
5072
Dmitry Shmidt04949592012-07-19 12:16:46 -07005073 if (!bss->in_deinit && !is_ap_interface(nlmode) &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005074 !is_mesh_interface(nlmode) &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005075 nl80211_mgmt_subscribe_non_ap(bss) < 0)
5076 wpa_printf(MSG_DEBUG, "nl80211: Failed to register Action "
5077 "frame processing - ignore for now");
5078
5079 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005080}
5081
5082
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005083int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
5084 enum nl80211_iftype nlmode)
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005085{
5086 return wpa_driver_nl80211_set_mode_impl(bss, nlmode, NULL);
5087}
5088
5089
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07005090static int wpa_driver_nl80211_set_mode_ibss(struct i802_bss *bss,
5091 struct hostapd_freq_params *freq)
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005092{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005093 return wpa_driver_nl80211_set_mode_impl(bss, NL80211_IFTYPE_ADHOC,
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07005094 freq);
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005095}
5096
5097
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005098static int wpa_driver_nl80211_get_capa(void *priv,
5099 struct wpa_driver_capa *capa)
5100{
5101 struct i802_bss *bss = priv;
5102 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07005103
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005104 if (!drv->has_capability)
5105 return -1;
5106 os_memcpy(capa, &drv->capa, sizeof(*capa));
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005107 if (drv->extended_capa && drv->extended_capa_mask) {
5108 capa->extended_capa = drv->extended_capa;
5109 capa->extended_capa_mask = drv->extended_capa_mask;
5110 capa->extended_capa_len = drv->extended_capa_len;
5111 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005112
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005113 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005114}
5115
5116
5117static int wpa_driver_nl80211_set_operstate(void *priv, int state)
5118{
5119 struct i802_bss *bss = priv;
5120 struct wpa_driver_nl80211_data *drv = bss->drv;
5121
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005122 wpa_printf(MSG_DEBUG, "nl80211: Set %s operstate %d->%d (%s)",
5123 bss->ifname, drv->operstate, state,
5124 state ? "UP" : "DORMANT");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005125 drv->operstate = state;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005126 return netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, -1,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005127 state ? IF_OPER_UP : IF_OPER_DORMANT);
5128}
5129
5130
5131static int wpa_driver_nl80211_set_supp_port(void *priv, int authorized)
5132{
5133 struct i802_bss *bss = priv;
5134 struct wpa_driver_nl80211_data *drv = bss->drv;
5135 struct nl_msg *msg;
5136 struct nl80211_sta_flag_update upd;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005137 int ret;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005138
5139 if (!drv->associated && is_zero_ether_addr(drv->bssid) && !authorized) {
5140 wpa_printf(MSG_DEBUG, "nl80211: Skip set_supp_port(unauthorized) while not associated");
5141 return 0;
5142 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005143
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005144 wpa_printf(MSG_DEBUG, "nl80211: Set supplicant port %sauthorized for "
5145 MACSTR, authorized ? "" : "un", MAC2STR(drv->bssid));
5146
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005147 os_memset(&upd, 0, sizeof(upd));
5148 upd.mask = BIT(NL80211_STA_FLAG_AUTHORIZED);
5149 if (authorized)
5150 upd.set = BIT(NL80211_STA_FLAG_AUTHORIZED);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005151
5152 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
5153 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid) ||
5154 nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd)) {
5155 nlmsg_free(msg);
5156 return -ENOBUFS;
5157 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005158
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005159 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005160 if (!ret)
5161 return 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005162 wpa_printf(MSG_DEBUG, "nl80211: Failed to set STA flag: %d (%s)",
5163 ret, strerror(-ret));
5164 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005165}
5166
5167
Jouni Malinen75ecf522011-06-27 15:19:46 -07005168/* Set kernel driver on given frequency (MHz) */
5169static int i802_set_freq(void *priv, struct hostapd_freq_params *freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005170{
Jouni Malinen75ecf522011-06-27 15:19:46 -07005171 struct i802_bss *bss = priv;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07005172 return nl80211_set_channel(bss, freq, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005173}
5174
5175
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005176static inline int min_int(int a, int b)
5177{
5178 if (a < b)
5179 return a;
5180 return b;
5181}
5182
5183
5184static int get_key_handler(struct nl_msg *msg, void *arg)
5185{
5186 struct nlattr *tb[NL80211_ATTR_MAX + 1];
5187 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
5188
5189 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
5190 genlmsg_attrlen(gnlh, 0), NULL);
5191
5192 /*
5193 * TODO: validate the key index and mac address!
5194 * Otherwise, there's a race condition as soon as
5195 * the kernel starts sending key notifications.
5196 */
5197
5198 if (tb[NL80211_ATTR_KEY_SEQ])
5199 memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]),
5200 min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6));
5201 return NL_SKIP;
5202}
5203
5204
5205static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
5206 int idx, u8 *seq)
5207{
5208 struct i802_bss *bss = priv;
5209 struct wpa_driver_nl80211_data *drv = bss->drv;
5210 struct nl_msg *msg;
5211
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005212 msg = nl80211_ifindex_msg(drv, if_nametoindex(iface), 0,
5213 NL80211_CMD_GET_KEY);
5214 if (!msg ||
5215 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
5216 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, idx)) {
5217 nlmsg_free(msg);
5218 return -ENOBUFS;
5219 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005220
5221 memset(seq, 0, 6);
5222
5223 return send_and_recv_msgs(drv, msg, get_key_handler, seq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005224}
5225
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005226
5227static int i802_set_rts(void *priv, int rts)
5228{
5229 struct i802_bss *bss = priv;
5230 struct wpa_driver_nl80211_data *drv = bss->drv;
5231 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005232 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005233 u32 val;
5234
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005235 if (rts >= 2347)
5236 val = (u32) -1;
5237 else
5238 val = rts;
5239
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005240 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_WIPHY)) ||
5241 nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD, val)) {
5242 nlmsg_free(msg);
5243 return -ENOBUFS;
5244 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005245
5246 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5247 if (!ret)
5248 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005249 wpa_printf(MSG_DEBUG, "nl80211: Failed to set RTS threshold %d: "
5250 "%d (%s)", rts, ret, strerror(-ret));
5251 return ret;
5252}
5253
5254
5255static int i802_set_frag(void *priv, int frag)
5256{
5257 struct i802_bss *bss = priv;
5258 struct wpa_driver_nl80211_data *drv = bss->drv;
5259 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005260 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005261 u32 val;
5262
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005263 if (frag >= 2346)
5264 val = (u32) -1;
5265 else
5266 val = frag;
5267
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005268 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_WIPHY)) ||
5269 nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD, val)) {
5270 nlmsg_free(msg);
5271 return -ENOBUFS;
5272 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005273
5274 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5275 if (!ret)
5276 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005277 wpa_printf(MSG_DEBUG, "nl80211: Failed to set fragmentation threshold "
5278 "%d: %d (%s)", frag, ret, strerror(-ret));
5279 return ret;
5280}
5281
5282
5283static int i802_flush(void *priv)
5284{
5285 struct i802_bss *bss = priv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005286 struct nl_msg *msg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005287 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005288
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005289 wpa_printf(MSG_DEBUG, "nl80211: flush -> DEL_STATION %s (all)",
5290 bss->ifname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005291
5292 /*
5293 * XXX: FIX! this needs to flush all VLANs too
5294 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005295 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_DEL_STATION);
5296 res = send_and_recv_msgs(bss->drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005297 if (res) {
5298 wpa_printf(MSG_DEBUG, "nl80211: Station flush failed: ret=%d "
5299 "(%s)", res, strerror(-res));
5300 }
5301 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005302}
5303
5304
5305static int get_sta_handler(struct nl_msg *msg, void *arg)
5306{
5307 struct nlattr *tb[NL80211_ATTR_MAX + 1];
5308 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
5309 struct hostap_sta_driver_data *data = arg;
5310 struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
5311 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
5312 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
5313 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
5314 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
5315 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
5316 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03005317 [NL80211_STA_INFO_TX_FAILED] = { .type = NLA_U32 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005318 };
5319
5320 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
5321 genlmsg_attrlen(gnlh, 0), NULL);
5322
5323 /*
5324 * TODO: validate the interface and mac address!
5325 * Otherwise, there's a race condition as soon as
5326 * the kernel starts sending station notifications.
5327 */
5328
5329 if (!tb[NL80211_ATTR_STA_INFO]) {
5330 wpa_printf(MSG_DEBUG, "sta stats missing!");
5331 return NL_SKIP;
5332 }
5333 if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
5334 tb[NL80211_ATTR_STA_INFO],
5335 stats_policy)) {
5336 wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
5337 return NL_SKIP;
5338 }
5339
5340 if (stats[NL80211_STA_INFO_INACTIVE_TIME])
5341 data->inactive_msec =
5342 nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
5343 if (stats[NL80211_STA_INFO_RX_BYTES])
5344 data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
5345 if (stats[NL80211_STA_INFO_TX_BYTES])
5346 data->tx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
5347 if (stats[NL80211_STA_INFO_RX_PACKETS])
5348 data->rx_packets =
5349 nla_get_u32(stats[NL80211_STA_INFO_RX_PACKETS]);
5350 if (stats[NL80211_STA_INFO_TX_PACKETS])
5351 data->tx_packets =
5352 nla_get_u32(stats[NL80211_STA_INFO_TX_PACKETS]);
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03005353 if (stats[NL80211_STA_INFO_TX_FAILED])
5354 data->tx_retry_failed =
5355 nla_get_u32(stats[NL80211_STA_INFO_TX_FAILED]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005356
5357 return NL_SKIP;
5358}
5359
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005360static int i802_read_sta_data(struct i802_bss *bss,
5361 struct hostap_sta_driver_data *data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005362 const u8 *addr)
5363{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005364 struct nl_msg *msg;
5365
5366 os_memset(data, 0, sizeof(*data));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005367
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005368 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_GET_STATION)) ||
5369 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
5370 nlmsg_free(msg);
5371 return -ENOBUFS;
5372 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005373
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005374 return send_and_recv_msgs(bss->drv, msg, get_sta_handler, data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005375}
5376
5377
5378static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
5379 int cw_min, int cw_max, int burst_time)
5380{
5381 struct i802_bss *bss = priv;
5382 struct wpa_driver_nl80211_data *drv = bss->drv;
5383 struct nl_msg *msg;
5384 struct nlattr *txq, *params;
5385
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005386 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005387 if (!msg)
5388 return -1;
5389
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005390 txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS);
5391 if (!txq)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005392 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005393
5394 /* We are only sending parameters for a single TXQ at a time */
5395 params = nla_nest_start(msg, 1);
5396 if (!params)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005397 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005398
5399 switch (queue) {
5400 case 0:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005401 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VO))
5402 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005403 break;
5404 case 1:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005405 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VI))
5406 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005407 break;
5408 case 2:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005409 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BE))
5410 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005411 break;
5412 case 3:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005413 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BK))
5414 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005415 break;
5416 }
5417 /* Burst time is configured in units of 0.1 msec and TXOP parameter in
5418 * 32 usec, so need to convert the value here. */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005419 if (nla_put_u16(msg, NL80211_TXQ_ATTR_TXOP,
5420 (burst_time * 100 + 16) / 32) ||
5421 nla_put_u16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min) ||
5422 nla_put_u16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max) ||
5423 nla_put_u8(msg, NL80211_TXQ_ATTR_AIFS, aifs))
5424 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005425
5426 nla_nest_end(msg, params);
5427
5428 nla_nest_end(msg, txq);
5429
5430 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
5431 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005432 msg = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005433fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005434 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005435 return -1;
5436}
5437
5438
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005439static int i802_set_sta_vlan(struct i802_bss *bss, const u8 *addr,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005440 const char *ifname, int vlan_id)
5441{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005442 struct wpa_driver_nl80211_data *drv = bss->drv;
5443 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005444 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005445
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005446 wpa_printf(MSG_DEBUG, "nl80211: %s[%d]: set_sta_vlan(" MACSTR
5447 ", ifname=%s[%d], vlan_id=%d)",
5448 bss->ifname, if_nametoindex(bss->ifname),
5449 MAC2STR(addr), ifname, if_nametoindex(ifname), vlan_id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005450 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
5451 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
5452 nla_put_u32(msg, NL80211_ATTR_STA_VLAN, if_nametoindex(ifname))) {
5453 nlmsg_free(msg);
5454 return -ENOBUFS;
5455 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005456
5457 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5458 if (ret < 0) {
5459 wpa_printf(MSG_ERROR, "nl80211: NL80211_ATTR_STA_VLAN (addr="
5460 MACSTR " ifname=%s vlan_id=%d) failed: %d (%s)",
5461 MAC2STR(addr), ifname, vlan_id, ret,
5462 strerror(-ret));
5463 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005464 return ret;
5465}
5466
5467
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005468static int i802_get_inact_sec(void *priv, const u8 *addr)
5469{
5470 struct hostap_sta_driver_data data;
5471 int ret;
5472
5473 data.inactive_msec = (unsigned long) -1;
5474 ret = i802_read_sta_data(priv, &data, addr);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08005475 if (ret == -ENOENT)
5476 return -ENOENT;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005477 if (ret || data.inactive_msec == (unsigned long) -1)
5478 return -1;
5479 return data.inactive_msec / 1000;
5480}
5481
5482
5483static int i802_sta_clear_stats(void *priv, const u8 *addr)
5484{
5485#if 0
5486 /* TODO */
5487#endif
5488 return 0;
5489}
5490
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005491
5492static int i802_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
5493 int reason)
5494{
5495 struct i802_bss *bss = priv;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005496 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005497 struct ieee80211_mgmt mgmt;
5498
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005499 if (is_mesh_interface(drv->nlmode))
5500 return -1;
5501
Dmitry Shmidt04949592012-07-19 12:16:46 -07005502 if (drv->device_ap_sme)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005503 return wpa_driver_nl80211_sta_remove(bss, addr, 1, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005504
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005505 memset(&mgmt, 0, sizeof(mgmt));
5506 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
5507 WLAN_FC_STYPE_DEAUTH);
5508 memcpy(mgmt.da, addr, ETH_ALEN);
5509 memcpy(mgmt.sa, own_addr, ETH_ALEN);
5510 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
5511 mgmt.u.deauth.reason_code = host_to_le16(reason);
5512 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
5513 IEEE80211_HDRLEN +
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005514 sizeof(mgmt.u.deauth), 0, 0, 0, 0,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005515 0, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005516}
5517
5518
5519static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
5520 int reason)
5521{
5522 struct i802_bss *bss = priv;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005523 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005524 struct ieee80211_mgmt mgmt;
5525
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005526 if (is_mesh_interface(drv->nlmode))
5527 return -1;
5528
Dmitry Shmidt04949592012-07-19 12:16:46 -07005529 if (drv->device_ap_sme)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005530 return wpa_driver_nl80211_sta_remove(bss, addr, 0, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005531
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005532 memset(&mgmt, 0, sizeof(mgmt));
5533 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
5534 WLAN_FC_STYPE_DISASSOC);
5535 memcpy(mgmt.da, addr, ETH_ALEN);
5536 memcpy(mgmt.sa, own_addr, ETH_ALEN);
5537 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
5538 mgmt.u.disassoc.reason_code = host_to_le16(reason);
5539 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
5540 IEEE80211_HDRLEN +
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005541 sizeof(mgmt.u.disassoc), 0, 0, 0, 0,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005542 0, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005543}
5544
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005545
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07005546static void dump_ifidx(struct wpa_driver_nl80211_data *drv)
5547{
5548 char buf[200], *pos, *end;
5549 int i, res;
5550
5551 pos = buf;
5552 end = pos + sizeof(buf);
5553
5554 for (i = 0; i < drv->num_if_indices; i++) {
5555 if (!drv->if_indices[i])
5556 continue;
5557 res = os_snprintf(pos, end - pos, " %d", drv->if_indices[i]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005558 if (os_snprintf_error(end - pos, res))
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07005559 break;
5560 pos += res;
5561 }
5562 *pos = '\0';
5563
5564 wpa_printf(MSG_DEBUG, "nl80211: if_indices[%d]:%s",
5565 drv->num_if_indices, buf);
5566}
5567
5568
Jouni Malinen75ecf522011-06-27 15:19:46 -07005569static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
5570{
5571 int i;
5572 int *old;
5573
5574 wpa_printf(MSG_DEBUG, "nl80211: Add own interface ifindex %d",
5575 ifidx);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07005576 if (have_ifidx(drv, ifidx)) {
5577 wpa_printf(MSG_DEBUG, "nl80211: ifindex %d already in the list",
5578 ifidx);
5579 return;
5580 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07005581 for (i = 0; i < drv->num_if_indices; i++) {
5582 if (drv->if_indices[i] == 0) {
5583 drv->if_indices[i] = ifidx;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07005584 dump_ifidx(drv);
Jouni Malinen75ecf522011-06-27 15:19:46 -07005585 return;
5586 }
5587 }
5588
5589 if (drv->if_indices != drv->default_if_indices)
5590 old = drv->if_indices;
5591 else
5592 old = NULL;
5593
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005594 drv->if_indices = os_realloc_array(old, drv->num_if_indices + 1,
5595 sizeof(int));
Jouni Malinen75ecf522011-06-27 15:19:46 -07005596 if (!drv->if_indices) {
5597 if (!old)
5598 drv->if_indices = drv->default_if_indices;
5599 else
5600 drv->if_indices = old;
5601 wpa_printf(MSG_ERROR, "Failed to reallocate memory for "
5602 "interfaces");
5603 wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx);
5604 return;
5605 } else if (!old)
5606 os_memcpy(drv->if_indices, drv->default_if_indices,
5607 sizeof(drv->default_if_indices));
5608 drv->if_indices[drv->num_if_indices] = ifidx;
5609 drv->num_if_indices++;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07005610 dump_ifidx(drv);
Jouni Malinen75ecf522011-06-27 15:19:46 -07005611}
5612
5613
5614static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
5615{
5616 int i;
5617
5618 for (i = 0; i < drv->num_if_indices; i++) {
5619 if (drv->if_indices[i] == ifidx) {
5620 drv->if_indices[i] = 0;
5621 break;
5622 }
5623 }
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07005624 dump_ifidx(drv);
Jouni Malinen75ecf522011-06-27 15:19:46 -07005625}
5626
5627
5628static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
5629{
5630 int i;
5631
5632 for (i = 0; i < drv->num_if_indices; i++)
5633 if (drv->if_indices[i] == ifidx)
5634 return 1;
5635
5636 return 0;
5637}
5638
5639
5640static int i802_set_wds_sta(void *priv, const u8 *addr, int aid, int val,
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07005641 const char *bridge_ifname, char *ifname_wds)
Jouni Malinen75ecf522011-06-27 15:19:46 -07005642{
5643 struct i802_bss *bss = priv;
5644 struct wpa_driver_nl80211_data *drv = bss->drv;
5645 char name[IFNAMSIZ + 1];
5646
5647 os_snprintf(name, sizeof(name), "%s.sta%d", bss->ifname, aid);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005648 if (ifname_wds)
5649 os_strlcpy(ifname_wds, name, IFNAMSIZ + 1);
5650
Jouni Malinen75ecf522011-06-27 15:19:46 -07005651 wpa_printf(MSG_DEBUG, "nl80211: Set WDS STA addr=" MACSTR
5652 " aid=%d val=%d name=%s", MAC2STR(addr), aid, val, name);
5653 if (val) {
5654 if (!if_nametoindex(name)) {
5655 if (nl80211_create_iface(drv, name,
5656 NL80211_IFTYPE_AP_VLAN,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005657 bss->addr, 1, NULL, NULL, 0) <
5658 0)
Jouni Malinen75ecf522011-06-27 15:19:46 -07005659 return -1;
5660 if (bridge_ifname &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005661 linux_br_add_if(drv->global->ioctl_sock,
5662 bridge_ifname, name) < 0)
Jouni Malinen75ecf522011-06-27 15:19:46 -07005663 return -1;
5664 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005665 if (linux_set_iface_flags(drv->global->ioctl_sock, name, 1)) {
5666 wpa_printf(MSG_ERROR, "nl80211: Failed to set WDS STA "
5667 "interface %s up", name);
5668 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07005669 return i802_set_sta_vlan(priv, addr, name, 0);
5670 } else {
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005671 if (bridge_ifname)
5672 linux_br_del_if(drv->global->ioctl_sock, bridge_ifname,
5673 name);
5674
Jouni Malinen75ecf522011-06-27 15:19:46 -07005675 i802_set_sta_vlan(priv, addr, bss->ifname, 0);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08005676 nl80211_remove_iface(drv, if_nametoindex(name));
5677 return 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07005678 }
5679}
5680
5681
5682static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
5683{
5684 struct wpa_driver_nl80211_data *drv = eloop_ctx;
5685 struct sockaddr_ll lladdr;
5686 unsigned char buf[3000];
5687 int len;
5688 socklen_t fromlen = sizeof(lladdr);
5689
5690 len = recvfrom(sock, buf, sizeof(buf), 0,
5691 (struct sockaddr *)&lladdr, &fromlen);
5692 if (len < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005693 wpa_printf(MSG_ERROR, "nl80211: EAPOL recv failed: %s",
5694 strerror(errno));
Jouni Malinen75ecf522011-06-27 15:19:46 -07005695 return;
5696 }
5697
5698 if (have_ifidx(drv, lladdr.sll_ifindex))
5699 drv_event_eapol_rx(drv->ctx, lladdr.sll_addr, buf, len);
5700}
5701
5702
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005703static int i802_check_bridge(struct wpa_driver_nl80211_data *drv,
5704 struct i802_bss *bss,
5705 const char *brname, const char *ifname)
5706{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005707 int br_ifindex;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005708 char in_br[IFNAMSIZ];
5709
5710 os_strlcpy(bss->brname, brname, IFNAMSIZ);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005711 br_ifindex = if_nametoindex(brname);
5712 if (br_ifindex == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005713 /*
5714 * Bridge was configured, but the bridge device does
5715 * not exist. Try to add it now.
5716 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005717 if (linux_br_add(drv->global->ioctl_sock, brname) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005718 wpa_printf(MSG_ERROR, "nl80211: Failed to add the "
5719 "bridge interface %s: %s",
5720 brname, strerror(errno));
5721 return -1;
5722 }
5723 bss->added_bridge = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005724 br_ifindex = if_nametoindex(brname);
5725 add_ifidx(drv, br_ifindex);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005726 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005727 bss->br_ifindex = br_ifindex;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005728
5729 if (linux_br_get(in_br, ifname) == 0) {
5730 if (os_strcmp(in_br, brname) == 0)
5731 return 0; /* already in the bridge */
5732
5733 wpa_printf(MSG_DEBUG, "nl80211: Removing interface %s from "
5734 "bridge %s", ifname, in_br);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005735 if (linux_br_del_if(drv->global->ioctl_sock, in_br, ifname) <
5736 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005737 wpa_printf(MSG_ERROR, "nl80211: Failed to "
5738 "remove interface %s from bridge "
5739 "%s: %s",
5740 ifname, brname, strerror(errno));
5741 return -1;
5742 }
5743 }
5744
5745 wpa_printf(MSG_DEBUG, "nl80211: Adding interface %s into bridge %s",
5746 ifname, brname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005747 if (linux_br_add_if(drv->global->ioctl_sock, brname, ifname) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005748 wpa_printf(MSG_ERROR, "nl80211: Failed to add interface %s "
5749 "into bridge %s: %s",
5750 ifname, brname, strerror(errno));
5751 return -1;
5752 }
5753 bss->added_if_into_bridge = 1;
5754
5755 return 0;
5756}
5757
5758
5759static void *i802_init(struct hostapd_data *hapd,
5760 struct wpa_init_params *params)
5761{
5762 struct wpa_driver_nl80211_data *drv;
5763 struct i802_bss *bss;
5764 size_t i;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005765 char master_ifname[IFNAMSIZ];
5766 int ifindex, br_ifindex = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005767 int br_added = 0;
5768
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005769 bss = wpa_driver_nl80211_drv_init(hapd, params->ifname,
5770 params->global_priv, 1,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005771 params->bssid, params->driver_params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005772 if (bss == NULL)
5773 return NULL;
5774
5775 drv = bss->drv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005776
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005777 if (linux_br_get(master_ifname, params->ifname) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005778 wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in bridge %s",
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005779 params->ifname, master_ifname);
5780 br_ifindex = if_nametoindex(master_ifname);
5781 os_strlcpy(bss->brname, master_ifname, IFNAMSIZ);
5782 } else if ((params->num_bridge == 0 || !params->bridge[0]) &&
5783 linux_master_get(master_ifname, params->ifname) == 0) {
5784 wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in master %s",
5785 params->ifname, master_ifname);
5786 /* start listening for EAPOL on the master interface */
5787 add_ifidx(drv, if_nametoindex(master_ifname));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005788 } else {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005789 master_ifname[0] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005790 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005791
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005792 bss->br_ifindex = br_ifindex;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005793
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005794 for (i = 0; i < params->num_bridge; i++) {
5795 if (params->bridge[i]) {
5796 ifindex = if_nametoindex(params->bridge[i]);
5797 if (ifindex)
5798 add_ifidx(drv, ifindex);
5799 if (ifindex == br_ifindex)
5800 br_added = 1;
5801 }
5802 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005803
5804 /* start listening for EAPOL on the default AP interface */
5805 add_ifidx(drv, drv->ifindex);
5806
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005807 if (params->num_bridge && params->bridge[0]) {
5808 if (i802_check_bridge(drv, bss, params->bridge[0],
5809 params->ifname) < 0)
5810 goto failed;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005811 if (os_strcmp(params->bridge[0], master_ifname) != 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005812 br_added = 1;
5813 }
5814
5815 if (!br_added && br_ifindex &&
5816 (params->num_bridge == 0 || !params->bridge[0]))
5817 add_ifidx(drv, br_ifindex);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005818
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005819#ifdef CONFIG_LIBNL3_ROUTE
5820 if (bss->added_if_into_bridge) {
5821 drv->rtnl_sk = nl_socket_alloc();
5822 if (drv->rtnl_sk == NULL) {
5823 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate nl_sock");
5824 goto failed;
5825 }
5826
5827 if (nl_connect(drv->rtnl_sk, NETLINK_ROUTE)) {
5828 wpa_printf(MSG_ERROR, "nl80211: Failed to connect nl_sock to NETLINK_ROUTE: %s",
5829 strerror(errno));
5830 goto failed;
5831 }
5832 }
5833#endif /* CONFIG_LIBNL3_ROUTE */
5834
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005835 drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
5836 if (drv->eapol_sock < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005837 wpa_printf(MSG_ERROR, "nl80211: socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE) failed: %s",
5838 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005839 goto failed;
5840 }
5841
5842 if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
5843 {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005844 wpa_printf(MSG_INFO, "nl80211: Could not register read socket for eapol");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005845 goto failed;
5846 }
5847
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005848 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
5849 params->own_addr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005850 goto failed;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005851 os_memcpy(drv->perm_addr, params->own_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005852
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005853 memcpy(bss->addr, params->own_addr, ETH_ALEN);
5854
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005855 return bss;
5856
5857failed:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005858 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005859 return NULL;
5860}
5861
5862
5863static void i802_deinit(void *priv)
5864{
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005865 struct i802_bss *bss = priv;
5866 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005867}
5868
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005869
5870static enum nl80211_iftype wpa_driver_nl80211_if_type(
5871 enum wpa_driver_if_type type)
5872{
5873 switch (type) {
5874 case WPA_IF_STATION:
5875 return NL80211_IFTYPE_STATION;
5876 case WPA_IF_P2P_CLIENT:
5877 case WPA_IF_P2P_GROUP:
5878 return NL80211_IFTYPE_P2P_CLIENT;
5879 case WPA_IF_AP_VLAN:
5880 return NL80211_IFTYPE_AP_VLAN;
5881 case WPA_IF_AP_BSS:
5882 return NL80211_IFTYPE_AP;
5883 case WPA_IF_P2P_GO:
5884 return NL80211_IFTYPE_P2P_GO;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005885 case WPA_IF_P2P_DEVICE:
5886 return NL80211_IFTYPE_P2P_DEVICE;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005887 case WPA_IF_MESH:
5888 return NL80211_IFTYPE_MESH_POINT;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005889 default:
5890 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005891 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005892}
5893
5894
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005895static int nl80211_addr_in_use(struct nl80211_global *global, const u8 *addr)
5896{
5897 struct wpa_driver_nl80211_data *drv;
5898 dl_list_for_each(drv, &global->interfaces,
5899 struct wpa_driver_nl80211_data, list) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005900 if (os_memcmp(addr, drv->first_bss->addr, ETH_ALEN) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005901 return 1;
5902 }
5903 return 0;
5904}
5905
5906
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005907static int nl80211_vif_addr(struct wpa_driver_nl80211_data *drv, u8 *new_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005908{
5909 unsigned int idx;
5910
5911 if (!drv->global)
5912 return -1;
5913
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005914 os_memcpy(new_addr, drv->first_bss->addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005915 for (idx = 0; idx < 64; idx++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005916 new_addr[0] = drv->first_bss->addr[0] | 0x02;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005917 new_addr[0] ^= idx << 2;
5918 if (!nl80211_addr_in_use(drv->global, new_addr))
5919 break;
5920 }
5921 if (idx == 64)
5922 return -1;
5923
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005924 wpa_printf(MSG_DEBUG, "nl80211: Assigned new virtual interface address "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005925 MACSTR, MAC2STR(new_addr));
5926
5927 return 0;
5928}
5929
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005930
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005931struct wdev_info {
5932 u64 wdev_id;
5933 int wdev_id_set;
5934 u8 macaddr[ETH_ALEN];
5935};
5936
5937static int nl80211_wdev_handler(struct nl_msg *msg, void *arg)
5938{
5939 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
5940 struct nlattr *tb[NL80211_ATTR_MAX + 1];
5941 struct wdev_info *wi = arg;
5942
5943 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
5944 genlmsg_attrlen(gnlh, 0), NULL);
5945 if (tb[NL80211_ATTR_WDEV]) {
5946 wi->wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
5947 wi->wdev_id_set = 1;
5948 }
5949
5950 if (tb[NL80211_ATTR_MAC])
5951 os_memcpy(wi->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
5952 ETH_ALEN);
5953
5954 return NL_SKIP;
5955}
5956
5957
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005958static int wpa_driver_nl80211_if_add(void *priv, enum wpa_driver_if_type type,
5959 const char *ifname, const u8 *addr,
5960 void *bss_ctx, void **drv_priv,
5961 char *force_ifname, u8 *if_addr,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005962 const char *bridge, int use_existing,
5963 int setup_ap)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005964{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005965 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005966 struct i802_bss *bss = priv;
5967 struct wpa_driver_nl80211_data *drv = bss->drv;
5968 int ifidx;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005969 int added = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005970
5971 if (addr)
5972 os_memcpy(if_addr, addr, ETH_ALEN);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005973 nlmode = wpa_driver_nl80211_if_type(type);
5974 if (nlmode == NL80211_IFTYPE_P2P_DEVICE) {
5975 struct wdev_info p2pdev_info;
5976
5977 os_memset(&p2pdev_info, 0, sizeof(p2pdev_info));
5978 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
5979 0, nl80211_wdev_handler,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005980 &p2pdev_info, use_existing);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005981 if (!p2pdev_info.wdev_id_set || ifidx != 0) {
5982 wpa_printf(MSG_ERROR, "nl80211: Failed to create a P2P Device interface %s",
5983 ifname);
5984 return -1;
5985 }
5986
5987 drv->global->if_add_wdevid = p2pdev_info.wdev_id;
5988 drv->global->if_add_wdevid_set = p2pdev_info.wdev_id_set;
5989 if (!is_zero_ether_addr(p2pdev_info.macaddr))
5990 os_memcpy(if_addr, p2pdev_info.macaddr, ETH_ALEN);
5991 wpa_printf(MSG_DEBUG, "nl80211: New P2P Device interface %s (0x%llx) created",
5992 ifname,
5993 (long long unsigned int) p2pdev_info.wdev_id);
5994 } else {
5995 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005996 0, NULL, NULL, use_existing);
5997 if (use_existing && ifidx == -ENFILE) {
5998 added = 0;
5999 ifidx = if_nametoindex(ifname);
6000 } else if (ifidx < 0) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006001 return -1;
6002 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006003 }
6004
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006005 if (!addr) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07006006 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006007 os_memcpy(if_addr, bss->addr, ETH_ALEN);
6008 else if (linux_get_ifhwaddr(drv->global->ioctl_sock,
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07006009 ifname, if_addr) < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006010 if (added)
6011 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006012 return -1;
6013 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006014 }
6015
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006016 if (!addr &&
6017 (type == WPA_IF_P2P_CLIENT || type == WPA_IF_P2P_GROUP ||
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07006018 type == WPA_IF_P2P_GO || type == WPA_IF_MESH ||
6019 type == WPA_IF_STATION)) {
6020 /* Enforce unique address */
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006021 u8 new_addr[ETH_ALEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006022
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006023 if (linux_get_ifhwaddr(drv->global->ioctl_sock, ifname,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006024 new_addr) < 0) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07006025 if (added)
6026 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006027 return -1;
6028 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006029 if (nl80211_addr_in_use(drv->global, new_addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006030 wpa_printf(MSG_DEBUG, "nl80211: Allocate new address "
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07006031 "for interface %s type %d", ifname, type);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006032 if (nl80211_vif_addr(drv, new_addr) < 0) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07006033 if (added)
6034 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006035 return -1;
6036 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006037 if (linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006038 new_addr) < 0) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07006039 if (added)
6040 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006041 return -1;
6042 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006043 }
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07006044 os_memcpy(if_addr, new_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006045 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006046
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006047 if (type == WPA_IF_AP_BSS && setup_ap) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006048 struct i802_bss *new_bss = os_zalloc(sizeof(*new_bss));
6049 if (new_bss == NULL) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006050 if (added)
6051 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006052 return -1;
6053 }
6054
6055 if (bridge &&
6056 i802_check_bridge(drv, new_bss, bridge, ifname) < 0) {
6057 wpa_printf(MSG_ERROR, "nl80211: Failed to add the new "
6058 "interface %s to a bridge %s",
6059 ifname, bridge);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006060 if (added)
6061 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006062 os_free(new_bss);
6063 return -1;
6064 }
6065
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006066 if (linux_set_iface_flags(drv->global->ioctl_sock, ifname, 1))
6067 {
Dmitry Shmidt71757432014-06-02 13:50:35 -07006068 if (added)
6069 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006070 os_free(new_bss);
6071 return -1;
6072 }
6073 os_strlcpy(new_bss->ifname, ifname, IFNAMSIZ);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006074 os_memcpy(new_bss->addr, if_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006075 new_bss->ifindex = ifidx;
6076 new_bss->drv = drv;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006077 new_bss->next = drv->first_bss->next;
6078 new_bss->freq = drv->first_bss->freq;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006079 new_bss->ctx = bss_ctx;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006080 new_bss->added_if = added;
6081 drv->first_bss->next = new_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006082 if (drv_priv)
6083 *drv_priv = new_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006084 nl80211_init_bss(new_bss);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006085
6086 /* Subscribe management frames for this WPA_IF_AP_BSS */
6087 if (nl80211_setup_ap(new_bss))
6088 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006089 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006090
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006091 if (drv->global)
6092 drv->global->if_add_ifindex = ifidx;
6093
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006094 /*
6095 * Some virtual interfaces need to process EAPOL packets and events on
6096 * the parent interface. This is used mainly with hostapd.
6097 */
6098 if (ifidx > 0 &&
6099 (drv->hostapd ||
6100 nlmode == NL80211_IFTYPE_AP_VLAN ||
6101 nlmode == NL80211_IFTYPE_WDS ||
6102 nlmode == NL80211_IFTYPE_MONITOR))
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006103 add_ifidx(drv, ifidx);
6104
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006105 return 0;
6106}
6107
6108
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006109static int wpa_driver_nl80211_if_remove(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006110 enum wpa_driver_if_type type,
6111 const char *ifname)
6112{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006113 struct wpa_driver_nl80211_data *drv = bss->drv;
6114 int ifindex = if_nametoindex(ifname);
6115
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006116 wpa_printf(MSG_DEBUG, "nl80211: %s(type=%d ifname=%s) ifindex=%d added_if=%d",
6117 __func__, type, ifname, ifindex, bss->added_if);
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08006118 if (ifindex > 0 && (bss->added_if || bss->ifindex != ifindex))
Dmitry Shmidt051af732013-10-22 13:52:46 -07006119 nl80211_remove_iface(drv, ifindex);
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07006120 else if (ifindex > 0 && !bss->added_if) {
6121 struct wpa_driver_nl80211_data *drv2;
6122 dl_list_for_each(drv2, &drv->global->interfaces,
6123 struct wpa_driver_nl80211_data, list)
6124 del_ifidx(drv2, ifindex);
6125 }
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006126
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006127 if (type != WPA_IF_AP_BSS)
6128 return 0;
6129
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006130 if (bss->added_if_into_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006131 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
6132 bss->ifname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006133 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
6134 "interface %s from bridge %s: %s",
6135 bss->ifname, bss->brname, strerror(errno));
6136 }
6137 if (bss->added_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006138 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006139 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
6140 "bridge %s: %s",
6141 bss->brname, strerror(errno));
6142 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006143
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006144 if (bss != drv->first_bss) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006145 struct i802_bss *tbss;
6146
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006147 wpa_printf(MSG_DEBUG, "nl80211: Not the first BSS - remove it");
6148 for (tbss = drv->first_bss; tbss; tbss = tbss->next) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006149 if (tbss->next == bss) {
6150 tbss->next = bss->next;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006151 /* Unsubscribe management frames */
6152 nl80211_teardown_ap(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006153 nl80211_destroy_bss(bss);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006154 if (!bss->added_if)
6155 i802_set_iface_flags(bss, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006156 os_free(bss);
6157 bss = NULL;
6158 break;
6159 }
6160 }
6161 if (bss)
6162 wpa_printf(MSG_INFO, "nl80211: %s - could not find "
6163 "BSS %p in the list", __func__, bss);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006164 } else {
6165 wpa_printf(MSG_DEBUG, "nl80211: First BSS - reassign context");
6166 nl80211_teardown_ap(bss);
6167 if (!bss->added_if && !drv->first_bss->next)
6168 wpa_driver_nl80211_del_beacon(drv);
6169 nl80211_destroy_bss(bss);
6170 if (!bss->added_if)
6171 i802_set_iface_flags(bss, 0);
6172 if (drv->first_bss->next) {
6173 drv->first_bss = drv->first_bss->next;
6174 drv->ctx = drv->first_bss->ctx;
6175 os_free(bss);
6176 } else {
6177 wpa_printf(MSG_DEBUG, "nl80211: No second BSS to reassign context to");
6178 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006179 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006180
6181 return 0;
6182}
6183
6184
6185static int cookie_handler(struct nl_msg *msg, void *arg)
6186{
6187 struct nlattr *tb[NL80211_ATTR_MAX + 1];
6188 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6189 u64 *cookie = arg;
6190 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6191 genlmsg_attrlen(gnlh, 0), NULL);
6192 if (tb[NL80211_ATTR_COOKIE])
6193 *cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
6194 return NL_SKIP;
6195}
6196
6197
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006198static int nl80211_send_frame_cmd(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006199 unsigned int freq, unsigned int wait,
6200 const u8 *buf, size_t buf_len,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006201 u64 *cookie_out, int no_cck, int no_ack,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006202 int offchanok, const u16 *csa_offs,
6203 size_t csa_offs_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006204{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006205 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006206 struct nl_msg *msg;
6207 u64 cookie;
6208 int ret = -1;
6209
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006210 wpa_printf(MSG_MSGDUMP, "nl80211: CMD_FRAME freq=%u wait=%u no_cck=%d "
Dmitry Shmidt04949592012-07-19 12:16:46 -07006211 "no_ack=%d offchanok=%d",
6212 freq, wait, no_cck, no_ack, offchanok);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006213 wpa_hexdump(MSG_MSGDUMP, "CMD_FRAME", buf, buf_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006214
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006215 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_FRAME)) ||
6216 (freq && nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
6217 (wait && nla_put_u32(msg, NL80211_ATTR_DURATION, wait)) ||
6218 (offchanok && ((drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
6219 drv->test_use_roc_tx) &&
6220 nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK)) ||
6221 (no_cck && nla_put_flag(msg, NL80211_ATTR_TX_NO_CCK_RATE)) ||
6222 (no_ack && nla_put_flag(msg, NL80211_ATTR_DONT_WAIT_FOR_ACK)) ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006223 (csa_offs && nla_put(msg, NL80211_ATTR_CSA_C_OFFSETS_TX,
6224 csa_offs_len * sizeof(u16), csa_offs)) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006225 nla_put(msg, NL80211_ATTR_FRAME, buf_len, buf))
6226 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006227
6228 cookie = 0;
6229 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
6230 msg = NULL;
6231 if (ret) {
6232 wpa_printf(MSG_DEBUG, "nl80211: Frame command failed: ret=%d "
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07006233 "(%s) (freq=%u wait=%u)", ret, strerror(-ret),
6234 freq, wait);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006235 } else {
6236 wpa_printf(MSG_MSGDUMP, "nl80211: Frame TX command accepted%s; "
6237 "cookie 0x%llx", no_ack ? " (no ACK)" : "",
6238 (long long unsigned int) cookie);
6239
6240 if (cookie_out)
6241 *cookie_out = no_ack ? (u64) -1 : cookie;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006242
6243 if (drv->num_send_action_cookies == MAX_SEND_ACTION_COOKIES) {
6244 wpa_printf(MSG_DEBUG,
6245 "nl80211: Drop oldest pending send action cookie 0x%llx",
6246 (long long unsigned int)
6247 drv->send_action_cookies[0]);
6248 os_memmove(&drv->send_action_cookies[0],
6249 &drv->send_action_cookies[1],
6250 (MAX_SEND_ACTION_COOKIES - 1) *
6251 sizeof(u64));
6252 drv->num_send_action_cookies--;
6253 }
6254 drv->send_action_cookies[drv->num_send_action_cookies] = cookie;
6255 drv->num_send_action_cookies++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006256 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006257
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006258fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006259 nlmsg_free(msg);
6260 return ret;
6261}
6262
6263
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006264static int wpa_driver_nl80211_send_action(struct i802_bss *bss,
6265 unsigned int freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006266 unsigned int wait_time,
6267 const u8 *dst, const u8 *src,
6268 const u8 *bssid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006269 const u8 *data, size_t data_len,
6270 int no_cck)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006271{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006272 struct wpa_driver_nl80211_data *drv = bss->drv;
6273 int ret = -1;
6274 u8 *buf;
6275 struct ieee80211_hdr *hdr;
6276
6277 wpa_printf(MSG_DEBUG, "nl80211: Send Action frame (ifindex=%d, "
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006278 "freq=%u MHz wait=%d ms no_cck=%d)",
6279 drv->ifindex, freq, wait_time, no_cck);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006280
6281 buf = os_zalloc(24 + data_len);
6282 if (buf == NULL)
6283 return ret;
6284 os_memcpy(buf + 24, data, data_len);
6285 hdr = (struct ieee80211_hdr *) buf;
6286 hdr->frame_control =
6287 IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_ACTION);
6288 os_memcpy(hdr->addr1, dst, ETH_ALEN);
6289 os_memcpy(hdr->addr2, src, ETH_ALEN);
6290 os_memcpy(hdr->addr3, bssid, ETH_ALEN);
6291
Dmitry Shmidt56052862013-10-04 10:23:25 -07006292 if (is_ap_interface(drv->nlmode) &&
6293 (!(drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
6294 (int) freq == bss->freq || drv->device_ap_sme ||
6295 !drv->use_monitor))
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006296 ret = wpa_driver_nl80211_send_mlme(bss, buf, 24 + data_len,
6297 0, freq, no_cck, 1,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006298 wait_time, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006299 else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006300 ret = nl80211_send_frame_cmd(bss, freq, wait_time, buf,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006301 24 + data_len,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006302 &drv->send_action_cookie,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006303 no_cck, 0, 1, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006304
6305 os_free(buf);
6306 return ret;
6307}
6308
6309
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006310static void nl80211_frame_wait_cancel(struct i802_bss *bss, u64 cookie)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006311{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006312 struct wpa_driver_nl80211_data *drv = bss->drv;
6313 struct nl_msg *msg;
6314 int ret;
6315
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08006316 wpa_printf(MSG_DEBUG, "nl80211: Cancel TX frame wait: cookie=0x%llx",
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006317 (long long unsigned int) cookie);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006318 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_FRAME_WAIT_CANCEL)) ||
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006319 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006320 nlmsg_free(msg);
6321 return;
6322 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006323
6324 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006325 if (ret)
6326 wpa_printf(MSG_DEBUG, "nl80211: wait cancel failed: ret=%d "
6327 "(%s)", ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006328}
6329
6330
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006331static void wpa_driver_nl80211_send_action_cancel_wait(void *priv)
6332{
6333 struct i802_bss *bss = priv;
6334 struct wpa_driver_nl80211_data *drv = bss->drv;
6335 unsigned int i;
6336 u64 cookie;
6337
6338 /* Cancel the last pending TX cookie */
6339 nl80211_frame_wait_cancel(bss, drv->send_action_cookie);
6340
6341 /*
6342 * Cancel the other pending TX cookies, if any. This is needed since
6343 * the driver may keep a list of all pending offchannel TX operations
6344 * and free up the radio only once they have expired or cancelled.
6345 */
6346 for (i = drv->num_send_action_cookies; i > 0; i--) {
6347 cookie = drv->send_action_cookies[i - 1];
6348 if (cookie != drv->send_action_cookie)
6349 nl80211_frame_wait_cancel(bss, cookie);
6350 }
6351 drv->num_send_action_cookies = 0;
6352}
6353
6354
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006355static int wpa_driver_nl80211_remain_on_channel(void *priv, unsigned int freq,
6356 unsigned int duration)
6357{
6358 struct i802_bss *bss = priv;
6359 struct wpa_driver_nl80211_data *drv = bss->drv;
6360 struct nl_msg *msg;
6361 int ret;
6362 u64 cookie;
6363
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006364 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_REMAIN_ON_CHANNEL)) ||
6365 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
6366 nla_put_u32(msg, NL80211_ATTR_DURATION, duration)) {
6367 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006368 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006369 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006370
6371 cookie = 0;
6372 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
6373 if (ret == 0) {
6374 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel cookie "
6375 "0x%llx for freq=%u MHz duration=%u",
6376 (long long unsigned int) cookie, freq, duration);
6377 drv->remain_on_chan_cookie = cookie;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006378 drv->pending_remain_on_chan = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006379 return 0;
6380 }
6381 wpa_printf(MSG_DEBUG, "nl80211: Failed to request remain-on-channel "
6382 "(freq=%d duration=%u): %d (%s)",
6383 freq, duration, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006384 return -1;
6385}
6386
6387
6388static int wpa_driver_nl80211_cancel_remain_on_channel(void *priv)
6389{
6390 struct i802_bss *bss = priv;
6391 struct wpa_driver_nl80211_data *drv = bss->drv;
6392 struct nl_msg *msg;
6393 int ret;
6394
6395 if (!drv->pending_remain_on_chan) {
6396 wpa_printf(MSG_DEBUG, "nl80211: No pending remain-on-channel "
6397 "to cancel");
6398 return -1;
6399 }
6400
6401 wpa_printf(MSG_DEBUG, "nl80211: Cancel remain-on-channel with cookie "
6402 "0x%llx",
6403 (long long unsigned int) drv->remain_on_chan_cookie);
6404
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006405 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL);
6406 if (!msg ||
6407 nla_put_u64(msg, NL80211_ATTR_COOKIE, drv->remain_on_chan_cookie)) {
6408 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006409 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006410 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006411
6412 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6413 if (ret == 0)
6414 return 0;
6415 wpa_printf(MSG_DEBUG, "nl80211: Failed to cancel remain-on-channel: "
6416 "%d (%s)", ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006417 return -1;
6418}
6419
6420
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006421static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss, int report)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006422{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006423 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07006424
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006425 if (!report) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006426 if (bss->nl_preq && drv->device_ap_sme &&
Dmitry Shmidt03658832014-08-13 11:03:49 -07006427 is_ap_interface(drv->nlmode) && !bss->in_deinit &&
6428 !bss->static_ap) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006429 /*
6430 * Do not disable Probe Request reporting that was
6431 * enabled in nl80211_setup_ap().
6432 */
6433 wpa_printf(MSG_DEBUG, "nl80211: Skip disabling of "
6434 "Probe Request reporting nl_preq=%p while "
6435 "in AP mode", bss->nl_preq);
6436 } else if (bss->nl_preq) {
6437 wpa_printf(MSG_DEBUG, "nl80211: Disable Probe Request "
6438 "reporting nl_preq=%p", bss->nl_preq);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006439 nl80211_destroy_eloop_handle(&bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006440 }
6441 return 0;
6442 }
6443
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006444 if (bss->nl_preq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006445 wpa_printf(MSG_DEBUG, "nl80211: Probe Request reporting "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006446 "already on! nl_preq=%p", bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006447 return 0;
6448 }
6449
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006450 bss->nl_preq = nl_create_handle(drv->global->nl_cb, "preq");
6451 if (bss->nl_preq == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006452 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006453 wpa_printf(MSG_DEBUG, "nl80211: Enable Probe Request "
6454 "reporting nl_preq=%p", bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006455
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006456 if (nl80211_register_frame(bss, bss->nl_preq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006457 (WLAN_FC_TYPE_MGMT << 2) |
6458 (WLAN_FC_STYPE_PROBE_REQ << 4),
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006459 NULL, 0) < 0)
6460 goto out_err;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07006461
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006462 nl80211_register_eloop_read(&bss->nl_preq,
6463 wpa_driver_nl80211_event_receive,
6464 bss->nl_cb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006465
6466 return 0;
6467
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006468 out_err:
6469 nl_destroy_handles(&bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006470 return -1;
6471}
6472
6473
6474static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
6475 int ifindex, int disabled)
6476{
6477 struct nl_msg *msg;
6478 struct nlattr *bands, *band;
6479 int ret;
6480
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006481 wpa_printf(MSG_DEBUG,
6482 "nl80211: NL80211_CMD_SET_TX_BITRATE_MASK (ifindex=%d %s)",
6483 ifindex, disabled ? "NL80211_TXRATE_LEGACY=OFDM-only" :
6484 "no NL80211_TXRATE_LEGACY constraint");
6485
6486 msg = nl80211_ifindex_msg(drv, ifindex, 0,
6487 NL80211_CMD_SET_TX_BITRATE_MASK);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006488 if (!msg)
6489 return -1;
6490
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006491 bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES);
6492 if (!bands)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006493 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006494
6495 /*
6496 * Disable 2 GHz rates 1, 2, 5.5, 11 Mbps by masking out everything
6497 * else apart from 6, 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS
6498 * rates. All 5 GHz rates are left enabled.
6499 */
6500 band = nla_nest_start(msg, NL80211_BAND_2GHZ);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006501 if (!band ||
6502 (disabled && nla_put(msg, NL80211_TXRATE_LEGACY, 8,
6503 "\x0c\x12\x18\x24\x30\x48\x60\x6c")))
6504 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006505 nla_nest_end(msg, band);
6506
6507 nla_nest_end(msg, bands);
6508
6509 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006510 if (ret) {
6511 wpa_printf(MSG_DEBUG, "nl80211: Set TX rates failed: ret=%d "
6512 "(%s)", ret, strerror(-ret));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006513 } else
6514 drv->disabled_11b_rates = disabled;
6515
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006516 return ret;
6517
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006518fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006519 nlmsg_free(msg);
6520 return -1;
6521}
6522
6523
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006524static int wpa_driver_nl80211_deinit_ap(void *priv)
6525{
6526 struct i802_bss *bss = priv;
6527 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006528 if (!is_ap_interface(drv->nlmode))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006529 return -1;
6530 wpa_driver_nl80211_del_beacon(drv);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006531 bss->beacon_set = 0;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006532
6533 /*
6534 * If the P2P GO interface was dynamically added, then it is
6535 * possible that the interface change to station is not possible.
6536 */
6537 if (drv->nlmode == NL80211_IFTYPE_P2P_GO && bss->if_dynamic)
6538 return 0;
6539
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006540 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006541}
6542
6543
Dmitry Shmidtea69e842013-05-13 14:52:28 -07006544static int wpa_driver_nl80211_stop_ap(void *priv)
6545{
6546 struct i802_bss *bss = priv;
6547 struct wpa_driver_nl80211_data *drv = bss->drv;
6548 if (!is_ap_interface(drv->nlmode))
6549 return -1;
6550 wpa_driver_nl80211_del_beacon(drv);
6551 bss->beacon_set = 0;
6552 return 0;
6553}
6554
6555
Dmitry Shmidt04949592012-07-19 12:16:46 -07006556static int wpa_driver_nl80211_deinit_p2p_cli(void *priv)
6557{
6558 struct i802_bss *bss = priv;
6559 struct wpa_driver_nl80211_data *drv = bss->drv;
6560 if (drv->nlmode != NL80211_IFTYPE_P2P_CLIENT)
6561 return -1;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006562
6563 /*
6564 * If the P2P Client interface was dynamically added, then it is
6565 * possible that the interface change to station is not possible.
6566 */
6567 if (bss->if_dynamic)
6568 return 0;
6569
Dmitry Shmidt04949592012-07-19 12:16:46 -07006570 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
6571}
6572
6573
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006574static void wpa_driver_nl80211_resume(void *priv)
6575{
6576 struct i802_bss *bss = priv;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006577 enum nl80211_iftype nlmode = nl80211_get_ifmode(bss);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006578
6579 if (i802_set_iface_flags(bss, 1))
6580 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface up on resume event");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006581
6582 if (is_p2p_net_interface(nlmode))
6583 nl80211_disable_11b_rates(bss->drv, bss->drv->ifindex, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006584}
6585
6586
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006587static int nl80211_signal_monitor(void *priv, int threshold, int hysteresis)
6588{
6589 struct i802_bss *bss = priv;
6590 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006591 struct nl_msg *msg;
6592 struct nlattr *cqm;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006593
6594 wpa_printf(MSG_DEBUG, "nl80211: Signal monitor threshold=%d "
6595 "hysteresis=%d", threshold, hysteresis);
6596
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006597 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_CQM)) ||
6598 !(cqm = nla_nest_start(msg, NL80211_ATTR_CQM)) ||
6599 nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THOLD, threshold) ||
6600 nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_HYST, hysteresis)) {
6601 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006602 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006603 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006604 nla_nest_end(msg, cqm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006605
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006606 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006607}
6608
6609
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006610static int get_channel_width(struct nl_msg *msg, void *arg)
6611{
6612 struct nlattr *tb[NL80211_ATTR_MAX + 1];
6613 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6614 struct wpa_signal_info *sig_change = arg;
6615
6616 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6617 genlmsg_attrlen(gnlh, 0), NULL);
6618
6619 sig_change->center_frq1 = -1;
6620 sig_change->center_frq2 = -1;
6621 sig_change->chanwidth = CHAN_WIDTH_UNKNOWN;
6622
6623 if (tb[NL80211_ATTR_CHANNEL_WIDTH]) {
6624 sig_change->chanwidth = convert2width(
6625 nla_get_u32(tb[NL80211_ATTR_CHANNEL_WIDTH]));
6626 if (tb[NL80211_ATTR_CENTER_FREQ1])
6627 sig_change->center_frq1 =
6628 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]);
6629 if (tb[NL80211_ATTR_CENTER_FREQ2])
6630 sig_change->center_frq2 =
6631 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]);
6632 }
6633
6634 return NL_SKIP;
6635}
6636
6637
6638static int nl80211_get_channel_width(struct wpa_driver_nl80211_data *drv,
6639 struct wpa_signal_info *sig)
6640{
6641 struct nl_msg *msg;
6642
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006643 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_GET_INTERFACE);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006644 return send_and_recv_msgs(drv, msg, get_channel_width, sig);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006645}
6646
6647
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006648static int nl80211_signal_poll(void *priv, struct wpa_signal_info *si)
6649{
6650 struct i802_bss *bss = priv;
6651 struct wpa_driver_nl80211_data *drv = bss->drv;
6652 int res;
6653
6654 os_memset(si, 0, sizeof(*si));
6655 res = nl80211_get_link_signal(drv, si);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006656 if (res) {
6657 if (drv->nlmode != NL80211_IFTYPE_ADHOC &&
6658 drv->nlmode != NL80211_IFTYPE_MESH_POINT)
6659 return res;
6660 si->current_signal = 0;
6661 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006662
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006663 res = nl80211_get_channel_width(drv, si);
6664 if (res != 0)
6665 return res;
6666
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006667 return nl80211_get_link_noise(drv, si);
6668}
6669
6670
6671static int nl80211_send_frame(void *priv, const u8 *data, size_t data_len,
6672 int encrypt)
6673{
6674 struct i802_bss *bss = priv;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006675 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt, 0,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006676 0, 0, 0, 0, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006677}
6678
6679
6680static int nl80211_set_param(void *priv, const char *param)
6681{
6682 wpa_printf(MSG_DEBUG, "nl80211: driver param='%s'", param);
6683 if (param == NULL)
6684 return 0;
6685
6686#ifdef CONFIG_P2P
6687 if (os_strstr(param, "use_p2p_group_interface=1")) {
6688 struct i802_bss *bss = priv;
6689 struct wpa_driver_nl80211_data *drv = bss->drv;
6690
6691 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
6692 "interface");
6693 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
6694 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
6695 }
6696#endif /* CONFIG_P2P */
6697
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006698 if (os_strstr(param, "use_monitor=1")) {
6699 struct i802_bss *bss = priv;
6700 struct wpa_driver_nl80211_data *drv = bss->drv;
6701 drv->use_monitor = 1;
6702 }
6703
6704 if (os_strstr(param, "force_connect_cmd=1")) {
6705 struct i802_bss *bss = priv;
6706 struct wpa_driver_nl80211_data *drv = bss->drv;
6707 drv->capa.flags &= ~WPA_DRIVER_FLAGS_SME;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006708 drv->force_connect_cmd = 1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006709 }
6710
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08006711 if (os_strstr(param, "no_offchannel_tx=1")) {
6712 struct i802_bss *bss = priv;
6713 struct wpa_driver_nl80211_data *drv = bss->drv;
6714 drv->capa.flags &= ~WPA_DRIVER_FLAGS_OFFCHANNEL_TX;
6715 drv->test_use_roc_tx = 1;
6716 }
6717
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006718 return 0;
6719}
6720
6721
6722static void * nl80211_global_init(void)
6723{
6724 struct nl80211_global *global;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006725 struct netlink_config *cfg;
6726
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006727 global = os_zalloc(sizeof(*global));
6728 if (global == NULL)
6729 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006730 global->ioctl_sock = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006731 dl_list_init(&global->interfaces);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006732 global->if_add_ifindex = -1;
6733
6734 cfg = os_zalloc(sizeof(*cfg));
6735 if (cfg == NULL)
6736 goto err;
6737
6738 cfg->ctx = global;
6739 cfg->newlink_cb = wpa_driver_nl80211_event_rtm_newlink;
6740 cfg->dellink_cb = wpa_driver_nl80211_event_rtm_dellink;
6741 global->netlink = netlink_init(cfg);
6742 if (global->netlink == NULL) {
6743 os_free(cfg);
6744 goto err;
6745 }
6746
6747 if (wpa_driver_nl80211_init_nl_global(global) < 0)
6748 goto err;
6749
6750 global->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
6751 if (global->ioctl_sock < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006752 wpa_printf(MSG_ERROR, "nl80211: socket(PF_INET,SOCK_DGRAM) failed: %s",
6753 strerror(errno));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006754 goto err;
6755 }
6756
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006757 return global;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006758
6759err:
6760 nl80211_global_deinit(global);
6761 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006762}
6763
6764
6765static void nl80211_global_deinit(void *priv)
6766{
6767 struct nl80211_global *global = priv;
6768 if (global == NULL)
6769 return;
6770 if (!dl_list_empty(&global->interfaces)) {
6771 wpa_printf(MSG_ERROR, "nl80211: %u interface(s) remain at "
6772 "nl80211_global_deinit",
6773 dl_list_len(&global->interfaces));
6774 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006775
6776 if (global->netlink)
6777 netlink_deinit(global->netlink);
6778
6779 nl_destroy_handles(&global->nl);
6780
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006781 if (global->nl_event)
6782 nl80211_destroy_eloop_handle(&global->nl_event);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006783
6784 nl_cb_put(global->nl_cb);
6785
6786 if (global->ioctl_sock >= 0)
6787 close(global->ioctl_sock);
6788
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006789 os_free(global);
6790}
6791
6792
6793static const char * nl80211_get_radio_name(void *priv)
6794{
6795 struct i802_bss *bss = priv;
6796 struct wpa_driver_nl80211_data *drv = bss->drv;
6797 return drv->phyname;
6798}
6799
6800
Jouni Malinen75ecf522011-06-27 15:19:46 -07006801static int nl80211_pmkid(struct i802_bss *bss, int cmd, const u8 *bssid,
6802 const u8 *pmkid)
6803{
6804 struct nl_msg *msg;
6805
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006806 if (!(msg = nl80211_bss_msg(bss, 0, cmd)) ||
6807 (pmkid && nla_put(msg, NL80211_ATTR_PMKID, 16, pmkid)) ||
6808 (bssid && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))) {
6809 nlmsg_free(msg);
6810 return -ENOBUFS;
6811 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07006812
6813 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
Jouni Malinen75ecf522011-06-27 15:19:46 -07006814}
6815
6816
6817static int nl80211_add_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
6818{
6819 struct i802_bss *bss = priv;
6820 wpa_printf(MSG_DEBUG, "nl80211: Add PMKID for " MACSTR, MAC2STR(bssid));
6821 return nl80211_pmkid(bss, NL80211_CMD_SET_PMKSA, bssid, pmkid);
6822}
6823
6824
6825static int nl80211_remove_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
6826{
6827 struct i802_bss *bss = priv;
6828 wpa_printf(MSG_DEBUG, "nl80211: Delete PMKID for " MACSTR,
6829 MAC2STR(bssid));
6830 return nl80211_pmkid(bss, NL80211_CMD_DEL_PMKSA, bssid, pmkid);
6831}
6832
6833
6834static int nl80211_flush_pmkid(void *priv)
6835{
6836 struct i802_bss *bss = priv;
6837 wpa_printf(MSG_DEBUG, "nl80211: Flush PMKIDs");
6838 return nl80211_pmkid(bss, NL80211_CMD_FLUSH_PMKSA, NULL, NULL);
6839}
6840
6841
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006842static void clean_survey_results(struct survey_results *survey_results)
6843{
6844 struct freq_survey *survey, *tmp;
6845
6846 if (dl_list_empty(&survey_results->survey_list))
6847 return;
6848
6849 dl_list_for_each_safe(survey, tmp, &survey_results->survey_list,
6850 struct freq_survey, list) {
6851 dl_list_del(&survey->list);
6852 os_free(survey);
6853 }
6854}
6855
6856
6857static void add_survey(struct nlattr **sinfo, u32 ifidx,
6858 struct dl_list *survey_list)
6859{
6860 struct freq_survey *survey;
6861
6862 survey = os_zalloc(sizeof(struct freq_survey));
6863 if (!survey)
6864 return;
6865
6866 survey->ifidx = ifidx;
6867 survey->freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
6868 survey->filled = 0;
6869
6870 if (sinfo[NL80211_SURVEY_INFO_NOISE]) {
6871 survey->nf = (int8_t)
6872 nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
6873 survey->filled |= SURVEY_HAS_NF;
6874 }
6875
6876 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]) {
6877 survey->channel_time =
6878 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]);
6879 survey->filled |= SURVEY_HAS_CHAN_TIME;
6880 }
6881
6882 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]) {
6883 survey->channel_time_busy =
6884 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]);
6885 survey->filled |= SURVEY_HAS_CHAN_TIME_BUSY;
6886 }
6887
6888 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]) {
6889 survey->channel_time_rx =
6890 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]);
6891 survey->filled |= SURVEY_HAS_CHAN_TIME_RX;
6892 }
6893
6894 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]) {
6895 survey->channel_time_tx =
6896 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]);
6897 survey->filled |= SURVEY_HAS_CHAN_TIME_TX;
6898 }
6899
6900 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)",
6901 survey->freq,
6902 survey->nf,
6903 (unsigned long int) survey->channel_time,
6904 (unsigned long int) survey->channel_time_busy,
6905 (unsigned long int) survey->channel_time_tx,
6906 (unsigned long int) survey->channel_time_rx,
6907 survey->filled);
6908
6909 dl_list_add_tail(survey_list, &survey->list);
6910}
6911
6912
6913static int check_survey_ok(struct nlattr **sinfo, u32 surveyed_freq,
6914 unsigned int freq_filter)
6915{
6916 if (!freq_filter)
6917 return 1;
6918
6919 return freq_filter == surveyed_freq;
6920}
6921
6922
6923static int survey_handler(struct nl_msg *msg, void *arg)
6924{
6925 struct nlattr *tb[NL80211_ATTR_MAX + 1];
6926 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6927 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
6928 struct survey_results *survey_results;
6929 u32 surveyed_freq = 0;
6930 u32 ifidx;
6931
6932 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
6933 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
6934 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
6935 };
6936
6937 survey_results = (struct survey_results *) arg;
6938
6939 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6940 genlmsg_attrlen(gnlh, 0), NULL);
6941
Dmitry Shmidt97672262014-02-03 13:02:54 -08006942 if (!tb[NL80211_ATTR_IFINDEX])
6943 return NL_SKIP;
6944
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006945 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
6946
6947 if (!tb[NL80211_ATTR_SURVEY_INFO])
6948 return NL_SKIP;
6949
6950 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
6951 tb[NL80211_ATTR_SURVEY_INFO],
6952 survey_policy))
6953 return NL_SKIP;
6954
6955 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY]) {
6956 wpa_printf(MSG_ERROR, "nl80211: Invalid survey data");
6957 return NL_SKIP;
6958 }
6959
6960 surveyed_freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
6961
6962 if (!check_survey_ok(sinfo, surveyed_freq,
6963 survey_results->freq_filter))
6964 return NL_SKIP;
6965
6966 if (survey_results->freq_filter &&
6967 survey_results->freq_filter != surveyed_freq) {
6968 wpa_printf(MSG_EXCESSIVE, "nl80211: Ignoring survey data for freq %d MHz",
6969 surveyed_freq);
6970 return NL_SKIP;
6971 }
6972
6973 add_survey(sinfo, ifidx, &survey_results->survey_list);
6974
6975 return NL_SKIP;
6976}
6977
6978
6979static int wpa_driver_nl80211_get_survey(void *priv, unsigned int freq)
6980{
6981 struct i802_bss *bss = priv;
6982 struct wpa_driver_nl80211_data *drv = bss->drv;
6983 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006984 int err;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006985 union wpa_event_data data;
6986 struct survey_results *survey_results;
6987
6988 os_memset(&data, 0, sizeof(data));
6989 survey_results = &data.survey_results;
6990
6991 dl_list_init(&survey_results->survey_list);
6992
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006993 msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006994 if (!msg)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006995 return -ENOBUFS;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006996
6997 if (freq)
6998 data.survey_results.freq_filter = freq;
6999
7000 do {
7001 wpa_printf(MSG_DEBUG, "nl80211: Fetch survey data");
7002 err = send_and_recv_msgs(drv, msg, survey_handler,
7003 survey_results);
7004 } while (err > 0);
7005
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007006 if (err)
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007007 wpa_printf(MSG_ERROR, "nl80211: Failed to process survey data");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007008 else
7009 wpa_supplicant_event(drv->ctx, EVENT_SURVEY, &data);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007010
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007011 clean_survey_results(survey_results);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007012 return err;
7013}
7014
7015
Dmitry Shmidt807291d2015-01-27 13:40:23 -08007016static void nl80211_set_rekey_info(void *priv, const u8 *kek, size_t kek_len,
7017 const u8 *kck, size_t kck_len,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007018 const u8 *replay_ctr)
7019{
7020 struct i802_bss *bss = priv;
7021 struct wpa_driver_nl80211_data *drv = bss->drv;
7022 struct nlattr *replay_nested;
7023 struct nl_msg *msg;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08007024 int ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007025
Dmitry Shmidtff787d52015-01-12 13:01:47 -08007026 if (!drv->set_rekey_offload)
7027 return;
7028
7029 wpa_printf(MSG_DEBUG, "nl80211: Set rekey offload");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007030 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_REKEY_OFFLOAD)) ||
7031 !(replay_nested = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA)) ||
Dmitry Shmidt807291d2015-01-27 13:40:23 -08007032 nla_put(msg, NL80211_REKEY_DATA_KEK, kek_len, kek) ||
7033 nla_put(msg, NL80211_REKEY_DATA_KCK, kck_len, kck) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007034 nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR, NL80211_REPLAY_CTR_LEN,
7035 replay_ctr)) {
7036 nl80211_nlmsg_clear(msg);
7037 nlmsg_free(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007038 return;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007039 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007040
7041 nla_nest_end(msg, replay_nested);
7042
Dmitry Shmidtff787d52015-01-12 13:01:47 -08007043 ret = send_and_recv_msgs(drv, msg, NULL, (void *) -1);
7044 if (ret == -EOPNOTSUPP) {
7045 wpa_printf(MSG_DEBUG,
7046 "nl80211: Driver does not support rekey offload");
7047 drv->set_rekey_offload = 0;
7048 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007049}
7050
7051
7052static void nl80211_send_null_frame(struct i802_bss *bss, const u8 *own_addr,
7053 const u8 *addr, int qos)
7054{
7055 /* send data frame to poll STA and check whether
7056 * this frame is ACKed */
7057 struct {
7058 struct ieee80211_hdr hdr;
7059 u16 qos_ctl;
7060 } STRUCT_PACKED nulldata;
7061 size_t size;
7062
7063 /* Send data frame to poll STA and check whether this frame is ACKed */
7064
7065 os_memset(&nulldata, 0, sizeof(nulldata));
7066
7067 if (qos) {
7068 nulldata.hdr.frame_control =
7069 IEEE80211_FC(WLAN_FC_TYPE_DATA,
7070 WLAN_FC_STYPE_QOS_NULL);
7071 size = sizeof(nulldata);
7072 } else {
7073 nulldata.hdr.frame_control =
7074 IEEE80211_FC(WLAN_FC_TYPE_DATA,
7075 WLAN_FC_STYPE_NULLFUNC);
7076 size = sizeof(struct ieee80211_hdr);
7077 }
7078
7079 nulldata.hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS);
7080 os_memcpy(nulldata.hdr.IEEE80211_DA_FROMDS, addr, ETH_ALEN);
7081 os_memcpy(nulldata.hdr.IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
7082 os_memcpy(nulldata.hdr.IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
7083
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007084 if (wpa_driver_nl80211_send_mlme(bss, (u8 *) &nulldata, size, 0, 0, 0,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007085 0, 0, NULL, 0) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007086 wpa_printf(MSG_DEBUG, "nl80211_send_null_frame: Failed to "
7087 "send poll frame");
7088}
7089
7090static void nl80211_poll_client(void *priv, const u8 *own_addr, const u8 *addr,
7091 int qos)
7092{
7093 struct i802_bss *bss = priv;
7094 struct wpa_driver_nl80211_data *drv = bss->drv;
7095 struct nl_msg *msg;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007096 int ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007097
7098 if (!drv->poll_command_supported) {
7099 nl80211_send_null_frame(bss, own_addr, addr, qos);
7100 return;
7101 }
7102
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007103 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_PROBE_CLIENT)) ||
7104 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
7105 nlmsg_free(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007106 return;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007107 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007108
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007109 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7110 if (ret < 0) {
7111 wpa_printf(MSG_DEBUG, "nl80211: Client probe request for "
7112 MACSTR " failed: ret=%d (%s)",
7113 MAC2STR(addr), ret, strerror(-ret));
7114 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007115}
7116
7117
7118static int nl80211_set_power_save(struct i802_bss *bss, int enabled)
7119{
7120 struct nl_msg *msg;
7121
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007122 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_POWER_SAVE)) ||
7123 nla_put_u32(msg, NL80211_ATTR_PS_STATE,
7124 enabled ? NL80211_PS_ENABLED : NL80211_PS_DISABLED)) {
7125 nlmsg_free(msg);
7126 return -ENOBUFS;
7127 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007128 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007129}
7130
7131
7132static int nl80211_set_p2p_powersave(void *priv, int legacy_ps, int opp_ps,
7133 int ctwindow)
7134{
7135 struct i802_bss *bss = priv;
7136
7137 wpa_printf(MSG_DEBUG, "nl80211: set_p2p_powersave (legacy_ps=%d "
7138 "opp_ps=%d ctwindow=%d)", legacy_ps, opp_ps, ctwindow);
7139
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08007140 if (opp_ps != -1 || ctwindow != -1) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007141#ifdef ANDROID_P2P
7142 wpa_driver_set_p2p_ps(priv, legacy_ps, opp_ps, ctwindow);
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08007143#else /* ANDROID_P2P */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007144 return -1; /* Not yet supported */
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08007145#endif /* ANDROID_P2P */
7146 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007147
7148 if (legacy_ps == -1)
7149 return 0;
7150 if (legacy_ps != 0 && legacy_ps != 1)
7151 return -1; /* Not yet supported */
7152
7153 return nl80211_set_power_save(bss, legacy_ps);
7154}
7155
7156
Dmitry Shmidt051af732013-10-22 13:52:46 -07007157static int nl80211_start_radar_detection(void *priv,
7158 struct hostapd_freq_params *freq)
Dmitry Shmidtea69e842013-05-13 14:52:28 -07007159{
7160 struct i802_bss *bss = priv;
7161 struct wpa_driver_nl80211_data *drv = bss->drv;
7162 struct nl_msg *msg;
7163 int ret;
7164
Dmitry Shmidt051af732013-10-22 13:52:46 -07007165 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)",
7166 freq->freq, freq->ht_enabled, freq->vht_enabled,
7167 freq->bandwidth, freq->center_freq1, freq->center_freq2);
7168
Dmitry Shmidtea69e842013-05-13 14:52:28 -07007169 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_RADAR)) {
7170 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support radar "
7171 "detection");
7172 return -1;
7173 }
7174
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007175 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_RADAR_DETECT)) ||
7176 nl80211_put_freq_params(msg, freq) < 0) {
7177 nlmsg_free(msg);
Dmitry Shmidtea69e842013-05-13 14:52:28 -07007178 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007179 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -07007180
7181 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7182 if (ret == 0)
7183 return 0;
7184 wpa_printf(MSG_DEBUG, "nl80211: Failed to start radar detection: "
7185 "%d (%s)", ret, strerror(-ret));
Dmitry Shmidtea69e842013-05-13 14:52:28 -07007186 return -1;
7187}
7188
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007189#ifdef CONFIG_TDLS
7190
7191static int nl80211_send_tdls_mgmt(void *priv, const u8 *dst, u8 action_code,
7192 u8 dialog_token, u16 status_code,
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07007193 u32 peer_capab, int initiator, const u8 *buf,
7194 size_t len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007195{
7196 struct i802_bss *bss = priv;
7197 struct wpa_driver_nl80211_data *drv = bss->drv;
7198 struct nl_msg *msg;
7199
7200 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
7201 return -EOPNOTSUPP;
7202
7203 if (!dst)
7204 return -EINVAL;
7205
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007206 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_TDLS_MGMT)) ||
7207 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
7208 nla_put_u8(msg, NL80211_ATTR_TDLS_ACTION, action_code) ||
7209 nla_put_u8(msg, NL80211_ATTR_TDLS_DIALOG_TOKEN, dialog_token) ||
7210 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status_code))
7211 goto fail;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07007212 if (peer_capab) {
7213 /*
7214 * The internal enum tdls_peer_capability definition is
7215 * currently identical with the nl80211 enum
7216 * nl80211_tdls_peer_capability, so no conversion is needed
7217 * here.
7218 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007219 if (nla_put_u32(msg, NL80211_ATTR_TDLS_PEER_CAPABILITY,
7220 peer_capab))
7221 goto fail;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07007222 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007223 if ((initiator &&
7224 nla_put_flag(msg, NL80211_ATTR_TDLS_INITIATOR)) ||
7225 nla_put(msg, NL80211_ATTR_IE, len, buf))
7226 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007227
7228 return send_and_recv_msgs(drv, msg, NULL, NULL);
7229
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007230fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007231 nlmsg_free(msg);
7232 return -ENOBUFS;
7233}
7234
7235
7236static int nl80211_tdls_oper(void *priv, enum tdls_oper oper, const u8 *peer)
7237{
7238 struct i802_bss *bss = priv;
7239 struct wpa_driver_nl80211_data *drv = bss->drv;
7240 struct nl_msg *msg;
7241 enum nl80211_tdls_operation nl80211_oper;
7242
7243 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
7244 return -EOPNOTSUPP;
7245
7246 switch (oper) {
7247 case TDLS_DISCOVERY_REQ:
7248 nl80211_oper = NL80211_TDLS_DISCOVERY_REQ;
7249 break;
7250 case TDLS_SETUP:
7251 nl80211_oper = NL80211_TDLS_SETUP;
7252 break;
7253 case TDLS_TEARDOWN:
7254 nl80211_oper = NL80211_TDLS_TEARDOWN;
7255 break;
7256 case TDLS_ENABLE_LINK:
7257 nl80211_oper = NL80211_TDLS_ENABLE_LINK;
7258 break;
7259 case TDLS_DISABLE_LINK:
7260 nl80211_oper = NL80211_TDLS_DISABLE_LINK;
7261 break;
7262 case TDLS_ENABLE:
7263 return 0;
7264 case TDLS_DISABLE:
7265 return 0;
7266 default:
7267 return -EINVAL;
7268 }
7269
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007270 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_TDLS_OPER)) ||
7271 nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, nl80211_oper) ||
7272 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer)) {
7273 nlmsg_free(msg);
7274 return -ENOBUFS;
7275 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007276
7277 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007278}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007279
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007280
7281static int
7282nl80211_tdls_enable_channel_switch(void *priv, const u8 *addr, u8 oper_class,
7283 const struct hostapd_freq_params *params)
7284{
7285 struct i802_bss *bss = priv;
7286 struct wpa_driver_nl80211_data *drv = bss->drv;
7287 struct nl_msg *msg;
7288 int ret = -ENOBUFS;
7289
7290 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT) ||
7291 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_CHANNEL_SWITCH))
7292 return -EOPNOTSUPP;
7293
7294 wpa_printf(MSG_DEBUG, "nl80211: Enable TDLS channel switch " MACSTR
7295 " oper_class=%u freq=%u",
7296 MAC2STR(addr), oper_class, params->freq);
7297 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_TDLS_CHANNEL_SWITCH);
7298 if (!msg ||
7299 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
7300 nla_put_u8(msg, NL80211_ATTR_OPER_CLASS, oper_class) ||
7301 (ret = nl80211_put_freq_params(msg, params))) {
7302 nlmsg_free(msg);
7303 wpa_printf(MSG_DEBUG, "nl80211: Could not build TDLS chan switch");
7304 return ret;
7305 }
7306
7307 return send_and_recv_msgs(drv, msg, NULL, NULL);
7308}
7309
7310
7311static int
7312nl80211_tdls_disable_channel_switch(void *priv, const u8 *addr)
7313{
7314 struct i802_bss *bss = priv;
7315 struct wpa_driver_nl80211_data *drv = bss->drv;
7316 struct nl_msg *msg;
7317
7318 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT) ||
7319 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_CHANNEL_SWITCH))
7320 return -EOPNOTSUPP;
7321
7322 wpa_printf(MSG_DEBUG, "nl80211: Disable TDLS channel switch " MACSTR,
7323 MAC2STR(addr));
7324 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_TDLS_CANCEL_CHANNEL_SWITCH);
7325 if (!msg ||
7326 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
7327 nlmsg_free(msg);
7328 wpa_printf(MSG_DEBUG,
7329 "nl80211: Could not build TDLS cancel chan switch");
7330 return -ENOBUFS;
7331 }
7332
7333 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007334}
7335
7336#endif /* CONFIG TDLS */
7337
7338
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007339static int driver_nl80211_set_key(const char *ifname, void *priv,
7340 enum wpa_alg alg, const u8 *addr,
7341 int key_idx, int set_tx,
7342 const u8 *seq, size_t seq_len,
7343 const u8 *key, size_t key_len)
7344{
7345 struct i802_bss *bss = priv;
7346 return wpa_driver_nl80211_set_key(ifname, bss, alg, addr, key_idx,
7347 set_tx, seq, seq_len, key, key_len);
7348}
7349
7350
7351static int driver_nl80211_scan2(void *priv,
7352 struct wpa_driver_scan_params *params)
7353{
7354 struct i802_bss *bss = priv;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007355#ifdef CONFIG_DRIVER_NL80211_QCA
7356 struct wpa_driver_nl80211_data *drv = bss->drv;
7357
7358 /*
7359 * Do a vendor specific scan if possible. If only_new_results is
7360 * set, do a normal scan since a kernel (cfg80211) BSS cache flush
7361 * cannot be achieved through a vendor scan. The below condition may
7362 * need to be modified if new scan flags are added in the future whose
7363 * functionality can only be achieved through a normal scan.
7364 */
7365 if (drv->scan_vendor_cmd_avail && !params->only_new_results)
7366 return wpa_driver_nl80211_vendor_scan(bss, params);
7367#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007368 return wpa_driver_nl80211_scan(bss, params);
7369}
7370
7371
7372static int driver_nl80211_deauthenticate(void *priv, const u8 *addr,
7373 int reason_code)
7374{
7375 struct i802_bss *bss = priv;
7376 return wpa_driver_nl80211_deauthenticate(bss, addr, reason_code);
7377}
7378
7379
7380static int driver_nl80211_authenticate(void *priv,
7381 struct wpa_driver_auth_params *params)
7382{
7383 struct i802_bss *bss = priv;
7384 return wpa_driver_nl80211_authenticate(bss, params);
7385}
7386
7387
7388static void driver_nl80211_deinit(void *priv)
7389{
7390 struct i802_bss *bss = priv;
7391 wpa_driver_nl80211_deinit(bss);
7392}
7393
7394
7395static int driver_nl80211_if_remove(void *priv, enum wpa_driver_if_type type,
7396 const char *ifname)
7397{
7398 struct i802_bss *bss = priv;
7399 return wpa_driver_nl80211_if_remove(bss, type, ifname);
7400}
7401
7402
7403static int driver_nl80211_send_mlme(void *priv, const u8 *data,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007404 size_t data_len, int noack,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007405 unsigned int freq,
7406 const u16 *csa_offs, size_t csa_offs_len)
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007407{
7408 struct i802_bss *bss = priv;
7409 return wpa_driver_nl80211_send_mlme(bss, data, data_len, noack,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007410 freq, 0, 0, 0, csa_offs,
7411 csa_offs_len);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007412}
7413
7414
7415static int driver_nl80211_sta_remove(void *priv, const u8 *addr)
7416{
7417 struct i802_bss *bss = priv;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007418 return wpa_driver_nl80211_sta_remove(bss, addr, -1, 0);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007419}
7420
7421
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007422static int driver_nl80211_set_sta_vlan(void *priv, const u8 *addr,
7423 const char *ifname, int vlan_id)
7424{
7425 struct i802_bss *bss = priv;
7426 return i802_set_sta_vlan(bss, addr, ifname, vlan_id);
7427}
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007428
7429
7430static int driver_nl80211_read_sta_data(void *priv,
7431 struct hostap_sta_driver_data *data,
7432 const u8 *addr)
7433{
7434 struct i802_bss *bss = priv;
7435 return i802_read_sta_data(bss, data, addr);
7436}
7437
7438
7439static int driver_nl80211_send_action(void *priv, unsigned int freq,
7440 unsigned int wait_time,
7441 const u8 *dst, const u8 *src,
7442 const u8 *bssid,
7443 const u8 *data, size_t data_len,
7444 int no_cck)
7445{
7446 struct i802_bss *bss = priv;
7447 return wpa_driver_nl80211_send_action(bss, freq, wait_time, dst, src,
7448 bssid, data, data_len, no_cck);
7449}
7450
7451
7452static int driver_nl80211_probe_req_report(void *priv, int report)
7453{
7454 struct i802_bss *bss = priv;
7455 return wpa_driver_nl80211_probe_req_report(bss, report);
7456}
7457
7458
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007459static int wpa_driver_nl80211_update_ft_ies(void *priv, const u8 *md,
7460 const u8 *ies, size_t ies_len)
7461{
7462 int ret;
7463 struct nl_msg *msg;
7464 struct i802_bss *bss = priv;
7465 struct wpa_driver_nl80211_data *drv = bss->drv;
7466 u16 mdid = WPA_GET_LE16(md);
7467
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007468 wpa_printf(MSG_DEBUG, "nl80211: Updating FT IEs");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007469 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_UPDATE_FT_IES)) ||
7470 nla_put(msg, NL80211_ATTR_IE, ies_len, ies) ||
7471 nla_put_u16(msg, NL80211_ATTR_MDID, mdid)) {
7472 nlmsg_free(msg);
7473 return -ENOBUFS;
7474 }
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007475
7476 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7477 if (ret) {
7478 wpa_printf(MSG_DEBUG, "nl80211: update_ft_ies failed "
7479 "err=%d (%s)", ret, strerror(-ret));
7480 }
7481
7482 return ret;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007483}
7484
7485
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007486const u8 * wpa_driver_nl80211_get_macaddr(void *priv)
7487{
7488 struct i802_bss *bss = priv;
7489 struct wpa_driver_nl80211_data *drv = bss->drv;
7490
7491 if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE)
7492 return NULL;
7493
7494 return bss->addr;
7495}
7496
7497
Dmitry Shmidt56052862013-10-04 10:23:25 -07007498static const char * scan_state_str(enum scan_states scan_state)
7499{
7500 switch (scan_state) {
7501 case NO_SCAN:
7502 return "NO_SCAN";
7503 case SCAN_REQUESTED:
7504 return "SCAN_REQUESTED";
7505 case SCAN_STARTED:
7506 return "SCAN_STARTED";
7507 case SCAN_COMPLETED:
7508 return "SCAN_COMPLETED";
7509 case SCAN_ABORTED:
7510 return "SCAN_ABORTED";
7511 case SCHED_SCAN_STARTED:
7512 return "SCHED_SCAN_STARTED";
7513 case SCHED_SCAN_STOPPED:
7514 return "SCHED_SCAN_STOPPED";
7515 case SCHED_SCAN_RESULTS:
7516 return "SCHED_SCAN_RESULTS";
7517 }
7518
7519 return "??";
7520}
7521
7522
7523static int wpa_driver_nl80211_status(void *priv, char *buf, size_t buflen)
7524{
7525 struct i802_bss *bss = priv;
7526 struct wpa_driver_nl80211_data *drv = bss->drv;
7527 int res;
7528 char *pos, *end;
7529
7530 pos = buf;
7531 end = buf + buflen;
7532
7533 res = os_snprintf(pos, end - pos,
7534 "ifindex=%d\n"
7535 "ifname=%s\n"
7536 "brname=%s\n"
7537 "addr=" MACSTR "\n"
7538 "freq=%d\n"
7539 "%s%s%s%s%s",
7540 bss->ifindex,
7541 bss->ifname,
7542 bss->brname,
7543 MAC2STR(bss->addr),
7544 bss->freq,
7545 bss->beacon_set ? "beacon_set=1\n" : "",
7546 bss->added_if_into_bridge ?
7547 "added_if_into_bridge=1\n" : "",
7548 bss->added_bridge ? "added_bridge=1\n" : "",
7549 bss->in_deinit ? "in_deinit=1\n" : "",
7550 bss->if_dynamic ? "if_dynamic=1\n" : "");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007551 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt56052862013-10-04 10:23:25 -07007552 return pos - buf;
7553 pos += res;
7554
7555 if (bss->wdev_id_set) {
7556 res = os_snprintf(pos, end - pos, "wdev_id=%llu\n",
7557 (unsigned long long) bss->wdev_id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007558 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt56052862013-10-04 10:23:25 -07007559 return pos - buf;
7560 pos += res;
7561 }
7562
7563 res = os_snprintf(pos, end - pos,
7564 "phyname=%s\n"
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007565 "perm_addr=" MACSTR "\n"
Dmitry Shmidt56052862013-10-04 10:23:25 -07007566 "drv_ifindex=%d\n"
7567 "operstate=%d\n"
7568 "scan_state=%s\n"
7569 "auth_bssid=" MACSTR "\n"
7570 "auth_attempt_bssid=" MACSTR "\n"
7571 "bssid=" MACSTR "\n"
7572 "prev_bssid=" MACSTR "\n"
7573 "associated=%d\n"
7574 "assoc_freq=%u\n"
7575 "monitor_sock=%d\n"
7576 "monitor_ifidx=%d\n"
7577 "monitor_refcount=%d\n"
7578 "last_mgmt_freq=%u\n"
7579 "eapol_tx_sock=%d\n"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007580 "%s%s%s%s%s%s%s%s%s%s%s%s%s",
Dmitry Shmidt56052862013-10-04 10:23:25 -07007581 drv->phyname,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007582 MAC2STR(drv->perm_addr),
Dmitry Shmidt56052862013-10-04 10:23:25 -07007583 drv->ifindex,
7584 drv->operstate,
7585 scan_state_str(drv->scan_state),
7586 MAC2STR(drv->auth_bssid),
7587 MAC2STR(drv->auth_attempt_bssid),
7588 MAC2STR(drv->bssid),
7589 MAC2STR(drv->prev_bssid),
7590 drv->associated,
7591 drv->assoc_freq,
7592 drv->monitor_sock,
7593 drv->monitor_ifidx,
7594 drv->monitor_refcount,
7595 drv->last_mgmt_freq,
7596 drv->eapol_tx_sock,
7597 drv->ignore_if_down_event ?
7598 "ignore_if_down_event=1\n" : "",
7599 drv->scan_complete_events ?
7600 "scan_complete_events=1\n" : "",
7601 drv->disabled_11b_rates ?
7602 "disabled_11b_rates=1\n" : "",
7603 drv->pending_remain_on_chan ?
7604 "pending_remain_on_chan=1\n" : "",
7605 drv->in_interface_list ? "in_interface_list=1\n" : "",
7606 drv->device_ap_sme ? "device_ap_sme=1\n" : "",
7607 drv->poll_command_supported ?
7608 "poll_command_supported=1\n" : "",
7609 drv->data_tx_status ? "data_tx_status=1\n" : "",
7610 drv->scan_for_auth ? "scan_for_auth=1\n" : "",
7611 drv->retry_auth ? "retry_auth=1\n" : "",
7612 drv->use_monitor ? "use_monitor=1\n" : "",
7613 drv->ignore_next_local_disconnect ?
7614 "ignore_next_local_disconnect=1\n" : "",
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07007615 drv->ignore_next_local_deauth ?
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007616 "ignore_next_local_deauth=1\n" : "");
7617 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt56052862013-10-04 10:23:25 -07007618 return pos - buf;
7619 pos += res;
7620
7621 if (drv->has_capability) {
7622 res = os_snprintf(pos, end - pos,
7623 "capa.key_mgmt=0x%x\n"
7624 "capa.enc=0x%x\n"
7625 "capa.auth=0x%x\n"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007626 "capa.flags=0x%llx\n"
7627 "capa.rrm_flags=0x%x\n"
Dmitry Shmidt56052862013-10-04 10:23:25 -07007628 "capa.max_scan_ssids=%d\n"
7629 "capa.max_sched_scan_ssids=%d\n"
7630 "capa.sched_scan_supported=%d\n"
7631 "capa.max_match_sets=%d\n"
7632 "capa.max_remain_on_chan=%u\n"
7633 "capa.max_stations=%u\n"
7634 "capa.probe_resp_offloads=0x%x\n"
7635 "capa.max_acl_mac_addrs=%u\n"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007636 "capa.num_multichan_concurrent=%u\n"
7637 "capa.mac_addr_rand_sched_scan_supported=%d\n"
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007638 "capa.mac_addr_rand_scan_supported=%d\n"
7639 "capa.conc_capab=%u\n"
7640 "capa.max_conc_chan_2_4=%u\n"
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007641 "capa.max_conc_chan_5_0=%u\n"
7642 "capa.max_sched_scan_plans=%u\n"
7643 "capa.max_sched_scan_plan_interval=%u\n"
7644 "capa.max_sched_scan_plan_iterations=%u\n",
Dmitry Shmidt56052862013-10-04 10:23:25 -07007645 drv->capa.key_mgmt,
7646 drv->capa.enc,
7647 drv->capa.auth,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007648 (unsigned long long) drv->capa.flags,
7649 drv->capa.rrm_flags,
Dmitry Shmidt56052862013-10-04 10:23:25 -07007650 drv->capa.max_scan_ssids,
7651 drv->capa.max_sched_scan_ssids,
7652 drv->capa.sched_scan_supported,
7653 drv->capa.max_match_sets,
7654 drv->capa.max_remain_on_chan,
7655 drv->capa.max_stations,
7656 drv->capa.probe_resp_offloads,
7657 drv->capa.max_acl_mac_addrs,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007658 drv->capa.num_multichan_concurrent,
7659 drv->capa.mac_addr_rand_sched_scan_supported,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007660 drv->capa.mac_addr_rand_scan_supported,
7661 drv->capa.conc_capab,
7662 drv->capa.max_conc_chan_2_4,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007663 drv->capa.max_conc_chan_5_0,
7664 drv->capa.max_sched_scan_plans,
7665 drv->capa.max_sched_scan_plan_interval,
7666 drv->capa.max_sched_scan_plan_iterations);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007667 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt56052862013-10-04 10:23:25 -07007668 return pos - buf;
7669 pos += res;
7670 }
7671
7672 return pos - buf;
7673}
7674
7675
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007676static int set_beacon_data(struct nl_msg *msg, struct beacon_data *settings)
7677{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007678 if ((settings->head &&
7679 nla_put(msg, NL80211_ATTR_BEACON_HEAD,
7680 settings->head_len, settings->head)) ||
7681 (settings->tail &&
7682 nla_put(msg, NL80211_ATTR_BEACON_TAIL,
7683 settings->tail_len, settings->tail)) ||
7684 (settings->beacon_ies &&
7685 nla_put(msg, NL80211_ATTR_IE,
7686 settings->beacon_ies_len, settings->beacon_ies)) ||
7687 (settings->proberesp_ies &&
7688 nla_put(msg, NL80211_ATTR_IE_PROBE_RESP,
7689 settings->proberesp_ies_len, settings->proberesp_ies)) ||
7690 (settings->assocresp_ies &&
7691 nla_put(msg, NL80211_ATTR_IE_ASSOC_RESP,
7692 settings->assocresp_ies_len, settings->assocresp_ies)) ||
7693 (settings->probe_resp &&
7694 nla_put(msg, NL80211_ATTR_PROBE_RESP,
7695 settings->probe_resp_len, settings->probe_resp)))
7696 return -ENOBUFS;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007697
7698 return 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007699}
7700
7701
7702static int nl80211_switch_channel(void *priv, struct csa_settings *settings)
7703{
7704 struct nl_msg *msg;
7705 struct i802_bss *bss = priv;
7706 struct wpa_driver_nl80211_data *drv = bss->drv;
7707 struct nlattr *beacon_csa;
7708 int ret = -ENOBUFS;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007709 int csa_off_len = 0;
7710 int i;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007711
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08007712 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 -08007713 settings->cs_count, settings->block_tx,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08007714 settings->freq_params.freq, settings->freq_params.bandwidth,
7715 settings->freq_params.center_freq1,
7716 settings->freq_params.center_freq2);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007717
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007718 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_AP_CSA)) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007719 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support channel switch command");
7720 return -EOPNOTSUPP;
7721 }
7722
7723 if ((drv->nlmode != NL80211_IFTYPE_AP) &&
7724 (drv->nlmode != NL80211_IFTYPE_P2P_GO))
7725 return -EOPNOTSUPP;
7726
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007727 /*
7728 * Remove empty counters, assuming Probe Response and Beacon frame
7729 * counters match. This implementation assumes that there are only two
7730 * counters.
7731 */
7732 if (settings->counter_offset_beacon[0] &&
7733 !settings->counter_offset_beacon[1]) {
7734 csa_off_len = 1;
7735 } else if (settings->counter_offset_beacon[1] &&
7736 !settings->counter_offset_beacon[0]) {
7737 csa_off_len = 1;
7738 settings->counter_offset_beacon[0] =
7739 settings->counter_offset_beacon[1];
7740 settings->counter_offset_presp[0] =
7741 settings->counter_offset_presp[1];
7742 } else if (settings->counter_offset_beacon[1] &&
7743 settings->counter_offset_beacon[0]) {
7744 csa_off_len = 2;
7745 } else {
7746 wpa_printf(MSG_ERROR, "nl80211: No CSA counters provided");
7747 return -EINVAL;
7748 }
7749
7750 /* Check CSA counters validity */
7751 if (drv->capa.max_csa_counters &&
7752 csa_off_len > drv->capa.max_csa_counters) {
7753 wpa_printf(MSG_ERROR,
7754 "nl80211: Too many CSA counters provided");
7755 return -EINVAL;
7756 }
7757
7758 if (!settings->beacon_csa.tail)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007759 return -EINVAL;
7760
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007761 for (i = 0; i < csa_off_len; i++) {
7762 u16 csa_c_off_bcn = settings->counter_offset_beacon[i];
7763 u16 csa_c_off_presp = settings->counter_offset_presp[i];
7764
7765 if ((settings->beacon_csa.tail_len <= csa_c_off_bcn) ||
7766 (settings->beacon_csa.tail[csa_c_off_bcn] !=
7767 settings->cs_count))
7768 return -EINVAL;
7769
7770 if (settings->beacon_csa.probe_resp &&
7771 ((settings->beacon_csa.probe_resp_len <=
7772 csa_c_off_presp) ||
7773 (settings->beacon_csa.probe_resp[csa_c_off_presp] !=
7774 settings->cs_count)))
7775 return -EINVAL;
7776 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007777
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007778 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_CHANNEL_SWITCH)) ||
7779 nla_put_u32(msg, NL80211_ATTR_CH_SWITCH_COUNT,
7780 settings->cs_count) ||
7781 (ret = nl80211_put_freq_params(msg, &settings->freq_params)) ||
7782 (settings->block_tx &&
7783 nla_put_flag(msg, NL80211_ATTR_CH_SWITCH_BLOCK_TX)))
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007784 goto error;
7785
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007786 /* beacon_after params */
7787 ret = set_beacon_data(msg, &settings->beacon_after);
7788 if (ret)
7789 goto error;
7790
7791 /* beacon_csa params */
7792 beacon_csa = nla_nest_start(msg, NL80211_ATTR_CSA_IES);
7793 if (!beacon_csa)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007794 goto fail;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007795
7796 ret = set_beacon_data(msg, &settings->beacon_csa);
7797 if (ret)
7798 goto error;
7799
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007800 if (nla_put(msg, NL80211_ATTR_CSA_C_OFF_BEACON,
7801 csa_off_len * sizeof(u16),
7802 settings->counter_offset_beacon) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007803 (settings->beacon_csa.probe_resp &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007804 nla_put(msg, NL80211_ATTR_CSA_C_OFF_PRESP,
7805 csa_off_len * sizeof(u16),
7806 settings->counter_offset_presp)))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007807 goto fail;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007808
7809 nla_nest_end(msg, beacon_csa);
7810 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7811 if (ret) {
7812 wpa_printf(MSG_DEBUG, "nl80211: switch_channel failed err=%d (%s)",
7813 ret, strerror(-ret));
7814 }
7815 return ret;
7816
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007817fail:
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007818 ret = -ENOBUFS;
7819error:
7820 nlmsg_free(msg);
7821 wpa_printf(MSG_DEBUG, "nl80211: Could not build channel switch request");
7822 return ret;
7823}
7824
7825
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007826static int nl80211_add_ts(void *priv, u8 tsid, const u8 *addr,
7827 u8 user_priority, u16 admitted_time)
7828{
7829 struct i802_bss *bss = priv;
7830 struct wpa_driver_nl80211_data *drv = bss->drv;
7831 struct nl_msg *msg;
7832 int ret;
7833
7834 wpa_printf(MSG_DEBUG,
7835 "nl80211: add_ts request: tsid=%u admitted_time=%u up=%d",
7836 tsid, admitted_time, user_priority);
7837
7838 if (!is_sta_interface(drv->nlmode))
7839 return -ENOTSUP;
7840
7841 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_ADD_TX_TS);
7842 if (!msg ||
7843 nla_put_u8(msg, NL80211_ATTR_TSID, tsid) ||
7844 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
7845 nla_put_u8(msg, NL80211_ATTR_USER_PRIO, user_priority) ||
7846 nla_put_u16(msg, NL80211_ATTR_ADMITTED_TIME, admitted_time)) {
7847 nlmsg_free(msg);
7848 return -ENOBUFS;
7849 }
7850
7851 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7852 if (ret)
7853 wpa_printf(MSG_DEBUG, "nl80211: add_ts failed err=%d (%s)",
7854 ret, strerror(-ret));
7855 return ret;
7856}
7857
7858
7859static int nl80211_del_ts(void *priv, u8 tsid, const u8 *addr)
7860{
7861 struct i802_bss *bss = priv;
7862 struct wpa_driver_nl80211_data *drv = bss->drv;
7863 struct nl_msg *msg;
7864 int ret;
7865
7866 wpa_printf(MSG_DEBUG, "nl80211: del_ts request: tsid=%u", tsid);
7867
7868 if (!is_sta_interface(drv->nlmode))
7869 return -ENOTSUP;
7870
7871 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_DEL_TX_TS)) ||
7872 nla_put_u8(msg, NL80211_ATTR_TSID, tsid) ||
7873 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
7874 nlmsg_free(msg);
7875 return -ENOBUFS;
7876 }
7877
7878 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7879 if (ret)
7880 wpa_printf(MSG_DEBUG, "nl80211: del_ts failed err=%d (%s)",
7881 ret, strerror(-ret));
7882 return ret;
7883}
7884
7885
Dmitry Shmidta38abf92014-03-06 13:38:44 -08007886#ifdef CONFIG_TESTING_OPTIONS
7887static int cmd_reply_handler(struct nl_msg *msg, void *arg)
7888{
7889 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
7890 struct wpabuf *buf = arg;
7891
7892 if (!buf)
7893 return NL_SKIP;
7894
7895 if ((size_t) genlmsg_attrlen(gnlh, 0) > wpabuf_tailroom(buf)) {
7896 wpa_printf(MSG_INFO, "nl80211: insufficient buffer space for reply");
7897 return NL_SKIP;
7898 }
7899
7900 wpabuf_put_data(buf, genlmsg_attrdata(gnlh, 0),
7901 genlmsg_attrlen(gnlh, 0));
7902
7903 return NL_SKIP;
7904}
7905#endif /* CONFIG_TESTING_OPTIONS */
7906
7907
7908static int vendor_reply_handler(struct nl_msg *msg, void *arg)
7909{
7910 struct nlattr *tb[NL80211_ATTR_MAX + 1];
7911 struct nlattr *nl_vendor_reply, *nl;
7912 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
7913 struct wpabuf *buf = arg;
7914 int rem;
7915
7916 if (!buf)
7917 return NL_SKIP;
7918
7919 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
7920 genlmsg_attrlen(gnlh, 0), NULL);
7921 nl_vendor_reply = tb[NL80211_ATTR_VENDOR_DATA];
7922
7923 if (!nl_vendor_reply)
7924 return NL_SKIP;
7925
7926 if ((size_t) nla_len(nl_vendor_reply) > wpabuf_tailroom(buf)) {
7927 wpa_printf(MSG_INFO, "nl80211: Vendor command: insufficient buffer space for reply");
7928 return NL_SKIP;
7929 }
7930
7931 nla_for_each_nested(nl, nl_vendor_reply, rem) {
7932 wpabuf_put_data(buf, nla_data(nl), nla_len(nl));
7933 }
7934
7935 return NL_SKIP;
7936}
7937
7938
7939static int nl80211_vendor_cmd(void *priv, unsigned int vendor_id,
7940 unsigned int subcmd, const u8 *data,
7941 size_t data_len, struct wpabuf *buf)
7942{
7943 struct i802_bss *bss = priv;
7944 struct wpa_driver_nl80211_data *drv = bss->drv;
7945 struct nl_msg *msg;
7946 int ret;
7947
Dmitry Shmidta38abf92014-03-06 13:38:44 -08007948#ifdef CONFIG_TESTING_OPTIONS
7949 if (vendor_id == 0xffffffff) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007950 msg = nlmsg_alloc();
7951 if (!msg)
7952 return -ENOMEM;
7953
Dmitry Shmidta38abf92014-03-06 13:38:44 -08007954 nl80211_cmd(drv, msg, 0, subcmd);
7955 if (nlmsg_append(msg, (void *) data, data_len, NLMSG_ALIGNTO) <
7956 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007957 goto fail;
Dmitry Shmidta38abf92014-03-06 13:38:44 -08007958 ret = send_and_recv_msgs(drv, msg, cmd_reply_handler, buf);
7959 if (ret)
7960 wpa_printf(MSG_DEBUG, "nl80211: command failed err=%d",
7961 ret);
7962 return ret;
7963 }
7964#endif /* CONFIG_TESTING_OPTIONS */
7965
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007966 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_VENDOR)) ||
7967 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, vendor_id) ||
7968 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD, subcmd) ||
7969 (data &&
7970 nla_put(msg, NL80211_ATTR_VENDOR_DATA, data_len, data)))
7971 goto fail;
Dmitry Shmidta38abf92014-03-06 13:38:44 -08007972
7973 ret = send_and_recv_msgs(drv, msg, vendor_reply_handler, buf);
7974 if (ret)
7975 wpa_printf(MSG_DEBUG, "nl80211: vendor command failed err=%d",
7976 ret);
7977 return ret;
7978
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007979fail:
Dmitry Shmidta38abf92014-03-06 13:38:44 -08007980 nlmsg_free(msg);
7981 return -ENOBUFS;
7982}
7983
7984
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007985static int nl80211_set_qos_map(void *priv, const u8 *qos_map_set,
7986 u8 qos_map_set_len)
7987{
7988 struct i802_bss *bss = priv;
7989 struct wpa_driver_nl80211_data *drv = bss->drv;
7990 struct nl_msg *msg;
7991 int ret;
7992
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007993 wpa_hexdump(MSG_DEBUG, "nl80211: Setting QoS Map",
7994 qos_map_set, qos_map_set_len);
7995
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007996 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_QOS_MAP)) ||
7997 nla_put(msg, NL80211_ATTR_QOS_MAP, qos_map_set_len, qos_map_set)) {
7998 nlmsg_free(msg);
7999 return -ENOBUFS;
8000 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008001
8002 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8003 if (ret)
8004 wpa_printf(MSG_DEBUG, "nl80211: Setting QoS Map failed");
8005
8006 return ret;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008007}
8008
8009
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07008010static int nl80211_set_wowlan(void *priv,
8011 const struct wowlan_triggers *triggers)
8012{
8013 struct i802_bss *bss = priv;
8014 struct wpa_driver_nl80211_data *drv = bss->drv;
8015 struct nl_msg *msg;
8016 struct nlattr *wowlan_triggers;
8017 int ret;
8018
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07008019 wpa_printf(MSG_DEBUG, "nl80211: Setting wowlan");
8020
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008021 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_SET_WOWLAN)) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008022 !(wowlan_triggers = nla_nest_start(msg,
8023 NL80211_ATTR_WOWLAN_TRIGGERS)) ||
8024 (triggers->any &&
8025 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
8026 (triggers->disconnect &&
8027 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
8028 (triggers->magic_pkt &&
8029 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
8030 (triggers->gtk_rekey_failure &&
8031 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
8032 (triggers->eap_identity_req &&
8033 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
8034 (triggers->four_way_handshake &&
8035 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
8036 (triggers->rfkill_release &&
8037 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))) {
8038 nlmsg_free(msg);
8039 return -ENOBUFS;
8040 }
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07008041
8042 nla_nest_end(msg, wowlan_triggers);
8043
8044 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8045 if (ret)
8046 wpa_printf(MSG_DEBUG, "nl80211: Setting wowlan failed");
8047
8048 return ret;
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07008049}
8050
8051
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008052#ifdef CONFIG_DRIVER_NL80211_QCA
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008053static int nl80211_roaming(void *priv, int allowed, const u8 *bssid)
8054{
8055 struct i802_bss *bss = priv;
8056 struct wpa_driver_nl80211_data *drv = bss->drv;
8057 struct nl_msg *msg;
8058 struct nlattr *params;
8059
8060 wpa_printf(MSG_DEBUG, "nl80211: Roaming policy: allowed=%d", allowed);
8061
8062 if (!drv->roaming_vendor_cmd_avail) {
8063 wpa_printf(MSG_DEBUG,
8064 "nl80211: Ignore roaming policy change since driver does not provide command for setting it");
8065 return -1;
8066 }
8067
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008068 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
8069 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
8070 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
8071 QCA_NL80211_VENDOR_SUBCMD_ROAMING) ||
8072 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
8073 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_ROAMING_POLICY,
8074 allowed ? QCA_ROAMING_ALLOWED_WITHIN_ESS :
8075 QCA_ROAMING_NOT_ALLOWED) ||
8076 (bssid &&
8077 nla_put(msg, QCA_WLAN_VENDOR_ATTR_MAC_ADDR, ETH_ALEN, bssid))) {
8078 nlmsg_free(msg);
8079 return -1;
8080 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008081 nla_nest_end(msg, params);
8082
8083 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008084}
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008085#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008086
8087
8088static int nl80211_set_mac_addr(void *priv, const u8 *addr)
8089{
8090 struct i802_bss *bss = priv;
8091 struct wpa_driver_nl80211_data *drv = bss->drv;
8092 int new_addr = addr != NULL;
8093
8094 if (!addr)
8095 addr = drv->perm_addr;
8096
8097 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0) < 0)
8098 return -1;
8099
8100 if (linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname, addr) < 0)
8101 {
8102 wpa_printf(MSG_DEBUG,
8103 "nl80211: failed to set_mac_addr for %s to " MACSTR,
8104 bss->ifname, MAC2STR(addr));
8105 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname,
8106 1) < 0) {
8107 wpa_printf(MSG_DEBUG,
8108 "nl80211: Could not restore interface UP after failed set_mac_addr");
8109 }
8110 return -1;
8111 }
8112
8113 wpa_printf(MSG_DEBUG, "nl80211: set_mac_addr for %s to " MACSTR,
8114 bss->ifname, MAC2STR(addr));
8115 drv->addr_changed = new_addr;
8116 os_memcpy(bss->addr, addr, ETH_ALEN);
8117
8118 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1) < 0)
8119 {
8120 wpa_printf(MSG_DEBUG,
8121 "nl80211: Could not restore interface UP after set_mac_addr");
8122 }
8123
8124 return 0;
8125}
8126
8127
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008128#ifdef CONFIG_MESH
8129
8130static int wpa_driver_nl80211_init_mesh(void *priv)
8131{
8132 if (wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_MESH_POINT)) {
8133 wpa_printf(MSG_INFO,
8134 "nl80211: Failed to set interface into mesh mode");
8135 return -1;
8136 }
8137 return 0;
8138}
8139
8140
Dmitry Shmidtff787d52015-01-12 13:01:47 -08008141static int nl80211_put_mesh_id(struct nl_msg *msg, const u8 *mesh_id,
8142 size_t mesh_id_len)
8143{
8144 if (mesh_id) {
8145 wpa_hexdump_ascii(MSG_DEBUG, " * Mesh ID (SSID)",
8146 mesh_id, mesh_id_len);
8147 return nla_put(msg, NL80211_ATTR_MESH_ID, mesh_id_len, mesh_id);
8148 }
8149
8150 return 0;
8151}
8152
8153
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008154static int nl80211_join_mesh(struct i802_bss *bss,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008155 struct wpa_driver_mesh_join_params *params)
8156{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008157 struct wpa_driver_nl80211_data *drv = bss->drv;
8158 struct nl_msg *msg;
8159 struct nlattr *container;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08008160 int ret = -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008161
8162 wpa_printf(MSG_DEBUG, "nl80211: mesh join (ifindex=%d)", drv->ifindex);
8163 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_JOIN_MESH);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08008164 if (!msg ||
8165 nl80211_put_freq_params(msg, &params->freq) ||
8166 nl80211_put_basic_rates(msg, params->basic_rates) ||
8167 nl80211_put_mesh_id(msg, params->meshid, params->meshid_len) ||
8168 nl80211_put_beacon_int(msg, params->beacon_int))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008169 goto fail;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008170
8171 wpa_printf(MSG_DEBUG, " * flags=%08X", params->flags);
8172
8173 container = nla_nest_start(msg, NL80211_ATTR_MESH_SETUP);
8174 if (!container)
8175 goto fail;
8176
8177 if (params->ies) {
8178 wpa_hexdump(MSG_DEBUG, " * IEs", params->ies, params->ie_len);
8179 if (nla_put(msg, NL80211_MESH_SETUP_IE, params->ie_len,
8180 params->ies))
8181 goto fail;
8182 }
8183 /* WPA_DRIVER_MESH_FLAG_OPEN_AUTH is treated as default by nl80211 */
8184 if (params->flags & WPA_DRIVER_MESH_FLAG_SAE_AUTH) {
8185 if (nla_put_u8(msg, NL80211_MESH_SETUP_AUTH_PROTOCOL, 0x1) ||
8186 nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_AUTH))
8187 goto fail;
8188 }
8189 if ((params->flags & WPA_DRIVER_MESH_FLAG_AMPE) &&
8190 nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_AMPE))
8191 goto fail;
8192 if ((params->flags & WPA_DRIVER_MESH_FLAG_USER_MPM) &&
8193 nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_MPM))
8194 goto fail;
8195 nla_nest_end(msg, container);
8196
8197 container = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
8198 if (!container)
8199 goto fail;
8200
8201 if (!(params->conf.flags & WPA_DRIVER_MESH_CONF_FLAG_AUTO_PLINKS) &&
8202 nla_put_u32(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS, 0))
8203 goto fail;
8204 if ((params->conf.flags & WPA_DRIVER_MESH_FLAG_DRIVER_MPM) &&
8205 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
8206 params->max_peer_links))
8207 goto fail;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08008208
8209 /*
8210 * Set NL80211_MESHCONF_PLINK_TIMEOUT even if user mpm is used because
8211 * the timer could disconnect stations even in that case.
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08008212 */
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008213 if (nla_put_u32(msg, NL80211_MESHCONF_PLINK_TIMEOUT,
8214 params->conf.peer_link_timeout)) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08008215 wpa_printf(MSG_ERROR, "nl80211: Failed to set PLINK_TIMEOUT");
8216 goto fail;
8217 }
8218
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008219 nla_nest_end(msg, container);
8220
8221 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8222 msg = NULL;
8223 if (ret) {
8224 wpa_printf(MSG_DEBUG, "nl80211: mesh join failed: ret=%d (%s)",
8225 ret, strerror(-ret));
8226 goto fail;
8227 }
8228 ret = 0;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08008229 bss->freq = params->freq.freq;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008230 wpa_printf(MSG_DEBUG, "nl80211: mesh join request send successfully");
8231
8232fail:
8233 nlmsg_free(msg);
8234 return ret;
8235}
8236
8237
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008238static int
8239wpa_driver_nl80211_join_mesh(void *priv,
8240 struct wpa_driver_mesh_join_params *params)
8241{
8242 struct i802_bss *bss = priv;
8243 int ret, timeout;
8244
8245 timeout = params->conf.peer_link_timeout;
8246
8247 /* Disable kernel inactivity timer */
8248 if (params->flags & WPA_DRIVER_MESH_FLAG_USER_MPM)
8249 params->conf.peer_link_timeout = 0;
8250
8251 ret = nl80211_join_mesh(bss, params);
8252 if (ret == -EINVAL && params->conf.peer_link_timeout == 0) {
8253 wpa_printf(MSG_DEBUG,
8254 "nl80211: Mesh join retry for peer_link_timeout");
8255 /*
8256 * Old kernel does not support setting
8257 * NL80211_MESHCONF_PLINK_TIMEOUT to zero, so set 60 seconds
8258 * into future from peer_link_timeout.
8259 */
8260 params->conf.peer_link_timeout = timeout + 60;
8261 ret = nl80211_join_mesh(priv, params);
8262 }
8263
8264 params->conf.peer_link_timeout = timeout;
8265 return ret;
8266}
8267
8268
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008269static int wpa_driver_nl80211_leave_mesh(void *priv)
8270{
8271 struct i802_bss *bss = priv;
8272 struct wpa_driver_nl80211_data *drv = bss->drv;
8273 struct nl_msg *msg;
8274 int ret;
8275
8276 wpa_printf(MSG_DEBUG, "nl80211: mesh leave (ifindex=%d)", drv->ifindex);
8277 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_LEAVE_MESH);
8278 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8279 if (ret) {
8280 wpa_printf(MSG_DEBUG, "nl80211: mesh leave failed: ret=%d (%s)",
8281 ret, strerror(-ret));
8282 } else {
8283 wpa_printf(MSG_DEBUG,
8284 "nl80211: mesh leave request send successfully");
8285 }
8286
8287 if (wpa_driver_nl80211_set_mode(drv->first_bss,
8288 NL80211_IFTYPE_STATION)) {
8289 wpa_printf(MSG_INFO,
8290 "nl80211: Failed to set interface into station mode");
8291 }
8292 return ret;
8293}
8294
8295#endif /* CONFIG_MESH */
8296
8297
8298static int wpa_driver_br_add_ip_neigh(void *priv, u8 version,
8299 const u8 *ipaddr, int prefixlen,
8300 const u8 *addr)
8301{
8302#ifdef CONFIG_LIBNL3_ROUTE
8303 struct i802_bss *bss = priv;
8304 struct wpa_driver_nl80211_data *drv = bss->drv;
8305 struct rtnl_neigh *rn;
8306 struct nl_addr *nl_ipaddr = NULL;
8307 struct nl_addr *nl_lladdr = NULL;
8308 int family, addrsize;
8309 int res;
8310
8311 if (!ipaddr || prefixlen == 0 || !addr)
8312 return -EINVAL;
8313
8314 if (bss->br_ifindex == 0) {
8315 wpa_printf(MSG_DEBUG,
8316 "nl80211: bridge must be set before adding an ip neigh to it");
8317 return -1;
8318 }
8319
8320 if (!drv->rtnl_sk) {
8321 wpa_printf(MSG_DEBUG,
8322 "nl80211: nl_sock for NETLINK_ROUTE is not initialized");
8323 return -1;
8324 }
8325
8326 if (version == 4) {
8327 family = AF_INET;
8328 addrsize = 4;
8329 } else if (version == 6) {
8330 family = AF_INET6;
8331 addrsize = 16;
8332 } else {
8333 return -EINVAL;
8334 }
8335
8336 rn = rtnl_neigh_alloc();
8337 if (rn == NULL)
8338 return -ENOMEM;
8339
8340 /* set the destination ip address for neigh */
8341 nl_ipaddr = nl_addr_build(family, (void *) ipaddr, addrsize);
8342 if (nl_ipaddr == NULL) {
8343 wpa_printf(MSG_DEBUG, "nl80211: nl_ipaddr build failed");
8344 res = -ENOMEM;
8345 goto errout;
8346 }
8347 nl_addr_set_prefixlen(nl_ipaddr, prefixlen);
8348 res = rtnl_neigh_set_dst(rn, nl_ipaddr);
8349 if (res) {
8350 wpa_printf(MSG_DEBUG,
8351 "nl80211: neigh set destination addr failed");
8352 goto errout;
8353 }
8354
8355 /* set the corresponding lladdr for neigh */
8356 nl_lladdr = nl_addr_build(AF_BRIDGE, (u8 *) addr, ETH_ALEN);
8357 if (nl_lladdr == NULL) {
8358 wpa_printf(MSG_DEBUG, "nl80211: neigh set lladdr failed");
8359 res = -ENOMEM;
8360 goto errout;
8361 }
8362 rtnl_neigh_set_lladdr(rn, nl_lladdr);
8363
8364 rtnl_neigh_set_ifindex(rn, bss->br_ifindex);
8365 rtnl_neigh_set_state(rn, NUD_PERMANENT);
8366
8367 res = rtnl_neigh_add(drv->rtnl_sk, rn, NLM_F_CREATE);
8368 if (res) {
8369 wpa_printf(MSG_DEBUG,
8370 "nl80211: Adding bridge ip neigh failed: %s",
8371 strerror(errno));
8372 }
8373errout:
8374 if (nl_lladdr)
8375 nl_addr_put(nl_lladdr);
8376 if (nl_ipaddr)
8377 nl_addr_put(nl_ipaddr);
8378 if (rn)
8379 rtnl_neigh_put(rn);
8380 return res;
8381#else /* CONFIG_LIBNL3_ROUTE */
8382 return -1;
8383#endif /* CONFIG_LIBNL3_ROUTE */
8384}
8385
8386
8387static int wpa_driver_br_delete_ip_neigh(void *priv, u8 version,
8388 const u8 *ipaddr)
8389{
8390#ifdef CONFIG_LIBNL3_ROUTE
8391 struct i802_bss *bss = priv;
8392 struct wpa_driver_nl80211_data *drv = bss->drv;
8393 struct rtnl_neigh *rn;
8394 struct nl_addr *nl_ipaddr;
8395 int family, addrsize;
8396 int res;
8397
8398 if (!ipaddr)
8399 return -EINVAL;
8400
8401 if (version == 4) {
8402 family = AF_INET;
8403 addrsize = 4;
8404 } else if (version == 6) {
8405 family = AF_INET6;
8406 addrsize = 16;
8407 } else {
8408 return -EINVAL;
8409 }
8410
8411 if (bss->br_ifindex == 0) {
8412 wpa_printf(MSG_DEBUG,
8413 "nl80211: bridge must be set to delete an ip neigh");
8414 return -1;
8415 }
8416
8417 if (!drv->rtnl_sk) {
8418 wpa_printf(MSG_DEBUG,
8419 "nl80211: nl_sock for NETLINK_ROUTE is not initialized");
8420 return -1;
8421 }
8422
8423 rn = rtnl_neigh_alloc();
8424 if (rn == NULL)
8425 return -ENOMEM;
8426
8427 /* set the destination ip address for neigh */
8428 nl_ipaddr = nl_addr_build(family, (void *) ipaddr, addrsize);
8429 if (nl_ipaddr == NULL) {
8430 wpa_printf(MSG_DEBUG, "nl80211: nl_ipaddr build failed");
8431 res = -ENOMEM;
8432 goto errout;
8433 }
8434 res = rtnl_neigh_set_dst(rn, nl_ipaddr);
8435 if (res) {
8436 wpa_printf(MSG_DEBUG,
8437 "nl80211: neigh set destination addr failed");
8438 goto errout;
8439 }
8440
8441 rtnl_neigh_set_ifindex(rn, bss->br_ifindex);
8442
8443 res = rtnl_neigh_delete(drv->rtnl_sk, rn, 0);
8444 if (res) {
8445 wpa_printf(MSG_DEBUG,
8446 "nl80211: Deleting bridge ip neigh failed: %s",
8447 strerror(errno));
8448 }
8449errout:
8450 if (nl_ipaddr)
8451 nl_addr_put(nl_ipaddr);
8452 if (rn)
8453 rtnl_neigh_put(rn);
8454 return res;
8455#else /* CONFIG_LIBNL3_ROUTE */
8456 return -1;
8457#endif /* CONFIG_LIBNL3_ROUTE */
8458}
8459
8460
8461static int linux_write_system_file(const char *path, unsigned int val)
8462{
8463 char buf[50];
8464 int fd, len;
8465
8466 len = os_snprintf(buf, sizeof(buf), "%u\n", val);
8467 if (os_snprintf_error(sizeof(buf), len))
8468 return -1;
8469
8470 fd = open(path, O_WRONLY);
8471 if (fd < 0)
8472 return -1;
8473
8474 if (write(fd, buf, len) < 0) {
8475 wpa_printf(MSG_DEBUG,
8476 "nl80211: Failed to write Linux system file: %s with the value of %d",
8477 path, val);
8478 close(fd);
8479 return -1;
8480 }
8481 close(fd);
8482
8483 return 0;
8484}
8485
8486
8487static const char * drv_br_port_attr_str(enum drv_br_port_attr attr)
8488{
8489 switch (attr) {
8490 case DRV_BR_PORT_ATTR_PROXYARP:
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07008491 return "proxyarp_wifi";
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008492 case DRV_BR_PORT_ATTR_HAIRPIN_MODE:
8493 return "hairpin_mode";
8494 }
8495
8496 return NULL;
8497}
8498
8499
8500static int wpa_driver_br_port_set_attr(void *priv, enum drv_br_port_attr attr,
8501 unsigned int val)
8502{
8503 struct i802_bss *bss = priv;
8504 char path[128];
8505 const char *attr_txt;
8506
8507 attr_txt = drv_br_port_attr_str(attr);
8508 if (attr_txt == NULL)
8509 return -EINVAL;
8510
8511 os_snprintf(path, sizeof(path), "/sys/class/net/%s/brport/%s",
8512 bss->ifname, attr_txt);
8513
8514 if (linux_write_system_file(path, val))
8515 return -1;
8516
8517 return 0;
8518}
8519
8520
8521static const char * drv_br_net_param_str(enum drv_br_net_param param)
8522{
8523 switch (param) {
8524 case DRV_BR_NET_PARAM_GARP_ACCEPT:
8525 return "arp_accept";
Dmitry Shmidt83474442015-04-15 13:47:09 -07008526 default:
8527 return NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008528 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008529}
8530
8531
8532static int wpa_driver_br_set_net_param(void *priv, enum drv_br_net_param param,
8533 unsigned int val)
8534{
8535 struct i802_bss *bss = priv;
8536 char path[128];
8537 const char *param_txt;
8538 int ip_version = 4;
8539
Dmitry Shmidt83474442015-04-15 13:47:09 -07008540 if (param == DRV_BR_MULTICAST_SNOOPING) {
8541 os_snprintf(path, sizeof(path),
8542 "/sys/devices/virtual/net/%s/bridge/multicast_snooping",
8543 bss->brname);
8544 goto set_val;
8545 }
8546
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008547 param_txt = drv_br_net_param_str(param);
8548 if (param_txt == NULL)
8549 return -EINVAL;
8550
8551 switch (param) {
8552 case DRV_BR_NET_PARAM_GARP_ACCEPT:
8553 ip_version = 4;
8554 break;
8555 default:
8556 return -EINVAL;
8557 }
8558
8559 os_snprintf(path, sizeof(path), "/proc/sys/net/ipv%d/conf/%s/%s",
8560 ip_version, bss->brname, param_txt);
8561
Dmitry Shmidt83474442015-04-15 13:47:09 -07008562set_val:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008563 if (linux_write_system_file(path, val))
8564 return -1;
8565
8566 return 0;
8567}
8568
8569
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008570#ifdef CONFIG_DRIVER_NL80211_QCA
8571
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008572static int hw_mode_to_qca_acs(enum hostapd_hw_mode hw_mode)
8573{
8574 switch (hw_mode) {
8575 case HOSTAPD_MODE_IEEE80211B:
8576 return QCA_ACS_MODE_IEEE80211B;
8577 case HOSTAPD_MODE_IEEE80211G:
8578 return QCA_ACS_MODE_IEEE80211G;
8579 case HOSTAPD_MODE_IEEE80211A:
8580 return QCA_ACS_MODE_IEEE80211A;
8581 case HOSTAPD_MODE_IEEE80211AD:
8582 return QCA_ACS_MODE_IEEE80211AD;
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07008583 case HOSTAPD_MODE_IEEE80211ANY:
8584 return QCA_ACS_MODE_IEEE80211ANY;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008585 default:
8586 return -1;
8587 }
8588}
8589
8590
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008591static int add_acs_freq_list(struct nl_msg *msg, const int *freq_list)
8592{
8593 int i, len, ret;
8594 u32 *freqs;
8595
8596 if (!freq_list)
8597 return 0;
8598 len = int_array_len(freq_list);
8599 freqs = os_malloc(sizeof(u32) * len);
8600 if (!freqs)
8601 return -1;
8602 for (i = 0; i < len; i++)
8603 freqs[i] = freq_list[i];
8604 ret = nla_put(msg, QCA_WLAN_VENDOR_ATTR_ACS_FREQ_LIST,
8605 sizeof(u32) * len, freqs);
8606 os_free(freqs);
8607 return ret;
8608}
8609
8610
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008611static int wpa_driver_do_acs(void *priv, struct drv_acs_params *params)
8612{
8613 struct i802_bss *bss = priv;
8614 struct wpa_driver_nl80211_data *drv = bss->drv;
8615 struct nl_msg *msg;
8616 struct nlattr *data;
8617 int ret;
8618 int mode;
8619
8620 mode = hw_mode_to_qca_acs(params->hw_mode);
8621 if (mode < 0)
8622 return -1;
8623
8624 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
8625 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
8626 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
8627 QCA_NL80211_VENDOR_SUBCMD_DO_ACS) ||
8628 !(data = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
8629 nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_ACS_HW_MODE, mode) ||
8630 (params->ht_enabled &&
8631 nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_HT_ENABLED)) ||
8632 (params->ht40_enabled &&
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07008633 nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_HT40_ENABLED)) ||
8634 (params->vht_enabled &&
8635 nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_VHT_ENABLED)) ||
8636 nla_put_u16(msg, QCA_WLAN_VENDOR_ATTR_ACS_CHWIDTH,
8637 params->ch_width) ||
8638 (params->ch_list_len &&
8639 nla_put(msg, QCA_WLAN_VENDOR_ATTR_ACS_CH_LIST, params->ch_list_len,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008640 params->ch_list)) ||
8641 add_acs_freq_list(msg, params->freq_list)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008642 nlmsg_free(msg);
8643 return -ENOBUFS;
8644 }
8645 nla_nest_end(msg, data);
8646
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07008647 wpa_printf(MSG_DEBUG,
8648 "nl80211: ACS Params: HW_MODE: %d HT: %d HT40: %d VHT: %d BW: %d CH_LIST_LEN: %u",
8649 params->hw_mode, params->ht_enabled, params->ht40_enabled,
8650 params->vht_enabled, params->ch_width, params->ch_list_len);
8651
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008652 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8653 if (ret) {
8654 wpa_printf(MSG_DEBUG,
8655 "nl80211: Failed to invoke driver ACS function: %s",
8656 strerror(errno));
8657 }
8658 return ret;
8659}
8660
8661
Ravi Joshie6ccb162015-07-16 17:45:41 -07008662static int nl80211_set_band(void *priv, enum set_band band)
8663{
8664 struct i802_bss *bss = priv;
8665 struct wpa_driver_nl80211_data *drv = bss->drv;
8666 struct nl_msg *msg;
8667 struct nlattr *data;
8668 int ret;
8669 enum qca_set_band qca_band;
8670
8671 if (!drv->setband_vendor_cmd_avail)
8672 return -1;
8673
8674 switch (band) {
8675 case WPA_SETBAND_AUTO:
8676 qca_band = QCA_SETBAND_AUTO;
8677 break;
8678 case WPA_SETBAND_5G:
8679 qca_band = QCA_SETBAND_5G;
8680 break;
8681 case WPA_SETBAND_2G:
8682 qca_band = QCA_SETBAND_2G;
8683 break;
8684 default:
8685 return -1;
8686 }
8687
8688 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
8689 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
8690 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
8691 QCA_NL80211_VENDOR_SUBCMD_SETBAND) ||
8692 !(data = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
8693 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_SETBAND_VALUE, qca_band)) {
8694 nlmsg_free(msg);
8695 return -ENOBUFS;
8696 }
8697 nla_nest_end(msg, data);
8698
8699 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8700 if (ret) {
8701 wpa_printf(MSG_DEBUG,
8702 "nl80211: Driver setband function failed: %s",
8703 strerror(errno));
8704 }
8705 return ret;
8706}
8707
8708
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008709struct nl80211_pcl {
8710 unsigned int num;
8711 unsigned int *freq_list;
8712};
8713
8714static int preferred_freq_info_handler(struct nl_msg *msg, void *arg)
8715{
8716 struct nlattr *tb[NL80211_ATTR_MAX + 1];
8717 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
8718 struct nl80211_pcl *param = arg;
8719 struct nlattr *nl_vend, *attr;
8720 enum qca_iface_type iface_type;
8721 struct nlattr *tb_vendor[QCA_WLAN_VENDOR_ATTR_MAX + 1];
8722 unsigned int num, max_num;
8723 u32 *freqs;
8724
8725 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
8726 genlmsg_attrlen(gnlh, 0), NULL);
8727
8728 nl_vend = tb[NL80211_ATTR_VENDOR_DATA];
8729 if (!nl_vend)
8730 return NL_SKIP;
8731
8732 nla_parse(tb_vendor, QCA_WLAN_VENDOR_ATTR_MAX,
8733 nla_data(nl_vend), nla_len(nl_vend), NULL);
8734
8735 attr = tb_vendor[
8736 QCA_WLAN_VENDOR_ATTR_GET_PREFERRED_FREQ_LIST_IFACE_TYPE];
8737 if (!attr) {
8738 wpa_printf(MSG_ERROR, "nl80211: iface_type couldn't be found");
8739 param->num = 0;
8740 return NL_SKIP;
8741 }
8742
8743 iface_type = (enum qca_iface_type) nla_get_u32(attr);
8744 wpa_printf(MSG_DEBUG, "nl80211: Driver returned iface_type=%d",
8745 iface_type);
8746
8747 attr = tb_vendor[QCA_WLAN_VENDOR_ATTR_GET_PREFERRED_FREQ_LIST];
8748 if (!attr) {
8749 wpa_printf(MSG_ERROR,
8750 "nl80211: preferred_freq_list couldn't be found");
8751 param->num = 0;
8752 return NL_SKIP;
8753 }
8754
8755 /*
8756 * param->num has the maximum number of entries for which there
8757 * is room in the freq_list provided by the caller.
8758 */
8759 freqs = nla_data(attr);
8760 max_num = nla_len(attr) / sizeof(u32);
8761 if (max_num > param->num)
8762 max_num = param->num;
8763 for (num = 0; num < max_num; num++)
8764 param->freq_list[num] = freqs[num];
8765 param->num = num;
8766
8767 return NL_SKIP;
8768}
8769
8770
8771static int nl80211_get_pref_freq_list(void *priv,
8772 enum wpa_driver_if_type if_type,
8773 unsigned int *num,
8774 unsigned int *freq_list)
8775{
8776 struct i802_bss *bss = priv;
8777 struct wpa_driver_nl80211_data *drv = bss->drv;
8778 struct nl_msg *msg;
8779 int ret;
8780 unsigned int i;
8781 struct nlattr *params;
8782 struct nl80211_pcl param;
8783 enum qca_iface_type iface_type;
8784
8785 if (!drv->get_pref_freq_list)
8786 return -1;
8787
8788 switch (if_type) {
8789 case WPA_IF_STATION:
8790 iface_type = QCA_IFACE_TYPE_STA;
8791 break;
8792 case WPA_IF_AP_BSS:
8793 iface_type = QCA_IFACE_TYPE_AP;
8794 break;
8795 case WPA_IF_P2P_GO:
8796 iface_type = QCA_IFACE_TYPE_P2P_GO;
8797 break;
8798 case WPA_IF_P2P_CLIENT:
8799 iface_type = QCA_IFACE_TYPE_P2P_CLIENT;
8800 break;
8801 case WPA_IF_IBSS:
8802 iface_type = QCA_IFACE_TYPE_IBSS;
8803 break;
8804 case WPA_IF_TDLS:
8805 iface_type = QCA_IFACE_TYPE_TDLS;
8806 break;
8807 default:
8808 return -1;
8809 }
8810
8811 param.num = *num;
8812 param.freq_list = freq_list;
8813
8814 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
8815 nla_put_u32(msg, NL80211_ATTR_IFINDEX, drv->ifindex) ||
8816 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
8817 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
8818 QCA_NL80211_VENDOR_SUBCMD_GET_PREFERRED_FREQ_LIST) ||
8819 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
8820 nla_put_u32(msg,
8821 QCA_WLAN_VENDOR_ATTR_GET_PREFERRED_FREQ_LIST_IFACE_TYPE,
8822 iface_type)) {
8823 wpa_printf(MSG_ERROR,
8824 "%s: err in adding vendor_cmd and vendor_data",
8825 __func__);
8826 nlmsg_free(msg);
8827 return -1;
8828 }
8829 nla_nest_end(msg, params);
8830
8831 os_memset(freq_list, 0, *num * sizeof(freq_list[0]));
8832 ret = send_and_recv_msgs(drv, msg, preferred_freq_info_handler, &param);
8833 if (ret) {
8834 wpa_printf(MSG_ERROR,
8835 "%s: err in send_and_recv_msgs", __func__);
8836 return ret;
8837 }
8838
8839 *num = param.num;
8840
8841 for (i = 0; i < *num; i++) {
8842 wpa_printf(MSG_DEBUG, "nl80211: preferred_channel_list[%d]=%d",
8843 i, freq_list[i]);
8844 }
8845
8846 return 0;
8847}
8848
8849
8850static int nl80211_set_prob_oper_freq(void *priv, unsigned int freq)
8851{
8852 struct i802_bss *bss = priv;
8853 struct wpa_driver_nl80211_data *drv = bss->drv;
8854 struct nl_msg *msg;
8855 int ret;
8856 struct nlattr *params;
8857
8858 if (!drv->set_prob_oper_freq)
8859 return -1;
8860
8861 wpa_printf(MSG_DEBUG,
8862 "nl80211: Set P2P probable operating freq %u for ifindex %d",
8863 freq, bss->ifindex);
8864
8865 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
8866 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
8867 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
8868 QCA_NL80211_VENDOR_SUBCMD_SET_PROBABLE_OPER_CHANNEL) ||
8869 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
8870 nla_put_u32(msg,
8871 QCA_WLAN_VENDOR_ATTR_PROBABLE_OPER_CHANNEL_IFACE_TYPE,
8872 QCA_IFACE_TYPE_P2P_CLIENT) ||
8873 nla_put_u32(msg,
8874 QCA_WLAN_VENDOR_ATTR_PROBABLE_OPER_CHANNEL_FREQ,
8875 freq)) {
8876 wpa_printf(MSG_ERROR,
8877 "%s: err in adding vendor_cmd and vendor_data",
8878 __func__);
8879 nlmsg_free(msg);
8880 return -1;
8881 }
8882 nla_nest_end(msg, params);
8883
8884 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8885 msg = NULL;
8886 if (ret) {
8887 wpa_printf(MSG_ERROR, "%s: err in send_and_recv_msgs",
8888 __func__);
8889 return ret;
8890 }
8891 nlmsg_free(msg);
8892 return 0;
8893}
8894
8895#endif /* CONFIG_DRIVER_NL80211_QCA */
8896
8897
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008898const struct wpa_driver_ops wpa_driver_nl80211_ops = {
8899 .name = "nl80211",
8900 .desc = "Linux nl80211/cfg80211",
8901 .get_bssid = wpa_driver_nl80211_get_bssid,
8902 .get_ssid = wpa_driver_nl80211_get_ssid,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008903 .set_key = driver_nl80211_set_key,
8904 .scan2 = driver_nl80211_scan2,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008905 .sched_scan = wpa_driver_nl80211_sched_scan,
8906 .stop_sched_scan = wpa_driver_nl80211_stop_sched_scan,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008907 .get_scan_results2 = wpa_driver_nl80211_get_scan_results,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008908 .abort_scan = wpa_driver_nl80211_abort_scan,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008909 .deauthenticate = driver_nl80211_deauthenticate,
8910 .authenticate = driver_nl80211_authenticate,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008911 .associate = wpa_driver_nl80211_associate,
8912 .global_init = nl80211_global_init,
8913 .global_deinit = nl80211_global_deinit,
8914 .init2 = wpa_driver_nl80211_init,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008915 .deinit = driver_nl80211_deinit,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008916 .get_capa = wpa_driver_nl80211_get_capa,
8917 .set_operstate = wpa_driver_nl80211_set_operstate,
8918 .set_supp_port = wpa_driver_nl80211_set_supp_port,
8919 .set_country = wpa_driver_nl80211_set_country,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008920 .get_country = wpa_driver_nl80211_get_country,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008921 .set_ap = wpa_driver_nl80211_set_ap,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07008922 .set_acl = wpa_driver_nl80211_set_acl,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008923 .if_add = wpa_driver_nl80211_if_add,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008924 .if_remove = driver_nl80211_if_remove,
8925 .send_mlme = driver_nl80211_send_mlme,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008926 .get_hw_feature_data = nl80211_get_hw_feature_data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008927 .sta_add = wpa_driver_nl80211_sta_add,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008928 .sta_remove = driver_nl80211_sta_remove,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008929 .hapd_send_eapol = wpa_driver_nl80211_hapd_send_eapol,
8930 .sta_set_flags = wpa_driver_nl80211_sta_set_flags,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008931 .hapd_init = i802_init,
8932 .hapd_deinit = i802_deinit,
Jouni Malinen75ecf522011-06-27 15:19:46 -07008933 .set_wds_sta = i802_set_wds_sta,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008934 .get_seqnum = i802_get_seqnum,
8935 .flush = i802_flush,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008936 .get_inact_sec = i802_get_inact_sec,
8937 .sta_clear_stats = i802_sta_clear_stats,
8938 .set_rts = i802_set_rts,
8939 .set_frag = i802_set_frag,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008940 .set_tx_queue_params = i802_set_tx_queue_params,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008941 .set_sta_vlan = driver_nl80211_set_sta_vlan,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008942 .sta_deauth = i802_sta_deauth,
8943 .sta_disassoc = i802_sta_disassoc,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008944 .read_sta_data = driver_nl80211_read_sta_data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008945 .set_freq = i802_set_freq,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008946 .send_action = driver_nl80211_send_action,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008947 .send_action_cancel_wait = wpa_driver_nl80211_send_action_cancel_wait,
8948 .remain_on_channel = wpa_driver_nl80211_remain_on_channel,
8949 .cancel_remain_on_channel =
8950 wpa_driver_nl80211_cancel_remain_on_channel,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008951 .probe_req_report = driver_nl80211_probe_req_report,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008952 .deinit_ap = wpa_driver_nl80211_deinit_ap,
Dmitry Shmidt04949592012-07-19 12:16:46 -07008953 .deinit_p2p_cli = wpa_driver_nl80211_deinit_p2p_cli,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008954 .resume = wpa_driver_nl80211_resume,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008955 .signal_monitor = nl80211_signal_monitor,
8956 .signal_poll = nl80211_signal_poll,
8957 .send_frame = nl80211_send_frame,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008958 .set_param = nl80211_set_param,
8959 .get_radio_name = nl80211_get_radio_name,
Jouni Malinen75ecf522011-06-27 15:19:46 -07008960 .add_pmkid = nl80211_add_pmkid,
8961 .remove_pmkid = nl80211_remove_pmkid,
8962 .flush_pmkid = nl80211_flush_pmkid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008963 .set_rekey_info = nl80211_set_rekey_info,
8964 .poll_client = nl80211_poll_client,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008965 .set_p2p_powersave = nl80211_set_p2p_powersave,
Dmitry Shmidtea69e842013-05-13 14:52:28 -07008966 .start_dfs_cac = nl80211_start_radar_detection,
8967 .stop_ap = wpa_driver_nl80211_stop_ap,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008968#ifdef CONFIG_TDLS
8969 .send_tdls_mgmt = nl80211_send_tdls_mgmt,
8970 .tdls_oper = nl80211_tdls_oper,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008971 .tdls_enable_channel_switch = nl80211_tdls_enable_channel_switch,
8972 .tdls_disable_channel_switch = nl80211_tdls_disable_channel_switch,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008973#endif /* CONFIG_TDLS */
Dmitry Shmidt700a1372013-03-15 14:14:44 -07008974 .update_ft_ies = wpa_driver_nl80211_update_ft_ies,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008975 .get_mac_addr = wpa_driver_nl80211_get_macaddr,
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008976 .get_survey = wpa_driver_nl80211_get_survey,
Dmitry Shmidt56052862013-10-04 10:23:25 -07008977 .status = wpa_driver_nl80211_status,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008978 .switch_channel = nl80211_switch_channel,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008979#ifdef ANDROID_P2P
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07008980 .set_noa = wpa_driver_set_p2p_noa,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008981 .get_noa = wpa_driver_get_p2p_noa,
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07008982 .set_ap_wps_ie = wpa_driver_set_ap_wps_p2p_ie,
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08008983#endif /* ANDROID_P2P */
Dmitry Shmidt738a26e2011-07-07 14:22:14 -07008984#ifdef ANDROID
Dmitry Shmidt41712582015-06-29 11:02:15 -07008985#ifndef ANDROID_LIB_STUB
Dmitry Shmidt738a26e2011-07-07 14:22:14 -07008986 .driver_cmd = wpa_driver_nl80211_driver_cmd,
Dmitry Shmidt41712582015-06-29 11:02:15 -07008987#endif /* !ANDROID_LIB_STUB */
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08008988#endif /* ANDROID */
Dmitry Shmidta38abf92014-03-06 13:38:44 -08008989 .vendor_cmd = nl80211_vendor_cmd,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008990 .set_qos_map = nl80211_set_qos_map,
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07008991 .set_wowlan = nl80211_set_wowlan,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008992#ifdef CONFIG_DRIVER_NL80211_QCA
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008993 .roaming = nl80211_roaming,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008994#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008995 .set_mac_addr = nl80211_set_mac_addr,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008996#ifdef CONFIG_MESH
8997 .init_mesh = wpa_driver_nl80211_init_mesh,
8998 .join_mesh = wpa_driver_nl80211_join_mesh,
8999 .leave_mesh = wpa_driver_nl80211_leave_mesh,
9000#endif /* CONFIG_MESH */
9001 .br_add_ip_neigh = wpa_driver_br_add_ip_neigh,
9002 .br_delete_ip_neigh = wpa_driver_br_delete_ip_neigh,
9003 .br_port_set_attr = wpa_driver_br_port_set_attr,
9004 .br_set_net_param = wpa_driver_br_set_net_param,
9005 .add_tx_ts = nl80211_add_ts,
9006 .del_tx_ts = nl80211_del_ts,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009007#ifdef CONFIG_DRIVER_NL80211_QCA
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009008 .do_acs = wpa_driver_do_acs,
Ravi Joshie6ccb162015-07-16 17:45:41 -07009009 .set_band = nl80211_set_band,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009010 .get_pref_freq_list = nl80211_get_pref_freq_list,
9011 .set_prob_oper_freq = nl80211_set_prob_oper_freq,
9012#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009013};