blob: 3ed98511754a72bd0274541ec7c78658ca3d5ed1 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * Driver interaction with Linux nl80211/cfg80211
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003 * Copyright (c) 2002-2014, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 * Copyright (c) 2003-2004, Instant802 Networks, Inc.
5 * Copyright (c) 2005-2006, Devicescape Software, Inc.
6 * Copyright (c) 2007, Johannes Berg <johannes@sipsolutions.net>
7 * Copyright (c) 2009-2010, Atheros Communications
8 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08009 * This software may be distributed under the terms of the BSD license.
10 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011 */
12
13#include "includes.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014#include <sys/types.h>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070015#include <fcntl.h>
16#include <net/if.h>
17#include <netlink/genl/genl.h>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070018#include <netlink/genl/ctrl.h>
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070019#ifdef CONFIG_LIBNL3_ROUTE
20#include <netlink/route/neighbour.h>
21#endif /* CONFIG_LIBNL3_ROUTE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070022#include <linux/rtnetlink.h>
23#include <netpacket/packet.h>
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080024#include <linux/errqueue.h>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070025
26#include "common.h"
27#include "eloop.h"
Dmitry Shmidtcf32e602014-01-28 10:57:39 -080028#include "common/qca-vendor.h"
Dmitry Shmidt7832adb2014-04-29 10:53:02 -070029#include "common/qca-vendor-attr.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070030#include "common/ieee802_11_defs.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080031#include "common/ieee802_11_common.h"
32#include "l2_packet/l2_packet.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070033#include "netlink.h"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080034#include "linux_defines.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070035#include "linux_ioctl.h"
36#include "radiotap.h"
37#include "radiotap_iter.h"
38#include "rfkill.h"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080039#include "driver_nl80211.h"
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080040
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080041
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080042#ifndef CONFIG_LIBNL20
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070043/*
44 * libnl 1.1 has a bug, it tries to allocate socket numbers densely
45 * but when you free a socket again it will mess up its bitmap and
46 * and use the wrong number the next time it needs a socket ID.
47 * Therefore, we wrap the handle alloc/destroy and add our own pid
48 * accounting.
49 */
50static uint32_t port_bitmap[32] = { 0 };
51
52static struct nl_handle *nl80211_handle_alloc(void *cb)
53{
54 struct nl_handle *handle;
55 uint32_t pid = getpid() & 0x3FFFFF;
56 int i;
57
58 handle = nl_handle_alloc_cb(cb);
59
60 for (i = 0; i < 1024; i++) {
61 if (port_bitmap[i / 32] & (1 << (i % 32)))
62 continue;
63 port_bitmap[i / 32] |= 1 << (i % 32);
64 pid += i << 22;
65 break;
66 }
67
68 nl_socket_set_local_port(handle, pid);
69
70 return handle;
71}
72
73static void nl80211_handle_destroy(struct nl_handle *handle)
74{
75 uint32_t port = nl_socket_get_local_port(handle);
76
77 port >>= 22;
78 port_bitmap[port / 32] &= ~(1 << (port % 32));
79
80 nl_handle_destroy(handle);
81}
82#endif /* CONFIG_LIBNL20 */
83
84
Dmitry Shmidt54605472013-11-08 11:10:19 -080085#ifdef ANDROID
86/* system/core/libnl_2 does not include nl_socket_set_nonblocking() */
Dmitry Shmidt54605472013-11-08 11:10:19 -080087#undef nl_socket_set_nonblocking
88#define nl_socket_set_nonblocking(h) android_nl_socket_set_nonblocking(h)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080089
90#define genl_ctrl_resolve android_genl_ctrl_resolve
Dmitry Shmidt54605472013-11-08 11:10:19 -080091#endif /* ANDROID */
92
93
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080094static struct nl_handle * nl_create_handle(struct nl_cb *cb, const char *dbg)
95{
96 struct nl_handle *handle;
97
98 handle = nl80211_handle_alloc(cb);
99 if (handle == NULL) {
100 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
101 "callbacks (%s)", dbg);
102 return NULL;
103 }
104
105 if (genl_connect(handle)) {
106 wpa_printf(MSG_ERROR, "nl80211: Failed to connect to generic "
107 "netlink (%s)", dbg);
108 nl80211_handle_destroy(handle);
109 return NULL;
110 }
111
112 return handle;
113}
114
115
116static void nl_destroy_handles(struct nl_handle **handle)
117{
118 if (*handle == NULL)
119 return;
120 nl80211_handle_destroy(*handle);
121 *handle = NULL;
122}
123
124
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700125#if __WORDSIZE == 64
126#define ELOOP_SOCKET_INVALID (intptr_t) 0x8888888888888889ULL
127#else
128#define ELOOP_SOCKET_INVALID (intptr_t) 0x88888889ULL
129#endif
130
131static void nl80211_register_eloop_read(struct nl_handle **handle,
132 eloop_sock_handler handler,
133 void *eloop_data)
134{
135 nl_socket_set_nonblocking(*handle);
136 eloop_register_read_sock(nl_socket_get_fd(*handle), handler,
137 eloop_data, *handle);
138 *handle = (void *) (((intptr_t) *handle) ^ ELOOP_SOCKET_INVALID);
139}
140
141
142static void nl80211_destroy_eloop_handle(struct nl_handle **handle)
143{
144 *handle = (void *) (((intptr_t) *handle) ^ ELOOP_SOCKET_INVALID);
145 eloop_unregister_read_sock(nl_socket_get_fd(*handle));
146 nl_destroy_handles(handle);
147}
148
149
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800150static void nl80211_global_deinit(void *priv);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800151
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800152static void wpa_driver_nl80211_deinit(struct i802_bss *bss);
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -0700153static int wpa_driver_nl80211_set_mode_ibss(struct i802_bss *bss,
154 struct hostapd_freq_params *freq);
Dmitry Shmidtd30ac602014-06-30 09:54:22 -0700155
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700156static int
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800157wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800158 const u8 *set_addr, int first,
159 const char *driver_params);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800160static int nl80211_send_frame_cmd(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700161 unsigned int freq, unsigned int wait,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800162 const u8 *buf, size_t buf_len, u64 *cookie,
163 int no_cck, int no_ack, int offchanok);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800164static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss,
165 int report);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700166
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700167static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
168static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
169static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
Dmitry Shmidt738a26e2011-07-07 14:22:14 -0700170
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700171static int nl80211_set_channel(struct i802_bss *bss,
172 struct hostapd_freq_params *freq, int set_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700173static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
174 int ifindex, int disabled);
175
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800176static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv,
177 int reset_mode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800178
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700179static int i802_set_iface_flags(struct i802_bss *bss, int up);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800180static int nl80211_set_param(void *priv, const char *param);
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700181
182
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800183/* Converts nl80211_chan_width to a common format */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800184enum chan_width convert2width(int width)
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800185{
186 switch (width) {
187 case NL80211_CHAN_WIDTH_20_NOHT:
188 return CHAN_WIDTH_20_NOHT;
189 case NL80211_CHAN_WIDTH_20:
190 return CHAN_WIDTH_20;
191 case NL80211_CHAN_WIDTH_40:
192 return CHAN_WIDTH_40;
193 case NL80211_CHAN_WIDTH_80:
194 return CHAN_WIDTH_80;
195 case NL80211_CHAN_WIDTH_80P80:
196 return CHAN_WIDTH_80P80;
197 case NL80211_CHAN_WIDTH_160:
198 return CHAN_WIDTH_160;
199 }
200 return CHAN_WIDTH_UNKNOWN;
201}
202
203
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800204int is_ap_interface(enum nl80211_iftype nlmode)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800205{
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700206 return nlmode == NL80211_IFTYPE_AP ||
207 nlmode == NL80211_IFTYPE_P2P_GO;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800208}
209
210
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800211int is_sta_interface(enum nl80211_iftype nlmode)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800212{
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700213 return nlmode == NL80211_IFTYPE_STATION ||
214 nlmode == NL80211_IFTYPE_P2P_CLIENT;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800215}
216
217
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700218static int is_p2p_net_interface(enum nl80211_iftype nlmode)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800219{
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700220 return nlmode == NL80211_IFTYPE_P2P_CLIENT ||
221 nlmode == NL80211_IFTYPE_P2P_GO;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800222}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700223
224
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800225struct i802_bss * get_bss_ifindex(struct wpa_driver_nl80211_data *drv,
226 int ifindex)
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -0700227{
228 struct i802_bss *bss;
229
230 for (bss = drv->first_bss; bss; bss = bss->next) {
231 if (bss->ifindex == ifindex)
232 return bss;
233 }
234
235 return NULL;
236}
237
238
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800239static int is_mesh_interface(enum nl80211_iftype nlmode)
240{
241 return nlmode == NL80211_IFTYPE_MESH_POINT;
242}
243
244
245void nl80211_mark_disconnected(struct wpa_driver_nl80211_data *drv)
Dmitry Shmidt8bae4132013-06-06 11:25:10 -0700246{
247 if (drv->associated)
248 os_memcpy(drv->prev_bssid, drv->bssid, ETH_ALEN);
249 drv->associated = 0;
250 os_memset(drv->bssid, 0, ETH_ALEN);
251}
252
253
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700254/* nl80211 code */
255static int ack_handler(struct nl_msg *msg, void *arg)
256{
257 int *err = arg;
258 *err = 0;
259 return NL_STOP;
260}
261
262static int finish_handler(struct nl_msg *msg, void *arg)
263{
264 int *ret = arg;
265 *ret = 0;
266 return NL_SKIP;
267}
268
269static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
270 void *arg)
271{
272 int *ret = arg;
273 *ret = err->error;
274 return NL_SKIP;
275}
276
277
278static int no_seq_check(struct nl_msg *msg, void *arg)
279{
280 return NL_OK;
281}
282
283
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800284static void nl80211_nlmsg_clear(struct nl_msg *msg)
285{
286 /*
287 * Clear nlmsg data, e.g., to make sure key material is not left in
288 * heap memory for unnecessarily long time.
289 */
290 if (msg) {
291 struct nlmsghdr *hdr = nlmsg_hdr(msg);
292 void *data = nlmsg_data(hdr);
293 /*
294 * This would use nlmsg_datalen() or the older nlmsg_len() if
295 * only libnl were to maintain a stable API.. Neither will work
296 * with all released versions, so just calculate the length
297 * here.
298 */
299 int len = hdr->nlmsg_len - NLMSG_HDRLEN;
300
301 os_memset(data, 0, len);
302 }
303}
304
305
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800306static int send_and_recv(struct nl80211_global *global,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700307 struct nl_handle *nl_handle, struct nl_msg *msg,
308 int (*valid_handler)(struct nl_msg *, void *),
309 void *valid_data)
310{
311 struct nl_cb *cb;
312 int err = -ENOMEM;
313
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800314 if (!msg)
315 return -ENOMEM;
316
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800317 cb = nl_cb_clone(global->nl_cb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700318 if (!cb)
319 goto out;
320
321 err = nl_send_auto_complete(nl_handle, msg);
322 if (err < 0)
323 goto out;
324
325 err = 1;
326
327 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
328 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
329 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
330
331 if (valid_handler)
332 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM,
333 valid_handler, valid_data);
334
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700335 while (err > 0) {
336 int res = nl_recvmsgs(nl_handle, cb);
Dmitry Shmidt71757432014-06-02 13:50:35 -0700337 if (res < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700338 wpa_printf(MSG_INFO,
339 "nl80211: %s->nl_recvmsgs failed: %d",
340 __func__, res);
341 }
342 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700343 out:
344 nl_cb_put(cb);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800345 if (!valid_handler && valid_data == (void *) -1)
346 nl80211_nlmsg_clear(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700347 nlmsg_free(msg);
348 return err;
349}
350
351
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800352int send_and_recv_msgs(struct wpa_driver_nl80211_data *drv,
353 struct nl_msg *msg,
354 int (*valid_handler)(struct nl_msg *, void *),
355 void *valid_data)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700356{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800357 return send_and_recv(drv->global, drv->global->nl, msg,
358 valid_handler, valid_data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700359}
360
361
362struct family_data {
363 const char *group;
364 int id;
365};
366
367
368static int family_handler(struct nl_msg *msg, void *arg)
369{
370 struct family_data *res = arg;
371 struct nlattr *tb[CTRL_ATTR_MAX + 1];
372 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
373 struct nlattr *mcgrp;
374 int i;
375
376 nla_parse(tb, CTRL_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
377 genlmsg_attrlen(gnlh, 0), NULL);
378 if (!tb[CTRL_ATTR_MCAST_GROUPS])
379 return NL_SKIP;
380
381 nla_for_each_nested(mcgrp, tb[CTRL_ATTR_MCAST_GROUPS], i) {
382 struct nlattr *tb2[CTRL_ATTR_MCAST_GRP_MAX + 1];
383 nla_parse(tb2, CTRL_ATTR_MCAST_GRP_MAX, nla_data(mcgrp),
384 nla_len(mcgrp), NULL);
385 if (!tb2[CTRL_ATTR_MCAST_GRP_NAME] ||
386 !tb2[CTRL_ATTR_MCAST_GRP_ID] ||
387 os_strncmp(nla_data(tb2[CTRL_ATTR_MCAST_GRP_NAME]),
388 res->group,
389 nla_len(tb2[CTRL_ATTR_MCAST_GRP_NAME])) != 0)
390 continue;
391 res->id = nla_get_u32(tb2[CTRL_ATTR_MCAST_GRP_ID]);
392 break;
393 };
394
395 return NL_SKIP;
396}
397
398
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800399static int nl_get_multicast_id(struct nl80211_global *global,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700400 const char *family, const char *group)
401{
402 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800403 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700404 struct family_data res = { group, -ENOENT };
405
406 msg = nlmsg_alloc();
407 if (!msg)
408 return -ENOMEM;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800409 if (!genlmsg_put(msg, 0, 0, genl_ctrl_resolve(global->nl, "nlctrl"),
410 0, 0, CTRL_CMD_GETFAMILY, 0) ||
411 nla_put_string(msg, CTRL_ATTR_FAMILY_NAME, family)) {
412 nlmsg_free(msg);
413 return -1;
414 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700415
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800416 ret = send_and_recv(global, global->nl, msg, family_handler, &res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700417 if (ret == 0)
418 ret = res.id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700419 return ret;
420}
421
422
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800423void * nl80211_cmd(struct wpa_driver_nl80211_data *drv,
424 struct nl_msg *msg, int flags, uint8_t cmd)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800425{
426 return genlmsg_put(msg, 0, 0, drv->global->nl80211_id,
427 0, flags, cmd, 0);
428}
429
430
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800431static int nl80211_set_iface_id(struct nl_msg *msg, struct i802_bss *bss)
432{
433 if (bss->wdev_id_set)
434 return nla_put_u64(msg, NL80211_ATTR_WDEV, bss->wdev_id);
435 return nla_put_u32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
436}
437
438
439struct nl_msg * nl80211_cmd_msg(struct i802_bss *bss, int flags, uint8_t cmd)
440{
441 struct nl_msg *msg;
442
443 msg = nlmsg_alloc();
444 if (!msg)
445 return NULL;
446
447 if (!nl80211_cmd(bss->drv, msg, flags, cmd) ||
448 nl80211_set_iface_id(msg, bss) < 0) {
449 nlmsg_free(msg);
450 return NULL;
451 }
452
453 return msg;
454}
455
456
457static struct nl_msg *
458nl80211_ifindex_msg(struct wpa_driver_nl80211_data *drv, int ifindex,
459 int flags, uint8_t cmd)
460{
461 struct nl_msg *msg;
462
463 msg = nlmsg_alloc();
464 if (!msg)
465 return NULL;
466
467 if (!nl80211_cmd(drv, msg, flags, cmd) ||
468 nla_put_u32(msg, NL80211_ATTR_IFINDEX, ifindex)) {
469 nlmsg_free(msg);
470 return NULL;
471 }
472
473 return msg;
474}
475
476
477struct nl_msg * nl80211_drv_msg(struct wpa_driver_nl80211_data *drv, int flags,
478 uint8_t cmd)
479{
480 return nl80211_ifindex_msg(drv, drv->ifindex, flags, cmd);
481}
482
483
484struct nl_msg * nl80211_bss_msg(struct i802_bss *bss, int flags, uint8_t cmd)
485{
486 return nl80211_ifindex_msg(bss->drv, bss->ifindex, flags, cmd);
487}
488
489
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800490struct wiphy_idx_data {
491 int wiphy_idx;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700492 enum nl80211_iftype nlmode;
493 u8 *macaddr;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800494};
495
496
497static int netdev_info_handler(struct nl_msg *msg, void *arg)
498{
499 struct nlattr *tb[NL80211_ATTR_MAX + 1];
500 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
501 struct wiphy_idx_data *info = arg;
502
503 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
504 genlmsg_attrlen(gnlh, 0), NULL);
505
506 if (tb[NL80211_ATTR_WIPHY])
507 info->wiphy_idx = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
508
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700509 if (tb[NL80211_ATTR_IFTYPE])
510 info->nlmode = nla_get_u32(tb[NL80211_ATTR_IFTYPE]);
511
512 if (tb[NL80211_ATTR_MAC] && info->macaddr)
513 os_memcpy(info->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
514 ETH_ALEN);
515
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800516 return NL_SKIP;
517}
518
519
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800520int nl80211_get_wiphy_index(struct i802_bss *bss)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800521{
522 struct nl_msg *msg;
523 struct wiphy_idx_data data = {
524 .wiphy_idx = -1,
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700525 .macaddr = NULL,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800526 };
527
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800528 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_GET_INTERFACE)))
529 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800530
531 if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0)
532 return data.wiphy_idx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800533 return -1;
534}
535
536
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700537static enum nl80211_iftype nl80211_get_ifmode(struct i802_bss *bss)
538{
539 struct nl_msg *msg;
540 struct wiphy_idx_data data = {
541 .nlmode = NL80211_IFTYPE_UNSPECIFIED,
542 .macaddr = NULL,
543 };
544
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800545 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_GET_INTERFACE)))
546 return NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700547
548 if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0)
549 return data.nlmode;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700550 return NL80211_IFTYPE_UNSPECIFIED;
551}
552
553
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700554static int nl80211_get_macaddr(struct i802_bss *bss)
555{
556 struct nl_msg *msg;
557 struct wiphy_idx_data data = {
558 .macaddr = bss->addr,
559 };
560
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800561 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_GET_INTERFACE)))
562 return -1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700563
564 return send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data);
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700565}
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700566
567
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800568static int nl80211_register_beacons(struct wpa_driver_nl80211_data *drv,
569 struct nl80211_wiphy_data *w)
570{
571 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800572 int ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800573
574 msg = nlmsg_alloc();
575 if (!msg)
576 return -1;
577
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800578 if (!nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_BEACONS) ||
579 nla_put_u32(msg, NL80211_ATTR_WIPHY, w->wiphy_idx)) {
580 nlmsg_free(msg);
581 return -1;
582 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800583
584 ret = send_and_recv(drv->global, w->nl_beacons, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800585 if (ret) {
586 wpa_printf(MSG_DEBUG, "nl80211: Register beacons command "
587 "failed: ret=%d (%s)",
588 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800589 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800590 return ret;
591}
592
593
594static void nl80211_recv_beacons(int sock, void *eloop_ctx, void *handle)
595{
596 struct nl80211_wiphy_data *w = eloop_ctx;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700597 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800598
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800599 wpa_printf(MSG_EXCESSIVE, "nl80211: Beacon event message available");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800600
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700601 res = nl_recvmsgs(handle, w->nl_cb);
Dmitry Shmidt71757432014-06-02 13:50:35 -0700602 if (res < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700603 wpa_printf(MSG_INFO, "nl80211: %s->nl_recvmsgs failed: %d",
604 __func__, res);
605 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800606}
607
608
609static int process_beacon_event(struct nl_msg *msg, void *arg)
610{
611 struct nl80211_wiphy_data *w = arg;
612 struct wpa_driver_nl80211_data *drv;
613 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
614 struct nlattr *tb[NL80211_ATTR_MAX + 1];
615 union wpa_event_data event;
616
617 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
618 genlmsg_attrlen(gnlh, 0), NULL);
619
620 if (gnlh->cmd != NL80211_CMD_FRAME) {
621 wpa_printf(MSG_DEBUG, "nl80211: Unexpected beacon event? (%d)",
622 gnlh->cmd);
623 return NL_SKIP;
624 }
625
626 if (!tb[NL80211_ATTR_FRAME])
627 return NL_SKIP;
628
629 dl_list_for_each(drv, &w->drvs, struct wpa_driver_nl80211_data,
630 wiphy_list) {
631 os_memset(&event, 0, sizeof(event));
632 event.rx_mgmt.frame = nla_data(tb[NL80211_ATTR_FRAME]);
633 event.rx_mgmt.frame_len = nla_len(tb[NL80211_ATTR_FRAME]);
634 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
635 }
636
637 return NL_SKIP;
638}
639
640
641static struct nl80211_wiphy_data *
642nl80211_get_wiphy_data_ap(struct i802_bss *bss)
643{
644 static DEFINE_DL_LIST(nl80211_wiphys);
645 struct nl80211_wiphy_data *w;
646 int wiphy_idx, found = 0;
647 struct i802_bss *tmp_bss;
648
649 if (bss->wiphy_data != NULL)
650 return bss->wiphy_data;
651
652 wiphy_idx = nl80211_get_wiphy_index(bss);
653
654 dl_list_for_each(w, &nl80211_wiphys, struct nl80211_wiphy_data, list) {
655 if (w->wiphy_idx == wiphy_idx)
656 goto add;
657 }
658
659 /* alloc new one */
660 w = os_zalloc(sizeof(*w));
661 if (w == NULL)
662 return NULL;
663 w->wiphy_idx = wiphy_idx;
664 dl_list_init(&w->bsss);
665 dl_list_init(&w->drvs);
666
667 w->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
668 if (!w->nl_cb) {
669 os_free(w);
670 return NULL;
671 }
672 nl_cb_set(w->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL);
673 nl_cb_set(w->nl_cb, NL_CB_VALID, NL_CB_CUSTOM, process_beacon_event,
674 w);
675
676 w->nl_beacons = nl_create_handle(bss->drv->global->nl_cb,
677 "wiphy beacons");
678 if (w->nl_beacons == NULL) {
679 os_free(w);
680 return NULL;
681 }
682
683 if (nl80211_register_beacons(bss->drv, w)) {
684 nl_destroy_handles(&w->nl_beacons);
685 os_free(w);
686 return NULL;
687 }
688
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700689 nl80211_register_eloop_read(&w->nl_beacons, nl80211_recv_beacons, w);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800690
691 dl_list_add(&nl80211_wiphys, &w->list);
692
693add:
694 /* drv entry for this bss already there? */
695 dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
696 if (tmp_bss->drv == bss->drv) {
697 found = 1;
698 break;
699 }
700 }
701 /* if not add it */
702 if (!found)
703 dl_list_add(&w->drvs, &bss->drv->wiphy_list);
704
705 dl_list_add(&w->bsss, &bss->wiphy_list);
706 bss->wiphy_data = w;
707 return w;
708}
709
710
711static void nl80211_put_wiphy_data_ap(struct i802_bss *bss)
712{
713 struct nl80211_wiphy_data *w = bss->wiphy_data;
714 struct i802_bss *tmp_bss;
715 int found = 0;
716
717 if (w == NULL)
718 return;
719 bss->wiphy_data = NULL;
720 dl_list_del(&bss->wiphy_list);
721
722 /* still any for this drv present? */
723 dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
724 if (tmp_bss->drv == bss->drv) {
725 found = 1;
726 break;
727 }
728 }
729 /* if not remove it */
730 if (!found)
731 dl_list_del(&bss->drv->wiphy_list);
732
733 if (!dl_list_empty(&w->bsss))
734 return;
735
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700736 nl80211_destroy_eloop_handle(&w->nl_beacons);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800737
738 nl_cb_put(w->nl_cb);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800739 dl_list_del(&w->list);
740 os_free(w);
741}
742
743
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700744static int wpa_driver_nl80211_get_bssid(void *priv, u8 *bssid)
745{
746 struct i802_bss *bss = priv;
747 struct wpa_driver_nl80211_data *drv = bss->drv;
748 if (!drv->associated)
749 return -1;
750 os_memcpy(bssid, drv->bssid, ETH_ALEN);
751 return 0;
752}
753
754
755static int wpa_driver_nl80211_get_ssid(void *priv, u8 *ssid)
756{
757 struct i802_bss *bss = priv;
758 struct wpa_driver_nl80211_data *drv = bss->drv;
759 if (!drv->associated)
760 return -1;
761 os_memcpy(ssid, drv->ssid, drv->ssid_len);
762 return drv->ssid_len;
763}
764
765
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800766static void wpa_driver_nl80211_event_newlink(
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800767 struct wpa_driver_nl80211_data *drv, const char *ifname)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700768{
769 union wpa_event_data event;
770
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800771 if (os_strcmp(drv->first_bss->ifname, ifname) == 0) {
772 if (if_nametoindex(drv->first_bss->ifname) == 0) {
773 wpa_printf(MSG_DEBUG, "nl80211: Interface %s does not exist - ignore RTM_NEWLINK",
774 drv->first_bss->ifname);
775 return;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700776 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800777 if (!drv->if_removed)
778 return;
779 wpa_printf(MSG_DEBUG, "nl80211: Mark if_removed=0 for %s based on RTM_NEWLINK event",
780 drv->first_bss->ifname);
781 drv->if_removed = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700782 }
783
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800784 os_memset(&event, 0, sizeof(event));
785 os_strlcpy(event.interface_status.ifname, ifname,
786 sizeof(event.interface_status.ifname));
787 event.interface_status.ievent = EVENT_INTERFACE_ADDED;
788 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
789}
790
791
792static void wpa_driver_nl80211_event_dellink(
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800793 struct wpa_driver_nl80211_data *drv, const char *ifname)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800794{
795 union wpa_event_data event;
796
797 if (os_strcmp(drv->first_bss->ifname, ifname) == 0) {
798 if (drv->if_removed) {
799 wpa_printf(MSG_DEBUG, "nl80211: if_removed already set - ignore RTM_DELLINK event for %s",
800 ifname);
801 return;
802 }
803 wpa_printf(MSG_DEBUG, "RTM_DELLINK: Interface '%s' removed - mark if_removed=1",
804 ifname);
805 drv->if_removed = 1;
806 } else {
807 wpa_printf(MSG_DEBUG, "RTM_DELLINK: Interface '%s' removed",
808 ifname);
809 }
810
811 os_memset(&event, 0, sizeof(event));
812 os_strlcpy(event.interface_status.ifname, ifname,
813 sizeof(event.interface_status.ifname));
814 event.interface_status.ievent = EVENT_INTERFACE_REMOVED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700815 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
816}
817
818
819static int wpa_driver_nl80211_own_ifname(struct wpa_driver_nl80211_data *drv,
820 u8 *buf, size_t len)
821{
822 int attrlen, rta_len;
823 struct rtattr *attr;
824
825 attrlen = len;
826 attr = (struct rtattr *) buf;
827
828 rta_len = RTA_ALIGN(sizeof(struct rtattr));
829 while (RTA_OK(attr, attrlen)) {
830 if (attr->rta_type == IFLA_IFNAME) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800831 if (os_strcmp(((char *) attr) + rta_len,
832 drv->first_bss->ifname) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700833 return 1;
834 else
835 break;
836 }
837 attr = RTA_NEXT(attr, attrlen);
838 }
839
840 return 0;
841}
842
843
844static int wpa_driver_nl80211_own_ifindex(struct wpa_driver_nl80211_data *drv,
845 int ifindex, u8 *buf, size_t len)
846{
847 if (drv->ifindex == ifindex)
848 return 1;
849
850 if (drv->if_removed && wpa_driver_nl80211_own_ifname(drv, buf, len)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700851 wpa_printf(MSG_DEBUG, "nl80211: Update ifindex for a removed "
852 "interface");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800853 wpa_driver_nl80211_finish_drv_init(drv, NULL, 0, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700854 return 1;
855 }
856
857 return 0;
858}
859
860
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800861static struct wpa_driver_nl80211_data *
862nl80211_find_drv(struct nl80211_global *global, int idx, u8 *buf, size_t len)
863{
864 struct wpa_driver_nl80211_data *drv;
865 dl_list_for_each(drv, &global->interfaces,
866 struct wpa_driver_nl80211_data, list) {
867 if (wpa_driver_nl80211_own_ifindex(drv, idx, buf, len) ||
868 have_ifidx(drv, idx))
869 return drv;
870 }
871 return NULL;
872}
873
874
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700875static void wpa_driver_nl80211_event_rtm_newlink(void *ctx,
876 struct ifinfomsg *ifi,
877 u8 *buf, size_t len)
878{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800879 struct nl80211_global *global = ctx;
880 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800881 int attrlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700882 struct rtattr *attr;
883 u32 brid = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800884 char namebuf[IFNAMSIZ];
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800885 char ifname[IFNAMSIZ + 1];
886 char extra[100], *pos, *end;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700887
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800888 drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
889 if (!drv) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800890 wpa_printf(MSG_DEBUG, "nl80211: Ignore RTM_NEWLINK event for foreign ifindex %d",
891 ifi->ifi_index);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700892 return;
893 }
894
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800895 extra[0] = '\0';
896 pos = extra;
897 end = pos + sizeof(extra);
898 ifname[0] = '\0';
899
900 attrlen = len;
901 attr = (struct rtattr *) buf;
902 while (RTA_OK(attr, attrlen)) {
903 switch (attr->rta_type) {
904 case IFLA_IFNAME:
905 if (RTA_PAYLOAD(attr) >= IFNAMSIZ)
906 break;
907 os_memcpy(ifname, RTA_DATA(attr), RTA_PAYLOAD(attr));
908 ifname[RTA_PAYLOAD(attr)] = '\0';
909 break;
910 case IFLA_MASTER:
911 brid = nla_get_u32((struct nlattr *) attr);
912 pos += os_snprintf(pos, end - pos, " master=%u", brid);
913 break;
914 case IFLA_WIRELESS:
915 pos += os_snprintf(pos, end - pos, " wext");
916 break;
917 case IFLA_OPERSTATE:
918 pos += os_snprintf(pos, end - pos, " operstate=%u",
919 nla_get_u32((struct nlattr *) attr));
920 break;
921 case IFLA_LINKMODE:
922 pos += os_snprintf(pos, end - pos, " linkmode=%u",
923 nla_get_u32((struct nlattr *) attr));
924 break;
925 }
926 attr = RTA_NEXT(attr, attrlen);
927 }
928 extra[sizeof(extra) - 1] = '\0';
929
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700930 wpa_printf(MSG_DEBUG, "RTM_NEWLINK: ifi_index=%d ifname=%s%s ifi_family=%d ifi_flags=0x%x (%s%s%s%s)",
931 ifi->ifi_index, ifname, extra, ifi->ifi_family,
932 ifi->ifi_flags,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700933 (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
934 (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
935 (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
936 (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
937
938 if (!drv->if_disabled && !(ifi->ifi_flags & IFF_UP)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800939 if (if_indextoname(ifi->ifi_index, namebuf) &&
940 linux_iface_up(drv->global->ioctl_sock,
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800941 drv->first_bss->ifname) > 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800942 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
943 "event since interface %s is up", namebuf);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800944 drv->ignore_if_down_event = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800945 return;
946 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700947 wpa_printf(MSG_DEBUG, "nl80211: Interface down");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800948 if (drv->ignore_if_down_event) {
949 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
950 "event generated by mode change");
951 drv->ignore_if_down_event = 0;
952 } else {
953 drv->if_disabled = 1;
954 wpa_supplicant_event(drv->ctx,
955 EVENT_INTERFACE_DISABLED, NULL);
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800956
957 /*
958 * Try to get drv again, since it may be removed as
959 * part of the EVENT_INTERFACE_DISABLED handling for
960 * dynamic interfaces
961 */
962 drv = nl80211_find_drv(global, ifi->ifi_index,
963 buf, len);
964 if (!drv)
965 return;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800966 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700967 }
968
969 if (drv->if_disabled && (ifi->ifi_flags & IFF_UP)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800970 if (if_indextoname(ifi->ifi_index, namebuf) &&
971 linux_iface_up(drv->global->ioctl_sock,
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800972 drv->first_bss->ifname) == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800973 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
974 "event since interface %s is down",
975 namebuf);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800976 } else if (if_nametoindex(drv->first_bss->ifname) == 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700977 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
978 "event since interface %s does not exist",
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800979 drv->first_bss->ifname);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700980 } else if (drv->if_removed) {
981 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
982 "event since interface %s is marked "
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800983 "removed", drv->first_bss->ifname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800984 } else {
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -0700985 struct i802_bss *bss;
986 u8 addr[ETH_ALEN];
987
988 /* Re-read MAC address as it may have changed */
989 bss = get_bss_ifindex(drv, ifi->ifi_index);
990 if (bss &&
991 linux_get_ifhwaddr(drv->global->ioctl_sock,
992 bss->ifname, addr) < 0) {
993 wpa_printf(MSG_DEBUG,
994 "nl80211: %s: failed to re-read MAC address",
995 bss->ifname);
996 } else if (bss &&
997 os_memcmp(addr, bss->addr, ETH_ALEN) != 0) {
998 wpa_printf(MSG_DEBUG,
999 "nl80211: Own MAC address on ifindex %d (%s) changed from "
1000 MACSTR " to " MACSTR,
1001 ifi->ifi_index, bss->ifname,
1002 MAC2STR(bss->addr),
1003 MAC2STR(addr));
1004 os_memcpy(bss->addr, addr, ETH_ALEN);
1005 }
1006
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001007 wpa_printf(MSG_DEBUG, "nl80211: Interface up");
1008 drv->if_disabled = 0;
1009 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_ENABLED,
1010 NULL);
1011 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001012 }
1013
1014 /*
1015 * Some drivers send the association event before the operup event--in
1016 * this case, lifting operstate in wpa_driver_nl80211_set_operstate()
1017 * fails. This will hit us when wpa_supplicant does not need to do
1018 * IEEE 802.1X authentication
1019 */
1020 if (drv->operstate == 1 &&
1021 (ifi->ifi_flags & (IFF_LOWER_UP | IFF_DORMANT)) == IFF_LOWER_UP &&
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001022 !(ifi->ifi_flags & IFF_RUNNING)) {
1023 wpa_printf(MSG_DEBUG, "nl80211: Set IF_OPER_UP again based on ifi_flags and expected operstate");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001024 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001025 -1, IF_OPER_UP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001026 }
1027
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001028 if (ifname[0])
1029 wpa_driver_nl80211_event_newlink(drv, ifname);
1030
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001031 if (ifi->ifi_family == AF_BRIDGE && brid) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001032 struct i802_bss *bss;
1033
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001034 /* device has been added to bridge */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001035 if (!if_indextoname(brid, namebuf)) {
1036 wpa_printf(MSG_DEBUG,
1037 "nl80211: Could not find bridge ifname for ifindex %u",
1038 brid);
1039 return;
1040 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001041 wpa_printf(MSG_DEBUG, "nl80211: Add ifindex %u for bridge %s",
1042 brid, namebuf);
1043 add_ifidx(drv, brid);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001044
1045 for (bss = drv->first_bss; bss; bss = bss->next) {
1046 if (os_strcmp(ifname, bss->ifname) == 0) {
1047 os_strlcpy(bss->brname, namebuf, IFNAMSIZ);
1048 break;
1049 }
1050 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001051 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001052}
1053
1054
1055static void wpa_driver_nl80211_event_rtm_dellink(void *ctx,
1056 struct ifinfomsg *ifi,
1057 u8 *buf, size_t len)
1058{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001059 struct nl80211_global *global = ctx;
1060 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001061 int attrlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001062 struct rtattr *attr;
1063 u32 brid = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001064 char ifname[IFNAMSIZ + 1];
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001065 char extra[100], *pos, *end;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001066
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001067 drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
1068 if (!drv) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001069 wpa_printf(MSG_DEBUG, "nl80211: Ignore RTM_DELLINK event for foreign ifindex %d",
1070 ifi->ifi_index);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001071 return;
1072 }
1073
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001074 extra[0] = '\0';
1075 pos = extra;
1076 end = pos + sizeof(extra);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001077 ifname[0] = '\0';
1078
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001079 attrlen = len;
1080 attr = (struct rtattr *) buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001081 while (RTA_OK(attr, attrlen)) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001082 switch (attr->rta_type) {
1083 case IFLA_IFNAME:
1084 if (RTA_PAYLOAD(attr) >= IFNAMSIZ)
1085 break;
1086 os_memcpy(ifname, RTA_DATA(attr), RTA_PAYLOAD(attr));
1087 ifname[RTA_PAYLOAD(attr)] = '\0';
1088 break;
1089 case IFLA_MASTER:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001090 brid = nla_get_u32((struct nlattr *) attr);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001091 pos += os_snprintf(pos, end - pos, " master=%u", brid);
1092 break;
1093 case IFLA_OPERSTATE:
1094 pos += os_snprintf(pos, end - pos, " operstate=%u",
1095 nla_get_u32((struct nlattr *) attr));
1096 break;
1097 case IFLA_LINKMODE:
1098 pos += os_snprintf(pos, end - pos, " linkmode=%u",
1099 nla_get_u32((struct nlattr *) attr));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001100 break;
1101 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001102 attr = RTA_NEXT(attr, attrlen);
1103 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001104 extra[sizeof(extra) - 1] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001105
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001106 wpa_printf(MSG_DEBUG, "RTM_DELLINK: ifi_index=%d ifname=%s%s ifi_family=%d ifi_flags=0x%x (%s%s%s%s)",
1107 ifi->ifi_index, ifname, extra, ifi->ifi_family,
1108 ifi->ifi_flags,
1109 (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
1110 (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
1111 (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
1112 (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
1113
1114 if (ifname[0] && (ifi->ifi_family != AF_BRIDGE || !brid))
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001115 wpa_driver_nl80211_event_dellink(drv, ifname);
1116
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001117 if (ifi->ifi_family == AF_BRIDGE && brid) {
1118 /* device has been removed from bridge */
1119 char namebuf[IFNAMSIZ];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001120
1121 if (!if_indextoname(brid, namebuf)) {
1122 wpa_printf(MSG_DEBUG,
1123 "nl80211: Could not find bridge ifname for ifindex %u",
1124 brid);
1125 } else {
1126 wpa_printf(MSG_DEBUG,
1127 "nl80211: Remove ifindex %u for bridge %s",
1128 brid, namebuf);
1129 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001130 del_ifidx(drv, brid);
1131 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001132}
1133
1134
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001135unsigned int nl80211_get_assoc_freq(struct wpa_driver_nl80211_data *drv)
Jouni Malinen87fd2792011-05-16 18:35:42 +03001136{
1137 struct nl_msg *msg;
1138 int ret;
1139 struct nl80211_bss_info_arg arg;
1140
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001141 msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
Jouni Malinen87fd2792011-05-16 18:35:42 +03001142 os_memset(&arg, 0, sizeof(arg));
Jouni Malinen87fd2792011-05-16 18:35:42 +03001143 arg.drv = drv;
1144 ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
Jouni Malinen87fd2792011-05-16 18:35:42 +03001145 if (ret == 0) {
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07001146 unsigned int freq = drv->nlmode == NL80211_IFTYPE_ADHOC ?
1147 arg.ibss_freq : arg.assoc_freq;
Jouni Malinen87fd2792011-05-16 18:35:42 +03001148 wpa_printf(MSG_DEBUG, "nl80211: Operating frequency for the "
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07001149 "associated BSS from scan results: %u MHz", freq);
1150 if (freq)
1151 drv->assoc_freq = freq;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001152 return drv->assoc_freq;
Jouni Malinen87fd2792011-05-16 18:35:42 +03001153 }
1154 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
1155 "(%s)", ret, strerror(-ret));
Jouni Malinen87fd2792011-05-16 18:35:42 +03001156 return drv->assoc_freq;
1157}
1158
1159
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001160static int get_link_signal(struct nl_msg *msg, void *arg)
1161{
1162 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1163 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1164 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
1165 static struct nla_policy policy[NL80211_STA_INFO_MAX + 1] = {
1166 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001167 [NL80211_STA_INFO_SIGNAL_AVG] = { .type = NLA_U8 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001168 };
1169 struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
1170 static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
1171 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
1172 [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
1173 [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
1174 [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
1175 };
1176 struct wpa_signal_info *sig_change = arg;
1177
1178 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1179 genlmsg_attrlen(gnlh, 0), NULL);
1180 if (!tb[NL80211_ATTR_STA_INFO] ||
1181 nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
1182 tb[NL80211_ATTR_STA_INFO], policy))
1183 return NL_SKIP;
1184 if (!sinfo[NL80211_STA_INFO_SIGNAL])
1185 return NL_SKIP;
1186
1187 sig_change->current_signal =
1188 (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
1189
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001190 if (sinfo[NL80211_STA_INFO_SIGNAL_AVG])
1191 sig_change->avg_signal =
1192 (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL_AVG]);
1193 else
1194 sig_change->avg_signal = 0;
1195
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001196 if (sinfo[NL80211_STA_INFO_TX_BITRATE]) {
1197 if (nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
1198 sinfo[NL80211_STA_INFO_TX_BITRATE],
1199 rate_policy)) {
1200 sig_change->current_txrate = 0;
1201 } else {
1202 if (rinfo[NL80211_RATE_INFO_BITRATE]) {
1203 sig_change->current_txrate =
1204 nla_get_u16(rinfo[
1205 NL80211_RATE_INFO_BITRATE]) * 100;
1206 }
1207 }
1208 }
1209
1210 return NL_SKIP;
1211}
1212
1213
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001214int nl80211_get_link_signal(struct wpa_driver_nl80211_data *drv,
1215 struct wpa_signal_info *sig)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001216{
1217 struct nl_msg *msg;
1218
1219 sig->current_signal = -9999;
1220 sig->current_txrate = 0;
1221
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001222 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_GET_STATION)) ||
1223 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid)) {
1224 nlmsg_free(msg);
1225 return -ENOBUFS;
1226 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001227
1228 return send_and_recv_msgs(drv, msg, get_link_signal, sig);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001229}
1230
1231
1232static int get_link_noise(struct nl_msg *msg, void *arg)
1233{
1234 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1235 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1236 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
1237 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
1238 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
1239 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
1240 };
1241 struct wpa_signal_info *sig_change = arg;
1242
1243 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1244 genlmsg_attrlen(gnlh, 0), NULL);
1245
1246 if (!tb[NL80211_ATTR_SURVEY_INFO]) {
1247 wpa_printf(MSG_DEBUG, "nl80211: survey data missing!");
1248 return NL_SKIP;
1249 }
1250
1251 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
1252 tb[NL80211_ATTR_SURVEY_INFO],
1253 survey_policy)) {
1254 wpa_printf(MSG_DEBUG, "nl80211: failed to parse nested "
1255 "attributes!");
1256 return NL_SKIP;
1257 }
1258
1259 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
1260 return NL_SKIP;
1261
1262 if (nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
1263 sig_change->frequency)
1264 return NL_SKIP;
1265
1266 if (!sinfo[NL80211_SURVEY_INFO_NOISE])
1267 return NL_SKIP;
1268
1269 sig_change->current_noise =
1270 (s8) nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
1271
1272 return NL_SKIP;
1273}
1274
1275
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001276int nl80211_get_link_noise(struct wpa_driver_nl80211_data *drv,
1277 struct wpa_signal_info *sig_change)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001278{
1279 struct nl_msg *msg;
1280
1281 sig_change->current_noise = 9999;
1282 sig_change->frequency = drv->assoc_freq;
1283
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001284 msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001285 return send_and_recv_msgs(drv, msg, get_link_noise, sig_change);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001286}
1287
1288
1289static void wpa_driver_nl80211_event_receive(int sock, void *eloop_ctx,
1290 void *handle)
1291{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001292 struct nl_cb *cb = eloop_ctx;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001293 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001294
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001295 wpa_printf(MSG_MSGDUMP, "nl80211: Event message available");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001296
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001297 res = nl_recvmsgs(handle, cb);
Dmitry Shmidt71757432014-06-02 13:50:35 -07001298 if (res < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001299 wpa_printf(MSG_INFO, "nl80211: %s->nl_recvmsgs failed: %d",
1300 __func__, res);
1301 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001302}
1303
1304
1305/**
1306 * wpa_driver_nl80211_set_country - ask nl80211 to set the regulatory domain
1307 * @priv: driver_nl80211 private data
1308 * @alpha2_arg: country to which to switch to
1309 * Returns: 0 on success, -1 on failure
1310 *
1311 * This asks nl80211 to set the regulatory domain for given
1312 * country ISO / IEC alpha2.
1313 */
1314static int wpa_driver_nl80211_set_country(void *priv, const char *alpha2_arg)
1315{
1316 struct i802_bss *bss = priv;
1317 struct wpa_driver_nl80211_data *drv = bss->drv;
1318 char alpha2[3];
1319 struct nl_msg *msg;
1320
1321 msg = nlmsg_alloc();
1322 if (!msg)
1323 return -ENOMEM;
1324
1325 alpha2[0] = alpha2_arg[0];
1326 alpha2[1] = alpha2_arg[1];
1327 alpha2[2] = '\0';
1328
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001329 if (!nl80211_cmd(drv, msg, 0, NL80211_CMD_REQ_SET_REG) ||
1330 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, alpha2)) {
1331 nlmsg_free(msg);
1332 return -EINVAL;
1333 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001334 if (send_and_recv_msgs(drv, msg, NULL, NULL))
1335 return -EINVAL;
1336 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001337}
1338
1339
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001340static int nl80211_get_country(struct nl_msg *msg, void *arg)
1341{
1342 char *alpha2 = arg;
1343 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
1344 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1345
1346 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1347 genlmsg_attrlen(gnlh, 0), NULL);
1348 if (!tb_msg[NL80211_ATTR_REG_ALPHA2]) {
1349 wpa_printf(MSG_DEBUG, "nl80211: No country information available");
1350 return NL_SKIP;
1351 }
1352 os_strlcpy(alpha2, nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]), 3);
1353 return NL_SKIP;
1354}
1355
1356
1357static int wpa_driver_nl80211_get_country(void *priv, char *alpha2)
1358{
1359 struct i802_bss *bss = priv;
1360 struct wpa_driver_nl80211_data *drv = bss->drv;
1361 struct nl_msg *msg;
1362 int ret;
1363
1364 msg = nlmsg_alloc();
1365 if (!msg)
1366 return -ENOMEM;
1367
1368 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG);
1369 alpha2[0] = '\0';
1370 ret = send_and_recv_msgs(drv, msg, nl80211_get_country, alpha2);
1371 if (!alpha2[0])
1372 ret = -1;
1373
1374 return ret;
1375}
1376
1377
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001378static int wpa_driver_nl80211_init_nl_global(struct nl80211_global *global)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001379{
1380 int ret;
1381
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001382 global->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
1383 if (global->nl_cb == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001384 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
1385 "callbacks");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001386 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001387 }
1388
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001389 global->nl = nl_create_handle(global->nl_cb, "nl");
1390 if (global->nl == NULL)
1391 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001392
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001393 global->nl80211_id = genl_ctrl_resolve(global->nl, "nl80211");
1394 if (global->nl80211_id < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001395 wpa_printf(MSG_ERROR, "nl80211: 'nl80211' generic netlink not "
1396 "found");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001397 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001398 }
1399
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001400 global->nl_event = nl_create_handle(global->nl_cb, "event");
1401 if (global->nl_event == NULL)
1402 goto err;
1403
1404 ret = nl_get_multicast_id(global, "nl80211", "scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001405 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001406 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001407 if (ret < 0) {
1408 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
1409 "membership for scan events: %d (%s)",
1410 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001411 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001412 }
1413
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001414 ret = nl_get_multicast_id(global, "nl80211", "mlme");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001415 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001416 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001417 if (ret < 0) {
1418 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
1419 "membership for mlme events: %d (%s)",
1420 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001421 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001422 }
1423
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001424 ret = nl_get_multicast_id(global, "nl80211", "regulatory");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001425 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001426 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001427 if (ret < 0) {
1428 wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
1429 "membership for regulatory events: %d (%s)",
1430 ret, strerror(-ret));
1431 /* Continue without regulatory events */
1432 }
1433
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001434 ret = nl_get_multicast_id(global, "nl80211", "vendor");
1435 if (ret >= 0)
1436 ret = nl_socket_add_membership(global->nl_event, ret);
1437 if (ret < 0) {
1438 wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
1439 "membership for vendor events: %d (%s)",
1440 ret, strerror(-ret));
1441 /* Continue without vendor events */
1442 }
1443
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001444 nl_cb_set(global->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
1445 no_seq_check, NULL);
1446 nl_cb_set(global->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
1447 process_global_event, global);
1448
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001449 nl80211_register_eloop_read(&global->nl_event,
1450 wpa_driver_nl80211_event_receive,
1451 global->nl_cb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001452
1453 return 0;
1454
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001455err:
1456 nl_destroy_handles(&global->nl_event);
1457 nl_destroy_handles(&global->nl);
1458 nl_cb_put(global->nl_cb);
1459 global->nl_cb = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001460 return -1;
1461}
1462
1463
1464static void wpa_driver_nl80211_rfkill_blocked(void *ctx)
1465{
1466 wpa_printf(MSG_DEBUG, "nl80211: RFKILL blocked");
1467 /*
1468 * This may be for any interface; use ifdown event to disable
1469 * interface.
1470 */
1471}
1472
1473
1474static void wpa_driver_nl80211_rfkill_unblocked(void *ctx)
1475{
1476 struct wpa_driver_nl80211_data *drv = ctx;
1477 wpa_printf(MSG_DEBUG, "nl80211: RFKILL unblocked");
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001478 if (i802_set_iface_flags(drv->first_bss, 1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001479 wpa_printf(MSG_DEBUG, "nl80211: Could not set interface UP "
1480 "after rfkill unblock");
1481 return;
1482 }
1483 /* rtnetlink ifup handler will report interface as enabled */
1484}
1485
1486
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001487static void wpa_driver_nl80211_handle_eapol_tx_status(int sock,
1488 void *eloop_ctx,
1489 void *handle)
1490{
1491 struct wpa_driver_nl80211_data *drv = eloop_ctx;
1492 u8 data[2048];
1493 struct msghdr msg;
1494 struct iovec entry;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001495 u8 control[512];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001496 struct cmsghdr *cmsg;
1497 int res, found_ee = 0, found_wifi = 0, acked = 0;
1498 union wpa_event_data event;
1499
1500 memset(&msg, 0, sizeof(msg));
1501 msg.msg_iov = &entry;
1502 msg.msg_iovlen = 1;
1503 entry.iov_base = data;
1504 entry.iov_len = sizeof(data);
1505 msg.msg_control = &control;
1506 msg.msg_controllen = sizeof(control);
1507
1508 res = recvmsg(sock, &msg, MSG_ERRQUEUE);
1509 /* if error or not fitting 802.3 header, return */
1510 if (res < 14)
1511 return;
1512
1513 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg))
1514 {
1515 if (cmsg->cmsg_level == SOL_SOCKET &&
1516 cmsg->cmsg_type == SCM_WIFI_STATUS) {
1517 int *ack;
1518
1519 found_wifi = 1;
1520 ack = (void *)CMSG_DATA(cmsg);
1521 acked = *ack;
1522 }
1523
1524 if (cmsg->cmsg_level == SOL_PACKET &&
1525 cmsg->cmsg_type == PACKET_TX_TIMESTAMP) {
1526 struct sock_extended_err *err =
1527 (struct sock_extended_err *)CMSG_DATA(cmsg);
1528
1529 if (err->ee_origin == SO_EE_ORIGIN_TXSTATUS)
1530 found_ee = 1;
1531 }
1532 }
1533
1534 if (!found_ee || !found_wifi)
1535 return;
1536
1537 memset(&event, 0, sizeof(event));
1538 event.eapol_tx_status.dst = data;
1539 event.eapol_tx_status.data = data + 14;
1540 event.eapol_tx_status.data_len = res - 14;
1541 event.eapol_tx_status.ack = acked;
1542 wpa_supplicant_event(drv->ctx, EVENT_EAPOL_TX_STATUS, &event);
1543}
1544
1545
1546static int nl80211_init_bss(struct i802_bss *bss)
1547{
1548 bss->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
1549 if (!bss->nl_cb)
1550 return -1;
1551
1552 nl_cb_set(bss->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
1553 no_seq_check, NULL);
1554 nl_cb_set(bss->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
1555 process_bss_event, bss);
1556
1557 return 0;
1558}
1559
1560
1561static void nl80211_destroy_bss(struct i802_bss *bss)
1562{
1563 nl_cb_put(bss->nl_cb);
1564 bss->nl_cb = NULL;
1565}
1566
1567
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001568static void * wpa_driver_nl80211_drv_init(void *ctx, const char *ifname,
1569 void *global_priv, int hostapd,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001570 const u8 *set_addr,
1571 const char *driver_params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001572{
1573 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001574 struct rfkill_config *rcfg;
1575 struct i802_bss *bss;
1576
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001577 if (global_priv == NULL)
1578 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001579 drv = os_zalloc(sizeof(*drv));
1580 if (drv == NULL)
1581 return NULL;
1582 drv->global = global_priv;
1583 drv->ctx = ctx;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001584 drv->hostapd = !!hostapd;
1585 drv->eapol_sock = -1;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001586
1587 /*
1588 * There is no driver capability flag for this, so assume it is
1589 * supported and disable this on first attempt to use if the driver
1590 * rejects the command due to missing support.
1591 */
1592 drv->set_rekey_offload = 1;
1593
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001594 drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int);
1595 drv->if_indices = drv->default_if_indices;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001596
1597 drv->first_bss = os_zalloc(sizeof(*drv->first_bss));
1598 if (!drv->first_bss) {
1599 os_free(drv);
1600 return NULL;
1601 }
1602 bss = drv->first_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001603 bss->drv = drv;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001604 bss->ctx = ctx;
1605
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001606 os_strlcpy(bss->ifname, ifname, sizeof(bss->ifname));
1607 drv->monitor_ifidx = -1;
1608 drv->monitor_sock = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001609 drv->eapol_tx_sock = -1;
1610 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001611
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001612 if (nl80211_init_bss(bss))
1613 goto failed;
1614
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001615 rcfg = os_zalloc(sizeof(*rcfg));
1616 if (rcfg == NULL)
1617 goto failed;
1618 rcfg->ctx = drv;
1619 os_strlcpy(rcfg->ifname, ifname, sizeof(rcfg->ifname));
1620 rcfg->blocked_cb = wpa_driver_nl80211_rfkill_blocked;
1621 rcfg->unblocked_cb = wpa_driver_nl80211_rfkill_unblocked;
1622 drv->rfkill = rfkill_init(rcfg);
1623 if (drv->rfkill == NULL) {
1624 wpa_printf(MSG_DEBUG, "nl80211: RFKILL status not available");
1625 os_free(rcfg);
1626 }
1627
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001628 if (linux_iface_up(drv->global->ioctl_sock, ifname) > 0)
1629 drv->start_iface_up = 1;
1630
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001631 if (wpa_driver_nl80211_finish_drv_init(drv, set_addr, 1, driver_params))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001632 goto failed;
1633
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001634 drv->eapol_tx_sock = socket(PF_PACKET, SOCK_DGRAM, 0);
1635 if (drv->eapol_tx_sock < 0)
1636 goto failed;
1637
1638 if (drv->data_tx_status) {
1639 int enabled = 1;
1640
1641 if (setsockopt(drv->eapol_tx_sock, SOL_SOCKET, SO_WIFI_STATUS,
1642 &enabled, sizeof(enabled)) < 0) {
1643 wpa_printf(MSG_DEBUG,
1644 "nl80211: wifi status sockopt failed\n");
1645 drv->data_tx_status = 0;
1646 if (!drv->use_monitor)
1647 drv->capa.flags &=
1648 ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
1649 } else {
1650 eloop_register_read_sock(drv->eapol_tx_sock,
1651 wpa_driver_nl80211_handle_eapol_tx_status,
1652 drv, NULL);
1653 }
1654 }
1655
1656 if (drv->global) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001657 dl_list_add(&drv->global->interfaces, &drv->list);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001658 drv->in_interface_list = 1;
1659 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001660
1661 return bss;
1662
1663failed:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001664 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001665 return NULL;
1666}
1667
1668
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001669/**
1670 * wpa_driver_nl80211_init - Initialize nl80211 driver interface
1671 * @ctx: context to be used when calling wpa_supplicant functions,
1672 * e.g., wpa_supplicant_event()
1673 * @ifname: interface name, e.g., wlan0
1674 * @global_priv: private driver global data from global_init()
1675 * Returns: Pointer to private data, %NULL on failure
1676 */
1677static void * wpa_driver_nl80211_init(void *ctx, const char *ifname,
1678 void *global_priv)
1679{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001680 return wpa_driver_nl80211_drv_init(ctx, ifname, global_priv, 0, NULL,
1681 NULL);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001682}
1683
1684
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001685static int nl80211_register_frame(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001686 struct nl_handle *nl_handle,
1687 u16 type, const u8 *match, size_t match_len)
1688{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001689 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001690 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001691 int ret;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001692 char buf[30];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001693
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001694 buf[0] = '\0';
1695 wpa_snprintf_hex(buf, sizeof(buf), match, match_len);
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07001696 wpa_printf(MSG_DEBUG, "nl80211: Register frame type=0x%x (%s) nl_handle=%p match=%s",
1697 type, fc2str(type), nl_handle, buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001698
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001699 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_REGISTER_ACTION)) ||
1700 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE, type) ||
1701 nla_put(msg, NL80211_ATTR_FRAME_MATCH, match_len, match)) {
1702 nlmsg_free(msg);
1703 return -1;
1704 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001705
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001706 ret = send_and_recv(drv->global, nl_handle, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001707 if (ret) {
1708 wpa_printf(MSG_DEBUG, "nl80211: Register frame command "
1709 "failed (type=%u): ret=%d (%s)",
1710 type, ret, strerror(-ret));
1711 wpa_hexdump(MSG_DEBUG, "nl80211: Register frame match",
1712 match, match_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001713 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001714 return ret;
1715}
1716
1717
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001718static int nl80211_alloc_mgmt_handle(struct i802_bss *bss)
1719{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001720 if (bss->nl_mgmt) {
1721 wpa_printf(MSG_DEBUG, "nl80211: Mgmt reporting "
1722 "already on! (nl_mgmt=%p)", bss->nl_mgmt);
1723 return -1;
1724 }
1725
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001726 bss->nl_mgmt = nl_create_handle(bss->nl_cb, "mgmt");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001727 if (bss->nl_mgmt == NULL)
1728 return -1;
1729
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001730 return 0;
1731}
1732
1733
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001734static void nl80211_mgmt_handle_register_eloop(struct i802_bss *bss)
1735{
1736 nl80211_register_eloop_read(&bss->nl_mgmt,
1737 wpa_driver_nl80211_event_receive,
1738 bss->nl_cb);
1739}
1740
1741
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001742static int nl80211_register_action_frame(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001743 const u8 *match, size_t match_len)
1744{
1745 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_ACTION << 4);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001746 return nl80211_register_frame(bss, bss->nl_mgmt,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001747 type, match, match_len);
1748}
1749
1750
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001751static int nl80211_mgmt_subscribe_non_ap(struct i802_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001752{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001753 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001754 int ret = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001755
1756 if (nl80211_alloc_mgmt_handle(bss))
1757 return -1;
1758 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with non-AP "
1759 "handle %p", bss->nl_mgmt);
1760
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001761 if (drv->nlmode == NL80211_IFTYPE_ADHOC) {
1762 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_AUTH << 4);
1763
1764 /* register for any AUTH message */
1765 nl80211_register_frame(bss, bss->nl_mgmt, type, NULL, 0);
1766 }
1767
Dmitry Shmidt051af732013-10-22 13:52:46 -07001768#ifdef CONFIG_INTERWORKING
1769 /* QoS Map Configure */
1770 if (nl80211_register_action_frame(bss, (u8 *) "\x01\x04", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001771 ret = -1;
Dmitry Shmidt051af732013-10-22 13:52:46 -07001772#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001773#if defined(CONFIG_P2P) || defined(CONFIG_INTERWORKING)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001774 /* GAS Initial Request */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001775 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0a", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001776 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001777 /* GAS Initial Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001778 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0b", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001779 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001780 /* GAS Comeback Request */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001781 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0c", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001782 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001783 /* GAS Comeback Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001784 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0d", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001785 ret = -1;
Dmitry Shmidt18463232014-01-24 12:29:41 -08001786 /* Protected GAS Initial Request */
1787 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0a", 2) < 0)
1788 ret = -1;
1789 /* Protected GAS Initial Response */
1790 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0b", 2) < 0)
1791 ret = -1;
1792 /* Protected GAS Comeback Request */
1793 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0c", 2) < 0)
1794 ret = -1;
1795 /* Protected GAS Comeback Response */
1796 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0d", 2) < 0)
1797 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001798#endif /* CONFIG_P2P || CONFIG_INTERWORKING */
1799#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001800 /* P2P Public Action */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001801 if (nl80211_register_action_frame(bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001802 (u8 *) "\x04\x09\x50\x6f\x9a\x09",
1803 6) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001804 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001805 /* P2P Action */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001806 if (nl80211_register_action_frame(bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001807 (u8 *) "\x7f\x50\x6f\x9a\x09",
1808 5) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001809 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001810#endif /* CONFIG_P2P */
1811#ifdef CONFIG_IEEE80211W
1812 /* SA Query Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001813 if (nl80211_register_action_frame(bss, (u8 *) "\x08\x01", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001814 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001815#endif /* CONFIG_IEEE80211W */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001816#ifdef CONFIG_TDLS
1817 if ((drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT)) {
1818 /* TDLS Discovery Response */
1819 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0e", 2) <
1820 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001821 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001822 }
1823#endif /* CONFIG_TDLS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001824
1825 /* FT Action frames */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001826 if (nl80211_register_action_frame(bss, (u8 *) "\x06", 1) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001827 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001828 else
1829 drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_FT |
1830 WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK;
1831
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001832 /* WNM - BSS Transition Management Request */
1833 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x07", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001834 ret = -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001835 /* WNM-Sleep Mode Response */
1836 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x11", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001837 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001838
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001839#ifdef CONFIG_HS20
1840 /* WNM-Notification */
1841 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x1a", 2) < 0)
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001842 ret = -1;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001843#endif /* CONFIG_HS20 */
1844
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001845 /* WMM-AC ADDTS Response */
1846 if (nl80211_register_action_frame(bss, (u8 *) "\x11\x01", 2) < 0)
1847 ret = -1;
1848
1849 /* WMM-AC DELTS */
1850 if (nl80211_register_action_frame(bss, (u8 *) "\x11\x02", 2) < 0)
1851 ret = -1;
1852
1853 /* Radio Measurement - Neighbor Report Response */
1854 if (nl80211_register_action_frame(bss, (u8 *) "\x05\x05", 2) < 0)
1855 ret = -1;
1856
1857 /* Radio Measurement - Link Measurement Request */
1858 if ((drv->capa.rrm_flags & WPA_DRIVER_FLAGS_TX_POWER_INSERTION) &&
1859 (nl80211_register_action_frame(bss, (u8 *) "\x05\x02", 2) < 0))
1860 ret = -1;
1861
1862 nl80211_mgmt_handle_register_eloop(bss);
1863
1864 return ret;
1865}
1866
1867
1868static int nl80211_mgmt_subscribe_mesh(struct i802_bss *bss)
1869{
1870 int ret = 0;
1871
1872 if (nl80211_alloc_mgmt_handle(bss))
1873 return -1;
1874
1875 wpa_printf(MSG_DEBUG,
1876 "nl80211: Subscribe to mgmt frames with mesh handle %p",
1877 bss->nl_mgmt);
1878
1879 /* Auth frames for mesh SAE */
1880 if (nl80211_register_frame(bss, bss->nl_mgmt,
1881 (WLAN_FC_TYPE_MGMT << 2) |
1882 (WLAN_FC_STYPE_AUTH << 4),
1883 NULL, 0) < 0)
1884 ret = -1;
1885
1886 /* Mesh peering open */
1887 if (nl80211_register_action_frame(bss, (u8 *) "\x0f\x01", 2) < 0)
1888 ret = -1;
1889 /* Mesh peering confirm */
1890 if (nl80211_register_action_frame(bss, (u8 *) "\x0f\x02", 2) < 0)
1891 ret = -1;
1892 /* Mesh peering close */
1893 if (nl80211_register_action_frame(bss, (u8 *) "\x0f\x03", 2) < 0)
1894 ret = -1;
1895
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001896 nl80211_mgmt_handle_register_eloop(bss);
1897
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001898 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001899}
1900
1901
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001902static int nl80211_register_spurious_class3(struct i802_bss *bss)
1903{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001904 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001905 int ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001906
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001907 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_UNEXPECTED_FRAME);
1908 ret = send_and_recv(bss->drv->global, bss->nl_mgmt, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001909 if (ret) {
1910 wpa_printf(MSG_DEBUG, "nl80211: Register spurious class3 "
1911 "failed: ret=%d (%s)",
1912 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001913 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001914 return ret;
1915}
1916
1917
1918static int nl80211_mgmt_subscribe_ap(struct i802_bss *bss)
1919{
1920 static const int stypes[] = {
1921 WLAN_FC_STYPE_AUTH,
1922 WLAN_FC_STYPE_ASSOC_REQ,
1923 WLAN_FC_STYPE_REASSOC_REQ,
1924 WLAN_FC_STYPE_DISASSOC,
1925 WLAN_FC_STYPE_DEAUTH,
1926 WLAN_FC_STYPE_ACTION,
1927 WLAN_FC_STYPE_PROBE_REQ,
1928/* Beacon doesn't work as mac80211 doesn't currently allow
1929 * it, but it wouldn't really be the right thing anyway as
1930 * it isn't per interface ... maybe just dump the scan
1931 * results periodically for OLBC?
1932 */
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07001933 /* WLAN_FC_STYPE_BEACON, */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001934 };
1935 unsigned int i;
1936
1937 if (nl80211_alloc_mgmt_handle(bss))
1938 return -1;
1939 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
1940 "handle %p", bss->nl_mgmt);
1941
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001942 for (i = 0; i < ARRAY_SIZE(stypes); i++) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001943 if (nl80211_register_frame(bss, bss->nl_mgmt,
1944 (WLAN_FC_TYPE_MGMT << 2) |
1945 (stypes[i] << 4),
1946 NULL, 0) < 0) {
1947 goto out_err;
1948 }
1949 }
1950
1951 if (nl80211_register_spurious_class3(bss))
1952 goto out_err;
1953
1954 if (nl80211_get_wiphy_data_ap(bss) == NULL)
1955 goto out_err;
1956
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001957 nl80211_mgmt_handle_register_eloop(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001958 return 0;
1959
1960out_err:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001961 nl_destroy_handles(&bss->nl_mgmt);
1962 return -1;
1963}
1964
1965
1966static int nl80211_mgmt_subscribe_ap_dev_sme(struct i802_bss *bss)
1967{
1968 if (nl80211_alloc_mgmt_handle(bss))
1969 return -1;
1970 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
1971 "handle %p (device SME)", bss->nl_mgmt);
1972
1973 if (nl80211_register_frame(bss, bss->nl_mgmt,
1974 (WLAN_FC_TYPE_MGMT << 2) |
1975 (WLAN_FC_STYPE_ACTION << 4),
1976 NULL, 0) < 0)
1977 goto out_err;
1978
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001979 nl80211_mgmt_handle_register_eloop(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001980 return 0;
1981
1982out_err:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001983 nl_destroy_handles(&bss->nl_mgmt);
1984 return -1;
1985}
1986
1987
1988static void nl80211_mgmt_unsubscribe(struct i802_bss *bss, const char *reason)
1989{
1990 if (bss->nl_mgmt == NULL)
1991 return;
1992 wpa_printf(MSG_DEBUG, "nl80211: Unsubscribe mgmt frames handle %p "
1993 "(%s)", bss->nl_mgmt, reason);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001994 nl80211_destroy_eloop_handle(&bss->nl_mgmt);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001995
1996 nl80211_put_wiphy_data_ap(bss);
1997}
1998
1999
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002000static void wpa_driver_nl80211_send_rfkill(void *eloop_ctx, void *timeout_ctx)
2001{
2002 wpa_supplicant_event(timeout_ctx, EVENT_INTERFACE_DISABLED, NULL);
2003}
2004
2005
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002006static void nl80211_del_p2pdev(struct i802_bss *bss)
2007{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002008 struct nl_msg *msg;
2009 int ret;
2010
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002011 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_DEL_INTERFACE);
2012 ret = send_and_recv_msgs(bss->drv, msg, NULL, NULL);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002013
2014 wpa_printf(MSG_DEBUG, "nl80211: Delete P2P Device %s (0x%llx): %s",
2015 bss->ifname, (long long unsigned int) bss->wdev_id,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002016 strerror(-ret));
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002017}
2018
2019
2020static int nl80211_set_p2pdev(struct i802_bss *bss, int start)
2021{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002022 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002023 int ret;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002024
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002025 msg = nl80211_cmd_msg(bss, 0, start ? NL80211_CMD_START_P2P_DEVICE :
2026 NL80211_CMD_STOP_P2P_DEVICE);
2027 ret = send_and_recv_msgs(bss->drv, msg, NULL, NULL);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002028
2029 wpa_printf(MSG_DEBUG, "nl80211: %s P2P Device %s (0x%llx): %s",
2030 start ? "Start" : "Stop",
2031 bss->ifname, (long long unsigned int) bss->wdev_id,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002032 strerror(-ret));
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002033 return ret;
2034}
2035
2036
2037static int i802_set_iface_flags(struct i802_bss *bss, int up)
2038{
2039 enum nl80211_iftype nlmode;
2040
2041 nlmode = nl80211_get_ifmode(bss);
2042 if (nlmode != NL80211_IFTYPE_P2P_DEVICE) {
2043 return linux_set_iface_flags(bss->drv->global->ioctl_sock,
2044 bss->ifname, up);
2045 }
2046
2047 /* P2P Device has start/stop which is equivalent */
2048 return nl80211_set_p2pdev(bss, up);
2049}
2050
2051
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002052static int
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002053wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002054 const u8 *set_addr, int first,
2055 const char *driver_params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002056{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002057 struct i802_bss *bss = drv->first_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002058 int send_rfkill_event = 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002059 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002060
2061 drv->ifindex = if_nametoindex(bss->ifname);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002062 bss->ifindex = drv->ifindex;
2063 bss->wdev_id = drv->global->if_add_wdevid;
2064 bss->wdev_id_set = drv->global->if_add_wdevid_set;
2065
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002066 bss->if_dynamic = drv->ifindex == drv->global->if_add_ifindex;
2067 bss->if_dynamic = bss->if_dynamic || drv->global->if_add_wdevid_set;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002068 drv->global->if_add_wdevid_set = 0;
2069
Dmitry Shmidt03658832014-08-13 11:03:49 -07002070 if (!bss->if_dynamic && nl80211_get_ifmode(bss) == NL80211_IFTYPE_AP)
2071 bss->static_ap = 1;
2072
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002073 if (wpa_driver_nl80211_capa(drv))
2074 return -1;
2075
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002076 if (driver_params && nl80211_set_param(bss, driver_params) < 0)
2077 return -1;
2078
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002079 wpa_printf(MSG_DEBUG, "nl80211: interface %s in phy %s",
2080 bss->ifname, drv->phyname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002081
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002082 if (set_addr &&
2083 (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0) ||
2084 linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
2085 set_addr)))
2086 return -1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002087
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002088 if (first && nl80211_get_ifmode(bss) == NL80211_IFTYPE_AP)
2089 drv->start_mode_ap = 1;
2090
Dmitry Shmidt03658832014-08-13 11:03:49 -07002091 if (drv->hostapd || bss->static_ap)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002092 nlmode = NL80211_IFTYPE_AP;
2093 else if (bss->if_dynamic)
2094 nlmode = nl80211_get_ifmode(bss);
2095 else
2096 nlmode = NL80211_IFTYPE_STATION;
2097
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002098 if (wpa_driver_nl80211_set_mode(bss, nlmode) < 0) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002099 wpa_printf(MSG_ERROR, "nl80211: Could not configure driver mode");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002100 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002101 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002102
Dmitry Shmidt98660862014-03-11 17:26:21 -07002103 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002104 nl80211_get_macaddr(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002105
Dmitry Shmidt98660862014-03-11 17:26:21 -07002106 if (!rfkill_is_blocked(drv->rfkill)) {
2107 int ret = i802_set_iface_flags(bss, 1);
2108 if (ret) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002109 wpa_printf(MSG_ERROR, "nl80211: Could not set "
2110 "interface '%s' UP", bss->ifname);
Dmitry Shmidt98660862014-03-11 17:26:21 -07002111 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002112 }
Dmitry Shmidt98660862014-03-11 17:26:21 -07002113 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
2114 return ret;
2115 } else {
2116 wpa_printf(MSG_DEBUG, "nl80211: Could not yet enable "
2117 "interface '%s' due to rfkill", bss->ifname);
2118 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
2119 return 0;
2120 drv->if_disabled = 1;
2121 send_rfkill_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002122 }
2123
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002124 if (!drv->hostapd)
2125 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
2126 1, IF_OPER_DORMANT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002127
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002128 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
2129 bss->addr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002130 return -1;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002131 os_memcpy(drv->perm_addr, bss->addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002132
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002133 if (send_rfkill_event) {
2134 eloop_register_timeout(0, 0, wpa_driver_nl80211_send_rfkill,
2135 drv, drv->ctx);
2136 }
2137
2138 return 0;
2139}
2140
2141
2142static int wpa_driver_nl80211_del_beacon(struct wpa_driver_nl80211_data *drv)
2143{
2144 struct nl_msg *msg;
2145
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002146 wpa_printf(MSG_DEBUG, "nl80211: Remove beacon (ifindex=%d)",
2147 drv->ifindex);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002148 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_DEL_BEACON);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002149 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002150}
2151
2152
2153/**
2154 * wpa_driver_nl80211_deinit - Deinitialize nl80211 driver interface
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002155 * @bss: Pointer to private nl80211 data from wpa_driver_nl80211_init()
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002156 *
2157 * Shut down driver interface and processing of driver events. Free
2158 * private data buffer if one was allocated in wpa_driver_nl80211_init().
2159 */
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002160static void wpa_driver_nl80211_deinit(struct i802_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002161{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002162 struct wpa_driver_nl80211_data *drv = bss->drv;
2163
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002164 wpa_printf(MSG_INFO, "nl80211: deinit ifname=%s disabled_11b_rates=%d",
2165 bss->ifname, drv->disabled_11b_rates);
2166
Dmitry Shmidt04949592012-07-19 12:16:46 -07002167 bss->in_deinit = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002168 if (drv->data_tx_status)
2169 eloop_unregister_read_sock(drv->eapol_tx_sock);
2170 if (drv->eapol_tx_sock >= 0)
2171 close(drv->eapol_tx_sock);
2172
2173 if (bss->nl_preq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002174 wpa_driver_nl80211_probe_req_report(bss, 0);
2175 if (bss->added_if_into_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002176 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
2177 bss->ifname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002178 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
2179 "interface %s from bridge %s: %s",
2180 bss->ifname, bss->brname, strerror(errno));
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002181 if (drv->rtnl_sk)
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07002182 nl80211_handle_destroy(drv->rtnl_sk);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002183 }
2184 if (bss->added_bridge) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002185 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->brname,
2186 0) < 0)
2187 wpa_printf(MSG_INFO,
2188 "nl80211: Could not set bridge %s down",
2189 bss->brname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002190 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002191 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
2192 "bridge %s: %s",
2193 bss->brname, strerror(errno));
2194 }
2195
2196 nl80211_remove_monitor_interface(drv);
2197
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002198 if (is_ap_interface(drv->nlmode))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002199 wpa_driver_nl80211_del_beacon(drv);
2200
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002201 if (drv->eapol_sock >= 0) {
2202 eloop_unregister_read_sock(drv->eapol_sock);
2203 close(drv->eapol_sock);
2204 }
2205
2206 if (drv->if_indices != drv->default_if_indices)
2207 os_free(drv->if_indices);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002208
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002209 if (drv->disabled_11b_rates)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002210 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
2211
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002212 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, 0,
2213 IF_OPER_UP);
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07002214 eloop_cancel_timeout(wpa_driver_nl80211_send_rfkill, drv, drv->ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002215 rfkill_deinit(drv->rfkill);
2216
2217 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
2218
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002219 if (!drv->start_iface_up)
2220 (void) i802_set_iface_flags(bss, 0);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002221
2222 if (drv->addr_changed) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002223 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname,
2224 0) < 0) {
2225 wpa_printf(MSG_DEBUG,
2226 "nl80211: Could not set interface down to restore permanent MAC address");
2227 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002228 if (linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
2229 drv->perm_addr) < 0) {
2230 wpa_printf(MSG_DEBUG,
2231 "nl80211: Could not restore permanent MAC address");
2232 }
2233 }
2234
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002235 if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002236 if (!drv->hostapd || !drv->start_mode_ap)
2237 wpa_driver_nl80211_set_mode(bss,
2238 NL80211_IFTYPE_STATION);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002239 nl80211_mgmt_unsubscribe(bss, "deinit");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002240 } else {
2241 nl80211_mgmt_unsubscribe(bss, "deinit");
2242 nl80211_del_p2pdev(bss);
2243 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002244
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002245 nl80211_destroy_bss(drv->first_bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002246
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002247 os_free(drv->filter_ssids);
2248
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002249 os_free(drv->auth_ie);
2250
2251 if (drv->in_interface_list)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002252 dl_list_del(&drv->list);
2253
Dmitry Shmidt444d5672013-04-01 13:08:44 -07002254 os_free(drv->extended_capa);
2255 os_free(drv->extended_capa_mask);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002256 os_free(drv->first_bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002257 os_free(drv);
2258}
2259
2260
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002261static u32 wpa_alg_to_cipher_suite(enum wpa_alg alg, size_t key_len)
2262{
2263 switch (alg) {
2264 case WPA_ALG_WEP:
2265 if (key_len == 5)
2266 return WLAN_CIPHER_SUITE_WEP40;
2267 return WLAN_CIPHER_SUITE_WEP104;
2268 case WPA_ALG_TKIP:
2269 return WLAN_CIPHER_SUITE_TKIP;
2270 case WPA_ALG_CCMP:
2271 return WLAN_CIPHER_SUITE_CCMP;
2272 case WPA_ALG_GCMP:
2273 return WLAN_CIPHER_SUITE_GCMP;
2274 case WPA_ALG_CCMP_256:
2275 return WLAN_CIPHER_SUITE_CCMP_256;
2276 case WPA_ALG_GCMP_256:
2277 return WLAN_CIPHER_SUITE_GCMP_256;
2278 case WPA_ALG_IGTK:
2279 return WLAN_CIPHER_SUITE_AES_CMAC;
2280 case WPA_ALG_BIP_GMAC_128:
2281 return WLAN_CIPHER_SUITE_BIP_GMAC_128;
2282 case WPA_ALG_BIP_GMAC_256:
2283 return WLAN_CIPHER_SUITE_BIP_GMAC_256;
2284 case WPA_ALG_BIP_CMAC_256:
2285 return WLAN_CIPHER_SUITE_BIP_CMAC_256;
2286 case WPA_ALG_SMS4:
2287 return WLAN_CIPHER_SUITE_SMS4;
2288 case WPA_ALG_KRK:
2289 return WLAN_CIPHER_SUITE_KRK;
2290 case WPA_ALG_NONE:
2291 case WPA_ALG_PMK:
2292 wpa_printf(MSG_ERROR, "nl80211: Unexpected encryption algorithm %d",
2293 alg);
2294 return 0;
2295 }
2296
2297 wpa_printf(MSG_ERROR, "nl80211: Unsupported encryption algorithm %d",
2298 alg);
2299 return 0;
2300}
2301
2302
2303static u32 wpa_cipher_to_cipher_suite(unsigned int cipher)
2304{
2305 switch (cipher) {
2306 case WPA_CIPHER_CCMP_256:
2307 return WLAN_CIPHER_SUITE_CCMP_256;
2308 case WPA_CIPHER_GCMP_256:
2309 return WLAN_CIPHER_SUITE_GCMP_256;
2310 case WPA_CIPHER_CCMP:
2311 return WLAN_CIPHER_SUITE_CCMP;
2312 case WPA_CIPHER_GCMP:
2313 return WLAN_CIPHER_SUITE_GCMP;
2314 case WPA_CIPHER_TKIP:
2315 return WLAN_CIPHER_SUITE_TKIP;
2316 case WPA_CIPHER_WEP104:
2317 return WLAN_CIPHER_SUITE_WEP104;
2318 case WPA_CIPHER_WEP40:
2319 return WLAN_CIPHER_SUITE_WEP40;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002320 case WPA_CIPHER_GTK_NOT_USED:
2321 return WLAN_CIPHER_SUITE_NO_GROUP_ADDR;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002322 }
2323
2324 return 0;
2325}
2326
2327
2328static int wpa_cipher_to_cipher_suites(unsigned int ciphers, u32 suites[],
2329 int max_suites)
2330{
2331 int num_suites = 0;
2332
2333 if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP_256)
2334 suites[num_suites++] = WLAN_CIPHER_SUITE_CCMP_256;
2335 if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP_256)
2336 suites[num_suites++] = WLAN_CIPHER_SUITE_GCMP_256;
2337 if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP)
2338 suites[num_suites++] = WLAN_CIPHER_SUITE_CCMP;
2339 if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP)
2340 suites[num_suites++] = WLAN_CIPHER_SUITE_GCMP;
2341 if (num_suites < max_suites && ciphers & WPA_CIPHER_TKIP)
2342 suites[num_suites++] = WLAN_CIPHER_SUITE_TKIP;
2343 if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP104)
2344 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP104;
2345 if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP40)
2346 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP40;
2347
2348 return num_suites;
2349}
2350
2351
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002352static int issue_key_mgmt_set_key(struct wpa_driver_nl80211_data *drv,
2353 const u8 *key, size_t key_len)
2354{
2355 struct nl_msg *msg;
2356 int ret;
2357
2358 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_KEY_MGMT_OFFLOAD))
2359 return 0;
2360
2361 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
2362 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
2363 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
2364 QCA_NL80211_VENDOR_SUBCMD_KEY_MGMT_SET_KEY) ||
2365 nla_put(msg, NL80211_ATTR_VENDOR_DATA, key_len, key)) {
2366 nl80211_nlmsg_clear(msg);
2367 nlmsg_free(msg);
2368 return -1;
2369 }
2370 ret = send_and_recv_msgs(drv, msg, NULL, (void *) -1);
2371 if (ret) {
2372 wpa_printf(MSG_DEBUG,
2373 "nl80211: Key management set key failed: ret=%d (%s)",
2374 ret, strerror(-ret));
2375 }
2376
2377 return ret;
2378}
2379
2380
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002381static int wpa_driver_nl80211_set_key(const char *ifname, struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002382 enum wpa_alg alg, const u8 *addr,
2383 int key_idx, int set_tx,
2384 const u8 *seq, size_t seq_len,
2385 const u8 *key, size_t key_len)
2386{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002387 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002388 int ifindex;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002389 struct nl_msg *msg;
2390 int ret;
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07002391 int tdls = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002392
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002393 /* Ignore for P2P Device */
2394 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
2395 return 0;
2396
2397 ifindex = if_nametoindex(ifname);
2398 wpa_printf(MSG_DEBUG, "%s: ifindex=%d (%s) alg=%d addr=%p key_idx=%d "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002399 "set_tx=%d seq_len=%lu key_len=%lu",
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002400 __func__, ifindex, ifname, alg, addr, key_idx, set_tx,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002401 (unsigned long) seq_len, (unsigned long) key_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002402#ifdef CONFIG_TDLS
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07002403 if (key_idx == -1) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002404 key_idx = 0;
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07002405 tdls = 1;
2406 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002407#endif /* CONFIG_TDLS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002408
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002409 if (alg == WPA_ALG_PMK &&
2410 (drv->capa.flags & WPA_DRIVER_FLAGS_KEY_MGMT_OFFLOAD)) {
2411 wpa_printf(MSG_DEBUG, "%s: calling issue_key_mgmt_set_key",
2412 __func__);
2413 ret = issue_key_mgmt_set_key(drv, key, key_len);
2414 return ret;
2415 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002416
2417 if (alg == WPA_ALG_NONE) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002418 msg = nl80211_ifindex_msg(drv, ifindex, 0, NL80211_CMD_DEL_KEY);
2419 if (!msg)
2420 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002421 } else {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002422 msg = nl80211_ifindex_msg(drv, ifindex, 0, NL80211_CMD_NEW_KEY);
2423 if (!msg ||
2424 nla_put(msg, NL80211_ATTR_KEY_DATA, key_len, key) ||
2425 nla_put_u32(msg, NL80211_ATTR_KEY_CIPHER,
2426 wpa_alg_to_cipher_suite(alg, key_len)))
2427 goto fail;
Dmitry Shmidt98660862014-03-11 17:26:21 -07002428 wpa_hexdump_key(MSG_DEBUG, "nl80211: KEY_DATA", key, key_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002429 }
2430
Dmitry Shmidt98660862014-03-11 17:26:21 -07002431 if (seq && seq_len) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002432 if (nla_put(msg, NL80211_ATTR_KEY_SEQ, seq_len, seq))
2433 goto fail;
Dmitry Shmidt98660862014-03-11 17:26:21 -07002434 wpa_hexdump(MSG_DEBUG, "nl80211: KEY_SEQ", seq, seq_len);
2435 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002436
2437 if (addr && !is_broadcast_ether_addr(addr)) {
2438 wpa_printf(MSG_DEBUG, " addr=" MACSTR, MAC2STR(addr));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002439 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
2440 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002441
2442 if (alg != WPA_ALG_WEP && key_idx && !set_tx) {
2443 wpa_printf(MSG_DEBUG, " RSN IBSS RX GTK");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002444 if (nla_put_u32(msg, NL80211_ATTR_KEY_TYPE,
2445 NL80211_KEYTYPE_GROUP))
2446 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002447 }
2448 } else if (addr && is_broadcast_ether_addr(addr)) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002449 struct nlattr *types;
2450
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002451 wpa_printf(MSG_DEBUG, " broadcast key");
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002452
2453 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002454 if (!types ||
2455 nla_put_flag(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST))
2456 goto fail;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002457 nla_nest_end(msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002458 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002459 if (nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
2460 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002461
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002462 ret = send_and_recv_msgs(drv, msg, NULL, key ? (void *) -1 : NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002463 if ((ret == -ENOENT || ret == -ENOLINK) && alg == WPA_ALG_NONE)
2464 ret = 0;
2465 if (ret)
2466 wpa_printf(MSG_DEBUG, "nl80211: set_key failed; err=%d %s)",
2467 ret, strerror(-ret));
2468
2469 /*
2470 * If we failed or don't need to set the default TX key (below),
2471 * we're done here.
2472 */
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07002473 if (ret || !set_tx || alg == WPA_ALG_NONE || tdls)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002474 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002475 if (is_ap_interface(drv->nlmode) && addr &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002476 !is_broadcast_ether_addr(addr))
2477 return ret;
2478
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002479 msg = nl80211_ifindex_msg(drv, ifindex, 0, NL80211_CMD_SET_KEY);
2480 if (!msg ||
2481 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx) ||
2482 nla_put_flag(msg, alg == WPA_ALG_IGTK ?
2483 NL80211_ATTR_KEY_DEFAULT_MGMT :
2484 NL80211_ATTR_KEY_DEFAULT))
2485 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002486 if (addr && is_broadcast_ether_addr(addr)) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002487 struct nlattr *types;
2488
2489 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002490 if (!types ||
2491 nla_put_flag(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST))
2492 goto fail;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002493 nla_nest_end(msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002494 } else if (addr) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002495 struct nlattr *types;
2496
2497 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002498 if (!types ||
2499 nla_put_flag(msg, NL80211_KEY_DEFAULT_TYPE_UNICAST))
2500 goto fail;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002501 nla_nest_end(msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002502 }
2503
2504 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
2505 if (ret == -ENOENT)
2506 ret = 0;
2507 if (ret)
2508 wpa_printf(MSG_DEBUG, "nl80211: set_key default failed; "
2509 "err=%d %s)", ret, strerror(-ret));
2510 return ret;
2511
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002512fail:
2513 nl80211_nlmsg_clear(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002514 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002515 return -ENOBUFS;
2516}
2517
2518
2519static int nl_add_key(struct nl_msg *msg, enum wpa_alg alg,
2520 int key_idx, int defkey,
2521 const u8 *seq, size_t seq_len,
2522 const u8 *key, size_t key_len)
2523{
2524 struct nlattr *key_attr = nla_nest_start(msg, NL80211_ATTR_KEY);
2525 if (!key_attr)
2526 return -1;
2527
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002528 if (defkey && alg == WPA_ALG_IGTK) {
2529 if (nla_put_flag(msg, NL80211_KEY_DEFAULT_MGMT))
2530 return -1;
2531 } else if (defkey) {
2532 if (nla_put_flag(msg, NL80211_KEY_DEFAULT))
2533 return -1;
2534 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002535
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002536 if (nla_put_u8(msg, NL80211_KEY_IDX, key_idx) ||
2537 nla_put_u32(msg, NL80211_KEY_CIPHER,
2538 wpa_alg_to_cipher_suite(alg, key_len)) ||
2539 (seq && seq_len &&
2540 nla_put(msg, NL80211_KEY_SEQ, seq_len, seq)) ||
2541 nla_put(msg, NL80211_KEY_DATA, key_len, key))
2542 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002543
2544 nla_nest_end(msg, key_attr);
2545
2546 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002547}
2548
2549
2550static int nl80211_set_conn_keys(struct wpa_driver_associate_params *params,
2551 struct nl_msg *msg)
2552{
2553 int i, privacy = 0;
2554 struct nlattr *nl_keys, *nl_key;
2555
2556 for (i = 0; i < 4; i++) {
2557 if (!params->wep_key[i])
2558 continue;
2559 privacy = 1;
2560 break;
2561 }
2562 if (params->wps == WPS_MODE_PRIVACY)
2563 privacy = 1;
2564 if (params->pairwise_suite &&
2565 params->pairwise_suite != WPA_CIPHER_NONE)
2566 privacy = 1;
2567
2568 if (!privacy)
2569 return 0;
2570
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002571 if (nla_put_flag(msg, NL80211_ATTR_PRIVACY))
2572 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002573
2574 nl_keys = nla_nest_start(msg, NL80211_ATTR_KEYS);
2575 if (!nl_keys)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002576 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002577
2578 for (i = 0; i < 4; i++) {
2579 if (!params->wep_key[i])
2580 continue;
2581
2582 nl_key = nla_nest_start(msg, i);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002583 if (!nl_key ||
2584 nla_put(msg, NL80211_KEY_DATA, params->wep_key_len[i],
2585 params->wep_key[i]) ||
2586 nla_put_u32(msg, NL80211_KEY_CIPHER,
2587 params->wep_key_len[i] == 5 ?
2588 WLAN_CIPHER_SUITE_WEP40 :
2589 WLAN_CIPHER_SUITE_WEP104) ||
2590 nla_put_u8(msg, NL80211_KEY_IDX, i) ||
2591 (i == params->wep_tx_keyidx &&
2592 nla_put_flag(msg, NL80211_KEY_DEFAULT)))
2593 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002594
2595 nla_nest_end(msg, nl_key);
2596 }
2597 nla_nest_end(msg, nl_keys);
2598
2599 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002600}
2601
2602
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002603int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
2604 const u8 *addr, int cmd, u16 reason_code,
2605 int local_state_change)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002606{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002607 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002608 struct nl_msg *msg;
2609
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002610 if (!(msg = nl80211_drv_msg(drv, 0, cmd)) ||
2611 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code) ||
2612 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
2613 (local_state_change &&
2614 nla_put_flag(msg, NL80211_ATTR_LOCAL_STATE_CHANGE))) {
2615 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002616 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002617 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002618
2619 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002620 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002621 wpa_dbg(drv->ctx, MSG_DEBUG,
2622 "nl80211: MLME command failed: reason=%u ret=%d (%s)",
2623 reason_code, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002624 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002625 return ret;
2626}
2627
2628
2629static int wpa_driver_nl80211_disconnect(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002630 int reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002631{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002632 int ret;
2633
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002634 wpa_printf(MSG_DEBUG, "%s(reason_code=%d)", __func__, reason_code);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07002635 nl80211_mark_disconnected(drv);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002636 /* Disconnect command doesn't need BSSID - it uses cached value */
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002637 ret = wpa_driver_nl80211_mlme(drv, NULL, NL80211_CMD_DISCONNECT,
2638 reason_code, 0);
2639 /*
2640 * For locally generated disconnect, supplicant already generates a
2641 * DEAUTH event, so ignore the event from NL80211.
2642 */
2643 drv->ignore_next_local_disconnect = ret == 0;
2644
2645 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002646}
2647
2648
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002649static int wpa_driver_nl80211_deauthenticate(struct i802_bss *bss,
2650 const u8 *addr, int reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002651{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002652 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07002653 int ret;
Dmitry Shmidt413dde72014-04-11 10:23:22 -07002654
2655 if (drv->nlmode == NL80211_IFTYPE_ADHOC) {
2656 nl80211_mark_disconnected(drv);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002657 return nl80211_leave_ibss(drv, 1);
Dmitry Shmidt413dde72014-04-11 10:23:22 -07002658 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002659 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME))
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002660 return wpa_driver_nl80211_disconnect(drv, reason_code);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002661 wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " reason_code=%d)",
2662 __func__, MAC2STR(addr), reason_code);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07002663 nl80211_mark_disconnected(drv);
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07002664 ret = wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DEAUTHENTICATE,
2665 reason_code, 0);
2666 /*
2667 * For locally generated deauthenticate, supplicant already generates a
2668 * DEAUTH event, so ignore the event from NL80211.
2669 */
2670 drv->ignore_next_local_deauth = ret == 0;
2671 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002672}
2673
2674
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002675static void nl80211_copy_auth_params(struct wpa_driver_nl80211_data *drv,
2676 struct wpa_driver_auth_params *params)
2677{
2678 int i;
2679
2680 drv->auth_freq = params->freq;
2681 drv->auth_alg = params->auth_alg;
2682 drv->auth_wep_tx_keyidx = params->wep_tx_keyidx;
2683 drv->auth_local_state_change = params->local_state_change;
2684 drv->auth_p2p = params->p2p;
2685
2686 if (params->bssid)
2687 os_memcpy(drv->auth_bssid_, params->bssid, ETH_ALEN);
2688 else
2689 os_memset(drv->auth_bssid_, 0, ETH_ALEN);
2690
2691 if (params->ssid) {
2692 os_memcpy(drv->auth_ssid, params->ssid, params->ssid_len);
2693 drv->auth_ssid_len = params->ssid_len;
2694 } else
2695 drv->auth_ssid_len = 0;
2696
2697
2698 os_free(drv->auth_ie);
2699 drv->auth_ie = NULL;
2700 drv->auth_ie_len = 0;
2701 if (params->ie) {
2702 drv->auth_ie = os_malloc(params->ie_len);
2703 if (drv->auth_ie) {
2704 os_memcpy(drv->auth_ie, params->ie, params->ie_len);
2705 drv->auth_ie_len = params->ie_len;
2706 }
2707 }
2708
2709 for (i = 0; i < 4; i++) {
2710 if (params->wep_key[i] && params->wep_key_len[i] &&
2711 params->wep_key_len[i] <= 16) {
2712 os_memcpy(drv->auth_wep_key[i], params->wep_key[i],
2713 params->wep_key_len[i]);
2714 drv->auth_wep_key_len[i] = params->wep_key_len[i];
2715 } else
2716 drv->auth_wep_key_len[i] = 0;
2717 }
2718}
2719
2720
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002721static void nl80211_unmask_11b_rates(struct i802_bss *bss)
2722{
2723 struct wpa_driver_nl80211_data *drv = bss->drv;
2724
2725 if (is_p2p_net_interface(drv->nlmode) || !drv->disabled_11b_rates)
2726 return;
2727
2728 /*
2729 * Looks like we failed to unmask 11b rates previously. This could
2730 * happen, e.g., if the interface was down at the point in time when a
2731 * P2P group was terminated.
2732 */
2733 wpa_printf(MSG_DEBUG,
2734 "nl80211: Interface %s mode is for non-P2P, but 11b rates were disabled - re-enable them",
2735 bss->ifname);
2736 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
2737}
2738
2739
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002740static int wpa_driver_nl80211_authenticate(
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002741 struct i802_bss *bss, struct wpa_driver_auth_params *params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002742{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002743 struct wpa_driver_nl80211_data *drv = bss->drv;
2744 int ret = -1, i;
2745 struct nl_msg *msg;
2746 enum nl80211_auth_type type;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002747 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002748 int count = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002749 int is_retry;
2750
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002751 nl80211_unmask_11b_rates(bss);
2752
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002753 is_retry = drv->retry_auth;
2754 drv->retry_auth = 0;
Dmitry Shmidt98660862014-03-11 17:26:21 -07002755 drv->ignore_deauth_event = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002756
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07002757 nl80211_mark_disconnected(drv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002758 os_memset(drv->auth_bssid, 0, ETH_ALEN);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07002759 if (params->bssid)
2760 os_memcpy(drv->auth_attempt_bssid, params->bssid, ETH_ALEN);
2761 else
2762 os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002763 /* FIX: IBSS mode */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002764 nlmode = params->p2p ?
2765 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
2766 if (drv->nlmode != nlmode &&
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002767 wpa_driver_nl80211_set_mode(bss, nlmode) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002768 return -1;
2769
2770retry:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002771 wpa_printf(MSG_DEBUG, "nl80211: Authenticate (ifindex=%d)",
2772 drv->ifindex);
2773
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002774 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_AUTHENTICATE);
2775 if (!msg)
2776 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002777
2778 for (i = 0; i < 4; i++) {
2779 if (!params->wep_key[i])
2780 continue;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002781 wpa_driver_nl80211_set_key(bss->ifname, bss, WPA_ALG_WEP,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002782 NULL, i,
2783 i == params->wep_tx_keyidx, NULL, 0,
2784 params->wep_key[i],
2785 params->wep_key_len[i]);
2786 if (params->wep_tx_keyidx != i)
2787 continue;
2788 if (nl_add_key(msg, WPA_ALG_WEP, i, 1, NULL, 0,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002789 params->wep_key[i], params->wep_key_len[i]))
2790 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002791 }
2792
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002793 if (params->bssid) {
2794 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
2795 MAC2STR(params->bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002796 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid))
2797 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002798 }
2799 if (params->freq) {
2800 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002801 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq))
2802 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002803 }
2804 if (params->ssid) {
2805 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
2806 params->ssid, params->ssid_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002807 if (nla_put(msg, NL80211_ATTR_SSID, params->ssid_len,
2808 params->ssid))
2809 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002810 }
2811 wpa_hexdump(MSG_DEBUG, " * IEs", params->ie, params->ie_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002812 if (params->ie &&
2813 nla_put(msg, NL80211_ATTR_IE, params->ie_len, params->ie))
2814 goto fail;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002815 if (params->sae_data) {
2816 wpa_hexdump(MSG_DEBUG, " * SAE data", params->sae_data,
2817 params->sae_data_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002818 if (nla_put(msg, NL80211_ATTR_SAE_DATA, params->sae_data_len,
2819 params->sae_data))
2820 goto fail;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002821 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002822 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
2823 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
2824 else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
2825 type = NL80211_AUTHTYPE_SHARED_KEY;
2826 else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
2827 type = NL80211_AUTHTYPE_NETWORK_EAP;
2828 else if (params->auth_alg & WPA_AUTH_ALG_FT)
2829 type = NL80211_AUTHTYPE_FT;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002830 else if (params->auth_alg & WPA_AUTH_ALG_SAE)
2831 type = NL80211_AUTHTYPE_SAE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002832 else
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002833 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002834 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002835 if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE, type))
2836 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002837 if (params->local_state_change) {
2838 wpa_printf(MSG_DEBUG, " * Local state change only");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002839 if (nla_put_flag(msg, NL80211_ATTR_LOCAL_STATE_CHANGE))
2840 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002841 }
2842
2843 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
2844 msg = NULL;
2845 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002846 wpa_dbg(drv->ctx, MSG_DEBUG,
2847 "nl80211: MLME command failed (auth): ret=%d (%s)",
2848 ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002849 count++;
2850 if (ret == -EALREADY && count == 1 && params->bssid &&
2851 !params->local_state_change) {
2852 /*
2853 * mac80211 does not currently accept new
2854 * authentication if we are already authenticated. As a
2855 * workaround, force deauthentication and try again.
2856 */
2857 wpa_printf(MSG_DEBUG, "nl80211: Retry authentication "
2858 "after forced deauthentication");
Dmitry Shmidt98660862014-03-11 17:26:21 -07002859 drv->ignore_deauth_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002860 wpa_driver_nl80211_deauthenticate(
2861 bss, params->bssid,
2862 WLAN_REASON_PREV_AUTH_NOT_VALID);
2863 nlmsg_free(msg);
2864 goto retry;
2865 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002866
2867 if (ret == -ENOENT && params->freq && !is_retry) {
2868 /*
2869 * cfg80211 has likely expired the BSS entry even
2870 * though it was previously available in our internal
2871 * BSS table. To recover quickly, start a single
2872 * channel scan on the specified channel.
2873 */
2874 struct wpa_driver_scan_params scan;
2875 int freqs[2];
2876
2877 os_memset(&scan, 0, sizeof(scan));
2878 scan.num_ssids = 1;
2879 if (params->ssid) {
2880 scan.ssids[0].ssid = params->ssid;
2881 scan.ssids[0].ssid_len = params->ssid_len;
2882 }
2883 freqs[0] = params->freq;
2884 freqs[1] = 0;
2885 scan.freqs = freqs;
2886 wpa_printf(MSG_DEBUG, "nl80211: Trigger single "
2887 "channel scan to refresh cfg80211 BSS "
2888 "entry");
2889 ret = wpa_driver_nl80211_scan(bss, &scan);
2890 if (ret == 0) {
2891 nl80211_copy_auth_params(drv, params);
2892 drv->scan_for_auth = 1;
2893 }
2894 } else if (is_retry) {
2895 /*
2896 * Need to indicate this with an event since the return
2897 * value from the retry is not delivered to core code.
2898 */
2899 union wpa_event_data event;
2900 wpa_printf(MSG_DEBUG, "nl80211: Authentication retry "
2901 "failed");
2902 os_memset(&event, 0, sizeof(event));
2903 os_memcpy(event.timeout_event.addr, drv->auth_bssid_,
2904 ETH_ALEN);
2905 wpa_supplicant_event(drv->ctx, EVENT_AUTH_TIMED_OUT,
2906 &event);
2907 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002908 } else {
2909 wpa_printf(MSG_DEBUG,
2910 "nl80211: Authentication request send successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002911 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002912
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002913fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002914 nlmsg_free(msg);
2915 return ret;
2916}
2917
2918
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002919int wpa_driver_nl80211_authenticate_retry(struct wpa_driver_nl80211_data *drv)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002920{
2921 struct wpa_driver_auth_params params;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002922 struct i802_bss *bss = drv->first_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002923 int i;
2924
2925 wpa_printf(MSG_DEBUG, "nl80211: Try to authenticate again");
2926
2927 os_memset(&params, 0, sizeof(params));
2928 params.freq = drv->auth_freq;
2929 params.auth_alg = drv->auth_alg;
2930 params.wep_tx_keyidx = drv->auth_wep_tx_keyidx;
2931 params.local_state_change = drv->auth_local_state_change;
2932 params.p2p = drv->auth_p2p;
2933
2934 if (!is_zero_ether_addr(drv->auth_bssid_))
2935 params.bssid = drv->auth_bssid_;
2936
2937 if (drv->auth_ssid_len) {
2938 params.ssid = drv->auth_ssid;
2939 params.ssid_len = drv->auth_ssid_len;
2940 }
2941
2942 params.ie = drv->auth_ie;
2943 params.ie_len = drv->auth_ie_len;
2944
2945 for (i = 0; i < 4; i++) {
2946 if (drv->auth_wep_key_len[i]) {
2947 params.wep_key[i] = drv->auth_wep_key[i];
2948 params.wep_key_len[i] = drv->auth_wep_key_len[i];
2949 }
2950 }
2951
2952 drv->retry_auth = 1;
2953 return wpa_driver_nl80211_authenticate(bss, &params);
2954}
2955
2956
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002957static int wpa_driver_nl80211_send_frame(struct i802_bss *bss,
2958 const void *data, size_t len,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002959 int encrypt, int noack,
2960 unsigned int freq, int no_cck,
2961 int offchanok, unsigned int wait_time)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002962{
2963 struct wpa_driver_nl80211_data *drv = bss->drv;
2964 u64 cookie;
Dmitry Shmidt051af732013-10-22 13:52:46 -07002965 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002966
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07002967 if (freq == 0 && drv->nlmode == NL80211_IFTYPE_ADHOC) {
2968 freq = nl80211_get_assoc_freq(drv);
2969 wpa_printf(MSG_DEBUG,
2970 "nl80211: send_frame - Use assoc_freq=%u for IBSS",
2971 freq);
2972 }
Dmitry Shmidt56052862013-10-04 10:23:25 -07002973 if (freq == 0) {
2974 wpa_printf(MSG_DEBUG, "nl80211: send_frame - Use bss->freq=%u",
2975 bss->freq);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002976 freq = bss->freq;
Dmitry Shmidt56052862013-10-04 10:23:25 -07002977 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002978
Dmitry Shmidt56052862013-10-04 10:23:25 -07002979 if (drv->use_monitor) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002980 wpa_printf(MSG_DEBUG, "nl80211: send_frame(freq=%u bss->freq=%u) -> send_monitor",
Dmitry Shmidt56052862013-10-04 10:23:25 -07002981 freq, bss->freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002982 return nl80211_send_monitor(drv, data, len, encrypt, noack);
Dmitry Shmidt56052862013-10-04 10:23:25 -07002983 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002984
Dmitry Shmidt56052862013-10-04 10:23:25 -07002985 wpa_printf(MSG_DEBUG, "nl80211: send_frame -> send_frame_cmd");
Dmitry Shmidt051af732013-10-22 13:52:46 -07002986 res = nl80211_send_frame_cmd(bss, freq, wait_time, data, len,
2987 &cookie, no_cck, noack, offchanok);
2988 if (res == 0 && !noack) {
2989 const struct ieee80211_mgmt *mgmt;
2990 u16 fc;
2991
2992 mgmt = (const struct ieee80211_mgmt *) data;
2993 fc = le_to_host16(mgmt->frame_control);
2994 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
2995 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_ACTION) {
2996 wpa_printf(MSG_MSGDUMP,
2997 "nl80211: Update send_action_cookie from 0x%llx to 0x%llx",
2998 (long long unsigned int)
2999 drv->send_action_cookie,
3000 (long long unsigned int) cookie);
3001 drv->send_action_cookie = cookie;
3002 }
3003 }
3004
3005 return res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003006}
3007
3008
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003009static int wpa_driver_nl80211_send_mlme(struct i802_bss *bss, const u8 *data,
3010 size_t data_len, int noack,
3011 unsigned int freq, int no_cck,
3012 int offchanok,
3013 unsigned int wait_time)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003014{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003015 struct wpa_driver_nl80211_data *drv = bss->drv;
3016 struct ieee80211_mgmt *mgmt;
3017 int encrypt = 1;
3018 u16 fc;
3019
3020 mgmt = (struct ieee80211_mgmt *) data;
3021 fc = le_to_host16(mgmt->frame_control);
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07003022 wpa_printf(MSG_DEBUG, "nl80211: send_mlme - da= " MACSTR
3023 " noack=%d freq=%u no_cck=%d offchanok=%d wait_time=%u fc=0x%x (%s) nlmode=%d",
3024 MAC2STR(mgmt->da), noack, freq, no_cck, offchanok, wait_time,
3025 fc, fc2str(fc), drv->nlmode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003026
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003027 if ((is_sta_interface(drv->nlmode) ||
3028 drv->nlmode == NL80211_IFTYPE_P2P_DEVICE) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003029 WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
3030 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_RESP) {
3031 /*
3032 * The use of last_mgmt_freq is a bit of a hack,
3033 * but it works due to the single-threaded nature
3034 * of wpa_supplicant.
3035 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07003036 if (freq == 0) {
3037 wpa_printf(MSG_DEBUG, "nl80211: Use last_mgmt_freq=%d",
3038 drv->last_mgmt_freq);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003039 freq = drv->last_mgmt_freq;
Dmitry Shmidt56052862013-10-04 10:23:25 -07003040 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003041 return nl80211_send_frame_cmd(bss, freq, 0,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003042 data, data_len, NULL, 1, noack,
3043 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003044 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003045
3046 if (drv->device_ap_sme && is_ap_interface(drv->nlmode)) {
Dmitry Shmidt56052862013-10-04 10:23:25 -07003047 if (freq == 0) {
3048 wpa_printf(MSG_DEBUG, "nl80211: Use bss->freq=%d",
3049 bss->freq);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003050 freq = bss->freq;
Dmitry Shmidt56052862013-10-04 10:23:25 -07003051 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003052 return nl80211_send_frame_cmd(bss, freq,
3053 (int) freq == bss->freq ? 0 :
3054 wait_time,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003055 data, data_len,
3056 &drv->send_action_cookie,
3057 no_cck, noack, offchanok);
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07003058 }
Dmitry Shmidtb638fe72012-03-20 12:51:25 -07003059
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003060 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
3061 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) {
3062 /*
3063 * Only one of the authentication frame types is encrypted.
3064 * In order for static WEP encryption to work properly (i.e.,
3065 * to not encrypt the frame), we need to tell mac80211 about
3066 * the frames that must not be encrypted.
3067 */
3068 u16 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
3069 u16 auth_trans = le_to_host16(mgmt->u.auth.auth_transaction);
3070 if (auth_alg != WLAN_AUTH_SHARED_KEY || auth_trans != 3)
3071 encrypt = 0;
3072 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003073
Dmitry Shmidt56052862013-10-04 10:23:25 -07003074 wpa_printf(MSG_DEBUG, "nl80211: send_mlme -> send_frame");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003075 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003076 noack, freq, no_cck, offchanok,
3077 wait_time);
3078}
3079
3080
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003081static int nl80211_put_basic_rates(struct nl_msg *msg, const int *basic_rates)
3082{
3083 u8 rates[NL80211_MAX_SUPP_RATES];
3084 u8 rates_len = 0;
3085 int i;
3086
3087 if (!basic_rates)
3088 return 0;
3089
3090 for (i = 0; i < NL80211_MAX_SUPP_RATES && basic_rates[i] >= 0; i++)
3091 rates[rates_len++] = basic_rates[i] / 5;
3092
3093 return nla_put(msg, NL80211_ATTR_BSS_BASIC_RATES, rates_len, rates);
3094}
3095
3096
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003097static int nl80211_set_bss(struct i802_bss *bss, int cts, int preamble,
3098 int slot, int ht_opmode, int ap_isolate,
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003099 const int *basic_rates)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003100{
3101 struct wpa_driver_nl80211_data *drv = bss->drv;
3102 struct nl_msg *msg;
3103
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003104 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_BSS)) ||
3105 (cts >= 0 &&
3106 nla_put_u8(msg, NL80211_ATTR_BSS_CTS_PROT, cts)) ||
3107 (preamble >= 0 &&
3108 nla_put_u8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble)) ||
3109 (slot >= 0 &&
3110 nla_put_u8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot)) ||
3111 (ht_opmode >= 0 &&
3112 nla_put_u16(msg, NL80211_ATTR_BSS_HT_OPMODE, ht_opmode)) ||
3113 (ap_isolate >= 0 &&
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003114 nla_put_u8(msg, NL80211_ATTR_AP_ISOLATE, ap_isolate)) ||
3115 nl80211_put_basic_rates(msg, basic_rates)) {
3116 nlmsg_free(msg);
3117 return -ENOBUFS;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003118 }
3119
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003120 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003121}
3122
3123
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003124static int wpa_driver_nl80211_set_acl(void *priv,
3125 struct hostapd_acl_params *params)
3126{
3127 struct i802_bss *bss = priv;
3128 struct wpa_driver_nl80211_data *drv = bss->drv;
3129 struct nl_msg *msg;
3130 struct nlattr *acl;
3131 unsigned int i;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003132 int ret;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003133
3134 if (!(drv->capa.max_acl_mac_addrs))
3135 return -ENOTSUP;
3136
3137 if (params->num_mac_acl > drv->capa.max_acl_mac_addrs)
3138 return -ENOTSUP;
3139
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003140 wpa_printf(MSG_DEBUG, "nl80211: Set %s ACL (num_mac_acl=%u)",
3141 params->acl_policy ? "Accept" : "Deny", params->num_mac_acl);
3142
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003143 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_MAC_ACL)) ||
3144 nla_put_u32(msg, NL80211_ATTR_ACL_POLICY, params->acl_policy ?
3145 NL80211_ACL_POLICY_DENY_UNLESS_LISTED :
3146 NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED) ||
3147 (acl = nla_nest_start(msg, NL80211_ATTR_MAC_ADDRS)) == NULL) {
3148 nlmsg_free(msg);
3149 return -ENOMEM;
3150 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003151
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003152 for (i = 0; i < params->num_mac_acl; i++) {
3153 if (nla_put(msg, i + 1, ETH_ALEN, params->mac_acl[i].addr)) {
3154 nlmsg_free(msg);
3155 return -ENOMEM;
3156 }
3157 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003158
3159 nla_nest_end(msg, acl);
3160
3161 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003162 if (ret) {
3163 wpa_printf(MSG_DEBUG, "nl80211: Failed to set MAC ACL: %d (%s)",
3164 ret, strerror(-ret));
3165 }
3166
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003167 return ret;
3168}
3169
3170
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003171static int nl80211_put_beacon_int(struct nl_msg *msg, int beacon_int)
3172{
3173 if (beacon_int > 0) {
3174 wpa_printf(MSG_DEBUG, " * beacon_int=%d", beacon_int);
3175 return nla_put_u32(msg, NL80211_ATTR_BEACON_INTERVAL,
3176 beacon_int);
3177 }
3178
3179 return 0;
3180}
3181
3182
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003183static int wpa_driver_nl80211_set_ap(void *priv,
3184 struct wpa_driver_ap_params *params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003185{
3186 struct i802_bss *bss = priv;
3187 struct wpa_driver_nl80211_data *drv = bss->drv;
3188 struct nl_msg *msg;
3189 u8 cmd = NL80211_CMD_NEW_BEACON;
3190 int ret;
3191 int beacon_set;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003192 int num_suites;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003193 int smps_mode;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003194 u32 suites[10], suite;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003195 u32 ver;
3196
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07003197 beacon_set = bss->beacon_set;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003198
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003199 wpa_printf(MSG_DEBUG, "nl80211: Set beacon (beacon_set=%d)",
3200 beacon_set);
3201 if (beacon_set)
3202 cmd = NL80211_CMD_SET_BEACON;
3203
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003204 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon head",
3205 params->head, params->head_len);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003206 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon tail",
3207 params->tail, params->tail_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003208 wpa_printf(MSG_DEBUG, "nl80211: ifindex=%d", bss->ifindex);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003209 wpa_printf(MSG_DEBUG, "nl80211: beacon_int=%d", params->beacon_int);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003210 wpa_printf(MSG_DEBUG, "nl80211: dtim_period=%d", params->dtim_period);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003211 wpa_hexdump_ascii(MSG_DEBUG, "nl80211: ssid",
3212 params->ssid, params->ssid_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003213 if (!(msg = nl80211_bss_msg(bss, 0, cmd)) ||
3214 nla_put(msg, NL80211_ATTR_BEACON_HEAD, params->head_len,
3215 params->head) ||
3216 nla_put(msg, NL80211_ATTR_BEACON_TAIL, params->tail_len,
3217 params->tail) ||
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003218 nl80211_put_beacon_int(msg, params->beacon_int) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003219 nla_put_u32(msg, NL80211_ATTR_DTIM_PERIOD, params->dtim_period) ||
3220 nla_put(msg, NL80211_ATTR_SSID, params->ssid_len, params->ssid))
3221 goto fail;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003222 if (params->proberesp && params->proberesp_len) {
3223 wpa_hexdump(MSG_DEBUG, "nl80211: proberesp (offload)",
3224 params->proberesp, params->proberesp_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003225 if (nla_put(msg, NL80211_ATTR_PROBE_RESP, params->proberesp_len,
3226 params->proberesp))
3227 goto fail;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003228 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003229 switch (params->hide_ssid) {
3230 case NO_SSID_HIDING:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003231 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID not in use");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003232 if (nla_put_u32(msg, NL80211_ATTR_HIDDEN_SSID,
3233 NL80211_HIDDEN_SSID_NOT_IN_USE))
3234 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003235 break;
3236 case HIDDEN_SSID_ZERO_LEN:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003237 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero len");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003238 if (nla_put_u32(msg, NL80211_ATTR_HIDDEN_SSID,
3239 NL80211_HIDDEN_SSID_ZERO_LEN))
3240 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003241 break;
3242 case HIDDEN_SSID_ZERO_CONTENTS:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003243 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero contents");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003244 if (nla_put_u32(msg, NL80211_ATTR_HIDDEN_SSID,
3245 NL80211_HIDDEN_SSID_ZERO_CONTENTS))
3246 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003247 break;
3248 }
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003249 wpa_printf(MSG_DEBUG, "nl80211: privacy=%d", params->privacy);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003250 if (params->privacy &&
3251 nla_put_flag(msg, NL80211_ATTR_PRIVACY))
3252 goto fail;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003253 wpa_printf(MSG_DEBUG, "nl80211: auth_algs=0x%x", params->auth_algs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003254 if ((params->auth_algs & (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) ==
3255 (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) {
3256 /* Leave out the attribute */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003257 } else if (params->auth_algs & WPA_AUTH_ALG_SHARED) {
3258 if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE,
3259 NL80211_AUTHTYPE_SHARED_KEY))
3260 goto fail;
3261 } else {
3262 if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE,
3263 NL80211_AUTHTYPE_OPEN_SYSTEM))
3264 goto fail;
3265 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003266
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003267 wpa_printf(MSG_DEBUG, "nl80211: wpa_version=0x%x", params->wpa_version);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003268 ver = 0;
3269 if (params->wpa_version & WPA_PROTO_WPA)
3270 ver |= NL80211_WPA_VERSION_1;
3271 if (params->wpa_version & WPA_PROTO_RSN)
3272 ver |= NL80211_WPA_VERSION_2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003273 if (ver &&
3274 nla_put_u32(msg, NL80211_ATTR_WPA_VERSIONS, ver))
3275 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003276
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003277 wpa_printf(MSG_DEBUG, "nl80211: key_mgmt_suites=0x%x",
3278 params->key_mgmt_suites);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003279 num_suites = 0;
3280 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X)
3281 suites[num_suites++] = WLAN_AKM_SUITE_8021X;
3282 if (params->key_mgmt_suites & WPA_KEY_MGMT_PSK)
3283 suites[num_suites++] = WLAN_AKM_SUITE_PSK;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003284 if (num_suites &&
3285 nla_put(msg, NL80211_ATTR_AKM_SUITES, num_suites * sizeof(u32),
3286 suites))
3287 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003288
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003289 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
3290 params->pairwise_ciphers & (WPA_CIPHER_WEP104 | WPA_CIPHER_WEP40) &&
3291 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT))
3292 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003293
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003294 wpa_printf(MSG_DEBUG, "nl80211: pairwise_ciphers=0x%x",
3295 params->pairwise_ciphers);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003296 num_suites = wpa_cipher_to_cipher_suites(params->pairwise_ciphers,
3297 suites, ARRAY_SIZE(suites));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003298 if (num_suites &&
3299 nla_put(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
3300 num_suites * sizeof(u32), suites))
3301 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003302
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003303 wpa_printf(MSG_DEBUG, "nl80211: group_cipher=0x%x",
3304 params->group_cipher);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003305 suite = wpa_cipher_to_cipher_suite(params->group_cipher);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003306 if (suite &&
3307 nla_put_u32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, suite))
3308 goto fail;
3309
3310 switch (params->smps_mode) {
3311 case HT_CAP_INFO_SMPS_DYNAMIC:
3312 wpa_printf(MSG_DEBUG, "nl80211: SMPS mode - dynamic");
3313 smps_mode = NL80211_SMPS_DYNAMIC;
3314 break;
3315 case HT_CAP_INFO_SMPS_STATIC:
3316 wpa_printf(MSG_DEBUG, "nl80211: SMPS mode - static");
3317 smps_mode = NL80211_SMPS_STATIC;
3318 break;
3319 default:
3320 /* invalid - fallback to smps off */
3321 case HT_CAP_INFO_SMPS_DISABLED:
3322 wpa_printf(MSG_DEBUG, "nl80211: SMPS mode - off");
3323 smps_mode = NL80211_SMPS_OFF;
3324 break;
3325 }
3326 if (nla_put_u32(msg, NL80211_ATTR_SMPS_MODE, smps_mode))
3327 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003328
3329 if (params->beacon_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003330 wpa_hexdump_buf(MSG_DEBUG, "nl80211: beacon_ies",
3331 params->beacon_ies);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003332 if (nla_put(msg, NL80211_ATTR_IE,
3333 wpabuf_len(params->beacon_ies),
3334 wpabuf_head(params->beacon_ies)))
3335 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003336 }
3337 if (params->proberesp_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003338 wpa_hexdump_buf(MSG_DEBUG, "nl80211: proberesp_ies",
3339 params->proberesp_ies);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003340 if (nla_put(msg, NL80211_ATTR_IE_PROBE_RESP,
3341 wpabuf_len(params->proberesp_ies),
3342 wpabuf_head(params->proberesp_ies)))
3343 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003344 }
3345 if (params->assocresp_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003346 wpa_hexdump_buf(MSG_DEBUG, "nl80211: assocresp_ies",
3347 params->assocresp_ies);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003348 if (nla_put(msg, NL80211_ATTR_IE_ASSOC_RESP,
3349 wpabuf_len(params->assocresp_ies),
3350 wpabuf_head(params->assocresp_ies)))
3351 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003352 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003353
Dmitry Shmidt04949592012-07-19 12:16:46 -07003354 if (drv->capa.flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003355 wpa_printf(MSG_DEBUG, "nl80211: ap_max_inactivity=%d",
3356 params->ap_max_inactivity);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003357 if (nla_put_u16(msg, NL80211_ATTR_INACTIVITY_TIMEOUT,
3358 params->ap_max_inactivity))
3359 goto fail;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003360 }
3361
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003362 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3363 if (ret) {
3364 wpa_printf(MSG_DEBUG, "nl80211: Beacon set failed: %d (%s)",
3365 ret, strerror(-ret));
3366 } else {
3367 bss->beacon_set = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003368 nl80211_set_bss(bss, params->cts_protect, params->preamble,
3369 params->short_slot_time, params->ht_opmode,
3370 params->isolate, params->basic_rates);
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07003371 if (beacon_set && params->freq &&
3372 params->freq->bandwidth != bss->bandwidth) {
3373 wpa_printf(MSG_DEBUG,
3374 "nl80211: Update BSS %s bandwidth: %d -> %d",
3375 bss->ifname, bss->bandwidth,
3376 params->freq->bandwidth);
3377 ret = nl80211_set_channel(bss, params->freq, 1);
3378 if (ret) {
3379 wpa_printf(MSG_DEBUG,
3380 "nl80211: Frequency set failed: %d (%s)",
3381 ret, strerror(-ret));
3382 } else {
3383 wpa_printf(MSG_DEBUG,
3384 "nl80211: Frequency set succeeded for ht2040 coex");
3385 bss->bandwidth = params->freq->bandwidth;
3386 }
3387 } else if (!beacon_set) {
3388 /*
3389 * cfg80211 updates the driver on frequence change in AP
3390 * mode only at the point when beaconing is started, so
3391 * set the initial value here.
3392 */
3393 bss->bandwidth = params->freq->bandwidth;
3394 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003395 }
3396 return ret;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003397fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003398 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003399 return -ENOBUFS;
3400}
3401
3402
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003403static int nl80211_put_freq_params(struct nl_msg *msg,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003404 const struct hostapd_freq_params *freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003405{
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003406 wpa_printf(MSG_DEBUG, " * freq=%d", freq->freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003407 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq->freq))
3408 return -ENOBUFS;
3409
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003410 wpa_printf(MSG_DEBUG, " * vht_enabled=%d", freq->vht_enabled);
3411 wpa_printf(MSG_DEBUG, " * ht_enabled=%d", freq->ht_enabled);
3412
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003413 if (freq->vht_enabled) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003414 enum nl80211_chan_width cw;
3415
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003416 wpa_printf(MSG_DEBUG, " * bandwidth=%d", freq->bandwidth);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003417 switch (freq->bandwidth) {
3418 case 20:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003419 cw = NL80211_CHAN_WIDTH_20;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003420 break;
3421 case 40:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003422 cw = NL80211_CHAN_WIDTH_40;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003423 break;
3424 case 80:
3425 if (freq->center_freq2)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003426 cw = NL80211_CHAN_WIDTH_80P80;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003427 else
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003428 cw = NL80211_CHAN_WIDTH_80;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003429 break;
3430 case 160:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003431 cw = NL80211_CHAN_WIDTH_160;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003432 break;
3433 default:
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003434 return -EINVAL;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003435 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003436
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003437 wpa_printf(MSG_DEBUG, " * channel_width=%d", cw);
3438 wpa_printf(MSG_DEBUG, " * center_freq1=%d",
3439 freq->center_freq1);
3440 wpa_printf(MSG_DEBUG, " * center_freq2=%d",
3441 freq->center_freq2);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003442 if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, cw) ||
3443 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1,
3444 freq->center_freq1) ||
3445 (freq->center_freq2 &&
3446 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2,
3447 freq->center_freq2)))
3448 return -ENOBUFS;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003449 } else if (freq->ht_enabled) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003450 enum nl80211_channel_type ct;
3451
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003452 wpa_printf(MSG_DEBUG, " * sec_channel_offset=%d",
3453 freq->sec_channel_offset);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003454 switch (freq->sec_channel_offset) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003455 case -1:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003456 ct = NL80211_CHAN_HT40MINUS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003457 break;
3458 case 1:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003459 ct = NL80211_CHAN_HT40PLUS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003460 break;
3461 default:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003462 ct = NL80211_CHAN_HT20;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003463 break;
3464 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003465
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003466 wpa_printf(MSG_DEBUG, " * channel_type=%d", ct);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003467 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, ct))
3468 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003469 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003470 return 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003471}
3472
3473
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07003474static int nl80211_set_channel(struct i802_bss *bss,
3475 struct hostapd_freq_params *freq, int set_chan)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003476{
3477 struct wpa_driver_nl80211_data *drv = bss->drv;
3478 struct nl_msg *msg;
3479 int ret;
3480
3481 wpa_printf(MSG_DEBUG,
3482 "nl80211: Set freq %d (ht_enabled=%d, vht_enabled=%d, bandwidth=%d MHz, cf1=%d MHz, cf2=%d MHz)",
3483 freq->freq, freq->ht_enabled, freq->vht_enabled,
3484 freq->bandwidth, freq->center_freq1, freq->center_freq2);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003485
3486 msg = nl80211_drv_msg(drv, 0, set_chan ? NL80211_CMD_SET_CHANNEL :
3487 NL80211_CMD_SET_WIPHY);
3488 if (!msg || nl80211_put_freq_params(msg, freq) < 0) {
3489 nlmsg_free(msg);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003490 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003491 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003492
3493 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003494 if (ret == 0) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003495 bss->freq = freq->freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003496 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003497 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003498 wpa_printf(MSG_DEBUG, "nl80211: Failed to set channel (freq=%d): "
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003499 "%d (%s)", freq->freq, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003500 return -1;
3501}
3502
3503
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003504static u32 sta_flags_nl80211(int flags)
3505{
3506 u32 f = 0;
3507
3508 if (flags & WPA_STA_AUTHORIZED)
3509 f |= BIT(NL80211_STA_FLAG_AUTHORIZED);
3510 if (flags & WPA_STA_WMM)
3511 f |= BIT(NL80211_STA_FLAG_WME);
3512 if (flags & WPA_STA_SHORT_PREAMBLE)
3513 f |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
3514 if (flags & WPA_STA_MFP)
3515 f |= BIT(NL80211_STA_FLAG_MFP);
3516 if (flags & WPA_STA_TDLS_PEER)
3517 f |= BIT(NL80211_STA_FLAG_TDLS_PEER);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003518 if (flags & WPA_STA_AUTHENTICATED)
3519 f |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003520
3521 return f;
3522}
3523
3524
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003525#ifdef CONFIG_MESH
3526static u32 sta_plink_state_nl80211(enum mesh_plink_state state)
3527{
3528 switch (state) {
3529 case PLINK_LISTEN:
3530 return NL80211_PLINK_LISTEN;
3531 case PLINK_OPEN_SENT:
3532 return NL80211_PLINK_OPN_SNT;
3533 case PLINK_OPEN_RCVD:
3534 return NL80211_PLINK_OPN_RCVD;
3535 case PLINK_CNF_RCVD:
3536 return NL80211_PLINK_CNF_RCVD;
3537 case PLINK_ESTAB:
3538 return NL80211_PLINK_ESTAB;
3539 case PLINK_HOLDING:
3540 return NL80211_PLINK_HOLDING;
3541 case PLINK_BLOCKED:
3542 return NL80211_PLINK_BLOCKED;
3543 default:
3544 wpa_printf(MSG_ERROR, "nl80211: Invalid mesh plink state %d",
3545 state);
3546 }
3547 return -1;
3548}
3549#endif /* CONFIG_MESH */
3550
3551
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003552static int wpa_driver_nl80211_sta_add(void *priv,
3553 struct hostapd_sta_add_params *params)
3554{
3555 struct i802_bss *bss = priv;
3556 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003557 struct nl_msg *msg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003558 struct nl80211_sta_flag_update upd;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003559 int ret = -ENOBUFS;
3560
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003561 if ((params->flags & WPA_STA_TDLS_PEER) &&
3562 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
3563 return -EOPNOTSUPP;
3564
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003565 wpa_printf(MSG_DEBUG, "nl80211: %s STA " MACSTR,
3566 params->set ? "Set" : "Add", MAC2STR(params->addr));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003567 msg = nl80211_bss_msg(bss, 0, params->set ? NL80211_CMD_SET_STATION :
3568 NL80211_CMD_NEW_STATION);
3569 if (!msg || nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr))
3570 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003571
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003572 if (!params->set || (params->flags & WPA_STA_TDLS_PEER)) {
3573 wpa_hexdump(MSG_DEBUG, " * supported rates",
3574 params->supp_rates, params->supp_rates_len);
3575 wpa_printf(MSG_DEBUG, " * capability=0x%x",
3576 params->capability);
3577 if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_RATES,
3578 params->supp_rates_len, params->supp_rates) ||
3579 nla_put_u16(msg, NL80211_ATTR_STA_CAPABILITY,
3580 params->capability))
3581 goto fail;
3582
3583 if (params->ht_capabilities) {
3584 wpa_hexdump(MSG_DEBUG, " * ht_capabilities",
3585 (u8 *) params->ht_capabilities,
3586 sizeof(*params->ht_capabilities));
3587 if (nla_put(msg, NL80211_ATTR_HT_CAPABILITY,
3588 sizeof(*params->ht_capabilities),
3589 params->ht_capabilities))
3590 goto fail;
3591 }
3592
3593 if (params->vht_capabilities) {
3594 wpa_hexdump(MSG_DEBUG, " * vht_capabilities",
3595 (u8 *) params->vht_capabilities,
3596 sizeof(*params->vht_capabilities));
3597 if (nla_put(msg, NL80211_ATTR_VHT_CAPABILITY,
3598 sizeof(*params->vht_capabilities),
3599 params->vht_capabilities))
3600 goto fail;
3601 }
3602
3603 if (params->ext_capab) {
3604 wpa_hexdump(MSG_DEBUG, " * ext_capab",
3605 params->ext_capab, params->ext_capab_len);
3606 if (nla_put(msg, NL80211_ATTR_STA_EXT_CAPABILITY,
3607 params->ext_capab_len, params->ext_capab))
3608 goto fail;
3609 }
3610 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003611 if (!params->set) {
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07003612 if (params->aid) {
3613 wpa_printf(MSG_DEBUG, " * aid=%u", params->aid);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003614 if (nla_put_u16(msg, NL80211_ATTR_STA_AID, params->aid))
3615 goto fail;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07003616 } else {
3617 /*
3618 * cfg80211 validates that AID is non-zero, so we have
3619 * to make this a non-zero value for the TDLS case where
3620 * a dummy STA entry is used for now.
3621 */
3622 wpa_printf(MSG_DEBUG, " * aid=1 (TDLS workaround)");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003623 if (nla_put_u16(msg, NL80211_ATTR_STA_AID, 1))
3624 goto fail;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07003625 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003626 wpa_printf(MSG_DEBUG, " * listen_interval=%u",
3627 params->listen_interval);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003628 if (nla_put_u16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
3629 params->listen_interval))
3630 goto fail;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003631 } else if (params->aid && (params->flags & WPA_STA_TDLS_PEER)) {
3632 wpa_printf(MSG_DEBUG, " * peer_aid=%u", params->aid);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003633 if (nla_put_u16(msg, NL80211_ATTR_PEER_AID, params->aid))
3634 goto fail;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003635 }
3636
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08003637 if (params->vht_opmode_enabled) {
3638 wpa_printf(MSG_DEBUG, " * opmode=%u", params->vht_opmode);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003639 if (nla_put_u8(msg, NL80211_ATTR_OPMODE_NOTIF,
3640 params->vht_opmode))
3641 goto fail;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003642 }
3643
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003644 if (params->supp_channels) {
3645 wpa_hexdump(MSG_DEBUG, " * supported channels",
3646 params->supp_channels, params->supp_channels_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003647 if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_CHANNELS,
3648 params->supp_channels_len, params->supp_channels))
3649 goto fail;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003650 }
3651
3652 if (params->supp_oper_classes) {
3653 wpa_hexdump(MSG_DEBUG, " * supported operating classes",
3654 params->supp_oper_classes,
3655 params->supp_oper_classes_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003656 if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES,
3657 params->supp_oper_classes_len,
3658 params->supp_oper_classes))
3659 goto fail;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003660 }
3661
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003662 os_memset(&upd, 0, sizeof(upd));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003663 upd.set = sta_flags_nl80211(params->flags);
3664 upd.mask = upd.set | sta_flags_nl80211(params->flags_mask);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003665 wpa_printf(MSG_DEBUG, " * flags set=0x%x mask=0x%x",
3666 upd.set, upd.mask);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003667 if (nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd))
3668 goto fail;
3669
3670#ifdef CONFIG_MESH
3671 if (params->plink_state &&
3672 nla_put_u8(msg, NL80211_ATTR_STA_PLINK_STATE,
3673 sta_plink_state_nl80211(params->plink_state)))
3674 goto fail;
3675#endif /* CONFIG_MESH */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003676
3677 if (params->flags & WPA_STA_WMM) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003678 struct nlattr *wme = nla_nest_start(msg, NL80211_ATTR_STA_WME);
3679
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003680 wpa_printf(MSG_DEBUG, " * qosinfo=0x%x", params->qosinfo);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003681 if (!wme ||
3682 nla_put_u8(msg, NL80211_STA_WME_UAPSD_QUEUES,
3683 params->qosinfo & WMM_QOSINFO_STA_AC_MASK) ||
3684 nla_put_u8(msg, NL80211_STA_WME_MAX_SP,
3685 (params->qosinfo >> WMM_QOSINFO_STA_SP_SHIFT) &
3686 WMM_QOSINFO_STA_SP_MASK))
3687 goto fail;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003688 nla_nest_end(msg, wme);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003689 }
3690
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003691 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003692 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003693 if (ret)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003694 wpa_printf(MSG_DEBUG, "nl80211: NL80211_CMD_%s_STATION "
3695 "result: %d (%s)", params->set ? "SET" : "NEW", ret,
3696 strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003697 if (ret == -EEXIST)
3698 ret = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003699fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003700 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003701 return ret;
3702}
3703
3704
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003705static void rtnl_neigh_delete_fdb_entry(struct i802_bss *bss, const u8 *addr)
3706{
3707#ifdef CONFIG_LIBNL3_ROUTE
3708 struct wpa_driver_nl80211_data *drv = bss->drv;
3709 struct rtnl_neigh *rn;
3710 struct nl_addr *nl_addr;
3711 int err;
3712
3713 rn = rtnl_neigh_alloc();
3714 if (!rn)
3715 return;
3716
3717 rtnl_neigh_set_family(rn, AF_BRIDGE);
3718 rtnl_neigh_set_ifindex(rn, bss->ifindex);
3719 nl_addr = nl_addr_build(AF_BRIDGE, (void *) addr, ETH_ALEN);
3720 if (!nl_addr) {
3721 rtnl_neigh_put(rn);
3722 return;
3723 }
3724 rtnl_neigh_set_lladdr(rn, nl_addr);
3725
3726 err = rtnl_neigh_delete(drv->rtnl_sk, rn, 0);
3727 if (err < 0) {
3728 wpa_printf(MSG_DEBUG, "nl80211: bridge FDB entry delete for "
3729 MACSTR " ifindex=%d failed: %s", MAC2STR(addr),
3730 bss->ifindex, nl_geterror(err));
3731 } else {
3732 wpa_printf(MSG_DEBUG, "nl80211: deleted bridge FDB entry for "
3733 MACSTR, MAC2STR(addr));
3734 }
3735
3736 nl_addr_put(nl_addr);
3737 rtnl_neigh_put(rn);
3738#endif /* CONFIG_LIBNL3_ROUTE */
3739}
3740
3741
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003742static int wpa_driver_nl80211_sta_remove(struct i802_bss *bss, const u8 *addr,
3743 int deauth, u16 reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003744{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003745 struct wpa_driver_nl80211_data *drv = bss->drv;
3746 struct nl_msg *msg;
3747 int ret;
3748
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003749 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_DEL_STATION)) ||
3750 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
3751 (deauth == 0 &&
3752 nla_put_u8(msg, NL80211_ATTR_MGMT_SUBTYPE,
3753 WLAN_FC_STYPE_DISASSOC)) ||
3754 (deauth == 1 &&
3755 nla_put_u8(msg, NL80211_ATTR_MGMT_SUBTYPE,
3756 WLAN_FC_STYPE_DEAUTH)) ||
3757 (reason_code &&
3758 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code))) {
3759 nlmsg_free(msg);
3760 return -ENOBUFS;
3761 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003762
3763 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07003764 wpa_printf(MSG_DEBUG, "nl80211: sta_remove -> DEL_STATION %s " MACSTR
3765 " --> %d (%s)",
3766 bss->ifname, MAC2STR(addr), ret, strerror(-ret));
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003767
3768 if (drv->rtnl_sk)
3769 rtnl_neigh_delete_fdb_entry(bss, addr);
3770
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003771 if (ret == -ENOENT)
3772 return 0;
3773 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003774}
3775
3776
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003777void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv, int ifidx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003778{
3779 struct nl_msg *msg;
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07003780 struct wpa_driver_nl80211_data *drv2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003781
3782 wpa_printf(MSG_DEBUG, "nl80211: Remove interface ifindex=%d", ifidx);
3783
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003784 /* stop listening for EAPOL on this interface */
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07003785 dl_list_for_each(drv2, &drv->global->interfaces,
3786 struct wpa_driver_nl80211_data, list)
3787 del_ifidx(drv2, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003788
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003789 msg = nl80211_ifindex_msg(drv, ifidx, 0, NL80211_CMD_DEL_INTERFACE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003790 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
3791 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003792 wpa_printf(MSG_ERROR, "Failed to remove interface (ifidx=%d)", ifidx);
3793}
3794
3795
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003796static const char * nl80211_iftype_str(enum nl80211_iftype mode)
3797{
3798 switch (mode) {
3799 case NL80211_IFTYPE_ADHOC:
3800 return "ADHOC";
3801 case NL80211_IFTYPE_STATION:
3802 return "STATION";
3803 case NL80211_IFTYPE_AP:
3804 return "AP";
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003805 case NL80211_IFTYPE_AP_VLAN:
3806 return "AP_VLAN";
3807 case NL80211_IFTYPE_WDS:
3808 return "WDS";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003809 case NL80211_IFTYPE_MONITOR:
3810 return "MONITOR";
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003811 case NL80211_IFTYPE_MESH_POINT:
3812 return "MESH_POINT";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003813 case NL80211_IFTYPE_P2P_CLIENT:
3814 return "P2P_CLIENT";
3815 case NL80211_IFTYPE_P2P_GO:
3816 return "P2P_GO";
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003817 case NL80211_IFTYPE_P2P_DEVICE:
3818 return "P2P_DEVICE";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003819 default:
3820 return "unknown";
3821 }
3822}
3823
3824
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003825static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv,
3826 const char *ifname,
3827 enum nl80211_iftype iftype,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003828 const u8 *addr, int wds,
3829 int (*handler)(struct nl_msg *, void *),
3830 void *arg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003831{
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003832 struct nl_msg *msg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003833 int ifidx;
3834 int ret = -ENOBUFS;
3835
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003836 wpa_printf(MSG_DEBUG, "nl80211: Create interface iftype %d (%s)",
3837 iftype, nl80211_iftype_str(iftype));
3838
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003839 msg = nl80211_cmd_msg(drv->first_bss, 0, NL80211_CMD_NEW_INTERFACE);
3840 if (!msg ||
3841 nla_put_string(msg, NL80211_ATTR_IFNAME, ifname) ||
3842 nla_put_u32(msg, NL80211_ATTR_IFTYPE, iftype))
3843 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003844
3845 if (iftype == NL80211_IFTYPE_MONITOR) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003846 struct nlattr *flags;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003847
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003848 flags = nla_nest_start(msg, NL80211_ATTR_MNTR_FLAGS);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003849 if (!flags ||
3850 nla_put_flag(msg, NL80211_MNTR_FLAG_COOK_FRAMES))
3851 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003852
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003853 nla_nest_end(msg, flags);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003854 } else if (wds) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003855 if (nla_put_u8(msg, NL80211_ATTR_4ADDR, wds))
3856 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003857 }
3858
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07003859 /*
3860 * Tell cfg80211 that the interface belongs to the socket that created
3861 * it, and the interface should be deleted when the socket is closed.
3862 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003863 if (nla_put_flag(msg, NL80211_ATTR_IFACE_SOCKET_OWNER))
3864 goto fail;
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07003865
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003866 ret = send_and_recv_msgs(drv, msg, handler, arg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003867 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003868 if (ret) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003869 fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003870 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003871 wpa_printf(MSG_ERROR, "Failed to create interface %s: %d (%s)",
3872 ifname, ret, strerror(-ret));
3873 return ret;
3874 }
3875
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003876 if (iftype == NL80211_IFTYPE_P2P_DEVICE)
3877 return 0;
3878
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003879 ifidx = if_nametoindex(ifname);
3880 wpa_printf(MSG_DEBUG, "nl80211: New interface %s created: ifindex=%d",
3881 ifname, ifidx);
3882
3883 if (ifidx <= 0)
3884 return -1;
3885
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07003886 /*
3887 * Some virtual interfaces need to process EAPOL packets and events on
3888 * the parent interface. This is used mainly with hostapd.
3889 */
3890 if (drv->hostapd ||
3891 iftype == NL80211_IFTYPE_AP_VLAN ||
3892 iftype == NL80211_IFTYPE_WDS ||
3893 iftype == NL80211_IFTYPE_MONITOR) {
3894 /* start listening for EAPOL on this interface */
3895 add_ifidx(drv, ifidx);
3896 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003897
3898 if (addr && iftype != NL80211_IFTYPE_MONITOR &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003899 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname, addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003900 nl80211_remove_iface(drv, ifidx);
3901 return -1;
3902 }
3903
3904 return ifidx;
3905}
3906
3907
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003908int nl80211_create_iface(struct wpa_driver_nl80211_data *drv,
3909 const char *ifname, enum nl80211_iftype iftype,
3910 const u8 *addr, int wds,
3911 int (*handler)(struct nl_msg *, void *),
3912 void *arg, int use_existing)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003913{
3914 int ret;
3915
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003916 ret = nl80211_create_iface_once(drv, ifname, iftype, addr, wds, handler,
3917 arg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003918
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003919 /* if error occurred and interface exists already */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003920 if (ret == -ENFILE && if_nametoindex(ifname)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003921 if (use_existing) {
3922 wpa_printf(MSG_DEBUG, "nl80211: Continue using existing interface %s",
3923 ifname);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07003924 if (addr && iftype != NL80211_IFTYPE_MONITOR &&
3925 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
3926 addr) < 0 &&
3927 (linux_set_iface_flags(drv->global->ioctl_sock,
3928 ifname, 0) < 0 ||
3929 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
3930 addr) < 0 ||
3931 linux_set_iface_flags(drv->global->ioctl_sock,
3932 ifname, 1) < 0))
3933 return -1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003934 return -ENFILE;
3935 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003936 wpa_printf(MSG_INFO, "Try to remove and re-create %s", ifname);
3937
3938 /* Try to remove the interface that was already there. */
3939 nl80211_remove_iface(drv, if_nametoindex(ifname));
3940
3941 /* Try to create the interface again */
3942 ret = nl80211_create_iface_once(drv, ifname, iftype, addr,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003943 wds, handler, arg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003944 }
3945
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003946 if (ret >= 0 && is_p2p_net_interface(iftype)) {
3947 wpa_printf(MSG_DEBUG,
3948 "nl80211: Interface %s created for P2P - disable 11b rates",
3949 ifname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003950 nl80211_disable_11b_rates(drv, ret, 1);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003951 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003952
3953 return ret;
3954}
3955
3956
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003957static int nl80211_setup_ap(struct i802_bss *bss)
3958{
3959 struct wpa_driver_nl80211_data *drv = bss->drv;
3960
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003961 wpa_printf(MSG_DEBUG, "nl80211: Setup AP(%s) - device_ap_sme=%d use_monitor=%d",
3962 bss->ifname, drv->device_ap_sme, drv->use_monitor);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003963
3964 /*
3965 * Disable Probe Request reporting unless we need it in this way for
3966 * devices that include the AP SME, in the other case (unless using
3967 * monitor iface) we'll get it through the nl_mgmt socket instead.
3968 */
3969 if (!drv->device_ap_sme)
3970 wpa_driver_nl80211_probe_req_report(bss, 0);
3971
3972 if (!drv->device_ap_sme && !drv->use_monitor)
3973 if (nl80211_mgmt_subscribe_ap(bss))
3974 return -1;
3975
3976 if (drv->device_ap_sme && !drv->use_monitor)
3977 if (nl80211_mgmt_subscribe_ap_dev_sme(bss))
3978 return -1;
3979
3980 if (!drv->device_ap_sme && drv->use_monitor &&
3981 nl80211_create_monitor_interface(drv) &&
3982 !drv->device_ap_sme)
Dmitry Shmidt04949592012-07-19 12:16:46 -07003983 return -1;
3984
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003985 if (drv->device_ap_sme &&
3986 wpa_driver_nl80211_probe_req_report(bss, 1) < 0) {
3987 wpa_printf(MSG_DEBUG, "nl80211: Failed to enable "
3988 "Probe Request frame reporting in AP mode");
3989 /* Try to survive without this */
3990 }
3991
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003992 return 0;
3993}
3994
3995
3996static void nl80211_teardown_ap(struct i802_bss *bss)
3997{
3998 struct wpa_driver_nl80211_data *drv = bss->drv;
3999
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004000 wpa_printf(MSG_DEBUG, "nl80211: Teardown AP(%s) - device_ap_sme=%d use_monitor=%d",
4001 bss->ifname, drv->device_ap_sme, drv->use_monitor);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004002 if (drv->device_ap_sme) {
4003 wpa_driver_nl80211_probe_req_report(bss, 0);
4004 if (!drv->use_monitor)
4005 nl80211_mgmt_unsubscribe(bss, "AP teardown (dev SME)");
4006 } else if (drv->use_monitor)
4007 nl80211_remove_monitor_interface(drv);
4008 else
4009 nl80211_mgmt_unsubscribe(bss, "AP teardown");
4010
4011 bss->beacon_set = 0;
4012}
4013
4014
4015static int nl80211_send_eapol_data(struct i802_bss *bss,
4016 const u8 *addr, const u8 *data,
4017 size_t data_len)
4018{
4019 struct sockaddr_ll ll;
4020 int ret;
4021
4022 if (bss->drv->eapol_tx_sock < 0) {
4023 wpa_printf(MSG_DEBUG, "nl80211: No socket to send EAPOL");
4024 return -1;
4025 }
4026
4027 os_memset(&ll, 0, sizeof(ll));
4028 ll.sll_family = AF_PACKET;
4029 ll.sll_ifindex = bss->ifindex;
4030 ll.sll_protocol = htons(ETH_P_PAE);
4031 ll.sll_halen = ETH_ALEN;
4032 os_memcpy(ll.sll_addr, addr, ETH_ALEN);
4033 ret = sendto(bss->drv->eapol_tx_sock, data, data_len, 0,
4034 (struct sockaddr *) &ll, sizeof(ll));
4035 if (ret < 0)
4036 wpa_printf(MSG_ERROR, "nl80211: EAPOL TX: %s",
4037 strerror(errno));
4038
4039 return ret;
4040}
4041
4042
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004043static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
4044
4045static int wpa_driver_nl80211_hapd_send_eapol(
4046 void *priv, const u8 *addr, const u8 *data,
4047 size_t data_len, int encrypt, const u8 *own_addr, u32 flags)
4048{
4049 struct i802_bss *bss = priv;
4050 struct wpa_driver_nl80211_data *drv = bss->drv;
4051 struct ieee80211_hdr *hdr;
4052 size_t len;
4053 u8 *pos;
4054 int res;
4055 int qos = flags & WPA_STA_WMM;
Dmitry Shmidt641185e2013-11-06 15:17:13 -08004056
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004057 if (drv->device_ap_sme || !drv->use_monitor)
4058 return nl80211_send_eapol_data(bss, addr, data, data_len);
4059
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004060 len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
4061 data_len;
4062 hdr = os_zalloc(len);
4063 if (hdr == NULL) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004064 wpa_printf(MSG_INFO, "nl80211: Failed to allocate EAPOL buffer(len=%lu)",
4065 (unsigned long) len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004066 return -1;
4067 }
4068
4069 hdr->frame_control =
4070 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
4071 hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
4072 if (encrypt)
4073 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
4074 if (qos) {
4075 hdr->frame_control |=
4076 host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
4077 }
4078
4079 memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
4080 memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
4081 memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
4082 pos = (u8 *) (hdr + 1);
4083
4084 if (qos) {
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004085 /* Set highest priority in QoS header */
4086 pos[0] = 7;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004087 pos[1] = 0;
4088 pos += 2;
4089 }
4090
4091 memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
4092 pos += sizeof(rfc1042_header);
4093 WPA_PUT_BE16(pos, ETH_P_PAE);
4094 pos += 2;
4095 memcpy(pos, data, data_len);
4096
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004097 res = wpa_driver_nl80211_send_frame(bss, (u8 *) hdr, len, encrypt, 0,
4098 0, 0, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004099 if (res < 0) {
4100 wpa_printf(MSG_ERROR, "i802_send_eapol - packet len: %lu - "
4101 "failed: %d (%s)",
4102 (unsigned long) len, errno, strerror(errno));
4103 }
4104 os_free(hdr);
4105
4106 return res;
4107}
4108
4109
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004110static int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr,
4111 int total_flags,
4112 int flags_or, int flags_and)
4113{
4114 struct i802_bss *bss = priv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004115 struct nl_msg *msg;
4116 struct nlattr *flags;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004117 struct nl80211_sta_flag_update upd;
4118
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004119 wpa_printf(MSG_DEBUG, "nl80211: Set STA flags - ifname=%s addr=" MACSTR
4120 " total_flags=0x%x flags_or=0x%x flags_and=0x%x authorized=%d",
4121 bss->ifname, MAC2STR(addr), total_flags, flags_or, flags_and,
4122 !!(total_flags & WPA_STA_AUTHORIZED));
4123
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004124 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
4125 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
4126 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004127
4128 /*
4129 * Backwards compatibility version using NL80211_ATTR_STA_FLAGS. This
4130 * can be removed eventually.
4131 */
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004132 flags = nla_nest_start(msg, NL80211_ATTR_STA_FLAGS);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004133 if (!flags ||
4134 ((total_flags & WPA_STA_AUTHORIZED) &&
4135 nla_put_flag(msg, NL80211_STA_FLAG_AUTHORIZED)) ||
4136 ((total_flags & WPA_STA_WMM) &&
4137 nla_put_flag(msg, NL80211_STA_FLAG_WME)) ||
4138 ((total_flags & WPA_STA_SHORT_PREAMBLE) &&
4139 nla_put_flag(msg, NL80211_STA_FLAG_SHORT_PREAMBLE)) ||
4140 ((total_flags & WPA_STA_MFP) &&
4141 nla_put_flag(msg, NL80211_STA_FLAG_MFP)) ||
4142 ((total_flags & WPA_STA_TDLS_PEER) &&
4143 nla_put_flag(msg, NL80211_STA_FLAG_TDLS_PEER)))
4144 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004145
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004146 nla_nest_end(msg, flags);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004147
4148 os_memset(&upd, 0, sizeof(upd));
4149 upd.mask = sta_flags_nl80211(flags_or | ~flags_and);
4150 upd.set = sta_flags_nl80211(flags_or);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004151 if (nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd))
4152 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004153
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004154 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
4155fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004156 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004157 return -ENOBUFS;
4158}
4159
4160
4161static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv,
4162 struct wpa_driver_associate_params *params)
4163{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004164 enum nl80211_iftype nlmode, old_mode;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004165
4166 if (params->p2p) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004167 wpa_printf(MSG_DEBUG, "nl80211: Setup AP operations for P2P "
4168 "group (GO)");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004169 nlmode = NL80211_IFTYPE_P2P_GO;
4170 } else
4171 nlmode = NL80211_IFTYPE_AP;
4172
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004173 old_mode = drv->nlmode;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004174 if (wpa_driver_nl80211_set_mode(drv->first_bss, nlmode)) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004175 nl80211_remove_monitor_interface(drv);
4176 return -1;
4177 }
4178
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004179 if (nl80211_set_channel(drv->first_bss, &params->freq, 0)) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004180 if (old_mode != nlmode)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004181 wpa_driver_nl80211_set_mode(drv->first_bss, old_mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004182 nl80211_remove_monitor_interface(drv);
4183 return -1;
4184 }
4185
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004186 return 0;
4187}
4188
4189
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004190static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv,
4191 int reset_mode)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004192{
4193 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004194 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004195
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004196 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_LEAVE_IBSS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004197 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004198 if (ret) {
4199 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS failed: ret=%d "
4200 "(%s)", ret, strerror(-ret));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004201 } else {
4202 wpa_printf(MSG_DEBUG,
4203 "nl80211: Leave IBSS request sent successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004204 }
4205
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004206 if (reset_mode &&
4207 wpa_driver_nl80211_set_mode(drv->first_bss,
Dmitry Shmidt56052862013-10-04 10:23:25 -07004208 NL80211_IFTYPE_STATION)) {
4209 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
4210 "station mode");
4211 }
4212
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004213 return ret;
4214}
4215
4216
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08004217static int nl80211_ht_vht_overrides(struct nl_msg *msg,
4218 struct wpa_driver_associate_params *params)
4219{
4220 if (params->disable_ht && nla_put_flag(msg, NL80211_ATTR_DISABLE_HT))
4221 return -1;
4222
4223 if (params->htcaps && params->htcaps_mask) {
4224 int sz = sizeof(struct ieee80211_ht_capabilities);
4225 wpa_hexdump(MSG_DEBUG, " * htcaps", params->htcaps, sz);
4226 wpa_hexdump(MSG_DEBUG, " * htcaps_mask",
4227 params->htcaps_mask, sz);
4228 if (nla_put(msg, NL80211_ATTR_HT_CAPABILITY, sz,
4229 params->htcaps) ||
4230 nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK, sz,
4231 params->htcaps_mask))
4232 return -1;
4233 }
4234
4235#ifdef CONFIG_VHT_OVERRIDES
4236 if (params->disable_vht) {
4237 wpa_printf(MSG_DEBUG, " * VHT disabled");
4238 if (nla_put_flag(msg, NL80211_ATTR_DISABLE_VHT))
4239 return -1;
4240 }
4241
4242 if (params->vhtcaps && params->vhtcaps_mask) {
4243 int sz = sizeof(struct ieee80211_vht_capabilities);
4244 wpa_hexdump(MSG_DEBUG, " * vhtcaps", params->vhtcaps, sz);
4245 wpa_hexdump(MSG_DEBUG, " * vhtcaps_mask",
4246 params->vhtcaps_mask, sz);
4247 if (nla_put(msg, NL80211_ATTR_VHT_CAPABILITY, sz,
4248 params->vhtcaps) ||
4249 nla_put(msg, NL80211_ATTR_VHT_CAPABILITY_MASK, sz,
4250 params->vhtcaps_mask))
4251 return -1;
4252 }
4253#endif /* CONFIG_VHT_OVERRIDES */
4254
4255 return 0;
4256}
4257
4258
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004259static int wpa_driver_nl80211_ibss(struct wpa_driver_nl80211_data *drv,
4260 struct wpa_driver_associate_params *params)
4261{
4262 struct nl_msg *msg;
4263 int ret = -1;
4264 int count = 0;
4265
4266 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS (ifindex=%d)", drv->ifindex);
4267
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004268 if (wpa_driver_nl80211_set_mode_ibss(drv->first_bss, &params->freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004269 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
4270 "IBSS mode");
4271 return -1;
4272 }
4273
4274retry:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004275 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_JOIN_IBSS)) ||
4276 params->ssid == NULL || params->ssid_len > sizeof(drv->ssid))
4277 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004278
4279 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
4280 params->ssid, params->ssid_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004281 if (nla_put(msg, NL80211_ATTR_SSID, params->ssid_len, params->ssid))
4282 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004283 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
4284 drv->ssid_len = params->ssid_len;
4285
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004286 if (nl80211_put_freq_params(msg, &params->freq) < 0 ||
4287 nl80211_put_beacon_int(msg, params->beacon_int))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004288 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004289
4290 ret = nl80211_set_conn_keys(params, msg);
4291 if (ret)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004292 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004293
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004294 if (params->bssid && params->fixed_bssid) {
4295 wpa_printf(MSG_DEBUG, " * BSSID=" MACSTR,
4296 MAC2STR(params->bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004297 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid))
4298 goto fail;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004299 }
4300
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004301 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
4302 params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
4303 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
4304 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004305 wpa_printf(MSG_DEBUG, " * control port");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004306 if (nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT))
4307 goto fail;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004308 }
4309
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004310 if (params->wpa_ie) {
4311 wpa_hexdump(MSG_DEBUG,
4312 " * Extra IEs for Beacon/Probe Response frames",
4313 params->wpa_ie, params->wpa_ie_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004314 if (nla_put(msg, NL80211_ATTR_IE, params->wpa_ie_len,
4315 params->wpa_ie))
4316 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004317 }
4318
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08004319 if (nl80211_ht_vht_overrides(msg, params) < 0)
4320 return -1;
4321
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004322 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4323 msg = NULL;
4324 if (ret) {
4325 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS failed: ret=%d (%s)",
4326 ret, strerror(-ret));
4327 count++;
4328 if (ret == -EALREADY && count == 1) {
4329 wpa_printf(MSG_DEBUG, "nl80211: Retry IBSS join after "
4330 "forced leave");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004331 nl80211_leave_ibss(drv, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004332 nlmsg_free(msg);
4333 goto retry;
4334 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004335 } else {
4336 wpa_printf(MSG_DEBUG,
4337 "nl80211: Join IBSS request sent successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004338 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004339
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004340fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004341 nlmsg_free(msg);
4342 return ret;
4343}
4344
4345
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004346static int nl80211_connect_common(struct wpa_driver_nl80211_data *drv,
4347 struct wpa_driver_associate_params *params,
4348 struct nl_msg *msg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004349{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004350 if (params->bssid) {
4351 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
4352 MAC2STR(params->bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004353 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid))
4354 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004355 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004356
Dmitry Shmidt96be6222014-02-13 10:16:51 -08004357 if (params->bssid_hint) {
4358 wpa_printf(MSG_DEBUG, " * bssid_hint=" MACSTR,
4359 MAC2STR(params->bssid_hint));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004360 if (nla_put(msg, NL80211_ATTR_MAC_HINT, ETH_ALEN,
4361 params->bssid_hint))
4362 return -1;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08004363 }
4364
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004365 if (params->freq.freq) {
4366 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq.freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004367 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
4368 params->freq.freq))
4369 return -1;
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004370 drv->assoc_freq = params->freq.freq;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004371 } else
4372 drv->assoc_freq = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004373
Dmitry Shmidt96be6222014-02-13 10:16:51 -08004374 if (params->freq_hint) {
4375 wpa_printf(MSG_DEBUG, " * freq_hint=%d", params->freq_hint);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004376 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ_HINT,
4377 params->freq_hint))
4378 return -1;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08004379 }
4380
Dmitry Shmidt04949592012-07-19 12:16:46 -07004381 if (params->bg_scan_period >= 0) {
4382 wpa_printf(MSG_DEBUG, " * bg scan period=%d",
4383 params->bg_scan_period);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004384 if (nla_put_u16(msg, NL80211_ATTR_BG_SCAN_PERIOD,
4385 params->bg_scan_period))
4386 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004387 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004388
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004389 if (params->ssid) {
4390 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
4391 params->ssid, params->ssid_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004392 if (nla_put(msg, NL80211_ATTR_SSID, params->ssid_len,
4393 params->ssid))
4394 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004395 if (params->ssid_len > sizeof(drv->ssid))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004396 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004397 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
4398 drv->ssid_len = params->ssid_len;
4399 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004400
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004401 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004402 if (params->wpa_ie &&
4403 nla_put(msg, NL80211_ATTR_IE, params->wpa_ie_len, params->wpa_ie))
4404 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004405
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004406 if (params->wpa_proto) {
4407 enum nl80211_wpa_versions ver = 0;
4408
4409 if (params->wpa_proto & WPA_PROTO_WPA)
4410 ver |= NL80211_WPA_VERSION_1;
4411 if (params->wpa_proto & WPA_PROTO_RSN)
4412 ver |= NL80211_WPA_VERSION_2;
4413
4414 wpa_printf(MSG_DEBUG, " * WPA Versions 0x%x", ver);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004415 if (nla_put_u32(msg, NL80211_ATTR_WPA_VERSIONS, ver))
4416 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004417 }
4418
4419 if (params->pairwise_suite != WPA_CIPHER_NONE) {
4420 u32 cipher = wpa_cipher_to_cipher_suite(params->pairwise_suite);
4421 wpa_printf(MSG_DEBUG, " * pairwise=0x%x", cipher);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004422 if (nla_put_u32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
4423 cipher))
4424 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004425 }
4426
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08004427 if (params->group_suite == WPA_CIPHER_GTK_NOT_USED &&
4428 !(drv->capa.enc & WPA_DRIVER_CAPA_ENC_GTK_NOT_USED)) {
4429 /*
4430 * This is likely to work even though many drivers do not
4431 * advertise support for operations without GTK.
4432 */
4433 wpa_printf(MSG_DEBUG, " * skip group cipher configuration for GTK_NOT_USED due to missing driver support advertisement");
4434 } else if (params->group_suite != WPA_CIPHER_NONE) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004435 u32 cipher = wpa_cipher_to_cipher_suite(params->group_suite);
4436 wpa_printf(MSG_DEBUG, " * group=0x%x", cipher);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004437 if (nla_put_u32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher))
4438 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004439 }
4440
4441 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
4442 params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
4443 params->key_mgmt_suite == WPA_KEY_MGMT_FT_IEEE8021X ||
4444 params->key_mgmt_suite == WPA_KEY_MGMT_FT_PSK ||
Dmitry Shmidt15907092014-03-25 10:42:57 -07004445 params->key_mgmt_suite == WPA_KEY_MGMT_CCKM ||
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -07004446 params->key_mgmt_suite == WPA_KEY_MGMT_OSEN ||
4447 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004448 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256 ||
4449 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004450 int mgmt = WLAN_AKM_SUITE_PSK;
4451
4452 switch (params->key_mgmt_suite) {
4453 case WPA_KEY_MGMT_CCKM:
4454 mgmt = WLAN_AKM_SUITE_CCKM;
4455 break;
4456 case WPA_KEY_MGMT_IEEE8021X:
4457 mgmt = WLAN_AKM_SUITE_8021X;
4458 break;
4459 case WPA_KEY_MGMT_FT_IEEE8021X:
4460 mgmt = WLAN_AKM_SUITE_FT_8021X;
4461 break;
4462 case WPA_KEY_MGMT_FT_PSK:
4463 mgmt = WLAN_AKM_SUITE_FT_PSK;
4464 break;
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -07004465 case WPA_KEY_MGMT_IEEE8021X_SHA256:
4466 mgmt = WLAN_AKM_SUITE_8021X_SHA256;
4467 break;
4468 case WPA_KEY_MGMT_PSK_SHA256:
4469 mgmt = WLAN_AKM_SUITE_PSK_SHA256;
4470 break;
Dmitry Shmidt15907092014-03-25 10:42:57 -07004471 case WPA_KEY_MGMT_OSEN:
4472 mgmt = WLAN_AKM_SUITE_OSEN;
4473 break;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004474 case WPA_KEY_MGMT_IEEE8021X_SUITE_B:
4475 mgmt = WLAN_AKM_SUITE_8021X_SUITE_B;
4476 break;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004477 case WPA_KEY_MGMT_PSK:
4478 default:
4479 mgmt = WLAN_AKM_SUITE_PSK;
4480 break;
4481 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07004482 wpa_printf(MSG_DEBUG, " * akm=0x%x", mgmt);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004483 if (nla_put_u32(msg, NL80211_ATTR_AKM_SUITES, mgmt))
4484 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004485 }
4486
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004487 if (nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT))
4488 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004489
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004490 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED &&
4491 nla_put_u32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED))
4492 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004493
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004494 if (params->rrm_used) {
4495 u32 drv_rrm_flags = drv->capa.rrm_flags;
4496 if (!(drv_rrm_flags &
4497 WPA_DRIVER_FLAGS_DS_PARAM_SET_IE_IN_PROBES) ||
4498 !(drv_rrm_flags & WPA_DRIVER_FLAGS_QUIET) ||
4499 nla_put_flag(msg, NL80211_ATTR_USE_RRM))
4500 return -1;
4501 }
4502
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08004503 if (nl80211_ht_vht_overrides(msg, params) < 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004504 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004505
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004506 if (params->p2p)
4507 wpa_printf(MSG_DEBUG, " * P2P group");
4508
4509 return 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004510}
4511
4512
4513static int wpa_driver_nl80211_try_connect(
4514 struct wpa_driver_nl80211_data *drv,
4515 struct wpa_driver_associate_params *params)
4516{
4517 struct nl_msg *msg;
4518 enum nl80211_auth_type type;
4519 int ret;
4520 int algs;
4521
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004522 if (params->req_key_mgmt_offload && params->psk &&
4523 (params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
4524 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256 ||
4525 params->key_mgmt_suite == WPA_KEY_MGMT_FT_PSK)) {
4526 wpa_printf(MSG_DEBUG, "nl80211: Key management set PSK");
4527 ret = issue_key_mgmt_set_key(drv, params->psk, 32);
4528 if (ret)
4529 return ret;
4530 }
4531
4532 wpa_printf(MSG_DEBUG, "nl80211: Connect (ifindex=%d)", drv->ifindex);
4533 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_CONNECT);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004534 if (!msg)
4535 return -1;
4536
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004537 ret = nl80211_connect_common(drv, params, msg);
4538 if (ret)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004539 goto fail;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004540
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004541 algs = 0;
4542 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
4543 algs++;
4544 if (params->auth_alg & WPA_AUTH_ALG_SHARED)
4545 algs++;
4546 if (params->auth_alg & WPA_AUTH_ALG_LEAP)
4547 algs++;
4548 if (algs > 1) {
4549 wpa_printf(MSG_DEBUG, " * Leave out Auth Type for automatic "
4550 "selection");
4551 goto skip_auth_type;
4552 }
4553
4554 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
4555 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
4556 else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
4557 type = NL80211_AUTHTYPE_SHARED_KEY;
4558 else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
4559 type = NL80211_AUTHTYPE_NETWORK_EAP;
4560 else if (params->auth_alg & WPA_AUTH_ALG_FT)
4561 type = NL80211_AUTHTYPE_FT;
4562 else
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004563 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004564
4565 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004566 if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE, type))
4567 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004568
4569skip_auth_type:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004570 ret = nl80211_set_conn_keys(params, msg);
4571 if (ret)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004572 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004573
4574 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4575 msg = NULL;
4576 if (ret) {
4577 wpa_printf(MSG_DEBUG, "nl80211: MLME connect failed: ret=%d "
4578 "(%s)", ret, strerror(-ret));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004579 } else {
4580 wpa_printf(MSG_DEBUG,
4581 "nl80211: Connect request send successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004582 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004583
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004584fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004585 nlmsg_free(msg);
4586 return ret;
4587
4588}
4589
4590
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004591static int wpa_driver_nl80211_connect(
4592 struct wpa_driver_nl80211_data *drv,
4593 struct wpa_driver_associate_params *params)
4594{
Jithu Jancea7c60b42014-12-03 18:54:40 +05304595 int ret;
4596
4597 /* Store the connection attempted bssid for future use */
4598 if (params->bssid)
4599 os_memcpy(drv->auth_attempt_bssid, params->bssid, ETH_ALEN);
4600 else
4601 os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
4602
4603 ret = wpa_driver_nl80211_try_connect(drv, params);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004604 if (ret == -EALREADY) {
4605 /*
4606 * cfg80211 does not currently accept new connections if
4607 * we are already connected. As a workaround, force
4608 * disconnection and try again.
4609 */
4610 wpa_printf(MSG_DEBUG, "nl80211: Explicitly "
4611 "disconnecting before reassociation "
4612 "attempt");
4613 if (wpa_driver_nl80211_disconnect(
4614 drv, WLAN_REASON_PREV_AUTH_NOT_VALID))
4615 return -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004616 ret = wpa_driver_nl80211_try_connect(drv, params);
4617 }
4618 return ret;
4619}
4620
4621
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004622static int wpa_driver_nl80211_associate(
4623 void *priv, struct wpa_driver_associate_params *params)
4624{
4625 struct i802_bss *bss = priv;
4626 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004627 int ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004628 struct nl_msg *msg;
4629
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004630 nl80211_unmask_11b_rates(bss);
4631
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004632 if (params->mode == IEEE80211_MODE_AP)
4633 return wpa_driver_nl80211_ap(drv, params);
4634
4635 if (params->mode == IEEE80211_MODE_IBSS)
4636 return wpa_driver_nl80211_ibss(drv, params);
4637
4638 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004639 enum nl80211_iftype nlmode = params->p2p ?
4640 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
4641
4642 if (wpa_driver_nl80211_set_mode(priv, nlmode) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004643 return -1;
4644 return wpa_driver_nl80211_connect(drv, params);
4645 }
4646
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004647 nl80211_mark_disconnected(drv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004648
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004649 wpa_printf(MSG_DEBUG, "nl80211: Associate (ifindex=%d)",
4650 drv->ifindex);
4651 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_ASSOCIATE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004652 if (!msg)
4653 return -1;
4654
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004655 ret = nl80211_connect_common(drv, params, msg);
4656 if (ret)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004657 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004658
4659 if (params->prev_bssid) {
4660 wpa_printf(MSG_DEBUG, " * prev_bssid=" MACSTR,
4661 MAC2STR(params->prev_bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004662 if (nla_put(msg, NL80211_ATTR_PREV_BSSID, ETH_ALEN,
4663 params->prev_bssid))
4664 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004665 }
4666
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004667 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4668 msg = NULL;
4669 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004670 wpa_dbg(drv->ctx, MSG_DEBUG,
4671 "nl80211: MLME command failed (assoc): ret=%d (%s)",
4672 ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004673 nl80211_dump_scan(drv);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004674 } else {
4675 wpa_printf(MSG_DEBUG,
4676 "nl80211: Association request send successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004677 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004678
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004679fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004680 nlmsg_free(msg);
4681 return ret;
4682}
4683
4684
4685static int nl80211_set_mode(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004686 int ifindex, enum nl80211_iftype mode)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004687{
4688 struct nl_msg *msg;
4689 int ret = -ENOBUFS;
4690
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004691 wpa_printf(MSG_DEBUG, "nl80211: Set mode ifindex %d iftype %d (%s)",
4692 ifindex, mode, nl80211_iftype_str(mode));
4693
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004694 msg = nl80211_cmd_msg(drv->first_bss, 0, NL80211_CMD_SET_INTERFACE);
4695 if (!msg || nla_put_u32(msg, NL80211_ATTR_IFTYPE, mode))
4696 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004697
4698 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004699 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004700 if (!ret)
4701 return 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004702fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004703 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004704 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface %d to mode %d:"
4705 " %d (%s)", ifindex, mode, ret, strerror(-ret));
4706 return ret;
4707}
4708
4709
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004710static int wpa_driver_nl80211_set_mode_impl(
4711 struct i802_bss *bss,
4712 enum nl80211_iftype nlmode,
4713 struct hostapd_freq_params *desired_freq_params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004714{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004715 struct wpa_driver_nl80211_data *drv = bss->drv;
4716 int ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004717 int i;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004718 int was_ap = is_ap_interface(drv->nlmode);
4719 int res;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004720 int mode_switch_res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004721
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004722 mode_switch_res = nl80211_set_mode(drv, drv->ifindex, nlmode);
4723 if (mode_switch_res && nlmode == nl80211_get_ifmode(bss))
4724 mode_switch_res = 0;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004725
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004726 if (mode_switch_res == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004727 drv->nlmode = nlmode;
4728 ret = 0;
4729 goto done;
4730 }
4731
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004732 if (mode_switch_res == -ENODEV)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004733 return -1;
4734
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004735 if (nlmode == drv->nlmode) {
4736 wpa_printf(MSG_DEBUG, "nl80211: Interface already in "
4737 "requested mode - ignore error");
4738 ret = 0;
4739 goto done; /* Already in the requested mode */
4740 }
4741
4742 /* mac80211 doesn't allow mode changes while the device is up, so
4743 * take the device down, try to set the mode again, and bring the
4744 * device back up.
4745 */
4746 wpa_printf(MSG_DEBUG, "nl80211: Try mode change after setting "
4747 "interface down");
4748 for (i = 0; i < 10; i++) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004749 res = i802_set_iface_flags(bss, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004750 if (res == -EACCES || res == -ENODEV)
4751 break;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004752 if (res != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004753 wpa_printf(MSG_DEBUG, "nl80211: Failed to set "
4754 "interface down");
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004755 os_sleep(0, 100000);
4756 continue;
4757 }
4758
4759 /*
4760 * Setting the mode will fail for some drivers if the phy is
4761 * on a frequency that the mode is disallowed in.
4762 */
4763 if (desired_freq_params) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004764 res = nl80211_set_channel(bss, desired_freq_params, 0);
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004765 if (res) {
4766 wpa_printf(MSG_DEBUG,
4767 "nl80211: Failed to set frequency on interface");
4768 }
4769 }
4770
4771 /* Try to set the mode again while the interface is down */
4772 mode_switch_res = nl80211_set_mode(drv, drv->ifindex, nlmode);
4773 if (mode_switch_res == -EBUSY) {
4774 wpa_printf(MSG_DEBUG,
4775 "nl80211: Delaying mode set while interface going down");
4776 os_sleep(0, 100000);
4777 continue;
4778 }
4779 ret = mode_switch_res;
4780 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004781 }
4782
4783 if (!ret) {
4784 wpa_printf(MSG_DEBUG, "nl80211: Mode change succeeded while "
4785 "interface is down");
4786 drv->nlmode = nlmode;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004787 drv->ignore_if_down_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004788 }
4789
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004790 /* Bring the interface back up */
4791 res = linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1);
4792 if (res != 0) {
4793 wpa_printf(MSG_DEBUG,
4794 "nl80211: Failed to set interface up after switching mode");
4795 ret = -1;
4796 }
4797
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004798done:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004799 if (ret) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004800 wpa_printf(MSG_DEBUG, "nl80211: Interface mode change to %d "
4801 "from %d failed", nlmode, drv->nlmode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004802 return ret;
4803 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004804
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004805 if (is_p2p_net_interface(nlmode)) {
4806 wpa_printf(MSG_DEBUG,
4807 "nl80211: Interface %s mode change to P2P - disable 11b rates",
4808 bss->ifname);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004809 nl80211_disable_11b_rates(drv, drv->ifindex, 1);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004810 } else if (drv->disabled_11b_rates) {
4811 wpa_printf(MSG_DEBUG,
4812 "nl80211: Interface %s mode changed to non-P2P - re-enable 11b rates",
4813 bss->ifname);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004814 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004815 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004816
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004817 if (is_ap_interface(nlmode)) {
4818 nl80211_mgmt_unsubscribe(bss, "start AP");
4819 /* Setup additional AP mode functionality if needed */
4820 if (nl80211_setup_ap(bss))
4821 return -1;
4822 } else if (was_ap) {
4823 /* Remove additional AP mode functionality */
4824 nl80211_teardown_ap(bss);
4825 } else {
4826 nl80211_mgmt_unsubscribe(bss, "mode change");
4827 }
4828
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004829 if (is_mesh_interface(nlmode) &&
4830 nl80211_mgmt_subscribe_mesh(bss))
4831 return -1;
4832
Dmitry Shmidt04949592012-07-19 12:16:46 -07004833 if (!bss->in_deinit && !is_ap_interface(nlmode) &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004834 !is_mesh_interface(nlmode) &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004835 nl80211_mgmt_subscribe_non_ap(bss) < 0)
4836 wpa_printf(MSG_DEBUG, "nl80211: Failed to register Action "
4837 "frame processing - ignore for now");
4838
4839 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004840}
4841
4842
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004843int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
4844 enum nl80211_iftype nlmode)
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004845{
4846 return wpa_driver_nl80211_set_mode_impl(bss, nlmode, NULL);
4847}
4848
4849
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004850static int wpa_driver_nl80211_set_mode_ibss(struct i802_bss *bss,
4851 struct hostapd_freq_params *freq)
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004852{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004853 return wpa_driver_nl80211_set_mode_impl(bss, NL80211_IFTYPE_ADHOC,
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004854 freq);
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004855}
4856
4857
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004858static int wpa_driver_nl80211_get_capa(void *priv,
4859 struct wpa_driver_capa *capa)
4860{
4861 struct i802_bss *bss = priv;
4862 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07004863
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004864 if (!drv->has_capability)
4865 return -1;
4866 os_memcpy(capa, &drv->capa, sizeof(*capa));
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004867 if (drv->extended_capa && drv->extended_capa_mask) {
4868 capa->extended_capa = drv->extended_capa;
4869 capa->extended_capa_mask = drv->extended_capa_mask;
4870 capa->extended_capa_len = drv->extended_capa_len;
4871 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004872
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004873 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004874}
4875
4876
4877static int wpa_driver_nl80211_set_operstate(void *priv, int state)
4878{
4879 struct i802_bss *bss = priv;
4880 struct wpa_driver_nl80211_data *drv = bss->drv;
4881
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004882 wpa_printf(MSG_DEBUG, "nl80211: Set %s operstate %d->%d (%s)",
4883 bss->ifname, drv->operstate, state,
4884 state ? "UP" : "DORMANT");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004885 drv->operstate = state;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004886 return netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, -1,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004887 state ? IF_OPER_UP : IF_OPER_DORMANT);
4888}
4889
4890
4891static int wpa_driver_nl80211_set_supp_port(void *priv, int authorized)
4892{
4893 struct i802_bss *bss = priv;
4894 struct wpa_driver_nl80211_data *drv = bss->drv;
4895 struct nl_msg *msg;
4896 struct nl80211_sta_flag_update upd;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004897 int ret;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004898
4899 if (!drv->associated && is_zero_ether_addr(drv->bssid) && !authorized) {
4900 wpa_printf(MSG_DEBUG, "nl80211: Skip set_supp_port(unauthorized) while not associated");
4901 return 0;
4902 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004903
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004904 wpa_printf(MSG_DEBUG, "nl80211: Set supplicant port %sauthorized for "
4905 MACSTR, authorized ? "" : "un", MAC2STR(drv->bssid));
4906
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004907 os_memset(&upd, 0, sizeof(upd));
4908 upd.mask = BIT(NL80211_STA_FLAG_AUTHORIZED);
4909 if (authorized)
4910 upd.set = BIT(NL80211_STA_FLAG_AUTHORIZED);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004911
4912 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
4913 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid) ||
4914 nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd)) {
4915 nlmsg_free(msg);
4916 return -ENOBUFS;
4917 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004918
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004919 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004920 if (!ret)
4921 return 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004922 wpa_printf(MSG_DEBUG, "nl80211: Failed to set STA flag: %d (%s)",
4923 ret, strerror(-ret));
4924 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004925}
4926
4927
Jouni Malinen75ecf522011-06-27 15:19:46 -07004928/* Set kernel driver on given frequency (MHz) */
4929static int i802_set_freq(void *priv, struct hostapd_freq_params *freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004930{
Jouni Malinen75ecf522011-06-27 15:19:46 -07004931 struct i802_bss *bss = priv;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07004932 return nl80211_set_channel(bss, freq, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004933}
4934
4935
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004936static inline int min_int(int a, int b)
4937{
4938 if (a < b)
4939 return a;
4940 return b;
4941}
4942
4943
4944static int get_key_handler(struct nl_msg *msg, void *arg)
4945{
4946 struct nlattr *tb[NL80211_ATTR_MAX + 1];
4947 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
4948
4949 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
4950 genlmsg_attrlen(gnlh, 0), NULL);
4951
4952 /*
4953 * TODO: validate the key index and mac address!
4954 * Otherwise, there's a race condition as soon as
4955 * the kernel starts sending key notifications.
4956 */
4957
4958 if (tb[NL80211_ATTR_KEY_SEQ])
4959 memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]),
4960 min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6));
4961 return NL_SKIP;
4962}
4963
4964
4965static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
4966 int idx, u8 *seq)
4967{
4968 struct i802_bss *bss = priv;
4969 struct wpa_driver_nl80211_data *drv = bss->drv;
4970 struct nl_msg *msg;
4971
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004972 msg = nl80211_ifindex_msg(drv, if_nametoindex(iface), 0,
4973 NL80211_CMD_GET_KEY);
4974 if (!msg ||
4975 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
4976 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, idx)) {
4977 nlmsg_free(msg);
4978 return -ENOBUFS;
4979 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004980
4981 memset(seq, 0, 6);
4982
4983 return send_and_recv_msgs(drv, msg, get_key_handler, seq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004984}
4985
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004986
4987static int i802_set_rts(void *priv, int rts)
4988{
4989 struct i802_bss *bss = priv;
4990 struct wpa_driver_nl80211_data *drv = bss->drv;
4991 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004992 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004993 u32 val;
4994
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004995 if (rts >= 2347)
4996 val = (u32) -1;
4997 else
4998 val = rts;
4999
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005000 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_WIPHY)) ||
5001 nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD, val)) {
5002 nlmsg_free(msg);
5003 return -ENOBUFS;
5004 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005005
5006 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5007 if (!ret)
5008 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005009 wpa_printf(MSG_DEBUG, "nl80211: Failed to set RTS threshold %d: "
5010 "%d (%s)", rts, ret, strerror(-ret));
5011 return ret;
5012}
5013
5014
5015static int i802_set_frag(void *priv, int frag)
5016{
5017 struct i802_bss *bss = priv;
5018 struct wpa_driver_nl80211_data *drv = bss->drv;
5019 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005020 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005021 u32 val;
5022
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005023 if (frag >= 2346)
5024 val = (u32) -1;
5025 else
5026 val = frag;
5027
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005028 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_WIPHY)) ||
5029 nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD, val)) {
5030 nlmsg_free(msg);
5031 return -ENOBUFS;
5032 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005033
5034 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5035 if (!ret)
5036 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005037 wpa_printf(MSG_DEBUG, "nl80211: Failed to set fragmentation threshold "
5038 "%d: %d (%s)", frag, ret, strerror(-ret));
5039 return ret;
5040}
5041
5042
5043static int i802_flush(void *priv)
5044{
5045 struct i802_bss *bss = priv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005046 struct nl_msg *msg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005047 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005048
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005049 wpa_printf(MSG_DEBUG, "nl80211: flush -> DEL_STATION %s (all)",
5050 bss->ifname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005051
5052 /*
5053 * XXX: FIX! this needs to flush all VLANs too
5054 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005055 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_DEL_STATION);
5056 res = send_and_recv_msgs(bss->drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005057 if (res) {
5058 wpa_printf(MSG_DEBUG, "nl80211: Station flush failed: ret=%d "
5059 "(%s)", res, strerror(-res));
5060 }
5061 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005062}
5063
5064
5065static int get_sta_handler(struct nl_msg *msg, void *arg)
5066{
5067 struct nlattr *tb[NL80211_ATTR_MAX + 1];
5068 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
5069 struct hostap_sta_driver_data *data = arg;
5070 struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
5071 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
5072 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
5073 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
5074 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
5075 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
5076 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03005077 [NL80211_STA_INFO_TX_FAILED] = { .type = NLA_U32 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005078 };
5079
5080 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
5081 genlmsg_attrlen(gnlh, 0), NULL);
5082
5083 /*
5084 * TODO: validate the interface and mac address!
5085 * Otherwise, there's a race condition as soon as
5086 * the kernel starts sending station notifications.
5087 */
5088
5089 if (!tb[NL80211_ATTR_STA_INFO]) {
5090 wpa_printf(MSG_DEBUG, "sta stats missing!");
5091 return NL_SKIP;
5092 }
5093 if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
5094 tb[NL80211_ATTR_STA_INFO],
5095 stats_policy)) {
5096 wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
5097 return NL_SKIP;
5098 }
5099
5100 if (stats[NL80211_STA_INFO_INACTIVE_TIME])
5101 data->inactive_msec =
5102 nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
5103 if (stats[NL80211_STA_INFO_RX_BYTES])
5104 data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
5105 if (stats[NL80211_STA_INFO_TX_BYTES])
5106 data->tx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
5107 if (stats[NL80211_STA_INFO_RX_PACKETS])
5108 data->rx_packets =
5109 nla_get_u32(stats[NL80211_STA_INFO_RX_PACKETS]);
5110 if (stats[NL80211_STA_INFO_TX_PACKETS])
5111 data->tx_packets =
5112 nla_get_u32(stats[NL80211_STA_INFO_TX_PACKETS]);
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03005113 if (stats[NL80211_STA_INFO_TX_FAILED])
5114 data->tx_retry_failed =
5115 nla_get_u32(stats[NL80211_STA_INFO_TX_FAILED]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005116
5117 return NL_SKIP;
5118}
5119
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005120static int i802_read_sta_data(struct i802_bss *bss,
5121 struct hostap_sta_driver_data *data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005122 const u8 *addr)
5123{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005124 struct nl_msg *msg;
5125
5126 os_memset(data, 0, sizeof(*data));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005127
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005128 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_GET_STATION)) ||
5129 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
5130 nlmsg_free(msg);
5131 return -ENOBUFS;
5132 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005133
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005134 return send_and_recv_msgs(bss->drv, msg, get_sta_handler, data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005135}
5136
5137
5138static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
5139 int cw_min, int cw_max, int burst_time)
5140{
5141 struct i802_bss *bss = priv;
5142 struct wpa_driver_nl80211_data *drv = bss->drv;
5143 struct nl_msg *msg;
5144 struct nlattr *txq, *params;
5145
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005146 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005147 if (!msg)
5148 return -1;
5149
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005150 txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS);
5151 if (!txq)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005152 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005153
5154 /* We are only sending parameters for a single TXQ at a time */
5155 params = nla_nest_start(msg, 1);
5156 if (!params)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005157 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005158
5159 switch (queue) {
5160 case 0:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005161 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VO))
5162 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005163 break;
5164 case 1:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005165 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VI))
5166 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005167 break;
5168 case 2:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005169 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BE))
5170 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005171 break;
5172 case 3:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005173 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BK))
5174 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005175 break;
5176 }
5177 /* Burst time is configured in units of 0.1 msec and TXOP parameter in
5178 * 32 usec, so need to convert the value here. */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005179 if (nla_put_u16(msg, NL80211_TXQ_ATTR_TXOP,
5180 (burst_time * 100 + 16) / 32) ||
5181 nla_put_u16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min) ||
5182 nla_put_u16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max) ||
5183 nla_put_u8(msg, NL80211_TXQ_ATTR_AIFS, aifs))
5184 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005185
5186 nla_nest_end(msg, params);
5187
5188 nla_nest_end(msg, txq);
5189
5190 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
5191 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005192 msg = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005193fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005194 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005195 return -1;
5196}
5197
5198
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005199static int i802_set_sta_vlan(struct i802_bss *bss, const u8 *addr,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005200 const char *ifname, int vlan_id)
5201{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005202 struct wpa_driver_nl80211_data *drv = bss->drv;
5203 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005204 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005205
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005206 wpa_printf(MSG_DEBUG, "nl80211: %s[%d]: set_sta_vlan(" MACSTR
5207 ", ifname=%s[%d], vlan_id=%d)",
5208 bss->ifname, if_nametoindex(bss->ifname),
5209 MAC2STR(addr), ifname, if_nametoindex(ifname), vlan_id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005210 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
5211 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
5212 nla_put_u32(msg, NL80211_ATTR_STA_VLAN, if_nametoindex(ifname))) {
5213 nlmsg_free(msg);
5214 return -ENOBUFS;
5215 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005216
5217 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5218 if (ret < 0) {
5219 wpa_printf(MSG_ERROR, "nl80211: NL80211_ATTR_STA_VLAN (addr="
5220 MACSTR " ifname=%s vlan_id=%d) failed: %d (%s)",
5221 MAC2STR(addr), ifname, vlan_id, ret,
5222 strerror(-ret));
5223 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005224 return ret;
5225}
5226
5227
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005228static int i802_get_inact_sec(void *priv, const u8 *addr)
5229{
5230 struct hostap_sta_driver_data data;
5231 int ret;
5232
5233 data.inactive_msec = (unsigned long) -1;
5234 ret = i802_read_sta_data(priv, &data, addr);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08005235 if (ret == -ENOENT)
5236 return -ENOENT;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005237 if (ret || data.inactive_msec == (unsigned long) -1)
5238 return -1;
5239 return data.inactive_msec / 1000;
5240}
5241
5242
5243static int i802_sta_clear_stats(void *priv, const u8 *addr)
5244{
5245#if 0
5246 /* TODO */
5247#endif
5248 return 0;
5249}
5250
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005251
5252static int i802_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
5253 int reason)
5254{
5255 struct i802_bss *bss = priv;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005256 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005257 struct ieee80211_mgmt mgmt;
5258
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005259 if (is_mesh_interface(drv->nlmode))
5260 return -1;
5261
Dmitry Shmidt04949592012-07-19 12:16:46 -07005262 if (drv->device_ap_sme)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005263 return wpa_driver_nl80211_sta_remove(bss, addr, 1, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005264
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005265 memset(&mgmt, 0, sizeof(mgmt));
5266 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
5267 WLAN_FC_STYPE_DEAUTH);
5268 memcpy(mgmt.da, addr, ETH_ALEN);
5269 memcpy(mgmt.sa, own_addr, ETH_ALEN);
5270 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
5271 mgmt.u.deauth.reason_code = host_to_le16(reason);
5272 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
5273 IEEE80211_HDRLEN +
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005274 sizeof(mgmt.u.deauth), 0, 0, 0, 0,
5275 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005276}
5277
5278
5279static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
5280 int reason)
5281{
5282 struct i802_bss *bss = priv;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005283 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005284 struct ieee80211_mgmt mgmt;
5285
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005286 if (is_mesh_interface(drv->nlmode))
5287 return -1;
5288
Dmitry Shmidt04949592012-07-19 12:16:46 -07005289 if (drv->device_ap_sme)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005290 return wpa_driver_nl80211_sta_remove(bss, addr, 0, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005291
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005292 memset(&mgmt, 0, sizeof(mgmt));
5293 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
5294 WLAN_FC_STYPE_DISASSOC);
5295 memcpy(mgmt.da, addr, ETH_ALEN);
5296 memcpy(mgmt.sa, own_addr, ETH_ALEN);
5297 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
5298 mgmt.u.disassoc.reason_code = host_to_le16(reason);
5299 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
5300 IEEE80211_HDRLEN +
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005301 sizeof(mgmt.u.disassoc), 0, 0, 0, 0,
5302 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005303}
5304
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005305
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07005306static void dump_ifidx(struct wpa_driver_nl80211_data *drv)
5307{
5308 char buf[200], *pos, *end;
5309 int i, res;
5310
5311 pos = buf;
5312 end = pos + sizeof(buf);
5313
5314 for (i = 0; i < drv->num_if_indices; i++) {
5315 if (!drv->if_indices[i])
5316 continue;
5317 res = os_snprintf(pos, end - pos, " %d", drv->if_indices[i]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005318 if (os_snprintf_error(end - pos, res))
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07005319 break;
5320 pos += res;
5321 }
5322 *pos = '\0';
5323
5324 wpa_printf(MSG_DEBUG, "nl80211: if_indices[%d]:%s",
5325 drv->num_if_indices, buf);
5326}
5327
5328
Jouni Malinen75ecf522011-06-27 15:19:46 -07005329static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
5330{
5331 int i;
5332 int *old;
5333
5334 wpa_printf(MSG_DEBUG, "nl80211: Add own interface ifindex %d",
5335 ifidx);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07005336 if (have_ifidx(drv, ifidx)) {
5337 wpa_printf(MSG_DEBUG, "nl80211: ifindex %d already in the list",
5338 ifidx);
5339 return;
5340 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07005341 for (i = 0; i < drv->num_if_indices; i++) {
5342 if (drv->if_indices[i] == 0) {
5343 drv->if_indices[i] = ifidx;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07005344 dump_ifidx(drv);
Jouni Malinen75ecf522011-06-27 15:19:46 -07005345 return;
5346 }
5347 }
5348
5349 if (drv->if_indices != drv->default_if_indices)
5350 old = drv->if_indices;
5351 else
5352 old = NULL;
5353
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005354 drv->if_indices = os_realloc_array(old, drv->num_if_indices + 1,
5355 sizeof(int));
Jouni Malinen75ecf522011-06-27 15:19:46 -07005356 if (!drv->if_indices) {
5357 if (!old)
5358 drv->if_indices = drv->default_if_indices;
5359 else
5360 drv->if_indices = old;
5361 wpa_printf(MSG_ERROR, "Failed to reallocate memory for "
5362 "interfaces");
5363 wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx);
5364 return;
5365 } else if (!old)
5366 os_memcpy(drv->if_indices, drv->default_if_indices,
5367 sizeof(drv->default_if_indices));
5368 drv->if_indices[drv->num_if_indices] = ifidx;
5369 drv->num_if_indices++;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07005370 dump_ifidx(drv);
Jouni Malinen75ecf522011-06-27 15:19:46 -07005371}
5372
5373
5374static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
5375{
5376 int i;
5377
5378 for (i = 0; i < drv->num_if_indices; i++) {
5379 if (drv->if_indices[i] == ifidx) {
5380 drv->if_indices[i] = 0;
5381 break;
5382 }
5383 }
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07005384 dump_ifidx(drv);
Jouni Malinen75ecf522011-06-27 15:19:46 -07005385}
5386
5387
5388static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
5389{
5390 int i;
5391
5392 for (i = 0; i < drv->num_if_indices; i++)
5393 if (drv->if_indices[i] == ifidx)
5394 return 1;
5395
5396 return 0;
5397}
5398
5399
5400static int i802_set_wds_sta(void *priv, const u8 *addr, int aid, int val,
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07005401 const char *bridge_ifname, char *ifname_wds)
Jouni Malinen75ecf522011-06-27 15:19:46 -07005402{
5403 struct i802_bss *bss = priv;
5404 struct wpa_driver_nl80211_data *drv = bss->drv;
5405 char name[IFNAMSIZ + 1];
5406
5407 os_snprintf(name, sizeof(name), "%s.sta%d", bss->ifname, aid);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005408 if (ifname_wds)
5409 os_strlcpy(ifname_wds, name, IFNAMSIZ + 1);
5410
Jouni Malinen75ecf522011-06-27 15:19:46 -07005411 wpa_printf(MSG_DEBUG, "nl80211: Set WDS STA addr=" MACSTR
5412 " aid=%d val=%d name=%s", MAC2STR(addr), aid, val, name);
5413 if (val) {
5414 if (!if_nametoindex(name)) {
5415 if (nl80211_create_iface(drv, name,
5416 NL80211_IFTYPE_AP_VLAN,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005417 bss->addr, 1, NULL, NULL, 0) <
5418 0)
Jouni Malinen75ecf522011-06-27 15:19:46 -07005419 return -1;
5420 if (bridge_ifname &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005421 linux_br_add_if(drv->global->ioctl_sock,
5422 bridge_ifname, name) < 0)
Jouni Malinen75ecf522011-06-27 15:19:46 -07005423 return -1;
5424 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005425 if (linux_set_iface_flags(drv->global->ioctl_sock, name, 1)) {
5426 wpa_printf(MSG_ERROR, "nl80211: Failed to set WDS STA "
5427 "interface %s up", name);
5428 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07005429 return i802_set_sta_vlan(priv, addr, name, 0);
5430 } else {
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005431 if (bridge_ifname)
5432 linux_br_del_if(drv->global->ioctl_sock, bridge_ifname,
5433 name);
5434
Jouni Malinen75ecf522011-06-27 15:19:46 -07005435 i802_set_sta_vlan(priv, addr, bss->ifname, 0);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08005436 nl80211_remove_iface(drv, if_nametoindex(name));
5437 return 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07005438 }
5439}
5440
5441
5442static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
5443{
5444 struct wpa_driver_nl80211_data *drv = eloop_ctx;
5445 struct sockaddr_ll lladdr;
5446 unsigned char buf[3000];
5447 int len;
5448 socklen_t fromlen = sizeof(lladdr);
5449
5450 len = recvfrom(sock, buf, sizeof(buf), 0,
5451 (struct sockaddr *)&lladdr, &fromlen);
5452 if (len < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005453 wpa_printf(MSG_ERROR, "nl80211: EAPOL recv failed: %s",
5454 strerror(errno));
Jouni Malinen75ecf522011-06-27 15:19:46 -07005455 return;
5456 }
5457
5458 if (have_ifidx(drv, lladdr.sll_ifindex))
5459 drv_event_eapol_rx(drv->ctx, lladdr.sll_addr, buf, len);
5460}
5461
5462
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005463static int i802_check_bridge(struct wpa_driver_nl80211_data *drv,
5464 struct i802_bss *bss,
5465 const char *brname, const char *ifname)
5466{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005467 int br_ifindex;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005468 char in_br[IFNAMSIZ];
5469
5470 os_strlcpy(bss->brname, brname, IFNAMSIZ);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005471 br_ifindex = if_nametoindex(brname);
5472 if (br_ifindex == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005473 /*
5474 * Bridge was configured, but the bridge device does
5475 * not exist. Try to add it now.
5476 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005477 if (linux_br_add(drv->global->ioctl_sock, brname) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005478 wpa_printf(MSG_ERROR, "nl80211: Failed to add the "
5479 "bridge interface %s: %s",
5480 brname, strerror(errno));
5481 return -1;
5482 }
5483 bss->added_bridge = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005484 br_ifindex = if_nametoindex(brname);
5485 add_ifidx(drv, br_ifindex);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005486 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005487 bss->br_ifindex = br_ifindex;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005488
5489 if (linux_br_get(in_br, ifname) == 0) {
5490 if (os_strcmp(in_br, brname) == 0)
5491 return 0; /* already in the bridge */
5492
5493 wpa_printf(MSG_DEBUG, "nl80211: Removing interface %s from "
5494 "bridge %s", ifname, in_br);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005495 if (linux_br_del_if(drv->global->ioctl_sock, in_br, ifname) <
5496 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005497 wpa_printf(MSG_ERROR, "nl80211: Failed to "
5498 "remove interface %s from bridge "
5499 "%s: %s",
5500 ifname, brname, strerror(errno));
5501 return -1;
5502 }
5503 }
5504
5505 wpa_printf(MSG_DEBUG, "nl80211: Adding interface %s into bridge %s",
5506 ifname, brname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005507 if (linux_br_add_if(drv->global->ioctl_sock, brname, ifname) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005508 wpa_printf(MSG_ERROR, "nl80211: Failed to add interface %s "
5509 "into bridge %s: %s",
5510 ifname, brname, strerror(errno));
5511 return -1;
5512 }
5513 bss->added_if_into_bridge = 1;
5514
5515 return 0;
5516}
5517
5518
5519static void *i802_init(struct hostapd_data *hapd,
5520 struct wpa_init_params *params)
5521{
5522 struct wpa_driver_nl80211_data *drv;
5523 struct i802_bss *bss;
5524 size_t i;
5525 char brname[IFNAMSIZ];
5526 int ifindex, br_ifindex;
5527 int br_added = 0;
5528
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005529 bss = wpa_driver_nl80211_drv_init(hapd, params->ifname,
5530 params->global_priv, 1,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005531 params->bssid, params->driver_params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005532 if (bss == NULL)
5533 return NULL;
5534
5535 drv = bss->drv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005536
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005537 if (linux_br_get(brname, params->ifname) == 0) {
5538 wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in bridge %s",
5539 params->ifname, brname);
5540 br_ifindex = if_nametoindex(brname);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005541 os_strlcpy(bss->brname, brname, IFNAMSIZ);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005542 } else {
5543 brname[0] = '\0';
5544 br_ifindex = 0;
5545 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005546 bss->br_ifindex = br_ifindex;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005547
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005548 for (i = 0; i < params->num_bridge; i++) {
5549 if (params->bridge[i]) {
5550 ifindex = if_nametoindex(params->bridge[i]);
5551 if (ifindex)
5552 add_ifidx(drv, ifindex);
5553 if (ifindex == br_ifindex)
5554 br_added = 1;
5555 }
5556 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005557
5558 /* start listening for EAPOL on the default AP interface */
5559 add_ifidx(drv, drv->ifindex);
5560
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005561 if (params->num_bridge && params->bridge[0]) {
5562 if (i802_check_bridge(drv, bss, params->bridge[0],
5563 params->ifname) < 0)
5564 goto failed;
5565 if (os_strcmp(params->bridge[0], brname) != 0)
5566 br_added = 1;
5567 }
5568
5569 if (!br_added && br_ifindex &&
5570 (params->num_bridge == 0 || !params->bridge[0]))
5571 add_ifidx(drv, br_ifindex);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005572
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005573#ifdef CONFIG_LIBNL3_ROUTE
5574 if (bss->added_if_into_bridge) {
5575 drv->rtnl_sk = nl_socket_alloc();
5576 if (drv->rtnl_sk == NULL) {
5577 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate nl_sock");
5578 goto failed;
5579 }
5580
5581 if (nl_connect(drv->rtnl_sk, NETLINK_ROUTE)) {
5582 wpa_printf(MSG_ERROR, "nl80211: Failed to connect nl_sock to NETLINK_ROUTE: %s",
5583 strerror(errno));
5584 goto failed;
5585 }
5586 }
5587#endif /* CONFIG_LIBNL3_ROUTE */
5588
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005589 drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
5590 if (drv->eapol_sock < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005591 wpa_printf(MSG_ERROR, "nl80211: socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE) failed: %s",
5592 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005593 goto failed;
5594 }
5595
5596 if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
5597 {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005598 wpa_printf(MSG_INFO, "nl80211: Could not register read socket for eapol");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005599 goto failed;
5600 }
5601
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005602 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
5603 params->own_addr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005604 goto failed;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005605 os_memcpy(drv->perm_addr, params->own_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005606
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005607 memcpy(bss->addr, params->own_addr, ETH_ALEN);
5608
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005609 return bss;
5610
5611failed:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005612 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005613 return NULL;
5614}
5615
5616
5617static void i802_deinit(void *priv)
5618{
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005619 struct i802_bss *bss = priv;
5620 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005621}
5622
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005623
5624static enum nl80211_iftype wpa_driver_nl80211_if_type(
5625 enum wpa_driver_if_type type)
5626{
5627 switch (type) {
5628 case WPA_IF_STATION:
5629 return NL80211_IFTYPE_STATION;
5630 case WPA_IF_P2P_CLIENT:
5631 case WPA_IF_P2P_GROUP:
5632 return NL80211_IFTYPE_P2P_CLIENT;
5633 case WPA_IF_AP_VLAN:
5634 return NL80211_IFTYPE_AP_VLAN;
5635 case WPA_IF_AP_BSS:
5636 return NL80211_IFTYPE_AP;
5637 case WPA_IF_P2P_GO:
5638 return NL80211_IFTYPE_P2P_GO;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005639 case WPA_IF_P2P_DEVICE:
5640 return NL80211_IFTYPE_P2P_DEVICE;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005641 case WPA_IF_MESH:
5642 return NL80211_IFTYPE_MESH_POINT;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005643 }
5644 return -1;
5645}
5646
5647
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005648#if defined(CONFIG_P2P) || defined(CONFIG_MESH)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005649
5650static int nl80211_addr_in_use(struct nl80211_global *global, const u8 *addr)
5651{
5652 struct wpa_driver_nl80211_data *drv;
5653 dl_list_for_each(drv, &global->interfaces,
5654 struct wpa_driver_nl80211_data, list) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005655 if (os_memcmp(addr, drv->first_bss->addr, ETH_ALEN) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005656 return 1;
5657 }
5658 return 0;
5659}
5660
5661
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005662static int nl80211_vif_addr(struct wpa_driver_nl80211_data *drv, u8 *new_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005663{
5664 unsigned int idx;
5665
5666 if (!drv->global)
5667 return -1;
5668
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005669 os_memcpy(new_addr, drv->first_bss->addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005670 for (idx = 0; idx < 64; idx++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005671 new_addr[0] = drv->first_bss->addr[0] | 0x02;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005672 new_addr[0] ^= idx << 2;
5673 if (!nl80211_addr_in_use(drv->global, new_addr))
5674 break;
5675 }
5676 if (idx == 64)
5677 return -1;
5678
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005679 wpa_printf(MSG_DEBUG, "nl80211: Assigned new virtual interface address "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005680 MACSTR, MAC2STR(new_addr));
5681
5682 return 0;
5683}
5684
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005685#endif /* CONFIG_P2P || CONFIG_MESH */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005686
5687
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005688struct wdev_info {
5689 u64 wdev_id;
5690 int wdev_id_set;
5691 u8 macaddr[ETH_ALEN];
5692};
5693
5694static int nl80211_wdev_handler(struct nl_msg *msg, void *arg)
5695{
5696 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
5697 struct nlattr *tb[NL80211_ATTR_MAX + 1];
5698 struct wdev_info *wi = arg;
5699
5700 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
5701 genlmsg_attrlen(gnlh, 0), NULL);
5702 if (tb[NL80211_ATTR_WDEV]) {
5703 wi->wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
5704 wi->wdev_id_set = 1;
5705 }
5706
5707 if (tb[NL80211_ATTR_MAC])
5708 os_memcpy(wi->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
5709 ETH_ALEN);
5710
5711 return NL_SKIP;
5712}
5713
5714
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005715static int wpa_driver_nl80211_if_add(void *priv, enum wpa_driver_if_type type,
5716 const char *ifname, const u8 *addr,
5717 void *bss_ctx, void **drv_priv,
5718 char *force_ifname, u8 *if_addr,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005719 const char *bridge, int use_existing)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005720{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005721 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005722 struct i802_bss *bss = priv;
5723 struct wpa_driver_nl80211_data *drv = bss->drv;
5724 int ifidx;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005725 int added = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005726
5727 if (addr)
5728 os_memcpy(if_addr, addr, ETH_ALEN);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005729 nlmode = wpa_driver_nl80211_if_type(type);
5730 if (nlmode == NL80211_IFTYPE_P2P_DEVICE) {
5731 struct wdev_info p2pdev_info;
5732
5733 os_memset(&p2pdev_info, 0, sizeof(p2pdev_info));
5734 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
5735 0, nl80211_wdev_handler,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005736 &p2pdev_info, use_existing);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005737 if (!p2pdev_info.wdev_id_set || ifidx != 0) {
5738 wpa_printf(MSG_ERROR, "nl80211: Failed to create a P2P Device interface %s",
5739 ifname);
5740 return -1;
5741 }
5742
5743 drv->global->if_add_wdevid = p2pdev_info.wdev_id;
5744 drv->global->if_add_wdevid_set = p2pdev_info.wdev_id_set;
5745 if (!is_zero_ether_addr(p2pdev_info.macaddr))
5746 os_memcpy(if_addr, p2pdev_info.macaddr, ETH_ALEN);
5747 wpa_printf(MSG_DEBUG, "nl80211: New P2P Device interface %s (0x%llx) created",
5748 ifname,
5749 (long long unsigned int) p2pdev_info.wdev_id);
5750 } else {
5751 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005752 0, NULL, NULL, use_existing);
5753 if (use_existing && ifidx == -ENFILE) {
5754 added = 0;
5755 ifidx = if_nametoindex(ifname);
5756 } else if (ifidx < 0) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005757 return -1;
5758 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005759 }
5760
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005761 if (!addr) {
5762 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
5763 os_memcpy(if_addr, bss->addr, ETH_ALEN);
5764 else if (linux_get_ifhwaddr(drv->global->ioctl_sock,
5765 bss->ifname, if_addr) < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005766 if (added)
5767 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005768 return -1;
5769 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005770 }
5771
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005772#if defined(CONFIG_P2P) || defined(CONFIG_MESH)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005773 if (!addr &&
5774 (type == WPA_IF_P2P_CLIENT || type == WPA_IF_P2P_GROUP ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005775 type == WPA_IF_P2P_GO || type == WPA_IF_MESH)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005776 /* Enforce unique P2P Interface Address */
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005777 u8 new_addr[ETH_ALEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005778
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005779 if (linux_get_ifhwaddr(drv->global->ioctl_sock, ifname,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005780 new_addr) < 0) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07005781 if (added)
5782 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005783 return -1;
5784 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005785 if (nl80211_addr_in_use(drv->global, new_addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005786 wpa_printf(MSG_DEBUG, "nl80211: Allocate new address "
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005787 "for %s interface", type == WPA_IF_MESH ?
5788 "mesh" : "P2P group");
5789 if (nl80211_vif_addr(drv, new_addr) < 0) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07005790 if (added)
5791 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005792 return -1;
5793 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005794 if (linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005795 new_addr) < 0) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07005796 if (added)
5797 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005798 return -1;
5799 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005800 }
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07005801 os_memcpy(if_addr, new_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005802 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005803#endif /* CONFIG_P2P || CONFIG_MESH */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005804
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005805 if (type == WPA_IF_AP_BSS) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005806 struct i802_bss *new_bss = os_zalloc(sizeof(*new_bss));
5807 if (new_bss == NULL) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005808 if (added)
5809 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005810 return -1;
5811 }
5812
5813 if (bridge &&
5814 i802_check_bridge(drv, new_bss, bridge, ifname) < 0) {
5815 wpa_printf(MSG_ERROR, "nl80211: Failed to add the new "
5816 "interface %s to a bridge %s",
5817 ifname, bridge);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005818 if (added)
5819 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005820 os_free(new_bss);
5821 return -1;
5822 }
5823
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005824 if (linux_set_iface_flags(drv->global->ioctl_sock, ifname, 1))
5825 {
Dmitry Shmidt71757432014-06-02 13:50:35 -07005826 if (added)
5827 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005828 os_free(new_bss);
5829 return -1;
5830 }
5831 os_strlcpy(new_bss->ifname, ifname, IFNAMSIZ);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005832 os_memcpy(new_bss->addr, if_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005833 new_bss->ifindex = ifidx;
5834 new_bss->drv = drv;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005835 new_bss->next = drv->first_bss->next;
5836 new_bss->freq = drv->first_bss->freq;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005837 new_bss->ctx = bss_ctx;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005838 new_bss->added_if = added;
5839 drv->first_bss->next = new_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005840 if (drv_priv)
5841 *drv_priv = new_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005842 nl80211_init_bss(new_bss);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005843
5844 /* Subscribe management frames for this WPA_IF_AP_BSS */
5845 if (nl80211_setup_ap(new_bss))
5846 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005847 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005848
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005849 if (drv->global)
5850 drv->global->if_add_ifindex = ifidx;
5851
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005852 /*
5853 * Some virtual interfaces need to process EAPOL packets and events on
5854 * the parent interface. This is used mainly with hostapd.
5855 */
5856 if (ifidx > 0 &&
5857 (drv->hostapd ||
5858 nlmode == NL80211_IFTYPE_AP_VLAN ||
5859 nlmode == NL80211_IFTYPE_WDS ||
5860 nlmode == NL80211_IFTYPE_MONITOR))
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07005861 add_ifidx(drv, ifidx);
5862
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005863 return 0;
5864}
5865
5866
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005867static int wpa_driver_nl80211_if_remove(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005868 enum wpa_driver_if_type type,
5869 const char *ifname)
5870{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005871 struct wpa_driver_nl80211_data *drv = bss->drv;
5872 int ifindex = if_nametoindex(ifname);
5873
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005874 wpa_printf(MSG_DEBUG, "nl80211: %s(type=%d ifname=%s) ifindex=%d added_if=%d",
5875 __func__, type, ifname, ifindex, bss->added_if);
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08005876 if (ifindex > 0 && (bss->added_if || bss->ifindex != ifindex))
Dmitry Shmidt051af732013-10-22 13:52:46 -07005877 nl80211_remove_iface(drv, ifindex);
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07005878 else if (ifindex > 0 && !bss->added_if) {
5879 struct wpa_driver_nl80211_data *drv2;
5880 dl_list_for_each(drv2, &drv->global->interfaces,
5881 struct wpa_driver_nl80211_data, list)
5882 del_ifidx(drv2, ifindex);
5883 }
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005884
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005885 if (type != WPA_IF_AP_BSS)
5886 return 0;
5887
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005888 if (bss->added_if_into_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005889 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
5890 bss->ifname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005891 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
5892 "interface %s from bridge %s: %s",
5893 bss->ifname, bss->brname, strerror(errno));
5894 }
5895 if (bss->added_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005896 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005897 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
5898 "bridge %s: %s",
5899 bss->brname, strerror(errno));
5900 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005901
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005902 if (bss != drv->first_bss) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005903 struct i802_bss *tbss;
5904
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005905 wpa_printf(MSG_DEBUG, "nl80211: Not the first BSS - remove it");
5906 for (tbss = drv->first_bss; tbss; tbss = tbss->next) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005907 if (tbss->next == bss) {
5908 tbss->next = bss->next;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005909 /* Unsubscribe management frames */
5910 nl80211_teardown_ap(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005911 nl80211_destroy_bss(bss);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07005912 if (!bss->added_if)
5913 i802_set_iface_flags(bss, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005914 os_free(bss);
5915 bss = NULL;
5916 break;
5917 }
5918 }
5919 if (bss)
5920 wpa_printf(MSG_INFO, "nl80211: %s - could not find "
5921 "BSS %p in the list", __func__, bss);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005922 } else {
5923 wpa_printf(MSG_DEBUG, "nl80211: First BSS - reassign context");
5924 nl80211_teardown_ap(bss);
5925 if (!bss->added_if && !drv->first_bss->next)
5926 wpa_driver_nl80211_del_beacon(drv);
5927 nl80211_destroy_bss(bss);
5928 if (!bss->added_if)
5929 i802_set_iface_flags(bss, 0);
5930 if (drv->first_bss->next) {
5931 drv->first_bss = drv->first_bss->next;
5932 drv->ctx = drv->first_bss->ctx;
5933 os_free(bss);
5934 } else {
5935 wpa_printf(MSG_DEBUG, "nl80211: No second BSS to reassign context to");
5936 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005937 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005938
5939 return 0;
5940}
5941
5942
5943static int cookie_handler(struct nl_msg *msg, void *arg)
5944{
5945 struct nlattr *tb[NL80211_ATTR_MAX + 1];
5946 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
5947 u64 *cookie = arg;
5948 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
5949 genlmsg_attrlen(gnlh, 0), NULL);
5950 if (tb[NL80211_ATTR_COOKIE])
5951 *cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
5952 return NL_SKIP;
5953}
5954
5955
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005956static int nl80211_send_frame_cmd(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005957 unsigned int freq, unsigned int wait,
5958 const u8 *buf, size_t buf_len,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005959 u64 *cookie_out, int no_cck, int no_ack,
5960 int offchanok)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005961{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005962 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005963 struct nl_msg *msg;
5964 u64 cookie;
5965 int ret = -1;
5966
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005967 wpa_printf(MSG_MSGDUMP, "nl80211: CMD_FRAME freq=%u wait=%u no_cck=%d "
Dmitry Shmidt04949592012-07-19 12:16:46 -07005968 "no_ack=%d offchanok=%d",
5969 freq, wait, no_cck, no_ack, offchanok);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005970 wpa_hexdump(MSG_MSGDUMP, "CMD_FRAME", buf, buf_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005971
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005972 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_FRAME)) ||
5973 (freq && nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
5974 (wait && nla_put_u32(msg, NL80211_ATTR_DURATION, wait)) ||
5975 (offchanok && ((drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
5976 drv->test_use_roc_tx) &&
5977 nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK)) ||
5978 (no_cck && nla_put_flag(msg, NL80211_ATTR_TX_NO_CCK_RATE)) ||
5979 (no_ack && nla_put_flag(msg, NL80211_ATTR_DONT_WAIT_FOR_ACK)) ||
5980 nla_put(msg, NL80211_ATTR_FRAME, buf_len, buf))
5981 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005982
5983 cookie = 0;
5984 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
5985 msg = NULL;
5986 if (ret) {
5987 wpa_printf(MSG_DEBUG, "nl80211: Frame command failed: ret=%d "
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07005988 "(%s) (freq=%u wait=%u)", ret, strerror(-ret),
5989 freq, wait);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005990 } else {
5991 wpa_printf(MSG_MSGDUMP, "nl80211: Frame TX command accepted%s; "
5992 "cookie 0x%llx", no_ack ? " (no ACK)" : "",
5993 (long long unsigned int) cookie);
5994
5995 if (cookie_out)
5996 *cookie_out = no_ack ? (u64) -1 : cookie;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005997 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005998
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005999fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006000 nlmsg_free(msg);
6001 return ret;
6002}
6003
6004
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006005static int wpa_driver_nl80211_send_action(struct i802_bss *bss,
6006 unsigned int freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006007 unsigned int wait_time,
6008 const u8 *dst, const u8 *src,
6009 const u8 *bssid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006010 const u8 *data, size_t data_len,
6011 int no_cck)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006012{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006013 struct wpa_driver_nl80211_data *drv = bss->drv;
6014 int ret = -1;
6015 u8 *buf;
6016 struct ieee80211_hdr *hdr;
6017
6018 wpa_printf(MSG_DEBUG, "nl80211: Send Action frame (ifindex=%d, "
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006019 "freq=%u MHz wait=%d ms no_cck=%d)",
6020 drv->ifindex, freq, wait_time, no_cck);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006021
6022 buf = os_zalloc(24 + data_len);
6023 if (buf == NULL)
6024 return ret;
6025 os_memcpy(buf + 24, data, data_len);
6026 hdr = (struct ieee80211_hdr *) buf;
6027 hdr->frame_control =
6028 IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_ACTION);
6029 os_memcpy(hdr->addr1, dst, ETH_ALEN);
6030 os_memcpy(hdr->addr2, src, ETH_ALEN);
6031 os_memcpy(hdr->addr3, bssid, ETH_ALEN);
6032
Dmitry Shmidt56052862013-10-04 10:23:25 -07006033 if (is_ap_interface(drv->nlmode) &&
6034 (!(drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
6035 (int) freq == bss->freq || drv->device_ap_sme ||
6036 !drv->use_monitor))
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006037 ret = wpa_driver_nl80211_send_mlme(bss, buf, 24 + data_len,
6038 0, freq, no_cck, 1,
6039 wait_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006040 else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006041 ret = nl80211_send_frame_cmd(bss, freq, wait_time, buf,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006042 24 + data_len,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006043 &drv->send_action_cookie,
6044 no_cck, 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006045
6046 os_free(buf);
6047 return ret;
6048}
6049
6050
6051static void wpa_driver_nl80211_send_action_cancel_wait(void *priv)
6052{
6053 struct i802_bss *bss = priv;
6054 struct wpa_driver_nl80211_data *drv = bss->drv;
6055 struct nl_msg *msg;
6056 int ret;
6057
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08006058 wpa_printf(MSG_DEBUG, "nl80211: Cancel TX frame wait: cookie=0x%llx",
6059 (long long unsigned int) drv->send_action_cookie);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006060 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_FRAME_WAIT_CANCEL)) ||
6061 nla_put_u64(msg, NL80211_ATTR_COOKIE, drv->send_action_cookie)) {
6062 nlmsg_free(msg);
6063 return;
6064 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006065
6066 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006067 if (ret)
6068 wpa_printf(MSG_DEBUG, "nl80211: wait cancel failed: ret=%d "
6069 "(%s)", ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006070}
6071
6072
6073static int wpa_driver_nl80211_remain_on_channel(void *priv, unsigned int freq,
6074 unsigned int duration)
6075{
6076 struct i802_bss *bss = priv;
6077 struct wpa_driver_nl80211_data *drv = bss->drv;
6078 struct nl_msg *msg;
6079 int ret;
6080 u64 cookie;
6081
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006082 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_REMAIN_ON_CHANNEL)) ||
6083 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
6084 nla_put_u32(msg, NL80211_ATTR_DURATION, duration)) {
6085 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006086 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006087 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006088
6089 cookie = 0;
6090 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
6091 if (ret == 0) {
6092 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel cookie "
6093 "0x%llx for freq=%u MHz duration=%u",
6094 (long long unsigned int) cookie, freq, duration);
6095 drv->remain_on_chan_cookie = cookie;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006096 drv->pending_remain_on_chan = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006097 return 0;
6098 }
6099 wpa_printf(MSG_DEBUG, "nl80211: Failed to request remain-on-channel "
6100 "(freq=%d duration=%u): %d (%s)",
6101 freq, duration, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006102 return -1;
6103}
6104
6105
6106static int wpa_driver_nl80211_cancel_remain_on_channel(void *priv)
6107{
6108 struct i802_bss *bss = priv;
6109 struct wpa_driver_nl80211_data *drv = bss->drv;
6110 struct nl_msg *msg;
6111 int ret;
6112
6113 if (!drv->pending_remain_on_chan) {
6114 wpa_printf(MSG_DEBUG, "nl80211: No pending remain-on-channel "
6115 "to cancel");
6116 return -1;
6117 }
6118
6119 wpa_printf(MSG_DEBUG, "nl80211: Cancel remain-on-channel with cookie "
6120 "0x%llx",
6121 (long long unsigned int) drv->remain_on_chan_cookie);
6122
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006123 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL);
6124 if (!msg ||
6125 nla_put_u64(msg, NL80211_ATTR_COOKIE, drv->remain_on_chan_cookie)) {
6126 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006127 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006128 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006129
6130 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6131 if (ret == 0)
6132 return 0;
6133 wpa_printf(MSG_DEBUG, "nl80211: Failed to cancel remain-on-channel: "
6134 "%d (%s)", ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006135 return -1;
6136}
6137
6138
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006139static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss, int report)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006140{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006141 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07006142
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006143 if (!report) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006144 if (bss->nl_preq && drv->device_ap_sme &&
Dmitry Shmidt03658832014-08-13 11:03:49 -07006145 is_ap_interface(drv->nlmode) && !bss->in_deinit &&
6146 !bss->static_ap) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006147 /*
6148 * Do not disable Probe Request reporting that was
6149 * enabled in nl80211_setup_ap().
6150 */
6151 wpa_printf(MSG_DEBUG, "nl80211: Skip disabling of "
6152 "Probe Request reporting nl_preq=%p while "
6153 "in AP mode", bss->nl_preq);
6154 } else if (bss->nl_preq) {
6155 wpa_printf(MSG_DEBUG, "nl80211: Disable Probe Request "
6156 "reporting nl_preq=%p", bss->nl_preq);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006157 nl80211_destroy_eloop_handle(&bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006158 }
6159 return 0;
6160 }
6161
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006162 if (bss->nl_preq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006163 wpa_printf(MSG_DEBUG, "nl80211: Probe Request reporting "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006164 "already on! nl_preq=%p", bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006165 return 0;
6166 }
6167
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006168 bss->nl_preq = nl_create_handle(drv->global->nl_cb, "preq");
6169 if (bss->nl_preq == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006170 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006171 wpa_printf(MSG_DEBUG, "nl80211: Enable Probe Request "
6172 "reporting nl_preq=%p", bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006173
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006174 if (nl80211_register_frame(bss, bss->nl_preq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006175 (WLAN_FC_TYPE_MGMT << 2) |
6176 (WLAN_FC_STYPE_PROBE_REQ << 4),
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006177 NULL, 0) < 0)
6178 goto out_err;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07006179
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006180 nl80211_register_eloop_read(&bss->nl_preq,
6181 wpa_driver_nl80211_event_receive,
6182 bss->nl_cb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006183
6184 return 0;
6185
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006186 out_err:
6187 nl_destroy_handles(&bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006188 return -1;
6189}
6190
6191
6192static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
6193 int ifindex, int disabled)
6194{
6195 struct nl_msg *msg;
6196 struct nlattr *bands, *band;
6197 int ret;
6198
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006199 wpa_printf(MSG_DEBUG,
6200 "nl80211: NL80211_CMD_SET_TX_BITRATE_MASK (ifindex=%d %s)",
6201 ifindex, disabled ? "NL80211_TXRATE_LEGACY=OFDM-only" :
6202 "no NL80211_TXRATE_LEGACY constraint");
6203
6204 msg = nl80211_ifindex_msg(drv, ifindex, 0,
6205 NL80211_CMD_SET_TX_BITRATE_MASK);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006206 if (!msg)
6207 return -1;
6208
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006209 bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES);
6210 if (!bands)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006211 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006212
6213 /*
6214 * Disable 2 GHz rates 1, 2, 5.5, 11 Mbps by masking out everything
6215 * else apart from 6, 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS
6216 * rates. All 5 GHz rates are left enabled.
6217 */
6218 band = nla_nest_start(msg, NL80211_BAND_2GHZ);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006219 if (!band ||
6220 (disabled && nla_put(msg, NL80211_TXRATE_LEGACY, 8,
6221 "\x0c\x12\x18\x24\x30\x48\x60\x6c")))
6222 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006223 nla_nest_end(msg, band);
6224
6225 nla_nest_end(msg, bands);
6226
6227 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006228 if (ret) {
6229 wpa_printf(MSG_DEBUG, "nl80211: Set TX rates failed: ret=%d "
6230 "(%s)", ret, strerror(-ret));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006231 } else
6232 drv->disabled_11b_rates = disabled;
6233
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006234 return ret;
6235
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006236fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006237 nlmsg_free(msg);
6238 return -1;
6239}
6240
6241
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006242static int wpa_driver_nl80211_deinit_ap(void *priv)
6243{
6244 struct i802_bss *bss = priv;
6245 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006246 if (!is_ap_interface(drv->nlmode))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006247 return -1;
6248 wpa_driver_nl80211_del_beacon(drv);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006249 bss->beacon_set = 0;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006250
6251 /*
6252 * If the P2P GO interface was dynamically added, then it is
6253 * possible that the interface change to station is not possible.
6254 */
6255 if (drv->nlmode == NL80211_IFTYPE_P2P_GO && bss->if_dynamic)
6256 return 0;
6257
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006258 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006259}
6260
6261
Dmitry Shmidtea69e842013-05-13 14:52:28 -07006262static int wpa_driver_nl80211_stop_ap(void *priv)
6263{
6264 struct i802_bss *bss = priv;
6265 struct wpa_driver_nl80211_data *drv = bss->drv;
6266 if (!is_ap_interface(drv->nlmode))
6267 return -1;
6268 wpa_driver_nl80211_del_beacon(drv);
6269 bss->beacon_set = 0;
6270 return 0;
6271}
6272
6273
Dmitry Shmidt04949592012-07-19 12:16:46 -07006274static int wpa_driver_nl80211_deinit_p2p_cli(void *priv)
6275{
6276 struct i802_bss *bss = priv;
6277 struct wpa_driver_nl80211_data *drv = bss->drv;
6278 if (drv->nlmode != NL80211_IFTYPE_P2P_CLIENT)
6279 return -1;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006280
6281 /*
6282 * If the P2P Client interface was dynamically added, then it is
6283 * possible that the interface change to station is not possible.
6284 */
6285 if (bss->if_dynamic)
6286 return 0;
6287
Dmitry Shmidt04949592012-07-19 12:16:46 -07006288 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
6289}
6290
6291
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006292static void wpa_driver_nl80211_resume(void *priv)
6293{
6294 struct i802_bss *bss = priv;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006295
6296 if (i802_set_iface_flags(bss, 1))
6297 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface up on resume event");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006298}
6299
6300
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006301static int nl80211_signal_monitor(void *priv, int threshold, int hysteresis)
6302{
6303 struct i802_bss *bss = priv;
6304 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006305 struct nl_msg *msg;
6306 struct nlattr *cqm;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006307
6308 wpa_printf(MSG_DEBUG, "nl80211: Signal monitor threshold=%d "
6309 "hysteresis=%d", threshold, hysteresis);
6310
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006311 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_CQM)) ||
6312 !(cqm = nla_nest_start(msg, NL80211_ATTR_CQM)) ||
6313 nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THOLD, threshold) ||
6314 nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_HYST, hysteresis)) {
6315 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006316 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006317 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006318 nla_nest_end(msg, cqm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006319
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006320 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006321}
6322
6323
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006324static int get_channel_width(struct nl_msg *msg, void *arg)
6325{
6326 struct nlattr *tb[NL80211_ATTR_MAX + 1];
6327 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6328 struct wpa_signal_info *sig_change = arg;
6329
6330 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6331 genlmsg_attrlen(gnlh, 0), NULL);
6332
6333 sig_change->center_frq1 = -1;
6334 sig_change->center_frq2 = -1;
6335 sig_change->chanwidth = CHAN_WIDTH_UNKNOWN;
6336
6337 if (tb[NL80211_ATTR_CHANNEL_WIDTH]) {
6338 sig_change->chanwidth = convert2width(
6339 nla_get_u32(tb[NL80211_ATTR_CHANNEL_WIDTH]));
6340 if (tb[NL80211_ATTR_CENTER_FREQ1])
6341 sig_change->center_frq1 =
6342 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]);
6343 if (tb[NL80211_ATTR_CENTER_FREQ2])
6344 sig_change->center_frq2 =
6345 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]);
6346 }
6347
6348 return NL_SKIP;
6349}
6350
6351
6352static int nl80211_get_channel_width(struct wpa_driver_nl80211_data *drv,
6353 struct wpa_signal_info *sig)
6354{
6355 struct nl_msg *msg;
6356
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006357 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_GET_INTERFACE);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006358 return send_and_recv_msgs(drv, msg, get_channel_width, sig);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006359}
6360
6361
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006362static int nl80211_signal_poll(void *priv, struct wpa_signal_info *si)
6363{
6364 struct i802_bss *bss = priv;
6365 struct wpa_driver_nl80211_data *drv = bss->drv;
6366 int res;
6367
6368 os_memset(si, 0, sizeof(*si));
6369 res = nl80211_get_link_signal(drv, si);
6370 if (res != 0)
6371 return res;
6372
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006373 res = nl80211_get_channel_width(drv, si);
6374 if (res != 0)
6375 return res;
6376
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006377 return nl80211_get_link_noise(drv, si);
6378}
6379
6380
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006381static int wpa_driver_nl80211_shared_freq(void *priv)
6382{
6383 struct i802_bss *bss = priv;
6384 struct wpa_driver_nl80211_data *drv = bss->drv;
6385 struct wpa_driver_nl80211_data *driver;
6386 int freq = 0;
6387
6388 /*
6389 * If the same PHY is in connected state with some other interface,
6390 * then retrieve the assoc freq.
6391 */
6392 wpa_printf(MSG_DEBUG, "nl80211: Get shared freq for PHY %s",
6393 drv->phyname);
6394
6395 dl_list_for_each(driver, &drv->global->interfaces,
6396 struct wpa_driver_nl80211_data, list) {
6397 if (drv == driver ||
6398 os_strcmp(drv->phyname, driver->phyname) != 0 ||
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006399 !driver->associated)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006400 continue;
6401
6402 wpa_printf(MSG_DEBUG, "nl80211: Found a match for PHY %s - %s "
6403 MACSTR,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006404 driver->phyname, driver->first_bss->ifname,
6405 MAC2STR(driver->first_bss->addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07006406 if (is_ap_interface(driver->nlmode))
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006407 freq = driver->first_bss->freq;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006408 else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006409 freq = nl80211_get_assoc_freq(driver);
6410 wpa_printf(MSG_DEBUG, "nl80211: Shared freq for PHY %s: %d",
6411 drv->phyname, freq);
6412 }
6413
6414 if (!freq)
6415 wpa_printf(MSG_DEBUG, "nl80211: No shared interface for "
6416 "PHY (%s) in associated state", drv->phyname);
6417
6418 return freq;
6419}
6420
6421
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006422static int nl80211_send_frame(void *priv, const u8 *data, size_t data_len,
6423 int encrypt)
6424{
6425 struct i802_bss *bss = priv;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006426 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt, 0,
6427 0, 0, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006428}
6429
6430
6431static int nl80211_set_param(void *priv, const char *param)
6432{
6433 wpa_printf(MSG_DEBUG, "nl80211: driver param='%s'", param);
6434 if (param == NULL)
6435 return 0;
6436
6437#ifdef CONFIG_P2P
6438 if (os_strstr(param, "use_p2p_group_interface=1")) {
6439 struct i802_bss *bss = priv;
6440 struct wpa_driver_nl80211_data *drv = bss->drv;
6441
6442 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
6443 "interface");
6444 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
6445 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
6446 }
6447#endif /* CONFIG_P2P */
6448
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006449 if (os_strstr(param, "use_monitor=1")) {
6450 struct i802_bss *bss = priv;
6451 struct wpa_driver_nl80211_data *drv = bss->drv;
6452 drv->use_monitor = 1;
6453 }
6454
6455 if (os_strstr(param, "force_connect_cmd=1")) {
6456 struct i802_bss *bss = priv;
6457 struct wpa_driver_nl80211_data *drv = bss->drv;
6458 drv->capa.flags &= ~WPA_DRIVER_FLAGS_SME;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006459 drv->force_connect_cmd = 1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006460 }
6461
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08006462 if (os_strstr(param, "no_offchannel_tx=1")) {
6463 struct i802_bss *bss = priv;
6464 struct wpa_driver_nl80211_data *drv = bss->drv;
6465 drv->capa.flags &= ~WPA_DRIVER_FLAGS_OFFCHANNEL_TX;
6466 drv->test_use_roc_tx = 1;
6467 }
6468
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006469 return 0;
6470}
6471
6472
6473static void * nl80211_global_init(void)
6474{
6475 struct nl80211_global *global;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006476 struct netlink_config *cfg;
6477
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006478 global = os_zalloc(sizeof(*global));
6479 if (global == NULL)
6480 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006481 global->ioctl_sock = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006482 dl_list_init(&global->interfaces);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006483 global->if_add_ifindex = -1;
6484
6485 cfg = os_zalloc(sizeof(*cfg));
6486 if (cfg == NULL)
6487 goto err;
6488
6489 cfg->ctx = global;
6490 cfg->newlink_cb = wpa_driver_nl80211_event_rtm_newlink;
6491 cfg->dellink_cb = wpa_driver_nl80211_event_rtm_dellink;
6492 global->netlink = netlink_init(cfg);
6493 if (global->netlink == NULL) {
6494 os_free(cfg);
6495 goto err;
6496 }
6497
6498 if (wpa_driver_nl80211_init_nl_global(global) < 0)
6499 goto err;
6500
6501 global->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
6502 if (global->ioctl_sock < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006503 wpa_printf(MSG_ERROR, "nl80211: socket(PF_INET,SOCK_DGRAM) failed: %s",
6504 strerror(errno));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006505 goto err;
6506 }
6507
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006508 return global;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006509
6510err:
6511 nl80211_global_deinit(global);
6512 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006513}
6514
6515
6516static void nl80211_global_deinit(void *priv)
6517{
6518 struct nl80211_global *global = priv;
6519 if (global == NULL)
6520 return;
6521 if (!dl_list_empty(&global->interfaces)) {
6522 wpa_printf(MSG_ERROR, "nl80211: %u interface(s) remain at "
6523 "nl80211_global_deinit",
6524 dl_list_len(&global->interfaces));
6525 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006526
6527 if (global->netlink)
6528 netlink_deinit(global->netlink);
6529
6530 nl_destroy_handles(&global->nl);
6531
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006532 if (global->nl_event)
6533 nl80211_destroy_eloop_handle(&global->nl_event);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006534
6535 nl_cb_put(global->nl_cb);
6536
6537 if (global->ioctl_sock >= 0)
6538 close(global->ioctl_sock);
6539
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006540 os_free(global);
6541}
6542
6543
6544static const char * nl80211_get_radio_name(void *priv)
6545{
6546 struct i802_bss *bss = priv;
6547 struct wpa_driver_nl80211_data *drv = bss->drv;
6548 return drv->phyname;
6549}
6550
6551
Jouni Malinen75ecf522011-06-27 15:19:46 -07006552static int nl80211_pmkid(struct i802_bss *bss, int cmd, const u8 *bssid,
6553 const u8 *pmkid)
6554{
6555 struct nl_msg *msg;
6556
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006557 if (!(msg = nl80211_bss_msg(bss, 0, cmd)) ||
6558 (pmkid && nla_put(msg, NL80211_ATTR_PMKID, 16, pmkid)) ||
6559 (bssid && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))) {
6560 nlmsg_free(msg);
6561 return -ENOBUFS;
6562 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07006563
6564 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
Jouni Malinen75ecf522011-06-27 15:19:46 -07006565}
6566
6567
6568static int nl80211_add_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
6569{
6570 struct i802_bss *bss = priv;
6571 wpa_printf(MSG_DEBUG, "nl80211: Add PMKID for " MACSTR, MAC2STR(bssid));
6572 return nl80211_pmkid(bss, NL80211_CMD_SET_PMKSA, bssid, pmkid);
6573}
6574
6575
6576static int nl80211_remove_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
6577{
6578 struct i802_bss *bss = priv;
6579 wpa_printf(MSG_DEBUG, "nl80211: Delete PMKID for " MACSTR,
6580 MAC2STR(bssid));
6581 return nl80211_pmkid(bss, NL80211_CMD_DEL_PMKSA, bssid, pmkid);
6582}
6583
6584
6585static int nl80211_flush_pmkid(void *priv)
6586{
6587 struct i802_bss *bss = priv;
6588 wpa_printf(MSG_DEBUG, "nl80211: Flush PMKIDs");
6589 return nl80211_pmkid(bss, NL80211_CMD_FLUSH_PMKSA, NULL, NULL);
6590}
6591
6592
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006593static void clean_survey_results(struct survey_results *survey_results)
6594{
6595 struct freq_survey *survey, *tmp;
6596
6597 if (dl_list_empty(&survey_results->survey_list))
6598 return;
6599
6600 dl_list_for_each_safe(survey, tmp, &survey_results->survey_list,
6601 struct freq_survey, list) {
6602 dl_list_del(&survey->list);
6603 os_free(survey);
6604 }
6605}
6606
6607
6608static void add_survey(struct nlattr **sinfo, u32 ifidx,
6609 struct dl_list *survey_list)
6610{
6611 struct freq_survey *survey;
6612
6613 survey = os_zalloc(sizeof(struct freq_survey));
6614 if (!survey)
6615 return;
6616
6617 survey->ifidx = ifidx;
6618 survey->freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
6619 survey->filled = 0;
6620
6621 if (sinfo[NL80211_SURVEY_INFO_NOISE]) {
6622 survey->nf = (int8_t)
6623 nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
6624 survey->filled |= SURVEY_HAS_NF;
6625 }
6626
6627 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]) {
6628 survey->channel_time =
6629 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]);
6630 survey->filled |= SURVEY_HAS_CHAN_TIME;
6631 }
6632
6633 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]) {
6634 survey->channel_time_busy =
6635 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]);
6636 survey->filled |= SURVEY_HAS_CHAN_TIME_BUSY;
6637 }
6638
6639 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]) {
6640 survey->channel_time_rx =
6641 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]);
6642 survey->filled |= SURVEY_HAS_CHAN_TIME_RX;
6643 }
6644
6645 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]) {
6646 survey->channel_time_tx =
6647 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]);
6648 survey->filled |= SURVEY_HAS_CHAN_TIME_TX;
6649 }
6650
6651 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)",
6652 survey->freq,
6653 survey->nf,
6654 (unsigned long int) survey->channel_time,
6655 (unsigned long int) survey->channel_time_busy,
6656 (unsigned long int) survey->channel_time_tx,
6657 (unsigned long int) survey->channel_time_rx,
6658 survey->filled);
6659
6660 dl_list_add_tail(survey_list, &survey->list);
6661}
6662
6663
6664static int check_survey_ok(struct nlattr **sinfo, u32 surveyed_freq,
6665 unsigned int freq_filter)
6666{
6667 if (!freq_filter)
6668 return 1;
6669
6670 return freq_filter == surveyed_freq;
6671}
6672
6673
6674static int survey_handler(struct nl_msg *msg, void *arg)
6675{
6676 struct nlattr *tb[NL80211_ATTR_MAX + 1];
6677 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6678 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
6679 struct survey_results *survey_results;
6680 u32 surveyed_freq = 0;
6681 u32 ifidx;
6682
6683 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
6684 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
6685 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
6686 };
6687
6688 survey_results = (struct survey_results *) arg;
6689
6690 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6691 genlmsg_attrlen(gnlh, 0), NULL);
6692
Dmitry Shmidt97672262014-02-03 13:02:54 -08006693 if (!tb[NL80211_ATTR_IFINDEX])
6694 return NL_SKIP;
6695
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006696 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
6697
6698 if (!tb[NL80211_ATTR_SURVEY_INFO])
6699 return NL_SKIP;
6700
6701 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
6702 tb[NL80211_ATTR_SURVEY_INFO],
6703 survey_policy))
6704 return NL_SKIP;
6705
6706 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY]) {
6707 wpa_printf(MSG_ERROR, "nl80211: Invalid survey data");
6708 return NL_SKIP;
6709 }
6710
6711 surveyed_freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
6712
6713 if (!check_survey_ok(sinfo, surveyed_freq,
6714 survey_results->freq_filter))
6715 return NL_SKIP;
6716
6717 if (survey_results->freq_filter &&
6718 survey_results->freq_filter != surveyed_freq) {
6719 wpa_printf(MSG_EXCESSIVE, "nl80211: Ignoring survey data for freq %d MHz",
6720 surveyed_freq);
6721 return NL_SKIP;
6722 }
6723
6724 add_survey(sinfo, ifidx, &survey_results->survey_list);
6725
6726 return NL_SKIP;
6727}
6728
6729
6730static int wpa_driver_nl80211_get_survey(void *priv, unsigned int freq)
6731{
6732 struct i802_bss *bss = priv;
6733 struct wpa_driver_nl80211_data *drv = bss->drv;
6734 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006735 int err;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006736 union wpa_event_data data;
6737 struct survey_results *survey_results;
6738
6739 os_memset(&data, 0, sizeof(data));
6740 survey_results = &data.survey_results;
6741
6742 dl_list_init(&survey_results->survey_list);
6743
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006744 msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006745 if (!msg)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006746 return -ENOBUFS;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006747
6748 if (freq)
6749 data.survey_results.freq_filter = freq;
6750
6751 do {
6752 wpa_printf(MSG_DEBUG, "nl80211: Fetch survey data");
6753 err = send_and_recv_msgs(drv, msg, survey_handler,
6754 survey_results);
6755 } while (err > 0);
6756
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006757 if (err)
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006758 wpa_printf(MSG_ERROR, "nl80211: Failed to process survey data");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006759 else
6760 wpa_supplicant_event(drv->ctx, EVENT_SURVEY, &data);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006761
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006762 clean_survey_results(survey_results);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006763 return err;
6764}
6765
6766
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006767static void nl80211_set_rekey_info(void *priv, const u8 *kek, const u8 *kck,
6768 const u8 *replay_ctr)
6769{
6770 struct i802_bss *bss = priv;
6771 struct wpa_driver_nl80211_data *drv = bss->drv;
6772 struct nlattr *replay_nested;
6773 struct nl_msg *msg;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08006774 int ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006775
Dmitry Shmidtff787d52015-01-12 13:01:47 -08006776 if (!drv->set_rekey_offload)
6777 return;
6778
6779 wpa_printf(MSG_DEBUG, "nl80211: Set rekey offload");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006780 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_REKEY_OFFLOAD)) ||
6781 !(replay_nested = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA)) ||
6782 nla_put(msg, NL80211_REKEY_DATA_KEK, NL80211_KEK_LEN, kek) ||
6783 nla_put(msg, NL80211_REKEY_DATA_KCK, NL80211_KCK_LEN, kck) ||
6784 nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR, NL80211_REPLAY_CTR_LEN,
6785 replay_ctr)) {
6786 nl80211_nlmsg_clear(msg);
6787 nlmsg_free(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006788 return;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006789 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006790
6791 nla_nest_end(msg, replay_nested);
6792
Dmitry Shmidtff787d52015-01-12 13:01:47 -08006793 ret = send_and_recv_msgs(drv, msg, NULL, (void *) -1);
6794 if (ret == -EOPNOTSUPP) {
6795 wpa_printf(MSG_DEBUG,
6796 "nl80211: Driver does not support rekey offload");
6797 drv->set_rekey_offload = 0;
6798 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006799}
6800
6801
6802static void nl80211_send_null_frame(struct i802_bss *bss, const u8 *own_addr,
6803 const u8 *addr, int qos)
6804{
6805 /* send data frame to poll STA and check whether
6806 * this frame is ACKed */
6807 struct {
6808 struct ieee80211_hdr hdr;
6809 u16 qos_ctl;
6810 } STRUCT_PACKED nulldata;
6811 size_t size;
6812
6813 /* Send data frame to poll STA and check whether this frame is ACKed */
6814
6815 os_memset(&nulldata, 0, sizeof(nulldata));
6816
6817 if (qos) {
6818 nulldata.hdr.frame_control =
6819 IEEE80211_FC(WLAN_FC_TYPE_DATA,
6820 WLAN_FC_STYPE_QOS_NULL);
6821 size = sizeof(nulldata);
6822 } else {
6823 nulldata.hdr.frame_control =
6824 IEEE80211_FC(WLAN_FC_TYPE_DATA,
6825 WLAN_FC_STYPE_NULLFUNC);
6826 size = sizeof(struct ieee80211_hdr);
6827 }
6828
6829 nulldata.hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS);
6830 os_memcpy(nulldata.hdr.IEEE80211_DA_FROMDS, addr, ETH_ALEN);
6831 os_memcpy(nulldata.hdr.IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
6832 os_memcpy(nulldata.hdr.IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
6833
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006834 if (wpa_driver_nl80211_send_mlme(bss, (u8 *) &nulldata, size, 0, 0, 0,
6835 0, 0) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006836 wpa_printf(MSG_DEBUG, "nl80211_send_null_frame: Failed to "
6837 "send poll frame");
6838}
6839
6840static void nl80211_poll_client(void *priv, const u8 *own_addr, const u8 *addr,
6841 int qos)
6842{
6843 struct i802_bss *bss = priv;
6844 struct wpa_driver_nl80211_data *drv = bss->drv;
6845 struct nl_msg *msg;
6846
6847 if (!drv->poll_command_supported) {
6848 nl80211_send_null_frame(bss, own_addr, addr, qos);
6849 return;
6850 }
6851
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006852 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_PROBE_CLIENT)) ||
6853 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
6854 nlmsg_free(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006855 return;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006856 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006857
6858 send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006859}
6860
6861
6862static int nl80211_set_power_save(struct i802_bss *bss, int enabled)
6863{
6864 struct nl_msg *msg;
6865
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006866 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_POWER_SAVE)) ||
6867 nla_put_u32(msg, NL80211_ATTR_PS_STATE,
6868 enabled ? NL80211_PS_ENABLED : NL80211_PS_DISABLED)) {
6869 nlmsg_free(msg);
6870 return -ENOBUFS;
6871 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006872 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006873}
6874
6875
6876static int nl80211_set_p2p_powersave(void *priv, int legacy_ps, int opp_ps,
6877 int ctwindow)
6878{
6879 struct i802_bss *bss = priv;
6880
6881 wpa_printf(MSG_DEBUG, "nl80211: set_p2p_powersave (legacy_ps=%d "
6882 "opp_ps=%d ctwindow=%d)", legacy_ps, opp_ps, ctwindow);
6883
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08006884 if (opp_ps != -1 || ctwindow != -1) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006885#ifdef ANDROID_P2P
6886 wpa_driver_set_p2p_ps(priv, legacy_ps, opp_ps, ctwindow);
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08006887#else /* ANDROID_P2P */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006888 return -1; /* Not yet supported */
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08006889#endif /* ANDROID_P2P */
6890 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006891
6892 if (legacy_ps == -1)
6893 return 0;
6894 if (legacy_ps != 0 && legacy_ps != 1)
6895 return -1; /* Not yet supported */
6896
6897 return nl80211_set_power_save(bss, legacy_ps);
6898}
6899
6900
Dmitry Shmidt051af732013-10-22 13:52:46 -07006901static int nl80211_start_radar_detection(void *priv,
6902 struct hostapd_freq_params *freq)
Dmitry Shmidtea69e842013-05-13 14:52:28 -07006903{
6904 struct i802_bss *bss = priv;
6905 struct wpa_driver_nl80211_data *drv = bss->drv;
6906 struct nl_msg *msg;
6907 int ret;
6908
Dmitry Shmidt051af732013-10-22 13:52:46 -07006909 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)",
6910 freq->freq, freq->ht_enabled, freq->vht_enabled,
6911 freq->bandwidth, freq->center_freq1, freq->center_freq2);
6912
Dmitry Shmidtea69e842013-05-13 14:52:28 -07006913 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_RADAR)) {
6914 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support radar "
6915 "detection");
6916 return -1;
6917 }
6918
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006919 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_RADAR_DETECT)) ||
6920 nl80211_put_freq_params(msg, freq) < 0) {
6921 nlmsg_free(msg);
Dmitry Shmidtea69e842013-05-13 14:52:28 -07006922 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006923 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -07006924
6925 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6926 if (ret == 0)
6927 return 0;
6928 wpa_printf(MSG_DEBUG, "nl80211: Failed to start radar detection: "
6929 "%d (%s)", ret, strerror(-ret));
Dmitry Shmidtea69e842013-05-13 14:52:28 -07006930 return -1;
6931}
6932
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006933#ifdef CONFIG_TDLS
6934
6935static int nl80211_send_tdls_mgmt(void *priv, const u8 *dst, u8 action_code,
6936 u8 dialog_token, u16 status_code,
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07006937 u32 peer_capab, int initiator, const u8 *buf,
6938 size_t len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006939{
6940 struct i802_bss *bss = priv;
6941 struct wpa_driver_nl80211_data *drv = bss->drv;
6942 struct nl_msg *msg;
6943
6944 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
6945 return -EOPNOTSUPP;
6946
6947 if (!dst)
6948 return -EINVAL;
6949
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006950 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_TDLS_MGMT)) ||
6951 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
6952 nla_put_u8(msg, NL80211_ATTR_TDLS_ACTION, action_code) ||
6953 nla_put_u8(msg, NL80211_ATTR_TDLS_DIALOG_TOKEN, dialog_token) ||
6954 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status_code))
6955 goto fail;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006956 if (peer_capab) {
6957 /*
6958 * The internal enum tdls_peer_capability definition is
6959 * currently identical with the nl80211 enum
6960 * nl80211_tdls_peer_capability, so no conversion is needed
6961 * here.
6962 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006963 if (nla_put_u32(msg, NL80211_ATTR_TDLS_PEER_CAPABILITY,
6964 peer_capab))
6965 goto fail;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006966 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006967 if ((initiator &&
6968 nla_put_flag(msg, NL80211_ATTR_TDLS_INITIATOR)) ||
6969 nla_put(msg, NL80211_ATTR_IE, len, buf))
6970 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006971
6972 return send_and_recv_msgs(drv, msg, NULL, NULL);
6973
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006974fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006975 nlmsg_free(msg);
6976 return -ENOBUFS;
6977}
6978
6979
6980static int nl80211_tdls_oper(void *priv, enum tdls_oper oper, const u8 *peer)
6981{
6982 struct i802_bss *bss = priv;
6983 struct wpa_driver_nl80211_data *drv = bss->drv;
6984 struct nl_msg *msg;
6985 enum nl80211_tdls_operation nl80211_oper;
6986
6987 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
6988 return -EOPNOTSUPP;
6989
6990 switch (oper) {
6991 case TDLS_DISCOVERY_REQ:
6992 nl80211_oper = NL80211_TDLS_DISCOVERY_REQ;
6993 break;
6994 case TDLS_SETUP:
6995 nl80211_oper = NL80211_TDLS_SETUP;
6996 break;
6997 case TDLS_TEARDOWN:
6998 nl80211_oper = NL80211_TDLS_TEARDOWN;
6999 break;
7000 case TDLS_ENABLE_LINK:
7001 nl80211_oper = NL80211_TDLS_ENABLE_LINK;
7002 break;
7003 case TDLS_DISABLE_LINK:
7004 nl80211_oper = NL80211_TDLS_DISABLE_LINK;
7005 break;
7006 case TDLS_ENABLE:
7007 return 0;
7008 case TDLS_DISABLE:
7009 return 0;
7010 default:
7011 return -EINVAL;
7012 }
7013
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007014 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_TDLS_OPER)) ||
7015 nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, nl80211_oper) ||
7016 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer)) {
7017 nlmsg_free(msg);
7018 return -ENOBUFS;
7019 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007020
7021 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007022}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007023
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007024
7025static int
7026nl80211_tdls_enable_channel_switch(void *priv, const u8 *addr, u8 oper_class,
7027 const struct hostapd_freq_params *params)
7028{
7029 struct i802_bss *bss = priv;
7030 struct wpa_driver_nl80211_data *drv = bss->drv;
7031 struct nl_msg *msg;
7032 int ret = -ENOBUFS;
7033
7034 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT) ||
7035 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_CHANNEL_SWITCH))
7036 return -EOPNOTSUPP;
7037
7038 wpa_printf(MSG_DEBUG, "nl80211: Enable TDLS channel switch " MACSTR
7039 " oper_class=%u freq=%u",
7040 MAC2STR(addr), oper_class, params->freq);
7041 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_TDLS_CHANNEL_SWITCH);
7042 if (!msg ||
7043 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
7044 nla_put_u8(msg, NL80211_ATTR_OPER_CLASS, oper_class) ||
7045 (ret = nl80211_put_freq_params(msg, params))) {
7046 nlmsg_free(msg);
7047 wpa_printf(MSG_DEBUG, "nl80211: Could not build TDLS chan switch");
7048 return ret;
7049 }
7050
7051 return send_and_recv_msgs(drv, msg, NULL, NULL);
7052}
7053
7054
7055static int
7056nl80211_tdls_disable_channel_switch(void *priv, const u8 *addr)
7057{
7058 struct i802_bss *bss = priv;
7059 struct wpa_driver_nl80211_data *drv = bss->drv;
7060 struct nl_msg *msg;
7061
7062 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT) ||
7063 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_CHANNEL_SWITCH))
7064 return -EOPNOTSUPP;
7065
7066 wpa_printf(MSG_DEBUG, "nl80211: Disable TDLS channel switch " MACSTR,
7067 MAC2STR(addr));
7068 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_TDLS_CANCEL_CHANNEL_SWITCH);
7069 if (!msg ||
7070 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
7071 nlmsg_free(msg);
7072 wpa_printf(MSG_DEBUG,
7073 "nl80211: Could not build TDLS cancel chan switch");
7074 return -ENOBUFS;
7075 }
7076
7077 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007078}
7079
7080#endif /* CONFIG TDLS */
7081
7082
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007083static int driver_nl80211_set_key(const char *ifname, void *priv,
7084 enum wpa_alg alg, const u8 *addr,
7085 int key_idx, int set_tx,
7086 const u8 *seq, size_t seq_len,
7087 const u8 *key, size_t key_len)
7088{
7089 struct i802_bss *bss = priv;
7090 return wpa_driver_nl80211_set_key(ifname, bss, alg, addr, key_idx,
7091 set_tx, seq, seq_len, key, key_len);
7092}
7093
7094
7095static int driver_nl80211_scan2(void *priv,
7096 struct wpa_driver_scan_params *params)
7097{
7098 struct i802_bss *bss = priv;
7099 return wpa_driver_nl80211_scan(bss, params);
7100}
7101
7102
7103static int driver_nl80211_deauthenticate(void *priv, const u8 *addr,
7104 int reason_code)
7105{
7106 struct i802_bss *bss = priv;
7107 return wpa_driver_nl80211_deauthenticate(bss, addr, reason_code);
7108}
7109
7110
7111static int driver_nl80211_authenticate(void *priv,
7112 struct wpa_driver_auth_params *params)
7113{
7114 struct i802_bss *bss = priv;
7115 return wpa_driver_nl80211_authenticate(bss, params);
7116}
7117
7118
7119static void driver_nl80211_deinit(void *priv)
7120{
7121 struct i802_bss *bss = priv;
7122 wpa_driver_nl80211_deinit(bss);
7123}
7124
7125
7126static int driver_nl80211_if_remove(void *priv, enum wpa_driver_if_type type,
7127 const char *ifname)
7128{
7129 struct i802_bss *bss = priv;
7130 return wpa_driver_nl80211_if_remove(bss, type, ifname);
7131}
7132
7133
7134static int driver_nl80211_send_mlme(void *priv, const u8 *data,
7135 size_t data_len, int noack)
7136{
7137 struct i802_bss *bss = priv;
7138 return wpa_driver_nl80211_send_mlme(bss, data, data_len, noack,
7139 0, 0, 0, 0);
7140}
7141
7142
7143static int driver_nl80211_sta_remove(void *priv, const u8 *addr)
7144{
7145 struct i802_bss *bss = priv;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007146 return wpa_driver_nl80211_sta_remove(bss, addr, -1, 0);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007147}
7148
7149
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007150static int driver_nl80211_set_sta_vlan(void *priv, const u8 *addr,
7151 const char *ifname, int vlan_id)
7152{
7153 struct i802_bss *bss = priv;
7154 return i802_set_sta_vlan(bss, addr, ifname, vlan_id);
7155}
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007156
7157
7158static int driver_nl80211_read_sta_data(void *priv,
7159 struct hostap_sta_driver_data *data,
7160 const u8 *addr)
7161{
7162 struct i802_bss *bss = priv;
7163 return i802_read_sta_data(bss, data, addr);
7164}
7165
7166
7167static int driver_nl80211_send_action(void *priv, unsigned int freq,
7168 unsigned int wait_time,
7169 const u8 *dst, const u8 *src,
7170 const u8 *bssid,
7171 const u8 *data, size_t data_len,
7172 int no_cck)
7173{
7174 struct i802_bss *bss = priv;
7175 return wpa_driver_nl80211_send_action(bss, freq, wait_time, dst, src,
7176 bssid, data, data_len, no_cck);
7177}
7178
7179
7180static int driver_nl80211_probe_req_report(void *priv, int report)
7181{
7182 struct i802_bss *bss = priv;
7183 return wpa_driver_nl80211_probe_req_report(bss, report);
7184}
7185
7186
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007187static int wpa_driver_nl80211_update_ft_ies(void *priv, const u8 *md,
7188 const u8 *ies, size_t ies_len)
7189{
7190 int ret;
7191 struct nl_msg *msg;
7192 struct i802_bss *bss = priv;
7193 struct wpa_driver_nl80211_data *drv = bss->drv;
7194 u16 mdid = WPA_GET_LE16(md);
7195
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007196 wpa_printf(MSG_DEBUG, "nl80211: Updating FT IEs");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007197 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_UPDATE_FT_IES)) ||
7198 nla_put(msg, NL80211_ATTR_IE, ies_len, ies) ||
7199 nla_put_u16(msg, NL80211_ATTR_MDID, mdid)) {
7200 nlmsg_free(msg);
7201 return -ENOBUFS;
7202 }
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007203
7204 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7205 if (ret) {
7206 wpa_printf(MSG_DEBUG, "nl80211: update_ft_ies failed "
7207 "err=%d (%s)", ret, strerror(-ret));
7208 }
7209
7210 return ret;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007211}
7212
7213
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007214const u8 * wpa_driver_nl80211_get_macaddr(void *priv)
7215{
7216 struct i802_bss *bss = priv;
7217 struct wpa_driver_nl80211_data *drv = bss->drv;
7218
7219 if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE)
7220 return NULL;
7221
7222 return bss->addr;
7223}
7224
7225
Dmitry Shmidt56052862013-10-04 10:23:25 -07007226static const char * scan_state_str(enum scan_states scan_state)
7227{
7228 switch (scan_state) {
7229 case NO_SCAN:
7230 return "NO_SCAN";
7231 case SCAN_REQUESTED:
7232 return "SCAN_REQUESTED";
7233 case SCAN_STARTED:
7234 return "SCAN_STARTED";
7235 case SCAN_COMPLETED:
7236 return "SCAN_COMPLETED";
7237 case SCAN_ABORTED:
7238 return "SCAN_ABORTED";
7239 case SCHED_SCAN_STARTED:
7240 return "SCHED_SCAN_STARTED";
7241 case SCHED_SCAN_STOPPED:
7242 return "SCHED_SCAN_STOPPED";
7243 case SCHED_SCAN_RESULTS:
7244 return "SCHED_SCAN_RESULTS";
7245 }
7246
7247 return "??";
7248}
7249
7250
7251static int wpa_driver_nl80211_status(void *priv, char *buf, size_t buflen)
7252{
7253 struct i802_bss *bss = priv;
7254 struct wpa_driver_nl80211_data *drv = bss->drv;
7255 int res;
7256 char *pos, *end;
7257
7258 pos = buf;
7259 end = buf + buflen;
7260
7261 res = os_snprintf(pos, end - pos,
7262 "ifindex=%d\n"
7263 "ifname=%s\n"
7264 "brname=%s\n"
7265 "addr=" MACSTR "\n"
7266 "freq=%d\n"
7267 "%s%s%s%s%s",
7268 bss->ifindex,
7269 bss->ifname,
7270 bss->brname,
7271 MAC2STR(bss->addr),
7272 bss->freq,
7273 bss->beacon_set ? "beacon_set=1\n" : "",
7274 bss->added_if_into_bridge ?
7275 "added_if_into_bridge=1\n" : "",
7276 bss->added_bridge ? "added_bridge=1\n" : "",
7277 bss->in_deinit ? "in_deinit=1\n" : "",
7278 bss->if_dynamic ? "if_dynamic=1\n" : "");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007279 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt56052862013-10-04 10:23:25 -07007280 return pos - buf;
7281 pos += res;
7282
7283 if (bss->wdev_id_set) {
7284 res = os_snprintf(pos, end - pos, "wdev_id=%llu\n",
7285 (unsigned long long) bss->wdev_id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007286 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt56052862013-10-04 10:23:25 -07007287 return pos - buf;
7288 pos += res;
7289 }
7290
7291 res = os_snprintf(pos, end - pos,
7292 "phyname=%s\n"
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007293 "perm_addr=" MACSTR "\n"
Dmitry Shmidt56052862013-10-04 10:23:25 -07007294 "drv_ifindex=%d\n"
7295 "operstate=%d\n"
7296 "scan_state=%s\n"
7297 "auth_bssid=" MACSTR "\n"
7298 "auth_attempt_bssid=" MACSTR "\n"
7299 "bssid=" MACSTR "\n"
7300 "prev_bssid=" MACSTR "\n"
7301 "associated=%d\n"
7302 "assoc_freq=%u\n"
7303 "monitor_sock=%d\n"
7304 "monitor_ifidx=%d\n"
7305 "monitor_refcount=%d\n"
7306 "last_mgmt_freq=%u\n"
7307 "eapol_tx_sock=%d\n"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007308 "%s%s%s%s%s%s%s%s%s%s%s%s%s",
Dmitry Shmidt56052862013-10-04 10:23:25 -07007309 drv->phyname,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007310 MAC2STR(drv->perm_addr),
Dmitry Shmidt56052862013-10-04 10:23:25 -07007311 drv->ifindex,
7312 drv->operstate,
7313 scan_state_str(drv->scan_state),
7314 MAC2STR(drv->auth_bssid),
7315 MAC2STR(drv->auth_attempt_bssid),
7316 MAC2STR(drv->bssid),
7317 MAC2STR(drv->prev_bssid),
7318 drv->associated,
7319 drv->assoc_freq,
7320 drv->monitor_sock,
7321 drv->monitor_ifidx,
7322 drv->monitor_refcount,
7323 drv->last_mgmt_freq,
7324 drv->eapol_tx_sock,
7325 drv->ignore_if_down_event ?
7326 "ignore_if_down_event=1\n" : "",
7327 drv->scan_complete_events ?
7328 "scan_complete_events=1\n" : "",
7329 drv->disabled_11b_rates ?
7330 "disabled_11b_rates=1\n" : "",
7331 drv->pending_remain_on_chan ?
7332 "pending_remain_on_chan=1\n" : "",
7333 drv->in_interface_list ? "in_interface_list=1\n" : "",
7334 drv->device_ap_sme ? "device_ap_sme=1\n" : "",
7335 drv->poll_command_supported ?
7336 "poll_command_supported=1\n" : "",
7337 drv->data_tx_status ? "data_tx_status=1\n" : "",
7338 drv->scan_for_auth ? "scan_for_auth=1\n" : "",
7339 drv->retry_auth ? "retry_auth=1\n" : "",
7340 drv->use_monitor ? "use_monitor=1\n" : "",
7341 drv->ignore_next_local_disconnect ?
7342 "ignore_next_local_disconnect=1\n" : "",
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07007343 drv->ignore_next_local_deauth ?
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007344 "ignore_next_local_deauth=1\n" : "");
7345 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt56052862013-10-04 10:23:25 -07007346 return pos - buf;
7347 pos += res;
7348
7349 if (drv->has_capability) {
7350 res = os_snprintf(pos, end - pos,
7351 "capa.key_mgmt=0x%x\n"
7352 "capa.enc=0x%x\n"
7353 "capa.auth=0x%x\n"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007354 "capa.flags=0x%llx\n"
7355 "capa.rrm_flags=0x%x\n"
Dmitry Shmidt56052862013-10-04 10:23:25 -07007356 "capa.max_scan_ssids=%d\n"
7357 "capa.max_sched_scan_ssids=%d\n"
7358 "capa.sched_scan_supported=%d\n"
7359 "capa.max_match_sets=%d\n"
7360 "capa.max_remain_on_chan=%u\n"
7361 "capa.max_stations=%u\n"
7362 "capa.probe_resp_offloads=0x%x\n"
7363 "capa.max_acl_mac_addrs=%u\n"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007364 "capa.num_multichan_concurrent=%u\n"
7365 "capa.mac_addr_rand_sched_scan_supported=%d\n"
7366 "capa.mac_addr_rand_scan_supported=%d\n",
Dmitry Shmidt56052862013-10-04 10:23:25 -07007367 drv->capa.key_mgmt,
7368 drv->capa.enc,
7369 drv->capa.auth,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007370 (unsigned long long) drv->capa.flags,
7371 drv->capa.rrm_flags,
Dmitry Shmidt56052862013-10-04 10:23:25 -07007372 drv->capa.max_scan_ssids,
7373 drv->capa.max_sched_scan_ssids,
7374 drv->capa.sched_scan_supported,
7375 drv->capa.max_match_sets,
7376 drv->capa.max_remain_on_chan,
7377 drv->capa.max_stations,
7378 drv->capa.probe_resp_offloads,
7379 drv->capa.max_acl_mac_addrs,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007380 drv->capa.num_multichan_concurrent,
7381 drv->capa.mac_addr_rand_sched_scan_supported,
7382 drv->capa.mac_addr_rand_scan_supported);
7383 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt56052862013-10-04 10:23:25 -07007384 return pos - buf;
7385 pos += res;
7386 }
7387
7388 return pos - buf;
7389}
7390
7391
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007392static int set_beacon_data(struct nl_msg *msg, struct beacon_data *settings)
7393{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007394 if ((settings->head &&
7395 nla_put(msg, NL80211_ATTR_BEACON_HEAD,
7396 settings->head_len, settings->head)) ||
7397 (settings->tail &&
7398 nla_put(msg, NL80211_ATTR_BEACON_TAIL,
7399 settings->tail_len, settings->tail)) ||
7400 (settings->beacon_ies &&
7401 nla_put(msg, NL80211_ATTR_IE,
7402 settings->beacon_ies_len, settings->beacon_ies)) ||
7403 (settings->proberesp_ies &&
7404 nla_put(msg, NL80211_ATTR_IE_PROBE_RESP,
7405 settings->proberesp_ies_len, settings->proberesp_ies)) ||
7406 (settings->assocresp_ies &&
7407 nla_put(msg, NL80211_ATTR_IE_ASSOC_RESP,
7408 settings->assocresp_ies_len, settings->assocresp_ies)) ||
7409 (settings->probe_resp &&
7410 nla_put(msg, NL80211_ATTR_PROBE_RESP,
7411 settings->probe_resp_len, settings->probe_resp)))
7412 return -ENOBUFS;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007413
7414 return 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007415}
7416
7417
7418static int nl80211_switch_channel(void *priv, struct csa_settings *settings)
7419{
7420 struct nl_msg *msg;
7421 struct i802_bss *bss = priv;
7422 struct wpa_driver_nl80211_data *drv = bss->drv;
7423 struct nlattr *beacon_csa;
7424 int ret = -ENOBUFS;
7425
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08007426 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 -08007427 settings->cs_count, settings->block_tx,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08007428 settings->freq_params.freq, settings->freq_params.bandwidth,
7429 settings->freq_params.center_freq1,
7430 settings->freq_params.center_freq2);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007431
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007432 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_AP_CSA)) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007433 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support channel switch command");
7434 return -EOPNOTSUPP;
7435 }
7436
7437 if ((drv->nlmode != NL80211_IFTYPE_AP) &&
7438 (drv->nlmode != NL80211_IFTYPE_P2P_GO))
7439 return -EOPNOTSUPP;
7440
7441 /* check settings validity */
7442 if (!settings->beacon_csa.tail ||
7443 ((settings->beacon_csa.tail_len <=
7444 settings->counter_offset_beacon) ||
7445 (settings->beacon_csa.tail[settings->counter_offset_beacon] !=
7446 settings->cs_count)))
7447 return -EINVAL;
7448
7449 if (settings->beacon_csa.probe_resp &&
7450 ((settings->beacon_csa.probe_resp_len <=
7451 settings->counter_offset_presp) ||
7452 (settings->beacon_csa.probe_resp[settings->counter_offset_presp] !=
7453 settings->cs_count)))
7454 return -EINVAL;
7455
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007456 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_CHANNEL_SWITCH)) ||
7457 nla_put_u32(msg, NL80211_ATTR_CH_SWITCH_COUNT,
7458 settings->cs_count) ||
7459 (ret = nl80211_put_freq_params(msg, &settings->freq_params)) ||
7460 (settings->block_tx &&
7461 nla_put_flag(msg, NL80211_ATTR_CH_SWITCH_BLOCK_TX)))
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007462 goto error;
7463
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007464 /* beacon_after params */
7465 ret = set_beacon_data(msg, &settings->beacon_after);
7466 if (ret)
7467 goto error;
7468
7469 /* beacon_csa params */
7470 beacon_csa = nla_nest_start(msg, NL80211_ATTR_CSA_IES);
7471 if (!beacon_csa)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007472 goto fail;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007473
7474 ret = set_beacon_data(msg, &settings->beacon_csa);
7475 if (ret)
7476 goto error;
7477
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007478 if (nla_put_u16(msg, NL80211_ATTR_CSA_C_OFF_BEACON,
7479 settings->counter_offset_beacon) ||
7480 (settings->beacon_csa.probe_resp &&
7481 nla_put_u16(msg, NL80211_ATTR_CSA_C_OFF_PRESP,
7482 settings->counter_offset_presp)))
7483 goto fail;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007484
7485 nla_nest_end(msg, beacon_csa);
7486 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7487 if (ret) {
7488 wpa_printf(MSG_DEBUG, "nl80211: switch_channel failed err=%d (%s)",
7489 ret, strerror(-ret));
7490 }
7491 return ret;
7492
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007493fail:
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007494 ret = -ENOBUFS;
7495error:
7496 nlmsg_free(msg);
7497 wpa_printf(MSG_DEBUG, "nl80211: Could not build channel switch request");
7498 return ret;
7499}
7500
7501
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007502static int nl80211_add_ts(void *priv, u8 tsid, const u8 *addr,
7503 u8 user_priority, u16 admitted_time)
7504{
7505 struct i802_bss *bss = priv;
7506 struct wpa_driver_nl80211_data *drv = bss->drv;
7507 struct nl_msg *msg;
7508 int ret;
7509
7510 wpa_printf(MSG_DEBUG,
7511 "nl80211: add_ts request: tsid=%u admitted_time=%u up=%d",
7512 tsid, admitted_time, user_priority);
7513
7514 if (!is_sta_interface(drv->nlmode))
7515 return -ENOTSUP;
7516
7517 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_ADD_TX_TS);
7518 if (!msg ||
7519 nla_put_u8(msg, NL80211_ATTR_TSID, tsid) ||
7520 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
7521 nla_put_u8(msg, NL80211_ATTR_USER_PRIO, user_priority) ||
7522 nla_put_u16(msg, NL80211_ATTR_ADMITTED_TIME, admitted_time)) {
7523 nlmsg_free(msg);
7524 return -ENOBUFS;
7525 }
7526
7527 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7528 if (ret)
7529 wpa_printf(MSG_DEBUG, "nl80211: add_ts failed err=%d (%s)",
7530 ret, strerror(-ret));
7531 return ret;
7532}
7533
7534
7535static int nl80211_del_ts(void *priv, u8 tsid, const u8 *addr)
7536{
7537 struct i802_bss *bss = priv;
7538 struct wpa_driver_nl80211_data *drv = bss->drv;
7539 struct nl_msg *msg;
7540 int ret;
7541
7542 wpa_printf(MSG_DEBUG, "nl80211: del_ts request: tsid=%u", tsid);
7543
7544 if (!is_sta_interface(drv->nlmode))
7545 return -ENOTSUP;
7546
7547 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_DEL_TX_TS)) ||
7548 nla_put_u8(msg, NL80211_ATTR_TSID, tsid) ||
7549 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
7550 nlmsg_free(msg);
7551 return -ENOBUFS;
7552 }
7553
7554 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7555 if (ret)
7556 wpa_printf(MSG_DEBUG, "nl80211: del_ts failed err=%d (%s)",
7557 ret, strerror(-ret));
7558 return ret;
7559}
7560
7561
Dmitry Shmidta38abf92014-03-06 13:38:44 -08007562#ifdef CONFIG_TESTING_OPTIONS
7563static int cmd_reply_handler(struct nl_msg *msg, void *arg)
7564{
7565 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
7566 struct wpabuf *buf = arg;
7567
7568 if (!buf)
7569 return NL_SKIP;
7570
7571 if ((size_t) genlmsg_attrlen(gnlh, 0) > wpabuf_tailroom(buf)) {
7572 wpa_printf(MSG_INFO, "nl80211: insufficient buffer space for reply");
7573 return NL_SKIP;
7574 }
7575
7576 wpabuf_put_data(buf, genlmsg_attrdata(gnlh, 0),
7577 genlmsg_attrlen(gnlh, 0));
7578
7579 return NL_SKIP;
7580}
7581#endif /* CONFIG_TESTING_OPTIONS */
7582
7583
7584static int vendor_reply_handler(struct nl_msg *msg, void *arg)
7585{
7586 struct nlattr *tb[NL80211_ATTR_MAX + 1];
7587 struct nlattr *nl_vendor_reply, *nl;
7588 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
7589 struct wpabuf *buf = arg;
7590 int rem;
7591
7592 if (!buf)
7593 return NL_SKIP;
7594
7595 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
7596 genlmsg_attrlen(gnlh, 0), NULL);
7597 nl_vendor_reply = tb[NL80211_ATTR_VENDOR_DATA];
7598
7599 if (!nl_vendor_reply)
7600 return NL_SKIP;
7601
7602 if ((size_t) nla_len(nl_vendor_reply) > wpabuf_tailroom(buf)) {
7603 wpa_printf(MSG_INFO, "nl80211: Vendor command: insufficient buffer space for reply");
7604 return NL_SKIP;
7605 }
7606
7607 nla_for_each_nested(nl, nl_vendor_reply, rem) {
7608 wpabuf_put_data(buf, nla_data(nl), nla_len(nl));
7609 }
7610
7611 return NL_SKIP;
7612}
7613
7614
7615static int nl80211_vendor_cmd(void *priv, unsigned int vendor_id,
7616 unsigned int subcmd, const u8 *data,
7617 size_t data_len, struct wpabuf *buf)
7618{
7619 struct i802_bss *bss = priv;
7620 struct wpa_driver_nl80211_data *drv = bss->drv;
7621 struct nl_msg *msg;
7622 int ret;
7623
Dmitry Shmidta38abf92014-03-06 13:38:44 -08007624#ifdef CONFIG_TESTING_OPTIONS
7625 if (vendor_id == 0xffffffff) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007626 msg = nlmsg_alloc();
7627 if (!msg)
7628 return -ENOMEM;
7629
Dmitry Shmidta38abf92014-03-06 13:38:44 -08007630 nl80211_cmd(drv, msg, 0, subcmd);
7631 if (nlmsg_append(msg, (void *) data, data_len, NLMSG_ALIGNTO) <
7632 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007633 goto fail;
Dmitry Shmidta38abf92014-03-06 13:38:44 -08007634 ret = send_and_recv_msgs(drv, msg, cmd_reply_handler, buf);
7635 if (ret)
7636 wpa_printf(MSG_DEBUG, "nl80211: command failed err=%d",
7637 ret);
7638 return ret;
7639 }
7640#endif /* CONFIG_TESTING_OPTIONS */
7641
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007642 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_VENDOR)) ||
7643 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, vendor_id) ||
7644 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD, subcmd) ||
7645 (data &&
7646 nla_put(msg, NL80211_ATTR_VENDOR_DATA, data_len, data)))
7647 goto fail;
Dmitry Shmidta38abf92014-03-06 13:38:44 -08007648
7649 ret = send_and_recv_msgs(drv, msg, vendor_reply_handler, buf);
7650 if (ret)
7651 wpa_printf(MSG_DEBUG, "nl80211: vendor command failed err=%d",
7652 ret);
7653 return ret;
7654
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007655fail:
Dmitry Shmidta38abf92014-03-06 13:38:44 -08007656 nlmsg_free(msg);
7657 return -ENOBUFS;
7658}
7659
7660
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007661static int nl80211_set_qos_map(void *priv, const u8 *qos_map_set,
7662 u8 qos_map_set_len)
7663{
7664 struct i802_bss *bss = priv;
7665 struct wpa_driver_nl80211_data *drv = bss->drv;
7666 struct nl_msg *msg;
7667 int ret;
7668
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007669 wpa_hexdump(MSG_DEBUG, "nl80211: Setting QoS Map",
7670 qos_map_set, qos_map_set_len);
7671
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007672 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_QOS_MAP)) ||
7673 nla_put(msg, NL80211_ATTR_QOS_MAP, qos_map_set_len, qos_map_set)) {
7674 nlmsg_free(msg);
7675 return -ENOBUFS;
7676 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007677
7678 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7679 if (ret)
7680 wpa_printf(MSG_DEBUG, "nl80211: Setting QoS Map failed");
7681
7682 return ret;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007683}
7684
7685
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07007686static int nl80211_set_wowlan(void *priv,
7687 const struct wowlan_triggers *triggers)
7688{
7689 struct i802_bss *bss = priv;
7690 struct wpa_driver_nl80211_data *drv = bss->drv;
7691 struct nl_msg *msg;
7692 struct nlattr *wowlan_triggers;
7693 int ret;
7694
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07007695 wpa_printf(MSG_DEBUG, "nl80211: Setting wowlan");
7696
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007697 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_WOWLAN)) ||
7698 !(wowlan_triggers = nla_nest_start(msg,
7699 NL80211_ATTR_WOWLAN_TRIGGERS)) ||
7700 (triggers->any &&
7701 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
7702 (triggers->disconnect &&
7703 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
7704 (triggers->magic_pkt &&
7705 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
7706 (triggers->gtk_rekey_failure &&
7707 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
7708 (triggers->eap_identity_req &&
7709 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
7710 (triggers->four_way_handshake &&
7711 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
7712 (triggers->rfkill_release &&
7713 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))) {
7714 nlmsg_free(msg);
7715 return -ENOBUFS;
7716 }
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07007717
7718 nla_nest_end(msg, wowlan_triggers);
7719
7720 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7721 if (ret)
7722 wpa_printf(MSG_DEBUG, "nl80211: Setting wowlan failed");
7723
7724 return ret;
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07007725}
7726
7727
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007728static int nl80211_roaming(void *priv, int allowed, const u8 *bssid)
7729{
7730 struct i802_bss *bss = priv;
7731 struct wpa_driver_nl80211_data *drv = bss->drv;
7732 struct nl_msg *msg;
7733 struct nlattr *params;
7734
7735 wpa_printf(MSG_DEBUG, "nl80211: Roaming policy: allowed=%d", allowed);
7736
7737 if (!drv->roaming_vendor_cmd_avail) {
7738 wpa_printf(MSG_DEBUG,
7739 "nl80211: Ignore roaming policy change since driver does not provide command for setting it");
7740 return -1;
7741 }
7742
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007743 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
7744 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
7745 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
7746 QCA_NL80211_VENDOR_SUBCMD_ROAMING) ||
7747 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
7748 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_ROAMING_POLICY,
7749 allowed ? QCA_ROAMING_ALLOWED_WITHIN_ESS :
7750 QCA_ROAMING_NOT_ALLOWED) ||
7751 (bssid &&
7752 nla_put(msg, QCA_WLAN_VENDOR_ATTR_MAC_ADDR, ETH_ALEN, bssid))) {
7753 nlmsg_free(msg);
7754 return -1;
7755 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007756 nla_nest_end(msg, params);
7757
7758 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007759}
7760
7761
7762static int nl80211_set_mac_addr(void *priv, const u8 *addr)
7763{
7764 struct i802_bss *bss = priv;
7765 struct wpa_driver_nl80211_data *drv = bss->drv;
7766 int new_addr = addr != NULL;
7767
7768 if (!addr)
7769 addr = drv->perm_addr;
7770
7771 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0) < 0)
7772 return -1;
7773
7774 if (linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname, addr) < 0)
7775 {
7776 wpa_printf(MSG_DEBUG,
7777 "nl80211: failed to set_mac_addr for %s to " MACSTR,
7778 bss->ifname, MAC2STR(addr));
7779 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname,
7780 1) < 0) {
7781 wpa_printf(MSG_DEBUG,
7782 "nl80211: Could not restore interface UP after failed set_mac_addr");
7783 }
7784 return -1;
7785 }
7786
7787 wpa_printf(MSG_DEBUG, "nl80211: set_mac_addr for %s to " MACSTR,
7788 bss->ifname, MAC2STR(addr));
7789 drv->addr_changed = new_addr;
7790 os_memcpy(bss->addr, addr, ETH_ALEN);
7791
7792 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1) < 0)
7793 {
7794 wpa_printf(MSG_DEBUG,
7795 "nl80211: Could not restore interface UP after set_mac_addr");
7796 }
7797
7798 return 0;
7799}
7800
7801
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007802#ifdef CONFIG_MESH
7803
7804static int wpa_driver_nl80211_init_mesh(void *priv)
7805{
7806 if (wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_MESH_POINT)) {
7807 wpa_printf(MSG_INFO,
7808 "nl80211: Failed to set interface into mesh mode");
7809 return -1;
7810 }
7811 return 0;
7812}
7813
7814
Dmitry Shmidtff787d52015-01-12 13:01:47 -08007815static int nl80211_put_mesh_id(struct nl_msg *msg, const u8 *mesh_id,
7816 size_t mesh_id_len)
7817{
7818 if (mesh_id) {
7819 wpa_hexdump_ascii(MSG_DEBUG, " * Mesh ID (SSID)",
7820 mesh_id, mesh_id_len);
7821 return nla_put(msg, NL80211_ATTR_MESH_ID, mesh_id_len, mesh_id);
7822 }
7823
7824 return 0;
7825}
7826
7827
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007828static int
7829wpa_driver_nl80211_join_mesh(void *priv,
7830 struct wpa_driver_mesh_join_params *params)
7831{
7832 struct i802_bss *bss = priv;
7833 struct wpa_driver_nl80211_data *drv = bss->drv;
7834 struct nl_msg *msg;
7835 struct nlattr *container;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08007836 int ret = -1;
7837 u32 timeout;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007838
7839 wpa_printf(MSG_DEBUG, "nl80211: mesh join (ifindex=%d)", drv->ifindex);
7840 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_JOIN_MESH);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08007841 if (!msg ||
7842 nl80211_put_freq_params(msg, &params->freq) ||
7843 nl80211_put_basic_rates(msg, params->basic_rates) ||
7844 nl80211_put_mesh_id(msg, params->meshid, params->meshid_len) ||
7845 nl80211_put_beacon_int(msg, params->beacon_int))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007846 goto fail;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007847
7848 wpa_printf(MSG_DEBUG, " * flags=%08X", params->flags);
7849
7850 container = nla_nest_start(msg, NL80211_ATTR_MESH_SETUP);
7851 if (!container)
7852 goto fail;
7853
7854 if (params->ies) {
7855 wpa_hexdump(MSG_DEBUG, " * IEs", params->ies, params->ie_len);
7856 if (nla_put(msg, NL80211_MESH_SETUP_IE, params->ie_len,
7857 params->ies))
7858 goto fail;
7859 }
7860 /* WPA_DRIVER_MESH_FLAG_OPEN_AUTH is treated as default by nl80211 */
7861 if (params->flags & WPA_DRIVER_MESH_FLAG_SAE_AUTH) {
7862 if (nla_put_u8(msg, NL80211_MESH_SETUP_AUTH_PROTOCOL, 0x1) ||
7863 nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_AUTH))
7864 goto fail;
7865 }
7866 if ((params->flags & WPA_DRIVER_MESH_FLAG_AMPE) &&
7867 nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_AMPE))
7868 goto fail;
7869 if ((params->flags & WPA_DRIVER_MESH_FLAG_USER_MPM) &&
7870 nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_MPM))
7871 goto fail;
7872 nla_nest_end(msg, container);
7873
7874 container = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
7875 if (!container)
7876 goto fail;
7877
7878 if (!(params->conf.flags & WPA_DRIVER_MESH_CONF_FLAG_AUTO_PLINKS) &&
7879 nla_put_u32(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS, 0))
7880 goto fail;
7881 if ((params->conf.flags & WPA_DRIVER_MESH_FLAG_DRIVER_MPM) &&
7882 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
7883 params->max_peer_links))
7884 goto fail;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08007885
7886 /*
7887 * Set NL80211_MESHCONF_PLINK_TIMEOUT even if user mpm is used because
7888 * the timer could disconnect stations even in that case.
7889 *
7890 * Set 0xffffffff instead of 0 because NL80211_MESHCONF_PLINK_TIMEOUT
7891 * does not allow 0.
7892 */
7893 timeout = params->conf.peer_link_timeout;
7894 if ((params->flags & WPA_DRIVER_MESH_FLAG_USER_MPM) || timeout == 0)
7895 timeout = 0xffffffff;
7896 if (nla_put_u32(msg, NL80211_MESHCONF_PLINK_TIMEOUT, timeout)) {
7897 wpa_printf(MSG_ERROR, "nl80211: Failed to set PLINK_TIMEOUT");
7898 goto fail;
7899 }
7900
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007901 nla_nest_end(msg, container);
7902
7903 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7904 msg = NULL;
7905 if (ret) {
7906 wpa_printf(MSG_DEBUG, "nl80211: mesh join failed: ret=%d (%s)",
7907 ret, strerror(-ret));
7908 goto fail;
7909 }
7910 ret = 0;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08007911 bss->freq = params->freq.freq;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007912 wpa_printf(MSG_DEBUG, "nl80211: mesh join request send successfully");
7913
7914fail:
7915 nlmsg_free(msg);
7916 return ret;
7917}
7918
7919
7920static int wpa_driver_nl80211_leave_mesh(void *priv)
7921{
7922 struct i802_bss *bss = priv;
7923 struct wpa_driver_nl80211_data *drv = bss->drv;
7924 struct nl_msg *msg;
7925 int ret;
7926
7927 wpa_printf(MSG_DEBUG, "nl80211: mesh leave (ifindex=%d)", drv->ifindex);
7928 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_LEAVE_MESH);
7929 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7930 if (ret) {
7931 wpa_printf(MSG_DEBUG, "nl80211: mesh leave failed: ret=%d (%s)",
7932 ret, strerror(-ret));
7933 } else {
7934 wpa_printf(MSG_DEBUG,
7935 "nl80211: mesh leave request send successfully");
7936 }
7937
7938 if (wpa_driver_nl80211_set_mode(drv->first_bss,
7939 NL80211_IFTYPE_STATION)) {
7940 wpa_printf(MSG_INFO,
7941 "nl80211: Failed to set interface into station mode");
7942 }
7943 return ret;
7944}
7945
7946#endif /* CONFIG_MESH */
7947
7948
7949static int wpa_driver_br_add_ip_neigh(void *priv, u8 version,
7950 const u8 *ipaddr, int prefixlen,
7951 const u8 *addr)
7952{
7953#ifdef CONFIG_LIBNL3_ROUTE
7954 struct i802_bss *bss = priv;
7955 struct wpa_driver_nl80211_data *drv = bss->drv;
7956 struct rtnl_neigh *rn;
7957 struct nl_addr *nl_ipaddr = NULL;
7958 struct nl_addr *nl_lladdr = NULL;
7959 int family, addrsize;
7960 int res;
7961
7962 if (!ipaddr || prefixlen == 0 || !addr)
7963 return -EINVAL;
7964
7965 if (bss->br_ifindex == 0) {
7966 wpa_printf(MSG_DEBUG,
7967 "nl80211: bridge must be set before adding an ip neigh to it");
7968 return -1;
7969 }
7970
7971 if (!drv->rtnl_sk) {
7972 wpa_printf(MSG_DEBUG,
7973 "nl80211: nl_sock for NETLINK_ROUTE is not initialized");
7974 return -1;
7975 }
7976
7977 if (version == 4) {
7978 family = AF_INET;
7979 addrsize = 4;
7980 } else if (version == 6) {
7981 family = AF_INET6;
7982 addrsize = 16;
7983 } else {
7984 return -EINVAL;
7985 }
7986
7987 rn = rtnl_neigh_alloc();
7988 if (rn == NULL)
7989 return -ENOMEM;
7990
7991 /* set the destination ip address for neigh */
7992 nl_ipaddr = nl_addr_build(family, (void *) ipaddr, addrsize);
7993 if (nl_ipaddr == NULL) {
7994 wpa_printf(MSG_DEBUG, "nl80211: nl_ipaddr build failed");
7995 res = -ENOMEM;
7996 goto errout;
7997 }
7998 nl_addr_set_prefixlen(nl_ipaddr, prefixlen);
7999 res = rtnl_neigh_set_dst(rn, nl_ipaddr);
8000 if (res) {
8001 wpa_printf(MSG_DEBUG,
8002 "nl80211: neigh set destination addr failed");
8003 goto errout;
8004 }
8005
8006 /* set the corresponding lladdr for neigh */
8007 nl_lladdr = nl_addr_build(AF_BRIDGE, (u8 *) addr, ETH_ALEN);
8008 if (nl_lladdr == NULL) {
8009 wpa_printf(MSG_DEBUG, "nl80211: neigh set lladdr failed");
8010 res = -ENOMEM;
8011 goto errout;
8012 }
8013 rtnl_neigh_set_lladdr(rn, nl_lladdr);
8014
8015 rtnl_neigh_set_ifindex(rn, bss->br_ifindex);
8016 rtnl_neigh_set_state(rn, NUD_PERMANENT);
8017
8018 res = rtnl_neigh_add(drv->rtnl_sk, rn, NLM_F_CREATE);
8019 if (res) {
8020 wpa_printf(MSG_DEBUG,
8021 "nl80211: Adding bridge ip neigh failed: %s",
8022 strerror(errno));
8023 }
8024errout:
8025 if (nl_lladdr)
8026 nl_addr_put(nl_lladdr);
8027 if (nl_ipaddr)
8028 nl_addr_put(nl_ipaddr);
8029 if (rn)
8030 rtnl_neigh_put(rn);
8031 return res;
8032#else /* CONFIG_LIBNL3_ROUTE */
8033 return -1;
8034#endif /* CONFIG_LIBNL3_ROUTE */
8035}
8036
8037
8038static int wpa_driver_br_delete_ip_neigh(void *priv, u8 version,
8039 const u8 *ipaddr)
8040{
8041#ifdef CONFIG_LIBNL3_ROUTE
8042 struct i802_bss *bss = priv;
8043 struct wpa_driver_nl80211_data *drv = bss->drv;
8044 struct rtnl_neigh *rn;
8045 struct nl_addr *nl_ipaddr;
8046 int family, addrsize;
8047 int res;
8048
8049 if (!ipaddr)
8050 return -EINVAL;
8051
8052 if (version == 4) {
8053 family = AF_INET;
8054 addrsize = 4;
8055 } else if (version == 6) {
8056 family = AF_INET6;
8057 addrsize = 16;
8058 } else {
8059 return -EINVAL;
8060 }
8061
8062 if (bss->br_ifindex == 0) {
8063 wpa_printf(MSG_DEBUG,
8064 "nl80211: bridge must be set to delete an ip neigh");
8065 return -1;
8066 }
8067
8068 if (!drv->rtnl_sk) {
8069 wpa_printf(MSG_DEBUG,
8070 "nl80211: nl_sock for NETLINK_ROUTE is not initialized");
8071 return -1;
8072 }
8073
8074 rn = rtnl_neigh_alloc();
8075 if (rn == NULL)
8076 return -ENOMEM;
8077
8078 /* set the destination ip address for neigh */
8079 nl_ipaddr = nl_addr_build(family, (void *) ipaddr, addrsize);
8080 if (nl_ipaddr == NULL) {
8081 wpa_printf(MSG_DEBUG, "nl80211: nl_ipaddr build failed");
8082 res = -ENOMEM;
8083 goto errout;
8084 }
8085 res = rtnl_neigh_set_dst(rn, nl_ipaddr);
8086 if (res) {
8087 wpa_printf(MSG_DEBUG,
8088 "nl80211: neigh set destination addr failed");
8089 goto errout;
8090 }
8091
8092 rtnl_neigh_set_ifindex(rn, bss->br_ifindex);
8093
8094 res = rtnl_neigh_delete(drv->rtnl_sk, rn, 0);
8095 if (res) {
8096 wpa_printf(MSG_DEBUG,
8097 "nl80211: Deleting bridge ip neigh failed: %s",
8098 strerror(errno));
8099 }
8100errout:
8101 if (nl_ipaddr)
8102 nl_addr_put(nl_ipaddr);
8103 if (rn)
8104 rtnl_neigh_put(rn);
8105 return res;
8106#else /* CONFIG_LIBNL3_ROUTE */
8107 return -1;
8108#endif /* CONFIG_LIBNL3_ROUTE */
8109}
8110
8111
8112static int linux_write_system_file(const char *path, unsigned int val)
8113{
8114 char buf[50];
8115 int fd, len;
8116
8117 len = os_snprintf(buf, sizeof(buf), "%u\n", val);
8118 if (os_snprintf_error(sizeof(buf), len))
8119 return -1;
8120
8121 fd = open(path, O_WRONLY);
8122 if (fd < 0)
8123 return -1;
8124
8125 if (write(fd, buf, len) < 0) {
8126 wpa_printf(MSG_DEBUG,
8127 "nl80211: Failed to write Linux system file: %s with the value of %d",
8128 path, val);
8129 close(fd);
8130 return -1;
8131 }
8132 close(fd);
8133
8134 return 0;
8135}
8136
8137
8138static const char * drv_br_port_attr_str(enum drv_br_port_attr attr)
8139{
8140 switch (attr) {
8141 case DRV_BR_PORT_ATTR_PROXYARP:
8142 return "proxyarp";
8143 case DRV_BR_PORT_ATTR_HAIRPIN_MODE:
8144 return "hairpin_mode";
8145 }
8146
8147 return NULL;
8148}
8149
8150
8151static int wpa_driver_br_port_set_attr(void *priv, enum drv_br_port_attr attr,
8152 unsigned int val)
8153{
8154 struct i802_bss *bss = priv;
8155 char path[128];
8156 const char *attr_txt;
8157
8158 attr_txt = drv_br_port_attr_str(attr);
8159 if (attr_txt == NULL)
8160 return -EINVAL;
8161
8162 os_snprintf(path, sizeof(path), "/sys/class/net/%s/brport/%s",
8163 bss->ifname, attr_txt);
8164
8165 if (linux_write_system_file(path, val))
8166 return -1;
8167
8168 return 0;
8169}
8170
8171
8172static const char * drv_br_net_param_str(enum drv_br_net_param param)
8173{
8174 switch (param) {
8175 case DRV_BR_NET_PARAM_GARP_ACCEPT:
8176 return "arp_accept";
8177 }
8178
8179 return NULL;
8180}
8181
8182
8183static int wpa_driver_br_set_net_param(void *priv, enum drv_br_net_param param,
8184 unsigned int val)
8185{
8186 struct i802_bss *bss = priv;
8187 char path[128];
8188 const char *param_txt;
8189 int ip_version = 4;
8190
8191 param_txt = drv_br_net_param_str(param);
8192 if (param_txt == NULL)
8193 return -EINVAL;
8194
8195 switch (param) {
8196 case DRV_BR_NET_PARAM_GARP_ACCEPT:
8197 ip_version = 4;
8198 break;
8199 default:
8200 return -EINVAL;
8201 }
8202
8203 os_snprintf(path, sizeof(path), "/proc/sys/net/ipv%d/conf/%s/%s",
8204 ip_version, bss->brname, param_txt);
8205
8206 if (linux_write_system_file(path, val))
8207 return -1;
8208
8209 return 0;
8210}
8211
8212
8213static int hw_mode_to_qca_acs(enum hostapd_hw_mode hw_mode)
8214{
8215 switch (hw_mode) {
8216 case HOSTAPD_MODE_IEEE80211B:
8217 return QCA_ACS_MODE_IEEE80211B;
8218 case HOSTAPD_MODE_IEEE80211G:
8219 return QCA_ACS_MODE_IEEE80211G;
8220 case HOSTAPD_MODE_IEEE80211A:
8221 return QCA_ACS_MODE_IEEE80211A;
8222 case HOSTAPD_MODE_IEEE80211AD:
8223 return QCA_ACS_MODE_IEEE80211AD;
8224 default:
8225 return -1;
8226 }
8227}
8228
8229
8230static int wpa_driver_do_acs(void *priv, struct drv_acs_params *params)
8231{
8232 struct i802_bss *bss = priv;
8233 struct wpa_driver_nl80211_data *drv = bss->drv;
8234 struct nl_msg *msg;
8235 struct nlattr *data;
8236 int ret;
8237 int mode;
8238
8239 mode = hw_mode_to_qca_acs(params->hw_mode);
8240 if (mode < 0)
8241 return -1;
8242
8243 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
8244 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
8245 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
8246 QCA_NL80211_VENDOR_SUBCMD_DO_ACS) ||
8247 !(data = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
8248 nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_ACS_HW_MODE, mode) ||
8249 (params->ht_enabled &&
8250 nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_HT_ENABLED)) ||
8251 (params->ht40_enabled &&
8252 nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_HT40_ENABLED))) {
8253 nlmsg_free(msg);
8254 return -ENOBUFS;
8255 }
8256 nla_nest_end(msg, data);
8257
8258 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8259 if (ret) {
8260 wpa_printf(MSG_DEBUG,
8261 "nl80211: Failed to invoke driver ACS function: %s",
8262 strerror(errno));
8263 }
8264 return ret;
8265}
8266
8267
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008268const struct wpa_driver_ops wpa_driver_nl80211_ops = {
8269 .name = "nl80211",
8270 .desc = "Linux nl80211/cfg80211",
8271 .get_bssid = wpa_driver_nl80211_get_bssid,
8272 .get_ssid = wpa_driver_nl80211_get_ssid,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008273 .set_key = driver_nl80211_set_key,
8274 .scan2 = driver_nl80211_scan2,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008275 .sched_scan = wpa_driver_nl80211_sched_scan,
8276 .stop_sched_scan = wpa_driver_nl80211_stop_sched_scan,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008277 .get_scan_results2 = wpa_driver_nl80211_get_scan_results,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008278 .deauthenticate = driver_nl80211_deauthenticate,
8279 .authenticate = driver_nl80211_authenticate,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008280 .associate = wpa_driver_nl80211_associate,
8281 .global_init = nl80211_global_init,
8282 .global_deinit = nl80211_global_deinit,
8283 .init2 = wpa_driver_nl80211_init,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008284 .deinit = driver_nl80211_deinit,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008285 .get_capa = wpa_driver_nl80211_get_capa,
8286 .set_operstate = wpa_driver_nl80211_set_operstate,
8287 .set_supp_port = wpa_driver_nl80211_set_supp_port,
8288 .set_country = wpa_driver_nl80211_set_country,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008289 .get_country = wpa_driver_nl80211_get_country,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008290 .set_ap = wpa_driver_nl80211_set_ap,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07008291 .set_acl = wpa_driver_nl80211_set_acl,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008292 .if_add = wpa_driver_nl80211_if_add,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008293 .if_remove = driver_nl80211_if_remove,
8294 .send_mlme = driver_nl80211_send_mlme,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008295 .get_hw_feature_data = nl80211_get_hw_feature_data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008296 .sta_add = wpa_driver_nl80211_sta_add,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008297 .sta_remove = driver_nl80211_sta_remove,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008298 .hapd_send_eapol = wpa_driver_nl80211_hapd_send_eapol,
8299 .sta_set_flags = wpa_driver_nl80211_sta_set_flags,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008300 .hapd_init = i802_init,
8301 .hapd_deinit = i802_deinit,
Jouni Malinen75ecf522011-06-27 15:19:46 -07008302 .set_wds_sta = i802_set_wds_sta,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008303 .get_seqnum = i802_get_seqnum,
8304 .flush = i802_flush,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008305 .get_inact_sec = i802_get_inact_sec,
8306 .sta_clear_stats = i802_sta_clear_stats,
8307 .set_rts = i802_set_rts,
8308 .set_frag = i802_set_frag,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008309 .set_tx_queue_params = i802_set_tx_queue_params,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008310 .set_sta_vlan = driver_nl80211_set_sta_vlan,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008311 .sta_deauth = i802_sta_deauth,
8312 .sta_disassoc = i802_sta_disassoc,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008313 .read_sta_data = driver_nl80211_read_sta_data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008314 .set_freq = i802_set_freq,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008315 .send_action = driver_nl80211_send_action,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008316 .send_action_cancel_wait = wpa_driver_nl80211_send_action_cancel_wait,
8317 .remain_on_channel = wpa_driver_nl80211_remain_on_channel,
8318 .cancel_remain_on_channel =
8319 wpa_driver_nl80211_cancel_remain_on_channel,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008320 .probe_req_report = driver_nl80211_probe_req_report,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008321 .deinit_ap = wpa_driver_nl80211_deinit_ap,
Dmitry Shmidt04949592012-07-19 12:16:46 -07008322 .deinit_p2p_cli = wpa_driver_nl80211_deinit_p2p_cli,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008323 .resume = wpa_driver_nl80211_resume,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008324 .signal_monitor = nl80211_signal_monitor,
8325 .signal_poll = nl80211_signal_poll,
8326 .send_frame = nl80211_send_frame,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008327 .shared_freq = wpa_driver_nl80211_shared_freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008328 .set_param = nl80211_set_param,
8329 .get_radio_name = nl80211_get_radio_name,
Jouni Malinen75ecf522011-06-27 15:19:46 -07008330 .add_pmkid = nl80211_add_pmkid,
8331 .remove_pmkid = nl80211_remove_pmkid,
8332 .flush_pmkid = nl80211_flush_pmkid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008333 .set_rekey_info = nl80211_set_rekey_info,
8334 .poll_client = nl80211_poll_client,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008335 .set_p2p_powersave = nl80211_set_p2p_powersave,
Dmitry Shmidtea69e842013-05-13 14:52:28 -07008336 .start_dfs_cac = nl80211_start_radar_detection,
8337 .stop_ap = wpa_driver_nl80211_stop_ap,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008338#ifdef CONFIG_TDLS
8339 .send_tdls_mgmt = nl80211_send_tdls_mgmt,
8340 .tdls_oper = nl80211_tdls_oper,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008341 .tdls_enable_channel_switch = nl80211_tdls_enable_channel_switch,
8342 .tdls_disable_channel_switch = nl80211_tdls_disable_channel_switch,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008343#endif /* CONFIG_TDLS */
Dmitry Shmidt700a1372013-03-15 14:14:44 -07008344 .update_ft_ies = wpa_driver_nl80211_update_ft_ies,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008345 .get_mac_addr = wpa_driver_nl80211_get_macaddr,
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008346 .get_survey = wpa_driver_nl80211_get_survey,
Dmitry Shmidt56052862013-10-04 10:23:25 -07008347 .status = wpa_driver_nl80211_status,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008348 .switch_channel = nl80211_switch_channel,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008349#ifdef ANDROID_P2P
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07008350 .set_noa = wpa_driver_set_p2p_noa,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008351 .get_noa = wpa_driver_get_p2p_noa,
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07008352 .set_ap_wps_ie = wpa_driver_set_ap_wps_p2p_ie,
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08008353#endif /* ANDROID_P2P */
Dmitry Shmidt738a26e2011-07-07 14:22:14 -07008354#ifdef ANDROID
8355 .driver_cmd = wpa_driver_nl80211_driver_cmd,
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08008356#endif /* ANDROID */
Dmitry Shmidta38abf92014-03-06 13:38:44 -08008357 .vendor_cmd = nl80211_vendor_cmd,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008358 .set_qos_map = nl80211_set_qos_map,
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07008359 .set_wowlan = nl80211_set_wowlan,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008360 .roaming = nl80211_roaming,
8361 .set_mac_addr = nl80211_set_mac_addr,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008362#ifdef CONFIG_MESH
8363 .init_mesh = wpa_driver_nl80211_init_mesh,
8364 .join_mesh = wpa_driver_nl80211_join_mesh,
8365 .leave_mesh = wpa_driver_nl80211_leave_mesh,
8366#endif /* CONFIG_MESH */
8367 .br_add_ip_neigh = wpa_driver_br_add_ip_neigh,
8368 .br_delete_ip_neigh = wpa_driver_br_delete_ip_neigh,
8369 .br_port_set_attr = wpa_driver_br_port_set_attr,
8370 .br_set_net_param = wpa_driver_br_set_net_param,
8371 .add_tx_ts = nl80211_add_ts,
8372 .del_tx_ts = nl80211_del_ts,
8373 .do_acs = wpa_driver_do_acs,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008374};