blob: ed5e4a8e23e73e45fe65916fe7a76ba5290f2dee [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 Shmidte0e48dc2013-11-18 12:00:06 -08001738 if (linux_iface_up(drv->global->ioctl_sock, ifname) > 0)
1739 drv->start_iface_up = 1;
1740
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001741 if (wpa_driver_nl80211_finish_drv_init(drv, set_addr, 1, driver_params))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001742 goto failed;
1743
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001744 drv->eapol_tx_sock = socket(PF_PACKET, SOCK_DGRAM, 0);
1745 if (drv->eapol_tx_sock < 0)
1746 goto failed;
1747
1748 if (drv->data_tx_status) {
1749 int enabled = 1;
1750
1751 if (setsockopt(drv->eapol_tx_sock, SOL_SOCKET, SO_WIFI_STATUS,
1752 &enabled, sizeof(enabled)) < 0) {
1753 wpa_printf(MSG_DEBUG,
1754 "nl80211: wifi status sockopt failed\n");
1755 drv->data_tx_status = 0;
1756 if (!drv->use_monitor)
1757 drv->capa.flags &=
1758 ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
1759 } else {
1760 eloop_register_read_sock(drv->eapol_tx_sock,
1761 wpa_driver_nl80211_handle_eapol_tx_status,
1762 drv, NULL);
1763 }
1764 }
1765
1766 if (drv->global) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001767 nl80211_check_global(drv->global);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001768 dl_list_add(&drv->global->interfaces, &drv->list);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001769 drv->in_interface_list = 1;
1770 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001771
1772 return bss;
1773
1774failed:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001775 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001776 return NULL;
1777}
1778
1779
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001780/**
1781 * wpa_driver_nl80211_init - Initialize nl80211 driver interface
1782 * @ctx: context to be used when calling wpa_supplicant functions,
1783 * e.g., wpa_supplicant_event()
1784 * @ifname: interface name, e.g., wlan0
1785 * @global_priv: private driver global data from global_init()
1786 * Returns: Pointer to private data, %NULL on failure
1787 */
1788static void * wpa_driver_nl80211_init(void *ctx, const char *ifname,
1789 void *global_priv)
1790{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001791 return wpa_driver_nl80211_drv_init(ctx, ifname, global_priv, 0, NULL,
1792 NULL);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001793}
1794
1795
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001796static int nl80211_register_frame(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001797 struct nl_handle *nl_handle,
1798 u16 type, const u8 *match, size_t match_len)
1799{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001800 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001801 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001802 int ret;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001803 char buf[30];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001804
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001805 buf[0] = '\0';
1806 wpa_snprintf_hex(buf, sizeof(buf), match, match_len);
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07001807 wpa_printf(MSG_DEBUG, "nl80211: Register frame type=0x%x (%s) nl_handle=%p match=%s",
1808 type, fc2str(type), nl_handle, buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001809
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001810 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_REGISTER_ACTION)) ||
1811 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE, type) ||
1812 nla_put(msg, NL80211_ATTR_FRAME_MATCH, match_len, match)) {
1813 nlmsg_free(msg);
1814 return -1;
1815 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001816
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001817 ret = send_and_recv(drv->global, nl_handle, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001818 if (ret) {
1819 wpa_printf(MSG_DEBUG, "nl80211: Register frame command "
1820 "failed (type=%u): ret=%d (%s)",
1821 type, ret, strerror(-ret));
1822 wpa_hexdump(MSG_DEBUG, "nl80211: Register frame match",
1823 match, match_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001824 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001825 return ret;
1826}
1827
1828
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001829static int nl80211_alloc_mgmt_handle(struct i802_bss *bss)
1830{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001831 if (bss->nl_mgmt) {
1832 wpa_printf(MSG_DEBUG, "nl80211: Mgmt reporting "
1833 "already on! (nl_mgmt=%p)", bss->nl_mgmt);
1834 return -1;
1835 }
1836
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001837 bss->nl_mgmt = nl_create_handle(bss->nl_cb, "mgmt");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001838 if (bss->nl_mgmt == NULL)
1839 return -1;
1840
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001841 return 0;
1842}
1843
1844
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001845static void nl80211_mgmt_handle_register_eloop(struct i802_bss *bss)
1846{
1847 nl80211_register_eloop_read(&bss->nl_mgmt,
1848 wpa_driver_nl80211_event_receive,
1849 bss->nl_cb);
1850}
1851
1852
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001853static int nl80211_register_action_frame(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001854 const u8 *match, size_t match_len)
1855{
1856 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_ACTION << 4);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001857 return nl80211_register_frame(bss, bss->nl_mgmt,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001858 type, match, match_len);
1859}
1860
1861
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001862static int nl80211_mgmt_subscribe_non_ap(struct i802_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001863{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001864 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001865 int ret = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001866
1867 if (nl80211_alloc_mgmt_handle(bss))
1868 return -1;
1869 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with non-AP "
1870 "handle %p", bss->nl_mgmt);
1871
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001872 if (drv->nlmode == NL80211_IFTYPE_ADHOC) {
1873 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_AUTH << 4);
1874
1875 /* register for any AUTH message */
1876 nl80211_register_frame(bss, bss->nl_mgmt, type, NULL, 0);
1877 }
1878
Dmitry Shmidt051af732013-10-22 13:52:46 -07001879#ifdef CONFIG_INTERWORKING
1880 /* QoS Map Configure */
1881 if (nl80211_register_action_frame(bss, (u8 *) "\x01\x04", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001882 ret = -1;
Dmitry Shmidt051af732013-10-22 13:52:46 -07001883#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001884#if defined(CONFIG_P2P) || defined(CONFIG_INTERWORKING)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001885 /* GAS Initial Request */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001886 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0a", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001887 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001888 /* GAS Initial Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001889 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0b", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001890 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001891 /* GAS Comeback Request */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001892 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0c", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001893 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001894 /* GAS Comeback Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001895 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0d", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001896 ret = -1;
Dmitry Shmidt18463232014-01-24 12:29:41 -08001897 /* Protected GAS Initial Request */
1898 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0a", 2) < 0)
1899 ret = -1;
1900 /* Protected GAS Initial Response */
1901 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0b", 2) < 0)
1902 ret = -1;
1903 /* Protected GAS Comeback Request */
1904 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0c", 2) < 0)
1905 ret = -1;
1906 /* Protected GAS Comeback Response */
1907 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0d", 2) < 0)
1908 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001909#endif /* CONFIG_P2P || CONFIG_INTERWORKING */
1910#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001911 /* P2P Public Action */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001912 if (nl80211_register_action_frame(bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001913 (u8 *) "\x04\x09\x50\x6f\x9a\x09",
1914 6) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001915 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001916 /* P2P Action */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001917 if (nl80211_register_action_frame(bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001918 (u8 *) "\x7f\x50\x6f\x9a\x09",
1919 5) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001920 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001921#endif /* CONFIG_P2P */
1922#ifdef CONFIG_IEEE80211W
1923 /* SA Query Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001924 if (nl80211_register_action_frame(bss, (u8 *) "\x08\x01", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001925 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001926#endif /* CONFIG_IEEE80211W */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001927#ifdef CONFIG_TDLS
1928 if ((drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT)) {
1929 /* TDLS Discovery Response */
1930 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0e", 2) <
1931 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001932 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001933 }
1934#endif /* CONFIG_TDLS */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001935#ifdef CONFIG_FST
1936 /* FST Action frames */
1937 if (nl80211_register_action_frame(bss, (u8 *) "\x12", 1) < 0)
1938 ret = -1;
1939#endif /* CONFIG_FST */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001940
1941 /* FT Action frames */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001942 if (nl80211_register_action_frame(bss, (u8 *) "\x06", 1) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001943 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001944 else
1945 drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_FT |
1946 WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK;
1947
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001948 /* WNM - BSS Transition Management Request */
1949 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x07", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001950 ret = -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001951 /* WNM-Sleep Mode Response */
1952 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x11", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001953 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001954
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001955#ifdef CONFIG_HS20
1956 /* WNM-Notification */
1957 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x1a", 2) < 0)
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001958 ret = -1;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001959#endif /* CONFIG_HS20 */
1960
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001961 /* WMM-AC ADDTS Response */
1962 if (nl80211_register_action_frame(bss, (u8 *) "\x11\x01", 2) < 0)
1963 ret = -1;
1964
1965 /* WMM-AC DELTS */
1966 if (nl80211_register_action_frame(bss, (u8 *) "\x11\x02", 2) < 0)
1967 ret = -1;
1968
1969 /* Radio Measurement - Neighbor Report Response */
1970 if (nl80211_register_action_frame(bss, (u8 *) "\x05\x05", 2) < 0)
1971 ret = -1;
1972
1973 /* Radio Measurement - Link Measurement Request */
1974 if ((drv->capa.rrm_flags & WPA_DRIVER_FLAGS_TX_POWER_INSERTION) &&
1975 (nl80211_register_action_frame(bss, (u8 *) "\x05\x02", 2) < 0))
1976 ret = -1;
1977
1978 nl80211_mgmt_handle_register_eloop(bss);
1979
1980 return ret;
1981}
1982
1983
1984static int nl80211_mgmt_subscribe_mesh(struct i802_bss *bss)
1985{
1986 int ret = 0;
1987
1988 if (nl80211_alloc_mgmt_handle(bss))
1989 return -1;
1990
1991 wpa_printf(MSG_DEBUG,
1992 "nl80211: Subscribe to mgmt frames with mesh handle %p",
1993 bss->nl_mgmt);
1994
1995 /* Auth frames for mesh SAE */
1996 if (nl80211_register_frame(bss, bss->nl_mgmt,
1997 (WLAN_FC_TYPE_MGMT << 2) |
1998 (WLAN_FC_STYPE_AUTH << 4),
1999 NULL, 0) < 0)
2000 ret = -1;
2001
2002 /* Mesh peering open */
2003 if (nl80211_register_action_frame(bss, (u8 *) "\x0f\x01", 2) < 0)
2004 ret = -1;
2005 /* Mesh peering confirm */
2006 if (nl80211_register_action_frame(bss, (u8 *) "\x0f\x02", 2) < 0)
2007 ret = -1;
2008 /* Mesh peering close */
2009 if (nl80211_register_action_frame(bss, (u8 *) "\x0f\x03", 2) < 0)
2010 ret = -1;
2011
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002012 nl80211_mgmt_handle_register_eloop(bss);
2013
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002014 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002015}
2016
2017
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002018static int nl80211_register_spurious_class3(struct i802_bss *bss)
2019{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002020 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002021 int ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002022
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002023 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_UNEXPECTED_FRAME);
2024 ret = send_and_recv(bss->drv->global, bss->nl_mgmt, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002025 if (ret) {
2026 wpa_printf(MSG_DEBUG, "nl80211: Register spurious class3 "
2027 "failed: ret=%d (%s)",
2028 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002029 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002030 return ret;
2031}
2032
2033
2034static int nl80211_mgmt_subscribe_ap(struct i802_bss *bss)
2035{
2036 static const int stypes[] = {
2037 WLAN_FC_STYPE_AUTH,
2038 WLAN_FC_STYPE_ASSOC_REQ,
2039 WLAN_FC_STYPE_REASSOC_REQ,
2040 WLAN_FC_STYPE_DISASSOC,
2041 WLAN_FC_STYPE_DEAUTH,
2042 WLAN_FC_STYPE_ACTION,
2043 WLAN_FC_STYPE_PROBE_REQ,
2044/* Beacon doesn't work as mac80211 doesn't currently allow
2045 * it, but it wouldn't really be the right thing anyway as
2046 * it isn't per interface ... maybe just dump the scan
2047 * results periodically for OLBC?
2048 */
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07002049 /* WLAN_FC_STYPE_BEACON, */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002050 };
2051 unsigned int i;
2052
2053 if (nl80211_alloc_mgmt_handle(bss))
2054 return -1;
2055 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
2056 "handle %p", bss->nl_mgmt);
2057
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002058 for (i = 0; i < ARRAY_SIZE(stypes); i++) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002059 if (nl80211_register_frame(bss, bss->nl_mgmt,
2060 (WLAN_FC_TYPE_MGMT << 2) |
2061 (stypes[i] << 4),
2062 NULL, 0) < 0) {
2063 goto out_err;
2064 }
2065 }
2066
2067 if (nl80211_register_spurious_class3(bss))
2068 goto out_err;
2069
2070 if (nl80211_get_wiphy_data_ap(bss) == NULL)
2071 goto out_err;
2072
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002073 nl80211_mgmt_handle_register_eloop(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002074 return 0;
2075
2076out_err:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002077 nl_destroy_handles(&bss->nl_mgmt);
2078 return -1;
2079}
2080
2081
2082static int nl80211_mgmt_subscribe_ap_dev_sme(struct i802_bss *bss)
2083{
2084 if (nl80211_alloc_mgmt_handle(bss))
2085 return -1;
2086 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
2087 "handle %p (device SME)", bss->nl_mgmt);
2088
2089 if (nl80211_register_frame(bss, bss->nl_mgmt,
2090 (WLAN_FC_TYPE_MGMT << 2) |
2091 (WLAN_FC_STYPE_ACTION << 4),
2092 NULL, 0) < 0)
2093 goto out_err;
2094
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002095 nl80211_mgmt_handle_register_eloop(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002096 return 0;
2097
2098out_err:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002099 nl_destroy_handles(&bss->nl_mgmt);
2100 return -1;
2101}
2102
2103
2104static void nl80211_mgmt_unsubscribe(struct i802_bss *bss, const char *reason)
2105{
2106 if (bss->nl_mgmt == NULL)
2107 return;
2108 wpa_printf(MSG_DEBUG, "nl80211: Unsubscribe mgmt frames handle %p "
2109 "(%s)", bss->nl_mgmt, reason);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002110 nl80211_destroy_eloop_handle(&bss->nl_mgmt);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002111
2112 nl80211_put_wiphy_data_ap(bss);
2113}
2114
2115
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002116static void wpa_driver_nl80211_send_rfkill(void *eloop_ctx, void *timeout_ctx)
2117{
2118 wpa_supplicant_event(timeout_ctx, EVENT_INTERFACE_DISABLED, NULL);
2119}
2120
2121
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002122static void nl80211_del_p2pdev(struct i802_bss *bss)
2123{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002124 struct nl_msg *msg;
2125 int ret;
2126
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002127 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_DEL_INTERFACE);
2128 ret = send_and_recv_msgs(bss->drv, msg, NULL, NULL);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002129
2130 wpa_printf(MSG_DEBUG, "nl80211: Delete P2P Device %s (0x%llx): %s",
2131 bss->ifname, (long long unsigned int) bss->wdev_id,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002132 strerror(-ret));
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002133}
2134
2135
2136static int nl80211_set_p2pdev(struct i802_bss *bss, int start)
2137{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002138 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002139 int ret;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002140
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002141 msg = nl80211_cmd_msg(bss, 0, start ? NL80211_CMD_START_P2P_DEVICE :
2142 NL80211_CMD_STOP_P2P_DEVICE);
2143 ret = send_and_recv_msgs(bss->drv, msg, NULL, NULL);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002144
2145 wpa_printf(MSG_DEBUG, "nl80211: %s P2P Device %s (0x%llx): %s",
2146 start ? "Start" : "Stop",
2147 bss->ifname, (long long unsigned int) bss->wdev_id,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002148 strerror(-ret));
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002149 return ret;
2150}
2151
2152
2153static int i802_set_iface_flags(struct i802_bss *bss, int up)
2154{
2155 enum nl80211_iftype nlmode;
2156
2157 nlmode = nl80211_get_ifmode(bss);
2158 if (nlmode != NL80211_IFTYPE_P2P_DEVICE) {
2159 return linux_set_iface_flags(bss->drv->global->ioctl_sock,
2160 bss->ifname, up);
2161 }
2162
2163 /* P2P Device has start/stop which is equivalent */
2164 return nl80211_set_p2pdev(bss, up);
2165}
2166
2167
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002168#ifdef CONFIG_TESTING_OPTIONS
2169static int qca_vendor_test_cmd_handler(struct nl_msg *msg, void *arg)
2170{
2171 /* struct wpa_driver_nl80211_data *drv = arg; */
2172 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2173 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2174
2175
2176 wpa_printf(MSG_DEBUG,
2177 "nl80211: QCA vendor test command response received");
2178
2179 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2180 genlmsg_attrlen(gnlh, 0), NULL);
2181 if (!tb[NL80211_ATTR_VENDOR_DATA]) {
2182 wpa_printf(MSG_DEBUG, "nl80211: No vendor data attribute");
2183 return NL_SKIP;
2184 }
2185
2186 wpa_hexdump(MSG_DEBUG,
2187 "nl80211: Received QCA vendor test command response",
2188 nla_data(tb[NL80211_ATTR_VENDOR_DATA]),
2189 nla_len(tb[NL80211_ATTR_VENDOR_DATA]));
2190
2191 return NL_SKIP;
2192}
2193#endif /* CONFIG_TESTING_OPTIONS */
2194
2195
2196static void qca_vendor_test(struct wpa_driver_nl80211_data *drv)
2197{
2198#ifdef CONFIG_TESTING_OPTIONS
2199 struct nl_msg *msg;
2200 struct nlattr *params;
2201 int ret;
2202
2203 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
2204 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
2205 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
2206 QCA_NL80211_VENDOR_SUBCMD_TEST) ||
2207 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
2208 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_TEST, 123)) {
2209 nlmsg_free(msg);
2210 return;
2211 }
2212 nla_nest_end(msg, params);
2213
2214 ret = send_and_recv_msgs(drv, msg, qca_vendor_test_cmd_handler, drv);
2215 wpa_printf(MSG_DEBUG,
2216 "nl80211: QCA vendor test command returned %d (%s)",
2217 ret, strerror(-ret));
2218#endif /* CONFIG_TESTING_OPTIONS */
2219}
2220
2221
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002222static int
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002223wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002224 const u8 *set_addr, int first,
2225 const char *driver_params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002226{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002227 struct i802_bss *bss = drv->first_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002228 int send_rfkill_event = 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002229 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002230
2231 drv->ifindex = if_nametoindex(bss->ifname);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002232 bss->ifindex = drv->ifindex;
2233 bss->wdev_id = drv->global->if_add_wdevid;
2234 bss->wdev_id_set = drv->global->if_add_wdevid_set;
2235
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002236 bss->if_dynamic = drv->ifindex == drv->global->if_add_ifindex;
2237 bss->if_dynamic = bss->if_dynamic || drv->global->if_add_wdevid_set;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002238 drv->global->if_add_wdevid_set = 0;
2239
Dmitry Shmidt03658832014-08-13 11:03:49 -07002240 if (!bss->if_dynamic && nl80211_get_ifmode(bss) == NL80211_IFTYPE_AP)
2241 bss->static_ap = 1;
2242
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002243 if (wpa_driver_nl80211_capa(drv))
2244 return -1;
2245
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002246 if (driver_params && nl80211_set_param(bss, driver_params) < 0)
2247 return -1;
2248
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002249 wpa_printf(MSG_DEBUG, "nl80211: interface %s in phy %s",
2250 bss->ifname, drv->phyname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002251
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002252 if (set_addr &&
2253 (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0) ||
2254 linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
2255 set_addr)))
2256 return -1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002257
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002258 if (first && nl80211_get_ifmode(bss) == NL80211_IFTYPE_AP)
2259 drv->start_mode_ap = 1;
2260
Dmitry Shmidt03658832014-08-13 11:03:49 -07002261 if (drv->hostapd || bss->static_ap)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002262 nlmode = NL80211_IFTYPE_AP;
2263 else if (bss->if_dynamic)
2264 nlmode = nl80211_get_ifmode(bss);
2265 else
2266 nlmode = NL80211_IFTYPE_STATION;
2267
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002268 if (wpa_driver_nl80211_set_mode(bss, nlmode) < 0) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002269 wpa_printf(MSG_ERROR, "nl80211: Could not configure driver mode");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002270 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002271 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002272
Dmitry Shmidt98660862014-03-11 17:26:21 -07002273 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002274 nl80211_get_macaddr(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002275
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08002276 wpa_driver_nl80211_drv_init_rfkill(drv);
2277
Dmitry Shmidt98660862014-03-11 17:26:21 -07002278 if (!rfkill_is_blocked(drv->rfkill)) {
2279 int ret = i802_set_iface_flags(bss, 1);
2280 if (ret) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002281 wpa_printf(MSG_ERROR, "nl80211: Could not set "
2282 "interface '%s' UP", bss->ifname);
Dmitry Shmidt98660862014-03-11 17:26:21 -07002283 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002284 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002285
2286 if (is_p2p_net_interface(nlmode))
2287 nl80211_disable_11b_rates(bss->drv,
2288 bss->drv->ifindex, 1);
2289
Dmitry Shmidt98660862014-03-11 17:26:21 -07002290 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
2291 return ret;
2292 } else {
2293 wpa_printf(MSG_DEBUG, "nl80211: Could not yet enable "
2294 "interface '%s' due to rfkill", bss->ifname);
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08002295 if (nlmode != NL80211_IFTYPE_P2P_DEVICE)
2296 drv->if_disabled = 1;
2297
Dmitry Shmidt98660862014-03-11 17:26:21 -07002298 send_rfkill_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002299 }
2300
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08002301 if (!drv->hostapd && nlmode != NL80211_IFTYPE_P2P_DEVICE)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002302 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
2303 1, IF_OPER_DORMANT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002304
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08002305 if (nlmode != NL80211_IFTYPE_P2P_DEVICE) {
2306 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
2307 bss->addr))
2308 return -1;
2309 os_memcpy(drv->perm_addr, bss->addr, ETH_ALEN);
2310 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002311
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002312 if (send_rfkill_event) {
2313 eloop_register_timeout(0, 0, wpa_driver_nl80211_send_rfkill,
2314 drv, drv->ctx);
2315 }
2316
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002317 if (drv->vendor_cmd_test_avail)
2318 qca_vendor_test(drv);
2319
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002320 return 0;
2321}
2322
2323
2324static int wpa_driver_nl80211_del_beacon(struct wpa_driver_nl80211_data *drv)
2325{
2326 struct nl_msg *msg;
2327
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002328 wpa_printf(MSG_DEBUG, "nl80211: Remove beacon (ifindex=%d)",
2329 drv->ifindex);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002330 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_DEL_BEACON);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002331 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002332}
2333
2334
2335/**
2336 * wpa_driver_nl80211_deinit - Deinitialize nl80211 driver interface
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002337 * @bss: Pointer to private nl80211 data from wpa_driver_nl80211_init()
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002338 *
2339 * Shut down driver interface and processing of driver events. Free
2340 * private data buffer if one was allocated in wpa_driver_nl80211_init().
2341 */
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002342static void wpa_driver_nl80211_deinit(struct i802_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002343{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002344 struct wpa_driver_nl80211_data *drv = bss->drv;
2345
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002346 wpa_printf(MSG_INFO, "nl80211: deinit ifname=%s disabled_11b_rates=%d",
2347 bss->ifname, drv->disabled_11b_rates);
2348
Dmitry Shmidt04949592012-07-19 12:16:46 -07002349 bss->in_deinit = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002350 if (drv->data_tx_status)
2351 eloop_unregister_read_sock(drv->eapol_tx_sock);
2352 if (drv->eapol_tx_sock >= 0)
2353 close(drv->eapol_tx_sock);
2354
2355 if (bss->nl_preq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002356 wpa_driver_nl80211_probe_req_report(bss, 0);
2357 if (bss->added_if_into_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002358 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
2359 bss->ifname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002360 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
2361 "interface %s from bridge %s: %s",
2362 bss->ifname, bss->brname, strerror(errno));
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002363 if (drv->rtnl_sk)
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07002364 nl80211_handle_destroy(drv->rtnl_sk);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002365 }
2366 if (bss->added_bridge) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002367 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->brname,
2368 0) < 0)
2369 wpa_printf(MSG_INFO,
2370 "nl80211: Could not set bridge %s down",
2371 bss->brname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002372 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002373 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
2374 "bridge %s: %s",
2375 bss->brname, strerror(errno));
2376 }
2377
2378 nl80211_remove_monitor_interface(drv);
2379
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002380 if (is_ap_interface(drv->nlmode))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002381 wpa_driver_nl80211_del_beacon(drv);
2382
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002383 if (drv->eapol_sock >= 0) {
2384 eloop_unregister_read_sock(drv->eapol_sock);
2385 close(drv->eapol_sock);
2386 }
2387
2388 if (drv->if_indices != drv->default_if_indices)
2389 os_free(drv->if_indices);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002390
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002391 if (drv->disabled_11b_rates)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002392 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
2393
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002394 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, 0,
2395 IF_OPER_UP);
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07002396 eloop_cancel_timeout(wpa_driver_nl80211_send_rfkill, drv, drv->ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002397 rfkill_deinit(drv->rfkill);
2398
2399 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
2400
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002401 if (!drv->start_iface_up)
2402 (void) i802_set_iface_flags(bss, 0);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002403
2404 if (drv->addr_changed) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002405 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname,
2406 0) < 0) {
2407 wpa_printf(MSG_DEBUG,
2408 "nl80211: Could not set interface down to restore permanent MAC address");
2409 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002410 if (linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
2411 drv->perm_addr) < 0) {
2412 wpa_printf(MSG_DEBUG,
2413 "nl80211: Could not restore permanent MAC address");
2414 }
2415 }
2416
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002417 if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002418 if (!drv->hostapd || !drv->start_mode_ap)
2419 wpa_driver_nl80211_set_mode(bss,
2420 NL80211_IFTYPE_STATION);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002421 nl80211_mgmt_unsubscribe(bss, "deinit");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002422 } else {
2423 nl80211_mgmt_unsubscribe(bss, "deinit");
2424 nl80211_del_p2pdev(bss);
2425 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002426
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002427 nl80211_destroy_bss(drv->first_bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002428
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002429 os_free(drv->filter_ssids);
2430
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002431 os_free(drv->auth_ie);
2432
2433 if (drv->in_interface_list)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002434 dl_list_del(&drv->list);
2435
Dmitry Shmidt444d5672013-04-01 13:08:44 -07002436 os_free(drv->extended_capa);
2437 os_free(drv->extended_capa_mask);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002438 os_free(drv->first_bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002439 os_free(drv);
2440}
2441
2442
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002443static u32 wpa_alg_to_cipher_suite(enum wpa_alg alg, size_t key_len)
2444{
2445 switch (alg) {
2446 case WPA_ALG_WEP:
2447 if (key_len == 5)
2448 return WLAN_CIPHER_SUITE_WEP40;
2449 return WLAN_CIPHER_SUITE_WEP104;
2450 case WPA_ALG_TKIP:
2451 return WLAN_CIPHER_SUITE_TKIP;
2452 case WPA_ALG_CCMP:
2453 return WLAN_CIPHER_SUITE_CCMP;
2454 case WPA_ALG_GCMP:
2455 return WLAN_CIPHER_SUITE_GCMP;
2456 case WPA_ALG_CCMP_256:
2457 return WLAN_CIPHER_SUITE_CCMP_256;
2458 case WPA_ALG_GCMP_256:
2459 return WLAN_CIPHER_SUITE_GCMP_256;
2460 case WPA_ALG_IGTK:
2461 return WLAN_CIPHER_SUITE_AES_CMAC;
2462 case WPA_ALG_BIP_GMAC_128:
2463 return WLAN_CIPHER_SUITE_BIP_GMAC_128;
2464 case WPA_ALG_BIP_GMAC_256:
2465 return WLAN_CIPHER_SUITE_BIP_GMAC_256;
2466 case WPA_ALG_BIP_CMAC_256:
2467 return WLAN_CIPHER_SUITE_BIP_CMAC_256;
2468 case WPA_ALG_SMS4:
2469 return WLAN_CIPHER_SUITE_SMS4;
2470 case WPA_ALG_KRK:
2471 return WLAN_CIPHER_SUITE_KRK;
2472 case WPA_ALG_NONE:
2473 case WPA_ALG_PMK:
2474 wpa_printf(MSG_ERROR, "nl80211: Unexpected encryption algorithm %d",
2475 alg);
2476 return 0;
2477 }
2478
2479 wpa_printf(MSG_ERROR, "nl80211: Unsupported encryption algorithm %d",
2480 alg);
2481 return 0;
2482}
2483
2484
2485static u32 wpa_cipher_to_cipher_suite(unsigned int cipher)
2486{
2487 switch (cipher) {
2488 case WPA_CIPHER_CCMP_256:
2489 return WLAN_CIPHER_SUITE_CCMP_256;
2490 case WPA_CIPHER_GCMP_256:
2491 return WLAN_CIPHER_SUITE_GCMP_256;
2492 case WPA_CIPHER_CCMP:
2493 return WLAN_CIPHER_SUITE_CCMP;
2494 case WPA_CIPHER_GCMP:
2495 return WLAN_CIPHER_SUITE_GCMP;
2496 case WPA_CIPHER_TKIP:
2497 return WLAN_CIPHER_SUITE_TKIP;
2498 case WPA_CIPHER_WEP104:
2499 return WLAN_CIPHER_SUITE_WEP104;
2500 case WPA_CIPHER_WEP40:
2501 return WLAN_CIPHER_SUITE_WEP40;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002502 case WPA_CIPHER_GTK_NOT_USED:
2503 return WLAN_CIPHER_SUITE_NO_GROUP_ADDR;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002504 }
2505
2506 return 0;
2507}
2508
2509
2510static int wpa_cipher_to_cipher_suites(unsigned int ciphers, u32 suites[],
2511 int max_suites)
2512{
2513 int num_suites = 0;
2514
2515 if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP_256)
2516 suites[num_suites++] = WLAN_CIPHER_SUITE_CCMP_256;
2517 if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP_256)
2518 suites[num_suites++] = WLAN_CIPHER_SUITE_GCMP_256;
2519 if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP)
2520 suites[num_suites++] = WLAN_CIPHER_SUITE_CCMP;
2521 if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP)
2522 suites[num_suites++] = WLAN_CIPHER_SUITE_GCMP;
2523 if (num_suites < max_suites && ciphers & WPA_CIPHER_TKIP)
2524 suites[num_suites++] = WLAN_CIPHER_SUITE_TKIP;
2525 if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP104)
2526 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP104;
2527 if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP40)
2528 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP40;
2529
2530 return num_suites;
2531}
2532
2533
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002534#ifdef CONFIG_DRIVER_NL80211_QCA
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002535static int issue_key_mgmt_set_key(struct wpa_driver_nl80211_data *drv,
2536 const u8 *key, size_t key_len)
2537{
2538 struct nl_msg *msg;
2539 int ret;
2540
2541 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_KEY_MGMT_OFFLOAD))
2542 return 0;
2543
2544 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
2545 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
2546 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
2547 QCA_NL80211_VENDOR_SUBCMD_KEY_MGMT_SET_KEY) ||
2548 nla_put(msg, NL80211_ATTR_VENDOR_DATA, key_len, key)) {
2549 nl80211_nlmsg_clear(msg);
2550 nlmsg_free(msg);
2551 return -1;
2552 }
2553 ret = send_and_recv_msgs(drv, msg, NULL, (void *) -1);
2554 if (ret) {
2555 wpa_printf(MSG_DEBUG,
2556 "nl80211: Key management set key failed: ret=%d (%s)",
2557 ret, strerror(-ret));
2558 }
2559
2560 return ret;
2561}
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002562#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002563
2564
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002565static int wpa_driver_nl80211_set_key(const char *ifname, struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002566 enum wpa_alg alg, const u8 *addr,
2567 int key_idx, int set_tx,
2568 const u8 *seq, size_t seq_len,
2569 const u8 *key, size_t key_len)
2570{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002571 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002572 int ifindex;
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002573 struct nl_msg *msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002574 int ret;
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07002575 int tdls = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002576
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002577 /* Ignore for P2P Device */
2578 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
2579 return 0;
2580
2581 ifindex = if_nametoindex(ifname);
2582 wpa_printf(MSG_DEBUG, "%s: ifindex=%d (%s) alg=%d addr=%p key_idx=%d "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002583 "set_tx=%d seq_len=%lu key_len=%lu",
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002584 __func__, ifindex, ifname, alg, addr, key_idx, set_tx,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002585 (unsigned long) seq_len, (unsigned long) key_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002586#ifdef CONFIG_TDLS
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07002587 if (key_idx == -1) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002588 key_idx = 0;
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07002589 tdls = 1;
2590 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002591#endif /* CONFIG_TDLS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002592
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002593#ifdef CONFIG_DRIVER_NL80211_QCA
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002594 if (alg == WPA_ALG_PMK &&
2595 (drv->capa.flags & WPA_DRIVER_FLAGS_KEY_MGMT_OFFLOAD)) {
2596 wpa_printf(MSG_DEBUG, "%s: calling issue_key_mgmt_set_key",
2597 __func__);
2598 ret = issue_key_mgmt_set_key(drv, key, key_len);
2599 return ret;
2600 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002601#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002602
2603 if (alg == WPA_ALG_NONE) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002604 msg = nl80211_ifindex_msg(drv, ifindex, 0, NL80211_CMD_DEL_KEY);
2605 if (!msg)
2606 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002607 } else {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002608 u32 suite;
2609
2610 suite = wpa_alg_to_cipher_suite(alg, key_len);
2611 if (!suite)
2612 goto fail;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002613 msg = nl80211_ifindex_msg(drv, ifindex, 0, NL80211_CMD_NEW_KEY);
2614 if (!msg ||
2615 nla_put(msg, NL80211_ATTR_KEY_DATA, key_len, key) ||
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002616 nla_put_u32(msg, NL80211_ATTR_KEY_CIPHER, suite))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002617 goto fail;
Dmitry Shmidt98660862014-03-11 17:26:21 -07002618 wpa_hexdump_key(MSG_DEBUG, "nl80211: KEY_DATA", key, key_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002619 }
2620
Dmitry Shmidt98660862014-03-11 17:26:21 -07002621 if (seq && seq_len) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002622 if (nla_put(msg, NL80211_ATTR_KEY_SEQ, seq_len, seq))
2623 goto fail;
Dmitry Shmidt98660862014-03-11 17:26:21 -07002624 wpa_hexdump(MSG_DEBUG, "nl80211: KEY_SEQ", seq, seq_len);
2625 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002626
2627 if (addr && !is_broadcast_ether_addr(addr)) {
2628 wpa_printf(MSG_DEBUG, " addr=" MACSTR, MAC2STR(addr));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002629 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
2630 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002631
2632 if (alg != WPA_ALG_WEP && key_idx && !set_tx) {
2633 wpa_printf(MSG_DEBUG, " RSN IBSS RX GTK");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002634 if (nla_put_u32(msg, NL80211_ATTR_KEY_TYPE,
2635 NL80211_KEYTYPE_GROUP))
2636 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002637 }
2638 } else if (addr && is_broadcast_ether_addr(addr)) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002639 struct nlattr *types;
2640
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002641 wpa_printf(MSG_DEBUG, " broadcast key");
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002642
2643 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002644 if (!types ||
2645 nla_put_flag(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST))
2646 goto fail;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002647 nla_nest_end(msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002648 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002649 if (nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
2650 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002651
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002652 ret = send_and_recv_msgs(drv, msg, NULL, key ? (void *) -1 : NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002653 if ((ret == -ENOENT || ret == -ENOLINK) && alg == WPA_ALG_NONE)
2654 ret = 0;
2655 if (ret)
2656 wpa_printf(MSG_DEBUG, "nl80211: set_key failed; err=%d %s)",
2657 ret, strerror(-ret));
2658
2659 /*
2660 * If we failed or don't need to set the default TX key (below),
2661 * we're done here.
2662 */
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07002663 if (ret || !set_tx || alg == WPA_ALG_NONE || tdls)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002664 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002665 if (is_ap_interface(drv->nlmode) && addr &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002666 !is_broadcast_ether_addr(addr))
2667 return ret;
2668
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002669 msg = nl80211_ifindex_msg(drv, ifindex, 0, NL80211_CMD_SET_KEY);
2670 if (!msg ||
2671 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx) ||
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002672 nla_put_flag(msg, (alg == WPA_ALG_IGTK ||
2673 alg == WPA_ALG_BIP_GMAC_128 ||
2674 alg == WPA_ALG_BIP_GMAC_256 ||
2675 alg == WPA_ALG_BIP_CMAC_256) ?
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002676 NL80211_ATTR_KEY_DEFAULT_MGMT :
2677 NL80211_ATTR_KEY_DEFAULT))
2678 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002679 if (addr && is_broadcast_ether_addr(addr)) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002680 struct nlattr *types;
2681
2682 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002683 if (!types ||
2684 nla_put_flag(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST))
2685 goto fail;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002686 nla_nest_end(msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002687 } else if (addr) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002688 struct nlattr *types;
2689
2690 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002691 if (!types ||
2692 nla_put_flag(msg, NL80211_KEY_DEFAULT_TYPE_UNICAST))
2693 goto fail;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002694 nla_nest_end(msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002695 }
2696
2697 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
2698 if (ret == -ENOENT)
2699 ret = 0;
2700 if (ret)
2701 wpa_printf(MSG_DEBUG, "nl80211: set_key default failed; "
2702 "err=%d %s)", ret, strerror(-ret));
2703 return ret;
2704
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002705fail:
2706 nl80211_nlmsg_clear(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002707 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002708 return -ENOBUFS;
2709}
2710
2711
2712static int nl_add_key(struct nl_msg *msg, enum wpa_alg alg,
2713 int key_idx, int defkey,
2714 const u8 *seq, size_t seq_len,
2715 const u8 *key, size_t key_len)
2716{
2717 struct nlattr *key_attr = nla_nest_start(msg, NL80211_ATTR_KEY);
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002718 u32 suite;
2719
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002720 if (!key_attr)
2721 return -1;
2722
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002723 suite = wpa_alg_to_cipher_suite(alg, key_len);
2724 if (!suite)
2725 return -1;
2726
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002727 if (defkey && alg == WPA_ALG_IGTK) {
2728 if (nla_put_flag(msg, NL80211_KEY_DEFAULT_MGMT))
2729 return -1;
2730 } else if (defkey) {
2731 if (nla_put_flag(msg, NL80211_KEY_DEFAULT))
2732 return -1;
2733 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002734
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002735 if (nla_put_u8(msg, NL80211_KEY_IDX, key_idx) ||
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002736 nla_put_u32(msg, NL80211_KEY_CIPHER, suite) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002737 (seq && seq_len &&
2738 nla_put(msg, NL80211_KEY_SEQ, seq_len, seq)) ||
2739 nla_put(msg, NL80211_KEY_DATA, key_len, key))
2740 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002741
2742 nla_nest_end(msg, key_attr);
2743
2744 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002745}
2746
2747
2748static int nl80211_set_conn_keys(struct wpa_driver_associate_params *params,
2749 struct nl_msg *msg)
2750{
2751 int i, privacy = 0;
2752 struct nlattr *nl_keys, *nl_key;
2753
2754 for (i = 0; i < 4; i++) {
2755 if (!params->wep_key[i])
2756 continue;
2757 privacy = 1;
2758 break;
2759 }
2760 if (params->wps == WPS_MODE_PRIVACY)
2761 privacy = 1;
2762 if (params->pairwise_suite &&
2763 params->pairwise_suite != WPA_CIPHER_NONE)
2764 privacy = 1;
2765
2766 if (!privacy)
2767 return 0;
2768
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002769 if (nla_put_flag(msg, NL80211_ATTR_PRIVACY))
2770 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002771
2772 nl_keys = nla_nest_start(msg, NL80211_ATTR_KEYS);
2773 if (!nl_keys)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002774 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002775
2776 for (i = 0; i < 4; i++) {
2777 if (!params->wep_key[i])
2778 continue;
2779
2780 nl_key = nla_nest_start(msg, i);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002781 if (!nl_key ||
2782 nla_put(msg, NL80211_KEY_DATA, params->wep_key_len[i],
2783 params->wep_key[i]) ||
2784 nla_put_u32(msg, NL80211_KEY_CIPHER,
2785 params->wep_key_len[i] == 5 ?
2786 WLAN_CIPHER_SUITE_WEP40 :
2787 WLAN_CIPHER_SUITE_WEP104) ||
2788 nla_put_u8(msg, NL80211_KEY_IDX, i) ||
2789 (i == params->wep_tx_keyidx &&
2790 nla_put_flag(msg, NL80211_KEY_DEFAULT)))
2791 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002792
2793 nla_nest_end(msg, nl_key);
2794 }
2795 nla_nest_end(msg, nl_keys);
2796
2797 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002798}
2799
2800
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002801int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
2802 const u8 *addr, int cmd, u16 reason_code,
2803 int local_state_change)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002804{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002805 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002806 struct nl_msg *msg;
2807
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002808 if (!(msg = nl80211_drv_msg(drv, 0, cmd)) ||
2809 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code) ||
2810 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
2811 (local_state_change &&
2812 nla_put_flag(msg, NL80211_ATTR_LOCAL_STATE_CHANGE))) {
2813 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002814 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002815 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002816
2817 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002818 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002819 wpa_dbg(drv->ctx, MSG_DEBUG,
2820 "nl80211: MLME command failed: reason=%u ret=%d (%s)",
2821 reason_code, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002822 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002823 return ret;
2824}
2825
2826
2827static int wpa_driver_nl80211_disconnect(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002828 int reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002829{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002830 int ret;
2831
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002832 wpa_printf(MSG_DEBUG, "%s(reason_code=%d)", __func__, reason_code);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07002833 nl80211_mark_disconnected(drv);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002834 /* Disconnect command doesn't need BSSID - it uses cached value */
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002835 ret = wpa_driver_nl80211_mlme(drv, NULL, NL80211_CMD_DISCONNECT,
2836 reason_code, 0);
2837 /*
2838 * For locally generated disconnect, supplicant already generates a
2839 * DEAUTH event, so ignore the event from NL80211.
2840 */
2841 drv->ignore_next_local_disconnect = ret == 0;
2842
2843 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002844}
2845
2846
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002847static int wpa_driver_nl80211_deauthenticate(struct i802_bss *bss,
2848 const u8 *addr, int reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002849{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002850 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07002851 int ret;
Dmitry Shmidt413dde72014-04-11 10:23:22 -07002852
2853 if (drv->nlmode == NL80211_IFTYPE_ADHOC) {
2854 nl80211_mark_disconnected(drv);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002855 return nl80211_leave_ibss(drv, 1);
Dmitry Shmidt413dde72014-04-11 10:23:22 -07002856 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002857 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME))
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002858 return wpa_driver_nl80211_disconnect(drv, reason_code);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002859 wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " reason_code=%d)",
2860 __func__, MAC2STR(addr), reason_code);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07002861 nl80211_mark_disconnected(drv);
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07002862 ret = wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DEAUTHENTICATE,
2863 reason_code, 0);
2864 /*
2865 * For locally generated deauthenticate, supplicant already generates a
2866 * DEAUTH event, so ignore the event from NL80211.
2867 */
2868 drv->ignore_next_local_deauth = ret == 0;
2869 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002870}
2871
2872
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002873static void nl80211_copy_auth_params(struct wpa_driver_nl80211_data *drv,
2874 struct wpa_driver_auth_params *params)
2875{
2876 int i;
2877
2878 drv->auth_freq = params->freq;
2879 drv->auth_alg = params->auth_alg;
2880 drv->auth_wep_tx_keyidx = params->wep_tx_keyidx;
2881 drv->auth_local_state_change = params->local_state_change;
2882 drv->auth_p2p = params->p2p;
2883
2884 if (params->bssid)
2885 os_memcpy(drv->auth_bssid_, params->bssid, ETH_ALEN);
2886 else
2887 os_memset(drv->auth_bssid_, 0, ETH_ALEN);
2888
2889 if (params->ssid) {
2890 os_memcpy(drv->auth_ssid, params->ssid, params->ssid_len);
2891 drv->auth_ssid_len = params->ssid_len;
2892 } else
2893 drv->auth_ssid_len = 0;
2894
2895
2896 os_free(drv->auth_ie);
2897 drv->auth_ie = NULL;
2898 drv->auth_ie_len = 0;
2899 if (params->ie) {
2900 drv->auth_ie = os_malloc(params->ie_len);
2901 if (drv->auth_ie) {
2902 os_memcpy(drv->auth_ie, params->ie, params->ie_len);
2903 drv->auth_ie_len = params->ie_len;
2904 }
2905 }
2906
2907 for (i = 0; i < 4; i++) {
2908 if (params->wep_key[i] && params->wep_key_len[i] &&
2909 params->wep_key_len[i] <= 16) {
2910 os_memcpy(drv->auth_wep_key[i], params->wep_key[i],
2911 params->wep_key_len[i]);
2912 drv->auth_wep_key_len[i] = params->wep_key_len[i];
2913 } else
2914 drv->auth_wep_key_len[i] = 0;
2915 }
2916}
2917
2918
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002919static void nl80211_unmask_11b_rates(struct i802_bss *bss)
2920{
2921 struct wpa_driver_nl80211_data *drv = bss->drv;
2922
2923 if (is_p2p_net_interface(drv->nlmode) || !drv->disabled_11b_rates)
2924 return;
2925
2926 /*
2927 * Looks like we failed to unmask 11b rates previously. This could
2928 * happen, e.g., if the interface was down at the point in time when a
2929 * P2P group was terminated.
2930 */
2931 wpa_printf(MSG_DEBUG,
2932 "nl80211: Interface %s mode is for non-P2P, but 11b rates were disabled - re-enable them",
2933 bss->ifname);
2934 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
2935}
2936
2937
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002938static int wpa_driver_nl80211_authenticate(
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002939 struct i802_bss *bss, struct wpa_driver_auth_params *params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002940{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002941 struct wpa_driver_nl80211_data *drv = bss->drv;
2942 int ret = -1, i;
2943 struct nl_msg *msg;
2944 enum nl80211_auth_type type;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002945 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002946 int count = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002947 int is_retry;
2948
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002949 nl80211_unmask_11b_rates(bss);
2950
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002951 is_retry = drv->retry_auth;
2952 drv->retry_auth = 0;
Dmitry Shmidt98660862014-03-11 17:26:21 -07002953 drv->ignore_deauth_event = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002954
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07002955 nl80211_mark_disconnected(drv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002956 os_memset(drv->auth_bssid, 0, ETH_ALEN);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07002957 if (params->bssid)
2958 os_memcpy(drv->auth_attempt_bssid, params->bssid, ETH_ALEN);
2959 else
2960 os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002961 /* FIX: IBSS mode */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002962 nlmode = params->p2p ?
2963 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
2964 if (drv->nlmode != nlmode &&
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002965 wpa_driver_nl80211_set_mode(bss, nlmode) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002966 return -1;
2967
2968retry:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002969 wpa_printf(MSG_DEBUG, "nl80211: Authenticate (ifindex=%d)",
2970 drv->ifindex);
2971
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002972 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_AUTHENTICATE);
2973 if (!msg)
2974 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002975
2976 for (i = 0; i < 4; i++) {
2977 if (!params->wep_key[i])
2978 continue;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002979 wpa_driver_nl80211_set_key(bss->ifname, bss, WPA_ALG_WEP,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002980 NULL, i,
2981 i == params->wep_tx_keyidx, NULL, 0,
2982 params->wep_key[i],
2983 params->wep_key_len[i]);
2984 if (params->wep_tx_keyidx != i)
2985 continue;
2986 if (nl_add_key(msg, WPA_ALG_WEP, i, 1, NULL, 0,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002987 params->wep_key[i], params->wep_key_len[i]))
2988 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002989 }
2990
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002991 if (params->bssid) {
2992 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
2993 MAC2STR(params->bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002994 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid))
2995 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002996 }
2997 if (params->freq) {
2998 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002999 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq))
3000 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003001 }
3002 if (params->ssid) {
3003 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
3004 params->ssid, params->ssid_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003005 if (nla_put(msg, NL80211_ATTR_SSID, params->ssid_len,
3006 params->ssid))
3007 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003008 }
3009 wpa_hexdump(MSG_DEBUG, " * IEs", params->ie, params->ie_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003010 if (params->ie &&
3011 nla_put(msg, NL80211_ATTR_IE, params->ie_len, params->ie))
3012 goto fail;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003013 if (params->sae_data) {
3014 wpa_hexdump(MSG_DEBUG, " * SAE data", params->sae_data,
3015 params->sae_data_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003016 if (nla_put(msg, NL80211_ATTR_SAE_DATA, params->sae_data_len,
3017 params->sae_data))
3018 goto fail;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003019 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003020 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
3021 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
3022 else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
3023 type = NL80211_AUTHTYPE_SHARED_KEY;
3024 else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
3025 type = NL80211_AUTHTYPE_NETWORK_EAP;
3026 else if (params->auth_alg & WPA_AUTH_ALG_FT)
3027 type = NL80211_AUTHTYPE_FT;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003028 else if (params->auth_alg & WPA_AUTH_ALG_SAE)
3029 type = NL80211_AUTHTYPE_SAE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003030 else
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003031 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003032 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003033 if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE, type))
3034 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003035 if (params->local_state_change) {
3036 wpa_printf(MSG_DEBUG, " * Local state change only");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003037 if (nla_put_flag(msg, NL80211_ATTR_LOCAL_STATE_CHANGE))
3038 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003039 }
3040
3041 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3042 msg = NULL;
3043 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003044 wpa_dbg(drv->ctx, MSG_DEBUG,
3045 "nl80211: MLME command failed (auth): ret=%d (%s)",
3046 ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003047 count++;
3048 if (ret == -EALREADY && count == 1 && params->bssid &&
3049 !params->local_state_change) {
3050 /*
3051 * mac80211 does not currently accept new
3052 * authentication if we are already authenticated. As a
3053 * workaround, force deauthentication and try again.
3054 */
3055 wpa_printf(MSG_DEBUG, "nl80211: Retry authentication "
3056 "after forced deauthentication");
Dmitry Shmidt98660862014-03-11 17:26:21 -07003057 drv->ignore_deauth_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003058 wpa_driver_nl80211_deauthenticate(
3059 bss, params->bssid,
3060 WLAN_REASON_PREV_AUTH_NOT_VALID);
3061 nlmsg_free(msg);
3062 goto retry;
3063 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003064
3065 if (ret == -ENOENT && params->freq && !is_retry) {
3066 /*
3067 * cfg80211 has likely expired the BSS entry even
3068 * though it was previously available in our internal
3069 * BSS table. To recover quickly, start a single
3070 * channel scan on the specified channel.
3071 */
3072 struct wpa_driver_scan_params scan;
3073 int freqs[2];
3074
3075 os_memset(&scan, 0, sizeof(scan));
3076 scan.num_ssids = 1;
3077 if (params->ssid) {
3078 scan.ssids[0].ssid = params->ssid;
3079 scan.ssids[0].ssid_len = params->ssid_len;
3080 }
3081 freqs[0] = params->freq;
3082 freqs[1] = 0;
3083 scan.freqs = freqs;
3084 wpa_printf(MSG_DEBUG, "nl80211: Trigger single "
3085 "channel scan to refresh cfg80211 BSS "
3086 "entry");
3087 ret = wpa_driver_nl80211_scan(bss, &scan);
3088 if (ret == 0) {
3089 nl80211_copy_auth_params(drv, params);
3090 drv->scan_for_auth = 1;
3091 }
3092 } else if (is_retry) {
3093 /*
3094 * Need to indicate this with an event since the return
3095 * value from the retry is not delivered to core code.
3096 */
3097 union wpa_event_data event;
3098 wpa_printf(MSG_DEBUG, "nl80211: Authentication retry "
3099 "failed");
3100 os_memset(&event, 0, sizeof(event));
3101 os_memcpy(event.timeout_event.addr, drv->auth_bssid_,
3102 ETH_ALEN);
3103 wpa_supplicant_event(drv->ctx, EVENT_AUTH_TIMED_OUT,
3104 &event);
3105 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003106 } else {
3107 wpa_printf(MSG_DEBUG,
3108 "nl80211: Authentication request send successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003109 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003110
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003111fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003112 nlmsg_free(msg);
3113 return ret;
3114}
3115
3116
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003117int wpa_driver_nl80211_authenticate_retry(struct wpa_driver_nl80211_data *drv)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003118{
3119 struct wpa_driver_auth_params params;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003120 struct i802_bss *bss = drv->first_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003121 int i;
3122
3123 wpa_printf(MSG_DEBUG, "nl80211: Try to authenticate again");
3124
3125 os_memset(&params, 0, sizeof(params));
3126 params.freq = drv->auth_freq;
3127 params.auth_alg = drv->auth_alg;
3128 params.wep_tx_keyidx = drv->auth_wep_tx_keyidx;
3129 params.local_state_change = drv->auth_local_state_change;
3130 params.p2p = drv->auth_p2p;
3131
3132 if (!is_zero_ether_addr(drv->auth_bssid_))
3133 params.bssid = drv->auth_bssid_;
3134
3135 if (drv->auth_ssid_len) {
3136 params.ssid = drv->auth_ssid;
3137 params.ssid_len = drv->auth_ssid_len;
3138 }
3139
3140 params.ie = drv->auth_ie;
3141 params.ie_len = drv->auth_ie_len;
3142
3143 for (i = 0; i < 4; i++) {
3144 if (drv->auth_wep_key_len[i]) {
3145 params.wep_key[i] = drv->auth_wep_key[i];
3146 params.wep_key_len[i] = drv->auth_wep_key_len[i];
3147 }
3148 }
3149
3150 drv->retry_auth = 1;
3151 return wpa_driver_nl80211_authenticate(bss, &params);
3152}
3153
3154
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003155static int wpa_driver_nl80211_send_frame(struct i802_bss *bss,
3156 const void *data, size_t len,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003157 int encrypt, int noack,
3158 unsigned int freq, int no_cck,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003159 int offchanok, unsigned int wait_time,
3160 const u16 *csa_offs,
3161 size_t csa_offs_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003162{
3163 struct wpa_driver_nl80211_data *drv = bss->drv;
3164 u64 cookie;
Dmitry Shmidt051af732013-10-22 13:52:46 -07003165 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003166
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07003167 if (freq == 0 && drv->nlmode == NL80211_IFTYPE_ADHOC) {
3168 freq = nl80211_get_assoc_freq(drv);
3169 wpa_printf(MSG_DEBUG,
3170 "nl80211: send_frame - Use assoc_freq=%u for IBSS",
3171 freq);
3172 }
Dmitry Shmidt56052862013-10-04 10:23:25 -07003173 if (freq == 0) {
3174 wpa_printf(MSG_DEBUG, "nl80211: send_frame - Use bss->freq=%u",
3175 bss->freq);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003176 freq = bss->freq;
Dmitry Shmidt56052862013-10-04 10:23:25 -07003177 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003178
Dmitry Shmidt56052862013-10-04 10:23:25 -07003179 if (drv->use_monitor) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003180 wpa_printf(MSG_DEBUG, "nl80211: send_frame(freq=%u bss->freq=%u) -> send_monitor",
Dmitry Shmidt56052862013-10-04 10:23:25 -07003181 freq, bss->freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003182 return nl80211_send_monitor(drv, data, len, encrypt, noack);
Dmitry Shmidt56052862013-10-04 10:23:25 -07003183 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003184
Dmitry Shmidt56052862013-10-04 10:23:25 -07003185 wpa_printf(MSG_DEBUG, "nl80211: send_frame -> send_frame_cmd");
Dmitry Shmidt051af732013-10-22 13:52:46 -07003186 res = nl80211_send_frame_cmd(bss, freq, wait_time, data, len,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003187 &cookie, no_cck, noack, offchanok,
3188 csa_offs, csa_offs_len);
Dmitry Shmidt051af732013-10-22 13:52:46 -07003189 if (res == 0 && !noack) {
3190 const struct ieee80211_mgmt *mgmt;
3191 u16 fc;
3192
3193 mgmt = (const struct ieee80211_mgmt *) data;
3194 fc = le_to_host16(mgmt->frame_control);
3195 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
3196 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_ACTION) {
3197 wpa_printf(MSG_MSGDUMP,
3198 "nl80211: Update send_action_cookie from 0x%llx to 0x%llx",
3199 (long long unsigned int)
3200 drv->send_action_cookie,
3201 (long long unsigned int) cookie);
3202 drv->send_action_cookie = cookie;
3203 }
3204 }
3205
3206 return res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003207}
3208
3209
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003210static int wpa_driver_nl80211_send_mlme(struct i802_bss *bss, const u8 *data,
3211 size_t data_len, int noack,
3212 unsigned int freq, int no_cck,
3213 int offchanok,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003214 unsigned int wait_time,
3215 const u16 *csa_offs,
3216 size_t csa_offs_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003217{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003218 struct wpa_driver_nl80211_data *drv = bss->drv;
3219 struct ieee80211_mgmt *mgmt;
3220 int encrypt = 1;
3221 u16 fc;
3222
3223 mgmt = (struct ieee80211_mgmt *) data;
3224 fc = le_to_host16(mgmt->frame_control);
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07003225 wpa_printf(MSG_DEBUG, "nl80211: send_mlme - da= " MACSTR
3226 " noack=%d freq=%u no_cck=%d offchanok=%d wait_time=%u fc=0x%x (%s) nlmode=%d",
3227 MAC2STR(mgmt->da), noack, freq, no_cck, offchanok, wait_time,
3228 fc, fc2str(fc), drv->nlmode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003229
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003230 if ((is_sta_interface(drv->nlmode) ||
3231 drv->nlmode == NL80211_IFTYPE_P2P_DEVICE) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003232 WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
3233 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_RESP) {
3234 /*
3235 * The use of last_mgmt_freq is a bit of a hack,
3236 * but it works due to the single-threaded nature
3237 * of wpa_supplicant.
3238 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07003239 if (freq == 0) {
3240 wpa_printf(MSG_DEBUG, "nl80211: Use last_mgmt_freq=%d",
3241 drv->last_mgmt_freq);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003242 freq = drv->last_mgmt_freq;
Dmitry Shmidt56052862013-10-04 10:23:25 -07003243 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003244 return nl80211_send_frame_cmd(bss, freq, 0,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003245 data, data_len, NULL, 1, noack,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003246 1, csa_offs, csa_offs_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003247 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003248
3249 if (drv->device_ap_sme && is_ap_interface(drv->nlmode)) {
Dmitry Shmidt56052862013-10-04 10:23:25 -07003250 if (freq == 0) {
3251 wpa_printf(MSG_DEBUG, "nl80211: Use bss->freq=%d",
3252 bss->freq);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003253 freq = bss->freq;
Dmitry Shmidt56052862013-10-04 10:23:25 -07003254 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003255 return nl80211_send_frame_cmd(bss, freq,
3256 (int) freq == bss->freq ? 0 :
3257 wait_time,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003258 data, data_len,
3259 &drv->send_action_cookie,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003260 no_cck, noack, offchanok,
3261 csa_offs, csa_offs_len);
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07003262 }
Dmitry Shmidtb638fe72012-03-20 12:51:25 -07003263
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003264 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
3265 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) {
3266 /*
3267 * Only one of the authentication frame types is encrypted.
3268 * In order for static WEP encryption to work properly (i.e.,
3269 * to not encrypt the frame), we need to tell mac80211 about
3270 * the frames that must not be encrypted.
3271 */
3272 u16 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
3273 u16 auth_trans = le_to_host16(mgmt->u.auth.auth_transaction);
3274 if (auth_alg != WLAN_AUTH_SHARED_KEY || auth_trans != 3)
3275 encrypt = 0;
3276 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003277
Dmitry Shmidt56052862013-10-04 10:23:25 -07003278 wpa_printf(MSG_DEBUG, "nl80211: send_mlme -> send_frame");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003279 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003280 noack, freq, no_cck, offchanok,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003281 wait_time, csa_offs,
3282 csa_offs_len);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003283}
3284
3285
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003286static int nl80211_put_basic_rates(struct nl_msg *msg, const int *basic_rates)
3287{
3288 u8 rates[NL80211_MAX_SUPP_RATES];
3289 u8 rates_len = 0;
3290 int i;
3291
3292 if (!basic_rates)
3293 return 0;
3294
3295 for (i = 0; i < NL80211_MAX_SUPP_RATES && basic_rates[i] >= 0; i++)
3296 rates[rates_len++] = basic_rates[i] / 5;
3297
3298 return nla_put(msg, NL80211_ATTR_BSS_BASIC_RATES, rates_len, rates);
3299}
3300
3301
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003302static int nl80211_set_bss(struct i802_bss *bss, int cts, int preamble,
3303 int slot, int ht_opmode, int ap_isolate,
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003304 const int *basic_rates)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003305{
3306 struct wpa_driver_nl80211_data *drv = bss->drv;
3307 struct nl_msg *msg;
3308
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003309 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_BSS)) ||
3310 (cts >= 0 &&
3311 nla_put_u8(msg, NL80211_ATTR_BSS_CTS_PROT, cts)) ||
3312 (preamble >= 0 &&
3313 nla_put_u8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble)) ||
3314 (slot >= 0 &&
3315 nla_put_u8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot)) ||
3316 (ht_opmode >= 0 &&
3317 nla_put_u16(msg, NL80211_ATTR_BSS_HT_OPMODE, ht_opmode)) ||
3318 (ap_isolate >= 0 &&
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003319 nla_put_u8(msg, NL80211_ATTR_AP_ISOLATE, ap_isolate)) ||
3320 nl80211_put_basic_rates(msg, basic_rates)) {
3321 nlmsg_free(msg);
3322 return -ENOBUFS;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003323 }
3324
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003325 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003326}
3327
3328
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003329static int wpa_driver_nl80211_set_acl(void *priv,
3330 struct hostapd_acl_params *params)
3331{
3332 struct i802_bss *bss = priv;
3333 struct wpa_driver_nl80211_data *drv = bss->drv;
3334 struct nl_msg *msg;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003335 struct nl_msg *acl;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003336 unsigned int i;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003337 int ret;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003338
3339 if (!(drv->capa.max_acl_mac_addrs))
3340 return -ENOTSUP;
3341
3342 if (params->num_mac_acl > drv->capa.max_acl_mac_addrs)
3343 return -ENOTSUP;
3344
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003345 wpa_printf(MSG_DEBUG, "nl80211: Set %s ACL (num_mac_acl=%u)",
3346 params->acl_policy ? "Accept" : "Deny", params->num_mac_acl);
3347
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003348 acl = nlmsg_alloc();
3349 if (!acl)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003350 return -ENOMEM;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003351 for (i = 0; i < params->num_mac_acl; i++) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003352 if (nla_put(acl, i + 1, ETH_ALEN, params->mac_acl[i].addr)) {
3353 nlmsg_free(acl);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003354 return -ENOMEM;
3355 }
3356 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003357
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003358 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_MAC_ACL)) ||
3359 nla_put_u32(msg, NL80211_ATTR_ACL_POLICY, params->acl_policy ?
3360 NL80211_ACL_POLICY_DENY_UNLESS_LISTED :
3361 NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED) ||
3362 nla_put_nested(msg, NL80211_ATTR_MAC_ADDRS, acl)) {
3363 nlmsg_free(msg);
3364 nlmsg_free(acl);
3365 return -ENOMEM;
3366 }
3367 nlmsg_free(acl);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003368
3369 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003370 if (ret) {
3371 wpa_printf(MSG_DEBUG, "nl80211: Failed to set MAC ACL: %d (%s)",
3372 ret, strerror(-ret));
3373 }
3374
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003375 return ret;
3376}
3377
3378
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003379static int nl80211_put_beacon_int(struct nl_msg *msg, int beacon_int)
3380{
3381 if (beacon_int > 0) {
3382 wpa_printf(MSG_DEBUG, " * beacon_int=%d", beacon_int);
3383 return nla_put_u32(msg, NL80211_ATTR_BEACON_INTERVAL,
3384 beacon_int);
3385 }
3386
3387 return 0;
3388}
3389
3390
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003391static int wpa_driver_nl80211_set_ap(void *priv,
3392 struct wpa_driver_ap_params *params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003393{
3394 struct i802_bss *bss = priv;
3395 struct wpa_driver_nl80211_data *drv = bss->drv;
3396 struct nl_msg *msg;
3397 u8 cmd = NL80211_CMD_NEW_BEACON;
3398 int ret;
3399 int beacon_set;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003400 int num_suites;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003401 int smps_mode;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003402 u32 suites[10], suite;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003403 u32 ver;
3404
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003405 beacon_set = params->reenable ? 0 : bss->beacon_set;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003406
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003407 wpa_printf(MSG_DEBUG, "nl80211: Set beacon (beacon_set=%d)",
3408 beacon_set);
3409 if (beacon_set)
3410 cmd = NL80211_CMD_SET_BEACON;
3411
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003412 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon head",
3413 params->head, params->head_len);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003414 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon tail",
3415 params->tail, params->tail_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003416 wpa_printf(MSG_DEBUG, "nl80211: ifindex=%d", bss->ifindex);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003417 wpa_printf(MSG_DEBUG, "nl80211: beacon_int=%d", params->beacon_int);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003418 wpa_printf(MSG_DEBUG, "nl80211: dtim_period=%d", params->dtim_period);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003419 wpa_hexdump_ascii(MSG_DEBUG, "nl80211: ssid",
3420 params->ssid, params->ssid_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003421 if (!(msg = nl80211_bss_msg(bss, 0, cmd)) ||
3422 nla_put(msg, NL80211_ATTR_BEACON_HEAD, params->head_len,
3423 params->head) ||
3424 nla_put(msg, NL80211_ATTR_BEACON_TAIL, params->tail_len,
3425 params->tail) ||
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003426 nl80211_put_beacon_int(msg, params->beacon_int) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003427 nla_put_u32(msg, NL80211_ATTR_DTIM_PERIOD, params->dtim_period) ||
3428 nla_put(msg, NL80211_ATTR_SSID, params->ssid_len, params->ssid))
3429 goto fail;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003430 if (params->proberesp && params->proberesp_len) {
3431 wpa_hexdump(MSG_DEBUG, "nl80211: proberesp (offload)",
3432 params->proberesp, params->proberesp_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003433 if (nla_put(msg, NL80211_ATTR_PROBE_RESP, params->proberesp_len,
3434 params->proberesp))
3435 goto fail;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003436 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003437 switch (params->hide_ssid) {
3438 case NO_SSID_HIDING:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003439 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID not in use");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003440 if (nla_put_u32(msg, NL80211_ATTR_HIDDEN_SSID,
3441 NL80211_HIDDEN_SSID_NOT_IN_USE))
3442 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003443 break;
3444 case HIDDEN_SSID_ZERO_LEN:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003445 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero len");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003446 if (nla_put_u32(msg, NL80211_ATTR_HIDDEN_SSID,
3447 NL80211_HIDDEN_SSID_ZERO_LEN))
3448 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003449 break;
3450 case HIDDEN_SSID_ZERO_CONTENTS:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003451 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero contents");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003452 if (nla_put_u32(msg, NL80211_ATTR_HIDDEN_SSID,
3453 NL80211_HIDDEN_SSID_ZERO_CONTENTS))
3454 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003455 break;
3456 }
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003457 wpa_printf(MSG_DEBUG, "nl80211: privacy=%d", params->privacy);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003458 if (params->privacy &&
3459 nla_put_flag(msg, NL80211_ATTR_PRIVACY))
3460 goto fail;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003461 wpa_printf(MSG_DEBUG, "nl80211: auth_algs=0x%x", params->auth_algs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003462 if ((params->auth_algs & (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) ==
3463 (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) {
3464 /* Leave out the attribute */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003465 } else if (params->auth_algs & WPA_AUTH_ALG_SHARED) {
3466 if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE,
3467 NL80211_AUTHTYPE_SHARED_KEY))
3468 goto fail;
3469 } else {
3470 if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE,
3471 NL80211_AUTHTYPE_OPEN_SYSTEM))
3472 goto fail;
3473 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003474
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003475 wpa_printf(MSG_DEBUG, "nl80211: wpa_version=0x%x", params->wpa_version);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003476 ver = 0;
3477 if (params->wpa_version & WPA_PROTO_WPA)
3478 ver |= NL80211_WPA_VERSION_1;
3479 if (params->wpa_version & WPA_PROTO_RSN)
3480 ver |= NL80211_WPA_VERSION_2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003481 if (ver &&
3482 nla_put_u32(msg, NL80211_ATTR_WPA_VERSIONS, ver))
3483 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003484
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003485 wpa_printf(MSG_DEBUG, "nl80211: key_mgmt_suites=0x%x",
3486 params->key_mgmt_suites);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003487 num_suites = 0;
3488 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X)
3489 suites[num_suites++] = WLAN_AKM_SUITE_8021X;
3490 if (params->key_mgmt_suites & WPA_KEY_MGMT_PSK)
3491 suites[num_suites++] = WLAN_AKM_SUITE_PSK;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003492 if (num_suites &&
3493 nla_put(msg, NL80211_ATTR_AKM_SUITES, num_suites * sizeof(u32),
3494 suites))
3495 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003496
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003497 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
3498 params->pairwise_ciphers & (WPA_CIPHER_WEP104 | WPA_CIPHER_WEP40) &&
3499 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT))
3500 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003501
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003502 wpa_printf(MSG_DEBUG, "nl80211: pairwise_ciphers=0x%x",
3503 params->pairwise_ciphers);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003504 num_suites = wpa_cipher_to_cipher_suites(params->pairwise_ciphers,
3505 suites, ARRAY_SIZE(suites));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003506 if (num_suites &&
3507 nla_put(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
3508 num_suites * sizeof(u32), suites))
3509 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003510
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003511 wpa_printf(MSG_DEBUG, "nl80211: group_cipher=0x%x",
3512 params->group_cipher);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003513 suite = wpa_cipher_to_cipher_suite(params->group_cipher);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003514 if (suite &&
3515 nla_put_u32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, suite))
3516 goto fail;
3517
3518 switch (params->smps_mode) {
3519 case HT_CAP_INFO_SMPS_DYNAMIC:
3520 wpa_printf(MSG_DEBUG, "nl80211: SMPS mode - dynamic");
3521 smps_mode = NL80211_SMPS_DYNAMIC;
3522 break;
3523 case HT_CAP_INFO_SMPS_STATIC:
3524 wpa_printf(MSG_DEBUG, "nl80211: SMPS mode - static");
3525 smps_mode = NL80211_SMPS_STATIC;
3526 break;
3527 default:
3528 /* invalid - fallback to smps off */
3529 case HT_CAP_INFO_SMPS_DISABLED:
3530 wpa_printf(MSG_DEBUG, "nl80211: SMPS mode - off");
3531 smps_mode = NL80211_SMPS_OFF;
3532 break;
3533 }
3534 if (nla_put_u32(msg, NL80211_ATTR_SMPS_MODE, smps_mode))
3535 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003536
3537 if (params->beacon_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003538 wpa_hexdump_buf(MSG_DEBUG, "nl80211: beacon_ies",
3539 params->beacon_ies);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003540 if (nla_put(msg, NL80211_ATTR_IE,
3541 wpabuf_len(params->beacon_ies),
3542 wpabuf_head(params->beacon_ies)))
3543 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003544 }
3545 if (params->proberesp_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003546 wpa_hexdump_buf(MSG_DEBUG, "nl80211: proberesp_ies",
3547 params->proberesp_ies);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003548 if (nla_put(msg, NL80211_ATTR_IE_PROBE_RESP,
3549 wpabuf_len(params->proberesp_ies),
3550 wpabuf_head(params->proberesp_ies)))
3551 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003552 }
3553 if (params->assocresp_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003554 wpa_hexdump_buf(MSG_DEBUG, "nl80211: assocresp_ies",
3555 params->assocresp_ies);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003556 if (nla_put(msg, NL80211_ATTR_IE_ASSOC_RESP,
3557 wpabuf_len(params->assocresp_ies),
3558 wpabuf_head(params->assocresp_ies)))
3559 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003560 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003561
Dmitry Shmidt04949592012-07-19 12:16:46 -07003562 if (drv->capa.flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003563 wpa_printf(MSG_DEBUG, "nl80211: ap_max_inactivity=%d",
3564 params->ap_max_inactivity);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003565 if (nla_put_u16(msg, NL80211_ATTR_INACTIVITY_TIMEOUT,
3566 params->ap_max_inactivity))
3567 goto fail;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003568 }
3569
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003570#ifdef CONFIG_P2P
3571 if (params->p2p_go_ctwindow > 0) {
3572 if (drv->p2p_go_ctwindow_supported) {
3573 wpa_printf(MSG_DEBUG, "nl80211: P2P GO ctwindow=%d",
3574 params->p2p_go_ctwindow);
3575 if (nla_put_u8(msg, NL80211_ATTR_P2P_CTWINDOW,
3576 params->p2p_go_ctwindow))
3577 goto fail;
3578 } else {
3579 wpa_printf(MSG_INFO,
3580 "nl80211: Driver does not support CTWindow configuration - ignore this parameter");
3581 }
3582 }
3583#endif /* CONFIG_P2P */
3584
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003585 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3586 if (ret) {
3587 wpa_printf(MSG_DEBUG, "nl80211: Beacon set failed: %d (%s)",
3588 ret, strerror(-ret));
3589 } else {
3590 bss->beacon_set = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003591 nl80211_set_bss(bss, params->cts_protect, params->preamble,
3592 params->short_slot_time, params->ht_opmode,
3593 params->isolate, params->basic_rates);
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07003594 if (beacon_set && params->freq &&
3595 params->freq->bandwidth != bss->bandwidth) {
3596 wpa_printf(MSG_DEBUG,
3597 "nl80211: Update BSS %s bandwidth: %d -> %d",
3598 bss->ifname, bss->bandwidth,
3599 params->freq->bandwidth);
3600 ret = nl80211_set_channel(bss, params->freq, 1);
3601 if (ret) {
3602 wpa_printf(MSG_DEBUG,
3603 "nl80211: Frequency set failed: %d (%s)",
3604 ret, strerror(-ret));
3605 } else {
3606 wpa_printf(MSG_DEBUG,
3607 "nl80211: Frequency set succeeded for ht2040 coex");
3608 bss->bandwidth = params->freq->bandwidth;
3609 }
3610 } else if (!beacon_set) {
3611 /*
3612 * cfg80211 updates the driver on frequence change in AP
3613 * mode only at the point when beaconing is started, so
3614 * set the initial value here.
3615 */
3616 bss->bandwidth = params->freq->bandwidth;
3617 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003618 }
3619 return ret;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003620fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003621 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003622 return -ENOBUFS;
3623}
3624
3625
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003626static int nl80211_put_freq_params(struct nl_msg *msg,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003627 const struct hostapd_freq_params *freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003628{
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003629 wpa_printf(MSG_DEBUG, " * freq=%d", freq->freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003630 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq->freq))
3631 return -ENOBUFS;
3632
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003633 wpa_printf(MSG_DEBUG, " * vht_enabled=%d", freq->vht_enabled);
3634 wpa_printf(MSG_DEBUG, " * ht_enabled=%d", freq->ht_enabled);
3635
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003636 if (freq->vht_enabled) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003637 enum nl80211_chan_width cw;
3638
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003639 wpa_printf(MSG_DEBUG, " * bandwidth=%d", freq->bandwidth);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003640 switch (freq->bandwidth) {
3641 case 20:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003642 cw = NL80211_CHAN_WIDTH_20;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003643 break;
3644 case 40:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003645 cw = NL80211_CHAN_WIDTH_40;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003646 break;
3647 case 80:
3648 if (freq->center_freq2)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003649 cw = NL80211_CHAN_WIDTH_80P80;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003650 else
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003651 cw = NL80211_CHAN_WIDTH_80;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003652 break;
3653 case 160:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003654 cw = NL80211_CHAN_WIDTH_160;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003655 break;
3656 default:
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003657 return -EINVAL;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003658 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003659
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003660 wpa_printf(MSG_DEBUG, " * channel_width=%d", cw);
3661 wpa_printf(MSG_DEBUG, " * center_freq1=%d",
3662 freq->center_freq1);
3663 wpa_printf(MSG_DEBUG, " * center_freq2=%d",
3664 freq->center_freq2);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003665 if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, cw) ||
3666 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1,
3667 freq->center_freq1) ||
3668 (freq->center_freq2 &&
3669 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2,
3670 freq->center_freq2)))
3671 return -ENOBUFS;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003672 } else if (freq->ht_enabled) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003673 enum nl80211_channel_type ct;
3674
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003675 wpa_printf(MSG_DEBUG, " * sec_channel_offset=%d",
3676 freq->sec_channel_offset);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003677 switch (freq->sec_channel_offset) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003678 case -1:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003679 ct = NL80211_CHAN_HT40MINUS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003680 break;
3681 case 1:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003682 ct = NL80211_CHAN_HT40PLUS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003683 break;
3684 default:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003685 ct = NL80211_CHAN_HT20;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003686 break;
3687 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003688
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003689 wpa_printf(MSG_DEBUG, " * channel_type=%d", ct);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003690 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, ct))
3691 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003692 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003693 return 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003694}
3695
3696
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07003697static int nl80211_set_channel(struct i802_bss *bss,
3698 struct hostapd_freq_params *freq, int set_chan)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003699{
3700 struct wpa_driver_nl80211_data *drv = bss->drv;
3701 struct nl_msg *msg;
3702 int ret;
3703
3704 wpa_printf(MSG_DEBUG,
3705 "nl80211: Set freq %d (ht_enabled=%d, vht_enabled=%d, bandwidth=%d MHz, cf1=%d MHz, cf2=%d MHz)",
3706 freq->freq, freq->ht_enabled, freq->vht_enabled,
3707 freq->bandwidth, freq->center_freq1, freq->center_freq2);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003708
3709 msg = nl80211_drv_msg(drv, 0, set_chan ? NL80211_CMD_SET_CHANNEL :
3710 NL80211_CMD_SET_WIPHY);
3711 if (!msg || nl80211_put_freq_params(msg, freq) < 0) {
3712 nlmsg_free(msg);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003713 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003714 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003715
3716 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003717 if (ret == 0) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003718 bss->freq = freq->freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003719 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003720 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003721 wpa_printf(MSG_DEBUG, "nl80211: Failed to set channel (freq=%d): "
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003722 "%d (%s)", freq->freq, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003723 return -1;
3724}
3725
3726
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003727static u32 sta_flags_nl80211(int flags)
3728{
3729 u32 f = 0;
3730
3731 if (flags & WPA_STA_AUTHORIZED)
3732 f |= BIT(NL80211_STA_FLAG_AUTHORIZED);
3733 if (flags & WPA_STA_WMM)
3734 f |= BIT(NL80211_STA_FLAG_WME);
3735 if (flags & WPA_STA_SHORT_PREAMBLE)
3736 f |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
3737 if (flags & WPA_STA_MFP)
3738 f |= BIT(NL80211_STA_FLAG_MFP);
3739 if (flags & WPA_STA_TDLS_PEER)
3740 f |= BIT(NL80211_STA_FLAG_TDLS_PEER);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003741 if (flags & WPA_STA_AUTHENTICATED)
3742 f |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003743
3744 return f;
3745}
3746
3747
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003748#ifdef CONFIG_MESH
3749static u32 sta_plink_state_nl80211(enum mesh_plink_state state)
3750{
3751 switch (state) {
3752 case PLINK_LISTEN:
3753 return NL80211_PLINK_LISTEN;
3754 case PLINK_OPEN_SENT:
3755 return NL80211_PLINK_OPN_SNT;
3756 case PLINK_OPEN_RCVD:
3757 return NL80211_PLINK_OPN_RCVD;
3758 case PLINK_CNF_RCVD:
3759 return NL80211_PLINK_CNF_RCVD;
3760 case PLINK_ESTAB:
3761 return NL80211_PLINK_ESTAB;
3762 case PLINK_HOLDING:
3763 return NL80211_PLINK_HOLDING;
3764 case PLINK_BLOCKED:
3765 return NL80211_PLINK_BLOCKED;
3766 default:
3767 wpa_printf(MSG_ERROR, "nl80211: Invalid mesh plink state %d",
3768 state);
3769 }
3770 return -1;
3771}
3772#endif /* CONFIG_MESH */
3773
3774
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003775static int wpa_driver_nl80211_sta_add(void *priv,
3776 struct hostapd_sta_add_params *params)
3777{
3778 struct i802_bss *bss = priv;
3779 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003780 struct nl_msg *msg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003781 struct nl80211_sta_flag_update upd;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003782 int ret = -ENOBUFS;
3783
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003784 if ((params->flags & WPA_STA_TDLS_PEER) &&
3785 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
3786 return -EOPNOTSUPP;
3787
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003788 wpa_printf(MSG_DEBUG, "nl80211: %s STA " MACSTR,
3789 params->set ? "Set" : "Add", MAC2STR(params->addr));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003790 msg = nl80211_bss_msg(bss, 0, params->set ? NL80211_CMD_SET_STATION :
3791 NL80211_CMD_NEW_STATION);
3792 if (!msg || nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr))
3793 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003794
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003795 if (!params->set || (params->flags & WPA_STA_TDLS_PEER)) {
3796 wpa_hexdump(MSG_DEBUG, " * supported rates",
3797 params->supp_rates, params->supp_rates_len);
3798 wpa_printf(MSG_DEBUG, " * capability=0x%x",
3799 params->capability);
3800 if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_RATES,
3801 params->supp_rates_len, params->supp_rates) ||
3802 nla_put_u16(msg, NL80211_ATTR_STA_CAPABILITY,
3803 params->capability))
3804 goto fail;
3805
3806 if (params->ht_capabilities) {
3807 wpa_hexdump(MSG_DEBUG, " * ht_capabilities",
3808 (u8 *) params->ht_capabilities,
3809 sizeof(*params->ht_capabilities));
3810 if (nla_put(msg, NL80211_ATTR_HT_CAPABILITY,
3811 sizeof(*params->ht_capabilities),
3812 params->ht_capabilities))
3813 goto fail;
3814 }
3815
3816 if (params->vht_capabilities) {
3817 wpa_hexdump(MSG_DEBUG, " * vht_capabilities",
3818 (u8 *) params->vht_capabilities,
3819 sizeof(*params->vht_capabilities));
3820 if (nla_put(msg, NL80211_ATTR_VHT_CAPABILITY,
3821 sizeof(*params->vht_capabilities),
3822 params->vht_capabilities))
3823 goto fail;
3824 }
3825
3826 if (params->ext_capab) {
3827 wpa_hexdump(MSG_DEBUG, " * ext_capab",
3828 params->ext_capab, params->ext_capab_len);
3829 if (nla_put(msg, NL80211_ATTR_STA_EXT_CAPABILITY,
3830 params->ext_capab_len, params->ext_capab))
3831 goto fail;
3832 }
3833 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003834 if (!params->set) {
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07003835 if (params->aid) {
3836 wpa_printf(MSG_DEBUG, " * aid=%u", params->aid);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003837 if (nla_put_u16(msg, NL80211_ATTR_STA_AID, params->aid))
3838 goto fail;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07003839 } else {
3840 /*
3841 * cfg80211 validates that AID is non-zero, so we have
3842 * to make this a non-zero value for the TDLS case where
3843 * a dummy STA entry is used for now.
3844 */
3845 wpa_printf(MSG_DEBUG, " * aid=1 (TDLS workaround)");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003846 if (nla_put_u16(msg, NL80211_ATTR_STA_AID, 1))
3847 goto fail;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07003848 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003849 wpa_printf(MSG_DEBUG, " * listen_interval=%u",
3850 params->listen_interval);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003851 if (nla_put_u16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
3852 params->listen_interval))
3853 goto fail;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003854 } else if (params->aid && (params->flags & WPA_STA_TDLS_PEER)) {
3855 wpa_printf(MSG_DEBUG, " * peer_aid=%u", params->aid);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003856 if (nla_put_u16(msg, NL80211_ATTR_PEER_AID, params->aid))
3857 goto fail;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003858 }
3859
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08003860 if (params->vht_opmode_enabled) {
3861 wpa_printf(MSG_DEBUG, " * opmode=%u", params->vht_opmode);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003862 if (nla_put_u8(msg, NL80211_ATTR_OPMODE_NOTIF,
3863 params->vht_opmode))
3864 goto fail;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003865 }
3866
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003867 if (params->supp_channels) {
3868 wpa_hexdump(MSG_DEBUG, " * supported channels",
3869 params->supp_channels, params->supp_channels_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003870 if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_CHANNELS,
3871 params->supp_channels_len, params->supp_channels))
3872 goto fail;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003873 }
3874
3875 if (params->supp_oper_classes) {
3876 wpa_hexdump(MSG_DEBUG, " * supported operating classes",
3877 params->supp_oper_classes,
3878 params->supp_oper_classes_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003879 if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES,
3880 params->supp_oper_classes_len,
3881 params->supp_oper_classes))
3882 goto fail;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003883 }
3884
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003885 os_memset(&upd, 0, sizeof(upd));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003886 upd.set = sta_flags_nl80211(params->flags);
3887 upd.mask = upd.set | sta_flags_nl80211(params->flags_mask);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003888 wpa_printf(MSG_DEBUG, " * flags set=0x%x mask=0x%x",
3889 upd.set, upd.mask);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003890 if (nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd))
3891 goto fail;
3892
3893#ifdef CONFIG_MESH
3894 if (params->plink_state &&
3895 nla_put_u8(msg, NL80211_ATTR_STA_PLINK_STATE,
3896 sta_plink_state_nl80211(params->plink_state)))
3897 goto fail;
3898#endif /* CONFIG_MESH */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003899
3900 if (params->flags & WPA_STA_WMM) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003901 struct nlattr *wme = nla_nest_start(msg, NL80211_ATTR_STA_WME);
3902
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003903 wpa_printf(MSG_DEBUG, " * qosinfo=0x%x", params->qosinfo);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003904 if (!wme ||
3905 nla_put_u8(msg, NL80211_STA_WME_UAPSD_QUEUES,
3906 params->qosinfo & WMM_QOSINFO_STA_AC_MASK) ||
3907 nla_put_u8(msg, NL80211_STA_WME_MAX_SP,
3908 (params->qosinfo >> WMM_QOSINFO_STA_SP_SHIFT) &
3909 WMM_QOSINFO_STA_SP_MASK))
3910 goto fail;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003911 nla_nest_end(msg, wme);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003912 }
3913
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003914 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003915 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003916 if (ret)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003917 wpa_printf(MSG_DEBUG, "nl80211: NL80211_CMD_%s_STATION "
3918 "result: %d (%s)", params->set ? "SET" : "NEW", ret,
3919 strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003920 if (ret == -EEXIST)
3921 ret = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003922fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003923 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003924 return ret;
3925}
3926
3927
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003928static void rtnl_neigh_delete_fdb_entry(struct i802_bss *bss, const u8 *addr)
3929{
3930#ifdef CONFIG_LIBNL3_ROUTE
3931 struct wpa_driver_nl80211_data *drv = bss->drv;
3932 struct rtnl_neigh *rn;
3933 struct nl_addr *nl_addr;
3934 int err;
3935
3936 rn = rtnl_neigh_alloc();
3937 if (!rn)
3938 return;
3939
3940 rtnl_neigh_set_family(rn, AF_BRIDGE);
3941 rtnl_neigh_set_ifindex(rn, bss->ifindex);
3942 nl_addr = nl_addr_build(AF_BRIDGE, (void *) addr, ETH_ALEN);
3943 if (!nl_addr) {
3944 rtnl_neigh_put(rn);
3945 return;
3946 }
3947 rtnl_neigh_set_lladdr(rn, nl_addr);
3948
3949 err = rtnl_neigh_delete(drv->rtnl_sk, rn, 0);
3950 if (err < 0) {
3951 wpa_printf(MSG_DEBUG, "nl80211: bridge FDB entry delete for "
3952 MACSTR " ifindex=%d failed: %s", MAC2STR(addr),
3953 bss->ifindex, nl_geterror(err));
3954 } else {
3955 wpa_printf(MSG_DEBUG, "nl80211: deleted bridge FDB entry for "
3956 MACSTR, MAC2STR(addr));
3957 }
3958
3959 nl_addr_put(nl_addr);
3960 rtnl_neigh_put(rn);
3961#endif /* CONFIG_LIBNL3_ROUTE */
3962}
3963
3964
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003965static int wpa_driver_nl80211_sta_remove(struct i802_bss *bss, const u8 *addr,
3966 int deauth, u16 reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003967{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003968 struct wpa_driver_nl80211_data *drv = bss->drv;
3969 struct nl_msg *msg;
3970 int ret;
3971
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003972 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_DEL_STATION)) ||
3973 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
3974 (deauth == 0 &&
3975 nla_put_u8(msg, NL80211_ATTR_MGMT_SUBTYPE,
3976 WLAN_FC_STYPE_DISASSOC)) ||
3977 (deauth == 1 &&
3978 nla_put_u8(msg, NL80211_ATTR_MGMT_SUBTYPE,
3979 WLAN_FC_STYPE_DEAUTH)) ||
3980 (reason_code &&
3981 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code))) {
3982 nlmsg_free(msg);
3983 return -ENOBUFS;
3984 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003985
3986 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07003987 wpa_printf(MSG_DEBUG, "nl80211: sta_remove -> DEL_STATION %s " MACSTR
3988 " --> %d (%s)",
3989 bss->ifname, MAC2STR(addr), ret, strerror(-ret));
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003990
3991 if (drv->rtnl_sk)
3992 rtnl_neigh_delete_fdb_entry(bss, addr);
3993
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003994 if (ret == -ENOENT)
3995 return 0;
3996 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003997}
3998
3999
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004000void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv, int ifidx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004001{
4002 struct nl_msg *msg;
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07004003 struct wpa_driver_nl80211_data *drv2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004004
4005 wpa_printf(MSG_DEBUG, "nl80211: Remove interface ifindex=%d", ifidx);
4006
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004007 /* stop listening for EAPOL on this interface */
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07004008 dl_list_for_each(drv2, &drv->global->interfaces,
4009 struct wpa_driver_nl80211_data, list)
4010 del_ifidx(drv2, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004011
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004012 msg = nl80211_ifindex_msg(drv, ifidx, 0, NL80211_CMD_DEL_INTERFACE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004013 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
4014 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004015 wpa_printf(MSG_ERROR, "Failed to remove interface (ifidx=%d)", ifidx);
4016}
4017
4018
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004019static const char * nl80211_iftype_str(enum nl80211_iftype mode)
4020{
4021 switch (mode) {
4022 case NL80211_IFTYPE_ADHOC:
4023 return "ADHOC";
4024 case NL80211_IFTYPE_STATION:
4025 return "STATION";
4026 case NL80211_IFTYPE_AP:
4027 return "AP";
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07004028 case NL80211_IFTYPE_AP_VLAN:
4029 return "AP_VLAN";
4030 case NL80211_IFTYPE_WDS:
4031 return "WDS";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004032 case NL80211_IFTYPE_MONITOR:
4033 return "MONITOR";
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07004034 case NL80211_IFTYPE_MESH_POINT:
4035 return "MESH_POINT";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004036 case NL80211_IFTYPE_P2P_CLIENT:
4037 return "P2P_CLIENT";
4038 case NL80211_IFTYPE_P2P_GO:
4039 return "P2P_GO";
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004040 case NL80211_IFTYPE_P2P_DEVICE:
4041 return "P2P_DEVICE";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004042 default:
4043 return "unknown";
4044 }
4045}
4046
4047
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004048static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv,
4049 const char *ifname,
4050 enum nl80211_iftype iftype,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004051 const u8 *addr, int wds,
4052 int (*handler)(struct nl_msg *, void *),
4053 void *arg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004054{
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004055 struct nl_msg *msg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004056 int ifidx;
4057 int ret = -ENOBUFS;
4058
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004059 wpa_printf(MSG_DEBUG, "nl80211: Create interface iftype %d (%s)",
4060 iftype, nl80211_iftype_str(iftype));
4061
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004062 msg = nl80211_cmd_msg(drv->first_bss, 0, NL80211_CMD_NEW_INTERFACE);
4063 if (!msg ||
4064 nla_put_string(msg, NL80211_ATTR_IFNAME, ifname) ||
4065 nla_put_u32(msg, NL80211_ATTR_IFTYPE, iftype))
4066 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004067
4068 if (iftype == NL80211_IFTYPE_MONITOR) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004069 struct nlattr *flags;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004070
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004071 flags = nla_nest_start(msg, NL80211_ATTR_MNTR_FLAGS);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004072 if (!flags ||
4073 nla_put_flag(msg, NL80211_MNTR_FLAG_COOK_FRAMES))
4074 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004075
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004076 nla_nest_end(msg, flags);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004077 } else if (wds) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004078 if (nla_put_u8(msg, NL80211_ATTR_4ADDR, wds))
4079 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004080 }
4081
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07004082 /*
4083 * Tell cfg80211 that the interface belongs to the socket that created
4084 * it, and the interface should be deleted when the socket is closed.
4085 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004086 if (nla_put_flag(msg, NL80211_ATTR_IFACE_SOCKET_OWNER))
4087 goto fail;
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07004088
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004089 ret = send_and_recv_msgs(drv, msg, handler, arg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004090 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004091 if (ret) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004092 fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004093 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004094 wpa_printf(MSG_ERROR, "Failed to create interface %s: %d (%s)",
4095 ifname, ret, strerror(-ret));
4096 return ret;
4097 }
4098
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004099 if (iftype == NL80211_IFTYPE_P2P_DEVICE)
4100 return 0;
4101
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004102 ifidx = if_nametoindex(ifname);
4103 wpa_printf(MSG_DEBUG, "nl80211: New interface %s created: ifindex=%d",
4104 ifname, ifidx);
4105
4106 if (ifidx <= 0)
4107 return -1;
4108
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07004109 /*
4110 * Some virtual interfaces need to process EAPOL packets and events on
4111 * the parent interface. This is used mainly with hostapd.
4112 */
4113 if (drv->hostapd ||
4114 iftype == NL80211_IFTYPE_AP_VLAN ||
4115 iftype == NL80211_IFTYPE_WDS ||
4116 iftype == NL80211_IFTYPE_MONITOR) {
4117 /* start listening for EAPOL on this interface */
4118 add_ifidx(drv, ifidx);
4119 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004120
4121 if (addr && iftype != NL80211_IFTYPE_MONITOR &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004122 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname, addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004123 nl80211_remove_iface(drv, ifidx);
4124 return -1;
4125 }
4126
4127 return ifidx;
4128}
4129
4130
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004131int nl80211_create_iface(struct wpa_driver_nl80211_data *drv,
4132 const char *ifname, enum nl80211_iftype iftype,
4133 const u8 *addr, int wds,
4134 int (*handler)(struct nl_msg *, void *),
4135 void *arg, int use_existing)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004136{
4137 int ret;
4138
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004139 ret = nl80211_create_iface_once(drv, ifname, iftype, addr, wds, handler,
4140 arg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004141
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004142 /* if error occurred and interface exists already */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004143 if (ret == -ENFILE && if_nametoindex(ifname)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004144 if (use_existing) {
4145 wpa_printf(MSG_DEBUG, "nl80211: Continue using existing interface %s",
4146 ifname);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07004147 if (addr && iftype != NL80211_IFTYPE_MONITOR &&
4148 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
4149 addr) < 0 &&
4150 (linux_set_iface_flags(drv->global->ioctl_sock,
4151 ifname, 0) < 0 ||
4152 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
4153 addr) < 0 ||
4154 linux_set_iface_flags(drv->global->ioctl_sock,
4155 ifname, 1) < 0))
4156 return -1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004157 return -ENFILE;
4158 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004159 wpa_printf(MSG_INFO, "Try to remove and re-create %s", ifname);
4160
4161 /* Try to remove the interface that was already there. */
4162 nl80211_remove_iface(drv, if_nametoindex(ifname));
4163
4164 /* Try to create the interface again */
4165 ret = nl80211_create_iface_once(drv, ifname, iftype, addr,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004166 wds, handler, arg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004167 }
4168
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004169 if (ret >= 0 && is_p2p_net_interface(iftype)) {
4170 wpa_printf(MSG_DEBUG,
4171 "nl80211: Interface %s created for P2P - disable 11b rates",
4172 ifname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004173 nl80211_disable_11b_rates(drv, ret, 1);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004174 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004175
4176 return ret;
4177}
4178
4179
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004180static int nl80211_setup_ap(struct i802_bss *bss)
4181{
4182 struct wpa_driver_nl80211_data *drv = bss->drv;
4183
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004184 wpa_printf(MSG_DEBUG, "nl80211: Setup AP(%s) - device_ap_sme=%d use_monitor=%d",
4185 bss->ifname, drv->device_ap_sme, drv->use_monitor);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004186
4187 /*
4188 * Disable Probe Request reporting unless we need it in this way for
4189 * devices that include the AP SME, in the other case (unless using
4190 * monitor iface) we'll get it through the nl_mgmt socket instead.
4191 */
4192 if (!drv->device_ap_sme)
4193 wpa_driver_nl80211_probe_req_report(bss, 0);
4194
4195 if (!drv->device_ap_sme && !drv->use_monitor)
4196 if (nl80211_mgmt_subscribe_ap(bss))
4197 return -1;
4198
4199 if (drv->device_ap_sme && !drv->use_monitor)
4200 if (nl80211_mgmt_subscribe_ap_dev_sme(bss))
4201 return -1;
4202
4203 if (!drv->device_ap_sme && drv->use_monitor &&
4204 nl80211_create_monitor_interface(drv) &&
4205 !drv->device_ap_sme)
Dmitry Shmidt04949592012-07-19 12:16:46 -07004206 return -1;
4207
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004208 if (drv->device_ap_sme &&
4209 wpa_driver_nl80211_probe_req_report(bss, 1) < 0) {
4210 wpa_printf(MSG_DEBUG, "nl80211: Failed to enable "
4211 "Probe Request frame reporting in AP mode");
4212 /* Try to survive without this */
4213 }
4214
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004215 return 0;
4216}
4217
4218
4219static void nl80211_teardown_ap(struct i802_bss *bss)
4220{
4221 struct wpa_driver_nl80211_data *drv = bss->drv;
4222
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004223 wpa_printf(MSG_DEBUG, "nl80211: Teardown AP(%s) - device_ap_sme=%d use_monitor=%d",
4224 bss->ifname, drv->device_ap_sme, drv->use_monitor);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004225 if (drv->device_ap_sme) {
4226 wpa_driver_nl80211_probe_req_report(bss, 0);
4227 if (!drv->use_monitor)
4228 nl80211_mgmt_unsubscribe(bss, "AP teardown (dev SME)");
4229 } else if (drv->use_monitor)
4230 nl80211_remove_monitor_interface(drv);
4231 else
4232 nl80211_mgmt_unsubscribe(bss, "AP teardown");
4233
4234 bss->beacon_set = 0;
4235}
4236
4237
4238static int nl80211_send_eapol_data(struct i802_bss *bss,
4239 const u8 *addr, const u8 *data,
4240 size_t data_len)
4241{
4242 struct sockaddr_ll ll;
4243 int ret;
4244
4245 if (bss->drv->eapol_tx_sock < 0) {
4246 wpa_printf(MSG_DEBUG, "nl80211: No socket to send EAPOL");
4247 return -1;
4248 }
4249
4250 os_memset(&ll, 0, sizeof(ll));
4251 ll.sll_family = AF_PACKET;
4252 ll.sll_ifindex = bss->ifindex;
4253 ll.sll_protocol = htons(ETH_P_PAE);
4254 ll.sll_halen = ETH_ALEN;
4255 os_memcpy(ll.sll_addr, addr, ETH_ALEN);
4256 ret = sendto(bss->drv->eapol_tx_sock, data, data_len, 0,
4257 (struct sockaddr *) &ll, sizeof(ll));
4258 if (ret < 0)
4259 wpa_printf(MSG_ERROR, "nl80211: EAPOL TX: %s",
4260 strerror(errno));
4261
4262 return ret;
4263}
4264
4265
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004266static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
4267
4268static int wpa_driver_nl80211_hapd_send_eapol(
4269 void *priv, const u8 *addr, const u8 *data,
4270 size_t data_len, int encrypt, const u8 *own_addr, u32 flags)
4271{
4272 struct i802_bss *bss = priv;
4273 struct wpa_driver_nl80211_data *drv = bss->drv;
4274 struct ieee80211_hdr *hdr;
4275 size_t len;
4276 u8 *pos;
4277 int res;
4278 int qos = flags & WPA_STA_WMM;
Dmitry Shmidt641185e2013-11-06 15:17:13 -08004279
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004280 if (drv->device_ap_sme || !drv->use_monitor)
4281 return nl80211_send_eapol_data(bss, addr, data, data_len);
4282
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004283 len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
4284 data_len;
4285 hdr = os_zalloc(len);
4286 if (hdr == NULL) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004287 wpa_printf(MSG_INFO, "nl80211: Failed to allocate EAPOL buffer(len=%lu)",
4288 (unsigned long) len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004289 return -1;
4290 }
4291
4292 hdr->frame_control =
4293 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
4294 hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
4295 if (encrypt)
4296 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
4297 if (qos) {
4298 hdr->frame_control |=
4299 host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
4300 }
4301
4302 memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
4303 memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
4304 memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
4305 pos = (u8 *) (hdr + 1);
4306
4307 if (qos) {
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004308 /* Set highest priority in QoS header */
4309 pos[0] = 7;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004310 pos[1] = 0;
4311 pos += 2;
4312 }
4313
4314 memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
4315 pos += sizeof(rfc1042_header);
4316 WPA_PUT_BE16(pos, ETH_P_PAE);
4317 pos += 2;
4318 memcpy(pos, data, data_len);
4319
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004320 res = wpa_driver_nl80211_send_frame(bss, (u8 *) hdr, len, encrypt, 0,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004321 0, 0, 0, 0, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004322 if (res < 0) {
4323 wpa_printf(MSG_ERROR, "i802_send_eapol - packet len: %lu - "
4324 "failed: %d (%s)",
4325 (unsigned long) len, errno, strerror(errno));
4326 }
4327 os_free(hdr);
4328
4329 return res;
4330}
4331
4332
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004333static int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004334 unsigned int total_flags,
4335 unsigned int flags_or,
4336 unsigned int flags_and)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004337{
4338 struct i802_bss *bss = priv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004339 struct nl_msg *msg;
4340 struct nlattr *flags;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004341 struct nl80211_sta_flag_update upd;
4342
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004343 wpa_printf(MSG_DEBUG, "nl80211: Set STA flags - ifname=%s addr=" MACSTR
4344 " total_flags=0x%x flags_or=0x%x flags_and=0x%x authorized=%d",
4345 bss->ifname, MAC2STR(addr), total_flags, flags_or, flags_and,
4346 !!(total_flags & WPA_STA_AUTHORIZED));
4347
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004348 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
4349 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
4350 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004351
4352 /*
4353 * Backwards compatibility version using NL80211_ATTR_STA_FLAGS. This
4354 * can be removed eventually.
4355 */
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004356 flags = nla_nest_start(msg, NL80211_ATTR_STA_FLAGS);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004357 if (!flags ||
4358 ((total_flags & WPA_STA_AUTHORIZED) &&
4359 nla_put_flag(msg, NL80211_STA_FLAG_AUTHORIZED)) ||
4360 ((total_flags & WPA_STA_WMM) &&
4361 nla_put_flag(msg, NL80211_STA_FLAG_WME)) ||
4362 ((total_flags & WPA_STA_SHORT_PREAMBLE) &&
4363 nla_put_flag(msg, NL80211_STA_FLAG_SHORT_PREAMBLE)) ||
4364 ((total_flags & WPA_STA_MFP) &&
4365 nla_put_flag(msg, NL80211_STA_FLAG_MFP)) ||
4366 ((total_flags & WPA_STA_TDLS_PEER) &&
4367 nla_put_flag(msg, NL80211_STA_FLAG_TDLS_PEER)))
4368 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004369
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004370 nla_nest_end(msg, flags);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004371
4372 os_memset(&upd, 0, sizeof(upd));
4373 upd.mask = sta_flags_nl80211(flags_or | ~flags_and);
4374 upd.set = sta_flags_nl80211(flags_or);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004375 if (nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd))
4376 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004377
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004378 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
4379fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004380 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004381 return -ENOBUFS;
4382}
4383
4384
4385static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv,
4386 struct wpa_driver_associate_params *params)
4387{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004388 enum nl80211_iftype nlmode, old_mode;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004389
4390 if (params->p2p) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004391 wpa_printf(MSG_DEBUG, "nl80211: Setup AP operations for P2P "
4392 "group (GO)");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004393 nlmode = NL80211_IFTYPE_P2P_GO;
4394 } else
4395 nlmode = NL80211_IFTYPE_AP;
4396
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004397 old_mode = drv->nlmode;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004398 if (wpa_driver_nl80211_set_mode(drv->first_bss, nlmode)) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004399 nl80211_remove_monitor_interface(drv);
4400 return -1;
4401 }
4402
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08004403 if (params->freq.freq &&
4404 nl80211_set_channel(drv->first_bss, &params->freq, 0)) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004405 if (old_mode != nlmode)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004406 wpa_driver_nl80211_set_mode(drv->first_bss, old_mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004407 nl80211_remove_monitor_interface(drv);
4408 return -1;
4409 }
4410
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004411 return 0;
4412}
4413
4414
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004415static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv,
4416 int reset_mode)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004417{
4418 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004419 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004420
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004421 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_LEAVE_IBSS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004422 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004423 if (ret) {
4424 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS failed: ret=%d "
4425 "(%s)", ret, strerror(-ret));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004426 } else {
4427 wpa_printf(MSG_DEBUG,
4428 "nl80211: Leave IBSS request sent successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004429 }
4430
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004431 if (reset_mode &&
4432 wpa_driver_nl80211_set_mode(drv->first_bss,
Dmitry Shmidt56052862013-10-04 10:23:25 -07004433 NL80211_IFTYPE_STATION)) {
4434 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
4435 "station mode");
4436 }
4437
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004438 return ret;
4439}
4440
4441
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08004442static int nl80211_ht_vht_overrides(struct nl_msg *msg,
4443 struct wpa_driver_associate_params *params)
4444{
4445 if (params->disable_ht && nla_put_flag(msg, NL80211_ATTR_DISABLE_HT))
4446 return -1;
4447
4448 if (params->htcaps && params->htcaps_mask) {
4449 int sz = sizeof(struct ieee80211_ht_capabilities);
4450 wpa_hexdump(MSG_DEBUG, " * htcaps", params->htcaps, sz);
4451 wpa_hexdump(MSG_DEBUG, " * htcaps_mask",
4452 params->htcaps_mask, sz);
4453 if (nla_put(msg, NL80211_ATTR_HT_CAPABILITY, sz,
4454 params->htcaps) ||
4455 nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK, sz,
4456 params->htcaps_mask))
4457 return -1;
4458 }
4459
4460#ifdef CONFIG_VHT_OVERRIDES
4461 if (params->disable_vht) {
4462 wpa_printf(MSG_DEBUG, " * VHT disabled");
4463 if (nla_put_flag(msg, NL80211_ATTR_DISABLE_VHT))
4464 return -1;
4465 }
4466
4467 if (params->vhtcaps && params->vhtcaps_mask) {
4468 int sz = sizeof(struct ieee80211_vht_capabilities);
4469 wpa_hexdump(MSG_DEBUG, " * vhtcaps", params->vhtcaps, sz);
4470 wpa_hexdump(MSG_DEBUG, " * vhtcaps_mask",
4471 params->vhtcaps_mask, sz);
4472 if (nla_put(msg, NL80211_ATTR_VHT_CAPABILITY, sz,
4473 params->vhtcaps) ||
4474 nla_put(msg, NL80211_ATTR_VHT_CAPABILITY_MASK, sz,
4475 params->vhtcaps_mask))
4476 return -1;
4477 }
4478#endif /* CONFIG_VHT_OVERRIDES */
4479
4480 return 0;
4481}
4482
4483
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004484static int wpa_driver_nl80211_ibss(struct wpa_driver_nl80211_data *drv,
4485 struct wpa_driver_associate_params *params)
4486{
4487 struct nl_msg *msg;
4488 int ret = -1;
4489 int count = 0;
4490
4491 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS (ifindex=%d)", drv->ifindex);
4492
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004493 if (wpa_driver_nl80211_set_mode_ibss(drv->first_bss, &params->freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004494 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
4495 "IBSS mode");
4496 return -1;
4497 }
4498
4499retry:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004500 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_JOIN_IBSS)) ||
4501 params->ssid == NULL || params->ssid_len > sizeof(drv->ssid))
4502 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004503
4504 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
4505 params->ssid, params->ssid_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004506 if (nla_put(msg, NL80211_ATTR_SSID, params->ssid_len, params->ssid))
4507 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004508 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
4509 drv->ssid_len = params->ssid_len;
4510
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004511 if (nl80211_put_freq_params(msg, &params->freq) < 0 ||
4512 nl80211_put_beacon_int(msg, params->beacon_int))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004513 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004514
4515 ret = nl80211_set_conn_keys(params, msg);
4516 if (ret)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004517 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004518
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004519 if (params->bssid && params->fixed_bssid) {
4520 wpa_printf(MSG_DEBUG, " * BSSID=" MACSTR,
4521 MAC2STR(params->bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004522 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid))
4523 goto fail;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004524 }
4525
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004526 if (params->fixed_freq) {
4527 wpa_printf(MSG_DEBUG, " * fixed_freq");
4528 if (nla_put_flag(msg, NL80211_ATTR_FREQ_FIXED))
4529 goto fail;
4530 }
4531
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004532 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
4533 params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
4534 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
4535 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004536 wpa_printf(MSG_DEBUG, " * control port");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004537 if (nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT))
4538 goto fail;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004539 }
4540
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004541 if (params->wpa_ie) {
4542 wpa_hexdump(MSG_DEBUG,
4543 " * Extra IEs for Beacon/Probe Response frames",
4544 params->wpa_ie, params->wpa_ie_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004545 if (nla_put(msg, NL80211_ATTR_IE, params->wpa_ie_len,
4546 params->wpa_ie))
4547 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004548 }
4549
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08004550 if (nl80211_ht_vht_overrides(msg, params) < 0)
4551 return -1;
4552
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004553 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4554 msg = NULL;
4555 if (ret) {
4556 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS failed: ret=%d (%s)",
4557 ret, strerror(-ret));
4558 count++;
4559 if (ret == -EALREADY && count == 1) {
4560 wpa_printf(MSG_DEBUG, "nl80211: Retry IBSS join after "
4561 "forced leave");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004562 nl80211_leave_ibss(drv, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004563 nlmsg_free(msg);
4564 goto retry;
4565 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004566 } else {
4567 wpa_printf(MSG_DEBUG,
4568 "nl80211: Join IBSS request sent successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004569 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004570
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004571fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004572 nlmsg_free(msg);
4573 return ret;
4574}
4575
4576
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004577static int nl80211_connect_common(struct wpa_driver_nl80211_data *drv,
4578 struct wpa_driver_associate_params *params,
4579 struct nl_msg *msg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004580{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004581 if (params->bssid) {
4582 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
4583 MAC2STR(params->bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004584 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid))
4585 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004586 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004587
Dmitry Shmidt96be6222014-02-13 10:16:51 -08004588 if (params->bssid_hint) {
4589 wpa_printf(MSG_DEBUG, " * bssid_hint=" MACSTR,
4590 MAC2STR(params->bssid_hint));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004591 if (nla_put(msg, NL80211_ATTR_MAC_HINT, ETH_ALEN,
4592 params->bssid_hint))
4593 return -1;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08004594 }
4595
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004596 if (params->freq.freq) {
4597 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq.freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004598 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
4599 params->freq.freq))
4600 return -1;
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004601 drv->assoc_freq = params->freq.freq;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004602 } else
4603 drv->assoc_freq = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004604
Dmitry Shmidt96be6222014-02-13 10:16:51 -08004605 if (params->freq_hint) {
4606 wpa_printf(MSG_DEBUG, " * freq_hint=%d", params->freq_hint);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004607 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ_HINT,
4608 params->freq_hint))
4609 return -1;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08004610 }
4611
Dmitry Shmidt04949592012-07-19 12:16:46 -07004612 if (params->bg_scan_period >= 0) {
4613 wpa_printf(MSG_DEBUG, " * bg scan period=%d",
4614 params->bg_scan_period);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004615 if (nla_put_u16(msg, NL80211_ATTR_BG_SCAN_PERIOD,
4616 params->bg_scan_period))
4617 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004618 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004619
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004620 if (params->ssid) {
4621 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
4622 params->ssid, params->ssid_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004623 if (nla_put(msg, NL80211_ATTR_SSID, params->ssid_len,
4624 params->ssid))
4625 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004626 if (params->ssid_len > sizeof(drv->ssid))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004627 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004628 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
4629 drv->ssid_len = params->ssid_len;
4630 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004631
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004632 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004633 if (params->wpa_ie &&
4634 nla_put(msg, NL80211_ATTR_IE, params->wpa_ie_len, params->wpa_ie))
4635 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004636
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004637 if (params->wpa_proto) {
4638 enum nl80211_wpa_versions ver = 0;
4639
4640 if (params->wpa_proto & WPA_PROTO_WPA)
4641 ver |= NL80211_WPA_VERSION_1;
4642 if (params->wpa_proto & WPA_PROTO_RSN)
4643 ver |= NL80211_WPA_VERSION_2;
4644
4645 wpa_printf(MSG_DEBUG, " * WPA Versions 0x%x", ver);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004646 if (nla_put_u32(msg, NL80211_ATTR_WPA_VERSIONS, ver))
4647 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004648 }
4649
4650 if (params->pairwise_suite != WPA_CIPHER_NONE) {
4651 u32 cipher = wpa_cipher_to_cipher_suite(params->pairwise_suite);
4652 wpa_printf(MSG_DEBUG, " * pairwise=0x%x", cipher);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004653 if (nla_put_u32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
4654 cipher))
4655 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004656 }
4657
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08004658 if (params->group_suite == WPA_CIPHER_GTK_NOT_USED &&
4659 !(drv->capa.enc & WPA_DRIVER_CAPA_ENC_GTK_NOT_USED)) {
4660 /*
4661 * This is likely to work even though many drivers do not
4662 * advertise support for operations without GTK.
4663 */
4664 wpa_printf(MSG_DEBUG, " * skip group cipher configuration for GTK_NOT_USED due to missing driver support advertisement");
4665 } else if (params->group_suite != WPA_CIPHER_NONE) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004666 u32 cipher = wpa_cipher_to_cipher_suite(params->group_suite);
4667 wpa_printf(MSG_DEBUG, " * group=0x%x", cipher);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004668 if (nla_put_u32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher))
4669 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004670 }
4671
4672 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
4673 params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
4674 params->key_mgmt_suite == WPA_KEY_MGMT_FT_IEEE8021X ||
4675 params->key_mgmt_suite == WPA_KEY_MGMT_FT_PSK ||
Dmitry Shmidt15907092014-03-25 10:42:57 -07004676 params->key_mgmt_suite == WPA_KEY_MGMT_CCKM ||
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -07004677 params->key_mgmt_suite == WPA_KEY_MGMT_OSEN ||
4678 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004679 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256 ||
Dmitry Shmidt807291d2015-01-27 13:40:23 -08004680 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SUITE_B ||
4681 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004682 int mgmt = WLAN_AKM_SUITE_PSK;
4683
4684 switch (params->key_mgmt_suite) {
4685 case WPA_KEY_MGMT_CCKM:
4686 mgmt = WLAN_AKM_SUITE_CCKM;
4687 break;
4688 case WPA_KEY_MGMT_IEEE8021X:
4689 mgmt = WLAN_AKM_SUITE_8021X;
4690 break;
4691 case WPA_KEY_MGMT_FT_IEEE8021X:
4692 mgmt = WLAN_AKM_SUITE_FT_8021X;
4693 break;
4694 case WPA_KEY_MGMT_FT_PSK:
4695 mgmt = WLAN_AKM_SUITE_FT_PSK;
4696 break;
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -07004697 case WPA_KEY_MGMT_IEEE8021X_SHA256:
4698 mgmt = WLAN_AKM_SUITE_8021X_SHA256;
4699 break;
4700 case WPA_KEY_MGMT_PSK_SHA256:
4701 mgmt = WLAN_AKM_SUITE_PSK_SHA256;
4702 break;
Dmitry Shmidt15907092014-03-25 10:42:57 -07004703 case WPA_KEY_MGMT_OSEN:
4704 mgmt = WLAN_AKM_SUITE_OSEN;
4705 break;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004706 case WPA_KEY_MGMT_IEEE8021X_SUITE_B:
4707 mgmt = WLAN_AKM_SUITE_8021X_SUITE_B;
4708 break;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08004709 case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192:
4710 mgmt = WLAN_AKM_SUITE_8021X_SUITE_B_192;
4711 break;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004712 case WPA_KEY_MGMT_PSK:
4713 default:
4714 mgmt = WLAN_AKM_SUITE_PSK;
4715 break;
4716 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07004717 wpa_printf(MSG_DEBUG, " * akm=0x%x", mgmt);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004718 if (nla_put_u32(msg, NL80211_ATTR_AKM_SUITES, mgmt))
4719 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004720 }
4721
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004722 if (nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT))
4723 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004724
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004725 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED &&
4726 nla_put_u32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED))
4727 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004728
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004729 if (params->rrm_used) {
4730 u32 drv_rrm_flags = drv->capa.rrm_flags;
4731 if (!(drv_rrm_flags &
4732 WPA_DRIVER_FLAGS_DS_PARAM_SET_IE_IN_PROBES) ||
4733 !(drv_rrm_flags & WPA_DRIVER_FLAGS_QUIET) ||
4734 nla_put_flag(msg, NL80211_ATTR_USE_RRM))
4735 return -1;
4736 }
4737
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08004738 if (nl80211_ht_vht_overrides(msg, params) < 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004739 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004740
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004741 if (params->p2p)
4742 wpa_printf(MSG_DEBUG, " * P2P group");
4743
4744 return 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004745}
4746
4747
4748static int wpa_driver_nl80211_try_connect(
4749 struct wpa_driver_nl80211_data *drv,
4750 struct wpa_driver_associate_params *params)
4751{
4752 struct nl_msg *msg;
4753 enum nl80211_auth_type type;
4754 int ret;
4755 int algs;
4756
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004757#ifdef CONFIG_DRIVER_NL80211_QCA
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004758 if (params->req_key_mgmt_offload && params->psk &&
4759 (params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
4760 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256 ||
4761 params->key_mgmt_suite == WPA_KEY_MGMT_FT_PSK)) {
4762 wpa_printf(MSG_DEBUG, "nl80211: Key management set PSK");
4763 ret = issue_key_mgmt_set_key(drv, params->psk, 32);
4764 if (ret)
4765 return ret;
4766 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004767#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004768
4769 wpa_printf(MSG_DEBUG, "nl80211: Connect (ifindex=%d)", drv->ifindex);
4770 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_CONNECT);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004771 if (!msg)
4772 return -1;
4773
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004774 ret = nl80211_connect_common(drv, params, msg);
4775 if (ret)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004776 goto fail;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004777
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004778 algs = 0;
4779 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
4780 algs++;
4781 if (params->auth_alg & WPA_AUTH_ALG_SHARED)
4782 algs++;
4783 if (params->auth_alg & WPA_AUTH_ALG_LEAP)
4784 algs++;
4785 if (algs > 1) {
4786 wpa_printf(MSG_DEBUG, " * Leave out Auth Type for automatic "
4787 "selection");
4788 goto skip_auth_type;
4789 }
4790
4791 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
4792 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
4793 else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
4794 type = NL80211_AUTHTYPE_SHARED_KEY;
4795 else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
4796 type = NL80211_AUTHTYPE_NETWORK_EAP;
4797 else if (params->auth_alg & WPA_AUTH_ALG_FT)
4798 type = NL80211_AUTHTYPE_FT;
4799 else
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004800 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004801
4802 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004803 if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE, type))
4804 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004805
4806skip_auth_type:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004807 ret = nl80211_set_conn_keys(params, msg);
4808 if (ret)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004809 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004810
4811 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4812 msg = NULL;
4813 if (ret) {
4814 wpa_printf(MSG_DEBUG, "nl80211: MLME connect failed: ret=%d "
4815 "(%s)", ret, strerror(-ret));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004816 } else {
4817 wpa_printf(MSG_DEBUG,
4818 "nl80211: Connect request send successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004819 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004820
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004821fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004822 nlmsg_free(msg);
4823 return ret;
4824
4825}
4826
4827
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004828static int wpa_driver_nl80211_connect(
4829 struct wpa_driver_nl80211_data *drv,
4830 struct wpa_driver_associate_params *params)
4831{
Jithu Jancea7c60b42014-12-03 18:54:40 +05304832 int ret;
4833
4834 /* Store the connection attempted bssid for future use */
4835 if (params->bssid)
4836 os_memcpy(drv->auth_attempt_bssid, params->bssid, ETH_ALEN);
4837 else
4838 os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
4839
4840 ret = wpa_driver_nl80211_try_connect(drv, params);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004841 if (ret == -EALREADY) {
4842 /*
4843 * cfg80211 does not currently accept new connections if
4844 * we are already connected. As a workaround, force
4845 * disconnection and try again.
4846 */
4847 wpa_printf(MSG_DEBUG, "nl80211: Explicitly "
4848 "disconnecting before reassociation "
4849 "attempt");
4850 if (wpa_driver_nl80211_disconnect(
4851 drv, WLAN_REASON_PREV_AUTH_NOT_VALID))
4852 return -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004853 ret = wpa_driver_nl80211_try_connect(drv, params);
4854 }
4855 return ret;
4856}
4857
4858
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004859static int wpa_driver_nl80211_associate(
4860 void *priv, struct wpa_driver_associate_params *params)
4861{
4862 struct i802_bss *bss = priv;
4863 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004864 int ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004865 struct nl_msg *msg;
4866
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004867 nl80211_unmask_11b_rates(bss);
4868
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004869 if (params->mode == IEEE80211_MODE_AP)
4870 return wpa_driver_nl80211_ap(drv, params);
4871
4872 if (params->mode == IEEE80211_MODE_IBSS)
4873 return wpa_driver_nl80211_ibss(drv, params);
4874
4875 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004876 enum nl80211_iftype nlmode = params->p2p ?
4877 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
4878
4879 if (wpa_driver_nl80211_set_mode(priv, nlmode) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004880 return -1;
4881 return wpa_driver_nl80211_connect(drv, params);
4882 }
4883
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004884 nl80211_mark_disconnected(drv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004885
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004886 wpa_printf(MSG_DEBUG, "nl80211: Associate (ifindex=%d)",
4887 drv->ifindex);
4888 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_ASSOCIATE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004889 if (!msg)
4890 return -1;
4891
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004892 ret = nl80211_connect_common(drv, params, msg);
4893 if (ret)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004894 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004895
4896 if (params->prev_bssid) {
4897 wpa_printf(MSG_DEBUG, " * prev_bssid=" MACSTR,
4898 MAC2STR(params->prev_bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004899 if (nla_put(msg, NL80211_ATTR_PREV_BSSID, ETH_ALEN,
4900 params->prev_bssid))
4901 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004902 }
4903
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004904 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4905 msg = NULL;
4906 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004907 wpa_dbg(drv->ctx, MSG_DEBUG,
4908 "nl80211: MLME command failed (assoc): ret=%d (%s)",
4909 ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004910 nl80211_dump_scan(drv);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004911 } else {
4912 wpa_printf(MSG_DEBUG,
4913 "nl80211: Association request send successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004914 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004915
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004916fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004917 nlmsg_free(msg);
4918 return ret;
4919}
4920
4921
4922static int nl80211_set_mode(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004923 int ifindex, enum nl80211_iftype mode)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004924{
4925 struct nl_msg *msg;
4926 int ret = -ENOBUFS;
4927
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004928 wpa_printf(MSG_DEBUG, "nl80211: Set mode ifindex %d iftype %d (%s)",
4929 ifindex, mode, nl80211_iftype_str(mode));
4930
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004931 msg = nl80211_cmd_msg(drv->first_bss, 0, NL80211_CMD_SET_INTERFACE);
4932 if (!msg || nla_put_u32(msg, NL80211_ATTR_IFTYPE, mode))
4933 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004934
4935 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004936 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004937 if (!ret)
4938 return 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004939fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004940 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004941 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface %d to mode %d:"
4942 " %d (%s)", ifindex, mode, ret, strerror(-ret));
4943 return ret;
4944}
4945
4946
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004947static int wpa_driver_nl80211_set_mode_impl(
4948 struct i802_bss *bss,
4949 enum nl80211_iftype nlmode,
4950 struct hostapd_freq_params *desired_freq_params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004951{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004952 struct wpa_driver_nl80211_data *drv = bss->drv;
4953 int ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004954 int i;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004955 int was_ap = is_ap_interface(drv->nlmode);
4956 int res;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004957 int mode_switch_res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004958
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004959 mode_switch_res = nl80211_set_mode(drv, drv->ifindex, nlmode);
4960 if (mode_switch_res && nlmode == nl80211_get_ifmode(bss))
4961 mode_switch_res = 0;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004962
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004963 if (mode_switch_res == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004964 drv->nlmode = nlmode;
4965 ret = 0;
4966 goto done;
4967 }
4968
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004969 if (mode_switch_res == -ENODEV)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004970 return -1;
4971
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004972 if (nlmode == drv->nlmode) {
4973 wpa_printf(MSG_DEBUG, "nl80211: Interface already in "
4974 "requested mode - ignore error");
4975 ret = 0;
4976 goto done; /* Already in the requested mode */
4977 }
4978
4979 /* mac80211 doesn't allow mode changes while the device is up, so
4980 * take the device down, try to set the mode again, and bring the
4981 * device back up.
4982 */
4983 wpa_printf(MSG_DEBUG, "nl80211: Try mode change after setting "
4984 "interface down");
4985 for (i = 0; i < 10; i++) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004986 res = i802_set_iface_flags(bss, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004987 if (res == -EACCES || res == -ENODEV)
4988 break;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004989 if (res != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004990 wpa_printf(MSG_DEBUG, "nl80211: Failed to set "
4991 "interface down");
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004992 os_sleep(0, 100000);
4993 continue;
4994 }
4995
4996 /*
4997 * Setting the mode will fail for some drivers if the phy is
4998 * on a frequency that the mode is disallowed in.
4999 */
5000 if (desired_freq_params) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005001 res = nl80211_set_channel(bss, desired_freq_params, 0);
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005002 if (res) {
5003 wpa_printf(MSG_DEBUG,
5004 "nl80211: Failed to set frequency on interface");
5005 }
5006 }
5007
5008 /* Try to set the mode again while the interface is down */
5009 mode_switch_res = nl80211_set_mode(drv, drv->ifindex, nlmode);
5010 if (mode_switch_res == -EBUSY) {
5011 wpa_printf(MSG_DEBUG,
5012 "nl80211: Delaying mode set while interface going down");
5013 os_sleep(0, 100000);
5014 continue;
5015 }
5016 ret = mode_switch_res;
5017 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005018 }
5019
5020 if (!ret) {
5021 wpa_printf(MSG_DEBUG, "nl80211: Mode change succeeded while "
5022 "interface is down");
5023 drv->nlmode = nlmode;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005024 drv->ignore_if_down_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005025 }
5026
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005027 /* Bring the interface back up */
5028 res = linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1);
5029 if (res != 0) {
5030 wpa_printf(MSG_DEBUG,
5031 "nl80211: Failed to set interface up after switching mode");
5032 ret = -1;
5033 }
5034
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005035done:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005036 if (ret) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005037 wpa_printf(MSG_DEBUG, "nl80211: Interface mode change to %d "
5038 "from %d failed", nlmode, drv->nlmode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005039 return ret;
5040 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005041
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005042 if (is_p2p_net_interface(nlmode)) {
5043 wpa_printf(MSG_DEBUG,
5044 "nl80211: Interface %s mode change to P2P - disable 11b rates",
5045 bss->ifname);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005046 nl80211_disable_11b_rates(drv, drv->ifindex, 1);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005047 } else if (drv->disabled_11b_rates) {
5048 wpa_printf(MSG_DEBUG,
5049 "nl80211: Interface %s mode changed to non-P2P - re-enable 11b rates",
5050 bss->ifname);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005051 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005052 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005053
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005054 if (is_ap_interface(nlmode)) {
5055 nl80211_mgmt_unsubscribe(bss, "start AP");
5056 /* Setup additional AP mode functionality if needed */
5057 if (nl80211_setup_ap(bss))
5058 return -1;
5059 } else if (was_ap) {
5060 /* Remove additional AP mode functionality */
5061 nl80211_teardown_ap(bss);
5062 } else {
5063 nl80211_mgmt_unsubscribe(bss, "mode change");
5064 }
5065
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005066 if (is_mesh_interface(nlmode) &&
5067 nl80211_mgmt_subscribe_mesh(bss))
5068 return -1;
5069
Dmitry Shmidt04949592012-07-19 12:16:46 -07005070 if (!bss->in_deinit && !is_ap_interface(nlmode) &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005071 !is_mesh_interface(nlmode) &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005072 nl80211_mgmt_subscribe_non_ap(bss) < 0)
5073 wpa_printf(MSG_DEBUG, "nl80211: Failed to register Action "
5074 "frame processing - ignore for now");
5075
5076 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005077}
5078
5079
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005080int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
5081 enum nl80211_iftype nlmode)
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005082{
5083 return wpa_driver_nl80211_set_mode_impl(bss, nlmode, NULL);
5084}
5085
5086
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07005087static int wpa_driver_nl80211_set_mode_ibss(struct i802_bss *bss,
5088 struct hostapd_freq_params *freq)
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005089{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005090 return wpa_driver_nl80211_set_mode_impl(bss, NL80211_IFTYPE_ADHOC,
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07005091 freq);
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005092}
5093
5094
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005095static int wpa_driver_nl80211_get_capa(void *priv,
5096 struct wpa_driver_capa *capa)
5097{
5098 struct i802_bss *bss = priv;
5099 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07005100
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005101 if (!drv->has_capability)
5102 return -1;
5103 os_memcpy(capa, &drv->capa, sizeof(*capa));
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005104 if (drv->extended_capa && drv->extended_capa_mask) {
5105 capa->extended_capa = drv->extended_capa;
5106 capa->extended_capa_mask = drv->extended_capa_mask;
5107 capa->extended_capa_len = drv->extended_capa_len;
5108 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005109
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005110 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005111}
5112
5113
5114static int wpa_driver_nl80211_set_operstate(void *priv, int state)
5115{
5116 struct i802_bss *bss = priv;
5117 struct wpa_driver_nl80211_data *drv = bss->drv;
5118
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005119 wpa_printf(MSG_DEBUG, "nl80211: Set %s operstate %d->%d (%s)",
5120 bss->ifname, drv->operstate, state,
5121 state ? "UP" : "DORMANT");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005122 drv->operstate = state;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005123 return netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, -1,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005124 state ? IF_OPER_UP : IF_OPER_DORMANT);
5125}
5126
5127
5128static int wpa_driver_nl80211_set_supp_port(void *priv, int authorized)
5129{
5130 struct i802_bss *bss = priv;
5131 struct wpa_driver_nl80211_data *drv = bss->drv;
5132 struct nl_msg *msg;
5133 struct nl80211_sta_flag_update upd;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005134 int ret;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005135
5136 if (!drv->associated && is_zero_ether_addr(drv->bssid) && !authorized) {
5137 wpa_printf(MSG_DEBUG, "nl80211: Skip set_supp_port(unauthorized) while not associated");
5138 return 0;
5139 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005140
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005141 wpa_printf(MSG_DEBUG, "nl80211: Set supplicant port %sauthorized for "
5142 MACSTR, authorized ? "" : "un", MAC2STR(drv->bssid));
5143
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005144 os_memset(&upd, 0, sizeof(upd));
5145 upd.mask = BIT(NL80211_STA_FLAG_AUTHORIZED);
5146 if (authorized)
5147 upd.set = BIT(NL80211_STA_FLAG_AUTHORIZED);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005148
5149 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
5150 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid) ||
5151 nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd)) {
5152 nlmsg_free(msg);
5153 return -ENOBUFS;
5154 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005155
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005156 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005157 if (!ret)
5158 return 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005159 wpa_printf(MSG_DEBUG, "nl80211: Failed to set STA flag: %d (%s)",
5160 ret, strerror(-ret));
5161 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005162}
5163
5164
Jouni Malinen75ecf522011-06-27 15:19:46 -07005165/* Set kernel driver on given frequency (MHz) */
5166static int i802_set_freq(void *priv, struct hostapd_freq_params *freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005167{
Jouni Malinen75ecf522011-06-27 15:19:46 -07005168 struct i802_bss *bss = priv;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07005169 return nl80211_set_channel(bss, freq, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005170}
5171
5172
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005173static inline int min_int(int a, int b)
5174{
5175 if (a < b)
5176 return a;
5177 return b;
5178}
5179
5180
5181static int get_key_handler(struct nl_msg *msg, void *arg)
5182{
5183 struct nlattr *tb[NL80211_ATTR_MAX + 1];
5184 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
5185
5186 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
5187 genlmsg_attrlen(gnlh, 0), NULL);
5188
5189 /*
5190 * TODO: validate the key index and mac address!
5191 * Otherwise, there's a race condition as soon as
5192 * the kernel starts sending key notifications.
5193 */
5194
5195 if (tb[NL80211_ATTR_KEY_SEQ])
5196 memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]),
5197 min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6));
5198 return NL_SKIP;
5199}
5200
5201
5202static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
5203 int idx, u8 *seq)
5204{
5205 struct i802_bss *bss = priv;
5206 struct wpa_driver_nl80211_data *drv = bss->drv;
5207 struct nl_msg *msg;
5208
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005209 msg = nl80211_ifindex_msg(drv, if_nametoindex(iface), 0,
5210 NL80211_CMD_GET_KEY);
5211 if (!msg ||
5212 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
5213 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, idx)) {
5214 nlmsg_free(msg);
5215 return -ENOBUFS;
5216 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005217
5218 memset(seq, 0, 6);
5219
5220 return send_and_recv_msgs(drv, msg, get_key_handler, seq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005221}
5222
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005223
5224static int i802_set_rts(void *priv, int rts)
5225{
5226 struct i802_bss *bss = priv;
5227 struct wpa_driver_nl80211_data *drv = bss->drv;
5228 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005229 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005230 u32 val;
5231
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005232 if (rts >= 2347)
5233 val = (u32) -1;
5234 else
5235 val = rts;
5236
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005237 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_WIPHY)) ||
5238 nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD, val)) {
5239 nlmsg_free(msg);
5240 return -ENOBUFS;
5241 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005242
5243 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5244 if (!ret)
5245 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005246 wpa_printf(MSG_DEBUG, "nl80211: Failed to set RTS threshold %d: "
5247 "%d (%s)", rts, ret, strerror(-ret));
5248 return ret;
5249}
5250
5251
5252static int i802_set_frag(void *priv, int frag)
5253{
5254 struct i802_bss *bss = priv;
5255 struct wpa_driver_nl80211_data *drv = bss->drv;
5256 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005257 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005258 u32 val;
5259
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005260 if (frag >= 2346)
5261 val = (u32) -1;
5262 else
5263 val = frag;
5264
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005265 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_WIPHY)) ||
5266 nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD, val)) {
5267 nlmsg_free(msg);
5268 return -ENOBUFS;
5269 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005270
5271 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5272 if (!ret)
5273 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005274 wpa_printf(MSG_DEBUG, "nl80211: Failed to set fragmentation threshold "
5275 "%d: %d (%s)", frag, ret, strerror(-ret));
5276 return ret;
5277}
5278
5279
5280static int i802_flush(void *priv)
5281{
5282 struct i802_bss *bss = priv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005283 struct nl_msg *msg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005284 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005285
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005286 wpa_printf(MSG_DEBUG, "nl80211: flush -> DEL_STATION %s (all)",
5287 bss->ifname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005288
5289 /*
5290 * XXX: FIX! this needs to flush all VLANs too
5291 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005292 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_DEL_STATION);
5293 res = send_and_recv_msgs(bss->drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005294 if (res) {
5295 wpa_printf(MSG_DEBUG, "nl80211: Station flush failed: ret=%d "
5296 "(%s)", res, strerror(-res));
5297 }
5298 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005299}
5300
5301
5302static int get_sta_handler(struct nl_msg *msg, void *arg)
5303{
5304 struct nlattr *tb[NL80211_ATTR_MAX + 1];
5305 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
5306 struct hostap_sta_driver_data *data = arg;
5307 struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
5308 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
5309 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
5310 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
5311 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
5312 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
5313 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03005314 [NL80211_STA_INFO_TX_FAILED] = { .type = NLA_U32 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005315 };
5316
5317 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
5318 genlmsg_attrlen(gnlh, 0), NULL);
5319
5320 /*
5321 * TODO: validate the interface and mac address!
5322 * Otherwise, there's a race condition as soon as
5323 * the kernel starts sending station notifications.
5324 */
5325
5326 if (!tb[NL80211_ATTR_STA_INFO]) {
5327 wpa_printf(MSG_DEBUG, "sta stats missing!");
5328 return NL_SKIP;
5329 }
5330 if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
5331 tb[NL80211_ATTR_STA_INFO],
5332 stats_policy)) {
5333 wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
5334 return NL_SKIP;
5335 }
5336
5337 if (stats[NL80211_STA_INFO_INACTIVE_TIME])
5338 data->inactive_msec =
5339 nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
5340 if (stats[NL80211_STA_INFO_RX_BYTES])
5341 data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
5342 if (stats[NL80211_STA_INFO_TX_BYTES])
5343 data->tx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
5344 if (stats[NL80211_STA_INFO_RX_PACKETS])
5345 data->rx_packets =
5346 nla_get_u32(stats[NL80211_STA_INFO_RX_PACKETS]);
5347 if (stats[NL80211_STA_INFO_TX_PACKETS])
5348 data->tx_packets =
5349 nla_get_u32(stats[NL80211_STA_INFO_TX_PACKETS]);
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03005350 if (stats[NL80211_STA_INFO_TX_FAILED])
5351 data->tx_retry_failed =
5352 nla_get_u32(stats[NL80211_STA_INFO_TX_FAILED]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005353
5354 return NL_SKIP;
5355}
5356
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005357static int i802_read_sta_data(struct i802_bss *bss,
5358 struct hostap_sta_driver_data *data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005359 const u8 *addr)
5360{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005361 struct nl_msg *msg;
5362
5363 os_memset(data, 0, sizeof(*data));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005364
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005365 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_GET_STATION)) ||
5366 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
5367 nlmsg_free(msg);
5368 return -ENOBUFS;
5369 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005370
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005371 return send_and_recv_msgs(bss->drv, msg, get_sta_handler, data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005372}
5373
5374
5375static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
5376 int cw_min, int cw_max, int burst_time)
5377{
5378 struct i802_bss *bss = priv;
5379 struct wpa_driver_nl80211_data *drv = bss->drv;
5380 struct nl_msg *msg;
5381 struct nlattr *txq, *params;
5382
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005383 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005384 if (!msg)
5385 return -1;
5386
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005387 txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS);
5388 if (!txq)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005389 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005390
5391 /* We are only sending parameters for a single TXQ at a time */
5392 params = nla_nest_start(msg, 1);
5393 if (!params)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005394 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005395
5396 switch (queue) {
5397 case 0:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005398 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VO))
5399 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005400 break;
5401 case 1:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005402 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VI))
5403 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005404 break;
5405 case 2:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005406 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BE))
5407 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005408 break;
5409 case 3:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005410 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BK))
5411 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005412 break;
5413 }
5414 /* Burst time is configured in units of 0.1 msec and TXOP parameter in
5415 * 32 usec, so need to convert the value here. */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005416 if (nla_put_u16(msg, NL80211_TXQ_ATTR_TXOP,
5417 (burst_time * 100 + 16) / 32) ||
5418 nla_put_u16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min) ||
5419 nla_put_u16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max) ||
5420 nla_put_u8(msg, NL80211_TXQ_ATTR_AIFS, aifs))
5421 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005422
5423 nla_nest_end(msg, params);
5424
5425 nla_nest_end(msg, txq);
5426
5427 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
5428 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005429 msg = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005430fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005431 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005432 return -1;
5433}
5434
5435
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005436static int i802_set_sta_vlan(struct i802_bss *bss, const u8 *addr,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005437 const char *ifname, int vlan_id)
5438{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005439 struct wpa_driver_nl80211_data *drv = bss->drv;
5440 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005441 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005442
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005443 wpa_printf(MSG_DEBUG, "nl80211: %s[%d]: set_sta_vlan(" MACSTR
5444 ", ifname=%s[%d], vlan_id=%d)",
5445 bss->ifname, if_nametoindex(bss->ifname),
5446 MAC2STR(addr), ifname, if_nametoindex(ifname), vlan_id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005447 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
5448 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
5449 nla_put_u32(msg, NL80211_ATTR_STA_VLAN, if_nametoindex(ifname))) {
5450 nlmsg_free(msg);
5451 return -ENOBUFS;
5452 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005453
5454 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5455 if (ret < 0) {
5456 wpa_printf(MSG_ERROR, "nl80211: NL80211_ATTR_STA_VLAN (addr="
5457 MACSTR " ifname=%s vlan_id=%d) failed: %d (%s)",
5458 MAC2STR(addr), ifname, vlan_id, ret,
5459 strerror(-ret));
5460 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005461 return ret;
5462}
5463
5464
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005465static int i802_get_inact_sec(void *priv, const u8 *addr)
5466{
5467 struct hostap_sta_driver_data data;
5468 int ret;
5469
5470 data.inactive_msec = (unsigned long) -1;
5471 ret = i802_read_sta_data(priv, &data, addr);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08005472 if (ret == -ENOENT)
5473 return -ENOENT;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005474 if (ret || data.inactive_msec == (unsigned long) -1)
5475 return -1;
5476 return data.inactive_msec / 1000;
5477}
5478
5479
5480static int i802_sta_clear_stats(void *priv, const u8 *addr)
5481{
5482#if 0
5483 /* TODO */
5484#endif
5485 return 0;
5486}
5487
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005488
5489static int i802_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
5490 int reason)
5491{
5492 struct i802_bss *bss = priv;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005493 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005494 struct ieee80211_mgmt mgmt;
5495
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005496 if (is_mesh_interface(drv->nlmode))
5497 return -1;
5498
Dmitry Shmidt04949592012-07-19 12:16:46 -07005499 if (drv->device_ap_sme)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005500 return wpa_driver_nl80211_sta_remove(bss, addr, 1, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005501
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005502 memset(&mgmt, 0, sizeof(mgmt));
5503 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
5504 WLAN_FC_STYPE_DEAUTH);
5505 memcpy(mgmt.da, addr, ETH_ALEN);
5506 memcpy(mgmt.sa, own_addr, ETH_ALEN);
5507 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
5508 mgmt.u.deauth.reason_code = host_to_le16(reason);
5509 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
5510 IEEE80211_HDRLEN +
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005511 sizeof(mgmt.u.deauth), 0, 0, 0, 0,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005512 0, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005513}
5514
5515
5516static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
5517 int reason)
5518{
5519 struct i802_bss *bss = priv;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005520 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005521 struct ieee80211_mgmt mgmt;
5522
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005523 if (is_mesh_interface(drv->nlmode))
5524 return -1;
5525
Dmitry Shmidt04949592012-07-19 12:16:46 -07005526 if (drv->device_ap_sme)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005527 return wpa_driver_nl80211_sta_remove(bss, addr, 0, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005528
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005529 memset(&mgmt, 0, sizeof(mgmt));
5530 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
5531 WLAN_FC_STYPE_DISASSOC);
5532 memcpy(mgmt.da, addr, ETH_ALEN);
5533 memcpy(mgmt.sa, own_addr, ETH_ALEN);
5534 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
5535 mgmt.u.disassoc.reason_code = host_to_le16(reason);
5536 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
5537 IEEE80211_HDRLEN +
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005538 sizeof(mgmt.u.disassoc), 0, 0, 0, 0,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005539 0, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005540}
5541
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005542
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07005543static void dump_ifidx(struct wpa_driver_nl80211_data *drv)
5544{
5545 char buf[200], *pos, *end;
5546 int i, res;
5547
5548 pos = buf;
5549 end = pos + sizeof(buf);
5550
5551 for (i = 0; i < drv->num_if_indices; i++) {
5552 if (!drv->if_indices[i])
5553 continue;
5554 res = os_snprintf(pos, end - pos, " %d", drv->if_indices[i]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005555 if (os_snprintf_error(end - pos, res))
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07005556 break;
5557 pos += res;
5558 }
5559 *pos = '\0';
5560
5561 wpa_printf(MSG_DEBUG, "nl80211: if_indices[%d]:%s",
5562 drv->num_if_indices, buf);
5563}
5564
5565
Jouni Malinen75ecf522011-06-27 15:19:46 -07005566static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
5567{
5568 int i;
5569 int *old;
5570
5571 wpa_printf(MSG_DEBUG, "nl80211: Add own interface ifindex %d",
5572 ifidx);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07005573 if (have_ifidx(drv, ifidx)) {
5574 wpa_printf(MSG_DEBUG, "nl80211: ifindex %d already in the list",
5575 ifidx);
5576 return;
5577 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07005578 for (i = 0; i < drv->num_if_indices; i++) {
5579 if (drv->if_indices[i] == 0) {
5580 drv->if_indices[i] = ifidx;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07005581 dump_ifidx(drv);
Jouni Malinen75ecf522011-06-27 15:19:46 -07005582 return;
5583 }
5584 }
5585
5586 if (drv->if_indices != drv->default_if_indices)
5587 old = drv->if_indices;
5588 else
5589 old = NULL;
5590
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005591 drv->if_indices = os_realloc_array(old, drv->num_if_indices + 1,
5592 sizeof(int));
Jouni Malinen75ecf522011-06-27 15:19:46 -07005593 if (!drv->if_indices) {
5594 if (!old)
5595 drv->if_indices = drv->default_if_indices;
5596 else
5597 drv->if_indices = old;
5598 wpa_printf(MSG_ERROR, "Failed to reallocate memory for "
5599 "interfaces");
5600 wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx);
5601 return;
5602 } else if (!old)
5603 os_memcpy(drv->if_indices, drv->default_if_indices,
5604 sizeof(drv->default_if_indices));
5605 drv->if_indices[drv->num_if_indices] = ifidx;
5606 drv->num_if_indices++;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07005607 dump_ifidx(drv);
Jouni Malinen75ecf522011-06-27 15:19:46 -07005608}
5609
5610
5611static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
5612{
5613 int i;
5614
5615 for (i = 0; i < drv->num_if_indices; i++) {
5616 if (drv->if_indices[i] == ifidx) {
5617 drv->if_indices[i] = 0;
5618 break;
5619 }
5620 }
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07005621 dump_ifidx(drv);
Jouni Malinen75ecf522011-06-27 15:19:46 -07005622}
5623
5624
5625static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
5626{
5627 int i;
5628
5629 for (i = 0; i < drv->num_if_indices; i++)
5630 if (drv->if_indices[i] == ifidx)
5631 return 1;
5632
5633 return 0;
5634}
5635
5636
5637static int i802_set_wds_sta(void *priv, const u8 *addr, int aid, int val,
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07005638 const char *bridge_ifname, char *ifname_wds)
Jouni Malinen75ecf522011-06-27 15:19:46 -07005639{
5640 struct i802_bss *bss = priv;
5641 struct wpa_driver_nl80211_data *drv = bss->drv;
5642 char name[IFNAMSIZ + 1];
5643
5644 os_snprintf(name, sizeof(name), "%s.sta%d", bss->ifname, aid);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005645 if (ifname_wds)
5646 os_strlcpy(ifname_wds, name, IFNAMSIZ + 1);
5647
Jouni Malinen75ecf522011-06-27 15:19:46 -07005648 wpa_printf(MSG_DEBUG, "nl80211: Set WDS STA addr=" MACSTR
5649 " aid=%d val=%d name=%s", MAC2STR(addr), aid, val, name);
5650 if (val) {
5651 if (!if_nametoindex(name)) {
5652 if (nl80211_create_iface(drv, name,
5653 NL80211_IFTYPE_AP_VLAN,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005654 bss->addr, 1, NULL, NULL, 0) <
5655 0)
Jouni Malinen75ecf522011-06-27 15:19:46 -07005656 return -1;
5657 if (bridge_ifname &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005658 linux_br_add_if(drv->global->ioctl_sock,
5659 bridge_ifname, name) < 0)
Jouni Malinen75ecf522011-06-27 15:19:46 -07005660 return -1;
5661 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005662 if (linux_set_iface_flags(drv->global->ioctl_sock, name, 1)) {
5663 wpa_printf(MSG_ERROR, "nl80211: Failed to set WDS STA "
5664 "interface %s up", name);
5665 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07005666 return i802_set_sta_vlan(priv, addr, name, 0);
5667 } else {
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005668 if (bridge_ifname)
5669 linux_br_del_if(drv->global->ioctl_sock, bridge_ifname,
5670 name);
5671
Jouni Malinen75ecf522011-06-27 15:19:46 -07005672 i802_set_sta_vlan(priv, addr, bss->ifname, 0);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08005673 nl80211_remove_iface(drv, if_nametoindex(name));
5674 return 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07005675 }
5676}
5677
5678
5679static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
5680{
5681 struct wpa_driver_nl80211_data *drv = eloop_ctx;
5682 struct sockaddr_ll lladdr;
5683 unsigned char buf[3000];
5684 int len;
5685 socklen_t fromlen = sizeof(lladdr);
5686
5687 len = recvfrom(sock, buf, sizeof(buf), 0,
5688 (struct sockaddr *)&lladdr, &fromlen);
5689 if (len < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005690 wpa_printf(MSG_ERROR, "nl80211: EAPOL recv failed: %s",
5691 strerror(errno));
Jouni Malinen75ecf522011-06-27 15:19:46 -07005692 return;
5693 }
5694
5695 if (have_ifidx(drv, lladdr.sll_ifindex))
5696 drv_event_eapol_rx(drv->ctx, lladdr.sll_addr, buf, len);
5697}
5698
5699
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005700static int i802_check_bridge(struct wpa_driver_nl80211_data *drv,
5701 struct i802_bss *bss,
5702 const char *brname, const char *ifname)
5703{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005704 int br_ifindex;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005705 char in_br[IFNAMSIZ];
5706
5707 os_strlcpy(bss->brname, brname, IFNAMSIZ);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005708 br_ifindex = if_nametoindex(brname);
5709 if (br_ifindex == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005710 /*
5711 * Bridge was configured, but the bridge device does
5712 * not exist. Try to add it now.
5713 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005714 if (linux_br_add(drv->global->ioctl_sock, brname) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005715 wpa_printf(MSG_ERROR, "nl80211: Failed to add the "
5716 "bridge interface %s: %s",
5717 brname, strerror(errno));
5718 return -1;
5719 }
5720 bss->added_bridge = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005721 br_ifindex = if_nametoindex(brname);
5722 add_ifidx(drv, br_ifindex);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005723 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005724 bss->br_ifindex = br_ifindex;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005725
5726 if (linux_br_get(in_br, ifname) == 0) {
5727 if (os_strcmp(in_br, brname) == 0)
5728 return 0; /* already in the bridge */
5729
5730 wpa_printf(MSG_DEBUG, "nl80211: Removing interface %s from "
5731 "bridge %s", ifname, in_br);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005732 if (linux_br_del_if(drv->global->ioctl_sock, in_br, ifname) <
5733 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005734 wpa_printf(MSG_ERROR, "nl80211: Failed to "
5735 "remove interface %s from bridge "
5736 "%s: %s",
5737 ifname, brname, strerror(errno));
5738 return -1;
5739 }
5740 }
5741
5742 wpa_printf(MSG_DEBUG, "nl80211: Adding interface %s into bridge %s",
5743 ifname, brname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005744 if (linux_br_add_if(drv->global->ioctl_sock, brname, ifname) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005745 wpa_printf(MSG_ERROR, "nl80211: Failed to add interface %s "
5746 "into bridge %s: %s",
5747 ifname, brname, strerror(errno));
5748 return -1;
5749 }
5750 bss->added_if_into_bridge = 1;
5751
5752 return 0;
5753}
5754
5755
5756static void *i802_init(struct hostapd_data *hapd,
5757 struct wpa_init_params *params)
5758{
5759 struct wpa_driver_nl80211_data *drv;
5760 struct i802_bss *bss;
5761 size_t i;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005762 char master_ifname[IFNAMSIZ];
5763 int ifindex, br_ifindex = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005764 int br_added = 0;
5765
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005766 bss = wpa_driver_nl80211_drv_init(hapd, params->ifname,
5767 params->global_priv, 1,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005768 params->bssid, params->driver_params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005769 if (bss == NULL)
5770 return NULL;
5771
5772 drv = bss->drv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005773
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005774 if (linux_br_get(master_ifname, params->ifname) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005775 wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in bridge %s",
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005776 params->ifname, master_ifname);
5777 br_ifindex = if_nametoindex(master_ifname);
5778 os_strlcpy(bss->brname, master_ifname, IFNAMSIZ);
5779 } else if ((params->num_bridge == 0 || !params->bridge[0]) &&
5780 linux_master_get(master_ifname, params->ifname) == 0) {
5781 wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in master %s",
5782 params->ifname, master_ifname);
5783 /* start listening for EAPOL on the master interface */
5784 add_ifidx(drv, if_nametoindex(master_ifname));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005785 } else {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005786 master_ifname[0] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005787 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005788
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005789 bss->br_ifindex = br_ifindex;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005790
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005791 for (i = 0; i < params->num_bridge; i++) {
5792 if (params->bridge[i]) {
5793 ifindex = if_nametoindex(params->bridge[i]);
5794 if (ifindex)
5795 add_ifidx(drv, ifindex);
5796 if (ifindex == br_ifindex)
5797 br_added = 1;
5798 }
5799 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005800
5801 /* start listening for EAPOL on the default AP interface */
5802 add_ifidx(drv, drv->ifindex);
5803
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005804 if (params->num_bridge && params->bridge[0]) {
5805 if (i802_check_bridge(drv, bss, params->bridge[0],
5806 params->ifname) < 0)
5807 goto failed;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005808 if (os_strcmp(params->bridge[0], master_ifname) != 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005809 br_added = 1;
5810 }
5811
5812 if (!br_added && br_ifindex &&
5813 (params->num_bridge == 0 || !params->bridge[0]))
5814 add_ifidx(drv, br_ifindex);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005815
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005816#ifdef CONFIG_LIBNL3_ROUTE
5817 if (bss->added_if_into_bridge) {
5818 drv->rtnl_sk = nl_socket_alloc();
5819 if (drv->rtnl_sk == NULL) {
5820 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate nl_sock");
5821 goto failed;
5822 }
5823
5824 if (nl_connect(drv->rtnl_sk, NETLINK_ROUTE)) {
5825 wpa_printf(MSG_ERROR, "nl80211: Failed to connect nl_sock to NETLINK_ROUTE: %s",
5826 strerror(errno));
5827 goto failed;
5828 }
5829 }
5830#endif /* CONFIG_LIBNL3_ROUTE */
5831
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005832 drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
5833 if (drv->eapol_sock < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005834 wpa_printf(MSG_ERROR, "nl80211: socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE) failed: %s",
5835 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005836 goto failed;
5837 }
5838
5839 if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
5840 {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005841 wpa_printf(MSG_INFO, "nl80211: Could not register read socket for eapol");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005842 goto failed;
5843 }
5844
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005845 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
5846 params->own_addr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005847 goto failed;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005848 os_memcpy(drv->perm_addr, params->own_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005849
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005850 memcpy(bss->addr, params->own_addr, ETH_ALEN);
5851
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005852 return bss;
5853
5854failed:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005855 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005856 return NULL;
5857}
5858
5859
5860static void i802_deinit(void *priv)
5861{
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005862 struct i802_bss *bss = priv;
5863 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005864}
5865
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005866
5867static enum nl80211_iftype wpa_driver_nl80211_if_type(
5868 enum wpa_driver_if_type type)
5869{
5870 switch (type) {
5871 case WPA_IF_STATION:
5872 return NL80211_IFTYPE_STATION;
5873 case WPA_IF_P2P_CLIENT:
5874 case WPA_IF_P2P_GROUP:
5875 return NL80211_IFTYPE_P2P_CLIENT;
5876 case WPA_IF_AP_VLAN:
5877 return NL80211_IFTYPE_AP_VLAN;
5878 case WPA_IF_AP_BSS:
5879 return NL80211_IFTYPE_AP;
5880 case WPA_IF_P2P_GO:
5881 return NL80211_IFTYPE_P2P_GO;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005882 case WPA_IF_P2P_DEVICE:
5883 return NL80211_IFTYPE_P2P_DEVICE;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005884 case WPA_IF_MESH:
5885 return NL80211_IFTYPE_MESH_POINT;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005886 default:
5887 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005888 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005889}
5890
5891
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005892static int nl80211_addr_in_use(struct nl80211_global *global, const u8 *addr)
5893{
5894 struct wpa_driver_nl80211_data *drv;
5895 dl_list_for_each(drv, &global->interfaces,
5896 struct wpa_driver_nl80211_data, list) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005897 if (os_memcmp(addr, drv->first_bss->addr, ETH_ALEN) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005898 return 1;
5899 }
5900 return 0;
5901}
5902
5903
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005904static int nl80211_vif_addr(struct wpa_driver_nl80211_data *drv, u8 *new_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005905{
5906 unsigned int idx;
5907
5908 if (!drv->global)
5909 return -1;
5910
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005911 os_memcpy(new_addr, drv->first_bss->addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005912 for (idx = 0; idx < 64; idx++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005913 new_addr[0] = drv->first_bss->addr[0] | 0x02;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005914 new_addr[0] ^= idx << 2;
5915 if (!nl80211_addr_in_use(drv->global, new_addr))
5916 break;
5917 }
5918 if (idx == 64)
5919 return -1;
5920
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005921 wpa_printf(MSG_DEBUG, "nl80211: Assigned new virtual interface address "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005922 MACSTR, MAC2STR(new_addr));
5923
5924 return 0;
5925}
5926
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005927
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005928struct wdev_info {
5929 u64 wdev_id;
5930 int wdev_id_set;
5931 u8 macaddr[ETH_ALEN];
5932};
5933
5934static int nl80211_wdev_handler(struct nl_msg *msg, void *arg)
5935{
5936 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
5937 struct nlattr *tb[NL80211_ATTR_MAX + 1];
5938 struct wdev_info *wi = arg;
5939
5940 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
5941 genlmsg_attrlen(gnlh, 0), NULL);
5942 if (tb[NL80211_ATTR_WDEV]) {
5943 wi->wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
5944 wi->wdev_id_set = 1;
5945 }
5946
5947 if (tb[NL80211_ATTR_MAC])
5948 os_memcpy(wi->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
5949 ETH_ALEN);
5950
5951 return NL_SKIP;
5952}
5953
5954
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005955static int wpa_driver_nl80211_if_add(void *priv, enum wpa_driver_if_type type,
5956 const char *ifname, const u8 *addr,
5957 void *bss_ctx, void **drv_priv,
5958 char *force_ifname, u8 *if_addr,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005959 const char *bridge, int use_existing,
5960 int setup_ap)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005961{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005962 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005963 struct i802_bss *bss = priv;
5964 struct wpa_driver_nl80211_data *drv = bss->drv;
5965 int ifidx;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005966 int added = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005967
5968 if (addr)
5969 os_memcpy(if_addr, addr, ETH_ALEN);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005970 nlmode = wpa_driver_nl80211_if_type(type);
5971 if (nlmode == NL80211_IFTYPE_P2P_DEVICE) {
5972 struct wdev_info p2pdev_info;
5973
5974 os_memset(&p2pdev_info, 0, sizeof(p2pdev_info));
5975 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
5976 0, nl80211_wdev_handler,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005977 &p2pdev_info, use_existing);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005978 if (!p2pdev_info.wdev_id_set || ifidx != 0) {
5979 wpa_printf(MSG_ERROR, "nl80211: Failed to create a P2P Device interface %s",
5980 ifname);
5981 return -1;
5982 }
5983
5984 drv->global->if_add_wdevid = p2pdev_info.wdev_id;
5985 drv->global->if_add_wdevid_set = p2pdev_info.wdev_id_set;
5986 if (!is_zero_ether_addr(p2pdev_info.macaddr))
5987 os_memcpy(if_addr, p2pdev_info.macaddr, ETH_ALEN);
5988 wpa_printf(MSG_DEBUG, "nl80211: New P2P Device interface %s (0x%llx) created",
5989 ifname,
5990 (long long unsigned int) p2pdev_info.wdev_id);
5991 } else {
5992 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005993 0, NULL, NULL, use_existing);
5994 if (use_existing && ifidx == -ENFILE) {
5995 added = 0;
5996 ifidx = if_nametoindex(ifname);
5997 } else if (ifidx < 0) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005998 return -1;
5999 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006000 }
6001
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006002 if (!addr) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07006003 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006004 os_memcpy(if_addr, bss->addr, ETH_ALEN);
6005 else if (linux_get_ifhwaddr(drv->global->ioctl_sock,
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07006006 ifname, if_addr) < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006007 if (added)
6008 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006009 return -1;
6010 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006011 }
6012
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006013 if (!addr &&
6014 (type == WPA_IF_P2P_CLIENT || type == WPA_IF_P2P_GROUP ||
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07006015 type == WPA_IF_P2P_GO || type == WPA_IF_MESH ||
6016 type == WPA_IF_STATION)) {
6017 /* Enforce unique address */
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006018 u8 new_addr[ETH_ALEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006019
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006020 if (linux_get_ifhwaddr(drv->global->ioctl_sock, ifname,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006021 new_addr) < 0) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07006022 if (added)
6023 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006024 return -1;
6025 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006026 if (nl80211_addr_in_use(drv->global, new_addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006027 wpa_printf(MSG_DEBUG, "nl80211: Allocate new address "
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07006028 "for interface %s type %d", ifname, type);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006029 if (nl80211_vif_addr(drv, new_addr) < 0) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07006030 if (added)
6031 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006032 return -1;
6033 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006034 if (linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006035 new_addr) < 0) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07006036 if (added)
6037 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006038 return -1;
6039 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006040 }
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07006041 os_memcpy(if_addr, new_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006042 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006043
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006044 if (type == WPA_IF_AP_BSS && setup_ap) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006045 struct i802_bss *new_bss = os_zalloc(sizeof(*new_bss));
6046 if (new_bss == NULL) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006047 if (added)
6048 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006049 return -1;
6050 }
6051
6052 if (bridge &&
6053 i802_check_bridge(drv, new_bss, bridge, ifname) < 0) {
6054 wpa_printf(MSG_ERROR, "nl80211: Failed to add the new "
6055 "interface %s to a bridge %s",
6056 ifname, bridge);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006057 if (added)
6058 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006059 os_free(new_bss);
6060 return -1;
6061 }
6062
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006063 if (linux_set_iface_flags(drv->global->ioctl_sock, ifname, 1))
6064 {
Dmitry Shmidt71757432014-06-02 13:50:35 -07006065 if (added)
6066 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006067 os_free(new_bss);
6068 return -1;
6069 }
6070 os_strlcpy(new_bss->ifname, ifname, IFNAMSIZ);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006071 os_memcpy(new_bss->addr, if_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006072 new_bss->ifindex = ifidx;
6073 new_bss->drv = drv;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006074 new_bss->next = drv->first_bss->next;
6075 new_bss->freq = drv->first_bss->freq;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006076 new_bss->ctx = bss_ctx;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006077 new_bss->added_if = added;
6078 drv->first_bss->next = new_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006079 if (drv_priv)
6080 *drv_priv = new_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006081 nl80211_init_bss(new_bss);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006082
6083 /* Subscribe management frames for this WPA_IF_AP_BSS */
6084 if (nl80211_setup_ap(new_bss))
6085 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006086 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006087
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006088 if (drv->global)
6089 drv->global->if_add_ifindex = ifidx;
6090
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006091 /*
6092 * Some virtual interfaces need to process EAPOL packets and events on
6093 * the parent interface. This is used mainly with hostapd.
6094 */
6095 if (ifidx > 0 &&
6096 (drv->hostapd ||
6097 nlmode == NL80211_IFTYPE_AP_VLAN ||
6098 nlmode == NL80211_IFTYPE_WDS ||
6099 nlmode == NL80211_IFTYPE_MONITOR))
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006100 add_ifidx(drv, ifidx);
6101
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006102 return 0;
6103}
6104
6105
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006106static int wpa_driver_nl80211_if_remove(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006107 enum wpa_driver_if_type type,
6108 const char *ifname)
6109{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006110 struct wpa_driver_nl80211_data *drv = bss->drv;
6111 int ifindex = if_nametoindex(ifname);
6112
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006113 wpa_printf(MSG_DEBUG, "nl80211: %s(type=%d ifname=%s) ifindex=%d added_if=%d",
6114 __func__, type, ifname, ifindex, bss->added_if);
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08006115 if (ifindex > 0 && (bss->added_if || bss->ifindex != ifindex))
Dmitry Shmidt051af732013-10-22 13:52:46 -07006116 nl80211_remove_iface(drv, ifindex);
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07006117 else if (ifindex > 0 && !bss->added_if) {
6118 struct wpa_driver_nl80211_data *drv2;
6119 dl_list_for_each(drv2, &drv->global->interfaces,
6120 struct wpa_driver_nl80211_data, list)
6121 del_ifidx(drv2, ifindex);
6122 }
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006123
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006124 if (type != WPA_IF_AP_BSS)
6125 return 0;
6126
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006127 if (bss->added_if_into_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006128 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
6129 bss->ifname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006130 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
6131 "interface %s from bridge %s: %s",
6132 bss->ifname, bss->brname, strerror(errno));
6133 }
6134 if (bss->added_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006135 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006136 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
6137 "bridge %s: %s",
6138 bss->brname, strerror(errno));
6139 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006140
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006141 if (bss != drv->first_bss) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006142 struct i802_bss *tbss;
6143
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006144 wpa_printf(MSG_DEBUG, "nl80211: Not the first BSS - remove it");
6145 for (tbss = drv->first_bss; tbss; tbss = tbss->next) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006146 if (tbss->next == bss) {
6147 tbss->next = bss->next;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006148 /* Unsubscribe management frames */
6149 nl80211_teardown_ap(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006150 nl80211_destroy_bss(bss);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006151 if (!bss->added_if)
6152 i802_set_iface_flags(bss, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006153 os_free(bss);
6154 bss = NULL;
6155 break;
6156 }
6157 }
6158 if (bss)
6159 wpa_printf(MSG_INFO, "nl80211: %s - could not find "
6160 "BSS %p in the list", __func__, bss);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006161 } else {
6162 wpa_printf(MSG_DEBUG, "nl80211: First BSS - reassign context");
6163 nl80211_teardown_ap(bss);
6164 if (!bss->added_if && !drv->first_bss->next)
6165 wpa_driver_nl80211_del_beacon(drv);
6166 nl80211_destroy_bss(bss);
6167 if (!bss->added_if)
6168 i802_set_iface_flags(bss, 0);
6169 if (drv->first_bss->next) {
6170 drv->first_bss = drv->first_bss->next;
6171 drv->ctx = drv->first_bss->ctx;
6172 os_free(bss);
6173 } else {
6174 wpa_printf(MSG_DEBUG, "nl80211: No second BSS to reassign context to");
6175 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006176 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006177
6178 return 0;
6179}
6180
6181
6182static int cookie_handler(struct nl_msg *msg, void *arg)
6183{
6184 struct nlattr *tb[NL80211_ATTR_MAX + 1];
6185 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6186 u64 *cookie = arg;
6187 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6188 genlmsg_attrlen(gnlh, 0), NULL);
6189 if (tb[NL80211_ATTR_COOKIE])
6190 *cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
6191 return NL_SKIP;
6192}
6193
6194
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006195static int nl80211_send_frame_cmd(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006196 unsigned int freq, unsigned int wait,
6197 const u8 *buf, size_t buf_len,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006198 u64 *cookie_out, int no_cck, int no_ack,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006199 int offchanok, const u16 *csa_offs,
6200 size_t csa_offs_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006201{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006202 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006203 struct nl_msg *msg;
6204 u64 cookie;
6205 int ret = -1;
6206
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006207 wpa_printf(MSG_MSGDUMP, "nl80211: CMD_FRAME freq=%u wait=%u no_cck=%d "
Dmitry Shmidt04949592012-07-19 12:16:46 -07006208 "no_ack=%d offchanok=%d",
6209 freq, wait, no_cck, no_ack, offchanok);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006210 wpa_hexdump(MSG_MSGDUMP, "CMD_FRAME", buf, buf_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006211
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006212 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_FRAME)) ||
6213 (freq && nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
6214 (wait && nla_put_u32(msg, NL80211_ATTR_DURATION, wait)) ||
6215 (offchanok && ((drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
6216 drv->test_use_roc_tx) &&
6217 nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK)) ||
6218 (no_cck && nla_put_flag(msg, NL80211_ATTR_TX_NO_CCK_RATE)) ||
6219 (no_ack && nla_put_flag(msg, NL80211_ATTR_DONT_WAIT_FOR_ACK)) ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006220 (csa_offs && nla_put(msg, NL80211_ATTR_CSA_C_OFFSETS_TX,
6221 csa_offs_len * sizeof(u16), csa_offs)) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006222 nla_put(msg, NL80211_ATTR_FRAME, buf_len, buf))
6223 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006224
6225 cookie = 0;
6226 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
6227 msg = NULL;
6228 if (ret) {
6229 wpa_printf(MSG_DEBUG, "nl80211: Frame command failed: ret=%d "
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07006230 "(%s) (freq=%u wait=%u)", ret, strerror(-ret),
6231 freq, wait);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006232 } else {
6233 wpa_printf(MSG_MSGDUMP, "nl80211: Frame TX command accepted%s; "
6234 "cookie 0x%llx", no_ack ? " (no ACK)" : "",
6235 (long long unsigned int) cookie);
6236
6237 if (cookie_out)
6238 *cookie_out = no_ack ? (u64) -1 : cookie;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006239
6240 if (drv->num_send_action_cookies == MAX_SEND_ACTION_COOKIES) {
6241 wpa_printf(MSG_DEBUG,
6242 "nl80211: Drop oldest pending send action cookie 0x%llx",
6243 (long long unsigned int)
6244 drv->send_action_cookies[0]);
6245 os_memmove(&drv->send_action_cookies[0],
6246 &drv->send_action_cookies[1],
6247 (MAX_SEND_ACTION_COOKIES - 1) *
6248 sizeof(u64));
6249 drv->num_send_action_cookies--;
6250 }
6251 drv->send_action_cookies[drv->num_send_action_cookies] = cookie;
6252 drv->num_send_action_cookies++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006253 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006254
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006255fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006256 nlmsg_free(msg);
6257 return ret;
6258}
6259
6260
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006261static int wpa_driver_nl80211_send_action(struct i802_bss *bss,
6262 unsigned int freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006263 unsigned int wait_time,
6264 const u8 *dst, const u8 *src,
6265 const u8 *bssid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006266 const u8 *data, size_t data_len,
6267 int no_cck)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006268{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006269 struct wpa_driver_nl80211_data *drv = bss->drv;
6270 int ret = -1;
6271 u8 *buf;
6272 struct ieee80211_hdr *hdr;
6273
6274 wpa_printf(MSG_DEBUG, "nl80211: Send Action frame (ifindex=%d, "
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006275 "freq=%u MHz wait=%d ms no_cck=%d)",
6276 drv->ifindex, freq, wait_time, no_cck);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006277
6278 buf = os_zalloc(24 + data_len);
6279 if (buf == NULL)
6280 return ret;
6281 os_memcpy(buf + 24, data, data_len);
6282 hdr = (struct ieee80211_hdr *) buf;
6283 hdr->frame_control =
6284 IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_ACTION);
6285 os_memcpy(hdr->addr1, dst, ETH_ALEN);
6286 os_memcpy(hdr->addr2, src, ETH_ALEN);
6287 os_memcpy(hdr->addr3, bssid, ETH_ALEN);
6288
Dmitry Shmidt56052862013-10-04 10:23:25 -07006289 if (is_ap_interface(drv->nlmode) &&
6290 (!(drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
6291 (int) freq == bss->freq || drv->device_ap_sme ||
6292 !drv->use_monitor))
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006293 ret = wpa_driver_nl80211_send_mlme(bss, buf, 24 + data_len,
6294 0, freq, no_cck, 1,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006295 wait_time, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006296 else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006297 ret = nl80211_send_frame_cmd(bss, freq, wait_time, buf,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006298 24 + data_len,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006299 &drv->send_action_cookie,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006300 no_cck, 0, 1, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006301
6302 os_free(buf);
6303 return ret;
6304}
6305
6306
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006307static void nl80211_frame_wait_cancel(struct i802_bss *bss, u64 cookie)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006308{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006309 struct wpa_driver_nl80211_data *drv = bss->drv;
6310 struct nl_msg *msg;
6311 int ret;
6312
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08006313 wpa_printf(MSG_DEBUG, "nl80211: Cancel TX frame wait: cookie=0x%llx",
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006314 (long long unsigned int) cookie);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006315 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_FRAME_WAIT_CANCEL)) ||
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006316 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006317 nlmsg_free(msg);
6318 return;
6319 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006320
6321 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006322 if (ret)
6323 wpa_printf(MSG_DEBUG, "nl80211: wait cancel failed: ret=%d "
6324 "(%s)", ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006325}
6326
6327
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006328static void wpa_driver_nl80211_send_action_cancel_wait(void *priv)
6329{
6330 struct i802_bss *bss = priv;
6331 struct wpa_driver_nl80211_data *drv = bss->drv;
6332 unsigned int i;
6333 u64 cookie;
6334
6335 /* Cancel the last pending TX cookie */
6336 nl80211_frame_wait_cancel(bss, drv->send_action_cookie);
6337
6338 /*
6339 * Cancel the other pending TX cookies, if any. This is needed since
6340 * the driver may keep a list of all pending offchannel TX operations
6341 * and free up the radio only once they have expired or cancelled.
6342 */
6343 for (i = drv->num_send_action_cookies; i > 0; i--) {
6344 cookie = drv->send_action_cookies[i - 1];
6345 if (cookie != drv->send_action_cookie)
6346 nl80211_frame_wait_cancel(bss, cookie);
6347 }
6348 drv->num_send_action_cookies = 0;
6349}
6350
6351
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006352static int wpa_driver_nl80211_remain_on_channel(void *priv, unsigned int freq,
6353 unsigned int duration)
6354{
6355 struct i802_bss *bss = priv;
6356 struct wpa_driver_nl80211_data *drv = bss->drv;
6357 struct nl_msg *msg;
6358 int ret;
6359 u64 cookie;
6360
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006361 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_REMAIN_ON_CHANNEL)) ||
6362 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
6363 nla_put_u32(msg, NL80211_ATTR_DURATION, duration)) {
6364 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006365 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006366 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006367
6368 cookie = 0;
6369 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
6370 if (ret == 0) {
6371 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel cookie "
6372 "0x%llx for freq=%u MHz duration=%u",
6373 (long long unsigned int) cookie, freq, duration);
6374 drv->remain_on_chan_cookie = cookie;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006375 drv->pending_remain_on_chan = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006376 return 0;
6377 }
6378 wpa_printf(MSG_DEBUG, "nl80211: Failed to request remain-on-channel "
6379 "(freq=%d duration=%u): %d (%s)",
6380 freq, duration, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006381 return -1;
6382}
6383
6384
6385static int wpa_driver_nl80211_cancel_remain_on_channel(void *priv)
6386{
6387 struct i802_bss *bss = priv;
6388 struct wpa_driver_nl80211_data *drv = bss->drv;
6389 struct nl_msg *msg;
6390 int ret;
6391
6392 if (!drv->pending_remain_on_chan) {
6393 wpa_printf(MSG_DEBUG, "nl80211: No pending remain-on-channel "
6394 "to cancel");
6395 return -1;
6396 }
6397
6398 wpa_printf(MSG_DEBUG, "nl80211: Cancel remain-on-channel with cookie "
6399 "0x%llx",
6400 (long long unsigned int) drv->remain_on_chan_cookie);
6401
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006402 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL);
6403 if (!msg ||
6404 nla_put_u64(msg, NL80211_ATTR_COOKIE, drv->remain_on_chan_cookie)) {
6405 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006406 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006407 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006408
6409 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6410 if (ret == 0)
6411 return 0;
6412 wpa_printf(MSG_DEBUG, "nl80211: Failed to cancel remain-on-channel: "
6413 "%d (%s)", ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006414 return -1;
6415}
6416
6417
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006418static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss, int report)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006419{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006420 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07006421
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006422 if (!report) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006423 if (bss->nl_preq && drv->device_ap_sme &&
Dmitry Shmidt03658832014-08-13 11:03:49 -07006424 is_ap_interface(drv->nlmode) && !bss->in_deinit &&
6425 !bss->static_ap) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006426 /*
6427 * Do not disable Probe Request reporting that was
6428 * enabled in nl80211_setup_ap().
6429 */
6430 wpa_printf(MSG_DEBUG, "nl80211: Skip disabling of "
6431 "Probe Request reporting nl_preq=%p while "
6432 "in AP mode", bss->nl_preq);
6433 } else if (bss->nl_preq) {
6434 wpa_printf(MSG_DEBUG, "nl80211: Disable Probe Request "
6435 "reporting nl_preq=%p", bss->nl_preq);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006436 nl80211_destroy_eloop_handle(&bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006437 }
6438 return 0;
6439 }
6440
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006441 if (bss->nl_preq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006442 wpa_printf(MSG_DEBUG, "nl80211: Probe Request reporting "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006443 "already on! nl_preq=%p", bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006444 return 0;
6445 }
6446
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006447 bss->nl_preq = nl_create_handle(drv->global->nl_cb, "preq");
6448 if (bss->nl_preq == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006449 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006450 wpa_printf(MSG_DEBUG, "nl80211: Enable Probe Request "
6451 "reporting nl_preq=%p", bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006452
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006453 if (nl80211_register_frame(bss, bss->nl_preq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006454 (WLAN_FC_TYPE_MGMT << 2) |
6455 (WLAN_FC_STYPE_PROBE_REQ << 4),
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006456 NULL, 0) < 0)
6457 goto out_err;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07006458
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006459 nl80211_register_eloop_read(&bss->nl_preq,
6460 wpa_driver_nl80211_event_receive,
6461 bss->nl_cb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006462
6463 return 0;
6464
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006465 out_err:
6466 nl_destroy_handles(&bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006467 return -1;
6468}
6469
6470
6471static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
6472 int ifindex, int disabled)
6473{
6474 struct nl_msg *msg;
6475 struct nlattr *bands, *band;
6476 int ret;
6477
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006478 wpa_printf(MSG_DEBUG,
6479 "nl80211: NL80211_CMD_SET_TX_BITRATE_MASK (ifindex=%d %s)",
6480 ifindex, disabled ? "NL80211_TXRATE_LEGACY=OFDM-only" :
6481 "no NL80211_TXRATE_LEGACY constraint");
6482
6483 msg = nl80211_ifindex_msg(drv, ifindex, 0,
6484 NL80211_CMD_SET_TX_BITRATE_MASK);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006485 if (!msg)
6486 return -1;
6487
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006488 bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES);
6489 if (!bands)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006490 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006491
6492 /*
6493 * Disable 2 GHz rates 1, 2, 5.5, 11 Mbps by masking out everything
6494 * else apart from 6, 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS
6495 * rates. All 5 GHz rates are left enabled.
6496 */
6497 band = nla_nest_start(msg, NL80211_BAND_2GHZ);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006498 if (!band ||
6499 (disabled && nla_put(msg, NL80211_TXRATE_LEGACY, 8,
6500 "\x0c\x12\x18\x24\x30\x48\x60\x6c")))
6501 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006502 nla_nest_end(msg, band);
6503
6504 nla_nest_end(msg, bands);
6505
6506 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006507 if (ret) {
6508 wpa_printf(MSG_DEBUG, "nl80211: Set TX rates failed: ret=%d "
6509 "(%s)", ret, strerror(-ret));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006510 } else
6511 drv->disabled_11b_rates = disabled;
6512
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006513 return ret;
6514
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006515fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006516 nlmsg_free(msg);
6517 return -1;
6518}
6519
6520
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006521static int wpa_driver_nl80211_deinit_ap(void *priv)
6522{
6523 struct i802_bss *bss = priv;
6524 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006525 if (!is_ap_interface(drv->nlmode))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006526 return -1;
6527 wpa_driver_nl80211_del_beacon(drv);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006528 bss->beacon_set = 0;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006529
6530 /*
6531 * If the P2P GO interface was dynamically added, then it is
6532 * possible that the interface change to station is not possible.
6533 */
6534 if (drv->nlmode == NL80211_IFTYPE_P2P_GO && bss->if_dynamic)
6535 return 0;
6536
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006537 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006538}
6539
6540
Dmitry Shmidtea69e842013-05-13 14:52:28 -07006541static int wpa_driver_nl80211_stop_ap(void *priv)
6542{
6543 struct i802_bss *bss = priv;
6544 struct wpa_driver_nl80211_data *drv = bss->drv;
6545 if (!is_ap_interface(drv->nlmode))
6546 return -1;
6547 wpa_driver_nl80211_del_beacon(drv);
6548 bss->beacon_set = 0;
6549 return 0;
6550}
6551
6552
Dmitry Shmidt04949592012-07-19 12:16:46 -07006553static int wpa_driver_nl80211_deinit_p2p_cli(void *priv)
6554{
6555 struct i802_bss *bss = priv;
6556 struct wpa_driver_nl80211_data *drv = bss->drv;
6557 if (drv->nlmode != NL80211_IFTYPE_P2P_CLIENT)
6558 return -1;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006559
6560 /*
6561 * If the P2P Client interface was dynamically added, then it is
6562 * possible that the interface change to station is not possible.
6563 */
6564 if (bss->if_dynamic)
6565 return 0;
6566
Dmitry Shmidt04949592012-07-19 12:16:46 -07006567 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
6568}
6569
6570
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006571static void wpa_driver_nl80211_resume(void *priv)
6572{
6573 struct i802_bss *bss = priv;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006574 enum nl80211_iftype nlmode = nl80211_get_ifmode(bss);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006575
6576 if (i802_set_iface_flags(bss, 1))
6577 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface up on resume event");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006578
6579 if (is_p2p_net_interface(nlmode))
6580 nl80211_disable_11b_rates(bss->drv, bss->drv->ifindex, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006581}
6582
6583
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006584static int nl80211_signal_monitor(void *priv, int threshold, int hysteresis)
6585{
6586 struct i802_bss *bss = priv;
6587 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006588 struct nl_msg *msg;
6589 struct nlattr *cqm;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006590
6591 wpa_printf(MSG_DEBUG, "nl80211: Signal monitor threshold=%d "
6592 "hysteresis=%d", threshold, hysteresis);
6593
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006594 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_CQM)) ||
6595 !(cqm = nla_nest_start(msg, NL80211_ATTR_CQM)) ||
6596 nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THOLD, threshold) ||
6597 nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_HYST, hysteresis)) {
6598 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006599 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006600 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006601 nla_nest_end(msg, cqm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006602
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006603 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006604}
6605
6606
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006607static int get_channel_width(struct nl_msg *msg, void *arg)
6608{
6609 struct nlattr *tb[NL80211_ATTR_MAX + 1];
6610 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6611 struct wpa_signal_info *sig_change = arg;
6612
6613 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6614 genlmsg_attrlen(gnlh, 0), NULL);
6615
6616 sig_change->center_frq1 = -1;
6617 sig_change->center_frq2 = -1;
6618 sig_change->chanwidth = CHAN_WIDTH_UNKNOWN;
6619
6620 if (tb[NL80211_ATTR_CHANNEL_WIDTH]) {
6621 sig_change->chanwidth = convert2width(
6622 nla_get_u32(tb[NL80211_ATTR_CHANNEL_WIDTH]));
6623 if (tb[NL80211_ATTR_CENTER_FREQ1])
6624 sig_change->center_frq1 =
6625 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]);
6626 if (tb[NL80211_ATTR_CENTER_FREQ2])
6627 sig_change->center_frq2 =
6628 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]);
6629 }
6630
6631 return NL_SKIP;
6632}
6633
6634
6635static int nl80211_get_channel_width(struct wpa_driver_nl80211_data *drv,
6636 struct wpa_signal_info *sig)
6637{
6638 struct nl_msg *msg;
6639
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006640 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_GET_INTERFACE);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006641 return send_and_recv_msgs(drv, msg, get_channel_width, sig);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006642}
6643
6644
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006645static int nl80211_signal_poll(void *priv, struct wpa_signal_info *si)
6646{
6647 struct i802_bss *bss = priv;
6648 struct wpa_driver_nl80211_data *drv = bss->drv;
6649 int res;
6650
6651 os_memset(si, 0, sizeof(*si));
6652 res = nl80211_get_link_signal(drv, si);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006653 if (res) {
6654 if (drv->nlmode != NL80211_IFTYPE_ADHOC &&
6655 drv->nlmode != NL80211_IFTYPE_MESH_POINT)
6656 return res;
6657 si->current_signal = 0;
6658 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006659
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006660 res = nl80211_get_channel_width(drv, si);
6661 if (res != 0)
6662 return res;
6663
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006664 return nl80211_get_link_noise(drv, si);
6665}
6666
6667
6668static int nl80211_send_frame(void *priv, const u8 *data, size_t data_len,
6669 int encrypt)
6670{
6671 struct i802_bss *bss = priv;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006672 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt, 0,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006673 0, 0, 0, 0, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006674}
6675
6676
6677static int nl80211_set_param(void *priv, const char *param)
6678{
6679 wpa_printf(MSG_DEBUG, "nl80211: driver param='%s'", param);
6680 if (param == NULL)
6681 return 0;
6682
6683#ifdef CONFIG_P2P
6684 if (os_strstr(param, "use_p2p_group_interface=1")) {
6685 struct i802_bss *bss = priv;
6686 struct wpa_driver_nl80211_data *drv = bss->drv;
6687
6688 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
6689 "interface");
6690 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
6691 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
6692 }
6693#endif /* CONFIG_P2P */
6694
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006695 if (os_strstr(param, "use_monitor=1")) {
6696 struct i802_bss *bss = priv;
6697 struct wpa_driver_nl80211_data *drv = bss->drv;
6698 drv->use_monitor = 1;
6699 }
6700
6701 if (os_strstr(param, "force_connect_cmd=1")) {
6702 struct i802_bss *bss = priv;
6703 struct wpa_driver_nl80211_data *drv = bss->drv;
6704 drv->capa.flags &= ~WPA_DRIVER_FLAGS_SME;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006705 drv->force_connect_cmd = 1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006706 }
6707
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08006708 if (os_strstr(param, "no_offchannel_tx=1")) {
6709 struct i802_bss *bss = priv;
6710 struct wpa_driver_nl80211_data *drv = bss->drv;
6711 drv->capa.flags &= ~WPA_DRIVER_FLAGS_OFFCHANNEL_TX;
6712 drv->test_use_roc_tx = 1;
6713 }
6714
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006715 return 0;
6716}
6717
6718
6719static void * nl80211_global_init(void)
6720{
6721 struct nl80211_global *global;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006722 struct netlink_config *cfg;
6723
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006724 global = os_zalloc(sizeof(*global));
6725 if (global == NULL)
6726 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006727 global->ioctl_sock = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006728 dl_list_init(&global->interfaces);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006729 global->if_add_ifindex = -1;
6730
6731 cfg = os_zalloc(sizeof(*cfg));
6732 if (cfg == NULL)
6733 goto err;
6734
6735 cfg->ctx = global;
6736 cfg->newlink_cb = wpa_driver_nl80211_event_rtm_newlink;
6737 cfg->dellink_cb = wpa_driver_nl80211_event_rtm_dellink;
6738 global->netlink = netlink_init(cfg);
6739 if (global->netlink == NULL) {
6740 os_free(cfg);
6741 goto err;
6742 }
6743
6744 if (wpa_driver_nl80211_init_nl_global(global) < 0)
6745 goto err;
6746
6747 global->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
6748 if (global->ioctl_sock < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006749 wpa_printf(MSG_ERROR, "nl80211: socket(PF_INET,SOCK_DGRAM) failed: %s",
6750 strerror(errno));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006751 goto err;
6752 }
6753
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006754 return global;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006755
6756err:
6757 nl80211_global_deinit(global);
6758 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006759}
6760
6761
6762static void nl80211_global_deinit(void *priv)
6763{
6764 struct nl80211_global *global = priv;
6765 if (global == NULL)
6766 return;
6767 if (!dl_list_empty(&global->interfaces)) {
6768 wpa_printf(MSG_ERROR, "nl80211: %u interface(s) remain at "
6769 "nl80211_global_deinit",
6770 dl_list_len(&global->interfaces));
6771 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006772
6773 if (global->netlink)
6774 netlink_deinit(global->netlink);
6775
6776 nl_destroy_handles(&global->nl);
6777
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006778 if (global->nl_event)
6779 nl80211_destroy_eloop_handle(&global->nl_event);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006780
6781 nl_cb_put(global->nl_cb);
6782
6783 if (global->ioctl_sock >= 0)
6784 close(global->ioctl_sock);
6785
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006786 os_free(global);
6787}
6788
6789
6790static const char * nl80211_get_radio_name(void *priv)
6791{
6792 struct i802_bss *bss = priv;
6793 struct wpa_driver_nl80211_data *drv = bss->drv;
6794 return drv->phyname;
6795}
6796
6797
Jouni Malinen75ecf522011-06-27 15:19:46 -07006798static int nl80211_pmkid(struct i802_bss *bss, int cmd, const u8 *bssid,
6799 const u8 *pmkid)
6800{
6801 struct nl_msg *msg;
6802
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006803 if (!(msg = nl80211_bss_msg(bss, 0, cmd)) ||
6804 (pmkid && nla_put(msg, NL80211_ATTR_PMKID, 16, pmkid)) ||
6805 (bssid && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))) {
6806 nlmsg_free(msg);
6807 return -ENOBUFS;
6808 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07006809
6810 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
Jouni Malinen75ecf522011-06-27 15:19:46 -07006811}
6812
6813
6814static int nl80211_add_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
6815{
6816 struct i802_bss *bss = priv;
6817 wpa_printf(MSG_DEBUG, "nl80211: Add PMKID for " MACSTR, MAC2STR(bssid));
6818 return nl80211_pmkid(bss, NL80211_CMD_SET_PMKSA, bssid, pmkid);
6819}
6820
6821
6822static int nl80211_remove_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
6823{
6824 struct i802_bss *bss = priv;
6825 wpa_printf(MSG_DEBUG, "nl80211: Delete PMKID for " MACSTR,
6826 MAC2STR(bssid));
6827 return nl80211_pmkid(bss, NL80211_CMD_DEL_PMKSA, bssid, pmkid);
6828}
6829
6830
6831static int nl80211_flush_pmkid(void *priv)
6832{
6833 struct i802_bss *bss = priv;
6834 wpa_printf(MSG_DEBUG, "nl80211: Flush PMKIDs");
6835 return nl80211_pmkid(bss, NL80211_CMD_FLUSH_PMKSA, NULL, NULL);
6836}
6837
6838
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006839static void clean_survey_results(struct survey_results *survey_results)
6840{
6841 struct freq_survey *survey, *tmp;
6842
6843 if (dl_list_empty(&survey_results->survey_list))
6844 return;
6845
6846 dl_list_for_each_safe(survey, tmp, &survey_results->survey_list,
6847 struct freq_survey, list) {
6848 dl_list_del(&survey->list);
6849 os_free(survey);
6850 }
6851}
6852
6853
6854static void add_survey(struct nlattr **sinfo, u32 ifidx,
6855 struct dl_list *survey_list)
6856{
6857 struct freq_survey *survey;
6858
6859 survey = os_zalloc(sizeof(struct freq_survey));
6860 if (!survey)
6861 return;
6862
6863 survey->ifidx = ifidx;
6864 survey->freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
6865 survey->filled = 0;
6866
6867 if (sinfo[NL80211_SURVEY_INFO_NOISE]) {
6868 survey->nf = (int8_t)
6869 nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
6870 survey->filled |= SURVEY_HAS_NF;
6871 }
6872
6873 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]) {
6874 survey->channel_time =
6875 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]);
6876 survey->filled |= SURVEY_HAS_CHAN_TIME;
6877 }
6878
6879 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]) {
6880 survey->channel_time_busy =
6881 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]);
6882 survey->filled |= SURVEY_HAS_CHAN_TIME_BUSY;
6883 }
6884
6885 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]) {
6886 survey->channel_time_rx =
6887 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]);
6888 survey->filled |= SURVEY_HAS_CHAN_TIME_RX;
6889 }
6890
6891 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]) {
6892 survey->channel_time_tx =
6893 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]);
6894 survey->filled |= SURVEY_HAS_CHAN_TIME_TX;
6895 }
6896
6897 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)",
6898 survey->freq,
6899 survey->nf,
6900 (unsigned long int) survey->channel_time,
6901 (unsigned long int) survey->channel_time_busy,
6902 (unsigned long int) survey->channel_time_tx,
6903 (unsigned long int) survey->channel_time_rx,
6904 survey->filled);
6905
6906 dl_list_add_tail(survey_list, &survey->list);
6907}
6908
6909
6910static int check_survey_ok(struct nlattr **sinfo, u32 surveyed_freq,
6911 unsigned int freq_filter)
6912{
6913 if (!freq_filter)
6914 return 1;
6915
6916 return freq_filter == surveyed_freq;
6917}
6918
6919
6920static int survey_handler(struct nl_msg *msg, void *arg)
6921{
6922 struct nlattr *tb[NL80211_ATTR_MAX + 1];
6923 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6924 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
6925 struct survey_results *survey_results;
6926 u32 surveyed_freq = 0;
6927 u32 ifidx;
6928
6929 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
6930 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
6931 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
6932 };
6933
6934 survey_results = (struct survey_results *) arg;
6935
6936 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6937 genlmsg_attrlen(gnlh, 0), NULL);
6938
Dmitry Shmidt97672262014-02-03 13:02:54 -08006939 if (!tb[NL80211_ATTR_IFINDEX])
6940 return NL_SKIP;
6941
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006942 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
6943
6944 if (!tb[NL80211_ATTR_SURVEY_INFO])
6945 return NL_SKIP;
6946
6947 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
6948 tb[NL80211_ATTR_SURVEY_INFO],
6949 survey_policy))
6950 return NL_SKIP;
6951
6952 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY]) {
6953 wpa_printf(MSG_ERROR, "nl80211: Invalid survey data");
6954 return NL_SKIP;
6955 }
6956
6957 surveyed_freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
6958
6959 if (!check_survey_ok(sinfo, surveyed_freq,
6960 survey_results->freq_filter))
6961 return NL_SKIP;
6962
6963 if (survey_results->freq_filter &&
6964 survey_results->freq_filter != surveyed_freq) {
6965 wpa_printf(MSG_EXCESSIVE, "nl80211: Ignoring survey data for freq %d MHz",
6966 surveyed_freq);
6967 return NL_SKIP;
6968 }
6969
6970 add_survey(sinfo, ifidx, &survey_results->survey_list);
6971
6972 return NL_SKIP;
6973}
6974
6975
6976static int wpa_driver_nl80211_get_survey(void *priv, unsigned int freq)
6977{
6978 struct i802_bss *bss = priv;
6979 struct wpa_driver_nl80211_data *drv = bss->drv;
6980 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006981 int err;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006982 union wpa_event_data data;
6983 struct survey_results *survey_results;
6984
6985 os_memset(&data, 0, sizeof(data));
6986 survey_results = &data.survey_results;
6987
6988 dl_list_init(&survey_results->survey_list);
6989
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006990 msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006991 if (!msg)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006992 return -ENOBUFS;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006993
6994 if (freq)
6995 data.survey_results.freq_filter = freq;
6996
6997 do {
6998 wpa_printf(MSG_DEBUG, "nl80211: Fetch survey data");
6999 err = send_and_recv_msgs(drv, msg, survey_handler,
7000 survey_results);
7001 } while (err > 0);
7002
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007003 if (err)
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007004 wpa_printf(MSG_ERROR, "nl80211: Failed to process survey data");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007005 else
7006 wpa_supplicant_event(drv->ctx, EVENT_SURVEY, &data);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007007
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007008 clean_survey_results(survey_results);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007009 return err;
7010}
7011
7012
Dmitry Shmidt807291d2015-01-27 13:40:23 -08007013static void nl80211_set_rekey_info(void *priv, const u8 *kek, size_t kek_len,
7014 const u8 *kck, size_t kck_len,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007015 const u8 *replay_ctr)
7016{
7017 struct i802_bss *bss = priv;
7018 struct wpa_driver_nl80211_data *drv = bss->drv;
7019 struct nlattr *replay_nested;
7020 struct nl_msg *msg;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08007021 int ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007022
Dmitry Shmidtff787d52015-01-12 13:01:47 -08007023 if (!drv->set_rekey_offload)
7024 return;
7025
7026 wpa_printf(MSG_DEBUG, "nl80211: Set rekey offload");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007027 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_REKEY_OFFLOAD)) ||
7028 !(replay_nested = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA)) ||
Dmitry Shmidt807291d2015-01-27 13:40:23 -08007029 nla_put(msg, NL80211_REKEY_DATA_KEK, kek_len, kek) ||
7030 nla_put(msg, NL80211_REKEY_DATA_KCK, kck_len, kck) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007031 nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR, NL80211_REPLAY_CTR_LEN,
7032 replay_ctr)) {
7033 nl80211_nlmsg_clear(msg);
7034 nlmsg_free(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007035 return;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007036 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007037
7038 nla_nest_end(msg, replay_nested);
7039
Dmitry Shmidtff787d52015-01-12 13:01:47 -08007040 ret = send_and_recv_msgs(drv, msg, NULL, (void *) -1);
7041 if (ret == -EOPNOTSUPP) {
7042 wpa_printf(MSG_DEBUG,
7043 "nl80211: Driver does not support rekey offload");
7044 drv->set_rekey_offload = 0;
7045 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007046}
7047
7048
7049static void nl80211_send_null_frame(struct i802_bss *bss, const u8 *own_addr,
7050 const u8 *addr, int qos)
7051{
7052 /* send data frame to poll STA and check whether
7053 * this frame is ACKed */
7054 struct {
7055 struct ieee80211_hdr hdr;
7056 u16 qos_ctl;
7057 } STRUCT_PACKED nulldata;
7058 size_t size;
7059
7060 /* Send data frame to poll STA and check whether this frame is ACKed */
7061
7062 os_memset(&nulldata, 0, sizeof(nulldata));
7063
7064 if (qos) {
7065 nulldata.hdr.frame_control =
7066 IEEE80211_FC(WLAN_FC_TYPE_DATA,
7067 WLAN_FC_STYPE_QOS_NULL);
7068 size = sizeof(nulldata);
7069 } else {
7070 nulldata.hdr.frame_control =
7071 IEEE80211_FC(WLAN_FC_TYPE_DATA,
7072 WLAN_FC_STYPE_NULLFUNC);
7073 size = sizeof(struct ieee80211_hdr);
7074 }
7075
7076 nulldata.hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS);
7077 os_memcpy(nulldata.hdr.IEEE80211_DA_FROMDS, addr, ETH_ALEN);
7078 os_memcpy(nulldata.hdr.IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
7079 os_memcpy(nulldata.hdr.IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
7080
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007081 if (wpa_driver_nl80211_send_mlme(bss, (u8 *) &nulldata, size, 0, 0, 0,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007082 0, 0, NULL, 0) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007083 wpa_printf(MSG_DEBUG, "nl80211_send_null_frame: Failed to "
7084 "send poll frame");
7085}
7086
7087static void nl80211_poll_client(void *priv, const u8 *own_addr, const u8 *addr,
7088 int qos)
7089{
7090 struct i802_bss *bss = priv;
7091 struct wpa_driver_nl80211_data *drv = bss->drv;
7092 struct nl_msg *msg;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007093 int ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007094
7095 if (!drv->poll_command_supported) {
7096 nl80211_send_null_frame(bss, own_addr, addr, qos);
7097 return;
7098 }
7099
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007100 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_PROBE_CLIENT)) ||
7101 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
7102 nlmsg_free(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007103 return;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007104 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007105
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007106 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7107 if (ret < 0) {
7108 wpa_printf(MSG_DEBUG, "nl80211: Client probe request for "
7109 MACSTR " failed: ret=%d (%s)",
7110 MAC2STR(addr), ret, strerror(-ret));
7111 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007112}
7113
7114
7115static int nl80211_set_power_save(struct i802_bss *bss, int enabled)
7116{
7117 struct nl_msg *msg;
7118
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007119 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_POWER_SAVE)) ||
7120 nla_put_u32(msg, NL80211_ATTR_PS_STATE,
7121 enabled ? NL80211_PS_ENABLED : NL80211_PS_DISABLED)) {
7122 nlmsg_free(msg);
7123 return -ENOBUFS;
7124 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007125 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007126}
7127
7128
7129static int nl80211_set_p2p_powersave(void *priv, int legacy_ps, int opp_ps,
7130 int ctwindow)
7131{
7132 struct i802_bss *bss = priv;
7133
7134 wpa_printf(MSG_DEBUG, "nl80211: set_p2p_powersave (legacy_ps=%d "
7135 "opp_ps=%d ctwindow=%d)", legacy_ps, opp_ps, ctwindow);
7136
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08007137 if (opp_ps != -1 || ctwindow != -1) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007138#ifdef ANDROID_P2P
7139 wpa_driver_set_p2p_ps(priv, legacy_ps, opp_ps, ctwindow);
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08007140#else /* ANDROID_P2P */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007141 return -1; /* Not yet supported */
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08007142#endif /* ANDROID_P2P */
7143 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007144
7145 if (legacy_ps == -1)
7146 return 0;
7147 if (legacy_ps != 0 && legacy_ps != 1)
7148 return -1; /* Not yet supported */
7149
7150 return nl80211_set_power_save(bss, legacy_ps);
7151}
7152
7153
Dmitry Shmidt051af732013-10-22 13:52:46 -07007154static int nl80211_start_radar_detection(void *priv,
7155 struct hostapd_freq_params *freq)
Dmitry Shmidtea69e842013-05-13 14:52:28 -07007156{
7157 struct i802_bss *bss = priv;
7158 struct wpa_driver_nl80211_data *drv = bss->drv;
7159 struct nl_msg *msg;
7160 int ret;
7161
Dmitry Shmidt051af732013-10-22 13:52:46 -07007162 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)",
7163 freq->freq, freq->ht_enabled, freq->vht_enabled,
7164 freq->bandwidth, freq->center_freq1, freq->center_freq2);
7165
Dmitry Shmidtea69e842013-05-13 14:52:28 -07007166 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_RADAR)) {
7167 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support radar "
7168 "detection");
7169 return -1;
7170 }
7171
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007172 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_RADAR_DETECT)) ||
7173 nl80211_put_freq_params(msg, freq) < 0) {
7174 nlmsg_free(msg);
Dmitry Shmidtea69e842013-05-13 14:52:28 -07007175 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007176 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -07007177
7178 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7179 if (ret == 0)
7180 return 0;
7181 wpa_printf(MSG_DEBUG, "nl80211: Failed to start radar detection: "
7182 "%d (%s)", ret, strerror(-ret));
Dmitry Shmidtea69e842013-05-13 14:52:28 -07007183 return -1;
7184}
7185
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007186#ifdef CONFIG_TDLS
7187
7188static int nl80211_send_tdls_mgmt(void *priv, const u8 *dst, u8 action_code,
7189 u8 dialog_token, u16 status_code,
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07007190 u32 peer_capab, int initiator, const u8 *buf,
7191 size_t len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007192{
7193 struct i802_bss *bss = priv;
7194 struct wpa_driver_nl80211_data *drv = bss->drv;
7195 struct nl_msg *msg;
7196
7197 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
7198 return -EOPNOTSUPP;
7199
7200 if (!dst)
7201 return -EINVAL;
7202
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007203 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_TDLS_MGMT)) ||
7204 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
7205 nla_put_u8(msg, NL80211_ATTR_TDLS_ACTION, action_code) ||
7206 nla_put_u8(msg, NL80211_ATTR_TDLS_DIALOG_TOKEN, dialog_token) ||
7207 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status_code))
7208 goto fail;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07007209 if (peer_capab) {
7210 /*
7211 * The internal enum tdls_peer_capability definition is
7212 * currently identical with the nl80211 enum
7213 * nl80211_tdls_peer_capability, so no conversion is needed
7214 * here.
7215 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007216 if (nla_put_u32(msg, NL80211_ATTR_TDLS_PEER_CAPABILITY,
7217 peer_capab))
7218 goto fail;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07007219 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007220 if ((initiator &&
7221 nla_put_flag(msg, NL80211_ATTR_TDLS_INITIATOR)) ||
7222 nla_put(msg, NL80211_ATTR_IE, len, buf))
7223 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007224
7225 return send_and_recv_msgs(drv, msg, NULL, NULL);
7226
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007227fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007228 nlmsg_free(msg);
7229 return -ENOBUFS;
7230}
7231
7232
7233static int nl80211_tdls_oper(void *priv, enum tdls_oper oper, const u8 *peer)
7234{
7235 struct i802_bss *bss = priv;
7236 struct wpa_driver_nl80211_data *drv = bss->drv;
7237 struct nl_msg *msg;
7238 enum nl80211_tdls_operation nl80211_oper;
7239
7240 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
7241 return -EOPNOTSUPP;
7242
7243 switch (oper) {
7244 case TDLS_DISCOVERY_REQ:
7245 nl80211_oper = NL80211_TDLS_DISCOVERY_REQ;
7246 break;
7247 case TDLS_SETUP:
7248 nl80211_oper = NL80211_TDLS_SETUP;
7249 break;
7250 case TDLS_TEARDOWN:
7251 nl80211_oper = NL80211_TDLS_TEARDOWN;
7252 break;
7253 case TDLS_ENABLE_LINK:
7254 nl80211_oper = NL80211_TDLS_ENABLE_LINK;
7255 break;
7256 case TDLS_DISABLE_LINK:
7257 nl80211_oper = NL80211_TDLS_DISABLE_LINK;
7258 break;
7259 case TDLS_ENABLE:
7260 return 0;
7261 case TDLS_DISABLE:
7262 return 0;
7263 default:
7264 return -EINVAL;
7265 }
7266
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007267 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_TDLS_OPER)) ||
7268 nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, nl80211_oper) ||
7269 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer)) {
7270 nlmsg_free(msg);
7271 return -ENOBUFS;
7272 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007273
7274 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007275}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007276
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007277
7278static int
7279nl80211_tdls_enable_channel_switch(void *priv, const u8 *addr, u8 oper_class,
7280 const struct hostapd_freq_params *params)
7281{
7282 struct i802_bss *bss = priv;
7283 struct wpa_driver_nl80211_data *drv = bss->drv;
7284 struct nl_msg *msg;
7285 int ret = -ENOBUFS;
7286
7287 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT) ||
7288 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_CHANNEL_SWITCH))
7289 return -EOPNOTSUPP;
7290
7291 wpa_printf(MSG_DEBUG, "nl80211: Enable TDLS channel switch " MACSTR
7292 " oper_class=%u freq=%u",
7293 MAC2STR(addr), oper_class, params->freq);
7294 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_TDLS_CHANNEL_SWITCH);
7295 if (!msg ||
7296 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
7297 nla_put_u8(msg, NL80211_ATTR_OPER_CLASS, oper_class) ||
7298 (ret = nl80211_put_freq_params(msg, params))) {
7299 nlmsg_free(msg);
7300 wpa_printf(MSG_DEBUG, "nl80211: Could not build TDLS chan switch");
7301 return ret;
7302 }
7303
7304 return send_and_recv_msgs(drv, msg, NULL, NULL);
7305}
7306
7307
7308static int
7309nl80211_tdls_disable_channel_switch(void *priv, const u8 *addr)
7310{
7311 struct i802_bss *bss = priv;
7312 struct wpa_driver_nl80211_data *drv = bss->drv;
7313 struct nl_msg *msg;
7314
7315 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT) ||
7316 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_CHANNEL_SWITCH))
7317 return -EOPNOTSUPP;
7318
7319 wpa_printf(MSG_DEBUG, "nl80211: Disable TDLS channel switch " MACSTR,
7320 MAC2STR(addr));
7321 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_TDLS_CANCEL_CHANNEL_SWITCH);
7322 if (!msg ||
7323 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
7324 nlmsg_free(msg);
7325 wpa_printf(MSG_DEBUG,
7326 "nl80211: Could not build TDLS cancel chan switch");
7327 return -ENOBUFS;
7328 }
7329
7330 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007331}
7332
7333#endif /* CONFIG TDLS */
7334
7335
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007336static int driver_nl80211_set_key(const char *ifname, void *priv,
7337 enum wpa_alg alg, const u8 *addr,
7338 int key_idx, int set_tx,
7339 const u8 *seq, size_t seq_len,
7340 const u8 *key, size_t key_len)
7341{
7342 struct i802_bss *bss = priv;
7343 return wpa_driver_nl80211_set_key(ifname, bss, alg, addr, key_idx,
7344 set_tx, seq, seq_len, key, key_len);
7345}
7346
7347
7348static int driver_nl80211_scan2(void *priv,
7349 struct wpa_driver_scan_params *params)
7350{
7351 struct i802_bss *bss = priv;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007352#ifdef CONFIG_DRIVER_NL80211_QCA
7353 struct wpa_driver_nl80211_data *drv = bss->drv;
7354
7355 /*
7356 * Do a vendor specific scan if possible. If only_new_results is
7357 * set, do a normal scan since a kernel (cfg80211) BSS cache flush
7358 * cannot be achieved through a vendor scan. The below condition may
7359 * need to be modified if new scan flags are added in the future whose
7360 * functionality can only be achieved through a normal scan.
7361 */
7362 if (drv->scan_vendor_cmd_avail && !params->only_new_results)
7363 return wpa_driver_nl80211_vendor_scan(bss, params);
7364#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007365 return wpa_driver_nl80211_scan(bss, params);
7366}
7367
7368
7369static int driver_nl80211_deauthenticate(void *priv, const u8 *addr,
7370 int reason_code)
7371{
7372 struct i802_bss *bss = priv;
7373 return wpa_driver_nl80211_deauthenticate(bss, addr, reason_code);
7374}
7375
7376
7377static int driver_nl80211_authenticate(void *priv,
7378 struct wpa_driver_auth_params *params)
7379{
7380 struct i802_bss *bss = priv;
7381 return wpa_driver_nl80211_authenticate(bss, params);
7382}
7383
7384
7385static void driver_nl80211_deinit(void *priv)
7386{
7387 struct i802_bss *bss = priv;
7388 wpa_driver_nl80211_deinit(bss);
7389}
7390
7391
7392static int driver_nl80211_if_remove(void *priv, enum wpa_driver_if_type type,
7393 const char *ifname)
7394{
7395 struct i802_bss *bss = priv;
7396 return wpa_driver_nl80211_if_remove(bss, type, ifname);
7397}
7398
7399
7400static int driver_nl80211_send_mlme(void *priv, const u8 *data,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007401 size_t data_len, int noack,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007402 unsigned int freq,
7403 const u16 *csa_offs, size_t csa_offs_len)
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007404{
7405 struct i802_bss *bss = priv;
7406 return wpa_driver_nl80211_send_mlme(bss, data, data_len, noack,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007407 freq, 0, 0, 0, csa_offs,
7408 csa_offs_len);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007409}
7410
7411
7412static int driver_nl80211_sta_remove(void *priv, const u8 *addr)
7413{
7414 struct i802_bss *bss = priv;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007415 return wpa_driver_nl80211_sta_remove(bss, addr, -1, 0);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007416}
7417
7418
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007419static int driver_nl80211_set_sta_vlan(void *priv, const u8 *addr,
7420 const char *ifname, int vlan_id)
7421{
7422 struct i802_bss *bss = priv;
7423 return i802_set_sta_vlan(bss, addr, ifname, vlan_id);
7424}
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007425
7426
7427static int driver_nl80211_read_sta_data(void *priv,
7428 struct hostap_sta_driver_data *data,
7429 const u8 *addr)
7430{
7431 struct i802_bss *bss = priv;
7432 return i802_read_sta_data(bss, data, addr);
7433}
7434
7435
7436static int driver_nl80211_send_action(void *priv, unsigned int freq,
7437 unsigned int wait_time,
7438 const u8 *dst, const u8 *src,
7439 const u8 *bssid,
7440 const u8 *data, size_t data_len,
7441 int no_cck)
7442{
7443 struct i802_bss *bss = priv;
7444 return wpa_driver_nl80211_send_action(bss, freq, wait_time, dst, src,
7445 bssid, data, data_len, no_cck);
7446}
7447
7448
7449static int driver_nl80211_probe_req_report(void *priv, int report)
7450{
7451 struct i802_bss *bss = priv;
7452 return wpa_driver_nl80211_probe_req_report(bss, report);
7453}
7454
7455
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007456static int wpa_driver_nl80211_update_ft_ies(void *priv, const u8 *md,
7457 const u8 *ies, size_t ies_len)
7458{
7459 int ret;
7460 struct nl_msg *msg;
7461 struct i802_bss *bss = priv;
7462 struct wpa_driver_nl80211_data *drv = bss->drv;
7463 u16 mdid = WPA_GET_LE16(md);
7464
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007465 wpa_printf(MSG_DEBUG, "nl80211: Updating FT IEs");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007466 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_UPDATE_FT_IES)) ||
7467 nla_put(msg, NL80211_ATTR_IE, ies_len, ies) ||
7468 nla_put_u16(msg, NL80211_ATTR_MDID, mdid)) {
7469 nlmsg_free(msg);
7470 return -ENOBUFS;
7471 }
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007472
7473 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7474 if (ret) {
7475 wpa_printf(MSG_DEBUG, "nl80211: update_ft_ies failed "
7476 "err=%d (%s)", ret, strerror(-ret));
7477 }
7478
7479 return ret;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007480}
7481
7482
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007483const u8 * wpa_driver_nl80211_get_macaddr(void *priv)
7484{
7485 struct i802_bss *bss = priv;
7486 struct wpa_driver_nl80211_data *drv = bss->drv;
7487
7488 if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE)
7489 return NULL;
7490
7491 return bss->addr;
7492}
7493
7494
Dmitry Shmidt56052862013-10-04 10:23:25 -07007495static const char * scan_state_str(enum scan_states scan_state)
7496{
7497 switch (scan_state) {
7498 case NO_SCAN:
7499 return "NO_SCAN";
7500 case SCAN_REQUESTED:
7501 return "SCAN_REQUESTED";
7502 case SCAN_STARTED:
7503 return "SCAN_STARTED";
7504 case SCAN_COMPLETED:
7505 return "SCAN_COMPLETED";
7506 case SCAN_ABORTED:
7507 return "SCAN_ABORTED";
7508 case SCHED_SCAN_STARTED:
7509 return "SCHED_SCAN_STARTED";
7510 case SCHED_SCAN_STOPPED:
7511 return "SCHED_SCAN_STOPPED";
7512 case SCHED_SCAN_RESULTS:
7513 return "SCHED_SCAN_RESULTS";
7514 }
7515
7516 return "??";
7517}
7518
7519
7520static int wpa_driver_nl80211_status(void *priv, char *buf, size_t buflen)
7521{
7522 struct i802_bss *bss = priv;
7523 struct wpa_driver_nl80211_data *drv = bss->drv;
7524 int res;
7525 char *pos, *end;
7526
7527 pos = buf;
7528 end = buf + buflen;
7529
7530 res = os_snprintf(pos, end - pos,
7531 "ifindex=%d\n"
7532 "ifname=%s\n"
7533 "brname=%s\n"
7534 "addr=" MACSTR "\n"
7535 "freq=%d\n"
7536 "%s%s%s%s%s",
7537 bss->ifindex,
7538 bss->ifname,
7539 bss->brname,
7540 MAC2STR(bss->addr),
7541 bss->freq,
7542 bss->beacon_set ? "beacon_set=1\n" : "",
7543 bss->added_if_into_bridge ?
7544 "added_if_into_bridge=1\n" : "",
7545 bss->added_bridge ? "added_bridge=1\n" : "",
7546 bss->in_deinit ? "in_deinit=1\n" : "",
7547 bss->if_dynamic ? "if_dynamic=1\n" : "");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007548 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt56052862013-10-04 10:23:25 -07007549 return pos - buf;
7550 pos += res;
7551
7552 if (bss->wdev_id_set) {
7553 res = os_snprintf(pos, end - pos, "wdev_id=%llu\n",
7554 (unsigned long long) bss->wdev_id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007555 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt56052862013-10-04 10:23:25 -07007556 return pos - buf;
7557 pos += res;
7558 }
7559
7560 res = os_snprintf(pos, end - pos,
7561 "phyname=%s\n"
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007562 "perm_addr=" MACSTR "\n"
Dmitry Shmidt56052862013-10-04 10:23:25 -07007563 "drv_ifindex=%d\n"
7564 "operstate=%d\n"
7565 "scan_state=%s\n"
7566 "auth_bssid=" MACSTR "\n"
7567 "auth_attempt_bssid=" MACSTR "\n"
7568 "bssid=" MACSTR "\n"
7569 "prev_bssid=" MACSTR "\n"
7570 "associated=%d\n"
7571 "assoc_freq=%u\n"
7572 "monitor_sock=%d\n"
7573 "monitor_ifidx=%d\n"
7574 "monitor_refcount=%d\n"
7575 "last_mgmt_freq=%u\n"
7576 "eapol_tx_sock=%d\n"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007577 "%s%s%s%s%s%s%s%s%s%s%s%s%s",
Dmitry Shmidt56052862013-10-04 10:23:25 -07007578 drv->phyname,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007579 MAC2STR(drv->perm_addr),
Dmitry Shmidt56052862013-10-04 10:23:25 -07007580 drv->ifindex,
7581 drv->operstate,
7582 scan_state_str(drv->scan_state),
7583 MAC2STR(drv->auth_bssid),
7584 MAC2STR(drv->auth_attempt_bssid),
7585 MAC2STR(drv->bssid),
7586 MAC2STR(drv->prev_bssid),
7587 drv->associated,
7588 drv->assoc_freq,
7589 drv->monitor_sock,
7590 drv->monitor_ifidx,
7591 drv->monitor_refcount,
7592 drv->last_mgmt_freq,
7593 drv->eapol_tx_sock,
7594 drv->ignore_if_down_event ?
7595 "ignore_if_down_event=1\n" : "",
7596 drv->scan_complete_events ?
7597 "scan_complete_events=1\n" : "",
7598 drv->disabled_11b_rates ?
7599 "disabled_11b_rates=1\n" : "",
7600 drv->pending_remain_on_chan ?
7601 "pending_remain_on_chan=1\n" : "",
7602 drv->in_interface_list ? "in_interface_list=1\n" : "",
7603 drv->device_ap_sme ? "device_ap_sme=1\n" : "",
7604 drv->poll_command_supported ?
7605 "poll_command_supported=1\n" : "",
7606 drv->data_tx_status ? "data_tx_status=1\n" : "",
7607 drv->scan_for_auth ? "scan_for_auth=1\n" : "",
7608 drv->retry_auth ? "retry_auth=1\n" : "",
7609 drv->use_monitor ? "use_monitor=1\n" : "",
7610 drv->ignore_next_local_disconnect ?
7611 "ignore_next_local_disconnect=1\n" : "",
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07007612 drv->ignore_next_local_deauth ?
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007613 "ignore_next_local_deauth=1\n" : "");
7614 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt56052862013-10-04 10:23:25 -07007615 return pos - buf;
7616 pos += res;
7617
7618 if (drv->has_capability) {
7619 res = os_snprintf(pos, end - pos,
7620 "capa.key_mgmt=0x%x\n"
7621 "capa.enc=0x%x\n"
7622 "capa.auth=0x%x\n"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007623 "capa.flags=0x%llx\n"
7624 "capa.rrm_flags=0x%x\n"
Dmitry Shmidt56052862013-10-04 10:23:25 -07007625 "capa.max_scan_ssids=%d\n"
7626 "capa.max_sched_scan_ssids=%d\n"
7627 "capa.sched_scan_supported=%d\n"
7628 "capa.max_match_sets=%d\n"
7629 "capa.max_remain_on_chan=%u\n"
7630 "capa.max_stations=%u\n"
7631 "capa.probe_resp_offloads=0x%x\n"
7632 "capa.max_acl_mac_addrs=%u\n"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007633 "capa.num_multichan_concurrent=%u\n"
7634 "capa.mac_addr_rand_sched_scan_supported=%d\n"
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007635 "capa.mac_addr_rand_scan_supported=%d\n"
7636 "capa.conc_capab=%u\n"
7637 "capa.max_conc_chan_2_4=%u\n"
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007638 "capa.max_conc_chan_5_0=%u\n"
7639 "capa.max_sched_scan_plans=%u\n"
7640 "capa.max_sched_scan_plan_interval=%u\n"
7641 "capa.max_sched_scan_plan_iterations=%u\n",
Dmitry Shmidt56052862013-10-04 10:23:25 -07007642 drv->capa.key_mgmt,
7643 drv->capa.enc,
7644 drv->capa.auth,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007645 (unsigned long long) drv->capa.flags,
7646 drv->capa.rrm_flags,
Dmitry Shmidt56052862013-10-04 10:23:25 -07007647 drv->capa.max_scan_ssids,
7648 drv->capa.max_sched_scan_ssids,
7649 drv->capa.sched_scan_supported,
7650 drv->capa.max_match_sets,
7651 drv->capa.max_remain_on_chan,
7652 drv->capa.max_stations,
7653 drv->capa.probe_resp_offloads,
7654 drv->capa.max_acl_mac_addrs,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007655 drv->capa.num_multichan_concurrent,
7656 drv->capa.mac_addr_rand_sched_scan_supported,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007657 drv->capa.mac_addr_rand_scan_supported,
7658 drv->capa.conc_capab,
7659 drv->capa.max_conc_chan_2_4,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007660 drv->capa.max_conc_chan_5_0,
7661 drv->capa.max_sched_scan_plans,
7662 drv->capa.max_sched_scan_plan_interval,
7663 drv->capa.max_sched_scan_plan_iterations);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007664 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt56052862013-10-04 10:23:25 -07007665 return pos - buf;
7666 pos += res;
7667 }
7668
7669 return pos - buf;
7670}
7671
7672
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007673static int set_beacon_data(struct nl_msg *msg, struct beacon_data *settings)
7674{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007675 if ((settings->head &&
7676 nla_put(msg, NL80211_ATTR_BEACON_HEAD,
7677 settings->head_len, settings->head)) ||
7678 (settings->tail &&
7679 nla_put(msg, NL80211_ATTR_BEACON_TAIL,
7680 settings->tail_len, settings->tail)) ||
7681 (settings->beacon_ies &&
7682 nla_put(msg, NL80211_ATTR_IE,
7683 settings->beacon_ies_len, settings->beacon_ies)) ||
7684 (settings->proberesp_ies &&
7685 nla_put(msg, NL80211_ATTR_IE_PROBE_RESP,
7686 settings->proberesp_ies_len, settings->proberesp_ies)) ||
7687 (settings->assocresp_ies &&
7688 nla_put(msg, NL80211_ATTR_IE_ASSOC_RESP,
7689 settings->assocresp_ies_len, settings->assocresp_ies)) ||
7690 (settings->probe_resp &&
7691 nla_put(msg, NL80211_ATTR_PROBE_RESP,
7692 settings->probe_resp_len, settings->probe_resp)))
7693 return -ENOBUFS;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007694
7695 return 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007696}
7697
7698
7699static int nl80211_switch_channel(void *priv, struct csa_settings *settings)
7700{
7701 struct nl_msg *msg;
7702 struct i802_bss *bss = priv;
7703 struct wpa_driver_nl80211_data *drv = bss->drv;
7704 struct nlattr *beacon_csa;
7705 int ret = -ENOBUFS;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007706 int csa_off_len = 0;
7707 int i;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007708
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08007709 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 -08007710 settings->cs_count, settings->block_tx,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08007711 settings->freq_params.freq, settings->freq_params.bandwidth,
7712 settings->freq_params.center_freq1,
7713 settings->freq_params.center_freq2);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007714
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007715 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_AP_CSA)) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007716 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support channel switch command");
7717 return -EOPNOTSUPP;
7718 }
7719
7720 if ((drv->nlmode != NL80211_IFTYPE_AP) &&
7721 (drv->nlmode != NL80211_IFTYPE_P2P_GO))
7722 return -EOPNOTSUPP;
7723
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007724 /*
7725 * Remove empty counters, assuming Probe Response and Beacon frame
7726 * counters match. This implementation assumes that there are only two
7727 * counters.
7728 */
7729 if (settings->counter_offset_beacon[0] &&
7730 !settings->counter_offset_beacon[1]) {
7731 csa_off_len = 1;
7732 } else if (settings->counter_offset_beacon[1] &&
7733 !settings->counter_offset_beacon[0]) {
7734 csa_off_len = 1;
7735 settings->counter_offset_beacon[0] =
7736 settings->counter_offset_beacon[1];
7737 settings->counter_offset_presp[0] =
7738 settings->counter_offset_presp[1];
7739 } else if (settings->counter_offset_beacon[1] &&
7740 settings->counter_offset_beacon[0]) {
7741 csa_off_len = 2;
7742 } else {
7743 wpa_printf(MSG_ERROR, "nl80211: No CSA counters provided");
7744 return -EINVAL;
7745 }
7746
7747 /* Check CSA counters validity */
7748 if (drv->capa.max_csa_counters &&
7749 csa_off_len > drv->capa.max_csa_counters) {
7750 wpa_printf(MSG_ERROR,
7751 "nl80211: Too many CSA counters provided");
7752 return -EINVAL;
7753 }
7754
7755 if (!settings->beacon_csa.tail)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007756 return -EINVAL;
7757
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007758 for (i = 0; i < csa_off_len; i++) {
7759 u16 csa_c_off_bcn = settings->counter_offset_beacon[i];
7760 u16 csa_c_off_presp = settings->counter_offset_presp[i];
7761
7762 if ((settings->beacon_csa.tail_len <= csa_c_off_bcn) ||
7763 (settings->beacon_csa.tail[csa_c_off_bcn] !=
7764 settings->cs_count))
7765 return -EINVAL;
7766
7767 if (settings->beacon_csa.probe_resp &&
7768 ((settings->beacon_csa.probe_resp_len <=
7769 csa_c_off_presp) ||
7770 (settings->beacon_csa.probe_resp[csa_c_off_presp] !=
7771 settings->cs_count)))
7772 return -EINVAL;
7773 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007774
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007775 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_CHANNEL_SWITCH)) ||
7776 nla_put_u32(msg, NL80211_ATTR_CH_SWITCH_COUNT,
7777 settings->cs_count) ||
7778 (ret = nl80211_put_freq_params(msg, &settings->freq_params)) ||
7779 (settings->block_tx &&
7780 nla_put_flag(msg, NL80211_ATTR_CH_SWITCH_BLOCK_TX)))
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007781 goto error;
7782
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007783 /* beacon_after params */
7784 ret = set_beacon_data(msg, &settings->beacon_after);
7785 if (ret)
7786 goto error;
7787
7788 /* beacon_csa params */
7789 beacon_csa = nla_nest_start(msg, NL80211_ATTR_CSA_IES);
7790 if (!beacon_csa)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007791 goto fail;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007792
7793 ret = set_beacon_data(msg, &settings->beacon_csa);
7794 if (ret)
7795 goto error;
7796
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007797 if (nla_put(msg, NL80211_ATTR_CSA_C_OFF_BEACON,
7798 csa_off_len * sizeof(u16),
7799 settings->counter_offset_beacon) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007800 (settings->beacon_csa.probe_resp &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007801 nla_put(msg, NL80211_ATTR_CSA_C_OFF_PRESP,
7802 csa_off_len * sizeof(u16),
7803 settings->counter_offset_presp)))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007804 goto fail;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007805
7806 nla_nest_end(msg, beacon_csa);
7807 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7808 if (ret) {
7809 wpa_printf(MSG_DEBUG, "nl80211: switch_channel failed err=%d (%s)",
7810 ret, strerror(-ret));
7811 }
7812 return ret;
7813
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007814fail:
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007815 ret = -ENOBUFS;
7816error:
7817 nlmsg_free(msg);
7818 wpa_printf(MSG_DEBUG, "nl80211: Could not build channel switch request");
7819 return ret;
7820}
7821
7822
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007823static int nl80211_add_ts(void *priv, u8 tsid, const u8 *addr,
7824 u8 user_priority, u16 admitted_time)
7825{
7826 struct i802_bss *bss = priv;
7827 struct wpa_driver_nl80211_data *drv = bss->drv;
7828 struct nl_msg *msg;
7829 int ret;
7830
7831 wpa_printf(MSG_DEBUG,
7832 "nl80211: add_ts request: tsid=%u admitted_time=%u up=%d",
7833 tsid, admitted_time, user_priority);
7834
7835 if (!is_sta_interface(drv->nlmode))
7836 return -ENOTSUP;
7837
7838 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_ADD_TX_TS);
7839 if (!msg ||
7840 nla_put_u8(msg, NL80211_ATTR_TSID, tsid) ||
7841 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
7842 nla_put_u8(msg, NL80211_ATTR_USER_PRIO, user_priority) ||
7843 nla_put_u16(msg, NL80211_ATTR_ADMITTED_TIME, admitted_time)) {
7844 nlmsg_free(msg);
7845 return -ENOBUFS;
7846 }
7847
7848 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7849 if (ret)
7850 wpa_printf(MSG_DEBUG, "nl80211: add_ts failed err=%d (%s)",
7851 ret, strerror(-ret));
7852 return ret;
7853}
7854
7855
7856static int nl80211_del_ts(void *priv, u8 tsid, const u8 *addr)
7857{
7858 struct i802_bss *bss = priv;
7859 struct wpa_driver_nl80211_data *drv = bss->drv;
7860 struct nl_msg *msg;
7861 int ret;
7862
7863 wpa_printf(MSG_DEBUG, "nl80211: del_ts request: tsid=%u", tsid);
7864
7865 if (!is_sta_interface(drv->nlmode))
7866 return -ENOTSUP;
7867
7868 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_DEL_TX_TS)) ||
7869 nla_put_u8(msg, NL80211_ATTR_TSID, tsid) ||
7870 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
7871 nlmsg_free(msg);
7872 return -ENOBUFS;
7873 }
7874
7875 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7876 if (ret)
7877 wpa_printf(MSG_DEBUG, "nl80211: del_ts failed err=%d (%s)",
7878 ret, strerror(-ret));
7879 return ret;
7880}
7881
7882
Dmitry Shmidta38abf92014-03-06 13:38:44 -08007883#ifdef CONFIG_TESTING_OPTIONS
7884static int cmd_reply_handler(struct nl_msg *msg, void *arg)
7885{
7886 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
7887 struct wpabuf *buf = arg;
7888
7889 if (!buf)
7890 return NL_SKIP;
7891
7892 if ((size_t) genlmsg_attrlen(gnlh, 0) > wpabuf_tailroom(buf)) {
7893 wpa_printf(MSG_INFO, "nl80211: insufficient buffer space for reply");
7894 return NL_SKIP;
7895 }
7896
7897 wpabuf_put_data(buf, genlmsg_attrdata(gnlh, 0),
7898 genlmsg_attrlen(gnlh, 0));
7899
7900 return NL_SKIP;
7901}
7902#endif /* CONFIG_TESTING_OPTIONS */
7903
7904
7905static int vendor_reply_handler(struct nl_msg *msg, void *arg)
7906{
7907 struct nlattr *tb[NL80211_ATTR_MAX + 1];
7908 struct nlattr *nl_vendor_reply, *nl;
7909 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
7910 struct wpabuf *buf = arg;
7911 int rem;
7912
7913 if (!buf)
7914 return NL_SKIP;
7915
7916 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
7917 genlmsg_attrlen(gnlh, 0), NULL);
7918 nl_vendor_reply = tb[NL80211_ATTR_VENDOR_DATA];
7919
7920 if (!nl_vendor_reply)
7921 return NL_SKIP;
7922
7923 if ((size_t) nla_len(nl_vendor_reply) > wpabuf_tailroom(buf)) {
7924 wpa_printf(MSG_INFO, "nl80211: Vendor command: insufficient buffer space for reply");
7925 return NL_SKIP;
7926 }
7927
7928 nla_for_each_nested(nl, nl_vendor_reply, rem) {
7929 wpabuf_put_data(buf, nla_data(nl), nla_len(nl));
7930 }
7931
7932 return NL_SKIP;
7933}
7934
7935
7936static int nl80211_vendor_cmd(void *priv, unsigned int vendor_id,
7937 unsigned int subcmd, const u8 *data,
7938 size_t data_len, struct wpabuf *buf)
7939{
7940 struct i802_bss *bss = priv;
7941 struct wpa_driver_nl80211_data *drv = bss->drv;
7942 struct nl_msg *msg;
7943 int ret;
7944
Dmitry Shmidta38abf92014-03-06 13:38:44 -08007945#ifdef CONFIG_TESTING_OPTIONS
7946 if (vendor_id == 0xffffffff) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007947 msg = nlmsg_alloc();
7948 if (!msg)
7949 return -ENOMEM;
7950
Dmitry Shmidta38abf92014-03-06 13:38:44 -08007951 nl80211_cmd(drv, msg, 0, subcmd);
7952 if (nlmsg_append(msg, (void *) data, data_len, NLMSG_ALIGNTO) <
7953 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007954 goto fail;
Dmitry Shmidta38abf92014-03-06 13:38:44 -08007955 ret = send_and_recv_msgs(drv, msg, cmd_reply_handler, buf);
7956 if (ret)
7957 wpa_printf(MSG_DEBUG, "nl80211: command failed err=%d",
7958 ret);
7959 return ret;
7960 }
7961#endif /* CONFIG_TESTING_OPTIONS */
7962
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007963 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_VENDOR)) ||
7964 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, vendor_id) ||
7965 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD, subcmd) ||
7966 (data &&
7967 nla_put(msg, NL80211_ATTR_VENDOR_DATA, data_len, data)))
7968 goto fail;
Dmitry Shmidta38abf92014-03-06 13:38:44 -08007969
7970 ret = send_and_recv_msgs(drv, msg, vendor_reply_handler, buf);
7971 if (ret)
7972 wpa_printf(MSG_DEBUG, "nl80211: vendor command failed err=%d",
7973 ret);
7974 return ret;
7975
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007976fail:
Dmitry Shmidta38abf92014-03-06 13:38:44 -08007977 nlmsg_free(msg);
7978 return -ENOBUFS;
7979}
7980
7981
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007982static int nl80211_set_qos_map(void *priv, const u8 *qos_map_set,
7983 u8 qos_map_set_len)
7984{
7985 struct i802_bss *bss = priv;
7986 struct wpa_driver_nl80211_data *drv = bss->drv;
7987 struct nl_msg *msg;
7988 int ret;
7989
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007990 wpa_hexdump(MSG_DEBUG, "nl80211: Setting QoS Map",
7991 qos_map_set, qos_map_set_len);
7992
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007993 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_QOS_MAP)) ||
7994 nla_put(msg, NL80211_ATTR_QOS_MAP, qos_map_set_len, qos_map_set)) {
7995 nlmsg_free(msg);
7996 return -ENOBUFS;
7997 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007998
7999 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8000 if (ret)
8001 wpa_printf(MSG_DEBUG, "nl80211: Setting QoS Map failed");
8002
8003 return ret;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008004}
8005
8006
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07008007static int nl80211_set_wowlan(void *priv,
8008 const struct wowlan_triggers *triggers)
8009{
8010 struct i802_bss *bss = priv;
8011 struct wpa_driver_nl80211_data *drv = bss->drv;
8012 struct nl_msg *msg;
8013 struct nlattr *wowlan_triggers;
8014 int ret;
8015
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07008016 wpa_printf(MSG_DEBUG, "nl80211: Setting wowlan");
8017
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008018 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_SET_WOWLAN)) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008019 !(wowlan_triggers = nla_nest_start(msg,
8020 NL80211_ATTR_WOWLAN_TRIGGERS)) ||
8021 (triggers->any &&
8022 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
8023 (triggers->disconnect &&
8024 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
8025 (triggers->magic_pkt &&
8026 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
8027 (triggers->gtk_rekey_failure &&
8028 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
8029 (triggers->eap_identity_req &&
8030 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
8031 (triggers->four_way_handshake &&
8032 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
8033 (triggers->rfkill_release &&
8034 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))) {
8035 nlmsg_free(msg);
8036 return -ENOBUFS;
8037 }
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07008038
8039 nla_nest_end(msg, wowlan_triggers);
8040
8041 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8042 if (ret)
8043 wpa_printf(MSG_DEBUG, "nl80211: Setting wowlan failed");
8044
8045 return ret;
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07008046}
8047
8048
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008049#ifdef CONFIG_DRIVER_NL80211_QCA
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008050static int nl80211_roaming(void *priv, int allowed, const u8 *bssid)
8051{
8052 struct i802_bss *bss = priv;
8053 struct wpa_driver_nl80211_data *drv = bss->drv;
8054 struct nl_msg *msg;
8055 struct nlattr *params;
8056
8057 wpa_printf(MSG_DEBUG, "nl80211: Roaming policy: allowed=%d", allowed);
8058
8059 if (!drv->roaming_vendor_cmd_avail) {
8060 wpa_printf(MSG_DEBUG,
8061 "nl80211: Ignore roaming policy change since driver does not provide command for setting it");
8062 return -1;
8063 }
8064
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008065 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
8066 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
8067 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
8068 QCA_NL80211_VENDOR_SUBCMD_ROAMING) ||
8069 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
8070 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_ROAMING_POLICY,
8071 allowed ? QCA_ROAMING_ALLOWED_WITHIN_ESS :
8072 QCA_ROAMING_NOT_ALLOWED) ||
8073 (bssid &&
8074 nla_put(msg, QCA_WLAN_VENDOR_ATTR_MAC_ADDR, ETH_ALEN, bssid))) {
8075 nlmsg_free(msg);
8076 return -1;
8077 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008078 nla_nest_end(msg, params);
8079
8080 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008081}
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008082#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008083
8084
8085static int nl80211_set_mac_addr(void *priv, const u8 *addr)
8086{
8087 struct i802_bss *bss = priv;
8088 struct wpa_driver_nl80211_data *drv = bss->drv;
8089 int new_addr = addr != NULL;
8090
8091 if (!addr)
8092 addr = drv->perm_addr;
8093
8094 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0) < 0)
8095 return -1;
8096
8097 if (linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname, addr) < 0)
8098 {
8099 wpa_printf(MSG_DEBUG,
8100 "nl80211: failed to set_mac_addr for %s to " MACSTR,
8101 bss->ifname, MAC2STR(addr));
8102 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname,
8103 1) < 0) {
8104 wpa_printf(MSG_DEBUG,
8105 "nl80211: Could not restore interface UP after failed set_mac_addr");
8106 }
8107 return -1;
8108 }
8109
8110 wpa_printf(MSG_DEBUG, "nl80211: set_mac_addr for %s to " MACSTR,
8111 bss->ifname, MAC2STR(addr));
8112 drv->addr_changed = new_addr;
8113 os_memcpy(bss->addr, addr, ETH_ALEN);
8114
8115 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1) < 0)
8116 {
8117 wpa_printf(MSG_DEBUG,
8118 "nl80211: Could not restore interface UP after set_mac_addr");
8119 }
8120
8121 return 0;
8122}
8123
8124
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008125#ifdef CONFIG_MESH
8126
8127static int wpa_driver_nl80211_init_mesh(void *priv)
8128{
8129 if (wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_MESH_POINT)) {
8130 wpa_printf(MSG_INFO,
8131 "nl80211: Failed to set interface into mesh mode");
8132 return -1;
8133 }
8134 return 0;
8135}
8136
8137
Dmitry Shmidtff787d52015-01-12 13:01:47 -08008138static int nl80211_put_mesh_id(struct nl_msg *msg, const u8 *mesh_id,
8139 size_t mesh_id_len)
8140{
8141 if (mesh_id) {
8142 wpa_hexdump_ascii(MSG_DEBUG, " * Mesh ID (SSID)",
8143 mesh_id, mesh_id_len);
8144 return nla_put(msg, NL80211_ATTR_MESH_ID, mesh_id_len, mesh_id);
8145 }
8146
8147 return 0;
8148}
8149
8150
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008151static int nl80211_join_mesh(struct i802_bss *bss,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008152 struct wpa_driver_mesh_join_params *params)
8153{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008154 struct wpa_driver_nl80211_data *drv = bss->drv;
8155 struct nl_msg *msg;
8156 struct nlattr *container;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08008157 int ret = -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008158
8159 wpa_printf(MSG_DEBUG, "nl80211: mesh join (ifindex=%d)", drv->ifindex);
8160 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_JOIN_MESH);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08008161 if (!msg ||
8162 nl80211_put_freq_params(msg, &params->freq) ||
8163 nl80211_put_basic_rates(msg, params->basic_rates) ||
8164 nl80211_put_mesh_id(msg, params->meshid, params->meshid_len) ||
8165 nl80211_put_beacon_int(msg, params->beacon_int))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008166 goto fail;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008167
8168 wpa_printf(MSG_DEBUG, " * flags=%08X", params->flags);
8169
8170 container = nla_nest_start(msg, NL80211_ATTR_MESH_SETUP);
8171 if (!container)
8172 goto fail;
8173
8174 if (params->ies) {
8175 wpa_hexdump(MSG_DEBUG, " * IEs", params->ies, params->ie_len);
8176 if (nla_put(msg, NL80211_MESH_SETUP_IE, params->ie_len,
8177 params->ies))
8178 goto fail;
8179 }
8180 /* WPA_DRIVER_MESH_FLAG_OPEN_AUTH is treated as default by nl80211 */
8181 if (params->flags & WPA_DRIVER_MESH_FLAG_SAE_AUTH) {
8182 if (nla_put_u8(msg, NL80211_MESH_SETUP_AUTH_PROTOCOL, 0x1) ||
8183 nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_AUTH))
8184 goto fail;
8185 }
8186 if ((params->flags & WPA_DRIVER_MESH_FLAG_AMPE) &&
8187 nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_AMPE))
8188 goto fail;
8189 if ((params->flags & WPA_DRIVER_MESH_FLAG_USER_MPM) &&
8190 nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_MPM))
8191 goto fail;
8192 nla_nest_end(msg, container);
8193
8194 container = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
8195 if (!container)
8196 goto fail;
8197
8198 if (!(params->conf.flags & WPA_DRIVER_MESH_CONF_FLAG_AUTO_PLINKS) &&
8199 nla_put_u32(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS, 0))
8200 goto fail;
8201 if ((params->conf.flags & WPA_DRIVER_MESH_FLAG_DRIVER_MPM) &&
8202 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
8203 params->max_peer_links))
8204 goto fail;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08008205
8206 /*
8207 * Set NL80211_MESHCONF_PLINK_TIMEOUT even if user mpm is used because
8208 * the timer could disconnect stations even in that case.
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08008209 */
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008210 if (nla_put_u32(msg, NL80211_MESHCONF_PLINK_TIMEOUT,
8211 params->conf.peer_link_timeout)) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08008212 wpa_printf(MSG_ERROR, "nl80211: Failed to set PLINK_TIMEOUT");
8213 goto fail;
8214 }
8215
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008216 nla_nest_end(msg, container);
8217
8218 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8219 msg = NULL;
8220 if (ret) {
8221 wpa_printf(MSG_DEBUG, "nl80211: mesh join failed: ret=%d (%s)",
8222 ret, strerror(-ret));
8223 goto fail;
8224 }
8225 ret = 0;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08008226 bss->freq = params->freq.freq;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008227 wpa_printf(MSG_DEBUG, "nl80211: mesh join request send successfully");
8228
8229fail:
8230 nlmsg_free(msg);
8231 return ret;
8232}
8233
8234
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008235static int
8236wpa_driver_nl80211_join_mesh(void *priv,
8237 struct wpa_driver_mesh_join_params *params)
8238{
8239 struct i802_bss *bss = priv;
8240 int ret, timeout;
8241
8242 timeout = params->conf.peer_link_timeout;
8243
8244 /* Disable kernel inactivity timer */
8245 if (params->flags & WPA_DRIVER_MESH_FLAG_USER_MPM)
8246 params->conf.peer_link_timeout = 0;
8247
8248 ret = nl80211_join_mesh(bss, params);
8249 if (ret == -EINVAL && params->conf.peer_link_timeout == 0) {
8250 wpa_printf(MSG_DEBUG,
8251 "nl80211: Mesh join retry for peer_link_timeout");
8252 /*
8253 * Old kernel does not support setting
8254 * NL80211_MESHCONF_PLINK_TIMEOUT to zero, so set 60 seconds
8255 * into future from peer_link_timeout.
8256 */
8257 params->conf.peer_link_timeout = timeout + 60;
8258 ret = nl80211_join_mesh(priv, params);
8259 }
8260
8261 params->conf.peer_link_timeout = timeout;
8262 return ret;
8263}
8264
8265
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008266static int wpa_driver_nl80211_leave_mesh(void *priv)
8267{
8268 struct i802_bss *bss = priv;
8269 struct wpa_driver_nl80211_data *drv = bss->drv;
8270 struct nl_msg *msg;
8271 int ret;
8272
8273 wpa_printf(MSG_DEBUG, "nl80211: mesh leave (ifindex=%d)", drv->ifindex);
8274 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_LEAVE_MESH);
8275 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8276 if (ret) {
8277 wpa_printf(MSG_DEBUG, "nl80211: mesh leave failed: ret=%d (%s)",
8278 ret, strerror(-ret));
8279 } else {
8280 wpa_printf(MSG_DEBUG,
8281 "nl80211: mesh leave request send successfully");
8282 }
8283
8284 if (wpa_driver_nl80211_set_mode(drv->first_bss,
8285 NL80211_IFTYPE_STATION)) {
8286 wpa_printf(MSG_INFO,
8287 "nl80211: Failed to set interface into station mode");
8288 }
8289 return ret;
8290}
8291
8292#endif /* CONFIG_MESH */
8293
8294
8295static int wpa_driver_br_add_ip_neigh(void *priv, u8 version,
8296 const u8 *ipaddr, int prefixlen,
8297 const u8 *addr)
8298{
8299#ifdef CONFIG_LIBNL3_ROUTE
8300 struct i802_bss *bss = priv;
8301 struct wpa_driver_nl80211_data *drv = bss->drv;
8302 struct rtnl_neigh *rn;
8303 struct nl_addr *nl_ipaddr = NULL;
8304 struct nl_addr *nl_lladdr = NULL;
8305 int family, addrsize;
8306 int res;
8307
8308 if (!ipaddr || prefixlen == 0 || !addr)
8309 return -EINVAL;
8310
8311 if (bss->br_ifindex == 0) {
8312 wpa_printf(MSG_DEBUG,
8313 "nl80211: bridge must be set before adding an ip neigh to it");
8314 return -1;
8315 }
8316
8317 if (!drv->rtnl_sk) {
8318 wpa_printf(MSG_DEBUG,
8319 "nl80211: nl_sock for NETLINK_ROUTE is not initialized");
8320 return -1;
8321 }
8322
8323 if (version == 4) {
8324 family = AF_INET;
8325 addrsize = 4;
8326 } else if (version == 6) {
8327 family = AF_INET6;
8328 addrsize = 16;
8329 } else {
8330 return -EINVAL;
8331 }
8332
8333 rn = rtnl_neigh_alloc();
8334 if (rn == NULL)
8335 return -ENOMEM;
8336
8337 /* set the destination ip address for neigh */
8338 nl_ipaddr = nl_addr_build(family, (void *) ipaddr, addrsize);
8339 if (nl_ipaddr == NULL) {
8340 wpa_printf(MSG_DEBUG, "nl80211: nl_ipaddr build failed");
8341 res = -ENOMEM;
8342 goto errout;
8343 }
8344 nl_addr_set_prefixlen(nl_ipaddr, prefixlen);
8345 res = rtnl_neigh_set_dst(rn, nl_ipaddr);
8346 if (res) {
8347 wpa_printf(MSG_DEBUG,
8348 "nl80211: neigh set destination addr failed");
8349 goto errout;
8350 }
8351
8352 /* set the corresponding lladdr for neigh */
8353 nl_lladdr = nl_addr_build(AF_BRIDGE, (u8 *) addr, ETH_ALEN);
8354 if (nl_lladdr == NULL) {
8355 wpa_printf(MSG_DEBUG, "nl80211: neigh set lladdr failed");
8356 res = -ENOMEM;
8357 goto errout;
8358 }
8359 rtnl_neigh_set_lladdr(rn, nl_lladdr);
8360
8361 rtnl_neigh_set_ifindex(rn, bss->br_ifindex);
8362 rtnl_neigh_set_state(rn, NUD_PERMANENT);
8363
8364 res = rtnl_neigh_add(drv->rtnl_sk, rn, NLM_F_CREATE);
8365 if (res) {
8366 wpa_printf(MSG_DEBUG,
8367 "nl80211: Adding bridge ip neigh failed: %s",
8368 strerror(errno));
8369 }
8370errout:
8371 if (nl_lladdr)
8372 nl_addr_put(nl_lladdr);
8373 if (nl_ipaddr)
8374 nl_addr_put(nl_ipaddr);
8375 if (rn)
8376 rtnl_neigh_put(rn);
8377 return res;
8378#else /* CONFIG_LIBNL3_ROUTE */
8379 return -1;
8380#endif /* CONFIG_LIBNL3_ROUTE */
8381}
8382
8383
8384static int wpa_driver_br_delete_ip_neigh(void *priv, u8 version,
8385 const u8 *ipaddr)
8386{
8387#ifdef CONFIG_LIBNL3_ROUTE
8388 struct i802_bss *bss = priv;
8389 struct wpa_driver_nl80211_data *drv = bss->drv;
8390 struct rtnl_neigh *rn;
8391 struct nl_addr *nl_ipaddr;
8392 int family, addrsize;
8393 int res;
8394
8395 if (!ipaddr)
8396 return -EINVAL;
8397
8398 if (version == 4) {
8399 family = AF_INET;
8400 addrsize = 4;
8401 } else if (version == 6) {
8402 family = AF_INET6;
8403 addrsize = 16;
8404 } else {
8405 return -EINVAL;
8406 }
8407
8408 if (bss->br_ifindex == 0) {
8409 wpa_printf(MSG_DEBUG,
8410 "nl80211: bridge must be set to delete an ip neigh");
8411 return -1;
8412 }
8413
8414 if (!drv->rtnl_sk) {
8415 wpa_printf(MSG_DEBUG,
8416 "nl80211: nl_sock for NETLINK_ROUTE is not initialized");
8417 return -1;
8418 }
8419
8420 rn = rtnl_neigh_alloc();
8421 if (rn == NULL)
8422 return -ENOMEM;
8423
8424 /* set the destination ip address for neigh */
8425 nl_ipaddr = nl_addr_build(family, (void *) ipaddr, addrsize);
8426 if (nl_ipaddr == NULL) {
8427 wpa_printf(MSG_DEBUG, "nl80211: nl_ipaddr build failed");
8428 res = -ENOMEM;
8429 goto errout;
8430 }
8431 res = rtnl_neigh_set_dst(rn, nl_ipaddr);
8432 if (res) {
8433 wpa_printf(MSG_DEBUG,
8434 "nl80211: neigh set destination addr failed");
8435 goto errout;
8436 }
8437
8438 rtnl_neigh_set_ifindex(rn, bss->br_ifindex);
8439
8440 res = rtnl_neigh_delete(drv->rtnl_sk, rn, 0);
8441 if (res) {
8442 wpa_printf(MSG_DEBUG,
8443 "nl80211: Deleting bridge ip neigh failed: %s",
8444 strerror(errno));
8445 }
8446errout:
8447 if (nl_ipaddr)
8448 nl_addr_put(nl_ipaddr);
8449 if (rn)
8450 rtnl_neigh_put(rn);
8451 return res;
8452#else /* CONFIG_LIBNL3_ROUTE */
8453 return -1;
8454#endif /* CONFIG_LIBNL3_ROUTE */
8455}
8456
8457
8458static int linux_write_system_file(const char *path, unsigned int val)
8459{
8460 char buf[50];
8461 int fd, len;
8462
8463 len = os_snprintf(buf, sizeof(buf), "%u\n", val);
8464 if (os_snprintf_error(sizeof(buf), len))
8465 return -1;
8466
8467 fd = open(path, O_WRONLY);
8468 if (fd < 0)
8469 return -1;
8470
8471 if (write(fd, buf, len) < 0) {
8472 wpa_printf(MSG_DEBUG,
8473 "nl80211: Failed to write Linux system file: %s with the value of %d",
8474 path, val);
8475 close(fd);
8476 return -1;
8477 }
8478 close(fd);
8479
8480 return 0;
8481}
8482
8483
8484static const char * drv_br_port_attr_str(enum drv_br_port_attr attr)
8485{
8486 switch (attr) {
8487 case DRV_BR_PORT_ATTR_PROXYARP:
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07008488 return "proxyarp_wifi";
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008489 case DRV_BR_PORT_ATTR_HAIRPIN_MODE:
8490 return "hairpin_mode";
8491 }
8492
8493 return NULL;
8494}
8495
8496
8497static int wpa_driver_br_port_set_attr(void *priv, enum drv_br_port_attr attr,
8498 unsigned int val)
8499{
8500 struct i802_bss *bss = priv;
8501 char path[128];
8502 const char *attr_txt;
8503
8504 attr_txt = drv_br_port_attr_str(attr);
8505 if (attr_txt == NULL)
8506 return -EINVAL;
8507
8508 os_snprintf(path, sizeof(path), "/sys/class/net/%s/brport/%s",
8509 bss->ifname, attr_txt);
8510
8511 if (linux_write_system_file(path, val))
8512 return -1;
8513
8514 return 0;
8515}
8516
8517
8518static const char * drv_br_net_param_str(enum drv_br_net_param param)
8519{
8520 switch (param) {
8521 case DRV_BR_NET_PARAM_GARP_ACCEPT:
8522 return "arp_accept";
Dmitry Shmidt83474442015-04-15 13:47:09 -07008523 default:
8524 return NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008525 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008526}
8527
8528
8529static int wpa_driver_br_set_net_param(void *priv, enum drv_br_net_param param,
8530 unsigned int val)
8531{
8532 struct i802_bss *bss = priv;
8533 char path[128];
8534 const char *param_txt;
8535 int ip_version = 4;
8536
Dmitry Shmidt83474442015-04-15 13:47:09 -07008537 if (param == DRV_BR_MULTICAST_SNOOPING) {
8538 os_snprintf(path, sizeof(path),
8539 "/sys/devices/virtual/net/%s/bridge/multicast_snooping",
8540 bss->brname);
8541 goto set_val;
8542 }
8543
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008544 param_txt = drv_br_net_param_str(param);
8545 if (param_txt == NULL)
8546 return -EINVAL;
8547
8548 switch (param) {
8549 case DRV_BR_NET_PARAM_GARP_ACCEPT:
8550 ip_version = 4;
8551 break;
8552 default:
8553 return -EINVAL;
8554 }
8555
8556 os_snprintf(path, sizeof(path), "/proc/sys/net/ipv%d/conf/%s/%s",
8557 ip_version, bss->brname, param_txt);
8558
Dmitry Shmidt83474442015-04-15 13:47:09 -07008559set_val:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008560 if (linux_write_system_file(path, val))
8561 return -1;
8562
8563 return 0;
8564}
8565
8566
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008567#ifdef CONFIG_DRIVER_NL80211_QCA
8568
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008569static int hw_mode_to_qca_acs(enum hostapd_hw_mode hw_mode)
8570{
8571 switch (hw_mode) {
8572 case HOSTAPD_MODE_IEEE80211B:
8573 return QCA_ACS_MODE_IEEE80211B;
8574 case HOSTAPD_MODE_IEEE80211G:
8575 return QCA_ACS_MODE_IEEE80211G;
8576 case HOSTAPD_MODE_IEEE80211A:
8577 return QCA_ACS_MODE_IEEE80211A;
8578 case HOSTAPD_MODE_IEEE80211AD:
8579 return QCA_ACS_MODE_IEEE80211AD;
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07008580 case HOSTAPD_MODE_IEEE80211ANY:
8581 return QCA_ACS_MODE_IEEE80211ANY;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008582 default:
8583 return -1;
8584 }
8585}
8586
8587
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008588static int add_acs_freq_list(struct nl_msg *msg, const int *freq_list)
8589{
8590 int i, len, ret;
8591 u32 *freqs;
8592
8593 if (!freq_list)
8594 return 0;
8595 len = int_array_len(freq_list);
8596 freqs = os_malloc(sizeof(u32) * len);
8597 if (!freqs)
8598 return -1;
8599 for (i = 0; i < len; i++)
8600 freqs[i] = freq_list[i];
8601 ret = nla_put(msg, QCA_WLAN_VENDOR_ATTR_ACS_FREQ_LIST,
8602 sizeof(u32) * len, freqs);
8603 os_free(freqs);
8604 return ret;
8605}
8606
8607
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008608static int wpa_driver_do_acs(void *priv, struct drv_acs_params *params)
8609{
8610 struct i802_bss *bss = priv;
8611 struct wpa_driver_nl80211_data *drv = bss->drv;
8612 struct nl_msg *msg;
8613 struct nlattr *data;
8614 int ret;
8615 int mode;
8616
8617 mode = hw_mode_to_qca_acs(params->hw_mode);
8618 if (mode < 0)
8619 return -1;
8620
8621 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
8622 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
8623 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
8624 QCA_NL80211_VENDOR_SUBCMD_DO_ACS) ||
8625 !(data = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
8626 nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_ACS_HW_MODE, mode) ||
8627 (params->ht_enabled &&
8628 nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_HT_ENABLED)) ||
8629 (params->ht40_enabled &&
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07008630 nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_HT40_ENABLED)) ||
8631 (params->vht_enabled &&
8632 nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_VHT_ENABLED)) ||
8633 nla_put_u16(msg, QCA_WLAN_VENDOR_ATTR_ACS_CHWIDTH,
8634 params->ch_width) ||
8635 (params->ch_list_len &&
8636 nla_put(msg, QCA_WLAN_VENDOR_ATTR_ACS_CH_LIST, params->ch_list_len,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008637 params->ch_list)) ||
8638 add_acs_freq_list(msg, params->freq_list)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008639 nlmsg_free(msg);
8640 return -ENOBUFS;
8641 }
8642 nla_nest_end(msg, data);
8643
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07008644 wpa_printf(MSG_DEBUG,
8645 "nl80211: ACS Params: HW_MODE: %d HT: %d HT40: %d VHT: %d BW: %d CH_LIST_LEN: %u",
8646 params->hw_mode, params->ht_enabled, params->ht40_enabled,
8647 params->vht_enabled, params->ch_width, params->ch_list_len);
8648
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008649 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8650 if (ret) {
8651 wpa_printf(MSG_DEBUG,
8652 "nl80211: Failed to invoke driver ACS function: %s",
8653 strerror(errno));
8654 }
8655 return ret;
8656}
8657
8658
Ravi Joshie6ccb162015-07-16 17:45:41 -07008659static int nl80211_set_band(void *priv, enum set_band band)
8660{
8661 struct i802_bss *bss = priv;
8662 struct wpa_driver_nl80211_data *drv = bss->drv;
8663 struct nl_msg *msg;
8664 struct nlattr *data;
8665 int ret;
8666 enum qca_set_band qca_band;
8667
8668 if (!drv->setband_vendor_cmd_avail)
8669 return -1;
8670
8671 switch (band) {
8672 case WPA_SETBAND_AUTO:
8673 qca_band = QCA_SETBAND_AUTO;
8674 break;
8675 case WPA_SETBAND_5G:
8676 qca_band = QCA_SETBAND_5G;
8677 break;
8678 case WPA_SETBAND_2G:
8679 qca_band = QCA_SETBAND_2G;
8680 break;
8681 default:
8682 return -1;
8683 }
8684
8685 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
8686 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
8687 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
8688 QCA_NL80211_VENDOR_SUBCMD_SETBAND) ||
8689 !(data = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
8690 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_SETBAND_VALUE, qca_band)) {
8691 nlmsg_free(msg);
8692 return -ENOBUFS;
8693 }
8694 nla_nest_end(msg, data);
8695
8696 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8697 if (ret) {
8698 wpa_printf(MSG_DEBUG,
8699 "nl80211: Driver setband function failed: %s",
8700 strerror(errno));
8701 }
8702 return ret;
8703}
8704
8705
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008706struct nl80211_pcl {
8707 unsigned int num;
8708 unsigned int *freq_list;
8709};
8710
8711static int preferred_freq_info_handler(struct nl_msg *msg, void *arg)
8712{
8713 struct nlattr *tb[NL80211_ATTR_MAX + 1];
8714 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
8715 struct nl80211_pcl *param = arg;
8716 struct nlattr *nl_vend, *attr;
8717 enum qca_iface_type iface_type;
8718 struct nlattr *tb_vendor[QCA_WLAN_VENDOR_ATTR_MAX + 1];
8719 unsigned int num, max_num;
8720 u32 *freqs;
8721
8722 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
8723 genlmsg_attrlen(gnlh, 0), NULL);
8724
8725 nl_vend = tb[NL80211_ATTR_VENDOR_DATA];
8726 if (!nl_vend)
8727 return NL_SKIP;
8728
8729 nla_parse(tb_vendor, QCA_WLAN_VENDOR_ATTR_MAX,
8730 nla_data(nl_vend), nla_len(nl_vend), NULL);
8731
8732 attr = tb_vendor[
8733 QCA_WLAN_VENDOR_ATTR_GET_PREFERRED_FREQ_LIST_IFACE_TYPE];
8734 if (!attr) {
8735 wpa_printf(MSG_ERROR, "nl80211: iface_type couldn't be found");
8736 param->num = 0;
8737 return NL_SKIP;
8738 }
8739
8740 iface_type = (enum qca_iface_type) nla_get_u32(attr);
8741 wpa_printf(MSG_DEBUG, "nl80211: Driver returned iface_type=%d",
8742 iface_type);
8743
8744 attr = tb_vendor[QCA_WLAN_VENDOR_ATTR_GET_PREFERRED_FREQ_LIST];
8745 if (!attr) {
8746 wpa_printf(MSG_ERROR,
8747 "nl80211: preferred_freq_list couldn't be found");
8748 param->num = 0;
8749 return NL_SKIP;
8750 }
8751
8752 /*
8753 * param->num has the maximum number of entries for which there
8754 * is room in the freq_list provided by the caller.
8755 */
8756 freqs = nla_data(attr);
8757 max_num = nla_len(attr) / sizeof(u32);
8758 if (max_num > param->num)
8759 max_num = param->num;
8760 for (num = 0; num < max_num; num++)
8761 param->freq_list[num] = freqs[num];
8762 param->num = num;
8763
8764 return NL_SKIP;
8765}
8766
8767
8768static int nl80211_get_pref_freq_list(void *priv,
8769 enum wpa_driver_if_type if_type,
8770 unsigned int *num,
8771 unsigned int *freq_list)
8772{
8773 struct i802_bss *bss = priv;
8774 struct wpa_driver_nl80211_data *drv = bss->drv;
8775 struct nl_msg *msg;
8776 int ret;
8777 unsigned int i;
8778 struct nlattr *params;
8779 struct nl80211_pcl param;
8780 enum qca_iface_type iface_type;
8781
8782 if (!drv->get_pref_freq_list)
8783 return -1;
8784
8785 switch (if_type) {
8786 case WPA_IF_STATION:
8787 iface_type = QCA_IFACE_TYPE_STA;
8788 break;
8789 case WPA_IF_AP_BSS:
8790 iface_type = QCA_IFACE_TYPE_AP;
8791 break;
8792 case WPA_IF_P2P_GO:
8793 iface_type = QCA_IFACE_TYPE_P2P_GO;
8794 break;
8795 case WPA_IF_P2P_CLIENT:
8796 iface_type = QCA_IFACE_TYPE_P2P_CLIENT;
8797 break;
8798 case WPA_IF_IBSS:
8799 iface_type = QCA_IFACE_TYPE_IBSS;
8800 break;
8801 case WPA_IF_TDLS:
8802 iface_type = QCA_IFACE_TYPE_TDLS;
8803 break;
8804 default:
8805 return -1;
8806 }
8807
8808 param.num = *num;
8809 param.freq_list = freq_list;
8810
8811 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
8812 nla_put_u32(msg, NL80211_ATTR_IFINDEX, drv->ifindex) ||
8813 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
8814 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
8815 QCA_NL80211_VENDOR_SUBCMD_GET_PREFERRED_FREQ_LIST) ||
8816 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
8817 nla_put_u32(msg,
8818 QCA_WLAN_VENDOR_ATTR_GET_PREFERRED_FREQ_LIST_IFACE_TYPE,
8819 iface_type)) {
8820 wpa_printf(MSG_ERROR,
8821 "%s: err in adding vendor_cmd and vendor_data",
8822 __func__);
8823 nlmsg_free(msg);
8824 return -1;
8825 }
8826 nla_nest_end(msg, params);
8827
8828 os_memset(freq_list, 0, *num * sizeof(freq_list[0]));
8829 ret = send_and_recv_msgs(drv, msg, preferred_freq_info_handler, &param);
8830 if (ret) {
8831 wpa_printf(MSG_ERROR,
8832 "%s: err in send_and_recv_msgs", __func__);
8833 return ret;
8834 }
8835
8836 *num = param.num;
8837
8838 for (i = 0; i < *num; i++) {
8839 wpa_printf(MSG_DEBUG, "nl80211: preferred_channel_list[%d]=%d",
8840 i, freq_list[i]);
8841 }
8842
8843 return 0;
8844}
8845
8846
8847static int nl80211_set_prob_oper_freq(void *priv, unsigned int freq)
8848{
8849 struct i802_bss *bss = priv;
8850 struct wpa_driver_nl80211_data *drv = bss->drv;
8851 struct nl_msg *msg;
8852 int ret;
8853 struct nlattr *params;
8854
8855 if (!drv->set_prob_oper_freq)
8856 return -1;
8857
8858 wpa_printf(MSG_DEBUG,
8859 "nl80211: Set P2P probable operating freq %u for ifindex %d",
8860 freq, bss->ifindex);
8861
8862 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
8863 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
8864 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
8865 QCA_NL80211_VENDOR_SUBCMD_SET_PROBABLE_OPER_CHANNEL) ||
8866 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
8867 nla_put_u32(msg,
8868 QCA_WLAN_VENDOR_ATTR_PROBABLE_OPER_CHANNEL_IFACE_TYPE,
8869 QCA_IFACE_TYPE_P2P_CLIENT) ||
8870 nla_put_u32(msg,
8871 QCA_WLAN_VENDOR_ATTR_PROBABLE_OPER_CHANNEL_FREQ,
8872 freq)) {
8873 wpa_printf(MSG_ERROR,
8874 "%s: err in adding vendor_cmd and vendor_data",
8875 __func__);
8876 nlmsg_free(msg);
8877 return -1;
8878 }
8879 nla_nest_end(msg, params);
8880
8881 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8882 msg = NULL;
8883 if (ret) {
8884 wpa_printf(MSG_ERROR, "%s: err in send_and_recv_msgs",
8885 __func__);
8886 return ret;
8887 }
8888 nlmsg_free(msg);
8889 return 0;
8890}
8891
8892#endif /* CONFIG_DRIVER_NL80211_QCA */
8893
8894
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008895const struct wpa_driver_ops wpa_driver_nl80211_ops = {
8896 .name = "nl80211",
8897 .desc = "Linux nl80211/cfg80211",
8898 .get_bssid = wpa_driver_nl80211_get_bssid,
8899 .get_ssid = wpa_driver_nl80211_get_ssid,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008900 .set_key = driver_nl80211_set_key,
8901 .scan2 = driver_nl80211_scan2,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008902 .sched_scan = wpa_driver_nl80211_sched_scan,
8903 .stop_sched_scan = wpa_driver_nl80211_stop_sched_scan,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008904 .get_scan_results2 = wpa_driver_nl80211_get_scan_results,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008905 .abort_scan = wpa_driver_nl80211_abort_scan,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008906 .deauthenticate = driver_nl80211_deauthenticate,
8907 .authenticate = driver_nl80211_authenticate,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008908 .associate = wpa_driver_nl80211_associate,
8909 .global_init = nl80211_global_init,
8910 .global_deinit = nl80211_global_deinit,
8911 .init2 = wpa_driver_nl80211_init,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008912 .deinit = driver_nl80211_deinit,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008913 .get_capa = wpa_driver_nl80211_get_capa,
8914 .set_operstate = wpa_driver_nl80211_set_operstate,
8915 .set_supp_port = wpa_driver_nl80211_set_supp_port,
8916 .set_country = wpa_driver_nl80211_set_country,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008917 .get_country = wpa_driver_nl80211_get_country,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008918 .set_ap = wpa_driver_nl80211_set_ap,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07008919 .set_acl = wpa_driver_nl80211_set_acl,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008920 .if_add = wpa_driver_nl80211_if_add,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008921 .if_remove = driver_nl80211_if_remove,
8922 .send_mlme = driver_nl80211_send_mlme,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008923 .get_hw_feature_data = nl80211_get_hw_feature_data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008924 .sta_add = wpa_driver_nl80211_sta_add,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008925 .sta_remove = driver_nl80211_sta_remove,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008926 .hapd_send_eapol = wpa_driver_nl80211_hapd_send_eapol,
8927 .sta_set_flags = wpa_driver_nl80211_sta_set_flags,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008928 .hapd_init = i802_init,
8929 .hapd_deinit = i802_deinit,
Jouni Malinen75ecf522011-06-27 15:19:46 -07008930 .set_wds_sta = i802_set_wds_sta,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008931 .get_seqnum = i802_get_seqnum,
8932 .flush = i802_flush,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008933 .get_inact_sec = i802_get_inact_sec,
8934 .sta_clear_stats = i802_sta_clear_stats,
8935 .set_rts = i802_set_rts,
8936 .set_frag = i802_set_frag,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008937 .set_tx_queue_params = i802_set_tx_queue_params,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008938 .set_sta_vlan = driver_nl80211_set_sta_vlan,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008939 .sta_deauth = i802_sta_deauth,
8940 .sta_disassoc = i802_sta_disassoc,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008941 .read_sta_data = driver_nl80211_read_sta_data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008942 .set_freq = i802_set_freq,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008943 .send_action = driver_nl80211_send_action,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008944 .send_action_cancel_wait = wpa_driver_nl80211_send_action_cancel_wait,
8945 .remain_on_channel = wpa_driver_nl80211_remain_on_channel,
8946 .cancel_remain_on_channel =
8947 wpa_driver_nl80211_cancel_remain_on_channel,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008948 .probe_req_report = driver_nl80211_probe_req_report,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008949 .deinit_ap = wpa_driver_nl80211_deinit_ap,
Dmitry Shmidt04949592012-07-19 12:16:46 -07008950 .deinit_p2p_cli = wpa_driver_nl80211_deinit_p2p_cli,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008951 .resume = wpa_driver_nl80211_resume,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008952 .signal_monitor = nl80211_signal_monitor,
8953 .signal_poll = nl80211_signal_poll,
8954 .send_frame = nl80211_send_frame,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008955 .set_param = nl80211_set_param,
8956 .get_radio_name = nl80211_get_radio_name,
Jouni Malinen75ecf522011-06-27 15:19:46 -07008957 .add_pmkid = nl80211_add_pmkid,
8958 .remove_pmkid = nl80211_remove_pmkid,
8959 .flush_pmkid = nl80211_flush_pmkid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008960 .set_rekey_info = nl80211_set_rekey_info,
8961 .poll_client = nl80211_poll_client,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008962 .set_p2p_powersave = nl80211_set_p2p_powersave,
Dmitry Shmidtea69e842013-05-13 14:52:28 -07008963 .start_dfs_cac = nl80211_start_radar_detection,
8964 .stop_ap = wpa_driver_nl80211_stop_ap,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008965#ifdef CONFIG_TDLS
8966 .send_tdls_mgmt = nl80211_send_tdls_mgmt,
8967 .tdls_oper = nl80211_tdls_oper,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008968 .tdls_enable_channel_switch = nl80211_tdls_enable_channel_switch,
8969 .tdls_disable_channel_switch = nl80211_tdls_disable_channel_switch,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008970#endif /* CONFIG_TDLS */
Dmitry Shmidt700a1372013-03-15 14:14:44 -07008971 .update_ft_ies = wpa_driver_nl80211_update_ft_ies,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008972 .get_mac_addr = wpa_driver_nl80211_get_macaddr,
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008973 .get_survey = wpa_driver_nl80211_get_survey,
Dmitry Shmidt56052862013-10-04 10:23:25 -07008974 .status = wpa_driver_nl80211_status,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008975 .switch_channel = nl80211_switch_channel,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008976#ifdef ANDROID_P2P
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07008977 .set_noa = wpa_driver_set_p2p_noa,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008978 .get_noa = wpa_driver_get_p2p_noa,
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07008979 .set_ap_wps_ie = wpa_driver_set_ap_wps_p2p_ie,
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08008980#endif /* ANDROID_P2P */
Dmitry Shmidt738a26e2011-07-07 14:22:14 -07008981#ifdef ANDROID
Dmitry Shmidt41712582015-06-29 11:02:15 -07008982#ifndef ANDROID_LIB_STUB
Dmitry Shmidt738a26e2011-07-07 14:22:14 -07008983 .driver_cmd = wpa_driver_nl80211_driver_cmd,
Dmitry Shmidt41712582015-06-29 11:02:15 -07008984#endif /* !ANDROID_LIB_STUB */
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08008985#endif /* ANDROID */
Dmitry Shmidta38abf92014-03-06 13:38:44 -08008986 .vendor_cmd = nl80211_vendor_cmd,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008987 .set_qos_map = nl80211_set_qos_map,
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07008988 .set_wowlan = nl80211_set_wowlan,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008989#ifdef CONFIG_DRIVER_NL80211_QCA
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008990 .roaming = nl80211_roaming,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008991#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008992 .set_mac_addr = nl80211_set_mac_addr,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008993#ifdef CONFIG_MESH
8994 .init_mesh = wpa_driver_nl80211_init_mesh,
8995 .join_mesh = wpa_driver_nl80211_join_mesh,
8996 .leave_mesh = wpa_driver_nl80211_leave_mesh,
8997#endif /* CONFIG_MESH */
8998 .br_add_ip_neigh = wpa_driver_br_add_ip_neigh,
8999 .br_delete_ip_neigh = wpa_driver_br_delete_ip_neigh,
9000 .br_port_set_attr = wpa_driver_br_port_set_attr,
9001 .br_set_net_param = wpa_driver_br_set_net_param,
9002 .add_tx_ts = nl80211_add_ts,
9003 .del_tx_ts = nl80211_del_ts,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009004#ifdef CONFIG_DRIVER_NL80211_QCA
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009005 .do_acs = wpa_driver_do_acs,
Ravi Joshie6ccb162015-07-16 17:45:41 -07009006 .set_band = nl80211_set_band,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009007 .get_pref_freq_list = nl80211_get_pref_freq_list,
9008 .set_prob_oper_freq = nl80211_set_prob_oper_freq,
9009#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009010};