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