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