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