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