blob: 7e3de511151a1a40db2b35acd022549573e1abb9 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * Driver interaction with Linux nl80211/cfg80211
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003 * Copyright (c) 2002-2014, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 * Copyright (c) 2003-2004, Instant802 Networks, Inc.
5 * Copyright (c) 2005-2006, Devicescape Software, Inc.
6 * Copyright (c) 2007, Johannes Berg <johannes@sipsolutions.net>
7 * Copyright (c) 2009-2010, Atheros Communications
8 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08009 * This software may be distributed under the terms of the BSD license.
10 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011 */
12
13#include "includes.h"
14#include <sys/ioctl.h>
15#include <sys/types.h>
16#include <sys/stat.h>
17#include <fcntl.h>
18#include <net/if.h>
19#include <netlink/genl/genl.h>
20#include <netlink/genl/family.h>
21#include <netlink/genl/ctrl.h>
22#include <linux/rtnetlink.h>
23#include <netpacket/packet.h>
24#include <linux/filter.h>
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080025#include <linux/errqueue.h>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070026#include "nl80211_copy.h"
27
28#include "common.h"
29#include "eloop.h"
30#include "utils/list.h"
Dmitry Shmidtcf32e602014-01-28 10:57:39 -080031#include "common/qca-vendor.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070032#include "common/ieee802_11_defs.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080033#include "common/ieee802_11_common.h"
34#include "l2_packet/l2_packet.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070035#include "netlink.h"
36#include "linux_ioctl.h"
37#include "radiotap.h"
38#include "radiotap_iter.h"
39#include "rfkill.h"
40#include "driver.h"
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080041
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080042#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
62#ifdef ANDROID
63#include "android_drv.h"
64#endif /* ANDROID */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070065#ifdef CONFIG_LIBNL20
66/* libnl 2.0 compatibility code */
67#define nl_handle nl_sock
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}
110#endif /* CONFIG_LIBNL20 */
111
112
Dmitry Shmidt54605472013-11-08 11:10:19 -0800113#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
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800124static struct nl_handle * nl_create_handle(struct nl_cb *cb, const char *dbg)
125{
126 struct nl_handle *handle;
127
128 handle = nl80211_handle_alloc(cb);
129 if (handle == NULL) {
130 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
131 "callbacks (%s)", dbg);
132 return NULL;
133 }
134
135 if (genl_connect(handle)) {
136 wpa_printf(MSG_ERROR, "nl80211: Failed to connect to generic "
137 "netlink (%s)", dbg);
138 nl80211_handle_destroy(handle);
139 return NULL;
140 }
141
142 return handle;
143}
144
145
146static void nl_destroy_handles(struct nl_handle **handle)
147{
148 if (*handle == NULL)
149 return;
150 nl80211_handle_destroy(*handle);
151 *handle = NULL;
152}
153
154
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700155#if __WORDSIZE == 64
156#define ELOOP_SOCKET_INVALID (intptr_t) 0x8888888888888889ULL
157#else
158#define ELOOP_SOCKET_INVALID (intptr_t) 0x88888889ULL
159#endif
160
161static void nl80211_register_eloop_read(struct nl_handle **handle,
162 eloop_sock_handler handler,
163 void *eloop_data)
164{
165 nl_socket_set_nonblocking(*handle);
166 eloop_register_read_sock(nl_socket_get_fd(*handle), handler,
167 eloop_data, *handle);
168 *handle = (void *) (((intptr_t) *handle) ^ ELOOP_SOCKET_INVALID);
169}
170
171
172static void nl80211_destroy_eloop_handle(struct nl_handle **handle)
173{
174 *handle = (void *) (((intptr_t) *handle) ^ ELOOP_SOCKET_INVALID);
175 eloop_unregister_read_sock(nl_socket_get_fd(*handle));
176 nl_destroy_handles(handle);
177}
178
179
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700180#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
194struct nl80211_global {
195 struct dl_list interfaces;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800196 int if_add_ifindex;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700197 u64 if_add_wdevid;
198 int if_add_wdevid_set;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800199 struct netlink_data *netlink;
200 struct nl_cb *nl_cb;
201 struct nl_handle *nl;
202 int nl80211_id;
203 int ioctl_sock; /* socket for ioctl() use */
204
205 struct nl_handle *nl_event;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700206};
207
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800208struct nl80211_wiphy_data {
209 struct dl_list list;
210 struct dl_list bsss;
211 struct dl_list drvs;
212
213 struct nl_handle *nl_beacons;
214 struct nl_cb *nl_cb;
215
216 int wiphy_idx;
217};
218
219static void nl80211_global_deinit(void *priv);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800220
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700221struct i802_bss {
222 struct wpa_driver_nl80211_data *drv;
223 struct i802_bss *next;
224 int ifindex;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700225 u64 wdev_id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700226 char ifname[IFNAMSIZ + 1];
227 char brname[IFNAMSIZ];
228 unsigned int beacon_set:1;
229 unsigned int added_if_into_bridge:1;
230 unsigned int added_bridge:1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700231 unsigned int in_deinit:1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700232 unsigned int wdev_id_set:1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800233 unsigned int added_if:1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800234
235 u8 addr[ETH_ALEN];
236
237 int freq;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700238 int if_dynamic;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800239
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800240 void *ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800241 struct nl_handle *nl_preq, *nl_mgmt;
242 struct nl_cb *nl_cb;
243
244 struct nl80211_wiphy_data *wiphy_data;
245 struct dl_list wiphy_list;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700246};
247
248struct wpa_driver_nl80211_data {
249 struct nl80211_global *global;
250 struct dl_list list;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800251 struct dl_list wiphy_list;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700252 char phyname[32];
253 void *ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700254 int ifindex;
255 int if_removed;
256 int if_disabled;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800257 int ignore_if_down_event;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700258 struct rfkill_data *rfkill;
259 struct wpa_driver_capa capa;
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700260 u8 *extended_capa, *extended_capa_mask;
261 unsigned int extended_capa_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700262 int has_capability;
263
264 int operstate;
265
266 int scan_complete_events;
Dmitry Shmidt56052862013-10-04 10:23:25 -0700267 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;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700272
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700273 struct nl_cb *nl_cb;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700274
275 u8 auth_bssid[ETH_ALEN];
Dmitry Shmidt8bae4132013-06-06 11:25:10 -0700276 u8 auth_attempt_bssid[ETH_ALEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700277 u8 bssid[ETH_ALEN];
Dmitry Shmidt8bae4132013-06-06 11:25:10 -0700278 u8 prev_bssid[ETH_ALEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700279 int associated;
280 u8 ssid[32];
281 size_t ssid_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800282 enum nl80211_iftype nlmode;
283 enum nl80211_iftype ap_scan_as_station;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700284 unsigned int assoc_freq;
285
286 int monitor_sock;
287 int monitor_ifidx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800288 int monitor_refcount;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700289
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800290 unsigned int disabled_11b_rates:1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700291 unsigned int pending_remain_on_chan:1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800292 unsigned int in_interface_list:1;
293 unsigned int device_ap_sme:1;
294 unsigned int poll_command_supported:1;
295 unsigned int data_tx_status:1;
296 unsigned int scan_for_auth:1;
297 unsigned int retry_auth:1;
298 unsigned int use_monitor:1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800299 unsigned int ignore_next_local_disconnect:1;
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -0700300 unsigned int ignore_next_local_deauth:1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700301 unsigned int allow_p2p_device:1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800302 unsigned int hostapd:1;
303 unsigned int start_mode_ap:1;
304 unsigned int start_iface_up:1;
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800305 unsigned int test_use_roc_tx:1;
Dmitry Shmidt98660862014-03-11 17:26:21 -0700306 unsigned int ignore_deauth_event:1;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700307 unsigned int dfs_vendor_cmd_avail:1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700308
309 u64 remain_on_chan_cookie;
310 u64 send_action_cookie;
311
312 unsigned int last_mgmt_freq;
313
314 struct wpa_driver_scan_filter *filter_ssids;
315 size_t num_filter_ssids;
316
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800317 struct i802_bss *first_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700318
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800319 int eapol_tx_sock;
320
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700321 int eapol_sock; /* socket for EAPOL frames */
322
323 int default_if_indices[16];
324 int *if_indices;
325 int num_if_indices;
326
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800327 /* From failed authentication command */
328 int auth_freq;
329 u8 auth_bssid_[ETH_ALEN];
330 u8 auth_ssid[32];
331 size_t auth_ssid_len;
332 int auth_alg;
333 u8 *auth_ie;
334 size_t auth_ie_len;
335 u8 auth_wep_key[4][16];
336 size_t auth_wep_key_len[4];
337 int auth_wep_tx_keyidx;
338 int auth_local_state_change;
339 int auth_p2p;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700340};
341
342
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800343static void wpa_driver_nl80211_deinit(struct i802_bss *bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700344static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx,
345 void *timeout_ctx);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800346static int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
347 enum nl80211_iftype nlmode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700348static int
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800349wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv,
350 const u8 *set_addr, int first);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700351static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
352 const u8 *addr, int cmd, u16 reason_code,
353 int local_state_change);
354static void nl80211_remove_monitor_interface(
355 struct wpa_driver_nl80211_data *drv);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800356static int nl80211_send_frame_cmd(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700357 unsigned int freq, unsigned int wait,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800358 const u8 *buf, size_t buf_len, u64 *cookie,
359 int no_cck, int no_ack, int offchanok);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700360static int nl80211_register_frame(struct i802_bss *bss,
361 struct nl_handle *hl_handle,
362 u16 type, const u8 *match, size_t match_len);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800363static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss,
364 int report);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800365#ifdef ANDROID
366static int android_pno_start(struct i802_bss *bss,
367 struct wpa_driver_scan_params *params);
368static int android_pno_stop(struct i802_bss *bss);
Dmitry Shmidt292b0c32013-11-22 12:54:42 -0800369extern int wpa_driver_nl80211_driver_cmd(void *priv, char *cmd, char *buf,
370 size_t buf_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800371#endif /* ANDROID */
372#ifdef ANDROID_P2P
Dmitry Shmidt6e933c12011-09-27 12:29:26 -0700373int wpa_driver_set_p2p_noa(void *priv, u8 count, int start, int duration);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800374int wpa_driver_get_p2p_noa(void *priv, u8 *buf, size_t len);
Dmitry Shmidt6e933c12011-09-27 12:29:26 -0700375int wpa_driver_set_p2p_ps(void *priv, int legacy_ps, int opp_ps, int ctwindow);
376int wpa_driver_set_ap_wps_p2p_ie(void *priv, const struct wpabuf *beacon,
Dmitry Shmidt292b0c32013-11-22 12:54:42 -0800377 const struct wpabuf *proberesp,
378 const struct wpabuf *assocresp);
379#endif /* ANDROID_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700380
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700381static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
382static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
383static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800384static int wpa_driver_nl80211_if_remove(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700385 enum wpa_driver_if_type type,
386 const char *ifname);
Dmitry Shmidt738a26e2011-07-07 14:22:14 -0700387
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800388static int wpa_driver_nl80211_set_freq(struct i802_bss *bss,
389 struct hostapd_freq_params *freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700390static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
391 int ifindex, int disabled);
392
393static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800394static int wpa_driver_nl80211_authenticate_retry(
395 struct wpa_driver_nl80211_data *drv);
396
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700397static int i802_set_iface_flags(struct i802_bss *bss, int up);
398
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800399
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700400static const char * nl80211_command_to_string(enum nl80211_commands cmd)
401{
402#define C2S(x) case x: return #x;
403 switch (cmd) {
404 C2S(NL80211_CMD_UNSPEC)
405 C2S(NL80211_CMD_GET_WIPHY)
406 C2S(NL80211_CMD_SET_WIPHY)
407 C2S(NL80211_CMD_NEW_WIPHY)
408 C2S(NL80211_CMD_DEL_WIPHY)
409 C2S(NL80211_CMD_GET_INTERFACE)
410 C2S(NL80211_CMD_SET_INTERFACE)
411 C2S(NL80211_CMD_NEW_INTERFACE)
412 C2S(NL80211_CMD_DEL_INTERFACE)
413 C2S(NL80211_CMD_GET_KEY)
414 C2S(NL80211_CMD_SET_KEY)
415 C2S(NL80211_CMD_NEW_KEY)
416 C2S(NL80211_CMD_DEL_KEY)
417 C2S(NL80211_CMD_GET_BEACON)
418 C2S(NL80211_CMD_SET_BEACON)
419 C2S(NL80211_CMD_START_AP)
420 C2S(NL80211_CMD_STOP_AP)
421 C2S(NL80211_CMD_GET_STATION)
422 C2S(NL80211_CMD_SET_STATION)
423 C2S(NL80211_CMD_NEW_STATION)
424 C2S(NL80211_CMD_DEL_STATION)
425 C2S(NL80211_CMD_GET_MPATH)
426 C2S(NL80211_CMD_SET_MPATH)
427 C2S(NL80211_CMD_NEW_MPATH)
428 C2S(NL80211_CMD_DEL_MPATH)
429 C2S(NL80211_CMD_SET_BSS)
430 C2S(NL80211_CMD_SET_REG)
431 C2S(NL80211_CMD_REQ_SET_REG)
432 C2S(NL80211_CMD_GET_MESH_CONFIG)
433 C2S(NL80211_CMD_SET_MESH_CONFIG)
434 C2S(NL80211_CMD_SET_MGMT_EXTRA_IE)
435 C2S(NL80211_CMD_GET_REG)
436 C2S(NL80211_CMD_GET_SCAN)
437 C2S(NL80211_CMD_TRIGGER_SCAN)
438 C2S(NL80211_CMD_NEW_SCAN_RESULTS)
439 C2S(NL80211_CMD_SCAN_ABORTED)
440 C2S(NL80211_CMD_REG_CHANGE)
441 C2S(NL80211_CMD_AUTHENTICATE)
442 C2S(NL80211_CMD_ASSOCIATE)
443 C2S(NL80211_CMD_DEAUTHENTICATE)
444 C2S(NL80211_CMD_DISASSOCIATE)
445 C2S(NL80211_CMD_MICHAEL_MIC_FAILURE)
446 C2S(NL80211_CMD_REG_BEACON_HINT)
447 C2S(NL80211_CMD_JOIN_IBSS)
448 C2S(NL80211_CMD_LEAVE_IBSS)
449 C2S(NL80211_CMD_TESTMODE)
450 C2S(NL80211_CMD_CONNECT)
451 C2S(NL80211_CMD_ROAM)
452 C2S(NL80211_CMD_DISCONNECT)
453 C2S(NL80211_CMD_SET_WIPHY_NETNS)
454 C2S(NL80211_CMD_GET_SURVEY)
455 C2S(NL80211_CMD_NEW_SURVEY_RESULTS)
456 C2S(NL80211_CMD_SET_PMKSA)
457 C2S(NL80211_CMD_DEL_PMKSA)
458 C2S(NL80211_CMD_FLUSH_PMKSA)
459 C2S(NL80211_CMD_REMAIN_ON_CHANNEL)
460 C2S(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL)
461 C2S(NL80211_CMD_SET_TX_BITRATE_MASK)
462 C2S(NL80211_CMD_REGISTER_FRAME)
463 C2S(NL80211_CMD_FRAME)
464 C2S(NL80211_CMD_FRAME_TX_STATUS)
465 C2S(NL80211_CMD_SET_POWER_SAVE)
466 C2S(NL80211_CMD_GET_POWER_SAVE)
467 C2S(NL80211_CMD_SET_CQM)
468 C2S(NL80211_CMD_NOTIFY_CQM)
469 C2S(NL80211_CMD_SET_CHANNEL)
470 C2S(NL80211_CMD_SET_WDS_PEER)
471 C2S(NL80211_CMD_FRAME_WAIT_CANCEL)
472 C2S(NL80211_CMD_JOIN_MESH)
473 C2S(NL80211_CMD_LEAVE_MESH)
474 C2S(NL80211_CMD_UNPROT_DEAUTHENTICATE)
475 C2S(NL80211_CMD_UNPROT_DISASSOCIATE)
476 C2S(NL80211_CMD_NEW_PEER_CANDIDATE)
477 C2S(NL80211_CMD_GET_WOWLAN)
478 C2S(NL80211_CMD_SET_WOWLAN)
479 C2S(NL80211_CMD_START_SCHED_SCAN)
480 C2S(NL80211_CMD_STOP_SCHED_SCAN)
481 C2S(NL80211_CMD_SCHED_SCAN_RESULTS)
482 C2S(NL80211_CMD_SCHED_SCAN_STOPPED)
483 C2S(NL80211_CMD_SET_REKEY_OFFLOAD)
484 C2S(NL80211_CMD_PMKSA_CANDIDATE)
485 C2S(NL80211_CMD_TDLS_OPER)
486 C2S(NL80211_CMD_TDLS_MGMT)
487 C2S(NL80211_CMD_UNEXPECTED_FRAME)
488 C2S(NL80211_CMD_PROBE_CLIENT)
489 C2S(NL80211_CMD_REGISTER_BEACONS)
490 C2S(NL80211_CMD_UNEXPECTED_4ADDR_FRAME)
491 C2S(NL80211_CMD_SET_NOACK_MAP)
492 C2S(NL80211_CMD_CH_SWITCH_NOTIFY)
493 C2S(NL80211_CMD_START_P2P_DEVICE)
494 C2S(NL80211_CMD_STOP_P2P_DEVICE)
495 C2S(NL80211_CMD_CONN_FAILED)
496 C2S(NL80211_CMD_SET_MCAST_RATE)
497 C2S(NL80211_CMD_SET_MAC_ACL)
498 C2S(NL80211_CMD_RADAR_DETECT)
499 C2S(NL80211_CMD_GET_PROTOCOL_FEATURES)
500 C2S(NL80211_CMD_UPDATE_FT_IES)
501 C2S(NL80211_CMD_FT_EVENT)
502 C2S(NL80211_CMD_CRIT_PROTOCOL_START)
503 C2S(NL80211_CMD_CRIT_PROTOCOL_STOP)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800504 C2S(NL80211_CMD_GET_COALESCE)
505 C2S(NL80211_CMD_SET_COALESCE)
506 C2S(NL80211_CMD_CHANNEL_SWITCH)
507 C2S(NL80211_CMD_VENDOR)
508 C2S(NL80211_CMD_SET_QOS_MAP)
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700509 default:
510 return "NL80211_CMD_UNKNOWN";
511 }
512#undef C2S
513}
514
515
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800516/* Converts nl80211_chan_width to a common format */
517static enum chan_width convert2width(int width)
518{
519 switch (width) {
520 case NL80211_CHAN_WIDTH_20_NOHT:
521 return CHAN_WIDTH_20_NOHT;
522 case NL80211_CHAN_WIDTH_20:
523 return CHAN_WIDTH_20;
524 case NL80211_CHAN_WIDTH_40:
525 return CHAN_WIDTH_40;
526 case NL80211_CHAN_WIDTH_80:
527 return CHAN_WIDTH_80;
528 case NL80211_CHAN_WIDTH_80P80:
529 return CHAN_WIDTH_80P80;
530 case NL80211_CHAN_WIDTH_160:
531 return CHAN_WIDTH_160;
532 }
533 return CHAN_WIDTH_UNKNOWN;
534}
535
536
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800537static int is_ap_interface(enum nl80211_iftype nlmode)
538{
539 return (nlmode == NL80211_IFTYPE_AP ||
540 nlmode == NL80211_IFTYPE_P2P_GO);
541}
542
543
544static int is_sta_interface(enum nl80211_iftype nlmode)
545{
546 return (nlmode == NL80211_IFTYPE_STATION ||
547 nlmode == NL80211_IFTYPE_P2P_CLIENT);
548}
549
550
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700551static int is_p2p_net_interface(enum nl80211_iftype nlmode)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800552{
553 return (nlmode == NL80211_IFTYPE_P2P_CLIENT ||
554 nlmode == NL80211_IFTYPE_P2P_GO);
555}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700556
557
Dmitry Shmidt8bae4132013-06-06 11:25:10 -0700558static void nl80211_mark_disconnected(struct wpa_driver_nl80211_data *drv)
559{
560 if (drv->associated)
561 os_memcpy(drv->prev_bssid, drv->bssid, ETH_ALEN);
562 drv->associated = 0;
563 os_memset(drv->bssid, 0, ETH_ALEN);
564}
565
566
Jouni Malinen87fd2792011-05-16 18:35:42 +0300567struct nl80211_bss_info_arg {
568 struct wpa_driver_nl80211_data *drv;
569 struct wpa_scan_results *res;
570 unsigned int assoc_freq;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800571 u8 assoc_bssid[ETH_ALEN];
Jouni Malinen87fd2792011-05-16 18:35:42 +0300572};
573
574static int bss_info_handler(struct nl_msg *msg, void *arg);
575
576
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700577/* nl80211 code */
578static int ack_handler(struct nl_msg *msg, void *arg)
579{
580 int *err = arg;
581 *err = 0;
582 return NL_STOP;
583}
584
585static int finish_handler(struct nl_msg *msg, void *arg)
586{
587 int *ret = arg;
588 *ret = 0;
589 return NL_SKIP;
590}
591
592static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
593 void *arg)
594{
595 int *ret = arg;
596 *ret = err->error;
597 return NL_SKIP;
598}
599
600
601static int no_seq_check(struct nl_msg *msg, void *arg)
602{
603 return NL_OK;
604}
605
606
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800607static int send_and_recv(struct nl80211_global *global,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700608 struct nl_handle *nl_handle, struct nl_msg *msg,
609 int (*valid_handler)(struct nl_msg *, void *),
610 void *valid_data)
611{
612 struct nl_cb *cb;
613 int err = -ENOMEM;
614
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800615 cb = nl_cb_clone(global->nl_cb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700616 if (!cb)
617 goto out;
618
619 err = nl_send_auto_complete(nl_handle, msg);
620 if (err < 0)
621 goto out;
622
623 err = 1;
624
625 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
626 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
627 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
628
629 if (valid_handler)
630 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM,
631 valid_handler, valid_data);
632
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700633 while (err > 0) {
634 int res = nl_recvmsgs(nl_handle, cb);
635 if (res) {
636 wpa_printf(MSG_INFO,
637 "nl80211: %s->nl_recvmsgs failed: %d",
638 __func__, res);
639 }
640 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700641 out:
642 nl_cb_put(cb);
643 nlmsg_free(msg);
644 return err;
645}
646
647
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800648static int send_and_recv_msgs_global(struct nl80211_global *global,
649 struct nl_msg *msg,
650 int (*valid_handler)(struct nl_msg *, void *),
651 void *valid_data)
652{
653 return send_and_recv(global, global->nl, msg, valid_handler,
654 valid_data);
655}
656
Dmitry Shmidt04949592012-07-19 12:16:46 -0700657
Dmitry Shmidt641185e2013-11-06 15:17:13 -0800658static int send_and_recv_msgs(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700659 struct nl_msg *msg,
660 int (*valid_handler)(struct nl_msg *, void *),
661 void *valid_data)
662{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800663 return send_and_recv(drv->global, drv->global->nl, msg,
664 valid_handler, valid_data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700665}
666
667
668struct family_data {
669 const char *group;
670 int id;
671};
672
673
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700674static int nl80211_set_iface_id(struct nl_msg *msg, struct i802_bss *bss)
675{
676 if (bss->wdev_id_set)
677 NLA_PUT_U64(msg, NL80211_ATTR_WDEV, bss->wdev_id);
678 else
679 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
680 return 0;
681
682nla_put_failure:
683 return -1;
684}
685
686
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700687static int family_handler(struct nl_msg *msg, void *arg)
688{
689 struct family_data *res = arg;
690 struct nlattr *tb[CTRL_ATTR_MAX + 1];
691 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
692 struct nlattr *mcgrp;
693 int i;
694
695 nla_parse(tb, CTRL_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
696 genlmsg_attrlen(gnlh, 0), NULL);
697 if (!tb[CTRL_ATTR_MCAST_GROUPS])
698 return NL_SKIP;
699
700 nla_for_each_nested(mcgrp, tb[CTRL_ATTR_MCAST_GROUPS], i) {
701 struct nlattr *tb2[CTRL_ATTR_MCAST_GRP_MAX + 1];
702 nla_parse(tb2, CTRL_ATTR_MCAST_GRP_MAX, nla_data(mcgrp),
703 nla_len(mcgrp), NULL);
704 if (!tb2[CTRL_ATTR_MCAST_GRP_NAME] ||
705 !tb2[CTRL_ATTR_MCAST_GRP_ID] ||
706 os_strncmp(nla_data(tb2[CTRL_ATTR_MCAST_GRP_NAME]),
707 res->group,
708 nla_len(tb2[CTRL_ATTR_MCAST_GRP_NAME])) != 0)
709 continue;
710 res->id = nla_get_u32(tb2[CTRL_ATTR_MCAST_GRP_ID]);
711 break;
712 };
713
714 return NL_SKIP;
715}
716
717
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800718static int nl_get_multicast_id(struct nl80211_global *global,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700719 const char *family, const char *group)
720{
721 struct nl_msg *msg;
722 int ret = -1;
723 struct family_data res = { group, -ENOENT };
724
725 msg = nlmsg_alloc();
726 if (!msg)
727 return -ENOMEM;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800728 genlmsg_put(msg, 0, 0, genl_ctrl_resolve(global->nl, "nlctrl"),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700729 0, 0, CTRL_CMD_GETFAMILY, 0);
730 NLA_PUT_STRING(msg, CTRL_ATTR_FAMILY_NAME, family);
731
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800732 ret = send_and_recv_msgs_global(global, msg, family_handler, &res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700733 msg = NULL;
734 if (ret == 0)
735 ret = res.id;
736
737nla_put_failure:
738 nlmsg_free(msg);
739 return ret;
740}
741
742
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800743static void * nl80211_cmd(struct wpa_driver_nl80211_data *drv,
744 struct nl_msg *msg, int flags, uint8_t cmd)
745{
746 return genlmsg_put(msg, 0, 0, drv->global->nl80211_id,
747 0, flags, cmd, 0);
748}
749
750
751struct wiphy_idx_data {
752 int wiphy_idx;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700753 enum nl80211_iftype nlmode;
754 u8 *macaddr;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800755};
756
757
758static int netdev_info_handler(struct nl_msg *msg, void *arg)
759{
760 struct nlattr *tb[NL80211_ATTR_MAX + 1];
761 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
762 struct wiphy_idx_data *info = arg;
763
764 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
765 genlmsg_attrlen(gnlh, 0), NULL);
766
767 if (tb[NL80211_ATTR_WIPHY])
768 info->wiphy_idx = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
769
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700770 if (tb[NL80211_ATTR_IFTYPE])
771 info->nlmode = nla_get_u32(tb[NL80211_ATTR_IFTYPE]);
772
773 if (tb[NL80211_ATTR_MAC] && info->macaddr)
774 os_memcpy(info->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
775 ETH_ALEN);
776
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800777 return NL_SKIP;
778}
779
780
781static int nl80211_get_wiphy_index(struct i802_bss *bss)
782{
783 struct nl_msg *msg;
784 struct wiphy_idx_data data = {
785 .wiphy_idx = -1,
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700786 .macaddr = NULL,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800787 };
788
789 msg = nlmsg_alloc();
790 if (!msg)
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700791 return NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800792
793 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_GET_INTERFACE);
794
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700795 if (nl80211_set_iface_id(msg, bss) < 0)
796 goto nla_put_failure;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800797
798 if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0)
799 return data.wiphy_idx;
800 msg = NULL;
801nla_put_failure:
802 nlmsg_free(msg);
803 return -1;
804}
805
806
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700807static enum nl80211_iftype nl80211_get_ifmode(struct i802_bss *bss)
808{
809 struct nl_msg *msg;
810 struct wiphy_idx_data data = {
811 .nlmode = NL80211_IFTYPE_UNSPECIFIED,
812 .macaddr = NULL,
813 };
814
815 msg = nlmsg_alloc();
816 if (!msg)
817 return -1;
818
819 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_GET_INTERFACE);
820
821 if (nl80211_set_iface_id(msg, bss) < 0)
822 goto nla_put_failure;
823
824 if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0)
825 return data.nlmode;
826 msg = NULL;
827nla_put_failure:
828 nlmsg_free(msg);
829 return NL80211_IFTYPE_UNSPECIFIED;
830}
831
832
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700833static int nl80211_get_macaddr(struct i802_bss *bss)
834{
835 struct nl_msg *msg;
836 struct wiphy_idx_data data = {
837 .macaddr = bss->addr,
838 };
839
840 msg = nlmsg_alloc();
841 if (!msg)
842 return NL80211_IFTYPE_UNSPECIFIED;
843
844 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_GET_INTERFACE);
845 if (nl80211_set_iface_id(msg, bss) < 0)
846 goto nla_put_failure;
847
848 return send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data);
849
850nla_put_failure:
851 nlmsg_free(msg);
852 return NL80211_IFTYPE_UNSPECIFIED;
853}
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700854
855
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800856static int nl80211_register_beacons(struct wpa_driver_nl80211_data *drv,
857 struct nl80211_wiphy_data *w)
858{
859 struct nl_msg *msg;
860 int ret = -1;
861
862 msg = nlmsg_alloc();
863 if (!msg)
864 return -1;
865
866 nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_BEACONS);
867
868 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, w->wiphy_idx);
869
870 ret = send_and_recv(drv->global, w->nl_beacons, msg, NULL, NULL);
871 msg = NULL;
872 if (ret) {
873 wpa_printf(MSG_DEBUG, "nl80211: Register beacons command "
874 "failed: ret=%d (%s)",
875 ret, strerror(-ret));
876 goto nla_put_failure;
877 }
878 ret = 0;
879nla_put_failure:
880 nlmsg_free(msg);
881 return ret;
882}
883
884
885static void nl80211_recv_beacons(int sock, void *eloop_ctx, void *handle)
886{
887 struct nl80211_wiphy_data *w = eloop_ctx;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700888 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800889
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800890 wpa_printf(MSG_EXCESSIVE, "nl80211: Beacon event message available");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800891
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700892 res = nl_recvmsgs(handle, w->nl_cb);
893 if (res) {
894 wpa_printf(MSG_INFO, "nl80211: %s->nl_recvmsgs failed: %d",
895 __func__, res);
896 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800897}
898
899
900static int process_beacon_event(struct nl_msg *msg, void *arg)
901{
902 struct nl80211_wiphy_data *w = arg;
903 struct wpa_driver_nl80211_data *drv;
904 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
905 struct nlattr *tb[NL80211_ATTR_MAX + 1];
906 union wpa_event_data event;
907
908 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
909 genlmsg_attrlen(gnlh, 0), NULL);
910
911 if (gnlh->cmd != NL80211_CMD_FRAME) {
912 wpa_printf(MSG_DEBUG, "nl80211: Unexpected beacon event? (%d)",
913 gnlh->cmd);
914 return NL_SKIP;
915 }
916
917 if (!tb[NL80211_ATTR_FRAME])
918 return NL_SKIP;
919
920 dl_list_for_each(drv, &w->drvs, struct wpa_driver_nl80211_data,
921 wiphy_list) {
922 os_memset(&event, 0, sizeof(event));
923 event.rx_mgmt.frame = nla_data(tb[NL80211_ATTR_FRAME]);
924 event.rx_mgmt.frame_len = nla_len(tb[NL80211_ATTR_FRAME]);
925 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
926 }
927
928 return NL_SKIP;
929}
930
931
932static struct nl80211_wiphy_data *
933nl80211_get_wiphy_data_ap(struct i802_bss *bss)
934{
935 static DEFINE_DL_LIST(nl80211_wiphys);
936 struct nl80211_wiphy_data *w;
937 int wiphy_idx, found = 0;
938 struct i802_bss *tmp_bss;
939
940 if (bss->wiphy_data != NULL)
941 return bss->wiphy_data;
942
943 wiphy_idx = nl80211_get_wiphy_index(bss);
944
945 dl_list_for_each(w, &nl80211_wiphys, struct nl80211_wiphy_data, list) {
946 if (w->wiphy_idx == wiphy_idx)
947 goto add;
948 }
949
950 /* alloc new one */
951 w = os_zalloc(sizeof(*w));
952 if (w == NULL)
953 return NULL;
954 w->wiphy_idx = wiphy_idx;
955 dl_list_init(&w->bsss);
956 dl_list_init(&w->drvs);
957
958 w->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
959 if (!w->nl_cb) {
960 os_free(w);
961 return NULL;
962 }
963 nl_cb_set(w->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL);
964 nl_cb_set(w->nl_cb, NL_CB_VALID, NL_CB_CUSTOM, process_beacon_event,
965 w);
966
967 w->nl_beacons = nl_create_handle(bss->drv->global->nl_cb,
968 "wiphy beacons");
969 if (w->nl_beacons == NULL) {
970 os_free(w);
971 return NULL;
972 }
973
974 if (nl80211_register_beacons(bss->drv, w)) {
975 nl_destroy_handles(&w->nl_beacons);
976 os_free(w);
977 return NULL;
978 }
979
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700980 nl80211_register_eloop_read(&w->nl_beacons, nl80211_recv_beacons, w);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800981
982 dl_list_add(&nl80211_wiphys, &w->list);
983
984add:
985 /* drv entry for this bss already there? */
986 dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
987 if (tmp_bss->drv == bss->drv) {
988 found = 1;
989 break;
990 }
991 }
992 /* if not add it */
993 if (!found)
994 dl_list_add(&w->drvs, &bss->drv->wiphy_list);
995
996 dl_list_add(&w->bsss, &bss->wiphy_list);
997 bss->wiphy_data = w;
998 return w;
999}
1000
1001
1002static void nl80211_put_wiphy_data_ap(struct i802_bss *bss)
1003{
1004 struct nl80211_wiphy_data *w = bss->wiphy_data;
1005 struct i802_bss *tmp_bss;
1006 int found = 0;
1007
1008 if (w == NULL)
1009 return;
1010 bss->wiphy_data = NULL;
1011 dl_list_del(&bss->wiphy_list);
1012
1013 /* still any for this drv present? */
1014 dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
1015 if (tmp_bss->drv == bss->drv) {
1016 found = 1;
1017 break;
1018 }
1019 }
1020 /* if not remove it */
1021 if (!found)
1022 dl_list_del(&bss->drv->wiphy_list);
1023
1024 if (!dl_list_empty(&w->bsss))
1025 return;
1026
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001027 nl80211_destroy_eloop_handle(&w->nl_beacons);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001028
1029 nl_cb_put(w->nl_cb);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001030 dl_list_del(&w->list);
1031 os_free(w);
1032}
1033
1034
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001035static int wpa_driver_nl80211_get_bssid(void *priv, u8 *bssid)
1036{
1037 struct i802_bss *bss = priv;
1038 struct wpa_driver_nl80211_data *drv = bss->drv;
1039 if (!drv->associated)
1040 return -1;
1041 os_memcpy(bssid, drv->bssid, ETH_ALEN);
1042 return 0;
1043}
1044
1045
1046static int wpa_driver_nl80211_get_ssid(void *priv, u8 *ssid)
1047{
1048 struct i802_bss *bss = priv;
1049 struct wpa_driver_nl80211_data *drv = bss->drv;
1050 if (!drv->associated)
1051 return -1;
1052 os_memcpy(ssid, drv->ssid, drv->ssid_len);
1053 return drv->ssid_len;
1054}
1055
1056
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001057static void wpa_driver_nl80211_event_newlink(
1058 struct wpa_driver_nl80211_data *drv, char *ifname)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001059{
1060 union wpa_event_data event;
1061
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001062 if (os_strcmp(drv->first_bss->ifname, ifname) == 0) {
1063 if (if_nametoindex(drv->first_bss->ifname) == 0) {
1064 wpa_printf(MSG_DEBUG, "nl80211: Interface %s does not exist - ignore RTM_NEWLINK",
1065 drv->first_bss->ifname);
1066 return;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001067 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001068 if (!drv->if_removed)
1069 return;
1070 wpa_printf(MSG_DEBUG, "nl80211: Mark if_removed=0 for %s based on RTM_NEWLINK event",
1071 drv->first_bss->ifname);
1072 drv->if_removed = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001073 }
1074
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001075 os_memset(&event, 0, sizeof(event));
1076 os_strlcpy(event.interface_status.ifname, ifname,
1077 sizeof(event.interface_status.ifname));
1078 event.interface_status.ievent = EVENT_INTERFACE_ADDED;
1079 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
1080}
1081
1082
1083static void wpa_driver_nl80211_event_dellink(
1084 struct wpa_driver_nl80211_data *drv, char *ifname)
1085{
1086 union wpa_event_data event;
1087
1088 if (os_strcmp(drv->first_bss->ifname, ifname) == 0) {
1089 if (drv->if_removed) {
1090 wpa_printf(MSG_DEBUG, "nl80211: if_removed already set - ignore RTM_DELLINK event for %s",
1091 ifname);
1092 return;
1093 }
1094 wpa_printf(MSG_DEBUG, "RTM_DELLINK: Interface '%s' removed - mark if_removed=1",
1095 ifname);
1096 drv->if_removed = 1;
1097 } else {
1098 wpa_printf(MSG_DEBUG, "RTM_DELLINK: Interface '%s' removed",
1099 ifname);
1100 }
1101
1102 os_memset(&event, 0, sizeof(event));
1103 os_strlcpy(event.interface_status.ifname, ifname,
1104 sizeof(event.interface_status.ifname));
1105 event.interface_status.ievent = EVENT_INTERFACE_REMOVED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001106 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
1107}
1108
1109
1110static int wpa_driver_nl80211_own_ifname(struct wpa_driver_nl80211_data *drv,
1111 u8 *buf, size_t len)
1112{
1113 int attrlen, rta_len;
1114 struct rtattr *attr;
1115
1116 attrlen = len;
1117 attr = (struct rtattr *) buf;
1118
1119 rta_len = RTA_ALIGN(sizeof(struct rtattr));
1120 while (RTA_OK(attr, attrlen)) {
1121 if (attr->rta_type == IFLA_IFNAME) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001122 if (os_strcmp(((char *) attr) + rta_len,
1123 drv->first_bss->ifname) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001124 return 1;
1125 else
1126 break;
1127 }
1128 attr = RTA_NEXT(attr, attrlen);
1129 }
1130
1131 return 0;
1132}
1133
1134
1135static int wpa_driver_nl80211_own_ifindex(struct wpa_driver_nl80211_data *drv,
1136 int ifindex, u8 *buf, size_t len)
1137{
1138 if (drv->ifindex == ifindex)
1139 return 1;
1140
1141 if (drv->if_removed && wpa_driver_nl80211_own_ifname(drv, buf, len)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001142 wpa_printf(MSG_DEBUG, "nl80211: Update ifindex for a removed "
1143 "interface");
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001144 wpa_driver_nl80211_finish_drv_init(drv, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001145 return 1;
1146 }
1147
1148 return 0;
1149}
1150
1151
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001152static struct wpa_driver_nl80211_data *
1153nl80211_find_drv(struct nl80211_global *global, int idx, u8 *buf, size_t len)
1154{
1155 struct wpa_driver_nl80211_data *drv;
1156 dl_list_for_each(drv, &global->interfaces,
1157 struct wpa_driver_nl80211_data, list) {
1158 if (wpa_driver_nl80211_own_ifindex(drv, idx, buf, len) ||
1159 have_ifidx(drv, idx))
1160 return drv;
1161 }
1162 return NULL;
1163}
1164
1165
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001166static void wpa_driver_nl80211_event_rtm_newlink(void *ctx,
1167 struct ifinfomsg *ifi,
1168 u8 *buf, size_t len)
1169{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001170 struct nl80211_global *global = ctx;
1171 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001172 int attrlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001173 struct rtattr *attr;
1174 u32 brid = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001175 char namebuf[IFNAMSIZ];
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001176 char ifname[IFNAMSIZ + 1];
1177 char extra[100], *pos, *end;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001178
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001179 drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
1180 if (!drv) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001181 wpa_printf(MSG_DEBUG, "nl80211: Ignore RTM_NEWLINK event for foreign ifindex %d",
1182 ifi->ifi_index);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001183 return;
1184 }
1185
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001186 extra[0] = '\0';
1187 pos = extra;
1188 end = pos + sizeof(extra);
1189 ifname[0] = '\0';
1190
1191 attrlen = len;
1192 attr = (struct rtattr *) buf;
1193 while (RTA_OK(attr, attrlen)) {
1194 switch (attr->rta_type) {
1195 case IFLA_IFNAME:
1196 if (RTA_PAYLOAD(attr) >= IFNAMSIZ)
1197 break;
1198 os_memcpy(ifname, RTA_DATA(attr), RTA_PAYLOAD(attr));
1199 ifname[RTA_PAYLOAD(attr)] = '\0';
1200 break;
1201 case IFLA_MASTER:
1202 brid = nla_get_u32((struct nlattr *) attr);
1203 pos += os_snprintf(pos, end - pos, " master=%u", brid);
1204 break;
1205 case IFLA_WIRELESS:
1206 pos += os_snprintf(pos, end - pos, " wext");
1207 break;
1208 case IFLA_OPERSTATE:
1209 pos += os_snprintf(pos, end - pos, " operstate=%u",
1210 nla_get_u32((struct nlattr *) attr));
1211 break;
1212 case IFLA_LINKMODE:
1213 pos += os_snprintf(pos, end - pos, " linkmode=%u",
1214 nla_get_u32((struct nlattr *) attr));
1215 break;
1216 }
1217 attr = RTA_NEXT(attr, attrlen);
1218 }
1219 extra[sizeof(extra) - 1] = '\0';
1220
1221 wpa_printf(MSG_DEBUG, "RTM_NEWLINK: ifi_index=%d ifname=%s%s ifi_flags=0x%x (%s%s%s%s)",
1222 ifi->ifi_index, ifname, extra, ifi->ifi_flags,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001223 (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
1224 (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
1225 (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
1226 (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
1227
1228 if (!drv->if_disabled && !(ifi->ifi_flags & IFF_UP)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001229 if (if_indextoname(ifi->ifi_index, namebuf) &&
1230 linux_iface_up(drv->global->ioctl_sock,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001231 drv->first_bss->ifname) > 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001232 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
1233 "event since interface %s is up", namebuf);
1234 return;
1235 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001236 wpa_printf(MSG_DEBUG, "nl80211: Interface down");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001237 if (drv->ignore_if_down_event) {
1238 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
1239 "event generated by mode change");
1240 drv->ignore_if_down_event = 0;
1241 } else {
1242 drv->if_disabled = 1;
1243 wpa_supplicant_event(drv->ctx,
1244 EVENT_INTERFACE_DISABLED, NULL);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001245
1246 /*
1247 * Try to get drv again, since it may be removed as
1248 * part of the EVENT_INTERFACE_DISABLED handling for
1249 * dynamic interfaces
1250 */
1251 drv = nl80211_find_drv(global, ifi->ifi_index,
1252 buf, len);
1253 if (!drv)
1254 return;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001255 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001256 }
1257
1258 if (drv->if_disabled && (ifi->ifi_flags & IFF_UP)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001259 if (if_indextoname(ifi->ifi_index, namebuf) &&
1260 linux_iface_up(drv->global->ioctl_sock,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001261 drv->first_bss->ifname) == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001262 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
1263 "event since interface %s is down",
1264 namebuf);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001265 } else if (if_nametoindex(drv->first_bss->ifname) == 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001266 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
1267 "event since interface %s does not exist",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001268 drv->first_bss->ifname);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001269 } else if (drv->if_removed) {
1270 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
1271 "event since interface %s is marked "
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001272 "removed", drv->first_bss->ifname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001273 } else {
1274 wpa_printf(MSG_DEBUG, "nl80211: Interface up");
1275 drv->if_disabled = 0;
1276 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_ENABLED,
1277 NULL);
1278 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001279 }
1280
1281 /*
1282 * Some drivers send the association event before the operup event--in
1283 * this case, lifting operstate in wpa_driver_nl80211_set_operstate()
1284 * fails. This will hit us when wpa_supplicant does not need to do
1285 * IEEE 802.1X authentication
1286 */
1287 if (drv->operstate == 1 &&
1288 (ifi->ifi_flags & (IFF_LOWER_UP | IFF_DORMANT)) == IFF_LOWER_UP &&
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001289 !(ifi->ifi_flags & IFF_RUNNING)) {
1290 wpa_printf(MSG_DEBUG, "nl80211: Set IF_OPER_UP again based on ifi_flags and expected operstate");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001291 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001292 -1, IF_OPER_UP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001293 }
1294
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001295 if (ifname[0])
1296 wpa_driver_nl80211_event_newlink(drv, ifname);
1297
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001298 if (ifi->ifi_family == AF_BRIDGE && brid) {
1299 /* device has been added to bridge */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001300 if_indextoname(brid, namebuf);
1301 wpa_printf(MSG_DEBUG, "nl80211: Add ifindex %u for bridge %s",
1302 brid, namebuf);
1303 add_ifidx(drv, brid);
1304 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001305}
1306
1307
1308static void wpa_driver_nl80211_event_rtm_dellink(void *ctx,
1309 struct ifinfomsg *ifi,
1310 u8 *buf, size_t len)
1311{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001312 struct nl80211_global *global = ctx;
1313 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001314 int attrlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001315 struct rtattr *attr;
1316 u32 brid = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001317 char ifname[IFNAMSIZ + 1];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001318
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001319 drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
1320 if (!drv) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001321 wpa_printf(MSG_DEBUG, "nl80211: Ignore RTM_DELLINK event for foreign ifindex %d",
1322 ifi->ifi_index);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001323 return;
1324 }
1325
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001326 ifname[0] = '\0';
1327
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001328 attrlen = len;
1329 attr = (struct rtattr *) buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001330 while (RTA_OK(attr, attrlen)) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001331 switch (attr->rta_type) {
1332 case IFLA_IFNAME:
1333 if (RTA_PAYLOAD(attr) >= IFNAMSIZ)
1334 break;
1335 os_memcpy(ifname, RTA_DATA(attr), RTA_PAYLOAD(attr));
1336 ifname[RTA_PAYLOAD(attr)] = '\0';
1337 break;
1338 case IFLA_MASTER:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001339 brid = nla_get_u32((struct nlattr *) attr);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001340 break;
1341 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001342 attr = RTA_NEXT(attr, attrlen);
1343 }
1344
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001345 if (ifname[0])
1346 wpa_driver_nl80211_event_dellink(drv, ifname);
1347
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001348 if (ifi->ifi_family == AF_BRIDGE && brid) {
1349 /* device has been removed from bridge */
1350 char namebuf[IFNAMSIZ];
1351 if_indextoname(brid, namebuf);
1352 wpa_printf(MSG_DEBUG, "nl80211: Remove ifindex %u for bridge "
1353 "%s", brid, namebuf);
1354 del_ifidx(drv, brid);
1355 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001356}
1357
1358
1359static void mlme_event_auth(struct wpa_driver_nl80211_data *drv,
1360 const u8 *frame, size_t len)
1361{
1362 const struct ieee80211_mgmt *mgmt;
1363 union wpa_event_data event;
1364
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001365 wpa_printf(MSG_DEBUG, "nl80211: Authenticate event");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001366 mgmt = (const struct ieee80211_mgmt *) frame;
1367 if (len < 24 + sizeof(mgmt->u.auth)) {
1368 wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
1369 "frame");
1370 return;
1371 }
1372
1373 os_memcpy(drv->auth_bssid, mgmt->sa, ETH_ALEN);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001374 os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001375 os_memset(&event, 0, sizeof(event));
1376 os_memcpy(event.auth.peer, mgmt->sa, ETH_ALEN);
1377 event.auth.auth_type = le_to_host16(mgmt->u.auth.auth_alg);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001378 event.auth.auth_transaction =
1379 le_to_host16(mgmt->u.auth.auth_transaction);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001380 event.auth.status_code = le_to_host16(mgmt->u.auth.status_code);
1381 if (len > 24 + sizeof(mgmt->u.auth)) {
1382 event.auth.ies = mgmt->u.auth.variable;
1383 event.auth.ies_len = len - 24 - sizeof(mgmt->u.auth);
1384 }
1385
1386 wpa_supplicant_event(drv->ctx, EVENT_AUTH, &event);
1387}
1388
1389
Jouni Malinen87fd2792011-05-16 18:35:42 +03001390static unsigned int nl80211_get_assoc_freq(struct wpa_driver_nl80211_data *drv)
1391{
1392 struct nl_msg *msg;
1393 int ret;
1394 struct nl80211_bss_info_arg arg;
1395
1396 os_memset(&arg, 0, sizeof(arg));
1397 msg = nlmsg_alloc();
1398 if (!msg)
1399 goto nla_put_failure;
1400
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001401 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
Jouni Malinen87fd2792011-05-16 18:35:42 +03001402 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
1403
1404 arg.drv = drv;
1405 ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
1406 msg = NULL;
1407 if (ret == 0) {
1408 wpa_printf(MSG_DEBUG, "nl80211: Operating frequency for the "
1409 "associated BSS from scan results: %u MHz",
1410 arg.assoc_freq);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001411 if (arg.assoc_freq)
1412 drv->assoc_freq = arg.assoc_freq;
1413 return drv->assoc_freq;
Jouni Malinen87fd2792011-05-16 18:35:42 +03001414 }
1415 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
1416 "(%s)", ret, strerror(-ret));
1417nla_put_failure:
1418 nlmsg_free(msg);
1419 return drv->assoc_freq;
1420}
1421
1422
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001423static void mlme_event_assoc(struct wpa_driver_nl80211_data *drv,
1424 const u8 *frame, size_t len)
1425{
1426 const struct ieee80211_mgmt *mgmt;
1427 union wpa_event_data event;
1428 u16 status;
1429
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001430 wpa_printf(MSG_DEBUG, "nl80211: Associate event");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001431 mgmt = (const struct ieee80211_mgmt *) frame;
1432 if (len < 24 + sizeof(mgmt->u.assoc_resp)) {
1433 wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
1434 "frame");
1435 return;
1436 }
1437
1438 status = le_to_host16(mgmt->u.assoc_resp.status_code);
1439 if (status != WLAN_STATUS_SUCCESS) {
1440 os_memset(&event, 0, sizeof(event));
1441 event.assoc_reject.bssid = mgmt->bssid;
1442 if (len > 24 + sizeof(mgmt->u.assoc_resp)) {
1443 event.assoc_reject.resp_ies =
1444 (u8 *) mgmt->u.assoc_resp.variable;
1445 event.assoc_reject.resp_ies_len =
1446 len - 24 - sizeof(mgmt->u.assoc_resp);
1447 }
1448 event.assoc_reject.status_code = status;
1449
1450 wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
1451 return;
1452 }
1453
1454 drv->associated = 1;
1455 os_memcpy(drv->bssid, mgmt->sa, ETH_ALEN);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001456 os_memcpy(drv->prev_bssid, mgmt->sa, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001457
1458 os_memset(&event, 0, sizeof(event));
1459 if (len > 24 + sizeof(mgmt->u.assoc_resp)) {
1460 event.assoc_info.resp_ies = (u8 *) mgmt->u.assoc_resp.variable;
1461 event.assoc_info.resp_ies_len =
1462 len - 24 - sizeof(mgmt->u.assoc_resp);
1463 }
1464
1465 event.assoc_info.freq = drv->assoc_freq;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001466
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001467 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
1468}
1469
1470
1471static void mlme_event_connect(struct wpa_driver_nl80211_data *drv,
1472 enum nl80211_commands cmd, struct nlattr *status,
1473 struct nlattr *addr, struct nlattr *req_ie,
1474 struct nlattr *resp_ie)
1475{
1476 union wpa_event_data event;
1477
1478 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
1479 /*
1480 * Avoid reporting two association events that would confuse
1481 * the core code.
1482 */
1483 wpa_printf(MSG_DEBUG, "nl80211: Ignore connect event (cmd=%d) "
1484 "when using userspace SME", cmd);
1485 return;
1486 }
1487
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001488 if (cmd == NL80211_CMD_CONNECT)
1489 wpa_printf(MSG_DEBUG, "nl80211: Connect event");
1490 else if (cmd == NL80211_CMD_ROAM)
1491 wpa_printf(MSG_DEBUG, "nl80211: Roam event");
1492
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001493 os_memset(&event, 0, sizeof(event));
1494 if (cmd == NL80211_CMD_CONNECT &&
1495 nla_get_u16(status) != WLAN_STATUS_SUCCESS) {
1496 if (addr)
1497 event.assoc_reject.bssid = nla_data(addr);
1498 if (resp_ie) {
1499 event.assoc_reject.resp_ies = nla_data(resp_ie);
1500 event.assoc_reject.resp_ies_len = nla_len(resp_ie);
1501 }
1502 event.assoc_reject.status_code = nla_get_u16(status);
1503 wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
1504 return;
1505 }
1506
1507 drv->associated = 1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001508 if (addr) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001509 os_memcpy(drv->bssid, nla_data(addr), ETH_ALEN);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001510 os_memcpy(drv->prev_bssid, drv->bssid, ETH_ALEN);
1511 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001512
1513 if (req_ie) {
1514 event.assoc_info.req_ies = nla_data(req_ie);
1515 event.assoc_info.req_ies_len = nla_len(req_ie);
1516 }
1517 if (resp_ie) {
1518 event.assoc_info.resp_ies = nla_data(resp_ie);
1519 event.assoc_info.resp_ies_len = nla_len(resp_ie);
1520 }
1521
Jouni Malinen87fd2792011-05-16 18:35:42 +03001522 event.assoc_info.freq = nl80211_get_assoc_freq(drv);
1523
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001524 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
1525}
1526
1527
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001528static void mlme_event_disconnect(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001529 struct nlattr *reason, struct nlattr *addr,
1530 struct nlattr *by_ap)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001531{
1532 union wpa_event_data data;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001533 unsigned int locally_generated = by_ap == NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001534
1535 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
1536 /*
1537 * Avoid reporting two disassociation events that could
1538 * confuse the core code.
1539 */
1540 wpa_printf(MSG_DEBUG, "nl80211: Ignore disconnect "
1541 "event when using userspace SME");
1542 return;
1543 }
1544
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001545 if (drv->ignore_next_local_disconnect) {
1546 drv->ignore_next_local_disconnect = 0;
1547 if (locally_generated) {
1548 wpa_printf(MSG_DEBUG, "nl80211: Ignore disconnect "
1549 "event triggered during reassociation");
1550 return;
1551 }
1552 wpa_printf(MSG_WARNING, "nl80211: Was expecting local "
1553 "disconnect but got another disconnect "
1554 "event first");
1555 }
1556
1557 wpa_printf(MSG_DEBUG, "nl80211: Disconnect event");
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001558 nl80211_mark_disconnected(drv);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001559 os_memset(&data, 0, sizeof(data));
1560 if (reason)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001561 data.deauth_info.reason_code = nla_get_u16(reason);
1562 data.deauth_info.locally_generated = by_ap == NULL;
1563 wpa_supplicant_event(drv->ctx, EVENT_DEAUTH, &data);
1564}
1565
1566
Dmitry Shmidt97672262014-02-03 13:02:54 -08001567static int calculate_chan_offset(int width, int freq, int cf1, int cf2)
1568{
1569 int freq1 = 0;
1570
1571 switch (convert2width(width)) {
1572 case CHAN_WIDTH_20_NOHT:
1573 case CHAN_WIDTH_20:
1574 return 0;
1575 case CHAN_WIDTH_40:
1576 freq1 = cf1 - 10;
1577 break;
1578 case CHAN_WIDTH_80:
1579 freq1 = cf1 - 30;
1580 break;
1581 case CHAN_WIDTH_160:
1582 freq1 = cf1 - 70;
1583 break;
1584 case CHAN_WIDTH_UNKNOWN:
1585 case CHAN_WIDTH_80P80:
1586 /* FIXME: implement this */
1587 return 0;
1588 }
1589
1590 return (abs(freq - freq1) / 20) % 2 == 0 ? 1 : -1;
1591}
1592
1593
Dmitry Shmidt04949592012-07-19 12:16:46 -07001594static void mlme_event_ch_switch(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001595 struct nlattr *ifindex, struct nlattr *freq,
1596 struct nlattr *type, struct nlattr *bw,
1597 struct nlattr *cf1, struct nlattr *cf2)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001598{
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001599 struct i802_bss *bss;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001600 union wpa_event_data data;
1601 int ht_enabled = 1;
1602 int chan_offset = 0;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001603 int ifidx;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001604
1605 wpa_printf(MSG_DEBUG, "nl80211: Channel switch event");
1606
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001607 if (!freq)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001608 return;
1609
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001610 ifidx = nla_get_u32(ifindex);
1611 for (bss = drv->first_bss; bss; bss = bss->next)
1612 if (bss->ifindex == ifidx)
1613 break;
1614
1615 if (bss == NULL) {
1616 wpa_printf(MSG_WARNING, "nl80211: Unknown ifindex (%d) for channel switch, ignoring",
1617 ifidx);
1618 return;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001619 }
1620
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001621 if (type) {
1622 switch (nla_get_u32(type)) {
1623 case NL80211_CHAN_NO_HT:
1624 ht_enabled = 0;
1625 break;
1626 case NL80211_CHAN_HT20:
1627 break;
1628 case NL80211_CHAN_HT40PLUS:
1629 chan_offset = 1;
1630 break;
1631 case NL80211_CHAN_HT40MINUS:
1632 chan_offset = -1;
1633 break;
1634 }
Dmitry Shmidt97672262014-02-03 13:02:54 -08001635 } else if (bw && cf1) {
1636 /* This can happen for example with VHT80 ch switch */
1637 chan_offset = calculate_chan_offset(nla_get_u32(bw),
1638 nla_get_u32(freq),
1639 nla_get_u32(cf1),
1640 cf2 ? nla_get_u32(cf2) : 0);
1641 } else {
1642 wpa_printf(MSG_WARNING, "nl80211: Unknown secondary channel information - following channel definition calculations may fail");
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001643 }
1644
1645 os_memset(&data, 0, sizeof(data));
Dmitry Shmidt04949592012-07-19 12:16:46 -07001646 data.ch_switch.freq = nla_get_u32(freq);
1647 data.ch_switch.ht_enabled = ht_enabled;
1648 data.ch_switch.ch_offset = chan_offset;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001649 if (bw)
1650 data.ch_switch.ch_width = convert2width(nla_get_u32(bw));
1651 if (cf1)
1652 data.ch_switch.cf1 = nla_get_u32(cf1);
1653 if (cf2)
1654 data.ch_switch.cf2 = nla_get_u32(cf2);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001655
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001656 bss->freq = data.ch_switch.freq;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001657
Dmitry Shmidt04949592012-07-19 12:16:46 -07001658 wpa_supplicant_event(drv->ctx, EVENT_CH_SWITCH, &data);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001659}
1660
1661
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001662static void mlme_timeout_event(struct wpa_driver_nl80211_data *drv,
1663 enum nl80211_commands cmd, struct nlattr *addr)
1664{
1665 union wpa_event_data event;
1666 enum wpa_event_type ev;
1667
1668 if (nla_len(addr) != ETH_ALEN)
1669 return;
1670
1671 wpa_printf(MSG_DEBUG, "nl80211: MLME event %d; timeout with " MACSTR,
1672 cmd, MAC2STR((u8 *) nla_data(addr)));
1673
1674 if (cmd == NL80211_CMD_AUTHENTICATE)
1675 ev = EVENT_AUTH_TIMED_OUT;
1676 else if (cmd == NL80211_CMD_ASSOCIATE)
1677 ev = EVENT_ASSOC_TIMED_OUT;
1678 else
1679 return;
1680
1681 os_memset(&event, 0, sizeof(event));
1682 os_memcpy(event.timeout_event.addr, nla_data(addr), ETH_ALEN);
1683 wpa_supplicant_event(drv->ctx, ev, &event);
1684}
1685
1686
Dmitry Shmidt98660862014-03-11 17:26:21 -07001687static void mlme_event_mgmt(struct i802_bss *bss,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001688 struct nlattr *freq, struct nlattr *sig,
1689 const u8 *frame, size_t len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001690{
Dmitry Shmidt98660862014-03-11 17:26:21 -07001691 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001692 const struct ieee80211_mgmt *mgmt;
1693 union wpa_event_data event;
1694 u16 fc, stype;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001695 int ssi_signal = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001696 int rx_freq = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001697
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001698 wpa_printf(MSG_MSGDUMP, "nl80211: Frame event");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001699 mgmt = (const struct ieee80211_mgmt *) frame;
1700 if (len < 24) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001701 wpa_printf(MSG_DEBUG, "nl80211: Too short management frame");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001702 return;
1703 }
1704
1705 fc = le_to_host16(mgmt->frame_control);
1706 stype = WLAN_FC_GET_STYPE(fc);
1707
Dmitry Shmidt04949592012-07-19 12:16:46 -07001708 if (sig)
1709 ssi_signal = (s32) nla_get_u32(sig);
1710
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001711 os_memset(&event, 0, sizeof(event));
1712 if (freq) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001713 event.rx_mgmt.freq = nla_get_u32(freq);
1714 rx_freq = drv->last_mgmt_freq = event.rx_mgmt.freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001715 }
Dmitry Shmidt56052862013-10-04 10:23:25 -07001716 wpa_printf(MSG_DEBUG,
1717 "nl80211: RX frame freq=%d ssi_signal=%d stype=%u len=%u",
1718 rx_freq, ssi_signal, stype, (unsigned int) len);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001719 event.rx_mgmt.frame = frame;
1720 event.rx_mgmt.frame_len = len;
1721 event.rx_mgmt.ssi_signal = ssi_signal;
Dmitry Shmidt98660862014-03-11 17:26:21 -07001722 event.rx_mgmt.drv_priv = bss;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001723 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001724}
1725
1726
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001727static void mlme_event_mgmt_tx_status(struct wpa_driver_nl80211_data *drv,
1728 struct nlattr *cookie, const u8 *frame,
1729 size_t len, struct nlattr *ack)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001730{
1731 union wpa_event_data event;
1732 const struct ieee80211_hdr *hdr;
1733 u16 fc;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001734
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001735 wpa_printf(MSG_DEBUG, "nl80211: Frame TX status event");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001736 if (!is_ap_interface(drv->nlmode)) {
1737 u64 cookie_val;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001738
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001739 if (!cookie)
1740 return;
1741
1742 cookie_val = nla_get_u64(cookie);
1743 wpa_printf(MSG_DEBUG, "nl80211: Action TX status:"
1744 " cookie=0%llx%s (ack=%d)",
1745 (long long unsigned int) cookie_val,
1746 cookie_val == drv->send_action_cookie ?
1747 " (match)" : " (unknown)", ack != NULL);
1748 if (cookie_val != drv->send_action_cookie)
1749 return;
1750 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001751
1752 hdr = (const struct ieee80211_hdr *) frame;
1753 fc = le_to_host16(hdr->frame_control);
1754
1755 os_memset(&event, 0, sizeof(event));
1756 event.tx_status.type = WLAN_FC_GET_TYPE(fc);
1757 event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
1758 event.tx_status.dst = hdr->addr1;
1759 event.tx_status.data = frame;
1760 event.tx_status.data_len = len;
1761 event.tx_status.ack = ack != NULL;
1762 wpa_supplicant_event(drv->ctx, EVENT_TX_STATUS, &event);
1763}
1764
1765
1766static void mlme_event_deauth_disassoc(struct wpa_driver_nl80211_data *drv,
1767 enum wpa_event_type type,
1768 const u8 *frame, size_t len)
1769{
1770 const struct ieee80211_mgmt *mgmt;
1771 union wpa_event_data event;
1772 const u8 *bssid = NULL;
1773 u16 reason_code = 0;
1774
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001775 if (type == EVENT_DEAUTH)
1776 wpa_printf(MSG_DEBUG, "nl80211: Deauthenticate event");
1777 else
1778 wpa_printf(MSG_DEBUG, "nl80211: Disassociate event");
1779
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001780 mgmt = (const struct ieee80211_mgmt *) frame;
1781 if (len >= 24) {
1782 bssid = mgmt->bssid;
1783
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001784 if ((drv->capa.flags & WPA_DRIVER_FLAGS_SME) &&
1785 !drv->associated &&
1786 os_memcmp(bssid, drv->auth_bssid, ETH_ALEN) != 0 &&
1787 os_memcmp(bssid, drv->auth_attempt_bssid, ETH_ALEN) != 0 &&
1788 os_memcmp(bssid, drv->prev_bssid, ETH_ALEN) == 0) {
1789 /*
1790 * Avoid issues with some roaming cases where
1791 * disconnection event for the old AP may show up after
1792 * we have started connection with the new AP.
1793 */
1794 wpa_printf(MSG_DEBUG, "nl80211: Ignore deauth/disassoc event from old AP " MACSTR " when already authenticating with " MACSTR,
1795 MAC2STR(bssid),
1796 MAC2STR(drv->auth_attempt_bssid));
1797 return;
1798 }
1799
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001800 if (drv->associated != 0 &&
1801 os_memcmp(bssid, drv->bssid, ETH_ALEN) != 0 &&
1802 os_memcmp(bssid, drv->auth_bssid, ETH_ALEN) != 0) {
1803 /*
1804 * We have presumably received this deauth as a
1805 * response to a clear_state_mismatch() outgoing
1806 * deauth. Don't let it take us offline!
1807 */
1808 wpa_printf(MSG_DEBUG, "nl80211: Deauth received "
1809 "from Unknown BSSID " MACSTR " -- ignoring",
1810 MAC2STR(bssid));
1811 return;
1812 }
1813 }
1814
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001815 nl80211_mark_disconnected(drv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001816 os_memset(&event, 0, sizeof(event));
1817
1818 /* Note: Same offset for Reason Code in both frame subtypes */
1819 if (len >= 24 + sizeof(mgmt->u.deauth))
1820 reason_code = le_to_host16(mgmt->u.deauth.reason_code);
1821
1822 if (type == EVENT_DISASSOC) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001823 event.disassoc_info.locally_generated =
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001824 !os_memcmp(mgmt->sa, drv->first_bss->addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001825 event.disassoc_info.addr = bssid;
1826 event.disassoc_info.reason_code = reason_code;
1827 if (frame + len > mgmt->u.disassoc.variable) {
1828 event.disassoc_info.ie = mgmt->u.disassoc.variable;
1829 event.disassoc_info.ie_len = frame + len -
1830 mgmt->u.disassoc.variable;
1831 }
1832 } else {
Dmitry Shmidt98660862014-03-11 17:26:21 -07001833 if (drv->ignore_deauth_event) {
1834 wpa_printf(MSG_DEBUG, "nl80211: Ignore deauth event due to previous forced deauth-during-auth");
1835 drv->ignore_deauth_event = 0;
1836 return;
1837 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001838 event.deauth_info.locally_generated =
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001839 !os_memcmp(mgmt->sa, drv->first_bss->addr, ETH_ALEN);
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07001840 if (drv->ignore_next_local_deauth) {
1841 drv->ignore_next_local_deauth = 0;
1842 if (event.deauth_info.locally_generated) {
1843 wpa_printf(MSG_DEBUG, "nl80211: Ignore deauth event triggered due to own deauth request");
1844 return;
1845 }
1846 wpa_printf(MSG_WARNING, "nl80211: Was expecting local deauth but got another disconnect event first");
1847 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001848 event.deauth_info.addr = bssid;
1849 event.deauth_info.reason_code = reason_code;
1850 if (frame + len > mgmt->u.deauth.variable) {
1851 event.deauth_info.ie = mgmt->u.deauth.variable;
1852 event.deauth_info.ie_len = frame + len -
1853 mgmt->u.deauth.variable;
1854 }
1855 }
1856
1857 wpa_supplicant_event(drv->ctx, type, &event);
1858}
1859
1860
1861static void mlme_event_unprot_disconnect(struct wpa_driver_nl80211_data *drv,
1862 enum wpa_event_type type,
1863 const u8 *frame, size_t len)
1864{
1865 const struct ieee80211_mgmt *mgmt;
1866 union wpa_event_data event;
1867 u16 reason_code = 0;
1868
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001869 if (type == EVENT_UNPROT_DEAUTH)
1870 wpa_printf(MSG_DEBUG, "nl80211: Unprot Deauthenticate event");
1871 else
1872 wpa_printf(MSG_DEBUG, "nl80211: Unprot Disassociate event");
1873
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001874 if (len < 24)
1875 return;
1876
1877 mgmt = (const struct ieee80211_mgmt *) frame;
1878
1879 os_memset(&event, 0, sizeof(event));
1880 /* Note: Same offset for Reason Code in both frame subtypes */
1881 if (len >= 24 + sizeof(mgmt->u.deauth))
1882 reason_code = le_to_host16(mgmt->u.deauth.reason_code);
1883
1884 if (type == EVENT_UNPROT_DISASSOC) {
1885 event.unprot_disassoc.sa = mgmt->sa;
1886 event.unprot_disassoc.da = mgmt->da;
1887 event.unprot_disassoc.reason_code = reason_code;
1888 } else {
1889 event.unprot_deauth.sa = mgmt->sa;
1890 event.unprot_deauth.da = mgmt->da;
1891 event.unprot_deauth.reason_code = reason_code;
1892 }
1893
1894 wpa_supplicant_event(drv->ctx, type, &event);
1895}
1896
1897
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001898static void mlme_event(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001899 enum nl80211_commands cmd, struct nlattr *frame,
1900 struct nlattr *addr, struct nlattr *timed_out,
1901 struct nlattr *freq, struct nlattr *ack,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001902 struct nlattr *cookie, struct nlattr *sig)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001903{
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001904 struct wpa_driver_nl80211_data *drv = bss->drv;
1905 const u8 *data;
1906 size_t len;
1907
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001908 if (timed_out && addr) {
1909 mlme_timeout_event(drv, cmd, addr);
1910 return;
1911 }
1912
1913 if (frame == NULL) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001914 wpa_printf(MSG_DEBUG,
1915 "nl80211: MLME event %d (%s) without frame data",
1916 cmd, nl80211_command_to_string(cmd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001917 return;
1918 }
1919
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001920 data = nla_data(frame);
1921 len = nla_len(frame);
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001922 if (len < 4 + 2 * ETH_ALEN) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001923 wpa_printf(MSG_MSGDUMP, "nl80211: MLME event %d (%s) on %s("
1924 MACSTR ") - too short",
1925 cmd, nl80211_command_to_string(cmd), bss->ifname,
1926 MAC2STR(bss->addr));
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001927 return;
1928 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001929 wpa_printf(MSG_MSGDUMP, "nl80211: MLME event %d (%s) on %s(" MACSTR
1930 ") A1=" MACSTR " A2=" MACSTR, cmd,
1931 nl80211_command_to_string(cmd), bss->ifname,
1932 MAC2STR(bss->addr), MAC2STR(data + 4),
1933 MAC2STR(data + 4 + ETH_ALEN));
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001934 if (cmd != NL80211_CMD_FRAME_TX_STATUS && !(data[4] & 0x01) &&
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001935 os_memcmp(bss->addr, data + 4, ETH_ALEN) != 0 &&
1936 os_memcmp(bss->addr, data + 4 + ETH_ALEN, ETH_ALEN) != 0) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001937 wpa_printf(MSG_MSGDUMP, "nl80211: %s: Ignore MLME frame event "
1938 "for foreign address", bss->ifname);
1939 return;
1940 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001941 wpa_hexdump(MSG_MSGDUMP, "nl80211: MLME event frame",
1942 nla_data(frame), nla_len(frame));
1943
1944 switch (cmd) {
1945 case NL80211_CMD_AUTHENTICATE:
1946 mlme_event_auth(drv, nla_data(frame), nla_len(frame));
1947 break;
1948 case NL80211_CMD_ASSOCIATE:
1949 mlme_event_assoc(drv, nla_data(frame), nla_len(frame));
1950 break;
1951 case NL80211_CMD_DEAUTHENTICATE:
1952 mlme_event_deauth_disassoc(drv, EVENT_DEAUTH,
1953 nla_data(frame), nla_len(frame));
1954 break;
1955 case NL80211_CMD_DISASSOCIATE:
1956 mlme_event_deauth_disassoc(drv, EVENT_DISASSOC,
1957 nla_data(frame), nla_len(frame));
1958 break;
1959 case NL80211_CMD_FRAME:
Dmitry Shmidt98660862014-03-11 17:26:21 -07001960 mlme_event_mgmt(bss, freq, sig, nla_data(frame),
Dmitry Shmidt04949592012-07-19 12:16:46 -07001961 nla_len(frame));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001962 break;
1963 case NL80211_CMD_FRAME_TX_STATUS:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001964 mlme_event_mgmt_tx_status(drv, cookie, nla_data(frame),
1965 nla_len(frame), ack);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001966 break;
1967 case NL80211_CMD_UNPROT_DEAUTHENTICATE:
1968 mlme_event_unprot_disconnect(drv, EVENT_UNPROT_DEAUTH,
1969 nla_data(frame), nla_len(frame));
1970 break;
1971 case NL80211_CMD_UNPROT_DISASSOCIATE:
1972 mlme_event_unprot_disconnect(drv, EVENT_UNPROT_DISASSOC,
1973 nla_data(frame), nla_len(frame));
1974 break;
1975 default:
1976 break;
1977 }
1978}
1979
1980
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001981static void mlme_event_michael_mic_failure(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001982 struct nlattr *tb[])
1983{
1984 union wpa_event_data data;
1985
1986 wpa_printf(MSG_DEBUG, "nl80211: MLME event Michael MIC failure");
1987 os_memset(&data, 0, sizeof(data));
1988 if (tb[NL80211_ATTR_MAC]) {
1989 wpa_hexdump(MSG_DEBUG, "nl80211: Source MAC address",
1990 nla_data(tb[NL80211_ATTR_MAC]),
1991 nla_len(tb[NL80211_ATTR_MAC]));
1992 data.michael_mic_failure.src = nla_data(tb[NL80211_ATTR_MAC]);
1993 }
1994 if (tb[NL80211_ATTR_KEY_SEQ]) {
1995 wpa_hexdump(MSG_DEBUG, "nl80211: TSC",
1996 nla_data(tb[NL80211_ATTR_KEY_SEQ]),
1997 nla_len(tb[NL80211_ATTR_KEY_SEQ]));
1998 }
1999 if (tb[NL80211_ATTR_KEY_TYPE]) {
2000 enum nl80211_key_type key_type =
2001 nla_get_u32(tb[NL80211_ATTR_KEY_TYPE]);
2002 wpa_printf(MSG_DEBUG, "nl80211: Key Type %d", key_type);
2003 if (key_type == NL80211_KEYTYPE_PAIRWISE)
2004 data.michael_mic_failure.unicast = 1;
2005 } else
2006 data.michael_mic_failure.unicast = 1;
2007
2008 if (tb[NL80211_ATTR_KEY_IDX]) {
2009 u8 key_id = nla_get_u8(tb[NL80211_ATTR_KEY_IDX]);
2010 wpa_printf(MSG_DEBUG, "nl80211: Key Id %d", key_id);
2011 }
2012
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002013 wpa_supplicant_event(bss->ctx, EVENT_MICHAEL_MIC_FAILURE, &data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002014}
2015
2016
2017static void mlme_event_join_ibss(struct wpa_driver_nl80211_data *drv,
2018 struct nlattr *tb[])
2019{
2020 if (tb[NL80211_ATTR_MAC] == NULL) {
2021 wpa_printf(MSG_DEBUG, "nl80211: No address in IBSS joined "
2022 "event");
2023 return;
2024 }
2025 os_memcpy(drv->bssid, nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002026
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002027 drv->associated = 1;
2028 wpa_printf(MSG_DEBUG, "nl80211: IBSS " MACSTR " joined",
2029 MAC2STR(drv->bssid));
2030
2031 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, NULL);
2032}
2033
2034
2035static void mlme_event_remain_on_channel(struct wpa_driver_nl80211_data *drv,
2036 int cancel_event, struct nlattr *tb[])
2037{
2038 unsigned int freq, chan_type, duration;
2039 union wpa_event_data data;
2040 u64 cookie;
2041
2042 if (tb[NL80211_ATTR_WIPHY_FREQ])
2043 freq = nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]);
2044 else
2045 freq = 0;
2046
2047 if (tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE])
2048 chan_type = nla_get_u32(tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
2049 else
2050 chan_type = 0;
2051
2052 if (tb[NL80211_ATTR_DURATION])
2053 duration = nla_get_u32(tb[NL80211_ATTR_DURATION]);
2054 else
2055 duration = 0;
2056
2057 if (tb[NL80211_ATTR_COOKIE])
2058 cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
2059 else
2060 cookie = 0;
2061
2062 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel event (cancel=%d "
2063 "freq=%u channel_type=%u duration=%u cookie=0x%llx (%s))",
2064 cancel_event, freq, chan_type, duration,
2065 (long long unsigned int) cookie,
2066 cookie == drv->remain_on_chan_cookie ? "match" : "unknown");
2067
2068 if (cookie != drv->remain_on_chan_cookie)
2069 return; /* not for us */
2070
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002071 if (cancel_event)
2072 drv->pending_remain_on_chan = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002073
2074 os_memset(&data, 0, sizeof(data));
2075 data.remain_on_channel.freq = freq;
2076 data.remain_on_channel.duration = duration;
2077 wpa_supplicant_event(drv->ctx, cancel_event ?
2078 EVENT_CANCEL_REMAIN_ON_CHANNEL :
2079 EVENT_REMAIN_ON_CHANNEL, &data);
2080}
2081
2082
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002083static void mlme_event_ft_event(struct wpa_driver_nl80211_data *drv,
2084 struct nlattr *tb[])
2085{
2086 union wpa_event_data data;
2087
2088 os_memset(&data, 0, sizeof(data));
2089
2090 if (tb[NL80211_ATTR_IE]) {
2091 data.ft_ies.ies = nla_data(tb[NL80211_ATTR_IE]);
2092 data.ft_ies.ies_len = nla_len(tb[NL80211_ATTR_IE]);
2093 }
2094
2095 if (tb[NL80211_ATTR_IE_RIC]) {
2096 data.ft_ies.ric_ies = nla_data(tb[NL80211_ATTR_IE_RIC]);
2097 data.ft_ies.ric_ies_len = nla_len(tb[NL80211_ATTR_IE_RIC]);
2098 }
2099
2100 if (tb[NL80211_ATTR_MAC])
2101 os_memcpy(data.ft_ies.target_ap,
2102 nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
2103
2104 wpa_printf(MSG_DEBUG, "nl80211: FT event target_ap " MACSTR,
2105 MAC2STR(data.ft_ies.target_ap));
2106
2107 wpa_supplicant_event(drv->ctx, EVENT_FT_RESPONSE, &data);
2108}
2109
2110
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002111static void send_scan_event(struct wpa_driver_nl80211_data *drv, int aborted,
2112 struct nlattr *tb[])
2113{
2114 union wpa_event_data event;
2115 struct nlattr *nl;
2116 int rem;
2117 struct scan_info *info;
2118#define MAX_REPORT_FREQS 50
2119 int freqs[MAX_REPORT_FREQS];
2120 int num_freqs = 0;
2121
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002122 if (drv->scan_for_auth) {
2123 drv->scan_for_auth = 0;
2124 wpa_printf(MSG_DEBUG, "nl80211: Scan results for missing "
2125 "cfg80211 BSS entry");
2126 wpa_driver_nl80211_authenticate_retry(drv);
2127 return;
2128 }
2129
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002130 os_memset(&event, 0, sizeof(event));
2131 info = &event.scan_info;
2132 info->aborted = aborted;
2133
2134 if (tb[NL80211_ATTR_SCAN_SSIDS]) {
2135 nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_SSIDS], rem) {
2136 struct wpa_driver_scan_ssid *s =
2137 &info->ssids[info->num_ssids];
2138 s->ssid = nla_data(nl);
2139 s->ssid_len = nla_len(nl);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002140 wpa_printf(MSG_DEBUG, "nl80211: Scan probed for SSID '%s'",
2141 wpa_ssid_txt(s->ssid, s->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002142 info->num_ssids++;
2143 if (info->num_ssids == WPAS_MAX_SCAN_SSIDS)
2144 break;
2145 }
2146 }
2147 if (tb[NL80211_ATTR_SCAN_FREQUENCIES]) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002148 char msg[200], *pos, *end;
2149 int res;
2150
2151 pos = msg;
2152 end = pos + sizeof(msg);
2153 *pos = '\0';
2154
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002155 nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_FREQUENCIES], rem)
2156 {
2157 freqs[num_freqs] = nla_get_u32(nl);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002158 res = os_snprintf(pos, end - pos, " %d",
2159 freqs[num_freqs]);
2160 if (res > 0 && end - pos > res)
2161 pos += res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002162 num_freqs++;
2163 if (num_freqs == MAX_REPORT_FREQS - 1)
2164 break;
2165 }
2166 info->freqs = freqs;
2167 info->num_freqs = num_freqs;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002168 wpa_printf(MSG_DEBUG, "nl80211: Scan included frequencies:%s",
2169 msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002170 }
2171 wpa_supplicant_event(drv->ctx, EVENT_SCAN_RESULTS, &event);
2172}
2173
2174
2175static int get_link_signal(struct nl_msg *msg, void *arg)
2176{
2177 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2178 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2179 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
2180 static struct nla_policy policy[NL80211_STA_INFO_MAX + 1] = {
2181 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002182 [NL80211_STA_INFO_SIGNAL_AVG] = { .type = NLA_U8 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002183 };
2184 struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
2185 static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
2186 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
2187 [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
2188 [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
2189 [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
2190 };
2191 struct wpa_signal_info *sig_change = arg;
2192
2193 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2194 genlmsg_attrlen(gnlh, 0), NULL);
2195 if (!tb[NL80211_ATTR_STA_INFO] ||
2196 nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
2197 tb[NL80211_ATTR_STA_INFO], policy))
2198 return NL_SKIP;
2199 if (!sinfo[NL80211_STA_INFO_SIGNAL])
2200 return NL_SKIP;
2201
2202 sig_change->current_signal =
2203 (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
2204
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002205 if (sinfo[NL80211_STA_INFO_SIGNAL_AVG])
2206 sig_change->avg_signal =
2207 (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL_AVG]);
2208 else
2209 sig_change->avg_signal = 0;
2210
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002211 if (sinfo[NL80211_STA_INFO_TX_BITRATE]) {
2212 if (nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
2213 sinfo[NL80211_STA_INFO_TX_BITRATE],
2214 rate_policy)) {
2215 sig_change->current_txrate = 0;
2216 } else {
2217 if (rinfo[NL80211_RATE_INFO_BITRATE]) {
2218 sig_change->current_txrate =
2219 nla_get_u16(rinfo[
2220 NL80211_RATE_INFO_BITRATE]) * 100;
2221 }
2222 }
2223 }
2224
2225 return NL_SKIP;
2226}
2227
2228
2229static int nl80211_get_link_signal(struct wpa_driver_nl80211_data *drv,
2230 struct wpa_signal_info *sig)
2231{
2232 struct nl_msg *msg;
2233
2234 sig->current_signal = -9999;
2235 sig->current_txrate = 0;
2236
2237 msg = nlmsg_alloc();
2238 if (!msg)
2239 return -ENOMEM;
2240
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002241 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002242
2243 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
2244 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid);
2245
2246 return send_and_recv_msgs(drv, msg, get_link_signal, sig);
2247 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002248 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002249 return -ENOBUFS;
2250}
2251
2252
2253static int get_link_noise(struct nl_msg *msg, void *arg)
2254{
2255 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2256 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2257 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
2258 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
2259 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
2260 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
2261 };
2262 struct wpa_signal_info *sig_change = arg;
2263
2264 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2265 genlmsg_attrlen(gnlh, 0), NULL);
2266
2267 if (!tb[NL80211_ATTR_SURVEY_INFO]) {
2268 wpa_printf(MSG_DEBUG, "nl80211: survey data missing!");
2269 return NL_SKIP;
2270 }
2271
2272 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
2273 tb[NL80211_ATTR_SURVEY_INFO],
2274 survey_policy)) {
2275 wpa_printf(MSG_DEBUG, "nl80211: failed to parse nested "
2276 "attributes!");
2277 return NL_SKIP;
2278 }
2279
2280 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
2281 return NL_SKIP;
2282
2283 if (nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
2284 sig_change->frequency)
2285 return NL_SKIP;
2286
2287 if (!sinfo[NL80211_SURVEY_INFO_NOISE])
2288 return NL_SKIP;
2289
2290 sig_change->current_noise =
2291 (s8) nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
2292
2293 return NL_SKIP;
2294}
2295
2296
2297static int nl80211_get_link_noise(struct wpa_driver_nl80211_data *drv,
2298 struct wpa_signal_info *sig_change)
2299{
2300 struct nl_msg *msg;
2301
2302 sig_change->current_noise = 9999;
2303 sig_change->frequency = drv->assoc_freq;
2304
2305 msg = nlmsg_alloc();
2306 if (!msg)
2307 return -ENOMEM;
2308
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002309 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002310
2311 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
2312
2313 return send_and_recv_msgs(drv, msg, get_link_noise, sig_change);
2314 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002315 nlmsg_free(msg);
2316 return -ENOBUFS;
2317}
2318
2319
2320static int get_noise_for_scan_results(struct nl_msg *msg, void *arg)
2321{
2322 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2323 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2324 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
2325 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
2326 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
2327 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
2328 };
2329 struct wpa_scan_results *scan_results = arg;
2330 struct wpa_scan_res *scan_res;
2331 size_t i;
2332
2333 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2334 genlmsg_attrlen(gnlh, 0), NULL);
2335
2336 if (!tb[NL80211_ATTR_SURVEY_INFO]) {
2337 wpa_printf(MSG_DEBUG, "nl80211: Survey data missing");
2338 return NL_SKIP;
2339 }
2340
2341 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
2342 tb[NL80211_ATTR_SURVEY_INFO],
2343 survey_policy)) {
2344 wpa_printf(MSG_DEBUG, "nl80211: Failed to parse nested "
2345 "attributes");
2346 return NL_SKIP;
2347 }
2348
2349 if (!sinfo[NL80211_SURVEY_INFO_NOISE])
2350 return NL_SKIP;
2351
2352 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
2353 return NL_SKIP;
2354
2355 for (i = 0; i < scan_results->num; ++i) {
2356 scan_res = scan_results->res[i];
2357 if (!scan_res)
2358 continue;
2359 if ((int) nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
2360 scan_res->freq)
2361 continue;
2362 if (!(scan_res->flags & WPA_SCAN_NOISE_INVALID))
2363 continue;
2364 scan_res->noise = (s8)
2365 nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
2366 scan_res->flags &= ~WPA_SCAN_NOISE_INVALID;
2367 }
2368
2369 return NL_SKIP;
2370}
2371
2372
2373static int nl80211_get_noise_for_scan_results(
2374 struct wpa_driver_nl80211_data *drv,
2375 struct wpa_scan_results *scan_res)
2376{
2377 struct nl_msg *msg;
2378
2379 msg = nlmsg_alloc();
2380 if (!msg)
2381 return -ENOMEM;
2382
2383 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
2384
2385 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
2386
2387 return send_and_recv_msgs(drv, msg, get_noise_for_scan_results,
2388 scan_res);
2389 nla_put_failure:
2390 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002391 return -ENOBUFS;
2392}
2393
2394
2395static void nl80211_cqm_event(struct wpa_driver_nl80211_data *drv,
2396 struct nlattr *tb[])
2397{
2398 static struct nla_policy cqm_policy[NL80211_ATTR_CQM_MAX + 1] = {
2399 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
2400 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U8 },
2401 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
2402 [NL80211_ATTR_CQM_PKT_LOSS_EVENT] = { .type = NLA_U32 },
2403 };
2404 struct nlattr *cqm[NL80211_ATTR_CQM_MAX + 1];
2405 enum nl80211_cqm_rssi_threshold_event event;
2406 union wpa_event_data ed;
2407 struct wpa_signal_info sig;
2408 int res;
2409
2410 if (tb[NL80211_ATTR_CQM] == NULL ||
2411 nla_parse_nested(cqm, NL80211_ATTR_CQM_MAX, tb[NL80211_ATTR_CQM],
2412 cqm_policy)) {
2413 wpa_printf(MSG_DEBUG, "nl80211: Ignore invalid CQM event");
2414 return;
2415 }
2416
2417 os_memset(&ed, 0, sizeof(ed));
2418
2419 if (cqm[NL80211_ATTR_CQM_PKT_LOSS_EVENT]) {
2420 if (!tb[NL80211_ATTR_MAC])
2421 return;
2422 os_memcpy(ed.low_ack.addr, nla_data(tb[NL80211_ATTR_MAC]),
2423 ETH_ALEN);
2424 wpa_supplicant_event(drv->ctx, EVENT_STATION_LOW_ACK, &ed);
2425 return;
2426 }
2427
2428 if (cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] == NULL)
2429 return;
2430 event = nla_get_u32(cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT]);
2431
2432 if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH) {
2433 wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor "
2434 "event: RSSI high");
2435 ed.signal_change.above_threshold = 1;
2436 } else if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW) {
2437 wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor "
2438 "event: RSSI low");
2439 ed.signal_change.above_threshold = 0;
2440 } else
2441 return;
2442
2443 res = nl80211_get_link_signal(drv, &sig);
2444 if (res == 0) {
2445 ed.signal_change.current_signal = sig.current_signal;
2446 ed.signal_change.current_txrate = sig.current_txrate;
2447 wpa_printf(MSG_DEBUG, "nl80211: Signal: %d dBm txrate: %d",
2448 sig.current_signal, sig.current_txrate);
2449 }
2450
2451 res = nl80211_get_link_noise(drv, &sig);
2452 if (res == 0) {
2453 ed.signal_change.current_noise = sig.current_noise;
2454 wpa_printf(MSG_DEBUG, "nl80211: Noise: %d dBm",
2455 sig.current_noise);
2456 }
2457
2458 wpa_supplicant_event(drv->ctx, EVENT_SIGNAL_CHANGE, &ed);
2459}
2460
2461
2462static void nl80211_new_station_event(struct wpa_driver_nl80211_data *drv,
2463 struct nlattr **tb)
2464{
2465 u8 *addr;
2466 union wpa_event_data data;
2467
2468 if (tb[NL80211_ATTR_MAC] == NULL)
2469 return;
2470 addr = nla_data(tb[NL80211_ATTR_MAC]);
2471 wpa_printf(MSG_DEBUG, "nl80211: New station " MACSTR, MAC2STR(addr));
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07002472
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002473 if (is_ap_interface(drv->nlmode) && drv->device_ap_sme) {
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07002474 u8 *ies = NULL;
2475 size_t ies_len = 0;
2476 if (tb[NL80211_ATTR_IE]) {
2477 ies = nla_data(tb[NL80211_ATTR_IE]);
2478 ies_len = nla_len(tb[NL80211_ATTR_IE]);
2479 }
2480 wpa_hexdump(MSG_DEBUG, "nl80211: Assoc Req IEs", ies, ies_len);
2481 drv_event_assoc(drv->ctx, addr, ies, ies_len, 0);
2482 return;
2483 }
2484
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002485 if (drv->nlmode != NL80211_IFTYPE_ADHOC)
2486 return;
2487
2488 os_memset(&data, 0, sizeof(data));
2489 os_memcpy(data.ibss_rsn_start.peer, addr, ETH_ALEN);
2490 wpa_supplicant_event(drv->ctx, EVENT_IBSS_RSN_START, &data);
2491}
2492
2493
2494static void nl80211_del_station_event(struct wpa_driver_nl80211_data *drv,
2495 struct nlattr **tb)
2496{
2497 u8 *addr;
2498 union wpa_event_data data;
2499
2500 if (tb[NL80211_ATTR_MAC] == NULL)
2501 return;
2502 addr = nla_data(tb[NL80211_ATTR_MAC]);
2503 wpa_printf(MSG_DEBUG, "nl80211: Delete station " MACSTR,
2504 MAC2STR(addr));
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07002505
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002506 if (is_ap_interface(drv->nlmode) && drv->device_ap_sme) {
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07002507 drv_event_disassoc(drv->ctx, addr);
2508 return;
2509 }
2510
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002511 if (drv->nlmode != NL80211_IFTYPE_ADHOC)
2512 return;
2513
2514 os_memset(&data, 0, sizeof(data));
2515 os_memcpy(data.ibss_peer_lost.peer, addr, ETH_ALEN);
2516 wpa_supplicant_event(drv->ctx, EVENT_IBSS_PEER_LOST, &data);
2517}
2518
2519
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002520static void nl80211_rekey_offload_event(struct wpa_driver_nl80211_data *drv,
2521 struct nlattr **tb)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002522{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002523 struct nlattr *rekey_info[NUM_NL80211_REKEY_DATA];
2524 static struct nla_policy rekey_policy[NUM_NL80211_REKEY_DATA] = {
2525 [NL80211_REKEY_DATA_KEK] = {
2526 .minlen = NL80211_KEK_LEN,
2527 .maxlen = NL80211_KEK_LEN,
2528 },
2529 [NL80211_REKEY_DATA_KCK] = {
2530 .minlen = NL80211_KCK_LEN,
2531 .maxlen = NL80211_KCK_LEN,
2532 },
2533 [NL80211_REKEY_DATA_REPLAY_CTR] = {
2534 .minlen = NL80211_REPLAY_CTR_LEN,
2535 .maxlen = NL80211_REPLAY_CTR_LEN,
2536 },
2537 };
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002538 union wpa_event_data data;
2539
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002540 if (!tb[NL80211_ATTR_MAC])
2541 return;
2542 if (!tb[NL80211_ATTR_REKEY_DATA])
2543 return;
2544 if (nla_parse_nested(rekey_info, MAX_NL80211_REKEY_DATA,
2545 tb[NL80211_ATTR_REKEY_DATA], rekey_policy))
2546 return;
2547 if (!rekey_info[NL80211_REKEY_DATA_REPLAY_CTR])
2548 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002549
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002550 os_memset(&data, 0, sizeof(data));
2551 data.driver_gtk_rekey.bssid = nla_data(tb[NL80211_ATTR_MAC]);
2552 wpa_printf(MSG_DEBUG, "nl80211: Rekey offload event for BSSID " MACSTR,
2553 MAC2STR(data.driver_gtk_rekey.bssid));
2554 data.driver_gtk_rekey.replay_ctr =
2555 nla_data(rekey_info[NL80211_REKEY_DATA_REPLAY_CTR]);
2556 wpa_hexdump(MSG_DEBUG, "nl80211: Rekey offload - Replay Counter",
2557 data.driver_gtk_rekey.replay_ctr, NL80211_REPLAY_CTR_LEN);
2558 wpa_supplicant_event(drv->ctx, EVENT_DRIVER_GTK_REKEY, &data);
2559}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002560
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002561
2562static void nl80211_pmksa_candidate_event(struct wpa_driver_nl80211_data *drv,
2563 struct nlattr **tb)
2564{
2565 struct nlattr *cand[NUM_NL80211_PMKSA_CANDIDATE];
2566 static struct nla_policy cand_policy[NUM_NL80211_PMKSA_CANDIDATE] = {
2567 [NL80211_PMKSA_CANDIDATE_INDEX] = { .type = NLA_U32 },
2568 [NL80211_PMKSA_CANDIDATE_BSSID] = {
2569 .minlen = ETH_ALEN,
2570 .maxlen = ETH_ALEN,
2571 },
2572 [NL80211_PMKSA_CANDIDATE_PREAUTH] = { .type = NLA_FLAG },
2573 };
2574 union wpa_event_data data;
2575
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002576 wpa_printf(MSG_DEBUG, "nl80211: PMKSA candidate event");
2577
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002578 if (!tb[NL80211_ATTR_PMKSA_CANDIDATE])
2579 return;
2580 if (nla_parse_nested(cand, MAX_NL80211_PMKSA_CANDIDATE,
2581 tb[NL80211_ATTR_PMKSA_CANDIDATE], cand_policy))
2582 return;
2583 if (!cand[NL80211_PMKSA_CANDIDATE_INDEX] ||
2584 !cand[NL80211_PMKSA_CANDIDATE_BSSID])
2585 return;
2586
2587 os_memset(&data, 0, sizeof(data));
2588 os_memcpy(data.pmkid_candidate.bssid,
2589 nla_data(cand[NL80211_PMKSA_CANDIDATE_BSSID]), ETH_ALEN);
2590 data.pmkid_candidate.index =
2591 nla_get_u32(cand[NL80211_PMKSA_CANDIDATE_INDEX]);
2592 data.pmkid_candidate.preauth =
2593 cand[NL80211_PMKSA_CANDIDATE_PREAUTH] != NULL;
2594 wpa_supplicant_event(drv->ctx, EVENT_PMKID_CANDIDATE, &data);
2595}
2596
2597
2598static void nl80211_client_probe_event(struct wpa_driver_nl80211_data *drv,
2599 struct nlattr **tb)
2600{
2601 union wpa_event_data data;
2602
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002603 wpa_printf(MSG_DEBUG, "nl80211: Probe client event");
2604
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002605 if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_ACK])
2606 return;
2607
2608 os_memset(&data, 0, sizeof(data));
2609 os_memcpy(data.client_poll.addr,
2610 nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
2611
2612 wpa_supplicant_event(drv->ctx, EVENT_DRIVER_CLIENT_POLL_OK, &data);
2613}
2614
2615
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002616static void nl80211_tdls_oper_event(struct wpa_driver_nl80211_data *drv,
2617 struct nlattr **tb)
2618{
2619 union wpa_event_data data;
2620
2621 wpa_printf(MSG_DEBUG, "nl80211: TDLS operation event");
2622
2623 if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_TDLS_OPERATION])
2624 return;
2625
2626 os_memset(&data, 0, sizeof(data));
2627 os_memcpy(data.tdls.peer, nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
2628 switch (nla_get_u8(tb[NL80211_ATTR_TDLS_OPERATION])) {
2629 case NL80211_TDLS_SETUP:
2630 wpa_printf(MSG_DEBUG, "nl80211: TDLS setup request for peer "
2631 MACSTR, MAC2STR(data.tdls.peer));
2632 data.tdls.oper = TDLS_REQUEST_SETUP;
2633 break;
2634 case NL80211_TDLS_TEARDOWN:
2635 wpa_printf(MSG_DEBUG, "nl80211: TDLS teardown request for peer "
2636 MACSTR, MAC2STR(data.tdls.peer));
2637 data.tdls.oper = TDLS_REQUEST_TEARDOWN;
2638 break;
2639 default:
2640 wpa_printf(MSG_DEBUG, "nl80211: Unsupported TDLS operatione "
2641 "event");
2642 return;
2643 }
2644 if (tb[NL80211_ATTR_REASON_CODE]) {
2645 data.tdls.reason_code =
2646 nla_get_u16(tb[NL80211_ATTR_REASON_CODE]);
2647 }
2648
2649 wpa_supplicant_event(drv->ctx, EVENT_TDLS, &data);
2650}
2651
2652
Dmitry Shmidt5393a0f2013-08-08 11:23:34 -07002653static void nl80211_stop_ap(struct wpa_driver_nl80211_data *drv,
2654 struct nlattr **tb)
2655{
2656 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_UNAVAILABLE, NULL);
2657}
2658
2659
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002660static void nl80211_connect_failed_event(struct wpa_driver_nl80211_data *drv,
2661 struct nlattr **tb)
2662{
2663 union wpa_event_data data;
2664 u32 reason;
2665
2666 wpa_printf(MSG_DEBUG, "nl80211: Connect failed event");
2667
2668 if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_CONN_FAILED_REASON])
2669 return;
2670
2671 os_memset(&data, 0, sizeof(data));
2672 os_memcpy(data.connect_failed_reason.addr,
2673 nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
2674
2675 reason = nla_get_u32(tb[NL80211_ATTR_CONN_FAILED_REASON]);
2676 switch (reason) {
2677 case NL80211_CONN_FAIL_MAX_CLIENTS:
2678 wpa_printf(MSG_DEBUG, "nl80211: Max client reached");
2679 data.connect_failed_reason.code = MAX_CLIENT_REACHED;
2680 break;
2681 case NL80211_CONN_FAIL_BLOCKED_CLIENT:
2682 wpa_printf(MSG_DEBUG, "nl80211: Blocked client " MACSTR
2683 " tried to connect",
2684 MAC2STR(data.connect_failed_reason.addr));
2685 data.connect_failed_reason.code = BLOCKED_CLIENT;
2686 break;
2687 default:
2688 wpa_printf(MSG_DEBUG, "nl8021l: Unknown connect failed reason "
2689 "%u", reason);
2690 return;
2691 }
2692
2693 wpa_supplicant_event(drv->ctx, EVENT_CONNECT_FAILED_REASON, &data);
2694}
2695
2696
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002697static void nl80211_radar_event(struct wpa_driver_nl80211_data *drv,
2698 struct nlattr **tb)
2699{
2700 union wpa_event_data data;
2701 enum nl80211_radar_event event_type;
2702
2703 if (!tb[NL80211_ATTR_WIPHY_FREQ] || !tb[NL80211_ATTR_RADAR_EVENT])
2704 return;
2705
2706 os_memset(&data, 0, sizeof(data));
Dmitry Shmidt051af732013-10-22 13:52:46 -07002707 data.dfs_event.freq = nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]);
2708 event_type = nla_get_u32(tb[NL80211_ATTR_RADAR_EVENT]);
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002709
Dmitry Shmidt051af732013-10-22 13:52:46 -07002710 /* Check HT params */
2711 if (tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
2712 data.dfs_event.ht_enabled = 1;
2713 data.dfs_event.chan_offset = 0;
2714
2715 switch (nla_get_u32(tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE])) {
2716 case NL80211_CHAN_NO_HT:
2717 data.dfs_event.ht_enabled = 0;
2718 break;
2719 case NL80211_CHAN_HT20:
2720 break;
2721 case NL80211_CHAN_HT40PLUS:
2722 data.dfs_event.chan_offset = 1;
2723 break;
2724 case NL80211_CHAN_HT40MINUS:
2725 data.dfs_event.chan_offset = -1;
2726 break;
2727 }
2728 }
2729
2730 /* Get VHT params */
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002731 if (tb[NL80211_ATTR_CHANNEL_WIDTH])
2732 data.dfs_event.chan_width =
2733 convert2width(nla_get_u32(
2734 tb[NL80211_ATTR_CHANNEL_WIDTH]));
2735 if (tb[NL80211_ATTR_CENTER_FREQ1])
2736 data.dfs_event.cf1 = nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002737 if (tb[NL80211_ATTR_CENTER_FREQ2])
2738 data.dfs_event.cf2 = nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]);
2739
2740 wpa_printf(MSG_DEBUG, "nl80211: DFS event on freq %d MHz, ht: %d, offset: %d, width: %d, cf1: %dMHz, cf2: %dMHz",
2741 data.dfs_event.freq, data.dfs_event.ht_enabled,
2742 data.dfs_event.chan_offset, data.dfs_event.chan_width,
2743 data.dfs_event.cf1, data.dfs_event.cf2);
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002744
2745 switch (event_type) {
2746 case NL80211_RADAR_DETECTED:
2747 wpa_supplicant_event(drv->ctx, EVENT_DFS_RADAR_DETECTED, &data);
2748 break;
2749 case NL80211_RADAR_CAC_FINISHED:
2750 wpa_supplicant_event(drv->ctx, EVENT_DFS_CAC_FINISHED, &data);
2751 break;
2752 case NL80211_RADAR_CAC_ABORTED:
2753 wpa_supplicant_event(drv->ctx, EVENT_DFS_CAC_ABORTED, &data);
2754 break;
2755 case NL80211_RADAR_NOP_FINISHED:
2756 wpa_supplicant_event(drv->ctx, EVENT_DFS_NOP_FINISHED, &data);
2757 break;
2758 default:
2759 wpa_printf(MSG_DEBUG, "nl80211: Unknown radar event %d "
2760 "received", event_type);
2761 break;
2762 }
2763}
2764
2765
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002766static void nl80211_spurious_frame(struct i802_bss *bss, struct nlattr **tb,
2767 int wds)
2768{
2769 struct wpa_driver_nl80211_data *drv = bss->drv;
2770 union wpa_event_data event;
2771
2772 if (!tb[NL80211_ATTR_MAC])
2773 return;
2774
2775 os_memset(&event, 0, sizeof(event));
2776 event.rx_from_unknown.bssid = bss->addr;
2777 event.rx_from_unknown.addr = nla_data(tb[NL80211_ATTR_MAC]);
2778 event.rx_from_unknown.wds = wds;
2779
2780 wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event);
2781}
2782
2783
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002784static void qca_nl80211_avoid_freq(struct wpa_driver_nl80211_data *drv,
2785 const u8 *data, size_t len)
2786{
2787 u32 i, count;
2788 union wpa_event_data event;
2789 struct wpa_freq_range *range = NULL;
2790 const struct qca_avoid_freq_list *freq_range;
2791
2792 freq_range = (const struct qca_avoid_freq_list *) data;
2793 if (len < sizeof(freq_range->count))
2794 return;
2795
2796 count = freq_range->count;
2797 if (len < sizeof(freq_range->count) +
2798 count * sizeof(struct qca_avoid_freq_range)) {
2799 wpa_printf(MSG_DEBUG, "nl80211: Ignored too short avoid frequency list (len=%u)",
2800 (unsigned int) len);
2801 return;
2802 }
2803
2804 if (count > 0) {
2805 range = os_calloc(count, sizeof(struct wpa_freq_range));
2806 if (range == NULL)
2807 return;
2808 }
2809
2810 os_memset(&event, 0, sizeof(event));
2811 for (i = 0; i < count; i++) {
2812 unsigned int idx = event.freq_range.num;
2813 range[idx].min = freq_range->range[i].start_freq;
2814 range[idx].max = freq_range->range[i].end_freq;
2815 wpa_printf(MSG_DEBUG, "nl80211: Avoid frequency range: %u-%u",
2816 range[idx].min, range[idx].max);
2817 if (range[idx].min > range[idx].max) {
2818 wpa_printf(MSG_DEBUG, "nl80211: Ignore invalid frequency range");
2819 continue;
2820 }
2821 event.freq_range.num++;
2822 }
2823 event.freq_range.range = range;
2824
2825 wpa_supplicant_event(drv->ctx, EVENT_AVOID_FREQUENCIES, &event);
2826
2827 os_free(range);
2828}
2829
2830
2831static void nl80211_vendor_event_qca(struct wpa_driver_nl80211_data *drv,
2832 u32 subcmd, u8 *data, size_t len)
2833{
2834 switch (subcmd) {
2835 case QCA_NL80211_VENDOR_SUBCMD_AVOID_FREQUENCY:
2836 qca_nl80211_avoid_freq(drv, data, len);
2837 break;
2838 default:
2839 wpa_printf(MSG_DEBUG,
2840 "nl80211: Ignore unsupported QCA vendor event %u",
2841 subcmd);
2842 break;
2843 }
2844}
2845
2846
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002847static void nl80211_vendor_event(struct wpa_driver_nl80211_data *drv,
2848 struct nlattr **tb)
2849{
2850 u32 vendor_id, subcmd, wiphy = 0;
2851 int wiphy_idx;
2852 u8 *data = NULL;
2853 size_t len = 0;
2854
2855 if (!tb[NL80211_ATTR_VENDOR_ID] ||
2856 !tb[NL80211_ATTR_VENDOR_SUBCMD])
2857 return;
2858
2859 vendor_id = nla_get_u32(tb[NL80211_ATTR_VENDOR_ID]);
2860 subcmd = nla_get_u32(tb[NL80211_ATTR_VENDOR_SUBCMD]);
2861
2862 if (tb[NL80211_ATTR_WIPHY])
2863 wiphy = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
2864
2865 wpa_printf(MSG_DEBUG, "nl80211: Vendor event: wiphy=%u vendor_id=0x%x subcmd=%u",
2866 wiphy, vendor_id, subcmd);
2867
2868 if (tb[NL80211_ATTR_VENDOR_DATA]) {
2869 data = nla_data(tb[NL80211_ATTR_VENDOR_DATA]);
2870 len = nla_len(tb[NL80211_ATTR_VENDOR_DATA]);
2871 wpa_hexdump(MSG_MSGDUMP, "nl80211: Vendor data", data, len);
2872 }
2873
2874 wiphy_idx = nl80211_get_wiphy_index(drv->first_bss);
2875 if (wiphy_idx >= 0 && wiphy_idx != (int) wiphy) {
2876 wpa_printf(MSG_DEBUG, "nl80211: Ignore vendor event for foreign wiphy %u (own: %d)",
2877 wiphy, wiphy_idx);
2878 return;
2879 }
2880
2881 switch (vendor_id) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002882 case OUI_QCA:
2883 nl80211_vendor_event_qca(drv, subcmd, data, len);
2884 break;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002885 default:
2886 wpa_printf(MSG_DEBUG, "nl80211: Ignore unsupported vendor event");
2887 break;
2888 }
2889}
2890
2891
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07002892static void nl80211_reg_change_event(struct wpa_driver_nl80211_data *drv,
2893 struct nlattr *tb[])
2894{
2895 union wpa_event_data data;
2896 enum nl80211_reg_initiator init;
2897
2898 wpa_printf(MSG_DEBUG, "nl80211: Regulatory domain change");
2899
2900 if (tb[NL80211_ATTR_REG_INITIATOR] == NULL)
2901 return;
2902
2903 os_memset(&data, 0, sizeof(data));
2904 init = nla_get_u8(tb[NL80211_ATTR_REG_INITIATOR]);
2905 wpa_printf(MSG_DEBUG, " * initiator=%d", init);
2906 switch (init) {
2907 case NL80211_REGDOM_SET_BY_CORE:
2908 data.channel_list_changed.initiator = REGDOM_SET_BY_CORE;
2909 break;
2910 case NL80211_REGDOM_SET_BY_USER:
2911 data.channel_list_changed.initiator = REGDOM_SET_BY_USER;
2912 break;
2913 case NL80211_REGDOM_SET_BY_DRIVER:
2914 data.channel_list_changed.initiator = REGDOM_SET_BY_DRIVER;
2915 break;
2916 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
2917 data.channel_list_changed.initiator = REGDOM_SET_BY_COUNTRY_IE;
2918 break;
2919 }
2920
2921 if (tb[NL80211_ATTR_REG_TYPE]) {
2922 enum nl80211_reg_type type;
2923 type = nla_get_u8(tb[NL80211_ATTR_REG_TYPE]);
2924 wpa_printf(MSG_DEBUG, " * type=%d", type);
2925 switch (type) {
2926 case NL80211_REGDOM_TYPE_COUNTRY:
2927 data.channel_list_changed.type = REGDOM_TYPE_COUNTRY;
2928 break;
2929 case NL80211_REGDOM_TYPE_WORLD:
2930 data.channel_list_changed.type = REGDOM_TYPE_WORLD;
2931 break;
2932 case NL80211_REGDOM_TYPE_CUSTOM_WORLD:
2933 data.channel_list_changed.type =
2934 REGDOM_TYPE_CUSTOM_WORLD;
2935 break;
2936 case NL80211_REGDOM_TYPE_INTERSECTION:
2937 data.channel_list_changed.type =
2938 REGDOM_TYPE_INTERSECTION;
2939 break;
2940 }
2941 }
2942
2943 if (tb[NL80211_ATTR_REG_ALPHA2]) {
2944 os_strlcpy(data.channel_list_changed.alpha2,
2945 nla_get_string(tb[NL80211_ATTR_REG_ALPHA2]),
2946 sizeof(data.channel_list_changed.alpha2));
2947 wpa_printf(MSG_DEBUG, " * alpha2=%s",
2948 data.channel_list_changed.alpha2);
2949 }
2950
2951 wpa_supplicant_event(drv->ctx, EVENT_CHANNEL_LIST_CHANGED, &data);
2952}
2953
2954
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002955static void do_process_drv_event(struct i802_bss *bss, int cmd,
2956 struct nlattr **tb)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002957{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002958 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002959 union wpa_event_data data;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002960
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002961 wpa_printf(MSG_DEBUG, "nl80211: Drv Event %d (%s) received for %s",
2962 cmd, nl80211_command_to_string(cmd), bss->ifname);
2963
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002964 if (drv->ap_scan_as_station != NL80211_IFTYPE_UNSPECIFIED &&
2965 (cmd == NL80211_CMD_NEW_SCAN_RESULTS ||
2966 cmd == NL80211_CMD_SCAN_ABORTED)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002967 wpa_driver_nl80211_set_mode(drv->first_bss,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002968 drv->ap_scan_as_station);
2969 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002970 }
2971
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002972 switch (cmd) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002973 case NL80211_CMD_TRIGGER_SCAN:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002974 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Scan trigger");
Dmitry Shmidt56052862013-10-04 10:23:25 -07002975 drv->scan_state = SCAN_STARTED;
Dmitry Shmidt6cb1f652014-03-21 10:54:03 -07002976 if (drv->scan_for_auth) {
2977 /*
2978 * Cannot indicate EVENT_SCAN_STARTED here since we skip
2979 * EVENT_SCAN_RESULTS in scan_for_auth case and the
2980 * upper layer implementation could get confused about
2981 * scanning state.
2982 */
2983 wpa_printf(MSG_DEBUG, "nl80211: Do not indicate scan-start event due to internal scan_for_auth");
2984 break;
2985 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002986 wpa_supplicant_event(drv->ctx, EVENT_SCAN_STARTED, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002987 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002988 case NL80211_CMD_START_SCHED_SCAN:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002989 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Sched scan started");
Dmitry Shmidt56052862013-10-04 10:23:25 -07002990 drv->scan_state = SCHED_SCAN_STARTED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002991 break;
2992 case NL80211_CMD_SCHED_SCAN_STOPPED:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002993 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Sched scan stopped");
Dmitry Shmidt56052862013-10-04 10:23:25 -07002994 drv->scan_state = SCHED_SCAN_STOPPED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002995 wpa_supplicant_event(drv->ctx, EVENT_SCHED_SCAN_STOPPED, NULL);
2996 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002997 case NL80211_CMD_NEW_SCAN_RESULTS:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002998 wpa_dbg(drv->ctx, MSG_DEBUG,
2999 "nl80211: New scan results available");
Dmitry Shmidt56052862013-10-04 10:23:25 -07003000 drv->scan_state = SCAN_COMPLETED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003001 drv->scan_complete_events = 1;
3002 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
3003 drv->ctx);
3004 send_scan_event(drv, 0, tb);
3005 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003006 case NL80211_CMD_SCHED_SCAN_RESULTS:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003007 wpa_dbg(drv->ctx, MSG_DEBUG,
3008 "nl80211: New sched scan results available");
Dmitry Shmidt56052862013-10-04 10:23:25 -07003009 drv->scan_state = SCHED_SCAN_RESULTS;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003010 send_scan_event(drv, 0, tb);
3011 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003012 case NL80211_CMD_SCAN_ABORTED:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003013 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Scan aborted");
Dmitry Shmidt56052862013-10-04 10:23:25 -07003014 drv->scan_state = SCAN_ABORTED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003015 /*
3016 * Need to indicate that scan results are available in order
3017 * not to make wpa_supplicant stop its scanning.
3018 */
3019 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
3020 drv->ctx);
3021 send_scan_event(drv, 1, tb);
3022 break;
3023 case NL80211_CMD_AUTHENTICATE:
3024 case NL80211_CMD_ASSOCIATE:
3025 case NL80211_CMD_DEAUTHENTICATE:
3026 case NL80211_CMD_DISASSOCIATE:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003027 case NL80211_CMD_FRAME_TX_STATUS:
3028 case NL80211_CMD_UNPROT_DEAUTHENTICATE:
3029 case NL80211_CMD_UNPROT_DISASSOCIATE:
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003030 mlme_event(bss, cmd, tb[NL80211_ATTR_FRAME],
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003031 tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT],
3032 tb[NL80211_ATTR_WIPHY_FREQ], tb[NL80211_ATTR_ACK],
Dmitry Shmidt04949592012-07-19 12:16:46 -07003033 tb[NL80211_ATTR_COOKIE],
3034 tb[NL80211_ATTR_RX_SIGNAL_DBM]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003035 break;
3036 case NL80211_CMD_CONNECT:
3037 case NL80211_CMD_ROAM:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003038 mlme_event_connect(drv, cmd,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003039 tb[NL80211_ATTR_STATUS_CODE],
3040 tb[NL80211_ATTR_MAC],
3041 tb[NL80211_ATTR_REQ_IE],
3042 tb[NL80211_ATTR_RESP_IE]);
3043 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003044 case NL80211_CMD_CH_SWITCH_NOTIFY:
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08003045 mlme_event_ch_switch(drv,
3046 tb[NL80211_ATTR_IFINDEX],
3047 tb[NL80211_ATTR_WIPHY_FREQ],
3048 tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE],
3049 tb[NL80211_ATTR_CHANNEL_WIDTH],
3050 tb[NL80211_ATTR_CENTER_FREQ1],
3051 tb[NL80211_ATTR_CENTER_FREQ2]);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003052 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003053 case NL80211_CMD_DISCONNECT:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003054 mlme_event_disconnect(drv, tb[NL80211_ATTR_REASON_CODE],
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003055 tb[NL80211_ATTR_MAC],
3056 tb[NL80211_ATTR_DISCONNECTED_BY_AP]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003057 break;
3058 case NL80211_CMD_MICHAEL_MIC_FAILURE:
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003059 mlme_event_michael_mic_failure(bss, tb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003060 break;
3061 case NL80211_CMD_JOIN_IBSS:
3062 mlme_event_join_ibss(drv, tb);
3063 break;
3064 case NL80211_CMD_REMAIN_ON_CHANNEL:
3065 mlme_event_remain_on_channel(drv, 0, tb);
3066 break;
3067 case NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL:
3068 mlme_event_remain_on_channel(drv, 1, tb);
3069 break;
3070 case NL80211_CMD_NOTIFY_CQM:
3071 nl80211_cqm_event(drv, tb);
3072 break;
3073 case NL80211_CMD_REG_CHANGE:
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07003074 nl80211_reg_change_event(drv, tb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003075 break;
3076 case NL80211_CMD_REG_BEACON_HINT:
3077 wpa_printf(MSG_DEBUG, "nl80211: Regulatory beacon hint");
Dmitry Shmidt97672262014-02-03 13:02:54 -08003078 os_memset(&data, 0, sizeof(data));
3079 data.channel_list_changed.initiator = REGDOM_BEACON_HINT;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003080 wpa_supplicant_event(drv->ctx, EVENT_CHANNEL_LIST_CHANGED,
Dmitry Shmidt97672262014-02-03 13:02:54 -08003081 &data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003082 break;
3083 case NL80211_CMD_NEW_STATION:
3084 nl80211_new_station_event(drv, tb);
3085 break;
3086 case NL80211_CMD_DEL_STATION:
3087 nl80211_del_station_event(drv, tb);
3088 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003089 case NL80211_CMD_SET_REKEY_OFFLOAD:
3090 nl80211_rekey_offload_event(drv, tb);
3091 break;
3092 case NL80211_CMD_PMKSA_CANDIDATE:
3093 nl80211_pmksa_candidate_event(drv, tb);
3094 break;
3095 case NL80211_CMD_PROBE_CLIENT:
3096 nl80211_client_probe_event(drv, tb);
3097 break;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003098 case NL80211_CMD_TDLS_OPER:
3099 nl80211_tdls_oper_event(drv, tb);
3100 break;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003101 case NL80211_CMD_CONN_FAILED:
3102 nl80211_connect_failed_event(drv, tb);
3103 break;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003104 case NL80211_CMD_FT_EVENT:
3105 mlme_event_ft_event(drv, tb);
3106 break;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07003107 case NL80211_CMD_RADAR_DETECT:
3108 nl80211_radar_event(drv, tb);
3109 break;
Dmitry Shmidt5393a0f2013-08-08 11:23:34 -07003110 case NL80211_CMD_STOP_AP:
3111 nl80211_stop_ap(drv, tb);
3112 break;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003113 case NL80211_CMD_VENDOR:
3114 nl80211_vendor_event(drv, tb);
3115 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003116 default:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003117 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Ignored unknown event "
3118 "(cmd=%d)", cmd);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003119 break;
3120 }
3121}
3122
3123
3124static int process_drv_event(struct nl_msg *msg, void *arg)
3125{
3126 struct wpa_driver_nl80211_data *drv = arg;
3127 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3128 struct nlattr *tb[NL80211_ATTR_MAX + 1];
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003129 struct i802_bss *bss;
3130 int ifidx = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003131
3132 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3133 genlmsg_attrlen(gnlh, 0), NULL);
3134
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003135 if (tb[NL80211_ATTR_IFINDEX]) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003136 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
3137
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003138 for (bss = drv->first_bss; bss; bss = bss->next)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003139 if (ifidx == -1 || ifidx == bss->ifindex) {
3140 do_process_drv_event(bss, gnlh->cmd, tb);
3141 return NL_SKIP;
3142 }
3143 wpa_printf(MSG_DEBUG,
3144 "nl80211: Ignored event (cmd=%d) for foreign interface (ifindex %d)",
3145 gnlh->cmd, ifidx);
3146 } else if (tb[NL80211_ATTR_WDEV]) {
3147 u64 wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
3148 wpa_printf(MSG_DEBUG, "nl80211: Process event on P2P device");
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003149 for (bss = drv->first_bss; bss; bss = bss->next) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003150 if (bss->wdev_id_set && wdev_id == bss->wdev_id) {
3151 do_process_drv_event(bss, gnlh->cmd, tb);
3152 return NL_SKIP;
3153 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003154 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003155 wpa_printf(MSG_DEBUG,
3156 "nl80211: Ignored event (cmd=%d) for foreign interface (wdev 0x%llx)",
3157 gnlh->cmd, (long long unsigned int) wdev_id);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003158 }
3159
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003160 return NL_SKIP;
3161}
3162
3163
3164static int process_global_event(struct nl_msg *msg, void *arg)
3165{
3166 struct nl80211_global *global = arg;
3167 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3168 struct nlattr *tb[NL80211_ATTR_MAX + 1];
Dmitry Shmidt04949592012-07-19 12:16:46 -07003169 struct wpa_driver_nl80211_data *drv, *tmp;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003170 int ifidx = -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003171 struct i802_bss *bss;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003172 u64 wdev_id = 0;
3173 int wdev_id_set = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003174
3175 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3176 genlmsg_attrlen(gnlh, 0), NULL);
3177
3178 if (tb[NL80211_ATTR_IFINDEX])
3179 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003180 else if (tb[NL80211_ATTR_WDEV]) {
3181 wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
3182 wdev_id_set = 1;
3183 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003184
Dmitry Shmidt04949592012-07-19 12:16:46 -07003185 dl_list_for_each_safe(drv, tmp, &global->interfaces,
3186 struct wpa_driver_nl80211_data, list) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003187 for (bss = drv->first_bss; bss; bss = bss->next) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003188 if ((ifidx == -1 && !wdev_id_set) ||
3189 ifidx == bss->ifindex ||
3190 (wdev_id_set && bss->wdev_id_set &&
3191 wdev_id == bss->wdev_id)) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003192 do_process_drv_event(bss, gnlh->cmd, tb);
3193 return NL_SKIP;
3194 }
3195 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003196 }
3197
3198 return NL_SKIP;
3199}
3200
3201
3202static int process_bss_event(struct nl_msg *msg, void *arg)
3203{
3204 struct i802_bss *bss = arg;
3205 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3206 struct nlattr *tb[NL80211_ATTR_MAX + 1];
3207
3208 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3209 genlmsg_attrlen(gnlh, 0), NULL);
3210
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003211 wpa_printf(MSG_DEBUG, "nl80211: BSS Event %d (%s) received for %s",
3212 gnlh->cmd, nl80211_command_to_string(gnlh->cmd),
3213 bss->ifname);
3214
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003215 switch (gnlh->cmd) {
3216 case NL80211_CMD_FRAME:
3217 case NL80211_CMD_FRAME_TX_STATUS:
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003218 mlme_event(bss, gnlh->cmd, tb[NL80211_ATTR_FRAME],
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003219 tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT],
3220 tb[NL80211_ATTR_WIPHY_FREQ], tb[NL80211_ATTR_ACK],
Dmitry Shmidt04949592012-07-19 12:16:46 -07003221 tb[NL80211_ATTR_COOKIE],
3222 tb[NL80211_ATTR_RX_SIGNAL_DBM]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003223 break;
3224 case NL80211_CMD_UNEXPECTED_FRAME:
3225 nl80211_spurious_frame(bss, tb, 0);
3226 break;
3227 case NL80211_CMD_UNEXPECTED_4ADDR_FRAME:
3228 nl80211_spurious_frame(bss, tb, 1);
3229 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003230 default:
3231 wpa_printf(MSG_DEBUG, "nl80211: Ignored unknown event "
3232 "(cmd=%d)", gnlh->cmd);
3233 break;
3234 }
3235
3236 return NL_SKIP;
3237}
3238
3239
3240static void wpa_driver_nl80211_event_receive(int sock, void *eloop_ctx,
3241 void *handle)
3242{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003243 struct nl_cb *cb = eloop_ctx;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003244 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003245
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003246 wpa_printf(MSG_MSGDUMP, "nl80211: Event message available");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003247
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003248 res = nl_recvmsgs(handle, cb);
3249 if (res) {
3250 wpa_printf(MSG_INFO, "nl80211: %s->nl_recvmsgs failed: %d",
3251 __func__, res);
3252 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003253}
3254
3255
3256/**
3257 * wpa_driver_nl80211_set_country - ask nl80211 to set the regulatory domain
3258 * @priv: driver_nl80211 private data
3259 * @alpha2_arg: country to which to switch to
3260 * Returns: 0 on success, -1 on failure
3261 *
3262 * This asks nl80211 to set the regulatory domain for given
3263 * country ISO / IEC alpha2.
3264 */
3265static int wpa_driver_nl80211_set_country(void *priv, const char *alpha2_arg)
3266{
3267 struct i802_bss *bss = priv;
3268 struct wpa_driver_nl80211_data *drv = bss->drv;
3269 char alpha2[3];
3270 struct nl_msg *msg;
3271
3272 msg = nlmsg_alloc();
3273 if (!msg)
3274 return -ENOMEM;
3275
3276 alpha2[0] = alpha2_arg[0];
3277 alpha2[1] = alpha2_arg[1];
3278 alpha2[2] = '\0';
3279
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003280 nl80211_cmd(drv, msg, 0, NL80211_CMD_REQ_SET_REG);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003281
3282 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, alpha2);
3283 if (send_and_recv_msgs(drv, msg, NULL, NULL))
3284 return -EINVAL;
3285 return 0;
3286nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003287 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003288 return -EINVAL;
3289}
3290
3291
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003292static int nl80211_get_country(struct nl_msg *msg, void *arg)
3293{
3294 char *alpha2 = arg;
3295 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
3296 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3297
3298 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3299 genlmsg_attrlen(gnlh, 0), NULL);
3300 if (!tb_msg[NL80211_ATTR_REG_ALPHA2]) {
3301 wpa_printf(MSG_DEBUG, "nl80211: No country information available");
3302 return NL_SKIP;
3303 }
3304 os_strlcpy(alpha2, nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]), 3);
3305 return NL_SKIP;
3306}
3307
3308
3309static int wpa_driver_nl80211_get_country(void *priv, char *alpha2)
3310{
3311 struct i802_bss *bss = priv;
3312 struct wpa_driver_nl80211_data *drv = bss->drv;
3313 struct nl_msg *msg;
3314 int ret;
3315
3316 msg = nlmsg_alloc();
3317 if (!msg)
3318 return -ENOMEM;
3319
3320 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG);
3321 alpha2[0] = '\0';
3322 ret = send_and_recv_msgs(drv, msg, nl80211_get_country, alpha2);
3323 if (!alpha2[0])
3324 ret = -1;
3325
3326 return ret;
3327}
3328
3329
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003330static int protocol_feature_handler(struct nl_msg *msg, void *arg)
3331{
3332 u32 *feat = arg;
3333 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
3334 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3335
3336 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3337 genlmsg_attrlen(gnlh, 0), NULL);
3338
3339 if (tb_msg[NL80211_ATTR_PROTOCOL_FEATURES])
3340 *feat = nla_get_u32(tb_msg[NL80211_ATTR_PROTOCOL_FEATURES]);
3341
3342 return NL_SKIP;
3343}
3344
3345
3346static u32 get_nl80211_protocol_features(struct wpa_driver_nl80211_data *drv)
3347{
3348 u32 feat = 0;
3349 struct nl_msg *msg;
3350
3351 msg = nlmsg_alloc();
3352 if (!msg)
3353 goto nla_put_failure;
3354
3355 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_PROTOCOL_FEATURES);
3356 if (send_and_recv_msgs(drv, msg, protocol_feature_handler, &feat) == 0)
3357 return feat;
3358
3359 msg = NULL;
3360nla_put_failure:
3361 nlmsg_free(msg);
3362 return 0;
3363}
3364
3365
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003366struct wiphy_info_data {
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003367 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003368 struct wpa_driver_capa *capa;
3369
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003370 unsigned int num_multichan_concurrent;
3371
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003372 unsigned int error:1;
3373 unsigned int device_ap_sme:1;
3374 unsigned int poll_command_supported:1;
3375 unsigned int data_tx_status:1;
3376 unsigned int monitor_supported:1;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003377 unsigned int auth_supported:1;
3378 unsigned int connect_supported:1;
3379 unsigned int p2p_go_supported:1;
3380 unsigned int p2p_client_supported:1;
3381 unsigned int p2p_concurrent:1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003382 unsigned int channel_switch_supported:1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003383 unsigned int set_qos_map_supported:1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003384};
3385
3386
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003387static unsigned int probe_resp_offload_support(int supp_protocols)
3388{
3389 unsigned int prot = 0;
3390
3391 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS)
3392 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS;
3393 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2)
3394 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS2;
3395 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P)
3396 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_P2P;
3397 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_80211U)
3398 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_INTERWORKING;
3399
3400 return prot;
3401}
3402
3403
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003404static void wiphy_info_supported_iftypes(struct wiphy_info_data *info,
3405 struct nlattr *tb)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003406{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003407 struct nlattr *nl_mode;
3408 int i;
3409
3410 if (tb == NULL)
3411 return;
3412
3413 nla_for_each_nested(nl_mode, tb, i) {
3414 switch (nla_type(nl_mode)) {
3415 case NL80211_IFTYPE_AP:
3416 info->capa->flags |= WPA_DRIVER_FLAGS_AP;
3417 break;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003418 case NL80211_IFTYPE_ADHOC:
3419 info->capa->flags |= WPA_DRIVER_FLAGS_IBSS;
3420 break;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003421 case NL80211_IFTYPE_P2P_DEVICE:
3422 info->capa->flags |=
3423 WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE;
3424 break;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003425 case NL80211_IFTYPE_P2P_GO:
3426 info->p2p_go_supported = 1;
3427 break;
3428 case NL80211_IFTYPE_P2P_CLIENT:
3429 info->p2p_client_supported = 1;
3430 break;
3431 case NL80211_IFTYPE_MONITOR:
3432 info->monitor_supported = 1;
3433 break;
3434 }
3435 }
3436}
3437
3438
3439static int wiphy_info_iface_comb_process(struct wiphy_info_data *info,
3440 struct nlattr *nl_combi)
3441{
3442 struct nlattr *tb_comb[NUM_NL80211_IFACE_COMB];
3443 struct nlattr *tb_limit[NUM_NL80211_IFACE_LIMIT];
3444 struct nlattr *nl_limit, *nl_mode;
3445 int err, rem_limit, rem_mode;
3446 int combination_has_p2p = 0, combination_has_mgd = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003447 static struct nla_policy
3448 iface_combination_policy[NUM_NL80211_IFACE_COMB] = {
3449 [NL80211_IFACE_COMB_LIMITS] = { .type = NLA_NESTED },
3450 [NL80211_IFACE_COMB_MAXNUM] = { .type = NLA_U32 },
3451 [NL80211_IFACE_COMB_STA_AP_BI_MATCH] = { .type = NLA_FLAG },
3452 [NL80211_IFACE_COMB_NUM_CHANNELS] = { .type = NLA_U32 },
Dmitry Shmidtea69e842013-05-13 14:52:28 -07003453 [NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS] = { .type = NLA_U32 },
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003454 },
3455 iface_limit_policy[NUM_NL80211_IFACE_LIMIT] = {
3456 [NL80211_IFACE_LIMIT_TYPES] = { .type = NLA_NESTED },
3457 [NL80211_IFACE_LIMIT_MAX] = { .type = NLA_U32 },
3458 };
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003459
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003460 err = nla_parse_nested(tb_comb, MAX_NL80211_IFACE_COMB,
3461 nl_combi, iface_combination_policy);
3462 if (err || !tb_comb[NL80211_IFACE_COMB_LIMITS] ||
3463 !tb_comb[NL80211_IFACE_COMB_MAXNUM] ||
3464 !tb_comb[NL80211_IFACE_COMB_NUM_CHANNELS])
3465 return 0; /* broken combination */
3466
Dmitry Shmidtea69e842013-05-13 14:52:28 -07003467 if (tb_comb[NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS])
3468 info->capa->flags |= WPA_DRIVER_FLAGS_RADAR;
3469
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003470 nla_for_each_nested(nl_limit, tb_comb[NL80211_IFACE_COMB_LIMITS],
3471 rem_limit) {
3472 err = nla_parse_nested(tb_limit, MAX_NL80211_IFACE_LIMIT,
3473 nl_limit, iface_limit_policy);
3474 if (err || !tb_limit[NL80211_IFACE_LIMIT_TYPES])
3475 return 0; /* broken combination */
3476
3477 nla_for_each_nested(nl_mode,
3478 tb_limit[NL80211_IFACE_LIMIT_TYPES],
3479 rem_mode) {
3480 int ift = nla_type(nl_mode);
3481 if (ift == NL80211_IFTYPE_P2P_GO ||
3482 ift == NL80211_IFTYPE_P2P_CLIENT)
3483 combination_has_p2p = 1;
3484 if (ift == NL80211_IFTYPE_STATION)
3485 combination_has_mgd = 1;
3486 }
3487 if (combination_has_p2p && combination_has_mgd)
3488 break;
3489 }
3490
3491 if (combination_has_p2p && combination_has_mgd) {
Dmitry Shmidt413dde72014-04-11 10:23:22 -07003492 unsigned int num_channels =
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003493 nla_get_u32(tb_comb[NL80211_IFACE_COMB_NUM_CHANNELS]);
Dmitry Shmidt413dde72014-04-11 10:23:22 -07003494
3495 info->p2p_concurrent = 1;
3496 if (info->num_multichan_concurrent < num_channels)
3497 info->num_multichan_concurrent = num_channels;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003498 }
3499
3500 return 0;
3501}
3502
3503
3504static void wiphy_info_iface_comb(struct wiphy_info_data *info,
3505 struct nlattr *tb)
3506{
3507 struct nlattr *nl_combi;
3508 int rem_combi;
3509
3510 if (tb == NULL)
3511 return;
3512
3513 nla_for_each_nested(nl_combi, tb, rem_combi) {
3514 if (wiphy_info_iface_comb_process(info, nl_combi) > 0)
3515 break;
3516 }
3517}
3518
3519
3520static void wiphy_info_supp_cmds(struct wiphy_info_data *info,
3521 struct nlattr *tb)
3522{
3523 struct nlattr *nl_cmd;
3524 int i;
3525
3526 if (tb == NULL)
3527 return;
3528
3529 nla_for_each_nested(nl_cmd, tb, i) {
3530 switch (nla_get_u32(nl_cmd)) {
3531 case NL80211_CMD_AUTHENTICATE:
3532 info->auth_supported = 1;
3533 break;
3534 case NL80211_CMD_CONNECT:
3535 info->connect_supported = 1;
3536 break;
3537 case NL80211_CMD_START_SCHED_SCAN:
3538 info->capa->sched_scan_supported = 1;
3539 break;
3540 case NL80211_CMD_PROBE_CLIENT:
3541 info->poll_command_supported = 1;
3542 break;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003543 case NL80211_CMD_CHANNEL_SWITCH:
3544 info->channel_switch_supported = 1;
3545 break;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003546 case NL80211_CMD_SET_QOS_MAP:
3547 info->set_qos_map_supported = 1;
3548 break;
3549 }
3550 }
3551}
3552
3553
3554static void wiphy_info_cipher_suites(struct wiphy_info_data *info,
3555 struct nlattr *tb)
3556{
3557 int i, num;
3558 u32 *ciphers;
3559
3560 if (tb == NULL)
3561 return;
3562
3563 num = nla_len(tb) / sizeof(u32);
3564 ciphers = nla_data(tb);
3565 for (i = 0; i < num; i++) {
3566 u32 c = ciphers[i];
3567
3568 wpa_printf(MSG_DEBUG, "nl80211: Supported cipher %02x-%02x-%02x:%d",
3569 c >> 24, (c >> 16) & 0xff,
3570 (c >> 8) & 0xff, c & 0xff);
3571 switch (c) {
3572 case WLAN_CIPHER_SUITE_CCMP_256:
3573 info->capa->enc |= WPA_DRIVER_CAPA_ENC_CCMP_256;
3574 break;
3575 case WLAN_CIPHER_SUITE_GCMP_256:
3576 info->capa->enc |= WPA_DRIVER_CAPA_ENC_GCMP_256;
3577 break;
3578 case WLAN_CIPHER_SUITE_CCMP:
3579 info->capa->enc |= WPA_DRIVER_CAPA_ENC_CCMP;
3580 break;
3581 case WLAN_CIPHER_SUITE_GCMP:
3582 info->capa->enc |= WPA_DRIVER_CAPA_ENC_GCMP;
3583 break;
3584 case WLAN_CIPHER_SUITE_TKIP:
3585 info->capa->enc |= WPA_DRIVER_CAPA_ENC_TKIP;
3586 break;
3587 case WLAN_CIPHER_SUITE_WEP104:
3588 info->capa->enc |= WPA_DRIVER_CAPA_ENC_WEP104;
3589 break;
3590 case WLAN_CIPHER_SUITE_WEP40:
3591 info->capa->enc |= WPA_DRIVER_CAPA_ENC_WEP40;
3592 break;
3593 case WLAN_CIPHER_SUITE_AES_CMAC:
3594 info->capa->enc |= WPA_DRIVER_CAPA_ENC_BIP;
3595 break;
3596 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
3597 info->capa->enc |= WPA_DRIVER_CAPA_ENC_BIP_GMAC_128;
3598 break;
3599 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
3600 info->capa->enc |= WPA_DRIVER_CAPA_ENC_BIP_GMAC_256;
3601 break;
3602 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
3603 info->capa->enc |= WPA_DRIVER_CAPA_ENC_BIP_CMAC_256;
3604 break;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003605 case WLAN_CIPHER_SUITE_NO_GROUP_ADDR:
3606 info->capa->enc |= WPA_DRIVER_CAPA_ENC_GTK_NOT_USED;
3607 break;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003608 }
3609 }
3610}
3611
3612
3613static void wiphy_info_max_roc(struct wpa_driver_capa *capa,
3614 struct nlattr *tb)
3615{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003616 if (tb)
3617 capa->max_remain_on_chan = nla_get_u32(tb);
3618}
3619
3620
3621static void wiphy_info_tdls(struct wpa_driver_capa *capa, struct nlattr *tdls,
3622 struct nlattr *ext_setup)
3623{
3624 if (tdls == NULL)
3625 return;
3626
3627 wpa_printf(MSG_DEBUG, "nl80211: TDLS supported");
3628 capa->flags |= WPA_DRIVER_FLAGS_TDLS_SUPPORT;
3629
3630 if (ext_setup) {
3631 wpa_printf(MSG_DEBUG, "nl80211: TDLS external setup");
3632 capa->flags |= WPA_DRIVER_FLAGS_TDLS_EXTERNAL_SETUP;
3633 }
3634}
3635
3636
3637static void wiphy_info_feature_flags(struct wiphy_info_data *info,
3638 struct nlattr *tb)
3639{
3640 u32 flags;
3641 struct wpa_driver_capa *capa = info->capa;
3642
3643 if (tb == NULL)
3644 return;
3645
3646 flags = nla_get_u32(tb);
3647
3648 if (flags & NL80211_FEATURE_SK_TX_STATUS)
3649 info->data_tx_status = 1;
3650
3651 if (flags & NL80211_FEATURE_INACTIVITY_TIMER)
3652 capa->flags |= WPA_DRIVER_FLAGS_INACTIVITY_TIMER;
3653
3654 if (flags & NL80211_FEATURE_SAE)
3655 capa->flags |= WPA_DRIVER_FLAGS_SAE;
3656
3657 if (flags & NL80211_FEATURE_NEED_OBSS_SCAN)
3658 capa->flags |= WPA_DRIVER_FLAGS_OBSS_SCAN;
3659}
3660
3661
3662static void wiphy_info_probe_resp_offload(struct wpa_driver_capa *capa,
3663 struct nlattr *tb)
3664{
3665 u32 protocols;
3666
3667 if (tb == NULL)
3668 return;
3669
3670 protocols = nla_get_u32(tb);
3671 wpa_printf(MSG_DEBUG, "nl80211: Supports Probe Response offload in AP "
3672 "mode");
3673 capa->flags |= WPA_DRIVER_FLAGS_PROBE_RESP_OFFLOAD;
3674 capa->probe_resp_offloads = probe_resp_offload_support(protocols);
3675}
3676
3677
3678static int wiphy_info_handler(struct nl_msg *msg, void *arg)
3679{
3680 struct nlattr *tb[NL80211_ATTR_MAX + 1];
3681 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3682 struct wiphy_info_data *info = arg;
3683 struct wpa_driver_capa *capa = info->capa;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003684 struct wpa_driver_nl80211_data *drv = info->drv;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003685
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003686 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3687 genlmsg_attrlen(gnlh, 0), NULL);
3688
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003689 if (tb[NL80211_ATTR_WIPHY_NAME])
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003690 os_strlcpy(drv->phyname,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003691 nla_get_string(tb[NL80211_ATTR_WIPHY_NAME]),
3692 sizeof(drv->phyname));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003693 if (tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS])
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003694 capa->max_scan_ssids =
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003695 nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS]);
3696
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003697 if (tb[NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS])
3698 capa->max_sched_scan_ssids =
3699 nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS]);
3700
3701 if (tb[NL80211_ATTR_MAX_MATCH_SETS])
3702 capa->max_match_sets =
3703 nla_get_u8(tb[NL80211_ATTR_MAX_MATCH_SETS]);
3704
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003705 if (tb[NL80211_ATTR_MAC_ACL_MAX])
3706 capa->max_acl_mac_addrs =
3707 nla_get_u8(tb[NL80211_ATTR_MAC_ACL_MAX]);
3708
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003709 wiphy_info_supported_iftypes(info, tb[NL80211_ATTR_SUPPORTED_IFTYPES]);
3710 wiphy_info_iface_comb(info, tb[NL80211_ATTR_INTERFACE_COMBINATIONS]);
3711 wiphy_info_supp_cmds(info, tb[NL80211_ATTR_SUPPORTED_COMMANDS]);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003712 wiphy_info_cipher_suites(info, tb[NL80211_ATTR_CIPHER_SUITES]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003713
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003714 if (tb[NL80211_ATTR_OFFCHANNEL_TX_OK]) {
3715 wpa_printf(MSG_DEBUG, "nl80211: Using driver-based "
3716 "off-channel TX");
3717 capa->flags |= WPA_DRIVER_FLAGS_OFFCHANNEL_TX;
3718 }
3719
3720 if (tb[NL80211_ATTR_ROAM_SUPPORT]) {
3721 wpa_printf(MSG_DEBUG, "nl80211: Using driver-based roaming");
3722 capa->flags |= WPA_DRIVER_FLAGS_BSS_SELECTION;
3723 }
3724
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003725 wiphy_info_max_roc(capa,
3726 tb[NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003727
3728 if (tb[NL80211_ATTR_SUPPORT_AP_UAPSD])
3729 capa->flags |= WPA_DRIVER_FLAGS_AP_UAPSD;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003730
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003731 wiphy_info_tdls(capa, tb[NL80211_ATTR_TDLS_SUPPORT],
3732 tb[NL80211_ATTR_TDLS_EXTERNAL_SETUP]);
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003733
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003734 if (tb[NL80211_ATTR_DEVICE_AP_SME])
3735 info->device_ap_sme = 1;
3736
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003737 wiphy_info_feature_flags(info, tb[NL80211_ATTR_FEATURE_FLAGS]);
3738 wiphy_info_probe_resp_offload(capa,
3739 tb[NL80211_ATTR_PROBE_RESP_OFFLOAD]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003740
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003741 if (tb[NL80211_ATTR_EXT_CAPA] && tb[NL80211_ATTR_EXT_CAPA_MASK] &&
3742 drv->extended_capa == NULL) {
3743 drv->extended_capa =
3744 os_malloc(nla_len(tb[NL80211_ATTR_EXT_CAPA]));
3745 if (drv->extended_capa) {
3746 os_memcpy(drv->extended_capa,
3747 nla_data(tb[NL80211_ATTR_EXT_CAPA]),
3748 nla_len(tb[NL80211_ATTR_EXT_CAPA]));
3749 drv->extended_capa_len =
3750 nla_len(tb[NL80211_ATTR_EXT_CAPA]);
3751 }
3752 drv->extended_capa_mask =
3753 os_malloc(nla_len(tb[NL80211_ATTR_EXT_CAPA]));
3754 if (drv->extended_capa_mask) {
3755 os_memcpy(drv->extended_capa_mask,
3756 nla_data(tb[NL80211_ATTR_EXT_CAPA]),
3757 nla_len(tb[NL80211_ATTR_EXT_CAPA]));
3758 } else {
3759 os_free(drv->extended_capa);
3760 drv->extended_capa = NULL;
3761 drv->extended_capa_len = 0;
3762 }
3763 }
3764
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003765 if (tb[NL80211_ATTR_VENDOR_DATA]) {
3766 struct nlattr *nl;
3767 int rem;
3768
3769 nla_for_each_nested(nl, tb[NL80211_ATTR_VENDOR_DATA], rem) {
3770 struct nl80211_vendor_cmd_info *vinfo;
Dmitry Shmidt18463232014-01-24 12:29:41 -08003771 if (nla_len(nl) != sizeof(*vinfo)) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003772 wpa_printf(MSG_DEBUG, "nl80211: Unexpected vendor data info");
3773 continue;
3774 }
3775 vinfo = nla_data(nl);
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07003776 if (vinfo->subcmd ==
3777 QCA_NL80211_VENDOR_SUBCMD_DFS_CAPABILITY)
3778 drv->dfs_vendor_cmd_avail = 1;
3779
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003780 wpa_printf(MSG_DEBUG, "nl80211: Supported vendor command: vendor_id=0x%x subcmd=%u",
3781 vinfo->vendor_id, vinfo->subcmd);
3782 }
3783 }
3784
3785 if (tb[NL80211_ATTR_VENDOR_EVENTS]) {
3786 struct nlattr *nl;
3787 int rem;
3788
3789 nla_for_each_nested(nl, tb[NL80211_ATTR_VENDOR_EVENTS], rem) {
3790 struct nl80211_vendor_cmd_info *vinfo;
Dmitry Shmidt18463232014-01-24 12:29:41 -08003791 if (nla_len(nl) != sizeof(*vinfo)) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003792 wpa_printf(MSG_DEBUG, "nl80211: Unexpected vendor data info");
3793 continue;
3794 }
3795 vinfo = nla_data(nl);
3796 wpa_printf(MSG_DEBUG, "nl80211: Supported vendor event: vendor_id=0x%x subcmd=%u",
3797 vinfo->vendor_id, vinfo->subcmd);
3798 }
3799 }
3800
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003801 return NL_SKIP;
3802}
3803
3804
3805static int wpa_driver_nl80211_get_info(struct wpa_driver_nl80211_data *drv,
3806 struct wiphy_info_data *info)
3807{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003808 u32 feat;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003809 struct nl_msg *msg;
3810
3811 os_memset(info, 0, sizeof(*info));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003812 info->capa = &drv->capa;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003813 info->drv = drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003814
3815 msg = nlmsg_alloc();
3816 if (!msg)
3817 return -1;
3818
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003819 feat = get_nl80211_protocol_features(drv);
3820 if (feat & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP)
3821 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_WIPHY);
3822 else
3823 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003824
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003825 NLA_PUT_FLAG(msg, NL80211_ATTR_SPLIT_WIPHY_DUMP);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003826 if (nl80211_set_iface_id(msg, drv->first_bss) < 0)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003827 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003828
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003829 if (send_and_recv_msgs(drv, msg, wiphy_info_handler, info))
3830 return -1;
3831
3832 if (info->auth_supported)
3833 drv->capa.flags |= WPA_DRIVER_FLAGS_SME;
3834 else if (!info->connect_supported) {
3835 wpa_printf(MSG_INFO, "nl80211: Driver does not support "
3836 "authentication/association or connect commands");
3837 info->error = 1;
3838 }
3839
3840 if (info->p2p_go_supported && info->p2p_client_supported)
3841 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CAPABLE;
3842 if (info->p2p_concurrent) {
3843 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
3844 "interface (driver advertised support)");
3845 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
3846 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
3847 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003848 if (info->num_multichan_concurrent > 1) {
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003849 wpa_printf(MSG_DEBUG, "nl80211: Enable multi-channel "
3850 "concurrent (driver advertised support)");
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003851 drv->capa.num_multichan_concurrent =
3852 info->num_multichan_concurrent;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003853 }
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07003854
3855 /* default to 5000 since early versions of mac80211 don't set it */
3856 if (!drv->capa.max_remain_on_chan)
3857 drv->capa.max_remain_on_chan = 5000;
3858
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003859 if (info->channel_switch_supported)
3860 drv->capa.flags |= WPA_DRIVER_FLAGS_AP_CSA;
3861
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003862 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003863nla_put_failure:
3864 nlmsg_free(msg);
3865 return -1;
3866}
3867
3868
3869static int wpa_driver_nl80211_capa(struct wpa_driver_nl80211_data *drv)
3870{
3871 struct wiphy_info_data info;
3872 if (wpa_driver_nl80211_get_info(drv, &info))
3873 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003874
3875 if (info.error)
3876 return -1;
3877
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003878 drv->has_capability = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003879 drv->capa.key_mgmt = WPA_DRIVER_CAPA_KEY_MGMT_WPA |
3880 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
3881 WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
3882 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003883 drv->capa.auth = WPA_DRIVER_AUTH_OPEN |
3884 WPA_DRIVER_AUTH_SHARED |
3885 WPA_DRIVER_AUTH_LEAP;
3886
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003887 drv->capa.flags |= WPA_DRIVER_FLAGS_SANE_ERROR_CODES;
3888 drv->capa.flags |= WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003889 drv->capa.flags |= WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003890
Dmitry Shmidta38abf92014-03-06 13:38:44 -08003891 /*
3892 * As all cfg80211 drivers must support cases where the AP interface is
3893 * removed without the knowledge of wpa_supplicant/hostapd, e.g., in
3894 * case that the user space daemon has crashed, they must be able to
3895 * cleanup all stations and key entries in the AP tear down flow. Thus,
3896 * this flag can/should always be set for cfg80211 drivers.
3897 */
3898 drv->capa.flags |= WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT;
3899
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003900 if (!info.device_ap_sme) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07003901 drv->capa.flags |= WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003902
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003903 /*
3904 * No AP SME is currently assumed to also indicate no AP MLME
3905 * in the driver/firmware.
3906 */
3907 drv->capa.flags |= WPA_DRIVER_FLAGS_AP_MLME;
3908 }
3909
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003910 drv->device_ap_sme = info.device_ap_sme;
3911 drv->poll_command_supported = info.poll_command_supported;
3912 drv->data_tx_status = info.data_tx_status;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003913 if (info.set_qos_map_supported)
3914 drv->capa.flags |= WPA_DRIVER_FLAGS_QOS_MAPPING;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003915
3916 /*
Dmitry Shmidtaa532512012-09-24 10:35:31 -07003917 * If poll command and tx status are supported, mac80211 is new enough
3918 * to have everything we need to not need monitor interfaces.
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003919 */
Dmitry Shmidtaa532512012-09-24 10:35:31 -07003920 drv->use_monitor = !info.poll_command_supported || !info.data_tx_status;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003921
3922 if (drv->device_ap_sme && drv->use_monitor) {
3923 /*
3924 * Non-mac80211 drivers may not support monitor interface.
3925 * Make sure we do not get stuck with incorrect capability here
3926 * by explicitly testing this.
3927 */
3928 if (!info.monitor_supported) {
3929 wpa_printf(MSG_DEBUG, "nl80211: Disable use_monitor "
3930 "with device_ap_sme since no monitor mode "
3931 "support detected");
3932 drv->use_monitor = 0;
3933 }
3934 }
3935
3936 /*
3937 * If we aren't going to use monitor interfaces, but the
3938 * driver doesn't support data TX status, we won't get TX
3939 * status for EAPOL frames.
3940 */
3941 if (!drv->use_monitor && !info.data_tx_status)
3942 drv->capa.flags &= ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003943
3944 return 0;
3945}
3946
3947
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003948#ifdef ANDROID
3949static int android_genl_ctrl_resolve(struct nl_handle *handle,
3950 const char *name)
3951{
3952 /*
3953 * Android ICS has very minimal genl_ctrl_resolve() implementation, so
3954 * need to work around that.
3955 */
3956 struct nl_cache *cache = NULL;
3957 struct genl_family *nl80211 = NULL;
3958 int id = -1;
3959
3960 if (genl_ctrl_alloc_cache(handle, &cache) < 0) {
3961 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic "
3962 "netlink cache");
3963 goto fail;
3964 }
3965
3966 nl80211 = genl_ctrl_search_by_name(cache, name);
3967 if (nl80211 == NULL)
3968 goto fail;
3969
3970 id = genl_family_get_id(nl80211);
3971
3972fail:
3973 if (nl80211)
3974 genl_family_put(nl80211);
3975 if (cache)
3976 nl_cache_free(cache);
3977
3978 return id;
3979}
3980#define genl_ctrl_resolve android_genl_ctrl_resolve
3981#endif /* ANDROID */
3982
3983
3984static int wpa_driver_nl80211_init_nl_global(struct nl80211_global *global)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003985{
3986 int ret;
3987
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003988 global->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
3989 if (global->nl_cb == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003990 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
3991 "callbacks");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003992 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003993 }
3994
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003995 global->nl = nl_create_handle(global->nl_cb, "nl");
3996 if (global->nl == NULL)
3997 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003998
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003999 global->nl80211_id = genl_ctrl_resolve(global->nl, "nl80211");
4000 if (global->nl80211_id < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004001 wpa_printf(MSG_ERROR, "nl80211: 'nl80211' generic netlink not "
4002 "found");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004003 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004004 }
4005
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004006 global->nl_event = nl_create_handle(global->nl_cb, "event");
4007 if (global->nl_event == NULL)
4008 goto err;
4009
4010 ret = nl_get_multicast_id(global, "nl80211", "scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004011 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004012 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004013 if (ret < 0) {
4014 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
4015 "membership for scan events: %d (%s)",
4016 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004017 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004018 }
4019
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004020 ret = nl_get_multicast_id(global, "nl80211", "mlme");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004021 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004022 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004023 if (ret < 0) {
4024 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
4025 "membership for mlme events: %d (%s)",
4026 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004027 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004028 }
4029
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004030 ret = nl_get_multicast_id(global, "nl80211", "regulatory");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004031 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004032 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004033 if (ret < 0) {
4034 wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
4035 "membership for regulatory events: %d (%s)",
4036 ret, strerror(-ret));
4037 /* Continue without regulatory events */
4038 }
4039
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004040 ret = nl_get_multicast_id(global, "nl80211", "vendor");
4041 if (ret >= 0)
4042 ret = nl_socket_add_membership(global->nl_event, ret);
4043 if (ret < 0) {
4044 wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
4045 "membership for vendor events: %d (%s)",
4046 ret, strerror(-ret));
4047 /* Continue without vendor events */
4048 }
4049
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004050 nl_cb_set(global->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
4051 no_seq_check, NULL);
4052 nl_cb_set(global->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
4053 process_global_event, global);
4054
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004055 nl80211_register_eloop_read(&global->nl_event,
4056 wpa_driver_nl80211_event_receive,
4057 global->nl_cb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004058
4059 return 0;
4060
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004061err:
4062 nl_destroy_handles(&global->nl_event);
4063 nl_destroy_handles(&global->nl);
4064 nl_cb_put(global->nl_cb);
4065 global->nl_cb = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004066 return -1;
4067}
4068
4069
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004070static int wpa_driver_nl80211_init_nl(struct wpa_driver_nl80211_data *drv)
4071{
4072 drv->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
4073 if (!drv->nl_cb) {
4074 wpa_printf(MSG_ERROR, "nl80211: Failed to alloc cb struct");
4075 return -1;
4076 }
4077
4078 nl_cb_set(drv->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
4079 no_seq_check, NULL);
4080 nl_cb_set(drv->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
4081 process_drv_event, drv);
4082
4083 return 0;
4084}
4085
4086
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004087static void wpa_driver_nl80211_rfkill_blocked(void *ctx)
4088{
4089 wpa_printf(MSG_DEBUG, "nl80211: RFKILL blocked");
4090 /*
4091 * This may be for any interface; use ifdown event to disable
4092 * interface.
4093 */
4094}
4095
4096
4097static void wpa_driver_nl80211_rfkill_unblocked(void *ctx)
4098{
4099 struct wpa_driver_nl80211_data *drv = ctx;
4100 wpa_printf(MSG_DEBUG, "nl80211: RFKILL unblocked");
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004101 if (i802_set_iface_flags(drv->first_bss, 1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004102 wpa_printf(MSG_DEBUG, "nl80211: Could not set interface UP "
4103 "after rfkill unblock");
4104 return;
4105 }
4106 /* rtnetlink ifup handler will report interface as enabled */
4107}
4108
4109
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004110static void wpa_driver_nl80211_handle_eapol_tx_status(int sock,
4111 void *eloop_ctx,
4112 void *handle)
4113{
4114 struct wpa_driver_nl80211_data *drv = eloop_ctx;
4115 u8 data[2048];
4116 struct msghdr msg;
4117 struct iovec entry;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004118 u8 control[512];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004119 struct cmsghdr *cmsg;
4120 int res, found_ee = 0, found_wifi = 0, acked = 0;
4121 union wpa_event_data event;
4122
4123 memset(&msg, 0, sizeof(msg));
4124 msg.msg_iov = &entry;
4125 msg.msg_iovlen = 1;
4126 entry.iov_base = data;
4127 entry.iov_len = sizeof(data);
4128 msg.msg_control = &control;
4129 msg.msg_controllen = sizeof(control);
4130
4131 res = recvmsg(sock, &msg, MSG_ERRQUEUE);
4132 /* if error or not fitting 802.3 header, return */
4133 if (res < 14)
4134 return;
4135
4136 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg))
4137 {
4138 if (cmsg->cmsg_level == SOL_SOCKET &&
4139 cmsg->cmsg_type == SCM_WIFI_STATUS) {
4140 int *ack;
4141
4142 found_wifi = 1;
4143 ack = (void *)CMSG_DATA(cmsg);
4144 acked = *ack;
4145 }
4146
4147 if (cmsg->cmsg_level == SOL_PACKET &&
4148 cmsg->cmsg_type == PACKET_TX_TIMESTAMP) {
4149 struct sock_extended_err *err =
4150 (struct sock_extended_err *)CMSG_DATA(cmsg);
4151
4152 if (err->ee_origin == SO_EE_ORIGIN_TXSTATUS)
4153 found_ee = 1;
4154 }
4155 }
4156
4157 if (!found_ee || !found_wifi)
4158 return;
4159
4160 memset(&event, 0, sizeof(event));
4161 event.eapol_tx_status.dst = data;
4162 event.eapol_tx_status.data = data + 14;
4163 event.eapol_tx_status.data_len = res - 14;
4164 event.eapol_tx_status.ack = acked;
4165 wpa_supplicant_event(drv->ctx, EVENT_EAPOL_TX_STATUS, &event);
4166}
4167
4168
4169static int nl80211_init_bss(struct i802_bss *bss)
4170{
4171 bss->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
4172 if (!bss->nl_cb)
4173 return -1;
4174
4175 nl_cb_set(bss->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
4176 no_seq_check, NULL);
4177 nl_cb_set(bss->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
4178 process_bss_event, bss);
4179
4180 return 0;
4181}
4182
4183
4184static void nl80211_destroy_bss(struct i802_bss *bss)
4185{
4186 nl_cb_put(bss->nl_cb);
4187 bss->nl_cb = NULL;
4188}
4189
4190
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004191static void * wpa_driver_nl80211_drv_init(void *ctx, const char *ifname,
4192 void *global_priv, int hostapd,
4193 const u8 *set_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004194{
4195 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004196 struct rfkill_config *rcfg;
4197 struct i802_bss *bss;
4198
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004199 if (global_priv == NULL)
4200 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004201 drv = os_zalloc(sizeof(*drv));
4202 if (drv == NULL)
4203 return NULL;
4204 drv->global = global_priv;
4205 drv->ctx = ctx;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004206 drv->hostapd = !!hostapd;
4207 drv->eapol_sock = -1;
4208 drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int);
4209 drv->if_indices = drv->default_if_indices;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004210
4211 drv->first_bss = os_zalloc(sizeof(*drv->first_bss));
4212 if (!drv->first_bss) {
4213 os_free(drv);
4214 return NULL;
4215 }
4216 bss = drv->first_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004217 bss->drv = drv;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004218 bss->ctx = ctx;
4219
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004220 os_strlcpy(bss->ifname, ifname, sizeof(bss->ifname));
4221 drv->monitor_ifidx = -1;
4222 drv->monitor_sock = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004223 drv->eapol_tx_sock = -1;
4224 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004225
4226 if (wpa_driver_nl80211_init_nl(drv)) {
4227 os_free(drv);
4228 return NULL;
4229 }
4230
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004231 if (nl80211_init_bss(bss))
4232 goto failed;
4233
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004234 rcfg = os_zalloc(sizeof(*rcfg));
4235 if (rcfg == NULL)
4236 goto failed;
4237 rcfg->ctx = drv;
4238 os_strlcpy(rcfg->ifname, ifname, sizeof(rcfg->ifname));
4239 rcfg->blocked_cb = wpa_driver_nl80211_rfkill_blocked;
4240 rcfg->unblocked_cb = wpa_driver_nl80211_rfkill_unblocked;
4241 drv->rfkill = rfkill_init(rcfg);
4242 if (drv->rfkill == NULL) {
4243 wpa_printf(MSG_DEBUG, "nl80211: RFKILL status not available");
4244 os_free(rcfg);
4245 }
4246
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004247 if (linux_iface_up(drv->global->ioctl_sock, ifname) > 0)
4248 drv->start_iface_up = 1;
4249
4250 if (wpa_driver_nl80211_finish_drv_init(drv, set_addr, 1))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004251 goto failed;
4252
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004253 drv->eapol_tx_sock = socket(PF_PACKET, SOCK_DGRAM, 0);
4254 if (drv->eapol_tx_sock < 0)
4255 goto failed;
4256
4257 if (drv->data_tx_status) {
4258 int enabled = 1;
4259
4260 if (setsockopt(drv->eapol_tx_sock, SOL_SOCKET, SO_WIFI_STATUS,
4261 &enabled, sizeof(enabled)) < 0) {
4262 wpa_printf(MSG_DEBUG,
4263 "nl80211: wifi status sockopt failed\n");
4264 drv->data_tx_status = 0;
4265 if (!drv->use_monitor)
4266 drv->capa.flags &=
4267 ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
4268 } else {
4269 eloop_register_read_sock(drv->eapol_tx_sock,
4270 wpa_driver_nl80211_handle_eapol_tx_status,
4271 drv, NULL);
4272 }
4273 }
4274
4275 if (drv->global) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004276 dl_list_add(&drv->global->interfaces, &drv->list);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004277 drv->in_interface_list = 1;
4278 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004279
4280 return bss;
4281
4282failed:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004283 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004284 return NULL;
4285}
4286
4287
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004288/**
4289 * wpa_driver_nl80211_init - Initialize nl80211 driver interface
4290 * @ctx: context to be used when calling wpa_supplicant functions,
4291 * e.g., wpa_supplicant_event()
4292 * @ifname: interface name, e.g., wlan0
4293 * @global_priv: private driver global data from global_init()
4294 * Returns: Pointer to private data, %NULL on failure
4295 */
4296static void * wpa_driver_nl80211_init(void *ctx, const char *ifname,
4297 void *global_priv)
4298{
4299 return wpa_driver_nl80211_drv_init(ctx, ifname, global_priv, 0, NULL);
4300}
4301
4302
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004303static int nl80211_register_frame(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004304 struct nl_handle *nl_handle,
4305 u16 type, const u8 *match, size_t match_len)
4306{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004307 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004308 struct nl_msg *msg;
4309 int ret = -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004310 char buf[30];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004311
4312 msg = nlmsg_alloc();
4313 if (!msg)
4314 return -1;
4315
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004316 buf[0] = '\0';
4317 wpa_snprintf_hex(buf, sizeof(buf), match, match_len);
4318 wpa_printf(MSG_DEBUG, "nl80211: Register frame type=0x%x nl_handle=%p match=%s",
4319 type, nl_handle, buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004320
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004321 nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_ACTION);
4322
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004323 if (nl80211_set_iface_id(msg, bss) < 0)
4324 goto nla_put_failure;
4325
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004326 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE, type);
4327 NLA_PUT(msg, NL80211_ATTR_FRAME_MATCH, match_len, match);
4328
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004329 ret = send_and_recv(drv->global, nl_handle, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004330 msg = NULL;
4331 if (ret) {
4332 wpa_printf(MSG_DEBUG, "nl80211: Register frame command "
4333 "failed (type=%u): ret=%d (%s)",
4334 type, ret, strerror(-ret));
4335 wpa_hexdump(MSG_DEBUG, "nl80211: Register frame match",
4336 match, match_len);
4337 goto nla_put_failure;
4338 }
4339 ret = 0;
4340nla_put_failure:
4341 nlmsg_free(msg);
4342 return ret;
4343}
4344
4345
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004346static int nl80211_alloc_mgmt_handle(struct i802_bss *bss)
4347{
4348 struct wpa_driver_nl80211_data *drv = bss->drv;
4349
4350 if (bss->nl_mgmt) {
4351 wpa_printf(MSG_DEBUG, "nl80211: Mgmt reporting "
4352 "already on! (nl_mgmt=%p)", bss->nl_mgmt);
4353 return -1;
4354 }
4355
4356 bss->nl_mgmt = nl_create_handle(drv->nl_cb, "mgmt");
4357 if (bss->nl_mgmt == NULL)
4358 return -1;
4359
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004360 return 0;
4361}
4362
4363
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004364static void nl80211_mgmt_handle_register_eloop(struct i802_bss *bss)
4365{
4366 nl80211_register_eloop_read(&bss->nl_mgmt,
4367 wpa_driver_nl80211_event_receive,
4368 bss->nl_cb);
4369}
4370
4371
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004372static int nl80211_register_action_frame(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004373 const u8 *match, size_t match_len)
4374{
4375 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_ACTION << 4);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004376 return nl80211_register_frame(bss, bss->nl_mgmt,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004377 type, match, match_len);
4378}
4379
4380
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004381static int nl80211_mgmt_subscribe_non_ap(struct i802_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004382{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004383 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004384 int ret = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004385
4386 if (nl80211_alloc_mgmt_handle(bss))
4387 return -1;
4388 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with non-AP "
4389 "handle %p", bss->nl_mgmt);
4390
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004391 if (drv->nlmode == NL80211_IFTYPE_ADHOC) {
4392 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_AUTH << 4);
4393
4394 /* register for any AUTH message */
4395 nl80211_register_frame(bss, bss->nl_mgmt, type, NULL, 0);
4396 }
4397
Dmitry Shmidt051af732013-10-22 13:52:46 -07004398#ifdef CONFIG_INTERWORKING
4399 /* QoS Map Configure */
4400 if (nl80211_register_action_frame(bss, (u8 *) "\x01\x04", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004401 ret = -1;
Dmitry Shmidt051af732013-10-22 13:52:46 -07004402#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004403#if defined(CONFIG_P2P) || defined(CONFIG_INTERWORKING)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004404 /* GAS Initial Request */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004405 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0a", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004406 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004407 /* GAS Initial Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004408 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0b", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004409 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004410 /* GAS Comeback Request */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004411 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0c", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004412 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004413 /* GAS Comeback Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004414 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0d", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004415 ret = -1;
Dmitry Shmidt18463232014-01-24 12:29:41 -08004416 /* Protected GAS Initial Request */
4417 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0a", 2) < 0)
4418 ret = -1;
4419 /* Protected GAS Initial Response */
4420 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0b", 2) < 0)
4421 ret = -1;
4422 /* Protected GAS Comeback Request */
4423 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0c", 2) < 0)
4424 ret = -1;
4425 /* Protected GAS Comeback Response */
4426 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0d", 2) < 0)
4427 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004428#endif /* CONFIG_P2P || CONFIG_INTERWORKING */
4429#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004430 /* P2P Public Action */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004431 if (nl80211_register_action_frame(bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004432 (u8 *) "\x04\x09\x50\x6f\x9a\x09",
4433 6) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004434 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004435 /* P2P Action */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004436 if (nl80211_register_action_frame(bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004437 (u8 *) "\x7f\x50\x6f\x9a\x09",
4438 5) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004439 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004440#endif /* CONFIG_P2P */
4441#ifdef CONFIG_IEEE80211W
4442 /* SA Query Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004443 if (nl80211_register_action_frame(bss, (u8 *) "\x08\x01", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004444 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004445#endif /* CONFIG_IEEE80211W */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004446#ifdef CONFIG_TDLS
4447 if ((drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT)) {
4448 /* TDLS Discovery Response */
4449 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0e", 2) <
4450 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004451 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004452 }
4453#endif /* CONFIG_TDLS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004454
4455 /* FT Action frames */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004456 if (nl80211_register_action_frame(bss, (u8 *) "\x06", 1) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004457 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004458 else
4459 drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_FT |
4460 WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK;
4461
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004462 /* WNM - BSS Transition Management Request */
4463 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x07", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004464 ret = -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004465 /* WNM-Sleep Mode Response */
4466 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x11", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004467 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004468
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08004469#ifdef CONFIG_HS20
4470 /* WNM-Notification */
4471 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x1a", 2) < 0)
4472 return -1;
4473#endif /* CONFIG_HS20 */
4474
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004475 nl80211_mgmt_handle_register_eloop(bss);
4476
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004477 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004478}
4479
4480
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004481static int nl80211_register_spurious_class3(struct i802_bss *bss)
4482{
4483 struct wpa_driver_nl80211_data *drv = bss->drv;
4484 struct nl_msg *msg;
4485 int ret = -1;
4486
4487 msg = nlmsg_alloc();
4488 if (!msg)
4489 return -1;
4490
4491 nl80211_cmd(drv, msg, 0, NL80211_CMD_UNEXPECTED_FRAME);
4492
4493 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
4494
4495 ret = send_and_recv(drv->global, bss->nl_mgmt, msg, NULL, NULL);
4496 msg = NULL;
4497 if (ret) {
4498 wpa_printf(MSG_DEBUG, "nl80211: Register spurious class3 "
4499 "failed: ret=%d (%s)",
4500 ret, strerror(-ret));
4501 goto nla_put_failure;
4502 }
4503 ret = 0;
4504nla_put_failure:
4505 nlmsg_free(msg);
4506 return ret;
4507}
4508
4509
4510static int nl80211_mgmt_subscribe_ap(struct i802_bss *bss)
4511{
4512 static const int stypes[] = {
4513 WLAN_FC_STYPE_AUTH,
4514 WLAN_FC_STYPE_ASSOC_REQ,
4515 WLAN_FC_STYPE_REASSOC_REQ,
4516 WLAN_FC_STYPE_DISASSOC,
4517 WLAN_FC_STYPE_DEAUTH,
4518 WLAN_FC_STYPE_ACTION,
4519 WLAN_FC_STYPE_PROBE_REQ,
4520/* Beacon doesn't work as mac80211 doesn't currently allow
4521 * it, but it wouldn't really be the right thing anyway as
4522 * it isn't per interface ... maybe just dump the scan
4523 * results periodically for OLBC?
4524 */
4525// WLAN_FC_STYPE_BEACON,
4526 };
4527 unsigned int i;
4528
4529 if (nl80211_alloc_mgmt_handle(bss))
4530 return -1;
4531 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
4532 "handle %p", bss->nl_mgmt);
4533
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004534 for (i = 0; i < ARRAY_SIZE(stypes); i++) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004535 if (nl80211_register_frame(bss, bss->nl_mgmt,
4536 (WLAN_FC_TYPE_MGMT << 2) |
4537 (stypes[i] << 4),
4538 NULL, 0) < 0) {
4539 goto out_err;
4540 }
4541 }
4542
4543 if (nl80211_register_spurious_class3(bss))
4544 goto out_err;
4545
4546 if (nl80211_get_wiphy_data_ap(bss) == NULL)
4547 goto out_err;
4548
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004549 nl80211_mgmt_handle_register_eloop(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004550 return 0;
4551
4552out_err:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004553 nl_destroy_handles(&bss->nl_mgmt);
4554 return -1;
4555}
4556
4557
4558static int nl80211_mgmt_subscribe_ap_dev_sme(struct i802_bss *bss)
4559{
4560 if (nl80211_alloc_mgmt_handle(bss))
4561 return -1;
4562 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
4563 "handle %p (device SME)", bss->nl_mgmt);
4564
4565 if (nl80211_register_frame(bss, bss->nl_mgmt,
4566 (WLAN_FC_TYPE_MGMT << 2) |
4567 (WLAN_FC_STYPE_ACTION << 4),
4568 NULL, 0) < 0)
4569 goto out_err;
4570
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004571 nl80211_mgmt_handle_register_eloop(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004572 return 0;
4573
4574out_err:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004575 nl_destroy_handles(&bss->nl_mgmt);
4576 return -1;
4577}
4578
4579
4580static void nl80211_mgmt_unsubscribe(struct i802_bss *bss, const char *reason)
4581{
4582 if (bss->nl_mgmt == NULL)
4583 return;
4584 wpa_printf(MSG_DEBUG, "nl80211: Unsubscribe mgmt frames handle %p "
4585 "(%s)", bss->nl_mgmt, reason);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004586 nl80211_destroy_eloop_handle(&bss->nl_mgmt);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004587
4588 nl80211_put_wiphy_data_ap(bss);
4589}
4590
4591
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004592static void wpa_driver_nl80211_send_rfkill(void *eloop_ctx, void *timeout_ctx)
4593{
4594 wpa_supplicant_event(timeout_ctx, EVENT_INTERFACE_DISABLED, NULL);
4595}
4596
4597
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004598static void nl80211_del_p2pdev(struct i802_bss *bss)
4599{
4600 struct wpa_driver_nl80211_data *drv = bss->drv;
4601 struct nl_msg *msg;
4602 int ret;
4603
4604 msg = nlmsg_alloc();
4605 if (!msg)
4606 return;
4607
4608 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_INTERFACE);
4609 NLA_PUT_U64(msg, NL80211_ATTR_WDEV, bss->wdev_id);
4610
4611 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4612 msg = NULL;
4613
4614 wpa_printf(MSG_DEBUG, "nl80211: Delete P2P Device %s (0x%llx): %s",
4615 bss->ifname, (long long unsigned int) bss->wdev_id,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004616 strerror(-ret));
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004617
4618nla_put_failure:
4619 nlmsg_free(msg);
4620}
4621
4622
4623static int nl80211_set_p2pdev(struct i802_bss *bss, int start)
4624{
4625 struct wpa_driver_nl80211_data *drv = bss->drv;
4626 struct nl_msg *msg;
4627 int ret = -1;
4628
4629 msg = nlmsg_alloc();
4630 if (!msg)
4631 return -1;
4632
4633 if (start)
4634 nl80211_cmd(drv, msg, 0, NL80211_CMD_START_P2P_DEVICE);
4635 else
4636 nl80211_cmd(drv, msg, 0, NL80211_CMD_STOP_P2P_DEVICE);
4637
4638 NLA_PUT_U64(msg, NL80211_ATTR_WDEV, bss->wdev_id);
4639
4640 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4641 msg = NULL;
4642
4643 wpa_printf(MSG_DEBUG, "nl80211: %s P2P Device %s (0x%llx): %s",
4644 start ? "Start" : "Stop",
4645 bss->ifname, (long long unsigned int) bss->wdev_id,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004646 strerror(-ret));
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004647
4648nla_put_failure:
4649 nlmsg_free(msg);
4650 return ret;
4651}
4652
4653
4654static int i802_set_iface_flags(struct i802_bss *bss, int up)
4655{
4656 enum nl80211_iftype nlmode;
4657
4658 nlmode = nl80211_get_ifmode(bss);
4659 if (nlmode != NL80211_IFTYPE_P2P_DEVICE) {
4660 return linux_set_iface_flags(bss->drv->global->ioctl_sock,
4661 bss->ifname, up);
4662 }
4663
4664 /* P2P Device has start/stop which is equivalent */
4665 return nl80211_set_p2pdev(bss, up);
4666}
4667
4668
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004669static int
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004670wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv,
4671 const u8 *set_addr, int first)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004672{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004673 struct i802_bss *bss = drv->first_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004674 int send_rfkill_event = 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004675 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004676
4677 drv->ifindex = if_nametoindex(bss->ifname);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004678 bss->ifindex = drv->ifindex;
4679 bss->wdev_id = drv->global->if_add_wdevid;
4680 bss->wdev_id_set = drv->global->if_add_wdevid_set;
4681
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004682 bss->if_dynamic = drv->ifindex == drv->global->if_add_ifindex;
4683 bss->if_dynamic = bss->if_dynamic || drv->global->if_add_wdevid_set;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004684 drv->global->if_add_wdevid_set = 0;
4685
4686 if (wpa_driver_nl80211_capa(drv))
4687 return -1;
4688
4689 wpa_printf(MSG_DEBUG, "nl80211: interface %s in phy %s",
4690 bss->ifname, drv->phyname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004691
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004692 if (set_addr &&
4693 (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0) ||
4694 linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
4695 set_addr)))
4696 return -1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004697
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004698 if (first && nl80211_get_ifmode(bss) == NL80211_IFTYPE_AP)
4699 drv->start_mode_ap = 1;
4700
4701 if (drv->hostapd)
4702 nlmode = NL80211_IFTYPE_AP;
4703 else if (bss->if_dynamic)
4704 nlmode = nl80211_get_ifmode(bss);
4705 else
4706 nlmode = NL80211_IFTYPE_STATION;
4707
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004708 if (wpa_driver_nl80211_set_mode(bss, nlmode) < 0) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004709 wpa_printf(MSG_ERROR, "nl80211: Could not configure driver mode");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004710 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004711 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004712
Dmitry Shmidt98660862014-03-11 17:26:21 -07004713 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004714 nl80211_get_macaddr(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004715
Dmitry Shmidt98660862014-03-11 17:26:21 -07004716 if (!rfkill_is_blocked(drv->rfkill)) {
4717 int ret = i802_set_iface_flags(bss, 1);
4718 if (ret) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004719 wpa_printf(MSG_ERROR, "nl80211: Could not set "
4720 "interface '%s' UP", bss->ifname);
Dmitry Shmidt98660862014-03-11 17:26:21 -07004721 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004722 }
Dmitry Shmidt98660862014-03-11 17:26:21 -07004723 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
4724 return ret;
4725 } else {
4726 wpa_printf(MSG_DEBUG, "nl80211: Could not yet enable "
4727 "interface '%s' due to rfkill", bss->ifname);
4728 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
4729 return 0;
4730 drv->if_disabled = 1;
4731 send_rfkill_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004732 }
4733
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004734 if (!drv->hostapd)
4735 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
4736 1, IF_OPER_DORMANT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004737
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004738 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
4739 bss->addr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004740 return -1;
4741
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004742 if (send_rfkill_event) {
4743 eloop_register_timeout(0, 0, wpa_driver_nl80211_send_rfkill,
4744 drv, drv->ctx);
4745 }
4746
4747 return 0;
4748}
4749
4750
4751static int wpa_driver_nl80211_del_beacon(struct wpa_driver_nl80211_data *drv)
4752{
4753 struct nl_msg *msg;
4754
4755 msg = nlmsg_alloc();
4756 if (!msg)
4757 return -ENOMEM;
4758
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004759 wpa_printf(MSG_DEBUG, "nl80211: Remove beacon (ifindex=%d)",
4760 drv->ifindex);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004761 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_BEACON);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004762 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
4763
4764 return send_and_recv_msgs(drv, msg, NULL, NULL);
4765 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004766 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004767 return -ENOBUFS;
4768}
4769
4770
4771/**
4772 * wpa_driver_nl80211_deinit - Deinitialize nl80211 driver interface
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004773 * @bss: Pointer to private nl80211 data from wpa_driver_nl80211_init()
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004774 *
4775 * Shut down driver interface and processing of driver events. Free
4776 * private data buffer if one was allocated in wpa_driver_nl80211_init().
4777 */
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004778static void wpa_driver_nl80211_deinit(struct i802_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004779{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004780 struct wpa_driver_nl80211_data *drv = bss->drv;
4781
Dmitry Shmidt04949592012-07-19 12:16:46 -07004782 bss->in_deinit = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004783 if (drv->data_tx_status)
4784 eloop_unregister_read_sock(drv->eapol_tx_sock);
4785 if (drv->eapol_tx_sock >= 0)
4786 close(drv->eapol_tx_sock);
4787
4788 if (bss->nl_preq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004789 wpa_driver_nl80211_probe_req_report(bss, 0);
4790 if (bss->added_if_into_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004791 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
4792 bss->ifname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004793 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
4794 "interface %s from bridge %s: %s",
4795 bss->ifname, bss->brname, strerror(errno));
4796 }
4797 if (bss->added_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004798 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004799 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
4800 "bridge %s: %s",
4801 bss->brname, strerror(errno));
4802 }
4803
4804 nl80211_remove_monitor_interface(drv);
4805
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004806 if (is_ap_interface(drv->nlmode))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004807 wpa_driver_nl80211_del_beacon(drv);
4808
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004809 if (drv->eapol_sock >= 0) {
4810 eloop_unregister_read_sock(drv->eapol_sock);
4811 close(drv->eapol_sock);
4812 }
4813
4814 if (drv->if_indices != drv->default_if_indices)
4815 os_free(drv->if_indices);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004816
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004817 if (drv->disabled_11b_rates)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004818 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
4819
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004820 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, 0,
4821 IF_OPER_UP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004822 rfkill_deinit(drv->rfkill);
4823
4824 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
4825
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004826 if (!drv->start_iface_up)
4827 (void) i802_set_iface_flags(bss, 0);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004828 if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004829 if (!drv->hostapd || !drv->start_mode_ap)
4830 wpa_driver_nl80211_set_mode(bss,
4831 NL80211_IFTYPE_STATION);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004832 nl80211_mgmt_unsubscribe(bss, "deinit");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004833 } else {
4834 nl80211_mgmt_unsubscribe(bss, "deinit");
4835 nl80211_del_p2pdev(bss);
4836 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004837 nl_cb_put(drv->nl_cb);
4838
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004839 nl80211_destroy_bss(drv->first_bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004840
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004841 os_free(drv->filter_ssids);
4842
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004843 os_free(drv->auth_ie);
4844
4845 if (drv->in_interface_list)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004846 dl_list_del(&drv->list);
4847
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004848 os_free(drv->extended_capa);
4849 os_free(drv->extended_capa_mask);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004850 os_free(drv->first_bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004851 os_free(drv);
4852}
4853
4854
4855/**
4856 * wpa_driver_nl80211_scan_timeout - Scan timeout to report scan completion
4857 * @eloop_ctx: Driver private data
4858 * @timeout_ctx: ctx argument given to wpa_driver_nl80211_init()
4859 *
4860 * This function can be used as registered timeout when starting a scan to
4861 * generate a scan completed event if the driver does not report this.
4862 */
4863static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx, void *timeout_ctx)
4864{
4865 struct wpa_driver_nl80211_data *drv = eloop_ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004866 if (drv->ap_scan_as_station != NL80211_IFTYPE_UNSPECIFIED) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004867 wpa_driver_nl80211_set_mode(drv->first_bss,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004868 drv->ap_scan_as_station);
4869 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004870 }
4871 wpa_printf(MSG_DEBUG, "Scan timeout - try to get results");
4872 wpa_supplicant_event(timeout_ctx, EVENT_SCAN_RESULTS, NULL);
4873}
4874
4875
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004876static struct nl_msg *
4877nl80211_scan_common(struct wpa_driver_nl80211_data *drv, u8 cmd,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004878 struct wpa_driver_scan_params *params, u64 *wdev_id)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004879{
4880 struct nl_msg *msg;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004881 size_t i;
4882
4883 msg = nlmsg_alloc();
4884 if (!msg)
4885 return NULL;
4886
4887 nl80211_cmd(drv, msg, 0, cmd);
4888
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004889 if (!wdev_id)
4890 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
4891 else
4892 NLA_PUT_U64(msg, NL80211_ATTR_WDEV, *wdev_id);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004893
4894 if (params->num_ssids) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004895 struct nlattr *ssids;
4896
4897 ssids = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004898 if (ssids == NULL)
4899 goto fail;
4900 for (i = 0; i < params->num_ssids; i++) {
4901 wpa_hexdump_ascii(MSG_MSGDUMP, "nl80211: Scan SSID",
4902 params->ssids[i].ssid,
4903 params->ssids[i].ssid_len);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004904 if (nla_put(msg, i + 1, params->ssids[i].ssid_len,
4905 params->ssids[i].ssid) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004906 goto fail;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004907 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004908 nla_nest_end(msg, ssids);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004909 }
4910
4911 if (params->extra_ies) {
4912 wpa_hexdump(MSG_MSGDUMP, "nl80211: Scan extra IEs",
4913 params->extra_ies, params->extra_ies_len);
4914 if (nla_put(msg, NL80211_ATTR_IE, params->extra_ies_len,
4915 params->extra_ies) < 0)
4916 goto fail;
4917 }
4918
4919 if (params->freqs) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004920 struct nlattr *freqs;
4921 freqs = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004922 if (freqs == NULL)
4923 goto fail;
4924 for (i = 0; params->freqs[i]; i++) {
4925 wpa_printf(MSG_MSGDUMP, "nl80211: Scan frequency %u "
4926 "MHz", params->freqs[i]);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004927 if (nla_put_u32(msg, i + 1, params->freqs[i]) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004928 goto fail;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004929 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004930 nla_nest_end(msg, freqs);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004931 }
4932
4933 os_free(drv->filter_ssids);
4934 drv->filter_ssids = params->filter_ssids;
4935 params->filter_ssids = NULL;
4936 drv->num_filter_ssids = params->num_filter_ssids;
4937
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004938 if (params->only_new_results) {
4939 wpa_printf(MSG_DEBUG, "nl80211: Add NL80211_SCAN_FLAG_FLUSH");
4940 NLA_PUT_U32(msg, NL80211_ATTR_SCAN_FLAGS,
4941 NL80211_SCAN_FLAG_FLUSH);
4942 }
4943
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004944 return msg;
4945
4946fail:
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004947nla_put_failure:
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004948 nlmsg_free(msg);
4949 return NULL;
4950}
4951
4952
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004953/**
4954 * wpa_driver_nl80211_scan - Request the driver to initiate scan
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004955 * @bss: Pointer to private driver data from wpa_driver_nl80211_init()
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004956 * @params: Scan parameters
4957 * Returns: 0 on success, -1 on failure
4958 */
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004959static int wpa_driver_nl80211_scan(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004960 struct wpa_driver_scan_params *params)
4961{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004962 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004963 int ret = -1, timeout;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004964 struct nl_msg *msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004965
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004966 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: scan request");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004967 drv->scan_for_auth = 0;
4968
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004969 msg = nl80211_scan_common(drv, NL80211_CMD_TRIGGER_SCAN, params,
4970 bss->wdev_id_set ? &bss->wdev_id : NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004971 if (!msg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004972 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004973
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004974 if (params->p2p_probe) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004975 struct nlattr *rates;
4976
Dmitry Shmidt04949592012-07-19 12:16:46 -07004977 wpa_printf(MSG_DEBUG, "nl80211: P2P probe - mask SuppRates");
4978
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004979 rates = nla_nest_start(msg, NL80211_ATTR_SCAN_SUPP_RATES);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004980 if (rates == NULL)
4981 goto nla_put_failure;
4982
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004983 /*
4984 * Remove 2.4 GHz rates 1, 2, 5.5, 11 Mbps from supported rates
4985 * by masking out everything else apart from the OFDM rates 6,
4986 * 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS rates. All 5 GHz
4987 * rates are left enabled.
4988 */
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004989 NLA_PUT(msg, NL80211_BAND_2GHZ, 8,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004990 "\x0c\x12\x18\x24\x30\x48\x60\x6c");
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004991 nla_nest_end(msg, rates);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004992
4993 NLA_PUT_FLAG(msg, NL80211_ATTR_TX_NO_CCK_RATE);
4994 }
4995
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004996 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4997 msg = NULL;
4998 if (ret) {
4999 wpa_printf(MSG_DEBUG, "nl80211: Scan trigger failed: ret=%d "
5000 "(%s)", ret, strerror(-ret));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005001 if (drv->hostapd && is_ap_interface(drv->nlmode)) {
Dmitry Shmidted003d22014-02-06 10:09:12 -08005002 enum nl80211_iftype old_mode = drv->nlmode;
5003
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005004 /*
5005 * mac80211 does not allow scan requests in AP mode, so
5006 * try to do this in station mode.
5007 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005008 if (wpa_driver_nl80211_set_mode(
5009 bss, NL80211_IFTYPE_STATION))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005010 goto nla_put_failure;
5011
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005012 if (wpa_driver_nl80211_scan(bss, params)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005013 wpa_driver_nl80211_set_mode(bss, drv->nlmode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005014 goto nla_put_failure;
5015 }
5016
5017 /* Restore AP mode when processing scan results */
Dmitry Shmidted003d22014-02-06 10:09:12 -08005018 drv->ap_scan_as_station = old_mode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005019 ret = 0;
5020 } else
5021 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005022 }
5023
Dmitry Shmidt56052862013-10-04 10:23:25 -07005024 drv->scan_state = SCAN_REQUESTED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005025 /* Not all drivers generate "scan completed" wireless event, so try to
5026 * read results after a timeout. */
5027 timeout = 10;
5028 if (drv->scan_complete_events) {
5029 /*
5030 * The driver seems to deliver events to notify when scan is
5031 * complete, so use longer timeout to avoid race conditions
5032 * with scanning and following association request.
5033 */
5034 timeout = 30;
5035 }
5036 wpa_printf(MSG_DEBUG, "Scan requested (ret=%d) - scan timeout %d "
5037 "seconds", ret, timeout);
5038 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
5039 eloop_register_timeout(timeout, 0, wpa_driver_nl80211_scan_timeout,
5040 drv, drv->ctx);
5041
5042nla_put_failure:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005043 nlmsg_free(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005044 return ret;
5045}
5046
5047
5048/**
5049 * wpa_driver_nl80211_sched_scan - Initiate a scheduled scan
5050 * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
5051 * @params: Scan parameters
5052 * @interval: Interval between scan cycles in milliseconds
5053 * Returns: 0 on success, -1 on failure or if not supported
5054 */
5055static int wpa_driver_nl80211_sched_scan(void *priv,
5056 struct wpa_driver_scan_params *params,
5057 u32 interval)
5058{
5059 struct i802_bss *bss = priv;
5060 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005061 int ret = -1;
5062 struct nl_msg *msg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005063 size_t i;
5064
Dmitry Shmidt700a1372013-03-15 14:14:44 -07005065 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: sched_scan request");
5066
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005067#ifdef ANDROID
5068 if (!drv->capa.sched_scan_supported)
5069 return android_pno_start(bss, params);
5070#endif /* ANDROID */
5071
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005072 msg = nl80211_scan_common(drv, NL80211_CMD_START_SCHED_SCAN, params,
5073 bss->wdev_id_set ? &bss->wdev_id : NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005074 if (!msg)
5075 goto nla_put_failure;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005076
5077 NLA_PUT_U32(msg, NL80211_ATTR_SCHED_SCAN_INTERVAL, interval);
5078
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005079 if ((drv->num_filter_ssids &&
5080 (int) drv->num_filter_ssids <= drv->capa.max_match_sets) ||
5081 params->filter_rssi) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005082 struct nlattr *match_sets;
5083 match_sets = nla_nest_start(msg, NL80211_ATTR_SCHED_SCAN_MATCH);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005084 if (match_sets == NULL)
5085 goto nla_put_failure;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005086
5087 for (i = 0; i < drv->num_filter_ssids; i++) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005088 struct nlattr *match_set_ssid;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005089 wpa_hexdump_ascii(MSG_MSGDUMP,
5090 "nl80211: Sched scan filter SSID",
5091 drv->filter_ssids[i].ssid,
5092 drv->filter_ssids[i].ssid_len);
5093
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005094 match_set_ssid = nla_nest_start(msg, i + 1);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005095 if (match_set_ssid == NULL)
5096 goto nla_put_failure;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005097 NLA_PUT(msg, NL80211_ATTR_SCHED_SCAN_MATCH_SSID,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005098 drv->filter_ssids[i].ssid_len,
5099 drv->filter_ssids[i].ssid);
Dmitry Shmidt97672262014-02-03 13:02:54 -08005100 if (params->filter_rssi)
5101 NLA_PUT_U32(msg,
5102 NL80211_SCHED_SCAN_MATCH_ATTR_RSSI,
5103 params->filter_rssi);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005104
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005105 nla_nest_end(msg, match_set_ssid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005106 }
5107
Dmitry Shmidt97672262014-02-03 13:02:54 -08005108 /*
5109 * Due to backward compatibility code, newer kernels treat this
5110 * matchset (with only an RSSI filter) as the default for all
5111 * other matchsets, unless it's the only one, in which case the
5112 * matchset will actually allow all SSIDs above the RSSI.
5113 */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005114 if (params->filter_rssi) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005115 struct nlattr *match_set_rssi;
5116 match_set_rssi = nla_nest_start(msg, 0);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005117 if (match_set_rssi == NULL)
5118 goto nla_put_failure;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005119 NLA_PUT_U32(msg, NL80211_SCHED_SCAN_MATCH_ATTR_RSSI,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005120 params->filter_rssi);
5121 wpa_printf(MSG_MSGDUMP,
5122 "nl80211: Sched scan RSSI filter %d dBm",
5123 params->filter_rssi);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005124 nla_nest_end(msg, match_set_rssi);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005125 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005126
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005127 nla_nest_end(msg, match_sets);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005128 }
5129
5130 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5131
5132 /* TODO: if we get an error here, we should fall back to normal scan */
5133
5134 msg = NULL;
5135 if (ret) {
5136 wpa_printf(MSG_DEBUG, "nl80211: Sched scan start failed: "
5137 "ret=%d (%s)", ret, strerror(-ret));
5138 goto nla_put_failure;
5139 }
5140
5141 wpa_printf(MSG_DEBUG, "nl80211: Sched scan requested (ret=%d) - "
5142 "scan interval %d msec", ret, interval);
5143
5144nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005145 nlmsg_free(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005146 return ret;
5147}
5148
5149
5150/**
5151 * wpa_driver_nl80211_stop_sched_scan - Stop a scheduled scan
5152 * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
5153 * Returns: 0 on success, -1 on failure or if not supported
5154 */
5155static int wpa_driver_nl80211_stop_sched_scan(void *priv)
5156{
5157 struct i802_bss *bss = priv;
5158 struct wpa_driver_nl80211_data *drv = bss->drv;
5159 int ret = 0;
5160 struct nl_msg *msg;
5161
5162#ifdef ANDROID
5163 if (!drv->capa.sched_scan_supported)
5164 return android_pno_stop(bss);
5165#endif /* ANDROID */
5166
5167 msg = nlmsg_alloc();
5168 if (!msg)
5169 return -1;
5170
5171 nl80211_cmd(drv, msg, 0, NL80211_CMD_STOP_SCHED_SCAN);
5172
5173 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
5174
5175 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5176 msg = NULL;
5177 if (ret) {
5178 wpa_printf(MSG_DEBUG, "nl80211: Sched scan stop failed: "
5179 "ret=%d (%s)", ret, strerror(-ret));
5180 goto nla_put_failure;
5181 }
5182
5183 wpa_printf(MSG_DEBUG, "nl80211: Sched scan stop sent (ret=%d)", ret);
5184
5185nla_put_failure:
5186 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005187 return ret;
5188}
5189
5190
5191static const u8 * nl80211_get_ie(const u8 *ies, size_t ies_len, u8 ie)
5192{
5193 const u8 *end, *pos;
5194
5195 if (ies == NULL)
5196 return NULL;
5197
5198 pos = ies;
5199 end = ies + ies_len;
5200
5201 while (pos + 1 < end) {
5202 if (pos + 2 + pos[1] > end)
5203 break;
5204 if (pos[0] == ie)
5205 return pos;
5206 pos += 2 + pos[1];
5207 }
5208
5209 return NULL;
5210}
5211
5212
5213static int nl80211_scan_filtered(struct wpa_driver_nl80211_data *drv,
5214 const u8 *ie, size_t ie_len)
5215{
5216 const u8 *ssid;
5217 size_t i;
5218
5219 if (drv->filter_ssids == NULL)
5220 return 0;
5221
5222 ssid = nl80211_get_ie(ie, ie_len, WLAN_EID_SSID);
5223 if (ssid == NULL)
5224 return 1;
5225
5226 for (i = 0; i < drv->num_filter_ssids; i++) {
5227 if (ssid[1] == drv->filter_ssids[i].ssid_len &&
5228 os_memcmp(ssid + 2, drv->filter_ssids[i].ssid, ssid[1]) ==
5229 0)
5230 return 0;
5231 }
5232
5233 return 1;
5234}
5235
5236
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005237static int bss_info_handler(struct nl_msg *msg, void *arg)
5238{
5239 struct nlattr *tb[NL80211_ATTR_MAX + 1];
5240 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
5241 struct nlattr *bss[NL80211_BSS_MAX + 1];
5242 static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
5243 [NL80211_BSS_BSSID] = { .type = NLA_UNSPEC },
5244 [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
5245 [NL80211_BSS_TSF] = { .type = NLA_U64 },
5246 [NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 },
5247 [NL80211_BSS_CAPABILITY] = { .type = NLA_U16 },
5248 [NL80211_BSS_INFORMATION_ELEMENTS] = { .type = NLA_UNSPEC },
5249 [NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 },
5250 [NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 },
5251 [NL80211_BSS_STATUS] = { .type = NLA_U32 },
5252 [NL80211_BSS_SEEN_MS_AGO] = { .type = NLA_U32 },
5253 [NL80211_BSS_BEACON_IES] = { .type = NLA_UNSPEC },
5254 };
5255 struct nl80211_bss_info_arg *_arg = arg;
5256 struct wpa_scan_results *res = _arg->res;
5257 struct wpa_scan_res **tmp;
5258 struct wpa_scan_res *r;
5259 const u8 *ie, *beacon_ie;
5260 size_t ie_len, beacon_ie_len;
5261 u8 *pos;
Jouni Malinen87fd2792011-05-16 18:35:42 +03005262 size_t i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005263
5264 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
5265 genlmsg_attrlen(gnlh, 0), NULL);
5266 if (!tb[NL80211_ATTR_BSS])
5267 return NL_SKIP;
5268 if (nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS],
5269 bss_policy))
5270 return NL_SKIP;
Jouni Malinen87fd2792011-05-16 18:35:42 +03005271 if (bss[NL80211_BSS_STATUS]) {
5272 enum nl80211_bss_status status;
5273 status = nla_get_u32(bss[NL80211_BSS_STATUS]);
5274 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
5275 bss[NL80211_BSS_FREQUENCY]) {
5276 _arg->assoc_freq =
5277 nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
5278 wpa_printf(MSG_DEBUG, "nl80211: Associated on %u MHz",
5279 _arg->assoc_freq);
5280 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005281 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
5282 bss[NL80211_BSS_BSSID]) {
5283 os_memcpy(_arg->assoc_bssid,
5284 nla_data(bss[NL80211_BSS_BSSID]), ETH_ALEN);
5285 wpa_printf(MSG_DEBUG, "nl80211: Associated with "
5286 MACSTR, MAC2STR(_arg->assoc_bssid));
5287 }
Jouni Malinen87fd2792011-05-16 18:35:42 +03005288 }
5289 if (!res)
5290 return NL_SKIP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005291 if (bss[NL80211_BSS_INFORMATION_ELEMENTS]) {
5292 ie = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
5293 ie_len = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
5294 } else {
5295 ie = NULL;
5296 ie_len = 0;
5297 }
5298 if (bss[NL80211_BSS_BEACON_IES]) {
5299 beacon_ie = nla_data(bss[NL80211_BSS_BEACON_IES]);
5300 beacon_ie_len = nla_len(bss[NL80211_BSS_BEACON_IES]);
5301 } else {
5302 beacon_ie = NULL;
5303 beacon_ie_len = 0;
5304 }
5305
5306 if (nl80211_scan_filtered(_arg->drv, ie ? ie : beacon_ie,
5307 ie ? ie_len : beacon_ie_len))
5308 return NL_SKIP;
5309
5310 r = os_zalloc(sizeof(*r) + ie_len + beacon_ie_len);
5311 if (r == NULL)
5312 return NL_SKIP;
5313 if (bss[NL80211_BSS_BSSID])
5314 os_memcpy(r->bssid, nla_data(bss[NL80211_BSS_BSSID]),
5315 ETH_ALEN);
5316 if (bss[NL80211_BSS_FREQUENCY])
5317 r->freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
5318 if (bss[NL80211_BSS_BEACON_INTERVAL])
5319 r->beacon_int = nla_get_u16(bss[NL80211_BSS_BEACON_INTERVAL]);
5320 if (bss[NL80211_BSS_CAPABILITY])
5321 r->caps = nla_get_u16(bss[NL80211_BSS_CAPABILITY]);
5322 r->flags |= WPA_SCAN_NOISE_INVALID;
5323 if (bss[NL80211_BSS_SIGNAL_MBM]) {
5324 r->level = nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]);
5325 r->level /= 100; /* mBm to dBm */
5326 r->flags |= WPA_SCAN_LEVEL_DBM | WPA_SCAN_QUAL_INVALID;
5327 } else if (bss[NL80211_BSS_SIGNAL_UNSPEC]) {
5328 r->level = nla_get_u8(bss[NL80211_BSS_SIGNAL_UNSPEC]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005329 r->flags |= WPA_SCAN_QUAL_INVALID;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005330 } else
5331 r->flags |= WPA_SCAN_LEVEL_INVALID | WPA_SCAN_QUAL_INVALID;
5332 if (bss[NL80211_BSS_TSF])
5333 r->tsf = nla_get_u64(bss[NL80211_BSS_TSF]);
5334 if (bss[NL80211_BSS_SEEN_MS_AGO])
5335 r->age = nla_get_u32(bss[NL80211_BSS_SEEN_MS_AGO]);
5336 r->ie_len = ie_len;
5337 pos = (u8 *) (r + 1);
5338 if (ie) {
5339 os_memcpy(pos, ie, ie_len);
5340 pos += ie_len;
5341 }
5342 r->beacon_ie_len = beacon_ie_len;
5343 if (beacon_ie)
5344 os_memcpy(pos, beacon_ie, beacon_ie_len);
5345
5346 if (bss[NL80211_BSS_STATUS]) {
5347 enum nl80211_bss_status status;
5348 status = nla_get_u32(bss[NL80211_BSS_STATUS]);
5349 switch (status) {
5350 case NL80211_BSS_STATUS_AUTHENTICATED:
5351 r->flags |= WPA_SCAN_AUTHENTICATED;
5352 break;
5353 case NL80211_BSS_STATUS_ASSOCIATED:
5354 r->flags |= WPA_SCAN_ASSOCIATED;
5355 break;
5356 default:
5357 break;
5358 }
5359 }
5360
Jouni Malinen87fd2792011-05-16 18:35:42 +03005361 /*
5362 * cfg80211 maintains separate BSS table entries for APs if the same
5363 * BSSID,SSID pair is seen on multiple channels. wpa_supplicant does
5364 * not use frequency as a separate key in the BSS table, so filter out
5365 * duplicated entries. Prefer associated BSS entry in such a case in
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005366 * order to get the correct frequency into the BSS table. Similarly,
5367 * prefer newer entries over older.
Jouni Malinen87fd2792011-05-16 18:35:42 +03005368 */
5369 for (i = 0; i < res->num; i++) {
5370 const u8 *s1, *s2;
5371 if (os_memcmp(res->res[i]->bssid, r->bssid, ETH_ALEN) != 0)
5372 continue;
5373
5374 s1 = nl80211_get_ie((u8 *) (res->res[i] + 1),
5375 res->res[i]->ie_len, WLAN_EID_SSID);
5376 s2 = nl80211_get_ie((u8 *) (r + 1), r->ie_len, WLAN_EID_SSID);
5377 if (s1 == NULL || s2 == NULL || s1[1] != s2[1] ||
5378 os_memcmp(s1, s2, 2 + s1[1]) != 0)
5379 continue;
5380
5381 /* Same BSSID,SSID was already included in scan results */
5382 wpa_printf(MSG_DEBUG, "nl80211: Remove duplicated scan result "
5383 "for " MACSTR, MAC2STR(r->bssid));
5384
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005385 if (((r->flags & WPA_SCAN_ASSOCIATED) &&
5386 !(res->res[i]->flags & WPA_SCAN_ASSOCIATED)) ||
5387 r->age < res->res[i]->age) {
Jouni Malinen87fd2792011-05-16 18:35:42 +03005388 os_free(res->res[i]);
5389 res->res[i] = r;
5390 } else
5391 os_free(r);
5392 return NL_SKIP;
5393 }
5394
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005395 tmp = os_realloc_array(res->res, res->num + 1,
5396 sizeof(struct wpa_scan_res *));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005397 if (tmp == NULL) {
5398 os_free(r);
5399 return NL_SKIP;
5400 }
5401 tmp[res->num++] = r;
5402 res->res = tmp;
5403
5404 return NL_SKIP;
5405}
5406
5407
5408static void clear_state_mismatch(struct wpa_driver_nl80211_data *drv,
5409 const u8 *addr)
5410{
5411 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
5412 wpa_printf(MSG_DEBUG, "nl80211: Clear possible state "
5413 "mismatch (" MACSTR ")", MAC2STR(addr));
5414 wpa_driver_nl80211_mlme(drv, addr,
5415 NL80211_CMD_DEAUTHENTICATE,
5416 WLAN_REASON_PREV_AUTH_NOT_VALID, 1);
5417 }
5418}
5419
5420
5421static void wpa_driver_nl80211_check_bss_status(
5422 struct wpa_driver_nl80211_data *drv, struct wpa_scan_results *res)
5423{
5424 size_t i;
5425
5426 for (i = 0; i < res->num; i++) {
5427 struct wpa_scan_res *r = res->res[i];
5428 if (r->flags & WPA_SCAN_AUTHENTICATED) {
5429 wpa_printf(MSG_DEBUG, "nl80211: Scan results "
5430 "indicates BSS status with " MACSTR
5431 " as authenticated",
5432 MAC2STR(r->bssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005433 if (is_sta_interface(drv->nlmode) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005434 os_memcmp(r->bssid, drv->bssid, ETH_ALEN) != 0 &&
5435 os_memcmp(r->bssid, drv->auth_bssid, ETH_ALEN) !=
5436 0) {
5437 wpa_printf(MSG_DEBUG, "nl80211: Unknown BSSID"
5438 " in local state (auth=" MACSTR
5439 " assoc=" MACSTR ")",
5440 MAC2STR(drv->auth_bssid),
5441 MAC2STR(drv->bssid));
5442 clear_state_mismatch(drv, r->bssid);
5443 }
5444 }
5445
5446 if (r->flags & WPA_SCAN_ASSOCIATED) {
5447 wpa_printf(MSG_DEBUG, "nl80211: Scan results "
5448 "indicate BSS status with " MACSTR
5449 " as associated",
5450 MAC2STR(r->bssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005451 if (is_sta_interface(drv->nlmode) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005452 !drv->associated) {
5453 wpa_printf(MSG_DEBUG, "nl80211: Local state "
5454 "(not associated) does not match "
5455 "with BSS state");
5456 clear_state_mismatch(drv, r->bssid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005457 } else if (is_sta_interface(drv->nlmode) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005458 os_memcmp(drv->bssid, r->bssid, ETH_ALEN) !=
5459 0) {
5460 wpa_printf(MSG_DEBUG, "nl80211: Local state "
5461 "(associated with " MACSTR ") does "
5462 "not match with BSS state",
5463 MAC2STR(drv->bssid));
5464 clear_state_mismatch(drv, r->bssid);
5465 clear_state_mismatch(drv, drv->bssid);
5466 }
5467 }
5468 }
5469}
5470
5471
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005472static struct wpa_scan_results *
5473nl80211_get_scan_results(struct wpa_driver_nl80211_data *drv)
5474{
5475 struct nl_msg *msg;
5476 struct wpa_scan_results *res;
5477 int ret;
5478 struct nl80211_bss_info_arg arg;
5479
5480 res = os_zalloc(sizeof(*res));
5481 if (res == NULL)
5482 return NULL;
5483 msg = nlmsg_alloc();
5484 if (!msg)
5485 goto nla_put_failure;
5486
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005487 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005488 if (nl80211_set_iface_id(msg, drv->first_bss) < 0)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005489 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005490
5491 arg.drv = drv;
5492 arg.res = res;
5493 ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
5494 msg = NULL;
5495 if (ret == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005496 wpa_printf(MSG_DEBUG, "nl80211: Received scan results (%lu "
5497 "BSSes)", (unsigned long) res->num);
5498 nl80211_get_noise_for_scan_results(drv, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005499 return res;
5500 }
5501 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
5502 "(%s)", ret, strerror(-ret));
5503nla_put_failure:
5504 nlmsg_free(msg);
5505 wpa_scan_results_free(res);
5506 return NULL;
5507}
5508
5509
5510/**
5511 * wpa_driver_nl80211_get_scan_results - Fetch the latest scan results
5512 * @priv: Pointer to private wext data from wpa_driver_nl80211_init()
5513 * Returns: Scan results on success, -1 on failure
5514 */
5515static struct wpa_scan_results *
5516wpa_driver_nl80211_get_scan_results(void *priv)
5517{
5518 struct i802_bss *bss = priv;
5519 struct wpa_driver_nl80211_data *drv = bss->drv;
5520 struct wpa_scan_results *res;
5521
5522 res = nl80211_get_scan_results(drv);
5523 if (res)
5524 wpa_driver_nl80211_check_bss_status(drv, res);
5525 return res;
5526}
5527
5528
5529static void nl80211_dump_scan(struct wpa_driver_nl80211_data *drv)
5530{
5531 struct wpa_scan_results *res;
5532 size_t i;
5533
5534 res = nl80211_get_scan_results(drv);
5535 if (res == NULL) {
5536 wpa_printf(MSG_DEBUG, "nl80211: Failed to get scan results");
5537 return;
5538 }
5539
5540 wpa_printf(MSG_DEBUG, "nl80211: Scan result dump");
5541 for (i = 0; i < res->num; i++) {
5542 struct wpa_scan_res *r = res->res[i];
5543 wpa_printf(MSG_DEBUG, "nl80211: %d/%d " MACSTR "%s%s",
5544 (int) i, (int) res->num, MAC2STR(r->bssid),
5545 r->flags & WPA_SCAN_AUTHENTICATED ? " [auth]" : "",
5546 r->flags & WPA_SCAN_ASSOCIATED ? " [assoc]" : "");
5547 }
5548
5549 wpa_scan_results_free(res);
5550}
5551
5552
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005553static u32 wpa_alg_to_cipher_suite(enum wpa_alg alg, size_t key_len)
5554{
5555 switch (alg) {
5556 case WPA_ALG_WEP:
5557 if (key_len == 5)
5558 return WLAN_CIPHER_SUITE_WEP40;
5559 return WLAN_CIPHER_SUITE_WEP104;
5560 case WPA_ALG_TKIP:
5561 return WLAN_CIPHER_SUITE_TKIP;
5562 case WPA_ALG_CCMP:
5563 return WLAN_CIPHER_SUITE_CCMP;
5564 case WPA_ALG_GCMP:
5565 return WLAN_CIPHER_SUITE_GCMP;
5566 case WPA_ALG_CCMP_256:
5567 return WLAN_CIPHER_SUITE_CCMP_256;
5568 case WPA_ALG_GCMP_256:
5569 return WLAN_CIPHER_SUITE_GCMP_256;
5570 case WPA_ALG_IGTK:
5571 return WLAN_CIPHER_SUITE_AES_CMAC;
5572 case WPA_ALG_BIP_GMAC_128:
5573 return WLAN_CIPHER_SUITE_BIP_GMAC_128;
5574 case WPA_ALG_BIP_GMAC_256:
5575 return WLAN_CIPHER_SUITE_BIP_GMAC_256;
5576 case WPA_ALG_BIP_CMAC_256:
5577 return WLAN_CIPHER_SUITE_BIP_CMAC_256;
5578 case WPA_ALG_SMS4:
5579 return WLAN_CIPHER_SUITE_SMS4;
5580 case WPA_ALG_KRK:
5581 return WLAN_CIPHER_SUITE_KRK;
5582 case WPA_ALG_NONE:
5583 case WPA_ALG_PMK:
5584 wpa_printf(MSG_ERROR, "nl80211: Unexpected encryption algorithm %d",
5585 alg);
5586 return 0;
5587 }
5588
5589 wpa_printf(MSG_ERROR, "nl80211: Unsupported encryption algorithm %d",
5590 alg);
5591 return 0;
5592}
5593
5594
5595static u32 wpa_cipher_to_cipher_suite(unsigned int cipher)
5596{
5597 switch (cipher) {
5598 case WPA_CIPHER_CCMP_256:
5599 return WLAN_CIPHER_SUITE_CCMP_256;
5600 case WPA_CIPHER_GCMP_256:
5601 return WLAN_CIPHER_SUITE_GCMP_256;
5602 case WPA_CIPHER_CCMP:
5603 return WLAN_CIPHER_SUITE_CCMP;
5604 case WPA_CIPHER_GCMP:
5605 return WLAN_CIPHER_SUITE_GCMP;
5606 case WPA_CIPHER_TKIP:
5607 return WLAN_CIPHER_SUITE_TKIP;
5608 case WPA_CIPHER_WEP104:
5609 return WLAN_CIPHER_SUITE_WEP104;
5610 case WPA_CIPHER_WEP40:
5611 return WLAN_CIPHER_SUITE_WEP40;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08005612 case WPA_CIPHER_GTK_NOT_USED:
5613 return WLAN_CIPHER_SUITE_NO_GROUP_ADDR;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005614 }
5615
5616 return 0;
5617}
5618
5619
5620static int wpa_cipher_to_cipher_suites(unsigned int ciphers, u32 suites[],
5621 int max_suites)
5622{
5623 int num_suites = 0;
5624
5625 if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP_256)
5626 suites[num_suites++] = WLAN_CIPHER_SUITE_CCMP_256;
5627 if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP_256)
5628 suites[num_suites++] = WLAN_CIPHER_SUITE_GCMP_256;
5629 if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP)
5630 suites[num_suites++] = WLAN_CIPHER_SUITE_CCMP;
5631 if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP)
5632 suites[num_suites++] = WLAN_CIPHER_SUITE_GCMP;
5633 if (num_suites < max_suites && ciphers & WPA_CIPHER_TKIP)
5634 suites[num_suites++] = WLAN_CIPHER_SUITE_TKIP;
5635 if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP104)
5636 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP104;
5637 if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP40)
5638 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP40;
5639
5640 return num_suites;
5641}
5642
5643
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005644static int wpa_driver_nl80211_set_key(const char *ifname, struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005645 enum wpa_alg alg, const u8 *addr,
5646 int key_idx, int set_tx,
5647 const u8 *seq, size_t seq_len,
5648 const u8 *key, size_t key_len)
5649{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005650 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005651 int ifindex;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005652 struct nl_msg *msg;
5653 int ret;
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07005654 int tdls = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005655
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005656 /* Ignore for P2P Device */
5657 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
5658 return 0;
5659
5660 ifindex = if_nametoindex(ifname);
5661 wpa_printf(MSG_DEBUG, "%s: ifindex=%d (%s) alg=%d addr=%p key_idx=%d "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005662 "set_tx=%d seq_len=%lu key_len=%lu",
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005663 __func__, ifindex, ifname, alg, addr, key_idx, set_tx,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005664 (unsigned long) seq_len, (unsigned long) key_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005665#ifdef CONFIG_TDLS
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07005666 if (key_idx == -1) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005667 key_idx = 0;
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07005668 tdls = 1;
5669 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005670#endif /* CONFIG_TDLS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005671
5672 msg = nlmsg_alloc();
5673 if (!msg)
5674 return -ENOMEM;
5675
5676 if (alg == WPA_ALG_NONE) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005677 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_KEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005678 } else {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005679 nl80211_cmd(drv, msg, 0, NL80211_CMD_NEW_KEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005680 NLA_PUT(msg, NL80211_ATTR_KEY_DATA, key_len, key);
Dmitry Shmidt98660862014-03-11 17:26:21 -07005681 wpa_hexdump_key(MSG_DEBUG, "nl80211: KEY_DATA", key, key_len);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005682 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
5683 wpa_alg_to_cipher_suite(alg, key_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005684 }
5685
Dmitry Shmidt98660862014-03-11 17:26:21 -07005686 if (seq && seq_len) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005687 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, seq_len, seq);
Dmitry Shmidt98660862014-03-11 17:26:21 -07005688 wpa_hexdump(MSG_DEBUG, "nl80211: KEY_SEQ", seq, seq_len);
5689 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005690
5691 if (addr && !is_broadcast_ether_addr(addr)) {
5692 wpa_printf(MSG_DEBUG, " addr=" MACSTR, MAC2STR(addr));
5693 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
5694
5695 if (alg != WPA_ALG_WEP && key_idx && !set_tx) {
5696 wpa_printf(MSG_DEBUG, " RSN IBSS RX GTK");
5697 NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE,
5698 NL80211_KEYTYPE_GROUP);
5699 }
5700 } else if (addr && is_broadcast_ether_addr(addr)) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005701 struct nlattr *types;
5702
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005703 wpa_printf(MSG_DEBUG, " broadcast key");
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005704
5705 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005706 if (!types)
5707 goto nla_put_failure;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005708 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST);
5709 nla_nest_end(msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005710 }
5711 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
5712 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
5713
5714 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5715 if ((ret == -ENOENT || ret == -ENOLINK) && alg == WPA_ALG_NONE)
5716 ret = 0;
5717 if (ret)
5718 wpa_printf(MSG_DEBUG, "nl80211: set_key failed; err=%d %s)",
5719 ret, strerror(-ret));
5720
5721 /*
5722 * If we failed or don't need to set the default TX key (below),
5723 * we're done here.
5724 */
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07005725 if (ret || !set_tx || alg == WPA_ALG_NONE || tdls)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005726 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005727 if (is_ap_interface(drv->nlmode) && addr &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005728 !is_broadcast_ether_addr(addr))
5729 return ret;
5730
5731 msg = nlmsg_alloc();
5732 if (!msg)
5733 return -ENOMEM;
5734
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005735 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_KEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005736 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
5737 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
5738 if (alg == WPA_ALG_IGTK)
5739 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT_MGMT);
5740 else
5741 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT);
5742 if (addr && is_broadcast_ether_addr(addr)) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005743 struct nlattr *types;
5744
5745 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005746 if (!types)
5747 goto nla_put_failure;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005748 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST);
5749 nla_nest_end(msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005750 } else if (addr) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005751 struct nlattr *types;
5752
5753 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005754 if (!types)
5755 goto nla_put_failure;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005756 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_UNICAST);
5757 nla_nest_end(msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005758 }
5759
5760 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5761 if (ret == -ENOENT)
5762 ret = 0;
5763 if (ret)
5764 wpa_printf(MSG_DEBUG, "nl80211: set_key default failed; "
5765 "err=%d %s)", ret, strerror(-ret));
5766 return ret;
5767
5768nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005769 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005770 return -ENOBUFS;
5771}
5772
5773
5774static int nl_add_key(struct nl_msg *msg, enum wpa_alg alg,
5775 int key_idx, int defkey,
5776 const u8 *seq, size_t seq_len,
5777 const u8 *key, size_t key_len)
5778{
5779 struct nlattr *key_attr = nla_nest_start(msg, NL80211_ATTR_KEY);
5780 if (!key_attr)
5781 return -1;
5782
5783 if (defkey && alg == WPA_ALG_IGTK)
5784 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_MGMT);
5785 else if (defkey)
5786 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
5787
5788 NLA_PUT_U8(msg, NL80211_KEY_IDX, key_idx);
5789
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005790 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
5791 wpa_alg_to_cipher_suite(alg, key_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005792
5793 if (seq && seq_len)
5794 NLA_PUT(msg, NL80211_KEY_SEQ, seq_len, seq);
5795
5796 NLA_PUT(msg, NL80211_KEY_DATA, key_len, key);
5797
5798 nla_nest_end(msg, key_attr);
5799
5800 return 0;
5801 nla_put_failure:
5802 return -1;
5803}
5804
5805
5806static int nl80211_set_conn_keys(struct wpa_driver_associate_params *params,
5807 struct nl_msg *msg)
5808{
5809 int i, privacy = 0;
5810 struct nlattr *nl_keys, *nl_key;
5811
5812 for (i = 0; i < 4; i++) {
5813 if (!params->wep_key[i])
5814 continue;
5815 privacy = 1;
5816 break;
5817 }
5818 if (params->wps == WPS_MODE_PRIVACY)
5819 privacy = 1;
5820 if (params->pairwise_suite &&
5821 params->pairwise_suite != WPA_CIPHER_NONE)
5822 privacy = 1;
5823
5824 if (!privacy)
5825 return 0;
5826
5827 NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
5828
5829 nl_keys = nla_nest_start(msg, NL80211_ATTR_KEYS);
5830 if (!nl_keys)
5831 goto nla_put_failure;
5832
5833 for (i = 0; i < 4; i++) {
5834 if (!params->wep_key[i])
5835 continue;
5836
5837 nl_key = nla_nest_start(msg, i);
5838 if (!nl_key)
5839 goto nla_put_failure;
5840
5841 NLA_PUT(msg, NL80211_KEY_DATA, params->wep_key_len[i],
5842 params->wep_key[i]);
5843 if (params->wep_key_len[i] == 5)
5844 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
5845 WLAN_CIPHER_SUITE_WEP40);
5846 else
5847 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
5848 WLAN_CIPHER_SUITE_WEP104);
5849
5850 NLA_PUT_U8(msg, NL80211_KEY_IDX, i);
5851
5852 if (i == params->wep_tx_keyidx)
5853 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
5854
5855 nla_nest_end(msg, nl_key);
5856 }
5857 nla_nest_end(msg, nl_keys);
5858
5859 return 0;
5860
5861nla_put_failure:
5862 return -ENOBUFS;
5863}
5864
5865
5866static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
5867 const u8 *addr, int cmd, u16 reason_code,
5868 int local_state_change)
5869{
5870 int ret = -1;
5871 struct nl_msg *msg;
5872
5873 msg = nlmsg_alloc();
5874 if (!msg)
5875 return -1;
5876
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005877 nl80211_cmd(drv, msg, 0, cmd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005878
5879 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
5880 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason_code);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005881 if (addr)
5882 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005883 if (local_state_change)
5884 NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE);
5885
5886 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5887 msg = NULL;
5888 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005889 wpa_dbg(drv->ctx, MSG_DEBUG,
5890 "nl80211: MLME command failed: reason=%u ret=%d (%s)",
5891 reason_code, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005892 goto nla_put_failure;
5893 }
5894 ret = 0;
5895
5896nla_put_failure:
5897 nlmsg_free(msg);
5898 return ret;
5899}
5900
5901
5902static int wpa_driver_nl80211_disconnect(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005903 int reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005904{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005905 int ret;
5906
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005907 wpa_printf(MSG_DEBUG, "%s(reason_code=%d)", __func__, reason_code);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005908 nl80211_mark_disconnected(drv);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005909 /* Disconnect command doesn't need BSSID - it uses cached value */
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005910 ret = wpa_driver_nl80211_mlme(drv, NULL, NL80211_CMD_DISCONNECT,
5911 reason_code, 0);
5912 /*
5913 * For locally generated disconnect, supplicant already generates a
5914 * DEAUTH event, so ignore the event from NL80211.
5915 */
5916 drv->ignore_next_local_disconnect = ret == 0;
5917
5918 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005919}
5920
5921
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005922static int wpa_driver_nl80211_deauthenticate(struct i802_bss *bss,
5923 const u8 *addr, int reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005924{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005925 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07005926 int ret;
Dmitry Shmidt413dde72014-04-11 10:23:22 -07005927
5928 if (drv->nlmode == NL80211_IFTYPE_ADHOC) {
5929 nl80211_mark_disconnected(drv);
5930 return nl80211_leave_ibss(drv);
5931 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005932 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME))
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005933 return wpa_driver_nl80211_disconnect(drv, reason_code);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005934 wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " reason_code=%d)",
5935 __func__, MAC2STR(addr), reason_code);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005936 nl80211_mark_disconnected(drv);
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07005937 ret = wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DEAUTHENTICATE,
5938 reason_code, 0);
5939 /*
5940 * For locally generated deauthenticate, supplicant already generates a
5941 * DEAUTH event, so ignore the event from NL80211.
5942 */
5943 drv->ignore_next_local_deauth = ret == 0;
5944 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005945}
5946
5947
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005948static void nl80211_copy_auth_params(struct wpa_driver_nl80211_data *drv,
5949 struct wpa_driver_auth_params *params)
5950{
5951 int i;
5952
5953 drv->auth_freq = params->freq;
5954 drv->auth_alg = params->auth_alg;
5955 drv->auth_wep_tx_keyidx = params->wep_tx_keyidx;
5956 drv->auth_local_state_change = params->local_state_change;
5957 drv->auth_p2p = params->p2p;
5958
5959 if (params->bssid)
5960 os_memcpy(drv->auth_bssid_, params->bssid, ETH_ALEN);
5961 else
5962 os_memset(drv->auth_bssid_, 0, ETH_ALEN);
5963
5964 if (params->ssid) {
5965 os_memcpy(drv->auth_ssid, params->ssid, params->ssid_len);
5966 drv->auth_ssid_len = params->ssid_len;
5967 } else
5968 drv->auth_ssid_len = 0;
5969
5970
5971 os_free(drv->auth_ie);
5972 drv->auth_ie = NULL;
5973 drv->auth_ie_len = 0;
5974 if (params->ie) {
5975 drv->auth_ie = os_malloc(params->ie_len);
5976 if (drv->auth_ie) {
5977 os_memcpy(drv->auth_ie, params->ie, params->ie_len);
5978 drv->auth_ie_len = params->ie_len;
5979 }
5980 }
5981
5982 for (i = 0; i < 4; i++) {
5983 if (params->wep_key[i] && params->wep_key_len[i] &&
5984 params->wep_key_len[i] <= 16) {
5985 os_memcpy(drv->auth_wep_key[i], params->wep_key[i],
5986 params->wep_key_len[i]);
5987 drv->auth_wep_key_len[i] = params->wep_key_len[i];
5988 } else
5989 drv->auth_wep_key_len[i] = 0;
5990 }
5991}
5992
5993
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005994static int wpa_driver_nl80211_authenticate(
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005995 struct i802_bss *bss, struct wpa_driver_auth_params *params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005996{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005997 struct wpa_driver_nl80211_data *drv = bss->drv;
5998 int ret = -1, i;
5999 struct nl_msg *msg;
6000 enum nl80211_auth_type type;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006001 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006002 int count = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006003 int is_retry;
6004
6005 is_retry = drv->retry_auth;
6006 drv->retry_auth = 0;
Dmitry Shmidt98660862014-03-11 17:26:21 -07006007 drv->ignore_deauth_event = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006008
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006009 nl80211_mark_disconnected(drv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006010 os_memset(drv->auth_bssid, 0, ETH_ALEN);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006011 if (params->bssid)
6012 os_memcpy(drv->auth_attempt_bssid, params->bssid, ETH_ALEN);
6013 else
6014 os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006015 /* FIX: IBSS mode */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006016 nlmode = params->p2p ?
6017 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
6018 if (drv->nlmode != nlmode &&
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006019 wpa_driver_nl80211_set_mode(bss, nlmode) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006020 return -1;
6021
6022retry:
6023 msg = nlmsg_alloc();
6024 if (!msg)
6025 return -1;
6026
6027 wpa_printf(MSG_DEBUG, "nl80211: Authenticate (ifindex=%d)",
6028 drv->ifindex);
6029
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006030 nl80211_cmd(drv, msg, 0, NL80211_CMD_AUTHENTICATE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006031
6032 for (i = 0; i < 4; i++) {
6033 if (!params->wep_key[i])
6034 continue;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006035 wpa_driver_nl80211_set_key(bss->ifname, bss, WPA_ALG_WEP,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006036 NULL, i,
6037 i == params->wep_tx_keyidx, NULL, 0,
6038 params->wep_key[i],
6039 params->wep_key_len[i]);
6040 if (params->wep_tx_keyidx != i)
6041 continue;
6042 if (nl_add_key(msg, WPA_ALG_WEP, i, 1, NULL, 0,
6043 params->wep_key[i], params->wep_key_len[i])) {
6044 nlmsg_free(msg);
6045 return -1;
6046 }
6047 }
6048
6049 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
6050 if (params->bssid) {
6051 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
6052 MAC2STR(params->bssid));
6053 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
6054 }
6055 if (params->freq) {
6056 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
6057 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
6058 }
6059 if (params->ssid) {
6060 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
6061 params->ssid, params->ssid_len);
6062 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
6063 params->ssid);
6064 }
6065 wpa_hexdump(MSG_DEBUG, " * IEs", params->ie, params->ie_len);
6066 if (params->ie)
6067 NLA_PUT(msg, NL80211_ATTR_IE, params->ie_len, params->ie);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006068 if (params->sae_data) {
6069 wpa_hexdump(MSG_DEBUG, " * SAE data", params->sae_data,
6070 params->sae_data_len);
6071 NLA_PUT(msg, NL80211_ATTR_SAE_DATA, params->sae_data_len,
6072 params->sae_data);
6073 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006074 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
6075 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
6076 else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
6077 type = NL80211_AUTHTYPE_SHARED_KEY;
6078 else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
6079 type = NL80211_AUTHTYPE_NETWORK_EAP;
6080 else if (params->auth_alg & WPA_AUTH_ALG_FT)
6081 type = NL80211_AUTHTYPE_FT;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006082 else if (params->auth_alg & WPA_AUTH_ALG_SAE)
6083 type = NL80211_AUTHTYPE_SAE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006084 else
6085 goto nla_put_failure;
6086 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
6087 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
6088 if (params->local_state_change) {
6089 wpa_printf(MSG_DEBUG, " * Local state change only");
6090 NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE);
6091 }
6092
6093 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6094 msg = NULL;
6095 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006096 wpa_dbg(drv->ctx, MSG_DEBUG,
6097 "nl80211: MLME command failed (auth): ret=%d (%s)",
6098 ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006099 count++;
6100 if (ret == -EALREADY && count == 1 && params->bssid &&
6101 !params->local_state_change) {
6102 /*
6103 * mac80211 does not currently accept new
6104 * authentication if we are already authenticated. As a
6105 * workaround, force deauthentication and try again.
6106 */
6107 wpa_printf(MSG_DEBUG, "nl80211: Retry authentication "
6108 "after forced deauthentication");
Dmitry Shmidt98660862014-03-11 17:26:21 -07006109 drv->ignore_deauth_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006110 wpa_driver_nl80211_deauthenticate(
6111 bss, params->bssid,
6112 WLAN_REASON_PREV_AUTH_NOT_VALID);
6113 nlmsg_free(msg);
6114 goto retry;
6115 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006116
6117 if (ret == -ENOENT && params->freq && !is_retry) {
6118 /*
6119 * cfg80211 has likely expired the BSS entry even
6120 * though it was previously available in our internal
6121 * BSS table. To recover quickly, start a single
6122 * channel scan on the specified channel.
6123 */
6124 struct wpa_driver_scan_params scan;
6125 int freqs[2];
6126
6127 os_memset(&scan, 0, sizeof(scan));
6128 scan.num_ssids = 1;
6129 if (params->ssid) {
6130 scan.ssids[0].ssid = params->ssid;
6131 scan.ssids[0].ssid_len = params->ssid_len;
6132 }
6133 freqs[0] = params->freq;
6134 freqs[1] = 0;
6135 scan.freqs = freqs;
6136 wpa_printf(MSG_DEBUG, "nl80211: Trigger single "
6137 "channel scan to refresh cfg80211 BSS "
6138 "entry");
6139 ret = wpa_driver_nl80211_scan(bss, &scan);
6140 if (ret == 0) {
6141 nl80211_copy_auth_params(drv, params);
6142 drv->scan_for_auth = 1;
6143 }
6144 } else if (is_retry) {
6145 /*
6146 * Need to indicate this with an event since the return
6147 * value from the retry is not delivered to core code.
6148 */
6149 union wpa_event_data event;
6150 wpa_printf(MSG_DEBUG, "nl80211: Authentication retry "
6151 "failed");
6152 os_memset(&event, 0, sizeof(event));
6153 os_memcpy(event.timeout_event.addr, drv->auth_bssid_,
6154 ETH_ALEN);
6155 wpa_supplicant_event(drv->ctx, EVENT_AUTH_TIMED_OUT,
6156 &event);
6157 }
6158
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006159 goto nla_put_failure;
6160 }
6161 ret = 0;
6162 wpa_printf(MSG_DEBUG, "nl80211: Authentication request send "
6163 "successfully");
6164
6165nla_put_failure:
6166 nlmsg_free(msg);
6167 return ret;
6168}
6169
6170
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006171static int wpa_driver_nl80211_authenticate_retry(
6172 struct wpa_driver_nl80211_data *drv)
6173{
6174 struct wpa_driver_auth_params params;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006175 struct i802_bss *bss = drv->first_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006176 int i;
6177
6178 wpa_printf(MSG_DEBUG, "nl80211: Try to authenticate again");
6179
6180 os_memset(&params, 0, sizeof(params));
6181 params.freq = drv->auth_freq;
6182 params.auth_alg = drv->auth_alg;
6183 params.wep_tx_keyidx = drv->auth_wep_tx_keyidx;
6184 params.local_state_change = drv->auth_local_state_change;
6185 params.p2p = drv->auth_p2p;
6186
6187 if (!is_zero_ether_addr(drv->auth_bssid_))
6188 params.bssid = drv->auth_bssid_;
6189
6190 if (drv->auth_ssid_len) {
6191 params.ssid = drv->auth_ssid;
6192 params.ssid_len = drv->auth_ssid_len;
6193 }
6194
6195 params.ie = drv->auth_ie;
6196 params.ie_len = drv->auth_ie_len;
6197
6198 for (i = 0; i < 4; i++) {
6199 if (drv->auth_wep_key_len[i]) {
6200 params.wep_key[i] = drv->auth_wep_key[i];
6201 params.wep_key_len[i] = drv->auth_wep_key_len[i];
6202 }
6203 }
6204
6205 drv->retry_auth = 1;
6206 return wpa_driver_nl80211_authenticate(bss, &params);
6207}
6208
6209
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006210struct phy_info_arg {
6211 u16 *num_modes;
6212 struct hostapd_hw_modes *modes;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006213 int last_mode, last_chan_idx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006214};
6215
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006216static void phy_info_ht_capa(struct hostapd_hw_modes *mode, struct nlattr *capa,
6217 struct nlattr *ampdu_factor,
6218 struct nlattr *ampdu_density,
6219 struct nlattr *mcs_set)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006220{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006221 if (capa)
6222 mode->ht_capab = nla_get_u16(capa);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006223
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006224 if (ampdu_factor)
6225 mode->a_mpdu_params |= nla_get_u8(ampdu_factor) & 0x03;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006226
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006227 if (ampdu_density)
6228 mode->a_mpdu_params |= nla_get_u8(ampdu_density) << 2;
6229
6230 if (mcs_set && nla_len(mcs_set) >= 16) {
6231 u8 *mcs;
6232 mcs = nla_data(mcs_set);
6233 os_memcpy(mode->mcs_set, mcs, 16);
6234 }
6235}
6236
6237
6238static void phy_info_vht_capa(struct hostapd_hw_modes *mode,
6239 struct nlattr *capa,
6240 struct nlattr *mcs_set)
6241{
6242 if (capa)
6243 mode->vht_capab = nla_get_u32(capa);
6244
6245 if (mcs_set && nla_len(mcs_set) >= 8) {
6246 u8 *mcs;
6247 mcs = nla_data(mcs_set);
6248 os_memcpy(mode->vht_mcs_set, mcs, 8);
6249 }
6250}
6251
6252
6253static void phy_info_freq(struct hostapd_hw_modes *mode,
6254 struct hostapd_channel_data *chan,
6255 struct nlattr *tb_freq[])
6256{
Dmitry Shmidt4b060592013-04-29 16:42:49 -07006257 u8 channel;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006258 chan->freq = nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_FREQ]);
6259 chan->flag = 0;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006260 chan->dfs_cac_ms = 0;
Dmitry Shmidt4b060592013-04-29 16:42:49 -07006261 if (ieee80211_freq_to_chan(chan->freq, &channel) != NUM_HOSTAPD_MODES)
6262 chan->chan = channel;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006263
6264 if (tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
6265 chan->flag |= HOSTAPD_CHAN_DISABLED;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006266 if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IR])
6267 chan->flag |= HOSTAPD_CHAN_PASSIVE_SCAN | HOSTAPD_CHAN_NO_IBSS;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006268 if (tb_freq[NL80211_FREQUENCY_ATTR_RADAR])
6269 chan->flag |= HOSTAPD_CHAN_RADAR;
6270
Dmitry Shmidtea69e842013-05-13 14:52:28 -07006271 if (tb_freq[NL80211_FREQUENCY_ATTR_DFS_STATE]) {
6272 enum nl80211_dfs_state state =
6273 nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_DFS_STATE]);
6274
6275 switch (state) {
6276 case NL80211_DFS_USABLE:
6277 chan->flag |= HOSTAPD_CHAN_DFS_USABLE;
6278 break;
6279 case NL80211_DFS_AVAILABLE:
6280 chan->flag |= HOSTAPD_CHAN_DFS_AVAILABLE;
6281 break;
6282 case NL80211_DFS_UNAVAILABLE:
6283 chan->flag |= HOSTAPD_CHAN_DFS_UNAVAILABLE;
6284 break;
6285 }
6286 }
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006287
6288 if (tb_freq[NL80211_FREQUENCY_ATTR_DFS_CAC_TIME]) {
6289 chan->dfs_cac_ms = nla_get_u32(
6290 tb_freq[NL80211_FREQUENCY_ATTR_DFS_CAC_TIME]);
6291 }
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006292}
6293
6294
6295static int phy_info_freqs(struct phy_info_arg *phy_info,
6296 struct hostapd_hw_modes *mode, struct nlattr *tb)
6297{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006298 static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
6299 [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
6300 [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006301 [NL80211_FREQUENCY_ATTR_NO_IR] = { .type = NLA_FLAG },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006302 [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
6303 [NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32 },
Dmitry Shmidtea69e842013-05-13 14:52:28 -07006304 [NL80211_FREQUENCY_ATTR_DFS_STATE] = { .type = NLA_U32 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006305 };
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006306 int new_channels = 0;
6307 struct hostapd_channel_data *channel;
6308 struct nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006309 struct nlattr *nl_freq;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006310 int rem_freq, idx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006311
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006312 if (tb == NULL)
6313 return NL_OK;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006314
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006315 nla_for_each_nested(nl_freq, tb, rem_freq) {
6316 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX,
6317 nla_data(nl_freq), nla_len(nl_freq), freq_policy);
6318 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
6319 continue;
6320 new_channels++;
6321 }
6322
6323 channel = os_realloc_array(mode->channels,
6324 mode->num_channels + new_channels,
6325 sizeof(struct hostapd_channel_data));
6326 if (!channel)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006327 return NL_SKIP;
6328
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006329 mode->channels = channel;
6330 mode->num_channels += new_channels;
6331
6332 idx = phy_info->last_chan_idx;
6333
6334 nla_for_each_nested(nl_freq, tb, rem_freq) {
6335 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX,
6336 nla_data(nl_freq), nla_len(nl_freq), freq_policy);
6337 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
6338 continue;
6339 phy_info_freq(mode, &mode->channels[idx], tb_freq);
6340 idx++;
6341 }
6342 phy_info->last_chan_idx = idx;
6343
6344 return NL_OK;
6345}
6346
6347
6348static int phy_info_rates(struct hostapd_hw_modes *mode, struct nlattr *tb)
6349{
6350 static struct nla_policy rate_policy[NL80211_BITRATE_ATTR_MAX + 1] = {
6351 [NL80211_BITRATE_ATTR_RATE] = { .type = NLA_U32 },
6352 [NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE] =
6353 { .type = NLA_FLAG },
6354 };
6355 struct nlattr *tb_rate[NL80211_BITRATE_ATTR_MAX + 1];
6356 struct nlattr *nl_rate;
6357 int rem_rate, idx;
6358
6359 if (tb == NULL)
6360 return NL_OK;
6361
6362 nla_for_each_nested(nl_rate, tb, rem_rate) {
6363 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX,
6364 nla_data(nl_rate), nla_len(nl_rate),
6365 rate_policy);
6366 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
6367 continue;
6368 mode->num_rates++;
6369 }
6370
6371 mode->rates = os_calloc(mode->num_rates, sizeof(int));
6372 if (!mode->rates)
6373 return NL_SKIP;
6374
6375 idx = 0;
6376
6377 nla_for_each_nested(nl_rate, tb, rem_rate) {
6378 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX,
6379 nla_data(nl_rate), nla_len(nl_rate),
6380 rate_policy);
6381 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
6382 continue;
6383 mode->rates[idx] = nla_get_u32(
6384 tb_rate[NL80211_BITRATE_ATTR_RATE]);
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006385 idx++;
6386 }
6387
6388 return NL_OK;
6389}
6390
6391
6392static int phy_info_band(struct phy_info_arg *phy_info, struct nlattr *nl_band)
6393{
6394 struct nlattr *tb_band[NL80211_BAND_ATTR_MAX + 1];
6395 struct hostapd_hw_modes *mode;
6396 int ret;
6397
6398 if (phy_info->last_mode != nl_band->nla_type) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006399 mode = os_realloc_array(phy_info->modes,
6400 *phy_info->num_modes + 1,
6401 sizeof(*mode));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006402 if (!mode)
6403 return NL_SKIP;
6404 phy_info->modes = mode;
6405
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006406 mode = &phy_info->modes[*(phy_info->num_modes)];
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006407 os_memset(mode, 0, sizeof(*mode));
6408 mode->mode = NUM_HOSTAPD_MODES;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006409 mode->flags = HOSTAPD_MODE_FLAG_HT_INFO_KNOWN |
6410 HOSTAPD_MODE_FLAG_VHT_INFO_KNOWN;
6411
6412 /*
6413 * Unsupported VHT MCS stream is defined as value 3, so the VHT
6414 * MCS RX/TX map must be initialized with 0xffff to mark all 8
6415 * possible streams as unsupported. This will be overridden if
6416 * driver advertises VHT support.
6417 */
6418 mode->vht_mcs_set[0] = 0xff;
6419 mode->vht_mcs_set[1] = 0xff;
6420 mode->vht_mcs_set[4] = 0xff;
6421 mode->vht_mcs_set[5] = 0xff;
6422
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006423 *(phy_info->num_modes) += 1;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006424 phy_info->last_mode = nl_band->nla_type;
6425 phy_info->last_chan_idx = 0;
6426 } else
6427 mode = &phy_info->modes[*(phy_info->num_modes) - 1];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006428
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006429 nla_parse(tb_band, NL80211_BAND_ATTR_MAX, nla_data(nl_band),
6430 nla_len(nl_band), NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006431
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006432 phy_info_ht_capa(mode, tb_band[NL80211_BAND_ATTR_HT_CAPA],
6433 tb_band[NL80211_BAND_ATTR_HT_AMPDU_FACTOR],
6434 tb_band[NL80211_BAND_ATTR_HT_AMPDU_DENSITY],
6435 tb_band[NL80211_BAND_ATTR_HT_MCS_SET]);
6436 phy_info_vht_capa(mode, tb_band[NL80211_BAND_ATTR_VHT_CAPA],
6437 tb_band[NL80211_BAND_ATTR_VHT_MCS_SET]);
6438 ret = phy_info_freqs(phy_info, mode, tb_band[NL80211_BAND_ATTR_FREQS]);
6439 if (ret != NL_OK)
6440 return ret;
6441 ret = phy_info_rates(mode, tb_band[NL80211_BAND_ATTR_RATES]);
6442 if (ret != NL_OK)
6443 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006444
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006445 return NL_OK;
6446}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006447
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006448
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006449static int phy_info_handler(struct nl_msg *msg, void *arg)
6450{
6451 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
6452 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6453 struct phy_info_arg *phy_info = arg;
6454 struct nlattr *nl_band;
6455 int rem_band;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006456
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006457 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6458 genlmsg_attrlen(gnlh, 0), NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006459
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006460 if (!tb_msg[NL80211_ATTR_WIPHY_BANDS])
6461 return NL_SKIP;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006462
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006463 nla_for_each_nested(nl_band, tb_msg[NL80211_ATTR_WIPHY_BANDS], rem_band)
6464 {
6465 int res = phy_info_band(phy_info, nl_band);
6466 if (res != NL_OK)
6467 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006468 }
6469
6470 return NL_SKIP;
6471}
6472
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006473
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006474static struct hostapd_hw_modes *
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07006475wpa_driver_nl80211_postprocess_modes(struct hostapd_hw_modes *modes,
6476 u16 *num_modes)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006477{
6478 u16 m;
6479 struct hostapd_hw_modes *mode11g = NULL, *nmodes, *mode;
6480 int i, mode11g_idx = -1;
6481
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07006482 /* heuristic to set up modes */
6483 for (m = 0; m < *num_modes; m++) {
6484 if (!modes[m].num_channels)
6485 continue;
6486 if (modes[m].channels[0].freq < 4000) {
6487 modes[m].mode = HOSTAPD_MODE_IEEE80211B;
6488 for (i = 0; i < modes[m].num_rates; i++) {
6489 if (modes[m].rates[i] > 200) {
6490 modes[m].mode = HOSTAPD_MODE_IEEE80211G;
6491 break;
6492 }
6493 }
6494 } else if (modes[m].channels[0].freq > 50000)
6495 modes[m].mode = HOSTAPD_MODE_IEEE80211AD;
6496 else
6497 modes[m].mode = HOSTAPD_MODE_IEEE80211A;
6498 }
6499
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006500 /* If only 802.11g mode is included, use it to construct matching
6501 * 802.11b mode data. */
6502
6503 for (m = 0; m < *num_modes; m++) {
6504 if (modes[m].mode == HOSTAPD_MODE_IEEE80211B)
6505 return modes; /* 802.11b already included */
6506 if (modes[m].mode == HOSTAPD_MODE_IEEE80211G)
6507 mode11g_idx = m;
6508 }
6509
6510 if (mode11g_idx < 0)
6511 return modes; /* 2.4 GHz band not supported at all */
6512
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006513 nmodes = os_realloc_array(modes, *num_modes + 1, sizeof(*nmodes));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006514 if (nmodes == NULL)
6515 return modes; /* Could not add 802.11b mode */
6516
6517 mode = &nmodes[*num_modes];
6518 os_memset(mode, 0, sizeof(*mode));
6519 (*num_modes)++;
6520 modes = nmodes;
6521
6522 mode->mode = HOSTAPD_MODE_IEEE80211B;
6523
6524 mode11g = &modes[mode11g_idx];
6525 mode->num_channels = mode11g->num_channels;
6526 mode->channels = os_malloc(mode11g->num_channels *
6527 sizeof(struct hostapd_channel_data));
6528 if (mode->channels == NULL) {
6529 (*num_modes)--;
6530 return modes; /* Could not add 802.11b mode */
6531 }
6532 os_memcpy(mode->channels, mode11g->channels,
6533 mode11g->num_channels * sizeof(struct hostapd_channel_data));
6534
6535 mode->num_rates = 0;
6536 mode->rates = os_malloc(4 * sizeof(int));
6537 if (mode->rates == NULL) {
6538 os_free(mode->channels);
6539 (*num_modes)--;
6540 return modes; /* Could not add 802.11b mode */
6541 }
6542
6543 for (i = 0; i < mode11g->num_rates; i++) {
6544 if (mode11g->rates[i] != 10 && mode11g->rates[i] != 20 &&
6545 mode11g->rates[i] != 55 && mode11g->rates[i] != 110)
6546 continue;
6547 mode->rates[mode->num_rates] = mode11g->rates[i];
6548 mode->num_rates++;
6549 if (mode->num_rates == 4)
6550 break;
6551 }
6552
6553 if (mode->num_rates == 0) {
6554 os_free(mode->channels);
6555 os_free(mode->rates);
6556 (*num_modes)--;
6557 return modes; /* No 802.11b rates */
6558 }
6559
6560 wpa_printf(MSG_DEBUG, "nl80211: Added 802.11b mode based on 802.11g "
6561 "information");
6562
6563 return modes;
6564}
6565
6566
6567static void nl80211_set_ht40_mode(struct hostapd_hw_modes *mode, int start,
6568 int end)
6569{
6570 int c;
6571
6572 for (c = 0; c < mode->num_channels; c++) {
6573 struct hostapd_channel_data *chan = &mode->channels[c];
6574 if (chan->freq - 10 >= start && chan->freq + 10 <= end)
6575 chan->flag |= HOSTAPD_CHAN_HT40;
6576 }
6577}
6578
6579
6580static void nl80211_set_ht40_mode_sec(struct hostapd_hw_modes *mode, int start,
6581 int end)
6582{
6583 int c;
6584
6585 for (c = 0; c < mode->num_channels; c++) {
6586 struct hostapd_channel_data *chan = &mode->channels[c];
6587 if (!(chan->flag & HOSTAPD_CHAN_HT40))
6588 continue;
6589 if (chan->freq - 30 >= start && chan->freq - 10 <= end)
6590 chan->flag |= HOSTAPD_CHAN_HT40MINUS;
6591 if (chan->freq + 10 >= start && chan->freq + 30 <= end)
6592 chan->flag |= HOSTAPD_CHAN_HT40PLUS;
6593 }
6594}
6595
6596
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006597static void nl80211_reg_rule_max_eirp(u32 start, u32 end, u32 max_eirp,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006598 struct phy_info_arg *results)
6599{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006600 u16 m;
6601
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006602 for (m = 0; m < *results->num_modes; m++) {
6603 int c;
6604 struct hostapd_hw_modes *mode = &results->modes[m];
6605
6606 for (c = 0; c < mode->num_channels; c++) {
6607 struct hostapd_channel_data *chan = &mode->channels[c];
6608 if ((u32) chan->freq - 10 >= start &&
6609 (u32) chan->freq + 10 <= end)
6610 chan->max_tx_power = max_eirp;
6611 }
6612 }
6613}
6614
6615
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006616static void nl80211_reg_rule_ht40(u32 start, u32 end,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006617 struct phy_info_arg *results)
6618{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006619 u16 m;
6620
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006621 for (m = 0; m < *results->num_modes; m++) {
6622 if (!(results->modes[m].ht_capab &
6623 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
6624 continue;
6625 nl80211_set_ht40_mode(&results->modes[m], start, end);
6626 }
6627}
6628
6629
6630static void nl80211_reg_rule_sec(struct nlattr *tb[],
6631 struct phy_info_arg *results)
6632{
6633 u32 start, end, max_bw;
6634 u16 m;
6635
6636 if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
6637 tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
6638 tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
6639 return;
6640
6641 start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
6642 end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
6643 max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
6644
6645 if (max_bw < 20)
6646 return;
6647
6648 for (m = 0; m < *results->num_modes; m++) {
6649 if (!(results->modes[m].ht_capab &
6650 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
6651 continue;
6652 nl80211_set_ht40_mode_sec(&results->modes[m], start, end);
6653 }
6654}
6655
6656
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006657static void nl80211_set_vht_mode(struct hostapd_hw_modes *mode, int start,
6658 int end)
6659{
6660 int c;
6661
6662 for (c = 0; c < mode->num_channels; c++) {
6663 struct hostapd_channel_data *chan = &mode->channels[c];
6664 if (chan->freq - 10 >= start && chan->freq + 70 <= end)
6665 chan->flag |= HOSTAPD_CHAN_VHT_10_70;
6666
6667 if (chan->freq - 30 >= start && chan->freq + 50 <= end)
6668 chan->flag |= HOSTAPD_CHAN_VHT_30_50;
6669
6670 if (chan->freq - 50 >= start && chan->freq + 30 <= end)
6671 chan->flag |= HOSTAPD_CHAN_VHT_50_30;
6672
6673 if (chan->freq - 70 >= start && chan->freq + 10 <= end)
6674 chan->flag |= HOSTAPD_CHAN_VHT_70_10;
6675 }
6676}
6677
6678
6679static void nl80211_reg_rule_vht(struct nlattr *tb[],
6680 struct phy_info_arg *results)
6681{
6682 u32 start, end, max_bw;
6683 u16 m;
6684
6685 if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
6686 tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
6687 tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
6688 return;
6689
6690 start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
6691 end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
6692 max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
6693
6694 if (max_bw < 80)
6695 return;
6696
6697 for (m = 0; m < *results->num_modes; m++) {
6698 if (!(results->modes[m].ht_capab &
6699 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
6700 continue;
6701 /* TODO: use a real VHT support indication */
6702 if (!results->modes[m].vht_capab)
6703 continue;
6704
6705 nl80211_set_vht_mode(&results->modes[m], start, end);
6706 }
6707}
6708
6709
Dmitry Shmidt97672262014-02-03 13:02:54 -08006710static const char * dfs_domain_name(enum nl80211_dfs_regions region)
6711{
6712 switch (region) {
6713 case NL80211_DFS_UNSET:
6714 return "DFS-UNSET";
6715 case NL80211_DFS_FCC:
6716 return "DFS-FCC";
6717 case NL80211_DFS_ETSI:
6718 return "DFS-ETSI";
6719 case NL80211_DFS_JP:
6720 return "DFS-JP";
6721 default:
6722 return "DFS-invalid";
6723 }
6724}
6725
6726
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006727static int nl80211_get_reg(struct nl_msg *msg, void *arg)
6728{
6729 struct phy_info_arg *results = arg;
6730 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
6731 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6732 struct nlattr *nl_rule;
6733 struct nlattr *tb_rule[NL80211_FREQUENCY_ATTR_MAX + 1];
6734 int rem_rule;
6735 static struct nla_policy reg_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
6736 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
6737 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
6738 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
6739 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
6740 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
6741 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
6742 };
6743
6744 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6745 genlmsg_attrlen(gnlh, 0), NULL);
6746 if (!tb_msg[NL80211_ATTR_REG_ALPHA2] ||
6747 !tb_msg[NL80211_ATTR_REG_RULES]) {
6748 wpa_printf(MSG_DEBUG, "nl80211: No regulatory information "
6749 "available");
6750 return NL_SKIP;
6751 }
6752
Dmitry Shmidt97672262014-02-03 13:02:54 -08006753 if (tb_msg[NL80211_ATTR_DFS_REGION]) {
6754 enum nl80211_dfs_regions dfs_domain;
6755 dfs_domain = nla_get_u8(tb_msg[NL80211_ATTR_DFS_REGION]);
6756 wpa_printf(MSG_DEBUG, "nl80211: Regulatory information - country=%s (%s)",
6757 (char *) nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]),
6758 dfs_domain_name(dfs_domain));
6759 } else {
6760 wpa_printf(MSG_DEBUG, "nl80211: Regulatory information - country=%s",
6761 (char *) nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]));
6762 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006763
6764 nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
6765 {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08006766 u32 start, end, max_eirp = 0, max_bw = 0, flags = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006767 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
6768 nla_data(nl_rule), nla_len(nl_rule), reg_policy);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006769 if (tb_rule[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
6770 tb_rule[NL80211_ATTR_FREQ_RANGE_END] == NULL)
6771 continue;
6772 start = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
6773 end = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
6774 if (tb_rule[NL80211_ATTR_POWER_RULE_MAX_EIRP])
6775 max_eirp = nla_get_u32(tb_rule[NL80211_ATTR_POWER_RULE_MAX_EIRP]) / 100;
6776 if (tb_rule[NL80211_ATTR_FREQ_RANGE_MAX_BW])
6777 max_bw = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08006778 if (tb_rule[NL80211_ATTR_REG_RULE_FLAGS])
6779 flags = nla_get_u32(tb_rule[NL80211_ATTR_REG_RULE_FLAGS]);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006780
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08006781 wpa_printf(MSG_DEBUG, "nl80211: %u-%u @ %u MHz %u mBm%s%s%s%s%s%s%s%s",
6782 start, end, max_bw, max_eirp,
6783 flags & NL80211_RRF_NO_OFDM ? " (no OFDM)" : "",
6784 flags & NL80211_RRF_NO_CCK ? " (no CCK)" : "",
6785 flags & NL80211_RRF_NO_INDOOR ? " (no indoor)" : "",
6786 flags & NL80211_RRF_NO_OUTDOOR ? " (no outdoor)" :
6787 "",
6788 flags & NL80211_RRF_DFS ? " (DFS)" : "",
6789 flags & NL80211_RRF_PTP_ONLY ? " (PTP only)" : "",
6790 flags & NL80211_RRF_PTMP_ONLY ? " (PTMP only)" : "",
6791 flags & NL80211_RRF_NO_IR ? " (no IR)" : "");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006792 if (max_bw >= 40)
6793 nl80211_reg_rule_ht40(start, end, results);
6794 if (tb_rule[NL80211_ATTR_POWER_RULE_MAX_EIRP])
6795 nl80211_reg_rule_max_eirp(start, end, max_eirp,
6796 results);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006797 }
6798
6799 nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
6800 {
6801 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
6802 nla_data(nl_rule), nla_len(nl_rule), reg_policy);
6803 nl80211_reg_rule_sec(tb_rule, results);
6804 }
6805
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006806 nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
6807 {
6808 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
6809 nla_data(nl_rule), nla_len(nl_rule), reg_policy);
6810 nl80211_reg_rule_vht(tb_rule, results);
6811 }
6812
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006813 return NL_SKIP;
6814}
6815
6816
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006817static int nl80211_set_regulatory_flags(struct wpa_driver_nl80211_data *drv,
6818 struct phy_info_arg *results)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006819{
6820 struct nl_msg *msg;
6821
6822 msg = nlmsg_alloc();
6823 if (!msg)
6824 return -ENOMEM;
6825
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006826 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006827 return send_and_recv_msgs(drv, msg, nl80211_get_reg, results);
6828}
6829
6830
6831static struct hostapd_hw_modes *
6832wpa_driver_nl80211_get_hw_feature_data(void *priv, u16 *num_modes, u16 *flags)
6833{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006834 u32 feat;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006835 struct i802_bss *bss = priv;
6836 struct wpa_driver_nl80211_data *drv = bss->drv;
6837 struct nl_msg *msg;
6838 struct phy_info_arg result = {
6839 .num_modes = num_modes,
6840 .modes = NULL,
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006841 .last_mode = -1,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006842 };
6843
6844 *num_modes = 0;
6845 *flags = 0;
6846
6847 msg = nlmsg_alloc();
6848 if (!msg)
6849 return NULL;
6850
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006851 feat = get_nl80211_protocol_features(drv);
6852 if (feat & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP)
6853 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_WIPHY);
6854 else
6855 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006856
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006857 NLA_PUT_FLAG(msg, NL80211_ATTR_SPLIT_WIPHY_DUMP);
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08006858 if (nl80211_set_iface_id(msg, bss) < 0)
6859 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006860
6861 if (send_and_recv_msgs(drv, msg, phy_info_handler, &result) == 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006862 nl80211_set_regulatory_flags(drv, &result);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07006863 return wpa_driver_nl80211_postprocess_modes(result.modes,
6864 num_modes);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006865 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006866 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006867 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006868 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006869 return NULL;
6870}
6871
6872
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006873static int wpa_driver_nl80211_send_mntr(struct wpa_driver_nl80211_data *drv,
6874 const void *data, size_t len,
6875 int encrypt, int noack)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006876{
6877 __u8 rtap_hdr[] = {
6878 0x00, 0x00, /* radiotap version */
6879 0x0e, 0x00, /* radiotap length */
6880 0x02, 0xc0, 0x00, 0x00, /* bmap: flags, tx and rx flags */
6881 IEEE80211_RADIOTAP_F_FRAG, /* F_FRAG (fragment if required) */
6882 0x00, /* padding */
6883 0x00, 0x00, /* RX and TX flags to indicate that */
6884 0x00, 0x00, /* this is the injected frame directly */
6885 };
6886 struct iovec iov[2] = {
6887 {
6888 .iov_base = &rtap_hdr,
6889 .iov_len = sizeof(rtap_hdr),
6890 },
6891 {
6892 .iov_base = (void *) data,
6893 .iov_len = len,
6894 }
6895 };
6896 struct msghdr msg = {
6897 .msg_name = NULL,
6898 .msg_namelen = 0,
6899 .msg_iov = iov,
6900 .msg_iovlen = 2,
6901 .msg_control = NULL,
6902 .msg_controllen = 0,
6903 .msg_flags = 0,
6904 };
6905 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006906 u16 txflags = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006907
6908 if (encrypt)
6909 rtap_hdr[8] |= IEEE80211_RADIOTAP_F_WEP;
6910
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07006911 if (drv->monitor_sock < 0) {
6912 wpa_printf(MSG_DEBUG, "nl80211: No monitor socket available "
6913 "for %s", __func__);
6914 return -1;
6915 }
6916
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006917 if (noack)
6918 txflags |= IEEE80211_RADIOTAP_F_TX_NOACK;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006919 WPA_PUT_LE16(&rtap_hdr[12], txflags);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006920
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006921 res = sendmsg(drv->monitor_sock, &msg, 0);
6922 if (res < 0) {
6923 wpa_printf(MSG_INFO, "nl80211: sendmsg: %s", strerror(errno));
6924 return -1;
6925 }
6926 return 0;
6927}
6928
6929
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006930static int wpa_driver_nl80211_send_frame(struct i802_bss *bss,
6931 const void *data, size_t len,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006932 int encrypt, int noack,
6933 unsigned int freq, int no_cck,
6934 int offchanok, unsigned int wait_time)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006935{
6936 struct wpa_driver_nl80211_data *drv = bss->drv;
6937 u64 cookie;
Dmitry Shmidt051af732013-10-22 13:52:46 -07006938 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006939
Dmitry Shmidt56052862013-10-04 10:23:25 -07006940 if (freq == 0) {
6941 wpa_printf(MSG_DEBUG, "nl80211: send_frame - Use bss->freq=%u",
6942 bss->freq);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006943 freq = bss->freq;
Dmitry Shmidt56052862013-10-04 10:23:25 -07006944 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006945
Dmitry Shmidt56052862013-10-04 10:23:25 -07006946 if (drv->use_monitor) {
6947 wpa_printf(MSG_DEBUG, "nl80211: send_frame(freq=%u bss->freq=%u) -> send_mntr",
6948 freq, bss->freq);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006949 return wpa_driver_nl80211_send_mntr(drv, data, len,
6950 encrypt, noack);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006951 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006952
Dmitry Shmidt56052862013-10-04 10:23:25 -07006953 wpa_printf(MSG_DEBUG, "nl80211: send_frame -> send_frame_cmd");
Dmitry Shmidt051af732013-10-22 13:52:46 -07006954 res = nl80211_send_frame_cmd(bss, freq, wait_time, data, len,
6955 &cookie, no_cck, noack, offchanok);
6956 if (res == 0 && !noack) {
6957 const struct ieee80211_mgmt *mgmt;
6958 u16 fc;
6959
6960 mgmt = (const struct ieee80211_mgmt *) data;
6961 fc = le_to_host16(mgmt->frame_control);
6962 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
6963 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_ACTION) {
6964 wpa_printf(MSG_MSGDUMP,
6965 "nl80211: Update send_action_cookie from 0x%llx to 0x%llx",
6966 (long long unsigned int)
6967 drv->send_action_cookie,
6968 (long long unsigned int) cookie);
6969 drv->send_action_cookie = cookie;
6970 }
6971 }
6972
6973 return res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006974}
6975
6976
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006977static int wpa_driver_nl80211_send_mlme(struct i802_bss *bss, const u8 *data,
6978 size_t data_len, int noack,
6979 unsigned int freq, int no_cck,
6980 int offchanok,
6981 unsigned int wait_time)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006982{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006983 struct wpa_driver_nl80211_data *drv = bss->drv;
6984 struct ieee80211_mgmt *mgmt;
6985 int encrypt = 1;
6986 u16 fc;
6987
6988 mgmt = (struct ieee80211_mgmt *) data;
6989 fc = le_to_host16(mgmt->frame_control);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006990 wpa_printf(MSG_DEBUG, "nl80211: send_mlme - noack=%d freq=%u no_cck=%d offchanok=%d wait_time=%u fc=0x%x nlmode=%d",
6991 noack, freq, no_cck, offchanok, wait_time, fc, drv->nlmode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006992
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006993 if ((is_sta_interface(drv->nlmode) ||
6994 drv->nlmode == NL80211_IFTYPE_P2P_DEVICE) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006995 WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
6996 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_RESP) {
6997 /*
6998 * The use of last_mgmt_freq is a bit of a hack,
6999 * but it works due to the single-threaded nature
7000 * of wpa_supplicant.
7001 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07007002 if (freq == 0) {
7003 wpa_printf(MSG_DEBUG, "nl80211: Use last_mgmt_freq=%d",
7004 drv->last_mgmt_freq);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007005 freq = drv->last_mgmt_freq;
Dmitry Shmidt56052862013-10-04 10:23:25 -07007006 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007007 return nl80211_send_frame_cmd(bss, freq, 0,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007008 data, data_len, NULL, 1, noack,
7009 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007010 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007011
7012 if (drv->device_ap_sme && is_ap_interface(drv->nlmode)) {
Dmitry Shmidt56052862013-10-04 10:23:25 -07007013 if (freq == 0) {
7014 wpa_printf(MSG_DEBUG, "nl80211: Use bss->freq=%d",
7015 bss->freq);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007016 freq = bss->freq;
Dmitry Shmidt56052862013-10-04 10:23:25 -07007017 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07007018 return nl80211_send_frame_cmd(bss, freq,
7019 (int) freq == bss->freq ? 0 :
7020 wait_time,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007021 data, data_len,
7022 &drv->send_action_cookie,
7023 no_cck, noack, offchanok);
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07007024 }
Dmitry Shmidtb638fe72012-03-20 12:51:25 -07007025
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007026 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
7027 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) {
7028 /*
7029 * Only one of the authentication frame types is encrypted.
7030 * In order for static WEP encryption to work properly (i.e.,
7031 * to not encrypt the frame), we need to tell mac80211 about
7032 * the frames that must not be encrypted.
7033 */
7034 u16 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
7035 u16 auth_trans = le_to_host16(mgmt->u.auth.auth_transaction);
7036 if (auth_alg != WLAN_AUTH_SHARED_KEY || auth_trans != 3)
7037 encrypt = 0;
7038 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007039
Dmitry Shmidt56052862013-10-04 10:23:25 -07007040 wpa_printf(MSG_DEBUG, "nl80211: send_mlme -> send_frame");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007041 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007042 noack, freq, no_cck, offchanok,
7043 wait_time);
7044}
7045
7046
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007047static int nl80211_set_bss(struct i802_bss *bss, int cts, int preamble,
7048 int slot, int ht_opmode, int ap_isolate,
7049 int *basic_rates)
7050{
7051 struct wpa_driver_nl80211_data *drv = bss->drv;
7052 struct nl_msg *msg;
7053
7054 msg = nlmsg_alloc();
7055 if (!msg)
7056 return -ENOMEM;
7057
7058 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_BSS);
7059
7060 if (cts >= 0)
7061 NLA_PUT_U8(msg, NL80211_ATTR_BSS_CTS_PROT, cts);
7062 if (preamble >= 0)
7063 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble);
7064 if (slot >= 0)
7065 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot);
7066 if (ht_opmode >= 0)
7067 NLA_PUT_U16(msg, NL80211_ATTR_BSS_HT_OPMODE, ht_opmode);
7068 if (ap_isolate >= 0)
7069 NLA_PUT_U8(msg, NL80211_ATTR_AP_ISOLATE, ap_isolate);
7070
7071 if (basic_rates) {
7072 u8 rates[NL80211_MAX_SUPP_RATES];
7073 u8 rates_len = 0;
7074 int i;
7075
7076 for (i = 0; i < NL80211_MAX_SUPP_RATES && basic_rates[i] >= 0;
7077 i++)
7078 rates[rates_len++] = basic_rates[i] / 5;
7079
7080 NLA_PUT(msg, NL80211_ATTR_BSS_BASIC_RATES, rates_len, rates);
7081 }
7082
7083 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
7084
7085 return send_and_recv_msgs(drv, msg, NULL, NULL);
7086 nla_put_failure:
7087 nlmsg_free(msg);
7088 return -ENOBUFS;
7089}
7090
7091
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07007092static int wpa_driver_nl80211_set_acl(void *priv,
7093 struct hostapd_acl_params *params)
7094{
7095 struct i802_bss *bss = priv;
7096 struct wpa_driver_nl80211_data *drv = bss->drv;
7097 struct nl_msg *msg;
7098 struct nlattr *acl;
7099 unsigned int i;
7100 int ret = 0;
7101
7102 if (!(drv->capa.max_acl_mac_addrs))
7103 return -ENOTSUP;
7104
7105 if (params->num_mac_acl > drv->capa.max_acl_mac_addrs)
7106 return -ENOTSUP;
7107
7108 msg = nlmsg_alloc();
7109 if (!msg)
7110 return -ENOMEM;
7111
7112 wpa_printf(MSG_DEBUG, "nl80211: Set %s ACL (num_mac_acl=%u)",
7113 params->acl_policy ? "Accept" : "Deny", params->num_mac_acl);
7114
7115 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_MAC_ACL);
7116
7117 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
7118
7119 NLA_PUT_U32(msg, NL80211_ATTR_ACL_POLICY, params->acl_policy ?
7120 NL80211_ACL_POLICY_DENY_UNLESS_LISTED :
7121 NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED);
7122
7123 acl = nla_nest_start(msg, NL80211_ATTR_MAC_ADDRS);
7124 if (acl == NULL)
7125 goto nla_put_failure;
7126
7127 for (i = 0; i < params->num_mac_acl; i++)
7128 NLA_PUT(msg, i + 1, ETH_ALEN, params->mac_acl[i].addr);
7129
7130 nla_nest_end(msg, acl);
7131
7132 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7133 msg = NULL;
7134 if (ret) {
7135 wpa_printf(MSG_DEBUG, "nl80211: Failed to set MAC ACL: %d (%s)",
7136 ret, strerror(-ret));
7137 }
7138
7139nla_put_failure:
7140 nlmsg_free(msg);
7141
7142 return ret;
7143}
7144
7145
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007146static int wpa_driver_nl80211_set_ap(void *priv,
7147 struct wpa_driver_ap_params *params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007148{
7149 struct i802_bss *bss = priv;
7150 struct wpa_driver_nl80211_data *drv = bss->drv;
7151 struct nl_msg *msg;
7152 u8 cmd = NL80211_CMD_NEW_BEACON;
7153 int ret;
7154 int beacon_set;
7155 int ifindex = if_nametoindex(bss->ifname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007156 int num_suites;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007157 u32 suites[10], suite;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007158 u32 ver;
7159
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07007160 beacon_set = bss->beacon_set;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007161
7162 msg = nlmsg_alloc();
7163 if (!msg)
7164 return -ENOMEM;
7165
7166 wpa_printf(MSG_DEBUG, "nl80211: Set beacon (beacon_set=%d)",
7167 beacon_set);
7168 if (beacon_set)
7169 cmd = NL80211_CMD_SET_BEACON;
7170
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007171 nl80211_cmd(drv, msg, 0, cmd);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007172 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon head",
7173 params->head, params->head_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007174 NLA_PUT(msg, NL80211_ATTR_BEACON_HEAD, params->head_len, params->head);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007175 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon tail",
7176 params->tail, params->tail_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007177 NLA_PUT(msg, NL80211_ATTR_BEACON_TAIL, params->tail_len, params->tail);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007178 wpa_printf(MSG_DEBUG, "nl80211: ifindex=%d", ifindex);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007179 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007180 wpa_printf(MSG_DEBUG, "nl80211: beacon_int=%d", params->beacon_int);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007181 NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL, params->beacon_int);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007182 wpa_printf(MSG_DEBUG, "nl80211: dtim_period=%d", params->dtim_period);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007183 NLA_PUT_U32(msg, NL80211_ATTR_DTIM_PERIOD, params->dtim_period);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007184 wpa_hexdump_ascii(MSG_DEBUG, "nl80211: ssid",
7185 params->ssid, params->ssid_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007186 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
7187 params->ssid);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007188 if (params->proberesp && params->proberesp_len) {
7189 wpa_hexdump(MSG_DEBUG, "nl80211: proberesp (offload)",
7190 params->proberesp, params->proberesp_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007191 NLA_PUT(msg, NL80211_ATTR_PROBE_RESP, params->proberesp_len,
7192 params->proberesp);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007193 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007194 switch (params->hide_ssid) {
7195 case NO_SSID_HIDING:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007196 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID not in use");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007197 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
7198 NL80211_HIDDEN_SSID_NOT_IN_USE);
7199 break;
7200 case HIDDEN_SSID_ZERO_LEN:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007201 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero len");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007202 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
7203 NL80211_HIDDEN_SSID_ZERO_LEN);
7204 break;
7205 case HIDDEN_SSID_ZERO_CONTENTS:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007206 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero contents");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007207 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
7208 NL80211_HIDDEN_SSID_ZERO_CONTENTS);
7209 break;
7210 }
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007211 wpa_printf(MSG_DEBUG, "nl80211: privacy=%d", params->privacy);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007212 if (params->privacy)
7213 NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007214 wpa_printf(MSG_DEBUG, "nl80211: auth_algs=0x%x", params->auth_algs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007215 if ((params->auth_algs & (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) ==
7216 (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) {
7217 /* Leave out the attribute */
7218 } else if (params->auth_algs & WPA_AUTH_ALG_SHARED)
7219 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE,
7220 NL80211_AUTHTYPE_SHARED_KEY);
7221 else
7222 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE,
7223 NL80211_AUTHTYPE_OPEN_SYSTEM);
7224
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007225 wpa_printf(MSG_DEBUG, "nl80211: wpa_version=0x%x", params->wpa_version);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007226 ver = 0;
7227 if (params->wpa_version & WPA_PROTO_WPA)
7228 ver |= NL80211_WPA_VERSION_1;
7229 if (params->wpa_version & WPA_PROTO_RSN)
7230 ver |= NL80211_WPA_VERSION_2;
7231 if (ver)
7232 NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver);
7233
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007234 wpa_printf(MSG_DEBUG, "nl80211: key_mgmt_suites=0x%x",
7235 params->key_mgmt_suites);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007236 num_suites = 0;
7237 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X)
7238 suites[num_suites++] = WLAN_AKM_SUITE_8021X;
7239 if (params->key_mgmt_suites & WPA_KEY_MGMT_PSK)
7240 suites[num_suites++] = WLAN_AKM_SUITE_PSK;
7241 if (num_suites) {
7242 NLA_PUT(msg, NL80211_ATTR_AKM_SUITES,
7243 num_suites * sizeof(u32), suites);
7244 }
7245
7246 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X &&
7247 params->pairwise_ciphers & (WPA_CIPHER_WEP104 | WPA_CIPHER_WEP40))
7248 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT);
7249
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007250 wpa_printf(MSG_DEBUG, "nl80211: pairwise_ciphers=0x%x",
7251 params->pairwise_ciphers);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007252 num_suites = wpa_cipher_to_cipher_suites(params->pairwise_ciphers,
7253 suites, ARRAY_SIZE(suites));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007254 if (num_suites) {
7255 NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
7256 num_suites * sizeof(u32), suites);
7257 }
7258
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007259 wpa_printf(MSG_DEBUG, "nl80211: group_cipher=0x%x",
7260 params->group_cipher);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007261 suite = wpa_cipher_to_cipher_suite(params->group_cipher);
7262 if (suite)
7263 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, suite);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007264
7265 if (params->beacon_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007266 wpa_hexdump_buf(MSG_DEBUG, "nl80211: beacon_ies",
7267 params->beacon_ies);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007268 NLA_PUT(msg, NL80211_ATTR_IE, wpabuf_len(params->beacon_ies),
7269 wpabuf_head(params->beacon_ies));
7270 }
7271 if (params->proberesp_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007272 wpa_hexdump_buf(MSG_DEBUG, "nl80211: proberesp_ies",
7273 params->proberesp_ies);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007274 NLA_PUT(msg, NL80211_ATTR_IE_PROBE_RESP,
7275 wpabuf_len(params->proberesp_ies),
7276 wpabuf_head(params->proberesp_ies));
7277 }
7278 if (params->assocresp_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007279 wpa_hexdump_buf(MSG_DEBUG, "nl80211: assocresp_ies",
7280 params->assocresp_ies);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007281 NLA_PUT(msg, NL80211_ATTR_IE_ASSOC_RESP,
7282 wpabuf_len(params->assocresp_ies),
7283 wpabuf_head(params->assocresp_ies));
7284 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007285
Dmitry Shmidt04949592012-07-19 12:16:46 -07007286 if (drv->capa.flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007287 wpa_printf(MSG_DEBUG, "nl80211: ap_max_inactivity=%d",
7288 params->ap_max_inactivity);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007289 NLA_PUT_U16(msg, NL80211_ATTR_INACTIVITY_TIMEOUT,
7290 params->ap_max_inactivity);
7291 }
7292
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007293 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7294 if (ret) {
7295 wpa_printf(MSG_DEBUG, "nl80211: Beacon set failed: %d (%s)",
7296 ret, strerror(-ret));
7297 } else {
7298 bss->beacon_set = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007299 nl80211_set_bss(bss, params->cts_protect, params->preamble,
7300 params->short_slot_time, params->ht_opmode,
7301 params->isolate, params->basic_rates);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007302 }
7303 return ret;
7304 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007305 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007306 return -ENOBUFS;
7307}
7308
7309
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007310static int nl80211_put_freq_params(struct nl_msg *msg,
7311 struct hostapd_freq_params *freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007312{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007313 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq->freq);
7314 if (freq->vht_enabled) {
7315 switch (freq->bandwidth) {
7316 case 20:
7317 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
7318 NL80211_CHAN_WIDTH_20);
7319 break;
7320 case 40:
7321 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
7322 NL80211_CHAN_WIDTH_40);
7323 break;
7324 case 80:
7325 if (freq->center_freq2)
7326 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
7327 NL80211_CHAN_WIDTH_80P80);
7328 else
7329 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
7330 NL80211_CHAN_WIDTH_80);
7331 break;
7332 case 160:
7333 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
7334 NL80211_CHAN_WIDTH_160);
7335 break;
7336 default:
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007337 return -EINVAL;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007338 }
7339 NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ1, freq->center_freq1);
7340 if (freq->center_freq2)
7341 NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ2,
7342 freq->center_freq2);
7343 } else if (freq->ht_enabled) {
7344 switch (freq->sec_channel_offset) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007345 case -1:
7346 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
7347 NL80211_CHAN_HT40MINUS);
7348 break;
7349 case 1:
7350 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
7351 NL80211_CHAN_HT40PLUS);
7352 break;
7353 default:
7354 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
7355 NL80211_CHAN_HT20);
7356 break;
7357 }
7358 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007359 return 0;
7360
7361nla_put_failure:
7362 return -ENOBUFS;
7363}
7364
7365
7366static int wpa_driver_nl80211_set_freq(struct i802_bss *bss,
7367 struct hostapd_freq_params *freq)
7368{
7369 struct wpa_driver_nl80211_data *drv = bss->drv;
7370 struct nl_msg *msg;
7371 int ret;
7372
7373 wpa_printf(MSG_DEBUG,
7374 "nl80211: Set freq %d (ht_enabled=%d, vht_enabled=%d, bandwidth=%d MHz, cf1=%d MHz, cf2=%d MHz)",
7375 freq->freq, freq->ht_enabled, freq->vht_enabled,
7376 freq->bandwidth, freq->center_freq1, freq->center_freq2);
7377 msg = nlmsg_alloc();
7378 if (!msg)
7379 return -1;
7380
7381 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
7382
7383 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
7384 if (nl80211_put_freq_params(msg, freq) < 0)
7385 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007386
7387 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007388 msg = NULL;
7389 if (ret == 0) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007390 bss->freq = freq->freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007391 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007392 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007393 wpa_printf(MSG_DEBUG, "nl80211: Failed to set channel (freq=%d): "
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007394 "%d (%s)", freq->freq, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007395nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007396 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007397 return -1;
7398}
7399
7400
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007401static u32 sta_flags_nl80211(int flags)
7402{
7403 u32 f = 0;
7404
7405 if (flags & WPA_STA_AUTHORIZED)
7406 f |= BIT(NL80211_STA_FLAG_AUTHORIZED);
7407 if (flags & WPA_STA_WMM)
7408 f |= BIT(NL80211_STA_FLAG_WME);
7409 if (flags & WPA_STA_SHORT_PREAMBLE)
7410 f |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
7411 if (flags & WPA_STA_MFP)
7412 f |= BIT(NL80211_STA_FLAG_MFP);
7413 if (flags & WPA_STA_TDLS_PEER)
7414 f |= BIT(NL80211_STA_FLAG_TDLS_PEER);
7415
7416 return f;
7417}
7418
7419
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007420static int wpa_driver_nl80211_sta_add(void *priv,
7421 struct hostapd_sta_add_params *params)
7422{
7423 struct i802_bss *bss = priv;
7424 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007425 struct nl_msg *msg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007426 struct nl80211_sta_flag_update upd;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007427 int ret = -ENOBUFS;
7428
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007429 if ((params->flags & WPA_STA_TDLS_PEER) &&
7430 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
7431 return -EOPNOTSUPP;
7432
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007433 msg = nlmsg_alloc();
7434 if (!msg)
7435 return -ENOMEM;
7436
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007437 wpa_printf(MSG_DEBUG, "nl80211: %s STA " MACSTR,
7438 params->set ? "Set" : "Add", MAC2STR(params->addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007439 nl80211_cmd(drv, msg, 0, params->set ? NL80211_CMD_SET_STATION :
7440 NL80211_CMD_NEW_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007441
7442 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
7443 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007444 NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_RATES, params->supp_rates_len,
7445 params->supp_rates);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007446 wpa_hexdump(MSG_DEBUG, " * supported rates", params->supp_rates,
7447 params->supp_rates_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007448 if (!params->set) {
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07007449 if (params->aid) {
7450 wpa_printf(MSG_DEBUG, " * aid=%u", params->aid);
7451 NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, params->aid);
7452 } else {
7453 /*
7454 * cfg80211 validates that AID is non-zero, so we have
7455 * to make this a non-zero value for the TDLS case where
7456 * a dummy STA entry is used for now.
7457 */
7458 wpa_printf(MSG_DEBUG, " * aid=1 (TDLS workaround)");
7459 NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, 1);
7460 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007461 wpa_printf(MSG_DEBUG, " * listen_interval=%u",
7462 params->listen_interval);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007463 NLA_PUT_U16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
7464 params->listen_interval);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07007465 } else if (params->aid && (params->flags & WPA_STA_TDLS_PEER)) {
7466 wpa_printf(MSG_DEBUG, " * peer_aid=%u", params->aid);
7467 NLA_PUT_U16(msg, NL80211_ATTR_PEER_AID, params->aid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007468 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007469 if (params->ht_capabilities) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007470 wpa_hexdump(MSG_DEBUG, " * ht_capabilities",
7471 (u8 *) params->ht_capabilities,
7472 sizeof(*params->ht_capabilities));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007473 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY,
7474 sizeof(*params->ht_capabilities),
7475 params->ht_capabilities);
7476 }
7477
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007478 if (params->vht_capabilities) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007479 wpa_hexdump(MSG_DEBUG, " * vht_capabilities",
7480 (u8 *) params->vht_capabilities,
7481 sizeof(*params->vht_capabilities));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007482 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY,
7483 sizeof(*params->vht_capabilities),
7484 params->vht_capabilities);
7485 }
7486
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08007487 if (params->vht_opmode_enabled) {
7488 wpa_printf(MSG_DEBUG, " * opmode=%u", params->vht_opmode);
7489 NLA_PUT_U8(msg, NL80211_ATTR_OPMODE_NOTIF,
7490 params->vht_opmode);
7491 }
7492
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007493 wpa_printf(MSG_DEBUG, " * capability=0x%x", params->capability);
7494 NLA_PUT_U16(msg, NL80211_ATTR_STA_CAPABILITY, params->capability);
7495
7496 if (params->ext_capab) {
7497 wpa_hexdump(MSG_DEBUG, " * ext_capab",
7498 params->ext_capab, params->ext_capab_len);
7499 NLA_PUT(msg, NL80211_ATTR_STA_EXT_CAPABILITY,
7500 params->ext_capab_len, params->ext_capab);
7501 }
7502
Dmitry Shmidt344abd32014-01-14 13:17:00 -08007503 if (params->supp_channels) {
7504 wpa_hexdump(MSG_DEBUG, " * supported channels",
7505 params->supp_channels, params->supp_channels_len);
7506 NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_CHANNELS,
7507 params->supp_channels_len, params->supp_channels);
7508 }
7509
7510 if (params->supp_oper_classes) {
7511 wpa_hexdump(MSG_DEBUG, " * supported operating classes",
7512 params->supp_oper_classes,
7513 params->supp_oper_classes_len);
7514 NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES,
7515 params->supp_oper_classes_len,
7516 params->supp_oper_classes);
7517 }
7518
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007519 os_memset(&upd, 0, sizeof(upd));
7520 upd.mask = sta_flags_nl80211(params->flags);
7521 upd.set = upd.mask;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007522 wpa_printf(MSG_DEBUG, " * flags set=0x%x mask=0x%x",
7523 upd.set, upd.mask);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007524 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
7525
7526 if (params->flags & WPA_STA_WMM) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007527 struct nlattr *wme = nla_nest_start(msg, NL80211_ATTR_STA_WME);
7528
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007529 if (!wme)
7530 goto nla_put_failure;
7531
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007532 wpa_printf(MSG_DEBUG, " * qosinfo=0x%x", params->qosinfo);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007533 NLA_PUT_U8(msg, NL80211_STA_WME_UAPSD_QUEUES,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007534 params->qosinfo & WMM_QOSINFO_STA_AC_MASK);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007535 NLA_PUT_U8(msg, NL80211_STA_WME_MAX_SP,
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007536 (params->qosinfo >> WMM_QOSINFO_STA_SP_SHIFT) &
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007537 WMM_QOSINFO_STA_SP_MASK);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007538 nla_nest_end(msg, wme);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007539 }
7540
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007541 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007542 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007543 if (ret)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007544 wpa_printf(MSG_DEBUG, "nl80211: NL80211_CMD_%s_STATION "
7545 "result: %d (%s)", params->set ? "SET" : "NEW", ret,
7546 strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007547 if (ret == -EEXIST)
7548 ret = 0;
7549 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007550 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007551 return ret;
7552}
7553
7554
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007555static int wpa_driver_nl80211_sta_remove(struct i802_bss *bss, const u8 *addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007556{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007557 struct wpa_driver_nl80211_data *drv = bss->drv;
7558 struct nl_msg *msg;
7559 int ret;
7560
7561 msg = nlmsg_alloc();
7562 if (!msg)
7563 return -ENOMEM;
7564
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007565 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007566
7567 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
7568 if_nametoindex(bss->ifname));
7569 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
7570
7571 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007572 wpa_printf(MSG_DEBUG, "nl80211: sta_remove -> DEL_STATION %s " MACSTR
7573 " --> %d (%s)",
7574 bss->ifname, MAC2STR(addr), ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007575 if (ret == -ENOENT)
7576 return 0;
7577 return ret;
7578 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007579 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007580 return -ENOBUFS;
7581}
7582
7583
7584static void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv,
7585 int ifidx)
7586{
7587 struct nl_msg *msg;
7588
7589 wpa_printf(MSG_DEBUG, "nl80211: Remove interface ifindex=%d", ifidx);
7590
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007591 /* stop listening for EAPOL on this interface */
7592 del_ifidx(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007593
7594 msg = nlmsg_alloc();
7595 if (!msg)
7596 goto nla_put_failure;
7597
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007598 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_INTERFACE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007599 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifidx);
7600
7601 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
7602 return;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007603 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007604 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007605 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007606 wpa_printf(MSG_ERROR, "Failed to remove interface (ifidx=%d)", ifidx);
7607}
7608
7609
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007610static const char * nl80211_iftype_str(enum nl80211_iftype mode)
7611{
7612 switch (mode) {
7613 case NL80211_IFTYPE_ADHOC:
7614 return "ADHOC";
7615 case NL80211_IFTYPE_STATION:
7616 return "STATION";
7617 case NL80211_IFTYPE_AP:
7618 return "AP";
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07007619 case NL80211_IFTYPE_AP_VLAN:
7620 return "AP_VLAN";
7621 case NL80211_IFTYPE_WDS:
7622 return "WDS";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007623 case NL80211_IFTYPE_MONITOR:
7624 return "MONITOR";
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07007625 case NL80211_IFTYPE_MESH_POINT:
7626 return "MESH_POINT";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007627 case NL80211_IFTYPE_P2P_CLIENT:
7628 return "P2P_CLIENT";
7629 case NL80211_IFTYPE_P2P_GO:
7630 return "P2P_GO";
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007631 case NL80211_IFTYPE_P2P_DEVICE:
7632 return "P2P_DEVICE";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007633 default:
7634 return "unknown";
7635 }
7636}
7637
7638
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007639static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv,
7640 const char *ifname,
7641 enum nl80211_iftype iftype,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007642 const u8 *addr, int wds,
7643 int (*handler)(struct nl_msg *, void *),
7644 void *arg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007645{
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007646 struct nl_msg *msg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007647 int ifidx;
7648 int ret = -ENOBUFS;
7649
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007650 wpa_printf(MSG_DEBUG, "nl80211: Create interface iftype %d (%s)",
7651 iftype, nl80211_iftype_str(iftype));
7652
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007653 msg = nlmsg_alloc();
7654 if (!msg)
7655 return -1;
7656
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007657 nl80211_cmd(drv, msg, 0, NL80211_CMD_NEW_INTERFACE);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007658 if (nl80211_set_iface_id(msg, drv->first_bss) < 0)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007659 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007660 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, ifname);
7661 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, iftype);
7662
7663 if (iftype == NL80211_IFTYPE_MONITOR) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007664 struct nlattr *flags;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007665
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007666 flags = nla_nest_start(msg, NL80211_ATTR_MNTR_FLAGS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007667 if (!flags)
7668 goto nla_put_failure;
7669
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007670 NLA_PUT_FLAG(msg, NL80211_MNTR_FLAG_COOK_FRAMES);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007671
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007672 nla_nest_end(msg, flags);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007673 } else if (wds) {
7674 NLA_PUT_U8(msg, NL80211_ATTR_4ADDR, wds);
7675 }
7676
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007677 ret = send_and_recv_msgs(drv, msg, handler, arg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007678 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007679 if (ret) {
7680 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007681 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007682 wpa_printf(MSG_ERROR, "Failed to create interface %s: %d (%s)",
7683 ifname, ret, strerror(-ret));
7684 return ret;
7685 }
7686
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007687 if (iftype == NL80211_IFTYPE_P2P_DEVICE)
7688 return 0;
7689
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007690 ifidx = if_nametoindex(ifname);
7691 wpa_printf(MSG_DEBUG, "nl80211: New interface %s created: ifindex=%d",
7692 ifname, ifidx);
7693
7694 if (ifidx <= 0)
7695 return -1;
7696
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007697 /* start listening for EAPOL on this interface */
7698 add_ifidx(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007699
7700 if (addr && iftype != NL80211_IFTYPE_MONITOR &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007701 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname, addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007702 nl80211_remove_iface(drv, ifidx);
7703 return -1;
7704 }
7705
7706 return ifidx;
7707}
7708
7709
7710static int nl80211_create_iface(struct wpa_driver_nl80211_data *drv,
7711 const char *ifname, enum nl80211_iftype iftype,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007712 const u8 *addr, int wds,
7713 int (*handler)(struct nl_msg *, void *),
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007714 void *arg, int use_existing)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007715{
7716 int ret;
7717
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007718 ret = nl80211_create_iface_once(drv, ifname, iftype, addr, wds, handler,
7719 arg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007720
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007721 /* if error occurred and interface exists already */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007722 if (ret == -ENFILE && if_nametoindex(ifname)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007723 if (use_existing) {
7724 wpa_printf(MSG_DEBUG, "nl80211: Continue using existing interface %s",
7725 ifname);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07007726 if (addr && iftype != NL80211_IFTYPE_MONITOR &&
7727 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
7728 addr) < 0 &&
7729 (linux_set_iface_flags(drv->global->ioctl_sock,
7730 ifname, 0) < 0 ||
7731 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
7732 addr) < 0 ||
7733 linux_set_iface_flags(drv->global->ioctl_sock,
7734 ifname, 1) < 0))
7735 return -1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007736 return -ENFILE;
7737 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007738 wpa_printf(MSG_INFO, "Try to remove and re-create %s", ifname);
7739
7740 /* Try to remove the interface that was already there. */
7741 nl80211_remove_iface(drv, if_nametoindex(ifname));
7742
7743 /* Try to create the interface again */
7744 ret = nl80211_create_iface_once(drv, ifname, iftype, addr,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007745 wds, handler, arg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007746 }
7747
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007748 if (ret >= 0 && is_p2p_net_interface(iftype))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007749 nl80211_disable_11b_rates(drv, ret, 1);
7750
7751 return ret;
7752}
7753
7754
7755static void handle_tx_callback(void *ctx, u8 *buf, size_t len, int ok)
7756{
7757 struct ieee80211_hdr *hdr;
7758 u16 fc;
7759 union wpa_event_data event;
7760
7761 hdr = (struct ieee80211_hdr *) buf;
7762 fc = le_to_host16(hdr->frame_control);
7763
7764 os_memset(&event, 0, sizeof(event));
7765 event.tx_status.type = WLAN_FC_GET_TYPE(fc);
7766 event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
7767 event.tx_status.dst = hdr->addr1;
7768 event.tx_status.data = buf;
7769 event.tx_status.data_len = len;
7770 event.tx_status.ack = ok;
7771 wpa_supplicant_event(ctx, EVENT_TX_STATUS, &event);
7772}
7773
7774
7775static void from_unknown_sta(struct wpa_driver_nl80211_data *drv,
7776 u8 *buf, size_t len)
7777{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007778 struct ieee80211_hdr *hdr = (void *)buf;
7779 u16 fc;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007780 union wpa_event_data event;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007781
7782 if (len < sizeof(*hdr))
7783 return;
7784
7785 fc = le_to_host16(hdr->frame_control);
7786
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007787 os_memset(&event, 0, sizeof(event));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007788 event.rx_from_unknown.bssid = get_hdr_bssid(hdr, len);
7789 event.rx_from_unknown.addr = hdr->addr2;
7790 event.rx_from_unknown.wds = (fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) ==
7791 (WLAN_FC_FROMDS | WLAN_FC_TODS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007792 wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event);
7793}
7794
7795
7796static void handle_frame(struct wpa_driver_nl80211_data *drv,
7797 u8 *buf, size_t len, int datarate, int ssi_signal)
7798{
7799 struct ieee80211_hdr *hdr;
7800 u16 fc;
7801 union wpa_event_data event;
7802
7803 hdr = (struct ieee80211_hdr *) buf;
7804 fc = le_to_host16(hdr->frame_control);
7805
7806 switch (WLAN_FC_GET_TYPE(fc)) {
7807 case WLAN_FC_TYPE_MGMT:
7808 os_memset(&event, 0, sizeof(event));
7809 event.rx_mgmt.frame = buf;
7810 event.rx_mgmt.frame_len = len;
7811 event.rx_mgmt.datarate = datarate;
7812 event.rx_mgmt.ssi_signal = ssi_signal;
7813 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
7814 break;
7815 case WLAN_FC_TYPE_CTRL:
7816 /* can only get here with PS-Poll frames */
7817 wpa_printf(MSG_DEBUG, "CTRL");
7818 from_unknown_sta(drv, buf, len);
7819 break;
7820 case WLAN_FC_TYPE_DATA:
7821 from_unknown_sta(drv, buf, len);
7822 break;
7823 }
7824}
7825
7826
7827static void handle_monitor_read(int sock, void *eloop_ctx, void *sock_ctx)
7828{
7829 struct wpa_driver_nl80211_data *drv = eloop_ctx;
7830 int len;
7831 unsigned char buf[3000];
7832 struct ieee80211_radiotap_iterator iter;
7833 int ret;
7834 int datarate = 0, ssi_signal = 0;
7835 int injected = 0, failed = 0, rxflags = 0;
7836
7837 len = recv(sock, buf, sizeof(buf), 0);
7838 if (len < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007839 wpa_printf(MSG_ERROR, "nl80211: Monitor socket recv failed: %s",
7840 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007841 return;
7842 }
7843
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07007844 if (ieee80211_radiotap_iterator_init(&iter, (void*)buf, len, NULL)) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007845 wpa_printf(MSG_INFO, "nl80211: received invalid radiotap frame");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007846 return;
7847 }
7848
7849 while (1) {
7850 ret = ieee80211_radiotap_iterator_next(&iter);
7851 if (ret == -ENOENT)
7852 break;
7853 if (ret) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007854 wpa_printf(MSG_INFO, "nl80211: received invalid radiotap frame (%d)",
7855 ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007856 return;
7857 }
7858 switch (iter.this_arg_index) {
7859 case IEEE80211_RADIOTAP_FLAGS:
7860 if (*iter.this_arg & IEEE80211_RADIOTAP_F_FCS)
7861 len -= 4;
7862 break;
7863 case IEEE80211_RADIOTAP_RX_FLAGS:
7864 rxflags = 1;
7865 break;
7866 case IEEE80211_RADIOTAP_TX_FLAGS:
7867 injected = 1;
7868 failed = le_to_host16((*(uint16_t *) iter.this_arg)) &
7869 IEEE80211_RADIOTAP_F_TX_FAIL;
7870 break;
7871 case IEEE80211_RADIOTAP_DATA_RETRIES:
7872 break;
7873 case IEEE80211_RADIOTAP_CHANNEL:
7874 /* TODO: convert from freq/flags to channel number */
7875 break;
7876 case IEEE80211_RADIOTAP_RATE:
7877 datarate = *iter.this_arg * 5;
7878 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007879 case IEEE80211_RADIOTAP_DBM_ANTSIGNAL:
7880 ssi_signal = (s8) *iter.this_arg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007881 break;
7882 }
7883 }
7884
7885 if (rxflags && injected)
7886 return;
7887
7888 if (!injected)
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07007889 handle_frame(drv, buf + iter._max_length,
7890 len - iter._max_length, datarate, ssi_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007891 else
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07007892 handle_tx_callback(drv->ctx, buf + iter._max_length,
7893 len - iter._max_length, !failed);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007894}
7895
7896
7897/*
7898 * we post-process the filter code later and rewrite
7899 * this to the offset to the last instruction
7900 */
7901#define PASS 0xFF
7902#define FAIL 0xFE
7903
7904static struct sock_filter msock_filter_insns[] = {
7905 /*
7906 * do a little-endian load of the radiotap length field
7907 */
7908 /* load lower byte into A */
7909 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 2),
7910 /* put it into X (== index register) */
7911 BPF_STMT(BPF_MISC| BPF_TAX, 0),
7912 /* load upper byte into A */
7913 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 3),
7914 /* left-shift it by 8 */
7915 BPF_STMT(BPF_ALU | BPF_LSH | BPF_K, 8),
7916 /* or with X */
7917 BPF_STMT(BPF_ALU | BPF_OR | BPF_X, 0),
7918 /* put result into X */
7919 BPF_STMT(BPF_MISC| BPF_TAX, 0),
7920
7921 /*
7922 * Allow management frames through, this also gives us those
7923 * management frames that we sent ourselves with status
7924 */
7925 /* load the lower byte of the IEEE 802.11 frame control field */
7926 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
7927 /* mask off frame type and version */
7928 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0xF),
7929 /* accept frame if it's both 0, fall through otherwise */
7930 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0, PASS, 0),
7931
7932 /*
7933 * TODO: add a bit to radiotap RX flags that indicates
7934 * that the sending station is not associated, then
7935 * add a filter here that filters on our DA and that flag
7936 * to allow us to deauth frames to that bad station.
7937 *
7938 * For now allow all To DS data frames through.
7939 */
7940 /* load the IEEE 802.11 frame control field */
7941 BPF_STMT(BPF_LD | BPF_H | BPF_IND, 0),
7942 /* mask off frame type, version and DS status */
7943 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0F03),
7944 /* accept frame if version 0, type 2 and To DS, fall through otherwise
7945 */
7946 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0801, PASS, 0),
7947
7948#if 0
7949 /*
7950 * drop non-data frames
7951 */
7952 /* load the lower byte of the frame control field */
7953 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
7954 /* mask off QoS bit */
7955 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0c),
7956 /* drop non-data frames */
7957 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 8, 0, FAIL),
7958#endif
7959 /* load the upper byte of the frame control field */
7960 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 1),
7961 /* mask off toDS/fromDS */
7962 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x03),
7963 /* accept WDS frames */
7964 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 3, PASS, 0),
7965
7966 /*
7967 * add header length to index
7968 */
7969 /* load the lower byte of the frame control field */
7970 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
7971 /* mask off QoS bit */
7972 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x80),
7973 /* right shift it by 6 to give 0 or 2 */
7974 BPF_STMT(BPF_ALU | BPF_RSH | BPF_K, 6),
7975 /* add data frame header length */
7976 BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 24),
7977 /* add index, was start of 802.11 header */
7978 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
7979 /* move to index, now start of LL header */
7980 BPF_STMT(BPF_MISC | BPF_TAX, 0),
7981
7982 /*
7983 * Accept empty data frames, we use those for
7984 * polling activity.
7985 */
7986 BPF_STMT(BPF_LD | BPF_W | BPF_LEN, 0),
7987 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_X, 0, PASS, 0),
7988
7989 /*
7990 * Accept EAPOL frames
7991 */
7992 BPF_STMT(BPF_LD | BPF_W | BPF_IND, 0),
7993 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0xAAAA0300, 0, FAIL),
7994 BPF_STMT(BPF_LD | BPF_W | BPF_IND, 4),
7995 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0000888E, PASS, FAIL),
7996
7997 /* keep these last two statements or change the code below */
7998 /* return 0 == "DROP" */
7999 BPF_STMT(BPF_RET | BPF_K, 0),
8000 /* return ~0 == "keep all" */
8001 BPF_STMT(BPF_RET | BPF_K, ~0),
8002};
8003
8004static struct sock_fprog msock_filter = {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008005 .len = ARRAY_SIZE(msock_filter_insns),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008006 .filter = msock_filter_insns,
8007};
8008
8009
8010static int add_monitor_filter(int s)
8011{
8012 int idx;
8013
8014 /* rewrite all PASS/FAIL jump offsets */
8015 for (idx = 0; idx < msock_filter.len; idx++) {
8016 struct sock_filter *insn = &msock_filter_insns[idx];
8017
8018 if (BPF_CLASS(insn->code) == BPF_JMP) {
8019 if (insn->code == (BPF_JMP|BPF_JA)) {
8020 if (insn->k == PASS)
8021 insn->k = msock_filter.len - idx - 2;
8022 else if (insn->k == FAIL)
8023 insn->k = msock_filter.len - idx - 3;
8024 }
8025
8026 if (insn->jt == PASS)
8027 insn->jt = msock_filter.len - idx - 2;
8028 else if (insn->jt == FAIL)
8029 insn->jt = msock_filter.len - idx - 3;
8030
8031 if (insn->jf == PASS)
8032 insn->jf = msock_filter.len - idx - 2;
8033 else if (insn->jf == FAIL)
8034 insn->jf = msock_filter.len - idx - 3;
8035 }
8036 }
8037
8038 if (setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER,
8039 &msock_filter, sizeof(msock_filter))) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008040 wpa_printf(MSG_ERROR, "nl80211: setsockopt(SO_ATTACH_FILTER) failed: %s",
8041 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008042 return -1;
8043 }
8044
8045 return 0;
8046}
8047
8048
8049static void nl80211_remove_monitor_interface(
8050 struct wpa_driver_nl80211_data *drv)
8051{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008052 if (drv->monitor_refcount > 0)
8053 drv->monitor_refcount--;
8054 wpa_printf(MSG_DEBUG, "nl80211: Remove monitor interface: refcount=%d",
8055 drv->monitor_refcount);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008056 if (drv->monitor_refcount > 0)
8057 return;
8058
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008059 if (drv->monitor_ifidx >= 0) {
8060 nl80211_remove_iface(drv, drv->monitor_ifidx);
8061 drv->monitor_ifidx = -1;
8062 }
8063 if (drv->monitor_sock >= 0) {
8064 eloop_unregister_read_sock(drv->monitor_sock);
8065 close(drv->monitor_sock);
8066 drv->monitor_sock = -1;
8067 }
8068}
8069
8070
8071static int
8072nl80211_create_monitor_interface(struct wpa_driver_nl80211_data *drv)
8073{
8074 char buf[IFNAMSIZ];
8075 struct sockaddr_ll ll;
8076 int optval;
8077 socklen_t optlen;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008078
8079 if (drv->monitor_ifidx >= 0) {
8080 drv->monitor_refcount++;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008081 wpa_printf(MSG_DEBUG, "nl80211: Re-use existing monitor interface: refcount=%d",
8082 drv->monitor_refcount);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008083 return 0;
8084 }
8085
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008086 if (os_strncmp(drv->first_bss->ifname, "p2p-", 4) == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008087 /*
8088 * P2P interface name is of the format p2p-%s-%d. For monitor
8089 * interface name corresponding to P2P GO, replace "p2p-" with
8090 * "mon-" to retain the same interface name length and to
8091 * indicate that it is a monitor interface.
8092 */
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008093 snprintf(buf, IFNAMSIZ, "mon-%s", drv->first_bss->ifname + 4);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008094 } else {
8095 /* Non-P2P interface with AP functionality. */
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008096 snprintf(buf, IFNAMSIZ, "mon.%s", drv->first_bss->ifname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008097 }
8098
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008099 buf[IFNAMSIZ - 1] = '\0';
8100
8101 drv->monitor_ifidx =
8102 nl80211_create_iface(drv, buf, NL80211_IFTYPE_MONITOR, NULL,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008103 0, NULL, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008104
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07008105 if (drv->monitor_ifidx == -EOPNOTSUPP) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008106 /*
8107 * This is backward compatibility for a few versions of
8108 * the kernel only that didn't advertise the right
8109 * attributes for the only driver that then supported
8110 * AP mode w/o monitor -- ath6kl.
8111 */
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07008112 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support "
8113 "monitor interface type - try to run without it");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008114 drv->device_ap_sme = 1;
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07008115 }
8116
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008117 if (drv->monitor_ifidx < 0)
8118 return -1;
8119
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008120 if (linux_set_iface_flags(drv->global->ioctl_sock, buf, 1))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008121 goto error;
8122
8123 memset(&ll, 0, sizeof(ll));
8124 ll.sll_family = AF_PACKET;
8125 ll.sll_ifindex = drv->monitor_ifidx;
8126 drv->monitor_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
8127 if (drv->monitor_sock < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008128 wpa_printf(MSG_ERROR, "nl80211: socket[PF_PACKET,SOCK_RAW] failed: %s",
8129 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008130 goto error;
8131 }
8132
8133 if (add_monitor_filter(drv->monitor_sock)) {
8134 wpa_printf(MSG_INFO, "Failed to set socket filter for monitor "
8135 "interface; do filtering in user space");
8136 /* This works, but will cost in performance. */
8137 }
8138
8139 if (bind(drv->monitor_sock, (struct sockaddr *) &ll, sizeof(ll)) < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008140 wpa_printf(MSG_ERROR, "nl80211: monitor socket bind failed: %s",
8141 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008142 goto error;
8143 }
8144
8145 optlen = sizeof(optval);
8146 optval = 20;
8147 if (setsockopt
8148 (drv->monitor_sock, SOL_SOCKET, SO_PRIORITY, &optval, optlen)) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008149 wpa_printf(MSG_ERROR, "nl80211: Failed to set socket priority: %s",
8150 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008151 goto error;
8152 }
8153
8154 if (eloop_register_read_sock(drv->monitor_sock, handle_monitor_read,
8155 drv, NULL)) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008156 wpa_printf(MSG_INFO, "nl80211: Could not register monitor read socket");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008157 goto error;
8158 }
8159
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008160 drv->monitor_refcount++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008161 return 0;
8162 error:
8163 nl80211_remove_monitor_interface(drv);
8164 return -1;
8165}
8166
8167
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008168static int nl80211_setup_ap(struct i802_bss *bss)
8169{
8170 struct wpa_driver_nl80211_data *drv = bss->drv;
8171
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008172 wpa_printf(MSG_DEBUG, "nl80211: Setup AP(%s) - device_ap_sme=%d use_monitor=%d",
8173 bss->ifname, drv->device_ap_sme, drv->use_monitor);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008174
8175 /*
8176 * Disable Probe Request reporting unless we need it in this way for
8177 * devices that include the AP SME, in the other case (unless using
8178 * monitor iface) we'll get it through the nl_mgmt socket instead.
8179 */
8180 if (!drv->device_ap_sme)
8181 wpa_driver_nl80211_probe_req_report(bss, 0);
8182
8183 if (!drv->device_ap_sme && !drv->use_monitor)
8184 if (nl80211_mgmt_subscribe_ap(bss))
8185 return -1;
8186
8187 if (drv->device_ap_sme && !drv->use_monitor)
8188 if (nl80211_mgmt_subscribe_ap_dev_sme(bss))
8189 return -1;
8190
8191 if (!drv->device_ap_sme && drv->use_monitor &&
8192 nl80211_create_monitor_interface(drv) &&
8193 !drv->device_ap_sme)
Dmitry Shmidt04949592012-07-19 12:16:46 -07008194 return -1;
8195
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008196 if (drv->device_ap_sme &&
8197 wpa_driver_nl80211_probe_req_report(bss, 1) < 0) {
8198 wpa_printf(MSG_DEBUG, "nl80211: Failed to enable "
8199 "Probe Request frame reporting in AP mode");
8200 /* Try to survive without this */
8201 }
8202
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008203 return 0;
8204}
8205
8206
8207static void nl80211_teardown_ap(struct i802_bss *bss)
8208{
8209 struct wpa_driver_nl80211_data *drv = bss->drv;
8210
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008211 wpa_printf(MSG_DEBUG, "nl80211: Teardown AP(%s) - device_ap_sme=%d use_monitor=%d",
8212 bss->ifname, drv->device_ap_sme, drv->use_monitor);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008213 if (drv->device_ap_sme) {
8214 wpa_driver_nl80211_probe_req_report(bss, 0);
8215 if (!drv->use_monitor)
8216 nl80211_mgmt_unsubscribe(bss, "AP teardown (dev SME)");
8217 } else if (drv->use_monitor)
8218 nl80211_remove_monitor_interface(drv);
8219 else
8220 nl80211_mgmt_unsubscribe(bss, "AP teardown");
8221
8222 bss->beacon_set = 0;
8223}
8224
8225
8226static int nl80211_send_eapol_data(struct i802_bss *bss,
8227 const u8 *addr, const u8 *data,
8228 size_t data_len)
8229{
8230 struct sockaddr_ll ll;
8231 int ret;
8232
8233 if (bss->drv->eapol_tx_sock < 0) {
8234 wpa_printf(MSG_DEBUG, "nl80211: No socket to send EAPOL");
8235 return -1;
8236 }
8237
8238 os_memset(&ll, 0, sizeof(ll));
8239 ll.sll_family = AF_PACKET;
8240 ll.sll_ifindex = bss->ifindex;
8241 ll.sll_protocol = htons(ETH_P_PAE);
8242 ll.sll_halen = ETH_ALEN;
8243 os_memcpy(ll.sll_addr, addr, ETH_ALEN);
8244 ret = sendto(bss->drv->eapol_tx_sock, data, data_len, 0,
8245 (struct sockaddr *) &ll, sizeof(ll));
8246 if (ret < 0)
8247 wpa_printf(MSG_ERROR, "nl80211: EAPOL TX: %s",
8248 strerror(errno));
8249
8250 return ret;
8251}
8252
8253
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008254static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
8255
8256static int wpa_driver_nl80211_hapd_send_eapol(
8257 void *priv, const u8 *addr, const u8 *data,
8258 size_t data_len, int encrypt, const u8 *own_addr, u32 flags)
8259{
8260 struct i802_bss *bss = priv;
8261 struct wpa_driver_nl80211_data *drv = bss->drv;
8262 struct ieee80211_hdr *hdr;
8263 size_t len;
8264 u8 *pos;
8265 int res;
8266 int qos = flags & WPA_STA_WMM;
Dmitry Shmidt641185e2013-11-06 15:17:13 -08008267
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008268 if (drv->device_ap_sme || !drv->use_monitor)
8269 return nl80211_send_eapol_data(bss, addr, data, data_len);
8270
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008271 len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
8272 data_len;
8273 hdr = os_zalloc(len);
8274 if (hdr == NULL) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008275 wpa_printf(MSG_INFO, "nl80211: Failed to allocate EAPOL buffer(len=%lu)",
8276 (unsigned long) len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008277 return -1;
8278 }
8279
8280 hdr->frame_control =
8281 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
8282 hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
8283 if (encrypt)
8284 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
8285 if (qos) {
8286 hdr->frame_control |=
8287 host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
8288 }
8289
8290 memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
8291 memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
8292 memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
8293 pos = (u8 *) (hdr + 1);
8294
8295 if (qos) {
Dmitry Shmidtaa532512012-09-24 10:35:31 -07008296 /* Set highest priority in QoS header */
8297 pos[0] = 7;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008298 pos[1] = 0;
8299 pos += 2;
8300 }
8301
8302 memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
8303 pos += sizeof(rfc1042_header);
8304 WPA_PUT_BE16(pos, ETH_P_PAE);
8305 pos += 2;
8306 memcpy(pos, data, data_len);
8307
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008308 res = wpa_driver_nl80211_send_frame(bss, (u8 *) hdr, len, encrypt, 0,
8309 0, 0, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008310 if (res < 0) {
8311 wpa_printf(MSG_ERROR, "i802_send_eapol - packet len: %lu - "
8312 "failed: %d (%s)",
8313 (unsigned long) len, errno, strerror(errno));
8314 }
8315 os_free(hdr);
8316
8317 return res;
8318}
8319
8320
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008321static int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr,
8322 int total_flags,
8323 int flags_or, int flags_and)
8324{
8325 struct i802_bss *bss = priv;
8326 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008327 struct nl_msg *msg;
8328 struct nlattr *flags;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008329 struct nl80211_sta_flag_update upd;
8330
8331 msg = nlmsg_alloc();
8332 if (!msg)
8333 return -ENOMEM;
8334
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008335 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008336
8337 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
8338 if_nametoindex(bss->ifname));
8339 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
8340
8341 /*
8342 * Backwards compatibility version using NL80211_ATTR_STA_FLAGS. This
8343 * can be removed eventually.
8344 */
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008345 flags = nla_nest_start(msg, NL80211_ATTR_STA_FLAGS);
8346 if (!flags)
8347 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008348 if (total_flags & WPA_STA_AUTHORIZED)
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008349 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_AUTHORIZED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008350
8351 if (total_flags & WPA_STA_WMM)
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008352 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_WME);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008353
8354 if (total_flags & WPA_STA_SHORT_PREAMBLE)
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008355 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_SHORT_PREAMBLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008356
8357 if (total_flags & WPA_STA_MFP)
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008358 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_MFP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008359
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008360 if (total_flags & WPA_STA_TDLS_PEER)
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008361 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_TDLS_PEER);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008362
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008363 nla_nest_end(msg, flags);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008364
8365 os_memset(&upd, 0, sizeof(upd));
8366 upd.mask = sta_flags_nl80211(flags_or | ~flags_and);
8367 upd.set = sta_flags_nl80211(flags_or);
8368 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
8369
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008370 return send_and_recv_msgs(drv, msg, NULL, NULL);
8371 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008372 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008373 return -ENOBUFS;
8374}
8375
8376
8377static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv,
8378 struct wpa_driver_associate_params *params)
8379{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008380 enum nl80211_iftype nlmode, old_mode;
8381 struct hostapd_freq_params freq = {
8382 .freq = params->freq,
8383 };
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008384
8385 if (params->p2p) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008386 wpa_printf(MSG_DEBUG, "nl80211: Setup AP operations for P2P "
8387 "group (GO)");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008388 nlmode = NL80211_IFTYPE_P2P_GO;
8389 } else
8390 nlmode = NL80211_IFTYPE_AP;
8391
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008392 old_mode = drv->nlmode;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008393 if (wpa_driver_nl80211_set_mode(drv->first_bss, nlmode)) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008394 nl80211_remove_monitor_interface(drv);
8395 return -1;
8396 }
8397
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008398 if (wpa_driver_nl80211_set_freq(drv->first_bss, &freq)) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008399 if (old_mode != nlmode)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008400 wpa_driver_nl80211_set_mode(drv->first_bss, old_mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008401 nl80211_remove_monitor_interface(drv);
8402 return -1;
8403 }
8404
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008405 return 0;
8406}
8407
8408
8409static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv)
8410{
8411 struct nl_msg *msg;
8412 int ret = -1;
8413
8414 msg = nlmsg_alloc();
8415 if (!msg)
8416 return -1;
8417
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008418 nl80211_cmd(drv, msg, 0, NL80211_CMD_LEAVE_IBSS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008419 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
8420 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8421 msg = NULL;
8422 if (ret) {
8423 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS failed: ret=%d "
8424 "(%s)", ret, strerror(-ret));
8425 goto nla_put_failure;
8426 }
8427
8428 ret = 0;
8429 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS request sent successfully");
8430
8431nla_put_failure:
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008432 if (wpa_driver_nl80211_set_mode(drv->first_bss,
Dmitry Shmidt56052862013-10-04 10:23:25 -07008433 NL80211_IFTYPE_STATION)) {
8434 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
8435 "station mode");
8436 }
8437
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008438 nlmsg_free(msg);
8439 return ret;
8440}
8441
8442
8443static int wpa_driver_nl80211_ibss(struct wpa_driver_nl80211_data *drv,
8444 struct wpa_driver_associate_params *params)
8445{
8446 struct nl_msg *msg;
8447 int ret = -1;
8448 int count = 0;
8449
8450 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS (ifindex=%d)", drv->ifindex);
8451
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008452 if (wpa_driver_nl80211_set_mode(drv->first_bss,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008453 NL80211_IFTYPE_ADHOC)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008454 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
8455 "IBSS mode");
8456 return -1;
8457 }
8458
8459retry:
8460 msg = nlmsg_alloc();
8461 if (!msg)
8462 return -1;
8463
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008464 nl80211_cmd(drv, msg, 0, NL80211_CMD_JOIN_IBSS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008465 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
8466
8467 if (params->ssid == NULL || params->ssid_len > sizeof(drv->ssid))
8468 goto nla_put_failure;
8469
8470 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
8471 params->ssid, params->ssid_len);
8472 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
8473 params->ssid);
8474 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
8475 drv->ssid_len = params->ssid_len;
8476
8477 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
8478 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
8479
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08008480 if (params->beacon_int > 0) {
8481 wpa_printf(MSG_DEBUG, " * beacon_int=%d", params->beacon_int);
8482 NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL,
8483 params->beacon_int);
8484 }
8485
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008486 ret = nl80211_set_conn_keys(params, msg);
8487 if (ret)
8488 goto nla_put_failure;
8489
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008490 if (params->bssid && params->fixed_bssid) {
8491 wpa_printf(MSG_DEBUG, " * BSSID=" MACSTR,
8492 MAC2STR(params->bssid));
8493 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
8494 }
8495
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008496 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
8497 params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
8498 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
8499 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008500 wpa_printf(MSG_DEBUG, " * control port");
8501 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT);
8502 }
8503
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008504 if (params->wpa_ie) {
8505 wpa_hexdump(MSG_DEBUG,
8506 " * Extra IEs for Beacon/Probe Response frames",
8507 params->wpa_ie, params->wpa_ie_len);
8508 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
8509 params->wpa_ie);
8510 }
8511
8512 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8513 msg = NULL;
8514 if (ret) {
8515 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS failed: ret=%d (%s)",
8516 ret, strerror(-ret));
8517 count++;
8518 if (ret == -EALREADY && count == 1) {
8519 wpa_printf(MSG_DEBUG, "nl80211: Retry IBSS join after "
8520 "forced leave");
8521 nl80211_leave_ibss(drv);
8522 nlmsg_free(msg);
8523 goto retry;
8524 }
8525
8526 goto nla_put_failure;
8527 }
8528 ret = 0;
8529 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS request sent successfully");
8530
8531nla_put_failure:
8532 nlmsg_free(msg);
8533 return ret;
8534}
8535
8536
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008537static int nl80211_connect_common(struct wpa_driver_nl80211_data *drv,
8538 struct wpa_driver_associate_params *params,
8539 struct nl_msg *msg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008540{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008541 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008542
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008543 if (params->bssid) {
8544 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
8545 MAC2STR(params->bssid));
8546 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
8547 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008548
Dmitry Shmidt96be6222014-02-13 10:16:51 -08008549 if (params->bssid_hint) {
8550 wpa_printf(MSG_DEBUG, " * bssid_hint=" MACSTR,
8551 MAC2STR(params->bssid_hint));
8552 NLA_PUT(msg, NL80211_ATTR_MAC_HINT, ETH_ALEN,
8553 params->bssid_hint);
8554 }
8555
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008556 if (params->freq) {
8557 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
8558 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008559 drv->assoc_freq = params->freq;
8560 } else
8561 drv->assoc_freq = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008562
Dmitry Shmidt96be6222014-02-13 10:16:51 -08008563 if (params->freq_hint) {
8564 wpa_printf(MSG_DEBUG, " * freq_hint=%d", params->freq_hint);
8565 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ_HINT,
8566 params->freq_hint);
8567 }
8568
Dmitry Shmidt04949592012-07-19 12:16:46 -07008569 if (params->bg_scan_period >= 0) {
8570 wpa_printf(MSG_DEBUG, " * bg scan period=%d",
8571 params->bg_scan_period);
8572 NLA_PUT_U16(msg, NL80211_ATTR_BG_SCAN_PERIOD,
8573 params->bg_scan_period);
8574 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008575
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008576 if (params->ssid) {
8577 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
8578 params->ssid, params->ssid_len);
8579 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
8580 params->ssid);
8581 if (params->ssid_len > sizeof(drv->ssid))
8582 goto nla_put_failure;
8583 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
8584 drv->ssid_len = params->ssid_len;
8585 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008586
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008587 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
8588 if (params->wpa_ie)
8589 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
8590 params->wpa_ie);
8591
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008592 if (params->wpa_proto) {
8593 enum nl80211_wpa_versions ver = 0;
8594
8595 if (params->wpa_proto & WPA_PROTO_WPA)
8596 ver |= NL80211_WPA_VERSION_1;
8597 if (params->wpa_proto & WPA_PROTO_RSN)
8598 ver |= NL80211_WPA_VERSION_2;
8599
8600 wpa_printf(MSG_DEBUG, " * WPA Versions 0x%x", ver);
8601 NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver);
8602 }
8603
8604 if (params->pairwise_suite != WPA_CIPHER_NONE) {
8605 u32 cipher = wpa_cipher_to_cipher_suite(params->pairwise_suite);
8606 wpa_printf(MSG_DEBUG, " * pairwise=0x%x", cipher);
8607 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE, cipher);
8608 }
8609
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08008610 if (params->group_suite == WPA_CIPHER_GTK_NOT_USED &&
8611 !(drv->capa.enc & WPA_DRIVER_CAPA_ENC_GTK_NOT_USED)) {
8612 /*
8613 * This is likely to work even though many drivers do not
8614 * advertise support for operations without GTK.
8615 */
8616 wpa_printf(MSG_DEBUG, " * skip group cipher configuration for GTK_NOT_USED due to missing driver support advertisement");
8617 } else if (params->group_suite != WPA_CIPHER_NONE) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008618 u32 cipher = wpa_cipher_to_cipher_suite(params->group_suite);
8619 wpa_printf(MSG_DEBUG, " * group=0x%x", cipher);
8620 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher);
8621 }
8622
8623 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
8624 params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
8625 params->key_mgmt_suite == WPA_KEY_MGMT_FT_IEEE8021X ||
8626 params->key_mgmt_suite == WPA_KEY_MGMT_FT_PSK ||
Dmitry Shmidt15907092014-03-25 10:42:57 -07008627 params->key_mgmt_suite == WPA_KEY_MGMT_CCKM ||
8628 params->key_mgmt_suite == WPA_KEY_MGMT_OSEN) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008629 int mgmt = WLAN_AKM_SUITE_PSK;
8630
8631 switch (params->key_mgmt_suite) {
8632 case WPA_KEY_MGMT_CCKM:
8633 mgmt = WLAN_AKM_SUITE_CCKM;
8634 break;
8635 case WPA_KEY_MGMT_IEEE8021X:
8636 mgmt = WLAN_AKM_SUITE_8021X;
8637 break;
8638 case WPA_KEY_MGMT_FT_IEEE8021X:
8639 mgmt = WLAN_AKM_SUITE_FT_8021X;
8640 break;
8641 case WPA_KEY_MGMT_FT_PSK:
8642 mgmt = WLAN_AKM_SUITE_FT_PSK;
8643 break;
Dmitry Shmidt15907092014-03-25 10:42:57 -07008644 case WPA_KEY_MGMT_OSEN:
8645 mgmt = WLAN_AKM_SUITE_OSEN;
8646 break;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008647 case WPA_KEY_MGMT_PSK:
8648 default:
8649 mgmt = WLAN_AKM_SUITE_PSK;
8650 break;
8651 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07008652 wpa_printf(MSG_DEBUG, " * akm=0x%x", mgmt);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008653 NLA_PUT_U32(msg, NL80211_ATTR_AKM_SUITES, mgmt);
8654 }
8655
8656 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT);
8657
8658 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED)
8659 NLA_PUT_U32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED);
8660
8661 if (params->disable_ht)
8662 NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_HT);
8663
8664 if (params->htcaps && params->htcaps_mask) {
8665 int sz = sizeof(struct ieee80211_ht_capabilities);
8666 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY, sz, params->htcaps);
8667 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY_MASK, sz,
8668 params->htcaps_mask);
8669 }
8670
8671#ifdef CONFIG_VHT_OVERRIDES
8672 if (params->disable_vht) {
8673 wpa_printf(MSG_DEBUG, " * VHT disabled");
8674 NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_VHT);
8675 }
8676
8677 if (params->vhtcaps && params->vhtcaps_mask) {
8678 int sz = sizeof(struct ieee80211_vht_capabilities);
8679 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY, sz, params->vhtcaps);
8680 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY_MASK, sz,
8681 params->vhtcaps_mask);
8682 }
8683#endif /* CONFIG_VHT_OVERRIDES */
8684
8685 if (params->p2p)
8686 wpa_printf(MSG_DEBUG, " * P2P group");
8687
8688 return 0;
8689nla_put_failure:
8690 return -1;
8691}
8692
8693
8694static int wpa_driver_nl80211_try_connect(
8695 struct wpa_driver_nl80211_data *drv,
8696 struct wpa_driver_associate_params *params)
8697{
8698 struct nl_msg *msg;
8699 enum nl80211_auth_type type;
8700 int ret;
8701 int algs;
8702
8703 msg = nlmsg_alloc();
8704 if (!msg)
8705 return -1;
8706
8707 wpa_printf(MSG_DEBUG, "nl80211: Connect (ifindex=%d)", drv->ifindex);
8708 nl80211_cmd(drv, msg, 0, NL80211_CMD_CONNECT);
8709
8710 ret = nl80211_connect_common(drv, params, msg);
8711 if (ret)
8712 goto nla_put_failure;
8713
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008714 algs = 0;
8715 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
8716 algs++;
8717 if (params->auth_alg & WPA_AUTH_ALG_SHARED)
8718 algs++;
8719 if (params->auth_alg & WPA_AUTH_ALG_LEAP)
8720 algs++;
8721 if (algs > 1) {
8722 wpa_printf(MSG_DEBUG, " * Leave out Auth Type for automatic "
8723 "selection");
8724 goto skip_auth_type;
8725 }
8726
8727 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
8728 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
8729 else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
8730 type = NL80211_AUTHTYPE_SHARED_KEY;
8731 else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
8732 type = NL80211_AUTHTYPE_NETWORK_EAP;
8733 else if (params->auth_alg & WPA_AUTH_ALG_FT)
8734 type = NL80211_AUTHTYPE_FT;
8735 else
8736 goto nla_put_failure;
8737
8738 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
8739 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
8740
8741skip_auth_type:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008742 ret = nl80211_set_conn_keys(params, msg);
8743 if (ret)
8744 goto nla_put_failure;
8745
8746 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8747 msg = NULL;
8748 if (ret) {
8749 wpa_printf(MSG_DEBUG, "nl80211: MLME connect failed: ret=%d "
8750 "(%s)", ret, strerror(-ret));
8751 goto nla_put_failure;
8752 }
8753 ret = 0;
8754 wpa_printf(MSG_DEBUG, "nl80211: Connect request send successfully");
8755
8756nla_put_failure:
8757 nlmsg_free(msg);
8758 return ret;
8759
8760}
8761
8762
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08008763static int wpa_driver_nl80211_connect(
8764 struct wpa_driver_nl80211_data *drv,
8765 struct wpa_driver_associate_params *params)
8766{
8767 int ret = wpa_driver_nl80211_try_connect(drv, params);
8768 if (ret == -EALREADY) {
8769 /*
8770 * cfg80211 does not currently accept new connections if
8771 * we are already connected. As a workaround, force
8772 * disconnection and try again.
8773 */
8774 wpa_printf(MSG_DEBUG, "nl80211: Explicitly "
8775 "disconnecting before reassociation "
8776 "attempt");
8777 if (wpa_driver_nl80211_disconnect(
8778 drv, WLAN_REASON_PREV_AUTH_NOT_VALID))
8779 return -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08008780 ret = wpa_driver_nl80211_try_connect(drv, params);
8781 }
8782 return ret;
8783}
8784
8785
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008786static int wpa_driver_nl80211_associate(
8787 void *priv, struct wpa_driver_associate_params *params)
8788{
8789 struct i802_bss *bss = priv;
8790 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008791 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008792 struct nl_msg *msg;
8793
8794 if (params->mode == IEEE80211_MODE_AP)
8795 return wpa_driver_nl80211_ap(drv, params);
8796
8797 if (params->mode == IEEE80211_MODE_IBSS)
8798 return wpa_driver_nl80211_ibss(drv, params);
8799
8800 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008801 enum nl80211_iftype nlmode = params->p2p ?
8802 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
8803
8804 if (wpa_driver_nl80211_set_mode(priv, nlmode) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008805 return -1;
8806 return wpa_driver_nl80211_connect(drv, params);
8807 }
8808
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07008809 nl80211_mark_disconnected(drv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008810
8811 msg = nlmsg_alloc();
8812 if (!msg)
8813 return -1;
8814
8815 wpa_printf(MSG_DEBUG, "nl80211: Associate (ifindex=%d)",
8816 drv->ifindex);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008817 nl80211_cmd(drv, msg, 0, NL80211_CMD_ASSOCIATE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008818
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008819 ret = nl80211_connect_common(drv, params, msg);
8820 if (ret)
8821 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008822
8823 if (params->prev_bssid) {
8824 wpa_printf(MSG_DEBUG, " * prev_bssid=" MACSTR,
8825 MAC2STR(params->prev_bssid));
8826 NLA_PUT(msg, NL80211_ATTR_PREV_BSSID, ETH_ALEN,
8827 params->prev_bssid);
8828 }
8829
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008830 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8831 msg = NULL;
8832 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008833 wpa_dbg(drv->ctx, MSG_DEBUG,
8834 "nl80211: MLME command failed (assoc): ret=%d (%s)",
8835 ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008836 nl80211_dump_scan(drv);
8837 goto nla_put_failure;
8838 }
8839 ret = 0;
8840 wpa_printf(MSG_DEBUG, "nl80211: Association request send "
8841 "successfully");
8842
8843nla_put_failure:
8844 nlmsg_free(msg);
8845 return ret;
8846}
8847
8848
8849static int nl80211_set_mode(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008850 int ifindex, enum nl80211_iftype mode)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008851{
8852 struct nl_msg *msg;
8853 int ret = -ENOBUFS;
8854
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008855 wpa_printf(MSG_DEBUG, "nl80211: Set mode ifindex %d iftype %d (%s)",
8856 ifindex, mode, nl80211_iftype_str(mode));
8857
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008858 msg = nlmsg_alloc();
8859 if (!msg)
8860 return -ENOMEM;
8861
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008862 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_INTERFACE);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008863 if (nl80211_set_iface_id(msg, drv->first_bss) < 0)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008864 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008865 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, mode);
8866
8867 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008868 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008869 if (!ret)
8870 return 0;
8871nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008872 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008873 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface %d to mode %d:"
8874 " %d (%s)", ifindex, mode, ret, strerror(-ret));
8875 return ret;
8876}
8877
8878
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008879static int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
8880 enum nl80211_iftype nlmode)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008881{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008882 struct wpa_driver_nl80211_data *drv = bss->drv;
8883 int ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008884 int i;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008885 int was_ap = is_ap_interface(drv->nlmode);
8886 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008887
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008888 res = nl80211_set_mode(drv, drv->ifindex, nlmode);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008889 if (res && nlmode == nl80211_get_ifmode(bss))
8890 res = 0;
8891
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008892 if (res == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008893 drv->nlmode = nlmode;
8894 ret = 0;
8895 goto done;
8896 }
8897
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008898 if (res == -ENODEV)
8899 return -1;
8900
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008901 if (nlmode == drv->nlmode) {
8902 wpa_printf(MSG_DEBUG, "nl80211: Interface already in "
8903 "requested mode - ignore error");
8904 ret = 0;
8905 goto done; /* Already in the requested mode */
8906 }
8907
8908 /* mac80211 doesn't allow mode changes while the device is up, so
8909 * take the device down, try to set the mode again, and bring the
8910 * device back up.
8911 */
8912 wpa_printf(MSG_DEBUG, "nl80211: Try mode change after setting "
8913 "interface down");
8914 for (i = 0; i < 10; i++) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008915 res = i802_set_iface_flags(bss, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008916 if (res == -EACCES || res == -ENODEV)
8917 break;
8918 if (res == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008919 /* Try to set the mode again while the interface is
8920 * down */
8921 ret = nl80211_set_mode(drv, drv->ifindex, nlmode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008922 if (ret == -EACCES)
8923 break;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008924 res = i802_set_iface_flags(bss, 1);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008925 if (res && !ret)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008926 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008927 else if (ret != -EBUSY)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008928 break;
8929 } else
8930 wpa_printf(MSG_DEBUG, "nl80211: Failed to set "
8931 "interface down");
8932 os_sleep(0, 100000);
8933 }
8934
8935 if (!ret) {
8936 wpa_printf(MSG_DEBUG, "nl80211: Mode change succeeded while "
8937 "interface is down");
8938 drv->nlmode = nlmode;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008939 drv->ignore_if_down_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008940 }
8941
8942done:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008943 if (ret) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008944 wpa_printf(MSG_DEBUG, "nl80211: Interface mode change to %d "
8945 "from %d failed", nlmode, drv->nlmode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008946 return ret;
8947 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008948
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008949 if (is_p2p_net_interface(nlmode))
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008950 nl80211_disable_11b_rates(drv, drv->ifindex, 1);
8951 else if (drv->disabled_11b_rates)
8952 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
8953
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008954 if (is_ap_interface(nlmode)) {
8955 nl80211_mgmt_unsubscribe(bss, "start AP");
8956 /* Setup additional AP mode functionality if needed */
8957 if (nl80211_setup_ap(bss))
8958 return -1;
8959 } else if (was_ap) {
8960 /* Remove additional AP mode functionality */
8961 nl80211_teardown_ap(bss);
8962 } else {
8963 nl80211_mgmt_unsubscribe(bss, "mode change");
8964 }
8965
Dmitry Shmidt04949592012-07-19 12:16:46 -07008966 if (!bss->in_deinit && !is_ap_interface(nlmode) &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008967 nl80211_mgmt_subscribe_non_ap(bss) < 0)
8968 wpa_printf(MSG_DEBUG, "nl80211: Failed to register Action "
8969 "frame processing - ignore for now");
8970
8971 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008972}
8973
8974
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07008975static int dfs_info_handler(struct nl_msg *msg, void *arg)
8976{
8977 struct nlattr *tb[NL80211_ATTR_MAX + 1];
8978 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
8979 int *dfs_capability_ptr = arg;
8980
8981 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
8982 genlmsg_attrlen(gnlh, 0), NULL);
8983
8984 if (tb[NL80211_ATTR_VENDOR_DATA]) {
8985 struct nlattr *nl_vend = tb[NL80211_ATTR_VENDOR_DATA];
8986 struct nlattr *tb_vendor[QCA_WLAN_VENDOR_ATTR_MAX + 1];
8987
8988 nla_parse(tb_vendor, QCA_WLAN_VENDOR_ATTR_MAX,
8989 nla_data(nl_vend), nla_len(nl_vend), NULL);
8990
8991 if (tb_vendor[QCA_WLAN_VENDOR_ATTR_DFS]) {
8992 u32 val;
8993 val = nla_get_u32(tb_vendor[QCA_WLAN_VENDOR_ATTR_DFS]);
8994 wpa_printf(MSG_DEBUG, "nl80211: DFS offload capability: %u",
8995 val);
8996 *dfs_capability_ptr = val;
8997 }
8998 }
8999
9000 return NL_SKIP;
9001}
9002
9003
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009004static int wpa_driver_nl80211_get_capa(void *priv,
9005 struct wpa_driver_capa *capa)
9006{
9007 struct i802_bss *bss = priv;
9008 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07009009 struct nl_msg *msg;
9010 int dfs_capability = 0;
9011 int ret = 0;
9012
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009013 if (!drv->has_capability)
9014 return -1;
9015 os_memcpy(capa, &drv->capa, sizeof(*capa));
Dmitry Shmidt444d5672013-04-01 13:08:44 -07009016 if (drv->extended_capa && drv->extended_capa_mask) {
9017 capa->extended_capa = drv->extended_capa;
9018 capa->extended_capa_mask = drv->extended_capa_mask;
9019 capa->extended_capa_len = drv->extended_capa_len;
9020 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009021
9022 if ((capa->flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
9023 !drv->allow_p2p_device) {
9024 wpa_printf(MSG_DEBUG, "nl80211: Do not indicate P2P_DEVICE support (p2p_device=1 driver param not specified)");
9025 capa->flags &= ~WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE;
9026 }
9027
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07009028 if (drv->dfs_vendor_cmd_avail == 1) {
9029 msg = nlmsg_alloc();
9030 if (!msg)
9031 return -ENOMEM;
9032
9033 nl80211_cmd(drv, msg, 0, NL80211_CMD_VENDOR);
9034
9035 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
9036 NLA_PUT_U32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA);
9037 NLA_PUT_U32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9038 QCA_NL80211_VENDOR_SUBCMD_DFS_CAPABILITY);
9039
9040 ret = send_and_recv_msgs(drv, msg, dfs_info_handler,
9041 &dfs_capability);
9042 if (!ret) {
9043 if (dfs_capability)
9044 capa->flags |= WPA_DRIVER_FLAGS_DFS_OFFLOAD;
9045 }
9046 }
9047
9048 return ret;
9049
9050 nla_put_failure:
9051 nlmsg_free(msg);
9052 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009053}
9054
9055
9056static int wpa_driver_nl80211_set_operstate(void *priv, int state)
9057{
9058 struct i802_bss *bss = priv;
9059 struct wpa_driver_nl80211_data *drv = bss->drv;
9060
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08009061 wpa_printf(MSG_DEBUG, "nl80211: Set %s operstate %d->%d (%s)",
9062 bss->ifname, drv->operstate, state,
9063 state ? "UP" : "DORMANT");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009064 drv->operstate = state;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009065 return netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, -1,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009066 state ? IF_OPER_UP : IF_OPER_DORMANT);
9067}
9068
9069
9070static int wpa_driver_nl80211_set_supp_port(void *priv, int authorized)
9071{
9072 struct i802_bss *bss = priv;
9073 struct wpa_driver_nl80211_data *drv = bss->drv;
9074 struct nl_msg *msg;
9075 struct nl80211_sta_flag_update upd;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08009076 int ret = -ENOBUFS;
9077
9078 if (!drv->associated && is_zero_ether_addr(drv->bssid) && !authorized) {
9079 wpa_printf(MSG_DEBUG, "nl80211: Skip set_supp_port(unauthorized) while not associated");
9080 return 0;
9081 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009082
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07009083 wpa_printf(MSG_DEBUG, "nl80211: Set supplicant port %sauthorized for "
9084 MACSTR, authorized ? "" : "un", MAC2STR(drv->bssid));
9085
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009086 msg = nlmsg_alloc();
9087 if (!msg)
9088 return -ENOMEM;
9089
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009090 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009091
9092 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
9093 if_nametoindex(bss->ifname));
9094 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid);
9095
9096 os_memset(&upd, 0, sizeof(upd));
9097 upd.mask = BIT(NL80211_STA_FLAG_AUTHORIZED);
9098 if (authorized)
9099 upd.set = BIT(NL80211_STA_FLAG_AUTHORIZED);
9100 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
9101
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08009102 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9103 msg = NULL;
9104 if (!ret)
9105 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009106 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009107 nlmsg_free(msg);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08009108 wpa_printf(MSG_DEBUG, "nl80211: Failed to set STA flag: %d (%s)",
9109 ret, strerror(-ret));
9110 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009111}
9112
9113
Jouni Malinen75ecf522011-06-27 15:19:46 -07009114/* Set kernel driver on given frequency (MHz) */
9115static int i802_set_freq(void *priv, struct hostapd_freq_params *freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009116{
Jouni Malinen75ecf522011-06-27 15:19:46 -07009117 struct i802_bss *bss = priv;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08009118 return wpa_driver_nl80211_set_freq(bss, freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009119}
9120
9121
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009122static inline int min_int(int a, int b)
9123{
9124 if (a < b)
9125 return a;
9126 return b;
9127}
9128
9129
9130static int get_key_handler(struct nl_msg *msg, void *arg)
9131{
9132 struct nlattr *tb[NL80211_ATTR_MAX + 1];
9133 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
9134
9135 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9136 genlmsg_attrlen(gnlh, 0), NULL);
9137
9138 /*
9139 * TODO: validate the key index and mac address!
9140 * Otherwise, there's a race condition as soon as
9141 * the kernel starts sending key notifications.
9142 */
9143
9144 if (tb[NL80211_ATTR_KEY_SEQ])
9145 memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]),
9146 min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6));
9147 return NL_SKIP;
9148}
9149
9150
9151static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
9152 int idx, u8 *seq)
9153{
9154 struct i802_bss *bss = priv;
9155 struct wpa_driver_nl80211_data *drv = bss->drv;
9156 struct nl_msg *msg;
9157
9158 msg = nlmsg_alloc();
9159 if (!msg)
9160 return -ENOMEM;
9161
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009162 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_KEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009163
9164 if (addr)
9165 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
9166 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, idx);
9167 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
9168
9169 memset(seq, 0, 6);
9170
9171 return send_and_recv_msgs(drv, msg, get_key_handler, seq);
9172 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009173 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009174 return -ENOBUFS;
9175}
9176
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009177
9178static int i802_set_rts(void *priv, int rts)
9179{
9180 struct i802_bss *bss = priv;
9181 struct wpa_driver_nl80211_data *drv = bss->drv;
9182 struct nl_msg *msg;
9183 int ret = -ENOBUFS;
9184 u32 val;
9185
9186 msg = nlmsg_alloc();
9187 if (!msg)
9188 return -ENOMEM;
9189
9190 if (rts >= 2347)
9191 val = (u32) -1;
9192 else
9193 val = rts;
9194
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009195 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009196 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
9197 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD, val);
9198
9199 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009200 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009201 if (!ret)
9202 return 0;
9203nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009204 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009205 wpa_printf(MSG_DEBUG, "nl80211: Failed to set RTS threshold %d: "
9206 "%d (%s)", rts, ret, strerror(-ret));
9207 return ret;
9208}
9209
9210
9211static int i802_set_frag(void *priv, int frag)
9212{
9213 struct i802_bss *bss = priv;
9214 struct wpa_driver_nl80211_data *drv = bss->drv;
9215 struct nl_msg *msg;
9216 int ret = -ENOBUFS;
9217 u32 val;
9218
9219 msg = nlmsg_alloc();
9220 if (!msg)
9221 return -ENOMEM;
9222
9223 if (frag >= 2346)
9224 val = (u32) -1;
9225 else
9226 val = frag;
9227
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009228 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009229 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
9230 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD, val);
9231
9232 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009233 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009234 if (!ret)
9235 return 0;
9236nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009237 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009238 wpa_printf(MSG_DEBUG, "nl80211: Failed to set fragmentation threshold "
9239 "%d: %d (%s)", frag, ret, strerror(-ret));
9240 return ret;
9241}
9242
9243
9244static int i802_flush(void *priv)
9245{
9246 struct i802_bss *bss = priv;
9247 struct wpa_driver_nl80211_data *drv = bss->drv;
9248 struct nl_msg *msg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009249 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009250
9251 msg = nlmsg_alloc();
9252 if (!msg)
9253 return -1;
9254
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07009255 wpa_printf(MSG_DEBUG, "nl80211: flush -> DEL_STATION %s (all)",
9256 bss->ifname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009257 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009258
9259 /*
9260 * XXX: FIX! this needs to flush all VLANs too
9261 */
9262 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
9263 if_nametoindex(bss->ifname));
9264
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009265 res = send_and_recv_msgs(drv, msg, NULL, NULL);
9266 if (res) {
9267 wpa_printf(MSG_DEBUG, "nl80211: Station flush failed: ret=%d "
9268 "(%s)", res, strerror(-res));
9269 }
9270 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009271 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009272 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009273 return -ENOBUFS;
9274}
9275
9276
9277static int get_sta_handler(struct nl_msg *msg, void *arg)
9278{
9279 struct nlattr *tb[NL80211_ATTR_MAX + 1];
9280 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
9281 struct hostap_sta_driver_data *data = arg;
9282 struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
9283 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
9284 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
9285 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
9286 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
9287 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
9288 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03009289 [NL80211_STA_INFO_TX_FAILED] = { .type = NLA_U32 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009290 };
9291
9292 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9293 genlmsg_attrlen(gnlh, 0), NULL);
9294
9295 /*
9296 * TODO: validate the interface and mac address!
9297 * Otherwise, there's a race condition as soon as
9298 * the kernel starts sending station notifications.
9299 */
9300
9301 if (!tb[NL80211_ATTR_STA_INFO]) {
9302 wpa_printf(MSG_DEBUG, "sta stats missing!");
9303 return NL_SKIP;
9304 }
9305 if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
9306 tb[NL80211_ATTR_STA_INFO],
9307 stats_policy)) {
9308 wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
9309 return NL_SKIP;
9310 }
9311
9312 if (stats[NL80211_STA_INFO_INACTIVE_TIME])
9313 data->inactive_msec =
9314 nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
9315 if (stats[NL80211_STA_INFO_RX_BYTES])
9316 data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
9317 if (stats[NL80211_STA_INFO_TX_BYTES])
9318 data->tx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
9319 if (stats[NL80211_STA_INFO_RX_PACKETS])
9320 data->rx_packets =
9321 nla_get_u32(stats[NL80211_STA_INFO_RX_PACKETS]);
9322 if (stats[NL80211_STA_INFO_TX_PACKETS])
9323 data->tx_packets =
9324 nla_get_u32(stats[NL80211_STA_INFO_TX_PACKETS]);
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03009325 if (stats[NL80211_STA_INFO_TX_FAILED])
9326 data->tx_retry_failed =
9327 nla_get_u32(stats[NL80211_STA_INFO_TX_FAILED]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009328
9329 return NL_SKIP;
9330}
9331
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009332static int i802_read_sta_data(struct i802_bss *bss,
9333 struct hostap_sta_driver_data *data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009334 const u8 *addr)
9335{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009336 struct wpa_driver_nl80211_data *drv = bss->drv;
9337 struct nl_msg *msg;
9338
9339 os_memset(data, 0, sizeof(*data));
9340 msg = nlmsg_alloc();
9341 if (!msg)
9342 return -ENOMEM;
9343
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009344 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009345
9346 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
9347 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
9348
9349 return send_and_recv_msgs(drv, msg, get_sta_handler, data);
9350 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009351 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009352 return -ENOBUFS;
9353}
9354
9355
9356static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
9357 int cw_min, int cw_max, int burst_time)
9358{
9359 struct i802_bss *bss = priv;
9360 struct wpa_driver_nl80211_data *drv = bss->drv;
9361 struct nl_msg *msg;
9362 struct nlattr *txq, *params;
9363
9364 msg = nlmsg_alloc();
9365 if (!msg)
9366 return -1;
9367
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009368 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009369
9370 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
9371
9372 txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS);
9373 if (!txq)
9374 goto nla_put_failure;
9375
9376 /* We are only sending parameters for a single TXQ at a time */
9377 params = nla_nest_start(msg, 1);
9378 if (!params)
9379 goto nla_put_failure;
9380
9381 switch (queue) {
9382 case 0:
9383 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VO);
9384 break;
9385 case 1:
9386 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VI);
9387 break;
9388 case 2:
9389 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BE);
9390 break;
9391 case 3:
9392 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BK);
9393 break;
9394 }
9395 /* Burst time is configured in units of 0.1 msec and TXOP parameter in
9396 * 32 usec, so need to convert the value here. */
9397 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_TXOP, (burst_time * 100 + 16) / 32);
9398 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min);
9399 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max);
9400 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_AIFS, aifs);
9401
9402 nla_nest_end(msg, params);
9403
9404 nla_nest_end(msg, txq);
9405
9406 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
9407 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009408 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009409 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009410 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009411 return -1;
9412}
9413
9414
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009415static int i802_set_sta_vlan(struct i802_bss *bss, const u8 *addr,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009416 const char *ifname, int vlan_id)
9417{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009418 struct wpa_driver_nl80211_data *drv = bss->drv;
9419 struct nl_msg *msg;
9420 int ret = -ENOBUFS;
9421
9422 msg = nlmsg_alloc();
9423 if (!msg)
9424 return -ENOMEM;
9425
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009426 wpa_printf(MSG_DEBUG, "nl80211: %s[%d]: set_sta_vlan(" MACSTR
9427 ", ifname=%s[%d], vlan_id=%d)",
9428 bss->ifname, if_nametoindex(bss->ifname),
9429 MAC2STR(addr), ifname, if_nametoindex(ifname), vlan_id);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009430 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009431
9432 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
9433 if_nametoindex(bss->ifname));
9434 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
9435 NLA_PUT_U32(msg, NL80211_ATTR_STA_VLAN,
9436 if_nametoindex(ifname));
9437
9438 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009439 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009440 if (ret < 0) {
9441 wpa_printf(MSG_ERROR, "nl80211: NL80211_ATTR_STA_VLAN (addr="
9442 MACSTR " ifname=%s vlan_id=%d) failed: %d (%s)",
9443 MAC2STR(addr), ifname, vlan_id, ret,
9444 strerror(-ret));
9445 }
9446 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009447 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009448 return ret;
9449}
9450
9451
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009452static int i802_get_inact_sec(void *priv, const u8 *addr)
9453{
9454 struct hostap_sta_driver_data data;
9455 int ret;
9456
9457 data.inactive_msec = (unsigned long) -1;
9458 ret = i802_read_sta_data(priv, &data, addr);
9459 if (ret || data.inactive_msec == (unsigned long) -1)
9460 return -1;
9461 return data.inactive_msec / 1000;
9462}
9463
9464
9465static int i802_sta_clear_stats(void *priv, const u8 *addr)
9466{
9467#if 0
9468 /* TODO */
9469#endif
9470 return 0;
9471}
9472
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009473
9474static int i802_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
9475 int reason)
9476{
9477 struct i802_bss *bss = priv;
Dmitry Shmidt04949592012-07-19 12:16:46 -07009478 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009479 struct ieee80211_mgmt mgmt;
9480
Dmitry Shmidt04949592012-07-19 12:16:46 -07009481 if (drv->device_ap_sme)
9482 return wpa_driver_nl80211_sta_remove(bss, addr);
9483
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009484 memset(&mgmt, 0, sizeof(mgmt));
9485 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
9486 WLAN_FC_STYPE_DEAUTH);
9487 memcpy(mgmt.da, addr, ETH_ALEN);
9488 memcpy(mgmt.sa, own_addr, ETH_ALEN);
9489 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
9490 mgmt.u.deauth.reason_code = host_to_le16(reason);
9491 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
9492 IEEE80211_HDRLEN +
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009493 sizeof(mgmt.u.deauth), 0, 0, 0, 0,
9494 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009495}
9496
9497
9498static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
9499 int reason)
9500{
9501 struct i802_bss *bss = priv;
Dmitry Shmidt04949592012-07-19 12:16:46 -07009502 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009503 struct ieee80211_mgmt mgmt;
9504
Dmitry Shmidt04949592012-07-19 12:16:46 -07009505 if (drv->device_ap_sme)
9506 return wpa_driver_nl80211_sta_remove(bss, addr);
9507
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009508 memset(&mgmt, 0, sizeof(mgmt));
9509 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
9510 WLAN_FC_STYPE_DISASSOC);
9511 memcpy(mgmt.da, addr, ETH_ALEN);
9512 memcpy(mgmt.sa, own_addr, ETH_ALEN);
9513 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
9514 mgmt.u.disassoc.reason_code = host_to_le16(reason);
9515 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
9516 IEEE80211_HDRLEN +
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009517 sizeof(mgmt.u.disassoc), 0, 0, 0, 0,
9518 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009519}
9520
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009521
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07009522static void dump_ifidx(struct wpa_driver_nl80211_data *drv)
9523{
9524 char buf[200], *pos, *end;
9525 int i, res;
9526
9527 pos = buf;
9528 end = pos + sizeof(buf);
9529
9530 for (i = 0; i < drv->num_if_indices; i++) {
9531 if (!drv->if_indices[i])
9532 continue;
9533 res = os_snprintf(pos, end - pos, " %d", drv->if_indices[i]);
9534 if (res < 0 || res >= end - pos)
9535 break;
9536 pos += res;
9537 }
9538 *pos = '\0';
9539
9540 wpa_printf(MSG_DEBUG, "nl80211: if_indices[%d]:%s",
9541 drv->num_if_indices, buf);
9542}
9543
9544
Jouni Malinen75ecf522011-06-27 15:19:46 -07009545static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
9546{
9547 int i;
9548 int *old;
9549
9550 wpa_printf(MSG_DEBUG, "nl80211: Add own interface ifindex %d",
9551 ifidx);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07009552 if (have_ifidx(drv, ifidx)) {
9553 wpa_printf(MSG_DEBUG, "nl80211: ifindex %d already in the list",
9554 ifidx);
9555 return;
9556 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07009557 for (i = 0; i < drv->num_if_indices; i++) {
9558 if (drv->if_indices[i] == 0) {
9559 drv->if_indices[i] = ifidx;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07009560 dump_ifidx(drv);
Jouni Malinen75ecf522011-06-27 15:19:46 -07009561 return;
9562 }
9563 }
9564
9565 if (drv->if_indices != drv->default_if_indices)
9566 old = drv->if_indices;
9567 else
9568 old = NULL;
9569
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07009570 drv->if_indices = os_realloc_array(old, drv->num_if_indices + 1,
9571 sizeof(int));
Jouni Malinen75ecf522011-06-27 15:19:46 -07009572 if (!drv->if_indices) {
9573 if (!old)
9574 drv->if_indices = drv->default_if_indices;
9575 else
9576 drv->if_indices = old;
9577 wpa_printf(MSG_ERROR, "Failed to reallocate memory for "
9578 "interfaces");
9579 wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx);
9580 return;
9581 } else if (!old)
9582 os_memcpy(drv->if_indices, drv->default_if_indices,
9583 sizeof(drv->default_if_indices));
9584 drv->if_indices[drv->num_if_indices] = ifidx;
9585 drv->num_if_indices++;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07009586 dump_ifidx(drv);
Jouni Malinen75ecf522011-06-27 15:19:46 -07009587}
9588
9589
9590static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
9591{
9592 int i;
9593
9594 for (i = 0; i < drv->num_if_indices; i++) {
9595 if (drv->if_indices[i] == ifidx) {
9596 drv->if_indices[i] = 0;
9597 break;
9598 }
9599 }
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07009600 dump_ifidx(drv);
Jouni Malinen75ecf522011-06-27 15:19:46 -07009601}
9602
9603
9604static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
9605{
9606 int i;
9607
9608 for (i = 0; i < drv->num_if_indices; i++)
9609 if (drv->if_indices[i] == ifidx)
9610 return 1;
9611
9612 return 0;
9613}
9614
9615
9616static int i802_set_wds_sta(void *priv, const u8 *addr, int aid, int val,
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07009617 const char *bridge_ifname, char *ifname_wds)
Jouni Malinen75ecf522011-06-27 15:19:46 -07009618{
9619 struct i802_bss *bss = priv;
9620 struct wpa_driver_nl80211_data *drv = bss->drv;
9621 char name[IFNAMSIZ + 1];
9622
9623 os_snprintf(name, sizeof(name), "%s.sta%d", bss->ifname, aid);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07009624 if (ifname_wds)
9625 os_strlcpy(ifname_wds, name, IFNAMSIZ + 1);
9626
Jouni Malinen75ecf522011-06-27 15:19:46 -07009627 wpa_printf(MSG_DEBUG, "nl80211: Set WDS STA addr=" MACSTR
9628 " aid=%d val=%d name=%s", MAC2STR(addr), aid, val, name);
9629 if (val) {
9630 if (!if_nametoindex(name)) {
9631 if (nl80211_create_iface(drv, name,
9632 NL80211_IFTYPE_AP_VLAN,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009633 bss->addr, 1, NULL, NULL, 0) <
9634 0)
Jouni Malinen75ecf522011-06-27 15:19:46 -07009635 return -1;
9636 if (bridge_ifname &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009637 linux_br_add_if(drv->global->ioctl_sock,
9638 bridge_ifname, name) < 0)
Jouni Malinen75ecf522011-06-27 15:19:46 -07009639 return -1;
9640 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07009641 if (linux_set_iface_flags(drv->global->ioctl_sock, name, 1)) {
9642 wpa_printf(MSG_ERROR, "nl80211: Failed to set WDS STA "
9643 "interface %s up", name);
9644 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07009645 return i802_set_sta_vlan(priv, addr, name, 0);
9646 } else {
Dmitry Shmidtaa532512012-09-24 10:35:31 -07009647 if (bridge_ifname)
9648 linux_br_del_if(drv->global->ioctl_sock, bridge_ifname,
9649 name);
9650
Jouni Malinen75ecf522011-06-27 15:19:46 -07009651 i802_set_sta_vlan(priv, addr, bss->ifname, 0);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08009652 nl80211_remove_iface(drv, if_nametoindex(name));
9653 return 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07009654 }
9655}
9656
9657
9658static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
9659{
9660 struct wpa_driver_nl80211_data *drv = eloop_ctx;
9661 struct sockaddr_ll lladdr;
9662 unsigned char buf[3000];
9663 int len;
9664 socklen_t fromlen = sizeof(lladdr);
9665
9666 len = recvfrom(sock, buf, sizeof(buf), 0,
9667 (struct sockaddr *)&lladdr, &fromlen);
9668 if (len < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07009669 wpa_printf(MSG_ERROR, "nl80211: EAPOL recv failed: %s",
9670 strerror(errno));
Jouni Malinen75ecf522011-06-27 15:19:46 -07009671 return;
9672 }
9673
9674 if (have_ifidx(drv, lladdr.sll_ifindex))
9675 drv_event_eapol_rx(drv->ctx, lladdr.sll_addr, buf, len);
9676}
9677
9678
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009679static int i802_check_bridge(struct wpa_driver_nl80211_data *drv,
9680 struct i802_bss *bss,
9681 const char *brname, const char *ifname)
9682{
9683 int ifindex;
9684 char in_br[IFNAMSIZ];
9685
9686 os_strlcpy(bss->brname, brname, IFNAMSIZ);
9687 ifindex = if_nametoindex(brname);
9688 if (ifindex == 0) {
9689 /*
9690 * Bridge was configured, but the bridge device does
9691 * not exist. Try to add it now.
9692 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009693 if (linux_br_add(drv->global->ioctl_sock, brname) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009694 wpa_printf(MSG_ERROR, "nl80211: Failed to add the "
9695 "bridge interface %s: %s",
9696 brname, strerror(errno));
9697 return -1;
9698 }
9699 bss->added_bridge = 1;
9700 add_ifidx(drv, if_nametoindex(brname));
9701 }
9702
9703 if (linux_br_get(in_br, ifname) == 0) {
9704 if (os_strcmp(in_br, brname) == 0)
9705 return 0; /* already in the bridge */
9706
9707 wpa_printf(MSG_DEBUG, "nl80211: Removing interface %s from "
9708 "bridge %s", ifname, in_br);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009709 if (linux_br_del_if(drv->global->ioctl_sock, in_br, ifname) <
9710 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009711 wpa_printf(MSG_ERROR, "nl80211: Failed to "
9712 "remove interface %s from bridge "
9713 "%s: %s",
9714 ifname, brname, strerror(errno));
9715 return -1;
9716 }
9717 }
9718
9719 wpa_printf(MSG_DEBUG, "nl80211: Adding interface %s into bridge %s",
9720 ifname, brname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009721 if (linux_br_add_if(drv->global->ioctl_sock, brname, ifname) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009722 wpa_printf(MSG_ERROR, "nl80211: Failed to add interface %s "
9723 "into bridge %s: %s",
9724 ifname, brname, strerror(errno));
9725 return -1;
9726 }
9727 bss->added_if_into_bridge = 1;
9728
9729 return 0;
9730}
9731
9732
9733static void *i802_init(struct hostapd_data *hapd,
9734 struct wpa_init_params *params)
9735{
9736 struct wpa_driver_nl80211_data *drv;
9737 struct i802_bss *bss;
9738 size_t i;
9739 char brname[IFNAMSIZ];
9740 int ifindex, br_ifindex;
9741 int br_added = 0;
9742
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08009743 bss = wpa_driver_nl80211_drv_init(hapd, params->ifname,
9744 params->global_priv, 1,
9745 params->bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009746 if (bss == NULL)
9747 return NULL;
9748
9749 drv = bss->drv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009750
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009751 if (linux_br_get(brname, params->ifname) == 0) {
9752 wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in bridge %s",
9753 params->ifname, brname);
9754 br_ifindex = if_nametoindex(brname);
9755 } else {
9756 brname[0] = '\0';
9757 br_ifindex = 0;
9758 }
9759
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009760 for (i = 0; i < params->num_bridge; i++) {
9761 if (params->bridge[i]) {
9762 ifindex = if_nametoindex(params->bridge[i]);
9763 if (ifindex)
9764 add_ifidx(drv, ifindex);
9765 if (ifindex == br_ifindex)
9766 br_added = 1;
9767 }
9768 }
9769 if (!br_added && br_ifindex &&
9770 (params->num_bridge == 0 || !params->bridge[0]))
9771 add_ifidx(drv, br_ifindex);
9772
9773 /* start listening for EAPOL on the default AP interface */
9774 add_ifidx(drv, drv->ifindex);
9775
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009776 if (params->num_bridge && params->bridge[0] &&
9777 i802_check_bridge(drv, bss, params->bridge[0], params->ifname) < 0)
9778 goto failed;
9779
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009780 drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
9781 if (drv->eapol_sock < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07009782 wpa_printf(MSG_ERROR, "nl80211: socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE) failed: %s",
9783 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009784 goto failed;
9785 }
9786
9787 if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
9788 {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07009789 wpa_printf(MSG_INFO, "nl80211: Could not register read socket for eapol");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009790 goto failed;
9791 }
9792
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009793 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
9794 params->own_addr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009795 goto failed;
9796
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009797 memcpy(bss->addr, params->own_addr, ETH_ALEN);
9798
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009799 return bss;
9800
9801failed:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009802 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009803 return NULL;
9804}
9805
9806
9807static void i802_deinit(void *priv)
9808{
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009809 struct i802_bss *bss = priv;
9810 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009811}
9812
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009813
9814static enum nl80211_iftype wpa_driver_nl80211_if_type(
9815 enum wpa_driver_if_type type)
9816{
9817 switch (type) {
9818 case WPA_IF_STATION:
9819 return NL80211_IFTYPE_STATION;
9820 case WPA_IF_P2P_CLIENT:
9821 case WPA_IF_P2P_GROUP:
9822 return NL80211_IFTYPE_P2P_CLIENT;
9823 case WPA_IF_AP_VLAN:
9824 return NL80211_IFTYPE_AP_VLAN;
9825 case WPA_IF_AP_BSS:
9826 return NL80211_IFTYPE_AP;
9827 case WPA_IF_P2P_GO:
9828 return NL80211_IFTYPE_P2P_GO;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009829 case WPA_IF_P2P_DEVICE:
9830 return NL80211_IFTYPE_P2P_DEVICE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009831 }
9832 return -1;
9833}
9834
9835
9836#ifdef CONFIG_P2P
9837
9838static int nl80211_addr_in_use(struct nl80211_global *global, const u8 *addr)
9839{
9840 struct wpa_driver_nl80211_data *drv;
9841 dl_list_for_each(drv, &global->interfaces,
9842 struct wpa_driver_nl80211_data, list) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009843 if (os_memcmp(addr, drv->first_bss->addr, ETH_ALEN) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009844 return 1;
9845 }
9846 return 0;
9847}
9848
9849
9850static int nl80211_p2p_interface_addr(struct wpa_driver_nl80211_data *drv,
9851 u8 *new_addr)
9852{
9853 unsigned int idx;
9854
9855 if (!drv->global)
9856 return -1;
9857
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009858 os_memcpy(new_addr, drv->first_bss->addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009859 for (idx = 0; idx < 64; idx++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009860 new_addr[0] = drv->first_bss->addr[0] | 0x02;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009861 new_addr[0] ^= idx << 2;
9862 if (!nl80211_addr_in_use(drv->global, new_addr))
9863 break;
9864 }
9865 if (idx == 64)
9866 return -1;
9867
9868 wpa_printf(MSG_DEBUG, "nl80211: Assigned new P2P Interface Address "
9869 MACSTR, MAC2STR(new_addr));
9870
9871 return 0;
9872}
9873
9874#endif /* CONFIG_P2P */
9875
9876
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009877struct wdev_info {
9878 u64 wdev_id;
9879 int wdev_id_set;
9880 u8 macaddr[ETH_ALEN];
9881};
9882
9883static int nl80211_wdev_handler(struct nl_msg *msg, void *arg)
9884{
9885 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
9886 struct nlattr *tb[NL80211_ATTR_MAX + 1];
9887 struct wdev_info *wi = arg;
9888
9889 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9890 genlmsg_attrlen(gnlh, 0), NULL);
9891 if (tb[NL80211_ATTR_WDEV]) {
9892 wi->wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
9893 wi->wdev_id_set = 1;
9894 }
9895
9896 if (tb[NL80211_ATTR_MAC])
9897 os_memcpy(wi->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
9898 ETH_ALEN);
9899
9900 return NL_SKIP;
9901}
9902
9903
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009904static int wpa_driver_nl80211_if_add(void *priv, enum wpa_driver_if_type type,
9905 const char *ifname, const u8 *addr,
9906 void *bss_ctx, void **drv_priv,
9907 char *force_ifname, u8 *if_addr,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009908 const char *bridge, int use_existing)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009909{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009910 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009911 struct i802_bss *bss = priv;
9912 struct wpa_driver_nl80211_data *drv = bss->drv;
9913 int ifidx;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009914 int added = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009915
9916 if (addr)
9917 os_memcpy(if_addr, addr, ETH_ALEN);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009918 nlmode = wpa_driver_nl80211_if_type(type);
9919 if (nlmode == NL80211_IFTYPE_P2P_DEVICE) {
9920 struct wdev_info p2pdev_info;
9921
9922 os_memset(&p2pdev_info, 0, sizeof(p2pdev_info));
9923 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
9924 0, nl80211_wdev_handler,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009925 &p2pdev_info, use_existing);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009926 if (!p2pdev_info.wdev_id_set || ifidx != 0) {
9927 wpa_printf(MSG_ERROR, "nl80211: Failed to create a P2P Device interface %s",
9928 ifname);
9929 return -1;
9930 }
9931
9932 drv->global->if_add_wdevid = p2pdev_info.wdev_id;
9933 drv->global->if_add_wdevid_set = p2pdev_info.wdev_id_set;
9934 if (!is_zero_ether_addr(p2pdev_info.macaddr))
9935 os_memcpy(if_addr, p2pdev_info.macaddr, ETH_ALEN);
9936 wpa_printf(MSG_DEBUG, "nl80211: New P2P Device interface %s (0x%llx) created",
9937 ifname,
9938 (long long unsigned int) p2pdev_info.wdev_id);
9939 } else {
9940 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009941 0, NULL, NULL, use_existing);
9942 if (use_existing && ifidx == -ENFILE) {
9943 added = 0;
9944 ifidx = if_nametoindex(ifname);
9945 } else if (ifidx < 0) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009946 return -1;
9947 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009948 }
9949
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009950 if (!addr) {
9951 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
9952 os_memcpy(if_addr, bss->addr, ETH_ALEN);
9953 else if (linux_get_ifhwaddr(drv->global->ioctl_sock,
9954 bss->ifname, if_addr) < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009955 if (added)
9956 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009957 return -1;
9958 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009959 }
9960
9961#ifdef CONFIG_P2P
9962 if (!addr &&
9963 (type == WPA_IF_P2P_CLIENT || type == WPA_IF_P2P_GROUP ||
9964 type == WPA_IF_P2P_GO)) {
9965 /* Enforce unique P2P Interface Address */
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009966 u8 new_addr[ETH_ALEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009967
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009968 if (linux_get_ifhwaddr(drv->global->ioctl_sock, ifname,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009969 new_addr) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009970 nl80211_remove_iface(drv, ifidx);
9971 return -1;
9972 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009973 if (nl80211_addr_in_use(drv->global, new_addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009974 wpa_printf(MSG_DEBUG, "nl80211: Allocate new address "
9975 "for P2P group interface");
9976 if (nl80211_p2p_interface_addr(drv, new_addr) < 0) {
9977 nl80211_remove_iface(drv, ifidx);
9978 return -1;
9979 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009980 if (linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009981 new_addr) < 0) {
9982 nl80211_remove_iface(drv, ifidx);
9983 return -1;
9984 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009985 }
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07009986 os_memcpy(if_addr, new_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009987 }
9988#endif /* CONFIG_P2P */
9989
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009990 if (type == WPA_IF_AP_BSS) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07009991 struct i802_bss *new_bss = os_zalloc(sizeof(*new_bss));
9992 if (new_bss == NULL) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009993 if (added)
9994 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07009995 return -1;
9996 }
9997
9998 if (bridge &&
9999 i802_check_bridge(drv, new_bss, bridge, ifname) < 0) {
10000 wpa_printf(MSG_ERROR, "nl80211: Failed to add the new "
10001 "interface %s to a bridge %s",
10002 ifname, bridge);
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010003 if (added)
10004 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070010005 os_free(new_bss);
10006 return -1;
10007 }
10008
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010009 if (linux_set_iface_flags(drv->global->ioctl_sock, ifname, 1))
10010 {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010011 nl80211_remove_iface(drv, ifidx);
10012 os_free(new_bss);
10013 return -1;
10014 }
10015 os_strlcpy(new_bss->ifname, ifname, IFNAMSIZ);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010016 os_memcpy(new_bss->addr, if_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010017 new_bss->ifindex = ifidx;
10018 new_bss->drv = drv;
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010019 new_bss->next = drv->first_bss->next;
10020 new_bss->freq = drv->first_bss->freq;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080010021 new_bss->ctx = bss_ctx;
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010022 new_bss->added_if = added;
10023 drv->first_bss->next = new_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010024 if (drv_priv)
10025 *drv_priv = new_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010026 nl80211_init_bss(new_bss);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080010027
10028 /* Subscribe management frames for this WPA_IF_AP_BSS */
10029 if (nl80211_setup_ap(new_bss))
10030 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010031 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010032
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010033 if (drv->global)
10034 drv->global->if_add_ifindex = ifidx;
10035
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -070010036 if (ifidx > 0)
10037 add_ifidx(drv, ifidx);
10038
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010039 return 0;
10040}
10041
10042
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010043static int wpa_driver_nl80211_if_remove(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010044 enum wpa_driver_if_type type,
10045 const char *ifname)
10046{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010047 struct wpa_driver_nl80211_data *drv = bss->drv;
10048 int ifindex = if_nametoindex(ifname);
10049
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010050 wpa_printf(MSG_DEBUG, "nl80211: %s(type=%d ifname=%s) ifindex=%d added_if=%d",
10051 __func__, type, ifname, ifindex, bss->added_if);
Dmitry Shmidt01904cf2013-12-05 11:08:35 -080010052 if (ifindex > 0 && (bss->added_if || bss->ifindex != ifindex))
Dmitry Shmidt051af732013-10-22 13:52:46 -070010053 nl80211_remove_iface(drv, ifindex);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -070010054 else if (ifindex > 0 && !bss->added_if)
10055 del_ifidx(drv, ifindex);
Dmitry Shmidtaa532512012-09-24 10:35:31 -070010056
Dmitry Shmidtaa532512012-09-24 10:35:31 -070010057 if (type != WPA_IF_AP_BSS)
10058 return 0;
10059
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010060 if (bss->added_if_into_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010061 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
10062 bss->ifname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010063 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
10064 "interface %s from bridge %s: %s",
10065 bss->ifname, bss->brname, strerror(errno));
10066 }
10067 if (bss->added_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010068 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010069 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
10070 "bridge %s: %s",
10071 bss->brname, strerror(errno));
10072 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010073
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010074 if (bss != drv->first_bss) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010075 struct i802_bss *tbss;
10076
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010077 wpa_printf(MSG_DEBUG, "nl80211: Not the first BSS - remove it");
10078 for (tbss = drv->first_bss; tbss; tbss = tbss->next) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010079 if (tbss->next == bss) {
10080 tbss->next = bss->next;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080010081 /* Unsubscribe management frames */
10082 nl80211_teardown_ap(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010083 nl80211_destroy_bss(bss);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -070010084 if (!bss->added_if)
10085 i802_set_iface_flags(bss, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010086 os_free(bss);
10087 bss = NULL;
10088 break;
10089 }
10090 }
10091 if (bss)
10092 wpa_printf(MSG_INFO, "nl80211: %s - could not find "
10093 "BSS %p in the list", __func__, bss);
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010094 } else {
10095 wpa_printf(MSG_DEBUG, "nl80211: First BSS - reassign context");
10096 nl80211_teardown_ap(bss);
10097 if (!bss->added_if && !drv->first_bss->next)
10098 wpa_driver_nl80211_del_beacon(drv);
10099 nl80211_destroy_bss(bss);
10100 if (!bss->added_if)
10101 i802_set_iface_flags(bss, 0);
10102 if (drv->first_bss->next) {
10103 drv->first_bss = drv->first_bss->next;
10104 drv->ctx = drv->first_bss->ctx;
10105 os_free(bss);
10106 } else {
10107 wpa_printf(MSG_DEBUG, "nl80211: No second BSS to reassign context to");
10108 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010109 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010110
10111 return 0;
10112}
10113
10114
10115static int cookie_handler(struct nl_msg *msg, void *arg)
10116{
10117 struct nlattr *tb[NL80211_ATTR_MAX + 1];
10118 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
10119 u64 *cookie = arg;
10120 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
10121 genlmsg_attrlen(gnlh, 0), NULL);
10122 if (tb[NL80211_ATTR_COOKIE])
10123 *cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
10124 return NL_SKIP;
10125}
10126
10127
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010128static int nl80211_send_frame_cmd(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010129 unsigned int freq, unsigned int wait,
10130 const u8 *buf, size_t buf_len,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010131 u64 *cookie_out, int no_cck, int no_ack,
10132 int offchanok)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010133{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010134 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010135 struct nl_msg *msg;
10136 u64 cookie;
10137 int ret = -1;
10138
10139 msg = nlmsg_alloc();
10140 if (!msg)
10141 return -1;
10142
Dmitry Shmidt8da800a2013-04-24 12:57:01 -070010143 wpa_printf(MSG_MSGDUMP, "nl80211: CMD_FRAME freq=%u wait=%u no_cck=%d "
Dmitry Shmidt04949592012-07-19 12:16:46 -070010144 "no_ack=%d offchanok=%d",
10145 freq, wait, no_cck, no_ack, offchanok);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -070010146 wpa_hexdump(MSG_MSGDUMP, "CMD_FRAME", buf, buf_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010147 nl80211_cmd(drv, msg, 0, NL80211_CMD_FRAME);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010148
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010149 if (nl80211_set_iface_id(msg, bss) < 0)
10150 goto nla_put_failure;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -070010151 if (freq)
10152 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
Dmitry Shmidt6e933c12011-09-27 12:29:26 -070010153 if (wait)
10154 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, wait);
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -080010155 if (offchanok && ((drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
10156 drv->test_use_roc_tx))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010157 NLA_PUT_FLAG(msg, NL80211_ATTR_OFFCHANNEL_TX_OK);
10158 if (no_cck)
10159 NLA_PUT_FLAG(msg, NL80211_ATTR_TX_NO_CCK_RATE);
10160 if (no_ack)
10161 NLA_PUT_FLAG(msg, NL80211_ATTR_DONT_WAIT_FOR_ACK);
10162
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010163 NLA_PUT(msg, NL80211_ATTR_FRAME, buf_len, buf);
10164
10165 cookie = 0;
10166 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
10167 msg = NULL;
10168 if (ret) {
10169 wpa_printf(MSG_DEBUG, "nl80211: Frame command failed: ret=%d "
Dmitry Shmidt6e933c12011-09-27 12:29:26 -070010170 "(%s) (freq=%u wait=%u)", ret, strerror(-ret),
10171 freq, wait);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010172 goto nla_put_failure;
10173 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -070010174 wpa_printf(MSG_MSGDUMP, "nl80211: Frame TX command accepted%s; "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010175 "cookie 0x%llx", no_ack ? " (no ACK)" : "",
10176 (long long unsigned int) cookie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010177
10178 if (cookie_out)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010179 *cookie_out = no_ack ? (u64) -1 : cookie;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010180
10181nla_put_failure:
10182 nlmsg_free(msg);
10183 return ret;
10184}
10185
10186
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010187static int wpa_driver_nl80211_send_action(struct i802_bss *bss,
10188 unsigned int freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010189 unsigned int wait_time,
10190 const u8 *dst, const u8 *src,
10191 const u8 *bssid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010192 const u8 *data, size_t data_len,
10193 int no_cck)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010194{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010195 struct wpa_driver_nl80211_data *drv = bss->drv;
10196 int ret = -1;
10197 u8 *buf;
10198 struct ieee80211_hdr *hdr;
10199
10200 wpa_printf(MSG_DEBUG, "nl80211: Send Action frame (ifindex=%d, "
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080010201 "freq=%u MHz wait=%d ms no_cck=%d)",
10202 drv->ifindex, freq, wait_time, no_cck);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010203
10204 buf = os_zalloc(24 + data_len);
10205 if (buf == NULL)
10206 return ret;
10207 os_memcpy(buf + 24, data, data_len);
10208 hdr = (struct ieee80211_hdr *) buf;
10209 hdr->frame_control =
10210 IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_ACTION);
10211 os_memcpy(hdr->addr1, dst, ETH_ALEN);
10212 os_memcpy(hdr->addr2, src, ETH_ALEN);
10213 os_memcpy(hdr->addr3, bssid, ETH_ALEN);
10214
Dmitry Shmidt56052862013-10-04 10:23:25 -070010215 if (is_ap_interface(drv->nlmode) &&
10216 (!(drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
10217 (int) freq == bss->freq || drv->device_ap_sme ||
10218 !drv->use_monitor))
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010219 ret = wpa_driver_nl80211_send_mlme(bss, buf, 24 + data_len,
10220 0, freq, no_cck, 1,
10221 wait_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010222 else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010223 ret = nl80211_send_frame_cmd(bss, freq, wait_time, buf,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010224 24 + data_len,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010225 &drv->send_action_cookie,
10226 no_cck, 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010227
10228 os_free(buf);
10229 return ret;
10230}
10231
10232
10233static void wpa_driver_nl80211_send_action_cancel_wait(void *priv)
10234{
10235 struct i802_bss *bss = priv;
10236 struct wpa_driver_nl80211_data *drv = bss->drv;
10237 struct nl_msg *msg;
10238 int ret;
10239
10240 msg = nlmsg_alloc();
10241 if (!msg)
10242 return;
10243
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -080010244 wpa_printf(MSG_DEBUG, "nl80211: Cancel TX frame wait: cookie=0x%llx",
10245 (long long unsigned int) drv->send_action_cookie);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010246 nl80211_cmd(drv, msg, 0, NL80211_CMD_FRAME_WAIT_CANCEL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010247
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010248 if (nl80211_set_iface_id(msg, bss) < 0)
10249 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010250 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->send_action_cookie);
10251
10252 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
10253 msg = NULL;
10254 if (ret)
10255 wpa_printf(MSG_DEBUG, "nl80211: wait cancel failed: ret=%d "
10256 "(%s)", ret, strerror(-ret));
10257
10258 nla_put_failure:
10259 nlmsg_free(msg);
10260}
10261
10262
10263static int wpa_driver_nl80211_remain_on_channel(void *priv, unsigned int freq,
10264 unsigned int duration)
10265{
10266 struct i802_bss *bss = priv;
10267 struct wpa_driver_nl80211_data *drv = bss->drv;
10268 struct nl_msg *msg;
10269 int ret;
10270 u64 cookie;
10271
10272 msg = nlmsg_alloc();
10273 if (!msg)
10274 return -1;
10275
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010276 nl80211_cmd(drv, msg, 0, NL80211_CMD_REMAIN_ON_CHANNEL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010277
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010278 if (nl80211_set_iface_id(msg, bss) < 0)
10279 goto nla_put_failure;
10280
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010281 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
10282 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
10283
10284 cookie = 0;
10285 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010286 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010287 if (ret == 0) {
10288 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel cookie "
10289 "0x%llx for freq=%u MHz duration=%u",
10290 (long long unsigned int) cookie, freq, duration);
10291 drv->remain_on_chan_cookie = cookie;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010292 drv->pending_remain_on_chan = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010293 return 0;
10294 }
10295 wpa_printf(MSG_DEBUG, "nl80211: Failed to request remain-on-channel "
10296 "(freq=%d duration=%u): %d (%s)",
10297 freq, duration, ret, strerror(-ret));
10298nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010299 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010300 return -1;
10301}
10302
10303
10304static int wpa_driver_nl80211_cancel_remain_on_channel(void *priv)
10305{
10306 struct i802_bss *bss = priv;
10307 struct wpa_driver_nl80211_data *drv = bss->drv;
10308 struct nl_msg *msg;
10309 int ret;
10310
10311 if (!drv->pending_remain_on_chan) {
10312 wpa_printf(MSG_DEBUG, "nl80211: No pending remain-on-channel "
10313 "to cancel");
10314 return -1;
10315 }
10316
10317 wpa_printf(MSG_DEBUG, "nl80211: Cancel remain-on-channel with cookie "
10318 "0x%llx",
10319 (long long unsigned int) drv->remain_on_chan_cookie);
10320
10321 msg = nlmsg_alloc();
10322 if (!msg)
10323 return -1;
10324
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010325 nl80211_cmd(drv, msg, 0, NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010326
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010327 if (nl80211_set_iface_id(msg, bss) < 0)
10328 goto nla_put_failure;
10329
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010330 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->remain_on_chan_cookie);
10331
10332 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010333 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010334 if (ret == 0)
10335 return 0;
10336 wpa_printf(MSG_DEBUG, "nl80211: Failed to cancel remain-on-channel: "
10337 "%d (%s)", ret, strerror(-ret));
10338nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010339 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010340 return -1;
10341}
10342
10343
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010344static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss, int report)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010345{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010346 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt6e933c12011-09-27 12:29:26 -070010347
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010348 if (!report) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010349 if (bss->nl_preq && drv->device_ap_sme &&
10350 is_ap_interface(drv->nlmode)) {
10351 /*
10352 * Do not disable Probe Request reporting that was
10353 * enabled in nl80211_setup_ap().
10354 */
10355 wpa_printf(MSG_DEBUG, "nl80211: Skip disabling of "
10356 "Probe Request reporting nl_preq=%p while "
10357 "in AP mode", bss->nl_preq);
10358 } else if (bss->nl_preq) {
10359 wpa_printf(MSG_DEBUG, "nl80211: Disable Probe Request "
10360 "reporting nl_preq=%p", bss->nl_preq);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070010361 nl80211_destroy_eloop_handle(&bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010362 }
10363 return 0;
10364 }
10365
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010366 if (bss->nl_preq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010367 wpa_printf(MSG_DEBUG, "nl80211: Probe Request reporting "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010368 "already on! nl_preq=%p", bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010369 return 0;
10370 }
10371
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010372 bss->nl_preq = nl_create_handle(drv->global->nl_cb, "preq");
10373 if (bss->nl_preq == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010374 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010375 wpa_printf(MSG_DEBUG, "nl80211: Enable Probe Request "
10376 "reporting nl_preq=%p", bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010377
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010378 if (nl80211_register_frame(bss, bss->nl_preq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010379 (WLAN_FC_TYPE_MGMT << 2) |
10380 (WLAN_FC_STYPE_PROBE_REQ << 4),
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010381 NULL, 0) < 0)
10382 goto out_err;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -070010383
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070010384 nl80211_register_eloop_read(&bss->nl_preq,
10385 wpa_driver_nl80211_event_receive,
10386 bss->nl_cb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010387
10388 return 0;
10389
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010390 out_err:
10391 nl_destroy_handles(&bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010392 return -1;
10393}
10394
10395
10396static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
10397 int ifindex, int disabled)
10398{
10399 struct nl_msg *msg;
10400 struct nlattr *bands, *band;
10401 int ret;
10402
10403 msg = nlmsg_alloc();
10404 if (!msg)
10405 return -1;
10406
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010407 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_TX_BITRATE_MASK);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010408 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
10409
10410 bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES);
10411 if (!bands)
10412 goto nla_put_failure;
10413
10414 /*
10415 * Disable 2 GHz rates 1, 2, 5.5, 11 Mbps by masking out everything
10416 * else apart from 6, 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS
10417 * rates. All 5 GHz rates are left enabled.
10418 */
10419 band = nla_nest_start(msg, NL80211_BAND_2GHZ);
10420 if (!band)
10421 goto nla_put_failure;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010422 if (disabled) {
10423 NLA_PUT(msg, NL80211_TXRATE_LEGACY, 8,
10424 "\x0c\x12\x18\x24\x30\x48\x60\x6c");
10425 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010426 nla_nest_end(msg, band);
10427
10428 nla_nest_end(msg, bands);
10429
10430 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
10431 msg = NULL;
10432 if (ret) {
10433 wpa_printf(MSG_DEBUG, "nl80211: Set TX rates failed: ret=%d "
10434 "(%s)", ret, strerror(-ret));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070010435 } else
10436 drv->disabled_11b_rates = disabled;
10437
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010438 return ret;
10439
10440nla_put_failure:
10441 nlmsg_free(msg);
10442 return -1;
10443}
10444
10445
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010446static int wpa_driver_nl80211_deinit_ap(void *priv)
10447{
10448 struct i802_bss *bss = priv;
10449 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010450 if (!is_ap_interface(drv->nlmode))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010451 return -1;
10452 wpa_driver_nl80211_del_beacon(drv);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070010453
10454 /*
10455 * If the P2P GO interface was dynamically added, then it is
10456 * possible that the interface change to station is not possible.
10457 */
10458 if (drv->nlmode == NL80211_IFTYPE_P2P_GO && bss->if_dynamic)
10459 return 0;
10460
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010461 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010462}
10463
10464
Dmitry Shmidtea69e842013-05-13 14:52:28 -070010465static int wpa_driver_nl80211_stop_ap(void *priv)
10466{
10467 struct i802_bss *bss = priv;
10468 struct wpa_driver_nl80211_data *drv = bss->drv;
10469 if (!is_ap_interface(drv->nlmode))
10470 return -1;
10471 wpa_driver_nl80211_del_beacon(drv);
10472 bss->beacon_set = 0;
10473 return 0;
10474}
10475
10476
Dmitry Shmidt04949592012-07-19 12:16:46 -070010477static int wpa_driver_nl80211_deinit_p2p_cli(void *priv)
10478{
10479 struct i802_bss *bss = priv;
10480 struct wpa_driver_nl80211_data *drv = bss->drv;
10481 if (drv->nlmode != NL80211_IFTYPE_P2P_CLIENT)
10482 return -1;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070010483
10484 /*
10485 * If the P2P Client interface was dynamically added, then it is
10486 * possible that the interface change to station is not possible.
10487 */
10488 if (bss->if_dynamic)
10489 return 0;
10490
Dmitry Shmidt04949592012-07-19 12:16:46 -070010491 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
10492}
10493
10494
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010495static void wpa_driver_nl80211_resume(void *priv)
10496{
10497 struct i802_bss *bss = priv;
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010498
10499 if (i802_set_iface_flags(bss, 1))
10500 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface up on resume event");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010501}
10502
10503
10504static int nl80211_send_ft_action(void *priv, u8 action, const u8 *target_ap,
10505 const u8 *ies, size_t ies_len)
10506{
10507 struct i802_bss *bss = priv;
10508 struct wpa_driver_nl80211_data *drv = bss->drv;
10509 int ret;
10510 u8 *data, *pos;
10511 size_t data_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010512 const u8 *own_addr = bss->addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010513
10514 if (action != 1) {
10515 wpa_printf(MSG_ERROR, "nl80211: Unsupported send_ft_action "
10516 "action %d", action);
10517 return -1;
10518 }
10519
10520 /*
10521 * Action frame payload:
10522 * Category[1] = 6 (Fast BSS Transition)
10523 * Action[1] = 1 (Fast BSS Transition Request)
10524 * STA Address
10525 * Target AP Address
10526 * FT IEs
10527 */
10528
10529 data_len = 2 + 2 * ETH_ALEN + ies_len;
10530 data = os_malloc(data_len);
10531 if (data == NULL)
10532 return -1;
10533 pos = data;
10534 *pos++ = 0x06; /* FT Action category */
10535 *pos++ = action;
10536 os_memcpy(pos, own_addr, ETH_ALEN);
10537 pos += ETH_ALEN;
10538 os_memcpy(pos, target_ap, ETH_ALEN);
10539 pos += ETH_ALEN;
10540 os_memcpy(pos, ies, ies_len);
10541
10542 ret = wpa_driver_nl80211_send_action(bss, drv->assoc_freq, 0,
10543 drv->bssid, own_addr, drv->bssid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010544 data, data_len, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010545 os_free(data);
10546
10547 return ret;
10548}
10549
10550
10551static int nl80211_signal_monitor(void *priv, int threshold, int hysteresis)
10552{
10553 struct i802_bss *bss = priv;
10554 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -070010555 struct nl_msg *msg;
10556 struct nlattr *cqm;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070010557 int ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010558
10559 wpa_printf(MSG_DEBUG, "nl80211: Signal monitor threshold=%d "
10560 "hysteresis=%d", threshold, hysteresis);
10561
10562 msg = nlmsg_alloc();
10563 if (!msg)
10564 return -1;
10565
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010566 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_CQM);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010567
10568 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
10569
Dmitry Shmidt8da800a2013-04-24 12:57:01 -070010570 cqm = nla_nest_start(msg, NL80211_ATTR_CQM);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010571 if (cqm == NULL)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070010572 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010573
Dmitry Shmidt8da800a2013-04-24 12:57:01 -070010574 NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_THOLD, threshold);
10575 NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_HYST, hysteresis);
10576 nla_nest_end(msg, cqm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010577
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070010578 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010579 msg = NULL;
10580
10581nla_put_failure:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010582 nlmsg_free(msg);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070010583 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010584}
10585
10586
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010587static int get_channel_width(struct nl_msg *msg, void *arg)
10588{
10589 struct nlattr *tb[NL80211_ATTR_MAX + 1];
10590 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
10591 struct wpa_signal_info *sig_change = arg;
10592
10593 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
10594 genlmsg_attrlen(gnlh, 0), NULL);
10595
10596 sig_change->center_frq1 = -1;
10597 sig_change->center_frq2 = -1;
10598 sig_change->chanwidth = CHAN_WIDTH_UNKNOWN;
10599
10600 if (tb[NL80211_ATTR_CHANNEL_WIDTH]) {
10601 sig_change->chanwidth = convert2width(
10602 nla_get_u32(tb[NL80211_ATTR_CHANNEL_WIDTH]));
10603 if (tb[NL80211_ATTR_CENTER_FREQ1])
10604 sig_change->center_frq1 =
10605 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]);
10606 if (tb[NL80211_ATTR_CENTER_FREQ2])
10607 sig_change->center_frq2 =
10608 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]);
10609 }
10610
10611 return NL_SKIP;
10612}
10613
10614
10615static int nl80211_get_channel_width(struct wpa_driver_nl80211_data *drv,
10616 struct wpa_signal_info *sig)
10617{
10618 struct nl_msg *msg;
10619
10620 msg = nlmsg_alloc();
10621 if (!msg)
10622 return -ENOMEM;
10623
10624 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_INTERFACE);
10625 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
10626
10627 return send_and_recv_msgs(drv, msg, get_channel_width, sig);
10628
10629nla_put_failure:
10630 nlmsg_free(msg);
10631 return -ENOBUFS;
10632}
10633
10634
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010635static int nl80211_signal_poll(void *priv, struct wpa_signal_info *si)
10636{
10637 struct i802_bss *bss = priv;
10638 struct wpa_driver_nl80211_data *drv = bss->drv;
10639 int res;
10640
10641 os_memset(si, 0, sizeof(*si));
10642 res = nl80211_get_link_signal(drv, si);
10643 if (res != 0)
10644 return res;
10645
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010646 res = nl80211_get_channel_width(drv, si);
10647 if (res != 0)
10648 return res;
10649
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010650 return nl80211_get_link_noise(drv, si);
10651}
10652
10653
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010654static int wpa_driver_nl80211_shared_freq(void *priv)
10655{
10656 struct i802_bss *bss = priv;
10657 struct wpa_driver_nl80211_data *drv = bss->drv;
10658 struct wpa_driver_nl80211_data *driver;
10659 int freq = 0;
10660
10661 /*
10662 * If the same PHY is in connected state with some other interface,
10663 * then retrieve the assoc freq.
10664 */
10665 wpa_printf(MSG_DEBUG, "nl80211: Get shared freq for PHY %s",
10666 drv->phyname);
10667
10668 dl_list_for_each(driver, &drv->global->interfaces,
10669 struct wpa_driver_nl80211_data, list) {
10670 if (drv == driver ||
10671 os_strcmp(drv->phyname, driver->phyname) != 0 ||
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010672 !driver->associated)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010673 continue;
10674
10675 wpa_printf(MSG_DEBUG, "nl80211: Found a match for PHY %s - %s "
10676 MACSTR,
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010677 driver->phyname, driver->first_bss->ifname,
10678 MAC2STR(driver->first_bss->addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -070010679 if (is_ap_interface(driver->nlmode))
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010680 freq = driver->first_bss->freq;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010681 else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010682 freq = nl80211_get_assoc_freq(driver);
10683 wpa_printf(MSG_DEBUG, "nl80211: Shared freq for PHY %s: %d",
10684 drv->phyname, freq);
10685 }
10686
10687 if (!freq)
10688 wpa_printf(MSG_DEBUG, "nl80211: No shared interface for "
10689 "PHY (%s) in associated state", drv->phyname);
10690
10691 return freq;
10692}
10693
10694
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010695static int nl80211_send_frame(void *priv, const u8 *data, size_t data_len,
10696 int encrypt)
10697{
10698 struct i802_bss *bss = priv;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080010699 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt, 0,
10700 0, 0, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010701}
10702
10703
10704static int nl80211_set_param(void *priv, const char *param)
10705{
10706 wpa_printf(MSG_DEBUG, "nl80211: driver param='%s'", param);
10707 if (param == NULL)
10708 return 0;
10709
10710#ifdef CONFIG_P2P
10711 if (os_strstr(param, "use_p2p_group_interface=1")) {
10712 struct i802_bss *bss = priv;
10713 struct wpa_driver_nl80211_data *drv = bss->drv;
10714
10715 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
10716 "interface");
10717 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
10718 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
10719 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010720
10721 if (os_strstr(param, "p2p_device=1")) {
10722 struct i802_bss *bss = priv;
10723 struct wpa_driver_nl80211_data *drv = bss->drv;
10724 drv->allow_p2p_device = 1;
10725 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010726#endif /* CONFIG_P2P */
10727
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080010728 if (os_strstr(param, "use_monitor=1")) {
10729 struct i802_bss *bss = priv;
10730 struct wpa_driver_nl80211_data *drv = bss->drv;
10731 drv->use_monitor = 1;
10732 }
10733
10734 if (os_strstr(param, "force_connect_cmd=1")) {
10735 struct i802_bss *bss = priv;
10736 struct wpa_driver_nl80211_data *drv = bss->drv;
10737 drv->capa.flags &= ~WPA_DRIVER_FLAGS_SME;
10738 }
10739
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -080010740 if (os_strstr(param, "no_offchannel_tx=1")) {
10741 struct i802_bss *bss = priv;
10742 struct wpa_driver_nl80211_data *drv = bss->drv;
10743 drv->capa.flags &= ~WPA_DRIVER_FLAGS_OFFCHANNEL_TX;
10744 drv->test_use_roc_tx = 1;
10745 }
10746
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010747 return 0;
10748}
10749
10750
10751static void * nl80211_global_init(void)
10752{
10753 struct nl80211_global *global;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010754 struct netlink_config *cfg;
10755
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010756 global = os_zalloc(sizeof(*global));
10757 if (global == NULL)
10758 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010759 global->ioctl_sock = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010760 dl_list_init(&global->interfaces);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010761 global->if_add_ifindex = -1;
10762
10763 cfg = os_zalloc(sizeof(*cfg));
10764 if (cfg == NULL)
10765 goto err;
10766
10767 cfg->ctx = global;
10768 cfg->newlink_cb = wpa_driver_nl80211_event_rtm_newlink;
10769 cfg->dellink_cb = wpa_driver_nl80211_event_rtm_dellink;
10770 global->netlink = netlink_init(cfg);
10771 if (global->netlink == NULL) {
10772 os_free(cfg);
10773 goto err;
10774 }
10775
10776 if (wpa_driver_nl80211_init_nl_global(global) < 0)
10777 goto err;
10778
10779 global->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
10780 if (global->ioctl_sock < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070010781 wpa_printf(MSG_ERROR, "nl80211: socket(PF_INET,SOCK_DGRAM) failed: %s",
10782 strerror(errno));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010783 goto err;
10784 }
10785
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010786 return global;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010787
10788err:
10789 nl80211_global_deinit(global);
10790 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010791}
10792
10793
10794static void nl80211_global_deinit(void *priv)
10795{
10796 struct nl80211_global *global = priv;
10797 if (global == NULL)
10798 return;
10799 if (!dl_list_empty(&global->interfaces)) {
10800 wpa_printf(MSG_ERROR, "nl80211: %u interface(s) remain at "
10801 "nl80211_global_deinit",
10802 dl_list_len(&global->interfaces));
10803 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010804
10805 if (global->netlink)
10806 netlink_deinit(global->netlink);
10807
10808 nl_destroy_handles(&global->nl);
10809
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070010810 if (global->nl_event)
10811 nl80211_destroy_eloop_handle(&global->nl_event);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010812
10813 nl_cb_put(global->nl_cb);
10814
10815 if (global->ioctl_sock >= 0)
10816 close(global->ioctl_sock);
10817
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010818 os_free(global);
10819}
10820
10821
10822static const char * nl80211_get_radio_name(void *priv)
10823{
10824 struct i802_bss *bss = priv;
10825 struct wpa_driver_nl80211_data *drv = bss->drv;
10826 return drv->phyname;
10827}
10828
10829
Jouni Malinen75ecf522011-06-27 15:19:46 -070010830static int nl80211_pmkid(struct i802_bss *bss, int cmd, const u8 *bssid,
10831 const u8 *pmkid)
10832{
10833 struct nl_msg *msg;
10834
10835 msg = nlmsg_alloc();
10836 if (!msg)
10837 return -ENOMEM;
10838
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010839 nl80211_cmd(bss->drv, msg, 0, cmd);
Jouni Malinen75ecf522011-06-27 15:19:46 -070010840
10841 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
10842 if (pmkid)
10843 NLA_PUT(msg, NL80211_ATTR_PMKID, 16, pmkid);
10844 if (bssid)
10845 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
10846
10847 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
10848 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010849 nlmsg_free(msg);
Jouni Malinen75ecf522011-06-27 15:19:46 -070010850 return -ENOBUFS;
10851}
10852
10853
10854static int nl80211_add_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
10855{
10856 struct i802_bss *bss = priv;
10857 wpa_printf(MSG_DEBUG, "nl80211: Add PMKID for " MACSTR, MAC2STR(bssid));
10858 return nl80211_pmkid(bss, NL80211_CMD_SET_PMKSA, bssid, pmkid);
10859}
10860
10861
10862static int nl80211_remove_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
10863{
10864 struct i802_bss *bss = priv;
10865 wpa_printf(MSG_DEBUG, "nl80211: Delete PMKID for " MACSTR,
10866 MAC2STR(bssid));
10867 return nl80211_pmkid(bss, NL80211_CMD_DEL_PMKSA, bssid, pmkid);
10868}
10869
10870
10871static int nl80211_flush_pmkid(void *priv)
10872{
10873 struct i802_bss *bss = priv;
10874 wpa_printf(MSG_DEBUG, "nl80211: Flush PMKIDs");
10875 return nl80211_pmkid(bss, NL80211_CMD_FLUSH_PMKSA, NULL, NULL);
10876}
10877
10878
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070010879static void clean_survey_results(struct survey_results *survey_results)
10880{
10881 struct freq_survey *survey, *tmp;
10882
10883 if (dl_list_empty(&survey_results->survey_list))
10884 return;
10885
10886 dl_list_for_each_safe(survey, tmp, &survey_results->survey_list,
10887 struct freq_survey, list) {
10888 dl_list_del(&survey->list);
10889 os_free(survey);
10890 }
10891}
10892
10893
10894static void add_survey(struct nlattr **sinfo, u32 ifidx,
10895 struct dl_list *survey_list)
10896{
10897 struct freq_survey *survey;
10898
10899 survey = os_zalloc(sizeof(struct freq_survey));
10900 if (!survey)
10901 return;
10902
10903 survey->ifidx = ifidx;
10904 survey->freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
10905 survey->filled = 0;
10906
10907 if (sinfo[NL80211_SURVEY_INFO_NOISE]) {
10908 survey->nf = (int8_t)
10909 nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
10910 survey->filled |= SURVEY_HAS_NF;
10911 }
10912
10913 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]) {
10914 survey->channel_time =
10915 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]);
10916 survey->filled |= SURVEY_HAS_CHAN_TIME;
10917 }
10918
10919 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]) {
10920 survey->channel_time_busy =
10921 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]);
10922 survey->filled |= SURVEY_HAS_CHAN_TIME_BUSY;
10923 }
10924
10925 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]) {
10926 survey->channel_time_rx =
10927 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]);
10928 survey->filled |= SURVEY_HAS_CHAN_TIME_RX;
10929 }
10930
10931 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]) {
10932 survey->channel_time_tx =
10933 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]);
10934 survey->filled |= SURVEY_HAS_CHAN_TIME_TX;
10935 }
10936
10937 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)",
10938 survey->freq,
10939 survey->nf,
10940 (unsigned long int) survey->channel_time,
10941 (unsigned long int) survey->channel_time_busy,
10942 (unsigned long int) survey->channel_time_tx,
10943 (unsigned long int) survey->channel_time_rx,
10944 survey->filled);
10945
10946 dl_list_add_tail(survey_list, &survey->list);
10947}
10948
10949
10950static int check_survey_ok(struct nlattr **sinfo, u32 surveyed_freq,
10951 unsigned int freq_filter)
10952{
10953 if (!freq_filter)
10954 return 1;
10955
10956 return freq_filter == surveyed_freq;
10957}
10958
10959
10960static int survey_handler(struct nl_msg *msg, void *arg)
10961{
10962 struct nlattr *tb[NL80211_ATTR_MAX + 1];
10963 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
10964 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
10965 struct survey_results *survey_results;
10966 u32 surveyed_freq = 0;
10967 u32 ifidx;
10968
10969 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
10970 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
10971 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
10972 };
10973
10974 survey_results = (struct survey_results *) arg;
10975
10976 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
10977 genlmsg_attrlen(gnlh, 0), NULL);
10978
Dmitry Shmidt97672262014-02-03 13:02:54 -080010979 if (!tb[NL80211_ATTR_IFINDEX])
10980 return NL_SKIP;
10981
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070010982 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
10983
10984 if (!tb[NL80211_ATTR_SURVEY_INFO])
10985 return NL_SKIP;
10986
10987 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
10988 tb[NL80211_ATTR_SURVEY_INFO],
10989 survey_policy))
10990 return NL_SKIP;
10991
10992 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY]) {
10993 wpa_printf(MSG_ERROR, "nl80211: Invalid survey data");
10994 return NL_SKIP;
10995 }
10996
10997 surveyed_freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
10998
10999 if (!check_survey_ok(sinfo, surveyed_freq,
11000 survey_results->freq_filter))
11001 return NL_SKIP;
11002
11003 if (survey_results->freq_filter &&
11004 survey_results->freq_filter != surveyed_freq) {
11005 wpa_printf(MSG_EXCESSIVE, "nl80211: Ignoring survey data for freq %d MHz",
11006 surveyed_freq);
11007 return NL_SKIP;
11008 }
11009
11010 add_survey(sinfo, ifidx, &survey_results->survey_list);
11011
11012 return NL_SKIP;
11013}
11014
11015
11016static int wpa_driver_nl80211_get_survey(void *priv, unsigned int freq)
11017{
11018 struct i802_bss *bss = priv;
11019 struct wpa_driver_nl80211_data *drv = bss->drv;
11020 struct nl_msg *msg;
11021 int err = -ENOBUFS;
11022 union wpa_event_data data;
11023 struct survey_results *survey_results;
11024
11025 os_memset(&data, 0, sizeof(data));
11026 survey_results = &data.survey_results;
11027
11028 dl_list_init(&survey_results->survey_list);
11029
11030 msg = nlmsg_alloc();
11031 if (!msg)
11032 goto nla_put_failure;
11033
11034 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
11035
11036 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
11037
11038 if (freq)
11039 data.survey_results.freq_filter = freq;
11040
11041 do {
11042 wpa_printf(MSG_DEBUG, "nl80211: Fetch survey data");
11043 err = send_and_recv_msgs(drv, msg, survey_handler,
11044 survey_results);
11045 } while (err > 0);
11046
11047 if (err) {
11048 wpa_printf(MSG_ERROR, "nl80211: Failed to process survey data");
11049 goto out_clean;
11050 }
11051
11052 wpa_supplicant_event(drv->ctx, EVENT_SURVEY, &data);
11053
11054out_clean:
11055 clean_survey_results(survey_results);
11056nla_put_failure:
11057 return err;
11058}
11059
11060
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011061static void nl80211_set_rekey_info(void *priv, const u8 *kek, const u8 *kck,
11062 const u8 *replay_ctr)
11063{
11064 struct i802_bss *bss = priv;
11065 struct wpa_driver_nl80211_data *drv = bss->drv;
11066 struct nlattr *replay_nested;
11067 struct nl_msg *msg;
11068
11069 msg = nlmsg_alloc();
11070 if (!msg)
11071 return;
11072
11073 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
11074
11075 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
11076
11077 replay_nested = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
11078 if (!replay_nested)
11079 goto nla_put_failure;
11080
11081 NLA_PUT(msg, NL80211_REKEY_DATA_KEK, NL80211_KEK_LEN, kek);
11082 NLA_PUT(msg, NL80211_REKEY_DATA_KCK, NL80211_KCK_LEN, kck);
11083 NLA_PUT(msg, NL80211_REKEY_DATA_REPLAY_CTR, NL80211_REPLAY_CTR_LEN,
11084 replay_ctr);
11085
11086 nla_nest_end(msg, replay_nested);
11087
11088 send_and_recv_msgs(drv, msg, NULL, NULL);
11089 return;
11090 nla_put_failure:
11091 nlmsg_free(msg);
11092}
11093
11094
11095static void nl80211_send_null_frame(struct i802_bss *bss, const u8 *own_addr,
11096 const u8 *addr, int qos)
11097{
11098 /* send data frame to poll STA and check whether
11099 * this frame is ACKed */
11100 struct {
11101 struct ieee80211_hdr hdr;
11102 u16 qos_ctl;
11103 } STRUCT_PACKED nulldata;
11104 size_t size;
11105
11106 /* Send data frame to poll STA and check whether this frame is ACKed */
11107
11108 os_memset(&nulldata, 0, sizeof(nulldata));
11109
11110 if (qos) {
11111 nulldata.hdr.frame_control =
11112 IEEE80211_FC(WLAN_FC_TYPE_DATA,
11113 WLAN_FC_STYPE_QOS_NULL);
11114 size = sizeof(nulldata);
11115 } else {
11116 nulldata.hdr.frame_control =
11117 IEEE80211_FC(WLAN_FC_TYPE_DATA,
11118 WLAN_FC_STYPE_NULLFUNC);
11119 size = sizeof(struct ieee80211_hdr);
11120 }
11121
11122 nulldata.hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS);
11123 os_memcpy(nulldata.hdr.IEEE80211_DA_FROMDS, addr, ETH_ALEN);
11124 os_memcpy(nulldata.hdr.IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
11125 os_memcpy(nulldata.hdr.IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
11126
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011127 if (wpa_driver_nl80211_send_mlme(bss, (u8 *) &nulldata, size, 0, 0, 0,
11128 0, 0) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011129 wpa_printf(MSG_DEBUG, "nl80211_send_null_frame: Failed to "
11130 "send poll frame");
11131}
11132
11133static void nl80211_poll_client(void *priv, const u8 *own_addr, const u8 *addr,
11134 int qos)
11135{
11136 struct i802_bss *bss = priv;
11137 struct wpa_driver_nl80211_data *drv = bss->drv;
11138 struct nl_msg *msg;
11139
11140 if (!drv->poll_command_supported) {
11141 nl80211_send_null_frame(bss, own_addr, addr, qos);
11142 return;
11143 }
11144
11145 msg = nlmsg_alloc();
11146 if (!msg)
11147 return;
11148
11149 nl80211_cmd(drv, msg, 0, NL80211_CMD_PROBE_CLIENT);
11150
11151 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
11152 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
11153
11154 send_and_recv_msgs(drv, msg, NULL, NULL);
11155 return;
11156 nla_put_failure:
11157 nlmsg_free(msg);
11158}
11159
11160
11161static int nl80211_set_power_save(struct i802_bss *bss, int enabled)
11162{
11163 struct nl_msg *msg;
11164
11165 msg = nlmsg_alloc();
11166 if (!msg)
11167 return -ENOMEM;
11168
11169 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_SET_POWER_SAVE);
11170 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
11171 NLA_PUT_U32(msg, NL80211_ATTR_PS_STATE,
11172 enabled ? NL80211_PS_ENABLED : NL80211_PS_DISABLED);
11173 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
11174nla_put_failure:
11175 nlmsg_free(msg);
11176 return -ENOBUFS;
11177}
11178
11179
11180static int nl80211_set_p2p_powersave(void *priv, int legacy_ps, int opp_ps,
11181 int ctwindow)
11182{
11183 struct i802_bss *bss = priv;
11184
11185 wpa_printf(MSG_DEBUG, "nl80211: set_p2p_powersave (legacy_ps=%d "
11186 "opp_ps=%d ctwindow=%d)", legacy_ps, opp_ps, ctwindow);
11187
Dmitry Shmidt292b0c32013-11-22 12:54:42 -080011188 if (opp_ps != -1 || ctwindow != -1) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080011189#ifdef ANDROID_P2P
11190 wpa_driver_set_p2p_ps(priv, legacy_ps, opp_ps, ctwindow);
Dmitry Shmidt292b0c32013-11-22 12:54:42 -080011191#else /* ANDROID_P2P */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011192 return -1; /* Not yet supported */
Dmitry Shmidt292b0c32013-11-22 12:54:42 -080011193#endif /* ANDROID_P2P */
11194 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011195
11196 if (legacy_ps == -1)
11197 return 0;
11198 if (legacy_ps != 0 && legacy_ps != 1)
11199 return -1; /* Not yet supported */
11200
11201 return nl80211_set_power_save(bss, legacy_ps);
11202}
11203
11204
Dmitry Shmidt051af732013-10-22 13:52:46 -070011205static int nl80211_start_radar_detection(void *priv,
11206 struct hostapd_freq_params *freq)
Dmitry Shmidtea69e842013-05-13 14:52:28 -070011207{
11208 struct i802_bss *bss = priv;
11209 struct wpa_driver_nl80211_data *drv = bss->drv;
11210 struct nl_msg *msg;
11211 int ret;
11212
Dmitry Shmidt051af732013-10-22 13:52:46 -070011213 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)",
11214 freq->freq, freq->ht_enabled, freq->vht_enabled,
11215 freq->bandwidth, freq->center_freq1, freq->center_freq2);
11216
Dmitry Shmidtea69e842013-05-13 14:52:28 -070011217 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_RADAR)) {
11218 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support radar "
11219 "detection");
11220 return -1;
11221 }
11222
11223 msg = nlmsg_alloc();
11224 if (!msg)
11225 return -1;
11226
11227 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_RADAR_DETECT);
11228 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
Dmitry Shmidt051af732013-10-22 13:52:46 -070011229 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq->freq);
Dmitry Shmidtea69e842013-05-13 14:52:28 -070011230
Dmitry Shmidt051af732013-10-22 13:52:46 -070011231 if (freq->vht_enabled) {
11232 switch (freq->bandwidth) {
11233 case 20:
11234 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
11235 NL80211_CHAN_WIDTH_20);
11236 break;
11237 case 40:
11238 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
11239 NL80211_CHAN_WIDTH_40);
11240 break;
11241 case 80:
11242 if (freq->center_freq2)
11243 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
11244 NL80211_CHAN_WIDTH_80P80);
11245 else
11246 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
11247 NL80211_CHAN_WIDTH_80);
11248 break;
11249 case 160:
11250 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
11251 NL80211_CHAN_WIDTH_160);
11252 break;
11253 default:
11254 return -1;
11255 }
11256 NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ1, freq->center_freq1);
11257 if (freq->center_freq2)
11258 NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ2,
11259 freq->center_freq2);
11260 } else if (freq->ht_enabled) {
11261 switch (freq->sec_channel_offset) {
11262 case -1:
11263 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
11264 NL80211_CHAN_HT40MINUS);
11265 break;
11266 case 1:
11267 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
11268 NL80211_CHAN_HT40PLUS);
11269 break;
11270 default:
11271 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
11272 NL80211_CHAN_HT20);
11273 break;
11274 }
11275 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -070011276
11277 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
11278 if (ret == 0)
11279 return 0;
11280 wpa_printf(MSG_DEBUG, "nl80211: Failed to start radar detection: "
11281 "%d (%s)", ret, strerror(-ret));
11282nla_put_failure:
11283 return -1;
11284}
11285
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011286#ifdef CONFIG_TDLS
11287
11288static int nl80211_send_tdls_mgmt(void *priv, const u8 *dst, u8 action_code,
11289 u8 dialog_token, u16 status_code,
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -070011290 u32 peer_capab, const u8 *buf, size_t len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011291{
11292 struct i802_bss *bss = priv;
11293 struct wpa_driver_nl80211_data *drv = bss->drv;
11294 struct nl_msg *msg;
11295
11296 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
11297 return -EOPNOTSUPP;
11298
11299 if (!dst)
11300 return -EINVAL;
11301
11302 msg = nlmsg_alloc();
11303 if (!msg)
11304 return -ENOMEM;
11305
11306 nl80211_cmd(drv, msg, 0, NL80211_CMD_TDLS_MGMT);
11307 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
11308 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
11309 NLA_PUT_U8(msg, NL80211_ATTR_TDLS_ACTION, action_code);
11310 NLA_PUT_U8(msg, NL80211_ATTR_TDLS_DIALOG_TOKEN, dialog_token);
11311 NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status_code);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -070011312 if (peer_capab) {
11313 /*
11314 * The internal enum tdls_peer_capability definition is
11315 * currently identical with the nl80211 enum
11316 * nl80211_tdls_peer_capability, so no conversion is needed
11317 * here.
11318 */
11319 NLA_PUT_U32(msg, NL80211_ATTR_TDLS_PEER_CAPABILITY, peer_capab);
11320 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011321 NLA_PUT(msg, NL80211_ATTR_IE, len, buf);
11322
11323 return send_and_recv_msgs(drv, msg, NULL, NULL);
11324
11325nla_put_failure:
11326 nlmsg_free(msg);
11327 return -ENOBUFS;
11328}
11329
11330
11331static int nl80211_tdls_oper(void *priv, enum tdls_oper oper, const u8 *peer)
11332{
11333 struct i802_bss *bss = priv;
11334 struct wpa_driver_nl80211_data *drv = bss->drv;
11335 struct nl_msg *msg;
11336 enum nl80211_tdls_operation nl80211_oper;
11337
11338 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
11339 return -EOPNOTSUPP;
11340
11341 switch (oper) {
11342 case TDLS_DISCOVERY_REQ:
11343 nl80211_oper = NL80211_TDLS_DISCOVERY_REQ;
11344 break;
11345 case TDLS_SETUP:
11346 nl80211_oper = NL80211_TDLS_SETUP;
11347 break;
11348 case TDLS_TEARDOWN:
11349 nl80211_oper = NL80211_TDLS_TEARDOWN;
11350 break;
11351 case TDLS_ENABLE_LINK:
11352 nl80211_oper = NL80211_TDLS_ENABLE_LINK;
11353 break;
11354 case TDLS_DISABLE_LINK:
11355 nl80211_oper = NL80211_TDLS_DISABLE_LINK;
11356 break;
11357 case TDLS_ENABLE:
11358 return 0;
11359 case TDLS_DISABLE:
11360 return 0;
11361 default:
11362 return -EINVAL;
11363 }
11364
11365 msg = nlmsg_alloc();
11366 if (!msg)
11367 return -ENOMEM;
11368
11369 nl80211_cmd(drv, msg, 0, NL80211_CMD_TDLS_OPER);
11370 NLA_PUT_U8(msg, NL80211_ATTR_TDLS_OPERATION, nl80211_oper);
11371 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
11372 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, peer);
11373
11374 return send_and_recv_msgs(drv, msg, NULL, NULL);
11375
11376nla_put_failure:
11377 nlmsg_free(msg);
11378 return -ENOBUFS;
11379}
11380
11381#endif /* CONFIG TDLS */
11382
11383
11384#ifdef ANDROID
11385
11386typedef struct android_wifi_priv_cmd {
11387 char *buf;
11388 int used_len;
11389 int total_len;
11390} android_wifi_priv_cmd;
11391
11392static int drv_errors = 0;
11393
11394static void wpa_driver_send_hang_msg(struct wpa_driver_nl80211_data *drv)
11395{
11396 drv_errors++;
11397 if (drv_errors > DRV_NUMBER_SEQUENTIAL_ERRORS) {
11398 drv_errors = 0;
11399 wpa_msg(drv->ctx, MSG_INFO, WPA_EVENT_DRIVER_STATE "HANGED");
11400 }
11401}
11402
11403
11404static int android_priv_cmd(struct i802_bss *bss, const char *cmd)
11405{
11406 struct wpa_driver_nl80211_data *drv = bss->drv;
11407 struct ifreq ifr;
11408 android_wifi_priv_cmd priv_cmd;
11409 char buf[MAX_DRV_CMD_SIZE];
11410 int ret;
11411
11412 os_memset(&ifr, 0, sizeof(ifr));
11413 os_memset(&priv_cmd, 0, sizeof(priv_cmd));
11414 os_strlcpy(ifr.ifr_name, bss->ifname, IFNAMSIZ);
11415
11416 os_memset(buf, 0, sizeof(buf));
11417 os_strlcpy(buf, cmd, sizeof(buf));
11418
11419 priv_cmd.buf = buf;
11420 priv_cmd.used_len = sizeof(buf);
11421 priv_cmd.total_len = sizeof(buf);
11422 ifr.ifr_data = &priv_cmd;
11423
11424 ret = ioctl(drv->global->ioctl_sock, SIOCDEVPRIVATE + 1, &ifr);
11425 if (ret < 0) {
11426 wpa_printf(MSG_ERROR, "%s: failed to issue private commands",
11427 __func__);
11428 wpa_driver_send_hang_msg(drv);
11429 return ret;
11430 }
11431
11432 drv_errors = 0;
11433 return 0;
11434}
11435
11436
11437static int android_pno_start(struct i802_bss *bss,
11438 struct wpa_driver_scan_params *params)
11439{
11440 struct wpa_driver_nl80211_data *drv = bss->drv;
11441 struct ifreq ifr;
11442 android_wifi_priv_cmd priv_cmd;
11443 int ret = 0, i = 0, bp;
11444 char buf[WEXT_PNO_MAX_COMMAND_SIZE];
11445
11446 bp = WEXT_PNOSETUP_HEADER_SIZE;
11447 os_memcpy(buf, WEXT_PNOSETUP_HEADER, bp);
11448 buf[bp++] = WEXT_PNO_TLV_PREFIX;
11449 buf[bp++] = WEXT_PNO_TLV_VERSION;
11450 buf[bp++] = WEXT_PNO_TLV_SUBVERSION;
11451 buf[bp++] = WEXT_PNO_TLV_RESERVED;
11452
11453 while (i < WEXT_PNO_AMOUNT && (size_t) i < params->num_ssids) {
11454 /* Check that there is enough space needed for 1 more SSID, the
11455 * other sections and null termination */
11456 if ((bp + WEXT_PNO_SSID_HEADER_SIZE + MAX_SSID_LEN +
11457 WEXT_PNO_NONSSID_SECTIONS_SIZE + 1) >= (int) sizeof(buf))
11458 break;
11459 wpa_hexdump_ascii(MSG_DEBUG, "For PNO Scan",
11460 params->ssids[i].ssid,
11461 params->ssids[i].ssid_len);
11462 buf[bp++] = WEXT_PNO_SSID_SECTION;
11463 buf[bp++] = params->ssids[i].ssid_len;
11464 os_memcpy(&buf[bp], params->ssids[i].ssid,
11465 params->ssids[i].ssid_len);
11466 bp += params->ssids[i].ssid_len;
11467 i++;
11468 }
11469
11470 buf[bp++] = WEXT_PNO_SCAN_INTERVAL_SECTION;
11471 os_snprintf(&buf[bp], WEXT_PNO_SCAN_INTERVAL_LENGTH + 1, "%x",
11472 WEXT_PNO_SCAN_INTERVAL);
11473 bp += WEXT_PNO_SCAN_INTERVAL_LENGTH;
11474
11475 buf[bp++] = WEXT_PNO_REPEAT_SECTION;
11476 os_snprintf(&buf[bp], WEXT_PNO_REPEAT_LENGTH + 1, "%x",
11477 WEXT_PNO_REPEAT);
11478 bp += WEXT_PNO_REPEAT_LENGTH;
11479
11480 buf[bp++] = WEXT_PNO_MAX_REPEAT_SECTION;
11481 os_snprintf(&buf[bp], WEXT_PNO_MAX_REPEAT_LENGTH + 1, "%x",
11482 WEXT_PNO_MAX_REPEAT);
11483 bp += WEXT_PNO_MAX_REPEAT_LENGTH + 1;
11484
11485 memset(&ifr, 0, sizeof(ifr));
11486 memset(&priv_cmd, 0, sizeof(priv_cmd));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070011487 os_strlcpy(ifr.ifr_name, bss->ifname, IFNAMSIZ);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011488
11489 priv_cmd.buf = buf;
11490 priv_cmd.used_len = bp;
11491 priv_cmd.total_len = bp;
11492 ifr.ifr_data = &priv_cmd;
11493
11494 ret = ioctl(drv->global->ioctl_sock, SIOCDEVPRIVATE + 1, &ifr);
11495
11496 if (ret < 0) {
11497 wpa_printf(MSG_ERROR, "ioctl[SIOCSIWPRIV] (pnosetup): %d",
11498 ret);
11499 wpa_driver_send_hang_msg(drv);
11500 return ret;
11501 }
11502
11503 drv_errors = 0;
11504
11505 return android_priv_cmd(bss, "PNOFORCE 1");
11506}
11507
11508
11509static int android_pno_stop(struct i802_bss *bss)
11510{
11511 return android_priv_cmd(bss, "PNOFORCE 0");
11512}
11513
11514#endif /* ANDROID */
11515
11516
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011517static int driver_nl80211_set_key(const char *ifname, void *priv,
11518 enum wpa_alg alg, const u8 *addr,
11519 int key_idx, int set_tx,
11520 const u8 *seq, size_t seq_len,
11521 const u8 *key, size_t key_len)
11522{
11523 struct i802_bss *bss = priv;
11524 return wpa_driver_nl80211_set_key(ifname, bss, alg, addr, key_idx,
11525 set_tx, seq, seq_len, key, key_len);
11526}
11527
11528
11529static int driver_nl80211_scan2(void *priv,
11530 struct wpa_driver_scan_params *params)
11531{
11532 struct i802_bss *bss = priv;
11533 return wpa_driver_nl80211_scan(bss, params);
11534}
11535
11536
11537static int driver_nl80211_deauthenticate(void *priv, const u8 *addr,
11538 int reason_code)
11539{
11540 struct i802_bss *bss = priv;
11541 return wpa_driver_nl80211_deauthenticate(bss, addr, reason_code);
11542}
11543
11544
11545static int driver_nl80211_authenticate(void *priv,
11546 struct wpa_driver_auth_params *params)
11547{
11548 struct i802_bss *bss = priv;
11549 return wpa_driver_nl80211_authenticate(bss, params);
11550}
11551
11552
11553static void driver_nl80211_deinit(void *priv)
11554{
11555 struct i802_bss *bss = priv;
11556 wpa_driver_nl80211_deinit(bss);
11557}
11558
11559
11560static int driver_nl80211_if_remove(void *priv, enum wpa_driver_if_type type,
11561 const char *ifname)
11562{
11563 struct i802_bss *bss = priv;
11564 return wpa_driver_nl80211_if_remove(bss, type, ifname);
11565}
11566
11567
11568static int driver_nl80211_send_mlme(void *priv, const u8 *data,
11569 size_t data_len, int noack)
11570{
11571 struct i802_bss *bss = priv;
11572 return wpa_driver_nl80211_send_mlme(bss, data, data_len, noack,
11573 0, 0, 0, 0);
11574}
11575
11576
11577static int driver_nl80211_sta_remove(void *priv, const u8 *addr)
11578{
11579 struct i802_bss *bss = priv;
11580 return wpa_driver_nl80211_sta_remove(bss, addr);
11581}
11582
11583
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011584static int driver_nl80211_set_sta_vlan(void *priv, const u8 *addr,
11585 const char *ifname, int vlan_id)
11586{
11587 struct i802_bss *bss = priv;
11588 return i802_set_sta_vlan(bss, addr, ifname, vlan_id);
11589}
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011590
11591
11592static int driver_nl80211_read_sta_data(void *priv,
11593 struct hostap_sta_driver_data *data,
11594 const u8 *addr)
11595{
11596 struct i802_bss *bss = priv;
11597 return i802_read_sta_data(bss, data, addr);
11598}
11599
11600
11601static int driver_nl80211_send_action(void *priv, unsigned int freq,
11602 unsigned int wait_time,
11603 const u8 *dst, const u8 *src,
11604 const u8 *bssid,
11605 const u8 *data, size_t data_len,
11606 int no_cck)
11607{
11608 struct i802_bss *bss = priv;
11609 return wpa_driver_nl80211_send_action(bss, freq, wait_time, dst, src,
11610 bssid, data, data_len, no_cck);
11611}
11612
11613
11614static int driver_nl80211_probe_req_report(void *priv, int report)
11615{
11616 struct i802_bss *bss = priv;
11617 return wpa_driver_nl80211_probe_req_report(bss, report);
11618}
11619
11620
Dmitry Shmidt700a1372013-03-15 14:14:44 -070011621static int wpa_driver_nl80211_update_ft_ies(void *priv, const u8 *md,
11622 const u8 *ies, size_t ies_len)
11623{
11624 int ret;
11625 struct nl_msg *msg;
11626 struct i802_bss *bss = priv;
11627 struct wpa_driver_nl80211_data *drv = bss->drv;
11628 u16 mdid = WPA_GET_LE16(md);
11629
11630 msg = nlmsg_alloc();
11631 if (!msg)
11632 return -ENOMEM;
11633
11634 wpa_printf(MSG_DEBUG, "nl80211: Updating FT IEs");
11635 nl80211_cmd(drv, msg, 0, NL80211_CMD_UPDATE_FT_IES);
11636 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
11637 NLA_PUT(msg, NL80211_ATTR_IE, ies_len, ies);
11638 NLA_PUT_U16(msg, NL80211_ATTR_MDID, mdid);
11639
11640 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
11641 if (ret) {
11642 wpa_printf(MSG_DEBUG, "nl80211: update_ft_ies failed "
11643 "err=%d (%s)", ret, strerror(-ret));
11644 }
11645
11646 return ret;
11647
11648nla_put_failure:
11649 nlmsg_free(msg);
11650 return -ENOBUFS;
11651}
11652
11653
Dmitry Shmidt34af3062013-07-11 10:46:32 -070011654const u8 * wpa_driver_nl80211_get_macaddr(void *priv)
11655{
11656 struct i802_bss *bss = priv;
11657 struct wpa_driver_nl80211_data *drv = bss->drv;
11658
11659 if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE)
11660 return NULL;
11661
11662 return bss->addr;
11663}
11664
11665
Dmitry Shmidt56052862013-10-04 10:23:25 -070011666static const char * scan_state_str(enum scan_states scan_state)
11667{
11668 switch (scan_state) {
11669 case NO_SCAN:
11670 return "NO_SCAN";
11671 case SCAN_REQUESTED:
11672 return "SCAN_REQUESTED";
11673 case SCAN_STARTED:
11674 return "SCAN_STARTED";
11675 case SCAN_COMPLETED:
11676 return "SCAN_COMPLETED";
11677 case SCAN_ABORTED:
11678 return "SCAN_ABORTED";
11679 case SCHED_SCAN_STARTED:
11680 return "SCHED_SCAN_STARTED";
11681 case SCHED_SCAN_STOPPED:
11682 return "SCHED_SCAN_STOPPED";
11683 case SCHED_SCAN_RESULTS:
11684 return "SCHED_SCAN_RESULTS";
11685 }
11686
11687 return "??";
11688}
11689
11690
11691static int wpa_driver_nl80211_status(void *priv, char *buf, size_t buflen)
11692{
11693 struct i802_bss *bss = priv;
11694 struct wpa_driver_nl80211_data *drv = bss->drv;
11695 int res;
11696 char *pos, *end;
11697
11698 pos = buf;
11699 end = buf + buflen;
11700
11701 res = os_snprintf(pos, end - pos,
11702 "ifindex=%d\n"
11703 "ifname=%s\n"
11704 "brname=%s\n"
11705 "addr=" MACSTR "\n"
11706 "freq=%d\n"
11707 "%s%s%s%s%s",
11708 bss->ifindex,
11709 bss->ifname,
11710 bss->brname,
11711 MAC2STR(bss->addr),
11712 bss->freq,
11713 bss->beacon_set ? "beacon_set=1\n" : "",
11714 bss->added_if_into_bridge ?
11715 "added_if_into_bridge=1\n" : "",
11716 bss->added_bridge ? "added_bridge=1\n" : "",
11717 bss->in_deinit ? "in_deinit=1\n" : "",
11718 bss->if_dynamic ? "if_dynamic=1\n" : "");
11719 if (res < 0 || res >= end - pos)
11720 return pos - buf;
11721 pos += res;
11722
11723 if (bss->wdev_id_set) {
11724 res = os_snprintf(pos, end - pos, "wdev_id=%llu\n",
11725 (unsigned long long) bss->wdev_id);
11726 if (res < 0 || res >= end - pos)
11727 return pos - buf;
11728 pos += res;
11729 }
11730
11731 res = os_snprintf(pos, end - pos,
11732 "phyname=%s\n"
11733 "drv_ifindex=%d\n"
11734 "operstate=%d\n"
11735 "scan_state=%s\n"
11736 "auth_bssid=" MACSTR "\n"
11737 "auth_attempt_bssid=" MACSTR "\n"
11738 "bssid=" MACSTR "\n"
11739 "prev_bssid=" MACSTR "\n"
11740 "associated=%d\n"
11741 "assoc_freq=%u\n"
11742 "monitor_sock=%d\n"
11743 "monitor_ifidx=%d\n"
11744 "monitor_refcount=%d\n"
11745 "last_mgmt_freq=%u\n"
11746 "eapol_tx_sock=%d\n"
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -070011747 "%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
Dmitry Shmidt56052862013-10-04 10:23:25 -070011748 drv->phyname,
11749 drv->ifindex,
11750 drv->operstate,
11751 scan_state_str(drv->scan_state),
11752 MAC2STR(drv->auth_bssid),
11753 MAC2STR(drv->auth_attempt_bssid),
11754 MAC2STR(drv->bssid),
11755 MAC2STR(drv->prev_bssid),
11756 drv->associated,
11757 drv->assoc_freq,
11758 drv->monitor_sock,
11759 drv->monitor_ifidx,
11760 drv->monitor_refcount,
11761 drv->last_mgmt_freq,
11762 drv->eapol_tx_sock,
11763 drv->ignore_if_down_event ?
11764 "ignore_if_down_event=1\n" : "",
11765 drv->scan_complete_events ?
11766 "scan_complete_events=1\n" : "",
11767 drv->disabled_11b_rates ?
11768 "disabled_11b_rates=1\n" : "",
11769 drv->pending_remain_on_chan ?
11770 "pending_remain_on_chan=1\n" : "",
11771 drv->in_interface_list ? "in_interface_list=1\n" : "",
11772 drv->device_ap_sme ? "device_ap_sme=1\n" : "",
11773 drv->poll_command_supported ?
11774 "poll_command_supported=1\n" : "",
11775 drv->data_tx_status ? "data_tx_status=1\n" : "",
11776 drv->scan_for_auth ? "scan_for_auth=1\n" : "",
11777 drv->retry_auth ? "retry_auth=1\n" : "",
11778 drv->use_monitor ? "use_monitor=1\n" : "",
11779 drv->ignore_next_local_disconnect ?
11780 "ignore_next_local_disconnect=1\n" : "",
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -070011781 drv->ignore_next_local_deauth ?
11782 "ignore_next_local_deauth=1\n" : "",
Dmitry Shmidt56052862013-10-04 10:23:25 -070011783 drv->allow_p2p_device ? "allow_p2p_device=1\n" : "");
11784 if (res < 0 || res >= end - pos)
11785 return pos - buf;
11786 pos += res;
11787
11788 if (drv->has_capability) {
11789 res = os_snprintf(pos, end - pos,
11790 "capa.key_mgmt=0x%x\n"
11791 "capa.enc=0x%x\n"
11792 "capa.auth=0x%x\n"
11793 "capa.flags=0x%x\n"
11794 "capa.max_scan_ssids=%d\n"
11795 "capa.max_sched_scan_ssids=%d\n"
11796 "capa.sched_scan_supported=%d\n"
11797 "capa.max_match_sets=%d\n"
11798 "capa.max_remain_on_chan=%u\n"
11799 "capa.max_stations=%u\n"
11800 "capa.probe_resp_offloads=0x%x\n"
11801 "capa.max_acl_mac_addrs=%u\n"
11802 "capa.num_multichan_concurrent=%u\n",
11803 drv->capa.key_mgmt,
11804 drv->capa.enc,
11805 drv->capa.auth,
11806 drv->capa.flags,
11807 drv->capa.max_scan_ssids,
11808 drv->capa.max_sched_scan_ssids,
11809 drv->capa.sched_scan_supported,
11810 drv->capa.max_match_sets,
11811 drv->capa.max_remain_on_chan,
11812 drv->capa.max_stations,
11813 drv->capa.probe_resp_offloads,
11814 drv->capa.max_acl_mac_addrs,
11815 drv->capa.num_multichan_concurrent);
11816 if (res < 0 || res >= end - pos)
11817 return pos - buf;
11818 pos += res;
11819 }
11820
11821 return pos - buf;
11822}
11823
11824
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080011825static int set_beacon_data(struct nl_msg *msg, struct beacon_data *settings)
11826{
11827 if (settings->head)
11828 NLA_PUT(msg, NL80211_ATTR_BEACON_HEAD,
11829 settings->head_len, settings->head);
11830
11831 if (settings->tail)
11832 NLA_PUT(msg, NL80211_ATTR_BEACON_TAIL,
11833 settings->tail_len, settings->tail);
11834
11835 if (settings->beacon_ies)
11836 NLA_PUT(msg, NL80211_ATTR_IE,
11837 settings->beacon_ies_len, settings->beacon_ies);
11838
11839 if (settings->proberesp_ies)
11840 NLA_PUT(msg, NL80211_ATTR_IE_PROBE_RESP,
11841 settings->proberesp_ies_len, settings->proberesp_ies);
11842
11843 if (settings->assocresp_ies)
11844 NLA_PUT(msg,
11845 NL80211_ATTR_IE_ASSOC_RESP,
11846 settings->assocresp_ies_len, settings->assocresp_ies);
11847
11848 if (settings->probe_resp)
11849 NLA_PUT(msg, NL80211_ATTR_PROBE_RESP,
11850 settings->probe_resp_len, settings->probe_resp);
11851
11852 return 0;
11853
11854nla_put_failure:
11855 return -ENOBUFS;
11856}
11857
11858
11859static int nl80211_switch_channel(void *priv, struct csa_settings *settings)
11860{
11861 struct nl_msg *msg;
11862 struct i802_bss *bss = priv;
11863 struct wpa_driver_nl80211_data *drv = bss->drv;
11864 struct nlattr *beacon_csa;
11865 int ret = -ENOBUFS;
11866
Dmitry Shmidt04f534e2013-12-09 15:50:16 -080011867 wpa_printf(MSG_DEBUG, "nl80211: Channel switch request (cs_count=%u block_tx=%u freq=%d width=%d cf1=%d cf2=%d)",
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080011868 settings->cs_count, settings->block_tx,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -080011869 settings->freq_params.freq, settings->freq_params.bandwidth,
11870 settings->freq_params.center_freq1,
11871 settings->freq_params.center_freq2);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080011872
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080011873 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_AP_CSA)) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080011874 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support channel switch command");
11875 return -EOPNOTSUPP;
11876 }
11877
11878 if ((drv->nlmode != NL80211_IFTYPE_AP) &&
11879 (drv->nlmode != NL80211_IFTYPE_P2P_GO))
11880 return -EOPNOTSUPP;
11881
11882 /* check settings validity */
11883 if (!settings->beacon_csa.tail ||
11884 ((settings->beacon_csa.tail_len <=
11885 settings->counter_offset_beacon) ||
11886 (settings->beacon_csa.tail[settings->counter_offset_beacon] !=
11887 settings->cs_count)))
11888 return -EINVAL;
11889
11890 if (settings->beacon_csa.probe_resp &&
11891 ((settings->beacon_csa.probe_resp_len <=
11892 settings->counter_offset_presp) ||
11893 (settings->beacon_csa.probe_resp[settings->counter_offset_presp] !=
11894 settings->cs_count)))
11895 return -EINVAL;
11896
11897 msg = nlmsg_alloc();
11898 if (!msg)
11899 return -ENOMEM;
11900
11901 nl80211_cmd(drv, msg, 0, NL80211_CMD_CHANNEL_SWITCH);
11902 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
11903 NLA_PUT_U32(msg, NL80211_ATTR_CH_SWITCH_COUNT, settings->cs_count);
11904 ret = nl80211_put_freq_params(msg, &settings->freq_params);
11905 if (ret)
11906 goto error;
11907
11908 if (settings->block_tx)
11909 NLA_PUT_FLAG(msg, NL80211_ATTR_CH_SWITCH_BLOCK_TX);
11910
11911 /* beacon_after params */
11912 ret = set_beacon_data(msg, &settings->beacon_after);
11913 if (ret)
11914 goto error;
11915
11916 /* beacon_csa params */
11917 beacon_csa = nla_nest_start(msg, NL80211_ATTR_CSA_IES);
11918 if (!beacon_csa)
11919 goto nla_put_failure;
11920
11921 ret = set_beacon_data(msg, &settings->beacon_csa);
11922 if (ret)
11923 goto error;
11924
11925 NLA_PUT_U16(msg, NL80211_ATTR_CSA_C_OFF_BEACON,
11926 settings->counter_offset_beacon);
11927
11928 if (settings->beacon_csa.probe_resp)
11929 NLA_PUT_U16(msg, NL80211_ATTR_CSA_C_OFF_PRESP,
11930 settings->counter_offset_presp);
11931
11932 nla_nest_end(msg, beacon_csa);
11933 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
11934 if (ret) {
11935 wpa_printf(MSG_DEBUG, "nl80211: switch_channel failed err=%d (%s)",
11936 ret, strerror(-ret));
11937 }
11938 return ret;
11939
11940nla_put_failure:
11941 ret = -ENOBUFS;
11942error:
11943 nlmsg_free(msg);
11944 wpa_printf(MSG_DEBUG, "nl80211: Could not build channel switch request");
11945 return ret;
11946}
11947
11948
Dmitry Shmidta38abf92014-03-06 13:38:44 -080011949#ifdef CONFIG_TESTING_OPTIONS
11950static int cmd_reply_handler(struct nl_msg *msg, void *arg)
11951{
11952 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
11953 struct wpabuf *buf = arg;
11954
11955 if (!buf)
11956 return NL_SKIP;
11957
11958 if ((size_t) genlmsg_attrlen(gnlh, 0) > wpabuf_tailroom(buf)) {
11959 wpa_printf(MSG_INFO, "nl80211: insufficient buffer space for reply");
11960 return NL_SKIP;
11961 }
11962
11963 wpabuf_put_data(buf, genlmsg_attrdata(gnlh, 0),
11964 genlmsg_attrlen(gnlh, 0));
11965
11966 return NL_SKIP;
11967}
11968#endif /* CONFIG_TESTING_OPTIONS */
11969
11970
11971static int vendor_reply_handler(struct nl_msg *msg, void *arg)
11972{
11973 struct nlattr *tb[NL80211_ATTR_MAX + 1];
11974 struct nlattr *nl_vendor_reply, *nl;
11975 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
11976 struct wpabuf *buf = arg;
11977 int rem;
11978
11979 if (!buf)
11980 return NL_SKIP;
11981
11982 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
11983 genlmsg_attrlen(gnlh, 0), NULL);
11984 nl_vendor_reply = tb[NL80211_ATTR_VENDOR_DATA];
11985
11986 if (!nl_vendor_reply)
11987 return NL_SKIP;
11988
11989 if ((size_t) nla_len(nl_vendor_reply) > wpabuf_tailroom(buf)) {
11990 wpa_printf(MSG_INFO, "nl80211: Vendor command: insufficient buffer space for reply");
11991 return NL_SKIP;
11992 }
11993
11994 nla_for_each_nested(nl, nl_vendor_reply, rem) {
11995 wpabuf_put_data(buf, nla_data(nl), nla_len(nl));
11996 }
11997
11998 return NL_SKIP;
11999}
12000
12001
12002static int nl80211_vendor_cmd(void *priv, unsigned int vendor_id,
12003 unsigned int subcmd, const u8 *data,
12004 size_t data_len, struct wpabuf *buf)
12005{
12006 struct i802_bss *bss = priv;
12007 struct wpa_driver_nl80211_data *drv = bss->drv;
12008 struct nl_msg *msg;
12009 int ret;
12010
12011 msg = nlmsg_alloc();
12012 if (!msg)
12013 return -ENOMEM;
12014
12015#ifdef CONFIG_TESTING_OPTIONS
12016 if (vendor_id == 0xffffffff) {
12017 nl80211_cmd(drv, msg, 0, subcmd);
12018 if (nlmsg_append(msg, (void *) data, data_len, NLMSG_ALIGNTO) <
12019 0)
12020 goto nla_put_failure;
12021 ret = send_and_recv_msgs(drv, msg, cmd_reply_handler, buf);
12022 if (ret)
12023 wpa_printf(MSG_DEBUG, "nl80211: command failed err=%d",
12024 ret);
12025 return ret;
12026 }
12027#endif /* CONFIG_TESTING_OPTIONS */
12028
12029 nl80211_cmd(drv, msg, 0, NL80211_CMD_VENDOR);
12030 if (nl80211_set_iface_id(msg, bss) < 0)
12031 goto nla_put_failure;
12032 NLA_PUT_U32(msg, NL80211_ATTR_VENDOR_ID, vendor_id);
12033 NLA_PUT_U32(msg, NL80211_ATTR_VENDOR_SUBCMD, subcmd);
12034 if (data)
12035 NLA_PUT(msg, NL80211_ATTR_VENDOR_DATA, data_len, data);
12036
12037 ret = send_and_recv_msgs(drv, msg, vendor_reply_handler, buf);
12038 if (ret)
12039 wpa_printf(MSG_DEBUG, "nl80211: vendor command failed err=%d",
12040 ret);
12041 return ret;
12042
12043nla_put_failure:
12044 nlmsg_free(msg);
12045 return -ENOBUFS;
12046}
12047
12048
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080012049static int nl80211_set_qos_map(void *priv, const u8 *qos_map_set,
12050 u8 qos_map_set_len)
12051{
12052 struct i802_bss *bss = priv;
12053 struct wpa_driver_nl80211_data *drv = bss->drv;
12054 struct nl_msg *msg;
12055 int ret;
12056
12057 msg = nlmsg_alloc();
12058 if (!msg)
12059 return -ENOMEM;
12060
12061 wpa_hexdump(MSG_DEBUG, "nl80211: Setting QoS Map",
12062 qos_map_set, qos_map_set_len);
12063
12064 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_QOS_MAP);
12065 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
12066 NLA_PUT(msg, NL80211_ATTR_QOS_MAP, qos_map_set_len, qos_map_set);
12067
12068 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
12069 if (ret)
12070 wpa_printf(MSG_DEBUG, "nl80211: Setting QoS Map failed");
12071
12072 return ret;
12073
12074nla_put_failure:
12075 nlmsg_free(msg);
12076 return -ENOBUFS;
12077}
12078
12079
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012080const struct wpa_driver_ops wpa_driver_nl80211_ops = {
12081 .name = "nl80211",
12082 .desc = "Linux nl80211/cfg80211",
12083 .get_bssid = wpa_driver_nl80211_get_bssid,
12084 .get_ssid = wpa_driver_nl80211_get_ssid,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080012085 .set_key = driver_nl80211_set_key,
12086 .scan2 = driver_nl80211_scan2,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080012087 .sched_scan = wpa_driver_nl80211_sched_scan,
12088 .stop_sched_scan = wpa_driver_nl80211_stop_sched_scan,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012089 .get_scan_results2 = wpa_driver_nl80211_get_scan_results,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080012090 .deauthenticate = driver_nl80211_deauthenticate,
12091 .authenticate = driver_nl80211_authenticate,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012092 .associate = wpa_driver_nl80211_associate,
12093 .global_init = nl80211_global_init,
12094 .global_deinit = nl80211_global_deinit,
12095 .init2 = wpa_driver_nl80211_init,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080012096 .deinit = driver_nl80211_deinit,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012097 .get_capa = wpa_driver_nl80211_get_capa,
12098 .set_operstate = wpa_driver_nl80211_set_operstate,
12099 .set_supp_port = wpa_driver_nl80211_set_supp_port,
12100 .set_country = wpa_driver_nl80211_set_country,
Dmitry Shmidtcce06662013-11-04 18:44:24 -080012101 .get_country = wpa_driver_nl80211_get_country,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080012102 .set_ap = wpa_driver_nl80211_set_ap,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -070012103 .set_acl = wpa_driver_nl80211_set_acl,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012104 .if_add = wpa_driver_nl80211_if_add,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080012105 .if_remove = driver_nl80211_if_remove,
12106 .send_mlme = driver_nl80211_send_mlme,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012107 .get_hw_feature_data = wpa_driver_nl80211_get_hw_feature_data,
12108 .sta_add = wpa_driver_nl80211_sta_add,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080012109 .sta_remove = driver_nl80211_sta_remove,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012110 .hapd_send_eapol = wpa_driver_nl80211_hapd_send_eapol,
12111 .sta_set_flags = wpa_driver_nl80211_sta_set_flags,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012112 .hapd_init = i802_init,
12113 .hapd_deinit = i802_deinit,
Jouni Malinen75ecf522011-06-27 15:19:46 -070012114 .set_wds_sta = i802_set_wds_sta,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012115 .get_seqnum = i802_get_seqnum,
12116 .flush = i802_flush,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012117 .get_inact_sec = i802_get_inact_sec,
12118 .sta_clear_stats = i802_sta_clear_stats,
12119 .set_rts = i802_set_rts,
12120 .set_frag = i802_set_frag,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012121 .set_tx_queue_params = i802_set_tx_queue_params,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080012122 .set_sta_vlan = driver_nl80211_set_sta_vlan,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012123 .sta_deauth = i802_sta_deauth,
12124 .sta_disassoc = i802_sta_disassoc,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080012125 .read_sta_data = driver_nl80211_read_sta_data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012126 .set_freq = i802_set_freq,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080012127 .send_action = driver_nl80211_send_action,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012128 .send_action_cancel_wait = wpa_driver_nl80211_send_action_cancel_wait,
12129 .remain_on_channel = wpa_driver_nl80211_remain_on_channel,
12130 .cancel_remain_on_channel =
12131 wpa_driver_nl80211_cancel_remain_on_channel,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080012132 .probe_req_report = driver_nl80211_probe_req_report,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012133 .deinit_ap = wpa_driver_nl80211_deinit_ap,
Dmitry Shmidt04949592012-07-19 12:16:46 -070012134 .deinit_p2p_cli = wpa_driver_nl80211_deinit_p2p_cli,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012135 .resume = wpa_driver_nl80211_resume,
12136 .send_ft_action = nl80211_send_ft_action,
12137 .signal_monitor = nl80211_signal_monitor,
12138 .signal_poll = nl80211_signal_poll,
12139 .send_frame = nl80211_send_frame,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080012140 .shared_freq = wpa_driver_nl80211_shared_freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012141 .set_param = nl80211_set_param,
12142 .get_radio_name = nl80211_get_radio_name,
Jouni Malinen75ecf522011-06-27 15:19:46 -070012143 .add_pmkid = nl80211_add_pmkid,
12144 .remove_pmkid = nl80211_remove_pmkid,
12145 .flush_pmkid = nl80211_flush_pmkid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080012146 .set_rekey_info = nl80211_set_rekey_info,
12147 .poll_client = nl80211_poll_client,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080012148 .set_p2p_powersave = nl80211_set_p2p_powersave,
Dmitry Shmidtea69e842013-05-13 14:52:28 -070012149 .start_dfs_cac = nl80211_start_radar_detection,
12150 .stop_ap = wpa_driver_nl80211_stop_ap,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080012151#ifdef CONFIG_TDLS
12152 .send_tdls_mgmt = nl80211_send_tdls_mgmt,
12153 .tdls_oper = nl80211_tdls_oper,
12154#endif /* CONFIG_TDLS */
Dmitry Shmidt700a1372013-03-15 14:14:44 -070012155 .update_ft_ies = wpa_driver_nl80211_update_ft_ies,
Dmitry Shmidt34af3062013-07-11 10:46:32 -070012156 .get_mac_addr = wpa_driver_nl80211_get_macaddr,
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070012157 .get_survey = wpa_driver_nl80211_get_survey,
Dmitry Shmidt56052862013-10-04 10:23:25 -070012158 .status = wpa_driver_nl80211_status,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080012159 .switch_channel = nl80211_switch_channel,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080012160#ifdef ANDROID_P2P
Dmitry Shmidt6e933c12011-09-27 12:29:26 -070012161 .set_noa = wpa_driver_set_p2p_noa,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080012162 .get_noa = wpa_driver_get_p2p_noa,
Dmitry Shmidt6e933c12011-09-27 12:29:26 -070012163 .set_ap_wps_ie = wpa_driver_set_ap_wps_p2p_ie,
Dmitry Shmidt292b0c32013-11-22 12:54:42 -080012164#endif /* ANDROID_P2P */
Dmitry Shmidt738a26e2011-07-07 14:22:14 -070012165#ifdef ANDROID
12166 .driver_cmd = wpa_driver_nl80211_driver_cmd,
Dmitry Shmidt292b0c32013-11-22 12:54:42 -080012167#endif /* ANDROID */
Dmitry Shmidta38abf92014-03-06 13:38:44 -080012168 .vendor_cmd = nl80211_vendor_cmd,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080012169 .set_qos_map = nl80211_set_qos_map,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012170};