]> git.ipfire.org Git - thirdparty/hostap.git/blame - src/drivers/driver_nl80211.c
wpadebug: Add broadcast intent receiver for Wi-Fi events
[thirdparty/hostap.git] / src / drivers / driver_nl80211.c
CommitLineData
3f5285e8 1/*
c5121837 2 * Driver interaction with Linux nl80211/cfg80211
3d9975d5 3 * Copyright (c) 2002-2012, Jouni Malinen <j@w1.fi>
072ad14c
JM
4 * Copyright (c) 2003-2004, Instant802 Networks, Inc.
5 * Copyright (c) 2005-2006, Devicescape Software, Inc.
6 * Copyright (c) 2007, Johannes Berg <johannes@sipsolutions.net>
58f6fbe0 7 * Copyright (c) 2009-2010, Atheros Communications
3f5285e8 8 *
0f3d578e
JM
9 * This software may be distributed under the terms of the BSD license.
10 * See README for more details.
3f5285e8
JM
11 */
12
13#include "includes.h"
14#include <sys/ioctl.h>
6859f1cb
BG
15#include <sys/types.h>
16#include <sys/stat.h>
17#include <fcntl.h>
37b7d082 18#include <net/if.h>
3f5285e8
JM
19#include <netlink/genl/genl.h>
20#include <netlink/genl/family.h>
21#include <netlink/genl/ctrl.h>
8602b0f2 22#include <linux/rtnetlink.h>
1b648c7e
JM
23#include <netpacket/packet.h>
24#include <linux/filter.h>
32ab4855 25#include <linux/errqueue.h>
7e45830a 26#include "nl80211_copy.h"
625f587b 27
3f5285e8 28#include "common.h"
1b648c7e 29#include "eloop.h"
f2ed8023 30#include "utils/list.h"
1b648c7e 31#include "common/ieee802_11_defs.h"
9b90955e 32#include "common/ieee802_11_common.h"
f10bfc9a 33#include "l2_packet/l2_packet.h"
e2d02c29 34#include "netlink.h"
34f2f814 35#include "linux_ioctl.h"
e2d02c29
JM
36#include "radiotap.h"
37#include "radiotap_iter.h"
8401a6b0 38#include "rfkill.h"
1b648c7e 39#include "driver.h"
0915d02c 40
32ab4855
JB
41#ifndef SO_WIFI_STATUS
42# if defined(__sparc__)
43# define SO_WIFI_STATUS 0x0025
44# elif defined(__parisc__)
45# define SO_WIFI_STATUS 0x4022
46# else
47# define SO_WIFI_STATUS 41
48# endif
49
50# define SCM_WIFI_STATUS SO_WIFI_STATUS
51#endif
52
53#ifndef SO_EE_ORIGIN_TXSTATUS
54#define SO_EE_ORIGIN_TXSTATUS 4
55#endif
56
57#ifndef PACKET_TX_TIMESTAMP
58#define PACKET_TX_TIMESTAMP 16
59#endif
60
216eede8
DS
61#ifdef ANDROID
62#include "android_drv.h"
63#endif /* ANDROID */
c5121837
JM
64#ifdef CONFIG_LIBNL20
65/* libnl 2.0 compatibility code */
2e8eac2d 66#define nl_handle nl_sock
a65a9aed
JB
67#define nl80211_handle_alloc nl_socket_alloc_cb
68#define nl80211_handle_destroy nl_socket_free
69#else
70/*
71 * libnl 1.1 has a bug, it tries to allocate socket numbers densely
72 * but when you free a socket again it will mess up its bitmap and
73 * and use the wrong number the next time it needs a socket ID.
74 * Therefore, we wrap the handle alloc/destroy and add our own pid
75 * accounting.
76 */
77static uint32_t port_bitmap[32] = { 0 };
78
79static struct nl_handle *nl80211_handle_alloc(void *cb)
80{
81 struct nl_handle *handle;
82 uint32_t pid = getpid() & 0x3FFFFF;
83 int i;
84
85 handle = nl_handle_alloc_cb(cb);
86
87 for (i = 0; i < 1024; i++) {
88 if (port_bitmap[i / 32] & (1 << (i % 32)))
89 continue;
90 port_bitmap[i / 32] |= 1 << (i % 32);
91 pid += i << 22;
92 break;
93 }
94
95 nl_socket_set_local_port(handle, pid);
96
97 return handle;
98}
99
100static void nl80211_handle_destroy(struct nl_handle *handle)
101{
102 uint32_t port = nl_socket_get_local_port(handle);
103
104 port >>= 22;
105 port_bitmap[port / 32] &= ~(1 << (port % 32));
106
107 nl_handle_destroy(handle);
108}
c5121837
JM
109#endif /* CONFIG_LIBNL20 */
110
c5121837 111
481234cf 112static struct nl_handle * nl_create_handle(struct nl_cb *cb, const char *dbg)
a92dfde8 113{
481234cf 114 struct nl_handle *handle;
a92dfde8 115
481234cf
JM
116 handle = nl80211_handle_alloc(cb);
117 if (handle == NULL) {
a92dfde8
JB
118 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
119 "callbacks (%s)", dbg);
481234cf 120 return NULL;
a92dfde8
JB
121 }
122
481234cf 123 if (genl_connect(handle)) {
a92dfde8
JB
124 wpa_printf(MSG_ERROR, "nl80211: Failed to connect to generic "
125 "netlink (%s)", dbg);
481234cf
JM
126 nl80211_handle_destroy(handle);
127 return NULL;
a92dfde8
JB
128 }
129
481234cf 130 return handle;
a92dfde8
JB
131}
132
133
481234cf 134static void nl_destroy_handles(struct nl_handle **handle)
a92dfde8 135{
481234cf 136 if (*handle == NULL)
a92dfde8 137 return;
481234cf
JM
138 nl80211_handle_destroy(*handle);
139 *handle = NULL;
a92dfde8
JB
140}
141
142
3f5285e8
JM
143#ifndef IFF_LOWER_UP
144#define IFF_LOWER_UP 0x10000 /* driver signals L1 up */
145#endif
146#ifndef IFF_DORMANT
147#define IFF_DORMANT 0x20000 /* driver signals dormant */
148#endif
149
150#ifndef IF_OPER_DORMANT
151#define IF_OPER_DORMANT 5
152#endif
153#ifndef IF_OPER_UP
154#define IF_OPER_UP 6
155#endif
156
f2ed8023
JM
157struct nl80211_global {
158 struct dl_list interfaces;
ff6a158b 159 int if_add_ifindex;
36d84860 160 struct netlink_data *netlink;
2a7b66f5 161 struct nl_cb *nl_cb;
481234cf 162 struct nl_handle *nl;
335d42b1 163 int nl80211_id;
c81eff1a 164 int ioctl_sock; /* socket for ioctl() use */
d6c9aab8 165
481234cf 166 struct nl_handle *nl_event;
f2ed8023
JM
167};
168
e32ad281
JB
169struct nl80211_wiphy_data {
170 struct dl_list list;
171 struct dl_list bsss;
172 struct dl_list drvs;
173
481234cf 174 struct nl_handle *nl_beacons;
e32ad281
JB
175 struct nl_cb *nl_cb;
176
177 int wiphy_idx;
178};
179
36d84860
BG
180static void nl80211_global_deinit(void *priv);
181
c5121837 182struct i802_bss {
a2e40bb6 183 struct wpa_driver_nl80211_data *drv;
c5121837 184 struct i802_bss *next;
b4fd6fab 185 int ifindex;
a2e40bb6 186 char ifname[IFNAMSIZ + 1];
e17a2477 187 char brname[IFNAMSIZ];
c5121837 188 unsigned int beacon_set:1;
e17a2477
JM
189 unsigned int added_if_into_bridge:1;
190 unsigned int added_bridge:1;
873d0fcf 191 unsigned int in_deinit:1;
221a59c9 192
341eebee
JB
193 u8 addr[ETH_ALEN];
194
e4fb2167
JB
195 int freq;
196
a5e1eb20 197 void *ctx;
481234cf 198 struct nl_handle *nl_preq, *nl_mgmt;
cc7a48d1 199 struct nl_cb *nl_cb;
e32ad281
JB
200
201 struct nl80211_wiphy_data *wiphy_data;
202 struct dl_list wiphy_list;
c5121837 203};
3f5285e8
JM
204
205struct wpa_driver_nl80211_data {
f2ed8023
JM
206 struct nl80211_global *global;
207 struct dl_list list;
e32ad281 208 struct dl_list wiphy_list;
6859f1cb 209 char phyname[32];
3f5285e8 210 void *ctx;
3f5285e8 211 int ifindex;
7524cfb1 212 int if_removed;
a63063b4 213 int if_disabled;
7d9c3698 214 int ignore_if_down_event;
8401a6b0 215 struct rfkill_data *rfkill;
c2a04078 216 struct wpa_driver_capa capa;
8cd6b7bc
JB
217 u8 *extended_capa, *extended_capa_mask;
218 unsigned int extended_capa_len;
c2a04078 219 int has_capability;
c2a04078 220
3f5285e8
JM
221 int operstate;
222
3f5285e8
JM
223 int scan_complete_events;
224
1afc986d 225 struct nl_cb *nl_cb;
1c873584 226
e6b8efeb 227 u8 auth_bssid[ETH_ALEN];
c2a04078
JM
228 u8 bssid[ETH_ALEN];
229 int associated;
fd05d64e
JM
230 u8 ssid[32];
231 size_t ssid_len;
b1f625e0
EP
232 enum nl80211_iftype nlmode;
233 enum nl80211_iftype ap_scan_as_station;
4832ecd7 234 unsigned int assoc_freq;
d2440ba0 235
0915d02c
JM
236 int monitor_sock;
237 int monitor_ifidx;
3fd1cefb 238 int monitor_refcount;
7da3abe7 239
b3af99d2 240 unsigned int disabled_11b_rates:1;
55777702 241 unsigned int pending_remain_on_chan:1;
dac12351 242 unsigned int in_interface_list:1;
61cbe2ff 243 unsigned int device_ap_sme:1;
39718852 244 unsigned int poll_command_supported:1;
32ab4855 245 unsigned int data_tx_status:1;
536fd62d
JM
246 unsigned int scan_for_auth:1;
247 unsigned int retry_auth:1;
a11241fa 248 unsigned int use_monitor:1;
a8c5b43a 249 unsigned int ignore_next_local_disconnect:1;
55777702
JM
250
251 u64 remain_on_chan_cookie;
58f6fbe0 252 u64 send_action_cookie;
c5121837 253
5582a5d1
JB
254 unsigned int last_mgmt_freq;
255
3812464c
JM
256 struct wpa_driver_scan_filter *filter_ssids;
257 size_t num_filter_ssids;
258
a2e40bb6
FF
259 struct i802_bss first_bss;
260
d12dab4c 261 int eapol_tx_sock;
f10bfc9a 262
c5121837 263#ifdef HOSTAPD
c5121837 264 int eapol_sock; /* socket for EAPOL frames */
c5121837
JM
265
266 int default_if_indices[16];
267 int *if_indices;
268 int num_if_indices;
269
c5121837
JM
270 int last_freq;
271 int last_freq_ht;
c5121837 272#endif /* HOSTAPD */
536fd62d
JM
273
274 /* From failed authentication command */
275 int auth_freq;
276 u8 auth_bssid_[ETH_ALEN];
277 u8 auth_ssid[32];
278 size_t auth_ssid_len;
279 int auth_alg;
280 u8 *auth_ie;
281 size_t auth_ie_len;
282 u8 auth_wep_key[4][16];
283 size_t auth_wep_key_len[4];
284 int auth_wep_tx_keyidx;
285 int auth_local_state_change;
286 int auth_p2p;
3f5285e8
JM
287};
288
289
9ebce9c5 290static void wpa_driver_nl80211_deinit(struct i802_bss *bss);
3f5285e8
JM
291static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx,
292 void *timeout_ctx);
b1f625e0
EP
293static int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
294 enum nl80211_iftype nlmode);
362f781e 295static int
7524cfb1 296wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv);
d72aad94 297static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
77339912
JM
298 const u8 *addr, int cmd, u16 reason_code,
299 int local_state_change);
460456f8
JM
300static void nl80211_remove_monitor_interface(
301 struct wpa_driver_nl80211_data *drv);
88df0ef7 302static int nl80211_send_frame_cmd(struct i802_bss *bss,
5dfca53f 303 unsigned int freq, unsigned int wait,
b106173a 304 const u8 *buf, size_t buf_len, u64 *cookie,
88df0ef7 305 int no_cck, int no_ack, int offchanok);
9ebce9c5
JM
306static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss,
307 int report);
216eede8
DS
308#ifdef ANDROID
309static int android_pno_start(struct i802_bss *bss,
310 struct wpa_driver_scan_params *params);
311static int android_pno_stop(struct i802_bss *bss);
312#endif /* ANDROID */
0915d02c 313
072ad14c 314#ifdef HOSTAPD
2135f224
JM
315static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
316static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
97cfcf64 317static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
9ebce9c5 318static int wpa_driver_nl80211_if_remove(struct i802_bss *bss,
fbbfcbac
FF
319 enum wpa_driver_if_type type,
320 const char *ifname);
37eb8da9 321#else /* HOSTAPD */
bd2df892
JM
322static inline void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
323{
324}
325
326static inline void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
327{
328}
329
330static inline int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
37eb8da9
JM
331{
332 return 0;
333}
072ad14c
JM
334#endif /* HOSTAPD */
335
9ebce9c5
JM
336static int wpa_driver_nl80211_set_freq(struct i802_bss *bss,
337 struct hostapd_freq_params *freq);
4e5cb1a3
JM
338static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
339 int ifindex, int disabled);
504e905c 340
21bdbe38 341static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv);
536fd62d
JM
342static int wpa_driver_nl80211_authenticate_retry(
343 struct wpa_driver_nl80211_data *drv);
21bdbe38 344
3f5285e8 345
b1f625e0
EP
346static int is_ap_interface(enum nl80211_iftype nlmode)
347{
348 return (nlmode == NL80211_IFTYPE_AP ||
349 nlmode == NL80211_IFTYPE_P2P_GO);
350}
351
352
353static int is_sta_interface(enum nl80211_iftype nlmode)
354{
355 return (nlmode == NL80211_IFTYPE_STATION ||
356 nlmode == NL80211_IFTYPE_P2P_CLIENT);
357}
358
359
b3af99d2
JM
360static int is_p2p_interface(enum nl80211_iftype nlmode)
361{
362 return (nlmode == NL80211_IFTYPE_P2P_CLIENT ||
363 nlmode == NL80211_IFTYPE_P2P_GO);
364}
365
366
f5a8d422
JM
367struct nl80211_bss_info_arg {
368 struct wpa_driver_nl80211_data *drv;
369 struct wpa_scan_results *res;
370 unsigned int assoc_freq;
20f5a4c2 371 u8 assoc_bssid[ETH_ALEN];
f5a8d422
JM
372};
373
374static int bss_info_handler(struct nl_msg *msg, void *arg);
375
376
6241fcb1
JM
377/* nl80211 code */
378static int ack_handler(struct nl_msg *msg, void *arg)
379{
380 int *err = arg;
381 *err = 0;
382 return NL_STOP;
383}
384
385static int finish_handler(struct nl_msg *msg, void *arg)
386{
8e8df255
JM
387 int *ret = arg;
388 *ret = 0;
6241fcb1
JM
389 return NL_SKIP;
390}
391
392static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
393 void *arg)
394{
395 int *ret = arg;
396 *ret = err->error;
397 return NL_SKIP;
398}
399
5b7b85f6
JM
400
401static int no_seq_check(struct nl_msg *msg, void *arg)
402{
403 return NL_OK;
404}
405
406
d6c9aab8 407static int send_and_recv(struct nl80211_global *global,
58f6fbe0
JM
408 struct nl_handle *nl_handle, struct nl_msg *msg,
409 int (*valid_handler)(struct nl_msg *, void *),
410 void *valid_data)
6241fcb1
JM
411{
412 struct nl_cb *cb;
413 int err = -ENOMEM;
414
d6c9aab8 415 cb = nl_cb_clone(global->nl_cb);
6241fcb1
JM
416 if (!cb)
417 goto out;
418
58f6fbe0 419 err = nl_send_auto_complete(nl_handle, msg);
6241fcb1
JM
420 if (err < 0)
421 goto out;
422
423 err = 1;
424
425 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
8e8df255 426 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
6241fcb1
JM
427 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
428
429 if (valid_handler)
430 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM,
431 valid_handler, valid_data);
432
433 while (err > 0)
58f6fbe0 434 nl_recvmsgs(nl_handle, cb);
6241fcb1
JM
435 out:
436 nl_cb_put(cb);
437 nlmsg_free(msg);
438 return err;
439}
440
441
d6c9aab8
JB
442static int send_and_recv_msgs_global(struct nl80211_global *global,
443 struct nl_msg *msg,
444 int (*valid_handler)(struct nl_msg *, void *),
445 void *valid_data)
446{
481234cf 447 return send_and_recv(global, global->nl, msg, valid_handler,
d6c9aab8
JB
448 valid_data);
449}
450
451
58f6fbe0
JM
452static int send_and_recv_msgs(struct wpa_driver_nl80211_data *drv,
453 struct nl_msg *msg,
454 int (*valid_handler)(struct nl_msg *, void *),
455 void *valid_data)
456{
481234cf 457 return send_and_recv(drv->global, drv->global->nl, msg,
d6c9aab8 458 valid_handler, valid_data);
58f6fbe0
JM
459}
460
461
97865538
JM
462struct family_data {
463 const char *group;
464 int id;
465};
466
467
468static int family_handler(struct nl_msg *msg, void *arg)
469{
470 struct family_data *res = arg;
471 struct nlattr *tb[CTRL_ATTR_MAX + 1];
472 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
473 struct nlattr *mcgrp;
474 int i;
475
476 nla_parse(tb, CTRL_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
477 genlmsg_attrlen(gnlh, 0), NULL);
478 if (!tb[CTRL_ATTR_MCAST_GROUPS])
479 return NL_SKIP;
480
481 nla_for_each_nested(mcgrp, tb[CTRL_ATTR_MCAST_GROUPS], i) {
482 struct nlattr *tb2[CTRL_ATTR_MCAST_GRP_MAX + 1];
483 nla_parse(tb2, CTRL_ATTR_MCAST_GRP_MAX, nla_data(mcgrp),
484 nla_len(mcgrp), NULL);
485 if (!tb2[CTRL_ATTR_MCAST_GRP_NAME] ||
486 !tb2[CTRL_ATTR_MCAST_GRP_ID] ||
487 os_strncmp(nla_data(tb2[CTRL_ATTR_MCAST_GRP_NAME]),
488 res->group,
489 nla_len(tb2[CTRL_ATTR_MCAST_GRP_NAME])) != 0)
490 continue;
491 res->id = nla_get_u32(tb2[CTRL_ATTR_MCAST_GRP_ID]);
492 break;
493 };
494
495 return NL_SKIP;
496}
497
498
d6c9aab8 499static int nl_get_multicast_id(struct nl80211_global *global,
97865538
JM
500 const char *family, const char *group)
501{
502 struct nl_msg *msg;
503 int ret = -1;
504 struct family_data res = { group, -ENOENT };
505
506 msg = nlmsg_alloc();
507 if (!msg)
508 return -ENOMEM;
481234cf 509 genlmsg_put(msg, 0, 0, genl_ctrl_resolve(global->nl, "nlctrl"),
97865538
JM
510 0, 0, CTRL_CMD_GETFAMILY, 0);
511 NLA_PUT_STRING(msg, CTRL_ATTR_FAMILY_NAME, family);
512
d6c9aab8 513 ret = send_and_recv_msgs_global(global, msg, family_handler, &res);
97865538
JM
514 msg = NULL;
515 if (ret == 0)
516 ret = res.id;
517
518nla_put_failure:
519 nlmsg_free(msg);
520 return ret;
521}
522
523
9fb04070
JM
524static void * nl80211_cmd(struct wpa_driver_nl80211_data *drv,
525 struct nl_msg *msg, int flags, uint8_t cmd)
526{
335d42b1 527 return genlmsg_put(msg, 0, 0, drv->global->nl80211_id,
276e2d67 528 0, flags, cmd, 0);
9fb04070
JM
529}
530
531
e32ad281
JB
532struct wiphy_idx_data {
533 int wiphy_idx;
534};
535
536
537static int netdev_info_handler(struct nl_msg *msg, void *arg)
538{
539 struct nlattr *tb[NL80211_ATTR_MAX + 1];
540 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
541 struct wiphy_idx_data *info = arg;
542
543 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
544 genlmsg_attrlen(gnlh, 0), NULL);
545
546 if (tb[NL80211_ATTR_WIPHY])
547 info->wiphy_idx = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
548
549 return NL_SKIP;
550}
551
552
553static int nl80211_get_wiphy_index(struct i802_bss *bss)
554{
555 struct nl_msg *msg;
556 struct wiphy_idx_data data = {
557 .wiphy_idx = -1,
558 };
559
560 msg = nlmsg_alloc();
561 if (!msg)
562 return -1;
563
564 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_GET_INTERFACE);
565
566 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
567
568 if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0)
569 return data.wiphy_idx;
570 msg = NULL;
571nla_put_failure:
572 nlmsg_free(msg);
573 return -1;
574}
575
576
577static int nl80211_register_beacons(struct wpa_driver_nl80211_data *drv,
578 struct nl80211_wiphy_data *w)
579{
580 struct nl_msg *msg;
581 int ret = -1;
582
583 msg = nlmsg_alloc();
584 if (!msg)
585 return -1;
586
587 nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_BEACONS);
588
589 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, w->wiphy_idx);
590
481234cf 591 ret = send_and_recv(drv->global, w->nl_beacons, msg, NULL, NULL);
e32ad281
JB
592 msg = NULL;
593 if (ret) {
594 wpa_printf(MSG_DEBUG, "nl80211: Register beacons command "
595 "failed: ret=%d (%s)",
596 ret, strerror(-ret));
597 goto nla_put_failure;
598 }
599 ret = 0;
600nla_put_failure:
601 nlmsg_free(msg);
602 return ret;
603}
604
605
606static void nl80211_recv_beacons(int sock, void *eloop_ctx, void *handle)
607{
608 struct nl80211_wiphy_data *w = eloop_ctx;
609
ee9fc67a 610 wpa_printf(MSG_EXCESSIVE, "nl80211: Beacon event message available");
e32ad281
JB
611
612 nl_recvmsgs(handle, w->nl_cb);
613}
614
615
616static int process_beacon_event(struct nl_msg *msg, void *arg)
617{
618 struct nl80211_wiphy_data *w = arg;
619 struct wpa_driver_nl80211_data *drv;
620 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
621 struct nlattr *tb[NL80211_ATTR_MAX + 1];
622 union wpa_event_data event;
623
624 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
625 genlmsg_attrlen(gnlh, 0), NULL);
626
627 if (gnlh->cmd != NL80211_CMD_FRAME) {
628 wpa_printf(MSG_DEBUG, "nl80211: Unexpected beacon event? (%d)",
629 gnlh->cmd);
630 return NL_SKIP;
631 }
632
633 if (!tb[NL80211_ATTR_FRAME])
634 return NL_SKIP;
635
636 dl_list_for_each(drv, &w->drvs, struct wpa_driver_nl80211_data,
637 wiphy_list) {
638 os_memset(&event, 0, sizeof(event));
639 event.rx_mgmt.frame = nla_data(tb[NL80211_ATTR_FRAME]);
640 event.rx_mgmt.frame_len = nla_len(tb[NL80211_ATTR_FRAME]);
641 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
642 }
643
644 return NL_SKIP;
645}
646
647
648static struct nl80211_wiphy_data *
649nl80211_get_wiphy_data_ap(struct i802_bss *bss)
650{
651 static DEFINE_DL_LIST(nl80211_wiphys);
652 struct nl80211_wiphy_data *w;
653 int wiphy_idx, found = 0;
654 struct i802_bss *tmp_bss;
655
656 if (bss->wiphy_data != NULL)
657 return bss->wiphy_data;
658
659 wiphy_idx = nl80211_get_wiphy_index(bss);
660
661 dl_list_for_each(w, &nl80211_wiphys, struct nl80211_wiphy_data, list) {
662 if (w->wiphy_idx == wiphy_idx)
663 goto add;
664 }
665
666 /* alloc new one */
667 w = os_zalloc(sizeof(*w));
668 if (w == NULL)
669 return NULL;
670 w->wiphy_idx = wiphy_idx;
671 dl_list_init(&w->bsss);
672 dl_list_init(&w->drvs);
673
674 w->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
675 if (!w->nl_cb) {
676 os_free(w);
677 return NULL;
678 }
679 nl_cb_set(w->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL);
680 nl_cb_set(w->nl_cb, NL_CB_VALID, NL_CB_CUSTOM, process_beacon_event,
681 w);
682
481234cf
JM
683 w->nl_beacons = nl_create_handle(bss->drv->global->nl_cb,
684 "wiphy beacons");
685 if (w->nl_beacons == NULL) {
e32ad281
JB
686 os_free(w);
687 return NULL;
688 }
689
690 if (nl80211_register_beacons(bss->drv, w)) {
691 nl_destroy_handles(&w->nl_beacons);
692 os_free(w);
693 return NULL;
694 }
695
481234cf
JM
696 eloop_register_read_sock(nl_socket_get_fd(w->nl_beacons),
697 nl80211_recv_beacons, w, w->nl_beacons);
e32ad281
JB
698
699 dl_list_add(&nl80211_wiphys, &w->list);
700
701add:
702 /* drv entry for this bss already there? */
703 dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
704 if (tmp_bss->drv == bss->drv) {
705 found = 1;
706 break;
707 }
708 }
709 /* if not add it */
710 if (!found)
711 dl_list_add(&w->drvs, &bss->drv->wiphy_list);
712
713 dl_list_add(&w->bsss, &bss->wiphy_list);
714 bss->wiphy_data = w;
715 return w;
716}
717
718
719static void nl80211_put_wiphy_data_ap(struct i802_bss *bss)
720{
721 struct nl80211_wiphy_data *w = bss->wiphy_data;
722 struct i802_bss *tmp_bss;
723 int found = 0;
724
725 if (w == NULL)
726 return;
727 bss->wiphy_data = NULL;
728 dl_list_del(&bss->wiphy_list);
729
730 /* still any for this drv present? */
731 dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
732 if (tmp_bss->drv == bss->drv) {
733 found = 1;
734 break;
735 }
736 }
737 /* if not remove it */
738 if (!found)
739 dl_list_del(&bss->drv->wiphy_list);
740
741 if (!dl_list_empty(&w->bsss))
742 return;
743
481234cf 744 eloop_unregister_read_sock(nl_socket_get_fd(w->nl_beacons));
e32ad281
JB
745
746 nl_cb_put(w->nl_cb);
747 nl_destroy_handles(&w->nl_beacons);
748 dl_list_del(&w->list);
749 os_free(w);
750}
751
752
3f5285e8
JM
753static int wpa_driver_nl80211_get_bssid(void *priv, u8 *bssid)
754{
a2e40bb6
FF
755 struct i802_bss *bss = priv;
756 struct wpa_driver_nl80211_data *drv = bss->drv;
c2a04078
JM
757 if (!drv->associated)
758 return -1;
759 os_memcpy(bssid, drv->bssid, ETH_ALEN);
760 return 0;
3f5285e8
JM
761}
762
763
3f5285e8
JM
764static int wpa_driver_nl80211_get_ssid(void *priv, u8 *ssid)
765{
a2e40bb6
FF
766 struct i802_bss *bss = priv;
767 struct wpa_driver_nl80211_data *drv = bss->drv;
fd05d64e
JM
768 if (!drv->associated)
769 return -1;
770 os_memcpy(ssid, drv->ssid, drv->ssid_len);
771 return drv->ssid_len;
3f5285e8
JM
772}
773
774
7524cfb1 775static void wpa_driver_nl80211_event_link(struct wpa_driver_nl80211_data *drv,
08063178 776 char *buf, size_t len, int del)
3f5285e8
JM
777{
778 union wpa_event_data event;
779
780 os_memset(&event, 0, sizeof(event));
781 if (len > sizeof(event.interface_status.ifname))
782 len = sizeof(event.interface_status.ifname) - 1;
783 os_memcpy(event.interface_status.ifname, buf, len);
784 event.interface_status.ievent = del ? EVENT_INTERFACE_REMOVED :
785 EVENT_INTERFACE_ADDED;
786
787 wpa_printf(MSG_DEBUG, "RTM_%sLINK, IFLA_IFNAME: Interface '%s' %s",
788 del ? "DEL" : "NEW",
789 event.interface_status.ifname,
790 del ? "removed" : "added");
791
a2e40bb6 792 if (os_strcmp(drv->first_bss.ifname, event.interface_status.ifname) == 0) {
d1f4942b
JM
793 if (del) {
794 if (drv->if_removed) {
795 wpa_printf(MSG_DEBUG, "nl80211: if_removed "
796 "already set - ignore event");
797 return;
798 }
7524cfb1 799 drv->if_removed = 1;
d1f4942b
JM
800 } else {
801 if (if_nametoindex(drv->first_bss.ifname) == 0) {
802 wpa_printf(MSG_DEBUG, "nl80211: Interface %s "
803 "does not exist - ignore "
804 "RTM_NEWLINK",
805 drv->first_bss.ifname);
806 return;
807 }
808 if (!drv->if_removed) {
809 wpa_printf(MSG_DEBUG, "nl80211: if_removed "
810 "already cleared - ignore event");
811 return;
812 }
7524cfb1 813 drv->if_removed = 0;
d1f4942b 814 }
7524cfb1
JM
815 }
816
08063178 817 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
3f5285e8
JM
818}
819
820
7524cfb1 821static int wpa_driver_nl80211_own_ifname(struct wpa_driver_nl80211_data *drv,
62d680c3 822 u8 *buf, size_t len)
7524cfb1 823{
62d680c3 824 int attrlen, rta_len;
7524cfb1
JM
825 struct rtattr *attr;
826
62d680c3
JM
827 attrlen = len;
828 attr = (struct rtattr *) buf;
7524cfb1
JM
829
830 rta_len = RTA_ALIGN(sizeof(struct rtattr));
831 while (RTA_OK(attr, attrlen)) {
832 if (attr->rta_type == IFLA_IFNAME) {
a2e40bb6 833 if (os_strcmp(((char *) attr) + rta_len, drv->first_bss.ifname)
7524cfb1
JM
834 == 0)
835 return 1;
836 else
837 break;
838 }
839 attr = RTA_NEXT(attr, attrlen);
840 }
841
842 return 0;
843}
844
845
846static int wpa_driver_nl80211_own_ifindex(struct wpa_driver_nl80211_data *drv,
62d680c3 847 int ifindex, u8 *buf, size_t len)
7524cfb1
JM
848{
849 if (drv->ifindex == ifindex)
850 return 1;
851
62d680c3 852 if (drv->if_removed && wpa_driver_nl80211_own_ifname(drv, buf, len)) {
a2e40bb6 853 drv->first_bss.ifindex = if_nametoindex(drv->first_bss.ifname);
7524cfb1
JM
854 wpa_printf(MSG_DEBUG, "nl80211: Update ifindex for a removed "
855 "interface");
856 wpa_driver_nl80211_finish_drv_init(drv);
857 return 1;
858 }
859
860 return 0;
861}
862
863
36d84860
BG
864static struct wpa_driver_nl80211_data *
865nl80211_find_drv(struct nl80211_global *global, int idx, u8 *buf, size_t len)
866{
867 struct wpa_driver_nl80211_data *drv;
868 dl_list_for_each(drv, &global->interfaces,
869 struct wpa_driver_nl80211_data, list) {
870 if (wpa_driver_nl80211_own_ifindex(drv, idx, buf, len) ||
871 have_ifidx(drv, idx))
872 return drv;
873 }
874 return NULL;
875}
876
877
62d680c3
JM
878static void wpa_driver_nl80211_event_rtm_newlink(void *ctx,
879 struct ifinfomsg *ifi,
880 u8 *buf, size_t len)
3f5285e8 881{
36d84860
BG
882 struct nl80211_global *global = ctx;
883 struct wpa_driver_nl80211_data *drv;
62d680c3
JM
884 int attrlen, rta_len;
885 struct rtattr *attr;
97cfcf64 886 u32 brid = 0;
aef85ba2 887 char namebuf[IFNAMSIZ];
3f5285e8 888
36d84860
BG
889 drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
890 if (!drv) {
97cfcf64
B
891 wpa_printf(MSG_DEBUG, "nl80211: Ignore event for foreign "
892 "ifindex %d", ifi->ifi_index);
3f5285e8
JM
893 return;
894 }
895
896 wpa_printf(MSG_DEBUG, "RTM_NEWLINK: operstate=%d ifi_flags=0x%x "
897 "(%s%s%s%s)",
898 drv->operstate, ifi->ifi_flags,
899 (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
900 (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
901 (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
902 (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
a63063b4
JM
903
904 if (!drv->if_disabled && !(ifi->ifi_flags & IFF_UP)) {
59d24925
JM
905 if (if_indextoname(ifi->ifi_index, namebuf) &&
906 linux_iface_up(drv->global->ioctl_sock,
907 drv->first_bss.ifname) > 0) {
908 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
909 "event since interface %s is up", namebuf);
910 return;
911 }
a63063b4 912 wpa_printf(MSG_DEBUG, "nl80211: Interface down");
7d9c3698
JM
913 if (drv->ignore_if_down_event) {
914 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
915 "event generated by mode change");
916 drv->ignore_if_down_event = 0;
917 } else {
918 drv->if_disabled = 1;
919 wpa_supplicant_event(drv->ctx,
920 EVENT_INTERFACE_DISABLED, NULL);
921 }
a63063b4
JM
922 }
923
924 if (drv->if_disabled && (ifi->ifi_flags & IFF_UP)) {
aef85ba2
JM
925 if (if_indextoname(ifi->ifi_index, namebuf) &&
926 linux_iface_up(drv->global->ioctl_sock,
927 drv->first_bss.ifname) == 0) {
928 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
929 "event since interface %s is down",
930 namebuf);
d1f4942b
JM
931 } else if (if_nametoindex(drv->first_bss.ifname) == 0) {
932 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
933 "event since interface %s does not exist",
934 drv->first_bss.ifname);
935 } else if (drv->if_removed) {
936 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
937 "event since interface %s is marked "
938 "removed", drv->first_bss.ifname);
aef85ba2
JM
939 } else {
940 wpa_printf(MSG_DEBUG, "nl80211: Interface up");
941 drv->if_disabled = 0;
942 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_ENABLED,
943 NULL);
944 }
a63063b4
JM
945 }
946
3f5285e8
JM
947 /*
948 * Some drivers send the association event before the operup event--in
949 * this case, lifting operstate in wpa_driver_nl80211_set_operstate()
950 * fails. This will hit us when wpa_supplicant does not need to do
951 * IEEE 802.1X authentication
952 */
953 if (drv->operstate == 1 &&
954 (ifi->ifi_flags & (IFF_LOWER_UP | IFF_DORMANT)) == IFF_LOWER_UP &&
955 !(ifi->ifi_flags & IFF_RUNNING))
36d84860 956 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
e2d02c29 957 -1, IF_OPER_UP);
3f5285e8 958
62d680c3
JM
959 attrlen = len;
960 attr = (struct rtattr *) buf;
3f5285e8
JM
961 rta_len = RTA_ALIGN(sizeof(struct rtattr));
962 while (RTA_OK(attr, attrlen)) {
d8816397 963 if (attr->rta_type == IFLA_IFNAME) {
7524cfb1 964 wpa_driver_nl80211_event_link(
08063178 965 drv,
7524cfb1
JM
966 ((char *) attr) + rta_len,
967 attr->rta_len - rta_len, 0);
97cfcf64
B
968 } else if (attr->rta_type == IFLA_MASTER)
969 brid = nla_get_u32((struct nlattr *) attr);
3f5285e8
JM
970 attr = RTA_NEXT(attr, attrlen);
971 }
97cfcf64
B
972
973 if (ifi->ifi_family == AF_BRIDGE && brid) {
974 /* device has been added to bridge */
97cfcf64
B
975 if_indextoname(brid, namebuf);
976 wpa_printf(MSG_DEBUG, "nl80211: Add ifindex %u for bridge %s",
977 brid, namebuf);
978 add_ifidx(drv, brid);
979 }
3f5285e8
JM
980}
981
982
62d680c3
JM
983static void wpa_driver_nl80211_event_rtm_dellink(void *ctx,
984 struct ifinfomsg *ifi,
985 u8 *buf, size_t len)
3f5285e8 986{
36d84860
BG
987 struct nl80211_global *global = ctx;
988 struct wpa_driver_nl80211_data *drv;
62d680c3
JM
989 int attrlen, rta_len;
990 struct rtattr *attr;
97cfcf64 991 u32 brid = 0;
3f5285e8 992
36d84860
BG
993 drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
994 if (!drv) {
995 wpa_printf(MSG_DEBUG, "nl80211: Ignore dellink event for "
996 "foreign ifindex %d", ifi->ifi_index);
997 return;
998 }
999
62d680c3
JM
1000 attrlen = len;
1001 attr = (struct rtattr *) buf;
3f5285e8
JM
1002
1003 rta_len = RTA_ALIGN(sizeof(struct rtattr));
1004 while (RTA_OK(attr, attrlen)) {
1005 if (attr->rta_type == IFLA_IFNAME) {
7524cfb1 1006 wpa_driver_nl80211_event_link(
08063178 1007 drv,
7524cfb1
JM
1008 ((char *) attr) + rta_len,
1009 attr->rta_len - rta_len, 1);
97cfcf64
B
1010 } else if (attr->rta_type == IFLA_MASTER)
1011 brid = nla_get_u32((struct nlattr *) attr);
3f5285e8
JM
1012 attr = RTA_NEXT(attr, attrlen);
1013 }
97cfcf64
B
1014
1015 if (ifi->ifi_family == AF_BRIDGE && brid) {
1016 /* device has been removed from bridge */
1017 char namebuf[IFNAMSIZ];
1018 if_indextoname(brid, namebuf);
1019 wpa_printf(MSG_DEBUG, "nl80211: Remove ifindex %u for bridge "
1020 "%s", brid, namebuf);
1021 del_ifidx(drv, brid);
1022 }
3f5285e8
JM
1023}
1024
1025
c2a04078
JM
1026static void mlme_event_auth(struct wpa_driver_nl80211_data *drv,
1027 const u8 *frame, size_t len)
1028{
1029 const struct ieee80211_mgmt *mgmt;
1030 union wpa_event_data event;
1031
7d81932d 1032 wpa_printf(MSG_DEBUG, "nl80211: Authenticate event");
c2a04078
JM
1033 mgmt = (const struct ieee80211_mgmt *) frame;
1034 if (len < 24 + sizeof(mgmt->u.auth)) {
1035 wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
1036 "frame");
1037 return;
1038 }
1039
e6b8efeb 1040 os_memcpy(drv->auth_bssid, mgmt->sa, ETH_ALEN);
c2a04078
JM
1041 os_memset(&event, 0, sizeof(event));
1042 os_memcpy(event.auth.peer, mgmt->sa, ETH_ALEN);
1043 event.auth.auth_type = le_to_host16(mgmt->u.auth.auth_alg);
569fed90
JM
1044 event.auth.auth_transaction =
1045 le_to_host16(mgmt->u.auth.auth_transaction);
c2a04078
JM
1046 event.auth.status_code = le_to_host16(mgmt->u.auth.status_code);
1047 if (len > 24 + sizeof(mgmt->u.auth)) {
1048 event.auth.ies = mgmt->u.auth.variable;
1049 event.auth.ies_len = len - 24 - sizeof(mgmt->u.auth);
1050 }
1051
1052 wpa_supplicant_event(drv->ctx, EVENT_AUTH, &event);
1053}
1054
1055
f5a8d422
JM
1056static unsigned int nl80211_get_assoc_freq(struct wpa_driver_nl80211_data *drv)
1057{
1058 struct nl_msg *msg;
1059 int ret;
1060 struct nl80211_bss_info_arg arg;
1061
1062 os_memset(&arg, 0, sizeof(arg));
1063 msg = nlmsg_alloc();
1064 if (!msg)
1065 goto nla_put_failure;
1066
9fb04070 1067 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
f5a8d422
JM
1068 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
1069
1070 arg.drv = drv;
1071 ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
1072 msg = NULL;
1073 if (ret == 0) {
1074 wpa_printf(MSG_DEBUG, "nl80211: Operating frequency for the "
1075 "associated BSS from scan results: %u MHz",
1076 arg.assoc_freq);
1077 return arg.assoc_freq ? arg.assoc_freq : drv->assoc_freq;
1078 }
1079 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
1080 "(%s)", ret, strerror(-ret));
1081nla_put_failure:
1082 nlmsg_free(msg);
1083 return drv->assoc_freq;
1084}
1085
1086
c2a04078
JM
1087static void mlme_event_assoc(struct wpa_driver_nl80211_data *drv,
1088 const u8 *frame, size_t len)
1089{
1090 const struct ieee80211_mgmt *mgmt;
1091 union wpa_event_data event;
1092 u16 status;
1093
7d81932d 1094 wpa_printf(MSG_DEBUG, "nl80211: Associate event");
c2a04078
JM
1095 mgmt = (const struct ieee80211_mgmt *) frame;
1096 if (len < 24 + sizeof(mgmt->u.assoc_resp)) {
1097 wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
1098 "frame");
1099 return;
1100 }
1101
1102 status = le_to_host16(mgmt->u.assoc_resp.status_code);
1103 if (status != WLAN_STATUS_SUCCESS) {
efa46078 1104 os_memset(&event, 0, sizeof(event));
59ddf221 1105 event.assoc_reject.bssid = mgmt->bssid;
efa46078
JM
1106 if (len > 24 + sizeof(mgmt->u.assoc_resp)) {
1107 event.assoc_reject.resp_ies =
1108 (u8 *) mgmt->u.assoc_resp.variable;
1109 event.assoc_reject.resp_ies_len =
1110 len - 24 - sizeof(mgmt->u.assoc_resp);
1111 }
1112 event.assoc_reject.status_code = status;
1113
1114 wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
c2a04078
JM
1115 return;
1116 }
1117
1118 drv->associated = 1;
1119 os_memcpy(drv->bssid, mgmt->sa, ETH_ALEN);
1120
1121 os_memset(&event, 0, sizeof(event));
1122 if (len > 24 + sizeof(mgmt->u.assoc_resp)) {
1123 event.assoc_info.resp_ies = (u8 *) mgmt->u.assoc_resp.variable;
1124 event.assoc_info.resp_ies_len =
efa46078 1125 len - 24 - sizeof(mgmt->u.assoc_resp);
c2a04078
JM
1126 }
1127
4832ecd7
JM
1128 event.assoc_info.freq = drv->assoc_freq;
1129
c2a04078
JM
1130 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
1131}
1132
c1bb3e0a 1133
da72a1c1
ZY
1134static void mlme_event_connect(struct wpa_driver_nl80211_data *drv,
1135 enum nl80211_commands cmd, struct nlattr *status,
1136 struct nlattr *addr, struct nlattr *req_ie,
1137 struct nlattr *resp_ie)
1138{
1139 union wpa_event_data event;
1140
7da2c527
JM
1141 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
1142 /*
1143 * Avoid reporting two association events that would confuse
1144 * the core code.
1145 */
1146 wpa_printf(MSG_DEBUG, "nl80211: Ignore connect event (cmd=%d) "
1147 "when using userspace SME", cmd);
1148 return;
1149 }
1150
7d81932d
JM
1151 if (cmd == NL80211_CMD_CONNECT)
1152 wpa_printf(MSG_DEBUG, "nl80211: Connect event");
1153 else if (cmd == NL80211_CMD_ROAM)
1154 wpa_printf(MSG_DEBUG, "nl80211: Roam event");
1155
da72a1c1
ZY
1156 os_memset(&event, 0, sizeof(event));
1157 if (cmd == NL80211_CMD_CONNECT &&
1158 nla_get_u16(status) != WLAN_STATUS_SUCCESS) {
df89c1c8
JM
1159 if (addr)
1160 event.assoc_reject.bssid = nla_data(addr);
da72a1c1
ZY
1161 if (resp_ie) {
1162 event.assoc_reject.resp_ies = nla_data(resp_ie);
1163 event.assoc_reject.resp_ies_len = nla_len(resp_ie);
1164 }
1165 event.assoc_reject.status_code = nla_get_u16(status);
1166 wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
1167 return;
1168 }
1169
1170 drv->associated = 1;
1171 if (addr)
1172 os_memcpy(drv->bssid, nla_data(addr), ETH_ALEN);
1173
1174 if (req_ie) {
1175 event.assoc_info.req_ies = nla_data(req_ie);
1176 event.assoc_info.req_ies_len = nla_len(req_ie);
1177 }
1178 if (resp_ie) {
1179 event.assoc_info.resp_ies = nla_data(resp_ie);
1180 event.assoc_info.resp_ies_len = nla_len(resp_ie);
1181 }
1182
f5a8d422
JM
1183 event.assoc_info.freq = nl80211_get_assoc_freq(drv);
1184
da72a1c1
ZY
1185 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
1186}
c2a04078 1187
c1bb3e0a 1188
20f5a4c2 1189static void mlme_event_disconnect(struct wpa_driver_nl80211_data *drv,
3d9975d5
JM
1190 struct nlattr *reason, struct nlattr *addr,
1191 struct nlattr *by_ap)
20f5a4c2
JM
1192{
1193 union wpa_event_data data;
a8c5b43a 1194 unsigned int locally_generated = by_ap == NULL;
20f5a4c2
JM
1195
1196 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
1197 /*
1198 * Avoid reporting two disassociation events that could
1199 * confuse the core code.
1200 */
1201 wpa_printf(MSG_DEBUG, "nl80211: Ignore disconnect "
1202 "event when using userspace SME");
1203 return;
1204 }
1205
a8c5b43a
CW
1206 if (drv->ignore_next_local_disconnect) {
1207 drv->ignore_next_local_disconnect = 0;
1208 if (locally_generated) {
1209 wpa_printf(MSG_DEBUG, "nl80211: Ignore disconnect "
1210 "event triggered during reassociation");
1211 return;
1212 }
1213 wpa_printf(MSG_WARNING, "nl80211: Was expecting local "
1214 "disconnect but got another disconnect "
1215 "event first");
1216 }
1217
7d81932d 1218 wpa_printf(MSG_DEBUG, "nl80211: Disconnect event");
20f5a4c2
JM
1219 drv->associated = 0;
1220 os_memset(&data, 0, sizeof(data));
1221 if (reason)
2e9f078c
JM
1222 data.deauth_info.reason_code = nla_get_u16(reason);
1223 data.deauth_info.locally_generated = by_ap == NULL;
1224 wpa_supplicant_event(drv->ctx, EVENT_DEAUTH, &data);
20f5a4c2
JM
1225}
1226
1227
1b487b8b
TP
1228static void mlme_event_ch_switch(struct wpa_driver_nl80211_data *drv,
1229 struct nlattr *freq, struct nlattr *type)
1230{
1231 union wpa_event_data data;
1232 int ht_enabled = 1;
1233 int chan_offset = 0;
1234
1235 wpa_printf(MSG_DEBUG, "nl80211: Channel switch event");
1236
1237 if (!freq || !type)
1238 return;
1239
1240 switch (nla_get_u32(type)) {
1241 case NL80211_CHAN_NO_HT:
1242 ht_enabled = 0;
1243 break;
1244 case NL80211_CHAN_HT20:
1245 break;
1246 case NL80211_CHAN_HT40PLUS:
1247 chan_offset = 1;
1248 break;
1249 case NL80211_CHAN_HT40MINUS:
1250 chan_offset = -1;
1251 break;
1252 }
1253
1254 data.ch_switch.freq = nla_get_u32(freq);
1255 data.ch_switch.ht_enabled = ht_enabled;
1256 data.ch_switch.ch_offset = chan_offset;
1257
1258 wpa_supplicant_event(drv->ctx, EVENT_CH_SWITCH, &data);
1259}
1260
1261
da1fb17c
JM
1262static void mlme_timeout_event(struct wpa_driver_nl80211_data *drv,
1263 enum nl80211_commands cmd, struct nlattr *addr)
1264{
1265 union wpa_event_data event;
1266 enum wpa_event_type ev;
1267
1268 if (nla_len(addr) != ETH_ALEN)
1269 return;
1270
1271 wpa_printf(MSG_DEBUG, "nl80211: MLME event %d; timeout with " MACSTR,
1272 cmd, MAC2STR((u8 *) nla_data(addr)));
1273
1274 if (cmd == NL80211_CMD_AUTHENTICATE)
1275 ev = EVENT_AUTH_TIMED_OUT;
1276 else if (cmd == NL80211_CMD_ASSOCIATE)
1277 ev = EVENT_ASSOC_TIMED_OUT;
1278 else
1279 return;
1280
1281 os_memset(&event, 0, sizeof(event));
1282 os_memcpy(event.timeout_event.addr, nla_data(addr), ETH_ALEN);
1283 wpa_supplicant_event(drv->ctx, ev, &event);
1284}
1285
1286
5582a5d1 1287static void mlme_event_mgmt(struct wpa_driver_nl80211_data *drv,
da873dbb
JB
1288 struct nlattr *freq, struct nlattr *sig,
1289 const u8 *frame, size_t len)
58f6fbe0
JM
1290{
1291 const struct ieee80211_mgmt *mgmt;
1292 union wpa_event_data event;
1293 u16 fc, stype;
da873dbb 1294 int ssi_signal = 0;
58f6fbe0 1295
cc2ada86 1296 wpa_printf(MSG_MSGDUMP, "nl80211: Frame event");
58f6fbe0
JM
1297 mgmt = (const struct ieee80211_mgmt *) frame;
1298 if (len < 24) {
1299 wpa_printf(MSG_DEBUG, "nl80211: Too short action frame");
1300 return;
1301 }
1302
1303 fc = le_to_host16(mgmt->frame_control);
1304 stype = WLAN_FC_GET_STYPE(fc);
1305
da873dbb
JB
1306 if (sig)
1307 ssi_signal = (s32) nla_get_u32(sig);
1308
58f6fbe0 1309 os_memset(&event, 0, sizeof(event));
5582a5d1 1310 if (freq) {
58f6fbe0 1311 event.rx_action.freq = nla_get_u32(freq);
5582a5d1
JB
1312 drv->last_mgmt_freq = event.rx_action.freq;
1313 }
1314 if (stype == WLAN_FC_STYPE_ACTION) {
1315 event.rx_action.da = mgmt->da;
1316 event.rx_action.sa = mgmt->sa;
1317 event.rx_action.bssid = mgmt->bssid;
1318 event.rx_action.category = mgmt->u.action.category;
1319 event.rx_action.data = &mgmt->u.action.category + 1;
1320 event.rx_action.len = frame + len - event.rx_action.data;
1321 wpa_supplicant_event(drv->ctx, EVENT_RX_ACTION, &event);
1322 } else {
1323 event.rx_mgmt.frame = frame;
1324 event.rx_mgmt.frame_len = len;
da873dbb 1325 event.rx_mgmt.ssi_signal = ssi_signal;
5582a5d1
JB
1326 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
1327 }
58f6fbe0
JM
1328}
1329
1330
a11241fa
JB
1331static void mlme_event_mgmt_tx_status(struct wpa_driver_nl80211_data *drv,
1332 struct nlattr *cookie, const u8 *frame,
1333 size_t len, struct nlattr *ack)
58f6fbe0
JM
1334{
1335 union wpa_event_data event;
1336 const struct ieee80211_hdr *hdr;
1337 u16 fc;
58f6fbe0 1338
7d81932d 1339 wpa_printf(MSG_DEBUG, "nl80211: Frame TX status event");
a11241fa
JB
1340 if (!is_ap_interface(drv->nlmode)) {
1341 u64 cookie_val;
58f6fbe0 1342
a11241fa
JB
1343 if (!cookie)
1344 return;
1345
1346 cookie_val = nla_get_u64(cookie);
1347 wpa_printf(MSG_DEBUG, "nl80211: Action TX status:"
1348 " cookie=0%llx%s (ack=%d)",
1349 (long long unsigned int) cookie_val,
1350 cookie_val == drv->send_action_cookie ?
1351 " (match)" : " (unknown)", ack != NULL);
1352 if (cookie_val != drv->send_action_cookie)
1353 return;
1354 }
58f6fbe0
JM
1355
1356 hdr = (const struct ieee80211_hdr *) frame;
1357 fc = le_to_host16(hdr->frame_control);
1358
1359 os_memset(&event, 0, sizeof(event));
1360 event.tx_status.type = WLAN_FC_GET_TYPE(fc);
1361 event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
1362 event.tx_status.dst = hdr->addr1;
1363 event.tx_status.data = frame;
1364 event.tx_status.data_len = len;
1365 event.tx_status.ack = ack != NULL;
1366 wpa_supplicant_event(drv->ctx, EVENT_TX_STATUS, &event);
1367}
1368
1369
0544b242
JM
1370static void mlme_event_deauth_disassoc(struct wpa_driver_nl80211_data *drv,
1371 enum wpa_event_type type,
1372 const u8 *frame, size_t len)
1373{
1374 const struct ieee80211_mgmt *mgmt;
1375 union wpa_event_data event;
1376 const u8 *bssid = NULL;
1377 u16 reason_code = 0;
1378
7d81932d
JM
1379 if (type == EVENT_DEAUTH)
1380 wpa_printf(MSG_DEBUG, "nl80211: Deauthenticate event");
1381 else
1382 wpa_printf(MSG_DEBUG, "nl80211: Disassociate event");
1383
cb30b297
PS
1384 mgmt = (const struct ieee80211_mgmt *) frame;
1385 if (len >= 24) {
1386 bssid = mgmt->bssid;
1387
1388 if (drv->associated != 0 &&
1389 os_memcmp(bssid, drv->bssid, ETH_ALEN) != 0 &&
1390 os_memcmp(bssid, drv->auth_bssid, ETH_ALEN) != 0) {
1391 /*
1392 * We have presumably received this deauth as a
1393 * response to a clear_state_mismatch() outgoing
1394 * deauth. Don't let it take us offline!
1395 */
1396 wpa_printf(MSG_DEBUG, "nl80211: Deauth received "
1397 "from Unknown BSSID " MACSTR " -- ignoring",
1398 MAC2STR(bssid));
1399 return;
1400 }
1401 }
1402
0544b242
JM
1403 drv->associated = 0;
1404 os_memset(&event, 0, sizeof(event));
1405
0544b242
JM
1406 /* Note: Same offset for Reason Code in both frame subtypes */
1407 if (len >= 24 + sizeof(mgmt->u.deauth))
1408 reason_code = le_to_host16(mgmt->u.deauth.reason_code);
1409
1410 if (type == EVENT_DISASSOC) {
3d9975d5
JM
1411 event.disassoc_info.locally_generated =
1412 !os_memcmp(mgmt->sa, drv->first_bss.addr, ETH_ALEN);
0544b242
JM
1413 event.disassoc_info.addr = bssid;
1414 event.disassoc_info.reason_code = reason_code;
046b26a2
JM
1415 if (frame + len > mgmt->u.disassoc.variable) {
1416 event.disassoc_info.ie = mgmt->u.disassoc.variable;
1417 event.disassoc_info.ie_len = frame + len -
1418 mgmt->u.disassoc.variable;
1419 }
0544b242 1420 } else {
3d9975d5
JM
1421 event.deauth_info.locally_generated =
1422 !os_memcmp(mgmt->sa, drv->first_bss.addr, ETH_ALEN);
0544b242
JM
1423 event.deauth_info.addr = bssid;
1424 event.deauth_info.reason_code = reason_code;
046b26a2
JM
1425 if (frame + len > mgmt->u.deauth.variable) {
1426 event.deauth_info.ie = mgmt->u.deauth.variable;
1427 event.deauth_info.ie_len = frame + len -
1428 mgmt->u.deauth.variable;
1429 }
0544b242
JM
1430 }
1431
1432 wpa_supplicant_event(drv->ctx, type, &event);
1433}
1434
1435
7d878ca7
JM
1436static void mlme_event_unprot_disconnect(struct wpa_driver_nl80211_data *drv,
1437 enum wpa_event_type type,
1438 const u8 *frame, size_t len)
1439{
1440 const struct ieee80211_mgmt *mgmt;
1441 union wpa_event_data event;
1442 u16 reason_code = 0;
1443
7d81932d
JM
1444 if (type == EVENT_UNPROT_DEAUTH)
1445 wpa_printf(MSG_DEBUG, "nl80211: Unprot Deauthenticate event");
1446 else
1447 wpa_printf(MSG_DEBUG, "nl80211: Unprot Disassociate event");
1448
7d878ca7
JM
1449 if (len < 24)
1450 return;
1451
1452 mgmt = (const struct ieee80211_mgmt *) frame;
1453
1454 os_memset(&event, 0, sizeof(event));
1455 /* Note: Same offset for Reason Code in both frame subtypes */
1456 if (len >= 24 + sizeof(mgmt->u.deauth))
1457 reason_code = le_to_host16(mgmt->u.deauth.reason_code);
1458
1459 if (type == EVENT_UNPROT_DISASSOC) {
1460 event.unprot_disassoc.sa = mgmt->sa;
1461 event.unprot_disassoc.da = mgmt->da;
1462 event.unprot_disassoc.reason_code = reason_code;
1463 } else {
1464 event.unprot_deauth.sa = mgmt->sa;
1465 event.unprot_deauth.da = mgmt->da;
1466 event.unprot_deauth.reason_code = reason_code;
1467 }
1468
1469 wpa_supplicant_event(drv->ctx, type, &event);
1470}
1471
1472
97279d8d 1473static void mlme_event(struct i802_bss *bss,
da1fb17c 1474 enum nl80211_commands cmd, struct nlattr *frame,
58f6fbe0
JM
1475 struct nlattr *addr, struct nlattr *timed_out,
1476 struct nlattr *freq, struct nlattr *ack,
da873dbb 1477 struct nlattr *cookie, struct nlattr *sig)
c2a04078 1478{
97279d8d
JM
1479 struct wpa_driver_nl80211_data *drv = bss->drv;
1480 const u8 *data;
1481 size_t len;
1482
da1fb17c
JM
1483 if (timed_out && addr) {
1484 mlme_timeout_event(drv, cmd, addr);
1485 return;
1486 }
1487
c2a04078
JM
1488 if (frame == NULL) {
1489 wpa_printf(MSG_DEBUG, "nl80211: MLME event %d without frame "
1490 "data", cmd);
1491 return;
1492 }
1493
97279d8d
JM
1494 data = nla_data(frame);
1495 len = nla_len(frame);
1496 if (len < 4 + ETH_ALEN) {
1497 wpa_printf(MSG_MSGDUMP, "nl80211: MLME event %d on %s(" MACSTR
1498 ") - too short",
1499 cmd, bss->ifname, MAC2STR(bss->addr));
1500 return;
1501 }
1502 wpa_printf(MSG_MSGDUMP, "nl80211: MLME event %d on %s(" MACSTR ") A1="
1503 MACSTR, cmd, bss->ifname, MAC2STR(bss->addr),
1504 MAC2STR(data + 4));
1505 if (cmd != NL80211_CMD_FRAME_TX_STATUS && !(data[4] & 0x01) &&
1506 os_memcmp(bss->addr, data + 4, ETH_ALEN) != 0) {
1507 wpa_printf(MSG_MSGDUMP, "nl80211: %s: Ignore MLME frame event "
1508 "for foreign address", bss->ifname);
1509 return;
1510 }
c2a04078
JM
1511 wpa_hexdump(MSG_MSGDUMP, "nl80211: MLME event frame",
1512 nla_data(frame), nla_len(frame));
1513
1514 switch (cmd) {
1515 case NL80211_CMD_AUTHENTICATE:
1516 mlme_event_auth(drv, nla_data(frame), nla_len(frame));
1517 break;
1518 case NL80211_CMD_ASSOCIATE:
1519 mlme_event_assoc(drv, nla_data(frame), nla_len(frame));
1520 break;
1521 case NL80211_CMD_DEAUTHENTICATE:
0544b242
JM
1522 mlme_event_deauth_disassoc(drv, EVENT_DEAUTH,
1523 nla_data(frame), nla_len(frame));
c2a04078
JM
1524 break;
1525 case NL80211_CMD_DISASSOCIATE:
0544b242
JM
1526 mlme_event_deauth_disassoc(drv, EVENT_DISASSOC,
1527 nla_data(frame), nla_len(frame));
c2a04078 1528 break;
bd94971e 1529 case NL80211_CMD_FRAME:
da873dbb
JB
1530 mlme_event_mgmt(drv, freq, sig, nla_data(frame),
1531 nla_len(frame));
58f6fbe0 1532 break;
bd94971e 1533 case NL80211_CMD_FRAME_TX_STATUS:
a11241fa
JB
1534 mlme_event_mgmt_tx_status(drv, cookie, nla_data(frame),
1535 nla_len(frame), ack);
58f6fbe0 1536 break;
7d878ca7
JM
1537 case NL80211_CMD_UNPROT_DEAUTHENTICATE:
1538 mlme_event_unprot_disconnect(drv, EVENT_UNPROT_DEAUTH,
1539 nla_data(frame), nla_len(frame));
1540 break;
1541 case NL80211_CMD_UNPROT_DISASSOCIATE:
1542 mlme_event_unprot_disconnect(drv, EVENT_UNPROT_DISASSOC,
1543 nla_data(frame), nla_len(frame));
1544 break;
c2a04078
JM
1545 default:
1546 break;
1547 }
1548}
1549
1550
a5e1eb20 1551static void mlme_event_michael_mic_failure(struct i802_bss *bss,
35583f3f
JM
1552 struct nlattr *tb[])
1553{
1554 union wpa_event_data data;
1555
1556 wpa_printf(MSG_DEBUG, "nl80211: MLME event Michael MIC failure");
1557 os_memset(&data, 0, sizeof(data));
1558 if (tb[NL80211_ATTR_MAC]) {
1559 wpa_hexdump(MSG_DEBUG, "nl80211: Source MAC address",
1560 nla_data(tb[NL80211_ATTR_MAC]),
1561 nla_len(tb[NL80211_ATTR_MAC]));
ad1e68e6 1562 data.michael_mic_failure.src = nla_data(tb[NL80211_ATTR_MAC]);
35583f3f
JM
1563 }
1564 if (tb[NL80211_ATTR_KEY_SEQ]) {
1565 wpa_hexdump(MSG_DEBUG, "nl80211: TSC",
1566 nla_data(tb[NL80211_ATTR_KEY_SEQ]),
1567 nla_len(tb[NL80211_ATTR_KEY_SEQ]));
1568 }
1569 if (tb[NL80211_ATTR_KEY_TYPE]) {
1570 enum nl80211_key_type key_type =
1571 nla_get_u32(tb[NL80211_ATTR_KEY_TYPE]);
1572 wpa_printf(MSG_DEBUG, "nl80211: Key Type %d", key_type);
1573 if (key_type == NL80211_KEYTYPE_PAIRWISE)
1574 data.michael_mic_failure.unicast = 1;
1575 } else
1576 data.michael_mic_failure.unicast = 1;
1577
1578 if (tb[NL80211_ATTR_KEY_IDX]) {
1579 u8 key_id = nla_get_u8(tb[NL80211_ATTR_KEY_IDX]);
1580 wpa_printf(MSG_DEBUG, "nl80211: Key Id %d", key_id);
1581 }
1582
a5e1eb20 1583 wpa_supplicant_event(bss->ctx, EVENT_MICHAEL_MIC_FAILURE, &data);
35583f3f
JM
1584}
1585
1586
5cc4d64b
JM
1587static void mlme_event_join_ibss(struct wpa_driver_nl80211_data *drv,
1588 struct nlattr *tb[])
1589{
1590 if (tb[NL80211_ATTR_MAC] == NULL) {
1591 wpa_printf(MSG_DEBUG, "nl80211: No address in IBSS joined "
1592 "event");
1593 return;
1594 }
1595 os_memcpy(drv->bssid, nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
1596 drv->associated = 1;
1597 wpa_printf(MSG_DEBUG, "nl80211: IBSS " MACSTR " joined",
1598 MAC2STR(drv->bssid));
1599
1600 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, NULL);
1601}
1602
1603
55777702
JM
1604static void mlme_event_remain_on_channel(struct wpa_driver_nl80211_data *drv,
1605 int cancel_event, struct nlattr *tb[])
1606{
1607 unsigned int freq, chan_type, duration;
1608 union wpa_event_data data;
1609 u64 cookie;
1610
1611 if (tb[NL80211_ATTR_WIPHY_FREQ])
1612 freq = nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]);
1613 else
1614 freq = 0;
1615
1616 if (tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE])
1617 chan_type = nla_get_u32(tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1618 else
1619 chan_type = 0;
1620
1621 if (tb[NL80211_ATTR_DURATION])
1622 duration = nla_get_u32(tb[NL80211_ATTR_DURATION]);
1623 else
1624 duration = 0;
1625
1626 if (tb[NL80211_ATTR_COOKIE])
1627 cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
1628 else
1629 cookie = 0;
1630
1631 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel event (cancel=%d "
1632 "freq=%u channel_type=%u duration=%u cookie=0x%llx (%s))",
1633 cancel_event, freq, chan_type, duration,
1634 (long long unsigned int) cookie,
1635 cookie == drv->remain_on_chan_cookie ? "match" : "unknown");
1636
1637 if (cookie != drv->remain_on_chan_cookie)
1638 return; /* not for us */
1639
531f0331
JB
1640 if (cancel_event)
1641 drv->pending_remain_on_chan = 0;
55777702
JM
1642
1643 os_memset(&data, 0, sizeof(data));
1644 data.remain_on_channel.freq = freq;
1645 data.remain_on_channel.duration = duration;
1646 wpa_supplicant_event(drv->ctx, cancel_event ?
1647 EVENT_CANCEL_REMAIN_ON_CHANNEL :
1648 EVENT_REMAIN_ON_CHANNEL, &data);
1649}
1650
1651
6a1ce395
DG
1652static void mlme_event_ft_event(struct wpa_driver_nl80211_data *drv,
1653 struct nlattr *tb[])
1654{
1655 union wpa_event_data data;
1656
1657 os_memset(&data, 0, sizeof(data));
1658
1659 if (tb[NL80211_ATTR_IE]) {
1660 data.ft_ies.ies = nla_data(tb[NL80211_ATTR_IE]);
1661 data.ft_ies.ies_len = nla_len(tb[NL80211_ATTR_IE]);
1662 }
1663
1664 if (tb[NL80211_ATTR_IE_RIC]) {
1665 data.ft_ies.ric_ies = nla_data(tb[NL80211_ATTR_IE_RIC]);
1666 data.ft_ies.ric_ies_len = nla_len(tb[NL80211_ATTR_IE_RIC]);
1667 }
1668
1669 if (tb[NL80211_ATTR_MAC])
1670 os_memcpy(data.ft_ies.target_ap,
1671 nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
1672
1673 wpa_printf(MSG_DEBUG, "nl80211: FT event target_ap " MACSTR,
1674 MAC2STR(data.ft_ies.target_ap));
1675
1676 wpa_supplicant_event(drv->ctx, EVENT_FT_RESPONSE, &data);
1677}
1678
1679
8d923a4a
JM
1680static void send_scan_event(struct wpa_driver_nl80211_data *drv, int aborted,
1681 struct nlattr *tb[])
1682{
1683 union wpa_event_data event;
1684 struct nlattr *nl;
1685 int rem;
1686 struct scan_info *info;
1687#define MAX_REPORT_FREQS 50
1688 int freqs[MAX_REPORT_FREQS];
1689 int num_freqs = 0;
1690
536fd62d
JM
1691 if (drv->scan_for_auth) {
1692 drv->scan_for_auth = 0;
1693 wpa_printf(MSG_DEBUG, "nl80211: Scan results for missing "
1694 "cfg80211 BSS entry");
1695 wpa_driver_nl80211_authenticate_retry(drv);
1696 return;
1697 }
1698
8d923a4a
JM
1699 os_memset(&event, 0, sizeof(event));
1700 info = &event.scan_info;
1701 info->aborted = aborted;
1702
1703 if (tb[NL80211_ATTR_SCAN_SSIDS]) {
1704 nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_SSIDS], rem) {
1705 struct wpa_driver_scan_ssid *s =
1706 &info->ssids[info->num_ssids];
1707 s->ssid = nla_data(nl);
1708 s->ssid_len = nla_len(nl);
1709 info->num_ssids++;
1710 if (info->num_ssids == WPAS_MAX_SCAN_SSIDS)
1711 break;
1712 }
1713 }
1714 if (tb[NL80211_ATTR_SCAN_FREQUENCIES]) {
1715 nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_FREQUENCIES], rem)
1716 {
1717 freqs[num_freqs] = nla_get_u32(nl);
1718 num_freqs++;
1719 if (num_freqs == MAX_REPORT_FREQS - 1)
1720 break;
1721 }
1722 info->freqs = freqs;
1723 info->num_freqs = num_freqs;
1724 }
1725 wpa_supplicant_event(drv->ctx, EVENT_SCAN_RESULTS, &event);
1726}
1727
1728
60a972a6
JM
1729static int get_link_signal(struct nl_msg *msg, void *arg)
1730{
1731 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1732 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1733 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
1734 static struct nla_policy policy[NL80211_STA_INFO_MAX + 1] = {
1735 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
1736 };
7ee35bf3
PS
1737 struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
1738 static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
1739 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
1740 [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
1741 [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
1742 [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
1743 };
1c5c7273 1744 struct wpa_signal_info *sig_change = arg;
60a972a6
JM
1745
1746 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1747 genlmsg_attrlen(gnlh, 0), NULL);
1748 if (!tb[NL80211_ATTR_STA_INFO] ||
1749 nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
1750 tb[NL80211_ATTR_STA_INFO], policy))
1751 return NL_SKIP;
1752 if (!sinfo[NL80211_STA_INFO_SIGNAL])
1753 return NL_SKIP;
1754
7ee35bf3
PS
1755 sig_change->current_signal =
1756 (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
1757
1758 if (sinfo[NL80211_STA_INFO_TX_BITRATE]) {
1759 if (nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
1760 sinfo[NL80211_STA_INFO_TX_BITRATE],
1761 rate_policy)) {
1762 sig_change->current_txrate = 0;
1763 } else {
1764 if (rinfo[NL80211_RATE_INFO_BITRATE]) {
1765 sig_change->current_txrate =
1766 nla_get_u16(rinfo[
1767 NL80211_RATE_INFO_BITRATE]) * 100;
1768 }
1769 }
1770 }
1771
60a972a6
JM
1772 return NL_SKIP;
1773}
1774
1775
1776static int nl80211_get_link_signal(struct wpa_driver_nl80211_data *drv,
1c5c7273 1777 struct wpa_signal_info *sig)
60a972a6
JM
1778{
1779 struct nl_msg *msg;
1780
7ee35bf3
PS
1781 sig->current_signal = -9999;
1782 sig->current_txrate = 0;
60a972a6
JM
1783
1784 msg = nlmsg_alloc();
1785 if (!msg)
1786 return -ENOMEM;
1787
9fb04070 1788 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_STATION);
60a972a6
JM
1789
1790 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
1791 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid);
1792
1793 return send_and_recv_msgs(drv, msg, get_link_signal, sig);
1794 nla_put_failure:
5883168a 1795 nlmsg_free(msg);
60a972a6
JM
1796 return -ENOBUFS;
1797}
1798
1799
7ee35bf3
PS
1800static int get_link_noise(struct nl_msg *msg, void *arg)
1801{
1802 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1803 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1804 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
1805 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
1806 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
1807 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
1808 };
1c5c7273 1809 struct wpa_signal_info *sig_change = arg;
7ee35bf3
PS
1810
1811 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1812 genlmsg_attrlen(gnlh, 0), NULL);
1813
1814 if (!tb[NL80211_ATTR_SURVEY_INFO]) {
1815 wpa_printf(MSG_DEBUG, "nl80211: survey data missing!");
1816 return NL_SKIP;
1817 }
1818
1819 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
1820 tb[NL80211_ATTR_SURVEY_INFO],
1821 survey_policy)) {
1822 wpa_printf(MSG_DEBUG, "nl80211: failed to parse nested "
1823 "attributes!");
1824 return NL_SKIP;
1825 }
1826
1827 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
1828 return NL_SKIP;
1829
1830 if (nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
1831 sig_change->frequency)
1832 return NL_SKIP;
1833
1834 if (!sinfo[NL80211_SURVEY_INFO_NOISE])
1835 return NL_SKIP;
1836
1837 sig_change->current_noise =
1838 (s8) nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
1839
1840 return NL_SKIP;
1841}
1842
1843
1844static int nl80211_get_link_noise(struct wpa_driver_nl80211_data *drv,
1c5c7273 1845 struct wpa_signal_info *sig_change)
7ee35bf3
PS
1846{
1847 struct nl_msg *msg;
1848
1849 sig_change->current_noise = 9999;
1850 sig_change->frequency = drv->assoc_freq;
1851
1852 msg = nlmsg_alloc();
1853 if (!msg)
1854 return -ENOMEM;
1855
9fb04070 1856 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
7ee35bf3
PS
1857
1858 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
1859
1860 return send_and_recv_msgs(drv, msg, get_link_noise, sig_change);
1861 nla_put_failure:
5883168a 1862 nlmsg_free(msg);
7ee35bf3
PS
1863 return -ENOBUFS;
1864}
1865
1866
577db0ae
GM
1867static int get_noise_for_scan_results(struct nl_msg *msg, void *arg)
1868{
1869 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1870 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1871 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
1872 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
1873 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
1874 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
1875 };
1876 struct wpa_scan_results *scan_results = arg;
1877 struct wpa_scan_res *scan_res;
1878 size_t i;
1879
1880 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1881 genlmsg_attrlen(gnlh, 0), NULL);
1882
1883 if (!tb[NL80211_ATTR_SURVEY_INFO]) {
1884 wpa_printf(MSG_DEBUG, "nl80211: Survey data missing");
1885 return NL_SKIP;
1886 }
1887
1888 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
1889 tb[NL80211_ATTR_SURVEY_INFO],
1890 survey_policy)) {
1891 wpa_printf(MSG_DEBUG, "nl80211: Failed to parse nested "
1892 "attributes");
1893 return NL_SKIP;
1894 }
1895
1896 if (!sinfo[NL80211_SURVEY_INFO_NOISE])
1897 return NL_SKIP;
1898
1899 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
1900 return NL_SKIP;
1901
1902 for (i = 0; i < scan_results->num; ++i) {
1903 scan_res = scan_results->res[i];
1904 if (!scan_res)
1905 continue;
1906 if ((int) nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
1907 scan_res->freq)
1908 continue;
1909 if (!(scan_res->flags & WPA_SCAN_NOISE_INVALID))
1910 continue;
1911 scan_res->noise = (s8)
1912 nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
1913 scan_res->flags &= ~WPA_SCAN_NOISE_INVALID;
1914 }
1915
1916 return NL_SKIP;
1917}
1918
1919
1920static int nl80211_get_noise_for_scan_results(
1921 struct wpa_driver_nl80211_data *drv,
1922 struct wpa_scan_results *scan_res)
1923{
1924 struct nl_msg *msg;
1925
1926 msg = nlmsg_alloc();
1927 if (!msg)
1928 return -ENOMEM;
1929
1930 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
1931
1932 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
1933
1934 return send_and_recv_msgs(drv, msg, get_noise_for_scan_results,
1935 scan_res);
1936 nla_put_failure:
9e088e74 1937 nlmsg_free(msg);
577db0ae
GM
1938 return -ENOBUFS;
1939}
1940
1941
93910401
JM
1942static void nl80211_cqm_event(struct wpa_driver_nl80211_data *drv,
1943 struct nlattr *tb[])
1944{
1945 static struct nla_policy cqm_policy[NL80211_ATTR_CQM_MAX + 1] = {
1946 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
1947 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U8 },
1948 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
0d7e5a3a 1949 [NL80211_ATTR_CQM_PKT_LOSS_EVENT] = { .type = NLA_U32 },
93910401
JM
1950 };
1951 struct nlattr *cqm[NL80211_ATTR_CQM_MAX + 1];
1952 enum nl80211_cqm_rssi_threshold_event event;
b625473c 1953 union wpa_event_data ed;
1c5c7273 1954 struct wpa_signal_info sig;
7ee35bf3 1955 int res;
93910401
JM
1956
1957 if (tb[NL80211_ATTR_CQM] == NULL ||
1958 nla_parse_nested(cqm, NL80211_ATTR_CQM_MAX, tb[NL80211_ATTR_CQM],
1959 cqm_policy)) {
1960 wpa_printf(MSG_DEBUG, "nl80211: Ignore invalid CQM event");
1961 return;
1962 }
1963
0d7e5a3a
JB
1964 os_memset(&ed, 0, sizeof(ed));
1965
1966 if (cqm[NL80211_ATTR_CQM_PKT_LOSS_EVENT]) {
1967 if (!tb[NL80211_ATTR_MAC])
1968 return;
1969 os_memcpy(ed.low_ack.addr, nla_data(tb[NL80211_ATTR_MAC]),
1970 ETH_ALEN);
1971 wpa_supplicant_event(drv->ctx, EVENT_STATION_LOW_ACK, &ed);
1972 return;
1973 }
1974
93910401
JM
1975 if (cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] == NULL)
1976 return;
1977 event = nla_get_u32(cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT]);
b625473c 1978
93910401
JM
1979 if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH) {
1980 wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor "
1981 "event: RSSI high");
b625473c 1982 ed.signal_change.above_threshold = 1;
93910401
JM
1983 } else if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW) {
1984 wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor "
1985 "event: RSSI low");
b625473c
JM
1986 ed.signal_change.above_threshold = 0;
1987 } else
1988 return;
1989
60a972a6
JM
1990 res = nl80211_get_link_signal(drv, &sig);
1991 if (res == 0) {
7ee35bf3
PS
1992 ed.signal_change.current_signal = sig.current_signal;
1993 ed.signal_change.current_txrate = sig.current_txrate;
1994 wpa_printf(MSG_DEBUG, "nl80211: Signal: %d dBm txrate: %d",
1995 sig.current_signal, sig.current_txrate);
1996 }
1997
1998 res = nl80211_get_link_noise(drv, &sig);
1999 if (res == 0) {
2000 ed.signal_change.current_noise = sig.current_noise;
2001 wpa_printf(MSG_DEBUG, "nl80211: Noise: %d dBm",
2002 sig.current_noise);
60a972a6
JM
2003 }
2004
b625473c 2005 wpa_supplicant_event(drv->ctx, EVENT_SIGNAL_CHANGE, &ed);
93910401
JM
2006}
2007
2008
18d2ba08
JM
2009static void nl80211_new_station_event(struct wpa_driver_nl80211_data *drv,
2010 struct nlattr **tb)
2011{
2012 u8 *addr;
2013 union wpa_event_data data;
2014
2015 if (tb[NL80211_ATTR_MAC] == NULL)
2016 return;
2017 addr = nla_data(tb[NL80211_ATTR_MAC]);
2018 wpa_printf(MSG_DEBUG, "nl80211: New station " MACSTR, MAC2STR(addr));
5f310a9e 2019
61cbe2ff 2020 if (is_ap_interface(drv->nlmode) && drv->device_ap_sme) {
5f310a9e
JM
2021 u8 *ies = NULL;
2022 size_t ies_len = 0;
2023 if (tb[NL80211_ATTR_IE]) {
2024 ies = nla_data(tb[NL80211_ATTR_IE]);
2025 ies_len = nla_len(tb[NL80211_ATTR_IE]);
2026 }
2027 wpa_hexdump(MSG_DEBUG, "nl80211: Assoc Req IEs", ies, ies_len);
2028 drv_event_assoc(drv->ctx, addr, ies, ies_len, 0);
2029 return;
2030 }
2031
18d2ba08
JM
2032 if (drv->nlmode != NL80211_IFTYPE_ADHOC)
2033 return;
2034
2035 os_memset(&data, 0, sizeof(data));
2036 os_memcpy(data.ibss_rsn_start.peer, addr, ETH_ALEN);
2037 wpa_supplicant_event(drv->ctx, EVENT_IBSS_RSN_START, &data);
2038}
2039
2040
ef985058
JM
2041static void nl80211_del_station_event(struct wpa_driver_nl80211_data *drv,
2042 struct nlattr **tb)
2043{
2044 u8 *addr;
2045 union wpa_event_data data;
2046
2047 if (tb[NL80211_ATTR_MAC] == NULL)
2048 return;
2049 addr = nla_data(tb[NL80211_ATTR_MAC]);
2050 wpa_printf(MSG_DEBUG, "nl80211: Delete station " MACSTR,
2051 MAC2STR(addr));
5f310a9e 2052
61cbe2ff 2053 if (is_ap_interface(drv->nlmode) && drv->device_ap_sme) {
5f310a9e
JM
2054 drv_event_disassoc(drv->ctx, addr);
2055 return;
2056 }
2057
ef985058
JM
2058 if (drv->nlmode != NL80211_IFTYPE_ADHOC)
2059 return;
2060
2061 os_memset(&data, 0, sizeof(data));
2062 os_memcpy(data.ibss_peer_lost.peer, addr, ETH_ALEN);
2063 wpa_supplicant_event(drv->ctx, EVENT_IBSS_PEER_LOST, &data);
2064}
2065
2066
b14a210c
JB
2067static void nl80211_rekey_offload_event(struct wpa_driver_nl80211_data *drv,
2068 struct nlattr **tb)
2069{
2070 struct nlattr *rekey_info[NUM_NL80211_REKEY_DATA];
2071 static struct nla_policy rekey_policy[NUM_NL80211_REKEY_DATA] = {
2072 [NL80211_REKEY_DATA_KEK] = {
2073 .minlen = NL80211_KEK_LEN,
2074 .maxlen = NL80211_KEK_LEN,
2075 },
2076 [NL80211_REKEY_DATA_KCK] = {
2077 .minlen = NL80211_KCK_LEN,
2078 .maxlen = NL80211_KCK_LEN,
2079 },
2080 [NL80211_REKEY_DATA_REPLAY_CTR] = {
2081 .minlen = NL80211_REPLAY_CTR_LEN,
2082 .maxlen = NL80211_REPLAY_CTR_LEN,
2083 },
2084 };
2085 union wpa_event_data data;
2086
2087 if (!tb[NL80211_ATTR_MAC])
2088 return;
2089 if (!tb[NL80211_ATTR_REKEY_DATA])
2090 return;
2091 if (nla_parse_nested(rekey_info, MAX_NL80211_REKEY_DATA,
2092 tb[NL80211_ATTR_REKEY_DATA], rekey_policy))
2093 return;
2094 if (!rekey_info[NL80211_REKEY_DATA_REPLAY_CTR])
2095 return;
2096
2097 os_memset(&data, 0, sizeof(data));
2098 data.driver_gtk_rekey.bssid = nla_data(tb[NL80211_ATTR_MAC]);
2099 wpa_printf(MSG_DEBUG, "nl80211: Rekey offload event for BSSID " MACSTR,
2100 MAC2STR(data.driver_gtk_rekey.bssid));
2101 data.driver_gtk_rekey.replay_ctr =
2102 nla_data(rekey_info[NL80211_REKEY_DATA_REPLAY_CTR]);
2103 wpa_hexdump(MSG_DEBUG, "nl80211: Rekey offload - Replay Counter",
2104 data.driver_gtk_rekey.replay_ctr, NL80211_REPLAY_CTR_LEN);
2105 wpa_supplicant_event(drv->ctx, EVENT_DRIVER_GTK_REKEY, &data);
2106}
2107
2108
c36d5242
JM
2109static void nl80211_pmksa_candidate_event(struct wpa_driver_nl80211_data *drv,
2110 struct nlattr **tb)
2111{
2112 struct nlattr *cand[NUM_NL80211_PMKSA_CANDIDATE];
2113 static struct nla_policy cand_policy[NUM_NL80211_PMKSA_CANDIDATE] = {
2114 [NL80211_PMKSA_CANDIDATE_INDEX] = { .type = NLA_U32 },
2115 [NL80211_PMKSA_CANDIDATE_BSSID] = {
2116 .minlen = ETH_ALEN,
2117 .maxlen = ETH_ALEN,
2118 },
2119 [NL80211_PMKSA_CANDIDATE_PREAUTH] = { .type = NLA_FLAG },
2120 };
2121 union wpa_event_data data;
2122
7d81932d
JM
2123 wpa_printf(MSG_DEBUG, "nl80211: PMKSA candidate event");
2124
c36d5242
JM
2125 if (!tb[NL80211_ATTR_PMKSA_CANDIDATE])
2126 return;
2127 if (nla_parse_nested(cand, MAX_NL80211_PMKSA_CANDIDATE,
2128 tb[NL80211_ATTR_PMKSA_CANDIDATE], cand_policy))
2129 return;
2130 if (!cand[NL80211_PMKSA_CANDIDATE_INDEX] ||
2131 !cand[NL80211_PMKSA_CANDIDATE_BSSID])
2132 return;
2133
2134 os_memset(&data, 0, sizeof(data));
2135 os_memcpy(data.pmkid_candidate.bssid,
2136 nla_data(cand[NL80211_PMKSA_CANDIDATE_BSSID]), ETH_ALEN);
2137 data.pmkid_candidate.index =
2138 nla_get_u32(cand[NL80211_PMKSA_CANDIDATE_INDEX]);
2139 data.pmkid_candidate.preauth =
2140 cand[NL80211_PMKSA_CANDIDATE_PREAUTH] != NULL;
2141 wpa_supplicant_event(drv->ctx, EVENT_PMKID_CANDIDATE, &data);
2142}
2143
2144
39718852
JB
2145static void nl80211_client_probe_event(struct wpa_driver_nl80211_data *drv,
2146 struct nlattr **tb)
2147{
2148 union wpa_event_data data;
2149
7d81932d
JM
2150 wpa_printf(MSG_DEBUG, "nl80211: Probe client event");
2151
39718852
JB
2152 if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_ACK])
2153 return;
2154
2155 os_memset(&data, 0, sizeof(data));
2156 os_memcpy(data.client_poll.addr,
2157 nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
2158
2159 wpa_supplicant_event(drv->ctx, EVENT_DRIVER_CLIENT_POLL_OK, &data);
2160}
2161
2162
6201b052
JM
2163static void nl80211_tdls_oper_event(struct wpa_driver_nl80211_data *drv,
2164 struct nlattr **tb)
2165{
2166 union wpa_event_data data;
2167
2168 wpa_printf(MSG_DEBUG, "nl80211: TDLS operation event");
2169
2170 if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_TDLS_OPERATION])
2171 return;
2172
2173 os_memset(&data, 0, sizeof(data));
2174 os_memcpy(data.tdls.peer, nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
2175 switch (nla_get_u8(tb[NL80211_ATTR_TDLS_OPERATION])) {
2176 case NL80211_TDLS_SETUP:
2177 wpa_printf(MSG_DEBUG, "nl80211: TDLS setup request for peer "
2178 MACSTR, MAC2STR(data.tdls.peer));
2179 data.tdls.oper = TDLS_REQUEST_SETUP;
2180 break;
2181 case NL80211_TDLS_TEARDOWN:
2182 wpa_printf(MSG_DEBUG, "nl80211: TDLS teardown request for peer "
2183 MACSTR, MAC2STR(data.tdls.peer));
2184 data.tdls.oper = TDLS_REQUEST_TEARDOWN;
2185 break;
2186 default:
2187 wpa_printf(MSG_DEBUG, "nl80211: Unsupported TDLS operatione "
2188 "event");
2189 return;
2190 }
2191 if (tb[NL80211_ATTR_REASON_CODE]) {
2192 data.tdls.reason_code =
2193 nla_get_u16(tb[NL80211_ATTR_REASON_CODE]);
2194 }
2195
2196 wpa_supplicant_event(drv->ctx, EVENT_TDLS, &data);
2197}
2198
2199
3140803b
RM
2200static void nl80211_connect_failed_event(struct wpa_driver_nl80211_data *drv,
2201 struct nlattr **tb)
2202{
2203 union wpa_event_data data;
2204 u32 reason;
2205
2206 wpa_printf(MSG_DEBUG, "nl80211: Connect failed event");
2207
2208 if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_CONN_FAILED_REASON])
2209 return;
2210
2211 os_memset(&data, 0, sizeof(data));
2212 os_memcpy(data.connect_failed_reason.addr,
2213 nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
2214
2215 reason = nla_get_u32(tb[NL80211_ATTR_CONN_FAILED_REASON]);
2216 switch (reason) {
2217 case NL80211_CONN_FAIL_MAX_CLIENTS:
2218 wpa_printf(MSG_DEBUG, "nl80211: Max client reached");
2219 data.connect_failed_reason.code = MAX_CLIENT_REACHED;
2220 break;
2221 case NL80211_CONN_FAIL_BLOCKED_CLIENT:
2222 wpa_printf(MSG_DEBUG, "nl80211: Blocked client " MACSTR
2223 " tried to connect",
2224 MAC2STR(data.connect_failed_reason.addr));
2225 data.connect_failed_reason.code = BLOCKED_CLIENT;
2226 break;
2227 default:
2228 wpa_printf(MSG_DEBUG, "nl8021l: Unknown connect failed reason "
2229 "%u", reason);
2230 return;
2231 }
2232
2233 wpa_supplicant_event(drv->ctx, EVENT_CONNECT_FAILED_REASON, &data);
2234}
2235
2236
04be54fa
SW
2237static void nl80211_radar_event(struct wpa_driver_nl80211_data *drv,
2238 struct nlattr **tb)
2239{
2240 union wpa_event_data data;
2241 enum nl80211_radar_event event_type;
2242
2243 if (!tb[NL80211_ATTR_WIPHY_FREQ] || !tb[NL80211_ATTR_RADAR_EVENT])
2244 return;
2245
2246 os_memset(&data, 0, sizeof(data));
2247 data.dfs_event.freq = nla_get_u16(tb[NL80211_ATTR_WIPHY_FREQ]);
2248 event_type = nla_get_u8(tb[NL80211_ATTR_RADAR_EVENT]);
2249
2250 wpa_printf(MSG_DEBUG, "nl80211: DFS event on freq %d MHz",
2251 data.dfs_event.freq);
2252
2253 switch (event_type) {
2254 case NL80211_RADAR_DETECTED:
2255 wpa_supplicant_event(drv->ctx, EVENT_DFS_RADAR_DETECTED, &data);
2256 break;
2257 case NL80211_RADAR_CAC_FINISHED:
2258 wpa_supplicant_event(drv->ctx, EVENT_DFS_CAC_FINISHED, &data);
2259 break;
2260 case NL80211_RADAR_CAC_ABORTED:
2261 wpa_supplicant_event(drv->ctx, EVENT_DFS_CAC_ABORTED, &data);
2262 break;
2263 case NL80211_RADAR_NOP_FINISHED:
2264 wpa_supplicant_event(drv->ctx, EVENT_DFS_NOP_FINISHED, &data);
2265 break;
2266 default:
2267 wpa_printf(MSG_DEBUG, "nl80211: Unknown radar event %d "
2268 "received", event_type);
2269 break;
2270 }
2271}
2272
2273
3088e4e5
JB
2274static void nl80211_spurious_frame(struct i802_bss *bss, struct nlattr **tb,
2275 int wds)
02bb32c3
JB
2276{
2277 struct wpa_driver_nl80211_data *drv = bss->drv;
2278 union wpa_event_data event;
02bb32c3
JB
2279
2280 if (!tb[NL80211_ATTR_MAC])
2281 return;
2282
02bb32c3 2283 os_memset(&event, 0, sizeof(event));
341eebee 2284 event.rx_from_unknown.bssid = bss->addr;
02bb32c3 2285 event.rx_from_unknown.addr = nla_data(tb[NL80211_ATTR_MAC]);
3088e4e5 2286 event.rx_from_unknown.wds = wds;
02bb32c3
JB
2287
2288 wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event);
2289}
2290
2291
a5e1eb20
SE
2292static void do_process_drv_event(struct i802_bss *bss, int cmd,
2293 struct nlattr **tb)
97865538 2294{
a5e1eb20
SE
2295 struct wpa_driver_nl80211_data *drv = bss->drv;
2296
b1f625e0 2297 if (drv->ap_scan_as_station != NL80211_IFTYPE_UNSPECIFIED &&
d6c9aab8
JB
2298 (cmd == NL80211_CMD_NEW_SCAN_RESULTS ||
2299 cmd == NL80211_CMD_SCAN_ABORTED)) {
a2e40bb6 2300 wpa_driver_nl80211_set_mode(&drv->first_bss,
b1f625e0
EP
2301 drv->ap_scan_as_station);
2302 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
ad1e68e6
JM
2303 }
2304
d6c9aab8 2305 switch (cmd) {
d942a79e 2306 case NL80211_CMD_TRIGGER_SCAN:
565110cd 2307 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Scan trigger");
d942a79e 2308 break;
d21c63b9 2309 case NL80211_CMD_START_SCHED_SCAN:
565110cd 2310 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Sched scan started");
d21c63b9
LC
2311 break;
2312 case NL80211_CMD_SCHED_SCAN_STOPPED:
565110cd 2313 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Sched scan stopped");
d21c63b9
LC
2314 wpa_supplicant_event(drv->ctx, EVENT_SCHED_SCAN_STOPPED, NULL);
2315 break;
97865538 2316 case NL80211_CMD_NEW_SCAN_RESULTS:
565110cd
JM
2317 wpa_dbg(drv->ctx, MSG_DEBUG,
2318 "nl80211: New scan results available");
97865538
JM
2319 drv->scan_complete_events = 1;
2320 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
2321 drv->ctx);
8d923a4a 2322 send_scan_event(drv, 0, tb);
97865538 2323 break;
d21c63b9 2324 case NL80211_CMD_SCHED_SCAN_RESULTS:
565110cd
JM
2325 wpa_dbg(drv->ctx, MSG_DEBUG,
2326 "nl80211: New sched scan results available");
d21c63b9
LC
2327 send_scan_event(drv, 0, tb);
2328 break;
97865538 2329 case NL80211_CMD_SCAN_ABORTED:
565110cd 2330 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Scan aborted");
97865538
JM
2331 /*
2332 * Need to indicate that scan results are available in order
2333 * not to make wpa_supplicant stop its scanning.
2334 */
2335 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
2336 drv->ctx);
8d923a4a 2337 send_scan_event(drv, 1, tb);
97865538 2338 break;
c2a04078
JM
2339 case NL80211_CMD_AUTHENTICATE:
2340 case NL80211_CMD_ASSOCIATE:
2341 case NL80211_CMD_DEAUTHENTICATE:
2342 case NL80211_CMD_DISASSOCIATE:
bd94971e 2343 case NL80211_CMD_FRAME_TX_STATUS:
7d878ca7
JM
2344 case NL80211_CMD_UNPROT_DEAUTHENTICATE:
2345 case NL80211_CMD_UNPROT_DISASSOCIATE:
97279d8d 2346 mlme_event(bss, cmd, tb[NL80211_ATTR_FRAME],
58f6fbe0
JM
2347 tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT],
2348 tb[NL80211_ATTR_WIPHY_FREQ], tb[NL80211_ATTR_ACK],
da873dbb
JB
2349 tb[NL80211_ATTR_COOKIE],
2350 tb[NL80211_ATTR_RX_SIGNAL_DBM]);
c2a04078 2351 break;
da72a1c1
ZY
2352 case NL80211_CMD_CONNECT:
2353 case NL80211_CMD_ROAM:
d6c9aab8 2354 mlme_event_connect(drv, cmd,
da72a1c1
ZY
2355 tb[NL80211_ATTR_STATUS_CODE],
2356 tb[NL80211_ATTR_MAC],
2357 tb[NL80211_ATTR_REQ_IE],
2358 tb[NL80211_ATTR_RESP_IE]);
2359 break;
1b487b8b
TP
2360 case NL80211_CMD_CH_SWITCH_NOTIFY:
2361 mlme_event_ch_switch(drv, tb[NL80211_ATTR_WIPHY_FREQ],
2362 tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
2363 break;
da72a1c1 2364 case NL80211_CMD_DISCONNECT:
20f5a4c2 2365 mlme_event_disconnect(drv, tb[NL80211_ATTR_REASON_CODE],
3d9975d5
JM
2366 tb[NL80211_ATTR_MAC],
2367 tb[NL80211_ATTR_DISCONNECTED_BY_AP]);
da72a1c1 2368 break;
35583f3f 2369 case NL80211_CMD_MICHAEL_MIC_FAILURE:
a5e1eb20 2370 mlme_event_michael_mic_failure(bss, tb);
35583f3f 2371 break;
5cc4d64b
JM
2372 case NL80211_CMD_JOIN_IBSS:
2373 mlme_event_join_ibss(drv, tb);
2374 break;
55777702
JM
2375 case NL80211_CMD_REMAIN_ON_CHANNEL:
2376 mlme_event_remain_on_channel(drv, 0, tb);
2377 break;
2378 case NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL:
2379 mlme_event_remain_on_channel(drv, 1, tb);
2380 break;
93910401
JM
2381 case NL80211_CMD_NOTIFY_CQM:
2382 nl80211_cqm_event(drv, tb);
2383 break;
33c5deb8
JM
2384 case NL80211_CMD_REG_CHANGE:
2385 wpa_printf(MSG_DEBUG, "nl80211: Regulatory domain change");
2386 wpa_supplicant_event(drv->ctx, EVENT_CHANNEL_LIST_CHANGED,
2387 NULL);
2388 break;
2389 case NL80211_CMD_REG_BEACON_HINT:
2390 wpa_printf(MSG_DEBUG, "nl80211: Regulatory beacon hint");
2391 wpa_supplicant_event(drv->ctx, EVENT_CHANNEL_LIST_CHANGED,
2392 NULL);
2393 break;
18d2ba08
JM
2394 case NL80211_CMD_NEW_STATION:
2395 nl80211_new_station_event(drv, tb);
2396 break;
ef985058
JM
2397 case NL80211_CMD_DEL_STATION:
2398 nl80211_del_station_event(drv, tb);
2399 break;
b14a210c
JB
2400 case NL80211_CMD_SET_REKEY_OFFLOAD:
2401 nl80211_rekey_offload_event(drv, tb);
2402 break;
c36d5242
JM
2403 case NL80211_CMD_PMKSA_CANDIDATE:
2404 nl80211_pmksa_candidate_event(drv, tb);
2405 break;
39718852
JB
2406 case NL80211_CMD_PROBE_CLIENT:
2407 nl80211_client_probe_event(drv, tb);
2408 break;
6201b052
JM
2409 case NL80211_CMD_TDLS_OPER:
2410 nl80211_tdls_oper_event(drv, tb);
2411 break;
3140803b
RM
2412 case NL80211_CMD_CONN_FAILED:
2413 nl80211_connect_failed_event(drv, tb);
2414 break;
6a1ce395
DG
2415 case NL80211_CMD_FT_EVENT:
2416 mlme_event_ft_event(drv, tb);
2417 break;
04be54fa
SW
2418 case NL80211_CMD_RADAR_DETECT:
2419 nl80211_radar_event(drv, tb);
2420 break;
97865538 2421 default:
565110cd
JM
2422 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Ignored unknown event "
2423 "(cmd=%d)", cmd);
97865538
JM
2424 break;
2425 }
d6c9aab8
JB
2426}
2427
2428
2429static int process_drv_event(struct nl_msg *msg, void *arg)
2430{
2431 struct wpa_driver_nl80211_data *drv = arg;
2432 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2433 struct nlattr *tb[NL80211_ATTR_MAX + 1];
a5e1eb20
SE
2434 struct i802_bss *bss;
2435 int ifidx = -1;
d6c9aab8
JB
2436
2437 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2438 genlmsg_attrlen(gnlh, 0), NULL);
2439
a5e1eb20
SE
2440 if (tb[NL80211_ATTR_IFINDEX])
2441 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
2442
2443 for (bss = &drv->first_bss; bss; bss = bss->next) {
2444 if (ifidx == -1 || ifidx == bss->ifindex) {
2445 do_process_drv_event(bss, gnlh->cmd, tb);
d6c9aab8
JB
2446 return NL_SKIP;
2447 }
2448 }
2449
a5e1eb20
SE
2450 wpa_printf(MSG_DEBUG, "nl80211: Ignored event (cmd=%d) for foreign "
2451 "interface (ifindex %d)", gnlh->cmd, ifidx);
2452
d6c9aab8
JB
2453 return NL_SKIP;
2454}
2455
2456
2457static int process_global_event(struct nl_msg *msg, void *arg)
2458{
2459 struct nl80211_global *global = arg;
2460 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2461 struct nlattr *tb[NL80211_ATTR_MAX + 1];
24b5bd8b 2462 struct wpa_driver_nl80211_data *drv, *tmp;
d6c9aab8 2463 int ifidx = -1;
a5e1eb20 2464 struct i802_bss *bss;
d6c9aab8
JB
2465
2466 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2467 genlmsg_attrlen(gnlh, 0), NULL);
2468
2469 if (tb[NL80211_ATTR_IFINDEX])
2470 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
2471
24b5bd8b
JB
2472 dl_list_for_each_safe(drv, tmp, &global->interfaces,
2473 struct wpa_driver_nl80211_data, list) {
a5e1eb20
SE
2474 for (bss = &drv->first_bss; bss; bss = bss->next) {
2475 if (ifidx == -1 || ifidx == bss->ifindex) {
2476 do_process_drv_event(bss, gnlh->cmd, tb);
2477 return NL_SKIP;
2478 }
2479 }
d6c9aab8 2480 }
97865538
JM
2481
2482 return NL_SKIP;
2483}
2484
2485
cc7a48d1
JB
2486static int process_bss_event(struct nl_msg *msg, void *arg)
2487{
a11241fa 2488 struct i802_bss *bss = arg;
cc7a48d1
JB
2489 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2490 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2491
2492 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2493 genlmsg_attrlen(gnlh, 0), NULL);
2494
2495 switch (gnlh->cmd) {
a11241fa
JB
2496 case NL80211_CMD_FRAME:
2497 case NL80211_CMD_FRAME_TX_STATUS:
97279d8d 2498 mlme_event(bss, gnlh->cmd, tb[NL80211_ATTR_FRAME],
a11241fa
JB
2499 tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT],
2500 tb[NL80211_ATTR_WIPHY_FREQ], tb[NL80211_ATTR_ACK],
da873dbb
JB
2501 tb[NL80211_ATTR_COOKIE],
2502 tb[NL80211_ATTR_RX_SIGNAL_DBM]);
a11241fa 2503 break;
02bb32c3 2504 case NL80211_CMD_UNEXPECTED_FRAME:
3088e4e5
JB
2505 nl80211_spurious_frame(bss, tb, 0);
2506 break;
2507 case NL80211_CMD_UNEXPECTED_4ADDR_FRAME:
2508 nl80211_spurious_frame(bss, tb, 1);
02bb32c3 2509 break;
cc7a48d1
JB
2510 default:
2511 wpa_printf(MSG_DEBUG, "nl80211: Ignored unknown event "
2512 "(cmd=%d)", gnlh->cmd);
2513 break;
2514 }
2515
2516 return NL_SKIP;
2517}
2518
2519
97865538 2520static void wpa_driver_nl80211_event_receive(int sock, void *eloop_ctx,
5582a5d1 2521 void *handle)
97865538 2522{
a4ae123c 2523 struct nl_cb *cb = eloop_ctx;
97865538 2524
cc2ada86 2525 wpa_printf(MSG_MSGDUMP, "nl80211: Event message available");
97865538 2526
a4ae123c 2527 nl_recvmsgs(handle, cb);
97865538
JM
2528}
2529
2530
6d158490
LR
2531/**
2532 * wpa_driver_nl80211_set_country - ask nl80211 to set the regulatory domain
2533 * @priv: driver_nl80211 private data
2534 * @alpha2_arg: country to which to switch to
2535 * Returns: 0 on success, -1 on failure
2536 *
2537 * This asks nl80211 to set the regulatory domain for given
2538 * country ISO / IEC alpha2.
2539 */
2540static int wpa_driver_nl80211_set_country(void *priv, const char *alpha2_arg)
2541{
a2e40bb6
FF
2542 struct i802_bss *bss = priv;
2543 struct wpa_driver_nl80211_data *drv = bss->drv;
6d158490
LR
2544 char alpha2[3];
2545 struct nl_msg *msg;
2546
2547 msg = nlmsg_alloc();
2548 if (!msg)
e785c2ba 2549 return -ENOMEM;
6d158490
LR
2550
2551 alpha2[0] = alpha2_arg[0];
2552 alpha2[1] = alpha2_arg[1];
2553 alpha2[2] = '\0';
2554
9fb04070 2555 nl80211_cmd(drv, msg, 0, NL80211_CMD_REQ_SET_REG);
6d158490
LR
2556
2557 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, alpha2);
2558 if (send_and_recv_msgs(drv, msg, NULL, NULL))
2559 return -EINVAL;
2560 return 0;
2561nla_put_failure:
5883168a 2562 nlmsg_free(msg);
6d158490
LR
2563 return -EINVAL;
2564}
2565
2566
43245552
DJ
2567static int protocol_feature_handler(struct nl_msg *msg, void *arg)
2568{
2569 u32 *feat = arg;
2570 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
2571 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2572
2573 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2574 genlmsg_attrlen(gnlh, 0), NULL);
2575
2576 if (tb_msg[NL80211_ATTR_PROTOCOL_FEATURES])
2577 *feat = nla_get_u32(tb_msg[NL80211_ATTR_PROTOCOL_FEATURES]);
2578
2579 return NL_SKIP;
2580}
2581
2582
2583static u32 get_nl80211_protocol_features(struct wpa_driver_nl80211_data *drv)
2584{
2585 u32 feat = 0;
2586 struct nl_msg *msg;
2587
2588 msg = nlmsg_alloc();
2589 if (!msg)
2590 goto nla_put_failure;
2591
2592 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_PROTOCOL_FEATURES);
2593 if (send_and_recv_msgs(drv, msg, protocol_feature_handler, &feat) == 0)
2594 return feat;
2595
2596 msg = NULL;
2597nla_put_failure:
2598 nlmsg_free(msg);
2599 return 0;
2600}
2601
2602
80bc75f1 2603struct wiphy_info_data {
8cd6b7bc 2604 struct wpa_driver_nl80211_data *drv;
e8b5e24e
JB
2605 struct wpa_driver_capa *capa;
2606
2607 unsigned int error:1;
61cbe2ff 2608 unsigned int device_ap_sme:1;
39718852 2609 unsigned int poll_command_supported:1;
32ab4855 2610 unsigned int data_tx_status:1;
536062f2 2611 unsigned int monitor_supported:1;
43245552
DJ
2612 unsigned int auth_supported:1;
2613 unsigned int connect_supported:1;
2614 unsigned int p2p_go_supported:1;
2615 unsigned int p2p_client_supported:1;
2616 unsigned int p2p_concurrent:1;
2617 unsigned int p2p_multichan_concurrent:1;
80bc75f1
JM
2618};
2619
2620
562c9d97
AN
2621static unsigned int probe_resp_offload_support(int supp_protocols)
2622{
2623 unsigned int prot = 0;
2624
2625 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS)
2626 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS;
2627 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2)
2628 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS2;
2629 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P)
2630 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_P2P;
2631 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_80211U)
2632 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_INTERWORKING;
2633
2634 return prot;
2635}
2636
2637
5f439107
JM
2638static void wiphy_info_supported_iftypes(struct wiphy_info_data *info,
2639 struct nlattr *tb)
80bc75f1 2640{
5f439107
JM
2641 struct nlattr *nl_mode;
2642 int i;
2643
2644 if (tb == NULL)
2645 return;
2646
2647 nla_for_each_nested(nl_mode, tb, i) {
2648 switch (nla_type(nl_mode)) {
2649 case NL80211_IFTYPE_AP:
2650 info->capa->flags |= WPA_DRIVER_FLAGS_AP;
2651 break;
65d52fc1
BR
2652 case NL80211_IFTYPE_ADHOC:
2653 info->capa->flags |= WPA_DRIVER_FLAGS_IBSS;
2654 break;
5f439107
JM
2655 case NL80211_IFTYPE_P2P_GO:
2656 info->p2p_go_supported = 1;
2657 break;
2658 case NL80211_IFTYPE_P2P_CLIENT:
2659 info->p2p_client_supported = 1;
2660 break;
2661 case NL80211_IFTYPE_MONITOR:
2662 info->monitor_supported = 1;
2663 break;
2664 }
2665 }
2666}
2667
2668
2669static int wiphy_info_iface_comb_process(struct wiphy_info_data *info,
2670 struct nlattr *nl_combi)
2671{
2672 struct nlattr *tb_comb[NUM_NL80211_IFACE_COMB];
2673 struct nlattr *tb_limit[NUM_NL80211_IFACE_LIMIT];
2674 struct nlattr *nl_limit, *nl_mode;
2675 int err, rem_limit, rem_mode;
2676 int combination_has_p2p = 0, combination_has_mgd = 0;
7626850d
JB
2677 static struct nla_policy
2678 iface_combination_policy[NUM_NL80211_IFACE_COMB] = {
2679 [NL80211_IFACE_COMB_LIMITS] = { .type = NLA_NESTED },
2680 [NL80211_IFACE_COMB_MAXNUM] = { .type = NLA_U32 },
2681 [NL80211_IFACE_COMB_STA_AP_BI_MATCH] = { .type = NLA_FLAG },
2682 [NL80211_IFACE_COMB_NUM_CHANNELS] = { .type = NLA_U32 },
f295d0c8 2683 [NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS] = { .type = NLA_U32 },
7626850d
JB
2684 },
2685 iface_limit_policy[NUM_NL80211_IFACE_LIMIT] = {
2686 [NL80211_IFACE_LIMIT_TYPES] = { .type = NLA_NESTED },
2687 [NL80211_IFACE_LIMIT_MAX] = { .type = NLA_U32 },
2688 };
80bc75f1 2689
5f439107
JM
2690 err = nla_parse_nested(tb_comb, MAX_NL80211_IFACE_COMB,
2691 nl_combi, iface_combination_policy);
2692 if (err || !tb_comb[NL80211_IFACE_COMB_LIMITS] ||
2693 !tb_comb[NL80211_IFACE_COMB_MAXNUM] ||
2694 !tb_comb[NL80211_IFACE_COMB_NUM_CHANNELS])
2695 return 0; /* broken combination */
2696
f295d0c8
SW
2697 if (tb_comb[NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS])
2698 info->capa->flags |= WPA_DRIVER_FLAGS_RADAR;
2699
5f439107
JM
2700 nla_for_each_nested(nl_limit, tb_comb[NL80211_IFACE_COMB_LIMITS],
2701 rem_limit) {
2702 err = nla_parse_nested(tb_limit, MAX_NL80211_IFACE_LIMIT,
2703 nl_limit, iface_limit_policy);
2704 if (err || !tb_limit[NL80211_IFACE_LIMIT_TYPES])
2705 return 0; /* broken combination */
2706
2707 nla_for_each_nested(nl_mode,
2708 tb_limit[NL80211_IFACE_LIMIT_TYPES],
2709 rem_mode) {
2710 int ift = nla_type(nl_mode);
2711 if (ift == NL80211_IFTYPE_P2P_GO ||
2712 ift == NL80211_IFTYPE_P2P_CLIENT)
2713 combination_has_p2p = 1;
2714 if (ift == NL80211_IFTYPE_STATION)
2715 combination_has_mgd = 1;
2716 }
2717 if (combination_has_p2p && combination_has_mgd)
2718 break;
2719 }
2720
2721 if (combination_has_p2p && combination_has_mgd) {
2722 info->p2p_concurrent = 1;
2723 if (nla_get_u32(tb_comb[NL80211_IFACE_COMB_NUM_CHANNELS]) > 1)
2724 info->p2p_multichan_concurrent = 1;
2725 return 1;
2726 }
2727
2728 return 0;
2729}
2730
2731
2732static void wiphy_info_iface_comb(struct wiphy_info_data *info,
2733 struct nlattr *tb)
2734{
2735 struct nlattr *nl_combi;
2736 int rem_combi;
2737
2738 if (tb == NULL)
2739 return;
2740
2741 nla_for_each_nested(nl_combi, tb, rem_combi) {
2742 if (wiphy_info_iface_comb_process(info, nl_combi) > 0)
2743 break;
2744 }
2745}
2746
2747
2748static void wiphy_info_supp_cmds(struct wiphy_info_data *info,
2749 struct nlattr *tb)
2750{
2751 struct nlattr *nl_cmd;
2752 int i;
2753
2754 if (tb == NULL)
2755 return;
2756
2757 nla_for_each_nested(nl_cmd, tb, i) {
2758 switch (nla_get_u32(nl_cmd)) {
2759 case NL80211_CMD_AUTHENTICATE:
2760 info->auth_supported = 1;
2761 break;
2762 case NL80211_CMD_CONNECT:
2763 info->connect_supported = 1;
2764 break;
2765 case NL80211_CMD_START_SCHED_SCAN:
2766 info->capa->sched_scan_supported = 1;
2767 break;
2768 case NL80211_CMD_PROBE_CLIENT:
2769 info->poll_command_supported = 1;
2770 break;
2771 }
2772 }
2773}
2774
2775
2776static void wiphy_info_max_roc(struct wpa_driver_capa *capa,
2777 struct nlattr *tb)
2778{
5f439107
JM
2779 if (tb)
2780 capa->max_remain_on_chan = nla_get_u32(tb);
2781}
2782
2783
2784static void wiphy_info_tdls(struct wpa_driver_capa *capa, struct nlattr *tdls,
2785 struct nlattr *ext_setup)
2786{
2787 if (tdls == NULL)
2788 return;
2789
2790 wpa_printf(MSG_DEBUG, "nl80211: TDLS supported");
2791 capa->flags |= WPA_DRIVER_FLAGS_TDLS_SUPPORT;
2792
2793 if (ext_setup) {
2794 wpa_printf(MSG_DEBUG, "nl80211: TDLS external setup");
2795 capa->flags |= WPA_DRIVER_FLAGS_TDLS_EXTERNAL_SETUP;
2796 }
2797}
2798
2799
2800static void wiphy_info_feature_flags(struct wiphy_info_data *info,
2801 struct nlattr *tb)
2802{
2803 u32 flags;
2804 struct wpa_driver_capa *capa = info->capa;
2805
2806 if (tb == NULL)
2807 return;
2808
2809 flags = nla_get_u32(tb);
2810
2811 if (flags & NL80211_FEATURE_SK_TX_STATUS)
2812 info->data_tx_status = 1;
2813
2814 if (flags & NL80211_FEATURE_INACTIVITY_TIMER)
2815 capa->flags |= WPA_DRIVER_FLAGS_INACTIVITY_TIMER;
2816
2817 if (flags & NL80211_FEATURE_SAE)
2818 capa->flags |= WPA_DRIVER_FLAGS_SAE;
2819
2820 if (flags & NL80211_FEATURE_NEED_OBSS_SCAN)
2821 capa->flags |= WPA_DRIVER_FLAGS_OBSS_SCAN;
2822}
2823
2824
2825static void wiphy_info_probe_resp_offload(struct wpa_driver_capa *capa,
2826 struct nlattr *tb)
2827{
2828 u32 protocols;
2829
2830 if (tb == NULL)
2831 return;
2832
2833 protocols = nla_get_u32(tb);
2834 wpa_printf(MSG_DEBUG, "nl80211: Supports Probe Response offload in AP "
2835 "mode");
2836 capa->flags |= WPA_DRIVER_FLAGS_PROBE_RESP_OFFLOAD;
2837 capa->probe_resp_offloads = probe_resp_offload_support(protocols);
2838}
2839
2840
2841static int wiphy_info_handler(struct nl_msg *msg, void *arg)
2842{
2843 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2844 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2845 struct wiphy_info_data *info = arg;
2846 struct wpa_driver_capa *capa = info->capa;
8cd6b7bc 2847 struct wpa_driver_nl80211_data *drv = info->drv;
5f439107 2848
80bc75f1
JM
2849 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2850 genlmsg_attrlen(gnlh, 0), NULL);
2851
2852 if (tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS])
e8b5e24e 2853 capa->max_scan_ssids =
80bc75f1
JM
2854 nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS]);
2855
d21c63b9 2856 if (tb[NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS])
e8b5e24e 2857 capa->max_sched_scan_ssids =
d21c63b9
LC
2858 nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS]);
2859
bd525934 2860 if (tb[NL80211_ATTR_MAX_MATCH_SETS])
e8b5e24e 2861 capa->max_match_sets =
bd525934
LC
2862 nla_get_u8(tb[NL80211_ATTR_MAX_MATCH_SETS]);
2863
5f439107
JM
2864 wiphy_info_supported_iftypes(info, tb[NL80211_ATTR_SUPPORTED_IFTYPES]);
2865 wiphy_info_iface_comb(info, tb[NL80211_ATTR_INTERFACE_COMBINATIONS]);
2866 wiphy_info_supp_cmds(info, tb[NL80211_ATTR_SUPPORTED_COMMANDS]);
93d11400 2867
e8b5e24e
JB
2868 if (tb[NL80211_ATTR_OFFCHANNEL_TX_OK]) {
2869 wpa_printf(MSG_DEBUG, "nl80211: Using driver-based "
2870 "off-channel TX");
2871 capa->flags |= WPA_DRIVER_FLAGS_OFFCHANNEL_TX;
2872 }
2873
2874 if (tb[NL80211_ATTR_ROAM_SUPPORT]) {
2875 wpa_printf(MSG_DEBUG, "nl80211: Using driver-based roaming");
2876 capa->flags |= WPA_DRIVER_FLAGS_BSS_SELECTION;
2877 }
5dfca53f 2878
5f439107
JM
2879 wiphy_info_max_roc(capa,
2880 tb[NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION]);
004ba773 2881
70619a5d
EP
2882 if (tb[NL80211_ATTR_SUPPORT_AP_UAPSD])
2883 capa->flags |= WPA_DRIVER_FLAGS_AP_UAPSD;
2884
5f439107
JM
2885 wiphy_info_tdls(capa, tb[NL80211_ATTR_TDLS_SUPPORT],
2886 tb[NL80211_ATTR_TDLS_EXTERNAL_SETUP]);
03ea1786 2887
61cbe2ff
JB
2888 if (tb[NL80211_ATTR_DEVICE_AP_SME])
2889 info->device_ap_sme = 1;
2890
5f439107
JM
2891 wiphy_info_feature_flags(info, tb[NL80211_ATTR_FEATURE_FLAGS]);
2892 wiphy_info_probe_resp_offload(capa,
2893 tb[NL80211_ATTR_PROBE_RESP_OFFLOAD]);
562c9d97 2894
8cd6b7bc
JB
2895 if (tb[NL80211_ATTR_EXT_CAPA] && tb[NL80211_ATTR_EXT_CAPA_MASK] &&
2896 drv->extended_capa == NULL) {
2897 drv->extended_capa =
2898 os_malloc(nla_len(tb[NL80211_ATTR_EXT_CAPA]));
2899 if (drv->extended_capa) {
2900 os_memcpy(drv->extended_capa,
2901 nla_data(tb[NL80211_ATTR_EXT_CAPA]),
2902 nla_len(tb[NL80211_ATTR_EXT_CAPA]));
2903 drv->extended_capa_len =
2904 nla_len(tb[NL80211_ATTR_EXT_CAPA]);
2905 }
2906 drv->extended_capa_mask =
2907 os_malloc(nla_len(tb[NL80211_ATTR_EXT_CAPA]));
2908 if (drv->extended_capa_mask) {
2909 os_memcpy(drv->extended_capa_mask,
2910 nla_data(tb[NL80211_ATTR_EXT_CAPA]),
2911 nla_len(tb[NL80211_ATTR_EXT_CAPA]));
2912 } else {
2913 os_free(drv->extended_capa);
2914 drv->extended_capa = NULL;
2915 drv->extended_capa_len = 0;
2916 }
2917 }
2918
80bc75f1
JM
2919 return NL_SKIP;
2920}
2921
2922
2923static int wpa_driver_nl80211_get_info(struct wpa_driver_nl80211_data *drv,
2924 struct wiphy_info_data *info)
2925{
43245552 2926 u32 feat;
80bc75f1
JM
2927 struct nl_msg *msg;
2928
2929 os_memset(info, 0, sizeof(*info));
e8b5e24e 2930 info->capa = &drv->capa;
8cd6b7bc 2931 info->drv = drv;
89e07afb 2932
80bc75f1
JM
2933 msg = nlmsg_alloc();
2934 if (!msg)
2935 return -1;
2936
43245552
DJ
2937 feat = get_nl80211_protocol_features(drv);
2938 if (feat & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP)
2939 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_WIPHY);
2940 else
2941 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_WIPHY);
80bc75f1 2942
43245552 2943 NLA_PUT_FLAG(msg, NL80211_ATTR_SPLIT_WIPHY_DUMP);
a2e40bb6 2944 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->first_bss.ifindex);
80bc75f1 2945
43245552
DJ
2946 if (send_and_recv_msgs(drv, msg, wiphy_info_handler, info))
2947 return -1;
2948
2949 if (info->auth_supported)
2950 drv->capa.flags |= WPA_DRIVER_FLAGS_SME;
2951 else if (!info->connect_supported) {
2952 wpa_printf(MSG_INFO, "nl80211: Driver does not support "
2953 "authentication/association or connect commands");
2954 info->error = 1;
2955 }
2956
2957 if (info->p2p_go_supported && info->p2p_client_supported)
2958 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CAPABLE;
2959 if (info->p2p_concurrent) {
2960 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
2961 "interface (driver advertised support)");
2962 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
2963 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
2964 }
2965 if (info->p2p_multichan_concurrent) {
2966 wpa_printf(MSG_DEBUG, "nl80211: Enable multi-channel "
2967 "concurrent (driver advertised support)");
2968 drv->capa.flags |= WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT;
2969 }
b691dcb1
IP
2970
2971 /* default to 5000 since early versions of mac80211 don't set it */
2972 if (!drv->capa.max_remain_on_chan)
2973 drv->capa.max_remain_on_chan = 5000;
2974
43245552 2975 return 0;
80bc75f1
JM
2976nla_put_failure:
2977 nlmsg_free(msg);
2978 return -1;
2979}
2980
2981
93d11400 2982static int wpa_driver_nl80211_capa(struct wpa_driver_nl80211_data *drv)
80bc75f1
JM
2983{
2984 struct wiphy_info_data info;
2985 if (wpa_driver_nl80211_get_info(drv, &info))
93d11400 2986 return -1;
e8b5e24e
JB
2987
2988 if (info.error)
2989 return -1;
2990
80bc75f1 2991 drv->has_capability = 1;
1b2a72e8
JM
2992 /* For now, assume TKIP, CCMP, WPA, WPA2 are supported */
2993 drv->capa.key_mgmt = WPA_DRIVER_CAPA_KEY_MGMT_WPA |
2994 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
2995 WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
2996 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK;
2997 drv->capa.enc = WPA_DRIVER_CAPA_ENC_WEP40 |
2998 WPA_DRIVER_CAPA_ENC_WEP104 |
2999 WPA_DRIVER_CAPA_ENC_TKIP |
3000 WPA_DRIVER_CAPA_ENC_CCMP;
291b6068
JM
3001 drv->capa.auth = WPA_DRIVER_AUTH_OPEN |
3002 WPA_DRIVER_AUTH_SHARED |
3003 WPA_DRIVER_AUTH_LEAP;
1b2a72e8 3004
871f4dd0 3005 drv->capa.flags |= WPA_DRIVER_FLAGS_SANE_ERROR_CODES;
0194fedb 3006 drv->capa.flags |= WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE;
2fee890a 3007 drv->capa.flags |= WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
e1bd4e19 3008
e5091674 3009 if (!info.device_ap_sme) {
e1bd4e19 3010 drv->capa.flags |= WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS;
0194fedb 3011
e5091674
JM
3012 /*
3013 * No AP SME is currently assumed to also indicate no AP MLME
3014 * in the driver/firmware.
3015 */
3016 drv->capa.flags |= WPA_DRIVER_FLAGS_AP_MLME;
3017 }
3018
61cbe2ff 3019 drv->device_ap_sme = info.device_ap_sme;
39718852 3020 drv->poll_command_supported = info.poll_command_supported;
32ab4855 3021 drv->data_tx_status = info.data_tx_status;
61cbe2ff 3022
a11241fa 3023 /*
73a3c6ff
FF
3024 * If poll command and tx status are supported, mac80211 is new enough
3025 * to have everything we need to not need monitor interfaces.
a11241fa 3026 */
73a3c6ff 3027 drv->use_monitor = !info.poll_command_supported || !info.data_tx_status;
a11241fa 3028
536062f2
JM
3029 if (drv->device_ap_sme && drv->use_monitor) {
3030 /*
3031 * Non-mac80211 drivers may not support monitor interface.
3032 * Make sure we do not get stuck with incorrect capability here
3033 * by explicitly testing this.
3034 */
3035 if (!info.monitor_supported) {
3036 wpa_printf(MSG_DEBUG, "nl80211: Disable use_monitor "
3037 "with device_ap_sme since no monitor mode "
3038 "support detected");
3039 drv->use_monitor = 0;
3040 }
3041 }
3042
a11241fa
JB
3043 /*
3044 * If we aren't going to use monitor interfaces, but the
3045 * driver doesn't support data TX status, we won't get TX
3046 * status for EAPOL frames.
3047 */
3048 if (!drv->use_monitor && !info.data_tx_status)
3049 drv->capa.flags &= ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
3050
93d11400 3051 return 0;
80bc75f1
JM
3052}
3053
3054
b088cf82
JM
3055#ifdef ANDROID
3056static int android_genl_ctrl_resolve(struct nl_handle *handle,
3057 const char *name)
3058{
3059 /*
3060 * Android ICS has very minimal genl_ctrl_resolve() implementation, so
3061 * need to work around that.
3062 */
3063 struct nl_cache *cache = NULL;
3064 struct genl_family *nl80211 = NULL;
3065 int id = -1;
3066
3067 if (genl_ctrl_alloc_cache(handle, &cache) < 0) {
3068 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic "
3069 "netlink cache");
3070 goto fail;
3071 }
3072
3073 nl80211 = genl_ctrl_search_by_name(cache, name);
3074 if (nl80211 == NULL)
3075 goto fail;
3076
3077 id = genl_family_get_id(nl80211);
3078
3079fail:
3080 if (nl80211)
3081 genl_family_put(nl80211);
3082 if (cache)
3083 nl_cache_free(cache);
3084
3085 return id;
3086}
3087#define genl_ctrl_resolve android_genl_ctrl_resolve
3088#endif /* ANDROID */
3089
3090
2a7b66f5
BG
3091static int wpa_driver_nl80211_init_nl_global(struct nl80211_global *global)
3092{
d6c9aab8
JB
3093 int ret;
3094
2a7b66f5
BG
3095 global->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
3096 if (global->nl_cb == NULL) {
3097 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
3098 "callbacks");
3099 return -1;
3100 }
3101
481234cf
JM
3102 global->nl = nl_create_handle(global->nl_cb, "nl");
3103 if (global->nl == NULL)
d6c9aab8 3104 goto err;
276e2d67 3105
481234cf 3106 global->nl80211_id = genl_ctrl_resolve(global->nl, "nl80211");
335d42b1 3107 if (global->nl80211_id < 0) {
276e2d67
BG
3108 wpa_printf(MSG_ERROR, "nl80211: 'nl80211' generic netlink not "
3109 "found");
d6c9aab8 3110 goto err;
276e2d67
BG
3111 }
3112
481234cf
JM
3113 global->nl_event = nl_create_handle(global->nl_cb, "event");
3114 if (global->nl_event == NULL)
d6c9aab8 3115 goto err;
9fff9fdc 3116
d6c9aab8 3117 ret = nl_get_multicast_id(global, "nl80211", "scan");
97865538 3118 if (ret >= 0)
481234cf 3119 ret = nl_socket_add_membership(global->nl_event, ret);
97865538
JM
3120 if (ret < 0) {
3121 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
3122 "membership for scan events: %d (%s)",
3123 ret, strerror(-ret));
d6c9aab8 3124 goto err;
97865538 3125 }
c2a04078 3126
d6c9aab8 3127 ret = nl_get_multicast_id(global, "nl80211", "mlme");
c2a04078 3128 if (ret >= 0)
481234cf 3129 ret = nl_socket_add_membership(global->nl_event, ret);
c2a04078
JM
3130 if (ret < 0) {
3131 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
3132 "membership for mlme events: %d (%s)",
3133 ret, strerror(-ret));
d6c9aab8 3134 goto err;
c2a04078 3135 }
c2a04078 3136
d6c9aab8 3137 ret = nl_get_multicast_id(global, "nl80211", "regulatory");
33c5deb8 3138 if (ret >= 0)
481234cf 3139 ret = nl_socket_add_membership(global->nl_event, ret);
33c5deb8
JM
3140 if (ret < 0) {
3141 wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
3142 "membership for regulatory events: %d (%s)",
3143 ret, strerror(-ret));
3144 /* Continue without regulatory events */
3145 }
3146
d6c9aab8
JB
3147 nl_cb_set(global->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
3148 no_seq_check, NULL);
3149 nl_cb_set(global->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
3150 process_global_event, global);
3151
481234cf 3152 eloop_register_read_sock(nl_socket_get_fd(global->nl_event),
d6c9aab8 3153 wpa_driver_nl80211_event_receive,
481234cf 3154 global->nl_cb, global->nl_event);
d6c9aab8
JB
3155
3156 return 0;
3157
3158err:
3159 nl_destroy_handles(&global->nl_event);
3160 nl_destroy_handles(&global->nl);
3161 nl_cb_put(global->nl_cb);
671a5039 3162 global->nl_cb = NULL;
d6c9aab8
JB
3163 return -1;
3164}
3165
3166
3167static int wpa_driver_nl80211_init_nl(struct wpa_driver_nl80211_data *drv)
3168{
1afc986d
JB
3169 drv->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
3170 if (!drv->nl_cb) {
3171 wpa_printf(MSG_ERROR, "nl80211: Failed to alloc cb struct");
d6c9aab8 3172 return -1;
1afc986d
JB
3173 }
3174
3175 nl_cb_set(drv->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
3176 no_seq_check, NULL);
f06aedd9
JB
3177 nl_cb_set(drv->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
3178 process_drv_event, drv);
1afc986d 3179
9fff9fdc 3180 return 0;
9fff9fdc
JM
3181}
3182
3183
8401a6b0
JM
3184static void wpa_driver_nl80211_rfkill_blocked(void *ctx)
3185{
8401a6b0 3186 wpa_printf(MSG_DEBUG, "nl80211: RFKILL blocked");
a63063b4
JM
3187 /*
3188 * This may be for any interface; use ifdown event to disable
3189 * interface.
3190 */
8401a6b0
JM
3191}
3192
3193
3194static void wpa_driver_nl80211_rfkill_unblocked(void *ctx)
3195{
3196 struct wpa_driver_nl80211_data *drv = ctx;
3197 wpa_printf(MSG_DEBUG, "nl80211: RFKILL unblocked");
c81eff1a
BG
3198 if (linux_set_iface_flags(drv->global->ioctl_sock,
3199 drv->first_bss.ifname, 1)) {
8401a6b0
JM
3200 wpa_printf(MSG_DEBUG, "nl80211: Could not set interface UP "
3201 "after rfkill unblock");
3202 return;
3203 }
a63063b4 3204 /* rtnetlink ifup handler will report interface as enabled */
8401a6b0
JM
3205}
3206
3207
6859f1cb
BG
3208static void nl80211_get_phy_name(struct wpa_driver_nl80211_data *drv)
3209{
3210 /* Find phy (radio) to which this interface belongs */
3211 char buf[90], *pos;
3212 int f, rv;
3213
3214 drv->phyname[0] = '\0';
3215 snprintf(buf, sizeof(buf) - 1, "/sys/class/net/%s/phy80211/name",
3216 drv->first_bss.ifname);
3217 f = open(buf, O_RDONLY);
3218 if (f < 0) {
3219 wpa_printf(MSG_DEBUG, "Could not open file %s: %s",
3220 buf, strerror(errno));
3221 return;
3222 }
3223
3224 rv = read(f, drv->phyname, sizeof(drv->phyname) - 1);
3225 close(f);
3226 if (rv < 0) {
3227 wpa_printf(MSG_DEBUG, "Could not read file %s: %s",
3228 buf, strerror(errno));
3229 return;
3230 }
3231
3232 drv->phyname[rv] = '\0';
3233 pos = os_strchr(drv->phyname, '\n');
3234 if (pos)
3235 *pos = '\0';
3236 wpa_printf(MSG_DEBUG, "nl80211: interface %s in phy %s",
3237 drv->first_bss.ifname, drv->phyname);
3238}
3239
3240
32ab4855
JB
3241static void wpa_driver_nl80211_handle_eapol_tx_status(int sock,
3242 void *eloop_ctx,
3243 void *handle)
3244{
3245 struct wpa_driver_nl80211_data *drv = eloop_ctx;
3246 u8 data[2048];
3247 struct msghdr msg;
3248 struct iovec entry;
cad0f50e 3249 u8 control[512];
32ab4855
JB
3250 struct cmsghdr *cmsg;
3251 int res, found_ee = 0, found_wifi = 0, acked = 0;
3252 union wpa_event_data event;
3253
3254 memset(&msg, 0, sizeof(msg));
3255 msg.msg_iov = &entry;
3256 msg.msg_iovlen = 1;
3257 entry.iov_base = data;
3258 entry.iov_len = sizeof(data);
3259 msg.msg_control = &control;
3260 msg.msg_controllen = sizeof(control);
3261
3262 res = recvmsg(sock, &msg, MSG_ERRQUEUE);
3263 /* if error or not fitting 802.3 header, return */
3264 if (res < 14)
3265 return;
3266
3267 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg))
3268 {
3269 if (cmsg->cmsg_level == SOL_SOCKET &&
3270 cmsg->cmsg_type == SCM_WIFI_STATUS) {
3271 int *ack;
3272
3273 found_wifi = 1;
3274 ack = (void *)CMSG_DATA(cmsg);
3275 acked = *ack;
3276 }
3277
3278 if (cmsg->cmsg_level == SOL_PACKET &&
3279 cmsg->cmsg_type == PACKET_TX_TIMESTAMP) {
3280 struct sock_extended_err *err =
3281 (struct sock_extended_err *)CMSG_DATA(cmsg);
3282
3283 if (err->ee_origin == SO_EE_ORIGIN_TXSTATUS)
3284 found_ee = 1;
3285 }
3286 }
3287
3288 if (!found_ee || !found_wifi)
3289 return;
3290
3291 memset(&event, 0, sizeof(event));
3292 event.eapol_tx_status.dst = data;
3293 event.eapol_tx_status.data = data + 14;
3294 event.eapol_tx_status.data_len = res - 14;
3295 event.eapol_tx_status.ack = acked;
3296 wpa_supplicant_event(drv->ctx, EVENT_EAPOL_TX_STATUS, &event);
3297}
3298
3299
cc7a48d1
JB
3300static int nl80211_init_bss(struct i802_bss *bss)
3301{
3302 bss->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
3303 if (!bss->nl_cb)
3304 return -1;
3305
3306 nl_cb_set(bss->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
3307 no_seq_check, NULL);
3308 nl_cb_set(bss->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
3309 process_bss_event, bss);
3310
3311 return 0;
3312}
3313
3314
3315static void nl80211_destroy_bss(struct i802_bss *bss)
3316{
3317 nl_cb_put(bss->nl_cb);
3318 bss->nl_cb = NULL;
3319}
3320
3321
9fff9fdc
JM
3322/**
3323 * wpa_driver_nl80211_init - Initialize nl80211 driver interface
3324 * @ctx: context to be used when calling wpa_supplicant functions,
3325 * e.g., wpa_supplicant_event()
3326 * @ifname: interface name, e.g., wlan0
f2ed8023 3327 * @global_priv: private driver global data from global_init()
9fff9fdc
JM
3328 * Returns: Pointer to private data, %NULL on failure
3329 */
f2ed8023
JM
3330static void * wpa_driver_nl80211_init(void *ctx, const char *ifname,
3331 void *global_priv)
9fff9fdc 3332{
9fff9fdc 3333 struct wpa_driver_nl80211_data *drv;
8401a6b0 3334 struct rfkill_config *rcfg;
a2e40bb6 3335 struct i802_bss *bss;
9fff9fdc 3336
a5c696ad
JM
3337 if (global_priv == NULL)
3338 return NULL;
9fff9fdc
JM
3339 drv = os_zalloc(sizeof(*drv));
3340 if (drv == NULL)
3341 return NULL;
f2ed8023 3342 drv->global = global_priv;
9fff9fdc 3343 drv->ctx = ctx;
a2e40bb6
FF
3344 bss = &drv->first_bss;
3345 bss->drv = drv;
a5e1eb20
SE
3346 bss->ctx = ctx;
3347
a2e40bb6 3348 os_strlcpy(bss->ifname, ifname, sizeof(bss->ifname));
9fff9fdc
JM
3349 drv->monitor_ifidx = -1;
3350 drv->monitor_sock = -1;
d12dab4c 3351 drv->eapol_tx_sock = -1;
b1f625e0 3352 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
9fff9fdc 3353
ba2d0d7d 3354 if (wpa_driver_nl80211_init_nl(drv)) {
bbaf0837
JM
3355 os_free(drv);
3356 return NULL;
3357 }
9fff9fdc 3358
cc7a48d1
JB
3359 if (nl80211_init_bss(bss))
3360 goto failed;
3361
6859f1cb
BG
3362 nl80211_get_phy_name(drv);
3363
8401a6b0
JM
3364 rcfg = os_zalloc(sizeof(*rcfg));
3365 if (rcfg == NULL)
3366 goto failed;
3367 rcfg->ctx = drv;
3368 os_strlcpy(rcfg->ifname, ifname, sizeof(rcfg->ifname));
3369 rcfg->blocked_cb = wpa_driver_nl80211_rfkill_blocked;
3370 rcfg->unblocked_cb = wpa_driver_nl80211_rfkill_unblocked;
3371 drv->rfkill = rfkill_init(rcfg);
52169389 3372 if (drv->rfkill == NULL) {
8401a6b0 3373 wpa_printf(MSG_DEBUG, "nl80211: RFKILL status not available");
52169389
JM
3374 os_free(rcfg);
3375 }
8401a6b0 3376
08063178 3377 if (wpa_driver_nl80211_finish_drv_init(drv))
bbaf0837 3378 goto failed;
7524cfb1 3379
d12dab4c 3380 drv->eapol_tx_sock = socket(PF_PACKET, SOCK_DGRAM, 0);
32ab4855
JB
3381 if (drv->eapol_tx_sock < 0)
3382 goto failed;
3383
3384 if (drv->data_tx_status) {
3385 int enabled = 1;
3386
3387 if (setsockopt(drv->eapol_tx_sock, SOL_SOCKET, SO_WIFI_STATUS,
3388 &enabled, sizeof(enabled)) < 0) {
3389 wpa_printf(MSG_DEBUG,
3390 "nl80211: wifi status sockopt failed\n");
3391 drv->data_tx_status = 0;
a11241fa
JB
3392 if (!drv->use_monitor)
3393 drv->capa.flags &=
3394 ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
32ab4855
JB
3395 } else {
3396 eloop_register_read_sock(drv->eapol_tx_sock,
3397 wpa_driver_nl80211_handle_eapol_tx_status,
3398 drv, NULL);
3399 }
3400 }
f10bfc9a 3401
dac12351 3402 if (drv->global) {
c4bb8817 3403 dl_list_add(&drv->global->interfaces, &drv->list);
dac12351
BG
3404 drv->in_interface_list = 1;
3405 }
c4bb8817 3406
a2e40bb6 3407 return bss;
7524cfb1 3408
bbaf0837 3409failed:
dac12351 3410 wpa_driver_nl80211_deinit(bss);
7524cfb1
JM
3411 return NULL;
3412}
3413
3414
a11241fa 3415static int nl80211_register_frame(struct i802_bss *bss,
5582a5d1 3416 struct nl_handle *nl_handle,
bd94971e 3417 u16 type, const u8 *match, size_t match_len)
58f6fbe0 3418{
a11241fa 3419 struct wpa_driver_nl80211_data *drv = bss->drv;
58f6fbe0
JM
3420 struct nl_msg *msg;
3421 int ret = -1;
3422
3423 msg = nlmsg_alloc();
3424 if (!msg)
3425 return -1;
3426
36488c05
JM
3427 wpa_printf(MSG_DEBUG, "nl80211: Register frame type=0x%x nl_handle=%p",
3428 type, nl_handle);
3429 wpa_hexdump(MSG_DEBUG, "nl80211: Register frame match",
3430 match, match_len);
3431
9fb04070 3432 nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_ACTION);
58f6fbe0 3433
a11241fa 3434 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
bd94971e 3435 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE, type);
58f6fbe0
JM
3436 NLA_PUT(msg, NL80211_ATTR_FRAME_MATCH, match_len, match);
3437
d6c9aab8 3438 ret = send_and_recv(drv->global, nl_handle, msg, NULL, NULL);
58f6fbe0
JM
3439 msg = NULL;
3440 if (ret) {
5582a5d1
JB
3441 wpa_printf(MSG_DEBUG, "nl80211: Register frame command "
3442 "failed (type=%u): ret=%d (%s)",
3443 type, ret, strerror(-ret));
3444 wpa_hexdump(MSG_DEBUG, "nl80211: Register frame match",
58f6fbe0
JM
3445 match, match_len);
3446 goto nla_put_failure;
3447 }
3448 ret = 0;
3449nla_put_failure:
3450 nlmsg_free(msg);
3451 return ret;
3452}
3453
3454
a11241fa
JB
3455static int nl80211_alloc_mgmt_handle(struct i802_bss *bss)
3456{
3457 struct wpa_driver_nl80211_data *drv = bss->drv;
3458
481234cf 3459 if (bss->nl_mgmt) {
a11241fa 3460 wpa_printf(MSG_DEBUG, "nl80211: Mgmt reporting "
36488c05 3461 "already on! (nl_mgmt=%p)", bss->nl_mgmt);
a11241fa
JB
3462 return -1;
3463 }
3464
481234cf
JM
3465 bss->nl_mgmt = nl_create_handle(drv->nl_cb, "mgmt");
3466 if (bss->nl_mgmt == NULL)
a11241fa
JB
3467 return -1;
3468
481234cf 3469 eloop_register_read_sock(nl_socket_get_fd(bss->nl_mgmt),
a11241fa 3470 wpa_driver_nl80211_event_receive, bss->nl_cb,
481234cf 3471 bss->nl_mgmt);
a11241fa
JB
3472
3473 return 0;
3474}
3475
3476
3477static int nl80211_register_action_frame(struct i802_bss *bss,
bd94971e
JB
3478 const u8 *match, size_t match_len)
3479{
3480 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_ACTION << 4);
481234cf 3481 return nl80211_register_frame(bss, bss->nl_mgmt,
5582a5d1 3482 type, match, match_len);
bd94971e
JB
3483}
3484
3485
a11241fa 3486static int nl80211_mgmt_subscribe_non_ap(struct i802_bss *bss)
58f6fbe0 3487{
a11241fa
JB
3488 struct wpa_driver_nl80211_data *drv = bss->drv;
3489
3490 if (nl80211_alloc_mgmt_handle(bss))
3491 return -1;
36488c05
JM
3492 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with non-AP "
3493 "handle %p", bss->nl_mgmt);
a11241fa 3494
4fe9fa0d 3495#if defined(CONFIG_P2P) || defined(CONFIG_INTERWORKING)
046b26a2 3496 /* GAS Initial Request */
a11241fa 3497 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0a", 2) < 0)
046b26a2
JM
3498 return -1;
3499 /* GAS Initial Response */
a11241fa 3500 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0b", 2) < 0)
046b26a2 3501 return -1;
18708aad 3502 /* GAS Comeback Request */
a11241fa 3503 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0c", 2) < 0)
18708aad
JM
3504 return -1;
3505 /* GAS Comeback Response */
a11241fa 3506 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0d", 2) < 0)
18708aad 3507 return -1;
4fe9fa0d
JM
3508#endif /* CONFIG_P2P || CONFIG_INTERWORKING */
3509#ifdef CONFIG_P2P
046b26a2 3510 /* P2P Public Action */
a11241fa 3511 if (nl80211_register_action_frame(bss,
046b26a2
JM
3512 (u8 *) "\x04\x09\x50\x6f\x9a\x09",
3513 6) < 0)
3514 return -1;
3515 /* P2P Action */
a11241fa 3516 if (nl80211_register_action_frame(bss,
046b26a2
JM
3517 (u8 *) "\x7f\x50\x6f\x9a\x09",
3518 5) < 0)
3519 return -1;
3520#endif /* CONFIG_P2P */
7d878ca7
JM
3521#ifdef CONFIG_IEEE80211W
3522 /* SA Query Response */
a11241fa 3523 if (nl80211_register_action_frame(bss, (u8 *) "\x08\x01", 2) < 0)
7d878ca7
JM
3524 return -1;
3525#endif /* CONFIG_IEEE80211W */
35287637
AN
3526#ifdef CONFIG_TDLS
3527 if ((drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT)) {
3528 /* TDLS Discovery Response */
aa543c0c 3529 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0e", 2) <
35287637
AN
3530 0)
3531 return -1;
3532 }
3533#endif /* CONFIG_TDLS */
046b26a2 3534
7b90c16a 3535 /* FT Action frames */
a11241fa 3536 if (nl80211_register_action_frame(bss, (u8 *) "\x06", 1) < 0)
7b90c16a
JM
3537 return -1;
3538 else
3539 drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_FT |
3540 WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK;
3541
71269b37 3542 /* WNM - BSS Transition Management Request */
a11241fa
JB
3543 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x07", 2) < 0)
3544 return -1;
bd896433
JM
3545 /* WNM-Sleep Mode Response */
3546 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x11", 2) < 0)
3547 return -1;
a11241fa
JB
3548
3549 return 0;
3550}
3551
3552
02bb32c3
JB
3553static int nl80211_register_spurious_class3(struct i802_bss *bss)
3554{
3555 struct wpa_driver_nl80211_data *drv = bss->drv;
3556 struct nl_msg *msg;
3557 int ret = -1;
3558
3559 msg = nlmsg_alloc();
3560 if (!msg)
3561 return -1;
3562
3563 nl80211_cmd(drv, msg, 0, NL80211_CMD_UNEXPECTED_FRAME);
3564
3565 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
3566
481234cf 3567 ret = send_and_recv(drv->global, bss->nl_mgmt, msg, NULL, NULL);
02bb32c3
JB
3568 msg = NULL;
3569 if (ret) {
3570 wpa_printf(MSG_DEBUG, "nl80211: Register spurious class3 "
3571 "failed: ret=%d (%s)",
3572 ret, strerror(-ret));
3573 goto nla_put_failure;
3574 }
3575 ret = 0;
3576nla_put_failure:
3577 nlmsg_free(msg);
3578 return ret;
3579}
3580
3581
a11241fa
JB
3582static int nl80211_mgmt_subscribe_ap(struct i802_bss *bss)
3583{
3584 static const int stypes[] = {
3585 WLAN_FC_STYPE_AUTH,
3586 WLAN_FC_STYPE_ASSOC_REQ,
3587 WLAN_FC_STYPE_REASSOC_REQ,
3588 WLAN_FC_STYPE_DISASSOC,
3589 WLAN_FC_STYPE_DEAUTH,
3590 WLAN_FC_STYPE_ACTION,
3591 WLAN_FC_STYPE_PROBE_REQ,
3592/* Beacon doesn't work as mac80211 doesn't currently allow
3593 * it, but it wouldn't really be the right thing anyway as
3594 * it isn't per interface ... maybe just dump the scan
3595 * results periodically for OLBC?
3596 */
3597// WLAN_FC_STYPE_BEACON,
3598 };
3599 unsigned int i;
3600
3601 if (nl80211_alloc_mgmt_handle(bss))
71269b37 3602 return -1;
36488c05
JM
3603 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
3604 "handle %p", bss->nl_mgmt);
71269b37 3605
a11241fa 3606 for (i = 0; i < sizeof(stypes) / sizeof(stypes[0]); i++) {
481234cf 3607 if (nl80211_register_frame(bss, bss->nl_mgmt,
a11241fa
JB
3608 (WLAN_FC_TYPE_MGMT << 2) |
3609 (stypes[i] << 4),
3610 NULL, 0) < 0) {
3611 goto out_err;
3612 }
3613 }
3614
02bb32c3
JB
3615 if (nl80211_register_spurious_class3(bss))
3616 goto out_err;
3617
e32ad281
JB
3618 if (nl80211_get_wiphy_data_ap(bss) == NULL)
3619 goto out_err;
3620
58f6fbe0 3621 return 0;
a11241fa
JB
3622
3623out_err:
481234cf 3624 eloop_unregister_read_sock(nl_socket_get_fd(bss->nl_mgmt));
a11241fa
JB
3625 nl_destroy_handles(&bss->nl_mgmt);
3626 return -1;
3627}
3628
3629
a6cc0602
JM
3630static int nl80211_mgmt_subscribe_ap_dev_sme(struct i802_bss *bss)
3631{
3632 if (nl80211_alloc_mgmt_handle(bss))
3633 return -1;
3634 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
3635 "handle %p (device SME)", bss->nl_mgmt);
3636
3637 if (nl80211_register_frame(bss, bss->nl_mgmt,
3638 (WLAN_FC_TYPE_MGMT << 2) |
3639 (WLAN_FC_STYPE_ACTION << 4),
3640 NULL, 0) < 0)
3641 goto out_err;
3642
3643 return 0;
3644
3645out_err:
3646 eloop_unregister_read_sock(nl_socket_get_fd(bss->nl_mgmt));
3647 nl_destroy_handles(&bss->nl_mgmt);
3648 return -1;
3649}
3650
3651
36488c05 3652static void nl80211_mgmt_unsubscribe(struct i802_bss *bss, const char *reason)
a11241fa 3653{
481234cf 3654 if (bss->nl_mgmt == NULL)
a11241fa 3655 return;
36488c05
JM
3656 wpa_printf(MSG_DEBUG, "nl80211: Unsubscribe mgmt frames handle %p "
3657 "(%s)", bss->nl_mgmt, reason);
481234cf 3658 eloop_unregister_read_sock(nl_socket_get_fd(bss->nl_mgmt));
a11241fa 3659 nl_destroy_handles(&bss->nl_mgmt);
e32ad281
JB
3660
3661 nl80211_put_wiphy_data_ap(bss);
58f6fbe0
JM
3662}
3663
3664
8401a6b0
JM
3665static void wpa_driver_nl80211_send_rfkill(void *eloop_ctx, void *timeout_ctx)
3666{
3667 wpa_supplicant_event(timeout_ctx, EVENT_INTERFACE_DISABLED, NULL);
3668}
3669
3670
362f781e 3671static int
7524cfb1
JM
3672wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv)
3673{
a2e40bb6 3674 struct i802_bss *bss = &drv->first_bss;
8401a6b0 3675 int send_rfkill_event = 0;
a2e40bb6
FF
3676
3677 drv->ifindex = if_nametoindex(bss->ifname);
3678 drv->first_bss.ifindex = drv->ifindex;
a87c9d96 3679
bbaf0837 3680#ifndef HOSTAPD
ff6a158b
JM
3681 /*
3682 * Make sure the interface starts up in station mode unless this is a
3683 * dynamically added interface (e.g., P2P) that was already configured
3684 * with proper iftype.
3685 */
a5c696ad 3686 if (drv->ifindex != drv->global->if_add_ifindex &&
b1f625e0 3687 wpa_driver_nl80211_set_mode(bss, NL80211_IFTYPE_STATION) < 0) {
6e8183d7 3688 wpa_printf(MSG_ERROR, "nl80211: Could not configure driver to "
a87c9d96 3689 "use managed mode");
6e8183d7 3690 return -1;
a87c9d96
JM
3691 }
3692
c81eff1a 3693 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1)) {
8401a6b0
JM
3694 if (rfkill_is_blocked(drv->rfkill)) {
3695 wpa_printf(MSG_DEBUG, "nl80211: Could not yet enable "
3696 "interface '%s' due to rfkill",
3697 bss->ifname);
a63063b4 3698 drv->if_disabled = 1;
8401a6b0
JM
3699 send_rfkill_event = 1;
3700 } else {
3701 wpa_printf(MSG_ERROR, "nl80211: Could not set "
3702 "interface '%s' UP", bss->ifname);
3703 return -1;
3704 }
362f781e 3705 }
3f5285e8 3706
36d84860 3707 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
e2d02c29 3708 1, IF_OPER_DORMANT);
bbaf0837 3709#endif /* HOSTAPD */
362f781e 3710
2fee890a
JM
3711 if (wpa_driver_nl80211_capa(drv))
3712 return -1;
3713
c81eff1a 3714 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
341eebee 3715 bss->addr))
2136f480 3716 return -1;
f2ed8023 3717
8401a6b0
JM
3718 if (send_rfkill_event) {
3719 eloop_register_timeout(0, 0, wpa_driver_nl80211_send_rfkill,
3720 drv, drv->ctx);
3721 }
3722
362f781e 3723 return 0;
3f5285e8
JM
3724}
3725
3726
8a27af5c
JM
3727static int wpa_driver_nl80211_del_beacon(struct wpa_driver_nl80211_data *drv)
3728{
3729 struct nl_msg *msg;
3730
3731 msg = nlmsg_alloc();
3732 if (!msg)
3733 return -ENOMEM;
3734
9fb04070 3735 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_BEACON);
8a27af5c
JM
3736 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
3737
3738 return send_and_recv_msgs(drv, msg, NULL, NULL);
3739 nla_put_failure:
5883168a 3740 nlmsg_free(msg);
8a27af5c
JM
3741 return -ENOBUFS;
3742}
8a27af5c
JM
3743
3744
3f5285e8 3745/**
7e5ba1b9 3746 * wpa_driver_nl80211_deinit - Deinitialize nl80211 driver interface
9ebce9c5 3747 * @bss: Pointer to private nl80211 data from wpa_driver_nl80211_init()
3f5285e8
JM
3748 *
3749 * Shut down driver interface and processing of driver events. Free
3750 * private data buffer if one was allocated in wpa_driver_nl80211_init().
3751 */
9ebce9c5 3752static void wpa_driver_nl80211_deinit(struct i802_bss *bss)
3f5285e8 3753{
a2e40bb6 3754 struct wpa_driver_nl80211_data *drv = bss->drv;
3f5285e8 3755
873d0fcf 3756 bss->in_deinit = 1;
32ab4855
JB
3757 if (drv->data_tx_status)
3758 eloop_unregister_read_sock(drv->eapol_tx_sock);
d12dab4c
JB
3759 if (drv->eapol_tx_sock >= 0)
3760 close(drv->eapol_tx_sock);
f10bfc9a 3761
481234cf 3762 if (bss->nl_preq)
5582a5d1 3763 wpa_driver_nl80211_probe_req_report(bss, 0);
e17a2477 3764 if (bss->added_if_into_bridge) {
c81eff1a
BG
3765 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
3766 bss->ifname) < 0)
94627f6c
JM
3767 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
3768 "interface %s from bridge %s: %s",
e17a2477 3769 bss->ifname, bss->brname, strerror(errno));
94627f6c 3770 }
e17a2477 3771 if (bss->added_bridge) {
c81eff1a 3772 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
94627f6c
JM
3773 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
3774 "bridge %s: %s",
e17a2477 3775 bss->brname, strerror(errno));
94627f6c
JM
3776 }
3777
460456f8 3778 nl80211_remove_monitor_interface(drv);
8a27af5c 3779
b1f625e0 3780 if (is_ap_interface(drv->nlmode))
8a27af5c 3781 wpa_driver_nl80211_del_beacon(drv);
0915d02c 3782
bbaf0837
JM
3783#ifdef HOSTAPD
3784 if (drv->last_freq_ht) {
3785 /* Clear HT flags from the driver */
3786 struct hostapd_freq_params freq;
3787 os_memset(&freq, 0, sizeof(freq));
3788 freq.freq = drv->last_freq;
9ebce9c5 3789 wpa_driver_nl80211_set_freq(bss, &freq);
bbaf0837
JM
3790 }
3791
bbaf0837
JM
3792 if (drv->eapol_sock >= 0) {
3793 eloop_unregister_read_sock(drv->eapol_sock);
3794 close(drv->eapol_sock);
3795 }
3796
3797 if (drv->if_indices != drv->default_if_indices)
3798 os_free(drv->if_indices);
1b648c7e 3799#endif /* HOSTAPD */
3f5285e8 3800
b3af99d2 3801 if (drv->disabled_11b_rates)
4e5cb1a3
JM
3802 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
3803
36d84860
BG
3804 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, 0,
3805 IF_OPER_UP);
8401a6b0 3806 rfkill_deinit(drv->rfkill);
3f5285e8 3807
bbaf0837
JM
3808 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
3809
c81eff1a 3810 (void) linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0);
b1f625e0 3811 wpa_driver_nl80211_set_mode(bss, NL80211_IFTYPE_STATION);
36488c05 3812 nl80211_mgmt_unsubscribe(bss, "deinit");
3f5285e8 3813
1afc986d 3814 nl_cb_put(drv->nl_cb);
3f5285e8 3815
cc7a48d1
JB
3816 nl80211_destroy_bss(&drv->first_bss);
3817
3812464c
JM
3818 os_free(drv->filter_ssids);
3819
536fd62d
JM
3820 os_free(drv->auth_ie);
3821
dac12351 3822 if (drv->in_interface_list)
f2ed8023
JM
3823 dl_list_del(&drv->list);
3824
8cd6b7bc
JB
3825 os_free(drv->extended_capa);
3826 os_free(drv->extended_capa_mask);
3f5285e8
JM
3827 os_free(drv);
3828}
3829
3830
3831/**
3832 * wpa_driver_nl80211_scan_timeout - Scan timeout to report scan completion
ad1e68e6 3833 * @eloop_ctx: Driver private data
3f5285e8
JM
3834 * @timeout_ctx: ctx argument given to wpa_driver_nl80211_init()
3835 *
3836 * This function can be used as registered timeout when starting a scan to
3837 * generate a scan completed event if the driver does not report this.
3838 */
3839static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx, void *timeout_ctx)
3840{
ad1e68e6 3841 struct wpa_driver_nl80211_data *drv = eloop_ctx;
b1f625e0 3842 if (drv->ap_scan_as_station != NL80211_IFTYPE_UNSPECIFIED) {
a2e40bb6 3843 wpa_driver_nl80211_set_mode(&drv->first_bss,
b1f625e0
EP
3844 drv->ap_scan_as_station);
3845 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
ad1e68e6 3846 }
3f5285e8
JM
3847 wpa_printf(MSG_DEBUG, "Scan timeout - try to get results");
3848 wpa_supplicant_event(timeout_ctx, EVENT_SCAN_RESULTS, NULL);
3849}
3850
3851
95ac3bf4
JM
3852static struct nl_msg *
3853nl80211_scan_common(struct wpa_driver_nl80211_data *drv, u8 cmd,
3854 struct wpa_driver_scan_params *params)
3f5285e8 3855{
95ac3bf4 3856 struct nl_msg *msg;
6a1063e0 3857 size_t i;
3f5285e8 3858
0e75527f 3859 msg = nlmsg_alloc();
f0494d0f 3860 if (!msg)
95ac3bf4 3861 return NULL;
3812464c 3862
95ac3bf4 3863 nl80211_cmd(drv, msg, 0, cmd);
0e75527f 3864
95ac3bf4
JM
3865 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, drv->ifindex) < 0)
3866 goto fail;
3f5285e8 3867
f0494d0f 3868 if (params->num_ssids) {
8970bae8
JB
3869 struct nlattr *ssids;
3870
3871 ssids = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
f0494d0f 3872 if (ssids == NULL)
95ac3bf4 3873 goto fail;
f0494d0f
JM
3874 for (i = 0; i < params->num_ssids; i++) {
3875 wpa_hexdump_ascii(MSG_MSGDUMP, "nl80211: Scan SSID",
3876 params->ssids[i].ssid,
3877 params->ssids[i].ssid_len);
8970bae8
JB
3878 if (nla_put(msg, i + 1, params->ssids[i].ssid_len,
3879 params->ssids[i].ssid) < 0)
95ac3bf4 3880 goto fail;
f0494d0f 3881 }
8970bae8 3882 nla_nest_end(msg, ssids);
3f5285e8
JM
3883 }
3884
d173df52 3885 if (params->extra_ies) {
3b1c7bfd
JM
3886 wpa_hexdump(MSG_MSGDUMP, "nl80211: Scan extra IEs",
3887 params->extra_ies, params->extra_ies_len);
95ac3bf4
JM
3888 if (nla_put(msg, NL80211_ATTR_IE, params->extra_ies_len,
3889 params->extra_ies) < 0)
3890 goto fail;
d173df52
JM
3891 }
3892
d3a98225 3893 if (params->freqs) {
8970bae8
JB
3894 struct nlattr *freqs;
3895 freqs = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
f0494d0f 3896 if (freqs == NULL)
95ac3bf4 3897 goto fail;
9fad706c
JM
3898 for (i = 0; params->freqs[i]; i++) {
3899 wpa_printf(MSG_MSGDUMP, "nl80211: Scan frequency %u "
3900 "MHz", params->freqs[i]);
8970bae8 3901 if (nla_put_u32(msg, i + 1, params->freqs[i]) < 0)
95ac3bf4 3902 goto fail;
9fad706c 3903 }
8970bae8 3904 nla_nest_end(msg, freqs);
d3a98225
JM
3905 }
3906
95ac3bf4
JM
3907 os_free(drv->filter_ssids);
3908 drv->filter_ssids = params->filter_ssids;
3909 params->filter_ssids = NULL;
3910 drv->num_filter_ssids = params->num_filter_ssids;
3911
3912 return msg;
3913
3914fail:
3915 nlmsg_free(msg);
3916 return NULL;
3917}
3918
3919
3920/**
3921 * wpa_driver_nl80211_scan - Request the driver to initiate scan
9ebce9c5 3922 * @bss: Pointer to private driver data from wpa_driver_nl80211_init()
95ac3bf4
JM
3923 * @params: Scan parameters
3924 * Returns: 0 on success, -1 on failure
3925 */
9ebce9c5 3926static int wpa_driver_nl80211_scan(struct i802_bss *bss,
95ac3bf4
JM
3927 struct wpa_driver_scan_params *params)
3928{
95ac3bf4
JM
3929 struct wpa_driver_nl80211_data *drv = bss->drv;
3930 int ret = -1, timeout;
8970bae8 3931 struct nl_msg *msg = NULL;
95ac3bf4 3932
565110cd 3933 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: scan request");
95ac3bf4
JM
3934 drv->scan_for_auth = 0;
3935
3936 msg = nl80211_scan_common(drv, NL80211_CMD_TRIGGER_SCAN, params);
3937 if (!msg)
3938 return -1;
3939
47185fc7 3940 if (params->p2p_probe) {
8970bae8
JB
3941 struct nlattr *rates;
3942
8a6a1e1b
JM
3943 wpa_printf(MSG_DEBUG, "nl80211: P2P probe - mask SuppRates");
3944
8970bae8 3945 rates = nla_nest_start(msg, NL80211_ATTR_SCAN_SUPP_RATES);
f0494d0f
JM
3946 if (rates == NULL)
3947 goto nla_put_failure;
3948
47185fc7
RM
3949 /*
3950 * Remove 2.4 GHz rates 1, 2, 5.5, 11 Mbps from supported rates
3951 * by masking out everything else apart from the OFDM rates 6,
3952 * 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS rates. All 5 GHz
3953 * rates are left enabled.
3954 */
8970bae8 3955 NLA_PUT(msg, NL80211_BAND_2GHZ, 8,
47185fc7 3956 "\x0c\x12\x18\x24\x30\x48\x60\x6c");
8970bae8 3957 nla_nest_end(msg, rates);
970fa12e
RM
3958
3959 NLA_PUT_FLAG(msg, NL80211_ATTR_TX_NO_CCK_RATE);
47185fc7
RM
3960 }
3961
0e75527f
JM
3962 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3963 msg = NULL;
3964 if (ret) {
3965 wpa_printf(MSG_DEBUG, "nl80211: Scan trigger failed: ret=%d "
3966 "(%s)", ret, strerror(-ret));
ad1e68e6 3967#ifdef HOSTAPD
b1f625e0 3968 if (is_ap_interface(drv->nlmode)) {
ad1e68e6
JM
3969 /*
3970 * mac80211 does not allow scan requests in AP mode, so
3971 * try to do this in station mode.
3972 */
b1f625e0
EP
3973 if (wpa_driver_nl80211_set_mode(
3974 bss, NL80211_IFTYPE_STATION))
ad1e68e6
JM
3975 goto nla_put_failure;
3976
085b29f1 3977 if (wpa_driver_nl80211_scan(bss, params)) {
b1f625e0 3978 wpa_driver_nl80211_set_mode(bss, drv->nlmode);
ad1e68e6
JM
3979 goto nla_put_failure;
3980 }
3981
3982 /* Restore AP mode when processing scan results */
b1f625e0 3983 drv->ap_scan_as_station = drv->nlmode;
ad1e68e6
JM
3984 ret = 0;
3985 } else
3986 goto nla_put_failure;
3987#else /* HOSTAPD */
0e75527f 3988 goto nla_put_failure;
ad1e68e6 3989#endif /* HOSTAPD */
3f5285e8
JM
3990 }
3991
3992 /* Not all drivers generate "scan completed" wireless event, so try to
3993 * read results after a timeout. */
0e75527f 3994 timeout = 10;
3f5285e8
JM
3995 if (drv->scan_complete_events) {
3996 /*
d173df52
JM
3997 * The driver seems to deliver events to notify when scan is
3998 * complete, so use longer timeout to avoid race conditions
3999 * with scanning and following association request.
3f5285e8
JM
4000 */
4001 timeout = 30;
4002 }
4003 wpa_printf(MSG_DEBUG, "Scan requested (ret=%d) - scan timeout %d "
4004 "seconds", ret, timeout);
4005 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
0e75527f
JM
4006 eloop_register_timeout(timeout, 0, wpa_driver_nl80211_scan_timeout,
4007 drv, drv->ctx);
3f5285e8 4008
0e75527f 4009nla_put_failure:
0e75527f 4010 nlmsg_free(msg);
3f5285e8
JM
4011 return ret;
4012}
4013
4014
d21c63b9
LC
4015/**
4016 * wpa_driver_nl80211_sched_scan - Initiate a scheduled scan
4017 * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
4018 * @params: Scan parameters
4019 * @interval: Interval between scan cycles in milliseconds
4020 * Returns: 0 on success, -1 on failure or if not supported
4021 */
4022static int wpa_driver_nl80211_sched_scan(void *priv,
4023 struct wpa_driver_scan_params *params,
4024 u32 interval)
4025{
4026 struct i802_bss *bss = priv;
4027 struct wpa_driver_nl80211_data *drv = bss->drv;
6afbc3d6 4028 int ret = -1;
95ac3bf4 4029 struct nl_msg *msg;
d21c63b9
LC
4030 size_t i;
4031
565110cd
JM
4032 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: sched_scan request");
4033
216eede8
DS
4034#ifdef ANDROID
4035 if (!drv->capa.sched_scan_supported)
4036 return android_pno_start(bss, params);
4037#endif /* ANDROID */
4038
95ac3bf4 4039 msg = nl80211_scan_common(drv, NL80211_CMD_START_SCHED_SCAN, params);
6afbc3d6
JM
4040 if (!msg)
4041 goto nla_put_failure;
d21c63b9 4042
d21c63b9
LC
4043 NLA_PUT_U32(msg, NL80211_ATTR_SCHED_SCAN_INTERVAL, interval);
4044
bf8d6d24
TP
4045 if ((drv->num_filter_ssids &&
4046 (int) drv->num_filter_ssids <= drv->capa.max_match_sets) ||
4047 params->filter_rssi) {
8970bae8
JB
4048 struct nlattr *match_sets;
4049 match_sets = nla_nest_start(msg, NL80211_ATTR_SCHED_SCAN_MATCH);
6afbc3d6
JM
4050 if (match_sets == NULL)
4051 goto nla_put_failure;
bd525934
LC
4052
4053 for (i = 0; i < drv->num_filter_ssids; i++) {
8970bae8 4054 struct nlattr *match_set_ssid;
bd525934
LC
4055 wpa_hexdump_ascii(MSG_MSGDUMP,
4056 "nl80211: Sched scan filter SSID",
4057 drv->filter_ssids[i].ssid,
4058 drv->filter_ssids[i].ssid_len);
4059
8970bae8 4060 match_set_ssid = nla_nest_start(msg, i + 1);
6afbc3d6
JM
4061 if (match_set_ssid == NULL)
4062 goto nla_put_failure;
8970bae8 4063 NLA_PUT(msg, NL80211_ATTR_SCHED_SCAN_MATCH_SSID,
bd525934
LC
4064 drv->filter_ssids[i].ssid_len,
4065 drv->filter_ssids[i].ssid);
4066
adc96dc2 4067 nla_nest_end(msg, match_set_ssid);
bd525934
LC
4068 }
4069
bf8d6d24 4070 if (params->filter_rssi) {
8970bae8
JB
4071 struct nlattr *match_set_rssi;
4072 match_set_rssi = nla_nest_start(msg, 0);
6afbc3d6
JM
4073 if (match_set_rssi == NULL)
4074 goto nla_put_failure;
8970bae8 4075 NLA_PUT_U32(msg, NL80211_SCHED_SCAN_MATCH_ATTR_RSSI,
bf8d6d24
TP
4076 params->filter_rssi);
4077 wpa_printf(MSG_MSGDUMP,
4078 "nl80211: Sched scan RSSI filter %d dBm",
4079 params->filter_rssi);
8970bae8 4080 nla_nest_end(msg, match_set_rssi);
bf8d6d24
TP
4081 }
4082
8970bae8 4083 nla_nest_end(msg, match_sets);
bd525934
LC
4084 }
4085
d21c63b9
LC
4086 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4087
4088 /* TODO: if we get an error here, we should fall back to normal scan */
4089
4090 msg = NULL;
4091 if (ret) {
4092 wpa_printf(MSG_DEBUG, "nl80211: Sched scan start failed: "
4093 "ret=%d (%s)", ret, strerror(-ret));
4094 goto nla_put_failure;
4095 }
4096
4097 wpa_printf(MSG_DEBUG, "nl80211: Sched scan requested (ret=%d) - "
4098 "scan interval %d msec", ret, interval);
4099
4100nla_put_failure:
d21c63b9 4101 nlmsg_free(msg);
d21c63b9
LC
4102 return ret;
4103}
4104
4105
4106/**
4107 * wpa_driver_nl80211_stop_sched_scan - Stop a scheduled scan
4108 * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
4109 * Returns: 0 on success, -1 on failure or if not supported
4110 */
4111static int wpa_driver_nl80211_stop_sched_scan(void *priv)
4112{
4113 struct i802_bss *bss = priv;
4114 struct wpa_driver_nl80211_data *drv = bss->drv;
4115 int ret = 0;
4116 struct nl_msg *msg;
4117
216eede8
DS
4118#ifdef ANDROID
4119 if (!drv->capa.sched_scan_supported)
4120 return android_pno_stop(bss);
4121#endif /* ANDROID */
4122
d21c63b9
LC
4123 msg = nlmsg_alloc();
4124 if (!msg)
4125 return -1;
4126
9fb04070 4127 nl80211_cmd(drv, msg, 0, NL80211_CMD_STOP_SCHED_SCAN);
d21c63b9
LC
4128
4129 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
4130
4131 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4132 msg = NULL;
4133 if (ret) {
4134 wpa_printf(MSG_DEBUG, "nl80211: Sched scan stop failed: "
4135 "ret=%d (%s)", ret, strerror(-ret));
4136 goto nla_put_failure;
4137 }
4138
4139 wpa_printf(MSG_DEBUG, "nl80211: Sched scan stop sent (ret=%d)", ret);
4140
4141nla_put_failure:
4142 nlmsg_free(msg);
4143 return ret;
4144}
4145
4146
3812464c
JM
4147static const u8 * nl80211_get_ie(const u8 *ies, size_t ies_len, u8 ie)
4148{
4149 const u8 *end, *pos;
4150
4151 if (ies == NULL)
4152 return NULL;
4153
4154 pos = ies;
4155 end = ies + ies_len;
4156
4157 while (pos + 1 < end) {
4158 if (pos + 2 + pos[1] > end)
4159 break;
4160 if (pos[0] == ie)
4161 return pos;
4162 pos += 2 + pos[1];
4163 }
4164
4165 return NULL;
4166}
4167
4168
4169static int nl80211_scan_filtered(struct wpa_driver_nl80211_data *drv,
4170 const u8 *ie, size_t ie_len)
4171{
4172 const u8 *ssid;
4173 size_t i;
4174
4175 if (drv->filter_ssids == NULL)
4176 return 0;
4177
4178 ssid = nl80211_get_ie(ie, ie_len, WLAN_EID_SSID);
4179 if (ssid == NULL)
4180 return 1;
4181
4182 for (i = 0; i < drv->num_filter_ssids; i++) {
4183 if (ssid[1] == drv->filter_ssids[i].ssid_len &&
4184 os_memcmp(ssid + 2, drv->filter_ssids[i].ssid, ssid[1]) ==
4185 0)
4186 return 0;
4187 }
4188
4189 return 1;
4190}
4191
4192
b3db1e1c 4193static int bss_info_handler(struct nl_msg *msg, void *arg)
3f5285e8 4194{
b3db1e1c
JM
4195 struct nlattr *tb[NL80211_ATTR_MAX + 1];
4196 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
4197 struct nlattr *bss[NL80211_BSS_MAX + 1];
4198 static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
4199 [NL80211_BSS_BSSID] = { .type = NLA_UNSPEC },
4200 [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
4201 [NL80211_BSS_TSF] = { .type = NLA_U64 },
4202 [NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 },
4203 [NL80211_BSS_CAPABILITY] = { .type = NLA_U16 },
4204 [NL80211_BSS_INFORMATION_ELEMENTS] = { .type = NLA_UNSPEC },
4205 [NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 },
4206 [NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 },
e6b8efeb 4207 [NL80211_BSS_STATUS] = { .type = NLA_U32 },
b3ad11bb 4208 [NL80211_BSS_SEEN_MS_AGO] = { .type = NLA_U32 },
8c090654 4209 [NL80211_BSS_BEACON_IES] = { .type = NLA_UNSPEC },
b3db1e1c 4210 };
3812464c
JM
4211 struct nl80211_bss_info_arg *_arg = arg;
4212 struct wpa_scan_results *res = _arg->res;
3f5285e8
JM
4213 struct wpa_scan_res **tmp;
4214 struct wpa_scan_res *r;
8c090654
JM
4215 const u8 *ie, *beacon_ie;
4216 size_t ie_len, beacon_ie_len;
4217 u8 *pos;
46957a9b 4218 size_t i;
3f5285e8 4219
b3db1e1c
JM
4220 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
4221 genlmsg_attrlen(gnlh, 0), NULL);
4222 if (!tb[NL80211_ATTR_BSS])
4223 return NL_SKIP;
4224 if (nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS],
4225 bss_policy))
4226 return NL_SKIP;
f5a8d422
JM
4227 if (bss[NL80211_BSS_STATUS]) {
4228 enum nl80211_bss_status status;
4229 status = nla_get_u32(bss[NL80211_BSS_STATUS]);
4230 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
4231 bss[NL80211_BSS_FREQUENCY]) {
4232 _arg->assoc_freq =
4233 nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
4234 wpa_printf(MSG_DEBUG, "nl80211: Associated on %u MHz",
4235 _arg->assoc_freq);
4236 }
20f5a4c2
JM
4237 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
4238 bss[NL80211_BSS_BSSID]) {
4239 os_memcpy(_arg->assoc_bssid,
4240 nla_data(bss[NL80211_BSS_BSSID]), ETH_ALEN);
4241 wpa_printf(MSG_DEBUG, "nl80211: Associated with "
4242 MACSTR, MAC2STR(_arg->assoc_bssid));
4243 }
f5a8d422
JM
4244 }
4245 if (!res)
4246 return NL_SKIP;
b3db1e1c
JM
4247 if (bss[NL80211_BSS_INFORMATION_ELEMENTS]) {
4248 ie = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
4249 ie_len = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
4250 } else {
4251 ie = NULL;
4252 ie_len = 0;
4253 }
8c090654
JM
4254 if (bss[NL80211_BSS_BEACON_IES]) {
4255 beacon_ie = nla_data(bss[NL80211_BSS_BEACON_IES]);
4256 beacon_ie_len = nla_len(bss[NL80211_BSS_BEACON_IES]);
4257 } else {
4258 beacon_ie = NULL;
4259 beacon_ie_len = 0;
4260 }
3f5285e8 4261
3812464c
JM
4262 if (nl80211_scan_filtered(_arg->drv, ie ? ie : beacon_ie,
4263 ie ? ie_len : beacon_ie_len))
4264 return NL_SKIP;
4265
8c090654 4266 r = os_zalloc(sizeof(*r) + ie_len + beacon_ie_len);
3f5285e8 4267 if (r == NULL)
b3db1e1c
JM
4268 return NL_SKIP;
4269 if (bss[NL80211_BSS_BSSID])
4270 os_memcpy(r->bssid, nla_data(bss[NL80211_BSS_BSSID]),
4271 ETH_ALEN);
4272 if (bss[NL80211_BSS_FREQUENCY])
4273 r->freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
4274 if (bss[NL80211_BSS_BEACON_INTERVAL])
4275 r->beacon_int = nla_get_u16(bss[NL80211_BSS_BEACON_INTERVAL]);
4276 if (bss[NL80211_BSS_CAPABILITY])
4277 r->caps = nla_get_u16(bss[NL80211_BSS_CAPABILITY]);
7c2849d2
JM
4278 r->flags |= WPA_SCAN_NOISE_INVALID;
4279 if (bss[NL80211_BSS_SIGNAL_MBM]) {
b3db1e1c 4280 r->level = nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]);
7c2849d2
JM
4281 r->level /= 100; /* mBm to dBm */
4282 r->flags |= WPA_SCAN_LEVEL_DBM | WPA_SCAN_QUAL_INVALID;
4283 } else if (bss[NL80211_BSS_SIGNAL_UNSPEC]) {
4284 r->level = nla_get_u8(bss[NL80211_BSS_SIGNAL_UNSPEC]);
98ac6763 4285 r->flags |= WPA_SCAN_QUAL_INVALID;
7c2849d2
JM
4286 } else
4287 r->flags |= WPA_SCAN_LEVEL_INVALID | WPA_SCAN_QUAL_INVALID;
b3db1e1c
JM
4288 if (bss[NL80211_BSS_TSF])
4289 r->tsf = nla_get_u64(bss[NL80211_BSS_TSF]);
b3ad11bb
JM
4290 if (bss[NL80211_BSS_SEEN_MS_AGO])
4291 r->age = nla_get_u32(bss[NL80211_BSS_SEEN_MS_AGO]);
b3db1e1c 4292 r->ie_len = ie_len;
8c090654
JM
4293 pos = (u8 *) (r + 1);
4294 if (ie) {
4295 os_memcpy(pos, ie, ie_len);
4296 pos += ie_len;
4297 }
4298 r->beacon_ie_len = beacon_ie_len;
4299 if (beacon_ie)
4300 os_memcpy(pos, beacon_ie, beacon_ie_len);
3f5285e8 4301
e6b8efeb
JM
4302 if (bss[NL80211_BSS_STATUS]) {
4303 enum nl80211_bss_status status;
4304 status = nla_get_u32(bss[NL80211_BSS_STATUS]);
4305 switch (status) {
4306 case NL80211_BSS_STATUS_AUTHENTICATED:
4307 r->flags |= WPA_SCAN_AUTHENTICATED;
4308 break;
4309 case NL80211_BSS_STATUS_ASSOCIATED:
4310 r->flags |= WPA_SCAN_ASSOCIATED;
4311 break;
4312 default:
4313 break;
4314 }
4315 }
4316
46957a9b
JM
4317 /*
4318 * cfg80211 maintains separate BSS table entries for APs if the same
4319 * BSSID,SSID pair is seen on multiple channels. wpa_supplicant does
4320 * not use frequency as a separate key in the BSS table, so filter out
4321 * duplicated entries. Prefer associated BSS entry in such a case in
4322 * order to get the correct frequency into the BSS table.
4323 */
4324 for (i = 0; i < res->num; i++) {
4325 const u8 *s1, *s2;
4326 if (os_memcmp(res->res[i]->bssid, r->bssid, ETH_ALEN) != 0)
4327 continue;
4328
4329 s1 = nl80211_get_ie((u8 *) (res->res[i] + 1),
4330 res->res[i]->ie_len, WLAN_EID_SSID);
4331 s2 = nl80211_get_ie((u8 *) (r + 1), r->ie_len, WLAN_EID_SSID);
4332 if (s1 == NULL || s2 == NULL || s1[1] != s2[1] ||
4333 os_memcmp(s1, s2, 2 + s1[1]) != 0)
4334 continue;
4335
4336 /* Same BSSID,SSID was already included in scan results */
4337 wpa_printf(MSG_DEBUG, "nl80211: Remove duplicated scan result "
4338 "for " MACSTR, MAC2STR(r->bssid));
4339
4340 if ((r->flags & WPA_SCAN_ASSOCIATED) &&
4341 !(res->res[i]->flags & WPA_SCAN_ASSOCIATED)) {
4342 os_free(res->res[i]);
4343 res->res[i] = r;
4344 } else
4345 os_free(r);
4346 return NL_SKIP;
4347 }
4348
067ffa26
JM
4349 tmp = os_realloc_array(res->res, res->num + 1,
4350 sizeof(struct wpa_scan_res *));
3f5285e8
JM
4351 if (tmp == NULL) {
4352 os_free(r);
b3db1e1c 4353 return NL_SKIP;
3f5285e8
JM
4354 }
4355 tmp[res->num++] = r;
4356 res->res = tmp;
b3db1e1c
JM
4357
4358 return NL_SKIP;
3f5285e8 4359}
b3db1e1c 4360
3f5285e8 4361
d72aad94
JM
4362static void clear_state_mismatch(struct wpa_driver_nl80211_data *drv,
4363 const u8 *addr)
4364{
4365 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
4366 wpa_printf(MSG_DEBUG, "nl80211: Clear possible state "
582507be 4367 "mismatch (" MACSTR ")", MAC2STR(addr));
d72aad94
JM
4368 wpa_driver_nl80211_mlme(drv, addr,
4369 NL80211_CMD_DEAUTHENTICATE,
77339912 4370 WLAN_REASON_PREV_AUTH_NOT_VALID, 1);
d72aad94
JM
4371 }
4372}
4373
4374
e6b8efeb
JM
4375static void wpa_driver_nl80211_check_bss_status(
4376 struct wpa_driver_nl80211_data *drv, struct wpa_scan_results *res)
4377{
4378 size_t i;
4379
4380 for (i = 0; i < res->num; i++) {
4381 struct wpa_scan_res *r = res->res[i];
4382 if (r->flags & WPA_SCAN_AUTHENTICATED) {
4383 wpa_printf(MSG_DEBUG, "nl80211: Scan results "
4384 "indicates BSS status with " MACSTR
4385 " as authenticated",
4386 MAC2STR(r->bssid));
b1f625e0 4387 if (is_sta_interface(drv->nlmode) &&
e6b8efeb
JM
4388 os_memcmp(r->bssid, drv->bssid, ETH_ALEN) != 0 &&
4389 os_memcmp(r->bssid, drv->auth_bssid, ETH_ALEN) !=
4390 0) {
4391 wpa_printf(MSG_DEBUG, "nl80211: Unknown BSSID"
4392 " in local state (auth=" MACSTR
4393 " assoc=" MACSTR ")",
4394 MAC2STR(drv->auth_bssid),
4395 MAC2STR(drv->bssid));
582507be 4396 clear_state_mismatch(drv, r->bssid);
e6b8efeb
JM
4397 }
4398 }
4399
4400 if (r->flags & WPA_SCAN_ASSOCIATED) {
4401 wpa_printf(MSG_DEBUG, "nl80211: Scan results "
4402 "indicate BSS status with " MACSTR
4403 " as associated",
4404 MAC2STR(r->bssid));
b1f625e0 4405 if (is_sta_interface(drv->nlmode) &&
e6b8efeb
JM
4406 !drv->associated) {
4407 wpa_printf(MSG_DEBUG, "nl80211: Local state "
4408 "(not associated) does not match "
4409 "with BSS state");
d72aad94 4410 clear_state_mismatch(drv, r->bssid);
b1f625e0 4411 } else if (is_sta_interface(drv->nlmode) &&
e6b8efeb
JM
4412 os_memcmp(drv->bssid, r->bssid, ETH_ALEN) !=
4413 0) {
4414 wpa_printf(MSG_DEBUG, "nl80211: Local state "
4415 "(associated with " MACSTR ") does "
4416 "not match with BSS state",
d72aad94
JM
4417 MAC2STR(drv->bssid));
4418 clear_state_mismatch(drv, r->bssid);
4419 clear_state_mismatch(drv, drv->bssid);
e6b8efeb
JM
4420 }
4421 }
4422 }
4423}
4424
4425
7e5ba1b9 4426static struct wpa_scan_results *
8856462d 4427nl80211_get_scan_results(struct wpa_driver_nl80211_data *drv)
3f5285e8 4428{
b3db1e1c 4429 struct nl_msg *msg;
3f5285e8 4430 struct wpa_scan_results *res;
b3db1e1c 4431 int ret;
3812464c 4432 struct nl80211_bss_info_arg arg;
3f5285e8
JM
4433
4434 res = os_zalloc(sizeof(*res));
b3db1e1c 4435 if (res == NULL)
8e2c104f 4436 return NULL;
b3db1e1c
JM
4437 msg = nlmsg_alloc();
4438 if (!msg)
4439 goto nla_put_failure;
3f5285e8 4440
9fb04070 4441 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
b3db1e1c 4442 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
3f5285e8 4443
3812464c
JM
4444 arg.drv = drv;
4445 arg.res = res;
4446 ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
b3db1e1c
JM
4447 msg = NULL;
4448 if (ret == 0) {
577db0ae
GM
4449 wpa_printf(MSG_DEBUG, "nl80211: Received scan results (%lu "
4450 "BSSes)", (unsigned long) res->num);
4451 nl80211_get_noise_for_scan_results(drv, res);
b3db1e1c 4452 return res;
3f5285e8 4453 }
b3db1e1c
JM
4454 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
4455 "(%s)", ret, strerror(-ret));
4456nla_put_failure:
4457 nlmsg_free(msg);
4458 wpa_scan_results_free(res);
4459 return NULL;
3f5285e8
JM
4460}
4461
4462
8856462d
JM
4463/**
4464 * wpa_driver_nl80211_get_scan_results - Fetch the latest scan results
4465 * @priv: Pointer to private wext data from wpa_driver_nl80211_init()
4466 * Returns: Scan results on success, -1 on failure
4467 */
4468static struct wpa_scan_results *
4469wpa_driver_nl80211_get_scan_results(void *priv)
4470{
a2e40bb6
FF
4471 struct i802_bss *bss = priv;
4472 struct wpa_driver_nl80211_data *drv = bss->drv;
8856462d
JM
4473 struct wpa_scan_results *res;
4474
4475 res = nl80211_get_scan_results(drv);
4476 if (res)
4477 wpa_driver_nl80211_check_bss_status(drv, res);
4478 return res;
4479}
4480
4481
4482static void nl80211_dump_scan(struct wpa_driver_nl80211_data *drv)
4483{
4484 struct wpa_scan_results *res;
4485 size_t i;
4486
4487 res = nl80211_get_scan_results(drv);
4488 if (res == NULL) {
4489 wpa_printf(MSG_DEBUG, "nl80211: Failed to get scan results");
4490 return;
4491 }
4492
4493 wpa_printf(MSG_DEBUG, "nl80211: Scan result dump");
4494 for (i = 0; i < res->num; i++) {
4495 struct wpa_scan_res *r = res->res[i];
4496 wpa_printf(MSG_DEBUG, "nl80211: %d/%d " MACSTR "%s%s",
4497 (int) i, (int) res->num, MAC2STR(r->bssid),
4498 r->flags & WPA_SCAN_AUTHENTICATED ? " [auth]" : "",
4499 r->flags & WPA_SCAN_ASSOCIATED ? " [assoc]" : "");
4500 }
4501
4502 wpa_scan_results_free(res);
4503}
4504
4505
9ebce9c5 4506static int wpa_driver_nl80211_set_key(const char *ifname, struct i802_bss *bss,
71934751
JM
4507 enum wpa_alg alg, const u8 *addr,
4508 int key_idx, int set_tx,
642187d6
JM
4509 const u8 *seq, size_t seq_len,
4510 const u8 *key, size_t key_len)
3f5285e8 4511{
a2e40bb6 4512 struct wpa_driver_nl80211_data *drv = bss->drv;
642187d6 4513 int ifindex = if_nametoindex(ifname);
3f5285e8 4514 struct nl_msg *msg;
1ad1cdc2 4515 int ret;
3f5285e8 4516
1ad1cdc2
JM
4517 wpa_printf(MSG_DEBUG, "%s: ifindex=%d alg=%d addr=%p key_idx=%d "
4518 "set_tx=%d seq_len=%lu key_len=%lu",
4519 __func__, ifindex, alg, addr, key_idx, set_tx,
3f5285e8 4520 (unsigned long) seq_len, (unsigned long) key_len);
8c66e185
JM
4521#ifdef CONFIG_TDLS
4522 if (key_idx == -1)
4523 key_idx = 0;
4524#endif /* CONFIG_TDLS */
3f5285e8
JM
4525
4526 msg = nlmsg_alloc();
1ad1cdc2
JM
4527 if (!msg)
4528 return -ENOMEM;
3f5285e8
JM
4529
4530 if (alg == WPA_ALG_NONE) {
9fb04070 4531 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_KEY);
3f5285e8 4532 } else {
9fb04070 4533 nl80211_cmd(drv, msg, 0, NL80211_CMD_NEW_KEY);
3f5285e8 4534 NLA_PUT(msg, NL80211_ATTR_KEY_DATA, key_len, key);
d723bab4
JM
4535 switch (alg) {
4536 case WPA_ALG_WEP:
4537 if (key_len == 5)
4538 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
4539 WLAN_CIPHER_SUITE_WEP40);
4540 else
4541 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
4542 WLAN_CIPHER_SUITE_WEP104);
4543 break;
4544 case WPA_ALG_TKIP:
4545 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
4546 WLAN_CIPHER_SUITE_TKIP);
4547 break;
4548 case WPA_ALG_CCMP:
4549 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
4550 WLAN_CIPHER_SUITE_CCMP);
4551 break;
eb7719ff
JM
4552 case WPA_ALG_GCMP:
4553 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
4554 WLAN_CIPHER_SUITE_GCMP);
4555 break;
d723bab4
JM
4556 case WPA_ALG_IGTK:
4557 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
4558 WLAN_CIPHER_SUITE_AES_CMAC);
4559 break;
369c8d7b
JM
4560 case WPA_ALG_SMS4:
4561 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
4562 WLAN_CIPHER_SUITE_SMS4);
4563 break;
4564 case WPA_ALG_KRK:
4565 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
4566 WLAN_CIPHER_SUITE_KRK);
4567 break;
d723bab4
JM
4568 default:
4569 wpa_printf(MSG_ERROR, "%s: Unsupported encryption "
4570 "algorithm %d", __func__, alg);
3f5285e8
JM
4571 nlmsg_free(msg);
4572 return -1;
4573 }
4574 }
4575
849ef835 4576 if (seq && seq_len)
1ad1cdc2
JM
4577 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, seq_len, seq);
4578
0382097e 4579 if (addr && !is_broadcast_ether_addr(addr)) {
3f5285e8
JM
4580 wpa_printf(MSG_DEBUG, " addr=" MACSTR, MAC2STR(addr));
4581 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
89c38e32
JM
4582
4583 if (alg != WPA_ALG_WEP && key_idx && !set_tx) {
4584 wpa_printf(MSG_DEBUG, " RSN IBSS RX GTK");
4585 NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE,
4586 NL80211_KEYTYPE_GROUP);
4587 }
60ea8187 4588 } else if (addr && is_broadcast_ether_addr(addr)) {
8970bae8
JB
4589 struct nlattr *types;
4590
60ea8187 4591 wpa_printf(MSG_DEBUG, " broadcast key");
8970bae8
JB
4592
4593 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
60ea8187
JM
4594 if (!types)
4595 goto nla_put_failure;
8970bae8
JB
4596 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST);
4597 nla_nest_end(msg, types);
3f5285e8
JM
4598 }
4599 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
1ad1cdc2 4600 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
3f5285e8 4601
1ad1cdc2 4602 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
15664ad0 4603 if ((ret == -ENOENT || ret == -ENOLINK) && alg == WPA_ALG_NONE)
1ad1cdc2
JM
4604 ret = 0;
4605 if (ret)
4606 wpa_printf(MSG_DEBUG, "nl80211: set_key failed; err=%d %s)",
4607 ret, strerror(-ret));
3f5285e8 4608
1ad1cdc2
JM
4609 /*
4610 * If we failed or don't need to set the default TX key (below),
4611 * we're done here.
4612 */
4613 if (ret || !set_tx || alg == WPA_ALG_NONE)
4614 return ret;
b1f625e0 4615 if (is_ap_interface(drv->nlmode) && addr &&
0382097e 4616 !is_broadcast_ether_addr(addr))
de12717a 4617 return ret;
3f5285e8 4618
1ad1cdc2
JM
4619 msg = nlmsg_alloc();
4620 if (!msg)
4621 return -ENOMEM;
3f5285e8 4622
9fb04070 4623 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_KEY);
1ad1cdc2
JM
4624 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
4625 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
4626 if (alg == WPA_ALG_IGTK)
4627 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT_MGMT);
4628 else
4629 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT);
60ea8187 4630 if (addr && is_broadcast_ether_addr(addr)) {
8970bae8
JB
4631 struct nlattr *types;
4632
4633 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
60ea8187
JM
4634 if (!types)
4635 goto nla_put_failure;
8970bae8
JB
4636 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST);
4637 nla_nest_end(msg, types);
60ea8187 4638 } else if (addr) {
8970bae8
JB
4639 struct nlattr *types;
4640
4641 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
60ea8187
JM
4642 if (!types)
4643 goto nla_put_failure;
8970bae8
JB
4644 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_UNICAST);
4645 nla_nest_end(msg, types);
60ea8187 4646 }
3f5285e8 4647
1ad1cdc2
JM
4648 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4649 if (ret == -ENOENT)
4650 ret = 0;
4651 if (ret)
4652 wpa_printf(MSG_DEBUG, "nl80211: set_key default failed; "
4653 "err=%d %s)", ret, strerror(-ret));
4654 return ret;
3f5285e8
JM
4655
4656nla_put_failure:
5883168a 4657 nlmsg_free(msg);
6241fcb1 4658 return -ENOBUFS;
3f5285e8
JM
4659}
4660
4661
71934751 4662static int nl_add_key(struct nl_msg *msg, enum wpa_alg alg,
0194fedb
JB
4663 int key_idx, int defkey,
4664 const u8 *seq, size_t seq_len,
4665 const u8 *key, size_t key_len)
4666{
4667 struct nlattr *key_attr = nla_nest_start(msg, NL80211_ATTR_KEY);
4668 if (!key_attr)
4669 return -1;
4670
4671 if (defkey && alg == WPA_ALG_IGTK)
4672 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_MGMT);
4673 else if (defkey)
4674 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
4675
4676 NLA_PUT_U8(msg, NL80211_KEY_IDX, key_idx);
4677
d723bab4
JM
4678 switch (alg) {
4679 case WPA_ALG_WEP:
4680 if (key_len == 5)
2aa5f847
JM
4681 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
4682 WLAN_CIPHER_SUITE_WEP40);
d723bab4 4683 else
2aa5f847
JM
4684 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
4685 WLAN_CIPHER_SUITE_WEP104);
d723bab4
JM
4686 break;
4687 case WPA_ALG_TKIP:
2aa5f847 4688 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, WLAN_CIPHER_SUITE_TKIP);
d723bab4
JM
4689 break;
4690 case WPA_ALG_CCMP:
2aa5f847 4691 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, WLAN_CIPHER_SUITE_CCMP);
d723bab4 4692 break;
eb7719ff
JM
4693 case WPA_ALG_GCMP:
4694 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, WLAN_CIPHER_SUITE_GCMP);
4695 break;
d723bab4 4696 case WPA_ALG_IGTK:
2aa5f847
JM
4697 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
4698 WLAN_CIPHER_SUITE_AES_CMAC);
d723bab4
JM
4699 break;
4700 default:
4701 wpa_printf(MSG_ERROR, "%s: Unsupported encryption "
4702 "algorithm %d", __func__, alg);
0194fedb 4703 return -1;
d723bab4 4704 }
0194fedb
JB
4705
4706 if (seq && seq_len)
4707 NLA_PUT(msg, NL80211_KEY_SEQ, seq_len, seq);
4708
4709 NLA_PUT(msg, NL80211_KEY_DATA, key_len, key);
4710
4711 nla_nest_end(msg, key_attr);
4712
4713 return 0;
4714 nla_put_failure:
4715 return -1;
4716}
4717
c811d5bc 4718
cfaab580
ZY
4719static int nl80211_set_conn_keys(struct wpa_driver_associate_params *params,
4720 struct nl_msg *msg)
4721{
4722 int i, privacy = 0;
4723 struct nlattr *nl_keys, *nl_key;
4724
4725 for (i = 0; i < 4; i++) {
4726 if (!params->wep_key[i])
4727 continue;
4728 privacy = 1;
4729 break;
4730 }
ce04af5a
JM
4731 if (params->wps == WPS_MODE_PRIVACY)
4732 privacy = 1;
4733 if (params->pairwise_suite &&
4734 params->pairwise_suite != WPA_CIPHER_NONE)
4735 privacy = 1;
4736
cfaab580
ZY
4737 if (!privacy)
4738 return 0;
4739
4740 NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
4741
4742 nl_keys = nla_nest_start(msg, NL80211_ATTR_KEYS);
4743 if (!nl_keys)
4744 goto nla_put_failure;
4745
4746 for (i = 0; i < 4; i++) {
4747 if (!params->wep_key[i])
4748 continue;
4749
4750 nl_key = nla_nest_start(msg, i);
4751 if (!nl_key)
4752 goto nla_put_failure;
4753
4754 NLA_PUT(msg, NL80211_KEY_DATA, params->wep_key_len[i],
4755 params->wep_key[i]);
4756 if (params->wep_key_len[i] == 5)
4757 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
4758 WLAN_CIPHER_SUITE_WEP40);
4759 else
4760 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
4761 WLAN_CIPHER_SUITE_WEP104);
4762
4763 NLA_PUT_U8(msg, NL80211_KEY_IDX, i);
4764
4765 if (i == params->wep_tx_keyidx)
4766 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
4767
4768 nla_nest_end(msg, nl_key);
4769 }
4770 nla_nest_end(msg, nl_keys);
4771
4772 return 0;
4773
4774nla_put_failure:
4775 return -ENOBUFS;
4776}
4777
4778
c2a04078 4779static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
77339912
JM
4780 const u8 *addr, int cmd, u16 reason_code,
4781 int local_state_change)
c2a04078
JM
4782{
4783 int ret = -1;
4784 struct nl_msg *msg;
4785
4786 msg = nlmsg_alloc();
4787 if (!msg)
4788 return -1;
4789
9fb04070 4790 nl80211_cmd(drv, msg, 0, cmd);
c2a04078
JM
4791
4792 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
4793 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason_code);
817762d9
MI
4794 if (addr)
4795 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
77339912
JM
4796 if (local_state_change)
4797 NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE);
c2a04078
JM
4798
4799 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4800 msg = NULL;
4801 if (ret) {
3b7ea880
BG
4802 wpa_dbg(drv->ctx, MSG_DEBUG,
4803 "nl80211: MLME command failed: reason=%u ret=%d (%s)",
4804 reason_code, ret, strerror(-ret));
c2a04078
JM
4805 goto nla_put_failure;
4806 }
4807 ret = 0;
4808
4809nla_put_failure:
4810 nlmsg_free(msg);
4811 return ret;
4812}
3f5285e8
JM
4813
4814
cfaab580 4815static int wpa_driver_nl80211_disconnect(struct wpa_driver_nl80211_data *drv,
817762d9 4816 int reason_code)
cfaab580 4817{
817762d9 4818 wpa_printf(MSG_DEBUG, "%s(reason_code=%d)", __func__, reason_code);
cfaab580 4819 drv->associated = 0;
a8c5b43a 4820 drv->ignore_next_local_disconnect = 0;
817762d9
MI
4821 /* Disconnect command doesn't need BSSID - it uses cached value */
4822 return wpa_driver_nl80211_mlme(drv, NULL, NL80211_CMD_DISCONNECT,
77339912 4823 reason_code, 0);
cfaab580
ZY
4824}
4825
4826
9ebce9c5
JM
4827static int wpa_driver_nl80211_deauthenticate(struct i802_bss *bss,
4828 const u8 *addr, int reason_code)
3f5285e8 4829{
a2e40bb6 4830 struct wpa_driver_nl80211_data *drv = bss->drv;
cfaab580 4831 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME))
817762d9 4832 return wpa_driver_nl80211_disconnect(drv, reason_code);
2e75a2b3
JM
4833 wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " reason_code=%d)",
4834 __func__, MAC2STR(addr), reason_code);
13405f35 4835 drv->associated = 0;
21bdbe38
JM
4836 if (drv->nlmode == NL80211_IFTYPE_ADHOC)
4837 return nl80211_leave_ibss(drv);
c2a04078 4838 return wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DEAUTHENTICATE,
77339912 4839 reason_code, 0);
3f5285e8
JM
4840}
4841
4842
536fd62d
JM
4843static void nl80211_copy_auth_params(struct wpa_driver_nl80211_data *drv,
4844 struct wpa_driver_auth_params *params)
4845{
4846 int i;
4847
4848 drv->auth_freq = params->freq;
4849 drv->auth_alg = params->auth_alg;
4850 drv->auth_wep_tx_keyidx = params->wep_tx_keyidx;
4851 drv->auth_local_state_change = params->local_state_change;
4852 drv->auth_p2p = params->p2p;
4853
4854 if (params->bssid)
4855 os_memcpy(drv->auth_bssid_, params->bssid, ETH_ALEN);
4856 else
4857 os_memset(drv->auth_bssid_, 0, ETH_ALEN);
4858
4859 if (params->ssid) {
4860 os_memcpy(drv->auth_ssid, params->ssid, params->ssid_len);
4861 drv->auth_ssid_len = params->ssid_len;
4862 } else
4863 drv->auth_ssid_len = 0;
4864
4865
4866 os_free(drv->auth_ie);
4867 drv->auth_ie = NULL;
4868 drv->auth_ie_len = 0;
4869 if (params->ie) {
4870 drv->auth_ie = os_malloc(params->ie_len);
4871 if (drv->auth_ie) {
4872 os_memcpy(drv->auth_ie, params->ie, params->ie_len);
4873 drv->auth_ie_len = params->ie_len;
4874 }
4875 }
4876
4877 for (i = 0; i < 4; i++) {
4878 if (params->wep_key[i] && params->wep_key_len[i] &&
4879 params->wep_key_len[i] <= 16) {
4880 os_memcpy(drv->auth_wep_key[i], params->wep_key[i],
4881 params->wep_key_len[i]);
4882 drv->auth_wep_key_len[i] = params->wep_key_len[i];
4883 } else
4884 drv->auth_wep_key_len[i] = 0;
4885 }
4886}
4887
4888
c2a04078 4889static int wpa_driver_nl80211_authenticate(
9ebce9c5 4890 struct i802_bss *bss, struct wpa_driver_auth_params *params)
c2a04078 4891{
a2e40bb6 4892 struct wpa_driver_nl80211_data *drv = bss->drv;
a0b2f99b 4893 int ret = -1, i;
c2a04078
JM
4894 struct nl_msg *msg;
4895 enum nl80211_auth_type type;
2f4f73b1 4896 enum nl80211_iftype nlmode;
6d6f4bb8 4897 int count = 0;
536fd62d
JM
4898 int is_retry;
4899
4900 is_retry = drv->retry_auth;
4901 drv->retry_auth = 0;
c2a04078
JM
4902
4903 drv->associated = 0;
e6b8efeb 4904 os_memset(drv->auth_bssid, 0, ETH_ALEN);
af473088 4905 /* FIX: IBSS mode */
2f4f73b1
EP
4906 nlmode = params->p2p ?
4907 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
4908 if (drv->nlmode != nlmode &&
9ebce9c5 4909 wpa_driver_nl80211_set_mode(bss, nlmode) < 0)
4a867032
JM
4910 return -1;
4911
6d6f4bb8 4912retry:
c2a04078
JM
4913 msg = nlmsg_alloc();
4914 if (!msg)
4915 return -1;
4916
4917 wpa_printf(MSG_DEBUG, "nl80211: Authenticate (ifindex=%d)",
4918 drv->ifindex);
a0b2f99b 4919
9fb04070 4920 nl80211_cmd(drv, msg, 0, NL80211_CMD_AUTHENTICATE);
0194fedb 4921
a0b2f99b
JM
4922 for (i = 0; i < 4; i++) {
4923 if (!params->wep_key[i])
4924 continue;
9ebce9c5 4925 wpa_driver_nl80211_set_key(bss->ifname, bss, WPA_ALG_WEP,
2ea2fcc7 4926 NULL, i,
a0b2f99b
JM
4927 i == params->wep_tx_keyidx, NULL, 0,
4928 params->wep_key[i],
4929 params->wep_key_len[i]);
0194fedb
JB
4930 if (params->wep_tx_keyidx != i)
4931 continue;
4932 if (nl_add_key(msg, WPA_ALG_WEP, i, 1, NULL, 0,
4933 params->wep_key[i], params->wep_key_len[i])) {
4934 nlmsg_free(msg);
4935 return -1;
4936 }
a0b2f99b
JM
4937 }
4938
c2a04078
JM
4939 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
4940 if (params->bssid) {
4941 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
4942 MAC2STR(params->bssid));
4943 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
4944 }
4945 if (params->freq) {
4946 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
4947 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
4948 }
4949 if (params->ssid) {
4950 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
4951 params->ssid, params->ssid_len);
4952 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
4953 params->ssid);
4954 }
4955 wpa_hexdump(MSG_DEBUG, " * IEs", params->ie, params->ie_len);
4956 if (params->ie)
4957 NLA_PUT(msg, NL80211_ATTR_IE, params->ie_len, params->ie);
569fed90
JM
4958 if (params->sae_data) {
4959 wpa_hexdump(MSG_DEBUG, " * SAE data", params->sae_data,
4960 params->sae_data_len);
4961 NLA_PUT(msg, NL80211_ATTR_SAE_DATA, params->sae_data_len,
4962 params->sae_data);
4963 }
abd9fafa 4964 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
c2a04078 4965 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
abd9fafa 4966 else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
c2a04078 4967 type = NL80211_AUTHTYPE_SHARED_KEY;
abd9fafa 4968 else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
c2a04078 4969 type = NL80211_AUTHTYPE_NETWORK_EAP;
abd9fafa 4970 else if (params->auth_alg & WPA_AUTH_ALG_FT)
c2a04078 4971 type = NL80211_AUTHTYPE_FT;
569fed90
JM
4972 else if (params->auth_alg & WPA_AUTH_ALG_SAE)
4973 type = NL80211_AUTHTYPE_SAE;
c2a04078
JM
4974 else
4975 goto nla_put_failure;
4976 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
4977 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
77339912
JM
4978 if (params->local_state_change) {
4979 wpa_printf(MSG_DEBUG, " * Local state change only");
4980 NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE);
4981 }
c2a04078
JM
4982
4983 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4984 msg = NULL;
4985 if (ret) {
3b7ea880
BG
4986 wpa_dbg(drv->ctx, MSG_DEBUG,
4987 "nl80211: MLME command failed (auth): ret=%d (%s)",
4988 ret, strerror(-ret));
6d6f4bb8 4989 count++;
77339912
JM
4990 if (ret == -EALREADY && count == 1 && params->bssid &&
4991 !params->local_state_change) {
6d6f4bb8
JM
4992 /*
4993 * mac80211 does not currently accept new
4994 * authentication if we are already authenticated. As a
4995 * workaround, force deauthentication and try again.
4996 */
4997 wpa_printf(MSG_DEBUG, "nl80211: Retry authentication "
4998 "after forced deauthentication");
4999 wpa_driver_nl80211_deauthenticate(
5205c4f9 5000 bss, params->bssid,
6d6f4bb8
JM
5001 WLAN_REASON_PREV_AUTH_NOT_VALID);
5002 nlmsg_free(msg);
5003 goto retry;
5004 }
536fd62d
JM
5005
5006 if (ret == -ENOENT && params->freq && !is_retry) {
5007 /*
5008 * cfg80211 has likely expired the BSS entry even
5009 * though it was previously available in our internal
5010 * BSS table. To recover quickly, start a single
5011 * channel scan on the specified channel.
5012 */
5013 struct wpa_driver_scan_params scan;
5014 int freqs[2];
5015
5016 os_memset(&scan, 0, sizeof(scan));
5017 scan.num_ssids = 1;
5018 if (params->ssid) {
5019 scan.ssids[0].ssid = params->ssid;
5020 scan.ssids[0].ssid_len = params->ssid_len;
5021 }
5022 freqs[0] = params->freq;
5023 freqs[1] = 0;
5024 scan.freqs = freqs;
5025 wpa_printf(MSG_DEBUG, "nl80211: Trigger single "
5026 "channel scan to refresh cfg80211 BSS "
5027 "entry");
5028 ret = wpa_driver_nl80211_scan(bss, &scan);
5029 if (ret == 0) {
5030 nl80211_copy_auth_params(drv, params);
5031 drv->scan_for_auth = 1;
5032 }
8c3ba078
JM
5033 } else if (is_retry) {
5034 /*
5035 * Need to indicate this with an event since the return
5036 * value from the retry is not delivered to core code.
5037 */
5038 union wpa_event_data event;
5039 wpa_printf(MSG_DEBUG, "nl80211: Authentication retry "
5040 "failed");
5041 os_memset(&event, 0, sizeof(event));
5042 os_memcpy(event.timeout_event.addr, drv->auth_bssid_,
5043 ETH_ALEN);
5044 wpa_supplicant_event(drv->ctx, EVENT_AUTH_TIMED_OUT,
5045 &event);
536fd62d
JM
5046 }
5047
c2a04078
JM
5048 goto nla_put_failure;
5049 }
5050 ret = 0;
5051 wpa_printf(MSG_DEBUG, "nl80211: Authentication request send "
5052 "successfully");
5053
5054nla_put_failure:
5055 nlmsg_free(msg);
5056 return ret;
5057}
5058
5059
536fd62d
JM
5060static int wpa_driver_nl80211_authenticate_retry(
5061 struct wpa_driver_nl80211_data *drv)
5062{
5063 struct wpa_driver_auth_params params;
5064 struct i802_bss *bss = &drv->first_bss;
5065 int i;
5066
5067 wpa_printf(MSG_DEBUG, "nl80211: Try to authenticate again");
5068
5069 os_memset(&params, 0, sizeof(params));
5070 params.freq = drv->auth_freq;
5071 params.auth_alg = drv->auth_alg;
5072 params.wep_tx_keyidx = drv->auth_wep_tx_keyidx;
5073 params.local_state_change = drv->auth_local_state_change;
5074 params.p2p = drv->auth_p2p;
5075
5076 if (!is_zero_ether_addr(drv->auth_bssid_))
5077 params.bssid = drv->auth_bssid_;
5078
5079 if (drv->auth_ssid_len) {
5080 params.ssid = drv->auth_ssid;
5081 params.ssid_len = drv->auth_ssid_len;
5082 }
5083
5084 params.ie = drv->auth_ie;
5085 params.ie_len = drv->auth_ie_len;
5086
5087 for (i = 0; i < 4; i++) {
5088 if (drv->auth_wep_key_len[i]) {
5089 params.wep_key[i] = drv->auth_wep_key[i];
5090 params.wep_key_len[i] = drv->auth_wep_key_len[i];
5091 }
5092 }
5093
5094 drv->retry_auth = 1;
5095 return wpa_driver_nl80211_authenticate(bss, &params);
5096}
5097
5098
282d5590
JM
5099struct phy_info_arg {
5100 u16 *num_modes;
5101 struct hostapd_hw_modes *modes;
43245552 5102 int last_mode, last_chan_idx;
282d5590
JM
5103};
5104
e62a1d43
JM
5105static void phy_info_ht_capa(struct hostapd_hw_modes *mode, struct nlattr *capa,
5106 struct nlattr *ampdu_factor,
5107 struct nlattr *ampdu_density,
5108 struct nlattr *mcs_set)
5109{
5110 if (capa)
5111 mode->ht_capab = nla_get_u16(capa);
5112
5113 if (ampdu_factor)
5114 mode->a_mpdu_params |= nla_get_u8(ampdu_factor) & 0x03;
5115
5116 if (ampdu_density)
5117 mode->a_mpdu_params |= nla_get_u8(ampdu_density) << 2;
5118
5119 if (mcs_set && nla_len(mcs_set) >= 16) {
5120 u8 *mcs;
5121 mcs = nla_data(mcs_set);
5122 os_memcpy(mode->mcs_set, mcs, 16);
5123 }
5124}
5125
5126
5127static void phy_info_vht_capa(struct hostapd_hw_modes *mode,
5128 struct nlattr *capa,
5129 struct nlattr *mcs_set)
5130{
5131 if (capa)
5132 mode->vht_capab = nla_get_u32(capa);
5133
5134 if (mcs_set && nla_len(mcs_set) >= 8) {
5135 u8 *mcs;
5136 mcs = nla_data(mcs_set);
5137 os_memcpy(mode->vht_mcs_set, mcs, 8);
5138 }
5139}
5140
5141
214a77b0
JM
5142static void phy_info_freq(struct hostapd_hw_modes *mode,
5143 struct hostapd_channel_data *chan,
5144 struct nlattr *tb_freq[])
5145{
e864c0ae 5146 u8 channel;
214a77b0
JM
5147 chan->freq = nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_FREQ]);
5148 chan->flag = 0;
e864c0ae
JM
5149 if (ieee80211_freq_to_chan(chan->freq, &channel) != NUM_HOSTAPD_MODES)
5150 chan->chan = channel;
214a77b0
JM
5151
5152 if (tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
5153 chan->flag |= HOSTAPD_CHAN_DISABLED;
5154 if (tb_freq[NL80211_FREQUENCY_ATTR_PASSIVE_SCAN])
5155 chan->flag |= HOSTAPD_CHAN_PASSIVE_SCAN;
5156 if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IBSS])
5157 chan->flag |= HOSTAPD_CHAN_NO_IBSS;
5158 if (tb_freq[NL80211_FREQUENCY_ATTR_RADAR])
5159 chan->flag |= HOSTAPD_CHAN_RADAR;
5160
5161 if (tb_freq[NL80211_FREQUENCY_ATTR_MAX_TX_POWER] &&
5162 !tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
5163 chan->max_tx_power = nla_get_u32(
5164 tb_freq[NL80211_FREQUENCY_ATTR_MAX_TX_POWER]) / 100;
fc96522e
SW
5165 if (tb_freq[NL80211_FREQUENCY_ATTR_DFS_STATE]) {
5166 enum nl80211_dfs_state state =
5167 nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_DFS_STATE]);
5168
5169 switch (state) {
5170 case NL80211_DFS_USABLE:
5171 chan->flag |= HOSTAPD_CHAN_DFS_USABLE;
5172 break;
5173 case NL80211_DFS_AVAILABLE:
5174 chan->flag |= HOSTAPD_CHAN_DFS_AVAILABLE;
5175 break;
5176 case NL80211_DFS_UNAVAILABLE:
5177 chan->flag |= HOSTAPD_CHAN_DFS_UNAVAILABLE;
5178 break;
5179 }
5180 }
214a77b0
JM
5181}
5182
5183
e62a1d43
JM
5184static int phy_info_freqs(struct phy_info_arg *phy_info,
5185 struct hostapd_hw_modes *mode, struct nlattr *tb)
282d5590 5186{
282d5590
JM
5187 static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
5188 [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
5189 [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
5190 [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] = { .type = NLA_FLAG },
5191 [NL80211_FREQUENCY_ATTR_NO_IBSS] = { .type = NLA_FLAG },
5192 [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
5193 [NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32 },
fc96522e 5194 [NL80211_FREQUENCY_ATTR_DFS_STATE] = { .type = NLA_U32 },
282d5590 5195 };
e62a1d43
JM
5196 int new_channels = 0;
5197 struct hostapd_channel_data *channel;
5198 struct nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1];
5199 struct nlattr *nl_freq;
5200 int rem_freq, idx;
5201
5202 if (tb == NULL)
5203 return NL_OK;
5204
5205 nla_for_each_nested(nl_freq, tb, rem_freq) {
5206 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX,
5207 nla_data(nl_freq), nla_len(nl_freq), freq_policy);
5208 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
5209 continue;
5210 new_channels++;
5211 }
5212
5213 channel = os_realloc_array(mode->channels,
5214 mode->num_channels + new_channels,
5215 sizeof(struct hostapd_channel_data));
5216 if (!channel)
5217 return NL_SKIP;
5218
5219 mode->channels = channel;
5220 mode->num_channels += new_channels;
5221
5222 idx = phy_info->last_chan_idx;
5223
5224 nla_for_each_nested(nl_freq, tb, rem_freq) {
5225 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX,
5226 nla_data(nl_freq), nla_len(nl_freq), freq_policy);
5227 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
5228 continue;
214a77b0 5229 phy_info_freq(mode, &mode->channels[idx], tb_freq);
e62a1d43
JM
5230 idx++;
5231 }
5232 phy_info->last_chan_idx = idx;
5233
5234 return NL_OK;
5235}
5236
5237
5238static int phy_info_rates(struct hostapd_hw_modes *mode, struct nlattr *tb)
5239{
282d5590
JM
5240 static struct nla_policy rate_policy[NL80211_BITRATE_ATTR_MAX + 1] = {
5241 [NL80211_BITRATE_ATTR_RATE] = { .type = NLA_U32 },
3cfcad1b
JM
5242 [NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE] =
5243 { .type = NLA_FLAG },
282d5590 5244 };
e62a1d43 5245 struct nlattr *tb_rate[NL80211_BITRATE_ATTR_MAX + 1];
282d5590 5246 struct nlattr *nl_rate;
e62a1d43
JM
5247 int rem_rate, idx;
5248
5249 if (tb == NULL)
5250 return NL_OK;
5251
5252 nla_for_each_nested(nl_rate, tb, rem_rate) {
5253 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX,
5254 nla_data(nl_rate), nla_len(nl_rate),
5255 rate_policy);
5256 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
5257 continue;
5258 mode->num_rates++;
5259 }
5260
5261 mode->rates = os_calloc(mode->num_rates, sizeof(int));
5262 if (!mode->rates)
5263 return NL_SKIP;
5264
5265 idx = 0;
5266
5267 nla_for_each_nested(nl_rate, tb, rem_rate) {
5268 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX,
5269 nla_data(nl_rate), nla_len(nl_rate),
5270 rate_policy);
5271 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
5272 continue;
5273 mode->rates[idx] = nla_get_u32(
5274 tb_rate[NL80211_BITRATE_ATTR_RATE]);
e62a1d43
JM
5275 idx++;
5276 }
5277
5278 return NL_OK;
5279}
5280
5281
5282static int phy_info_band(struct phy_info_arg *phy_info, struct nlattr *nl_band)
5283{
5284 struct nlattr *tb_band[NL80211_BAND_ATTR_MAX + 1];
282d5590 5285 struct hostapd_hw_modes *mode;
e62a1d43 5286 int ret;
282d5590 5287
3cfcad1b
JM
5288 if (phy_info->last_mode != nl_band->nla_type) {
5289 mode = os_realloc_array(phy_info->modes,
5290 *phy_info->num_modes + 1,
5291 sizeof(*mode));
5292 if (!mode)
5293 return NL_SKIP;
5294 phy_info->modes = mode;
5295
5296 mode = &phy_info->modes[*(phy_info->num_modes)];
5297 os_memset(mode, 0, sizeof(*mode));
5298 mode->mode = NUM_HOSTAPD_MODES;
5299 mode->flags = HOSTAPD_MODE_FLAG_HT_INFO_KNOWN;
5300 *(phy_info->num_modes) += 1;
5301 phy_info->last_mode = nl_band->nla_type;
5302 phy_info->last_chan_idx = 0;
5303 } else
5304 mode = &phy_info->modes[*(phy_info->num_modes) - 1];
282d5590 5305
3cfcad1b
JM
5306 nla_parse(tb_band, NL80211_BAND_ATTR_MAX, nla_data(nl_band),
5307 nla_len(nl_band), NULL);
282d5590 5308
e62a1d43
JM
5309 phy_info_ht_capa(mode, tb_band[NL80211_BAND_ATTR_HT_CAPA],
5310 tb_band[NL80211_BAND_ATTR_HT_AMPDU_FACTOR],
5311 tb_band[NL80211_BAND_ATTR_HT_AMPDU_DENSITY],
5312 tb_band[NL80211_BAND_ATTR_HT_MCS_SET]);
5313 phy_info_vht_capa(mode, tb_band[NL80211_BAND_ATTR_VHT_CAPA],
5314 tb_band[NL80211_BAND_ATTR_VHT_MCS_SET]);
5315 ret = phy_info_freqs(phy_info, mode, tb_band[NL80211_BAND_ATTR_FREQS]);
5316 if (ret != NL_OK)
5317 return ret;
5318 ret = phy_info_rates(mode, tb_band[NL80211_BAND_ATTR_RATES]);
5319 if (ret != NL_OK)
5320 return ret;
282d5590 5321
3cfcad1b
JM
5322 return NL_OK;
5323}
5324
5325
5326static int phy_info_handler(struct nl_msg *msg, void *arg)
5327{
5328 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
5329 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
5330 struct phy_info_arg *phy_info = arg;
5331 struct nlattr *nl_band;
5332 int rem_band;
5333
5334 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
5335 genlmsg_attrlen(gnlh, 0), NULL);
5336
5337 if (!tb_msg[NL80211_ATTR_WIPHY_BANDS])
5338 return NL_SKIP;
5339
5340 nla_for_each_nested(nl_band, tb_msg[NL80211_ATTR_WIPHY_BANDS], rem_band)
5341 {
5342 int res = phy_info_band(phy_info, nl_band);
5343 if (res != NL_OK)
5344 return res;
5345 }
5346
282d5590
JM
5347 return NL_SKIP;
5348}
5349
3cfcad1b 5350
282d5590 5351static struct hostapd_hw_modes *
c30a4ab0
JB
5352wpa_driver_nl80211_postprocess_modes(struct hostapd_hw_modes *modes,
5353 u16 *num_modes)
282d5590
JM
5354{
5355 u16 m;
5356 struct hostapd_hw_modes *mode11g = NULL, *nmodes, *mode;
5357 int i, mode11g_idx = -1;
5358
c30a4ab0
JB
5359 /* heuristic to set up modes */
5360 for (m = 0; m < *num_modes; m++) {
5361 if (!modes[m].num_channels)
5362 continue;
5363 if (modes[m].channels[0].freq < 4000) {
5364 modes[m].mode = HOSTAPD_MODE_IEEE80211B;
5365 for (i = 0; i < modes[m].num_rates; i++) {
5366 if (modes[m].rates[i] > 200) {
5367 modes[m].mode = HOSTAPD_MODE_IEEE80211G;
5368 break;
5369 }
5370 }
5371 } else if (modes[m].channels[0].freq > 50000)
5372 modes[m].mode = HOSTAPD_MODE_IEEE80211AD;
5373 else
5374 modes[m].mode = HOSTAPD_MODE_IEEE80211A;
5375 }
5376
282d5590
JM
5377 /* If only 802.11g mode is included, use it to construct matching
5378 * 802.11b mode data. */
5379
5380 for (m = 0; m < *num_modes; m++) {
5381 if (modes[m].mode == HOSTAPD_MODE_IEEE80211B)
5382 return modes; /* 802.11b already included */
5383 if (modes[m].mode == HOSTAPD_MODE_IEEE80211G)
5384 mode11g_idx = m;
5385 }
5386
5387 if (mode11g_idx < 0)
5388 return modes; /* 2.4 GHz band not supported at all */
5389
067ffa26 5390 nmodes = os_realloc_array(modes, *num_modes + 1, sizeof(*nmodes));
282d5590
JM
5391 if (nmodes == NULL)
5392 return modes; /* Could not add 802.11b mode */
5393
5394 mode = &nmodes[*num_modes];
5395 os_memset(mode, 0, sizeof(*mode));
5396 (*num_modes)++;
5397 modes = nmodes;
5398
5399 mode->mode = HOSTAPD_MODE_IEEE80211B;
5400
5401 mode11g = &modes[mode11g_idx];
5402 mode->num_channels = mode11g->num_channels;
5403 mode->channels = os_malloc(mode11g->num_channels *
5404 sizeof(struct hostapd_channel_data));
5405 if (mode->channels == NULL) {
5406 (*num_modes)--;
5407 return modes; /* Could not add 802.11b mode */
5408 }
5409 os_memcpy(mode->channels, mode11g->channels,
5410 mode11g->num_channels * sizeof(struct hostapd_channel_data));
5411
5412 mode->num_rates = 0;
fb7842aa 5413 mode->rates = os_malloc(4 * sizeof(int));
282d5590
JM
5414 if (mode->rates == NULL) {
5415 os_free(mode->channels);
5416 (*num_modes)--;
5417 return modes; /* Could not add 802.11b mode */
5418 }
5419
5420 for (i = 0; i < mode11g->num_rates; i++) {
fb7842aa
JM
5421 if (mode11g->rates[i] != 10 && mode11g->rates[i] != 20 &&
5422 mode11g->rates[i] != 55 && mode11g->rates[i] != 110)
282d5590
JM
5423 continue;
5424 mode->rates[mode->num_rates] = mode11g->rates[i];
5425 mode->num_rates++;
5426 if (mode->num_rates == 4)
5427 break;
5428 }
5429
5430 if (mode->num_rates == 0) {
5431 os_free(mode->channels);
5432 os_free(mode->rates);
5433 (*num_modes)--;
5434 return modes; /* No 802.11b rates */
5435 }
5436
5437 wpa_printf(MSG_DEBUG, "nl80211: Added 802.11b mode based on 802.11g "
5438 "information");
5439
5440 return modes;
5441}
5442
5443
d8e66e80
JM
5444static void nl80211_set_ht40_mode(struct hostapd_hw_modes *mode, int start,
5445 int end)
5446{
5447 int c;
5448
5449 for (c = 0; c < mode->num_channels; c++) {
5450 struct hostapd_channel_data *chan = &mode->channels[c];
5451 if (chan->freq - 10 >= start && chan->freq + 10 <= end)
5452 chan->flag |= HOSTAPD_CHAN_HT40;
5453 }
5454}
5455
5456
5457static void nl80211_set_ht40_mode_sec(struct hostapd_hw_modes *mode, int start,
5458 int end)
5459{
5460 int c;
5461
5462 for (c = 0; c < mode->num_channels; c++) {
5463 struct hostapd_channel_data *chan = &mode->channels[c];
5464 if (!(chan->flag & HOSTAPD_CHAN_HT40))
5465 continue;
5466 if (chan->freq - 30 >= start && chan->freq - 10 <= end)
5467 chan->flag |= HOSTAPD_CHAN_HT40MINUS;
5468 if (chan->freq + 10 >= start && chan->freq + 30 <= end)
5469 chan->flag |= HOSTAPD_CHAN_HT40PLUS;
5470 }
5471}
5472
5473
5474static void nl80211_reg_rule_ht40(struct nlattr *tb[],
5475 struct phy_info_arg *results)
5476{
5477 u32 start, end, max_bw;
5478 u16 m;
5479
5480 if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
5481 tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
5482 tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
5483 return;
5484
5485 start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
5486 end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
5487 max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
5488
5489 wpa_printf(MSG_DEBUG, "nl80211: %u-%u @ %u MHz",
5490 start, end, max_bw);
5491 if (max_bw < 40)
5492 return;
5493
5494 for (m = 0; m < *results->num_modes; m++) {
5495 if (!(results->modes[m].ht_capab &
5496 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
5497 continue;
5498 nl80211_set_ht40_mode(&results->modes[m], start, end);
5499 }
5500}
5501
5502
5503static void nl80211_reg_rule_sec(struct nlattr *tb[],
5504 struct phy_info_arg *results)
5505{
5506 u32 start, end, max_bw;
5507 u16 m;
5508
5509 if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
5510 tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
5511 tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
5512 return;
5513
5514 start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
5515 end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
5516 max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
5517
5518 if (max_bw < 20)
5519 return;
5520
5521 for (m = 0; m < *results->num_modes; m++) {
5522 if (!(results->modes[m].ht_capab &
5523 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
5524 continue;
5525 nl80211_set_ht40_mode_sec(&results->modes[m], start, end);
5526 }
5527}
5528
5529
5530static int nl80211_get_reg(struct nl_msg *msg, void *arg)
5531{
5532 struct phy_info_arg *results = arg;
5533 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
5534 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
5535 struct nlattr *nl_rule;
5536 struct nlattr *tb_rule[NL80211_FREQUENCY_ATTR_MAX + 1];
5537 int rem_rule;
5538 static struct nla_policy reg_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
5539 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
5540 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
5541 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
5542 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
5543 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
5544 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
5545 };
5546
5547 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
5548 genlmsg_attrlen(gnlh, 0), NULL);
5549 if (!tb_msg[NL80211_ATTR_REG_ALPHA2] ||
5550 !tb_msg[NL80211_ATTR_REG_RULES]) {
5551 wpa_printf(MSG_DEBUG, "nl80211: No regulatory information "
5552 "available");
5553 return NL_SKIP;
5554 }
5555
5556 wpa_printf(MSG_DEBUG, "nl80211: Regulatory information - country=%s",
5557 (char *) nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]));
5558
5559 nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
5560 {
5561 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
5562 nla_data(nl_rule), nla_len(nl_rule), reg_policy);
5563 nl80211_reg_rule_ht40(tb_rule, results);
5564 }
5565
5566 nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
5567 {
5568 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
5569 nla_data(nl_rule), nla_len(nl_rule), reg_policy);
5570 nl80211_reg_rule_sec(tb_rule, results);
5571 }
5572
5573 return NL_SKIP;
5574}
5575
5576
5577static int nl80211_set_ht40_flags(struct wpa_driver_nl80211_data *drv,
5578 struct phy_info_arg *results)
5579{
5580 struct nl_msg *msg;
5581
5582 msg = nlmsg_alloc();
5583 if (!msg)
5584 return -ENOMEM;
5585
9fb04070 5586 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG);
d8e66e80
JM
5587 return send_and_recv_msgs(drv, msg, nl80211_get_reg, results);
5588}
5589
5590
282d5590
JM
5591static struct hostapd_hw_modes *
5592wpa_driver_nl80211_get_hw_feature_data(void *priv, u16 *num_modes, u16 *flags)
5593{
43245552 5594 u32 feat;
a2e40bb6
FF
5595 struct i802_bss *bss = priv;
5596 struct wpa_driver_nl80211_data *drv = bss->drv;
282d5590
JM
5597 struct nl_msg *msg;
5598 struct phy_info_arg result = {
5599 .num_modes = num_modes,
5600 .modes = NULL,
43245552 5601 .last_mode = -1,
282d5590
JM
5602 };
5603
5604 *num_modes = 0;
5605 *flags = 0;
5606
5607 msg = nlmsg_alloc();
5608 if (!msg)
5609 return NULL;
5610
43245552
DJ
5611 feat = get_nl80211_protocol_features(drv);
5612 if (feat & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP)
5613 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_WIPHY);
5614 else
5615 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_WIPHY);
282d5590 5616
43245552 5617 NLA_PUT_FLAG(msg, NL80211_ATTR_SPLIT_WIPHY_DUMP);
282d5590
JM
5618 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
5619
d8e66e80
JM
5620 if (send_and_recv_msgs(drv, msg, phy_info_handler, &result) == 0) {
5621 nl80211_set_ht40_flags(drv, &result);
c30a4ab0
JB
5622 return wpa_driver_nl80211_postprocess_modes(result.modes,
5623 num_modes);
d8e66e80 5624 }
5883168a 5625 msg = NULL;
282d5590 5626 nla_put_failure:
5883168a 5627 nlmsg_free(msg);
282d5590
JM
5628 return NULL;
5629}
5630
5631
a11241fa
JB
5632static int wpa_driver_nl80211_send_mntr(struct wpa_driver_nl80211_data *drv,
5633 const void *data, size_t len,
5634 int encrypt, int noack)
2c2010ac
JM
5635{
5636 __u8 rtap_hdr[] = {
5637 0x00, 0x00, /* radiotap version */
5638 0x0e, 0x00, /* radiotap length */
5639 0x02, 0xc0, 0x00, 0x00, /* bmap: flags, tx and rx flags */
5640 IEEE80211_RADIOTAP_F_FRAG, /* F_FRAG (fragment if required) */
5641 0x00, /* padding */
5642 0x00, 0x00, /* RX and TX flags to indicate that */
5643 0x00, 0x00, /* this is the injected frame directly */
5644 };
5645 struct iovec iov[2] = {
5646 {
5647 .iov_base = &rtap_hdr,
5648 .iov_len = sizeof(rtap_hdr),
5649 },
5650 {
5651 .iov_base = (void *) data,
5652 .iov_len = len,
5653 }
5654 };
5655 struct msghdr msg = {
5656 .msg_name = NULL,
5657 .msg_namelen = 0,
5658 .msg_iov = iov,
5659 .msg_iovlen = 2,
5660 .msg_control = NULL,
5661 .msg_controllen = 0,
5662 .msg_flags = 0,
5663 };
ebbec8b2 5664 int res;
fab25336 5665 u16 txflags = 0;
2c2010ac
JM
5666
5667 if (encrypt)
5668 rtap_hdr[8] |= IEEE80211_RADIOTAP_F_WEP;
5669
866af8b6
JM
5670 if (drv->monitor_sock < 0) {
5671 wpa_printf(MSG_DEBUG, "nl80211: No monitor socket available "
5672 "for %s", __func__);
5673 return -1;
5674 }
5675
fab25336
HS
5676 if (noack)
5677 txflags |= IEEE80211_RADIOTAP_F_TX_NOACK;
9d7a63dc 5678 WPA_PUT_LE16(&rtap_hdr[12], txflags);
fab25336 5679
ebbec8b2
JM
5680 res = sendmsg(drv->monitor_sock, &msg, 0);
5681 if (res < 0) {
5682 wpa_printf(MSG_INFO, "nl80211: sendmsg: %s", strerror(errno));
5683 return -1;
5684 }
5685 return 0;
2c2010ac
JM
5686}
5687
5688
a11241fa
JB
5689static int wpa_driver_nl80211_send_frame(struct i802_bss *bss,
5690 const void *data, size_t len,
55231068
JM
5691 int encrypt, int noack,
5692 unsigned int freq, int no_cck,
5693 int offchanok, unsigned int wait_time)
a11241fa
JB
5694{
5695 struct wpa_driver_nl80211_data *drv = bss->drv;
5696 u64 cookie;
5697
55231068
JM
5698 if (freq == 0)
5699 freq = bss->freq;
5700
a11241fa
JB
5701 if (drv->use_monitor)
5702 return wpa_driver_nl80211_send_mntr(drv, data, len,
5703 encrypt, noack);
5704
55231068
JM
5705 return nl80211_send_frame_cmd(bss, freq, wait_time, data, len,
5706 &cookie, no_cck, noack, offchanok);
a11241fa
JB
5707}
5708
5709
9ebce9c5
JM
5710static int wpa_driver_nl80211_send_mlme(struct i802_bss *bss, const u8 *data,
5711 size_t data_len, int noack,
5712 unsigned int freq, int no_cck,
5713 int offchanok,
5714 unsigned int wait_time)
2c2010ac 5715{
a2e40bb6 5716 struct wpa_driver_nl80211_data *drv = bss->drv;
2c2010ac 5717 struct ieee80211_mgmt *mgmt;
7a47d567 5718 int encrypt = 1;
2c2010ac
JM
5719 u16 fc;
5720
5721 mgmt = (struct ieee80211_mgmt *) data;
5722 fc = le_to_host16(mgmt->frame_control);
5723
b1f625e0 5724 if (is_sta_interface(drv->nlmode) &&
5582a5d1
JB
5725 WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
5726 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_RESP) {
5727 /*
5728 * The use of last_mgmt_freq is a bit of a hack,
5729 * but it works due to the single-threaded nature
5730 * of wpa_supplicant.
5731 */
55231068
JM
5732 if (freq == 0)
5733 freq = drv->last_mgmt_freq;
5734 return nl80211_send_frame_cmd(bss, freq, 0,
88df0ef7
JB
5735 data, data_len, NULL, 1, noack,
5736 1);
5582a5d1
JB
5737 }
5738
61cbe2ff 5739 if (drv->device_ap_sme && is_ap_interface(drv->nlmode)) {
55231068
JM
5740 if (freq == 0)
5741 freq = bss->freq;
b5671498
JM
5742 return nl80211_send_frame_cmd(bss, freq,
5743 (int) freq == bss->freq ? 0 :
5744 wait_time,
d8d6b32e
DG
5745 data, data_len,
5746 &drv->send_action_cookie,
55231068 5747 no_cck, noack, offchanok);
86957e62
JM
5748 }
5749
2c2010ac
JM
5750 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
5751 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) {
5752 /*
5753 * Only one of the authentication frame types is encrypted.
5754 * In order for static WEP encryption to work properly (i.e.,
5755 * to not encrypt the frame), we need to tell mac80211 about
5756 * the frames that must not be encrypted.
5757 */
5758 u16 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
5759 u16 auth_trans = le_to_host16(mgmt->u.auth.auth_transaction);
7a47d567
JB
5760 if (auth_alg != WLAN_AUTH_SHARED_KEY || auth_trans != 3)
5761 encrypt = 0;
2c2010ac
JM
5762 }
5763
a11241fa 5764 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt,
55231068
JM
5765 noack, freq, no_cck, offchanok,
5766 wait_time);
5767}
5768
5769
31357268 5770static int nl80211_set_bss(struct i802_bss *bss, int cts, int preamble,
e5693c47
JM
5771 int slot, int ht_opmode, int ap_isolate,
5772 int *basic_rates)
31357268
JM
5773{
5774 struct wpa_driver_nl80211_data *drv = bss->drv;
5775 struct nl_msg *msg;
5776
5777 msg = nlmsg_alloc();
5778 if (!msg)
5779 return -ENOMEM;
5780
9fb04070 5781 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_BSS);
31357268
JM
5782
5783 if (cts >= 0)
5784 NLA_PUT_U8(msg, NL80211_ATTR_BSS_CTS_PROT, cts);
5785 if (preamble >= 0)
5786 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble);
5787 if (slot >= 0)
5788 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot);
5789 if (ht_opmode >= 0)
5790 NLA_PUT_U16(msg, NL80211_ATTR_BSS_HT_OPMODE, ht_opmode);
d03e8d11
JM
5791 if (ap_isolate >= 0)
5792 NLA_PUT_U8(msg, NL80211_ATTR_AP_ISOLATE, ap_isolate);
e5693c47
JM
5793
5794 if (basic_rates) {
5795 u8 rates[NL80211_MAX_SUPP_RATES];
5796 u8 rates_len = 0;
5797 int i;
5798
5799 for (i = 0; i < NL80211_MAX_SUPP_RATES && basic_rates[i] >= 0;
5800 i++)
5801 rates[rates_len++] = basic_rates[i] / 5;
5802
5803 NLA_PUT(msg, NL80211_ATTR_BSS_BASIC_RATES, rates_len, rates);
5804 }
5805
31357268
JM
5806 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
5807
5808 return send_and_recv_msgs(drv, msg, NULL, NULL);
5809 nla_put_failure:
5883168a 5810 nlmsg_free(msg);
31357268
JM
5811 return -ENOBUFS;
5812}
5813
5814
19c3b566
JM
5815static int wpa_driver_nl80211_set_ap(void *priv,
5816 struct wpa_driver_ap_params *params)
d2440ba0 5817{
a2e40bb6
FF
5818 struct i802_bss *bss = priv;
5819 struct wpa_driver_nl80211_data *drv = bss->drv;
d2440ba0
JM
5820 struct nl_msg *msg;
5821 u8 cmd = NL80211_CMD_NEW_BEACON;
5822 int ret;
b4fd6fab 5823 int beacon_set;
8b897f5a 5824 int ifindex = if_nametoindex(bss->ifname);
b11d1d64
JM
5825 int num_suites;
5826 u32 suites[10];
5827 u32 ver;
b4fd6fab 5828
b4fd6fab 5829 beacon_set = bss->beacon_set;
d2440ba0
JM
5830
5831 msg = nlmsg_alloc();
5832 if (!msg)
5833 return -ENOMEM;
5834
5835 wpa_printf(MSG_DEBUG, "nl80211: Set beacon (beacon_set=%d)",
b4fd6fab
JM
5836 beacon_set);
5837 if (beacon_set)
d2440ba0
JM
5838 cmd = NL80211_CMD_SET_BEACON;
5839
9fb04070 5840 nl80211_cmd(drv, msg, 0, cmd);
b92e08fc
JM
5841 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon head",
5842 params->head, params->head_len);
19c3b566 5843 NLA_PUT(msg, NL80211_ATTR_BEACON_HEAD, params->head_len, params->head);
b92e08fc
JM
5844 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon tail",
5845 params->tail, params->tail_len);
19c3b566 5846 NLA_PUT(msg, NL80211_ATTR_BEACON_TAIL, params->tail_len, params->tail);
b92e08fc 5847 wpa_printf(MSG_DEBUG, "nl80211: ifindex=%d", ifindex);
b4fd6fab 5848 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
b92e08fc 5849 wpa_printf(MSG_DEBUG, "nl80211: beacon_int=%d", params->beacon_int);
19c3b566 5850 NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL, params->beacon_int);
b92e08fc 5851 wpa_printf(MSG_DEBUG, "nl80211: dtim_period=%d", params->dtim_period);
19c3b566 5852 NLA_PUT_U32(msg, NL80211_ATTR_DTIM_PERIOD, params->dtim_period);
b92e08fc
JM
5853 wpa_hexdump_ascii(MSG_DEBUG, "nl80211: ssid",
5854 params->ssid, params->ssid_len);
ccb941e6
JM
5855 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
5856 params->ssid);
b92e08fc
JM
5857 if (params->proberesp && params->proberesp_len) {
5858 wpa_hexdump(MSG_DEBUG, "nl80211: proberesp (offload)",
5859 params->proberesp, params->proberesp_len);
5ed33546
AN
5860 NLA_PUT(msg, NL80211_ATTR_PROBE_RESP, params->proberesp_len,
5861 params->proberesp);
b92e08fc 5862 }
97a7a0b5
JM
5863 switch (params->hide_ssid) {
5864 case NO_SSID_HIDING:
b92e08fc 5865 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID not in use");
97a7a0b5
JM
5866 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
5867 NL80211_HIDDEN_SSID_NOT_IN_USE);
5868 break;
5869 case HIDDEN_SSID_ZERO_LEN:
b92e08fc 5870 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero len");
97a7a0b5
JM
5871 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
5872 NL80211_HIDDEN_SSID_ZERO_LEN);
5873 break;
5874 case HIDDEN_SSID_ZERO_CONTENTS:
b92e08fc 5875 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero contents");
97a7a0b5
JM
5876 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
5877 NL80211_HIDDEN_SSID_ZERO_CONTENTS);
5878 break;
5879 }
b92e08fc 5880 wpa_printf(MSG_DEBUG, "nl80211: privacy=%d", params->privacy);
b11d1d64
JM
5881 if (params->privacy)
5882 NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
b92e08fc 5883 wpa_printf(MSG_DEBUG, "nl80211: auth_algs=0x%x", params->auth_algs);
b11d1d64
JM
5884 if ((params->auth_algs & (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) ==
5885 (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) {
5886 /* Leave out the attribute */
5887 } else if (params->auth_algs & WPA_AUTH_ALG_SHARED)
5888 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE,
5889 NL80211_AUTHTYPE_SHARED_KEY);
5890 else
5891 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE,
5892 NL80211_AUTHTYPE_OPEN_SYSTEM);
5893
b92e08fc 5894 wpa_printf(MSG_DEBUG, "nl80211: wpa_version=0x%x", params->wpa_version);
b11d1d64
JM
5895 ver = 0;
5896 if (params->wpa_version & WPA_PROTO_WPA)
5897 ver |= NL80211_WPA_VERSION_1;
5898 if (params->wpa_version & WPA_PROTO_RSN)
5899 ver |= NL80211_WPA_VERSION_2;
5900 if (ver)
5901 NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver);
5902
b92e08fc
JM
5903 wpa_printf(MSG_DEBUG, "nl80211: key_mgmt_suites=0x%x",
5904 params->key_mgmt_suites);
b11d1d64
JM
5905 num_suites = 0;
5906 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X)
5907 suites[num_suites++] = WLAN_AKM_SUITE_8021X;
5908 if (params->key_mgmt_suites & WPA_KEY_MGMT_PSK)
5909 suites[num_suites++] = WLAN_AKM_SUITE_PSK;
5910 if (num_suites) {
5911 NLA_PUT(msg, NL80211_ATTR_AKM_SUITES,
5912 num_suites * sizeof(u32), suites);
5913 }
5914
9f12614b
JB
5915 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X &&
5916 params->pairwise_ciphers & (WPA_CIPHER_WEP104 | WPA_CIPHER_WEP40))
5917 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT);
5918
b92e08fc
JM
5919 wpa_printf(MSG_DEBUG, "nl80211: pairwise_ciphers=0x%x",
5920 params->pairwise_ciphers);
b11d1d64
JM
5921 num_suites = 0;
5922 if (params->pairwise_ciphers & WPA_CIPHER_CCMP)
5923 suites[num_suites++] = WLAN_CIPHER_SUITE_CCMP;
eb7719ff
JM
5924 if (params->pairwise_ciphers & WPA_CIPHER_GCMP)
5925 suites[num_suites++] = WLAN_CIPHER_SUITE_GCMP;
b11d1d64
JM
5926 if (params->pairwise_ciphers & WPA_CIPHER_TKIP)
5927 suites[num_suites++] = WLAN_CIPHER_SUITE_TKIP;
5928 if (params->pairwise_ciphers & WPA_CIPHER_WEP104)
5929 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP104;
5930 if (params->pairwise_ciphers & WPA_CIPHER_WEP40)
5931 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP40;
5932 if (num_suites) {
5933 NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
5934 num_suites * sizeof(u32), suites);
5935 }
5936
b92e08fc
JM
5937 wpa_printf(MSG_DEBUG, "nl80211: group_cipher=0x%x",
5938 params->group_cipher);
b11d1d64
JM
5939 switch (params->group_cipher) {
5940 case WPA_CIPHER_CCMP:
5941 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
5942 WLAN_CIPHER_SUITE_CCMP);
5943 break;
eb7719ff
JM
5944 case WPA_CIPHER_GCMP:
5945 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
5946 WLAN_CIPHER_SUITE_GCMP);
5947 break;
b11d1d64
JM
5948 case WPA_CIPHER_TKIP:
5949 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
5950 WLAN_CIPHER_SUITE_TKIP);
5951 break;
5952 case WPA_CIPHER_WEP104:
5953 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
5954 WLAN_CIPHER_SUITE_WEP104);
5955 break;
5956 case WPA_CIPHER_WEP40:
5957 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
5958 WLAN_CIPHER_SUITE_WEP40);
5959 break;
5960 }
d2440ba0 5961
fb91db56 5962 if (params->beacon_ies) {
b92e08fc
JM
5963 wpa_hexdump_buf(MSG_DEBUG, "nl80211: beacon_ies",
5964 params->beacon_ies);
fb91db56
JM
5965 NLA_PUT(msg, NL80211_ATTR_IE, wpabuf_len(params->beacon_ies),
5966 wpabuf_head(params->beacon_ies));
5967 }
5968 if (params->proberesp_ies) {
b92e08fc
JM
5969 wpa_hexdump_buf(MSG_DEBUG, "nl80211: proberesp_ies",
5970 params->proberesp_ies);
fb91db56
JM
5971 NLA_PUT(msg, NL80211_ATTR_IE_PROBE_RESP,
5972 wpabuf_len(params->proberesp_ies),
5973 wpabuf_head(params->proberesp_ies));
5974 }
5975 if (params->assocresp_ies) {
b92e08fc
JM
5976 wpa_hexdump_buf(MSG_DEBUG, "nl80211: assocresp_ies",
5977 params->assocresp_ies);
fb91db56
JM
5978 NLA_PUT(msg, NL80211_ATTR_IE_ASSOC_RESP,
5979 wpabuf_len(params->assocresp_ies),
5980 wpabuf_head(params->assocresp_ies));
5981 }
5982
a0133ee1 5983 if (drv->capa.flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER) {
b92e08fc
JM
5984 wpa_printf(MSG_DEBUG, "nl80211: ap_max_inactivity=%d",
5985 params->ap_max_inactivity);
a0133ee1
VT
5986 NLA_PUT_U16(msg, NL80211_ATTR_INACTIVITY_TIMEOUT,
5987 params->ap_max_inactivity);
5988 }
5989
d2440ba0
JM
5990 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5991 if (ret) {
5992 wpa_printf(MSG_DEBUG, "nl80211: Beacon set failed: %d (%s)",
5993 ret, strerror(-ret));
b4fd6fab 5994 } else {
b4fd6fab 5995 bss->beacon_set = 1;
31357268 5996 nl80211_set_bss(bss, params->cts_protect, params->preamble,
d03e8d11 5997 params->short_slot_time, params->ht_opmode,
e5693c47 5998 params->isolate, params->basic_rates);
b4fd6fab 5999 }
d2440ba0
JM
6000 return ret;
6001 nla_put_failure:
5883168a 6002 nlmsg_free(msg);
d2440ba0
JM
6003 return -ENOBUFS;
6004}
6005
6006
e4fb2167 6007static int wpa_driver_nl80211_set_freq(struct i802_bss *bss,
89b800d7 6008 struct hostapd_freq_params *freq)
1581b38b 6009{
e4fb2167 6010 struct wpa_driver_nl80211_data *drv = bss->drv;
1581b38b 6011 struct nl_msg *msg;
d2440ba0 6012 int ret;
1581b38b 6013
89b800d7
JB
6014 wpa_printf(MSG_DEBUG, "nl80211: Set freq %d (ht_enabled=%d, vht_enabled=%d,"
6015 " bandwidth=%d MHz, cf1=%d MHz, cf2=%d MHz)",
6016 freq->freq, freq->ht_enabled, freq->vht_enabled,
6017 freq->bandwidth, freq->center_freq1, freq->center_freq2);
1581b38b
JM
6018 msg = nlmsg_alloc();
6019 if (!msg)
6020 return -1;
6021
9fb04070 6022 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
1581b38b
JM
6023
6024 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
89b800d7
JB
6025 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq->freq);
6026 if (freq->vht_enabled) {
6027 switch (freq->bandwidth) {
6028 case 20:
6029 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
6030 NL80211_CHAN_WIDTH_20);
6031 break;
6032 case 40:
6033 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
6034 NL80211_CHAN_WIDTH_40);
6035 break;
6036 case 80:
6037 if (freq->center_freq2)
6038 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
6039 NL80211_CHAN_WIDTH_80P80);
6040 else
6041 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
6042 NL80211_CHAN_WIDTH_80);
6043 break;
6044 case 160:
6045 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
6046 NL80211_CHAN_WIDTH_160);
6047 break;
6048 default:
6049 return -1;
6050 }
6051 NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ1, freq->center_freq1);
6052 if (freq->center_freq2)
6053 NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ2,
6054 freq->center_freq2);
6055 } else if (freq->ht_enabled) {
6056 switch (freq->sec_channel_offset) {
f019981a
JM
6057 case -1:
6058 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
6059 NL80211_CHAN_HT40MINUS);
6060 break;
6061 case 1:
6062 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
6063 NL80211_CHAN_HT40PLUS);
6064 break;
6065 default:
6066 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
6067 NL80211_CHAN_HT20);
6068 break;
6069 }
6070 }
1581b38b 6071
d2440ba0 6072 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5883168a 6073 msg = NULL;
e4fb2167 6074 if (ret == 0) {
89b800d7 6075 bss->freq = freq->freq;
1581b38b 6076 return 0;
e4fb2167 6077 }
f019981a 6078 wpa_printf(MSG_DEBUG, "nl80211: Failed to set channel (freq=%d): "
89b800d7 6079 "%d (%s)", freq->freq, ret, strerror(-ret));
1581b38b 6080nla_put_failure:
5883168a 6081 nlmsg_free(msg);
1581b38b
JM
6082 return -1;
6083}
6084
0f4e8b4f 6085
95ab6063
AN
6086static u32 sta_flags_nl80211(int flags)
6087{
6088 u32 f = 0;
6089
6090 if (flags & WPA_STA_AUTHORIZED)
6091 f |= BIT(NL80211_STA_FLAG_AUTHORIZED);
6092 if (flags & WPA_STA_WMM)
6093 f |= BIT(NL80211_STA_FLAG_WME);
6094 if (flags & WPA_STA_SHORT_PREAMBLE)
6095 f |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
6096 if (flags & WPA_STA_MFP)
6097 f |= BIT(NL80211_STA_FLAG_MFP);
45b722f1
AN
6098 if (flags & WPA_STA_TDLS_PEER)
6099 f |= BIT(NL80211_STA_FLAG_TDLS_PEER);
95ab6063
AN
6100
6101 return f;
6102}
6103
6104
62847751 6105static int wpa_driver_nl80211_sta_add(void *priv,
0f4e8b4f
JM
6106 struct hostapd_sta_add_params *params)
6107{
a2e40bb6
FF
6108 struct i802_bss *bss = priv;
6109 struct wpa_driver_nl80211_data *drv = bss->drv;
8970bae8 6110 struct nl_msg *msg;
95ab6063 6111 struct nl80211_sta_flag_update upd;
0f4e8b4f
JM
6112 int ret = -ENOBUFS;
6113
45b722f1
AN
6114 if ((params->flags & WPA_STA_TDLS_PEER) &&
6115 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
6116 return -EOPNOTSUPP;
6117
0f4e8b4f
JM
6118 msg = nlmsg_alloc();
6119 if (!msg)
6120 return -ENOMEM;
6121
e4dea253
JM
6122 wpa_printf(MSG_DEBUG, "nl80211: %s STA " MACSTR,
6123 params->set ? "Set" : "Add", MAC2STR(params->addr));
45b722f1
AN
6124 nl80211_cmd(drv, msg, 0, params->set ? NL80211_CMD_SET_STATION :
6125 NL80211_CMD_NEW_STATION);
0f4e8b4f 6126
62847751 6127 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
0f4e8b4f 6128 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr);
0f4e8b4f
JM
6129 NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_RATES, params->supp_rates_len,
6130 params->supp_rates);
e4dea253
JM
6131 wpa_hexdump(MSG_DEBUG, " * supported rates", params->supp_rates,
6132 params->supp_rates_len);
45b722f1 6133 if (!params->set) {
f11b72c3
JM
6134 if (params->aid) {
6135 wpa_printf(MSG_DEBUG, " * aid=%u", params->aid);
6136 NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, params->aid);
6137 } else {
6138 /*
6139 * cfg80211 validates that AID is non-zero, so we have
6140 * to make this a non-zero value for the TDLS case where
6141 * a dummy STA entry is used for now.
6142 */
6143 wpa_printf(MSG_DEBUG, " * aid=1 (TDLS workaround)");
6144 NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, 1);
6145 }
e4dea253
JM
6146 wpa_printf(MSG_DEBUG, " * listen_interval=%u",
6147 params->listen_interval);
45b722f1
AN
6148 NLA_PUT_U16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
6149 params->listen_interval);
6150 }
0f4e8b4f 6151 if (params->ht_capabilities) {
e4dea253
JM
6152 wpa_hexdump(MSG_DEBUG, " * ht_capabilities",
6153 (u8 *) params->ht_capabilities,
6154 sizeof(*params->ht_capabilities));
0f4e8b4f 6155 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY,
fc4e2d95
JM
6156 sizeof(*params->ht_capabilities),
6157 params->ht_capabilities);
0f4e8b4f 6158 }
0f4e8b4f 6159
05a8d422 6160 if (params->vht_capabilities) {
e4dea253
JM
6161 wpa_hexdump(MSG_DEBUG, " * vht_capabilities",
6162 (u8 *) params->vht_capabilities,
6163 sizeof(*params->vht_capabilities));
05a8d422
JB
6164 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY,
6165 sizeof(*params->vht_capabilities),
6166 params->vht_capabilities);
6167 }
6168
122d16f2
SD
6169 wpa_printf(MSG_DEBUG, " * capability=0x%x", params->capability);
6170 NLA_PUT_U16(msg, NL80211_ATTR_STA_CAPABILITY, params->capability);
6171
6172 if (params->ext_capab) {
6173 wpa_hexdump(MSG_DEBUG, " * ext_capab",
6174 params->ext_capab, params->ext_capab_len);
6175 NLA_PUT(msg, NL80211_ATTR_STA_EXT_CAPABILITY,
6176 params->ext_capab_len, params->ext_capab);
6177 }
6178
95ab6063
AN
6179 os_memset(&upd, 0, sizeof(upd));
6180 upd.mask = sta_flags_nl80211(params->flags);
6181 upd.set = upd.mask;
e4dea253
JM
6182 wpa_printf(MSG_DEBUG, " * flags set=0x%x mask=0x%x",
6183 upd.set, upd.mask);
95ab6063
AN
6184 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
6185
774bfa62 6186 if (params->flags & WPA_STA_WMM) {
8970bae8
JB
6187 struct nlattr *wme = nla_nest_start(msg, NL80211_ATTR_STA_WME);
6188
774bfa62
EP
6189 if (!wme)
6190 goto nla_put_failure;
6191
e4dea253 6192 wpa_printf(MSG_DEBUG, " * qosinfo=0x%x", params->qosinfo);
8970bae8 6193 NLA_PUT_U8(msg, NL80211_STA_WME_UAPSD_QUEUES,
5d061637 6194 params->qosinfo & WMM_QOSINFO_STA_AC_MASK);
8970bae8 6195 NLA_PUT_U8(msg, NL80211_STA_WME_MAX_SP,
bdaf1748 6196 (params->qosinfo >> WMM_QOSINFO_STA_SP_SHIFT) &
5d061637 6197 WMM_QOSINFO_STA_SP_MASK);
8970bae8 6198 nla_nest_end(msg, wme);
774bfa62
EP
6199 }
6200
0f4e8b4f 6201 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5883168a 6202 msg = NULL;
0f4e8b4f 6203 if (ret)
45b722f1
AN
6204 wpa_printf(MSG_DEBUG, "nl80211: NL80211_CMD_%s_STATION "
6205 "result: %d (%s)", params->set ? "SET" : "NEW", ret,
6206 strerror(-ret));
0f4e8b4f
JM
6207 if (ret == -EEXIST)
6208 ret = 0;
6209 nla_put_failure:
5883168a 6210 nlmsg_free(msg);
0f4e8b4f
JM
6211 return ret;
6212}
6213
6214
9ebce9c5 6215static int wpa_driver_nl80211_sta_remove(struct i802_bss *bss, const u8 *addr)
0f4e8b4f 6216{
a2e40bb6 6217 struct wpa_driver_nl80211_data *drv = bss->drv;
0f4e8b4f
JM
6218 struct nl_msg *msg;
6219 int ret;
6220
6221 msg = nlmsg_alloc();
6222 if (!msg)
6223 return -ENOMEM;
6224
9fb04070 6225 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_STATION);
0f4e8b4f
JM
6226
6227 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
a2e40bb6 6228 if_nametoindex(bss->ifname));
0f4e8b4f
JM
6229 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
6230
6231 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6232 if (ret == -ENOENT)
6233 return 0;
6234 return ret;
6235 nla_put_failure:
5883168a 6236 nlmsg_free(msg);
0f4e8b4f
JM
6237 return -ENOBUFS;
6238}
6239
1581b38b 6240
0915d02c
JM
6241static void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv,
6242 int ifidx)
6243{
6244 struct nl_msg *msg;
6245
c6e8e8e4
JM
6246 wpa_printf(MSG_DEBUG, "nl80211: Remove interface ifindex=%d", ifidx);
6247
2135f224
JM
6248 /* stop listening for EAPOL on this interface */
6249 del_ifidx(drv, ifidx);
2135f224 6250
0915d02c
JM
6251 msg = nlmsg_alloc();
6252 if (!msg)
6253 goto nla_put_failure;
6254
9fb04070 6255 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_INTERFACE);
0915d02c
JM
6256 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifidx);
6257
6258 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
6259 return;
5883168a 6260 msg = NULL;
0915d02c 6261 nla_put_failure:
5883168a 6262 nlmsg_free(msg);
e748062b 6263 wpa_printf(MSG_ERROR, "Failed to remove interface (ifidx=%d)", ifidx);
0915d02c
JM
6264}
6265
6266
a1922f93
JM
6267static const char * nl80211_iftype_str(enum nl80211_iftype mode)
6268{
6269 switch (mode) {
6270 case NL80211_IFTYPE_ADHOC:
6271 return "ADHOC";
6272 case NL80211_IFTYPE_STATION:
6273 return "STATION";
6274 case NL80211_IFTYPE_AP:
6275 return "AP";
6276 case NL80211_IFTYPE_MONITOR:
6277 return "MONITOR";
6278 case NL80211_IFTYPE_P2P_CLIENT:
6279 return "P2P_CLIENT";
6280 case NL80211_IFTYPE_P2P_GO:
6281 return "P2P_GO";
6282 default:
6283 return "unknown";
6284 }
6285}
6286
6287
a35187e7
KH
6288static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv,
6289 const char *ifname,
6290 enum nl80211_iftype iftype,
fbbfcbac 6291 const u8 *addr, int wds)
0915d02c 6292{
8970bae8 6293 struct nl_msg *msg;
0915d02c
JM
6294 int ifidx;
6295 int ret = -ENOBUFS;
6296
a1922f93
JM
6297 wpa_printf(MSG_DEBUG, "nl80211: Create interface iftype %d (%s)",
6298 iftype, nl80211_iftype_str(iftype));
6299
0915d02c
JM
6300 msg = nlmsg_alloc();
6301 if (!msg)
6302 return -1;
6303
9fb04070 6304 nl80211_cmd(drv, msg, 0, NL80211_CMD_NEW_INTERFACE);
0915d02c
JM
6305 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
6306 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, ifname);
6307 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, iftype);
6308
6309 if (iftype == NL80211_IFTYPE_MONITOR) {
8970bae8 6310 struct nlattr *flags;
0915d02c 6311
8970bae8 6312 flags = nla_nest_start(msg, NL80211_ATTR_MNTR_FLAGS);
0915d02c
JM
6313 if (!flags)
6314 goto nla_put_failure;
6315
8970bae8 6316 NLA_PUT_FLAG(msg, NL80211_MNTR_FLAG_COOK_FRAMES);
0915d02c 6317
8970bae8 6318 nla_nest_end(msg, flags);
fbbfcbac
FF
6319 } else if (wds) {
6320 NLA_PUT_U8(msg, NL80211_ATTR_4ADDR, wds);
0915d02c
JM
6321 }
6322
6323 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5883168a 6324 msg = NULL;
0915d02c
JM
6325 if (ret) {
6326 nla_put_failure:
5883168a 6327 nlmsg_free(msg);
a35187e7
KH
6328 wpa_printf(MSG_ERROR, "Failed to create interface %s: %d (%s)",
6329 ifname, ret, strerror(-ret));
0915d02c
JM
6330 return ret;
6331 }
6332
6333 ifidx = if_nametoindex(ifname);
c6e8e8e4
JM
6334 wpa_printf(MSG_DEBUG, "nl80211: New interface %s created: ifindex=%d",
6335 ifname, ifidx);
0915d02c
JM
6336
6337 if (ifidx <= 0)
6338 return -1;
6339
2135f224
JM
6340 /* start listening for EAPOL on this interface */
6341 add_ifidx(drv, ifidx);
6342
7bfc47c3 6343 if (addr && iftype != NL80211_IFTYPE_MONITOR &&
c81eff1a 6344 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname, addr)) {
41d931ee
JM
6345 nl80211_remove_iface(drv, ifidx);
6346 return -1;
2135f224 6347 }
2135f224 6348
0915d02c
JM
6349 return ifidx;
6350}
22a7c9d7
JM
6351
6352
a35187e7
KH
6353static int nl80211_create_iface(struct wpa_driver_nl80211_data *drv,
6354 const char *ifname, enum nl80211_iftype iftype,
fbbfcbac 6355 const u8 *addr, int wds)
a35187e7
KH
6356{
6357 int ret;
6358
fbbfcbac 6359 ret = nl80211_create_iface_once(drv, ifname, iftype, addr, wds);
a35187e7 6360
ffbf1eaa 6361 /* if error occurred and interface exists already */
a35187e7
KH
6362 if (ret == -ENFILE && if_nametoindex(ifname)) {
6363 wpa_printf(MSG_INFO, "Try to remove and re-create %s", ifname);
6364
6365 /* Try to remove the interface that was already there. */
6366 nl80211_remove_iface(drv, if_nametoindex(ifname));
6367
6368 /* Try to create the interface again */
fbbfcbac
FF
6369 ret = nl80211_create_iface_once(drv, ifname, iftype, addr,
6370 wds);
a35187e7
KH
6371 }
6372
b3af99d2 6373 if (ret >= 0 && is_p2p_interface(iftype))
4e5cb1a3
JM
6374 nl80211_disable_11b_rates(drv, ret, 1);
6375
a35187e7
KH
6376 return ret;
6377}
0915d02c 6378
2135f224 6379
0915d02c
JM
6380static void handle_tx_callback(void *ctx, u8 *buf, size_t len, int ok)
6381{
6382 struct ieee80211_hdr *hdr;
f8b1f695
JM
6383 u16 fc;
6384 union wpa_event_data event;
0915d02c
JM
6385
6386 hdr = (struct ieee80211_hdr *) buf;
6387 fc = le_to_host16(hdr->frame_control);
6388
f8b1f695
JM
6389 os_memset(&event, 0, sizeof(event));
6390 event.tx_status.type = WLAN_FC_GET_TYPE(fc);
6391 event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
6392 event.tx_status.dst = hdr->addr1;
6393 event.tx_status.data = buf;
6394 event.tx_status.data_len = len;
6395 event.tx_status.ack = ok;
6396 wpa_supplicant_event(ctx, EVENT_TX_STATUS, &event);
0915d02c
JM
6397}
6398
6399
4b9841d3 6400static void from_unknown_sta(struct wpa_driver_nl80211_data *drv,
0d9fc3d8 6401 u8 *buf, size_t len)
0915d02c 6402{
9b90955e
JB
6403 struct ieee80211_hdr *hdr = (void *)buf;
6404 u16 fc;
f8b1f695 6405 union wpa_event_data event;
9b90955e
JB
6406
6407 if (len < sizeof(*hdr))
6408 return;
6409
6410 fc = le_to_host16(hdr->frame_control);
6411
f8b1f695 6412 os_memset(&event, 0, sizeof(event));
9b90955e
JB
6413 event.rx_from_unknown.bssid = get_hdr_bssid(hdr, len);
6414 event.rx_from_unknown.addr = hdr->addr2;
6415 event.rx_from_unknown.wds = (fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) ==
6416 (WLAN_FC_FROMDS | WLAN_FC_TODS);
f8b1f695 6417 wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event);
4b9841d3 6418}
0915d02c 6419
4b9841d3
JM
6420
6421static void handle_frame(struct wpa_driver_nl80211_data *drv,
2a8b7416 6422 u8 *buf, size_t len, int datarate, int ssi_signal)
4b9841d3
JM
6423{
6424 struct ieee80211_hdr *hdr;
f8b1f695
JM
6425 u16 fc;
6426 union wpa_event_data event;
0915d02c
JM
6427
6428 hdr = (struct ieee80211_hdr *) buf;
6429 fc = le_to_host16(hdr->frame_control);
0915d02c 6430
4b9841d3 6431 switch (WLAN_FC_GET_TYPE(fc)) {
0915d02c 6432 case WLAN_FC_TYPE_MGMT:
f8b1f695
JM
6433 os_memset(&event, 0, sizeof(event));
6434 event.rx_mgmt.frame = buf;
6435 event.rx_mgmt.frame_len = len;
2a8b7416
JM
6436 event.rx_mgmt.datarate = datarate;
6437 event.rx_mgmt.ssi_signal = ssi_signal;
f8b1f695 6438 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
0915d02c
JM
6439 break;
6440 case WLAN_FC_TYPE_CTRL:
6441 /* can only get here with PS-Poll frames */
6442 wpa_printf(MSG_DEBUG, "CTRL");
0d9fc3d8 6443 from_unknown_sta(drv, buf, len);
0915d02c
JM
6444 break;
6445 case WLAN_FC_TYPE_DATA:
0d9fc3d8 6446 from_unknown_sta(drv, buf, len);
0915d02c
JM
6447 break;
6448 }
6449}
6450
6451
6452static void handle_monitor_read(int sock, void *eloop_ctx, void *sock_ctx)
6453{
6454 struct wpa_driver_nl80211_data *drv = eloop_ctx;
6455 int len;
6456 unsigned char buf[3000];
6457 struct ieee80211_radiotap_iterator iter;
6458 int ret;
2a8b7416 6459 int datarate = 0, ssi_signal = 0;
4b9841d3 6460 int injected = 0, failed = 0, rxflags = 0;
0915d02c
JM
6461
6462 len = recv(sock, buf, sizeof(buf), 0);
6463 if (len < 0) {
6464 perror("recv");
6465 return;
6466 }
6467
6468 if (ieee80211_radiotap_iterator_init(&iter, (void*)buf, len)) {
6469 printf("received invalid radiotap frame\n");
6470 return;
6471 }
6472
0915d02c
JM
6473 while (1) {
6474 ret = ieee80211_radiotap_iterator_next(&iter);
6475 if (ret == -ENOENT)
6476 break;
6477 if (ret) {
6478 printf("received invalid radiotap frame (%d)\n", ret);
6479 return;
6480 }
6481 switch (iter.this_arg_index) {
6482 case IEEE80211_RADIOTAP_FLAGS:
6483 if (*iter.this_arg & IEEE80211_RADIOTAP_F_FCS)
6484 len -= 4;
6485 break;
6486 case IEEE80211_RADIOTAP_RX_FLAGS:
6487 rxflags = 1;
6488 break;
6489 case IEEE80211_RADIOTAP_TX_FLAGS:
6490 injected = 1;
6491 failed = le_to_host16((*(uint16_t *) iter.this_arg)) &
6492 IEEE80211_RADIOTAP_F_TX_FAIL;
6493 break;
6494 case IEEE80211_RADIOTAP_DATA_RETRIES:
6495 break;
6496 case IEEE80211_RADIOTAP_CHANNEL:
2a8b7416 6497 /* TODO: convert from freq/flags to channel number */
0915d02c
JM
6498 break;
6499 case IEEE80211_RADIOTAP_RATE:
2a8b7416 6500 datarate = *iter.this_arg * 5;
0915d02c 6501 break;
baf513d6
JB
6502 case IEEE80211_RADIOTAP_DBM_ANTSIGNAL:
6503 ssi_signal = (s8) *iter.this_arg;
0915d02c
JM
6504 break;
6505 }
6506 }
6507
6508 if (rxflags && injected)
6509 return;
6510
6511 if (!injected)
4b9841d3 6512 handle_frame(drv, buf + iter.max_length,
2a8b7416 6513 len - iter.max_length, datarate, ssi_signal);
0915d02c 6514 else
4b9841d3
JM
6515 handle_tx_callback(drv->ctx, buf + iter.max_length,
6516 len - iter.max_length, !failed);
0915d02c
JM
6517}
6518
6519
6520/*
6521 * we post-process the filter code later and rewrite
6522 * this to the offset to the last instruction
6523 */
6524#define PASS 0xFF
6525#define FAIL 0xFE
6526
6527static struct sock_filter msock_filter_insns[] = {
6528 /*
6529 * do a little-endian load of the radiotap length field
6530 */
6531 /* load lower byte into A */
6532 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 2),
6533 /* put it into X (== index register) */
6534 BPF_STMT(BPF_MISC| BPF_TAX, 0),
6535 /* load upper byte into A */
6536 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 3),
6537 /* left-shift it by 8 */
6538 BPF_STMT(BPF_ALU | BPF_LSH | BPF_K, 8),
6539 /* or with X */
6540 BPF_STMT(BPF_ALU | BPF_OR | BPF_X, 0),
6541 /* put result into X */
6542 BPF_STMT(BPF_MISC| BPF_TAX, 0),
6543
6544 /*
6545 * Allow management frames through, this also gives us those
6546 * management frames that we sent ourselves with status
6547 */
6548 /* load the lower byte of the IEEE 802.11 frame control field */
6549 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
6550 /* mask off frame type and version */
6551 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0xF),
6552 /* accept frame if it's both 0, fall through otherwise */
6553 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0, PASS, 0),
6554
6555 /*
6556 * TODO: add a bit to radiotap RX flags that indicates
6557 * that the sending station is not associated, then
6558 * add a filter here that filters on our DA and that flag
6559 * to allow us to deauth frames to that bad station.
6560 *
65ae1afd 6561 * For now allow all To DS data frames through.
0915d02c 6562 */
65ae1afd
HS
6563 /* load the IEEE 802.11 frame control field */
6564 BPF_STMT(BPF_LD | BPF_H | BPF_IND, 0),
6565 /* mask off frame type, version and DS status */
6566 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0F03),
6567 /* accept frame if version 0, type 2 and To DS, fall through otherwise
6568 */
6569 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0801, PASS, 0),
0915d02c
JM
6570
6571#if 0
6572 /*
fbbfcbac 6573 * drop non-data frames
0915d02c
JM
6574 */
6575 /* load the lower byte of the frame control field */
6576 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
6577 /* mask off QoS bit */
6578 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0c),
6579 /* drop non-data frames */
6580 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 8, 0, FAIL),
fbbfcbac 6581#endif
0915d02c 6582 /* load the upper byte of the frame control field */
fbbfcbac 6583 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 1),
0915d02c
JM
6584 /* mask off toDS/fromDS */
6585 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x03),
fbbfcbac
FF
6586 /* accept WDS frames */
6587 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 3, PASS, 0),
0915d02c
JM
6588
6589 /*
6590 * add header length to index
6591 */
6592 /* load the lower byte of the frame control field */
6593 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
6594 /* mask off QoS bit */
6595 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x80),
6596 /* right shift it by 6 to give 0 or 2 */
6597 BPF_STMT(BPF_ALU | BPF_RSH | BPF_K, 6),
6598 /* add data frame header length */
6599 BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 24),
6600 /* add index, was start of 802.11 header */
6601 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
6602 /* move to index, now start of LL header */
6603 BPF_STMT(BPF_MISC | BPF_TAX, 0),
6604
6605 /*
6606 * Accept empty data frames, we use those for
6607 * polling activity.
6608 */
6609 BPF_STMT(BPF_LD | BPF_W | BPF_LEN, 0),
6610 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_X, 0, PASS, 0),
6611
6612 /*
6613 * Accept EAPOL frames
6614 */
6615 BPF_STMT(BPF_LD | BPF_W | BPF_IND, 0),
6616 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0xAAAA0300, 0, FAIL),
6617 BPF_STMT(BPF_LD | BPF_W | BPF_IND, 4),
6618 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0000888E, PASS, FAIL),
6619
6620 /* keep these last two statements or change the code below */
6621 /* return 0 == "DROP" */
6622 BPF_STMT(BPF_RET | BPF_K, 0),
6623 /* return ~0 == "keep all" */
6624 BPF_STMT(BPF_RET | BPF_K, ~0),
6625};
6626
6627static struct sock_fprog msock_filter = {
6628 .len = sizeof(msock_filter_insns)/sizeof(msock_filter_insns[0]),
6629 .filter = msock_filter_insns,
6630};
6631
6632
6633static int add_monitor_filter(int s)
6634{
6635 int idx;
6636
6637 /* rewrite all PASS/FAIL jump offsets */
6638 for (idx = 0; idx < msock_filter.len; idx++) {
6639 struct sock_filter *insn = &msock_filter_insns[idx];
6640
6641 if (BPF_CLASS(insn->code) == BPF_JMP) {
6642 if (insn->code == (BPF_JMP|BPF_JA)) {
6643 if (insn->k == PASS)
6644 insn->k = msock_filter.len - idx - 2;
6645 else if (insn->k == FAIL)
6646 insn->k = msock_filter.len - idx - 3;
6647 }
6648
6649 if (insn->jt == PASS)
6650 insn->jt = msock_filter.len - idx - 2;
6651 else if (insn->jt == FAIL)
6652 insn->jt = msock_filter.len - idx - 3;
6653
6654 if (insn->jf == PASS)
6655 insn->jf = msock_filter.len - idx - 2;
6656 else if (insn->jf == FAIL)
6657 insn->jf = msock_filter.len - idx - 3;
6658 }
6659 }
6660
6661 if (setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER,
6662 &msock_filter, sizeof(msock_filter))) {
6663 perror("SO_ATTACH_FILTER");
6664 return -1;
6665 }
6666
6667 return 0;
6668}
6669
6670
460456f8
JM
6671static void nl80211_remove_monitor_interface(
6672 struct wpa_driver_nl80211_data *drv)
6673{
3fd1cefb
JB
6674 drv->monitor_refcount--;
6675 if (drv->monitor_refcount > 0)
6676 return;
6677
460456f8
JM
6678 if (drv->monitor_ifidx >= 0) {
6679 nl80211_remove_iface(drv, drv->monitor_ifidx);
6680 drv->monitor_ifidx = -1;
6681 }
504e905c
JM
6682 if (drv->monitor_sock >= 0) {
6683 eloop_unregister_read_sock(drv->monitor_sock);
6684 close(drv->monitor_sock);
6685 drv->monitor_sock = -1;
6686 }
460456f8
JM
6687}
6688
6689
0915d02c
JM
6690static int
6691nl80211_create_monitor_interface(struct wpa_driver_nl80211_data *drv)
6692{
6693 char buf[IFNAMSIZ];
6694 struct sockaddr_ll ll;
6695 int optval;
6696 socklen_t optlen;
0915d02c 6697
3fd1cefb
JB
6698 if (drv->monitor_ifidx >= 0) {
6699 drv->monitor_refcount++;
6700 return 0;
6701 }
6702
6758b167
JJ
6703 if (os_strncmp(drv->first_bss.ifname, "p2p-", 4) == 0) {
6704 /*
6705 * P2P interface name is of the format p2p-%s-%d. For monitor
6706 * interface name corresponding to P2P GO, replace "p2p-" with
6707 * "mon-" to retain the same interface name length and to
6708 * indicate that it is a monitor interface.
6709 */
6710 snprintf(buf, IFNAMSIZ, "mon-%s", drv->first_bss.ifname + 4);
6711 } else {
6712 /* Non-P2P interface with AP functionality. */
6713 snprintf(buf, IFNAMSIZ, "mon.%s", drv->first_bss.ifname);
6714 }
6715
0915d02c
JM
6716 buf[IFNAMSIZ - 1] = '\0';
6717
6718 drv->monitor_ifidx =
fbbfcbac
FF
6719 nl80211_create_iface(drv, buf, NL80211_IFTYPE_MONITOR, NULL,
6720 0);
0915d02c 6721
866af8b6 6722 if (drv->monitor_ifidx == -EOPNOTSUPP) {
61cbe2ff
JB
6723 /*
6724 * This is backward compatibility for a few versions of
6725 * the kernel only that didn't advertise the right
6726 * attributes for the only driver that then supported
6727 * AP mode w/o monitor -- ath6kl.
6728 */
866af8b6
JM
6729 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support "
6730 "monitor interface type - try to run without it");
61cbe2ff 6731 drv->device_ap_sme = 1;
866af8b6
JM
6732 }
6733
0915d02c
JM
6734 if (drv->monitor_ifidx < 0)
6735 return -1;
6736
c81eff1a 6737 if (linux_set_iface_flags(drv->global->ioctl_sock, buf, 1))
0915d02c 6738 goto error;
0915d02c
JM
6739
6740 memset(&ll, 0, sizeof(ll));
6741 ll.sll_family = AF_PACKET;
6742 ll.sll_ifindex = drv->monitor_ifidx;
6743 drv->monitor_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
6744 if (drv->monitor_sock < 0) {
6745 perror("socket[PF_PACKET,SOCK_RAW]");
6746 goto error;
6747 }
6748
6749 if (add_monitor_filter(drv->monitor_sock)) {
6750 wpa_printf(MSG_INFO, "Failed to set socket filter for monitor "
6751 "interface; do filtering in user space");
6752 /* This works, but will cost in performance. */
6753 }
6754
2135f224 6755 if (bind(drv->monitor_sock, (struct sockaddr *) &ll, sizeof(ll)) < 0) {
0915d02c
JM
6756 perror("monitor socket bind");
6757 goto error;
6758 }
6759
6760 optlen = sizeof(optval);
6761 optval = 20;
6762 if (setsockopt
6763 (drv->monitor_sock, SOL_SOCKET, SO_PRIORITY, &optval, optlen)) {
6764 perror("Failed to set socket priority");
6765 goto error;
6766 }
6767
6768 if (eloop_register_read_sock(drv->monitor_sock, handle_monitor_read,
6769 drv, NULL)) {
6770 printf("Could not register monitor read socket\n");
6771 goto error;
6772 }
6773
6774 return 0;
6775 error:
460456f8 6776 nl80211_remove_monitor_interface(drv);
0915d02c
JM
6777 return -1;
6778}
6779
db149ac9 6780
3fd1cefb
JB
6781static int nl80211_setup_ap(struct i802_bss *bss)
6782{
6783 struct wpa_driver_nl80211_data *drv = bss->drv;
6784
36488c05
JM
6785 wpa_printf(MSG_DEBUG, "nl80211: Setup AP - device_ap_sme=%d "
6786 "use_monitor=%d", drv->device_ap_sme, drv->use_monitor);
6787
a11241fa
JB
6788 /*
6789 * Disable Probe Request reporting unless we need it in this way for
6790 * devices that include the AP SME, in the other case (unless using
6791 * monitor iface) we'll get it through the nl_mgmt socket instead.
6792 */
6793 if (!drv->device_ap_sme)
6794 wpa_driver_nl80211_probe_req_report(bss, 0);
6795
6796 if (!drv->device_ap_sme && !drv->use_monitor)
6797 if (nl80211_mgmt_subscribe_ap(bss))
6798 return -1;
6799
a6cc0602
JM
6800 if (drv->device_ap_sme && !drv->use_monitor)
6801 if (nl80211_mgmt_subscribe_ap_dev_sme(bss))
6802 return -1;
6803
a11241fa 6804 if (!drv->device_ap_sme && drv->use_monitor &&
3fd1cefb
JB
6805 nl80211_create_monitor_interface(drv) &&
6806 !drv->device_ap_sme)
6807 return -1;
6808
6809 if (drv->device_ap_sme &&
6810 wpa_driver_nl80211_probe_req_report(bss, 1) < 0) {
6811 wpa_printf(MSG_DEBUG, "nl80211: Failed to enable "
6812 "Probe Request frame reporting in AP mode");
6813 /* Try to survive without this */
6814 }
6815
6816 return 0;
6817}
6818
6819
6820static void nl80211_teardown_ap(struct i802_bss *bss)
6821{
6822 struct wpa_driver_nl80211_data *drv = bss->drv;
6823
a6cc0602 6824 if (drv->device_ap_sme) {
3fd1cefb 6825 wpa_driver_nl80211_probe_req_report(bss, 0);
a6cc0602
JM
6826 if (!drv->use_monitor)
6827 nl80211_mgmt_unsubscribe(bss, "AP teardown (dev SME)");
6828 } else if (drv->use_monitor)
3fd1cefb 6829 nl80211_remove_monitor_interface(drv);
a11241fa 6830 else
36488c05 6831 nl80211_mgmt_unsubscribe(bss, "AP teardown");
a11241fa 6832
3fd1cefb
JB
6833 bss->beacon_set = 0;
6834}
6835
6836
f10bfc9a
JM
6837static int nl80211_send_eapol_data(struct i802_bss *bss,
6838 const u8 *addr, const u8 *data,
d12dab4c 6839 size_t data_len)
f10bfc9a 6840{
d12dab4c
JB
6841 struct sockaddr_ll ll;
6842 int ret;
6843
6844 if (bss->drv->eapol_tx_sock < 0) {
6845 wpa_printf(MSG_DEBUG, "nl80211: No socket to send EAPOL");
f10bfc9a
JM
6846 return -1;
6847 }
6848
d12dab4c
JB
6849 os_memset(&ll, 0, sizeof(ll));
6850 ll.sll_family = AF_PACKET;
6851 ll.sll_ifindex = bss->ifindex;
6852 ll.sll_protocol = htons(ETH_P_PAE);
6853 ll.sll_halen = ETH_ALEN;
6854 os_memcpy(ll.sll_addr, addr, ETH_ALEN);
6855 ret = sendto(bss->drv->eapol_tx_sock, data, data_len, 0,
6856 (struct sockaddr *) &ll, sizeof(ll));
6857 if (ret < 0)
6858 wpa_printf(MSG_ERROR, "nl80211: EAPOL TX: %s",
6859 strerror(errno));
6860
6861 return ret;
f10bfc9a 6862}
5fb1a232 6863
f10bfc9a 6864
db149ac9
JM
6865static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
6866
6867static int wpa_driver_nl80211_hapd_send_eapol(
6868 void *priv, const u8 *addr, const u8 *data,
4378fc14 6869 size_t data_len, int encrypt, const u8 *own_addr, u32 flags)
db149ac9 6870{
a2e40bb6
FF
6871 struct i802_bss *bss = priv;
6872 struct wpa_driver_nl80211_data *drv = bss->drv;
db149ac9
JM
6873 struct ieee80211_hdr *hdr;
6874 size_t len;
6875 u8 *pos;
6876 int res;
4378fc14 6877 int qos = flags & WPA_STA_WMM;
db149ac9 6878
a11241fa 6879 if (drv->device_ap_sme || !drv->use_monitor)
d12dab4c 6880 return nl80211_send_eapol_data(bss, addr, data, data_len);
f10bfc9a 6881
db149ac9
JM
6882 len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
6883 data_len;
6884 hdr = os_zalloc(len);
6885 if (hdr == NULL) {
6886 printf("malloc() failed for i802_send_data(len=%lu)\n",
6887 (unsigned long) len);
6888 return -1;
6889 }
6890
6891 hdr->frame_control =
6892 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
6893 hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
6894 if (encrypt)
6895 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
db149ac9
JM
6896 if (qos) {
6897 hdr->frame_control |=
6898 host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
6899 }
db149ac9
JM
6900
6901 memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
6902 memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
6903 memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
6904 pos = (u8 *) (hdr + 1);
6905
db149ac9 6906 if (qos) {
92d521d8
FF
6907 /* Set highest priority in QoS header */
6908 pos[0] = 7;
db149ac9
JM
6909 pos[1] = 0;
6910 pos += 2;
6911 }
db149ac9
JM
6912
6913 memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
6914 pos += sizeof(rfc1042_header);
6915 WPA_PUT_BE16(pos, ETH_P_PAE);
6916 pos += 2;
6917 memcpy(pos, data, data_len);
6918
55231068
JM
6919 res = wpa_driver_nl80211_send_frame(bss, (u8 *) hdr, len, encrypt, 0,
6920 0, 0, 0, 0);
db149ac9
JM
6921 if (res < 0) {
6922 wpa_printf(MSG_ERROR, "i802_send_eapol - packet len: %lu - "
6923 "failed: %d (%s)",
6924 (unsigned long) len, errno, strerror(errno));
6925 }
7bf12757 6926 os_free(hdr);
db149ac9
JM
6927
6928 return res;
6929}
6930
a8d6ffa4 6931
3234cba4
JM
6932static int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr,
6933 int total_flags,
4c32757d 6934 int flags_or, int flags_and)
a8d6ffa4 6935{
a2e40bb6
FF
6936 struct i802_bss *bss = priv;
6937 struct wpa_driver_nl80211_data *drv = bss->drv;
8970bae8
JB
6938 struct nl_msg *msg;
6939 struct nlattr *flags;
7e76ee9c 6940 struct nl80211_sta_flag_update upd;
a8d6ffa4
JM
6941
6942 msg = nlmsg_alloc();
6943 if (!msg)
6944 return -ENOMEM;
6945
9fb04070 6946 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
a8d6ffa4
JM
6947
6948 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
3234cba4 6949 if_nametoindex(bss->ifname));
a8d6ffa4
JM
6950 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
6951
7e76ee9c
JM
6952 /*
6953 * Backwards compatibility version using NL80211_ATTR_STA_FLAGS. This
6954 * can be removed eventually.
6955 */
8970bae8
JB
6956 flags = nla_nest_start(msg, NL80211_ATTR_STA_FLAGS);
6957 if (!flags)
6958 goto nla_put_failure;
0de39516 6959 if (total_flags & WPA_STA_AUTHORIZED)
8970bae8 6960 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_AUTHORIZED);
a8d6ffa4 6961
0de39516 6962 if (total_flags & WPA_STA_WMM)
8970bae8 6963 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_WME);
a8d6ffa4 6964
0de39516 6965 if (total_flags & WPA_STA_SHORT_PREAMBLE)
8970bae8 6966 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_SHORT_PREAMBLE);
a8d6ffa4 6967
0de39516 6968 if (total_flags & WPA_STA_MFP)
8970bae8 6969 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_MFP);
a8d6ffa4 6970
45b722f1 6971 if (total_flags & WPA_STA_TDLS_PEER)
8970bae8 6972 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_TDLS_PEER);
45b722f1 6973
8970bae8 6974 nla_nest_end(msg, flags);
a8d6ffa4 6975
7e76ee9c
JM
6976 os_memset(&upd, 0, sizeof(upd));
6977 upd.mask = sta_flags_nl80211(flags_or | ~flags_and);
6978 upd.set = sta_flags_nl80211(flags_or);
6979 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
6980
a8d6ffa4
JM
6981 return send_and_recv_msgs(drv, msg, NULL, NULL);
6982 nla_put_failure:
5883168a 6983 nlmsg_free(msg);
a8d6ffa4
JM
6984 return -ENOBUFS;
6985}
6986
0915d02c 6987
1581b38b
JM
6988static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv,
6989 struct wpa_driver_associate_params *params)
6990{
708bc8e0 6991 enum nl80211_iftype nlmode, old_mode;
89b800d7
JB
6992 struct hostapd_freq_params freq = {
6993 .freq = params->freq,
6994 };
b1f625e0
EP
6995
6996 if (params->p2p) {
046b26a2
JM
6997 wpa_printf(MSG_DEBUG, "nl80211: Setup AP operations for P2P "
6998 "group (GO)");
b1f625e0
EP
6999 nlmode = NL80211_IFTYPE_P2P_GO;
7000 } else
7001 nlmode = NL80211_IFTYPE_AP;
7002
708bc8e0
JM
7003 old_mode = drv->nlmode;
7004 if (wpa_driver_nl80211_set_mode(&drv->first_bss, nlmode)) {
7005 nl80211_remove_monitor_interface(drv);
7006 return -1;
7007 }
7008
89b800d7 7009 if (wpa_driver_nl80211_set_freq(&drv->first_bss, &freq)) {
708bc8e0
JM
7010 if (old_mode != nlmode)
7011 wpa_driver_nl80211_set_mode(&drv->first_bss, old_mode);
460456f8 7012 nl80211_remove_monitor_interface(drv);
1581b38b 7013 return -1;
0915d02c 7014 }
1581b38b 7015
1581b38b
JM
7016 return 0;
7017}
1581b38b
JM
7018
7019
5cc4d64b
JM
7020static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv)
7021{
7022 struct nl_msg *msg;
7023 int ret = -1;
7024
7025 msg = nlmsg_alloc();
7026 if (!msg)
7027 return -1;
7028
9fb04070 7029 nl80211_cmd(drv, msg, 0, NL80211_CMD_LEAVE_IBSS);
5cc4d64b
JM
7030 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
7031 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7032 msg = NULL;
7033 if (ret) {
7034 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS failed: ret=%d "
7035 "(%s)", ret, strerror(-ret));
7036 goto nla_put_failure;
7037 }
7038
7039 ret = 0;
7040 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS request sent successfully");
7041
7042nla_put_failure:
7043 nlmsg_free(msg);
7044 return ret;
7045}
7046
7047
7048static int wpa_driver_nl80211_ibss(struct wpa_driver_nl80211_data *drv,
7049 struct wpa_driver_associate_params *params)
7050{
7051 struct nl_msg *msg;
7052 int ret = -1;
7053 int count = 0;
7054
7055 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS (ifindex=%d)", drv->ifindex);
7056
b1f625e0
EP
7057 if (wpa_driver_nl80211_set_mode(&drv->first_bss,
7058 NL80211_IFTYPE_ADHOC)) {
5cc4d64b
JM
7059 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
7060 "IBSS mode");
7061 return -1;
7062 }
7063
7064retry:
7065 msg = nlmsg_alloc();
7066 if (!msg)
7067 return -1;
7068
9fb04070 7069 nl80211_cmd(drv, msg, 0, NL80211_CMD_JOIN_IBSS);
5cc4d64b
JM
7070 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
7071
7072 if (params->ssid == NULL || params->ssid_len > sizeof(drv->ssid))
7073 goto nla_put_failure;
7074
7075 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
7076 params->ssid, params->ssid_len);
7077 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
7078 params->ssid);
7079 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
7080 drv->ssid_len = params->ssid_len;
7081
7082 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
7083 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
7084
7085 ret = nl80211_set_conn_keys(params, msg);
7086 if (ret)
7087 goto nla_put_failure;
7088
913e3cf7
NC
7089 if (params->bssid && params->fixed_bssid) {
7090 wpa_printf(MSG_DEBUG, " * BSSID=" MACSTR,
7091 MAC2STR(params->bssid));
7092 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
7093 }
7094
e640888c
AQ
7095 if (params->key_mgmt_suite == KEY_MGMT_802_1X ||
7096 params->key_mgmt_suite == KEY_MGMT_PSK ||
7097 params->key_mgmt_suite == KEY_MGMT_802_1X_SHA256 ||
7098 params->key_mgmt_suite == KEY_MGMT_PSK_SHA256) {
7099 wpa_printf(MSG_DEBUG, " * control port");
7100 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT);
7101 }
7102
a95795ad
JM
7103 if (params->wpa_ie) {
7104 wpa_hexdump(MSG_DEBUG,
7105 " * Extra IEs for Beacon/Probe Response frames",
7106 params->wpa_ie, params->wpa_ie_len);
7107 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
7108 params->wpa_ie);
7109 }
7110
5cc4d64b
JM
7111 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7112 msg = NULL;
7113 if (ret) {
7114 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS failed: ret=%d (%s)",
7115 ret, strerror(-ret));
7116 count++;
7117 if (ret == -EALREADY && count == 1) {
7118 wpa_printf(MSG_DEBUG, "nl80211: Retry IBSS join after "
7119 "forced leave");
7120 nl80211_leave_ibss(drv);
7121 nlmsg_free(msg);
7122 goto retry;
7123 }
7124
7125 goto nla_put_failure;
7126 }
7127 ret = 0;
7128 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS request sent successfully");
7129
7130nla_put_failure:
7131 nlmsg_free(msg);
7132 return ret;
7133}
7134
7135
a8c5b43a 7136static int wpa_driver_nl80211_try_connect(
cfaab580
ZY
7137 struct wpa_driver_nl80211_data *drv,
7138 struct wpa_driver_associate_params *params)
7139{
7140 struct nl_msg *msg;
7141 enum nl80211_auth_type type;
7142 int ret = 0;
3f360238 7143 int algs;
cfaab580
ZY
7144
7145 msg = nlmsg_alloc();
7146 if (!msg)
7147 return -1;
7148
7149 wpa_printf(MSG_DEBUG, "nl80211: Connect (ifindex=%d)", drv->ifindex);
9fb04070 7150 nl80211_cmd(drv, msg, 0, NL80211_CMD_CONNECT);
cfaab580
ZY
7151
7152 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
7153 if (params->bssid) {
7154 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
7155 MAC2STR(params->bssid));
7156 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
7157 }
7158 if (params->freq) {
7159 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
7160 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
7161 }
1f6c0ab8
BS
7162 if (params->bg_scan_period >= 0) {
7163 wpa_printf(MSG_DEBUG, " * bg scan period=%d",
7164 params->bg_scan_period);
7165 NLA_PUT_U16(msg, NL80211_ATTR_BG_SCAN_PERIOD,
7166 params->bg_scan_period);
7167 }
cfaab580
ZY
7168 if (params->ssid) {
7169 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
7170 params->ssid, params->ssid_len);
7171 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
7172 params->ssid);
7173 if (params->ssid_len > sizeof(drv->ssid))
7174 goto nla_put_failure;
7175 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
7176 drv->ssid_len = params->ssid_len;
7177 }
7178 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
7179 if (params->wpa_ie)
7180 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
7181 params->wpa_ie);
7182
3f360238
JM
7183 algs = 0;
7184 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
7185 algs++;
7186 if (params->auth_alg & WPA_AUTH_ALG_SHARED)
7187 algs++;
7188 if (params->auth_alg & WPA_AUTH_ALG_LEAP)
7189 algs++;
7190 if (algs > 1) {
7191 wpa_printf(MSG_DEBUG, " * Leave out Auth Type for automatic "
7192 "selection");
7193 goto skip_auth_type;
7194 }
7195
abd9fafa 7196 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
cfaab580 7197 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
abd9fafa 7198 else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
cfaab580 7199 type = NL80211_AUTHTYPE_SHARED_KEY;
abd9fafa 7200 else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
cfaab580 7201 type = NL80211_AUTHTYPE_NETWORK_EAP;
abd9fafa 7202 else if (params->auth_alg & WPA_AUTH_ALG_FT)
cfaab580
ZY
7203 type = NL80211_AUTHTYPE_FT;
7204 else
7205 goto nla_put_failure;
7206
7207 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
7208 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
7209
3f360238 7210skip_auth_type:
64fa840a
JM
7211 if (params->wpa_proto) {
7212 enum nl80211_wpa_versions ver = 0;
cfaab580 7213
64fa840a
JM
7214 if (params->wpa_proto & WPA_PROTO_WPA)
7215 ver |= NL80211_WPA_VERSION_1;
7216 if (params->wpa_proto & WPA_PROTO_RSN)
7217 ver |= NL80211_WPA_VERSION_2;
cfaab580 7218
64fa840a 7219 wpa_printf(MSG_DEBUG, " * WPA Versions 0x%x", ver);
cfaab580
ZY
7220 NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver);
7221 }
7222
7223 if (params->pairwise_suite != CIPHER_NONE) {
c1bb3e0a 7224 int cipher;
cfaab580
ZY
7225
7226 switch (params->pairwise_suite) {
369c8d7b
JM
7227 case CIPHER_SMS4:
7228 cipher = WLAN_CIPHER_SUITE_SMS4;
7229 break;
cfaab580
ZY
7230 case CIPHER_WEP40:
7231 cipher = WLAN_CIPHER_SUITE_WEP40;
7232 break;
7233 case CIPHER_WEP104:
7234 cipher = WLAN_CIPHER_SUITE_WEP104;
7235 break;
7236 case CIPHER_CCMP:
7237 cipher = WLAN_CIPHER_SUITE_CCMP;
7238 break;
eb7719ff
JM
7239 case CIPHER_GCMP:
7240 cipher = WLAN_CIPHER_SUITE_GCMP;
7241 break;
cfaab580
ZY
7242 case CIPHER_TKIP:
7243 default:
7244 cipher = WLAN_CIPHER_SUITE_TKIP;
7245 break;
7246 }
7247 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE, cipher);
7248 }
7249
7250 if (params->group_suite != CIPHER_NONE) {
c1bb3e0a 7251 int cipher;
cfaab580
ZY
7252
7253 switch (params->group_suite) {
369c8d7b
JM
7254 case CIPHER_SMS4:
7255 cipher = WLAN_CIPHER_SUITE_SMS4;
7256 break;
cfaab580
ZY
7257 case CIPHER_WEP40:
7258 cipher = WLAN_CIPHER_SUITE_WEP40;
7259 break;
7260 case CIPHER_WEP104:
7261 cipher = WLAN_CIPHER_SUITE_WEP104;
7262 break;
7263 case CIPHER_CCMP:
7264 cipher = WLAN_CIPHER_SUITE_CCMP;
7265 break;
eb7719ff
JM
7266 case CIPHER_GCMP:
7267 cipher = WLAN_CIPHER_SUITE_GCMP;
7268 break;
cfaab580
ZY
7269 case CIPHER_TKIP:
7270 default:
7271 cipher = WLAN_CIPHER_SUITE_TKIP;
7272 break;
7273 }
7274 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher);
7275 }
7276
7277 if (params->key_mgmt_suite == KEY_MGMT_802_1X ||
369c8d7b 7278 params->key_mgmt_suite == KEY_MGMT_PSK ||
6a1ce395
DG
7279 params->key_mgmt_suite == KEY_MGMT_FT_802_1X ||
7280 params->key_mgmt_suite == KEY_MGMT_FT_PSK ||
369c8d7b 7281 params->key_mgmt_suite == KEY_MGMT_CCKM) {
cfaab580
ZY
7282 int mgmt = WLAN_AKM_SUITE_PSK;
7283
7284 switch (params->key_mgmt_suite) {
369c8d7b
JM
7285 case KEY_MGMT_CCKM:
7286 mgmt = WLAN_AKM_SUITE_CCKM;
7287 break;
cfaab580
ZY
7288 case KEY_MGMT_802_1X:
7289 mgmt = WLAN_AKM_SUITE_8021X;
7290 break;
6a1ce395
DG
7291 case KEY_MGMT_FT_802_1X:
7292 mgmt = WLAN_AKM_SUITE_FT_8021X;
7293 break;
7294 case KEY_MGMT_FT_PSK:
7295 mgmt = WLAN_AKM_SUITE_FT_PSK;
7296 break;
cfaab580
ZY
7297 case KEY_MGMT_PSK:
7298 default:
7299 mgmt = WLAN_AKM_SUITE_PSK;
7300 break;
7301 }
7302 NLA_PUT_U32(msg, NL80211_ATTR_AKM_SUITES, mgmt);
7303 }
7304
8b706a99
JM
7305#ifdef CONFIG_IEEE80211W
7306 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED)
7307 NLA_PUT_U32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED);
7308#endif /* CONFIG_IEEE80211W */
7309
80e8a5ee
BG
7310 if (params->disable_ht)
7311 NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_HT);
7312
7313 if (params->htcaps && params->htcaps_mask) {
7314 int sz = sizeof(struct ieee80211_ht_capabilities);
7315 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY, sz, params->htcaps);
7316 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY_MASK, sz,
7317 params->htcaps_mask);
7318 }
7319
e9ee8dc3
JB
7320#ifdef CONFIG_VHT_OVERRIDES
7321 if (params->disable_vht) {
7322 wpa_printf(MSG_DEBUG, " * VHT disabled");
7323 NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_VHT);
7324 }
7325
7326 if (params->vhtcaps && params->vhtcaps_mask) {
7327 int sz = sizeof(struct ieee80211_vht_capabilities);
7328 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY, sz, params->vhtcaps);
7329 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY_MASK, sz,
7330 params->vhtcaps_mask);
7331 }
7332#endif /* CONFIG_VHT_OVERRIDES */
7333
cfaab580
ZY
7334 ret = nl80211_set_conn_keys(params, msg);
7335 if (ret)
7336 goto nla_put_failure;
7337
7338 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7339 msg = NULL;
7340 if (ret) {
7341 wpa_printf(MSG_DEBUG, "nl80211: MLME connect failed: ret=%d "
7342 "(%s)", ret, strerror(-ret));
7343 goto nla_put_failure;
7344 }
7345 ret = 0;
7346 wpa_printf(MSG_DEBUG, "nl80211: Connect request send successfully");
7347
7348nla_put_failure:
7349 nlmsg_free(msg);
7350 return ret;
7351
7352}
7353
7354
a8c5b43a
CW
7355static int wpa_driver_nl80211_connect(
7356 struct wpa_driver_nl80211_data *drv,
7357 struct wpa_driver_associate_params *params)
7358{
7359 int ret = wpa_driver_nl80211_try_connect(drv, params);
7360 if (ret == -EALREADY) {
7361 /*
7362 * cfg80211 does not currently accept new connections if
7363 * we are already connected. As a workaround, force
7364 * disconnection and try again.
7365 */
7366 wpa_printf(MSG_DEBUG, "nl80211: Explicitly "
7367 "disconnecting before reassociation "
7368 "attempt");
7369 if (wpa_driver_nl80211_disconnect(
7370 drv, WLAN_REASON_PREV_AUTH_NOT_VALID))
7371 return -1;
7372 /* Ignore the next local disconnect message. */
7373 drv->ignore_next_local_disconnect = 1;
7374 ret = wpa_driver_nl80211_try_connect(drv, params);
7375 }
7376 return ret;
7377}
7378
7379
c2a04078
JM
7380static int wpa_driver_nl80211_associate(
7381 void *priv, struct wpa_driver_associate_params *params)
7382{
a2e40bb6
FF
7383 struct i802_bss *bss = priv;
7384 struct wpa_driver_nl80211_data *drv = bss->drv;
c2a04078
JM
7385 int ret = -1;
7386 struct nl_msg *msg;
7387
5cc4d64b 7388 if (params->mode == IEEE80211_MODE_AP)
1581b38b 7389 return wpa_driver_nl80211_ap(drv, params);
1581b38b 7390
5cc4d64b
JM
7391 if (params->mode == IEEE80211_MODE_IBSS)
7392 return wpa_driver_nl80211_ibss(drv, params);
7393
4a867032 7394 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
b1f625e0
EP
7395 enum nl80211_iftype nlmode = params->p2p ?
7396 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
7397
7398 if (wpa_driver_nl80211_set_mode(priv, nlmode) < 0)
4a867032 7399 return -1;
cfaab580 7400 return wpa_driver_nl80211_connect(drv, params);
4a867032 7401 }
cfaab580 7402
c2a04078
JM
7403 drv->associated = 0;
7404
7405 msg = nlmsg_alloc();
7406 if (!msg)
7407 return -1;
7408
7409 wpa_printf(MSG_DEBUG, "nl80211: Associate (ifindex=%d)",
7410 drv->ifindex);
9fb04070 7411 nl80211_cmd(drv, msg, 0, NL80211_CMD_ASSOCIATE);
c2a04078
JM
7412
7413 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
7414 if (params->bssid) {
7415 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
7416 MAC2STR(params->bssid));
7417 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
7418 }
7419 if (params->freq) {
7420 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
7421 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
4832ecd7
JM
7422 drv->assoc_freq = params->freq;
7423 } else
7424 drv->assoc_freq = 0;
1f6c0ab8
BS
7425 if (params->bg_scan_period >= 0) {
7426 wpa_printf(MSG_DEBUG, " * bg scan period=%d",
7427 params->bg_scan_period);
7428 NLA_PUT_U16(msg, NL80211_ATTR_BG_SCAN_PERIOD,
7429 params->bg_scan_period);
7430 }
c2a04078
JM
7431 if (params->ssid) {
7432 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
7433 params->ssid, params->ssid_len);
7434 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
7435 params->ssid);
fd05d64e
JM
7436 if (params->ssid_len > sizeof(drv->ssid))
7437 goto nla_put_failure;
7438 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
7439 drv->ssid_len = params->ssid_len;
c2a04078
JM
7440 }
7441 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
7442 if (params->wpa_ie)
7443 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
7444 params->wpa_ie);
7445
aca01605
JM
7446 if (params->pairwise_suite != CIPHER_NONE) {
7447 int cipher;
7448
7449 switch (params->pairwise_suite) {
7450 case CIPHER_WEP40:
7451 cipher = WLAN_CIPHER_SUITE_WEP40;
7452 break;
7453 case CIPHER_WEP104:
7454 cipher = WLAN_CIPHER_SUITE_WEP104;
7455 break;
7456 case CIPHER_CCMP:
7457 cipher = WLAN_CIPHER_SUITE_CCMP;
7458 break;
eb7719ff
JM
7459 case CIPHER_GCMP:
7460 cipher = WLAN_CIPHER_SUITE_GCMP;
7461 break;
aca01605
JM
7462 case CIPHER_TKIP:
7463 default:
7464 cipher = WLAN_CIPHER_SUITE_TKIP;
7465 break;
7466 }
79c3124c 7467 wpa_printf(MSG_DEBUG, " * pairwise=0x%x", cipher);
aca01605
JM
7468 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE, cipher);
7469 }
7470
7471 if (params->group_suite != CIPHER_NONE) {
7472 int cipher;
7473
7474 switch (params->group_suite) {
7475 case CIPHER_WEP40:
7476 cipher = WLAN_CIPHER_SUITE_WEP40;
7477 break;
7478 case CIPHER_WEP104:
7479 cipher = WLAN_CIPHER_SUITE_WEP104;
7480 break;
7481 case CIPHER_CCMP:
7482 cipher = WLAN_CIPHER_SUITE_CCMP;
7483 break;
eb7719ff
JM
7484 case CIPHER_GCMP:
7485 cipher = WLAN_CIPHER_SUITE_GCMP;
7486 break;
aca01605
JM
7487 case CIPHER_TKIP:
7488 default:
7489 cipher = WLAN_CIPHER_SUITE_TKIP;
7490 break;
7491 }
79c3124c 7492 wpa_printf(MSG_DEBUG, " * group=0x%x", cipher);
aca01605
JM
7493 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher);
7494 }
7495
e572fa33
JM
7496#ifdef CONFIG_IEEE80211W
7497 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED)
7498 NLA_PUT_U32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED);
7499#endif /* CONFIG_IEEE80211W */
7500
01652550
JM
7501 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT);
7502
62fa124c
JM
7503 if (params->prev_bssid) {
7504 wpa_printf(MSG_DEBUG, " * prev_bssid=" MACSTR,
7505 MAC2STR(params->prev_bssid));
7506 NLA_PUT(msg, NL80211_ATTR_PREV_BSSID, ETH_ALEN,
7507 params->prev_bssid);
7508 }
7509
80e8a5ee
BG
7510 if (params->disable_ht)
7511 NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_HT);
7512
7513 if (params->htcaps && params->htcaps_mask) {
7514 int sz = sizeof(struct ieee80211_ht_capabilities);
7515 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY, sz, params->htcaps);
7516 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY_MASK, sz,
7517 params->htcaps_mask);
7518 }
7519
e9ee8dc3
JB
7520#ifdef CONFIG_VHT_OVERRIDES
7521 if (params->disable_vht) {
7522 wpa_printf(MSG_DEBUG, " * VHT disabled");
7523 NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_VHT);
7524 }
7525
7526 if (params->vhtcaps && params->vhtcaps_mask) {
7527 int sz = sizeof(struct ieee80211_vht_capabilities);
7528 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY, sz, params->vhtcaps);
7529 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY_MASK, sz,
7530 params->vhtcaps_mask);
7531 }
7532#endif /* CONFIG_VHT_OVERRIDES */
7533
046b26a2
JM
7534 if (params->p2p)
7535 wpa_printf(MSG_DEBUG, " * P2P group");
7536
c2a04078
JM
7537 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7538 msg = NULL;
7539 if (ret) {
3b7ea880
BG
7540 wpa_dbg(drv->ctx, MSG_DEBUG,
7541 "nl80211: MLME command failed (assoc): ret=%d (%s)",
7542 ret, strerror(-ret));
8856462d 7543 nl80211_dump_scan(drv);
c2a04078
JM
7544 goto nla_put_failure;
7545 }
7546 ret = 0;
7547 wpa_printf(MSG_DEBUG, "nl80211: Association request send "
7548 "successfully");
7549
7550nla_put_failure:
7551 nlmsg_free(msg);
7552 return ret;
7553}
3f5285e8
JM
7554
7555
ad1e68e6 7556static int nl80211_set_mode(struct wpa_driver_nl80211_data *drv,
a1922f93 7557 int ifindex, enum nl80211_iftype mode)
3f5285e8 7558{
3f5285e8 7559 struct nl_msg *msg;
ad1e68e6
JM
7560 int ret = -ENOBUFS;
7561
a1922f93
JM
7562 wpa_printf(MSG_DEBUG, "nl80211: Set mode ifindex %d iftype %d (%s)",
7563 ifindex, mode, nl80211_iftype_str(mode));
7564
ad1e68e6
JM
7565 msg = nlmsg_alloc();
7566 if (!msg)
7567 return -ENOMEM;
7568
9fb04070 7569 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_INTERFACE);
ad1e68e6
JM
7570 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
7571 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, mode);
7572
7573 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5883168a 7574 msg = NULL;
ad1e68e6
JM
7575 if (!ret)
7576 return 0;
7577nla_put_failure:
5883168a 7578 nlmsg_free(msg);
ad1e68e6
JM
7579 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface %d to mode %d:"
7580 " %d (%s)", ifindex, mode, ret, strerror(-ret));
7581 return ret;
7582}
7583
7584
b1f625e0
EP
7585static int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
7586 enum nl80211_iftype nlmode)
ad1e68e6 7587{
a2e40bb6 7588 struct wpa_driver_nl80211_data *drv = bss->drv;
ad1e68e6 7589 int ret = -1;
26af9dca 7590 int i;
86957e62 7591 int was_ap = is_ap_interface(drv->nlmode);
671a5039 7592 int res;
1581b38b 7593
671a5039
JM
7594 res = nl80211_set_mode(drv, drv->ifindex, nlmode);
7595 if (res == 0) {
ad1e68e6 7596 drv->nlmode = nlmode;
460456f8
JM
7597 ret = 0;
7598 goto done;
ad1e68e6 7599 }
3f5285e8 7600
671a5039
JM
7601 if (res == -ENODEV)
7602 return -1;
7603
460456f8 7604 if (nlmode == drv->nlmode) {
c6e8e8e4
JM
7605 wpa_printf(MSG_DEBUG, "nl80211: Interface already in "
7606 "requested mode - ignore error");
460456f8
JM
7607 ret = 0;
7608 goto done; /* Already in the requested mode */
7609 }
3f5285e8 7610
3f5285e8
JM
7611 /* mac80211 doesn't allow mode changes while the device is up, so
7612 * take the device down, try to set the mode again, and bring the
7613 * device back up.
7614 */
26af9dca
JM
7615 wpa_printf(MSG_DEBUG, "nl80211: Try mode change after setting "
7616 "interface down");
7617 for (i = 0; i < 10; i++) {
c81eff1a
BG
7618 res = linux_set_iface_flags(drv->global->ioctl_sock,
7619 bss->ifname, 0);
6e8183d7
JM
7620 if (res == -EACCES || res == -ENODEV)
7621 break;
7622 if (res == 0) {
26af9dca
JM
7623 /* Try to set the mode again while the interface is
7624 * down */
7625 ret = nl80211_set_mode(drv, drv->ifindex, nlmode);
6e8183d7
JM
7626 if (ret == -EACCES)
7627 break;
c81eff1a 7628 res = linux_set_iface_flags(drv->global->ioctl_sock,
6e8183d7
JM
7629 bss->ifname, 1);
7630 if (res && !ret)
26af9dca 7631 ret = -1;
6e8183d7 7632 else if (ret != -EBUSY)
26af9dca
JM
7633 break;
7634 } else
7635 wpa_printf(MSG_DEBUG, "nl80211: Failed to set "
7636 "interface down");
7637 os_sleep(0, 100000);
3f5285e8
JM
7638 }
7639
c6e8e8e4
JM
7640 if (!ret) {
7641 wpa_printf(MSG_DEBUG, "nl80211: Mode change succeeded while "
7642 "interface is down");
ad1e68e6 7643 drv->nlmode = nlmode;
7d9c3698 7644 drv->ignore_if_down_event = 1;
c6e8e8e4 7645 }
ad1e68e6 7646
460456f8 7647done:
3fd1cefb
JB
7648 if (ret) {
7649 wpa_printf(MSG_DEBUG, "nl80211: Interface mode change to %d "
7650 "from %d failed", nlmode, drv->nlmode);
7651 return ret;
7652 }
7653
edb9bfba
RM
7654 if (is_p2p_interface(nlmode))
7655 nl80211_disable_11b_rates(drv, drv->ifindex, 1);
1d0c6fb1
JM
7656 else if (drv->disabled_11b_rates)
7657 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
edb9bfba 7658
3fd1cefb 7659 if (is_ap_interface(nlmode)) {
36488c05 7660 nl80211_mgmt_unsubscribe(bss, "start AP");
460456f8 7661 /* Setup additional AP mode functionality if needed */
3fd1cefb 7662 if (nl80211_setup_ap(bss))
460456f8 7663 return -1;
3fd1cefb 7664 } else if (was_ap) {
460456f8 7665 /* Remove additional AP mode functionality */
3fd1cefb 7666 nl80211_teardown_ap(bss);
a11241fa 7667 } else {
36488c05 7668 nl80211_mgmt_unsubscribe(bss, "mode change");
460456f8 7669 }
460456f8 7670
873d0fcf 7671 if (!bss->in_deinit && !is_ap_interface(nlmode) &&
a11241fa
JB
7672 nl80211_mgmt_subscribe_non_ap(bss) < 0)
7673 wpa_printf(MSG_DEBUG, "nl80211: Failed to register Action "
7674 "frame processing - ignore for now");
08359050 7675
3fd1cefb 7676 return 0;
3f5285e8
JM
7677}
7678
7679
3f5285e8
JM
7680static int wpa_driver_nl80211_get_capa(void *priv,
7681 struct wpa_driver_capa *capa)
7682{
a2e40bb6
FF
7683 struct i802_bss *bss = priv;
7684 struct wpa_driver_nl80211_data *drv = bss->drv;
3f5285e8
JM
7685 if (!drv->has_capability)
7686 return -1;
7687 os_memcpy(capa, &drv->capa, sizeof(*capa));
8cd6b7bc
JB
7688 if (drv->extended_capa && drv->extended_capa_mask) {
7689 capa->extended_capa = drv->extended_capa;
7690 capa->extended_capa_mask = drv->extended_capa_mask;
7691 capa->extended_capa_len = drv->extended_capa_len;
7692 }
3f5285e8
JM
7693 return 0;
7694}
7695
7696
7697static int wpa_driver_nl80211_set_operstate(void *priv, int state)
7698{
a2e40bb6
FF
7699 struct i802_bss *bss = priv;
7700 struct wpa_driver_nl80211_data *drv = bss->drv;
3f5285e8
JM
7701
7702 wpa_printf(MSG_DEBUG, "%s: operstate %d->%d (%s)",
7703 __func__, drv->operstate, state, state ? "UP" : "DORMANT");
7704 drv->operstate = state;
36d84860 7705 return netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, -1,
e2d02c29 7706 state ? IF_OPER_UP : IF_OPER_DORMANT);
3f5285e8
JM
7707}
7708
01652550
JM
7709
7710static int wpa_driver_nl80211_set_supp_port(void *priv, int authorized)
7711{
a2e40bb6
FF
7712 struct i802_bss *bss = priv;
7713 struct wpa_driver_nl80211_data *drv = bss->drv;
01652550
JM
7714 struct nl_msg *msg;
7715 struct nl80211_sta_flag_update upd;
7716
7717 msg = nlmsg_alloc();
7718 if (!msg)
7719 return -ENOMEM;
7720
9fb04070 7721 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
01652550
JM
7722
7723 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
a2e40bb6 7724 if_nametoindex(bss->ifname));
01652550
JM
7725 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid);
7726
7727 os_memset(&upd, 0, sizeof(upd));
7728 upd.mask = BIT(NL80211_STA_FLAG_AUTHORIZED);
7729 if (authorized)
7730 upd.set = BIT(NL80211_STA_FLAG_AUTHORIZED);
7731 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
7732
7733 return send_and_recv_msgs(drv, msg, NULL, NULL);
7734 nla_put_failure:
5883168a 7735 nlmsg_free(msg);
01652550
JM
7736 return -ENOBUFS;
7737}
7738
3f5285e8 7739
f07ead6a
JM
7740/* Set kernel driver on given frequency (MHz) */
7741static int i802_set_freq(void *priv, struct hostapd_freq_params *freq)
c5121837 7742{
f07ead6a 7743 struct i802_bss *bss = priv;
89b800d7 7744 return wpa_driver_nl80211_set_freq(bss, freq);
c5121837
JM
7745}
7746
f7b3920c
JM
7747
7748#if defined(HOSTAPD) || defined(CONFIG_AP)
c5121837 7749
c5121837
JM
7750static inline int min_int(int a, int b)
7751{
7752 if (a < b)
7753 return a;
7754 return b;
7755}
7756
7757
7758static int get_key_handler(struct nl_msg *msg, void *arg)
7759{
7760 struct nlattr *tb[NL80211_ATTR_MAX + 1];
7761 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
7762
7763 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
7764 genlmsg_attrlen(gnlh, 0), NULL);
7765
7766 /*
7767 * TODO: validate the key index and mac address!
7768 * Otherwise, there's a race condition as soon as
7769 * the kernel starts sending key notifications.
7770 */
7771
7772 if (tb[NL80211_ATTR_KEY_SEQ])
7773 memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]),
7774 min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6));
7775 return NL_SKIP;
7776}
7777
7778
7779static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
7780 int idx, u8 *seq)
7781{
a2e40bb6
FF
7782 struct i802_bss *bss = priv;
7783 struct wpa_driver_nl80211_data *drv = bss->drv;
c5121837
JM
7784 struct nl_msg *msg;
7785
7786 msg = nlmsg_alloc();
7787 if (!msg)
7788 return -ENOMEM;
7789
9fb04070 7790 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_KEY);
c5121837
JM
7791
7792 if (addr)
7793 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
7794 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, idx);
7795 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
7796
7797 memset(seq, 0, 6);
7798
7799 return send_and_recv_msgs(drv, msg, get_key_handler, seq);
7800 nla_put_failure:
9e088e74 7801 nlmsg_free(msg);
c5121837
JM
7802 return -ENOBUFS;
7803}
7804
7805
c5121837
JM
7806static int i802_set_rts(void *priv, int rts)
7807{
a2e40bb6
FF
7808 struct i802_bss *bss = priv;
7809 struct wpa_driver_nl80211_data *drv = bss->drv;
ad649451
JM
7810 struct nl_msg *msg;
7811 int ret = -ENOBUFS;
7812 u32 val;
c5121837 7813
ad649451
JM
7814 msg = nlmsg_alloc();
7815 if (!msg)
7816 return -ENOMEM;
c5121837 7817
ad649451
JM
7818 if (rts >= 2347)
7819 val = (u32) -1;
7820 else
7821 val = rts;
c5121837 7822
9fb04070 7823 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
ad649451
JM
7824 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
7825 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD, val);
7826
7827 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5883168a 7828 msg = NULL;
ad649451
JM
7829 if (!ret)
7830 return 0;
7831nla_put_failure:
5883168a 7832 nlmsg_free(msg);
ad649451
JM
7833 wpa_printf(MSG_DEBUG, "nl80211: Failed to set RTS threshold %d: "
7834 "%d (%s)", rts, ret, strerror(-ret));
7835 return ret;
c5121837
JM
7836}
7837
7838
7839static int i802_set_frag(void *priv, int frag)
7840{
a2e40bb6
FF
7841 struct i802_bss *bss = priv;
7842 struct wpa_driver_nl80211_data *drv = bss->drv;
ad649451
JM
7843 struct nl_msg *msg;
7844 int ret = -ENOBUFS;
7845 u32 val;
c5121837 7846
ad649451
JM
7847 msg = nlmsg_alloc();
7848 if (!msg)
7849 return -ENOMEM;
c5121837 7850
ad649451
JM
7851 if (frag >= 2346)
7852 val = (u32) -1;
7853 else
7854 val = frag;
c5121837 7855
9fb04070 7856 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
ad649451
JM
7857 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
7858 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD, val);
7859
7860 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5883168a 7861 msg = NULL;
ad649451
JM
7862 if (!ret)
7863 return 0;
7864nla_put_failure:
5883168a 7865 nlmsg_free(msg);
ad649451
JM
7866 wpa_printf(MSG_DEBUG, "nl80211: Failed to set fragmentation threshold "
7867 "%d: %d (%s)", frag, ret, strerror(-ret));
7868 return ret;
c5121837
JM
7869}
7870
7871
c5121837
JM
7872static int i802_flush(void *priv)
7873{
a2e40bb6
FF
7874 struct i802_bss *bss = priv;
7875 struct wpa_driver_nl80211_data *drv = bss->drv;
c5121837 7876 struct nl_msg *msg;
82554b10 7877 int res;
c5121837
JM
7878
7879 msg = nlmsg_alloc();
7880 if (!msg)
7881 return -1;
7882
9fb04070 7883 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_STATION);
c5121837
JM
7884
7885 /*
7886 * XXX: FIX! this needs to flush all VLANs too
7887 */
7888 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
a2e40bb6 7889 if_nametoindex(bss->ifname));
c5121837 7890
82554b10
JM
7891 res = send_and_recv_msgs(drv, msg, NULL, NULL);
7892 if (res) {
7893 wpa_printf(MSG_DEBUG, "nl80211: Station flush failed: ret=%d "
7894 "(%s)", res, strerror(-res));
7895 }
7896 return res;
c5121837 7897 nla_put_failure:
9e088e74 7898 nlmsg_free(msg);
c5121837
JM
7899 return -ENOBUFS;
7900}
7901
d732463c
JM
7902#endif /* HOSTAPD || CONFIG_AP */
7903
c5121837
JM
7904
7905static int get_sta_handler(struct nl_msg *msg, void *arg)
7906{
7907 struct nlattr *tb[NL80211_ATTR_MAX + 1];
7908 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
7909 struct hostap_sta_driver_data *data = arg;
7910 struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
7911 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
7912 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
7913 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
7914 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
7915 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
7916 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
dc7785f8 7917 [NL80211_STA_INFO_TX_FAILED] = { .type = NLA_U32 },
c5121837
JM
7918 };
7919
7920 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
7921 genlmsg_attrlen(gnlh, 0), NULL);
7922
7923 /*
7924 * TODO: validate the interface and mac address!
7925 * Otherwise, there's a race condition as soon as
7926 * the kernel starts sending station notifications.
7927 */
7928
7929 if (!tb[NL80211_ATTR_STA_INFO]) {
7930 wpa_printf(MSG_DEBUG, "sta stats missing!");
7931 return NL_SKIP;
7932 }
7933 if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
7934 tb[NL80211_ATTR_STA_INFO],
7935 stats_policy)) {
7936 wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
7937 return NL_SKIP;
7938 }
7939
7940 if (stats[NL80211_STA_INFO_INACTIVE_TIME])
7941 data->inactive_msec =
7942 nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
7943 if (stats[NL80211_STA_INFO_RX_BYTES])
7944 data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
7945 if (stats[NL80211_STA_INFO_TX_BYTES])
7946 data->tx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
7947 if (stats[NL80211_STA_INFO_RX_PACKETS])
7948 data->rx_packets =
7949 nla_get_u32(stats[NL80211_STA_INFO_RX_PACKETS]);
7950 if (stats[NL80211_STA_INFO_TX_PACKETS])
7951 data->tx_packets =
7952 nla_get_u32(stats[NL80211_STA_INFO_TX_PACKETS]);
dc7785f8
YZ
7953 if (stats[NL80211_STA_INFO_TX_FAILED])
7954 data->tx_retry_failed =
7955 nla_get_u32(stats[NL80211_STA_INFO_TX_FAILED]);
c5121837
JM
7956
7957 return NL_SKIP;
7958}
7959
9ebce9c5
JM
7960static int i802_read_sta_data(struct i802_bss *bss,
7961 struct hostap_sta_driver_data *data,
c5121837
JM
7962 const u8 *addr)
7963{
a2e40bb6 7964 struct wpa_driver_nl80211_data *drv = bss->drv;
c5121837
JM
7965 struct nl_msg *msg;
7966
7967 os_memset(data, 0, sizeof(*data));
7968 msg = nlmsg_alloc();
7969 if (!msg)
7970 return -ENOMEM;
7971
9fb04070 7972 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_STATION);
c5121837
JM
7973
7974 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
a2e40bb6 7975 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
c5121837
JM
7976
7977 return send_and_recv_msgs(drv, msg, get_sta_handler, data);
7978 nla_put_failure:
9e088e74 7979 nlmsg_free(msg);
c5121837
JM
7980 return -ENOBUFS;
7981}
7982
7983
d732463c
JM
7984#if defined(HOSTAPD) || defined(CONFIG_AP)
7985
c5121837
JM
7986static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
7987 int cw_min, int cw_max, int burst_time)
7988{
a2e40bb6
FF
7989 struct i802_bss *bss = priv;
7990 struct wpa_driver_nl80211_data *drv = bss->drv;
c5121837
JM
7991 struct nl_msg *msg;
7992 struct nlattr *txq, *params;
7993
7994 msg = nlmsg_alloc();
7995 if (!msg)
7996 return -1;
7997
9fb04070 7998 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
c5121837 7999
a2e40bb6 8000 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
c5121837
JM
8001
8002 txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS);
8003 if (!txq)
8004 goto nla_put_failure;
8005
8006 /* We are only sending parameters for a single TXQ at a time */
8007 params = nla_nest_start(msg, 1);
8008 if (!params)
8009 goto nla_put_failure;
8010
7e3c1781
JM
8011 switch (queue) {
8012 case 0:
8013 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VO);
8014 break;
8015 case 1:
8016 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VI);
8017 break;
8018 case 2:
8019 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BE);
8020 break;
8021 case 3:
8022 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BK);
8023 break;
8024 }
c5121837
JM
8025 /* Burst time is configured in units of 0.1 msec and TXOP parameter in
8026 * 32 usec, so need to convert the value here. */
8027 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_TXOP, (burst_time * 100 + 16) / 32);
8028 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min);
8029 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max);
8030 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_AIFS, aifs);
8031
8032 nla_nest_end(msg, params);
8033
8034 nla_nest_end(msg, txq);
8035
8036 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
8037 return 0;
9e088e74 8038 msg = NULL;
c5121837 8039 nla_put_failure:
9e088e74 8040 nlmsg_free(msg);
c5121837
JM
8041 return -1;
8042}
8043
8044
9ebce9c5 8045static int i802_set_sta_vlan(struct i802_bss *bss, const u8 *addr,
c5121837
JM
8046 const char *ifname, int vlan_id)
8047{
a2e40bb6 8048 struct wpa_driver_nl80211_data *drv = bss->drv;
c5121837 8049 struct nl_msg *msg;
cd1d72c1 8050 int ret = -ENOBUFS;
c5121837
JM
8051
8052 msg = nlmsg_alloc();
8053 if (!msg)
8054 return -ENOMEM;
8055
9fb04070 8056 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
c5121837
JM
8057
8058 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
a2e40bb6 8059 if_nametoindex(bss->ifname));
c5121837 8060 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
1c766b09 8061 NLA_PUT_U32(msg, NL80211_ATTR_STA_VLAN,
c5121837
JM
8062 if_nametoindex(ifname));
8063
cd1d72c1 8064 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9e088e74 8065 msg = NULL;
cd1d72c1
JM
8066 if (ret < 0) {
8067 wpa_printf(MSG_ERROR, "nl80211: NL80211_ATTR_STA_VLAN (addr="
8068 MACSTR " ifname=%s vlan_id=%d) failed: %d (%s)",
8069 MAC2STR(addr), ifname, vlan_id, ret,
8070 strerror(-ret));
8071 }
c5121837 8072 nla_put_failure:
9e088e74 8073 nlmsg_free(msg);
cd1d72c1 8074 return ret;
c5121837
JM
8075}
8076
fbbfcbac 8077
c5121837
JM
8078static int i802_get_inact_sec(void *priv, const u8 *addr)
8079{
8080 struct hostap_sta_driver_data data;
8081 int ret;
8082
8083 data.inactive_msec = (unsigned long) -1;
8084 ret = i802_read_sta_data(priv, &data, addr);
8085 if (ret || data.inactive_msec == (unsigned long) -1)
8086 return -1;
8087 return data.inactive_msec / 1000;
8088}
8089
8090
8091static int i802_sta_clear_stats(void *priv, const u8 *addr)
8092{
8093#if 0
8094 /* TODO */
8095#endif
8096 return 0;
8097}
8098
8099
731723a5
JM
8100static int i802_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
8101 int reason)
c5121837 8102{
a2e40bb6 8103 struct i802_bss *bss = priv;
e1bd4e19 8104 struct wpa_driver_nl80211_data *drv = bss->drv;
c5121837
JM
8105 struct ieee80211_mgmt mgmt;
8106
e1bd4e19
AT
8107 if (drv->device_ap_sme)
8108 return wpa_driver_nl80211_sta_remove(bss, addr);
8109
c5121837
JM
8110 memset(&mgmt, 0, sizeof(mgmt));
8111 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
8112 WLAN_FC_STYPE_DEAUTH);
8113 memcpy(mgmt.da, addr, ETH_ALEN);
731723a5
JM
8114 memcpy(mgmt.sa, own_addr, ETH_ALEN);
8115 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
c5121837 8116 mgmt.u.deauth.reason_code = host_to_le16(reason);
a2e40bb6 8117 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
9f324b61 8118 IEEE80211_HDRLEN +
9ebce9c5
JM
8119 sizeof(mgmt.u.deauth), 0, 0, 0, 0,
8120 0);
c5121837
JM
8121}
8122
8123
731723a5
JM
8124static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
8125 int reason)
c5121837 8126{
a2e40bb6 8127 struct i802_bss *bss = priv;
e1bd4e19 8128 struct wpa_driver_nl80211_data *drv = bss->drv;
c5121837
JM
8129 struct ieee80211_mgmt mgmt;
8130
e1bd4e19
AT
8131 if (drv->device_ap_sme)
8132 return wpa_driver_nl80211_sta_remove(bss, addr);
8133
c5121837
JM
8134 memset(&mgmt, 0, sizeof(mgmt));
8135 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
8136 WLAN_FC_STYPE_DISASSOC);
8137 memcpy(mgmt.da, addr, ETH_ALEN);
731723a5
JM
8138 memcpy(mgmt.sa, own_addr, ETH_ALEN);
8139 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
c5121837 8140 mgmt.u.disassoc.reason_code = host_to_le16(reason);
a2e40bb6 8141 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
9f324b61 8142 IEEE80211_HDRLEN +
9ebce9c5
JM
8143 sizeof(mgmt.u.disassoc), 0, 0, 0, 0,
8144 0);
c5121837
JM
8145}
8146
ee7ab173
JB
8147#endif /* HOSTAPD || CONFIG_AP */
8148
8149#ifdef HOSTAPD
c5121837 8150
f07ead6a
JM
8151static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
8152{
8153 int i;
8154 int *old;
8155
8156 wpa_printf(MSG_DEBUG, "nl80211: Add own interface ifindex %d",
8157 ifidx);
8158 for (i = 0; i < drv->num_if_indices; i++) {
8159 if (drv->if_indices[i] == 0) {
8160 drv->if_indices[i] = ifidx;
8161 return;
8162 }
8163 }
8164
8165 if (drv->if_indices != drv->default_if_indices)
8166 old = drv->if_indices;
8167 else
8168 old = NULL;
8169
067ffa26
JM
8170 drv->if_indices = os_realloc_array(old, drv->num_if_indices + 1,
8171 sizeof(int));
f07ead6a
JM
8172 if (!drv->if_indices) {
8173 if (!old)
8174 drv->if_indices = drv->default_if_indices;
8175 else
8176 drv->if_indices = old;
8177 wpa_printf(MSG_ERROR, "Failed to reallocate memory for "
8178 "interfaces");
8179 wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx);
8180 return;
8181 } else if (!old)
8182 os_memcpy(drv->if_indices, drv->default_if_indices,
8183 sizeof(drv->default_if_indices));
8184 drv->if_indices[drv->num_if_indices] = ifidx;
8185 drv->num_if_indices++;
8186}
8187
8188
8189static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
8190{
8191 int i;
8192
8193 for (i = 0; i < drv->num_if_indices; i++) {
8194 if (drv->if_indices[i] == ifidx) {
8195 drv->if_indices[i] = 0;
8196 break;
8197 }
8198 }
8199}
8200
8201
8202static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
8203{
8204 int i;
8205
8206 for (i = 0; i < drv->num_if_indices; i++)
8207 if (drv->if_indices[i] == ifidx)
8208 return 1;
8209
8210 return 0;
8211}
8212
8213
8214static int i802_set_wds_sta(void *priv, const u8 *addr, int aid, int val,
8215 const char *bridge_ifname)
8216{
8217 struct i802_bss *bss = priv;
8218 struct wpa_driver_nl80211_data *drv = bss->drv;
8219 char name[IFNAMSIZ + 1];
8220
8221 os_snprintf(name, sizeof(name), "%s.sta%d", bss->ifname, aid);
8222 wpa_printf(MSG_DEBUG, "nl80211: Set WDS STA addr=" MACSTR
8223 " aid=%d val=%d name=%s", MAC2STR(addr), aid, val, name);
8224 if (val) {
8225 if (!if_nametoindex(name)) {
8226 if (nl80211_create_iface(drv, name,
8227 NL80211_IFTYPE_AP_VLAN,
c3e3a5b9 8228 bss->addr, 1) < 0)
f07ead6a
JM
8229 return -1;
8230 if (bridge_ifname &&
c81eff1a
BG
8231 linux_br_add_if(drv->global->ioctl_sock,
8232 bridge_ifname, name) < 0)
f07ead6a
JM
8233 return -1;
8234 }
75227f3a
JM
8235 if (linux_set_iface_flags(drv->global->ioctl_sock, name, 1)) {
8236 wpa_printf(MSG_ERROR, "nl80211: Failed to set WDS STA "
8237 "interface %s up", name);
8238 }
f07ead6a
JM
8239 return i802_set_sta_vlan(priv, addr, name, 0);
8240 } else {
c34e618d
FF
8241 if (bridge_ifname)
8242 linux_br_del_if(drv->global->ioctl_sock, bridge_ifname,
8243 name);
8244
f07ead6a
JM
8245 i802_set_sta_vlan(priv, addr, bss->ifname, 0);
8246 return wpa_driver_nl80211_if_remove(priv, WPA_IF_AP_VLAN,
8247 name);
8248 }
8249}
8250
8251
8252static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
8253{
8254 struct wpa_driver_nl80211_data *drv = eloop_ctx;
8255 struct sockaddr_ll lladdr;
8256 unsigned char buf[3000];
8257 int len;
8258 socklen_t fromlen = sizeof(lladdr);
8259
8260 len = recvfrom(sock, buf, sizeof(buf), 0,
8261 (struct sockaddr *)&lladdr, &fromlen);
8262 if (len < 0) {
8263 perror("recv");
8264 return;
8265 }
8266
8267 if (have_ifidx(drv, lladdr.sll_ifindex))
8268 drv_event_eapol_rx(drv->ctx, lladdr.sll_addr, buf, len);
8269}
8270
8271
94627f6c 8272static int i802_check_bridge(struct wpa_driver_nl80211_data *drv,
e17a2477 8273 struct i802_bss *bss,
94627f6c
JM
8274 const char *brname, const char *ifname)
8275{
8276 int ifindex;
8277 char in_br[IFNAMSIZ];
8278
e17a2477 8279 os_strlcpy(bss->brname, brname, IFNAMSIZ);
94627f6c
JM
8280 ifindex = if_nametoindex(brname);
8281 if (ifindex == 0) {
8282 /*
8283 * Bridge was configured, but the bridge device does
8284 * not exist. Try to add it now.
8285 */
c81eff1a 8286 if (linux_br_add(drv->global->ioctl_sock, brname) < 0) {
94627f6c
JM
8287 wpa_printf(MSG_ERROR, "nl80211: Failed to add the "
8288 "bridge interface %s: %s",
8289 brname, strerror(errno));
8290 return -1;
8291 }
e17a2477 8292 bss->added_bridge = 1;
94627f6c
JM
8293 add_ifidx(drv, if_nametoindex(brname));
8294 }
8295
8296 if (linux_br_get(in_br, ifname) == 0) {
8297 if (os_strcmp(in_br, brname) == 0)
8298 return 0; /* already in the bridge */
8299
8300 wpa_printf(MSG_DEBUG, "nl80211: Removing interface %s from "
8301 "bridge %s", ifname, in_br);
c81eff1a
BG
8302 if (linux_br_del_if(drv->global->ioctl_sock, in_br, ifname) <
8303 0) {
94627f6c
JM
8304 wpa_printf(MSG_ERROR, "nl80211: Failed to "
8305 "remove interface %s from bridge "
8306 "%s: %s",
8307 ifname, brname, strerror(errno));
8308 return -1;
8309 }
8310 }
8311
8312 wpa_printf(MSG_DEBUG, "nl80211: Adding interface %s into bridge %s",
8313 ifname, brname);
c81eff1a 8314 if (linux_br_add_if(drv->global->ioctl_sock, brname, ifname) < 0) {
94627f6c
JM
8315 wpa_printf(MSG_ERROR, "nl80211: Failed to add interface %s "
8316 "into bridge %s: %s",
8317 ifname, brname, strerror(errno));
8318 return -1;
8319 }
e17a2477 8320 bss->added_if_into_bridge = 1;
94627f6c
JM
8321
8322 return 0;
8323}
8324
8325
92f475b4
JM
8326static void *i802_init(struct hostapd_data *hapd,
8327 struct wpa_init_params *params)
c5121837
JM
8328{
8329 struct wpa_driver_nl80211_data *drv;
a2e40bb6 8330 struct i802_bss *bss;
c5121837 8331 size_t i;
94627f6c
JM
8332 char brname[IFNAMSIZ];
8333 int ifindex, br_ifindex;
8334 int br_added = 0;
c5121837 8335
4b24282a
JM
8336 bss = wpa_driver_nl80211_init(hapd, params->ifname,
8337 params->global_priv);
a2e40bb6 8338 if (bss == NULL)
c5121837 8339 return NULL;
c5121837 8340
a2e40bb6 8341 drv = bss->drv;
0382097e 8342 drv->nlmode = NL80211_IFTYPE_AP;
7635bfb0
JB
8343 drv->eapol_sock = -1;
8344
94627f6c
JM
8345 if (linux_br_get(brname, params->ifname) == 0) {
8346 wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in bridge %s",
8347 params->ifname, brname);
8348 br_ifindex = if_nametoindex(brname);
8349 } else {
8350 brname[0] = '\0';
8351 br_ifindex = 0;
8352 }
8353
c5121837
JM
8354 drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int);
8355 drv->if_indices = drv->default_if_indices;
92f475b4 8356 for (i = 0; i < params->num_bridge; i++) {
94627f6c
JM
8357 if (params->bridge[i]) {
8358 ifindex = if_nametoindex(params->bridge[i]);
8359 if (ifindex)
8360 add_ifidx(drv, ifindex);
8361 if (ifindex == br_ifindex)
8362 br_added = 1;
8363 }
c5121837 8364 }
94627f6c
JM
8365 if (!br_added && br_ifindex &&
8366 (params->num_bridge == 0 || !params->bridge[0]))
8367 add_ifidx(drv, br_ifindex);
c5121837 8368
ad1e68e6
JM
8369 /* start listening for EAPOL on the default AP interface */
8370 add_ifidx(drv, drv->ifindex);
8371
c81eff1a 8372 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0))
6980c191 8373 goto failed;
ad1e68e6 8374
6980c191 8375 if (params->bssid) {
c81eff1a 8376 if (linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
2ac9688e 8377 params->bssid))
460456f8
JM
8378 goto failed;
8379 }
ad1e68e6 8380
b1f625e0 8381 if (wpa_driver_nl80211_set_mode(bss, drv->nlmode)) {
ad1e68e6 8382 wpa_printf(MSG_ERROR, "nl80211: Failed to set interface %s "
a2e40bb6 8383 "into AP mode", bss->ifname);
bbaf0837 8384 goto failed;
ad1e68e6
JM
8385 }
8386
94627f6c 8387 if (params->num_bridge && params->bridge[0] &&
e17a2477 8388 i802_check_bridge(drv, bss, params->bridge[0], params->ifname) < 0)
94627f6c
JM
8389 goto failed;
8390
c81eff1a 8391 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1))
bbaf0837 8392 goto failed;
ad1e68e6
JM
8393
8394 drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
8395 if (drv->eapol_sock < 0) {
8396 perror("socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE)");
bbaf0837 8397 goto failed;
ad1e68e6
JM
8398 }
8399
8400 if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
8401 {
8402 printf("Could not register read socket for eapol\n");
c5121837 8403 goto failed;
ad1e68e6
JM
8404 }
8405
c81eff1a
BG
8406 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
8407 params->own_addr))
bbaf0837 8408 goto failed;
c5121837 8409
341eebee
JB
8410 memcpy(bss->addr, params->own_addr, ETH_ALEN);
8411
a2e40bb6 8412 return bss;
c5121837
JM
8413
8414failed:
7635bfb0 8415 wpa_driver_nl80211_deinit(bss);
bbaf0837
JM
8416 return NULL;
8417}
c5121837 8418
c5121837 8419
bbaf0837
JM
8420static void i802_deinit(void *priv)
8421{
9ebce9c5
JM
8422 struct i802_bss *bss = priv;
8423 wpa_driver_nl80211_deinit(bss);
c5121837
JM
8424}
8425
8426#endif /* HOSTAPD */
8427
8428
22a7c9d7
JM
8429static enum nl80211_iftype wpa_driver_nl80211_if_type(
8430 enum wpa_driver_if_type type)
8431{
8432 switch (type) {
8433 case WPA_IF_STATION:
9f51b113 8434 return NL80211_IFTYPE_STATION;
75bde05d
JM
8435 case WPA_IF_P2P_CLIENT:
8436 case WPA_IF_P2P_GROUP:
9f51b113 8437 return NL80211_IFTYPE_P2P_CLIENT;
22a7c9d7
JM
8438 case WPA_IF_AP_VLAN:
8439 return NL80211_IFTYPE_AP_VLAN;
8440 case WPA_IF_AP_BSS:
8441 return NL80211_IFTYPE_AP;
9f51b113
JB
8442 case WPA_IF_P2P_GO:
8443 return NL80211_IFTYPE_P2P_GO;
22a7c9d7
JM
8444 }
8445 return -1;
8446}
8447
8448
482856c8
JM
8449#ifdef CONFIG_P2P
8450
f2ed8023
JM
8451static int nl80211_addr_in_use(struct nl80211_global *global, const u8 *addr)
8452{
8453 struct wpa_driver_nl80211_data *drv;
8454 dl_list_for_each(drv, &global->interfaces,
8455 struct wpa_driver_nl80211_data, list) {
341eebee 8456 if (os_memcmp(addr, drv->first_bss.addr, ETH_ALEN) == 0)
f2ed8023
JM
8457 return 1;
8458 }
8459 return 0;
8460}
8461
8462
8463static int nl80211_p2p_interface_addr(struct wpa_driver_nl80211_data *drv,
8464 u8 *new_addr)
8465{
8466 unsigned int idx;
8467
8468 if (!drv->global)
8469 return -1;
8470
341eebee 8471 os_memcpy(new_addr, drv->first_bss.addr, ETH_ALEN);
f2ed8023 8472 for (idx = 0; idx < 64; idx++) {
341eebee 8473 new_addr[0] = drv->first_bss.addr[0] | 0x02;
f2ed8023
JM
8474 new_addr[0] ^= idx << 2;
8475 if (!nl80211_addr_in_use(drv->global, new_addr))
8476 break;
8477 }
8478 if (idx == 64)
8479 return -1;
8480
8481 wpa_printf(MSG_DEBUG, "nl80211: Assigned new P2P Interface Address "
8482 MACSTR, MAC2STR(new_addr));
8483
8484 return 0;
8485}
8486
482856c8
JM
8487#endif /* CONFIG_P2P */
8488
f2ed8023 8489
7ab68865 8490static int wpa_driver_nl80211_if_add(void *priv, enum wpa_driver_if_type type,
8043e725 8491 const char *ifname, const u8 *addr,
f3585c8a 8492 void *bss_ctx, void **drv_priv,
e17a2477
JM
8493 char *force_ifname, u8 *if_addr,
8494 const char *bridge)
22a7c9d7 8495{
a2e40bb6
FF
8496 struct i802_bss *bss = priv;
8497 struct wpa_driver_nl80211_data *drv = bss->drv;
22a7c9d7
JM
8498 int ifidx;
8499#ifdef HOSTAPD
a2e40bb6 8500 struct i802_bss *new_bss = NULL;
22a7c9d7
JM
8501
8502 if (type == WPA_IF_AP_BSS) {
a2e40bb6
FF
8503 new_bss = os_zalloc(sizeof(*new_bss));
8504 if (new_bss == NULL)
22a7c9d7
JM
8505 return -1;
8506 }
8507#endif /* HOSTAPD */
8508
f3585c8a
JM
8509 if (addr)
8510 os_memcpy(if_addr, addr, ETH_ALEN);
22a7c9d7 8511 ifidx = nl80211_create_iface(drv, ifname,
fbbfcbac
FF
8512 wpa_driver_nl80211_if_type(type), addr,
8513 0);
22a7c9d7
JM
8514 if (ifidx < 0) {
8515#ifdef HOSTAPD
a2e40bb6 8516 os_free(new_bss);
22a7c9d7
JM
8517#endif /* HOSTAPD */
8518 return -1;
8519 }
8520
f3585c8a 8521 if (!addr &&
c81eff1a
BG
8522 linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
8523 if_addr) < 0) {
c55f774d 8524 nl80211_remove_iface(drv, ifidx);
f3585c8a 8525 return -1;
c55f774d
JM
8526 }
8527
8528#ifdef CONFIG_P2P
8529 if (!addr &&
8530 (type == WPA_IF_P2P_CLIENT || type == WPA_IF_P2P_GROUP ||
8531 type == WPA_IF_P2P_GO)) {
8532 /* Enforce unique P2P Interface Address */
8533 u8 new_addr[ETH_ALEN], own_addr[ETH_ALEN];
8534
c81eff1a
BG
8535 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
8536 own_addr) < 0 ||
8537 linux_get_ifhwaddr(drv->global->ioctl_sock, ifname,
8538 new_addr) < 0) {
c55f774d
JM
8539 nl80211_remove_iface(drv, ifidx);
8540 return -1;
8541 }
8542 if (os_memcmp(own_addr, new_addr, ETH_ALEN) == 0) {
8543 wpa_printf(MSG_DEBUG, "nl80211: Allocate new address "
8544 "for P2P group interface");
f2ed8023 8545 if (nl80211_p2p_interface_addr(drv, new_addr) < 0) {
c55f774d
JM
8546 nl80211_remove_iface(drv, ifidx);
8547 return -1;
8548 }
c81eff1a 8549 if (linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
c55f774d
JM
8550 new_addr) < 0) {
8551 nl80211_remove_iface(drv, ifidx);
8552 return -1;
8553 }
c55f774d 8554 }
f67eeb5c 8555 os_memcpy(if_addr, new_addr, ETH_ALEN);
c55f774d
JM
8556 }
8557#endif /* CONFIG_P2P */
f3585c8a 8558
22a7c9d7 8559#ifdef HOSTAPD
e17a2477
JM
8560 if (bridge &&
8561 i802_check_bridge(drv, new_bss, bridge, ifname) < 0) {
8562 wpa_printf(MSG_ERROR, "nl80211: Failed to add the new "
8563 "interface %s to a bridge %s", ifname, bridge);
8564 nl80211_remove_iface(drv, ifidx);
8565 os_free(new_bss);
8566 return -1;
8567 }
8568
22a7c9d7 8569 if (type == WPA_IF_AP_BSS) {
c81eff1a
BG
8570 if (linux_set_iface_flags(drv->global->ioctl_sock, ifname, 1))
8571 {
34f2f814 8572 nl80211_remove_iface(drv, ifidx);
07179987 8573 os_free(new_bss);
22a7c9d7
JM
8574 return -1;
8575 }
a2e40bb6 8576 os_strlcpy(new_bss->ifname, ifname, IFNAMSIZ);
341eebee 8577 os_memcpy(new_bss->addr, if_addr, ETH_ALEN);
a2e40bb6
FF
8578 new_bss->ifindex = ifidx;
8579 new_bss->drv = drv;
8580 new_bss->next = drv->first_bss.next;
a7a6af4c 8581 new_bss->freq = drv->first_bss.freq;
a5e1eb20 8582 new_bss->ctx = bss_ctx;
a2e40bb6
FF
8583 drv->first_bss.next = new_bss;
8584 if (drv_priv)
8585 *drv_priv = new_bss;
cc7a48d1 8586 nl80211_init_bss(new_bss);
3dd1d890
YAP
8587
8588 /* Subscribe management frames for this WPA_IF_AP_BSS */
8589 if (nl80211_setup_ap(new_bss))
8590 return -1;
22a7c9d7
JM
8591 }
8592#endif /* HOSTAPD */
8593
ff6a158b
JM
8594 if (drv->global)
8595 drv->global->if_add_ifindex = ifidx;
8596
22a7c9d7
JM
8597 return 0;
8598}
8599
8600
9ebce9c5 8601static int wpa_driver_nl80211_if_remove(struct i802_bss *bss,
22a7c9d7
JM
8602 enum wpa_driver_if_type type,
8603 const char *ifname)
8604{
a2e40bb6 8605 struct wpa_driver_nl80211_data *drv = bss->drv;
22a7c9d7
JM
8606 int ifindex = if_nametoindex(ifname);
8607
e748062b
JM
8608 wpa_printf(MSG_DEBUG, "nl80211: %s(type=%d ifname=%s) ifindex=%d",
8609 __func__, type, ifname, ifindex);
8610 if (ifindex <= 0)
8611 return -1;
e17a2477 8612
c34e618d
FF
8613 nl80211_remove_iface(drv, ifindex);
8614
e17a2477 8615#ifdef HOSTAPD
c34e618d
FF
8616 if (type != WPA_IF_AP_BSS)
8617 return 0;
8618
e17a2477 8619 if (bss->added_if_into_bridge) {
c81eff1a
BG
8620 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
8621 bss->ifname) < 0)
e17a2477
JM
8622 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
8623 "interface %s from bridge %s: %s",
8624 bss->ifname, bss->brname, strerror(errno));
8625 }
8626 if (bss->added_bridge) {
c81eff1a 8627 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
e17a2477
JM
8628 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
8629 "bridge %s: %s",
8630 bss->brname, strerror(errno));
8631 }
a2e40bb6
FF
8632
8633 if (bss != &drv->first_bss) {
8546ea19 8634 struct i802_bss *tbss;
a2e40bb6 8635
8546ea19
JM
8636 for (tbss = &drv->first_bss; tbss; tbss = tbss->next) {
8637 if (tbss->next == bss) {
8638 tbss->next = bss->next;
3dd1d890
YAP
8639 /* Unsubscribe management frames */
8640 nl80211_teardown_ap(bss);
cc7a48d1 8641 nl80211_destroy_bss(bss);
8546ea19
JM
8642 os_free(bss);
8643 bss = NULL;
8644 break;
8645 }
22a7c9d7 8646 }
8546ea19
JM
8647 if (bss)
8648 wpa_printf(MSG_INFO, "nl80211: %s - could not find "
8649 "BSS %p in the list", __func__, bss);
22a7c9d7
JM
8650 }
8651#endif /* HOSTAPD */
8652
8653 return 0;
8654}
8655
8656
55777702
JM
8657static int cookie_handler(struct nl_msg *msg, void *arg)
8658{
8659 struct nlattr *tb[NL80211_ATTR_MAX + 1];
8660 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
8661 u64 *cookie = arg;
8662 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
8663 genlmsg_attrlen(gnlh, 0), NULL);
8664 if (tb[NL80211_ATTR_COOKIE])
8665 *cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
8666 return NL_SKIP;
8667}
8668
8669
88df0ef7 8670static int nl80211_send_frame_cmd(struct i802_bss *bss,
5dfca53f
JB
8671 unsigned int freq, unsigned int wait,
8672 const u8 *buf, size_t buf_len,
88df0ef7
JB
8673 u64 *cookie_out, int no_cck, int no_ack,
8674 int offchanok)
9884f9cc 8675{
88df0ef7 8676 struct wpa_driver_nl80211_data *drv = bss->drv;
9884f9cc
JB
8677 struct nl_msg *msg;
8678 u64 cookie;
8679 int ret = -1;
8680
8681 msg = nlmsg_alloc();
8682 if (!msg)
8683 return -1;
8684
cc2ada86 8685 wpa_printf(MSG_MSGDUMP, "nl80211: CMD_FRAME freq=%u wait=%u no_cck=%d "
2e3e4566
JM
8686 "no_ack=%d offchanok=%d",
8687 freq, wait, no_cck, no_ack, offchanok);
9fb04070 8688 nl80211_cmd(drv, msg, 0, NL80211_CMD_FRAME);
9884f9cc 8689
88df0ef7 8690 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
9884f9cc 8691 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
9db931ed
JM
8692 if (wait)
8693 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, wait);
b9fd8ce8 8694 if (offchanok && (drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX))
88df0ef7 8695 NLA_PUT_FLAG(msg, NL80211_ATTR_OFFCHANNEL_TX_OK);
b106173a
JM
8696 if (no_cck)
8697 NLA_PUT_FLAG(msg, NL80211_ATTR_TX_NO_CCK_RATE);
ddc53271
JM
8698 if (no_ack)
8699 NLA_PUT_FLAG(msg, NL80211_ATTR_DONT_WAIT_FOR_ACK);
970fa12e 8700
9884f9cc
JB
8701 NLA_PUT(msg, NL80211_ATTR_FRAME, buf_len, buf);
8702
8703 cookie = 0;
8704 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
8705 msg = NULL;
8706 if (ret) {
8707 wpa_printf(MSG_DEBUG, "nl80211: Frame command failed: ret=%d "
a05225c8
JM
8708 "(%s) (freq=%u wait=%u)", ret, strerror(-ret),
8709 freq, wait);
9884f9cc
JB
8710 goto nla_put_failure;
8711 }
cc2ada86 8712 wpa_printf(MSG_MSGDUMP, "nl80211: Frame TX command accepted%s; "
ddc53271
JM
8713 "cookie 0x%llx", no_ack ? " (no ACK)" : "",
8714 (long long unsigned int) cookie);
9884f9cc
JB
8715
8716 if (cookie_out)
ddc53271 8717 *cookie_out = no_ack ? (u64) -1 : cookie;
9884f9cc
JB
8718
8719nla_put_failure:
8720 nlmsg_free(msg);
8721 return ret;
8722}
8723
8724
9ebce9c5
JM
8725static int wpa_driver_nl80211_send_action(struct i802_bss *bss,
8726 unsigned int freq,
190b9062 8727 unsigned int wait_time,
58f6fbe0
JM
8728 const u8 *dst, const u8 *src,
8729 const u8 *bssid,
b106173a
JM
8730 const u8 *data, size_t data_len,
8731 int no_cck)
58f6fbe0 8732{
a2e40bb6 8733 struct wpa_driver_nl80211_data *drv = bss->drv;
58f6fbe0 8734 int ret = -1;
58f6fbe0
JM
8735 u8 *buf;
8736 struct ieee80211_hdr *hdr;
58f6fbe0 8737
5dfca53f 8738 wpa_printf(MSG_DEBUG, "nl80211: Send Action frame (ifindex=%d, "
55231068
JM
8739 "freq=%u MHz wait=%d ms no_cck=%d)",
8740 drv->ifindex, freq, wait_time, no_cck);
58f6fbe0
JM
8741
8742 buf = os_zalloc(24 + data_len);
8743 if (buf == NULL)
8744 return ret;
8745 os_memcpy(buf + 24, data, data_len);
8746 hdr = (struct ieee80211_hdr *) buf;
8747 hdr->frame_control =
8748 IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_ACTION);
8749 os_memcpy(hdr->addr1, dst, ETH_ALEN);
8750 os_memcpy(hdr->addr2, src, ETH_ALEN);
8751 os_memcpy(hdr->addr3, bssid, ETH_ALEN);
8752
b1f625e0 8753 if (is_ap_interface(drv->nlmode))
9ebce9c5
JM
8754 ret = wpa_driver_nl80211_send_mlme(bss, buf, 24 + data_len,
8755 0, freq, no_cck, 1,
8756 wait_time);
9884f9cc 8757 else
88df0ef7 8758 ret = nl80211_send_frame_cmd(bss, freq, wait_time, buf,
5dfca53f 8759 24 + data_len,
b106173a 8760 &drv->send_action_cookie,
88df0ef7 8761 no_cck, 0, 1);
58f6fbe0 8762
f8bf1421 8763 os_free(buf);
58f6fbe0
JM
8764 return ret;
8765}
8766
8767
5dfca53f
JB
8768static void wpa_driver_nl80211_send_action_cancel_wait(void *priv)
8769{
8770 struct i802_bss *bss = priv;
8771 struct wpa_driver_nl80211_data *drv = bss->drv;
8772 struct nl_msg *msg;
8773 int ret;
8774
8775 msg = nlmsg_alloc();
8776 if (!msg)
8777 return;
8778
316a9e4d
JM
8779 wpa_printf(MSG_DEBUG, "nl80211: Cancel TX frame wait: cookie=0x%llx",
8780 (long long unsigned int) drv->send_action_cookie);
9fb04070 8781 nl80211_cmd(drv, msg, 0, NL80211_CMD_FRAME_WAIT_CANCEL);
5dfca53f
JB
8782
8783 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
8784 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->send_action_cookie);
8785
8786 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8787 msg = NULL;
8788 if (ret)
8789 wpa_printf(MSG_DEBUG, "nl80211: wait cancel failed: ret=%d "
8790 "(%s)", ret, strerror(-ret));
8791
8792 nla_put_failure:
8793 nlmsg_free(msg);
8794}
8795
8796
55777702
JM
8797static int wpa_driver_nl80211_remain_on_channel(void *priv, unsigned int freq,
8798 unsigned int duration)
8799{
a2e40bb6
FF
8800 struct i802_bss *bss = priv;
8801 struct wpa_driver_nl80211_data *drv = bss->drv;
55777702
JM
8802 struct nl_msg *msg;
8803 int ret;
8804 u64 cookie;
8805
8806 msg = nlmsg_alloc();
8807 if (!msg)
8808 return -1;
8809
9fb04070 8810 nl80211_cmd(drv, msg, 0, NL80211_CMD_REMAIN_ON_CHANNEL);
55777702
JM
8811
8812 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
8813 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
8814 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
8815
8816 cookie = 0;
8817 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
5883168a 8818 msg = NULL;
55777702
JM
8819 if (ret == 0) {
8820 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel cookie "
8821 "0x%llx for freq=%u MHz duration=%u",
8822 (long long unsigned int) cookie, freq, duration);
8823 drv->remain_on_chan_cookie = cookie;
531f0331 8824 drv->pending_remain_on_chan = 1;
55777702
JM
8825 return 0;
8826 }
8827 wpa_printf(MSG_DEBUG, "nl80211: Failed to request remain-on-channel "
15ed5535
JM
8828 "(freq=%d duration=%u): %d (%s)",
8829 freq, duration, ret, strerror(-ret));
55777702 8830nla_put_failure:
5883168a 8831 nlmsg_free(msg);
55777702
JM
8832 return -1;
8833}
8834
8835
8836static int wpa_driver_nl80211_cancel_remain_on_channel(void *priv)
8837{
a2e40bb6
FF
8838 struct i802_bss *bss = priv;
8839 struct wpa_driver_nl80211_data *drv = bss->drv;
55777702
JM
8840 struct nl_msg *msg;
8841 int ret;
8842
8843 if (!drv->pending_remain_on_chan) {
8844 wpa_printf(MSG_DEBUG, "nl80211: No pending remain-on-channel "
8845 "to cancel");
8846 return -1;
8847 }
8848
8849 wpa_printf(MSG_DEBUG, "nl80211: Cancel remain-on-channel with cookie "
8850 "0x%llx",
8851 (long long unsigned int) drv->remain_on_chan_cookie);
8852
8853 msg = nlmsg_alloc();
8854 if (!msg)
8855 return -1;
8856
9fb04070 8857 nl80211_cmd(drv, msg, 0, NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL);
55777702
JM
8858
8859 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
8860 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->remain_on_chan_cookie);
8861
8862 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5883168a 8863 msg = NULL;
55777702
JM
8864 if (ret == 0)
8865 return 0;
8866 wpa_printf(MSG_DEBUG, "nl80211: Failed to cancel remain-on-channel: "
8867 "%d (%s)", ret, strerror(-ret));
8868nla_put_failure:
5883168a 8869 nlmsg_free(msg);
55777702
JM
8870 return -1;
8871}
8872
8873
9ebce9c5 8874static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss, int report)
504e905c 8875{
a2e40bb6 8876 struct wpa_driver_nl80211_data *drv = bss->drv;
504e905c 8877
5582a5d1 8878 if (!report) {
0d891981
JM
8879 if (bss->nl_preq && drv->device_ap_sme &&
8880 is_ap_interface(drv->nlmode)) {
8881 /*
8882 * Do not disable Probe Request reporting that was
8883 * enabled in nl80211_setup_ap().
8884 */
8885 wpa_printf(MSG_DEBUG, "nl80211: Skip disabling of "
8886 "Probe Request reporting nl_preq=%p while "
8887 "in AP mode", bss->nl_preq);
8888 } else if (bss->nl_preq) {
36488c05
JM
8889 wpa_printf(MSG_DEBUG, "nl80211: Disable Probe Request "
8890 "reporting nl_preq=%p", bss->nl_preq);
5582a5d1 8891 eloop_unregister_read_sock(
481234cf 8892 nl_socket_get_fd(bss->nl_preq));
221a59c9 8893 nl_destroy_handles(&bss->nl_preq);
5582a5d1
JB
8894 }
8895 return 0;
8896 }
8897
481234cf 8898 if (bss->nl_preq) {
5582a5d1 8899 wpa_printf(MSG_DEBUG, "nl80211: Probe Request reporting "
36488c05 8900 "already on! nl_preq=%p", bss->nl_preq);
5582a5d1
JB
8901 return 0;
8902 }
8903
481234cf
JM
8904 bss->nl_preq = nl_create_handle(drv->global->nl_cb, "preq");
8905 if (bss->nl_preq == NULL)
5582a5d1 8906 return -1;
36488c05
JM
8907 wpa_printf(MSG_DEBUG, "nl80211: Enable Probe Request "
8908 "reporting nl_preq=%p", bss->nl_preq);
5582a5d1 8909
481234cf 8910 if (nl80211_register_frame(bss, bss->nl_preq,
5582a5d1
JB
8911 (WLAN_FC_TYPE_MGMT << 2) |
8912 (WLAN_FC_STYPE_PROBE_REQ << 4),
a92dfde8
JB
8913 NULL, 0) < 0)
8914 goto out_err;
5582a5d1 8915
481234cf 8916 eloop_register_read_sock(nl_socket_get_fd(bss->nl_preq),
a11241fa 8917 wpa_driver_nl80211_event_receive, bss->nl_cb,
481234cf 8918 bss->nl_preq);
5582a5d1 8919
504e905c 8920 return 0;
5582a5d1 8921
a92dfde8 8922 out_err:
221a59c9 8923 nl_destroy_handles(&bss->nl_preq);
5582a5d1 8924 return -1;
504e905c
JM
8925}
8926
8927
4e5cb1a3
JM
8928static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
8929 int ifindex, int disabled)
8930{
8931 struct nl_msg *msg;
8932 struct nlattr *bands, *band;
8933 int ret;
8934
8935 msg = nlmsg_alloc();
8936 if (!msg)
8937 return -1;
8938
9fb04070 8939 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_TX_BITRATE_MASK);
4e5cb1a3
JM
8940 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
8941
8942 bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES);
8943 if (!bands)
8944 goto nla_put_failure;
8945
8946 /*
8947 * Disable 2 GHz rates 1, 2, 5.5, 11 Mbps by masking out everything
8948 * else apart from 6, 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS
8949 * rates. All 5 GHz rates are left enabled.
8950 */
8951 band = nla_nest_start(msg, NL80211_BAND_2GHZ);
8952 if (!band)
8953 goto nla_put_failure;
1dea5882
JM
8954 if (disabled) {
8955 NLA_PUT(msg, NL80211_TXRATE_LEGACY, 8,
8956 "\x0c\x12\x18\x24\x30\x48\x60\x6c");
8957 }
4e5cb1a3
JM
8958 nla_nest_end(msg, band);
8959
8960 nla_nest_end(msg, bands);
8961
8962 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8963 msg = NULL;
8964 if (ret) {
8965 wpa_printf(MSG_DEBUG, "nl80211: Set TX rates failed: ret=%d "
8966 "(%s)", ret, strerror(-ret));
1d0c6fb1
JM
8967 } else
8968 drv->disabled_11b_rates = disabled;
4e5cb1a3
JM
8969
8970 return ret;
8971
8972nla_put_failure:
8973 nlmsg_free(msg);
8974 return -1;
8975}
8976
8977
af473088
JM
8978static int wpa_driver_nl80211_deinit_ap(void *priv)
8979{
a2e40bb6
FF
8980 struct i802_bss *bss = priv;
8981 struct wpa_driver_nl80211_data *drv = bss->drv;
b1f625e0 8982 if (!is_ap_interface(drv->nlmode))
af473088
JM
8983 return -1;
8984 wpa_driver_nl80211_del_beacon(drv);
b1f625e0 8985 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
af473088
JM
8986}
8987
8988
695c7038
SW
8989static int wpa_driver_nl80211_stop_ap(void *priv)
8990{
8991 struct i802_bss *bss = priv;
8992 struct wpa_driver_nl80211_data *drv = bss->drv;
8993 if (!is_ap_interface(drv->nlmode))
8994 return -1;
8995 wpa_driver_nl80211_del_beacon(drv);
8996 bss->beacon_set = 0;
8997 return 0;
8998}
8999
9000
3c29244e
EP
9001static int wpa_driver_nl80211_deinit_p2p_cli(void *priv)
9002{
9003 struct i802_bss *bss = priv;
9004 struct wpa_driver_nl80211_data *drv = bss->drv;
9005 if (drv->nlmode != NL80211_IFTYPE_P2P_CLIENT)
9006 return -1;
9007 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
9008}
9009
9010
207ef3fb
JM
9011static void wpa_driver_nl80211_resume(void *priv)
9012{
a2e40bb6
FF
9013 struct i802_bss *bss = priv;
9014 struct wpa_driver_nl80211_data *drv = bss->drv;
c81eff1a 9015 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1)) {
207ef3fb
JM
9016 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface up on "
9017 "resume event");
9018 }
9019}
9020
9021
7b90c16a
JM
9022static int nl80211_send_ft_action(void *priv, u8 action, const u8 *target_ap,
9023 const u8 *ies, size_t ies_len)
9024{
9025 struct i802_bss *bss = priv;
9026 struct wpa_driver_nl80211_data *drv = bss->drv;
9027 int ret;
9028 u8 *data, *pos;
9029 size_t data_len;
341eebee 9030 const u8 *own_addr = bss->addr;
7b90c16a
JM
9031
9032 if (action != 1) {
9033 wpa_printf(MSG_ERROR, "nl80211: Unsupported send_ft_action "
9034 "action %d", action);
9035 return -1;
9036 }
9037
9038 /*
9039 * Action frame payload:
9040 * Category[1] = 6 (Fast BSS Transition)
9041 * Action[1] = 1 (Fast BSS Transition Request)
9042 * STA Address
9043 * Target AP Address
9044 * FT IEs
9045 */
9046
73fc617d
JM
9047 data_len = 2 + 2 * ETH_ALEN + ies_len;
9048 data = os_malloc(data_len);
7b90c16a
JM
9049 if (data == NULL)
9050 return -1;
9051 pos = data;
9052 *pos++ = 0x06; /* FT Action category */
9053 *pos++ = action;
9054 os_memcpy(pos, own_addr, ETH_ALEN);
9055 pos += ETH_ALEN;
9056 os_memcpy(pos, target_ap, ETH_ALEN);
9057 pos += ETH_ALEN;
9058 os_memcpy(pos, ies, ies_len);
9059
190b9062
JB
9060 ret = wpa_driver_nl80211_send_action(bss, drv->assoc_freq, 0,
9061 drv->bssid, own_addr, drv->bssid,
b106173a 9062 data, data_len, 0);
7b90c16a
JM
9063 os_free(data);
9064
9065 return ret;
9066}
9067
9068
b625473c
JM
9069static int nl80211_signal_monitor(void *priv, int threshold, int hysteresis)
9070{
9071 struct i802_bss *bss = priv;
9072 struct wpa_driver_nl80211_data *drv = bss->drv;
8970bae8
JB
9073 struct nl_msg *msg;
9074 struct nlattr *cqm;
f0494d0f 9075 int ret = -1;
b625473c
JM
9076
9077 wpa_printf(MSG_DEBUG, "nl80211: Signal monitor threshold=%d "
9078 "hysteresis=%d", threshold, hysteresis);
9079
9080 msg = nlmsg_alloc();
9081 if (!msg)
9082 return -1;
9083
9fb04070 9084 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_CQM);
b625473c
JM
9085
9086 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
9087
8970bae8 9088 cqm = nla_nest_start(msg, NL80211_ATTR_CQM);
b625473c 9089 if (cqm == NULL)
21270bb4 9090 goto nla_put_failure;
b625473c 9091
8970bae8
JB
9092 NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_THOLD, threshold);
9093 NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_HYST, hysteresis);
9094 nla_nest_end(msg, cqm);
21270bb4 9095
f0494d0f 9096 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
b625473c
JM
9097 msg = NULL;
9098
9099nla_put_failure:
b625473c 9100 nlmsg_free(msg);
f0494d0f 9101 return ret;
b625473c
JM
9102}
9103
9104
1c5c7273
PS
9105static int nl80211_signal_poll(void *priv, struct wpa_signal_info *si)
9106{
9107 struct i802_bss *bss = priv;
9108 struct wpa_driver_nl80211_data *drv = bss->drv;
9109 int res;
9110
9111 os_memset(si, 0, sizeof(*si));
9112 res = nl80211_get_link_signal(drv, si);
9113 if (res != 0)
9114 return res;
9115
9116 return nl80211_get_link_noise(drv, si);
9117}
9118
9119
57ebba59
JJ
9120static int wpa_driver_nl80211_shared_freq(void *priv)
9121{
9122 struct i802_bss *bss = priv;
9123 struct wpa_driver_nl80211_data *drv = bss->drv;
9124 struct wpa_driver_nl80211_data *driver;
9125 int freq = 0;
9126
9127 /*
9128 * If the same PHY is in connected state with some other interface,
9129 * then retrieve the assoc freq.
9130 */
9131 wpa_printf(MSG_DEBUG, "nl80211: Get shared freq for PHY %s",
9132 drv->phyname);
9133
9134 dl_list_for_each(driver, &drv->global->interfaces,
9135 struct wpa_driver_nl80211_data, list) {
9136 if (drv == driver ||
9137 os_strcmp(drv->phyname, driver->phyname) != 0 ||
9138 !driver->associated)
9139 continue;
9140
9141 wpa_printf(MSG_DEBUG, "nl80211: Found a match for PHY %s - %s "
9142 MACSTR,
9143 driver->phyname, driver->first_bss.ifname,
341eebee 9144 MAC2STR(driver->first_bss.addr));
d3bd0f05
JJ
9145 if (is_ap_interface(driver->nlmode))
9146 freq = driver->first_bss.freq;
9147 else
9148 freq = nl80211_get_assoc_freq(driver);
57ebba59
JJ
9149 wpa_printf(MSG_DEBUG, "nl80211: Shared freq for PHY %s: %d",
9150 drv->phyname, freq);
9151 }
9152
9153 if (!freq)
9154 wpa_printf(MSG_DEBUG, "nl80211: No shared interface for "
9155 "PHY (%s) in associated state", drv->phyname);
9156
9157 return freq;
9158}
9159
9160
b91ab76e
JM
9161static int nl80211_send_frame(void *priv, const u8 *data, size_t data_len,
9162 int encrypt)
9163{
9164 struct i802_bss *bss = priv;
55231068
JM
9165 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt, 0,
9166 0, 0, 0, 0);
b91ab76e
JM
9167}
9168
9169
c55f774d
JM
9170static int nl80211_set_param(void *priv, const char *param)
9171{
c55f774d
JM
9172 wpa_printf(MSG_DEBUG, "nl80211: driver param='%s'", param);
9173 if (param == NULL)
9174 return 0;
9175
9176#ifdef CONFIG_P2P
9177 if (os_strstr(param, "use_p2p_group_interface=1")) {
482856c8
JM
9178 struct i802_bss *bss = priv;
9179 struct wpa_driver_nl80211_data *drv = bss->drv;
9180
c55f774d
JM
9181 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
9182 "interface");
9183 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
9184 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
9185 }
9186#endif /* CONFIG_P2P */
9187
9188 return 0;
9189}
9190
9191
f2ed8023
JM
9192static void * nl80211_global_init(void)
9193{
9194 struct nl80211_global *global;
36d84860
BG
9195 struct netlink_config *cfg;
9196
f2ed8023
JM
9197 global = os_zalloc(sizeof(*global));
9198 if (global == NULL)
9199 return NULL;
c81eff1a 9200 global->ioctl_sock = -1;
f2ed8023 9201 dl_list_init(&global->interfaces);
ff6a158b 9202 global->if_add_ifindex = -1;
36d84860
BG
9203
9204 cfg = os_zalloc(sizeof(*cfg));
9205 if (cfg == NULL)
9206 goto err;
9207
9208 cfg->ctx = global;
9209 cfg->newlink_cb = wpa_driver_nl80211_event_rtm_newlink;
9210 cfg->dellink_cb = wpa_driver_nl80211_event_rtm_dellink;
9211 global->netlink = netlink_init(cfg);
9212 if (global->netlink == NULL) {
9213 os_free(cfg);
9214 goto err;
9215 }
9216
2a7b66f5
BG
9217 if (wpa_driver_nl80211_init_nl_global(global) < 0)
9218 goto err;
9219
c81eff1a
BG
9220 global->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
9221 if (global->ioctl_sock < 0) {
9222 perror("socket(PF_INET,SOCK_DGRAM)");
9223 goto err;
9224 }
9225
f2ed8023 9226 return global;
36d84860
BG
9227
9228err:
9229 nl80211_global_deinit(global);
9230 return NULL;
f2ed8023
JM
9231}
9232
9233
9234static void nl80211_global_deinit(void *priv)
9235{
9236 struct nl80211_global *global = priv;
9237 if (global == NULL)
9238 return;
9239 if (!dl_list_empty(&global->interfaces)) {
9240 wpa_printf(MSG_ERROR, "nl80211: %u interface(s) remain at "
9241 "nl80211_global_deinit",
9242 dl_list_len(&global->interfaces));
9243 }
36d84860
BG
9244
9245 if (global->netlink)
9246 netlink_deinit(global->netlink);
9247
276e2d67
BG
9248 nl_destroy_handles(&global->nl);
9249
481234cf 9250 if (global->nl_event) {
d6c9aab8 9251 eloop_unregister_read_sock(
481234cf 9252 nl_socket_get_fd(global->nl_event));
d6c9aab8
JB
9253 nl_destroy_handles(&global->nl_event);
9254 }
9255
9256 nl_cb_put(global->nl_cb);
2a7b66f5 9257
c81eff1a
BG
9258 if (global->ioctl_sock >= 0)
9259 close(global->ioctl_sock);
9260
f2ed8023
JM
9261 os_free(global);
9262}
9263
9264
6859f1cb
BG
9265static const char * nl80211_get_radio_name(void *priv)
9266{
9267 struct i802_bss *bss = priv;
9268 struct wpa_driver_nl80211_data *drv = bss->drv;
9269 return drv->phyname;
9270}
9271
9272
a6efc65d
JM
9273static int nl80211_pmkid(struct i802_bss *bss, int cmd, const u8 *bssid,
9274 const u8 *pmkid)
9275{
9276 struct nl_msg *msg;
9277
9278 msg = nlmsg_alloc();
9279 if (!msg)
9280 return -ENOMEM;
9281
9fb04070 9282 nl80211_cmd(bss->drv, msg, 0, cmd);
a6efc65d
JM
9283
9284 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
9285 if (pmkid)
9286 NLA_PUT(msg, NL80211_ATTR_PMKID, 16, pmkid);
9287 if (bssid)
9288 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
9289
9290 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
9291 nla_put_failure:
9e088e74 9292 nlmsg_free(msg);
a6efc65d
JM
9293 return -ENOBUFS;
9294}
9295
9296
9297static int nl80211_add_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
9298{
9299 struct i802_bss *bss = priv;
9300 wpa_printf(MSG_DEBUG, "nl80211: Add PMKID for " MACSTR, MAC2STR(bssid));
9301 return nl80211_pmkid(bss, NL80211_CMD_SET_PMKSA, bssid, pmkid);
9302}
9303
9304
9305static int nl80211_remove_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
9306{
9307 struct i802_bss *bss = priv;
9308 wpa_printf(MSG_DEBUG, "nl80211: Delete PMKID for " MACSTR,
9309 MAC2STR(bssid));
9310 return nl80211_pmkid(bss, NL80211_CMD_DEL_PMKSA, bssid, pmkid);
9311}
9312
9313
9314static int nl80211_flush_pmkid(void *priv)
9315{
9316 struct i802_bss *bss = priv;
9317 wpa_printf(MSG_DEBUG, "nl80211: Flush PMKIDs");
9318 return nl80211_pmkid(bss, NL80211_CMD_FLUSH_PMKSA, NULL, NULL);
9319}
9320
9321
b14a210c
JB
9322static void nl80211_set_rekey_info(void *priv, const u8 *kek, const u8 *kck,
9323 const u8 *replay_ctr)
9324{
9325 struct i802_bss *bss = priv;
9326 struct wpa_driver_nl80211_data *drv = bss->drv;
9327 struct nlattr *replay_nested;
9328 struct nl_msg *msg;
9329
9330 msg = nlmsg_alloc();
9331 if (!msg)
9332 return;
9333
9fb04070 9334 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
b14a210c
JB
9335
9336 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
9337
9338 replay_nested = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
9339 if (!replay_nested)
9340 goto nla_put_failure;
9341
9342 NLA_PUT(msg, NL80211_REKEY_DATA_KEK, NL80211_KEK_LEN, kek);
9343 NLA_PUT(msg, NL80211_REKEY_DATA_KCK, NL80211_KCK_LEN, kck);
9344 NLA_PUT(msg, NL80211_REKEY_DATA_REPLAY_CTR, NL80211_REPLAY_CTR_LEN,
9345 replay_ctr);
9346
9347 nla_nest_end(msg, replay_nested);
9348
9349 send_and_recv_msgs(drv, msg, NULL, NULL);
9350 return;
9351 nla_put_failure:
9352 nlmsg_free(msg);
9353}
9354
9355
39718852
JB
9356static void nl80211_send_null_frame(struct i802_bss *bss, const u8 *own_addr,
9357 const u8 *addr, int qos)
bcf24348 9358{
39718852
JB
9359 /* send data frame to poll STA and check whether
9360 * this frame is ACKed */
bcf24348
JB
9361 struct {
9362 struct ieee80211_hdr hdr;
9363 u16 qos_ctl;
9364 } STRUCT_PACKED nulldata;
9365 size_t size;
9366
9367 /* Send data frame to poll STA and check whether this frame is ACKed */
9368
9369 os_memset(&nulldata, 0, sizeof(nulldata));
9370
9371 if (qos) {
9372 nulldata.hdr.frame_control =
9373 IEEE80211_FC(WLAN_FC_TYPE_DATA,
9374 WLAN_FC_STYPE_QOS_NULL);
9375 size = sizeof(nulldata);
9376 } else {
9377 nulldata.hdr.frame_control =
9378 IEEE80211_FC(WLAN_FC_TYPE_DATA,
9379 WLAN_FC_STYPE_NULLFUNC);
9380 size = sizeof(struct ieee80211_hdr);
9381 }
9382
9383 nulldata.hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS);
9384 os_memcpy(nulldata.hdr.IEEE80211_DA_FROMDS, addr, ETH_ALEN);
9385 os_memcpy(nulldata.hdr.IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
9386 os_memcpy(nulldata.hdr.IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
9387
9ebce9c5
JM
9388 if (wpa_driver_nl80211_send_mlme(bss, (u8 *) &nulldata, size, 0, 0, 0,
9389 0, 0) < 0)
bcf24348
JB
9390 wpa_printf(MSG_DEBUG, "nl80211_send_null_frame: Failed to "
9391 "send poll frame");
9392}
9393
39718852
JB
9394static void nl80211_poll_client(void *priv, const u8 *own_addr, const u8 *addr,
9395 int qos)
9396{
9397 struct i802_bss *bss = priv;
9398 struct wpa_driver_nl80211_data *drv = bss->drv;
9399 struct nl_msg *msg;
9400
9401 if (!drv->poll_command_supported) {
9402 nl80211_send_null_frame(bss, own_addr, addr, qos);
9403 return;
9404 }
9405
9406 msg = nlmsg_alloc();
9407 if (!msg)
9408 return;
9409
9410 nl80211_cmd(drv, msg, 0, NL80211_CMD_PROBE_CLIENT);
9411
9412 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
9413 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
9414
9415 send_and_recv_msgs(drv, msg, NULL, NULL);
9416 return;
9417 nla_put_failure:
9418 nlmsg_free(msg);
9419}
9420
bcf24348 9421
29f338af
JM
9422static int nl80211_set_power_save(struct i802_bss *bss, int enabled)
9423{
9424 struct nl_msg *msg;
9425
9426 msg = nlmsg_alloc();
9427 if (!msg)
9428 return -ENOMEM;
9429
9430 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_SET_POWER_SAVE);
9431 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
9432 NLA_PUT_U32(msg, NL80211_ATTR_PS_STATE,
9433 enabled ? NL80211_PS_ENABLED : NL80211_PS_DISABLED);
9434 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
9435nla_put_failure:
9436 nlmsg_free(msg);
9437 return -ENOBUFS;
9438}
9439
9440
9441static int nl80211_set_p2p_powersave(void *priv, int legacy_ps, int opp_ps,
9442 int ctwindow)
9443{
9444 struct i802_bss *bss = priv;
9445
9446 wpa_printf(MSG_DEBUG, "nl80211: set_p2p_powersave (legacy_ps=%d "
9447 "opp_ps=%d ctwindow=%d)", legacy_ps, opp_ps, ctwindow);
9448
9449 if (opp_ps != -1 || ctwindow != -1)
9450 return -1; /* Not yet supported */
9451
9452 if (legacy_ps == -1)
9453 return 0;
9454 if (legacy_ps != 0 && legacy_ps != 1)
9455 return -1; /* Not yet supported */
9456
9457 return nl80211_set_power_save(bss, legacy_ps);
9458}
9459
9460
f90e9c1c
SW
9461static int nl80211_start_radar_detection(void *priv, int freq)
9462{
9463 struct i802_bss *bss = priv;
9464 struct wpa_driver_nl80211_data *drv = bss->drv;
9465 struct nl_msg *msg;
9466 int ret;
9467
9468 wpa_printf(MSG_DEBUG, "nl80211: Start radar detection (CAC)");
9469 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_RADAR)) {
9470 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support radar "
9471 "detection");
9472 return -1;
9473 }
9474
9475 msg = nlmsg_alloc();
9476 if (!msg)
9477 return -1;
9478
9479 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_RADAR_DETECT);
9480 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
9481 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
9482
9483 /* only HT20 is supported at this point */
9484 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, NL80211_CHAN_HT20);
9485
9486 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9487 if (ret == 0)
9488 return 0;
9489 wpa_printf(MSG_DEBUG, "nl80211: Failed to start radar detection: "
9490 "%d (%s)", ret, strerror(-ret));
9491nla_put_failure:
9492 return -1;
9493}
9494
03ea1786
AN
9495#ifdef CONFIG_TDLS
9496
9497static int nl80211_send_tdls_mgmt(void *priv, const u8 *dst, u8 action_code,
9498 u8 dialog_token, u16 status_code,
9499 const u8 *buf, size_t len)
9500{
9501 struct i802_bss *bss = priv;
9502 struct wpa_driver_nl80211_data *drv = bss->drv;
9503 struct nl_msg *msg;
9504
9505 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
9506 return -EOPNOTSUPP;
9507
9508 if (!dst)
9509 return -EINVAL;
9510
9511 msg = nlmsg_alloc();
9512 if (!msg)
9513 return -ENOMEM;
9514
9515 nl80211_cmd(drv, msg, 0, NL80211_CMD_TDLS_MGMT);
9516 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
9517 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
9518 NLA_PUT_U8(msg, NL80211_ATTR_TDLS_ACTION, action_code);
9519 NLA_PUT_U8(msg, NL80211_ATTR_TDLS_DIALOG_TOKEN, dialog_token);
9520 NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status_code);
9521 NLA_PUT(msg, NL80211_ATTR_IE, len, buf);
9522
9523 return send_and_recv_msgs(drv, msg, NULL, NULL);
9524
9525nla_put_failure:
9526 nlmsg_free(msg);
9527 return -ENOBUFS;
9528}
9529
9530
9531static int nl80211_tdls_oper(void *priv, enum tdls_oper oper, const u8 *peer)
9532{
9533 struct i802_bss *bss = priv;
9534 struct wpa_driver_nl80211_data *drv = bss->drv;
9535 struct nl_msg *msg;
9536 enum nl80211_tdls_operation nl80211_oper;
9537
9538 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
9539 return -EOPNOTSUPP;
9540
9541 switch (oper) {
9542 case TDLS_DISCOVERY_REQ:
9543 nl80211_oper = NL80211_TDLS_DISCOVERY_REQ;
9544 break;
9545 case TDLS_SETUP:
9546 nl80211_oper = NL80211_TDLS_SETUP;
9547 break;
9548 case TDLS_TEARDOWN:
9549 nl80211_oper = NL80211_TDLS_TEARDOWN;
9550 break;
9551 case TDLS_ENABLE_LINK:
9552 nl80211_oper = NL80211_TDLS_ENABLE_LINK;
9553 break;
9554 case TDLS_DISABLE_LINK:
9555 nl80211_oper = NL80211_TDLS_DISABLE_LINK;
9556 break;
9557 case TDLS_ENABLE:
9558 return 0;
9559 case TDLS_DISABLE:
9560 return 0;
9561 default:
9562 return -EINVAL;
9563 }
9564
9565 msg = nlmsg_alloc();
9566 if (!msg)
9567 return -ENOMEM;
9568
9569 nl80211_cmd(drv, msg, 0, NL80211_CMD_TDLS_OPER);
9570 NLA_PUT_U8(msg, NL80211_ATTR_TDLS_OPERATION, nl80211_oper);
9571 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
9572 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, peer);
9573
9574 return send_and_recv_msgs(drv, msg, NULL, NULL);
9575
9576nla_put_failure:
9577 nlmsg_free(msg);
9578 return -ENOBUFS;
9579}
9580
9581#endif /* CONFIG TDLS */
9582
9583
216eede8
DS
9584#ifdef ANDROID
9585
9586typedef struct android_wifi_priv_cmd {
9587 char *buf;
9588 int used_len;
9589 int total_len;
9590} android_wifi_priv_cmd;
9591
9592static int drv_errors = 0;
9593
9594static void wpa_driver_send_hang_msg(struct wpa_driver_nl80211_data *drv)
9595{
9596 drv_errors++;
9597 if (drv_errors > DRV_NUMBER_SEQUENTIAL_ERRORS) {
9598 drv_errors = 0;
9599 wpa_msg(drv->ctx, MSG_INFO, WPA_EVENT_DRIVER_STATE "HANGED");
9600 }
9601}
9602
9603
9604static int android_priv_cmd(struct i802_bss *bss, const char *cmd)
9605{
9606 struct wpa_driver_nl80211_data *drv = bss->drv;
9607 struct ifreq ifr;
9608 android_wifi_priv_cmd priv_cmd;
9609 char buf[MAX_DRV_CMD_SIZE];
9610 int ret;
9611
9612 os_memset(&ifr, 0, sizeof(ifr));
9613 os_memset(&priv_cmd, 0, sizeof(priv_cmd));
9614 os_strlcpy(ifr.ifr_name, bss->ifname, IFNAMSIZ);
9615
9616 os_memset(buf, 0, sizeof(buf));
9617 os_strlcpy(buf, cmd, sizeof(buf));
9618
9619 priv_cmd.buf = buf;
9620 priv_cmd.used_len = sizeof(buf);
9621 priv_cmd.total_len = sizeof(buf);
9622 ifr.ifr_data = &priv_cmd;
9623
9624 ret = ioctl(drv->global->ioctl_sock, SIOCDEVPRIVATE + 1, &ifr);
9625 if (ret < 0) {
9626 wpa_printf(MSG_ERROR, "%s: failed to issue private commands",
9627 __func__);
9628 wpa_driver_send_hang_msg(drv);
9629 return ret;
9630 }
9631
9632 drv_errors = 0;
9633 return 0;
9634}
9635
9636
9637static int android_pno_start(struct i802_bss *bss,
9638 struct wpa_driver_scan_params *params)
9639{
9640 struct wpa_driver_nl80211_data *drv = bss->drv;
9641 struct ifreq ifr;
9642 android_wifi_priv_cmd priv_cmd;
9643 int ret = 0, i = 0, bp;
9644 char buf[WEXT_PNO_MAX_COMMAND_SIZE];
9645
9646 bp = WEXT_PNOSETUP_HEADER_SIZE;
9647 os_memcpy(buf, WEXT_PNOSETUP_HEADER, bp);
9648 buf[bp++] = WEXT_PNO_TLV_PREFIX;
9649 buf[bp++] = WEXT_PNO_TLV_VERSION;
9650 buf[bp++] = WEXT_PNO_TLV_SUBVERSION;
9651 buf[bp++] = WEXT_PNO_TLV_RESERVED;
9652
9653 while (i < WEXT_PNO_AMOUNT && (size_t) i < params->num_ssids) {
9654 /* Check that there is enough space needed for 1 more SSID, the
9655 * other sections and null termination */
9656 if ((bp + WEXT_PNO_SSID_HEADER_SIZE + MAX_SSID_LEN +
9657 WEXT_PNO_NONSSID_SECTIONS_SIZE + 1) >= (int) sizeof(buf))
9658 break;
9659 wpa_hexdump_ascii(MSG_DEBUG, "For PNO Scan",
a97bde0a
JM
9660 params->ssids[i].ssid,
9661 params->ssids[i].ssid_len);
216eede8
DS
9662 buf[bp++] = WEXT_PNO_SSID_SECTION;
9663 buf[bp++] = params->ssids[i].ssid_len;
9664 os_memcpy(&buf[bp], params->ssids[i].ssid,
9665 params->ssids[i].ssid_len);
9666 bp += params->ssids[i].ssid_len;
9667 i++;
9668 }
9669
9670 buf[bp++] = WEXT_PNO_SCAN_INTERVAL_SECTION;
9671 os_snprintf(&buf[bp], WEXT_PNO_SCAN_INTERVAL_LENGTH + 1, "%x",
9672 WEXT_PNO_SCAN_INTERVAL);
9673 bp += WEXT_PNO_SCAN_INTERVAL_LENGTH;
9674
9675 buf[bp++] = WEXT_PNO_REPEAT_SECTION;
9676 os_snprintf(&buf[bp], WEXT_PNO_REPEAT_LENGTH + 1, "%x",
9677 WEXT_PNO_REPEAT);
9678 bp += WEXT_PNO_REPEAT_LENGTH;
9679
9680 buf[bp++] = WEXT_PNO_MAX_REPEAT_SECTION;
9681 os_snprintf(&buf[bp], WEXT_PNO_MAX_REPEAT_LENGTH + 1, "%x",
9682 WEXT_PNO_MAX_REPEAT);
9683 bp += WEXT_PNO_MAX_REPEAT_LENGTH + 1;
9684
9685 memset(&ifr, 0, sizeof(ifr));
9686 memset(&priv_cmd, 0, sizeof(priv_cmd));
9687 os_strncpy(ifr.ifr_name, bss->ifname, IFNAMSIZ);
9688
9689 priv_cmd.buf = buf;
9690 priv_cmd.used_len = bp;
9691 priv_cmd.total_len = bp;
9692 ifr.ifr_data = &priv_cmd;
9693
9694 ret = ioctl(drv->global->ioctl_sock, SIOCDEVPRIVATE + 1, &ifr);
9695
9696 if (ret < 0) {
9697 wpa_printf(MSG_ERROR, "ioctl[SIOCSIWPRIV] (pnosetup): %d",
9698 ret);
9699 wpa_driver_send_hang_msg(drv);
9700 return ret;
9701 }
9702
9703 drv_errors = 0;
9704
9705 return android_priv_cmd(bss, "PNOFORCE 1");
9706}
9707
9708
9709static int android_pno_stop(struct i802_bss *bss)
9710{
9711 return android_priv_cmd(bss, "PNOFORCE 0");
9712}
9713
9714#endif /* ANDROID */
9715
9716
9ebce9c5
JM
9717static int driver_nl80211_set_key(const char *ifname, void *priv,
9718 enum wpa_alg alg, const u8 *addr,
9719 int key_idx, int set_tx,
9720 const u8 *seq, size_t seq_len,
9721 const u8 *key, size_t key_len)
9722{
9723 struct i802_bss *bss = priv;
9724 return wpa_driver_nl80211_set_key(ifname, bss, alg, addr, key_idx,
9725 set_tx, seq, seq_len, key, key_len);
9726}
9727
9728
9729static int driver_nl80211_scan2(void *priv,
9730 struct wpa_driver_scan_params *params)
9731{
9732 struct i802_bss *bss = priv;
9733 return wpa_driver_nl80211_scan(bss, params);
9734}
9735
9736
9737static int driver_nl80211_deauthenticate(void *priv, const u8 *addr,
9738 int reason_code)
9739{
9740 struct i802_bss *bss = priv;
9741 return wpa_driver_nl80211_deauthenticate(bss, addr, reason_code);
9742}
9743
9744
9745static int driver_nl80211_authenticate(void *priv,
9746 struct wpa_driver_auth_params *params)
9747{
9748 struct i802_bss *bss = priv;
9749 return wpa_driver_nl80211_authenticate(bss, params);
9750}
9751
9752
9753static void driver_nl80211_deinit(void *priv)
9754{
9755 struct i802_bss *bss = priv;
9756 wpa_driver_nl80211_deinit(bss);
9757}
9758
9759
9760static int driver_nl80211_if_remove(void *priv, enum wpa_driver_if_type type,
9761 const char *ifname)
9762{
9763 struct i802_bss *bss = priv;
9764 return wpa_driver_nl80211_if_remove(bss, type, ifname);
9765}
9766
9767
9768static int driver_nl80211_send_mlme(void *priv, const u8 *data,
9769 size_t data_len, int noack)
9770{
9771 struct i802_bss *bss = priv;
9772 return wpa_driver_nl80211_send_mlme(bss, data, data_len, noack,
9773 0, 0, 0, 0);
9774}
9775
9776
9777static int driver_nl80211_sta_remove(void *priv, const u8 *addr)
9778{
9779 struct i802_bss *bss = priv;
9780 return wpa_driver_nl80211_sta_remove(bss, addr);
9781}
9782
9783
9784#if defined(HOSTAPD) || defined(CONFIG_AP)
9785static int driver_nl80211_set_sta_vlan(void *priv, const u8 *addr,
9786 const char *ifname, int vlan_id)
9787{
9788 struct i802_bss *bss = priv;
9789 return i802_set_sta_vlan(bss, addr, ifname, vlan_id);
9790}
9791#endif /* HOSTAPD || CONFIG_AP */
9792
9793
9794static int driver_nl80211_read_sta_data(void *priv,
9795 struct hostap_sta_driver_data *data,
9796 const u8 *addr)
9797{
9798 struct i802_bss *bss = priv;
9799 return i802_read_sta_data(bss, data, addr);
9800}
9801
9802
9803static int driver_nl80211_send_action(void *priv, unsigned int freq,
9804 unsigned int wait_time,
9805 const u8 *dst, const u8 *src,
9806 const u8 *bssid,
9807 const u8 *data, size_t data_len,
9808 int no_cck)
9809{
9810 struct i802_bss *bss = priv;
9811 return wpa_driver_nl80211_send_action(bss, freq, wait_time, dst, src,
9812 bssid, data, data_len, no_cck);
9813}
9814
9815
9816static int driver_nl80211_probe_req_report(void *priv, int report)
9817{
9818 struct i802_bss *bss = priv;
9819 return wpa_driver_nl80211_probe_req_report(bss, report);
9820}
9821
9822
6a1ce395
DG
9823static int wpa_driver_nl80211_update_ft_ies(void *priv, const u8 *md,
9824 const u8 *ies, size_t ies_len)
9825{
9826 int ret;
9827 struct nl_msg *msg;
9828 struct i802_bss *bss = priv;
9829 struct wpa_driver_nl80211_data *drv = bss->drv;
9830 u16 mdid = WPA_GET_LE16(md);
9831
9832 msg = nlmsg_alloc();
9833 if (!msg)
9834 return -ENOMEM;
9835
9836 wpa_printf(MSG_DEBUG, "nl80211: Updating FT IEs");
9837 nl80211_cmd(drv, msg, 0, NL80211_CMD_UPDATE_FT_IES);
9838 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
9839 NLA_PUT(msg, NL80211_ATTR_IE, ies_len, ies);
9840 NLA_PUT_U16(msg, NL80211_ATTR_MDID, mdid);
9841
9842 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9843 if (ret) {
9844 wpa_printf(MSG_DEBUG, "nl80211: update_ft_ies failed "
9845 "err=%d (%s)", ret, strerror(-ret));
9846 }
9847
9848 return ret;
9849
9850nla_put_failure:
9851 nlmsg_free(msg);
9852 return -ENOBUFS;
9853}
9854
9855
3f5285e8
JM
9856const struct wpa_driver_ops wpa_driver_nl80211_ops = {
9857 .name = "nl80211",
9858 .desc = "Linux nl80211/cfg80211",
9859 .get_bssid = wpa_driver_nl80211_get_bssid,
9860 .get_ssid = wpa_driver_nl80211_get_ssid,
9ebce9c5
JM
9861 .set_key = driver_nl80211_set_key,
9862 .scan2 = driver_nl80211_scan2,
d21c63b9
LC
9863 .sched_scan = wpa_driver_nl80211_sched_scan,
9864 .stop_sched_scan = wpa_driver_nl80211_stop_sched_scan,
3f5285e8 9865 .get_scan_results2 = wpa_driver_nl80211_get_scan_results,
9ebce9c5
JM
9866 .deauthenticate = driver_nl80211_deauthenticate,
9867 .authenticate = driver_nl80211_authenticate,
3f5285e8 9868 .associate = wpa_driver_nl80211_associate,
f2ed8023
JM
9869 .global_init = nl80211_global_init,
9870 .global_deinit = nl80211_global_deinit,
9871 .init2 = wpa_driver_nl80211_init,
9ebce9c5 9872 .deinit = driver_nl80211_deinit,
3f5285e8
JM
9873 .get_capa = wpa_driver_nl80211_get_capa,
9874 .set_operstate = wpa_driver_nl80211_set_operstate,
01652550 9875 .set_supp_port = wpa_driver_nl80211_set_supp_port,
6d158490 9876 .set_country = wpa_driver_nl80211_set_country,
19c3b566 9877 .set_ap = wpa_driver_nl80211_set_ap,
22a7c9d7 9878 .if_add = wpa_driver_nl80211_if_add,
9ebce9c5
JM
9879 .if_remove = driver_nl80211_if_remove,
9880 .send_mlme = driver_nl80211_send_mlme,
c3965310 9881 .get_hw_feature_data = wpa_driver_nl80211_get_hw_feature_data,
0f4e8b4f 9882 .sta_add = wpa_driver_nl80211_sta_add,
9ebce9c5 9883 .sta_remove = driver_nl80211_sta_remove,
db149ac9 9884 .hapd_send_eapol = wpa_driver_nl80211_hapd_send_eapol,
a8d6ffa4 9885 .sta_set_flags = wpa_driver_nl80211_sta_set_flags,
c5121837
JM
9886#ifdef HOSTAPD
9887 .hapd_init = i802_init,
c5121837 9888 .hapd_deinit = i802_deinit,
f7b3920c
JM
9889 .set_wds_sta = i802_set_wds_sta,
9890#endif /* HOSTAPD */
9891#if defined(HOSTAPD) || defined(CONFIG_AP)
c5121837
JM
9892 .get_seqnum = i802_get_seqnum,
9893 .flush = i802_flush,
c5121837
JM
9894 .get_inact_sec = i802_get_inact_sec,
9895 .sta_clear_stats = i802_sta_clear_stats,
c5121837
JM
9896 .set_rts = i802_set_rts,
9897 .set_frag = i802_set_frag,
c5121837 9898 .set_tx_queue_params = i802_set_tx_queue_params,
9ebce9c5 9899 .set_sta_vlan = driver_nl80211_set_sta_vlan,
ee7ab173
JB
9900 .sta_deauth = i802_sta_deauth,
9901 .sta_disassoc = i802_sta_disassoc,
9902#endif /* HOSTAPD || CONFIG_AP */
9ebce9c5 9903 .read_sta_data = driver_nl80211_read_sta_data,
e3802622 9904 .set_freq = i802_set_freq,
9ebce9c5 9905 .send_action = driver_nl80211_send_action,
5dfca53f 9906 .send_action_cancel_wait = wpa_driver_nl80211_send_action_cancel_wait,
55777702
JM
9907 .remain_on_channel = wpa_driver_nl80211_remain_on_channel,
9908 .cancel_remain_on_channel =
9909 wpa_driver_nl80211_cancel_remain_on_channel,
9ebce9c5 9910 .probe_req_report = driver_nl80211_probe_req_report,
af473088 9911 .deinit_ap = wpa_driver_nl80211_deinit_ap,
3c29244e 9912 .deinit_p2p_cli = wpa_driver_nl80211_deinit_p2p_cli,
207ef3fb 9913 .resume = wpa_driver_nl80211_resume,
7b90c16a 9914 .send_ft_action = nl80211_send_ft_action,
b625473c 9915 .signal_monitor = nl80211_signal_monitor,
1c5c7273 9916 .signal_poll = nl80211_signal_poll,
b91ab76e 9917 .send_frame = nl80211_send_frame,
57ebba59 9918 .shared_freq = wpa_driver_nl80211_shared_freq,
c55f774d 9919 .set_param = nl80211_set_param,
6859f1cb 9920 .get_radio_name = nl80211_get_radio_name,
a6efc65d
JM
9921 .add_pmkid = nl80211_add_pmkid,
9922 .remove_pmkid = nl80211_remove_pmkid,
9923 .flush_pmkid = nl80211_flush_pmkid,
b14a210c 9924 .set_rekey_info = nl80211_set_rekey_info,
bcf24348 9925 .poll_client = nl80211_poll_client,
29f338af 9926 .set_p2p_powersave = nl80211_set_p2p_powersave,
f90e9c1c 9927 .start_dfs_cac = nl80211_start_radar_detection,
695c7038 9928 .stop_ap = wpa_driver_nl80211_stop_ap,
03ea1786
AN
9929#ifdef CONFIG_TDLS
9930 .send_tdls_mgmt = nl80211_send_tdls_mgmt,
9931 .tdls_oper = nl80211_tdls_oper,
9932#endif /* CONFIG_TDLS */
6a1ce395 9933 .update_ft_ies = wpa_driver_nl80211_update_ft_ies,
3f5285e8 9934};