blob: dcd002e4c8a2f0105f884f7e95b455fcfbe96552 [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 Shmidt34af3062013-07-11 10:46:32 -0700300 unsigned int allow_p2p_device:1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800301 unsigned int hostapd:1;
302 unsigned int start_mode_ap:1;
303 unsigned int start_iface_up:1;
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800304 unsigned int test_use_roc_tx:1;
Dmitry Shmidt98660862014-03-11 17:26:21 -0700305 unsigned int ignore_deauth_event:1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700306
307 u64 remain_on_chan_cookie;
308 u64 send_action_cookie;
309
310 unsigned int last_mgmt_freq;
311
312 struct wpa_driver_scan_filter *filter_ssids;
313 size_t num_filter_ssids;
314
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800315 struct i802_bss *first_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700316
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800317 int eapol_tx_sock;
318
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700319 int eapol_sock; /* socket for EAPOL frames */
320
321 int default_if_indices[16];
322 int *if_indices;
323 int num_if_indices;
324
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800325 /* From failed authentication command */
326 int auth_freq;
327 u8 auth_bssid_[ETH_ALEN];
328 u8 auth_ssid[32];
329 size_t auth_ssid_len;
330 int auth_alg;
331 u8 *auth_ie;
332 size_t auth_ie_len;
333 u8 auth_wep_key[4][16];
334 size_t auth_wep_key_len[4];
335 int auth_wep_tx_keyidx;
336 int auth_local_state_change;
337 int auth_p2p;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700338};
339
340
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800341static void wpa_driver_nl80211_deinit(struct i802_bss *bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700342static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx,
343 void *timeout_ctx);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800344static int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
345 enum nl80211_iftype nlmode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700346static int
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800347wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv,
348 const u8 *set_addr, int first);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700349static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
350 const u8 *addr, int cmd, u16 reason_code,
351 int local_state_change);
352static void nl80211_remove_monitor_interface(
353 struct wpa_driver_nl80211_data *drv);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800354static int nl80211_send_frame_cmd(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700355 unsigned int freq, unsigned int wait,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800356 const u8 *buf, size_t buf_len, u64 *cookie,
357 int no_cck, int no_ack, int offchanok);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700358static int nl80211_register_frame(struct i802_bss *bss,
359 struct nl_handle *hl_handle,
360 u16 type, const u8 *match, size_t match_len);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800361static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss,
362 int report);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800363#ifdef ANDROID
364static int android_pno_start(struct i802_bss *bss,
365 struct wpa_driver_scan_params *params);
366static int android_pno_stop(struct i802_bss *bss);
Dmitry Shmidt292b0c32013-11-22 12:54:42 -0800367extern int wpa_driver_nl80211_driver_cmd(void *priv, char *cmd, char *buf,
368 size_t buf_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800369#endif /* ANDROID */
370#ifdef ANDROID_P2P
Dmitry Shmidt6e933c12011-09-27 12:29:26 -0700371int wpa_driver_set_p2p_noa(void *priv, u8 count, int start, int duration);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800372int wpa_driver_get_p2p_noa(void *priv, u8 *buf, size_t len);
Dmitry Shmidt6e933c12011-09-27 12:29:26 -0700373int wpa_driver_set_p2p_ps(void *priv, int legacy_ps, int opp_ps, int ctwindow);
374int wpa_driver_set_ap_wps_p2p_ie(void *priv, const struct wpabuf *beacon,
Dmitry Shmidt292b0c32013-11-22 12:54:42 -0800375 const struct wpabuf *proberesp,
376 const struct wpabuf *assocresp);
377#endif /* ANDROID_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700378
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700379static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
380static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
381static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800382static int wpa_driver_nl80211_if_remove(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700383 enum wpa_driver_if_type type,
384 const char *ifname);
Dmitry Shmidt738a26e2011-07-07 14:22:14 -0700385
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800386static int wpa_driver_nl80211_set_freq(struct i802_bss *bss,
387 struct hostapd_freq_params *freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700388static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
389 int ifindex, int disabled);
390
391static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800392static int wpa_driver_nl80211_authenticate_retry(
393 struct wpa_driver_nl80211_data *drv);
394
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700395static int i802_set_iface_flags(struct i802_bss *bss, int up);
396
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800397
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700398static const char * nl80211_command_to_string(enum nl80211_commands cmd)
399{
400#define C2S(x) case x: return #x;
401 switch (cmd) {
402 C2S(NL80211_CMD_UNSPEC)
403 C2S(NL80211_CMD_GET_WIPHY)
404 C2S(NL80211_CMD_SET_WIPHY)
405 C2S(NL80211_CMD_NEW_WIPHY)
406 C2S(NL80211_CMD_DEL_WIPHY)
407 C2S(NL80211_CMD_GET_INTERFACE)
408 C2S(NL80211_CMD_SET_INTERFACE)
409 C2S(NL80211_CMD_NEW_INTERFACE)
410 C2S(NL80211_CMD_DEL_INTERFACE)
411 C2S(NL80211_CMD_GET_KEY)
412 C2S(NL80211_CMD_SET_KEY)
413 C2S(NL80211_CMD_NEW_KEY)
414 C2S(NL80211_CMD_DEL_KEY)
415 C2S(NL80211_CMD_GET_BEACON)
416 C2S(NL80211_CMD_SET_BEACON)
417 C2S(NL80211_CMD_START_AP)
418 C2S(NL80211_CMD_STOP_AP)
419 C2S(NL80211_CMD_GET_STATION)
420 C2S(NL80211_CMD_SET_STATION)
421 C2S(NL80211_CMD_NEW_STATION)
422 C2S(NL80211_CMD_DEL_STATION)
423 C2S(NL80211_CMD_GET_MPATH)
424 C2S(NL80211_CMD_SET_MPATH)
425 C2S(NL80211_CMD_NEW_MPATH)
426 C2S(NL80211_CMD_DEL_MPATH)
427 C2S(NL80211_CMD_SET_BSS)
428 C2S(NL80211_CMD_SET_REG)
429 C2S(NL80211_CMD_REQ_SET_REG)
430 C2S(NL80211_CMD_GET_MESH_CONFIG)
431 C2S(NL80211_CMD_SET_MESH_CONFIG)
432 C2S(NL80211_CMD_SET_MGMT_EXTRA_IE)
433 C2S(NL80211_CMD_GET_REG)
434 C2S(NL80211_CMD_GET_SCAN)
435 C2S(NL80211_CMD_TRIGGER_SCAN)
436 C2S(NL80211_CMD_NEW_SCAN_RESULTS)
437 C2S(NL80211_CMD_SCAN_ABORTED)
438 C2S(NL80211_CMD_REG_CHANGE)
439 C2S(NL80211_CMD_AUTHENTICATE)
440 C2S(NL80211_CMD_ASSOCIATE)
441 C2S(NL80211_CMD_DEAUTHENTICATE)
442 C2S(NL80211_CMD_DISASSOCIATE)
443 C2S(NL80211_CMD_MICHAEL_MIC_FAILURE)
444 C2S(NL80211_CMD_REG_BEACON_HINT)
445 C2S(NL80211_CMD_JOIN_IBSS)
446 C2S(NL80211_CMD_LEAVE_IBSS)
447 C2S(NL80211_CMD_TESTMODE)
448 C2S(NL80211_CMD_CONNECT)
449 C2S(NL80211_CMD_ROAM)
450 C2S(NL80211_CMD_DISCONNECT)
451 C2S(NL80211_CMD_SET_WIPHY_NETNS)
452 C2S(NL80211_CMD_GET_SURVEY)
453 C2S(NL80211_CMD_NEW_SURVEY_RESULTS)
454 C2S(NL80211_CMD_SET_PMKSA)
455 C2S(NL80211_CMD_DEL_PMKSA)
456 C2S(NL80211_CMD_FLUSH_PMKSA)
457 C2S(NL80211_CMD_REMAIN_ON_CHANNEL)
458 C2S(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL)
459 C2S(NL80211_CMD_SET_TX_BITRATE_MASK)
460 C2S(NL80211_CMD_REGISTER_FRAME)
461 C2S(NL80211_CMD_FRAME)
462 C2S(NL80211_CMD_FRAME_TX_STATUS)
463 C2S(NL80211_CMD_SET_POWER_SAVE)
464 C2S(NL80211_CMD_GET_POWER_SAVE)
465 C2S(NL80211_CMD_SET_CQM)
466 C2S(NL80211_CMD_NOTIFY_CQM)
467 C2S(NL80211_CMD_SET_CHANNEL)
468 C2S(NL80211_CMD_SET_WDS_PEER)
469 C2S(NL80211_CMD_FRAME_WAIT_CANCEL)
470 C2S(NL80211_CMD_JOIN_MESH)
471 C2S(NL80211_CMD_LEAVE_MESH)
472 C2S(NL80211_CMD_UNPROT_DEAUTHENTICATE)
473 C2S(NL80211_CMD_UNPROT_DISASSOCIATE)
474 C2S(NL80211_CMD_NEW_PEER_CANDIDATE)
475 C2S(NL80211_CMD_GET_WOWLAN)
476 C2S(NL80211_CMD_SET_WOWLAN)
477 C2S(NL80211_CMD_START_SCHED_SCAN)
478 C2S(NL80211_CMD_STOP_SCHED_SCAN)
479 C2S(NL80211_CMD_SCHED_SCAN_RESULTS)
480 C2S(NL80211_CMD_SCHED_SCAN_STOPPED)
481 C2S(NL80211_CMD_SET_REKEY_OFFLOAD)
482 C2S(NL80211_CMD_PMKSA_CANDIDATE)
483 C2S(NL80211_CMD_TDLS_OPER)
484 C2S(NL80211_CMD_TDLS_MGMT)
485 C2S(NL80211_CMD_UNEXPECTED_FRAME)
486 C2S(NL80211_CMD_PROBE_CLIENT)
487 C2S(NL80211_CMD_REGISTER_BEACONS)
488 C2S(NL80211_CMD_UNEXPECTED_4ADDR_FRAME)
489 C2S(NL80211_CMD_SET_NOACK_MAP)
490 C2S(NL80211_CMD_CH_SWITCH_NOTIFY)
491 C2S(NL80211_CMD_START_P2P_DEVICE)
492 C2S(NL80211_CMD_STOP_P2P_DEVICE)
493 C2S(NL80211_CMD_CONN_FAILED)
494 C2S(NL80211_CMD_SET_MCAST_RATE)
495 C2S(NL80211_CMD_SET_MAC_ACL)
496 C2S(NL80211_CMD_RADAR_DETECT)
497 C2S(NL80211_CMD_GET_PROTOCOL_FEATURES)
498 C2S(NL80211_CMD_UPDATE_FT_IES)
499 C2S(NL80211_CMD_FT_EVENT)
500 C2S(NL80211_CMD_CRIT_PROTOCOL_START)
501 C2S(NL80211_CMD_CRIT_PROTOCOL_STOP)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800502 C2S(NL80211_CMD_GET_COALESCE)
503 C2S(NL80211_CMD_SET_COALESCE)
504 C2S(NL80211_CMD_CHANNEL_SWITCH)
505 C2S(NL80211_CMD_VENDOR)
506 C2S(NL80211_CMD_SET_QOS_MAP)
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700507 default:
508 return "NL80211_CMD_UNKNOWN";
509 }
510#undef C2S
511}
512
513
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800514/* Converts nl80211_chan_width to a common format */
515static enum chan_width convert2width(int width)
516{
517 switch (width) {
518 case NL80211_CHAN_WIDTH_20_NOHT:
519 return CHAN_WIDTH_20_NOHT;
520 case NL80211_CHAN_WIDTH_20:
521 return CHAN_WIDTH_20;
522 case NL80211_CHAN_WIDTH_40:
523 return CHAN_WIDTH_40;
524 case NL80211_CHAN_WIDTH_80:
525 return CHAN_WIDTH_80;
526 case NL80211_CHAN_WIDTH_80P80:
527 return CHAN_WIDTH_80P80;
528 case NL80211_CHAN_WIDTH_160:
529 return CHAN_WIDTH_160;
530 }
531 return CHAN_WIDTH_UNKNOWN;
532}
533
534
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800535static int is_ap_interface(enum nl80211_iftype nlmode)
536{
537 return (nlmode == NL80211_IFTYPE_AP ||
538 nlmode == NL80211_IFTYPE_P2P_GO);
539}
540
541
542static int is_sta_interface(enum nl80211_iftype nlmode)
543{
544 return (nlmode == NL80211_IFTYPE_STATION ||
545 nlmode == NL80211_IFTYPE_P2P_CLIENT);
546}
547
548
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700549static int is_p2p_net_interface(enum nl80211_iftype nlmode)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800550{
551 return (nlmode == NL80211_IFTYPE_P2P_CLIENT ||
552 nlmode == NL80211_IFTYPE_P2P_GO);
553}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700554
555
Dmitry Shmidt8bae4132013-06-06 11:25:10 -0700556static void nl80211_mark_disconnected(struct wpa_driver_nl80211_data *drv)
557{
558 if (drv->associated)
559 os_memcpy(drv->prev_bssid, drv->bssid, ETH_ALEN);
560 drv->associated = 0;
561 os_memset(drv->bssid, 0, ETH_ALEN);
562}
563
564
Jouni Malinen87fd2792011-05-16 18:35:42 +0300565struct nl80211_bss_info_arg {
566 struct wpa_driver_nl80211_data *drv;
567 struct wpa_scan_results *res;
568 unsigned int assoc_freq;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800569 u8 assoc_bssid[ETH_ALEN];
Jouni Malinen87fd2792011-05-16 18:35:42 +0300570};
571
572static int bss_info_handler(struct nl_msg *msg, void *arg);
573
574
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700575/* nl80211 code */
576static int ack_handler(struct nl_msg *msg, void *arg)
577{
578 int *err = arg;
579 *err = 0;
580 return NL_STOP;
581}
582
583static int finish_handler(struct nl_msg *msg, void *arg)
584{
585 int *ret = arg;
586 *ret = 0;
587 return NL_SKIP;
588}
589
590static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
591 void *arg)
592{
593 int *ret = arg;
594 *ret = err->error;
595 return NL_SKIP;
596}
597
598
599static int no_seq_check(struct nl_msg *msg, void *arg)
600{
601 return NL_OK;
602}
603
604
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800605static int send_and_recv(struct nl80211_global *global,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700606 struct nl_handle *nl_handle, struct nl_msg *msg,
607 int (*valid_handler)(struct nl_msg *, void *),
608 void *valid_data)
609{
610 struct nl_cb *cb;
611 int err = -ENOMEM;
612
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800613 cb = nl_cb_clone(global->nl_cb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700614 if (!cb)
615 goto out;
616
617 err = nl_send_auto_complete(nl_handle, msg);
618 if (err < 0)
619 goto out;
620
621 err = 1;
622
623 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
624 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
625 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
626
627 if (valid_handler)
628 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM,
629 valid_handler, valid_data);
630
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700631 while (err > 0) {
632 int res = nl_recvmsgs(nl_handle, cb);
633 if (res) {
634 wpa_printf(MSG_INFO,
635 "nl80211: %s->nl_recvmsgs failed: %d",
636 __func__, res);
637 }
638 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700639 out:
640 nl_cb_put(cb);
641 nlmsg_free(msg);
642 return err;
643}
644
645
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800646static int send_and_recv_msgs_global(struct nl80211_global *global,
647 struct nl_msg *msg,
648 int (*valid_handler)(struct nl_msg *, void *),
649 void *valid_data)
650{
651 return send_and_recv(global, global->nl, msg, valid_handler,
652 valid_data);
653}
654
Dmitry Shmidt04949592012-07-19 12:16:46 -0700655
Dmitry Shmidt641185e2013-11-06 15:17:13 -0800656static int send_and_recv_msgs(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700657 struct nl_msg *msg,
658 int (*valid_handler)(struct nl_msg *, void *),
659 void *valid_data)
660{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800661 return send_and_recv(drv->global, drv->global->nl, msg,
662 valid_handler, valid_data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700663}
664
665
666struct family_data {
667 const char *group;
668 int id;
669};
670
671
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700672static int nl80211_set_iface_id(struct nl_msg *msg, struct i802_bss *bss)
673{
674 if (bss->wdev_id_set)
675 NLA_PUT_U64(msg, NL80211_ATTR_WDEV, bss->wdev_id);
676 else
677 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
678 return 0;
679
680nla_put_failure:
681 return -1;
682}
683
684
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700685static int family_handler(struct nl_msg *msg, void *arg)
686{
687 struct family_data *res = arg;
688 struct nlattr *tb[CTRL_ATTR_MAX + 1];
689 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
690 struct nlattr *mcgrp;
691 int i;
692
693 nla_parse(tb, CTRL_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
694 genlmsg_attrlen(gnlh, 0), NULL);
695 if (!tb[CTRL_ATTR_MCAST_GROUPS])
696 return NL_SKIP;
697
698 nla_for_each_nested(mcgrp, tb[CTRL_ATTR_MCAST_GROUPS], i) {
699 struct nlattr *tb2[CTRL_ATTR_MCAST_GRP_MAX + 1];
700 nla_parse(tb2, CTRL_ATTR_MCAST_GRP_MAX, nla_data(mcgrp),
701 nla_len(mcgrp), NULL);
702 if (!tb2[CTRL_ATTR_MCAST_GRP_NAME] ||
703 !tb2[CTRL_ATTR_MCAST_GRP_ID] ||
704 os_strncmp(nla_data(tb2[CTRL_ATTR_MCAST_GRP_NAME]),
705 res->group,
706 nla_len(tb2[CTRL_ATTR_MCAST_GRP_NAME])) != 0)
707 continue;
708 res->id = nla_get_u32(tb2[CTRL_ATTR_MCAST_GRP_ID]);
709 break;
710 };
711
712 return NL_SKIP;
713}
714
715
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800716static int nl_get_multicast_id(struct nl80211_global *global,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700717 const char *family, const char *group)
718{
719 struct nl_msg *msg;
720 int ret = -1;
721 struct family_data res = { group, -ENOENT };
722
723 msg = nlmsg_alloc();
724 if (!msg)
725 return -ENOMEM;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800726 genlmsg_put(msg, 0, 0, genl_ctrl_resolve(global->nl, "nlctrl"),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700727 0, 0, CTRL_CMD_GETFAMILY, 0);
728 NLA_PUT_STRING(msg, CTRL_ATTR_FAMILY_NAME, family);
729
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800730 ret = send_and_recv_msgs_global(global, msg, family_handler, &res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700731 msg = NULL;
732 if (ret == 0)
733 ret = res.id;
734
735nla_put_failure:
736 nlmsg_free(msg);
737 return ret;
738}
739
740
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800741static void * nl80211_cmd(struct wpa_driver_nl80211_data *drv,
742 struct nl_msg *msg, int flags, uint8_t cmd)
743{
744 return genlmsg_put(msg, 0, 0, drv->global->nl80211_id,
745 0, flags, cmd, 0);
746}
747
748
749struct wiphy_idx_data {
750 int wiphy_idx;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700751 enum nl80211_iftype nlmode;
752 u8 *macaddr;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800753};
754
755
756static int netdev_info_handler(struct nl_msg *msg, void *arg)
757{
758 struct nlattr *tb[NL80211_ATTR_MAX + 1];
759 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
760 struct wiphy_idx_data *info = arg;
761
762 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
763 genlmsg_attrlen(gnlh, 0), NULL);
764
765 if (tb[NL80211_ATTR_WIPHY])
766 info->wiphy_idx = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
767
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700768 if (tb[NL80211_ATTR_IFTYPE])
769 info->nlmode = nla_get_u32(tb[NL80211_ATTR_IFTYPE]);
770
771 if (tb[NL80211_ATTR_MAC] && info->macaddr)
772 os_memcpy(info->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
773 ETH_ALEN);
774
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800775 return NL_SKIP;
776}
777
778
779static int nl80211_get_wiphy_index(struct i802_bss *bss)
780{
781 struct nl_msg *msg;
782 struct wiphy_idx_data data = {
783 .wiphy_idx = -1,
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700784 .macaddr = NULL,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800785 };
786
787 msg = nlmsg_alloc();
788 if (!msg)
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700789 return NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800790
791 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_GET_INTERFACE);
792
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700793 if (nl80211_set_iface_id(msg, bss) < 0)
794 goto nla_put_failure;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800795
796 if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0)
797 return data.wiphy_idx;
798 msg = NULL;
799nla_put_failure:
800 nlmsg_free(msg);
801 return -1;
802}
803
804
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700805static enum nl80211_iftype nl80211_get_ifmode(struct i802_bss *bss)
806{
807 struct nl_msg *msg;
808 struct wiphy_idx_data data = {
809 .nlmode = NL80211_IFTYPE_UNSPECIFIED,
810 .macaddr = NULL,
811 };
812
813 msg = nlmsg_alloc();
814 if (!msg)
815 return -1;
816
817 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_GET_INTERFACE);
818
819 if (nl80211_set_iface_id(msg, bss) < 0)
820 goto nla_put_failure;
821
822 if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0)
823 return data.nlmode;
824 msg = NULL;
825nla_put_failure:
826 nlmsg_free(msg);
827 return NL80211_IFTYPE_UNSPECIFIED;
828}
829
830
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700831static int nl80211_get_macaddr(struct i802_bss *bss)
832{
833 struct nl_msg *msg;
834 struct wiphy_idx_data data = {
835 .macaddr = bss->addr,
836 };
837
838 msg = nlmsg_alloc();
839 if (!msg)
840 return NL80211_IFTYPE_UNSPECIFIED;
841
842 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_GET_INTERFACE);
843 if (nl80211_set_iface_id(msg, bss) < 0)
844 goto nla_put_failure;
845
846 return send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data);
847
848nla_put_failure:
849 nlmsg_free(msg);
850 return NL80211_IFTYPE_UNSPECIFIED;
851}
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700852
853
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800854static int nl80211_register_beacons(struct wpa_driver_nl80211_data *drv,
855 struct nl80211_wiphy_data *w)
856{
857 struct nl_msg *msg;
858 int ret = -1;
859
860 msg = nlmsg_alloc();
861 if (!msg)
862 return -1;
863
864 nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_BEACONS);
865
866 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, w->wiphy_idx);
867
868 ret = send_and_recv(drv->global, w->nl_beacons, msg, NULL, NULL);
869 msg = NULL;
870 if (ret) {
871 wpa_printf(MSG_DEBUG, "nl80211: Register beacons command "
872 "failed: ret=%d (%s)",
873 ret, strerror(-ret));
874 goto nla_put_failure;
875 }
876 ret = 0;
877nla_put_failure:
878 nlmsg_free(msg);
879 return ret;
880}
881
882
883static void nl80211_recv_beacons(int sock, void *eloop_ctx, void *handle)
884{
885 struct nl80211_wiphy_data *w = eloop_ctx;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700886 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800887
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800888 wpa_printf(MSG_EXCESSIVE, "nl80211: Beacon event message available");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800889
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700890 res = nl_recvmsgs(handle, w->nl_cb);
891 if (res) {
892 wpa_printf(MSG_INFO, "nl80211: %s->nl_recvmsgs failed: %d",
893 __func__, res);
894 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800895}
896
897
898static int process_beacon_event(struct nl_msg *msg, void *arg)
899{
900 struct nl80211_wiphy_data *w = arg;
901 struct wpa_driver_nl80211_data *drv;
902 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
903 struct nlattr *tb[NL80211_ATTR_MAX + 1];
904 union wpa_event_data event;
905
906 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
907 genlmsg_attrlen(gnlh, 0), NULL);
908
909 if (gnlh->cmd != NL80211_CMD_FRAME) {
910 wpa_printf(MSG_DEBUG, "nl80211: Unexpected beacon event? (%d)",
911 gnlh->cmd);
912 return NL_SKIP;
913 }
914
915 if (!tb[NL80211_ATTR_FRAME])
916 return NL_SKIP;
917
918 dl_list_for_each(drv, &w->drvs, struct wpa_driver_nl80211_data,
919 wiphy_list) {
920 os_memset(&event, 0, sizeof(event));
921 event.rx_mgmt.frame = nla_data(tb[NL80211_ATTR_FRAME]);
922 event.rx_mgmt.frame_len = nla_len(tb[NL80211_ATTR_FRAME]);
923 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
924 }
925
926 return NL_SKIP;
927}
928
929
930static struct nl80211_wiphy_data *
931nl80211_get_wiphy_data_ap(struct i802_bss *bss)
932{
933 static DEFINE_DL_LIST(nl80211_wiphys);
934 struct nl80211_wiphy_data *w;
935 int wiphy_idx, found = 0;
936 struct i802_bss *tmp_bss;
937
938 if (bss->wiphy_data != NULL)
939 return bss->wiphy_data;
940
941 wiphy_idx = nl80211_get_wiphy_index(bss);
942
943 dl_list_for_each(w, &nl80211_wiphys, struct nl80211_wiphy_data, list) {
944 if (w->wiphy_idx == wiphy_idx)
945 goto add;
946 }
947
948 /* alloc new one */
949 w = os_zalloc(sizeof(*w));
950 if (w == NULL)
951 return NULL;
952 w->wiphy_idx = wiphy_idx;
953 dl_list_init(&w->bsss);
954 dl_list_init(&w->drvs);
955
956 w->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
957 if (!w->nl_cb) {
958 os_free(w);
959 return NULL;
960 }
961 nl_cb_set(w->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL);
962 nl_cb_set(w->nl_cb, NL_CB_VALID, NL_CB_CUSTOM, process_beacon_event,
963 w);
964
965 w->nl_beacons = nl_create_handle(bss->drv->global->nl_cb,
966 "wiphy beacons");
967 if (w->nl_beacons == NULL) {
968 os_free(w);
969 return NULL;
970 }
971
972 if (nl80211_register_beacons(bss->drv, w)) {
973 nl_destroy_handles(&w->nl_beacons);
974 os_free(w);
975 return NULL;
976 }
977
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700978 nl80211_register_eloop_read(&w->nl_beacons, nl80211_recv_beacons, w);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800979
980 dl_list_add(&nl80211_wiphys, &w->list);
981
982add:
983 /* drv entry for this bss already there? */
984 dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
985 if (tmp_bss->drv == bss->drv) {
986 found = 1;
987 break;
988 }
989 }
990 /* if not add it */
991 if (!found)
992 dl_list_add(&w->drvs, &bss->drv->wiphy_list);
993
994 dl_list_add(&w->bsss, &bss->wiphy_list);
995 bss->wiphy_data = w;
996 return w;
997}
998
999
1000static void nl80211_put_wiphy_data_ap(struct i802_bss *bss)
1001{
1002 struct nl80211_wiphy_data *w = bss->wiphy_data;
1003 struct i802_bss *tmp_bss;
1004 int found = 0;
1005
1006 if (w == NULL)
1007 return;
1008 bss->wiphy_data = NULL;
1009 dl_list_del(&bss->wiphy_list);
1010
1011 /* still any for this drv present? */
1012 dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
1013 if (tmp_bss->drv == bss->drv) {
1014 found = 1;
1015 break;
1016 }
1017 }
1018 /* if not remove it */
1019 if (!found)
1020 dl_list_del(&bss->drv->wiphy_list);
1021
1022 if (!dl_list_empty(&w->bsss))
1023 return;
1024
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001025 nl80211_destroy_eloop_handle(&w->nl_beacons);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001026
1027 nl_cb_put(w->nl_cb);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001028 dl_list_del(&w->list);
1029 os_free(w);
1030}
1031
1032
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001033static int wpa_driver_nl80211_get_bssid(void *priv, u8 *bssid)
1034{
1035 struct i802_bss *bss = priv;
1036 struct wpa_driver_nl80211_data *drv = bss->drv;
1037 if (!drv->associated)
1038 return -1;
1039 os_memcpy(bssid, drv->bssid, ETH_ALEN);
1040 return 0;
1041}
1042
1043
1044static int wpa_driver_nl80211_get_ssid(void *priv, u8 *ssid)
1045{
1046 struct i802_bss *bss = priv;
1047 struct wpa_driver_nl80211_data *drv = bss->drv;
1048 if (!drv->associated)
1049 return -1;
1050 os_memcpy(ssid, drv->ssid, drv->ssid_len);
1051 return drv->ssid_len;
1052}
1053
1054
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001055static void wpa_driver_nl80211_event_newlink(
1056 struct wpa_driver_nl80211_data *drv, char *ifname)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001057{
1058 union wpa_event_data event;
1059
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001060 if (os_strcmp(drv->first_bss->ifname, ifname) == 0) {
1061 if (if_nametoindex(drv->first_bss->ifname) == 0) {
1062 wpa_printf(MSG_DEBUG, "nl80211: Interface %s does not exist - ignore RTM_NEWLINK",
1063 drv->first_bss->ifname);
1064 return;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001065 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001066 if (!drv->if_removed)
1067 return;
1068 wpa_printf(MSG_DEBUG, "nl80211: Mark if_removed=0 for %s based on RTM_NEWLINK event",
1069 drv->first_bss->ifname);
1070 drv->if_removed = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001071 }
1072
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001073 os_memset(&event, 0, sizeof(event));
1074 os_strlcpy(event.interface_status.ifname, ifname,
1075 sizeof(event.interface_status.ifname));
1076 event.interface_status.ievent = EVENT_INTERFACE_ADDED;
1077 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
1078}
1079
1080
1081static void wpa_driver_nl80211_event_dellink(
1082 struct wpa_driver_nl80211_data *drv, char *ifname)
1083{
1084 union wpa_event_data event;
1085
1086 if (os_strcmp(drv->first_bss->ifname, ifname) == 0) {
1087 if (drv->if_removed) {
1088 wpa_printf(MSG_DEBUG, "nl80211: if_removed already set - ignore RTM_DELLINK event for %s",
1089 ifname);
1090 return;
1091 }
1092 wpa_printf(MSG_DEBUG, "RTM_DELLINK: Interface '%s' removed - mark if_removed=1",
1093 ifname);
1094 drv->if_removed = 1;
1095 } else {
1096 wpa_printf(MSG_DEBUG, "RTM_DELLINK: Interface '%s' removed",
1097 ifname);
1098 }
1099
1100 os_memset(&event, 0, sizeof(event));
1101 os_strlcpy(event.interface_status.ifname, ifname,
1102 sizeof(event.interface_status.ifname));
1103 event.interface_status.ievent = EVENT_INTERFACE_REMOVED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001104 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
1105}
1106
1107
1108static int wpa_driver_nl80211_own_ifname(struct wpa_driver_nl80211_data *drv,
1109 u8 *buf, size_t len)
1110{
1111 int attrlen, rta_len;
1112 struct rtattr *attr;
1113
1114 attrlen = len;
1115 attr = (struct rtattr *) buf;
1116
1117 rta_len = RTA_ALIGN(sizeof(struct rtattr));
1118 while (RTA_OK(attr, attrlen)) {
1119 if (attr->rta_type == IFLA_IFNAME) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001120 if (os_strcmp(((char *) attr) + rta_len,
1121 drv->first_bss->ifname) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001122 return 1;
1123 else
1124 break;
1125 }
1126 attr = RTA_NEXT(attr, attrlen);
1127 }
1128
1129 return 0;
1130}
1131
1132
1133static int wpa_driver_nl80211_own_ifindex(struct wpa_driver_nl80211_data *drv,
1134 int ifindex, u8 *buf, size_t len)
1135{
1136 if (drv->ifindex == ifindex)
1137 return 1;
1138
1139 if (drv->if_removed && wpa_driver_nl80211_own_ifname(drv, buf, len)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001140 wpa_printf(MSG_DEBUG, "nl80211: Update ifindex for a removed "
1141 "interface");
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001142 wpa_driver_nl80211_finish_drv_init(drv, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001143 return 1;
1144 }
1145
1146 return 0;
1147}
1148
1149
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001150static struct wpa_driver_nl80211_data *
1151nl80211_find_drv(struct nl80211_global *global, int idx, u8 *buf, size_t len)
1152{
1153 struct wpa_driver_nl80211_data *drv;
1154 dl_list_for_each(drv, &global->interfaces,
1155 struct wpa_driver_nl80211_data, list) {
1156 if (wpa_driver_nl80211_own_ifindex(drv, idx, buf, len) ||
1157 have_ifidx(drv, idx))
1158 return drv;
1159 }
1160 return NULL;
1161}
1162
1163
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001164static void wpa_driver_nl80211_event_rtm_newlink(void *ctx,
1165 struct ifinfomsg *ifi,
1166 u8 *buf, size_t len)
1167{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001168 struct nl80211_global *global = ctx;
1169 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001170 int attrlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001171 struct rtattr *attr;
1172 u32 brid = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001173 char namebuf[IFNAMSIZ];
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001174 char ifname[IFNAMSIZ + 1];
1175 char extra[100], *pos, *end;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001176
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001177 drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
1178 if (!drv) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001179 wpa_printf(MSG_DEBUG, "nl80211: Ignore RTM_NEWLINK event for foreign ifindex %d",
1180 ifi->ifi_index);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001181 return;
1182 }
1183
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001184 extra[0] = '\0';
1185 pos = extra;
1186 end = pos + sizeof(extra);
1187 ifname[0] = '\0';
1188
1189 attrlen = len;
1190 attr = (struct rtattr *) buf;
1191 while (RTA_OK(attr, attrlen)) {
1192 switch (attr->rta_type) {
1193 case IFLA_IFNAME:
1194 if (RTA_PAYLOAD(attr) >= IFNAMSIZ)
1195 break;
1196 os_memcpy(ifname, RTA_DATA(attr), RTA_PAYLOAD(attr));
1197 ifname[RTA_PAYLOAD(attr)] = '\0';
1198 break;
1199 case IFLA_MASTER:
1200 brid = nla_get_u32((struct nlattr *) attr);
1201 pos += os_snprintf(pos, end - pos, " master=%u", brid);
1202 break;
1203 case IFLA_WIRELESS:
1204 pos += os_snprintf(pos, end - pos, " wext");
1205 break;
1206 case IFLA_OPERSTATE:
1207 pos += os_snprintf(pos, end - pos, " operstate=%u",
1208 nla_get_u32((struct nlattr *) attr));
1209 break;
1210 case IFLA_LINKMODE:
1211 pos += os_snprintf(pos, end - pos, " linkmode=%u",
1212 nla_get_u32((struct nlattr *) attr));
1213 break;
1214 }
1215 attr = RTA_NEXT(attr, attrlen);
1216 }
1217 extra[sizeof(extra) - 1] = '\0';
1218
1219 wpa_printf(MSG_DEBUG, "RTM_NEWLINK: ifi_index=%d ifname=%s%s ifi_flags=0x%x (%s%s%s%s)",
1220 ifi->ifi_index, ifname, extra, ifi->ifi_flags,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001221 (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
1222 (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
1223 (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
1224 (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
1225
1226 if (!drv->if_disabled && !(ifi->ifi_flags & IFF_UP)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001227 if (if_indextoname(ifi->ifi_index, namebuf) &&
1228 linux_iface_up(drv->global->ioctl_sock,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001229 drv->first_bss->ifname) > 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001230 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
1231 "event since interface %s is up", namebuf);
1232 return;
1233 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001234 wpa_printf(MSG_DEBUG, "nl80211: Interface down");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001235 if (drv->ignore_if_down_event) {
1236 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
1237 "event generated by mode change");
1238 drv->ignore_if_down_event = 0;
1239 } else {
1240 drv->if_disabled = 1;
1241 wpa_supplicant_event(drv->ctx,
1242 EVENT_INTERFACE_DISABLED, NULL);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001243
1244 /*
1245 * Try to get drv again, since it may be removed as
1246 * part of the EVENT_INTERFACE_DISABLED handling for
1247 * dynamic interfaces
1248 */
1249 drv = nl80211_find_drv(global, ifi->ifi_index,
1250 buf, len);
1251 if (!drv)
1252 return;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001253 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001254 }
1255
1256 if (drv->if_disabled && (ifi->ifi_flags & IFF_UP)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001257 if (if_indextoname(ifi->ifi_index, namebuf) &&
1258 linux_iface_up(drv->global->ioctl_sock,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001259 drv->first_bss->ifname) == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001260 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
1261 "event since interface %s is down",
1262 namebuf);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001263 } else if (if_nametoindex(drv->first_bss->ifname) == 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001264 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
1265 "event since interface %s does not exist",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001266 drv->first_bss->ifname);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001267 } else if (drv->if_removed) {
1268 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
1269 "event since interface %s is marked "
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001270 "removed", drv->first_bss->ifname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001271 } else {
1272 wpa_printf(MSG_DEBUG, "nl80211: Interface up");
1273 drv->if_disabled = 0;
1274 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_ENABLED,
1275 NULL);
1276 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001277 }
1278
1279 /*
1280 * Some drivers send the association event before the operup event--in
1281 * this case, lifting operstate in wpa_driver_nl80211_set_operstate()
1282 * fails. This will hit us when wpa_supplicant does not need to do
1283 * IEEE 802.1X authentication
1284 */
1285 if (drv->operstate == 1 &&
1286 (ifi->ifi_flags & (IFF_LOWER_UP | IFF_DORMANT)) == IFF_LOWER_UP &&
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001287 !(ifi->ifi_flags & IFF_RUNNING)) {
1288 wpa_printf(MSG_DEBUG, "nl80211: Set IF_OPER_UP again based on ifi_flags and expected operstate");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001289 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001290 -1, IF_OPER_UP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001291 }
1292
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001293 if (ifname[0])
1294 wpa_driver_nl80211_event_newlink(drv, ifname);
1295
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001296 if (ifi->ifi_family == AF_BRIDGE && brid) {
1297 /* device has been added to bridge */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001298 if_indextoname(brid, namebuf);
1299 wpa_printf(MSG_DEBUG, "nl80211: Add ifindex %u for bridge %s",
1300 brid, namebuf);
1301 add_ifidx(drv, brid);
1302 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001303}
1304
1305
1306static void wpa_driver_nl80211_event_rtm_dellink(void *ctx,
1307 struct ifinfomsg *ifi,
1308 u8 *buf, size_t len)
1309{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001310 struct nl80211_global *global = ctx;
1311 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001312 int attrlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001313 struct rtattr *attr;
1314 u32 brid = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001315 char ifname[IFNAMSIZ + 1];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001316
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001317 drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
1318 if (!drv) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001319 wpa_printf(MSG_DEBUG, "nl80211: Ignore RTM_DELLINK event for foreign ifindex %d",
1320 ifi->ifi_index);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001321 return;
1322 }
1323
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001324 ifname[0] = '\0';
1325
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001326 attrlen = len;
1327 attr = (struct rtattr *) buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001328 while (RTA_OK(attr, attrlen)) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001329 switch (attr->rta_type) {
1330 case IFLA_IFNAME:
1331 if (RTA_PAYLOAD(attr) >= IFNAMSIZ)
1332 break;
1333 os_memcpy(ifname, RTA_DATA(attr), RTA_PAYLOAD(attr));
1334 ifname[RTA_PAYLOAD(attr)] = '\0';
1335 break;
1336 case IFLA_MASTER:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001337 brid = nla_get_u32((struct nlattr *) attr);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001338 break;
1339 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001340 attr = RTA_NEXT(attr, attrlen);
1341 }
1342
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001343 if (ifname[0])
1344 wpa_driver_nl80211_event_dellink(drv, ifname);
1345
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001346 if (ifi->ifi_family == AF_BRIDGE && brid) {
1347 /* device has been removed from bridge */
1348 char namebuf[IFNAMSIZ];
1349 if_indextoname(brid, namebuf);
1350 wpa_printf(MSG_DEBUG, "nl80211: Remove ifindex %u for bridge "
1351 "%s", brid, namebuf);
1352 del_ifidx(drv, brid);
1353 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001354}
1355
1356
1357static void mlme_event_auth(struct wpa_driver_nl80211_data *drv,
1358 const u8 *frame, size_t len)
1359{
1360 const struct ieee80211_mgmt *mgmt;
1361 union wpa_event_data event;
1362
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001363 wpa_printf(MSG_DEBUG, "nl80211: Authenticate event");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001364 mgmt = (const struct ieee80211_mgmt *) frame;
1365 if (len < 24 + sizeof(mgmt->u.auth)) {
1366 wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
1367 "frame");
1368 return;
1369 }
1370
1371 os_memcpy(drv->auth_bssid, mgmt->sa, ETH_ALEN);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001372 os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001373 os_memset(&event, 0, sizeof(event));
1374 os_memcpy(event.auth.peer, mgmt->sa, ETH_ALEN);
1375 event.auth.auth_type = le_to_host16(mgmt->u.auth.auth_alg);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001376 event.auth.auth_transaction =
1377 le_to_host16(mgmt->u.auth.auth_transaction);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001378 event.auth.status_code = le_to_host16(mgmt->u.auth.status_code);
1379 if (len > 24 + sizeof(mgmt->u.auth)) {
1380 event.auth.ies = mgmt->u.auth.variable;
1381 event.auth.ies_len = len - 24 - sizeof(mgmt->u.auth);
1382 }
1383
1384 wpa_supplicant_event(drv->ctx, EVENT_AUTH, &event);
1385}
1386
1387
Jouni Malinen87fd2792011-05-16 18:35:42 +03001388static unsigned int nl80211_get_assoc_freq(struct wpa_driver_nl80211_data *drv)
1389{
1390 struct nl_msg *msg;
1391 int ret;
1392 struct nl80211_bss_info_arg arg;
1393
1394 os_memset(&arg, 0, sizeof(arg));
1395 msg = nlmsg_alloc();
1396 if (!msg)
1397 goto nla_put_failure;
1398
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001399 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
Jouni Malinen87fd2792011-05-16 18:35:42 +03001400 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
1401
1402 arg.drv = drv;
1403 ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
1404 msg = NULL;
1405 if (ret == 0) {
1406 wpa_printf(MSG_DEBUG, "nl80211: Operating frequency for the "
1407 "associated BSS from scan results: %u MHz",
1408 arg.assoc_freq);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001409 if (arg.assoc_freq)
1410 drv->assoc_freq = arg.assoc_freq;
1411 return drv->assoc_freq;
Jouni Malinen87fd2792011-05-16 18:35:42 +03001412 }
1413 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
1414 "(%s)", ret, strerror(-ret));
1415nla_put_failure:
1416 nlmsg_free(msg);
1417 return drv->assoc_freq;
1418}
1419
1420
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001421static void mlme_event_assoc(struct wpa_driver_nl80211_data *drv,
1422 const u8 *frame, size_t len)
1423{
1424 const struct ieee80211_mgmt *mgmt;
1425 union wpa_event_data event;
1426 u16 status;
1427
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001428 wpa_printf(MSG_DEBUG, "nl80211: Associate event");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001429 mgmt = (const struct ieee80211_mgmt *) frame;
1430 if (len < 24 + sizeof(mgmt->u.assoc_resp)) {
1431 wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
1432 "frame");
1433 return;
1434 }
1435
1436 status = le_to_host16(mgmt->u.assoc_resp.status_code);
1437 if (status != WLAN_STATUS_SUCCESS) {
1438 os_memset(&event, 0, sizeof(event));
1439 event.assoc_reject.bssid = mgmt->bssid;
1440 if (len > 24 + sizeof(mgmt->u.assoc_resp)) {
1441 event.assoc_reject.resp_ies =
1442 (u8 *) mgmt->u.assoc_resp.variable;
1443 event.assoc_reject.resp_ies_len =
1444 len - 24 - sizeof(mgmt->u.assoc_resp);
1445 }
1446 event.assoc_reject.status_code = status;
1447
1448 wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
1449 return;
1450 }
1451
1452 drv->associated = 1;
1453 os_memcpy(drv->bssid, mgmt->sa, ETH_ALEN);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001454 os_memcpy(drv->prev_bssid, mgmt->sa, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001455
1456 os_memset(&event, 0, sizeof(event));
1457 if (len > 24 + sizeof(mgmt->u.assoc_resp)) {
1458 event.assoc_info.resp_ies = (u8 *) mgmt->u.assoc_resp.variable;
1459 event.assoc_info.resp_ies_len =
1460 len - 24 - sizeof(mgmt->u.assoc_resp);
1461 }
1462
1463 event.assoc_info.freq = drv->assoc_freq;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001464
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001465 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
1466}
1467
1468
1469static void mlme_event_connect(struct wpa_driver_nl80211_data *drv,
1470 enum nl80211_commands cmd, struct nlattr *status,
1471 struct nlattr *addr, struct nlattr *req_ie,
1472 struct nlattr *resp_ie)
1473{
1474 union wpa_event_data event;
1475
1476 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
1477 /*
1478 * Avoid reporting two association events that would confuse
1479 * the core code.
1480 */
1481 wpa_printf(MSG_DEBUG, "nl80211: Ignore connect event (cmd=%d) "
1482 "when using userspace SME", cmd);
1483 return;
1484 }
1485
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001486 if (cmd == NL80211_CMD_CONNECT)
1487 wpa_printf(MSG_DEBUG, "nl80211: Connect event");
1488 else if (cmd == NL80211_CMD_ROAM)
1489 wpa_printf(MSG_DEBUG, "nl80211: Roam event");
1490
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001491 os_memset(&event, 0, sizeof(event));
1492 if (cmd == NL80211_CMD_CONNECT &&
1493 nla_get_u16(status) != WLAN_STATUS_SUCCESS) {
1494 if (addr)
1495 event.assoc_reject.bssid = nla_data(addr);
1496 if (resp_ie) {
1497 event.assoc_reject.resp_ies = nla_data(resp_ie);
1498 event.assoc_reject.resp_ies_len = nla_len(resp_ie);
1499 }
1500 event.assoc_reject.status_code = nla_get_u16(status);
1501 wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
1502 return;
1503 }
1504
1505 drv->associated = 1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001506 if (addr) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001507 os_memcpy(drv->bssid, nla_data(addr), ETH_ALEN);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001508 os_memcpy(drv->prev_bssid, drv->bssid, ETH_ALEN);
1509 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001510
1511 if (req_ie) {
1512 event.assoc_info.req_ies = nla_data(req_ie);
1513 event.assoc_info.req_ies_len = nla_len(req_ie);
1514 }
1515 if (resp_ie) {
1516 event.assoc_info.resp_ies = nla_data(resp_ie);
1517 event.assoc_info.resp_ies_len = nla_len(resp_ie);
1518 }
1519
Jouni Malinen87fd2792011-05-16 18:35:42 +03001520 event.assoc_info.freq = nl80211_get_assoc_freq(drv);
1521
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001522 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
1523}
1524
1525
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001526static void mlme_event_disconnect(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001527 struct nlattr *reason, struct nlattr *addr,
1528 struct nlattr *by_ap)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001529{
1530 union wpa_event_data data;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001531 unsigned int locally_generated = by_ap == NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001532
1533 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
1534 /*
1535 * Avoid reporting two disassociation events that could
1536 * confuse the core code.
1537 */
1538 wpa_printf(MSG_DEBUG, "nl80211: Ignore disconnect "
1539 "event when using userspace SME");
1540 return;
1541 }
1542
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001543 if (drv->ignore_next_local_disconnect) {
1544 drv->ignore_next_local_disconnect = 0;
1545 if (locally_generated) {
1546 wpa_printf(MSG_DEBUG, "nl80211: Ignore disconnect "
1547 "event triggered during reassociation");
1548 return;
1549 }
1550 wpa_printf(MSG_WARNING, "nl80211: Was expecting local "
1551 "disconnect but got another disconnect "
1552 "event first");
1553 }
1554
1555 wpa_printf(MSG_DEBUG, "nl80211: Disconnect event");
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001556 nl80211_mark_disconnected(drv);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001557 os_memset(&data, 0, sizeof(data));
1558 if (reason)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001559 data.deauth_info.reason_code = nla_get_u16(reason);
1560 data.deauth_info.locally_generated = by_ap == NULL;
1561 wpa_supplicant_event(drv->ctx, EVENT_DEAUTH, &data);
1562}
1563
1564
Dmitry Shmidt97672262014-02-03 13:02:54 -08001565static int calculate_chan_offset(int width, int freq, int cf1, int cf2)
1566{
1567 int freq1 = 0;
1568
1569 switch (convert2width(width)) {
1570 case CHAN_WIDTH_20_NOHT:
1571 case CHAN_WIDTH_20:
1572 return 0;
1573 case CHAN_WIDTH_40:
1574 freq1 = cf1 - 10;
1575 break;
1576 case CHAN_WIDTH_80:
1577 freq1 = cf1 - 30;
1578 break;
1579 case CHAN_WIDTH_160:
1580 freq1 = cf1 - 70;
1581 break;
1582 case CHAN_WIDTH_UNKNOWN:
1583 case CHAN_WIDTH_80P80:
1584 /* FIXME: implement this */
1585 return 0;
1586 }
1587
1588 return (abs(freq - freq1) / 20) % 2 == 0 ? 1 : -1;
1589}
1590
1591
Dmitry Shmidt04949592012-07-19 12:16:46 -07001592static void mlme_event_ch_switch(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001593 struct nlattr *ifindex, struct nlattr *freq,
1594 struct nlattr *type, struct nlattr *bw,
1595 struct nlattr *cf1, struct nlattr *cf2)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001596{
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001597 struct i802_bss *bss;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001598 union wpa_event_data data;
1599 int ht_enabled = 1;
1600 int chan_offset = 0;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001601 int ifidx;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001602
1603 wpa_printf(MSG_DEBUG, "nl80211: Channel switch event");
1604
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001605 if (!freq)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001606 return;
1607
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001608 ifidx = nla_get_u32(ifindex);
1609 for (bss = drv->first_bss; bss; bss = bss->next)
1610 if (bss->ifindex == ifidx)
1611 break;
1612
1613 if (bss == NULL) {
1614 wpa_printf(MSG_WARNING, "nl80211: Unknown ifindex (%d) for channel switch, ignoring",
1615 ifidx);
1616 return;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001617 }
1618
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001619 if (type) {
1620 switch (nla_get_u32(type)) {
1621 case NL80211_CHAN_NO_HT:
1622 ht_enabled = 0;
1623 break;
1624 case NL80211_CHAN_HT20:
1625 break;
1626 case NL80211_CHAN_HT40PLUS:
1627 chan_offset = 1;
1628 break;
1629 case NL80211_CHAN_HT40MINUS:
1630 chan_offset = -1;
1631 break;
1632 }
Dmitry Shmidt97672262014-02-03 13:02:54 -08001633 } else if (bw && cf1) {
1634 /* This can happen for example with VHT80 ch switch */
1635 chan_offset = calculate_chan_offset(nla_get_u32(bw),
1636 nla_get_u32(freq),
1637 nla_get_u32(cf1),
1638 cf2 ? nla_get_u32(cf2) : 0);
1639 } else {
1640 wpa_printf(MSG_WARNING, "nl80211: Unknown secondary channel information - following channel definition calculations may fail");
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001641 }
1642
1643 os_memset(&data, 0, sizeof(data));
Dmitry Shmidt04949592012-07-19 12:16:46 -07001644 data.ch_switch.freq = nla_get_u32(freq);
1645 data.ch_switch.ht_enabled = ht_enabled;
1646 data.ch_switch.ch_offset = chan_offset;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001647 if (bw)
1648 data.ch_switch.ch_width = convert2width(nla_get_u32(bw));
1649 if (cf1)
1650 data.ch_switch.cf1 = nla_get_u32(cf1);
1651 if (cf2)
1652 data.ch_switch.cf2 = nla_get_u32(cf2);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001653
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001654 bss->freq = data.ch_switch.freq;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001655
Dmitry Shmidt04949592012-07-19 12:16:46 -07001656 wpa_supplicant_event(drv->ctx, EVENT_CH_SWITCH, &data);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001657}
1658
1659
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001660static void mlme_timeout_event(struct wpa_driver_nl80211_data *drv,
1661 enum nl80211_commands cmd, struct nlattr *addr)
1662{
1663 union wpa_event_data event;
1664 enum wpa_event_type ev;
1665
1666 if (nla_len(addr) != ETH_ALEN)
1667 return;
1668
1669 wpa_printf(MSG_DEBUG, "nl80211: MLME event %d; timeout with " MACSTR,
1670 cmd, MAC2STR((u8 *) nla_data(addr)));
1671
1672 if (cmd == NL80211_CMD_AUTHENTICATE)
1673 ev = EVENT_AUTH_TIMED_OUT;
1674 else if (cmd == NL80211_CMD_ASSOCIATE)
1675 ev = EVENT_ASSOC_TIMED_OUT;
1676 else
1677 return;
1678
1679 os_memset(&event, 0, sizeof(event));
1680 os_memcpy(event.timeout_event.addr, nla_data(addr), ETH_ALEN);
1681 wpa_supplicant_event(drv->ctx, ev, &event);
1682}
1683
1684
Dmitry Shmidt98660862014-03-11 17:26:21 -07001685static void mlme_event_mgmt(struct i802_bss *bss,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001686 struct nlattr *freq, struct nlattr *sig,
1687 const u8 *frame, size_t len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001688{
Dmitry Shmidt98660862014-03-11 17:26:21 -07001689 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001690 const struct ieee80211_mgmt *mgmt;
1691 union wpa_event_data event;
1692 u16 fc, stype;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001693 int ssi_signal = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001694 int rx_freq = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001695
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001696 wpa_printf(MSG_MSGDUMP, "nl80211: Frame event");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001697 mgmt = (const struct ieee80211_mgmt *) frame;
1698 if (len < 24) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001699 wpa_printf(MSG_DEBUG, "nl80211: Too short management frame");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001700 return;
1701 }
1702
1703 fc = le_to_host16(mgmt->frame_control);
1704 stype = WLAN_FC_GET_STYPE(fc);
1705
Dmitry Shmidt04949592012-07-19 12:16:46 -07001706 if (sig)
1707 ssi_signal = (s32) nla_get_u32(sig);
1708
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001709 os_memset(&event, 0, sizeof(event));
1710 if (freq) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001711 event.rx_mgmt.freq = nla_get_u32(freq);
1712 rx_freq = drv->last_mgmt_freq = event.rx_mgmt.freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001713 }
Dmitry Shmidt56052862013-10-04 10:23:25 -07001714 wpa_printf(MSG_DEBUG,
1715 "nl80211: RX frame freq=%d ssi_signal=%d stype=%u len=%u",
1716 rx_freq, ssi_signal, stype, (unsigned int) len);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001717 event.rx_mgmt.frame = frame;
1718 event.rx_mgmt.frame_len = len;
1719 event.rx_mgmt.ssi_signal = ssi_signal;
Dmitry Shmidt98660862014-03-11 17:26:21 -07001720 event.rx_mgmt.drv_priv = bss;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001721 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001722}
1723
1724
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001725static void mlme_event_mgmt_tx_status(struct wpa_driver_nl80211_data *drv,
1726 struct nlattr *cookie, const u8 *frame,
1727 size_t len, struct nlattr *ack)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001728{
1729 union wpa_event_data event;
1730 const struct ieee80211_hdr *hdr;
1731 u16 fc;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001732
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001733 wpa_printf(MSG_DEBUG, "nl80211: Frame TX status event");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001734 if (!is_ap_interface(drv->nlmode)) {
1735 u64 cookie_val;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001736
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001737 if (!cookie)
1738 return;
1739
1740 cookie_val = nla_get_u64(cookie);
1741 wpa_printf(MSG_DEBUG, "nl80211: Action TX status:"
1742 " cookie=0%llx%s (ack=%d)",
1743 (long long unsigned int) cookie_val,
1744 cookie_val == drv->send_action_cookie ?
1745 " (match)" : " (unknown)", ack != NULL);
1746 if (cookie_val != drv->send_action_cookie)
1747 return;
1748 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001749
1750 hdr = (const struct ieee80211_hdr *) frame;
1751 fc = le_to_host16(hdr->frame_control);
1752
1753 os_memset(&event, 0, sizeof(event));
1754 event.tx_status.type = WLAN_FC_GET_TYPE(fc);
1755 event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
1756 event.tx_status.dst = hdr->addr1;
1757 event.tx_status.data = frame;
1758 event.tx_status.data_len = len;
1759 event.tx_status.ack = ack != NULL;
1760 wpa_supplicant_event(drv->ctx, EVENT_TX_STATUS, &event);
1761}
1762
1763
1764static void mlme_event_deauth_disassoc(struct wpa_driver_nl80211_data *drv,
1765 enum wpa_event_type type,
1766 const u8 *frame, size_t len)
1767{
1768 const struct ieee80211_mgmt *mgmt;
1769 union wpa_event_data event;
1770 const u8 *bssid = NULL;
1771 u16 reason_code = 0;
1772
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001773 if (type == EVENT_DEAUTH)
1774 wpa_printf(MSG_DEBUG, "nl80211: Deauthenticate event");
1775 else
1776 wpa_printf(MSG_DEBUG, "nl80211: Disassociate event");
1777
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001778 mgmt = (const struct ieee80211_mgmt *) frame;
1779 if (len >= 24) {
1780 bssid = mgmt->bssid;
1781
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001782 if ((drv->capa.flags & WPA_DRIVER_FLAGS_SME) &&
1783 !drv->associated &&
1784 os_memcmp(bssid, drv->auth_bssid, ETH_ALEN) != 0 &&
1785 os_memcmp(bssid, drv->auth_attempt_bssid, ETH_ALEN) != 0 &&
1786 os_memcmp(bssid, drv->prev_bssid, ETH_ALEN) == 0) {
1787 /*
1788 * Avoid issues with some roaming cases where
1789 * disconnection event for the old AP may show up after
1790 * we have started connection with the new AP.
1791 */
1792 wpa_printf(MSG_DEBUG, "nl80211: Ignore deauth/disassoc event from old AP " MACSTR " when already authenticating with " MACSTR,
1793 MAC2STR(bssid),
1794 MAC2STR(drv->auth_attempt_bssid));
1795 return;
1796 }
1797
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001798 if (drv->associated != 0 &&
1799 os_memcmp(bssid, drv->bssid, ETH_ALEN) != 0 &&
1800 os_memcmp(bssid, drv->auth_bssid, ETH_ALEN) != 0) {
1801 /*
1802 * We have presumably received this deauth as a
1803 * response to a clear_state_mismatch() outgoing
1804 * deauth. Don't let it take us offline!
1805 */
1806 wpa_printf(MSG_DEBUG, "nl80211: Deauth received "
1807 "from Unknown BSSID " MACSTR " -- ignoring",
1808 MAC2STR(bssid));
1809 return;
1810 }
1811 }
1812
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001813 nl80211_mark_disconnected(drv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001814 os_memset(&event, 0, sizeof(event));
1815
1816 /* Note: Same offset for Reason Code in both frame subtypes */
1817 if (len >= 24 + sizeof(mgmt->u.deauth))
1818 reason_code = le_to_host16(mgmt->u.deauth.reason_code);
1819
1820 if (type == EVENT_DISASSOC) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001821 event.disassoc_info.locally_generated =
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001822 !os_memcmp(mgmt->sa, drv->first_bss->addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001823 event.disassoc_info.addr = bssid;
1824 event.disassoc_info.reason_code = reason_code;
1825 if (frame + len > mgmt->u.disassoc.variable) {
1826 event.disassoc_info.ie = mgmt->u.disassoc.variable;
1827 event.disassoc_info.ie_len = frame + len -
1828 mgmt->u.disassoc.variable;
1829 }
1830 } else {
Dmitry Shmidt98660862014-03-11 17:26:21 -07001831 if (drv->ignore_deauth_event) {
1832 wpa_printf(MSG_DEBUG, "nl80211: Ignore deauth event due to previous forced deauth-during-auth");
1833 drv->ignore_deauth_event = 0;
1834 return;
1835 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001836 event.deauth_info.locally_generated =
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001837 !os_memcmp(mgmt->sa, drv->first_bss->addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001838 event.deauth_info.addr = bssid;
1839 event.deauth_info.reason_code = reason_code;
1840 if (frame + len > mgmt->u.deauth.variable) {
1841 event.deauth_info.ie = mgmt->u.deauth.variable;
1842 event.deauth_info.ie_len = frame + len -
1843 mgmt->u.deauth.variable;
1844 }
1845 }
1846
1847 wpa_supplicant_event(drv->ctx, type, &event);
1848}
1849
1850
1851static void mlme_event_unprot_disconnect(struct wpa_driver_nl80211_data *drv,
1852 enum wpa_event_type type,
1853 const u8 *frame, size_t len)
1854{
1855 const struct ieee80211_mgmt *mgmt;
1856 union wpa_event_data event;
1857 u16 reason_code = 0;
1858
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001859 if (type == EVENT_UNPROT_DEAUTH)
1860 wpa_printf(MSG_DEBUG, "nl80211: Unprot Deauthenticate event");
1861 else
1862 wpa_printf(MSG_DEBUG, "nl80211: Unprot Disassociate event");
1863
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001864 if (len < 24)
1865 return;
1866
1867 mgmt = (const struct ieee80211_mgmt *) frame;
1868
1869 os_memset(&event, 0, sizeof(event));
1870 /* Note: Same offset for Reason Code in both frame subtypes */
1871 if (len >= 24 + sizeof(mgmt->u.deauth))
1872 reason_code = le_to_host16(mgmt->u.deauth.reason_code);
1873
1874 if (type == EVENT_UNPROT_DISASSOC) {
1875 event.unprot_disassoc.sa = mgmt->sa;
1876 event.unprot_disassoc.da = mgmt->da;
1877 event.unprot_disassoc.reason_code = reason_code;
1878 } else {
1879 event.unprot_deauth.sa = mgmt->sa;
1880 event.unprot_deauth.da = mgmt->da;
1881 event.unprot_deauth.reason_code = reason_code;
1882 }
1883
1884 wpa_supplicant_event(drv->ctx, type, &event);
1885}
1886
1887
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001888static void mlme_event(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001889 enum nl80211_commands cmd, struct nlattr *frame,
1890 struct nlattr *addr, struct nlattr *timed_out,
1891 struct nlattr *freq, struct nlattr *ack,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001892 struct nlattr *cookie, struct nlattr *sig)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001893{
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001894 struct wpa_driver_nl80211_data *drv = bss->drv;
1895 const u8 *data;
1896 size_t len;
1897
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001898 if (timed_out && addr) {
1899 mlme_timeout_event(drv, cmd, addr);
1900 return;
1901 }
1902
1903 if (frame == NULL) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001904 wpa_printf(MSG_DEBUG,
1905 "nl80211: MLME event %d (%s) without frame data",
1906 cmd, nl80211_command_to_string(cmd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001907 return;
1908 }
1909
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001910 data = nla_data(frame);
1911 len = nla_len(frame);
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001912 if (len < 4 + 2 * ETH_ALEN) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001913 wpa_printf(MSG_MSGDUMP, "nl80211: MLME event %d (%s) on %s("
1914 MACSTR ") - too short",
1915 cmd, nl80211_command_to_string(cmd), bss->ifname,
1916 MAC2STR(bss->addr));
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001917 return;
1918 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001919 wpa_printf(MSG_MSGDUMP, "nl80211: MLME event %d (%s) on %s(" MACSTR
1920 ") A1=" MACSTR " A2=" MACSTR, cmd,
1921 nl80211_command_to_string(cmd), bss->ifname,
1922 MAC2STR(bss->addr), MAC2STR(data + 4),
1923 MAC2STR(data + 4 + ETH_ALEN));
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001924 if (cmd != NL80211_CMD_FRAME_TX_STATUS && !(data[4] & 0x01) &&
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001925 os_memcmp(bss->addr, data + 4, ETH_ALEN) != 0 &&
1926 os_memcmp(bss->addr, data + 4 + ETH_ALEN, ETH_ALEN) != 0) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001927 wpa_printf(MSG_MSGDUMP, "nl80211: %s: Ignore MLME frame event "
1928 "for foreign address", bss->ifname);
1929 return;
1930 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001931 wpa_hexdump(MSG_MSGDUMP, "nl80211: MLME event frame",
1932 nla_data(frame), nla_len(frame));
1933
1934 switch (cmd) {
1935 case NL80211_CMD_AUTHENTICATE:
1936 mlme_event_auth(drv, nla_data(frame), nla_len(frame));
1937 break;
1938 case NL80211_CMD_ASSOCIATE:
1939 mlme_event_assoc(drv, nla_data(frame), nla_len(frame));
1940 break;
1941 case NL80211_CMD_DEAUTHENTICATE:
1942 mlme_event_deauth_disassoc(drv, EVENT_DEAUTH,
1943 nla_data(frame), nla_len(frame));
1944 break;
1945 case NL80211_CMD_DISASSOCIATE:
1946 mlme_event_deauth_disassoc(drv, EVENT_DISASSOC,
1947 nla_data(frame), nla_len(frame));
1948 break;
1949 case NL80211_CMD_FRAME:
Dmitry Shmidt98660862014-03-11 17:26:21 -07001950 mlme_event_mgmt(bss, freq, sig, nla_data(frame),
Dmitry Shmidt04949592012-07-19 12:16:46 -07001951 nla_len(frame));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001952 break;
1953 case NL80211_CMD_FRAME_TX_STATUS:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001954 mlme_event_mgmt_tx_status(drv, cookie, nla_data(frame),
1955 nla_len(frame), ack);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001956 break;
1957 case NL80211_CMD_UNPROT_DEAUTHENTICATE:
1958 mlme_event_unprot_disconnect(drv, EVENT_UNPROT_DEAUTH,
1959 nla_data(frame), nla_len(frame));
1960 break;
1961 case NL80211_CMD_UNPROT_DISASSOCIATE:
1962 mlme_event_unprot_disconnect(drv, EVENT_UNPROT_DISASSOC,
1963 nla_data(frame), nla_len(frame));
1964 break;
1965 default:
1966 break;
1967 }
1968}
1969
1970
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001971static void mlme_event_michael_mic_failure(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001972 struct nlattr *tb[])
1973{
1974 union wpa_event_data data;
1975
1976 wpa_printf(MSG_DEBUG, "nl80211: MLME event Michael MIC failure");
1977 os_memset(&data, 0, sizeof(data));
1978 if (tb[NL80211_ATTR_MAC]) {
1979 wpa_hexdump(MSG_DEBUG, "nl80211: Source MAC address",
1980 nla_data(tb[NL80211_ATTR_MAC]),
1981 nla_len(tb[NL80211_ATTR_MAC]));
1982 data.michael_mic_failure.src = nla_data(tb[NL80211_ATTR_MAC]);
1983 }
1984 if (tb[NL80211_ATTR_KEY_SEQ]) {
1985 wpa_hexdump(MSG_DEBUG, "nl80211: TSC",
1986 nla_data(tb[NL80211_ATTR_KEY_SEQ]),
1987 nla_len(tb[NL80211_ATTR_KEY_SEQ]));
1988 }
1989 if (tb[NL80211_ATTR_KEY_TYPE]) {
1990 enum nl80211_key_type key_type =
1991 nla_get_u32(tb[NL80211_ATTR_KEY_TYPE]);
1992 wpa_printf(MSG_DEBUG, "nl80211: Key Type %d", key_type);
1993 if (key_type == NL80211_KEYTYPE_PAIRWISE)
1994 data.michael_mic_failure.unicast = 1;
1995 } else
1996 data.michael_mic_failure.unicast = 1;
1997
1998 if (tb[NL80211_ATTR_KEY_IDX]) {
1999 u8 key_id = nla_get_u8(tb[NL80211_ATTR_KEY_IDX]);
2000 wpa_printf(MSG_DEBUG, "nl80211: Key Id %d", key_id);
2001 }
2002
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002003 wpa_supplicant_event(bss->ctx, EVENT_MICHAEL_MIC_FAILURE, &data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002004}
2005
2006
2007static void mlme_event_join_ibss(struct wpa_driver_nl80211_data *drv,
2008 struct nlattr *tb[])
2009{
2010 if (tb[NL80211_ATTR_MAC] == NULL) {
2011 wpa_printf(MSG_DEBUG, "nl80211: No address in IBSS joined "
2012 "event");
2013 return;
2014 }
2015 os_memcpy(drv->bssid, nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002016
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002017 drv->associated = 1;
2018 wpa_printf(MSG_DEBUG, "nl80211: IBSS " MACSTR " joined",
2019 MAC2STR(drv->bssid));
2020
2021 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, NULL);
2022}
2023
2024
2025static void mlme_event_remain_on_channel(struct wpa_driver_nl80211_data *drv,
2026 int cancel_event, struct nlattr *tb[])
2027{
2028 unsigned int freq, chan_type, duration;
2029 union wpa_event_data data;
2030 u64 cookie;
2031
2032 if (tb[NL80211_ATTR_WIPHY_FREQ])
2033 freq = nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]);
2034 else
2035 freq = 0;
2036
2037 if (tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE])
2038 chan_type = nla_get_u32(tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
2039 else
2040 chan_type = 0;
2041
2042 if (tb[NL80211_ATTR_DURATION])
2043 duration = nla_get_u32(tb[NL80211_ATTR_DURATION]);
2044 else
2045 duration = 0;
2046
2047 if (tb[NL80211_ATTR_COOKIE])
2048 cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
2049 else
2050 cookie = 0;
2051
2052 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel event (cancel=%d "
2053 "freq=%u channel_type=%u duration=%u cookie=0x%llx (%s))",
2054 cancel_event, freq, chan_type, duration,
2055 (long long unsigned int) cookie,
2056 cookie == drv->remain_on_chan_cookie ? "match" : "unknown");
2057
2058 if (cookie != drv->remain_on_chan_cookie)
2059 return; /* not for us */
2060
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002061 if (cancel_event)
2062 drv->pending_remain_on_chan = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002063
2064 os_memset(&data, 0, sizeof(data));
2065 data.remain_on_channel.freq = freq;
2066 data.remain_on_channel.duration = duration;
2067 wpa_supplicant_event(drv->ctx, cancel_event ?
2068 EVENT_CANCEL_REMAIN_ON_CHANNEL :
2069 EVENT_REMAIN_ON_CHANNEL, &data);
2070}
2071
2072
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002073static void mlme_event_ft_event(struct wpa_driver_nl80211_data *drv,
2074 struct nlattr *tb[])
2075{
2076 union wpa_event_data data;
2077
2078 os_memset(&data, 0, sizeof(data));
2079
2080 if (tb[NL80211_ATTR_IE]) {
2081 data.ft_ies.ies = nla_data(tb[NL80211_ATTR_IE]);
2082 data.ft_ies.ies_len = nla_len(tb[NL80211_ATTR_IE]);
2083 }
2084
2085 if (tb[NL80211_ATTR_IE_RIC]) {
2086 data.ft_ies.ric_ies = nla_data(tb[NL80211_ATTR_IE_RIC]);
2087 data.ft_ies.ric_ies_len = nla_len(tb[NL80211_ATTR_IE_RIC]);
2088 }
2089
2090 if (tb[NL80211_ATTR_MAC])
2091 os_memcpy(data.ft_ies.target_ap,
2092 nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
2093
2094 wpa_printf(MSG_DEBUG, "nl80211: FT event target_ap " MACSTR,
2095 MAC2STR(data.ft_ies.target_ap));
2096
2097 wpa_supplicant_event(drv->ctx, EVENT_FT_RESPONSE, &data);
2098}
2099
2100
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002101static void send_scan_event(struct wpa_driver_nl80211_data *drv, int aborted,
2102 struct nlattr *tb[])
2103{
2104 union wpa_event_data event;
2105 struct nlattr *nl;
2106 int rem;
2107 struct scan_info *info;
2108#define MAX_REPORT_FREQS 50
2109 int freqs[MAX_REPORT_FREQS];
2110 int num_freqs = 0;
2111
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002112 if (drv->scan_for_auth) {
2113 drv->scan_for_auth = 0;
2114 wpa_printf(MSG_DEBUG, "nl80211: Scan results for missing "
2115 "cfg80211 BSS entry");
2116 wpa_driver_nl80211_authenticate_retry(drv);
2117 return;
2118 }
2119
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002120 os_memset(&event, 0, sizeof(event));
2121 info = &event.scan_info;
2122 info->aborted = aborted;
2123
2124 if (tb[NL80211_ATTR_SCAN_SSIDS]) {
2125 nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_SSIDS], rem) {
2126 struct wpa_driver_scan_ssid *s =
2127 &info->ssids[info->num_ssids];
2128 s->ssid = nla_data(nl);
2129 s->ssid_len = nla_len(nl);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002130 wpa_printf(MSG_DEBUG, "nl80211: Scan probed for SSID '%s'",
2131 wpa_ssid_txt(s->ssid, s->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002132 info->num_ssids++;
2133 if (info->num_ssids == WPAS_MAX_SCAN_SSIDS)
2134 break;
2135 }
2136 }
2137 if (tb[NL80211_ATTR_SCAN_FREQUENCIES]) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002138 char msg[200], *pos, *end;
2139 int res;
2140
2141 pos = msg;
2142 end = pos + sizeof(msg);
2143 *pos = '\0';
2144
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002145 nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_FREQUENCIES], rem)
2146 {
2147 freqs[num_freqs] = nla_get_u32(nl);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002148 res = os_snprintf(pos, end - pos, " %d",
2149 freqs[num_freqs]);
2150 if (res > 0 && end - pos > res)
2151 pos += res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002152 num_freqs++;
2153 if (num_freqs == MAX_REPORT_FREQS - 1)
2154 break;
2155 }
2156 info->freqs = freqs;
2157 info->num_freqs = num_freqs;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002158 wpa_printf(MSG_DEBUG, "nl80211: Scan included frequencies:%s",
2159 msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002160 }
2161 wpa_supplicant_event(drv->ctx, EVENT_SCAN_RESULTS, &event);
2162}
2163
2164
2165static int get_link_signal(struct nl_msg *msg, void *arg)
2166{
2167 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2168 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2169 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
2170 static struct nla_policy policy[NL80211_STA_INFO_MAX + 1] = {
2171 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002172 [NL80211_STA_INFO_SIGNAL_AVG] = { .type = NLA_U8 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002173 };
2174 struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
2175 static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
2176 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
2177 [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
2178 [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
2179 [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
2180 };
2181 struct wpa_signal_info *sig_change = arg;
2182
2183 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2184 genlmsg_attrlen(gnlh, 0), NULL);
2185 if (!tb[NL80211_ATTR_STA_INFO] ||
2186 nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
2187 tb[NL80211_ATTR_STA_INFO], policy))
2188 return NL_SKIP;
2189 if (!sinfo[NL80211_STA_INFO_SIGNAL])
2190 return NL_SKIP;
2191
2192 sig_change->current_signal =
2193 (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
2194
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002195 if (sinfo[NL80211_STA_INFO_SIGNAL_AVG])
2196 sig_change->avg_signal =
2197 (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL_AVG]);
2198 else
2199 sig_change->avg_signal = 0;
2200
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002201 if (sinfo[NL80211_STA_INFO_TX_BITRATE]) {
2202 if (nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
2203 sinfo[NL80211_STA_INFO_TX_BITRATE],
2204 rate_policy)) {
2205 sig_change->current_txrate = 0;
2206 } else {
2207 if (rinfo[NL80211_RATE_INFO_BITRATE]) {
2208 sig_change->current_txrate =
2209 nla_get_u16(rinfo[
2210 NL80211_RATE_INFO_BITRATE]) * 100;
2211 }
2212 }
2213 }
2214
2215 return NL_SKIP;
2216}
2217
2218
2219static int nl80211_get_link_signal(struct wpa_driver_nl80211_data *drv,
2220 struct wpa_signal_info *sig)
2221{
2222 struct nl_msg *msg;
2223
2224 sig->current_signal = -9999;
2225 sig->current_txrate = 0;
2226
2227 msg = nlmsg_alloc();
2228 if (!msg)
2229 return -ENOMEM;
2230
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002231 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002232
2233 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
2234 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid);
2235
2236 return send_and_recv_msgs(drv, msg, get_link_signal, sig);
2237 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002238 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002239 return -ENOBUFS;
2240}
2241
2242
2243static int get_link_noise(struct nl_msg *msg, void *arg)
2244{
2245 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2246 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2247 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
2248 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
2249 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
2250 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
2251 };
2252 struct wpa_signal_info *sig_change = arg;
2253
2254 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2255 genlmsg_attrlen(gnlh, 0), NULL);
2256
2257 if (!tb[NL80211_ATTR_SURVEY_INFO]) {
2258 wpa_printf(MSG_DEBUG, "nl80211: survey data missing!");
2259 return NL_SKIP;
2260 }
2261
2262 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
2263 tb[NL80211_ATTR_SURVEY_INFO],
2264 survey_policy)) {
2265 wpa_printf(MSG_DEBUG, "nl80211: failed to parse nested "
2266 "attributes!");
2267 return NL_SKIP;
2268 }
2269
2270 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
2271 return NL_SKIP;
2272
2273 if (nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
2274 sig_change->frequency)
2275 return NL_SKIP;
2276
2277 if (!sinfo[NL80211_SURVEY_INFO_NOISE])
2278 return NL_SKIP;
2279
2280 sig_change->current_noise =
2281 (s8) nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
2282
2283 return NL_SKIP;
2284}
2285
2286
2287static int nl80211_get_link_noise(struct wpa_driver_nl80211_data *drv,
2288 struct wpa_signal_info *sig_change)
2289{
2290 struct nl_msg *msg;
2291
2292 sig_change->current_noise = 9999;
2293 sig_change->frequency = drv->assoc_freq;
2294
2295 msg = nlmsg_alloc();
2296 if (!msg)
2297 return -ENOMEM;
2298
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002299 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002300
2301 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
2302
2303 return send_and_recv_msgs(drv, msg, get_link_noise, sig_change);
2304 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002305 nlmsg_free(msg);
2306 return -ENOBUFS;
2307}
2308
2309
2310static int get_noise_for_scan_results(struct nl_msg *msg, void *arg)
2311{
2312 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2313 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2314 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
2315 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
2316 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
2317 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
2318 };
2319 struct wpa_scan_results *scan_results = arg;
2320 struct wpa_scan_res *scan_res;
2321 size_t i;
2322
2323 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2324 genlmsg_attrlen(gnlh, 0), NULL);
2325
2326 if (!tb[NL80211_ATTR_SURVEY_INFO]) {
2327 wpa_printf(MSG_DEBUG, "nl80211: Survey data missing");
2328 return NL_SKIP;
2329 }
2330
2331 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
2332 tb[NL80211_ATTR_SURVEY_INFO],
2333 survey_policy)) {
2334 wpa_printf(MSG_DEBUG, "nl80211: Failed to parse nested "
2335 "attributes");
2336 return NL_SKIP;
2337 }
2338
2339 if (!sinfo[NL80211_SURVEY_INFO_NOISE])
2340 return NL_SKIP;
2341
2342 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
2343 return NL_SKIP;
2344
2345 for (i = 0; i < scan_results->num; ++i) {
2346 scan_res = scan_results->res[i];
2347 if (!scan_res)
2348 continue;
2349 if ((int) nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
2350 scan_res->freq)
2351 continue;
2352 if (!(scan_res->flags & WPA_SCAN_NOISE_INVALID))
2353 continue;
2354 scan_res->noise = (s8)
2355 nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
2356 scan_res->flags &= ~WPA_SCAN_NOISE_INVALID;
2357 }
2358
2359 return NL_SKIP;
2360}
2361
2362
2363static int nl80211_get_noise_for_scan_results(
2364 struct wpa_driver_nl80211_data *drv,
2365 struct wpa_scan_results *scan_res)
2366{
2367 struct nl_msg *msg;
2368
2369 msg = nlmsg_alloc();
2370 if (!msg)
2371 return -ENOMEM;
2372
2373 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
2374
2375 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
2376
2377 return send_and_recv_msgs(drv, msg, get_noise_for_scan_results,
2378 scan_res);
2379 nla_put_failure:
2380 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002381 return -ENOBUFS;
2382}
2383
2384
2385static void nl80211_cqm_event(struct wpa_driver_nl80211_data *drv,
2386 struct nlattr *tb[])
2387{
2388 static struct nla_policy cqm_policy[NL80211_ATTR_CQM_MAX + 1] = {
2389 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
2390 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U8 },
2391 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
2392 [NL80211_ATTR_CQM_PKT_LOSS_EVENT] = { .type = NLA_U32 },
2393 };
2394 struct nlattr *cqm[NL80211_ATTR_CQM_MAX + 1];
2395 enum nl80211_cqm_rssi_threshold_event event;
2396 union wpa_event_data ed;
2397 struct wpa_signal_info sig;
2398 int res;
2399
2400 if (tb[NL80211_ATTR_CQM] == NULL ||
2401 nla_parse_nested(cqm, NL80211_ATTR_CQM_MAX, tb[NL80211_ATTR_CQM],
2402 cqm_policy)) {
2403 wpa_printf(MSG_DEBUG, "nl80211: Ignore invalid CQM event");
2404 return;
2405 }
2406
2407 os_memset(&ed, 0, sizeof(ed));
2408
2409 if (cqm[NL80211_ATTR_CQM_PKT_LOSS_EVENT]) {
2410 if (!tb[NL80211_ATTR_MAC])
2411 return;
2412 os_memcpy(ed.low_ack.addr, nla_data(tb[NL80211_ATTR_MAC]),
2413 ETH_ALEN);
2414 wpa_supplicant_event(drv->ctx, EVENT_STATION_LOW_ACK, &ed);
2415 return;
2416 }
2417
2418 if (cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] == NULL)
2419 return;
2420 event = nla_get_u32(cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT]);
2421
2422 if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH) {
2423 wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor "
2424 "event: RSSI high");
2425 ed.signal_change.above_threshold = 1;
2426 } else if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW) {
2427 wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor "
2428 "event: RSSI low");
2429 ed.signal_change.above_threshold = 0;
2430 } else
2431 return;
2432
2433 res = nl80211_get_link_signal(drv, &sig);
2434 if (res == 0) {
2435 ed.signal_change.current_signal = sig.current_signal;
2436 ed.signal_change.current_txrate = sig.current_txrate;
2437 wpa_printf(MSG_DEBUG, "nl80211: Signal: %d dBm txrate: %d",
2438 sig.current_signal, sig.current_txrate);
2439 }
2440
2441 res = nl80211_get_link_noise(drv, &sig);
2442 if (res == 0) {
2443 ed.signal_change.current_noise = sig.current_noise;
2444 wpa_printf(MSG_DEBUG, "nl80211: Noise: %d dBm",
2445 sig.current_noise);
2446 }
2447
2448 wpa_supplicant_event(drv->ctx, EVENT_SIGNAL_CHANGE, &ed);
2449}
2450
2451
2452static void nl80211_new_station_event(struct wpa_driver_nl80211_data *drv,
2453 struct nlattr **tb)
2454{
2455 u8 *addr;
2456 union wpa_event_data data;
2457
2458 if (tb[NL80211_ATTR_MAC] == NULL)
2459 return;
2460 addr = nla_data(tb[NL80211_ATTR_MAC]);
2461 wpa_printf(MSG_DEBUG, "nl80211: New station " MACSTR, MAC2STR(addr));
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07002462
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002463 if (is_ap_interface(drv->nlmode) && drv->device_ap_sme) {
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07002464 u8 *ies = NULL;
2465 size_t ies_len = 0;
2466 if (tb[NL80211_ATTR_IE]) {
2467 ies = nla_data(tb[NL80211_ATTR_IE]);
2468 ies_len = nla_len(tb[NL80211_ATTR_IE]);
2469 }
2470 wpa_hexdump(MSG_DEBUG, "nl80211: Assoc Req IEs", ies, ies_len);
2471 drv_event_assoc(drv->ctx, addr, ies, ies_len, 0);
2472 return;
2473 }
2474
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002475 if (drv->nlmode != NL80211_IFTYPE_ADHOC)
2476 return;
2477
2478 os_memset(&data, 0, sizeof(data));
2479 os_memcpy(data.ibss_rsn_start.peer, addr, ETH_ALEN);
2480 wpa_supplicant_event(drv->ctx, EVENT_IBSS_RSN_START, &data);
2481}
2482
2483
2484static void nl80211_del_station_event(struct wpa_driver_nl80211_data *drv,
2485 struct nlattr **tb)
2486{
2487 u8 *addr;
2488 union wpa_event_data data;
2489
2490 if (tb[NL80211_ATTR_MAC] == NULL)
2491 return;
2492 addr = nla_data(tb[NL80211_ATTR_MAC]);
2493 wpa_printf(MSG_DEBUG, "nl80211: Delete station " MACSTR,
2494 MAC2STR(addr));
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07002495
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002496 if (is_ap_interface(drv->nlmode) && drv->device_ap_sme) {
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07002497 drv_event_disassoc(drv->ctx, addr);
2498 return;
2499 }
2500
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002501 if (drv->nlmode != NL80211_IFTYPE_ADHOC)
2502 return;
2503
2504 os_memset(&data, 0, sizeof(data));
2505 os_memcpy(data.ibss_peer_lost.peer, addr, ETH_ALEN);
2506 wpa_supplicant_event(drv->ctx, EVENT_IBSS_PEER_LOST, &data);
2507}
2508
2509
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002510static void nl80211_rekey_offload_event(struct wpa_driver_nl80211_data *drv,
2511 struct nlattr **tb)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002512{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002513 struct nlattr *rekey_info[NUM_NL80211_REKEY_DATA];
2514 static struct nla_policy rekey_policy[NUM_NL80211_REKEY_DATA] = {
2515 [NL80211_REKEY_DATA_KEK] = {
2516 .minlen = NL80211_KEK_LEN,
2517 .maxlen = NL80211_KEK_LEN,
2518 },
2519 [NL80211_REKEY_DATA_KCK] = {
2520 .minlen = NL80211_KCK_LEN,
2521 .maxlen = NL80211_KCK_LEN,
2522 },
2523 [NL80211_REKEY_DATA_REPLAY_CTR] = {
2524 .minlen = NL80211_REPLAY_CTR_LEN,
2525 .maxlen = NL80211_REPLAY_CTR_LEN,
2526 },
2527 };
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002528 union wpa_event_data data;
2529
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002530 if (!tb[NL80211_ATTR_MAC])
2531 return;
2532 if (!tb[NL80211_ATTR_REKEY_DATA])
2533 return;
2534 if (nla_parse_nested(rekey_info, MAX_NL80211_REKEY_DATA,
2535 tb[NL80211_ATTR_REKEY_DATA], rekey_policy))
2536 return;
2537 if (!rekey_info[NL80211_REKEY_DATA_REPLAY_CTR])
2538 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002539
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002540 os_memset(&data, 0, sizeof(data));
2541 data.driver_gtk_rekey.bssid = nla_data(tb[NL80211_ATTR_MAC]);
2542 wpa_printf(MSG_DEBUG, "nl80211: Rekey offload event for BSSID " MACSTR,
2543 MAC2STR(data.driver_gtk_rekey.bssid));
2544 data.driver_gtk_rekey.replay_ctr =
2545 nla_data(rekey_info[NL80211_REKEY_DATA_REPLAY_CTR]);
2546 wpa_hexdump(MSG_DEBUG, "nl80211: Rekey offload - Replay Counter",
2547 data.driver_gtk_rekey.replay_ctr, NL80211_REPLAY_CTR_LEN);
2548 wpa_supplicant_event(drv->ctx, EVENT_DRIVER_GTK_REKEY, &data);
2549}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002550
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002551
2552static void nl80211_pmksa_candidate_event(struct wpa_driver_nl80211_data *drv,
2553 struct nlattr **tb)
2554{
2555 struct nlattr *cand[NUM_NL80211_PMKSA_CANDIDATE];
2556 static struct nla_policy cand_policy[NUM_NL80211_PMKSA_CANDIDATE] = {
2557 [NL80211_PMKSA_CANDIDATE_INDEX] = { .type = NLA_U32 },
2558 [NL80211_PMKSA_CANDIDATE_BSSID] = {
2559 .minlen = ETH_ALEN,
2560 .maxlen = ETH_ALEN,
2561 },
2562 [NL80211_PMKSA_CANDIDATE_PREAUTH] = { .type = NLA_FLAG },
2563 };
2564 union wpa_event_data data;
2565
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002566 wpa_printf(MSG_DEBUG, "nl80211: PMKSA candidate event");
2567
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002568 if (!tb[NL80211_ATTR_PMKSA_CANDIDATE])
2569 return;
2570 if (nla_parse_nested(cand, MAX_NL80211_PMKSA_CANDIDATE,
2571 tb[NL80211_ATTR_PMKSA_CANDIDATE], cand_policy))
2572 return;
2573 if (!cand[NL80211_PMKSA_CANDIDATE_INDEX] ||
2574 !cand[NL80211_PMKSA_CANDIDATE_BSSID])
2575 return;
2576
2577 os_memset(&data, 0, sizeof(data));
2578 os_memcpy(data.pmkid_candidate.bssid,
2579 nla_data(cand[NL80211_PMKSA_CANDIDATE_BSSID]), ETH_ALEN);
2580 data.pmkid_candidate.index =
2581 nla_get_u32(cand[NL80211_PMKSA_CANDIDATE_INDEX]);
2582 data.pmkid_candidate.preauth =
2583 cand[NL80211_PMKSA_CANDIDATE_PREAUTH] != NULL;
2584 wpa_supplicant_event(drv->ctx, EVENT_PMKID_CANDIDATE, &data);
2585}
2586
2587
2588static void nl80211_client_probe_event(struct wpa_driver_nl80211_data *drv,
2589 struct nlattr **tb)
2590{
2591 union wpa_event_data data;
2592
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002593 wpa_printf(MSG_DEBUG, "nl80211: Probe client event");
2594
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002595 if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_ACK])
2596 return;
2597
2598 os_memset(&data, 0, sizeof(data));
2599 os_memcpy(data.client_poll.addr,
2600 nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
2601
2602 wpa_supplicant_event(drv->ctx, EVENT_DRIVER_CLIENT_POLL_OK, &data);
2603}
2604
2605
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002606static void nl80211_tdls_oper_event(struct wpa_driver_nl80211_data *drv,
2607 struct nlattr **tb)
2608{
2609 union wpa_event_data data;
2610
2611 wpa_printf(MSG_DEBUG, "nl80211: TDLS operation event");
2612
2613 if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_TDLS_OPERATION])
2614 return;
2615
2616 os_memset(&data, 0, sizeof(data));
2617 os_memcpy(data.tdls.peer, nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
2618 switch (nla_get_u8(tb[NL80211_ATTR_TDLS_OPERATION])) {
2619 case NL80211_TDLS_SETUP:
2620 wpa_printf(MSG_DEBUG, "nl80211: TDLS setup request for peer "
2621 MACSTR, MAC2STR(data.tdls.peer));
2622 data.tdls.oper = TDLS_REQUEST_SETUP;
2623 break;
2624 case NL80211_TDLS_TEARDOWN:
2625 wpa_printf(MSG_DEBUG, "nl80211: TDLS teardown request for peer "
2626 MACSTR, MAC2STR(data.tdls.peer));
2627 data.tdls.oper = TDLS_REQUEST_TEARDOWN;
2628 break;
2629 default:
2630 wpa_printf(MSG_DEBUG, "nl80211: Unsupported TDLS operatione "
2631 "event");
2632 return;
2633 }
2634 if (tb[NL80211_ATTR_REASON_CODE]) {
2635 data.tdls.reason_code =
2636 nla_get_u16(tb[NL80211_ATTR_REASON_CODE]);
2637 }
2638
2639 wpa_supplicant_event(drv->ctx, EVENT_TDLS, &data);
2640}
2641
2642
Dmitry Shmidt5393a0f2013-08-08 11:23:34 -07002643static void nl80211_stop_ap(struct wpa_driver_nl80211_data *drv,
2644 struct nlattr **tb)
2645{
2646 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_UNAVAILABLE, NULL);
2647}
2648
2649
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002650static void nl80211_connect_failed_event(struct wpa_driver_nl80211_data *drv,
2651 struct nlattr **tb)
2652{
2653 union wpa_event_data data;
2654 u32 reason;
2655
2656 wpa_printf(MSG_DEBUG, "nl80211: Connect failed event");
2657
2658 if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_CONN_FAILED_REASON])
2659 return;
2660
2661 os_memset(&data, 0, sizeof(data));
2662 os_memcpy(data.connect_failed_reason.addr,
2663 nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
2664
2665 reason = nla_get_u32(tb[NL80211_ATTR_CONN_FAILED_REASON]);
2666 switch (reason) {
2667 case NL80211_CONN_FAIL_MAX_CLIENTS:
2668 wpa_printf(MSG_DEBUG, "nl80211: Max client reached");
2669 data.connect_failed_reason.code = MAX_CLIENT_REACHED;
2670 break;
2671 case NL80211_CONN_FAIL_BLOCKED_CLIENT:
2672 wpa_printf(MSG_DEBUG, "nl80211: Blocked client " MACSTR
2673 " tried to connect",
2674 MAC2STR(data.connect_failed_reason.addr));
2675 data.connect_failed_reason.code = BLOCKED_CLIENT;
2676 break;
2677 default:
2678 wpa_printf(MSG_DEBUG, "nl8021l: Unknown connect failed reason "
2679 "%u", reason);
2680 return;
2681 }
2682
2683 wpa_supplicant_event(drv->ctx, EVENT_CONNECT_FAILED_REASON, &data);
2684}
2685
2686
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002687static void nl80211_radar_event(struct wpa_driver_nl80211_data *drv,
2688 struct nlattr **tb)
2689{
2690 union wpa_event_data data;
2691 enum nl80211_radar_event event_type;
2692
2693 if (!tb[NL80211_ATTR_WIPHY_FREQ] || !tb[NL80211_ATTR_RADAR_EVENT])
2694 return;
2695
2696 os_memset(&data, 0, sizeof(data));
Dmitry Shmidt051af732013-10-22 13:52:46 -07002697 data.dfs_event.freq = nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]);
2698 event_type = nla_get_u32(tb[NL80211_ATTR_RADAR_EVENT]);
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002699
Dmitry Shmidt051af732013-10-22 13:52:46 -07002700 /* Check HT params */
2701 if (tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
2702 data.dfs_event.ht_enabled = 1;
2703 data.dfs_event.chan_offset = 0;
2704
2705 switch (nla_get_u32(tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE])) {
2706 case NL80211_CHAN_NO_HT:
2707 data.dfs_event.ht_enabled = 0;
2708 break;
2709 case NL80211_CHAN_HT20:
2710 break;
2711 case NL80211_CHAN_HT40PLUS:
2712 data.dfs_event.chan_offset = 1;
2713 break;
2714 case NL80211_CHAN_HT40MINUS:
2715 data.dfs_event.chan_offset = -1;
2716 break;
2717 }
2718 }
2719
2720 /* Get VHT params */
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002721 if (tb[NL80211_ATTR_CHANNEL_WIDTH])
2722 data.dfs_event.chan_width =
2723 convert2width(nla_get_u32(
2724 tb[NL80211_ATTR_CHANNEL_WIDTH]));
2725 if (tb[NL80211_ATTR_CENTER_FREQ1])
2726 data.dfs_event.cf1 = nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002727 if (tb[NL80211_ATTR_CENTER_FREQ2])
2728 data.dfs_event.cf2 = nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]);
2729
2730 wpa_printf(MSG_DEBUG, "nl80211: DFS event on freq %d MHz, ht: %d, offset: %d, width: %d, cf1: %dMHz, cf2: %dMHz",
2731 data.dfs_event.freq, data.dfs_event.ht_enabled,
2732 data.dfs_event.chan_offset, data.dfs_event.chan_width,
2733 data.dfs_event.cf1, data.dfs_event.cf2);
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002734
2735 switch (event_type) {
2736 case NL80211_RADAR_DETECTED:
2737 wpa_supplicant_event(drv->ctx, EVENT_DFS_RADAR_DETECTED, &data);
2738 break;
2739 case NL80211_RADAR_CAC_FINISHED:
2740 wpa_supplicant_event(drv->ctx, EVENT_DFS_CAC_FINISHED, &data);
2741 break;
2742 case NL80211_RADAR_CAC_ABORTED:
2743 wpa_supplicant_event(drv->ctx, EVENT_DFS_CAC_ABORTED, &data);
2744 break;
2745 case NL80211_RADAR_NOP_FINISHED:
2746 wpa_supplicant_event(drv->ctx, EVENT_DFS_NOP_FINISHED, &data);
2747 break;
2748 default:
2749 wpa_printf(MSG_DEBUG, "nl80211: Unknown radar event %d "
2750 "received", event_type);
2751 break;
2752 }
2753}
2754
2755
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002756static void nl80211_spurious_frame(struct i802_bss *bss, struct nlattr **tb,
2757 int wds)
2758{
2759 struct wpa_driver_nl80211_data *drv = bss->drv;
2760 union wpa_event_data event;
2761
2762 if (!tb[NL80211_ATTR_MAC])
2763 return;
2764
2765 os_memset(&event, 0, sizeof(event));
2766 event.rx_from_unknown.bssid = bss->addr;
2767 event.rx_from_unknown.addr = nla_data(tb[NL80211_ATTR_MAC]);
2768 event.rx_from_unknown.wds = wds;
2769
2770 wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event);
2771}
2772
2773
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002774static void qca_nl80211_avoid_freq(struct wpa_driver_nl80211_data *drv,
2775 const u8 *data, size_t len)
2776{
2777 u32 i, count;
2778 union wpa_event_data event;
2779 struct wpa_freq_range *range = NULL;
2780 const struct qca_avoid_freq_list *freq_range;
2781
2782 freq_range = (const struct qca_avoid_freq_list *) data;
2783 if (len < sizeof(freq_range->count))
2784 return;
2785
2786 count = freq_range->count;
2787 if (len < sizeof(freq_range->count) +
2788 count * sizeof(struct qca_avoid_freq_range)) {
2789 wpa_printf(MSG_DEBUG, "nl80211: Ignored too short avoid frequency list (len=%u)",
2790 (unsigned int) len);
2791 return;
2792 }
2793
2794 if (count > 0) {
2795 range = os_calloc(count, sizeof(struct wpa_freq_range));
2796 if (range == NULL)
2797 return;
2798 }
2799
2800 os_memset(&event, 0, sizeof(event));
2801 for (i = 0; i < count; i++) {
2802 unsigned int idx = event.freq_range.num;
2803 range[idx].min = freq_range->range[i].start_freq;
2804 range[idx].max = freq_range->range[i].end_freq;
2805 wpa_printf(MSG_DEBUG, "nl80211: Avoid frequency range: %u-%u",
2806 range[idx].min, range[idx].max);
2807 if (range[idx].min > range[idx].max) {
2808 wpa_printf(MSG_DEBUG, "nl80211: Ignore invalid frequency range");
2809 continue;
2810 }
2811 event.freq_range.num++;
2812 }
2813 event.freq_range.range = range;
2814
2815 wpa_supplicant_event(drv->ctx, EVENT_AVOID_FREQUENCIES, &event);
2816
2817 os_free(range);
2818}
2819
2820
2821static void nl80211_vendor_event_qca(struct wpa_driver_nl80211_data *drv,
2822 u32 subcmd, u8 *data, size_t len)
2823{
2824 switch (subcmd) {
2825 case QCA_NL80211_VENDOR_SUBCMD_AVOID_FREQUENCY:
2826 qca_nl80211_avoid_freq(drv, data, len);
2827 break;
2828 default:
2829 wpa_printf(MSG_DEBUG,
2830 "nl80211: Ignore unsupported QCA vendor event %u",
2831 subcmd);
2832 break;
2833 }
2834}
2835
2836
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002837static void nl80211_vendor_event(struct wpa_driver_nl80211_data *drv,
2838 struct nlattr **tb)
2839{
2840 u32 vendor_id, subcmd, wiphy = 0;
2841 int wiphy_idx;
2842 u8 *data = NULL;
2843 size_t len = 0;
2844
2845 if (!tb[NL80211_ATTR_VENDOR_ID] ||
2846 !tb[NL80211_ATTR_VENDOR_SUBCMD])
2847 return;
2848
2849 vendor_id = nla_get_u32(tb[NL80211_ATTR_VENDOR_ID]);
2850 subcmd = nla_get_u32(tb[NL80211_ATTR_VENDOR_SUBCMD]);
2851
2852 if (tb[NL80211_ATTR_WIPHY])
2853 wiphy = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
2854
2855 wpa_printf(MSG_DEBUG, "nl80211: Vendor event: wiphy=%u vendor_id=0x%x subcmd=%u",
2856 wiphy, vendor_id, subcmd);
2857
2858 if (tb[NL80211_ATTR_VENDOR_DATA]) {
2859 data = nla_data(tb[NL80211_ATTR_VENDOR_DATA]);
2860 len = nla_len(tb[NL80211_ATTR_VENDOR_DATA]);
2861 wpa_hexdump(MSG_MSGDUMP, "nl80211: Vendor data", data, len);
2862 }
2863
2864 wiphy_idx = nl80211_get_wiphy_index(drv->first_bss);
2865 if (wiphy_idx >= 0 && wiphy_idx != (int) wiphy) {
2866 wpa_printf(MSG_DEBUG, "nl80211: Ignore vendor event for foreign wiphy %u (own: %d)",
2867 wiphy, wiphy_idx);
2868 return;
2869 }
2870
2871 switch (vendor_id) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002872 case OUI_QCA:
2873 nl80211_vendor_event_qca(drv, subcmd, data, len);
2874 break;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002875 default:
2876 wpa_printf(MSG_DEBUG, "nl80211: Ignore unsupported vendor event");
2877 break;
2878 }
2879}
2880
2881
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002882static void do_process_drv_event(struct i802_bss *bss, int cmd,
2883 struct nlattr **tb)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002884{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002885 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002886 union wpa_event_data data;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002887
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002888 wpa_printf(MSG_DEBUG, "nl80211: Drv Event %d (%s) received for %s",
2889 cmd, nl80211_command_to_string(cmd), bss->ifname);
2890
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002891 if (drv->ap_scan_as_station != NL80211_IFTYPE_UNSPECIFIED &&
2892 (cmd == NL80211_CMD_NEW_SCAN_RESULTS ||
2893 cmd == NL80211_CMD_SCAN_ABORTED)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002894 wpa_driver_nl80211_set_mode(drv->first_bss,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002895 drv->ap_scan_as_station);
2896 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002897 }
2898
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002899 switch (cmd) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002900 case NL80211_CMD_TRIGGER_SCAN:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002901 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Scan trigger");
Dmitry Shmidt56052862013-10-04 10:23:25 -07002902 drv->scan_state = SCAN_STARTED;
Dmitry Shmidt6cb1f652014-03-21 10:54:03 -07002903 if (drv->scan_for_auth) {
2904 /*
2905 * Cannot indicate EVENT_SCAN_STARTED here since we skip
2906 * EVENT_SCAN_RESULTS in scan_for_auth case and the
2907 * upper layer implementation could get confused about
2908 * scanning state.
2909 */
2910 wpa_printf(MSG_DEBUG, "nl80211: Do not indicate scan-start event due to internal scan_for_auth");
2911 break;
2912 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002913 wpa_supplicant_event(drv->ctx, EVENT_SCAN_STARTED, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002914 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002915 case NL80211_CMD_START_SCHED_SCAN:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002916 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Sched scan started");
Dmitry Shmidt56052862013-10-04 10:23:25 -07002917 drv->scan_state = SCHED_SCAN_STARTED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002918 break;
2919 case NL80211_CMD_SCHED_SCAN_STOPPED:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002920 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Sched scan stopped");
Dmitry Shmidt56052862013-10-04 10:23:25 -07002921 drv->scan_state = SCHED_SCAN_STOPPED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002922 wpa_supplicant_event(drv->ctx, EVENT_SCHED_SCAN_STOPPED, NULL);
2923 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002924 case NL80211_CMD_NEW_SCAN_RESULTS:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002925 wpa_dbg(drv->ctx, MSG_DEBUG,
2926 "nl80211: New scan results available");
Dmitry Shmidt56052862013-10-04 10:23:25 -07002927 drv->scan_state = SCAN_COMPLETED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002928 drv->scan_complete_events = 1;
2929 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
2930 drv->ctx);
2931 send_scan_event(drv, 0, tb);
2932 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002933 case NL80211_CMD_SCHED_SCAN_RESULTS:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002934 wpa_dbg(drv->ctx, MSG_DEBUG,
2935 "nl80211: New sched scan results available");
Dmitry Shmidt56052862013-10-04 10:23:25 -07002936 drv->scan_state = SCHED_SCAN_RESULTS;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002937 send_scan_event(drv, 0, tb);
2938 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002939 case NL80211_CMD_SCAN_ABORTED:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002940 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Scan aborted");
Dmitry Shmidt56052862013-10-04 10:23:25 -07002941 drv->scan_state = SCAN_ABORTED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002942 /*
2943 * Need to indicate that scan results are available in order
2944 * not to make wpa_supplicant stop its scanning.
2945 */
2946 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
2947 drv->ctx);
2948 send_scan_event(drv, 1, tb);
2949 break;
2950 case NL80211_CMD_AUTHENTICATE:
2951 case NL80211_CMD_ASSOCIATE:
2952 case NL80211_CMD_DEAUTHENTICATE:
2953 case NL80211_CMD_DISASSOCIATE:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002954 case NL80211_CMD_FRAME_TX_STATUS:
2955 case NL80211_CMD_UNPROT_DEAUTHENTICATE:
2956 case NL80211_CMD_UNPROT_DISASSOCIATE:
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002957 mlme_event(bss, cmd, tb[NL80211_ATTR_FRAME],
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002958 tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT],
2959 tb[NL80211_ATTR_WIPHY_FREQ], tb[NL80211_ATTR_ACK],
Dmitry Shmidt04949592012-07-19 12:16:46 -07002960 tb[NL80211_ATTR_COOKIE],
2961 tb[NL80211_ATTR_RX_SIGNAL_DBM]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002962 break;
2963 case NL80211_CMD_CONNECT:
2964 case NL80211_CMD_ROAM:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002965 mlme_event_connect(drv, cmd,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002966 tb[NL80211_ATTR_STATUS_CODE],
2967 tb[NL80211_ATTR_MAC],
2968 tb[NL80211_ATTR_REQ_IE],
2969 tb[NL80211_ATTR_RESP_IE]);
2970 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002971 case NL80211_CMD_CH_SWITCH_NOTIFY:
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002972 mlme_event_ch_switch(drv,
2973 tb[NL80211_ATTR_IFINDEX],
2974 tb[NL80211_ATTR_WIPHY_FREQ],
2975 tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE],
2976 tb[NL80211_ATTR_CHANNEL_WIDTH],
2977 tb[NL80211_ATTR_CENTER_FREQ1],
2978 tb[NL80211_ATTR_CENTER_FREQ2]);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002979 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002980 case NL80211_CMD_DISCONNECT:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002981 mlme_event_disconnect(drv, tb[NL80211_ATTR_REASON_CODE],
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002982 tb[NL80211_ATTR_MAC],
2983 tb[NL80211_ATTR_DISCONNECTED_BY_AP]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002984 break;
2985 case NL80211_CMD_MICHAEL_MIC_FAILURE:
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002986 mlme_event_michael_mic_failure(bss, tb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002987 break;
2988 case NL80211_CMD_JOIN_IBSS:
2989 mlme_event_join_ibss(drv, tb);
2990 break;
2991 case NL80211_CMD_REMAIN_ON_CHANNEL:
2992 mlme_event_remain_on_channel(drv, 0, tb);
2993 break;
2994 case NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL:
2995 mlme_event_remain_on_channel(drv, 1, tb);
2996 break;
2997 case NL80211_CMD_NOTIFY_CQM:
2998 nl80211_cqm_event(drv, tb);
2999 break;
3000 case NL80211_CMD_REG_CHANGE:
3001 wpa_printf(MSG_DEBUG, "nl80211: Regulatory domain change");
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003002 if (tb[NL80211_ATTR_REG_INITIATOR] == NULL)
3003 break;
3004 os_memset(&data, 0, sizeof(data));
3005 switch (nla_get_u8(tb[NL80211_ATTR_REG_INITIATOR])) {
3006 case NL80211_REGDOM_SET_BY_CORE:
3007 data.channel_list_changed.initiator =
3008 REGDOM_SET_BY_CORE;
3009 break;
3010 case NL80211_REGDOM_SET_BY_USER:
3011 data.channel_list_changed.initiator =
3012 REGDOM_SET_BY_USER;
3013 break;
3014 case NL80211_REGDOM_SET_BY_DRIVER:
3015 data.channel_list_changed.initiator =
3016 REGDOM_SET_BY_DRIVER;
3017 break;
3018 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
3019 data.channel_list_changed.initiator =
3020 REGDOM_SET_BY_COUNTRY_IE;
3021 break;
3022 default:
3023 wpa_printf(MSG_DEBUG, "nl80211: Unknown reg change initiator %d received",
3024 nla_get_u8(tb[NL80211_ATTR_REG_INITIATOR]));
3025 break;
3026 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003027 wpa_supplicant_event(drv->ctx, EVENT_CHANNEL_LIST_CHANGED,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003028 &data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003029 break;
3030 case NL80211_CMD_REG_BEACON_HINT:
3031 wpa_printf(MSG_DEBUG, "nl80211: Regulatory beacon hint");
Dmitry Shmidt97672262014-02-03 13:02:54 -08003032 os_memset(&data, 0, sizeof(data));
3033 data.channel_list_changed.initiator = REGDOM_BEACON_HINT;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003034 wpa_supplicant_event(drv->ctx, EVENT_CHANNEL_LIST_CHANGED,
Dmitry Shmidt97672262014-02-03 13:02:54 -08003035 &data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003036 break;
3037 case NL80211_CMD_NEW_STATION:
3038 nl80211_new_station_event(drv, tb);
3039 break;
3040 case NL80211_CMD_DEL_STATION:
3041 nl80211_del_station_event(drv, tb);
3042 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003043 case NL80211_CMD_SET_REKEY_OFFLOAD:
3044 nl80211_rekey_offload_event(drv, tb);
3045 break;
3046 case NL80211_CMD_PMKSA_CANDIDATE:
3047 nl80211_pmksa_candidate_event(drv, tb);
3048 break;
3049 case NL80211_CMD_PROBE_CLIENT:
3050 nl80211_client_probe_event(drv, tb);
3051 break;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003052 case NL80211_CMD_TDLS_OPER:
3053 nl80211_tdls_oper_event(drv, tb);
3054 break;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003055 case NL80211_CMD_CONN_FAILED:
3056 nl80211_connect_failed_event(drv, tb);
3057 break;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003058 case NL80211_CMD_FT_EVENT:
3059 mlme_event_ft_event(drv, tb);
3060 break;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07003061 case NL80211_CMD_RADAR_DETECT:
3062 nl80211_radar_event(drv, tb);
3063 break;
Dmitry Shmidt5393a0f2013-08-08 11:23:34 -07003064 case NL80211_CMD_STOP_AP:
3065 nl80211_stop_ap(drv, tb);
3066 break;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003067 case NL80211_CMD_VENDOR:
3068 nl80211_vendor_event(drv, tb);
3069 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003070 default:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003071 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Ignored unknown event "
3072 "(cmd=%d)", cmd);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003073 break;
3074 }
3075}
3076
3077
3078static int process_drv_event(struct nl_msg *msg, void *arg)
3079{
3080 struct wpa_driver_nl80211_data *drv = arg;
3081 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3082 struct nlattr *tb[NL80211_ATTR_MAX + 1];
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003083 struct i802_bss *bss;
3084 int ifidx = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003085
3086 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3087 genlmsg_attrlen(gnlh, 0), NULL);
3088
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003089 if (tb[NL80211_ATTR_IFINDEX]) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003090 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
3091
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003092 for (bss = drv->first_bss; bss; bss = bss->next)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003093 if (ifidx == -1 || ifidx == bss->ifindex) {
3094 do_process_drv_event(bss, gnlh->cmd, tb);
3095 return NL_SKIP;
3096 }
3097 wpa_printf(MSG_DEBUG,
3098 "nl80211: Ignored event (cmd=%d) for foreign interface (ifindex %d)",
3099 gnlh->cmd, ifidx);
3100 } else if (tb[NL80211_ATTR_WDEV]) {
3101 u64 wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
3102 wpa_printf(MSG_DEBUG, "nl80211: Process event on P2P device");
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003103 for (bss = drv->first_bss; bss; bss = bss->next) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003104 if (bss->wdev_id_set && wdev_id == bss->wdev_id) {
3105 do_process_drv_event(bss, gnlh->cmd, tb);
3106 return NL_SKIP;
3107 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003108 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003109 wpa_printf(MSG_DEBUG,
3110 "nl80211: Ignored event (cmd=%d) for foreign interface (wdev 0x%llx)",
3111 gnlh->cmd, (long long unsigned int) wdev_id);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003112 }
3113
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003114 return NL_SKIP;
3115}
3116
3117
3118static int process_global_event(struct nl_msg *msg, void *arg)
3119{
3120 struct nl80211_global *global = arg;
3121 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3122 struct nlattr *tb[NL80211_ATTR_MAX + 1];
Dmitry Shmidt04949592012-07-19 12:16:46 -07003123 struct wpa_driver_nl80211_data *drv, *tmp;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003124 int ifidx = -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003125 struct i802_bss *bss;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003126 u64 wdev_id = 0;
3127 int wdev_id_set = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003128
3129 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3130 genlmsg_attrlen(gnlh, 0), NULL);
3131
3132 if (tb[NL80211_ATTR_IFINDEX])
3133 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003134 else if (tb[NL80211_ATTR_WDEV]) {
3135 wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
3136 wdev_id_set = 1;
3137 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003138
Dmitry Shmidt04949592012-07-19 12:16:46 -07003139 dl_list_for_each_safe(drv, tmp, &global->interfaces,
3140 struct wpa_driver_nl80211_data, list) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003141 for (bss = drv->first_bss; bss; bss = bss->next) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003142 if ((ifidx == -1 && !wdev_id_set) ||
3143 ifidx == bss->ifindex ||
3144 (wdev_id_set && bss->wdev_id_set &&
3145 wdev_id == bss->wdev_id)) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003146 do_process_drv_event(bss, gnlh->cmd, tb);
3147 return NL_SKIP;
3148 }
3149 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003150 }
3151
3152 return NL_SKIP;
3153}
3154
3155
3156static int process_bss_event(struct nl_msg *msg, void *arg)
3157{
3158 struct i802_bss *bss = arg;
3159 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3160 struct nlattr *tb[NL80211_ATTR_MAX + 1];
3161
3162 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3163 genlmsg_attrlen(gnlh, 0), NULL);
3164
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003165 wpa_printf(MSG_DEBUG, "nl80211: BSS Event %d (%s) received for %s",
3166 gnlh->cmd, nl80211_command_to_string(gnlh->cmd),
3167 bss->ifname);
3168
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003169 switch (gnlh->cmd) {
3170 case NL80211_CMD_FRAME:
3171 case NL80211_CMD_FRAME_TX_STATUS:
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003172 mlme_event(bss, gnlh->cmd, tb[NL80211_ATTR_FRAME],
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003173 tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT],
3174 tb[NL80211_ATTR_WIPHY_FREQ], tb[NL80211_ATTR_ACK],
Dmitry Shmidt04949592012-07-19 12:16:46 -07003175 tb[NL80211_ATTR_COOKIE],
3176 tb[NL80211_ATTR_RX_SIGNAL_DBM]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003177 break;
3178 case NL80211_CMD_UNEXPECTED_FRAME:
3179 nl80211_spurious_frame(bss, tb, 0);
3180 break;
3181 case NL80211_CMD_UNEXPECTED_4ADDR_FRAME:
3182 nl80211_spurious_frame(bss, tb, 1);
3183 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003184 default:
3185 wpa_printf(MSG_DEBUG, "nl80211: Ignored unknown event "
3186 "(cmd=%d)", gnlh->cmd);
3187 break;
3188 }
3189
3190 return NL_SKIP;
3191}
3192
3193
3194static void wpa_driver_nl80211_event_receive(int sock, void *eloop_ctx,
3195 void *handle)
3196{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003197 struct nl_cb *cb = eloop_ctx;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003198 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003199
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003200 wpa_printf(MSG_MSGDUMP, "nl80211: Event message available");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003201
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003202 res = nl_recvmsgs(handle, cb);
3203 if (res) {
3204 wpa_printf(MSG_INFO, "nl80211: %s->nl_recvmsgs failed: %d",
3205 __func__, res);
3206 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003207}
3208
3209
3210/**
3211 * wpa_driver_nl80211_set_country - ask nl80211 to set the regulatory domain
3212 * @priv: driver_nl80211 private data
3213 * @alpha2_arg: country to which to switch to
3214 * Returns: 0 on success, -1 on failure
3215 *
3216 * This asks nl80211 to set the regulatory domain for given
3217 * country ISO / IEC alpha2.
3218 */
3219static int wpa_driver_nl80211_set_country(void *priv, const char *alpha2_arg)
3220{
3221 struct i802_bss *bss = priv;
3222 struct wpa_driver_nl80211_data *drv = bss->drv;
3223 char alpha2[3];
3224 struct nl_msg *msg;
3225
3226 msg = nlmsg_alloc();
3227 if (!msg)
3228 return -ENOMEM;
3229
3230 alpha2[0] = alpha2_arg[0];
3231 alpha2[1] = alpha2_arg[1];
3232 alpha2[2] = '\0';
3233
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003234 nl80211_cmd(drv, msg, 0, NL80211_CMD_REQ_SET_REG);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003235
3236 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, alpha2);
3237 if (send_and_recv_msgs(drv, msg, NULL, NULL))
3238 return -EINVAL;
3239 return 0;
3240nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003241 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003242 return -EINVAL;
3243}
3244
3245
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003246static int nl80211_get_country(struct nl_msg *msg, void *arg)
3247{
3248 char *alpha2 = arg;
3249 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
3250 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3251
3252 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3253 genlmsg_attrlen(gnlh, 0), NULL);
3254 if (!tb_msg[NL80211_ATTR_REG_ALPHA2]) {
3255 wpa_printf(MSG_DEBUG, "nl80211: No country information available");
3256 return NL_SKIP;
3257 }
3258 os_strlcpy(alpha2, nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]), 3);
3259 return NL_SKIP;
3260}
3261
3262
3263static int wpa_driver_nl80211_get_country(void *priv, char *alpha2)
3264{
3265 struct i802_bss *bss = priv;
3266 struct wpa_driver_nl80211_data *drv = bss->drv;
3267 struct nl_msg *msg;
3268 int ret;
3269
3270 msg = nlmsg_alloc();
3271 if (!msg)
3272 return -ENOMEM;
3273
3274 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG);
3275 alpha2[0] = '\0';
3276 ret = send_and_recv_msgs(drv, msg, nl80211_get_country, alpha2);
3277 if (!alpha2[0])
3278 ret = -1;
3279
3280 return ret;
3281}
3282
3283
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003284static int protocol_feature_handler(struct nl_msg *msg, void *arg)
3285{
3286 u32 *feat = arg;
3287 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
3288 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3289
3290 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3291 genlmsg_attrlen(gnlh, 0), NULL);
3292
3293 if (tb_msg[NL80211_ATTR_PROTOCOL_FEATURES])
3294 *feat = nla_get_u32(tb_msg[NL80211_ATTR_PROTOCOL_FEATURES]);
3295
3296 return NL_SKIP;
3297}
3298
3299
3300static u32 get_nl80211_protocol_features(struct wpa_driver_nl80211_data *drv)
3301{
3302 u32 feat = 0;
3303 struct nl_msg *msg;
3304
3305 msg = nlmsg_alloc();
3306 if (!msg)
3307 goto nla_put_failure;
3308
3309 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_PROTOCOL_FEATURES);
3310 if (send_and_recv_msgs(drv, msg, protocol_feature_handler, &feat) == 0)
3311 return feat;
3312
3313 msg = NULL;
3314nla_put_failure:
3315 nlmsg_free(msg);
3316 return 0;
3317}
3318
3319
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003320struct wiphy_info_data {
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003321 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003322 struct wpa_driver_capa *capa;
3323
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003324 unsigned int num_multichan_concurrent;
3325
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003326 unsigned int error:1;
3327 unsigned int device_ap_sme:1;
3328 unsigned int poll_command_supported:1;
3329 unsigned int data_tx_status:1;
3330 unsigned int monitor_supported:1;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003331 unsigned int auth_supported:1;
3332 unsigned int connect_supported:1;
3333 unsigned int p2p_go_supported:1;
3334 unsigned int p2p_client_supported:1;
3335 unsigned int p2p_concurrent:1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003336 unsigned int channel_switch_supported:1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003337 unsigned int set_qos_map_supported:1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003338};
3339
3340
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003341static unsigned int probe_resp_offload_support(int supp_protocols)
3342{
3343 unsigned int prot = 0;
3344
3345 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS)
3346 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS;
3347 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2)
3348 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS2;
3349 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P)
3350 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_P2P;
3351 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_80211U)
3352 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_INTERWORKING;
3353
3354 return prot;
3355}
3356
3357
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003358static void wiphy_info_supported_iftypes(struct wiphy_info_data *info,
3359 struct nlattr *tb)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003360{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003361 struct nlattr *nl_mode;
3362 int i;
3363
3364 if (tb == NULL)
3365 return;
3366
3367 nla_for_each_nested(nl_mode, tb, i) {
3368 switch (nla_type(nl_mode)) {
3369 case NL80211_IFTYPE_AP:
3370 info->capa->flags |= WPA_DRIVER_FLAGS_AP;
3371 break;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003372 case NL80211_IFTYPE_ADHOC:
3373 info->capa->flags |= WPA_DRIVER_FLAGS_IBSS;
3374 break;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003375 case NL80211_IFTYPE_P2P_DEVICE:
3376 info->capa->flags |=
3377 WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE;
3378 break;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003379 case NL80211_IFTYPE_P2P_GO:
3380 info->p2p_go_supported = 1;
3381 break;
3382 case NL80211_IFTYPE_P2P_CLIENT:
3383 info->p2p_client_supported = 1;
3384 break;
3385 case NL80211_IFTYPE_MONITOR:
3386 info->monitor_supported = 1;
3387 break;
3388 }
3389 }
3390}
3391
3392
3393static int wiphy_info_iface_comb_process(struct wiphy_info_data *info,
3394 struct nlattr *nl_combi)
3395{
3396 struct nlattr *tb_comb[NUM_NL80211_IFACE_COMB];
3397 struct nlattr *tb_limit[NUM_NL80211_IFACE_LIMIT];
3398 struct nlattr *nl_limit, *nl_mode;
3399 int err, rem_limit, rem_mode;
3400 int combination_has_p2p = 0, combination_has_mgd = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003401 static struct nla_policy
3402 iface_combination_policy[NUM_NL80211_IFACE_COMB] = {
3403 [NL80211_IFACE_COMB_LIMITS] = { .type = NLA_NESTED },
3404 [NL80211_IFACE_COMB_MAXNUM] = { .type = NLA_U32 },
3405 [NL80211_IFACE_COMB_STA_AP_BI_MATCH] = { .type = NLA_FLAG },
3406 [NL80211_IFACE_COMB_NUM_CHANNELS] = { .type = NLA_U32 },
Dmitry Shmidtea69e842013-05-13 14:52:28 -07003407 [NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS] = { .type = NLA_U32 },
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003408 },
3409 iface_limit_policy[NUM_NL80211_IFACE_LIMIT] = {
3410 [NL80211_IFACE_LIMIT_TYPES] = { .type = NLA_NESTED },
3411 [NL80211_IFACE_LIMIT_MAX] = { .type = NLA_U32 },
3412 };
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003413
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003414 err = nla_parse_nested(tb_comb, MAX_NL80211_IFACE_COMB,
3415 nl_combi, iface_combination_policy);
3416 if (err || !tb_comb[NL80211_IFACE_COMB_LIMITS] ||
3417 !tb_comb[NL80211_IFACE_COMB_MAXNUM] ||
3418 !tb_comb[NL80211_IFACE_COMB_NUM_CHANNELS])
3419 return 0; /* broken combination */
3420
Dmitry Shmidtea69e842013-05-13 14:52:28 -07003421 if (tb_comb[NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS])
3422 info->capa->flags |= WPA_DRIVER_FLAGS_RADAR;
3423
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003424 nla_for_each_nested(nl_limit, tb_comb[NL80211_IFACE_COMB_LIMITS],
3425 rem_limit) {
3426 err = nla_parse_nested(tb_limit, MAX_NL80211_IFACE_LIMIT,
3427 nl_limit, iface_limit_policy);
3428 if (err || !tb_limit[NL80211_IFACE_LIMIT_TYPES])
3429 return 0; /* broken combination */
3430
3431 nla_for_each_nested(nl_mode,
3432 tb_limit[NL80211_IFACE_LIMIT_TYPES],
3433 rem_mode) {
3434 int ift = nla_type(nl_mode);
3435 if (ift == NL80211_IFTYPE_P2P_GO ||
3436 ift == NL80211_IFTYPE_P2P_CLIENT)
3437 combination_has_p2p = 1;
3438 if (ift == NL80211_IFTYPE_STATION)
3439 combination_has_mgd = 1;
3440 }
3441 if (combination_has_p2p && combination_has_mgd)
3442 break;
3443 }
3444
3445 if (combination_has_p2p && combination_has_mgd) {
3446 info->p2p_concurrent = 1;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003447 info->num_multichan_concurrent =
3448 nla_get_u32(tb_comb[NL80211_IFACE_COMB_NUM_CHANNELS]);
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003449 return 1;
3450 }
3451
3452 return 0;
3453}
3454
3455
3456static void wiphy_info_iface_comb(struct wiphy_info_data *info,
3457 struct nlattr *tb)
3458{
3459 struct nlattr *nl_combi;
3460 int rem_combi;
3461
3462 if (tb == NULL)
3463 return;
3464
3465 nla_for_each_nested(nl_combi, tb, rem_combi) {
3466 if (wiphy_info_iface_comb_process(info, nl_combi) > 0)
3467 break;
3468 }
3469}
3470
3471
3472static void wiphy_info_supp_cmds(struct wiphy_info_data *info,
3473 struct nlattr *tb)
3474{
3475 struct nlattr *nl_cmd;
3476 int i;
3477
3478 if (tb == NULL)
3479 return;
3480
3481 nla_for_each_nested(nl_cmd, tb, i) {
3482 switch (nla_get_u32(nl_cmd)) {
3483 case NL80211_CMD_AUTHENTICATE:
3484 info->auth_supported = 1;
3485 break;
3486 case NL80211_CMD_CONNECT:
3487 info->connect_supported = 1;
3488 break;
3489 case NL80211_CMD_START_SCHED_SCAN:
3490 info->capa->sched_scan_supported = 1;
3491 break;
3492 case NL80211_CMD_PROBE_CLIENT:
3493 info->poll_command_supported = 1;
3494 break;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003495 case NL80211_CMD_CHANNEL_SWITCH:
3496 info->channel_switch_supported = 1;
3497 break;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003498 case NL80211_CMD_SET_QOS_MAP:
3499 info->set_qos_map_supported = 1;
3500 break;
3501 }
3502 }
3503}
3504
3505
3506static void wiphy_info_cipher_suites(struct wiphy_info_data *info,
3507 struct nlattr *tb)
3508{
3509 int i, num;
3510 u32 *ciphers;
3511
3512 if (tb == NULL)
3513 return;
3514
3515 num = nla_len(tb) / sizeof(u32);
3516 ciphers = nla_data(tb);
3517 for (i = 0; i < num; i++) {
3518 u32 c = ciphers[i];
3519
3520 wpa_printf(MSG_DEBUG, "nl80211: Supported cipher %02x-%02x-%02x:%d",
3521 c >> 24, (c >> 16) & 0xff,
3522 (c >> 8) & 0xff, c & 0xff);
3523 switch (c) {
3524 case WLAN_CIPHER_SUITE_CCMP_256:
3525 info->capa->enc |= WPA_DRIVER_CAPA_ENC_CCMP_256;
3526 break;
3527 case WLAN_CIPHER_SUITE_GCMP_256:
3528 info->capa->enc |= WPA_DRIVER_CAPA_ENC_GCMP_256;
3529 break;
3530 case WLAN_CIPHER_SUITE_CCMP:
3531 info->capa->enc |= WPA_DRIVER_CAPA_ENC_CCMP;
3532 break;
3533 case WLAN_CIPHER_SUITE_GCMP:
3534 info->capa->enc |= WPA_DRIVER_CAPA_ENC_GCMP;
3535 break;
3536 case WLAN_CIPHER_SUITE_TKIP:
3537 info->capa->enc |= WPA_DRIVER_CAPA_ENC_TKIP;
3538 break;
3539 case WLAN_CIPHER_SUITE_WEP104:
3540 info->capa->enc |= WPA_DRIVER_CAPA_ENC_WEP104;
3541 break;
3542 case WLAN_CIPHER_SUITE_WEP40:
3543 info->capa->enc |= WPA_DRIVER_CAPA_ENC_WEP40;
3544 break;
3545 case WLAN_CIPHER_SUITE_AES_CMAC:
3546 info->capa->enc |= WPA_DRIVER_CAPA_ENC_BIP;
3547 break;
3548 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
3549 info->capa->enc |= WPA_DRIVER_CAPA_ENC_BIP_GMAC_128;
3550 break;
3551 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
3552 info->capa->enc |= WPA_DRIVER_CAPA_ENC_BIP_GMAC_256;
3553 break;
3554 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
3555 info->capa->enc |= WPA_DRIVER_CAPA_ENC_BIP_CMAC_256;
3556 break;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003557 case WLAN_CIPHER_SUITE_NO_GROUP_ADDR:
3558 info->capa->enc |= WPA_DRIVER_CAPA_ENC_GTK_NOT_USED;
3559 break;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003560 }
3561 }
3562}
3563
3564
3565static void wiphy_info_max_roc(struct wpa_driver_capa *capa,
3566 struct nlattr *tb)
3567{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003568 if (tb)
3569 capa->max_remain_on_chan = nla_get_u32(tb);
3570}
3571
3572
3573static void wiphy_info_tdls(struct wpa_driver_capa *capa, struct nlattr *tdls,
3574 struct nlattr *ext_setup)
3575{
3576 if (tdls == NULL)
3577 return;
3578
3579 wpa_printf(MSG_DEBUG, "nl80211: TDLS supported");
3580 capa->flags |= WPA_DRIVER_FLAGS_TDLS_SUPPORT;
3581
3582 if (ext_setup) {
3583 wpa_printf(MSG_DEBUG, "nl80211: TDLS external setup");
3584 capa->flags |= WPA_DRIVER_FLAGS_TDLS_EXTERNAL_SETUP;
3585 }
3586}
3587
3588
3589static void wiphy_info_feature_flags(struct wiphy_info_data *info,
3590 struct nlattr *tb)
3591{
3592 u32 flags;
3593 struct wpa_driver_capa *capa = info->capa;
3594
3595 if (tb == NULL)
3596 return;
3597
3598 flags = nla_get_u32(tb);
3599
3600 if (flags & NL80211_FEATURE_SK_TX_STATUS)
3601 info->data_tx_status = 1;
3602
3603 if (flags & NL80211_FEATURE_INACTIVITY_TIMER)
3604 capa->flags |= WPA_DRIVER_FLAGS_INACTIVITY_TIMER;
3605
3606 if (flags & NL80211_FEATURE_SAE)
3607 capa->flags |= WPA_DRIVER_FLAGS_SAE;
3608
3609 if (flags & NL80211_FEATURE_NEED_OBSS_SCAN)
3610 capa->flags |= WPA_DRIVER_FLAGS_OBSS_SCAN;
3611}
3612
3613
3614static void wiphy_info_probe_resp_offload(struct wpa_driver_capa *capa,
3615 struct nlattr *tb)
3616{
3617 u32 protocols;
3618
3619 if (tb == NULL)
3620 return;
3621
3622 protocols = nla_get_u32(tb);
3623 wpa_printf(MSG_DEBUG, "nl80211: Supports Probe Response offload in AP "
3624 "mode");
3625 capa->flags |= WPA_DRIVER_FLAGS_PROBE_RESP_OFFLOAD;
3626 capa->probe_resp_offloads = probe_resp_offload_support(protocols);
3627}
3628
3629
3630static int wiphy_info_handler(struct nl_msg *msg, void *arg)
3631{
3632 struct nlattr *tb[NL80211_ATTR_MAX + 1];
3633 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3634 struct wiphy_info_data *info = arg;
3635 struct wpa_driver_capa *capa = info->capa;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003636 struct wpa_driver_nl80211_data *drv = info->drv;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003637
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003638 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3639 genlmsg_attrlen(gnlh, 0), NULL);
3640
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003641 if (tb[NL80211_ATTR_WIPHY_NAME])
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003642 os_strlcpy(drv->phyname,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003643 nla_get_string(tb[NL80211_ATTR_WIPHY_NAME]),
3644 sizeof(drv->phyname));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003645 if (tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS])
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003646 capa->max_scan_ssids =
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003647 nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS]);
3648
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003649 if (tb[NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS])
3650 capa->max_sched_scan_ssids =
3651 nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS]);
3652
3653 if (tb[NL80211_ATTR_MAX_MATCH_SETS])
3654 capa->max_match_sets =
3655 nla_get_u8(tb[NL80211_ATTR_MAX_MATCH_SETS]);
3656
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003657 if (tb[NL80211_ATTR_MAC_ACL_MAX])
3658 capa->max_acl_mac_addrs =
3659 nla_get_u8(tb[NL80211_ATTR_MAC_ACL_MAX]);
3660
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003661 wiphy_info_supported_iftypes(info, tb[NL80211_ATTR_SUPPORTED_IFTYPES]);
3662 wiphy_info_iface_comb(info, tb[NL80211_ATTR_INTERFACE_COMBINATIONS]);
3663 wiphy_info_supp_cmds(info, tb[NL80211_ATTR_SUPPORTED_COMMANDS]);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003664 wiphy_info_cipher_suites(info, tb[NL80211_ATTR_CIPHER_SUITES]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003665
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003666 if (tb[NL80211_ATTR_OFFCHANNEL_TX_OK]) {
3667 wpa_printf(MSG_DEBUG, "nl80211: Using driver-based "
3668 "off-channel TX");
3669 capa->flags |= WPA_DRIVER_FLAGS_OFFCHANNEL_TX;
3670 }
3671
3672 if (tb[NL80211_ATTR_ROAM_SUPPORT]) {
3673 wpa_printf(MSG_DEBUG, "nl80211: Using driver-based roaming");
3674 capa->flags |= WPA_DRIVER_FLAGS_BSS_SELECTION;
3675 }
3676
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003677 wiphy_info_max_roc(capa,
3678 tb[NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003679
3680 if (tb[NL80211_ATTR_SUPPORT_AP_UAPSD])
3681 capa->flags |= WPA_DRIVER_FLAGS_AP_UAPSD;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003682
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003683 wiphy_info_tdls(capa, tb[NL80211_ATTR_TDLS_SUPPORT],
3684 tb[NL80211_ATTR_TDLS_EXTERNAL_SETUP]);
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003685
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003686 if (tb[NL80211_ATTR_DEVICE_AP_SME])
3687 info->device_ap_sme = 1;
3688
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003689 wiphy_info_feature_flags(info, tb[NL80211_ATTR_FEATURE_FLAGS]);
3690 wiphy_info_probe_resp_offload(capa,
3691 tb[NL80211_ATTR_PROBE_RESP_OFFLOAD]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003692
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003693 if (tb[NL80211_ATTR_EXT_CAPA] && tb[NL80211_ATTR_EXT_CAPA_MASK] &&
3694 drv->extended_capa == NULL) {
3695 drv->extended_capa =
3696 os_malloc(nla_len(tb[NL80211_ATTR_EXT_CAPA]));
3697 if (drv->extended_capa) {
3698 os_memcpy(drv->extended_capa,
3699 nla_data(tb[NL80211_ATTR_EXT_CAPA]),
3700 nla_len(tb[NL80211_ATTR_EXT_CAPA]));
3701 drv->extended_capa_len =
3702 nla_len(tb[NL80211_ATTR_EXT_CAPA]);
3703 }
3704 drv->extended_capa_mask =
3705 os_malloc(nla_len(tb[NL80211_ATTR_EXT_CAPA]));
3706 if (drv->extended_capa_mask) {
3707 os_memcpy(drv->extended_capa_mask,
3708 nla_data(tb[NL80211_ATTR_EXT_CAPA]),
3709 nla_len(tb[NL80211_ATTR_EXT_CAPA]));
3710 } else {
3711 os_free(drv->extended_capa);
3712 drv->extended_capa = NULL;
3713 drv->extended_capa_len = 0;
3714 }
3715 }
3716
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003717 if (tb[NL80211_ATTR_VENDOR_DATA]) {
3718 struct nlattr *nl;
3719 int rem;
3720
3721 nla_for_each_nested(nl, tb[NL80211_ATTR_VENDOR_DATA], rem) {
3722 struct nl80211_vendor_cmd_info *vinfo;
Dmitry Shmidt18463232014-01-24 12:29:41 -08003723 if (nla_len(nl) != sizeof(*vinfo)) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003724 wpa_printf(MSG_DEBUG, "nl80211: Unexpected vendor data info");
3725 continue;
3726 }
3727 vinfo = nla_data(nl);
3728 wpa_printf(MSG_DEBUG, "nl80211: Supported vendor command: vendor_id=0x%x subcmd=%u",
3729 vinfo->vendor_id, vinfo->subcmd);
3730 }
3731 }
3732
3733 if (tb[NL80211_ATTR_VENDOR_EVENTS]) {
3734 struct nlattr *nl;
3735 int rem;
3736
3737 nla_for_each_nested(nl, tb[NL80211_ATTR_VENDOR_EVENTS], rem) {
3738 struct nl80211_vendor_cmd_info *vinfo;
Dmitry Shmidt18463232014-01-24 12:29:41 -08003739 if (nla_len(nl) != sizeof(*vinfo)) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003740 wpa_printf(MSG_DEBUG, "nl80211: Unexpected vendor data info");
3741 continue;
3742 }
3743 vinfo = nla_data(nl);
3744 wpa_printf(MSG_DEBUG, "nl80211: Supported vendor event: vendor_id=0x%x subcmd=%u",
3745 vinfo->vendor_id, vinfo->subcmd);
3746 }
3747 }
3748
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003749 return NL_SKIP;
3750}
3751
3752
3753static int wpa_driver_nl80211_get_info(struct wpa_driver_nl80211_data *drv,
3754 struct wiphy_info_data *info)
3755{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003756 u32 feat;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003757 struct nl_msg *msg;
3758
3759 os_memset(info, 0, sizeof(*info));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003760 info->capa = &drv->capa;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003761 info->drv = drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003762
3763 msg = nlmsg_alloc();
3764 if (!msg)
3765 return -1;
3766
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003767 feat = get_nl80211_protocol_features(drv);
3768 if (feat & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP)
3769 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_WIPHY);
3770 else
3771 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003772
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003773 NLA_PUT_FLAG(msg, NL80211_ATTR_SPLIT_WIPHY_DUMP);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003774 if (nl80211_set_iface_id(msg, drv->first_bss) < 0)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003775 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003776
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003777 if (send_and_recv_msgs(drv, msg, wiphy_info_handler, info))
3778 return -1;
3779
3780 if (info->auth_supported)
3781 drv->capa.flags |= WPA_DRIVER_FLAGS_SME;
3782 else if (!info->connect_supported) {
3783 wpa_printf(MSG_INFO, "nl80211: Driver does not support "
3784 "authentication/association or connect commands");
3785 info->error = 1;
3786 }
3787
3788 if (info->p2p_go_supported && info->p2p_client_supported)
3789 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CAPABLE;
3790 if (info->p2p_concurrent) {
3791 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
3792 "interface (driver advertised support)");
3793 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
3794 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
3795 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003796 if (info->num_multichan_concurrent > 1) {
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003797 wpa_printf(MSG_DEBUG, "nl80211: Enable multi-channel "
3798 "concurrent (driver advertised support)");
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003799 drv->capa.num_multichan_concurrent =
3800 info->num_multichan_concurrent;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003801 }
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07003802
3803 /* default to 5000 since early versions of mac80211 don't set it */
3804 if (!drv->capa.max_remain_on_chan)
3805 drv->capa.max_remain_on_chan = 5000;
3806
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003807 if (info->channel_switch_supported)
3808 drv->capa.flags |= WPA_DRIVER_FLAGS_AP_CSA;
3809
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003810 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003811nla_put_failure:
3812 nlmsg_free(msg);
3813 return -1;
3814}
3815
3816
3817static int wpa_driver_nl80211_capa(struct wpa_driver_nl80211_data *drv)
3818{
3819 struct wiphy_info_data info;
3820 if (wpa_driver_nl80211_get_info(drv, &info))
3821 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003822
3823 if (info.error)
3824 return -1;
3825
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003826 drv->has_capability = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003827 drv->capa.key_mgmt = WPA_DRIVER_CAPA_KEY_MGMT_WPA |
3828 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
3829 WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
3830 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003831 drv->capa.auth = WPA_DRIVER_AUTH_OPEN |
3832 WPA_DRIVER_AUTH_SHARED |
3833 WPA_DRIVER_AUTH_LEAP;
3834
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003835 drv->capa.flags |= WPA_DRIVER_FLAGS_SANE_ERROR_CODES;
3836 drv->capa.flags |= WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003837 drv->capa.flags |= WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003838
Dmitry Shmidta38abf92014-03-06 13:38:44 -08003839 /*
3840 * As all cfg80211 drivers must support cases where the AP interface is
3841 * removed without the knowledge of wpa_supplicant/hostapd, e.g., in
3842 * case that the user space daemon has crashed, they must be able to
3843 * cleanup all stations and key entries in the AP tear down flow. Thus,
3844 * this flag can/should always be set for cfg80211 drivers.
3845 */
3846 drv->capa.flags |= WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT;
3847
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003848 if (!info.device_ap_sme) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07003849 drv->capa.flags |= WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003850
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003851 /*
3852 * No AP SME is currently assumed to also indicate no AP MLME
3853 * in the driver/firmware.
3854 */
3855 drv->capa.flags |= WPA_DRIVER_FLAGS_AP_MLME;
3856 }
3857
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003858 drv->device_ap_sme = info.device_ap_sme;
3859 drv->poll_command_supported = info.poll_command_supported;
3860 drv->data_tx_status = info.data_tx_status;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003861 if (info.set_qos_map_supported)
3862 drv->capa.flags |= WPA_DRIVER_FLAGS_QOS_MAPPING;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003863
3864 /*
Dmitry Shmidtaa532512012-09-24 10:35:31 -07003865 * If poll command and tx status are supported, mac80211 is new enough
3866 * to have everything we need to not need monitor interfaces.
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003867 */
Dmitry Shmidtaa532512012-09-24 10:35:31 -07003868 drv->use_monitor = !info.poll_command_supported || !info.data_tx_status;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003869
3870 if (drv->device_ap_sme && drv->use_monitor) {
3871 /*
3872 * Non-mac80211 drivers may not support monitor interface.
3873 * Make sure we do not get stuck with incorrect capability here
3874 * by explicitly testing this.
3875 */
3876 if (!info.monitor_supported) {
3877 wpa_printf(MSG_DEBUG, "nl80211: Disable use_monitor "
3878 "with device_ap_sme since no monitor mode "
3879 "support detected");
3880 drv->use_monitor = 0;
3881 }
3882 }
3883
3884 /*
3885 * If we aren't going to use monitor interfaces, but the
3886 * driver doesn't support data TX status, we won't get TX
3887 * status for EAPOL frames.
3888 */
3889 if (!drv->use_monitor && !info.data_tx_status)
3890 drv->capa.flags &= ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003891
3892 return 0;
3893}
3894
3895
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003896#ifdef ANDROID
3897static int android_genl_ctrl_resolve(struct nl_handle *handle,
3898 const char *name)
3899{
3900 /*
3901 * Android ICS has very minimal genl_ctrl_resolve() implementation, so
3902 * need to work around that.
3903 */
3904 struct nl_cache *cache = NULL;
3905 struct genl_family *nl80211 = NULL;
3906 int id = -1;
3907
3908 if (genl_ctrl_alloc_cache(handle, &cache) < 0) {
3909 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic "
3910 "netlink cache");
3911 goto fail;
3912 }
3913
3914 nl80211 = genl_ctrl_search_by_name(cache, name);
3915 if (nl80211 == NULL)
3916 goto fail;
3917
3918 id = genl_family_get_id(nl80211);
3919
3920fail:
3921 if (nl80211)
3922 genl_family_put(nl80211);
3923 if (cache)
3924 nl_cache_free(cache);
3925
3926 return id;
3927}
3928#define genl_ctrl_resolve android_genl_ctrl_resolve
3929#endif /* ANDROID */
3930
3931
3932static int wpa_driver_nl80211_init_nl_global(struct nl80211_global *global)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003933{
3934 int ret;
3935
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003936 global->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
3937 if (global->nl_cb == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003938 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
3939 "callbacks");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003940 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003941 }
3942
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003943 global->nl = nl_create_handle(global->nl_cb, "nl");
3944 if (global->nl == NULL)
3945 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003946
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003947 global->nl80211_id = genl_ctrl_resolve(global->nl, "nl80211");
3948 if (global->nl80211_id < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003949 wpa_printf(MSG_ERROR, "nl80211: 'nl80211' generic netlink not "
3950 "found");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003951 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003952 }
3953
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003954 global->nl_event = nl_create_handle(global->nl_cb, "event");
3955 if (global->nl_event == NULL)
3956 goto err;
3957
3958 ret = nl_get_multicast_id(global, "nl80211", "scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003959 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003960 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003961 if (ret < 0) {
3962 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
3963 "membership for scan events: %d (%s)",
3964 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003965 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003966 }
3967
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003968 ret = nl_get_multicast_id(global, "nl80211", "mlme");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003969 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003970 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003971 if (ret < 0) {
3972 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
3973 "membership for mlme events: %d (%s)",
3974 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003975 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003976 }
3977
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003978 ret = nl_get_multicast_id(global, "nl80211", "regulatory");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003979 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003980 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003981 if (ret < 0) {
3982 wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
3983 "membership for regulatory events: %d (%s)",
3984 ret, strerror(-ret));
3985 /* Continue without regulatory events */
3986 }
3987
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003988 ret = nl_get_multicast_id(global, "nl80211", "vendor");
3989 if (ret >= 0)
3990 ret = nl_socket_add_membership(global->nl_event, ret);
3991 if (ret < 0) {
3992 wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
3993 "membership for vendor events: %d (%s)",
3994 ret, strerror(-ret));
3995 /* Continue without vendor events */
3996 }
3997
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003998 nl_cb_set(global->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
3999 no_seq_check, NULL);
4000 nl_cb_set(global->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
4001 process_global_event, global);
4002
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004003 nl80211_register_eloop_read(&global->nl_event,
4004 wpa_driver_nl80211_event_receive,
4005 global->nl_cb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004006
4007 return 0;
4008
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004009err:
4010 nl_destroy_handles(&global->nl_event);
4011 nl_destroy_handles(&global->nl);
4012 nl_cb_put(global->nl_cb);
4013 global->nl_cb = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004014 return -1;
4015}
4016
4017
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004018static int wpa_driver_nl80211_init_nl(struct wpa_driver_nl80211_data *drv)
4019{
4020 drv->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
4021 if (!drv->nl_cb) {
4022 wpa_printf(MSG_ERROR, "nl80211: Failed to alloc cb struct");
4023 return -1;
4024 }
4025
4026 nl_cb_set(drv->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
4027 no_seq_check, NULL);
4028 nl_cb_set(drv->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
4029 process_drv_event, drv);
4030
4031 return 0;
4032}
4033
4034
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004035static void wpa_driver_nl80211_rfkill_blocked(void *ctx)
4036{
4037 wpa_printf(MSG_DEBUG, "nl80211: RFKILL blocked");
4038 /*
4039 * This may be for any interface; use ifdown event to disable
4040 * interface.
4041 */
4042}
4043
4044
4045static void wpa_driver_nl80211_rfkill_unblocked(void *ctx)
4046{
4047 struct wpa_driver_nl80211_data *drv = ctx;
4048 wpa_printf(MSG_DEBUG, "nl80211: RFKILL unblocked");
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004049 if (i802_set_iface_flags(drv->first_bss, 1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004050 wpa_printf(MSG_DEBUG, "nl80211: Could not set interface UP "
4051 "after rfkill unblock");
4052 return;
4053 }
4054 /* rtnetlink ifup handler will report interface as enabled */
4055}
4056
4057
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004058static void wpa_driver_nl80211_handle_eapol_tx_status(int sock,
4059 void *eloop_ctx,
4060 void *handle)
4061{
4062 struct wpa_driver_nl80211_data *drv = eloop_ctx;
4063 u8 data[2048];
4064 struct msghdr msg;
4065 struct iovec entry;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004066 u8 control[512];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004067 struct cmsghdr *cmsg;
4068 int res, found_ee = 0, found_wifi = 0, acked = 0;
4069 union wpa_event_data event;
4070
4071 memset(&msg, 0, sizeof(msg));
4072 msg.msg_iov = &entry;
4073 msg.msg_iovlen = 1;
4074 entry.iov_base = data;
4075 entry.iov_len = sizeof(data);
4076 msg.msg_control = &control;
4077 msg.msg_controllen = sizeof(control);
4078
4079 res = recvmsg(sock, &msg, MSG_ERRQUEUE);
4080 /* if error or not fitting 802.3 header, return */
4081 if (res < 14)
4082 return;
4083
4084 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg))
4085 {
4086 if (cmsg->cmsg_level == SOL_SOCKET &&
4087 cmsg->cmsg_type == SCM_WIFI_STATUS) {
4088 int *ack;
4089
4090 found_wifi = 1;
4091 ack = (void *)CMSG_DATA(cmsg);
4092 acked = *ack;
4093 }
4094
4095 if (cmsg->cmsg_level == SOL_PACKET &&
4096 cmsg->cmsg_type == PACKET_TX_TIMESTAMP) {
4097 struct sock_extended_err *err =
4098 (struct sock_extended_err *)CMSG_DATA(cmsg);
4099
4100 if (err->ee_origin == SO_EE_ORIGIN_TXSTATUS)
4101 found_ee = 1;
4102 }
4103 }
4104
4105 if (!found_ee || !found_wifi)
4106 return;
4107
4108 memset(&event, 0, sizeof(event));
4109 event.eapol_tx_status.dst = data;
4110 event.eapol_tx_status.data = data + 14;
4111 event.eapol_tx_status.data_len = res - 14;
4112 event.eapol_tx_status.ack = acked;
4113 wpa_supplicant_event(drv->ctx, EVENT_EAPOL_TX_STATUS, &event);
4114}
4115
4116
4117static int nl80211_init_bss(struct i802_bss *bss)
4118{
4119 bss->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
4120 if (!bss->nl_cb)
4121 return -1;
4122
4123 nl_cb_set(bss->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
4124 no_seq_check, NULL);
4125 nl_cb_set(bss->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
4126 process_bss_event, bss);
4127
4128 return 0;
4129}
4130
4131
4132static void nl80211_destroy_bss(struct i802_bss *bss)
4133{
4134 nl_cb_put(bss->nl_cb);
4135 bss->nl_cb = NULL;
4136}
4137
4138
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004139static void * wpa_driver_nl80211_drv_init(void *ctx, const char *ifname,
4140 void *global_priv, int hostapd,
4141 const u8 *set_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004142{
4143 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004144 struct rfkill_config *rcfg;
4145 struct i802_bss *bss;
4146
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004147 if (global_priv == NULL)
4148 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004149 drv = os_zalloc(sizeof(*drv));
4150 if (drv == NULL)
4151 return NULL;
4152 drv->global = global_priv;
4153 drv->ctx = ctx;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004154 drv->hostapd = !!hostapd;
4155 drv->eapol_sock = -1;
4156 drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int);
4157 drv->if_indices = drv->default_if_indices;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004158
4159 drv->first_bss = os_zalloc(sizeof(*drv->first_bss));
4160 if (!drv->first_bss) {
4161 os_free(drv);
4162 return NULL;
4163 }
4164 bss = drv->first_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004165 bss->drv = drv;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004166 bss->ctx = ctx;
4167
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004168 os_strlcpy(bss->ifname, ifname, sizeof(bss->ifname));
4169 drv->monitor_ifidx = -1;
4170 drv->monitor_sock = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004171 drv->eapol_tx_sock = -1;
4172 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004173
4174 if (wpa_driver_nl80211_init_nl(drv)) {
4175 os_free(drv);
4176 return NULL;
4177 }
4178
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004179 if (nl80211_init_bss(bss))
4180 goto failed;
4181
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004182 rcfg = os_zalloc(sizeof(*rcfg));
4183 if (rcfg == NULL)
4184 goto failed;
4185 rcfg->ctx = drv;
4186 os_strlcpy(rcfg->ifname, ifname, sizeof(rcfg->ifname));
4187 rcfg->blocked_cb = wpa_driver_nl80211_rfkill_blocked;
4188 rcfg->unblocked_cb = wpa_driver_nl80211_rfkill_unblocked;
4189 drv->rfkill = rfkill_init(rcfg);
4190 if (drv->rfkill == NULL) {
4191 wpa_printf(MSG_DEBUG, "nl80211: RFKILL status not available");
4192 os_free(rcfg);
4193 }
4194
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004195 if (linux_iface_up(drv->global->ioctl_sock, ifname) > 0)
4196 drv->start_iface_up = 1;
4197
4198 if (wpa_driver_nl80211_finish_drv_init(drv, set_addr, 1))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004199 goto failed;
4200
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004201 drv->eapol_tx_sock = socket(PF_PACKET, SOCK_DGRAM, 0);
4202 if (drv->eapol_tx_sock < 0)
4203 goto failed;
4204
4205 if (drv->data_tx_status) {
4206 int enabled = 1;
4207
4208 if (setsockopt(drv->eapol_tx_sock, SOL_SOCKET, SO_WIFI_STATUS,
4209 &enabled, sizeof(enabled)) < 0) {
4210 wpa_printf(MSG_DEBUG,
4211 "nl80211: wifi status sockopt failed\n");
4212 drv->data_tx_status = 0;
4213 if (!drv->use_monitor)
4214 drv->capa.flags &=
4215 ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
4216 } else {
4217 eloop_register_read_sock(drv->eapol_tx_sock,
4218 wpa_driver_nl80211_handle_eapol_tx_status,
4219 drv, NULL);
4220 }
4221 }
4222
4223 if (drv->global) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004224 dl_list_add(&drv->global->interfaces, &drv->list);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004225 drv->in_interface_list = 1;
4226 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004227
4228 return bss;
4229
4230failed:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004231 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004232 return NULL;
4233}
4234
4235
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004236/**
4237 * wpa_driver_nl80211_init - Initialize nl80211 driver interface
4238 * @ctx: context to be used when calling wpa_supplicant functions,
4239 * e.g., wpa_supplicant_event()
4240 * @ifname: interface name, e.g., wlan0
4241 * @global_priv: private driver global data from global_init()
4242 * Returns: Pointer to private data, %NULL on failure
4243 */
4244static void * wpa_driver_nl80211_init(void *ctx, const char *ifname,
4245 void *global_priv)
4246{
4247 return wpa_driver_nl80211_drv_init(ctx, ifname, global_priv, 0, NULL);
4248}
4249
4250
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004251static int nl80211_register_frame(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004252 struct nl_handle *nl_handle,
4253 u16 type, const u8 *match, size_t match_len)
4254{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004255 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004256 struct nl_msg *msg;
4257 int ret = -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004258 char buf[30];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004259
4260 msg = nlmsg_alloc();
4261 if (!msg)
4262 return -1;
4263
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004264 buf[0] = '\0';
4265 wpa_snprintf_hex(buf, sizeof(buf), match, match_len);
4266 wpa_printf(MSG_DEBUG, "nl80211: Register frame type=0x%x nl_handle=%p match=%s",
4267 type, nl_handle, buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004268
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004269 nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_ACTION);
4270
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004271 if (nl80211_set_iface_id(msg, bss) < 0)
4272 goto nla_put_failure;
4273
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004274 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE, type);
4275 NLA_PUT(msg, NL80211_ATTR_FRAME_MATCH, match_len, match);
4276
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004277 ret = send_and_recv(drv->global, nl_handle, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004278 msg = NULL;
4279 if (ret) {
4280 wpa_printf(MSG_DEBUG, "nl80211: Register frame command "
4281 "failed (type=%u): ret=%d (%s)",
4282 type, ret, strerror(-ret));
4283 wpa_hexdump(MSG_DEBUG, "nl80211: Register frame match",
4284 match, match_len);
4285 goto nla_put_failure;
4286 }
4287 ret = 0;
4288nla_put_failure:
4289 nlmsg_free(msg);
4290 return ret;
4291}
4292
4293
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004294static int nl80211_alloc_mgmt_handle(struct i802_bss *bss)
4295{
4296 struct wpa_driver_nl80211_data *drv = bss->drv;
4297
4298 if (bss->nl_mgmt) {
4299 wpa_printf(MSG_DEBUG, "nl80211: Mgmt reporting "
4300 "already on! (nl_mgmt=%p)", bss->nl_mgmt);
4301 return -1;
4302 }
4303
4304 bss->nl_mgmt = nl_create_handle(drv->nl_cb, "mgmt");
4305 if (bss->nl_mgmt == NULL)
4306 return -1;
4307
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004308 return 0;
4309}
4310
4311
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004312static void nl80211_mgmt_handle_register_eloop(struct i802_bss *bss)
4313{
4314 nl80211_register_eloop_read(&bss->nl_mgmt,
4315 wpa_driver_nl80211_event_receive,
4316 bss->nl_cb);
4317}
4318
4319
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004320static int nl80211_register_action_frame(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004321 const u8 *match, size_t match_len)
4322{
4323 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_ACTION << 4);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004324 return nl80211_register_frame(bss, bss->nl_mgmt,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004325 type, match, match_len);
4326}
4327
4328
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004329static int nl80211_mgmt_subscribe_non_ap(struct i802_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004330{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004331 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004332 int ret = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004333
4334 if (nl80211_alloc_mgmt_handle(bss))
4335 return -1;
4336 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with non-AP "
4337 "handle %p", bss->nl_mgmt);
4338
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004339 if (drv->nlmode == NL80211_IFTYPE_ADHOC) {
4340 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_AUTH << 4);
4341
4342 /* register for any AUTH message */
4343 nl80211_register_frame(bss, bss->nl_mgmt, type, NULL, 0);
4344 }
4345
Dmitry Shmidt051af732013-10-22 13:52:46 -07004346#ifdef CONFIG_INTERWORKING
4347 /* QoS Map Configure */
4348 if (nl80211_register_action_frame(bss, (u8 *) "\x01\x04", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004349 ret = -1;
Dmitry Shmidt051af732013-10-22 13:52:46 -07004350#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004351#if defined(CONFIG_P2P) || defined(CONFIG_INTERWORKING)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004352 /* GAS Initial Request */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004353 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0a", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004354 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004355 /* GAS Initial Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004356 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0b", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004357 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004358 /* GAS Comeback Request */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004359 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0c", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004360 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004361 /* GAS Comeback Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004362 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0d", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004363 ret = -1;
Dmitry Shmidt18463232014-01-24 12:29:41 -08004364 /* Protected GAS Initial Request */
4365 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0a", 2) < 0)
4366 ret = -1;
4367 /* Protected GAS Initial Response */
4368 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0b", 2) < 0)
4369 ret = -1;
4370 /* Protected GAS Comeback Request */
4371 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0c", 2) < 0)
4372 ret = -1;
4373 /* Protected GAS Comeback Response */
4374 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0d", 2) < 0)
4375 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004376#endif /* CONFIG_P2P || CONFIG_INTERWORKING */
4377#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004378 /* P2P Public Action */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004379 if (nl80211_register_action_frame(bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004380 (u8 *) "\x04\x09\x50\x6f\x9a\x09",
4381 6) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004382 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004383 /* P2P Action */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004384 if (nl80211_register_action_frame(bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004385 (u8 *) "\x7f\x50\x6f\x9a\x09",
4386 5) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004387 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004388#endif /* CONFIG_P2P */
4389#ifdef CONFIG_IEEE80211W
4390 /* SA Query Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004391 if (nl80211_register_action_frame(bss, (u8 *) "\x08\x01", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004392 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004393#endif /* CONFIG_IEEE80211W */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004394#ifdef CONFIG_TDLS
4395 if ((drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT)) {
4396 /* TDLS Discovery Response */
4397 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0e", 2) <
4398 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004399 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004400 }
4401#endif /* CONFIG_TDLS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004402
4403 /* FT Action frames */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004404 if (nl80211_register_action_frame(bss, (u8 *) "\x06", 1) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004405 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004406 else
4407 drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_FT |
4408 WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK;
4409
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004410 /* WNM - BSS Transition Management Request */
4411 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x07", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004412 ret = -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004413 /* WNM-Sleep Mode Response */
4414 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x11", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004415 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004416
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08004417#ifdef CONFIG_HS20
4418 /* WNM-Notification */
4419 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x1a", 2) < 0)
4420 return -1;
4421#endif /* CONFIG_HS20 */
4422
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004423 nl80211_mgmt_handle_register_eloop(bss);
4424
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004425 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004426}
4427
4428
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004429static int nl80211_register_spurious_class3(struct i802_bss *bss)
4430{
4431 struct wpa_driver_nl80211_data *drv = bss->drv;
4432 struct nl_msg *msg;
4433 int ret = -1;
4434
4435 msg = nlmsg_alloc();
4436 if (!msg)
4437 return -1;
4438
4439 nl80211_cmd(drv, msg, 0, NL80211_CMD_UNEXPECTED_FRAME);
4440
4441 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
4442
4443 ret = send_and_recv(drv->global, bss->nl_mgmt, msg, NULL, NULL);
4444 msg = NULL;
4445 if (ret) {
4446 wpa_printf(MSG_DEBUG, "nl80211: Register spurious class3 "
4447 "failed: ret=%d (%s)",
4448 ret, strerror(-ret));
4449 goto nla_put_failure;
4450 }
4451 ret = 0;
4452nla_put_failure:
4453 nlmsg_free(msg);
4454 return ret;
4455}
4456
4457
4458static int nl80211_mgmt_subscribe_ap(struct i802_bss *bss)
4459{
4460 static const int stypes[] = {
4461 WLAN_FC_STYPE_AUTH,
4462 WLAN_FC_STYPE_ASSOC_REQ,
4463 WLAN_FC_STYPE_REASSOC_REQ,
4464 WLAN_FC_STYPE_DISASSOC,
4465 WLAN_FC_STYPE_DEAUTH,
4466 WLAN_FC_STYPE_ACTION,
4467 WLAN_FC_STYPE_PROBE_REQ,
4468/* Beacon doesn't work as mac80211 doesn't currently allow
4469 * it, but it wouldn't really be the right thing anyway as
4470 * it isn't per interface ... maybe just dump the scan
4471 * results periodically for OLBC?
4472 */
4473// WLAN_FC_STYPE_BEACON,
4474 };
4475 unsigned int i;
4476
4477 if (nl80211_alloc_mgmt_handle(bss))
4478 return -1;
4479 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
4480 "handle %p", bss->nl_mgmt);
4481
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004482 for (i = 0; i < ARRAY_SIZE(stypes); i++) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004483 if (nl80211_register_frame(bss, bss->nl_mgmt,
4484 (WLAN_FC_TYPE_MGMT << 2) |
4485 (stypes[i] << 4),
4486 NULL, 0) < 0) {
4487 goto out_err;
4488 }
4489 }
4490
4491 if (nl80211_register_spurious_class3(bss))
4492 goto out_err;
4493
4494 if (nl80211_get_wiphy_data_ap(bss) == NULL)
4495 goto out_err;
4496
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004497 nl80211_mgmt_handle_register_eloop(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004498 return 0;
4499
4500out_err:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004501 nl_destroy_handles(&bss->nl_mgmt);
4502 return -1;
4503}
4504
4505
4506static int nl80211_mgmt_subscribe_ap_dev_sme(struct i802_bss *bss)
4507{
4508 if (nl80211_alloc_mgmt_handle(bss))
4509 return -1;
4510 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
4511 "handle %p (device SME)", bss->nl_mgmt);
4512
4513 if (nl80211_register_frame(bss, bss->nl_mgmt,
4514 (WLAN_FC_TYPE_MGMT << 2) |
4515 (WLAN_FC_STYPE_ACTION << 4),
4516 NULL, 0) < 0)
4517 goto out_err;
4518
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004519 nl80211_mgmt_handle_register_eloop(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004520 return 0;
4521
4522out_err:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004523 nl_destroy_handles(&bss->nl_mgmt);
4524 return -1;
4525}
4526
4527
4528static void nl80211_mgmt_unsubscribe(struct i802_bss *bss, const char *reason)
4529{
4530 if (bss->nl_mgmt == NULL)
4531 return;
4532 wpa_printf(MSG_DEBUG, "nl80211: Unsubscribe mgmt frames handle %p "
4533 "(%s)", bss->nl_mgmt, reason);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004534 nl80211_destroy_eloop_handle(&bss->nl_mgmt);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004535
4536 nl80211_put_wiphy_data_ap(bss);
4537}
4538
4539
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004540static void wpa_driver_nl80211_send_rfkill(void *eloop_ctx, void *timeout_ctx)
4541{
4542 wpa_supplicant_event(timeout_ctx, EVENT_INTERFACE_DISABLED, NULL);
4543}
4544
4545
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004546static void nl80211_del_p2pdev(struct i802_bss *bss)
4547{
4548 struct wpa_driver_nl80211_data *drv = bss->drv;
4549 struct nl_msg *msg;
4550 int ret;
4551
4552 msg = nlmsg_alloc();
4553 if (!msg)
4554 return;
4555
4556 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_INTERFACE);
4557 NLA_PUT_U64(msg, NL80211_ATTR_WDEV, bss->wdev_id);
4558
4559 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4560 msg = NULL;
4561
4562 wpa_printf(MSG_DEBUG, "nl80211: Delete P2P Device %s (0x%llx): %s",
4563 bss->ifname, (long long unsigned int) bss->wdev_id,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004564 strerror(-ret));
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004565
4566nla_put_failure:
4567 nlmsg_free(msg);
4568}
4569
4570
4571static int nl80211_set_p2pdev(struct i802_bss *bss, int start)
4572{
4573 struct wpa_driver_nl80211_data *drv = bss->drv;
4574 struct nl_msg *msg;
4575 int ret = -1;
4576
4577 msg = nlmsg_alloc();
4578 if (!msg)
4579 return -1;
4580
4581 if (start)
4582 nl80211_cmd(drv, msg, 0, NL80211_CMD_START_P2P_DEVICE);
4583 else
4584 nl80211_cmd(drv, msg, 0, NL80211_CMD_STOP_P2P_DEVICE);
4585
4586 NLA_PUT_U64(msg, NL80211_ATTR_WDEV, bss->wdev_id);
4587
4588 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4589 msg = NULL;
4590
4591 wpa_printf(MSG_DEBUG, "nl80211: %s P2P Device %s (0x%llx): %s",
4592 start ? "Start" : "Stop",
4593 bss->ifname, (long long unsigned int) bss->wdev_id,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004594 strerror(-ret));
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004595
4596nla_put_failure:
4597 nlmsg_free(msg);
4598 return ret;
4599}
4600
4601
4602static int i802_set_iface_flags(struct i802_bss *bss, int up)
4603{
4604 enum nl80211_iftype nlmode;
4605
4606 nlmode = nl80211_get_ifmode(bss);
4607 if (nlmode != NL80211_IFTYPE_P2P_DEVICE) {
4608 return linux_set_iface_flags(bss->drv->global->ioctl_sock,
4609 bss->ifname, up);
4610 }
4611
4612 /* P2P Device has start/stop which is equivalent */
4613 return nl80211_set_p2pdev(bss, up);
4614}
4615
4616
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004617static int
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004618wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv,
4619 const u8 *set_addr, int first)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004620{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004621 struct i802_bss *bss = drv->first_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004622 int send_rfkill_event = 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004623 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004624
4625 drv->ifindex = if_nametoindex(bss->ifname);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004626 bss->ifindex = drv->ifindex;
4627 bss->wdev_id = drv->global->if_add_wdevid;
4628 bss->wdev_id_set = drv->global->if_add_wdevid_set;
4629
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004630 bss->if_dynamic = drv->ifindex == drv->global->if_add_ifindex;
4631 bss->if_dynamic = bss->if_dynamic || drv->global->if_add_wdevid_set;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004632 drv->global->if_add_wdevid_set = 0;
4633
4634 if (wpa_driver_nl80211_capa(drv))
4635 return -1;
4636
4637 wpa_printf(MSG_DEBUG, "nl80211: interface %s in phy %s",
4638 bss->ifname, drv->phyname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004639
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004640 if (set_addr &&
4641 (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0) ||
4642 linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
4643 set_addr)))
4644 return -1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004645
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004646 if (first && nl80211_get_ifmode(bss) == NL80211_IFTYPE_AP)
4647 drv->start_mode_ap = 1;
4648
4649 if (drv->hostapd)
4650 nlmode = NL80211_IFTYPE_AP;
4651 else if (bss->if_dynamic)
4652 nlmode = nl80211_get_ifmode(bss);
4653 else
4654 nlmode = NL80211_IFTYPE_STATION;
4655
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004656 if (wpa_driver_nl80211_set_mode(bss, nlmode) < 0) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004657 wpa_printf(MSG_ERROR, "nl80211: Could not configure driver mode");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004658 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004659 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004660
Dmitry Shmidt98660862014-03-11 17:26:21 -07004661 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004662 nl80211_get_macaddr(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004663
Dmitry Shmidt98660862014-03-11 17:26:21 -07004664 if (!rfkill_is_blocked(drv->rfkill)) {
4665 int ret = i802_set_iface_flags(bss, 1);
4666 if (ret) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004667 wpa_printf(MSG_ERROR, "nl80211: Could not set "
4668 "interface '%s' UP", bss->ifname);
Dmitry Shmidt98660862014-03-11 17:26:21 -07004669 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004670 }
Dmitry Shmidt98660862014-03-11 17:26:21 -07004671 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
4672 return ret;
4673 } else {
4674 wpa_printf(MSG_DEBUG, "nl80211: Could not yet enable "
4675 "interface '%s' due to rfkill", bss->ifname);
4676 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
4677 return 0;
4678 drv->if_disabled = 1;
4679 send_rfkill_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004680 }
4681
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004682 if (!drv->hostapd)
4683 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
4684 1, IF_OPER_DORMANT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004685
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004686 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
4687 bss->addr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004688 return -1;
4689
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004690 if (send_rfkill_event) {
4691 eloop_register_timeout(0, 0, wpa_driver_nl80211_send_rfkill,
4692 drv, drv->ctx);
4693 }
4694
4695 return 0;
4696}
4697
4698
4699static int wpa_driver_nl80211_del_beacon(struct wpa_driver_nl80211_data *drv)
4700{
4701 struct nl_msg *msg;
4702
4703 msg = nlmsg_alloc();
4704 if (!msg)
4705 return -ENOMEM;
4706
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004707 wpa_printf(MSG_DEBUG, "nl80211: Remove beacon (ifindex=%d)",
4708 drv->ifindex);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004709 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_BEACON);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004710 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
4711
4712 return send_and_recv_msgs(drv, msg, NULL, NULL);
4713 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004714 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004715 return -ENOBUFS;
4716}
4717
4718
4719/**
4720 * wpa_driver_nl80211_deinit - Deinitialize nl80211 driver interface
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004721 * @bss: Pointer to private nl80211 data from wpa_driver_nl80211_init()
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004722 *
4723 * Shut down driver interface and processing of driver events. Free
4724 * private data buffer if one was allocated in wpa_driver_nl80211_init().
4725 */
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004726static void wpa_driver_nl80211_deinit(struct i802_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004727{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004728 struct wpa_driver_nl80211_data *drv = bss->drv;
4729
Dmitry Shmidt04949592012-07-19 12:16:46 -07004730 bss->in_deinit = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004731 if (drv->data_tx_status)
4732 eloop_unregister_read_sock(drv->eapol_tx_sock);
4733 if (drv->eapol_tx_sock >= 0)
4734 close(drv->eapol_tx_sock);
4735
4736 if (bss->nl_preq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004737 wpa_driver_nl80211_probe_req_report(bss, 0);
4738 if (bss->added_if_into_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004739 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
4740 bss->ifname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004741 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
4742 "interface %s from bridge %s: %s",
4743 bss->ifname, bss->brname, strerror(errno));
4744 }
4745 if (bss->added_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004746 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004747 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
4748 "bridge %s: %s",
4749 bss->brname, strerror(errno));
4750 }
4751
4752 nl80211_remove_monitor_interface(drv);
4753
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004754 if (is_ap_interface(drv->nlmode))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004755 wpa_driver_nl80211_del_beacon(drv);
4756
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004757 if (drv->eapol_sock >= 0) {
4758 eloop_unregister_read_sock(drv->eapol_sock);
4759 close(drv->eapol_sock);
4760 }
4761
4762 if (drv->if_indices != drv->default_if_indices)
4763 os_free(drv->if_indices);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004764
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004765 if (drv->disabled_11b_rates)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004766 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
4767
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004768 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, 0,
4769 IF_OPER_UP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004770 rfkill_deinit(drv->rfkill);
4771
4772 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
4773
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004774 if (!drv->start_iface_up)
4775 (void) i802_set_iface_flags(bss, 0);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004776 if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004777 if (!drv->hostapd || !drv->start_mode_ap)
4778 wpa_driver_nl80211_set_mode(bss,
4779 NL80211_IFTYPE_STATION);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004780 nl80211_mgmt_unsubscribe(bss, "deinit");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004781 } else {
4782 nl80211_mgmt_unsubscribe(bss, "deinit");
4783 nl80211_del_p2pdev(bss);
4784 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004785 nl_cb_put(drv->nl_cb);
4786
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004787 nl80211_destroy_bss(drv->first_bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004788
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004789 os_free(drv->filter_ssids);
4790
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004791 os_free(drv->auth_ie);
4792
4793 if (drv->in_interface_list)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004794 dl_list_del(&drv->list);
4795
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004796 os_free(drv->extended_capa);
4797 os_free(drv->extended_capa_mask);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004798 os_free(drv->first_bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004799 os_free(drv);
4800}
4801
4802
4803/**
4804 * wpa_driver_nl80211_scan_timeout - Scan timeout to report scan completion
4805 * @eloop_ctx: Driver private data
4806 * @timeout_ctx: ctx argument given to wpa_driver_nl80211_init()
4807 *
4808 * This function can be used as registered timeout when starting a scan to
4809 * generate a scan completed event if the driver does not report this.
4810 */
4811static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx, void *timeout_ctx)
4812{
4813 struct wpa_driver_nl80211_data *drv = eloop_ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004814 if (drv->ap_scan_as_station != NL80211_IFTYPE_UNSPECIFIED) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004815 wpa_driver_nl80211_set_mode(drv->first_bss,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004816 drv->ap_scan_as_station);
4817 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004818 }
4819 wpa_printf(MSG_DEBUG, "Scan timeout - try to get results");
4820 wpa_supplicant_event(timeout_ctx, EVENT_SCAN_RESULTS, NULL);
4821}
4822
4823
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004824static struct nl_msg *
4825nl80211_scan_common(struct wpa_driver_nl80211_data *drv, u8 cmd,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004826 struct wpa_driver_scan_params *params, u64 *wdev_id)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004827{
4828 struct nl_msg *msg;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004829 size_t i;
4830
4831 msg = nlmsg_alloc();
4832 if (!msg)
4833 return NULL;
4834
4835 nl80211_cmd(drv, msg, 0, cmd);
4836
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004837 if (!wdev_id)
4838 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
4839 else
4840 NLA_PUT_U64(msg, NL80211_ATTR_WDEV, *wdev_id);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004841
4842 if (params->num_ssids) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004843 struct nlattr *ssids;
4844
4845 ssids = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004846 if (ssids == NULL)
4847 goto fail;
4848 for (i = 0; i < params->num_ssids; i++) {
4849 wpa_hexdump_ascii(MSG_MSGDUMP, "nl80211: Scan SSID",
4850 params->ssids[i].ssid,
4851 params->ssids[i].ssid_len);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004852 if (nla_put(msg, i + 1, params->ssids[i].ssid_len,
4853 params->ssids[i].ssid) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004854 goto fail;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004855 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004856 nla_nest_end(msg, ssids);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004857 }
4858
4859 if (params->extra_ies) {
4860 wpa_hexdump(MSG_MSGDUMP, "nl80211: Scan extra IEs",
4861 params->extra_ies, params->extra_ies_len);
4862 if (nla_put(msg, NL80211_ATTR_IE, params->extra_ies_len,
4863 params->extra_ies) < 0)
4864 goto fail;
4865 }
4866
4867 if (params->freqs) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004868 struct nlattr *freqs;
4869 freqs = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004870 if (freqs == NULL)
4871 goto fail;
4872 for (i = 0; params->freqs[i]; i++) {
4873 wpa_printf(MSG_MSGDUMP, "nl80211: Scan frequency %u "
4874 "MHz", params->freqs[i]);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004875 if (nla_put_u32(msg, i + 1, params->freqs[i]) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004876 goto fail;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004877 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004878 nla_nest_end(msg, freqs);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004879 }
4880
4881 os_free(drv->filter_ssids);
4882 drv->filter_ssids = params->filter_ssids;
4883 params->filter_ssids = NULL;
4884 drv->num_filter_ssids = params->num_filter_ssids;
4885
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004886 if (params->only_new_results) {
4887 wpa_printf(MSG_DEBUG, "nl80211: Add NL80211_SCAN_FLAG_FLUSH");
4888 NLA_PUT_U32(msg, NL80211_ATTR_SCAN_FLAGS,
4889 NL80211_SCAN_FLAG_FLUSH);
4890 }
4891
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004892 return msg;
4893
4894fail:
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004895nla_put_failure:
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004896 nlmsg_free(msg);
4897 return NULL;
4898}
4899
4900
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004901/**
4902 * wpa_driver_nl80211_scan - Request the driver to initiate scan
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004903 * @bss: Pointer to private driver data from wpa_driver_nl80211_init()
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004904 * @params: Scan parameters
4905 * Returns: 0 on success, -1 on failure
4906 */
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004907static int wpa_driver_nl80211_scan(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004908 struct wpa_driver_scan_params *params)
4909{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004910 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004911 int ret = -1, timeout;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004912 struct nl_msg *msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004913
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004914 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: scan request");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004915 drv->scan_for_auth = 0;
4916
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004917 msg = nl80211_scan_common(drv, NL80211_CMD_TRIGGER_SCAN, params,
4918 bss->wdev_id_set ? &bss->wdev_id : NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004919 if (!msg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004920 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004921
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004922 if (params->p2p_probe) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004923 struct nlattr *rates;
4924
Dmitry Shmidt04949592012-07-19 12:16:46 -07004925 wpa_printf(MSG_DEBUG, "nl80211: P2P probe - mask SuppRates");
4926
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004927 rates = nla_nest_start(msg, NL80211_ATTR_SCAN_SUPP_RATES);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004928 if (rates == NULL)
4929 goto nla_put_failure;
4930
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004931 /*
4932 * Remove 2.4 GHz rates 1, 2, 5.5, 11 Mbps from supported rates
4933 * by masking out everything else apart from the OFDM rates 6,
4934 * 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS rates. All 5 GHz
4935 * rates are left enabled.
4936 */
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004937 NLA_PUT(msg, NL80211_BAND_2GHZ, 8,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004938 "\x0c\x12\x18\x24\x30\x48\x60\x6c");
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004939 nla_nest_end(msg, rates);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004940
4941 NLA_PUT_FLAG(msg, NL80211_ATTR_TX_NO_CCK_RATE);
4942 }
4943
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004944 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4945 msg = NULL;
4946 if (ret) {
4947 wpa_printf(MSG_DEBUG, "nl80211: Scan trigger failed: ret=%d "
4948 "(%s)", ret, strerror(-ret));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004949 if (drv->hostapd && is_ap_interface(drv->nlmode)) {
Dmitry Shmidted003d22014-02-06 10:09:12 -08004950 enum nl80211_iftype old_mode = drv->nlmode;
4951
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004952 /*
4953 * mac80211 does not allow scan requests in AP mode, so
4954 * try to do this in station mode.
4955 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004956 if (wpa_driver_nl80211_set_mode(
4957 bss, NL80211_IFTYPE_STATION))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004958 goto nla_put_failure;
4959
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004960 if (wpa_driver_nl80211_scan(bss, params)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004961 wpa_driver_nl80211_set_mode(bss, drv->nlmode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004962 goto nla_put_failure;
4963 }
4964
4965 /* Restore AP mode when processing scan results */
Dmitry Shmidted003d22014-02-06 10:09:12 -08004966 drv->ap_scan_as_station = old_mode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004967 ret = 0;
4968 } else
4969 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004970 }
4971
Dmitry Shmidt56052862013-10-04 10:23:25 -07004972 drv->scan_state = SCAN_REQUESTED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004973 /* Not all drivers generate "scan completed" wireless event, so try to
4974 * read results after a timeout. */
4975 timeout = 10;
4976 if (drv->scan_complete_events) {
4977 /*
4978 * The driver seems to deliver events to notify when scan is
4979 * complete, so use longer timeout to avoid race conditions
4980 * with scanning and following association request.
4981 */
4982 timeout = 30;
4983 }
4984 wpa_printf(MSG_DEBUG, "Scan requested (ret=%d) - scan timeout %d "
4985 "seconds", ret, timeout);
4986 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
4987 eloop_register_timeout(timeout, 0, wpa_driver_nl80211_scan_timeout,
4988 drv, drv->ctx);
4989
4990nla_put_failure:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004991 nlmsg_free(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004992 return ret;
4993}
4994
4995
4996/**
4997 * wpa_driver_nl80211_sched_scan - Initiate a scheduled scan
4998 * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
4999 * @params: Scan parameters
5000 * @interval: Interval between scan cycles in milliseconds
5001 * Returns: 0 on success, -1 on failure or if not supported
5002 */
5003static int wpa_driver_nl80211_sched_scan(void *priv,
5004 struct wpa_driver_scan_params *params,
5005 u32 interval)
5006{
5007 struct i802_bss *bss = priv;
5008 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005009 int ret = -1;
5010 struct nl_msg *msg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005011 size_t i;
5012
Dmitry Shmidt700a1372013-03-15 14:14:44 -07005013 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: sched_scan request");
5014
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005015#ifdef ANDROID
5016 if (!drv->capa.sched_scan_supported)
5017 return android_pno_start(bss, params);
5018#endif /* ANDROID */
5019
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005020 msg = nl80211_scan_common(drv, NL80211_CMD_START_SCHED_SCAN, params,
5021 bss->wdev_id_set ? &bss->wdev_id : NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005022 if (!msg)
5023 goto nla_put_failure;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005024
5025 NLA_PUT_U32(msg, NL80211_ATTR_SCHED_SCAN_INTERVAL, interval);
5026
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005027 if ((drv->num_filter_ssids &&
5028 (int) drv->num_filter_ssids <= drv->capa.max_match_sets) ||
5029 params->filter_rssi) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005030 struct nlattr *match_sets;
5031 match_sets = nla_nest_start(msg, NL80211_ATTR_SCHED_SCAN_MATCH);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005032 if (match_sets == NULL)
5033 goto nla_put_failure;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005034
5035 for (i = 0; i < drv->num_filter_ssids; i++) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005036 struct nlattr *match_set_ssid;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005037 wpa_hexdump_ascii(MSG_MSGDUMP,
5038 "nl80211: Sched scan filter SSID",
5039 drv->filter_ssids[i].ssid,
5040 drv->filter_ssids[i].ssid_len);
5041
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005042 match_set_ssid = nla_nest_start(msg, i + 1);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005043 if (match_set_ssid == NULL)
5044 goto nla_put_failure;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005045 NLA_PUT(msg, NL80211_ATTR_SCHED_SCAN_MATCH_SSID,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005046 drv->filter_ssids[i].ssid_len,
5047 drv->filter_ssids[i].ssid);
Dmitry Shmidt97672262014-02-03 13:02:54 -08005048 if (params->filter_rssi)
5049 NLA_PUT_U32(msg,
5050 NL80211_SCHED_SCAN_MATCH_ATTR_RSSI,
5051 params->filter_rssi);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005052
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005053 nla_nest_end(msg, match_set_ssid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005054 }
5055
Dmitry Shmidt97672262014-02-03 13:02:54 -08005056 /*
5057 * Due to backward compatibility code, newer kernels treat this
5058 * matchset (with only an RSSI filter) as the default for all
5059 * other matchsets, unless it's the only one, in which case the
5060 * matchset will actually allow all SSIDs above the RSSI.
5061 */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005062 if (params->filter_rssi) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005063 struct nlattr *match_set_rssi;
5064 match_set_rssi = nla_nest_start(msg, 0);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005065 if (match_set_rssi == NULL)
5066 goto nla_put_failure;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005067 NLA_PUT_U32(msg, NL80211_SCHED_SCAN_MATCH_ATTR_RSSI,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005068 params->filter_rssi);
5069 wpa_printf(MSG_MSGDUMP,
5070 "nl80211: Sched scan RSSI filter %d dBm",
5071 params->filter_rssi);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005072 nla_nest_end(msg, match_set_rssi);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005073 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005074
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005075 nla_nest_end(msg, match_sets);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005076 }
5077
5078 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5079
5080 /* TODO: if we get an error here, we should fall back to normal scan */
5081
5082 msg = NULL;
5083 if (ret) {
5084 wpa_printf(MSG_DEBUG, "nl80211: Sched scan start failed: "
5085 "ret=%d (%s)", ret, strerror(-ret));
5086 goto nla_put_failure;
5087 }
5088
5089 wpa_printf(MSG_DEBUG, "nl80211: Sched scan requested (ret=%d) - "
5090 "scan interval %d msec", ret, interval);
5091
5092nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005093 nlmsg_free(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005094 return ret;
5095}
5096
5097
5098/**
5099 * wpa_driver_nl80211_stop_sched_scan - Stop a scheduled scan
5100 * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
5101 * Returns: 0 on success, -1 on failure or if not supported
5102 */
5103static int wpa_driver_nl80211_stop_sched_scan(void *priv)
5104{
5105 struct i802_bss *bss = priv;
5106 struct wpa_driver_nl80211_data *drv = bss->drv;
5107 int ret = 0;
5108 struct nl_msg *msg;
5109
5110#ifdef ANDROID
5111 if (!drv->capa.sched_scan_supported)
5112 return android_pno_stop(bss);
5113#endif /* ANDROID */
5114
5115 msg = nlmsg_alloc();
5116 if (!msg)
5117 return -1;
5118
5119 nl80211_cmd(drv, msg, 0, NL80211_CMD_STOP_SCHED_SCAN);
5120
5121 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
5122
5123 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5124 msg = NULL;
5125 if (ret) {
5126 wpa_printf(MSG_DEBUG, "nl80211: Sched scan stop failed: "
5127 "ret=%d (%s)", ret, strerror(-ret));
5128 goto nla_put_failure;
5129 }
5130
5131 wpa_printf(MSG_DEBUG, "nl80211: Sched scan stop sent (ret=%d)", ret);
5132
5133nla_put_failure:
5134 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005135 return ret;
5136}
5137
5138
5139static const u8 * nl80211_get_ie(const u8 *ies, size_t ies_len, u8 ie)
5140{
5141 const u8 *end, *pos;
5142
5143 if (ies == NULL)
5144 return NULL;
5145
5146 pos = ies;
5147 end = ies + ies_len;
5148
5149 while (pos + 1 < end) {
5150 if (pos + 2 + pos[1] > end)
5151 break;
5152 if (pos[0] == ie)
5153 return pos;
5154 pos += 2 + pos[1];
5155 }
5156
5157 return NULL;
5158}
5159
5160
5161static int nl80211_scan_filtered(struct wpa_driver_nl80211_data *drv,
5162 const u8 *ie, size_t ie_len)
5163{
5164 const u8 *ssid;
5165 size_t i;
5166
5167 if (drv->filter_ssids == NULL)
5168 return 0;
5169
5170 ssid = nl80211_get_ie(ie, ie_len, WLAN_EID_SSID);
5171 if (ssid == NULL)
5172 return 1;
5173
5174 for (i = 0; i < drv->num_filter_ssids; i++) {
5175 if (ssid[1] == drv->filter_ssids[i].ssid_len &&
5176 os_memcmp(ssid + 2, drv->filter_ssids[i].ssid, ssid[1]) ==
5177 0)
5178 return 0;
5179 }
5180
5181 return 1;
5182}
5183
5184
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005185static int bss_info_handler(struct nl_msg *msg, void *arg)
5186{
5187 struct nlattr *tb[NL80211_ATTR_MAX + 1];
5188 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
5189 struct nlattr *bss[NL80211_BSS_MAX + 1];
5190 static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
5191 [NL80211_BSS_BSSID] = { .type = NLA_UNSPEC },
5192 [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
5193 [NL80211_BSS_TSF] = { .type = NLA_U64 },
5194 [NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 },
5195 [NL80211_BSS_CAPABILITY] = { .type = NLA_U16 },
5196 [NL80211_BSS_INFORMATION_ELEMENTS] = { .type = NLA_UNSPEC },
5197 [NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 },
5198 [NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 },
5199 [NL80211_BSS_STATUS] = { .type = NLA_U32 },
5200 [NL80211_BSS_SEEN_MS_AGO] = { .type = NLA_U32 },
5201 [NL80211_BSS_BEACON_IES] = { .type = NLA_UNSPEC },
5202 };
5203 struct nl80211_bss_info_arg *_arg = arg;
5204 struct wpa_scan_results *res = _arg->res;
5205 struct wpa_scan_res **tmp;
5206 struct wpa_scan_res *r;
5207 const u8 *ie, *beacon_ie;
5208 size_t ie_len, beacon_ie_len;
5209 u8 *pos;
Jouni Malinen87fd2792011-05-16 18:35:42 +03005210 size_t i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005211
5212 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
5213 genlmsg_attrlen(gnlh, 0), NULL);
5214 if (!tb[NL80211_ATTR_BSS])
5215 return NL_SKIP;
5216 if (nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS],
5217 bss_policy))
5218 return NL_SKIP;
Jouni Malinen87fd2792011-05-16 18:35:42 +03005219 if (bss[NL80211_BSS_STATUS]) {
5220 enum nl80211_bss_status status;
5221 status = nla_get_u32(bss[NL80211_BSS_STATUS]);
5222 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
5223 bss[NL80211_BSS_FREQUENCY]) {
5224 _arg->assoc_freq =
5225 nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
5226 wpa_printf(MSG_DEBUG, "nl80211: Associated on %u MHz",
5227 _arg->assoc_freq);
5228 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005229 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
5230 bss[NL80211_BSS_BSSID]) {
5231 os_memcpy(_arg->assoc_bssid,
5232 nla_data(bss[NL80211_BSS_BSSID]), ETH_ALEN);
5233 wpa_printf(MSG_DEBUG, "nl80211: Associated with "
5234 MACSTR, MAC2STR(_arg->assoc_bssid));
5235 }
Jouni Malinen87fd2792011-05-16 18:35:42 +03005236 }
5237 if (!res)
5238 return NL_SKIP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005239 if (bss[NL80211_BSS_INFORMATION_ELEMENTS]) {
5240 ie = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
5241 ie_len = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
5242 } else {
5243 ie = NULL;
5244 ie_len = 0;
5245 }
5246 if (bss[NL80211_BSS_BEACON_IES]) {
5247 beacon_ie = nla_data(bss[NL80211_BSS_BEACON_IES]);
5248 beacon_ie_len = nla_len(bss[NL80211_BSS_BEACON_IES]);
5249 } else {
5250 beacon_ie = NULL;
5251 beacon_ie_len = 0;
5252 }
5253
5254 if (nl80211_scan_filtered(_arg->drv, ie ? ie : beacon_ie,
5255 ie ? ie_len : beacon_ie_len))
5256 return NL_SKIP;
5257
5258 r = os_zalloc(sizeof(*r) + ie_len + beacon_ie_len);
5259 if (r == NULL)
5260 return NL_SKIP;
5261 if (bss[NL80211_BSS_BSSID])
5262 os_memcpy(r->bssid, nla_data(bss[NL80211_BSS_BSSID]),
5263 ETH_ALEN);
5264 if (bss[NL80211_BSS_FREQUENCY])
5265 r->freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
5266 if (bss[NL80211_BSS_BEACON_INTERVAL])
5267 r->beacon_int = nla_get_u16(bss[NL80211_BSS_BEACON_INTERVAL]);
5268 if (bss[NL80211_BSS_CAPABILITY])
5269 r->caps = nla_get_u16(bss[NL80211_BSS_CAPABILITY]);
5270 r->flags |= WPA_SCAN_NOISE_INVALID;
5271 if (bss[NL80211_BSS_SIGNAL_MBM]) {
5272 r->level = nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]);
5273 r->level /= 100; /* mBm to dBm */
5274 r->flags |= WPA_SCAN_LEVEL_DBM | WPA_SCAN_QUAL_INVALID;
5275 } else if (bss[NL80211_BSS_SIGNAL_UNSPEC]) {
5276 r->level = nla_get_u8(bss[NL80211_BSS_SIGNAL_UNSPEC]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005277 r->flags |= WPA_SCAN_QUAL_INVALID;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005278 } else
5279 r->flags |= WPA_SCAN_LEVEL_INVALID | WPA_SCAN_QUAL_INVALID;
5280 if (bss[NL80211_BSS_TSF])
5281 r->tsf = nla_get_u64(bss[NL80211_BSS_TSF]);
5282 if (bss[NL80211_BSS_SEEN_MS_AGO])
5283 r->age = nla_get_u32(bss[NL80211_BSS_SEEN_MS_AGO]);
5284 r->ie_len = ie_len;
5285 pos = (u8 *) (r + 1);
5286 if (ie) {
5287 os_memcpy(pos, ie, ie_len);
5288 pos += ie_len;
5289 }
5290 r->beacon_ie_len = beacon_ie_len;
5291 if (beacon_ie)
5292 os_memcpy(pos, beacon_ie, beacon_ie_len);
5293
5294 if (bss[NL80211_BSS_STATUS]) {
5295 enum nl80211_bss_status status;
5296 status = nla_get_u32(bss[NL80211_BSS_STATUS]);
5297 switch (status) {
5298 case NL80211_BSS_STATUS_AUTHENTICATED:
5299 r->flags |= WPA_SCAN_AUTHENTICATED;
5300 break;
5301 case NL80211_BSS_STATUS_ASSOCIATED:
5302 r->flags |= WPA_SCAN_ASSOCIATED;
5303 break;
5304 default:
5305 break;
5306 }
5307 }
5308
Jouni Malinen87fd2792011-05-16 18:35:42 +03005309 /*
5310 * cfg80211 maintains separate BSS table entries for APs if the same
5311 * BSSID,SSID pair is seen on multiple channels. wpa_supplicant does
5312 * not use frequency as a separate key in the BSS table, so filter out
5313 * duplicated entries. Prefer associated BSS entry in such a case in
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005314 * order to get the correct frequency into the BSS table. Similarly,
5315 * prefer newer entries over older.
Jouni Malinen87fd2792011-05-16 18:35:42 +03005316 */
5317 for (i = 0; i < res->num; i++) {
5318 const u8 *s1, *s2;
5319 if (os_memcmp(res->res[i]->bssid, r->bssid, ETH_ALEN) != 0)
5320 continue;
5321
5322 s1 = nl80211_get_ie((u8 *) (res->res[i] + 1),
5323 res->res[i]->ie_len, WLAN_EID_SSID);
5324 s2 = nl80211_get_ie((u8 *) (r + 1), r->ie_len, WLAN_EID_SSID);
5325 if (s1 == NULL || s2 == NULL || s1[1] != s2[1] ||
5326 os_memcmp(s1, s2, 2 + s1[1]) != 0)
5327 continue;
5328
5329 /* Same BSSID,SSID was already included in scan results */
5330 wpa_printf(MSG_DEBUG, "nl80211: Remove duplicated scan result "
5331 "for " MACSTR, MAC2STR(r->bssid));
5332
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005333 if (((r->flags & WPA_SCAN_ASSOCIATED) &&
5334 !(res->res[i]->flags & WPA_SCAN_ASSOCIATED)) ||
5335 r->age < res->res[i]->age) {
Jouni Malinen87fd2792011-05-16 18:35:42 +03005336 os_free(res->res[i]);
5337 res->res[i] = r;
5338 } else
5339 os_free(r);
5340 return NL_SKIP;
5341 }
5342
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005343 tmp = os_realloc_array(res->res, res->num + 1,
5344 sizeof(struct wpa_scan_res *));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005345 if (tmp == NULL) {
5346 os_free(r);
5347 return NL_SKIP;
5348 }
5349 tmp[res->num++] = r;
5350 res->res = tmp;
5351
5352 return NL_SKIP;
5353}
5354
5355
5356static void clear_state_mismatch(struct wpa_driver_nl80211_data *drv,
5357 const u8 *addr)
5358{
5359 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
5360 wpa_printf(MSG_DEBUG, "nl80211: Clear possible state "
5361 "mismatch (" MACSTR ")", MAC2STR(addr));
5362 wpa_driver_nl80211_mlme(drv, addr,
5363 NL80211_CMD_DEAUTHENTICATE,
5364 WLAN_REASON_PREV_AUTH_NOT_VALID, 1);
5365 }
5366}
5367
5368
5369static void wpa_driver_nl80211_check_bss_status(
5370 struct wpa_driver_nl80211_data *drv, struct wpa_scan_results *res)
5371{
5372 size_t i;
5373
5374 for (i = 0; i < res->num; i++) {
5375 struct wpa_scan_res *r = res->res[i];
5376 if (r->flags & WPA_SCAN_AUTHENTICATED) {
5377 wpa_printf(MSG_DEBUG, "nl80211: Scan results "
5378 "indicates BSS status with " MACSTR
5379 " as authenticated",
5380 MAC2STR(r->bssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005381 if (is_sta_interface(drv->nlmode) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005382 os_memcmp(r->bssid, drv->bssid, ETH_ALEN) != 0 &&
5383 os_memcmp(r->bssid, drv->auth_bssid, ETH_ALEN) !=
5384 0) {
5385 wpa_printf(MSG_DEBUG, "nl80211: Unknown BSSID"
5386 " in local state (auth=" MACSTR
5387 " assoc=" MACSTR ")",
5388 MAC2STR(drv->auth_bssid),
5389 MAC2STR(drv->bssid));
5390 clear_state_mismatch(drv, r->bssid);
5391 }
5392 }
5393
5394 if (r->flags & WPA_SCAN_ASSOCIATED) {
5395 wpa_printf(MSG_DEBUG, "nl80211: Scan results "
5396 "indicate BSS status with " MACSTR
5397 " as associated",
5398 MAC2STR(r->bssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005399 if (is_sta_interface(drv->nlmode) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005400 !drv->associated) {
5401 wpa_printf(MSG_DEBUG, "nl80211: Local state "
5402 "(not associated) does not match "
5403 "with BSS state");
5404 clear_state_mismatch(drv, r->bssid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005405 } else if (is_sta_interface(drv->nlmode) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005406 os_memcmp(drv->bssid, r->bssid, ETH_ALEN) !=
5407 0) {
5408 wpa_printf(MSG_DEBUG, "nl80211: Local state "
5409 "(associated with " MACSTR ") does "
5410 "not match with BSS state",
5411 MAC2STR(drv->bssid));
5412 clear_state_mismatch(drv, r->bssid);
5413 clear_state_mismatch(drv, drv->bssid);
5414 }
5415 }
5416 }
5417}
5418
5419
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005420static struct wpa_scan_results *
5421nl80211_get_scan_results(struct wpa_driver_nl80211_data *drv)
5422{
5423 struct nl_msg *msg;
5424 struct wpa_scan_results *res;
5425 int ret;
5426 struct nl80211_bss_info_arg arg;
5427
5428 res = os_zalloc(sizeof(*res));
5429 if (res == NULL)
5430 return NULL;
5431 msg = nlmsg_alloc();
5432 if (!msg)
5433 goto nla_put_failure;
5434
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005435 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005436 if (nl80211_set_iface_id(msg, drv->first_bss) < 0)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005437 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005438
5439 arg.drv = drv;
5440 arg.res = res;
5441 ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
5442 msg = NULL;
5443 if (ret == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005444 wpa_printf(MSG_DEBUG, "nl80211: Received scan results (%lu "
5445 "BSSes)", (unsigned long) res->num);
5446 nl80211_get_noise_for_scan_results(drv, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005447 return res;
5448 }
5449 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
5450 "(%s)", ret, strerror(-ret));
5451nla_put_failure:
5452 nlmsg_free(msg);
5453 wpa_scan_results_free(res);
5454 return NULL;
5455}
5456
5457
5458/**
5459 * wpa_driver_nl80211_get_scan_results - Fetch the latest scan results
5460 * @priv: Pointer to private wext data from wpa_driver_nl80211_init()
5461 * Returns: Scan results on success, -1 on failure
5462 */
5463static struct wpa_scan_results *
5464wpa_driver_nl80211_get_scan_results(void *priv)
5465{
5466 struct i802_bss *bss = priv;
5467 struct wpa_driver_nl80211_data *drv = bss->drv;
5468 struct wpa_scan_results *res;
5469
5470 res = nl80211_get_scan_results(drv);
5471 if (res)
5472 wpa_driver_nl80211_check_bss_status(drv, res);
5473 return res;
5474}
5475
5476
5477static void nl80211_dump_scan(struct wpa_driver_nl80211_data *drv)
5478{
5479 struct wpa_scan_results *res;
5480 size_t i;
5481
5482 res = nl80211_get_scan_results(drv);
5483 if (res == NULL) {
5484 wpa_printf(MSG_DEBUG, "nl80211: Failed to get scan results");
5485 return;
5486 }
5487
5488 wpa_printf(MSG_DEBUG, "nl80211: Scan result dump");
5489 for (i = 0; i < res->num; i++) {
5490 struct wpa_scan_res *r = res->res[i];
5491 wpa_printf(MSG_DEBUG, "nl80211: %d/%d " MACSTR "%s%s",
5492 (int) i, (int) res->num, MAC2STR(r->bssid),
5493 r->flags & WPA_SCAN_AUTHENTICATED ? " [auth]" : "",
5494 r->flags & WPA_SCAN_ASSOCIATED ? " [assoc]" : "");
5495 }
5496
5497 wpa_scan_results_free(res);
5498}
5499
5500
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005501static u32 wpa_alg_to_cipher_suite(enum wpa_alg alg, size_t key_len)
5502{
5503 switch (alg) {
5504 case WPA_ALG_WEP:
5505 if (key_len == 5)
5506 return WLAN_CIPHER_SUITE_WEP40;
5507 return WLAN_CIPHER_SUITE_WEP104;
5508 case WPA_ALG_TKIP:
5509 return WLAN_CIPHER_SUITE_TKIP;
5510 case WPA_ALG_CCMP:
5511 return WLAN_CIPHER_SUITE_CCMP;
5512 case WPA_ALG_GCMP:
5513 return WLAN_CIPHER_SUITE_GCMP;
5514 case WPA_ALG_CCMP_256:
5515 return WLAN_CIPHER_SUITE_CCMP_256;
5516 case WPA_ALG_GCMP_256:
5517 return WLAN_CIPHER_SUITE_GCMP_256;
5518 case WPA_ALG_IGTK:
5519 return WLAN_CIPHER_SUITE_AES_CMAC;
5520 case WPA_ALG_BIP_GMAC_128:
5521 return WLAN_CIPHER_SUITE_BIP_GMAC_128;
5522 case WPA_ALG_BIP_GMAC_256:
5523 return WLAN_CIPHER_SUITE_BIP_GMAC_256;
5524 case WPA_ALG_BIP_CMAC_256:
5525 return WLAN_CIPHER_SUITE_BIP_CMAC_256;
5526 case WPA_ALG_SMS4:
5527 return WLAN_CIPHER_SUITE_SMS4;
5528 case WPA_ALG_KRK:
5529 return WLAN_CIPHER_SUITE_KRK;
5530 case WPA_ALG_NONE:
5531 case WPA_ALG_PMK:
5532 wpa_printf(MSG_ERROR, "nl80211: Unexpected encryption algorithm %d",
5533 alg);
5534 return 0;
5535 }
5536
5537 wpa_printf(MSG_ERROR, "nl80211: Unsupported encryption algorithm %d",
5538 alg);
5539 return 0;
5540}
5541
5542
5543static u32 wpa_cipher_to_cipher_suite(unsigned int cipher)
5544{
5545 switch (cipher) {
5546 case WPA_CIPHER_CCMP_256:
5547 return WLAN_CIPHER_SUITE_CCMP_256;
5548 case WPA_CIPHER_GCMP_256:
5549 return WLAN_CIPHER_SUITE_GCMP_256;
5550 case WPA_CIPHER_CCMP:
5551 return WLAN_CIPHER_SUITE_CCMP;
5552 case WPA_CIPHER_GCMP:
5553 return WLAN_CIPHER_SUITE_GCMP;
5554 case WPA_CIPHER_TKIP:
5555 return WLAN_CIPHER_SUITE_TKIP;
5556 case WPA_CIPHER_WEP104:
5557 return WLAN_CIPHER_SUITE_WEP104;
5558 case WPA_CIPHER_WEP40:
5559 return WLAN_CIPHER_SUITE_WEP40;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08005560 case WPA_CIPHER_GTK_NOT_USED:
5561 return WLAN_CIPHER_SUITE_NO_GROUP_ADDR;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005562 }
5563
5564 return 0;
5565}
5566
5567
5568static int wpa_cipher_to_cipher_suites(unsigned int ciphers, u32 suites[],
5569 int max_suites)
5570{
5571 int num_suites = 0;
5572
5573 if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP_256)
5574 suites[num_suites++] = WLAN_CIPHER_SUITE_CCMP_256;
5575 if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP_256)
5576 suites[num_suites++] = WLAN_CIPHER_SUITE_GCMP_256;
5577 if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP)
5578 suites[num_suites++] = WLAN_CIPHER_SUITE_CCMP;
5579 if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP)
5580 suites[num_suites++] = WLAN_CIPHER_SUITE_GCMP;
5581 if (num_suites < max_suites && ciphers & WPA_CIPHER_TKIP)
5582 suites[num_suites++] = WLAN_CIPHER_SUITE_TKIP;
5583 if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP104)
5584 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP104;
5585 if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP40)
5586 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP40;
5587
5588 return num_suites;
5589}
5590
5591
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005592static int wpa_driver_nl80211_set_key(const char *ifname, struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005593 enum wpa_alg alg, const u8 *addr,
5594 int key_idx, int set_tx,
5595 const u8 *seq, size_t seq_len,
5596 const u8 *key, size_t key_len)
5597{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005598 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005599 int ifindex;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005600 struct nl_msg *msg;
5601 int ret;
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07005602 int tdls = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005603
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005604 /* Ignore for P2P Device */
5605 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
5606 return 0;
5607
5608 ifindex = if_nametoindex(ifname);
5609 wpa_printf(MSG_DEBUG, "%s: ifindex=%d (%s) alg=%d addr=%p key_idx=%d "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005610 "set_tx=%d seq_len=%lu key_len=%lu",
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005611 __func__, ifindex, ifname, alg, addr, key_idx, set_tx,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005612 (unsigned long) seq_len, (unsigned long) key_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005613#ifdef CONFIG_TDLS
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07005614 if (key_idx == -1) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005615 key_idx = 0;
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07005616 tdls = 1;
5617 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005618#endif /* CONFIG_TDLS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005619
5620 msg = nlmsg_alloc();
5621 if (!msg)
5622 return -ENOMEM;
5623
5624 if (alg == WPA_ALG_NONE) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005625 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_KEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005626 } else {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005627 nl80211_cmd(drv, msg, 0, NL80211_CMD_NEW_KEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005628 NLA_PUT(msg, NL80211_ATTR_KEY_DATA, key_len, key);
Dmitry Shmidt98660862014-03-11 17:26:21 -07005629 wpa_hexdump_key(MSG_DEBUG, "nl80211: KEY_DATA", key, key_len);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005630 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
5631 wpa_alg_to_cipher_suite(alg, key_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005632 }
5633
Dmitry Shmidt98660862014-03-11 17:26:21 -07005634 if (seq && seq_len) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005635 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, seq_len, seq);
Dmitry Shmidt98660862014-03-11 17:26:21 -07005636 wpa_hexdump(MSG_DEBUG, "nl80211: KEY_SEQ", seq, seq_len);
5637 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005638
5639 if (addr && !is_broadcast_ether_addr(addr)) {
5640 wpa_printf(MSG_DEBUG, " addr=" MACSTR, MAC2STR(addr));
5641 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
5642
5643 if (alg != WPA_ALG_WEP && key_idx && !set_tx) {
5644 wpa_printf(MSG_DEBUG, " RSN IBSS RX GTK");
5645 NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE,
5646 NL80211_KEYTYPE_GROUP);
5647 }
5648 } else if (addr && is_broadcast_ether_addr(addr)) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005649 struct nlattr *types;
5650
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005651 wpa_printf(MSG_DEBUG, " broadcast key");
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005652
5653 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005654 if (!types)
5655 goto nla_put_failure;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005656 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST);
5657 nla_nest_end(msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005658 }
5659 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
5660 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
5661
5662 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5663 if ((ret == -ENOENT || ret == -ENOLINK) && alg == WPA_ALG_NONE)
5664 ret = 0;
5665 if (ret)
5666 wpa_printf(MSG_DEBUG, "nl80211: set_key failed; err=%d %s)",
5667 ret, strerror(-ret));
5668
5669 /*
5670 * If we failed or don't need to set the default TX key (below),
5671 * we're done here.
5672 */
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07005673 if (ret || !set_tx || alg == WPA_ALG_NONE || tdls)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005674 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005675 if (is_ap_interface(drv->nlmode) && addr &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005676 !is_broadcast_ether_addr(addr))
5677 return ret;
5678
5679 msg = nlmsg_alloc();
5680 if (!msg)
5681 return -ENOMEM;
5682
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005683 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_KEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005684 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
5685 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
5686 if (alg == WPA_ALG_IGTK)
5687 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT_MGMT);
5688 else
5689 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT);
5690 if (addr && is_broadcast_ether_addr(addr)) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005691 struct nlattr *types;
5692
5693 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005694 if (!types)
5695 goto nla_put_failure;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005696 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST);
5697 nla_nest_end(msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005698 } else if (addr) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005699 struct nlattr *types;
5700
5701 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005702 if (!types)
5703 goto nla_put_failure;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005704 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_UNICAST);
5705 nla_nest_end(msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005706 }
5707
5708 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5709 if (ret == -ENOENT)
5710 ret = 0;
5711 if (ret)
5712 wpa_printf(MSG_DEBUG, "nl80211: set_key default failed; "
5713 "err=%d %s)", ret, strerror(-ret));
5714 return ret;
5715
5716nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005717 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005718 return -ENOBUFS;
5719}
5720
5721
5722static int nl_add_key(struct nl_msg *msg, enum wpa_alg alg,
5723 int key_idx, int defkey,
5724 const u8 *seq, size_t seq_len,
5725 const u8 *key, size_t key_len)
5726{
5727 struct nlattr *key_attr = nla_nest_start(msg, NL80211_ATTR_KEY);
5728 if (!key_attr)
5729 return -1;
5730
5731 if (defkey && alg == WPA_ALG_IGTK)
5732 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_MGMT);
5733 else if (defkey)
5734 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
5735
5736 NLA_PUT_U8(msg, NL80211_KEY_IDX, key_idx);
5737
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005738 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
5739 wpa_alg_to_cipher_suite(alg, key_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005740
5741 if (seq && seq_len)
5742 NLA_PUT(msg, NL80211_KEY_SEQ, seq_len, seq);
5743
5744 NLA_PUT(msg, NL80211_KEY_DATA, key_len, key);
5745
5746 nla_nest_end(msg, key_attr);
5747
5748 return 0;
5749 nla_put_failure:
5750 return -1;
5751}
5752
5753
5754static int nl80211_set_conn_keys(struct wpa_driver_associate_params *params,
5755 struct nl_msg *msg)
5756{
5757 int i, privacy = 0;
5758 struct nlattr *nl_keys, *nl_key;
5759
5760 for (i = 0; i < 4; i++) {
5761 if (!params->wep_key[i])
5762 continue;
5763 privacy = 1;
5764 break;
5765 }
5766 if (params->wps == WPS_MODE_PRIVACY)
5767 privacy = 1;
5768 if (params->pairwise_suite &&
5769 params->pairwise_suite != WPA_CIPHER_NONE)
5770 privacy = 1;
5771
5772 if (!privacy)
5773 return 0;
5774
5775 NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
5776
5777 nl_keys = nla_nest_start(msg, NL80211_ATTR_KEYS);
5778 if (!nl_keys)
5779 goto nla_put_failure;
5780
5781 for (i = 0; i < 4; i++) {
5782 if (!params->wep_key[i])
5783 continue;
5784
5785 nl_key = nla_nest_start(msg, i);
5786 if (!nl_key)
5787 goto nla_put_failure;
5788
5789 NLA_PUT(msg, NL80211_KEY_DATA, params->wep_key_len[i],
5790 params->wep_key[i]);
5791 if (params->wep_key_len[i] == 5)
5792 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
5793 WLAN_CIPHER_SUITE_WEP40);
5794 else
5795 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
5796 WLAN_CIPHER_SUITE_WEP104);
5797
5798 NLA_PUT_U8(msg, NL80211_KEY_IDX, i);
5799
5800 if (i == params->wep_tx_keyidx)
5801 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
5802
5803 nla_nest_end(msg, nl_key);
5804 }
5805 nla_nest_end(msg, nl_keys);
5806
5807 return 0;
5808
5809nla_put_failure:
5810 return -ENOBUFS;
5811}
5812
5813
5814static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
5815 const u8 *addr, int cmd, u16 reason_code,
5816 int local_state_change)
5817{
5818 int ret = -1;
5819 struct nl_msg *msg;
5820
5821 msg = nlmsg_alloc();
5822 if (!msg)
5823 return -1;
5824
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005825 nl80211_cmd(drv, msg, 0, cmd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005826
5827 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
5828 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason_code);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005829 if (addr)
5830 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005831 if (local_state_change)
5832 NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE);
5833
5834 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5835 msg = NULL;
5836 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005837 wpa_dbg(drv->ctx, MSG_DEBUG,
5838 "nl80211: MLME command failed: reason=%u ret=%d (%s)",
5839 reason_code, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005840 goto nla_put_failure;
5841 }
5842 ret = 0;
5843
5844nla_put_failure:
5845 nlmsg_free(msg);
5846 return ret;
5847}
5848
5849
5850static int wpa_driver_nl80211_disconnect(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005851 int reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005852{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005853 int ret;
5854
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005855 wpa_printf(MSG_DEBUG, "%s(reason_code=%d)", __func__, reason_code);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005856 nl80211_mark_disconnected(drv);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005857 /* Disconnect command doesn't need BSSID - it uses cached value */
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005858 ret = wpa_driver_nl80211_mlme(drv, NULL, NL80211_CMD_DISCONNECT,
5859 reason_code, 0);
5860 /*
5861 * For locally generated disconnect, supplicant already generates a
5862 * DEAUTH event, so ignore the event from NL80211.
5863 */
5864 drv->ignore_next_local_disconnect = ret == 0;
5865
5866 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005867}
5868
5869
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005870static int wpa_driver_nl80211_deauthenticate(struct i802_bss *bss,
5871 const u8 *addr, int reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005872{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005873 struct wpa_driver_nl80211_data *drv = bss->drv;
5874 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME))
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005875 return wpa_driver_nl80211_disconnect(drv, reason_code);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005876 wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " reason_code=%d)",
5877 __func__, MAC2STR(addr), reason_code);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005878 nl80211_mark_disconnected(drv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005879 if (drv->nlmode == NL80211_IFTYPE_ADHOC)
5880 return nl80211_leave_ibss(drv);
5881 return wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DEAUTHENTICATE,
5882 reason_code, 0);
5883}
5884
5885
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005886static void nl80211_copy_auth_params(struct wpa_driver_nl80211_data *drv,
5887 struct wpa_driver_auth_params *params)
5888{
5889 int i;
5890
5891 drv->auth_freq = params->freq;
5892 drv->auth_alg = params->auth_alg;
5893 drv->auth_wep_tx_keyidx = params->wep_tx_keyidx;
5894 drv->auth_local_state_change = params->local_state_change;
5895 drv->auth_p2p = params->p2p;
5896
5897 if (params->bssid)
5898 os_memcpy(drv->auth_bssid_, params->bssid, ETH_ALEN);
5899 else
5900 os_memset(drv->auth_bssid_, 0, ETH_ALEN);
5901
5902 if (params->ssid) {
5903 os_memcpy(drv->auth_ssid, params->ssid, params->ssid_len);
5904 drv->auth_ssid_len = params->ssid_len;
5905 } else
5906 drv->auth_ssid_len = 0;
5907
5908
5909 os_free(drv->auth_ie);
5910 drv->auth_ie = NULL;
5911 drv->auth_ie_len = 0;
5912 if (params->ie) {
5913 drv->auth_ie = os_malloc(params->ie_len);
5914 if (drv->auth_ie) {
5915 os_memcpy(drv->auth_ie, params->ie, params->ie_len);
5916 drv->auth_ie_len = params->ie_len;
5917 }
5918 }
5919
5920 for (i = 0; i < 4; i++) {
5921 if (params->wep_key[i] && params->wep_key_len[i] &&
5922 params->wep_key_len[i] <= 16) {
5923 os_memcpy(drv->auth_wep_key[i], params->wep_key[i],
5924 params->wep_key_len[i]);
5925 drv->auth_wep_key_len[i] = params->wep_key_len[i];
5926 } else
5927 drv->auth_wep_key_len[i] = 0;
5928 }
5929}
5930
5931
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005932static int wpa_driver_nl80211_authenticate(
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005933 struct i802_bss *bss, struct wpa_driver_auth_params *params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005934{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005935 struct wpa_driver_nl80211_data *drv = bss->drv;
5936 int ret = -1, i;
5937 struct nl_msg *msg;
5938 enum nl80211_auth_type type;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005939 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005940 int count = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005941 int is_retry;
5942
5943 is_retry = drv->retry_auth;
5944 drv->retry_auth = 0;
Dmitry Shmidt98660862014-03-11 17:26:21 -07005945 drv->ignore_deauth_event = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005946
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005947 nl80211_mark_disconnected(drv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005948 os_memset(drv->auth_bssid, 0, ETH_ALEN);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005949 if (params->bssid)
5950 os_memcpy(drv->auth_attempt_bssid, params->bssid, ETH_ALEN);
5951 else
5952 os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005953 /* FIX: IBSS mode */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005954 nlmode = params->p2p ?
5955 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
5956 if (drv->nlmode != nlmode &&
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005957 wpa_driver_nl80211_set_mode(bss, nlmode) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005958 return -1;
5959
5960retry:
5961 msg = nlmsg_alloc();
5962 if (!msg)
5963 return -1;
5964
5965 wpa_printf(MSG_DEBUG, "nl80211: Authenticate (ifindex=%d)",
5966 drv->ifindex);
5967
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005968 nl80211_cmd(drv, msg, 0, NL80211_CMD_AUTHENTICATE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005969
5970 for (i = 0; i < 4; i++) {
5971 if (!params->wep_key[i])
5972 continue;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005973 wpa_driver_nl80211_set_key(bss->ifname, bss, WPA_ALG_WEP,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005974 NULL, i,
5975 i == params->wep_tx_keyidx, NULL, 0,
5976 params->wep_key[i],
5977 params->wep_key_len[i]);
5978 if (params->wep_tx_keyidx != i)
5979 continue;
5980 if (nl_add_key(msg, WPA_ALG_WEP, i, 1, NULL, 0,
5981 params->wep_key[i], params->wep_key_len[i])) {
5982 nlmsg_free(msg);
5983 return -1;
5984 }
5985 }
5986
5987 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
5988 if (params->bssid) {
5989 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
5990 MAC2STR(params->bssid));
5991 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
5992 }
5993 if (params->freq) {
5994 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
5995 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
5996 }
5997 if (params->ssid) {
5998 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
5999 params->ssid, params->ssid_len);
6000 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
6001 params->ssid);
6002 }
6003 wpa_hexdump(MSG_DEBUG, " * IEs", params->ie, params->ie_len);
6004 if (params->ie)
6005 NLA_PUT(msg, NL80211_ATTR_IE, params->ie_len, params->ie);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006006 if (params->sae_data) {
6007 wpa_hexdump(MSG_DEBUG, " * SAE data", params->sae_data,
6008 params->sae_data_len);
6009 NLA_PUT(msg, NL80211_ATTR_SAE_DATA, params->sae_data_len,
6010 params->sae_data);
6011 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006012 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
6013 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
6014 else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
6015 type = NL80211_AUTHTYPE_SHARED_KEY;
6016 else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
6017 type = NL80211_AUTHTYPE_NETWORK_EAP;
6018 else if (params->auth_alg & WPA_AUTH_ALG_FT)
6019 type = NL80211_AUTHTYPE_FT;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006020 else if (params->auth_alg & WPA_AUTH_ALG_SAE)
6021 type = NL80211_AUTHTYPE_SAE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006022 else
6023 goto nla_put_failure;
6024 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
6025 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
6026 if (params->local_state_change) {
6027 wpa_printf(MSG_DEBUG, " * Local state change only");
6028 NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE);
6029 }
6030
6031 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6032 msg = NULL;
6033 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006034 wpa_dbg(drv->ctx, MSG_DEBUG,
6035 "nl80211: MLME command failed (auth): ret=%d (%s)",
6036 ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006037 count++;
6038 if (ret == -EALREADY && count == 1 && params->bssid &&
6039 !params->local_state_change) {
6040 /*
6041 * mac80211 does not currently accept new
6042 * authentication if we are already authenticated. As a
6043 * workaround, force deauthentication and try again.
6044 */
6045 wpa_printf(MSG_DEBUG, "nl80211: Retry authentication "
6046 "after forced deauthentication");
Dmitry Shmidt98660862014-03-11 17:26:21 -07006047 drv->ignore_deauth_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006048 wpa_driver_nl80211_deauthenticate(
6049 bss, params->bssid,
6050 WLAN_REASON_PREV_AUTH_NOT_VALID);
6051 nlmsg_free(msg);
6052 goto retry;
6053 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006054
6055 if (ret == -ENOENT && params->freq && !is_retry) {
6056 /*
6057 * cfg80211 has likely expired the BSS entry even
6058 * though it was previously available in our internal
6059 * BSS table. To recover quickly, start a single
6060 * channel scan on the specified channel.
6061 */
6062 struct wpa_driver_scan_params scan;
6063 int freqs[2];
6064
6065 os_memset(&scan, 0, sizeof(scan));
6066 scan.num_ssids = 1;
6067 if (params->ssid) {
6068 scan.ssids[0].ssid = params->ssid;
6069 scan.ssids[0].ssid_len = params->ssid_len;
6070 }
6071 freqs[0] = params->freq;
6072 freqs[1] = 0;
6073 scan.freqs = freqs;
6074 wpa_printf(MSG_DEBUG, "nl80211: Trigger single "
6075 "channel scan to refresh cfg80211 BSS "
6076 "entry");
6077 ret = wpa_driver_nl80211_scan(bss, &scan);
6078 if (ret == 0) {
6079 nl80211_copy_auth_params(drv, params);
6080 drv->scan_for_auth = 1;
6081 }
6082 } else if (is_retry) {
6083 /*
6084 * Need to indicate this with an event since the return
6085 * value from the retry is not delivered to core code.
6086 */
6087 union wpa_event_data event;
6088 wpa_printf(MSG_DEBUG, "nl80211: Authentication retry "
6089 "failed");
6090 os_memset(&event, 0, sizeof(event));
6091 os_memcpy(event.timeout_event.addr, drv->auth_bssid_,
6092 ETH_ALEN);
6093 wpa_supplicant_event(drv->ctx, EVENT_AUTH_TIMED_OUT,
6094 &event);
6095 }
6096
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006097 goto nla_put_failure;
6098 }
6099 ret = 0;
6100 wpa_printf(MSG_DEBUG, "nl80211: Authentication request send "
6101 "successfully");
6102
6103nla_put_failure:
6104 nlmsg_free(msg);
6105 return ret;
6106}
6107
6108
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006109static int wpa_driver_nl80211_authenticate_retry(
6110 struct wpa_driver_nl80211_data *drv)
6111{
6112 struct wpa_driver_auth_params params;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006113 struct i802_bss *bss = drv->first_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006114 int i;
6115
6116 wpa_printf(MSG_DEBUG, "nl80211: Try to authenticate again");
6117
6118 os_memset(&params, 0, sizeof(params));
6119 params.freq = drv->auth_freq;
6120 params.auth_alg = drv->auth_alg;
6121 params.wep_tx_keyidx = drv->auth_wep_tx_keyidx;
6122 params.local_state_change = drv->auth_local_state_change;
6123 params.p2p = drv->auth_p2p;
6124
6125 if (!is_zero_ether_addr(drv->auth_bssid_))
6126 params.bssid = drv->auth_bssid_;
6127
6128 if (drv->auth_ssid_len) {
6129 params.ssid = drv->auth_ssid;
6130 params.ssid_len = drv->auth_ssid_len;
6131 }
6132
6133 params.ie = drv->auth_ie;
6134 params.ie_len = drv->auth_ie_len;
6135
6136 for (i = 0; i < 4; i++) {
6137 if (drv->auth_wep_key_len[i]) {
6138 params.wep_key[i] = drv->auth_wep_key[i];
6139 params.wep_key_len[i] = drv->auth_wep_key_len[i];
6140 }
6141 }
6142
6143 drv->retry_auth = 1;
6144 return wpa_driver_nl80211_authenticate(bss, &params);
6145}
6146
6147
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006148struct phy_info_arg {
6149 u16 *num_modes;
6150 struct hostapd_hw_modes *modes;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006151 int last_mode, last_chan_idx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006152};
6153
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006154static void phy_info_ht_capa(struct hostapd_hw_modes *mode, struct nlattr *capa,
6155 struct nlattr *ampdu_factor,
6156 struct nlattr *ampdu_density,
6157 struct nlattr *mcs_set)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006158{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006159 if (capa)
6160 mode->ht_capab = nla_get_u16(capa);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006161
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006162 if (ampdu_factor)
6163 mode->a_mpdu_params |= nla_get_u8(ampdu_factor) & 0x03;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006164
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006165 if (ampdu_density)
6166 mode->a_mpdu_params |= nla_get_u8(ampdu_density) << 2;
6167
6168 if (mcs_set && nla_len(mcs_set) >= 16) {
6169 u8 *mcs;
6170 mcs = nla_data(mcs_set);
6171 os_memcpy(mode->mcs_set, mcs, 16);
6172 }
6173}
6174
6175
6176static void phy_info_vht_capa(struct hostapd_hw_modes *mode,
6177 struct nlattr *capa,
6178 struct nlattr *mcs_set)
6179{
6180 if (capa)
6181 mode->vht_capab = nla_get_u32(capa);
6182
6183 if (mcs_set && nla_len(mcs_set) >= 8) {
6184 u8 *mcs;
6185 mcs = nla_data(mcs_set);
6186 os_memcpy(mode->vht_mcs_set, mcs, 8);
6187 }
6188}
6189
6190
6191static void phy_info_freq(struct hostapd_hw_modes *mode,
6192 struct hostapd_channel_data *chan,
6193 struct nlattr *tb_freq[])
6194{
Dmitry Shmidt4b060592013-04-29 16:42:49 -07006195 u8 channel;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006196 chan->freq = nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_FREQ]);
6197 chan->flag = 0;
Dmitry Shmidt4b060592013-04-29 16:42:49 -07006198 if (ieee80211_freq_to_chan(chan->freq, &channel) != NUM_HOSTAPD_MODES)
6199 chan->chan = channel;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006200
6201 if (tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
6202 chan->flag |= HOSTAPD_CHAN_DISABLED;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006203 if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IR])
6204 chan->flag |= HOSTAPD_CHAN_PASSIVE_SCAN | HOSTAPD_CHAN_NO_IBSS;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006205 if (tb_freq[NL80211_FREQUENCY_ATTR_RADAR])
6206 chan->flag |= HOSTAPD_CHAN_RADAR;
6207
Dmitry Shmidtea69e842013-05-13 14:52:28 -07006208 if (tb_freq[NL80211_FREQUENCY_ATTR_DFS_STATE]) {
6209 enum nl80211_dfs_state state =
6210 nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_DFS_STATE]);
6211
6212 switch (state) {
6213 case NL80211_DFS_USABLE:
6214 chan->flag |= HOSTAPD_CHAN_DFS_USABLE;
6215 break;
6216 case NL80211_DFS_AVAILABLE:
6217 chan->flag |= HOSTAPD_CHAN_DFS_AVAILABLE;
6218 break;
6219 case NL80211_DFS_UNAVAILABLE:
6220 chan->flag |= HOSTAPD_CHAN_DFS_UNAVAILABLE;
6221 break;
6222 }
6223 }
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006224}
6225
6226
6227static int phy_info_freqs(struct phy_info_arg *phy_info,
6228 struct hostapd_hw_modes *mode, struct nlattr *tb)
6229{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006230 static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
6231 [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
6232 [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006233 [NL80211_FREQUENCY_ATTR_NO_IR] = { .type = NLA_FLAG },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006234 [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
6235 [NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32 },
Dmitry Shmidtea69e842013-05-13 14:52:28 -07006236 [NL80211_FREQUENCY_ATTR_DFS_STATE] = { .type = NLA_U32 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006237 };
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006238 int new_channels = 0;
6239 struct hostapd_channel_data *channel;
6240 struct nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006241 struct nlattr *nl_freq;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006242 int rem_freq, idx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006243
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006244 if (tb == NULL)
6245 return NL_OK;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006246
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006247 nla_for_each_nested(nl_freq, tb, rem_freq) {
6248 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX,
6249 nla_data(nl_freq), nla_len(nl_freq), freq_policy);
6250 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
6251 continue;
6252 new_channels++;
6253 }
6254
6255 channel = os_realloc_array(mode->channels,
6256 mode->num_channels + new_channels,
6257 sizeof(struct hostapd_channel_data));
6258 if (!channel)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006259 return NL_SKIP;
6260
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006261 mode->channels = channel;
6262 mode->num_channels += new_channels;
6263
6264 idx = phy_info->last_chan_idx;
6265
6266 nla_for_each_nested(nl_freq, tb, rem_freq) {
6267 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX,
6268 nla_data(nl_freq), nla_len(nl_freq), freq_policy);
6269 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
6270 continue;
6271 phy_info_freq(mode, &mode->channels[idx], tb_freq);
6272 idx++;
6273 }
6274 phy_info->last_chan_idx = idx;
6275
6276 return NL_OK;
6277}
6278
6279
6280static int phy_info_rates(struct hostapd_hw_modes *mode, struct nlattr *tb)
6281{
6282 static struct nla_policy rate_policy[NL80211_BITRATE_ATTR_MAX + 1] = {
6283 [NL80211_BITRATE_ATTR_RATE] = { .type = NLA_U32 },
6284 [NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE] =
6285 { .type = NLA_FLAG },
6286 };
6287 struct nlattr *tb_rate[NL80211_BITRATE_ATTR_MAX + 1];
6288 struct nlattr *nl_rate;
6289 int rem_rate, idx;
6290
6291 if (tb == NULL)
6292 return NL_OK;
6293
6294 nla_for_each_nested(nl_rate, tb, rem_rate) {
6295 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX,
6296 nla_data(nl_rate), nla_len(nl_rate),
6297 rate_policy);
6298 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
6299 continue;
6300 mode->num_rates++;
6301 }
6302
6303 mode->rates = os_calloc(mode->num_rates, sizeof(int));
6304 if (!mode->rates)
6305 return NL_SKIP;
6306
6307 idx = 0;
6308
6309 nla_for_each_nested(nl_rate, tb, rem_rate) {
6310 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX,
6311 nla_data(nl_rate), nla_len(nl_rate),
6312 rate_policy);
6313 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
6314 continue;
6315 mode->rates[idx] = nla_get_u32(
6316 tb_rate[NL80211_BITRATE_ATTR_RATE]);
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006317 idx++;
6318 }
6319
6320 return NL_OK;
6321}
6322
6323
6324static int phy_info_band(struct phy_info_arg *phy_info, struct nlattr *nl_band)
6325{
6326 struct nlattr *tb_band[NL80211_BAND_ATTR_MAX + 1];
6327 struct hostapd_hw_modes *mode;
6328 int ret;
6329
6330 if (phy_info->last_mode != nl_band->nla_type) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006331 mode = os_realloc_array(phy_info->modes,
6332 *phy_info->num_modes + 1,
6333 sizeof(*mode));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006334 if (!mode)
6335 return NL_SKIP;
6336 phy_info->modes = mode;
6337
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006338 mode = &phy_info->modes[*(phy_info->num_modes)];
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006339 os_memset(mode, 0, sizeof(*mode));
6340 mode->mode = NUM_HOSTAPD_MODES;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006341 mode->flags = HOSTAPD_MODE_FLAG_HT_INFO_KNOWN |
6342 HOSTAPD_MODE_FLAG_VHT_INFO_KNOWN;
6343
6344 /*
6345 * Unsupported VHT MCS stream is defined as value 3, so the VHT
6346 * MCS RX/TX map must be initialized with 0xffff to mark all 8
6347 * possible streams as unsupported. This will be overridden if
6348 * driver advertises VHT support.
6349 */
6350 mode->vht_mcs_set[0] = 0xff;
6351 mode->vht_mcs_set[1] = 0xff;
6352 mode->vht_mcs_set[4] = 0xff;
6353 mode->vht_mcs_set[5] = 0xff;
6354
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006355 *(phy_info->num_modes) += 1;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006356 phy_info->last_mode = nl_band->nla_type;
6357 phy_info->last_chan_idx = 0;
6358 } else
6359 mode = &phy_info->modes[*(phy_info->num_modes) - 1];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006360
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006361 nla_parse(tb_band, NL80211_BAND_ATTR_MAX, nla_data(nl_band),
6362 nla_len(nl_band), NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006363
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006364 phy_info_ht_capa(mode, tb_band[NL80211_BAND_ATTR_HT_CAPA],
6365 tb_band[NL80211_BAND_ATTR_HT_AMPDU_FACTOR],
6366 tb_band[NL80211_BAND_ATTR_HT_AMPDU_DENSITY],
6367 tb_band[NL80211_BAND_ATTR_HT_MCS_SET]);
6368 phy_info_vht_capa(mode, tb_band[NL80211_BAND_ATTR_VHT_CAPA],
6369 tb_band[NL80211_BAND_ATTR_VHT_MCS_SET]);
6370 ret = phy_info_freqs(phy_info, mode, tb_band[NL80211_BAND_ATTR_FREQS]);
6371 if (ret != NL_OK)
6372 return ret;
6373 ret = phy_info_rates(mode, tb_band[NL80211_BAND_ATTR_RATES]);
6374 if (ret != NL_OK)
6375 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006376
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006377 return NL_OK;
6378}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006379
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006380
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006381static int phy_info_handler(struct nl_msg *msg, void *arg)
6382{
6383 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
6384 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6385 struct phy_info_arg *phy_info = arg;
6386 struct nlattr *nl_band;
6387 int rem_band;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006388
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006389 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6390 genlmsg_attrlen(gnlh, 0), NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006391
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006392 if (!tb_msg[NL80211_ATTR_WIPHY_BANDS])
6393 return NL_SKIP;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006394
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006395 nla_for_each_nested(nl_band, tb_msg[NL80211_ATTR_WIPHY_BANDS], rem_band)
6396 {
6397 int res = phy_info_band(phy_info, nl_band);
6398 if (res != NL_OK)
6399 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006400 }
6401
6402 return NL_SKIP;
6403}
6404
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006405
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006406static struct hostapd_hw_modes *
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07006407wpa_driver_nl80211_postprocess_modes(struct hostapd_hw_modes *modes,
6408 u16 *num_modes)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006409{
6410 u16 m;
6411 struct hostapd_hw_modes *mode11g = NULL, *nmodes, *mode;
6412 int i, mode11g_idx = -1;
6413
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07006414 /* heuristic to set up modes */
6415 for (m = 0; m < *num_modes; m++) {
6416 if (!modes[m].num_channels)
6417 continue;
6418 if (modes[m].channels[0].freq < 4000) {
6419 modes[m].mode = HOSTAPD_MODE_IEEE80211B;
6420 for (i = 0; i < modes[m].num_rates; i++) {
6421 if (modes[m].rates[i] > 200) {
6422 modes[m].mode = HOSTAPD_MODE_IEEE80211G;
6423 break;
6424 }
6425 }
6426 } else if (modes[m].channels[0].freq > 50000)
6427 modes[m].mode = HOSTAPD_MODE_IEEE80211AD;
6428 else
6429 modes[m].mode = HOSTAPD_MODE_IEEE80211A;
6430 }
6431
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006432 /* If only 802.11g mode is included, use it to construct matching
6433 * 802.11b mode data. */
6434
6435 for (m = 0; m < *num_modes; m++) {
6436 if (modes[m].mode == HOSTAPD_MODE_IEEE80211B)
6437 return modes; /* 802.11b already included */
6438 if (modes[m].mode == HOSTAPD_MODE_IEEE80211G)
6439 mode11g_idx = m;
6440 }
6441
6442 if (mode11g_idx < 0)
6443 return modes; /* 2.4 GHz band not supported at all */
6444
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006445 nmodes = os_realloc_array(modes, *num_modes + 1, sizeof(*nmodes));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006446 if (nmodes == NULL)
6447 return modes; /* Could not add 802.11b mode */
6448
6449 mode = &nmodes[*num_modes];
6450 os_memset(mode, 0, sizeof(*mode));
6451 (*num_modes)++;
6452 modes = nmodes;
6453
6454 mode->mode = HOSTAPD_MODE_IEEE80211B;
6455
6456 mode11g = &modes[mode11g_idx];
6457 mode->num_channels = mode11g->num_channels;
6458 mode->channels = os_malloc(mode11g->num_channels *
6459 sizeof(struct hostapd_channel_data));
6460 if (mode->channels == NULL) {
6461 (*num_modes)--;
6462 return modes; /* Could not add 802.11b mode */
6463 }
6464 os_memcpy(mode->channels, mode11g->channels,
6465 mode11g->num_channels * sizeof(struct hostapd_channel_data));
6466
6467 mode->num_rates = 0;
6468 mode->rates = os_malloc(4 * sizeof(int));
6469 if (mode->rates == NULL) {
6470 os_free(mode->channels);
6471 (*num_modes)--;
6472 return modes; /* Could not add 802.11b mode */
6473 }
6474
6475 for (i = 0; i < mode11g->num_rates; i++) {
6476 if (mode11g->rates[i] != 10 && mode11g->rates[i] != 20 &&
6477 mode11g->rates[i] != 55 && mode11g->rates[i] != 110)
6478 continue;
6479 mode->rates[mode->num_rates] = mode11g->rates[i];
6480 mode->num_rates++;
6481 if (mode->num_rates == 4)
6482 break;
6483 }
6484
6485 if (mode->num_rates == 0) {
6486 os_free(mode->channels);
6487 os_free(mode->rates);
6488 (*num_modes)--;
6489 return modes; /* No 802.11b rates */
6490 }
6491
6492 wpa_printf(MSG_DEBUG, "nl80211: Added 802.11b mode based on 802.11g "
6493 "information");
6494
6495 return modes;
6496}
6497
6498
6499static void nl80211_set_ht40_mode(struct hostapd_hw_modes *mode, int start,
6500 int end)
6501{
6502 int c;
6503
6504 for (c = 0; c < mode->num_channels; c++) {
6505 struct hostapd_channel_data *chan = &mode->channels[c];
6506 if (chan->freq - 10 >= start && chan->freq + 10 <= end)
6507 chan->flag |= HOSTAPD_CHAN_HT40;
6508 }
6509}
6510
6511
6512static void nl80211_set_ht40_mode_sec(struct hostapd_hw_modes *mode, int start,
6513 int end)
6514{
6515 int c;
6516
6517 for (c = 0; c < mode->num_channels; c++) {
6518 struct hostapd_channel_data *chan = &mode->channels[c];
6519 if (!(chan->flag & HOSTAPD_CHAN_HT40))
6520 continue;
6521 if (chan->freq - 30 >= start && chan->freq - 10 <= end)
6522 chan->flag |= HOSTAPD_CHAN_HT40MINUS;
6523 if (chan->freq + 10 >= start && chan->freq + 30 <= end)
6524 chan->flag |= HOSTAPD_CHAN_HT40PLUS;
6525 }
6526}
6527
6528
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006529static void nl80211_reg_rule_max_eirp(u32 start, u32 end, u32 max_eirp,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006530 struct phy_info_arg *results)
6531{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006532 u16 m;
6533
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006534 for (m = 0; m < *results->num_modes; m++) {
6535 int c;
6536 struct hostapd_hw_modes *mode = &results->modes[m];
6537
6538 for (c = 0; c < mode->num_channels; c++) {
6539 struct hostapd_channel_data *chan = &mode->channels[c];
6540 if ((u32) chan->freq - 10 >= start &&
6541 (u32) chan->freq + 10 <= end)
6542 chan->max_tx_power = max_eirp;
6543 }
6544 }
6545}
6546
6547
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006548static void nl80211_reg_rule_ht40(u32 start, u32 end,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006549 struct phy_info_arg *results)
6550{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006551 u16 m;
6552
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006553 for (m = 0; m < *results->num_modes; m++) {
6554 if (!(results->modes[m].ht_capab &
6555 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
6556 continue;
6557 nl80211_set_ht40_mode(&results->modes[m], start, end);
6558 }
6559}
6560
6561
6562static void nl80211_reg_rule_sec(struct nlattr *tb[],
6563 struct phy_info_arg *results)
6564{
6565 u32 start, end, max_bw;
6566 u16 m;
6567
6568 if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
6569 tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
6570 tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
6571 return;
6572
6573 start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
6574 end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
6575 max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
6576
6577 if (max_bw < 20)
6578 return;
6579
6580 for (m = 0; m < *results->num_modes; m++) {
6581 if (!(results->modes[m].ht_capab &
6582 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
6583 continue;
6584 nl80211_set_ht40_mode_sec(&results->modes[m], start, end);
6585 }
6586}
6587
6588
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006589static void nl80211_set_vht_mode(struct hostapd_hw_modes *mode, int start,
6590 int end)
6591{
6592 int c;
6593
6594 for (c = 0; c < mode->num_channels; c++) {
6595 struct hostapd_channel_data *chan = &mode->channels[c];
6596 if (chan->freq - 10 >= start && chan->freq + 70 <= end)
6597 chan->flag |= HOSTAPD_CHAN_VHT_10_70;
6598
6599 if (chan->freq - 30 >= start && chan->freq + 50 <= end)
6600 chan->flag |= HOSTAPD_CHAN_VHT_30_50;
6601
6602 if (chan->freq - 50 >= start && chan->freq + 30 <= end)
6603 chan->flag |= HOSTAPD_CHAN_VHT_50_30;
6604
6605 if (chan->freq - 70 >= start && chan->freq + 10 <= end)
6606 chan->flag |= HOSTAPD_CHAN_VHT_70_10;
6607 }
6608}
6609
6610
6611static void nl80211_reg_rule_vht(struct nlattr *tb[],
6612 struct phy_info_arg *results)
6613{
6614 u32 start, end, max_bw;
6615 u16 m;
6616
6617 if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
6618 tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
6619 tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
6620 return;
6621
6622 start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
6623 end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
6624 max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
6625
6626 if (max_bw < 80)
6627 return;
6628
6629 for (m = 0; m < *results->num_modes; m++) {
6630 if (!(results->modes[m].ht_capab &
6631 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
6632 continue;
6633 /* TODO: use a real VHT support indication */
6634 if (!results->modes[m].vht_capab)
6635 continue;
6636
6637 nl80211_set_vht_mode(&results->modes[m], start, end);
6638 }
6639}
6640
6641
Dmitry Shmidt97672262014-02-03 13:02:54 -08006642static const char * dfs_domain_name(enum nl80211_dfs_regions region)
6643{
6644 switch (region) {
6645 case NL80211_DFS_UNSET:
6646 return "DFS-UNSET";
6647 case NL80211_DFS_FCC:
6648 return "DFS-FCC";
6649 case NL80211_DFS_ETSI:
6650 return "DFS-ETSI";
6651 case NL80211_DFS_JP:
6652 return "DFS-JP";
6653 default:
6654 return "DFS-invalid";
6655 }
6656}
6657
6658
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006659static int nl80211_get_reg(struct nl_msg *msg, void *arg)
6660{
6661 struct phy_info_arg *results = arg;
6662 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
6663 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6664 struct nlattr *nl_rule;
6665 struct nlattr *tb_rule[NL80211_FREQUENCY_ATTR_MAX + 1];
6666 int rem_rule;
6667 static struct nla_policy reg_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
6668 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
6669 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
6670 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
6671 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
6672 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
6673 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
6674 };
6675
6676 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6677 genlmsg_attrlen(gnlh, 0), NULL);
6678 if (!tb_msg[NL80211_ATTR_REG_ALPHA2] ||
6679 !tb_msg[NL80211_ATTR_REG_RULES]) {
6680 wpa_printf(MSG_DEBUG, "nl80211: No regulatory information "
6681 "available");
6682 return NL_SKIP;
6683 }
6684
Dmitry Shmidt97672262014-02-03 13:02:54 -08006685 if (tb_msg[NL80211_ATTR_DFS_REGION]) {
6686 enum nl80211_dfs_regions dfs_domain;
6687 dfs_domain = nla_get_u8(tb_msg[NL80211_ATTR_DFS_REGION]);
6688 wpa_printf(MSG_DEBUG, "nl80211: Regulatory information - country=%s (%s)",
6689 (char *) nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]),
6690 dfs_domain_name(dfs_domain));
6691 } else {
6692 wpa_printf(MSG_DEBUG, "nl80211: Regulatory information - country=%s",
6693 (char *) nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]));
6694 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006695
6696 nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
6697 {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08006698 u32 start, end, max_eirp = 0, max_bw = 0, flags = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006699 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
6700 nla_data(nl_rule), nla_len(nl_rule), reg_policy);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006701 if (tb_rule[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
6702 tb_rule[NL80211_ATTR_FREQ_RANGE_END] == NULL)
6703 continue;
6704 start = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
6705 end = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
6706 if (tb_rule[NL80211_ATTR_POWER_RULE_MAX_EIRP])
6707 max_eirp = nla_get_u32(tb_rule[NL80211_ATTR_POWER_RULE_MAX_EIRP]) / 100;
6708 if (tb_rule[NL80211_ATTR_FREQ_RANGE_MAX_BW])
6709 max_bw = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08006710 if (tb_rule[NL80211_ATTR_REG_RULE_FLAGS])
6711 flags = nla_get_u32(tb_rule[NL80211_ATTR_REG_RULE_FLAGS]);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006712
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08006713 wpa_printf(MSG_DEBUG, "nl80211: %u-%u @ %u MHz %u mBm%s%s%s%s%s%s%s%s",
6714 start, end, max_bw, max_eirp,
6715 flags & NL80211_RRF_NO_OFDM ? " (no OFDM)" : "",
6716 flags & NL80211_RRF_NO_CCK ? " (no CCK)" : "",
6717 flags & NL80211_RRF_NO_INDOOR ? " (no indoor)" : "",
6718 flags & NL80211_RRF_NO_OUTDOOR ? " (no outdoor)" :
6719 "",
6720 flags & NL80211_RRF_DFS ? " (DFS)" : "",
6721 flags & NL80211_RRF_PTP_ONLY ? " (PTP only)" : "",
6722 flags & NL80211_RRF_PTMP_ONLY ? " (PTMP only)" : "",
6723 flags & NL80211_RRF_NO_IR ? " (no IR)" : "");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006724 if (max_bw >= 40)
6725 nl80211_reg_rule_ht40(start, end, results);
6726 if (tb_rule[NL80211_ATTR_POWER_RULE_MAX_EIRP])
6727 nl80211_reg_rule_max_eirp(start, end, max_eirp,
6728 results);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006729 }
6730
6731 nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
6732 {
6733 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
6734 nla_data(nl_rule), nla_len(nl_rule), reg_policy);
6735 nl80211_reg_rule_sec(tb_rule, results);
6736 }
6737
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006738 nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
6739 {
6740 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
6741 nla_data(nl_rule), nla_len(nl_rule), reg_policy);
6742 nl80211_reg_rule_vht(tb_rule, results);
6743 }
6744
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006745 return NL_SKIP;
6746}
6747
6748
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006749static int nl80211_set_regulatory_flags(struct wpa_driver_nl80211_data *drv,
6750 struct phy_info_arg *results)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006751{
6752 struct nl_msg *msg;
6753
6754 msg = nlmsg_alloc();
6755 if (!msg)
6756 return -ENOMEM;
6757
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006758 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006759 return send_and_recv_msgs(drv, msg, nl80211_get_reg, results);
6760}
6761
6762
6763static struct hostapd_hw_modes *
6764wpa_driver_nl80211_get_hw_feature_data(void *priv, u16 *num_modes, u16 *flags)
6765{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006766 u32 feat;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006767 struct i802_bss *bss = priv;
6768 struct wpa_driver_nl80211_data *drv = bss->drv;
6769 struct nl_msg *msg;
6770 struct phy_info_arg result = {
6771 .num_modes = num_modes,
6772 .modes = NULL,
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006773 .last_mode = -1,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006774 };
6775
6776 *num_modes = 0;
6777 *flags = 0;
6778
6779 msg = nlmsg_alloc();
6780 if (!msg)
6781 return NULL;
6782
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006783 feat = get_nl80211_protocol_features(drv);
6784 if (feat & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP)
6785 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_WIPHY);
6786 else
6787 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006788
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006789 NLA_PUT_FLAG(msg, NL80211_ATTR_SPLIT_WIPHY_DUMP);
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08006790 if (nl80211_set_iface_id(msg, bss) < 0)
6791 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006792
6793 if (send_and_recv_msgs(drv, msg, phy_info_handler, &result) == 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006794 nl80211_set_regulatory_flags(drv, &result);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07006795 return wpa_driver_nl80211_postprocess_modes(result.modes,
6796 num_modes);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006797 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006798 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006799 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006800 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006801 return NULL;
6802}
6803
6804
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006805static int wpa_driver_nl80211_send_mntr(struct wpa_driver_nl80211_data *drv,
6806 const void *data, size_t len,
6807 int encrypt, int noack)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006808{
6809 __u8 rtap_hdr[] = {
6810 0x00, 0x00, /* radiotap version */
6811 0x0e, 0x00, /* radiotap length */
6812 0x02, 0xc0, 0x00, 0x00, /* bmap: flags, tx and rx flags */
6813 IEEE80211_RADIOTAP_F_FRAG, /* F_FRAG (fragment if required) */
6814 0x00, /* padding */
6815 0x00, 0x00, /* RX and TX flags to indicate that */
6816 0x00, 0x00, /* this is the injected frame directly */
6817 };
6818 struct iovec iov[2] = {
6819 {
6820 .iov_base = &rtap_hdr,
6821 .iov_len = sizeof(rtap_hdr),
6822 },
6823 {
6824 .iov_base = (void *) data,
6825 .iov_len = len,
6826 }
6827 };
6828 struct msghdr msg = {
6829 .msg_name = NULL,
6830 .msg_namelen = 0,
6831 .msg_iov = iov,
6832 .msg_iovlen = 2,
6833 .msg_control = NULL,
6834 .msg_controllen = 0,
6835 .msg_flags = 0,
6836 };
6837 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006838 u16 txflags = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006839
6840 if (encrypt)
6841 rtap_hdr[8] |= IEEE80211_RADIOTAP_F_WEP;
6842
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07006843 if (drv->monitor_sock < 0) {
6844 wpa_printf(MSG_DEBUG, "nl80211: No monitor socket available "
6845 "for %s", __func__);
6846 return -1;
6847 }
6848
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006849 if (noack)
6850 txflags |= IEEE80211_RADIOTAP_F_TX_NOACK;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006851 WPA_PUT_LE16(&rtap_hdr[12], txflags);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006852
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006853 res = sendmsg(drv->monitor_sock, &msg, 0);
6854 if (res < 0) {
6855 wpa_printf(MSG_INFO, "nl80211: sendmsg: %s", strerror(errno));
6856 return -1;
6857 }
6858 return 0;
6859}
6860
6861
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006862static int wpa_driver_nl80211_send_frame(struct i802_bss *bss,
6863 const void *data, size_t len,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006864 int encrypt, int noack,
6865 unsigned int freq, int no_cck,
6866 int offchanok, unsigned int wait_time)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006867{
6868 struct wpa_driver_nl80211_data *drv = bss->drv;
6869 u64 cookie;
Dmitry Shmidt051af732013-10-22 13:52:46 -07006870 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006871
Dmitry Shmidt56052862013-10-04 10:23:25 -07006872 if (freq == 0) {
6873 wpa_printf(MSG_DEBUG, "nl80211: send_frame - Use bss->freq=%u",
6874 bss->freq);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006875 freq = bss->freq;
Dmitry Shmidt56052862013-10-04 10:23:25 -07006876 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006877
Dmitry Shmidt56052862013-10-04 10:23:25 -07006878 if (drv->use_monitor) {
6879 wpa_printf(MSG_DEBUG, "nl80211: send_frame(freq=%u bss->freq=%u) -> send_mntr",
6880 freq, bss->freq);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006881 return wpa_driver_nl80211_send_mntr(drv, data, len,
6882 encrypt, noack);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006883 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006884
Dmitry Shmidt56052862013-10-04 10:23:25 -07006885 wpa_printf(MSG_DEBUG, "nl80211: send_frame -> send_frame_cmd");
Dmitry Shmidt051af732013-10-22 13:52:46 -07006886 res = nl80211_send_frame_cmd(bss, freq, wait_time, data, len,
6887 &cookie, no_cck, noack, offchanok);
6888 if (res == 0 && !noack) {
6889 const struct ieee80211_mgmt *mgmt;
6890 u16 fc;
6891
6892 mgmt = (const struct ieee80211_mgmt *) data;
6893 fc = le_to_host16(mgmt->frame_control);
6894 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
6895 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_ACTION) {
6896 wpa_printf(MSG_MSGDUMP,
6897 "nl80211: Update send_action_cookie from 0x%llx to 0x%llx",
6898 (long long unsigned int)
6899 drv->send_action_cookie,
6900 (long long unsigned int) cookie);
6901 drv->send_action_cookie = cookie;
6902 }
6903 }
6904
6905 return res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006906}
6907
6908
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006909static int wpa_driver_nl80211_send_mlme(struct i802_bss *bss, const u8 *data,
6910 size_t data_len, int noack,
6911 unsigned int freq, int no_cck,
6912 int offchanok,
6913 unsigned int wait_time)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006914{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006915 struct wpa_driver_nl80211_data *drv = bss->drv;
6916 struct ieee80211_mgmt *mgmt;
6917 int encrypt = 1;
6918 u16 fc;
6919
6920 mgmt = (struct ieee80211_mgmt *) data;
6921 fc = le_to_host16(mgmt->frame_control);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006922 wpa_printf(MSG_DEBUG, "nl80211: send_mlme - noack=%d freq=%u no_cck=%d offchanok=%d wait_time=%u fc=0x%x nlmode=%d",
6923 noack, freq, no_cck, offchanok, wait_time, fc, drv->nlmode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006924
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006925 if ((is_sta_interface(drv->nlmode) ||
6926 drv->nlmode == NL80211_IFTYPE_P2P_DEVICE) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006927 WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
6928 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_RESP) {
6929 /*
6930 * The use of last_mgmt_freq is a bit of a hack,
6931 * but it works due to the single-threaded nature
6932 * of wpa_supplicant.
6933 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07006934 if (freq == 0) {
6935 wpa_printf(MSG_DEBUG, "nl80211: Use last_mgmt_freq=%d",
6936 drv->last_mgmt_freq);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006937 freq = drv->last_mgmt_freq;
Dmitry Shmidt56052862013-10-04 10:23:25 -07006938 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006939 return nl80211_send_frame_cmd(bss, freq, 0,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006940 data, data_len, NULL, 1, noack,
6941 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006942 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006943
6944 if (drv->device_ap_sme && is_ap_interface(drv->nlmode)) {
Dmitry Shmidt56052862013-10-04 10:23:25 -07006945 if (freq == 0) {
6946 wpa_printf(MSG_DEBUG, "nl80211: Use bss->freq=%d",
6947 bss->freq);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006948 freq = bss->freq;
Dmitry Shmidt56052862013-10-04 10:23:25 -07006949 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07006950 return nl80211_send_frame_cmd(bss, freq,
6951 (int) freq == bss->freq ? 0 :
6952 wait_time,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006953 data, data_len,
6954 &drv->send_action_cookie,
6955 no_cck, noack, offchanok);
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07006956 }
Dmitry Shmidtb638fe72012-03-20 12:51:25 -07006957
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006958 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
6959 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) {
6960 /*
6961 * Only one of the authentication frame types is encrypted.
6962 * In order for static WEP encryption to work properly (i.e.,
6963 * to not encrypt the frame), we need to tell mac80211 about
6964 * the frames that must not be encrypted.
6965 */
6966 u16 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
6967 u16 auth_trans = le_to_host16(mgmt->u.auth.auth_transaction);
6968 if (auth_alg != WLAN_AUTH_SHARED_KEY || auth_trans != 3)
6969 encrypt = 0;
6970 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006971
Dmitry Shmidt56052862013-10-04 10:23:25 -07006972 wpa_printf(MSG_DEBUG, "nl80211: send_mlme -> send_frame");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006973 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006974 noack, freq, no_cck, offchanok,
6975 wait_time);
6976}
6977
6978
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006979static int nl80211_set_bss(struct i802_bss *bss, int cts, int preamble,
6980 int slot, int ht_opmode, int ap_isolate,
6981 int *basic_rates)
6982{
6983 struct wpa_driver_nl80211_data *drv = bss->drv;
6984 struct nl_msg *msg;
6985
6986 msg = nlmsg_alloc();
6987 if (!msg)
6988 return -ENOMEM;
6989
6990 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_BSS);
6991
6992 if (cts >= 0)
6993 NLA_PUT_U8(msg, NL80211_ATTR_BSS_CTS_PROT, cts);
6994 if (preamble >= 0)
6995 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble);
6996 if (slot >= 0)
6997 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot);
6998 if (ht_opmode >= 0)
6999 NLA_PUT_U16(msg, NL80211_ATTR_BSS_HT_OPMODE, ht_opmode);
7000 if (ap_isolate >= 0)
7001 NLA_PUT_U8(msg, NL80211_ATTR_AP_ISOLATE, ap_isolate);
7002
7003 if (basic_rates) {
7004 u8 rates[NL80211_MAX_SUPP_RATES];
7005 u8 rates_len = 0;
7006 int i;
7007
7008 for (i = 0; i < NL80211_MAX_SUPP_RATES && basic_rates[i] >= 0;
7009 i++)
7010 rates[rates_len++] = basic_rates[i] / 5;
7011
7012 NLA_PUT(msg, NL80211_ATTR_BSS_BASIC_RATES, rates_len, rates);
7013 }
7014
7015 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
7016
7017 return send_and_recv_msgs(drv, msg, NULL, NULL);
7018 nla_put_failure:
7019 nlmsg_free(msg);
7020 return -ENOBUFS;
7021}
7022
7023
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07007024static int wpa_driver_nl80211_set_acl(void *priv,
7025 struct hostapd_acl_params *params)
7026{
7027 struct i802_bss *bss = priv;
7028 struct wpa_driver_nl80211_data *drv = bss->drv;
7029 struct nl_msg *msg;
7030 struct nlattr *acl;
7031 unsigned int i;
7032 int ret = 0;
7033
7034 if (!(drv->capa.max_acl_mac_addrs))
7035 return -ENOTSUP;
7036
7037 if (params->num_mac_acl > drv->capa.max_acl_mac_addrs)
7038 return -ENOTSUP;
7039
7040 msg = nlmsg_alloc();
7041 if (!msg)
7042 return -ENOMEM;
7043
7044 wpa_printf(MSG_DEBUG, "nl80211: Set %s ACL (num_mac_acl=%u)",
7045 params->acl_policy ? "Accept" : "Deny", params->num_mac_acl);
7046
7047 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_MAC_ACL);
7048
7049 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
7050
7051 NLA_PUT_U32(msg, NL80211_ATTR_ACL_POLICY, params->acl_policy ?
7052 NL80211_ACL_POLICY_DENY_UNLESS_LISTED :
7053 NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED);
7054
7055 acl = nla_nest_start(msg, NL80211_ATTR_MAC_ADDRS);
7056 if (acl == NULL)
7057 goto nla_put_failure;
7058
7059 for (i = 0; i < params->num_mac_acl; i++)
7060 NLA_PUT(msg, i + 1, ETH_ALEN, params->mac_acl[i].addr);
7061
7062 nla_nest_end(msg, acl);
7063
7064 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7065 msg = NULL;
7066 if (ret) {
7067 wpa_printf(MSG_DEBUG, "nl80211: Failed to set MAC ACL: %d (%s)",
7068 ret, strerror(-ret));
7069 }
7070
7071nla_put_failure:
7072 nlmsg_free(msg);
7073
7074 return ret;
7075}
7076
7077
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007078static int wpa_driver_nl80211_set_ap(void *priv,
7079 struct wpa_driver_ap_params *params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007080{
7081 struct i802_bss *bss = priv;
7082 struct wpa_driver_nl80211_data *drv = bss->drv;
7083 struct nl_msg *msg;
7084 u8 cmd = NL80211_CMD_NEW_BEACON;
7085 int ret;
7086 int beacon_set;
7087 int ifindex = if_nametoindex(bss->ifname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007088 int num_suites;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007089 u32 suites[10], suite;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007090 u32 ver;
7091
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07007092 beacon_set = bss->beacon_set;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007093
7094 msg = nlmsg_alloc();
7095 if (!msg)
7096 return -ENOMEM;
7097
7098 wpa_printf(MSG_DEBUG, "nl80211: Set beacon (beacon_set=%d)",
7099 beacon_set);
7100 if (beacon_set)
7101 cmd = NL80211_CMD_SET_BEACON;
7102
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007103 nl80211_cmd(drv, msg, 0, cmd);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007104 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon head",
7105 params->head, params->head_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007106 NLA_PUT(msg, NL80211_ATTR_BEACON_HEAD, params->head_len, params->head);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007107 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon tail",
7108 params->tail, params->tail_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007109 NLA_PUT(msg, NL80211_ATTR_BEACON_TAIL, params->tail_len, params->tail);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007110 wpa_printf(MSG_DEBUG, "nl80211: ifindex=%d", ifindex);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007111 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007112 wpa_printf(MSG_DEBUG, "nl80211: beacon_int=%d", params->beacon_int);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007113 NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL, params->beacon_int);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007114 wpa_printf(MSG_DEBUG, "nl80211: dtim_period=%d", params->dtim_period);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007115 NLA_PUT_U32(msg, NL80211_ATTR_DTIM_PERIOD, params->dtim_period);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007116 wpa_hexdump_ascii(MSG_DEBUG, "nl80211: ssid",
7117 params->ssid, params->ssid_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007118 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
7119 params->ssid);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007120 if (params->proberesp && params->proberesp_len) {
7121 wpa_hexdump(MSG_DEBUG, "nl80211: proberesp (offload)",
7122 params->proberesp, params->proberesp_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007123 NLA_PUT(msg, NL80211_ATTR_PROBE_RESP, params->proberesp_len,
7124 params->proberesp);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007125 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007126 switch (params->hide_ssid) {
7127 case NO_SSID_HIDING:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007128 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID not in use");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007129 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
7130 NL80211_HIDDEN_SSID_NOT_IN_USE);
7131 break;
7132 case HIDDEN_SSID_ZERO_LEN:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007133 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero len");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007134 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
7135 NL80211_HIDDEN_SSID_ZERO_LEN);
7136 break;
7137 case HIDDEN_SSID_ZERO_CONTENTS:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007138 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero contents");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007139 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
7140 NL80211_HIDDEN_SSID_ZERO_CONTENTS);
7141 break;
7142 }
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007143 wpa_printf(MSG_DEBUG, "nl80211: privacy=%d", params->privacy);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007144 if (params->privacy)
7145 NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007146 wpa_printf(MSG_DEBUG, "nl80211: auth_algs=0x%x", params->auth_algs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007147 if ((params->auth_algs & (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) ==
7148 (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) {
7149 /* Leave out the attribute */
7150 } else if (params->auth_algs & WPA_AUTH_ALG_SHARED)
7151 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE,
7152 NL80211_AUTHTYPE_SHARED_KEY);
7153 else
7154 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE,
7155 NL80211_AUTHTYPE_OPEN_SYSTEM);
7156
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007157 wpa_printf(MSG_DEBUG, "nl80211: wpa_version=0x%x", params->wpa_version);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007158 ver = 0;
7159 if (params->wpa_version & WPA_PROTO_WPA)
7160 ver |= NL80211_WPA_VERSION_1;
7161 if (params->wpa_version & WPA_PROTO_RSN)
7162 ver |= NL80211_WPA_VERSION_2;
7163 if (ver)
7164 NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver);
7165
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007166 wpa_printf(MSG_DEBUG, "nl80211: key_mgmt_suites=0x%x",
7167 params->key_mgmt_suites);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007168 num_suites = 0;
7169 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X)
7170 suites[num_suites++] = WLAN_AKM_SUITE_8021X;
7171 if (params->key_mgmt_suites & WPA_KEY_MGMT_PSK)
7172 suites[num_suites++] = WLAN_AKM_SUITE_PSK;
7173 if (num_suites) {
7174 NLA_PUT(msg, NL80211_ATTR_AKM_SUITES,
7175 num_suites * sizeof(u32), suites);
7176 }
7177
7178 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X &&
7179 params->pairwise_ciphers & (WPA_CIPHER_WEP104 | WPA_CIPHER_WEP40))
7180 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT);
7181
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007182 wpa_printf(MSG_DEBUG, "nl80211: pairwise_ciphers=0x%x",
7183 params->pairwise_ciphers);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007184 num_suites = wpa_cipher_to_cipher_suites(params->pairwise_ciphers,
7185 suites, ARRAY_SIZE(suites));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007186 if (num_suites) {
7187 NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
7188 num_suites * sizeof(u32), suites);
7189 }
7190
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007191 wpa_printf(MSG_DEBUG, "nl80211: group_cipher=0x%x",
7192 params->group_cipher);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007193 suite = wpa_cipher_to_cipher_suite(params->group_cipher);
7194 if (suite)
7195 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, suite);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007196
7197 if (params->beacon_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007198 wpa_hexdump_buf(MSG_DEBUG, "nl80211: beacon_ies",
7199 params->beacon_ies);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007200 NLA_PUT(msg, NL80211_ATTR_IE, wpabuf_len(params->beacon_ies),
7201 wpabuf_head(params->beacon_ies));
7202 }
7203 if (params->proberesp_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007204 wpa_hexdump_buf(MSG_DEBUG, "nl80211: proberesp_ies",
7205 params->proberesp_ies);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007206 NLA_PUT(msg, NL80211_ATTR_IE_PROBE_RESP,
7207 wpabuf_len(params->proberesp_ies),
7208 wpabuf_head(params->proberesp_ies));
7209 }
7210 if (params->assocresp_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007211 wpa_hexdump_buf(MSG_DEBUG, "nl80211: assocresp_ies",
7212 params->assocresp_ies);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007213 NLA_PUT(msg, NL80211_ATTR_IE_ASSOC_RESP,
7214 wpabuf_len(params->assocresp_ies),
7215 wpabuf_head(params->assocresp_ies));
7216 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007217
Dmitry Shmidt04949592012-07-19 12:16:46 -07007218 if (drv->capa.flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007219 wpa_printf(MSG_DEBUG, "nl80211: ap_max_inactivity=%d",
7220 params->ap_max_inactivity);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007221 NLA_PUT_U16(msg, NL80211_ATTR_INACTIVITY_TIMEOUT,
7222 params->ap_max_inactivity);
7223 }
7224
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007225 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7226 if (ret) {
7227 wpa_printf(MSG_DEBUG, "nl80211: Beacon set failed: %d (%s)",
7228 ret, strerror(-ret));
7229 } else {
7230 bss->beacon_set = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007231 nl80211_set_bss(bss, params->cts_protect, params->preamble,
7232 params->short_slot_time, params->ht_opmode,
7233 params->isolate, params->basic_rates);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007234 }
7235 return ret;
7236 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007237 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007238 return -ENOBUFS;
7239}
7240
7241
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007242static int nl80211_put_freq_params(struct nl_msg *msg,
7243 struct hostapd_freq_params *freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007244{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007245 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq->freq);
7246 if (freq->vht_enabled) {
7247 switch (freq->bandwidth) {
7248 case 20:
7249 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
7250 NL80211_CHAN_WIDTH_20);
7251 break;
7252 case 40:
7253 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
7254 NL80211_CHAN_WIDTH_40);
7255 break;
7256 case 80:
7257 if (freq->center_freq2)
7258 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
7259 NL80211_CHAN_WIDTH_80P80);
7260 else
7261 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
7262 NL80211_CHAN_WIDTH_80);
7263 break;
7264 case 160:
7265 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
7266 NL80211_CHAN_WIDTH_160);
7267 break;
7268 default:
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007269 return -EINVAL;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007270 }
7271 NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ1, freq->center_freq1);
7272 if (freq->center_freq2)
7273 NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ2,
7274 freq->center_freq2);
7275 } else if (freq->ht_enabled) {
7276 switch (freq->sec_channel_offset) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007277 case -1:
7278 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
7279 NL80211_CHAN_HT40MINUS);
7280 break;
7281 case 1:
7282 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
7283 NL80211_CHAN_HT40PLUS);
7284 break;
7285 default:
7286 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
7287 NL80211_CHAN_HT20);
7288 break;
7289 }
7290 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007291 return 0;
7292
7293nla_put_failure:
7294 return -ENOBUFS;
7295}
7296
7297
7298static int wpa_driver_nl80211_set_freq(struct i802_bss *bss,
7299 struct hostapd_freq_params *freq)
7300{
7301 struct wpa_driver_nl80211_data *drv = bss->drv;
7302 struct nl_msg *msg;
7303 int ret;
7304
7305 wpa_printf(MSG_DEBUG,
7306 "nl80211: Set freq %d (ht_enabled=%d, vht_enabled=%d, bandwidth=%d MHz, cf1=%d MHz, cf2=%d MHz)",
7307 freq->freq, freq->ht_enabled, freq->vht_enabled,
7308 freq->bandwidth, freq->center_freq1, freq->center_freq2);
7309 msg = nlmsg_alloc();
7310 if (!msg)
7311 return -1;
7312
7313 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
7314
7315 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
7316 if (nl80211_put_freq_params(msg, freq) < 0)
7317 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007318
7319 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007320 msg = NULL;
7321 if (ret == 0) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007322 bss->freq = freq->freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007323 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007324 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007325 wpa_printf(MSG_DEBUG, "nl80211: Failed to set channel (freq=%d): "
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007326 "%d (%s)", freq->freq, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007327nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007328 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007329 return -1;
7330}
7331
7332
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007333static u32 sta_flags_nl80211(int flags)
7334{
7335 u32 f = 0;
7336
7337 if (flags & WPA_STA_AUTHORIZED)
7338 f |= BIT(NL80211_STA_FLAG_AUTHORIZED);
7339 if (flags & WPA_STA_WMM)
7340 f |= BIT(NL80211_STA_FLAG_WME);
7341 if (flags & WPA_STA_SHORT_PREAMBLE)
7342 f |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
7343 if (flags & WPA_STA_MFP)
7344 f |= BIT(NL80211_STA_FLAG_MFP);
7345 if (flags & WPA_STA_TDLS_PEER)
7346 f |= BIT(NL80211_STA_FLAG_TDLS_PEER);
7347
7348 return f;
7349}
7350
7351
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007352static int wpa_driver_nl80211_sta_add(void *priv,
7353 struct hostapd_sta_add_params *params)
7354{
7355 struct i802_bss *bss = priv;
7356 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007357 struct nl_msg *msg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007358 struct nl80211_sta_flag_update upd;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007359 int ret = -ENOBUFS;
7360
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007361 if ((params->flags & WPA_STA_TDLS_PEER) &&
7362 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
7363 return -EOPNOTSUPP;
7364
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007365 msg = nlmsg_alloc();
7366 if (!msg)
7367 return -ENOMEM;
7368
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007369 wpa_printf(MSG_DEBUG, "nl80211: %s STA " MACSTR,
7370 params->set ? "Set" : "Add", MAC2STR(params->addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007371 nl80211_cmd(drv, msg, 0, params->set ? NL80211_CMD_SET_STATION :
7372 NL80211_CMD_NEW_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007373
7374 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
7375 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007376 NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_RATES, params->supp_rates_len,
7377 params->supp_rates);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007378 wpa_hexdump(MSG_DEBUG, " * supported rates", params->supp_rates,
7379 params->supp_rates_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007380 if (!params->set) {
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07007381 if (params->aid) {
7382 wpa_printf(MSG_DEBUG, " * aid=%u", params->aid);
7383 NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, params->aid);
7384 } else {
7385 /*
7386 * cfg80211 validates that AID is non-zero, so we have
7387 * to make this a non-zero value for the TDLS case where
7388 * a dummy STA entry is used for now.
7389 */
7390 wpa_printf(MSG_DEBUG, " * aid=1 (TDLS workaround)");
7391 NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, 1);
7392 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007393 wpa_printf(MSG_DEBUG, " * listen_interval=%u",
7394 params->listen_interval);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007395 NLA_PUT_U16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
7396 params->listen_interval);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07007397 } else if (params->aid && (params->flags & WPA_STA_TDLS_PEER)) {
7398 wpa_printf(MSG_DEBUG, " * peer_aid=%u", params->aid);
7399 NLA_PUT_U16(msg, NL80211_ATTR_PEER_AID, params->aid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007400 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007401 if (params->ht_capabilities) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007402 wpa_hexdump(MSG_DEBUG, " * ht_capabilities",
7403 (u8 *) params->ht_capabilities,
7404 sizeof(*params->ht_capabilities));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007405 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY,
7406 sizeof(*params->ht_capabilities),
7407 params->ht_capabilities);
7408 }
7409
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007410 if (params->vht_capabilities) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007411 wpa_hexdump(MSG_DEBUG, " * vht_capabilities",
7412 (u8 *) params->vht_capabilities,
7413 sizeof(*params->vht_capabilities));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007414 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY,
7415 sizeof(*params->vht_capabilities),
7416 params->vht_capabilities);
7417 }
7418
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08007419 if (params->vht_opmode_enabled) {
7420 wpa_printf(MSG_DEBUG, " * opmode=%u", params->vht_opmode);
7421 NLA_PUT_U8(msg, NL80211_ATTR_OPMODE_NOTIF,
7422 params->vht_opmode);
7423 }
7424
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007425 wpa_printf(MSG_DEBUG, " * capability=0x%x", params->capability);
7426 NLA_PUT_U16(msg, NL80211_ATTR_STA_CAPABILITY, params->capability);
7427
7428 if (params->ext_capab) {
7429 wpa_hexdump(MSG_DEBUG, " * ext_capab",
7430 params->ext_capab, params->ext_capab_len);
7431 NLA_PUT(msg, NL80211_ATTR_STA_EXT_CAPABILITY,
7432 params->ext_capab_len, params->ext_capab);
7433 }
7434
Dmitry Shmidt344abd32014-01-14 13:17:00 -08007435 if (params->supp_channels) {
7436 wpa_hexdump(MSG_DEBUG, " * supported channels",
7437 params->supp_channels, params->supp_channels_len);
7438 NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_CHANNELS,
7439 params->supp_channels_len, params->supp_channels);
7440 }
7441
7442 if (params->supp_oper_classes) {
7443 wpa_hexdump(MSG_DEBUG, " * supported operating classes",
7444 params->supp_oper_classes,
7445 params->supp_oper_classes_len);
7446 NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES,
7447 params->supp_oper_classes_len,
7448 params->supp_oper_classes);
7449 }
7450
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007451 os_memset(&upd, 0, sizeof(upd));
7452 upd.mask = sta_flags_nl80211(params->flags);
7453 upd.set = upd.mask;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007454 wpa_printf(MSG_DEBUG, " * flags set=0x%x mask=0x%x",
7455 upd.set, upd.mask);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007456 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
7457
7458 if (params->flags & WPA_STA_WMM) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007459 struct nlattr *wme = nla_nest_start(msg, NL80211_ATTR_STA_WME);
7460
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007461 if (!wme)
7462 goto nla_put_failure;
7463
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007464 wpa_printf(MSG_DEBUG, " * qosinfo=0x%x", params->qosinfo);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007465 NLA_PUT_U8(msg, NL80211_STA_WME_UAPSD_QUEUES,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007466 params->qosinfo & WMM_QOSINFO_STA_AC_MASK);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007467 NLA_PUT_U8(msg, NL80211_STA_WME_MAX_SP,
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007468 (params->qosinfo >> WMM_QOSINFO_STA_SP_SHIFT) &
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007469 WMM_QOSINFO_STA_SP_MASK);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007470 nla_nest_end(msg, wme);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007471 }
7472
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007473 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007474 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007475 if (ret)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007476 wpa_printf(MSG_DEBUG, "nl80211: NL80211_CMD_%s_STATION "
7477 "result: %d (%s)", params->set ? "SET" : "NEW", ret,
7478 strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007479 if (ret == -EEXIST)
7480 ret = 0;
7481 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007482 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007483 return ret;
7484}
7485
7486
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007487static int wpa_driver_nl80211_sta_remove(struct i802_bss *bss, const u8 *addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007488{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007489 struct wpa_driver_nl80211_data *drv = bss->drv;
7490 struct nl_msg *msg;
7491 int ret;
7492
7493 msg = nlmsg_alloc();
7494 if (!msg)
7495 return -ENOMEM;
7496
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007497 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007498
7499 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
7500 if_nametoindex(bss->ifname));
7501 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
7502
7503 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007504 wpa_printf(MSG_DEBUG, "nl80211: sta_remove -> DEL_STATION %s " MACSTR
7505 " --> %d (%s)",
7506 bss->ifname, MAC2STR(addr), ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007507 if (ret == -ENOENT)
7508 return 0;
7509 return ret;
7510 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007511 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007512 return -ENOBUFS;
7513}
7514
7515
7516static void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv,
7517 int ifidx)
7518{
7519 struct nl_msg *msg;
7520
7521 wpa_printf(MSG_DEBUG, "nl80211: Remove interface ifindex=%d", ifidx);
7522
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007523 /* stop listening for EAPOL on this interface */
7524 del_ifidx(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007525
7526 msg = nlmsg_alloc();
7527 if (!msg)
7528 goto nla_put_failure;
7529
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007530 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_INTERFACE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007531 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifidx);
7532
7533 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
7534 return;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007535 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007536 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007537 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007538 wpa_printf(MSG_ERROR, "Failed to remove interface (ifidx=%d)", ifidx);
7539}
7540
7541
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007542static const char * nl80211_iftype_str(enum nl80211_iftype mode)
7543{
7544 switch (mode) {
7545 case NL80211_IFTYPE_ADHOC:
7546 return "ADHOC";
7547 case NL80211_IFTYPE_STATION:
7548 return "STATION";
7549 case NL80211_IFTYPE_AP:
7550 return "AP";
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07007551 case NL80211_IFTYPE_AP_VLAN:
7552 return "AP_VLAN";
7553 case NL80211_IFTYPE_WDS:
7554 return "WDS";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007555 case NL80211_IFTYPE_MONITOR:
7556 return "MONITOR";
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07007557 case NL80211_IFTYPE_MESH_POINT:
7558 return "MESH_POINT";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007559 case NL80211_IFTYPE_P2P_CLIENT:
7560 return "P2P_CLIENT";
7561 case NL80211_IFTYPE_P2P_GO:
7562 return "P2P_GO";
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007563 case NL80211_IFTYPE_P2P_DEVICE:
7564 return "P2P_DEVICE";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007565 default:
7566 return "unknown";
7567 }
7568}
7569
7570
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007571static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv,
7572 const char *ifname,
7573 enum nl80211_iftype iftype,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007574 const u8 *addr, int wds,
7575 int (*handler)(struct nl_msg *, void *),
7576 void *arg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007577{
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007578 struct nl_msg *msg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007579 int ifidx;
7580 int ret = -ENOBUFS;
7581
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007582 wpa_printf(MSG_DEBUG, "nl80211: Create interface iftype %d (%s)",
7583 iftype, nl80211_iftype_str(iftype));
7584
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007585 msg = nlmsg_alloc();
7586 if (!msg)
7587 return -1;
7588
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007589 nl80211_cmd(drv, msg, 0, NL80211_CMD_NEW_INTERFACE);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007590 if (nl80211_set_iface_id(msg, drv->first_bss) < 0)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007591 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007592 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, ifname);
7593 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, iftype);
7594
7595 if (iftype == NL80211_IFTYPE_MONITOR) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007596 struct nlattr *flags;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007597
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007598 flags = nla_nest_start(msg, NL80211_ATTR_MNTR_FLAGS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007599 if (!flags)
7600 goto nla_put_failure;
7601
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007602 NLA_PUT_FLAG(msg, NL80211_MNTR_FLAG_COOK_FRAMES);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007603
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007604 nla_nest_end(msg, flags);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007605 } else if (wds) {
7606 NLA_PUT_U8(msg, NL80211_ATTR_4ADDR, wds);
7607 }
7608
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007609 ret = send_and_recv_msgs(drv, msg, handler, arg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007610 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007611 if (ret) {
7612 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007613 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007614 wpa_printf(MSG_ERROR, "Failed to create interface %s: %d (%s)",
7615 ifname, ret, strerror(-ret));
7616 return ret;
7617 }
7618
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007619 if (iftype == NL80211_IFTYPE_P2P_DEVICE)
7620 return 0;
7621
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007622 ifidx = if_nametoindex(ifname);
7623 wpa_printf(MSG_DEBUG, "nl80211: New interface %s created: ifindex=%d",
7624 ifname, ifidx);
7625
7626 if (ifidx <= 0)
7627 return -1;
7628
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007629 /* start listening for EAPOL on this interface */
7630 add_ifidx(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007631
7632 if (addr && iftype != NL80211_IFTYPE_MONITOR &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007633 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname, addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007634 nl80211_remove_iface(drv, ifidx);
7635 return -1;
7636 }
7637
7638 return ifidx;
7639}
7640
7641
7642static int nl80211_create_iface(struct wpa_driver_nl80211_data *drv,
7643 const char *ifname, enum nl80211_iftype iftype,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007644 const u8 *addr, int wds,
7645 int (*handler)(struct nl_msg *, void *),
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007646 void *arg, int use_existing)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007647{
7648 int ret;
7649
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007650 ret = nl80211_create_iface_once(drv, ifname, iftype, addr, wds, handler,
7651 arg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007652
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007653 /* if error occurred and interface exists already */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007654 if (ret == -ENFILE && if_nametoindex(ifname)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007655 if (use_existing) {
7656 wpa_printf(MSG_DEBUG, "nl80211: Continue using existing interface %s",
7657 ifname);
7658 return -ENFILE;
7659 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007660 wpa_printf(MSG_INFO, "Try to remove and re-create %s", ifname);
7661
7662 /* Try to remove the interface that was already there. */
7663 nl80211_remove_iface(drv, if_nametoindex(ifname));
7664
7665 /* Try to create the interface again */
7666 ret = nl80211_create_iface_once(drv, ifname, iftype, addr,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007667 wds, handler, arg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007668 }
7669
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007670 if (ret >= 0 && is_p2p_net_interface(iftype))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007671 nl80211_disable_11b_rates(drv, ret, 1);
7672
7673 return ret;
7674}
7675
7676
7677static void handle_tx_callback(void *ctx, u8 *buf, size_t len, int ok)
7678{
7679 struct ieee80211_hdr *hdr;
7680 u16 fc;
7681 union wpa_event_data event;
7682
7683 hdr = (struct ieee80211_hdr *) buf;
7684 fc = le_to_host16(hdr->frame_control);
7685
7686 os_memset(&event, 0, sizeof(event));
7687 event.tx_status.type = WLAN_FC_GET_TYPE(fc);
7688 event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
7689 event.tx_status.dst = hdr->addr1;
7690 event.tx_status.data = buf;
7691 event.tx_status.data_len = len;
7692 event.tx_status.ack = ok;
7693 wpa_supplicant_event(ctx, EVENT_TX_STATUS, &event);
7694}
7695
7696
7697static void from_unknown_sta(struct wpa_driver_nl80211_data *drv,
7698 u8 *buf, size_t len)
7699{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007700 struct ieee80211_hdr *hdr = (void *)buf;
7701 u16 fc;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007702 union wpa_event_data event;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007703
7704 if (len < sizeof(*hdr))
7705 return;
7706
7707 fc = le_to_host16(hdr->frame_control);
7708
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007709 os_memset(&event, 0, sizeof(event));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007710 event.rx_from_unknown.bssid = get_hdr_bssid(hdr, len);
7711 event.rx_from_unknown.addr = hdr->addr2;
7712 event.rx_from_unknown.wds = (fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) ==
7713 (WLAN_FC_FROMDS | WLAN_FC_TODS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007714 wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event);
7715}
7716
7717
7718static void handle_frame(struct wpa_driver_nl80211_data *drv,
7719 u8 *buf, size_t len, int datarate, int ssi_signal)
7720{
7721 struct ieee80211_hdr *hdr;
7722 u16 fc;
7723 union wpa_event_data event;
7724
7725 hdr = (struct ieee80211_hdr *) buf;
7726 fc = le_to_host16(hdr->frame_control);
7727
7728 switch (WLAN_FC_GET_TYPE(fc)) {
7729 case WLAN_FC_TYPE_MGMT:
7730 os_memset(&event, 0, sizeof(event));
7731 event.rx_mgmt.frame = buf;
7732 event.rx_mgmt.frame_len = len;
7733 event.rx_mgmt.datarate = datarate;
7734 event.rx_mgmt.ssi_signal = ssi_signal;
7735 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
7736 break;
7737 case WLAN_FC_TYPE_CTRL:
7738 /* can only get here with PS-Poll frames */
7739 wpa_printf(MSG_DEBUG, "CTRL");
7740 from_unknown_sta(drv, buf, len);
7741 break;
7742 case WLAN_FC_TYPE_DATA:
7743 from_unknown_sta(drv, buf, len);
7744 break;
7745 }
7746}
7747
7748
7749static void handle_monitor_read(int sock, void *eloop_ctx, void *sock_ctx)
7750{
7751 struct wpa_driver_nl80211_data *drv = eloop_ctx;
7752 int len;
7753 unsigned char buf[3000];
7754 struct ieee80211_radiotap_iterator iter;
7755 int ret;
7756 int datarate = 0, ssi_signal = 0;
7757 int injected = 0, failed = 0, rxflags = 0;
7758
7759 len = recv(sock, buf, sizeof(buf), 0);
7760 if (len < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007761 wpa_printf(MSG_ERROR, "nl80211: Monitor socket recv failed: %s",
7762 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007763 return;
7764 }
7765
7766 if (ieee80211_radiotap_iterator_init(&iter, (void*)buf, len)) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007767 wpa_printf(MSG_INFO, "nl80211: received invalid radiotap frame");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007768 return;
7769 }
7770
7771 while (1) {
7772 ret = ieee80211_radiotap_iterator_next(&iter);
7773 if (ret == -ENOENT)
7774 break;
7775 if (ret) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007776 wpa_printf(MSG_INFO, "nl80211: received invalid radiotap frame (%d)",
7777 ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007778 return;
7779 }
7780 switch (iter.this_arg_index) {
7781 case IEEE80211_RADIOTAP_FLAGS:
7782 if (*iter.this_arg & IEEE80211_RADIOTAP_F_FCS)
7783 len -= 4;
7784 break;
7785 case IEEE80211_RADIOTAP_RX_FLAGS:
7786 rxflags = 1;
7787 break;
7788 case IEEE80211_RADIOTAP_TX_FLAGS:
7789 injected = 1;
7790 failed = le_to_host16((*(uint16_t *) iter.this_arg)) &
7791 IEEE80211_RADIOTAP_F_TX_FAIL;
7792 break;
7793 case IEEE80211_RADIOTAP_DATA_RETRIES:
7794 break;
7795 case IEEE80211_RADIOTAP_CHANNEL:
7796 /* TODO: convert from freq/flags to channel number */
7797 break;
7798 case IEEE80211_RADIOTAP_RATE:
7799 datarate = *iter.this_arg * 5;
7800 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007801 case IEEE80211_RADIOTAP_DBM_ANTSIGNAL:
7802 ssi_signal = (s8) *iter.this_arg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007803 break;
7804 }
7805 }
7806
7807 if (rxflags && injected)
7808 return;
7809
7810 if (!injected)
7811 handle_frame(drv, buf + iter.max_length,
7812 len - iter.max_length, datarate, ssi_signal);
7813 else
7814 handle_tx_callback(drv->ctx, buf + iter.max_length,
7815 len - iter.max_length, !failed);
7816}
7817
7818
7819/*
7820 * we post-process the filter code later and rewrite
7821 * this to the offset to the last instruction
7822 */
7823#define PASS 0xFF
7824#define FAIL 0xFE
7825
7826static struct sock_filter msock_filter_insns[] = {
7827 /*
7828 * do a little-endian load of the radiotap length field
7829 */
7830 /* load lower byte into A */
7831 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 2),
7832 /* put it into X (== index register) */
7833 BPF_STMT(BPF_MISC| BPF_TAX, 0),
7834 /* load upper byte into A */
7835 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 3),
7836 /* left-shift it by 8 */
7837 BPF_STMT(BPF_ALU | BPF_LSH | BPF_K, 8),
7838 /* or with X */
7839 BPF_STMT(BPF_ALU | BPF_OR | BPF_X, 0),
7840 /* put result into X */
7841 BPF_STMT(BPF_MISC| BPF_TAX, 0),
7842
7843 /*
7844 * Allow management frames through, this also gives us those
7845 * management frames that we sent ourselves with status
7846 */
7847 /* load the lower byte of the IEEE 802.11 frame control field */
7848 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
7849 /* mask off frame type and version */
7850 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0xF),
7851 /* accept frame if it's both 0, fall through otherwise */
7852 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0, PASS, 0),
7853
7854 /*
7855 * TODO: add a bit to radiotap RX flags that indicates
7856 * that the sending station is not associated, then
7857 * add a filter here that filters on our DA and that flag
7858 * to allow us to deauth frames to that bad station.
7859 *
7860 * For now allow all To DS data frames through.
7861 */
7862 /* load the IEEE 802.11 frame control field */
7863 BPF_STMT(BPF_LD | BPF_H | BPF_IND, 0),
7864 /* mask off frame type, version and DS status */
7865 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0F03),
7866 /* accept frame if version 0, type 2 and To DS, fall through otherwise
7867 */
7868 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0801, PASS, 0),
7869
7870#if 0
7871 /*
7872 * drop non-data frames
7873 */
7874 /* load the lower byte of the frame control field */
7875 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
7876 /* mask off QoS bit */
7877 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0c),
7878 /* drop non-data frames */
7879 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 8, 0, FAIL),
7880#endif
7881 /* load the upper byte of the frame control field */
7882 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 1),
7883 /* mask off toDS/fromDS */
7884 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x03),
7885 /* accept WDS frames */
7886 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 3, PASS, 0),
7887
7888 /*
7889 * add header length to index
7890 */
7891 /* load the lower byte of the frame control field */
7892 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
7893 /* mask off QoS bit */
7894 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x80),
7895 /* right shift it by 6 to give 0 or 2 */
7896 BPF_STMT(BPF_ALU | BPF_RSH | BPF_K, 6),
7897 /* add data frame header length */
7898 BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 24),
7899 /* add index, was start of 802.11 header */
7900 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
7901 /* move to index, now start of LL header */
7902 BPF_STMT(BPF_MISC | BPF_TAX, 0),
7903
7904 /*
7905 * Accept empty data frames, we use those for
7906 * polling activity.
7907 */
7908 BPF_STMT(BPF_LD | BPF_W | BPF_LEN, 0),
7909 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_X, 0, PASS, 0),
7910
7911 /*
7912 * Accept EAPOL frames
7913 */
7914 BPF_STMT(BPF_LD | BPF_W | BPF_IND, 0),
7915 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0xAAAA0300, 0, FAIL),
7916 BPF_STMT(BPF_LD | BPF_W | BPF_IND, 4),
7917 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0000888E, PASS, FAIL),
7918
7919 /* keep these last two statements or change the code below */
7920 /* return 0 == "DROP" */
7921 BPF_STMT(BPF_RET | BPF_K, 0),
7922 /* return ~0 == "keep all" */
7923 BPF_STMT(BPF_RET | BPF_K, ~0),
7924};
7925
7926static struct sock_fprog msock_filter = {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007927 .len = ARRAY_SIZE(msock_filter_insns),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007928 .filter = msock_filter_insns,
7929};
7930
7931
7932static int add_monitor_filter(int s)
7933{
7934 int idx;
7935
7936 /* rewrite all PASS/FAIL jump offsets */
7937 for (idx = 0; idx < msock_filter.len; idx++) {
7938 struct sock_filter *insn = &msock_filter_insns[idx];
7939
7940 if (BPF_CLASS(insn->code) == BPF_JMP) {
7941 if (insn->code == (BPF_JMP|BPF_JA)) {
7942 if (insn->k == PASS)
7943 insn->k = msock_filter.len - idx - 2;
7944 else if (insn->k == FAIL)
7945 insn->k = msock_filter.len - idx - 3;
7946 }
7947
7948 if (insn->jt == PASS)
7949 insn->jt = msock_filter.len - idx - 2;
7950 else if (insn->jt == FAIL)
7951 insn->jt = msock_filter.len - idx - 3;
7952
7953 if (insn->jf == PASS)
7954 insn->jf = msock_filter.len - idx - 2;
7955 else if (insn->jf == FAIL)
7956 insn->jf = msock_filter.len - idx - 3;
7957 }
7958 }
7959
7960 if (setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER,
7961 &msock_filter, sizeof(msock_filter))) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007962 wpa_printf(MSG_ERROR, "nl80211: setsockopt(SO_ATTACH_FILTER) failed: %s",
7963 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007964 return -1;
7965 }
7966
7967 return 0;
7968}
7969
7970
7971static void nl80211_remove_monitor_interface(
7972 struct wpa_driver_nl80211_data *drv)
7973{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007974 if (drv->monitor_refcount > 0)
7975 drv->monitor_refcount--;
7976 wpa_printf(MSG_DEBUG, "nl80211: Remove monitor interface: refcount=%d",
7977 drv->monitor_refcount);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007978 if (drv->monitor_refcount > 0)
7979 return;
7980
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007981 if (drv->monitor_ifidx >= 0) {
7982 nl80211_remove_iface(drv, drv->monitor_ifidx);
7983 drv->monitor_ifidx = -1;
7984 }
7985 if (drv->monitor_sock >= 0) {
7986 eloop_unregister_read_sock(drv->monitor_sock);
7987 close(drv->monitor_sock);
7988 drv->monitor_sock = -1;
7989 }
7990}
7991
7992
7993static int
7994nl80211_create_monitor_interface(struct wpa_driver_nl80211_data *drv)
7995{
7996 char buf[IFNAMSIZ];
7997 struct sockaddr_ll ll;
7998 int optval;
7999 socklen_t optlen;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008000
8001 if (drv->monitor_ifidx >= 0) {
8002 drv->monitor_refcount++;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008003 wpa_printf(MSG_DEBUG, "nl80211: Re-use existing monitor interface: refcount=%d",
8004 drv->monitor_refcount);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008005 return 0;
8006 }
8007
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008008 if (os_strncmp(drv->first_bss->ifname, "p2p-", 4) == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008009 /*
8010 * P2P interface name is of the format p2p-%s-%d. For monitor
8011 * interface name corresponding to P2P GO, replace "p2p-" with
8012 * "mon-" to retain the same interface name length and to
8013 * indicate that it is a monitor interface.
8014 */
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008015 snprintf(buf, IFNAMSIZ, "mon-%s", drv->first_bss->ifname + 4);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008016 } else {
8017 /* Non-P2P interface with AP functionality. */
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008018 snprintf(buf, IFNAMSIZ, "mon.%s", drv->first_bss->ifname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008019 }
8020
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008021 buf[IFNAMSIZ - 1] = '\0';
8022
8023 drv->monitor_ifidx =
8024 nl80211_create_iface(drv, buf, NL80211_IFTYPE_MONITOR, NULL,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008025 0, NULL, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008026
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07008027 if (drv->monitor_ifidx == -EOPNOTSUPP) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008028 /*
8029 * This is backward compatibility for a few versions of
8030 * the kernel only that didn't advertise the right
8031 * attributes for the only driver that then supported
8032 * AP mode w/o monitor -- ath6kl.
8033 */
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07008034 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support "
8035 "monitor interface type - try to run without it");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008036 drv->device_ap_sme = 1;
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07008037 }
8038
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008039 if (drv->monitor_ifidx < 0)
8040 return -1;
8041
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008042 if (linux_set_iface_flags(drv->global->ioctl_sock, buf, 1))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008043 goto error;
8044
8045 memset(&ll, 0, sizeof(ll));
8046 ll.sll_family = AF_PACKET;
8047 ll.sll_ifindex = drv->monitor_ifidx;
8048 drv->monitor_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
8049 if (drv->monitor_sock < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008050 wpa_printf(MSG_ERROR, "nl80211: socket[PF_PACKET,SOCK_RAW] failed: %s",
8051 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008052 goto error;
8053 }
8054
8055 if (add_monitor_filter(drv->monitor_sock)) {
8056 wpa_printf(MSG_INFO, "Failed to set socket filter for monitor "
8057 "interface; do filtering in user space");
8058 /* This works, but will cost in performance. */
8059 }
8060
8061 if (bind(drv->monitor_sock, (struct sockaddr *) &ll, sizeof(ll)) < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008062 wpa_printf(MSG_ERROR, "nl80211: monitor socket bind failed: %s",
8063 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008064 goto error;
8065 }
8066
8067 optlen = sizeof(optval);
8068 optval = 20;
8069 if (setsockopt
8070 (drv->monitor_sock, SOL_SOCKET, SO_PRIORITY, &optval, optlen)) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008071 wpa_printf(MSG_ERROR, "nl80211: Failed to set socket priority: %s",
8072 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008073 goto error;
8074 }
8075
8076 if (eloop_register_read_sock(drv->monitor_sock, handle_monitor_read,
8077 drv, NULL)) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008078 wpa_printf(MSG_INFO, "nl80211: Could not register monitor read socket");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008079 goto error;
8080 }
8081
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008082 drv->monitor_refcount++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008083 return 0;
8084 error:
8085 nl80211_remove_monitor_interface(drv);
8086 return -1;
8087}
8088
8089
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008090static int nl80211_setup_ap(struct i802_bss *bss)
8091{
8092 struct wpa_driver_nl80211_data *drv = bss->drv;
8093
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008094 wpa_printf(MSG_DEBUG, "nl80211: Setup AP(%s) - device_ap_sme=%d use_monitor=%d",
8095 bss->ifname, drv->device_ap_sme, drv->use_monitor);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008096
8097 /*
8098 * Disable Probe Request reporting unless we need it in this way for
8099 * devices that include the AP SME, in the other case (unless using
8100 * monitor iface) we'll get it through the nl_mgmt socket instead.
8101 */
8102 if (!drv->device_ap_sme)
8103 wpa_driver_nl80211_probe_req_report(bss, 0);
8104
8105 if (!drv->device_ap_sme && !drv->use_monitor)
8106 if (nl80211_mgmt_subscribe_ap(bss))
8107 return -1;
8108
8109 if (drv->device_ap_sme && !drv->use_monitor)
8110 if (nl80211_mgmt_subscribe_ap_dev_sme(bss))
8111 return -1;
8112
8113 if (!drv->device_ap_sme && drv->use_monitor &&
8114 nl80211_create_monitor_interface(drv) &&
8115 !drv->device_ap_sme)
Dmitry Shmidt04949592012-07-19 12:16:46 -07008116 return -1;
8117
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008118 if (drv->device_ap_sme &&
8119 wpa_driver_nl80211_probe_req_report(bss, 1) < 0) {
8120 wpa_printf(MSG_DEBUG, "nl80211: Failed to enable "
8121 "Probe Request frame reporting in AP mode");
8122 /* Try to survive without this */
8123 }
8124
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008125 return 0;
8126}
8127
8128
8129static void nl80211_teardown_ap(struct i802_bss *bss)
8130{
8131 struct wpa_driver_nl80211_data *drv = bss->drv;
8132
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008133 wpa_printf(MSG_DEBUG, "nl80211: Teardown AP(%s) - device_ap_sme=%d use_monitor=%d",
8134 bss->ifname, drv->device_ap_sme, drv->use_monitor);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008135 if (drv->device_ap_sme) {
8136 wpa_driver_nl80211_probe_req_report(bss, 0);
8137 if (!drv->use_monitor)
8138 nl80211_mgmt_unsubscribe(bss, "AP teardown (dev SME)");
8139 } else if (drv->use_monitor)
8140 nl80211_remove_monitor_interface(drv);
8141 else
8142 nl80211_mgmt_unsubscribe(bss, "AP teardown");
8143
8144 bss->beacon_set = 0;
8145}
8146
8147
8148static int nl80211_send_eapol_data(struct i802_bss *bss,
8149 const u8 *addr, const u8 *data,
8150 size_t data_len)
8151{
8152 struct sockaddr_ll ll;
8153 int ret;
8154
8155 if (bss->drv->eapol_tx_sock < 0) {
8156 wpa_printf(MSG_DEBUG, "nl80211: No socket to send EAPOL");
8157 return -1;
8158 }
8159
8160 os_memset(&ll, 0, sizeof(ll));
8161 ll.sll_family = AF_PACKET;
8162 ll.sll_ifindex = bss->ifindex;
8163 ll.sll_protocol = htons(ETH_P_PAE);
8164 ll.sll_halen = ETH_ALEN;
8165 os_memcpy(ll.sll_addr, addr, ETH_ALEN);
8166 ret = sendto(bss->drv->eapol_tx_sock, data, data_len, 0,
8167 (struct sockaddr *) &ll, sizeof(ll));
8168 if (ret < 0)
8169 wpa_printf(MSG_ERROR, "nl80211: EAPOL TX: %s",
8170 strerror(errno));
8171
8172 return ret;
8173}
8174
8175
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008176static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
8177
8178static int wpa_driver_nl80211_hapd_send_eapol(
8179 void *priv, const u8 *addr, const u8 *data,
8180 size_t data_len, int encrypt, const u8 *own_addr, u32 flags)
8181{
8182 struct i802_bss *bss = priv;
8183 struct wpa_driver_nl80211_data *drv = bss->drv;
8184 struct ieee80211_hdr *hdr;
8185 size_t len;
8186 u8 *pos;
8187 int res;
8188 int qos = flags & WPA_STA_WMM;
Dmitry Shmidt641185e2013-11-06 15:17:13 -08008189
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008190 if (drv->device_ap_sme || !drv->use_monitor)
8191 return nl80211_send_eapol_data(bss, addr, data, data_len);
8192
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008193 len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
8194 data_len;
8195 hdr = os_zalloc(len);
8196 if (hdr == NULL) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008197 wpa_printf(MSG_INFO, "nl80211: Failed to allocate EAPOL buffer(len=%lu)",
8198 (unsigned long) len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008199 return -1;
8200 }
8201
8202 hdr->frame_control =
8203 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
8204 hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
8205 if (encrypt)
8206 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
8207 if (qos) {
8208 hdr->frame_control |=
8209 host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
8210 }
8211
8212 memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
8213 memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
8214 memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
8215 pos = (u8 *) (hdr + 1);
8216
8217 if (qos) {
Dmitry Shmidtaa532512012-09-24 10:35:31 -07008218 /* Set highest priority in QoS header */
8219 pos[0] = 7;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008220 pos[1] = 0;
8221 pos += 2;
8222 }
8223
8224 memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
8225 pos += sizeof(rfc1042_header);
8226 WPA_PUT_BE16(pos, ETH_P_PAE);
8227 pos += 2;
8228 memcpy(pos, data, data_len);
8229
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008230 res = wpa_driver_nl80211_send_frame(bss, (u8 *) hdr, len, encrypt, 0,
8231 0, 0, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008232 if (res < 0) {
8233 wpa_printf(MSG_ERROR, "i802_send_eapol - packet len: %lu - "
8234 "failed: %d (%s)",
8235 (unsigned long) len, errno, strerror(errno));
8236 }
8237 os_free(hdr);
8238
8239 return res;
8240}
8241
8242
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008243static int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr,
8244 int total_flags,
8245 int flags_or, int flags_and)
8246{
8247 struct i802_bss *bss = priv;
8248 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008249 struct nl_msg *msg;
8250 struct nlattr *flags;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008251 struct nl80211_sta_flag_update upd;
8252
8253 msg = nlmsg_alloc();
8254 if (!msg)
8255 return -ENOMEM;
8256
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008257 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008258
8259 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
8260 if_nametoindex(bss->ifname));
8261 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
8262
8263 /*
8264 * Backwards compatibility version using NL80211_ATTR_STA_FLAGS. This
8265 * can be removed eventually.
8266 */
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008267 flags = nla_nest_start(msg, NL80211_ATTR_STA_FLAGS);
8268 if (!flags)
8269 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008270 if (total_flags & WPA_STA_AUTHORIZED)
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008271 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_AUTHORIZED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008272
8273 if (total_flags & WPA_STA_WMM)
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008274 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_WME);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008275
8276 if (total_flags & WPA_STA_SHORT_PREAMBLE)
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008277 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_SHORT_PREAMBLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008278
8279 if (total_flags & WPA_STA_MFP)
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008280 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_MFP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008281
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008282 if (total_flags & WPA_STA_TDLS_PEER)
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008283 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_TDLS_PEER);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008284
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008285 nla_nest_end(msg, flags);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008286
8287 os_memset(&upd, 0, sizeof(upd));
8288 upd.mask = sta_flags_nl80211(flags_or | ~flags_and);
8289 upd.set = sta_flags_nl80211(flags_or);
8290 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
8291
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008292 return send_and_recv_msgs(drv, msg, NULL, NULL);
8293 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008294 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008295 return -ENOBUFS;
8296}
8297
8298
8299static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv,
8300 struct wpa_driver_associate_params *params)
8301{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008302 enum nl80211_iftype nlmode, old_mode;
8303 struct hostapd_freq_params freq = {
8304 .freq = params->freq,
8305 };
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008306
8307 if (params->p2p) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008308 wpa_printf(MSG_DEBUG, "nl80211: Setup AP operations for P2P "
8309 "group (GO)");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008310 nlmode = NL80211_IFTYPE_P2P_GO;
8311 } else
8312 nlmode = NL80211_IFTYPE_AP;
8313
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008314 old_mode = drv->nlmode;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008315 if (wpa_driver_nl80211_set_mode(drv->first_bss, nlmode)) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008316 nl80211_remove_monitor_interface(drv);
8317 return -1;
8318 }
8319
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008320 if (wpa_driver_nl80211_set_freq(drv->first_bss, &freq)) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008321 if (old_mode != nlmode)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008322 wpa_driver_nl80211_set_mode(drv->first_bss, old_mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008323 nl80211_remove_monitor_interface(drv);
8324 return -1;
8325 }
8326
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008327 return 0;
8328}
8329
8330
8331static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv)
8332{
8333 struct nl_msg *msg;
8334 int ret = -1;
8335
8336 msg = nlmsg_alloc();
8337 if (!msg)
8338 return -1;
8339
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008340 nl80211_cmd(drv, msg, 0, NL80211_CMD_LEAVE_IBSS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008341 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
8342 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8343 msg = NULL;
8344 if (ret) {
8345 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS failed: ret=%d "
8346 "(%s)", ret, strerror(-ret));
8347 goto nla_put_failure;
8348 }
8349
8350 ret = 0;
8351 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS request sent successfully");
8352
8353nla_put_failure:
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008354 if (wpa_driver_nl80211_set_mode(drv->first_bss,
Dmitry Shmidt56052862013-10-04 10:23:25 -07008355 NL80211_IFTYPE_STATION)) {
8356 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
8357 "station mode");
8358 }
8359
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008360 nlmsg_free(msg);
8361 return ret;
8362}
8363
8364
8365static int wpa_driver_nl80211_ibss(struct wpa_driver_nl80211_data *drv,
8366 struct wpa_driver_associate_params *params)
8367{
8368 struct nl_msg *msg;
8369 int ret = -1;
8370 int count = 0;
8371
8372 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS (ifindex=%d)", drv->ifindex);
8373
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008374 if (wpa_driver_nl80211_set_mode(drv->first_bss,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008375 NL80211_IFTYPE_ADHOC)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008376 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
8377 "IBSS mode");
8378 return -1;
8379 }
8380
8381retry:
8382 msg = nlmsg_alloc();
8383 if (!msg)
8384 return -1;
8385
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008386 nl80211_cmd(drv, msg, 0, NL80211_CMD_JOIN_IBSS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008387 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
8388
8389 if (params->ssid == NULL || params->ssid_len > sizeof(drv->ssid))
8390 goto nla_put_failure;
8391
8392 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
8393 params->ssid, params->ssid_len);
8394 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
8395 params->ssid);
8396 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
8397 drv->ssid_len = params->ssid_len;
8398
8399 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
8400 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
8401
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08008402 if (params->beacon_int > 0) {
8403 wpa_printf(MSG_DEBUG, " * beacon_int=%d", params->beacon_int);
8404 NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL,
8405 params->beacon_int);
8406 }
8407
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008408 ret = nl80211_set_conn_keys(params, msg);
8409 if (ret)
8410 goto nla_put_failure;
8411
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008412 if (params->bssid && params->fixed_bssid) {
8413 wpa_printf(MSG_DEBUG, " * BSSID=" MACSTR,
8414 MAC2STR(params->bssid));
8415 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
8416 }
8417
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008418 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
8419 params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
8420 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
8421 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008422 wpa_printf(MSG_DEBUG, " * control port");
8423 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT);
8424 }
8425
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008426 if (params->wpa_ie) {
8427 wpa_hexdump(MSG_DEBUG,
8428 " * Extra IEs for Beacon/Probe Response frames",
8429 params->wpa_ie, params->wpa_ie_len);
8430 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
8431 params->wpa_ie);
8432 }
8433
8434 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8435 msg = NULL;
8436 if (ret) {
8437 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS failed: ret=%d (%s)",
8438 ret, strerror(-ret));
8439 count++;
8440 if (ret == -EALREADY && count == 1) {
8441 wpa_printf(MSG_DEBUG, "nl80211: Retry IBSS join after "
8442 "forced leave");
8443 nl80211_leave_ibss(drv);
8444 nlmsg_free(msg);
8445 goto retry;
8446 }
8447
8448 goto nla_put_failure;
8449 }
8450 ret = 0;
8451 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS request sent successfully");
8452
8453nla_put_failure:
8454 nlmsg_free(msg);
8455 return ret;
8456}
8457
8458
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008459static int nl80211_connect_common(struct wpa_driver_nl80211_data *drv,
8460 struct wpa_driver_associate_params *params,
8461 struct nl_msg *msg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008462{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008463 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008464
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008465 if (params->bssid) {
8466 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
8467 MAC2STR(params->bssid));
8468 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
8469 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008470
Dmitry Shmidt96be6222014-02-13 10:16:51 -08008471 if (params->bssid_hint) {
8472 wpa_printf(MSG_DEBUG, " * bssid_hint=" MACSTR,
8473 MAC2STR(params->bssid_hint));
8474 NLA_PUT(msg, NL80211_ATTR_MAC_HINT, ETH_ALEN,
8475 params->bssid_hint);
8476 }
8477
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008478 if (params->freq) {
8479 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
8480 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008481 drv->assoc_freq = params->freq;
8482 } else
8483 drv->assoc_freq = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008484
Dmitry Shmidt96be6222014-02-13 10:16:51 -08008485 if (params->freq_hint) {
8486 wpa_printf(MSG_DEBUG, " * freq_hint=%d", params->freq_hint);
8487 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ_HINT,
8488 params->freq_hint);
8489 }
8490
Dmitry Shmidt04949592012-07-19 12:16:46 -07008491 if (params->bg_scan_period >= 0) {
8492 wpa_printf(MSG_DEBUG, " * bg scan period=%d",
8493 params->bg_scan_period);
8494 NLA_PUT_U16(msg, NL80211_ATTR_BG_SCAN_PERIOD,
8495 params->bg_scan_period);
8496 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008497
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008498 if (params->ssid) {
8499 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
8500 params->ssid, params->ssid_len);
8501 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
8502 params->ssid);
8503 if (params->ssid_len > sizeof(drv->ssid))
8504 goto nla_put_failure;
8505 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
8506 drv->ssid_len = params->ssid_len;
8507 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008508
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008509 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
8510 if (params->wpa_ie)
8511 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
8512 params->wpa_ie);
8513
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008514 if (params->wpa_proto) {
8515 enum nl80211_wpa_versions ver = 0;
8516
8517 if (params->wpa_proto & WPA_PROTO_WPA)
8518 ver |= NL80211_WPA_VERSION_1;
8519 if (params->wpa_proto & WPA_PROTO_RSN)
8520 ver |= NL80211_WPA_VERSION_2;
8521
8522 wpa_printf(MSG_DEBUG, " * WPA Versions 0x%x", ver);
8523 NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver);
8524 }
8525
8526 if (params->pairwise_suite != WPA_CIPHER_NONE) {
8527 u32 cipher = wpa_cipher_to_cipher_suite(params->pairwise_suite);
8528 wpa_printf(MSG_DEBUG, " * pairwise=0x%x", cipher);
8529 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE, cipher);
8530 }
8531
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08008532 if (params->group_suite == WPA_CIPHER_GTK_NOT_USED &&
8533 !(drv->capa.enc & WPA_DRIVER_CAPA_ENC_GTK_NOT_USED)) {
8534 /*
8535 * This is likely to work even though many drivers do not
8536 * advertise support for operations without GTK.
8537 */
8538 wpa_printf(MSG_DEBUG, " * skip group cipher configuration for GTK_NOT_USED due to missing driver support advertisement");
8539 } else if (params->group_suite != WPA_CIPHER_NONE) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008540 u32 cipher = wpa_cipher_to_cipher_suite(params->group_suite);
8541 wpa_printf(MSG_DEBUG, " * group=0x%x", cipher);
8542 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher);
8543 }
8544
8545 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
8546 params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
8547 params->key_mgmt_suite == WPA_KEY_MGMT_FT_IEEE8021X ||
8548 params->key_mgmt_suite == WPA_KEY_MGMT_FT_PSK ||
8549 params->key_mgmt_suite == WPA_KEY_MGMT_CCKM) {
8550 int mgmt = WLAN_AKM_SUITE_PSK;
8551
8552 switch (params->key_mgmt_suite) {
8553 case WPA_KEY_MGMT_CCKM:
8554 mgmt = WLAN_AKM_SUITE_CCKM;
8555 break;
8556 case WPA_KEY_MGMT_IEEE8021X:
8557 mgmt = WLAN_AKM_SUITE_8021X;
8558 break;
8559 case WPA_KEY_MGMT_FT_IEEE8021X:
8560 mgmt = WLAN_AKM_SUITE_FT_8021X;
8561 break;
8562 case WPA_KEY_MGMT_FT_PSK:
8563 mgmt = WLAN_AKM_SUITE_FT_PSK;
8564 break;
8565 case WPA_KEY_MGMT_PSK:
8566 default:
8567 mgmt = WLAN_AKM_SUITE_PSK;
8568 break;
8569 }
8570 NLA_PUT_U32(msg, NL80211_ATTR_AKM_SUITES, mgmt);
8571 }
8572
8573 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT);
8574
8575 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED)
8576 NLA_PUT_U32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED);
8577
8578 if (params->disable_ht)
8579 NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_HT);
8580
8581 if (params->htcaps && params->htcaps_mask) {
8582 int sz = sizeof(struct ieee80211_ht_capabilities);
8583 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY, sz, params->htcaps);
8584 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY_MASK, sz,
8585 params->htcaps_mask);
8586 }
8587
8588#ifdef CONFIG_VHT_OVERRIDES
8589 if (params->disable_vht) {
8590 wpa_printf(MSG_DEBUG, " * VHT disabled");
8591 NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_VHT);
8592 }
8593
8594 if (params->vhtcaps && params->vhtcaps_mask) {
8595 int sz = sizeof(struct ieee80211_vht_capabilities);
8596 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY, sz, params->vhtcaps);
8597 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY_MASK, sz,
8598 params->vhtcaps_mask);
8599 }
8600#endif /* CONFIG_VHT_OVERRIDES */
8601
8602 if (params->p2p)
8603 wpa_printf(MSG_DEBUG, " * P2P group");
8604
8605 return 0;
8606nla_put_failure:
8607 return -1;
8608}
8609
8610
8611static int wpa_driver_nl80211_try_connect(
8612 struct wpa_driver_nl80211_data *drv,
8613 struct wpa_driver_associate_params *params)
8614{
8615 struct nl_msg *msg;
8616 enum nl80211_auth_type type;
8617 int ret;
8618 int algs;
8619
8620 msg = nlmsg_alloc();
8621 if (!msg)
8622 return -1;
8623
8624 wpa_printf(MSG_DEBUG, "nl80211: Connect (ifindex=%d)", drv->ifindex);
8625 nl80211_cmd(drv, msg, 0, NL80211_CMD_CONNECT);
8626
8627 ret = nl80211_connect_common(drv, params, msg);
8628 if (ret)
8629 goto nla_put_failure;
8630
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008631 algs = 0;
8632 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
8633 algs++;
8634 if (params->auth_alg & WPA_AUTH_ALG_SHARED)
8635 algs++;
8636 if (params->auth_alg & WPA_AUTH_ALG_LEAP)
8637 algs++;
8638 if (algs > 1) {
8639 wpa_printf(MSG_DEBUG, " * Leave out Auth Type for automatic "
8640 "selection");
8641 goto skip_auth_type;
8642 }
8643
8644 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
8645 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
8646 else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
8647 type = NL80211_AUTHTYPE_SHARED_KEY;
8648 else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
8649 type = NL80211_AUTHTYPE_NETWORK_EAP;
8650 else if (params->auth_alg & WPA_AUTH_ALG_FT)
8651 type = NL80211_AUTHTYPE_FT;
8652 else
8653 goto nla_put_failure;
8654
8655 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
8656 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
8657
8658skip_auth_type:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008659 ret = nl80211_set_conn_keys(params, msg);
8660 if (ret)
8661 goto nla_put_failure;
8662
8663 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8664 msg = NULL;
8665 if (ret) {
8666 wpa_printf(MSG_DEBUG, "nl80211: MLME connect failed: ret=%d "
8667 "(%s)", ret, strerror(-ret));
8668 goto nla_put_failure;
8669 }
8670 ret = 0;
8671 wpa_printf(MSG_DEBUG, "nl80211: Connect request send successfully");
8672
8673nla_put_failure:
8674 nlmsg_free(msg);
8675 return ret;
8676
8677}
8678
8679
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08008680static int wpa_driver_nl80211_connect(
8681 struct wpa_driver_nl80211_data *drv,
8682 struct wpa_driver_associate_params *params)
8683{
8684 int ret = wpa_driver_nl80211_try_connect(drv, params);
8685 if (ret == -EALREADY) {
8686 /*
8687 * cfg80211 does not currently accept new connections if
8688 * we are already connected. As a workaround, force
8689 * disconnection and try again.
8690 */
8691 wpa_printf(MSG_DEBUG, "nl80211: Explicitly "
8692 "disconnecting before reassociation "
8693 "attempt");
8694 if (wpa_driver_nl80211_disconnect(
8695 drv, WLAN_REASON_PREV_AUTH_NOT_VALID))
8696 return -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08008697 ret = wpa_driver_nl80211_try_connect(drv, params);
8698 }
8699 return ret;
8700}
8701
8702
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008703static int wpa_driver_nl80211_associate(
8704 void *priv, struct wpa_driver_associate_params *params)
8705{
8706 struct i802_bss *bss = priv;
8707 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008708 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008709 struct nl_msg *msg;
8710
8711 if (params->mode == IEEE80211_MODE_AP)
8712 return wpa_driver_nl80211_ap(drv, params);
8713
8714 if (params->mode == IEEE80211_MODE_IBSS)
8715 return wpa_driver_nl80211_ibss(drv, params);
8716
8717 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008718 enum nl80211_iftype nlmode = params->p2p ?
8719 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
8720
8721 if (wpa_driver_nl80211_set_mode(priv, nlmode) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008722 return -1;
8723 return wpa_driver_nl80211_connect(drv, params);
8724 }
8725
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07008726 nl80211_mark_disconnected(drv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008727
8728 msg = nlmsg_alloc();
8729 if (!msg)
8730 return -1;
8731
8732 wpa_printf(MSG_DEBUG, "nl80211: Associate (ifindex=%d)",
8733 drv->ifindex);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008734 nl80211_cmd(drv, msg, 0, NL80211_CMD_ASSOCIATE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008735
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008736 ret = nl80211_connect_common(drv, params, msg);
8737 if (ret)
8738 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008739
8740 if (params->prev_bssid) {
8741 wpa_printf(MSG_DEBUG, " * prev_bssid=" MACSTR,
8742 MAC2STR(params->prev_bssid));
8743 NLA_PUT(msg, NL80211_ATTR_PREV_BSSID, ETH_ALEN,
8744 params->prev_bssid);
8745 }
8746
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008747 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8748 msg = NULL;
8749 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008750 wpa_dbg(drv->ctx, MSG_DEBUG,
8751 "nl80211: MLME command failed (assoc): ret=%d (%s)",
8752 ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008753 nl80211_dump_scan(drv);
8754 goto nla_put_failure;
8755 }
8756 ret = 0;
8757 wpa_printf(MSG_DEBUG, "nl80211: Association request send "
8758 "successfully");
8759
8760nla_put_failure:
8761 nlmsg_free(msg);
8762 return ret;
8763}
8764
8765
8766static int nl80211_set_mode(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008767 int ifindex, enum nl80211_iftype mode)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008768{
8769 struct nl_msg *msg;
8770 int ret = -ENOBUFS;
8771
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008772 wpa_printf(MSG_DEBUG, "nl80211: Set mode ifindex %d iftype %d (%s)",
8773 ifindex, mode, nl80211_iftype_str(mode));
8774
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008775 msg = nlmsg_alloc();
8776 if (!msg)
8777 return -ENOMEM;
8778
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008779 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_INTERFACE);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008780 if (nl80211_set_iface_id(msg, drv->first_bss) < 0)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008781 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008782 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, mode);
8783
8784 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008785 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008786 if (!ret)
8787 return 0;
8788nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008789 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008790 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface %d to mode %d:"
8791 " %d (%s)", ifindex, mode, ret, strerror(-ret));
8792 return ret;
8793}
8794
8795
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008796static int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
8797 enum nl80211_iftype nlmode)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008798{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008799 struct wpa_driver_nl80211_data *drv = bss->drv;
8800 int ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008801 int i;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008802 int was_ap = is_ap_interface(drv->nlmode);
8803 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008804
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008805 res = nl80211_set_mode(drv, drv->ifindex, nlmode);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008806 if (res && nlmode == nl80211_get_ifmode(bss))
8807 res = 0;
8808
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008809 if (res == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008810 drv->nlmode = nlmode;
8811 ret = 0;
8812 goto done;
8813 }
8814
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008815 if (res == -ENODEV)
8816 return -1;
8817
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008818 if (nlmode == drv->nlmode) {
8819 wpa_printf(MSG_DEBUG, "nl80211: Interface already in "
8820 "requested mode - ignore error");
8821 ret = 0;
8822 goto done; /* Already in the requested mode */
8823 }
8824
8825 /* mac80211 doesn't allow mode changes while the device is up, so
8826 * take the device down, try to set the mode again, and bring the
8827 * device back up.
8828 */
8829 wpa_printf(MSG_DEBUG, "nl80211: Try mode change after setting "
8830 "interface down");
8831 for (i = 0; i < 10; i++) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008832 res = i802_set_iface_flags(bss, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008833 if (res == -EACCES || res == -ENODEV)
8834 break;
8835 if (res == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008836 /* Try to set the mode again while the interface is
8837 * down */
8838 ret = nl80211_set_mode(drv, drv->ifindex, nlmode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008839 if (ret == -EACCES)
8840 break;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008841 res = i802_set_iface_flags(bss, 1);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008842 if (res && !ret)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008843 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008844 else if (ret != -EBUSY)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008845 break;
8846 } else
8847 wpa_printf(MSG_DEBUG, "nl80211: Failed to set "
8848 "interface down");
8849 os_sleep(0, 100000);
8850 }
8851
8852 if (!ret) {
8853 wpa_printf(MSG_DEBUG, "nl80211: Mode change succeeded while "
8854 "interface is down");
8855 drv->nlmode = nlmode;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008856 drv->ignore_if_down_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008857 }
8858
8859done:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008860 if (ret) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008861 wpa_printf(MSG_DEBUG, "nl80211: Interface mode change to %d "
8862 "from %d failed", nlmode, drv->nlmode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008863 return ret;
8864 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008865
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008866 if (is_p2p_net_interface(nlmode))
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008867 nl80211_disable_11b_rates(drv, drv->ifindex, 1);
8868 else if (drv->disabled_11b_rates)
8869 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
8870
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008871 if (is_ap_interface(nlmode)) {
8872 nl80211_mgmt_unsubscribe(bss, "start AP");
8873 /* Setup additional AP mode functionality if needed */
8874 if (nl80211_setup_ap(bss))
8875 return -1;
8876 } else if (was_ap) {
8877 /* Remove additional AP mode functionality */
8878 nl80211_teardown_ap(bss);
8879 } else {
8880 nl80211_mgmt_unsubscribe(bss, "mode change");
8881 }
8882
Dmitry Shmidt04949592012-07-19 12:16:46 -07008883 if (!bss->in_deinit && !is_ap_interface(nlmode) &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008884 nl80211_mgmt_subscribe_non_ap(bss) < 0)
8885 wpa_printf(MSG_DEBUG, "nl80211: Failed to register Action "
8886 "frame processing - ignore for now");
8887
8888 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008889}
8890
8891
8892static int wpa_driver_nl80211_get_capa(void *priv,
8893 struct wpa_driver_capa *capa)
8894{
8895 struct i802_bss *bss = priv;
8896 struct wpa_driver_nl80211_data *drv = bss->drv;
8897 if (!drv->has_capability)
8898 return -1;
8899 os_memcpy(capa, &drv->capa, sizeof(*capa));
Dmitry Shmidt444d5672013-04-01 13:08:44 -07008900 if (drv->extended_capa && drv->extended_capa_mask) {
8901 capa->extended_capa = drv->extended_capa;
8902 capa->extended_capa_mask = drv->extended_capa_mask;
8903 capa->extended_capa_len = drv->extended_capa_len;
8904 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008905
8906 if ((capa->flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
8907 !drv->allow_p2p_device) {
8908 wpa_printf(MSG_DEBUG, "nl80211: Do not indicate P2P_DEVICE support (p2p_device=1 driver param not specified)");
8909 capa->flags &= ~WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE;
8910 }
8911
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008912 return 0;
8913}
8914
8915
8916static int wpa_driver_nl80211_set_operstate(void *priv, int state)
8917{
8918 struct i802_bss *bss = priv;
8919 struct wpa_driver_nl80211_data *drv = bss->drv;
8920
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008921 wpa_printf(MSG_DEBUG, "nl80211: Set %s operstate %d->%d (%s)",
8922 bss->ifname, drv->operstate, state,
8923 state ? "UP" : "DORMANT");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008924 drv->operstate = state;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008925 return netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, -1,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008926 state ? IF_OPER_UP : IF_OPER_DORMANT);
8927}
8928
8929
8930static int wpa_driver_nl80211_set_supp_port(void *priv, int authorized)
8931{
8932 struct i802_bss *bss = priv;
8933 struct wpa_driver_nl80211_data *drv = bss->drv;
8934 struct nl_msg *msg;
8935 struct nl80211_sta_flag_update upd;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008936 int ret = -ENOBUFS;
8937
8938 if (!drv->associated && is_zero_ether_addr(drv->bssid) && !authorized) {
8939 wpa_printf(MSG_DEBUG, "nl80211: Skip set_supp_port(unauthorized) while not associated");
8940 return 0;
8941 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008942
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07008943 wpa_printf(MSG_DEBUG, "nl80211: Set supplicant port %sauthorized for "
8944 MACSTR, authorized ? "" : "un", MAC2STR(drv->bssid));
8945
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008946 msg = nlmsg_alloc();
8947 if (!msg)
8948 return -ENOMEM;
8949
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008950 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008951
8952 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
8953 if_nametoindex(bss->ifname));
8954 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid);
8955
8956 os_memset(&upd, 0, sizeof(upd));
8957 upd.mask = BIT(NL80211_STA_FLAG_AUTHORIZED);
8958 if (authorized)
8959 upd.set = BIT(NL80211_STA_FLAG_AUTHORIZED);
8960 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
8961
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008962 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8963 msg = NULL;
8964 if (!ret)
8965 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008966 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008967 nlmsg_free(msg);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008968 wpa_printf(MSG_DEBUG, "nl80211: Failed to set STA flag: %d (%s)",
8969 ret, strerror(-ret));
8970 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008971}
8972
8973
Jouni Malinen75ecf522011-06-27 15:19:46 -07008974/* Set kernel driver on given frequency (MHz) */
8975static int i802_set_freq(void *priv, struct hostapd_freq_params *freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008976{
Jouni Malinen75ecf522011-06-27 15:19:46 -07008977 struct i802_bss *bss = priv;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008978 return wpa_driver_nl80211_set_freq(bss, freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008979}
8980
8981
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008982static inline int min_int(int a, int b)
8983{
8984 if (a < b)
8985 return a;
8986 return b;
8987}
8988
8989
8990static int get_key_handler(struct nl_msg *msg, void *arg)
8991{
8992 struct nlattr *tb[NL80211_ATTR_MAX + 1];
8993 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
8994
8995 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
8996 genlmsg_attrlen(gnlh, 0), NULL);
8997
8998 /*
8999 * TODO: validate the key index and mac address!
9000 * Otherwise, there's a race condition as soon as
9001 * the kernel starts sending key notifications.
9002 */
9003
9004 if (tb[NL80211_ATTR_KEY_SEQ])
9005 memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]),
9006 min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6));
9007 return NL_SKIP;
9008}
9009
9010
9011static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
9012 int idx, u8 *seq)
9013{
9014 struct i802_bss *bss = priv;
9015 struct wpa_driver_nl80211_data *drv = bss->drv;
9016 struct nl_msg *msg;
9017
9018 msg = nlmsg_alloc();
9019 if (!msg)
9020 return -ENOMEM;
9021
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009022 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_KEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009023
9024 if (addr)
9025 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
9026 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, idx);
9027 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
9028
9029 memset(seq, 0, 6);
9030
9031 return send_and_recv_msgs(drv, msg, get_key_handler, seq);
9032 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009033 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009034 return -ENOBUFS;
9035}
9036
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009037
9038static int i802_set_rts(void *priv, int rts)
9039{
9040 struct i802_bss *bss = priv;
9041 struct wpa_driver_nl80211_data *drv = bss->drv;
9042 struct nl_msg *msg;
9043 int ret = -ENOBUFS;
9044 u32 val;
9045
9046 msg = nlmsg_alloc();
9047 if (!msg)
9048 return -ENOMEM;
9049
9050 if (rts >= 2347)
9051 val = (u32) -1;
9052 else
9053 val = rts;
9054
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009055 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009056 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
9057 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD, val);
9058
9059 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009060 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009061 if (!ret)
9062 return 0;
9063nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009064 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009065 wpa_printf(MSG_DEBUG, "nl80211: Failed to set RTS threshold %d: "
9066 "%d (%s)", rts, ret, strerror(-ret));
9067 return ret;
9068}
9069
9070
9071static int i802_set_frag(void *priv, int frag)
9072{
9073 struct i802_bss *bss = priv;
9074 struct wpa_driver_nl80211_data *drv = bss->drv;
9075 struct nl_msg *msg;
9076 int ret = -ENOBUFS;
9077 u32 val;
9078
9079 msg = nlmsg_alloc();
9080 if (!msg)
9081 return -ENOMEM;
9082
9083 if (frag >= 2346)
9084 val = (u32) -1;
9085 else
9086 val = frag;
9087
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009088 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009089 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
9090 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD, val);
9091
9092 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009093 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009094 if (!ret)
9095 return 0;
9096nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009097 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009098 wpa_printf(MSG_DEBUG, "nl80211: Failed to set fragmentation threshold "
9099 "%d: %d (%s)", frag, ret, strerror(-ret));
9100 return ret;
9101}
9102
9103
9104static int i802_flush(void *priv)
9105{
9106 struct i802_bss *bss = priv;
9107 struct wpa_driver_nl80211_data *drv = bss->drv;
9108 struct nl_msg *msg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009109 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009110
9111 msg = nlmsg_alloc();
9112 if (!msg)
9113 return -1;
9114
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07009115 wpa_printf(MSG_DEBUG, "nl80211: flush -> DEL_STATION %s (all)",
9116 bss->ifname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009117 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009118
9119 /*
9120 * XXX: FIX! this needs to flush all VLANs too
9121 */
9122 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
9123 if_nametoindex(bss->ifname));
9124
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009125 res = send_and_recv_msgs(drv, msg, NULL, NULL);
9126 if (res) {
9127 wpa_printf(MSG_DEBUG, "nl80211: Station flush failed: ret=%d "
9128 "(%s)", res, strerror(-res));
9129 }
9130 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009131 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009132 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009133 return -ENOBUFS;
9134}
9135
9136
9137static int get_sta_handler(struct nl_msg *msg, void *arg)
9138{
9139 struct nlattr *tb[NL80211_ATTR_MAX + 1];
9140 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
9141 struct hostap_sta_driver_data *data = arg;
9142 struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
9143 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
9144 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
9145 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
9146 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
9147 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
9148 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03009149 [NL80211_STA_INFO_TX_FAILED] = { .type = NLA_U32 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009150 };
9151
9152 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9153 genlmsg_attrlen(gnlh, 0), NULL);
9154
9155 /*
9156 * TODO: validate the interface and mac address!
9157 * Otherwise, there's a race condition as soon as
9158 * the kernel starts sending station notifications.
9159 */
9160
9161 if (!tb[NL80211_ATTR_STA_INFO]) {
9162 wpa_printf(MSG_DEBUG, "sta stats missing!");
9163 return NL_SKIP;
9164 }
9165 if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
9166 tb[NL80211_ATTR_STA_INFO],
9167 stats_policy)) {
9168 wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
9169 return NL_SKIP;
9170 }
9171
9172 if (stats[NL80211_STA_INFO_INACTIVE_TIME])
9173 data->inactive_msec =
9174 nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
9175 if (stats[NL80211_STA_INFO_RX_BYTES])
9176 data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
9177 if (stats[NL80211_STA_INFO_TX_BYTES])
9178 data->tx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
9179 if (stats[NL80211_STA_INFO_RX_PACKETS])
9180 data->rx_packets =
9181 nla_get_u32(stats[NL80211_STA_INFO_RX_PACKETS]);
9182 if (stats[NL80211_STA_INFO_TX_PACKETS])
9183 data->tx_packets =
9184 nla_get_u32(stats[NL80211_STA_INFO_TX_PACKETS]);
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03009185 if (stats[NL80211_STA_INFO_TX_FAILED])
9186 data->tx_retry_failed =
9187 nla_get_u32(stats[NL80211_STA_INFO_TX_FAILED]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009188
9189 return NL_SKIP;
9190}
9191
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009192static int i802_read_sta_data(struct i802_bss *bss,
9193 struct hostap_sta_driver_data *data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009194 const u8 *addr)
9195{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009196 struct wpa_driver_nl80211_data *drv = bss->drv;
9197 struct nl_msg *msg;
9198
9199 os_memset(data, 0, sizeof(*data));
9200 msg = nlmsg_alloc();
9201 if (!msg)
9202 return -ENOMEM;
9203
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009204 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009205
9206 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
9207 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
9208
9209 return send_and_recv_msgs(drv, msg, get_sta_handler, data);
9210 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009211 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009212 return -ENOBUFS;
9213}
9214
9215
9216static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
9217 int cw_min, int cw_max, int burst_time)
9218{
9219 struct i802_bss *bss = priv;
9220 struct wpa_driver_nl80211_data *drv = bss->drv;
9221 struct nl_msg *msg;
9222 struct nlattr *txq, *params;
9223
9224 msg = nlmsg_alloc();
9225 if (!msg)
9226 return -1;
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
9230 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
9231
9232 txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS);
9233 if (!txq)
9234 goto nla_put_failure;
9235
9236 /* We are only sending parameters for a single TXQ at a time */
9237 params = nla_nest_start(msg, 1);
9238 if (!params)
9239 goto nla_put_failure;
9240
9241 switch (queue) {
9242 case 0:
9243 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VO);
9244 break;
9245 case 1:
9246 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VI);
9247 break;
9248 case 2:
9249 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BE);
9250 break;
9251 case 3:
9252 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BK);
9253 break;
9254 }
9255 /* Burst time is configured in units of 0.1 msec and TXOP parameter in
9256 * 32 usec, so need to convert the value here. */
9257 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_TXOP, (burst_time * 100 + 16) / 32);
9258 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min);
9259 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max);
9260 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_AIFS, aifs);
9261
9262 nla_nest_end(msg, params);
9263
9264 nla_nest_end(msg, txq);
9265
9266 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
9267 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009268 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009269 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009270 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009271 return -1;
9272}
9273
9274
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009275static int i802_set_sta_vlan(struct i802_bss *bss, const u8 *addr,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009276 const char *ifname, int vlan_id)
9277{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009278 struct wpa_driver_nl80211_data *drv = bss->drv;
9279 struct nl_msg *msg;
9280 int ret = -ENOBUFS;
9281
9282 msg = nlmsg_alloc();
9283 if (!msg)
9284 return -ENOMEM;
9285
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009286 wpa_printf(MSG_DEBUG, "nl80211: %s[%d]: set_sta_vlan(" MACSTR
9287 ", ifname=%s[%d], vlan_id=%d)",
9288 bss->ifname, if_nametoindex(bss->ifname),
9289 MAC2STR(addr), ifname, if_nametoindex(ifname), vlan_id);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009290 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009291
9292 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
9293 if_nametoindex(bss->ifname));
9294 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
9295 NLA_PUT_U32(msg, NL80211_ATTR_STA_VLAN,
9296 if_nametoindex(ifname));
9297
9298 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009299 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009300 if (ret < 0) {
9301 wpa_printf(MSG_ERROR, "nl80211: NL80211_ATTR_STA_VLAN (addr="
9302 MACSTR " ifname=%s vlan_id=%d) failed: %d (%s)",
9303 MAC2STR(addr), ifname, vlan_id, ret,
9304 strerror(-ret));
9305 }
9306 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009307 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009308 return ret;
9309}
9310
9311
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009312static int i802_get_inact_sec(void *priv, const u8 *addr)
9313{
9314 struct hostap_sta_driver_data data;
9315 int ret;
9316
9317 data.inactive_msec = (unsigned long) -1;
9318 ret = i802_read_sta_data(priv, &data, addr);
9319 if (ret || data.inactive_msec == (unsigned long) -1)
9320 return -1;
9321 return data.inactive_msec / 1000;
9322}
9323
9324
9325static int i802_sta_clear_stats(void *priv, const u8 *addr)
9326{
9327#if 0
9328 /* TODO */
9329#endif
9330 return 0;
9331}
9332
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009333
9334static int i802_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
9335 int reason)
9336{
9337 struct i802_bss *bss = priv;
Dmitry Shmidt04949592012-07-19 12:16:46 -07009338 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009339 struct ieee80211_mgmt mgmt;
9340
Dmitry Shmidt04949592012-07-19 12:16:46 -07009341 if (drv->device_ap_sme)
9342 return wpa_driver_nl80211_sta_remove(bss, addr);
9343
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009344 memset(&mgmt, 0, sizeof(mgmt));
9345 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
9346 WLAN_FC_STYPE_DEAUTH);
9347 memcpy(mgmt.da, addr, ETH_ALEN);
9348 memcpy(mgmt.sa, own_addr, ETH_ALEN);
9349 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
9350 mgmt.u.deauth.reason_code = host_to_le16(reason);
9351 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
9352 IEEE80211_HDRLEN +
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009353 sizeof(mgmt.u.deauth), 0, 0, 0, 0,
9354 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009355}
9356
9357
9358static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
9359 int reason)
9360{
9361 struct i802_bss *bss = priv;
Dmitry Shmidt04949592012-07-19 12:16:46 -07009362 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009363 struct ieee80211_mgmt mgmt;
9364
Dmitry Shmidt04949592012-07-19 12:16:46 -07009365 if (drv->device_ap_sme)
9366 return wpa_driver_nl80211_sta_remove(bss, addr);
9367
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009368 memset(&mgmt, 0, sizeof(mgmt));
9369 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
9370 WLAN_FC_STYPE_DISASSOC);
9371 memcpy(mgmt.da, addr, ETH_ALEN);
9372 memcpy(mgmt.sa, own_addr, ETH_ALEN);
9373 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
9374 mgmt.u.disassoc.reason_code = host_to_le16(reason);
9375 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
9376 IEEE80211_HDRLEN +
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009377 sizeof(mgmt.u.disassoc), 0, 0, 0, 0,
9378 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009379}
9380
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009381
Jouni Malinen75ecf522011-06-27 15:19:46 -07009382static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
9383{
9384 int i;
9385 int *old;
9386
9387 wpa_printf(MSG_DEBUG, "nl80211: Add own interface ifindex %d",
9388 ifidx);
9389 for (i = 0; i < drv->num_if_indices; i++) {
9390 if (drv->if_indices[i] == 0) {
9391 drv->if_indices[i] = ifidx;
9392 return;
9393 }
9394 }
9395
9396 if (drv->if_indices != drv->default_if_indices)
9397 old = drv->if_indices;
9398 else
9399 old = NULL;
9400
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07009401 drv->if_indices = os_realloc_array(old, drv->num_if_indices + 1,
9402 sizeof(int));
Jouni Malinen75ecf522011-06-27 15:19:46 -07009403 if (!drv->if_indices) {
9404 if (!old)
9405 drv->if_indices = drv->default_if_indices;
9406 else
9407 drv->if_indices = old;
9408 wpa_printf(MSG_ERROR, "Failed to reallocate memory for "
9409 "interfaces");
9410 wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx);
9411 return;
9412 } else if (!old)
9413 os_memcpy(drv->if_indices, drv->default_if_indices,
9414 sizeof(drv->default_if_indices));
9415 drv->if_indices[drv->num_if_indices] = ifidx;
9416 drv->num_if_indices++;
9417}
9418
9419
9420static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
9421{
9422 int i;
9423
9424 for (i = 0; i < drv->num_if_indices; i++) {
9425 if (drv->if_indices[i] == ifidx) {
9426 drv->if_indices[i] = 0;
9427 break;
9428 }
9429 }
9430}
9431
9432
9433static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
9434{
9435 int i;
9436
9437 for (i = 0; i < drv->num_if_indices; i++)
9438 if (drv->if_indices[i] == ifidx)
9439 return 1;
9440
9441 return 0;
9442}
9443
9444
9445static int i802_set_wds_sta(void *priv, const u8 *addr, int aid, int val,
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07009446 const char *bridge_ifname, char *ifname_wds)
Jouni Malinen75ecf522011-06-27 15:19:46 -07009447{
9448 struct i802_bss *bss = priv;
9449 struct wpa_driver_nl80211_data *drv = bss->drv;
9450 char name[IFNAMSIZ + 1];
9451
9452 os_snprintf(name, sizeof(name), "%s.sta%d", bss->ifname, aid);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07009453 if (ifname_wds)
9454 os_strlcpy(ifname_wds, name, IFNAMSIZ + 1);
9455
Jouni Malinen75ecf522011-06-27 15:19:46 -07009456 wpa_printf(MSG_DEBUG, "nl80211: Set WDS STA addr=" MACSTR
9457 " aid=%d val=%d name=%s", MAC2STR(addr), aid, val, name);
9458 if (val) {
9459 if (!if_nametoindex(name)) {
9460 if (nl80211_create_iface(drv, name,
9461 NL80211_IFTYPE_AP_VLAN,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009462 bss->addr, 1, NULL, NULL, 0) <
9463 0)
Jouni Malinen75ecf522011-06-27 15:19:46 -07009464 return -1;
9465 if (bridge_ifname &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009466 linux_br_add_if(drv->global->ioctl_sock,
9467 bridge_ifname, name) < 0)
Jouni Malinen75ecf522011-06-27 15:19:46 -07009468 return -1;
9469 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07009470 if (linux_set_iface_flags(drv->global->ioctl_sock, name, 1)) {
9471 wpa_printf(MSG_ERROR, "nl80211: Failed to set WDS STA "
9472 "interface %s up", name);
9473 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07009474 return i802_set_sta_vlan(priv, addr, name, 0);
9475 } else {
Dmitry Shmidtaa532512012-09-24 10:35:31 -07009476 if (bridge_ifname)
9477 linux_br_del_if(drv->global->ioctl_sock, bridge_ifname,
9478 name);
9479
Jouni Malinen75ecf522011-06-27 15:19:46 -07009480 i802_set_sta_vlan(priv, addr, bss->ifname, 0);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08009481 nl80211_remove_iface(drv, if_nametoindex(name));
9482 return 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07009483 }
9484}
9485
9486
9487static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
9488{
9489 struct wpa_driver_nl80211_data *drv = eloop_ctx;
9490 struct sockaddr_ll lladdr;
9491 unsigned char buf[3000];
9492 int len;
9493 socklen_t fromlen = sizeof(lladdr);
9494
9495 len = recvfrom(sock, buf, sizeof(buf), 0,
9496 (struct sockaddr *)&lladdr, &fromlen);
9497 if (len < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07009498 wpa_printf(MSG_ERROR, "nl80211: EAPOL recv failed: %s",
9499 strerror(errno));
Jouni Malinen75ecf522011-06-27 15:19:46 -07009500 return;
9501 }
9502
9503 if (have_ifidx(drv, lladdr.sll_ifindex))
9504 drv_event_eapol_rx(drv->ctx, lladdr.sll_addr, buf, len);
9505}
9506
9507
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009508static int i802_check_bridge(struct wpa_driver_nl80211_data *drv,
9509 struct i802_bss *bss,
9510 const char *brname, const char *ifname)
9511{
9512 int ifindex;
9513 char in_br[IFNAMSIZ];
9514
9515 os_strlcpy(bss->brname, brname, IFNAMSIZ);
9516 ifindex = if_nametoindex(brname);
9517 if (ifindex == 0) {
9518 /*
9519 * Bridge was configured, but the bridge device does
9520 * not exist. Try to add it now.
9521 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009522 if (linux_br_add(drv->global->ioctl_sock, brname) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009523 wpa_printf(MSG_ERROR, "nl80211: Failed to add the "
9524 "bridge interface %s: %s",
9525 brname, strerror(errno));
9526 return -1;
9527 }
9528 bss->added_bridge = 1;
9529 add_ifidx(drv, if_nametoindex(brname));
9530 }
9531
9532 if (linux_br_get(in_br, ifname) == 0) {
9533 if (os_strcmp(in_br, brname) == 0)
9534 return 0; /* already in the bridge */
9535
9536 wpa_printf(MSG_DEBUG, "nl80211: Removing interface %s from "
9537 "bridge %s", ifname, in_br);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009538 if (linux_br_del_if(drv->global->ioctl_sock, in_br, ifname) <
9539 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009540 wpa_printf(MSG_ERROR, "nl80211: Failed to "
9541 "remove interface %s from bridge "
9542 "%s: %s",
9543 ifname, brname, strerror(errno));
9544 return -1;
9545 }
9546 }
9547
9548 wpa_printf(MSG_DEBUG, "nl80211: Adding interface %s into bridge %s",
9549 ifname, brname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009550 if (linux_br_add_if(drv->global->ioctl_sock, brname, ifname) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009551 wpa_printf(MSG_ERROR, "nl80211: Failed to add interface %s "
9552 "into bridge %s: %s",
9553 ifname, brname, strerror(errno));
9554 return -1;
9555 }
9556 bss->added_if_into_bridge = 1;
9557
9558 return 0;
9559}
9560
9561
9562static void *i802_init(struct hostapd_data *hapd,
9563 struct wpa_init_params *params)
9564{
9565 struct wpa_driver_nl80211_data *drv;
9566 struct i802_bss *bss;
9567 size_t i;
9568 char brname[IFNAMSIZ];
9569 int ifindex, br_ifindex;
9570 int br_added = 0;
9571
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08009572 bss = wpa_driver_nl80211_drv_init(hapd, params->ifname,
9573 params->global_priv, 1,
9574 params->bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009575 if (bss == NULL)
9576 return NULL;
9577
9578 drv = bss->drv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009579
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009580 if (linux_br_get(brname, params->ifname) == 0) {
9581 wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in bridge %s",
9582 params->ifname, brname);
9583 br_ifindex = if_nametoindex(brname);
9584 } else {
9585 brname[0] = '\0';
9586 br_ifindex = 0;
9587 }
9588
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009589 for (i = 0; i < params->num_bridge; i++) {
9590 if (params->bridge[i]) {
9591 ifindex = if_nametoindex(params->bridge[i]);
9592 if (ifindex)
9593 add_ifidx(drv, ifindex);
9594 if (ifindex == br_ifindex)
9595 br_added = 1;
9596 }
9597 }
9598 if (!br_added && br_ifindex &&
9599 (params->num_bridge == 0 || !params->bridge[0]))
9600 add_ifidx(drv, br_ifindex);
9601
9602 /* start listening for EAPOL on the default AP interface */
9603 add_ifidx(drv, drv->ifindex);
9604
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009605 if (params->num_bridge && params->bridge[0] &&
9606 i802_check_bridge(drv, bss, params->bridge[0], params->ifname) < 0)
9607 goto failed;
9608
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009609 drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
9610 if (drv->eapol_sock < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07009611 wpa_printf(MSG_ERROR, "nl80211: socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE) failed: %s",
9612 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009613 goto failed;
9614 }
9615
9616 if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
9617 {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07009618 wpa_printf(MSG_INFO, "nl80211: Could not register read socket for eapol");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009619 goto failed;
9620 }
9621
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009622 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
9623 params->own_addr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009624 goto failed;
9625
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009626 memcpy(bss->addr, params->own_addr, ETH_ALEN);
9627
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009628 return bss;
9629
9630failed:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009631 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009632 return NULL;
9633}
9634
9635
9636static void i802_deinit(void *priv)
9637{
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009638 struct i802_bss *bss = priv;
9639 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009640}
9641
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009642
9643static enum nl80211_iftype wpa_driver_nl80211_if_type(
9644 enum wpa_driver_if_type type)
9645{
9646 switch (type) {
9647 case WPA_IF_STATION:
9648 return NL80211_IFTYPE_STATION;
9649 case WPA_IF_P2P_CLIENT:
9650 case WPA_IF_P2P_GROUP:
9651 return NL80211_IFTYPE_P2P_CLIENT;
9652 case WPA_IF_AP_VLAN:
9653 return NL80211_IFTYPE_AP_VLAN;
9654 case WPA_IF_AP_BSS:
9655 return NL80211_IFTYPE_AP;
9656 case WPA_IF_P2P_GO:
9657 return NL80211_IFTYPE_P2P_GO;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009658 case WPA_IF_P2P_DEVICE:
9659 return NL80211_IFTYPE_P2P_DEVICE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009660 }
9661 return -1;
9662}
9663
9664
9665#ifdef CONFIG_P2P
9666
9667static int nl80211_addr_in_use(struct nl80211_global *global, const u8 *addr)
9668{
9669 struct wpa_driver_nl80211_data *drv;
9670 dl_list_for_each(drv, &global->interfaces,
9671 struct wpa_driver_nl80211_data, list) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009672 if (os_memcmp(addr, drv->first_bss->addr, ETH_ALEN) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009673 return 1;
9674 }
9675 return 0;
9676}
9677
9678
9679static int nl80211_p2p_interface_addr(struct wpa_driver_nl80211_data *drv,
9680 u8 *new_addr)
9681{
9682 unsigned int idx;
9683
9684 if (!drv->global)
9685 return -1;
9686
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009687 os_memcpy(new_addr, drv->first_bss->addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009688 for (idx = 0; idx < 64; idx++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009689 new_addr[0] = drv->first_bss->addr[0] | 0x02;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009690 new_addr[0] ^= idx << 2;
9691 if (!nl80211_addr_in_use(drv->global, new_addr))
9692 break;
9693 }
9694 if (idx == 64)
9695 return -1;
9696
9697 wpa_printf(MSG_DEBUG, "nl80211: Assigned new P2P Interface Address "
9698 MACSTR, MAC2STR(new_addr));
9699
9700 return 0;
9701}
9702
9703#endif /* CONFIG_P2P */
9704
9705
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009706struct wdev_info {
9707 u64 wdev_id;
9708 int wdev_id_set;
9709 u8 macaddr[ETH_ALEN];
9710};
9711
9712static int nl80211_wdev_handler(struct nl_msg *msg, void *arg)
9713{
9714 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
9715 struct nlattr *tb[NL80211_ATTR_MAX + 1];
9716 struct wdev_info *wi = arg;
9717
9718 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9719 genlmsg_attrlen(gnlh, 0), NULL);
9720 if (tb[NL80211_ATTR_WDEV]) {
9721 wi->wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
9722 wi->wdev_id_set = 1;
9723 }
9724
9725 if (tb[NL80211_ATTR_MAC])
9726 os_memcpy(wi->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
9727 ETH_ALEN);
9728
9729 return NL_SKIP;
9730}
9731
9732
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009733static int wpa_driver_nl80211_if_add(void *priv, enum wpa_driver_if_type type,
9734 const char *ifname, const u8 *addr,
9735 void *bss_ctx, void **drv_priv,
9736 char *force_ifname, u8 *if_addr,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009737 const char *bridge, int use_existing)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009738{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009739 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009740 struct i802_bss *bss = priv;
9741 struct wpa_driver_nl80211_data *drv = bss->drv;
9742 int ifidx;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009743 int added = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009744
9745 if (addr)
9746 os_memcpy(if_addr, addr, ETH_ALEN);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009747 nlmode = wpa_driver_nl80211_if_type(type);
9748 if (nlmode == NL80211_IFTYPE_P2P_DEVICE) {
9749 struct wdev_info p2pdev_info;
9750
9751 os_memset(&p2pdev_info, 0, sizeof(p2pdev_info));
9752 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
9753 0, nl80211_wdev_handler,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009754 &p2pdev_info, use_existing);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009755 if (!p2pdev_info.wdev_id_set || ifidx != 0) {
9756 wpa_printf(MSG_ERROR, "nl80211: Failed to create a P2P Device interface %s",
9757 ifname);
9758 return -1;
9759 }
9760
9761 drv->global->if_add_wdevid = p2pdev_info.wdev_id;
9762 drv->global->if_add_wdevid_set = p2pdev_info.wdev_id_set;
9763 if (!is_zero_ether_addr(p2pdev_info.macaddr))
9764 os_memcpy(if_addr, p2pdev_info.macaddr, ETH_ALEN);
9765 wpa_printf(MSG_DEBUG, "nl80211: New P2P Device interface %s (0x%llx) created",
9766 ifname,
9767 (long long unsigned int) p2pdev_info.wdev_id);
9768 } else {
9769 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009770 0, NULL, NULL, use_existing);
9771 if (use_existing && ifidx == -ENFILE) {
9772 added = 0;
9773 ifidx = if_nametoindex(ifname);
9774 } else if (ifidx < 0) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009775 return -1;
9776 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009777 }
9778
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009779 if (!addr) {
9780 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
9781 os_memcpy(if_addr, bss->addr, ETH_ALEN);
9782 else if (linux_get_ifhwaddr(drv->global->ioctl_sock,
9783 bss->ifname, if_addr) < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009784 if (added)
9785 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009786 return -1;
9787 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009788 }
9789
9790#ifdef CONFIG_P2P
9791 if (!addr &&
9792 (type == WPA_IF_P2P_CLIENT || type == WPA_IF_P2P_GROUP ||
9793 type == WPA_IF_P2P_GO)) {
9794 /* Enforce unique P2P Interface Address */
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009795 u8 new_addr[ETH_ALEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009796
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009797 if (linux_get_ifhwaddr(drv->global->ioctl_sock, ifname,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009798 new_addr) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009799 nl80211_remove_iface(drv, ifidx);
9800 return -1;
9801 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009802 if (nl80211_addr_in_use(drv->global, new_addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009803 wpa_printf(MSG_DEBUG, "nl80211: Allocate new address "
9804 "for P2P group interface");
9805 if (nl80211_p2p_interface_addr(drv, new_addr) < 0) {
9806 nl80211_remove_iface(drv, ifidx);
9807 return -1;
9808 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009809 if (linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009810 new_addr) < 0) {
9811 nl80211_remove_iface(drv, ifidx);
9812 return -1;
9813 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009814 }
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07009815 os_memcpy(if_addr, new_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009816 }
9817#endif /* CONFIG_P2P */
9818
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009819 if (type == WPA_IF_AP_BSS) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07009820 struct i802_bss *new_bss = os_zalloc(sizeof(*new_bss));
9821 if (new_bss == NULL) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009822 if (added)
9823 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07009824 return -1;
9825 }
9826
9827 if (bridge &&
9828 i802_check_bridge(drv, new_bss, bridge, ifname) < 0) {
9829 wpa_printf(MSG_ERROR, "nl80211: Failed to add the new "
9830 "interface %s to a bridge %s",
9831 ifname, bridge);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009832 if (added)
9833 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07009834 os_free(new_bss);
9835 return -1;
9836 }
9837
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009838 if (linux_set_iface_flags(drv->global->ioctl_sock, ifname, 1))
9839 {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009840 nl80211_remove_iface(drv, ifidx);
9841 os_free(new_bss);
9842 return -1;
9843 }
9844 os_strlcpy(new_bss->ifname, ifname, IFNAMSIZ);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009845 os_memcpy(new_bss->addr, if_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009846 new_bss->ifindex = ifidx;
9847 new_bss->drv = drv;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009848 new_bss->next = drv->first_bss->next;
9849 new_bss->freq = drv->first_bss->freq;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08009850 new_bss->ctx = bss_ctx;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009851 new_bss->added_if = added;
9852 drv->first_bss->next = new_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009853 if (drv_priv)
9854 *drv_priv = new_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009855 nl80211_init_bss(new_bss);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08009856
9857 /* Subscribe management frames for this WPA_IF_AP_BSS */
9858 if (nl80211_setup_ap(new_bss))
9859 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009860 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009861
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009862 if (drv->global)
9863 drv->global->if_add_ifindex = ifidx;
9864
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009865 return 0;
9866}
9867
9868
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009869static int wpa_driver_nl80211_if_remove(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009870 enum wpa_driver_if_type type,
9871 const char *ifname)
9872{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009873 struct wpa_driver_nl80211_data *drv = bss->drv;
9874 int ifindex = if_nametoindex(ifname);
9875
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009876 wpa_printf(MSG_DEBUG, "nl80211: %s(type=%d ifname=%s) ifindex=%d added_if=%d",
9877 __func__, type, ifname, ifindex, bss->added_if);
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08009878 if (ifindex > 0 && (bss->added_if || bss->ifindex != ifindex))
Dmitry Shmidt051af732013-10-22 13:52:46 -07009879 nl80211_remove_iface(drv, ifindex);
Dmitry Shmidtaa532512012-09-24 10:35:31 -07009880
Dmitry Shmidtaa532512012-09-24 10:35:31 -07009881 if (type != WPA_IF_AP_BSS)
9882 return 0;
9883
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009884 if (bss->added_if_into_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009885 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
9886 bss->ifname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009887 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
9888 "interface %s from bridge %s: %s",
9889 bss->ifname, bss->brname, strerror(errno));
9890 }
9891 if (bss->added_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009892 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009893 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
9894 "bridge %s: %s",
9895 bss->brname, strerror(errno));
9896 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009897
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009898 if (bss != drv->first_bss) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009899 struct i802_bss *tbss;
9900
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009901 wpa_printf(MSG_DEBUG, "nl80211: Not the first BSS - remove it");
9902 for (tbss = drv->first_bss; tbss; tbss = tbss->next) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009903 if (tbss->next == bss) {
9904 tbss->next = bss->next;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08009905 /* Unsubscribe management frames */
9906 nl80211_teardown_ap(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009907 nl80211_destroy_bss(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009908 os_free(bss);
9909 bss = NULL;
9910 break;
9911 }
9912 }
9913 if (bss)
9914 wpa_printf(MSG_INFO, "nl80211: %s - could not find "
9915 "BSS %p in the list", __func__, bss);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009916 } else {
9917 wpa_printf(MSG_DEBUG, "nl80211: First BSS - reassign context");
9918 nl80211_teardown_ap(bss);
9919 if (!bss->added_if && !drv->first_bss->next)
9920 wpa_driver_nl80211_del_beacon(drv);
9921 nl80211_destroy_bss(bss);
9922 if (!bss->added_if)
9923 i802_set_iface_flags(bss, 0);
9924 if (drv->first_bss->next) {
9925 drv->first_bss = drv->first_bss->next;
9926 drv->ctx = drv->first_bss->ctx;
9927 os_free(bss);
9928 } else {
9929 wpa_printf(MSG_DEBUG, "nl80211: No second BSS to reassign context to");
9930 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009931 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009932
9933 return 0;
9934}
9935
9936
9937static int cookie_handler(struct nl_msg *msg, void *arg)
9938{
9939 struct nlattr *tb[NL80211_ATTR_MAX + 1];
9940 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
9941 u64 *cookie = arg;
9942 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9943 genlmsg_attrlen(gnlh, 0), NULL);
9944 if (tb[NL80211_ATTR_COOKIE])
9945 *cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
9946 return NL_SKIP;
9947}
9948
9949
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009950static int nl80211_send_frame_cmd(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009951 unsigned int freq, unsigned int wait,
9952 const u8 *buf, size_t buf_len,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009953 u64 *cookie_out, int no_cck, int no_ack,
9954 int offchanok)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009955{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009956 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009957 struct nl_msg *msg;
9958 u64 cookie;
9959 int ret = -1;
9960
9961 msg = nlmsg_alloc();
9962 if (!msg)
9963 return -1;
9964
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07009965 wpa_printf(MSG_MSGDUMP, "nl80211: CMD_FRAME freq=%u wait=%u no_cck=%d "
Dmitry Shmidt04949592012-07-19 12:16:46 -07009966 "no_ack=%d offchanok=%d",
9967 freq, wait, no_cck, no_ack, offchanok);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07009968 wpa_hexdump(MSG_MSGDUMP, "CMD_FRAME", buf, buf_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009969 nl80211_cmd(drv, msg, 0, NL80211_CMD_FRAME);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009970
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009971 if (nl80211_set_iface_id(msg, bss) < 0)
9972 goto nla_put_failure;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07009973 if (freq)
9974 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07009975 if (wait)
9976 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, wait);
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08009977 if (offchanok && ((drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
9978 drv->test_use_roc_tx))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009979 NLA_PUT_FLAG(msg, NL80211_ATTR_OFFCHANNEL_TX_OK);
9980 if (no_cck)
9981 NLA_PUT_FLAG(msg, NL80211_ATTR_TX_NO_CCK_RATE);
9982 if (no_ack)
9983 NLA_PUT_FLAG(msg, NL80211_ATTR_DONT_WAIT_FOR_ACK);
9984
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009985 NLA_PUT(msg, NL80211_ATTR_FRAME, buf_len, buf);
9986
9987 cookie = 0;
9988 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
9989 msg = NULL;
9990 if (ret) {
9991 wpa_printf(MSG_DEBUG, "nl80211: Frame command failed: ret=%d "
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07009992 "(%s) (freq=%u wait=%u)", ret, strerror(-ret),
9993 freq, wait);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009994 goto nla_put_failure;
9995 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07009996 wpa_printf(MSG_MSGDUMP, "nl80211: Frame TX command accepted%s; "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009997 "cookie 0x%llx", no_ack ? " (no ACK)" : "",
9998 (long long unsigned int) cookie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009999
10000 if (cookie_out)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010001 *cookie_out = no_ack ? (u64) -1 : cookie;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010002
10003nla_put_failure:
10004 nlmsg_free(msg);
10005 return ret;
10006}
10007
10008
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010009static int wpa_driver_nl80211_send_action(struct i802_bss *bss,
10010 unsigned int freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010011 unsigned int wait_time,
10012 const u8 *dst, const u8 *src,
10013 const u8 *bssid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010014 const u8 *data, size_t data_len,
10015 int no_cck)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010016{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010017 struct wpa_driver_nl80211_data *drv = bss->drv;
10018 int ret = -1;
10019 u8 *buf;
10020 struct ieee80211_hdr *hdr;
10021
10022 wpa_printf(MSG_DEBUG, "nl80211: Send Action frame (ifindex=%d, "
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080010023 "freq=%u MHz wait=%d ms no_cck=%d)",
10024 drv->ifindex, freq, wait_time, no_cck);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010025
10026 buf = os_zalloc(24 + data_len);
10027 if (buf == NULL)
10028 return ret;
10029 os_memcpy(buf + 24, data, data_len);
10030 hdr = (struct ieee80211_hdr *) buf;
10031 hdr->frame_control =
10032 IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_ACTION);
10033 os_memcpy(hdr->addr1, dst, ETH_ALEN);
10034 os_memcpy(hdr->addr2, src, ETH_ALEN);
10035 os_memcpy(hdr->addr3, bssid, ETH_ALEN);
10036
Dmitry Shmidt56052862013-10-04 10:23:25 -070010037 if (is_ap_interface(drv->nlmode) &&
10038 (!(drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
10039 (int) freq == bss->freq || drv->device_ap_sme ||
10040 !drv->use_monitor))
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010041 ret = wpa_driver_nl80211_send_mlme(bss, buf, 24 + data_len,
10042 0, freq, no_cck, 1,
10043 wait_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010044 else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010045 ret = nl80211_send_frame_cmd(bss, freq, wait_time, buf,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010046 24 + data_len,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010047 &drv->send_action_cookie,
10048 no_cck, 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010049
10050 os_free(buf);
10051 return ret;
10052}
10053
10054
10055static void wpa_driver_nl80211_send_action_cancel_wait(void *priv)
10056{
10057 struct i802_bss *bss = priv;
10058 struct wpa_driver_nl80211_data *drv = bss->drv;
10059 struct nl_msg *msg;
10060 int ret;
10061
10062 msg = nlmsg_alloc();
10063 if (!msg)
10064 return;
10065
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -080010066 wpa_printf(MSG_DEBUG, "nl80211: Cancel TX frame wait: cookie=0x%llx",
10067 (long long unsigned int) drv->send_action_cookie);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010068 nl80211_cmd(drv, msg, 0, NL80211_CMD_FRAME_WAIT_CANCEL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010069
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010070 if (nl80211_set_iface_id(msg, bss) < 0)
10071 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010072 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->send_action_cookie);
10073
10074 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
10075 msg = NULL;
10076 if (ret)
10077 wpa_printf(MSG_DEBUG, "nl80211: wait cancel failed: ret=%d "
10078 "(%s)", ret, strerror(-ret));
10079
10080 nla_put_failure:
10081 nlmsg_free(msg);
10082}
10083
10084
10085static int wpa_driver_nl80211_remain_on_channel(void *priv, unsigned int freq,
10086 unsigned int duration)
10087{
10088 struct i802_bss *bss = priv;
10089 struct wpa_driver_nl80211_data *drv = bss->drv;
10090 struct nl_msg *msg;
10091 int ret;
10092 u64 cookie;
10093
10094 msg = nlmsg_alloc();
10095 if (!msg)
10096 return -1;
10097
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010098 nl80211_cmd(drv, msg, 0, NL80211_CMD_REMAIN_ON_CHANNEL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010099
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010100 if (nl80211_set_iface_id(msg, bss) < 0)
10101 goto nla_put_failure;
10102
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010103 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
10104 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
10105
10106 cookie = 0;
10107 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010108 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010109 if (ret == 0) {
10110 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel cookie "
10111 "0x%llx for freq=%u MHz duration=%u",
10112 (long long unsigned int) cookie, freq, duration);
10113 drv->remain_on_chan_cookie = cookie;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010114 drv->pending_remain_on_chan = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010115 return 0;
10116 }
10117 wpa_printf(MSG_DEBUG, "nl80211: Failed to request remain-on-channel "
10118 "(freq=%d duration=%u): %d (%s)",
10119 freq, duration, ret, strerror(-ret));
10120nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010121 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010122 return -1;
10123}
10124
10125
10126static int wpa_driver_nl80211_cancel_remain_on_channel(void *priv)
10127{
10128 struct i802_bss *bss = priv;
10129 struct wpa_driver_nl80211_data *drv = bss->drv;
10130 struct nl_msg *msg;
10131 int ret;
10132
10133 if (!drv->pending_remain_on_chan) {
10134 wpa_printf(MSG_DEBUG, "nl80211: No pending remain-on-channel "
10135 "to cancel");
10136 return -1;
10137 }
10138
10139 wpa_printf(MSG_DEBUG, "nl80211: Cancel remain-on-channel with cookie "
10140 "0x%llx",
10141 (long long unsigned int) drv->remain_on_chan_cookie);
10142
10143 msg = nlmsg_alloc();
10144 if (!msg)
10145 return -1;
10146
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010147 nl80211_cmd(drv, msg, 0, NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL);
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;
10151
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010152 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->remain_on_chan_cookie);
10153
10154 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010155 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010156 if (ret == 0)
10157 return 0;
10158 wpa_printf(MSG_DEBUG, "nl80211: Failed to cancel remain-on-channel: "
10159 "%d (%s)", ret, strerror(-ret));
10160nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010161 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010162 return -1;
10163}
10164
10165
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010166static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss, int report)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010167{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010168 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt6e933c12011-09-27 12:29:26 -070010169
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010170 if (!report) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010171 if (bss->nl_preq && drv->device_ap_sme &&
10172 is_ap_interface(drv->nlmode)) {
10173 /*
10174 * Do not disable Probe Request reporting that was
10175 * enabled in nl80211_setup_ap().
10176 */
10177 wpa_printf(MSG_DEBUG, "nl80211: Skip disabling of "
10178 "Probe Request reporting nl_preq=%p while "
10179 "in AP mode", bss->nl_preq);
10180 } else if (bss->nl_preq) {
10181 wpa_printf(MSG_DEBUG, "nl80211: Disable Probe Request "
10182 "reporting nl_preq=%p", bss->nl_preq);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070010183 nl80211_destroy_eloop_handle(&bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010184 }
10185 return 0;
10186 }
10187
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010188 if (bss->nl_preq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010189 wpa_printf(MSG_DEBUG, "nl80211: Probe Request reporting "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010190 "already on! nl_preq=%p", bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010191 return 0;
10192 }
10193
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010194 bss->nl_preq = nl_create_handle(drv->global->nl_cb, "preq");
10195 if (bss->nl_preq == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010196 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010197 wpa_printf(MSG_DEBUG, "nl80211: Enable Probe Request "
10198 "reporting nl_preq=%p", bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010199
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010200 if (nl80211_register_frame(bss, bss->nl_preq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010201 (WLAN_FC_TYPE_MGMT << 2) |
10202 (WLAN_FC_STYPE_PROBE_REQ << 4),
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010203 NULL, 0) < 0)
10204 goto out_err;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -070010205
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070010206 nl80211_register_eloop_read(&bss->nl_preq,
10207 wpa_driver_nl80211_event_receive,
10208 bss->nl_cb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010209
10210 return 0;
10211
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010212 out_err:
10213 nl_destroy_handles(&bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010214 return -1;
10215}
10216
10217
10218static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
10219 int ifindex, int disabled)
10220{
10221 struct nl_msg *msg;
10222 struct nlattr *bands, *band;
10223 int ret;
10224
10225 msg = nlmsg_alloc();
10226 if (!msg)
10227 return -1;
10228
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010229 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_TX_BITRATE_MASK);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010230 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
10231
10232 bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES);
10233 if (!bands)
10234 goto nla_put_failure;
10235
10236 /*
10237 * Disable 2 GHz rates 1, 2, 5.5, 11 Mbps by masking out everything
10238 * else apart from 6, 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS
10239 * rates. All 5 GHz rates are left enabled.
10240 */
10241 band = nla_nest_start(msg, NL80211_BAND_2GHZ);
10242 if (!band)
10243 goto nla_put_failure;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010244 if (disabled) {
10245 NLA_PUT(msg, NL80211_TXRATE_LEGACY, 8,
10246 "\x0c\x12\x18\x24\x30\x48\x60\x6c");
10247 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010248 nla_nest_end(msg, band);
10249
10250 nla_nest_end(msg, bands);
10251
10252 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
10253 msg = NULL;
10254 if (ret) {
10255 wpa_printf(MSG_DEBUG, "nl80211: Set TX rates failed: ret=%d "
10256 "(%s)", ret, strerror(-ret));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070010257 } else
10258 drv->disabled_11b_rates = disabled;
10259
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010260 return ret;
10261
10262nla_put_failure:
10263 nlmsg_free(msg);
10264 return -1;
10265}
10266
10267
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010268static int wpa_driver_nl80211_deinit_ap(void *priv)
10269{
10270 struct i802_bss *bss = priv;
10271 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010272 if (!is_ap_interface(drv->nlmode))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010273 return -1;
10274 wpa_driver_nl80211_del_beacon(drv);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070010275
10276 /*
10277 * If the P2P GO interface was dynamically added, then it is
10278 * possible that the interface change to station is not possible.
10279 */
10280 if (drv->nlmode == NL80211_IFTYPE_P2P_GO && bss->if_dynamic)
10281 return 0;
10282
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010283 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010284}
10285
10286
Dmitry Shmidtea69e842013-05-13 14:52:28 -070010287static int wpa_driver_nl80211_stop_ap(void *priv)
10288{
10289 struct i802_bss *bss = priv;
10290 struct wpa_driver_nl80211_data *drv = bss->drv;
10291 if (!is_ap_interface(drv->nlmode))
10292 return -1;
10293 wpa_driver_nl80211_del_beacon(drv);
10294 bss->beacon_set = 0;
10295 return 0;
10296}
10297
10298
Dmitry Shmidt04949592012-07-19 12:16:46 -070010299static int wpa_driver_nl80211_deinit_p2p_cli(void *priv)
10300{
10301 struct i802_bss *bss = priv;
10302 struct wpa_driver_nl80211_data *drv = bss->drv;
10303 if (drv->nlmode != NL80211_IFTYPE_P2P_CLIENT)
10304 return -1;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070010305
10306 /*
10307 * If the P2P Client interface was dynamically added, then it is
10308 * possible that the interface change to station is not possible.
10309 */
10310 if (bss->if_dynamic)
10311 return 0;
10312
Dmitry Shmidt04949592012-07-19 12:16:46 -070010313 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
10314}
10315
10316
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010317static void wpa_driver_nl80211_resume(void *priv)
10318{
10319 struct i802_bss *bss = priv;
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010320
10321 if (i802_set_iface_flags(bss, 1))
10322 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface up on resume event");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010323}
10324
10325
10326static int nl80211_send_ft_action(void *priv, u8 action, const u8 *target_ap,
10327 const u8 *ies, size_t ies_len)
10328{
10329 struct i802_bss *bss = priv;
10330 struct wpa_driver_nl80211_data *drv = bss->drv;
10331 int ret;
10332 u8 *data, *pos;
10333 size_t data_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010334 const u8 *own_addr = bss->addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010335
10336 if (action != 1) {
10337 wpa_printf(MSG_ERROR, "nl80211: Unsupported send_ft_action "
10338 "action %d", action);
10339 return -1;
10340 }
10341
10342 /*
10343 * Action frame payload:
10344 * Category[1] = 6 (Fast BSS Transition)
10345 * Action[1] = 1 (Fast BSS Transition Request)
10346 * STA Address
10347 * Target AP Address
10348 * FT IEs
10349 */
10350
10351 data_len = 2 + 2 * ETH_ALEN + ies_len;
10352 data = os_malloc(data_len);
10353 if (data == NULL)
10354 return -1;
10355 pos = data;
10356 *pos++ = 0x06; /* FT Action category */
10357 *pos++ = action;
10358 os_memcpy(pos, own_addr, ETH_ALEN);
10359 pos += ETH_ALEN;
10360 os_memcpy(pos, target_ap, ETH_ALEN);
10361 pos += ETH_ALEN;
10362 os_memcpy(pos, ies, ies_len);
10363
10364 ret = wpa_driver_nl80211_send_action(bss, drv->assoc_freq, 0,
10365 drv->bssid, own_addr, drv->bssid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010366 data, data_len, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010367 os_free(data);
10368
10369 return ret;
10370}
10371
10372
10373static int nl80211_signal_monitor(void *priv, int threshold, int hysteresis)
10374{
10375 struct i802_bss *bss = priv;
10376 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -070010377 struct nl_msg *msg;
10378 struct nlattr *cqm;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070010379 int ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010380
10381 wpa_printf(MSG_DEBUG, "nl80211: Signal monitor threshold=%d "
10382 "hysteresis=%d", threshold, hysteresis);
10383
10384 msg = nlmsg_alloc();
10385 if (!msg)
10386 return -1;
10387
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010388 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_CQM);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010389
10390 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
10391
Dmitry Shmidt8da800a2013-04-24 12:57:01 -070010392 cqm = nla_nest_start(msg, NL80211_ATTR_CQM);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010393 if (cqm == NULL)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070010394 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010395
Dmitry Shmidt8da800a2013-04-24 12:57:01 -070010396 NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_THOLD, threshold);
10397 NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_HYST, hysteresis);
10398 nla_nest_end(msg, cqm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010399
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070010400 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010401 msg = NULL;
10402
10403nla_put_failure:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010404 nlmsg_free(msg);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070010405 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010406}
10407
10408
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010409static int get_channel_width(struct nl_msg *msg, void *arg)
10410{
10411 struct nlattr *tb[NL80211_ATTR_MAX + 1];
10412 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
10413 struct wpa_signal_info *sig_change = arg;
10414
10415 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
10416 genlmsg_attrlen(gnlh, 0), NULL);
10417
10418 sig_change->center_frq1 = -1;
10419 sig_change->center_frq2 = -1;
10420 sig_change->chanwidth = CHAN_WIDTH_UNKNOWN;
10421
10422 if (tb[NL80211_ATTR_CHANNEL_WIDTH]) {
10423 sig_change->chanwidth = convert2width(
10424 nla_get_u32(tb[NL80211_ATTR_CHANNEL_WIDTH]));
10425 if (tb[NL80211_ATTR_CENTER_FREQ1])
10426 sig_change->center_frq1 =
10427 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]);
10428 if (tb[NL80211_ATTR_CENTER_FREQ2])
10429 sig_change->center_frq2 =
10430 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]);
10431 }
10432
10433 return NL_SKIP;
10434}
10435
10436
10437static int nl80211_get_channel_width(struct wpa_driver_nl80211_data *drv,
10438 struct wpa_signal_info *sig)
10439{
10440 struct nl_msg *msg;
10441
10442 msg = nlmsg_alloc();
10443 if (!msg)
10444 return -ENOMEM;
10445
10446 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_INTERFACE);
10447 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
10448
10449 return send_and_recv_msgs(drv, msg, get_channel_width, sig);
10450
10451nla_put_failure:
10452 nlmsg_free(msg);
10453 return -ENOBUFS;
10454}
10455
10456
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010457static int nl80211_signal_poll(void *priv, struct wpa_signal_info *si)
10458{
10459 struct i802_bss *bss = priv;
10460 struct wpa_driver_nl80211_data *drv = bss->drv;
10461 int res;
10462
10463 os_memset(si, 0, sizeof(*si));
10464 res = nl80211_get_link_signal(drv, si);
10465 if (res != 0)
10466 return res;
10467
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010468 res = nl80211_get_channel_width(drv, si);
10469 if (res != 0)
10470 return res;
10471
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010472 return nl80211_get_link_noise(drv, si);
10473}
10474
10475
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010476static int wpa_driver_nl80211_shared_freq(void *priv)
10477{
10478 struct i802_bss *bss = priv;
10479 struct wpa_driver_nl80211_data *drv = bss->drv;
10480 struct wpa_driver_nl80211_data *driver;
10481 int freq = 0;
10482
10483 /*
10484 * If the same PHY is in connected state with some other interface,
10485 * then retrieve the assoc freq.
10486 */
10487 wpa_printf(MSG_DEBUG, "nl80211: Get shared freq for PHY %s",
10488 drv->phyname);
10489
10490 dl_list_for_each(driver, &drv->global->interfaces,
10491 struct wpa_driver_nl80211_data, list) {
10492 if (drv == driver ||
10493 os_strcmp(drv->phyname, driver->phyname) != 0 ||
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010494 !driver->associated)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010495 continue;
10496
10497 wpa_printf(MSG_DEBUG, "nl80211: Found a match for PHY %s - %s "
10498 MACSTR,
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010499 driver->phyname, driver->first_bss->ifname,
10500 MAC2STR(driver->first_bss->addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -070010501 if (is_ap_interface(driver->nlmode))
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010502 freq = driver->first_bss->freq;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010503 else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010504 freq = nl80211_get_assoc_freq(driver);
10505 wpa_printf(MSG_DEBUG, "nl80211: Shared freq for PHY %s: %d",
10506 drv->phyname, freq);
10507 }
10508
10509 if (!freq)
10510 wpa_printf(MSG_DEBUG, "nl80211: No shared interface for "
10511 "PHY (%s) in associated state", drv->phyname);
10512
10513 return freq;
10514}
10515
10516
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010517static int nl80211_send_frame(void *priv, const u8 *data, size_t data_len,
10518 int encrypt)
10519{
10520 struct i802_bss *bss = priv;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080010521 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt, 0,
10522 0, 0, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010523}
10524
10525
10526static int nl80211_set_param(void *priv, const char *param)
10527{
10528 wpa_printf(MSG_DEBUG, "nl80211: driver param='%s'", param);
10529 if (param == NULL)
10530 return 0;
10531
10532#ifdef CONFIG_P2P
10533 if (os_strstr(param, "use_p2p_group_interface=1")) {
10534 struct i802_bss *bss = priv;
10535 struct wpa_driver_nl80211_data *drv = bss->drv;
10536
10537 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
10538 "interface");
10539 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
10540 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
10541 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010542
10543 if (os_strstr(param, "p2p_device=1")) {
10544 struct i802_bss *bss = priv;
10545 struct wpa_driver_nl80211_data *drv = bss->drv;
10546 drv->allow_p2p_device = 1;
10547 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010548#endif /* CONFIG_P2P */
10549
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080010550 if (os_strstr(param, "use_monitor=1")) {
10551 struct i802_bss *bss = priv;
10552 struct wpa_driver_nl80211_data *drv = bss->drv;
10553 drv->use_monitor = 1;
10554 }
10555
10556 if (os_strstr(param, "force_connect_cmd=1")) {
10557 struct i802_bss *bss = priv;
10558 struct wpa_driver_nl80211_data *drv = bss->drv;
10559 drv->capa.flags &= ~WPA_DRIVER_FLAGS_SME;
10560 }
10561
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -080010562 if (os_strstr(param, "no_offchannel_tx=1")) {
10563 struct i802_bss *bss = priv;
10564 struct wpa_driver_nl80211_data *drv = bss->drv;
10565 drv->capa.flags &= ~WPA_DRIVER_FLAGS_OFFCHANNEL_TX;
10566 drv->test_use_roc_tx = 1;
10567 }
10568
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010569 return 0;
10570}
10571
10572
10573static void * nl80211_global_init(void)
10574{
10575 struct nl80211_global *global;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010576 struct netlink_config *cfg;
10577
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010578 global = os_zalloc(sizeof(*global));
10579 if (global == NULL)
10580 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010581 global->ioctl_sock = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010582 dl_list_init(&global->interfaces);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010583 global->if_add_ifindex = -1;
10584
10585 cfg = os_zalloc(sizeof(*cfg));
10586 if (cfg == NULL)
10587 goto err;
10588
10589 cfg->ctx = global;
10590 cfg->newlink_cb = wpa_driver_nl80211_event_rtm_newlink;
10591 cfg->dellink_cb = wpa_driver_nl80211_event_rtm_dellink;
10592 global->netlink = netlink_init(cfg);
10593 if (global->netlink == NULL) {
10594 os_free(cfg);
10595 goto err;
10596 }
10597
10598 if (wpa_driver_nl80211_init_nl_global(global) < 0)
10599 goto err;
10600
10601 global->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
10602 if (global->ioctl_sock < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070010603 wpa_printf(MSG_ERROR, "nl80211: socket(PF_INET,SOCK_DGRAM) failed: %s",
10604 strerror(errno));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010605 goto err;
10606 }
10607
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010608 return global;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010609
10610err:
10611 nl80211_global_deinit(global);
10612 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010613}
10614
10615
10616static void nl80211_global_deinit(void *priv)
10617{
10618 struct nl80211_global *global = priv;
10619 if (global == NULL)
10620 return;
10621 if (!dl_list_empty(&global->interfaces)) {
10622 wpa_printf(MSG_ERROR, "nl80211: %u interface(s) remain at "
10623 "nl80211_global_deinit",
10624 dl_list_len(&global->interfaces));
10625 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010626
10627 if (global->netlink)
10628 netlink_deinit(global->netlink);
10629
10630 nl_destroy_handles(&global->nl);
10631
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070010632 if (global->nl_event)
10633 nl80211_destroy_eloop_handle(&global->nl_event);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010634
10635 nl_cb_put(global->nl_cb);
10636
10637 if (global->ioctl_sock >= 0)
10638 close(global->ioctl_sock);
10639
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010640 os_free(global);
10641}
10642
10643
10644static const char * nl80211_get_radio_name(void *priv)
10645{
10646 struct i802_bss *bss = priv;
10647 struct wpa_driver_nl80211_data *drv = bss->drv;
10648 return drv->phyname;
10649}
10650
10651
Jouni Malinen75ecf522011-06-27 15:19:46 -070010652static int nl80211_pmkid(struct i802_bss *bss, int cmd, const u8 *bssid,
10653 const u8 *pmkid)
10654{
10655 struct nl_msg *msg;
10656
10657 msg = nlmsg_alloc();
10658 if (!msg)
10659 return -ENOMEM;
10660
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010661 nl80211_cmd(bss->drv, msg, 0, cmd);
Jouni Malinen75ecf522011-06-27 15:19:46 -070010662
10663 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
10664 if (pmkid)
10665 NLA_PUT(msg, NL80211_ATTR_PMKID, 16, pmkid);
10666 if (bssid)
10667 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
10668
10669 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
10670 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010671 nlmsg_free(msg);
Jouni Malinen75ecf522011-06-27 15:19:46 -070010672 return -ENOBUFS;
10673}
10674
10675
10676static int nl80211_add_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
10677{
10678 struct i802_bss *bss = priv;
10679 wpa_printf(MSG_DEBUG, "nl80211: Add PMKID for " MACSTR, MAC2STR(bssid));
10680 return nl80211_pmkid(bss, NL80211_CMD_SET_PMKSA, bssid, pmkid);
10681}
10682
10683
10684static int nl80211_remove_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
10685{
10686 struct i802_bss *bss = priv;
10687 wpa_printf(MSG_DEBUG, "nl80211: Delete PMKID for " MACSTR,
10688 MAC2STR(bssid));
10689 return nl80211_pmkid(bss, NL80211_CMD_DEL_PMKSA, bssid, pmkid);
10690}
10691
10692
10693static int nl80211_flush_pmkid(void *priv)
10694{
10695 struct i802_bss *bss = priv;
10696 wpa_printf(MSG_DEBUG, "nl80211: Flush PMKIDs");
10697 return nl80211_pmkid(bss, NL80211_CMD_FLUSH_PMKSA, NULL, NULL);
10698}
10699
10700
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070010701static void clean_survey_results(struct survey_results *survey_results)
10702{
10703 struct freq_survey *survey, *tmp;
10704
10705 if (dl_list_empty(&survey_results->survey_list))
10706 return;
10707
10708 dl_list_for_each_safe(survey, tmp, &survey_results->survey_list,
10709 struct freq_survey, list) {
10710 dl_list_del(&survey->list);
10711 os_free(survey);
10712 }
10713}
10714
10715
10716static void add_survey(struct nlattr **sinfo, u32 ifidx,
10717 struct dl_list *survey_list)
10718{
10719 struct freq_survey *survey;
10720
10721 survey = os_zalloc(sizeof(struct freq_survey));
10722 if (!survey)
10723 return;
10724
10725 survey->ifidx = ifidx;
10726 survey->freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
10727 survey->filled = 0;
10728
10729 if (sinfo[NL80211_SURVEY_INFO_NOISE]) {
10730 survey->nf = (int8_t)
10731 nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
10732 survey->filled |= SURVEY_HAS_NF;
10733 }
10734
10735 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]) {
10736 survey->channel_time =
10737 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]);
10738 survey->filled |= SURVEY_HAS_CHAN_TIME;
10739 }
10740
10741 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]) {
10742 survey->channel_time_busy =
10743 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]);
10744 survey->filled |= SURVEY_HAS_CHAN_TIME_BUSY;
10745 }
10746
10747 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]) {
10748 survey->channel_time_rx =
10749 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]);
10750 survey->filled |= SURVEY_HAS_CHAN_TIME_RX;
10751 }
10752
10753 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]) {
10754 survey->channel_time_tx =
10755 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]);
10756 survey->filled |= SURVEY_HAS_CHAN_TIME_TX;
10757 }
10758
10759 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)",
10760 survey->freq,
10761 survey->nf,
10762 (unsigned long int) survey->channel_time,
10763 (unsigned long int) survey->channel_time_busy,
10764 (unsigned long int) survey->channel_time_tx,
10765 (unsigned long int) survey->channel_time_rx,
10766 survey->filled);
10767
10768 dl_list_add_tail(survey_list, &survey->list);
10769}
10770
10771
10772static int check_survey_ok(struct nlattr **sinfo, u32 surveyed_freq,
10773 unsigned int freq_filter)
10774{
10775 if (!freq_filter)
10776 return 1;
10777
10778 return freq_filter == surveyed_freq;
10779}
10780
10781
10782static int survey_handler(struct nl_msg *msg, void *arg)
10783{
10784 struct nlattr *tb[NL80211_ATTR_MAX + 1];
10785 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
10786 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
10787 struct survey_results *survey_results;
10788 u32 surveyed_freq = 0;
10789 u32 ifidx;
10790
10791 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
10792 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
10793 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
10794 };
10795
10796 survey_results = (struct survey_results *) arg;
10797
10798 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
10799 genlmsg_attrlen(gnlh, 0), NULL);
10800
Dmitry Shmidt97672262014-02-03 13:02:54 -080010801 if (!tb[NL80211_ATTR_IFINDEX])
10802 return NL_SKIP;
10803
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070010804 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
10805
10806 if (!tb[NL80211_ATTR_SURVEY_INFO])
10807 return NL_SKIP;
10808
10809 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
10810 tb[NL80211_ATTR_SURVEY_INFO],
10811 survey_policy))
10812 return NL_SKIP;
10813
10814 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY]) {
10815 wpa_printf(MSG_ERROR, "nl80211: Invalid survey data");
10816 return NL_SKIP;
10817 }
10818
10819 surveyed_freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
10820
10821 if (!check_survey_ok(sinfo, surveyed_freq,
10822 survey_results->freq_filter))
10823 return NL_SKIP;
10824
10825 if (survey_results->freq_filter &&
10826 survey_results->freq_filter != surveyed_freq) {
10827 wpa_printf(MSG_EXCESSIVE, "nl80211: Ignoring survey data for freq %d MHz",
10828 surveyed_freq);
10829 return NL_SKIP;
10830 }
10831
10832 add_survey(sinfo, ifidx, &survey_results->survey_list);
10833
10834 return NL_SKIP;
10835}
10836
10837
10838static int wpa_driver_nl80211_get_survey(void *priv, unsigned int freq)
10839{
10840 struct i802_bss *bss = priv;
10841 struct wpa_driver_nl80211_data *drv = bss->drv;
10842 struct nl_msg *msg;
10843 int err = -ENOBUFS;
10844 union wpa_event_data data;
10845 struct survey_results *survey_results;
10846
10847 os_memset(&data, 0, sizeof(data));
10848 survey_results = &data.survey_results;
10849
10850 dl_list_init(&survey_results->survey_list);
10851
10852 msg = nlmsg_alloc();
10853 if (!msg)
10854 goto nla_put_failure;
10855
10856 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
10857
10858 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
10859
10860 if (freq)
10861 data.survey_results.freq_filter = freq;
10862
10863 do {
10864 wpa_printf(MSG_DEBUG, "nl80211: Fetch survey data");
10865 err = send_and_recv_msgs(drv, msg, survey_handler,
10866 survey_results);
10867 } while (err > 0);
10868
10869 if (err) {
10870 wpa_printf(MSG_ERROR, "nl80211: Failed to process survey data");
10871 goto out_clean;
10872 }
10873
10874 wpa_supplicant_event(drv->ctx, EVENT_SURVEY, &data);
10875
10876out_clean:
10877 clean_survey_results(survey_results);
10878nla_put_failure:
10879 return err;
10880}
10881
10882
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010883static void nl80211_set_rekey_info(void *priv, const u8 *kek, const u8 *kck,
10884 const u8 *replay_ctr)
10885{
10886 struct i802_bss *bss = priv;
10887 struct wpa_driver_nl80211_data *drv = bss->drv;
10888 struct nlattr *replay_nested;
10889 struct nl_msg *msg;
10890
10891 msg = nlmsg_alloc();
10892 if (!msg)
10893 return;
10894
10895 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
10896
10897 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
10898
10899 replay_nested = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
10900 if (!replay_nested)
10901 goto nla_put_failure;
10902
10903 NLA_PUT(msg, NL80211_REKEY_DATA_KEK, NL80211_KEK_LEN, kek);
10904 NLA_PUT(msg, NL80211_REKEY_DATA_KCK, NL80211_KCK_LEN, kck);
10905 NLA_PUT(msg, NL80211_REKEY_DATA_REPLAY_CTR, NL80211_REPLAY_CTR_LEN,
10906 replay_ctr);
10907
10908 nla_nest_end(msg, replay_nested);
10909
10910 send_and_recv_msgs(drv, msg, NULL, NULL);
10911 return;
10912 nla_put_failure:
10913 nlmsg_free(msg);
10914}
10915
10916
10917static void nl80211_send_null_frame(struct i802_bss *bss, const u8 *own_addr,
10918 const u8 *addr, int qos)
10919{
10920 /* send data frame to poll STA and check whether
10921 * this frame is ACKed */
10922 struct {
10923 struct ieee80211_hdr hdr;
10924 u16 qos_ctl;
10925 } STRUCT_PACKED nulldata;
10926 size_t size;
10927
10928 /* Send data frame to poll STA and check whether this frame is ACKed */
10929
10930 os_memset(&nulldata, 0, sizeof(nulldata));
10931
10932 if (qos) {
10933 nulldata.hdr.frame_control =
10934 IEEE80211_FC(WLAN_FC_TYPE_DATA,
10935 WLAN_FC_STYPE_QOS_NULL);
10936 size = sizeof(nulldata);
10937 } else {
10938 nulldata.hdr.frame_control =
10939 IEEE80211_FC(WLAN_FC_TYPE_DATA,
10940 WLAN_FC_STYPE_NULLFUNC);
10941 size = sizeof(struct ieee80211_hdr);
10942 }
10943
10944 nulldata.hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS);
10945 os_memcpy(nulldata.hdr.IEEE80211_DA_FROMDS, addr, ETH_ALEN);
10946 os_memcpy(nulldata.hdr.IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
10947 os_memcpy(nulldata.hdr.IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
10948
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010949 if (wpa_driver_nl80211_send_mlme(bss, (u8 *) &nulldata, size, 0, 0, 0,
10950 0, 0) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010951 wpa_printf(MSG_DEBUG, "nl80211_send_null_frame: Failed to "
10952 "send poll frame");
10953}
10954
10955static void nl80211_poll_client(void *priv, const u8 *own_addr, const u8 *addr,
10956 int qos)
10957{
10958 struct i802_bss *bss = priv;
10959 struct wpa_driver_nl80211_data *drv = bss->drv;
10960 struct nl_msg *msg;
10961
10962 if (!drv->poll_command_supported) {
10963 nl80211_send_null_frame(bss, own_addr, addr, qos);
10964 return;
10965 }
10966
10967 msg = nlmsg_alloc();
10968 if (!msg)
10969 return;
10970
10971 nl80211_cmd(drv, msg, 0, NL80211_CMD_PROBE_CLIENT);
10972
10973 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
10974 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
10975
10976 send_and_recv_msgs(drv, msg, NULL, NULL);
10977 return;
10978 nla_put_failure:
10979 nlmsg_free(msg);
10980}
10981
10982
10983static int nl80211_set_power_save(struct i802_bss *bss, int enabled)
10984{
10985 struct nl_msg *msg;
10986
10987 msg = nlmsg_alloc();
10988 if (!msg)
10989 return -ENOMEM;
10990
10991 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_SET_POWER_SAVE);
10992 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
10993 NLA_PUT_U32(msg, NL80211_ATTR_PS_STATE,
10994 enabled ? NL80211_PS_ENABLED : NL80211_PS_DISABLED);
10995 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
10996nla_put_failure:
10997 nlmsg_free(msg);
10998 return -ENOBUFS;
10999}
11000
11001
11002static int nl80211_set_p2p_powersave(void *priv, int legacy_ps, int opp_ps,
11003 int ctwindow)
11004{
11005 struct i802_bss *bss = priv;
11006
11007 wpa_printf(MSG_DEBUG, "nl80211: set_p2p_powersave (legacy_ps=%d "
11008 "opp_ps=%d ctwindow=%d)", legacy_ps, opp_ps, ctwindow);
11009
Dmitry Shmidt292b0c32013-11-22 12:54:42 -080011010 if (opp_ps != -1 || ctwindow != -1) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080011011#ifdef ANDROID_P2P
11012 wpa_driver_set_p2p_ps(priv, legacy_ps, opp_ps, ctwindow);
Dmitry Shmidt292b0c32013-11-22 12:54:42 -080011013#else /* ANDROID_P2P */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011014 return -1; /* Not yet supported */
Dmitry Shmidt292b0c32013-11-22 12:54:42 -080011015#endif /* ANDROID_P2P */
11016 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011017
11018 if (legacy_ps == -1)
11019 return 0;
11020 if (legacy_ps != 0 && legacy_ps != 1)
11021 return -1; /* Not yet supported */
11022
11023 return nl80211_set_power_save(bss, legacy_ps);
11024}
11025
11026
Dmitry Shmidt051af732013-10-22 13:52:46 -070011027static int nl80211_start_radar_detection(void *priv,
11028 struct hostapd_freq_params *freq)
Dmitry Shmidtea69e842013-05-13 14:52:28 -070011029{
11030 struct i802_bss *bss = priv;
11031 struct wpa_driver_nl80211_data *drv = bss->drv;
11032 struct nl_msg *msg;
11033 int ret;
11034
Dmitry Shmidt051af732013-10-22 13:52:46 -070011035 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)",
11036 freq->freq, freq->ht_enabled, freq->vht_enabled,
11037 freq->bandwidth, freq->center_freq1, freq->center_freq2);
11038
Dmitry Shmidtea69e842013-05-13 14:52:28 -070011039 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_RADAR)) {
11040 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support radar "
11041 "detection");
11042 return -1;
11043 }
11044
11045 msg = nlmsg_alloc();
11046 if (!msg)
11047 return -1;
11048
11049 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_RADAR_DETECT);
11050 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
Dmitry Shmidt051af732013-10-22 13:52:46 -070011051 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq->freq);
Dmitry Shmidtea69e842013-05-13 14:52:28 -070011052
Dmitry Shmidt051af732013-10-22 13:52:46 -070011053 if (freq->vht_enabled) {
11054 switch (freq->bandwidth) {
11055 case 20:
11056 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
11057 NL80211_CHAN_WIDTH_20);
11058 break;
11059 case 40:
11060 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
11061 NL80211_CHAN_WIDTH_40);
11062 break;
11063 case 80:
11064 if (freq->center_freq2)
11065 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
11066 NL80211_CHAN_WIDTH_80P80);
11067 else
11068 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
11069 NL80211_CHAN_WIDTH_80);
11070 break;
11071 case 160:
11072 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
11073 NL80211_CHAN_WIDTH_160);
11074 break;
11075 default:
11076 return -1;
11077 }
11078 NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ1, freq->center_freq1);
11079 if (freq->center_freq2)
11080 NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ2,
11081 freq->center_freq2);
11082 } else if (freq->ht_enabled) {
11083 switch (freq->sec_channel_offset) {
11084 case -1:
11085 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
11086 NL80211_CHAN_HT40MINUS);
11087 break;
11088 case 1:
11089 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
11090 NL80211_CHAN_HT40PLUS);
11091 break;
11092 default:
11093 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
11094 NL80211_CHAN_HT20);
11095 break;
11096 }
11097 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -070011098
11099 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
11100 if (ret == 0)
11101 return 0;
11102 wpa_printf(MSG_DEBUG, "nl80211: Failed to start radar detection: "
11103 "%d (%s)", ret, strerror(-ret));
11104nla_put_failure:
11105 return -1;
11106}
11107
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011108#ifdef CONFIG_TDLS
11109
11110static int nl80211_send_tdls_mgmt(void *priv, const u8 *dst, u8 action_code,
11111 u8 dialog_token, u16 status_code,
11112 const u8 *buf, size_t len)
11113{
11114 struct i802_bss *bss = priv;
11115 struct wpa_driver_nl80211_data *drv = bss->drv;
11116 struct nl_msg *msg;
11117
11118 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
11119 return -EOPNOTSUPP;
11120
11121 if (!dst)
11122 return -EINVAL;
11123
11124 msg = nlmsg_alloc();
11125 if (!msg)
11126 return -ENOMEM;
11127
11128 nl80211_cmd(drv, msg, 0, NL80211_CMD_TDLS_MGMT);
11129 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
11130 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
11131 NLA_PUT_U8(msg, NL80211_ATTR_TDLS_ACTION, action_code);
11132 NLA_PUT_U8(msg, NL80211_ATTR_TDLS_DIALOG_TOKEN, dialog_token);
11133 NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status_code);
11134 NLA_PUT(msg, NL80211_ATTR_IE, len, buf);
11135
11136 return send_and_recv_msgs(drv, msg, NULL, NULL);
11137
11138nla_put_failure:
11139 nlmsg_free(msg);
11140 return -ENOBUFS;
11141}
11142
11143
11144static int nl80211_tdls_oper(void *priv, enum tdls_oper oper, const u8 *peer)
11145{
11146 struct i802_bss *bss = priv;
11147 struct wpa_driver_nl80211_data *drv = bss->drv;
11148 struct nl_msg *msg;
11149 enum nl80211_tdls_operation nl80211_oper;
11150
11151 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
11152 return -EOPNOTSUPP;
11153
11154 switch (oper) {
11155 case TDLS_DISCOVERY_REQ:
11156 nl80211_oper = NL80211_TDLS_DISCOVERY_REQ;
11157 break;
11158 case TDLS_SETUP:
11159 nl80211_oper = NL80211_TDLS_SETUP;
11160 break;
11161 case TDLS_TEARDOWN:
11162 nl80211_oper = NL80211_TDLS_TEARDOWN;
11163 break;
11164 case TDLS_ENABLE_LINK:
11165 nl80211_oper = NL80211_TDLS_ENABLE_LINK;
11166 break;
11167 case TDLS_DISABLE_LINK:
11168 nl80211_oper = NL80211_TDLS_DISABLE_LINK;
11169 break;
11170 case TDLS_ENABLE:
11171 return 0;
11172 case TDLS_DISABLE:
11173 return 0;
11174 default:
11175 return -EINVAL;
11176 }
11177
11178 msg = nlmsg_alloc();
11179 if (!msg)
11180 return -ENOMEM;
11181
11182 nl80211_cmd(drv, msg, 0, NL80211_CMD_TDLS_OPER);
11183 NLA_PUT_U8(msg, NL80211_ATTR_TDLS_OPERATION, nl80211_oper);
11184 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
11185 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, peer);
11186
11187 return send_and_recv_msgs(drv, msg, NULL, NULL);
11188
11189nla_put_failure:
11190 nlmsg_free(msg);
11191 return -ENOBUFS;
11192}
11193
11194#endif /* CONFIG TDLS */
11195
11196
11197#ifdef ANDROID
11198
11199typedef struct android_wifi_priv_cmd {
11200 char *buf;
11201 int used_len;
11202 int total_len;
11203} android_wifi_priv_cmd;
11204
11205static int drv_errors = 0;
11206
11207static void wpa_driver_send_hang_msg(struct wpa_driver_nl80211_data *drv)
11208{
11209 drv_errors++;
11210 if (drv_errors > DRV_NUMBER_SEQUENTIAL_ERRORS) {
11211 drv_errors = 0;
11212 wpa_msg(drv->ctx, MSG_INFO, WPA_EVENT_DRIVER_STATE "HANGED");
11213 }
11214}
11215
11216
11217static int android_priv_cmd(struct i802_bss *bss, const char *cmd)
11218{
11219 struct wpa_driver_nl80211_data *drv = bss->drv;
11220 struct ifreq ifr;
11221 android_wifi_priv_cmd priv_cmd;
11222 char buf[MAX_DRV_CMD_SIZE];
11223 int ret;
11224
11225 os_memset(&ifr, 0, sizeof(ifr));
11226 os_memset(&priv_cmd, 0, sizeof(priv_cmd));
11227 os_strlcpy(ifr.ifr_name, bss->ifname, IFNAMSIZ);
11228
11229 os_memset(buf, 0, sizeof(buf));
11230 os_strlcpy(buf, cmd, sizeof(buf));
11231
11232 priv_cmd.buf = buf;
11233 priv_cmd.used_len = sizeof(buf);
11234 priv_cmd.total_len = sizeof(buf);
11235 ifr.ifr_data = &priv_cmd;
11236
11237 ret = ioctl(drv->global->ioctl_sock, SIOCDEVPRIVATE + 1, &ifr);
11238 if (ret < 0) {
11239 wpa_printf(MSG_ERROR, "%s: failed to issue private commands",
11240 __func__);
11241 wpa_driver_send_hang_msg(drv);
11242 return ret;
11243 }
11244
11245 drv_errors = 0;
11246 return 0;
11247}
11248
11249
11250static int android_pno_start(struct i802_bss *bss,
11251 struct wpa_driver_scan_params *params)
11252{
11253 struct wpa_driver_nl80211_data *drv = bss->drv;
11254 struct ifreq ifr;
11255 android_wifi_priv_cmd priv_cmd;
11256 int ret = 0, i = 0, bp;
11257 char buf[WEXT_PNO_MAX_COMMAND_SIZE];
11258
11259 bp = WEXT_PNOSETUP_HEADER_SIZE;
11260 os_memcpy(buf, WEXT_PNOSETUP_HEADER, bp);
11261 buf[bp++] = WEXT_PNO_TLV_PREFIX;
11262 buf[bp++] = WEXT_PNO_TLV_VERSION;
11263 buf[bp++] = WEXT_PNO_TLV_SUBVERSION;
11264 buf[bp++] = WEXT_PNO_TLV_RESERVED;
11265
11266 while (i < WEXT_PNO_AMOUNT && (size_t) i < params->num_ssids) {
11267 /* Check that there is enough space needed for 1 more SSID, the
11268 * other sections and null termination */
11269 if ((bp + WEXT_PNO_SSID_HEADER_SIZE + MAX_SSID_LEN +
11270 WEXT_PNO_NONSSID_SECTIONS_SIZE + 1) >= (int) sizeof(buf))
11271 break;
11272 wpa_hexdump_ascii(MSG_DEBUG, "For PNO Scan",
11273 params->ssids[i].ssid,
11274 params->ssids[i].ssid_len);
11275 buf[bp++] = WEXT_PNO_SSID_SECTION;
11276 buf[bp++] = params->ssids[i].ssid_len;
11277 os_memcpy(&buf[bp], params->ssids[i].ssid,
11278 params->ssids[i].ssid_len);
11279 bp += params->ssids[i].ssid_len;
11280 i++;
11281 }
11282
11283 buf[bp++] = WEXT_PNO_SCAN_INTERVAL_SECTION;
11284 os_snprintf(&buf[bp], WEXT_PNO_SCAN_INTERVAL_LENGTH + 1, "%x",
11285 WEXT_PNO_SCAN_INTERVAL);
11286 bp += WEXT_PNO_SCAN_INTERVAL_LENGTH;
11287
11288 buf[bp++] = WEXT_PNO_REPEAT_SECTION;
11289 os_snprintf(&buf[bp], WEXT_PNO_REPEAT_LENGTH + 1, "%x",
11290 WEXT_PNO_REPEAT);
11291 bp += WEXT_PNO_REPEAT_LENGTH;
11292
11293 buf[bp++] = WEXT_PNO_MAX_REPEAT_SECTION;
11294 os_snprintf(&buf[bp], WEXT_PNO_MAX_REPEAT_LENGTH + 1, "%x",
11295 WEXT_PNO_MAX_REPEAT);
11296 bp += WEXT_PNO_MAX_REPEAT_LENGTH + 1;
11297
11298 memset(&ifr, 0, sizeof(ifr));
11299 memset(&priv_cmd, 0, sizeof(priv_cmd));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070011300 os_strlcpy(ifr.ifr_name, bss->ifname, IFNAMSIZ);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011301
11302 priv_cmd.buf = buf;
11303 priv_cmd.used_len = bp;
11304 priv_cmd.total_len = bp;
11305 ifr.ifr_data = &priv_cmd;
11306
11307 ret = ioctl(drv->global->ioctl_sock, SIOCDEVPRIVATE + 1, &ifr);
11308
11309 if (ret < 0) {
11310 wpa_printf(MSG_ERROR, "ioctl[SIOCSIWPRIV] (pnosetup): %d",
11311 ret);
11312 wpa_driver_send_hang_msg(drv);
11313 return ret;
11314 }
11315
11316 drv_errors = 0;
11317
11318 return android_priv_cmd(bss, "PNOFORCE 1");
11319}
11320
11321
11322static int android_pno_stop(struct i802_bss *bss)
11323{
11324 return android_priv_cmd(bss, "PNOFORCE 0");
11325}
11326
11327#endif /* ANDROID */
11328
11329
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011330static int driver_nl80211_set_key(const char *ifname, void *priv,
11331 enum wpa_alg alg, const u8 *addr,
11332 int key_idx, int set_tx,
11333 const u8 *seq, size_t seq_len,
11334 const u8 *key, size_t key_len)
11335{
11336 struct i802_bss *bss = priv;
11337 return wpa_driver_nl80211_set_key(ifname, bss, alg, addr, key_idx,
11338 set_tx, seq, seq_len, key, key_len);
11339}
11340
11341
11342static int driver_nl80211_scan2(void *priv,
11343 struct wpa_driver_scan_params *params)
11344{
11345 struct i802_bss *bss = priv;
11346 return wpa_driver_nl80211_scan(bss, params);
11347}
11348
11349
11350static int driver_nl80211_deauthenticate(void *priv, const u8 *addr,
11351 int reason_code)
11352{
11353 struct i802_bss *bss = priv;
11354 return wpa_driver_nl80211_deauthenticate(bss, addr, reason_code);
11355}
11356
11357
11358static int driver_nl80211_authenticate(void *priv,
11359 struct wpa_driver_auth_params *params)
11360{
11361 struct i802_bss *bss = priv;
11362 return wpa_driver_nl80211_authenticate(bss, params);
11363}
11364
11365
11366static void driver_nl80211_deinit(void *priv)
11367{
11368 struct i802_bss *bss = priv;
11369 wpa_driver_nl80211_deinit(bss);
11370}
11371
11372
11373static int driver_nl80211_if_remove(void *priv, enum wpa_driver_if_type type,
11374 const char *ifname)
11375{
11376 struct i802_bss *bss = priv;
11377 return wpa_driver_nl80211_if_remove(bss, type, ifname);
11378}
11379
11380
11381static int driver_nl80211_send_mlme(void *priv, const u8 *data,
11382 size_t data_len, int noack)
11383{
11384 struct i802_bss *bss = priv;
11385 return wpa_driver_nl80211_send_mlme(bss, data, data_len, noack,
11386 0, 0, 0, 0);
11387}
11388
11389
11390static int driver_nl80211_sta_remove(void *priv, const u8 *addr)
11391{
11392 struct i802_bss *bss = priv;
11393 return wpa_driver_nl80211_sta_remove(bss, addr);
11394}
11395
11396
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011397static int driver_nl80211_set_sta_vlan(void *priv, const u8 *addr,
11398 const char *ifname, int vlan_id)
11399{
11400 struct i802_bss *bss = priv;
11401 return i802_set_sta_vlan(bss, addr, ifname, vlan_id);
11402}
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011403
11404
11405static int driver_nl80211_read_sta_data(void *priv,
11406 struct hostap_sta_driver_data *data,
11407 const u8 *addr)
11408{
11409 struct i802_bss *bss = priv;
11410 return i802_read_sta_data(bss, data, addr);
11411}
11412
11413
11414static int driver_nl80211_send_action(void *priv, unsigned int freq,
11415 unsigned int wait_time,
11416 const u8 *dst, const u8 *src,
11417 const u8 *bssid,
11418 const u8 *data, size_t data_len,
11419 int no_cck)
11420{
11421 struct i802_bss *bss = priv;
11422 return wpa_driver_nl80211_send_action(bss, freq, wait_time, dst, src,
11423 bssid, data, data_len, no_cck);
11424}
11425
11426
11427static int driver_nl80211_probe_req_report(void *priv, int report)
11428{
11429 struct i802_bss *bss = priv;
11430 return wpa_driver_nl80211_probe_req_report(bss, report);
11431}
11432
11433
Dmitry Shmidt700a1372013-03-15 14:14:44 -070011434static int wpa_driver_nl80211_update_ft_ies(void *priv, const u8 *md,
11435 const u8 *ies, size_t ies_len)
11436{
11437 int ret;
11438 struct nl_msg *msg;
11439 struct i802_bss *bss = priv;
11440 struct wpa_driver_nl80211_data *drv = bss->drv;
11441 u16 mdid = WPA_GET_LE16(md);
11442
11443 msg = nlmsg_alloc();
11444 if (!msg)
11445 return -ENOMEM;
11446
11447 wpa_printf(MSG_DEBUG, "nl80211: Updating FT IEs");
11448 nl80211_cmd(drv, msg, 0, NL80211_CMD_UPDATE_FT_IES);
11449 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
11450 NLA_PUT(msg, NL80211_ATTR_IE, ies_len, ies);
11451 NLA_PUT_U16(msg, NL80211_ATTR_MDID, mdid);
11452
11453 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
11454 if (ret) {
11455 wpa_printf(MSG_DEBUG, "nl80211: update_ft_ies failed "
11456 "err=%d (%s)", ret, strerror(-ret));
11457 }
11458
11459 return ret;
11460
11461nla_put_failure:
11462 nlmsg_free(msg);
11463 return -ENOBUFS;
11464}
11465
11466
Dmitry Shmidt34af3062013-07-11 10:46:32 -070011467const u8 * wpa_driver_nl80211_get_macaddr(void *priv)
11468{
11469 struct i802_bss *bss = priv;
11470 struct wpa_driver_nl80211_data *drv = bss->drv;
11471
11472 if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE)
11473 return NULL;
11474
11475 return bss->addr;
11476}
11477
11478
Dmitry Shmidt56052862013-10-04 10:23:25 -070011479static const char * scan_state_str(enum scan_states scan_state)
11480{
11481 switch (scan_state) {
11482 case NO_SCAN:
11483 return "NO_SCAN";
11484 case SCAN_REQUESTED:
11485 return "SCAN_REQUESTED";
11486 case SCAN_STARTED:
11487 return "SCAN_STARTED";
11488 case SCAN_COMPLETED:
11489 return "SCAN_COMPLETED";
11490 case SCAN_ABORTED:
11491 return "SCAN_ABORTED";
11492 case SCHED_SCAN_STARTED:
11493 return "SCHED_SCAN_STARTED";
11494 case SCHED_SCAN_STOPPED:
11495 return "SCHED_SCAN_STOPPED";
11496 case SCHED_SCAN_RESULTS:
11497 return "SCHED_SCAN_RESULTS";
11498 }
11499
11500 return "??";
11501}
11502
11503
11504static int wpa_driver_nl80211_status(void *priv, char *buf, size_t buflen)
11505{
11506 struct i802_bss *bss = priv;
11507 struct wpa_driver_nl80211_data *drv = bss->drv;
11508 int res;
11509 char *pos, *end;
11510
11511 pos = buf;
11512 end = buf + buflen;
11513
11514 res = os_snprintf(pos, end - pos,
11515 "ifindex=%d\n"
11516 "ifname=%s\n"
11517 "brname=%s\n"
11518 "addr=" MACSTR "\n"
11519 "freq=%d\n"
11520 "%s%s%s%s%s",
11521 bss->ifindex,
11522 bss->ifname,
11523 bss->brname,
11524 MAC2STR(bss->addr),
11525 bss->freq,
11526 bss->beacon_set ? "beacon_set=1\n" : "",
11527 bss->added_if_into_bridge ?
11528 "added_if_into_bridge=1\n" : "",
11529 bss->added_bridge ? "added_bridge=1\n" : "",
11530 bss->in_deinit ? "in_deinit=1\n" : "",
11531 bss->if_dynamic ? "if_dynamic=1\n" : "");
11532 if (res < 0 || res >= end - pos)
11533 return pos - buf;
11534 pos += res;
11535
11536 if (bss->wdev_id_set) {
11537 res = os_snprintf(pos, end - pos, "wdev_id=%llu\n",
11538 (unsigned long long) bss->wdev_id);
11539 if (res < 0 || res >= end - pos)
11540 return pos - buf;
11541 pos += res;
11542 }
11543
11544 res = os_snprintf(pos, end - pos,
11545 "phyname=%s\n"
11546 "drv_ifindex=%d\n"
11547 "operstate=%d\n"
11548 "scan_state=%s\n"
11549 "auth_bssid=" MACSTR "\n"
11550 "auth_attempt_bssid=" MACSTR "\n"
11551 "bssid=" MACSTR "\n"
11552 "prev_bssid=" MACSTR "\n"
11553 "associated=%d\n"
11554 "assoc_freq=%u\n"
11555 "monitor_sock=%d\n"
11556 "monitor_ifidx=%d\n"
11557 "monitor_refcount=%d\n"
11558 "last_mgmt_freq=%u\n"
11559 "eapol_tx_sock=%d\n"
11560 "%s%s%s%s%s%s%s%s%s%s%s%s%s",
11561 drv->phyname,
11562 drv->ifindex,
11563 drv->operstate,
11564 scan_state_str(drv->scan_state),
11565 MAC2STR(drv->auth_bssid),
11566 MAC2STR(drv->auth_attempt_bssid),
11567 MAC2STR(drv->bssid),
11568 MAC2STR(drv->prev_bssid),
11569 drv->associated,
11570 drv->assoc_freq,
11571 drv->monitor_sock,
11572 drv->monitor_ifidx,
11573 drv->monitor_refcount,
11574 drv->last_mgmt_freq,
11575 drv->eapol_tx_sock,
11576 drv->ignore_if_down_event ?
11577 "ignore_if_down_event=1\n" : "",
11578 drv->scan_complete_events ?
11579 "scan_complete_events=1\n" : "",
11580 drv->disabled_11b_rates ?
11581 "disabled_11b_rates=1\n" : "",
11582 drv->pending_remain_on_chan ?
11583 "pending_remain_on_chan=1\n" : "",
11584 drv->in_interface_list ? "in_interface_list=1\n" : "",
11585 drv->device_ap_sme ? "device_ap_sme=1\n" : "",
11586 drv->poll_command_supported ?
11587 "poll_command_supported=1\n" : "",
11588 drv->data_tx_status ? "data_tx_status=1\n" : "",
11589 drv->scan_for_auth ? "scan_for_auth=1\n" : "",
11590 drv->retry_auth ? "retry_auth=1\n" : "",
11591 drv->use_monitor ? "use_monitor=1\n" : "",
11592 drv->ignore_next_local_disconnect ?
11593 "ignore_next_local_disconnect=1\n" : "",
11594 drv->allow_p2p_device ? "allow_p2p_device=1\n" : "");
11595 if (res < 0 || res >= end - pos)
11596 return pos - buf;
11597 pos += res;
11598
11599 if (drv->has_capability) {
11600 res = os_snprintf(pos, end - pos,
11601 "capa.key_mgmt=0x%x\n"
11602 "capa.enc=0x%x\n"
11603 "capa.auth=0x%x\n"
11604 "capa.flags=0x%x\n"
11605 "capa.max_scan_ssids=%d\n"
11606 "capa.max_sched_scan_ssids=%d\n"
11607 "capa.sched_scan_supported=%d\n"
11608 "capa.max_match_sets=%d\n"
11609 "capa.max_remain_on_chan=%u\n"
11610 "capa.max_stations=%u\n"
11611 "capa.probe_resp_offloads=0x%x\n"
11612 "capa.max_acl_mac_addrs=%u\n"
11613 "capa.num_multichan_concurrent=%u\n",
11614 drv->capa.key_mgmt,
11615 drv->capa.enc,
11616 drv->capa.auth,
11617 drv->capa.flags,
11618 drv->capa.max_scan_ssids,
11619 drv->capa.max_sched_scan_ssids,
11620 drv->capa.sched_scan_supported,
11621 drv->capa.max_match_sets,
11622 drv->capa.max_remain_on_chan,
11623 drv->capa.max_stations,
11624 drv->capa.probe_resp_offloads,
11625 drv->capa.max_acl_mac_addrs,
11626 drv->capa.num_multichan_concurrent);
11627 if (res < 0 || res >= end - pos)
11628 return pos - buf;
11629 pos += res;
11630 }
11631
11632 return pos - buf;
11633}
11634
11635
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080011636static int set_beacon_data(struct nl_msg *msg, struct beacon_data *settings)
11637{
11638 if (settings->head)
11639 NLA_PUT(msg, NL80211_ATTR_BEACON_HEAD,
11640 settings->head_len, settings->head);
11641
11642 if (settings->tail)
11643 NLA_PUT(msg, NL80211_ATTR_BEACON_TAIL,
11644 settings->tail_len, settings->tail);
11645
11646 if (settings->beacon_ies)
11647 NLA_PUT(msg, NL80211_ATTR_IE,
11648 settings->beacon_ies_len, settings->beacon_ies);
11649
11650 if (settings->proberesp_ies)
11651 NLA_PUT(msg, NL80211_ATTR_IE_PROBE_RESP,
11652 settings->proberesp_ies_len, settings->proberesp_ies);
11653
11654 if (settings->assocresp_ies)
11655 NLA_PUT(msg,
11656 NL80211_ATTR_IE_ASSOC_RESP,
11657 settings->assocresp_ies_len, settings->assocresp_ies);
11658
11659 if (settings->probe_resp)
11660 NLA_PUT(msg, NL80211_ATTR_PROBE_RESP,
11661 settings->probe_resp_len, settings->probe_resp);
11662
11663 return 0;
11664
11665nla_put_failure:
11666 return -ENOBUFS;
11667}
11668
11669
11670static int nl80211_switch_channel(void *priv, struct csa_settings *settings)
11671{
11672 struct nl_msg *msg;
11673 struct i802_bss *bss = priv;
11674 struct wpa_driver_nl80211_data *drv = bss->drv;
11675 struct nlattr *beacon_csa;
11676 int ret = -ENOBUFS;
11677
Dmitry Shmidt04f534e2013-12-09 15:50:16 -080011678 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 -080011679 settings->cs_count, settings->block_tx,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -080011680 settings->freq_params.freq, settings->freq_params.bandwidth,
11681 settings->freq_params.center_freq1,
11682 settings->freq_params.center_freq2);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080011683
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080011684 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_AP_CSA)) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080011685 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support channel switch command");
11686 return -EOPNOTSUPP;
11687 }
11688
11689 if ((drv->nlmode != NL80211_IFTYPE_AP) &&
11690 (drv->nlmode != NL80211_IFTYPE_P2P_GO))
11691 return -EOPNOTSUPP;
11692
11693 /* check settings validity */
11694 if (!settings->beacon_csa.tail ||
11695 ((settings->beacon_csa.tail_len <=
11696 settings->counter_offset_beacon) ||
11697 (settings->beacon_csa.tail[settings->counter_offset_beacon] !=
11698 settings->cs_count)))
11699 return -EINVAL;
11700
11701 if (settings->beacon_csa.probe_resp &&
11702 ((settings->beacon_csa.probe_resp_len <=
11703 settings->counter_offset_presp) ||
11704 (settings->beacon_csa.probe_resp[settings->counter_offset_presp] !=
11705 settings->cs_count)))
11706 return -EINVAL;
11707
11708 msg = nlmsg_alloc();
11709 if (!msg)
11710 return -ENOMEM;
11711
11712 nl80211_cmd(drv, msg, 0, NL80211_CMD_CHANNEL_SWITCH);
11713 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
11714 NLA_PUT_U32(msg, NL80211_ATTR_CH_SWITCH_COUNT, settings->cs_count);
11715 ret = nl80211_put_freq_params(msg, &settings->freq_params);
11716 if (ret)
11717 goto error;
11718
11719 if (settings->block_tx)
11720 NLA_PUT_FLAG(msg, NL80211_ATTR_CH_SWITCH_BLOCK_TX);
11721
11722 /* beacon_after params */
11723 ret = set_beacon_data(msg, &settings->beacon_after);
11724 if (ret)
11725 goto error;
11726
11727 /* beacon_csa params */
11728 beacon_csa = nla_nest_start(msg, NL80211_ATTR_CSA_IES);
11729 if (!beacon_csa)
11730 goto nla_put_failure;
11731
11732 ret = set_beacon_data(msg, &settings->beacon_csa);
11733 if (ret)
11734 goto error;
11735
11736 NLA_PUT_U16(msg, NL80211_ATTR_CSA_C_OFF_BEACON,
11737 settings->counter_offset_beacon);
11738
11739 if (settings->beacon_csa.probe_resp)
11740 NLA_PUT_U16(msg, NL80211_ATTR_CSA_C_OFF_PRESP,
11741 settings->counter_offset_presp);
11742
11743 nla_nest_end(msg, beacon_csa);
11744 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
11745 if (ret) {
11746 wpa_printf(MSG_DEBUG, "nl80211: switch_channel failed err=%d (%s)",
11747 ret, strerror(-ret));
11748 }
11749 return ret;
11750
11751nla_put_failure:
11752 ret = -ENOBUFS;
11753error:
11754 nlmsg_free(msg);
11755 wpa_printf(MSG_DEBUG, "nl80211: Could not build channel switch request");
11756 return ret;
11757}
11758
11759
Dmitry Shmidta38abf92014-03-06 13:38:44 -080011760#ifdef CONFIG_TESTING_OPTIONS
11761static int cmd_reply_handler(struct nl_msg *msg, void *arg)
11762{
11763 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
11764 struct wpabuf *buf = arg;
11765
11766 if (!buf)
11767 return NL_SKIP;
11768
11769 if ((size_t) genlmsg_attrlen(gnlh, 0) > wpabuf_tailroom(buf)) {
11770 wpa_printf(MSG_INFO, "nl80211: insufficient buffer space for reply");
11771 return NL_SKIP;
11772 }
11773
11774 wpabuf_put_data(buf, genlmsg_attrdata(gnlh, 0),
11775 genlmsg_attrlen(gnlh, 0));
11776
11777 return NL_SKIP;
11778}
11779#endif /* CONFIG_TESTING_OPTIONS */
11780
11781
11782static int vendor_reply_handler(struct nl_msg *msg, void *arg)
11783{
11784 struct nlattr *tb[NL80211_ATTR_MAX + 1];
11785 struct nlattr *nl_vendor_reply, *nl;
11786 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
11787 struct wpabuf *buf = arg;
11788 int rem;
11789
11790 if (!buf)
11791 return NL_SKIP;
11792
11793 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
11794 genlmsg_attrlen(gnlh, 0), NULL);
11795 nl_vendor_reply = tb[NL80211_ATTR_VENDOR_DATA];
11796
11797 if (!nl_vendor_reply)
11798 return NL_SKIP;
11799
11800 if ((size_t) nla_len(nl_vendor_reply) > wpabuf_tailroom(buf)) {
11801 wpa_printf(MSG_INFO, "nl80211: Vendor command: insufficient buffer space for reply");
11802 return NL_SKIP;
11803 }
11804
11805 nla_for_each_nested(nl, nl_vendor_reply, rem) {
11806 wpabuf_put_data(buf, nla_data(nl), nla_len(nl));
11807 }
11808
11809 return NL_SKIP;
11810}
11811
11812
11813static int nl80211_vendor_cmd(void *priv, unsigned int vendor_id,
11814 unsigned int subcmd, const u8 *data,
11815 size_t data_len, struct wpabuf *buf)
11816{
11817 struct i802_bss *bss = priv;
11818 struct wpa_driver_nl80211_data *drv = bss->drv;
11819 struct nl_msg *msg;
11820 int ret;
11821
11822 msg = nlmsg_alloc();
11823 if (!msg)
11824 return -ENOMEM;
11825
11826#ifdef CONFIG_TESTING_OPTIONS
11827 if (vendor_id == 0xffffffff) {
11828 nl80211_cmd(drv, msg, 0, subcmd);
11829 if (nlmsg_append(msg, (void *) data, data_len, NLMSG_ALIGNTO) <
11830 0)
11831 goto nla_put_failure;
11832 ret = send_and_recv_msgs(drv, msg, cmd_reply_handler, buf);
11833 if (ret)
11834 wpa_printf(MSG_DEBUG, "nl80211: command failed err=%d",
11835 ret);
11836 return ret;
11837 }
11838#endif /* CONFIG_TESTING_OPTIONS */
11839
11840 nl80211_cmd(drv, msg, 0, NL80211_CMD_VENDOR);
11841 if (nl80211_set_iface_id(msg, bss) < 0)
11842 goto nla_put_failure;
11843 NLA_PUT_U32(msg, NL80211_ATTR_VENDOR_ID, vendor_id);
11844 NLA_PUT_U32(msg, NL80211_ATTR_VENDOR_SUBCMD, subcmd);
11845 if (data)
11846 NLA_PUT(msg, NL80211_ATTR_VENDOR_DATA, data_len, data);
11847
11848 ret = send_and_recv_msgs(drv, msg, vendor_reply_handler, buf);
11849 if (ret)
11850 wpa_printf(MSG_DEBUG, "nl80211: vendor command failed err=%d",
11851 ret);
11852 return ret;
11853
11854nla_put_failure:
11855 nlmsg_free(msg);
11856 return -ENOBUFS;
11857}
11858
11859
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080011860static int nl80211_set_qos_map(void *priv, const u8 *qos_map_set,
11861 u8 qos_map_set_len)
11862{
11863 struct i802_bss *bss = priv;
11864 struct wpa_driver_nl80211_data *drv = bss->drv;
11865 struct nl_msg *msg;
11866 int ret;
11867
11868 msg = nlmsg_alloc();
11869 if (!msg)
11870 return -ENOMEM;
11871
11872 wpa_hexdump(MSG_DEBUG, "nl80211: Setting QoS Map",
11873 qos_map_set, qos_map_set_len);
11874
11875 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_QOS_MAP);
11876 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
11877 NLA_PUT(msg, NL80211_ATTR_QOS_MAP, qos_map_set_len, qos_map_set);
11878
11879 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
11880 if (ret)
11881 wpa_printf(MSG_DEBUG, "nl80211: Setting QoS Map failed");
11882
11883 return ret;
11884
11885nla_put_failure:
11886 nlmsg_free(msg);
11887 return -ENOBUFS;
11888}
11889
11890
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011891const struct wpa_driver_ops wpa_driver_nl80211_ops = {
11892 .name = "nl80211",
11893 .desc = "Linux nl80211/cfg80211",
11894 .get_bssid = wpa_driver_nl80211_get_bssid,
11895 .get_ssid = wpa_driver_nl80211_get_ssid,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011896 .set_key = driver_nl80211_set_key,
11897 .scan2 = driver_nl80211_scan2,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011898 .sched_scan = wpa_driver_nl80211_sched_scan,
11899 .stop_sched_scan = wpa_driver_nl80211_stop_sched_scan,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011900 .get_scan_results2 = wpa_driver_nl80211_get_scan_results,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011901 .deauthenticate = driver_nl80211_deauthenticate,
11902 .authenticate = driver_nl80211_authenticate,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011903 .associate = wpa_driver_nl80211_associate,
11904 .global_init = nl80211_global_init,
11905 .global_deinit = nl80211_global_deinit,
11906 .init2 = wpa_driver_nl80211_init,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011907 .deinit = driver_nl80211_deinit,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011908 .get_capa = wpa_driver_nl80211_get_capa,
11909 .set_operstate = wpa_driver_nl80211_set_operstate,
11910 .set_supp_port = wpa_driver_nl80211_set_supp_port,
11911 .set_country = wpa_driver_nl80211_set_country,
Dmitry Shmidtcce06662013-11-04 18:44:24 -080011912 .get_country = wpa_driver_nl80211_get_country,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011913 .set_ap = wpa_driver_nl80211_set_ap,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -070011914 .set_acl = wpa_driver_nl80211_set_acl,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011915 .if_add = wpa_driver_nl80211_if_add,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011916 .if_remove = driver_nl80211_if_remove,
11917 .send_mlme = driver_nl80211_send_mlme,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011918 .get_hw_feature_data = wpa_driver_nl80211_get_hw_feature_data,
11919 .sta_add = wpa_driver_nl80211_sta_add,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011920 .sta_remove = driver_nl80211_sta_remove,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011921 .hapd_send_eapol = wpa_driver_nl80211_hapd_send_eapol,
11922 .sta_set_flags = wpa_driver_nl80211_sta_set_flags,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011923 .hapd_init = i802_init,
11924 .hapd_deinit = i802_deinit,
Jouni Malinen75ecf522011-06-27 15:19:46 -070011925 .set_wds_sta = i802_set_wds_sta,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011926 .get_seqnum = i802_get_seqnum,
11927 .flush = i802_flush,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011928 .get_inact_sec = i802_get_inact_sec,
11929 .sta_clear_stats = i802_sta_clear_stats,
11930 .set_rts = i802_set_rts,
11931 .set_frag = i802_set_frag,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011932 .set_tx_queue_params = i802_set_tx_queue_params,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011933 .set_sta_vlan = driver_nl80211_set_sta_vlan,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011934 .sta_deauth = i802_sta_deauth,
11935 .sta_disassoc = i802_sta_disassoc,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011936 .read_sta_data = driver_nl80211_read_sta_data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011937 .set_freq = i802_set_freq,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011938 .send_action = driver_nl80211_send_action,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011939 .send_action_cancel_wait = wpa_driver_nl80211_send_action_cancel_wait,
11940 .remain_on_channel = wpa_driver_nl80211_remain_on_channel,
11941 .cancel_remain_on_channel =
11942 wpa_driver_nl80211_cancel_remain_on_channel,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011943 .probe_req_report = driver_nl80211_probe_req_report,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011944 .deinit_ap = wpa_driver_nl80211_deinit_ap,
Dmitry Shmidt04949592012-07-19 12:16:46 -070011945 .deinit_p2p_cli = wpa_driver_nl80211_deinit_p2p_cli,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011946 .resume = wpa_driver_nl80211_resume,
11947 .send_ft_action = nl80211_send_ft_action,
11948 .signal_monitor = nl80211_signal_monitor,
11949 .signal_poll = nl80211_signal_poll,
11950 .send_frame = nl80211_send_frame,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011951 .shared_freq = wpa_driver_nl80211_shared_freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011952 .set_param = nl80211_set_param,
11953 .get_radio_name = nl80211_get_radio_name,
Jouni Malinen75ecf522011-06-27 15:19:46 -070011954 .add_pmkid = nl80211_add_pmkid,
11955 .remove_pmkid = nl80211_remove_pmkid,
11956 .flush_pmkid = nl80211_flush_pmkid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011957 .set_rekey_info = nl80211_set_rekey_info,
11958 .poll_client = nl80211_poll_client,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011959 .set_p2p_powersave = nl80211_set_p2p_powersave,
Dmitry Shmidtea69e842013-05-13 14:52:28 -070011960 .start_dfs_cac = nl80211_start_radar_detection,
11961 .stop_ap = wpa_driver_nl80211_stop_ap,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011962#ifdef CONFIG_TDLS
11963 .send_tdls_mgmt = nl80211_send_tdls_mgmt,
11964 .tdls_oper = nl80211_tdls_oper,
11965#endif /* CONFIG_TDLS */
Dmitry Shmidt700a1372013-03-15 14:14:44 -070011966 .update_ft_ies = wpa_driver_nl80211_update_ft_ies,
Dmitry Shmidt34af3062013-07-11 10:46:32 -070011967 .get_mac_addr = wpa_driver_nl80211_get_macaddr,
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070011968 .get_survey = wpa_driver_nl80211_get_survey,
Dmitry Shmidt56052862013-10-04 10:23:25 -070011969 .status = wpa_driver_nl80211_status,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080011970 .switch_channel = nl80211_switch_channel,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011971#ifdef ANDROID_P2P
Dmitry Shmidt6e933c12011-09-27 12:29:26 -070011972 .set_noa = wpa_driver_set_p2p_noa,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011973 .get_noa = wpa_driver_get_p2p_noa,
Dmitry Shmidt6e933c12011-09-27 12:29:26 -070011974 .set_ap_wps_ie = wpa_driver_set_ap_wps_p2p_ie,
Dmitry Shmidt292b0c32013-11-22 12:54:42 -080011975#endif /* ANDROID_P2P */
Dmitry Shmidt738a26e2011-07-07 14:22:14 -070011976#ifdef ANDROID
11977 .driver_cmd = wpa_driver_nl80211_driver_cmd,
Dmitry Shmidt292b0c32013-11-22 12:54:42 -080011978#endif /* ANDROID */
Dmitry Shmidta38abf92014-03-06 13:38:44 -080011979 .vendor_cmd = nl80211_vendor_cmd,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080011980 .set_qos_map = nl80211_set_qos_map,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011981};