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