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