blob: fce6efdefb64cb5ce6a960f6321e72c8e39288cc [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"
31#include "common/ieee802_11_defs.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080032#include "common/ieee802_11_common.h"
33#include "l2_packet/l2_packet.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070034#include "netlink.h"
35#include "linux_ioctl.h"
36#include "radiotap.h"
37#include "radiotap_iter.h"
38#include "rfkill.h"
39#include "driver.h"
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080040
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080041#ifndef SO_WIFI_STATUS
42# if defined(__sparc__)
43# define SO_WIFI_STATUS 0x0025
44# elif defined(__parisc__)
45# define SO_WIFI_STATUS 0x4022
46# else
47# define SO_WIFI_STATUS 41
48# endif
49
50# define SCM_WIFI_STATUS SO_WIFI_STATUS
51#endif
52
53#ifndef SO_EE_ORIGIN_TXSTATUS
54#define SO_EE_ORIGIN_TXSTATUS 4
55#endif
56
57#ifndef PACKET_TX_TIMESTAMP
58#define PACKET_TX_TIMESTAMP 16
59#endif
60
61#ifdef ANDROID
62#include "android_drv.h"
63#endif /* ANDROID */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070064#ifdef CONFIG_LIBNL20
65/* libnl 2.0 compatibility code */
66#define nl_handle nl_sock
67#define nl80211_handle_alloc nl_socket_alloc_cb
68#define nl80211_handle_destroy nl_socket_free
69#else
70/*
71 * libnl 1.1 has a bug, it tries to allocate socket numbers densely
72 * but when you free a socket again it will mess up its bitmap and
73 * and use the wrong number the next time it needs a socket ID.
74 * Therefore, we wrap the handle alloc/destroy and add our own pid
75 * accounting.
76 */
77static uint32_t port_bitmap[32] = { 0 };
78
79static struct nl_handle *nl80211_handle_alloc(void *cb)
80{
81 struct nl_handle *handle;
82 uint32_t pid = getpid() & 0x3FFFFF;
83 int i;
84
85 handle = nl_handle_alloc_cb(cb);
86
87 for (i = 0; i < 1024; i++) {
88 if (port_bitmap[i / 32] & (1 << (i % 32)))
89 continue;
90 port_bitmap[i / 32] |= 1 << (i % 32);
91 pid += i << 22;
92 break;
93 }
94
95 nl_socket_set_local_port(handle, pid);
96
97 return handle;
98}
99
100static void nl80211_handle_destroy(struct nl_handle *handle)
101{
102 uint32_t port = nl_socket_get_local_port(handle);
103
104 port >>= 22;
105 port_bitmap[port / 32] &= ~(1 << (port % 32));
106
107 nl_handle_destroy(handle);
108}
109#endif /* CONFIG_LIBNL20 */
110
111
Dmitry Shmidt54605472013-11-08 11:10:19 -0800112#ifdef ANDROID
113/* system/core/libnl_2 does not include nl_socket_set_nonblocking() */
114static int android_nl_socket_set_nonblocking(struct nl_handle *handle)
115{
116 return fcntl(nl_socket_get_fd(handle), F_SETFL, O_NONBLOCK);
117}
118#undef nl_socket_set_nonblocking
119#define nl_socket_set_nonblocking(h) android_nl_socket_set_nonblocking(h)
120#endif /* ANDROID */
121
122
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800123static struct nl_handle * nl_create_handle(struct nl_cb *cb, const char *dbg)
124{
125 struct nl_handle *handle;
126
127 handle = nl80211_handle_alloc(cb);
128 if (handle == NULL) {
129 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
130 "callbacks (%s)", dbg);
131 return NULL;
132 }
133
134 if (genl_connect(handle)) {
135 wpa_printf(MSG_ERROR, "nl80211: Failed to connect to generic "
136 "netlink (%s)", dbg);
137 nl80211_handle_destroy(handle);
138 return NULL;
139 }
140
141 return handle;
142}
143
144
145static void nl_destroy_handles(struct nl_handle **handle)
146{
147 if (*handle == NULL)
148 return;
149 nl80211_handle_destroy(*handle);
150 *handle = NULL;
151}
152
153
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700154#if __WORDSIZE == 64
155#define ELOOP_SOCKET_INVALID (intptr_t) 0x8888888888888889ULL
156#else
157#define ELOOP_SOCKET_INVALID (intptr_t) 0x88888889ULL
158#endif
159
160static void nl80211_register_eloop_read(struct nl_handle **handle,
161 eloop_sock_handler handler,
162 void *eloop_data)
163{
164 nl_socket_set_nonblocking(*handle);
165 eloop_register_read_sock(nl_socket_get_fd(*handle), handler,
166 eloop_data, *handle);
167 *handle = (void *) (((intptr_t) *handle) ^ ELOOP_SOCKET_INVALID);
168}
169
170
171static void nl80211_destroy_eloop_handle(struct nl_handle **handle)
172{
173 *handle = (void *) (((intptr_t) *handle) ^ ELOOP_SOCKET_INVALID);
174 eloop_unregister_read_sock(nl_socket_get_fd(*handle));
175 nl_destroy_handles(handle);
176}
177
178
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700179#ifndef IFF_LOWER_UP
180#define IFF_LOWER_UP 0x10000 /* driver signals L1 up */
181#endif
182#ifndef IFF_DORMANT
183#define IFF_DORMANT 0x20000 /* driver signals dormant */
184#endif
185
186#ifndef IF_OPER_DORMANT
187#define IF_OPER_DORMANT 5
188#endif
189#ifndef IF_OPER_UP
190#define IF_OPER_UP 6
191#endif
192
193struct nl80211_global {
194 struct dl_list interfaces;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800195 int if_add_ifindex;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700196 u64 if_add_wdevid;
197 int if_add_wdevid_set;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800198 struct netlink_data *netlink;
199 struct nl_cb *nl_cb;
200 struct nl_handle *nl;
201 int nl80211_id;
202 int ioctl_sock; /* socket for ioctl() use */
203
204 struct nl_handle *nl_event;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700205};
206
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800207struct nl80211_wiphy_data {
208 struct dl_list list;
209 struct dl_list bsss;
210 struct dl_list drvs;
211
212 struct nl_handle *nl_beacons;
213 struct nl_cb *nl_cb;
214
215 int wiphy_idx;
216};
217
218static void nl80211_global_deinit(void *priv);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800219
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700220struct i802_bss {
221 struct wpa_driver_nl80211_data *drv;
222 struct i802_bss *next;
223 int ifindex;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700224 u64 wdev_id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700225 char ifname[IFNAMSIZ + 1];
226 char brname[IFNAMSIZ];
227 unsigned int beacon_set:1;
228 unsigned int added_if_into_bridge:1;
229 unsigned int added_bridge:1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700230 unsigned int in_deinit:1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700231 unsigned int wdev_id_set:1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800232 unsigned int added_if:1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800233
234 u8 addr[ETH_ALEN];
235
236 int freq;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700237 int if_dynamic;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800238
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800239 void *ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800240 struct nl_handle *nl_preq, *nl_mgmt;
241 struct nl_cb *nl_cb;
242
243 struct nl80211_wiphy_data *wiphy_data;
244 struct dl_list wiphy_list;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700245};
246
247struct wpa_driver_nl80211_data {
248 struct nl80211_global *global;
249 struct dl_list list;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800250 struct dl_list wiphy_list;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700251 char phyname[32];
252 void *ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700253 int ifindex;
254 int if_removed;
255 int if_disabled;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800256 int ignore_if_down_event;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700257 struct rfkill_data *rfkill;
258 struct wpa_driver_capa capa;
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700259 u8 *extended_capa, *extended_capa_mask;
260 unsigned int extended_capa_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700261 int has_capability;
262
263 int operstate;
264
265 int scan_complete_events;
Dmitry Shmidt56052862013-10-04 10:23:25 -0700266 enum scan_states {
267 NO_SCAN, SCAN_REQUESTED, SCAN_STARTED, SCAN_COMPLETED,
268 SCAN_ABORTED, SCHED_SCAN_STARTED, SCHED_SCAN_STOPPED,
269 SCHED_SCAN_RESULTS
270 } scan_state;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700271
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700272 struct nl_cb *nl_cb;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700273
274 u8 auth_bssid[ETH_ALEN];
Dmitry Shmidt8bae4132013-06-06 11:25:10 -0700275 u8 auth_attempt_bssid[ETH_ALEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700276 u8 bssid[ETH_ALEN];
Dmitry Shmidt8bae4132013-06-06 11:25:10 -0700277 u8 prev_bssid[ETH_ALEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700278 int associated;
279 u8 ssid[32];
280 size_t ssid_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800281 enum nl80211_iftype nlmode;
282 enum nl80211_iftype ap_scan_as_station;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700283 unsigned int assoc_freq;
284
285 int monitor_sock;
286 int monitor_ifidx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800287 int monitor_refcount;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700288
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800289 unsigned int disabled_11b_rates:1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700290 unsigned int pending_remain_on_chan:1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800291 unsigned int in_interface_list:1;
292 unsigned int device_ap_sme:1;
293 unsigned int poll_command_supported:1;
294 unsigned int data_tx_status:1;
295 unsigned int scan_for_auth:1;
296 unsigned int retry_auth:1;
297 unsigned int use_monitor:1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800298 unsigned int ignore_next_local_disconnect:1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700299 unsigned int allow_p2p_device:1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800300 unsigned int hostapd:1;
301 unsigned int start_mode_ap:1;
302 unsigned int start_iface_up:1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700303
304 u64 remain_on_chan_cookie;
305 u64 send_action_cookie;
306
307 unsigned int last_mgmt_freq;
308
309 struct wpa_driver_scan_filter *filter_ssids;
310 size_t num_filter_ssids;
311
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800312 struct i802_bss *first_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700313
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800314 int eapol_tx_sock;
315
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700316 int eapol_sock; /* socket for EAPOL frames */
317
318 int default_if_indices[16];
319 int *if_indices;
320 int num_if_indices;
321
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800322 /* From failed authentication command */
323 int auth_freq;
324 u8 auth_bssid_[ETH_ALEN];
325 u8 auth_ssid[32];
326 size_t auth_ssid_len;
327 int auth_alg;
328 u8 *auth_ie;
329 size_t auth_ie_len;
330 u8 auth_wep_key[4][16];
331 size_t auth_wep_key_len[4];
332 int auth_wep_tx_keyidx;
333 int auth_local_state_change;
334 int auth_p2p;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700335};
336
337
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800338static void wpa_driver_nl80211_deinit(struct i802_bss *bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700339static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx,
340 void *timeout_ctx);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800341static int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
342 enum nl80211_iftype nlmode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700343static int
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800344wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv,
345 const u8 *set_addr, int first);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700346static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
347 const u8 *addr, int cmd, u16 reason_code,
348 int local_state_change);
349static void nl80211_remove_monitor_interface(
350 struct wpa_driver_nl80211_data *drv);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800351static int nl80211_send_frame_cmd(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700352 unsigned int freq, unsigned int wait,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800353 const u8 *buf, size_t buf_len, u64 *cookie,
354 int no_cck, int no_ack, int offchanok);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700355static int nl80211_register_frame(struct i802_bss *bss,
356 struct nl_handle *hl_handle,
357 u16 type, const u8 *match, size_t match_len);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800358static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss,
359 int report);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800360#ifdef ANDROID
361static int android_pno_start(struct i802_bss *bss,
362 struct wpa_driver_scan_params *params);
363static int android_pno_stop(struct i802_bss *bss);
Dmitry Shmidt292b0c32013-11-22 12:54:42 -0800364extern int wpa_driver_nl80211_driver_cmd(void *priv, char *cmd, char *buf,
365 size_t buf_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800366#endif /* ANDROID */
367#ifdef ANDROID_P2P
Dmitry Shmidt6e933c12011-09-27 12:29:26 -0700368int wpa_driver_set_p2p_noa(void *priv, u8 count, int start, int duration);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800369int wpa_driver_get_p2p_noa(void *priv, u8 *buf, size_t len);
Dmitry Shmidt6e933c12011-09-27 12:29:26 -0700370int wpa_driver_set_p2p_ps(void *priv, int legacy_ps, int opp_ps, int ctwindow);
371int wpa_driver_set_ap_wps_p2p_ie(void *priv, const struct wpabuf *beacon,
Dmitry Shmidt292b0c32013-11-22 12:54:42 -0800372 const struct wpabuf *proberesp,
373 const struct wpabuf *assocresp);
374#endif /* ANDROID_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700375
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700376static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
377static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
378static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800379static int wpa_driver_nl80211_if_remove(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700380 enum wpa_driver_if_type type,
381 const char *ifname);
Dmitry Shmidt738a26e2011-07-07 14:22:14 -0700382
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800383static int wpa_driver_nl80211_set_freq(struct i802_bss *bss,
384 struct hostapd_freq_params *freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700385static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
386 int ifindex, int disabled);
387
388static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800389static int wpa_driver_nl80211_authenticate_retry(
390 struct wpa_driver_nl80211_data *drv);
391
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700392static int i802_set_iface_flags(struct i802_bss *bss, int up);
393
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800394
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700395static const char * nl80211_command_to_string(enum nl80211_commands cmd)
396{
397#define C2S(x) case x: return #x;
398 switch (cmd) {
399 C2S(NL80211_CMD_UNSPEC)
400 C2S(NL80211_CMD_GET_WIPHY)
401 C2S(NL80211_CMD_SET_WIPHY)
402 C2S(NL80211_CMD_NEW_WIPHY)
403 C2S(NL80211_CMD_DEL_WIPHY)
404 C2S(NL80211_CMD_GET_INTERFACE)
405 C2S(NL80211_CMD_SET_INTERFACE)
406 C2S(NL80211_CMD_NEW_INTERFACE)
407 C2S(NL80211_CMD_DEL_INTERFACE)
408 C2S(NL80211_CMD_GET_KEY)
409 C2S(NL80211_CMD_SET_KEY)
410 C2S(NL80211_CMD_NEW_KEY)
411 C2S(NL80211_CMD_DEL_KEY)
412 C2S(NL80211_CMD_GET_BEACON)
413 C2S(NL80211_CMD_SET_BEACON)
414 C2S(NL80211_CMD_START_AP)
415 C2S(NL80211_CMD_STOP_AP)
416 C2S(NL80211_CMD_GET_STATION)
417 C2S(NL80211_CMD_SET_STATION)
418 C2S(NL80211_CMD_NEW_STATION)
419 C2S(NL80211_CMD_DEL_STATION)
420 C2S(NL80211_CMD_GET_MPATH)
421 C2S(NL80211_CMD_SET_MPATH)
422 C2S(NL80211_CMD_NEW_MPATH)
423 C2S(NL80211_CMD_DEL_MPATH)
424 C2S(NL80211_CMD_SET_BSS)
425 C2S(NL80211_CMD_SET_REG)
426 C2S(NL80211_CMD_REQ_SET_REG)
427 C2S(NL80211_CMD_GET_MESH_CONFIG)
428 C2S(NL80211_CMD_SET_MESH_CONFIG)
429 C2S(NL80211_CMD_SET_MGMT_EXTRA_IE)
430 C2S(NL80211_CMD_GET_REG)
431 C2S(NL80211_CMD_GET_SCAN)
432 C2S(NL80211_CMD_TRIGGER_SCAN)
433 C2S(NL80211_CMD_NEW_SCAN_RESULTS)
434 C2S(NL80211_CMD_SCAN_ABORTED)
435 C2S(NL80211_CMD_REG_CHANGE)
436 C2S(NL80211_CMD_AUTHENTICATE)
437 C2S(NL80211_CMD_ASSOCIATE)
438 C2S(NL80211_CMD_DEAUTHENTICATE)
439 C2S(NL80211_CMD_DISASSOCIATE)
440 C2S(NL80211_CMD_MICHAEL_MIC_FAILURE)
441 C2S(NL80211_CMD_REG_BEACON_HINT)
442 C2S(NL80211_CMD_JOIN_IBSS)
443 C2S(NL80211_CMD_LEAVE_IBSS)
444 C2S(NL80211_CMD_TESTMODE)
445 C2S(NL80211_CMD_CONNECT)
446 C2S(NL80211_CMD_ROAM)
447 C2S(NL80211_CMD_DISCONNECT)
448 C2S(NL80211_CMD_SET_WIPHY_NETNS)
449 C2S(NL80211_CMD_GET_SURVEY)
450 C2S(NL80211_CMD_NEW_SURVEY_RESULTS)
451 C2S(NL80211_CMD_SET_PMKSA)
452 C2S(NL80211_CMD_DEL_PMKSA)
453 C2S(NL80211_CMD_FLUSH_PMKSA)
454 C2S(NL80211_CMD_REMAIN_ON_CHANNEL)
455 C2S(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL)
456 C2S(NL80211_CMD_SET_TX_BITRATE_MASK)
457 C2S(NL80211_CMD_REGISTER_FRAME)
458 C2S(NL80211_CMD_FRAME)
459 C2S(NL80211_CMD_FRAME_TX_STATUS)
460 C2S(NL80211_CMD_SET_POWER_SAVE)
461 C2S(NL80211_CMD_GET_POWER_SAVE)
462 C2S(NL80211_CMD_SET_CQM)
463 C2S(NL80211_CMD_NOTIFY_CQM)
464 C2S(NL80211_CMD_SET_CHANNEL)
465 C2S(NL80211_CMD_SET_WDS_PEER)
466 C2S(NL80211_CMD_FRAME_WAIT_CANCEL)
467 C2S(NL80211_CMD_JOIN_MESH)
468 C2S(NL80211_CMD_LEAVE_MESH)
469 C2S(NL80211_CMD_UNPROT_DEAUTHENTICATE)
470 C2S(NL80211_CMD_UNPROT_DISASSOCIATE)
471 C2S(NL80211_CMD_NEW_PEER_CANDIDATE)
472 C2S(NL80211_CMD_GET_WOWLAN)
473 C2S(NL80211_CMD_SET_WOWLAN)
474 C2S(NL80211_CMD_START_SCHED_SCAN)
475 C2S(NL80211_CMD_STOP_SCHED_SCAN)
476 C2S(NL80211_CMD_SCHED_SCAN_RESULTS)
477 C2S(NL80211_CMD_SCHED_SCAN_STOPPED)
478 C2S(NL80211_CMD_SET_REKEY_OFFLOAD)
479 C2S(NL80211_CMD_PMKSA_CANDIDATE)
480 C2S(NL80211_CMD_TDLS_OPER)
481 C2S(NL80211_CMD_TDLS_MGMT)
482 C2S(NL80211_CMD_UNEXPECTED_FRAME)
483 C2S(NL80211_CMD_PROBE_CLIENT)
484 C2S(NL80211_CMD_REGISTER_BEACONS)
485 C2S(NL80211_CMD_UNEXPECTED_4ADDR_FRAME)
486 C2S(NL80211_CMD_SET_NOACK_MAP)
487 C2S(NL80211_CMD_CH_SWITCH_NOTIFY)
488 C2S(NL80211_CMD_START_P2P_DEVICE)
489 C2S(NL80211_CMD_STOP_P2P_DEVICE)
490 C2S(NL80211_CMD_CONN_FAILED)
491 C2S(NL80211_CMD_SET_MCAST_RATE)
492 C2S(NL80211_CMD_SET_MAC_ACL)
493 C2S(NL80211_CMD_RADAR_DETECT)
494 C2S(NL80211_CMD_GET_PROTOCOL_FEATURES)
495 C2S(NL80211_CMD_UPDATE_FT_IES)
496 C2S(NL80211_CMD_FT_EVENT)
497 C2S(NL80211_CMD_CRIT_PROTOCOL_START)
498 C2S(NL80211_CMD_CRIT_PROTOCOL_STOP)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800499 C2S(NL80211_CMD_GET_COALESCE)
500 C2S(NL80211_CMD_SET_COALESCE)
501 C2S(NL80211_CMD_CHANNEL_SWITCH)
502 C2S(NL80211_CMD_VENDOR)
503 C2S(NL80211_CMD_SET_QOS_MAP)
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700504 default:
505 return "NL80211_CMD_UNKNOWN";
506 }
507#undef C2S
508}
509
510
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800511/* Converts nl80211_chan_width to a common format */
512static enum chan_width convert2width(int width)
513{
514 switch (width) {
515 case NL80211_CHAN_WIDTH_20_NOHT:
516 return CHAN_WIDTH_20_NOHT;
517 case NL80211_CHAN_WIDTH_20:
518 return CHAN_WIDTH_20;
519 case NL80211_CHAN_WIDTH_40:
520 return CHAN_WIDTH_40;
521 case NL80211_CHAN_WIDTH_80:
522 return CHAN_WIDTH_80;
523 case NL80211_CHAN_WIDTH_80P80:
524 return CHAN_WIDTH_80P80;
525 case NL80211_CHAN_WIDTH_160:
526 return CHAN_WIDTH_160;
527 }
528 return CHAN_WIDTH_UNKNOWN;
529}
530
531
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800532static int is_ap_interface(enum nl80211_iftype nlmode)
533{
534 return (nlmode == NL80211_IFTYPE_AP ||
535 nlmode == NL80211_IFTYPE_P2P_GO);
536}
537
538
539static int is_sta_interface(enum nl80211_iftype nlmode)
540{
541 return (nlmode == NL80211_IFTYPE_STATION ||
542 nlmode == NL80211_IFTYPE_P2P_CLIENT);
543}
544
545
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700546static int is_p2p_net_interface(enum nl80211_iftype nlmode)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800547{
548 return (nlmode == NL80211_IFTYPE_P2P_CLIENT ||
549 nlmode == NL80211_IFTYPE_P2P_GO);
550}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700551
552
Dmitry Shmidt8bae4132013-06-06 11:25:10 -0700553static void nl80211_mark_disconnected(struct wpa_driver_nl80211_data *drv)
554{
555 if (drv->associated)
556 os_memcpy(drv->prev_bssid, drv->bssid, ETH_ALEN);
557 drv->associated = 0;
558 os_memset(drv->bssid, 0, ETH_ALEN);
559}
560
561
Jouni Malinen87fd2792011-05-16 18:35:42 +0300562struct nl80211_bss_info_arg {
563 struct wpa_driver_nl80211_data *drv;
564 struct wpa_scan_results *res;
565 unsigned int assoc_freq;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800566 u8 assoc_bssid[ETH_ALEN];
Jouni Malinen87fd2792011-05-16 18:35:42 +0300567};
568
569static int bss_info_handler(struct nl_msg *msg, void *arg);
570
571
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700572/* nl80211 code */
573static int ack_handler(struct nl_msg *msg, void *arg)
574{
575 int *err = arg;
576 *err = 0;
577 return NL_STOP;
578}
579
580static int finish_handler(struct nl_msg *msg, void *arg)
581{
582 int *ret = arg;
583 *ret = 0;
584 return NL_SKIP;
585}
586
587static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
588 void *arg)
589{
590 int *ret = arg;
591 *ret = err->error;
592 return NL_SKIP;
593}
594
595
596static int no_seq_check(struct nl_msg *msg, void *arg)
597{
598 return NL_OK;
599}
600
601
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800602static int send_and_recv(struct nl80211_global *global,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700603 struct nl_handle *nl_handle, struct nl_msg *msg,
604 int (*valid_handler)(struct nl_msg *, void *),
605 void *valid_data)
606{
607 struct nl_cb *cb;
608 int err = -ENOMEM;
609
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800610 cb = nl_cb_clone(global->nl_cb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700611 if (!cb)
612 goto out;
613
614 err = nl_send_auto_complete(nl_handle, msg);
615 if (err < 0)
616 goto out;
617
618 err = 1;
619
620 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
621 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
622 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
623
624 if (valid_handler)
625 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM,
626 valid_handler, valid_data);
627
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700628 while (err > 0) {
629 int res = nl_recvmsgs(nl_handle, cb);
630 if (res) {
631 wpa_printf(MSG_INFO,
632 "nl80211: %s->nl_recvmsgs failed: %d",
633 __func__, res);
634 }
635 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700636 out:
637 nl_cb_put(cb);
638 nlmsg_free(msg);
639 return err;
640}
641
642
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800643static int send_and_recv_msgs_global(struct nl80211_global *global,
644 struct nl_msg *msg,
645 int (*valid_handler)(struct nl_msg *, void *),
646 void *valid_data)
647{
648 return send_and_recv(global, global->nl, msg, valid_handler,
649 valid_data);
650}
651
Dmitry Shmidt04949592012-07-19 12:16:46 -0700652
Dmitry Shmidt641185e2013-11-06 15:17:13 -0800653static int send_and_recv_msgs(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700654 struct nl_msg *msg,
655 int (*valid_handler)(struct nl_msg *, void *),
656 void *valid_data)
657{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800658 return send_and_recv(drv->global, drv->global->nl, msg,
659 valid_handler, valid_data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700660}
661
662
663struct family_data {
664 const char *group;
665 int id;
666};
667
668
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700669static int nl80211_set_iface_id(struct nl_msg *msg, struct i802_bss *bss)
670{
671 if (bss->wdev_id_set)
672 NLA_PUT_U64(msg, NL80211_ATTR_WDEV, bss->wdev_id);
673 else
674 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
675 return 0;
676
677nla_put_failure:
678 return -1;
679}
680
681
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700682static int family_handler(struct nl_msg *msg, void *arg)
683{
684 struct family_data *res = arg;
685 struct nlattr *tb[CTRL_ATTR_MAX + 1];
686 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
687 struct nlattr *mcgrp;
688 int i;
689
690 nla_parse(tb, CTRL_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
691 genlmsg_attrlen(gnlh, 0), NULL);
692 if (!tb[CTRL_ATTR_MCAST_GROUPS])
693 return NL_SKIP;
694
695 nla_for_each_nested(mcgrp, tb[CTRL_ATTR_MCAST_GROUPS], i) {
696 struct nlattr *tb2[CTRL_ATTR_MCAST_GRP_MAX + 1];
697 nla_parse(tb2, CTRL_ATTR_MCAST_GRP_MAX, nla_data(mcgrp),
698 nla_len(mcgrp), NULL);
699 if (!tb2[CTRL_ATTR_MCAST_GRP_NAME] ||
700 !tb2[CTRL_ATTR_MCAST_GRP_ID] ||
701 os_strncmp(nla_data(tb2[CTRL_ATTR_MCAST_GRP_NAME]),
702 res->group,
703 nla_len(tb2[CTRL_ATTR_MCAST_GRP_NAME])) != 0)
704 continue;
705 res->id = nla_get_u32(tb2[CTRL_ATTR_MCAST_GRP_ID]);
706 break;
707 };
708
709 return NL_SKIP;
710}
711
712
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800713static int nl_get_multicast_id(struct nl80211_global *global,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700714 const char *family, const char *group)
715{
716 struct nl_msg *msg;
717 int ret = -1;
718 struct family_data res = { group, -ENOENT };
719
720 msg = nlmsg_alloc();
721 if (!msg)
722 return -ENOMEM;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800723 genlmsg_put(msg, 0, 0, genl_ctrl_resolve(global->nl, "nlctrl"),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700724 0, 0, CTRL_CMD_GETFAMILY, 0);
725 NLA_PUT_STRING(msg, CTRL_ATTR_FAMILY_NAME, family);
726
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800727 ret = send_and_recv_msgs_global(global, msg, family_handler, &res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700728 msg = NULL;
729 if (ret == 0)
730 ret = res.id;
731
732nla_put_failure:
733 nlmsg_free(msg);
734 return ret;
735}
736
737
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800738static void * nl80211_cmd(struct wpa_driver_nl80211_data *drv,
739 struct nl_msg *msg, int flags, uint8_t cmd)
740{
741 return genlmsg_put(msg, 0, 0, drv->global->nl80211_id,
742 0, flags, cmd, 0);
743}
744
745
746struct wiphy_idx_data {
747 int wiphy_idx;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700748 enum nl80211_iftype nlmode;
749 u8 *macaddr;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800750};
751
752
753static int netdev_info_handler(struct nl_msg *msg, void *arg)
754{
755 struct nlattr *tb[NL80211_ATTR_MAX + 1];
756 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
757 struct wiphy_idx_data *info = arg;
758
759 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
760 genlmsg_attrlen(gnlh, 0), NULL);
761
762 if (tb[NL80211_ATTR_WIPHY])
763 info->wiphy_idx = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
764
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700765 if (tb[NL80211_ATTR_IFTYPE])
766 info->nlmode = nla_get_u32(tb[NL80211_ATTR_IFTYPE]);
767
768 if (tb[NL80211_ATTR_MAC] && info->macaddr)
769 os_memcpy(info->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
770 ETH_ALEN);
771
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800772 return NL_SKIP;
773}
774
775
776static int nl80211_get_wiphy_index(struct i802_bss *bss)
777{
778 struct nl_msg *msg;
779 struct wiphy_idx_data data = {
780 .wiphy_idx = -1,
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700781 .macaddr = NULL,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800782 };
783
784 msg = nlmsg_alloc();
785 if (!msg)
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700786 return NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800787
788 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_GET_INTERFACE);
789
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700790 if (nl80211_set_iface_id(msg, bss) < 0)
791 goto nla_put_failure;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800792
793 if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0)
794 return data.wiphy_idx;
795 msg = NULL;
796nla_put_failure:
797 nlmsg_free(msg);
798 return -1;
799}
800
801
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700802static enum nl80211_iftype nl80211_get_ifmode(struct i802_bss *bss)
803{
804 struct nl_msg *msg;
805 struct wiphy_idx_data data = {
806 .nlmode = NL80211_IFTYPE_UNSPECIFIED,
807 .macaddr = NULL,
808 };
809
810 msg = nlmsg_alloc();
811 if (!msg)
812 return -1;
813
814 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_GET_INTERFACE);
815
816 if (nl80211_set_iface_id(msg, bss) < 0)
817 goto nla_put_failure;
818
819 if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0)
820 return data.nlmode;
821 msg = NULL;
822nla_put_failure:
823 nlmsg_free(msg);
824 return NL80211_IFTYPE_UNSPECIFIED;
825}
826
827
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700828static int nl80211_get_macaddr(struct i802_bss *bss)
829{
830 struct nl_msg *msg;
831 struct wiphy_idx_data data = {
832 .macaddr = bss->addr,
833 };
834
835 msg = nlmsg_alloc();
836 if (!msg)
837 return NL80211_IFTYPE_UNSPECIFIED;
838
839 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_GET_INTERFACE);
840 if (nl80211_set_iface_id(msg, bss) < 0)
841 goto nla_put_failure;
842
843 return send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data);
844
845nla_put_failure:
846 nlmsg_free(msg);
847 return NL80211_IFTYPE_UNSPECIFIED;
848}
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700849
850
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800851static int nl80211_register_beacons(struct wpa_driver_nl80211_data *drv,
852 struct nl80211_wiphy_data *w)
853{
854 struct nl_msg *msg;
855 int ret = -1;
856
857 msg = nlmsg_alloc();
858 if (!msg)
859 return -1;
860
861 nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_BEACONS);
862
863 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, w->wiphy_idx);
864
865 ret = send_and_recv(drv->global, w->nl_beacons, msg, NULL, NULL);
866 msg = NULL;
867 if (ret) {
868 wpa_printf(MSG_DEBUG, "nl80211: Register beacons command "
869 "failed: ret=%d (%s)",
870 ret, strerror(-ret));
871 goto nla_put_failure;
872 }
873 ret = 0;
874nla_put_failure:
875 nlmsg_free(msg);
876 return ret;
877}
878
879
880static void nl80211_recv_beacons(int sock, void *eloop_ctx, void *handle)
881{
882 struct nl80211_wiphy_data *w = eloop_ctx;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700883 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800884
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800885 wpa_printf(MSG_EXCESSIVE, "nl80211: Beacon event message available");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800886
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700887 res = nl_recvmsgs(handle, w->nl_cb);
888 if (res) {
889 wpa_printf(MSG_INFO, "nl80211: %s->nl_recvmsgs failed: %d",
890 __func__, res);
891 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800892}
893
894
895static int process_beacon_event(struct nl_msg *msg, void *arg)
896{
897 struct nl80211_wiphy_data *w = arg;
898 struct wpa_driver_nl80211_data *drv;
899 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
900 struct nlattr *tb[NL80211_ATTR_MAX + 1];
901 union wpa_event_data event;
902
903 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
904 genlmsg_attrlen(gnlh, 0), NULL);
905
906 if (gnlh->cmd != NL80211_CMD_FRAME) {
907 wpa_printf(MSG_DEBUG, "nl80211: Unexpected beacon event? (%d)",
908 gnlh->cmd);
909 return NL_SKIP;
910 }
911
912 if (!tb[NL80211_ATTR_FRAME])
913 return NL_SKIP;
914
915 dl_list_for_each(drv, &w->drvs, struct wpa_driver_nl80211_data,
916 wiphy_list) {
917 os_memset(&event, 0, sizeof(event));
918 event.rx_mgmt.frame = nla_data(tb[NL80211_ATTR_FRAME]);
919 event.rx_mgmt.frame_len = nla_len(tb[NL80211_ATTR_FRAME]);
920 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
921 }
922
923 return NL_SKIP;
924}
925
926
927static struct nl80211_wiphy_data *
928nl80211_get_wiphy_data_ap(struct i802_bss *bss)
929{
930 static DEFINE_DL_LIST(nl80211_wiphys);
931 struct nl80211_wiphy_data *w;
932 int wiphy_idx, found = 0;
933 struct i802_bss *tmp_bss;
934
935 if (bss->wiphy_data != NULL)
936 return bss->wiphy_data;
937
938 wiphy_idx = nl80211_get_wiphy_index(bss);
939
940 dl_list_for_each(w, &nl80211_wiphys, struct nl80211_wiphy_data, list) {
941 if (w->wiphy_idx == wiphy_idx)
942 goto add;
943 }
944
945 /* alloc new one */
946 w = os_zalloc(sizeof(*w));
947 if (w == NULL)
948 return NULL;
949 w->wiphy_idx = wiphy_idx;
950 dl_list_init(&w->bsss);
951 dl_list_init(&w->drvs);
952
953 w->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
954 if (!w->nl_cb) {
955 os_free(w);
956 return NULL;
957 }
958 nl_cb_set(w->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL);
959 nl_cb_set(w->nl_cb, NL_CB_VALID, NL_CB_CUSTOM, process_beacon_event,
960 w);
961
962 w->nl_beacons = nl_create_handle(bss->drv->global->nl_cb,
963 "wiphy beacons");
964 if (w->nl_beacons == NULL) {
965 os_free(w);
966 return NULL;
967 }
968
969 if (nl80211_register_beacons(bss->drv, w)) {
970 nl_destroy_handles(&w->nl_beacons);
971 os_free(w);
972 return NULL;
973 }
974
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700975 nl80211_register_eloop_read(&w->nl_beacons, nl80211_recv_beacons, w);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800976
977 dl_list_add(&nl80211_wiphys, &w->list);
978
979add:
980 /* drv entry for this bss already there? */
981 dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
982 if (tmp_bss->drv == bss->drv) {
983 found = 1;
984 break;
985 }
986 }
987 /* if not add it */
988 if (!found)
989 dl_list_add(&w->drvs, &bss->drv->wiphy_list);
990
991 dl_list_add(&w->bsss, &bss->wiphy_list);
992 bss->wiphy_data = w;
993 return w;
994}
995
996
997static void nl80211_put_wiphy_data_ap(struct i802_bss *bss)
998{
999 struct nl80211_wiphy_data *w = bss->wiphy_data;
1000 struct i802_bss *tmp_bss;
1001 int found = 0;
1002
1003 if (w == NULL)
1004 return;
1005 bss->wiphy_data = NULL;
1006 dl_list_del(&bss->wiphy_list);
1007
1008 /* still any for this drv present? */
1009 dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
1010 if (tmp_bss->drv == bss->drv) {
1011 found = 1;
1012 break;
1013 }
1014 }
1015 /* if not remove it */
1016 if (!found)
1017 dl_list_del(&bss->drv->wiphy_list);
1018
1019 if (!dl_list_empty(&w->bsss))
1020 return;
1021
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001022 nl80211_destroy_eloop_handle(&w->nl_beacons);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001023
1024 nl_cb_put(w->nl_cb);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001025 dl_list_del(&w->list);
1026 os_free(w);
1027}
1028
1029
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001030static int wpa_driver_nl80211_get_bssid(void *priv, u8 *bssid)
1031{
1032 struct i802_bss *bss = priv;
1033 struct wpa_driver_nl80211_data *drv = bss->drv;
1034 if (!drv->associated)
1035 return -1;
1036 os_memcpy(bssid, drv->bssid, ETH_ALEN);
1037 return 0;
1038}
1039
1040
1041static int wpa_driver_nl80211_get_ssid(void *priv, u8 *ssid)
1042{
1043 struct i802_bss *bss = priv;
1044 struct wpa_driver_nl80211_data *drv = bss->drv;
1045 if (!drv->associated)
1046 return -1;
1047 os_memcpy(ssid, drv->ssid, drv->ssid_len);
1048 return drv->ssid_len;
1049}
1050
1051
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001052static void wpa_driver_nl80211_event_newlink(
1053 struct wpa_driver_nl80211_data *drv, char *ifname)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001054{
1055 union wpa_event_data event;
1056
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001057 if (os_strcmp(drv->first_bss->ifname, ifname) == 0) {
1058 if (if_nametoindex(drv->first_bss->ifname) == 0) {
1059 wpa_printf(MSG_DEBUG, "nl80211: Interface %s does not exist - ignore RTM_NEWLINK",
1060 drv->first_bss->ifname);
1061 return;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001062 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001063 if (!drv->if_removed)
1064 return;
1065 wpa_printf(MSG_DEBUG, "nl80211: Mark if_removed=0 for %s based on RTM_NEWLINK event",
1066 drv->first_bss->ifname);
1067 drv->if_removed = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001068 }
1069
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001070 os_memset(&event, 0, sizeof(event));
1071 os_strlcpy(event.interface_status.ifname, ifname,
1072 sizeof(event.interface_status.ifname));
1073 event.interface_status.ievent = EVENT_INTERFACE_ADDED;
1074 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
1075}
1076
1077
1078static void wpa_driver_nl80211_event_dellink(
1079 struct wpa_driver_nl80211_data *drv, char *ifname)
1080{
1081 union wpa_event_data event;
1082
1083 if (os_strcmp(drv->first_bss->ifname, ifname) == 0) {
1084 if (drv->if_removed) {
1085 wpa_printf(MSG_DEBUG, "nl80211: if_removed already set - ignore RTM_DELLINK event for %s",
1086 ifname);
1087 return;
1088 }
1089 wpa_printf(MSG_DEBUG, "RTM_DELLINK: Interface '%s' removed - mark if_removed=1",
1090 ifname);
1091 drv->if_removed = 1;
1092 } else {
1093 wpa_printf(MSG_DEBUG, "RTM_DELLINK: Interface '%s' removed",
1094 ifname);
1095 }
1096
1097 os_memset(&event, 0, sizeof(event));
1098 os_strlcpy(event.interface_status.ifname, ifname,
1099 sizeof(event.interface_status.ifname));
1100 event.interface_status.ievent = EVENT_INTERFACE_REMOVED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001101 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
1102}
1103
1104
1105static int wpa_driver_nl80211_own_ifname(struct wpa_driver_nl80211_data *drv,
1106 u8 *buf, size_t len)
1107{
1108 int attrlen, rta_len;
1109 struct rtattr *attr;
1110
1111 attrlen = len;
1112 attr = (struct rtattr *) buf;
1113
1114 rta_len = RTA_ALIGN(sizeof(struct rtattr));
1115 while (RTA_OK(attr, attrlen)) {
1116 if (attr->rta_type == IFLA_IFNAME) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001117 if (os_strcmp(((char *) attr) + rta_len,
1118 drv->first_bss->ifname) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001119 return 1;
1120 else
1121 break;
1122 }
1123 attr = RTA_NEXT(attr, attrlen);
1124 }
1125
1126 return 0;
1127}
1128
1129
1130static int wpa_driver_nl80211_own_ifindex(struct wpa_driver_nl80211_data *drv,
1131 int ifindex, u8 *buf, size_t len)
1132{
1133 if (drv->ifindex == ifindex)
1134 return 1;
1135
1136 if (drv->if_removed && wpa_driver_nl80211_own_ifname(drv, buf, len)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001137 wpa_printf(MSG_DEBUG, "nl80211: Update ifindex for a removed "
1138 "interface");
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001139 wpa_driver_nl80211_finish_drv_init(drv, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001140 return 1;
1141 }
1142
1143 return 0;
1144}
1145
1146
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001147static struct wpa_driver_nl80211_data *
1148nl80211_find_drv(struct nl80211_global *global, int idx, u8 *buf, size_t len)
1149{
1150 struct wpa_driver_nl80211_data *drv;
1151 dl_list_for_each(drv, &global->interfaces,
1152 struct wpa_driver_nl80211_data, list) {
1153 if (wpa_driver_nl80211_own_ifindex(drv, idx, buf, len) ||
1154 have_ifidx(drv, idx))
1155 return drv;
1156 }
1157 return NULL;
1158}
1159
1160
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001161static void wpa_driver_nl80211_event_rtm_newlink(void *ctx,
1162 struct ifinfomsg *ifi,
1163 u8 *buf, size_t len)
1164{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001165 struct nl80211_global *global = ctx;
1166 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001167 int attrlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001168 struct rtattr *attr;
1169 u32 brid = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001170 char namebuf[IFNAMSIZ];
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001171 char ifname[IFNAMSIZ + 1];
1172 char extra[100], *pos, *end;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001173
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001174 drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
1175 if (!drv) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001176 wpa_printf(MSG_DEBUG, "nl80211: Ignore RTM_NEWLINK event for foreign ifindex %d",
1177 ifi->ifi_index);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001178 return;
1179 }
1180
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001181 extra[0] = '\0';
1182 pos = extra;
1183 end = pos + sizeof(extra);
1184 ifname[0] = '\0';
1185
1186 attrlen = len;
1187 attr = (struct rtattr *) buf;
1188 while (RTA_OK(attr, attrlen)) {
1189 switch (attr->rta_type) {
1190 case IFLA_IFNAME:
1191 if (RTA_PAYLOAD(attr) >= IFNAMSIZ)
1192 break;
1193 os_memcpy(ifname, RTA_DATA(attr), RTA_PAYLOAD(attr));
1194 ifname[RTA_PAYLOAD(attr)] = '\0';
1195 break;
1196 case IFLA_MASTER:
1197 brid = nla_get_u32((struct nlattr *) attr);
1198 pos += os_snprintf(pos, end - pos, " master=%u", brid);
1199 break;
1200 case IFLA_WIRELESS:
1201 pos += os_snprintf(pos, end - pos, " wext");
1202 break;
1203 case IFLA_OPERSTATE:
1204 pos += os_snprintf(pos, end - pos, " operstate=%u",
1205 nla_get_u32((struct nlattr *) attr));
1206 break;
1207 case IFLA_LINKMODE:
1208 pos += os_snprintf(pos, end - pos, " linkmode=%u",
1209 nla_get_u32((struct nlattr *) attr));
1210 break;
1211 }
1212 attr = RTA_NEXT(attr, attrlen);
1213 }
1214 extra[sizeof(extra) - 1] = '\0';
1215
1216 wpa_printf(MSG_DEBUG, "RTM_NEWLINK: ifi_index=%d ifname=%s%s ifi_flags=0x%x (%s%s%s%s)",
1217 ifi->ifi_index, ifname, extra, ifi->ifi_flags,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001218 (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
1219 (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
1220 (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
1221 (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
1222
1223 if (!drv->if_disabled && !(ifi->ifi_flags & IFF_UP)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001224 if (if_indextoname(ifi->ifi_index, namebuf) &&
1225 linux_iface_up(drv->global->ioctl_sock,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001226 drv->first_bss->ifname) > 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001227 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
1228 "event since interface %s is up", namebuf);
1229 return;
1230 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001231 wpa_printf(MSG_DEBUG, "nl80211: Interface down");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001232 if (drv->ignore_if_down_event) {
1233 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
1234 "event generated by mode change");
1235 drv->ignore_if_down_event = 0;
1236 } else {
1237 drv->if_disabled = 1;
1238 wpa_supplicant_event(drv->ctx,
1239 EVENT_INTERFACE_DISABLED, NULL);
1240 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001241 }
1242
1243 if (drv->if_disabled && (ifi->ifi_flags & IFF_UP)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001244 if (if_indextoname(ifi->ifi_index, namebuf) &&
1245 linux_iface_up(drv->global->ioctl_sock,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001246 drv->first_bss->ifname) == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001247 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
1248 "event since interface %s is down",
1249 namebuf);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001250 } else if (if_nametoindex(drv->first_bss->ifname) == 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001251 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
1252 "event since interface %s does not exist",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001253 drv->first_bss->ifname);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001254 } else if (drv->if_removed) {
1255 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
1256 "event since interface %s is marked "
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001257 "removed", drv->first_bss->ifname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001258 } else {
1259 wpa_printf(MSG_DEBUG, "nl80211: Interface up");
1260 drv->if_disabled = 0;
1261 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_ENABLED,
1262 NULL);
1263 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001264 }
1265
1266 /*
1267 * Some drivers send the association event before the operup event--in
1268 * this case, lifting operstate in wpa_driver_nl80211_set_operstate()
1269 * fails. This will hit us when wpa_supplicant does not need to do
1270 * IEEE 802.1X authentication
1271 */
1272 if (drv->operstate == 1 &&
1273 (ifi->ifi_flags & (IFF_LOWER_UP | IFF_DORMANT)) == IFF_LOWER_UP &&
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001274 !(ifi->ifi_flags & IFF_RUNNING)) {
1275 wpa_printf(MSG_DEBUG, "nl80211: Set IF_OPER_UP again based on ifi_flags and expected operstate");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001276 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001277 -1, IF_OPER_UP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001278 }
1279
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001280 if (ifname[0])
1281 wpa_driver_nl80211_event_newlink(drv, ifname);
1282
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001283 if (ifi->ifi_family == AF_BRIDGE && brid) {
1284 /* device has been added to bridge */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001285 if_indextoname(brid, namebuf);
1286 wpa_printf(MSG_DEBUG, "nl80211: Add ifindex %u for bridge %s",
1287 brid, namebuf);
1288 add_ifidx(drv, brid);
1289 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001290}
1291
1292
1293static void wpa_driver_nl80211_event_rtm_dellink(void *ctx,
1294 struct ifinfomsg *ifi,
1295 u8 *buf, size_t len)
1296{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001297 struct nl80211_global *global = ctx;
1298 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001299 int attrlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001300 struct rtattr *attr;
1301 u32 brid = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001302 char ifname[IFNAMSIZ + 1];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001303
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001304 drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
1305 if (!drv) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001306 wpa_printf(MSG_DEBUG, "nl80211: Ignore RTM_DELLINK event for foreign ifindex %d",
1307 ifi->ifi_index);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001308 return;
1309 }
1310
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001311 ifname[0] = '\0';
1312
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001313 attrlen = len;
1314 attr = (struct rtattr *) buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001315 while (RTA_OK(attr, attrlen)) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001316 switch (attr->rta_type) {
1317 case IFLA_IFNAME:
1318 if (RTA_PAYLOAD(attr) >= IFNAMSIZ)
1319 break;
1320 os_memcpy(ifname, RTA_DATA(attr), RTA_PAYLOAD(attr));
1321 ifname[RTA_PAYLOAD(attr)] = '\0';
1322 break;
1323 case IFLA_MASTER:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001324 brid = nla_get_u32((struct nlattr *) attr);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001325 break;
1326 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001327 attr = RTA_NEXT(attr, attrlen);
1328 }
1329
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001330 if (ifname[0])
1331 wpa_driver_nl80211_event_dellink(drv, ifname);
1332
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001333 if (ifi->ifi_family == AF_BRIDGE && brid) {
1334 /* device has been removed from bridge */
1335 char namebuf[IFNAMSIZ];
1336 if_indextoname(brid, namebuf);
1337 wpa_printf(MSG_DEBUG, "nl80211: Remove ifindex %u for bridge "
1338 "%s", brid, namebuf);
1339 del_ifidx(drv, brid);
1340 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001341}
1342
1343
1344static void mlme_event_auth(struct wpa_driver_nl80211_data *drv,
1345 const u8 *frame, size_t len)
1346{
1347 const struct ieee80211_mgmt *mgmt;
1348 union wpa_event_data event;
1349
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001350 wpa_printf(MSG_DEBUG, "nl80211: Authenticate event");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001351 mgmt = (const struct ieee80211_mgmt *) frame;
1352 if (len < 24 + sizeof(mgmt->u.auth)) {
1353 wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
1354 "frame");
1355 return;
1356 }
1357
1358 os_memcpy(drv->auth_bssid, mgmt->sa, ETH_ALEN);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001359 os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001360 os_memset(&event, 0, sizeof(event));
1361 os_memcpy(event.auth.peer, mgmt->sa, ETH_ALEN);
1362 event.auth.auth_type = le_to_host16(mgmt->u.auth.auth_alg);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001363 event.auth.auth_transaction =
1364 le_to_host16(mgmt->u.auth.auth_transaction);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001365 event.auth.status_code = le_to_host16(mgmt->u.auth.status_code);
1366 if (len > 24 + sizeof(mgmt->u.auth)) {
1367 event.auth.ies = mgmt->u.auth.variable;
1368 event.auth.ies_len = len - 24 - sizeof(mgmt->u.auth);
1369 }
1370
1371 wpa_supplicant_event(drv->ctx, EVENT_AUTH, &event);
1372}
1373
1374
Jouni Malinen87fd2792011-05-16 18:35:42 +03001375static unsigned int nl80211_get_assoc_freq(struct wpa_driver_nl80211_data *drv)
1376{
1377 struct nl_msg *msg;
1378 int ret;
1379 struct nl80211_bss_info_arg arg;
1380
1381 os_memset(&arg, 0, sizeof(arg));
1382 msg = nlmsg_alloc();
1383 if (!msg)
1384 goto nla_put_failure;
1385
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001386 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
Jouni Malinen87fd2792011-05-16 18:35:42 +03001387 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
1388
1389 arg.drv = drv;
1390 ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
1391 msg = NULL;
1392 if (ret == 0) {
1393 wpa_printf(MSG_DEBUG, "nl80211: Operating frequency for the "
1394 "associated BSS from scan results: %u MHz",
1395 arg.assoc_freq);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001396 if (arg.assoc_freq)
1397 drv->assoc_freq = arg.assoc_freq;
1398 return drv->assoc_freq;
Jouni Malinen87fd2792011-05-16 18:35:42 +03001399 }
1400 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
1401 "(%s)", ret, strerror(-ret));
1402nla_put_failure:
1403 nlmsg_free(msg);
1404 return drv->assoc_freq;
1405}
1406
1407
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001408static void mlme_event_assoc(struct wpa_driver_nl80211_data *drv,
1409 const u8 *frame, size_t len)
1410{
1411 const struct ieee80211_mgmt *mgmt;
1412 union wpa_event_data event;
1413 u16 status;
1414
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001415 wpa_printf(MSG_DEBUG, "nl80211: Associate event");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001416 mgmt = (const struct ieee80211_mgmt *) frame;
1417 if (len < 24 + sizeof(mgmt->u.assoc_resp)) {
1418 wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
1419 "frame");
1420 return;
1421 }
1422
1423 status = le_to_host16(mgmt->u.assoc_resp.status_code);
1424 if (status != WLAN_STATUS_SUCCESS) {
1425 os_memset(&event, 0, sizeof(event));
1426 event.assoc_reject.bssid = mgmt->bssid;
1427 if (len > 24 + sizeof(mgmt->u.assoc_resp)) {
1428 event.assoc_reject.resp_ies =
1429 (u8 *) mgmt->u.assoc_resp.variable;
1430 event.assoc_reject.resp_ies_len =
1431 len - 24 - sizeof(mgmt->u.assoc_resp);
1432 }
1433 event.assoc_reject.status_code = status;
1434
1435 wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
1436 return;
1437 }
1438
1439 drv->associated = 1;
1440 os_memcpy(drv->bssid, mgmt->sa, ETH_ALEN);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001441 os_memcpy(drv->prev_bssid, mgmt->sa, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001442
1443 os_memset(&event, 0, sizeof(event));
1444 if (len > 24 + sizeof(mgmt->u.assoc_resp)) {
1445 event.assoc_info.resp_ies = (u8 *) mgmt->u.assoc_resp.variable;
1446 event.assoc_info.resp_ies_len =
1447 len - 24 - sizeof(mgmt->u.assoc_resp);
1448 }
1449
1450 event.assoc_info.freq = drv->assoc_freq;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001451
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001452 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
1453}
1454
1455
1456static void mlme_event_connect(struct wpa_driver_nl80211_data *drv,
1457 enum nl80211_commands cmd, struct nlattr *status,
1458 struct nlattr *addr, struct nlattr *req_ie,
1459 struct nlattr *resp_ie)
1460{
1461 union wpa_event_data event;
1462
1463 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
1464 /*
1465 * Avoid reporting two association events that would confuse
1466 * the core code.
1467 */
1468 wpa_printf(MSG_DEBUG, "nl80211: Ignore connect event (cmd=%d) "
1469 "when using userspace SME", cmd);
1470 return;
1471 }
1472
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001473 if (cmd == NL80211_CMD_CONNECT)
1474 wpa_printf(MSG_DEBUG, "nl80211: Connect event");
1475 else if (cmd == NL80211_CMD_ROAM)
1476 wpa_printf(MSG_DEBUG, "nl80211: Roam event");
1477
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001478 os_memset(&event, 0, sizeof(event));
1479 if (cmd == NL80211_CMD_CONNECT &&
1480 nla_get_u16(status) != WLAN_STATUS_SUCCESS) {
1481 if (addr)
1482 event.assoc_reject.bssid = nla_data(addr);
1483 if (resp_ie) {
1484 event.assoc_reject.resp_ies = nla_data(resp_ie);
1485 event.assoc_reject.resp_ies_len = nla_len(resp_ie);
1486 }
1487 event.assoc_reject.status_code = nla_get_u16(status);
1488 wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
1489 return;
1490 }
1491
1492 drv->associated = 1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001493 if (addr) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001494 os_memcpy(drv->bssid, nla_data(addr), ETH_ALEN);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001495 os_memcpy(drv->prev_bssid, drv->bssid, ETH_ALEN);
1496 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001497
1498 if (req_ie) {
1499 event.assoc_info.req_ies = nla_data(req_ie);
1500 event.assoc_info.req_ies_len = nla_len(req_ie);
1501 }
1502 if (resp_ie) {
1503 event.assoc_info.resp_ies = nla_data(resp_ie);
1504 event.assoc_info.resp_ies_len = nla_len(resp_ie);
1505 }
1506
Jouni Malinen87fd2792011-05-16 18:35:42 +03001507 event.assoc_info.freq = nl80211_get_assoc_freq(drv);
1508
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001509 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
1510}
1511
1512
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001513static void mlme_event_disconnect(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001514 struct nlattr *reason, struct nlattr *addr,
1515 struct nlattr *by_ap)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001516{
1517 union wpa_event_data data;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001518 unsigned int locally_generated = by_ap == NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001519
1520 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
1521 /*
1522 * Avoid reporting two disassociation events that could
1523 * confuse the core code.
1524 */
1525 wpa_printf(MSG_DEBUG, "nl80211: Ignore disconnect "
1526 "event when using userspace SME");
1527 return;
1528 }
1529
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001530 if (drv->ignore_next_local_disconnect) {
1531 drv->ignore_next_local_disconnect = 0;
1532 if (locally_generated) {
1533 wpa_printf(MSG_DEBUG, "nl80211: Ignore disconnect "
1534 "event triggered during reassociation");
1535 return;
1536 }
1537 wpa_printf(MSG_WARNING, "nl80211: Was expecting local "
1538 "disconnect but got another disconnect "
1539 "event first");
1540 }
1541
1542 wpa_printf(MSG_DEBUG, "nl80211: Disconnect event");
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001543 nl80211_mark_disconnected(drv);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001544 os_memset(&data, 0, sizeof(data));
1545 if (reason)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001546 data.deauth_info.reason_code = nla_get_u16(reason);
1547 data.deauth_info.locally_generated = by_ap == NULL;
1548 wpa_supplicant_event(drv->ctx, EVENT_DEAUTH, &data);
1549}
1550
1551
1552static void mlme_event_ch_switch(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001553 struct nlattr *ifindex, struct nlattr *freq,
1554 struct nlattr *type, struct nlattr *bw,
1555 struct nlattr *cf1, struct nlattr *cf2)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001556{
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001557 struct i802_bss *bss;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001558 union wpa_event_data data;
1559 int ht_enabled = 1;
1560 int chan_offset = 0;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001561 int ifidx;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001562
1563 wpa_printf(MSG_DEBUG, "nl80211: Channel switch event");
1564
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001565 if (!freq)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001566 return;
1567
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001568 ifidx = nla_get_u32(ifindex);
1569 for (bss = drv->first_bss; bss; bss = bss->next)
1570 if (bss->ifindex == ifidx)
1571 break;
1572
1573 if (bss == NULL) {
1574 wpa_printf(MSG_WARNING, "nl80211: Unknown ifindex (%d) for channel switch, ignoring",
1575 ifidx);
1576 return;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001577 }
1578
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001579 if (type) {
1580 switch (nla_get_u32(type)) {
1581 case NL80211_CHAN_NO_HT:
1582 ht_enabled = 0;
1583 break;
1584 case NL80211_CHAN_HT20:
1585 break;
1586 case NL80211_CHAN_HT40PLUS:
1587 chan_offset = 1;
1588 break;
1589 case NL80211_CHAN_HT40MINUS:
1590 chan_offset = -1;
1591 break;
1592 }
1593 }
1594
1595 os_memset(&data, 0, sizeof(data));
Dmitry Shmidt04949592012-07-19 12:16:46 -07001596 data.ch_switch.freq = nla_get_u32(freq);
1597 data.ch_switch.ht_enabled = ht_enabled;
1598 data.ch_switch.ch_offset = chan_offset;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001599 if (bw)
1600 data.ch_switch.ch_width = convert2width(nla_get_u32(bw));
1601 if (cf1)
1602 data.ch_switch.cf1 = nla_get_u32(cf1);
1603 if (cf2)
1604 data.ch_switch.cf2 = nla_get_u32(cf2);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001605
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001606 bss->freq = data.ch_switch.freq;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001607
Dmitry Shmidt04949592012-07-19 12:16:46 -07001608 wpa_supplicant_event(drv->ctx, EVENT_CH_SWITCH, &data);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001609}
1610
1611
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001612static void mlme_timeout_event(struct wpa_driver_nl80211_data *drv,
1613 enum nl80211_commands cmd, struct nlattr *addr)
1614{
1615 union wpa_event_data event;
1616 enum wpa_event_type ev;
1617
1618 if (nla_len(addr) != ETH_ALEN)
1619 return;
1620
1621 wpa_printf(MSG_DEBUG, "nl80211: MLME event %d; timeout with " MACSTR,
1622 cmd, MAC2STR((u8 *) nla_data(addr)));
1623
1624 if (cmd == NL80211_CMD_AUTHENTICATE)
1625 ev = EVENT_AUTH_TIMED_OUT;
1626 else if (cmd == NL80211_CMD_ASSOCIATE)
1627 ev = EVENT_ASSOC_TIMED_OUT;
1628 else
1629 return;
1630
1631 os_memset(&event, 0, sizeof(event));
1632 os_memcpy(event.timeout_event.addr, nla_data(addr), ETH_ALEN);
1633 wpa_supplicant_event(drv->ctx, ev, &event);
1634}
1635
1636
1637static void mlme_event_mgmt(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001638 struct nlattr *freq, struct nlattr *sig,
1639 const u8 *frame, size_t len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001640{
1641 const struct ieee80211_mgmt *mgmt;
1642 union wpa_event_data event;
1643 u16 fc, stype;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001644 int ssi_signal = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001645 int rx_freq = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001646
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001647 wpa_printf(MSG_MSGDUMP, "nl80211: Frame event");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001648 mgmt = (const struct ieee80211_mgmt *) frame;
1649 if (len < 24) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001650 wpa_printf(MSG_DEBUG, "nl80211: Too short management frame");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001651 return;
1652 }
1653
1654 fc = le_to_host16(mgmt->frame_control);
1655 stype = WLAN_FC_GET_STYPE(fc);
1656
Dmitry Shmidt04949592012-07-19 12:16:46 -07001657 if (sig)
1658 ssi_signal = (s32) nla_get_u32(sig);
1659
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001660 os_memset(&event, 0, sizeof(event));
1661 if (freq) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001662 event.rx_mgmt.freq = nla_get_u32(freq);
1663 rx_freq = drv->last_mgmt_freq = event.rx_mgmt.freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001664 }
Dmitry Shmidt56052862013-10-04 10:23:25 -07001665 wpa_printf(MSG_DEBUG,
1666 "nl80211: RX frame freq=%d ssi_signal=%d stype=%u len=%u",
1667 rx_freq, ssi_signal, stype, (unsigned int) len);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001668 event.rx_mgmt.frame = frame;
1669 event.rx_mgmt.frame_len = len;
1670 event.rx_mgmt.ssi_signal = ssi_signal;
1671 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001672}
1673
1674
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001675static void mlme_event_mgmt_tx_status(struct wpa_driver_nl80211_data *drv,
1676 struct nlattr *cookie, const u8 *frame,
1677 size_t len, struct nlattr *ack)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001678{
1679 union wpa_event_data event;
1680 const struct ieee80211_hdr *hdr;
1681 u16 fc;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001682
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001683 wpa_printf(MSG_DEBUG, "nl80211: Frame TX status event");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001684 if (!is_ap_interface(drv->nlmode)) {
1685 u64 cookie_val;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001686
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001687 if (!cookie)
1688 return;
1689
1690 cookie_val = nla_get_u64(cookie);
1691 wpa_printf(MSG_DEBUG, "nl80211: Action TX status:"
1692 " cookie=0%llx%s (ack=%d)",
1693 (long long unsigned int) cookie_val,
1694 cookie_val == drv->send_action_cookie ?
1695 " (match)" : " (unknown)", ack != NULL);
1696 if (cookie_val != drv->send_action_cookie)
1697 return;
1698 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001699
1700 hdr = (const struct ieee80211_hdr *) frame;
1701 fc = le_to_host16(hdr->frame_control);
1702
1703 os_memset(&event, 0, sizeof(event));
1704 event.tx_status.type = WLAN_FC_GET_TYPE(fc);
1705 event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
1706 event.tx_status.dst = hdr->addr1;
1707 event.tx_status.data = frame;
1708 event.tx_status.data_len = len;
1709 event.tx_status.ack = ack != NULL;
1710 wpa_supplicant_event(drv->ctx, EVENT_TX_STATUS, &event);
1711}
1712
1713
1714static void mlme_event_deauth_disassoc(struct wpa_driver_nl80211_data *drv,
1715 enum wpa_event_type type,
1716 const u8 *frame, size_t len)
1717{
1718 const struct ieee80211_mgmt *mgmt;
1719 union wpa_event_data event;
1720 const u8 *bssid = NULL;
1721 u16 reason_code = 0;
1722
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001723 if (type == EVENT_DEAUTH)
1724 wpa_printf(MSG_DEBUG, "nl80211: Deauthenticate event");
1725 else
1726 wpa_printf(MSG_DEBUG, "nl80211: Disassociate event");
1727
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001728 mgmt = (const struct ieee80211_mgmt *) frame;
1729 if (len >= 24) {
1730 bssid = mgmt->bssid;
1731
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001732 if ((drv->capa.flags & WPA_DRIVER_FLAGS_SME) &&
1733 !drv->associated &&
1734 os_memcmp(bssid, drv->auth_bssid, ETH_ALEN) != 0 &&
1735 os_memcmp(bssid, drv->auth_attempt_bssid, ETH_ALEN) != 0 &&
1736 os_memcmp(bssid, drv->prev_bssid, ETH_ALEN) == 0) {
1737 /*
1738 * Avoid issues with some roaming cases where
1739 * disconnection event for the old AP may show up after
1740 * we have started connection with the new AP.
1741 */
1742 wpa_printf(MSG_DEBUG, "nl80211: Ignore deauth/disassoc event from old AP " MACSTR " when already authenticating with " MACSTR,
1743 MAC2STR(bssid),
1744 MAC2STR(drv->auth_attempt_bssid));
1745 return;
1746 }
1747
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001748 if (drv->associated != 0 &&
1749 os_memcmp(bssid, drv->bssid, ETH_ALEN) != 0 &&
1750 os_memcmp(bssid, drv->auth_bssid, ETH_ALEN) != 0) {
1751 /*
1752 * We have presumably received this deauth as a
1753 * response to a clear_state_mismatch() outgoing
1754 * deauth. Don't let it take us offline!
1755 */
1756 wpa_printf(MSG_DEBUG, "nl80211: Deauth received "
1757 "from Unknown BSSID " MACSTR " -- ignoring",
1758 MAC2STR(bssid));
1759 return;
1760 }
1761 }
1762
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001763 nl80211_mark_disconnected(drv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001764 os_memset(&event, 0, sizeof(event));
1765
1766 /* Note: Same offset for Reason Code in both frame subtypes */
1767 if (len >= 24 + sizeof(mgmt->u.deauth))
1768 reason_code = le_to_host16(mgmt->u.deauth.reason_code);
1769
1770 if (type == EVENT_DISASSOC) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001771 event.disassoc_info.locally_generated =
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001772 !os_memcmp(mgmt->sa, drv->first_bss->addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001773 event.disassoc_info.addr = bssid;
1774 event.disassoc_info.reason_code = reason_code;
1775 if (frame + len > mgmt->u.disassoc.variable) {
1776 event.disassoc_info.ie = mgmt->u.disassoc.variable;
1777 event.disassoc_info.ie_len = frame + len -
1778 mgmt->u.disassoc.variable;
1779 }
1780 } else {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001781 event.deauth_info.locally_generated =
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001782 !os_memcmp(mgmt->sa, drv->first_bss->addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001783 event.deauth_info.addr = bssid;
1784 event.deauth_info.reason_code = reason_code;
1785 if (frame + len > mgmt->u.deauth.variable) {
1786 event.deauth_info.ie = mgmt->u.deauth.variable;
1787 event.deauth_info.ie_len = frame + len -
1788 mgmt->u.deauth.variable;
1789 }
1790 }
1791
1792 wpa_supplicant_event(drv->ctx, type, &event);
1793}
1794
1795
1796static void mlme_event_unprot_disconnect(struct wpa_driver_nl80211_data *drv,
1797 enum wpa_event_type type,
1798 const u8 *frame, size_t len)
1799{
1800 const struct ieee80211_mgmt *mgmt;
1801 union wpa_event_data event;
1802 u16 reason_code = 0;
1803
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001804 if (type == EVENT_UNPROT_DEAUTH)
1805 wpa_printf(MSG_DEBUG, "nl80211: Unprot Deauthenticate event");
1806 else
1807 wpa_printf(MSG_DEBUG, "nl80211: Unprot Disassociate event");
1808
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001809 if (len < 24)
1810 return;
1811
1812 mgmt = (const struct ieee80211_mgmt *) frame;
1813
1814 os_memset(&event, 0, sizeof(event));
1815 /* Note: Same offset for Reason Code in both frame subtypes */
1816 if (len >= 24 + sizeof(mgmt->u.deauth))
1817 reason_code = le_to_host16(mgmt->u.deauth.reason_code);
1818
1819 if (type == EVENT_UNPROT_DISASSOC) {
1820 event.unprot_disassoc.sa = mgmt->sa;
1821 event.unprot_disassoc.da = mgmt->da;
1822 event.unprot_disassoc.reason_code = reason_code;
1823 } else {
1824 event.unprot_deauth.sa = mgmt->sa;
1825 event.unprot_deauth.da = mgmt->da;
1826 event.unprot_deauth.reason_code = reason_code;
1827 }
1828
1829 wpa_supplicant_event(drv->ctx, type, &event);
1830}
1831
1832
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001833static void mlme_event(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001834 enum nl80211_commands cmd, struct nlattr *frame,
1835 struct nlattr *addr, struct nlattr *timed_out,
1836 struct nlattr *freq, struct nlattr *ack,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001837 struct nlattr *cookie, struct nlattr *sig)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001838{
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001839 struct wpa_driver_nl80211_data *drv = bss->drv;
1840 const u8 *data;
1841 size_t len;
1842
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001843 if (timed_out && addr) {
1844 mlme_timeout_event(drv, cmd, addr);
1845 return;
1846 }
1847
1848 if (frame == NULL) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001849 wpa_printf(MSG_DEBUG,
1850 "nl80211: MLME event %d (%s) without frame data",
1851 cmd, nl80211_command_to_string(cmd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001852 return;
1853 }
1854
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001855 data = nla_data(frame);
1856 len = nla_len(frame);
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001857 if (len < 4 + 2 * ETH_ALEN) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001858 wpa_printf(MSG_MSGDUMP, "nl80211: MLME event %d (%s) on %s("
1859 MACSTR ") - too short",
1860 cmd, nl80211_command_to_string(cmd), bss->ifname,
1861 MAC2STR(bss->addr));
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001862 return;
1863 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001864 wpa_printf(MSG_MSGDUMP, "nl80211: MLME event %d (%s) on %s(" MACSTR
1865 ") A1=" MACSTR " A2=" MACSTR, cmd,
1866 nl80211_command_to_string(cmd), bss->ifname,
1867 MAC2STR(bss->addr), MAC2STR(data + 4),
1868 MAC2STR(data + 4 + ETH_ALEN));
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001869 if (cmd != NL80211_CMD_FRAME_TX_STATUS && !(data[4] & 0x01) &&
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001870 os_memcmp(bss->addr, data + 4, ETH_ALEN) != 0 &&
1871 os_memcmp(bss->addr, data + 4 + ETH_ALEN, ETH_ALEN) != 0) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001872 wpa_printf(MSG_MSGDUMP, "nl80211: %s: Ignore MLME frame event "
1873 "for foreign address", bss->ifname);
1874 return;
1875 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001876 wpa_hexdump(MSG_MSGDUMP, "nl80211: MLME event frame",
1877 nla_data(frame), nla_len(frame));
1878
1879 switch (cmd) {
1880 case NL80211_CMD_AUTHENTICATE:
1881 mlme_event_auth(drv, nla_data(frame), nla_len(frame));
1882 break;
1883 case NL80211_CMD_ASSOCIATE:
1884 mlme_event_assoc(drv, nla_data(frame), nla_len(frame));
1885 break;
1886 case NL80211_CMD_DEAUTHENTICATE:
1887 mlme_event_deauth_disassoc(drv, EVENT_DEAUTH,
1888 nla_data(frame), nla_len(frame));
1889 break;
1890 case NL80211_CMD_DISASSOCIATE:
1891 mlme_event_deauth_disassoc(drv, EVENT_DISASSOC,
1892 nla_data(frame), nla_len(frame));
1893 break;
1894 case NL80211_CMD_FRAME:
Dmitry Shmidt04949592012-07-19 12:16:46 -07001895 mlme_event_mgmt(drv, freq, sig, nla_data(frame),
1896 nla_len(frame));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001897 break;
1898 case NL80211_CMD_FRAME_TX_STATUS:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001899 mlme_event_mgmt_tx_status(drv, cookie, nla_data(frame),
1900 nla_len(frame), ack);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001901 break;
1902 case NL80211_CMD_UNPROT_DEAUTHENTICATE:
1903 mlme_event_unprot_disconnect(drv, EVENT_UNPROT_DEAUTH,
1904 nla_data(frame), nla_len(frame));
1905 break;
1906 case NL80211_CMD_UNPROT_DISASSOCIATE:
1907 mlme_event_unprot_disconnect(drv, EVENT_UNPROT_DISASSOC,
1908 nla_data(frame), nla_len(frame));
1909 break;
1910 default:
1911 break;
1912 }
1913}
1914
1915
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001916static void mlme_event_michael_mic_failure(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001917 struct nlattr *tb[])
1918{
1919 union wpa_event_data data;
1920
1921 wpa_printf(MSG_DEBUG, "nl80211: MLME event Michael MIC failure");
1922 os_memset(&data, 0, sizeof(data));
1923 if (tb[NL80211_ATTR_MAC]) {
1924 wpa_hexdump(MSG_DEBUG, "nl80211: Source MAC address",
1925 nla_data(tb[NL80211_ATTR_MAC]),
1926 nla_len(tb[NL80211_ATTR_MAC]));
1927 data.michael_mic_failure.src = nla_data(tb[NL80211_ATTR_MAC]);
1928 }
1929 if (tb[NL80211_ATTR_KEY_SEQ]) {
1930 wpa_hexdump(MSG_DEBUG, "nl80211: TSC",
1931 nla_data(tb[NL80211_ATTR_KEY_SEQ]),
1932 nla_len(tb[NL80211_ATTR_KEY_SEQ]));
1933 }
1934 if (tb[NL80211_ATTR_KEY_TYPE]) {
1935 enum nl80211_key_type key_type =
1936 nla_get_u32(tb[NL80211_ATTR_KEY_TYPE]);
1937 wpa_printf(MSG_DEBUG, "nl80211: Key Type %d", key_type);
1938 if (key_type == NL80211_KEYTYPE_PAIRWISE)
1939 data.michael_mic_failure.unicast = 1;
1940 } else
1941 data.michael_mic_failure.unicast = 1;
1942
1943 if (tb[NL80211_ATTR_KEY_IDX]) {
1944 u8 key_id = nla_get_u8(tb[NL80211_ATTR_KEY_IDX]);
1945 wpa_printf(MSG_DEBUG, "nl80211: Key Id %d", key_id);
1946 }
1947
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001948 wpa_supplicant_event(bss->ctx, EVENT_MICHAEL_MIC_FAILURE, &data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001949}
1950
1951
1952static void mlme_event_join_ibss(struct wpa_driver_nl80211_data *drv,
1953 struct nlattr *tb[])
1954{
1955 if (tb[NL80211_ATTR_MAC] == NULL) {
1956 wpa_printf(MSG_DEBUG, "nl80211: No address in IBSS joined "
1957 "event");
1958 return;
1959 }
1960 os_memcpy(drv->bssid, nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07001961
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001962 drv->associated = 1;
1963 wpa_printf(MSG_DEBUG, "nl80211: IBSS " MACSTR " joined",
1964 MAC2STR(drv->bssid));
1965
1966 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, NULL);
1967}
1968
1969
1970static void mlme_event_remain_on_channel(struct wpa_driver_nl80211_data *drv,
1971 int cancel_event, struct nlattr *tb[])
1972{
1973 unsigned int freq, chan_type, duration;
1974 union wpa_event_data data;
1975 u64 cookie;
1976
1977 if (tb[NL80211_ATTR_WIPHY_FREQ])
1978 freq = nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]);
1979 else
1980 freq = 0;
1981
1982 if (tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE])
1983 chan_type = nla_get_u32(tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1984 else
1985 chan_type = 0;
1986
1987 if (tb[NL80211_ATTR_DURATION])
1988 duration = nla_get_u32(tb[NL80211_ATTR_DURATION]);
1989 else
1990 duration = 0;
1991
1992 if (tb[NL80211_ATTR_COOKIE])
1993 cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
1994 else
1995 cookie = 0;
1996
1997 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel event (cancel=%d "
1998 "freq=%u channel_type=%u duration=%u cookie=0x%llx (%s))",
1999 cancel_event, freq, chan_type, duration,
2000 (long long unsigned int) cookie,
2001 cookie == drv->remain_on_chan_cookie ? "match" : "unknown");
2002
2003 if (cookie != drv->remain_on_chan_cookie)
2004 return; /* not for us */
2005
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002006 if (cancel_event)
2007 drv->pending_remain_on_chan = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002008
2009 os_memset(&data, 0, sizeof(data));
2010 data.remain_on_channel.freq = freq;
2011 data.remain_on_channel.duration = duration;
2012 wpa_supplicant_event(drv->ctx, cancel_event ?
2013 EVENT_CANCEL_REMAIN_ON_CHANNEL :
2014 EVENT_REMAIN_ON_CHANNEL, &data);
2015}
2016
2017
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002018static void mlme_event_ft_event(struct wpa_driver_nl80211_data *drv,
2019 struct nlattr *tb[])
2020{
2021 union wpa_event_data data;
2022
2023 os_memset(&data, 0, sizeof(data));
2024
2025 if (tb[NL80211_ATTR_IE]) {
2026 data.ft_ies.ies = nla_data(tb[NL80211_ATTR_IE]);
2027 data.ft_ies.ies_len = nla_len(tb[NL80211_ATTR_IE]);
2028 }
2029
2030 if (tb[NL80211_ATTR_IE_RIC]) {
2031 data.ft_ies.ric_ies = nla_data(tb[NL80211_ATTR_IE_RIC]);
2032 data.ft_ies.ric_ies_len = nla_len(tb[NL80211_ATTR_IE_RIC]);
2033 }
2034
2035 if (tb[NL80211_ATTR_MAC])
2036 os_memcpy(data.ft_ies.target_ap,
2037 nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
2038
2039 wpa_printf(MSG_DEBUG, "nl80211: FT event target_ap " MACSTR,
2040 MAC2STR(data.ft_ies.target_ap));
2041
2042 wpa_supplicant_event(drv->ctx, EVENT_FT_RESPONSE, &data);
2043}
2044
2045
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002046static void send_scan_event(struct wpa_driver_nl80211_data *drv, int aborted,
2047 struct nlattr *tb[])
2048{
2049 union wpa_event_data event;
2050 struct nlattr *nl;
2051 int rem;
2052 struct scan_info *info;
2053#define MAX_REPORT_FREQS 50
2054 int freqs[MAX_REPORT_FREQS];
2055 int num_freqs = 0;
2056
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002057 if (drv->scan_for_auth) {
2058 drv->scan_for_auth = 0;
2059 wpa_printf(MSG_DEBUG, "nl80211: Scan results for missing "
2060 "cfg80211 BSS entry");
2061 wpa_driver_nl80211_authenticate_retry(drv);
2062 return;
2063 }
2064
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002065 os_memset(&event, 0, sizeof(event));
2066 info = &event.scan_info;
2067 info->aborted = aborted;
2068
2069 if (tb[NL80211_ATTR_SCAN_SSIDS]) {
2070 nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_SSIDS], rem) {
2071 struct wpa_driver_scan_ssid *s =
2072 &info->ssids[info->num_ssids];
2073 s->ssid = nla_data(nl);
2074 s->ssid_len = nla_len(nl);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002075 wpa_printf(MSG_DEBUG, "nl80211: Scan probed for SSID '%s'",
2076 wpa_ssid_txt(s->ssid, s->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002077 info->num_ssids++;
2078 if (info->num_ssids == WPAS_MAX_SCAN_SSIDS)
2079 break;
2080 }
2081 }
2082 if (tb[NL80211_ATTR_SCAN_FREQUENCIES]) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002083 char msg[200], *pos, *end;
2084 int res;
2085
2086 pos = msg;
2087 end = pos + sizeof(msg);
2088 *pos = '\0';
2089
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002090 nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_FREQUENCIES], rem)
2091 {
2092 freqs[num_freqs] = nla_get_u32(nl);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002093 res = os_snprintf(pos, end - pos, " %d",
2094 freqs[num_freqs]);
2095 if (res > 0 && end - pos > res)
2096 pos += res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002097 num_freqs++;
2098 if (num_freqs == MAX_REPORT_FREQS - 1)
2099 break;
2100 }
2101 info->freqs = freqs;
2102 info->num_freqs = num_freqs;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002103 wpa_printf(MSG_DEBUG, "nl80211: Scan included frequencies:%s",
2104 msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002105 }
2106 wpa_supplicant_event(drv->ctx, EVENT_SCAN_RESULTS, &event);
2107}
2108
2109
2110static int get_link_signal(struct nl_msg *msg, void *arg)
2111{
2112 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2113 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2114 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
2115 static struct nla_policy policy[NL80211_STA_INFO_MAX + 1] = {
2116 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002117 [NL80211_STA_INFO_SIGNAL_AVG] = { .type = NLA_U8 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002118 };
2119 struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
2120 static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
2121 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
2122 [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
2123 [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
2124 [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
2125 };
2126 struct wpa_signal_info *sig_change = arg;
2127
2128 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2129 genlmsg_attrlen(gnlh, 0), NULL);
2130 if (!tb[NL80211_ATTR_STA_INFO] ||
2131 nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
2132 tb[NL80211_ATTR_STA_INFO], policy))
2133 return NL_SKIP;
2134 if (!sinfo[NL80211_STA_INFO_SIGNAL])
2135 return NL_SKIP;
2136
2137 sig_change->current_signal =
2138 (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
2139
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002140 if (sinfo[NL80211_STA_INFO_SIGNAL_AVG])
2141 sig_change->avg_signal =
2142 (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL_AVG]);
2143 else
2144 sig_change->avg_signal = 0;
2145
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002146 if (sinfo[NL80211_STA_INFO_TX_BITRATE]) {
2147 if (nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
2148 sinfo[NL80211_STA_INFO_TX_BITRATE],
2149 rate_policy)) {
2150 sig_change->current_txrate = 0;
2151 } else {
2152 if (rinfo[NL80211_RATE_INFO_BITRATE]) {
2153 sig_change->current_txrate =
2154 nla_get_u16(rinfo[
2155 NL80211_RATE_INFO_BITRATE]) * 100;
2156 }
2157 }
2158 }
2159
2160 return NL_SKIP;
2161}
2162
2163
2164static int nl80211_get_link_signal(struct wpa_driver_nl80211_data *drv,
2165 struct wpa_signal_info *sig)
2166{
2167 struct nl_msg *msg;
2168
2169 sig->current_signal = -9999;
2170 sig->current_txrate = 0;
2171
2172 msg = nlmsg_alloc();
2173 if (!msg)
2174 return -ENOMEM;
2175
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002176 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002177
2178 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
2179 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid);
2180
2181 return send_and_recv_msgs(drv, msg, get_link_signal, sig);
2182 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002183 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002184 return -ENOBUFS;
2185}
2186
2187
2188static int get_link_noise(struct nl_msg *msg, void *arg)
2189{
2190 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2191 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2192 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
2193 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
2194 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
2195 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
2196 };
2197 struct wpa_signal_info *sig_change = arg;
2198
2199 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2200 genlmsg_attrlen(gnlh, 0), NULL);
2201
2202 if (!tb[NL80211_ATTR_SURVEY_INFO]) {
2203 wpa_printf(MSG_DEBUG, "nl80211: survey data missing!");
2204 return NL_SKIP;
2205 }
2206
2207 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
2208 tb[NL80211_ATTR_SURVEY_INFO],
2209 survey_policy)) {
2210 wpa_printf(MSG_DEBUG, "nl80211: failed to parse nested "
2211 "attributes!");
2212 return NL_SKIP;
2213 }
2214
2215 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
2216 return NL_SKIP;
2217
2218 if (nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
2219 sig_change->frequency)
2220 return NL_SKIP;
2221
2222 if (!sinfo[NL80211_SURVEY_INFO_NOISE])
2223 return NL_SKIP;
2224
2225 sig_change->current_noise =
2226 (s8) nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
2227
2228 return NL_SKIP;
2229}
2230
2231
2232static int nl80211_get_link_noise(struct wpa_driver_nl80211_data *drv,
2233 struct wpa_signal_info *sig_change)
2234{
2235 struct nl_msg *msg;
2236
2237 sig_change->current_noise = 9999;
2238 sig_change->frequency = drv->assoc_freq;
2239
2240 msg = nlmsg_alloc();
2241 if (!msg)
2242 return -ENOMEM;
2243
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002244 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002245
2246 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
2247
2248 return send_and_recv_msgs(drv, msg, get_link_noise, sig_change);
2249 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002250 nlmsg_free(msg);
2251 return -ENOBUFS;
2252}
2253
2254
2255static int get_noise_for_scan_results(struct nl_msg *msg, void *arg)
2256{
2257 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2258 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2259 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
2260 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
2261 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
2262 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
2263 };
2264 struct wpa_scan_results *scan_results = arg;
2265 struct wpa_scan_res *scan_res;
2266 size_t i;
2267
2268 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2269 genlmsg_attrlen(gnlh, 0), NULL);
2270
2271 if (!tb[NL80211_ATTR_SURVEY_INFO]) {
2272 wpa_printf(MSG_DEBUG, "nl80211: Survey data missing");
2273 return NL_SKIP;
2274 }
2275
2276 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
2277 tb[NL80211_ATTR_SURVEY_INFO],
2278 survey_policy)) {
2279 wpa_printf(MSG_DEBUG, "nl80211: Failed to parse nested "
2280 "attributes");
2281 return NL_SKIP;
2282 }
2283
2284 if (!sinfo[NL80211_SURVEY_INFO_NOISE])
2285 return NL_SKIP;
2286
2287 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
2288 return NL_SKIP;
2289
2290 for (i = 0; i < scan_results->num; ++i) {
2291 scan_res = scan_results->res[i];
2292 if (!scan_res)
2293 continue;
2294 if ((int) nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
2295 scan_res->freq)
2296 continue;
2297 if (!(scan_res->flags & WPA_SCAN_NOISE_INVALID))
2298 continue;
2299 scan_res->noise = (s8)
2300 nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
2301 scan_res->flags &= ~WPA_SCAN_NOISE_INVALID;
2302 }
2303
2304 return NL_SKIP;
2305}
2306
2307
2308static int nl80211_get_noise_for_scan_results(
2309 struct wpa_driver_nl80211_data *drv,
2310 struct wpa_scan_results *scan_res)
2311{
2312 struct nl_msg *msg;
2313
2314 msg = nlmsg_alloc();
2315 if (!msg)
2316 return -ENOMEM;
2317
2318 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
2319
2320 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
2321
2322 return send_and_recv_msgs(drv, msg, get_noise_for_scan_results,
2323 scan_res);
2324 nla_put_failure:
2325 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002326 return -ENOBUFS;
2327}
2328
2329
2330static void nl80211_cqm_event(struct wpa_driver_nl80211_data *drv,
2331 struct nlattr *tb[])
2332{
2333 static struct nla_policy cqm_policy[NL80211_ATTR_CQM_MAX + 1] = {
2334 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
2335 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U8 },
2336 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
2337 [NL80211_ATTR_CQM_PKT_LOSS_EVENT] = { .type = NLA_U32 },
2338 };
2339 struct nlattr *cqm[NL80211_ATTR_CQM_MAX + 1];
2340 enum nl80211_cqm_rssi_threshold_event event;
2341 union wpa_event_data ed;
2342 struct wpa_signal_info sig;
2343 int res;
2344
2345 if (tb[NL80211_ATTR_CQM] == NULL ||
2346 nla_parse_nested(cqm, NL80211_ATTR_CQM_MAX, tb[NL80211_ATTR_CQM],
2347 cqm_policy)) {
2348 wpa_printf(MSG_DEBUG, "nl80211: Ignore invalid CQM event");
2349 return;
2350 }
2351
2352 os_memset(&ed, 0, sizeof(ed));
2353
2354 if (cqm[NL80211_ATTR_CQM_PKT_LOSS_EVENT]) {
2355 if (!tb[NL80211_ATTR_MAC])
2356 return;
2357 os_memcpy(ed.low_ack.addr, nla_data(tb[NL80211_ATTR_MAC]),
2358 ETH_ALEN);
2359 wpa_supplicant_event(drv->ctx, EVENT_STATION_LOW_ACK, &ed);
2360 return;
2361 }
2362
2363 if (cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] == NULL)
2364 return;
2365 event = nla_get_u32(cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT]);
2366
2367 if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH) {
2368 wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor "
2369 "event: RSSI high");
2370 ed.signal_change.above_threshold = 1;
2371 } else if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW) {
2372 wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor "
2373 "event: RSSI low");
2374 ed.signal_change.above_threshold = 0;
2375 } else
2376 return;
2377
2378 res = nl80211_get_link_signal(drv, &sig);
2379 if (res == 0) {
2380 ed.signal_change.current_signal = sig.current_signal;
2381 ed.signal_change.current_txrate = sig.current_txrate;
2382 wpa_printf(MSG_DEBUG, "nl80211: Signal: %d dBm txrate: %d",
2383 sig.current_signal, sig.current_txrate);
2384 }
2385
2386 res = nl80211_get_link_noise(drv, &sig);
2387 if (res == 0) {
2388 ed.signal_change.current_noise = sig.current_noise;
2389 wpa_printf(MSG_DEBUG, "nl80211: Noise: %d dBm",
2390 sig.current_noise);
2391 }
2392
2393 wpa_supplicant_event(drv->ctx, EVENT_SIGNAL_CHANGE, &ed);
2394}
2395
2396
2397static void nl80211_new_station_event(struct wpa_driver_nl80211_data *drv,
2398 struct nlattr **tb)
2399{
2400 u8 *addr;
2401 union wpa_event_data data;
2402
2403 if (tb[NL80211_ATTR_MAC] == NULL)
2404 return;
2405 addr = nla_data(tb[NL80211_ATTR_MAC]);
2406 wpa_printf(MSG_DEBUG, "nl80211: New station " MACSTR, MAC2STR(addr));
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07002407
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002408 if (is_ap_interface(drv->nlmode) && drv->device_ap_sme) {
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07002409 u8 *ies = NULL;
2410 size_t ies_len = 0;
2411 if (tb[NL80211_ATTR_IE]) {
2412 ies = nla_data(tb[NL80211_ATTR_IE]);
2413 ies_len = nla_len(tb[NL80211_ATTR_IE]);
2414 }
2415 wpa_hexdump(MSG_DEBUG, "nl80211: Assoc Req IEs", ies, ies_len);
2416 drv_event_assoc(drv->ctx, addr, ies, ies_len, 0);
2417 return;
2418 }
2419
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002420 if (drv->nlmode != NL80211_IFTYPE_ADHOC)
2421 return;
2422
2423 os_memset(&data, 0, sizeof(data));
2424 os_memcpy(data.ibss_rsn_start.peer, addr, ETH_ALEN);
2425 wpa_supplicant_event(drv->ctx, EVENT_IBSS_RSN_START, &data);
2426}
2427
2428
2429static void nl80211_del_station_event(struct wpa_driver_nl80211_data *drv,
2430 struct nlattr **tb)
2431{
2432 u8 *addr;
2433 union wpa_event_data data;
2434
2435 if (tb[NL80211_ATTR_MAC] == NULL)
2436 return;
2437 addr = nla_data(tb[NL80211_ATTR_MAC]);
2438 wpa_printf(MSG_DEBUG, "nl80211: Delete station " MACSTR,
2439 MAC2STR(addr));
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07002440
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002441 if (is_ap_interface(drv->nlmode) && drv->device_ap_sme) {
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07002442 drv_event_disassoc(drv->ctx, addr);
2443 return;
2444 }
2445
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002446 if (drv->nlmode != NL80211_IFTYPE_ADHOC)
2447 return;
2448
2449 os_memset(&data, 0, sizeof(data));
2450 os_memcpy(data.ibss_peer_lost.peer, addr, ETH_ALEN);
2451 wpa_supplicant_event(drv->ctx, EVENT_IBSS_PEER_LOST, &data);
2452}
2453
2454
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002455static void nl80211_rekey_offload_event(struct wpa_driver_nl80211_data *drv,
2456 struct nlattr **tb)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002457{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002458 struct nlattr *rekey_info[NUM_NL80211_REKEY_DATA];
2459 static struct nla_policy rekey_policy[NUM_NL80211_REKEY_DATA] = {
2460 [NL80211_REKEY_DATA_KEK] = {
2461 .minlen = NL80211_KEK_LEN,
2462 .maxlen = NL80211_KEK_LEN,
2463 },
2464 [NL80211_REKEY_DATA_KCK] = {
2465 .minlen = NL80211_KCK_LEN,
2466 .maxlen = NL80211_KCK_LEN,
2467 },
2468 [NL80211_REKEY_DATA_REPLAY_CTR] = {
2469 .minlen = NL80211_REPLAY_CTR_LEN,
2470 .maxlen = NL80211_REPLAY_CTR_LEN,
2471 },
2472 };
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002473 union wpa_event_data data;
2474
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002475 if (!tb[NL80211_ATTR_MAC])
2476 return;
2477 if (!tb[NL80211_ATTR_REKEY_DATA])
2478 return;
2479 if (nla_parse_nested(rekey_info, MAX_NL80211_REKEY_DATA,
2480 tb[NL80211_ATTR_REKEY_DATA], rekey_policy))
2481 return;
2482 if (!rekey_info[NL80211_REKEY_DATA_REPLAY_CTR])
2483 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002484
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002485 os_memset(&data, 0, sizeof(data));
2486 data.driver_gtk_rekey.bssid = nla_data(tb[NL80211_ATTR_MAC]);
2487 wpa_printf(MSG_DEBUG, "nl80211: Rekey offload event for BSSID " MACSTR,
2488 MAC2STR(data.driver_gtk_rekey.bssid));
2489 data.driver_gtk_rekey.replay_ctr =
2490 nla_data(rekey_info[NL80211_REKEY_DATA_REPLAY_CTR]);
2491 wpa_hexdump(MSG_DEBUG, "nl80211: Rekey offload - Replay Counter",
2492 data.driver_gtk_rekey.replay_ctr, NL80211_REPLAY_CTR_LEN);
2493 wpa_supplicant_event(drv->ctx, EVENT_DRIVER_GTK_REKEY, &data);
2494}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002495
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002496
2497static void nl80211_pmksa_candidate_event(struct wpa_driver_nl80211_data *drv,
2498 struct nlattr **tb)
2499{
2500 struct nlattr *cand[NUM_NL80211_PMKSA_CANDIDATE];
2501 static struct nla_policy cand_policy[NUM_NL80211_PMKSA_CANDIDATE] = {
2502 [NL80211_PMKSA_CANDIDATE_INDEX] = { .type = NLA_U32 },
2503 [NL80211_PMKSA_CANDIDATE_BSSID] = {
2504 .minlen = ETH_ALEN,
2505 .maxlen = ETH_ALEN,
2506 },
2507 [NL80211_PMKSA_CANDIDATE_PREAUTH] = { .type = NLA_FLAG },
2508 };
2509 union wpa_event_data data;
2510
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002511 wpa_printf(MSG_DEBUG, "nl80211: PMKSA candidate event");
2512
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002513 if (!tb[NL80211_ATTR_PMKSA_CANDIDATE])
2514 return;
2515 if (nla_parse_nested(cand, MAX_NL80211_PMKSA_CANDIDATE,
2516 tb[NL80211_ATTR_PMKSA_CANDIDATE], cand_policy))
2517 return;
2518 if (!cand[NL80211_PMKSA_CANDIDATE_INDEX] ||
2519 !cand[NL80211_PMKSA_CANDIDATE_BSSID])
2520 return;
2521
2522 os_memset(&data, 0, sizeof(data));
2523 os_memcpy(data.pmkid_candidate.bssid,
2524 nla_data(cand[NL80211_PMKSA_CANDIDATE_BSSID]), ETH_ALEN);
2525 data.pmkid_candidate.index =
2526 nla_get_u32(cand[NL80211_PMKSA_CANDIDATE_INDEX]);
2527 data.pmkid_candidate.preauth =
2528 cand[NL80211_PMKSA_CANDIDATE_PREAUTH] != NULL;
2529 wpa_supplicant_event(drv->ctx, EVENT_PMKID_CANDIDATE, &data);
2530}
2531
2532
2533static void nl80211_client_probe_event(struct wpa_driver_nl80211_data *drv,
2534 struct nlattr **tb)
2535{
2536 union wpa_event_data data;
2537
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002538 wpa_printf(MSG_DEBUG, "nl80211: Probe client event");
2539
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002540 if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_ACK])
2541 return;
2542
2543 os_memset(&data, 0, sizeof(data));
2544 os_memcpy(data.client_poll.addr,
2545 nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
2546
2547 wpa_supplicant_event(drv->ctx, EVENT_DRIVER_CLIENT_POLL_OK, &data);
2548}
2549
2550
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002551static void nl80211_tdls_oper_event(struct wpa_driver_nl80211_data *drv,
2552 struct nlattr **tb)
2553{
2554 union wpa_event_data data;
2555
2556 wpa_printf(MSG_DEBUG, "nl80211: TDLS operation event");
2557
2558 if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_TDLS_OPERATION])
2559 return;
2560
2561 os_memset(&data, 0, sizeof(data));
2562 os_memcpy(data.tdls.peer, nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
2563 switch (nla_get_u8(tb[NL80211_ATTR_TDLS_OPERATION])) {
2564 case NL80211_TDLS_SETUP:
2565 wpa_printf(MSG_DEBUG, "nl80211: TDLS setup request for peer "
2566 MACSTR, MAC2STR(data.tdls.peer));
2567 data.tdls.oper = TDLS_REQUEST_SETUP;
2568 break;
2569 case NL80211_TDLS_TEARDOWN:
2570 wpa_printf(MSG_DEBUG, "nl80211: TDLS teardown request for peer "
2571 MACSTR, MAC2STR(data.tdls.peer));
2572 data.tdls.oper = TDLS_REQUEST_TEARDOWN;
2573 break;
2574 default:
2575 wpa_printf(MSG_DEBUG, "nl80211: Unsupported TDLS operatione "
2576 "event");
2577 return;
2578 }
2579 if (tb[NL80211_ATTR_REASON_CODE]) {
2580 data.tdls.reason_code =
2581 nla_get_u16(tb[NL80211_ATTR_REASON_CODE]);
2582 }
2583
2584 wpa_supplicant_event(drv->ctx, EVENT_TDLS, &data);
2585}
2586
2587
Dmitry Shmidt5393a0f2013-08-08 11:23:34 -07002588static void nl80211_stop_ap(struct wpa_driver_nl80211_data *drv,
2589 struct nlattr **tb)
2590{
2591 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_UNAVAILABLE, NULL);
2592}
2593
2594
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002595static void nl80211_connect_failed_event(struct wpa_driver_nl80211_data *drv,
2596 struct nlattr **tb)
2597{
2598 union wpa_event_data data;
2599 u32 reason;
2600
2601 wpa_printf(MSG_DEBUG, "nl80211: Connect failed event");
2602
2603 if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_CONN_FAILED_REASON])
2604 return;
2605
2606 os_memset(&data, 0, sizeof(data));
2607 os_memcpy(data.connect_failed_reason.addr,
2608 nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
2609
2610 reason = nla_get_u32(tb[NL80211_ATTR_CONN_FAILED_REASON]);
2611 switch (reason) {
2612 case NL80211_CONN_FAIL_MAX_CLIENTS:
2613 wpa_printf(MSG_DEBUG, "nl80211: Max client reached");
2614 data.connect_failed_reason.code = MAX_CLIENT_REACHED;
2615 break;
2616 case NL80211_CONN_FAIL_BLOCKED_CLIENT:
2617 wpa_printf(MSG_DEBUG, "nl80211: Blocked client " MACSTR
2618 " tried to connect",
2619 MAC2STR(data.connect_failed_reason.addr));
2620 data.connect_failed_reason.code = BLOCKED_CLIENT;
2621 break;
2622 default:
2623 wpa_printf(MSG_DEBUG, "nl8021l: Unknown connect failed reason "
2624 "%u", reason);
2625 return;
2626 }
2627
2628 wpa_supplicant_event(drv->ctx, EVENT_CONNECT_FAILED_REASON, &data);
2629}
2630
2631
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002632static void nl80211_radar_event(struct wpa_driver_nl80211_data *drv,
2633 struct nlattr **tb)
2634{
2635 union wpa_event_data data;
2636 enum nl80211_radar_event event_type;
2637
2638 if (!tb[NL80211_ATTR_WIPHY_FREQ] || !tb[NL80211_ATTR_RADAR_EVENT])
2639 return;
2640
2641 os_memset(&data, 0, sizeof(data));
Dmitry Shmidt051af732013-10-22 13:52:46 -07002642 data.dfs_event.freq = nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]);
2643 event_type = nla_get_u32(tb[NL80211_ATTR_RADAR_EVENT]);
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002644
Dmitry Shmidt051af732013-10-22 13:52:46 -07002645 /* Check HT params */
2646 if (tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
2647 data.dfs_event.ht_enabled = 1;
2648 data.dfs_event.chan_offset = 0;
2649
2650 switch (nla_get_u32(tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE])) {
2651 case NL80211_CHAN_NO_HT:
2652 data.dfs_event.ht_enabled = 0;
2653 break;
2654 case NL80211_CHAN_HT20:
2655 break;
2656 case NL80211_CHAN_HT40PLUS:
2657 data.dfs_event.chan_offset = 1;
2658 break;
2659 case NL80211_CHAN_HT40MINUS:
2660 data.dfs_event.chan_offset = -1;
2661 break;
2662 }
2663 }
2664
2665 /* Get VHT params */
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002666 if (tb[NL80211_ATTR_CHANNEL_WIDTH])
2667 data.dfs_event.chan_width =
2668 convert2width(nla_get_u32(
2669 tb[NL80211_ATTR_CHANNEL_WIDTH]));
2670 if (tb[NL80211_ATTR_CENTER_FREQ1])
2671 data.dfs_event.cf1 = nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002672 if (tb[NL80211_ATTR_CENTER_FREQ2])
2673 data.dfs_event.cf2 = nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]);
2674
2675 wpa_printf(MSG_DEBUG, "nl80211: DFS event on freq %d MHz, ht: %d, offset: %d, width: %d, cf1: %dMHz, cf2: %dMHz",
2676 data.dfs_event.freq, data.dfs_event.ht_enabled,
2677 data.dfs_event.chan_offset, data.dfs_event.chan_width,
2678 data.dfs_event.cf1, data.dfs_event.cf2);
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002679
2680 switch (event_type) {
2681 case NL80211_RADAR_DETECTED:
2682 wpa_supplicant_event(drv->ctx, EVENT_DFS_RADAR_DETECTED, &data);
2683 break;
2684 case NL80211_RADAR_CAC_FINISHED:
2685 wpa_supplicant_event(drv->ctx, EVENT_DFS_CAC_FINISHED, &data);
2686 break;
2687 case NL80211_RADAR_CAC_ABORTED:
2688 wpa_supplicant_event(drv->ctx, EVENT_DFS_CAC_ABORTED, &data);
2689 break;
2690 case NL80211_RADAR_NOP_FINISHED:
2691 wpa_supplicant_event(drv->ctx, EVENT_DFS_NOP_FINISHED, &data);
2692 break;
2693 default:
2694 wpa_printf(MSG_DEBUG, "nl80211: Unknown radar event %d "
2695 "received", event_type);
2696 break;
2697 }
2698}
2699
2700
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002701static void nl80211_spurious_frame(struct i802_bss *bss, struct nlattr **tb,
2702 int wds)
2703{
2704 struct wpa_driver_nl80211_data *drv = bss->drv;
2705 union wpa_event_data event;
2706
2707 if (!tb[NL80211_ATTR_MAC])
2708 return;
2709
2710 os_memset(&event, 0, sizeof(event));
2711 event.rx_from_unknown.bssid = bss->addr;
2712 event.rx_from_unknown.addr = nla_data(tb[NL80211_ATTR_MAC]);
2713 event.rx_from_unknown.wds = wds;
2714
2715 wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event);
2716}
2717
2718
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002719static void nl80211_vendor_event(struct wpa_driver_nl80211_data *drv,
2720 struct nlattr **tb)
2721{
2722 u32 vendor_id, subcmd, wiphy = 0;
2723 int wiphy_idx;
2724 u8 *data = NULL;
2725 size_t len = 0;
2726
2727 if (!tb[NL80211_ATTR_VENDOR_ID] ||
2728 !tb[NL80211_ATTR_VENDOR_SUBCMD])
2729 return;
2730
2731 vendor_id = nla_get_u32(tb[NL80211_ATTR_VENDOR_ID]);
2732 subcmd = nla_get_u32(tb[NL80211_ATTR_VENDOR_SUBCMD]);
2733
2734 if (tb[NL80211_ATTR_WIPHY])
2735 wiphy = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
2736
2737 wpa_printf(MSG_DEBUG, "nl80211: Vendor event: wiphy=%u vendor_id=0x%x subcmd=%u",
2738 wiphy, vendor_id, subcmd);
2739
2740 if (tb[NL80211_ATTR_VENDOR_DATA]) {
2741 data = nla_data(tb[NL80211_ATTR_VENDOR_DATA]);
2742 len = nla_len(tb[NL80211_ATTR_VENDOR_DATA]);
2743 wpa_hexdump(MSG_MSGDUMP, "nl80211: Vendor data", data, len);
2744 }
2745
2746 wiphy_idx = nl80211_get_wiphy_index(drv->first_bss);
2747 if (wiphy_idx >= 0 && wiphy_idx != (int) wiphy) {
2748 wpa_printf(MSG_DEBUG, "nl80211: Ignore vendor event for foreign wiphy %u (own: %d)",
2749 wiphy, wiphy_idx);
2750 return;
2751 }
2752
2753 switch (vendor_id) {
2754 default:
2755 wpa_printf(MSG_DEBUG, "nl80211: Ignore unsupported vendor event");
2756 break;
2757 }
2758}
2759
2760
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002761static void do_process_drv_event(struct i802_bss *bss, int cmd,
2762 struct nlattr **tb)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002763{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002764 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002765 union wpa_event_data data;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002766
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002767 wpa_printf(MSG_DEBUG, "nl80211: Drv Event %d (%s) received for %s",
2768 cmd, nl80211_command_to_string(cmd), bss->ifname);
2769
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002770 if (drv->ap_scan_as_station != NL80211_IFTYPE_UNSPECIFIED &&
2771 (cmd == NL80211_CMD_NEW_SCAN_RESULTS ||
2772 cmd == NL80211_CMD_SCAN_ABORTED)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002773 wpa_driver_nl80211_set_mode(drv->first_bss,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002774 drv->ap_scan_as_station);
2775 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002776 }
2777
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002778 switch (cmd) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002779 case NL80211_CMD_TRIGGER_SCAN:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002780 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Scan trigger");
Dmitry Shmidt56052862013-10-04 10:23:25 -07002781 drv->scan_state = SCAN_STARTED;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002782 wpa_supplicant_event(drv->ctx, EVENT_SCAN_STARTED, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002783 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002784 case NL80211_CMD_START_SCHED_SCAN:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002785 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Sched scan started");
Dmitry Shmidt56052862013-10-04 10:23:25 -07002786 drv->scan_state = SCHED_SCAN_STARTED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002787 break;
2788 case NL80211_CMD_SCHED_SCAN_STOPPED:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002789 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Sched scan stopped");
Dmitry Shmidt56052862013-10-04 10:23:25 -07002790 drv->scan_state = SCHED_SCAN_STOPPED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002791 wpa_supplicant_event(drv->ctx, EVENT_SCHED_SCAN_STOPPED, NULL);
2792 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002793 case NL80211_CMD_NEW_SCAN_RESULTS:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002794 wpa_dbg(drv->ctx, MSG_DEBUG,
2795 "nl80211: New scan results available");
Dmitry Shmidt56052862013-10-04 10:23:25 -07002796 drv->scan_state = SCAN_COMPLETED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002797 drv->scan_complete_events = 1;
2798 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
2799 drv->ctx);
2800 send_scan_event(drv, 0, tb);
2801 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002802 case NL80211_CMD_SCHED_SCAN_RESULTS:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002803 wpa_dbg(drv->ctx, MSG_DEBUG,
2804 "nl80211: New sched scan results available");
Dmitry Shmidt56052862013-10-04 10:23:25 -07002805 drv->scan_state = SCHED_SCAN_RESULTS;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002806 send_scan_event(drv, 0, tb);
2807 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002808 case NL80211_CMD_SCAN_ABORTED:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002809 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Scan aborted");
Dmitry Shmidt56052862013-10-04 10:23:25 -07002810 drv->scan_state = SCAN_ABORTED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002811 /*
2812 * Need to indicate that scan results are available in order
2813 * not to make wpa_supplicant stop its scanning.
2814 */
2815 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
2816 drv->ctx);
2817 send_scan_event(drv, 1, tb);
2818 break;
2819 case NL80211_CMD_AUTHENTICATE:
2820 case NL80211_CMD_ASSOCIATE:
2821 case NL80211_CMD_DEAUTHENTICATE:
2822 case NL80211_CMD_DISASSOCIATE:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002823 case NL80211_CMD_FRAME_TX_STATUS:
2824 case NL80211_CMD_UNPROT_DEAUTHENTICATE:
2825 case NL80211_CMD_UNPROT_DISASSOCIATE:
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002826 mlme_event(bss, cmd, tb[NL80211_ATTR_FRAME],
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002827 tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT],
2828 tb[NL80211_ATTR_WIPHY_FREQ], tb[NL80211_ATTR_ACK],
Dmitry Shmidt04949592012-07-19 12:16:46 -07002829 tb[NL80211_ATTR_COOKIE],
2830 tb[NL80211_ATTR_RX_SIGNAL_DBM]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002831 break;
2832 case NL80211_CMD_CONNECT:
2833 case NL80211_CMD_ROAM:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002834 mlme_event_connect(drv, cmd,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002835 tb[NL80211_ATTR_STATUS_CODE],
2836 tb[NL80211_ATTR_MAC],
2837 tb[NL80211_ATTR_REQ_IE],
2838 tb[NL80211_ATTR_RESP_IE]);
2839 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002840 case NL80211_CMD_CH_SWITCH_NOTIFY:
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002841 mlme_event_ch_switch(drv,
2842 tb[NL80211_ATTR_IFINDEX],
2843 tb[NL80211_ATTR_WIPHY_FREQ],
2844 tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE],
2845 tb[NL80211_ATTR_CHANNEL_WIDTH],
2846 tb[NL80211_ATTR_CENTER_FREQ1],
2847 tb[NL80211_ATTR_CENTER_FREQ2]);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002848 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002849 case NL80211_CMD_DISCONNECT:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002850 mlme_event_disconnect(drv, tb[NL80211_ATTR_REASON_CODE],
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002851 tb[NL80211_ATTR_MAC],
2852 tb[NL80211_ATTR_DISCONNECTED_BY_AP]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002853 break;
2854 case NL80211_CMD_MICHAEL_MIC_FAILURE:
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002855 mlme_event_michael_mic_failure(bss, tb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002856 break;
2857 case NL80211_CMD_JOIN_IBSS:
2858 mlme_event_join_ibss(drv, tb);
2859 break;
2860 case NL80211_CMD_REMAIN_ON_CHANNEL:
2861 mlme_event_remain_on_channel(drv, 0, tb);
2862 break;
2863 case NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL:
2864 mlme_event_remain_on_channel(drv, 1, tb);
2865 break;
2866 case NL80211_CMD_NOTIFY_CQM:
2867 nl80211_cqm_event(drv, tb);
2868 break;
2869 case NL80211_CMD_REG_CHANGE:
2870 wpa_printf(MSG_DEBUG, "nl80211: Regulatory domain change");
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002871 if (tb[NL80211_ATTR_REG_INITIATOR] == NULL)
2872 break;
2873 os_memset(&data, 0, sizeof(data));
2874 switch (nla_get_u8(tb[NL80211_ATTR_REG_INITIATOR])) {
2875 case NL80211_REGDOM_SET_BY_CORE:
2876 data.channel_list_changed.initiator =
2877 REGDOM_SET_BY_CORE;
2878 break;
2879 case NL80211_REGDOM_SET_BY_USER:
2880 data.channel_list_changed.initiator =
2881 REGDOM_SET_BY_USER;
2882 break;
2883 case NL80211_REGDOM_SET_BY_DRIVER:
2884 data.channel_list_changed.initiator =
2885 REGDOM_SET_BY_DRIVER;
2886 break;
2887 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
2888 data.channel_list_changed.initiator =
2889 REGDOM_SET_BY_COUNTRY_IE;
2890 break;
2891 default:
2892 wpa_printf(MSG_DEBUG, "nl80211: Unknown reg change initiator %d received",
2893 nla_get_u8(tb[NL80211_ATTR_REG_INITIATOR]));
2894 break;
2895 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002896 wpa_supplicant_event(drv->ctx, EVENT_CHANNEL_LIST_CHANGED,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002897 &data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002898 break;
2899 case NL80211_CMD_REG_BEACON_HINT:
2900 wpa_printf(MSG_DEBUG, "nl80211: Regulatory beacon hint");
2901 wpa_supplicant_event(drv->ctx, EVENT_CHANNEL_LIST_CHANGED,
2902 NULL);
2903 break;
2904 case NL80211_CMD_NEW_STATION:
2905 nl80211_new_station_event(drv, tb);
2906 break;
2907 case NL80211_CMD_DEL_STATION:
2908 nl80211_del_station_event(drv, tb);
2909 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002910 case NL80211_CMD_SET_REKEY_OFFLOAD:
2911 nl80211_rekey_offload_event(drv, tb);
2912 break;
2913 case NL80211_CMD_PMKSA_CANDIDATE:
2914 nl80211_pmksa_candidate_event(drv, tb);
2915 break;
2916 case NL80211_CMD_PROBE_CLIENT:
2917 nl80211_client_probe_event(drv, tb);
2918 break;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002919 case NL80211_CMD_TDLS_OPER:
2920 nl80211_tdls_oper_event(drv, tb);
2921 break;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002922 case NL80211_CMD_CONN_FAILED:
2923 nl80211_connect_failed_event(drv, tb);
2924 break;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002925 case NL80211_CMD_FT_EVENT:
2926 mlme_event_ft_event(drv, tb);
2927 break;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002928 case NL80211_CMD_RADAR_DETECT:
2929 nl80211_radar_event(drv, tb);
2930 break;
Dmitry Shmidt5393a0f2013-08-08 11:23:34 -07002931 case NL80211_CMD_STOP_AP:
2932 nl80211_stop_ap(drv, tb);
2933 break;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002934 case NL80211_CMD_VENDOR:
2935 nl80211_vendor_event(drv, tb);
2936 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002937 default:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002938 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Ignored unknown event "
2939 "(cmd=%d)", cmd);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002940 break;
2941 }
2942}
2943
2944
2945static int process_drv_event(struct nl_msg *msg, void *arg)
2946{
2947 struct wpa_driver_nl80211_data *drv = arg;
2948 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2949 struct nlattr *tb[NL80211_ATTR_MAX + 1];
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002950 struct i802_bss *bss;
2951 int ifidx = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002952
2953 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2954 genlmsg_attrlen(gnlh, 0), NULL);
2955
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002956 if (tb[NL80211_ATTR_IFINDEX]) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002957 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
2958
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002959 for (bss = drv->first_bss; bss; bss = bss->next)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002960 if (ifidx == -1 || ifidx == bss->ifindex) {
2961 do_process_drv_event(bss, gnlh->cmd, tb);
2962 return NL_SKIP;
2963 }
2964 wpa_printf(MSG_DEBUG,
2965 "nl80211: Ignored event (cmd=%d) for foreign interface (ifindex %d)",
2966 gnlh->cmd, ifidx);
2967 } else if (tb[NL80211_ATTR_WDEV]) {
2968 u64 wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
2969 wpa_printf(MSG_DEBUG, "nl80211: Process event on P2P device");
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002970 for (bss = drv->first_bss; bss; bss = bss->next) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002971 if (bss->wdev_id_set && wdev_id == bss->wdev_id) {
2972 do_process_drv_event(bss, gnlh->cmd, tb);
2973 return NL_SKIP;
2974 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002975 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002976 wpa_printf(MSG_DEBUG,
2977 "nl80211: Ignored event (cmd=%d) for foreign interface (wdev 0x%llx)",
2978 gnlh->cmd, (long long unsigned int) wdev_id);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002979 }
2980
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002981 return NL_SKIP;
2982}
2983
2984
2985static int process_global_event(struct nl_msg *msg, void *arg)
2986{
2987 struct nl80211_global *global = arg;
2988 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2989 struct nlattr *tb[NL80211_ATTR_MAX + 1];
Dmitry Shmidt04949592012-07-19 12:16:46 -07002990 struct wpa_driver_nl80211_data *drv, *tmp;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002991 int ifidx = -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002992 struct i802_bss *bss;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002993 u64 wdev_id = 0;
2994 int wdev_id_set = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002995
2996 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2997 genlmsg_attrlen(gnlh, 0), NULL);
2998
2999 if (tb[NL80211_ATTR_IFINDEX])
3000 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003001 else if (tb[NL80211_ATTR_WDEV]) {
3002 wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
3003 wdev_id_set = 1;
3004 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003005
Dmitry Shmidt04949592012-07-19 12:16:46 -07003006 dl_list_for_each_safe(drv, tmp, &global->interfaces,
3007 struct wpa_driver_nl80211_data, list) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003008 for (bss = drv->first_bss; bss; bss = bss->next) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003009 if ((ifidx == -1 && !wdev_id_set) ||
3010 ifidx == bss->ifindex ||
3011 (wdev_id_set && bss->wdev_id_set &&
3012 wdev_id == bss->wdev_id)) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003013 do_process_drv_event(bss, gnlh->cmd, tb);
3014 return NL_SKIP;
3015 }
3016 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003017 }
3018
3019 return NL_SKIP;
3020}
3021
3022
3023static int process_bss_event(struct nl_msg *msg, void *arg)
3024{
3025 struct i802_bss *bss = arg;
3026 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3027 struct nlattr *tb[NL80211_ATTR_MAX + 1];
3028
3029 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3030 genlmsg_attrlen(gnlh, 0), NULL);
3031
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003032 wpa_printf(MSG_DEBUG, "nl80211: BSS Event %d (%s) received for %s",
3033 gnlh->cmd, nl80211_command_to_string(gnlh->cmd),
3034 bss->ifname);
3035
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003036 switch (gnlh->cmd) {
3037 case NL80211_CMD_FRAME:
3038 case NL80211_CMD_FRAME_TX_STATUS:
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003039 mlme_event(bss, gnlh->cmd, tb[NL80211_ATTR_FRAME],
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003040 tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT],
3041 tb[NL80211_ATTR_WIPHY_FREQ], tb[NL80211_ATTR_ACK],
Dmitry Shmidt04949592012-07-19 12:16:46 -07003042 tb[NL80211_ATTR_COOKIE],
3043 tb[NL80211_ATTR_RX_SIGNAL_DBM]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003044 break;
3045 case NL80211_CMD_UNEXPECTED_FRAME:
3046 nl80211_spurious_frame(bss, tb, 0);
3047 break;
3048 case NL80211_CMD_UNEXPECTED_4ADDR_FRAME:
3049 nl80211_spurious_frame(bss, tb, 1);
3050 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003051 default:
3052 wpa_printf(MSG_DEBUG, "nl80211: Ignored unknown event "
3053 "(cmd=%d)", gnlh->cmd);
3054 break;
3055 }
3056
3057 return NL_SKIP;
3058}
3059
3060
3061static void wpa_driver_nl80211_event_receive(int sock, void *eloop_ctx,
3062 void *handle)
3063{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003064 struct nl_cb *cb = eloop_ctx;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003065 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003066
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003067 wpa_printf(MSG_MSGDUMP, "nl80211: Event message available");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003068
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003069 res = nl_recvmsgs(handle, cb);
3070 if (res) {
3071 wpa_printf(MSG_INFO, "nl80211: %s->nl_recvmsgs failed: %d",
3072 __func__, res);
3073 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003074}
3075
3076
3077/**
3078 * wpa_driver_nl80211_set_country - ask nl80211 to set the regulatory domain
3079 * @priv: driver_nl80211 private data
3080 * @alpha2_arg: country to which to switch to
3081 * Returns: 0 on success, -1 on failure
3082 *
3083 * This asks nl80211 to set the regulatory domain for given
3084 * country ISO / IEC alpha2.
3085 */
3086static int wpa_driver_nl80211_set_country(void *priv, const char *alpha2_arg)
3087{
3088 struct i802_bss *bss = priv;
3089 struct wpa_driver_nl80211_data *drv = bss->drv;
3090 char alpha2[3];
3091 struct nl_msg *msg;
3092
3093 msg = nlmsg_alloc();
3094 if (!msg)
3095 return -ENOMEM;
3096
3097 alpha2[0] = alpha2_arg[0];
3098 alpha2[1] = alpha2_arg[1];
3099 alpha2[2] = '\0';
3100
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003101 nl80211_cmd(drv, msg, 0, NL80211_CMD_REQ_SET_REG);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003102
3103 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, alpha2);
3104 if (send_and_recv_msgs(drv, msg, NULL, NULL))
3105 return -EINVAL;
3106 return 0;
3107nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003108 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003109 return -EINVAL;
3110}
3111
3112
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003113static int nl80211_get_country(struct nl_msg *msg, void *arg)
3114{
3115 char *alpha2 = arg;
3116 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
3117 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3118
3119 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3120 genlmsg_attrlen(gnlh, 0), NULL);
3121 if (!tb_msg[NL80211_ATTR_REG_ALPHA2]) {
3122 wpa_printf(MSG_DEBUG, "nl80211: No country information available");
3123 return NL_SKIP;
3124 }
3125 os_strlcpy(alpha2, nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]), 3);
3126 return NL_SKIP;
3127}
3128
3129
3130static int wpa_driver_nl80211_get_country(void *priv, char *alpha2)
3131{
3132 struct i802_bss *bss = priv;
3133 struct wpa_driver_nl80211_data *drv = bss->drv;
3134 struct nl_msg *msg;
3135 int ret;
3136
3137 msg = nlmsg_alloc();
3138 if (!msg)
3139 return -ENOMEM;
3140
3141 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG);
3142 alpha2[0] = '\0';
3143 ret = send_and_recv_msgs(drv, msg, nl80211_get_country, alpha2);
3144 if (!alpha2[0])
3145 ret = -1;
3146
3147 return ret;
3148}
3149
3150
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003151static int protocol_feature_handler(struct nl_msg *msg, void *arg)
3152{
3153 u32 *feat = arg;
3154 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
3155 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3156
3157 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3158 genlmsg_attrlen(gnlh, 0), NULL);
3159
3160 if (tb_msg[NL80211_ATTR_PROTOCOL_FEATURES])
3161 *feat = nla_get_u32(tb_msg[NL80211_ATTR_PROTOCOL_FEATURES]);
3162
3163 return NL_SKIP;
3164}
3165
3166
3167static u32 get_nl80211_protocol_features(struct wpa_driver_nl80211_data *drv)
3168{
3169 u32 feat = 0;
3170 struct nl_msg *msg;
3171
3172 msg = nlmsg_alloc();
3173 if (!msg)
3174 goto nla_put_failure;
3175
3176 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_PROTOCOL_FEATURES);
3177 if (send_and_recv_msgs(drv, msg, protocol_feature_handler, &feat) == 0)
3178 return feat;
3179
3180 msg = NULL;
3181nla_put_failure:
3182 nlmsg_free(msg);
3183 return 0;
3184}
3185
3186
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003187struct wiphy_info_data {
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003188 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003189 struct wpa_driver_capa *capa;
3190
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003191 unsigned int num_multichan_concurrent;
3192
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003193 unsigned int error:1;
3194 unsigned int device_ap_sme:1;
3195 unsigned int poll_command_supported:1;
3196 unsigned int data_tx_status:1;
3197 unsigned int monitor_supported:1;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003198 unsigned int auth_supported:1;
3199 unsigned int connect_supported:1;
3200 unsigned int p2p_go_supported:1;
3201 unsigned int p2p_client_supported:1;
3202 unsigned int p2p_concurrent:1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003203 unsigned int channel_switch_supported:1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003204 unsigned int set_qos_map_supported:1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003205};
3206
3207
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003208static unsigned int probe_resp_offload_support(int supp_protocols)
3209{
3210 unsigned int prot = 0;
3211
3212 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS)
3213 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS;
3214 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2)
3215 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS2;
3216 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P)
3217 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_P2P;
3218 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_80211U)
3219 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_INTERWORKING;
3220
3221 return prot;
3222}
3223
3224
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003225static void wiphy_info_supported_iftypes(struct wiphy_info_data *info,
3226 struct nlattr *tb)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003227{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003228 struct nlattr *nl_mode;
3229 int i;
3230
3231 if (tb == NULL)
3232 return;
3233
3234 nla_for_each_nested(nl_mode, tb, i) {
3235 switch (nla_type(nl_mode)) {
3236 case NL80211_IFTYPE_AP:
3237 info->capa->flags |= WPA_DRIVER_FLAGS_AP;
3238 break;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003239 case NL80211_IFTYPE_ADHOC:
3240 info->capa->flags |= WPA_DRIVER_FLAGS_IBSS;
3241 break;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003242 case NL80211_IFTYPE_P2P_DEVICE:
3243 info->capa->flags |=
3244 WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE;
3245 break;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003246 case NL80211_IFTYPE_P2P_GO:
3247 info->p2p_go_supported = 1;
3248 break;
3249 case NL80211_IFTYPE_P2P_CLIENT:
3250 info->p2p_client_supported = 1;
3251 break;
3252 case NL80211_IFTYPE_MONITOR:
3253 info->monitor_supported = 1;
3254 break;
3255 }
3256 }
3257}
3258
3259
3260static int wiphy_info_iface_comb_process(struct wiphy_info_data *info,
3261 struct nlattr *nl_combi)
3262{
3263 struct nlattr *tb_comb[NUM_NL80211_IFACE_COMB];
3264 struct nlattr *tb_limit[NUM_NL80211_IFACE_LIMIT];
3265 struct nlattr *nl_limit, *nl_mode;
3266 int err, rem_limit, rem_mode;
3267 int combination_has_p2p = 0, combination_has_mgd = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003268 static struct nla_policy
3269 iface_combination_policy[NUM_NL80211_IFACE_COMB] = {
3270 [NL80211_IFACE_COMB_LIMITS] = { .type = NLA_NESTED },
3271 [NL80211_IFACE_COMB_MAXNUM] = { .type = NLA_U32 },
3272 [NL80211_IFACE_COMB_STA_AP_BI_MATCH] = { .type = NLA_FLAG },
3273 [NL80211_IFACE_COMB_NUM_CHANNELS] = { .type = NLA_U32 },
Dmitry Shmidtea69e842013-05-13 14:52:28 -07003274 [NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS] = { .type = NLA_U32 },
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003275 },
3276 iface_limit_policy[NUM_NL80211_IFACE_LIMIT] = {
3277 [NL80211_IFACE_LIMIT_TYPES] = { .type = NLA_NESTED },
3278 [NL80211_IFACE_LIMIT_MAX] = { .type = NLA_U32 },
3279 };
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003280
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003281 err = nla_parse_nested(tb_comb, MAX_NL80211_IFACE_COMB,
3282 nl_combi, iface_combination_policy);
3283 if (err || !tb_comb[NL80211_IFACE_COMB_LIMITS] ||
3284 !tb_comb[NL80211_IFACE_COMB_MAXNUM] ||
3285 !tb_comb[NL80211_IFACE_COMB_NUM_CHANNELS])
3286 return 0; /* broken combination */
3287
Dmitry Shmidtea69e842013-05-13 14:52:28 -07003288 if (tb_comb[NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS])
3289 info->capa->flags |= WPA_DRIVER_FLAGS_RADAR;
3290
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003291 nla_for_each_nested(nl_limit, tb_comb[NL80211_IFACE_COMB_LIMITS],
3292 rem_limit) {
3293 err = nla_parse_nested(tb_limit, MAX_NL80211_IFACE_LIMIT,
3294 nl_limit, iface_limit_policy);
3295 if (err || !tb_limit[NL80211_IFACE_LIMIT_TYPES])
3296 return 0; /* broken combination */
3297
3298 nla_for_each_nested(nl_mode,
3299 tb_limit[NL80211_IFACE_LIMIT_TYPES],
3300 rem_mode) {
3301 int ift = nla_type(nl_mode);
3302 if (ift == NL80211_IFTYPE_P2P_GO ||
3303 ift == NL80211_IFTYPE_P2P_CLIENT)
3304 combination_has_p2p = 1;
3305 if (ift == NL80211_IFTYPE_STATION)
3306 combination_has_mgd = 1;
3307 }
3308 if (combination_has_p2p && combination_has_mgd)
3309 break;
3310 }
3311
3312 if (combination_has_p2p && combination_has_mgd) {
3313 info->p2p_concurrent = 1;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003314 info->num_multichan_concurrent =
3315 nla_get_u32(tb_comb[NL80211_IFACE_COMB_NUM_CHANNELS]);
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003316 return 1;
3317 }
3318
3319 return 0;
3320}
3321
3322
3323static void wiphy_info_iface_comb(struct wiphy_info_data *info,
3324 struct nlattr *tb)
3325{
3326 struct nlattr *nl_combi;
3327 int rem_combi;
3328
3329 if (tb == NULL)
3330 return;
3331
3332 nla_for_each_nested(nl_combi, tb, rem_combi) {
3333 if (wiphy_info_iface_comb_process(info, nl_combi) > 0)
3334 break;
3335 }
3336}
3337
3338
3339static void wiphy_info_supp_cmds(struct wiphy_info_data *info,
3340 struct nlattr *tb)
3341{
3342 struct nlattr *nl_cmd;
3343 int i;
3344
3345 if (tb == NULL)
3346 return;
3347
3348 nla_for_each_nested(nl_cmd, tb, i) {
3349 switch (nla_get_u32(nl_cmd)) {
3350 case NL80211_CMD_AUTHENTICATE:
3351 info->auth_supported = 1;
3352 break;
3353 case NL80211_CMD_CONNECT:
3354 info->connect_supported = 1;
3355 break;
3356 case NL80211_CMD_START_SCHED_SCAN:
3357 info->capa->sched_scan_supported = 1;
3358 break;
3359 case NL80211_CMD_PROBE_CLIENT:
3360 info->poll_command_supported = 1;
3361 break;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003362 case NL80211_CMD_CHANNEL_SWITCH:
3363 info->channel_switch_supported = 1;
3364 break;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003365 case NL80211_CMD_SET_QOS_MAP:
3366 info->set_qos_map_supported = 1;
3367 break;
3368 }
3369 }
3370}
3371
3372
3373static void wiphy_info_cipher_suites(struct wiphy_info_data *info,
3374 struct nlattr *tb)
3375{
3376 int i, num;
3377 u32 *ciphers;
3378
3379 if (tb == NULL)
3380 return;
3381
3382 num = nla_len(tb) / sizeof(u32);
3383 ciphers = nla_data(tb);
3384 for (i = 0; i < num; i++) {
3385 u32 c = ciphers[i];
3386
3387 wpa_printf(MSG_DEBUG, "nl80211: Supported cipher %02x-%02x-%02x:%d",
3388 c >> 24, (c >> 16) & 0xff,
3389 (c >> 8) & 0xff, c & 0xff);
3390 switch (c) {
3391 case WLAN_CIPHER_SUITE_CCMP_256:
3392 info->capa->enc |= WPA_DRIVER_CAPA_ENC_CCMP_256;
3393 break;
3394 case WLAN_CIPHER_SUITE_GCMP_256:
3395 info->capa->enc |= WPA_DRIVER_CAPA_ENC_GCMP_256;
3396 break;
3397 case WLAN_CIPHER_SUITE_CCMP:
3398 info->capa->enc |= WPA_DRIVER_CAPA_ENC_CCMP;
3399 break;
3400 case WLAN_CIPHER_SUITE_GCMP:
3401 info->capa->enc |= WPA_DRIVER_CAPA_ENC_GCMP;
3402 break;
3403 case WLAN_CIPHER_SUITE_TKIP:
3404 info->capa->enc |= WPA_DRIVER_CAPA_ENC_TKIP;
3405 break;
3406 case WLAN_CIPHER_SUITE_WEP104:
3407 info->capa->enc |= WPA_DRIVER_CAPA_ENC_WEP104;
3408 break;
3409 case WLAN_CIPHER_SUITE_WEP40:
3410 info->capa->enc |= WPA_DRIVER_CAPA_ENC_WEP40;
3411 break;
3412 case WLAN_CIPHER_SUITE_AES_CMAC:
3413 info->capa->enc |= WPA_DRIVER_CAPA_ENC_BIP;
3414 break;
3415 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
3416 info->capa->enc |= WPA_DRIVER_CAPA_ENC_BIP_GMAC_128;
3417 break;
3418 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
3419 info->capa->enc |= WPA_DRIVER_CAPA_ENC_BIP_GMAC_256;
3420 break;
3421 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
3422 info->capa->enc |= WPA_DRIVER_CAPA_ENC_BIP_CMAC_256;
3423 break;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003424 }
3425 }
3426}
3427
3428
3429static void wiphy_info_max_roc(struct wpa_driver_capa *capa,
3430 struct nlattr *tb)
3431{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003432 if (tb)
3433 capa->max_remain_on_chan = nla_get_u32(tb);
3434}
3435
3436
3437static void wiphy_info_tdls(struct wpa_driver_capa *capa, struct nlattr *tdls,
3438 struct nlattr *ext_setup)
3439{
3440 if (tdls == NULL)
3441 return;
3442
3443 wpa_printf(MSG_DEBUG, "nl80211: TDLS supported");
3444 capa->flags |= WPA_DRIVER_FLAGS_TDLS_SUPPORT;
3445
3446 if (ext_setup) {
3447 wpa_printf(MSG_DEBUG, "nl80211: TDLS external setup");
3448 capa->flags |= WPA_DRIVER_FLAGS_TDLS_EXTERNAL_SETUP;
3449 }
3450}
3451
3452
3453static void wiphy_info_feature_flags(struct wiphy_info_data *info,
3454 struct nlattr *tb)
3455{
3456 u32 flags;
3457 struct wpa_driver_capa *capa = info->capa;
3458
3459 if (tb == NULL)
3460 return;
3461
3462 flags = nla_get_u32(tb);
3463
3464 if (flags & NL80211_FEATURE_SK_TX_STATUS)
3465 info->data_tx_status = 1;
3466
3467 if (flags & NL80211_FEATURE_INACTIVITY_TIMER)
3468 capa->flags |= WPA_DRIVER_FLAGS_INACTIVITY_TIMER;
3469
3470 if (flags & NL80211_FEATURE_SAE)
3471 capa->flags |= WPA_DRIVER_FLAGS_SAE;
3472
3473 if (flags & NL80211_FEATURE_NEED_OBSS_SCAN)
3474 capa->flags |= WPA_DRIVER_FLAGS_OBSS_SCAN;
3475}
3476
3477
3478static void wiphy_info_probe_resp_offload(struct wpa_driver_capa *capa,
3479 struct nlattr *tb)
3480{
3481 u32 protocols;
3482
3483 if (tb == NULL)
3484 return;
3485
3486 protocols = nla_get_u32(tb);
3487 wpa_printf(MSG_DEBUG, "nl80211: Supports Probe Response offload in AP "
3488 "mode");
3489 capa->flags |= WPA_DRIVER_FLAGS_PROBE_RESP_OFFLOAD;
3490 capa->probe_resp_offloads = probe_resp_offload_support(protocols);
3491}
3492
3493
3494static int wiphy_info_handler(struct nl_msg *msg, void *arg)
3495{
3496 struct nlattr *tb[NL80211_ATTR_MAX + 1];
3497 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3498 struct wiphy_info_data *info = arg;
3499 struct wpa_driver_capa *capa = info->capa;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003500 struct wpa_driver_nl80211_data *drv = info->drv;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003501
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003502 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3503 genlmsg_attrlen(gnlh, 0), NULL);
3504
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003505 if (tb[NL80211_ATTR_WIPHY_NAME])
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003506 os_strlcpy(drv->phyname,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003507 nla_get_string(tb[NL80211_ATTR_WIPHY_NAME]),
3508 sizeof(drv->phyname));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003509 if (tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS])
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003510 capa->max_scan_ssids =
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003511 nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS]);
3512
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003513 if (tb[NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS])
3514 capa->max_sched_scan_ssids =
3515 nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS]);
3516
3517 if (tb[NL80211_ATTR_MAX_MATCH_SETS])
3518 capa->max_match_sets =
3519 nla_get_u8(tb[NL80211_ATTR_MAX_MATCH_SETS]);
3520
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003521 if (tb[NL80211_ATTR_MAC_ACL_MAX])
3522 capa->max_acl_mac_addrs =
3523 nla_get_u8(tb[NL80211_ATTR_MAC_ACL_MAX]);
3524
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003525 wiphy_info_supported_iftypes(info, tb[NL80211_ATTR_SUPPORTED_IFTYPES]);
3526 wiphy_info_iface_comb(info, tb[NL80211_ATTR_INTERFACE_COMBINATIONS]);
3527 wiphy_info_supp_cmds(info, tb[NL80211_ATTR_SUPPORTED_COMMANDS]);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003528 wiphy_info_cipher_suites(info, tb[NL80211_ATTR_CIPHER_SUITES]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003529
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003530 if (tb[NL80211_ATTR_OFFCHANNEL_TX_OK]) {
3531 wpa_printf(MSG_DEBUG, "nl80211: Using driver-based "
3532 "off-channel TX");
3533 capa->flags |= WPA_DRIVER_FLAGS_OFFCHANNEL_TX;
3534 }
3535
3536 if (tb[NL80211_ATTR_ROAM_SUPPORT]) {
3537 wpa_printf(MSG_DEBUG, "nl80211: Using driver-based roaming");
3538 capa->flags |= WPA_DRIVER_FLAGS_BSS_SELECTION;
3539 }
3540
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003541 wiphy_info_max_roc(capa,
3542 tb[NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003543
3544 if (tb[NL80211_ATTR_SUPPORT_AP_UAPSD])
3545 capa->flags |= WPA_DRIVER_FLAGS_AP_UAPSD;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003546
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003547 wiphy_info_tdls(capa, tb[NL80211_ATTR_TDLS_SUPPORT],
3548 tb[NL80211_ATTR_TDLS_EXTERNAL_SETUP]);
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003549
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003550 if (tb[NL80211_ATTR_DEVICE_AP_SME])
3551 info->device_ap_sme = 1;
3552
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003553 wiphy_info_feature_flags(info, tb[NL80211_ATTR_FEATURE_FLAGS]);
3554 wiphy_info_probe_resp_offload(capa,
3555 tb[NL80211_ATTR_PROBE_RESP_OFFLOAD]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003556
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003557 if (tb[NL80211_ATTR_EXT_CAPA] && tb[NL80211_ATTR_EXT_CAPA_MASK] &&
3558 drv->extended_capa == NULL) {
3559 drv->extended_capa =
3560 os_malloc(nla_len(tb[NL80211_ATTR_EXT_CAPA]));
3561 if (drv->extended_capa) {
3562 os_memcpy(drv->extended_capa,
3563 nla_data(tb[NL80211_ATTR_EXT_CAPA]),
3564 nla_len(tb[NL80211_ATTR_EXT_CAPA]));
3565 drv->extended_capa_len =
3566 nla_len(tb[NL80211_ATTR_EXT_CAPA]);
3567 }
3568 drv->extended_capa_mask =
3569 os_malloc(nla_len(tb[NL80211_ATTR_EXT_CAPA]));
3570 if (drv->extended_capa_mask) {
3571 os_memcpy(drv->extended_capa_mask,
3572 nla_data(tb[NL80211_ATTR_EXT_CAPA]),
3573 nla_len(tb[NL80211_ATTR_EXT_CAPA]));
3574 } else {
3575 os_free(drv->extended_capa);
3576 drv->extended_capa = NULL;
3577 drv->extended_capa_len = 0;
3578 }
3579 }
3580
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003581 if (tb[NL80211_ATTR_VENDOR_DATA]) {
3582 struct nlattr *nl;
3583 int rem;
3584
3585 nla_for_each_nested(nl, tb[NL80211_ATTR_VENDOR_DATA], rem) {
3586 struct nl80211_vendor_cmd_info *vinfo;
3587 if (nla_len(nl) != sizeof(vinfo)) {
3588 wpa_printf(MSG_DEBUG, "nl80211: Unexpected vendor data info");
3589 continue;
3590 }
3591 vinfo = nla_data(nl);
3592 wpa_printf(MSG_DEBUG, "nl80211: Supported vendor command: vendor_id=0x%x subcmd=%u",
3593 vinfo->vendor_id, vinfo->subcmd);
3594 }
3595 }
3596
3597 if (tb[NL80211_ATTR_VENDOR_EVENTS]) {
3598 struct nlattr *nl;
3599 int rem;
3600
3601 nla_for_each_nested(nl, tb[NL80211_ATTR_VENDOR_EVENTS], rem) {
3602 struct nl80211_vendor_cmd_info *vinfo;
3603 if (nla_len(nl) != sizeof(vinfo)) {
3604 wpa_printf(MSG_DEBUG, "nl80211: Unexpected vendor data info");
3605 continue;
3606 }
3607 vinfo = nla_data(nl);
3608 wpa_printf(MSG_DEBUG, "nl80211: Supported vendor event: vendor_id=0x%x subcmd=%u",
3609 vinfo->vendor_id, vinfo->subcmd);
3610 }
3611 }
3612
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003613 return NL_SKIP;
3614}
3615
3616
3617static int wpa_driver_nl80211_get_info(struct wpa_driver_nl80211_data *drv,
3618 struct wiphy_info_data *info)
3619{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003620 u32 feat;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003621 struct nl_msg *msg;
3622
3623 os_memset(info, 0, sizeof(*info));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003624 info->capa = &drv->capa;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003625 info->drv = drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003626
3627 msg = nlmsg_alloc();
3628 if (!msg)
3629 return -1;
3630
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003631 feat = get_nl80211_protocol_features(drv);
3632 if (feat & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP)
3633 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_WIPHY);
3634 else
3635 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003636
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003637 NLA_PUT_FLAG(msg, NL80211_ATTR_SPLIT_WIPHY_DUMP);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003638 if (nl80211_set_iface_id(msg, drv->first_bss) < 0)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003639 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003640
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003641 if (send_and_recv_msgs(drv, msg, wiphy_info_handler, info))
3642 return -1;
3643
3644 if (info->auth_supported)
3645 drv->capa.flags |= WPA_DRIVER_FLAGS_SME;
3646 else if (!info->connect_supported) {
3647 wpa_printf(MSG_INFO, "nl80211: Driver does not support "
3648 "authentication/association or connect commands");
3649 info->error = 1;
3650 }
3651
3652 if (info->p2p_go_supported && info->p2p_client_supported)
3653 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CAPABLE;
3654 if (info->p2p_concurrent) {
3655 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
3656 "interface (driver advertised support)");
3657 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
3658 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
3659 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003660 if (info->num_multichan_concurrent > 1) {
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003661 wpa_printf(MSG_DEBUG, "nl80211: Enable multi-channel "
3662 "concurrent (driver advertised support)");
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003663 drv->capa.num_multichan_concurrent =
3664 info->num_multichan_concurrent;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003665 }
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07003666
3667 /* default to 5000 since early versions of mac80211 don't set it */
3668 if (!drv->capa.max_remain_on_chan)
3669 drv->capa.max_remain_on_chan = 5000;
3670
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003671 if (info->channel_switch_supported)
3672 drv->capa.flags |= WPA_DRIVER_FLAGS_AP_CSA;
3673
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003674 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003675nla_put_failure:
3676 nlmsg_free(msg);
3677 return -1;
3678}
3679
3680
3681static int wpa_driver_nl80211_capa(struct wpa_driver_nl80211_data *drv)
3682{
3683 struct wiphy_info_data info;
3684 if (wpa_driver_nl80211_get_info(drv, &info))
3685 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003686
3687 if (info.error)
3688 return -1;
3689
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003690 drv->has_capability = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003691 drv->capa.key_mgmt = WPA_DRIVER_CAPA_KEY_MGMT_WPA |
3692 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
3693 WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
3694 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003695 drv->capa.auth = WPA_DRIVER_AUTH_OPEN |
3696 WPA_DRIVER_AUTH_SHARED |
3697 WPA_DRIVER_AUTH_LEAP;
3698
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003699 drv->capa.flags |= WPA_DRIVER_FLAGS_SANE_ERROR_CODES;
3700 drv->capa.flags |= WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003701 drv->capa.flags |= WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003702
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003703 if (!info.device_ap_sme) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07003704 drv->capa.flags |= WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003705
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003706 /*
3707 * No AP SME is currently assumed to also indicate no AP MLME
3708 * in the driver/firmware.
3709 */
3710 drv->capa.flags |= WPA_DRIVER_FLAGS_AP_MLME;
3711 }
3712
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003713 drv->device_ap_sme = info.device_ap_sme;
3714 drv->poll_command_supported = info.poll_command_supported;
3715 drv->data_tx_status = info.data_tx_status;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003716 if (info.set_qos_map_supported)
3717 drv->capa.flags |= WPA_DRIVER_FLAGS_QOS_MAPPING;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003718
3719 /*
Dmitry Shmidtaa532512012-09-24 10:35:31 -07003720 * If poll command and tx status are supported, mac80211 is new enough
3721 * to have everything we need to not need monitor interfaces.
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003722 */
Dmitry Shmidtaa532512012-09-24 10:35:31 -07003723 drv->use_monitor = !info.poll_command_supported || !info.data_tx_status;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003724
3725 if (drv->device_ap_sme && drv->use_monitor) {
3726 /*
3727 * Non-mac80211 drivers may not support monitor interface.
3728 * Make sure we do not get stuck with incorrect capability here
3729 * by explicitly testing this.
3730 */
3731 if (!info.monitor_supported) {
3732 wpa_printf(MSG_DEBUG, "nl80211: Disable use_monitor "
3733 "with device_ap_sme since no monitor mode "
3734 "support detected");
3735 drv->use_monitor = 0;
3736 }
3737 }
3738
3739 /*
3740 * If we aren't going to use monitor interfaces, but the
3741 * driver doesn't support data TX status, we won't get TX
3742 * status for EAPOL frames.
3743 */
3744 if (!drv->use_monitor && !info.data_tx_status)
3745 drv->capa.flags &= ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003746
3747 return 0;
3748}
3749
3750
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003751#ifdef ANDROID
3752static int android_genl_ctrl_resolve(struct nl_handle *handle,
3753 const char *name)
3754{
3755 /*
3756 * Android ICS has very minimal genl_ctrl_resolve() implementation, so
3757 * need to work around that.
3758 */
3759 struct nl_cache *cache = NULL;
3760 struct genl_family *nl80211 = NULL;
3761 int id = -1;
3762
3763 if (genl_ctrl_alloc_cache(handle, &cache) < 0) {
3764 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic "
3765 "netlink cache");
3766 goto fail;
3767 }
3768
3769 nl80211 = genl_ctrl_search_by_name(cache, name);
3770 if (nl80211 == NULL)
3771 goto fail;
3772
3773 id = genl_family_get_id(nl80211);
3774
3775fail:
3776 if (nl80211)
3777 genl_family_put(nl80211);
3778 if (cache)
3779 nl_cache_free(cache);
3780
3781 return id;
3782}
3783#define genl_ctrl_resolve android_genl_ctrl_resolve
3784#endif /* ANDROID */
3785
3786
3787static int wpa_driver_nl80211_init_nl_global(struct nl80211_global *global)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003788{
3789 int ret;
3790
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003791 global->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
3792 if (global->nl_cb == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003793 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
3794 "callbacks");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003795 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003796 }
3797
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003798 global->nl = nl_create_handle(global->nl_cb, "nl");
3799 if (global->nl == NULL)
3800 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003801
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003802 global->nl80211_id = genl_ctrl_resolve(global->nl, "nl80211");
3803 if (global->nl80211_id < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003804 wpa_printf(MSG_ERROR, "nl80211: 'nl80211' generic netlink not "
3805 "found");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003806 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003807 }
3808
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003809 global->nl_event = nl_create_handle(global->nl_cb, "event");
3810 if (global->nl_event == NULL)
3811 goto err;
3812
3813 ret = nl_get_multicast_id(global, "nl80211", "scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003814 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003815 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003816 if (ret < 0) {
3817 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
3818 "membership for scan events: %d (%s)",
3819 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003820 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003821 }
3822
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003823 ret = nl_get_multicast_id(global, "nl80211", "mlme");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003824 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003825 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003826 if (ret < 0) {
3827 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
3828 "membership for mlme events: %d (%s)",
3829 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003830 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003831 }
3832
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003833 ret = nl_get_multicast_id(global, "nl80211", "regulatory");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003834 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003835 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003836 if (ret < 0) {
3837 wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
3838 "membership for regulatory events: %d (%s)",
3839 ret, strerror(-ret));
3840 /* Continue without regulatory events */
3841 }
3842
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003843 ret = nl_get_multicast_id(global, "nl80211", "vendor");
3844 if (ret >= 0)
3845 ret = nl_socket_add_membership(global->nl_event, ret);
3846 if (ret < 0) {
3847 wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
3848 "membership for vendor events: %d (%s)",
3849 ret, strerror(-ret));
3850 /* Continue without vendor events */
3851 }
3852
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003853 nl_cb_set(global->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
3854 no_seq_check, NULL);
3855 nl_cb_set(global->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
3856 process_global_event, global);
3857
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003858 nl80211_register_eloop_read(&global->nl_event,
3859 wpa_driver_nl80211_event_receive,
3860 global->nl_cb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003861
3862 return 0;
3863
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003864err:
3865 nl_destroy_handles(&global->nl_event);
3866 nl_destroy_handles(&global->nl);
3867 nl_cb_put(global->nl_cb);
3868 global->nl_cb = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003869 return -1;
3870}
3871
3872
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003873static int wpa_driver_nl80211_init_nl(struct wpa_driver_nl80211_data *drv)
3874{
3875 drv->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
3876 if (!drv->nl_cb) {
3877 wpa_printf(MSG_ERROR, "nl80211: Failed to alloc cb struct");
3878 return -1;
3879 }
3880
3881 nl_cb_set(drv->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
3882 no_seq_check, NULL);
3883 nl_cb_set(drv->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
3884 process_drv_event, drv);
3885
3886 return 0;
3887}
3888
3889
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003890static void wpa_driver_nl80211_rfkill_blocked(void *ctx)
3891{
3892 wpa_printf(MSG_DEBUG, "nl80211: RFKILL blocked");
3893 /*
3894 * This may be for any interface; use ifdown event to disable
3895 * interface.
3896 */
3897}
3898
3899
3900static void wpa_driver_nl80211_rfkill_unblocked(void *ctx)
3901{
3902 struct wpa_driver_nl80211_data *drv = ctx;
3903 wpa_printf(MSG_DEBUG, "nl80211: RFKILL unblocked");
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003904 if (i802_set_iface_flags(drv->first_bss, 1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003905 wpa_printf(MSG_DEBUG, "nl80211: Could not set interface UP "
3906 "after rfkill unblock");
3907 return;
3908 }
3909 /* rtnetlink ifup handler will report interface as enabled */
3910}
3911
3912
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003913static void wpa_driver_nl80211_handle_eapol_tx_status(int sock,
3914 void *eloop_ctx,
3915 void *handle)
3916{
3917 struct wpa_driver_nl80211_data *drv = eloop_ctx;
3918 u8 data[2048];
3919 struct msghdr msg;
3920 struct iovec entry;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003921 u8 control[512];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003922 struct cmsghdr *cmsg;
3923 int res, found_ee = 0, found_wifi = 0, acked = 0;
3924 union wpa_event_data event;
3925
3926 memset(&msg, 0, sizeof(msg));
3927 msg.msg_iov = &entry;
3928 msg.msg_iovlen = 1;
3929 entry.iov_base = data;
3930 entry.iov_len = sizeof(data);
3931 msg.msg_control = &control;
3932 msg.msg_controllen = sizeof(control);
3933
3934 res = recvmsg(sock, &msg, MSG_ERRQUEUE);
3935 /* if error or not fitting 802.3 header, return */
3936 if (res < 14)
3937 return;
3938
3939 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg))
3940 {
3941 if (cmsg->cmsg_level == SOL_SOCKET &&
3942 cmsg->cmsg_type == SCM_WIFI_STATUS) {
3943 int *ack;
3944
3945 found_wifi = 1;
3946 ack = (void *)CMSG_DATA(cmsg);
3947 acked = *ack;
3948 }
3949
3950 if (cmsg->cmsg_level == SOL_PACKET &&
3951 cmsg->cmsg_type == PACKET_TX_TIMESTAMP) {
3952 struct sock_extended_err *err =
3953 (struct sock_extended_err *)CMSG_DATA(cmsg);
3954
3955 if (err->ee_origin == SO_EE_ORIGIN_TXSTATUS)
3956 found_ee = 1;
3957 }
3958 }
3959
3960 if (!found_ee || !found_wifi)
3961 return;
3962
3963 memset(&event, 0, sizeof(event));
3964 event.eapol_tx_status.dst = data;
3965 event.eapol_tx_status.data = data + 14;
3966 event.eapol_tx_status.data_len = res - 14;
3967 event.eapol_tx_status.ack = acked;
3968 wpa_supplicant_event(drv->ctx, EVENT_EAPOL_TX_STATUS, &event);
3969}
3970
3971
3972static int nl80211_init_bss(struct i802_bss *bss)
3973{
3974 bss->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
3975 if (!bss->nl_cb)
3976 return -1;
3977
3978 nl_cb_set(bss->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
3979 no_seq_check, NULL);
3980 nl_cb_set(bss->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
3981 process_bss_event, bss);
3982
3983 return 0;
3984}
3985
3986
3987static void nl80211_destroy_bss(struct i802_bss *bss)
3988{
3989 nl_cb_put(bss->nl_cb);
3990 bss->nl_cb = NULL;
3991}
3992
3993
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003994static void * wpa_driver_nl80211_drv_init(void *ctx, const char *ifname,
3995 void *global_priv, int hostapd,
3996 const u8 *set_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003997{
3998 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003999 struct rfkill_config *rcfg;
4000 struct i802_bss *bss;
4001
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004002 if (global_priv == NULL)
4003 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004004 drv = os_zalloc(sizeof(*drv));
4005 if (drv == NULL)
4006 return NULL;
4007 drv->global = global_priv;
4008 drv->ctx = ctx;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004009 drv->hostapd = !!hostapd;
4010 drv->eapol_sock = -1;
4011 drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int);
4012 drv->if_indices = drv->default_if_indices;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004013
4014 drv->first_bss = os_zalloc(sizeof(*drv->first_bss));
4015 if (!drv->first_bss) {
4016 os_free(drv);
4017 return NULL;
4018 }
4019 bss = drv->first_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004020 bss->drv = drv;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004021 bss->ctx = ctx;
4022
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004023 os_strlcpy(bss->ifname, ifname, sizeof(bss->ifname));
4024 drv->monitor_ifidx = -1;
4025 drv->monitor_sock = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004026 drv->eapol_tx_sock = -1;
4027 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004028
4029 if (wpa_driver_nl80211_init_nl(drv)) {
4030 os_free(drv);
4031 return NULL;
4032 }
4033
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004034 if (nl80211_init_bss(bss))
4035 goto failed;
4036
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004037 rcfg = os_zalloc(sizeof(*rcfg));
4038 if (rcfg == NULL)
4039 goto failed;
4040 rcfg->ctx = drv;
4041 os_strlcpy(rcfg->ifname, ifname, sizeof(rcfg->ifname));
4042 rcfg->blocked_cb = wpa_driver_nl80211_rfkill_blocked;
4043 rcfg->unblocked_cb = wpa_driver_nl80211_rfkill_unblocked;
4044 drv->rfkill = rfkill_init(rcfg);
4045 if (drv->rfkill == NULL) {
4046 wpa_printf(MSG_DEBUG, "nl80211: RFKILL status not available");
4047 os_free(rcfg);
4048 }
4049
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004050 if (linux_iface_up(drv->global->ioctl_sock, ifname) > 0)
4051 drv->start_iface_up = 1;
4052
4053 if (wpa_driver_nl80211_finish_drv_init(drv, set_addr, 1))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004054 goto failed;
4055
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004056 drv->eapol_tx_sock = socket(PF_PACKET, SOCK_DGRAM, 0);
4057 if (drv->eapol_tx_sock < 0)
4058 goto failed;
4059
4060 if (drv->data_tx_status) {
4061 int enabled = 1;
4062
4063 if (setsockopt(drv->eapol_tx_sock, SOL_SOCKET, SO_WIFI_STATUS,
4064 &enabled, sizeof(enabled)) < 0) {
4065 wpa_printf(MSG_DEBUG,
4066 "nl80211: wifi status sockopt failed\n");
4067 drv->data_tx_status = 0;
4068 if (!drv->use_monitor)
4069 drv->capa.flags &=
4070 ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
4071 } else {
4072 eloop_register_read_sock(drv->eapol_tx_sock,
4073 wpa_driver_nl80211_handle_eapol_tx_status,
4074 drv, NULL);
4075 }
4076 }
4077
4078 if (drv->global) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004079 dl_list_add(&drv->global->interfaces, &drv->list);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004080 drv->in_interface_list = 1;
4081 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004082
4083 return bss;
4084
4085failed:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004086 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004087 return NULL;
4088}
4089
4090
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004091/**
4092 * wpa_driver_nl80211_init - Initialize nl80211 driver interface
4093 * @ctx: context to be used when calling wpa_supplicant functions,
4094 * e.g., wpa_supplicant_event()
4095 * @ifname: interface name, e.g., wlan0
4096 * @global_priv: private driver global data from global_init()
4097 * Returns: Pointer to private data, %NULL on failure
4098 */
4099static void * wpa_driver_nl80211_init(void *ctx, const char *ifname,
4100 void *global_priv)
4101{
4102 return wpa_driver_nl80211_drv_init(ctx, ifname, global_priv, 0, NULL);
4103}
4104
4105
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004106static int nl80211_register_frame(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004107 struct nl_handle *nl_handle,
4108 u16 type, const u8 *match, size_t match_len)
4109{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004110 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004111 struct nl_msg *msg;
4112 int ret = -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004113 char buf[30];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004114
4115 msg = nlmsg_alloc();
4116 if (!msg)
4117 return -1;
4118
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004119 buf[0] = '\0';
4120 wpa_snprintf_hex(buf, sizeof(buf), match, match_len);
4121 wpa_printf(MSG_DEBUG, "nl80211: Register frame type=0x%x nl_handle=%p match=%s",
4122 type, nl_handle, buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004123
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004124 nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_ACTION);
4125
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004126 if (nl80211_set_iface_id(msg, bss) < 0)
4127 goto nla_put_failure;
4128
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004129 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE, type);
4130 NLA_PUT(msg, NL80211_ATTR_FRAME_MATCH, match_len, match);
4131
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004132 ret = send_and_recv(drv->global, nl_handle, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004133 msg = NULL;
4134 if (ret) {
4135 wpa_printf(MSG_DEBUG, "nl80211: Register frame command "
4136 "failed (type=%u): ret=%d (%s)",
4137 type, ret, strerror(-ret));
4138 wpa_hexdump(MSG_DEBUG, "nl80211: Register frame match",
4139 match, match_len);
4140 goto nla_put_failure;
4141 }
4142 ret = 0;
4143nla_put_failure:
4144 nlmsg_free(msg);
4145 return ret;
4146}
4147
4148
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004149static int nl80211_alloc_mgmt_handle(struct i802_bss *bss)
4150{
4151 struct wpa_driver_nl80211_data *drv = bss->drv;
4152
4153 if (bss->nl_mgmt) {
4154 wpa_printf(MSG_DEBUG, "nl80211: Mgmt reporting "
4155 "already on! (nl_mgmt=%p)", bss->nl_mgmt);
4156 return -1;
4157 }
4158
4159 bss->nl_mgmt = nl_create_handle(drv->nl_cb, "mgmt");
4160 if (bss->nl_mgmt == NULL)
4161 return -1;
4162
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004163 return 0;
4164}
4165
4166
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004167static void nl80211_mgmt_handle_register_eloop(struct i802_bss *bss)
4168{
4169 nl80211_register_eloop_read(&bss->nl_mgmt,
4170 wpa_driver_nl80211_event_receive,
4171 bss->nl_cb);
4172}
4173
4174
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004175static int nl80211_register_action_frame(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004176 const u8 *match, size_t match_len)
4177{
4178 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_ACTION << 4);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004179 return nl80211_register_frame(bss, bss->nl_mgmt,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004180 type, match, match_len);
4181}
4182
4183
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004184static int nl80211_mgmt_subscribe_non_ap(struct i802_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004185{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004186 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004187 int ret = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004188
4189 if (nl80211_alloc_mgmt_handle(bss))
4190 return -1;
4191 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with non-AP "
4192 "handle %p", bss->nl_mgmt);
4193
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004194 if (drv->nlmode == NL80211_IFTYPE_ADHOC) {
4195 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_AUTH << 4);
4196
4197 /* register for any AUTH message */
4198 nl80211_register_frame(bss, bss->nl_mgmt, type, NULL, 0);
4199 }
4200
Dmitry Shmidt051af732013-10-22 13:52:46 -07004201#ifdef CONFIG_INTERWORKING
4202 /* QoS Map Configure */
4203 if (nl80211_register_action_frame(bss, (u8 *) "\x01\x04", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004204 ret = -1;
Dmitry Shmidt051af732013-10-22 13:52:46 -07004205#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004206#if defined(CONFIG_P2P) || defined(CONFIG_INTERWORKING)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004207 /* GAS Initial Request */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004208 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0a", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004209 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004210 /* GAS Initial Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004211 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0b", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004212 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004213 /* GAS Comeback Request */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004214 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0c", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004215 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004216 /* GAS Comeback Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004217 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0d", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004218 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004219#endif /* CONFIG_P2P || CONFIG_INTERWORKING */
4220#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004221 /* P2P Public Action */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004222 if (nl80211_register_action_frame(bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004223 (u8 *) "\x04\x09\x50\x6f\x9a\x09",
4224 6) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004225 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004226 /* P2P Action */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004227 if (nl80211_register_action_frame(bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004228 (u8 *) "\x7f\x50\x6f\x9a\x09",
4229 5) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004230 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004231#endif /* CONFIG_P2P */
4232#ifdef CONFIG_IEEE80211W
4233 /* SA Query Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004234 if (nl80211_register_action_frame(bss, (u8 *) "\x08\x01", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004235 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004236#endif /* CONFIG_IEEE80211W */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004237#ifdef CONFIG_TDLS
4238 if ((drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT)) {
4239 /* TDLS Discovery Response */
4240 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0e", 2) <
4241 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004242 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004243 }
4244#endif /* CONFIG_TDLS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004245
4246 /* FT Action frames */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004247 if (nl80211_register_action_frame(bss, (u8 *) "\x06", 1) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004248 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004249 else
4250 drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_FT |
4251 WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK;
4252
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004253 /* WNM - BSS Transition Management Request */
4254 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x07", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004255 ret = -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004256 /* WNM-Sleep Mode Response */
4257 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x11", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004258 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004259
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004260 nl80211_mgmt_handle_register_eloop(bss);
4261
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004262 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004263}
4264
4265
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004266static int nl80211_register_spurious_class3(struct i802_bss *bss)
4267{
4268 struct wpa_driver_nl80211_data *drv = bss->drv;
4269 struct nl_msg *msg;
4270 int ret = -1;
4271
4272 msg = nlmsg_alloc();
4273 if (!msg)
4274 return -1;
4275
4276 nl80211_cmd(drv, msg, 0, NL80211_CMD_UNEXPECTED_FRAME);
4277
4278 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
4279
4280 ret = send_and_recv(drv->global, bss->nl_mgmt, msg, NULL, NULL);
4281 msg = NULL;
4282 if (ret) {
4283 wpa_printf(MSG_DEBUG, "nl80211: Register spurious class3 "
4284 "failed: ret=%d (%s)",
4285 ret, strerror(-ret));
4286 goto nla_put_failure;
4287 }
4288 ret = 0;
4289nla_put_failure:
4290 nlmsg_free(msg);
4291 return ret;
4292}
4293
4294
4295static int nl80211_mgmt_subscribe_ap(struct i802_bss *bss)
4296{
4297 static const int stypes[] = {
4298 WLAN_FC_STYPE_AUTH,
4299 WLAN_FC_STYPE_ASSOC_REQ,
4300 WLAN_FC_STYPE_REASSOC_REQ,
4301 WLAN_FC_STYPE_DISASSOC,
4302 WLAN_FC_STYPE_DEAUTH,
4303 WLAN_FC_STYPE_ACTION,
4304 WLAN_FC_STYPE_PROBE_REQ,
4305/* Beacon doesn't work as mac80211 doesn't currently allow
4306 * it, but it wouldn't really be the right thing anyway as
4307 * it isn't per interface ... maybe just dump the scan
4308 * results periodically for OLBC?
4309 */
4310// WLAN_FC_STYPE_BEACON,
4311 };
4312 unsigned int i;
4313
4314 if (nl80211_alloc_mgmt_handle(bss))
4315 return -1;
4316 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
4317 "handle %p", bss->nl_mgmt);
4318
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004319 for (i = 0; i < ARRAY_SIZE(stypes); i++) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004320 if (nl80211_register_frame(bss, bss->nl_mgmt,
4321 (WLAN_FC_TYPE_MGMT << 2) |
4322 (stypes[i] << 4),
4323 NULL, 0) < 0) {
4324 goto out_err;
4325 }
4326 }
4327
4328 if (nl80211_register_spurious_class3(bss))
4329 goto out_err;
4330
4331 if (nl80211_get_wiphy_data_ap(bss) == NULL)
4332 goto out_err;
4333
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004334 nl80211_mgmt_handle_register_eloop(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004335 return 0;
4336
4337out_err:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004338 nl_destroy_handles(&bss->nl_mgmt);
4339 return -1;
4340}
4341
4342
4343static int nl80211_mgmt_subscribe_ap_dev_sme(struct i802_bss *bss)
4344{
4345 if (nl80211_alloc_mgmt_handle(bss))
4346 return -1;
4347 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
4348 "handle %p (device SME)", bss->nl_mgmt);
4349
4350 if (nl80211_register_frame(bss, bss->nl_mgmt,
4351 (WLAN_FC_TYPE_MGMT << 2) |
4352 (WLAN_FC_STYPE_ACTION << 4),
4353 NULL, 0) < 0)
4354 goto out_err;
4355
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004356 nl80211_mgmt_handle_register_eloop(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004357 return 0;
4358
4359out_err:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004360 nl_destroy_handles(&bss->nl_mgmt);
4361 return -1;
4362}
4363
4364
4365static void nl80211_mgmt_unsubscribe(struct i802_bss *bss, const char *reason)
4366{
4367 if (bss->nl_mgmt == NULL)
4368 return;
4369 wpa_printf(MSG_DEBUG, "nl80211: Unsubscribe mgmt frames handle %p "
4370 "(%s)", bss->nl_mgmt, reason);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004371 nl80211_destroy_eloop_handle(&bss->nl_mgmt);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004372
4373 nl80211_put_wiphy_data_ap(bss);
4374}
4375
4376
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004377static void wpa_driver_nl80211_send_rfkill(void *eloop_ctx, void *timeout_ctx)
4378{
4379 wpa_supplicant_event(timeout_ctx, EVENT_INTERFACE_DISABLED, NULL);
4380}
4381
4382
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004383static void nl80211_del_p2pdev(struct i802_bss *bss)
4384{
4385 struct wpa_driver_nl80211_data *drv = bss->drv;
4386 struct nl_msg *msg;
4387 int ret;
4388
4389 msg = nlmsg_alloc();
4390 if (!msg)
4391 return;
4392
4393 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_INTERFACE);
4394 NLA_PUT_U64(msg, NL80211_ATTR_WDEV, bss->wdev_id);
4395
4396 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4397 msg = NULL;
4398
4399 wpa_printf(MSG_DEBUG, "nl80211: Delete P2P Device %s (0x%llx): %s",
4400 bss->ifname, (long long unsigned int) bss->wdev_id,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004401 strerror(-ret));
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004402
4403nla_put_failure:
4404 nlmsg_free(msg);
4405}
4406
4407
4408static int nl80211_set_p2pdev(struct i802_bss *bss, int start)
4409{
4410 struct wpa_driver_nl80211_data *drv = bss->drv;
4411 struct nl_msg *msg;
4412 int ret = -1;
4413
4414 msg = nlmsg_alloc();
4415 if (!msg)
4416 return -1;
4417
4418 if (start)
4419 nl80211_cmd(drv, msg, 0, NL80211_CMD_START_P2P_DEVICE);
4420 else
4421 nl80211_cmd(drv, msg, 0, NL80211_CMD_STOP_P2P_DEVICE);
4422
4423 NLA_PUT_U64(msg, NL80211_ATTR_WDEV, bss->wdev_id);
4424
4425 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4426 msg = NULL;
4427
4428 wpa_printf(MSG_DEBUG, "nl80211: %s P2P Device %s (0x%llx): %s",
4429 start ? "Start" : "Stop",
4430 bss->ifname, (long long unsigned int) bss->wdev_id,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004431 strerror(-ret));
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004432
4433nla_put_failure:
4434 nlmsg_free(msg);
4435 return ret;
4436}
4437
4438
4439static int i802_set_iface_flags(struct i802_bss *bss, int up)
4440{
4441 enum nl80211_iftype nlmode;
4442
4443 nlmode = nl80211_get_ifmode(bss);
4444 if (nlmode != NL80211_IFTYPE_P2P_DEVICE) {
4445 return linux_set_iface_flags(bss->drv->global->ioctl_sock,
4446 bss->ifname, up);
4447 }
4448
4449 /* P2P Device has start/stop which is equivalent */
4450 return nl80211_set_p2pdev(bss, up);
4451}
4452
4453
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004454static int
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004455wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv,
4456 const u8 *set_addr, int first)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004457{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004458 struct i802_bss *bss = drv->first_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004459 int send_rfkill_event = 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004460 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004461
4462 drv->ifindex = if_nametoindex(bss->ifname);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004463 bss->ifindex = drv->ifindex;
4464 bss->wdev_id = drv->global->if_add_wdevid;
4465 bss->wdev_id_set = drv->global->if_add_wdevid_set;
4466
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004467 bss->if_dynamic = drv->ifindex == drv->global->if_add_ifindex;
4468 bss->if_dynamic = bss->if_dynamic || drv->global->if_add_wdevid_set;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004469 drv->global->if_add_wdevid_set = 0;
4470
4471 if (wpa_driver_nl80211_capa(drv))
4472 return -1;
4473
4474 wpa_printf(MSG_DEBUG, "nl80211: interface %s in phy %s",
4475 bss->ifname, drv->phyname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004476
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004477 if (set_addr &&
4478 (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0) ||
4479 linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
4480 set_addr)))
4481 return -1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004482
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004483 if (first && nl80211_get_ifmode(bss) == NL80211_IFTYPE_AP)
4484 drv->start_mode_ap = 1;
4485
4486 if (drv->hostapd)
4487 nlmode = NL80211_IFTYPE_AP;
4488 else if (bss->if_dynamic)
4489 nlmode = nl80211_get_ifmode(bss);
4490 else
4491 nlmode = NL80211_IFTYPE_STATION;
4492
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004493 if (wpa_driver_nl80211_set_mode(bss, nlmode) < 0) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004494 wpa_printf(MSG_ERROR, "nl80211: Could not configure driver mode");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004495 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004496 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004497
4498 if (nlmode == NL80211_IFTYPE_P2P_DEVICE) {
4499 int ret = nl80211_set_p2pdev(bss, 1);
4500 if (ret < 0)
4501 wpa_printf(MSG_ERROR, "nl80211: Could not start P2P device");
4502 nl80211_get_macaddr(bss);
4503 return ret;
4504 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004505
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004506 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004507 if (rfkill_is_blocked(drv->rfkill)) {
4508 wpa_printf(MSG_DEBUG, "nl80211: Could not yet enable "
4509 "interface '%s' due to rfkill",
4510 bss->ifname);
4511 drv->if_disabled = 1;
4512 send_rfkill_event = 1;
4513 } else {
4514 wpa_printf(MSG_ERROR, "nl80211: Could not set "
4515 "interface '%s' UP", bss->ifname);
4516 return -1;
4517 }
4518 }
4519
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004520 if (!drv->hostapd)
4521 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
4522 1, IF_OPER_DORMANT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004523
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004524 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
4525 bss->addr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004526 return -1;
4527
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004528 if (send_rfkill_event) {
4529 eloop_register_timeout(0, 0, wpa_driver_nl80211_send_rfkill,
4530 drv, drv->ctx);
4531 }
4532
4533 return 0;
4534}
4535
4536
4537static int wpa_driver_nl80211_del_beacon(struct wpa_driver_nl80211_data *drv)
4538{
4539 struct nl_msg *msg;
4540
4541 msg = nlmsg_alloc();
4542 if (!msg)
4543 return -ENOMEM;
4544
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004545 wpa_printf(MSG_DEBUG, "nl80211: Remove beacon (ifindex=%d)",
4546 drv->ifindex);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004547 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_BEACON);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004548 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
4549
4550 return send_and_recv_msgs(drv, msg, NULL, NULL);
4551 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004552 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004553 return -ENOBUFS;
4554}
4555
4556
4557/**
4558 * wpa_driver_nl80211_deinit - Deinitialize nl80211 driver interface
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004559 * @bss: Pointer to private nl80211 data from wpa_driver_nl80211_init()
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004560 *
4561 * Shut down driver interface and processing of driver events. Free
4562 * private data buffer if one was allocated in wpa_driver_nl80211_init().
4563 */
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004564static void wpa_driver_nl80211_deinit(struct i802_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004565{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004566 struct wpa_driver_nl80211_data *drv = bss->drv;
4567
Dmitry Shmidt04949592012-07-19 12:16:46 -07004568 bss->in_deinit = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004569 if (drv->data_tx_status)
4570 eloop_unregister_read_sock(drv->eapol_tx_sock);
4571 if (drv->eapol_tx_sock >= 0)
4572 close(drv->eapol_tx_sock);
4573
4574 if (bss->nl_preq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004575 wpa_driver_nl80211_probe_req_report(bss, 0);
4576 if (bss->added_if_into_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004577 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
4578 bss->ifname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004579 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
4580 "interface %s from bridge %s: %s",
4581 bss->ifname, bss->brname, strerror(errno));
4582 }
4583 if (bss->added_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004584 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004585 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
4586 "bridge %s: %s",
4587 bss->brname, strerror(errno));
4588 }
4589
4590 nl80211_remove_monitor_interface(drv);
4591
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004592 if (is_ap_interface(drv->nlmode))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004593 wpa_driver_nl80211_del_beacon(drv);
4594
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004595 if (drv->eapol_sock >= 0) {
4596 eloop_unregister_read_sock(drv->eapol_sock);
4597 close(drv->eapol_sock);
4598 }
4599
4600 if (drv->if_indices != drv->default_if_indices)
4601 os_free(drv->if_indices);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004602
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004603 if (drv->disabled_11b_rates)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004604 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
4605
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004606 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, 0,
4607 IF_OPER_UP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004608 rfkill_deinit(drv->rfkill);
4609
4610 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
4611
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004612 if (!drv->start_iface_up)
4613 (void) i802_set_iface_flags(bss, 0);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004614 if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004615 if (!drv->hostapd || !drv->start_mode_ap)
4616 wpa_driver_nl80211_set_mode(bss,
4617 NL80211_IFTYPE_STATION);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004618 nl80211_mgmt_unsubscribe(bss, "deinit");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004619 } else {
4620 nl80211_mgmt_unsubscribe(bss, "deinit");
4621 nl80211_del_p2pdev(bss);
4622 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004623 nl_cb_put(drv->nl_cb);
4624
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004625 nl80211_destroy_bss(drv->first_bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004626
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004627 os_free(drv->filter_ssids);
4628
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004629 os_free(drv->auth_ie);
4630
4631 if (drv->in_interface_list)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004632 dl_list_del(&drv->list);
4633
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004634 os_free(drv->extended_capa);
4635 os_free(drv->extended_capa_mask);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004636 os_free(drv->first_bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004637 os_free(drv);
4638}
4639
4640
4641/**
4642 * wpa_driver_nl80211_scan_timeout - Scan timeout to report scan completion
4643 * @eloop_ctx: Driver private data
4644 * @timeout_ctx: ctx argument given to wpa_driver_nl80211_init()
4645 *
4646 * This function can be used as registered timeout when starting a scan to
4647 * generate a scan completed event if the driver does not report this.
4648 */
4649static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx, void *timeout_ctx)
4650{
4651 struct wpa_driver_nl80211_data *drv = eloop_ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004652 if (drv->ap_scan_as_station != NL80211_IFTYPE_UNSPECIFIED) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004653 wpa_driver_nl80211_set_mode(drv->first_bss,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004654 drv->ap_scan_as_station);
4655 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004656 }
4657 wpa_printf(MSG_DEBUG, "Scan timeout - try to get results");
4658 wpa_supplicant_event(timeout_ctx, EVENT_SCAN_RESULTS, NULL);
4659}
4660
4661
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004662static struct nl_msg *
4663nl80211_scan_common(struct wpa_driver_nl80211_data *drv, u8 cmd,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004664 struct wpa_driver_scan_params *params, u64 *wdev_id)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004665{
4666 struct nl_msg *msg;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004667 size_t i;
4668
4669 msg = nlmsg_alloc();
4670 if (!msg)
4671 return NULL;
4672
4673 nl80211_cmd(drv, msg, 0, cmd);
4674
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004675 if (!wdev_id)
4676 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
4677 else
4678 NLA_PUT_U64(msg, NL80211_ATTR_WDEV, *wdev_id);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004679
4680 if (params->num_ssids) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004681 struct nlattr *ssids;
4682
4683 ssids = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004684 if (ssids == NULL)
4685 goto fail;
4686 for (i = 0; i < params->num_ssids; i++) {
4687 wpa_hexdump_ascii(MSG_MSGDUMP, "nl80211: Scan SSID",
4688 params->ssids[i].ssid,
4689 params->ssids[i].ssid_len);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004690 if (nla_put(msg, i + 1, params->ssids[i].ssid_len,
4691 params->ssids[i].ssid) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004692 goto fail;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004693 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004694 nla_nest_end(msg, ssids);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004695 }
4696
4697 if (params->extra_ies) {
4698 wpa_hexdump(MSG_MSGDUMP, "nl80211: Scan extra IEs",
4699 params->extra_ies, params->extra_ies_len);
4700 if (nla_put(msg, NL80211_ATTR_IE, params->extra_ies_len,
4701 params->extra_ies) < 0)
4702 goto fail;
4703 }
4704
4705 if (params->freqs) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004706 struct nlattr *freqs;
4707 freqs = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004708 if (freqs == NULL)
4709 goto fail;
4710 for (i = 0; params->freqs[i]; i++) {
4711 wpa_printf(MSG_MSGDUMP, "nl80211: Scan frequency %u "
4712 "MHz", params->freqs[i]);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004713 if (nla_put_u32(msg, i + 1, params->freqs[i]) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004714 goto fail;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004715 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004716 nla_nest_end(msg, freqs);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004717 }
4718
4719 os_free(drv->filter_ssids);
4720 drv->filter_ssids = params->filter_ssids;
4721 params->filter_ssids = NULL;
4722 drv->num_filter_ssids = params->num_filter_ssids;
4723
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004724 if (params->only_new_results) {
4725 wpa_printf(MSG_DEBUG, "nl80211: Add NL80211_SCAN_FLAG_FLUSH");
4726 NLA_PUT_U32(msg, NL80211_ATTR_SCAN_FLAGS,
4727 NL80211_SCAN_FLAG_FLUSH);
4728 }
4729
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004730 return msg;
4731
4732fail:
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004733nla_put_failure:
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004734 nlmsg_free(msg);
4735 return NULL;
4736}
4737
4738
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004739/**
4740 * wpa_driver_nl80211_scan - Request the driver to initiate scan
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004741 * @bss: Pointer to private driver data from wpa_driver_nl80211_init()
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004742 * @params: Scan parameters
4743 * Returns: 0 on success, -1 on failure
4744 */
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004745static int wpa_driver_nl80211_scan(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004746 struct wpa_driver_scan_params *params)
4747{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004748 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004749 int ret = -1, timeout;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004750 struct nl_msg *msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004751
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004752 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: scan request");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004753 drv->scan_for_auth = 0;
4754
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004755 msg = nl80211_scan_common(drv, NL80211_CMD_TRIGGER_SCAN, params,
4756 bss->wdev_id_set ? &bss->wdev_id : NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004757 if (!msg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004758 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004759
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004760 if (params->p2p_probe) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004761 struct nlattr *rates;
4762
Dmitry Shmidt04949592012-07-19 12:16:46 -07004763 wpa_printf(MSG_DEBUG, "nl80211: P2P probe - mask SuppRates");
4764
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004765 rates = nla_nest_start(msg, NL80211_ATTR_SCAN_SUPP_RATES);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004766 if (rates == NULL)
4767 goto nla_put_failure;
4768
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004769 /*
4770 * Remove 2.4 GHz rates 1, 2, 5.5, 11 Mbps from supported rates
4771 * by masking out everything else apart from the OFDM rates 6,
4772 * 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS rates. All 5 GHz
4773 * rates are left enabled.
4774 */
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004775 NLA_PUT(msg, NL80211_BAND_2GHZ, 8,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004776 "\x0c\x12\x18\x24\x30\x48\x60\x6c");
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004777 nla_nest_end(msg, rates);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004778
4779 NLA_PUT_FLAG(msg, NL80211_ATTR_TX_NO_CCK_RATE);
4780 }
4781
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004782 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4783 msg = NULL;
4784 if (ret) {
4785 wpa_printf(MSG_DEBUG, "nl80211: Scan trigger failed: ret=%d "
4786 "(%s)", ret, strerror(-ret));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004787 if (drv->hostapd && is_ap_interface(drv->nlmode)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004788 /*
4789 * mac80211 does not allow scan requests in AP mode, so
4790 * try to do this in station mode.
4791 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004792 if (wpa_driver_nl80211_set_mode(
4793 bss, NL80211_IFTYPE_STATION))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004794 goto nla_put_failure;
4795
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004796 if (wpa_driver_nl80211_scan(bss, params)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004797 wpa_driver_nl80211_set_mode(bss, drv->nlmode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004798 goto nla_put_failure;
4799 }
4800
4801 /* Restore AP mode when processing scan results */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004802 drv->ap_scan_as_station = drv->nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004803 ret = 0;
4804 } else
4805 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004806 }
4807
Dmitry Shmidt56052862013-10-04 10:23:25 -07004808 drv->scan_state = SCAN_REQUESTED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004809 /* Not all drivers generate "scan completed" wireless event, so try to
4810 * read results after a timeout. */
4811 timeout = 10;
4812 if (drv->scan_complete_events) {
4813 /*
4814 * The driver seems to deliver events to notify when scan is
4815 * complete, so use longer timeout to avoid race conditions
4816 * with scanning and following association request.
4817 */
4818 timeout = 30;
4819 }
4820 wpa_printf(MSG_DEBUG, "Scan requested (ret=%d) - scan timeout %d "
4821 "seconds", ret, timeout);
4822 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
4823 eloop_register_timeout(timeout, 0, wpa_driver_nl80211_scan_timeout,
4824 drv, drv->ctx);
4825
4826nla_put_failure:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004827 nlmsg_free(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004828 return ret;
4829}
4830
4831
4832/**
4833 * wpa_driver_nl80211_sched_scan - Initiate a scheduled scan
4834 * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
4835 * @params: Scan parameters
4836 * @interval: Interval between scan cycles in milliseconds
4837 * Returns: 0 on success, -1 on failure or if not supported
4838 */
4839static int wpa_driver_nl80211_sched_scan(void *priv,
4840 struct wpa_driver_scan_params *params,
4841 u32 interval)
4842{
4843 struct i802_bss *bss = priv;
4844 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004845 int ret = -1;
4846 struct nl_msg *msg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004847 size_t i;
4848
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004849 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: sched_scan request");
4850
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004851#ifdef ANDROID
4852 if (!drv->capa.sched_scan_supported)
4853 return android_pno_start(bss, params);
4854#endif /* ANDROID */
4855
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004856 msg = nl80211_scan_common(drv, NL80211_CMD_START_SCHED_SCAN, params,
4857 bss->wdev_id_set ? &bss->wdev_id : NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004858 if (!msg)
4859 goto nla_put_failure;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004860
4861 NLA_PUT_U32(msg, NL80211_ATTR_SCHED_SCAN_INTERVAL, interval);
4862
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004863 if ((drv->num_filter_ssids &&
4864 (int) drv->num_filter_ssids <= drv->capa.max_match_sets) ||
4865 params->filter_rssi) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004866 struct nlattr *match_sets;
4867 match_sets = nla_nest_start(msg, NL80211_ATTR_SCHED_SCAN_MATCH);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004868 if (match_sets == NULL)
4869 goto nla_put_failure;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004870
4871 for (i = 0; i < drv->num_filter_ssids; i++) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004872 struct nlattr *match_set_ssid;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004873 wpa_hexdump_ascii(MSG_MSGDUMP,
4874 "nl80211: Sched scan filter SSID",
4875 drv->filter_ssids[i].ssid,
4876 drv->filter_ssids[i].ssid_len);
4877
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004878 match_set_ssid = nla_nest_start(msg, i + 1);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004879 if (match_set_ssid == NULL)
4880 goto nla_put_failure;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004881 NLA_PUT(msg, NL80211_ATTR_SCHED_SCAN_MATCH_SSID,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004882 drv->filter_ssids[i].ssid_len,
4883 drv->filter_ssids[i].ssid);
4884
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004885 nla_nest_end(msg, match_set_ssid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004886 }
4887
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004888 if (params->filter_rssi) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004889 struct nlattr *match_set_rssi;
4890 match_set_rssi = nla_nest_start(msg, 0);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004891 if (match_set_rssi == NULL)
4892 goto nla_put_failure;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004893 NLA_PUT_U32(msg, NL80211_SCHED_SCAN_MATCH_ATTR_RSSI,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004894 params->filter_rssi);
4895 wpa_printf(MSG_MSGDUMP,
4896 "nl80211: Sched scan RSSI filter %d dBm",
4897 params->filter_rssi);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004898 nla_nest_end(msg, match_set_rssi);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004899 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004900
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004901 nla_nest_end(msg, match_sets);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004902 }
4903
4904 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4905
4906 /* TODO: if we get an error here, we should fall back to normal scan */
4907
4908 msg = NULL;
4909 if (ret) {
4910 wpa_printf(MSG_DEBUG, "nl80211: Sched scan start failed: "
4911 "ret=%d (%s)", ret, strerror(-ret));
4912 goto nla_put_failure;
4913 }
4914
4915 wpa_printf(MSG_DEBUG, "nl80211: Sched scan requested (ret=%d) - "
4916 "scan interval %d msec", ret, interval);
4917
4918nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004919 nlmsg_free(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004920 return ret;
4921}
4922
4923
4924/**
4925 * wpa_driver_nl80211_stop_sched_scan - Stop a scheduled scan
4926 * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
4927 * Returns: 0 on success, -1 on failure or if not supported
4928 */
4929static int wpa_driver_nl80211_stop_sched_scan(void *priv)
4930{
4931 struct i802_bss *bss = priv;
4932 struct wpa_driver_nl80211_data *drv = bss->drv;
4933 int ret = 0;
4934 struct nl_msg *msg;
4935
4936#ifdef ANDROID
4937 if (!drv->capa.sched_scan_supported)
4938 return android_pno_stop(bss);
4939#endif /* ANDROID */
4940
4941 msg = nlmsg_alloc();
4942 if (!msg)
4943 return -1;
4944
4945 nl80211_cmd(drv, msg, 0, NL80211_CMD_STOP_SCHED_SCAN);
4946
4947 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
4948
4949 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4950 msg = NULL;
4951 if (ret) {
4952 wpa_printf(MSG_DEBUG, "nl80211: Sched scan stop failed: "
4953 "ret=%d (%s)", ret, strerror(-ret));
4954 goto nla_put_failure;
4955 }
4956
4957 wpa_printf(MSG_DEBUG, "nl80211: Sched scan stop sent (ret=%d)", ret);
4958
4959nla_put_failure:
4960 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004961 return ret;
4962}
4963
4964
4965static const u8 * nl80211_get_ie(const u8 *ies, size_t ies_len, u8 ie)
4966{
4967 const u8 *end, *pos;
4968
4969 if (ies == NULL)
4970 return NULL;
4971
4972 pos = ies;
4973 end = ies + ies_len;
4974
4975 while (pos + 1 < end) {
4976 if (pos + 2 + pos[1] > end)
4977 break;
4978 if (pos[0] == ie)
4979 return pos;
4980 pos += 2 + pos[1];
4981 }
4982
4983 return NULL;
4984}
4985
4986
4987static int nl80211_scan_filtered(struct wpa_driver_nl80211_data *drv,
4988 const u8 *ie, size_t ie_len)
4989{
4990 const u8 *ssid;
4991 size_t i;
4992
4993 if (drv->filter_ssids == NULL)
4994 return 0;
4995
4996 ssid = nl80211_get_ie(ie, ie_len, WLAN_EID_SSID);
4997 if (ssid == NULL)
4998 return 1;
4999
5000 for (i = 0; i < drv->num_filter_ssids; i++) {
5001 if (ssid[1] == drv->filter_ssids[i].ssid_len &&
5002 os_memcmp(ssid + 2, drv->filter_ssids[i].ssid, ssid[1]) ==
5003 0)
5004 return 0;
5005 }
5006
5007 return 1;
5008}
5009
5010
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005011static int bss_info_handler(struct nl_msg *msg, void *arg)
5012{
5013 struct nlattr *tb[NL80211_ATTR_MAX + 1];
5014 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
5015 struct nlattr *bss[NL80211_BSS_MAX + 1];
5016 static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
5017 [NL80211_BSS_BSSID] = { .type = NLA_UNSPEC },
5018 [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
5019 [NL80211_BSS_TSF] = { .type = NLA_U64 },
5020 [NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 },
5021 [NL80211_BSS_CAPABILITY] = { .type = NLA_U16 },
5022 [NL80211_BSS_INFORMATION_ELEMENTS] = { .type = NLA_UNSPEC },
5023 [NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 },
5024 [NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 },
5025 [NL80211_BSS_STATUS] = { .type = NLA_U32 },
5026 [NL80211_BSS_SEEN_MS_AGO] = { .type = NLA_U32 },
5027 [NL80211_BSS_BEACON_IES] = { .type = NLA_UNSPEC },
5028 };
5029 struct nl80211_bss_info_arg *_arg = arg;
5030 struct wpa_scan_results *res = _arg->res;
5031 struct wpa_scan_res **tmp;
5032 struct wpa_scan_res *r;
5033 const u8 *ie, *beacon_ie;
5034 size_t ie_len, beacon_ie_len;
5035 u8 *pos;
Jouni Malinen87fd2792011-05-16 18:35:42 +03005036 size_t i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005037
5038 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
5039 genlmsg_attrlen(gnlh, 0), NULL);
5040 if (!tb[NL80211_ATTR_BSS])
5041 return NL_SKIP;
5042 if (nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS],
5043 bss_policy))
5044 return NL_SKIP;
Jouni Malinen87fd2792011-05-16 18:35:42 +03005045 if (bss[NL80211_BSS_STATUS]) {
5046 enum nl80211_bss_status status;
5047 status = nla_get_u32(bss[NL80211_BSS_STATUS]);
5048 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
5049 bss[NL80211_BSS_FREQUENCY]) {
5050 _arg->assoc_freq =
5051 nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
5052 wpa_printf(MSG_DEBUG, "nl80211: Associated on %u MHz",
5053 _arg->assoc_freq);
5054 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005055 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
5056 bss[NL80211_BSS_BSSID]) {
5057 os_memcpy(_arg->assoc_bssid,
5058 nla_data(bss[NL80211_BSS_BSSID]), ETH_ALEN);
5059 wpa_printf(MSG_DEBUG, "nl80211: Associated with "
5060 MACSTR, MAC2STR(_arg->assoc_bssid));
5061 }
Jouni Malinen87fd2792011-05-16 18:35:42 +03005062 }
5063 if (!res)
5064 return NL_SKIP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005065 if (bss[NL80211_BSS_INFORMATION_ELEMENTS]) {
5066 ie = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
5067 ie_len = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
5068 } else {
5069 ie = NULL;
5070 ie_len = 0;
5071 }
5072 if (bss[NL80211_BSS_BEACON_IES]) {
5073 beacon_ie = nla_data(bss[NL80211_BSS_BEACON_IES]);
5074 beacon_ie_len = nla_len(bss[NL80211_BSS_BEACON_IES]);
5075 } else {
5076 beacon_ie = NULL;
5077 beacon_ie_len = 0;
5078 }
5079
5080 if (nl80211_scan_filtered(_arg->drv, ie ? ie : beacon_ie,
5081 ie ? ie_len : beacon_ie_len))
5082 return NL_SKIP;
5083
5084 r = os_zalloc(sizeof(*r) + ie_len + beacon_ie_len);
5085 if (r == NULL)
5086 return NL_SKIP;
5087 if (bss[NL80211_BSS_BSSID])
5088 os_memcpy(r->bssid, nla_data(bss[NL80211_BSS_BSSID]),
5089 ETH_ALEN);
5090 if (bss[NL80211_BSS_FREQUENCY])
5091 r->freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
5092 if (bss[NL80211_BSS_BEACON_INTERVAL])
5093 r->beacon_int = nla_get_u16(bss[NL80211_BSS_BEACON_INTERVAL]);
5094 if (bss[NL80211_BSS_CAPABILITY])
5095 r->caps = nla_get_u16(bss[NL80211_BSS_CAPABILITY]);
5096 r->flags |= WPA_SCAN_NOISE_INVALID;
5097 if (bss[NL80211_BSS_SIGNAL_MBM]) {
5098 r->level = nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]);
5099 r->level /= 100; /* mBm to dBm */
5100 r->flags |= WPA_SCAN_LEVEL_DBM | WPA_SCAN_QUAL_INVALID;
5101 } else if (bss[NL80211_BSS_SIGNAL_UNSPEC]) {
5102 r->level = nla_get_u8(bss[NL80211_BSS_SIGNAL_UNSPEC]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005103 r->flags |= WPA_SCAN_QUAL_INVALID;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005104 } else
5105 r->flags |= WPA_SCAN_LEVEL_INVALID | WPA_SCAN_QUAL_INVALID;
5106 if (bss[NL80211_BSS_TSF])
5107 r->tsf = nla_get_u64(bss[NL80211_BSS_TSF]);
5108 if (bss[NL80211_BSS_SEEN_MS_AGO])
5109 r->age = nla_get_u32(bss[NL80211_BSS_SEEN_MS_AGO]);
5110 r->ie_len = ie_len;
5111 pos = (u8 *) (r + 1);
5112 if (ie) {
5113 os_memcpy(pos, ie, ie_len);
5114 pos += ie_len;
5115 }
5116 r->beacon_ie_len = beacon_ie_len;
5117 if (beacon_ie)
5118 os_memcpy(pos, beacon_ie, beacon_ie_len);
5119
5120 if (bss[NL80211_BSS_STATUS]) {
5121 enum nl80211_bss_status status;
5122 status = nla_get_u32(bss[NL80211_BSS_STATUS]);
5123 switch (status) {
5124 case NL80211_BSS_STATUS_AUTHENTICATED:
5125 r->flags |= WPA_SCAN_AUTHENTICATED;
5126 break;
5127 case NL80211_BSS_STATUS_ASSOCIATED:
5128 r->flags |= WPA_SCAN_ASSOCIATED;
5129 break;
5130 default:
5131 break;
5132 }
5133 }
5134
Jouni Malinen87fd2792011-05-16 18:35:42 +03005135 /*
5136 * cfg80211 maintains separate BSS table entries for APs if the same
5137 * BSSID,SSID pair is seen on multiple channels. wpa_supplicant does
5138 * not use frequency as a separate key in the BSS table, so filter out
5139 * duplicated entries. Prefer associated BSS entry in such a case in
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005140 * order to get the correct frequency into the BSS table. Similarly,
5141 * prefer newer entries over older.
Jouni Malinen87fd2792011-05-16 18:35:42 +03005142 */
5143 for (i = 0; i < res->num; i++) {
5144 const u8 *s1, *s2;
5145 if (os_memcmp(res->res[i]->bssid, r->bssid, ETH_ALEN) != 0)
5146 continue;
5147
5148 s1 = nl80211_get_ie((u8 *) (res->res[i] + 1),
5149 res->res[i]->ie_len, WLAN_EID_SSID);
5150 s2 = nl80211_get_ie((u8 *) (r + 1), r->ie_len, WLAN_EID_SSID);
5151 if (s1 == NULL || s2 == NULL || s1[1] != s2[1] ||
5152 os_memcmp(s1, s2, 2 + s1[1]) != 0)
5153 continue;
5154
5155 /* Same BSSID,SSID was already included in scan results */
5156 wpa_printf(MSG_DEBUG, "nl80211: Remove duplicated scan result "
5157 "for " MACSTR, MAC2STR(r->bssid));
5158
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005159 if (((r->flags & WPA_SCAN_ASSOCIATED) &&
5160 !(res->res[i]->flags & WPA_SCAN_ASSOCIATED)) ||
5161 r->age < res->res[i]->age) {
Jouni Malinen87fd2792011-05-16 18:35:42 +03005162 os_free(res->res[i]);
5163 res->res[i] = r;
5164 } else
5165 os_free(r);
5166 return NL_SKIP;
5167 }
5168
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005169 tmp = os_realloc_array(res->res, res->num + 1,
5170 sizeof(struct wpa_scan_res *));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005171 if (tmp == NULL) {
5172 os_free(r);
5173 return NL_SKIP;
5174 }
5175 tmp[res->num++] = r;
5176 res->res = tmp;
5177
5178 return NL_SKIP;
5179}
5180
5181
5182static void clear_state_mismatch(struct wpa_driver_nl80211_data *drv,
5183 const u8 *addr)
5184{
5185 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
5186 wpa_printf(MSG_DEBUG, "nl80211: Clear possible state "
5187 "mismatch (" MACSTR ")", MAC2STR(addr));
5188 wpa_driver_nl80211_mlme(drv, addr,
5189 NL80211_CMD_DEAUTHENTICATE,
5190 WLAN_REASON_PREV_AUTH_NOT_VALID, 1);
5191 }
5192}
5193
5194
5195static void wpa_driver_nl80211_check_bss_status(
5196 struct wpa_driver_nl80211_data *drv, struct wpa_scan_results *res)
5197{
5198 size_t i;
5199
5200 for (i = 0; i < res->num; i++) {
5201 struct wpa_scan_res *r = res->res[i];
5202 if (r->flags & WPA_SCAN_AUTHENTICATED) {
5203 wpa_printf(MSG_DEBUG, "nl80211: Scan results "
5204 "indicates BSS status with " MACSTR
5205 " as authenticated",
5206 MAC2STR(r->bssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005207 if (is_sta_interface(drv->nlmode) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005208 os_memcmp(r->bssid, drv->bssid, ETH_ALEN) != 0 &&
5209 os_memcmp(r->bssid, drv->auth_bssid, ETH_ALEN) !=
5210 0) {
5211 wpa_printf(MSG_DEBUG, "nl80211: Unknown BSSID"
5212 " in local state (auth=" MACSTR
5213 " assoc=" MACSTR ")",
5214 MAC2STR(drv->auth_bssid),
5215 MAC2STR(drv->bssid));
5216 clear_state_mismatch(drv, r->bssid);
5217 }
5218 }
5219
5220 if (r->flags & WPA_SCAN_ASSOCIATED) {
5221 wpa_printf(MSG_DEBUG, "nl80211: Scan results "
5222 "indicate BSS status with " MACSTR
5223 " as associated",
5224 MAC2STR(r->bssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005225 if (is_sta_interface(drv->nlmode) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005226 !drv->associated) {
5227 wpa_printf(MSG_DEBUG, "nl80211: Local state "
5228 "(not associated) does not match "
5229 "with BSS state");
5230 clear_state_mismatch(drv, r->bssid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005231 } else if (is_sta_interface(drv->nlmode) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005232 os_memcmp(drv->bssid, r->bssid, ETH_ALEN) !=
5233 0) {
5234 wpa_printf(MSG_DEBUG, "nl80211: Local state "
5235 "(associated with " MACSTR ") does "
5236 "not match with BSS state",
5237 MAC2STR(drv->bssid));
5238 clear_state_mismatch(drv, r->bssid);
5239 clear_state_mismatch(drv, drv->bssid);
5240 }
5241 }
5242 }
5243}
5244
5245
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005246static struct wpa_scan_results *
5247nl80211_get_scan_results(struct wpa_driver_nl80211_data *drv)
5248{
5249 struct nl_msg *msg;
5250 struct wpa_scan_results *res;
5251 int ret;
5252 struct nl80211_bss_info_arg arg;
5253
5254 res = os_zalloc(sizeof(*res));
5255 if (res == NULL)
5256 return NULL;
5257 msg = nlmsg_alloc();
5258 if (!msg)
5259 goto nla_put_failure;
5260
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005261 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005262 if (nl80211_set_iface_id(msg, drv->first_bss) < 0)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005263 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005264
5265 arg.drv = drv;
5266 arg.res = res;
5267 ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
5268 msg = NULL;
5269 if (ret == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005270 wpa_printf(MSG_DEBUG, "nl80211: Received scan results (%lu "
5271 "BSSes)", (unsigned long) res->num);
5272 nl80211_get_noise_for_scan_results(drv, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005273 return res;
5274 }
5275 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
5276 "(%s)", ret, strerror(-ret));
5277nla_put_failure:
5278 nlmsg_free(msg);
5279 wpa_scan_results_free(res);
5280 return NULL;
5281}
5282
5283
5284/**
5285 * wpa_driver_nl80211_get_scan_results - Fetch the latest scan results
5286 * @priv: Pointer to private wext data from wpa_driver_nl80211_init()
5287 * Returns: Scan results on success, -1 on failure
5288 */
5289static struct wpa_scan_results *
5290wpa_driver_nl80211_get_scan_results(void *priv)
5291{
5292 struct i802_bss *bss = priv;
5293 struct wpa_driver_nl80211_data *drv = bss->drv;
5294 struct wpa_scan_results *res;
5295
5296 res = nl80211_get_scan_results(drv);
5297 if (res)
5298 wpa_driver_nl80211_check_bss_status(drv, res);
5299 return res;
5300}
5301
5302
5303static void nl80211_dump_scan(struct wpa_driver_nl80211_data *drv)
5304{
5305 struct wpa_scan_results *res;
5306 size_t i;
5307
5308 res = nl80211_get_scan_results(drv);
5309 if (res == NULL) {
5310 wpa_printf(MSG_DEBUG, "nl80211: Failed to get scan results");
5311 return;
5312 }
5313
5314 wpa_printf(MSG_DEBUG, "nl80211: Scan result dump");
5315 for (i = 0; i < res->num; i++) {
5316 struct wpa_scan_res *r = res->res[i];
5317 wpa_printf(MSG_DEBUG, "nl80211: %d/%d " MACSTR "%s%s",
5318 (int) i, (int) res->num, MAC2STR(r->bssid),
5319 r->flags & WPA_SCAN_AUTHENTICATED ? " [auth]" : "",
5320 r->flags & WPA_SCAN_ASSOCIATED ? " [assoc]" : "");
5321 }
5322
5323 wpa_scan_results_free(res);
5324}
5325
5326
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005327static u32 wpa_alg_to_cipher_suite(enum wpa_alg alg, size_t key_len)
5328{
5329 switch (alg) {
5330 case WPA_ALG_WEP:
5331 if (key_len == 5)
5332 return WLAN_CIPHER_SUITE_WEP40;
5333 return WLAN_CIPHER_SUITE_WEP104;
5334 case WPA_ALG_TKIP:
5335 return WLAN_CIPHER_SUITE_TKIP;
5336 case WPA_ALG_CCMP:
5337 return WLAN_CIPHER_SUITE_CCMP;
5338 case WPA_ALG_GCMP:
5339 return WLAN_CIPHER_SUITE_GCMP;
5340 case WPA_ALG_CCMP_256:
5341 return WLAN_CIPHER_SUITE_CCMP_256;
5342 case WPA_ALG_GCMP_256:
5343 return WLAN_CIPHER_SUITE_GCMP_256;
5344 case WPA_ALG_IGTK:
5345 return WLAN_CIPHER_SUITE_AES_CMAC;
5346 case WPA_ALG_BIP_GMAC_128:
5347 return WLAN_CIPHER_SUITE_BIP_GMAC_128;
5348 case WPA_ALG_BIP_GMAC_256:
5349 return WLAN_CIPHER_SUITE_BIP_GMAC_256;
5350 case WPA_ALG_BIP_CMAC_256:
5351 return WLAN_CIPHER_SUITE_BIP_CMAC_256;
5352 case WPA_ALG_SMS4:
5353 return WLAN_CIPHER_SUITE_SMS4;
5354 case WPA_ALG_KRK:
5355 return WLAN_CIPHER_SUITE_KRK;
5356 case WPA_ALG_NONE:
5357 case WPA_ALG_PMK:
5358 wpa_printf(MSG_ERROR, "nl80211: Unexpected encryption algorithm %d",
5359 alg);
5360 return 0;
5361 }
5362
5363 wpa_printf(MSG_ERROR, "nl80211: Unsupported encryption algorithm %d",
5364 alg);
5365 return 0;
5366}
5367
5368
5369static u32 wpa_cipher_to_cipher_suite(unsigned int cipher)
5370{
5371 switch (cipher) {
5372 case WPA_CIPHER_CCMP_256:
5373 return WLAN_CIPHER_SUITE_CCMP_256;
5374 case WPA_CIPHER_GCMP_256:
5375 return WLAN_CIPHER_SUITE_GCMP_256;
5376 case WPA_CIPHER_CCMP:
5377 return WLAN_CIPHER_SUITE_CCMP;
5378 case WPA_CIPHER_GCMP:
5379 return WLAN_CIPHER_SUITE_GCMP;
5380 case WPA_CIPHER_TKIP:
5381 return WLAN_CIPHER_SUITE_TKIP;
5382 case WPA_CIPHER_WEP104:
5383 return WLAN_CIPHER_SUITE_WEP104;
5384 case WPA_CIPHER_WEP40:
5385 return WLAN_CIPHER_SUITE_WEP40;
5386 }
5387
5388 return 0;
5389}
5390
5391
5392static int wpa_cipher_to_cipher_suites(unsigned int ciphers, u32 suites[],
5393 int max_suites)
5394{
5395 int num_suites = 0;
5396
5397 if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP_256)
5398 suites[num_suites++] = WLAN_CIPHER_SUITE_CCMP_256;
5399 if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP_256)
5400 suites[num_suites++] = WLAN_CIPHER_SUITE_GCMP_256;
5401 if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP)
5402 suites[num_suites++] = WLAN_CIPHER_SUITE_CCMP;
5403 if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP)
5404 suites[num_suites++] = WLAN_CIPHER_SUITE_GCMP;
5405 if (num_suites < max_suites && ciphers & WPA_CIPHER_TKIP)
5406 suites[num_suites++] = WLAN_CIPHER_SUITE_TKIP;
5407 if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP104)
5408 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP104;
5409 if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP40)
5410 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP40;
5411
5412 return num_suites;
5413}
5414
5415
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005416static int wpa_driver_nl80211_set_key(const char *ifname, struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005417 enum wpa_alg alg, const u8 *addr,
5418 int key_idx, int set_tx,
5419 const u8 *seq, size_t seq_len,
5420 const u8 *key, size_t key_len)
5421{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005422 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005423 int ifindex;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005424 struct nl_msg *msg;
5425 int ret;
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07005426 int tdls = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005427
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005428 /* Ignore for P2P Device */
5429 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
5430 return 0;
5431
5432 ifindex = if_nametoindex(ifname);
5433 wpa_printf(MSG_DEBUG, "%s: ifindex=%d (%s) alg=%d addr=%p key_idx=%d "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005434 "set_tx=%d seq_len=%lu key_len=%lu",
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005435 __func__, ifindex, ifname, alg, addr, key_idx, set_tx,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005436 (unsigned long) seq_len, (unsigned long) key_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005437#ifdef CONFIG_TDLS
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07005438 if (key_idx == -1) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005439 key_idx = 0;
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07005440 tdls = 1;
5441 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005442#endif /* CONFIG_TDLS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005443
5444 msg = nlmsg_alloc();
5445 if (!msg)
5446 return -ENOMEM;
5447
5448 if (alg == WPA_ALG_NONE) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005449 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_KEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005450 } else {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005451 nl80211_cmd(drv, msg, 0, NL80211_CMD_NEW_KEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005452 NLA_PUT(msg, NL80211_ATTR_KEY_DATA, key_len, key);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005453 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
5454 wpa_alg_to_cipher_suite(alg, key_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005455 }
5456
5457 if (seq && seq_len)
5458 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, seq_len, seq);
5459
5460 if (addr && !is_broadcast_ether_addr(addr)) {
5461 wpa_printf(MSG_DEBUG, " addr=" MACSTR, MAC2STR(addr));
5462 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
5463
5464 if (alg != WPA_ALG_WEP && key_idx && !set_tx) {
5465 wpa_printf(MSG_DEBUG, " RSN IBSS RX GTK");
5466 NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE,
5467 NL80211_KEYTYPE_GROUP);
5468 }
5469 } else if (addr && is_broadcast_ether_addr(addr)) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005470 struct nlattr *types;
5471
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005472 wpa_printf(MSG_DEBUG, " broadcast key");
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005473
5474 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005475 if (!types)
5476 goto nla_put_failure;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005477 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST);
5478 nla_nest_end(msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005479 }
5480 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
5481 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
5482
5483 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5484 if ((ret == -ENOENT || ret == -ENOLINK) && alg == WPA_ALG_NONE)
5485 ret = 0;
5486 if (ret)
5487 wpa_printf(MSG_DEBUG, "nl80211: set_key failed; err=%d %s)",
5488 ret, strerror(-ret));
5489
5490 /*
5491 * If we failed or don't need to set the default TX key (below),
5492 * we're done here.
5493 */
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07005494 if (ret || !set_tx || alg == WPA_ALG_NONE || tdls)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005495 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005496 if (is_ap_interface(drv->nlmode) && addr &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005497 !is_broadcast_ether_addr(addr))
5498 return ret;
5499
5500 msg = nlmsg_alloc();
5501 if (!msg)
5502 return -ENOMEM;
5503
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005504 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_KEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005505 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
5506 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
5507 if (alg == WPA_ALG_IGTK)
5508 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT_MGMT);
5509 else
5510 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT);
5511 if (addr && is_broadcast_ether_addr(addr)) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005512 struct nlattr *types;
5513
5514 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005515 if (!types)
5516 goto nla_put_failure;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005517 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST);
5518 nla_nest_end(msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005519 } else if (addr) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005520 struct nlattr *types;
5521
5522 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005523 if (!types)
5524 goto nla_put_failure;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005525 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_UNICAST);
5526 nla_nest_end(msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005527 }
5528
5529 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5530 if (ret == -ENOENT)
5531 ret = 0;
5532 if (ret)
5533 wpa_printf(MSG_DEBUG, "nl80211: set_key default failed; "
5534 "err=%d %s)", ret, strerror(-ret));
5535 return ret;
5536
5537nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005538 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005539 return -ENOBUFS;
5540}
5541
5542
5543static int nl_add_key(struct nl_msg *msg, enum wpa_alg alg,
5544 int key_idx, int defkey,
5545 const u8 *seq, size_t seq_len,
5546 const u8 *key, size_t key_len)
5547{
5548 struct nlattr *key_attr = nla_nest_start(msg, NL80211_ATTR_KEY);
5549 if (!key_attr)
5550 return -1;
5551
5552 if (defkey && alg == WPA_ALG_IGTK)
5553 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_MGMT);
5554 else if (defkey)
5555 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
5556
5557 NLA_PUT_U8(msg, NL80211_KEY_IDX, key_idx);
5558
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005559 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
5560 wpa_alg_to_cipher_suite(alg, key_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005561
5562 if (seq && seq_len)
5563 NLA_PUT(msg, NL80211_KEY_SEQ, seq_len, seq);
5564
5565 NLA_PUT(msg, NL80211_KEY_DATA, key_len, key);
5566
5567 nla_nest_end(msg, key_attr);
5568
5569 return 0;
5570 nla_put_failure:
5571 return -1;
5572}
5573
5574
5575static int nl80211_set_conn_keys(struct wpa_driver_associate_params *params,
5576 struct nl_msg *msg)
5577{
5578 int i, privacy = 0;
5579 struct nlattr *nl_keys, *nl_key;
5580
5581 for (i = 0; i < 4; i++) {
5582 if (!params->wep_key[i])
5583 continue;
5584 privacy = 1;
5585 break;
5586 }
5587 if (params->wps == WPS_MODE_PRIVACY)
5588 privacy = 1;
5589 if (params->pairwise_suite &&
5590 params->pairwise_suite != WPA_CIPHER_NONE)
5591 privacy = 1;
5592
5593 if (!privacy)
5594 return 0;
5595
5596 NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
5597
5598 nl_keys = nla_nest_start(msg, NL80211_ATTR_KEYS);
5599 if (!nl_keys)
5600 goto nla_put_failure;
5601
5602 for (i = 0; i < 4; i++) {
5603 if (!params->wep_key[i])
5604 continue;
5605
5606 nl_key = nla_nest_start(msg, i);
5607 if (!nl_key)
5608 goto nla_put_failure;
5609
5610 NLA_PUT(msg, NL80211_KEY_DATA, params->wep_key_len[i],
5611 params->wep_key[i]);
5612 if (params->wep_key_len[i] == 5)
5613 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
5614 WLAN_CIPHER_SUITE_WEP40);
5615 else
5616 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
5617 WLAN_CIPHER_SUITE_WEP104);
5618
5619 NLA_PUT_U8(msg, NL80211_KEY_IDX, i);
5620
5621 if (i == params->wep_tx_keyidx)
5622 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
5623
5624 nla_nest_end(msg, nl_key);
5625 }
5626 nla_nest_end(msg, nl_keys);
5627
5628 return 0;
5629
5630nla_put_failure:
5631 return -ENOBUFS;
5632}
5633
5634
5635static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
5636 const u8 *addr, int cmd, u16 reason_code,
5637 int local_state_change)
5638{
5639 int ret = -1;
5640 struct nl_msg *msg;
5641
5642 msg = nlmsg_alloc();
5643 if (!msg)
5644 return -1;
5645
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005646 nl80211_cmd(drv, msg, 0, cmd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005647
5648 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
5649 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason_code);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005650 if (addr)
5651 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005652 if (local_state_change)
5653 NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE);
5654
5655 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5656 msg = NULL;
5657 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005658 wpa_dbg(drv->ctx, MSG_DEBUG,
5659 "nl80211: MLME command failed: reason=%u ret=%d (%s)",
5660 reason_code, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005661 goto nla_put_failure;
5662 }
5663 ret = 0;
5664
5665nla_put_failure:
5666 nlmsg_free(msg);
5667 return ret;
5668}
5669
5670
5671static int wpa_driver_nl80211_disconnect(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005672 int reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005673{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005674 int ret;
5675
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005676 wpa_printf(MSG_DEBUG, "%s(reason_code=%d)", __func__, reason_code);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005677 nl80211_mark_disconnected(drv);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005678 /* Disconnect command doesn't need BSSID - it uses cached value */
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005679 ret = wpa_driver_nl80211_mlme(drv, NULL, NL80211_CMD_DISCONNECT,
5680 reason_code, 0);
5681 /*
5682 * For locally generated disconnect, supplicant already generates a
5683 * DEAUTH event, so ignore the event from NL80211.
5684 */
5685 drv->ignore_next_local_disconnect = ret == 0;
5686
5687 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005688}
5689
5690
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005691static int wpa_driver_nl80211_deauthenticate(struct i802_bss *bss,
5692 const u8 *addr, int reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005693{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005694 struct wpa_driver_nl80211_data *drv = bss->drv;
5695 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME))
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005696 return wpa_driver_nl80211_disconnect(drv, reason_code);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005697 wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " reason_code=%d)",
5698 __func__, MAC2STR(addr), reason_code);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005699 nl80211_mark_disconnected(drv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005700 if (drv->nlmode == NL80211_IFTYPE_ADHOC)
5701 return nl80211_leave_ibss(drv);
5702 return wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DEAUTHENTICATE,
5703 reason_code, 0);
5704}
5705
5706
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005707static void nl80211_copy_auth_params(struct wpa_driver_nl80211_data *drv,
5708 struct wpa_driver_auth_params *params)
5709{
5710 int i;
5711
5712 drv->auth_freq = params->freq;
5713 drv->auth_alg = params->auth_alg;
5714 drv->auth_wep_tx_keyidx = params->wep_tx_keyidx;
5715 drv->auth_local_state_change = params->local_state_change;
5716 drv->auth_p2p = params->p2p;
5717
5718 if (params->bssid)
5719 os_memcpy(drv->auth_bssid_, params->bssid, ETH_ALEN);
5720 else
5721 os_memset(drv->auth_bssid_, 0, ETH_ALEN);
5722
5723 if (params->ssid) {
5724 os_memcpy(drv->auth_ssid, params->ssid, params->ssid_len);
5725 drv->auth_ssid_len = params->ssid_len;
5726 } else
5727 drv->auth_ssid_len = 0;
5728
5729
5730 os_free(drv->auth_ie);
5731 drv->auth_ie = NULL;
5732 drv->auth_ie_len = 0;
5733 if (params->ie) {
5734 drv->auth_ie = os_malloc(params->ie_len);
5735 if (drv->auth_ie) {
5736 os_memcpy(drv->auth_ie, params->ie, params->ie_len);
5737 drv->auth_ie_len = params->ie_len;
5738 }
5739 }
5740
5741 for (i = 0; i < 4; i++) {
5742 if (params->wep_key[i] && params->wep_key_len[i] &&
5743 params->wep_key_len[i] <= 16) {
5744 os_memcpy(drv->auth_wep_key[i], params->wep_key[i],
5745 params->wep_key_len[i]);
5746 drv->auth_wep_key_len[i] = params->wep_key_len[i];
5747 } else
5748 drv->auth_wep_key_len[i] = 0;
5749 }
5750}
5751
5752
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005753static int wpa_driver_nl80211_authenticate(
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005754 struct i802_bss *bss, struct wpa_driver_auth_params *params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005755{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005756 struct wpa_driver_nl80211_data *drv = bss->drv;
5757 int ret = -1, i;
5758 struct nl_msg *msg;
5759 enum nl80211_auth_type type;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005760 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005761 int count = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005762 int is_retry;
5763
5764 is_retry = drv->retry_auth;
5765 drv->retry_auth = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005766
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005767 nl80211_mark_disconnected(drv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005768 os_memset(drv->auth_bssid, 0, ETH_ALEN);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005769 if (params->bssid)
5770 os_memcpy(drv->auth_attempt_bssid, params->bssid, ETH_ALEN);
5771 else
5772 os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005773 /* FIX: IBSS mode */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005774 nlmode = params->p2p ?
5775 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
5776 if (drv->nlmode != nlmode &&
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005777 wpa_driver_nl80211_set_mode(bss, nlmode) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005778 return -1;
5779
5780retry:
5781 msg = nlmsg_alloc();
5782 if (!msg)
5783 return -1;
5784
5785 wpa_printf(MSG_DEBUG, "nl80211: Authenticate (ifindex=%d)",
5786 drv->ifindex);
5787
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005788 nl80211_cmd(drv, msg, 0, NL80211_CMD_AUTHENTICATE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005789
5790 for (i = 0; i < 4; i++) {
5791 if (!params->wep_key[i])
5792 continue;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005793 wpa_driver_nl80211_set_key(bss->ifname, bss, WPA_ALG_WEP,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005794 NULL, i,
5795 i == params->wep_tx_keyidx, NULL, 0,
5796 params->wep_key[i],
5797 params->wep_key_len[i]);
5798 if (params->wep_tx_keyidx != i)
5799 continue;
5800 if (nl_add_key(msg, WPA_ALG_WEP, i, 1, NULL, 0,
5801 params->wep_key[i], params->wep_key_len[i])) {
5802 nlmsg_free(msg);
5803 return -1;
5804 }
5805 }
5806
5807 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
5808 if (params->bssid) {
5809 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
5810 MAC2STR(params->bssid));
5811 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
5812 }
5813 if (params->freq) {
5814 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
5815 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
5816 }
5817 if (params->ssid) {
5818 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
5819 params->ssid, params->ssid_len);
5820 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
5821 params->ssid);
5822 }
5823 wpa_hexdump(MSG_DEBUG, " * IEs", params->ie, params->ie_len);
5824 if (params->ie)
5825 NLA_PUT(msg, NL80211_ATTR_IE, params->ie_len, params->ie);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005826 if (params->sae_data) {
5827 wpa_hexdump(MSG_DEBUG, " * SAE data", params->sae_data,
5828 params->sae_data_len);
5829 NLA_PUT(msg, NL80211_ATTR_SAE_DATA, params->sae_data_len,
5830 params->sae_data);
5831 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005832 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
5833 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
5834 else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
5835 type = NL80211_AUTHTYPE_SHARED_KEY;
5836 else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
5837 type = NL80211_AUTHTYPE_NETWORK_EAP;
5838 else if (params->auth_alg & WPA_AUTH_ALG_FT)
5839 type = NL80211_AUTHTYPE_FT;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005840 else if (params->auth_alg & WPA_AUTH_ALG_SAE)
5841 type = NL80211_AUTHTYPE_SAE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005842 else
5843 goto nla_put_failure;
5844 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
5845 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
5846 if (params->local_state_change) {
5847 wpa_printf(MSG_DEBUG, " * Local state change only");
5848 NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE);
5849 }
5850
5851 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5852 msg = NULL;
5853 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005854 wpa_dbg(drv->ctx, MSG_DEBUG,
5855 "nl80211: MLME command failed (auth): ret=%d (%s)",
5856 ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005857 count++;
5858 if (ret == -EALREADY && count == 1 && params->bssid &&
5859 !params->local_state_change) {
5860 /*
5861 * mac80211 does not currently accept new
5862 * authentication if we are already authenticated. As a
5863 * workaround, force deauthentication and try again.
5864 */
5865 wpa_printf(MSG_DEBUG, "nl80211: Retry authentication "
5866 "after forced deauthentication");
5867 wpa_driver_nl80211_deauthenticate(
5868 bss, params->bssid,
5869 WLAN_REASON_PREV_AUTH_NOT_VALID);
5870 nlmsg_free(msg);
5871 goto retry;
5872 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005873
5874 if (ret == -ENOENT && params->freq && !is_retry) {
5875 /*
5876 * cfg80211 has likely expired the BSS entry even
5877 * though it was previously available in our internal
5878 * BSS table. To recover quickly, start a single
5879 * channel scan on the specified channel.
5880 */
5881 struct wpa_driver_scan_params scan;
5882 int freqs[2];
5883
5884 os_memset(&scan, 0, sizeof(scan));
5885 scan.num_ssids = 1;
5886 if (params->ssid) {
5887 scan.ssids[0].ssid = params->ssid;
5888 scan.ssids[0].ssid_len = params->ssid_len;
5889 }
5890 freqs[0] = params->freq;
5891 freqs[1] = 0;
5892 scan.freqs = freqs;
5893 wpa_printf(MSG_DEBUG, "nl80211: Trigger single "
5894 "channel scan to refresh cfg80211 BSS "
5895 "entry");
5896 ret = wpa_driver_nl80211_scan(bss, &scan);
5897 if (ret == 0) {
5898 nl80211_copy_auth_params(drv, params);
5899 drv->scan_for_auth = 1;
5900 }
5901 } else if (is_retry) {
5902 /*
5903 * Need to indicate this with an event since the return
5904 * value from the retry is not delivered to core code.
5905 */
5906 union wpa_event_data event;
5907 wpa_printf(MSG_DEBUG, "nl80211: Authentication retry "
5908 "failed");
5909 os_memset(&event, 0, sizeof(event));
5910 os_memcpy(event.timeout_event.addr, drv->auth_bssid_,
5911 ETH_ALEN);
5912 wpa_supplicant_event(drv->ctx, EVENT_AUTH_TIMED_OUT,
5913 &event);
5914 }
5915
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005916 goto nla_put_failure;
5917 }
5918 ret = 0;
5919 wpa_printf(MSG_DEBUG, "nl80211: Authentication request send "
5920 "successfully");
5921
5922nla_put_failure:
5923 nlmsg_free(msg);
5924 return ret;
5925}
5926
5927
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005928static int wpa_driver_nl80211_authenticate_retry(
5929 struct wpa_driver_nl80211_data *drv)
5930{
5931 struct wpa_driver_auth_params params;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005932 struct i802_bss *bss = drv->first_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005933 int i;
5934
5935 wpa_printf(MSG_DEBUG, "nl80211: Try to authenticate again");
5936
5937 os_memset(&params, 0, sizeof(params));
5938 params.freq = drv->auth_freq;
5939 params.auth_alg = drv->auth_alg;
5940 params.wep_tx_keyidx = drv->auth_wep_tx_keyidx;
5941 params.local_state_change = drv->auth_local_state_change;
5942 params.p2p = drv->auth_p2p;
5943
5944 if (!is_zero_ether_addr(drv->auth_bssid_))
5945 params.bssid = drv->auth_bssid_;
5946
5947 if (drv->auth_ssid_len) {
5948 params.ssid = drv->auth_ssid;
5949 params.ssid_len = drv->auth_ssid_len;
5950 }
5951
5952 params.ie = drv->auth_ie;
5953 params.ie_len = drv->auth_ie_len;
5954
5955 for (i = 0; i < 4; i++) {
5956 if (drv->auth_wep_key_len[i]) {
5957 params.wep_key[i] = drv->auth_wep_key[i];
5958 params.wep_key_len[i] = drv->auth_wep_key_len[i];
5959 }
5960 }
5961
5962 drv->retry_auth = 1;
5963 return wpa_driver_nl80211_authenticate(bss, &params);
5964}
5965
5966
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005967struct phy_info_arg {
5968 u16 *num_modes;
5969 struct hostapd_hw_modes *modes;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005970 int last_mode, last_chan_idx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005971};
5972
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005973static void phy_info_ht_capa(struct hostapd_hw_modes *mode, struct nlattr *capa,
5974 struct nlattr *ampdu_factor,
5975 struct nlattr *ampdu_density,
5976 struct nlattr *mcs_set)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005977{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005978 if (capa)
5979 mode->ht_capab = nla_get_u16(capa);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005980
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005981 if (ampdu_factor)
5982 mode->a_mpdu_params |= nla_get_u8(ampdu_factor) & 0x03;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005983
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005984 if (ampdu_density)
5985 mode->a_mpdu_params |= nla_get_u8(ampdu_density) << 2;
5986
5987 if (mcs_set && nla_len(mcs_set) >= 16) {
5988 u8 *mcs;
5989 mcs = nla_data(mcs_set);
5990 os_memcpy(mode->mcs_set, mcs, 16);
5991 }
5992}
5993
5994
5995static void phy_info_vht_capa(struct hostapd_hw_modes *mode,
5996 struct nlattr *capa,
5997 struct nlattr *mcs_set)
5998{
5999 if (capa)
6000 mode->vht_capab = nla_get_u32(capa);
6001
6002 if (mcs_set && nla_len(mcs_set) >= 8) {
6003 u8 *mcs;
6004 mcs = nla_data(mcs_set);
6005 os_memcpy(mode->vht_mcs_set, mcs, 8);
6006 }
6007}
6008
6009
6010static void phy_info_freq(struct hostapd_hw_modes *mode,
6011 struct hostapd_channel_data *chan,
6012 struct nlattr *tb_freq[])
6013{
Dmitry Shmidt4b060592013-04-29 16:42:49 -07006014 u8 channel;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006015 chan->freq = nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_FREQ]);
6016 chan->flag = 0;
Dmitry Shmidt4b060592013-04-29 16:42:49 -07006017 if (ieee80211_freq_to_chan(chan->freq, &channel) != NUM_HOSTAPD_MODES)
6018 chan->chan = channel;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006019
6020 if (tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
6021 chan->flag |= HOSTAPD_CHAN_DISABLED;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006022 if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IR])
6023 chan->flag |= HOSTAPD_CHAN_PASSIVE_SCAN | HOSTAPD_CHAN_NO_IBSS;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006024 if (tb_freq[NL80211_FREQUENCY_ATTR_RADAR])
6025 chan->flag |= HOSTAPD_CHAN_RADAR;
6026
Dmitry Shmidtea69e842013-05-13 14:52:28 -07006027 if (tb_freq[NL80211_FREQUENCY_ATTR_DFS_STATE]) {
6028 enum nl80211_dfs_state state =
6029 nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_DFS_STATE]);
6030
6031 switch (state) {
6032 case NL80211_DFS_USABLE:
6033 chan->flag |= HOSTAPD_CHAN_DFS_USABLE;
6034 break;
6035 case NL80211_DFS_AVAILABLE:
6036 chan->flag |= HOSTAPD_CHAN_DFS_AVAILABLE;
6037 break;
6038 case NL80211_DFS_UNAVAILABLE:
6039 chan->flag |= HOSTAPD_CHAN_DFS_UNAVAILABLE;
6040 break;
6041 }
6042 }
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006043}
6044
6045
6046static int phy_info_freqs(struct phy_info_arg *phy_info,
6047 struct hostapd_hw_modes *mode, struct nlattr *tb)
6048{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006049 static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
6050 [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
6051 [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006052 [NL80211_FREQUENCY_ATTR_NO_IR] = { .type = NLA_FLAG },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006053 [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
6054 [NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32 },
Dmitry Shmidtea69e842013-05-13 14:52:28 -07006055 [NL80211_FREQUENCY_ATTR_DFS_STATE] = { .type = NLA_U32 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006056 };
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006057 int new_channels = 0;
6058 struct hostapd_channel_data *channel;
6059 struct nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006060 struct nlattr *nl_freq;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006061 int rem_freq, idx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006062
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006063 if (tb == NULL)
6064 return NL_OK;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006065
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006066 nla_for_each_nested(nl_freq, tb, rem_freq) {
6067 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX,
6068 nla_data(nl_freq), nla_len(nl_freq), freq_policy);
6069 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
6070 continue;
6071 new_channels++;
6072 }
6073
6074 channel = os_realloc_array(mode->channels,
6075 mode->num_channels + new_channels,
6076 sizeof(struct hostapd_channel_data));
6077 if (!channel)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006078 return NL_SKIP;
6079
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006080 mode->channels = channel;
6081 mode->num_channels += new_channels;
6082
6083 idx = phy_info->last_chan_idx;
6084
6085 nla_for_each_nested(nl_freq, tb, rem_freq) {
6086 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX,
6087 nla_data(nl_freq), nla_len(nl_freq), freq_policy);
6088 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
6089 continue;
6090 phy_info_freq(mode, &mode->channels[idx], tb_freq);
6091 idx++;
6092 }
6093 phy_info->last_chan_idx = idx;
6094
6095 return NL_OK;
6096}
6097
6098
6099static int phy_info_rates(struct hostapd_hw_modes *mode, struct nlattr *tb)
6100{
6101 static struct nla_policy rate_policy[NL80211_BITRATE_ATTR_MAX + 1] = {
6102 [NL80211_BITRATE_ATTR_RATE] = { .type = NLA_U32 },
6103 [NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE] =
6104 { .type = NLA_FLAG },
6105 };
6106 struct nlattr *tb_rate[NL80211_BITRATE_ATTR_MAX + 1];
6107 struct nlattr *nl_rate;
6108 int rem_rate, idx;
6109
6110 if (tb == NULL)
6111 return NL_OK;
6112
6113 nla_for_each_nested(nl_rate, tb, rem_rate) {
6114 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX,
6115 nla_data(nl_rate), nla_len(nl_rate),
6116 rate_policy);
6117 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
6118 continue;
6119 mode->num_rates++;
6120 }
6121
6122 mode->rates = os_calloc(mode->num_rates, sizeof(int));
6123 if (!mode->rates)
6124 return NL_SKIP;
6125
6126 idx = 0;
6127
6128 nla_for_each_nested(nl_rate, tb, rem_rate) {
6129 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX,
6130 nla_data(nl_rate), nla_len(nl_rate),
6131 rate_policy);
6132 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
6133 continue;
6134 mode->rates[idx] = nla_get_u32(
6135 tb_rate[NL80211_BITRATE_ATTR_RATE]);
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006136 idx++;
6137 }
6138
6139 return NL_OK;
6140}
6141
6142
6143static int phy_info_band(struct phy_info_arg *phy_info, struct nlattr *nl_band)
6144{
6145 struct nlattr *tb_band[NL80211_BAND_ATTR_MAX + 1];
6146 struct hostapd_hw_modes *mode;
6147 int ret;
6148
6149 if (phy_info->last_mode != nl_band->nla_type) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006150 mode = os_realloc_array(phy_info->modes,
6151 *phy_info->num_modes + 1,
6152 sizeof(*mode));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006153 if (!mode)
6154 return NL_SKIP;
6155 phy_info->modes = mode;
6156
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006157 mode = &phy_info->modes[*(phy_info->num_modes)];
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006158 os_memset(mode, 0, sizeof(*mode));
6159 mode->mode = NUM_HOSTAPD_MODES;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006160 mode->flags = HOSTAPD_MODE_FLAG_HT_INFO_KNOWN |
6161 HOSTAPD_MODE_FLAG_VHT_INFO_KNOWN;
6162
6163 /*
6164 * Unsupported VHT MCS stream is defined as value 3, so the VHT
6165 * MCS RX/TX map must be initialized with 0xffff to mark all 8
6166 * possible streams as unsupported. This will be overridden if
6167 * driver advertises VHT support.
6168 */
6169 mode->vht_mcs_set[0] = 0xff;
6170 mode->vht_mcs_set[1] = 0xff;
6171 mode->vht_mcs_set[4] = 0xff;
6172 mode->vht_mcs_set[5] = 0xff;
6173
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006174 *(phy_info->num_modes) += 1;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006175 phy_info->last_mode = nl_band->nla_type;
6176 phy_info->last_chan_idx = 0;
6177 } else
6178 mode = &phy_info->modes[*(phy_info->num_modes) - 1];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006179
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006180 nla_parse(tb_band, NL80211_BAND_ATTR_MAX, nla_data(nl_band),
6181 nla_len(nl_band), NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006182
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006183 phy_info_ht_capa(mode, tb_band[NL80211_BAND_ATTR_HT_CAPA],
6184 tb_band[NL80211_BAND_ATTR_HT_AMPDU_FACTOR],
6185 tb_band[NL80211_BAND_ATTR_HT_AMPDU_DENSITY],
6186 tb_band[NL80211_BAND_ATTR_HT_MCS_SET]);
6187 phy_info_vht_capa(mode, tb_band[NL80211_BAND_ATTR_VHT_CAPA],
6188 tb_band[NL80211_BAND_ATTR_VHT_MCS_SET]);
6189 ret = phy_info_freqs(phy_info, mode, tb_band[NL80211_BAND_ATTR_FREQS]);
6190 if (ret != NL_OK)
6191 return ret;
6192 ret = phy_info_rates(mode, tb_band[NL80211_BAND_ATTR_RATES]);
6193 if (ret != NL_OK)
6194 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006195
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006196 return NL_OK;
6197}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006198
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006199
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006200static int phy_info_handler(struct nl_msg *msg, void *arg)
6201{
6202 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
6203 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6204 struct phy_info_arg *phy_info = arg;
6205 struct nlattr *nl_band;
6206 int rem_band;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006207
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006208 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6209 genlmsg_attrlen(gnlh, 0), NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006210
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006211 if (!tb_msg[NL80211_ATTR_WIPHY_BANDS])
6212 return NL_SKIP;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006213
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006214 nla_for_each_nested(nl_band, tb_msg[NL80211_ATTR_WIPHY_BANDS], rem_band)
6215 {
6216 int res = phy_info_band(phy_info, nl_band);
6217 if (res != NL_OK)
6218 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006219 }
6220
6221 return NL_SKIP;
6222}
6223
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006224
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006225static struct hostapd_hw_modes *
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07006226wpa_driver_nl80211_postprocess_modes(struct hostapd_hw_modes *modes,
6227 u16 *num_modes)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006228{
6229 u16 m;
6230 struct hostapd_hw_modes *mode11g = NULL, *nmodes, *mode;
6231 int i, mode11g_idx = -1;
6232
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07006233 /* heuristic to set up modes */
6234 for (m = 0; m < *num_modes; m++) {
6235 if (!modes[m].num_channels)
6236 continue;
6237 if (modes[m].channels[0].freq < 4000) {
6238 modes[m].mode = HOSTAPD_MODE_IEEE80211B;
6239 for (i = 0; i < modes[m].num_rates; i++) {
6240 if (modes[m].rates[i] > 200) {
6241 modes[m].mode = HOSTAPD_MODE_IEEE80211G;
6242 break;
6243 }
6244 }
6245 } else if (modes[m].channels[0].freq > 50000)
6246 modes[m].mode = HOSTAPD_MODE_IEEE80211AD;
6247 else
6248 modes[m].mode = HOSTAPD_MODE_IEEE80211A;
6249 }
6250
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006251 /* If only 802.11g mode is included, use it to construct matching
6252 * 802.11b mode data. */
6253
6254 for (m = 0; m < *num_modes; m++) {
6255 if (modes[m].mode == HOSTAPD_MODE_IEEE80211B)
6256 return modes; /* 802.11b already included */
6257 if (modes[m].mode == HOSTAPD_MODE_IEEE80211G)
6258 mode11g_idx = m;
6259 }
6260
6261 if (mode11g_idx < 0)
6262 return modes; /* 2.4 GHz band not supported at all */
6263
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006264 nmodes = os_realloc_array(modes, *num_modes + 1, sizeof(*nmodes));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006265 if (nmodes == NULL)
6266 return modes; /* Could not add 802.11b mode */
6267
6268 mode = &nmodes[*num_modes];
6269 os_memset(mode, 0, sizeof(*mode));
6270 (*num_modes)++;
6271 modes = nmodes;
6272
6273 mode->mode = HOSTAPD_MODE_IEEE80211B;
6274
6275 mode11g = &modes[mode11g_idx];
6276 mode->num_channels = mode11g->num_channels;
6277 mode->channels = os_malloc(mode11g->num_channels *
6278 sizeof(struct hostapd_channel_data));
6279 if (mode->channels == NULL) {
6280 (*num_modes)--;
6281 return modes; /* Could not add 802.11b mode */
6282 }
6283 os_memcpy(mode->channels, mode11g->channels,
6284 mode11g->num_channels * sizeof(struct hostapd_channel_data));
6285
6286 mode->num_rates = 0;
6287 mode->rates = os_malloc(4 * sizeof(int));
6288 if (mode->rates == NULL) {
6289 os_free(mode->channels);
6290 (*num_modes)--;
6291 return modes; /* Could not add 802.11b mode */
6292 }
6293
6294 for (i = 0; i < mode11g->num_rates; i++) {
6295 if (mode11g->rates[i] != 10 && mode11g->rates[i] != 20 &&
6296 mode11g->rates[i] != 55 && mode11g->rates[i] != 110)
6297 continue;
6298 mode->rates[mode->num_rates] = mode11g->rates[i];
6299 mode->num_rates++;
6300 if (mode->num_rates == 4)
6301 break;
6302 }
6303
6304 if (mode->num_rates == 0) {
6305 os_free(mode->channels);
6306 os_free(mode->rates);
6307 (*num_modes)--;
6308 return modes; /* No 802.11b rates */
6309 }
6310
6311 wpa_printf(MSG_DEBUG, "nl80211: Added 802.11b mode based on 802.11g "
6312 "information");
6313
6314 return modes;
6315}
6316
6317
6318static void nl80211_set_ht40_mode(struct hostapd_hw_modes *mode, int start,
6319 int end)
6320{
6321 int c;
6322
6323 for (c = 0; c < mode->num_channels; c++) {
6324 struct hostapd_channel_data *chan = &mode->channels[c];
6325 if (chan->freq - 10 >= start && chan->freq + 10 <= end)
6326 chan->flag |= HOSTAPD_CHAN_HT40;
6327 }
6328}
6329
6330
6331static void nl80211_set_ht40_mode_sec(struct hostapd_hw_modes *mode, int start,
6332 int end)
6333{
6334 int c;
6335
6336 for (c = 0; c < mode->num_channels; c++) {
6337 struct hostapd_channel_data *chan = &mode->channels[c];
6338 if (!(chan->flag & HOSTAPD_CHAN_HT40))
6339 continue;
6340 if (chan->freq - 30 >= start && chan->freq - 10 <= end)
6341 chan->flag |= HOSTAPD_CHAN_HT40MINUS;
6342 if (chan->freq + 10 >= start && chan->freq + 30 <= end)
6343 chan->flag |= HOSTAPD_CHAN_HT40PLUS;
6344 }
6345}
6346
6347
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006348static void nl80211_reg_rule_max_eirp(u32 start, u32 end, u32 max_eirp,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006349 struct phy_info_arg *results)
6350{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006351 u16 m;
6352
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006353 for (m = 0; m < *results->num_modes; m++) {
6354 int c;
6355 struct hostapd_hw_modes *mode = &results->modes[m];
6356
6357 for (c = 0; c < mode->num_channels; c++) {
6358 struct hostapd_channel_data *chan = &mode->channels[c];
6359 if ((u32) chan->freq - 10 >= start &&
6360 (u32) chan->freq + 10 <= end)
6361 chan->max_tx_power = max_eirp;
6362 }
6363 }
6364}
6365
6366
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006367static void nl80211_reg_rule_ht40(u32 start, u32 end,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006368 struct phy_info_arg *results)
6369{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006370 u16 m;
6371
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006372 for (m = 0; m < *results->num_modes; m++) {
6373 if (!(results->modes[m].ht_capab &
6374 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
6375 continue;
6376 nl80211_set_ht40_mode(&results->modes[m], start, end);
6377 }
6378}
6379
6380
6381static void nl80211_reg_rule_sec(struct nlattr *tb[],
6382 struct phy_info_arg *results)
6383{
6384 u32 start, end, max_bw;
6385 u16 m;
6386
6387 if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
6388 tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
6389 tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
6390 return;
6391
6392 start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
6393 end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
6394 max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
6395
6396 if (max_bw < 20)
6397 return;
6398
6399 for (m = 0; m < *results->num_modes; m++) {
6400 if (!(results->modes[m].ht_capab &
6401 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
6402 continue;
6403 nl80211_set_ht40_mode_sec(&results->modes[m], start, end);
6404 }
6405}
6406
6407
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006408static void nl80211_set_vht_mode(struct hostapd_hw_modes *mode, int start,
6409 int end)
6410{
6411 int c;
6412
6413 for (c = 0; c < mode->num_channels; c++) {
6414 struct hostapd_channel_data *chan = &mode->channels[c];
6415 if (chan->freq - 10 >= start && chan->freq + 70 <= end)
6416 chan->flag |= HOSTAPD_CHAN_VHT_10_70;
6417
6418 if (chan->freq - 30 >= start && chan->freq + 50 <= end)
6419 chan->flag |= HOSTAPD_CHAN_VHT_30_50;
6420
6421 if (chan->freq - 50 >= start && chan->freq + 30 <= end)
6422 chan->flag |= HOSTAPD_CHAN_VHT_50_30;
6423
6424 if (chan->freq - 70 >= start && chan->freq + 10 <= end)
6425 chan->flag |= HOSTAPD_CHAN_VHT_70_10;
6426 }
6427}
6428
6429
6430static void nl80211_reg_rule_vht(struct nlattr *tb[],
6431 struct phy_info_arg *results)
6432{
6433 u32 start, end, max_bw;
6434 u16 m;
6435
6436 if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
6437 tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
6438 tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
6439 return;
6440
6441 start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
6442 end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
6443 max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
6444
6445 if (max_bw < 80)
6446 return;
6447
6448 for (m = 0; m < *results->num_modes; m++) {
6449 if (!(results->modes[m].ht_capab &
6450 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
6451 continue;
6452 /* TODO: use a real VHT support indication */
6453 if (!results->modes[m].vht_capab)
6454 continue;
6455
6456 nl80211_set_vht_mode(&results->modes[m], start, end);
6457 }
6458}
6459
6460
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006461static int nl80211_get_reg(struct nl_msg *msg, void *arg)
6462{
6463 struct phy_info_arg *results = arg;
6464 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
6465 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6466 struct nlattr *nl_rule;
6467 struct nlattr *tb_rule[NL80211_FREQUENCY_ATTR_MAX + 1];
6468 int rem_rule;
6469 static struct nla_policy reg_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
6470 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
6471 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
6472 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
6473 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
6474 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
6475 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
6476 };
6477
6478 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6479 genlmsg_attrlen(gnlh, 0), NULL);
6480 if (!tb_msg[NL80211_ATTR_REG_ALPHA2] ||
6481 !tb_msg[NL80211_ATTR_REG_RULES]) {
6482 wpa_printf(MSG_DEBUG, "nl80211: No regulatory information "
6483 "available");
6484 return NL_SKIP;
6485 }
6486
6487 wpa_printf(MSG_DEBUG, "nl80211: Regulatory information - country=%s",
6488 (char *) nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]));
6489
6490 nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
6491 {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006492 u32 start, end, max_eirp = 0, max_bw = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006493 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
6494 nla_data(nl_rule), nla_len(nl_rule), reg_policy);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006495 if (tb_rule[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
6496 tb_rule[NL80211_ATTR_FREQ_RANGE_END] == NULL)
6497 continue;
6498 start = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
6499 end = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
6500 if (tb_rule[NL80211_ATTR_POWER_RULE_MAX_EIRP])
6501 max_eirp = nla_get_u32(tb_rule[NL80211_ATTR_POWER_RULE_MAX_EIRP]) / 100;
6502 if (tb_rule[NL80211_ATTR_FREQ_RANGE_MAX_BW])
6503 max_bw = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
6504
6505 wpa_printf(MSG_DEBUG, "nl80211: %u-%u @ %u MHz %u mBm",
6506 start, end, max_bw, max_eirp);
6507 if (max_bw >= 40)
6508 nl80211_reg_rule_ht40(start, end, results);
6509 if (tb_rule[NL80211_ATTR_POWER_RULE_MAX_EIRP])
6510 nl80211_reg_rule_max_eirp(start, end, max_eirp,
6511 results);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006512 }
6513
6514 nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
6515 {
6516 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
6517 nla_data(nl_rule), nla_len(nl_rule), reg_policy);
6518 nl80211_reg_rule_sec(tb_rule, results);
6519 }
6520
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006521 nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
6522 {
6523 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
6524 nla_data(nl_rule), nla_len(nl_rule), reg_policy);
6525 nl80211_reg_rule_vht(tb_rule, results);
6526 }
6527
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006528 return NL_SKIP;
6529}
6530
6531
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006532static int nl80211_set_regulatory_flags(struct wpa_driver_nl80211_data *drv,
6533 struct phy_info_arg *results)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006534{
6535 struct nl_msg *msg;
6536
6537 msg = nlmsg_alloc();
6538 if (!msg)
6539 return -ENOMEM;
6540
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006541 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006542 return send_and_recv_msgs(drv, msg, nl80211_get_reg, results);
6543}
6544
6545
6546static struct hostapd_hw_modes *
6547wpa_driver_nl80211_get_hw_feature_data(void *priv, u16 *num_modes, u16 *flags)
6548{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006549 u32 feat;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006550 struct i802_bss *bss = priv;
6551 struct wpa_driver_nl80211_data *drv = bss->drv;
6552 struct nl_msg *msg;
6553 struct phy_info_arg result = {
6554 .num_modes = num_modes,
6555 .modes = NULL,
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006556 .last_mode = -1,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006557 };
6558
6559 *num_modes = 0;
6560 *flags = 0;
6561
6562 msg = nlmsg_alloc();
6563 if (!msg)
6564 return NULL;
6565
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006566 feat = get_nl80211_protocol_features(drv);
6567 if (feat & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP)
6568 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_WIPHY);
6569 else
6570 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006571
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006572 NLA_PUT_FLAG(msg, NL80211_ATTR_SPLIT_WIPHY_DUMP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006573 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
6574
6575 if (send_and_recv_msgs(drv, msg, phy_info_handler, &result) == 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006576 nl80211_set_regulatory_flags(drv, &result);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07006577 return wpa_driver_nl80211_postprocess_modes(result.modes,
6578 num_modes);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006579 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006580 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006581 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006582 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006583 return NULL;
6584}
6585
6586
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006587static int wpa_driver_nl80211_send_mntr(struct wpa_driver_nl80211_data *drv,
6588 const void *data, size_t len,
6589 int encrypt, int noack)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006590{
6591 __u8 rtap_hdr[] = {
6592 0x00, 0x00, /* radiotap version */
6593 0x0e, 0x00, /* radiotap length */
6594 0x02, 0xc0, 0x00, 0x00, /* bmap: flags, tx and rx flags */
6595 IEEE80211_RADIOTAP_F_FRAG, /* F_FRAG (fragment if required) */
6596 0x00, /* padding */
6597 0x00, 0x00, /* RX and TX flags to indicate that */
6598 0x00, 0x00, /* this is the injected frame directly */
6599 };
6600 struct iovec iov[2] = {
6601 {
6602 .iov_base = &rtap_hdr,
6603 .iov_len = sizeof(rtap_hdr),
6604 },
6605 {
6606 .iov_base = (void *) data,
6607 .iov_len = len,
6608 }
6609 };
6610 struct msghdr msg = {
6611 .msg_name = NULL,
6612 .msg_namelen = 0,
6613 .msg_iov = iov,
6614 .msg_iovlen = 2,
6615 .msg_control = NULL,
6616 .msg_controllen = 0,
6617 .msg_flags = 0,
6618 };
6619 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006620 u16 txflags = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006621
6622 if (encrypt)
6623 rtap_hdr[8] |= IEEE80211_RADIOTAP_F_WEP;
6624
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07006625 if (drv->monitor_sock < 0) {
6626 wpa_printf(MSG_DEBUG, "nl80211: No monitor socket available "
6627 "for %s", __func__);
6628 return -1;
6629 }
6630
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006631 if (noack)
6632 txflags |= IEEE80211_RADIOTAP_F_TX_NOACK;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006633 WPA_PUT_LE16(&rtap_hdr[12], txflags);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006634
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006635 res = sendmsg(drv->monitor_sock, &msg, 0);
6636 if (res < 0) {
6637 wpa_printf(MSG_INFO, "nl80211: sendmsg: %s", strerror(errno));
6638 return -1;
6639 }
6640 return 0;
6641}
6642
6643
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006644static int wpa_driver_nl80211_send_frame(struct i802_bss *bss,
6645 const void *data, size_t len,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006646 int encrypt, int noack,
6647 unsigned int freq, int no_cck,
6648 int offchanok, unsigned int wait_time)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006649{
6650 struct wpa_driver_nl80211_data *drv = bss->drv;
6651 u64 cookie;
Dmitry Shmidt051af732013-10-22 13:52:46 -07006652 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006653
Dmitry Shmidt56052862013-10-04 10:23:25 -07006654 if (freq == 0) {
6655 wpa_printf(MSG_DEBUG, "nl80211: send_frame - Use bss->freq=%u",
6656 bss->freq);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006657 freq = bss->freq;
Dmitry Shmidt56052862013-10-04 10:23:25 -07006658 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006659
Dmitry Shmidt56052862013-10-04 10:23:25 -07006660 if (drv->use_monitor) {
6661 wpa_printf(MSG_DEBUG, "nl80211: send_frame(freq=%u bss->freq=%u) -> send_mntr",
6662 freq, bss->freq);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006663 return wpa_driver_nl80211_send_mntr(drv, data, len,
6664 encrypt, noack);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006665 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006666
Dmitry Shmidt56052862013-10-04 10:23:25 -07006667 wpa_printf(MSG_DEBUG, "nl80211: send_frame -> send_frame_cmd");
Dmitry Shmidt051af732013-10-22 13:52:46 -07006668 res = nl80211_send_frame_cmd(bss, freq, wait_time, data, len,
6669 &cookie, no_cck, noack, offchanok);
6670 if (res == 0 && !noack) {
6671 const struct ieee80211_mgmt *mgmt;
6672 u16 fc;
6673
6674 mgmt = (const struct ieee80211_mgmt *) data;
6675 fc = le_to_host16(mgmt->frame_control);
6676 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
6677 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_ACTION) {
6678 wpa_printf(MSG_MSGDUMP,
6679 "nl80211: Update send_action_cookie from 0x%llx to 0x%llx",
6680 (long long unsigned int)
6681 drv->send_action_cookie,
6682 (long long unsigned int) cookie);
6683 drv->send_action_cookie = cookie;
6684 }
6685 }
6686
6687 return res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006688}
6689
6690
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006691static int wpa_driver_nl80211_send_mlme(struct i802_bss *bss, const u8 *data,
6692 size_t data_len, int noack,
6693 unsigned int freq, int no_cck,
6694 int offchanok,
6695 unsigned int wait_time)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006696{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006697 struct wpa_driver_nl80211_data *drv = bss->drv;
6698 struct ieee80211_mgmt *mgmt;
6699 int encrypt = 1;
6700 u16 fc;
6701
6702 mgmt = (struct ieee80211_mgmt *) data;
6703 fc = le_to_host16(mgmt->frame_control);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006704 wpa_printf(MSG_DEBUG, "nl80211: send_mlme - noack=%d freq=%u no_cck=%d offchanok=%d wait_time=%u fc=0x%x nlmode=%d",
6705 noack, freq, no_cck, offchanok, wait_time, fc, drv->nlmode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006706
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006707 if ((is_sta_interface(drv->nlmode) ||
6708 drv->nlmode == NL80211_IFTYPE_P2P_DEVICE) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006709 WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
6710 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_RESP) {
6711 /*
6712 * The use of last_mgmt_freq is a bit of a hack,
6713 * but it works due to the single-threaded nature
6714 * of wpa_supplicant.
6715 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07006716 if (freq == 0) {
6717 wpa_printf(MSG_DEBUG, "nl80211: Use last_mgmt_freq=%d",
6718 drv->last_mgmt_freq);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006719 freq = drv->last_mgmt_freq;
Dmitry Shmidt56052862013-10-04 10:23:25 -07006720 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006721 return nl80211_send_frame_cmd(bss, freq, 0,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006722 data, data_len, NULL, 1, noack,
6723 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006724 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006725
6726 if (drv->device_ap_sme && is_ap_interface(drv->nlmode)) {
Dmitry Shmidt56052862013-10-04 10:23:25 -07006727 if (freq == 0) {
6728 wpa_printf(MSG_DEBUG, "nl80211: Use bss->freq=%d",
6729 bss->freq);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006730 freq = bss->freq;
Dmitry Shmidt56052862013-10-04 10:23:25 -07006731 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07006732 return nl80211_send_frame_cmd(bss, freq,
6733 (int) freq == bss->freq ? 0 :
6734 wait_time,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006735 data, data_len,
6736 &drv->send_action_cookie,
6737 no_cck, noack, offchanok);
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07006738 }
Dmitry Shmidtb638fe72012-03-20 12:51:25 -07006739
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006740 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
6741 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) {
6742 /*
6743 * Only one of the authentication frame types is encrypted.
6744 * In order for static WEP encryption to work properly (i.e.,
6745 * to not encrypt the frame), we need to tell mac80211 about
6746 * the frames that must not be encrypted.
6747 */
6748 u16 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
6749 u16 auth_trans = le_to_host16(mgmt->u.auth.auth_transaction);
6750 if (auth_alg != WLAN_AUTH_SHARED_KEY || auth_trans != 3)
6751 encrypt = 0;
6752 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006753
Dmitry Shmidt56052862013-10-04 10:23:25 -07006754 wpa_printf(MSG_DEBUG, "nl80211: send_mlme -> send_frame");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006755 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006756 noack, freq, no_cck, offchanok,
6757 wait_time);
6758}
6759
6760
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006761static int nl80211_set_bss(struct i802_bss *bss, int cts, int preamble,
6762 int slot, int ht_opmode, int ap_isolate,
6763 int *basic_rates)
6764{
6765 struct wpa_driver_nl80211_data *drv = bss->drv;
6766 struct nl_msg *msg;
6767
6768 msg = nlmsg_alloc();
6769 if (!msg)
6770 return -ENOMEM;
6771
6772 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_BSS);
6773
6774 if (cts >= 0)
6775 NLA_PUT_U8(msg, NL80211_ATTR_BSS_CTS_PROT, cts);
6776 if (preamble >= 0)
6777 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble);
6778 if (slot >= 0)
6779 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot);
6780 if (ht_opmode >= 0)
6781 NLA_PUT_U16(msg, NL80211_ATTR_BSS_HT_OPMODE, ht_opmode);
6782 if (ap_isolate >= 0)
6783 NLA_PUT_U8(msg, NL80211_ATTR_AP_ISOLATE, ap_isolate);
6784
6785 if (basic_rates) {
6786 u8 rates[NL80211_MAX_SUPP_RATES];
6787 u8 rates_len = 0;
6788 int i;
6789
6790 for (i = 0; i < NL80211_MAX_SUPP_RATES && basic_rates[i] >= 0;
6791 i++)
6792 rates[rates_len++] = basic_rates[i] / 5;
6793
6794 NLA_PUT(msg, NL80211_ATTR_BSS_BASIC_RATES, rates_len, rates);
6795 }
6796
6797 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
6798
6799 return send_and_recv_msgs(drv, msg, NULL, NULL);
6800 nla_put_failure:
6801 nlmsg_free(msg);
6802 return -ENOBUFS;
6803}
6804
6805
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006806static int wpa_driver_nl80211_set_acl(void *priv,
6807 struct hostapd_acl_params *params)
6808{
6809 struct i802_bss *bss = priv;
6810 struct wpa_driver_nl80211_data *drv = bss->drv;
6811 struct nl_msg *msg;
6812 struct nlattr *acl;
6813 unsigned int i;
6814 int ret = 0;
6815
6816 if (!(drv->capa.max_acl_mac_addrs))
6817 return -ENOTSUP;
6818
6819 if (params->num_mac_acl > drv->capa.max_acl_mac_addrs)
6820 return -ENOTSUP;
6821
6822 msg = nlmsg_alloc();
6823 if (!msg)
6824 return -ENOMEM;
6825
6826 wpa_printf(MSG_DEBUG, "nl80211: Set %s ACL (num_mac_acl=%u)",
6827 params->acl_policy ? "Accept" : "Deny", params->num_mac_acl);
6828
6829 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_MAC_ACL);
6830
6831 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
6832
6833 NLA_PUT_U32(msg, NL80211_ATTR_ACL_POLICY, params->acl_policy ?
6834 NL80211_ACL_POLICY_DENY_UNLESS_LISTED :
6835 NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED);
6836
6837 acl = nla_nest_start(msg, NL80211_ATTR_MAC_ADDRS);
6838 if (acl == NULL)
6839 goto nla_put_failure;
6840
6841 for (i = 0; i < params->num_mac_acl; i++)
6842 NLA_PUT(msg, i + 1, ETH_ALEN, params->mac_acl[i].addr);
6843
6844 nla_nest_end(msg, acl);
6845
6846 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6847 msg = NULL;
6848 if (ret) {
6849 wpa_printf(MSG_DEBUG, "nl80211: Failed to set MAC ACL: %d (%s)",
6850 ret, strerror(-ret));
6851 }
6852
6853nla_put_failure:
6854 nlmsg_free(msg);
6855
6856 return ret;
6857}
6858
6859
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006860static int wpa_driver_nl80211_set_ap(void *priv,
6861 struct wpa_driver_ap_params *params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006862{
6863 struct i802_bss *bss = priv;
6864 struct wpa_driver_nl80211_data *drv = bss->drv;
6865 struct nl_msg *msg;
6866 u8 cmd = NL80211_CMD_NEW_BEACON;
6867 int ret;
6868 int beacon_set;
6869 int ifindex = if_nametoindex(bss->ifname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006870 int num_suites;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006871 u32 suites[10], suite;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006872 u32 ver;
6873
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07006874 beacon_set = bss->beacon_set;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006875
6876 msg = nlmsg_alloc();
6877 if (!msg)
6878 return -ENOMEM;
6879
6880 wpa_printf(MSG_DEBUG, "nl80211: Set beacon (beacon_set=%d)",
6881 beacon_set);
6882 if (beacon_set)
6883 cmd = NL80211_CMD_SET_BEACON;
6884
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006885 nl80211_cmd(drv, msg, 0, cmd);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07006886 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon head",
6887 params->head, params->head_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006888 NLA_PUT(msg, NL80211_ATTR_BEACON_HEAD, params->head_len, params->head);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07006889 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon tail",
6890 params->tail, params->tail_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006891 NLA_PUT(msg, NL80211_ATTR_BEACON_TAIL, params->tail_len, params->tail);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07006892 wpa_printf(MSG_DEBUG, "nl80211: ifindex=%d", ifindex);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006893 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07006894 wpa_printf(MSG_DEBUG, "nl80211: beacon_int=%d", params->beacon_int);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006895 NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL, params->beacon_int);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07006896 wpa_printf(MSG_DEBUG, "nl80211: dtim_period=%d", params->dtim_period);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006897 NLA_PUT_U32(msg, NL80211_ATTR_DTIM_PERIOD, params->dtim_period);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07006898 wpa_hexdump_ascii(MSG_DEBUG, "nl80211: ssid",
6899 params->ssid, params->ssid_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006900 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
6901 params->ssid);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07006902 if (params->proberesp && params->proberesp_len) {
6903 wpa_hexdump(MSG_DEBUG, "nl80211: proberesp (offload)",
6904 params->proberesp, params->proberesp_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006905 NLA_PUT(msg, NL80211_ATTR_PROBE_RESP, params->proberesp_len,
6906 params->proberesp);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07006907 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006908 switch (params->hide_ssid) {
6909 case NO_SSID_HIDING:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07006910 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID not in use");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006911 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
6912 NL80211_HIDDEN_SSID_NOT_IN_USE);
6913 break;
6914 case HIDDEN_SSID_ZERO_LEN:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07006915 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero len");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006916 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
6917 NL80211_HIDDEN_SSID_ZERO_LEN);
6918 break;
6919 case HIDDEN_SSID_ZERO_CONTENTS:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07006920 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero contents");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006921 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
6922 NL80211_HIDDEN_SSID_ZERO_CONTENTS);
6923 break;
6924 }
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07006925 wpa_printf(MSG_DEBUG, "nl80211: privacy=%d", params->privacy);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006926 if (params->privacy)
6927 NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07006928 wpa_printf(MSG_DEBUG, "nl80211: auth_algs=0x%x", params->auth_algs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006929 if ((params->auth_algs & (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) ==
6930 (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) {
6931 /* Leave out the attribute */
6932 } else if (params->auth_algs & WPA_AUTH_ALG_SHARED)
6933 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE,
6934 NL80211_AUTHTYPE_SHARED_KEY);
6935 else
6936 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE,
6937 NL80211_AUTHTYPE_OPEN_SYSTEM);
6938
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07006939 wpa_printf(MSG_DEBUG, "nl80211: wpa_version=0x%x", params->wpa_version);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006940 ver = 0;
6941 if (params->wpa_version & WPA_PROTO_WPA)
6942 ver |= NL80211_WPA_VERSION_1;
6943 if (params->wpa_version & WPA_PROTO_RSN)
6944 ver |= NL80211_WPA_VERSION_2;
6945 if (ver)
6946 NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver);
6947
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07006948 wpa_printf(MSG_DEBUG, "nl80211: key_mgmt_suites=0x%x",
6949 params->key_mgmt_suites);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006950 num_suites = 0;
6951 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X)
6952 suites[num_suites++] = WLAN_AKM_SUITE_8021X;
6953 if (params->key_mgmt_suites & WPA_KEY_MGMT_PSK)
6954 suites[num_suites++] = WLAN_AKM_SUITE_PSK;
6955 if (num_suites) {
6956 NLA_PUT(msg, NL80211_ATTR_AKM_SUITES,
6957 num_suites * sizeof(u32), suites);
6958 }
6959
6960 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X &&
6961 params->pairwise_ciphers & (WPA_CIPHER_WEP104 | WPA_CIPHER_WEP40))
6962 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT);
6963
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07006964 wpa_printf(MSG_DEBUG, "nl80211: pairwise_ciphers=0x%x",
6965 params->pairwise_ciphers);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006966 num_suites = wpa_cipher_to_cipher_suites(params->pairwise_ciphers,
6967 suites, ARRAY_SIZE(suites));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006968 if (num_suites) {
6969 NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
6970 num_suites * sizeof(u32), suites);
6971 }
6972
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07006973 wpa_printf(MSG_DEBUG, "nl80211: group_cipher=0x%x",
6974 params->group_cipher);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006975 suite = wpa_cipher_to_cipher_suite(params->group_cipher);
6976 if (suite)
6977 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, suite);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006978
6979 if (params->beacon_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07006980 wpa_hexdump_buf(MSG_DEBUG, "nl80211: beacon_ies",
6981 params->beacon_ies);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006982 NLA_PUT(msg, NL80211_ATTR_IE, wpabuf_len(params->beacon_ies),
6983 wpabuf_head(params->beacon_ies));
6984 }
6985 if (params->proberesp_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07006986 wpa_hexdump_buf(MSG_DEBUG, "nl80211: proberesp_ies",
6987 params->proberesp_ies);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006988 NLA_PUT(msg, NL80211_ATTR_IE_PROBE_RESP,
6989 wpabuf_len(params->proberesp_ies),
6990 wpabuf_head(params->proberesp_ies));
6991 }
6992 if (params->assocresp_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07006993 wpa_hexdump_buf(MSG_DEBUG, "nl80211: assocresp_ies",
6994 params->assocresp_ies);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006995 NLA_PUT(msg, NL80211_ATTR_IE_ASSOC_RESP,
6996 wpabuf_len(params->assocresp_ies),
6997 wpabuf_head(params->assocresp_ies));
6998 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006999
Dmitry Shmidt04949592012-07-19 12:16:46 -07007000 if (drv->capa.flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007001 wpa_printf(MSG_DEBUG, "nl80211: ap_max_inactivity=%d",
7002 params->ap_max_inactivity);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007003 NLA_PUT_U16(msg, NL80211_ATTR_INACTIVITY_TIMEOUT,
7004 params->ap_max_inactivity);
7005 }
7006
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007007 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7008 if (ret) {
7009 wpa_printf(MSG_DEBUG, "nl80211: Beacon set failed: %d (%s)",
7010 ret, strerror(-ret));
7011 } else {
7012 bss->beacon_set = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007013 nl80211_set_bss(bss, params->cts_protect, params->preamble,
7014 params->short_slot_time, params->ht_opmode,
7015 params->isolate, params->basic_rates);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007016 }
7017 return ret;
7018 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007019 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007020 return -ENOBUFS;
7021}
7022
7023
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007024static int nl80211_put_freq_params(struct nl_msg *msg,
7025 struct hostapd_freq_params *freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007026{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007027 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq->freq);
7028 if (freq->vht_enabled) {
7029 switch (freq->bandwidth) {
7030 case 20:
7031 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
7032 NL80211_CHAN_WIDTH_20);
7033 break;
7034 case 40:
7035 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
7036 NL80211_CHAN_WIDTH_40);
7037 break;
7038 case 80:
7039 if (freq->center_freq2)
7040 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
7041 NL80211_CHAN_WIDTH_80P80);
7042 else
7043 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
7044 NL80211_CHAN_WIDTH_80);
7045 break;
7046 case 160:
7047 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
7048 NL80211_CHAN_WIDTH_160);
7049 break;
7050 default:
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007051 return -EINVAL;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007052 }
7053 NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ1, freq->center_freq1);
7054 if (freq->center_freq2)
7055 NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ2,
7056 freq->center_freq2);
7057 } else if (freq->ht_enabled) {
7058 switch (freq->sec_channel_offset) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007059 case -1:
7060 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
7061 NL80211_CHAN_HT40MINUS);
7062 break;
7063 case 1:
7064 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
7065 NL80211_CHAN_HT40PLUS);
7066 break;
7067 default:
7068 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
7069 NL80211_CHAN_HT20);
7070 break;
7071 }
7072 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007073 return 0;
7074
7075nla_put_failure:
7076 return -ENOBUFS;
7077}
7078
7079
7080static int wpa_driver_nl80211_set_freq(struct i802_bss *bss,
7081 struct hostapd_freq_params *freq)
7082{
7083 struct wpa_driver_nl80211_data *drv = bss->drv;
7084 struct nl_msg *msg;
7085 int ret;
7086
7087 wpa_printf(MSG_DEBUG,
7088 "nl80211: Set freq %d (ht_enabled=%d, vht_enabled=%d, bandwidth=%d MHz, cf1=%d MHz, cf2=%d MHz)",
7089 freq->freq, freq->ht_enabled, freq->vht_enabled,
7090 freq->bandwidth, freq->center_freq1, freq->center_freq2);
7091 msg = nlmsg_alloc();
7092 if (!msg)
7093 return -1;
7094
7095 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
7096
7097 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
7098 if (nl80211_put_freq_params(msg, freq) < 0)
7099 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007100
7101 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007102 msg = NULL;
7103 if (ret == 0) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007104 bss->freq = freq->freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007105 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007106 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007107 wpa_printf(MSG_DEBUG, "nl80211: Failed to set channel (freq=%d): "
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007108 "%d (%s)", freq->freq, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007109nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007110 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007111 return -1;
7112}
7113
7114
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007115static u32 sta_flags_nl80211(int flags)
7116{
7117 u32 f = 0;
7118
7119 if (flags & WPA_STA_AUTHORIZED)
7120 f |= BIT(NL80211_STA_FLAG_AUTHORIZED);
7121 if (flags & WPA_STA_WMM)
7122 f |= BIT(NL80211_STA_FLAG_WME);
7123 if (flags & WPA_STA_SHORT_PREAMBLE)
7124 f |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
7125 if (flags & WPA_STA_MFP)
7126 f |= BIT(NL80211_STA_FLAG_MFP);
7127 if (flags & WPA_STA_TDLS_PEER)
7128 f |= BIT(NL80211_STA_FLAG_TDLS_PEER);
7129
7130 return f;
7131}
7132
7133
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007134static int wpa_driver_nl80211_sta_add(void *priv,
7135 struct hostapd_sta_add_params *params)
7136{
7137 struct i802_bss *bss = priv;
7138 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007139 struct nl_msg *msg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007140 struct nl80211_sta_flag_update upd;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007141 int ret = -ENOBUFS;
7142
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007143 if ((params->flags & WPA_STA_TDLS_PEER) &&
7144 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
7145 return -EOPNOTSUPP;
7146
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007147 msg = nlmsg_alloc();
7148 if (!msg)
7149 return -ENOMEM;
7150
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007151 wpa_printf(MSG_DEBUG, "nl80211: %s STA " MACSTR,
7152 params->set ? "Set" : "Add", MAC2STR(params->addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007153 nl80211_cmd(drv, msg, 0, params->set ? NL80211_CMD_SET_STATION :
7154 NL80211_CMD_NEW_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007155
7156 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
7157 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007158 NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_RATES, params->supp_rates_len,
7159 params->supp_rates);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007160 wpa_hexdump(MSG_DEBUG, " * supported rates", params->supp_rates,
7161 params->supp_rates_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007162 if (!params->set) {
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07007163 if (params->aid) {
7164 wpa_printf(MSG_DEBUG, " * aid=%u", params->aid);
7165 NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, params->aid);
7166 } else {
7167 /*
7168 * cfg80211 validates that AID is non-zero, so we have
7169 * to make this a non-zero value for the TDLS case where
7170 * a dummy STA entry is used for now.
7171 */
7172 wpa_printf(MSG_DEBUG, " * aid=1 (TDLS workaround)");
7173 NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, 1);
7174 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007175 wpa_printf(MSG_DEBUG, " * listen_interval=%u",
7176 params->listen_interval);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007177 NLA_PUT_U16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
7178 params->listen_interval);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07007179 } else if (params->aid && (params->flags & WPA_STA_TDLS_PEER)) {
7180 wpa_printf(MSG_DEBUG, " * peer_aid=%u", params->aid);
7181 NLA_PUT_U16(msg, NL80211_ATTR_PEER_AID, params->aid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007182 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007183 if (params->ht_capabilities) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007184 wpa_hexdump(MSG_DEBUG, " * ht_capabilities",
7185 (u8 *) params->ht_capabilities,
7186 sizeof(*params->ht_capabilities));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007187 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY,
7188 sizeof(*params->ht_capabilities),
7189 params->ht_capabilities);
7190 }
7191
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007192 if (params->vht_capabilities) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007193 wpa_hexdump(MSG_DEBUG, " * vht_capabilities",
7194 (u8 *) params->vht_capabilities,
7195 sizeof(*params->vht_capabilities));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007196 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY,
7197 sizeof(*params->vht_capabilities),
7198 params->vht_capabilities);
7199 }
7200
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007201 wpa_printf(MSG_DEBUG, " * capability=0x%x", params->capability);
7202 NLA_PUT_U16(msg, NL80211_ATTR_STA_CAPABILITY, params->capability);
7203
7204 if (params->ext_capab) {
7205 wpa_hexdump(MSG_DEBUG, " * ext_capab",
7206 params->ext_capab, params->ext_capab_len);
7207 NLA_PUT(msg, NL80211_ATTR_STA_EXT_CAPABILITY,
7208 params->ext_capab_len, params->ext_capab);
7209 }
7210
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007211 os_memset(&upd, 0, sizeof(upd));
7212 upd.mask = sta_flags_nl80211(params->flags);
7213 upd.set = upd.mask;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007214 wpa_printf(MSG_DEBUG, " * flags set=0x%x mask=0x%x",
7215 upd.set, upd.mask);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007216 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
7217
7218 if (params->flags & WPA_STA_WMM) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007219 struct nlattr *wme = nla_nest_start(msg, NL80211_ATTR_STA_WME);
7220
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007221 if (!wme)
7222 goto nla_put_failure;
7223
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007224 wpa_printf(MSG_DEBUG, " * qosinfo=0x%x", params->qosinfo);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007225 NLA_PUT_U8(msg, NL80211_STA_WME_UAPSD_QUEUES,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007226 params->qosinfo & WMM_QOSINFO_STA_AC_MASK);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007227 NLA_PUT_U8(msg, NL80211_STA_WME_MAX_SP,
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007228 (params->qosinfo >> WMM_QOSINFO_STA_SP_SHIFT) &
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007229 WMM_QOSINFO_STA_SP_MASK);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007230 nla_nest_end(msg, wme);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007231 }
7232
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007233 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007234 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007235 if (ret)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007236 wpa_printf(MSG_DEBUG, "nl80211: NL80211_CMD_%s_STATION "
7237 "result: %d (%s)", params->set ? "SET" : "NEW", ret,
7238 strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007239 if (ret == -EEXIST)
7240 ret = 0;
7241 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007242 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007243 return ret;
7244}
7245
7246
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007247static int wpa_driver_nl80211_sta_remove(struct i802_bss *bss, const u8 *addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007248{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007249 struct wpa_driver_nl80211_data *drv = bss->drv;
7250 struct nl_msg *msg;
7251 int ret;
7252
7253 msg = nlmsg_alloc();
7254 if (!msg)
7255 return -ENOMEM;
7256
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007257 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007258
7259 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
7260 if_nametoindex(bss->ifname));
7261 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
7262
7263 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007264 wpa_printf(MSG_DEBUG, "nl80211: sta_remove -> DEL_STATION %s " MACSTR
7265 " --> %d (%s)",
7266 bss->ifname, MAC2STR(addr), ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007267 if (ret == -ENOENT)
7268 return 0;
7269 return ret;
7270 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007271 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007272 return -ENOBUFS;
7273}
7274
7275
7276static void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv,
7277 int ifidx)
7278{
7279 struct nl_msg *msg;
7280
7281 wpa_printf(MSG_DEBUG, "nl80211: Remove interface ifindex=%d", ifidx);
7282
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007283 /* stop listening for EAPOL on this interface */
7284 del_ifidx(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007285
7286 msg = nlmsg_alloc();
7287 if (!msg)
7288 goto nla_put_failure;
7289
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007290 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_INTERFACE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007291 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifidx);
7292
7293 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
7294 return;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007295 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007296 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007297 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007298 wpa_printf(MSG_ERROR, "Failed to remove interface (ifidx=%d)", ifidx);
7299}
7300
7301
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007302static const char * nl80211_iftype_str(enum nl80211_iftype mode)
7303{
7304 switch (mode) {
7305 case NL80211_IFTYPE_ADHOC:
7306 return "ADHOC";
7307 case NL80211_IFTYPE_STATION:
7308 return "STATION";
7309 case NL80211_IFTYPE_AP:
7310 return "AP";
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07007311 case NL80211_IFTYPE_AP_VLAN:
7312 return "AP_VLAN";
7313 case NL80211_IFTYPE_WDS:
7314 return "WDS";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007315 case NL80211_IFTYPE_MONITOR:
7316 return "MONITOR";
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07007317 case NL80211_IFTYPE_MESH_POINT:
7318 return "MESH_POINT";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007319 case NL80211_IFTYPE_P2P_CLIENT:
7320 return "P2P_CLIENT";
7321 case NL80211_IFTYPE_P2P_GO:
7322 return "P2P_GO";
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007323 case NL80211_IFTYPE_P2P_DEVICE:
7324 return "P2P_DEVICE";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007325 default:
7326 return "unknown";
7327 }
7328}
7329
7330
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007331static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv,
7332 const char *ifname,
7333 enum nl80211_iftype iftype,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007334 const u8 *addr, int wds,
7335 int (*handler)(struct nl_msg *, void *),
7336 void *arg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007337{
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007338 struct nl_msg *msg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007339 int ifidx;
7340 int ret = -ENOBUFS;
7341
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007342 wpa_printf(MSG_DEBUG, "nl80211: Create interface iftype %d (%s)",
7343 iftype, nl80211_iftype_str(iftype));
7344
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007345 msg = nlmsg_alloc();
7346 if (!msg)
7347 return -1;
7348
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007349 nl80211_cmd(drv, msg, 0, NL80211_CMD_NEW_INTERFACE);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007350 if (nl80211_set_iface_id(msg, drv->first_bss) < 0)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007351 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007352 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, ifname);
7353 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, iftype);
7354
7355 if (iftype == NL80211_IFTYPE_MONITOR) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007356 struct nlattr *flags;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007357
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007358 flags = nla_nest_start(msg, NL80211_ATTR_MNTR_FLAGS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007359 if (!flags)
7360 goto nla_put_failure;
7361
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007362 NLA_PUT_FLAG(msg, NL80211_MNTR_FLAG_COOK_FRAMES);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007363
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007364 nla_nest_end(msg, flags);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007365 } else if (wds) {
7366 NLA_PUT_U8(msg, NL80211_ATTR_4ADDR, wds);
7367 }
7368
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007369 ret = send_and_recv_msgs(drv, msg, handler, arg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007370 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007371 if (ret) {
7372 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007373 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007374 wpa_printf(MSG_ERROR, "Failed to create interface %s: %d (%s)",
7375 ifname, ret, strerror(-ret));
7376 return ret;
7377 }
7378
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007379 if (iftype == NL80211_IFTYPE_P2P_DEVICE)
7380 return 0;
7381
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007382 ifidx = if_nametoindex(ifname);
7383 wpa_printf(MSG_DEBUG, "nl80211: New interface %s created: ifindex=%d",
7384 ifname, ifidx);
7385
7386 if (ifidx <= 0)
7387 return -1;
7388
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007389 /* start listening for EAPOL on this interface */
7390 add_ifidx(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007391
7392 if (addr && iftype != NL80211_IFTYPE_MONITOR &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007393 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname, addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007394 nl80211_remove_iface(drv, ifidx);
7395 return -1;
7396 }
7397
7398 return ifidx;
7399}
7400
7401
7402static int nl80211_create_iface(struct wpa_driver_nl80211_data *drv,
7403 const char *ifname, enum nl80211_iftype iftype,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007404 const u8 *addr, int wds,
7405 int (*handler)(struct nl_msg *, void *),
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007406 void *arg, int use_existing)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007407{
7408 int ret;
7409
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007410 ret = nl80211_create_iface_once(drv, ifname, iftype, addr, wds, handler,
7411 arg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007412
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007413 /* if error occurred and interface exists already */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007414 if (ret == -ENFILE && if_nametoindex(ifname)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007415 if (use_existing) {
7416 wpa_printf(MSG_DEBUG, "nl80211: Continue using existing interface %s",
7417 ifname);
7418 return -ENFILE;
7419 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007420 wpa_printf(MSG_INFO, "Try to remove and re-create %s", ifname);
7421
7422 /* Try to remove the interface that was already there. */
7423 nl80211_remove_iface(drv, if_nametoindex(ifname));
7424
7425 /* Try to create the interface again */
7426 ret = nl80211_create_iface_once(drv, ifname, iftype, addr,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007427 wds, handler, arg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007428 }
7429
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007430 if (ret >= 0 && is_p2p_net_interface(iftype))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007431 nl80211_disable_11b_rates(drv, ret, 1);
7432
7433 return ret;
7434}
7435
7436
7437static void handle_tx_callback(void *ctx, u8 *buf, size_t len, int ok)
7438{
7439 struct ieee80211_hdr *hdr;
7440 u16 fc;
7441 union wpa_event_data event;
7442
7443 hdr = (struct ieee80211_hdr *) buf;
7444 fc = le_to_host16(hdr->frame_control);
7445
7446 os_memset(&event, 0, sizeof(event));
7447 event.tx_status.type = WLAN_FC_GET_TYPE(fc);
7448 event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
7449 event.tx_status.dst = hdr->addr1;
7450 event.tx_status.data = buf;
7451 event.tx_status.data_len = len;
7452 event.tx_status.ack = ok;
7453 wpa_supplicant_event(ctx, EVENT_TX_STATUS, &event);
7454}
7455
7456
7457static void from_unknown_sta(struct wpa_driver_nl80211_data *drv,
7458 u8 *buf, size_t len)
7459{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007460 struct ieee80211_hdr *hdr = (void *)buf;
7461 u16 fc;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007462 union wpa_event_data event;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007463
7464 if (len < sizeof(*hdr))
7465 return;
7466
7467 fc = le_to_host16(hdr->frame_control);
7468
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007469 os_memset(&event, 0, sizeof(event));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007470 event.rx_from_unknown.bssid = get_hdr_bssid(hdr, len);
7471 event.rx_from_unknown.addr = hdr->addr2;
7472 event.rx_from_unknown.wds = (fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) ==
7473 (WLAN_FC_FROMDS | WLAN_FC_TODS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007474 wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event);
7475}
7476
7477
7478static void handle_frame(struct wpa_driver_nl80211_data *drv,
7479 u8 *buf, size_t len, int datarate, int ssi_signal)
7480{
7481 struct ieee80211_hdr *hdr;
7482 u16 fc;
7483 union wpa_event_data event;
7484
7485 hdr = (struct ieee80211_hdr *) buf;
7486 fc = le_to_host16(hdr->frame_control);
7487
7488 switch (WLAN_FC_GET_TYPE(fc)) {
7489 case WLAN_FC_TYPE_MGMT:
7490 os_memset(&event, 0, sizeof(event));
7491 event.rx_mgmt.frame = buf;
7492 event.rx_mgmt.frame_len = len;
7493 event.rx_mgmt.datarate = datarate;
7494 event.rx_mgmt.ssi_signal = ssi_signal;
7495 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
7496 break;
7497 case WLAN_FC_TYPE_CTRL:
7498 /* can only get here with PS-Poll frames */
7499 wpa_printf(MSG_DEBUG, "CTRL");
7500 from_unknown_sta(drv, buf, len);
7501 break;
7502 case WLAN_FC_TYPE_DATA:
7503 from_unknown_sta(drv, buf, len);
7504 break;
7505 }
7506}
7507
7508
7509static void handle_monitor_read(int sock, void *eloop_ctx, void *sock_ctx)
7510{
7511 struct wpa_driver_nl80211_data *drv = eloop_ctx;
7512 int len;
7513 unsigned char buf[3000];
7514 struct ieee80211_radiotap_iterator iter;
7515 int ret;
7516 int datarate = 0, ssi_signal = 0;
7517 int injected = 0, failed = 0, rxflags = 0;
7518
7519 len = recv(sock, buf, sizeof(buf), 0);
7520 if (len < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007521 wpa_printf(MSG_ERROR, "nl80211: Monitor socket recv failed: %s",
7522 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007523 return;
7524 }
7525
7526 if (ieee80211_radiotap_iterator_init(&iter, (void*)buf, len)) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007527 wpa_printf(MSG_INFO, "nl80211: received invalid radiotap frame");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007528 return;
7529 }
7530
7531 while (1) {
7532 ret = ieee80211_radiotap_iterator_next(&iter);
7533 if (ret == -ENOENT)
7534 break;
7535 if (ret) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007536 wpa_printf(MSG_INFO, "nl80211: received invalid radiotap frame (%d)",
7537 ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007538 return;
7539 }
7540 switch (iter.this_arg_index) {
7541 case IEEE80211_RADIOTAP_FLAGS:
7542 if (*iter.this_arg & IEEE80211_RADIOTAP_F_FCS)
7543 len -= 4;
7544 break;
7545 case IEEE80211_RADIOTAP_RX_FLAGS:
7546 rxflags = 1;
7547 break;
7548 case IEEE80211_RADIOTAP_TX_FLAGS:
7549 injected = 1;
7550 failed = le_to_host16((*(uint16_t *) iter.this_arg)) &
7551 IEEE80211_RADIOTAP_F_TX_FAIL;
7552 break;
7553 case IEEE80211_RADIOTAP_DATA_RETRIES:
7554 break;
7555 case IEEE80211_RADIOTAP_CHANNEL:
7556 /* TODO: convert from freq/flags to channel number */
7557 break;
7558 case IEEE80211_RADIOTAP_RATE:
7559 datarate = *iter.this_arg * 5;
7560 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007561 case IEEE80211_RADIOTAP_DBM_ANTSIGNAL:
7562 ssi_signal = (s8) *iter.this_arg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007563 break;
7564 }
7565 }
7566
7567 if (rxflags && injected)
7568 return;
7569
7570 if (!injected)
7571 handle_frame(drv, buf + iter.max_length,
7572 len - iter.max_length, datarate, ssi_signal);
7573 else
7574 handle_tx_callback(drv->ctx, buf + iter.max_length,
7575 len - iter.max_length, !failed);
7576}
7577
7578
7579/*
7580 * we post-process the filter code later and rewrite
7581 * this to the offset to the last instruction
7582 */
7583#define PASS 0xFF
7584#define FAIL 0xFE
7585
7586static struct sock_filter msock_filter_insns[] = {
7587 /*
7588 * do a little-endian load of the radiotap length field
7589 */
7590 /* load lower byte into A */
7591 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 2),
7592 /* put it into X (== index register) */
7593 BPF_STMT(BPF_MISC| BPF_TAX, 0),
7594 /* load upper byte into A */
7595 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 3),
7596 /* left-shift it by 8 */
7597 BPF_STMT(BPF_ALU | BPF_LSH | BPF_K, 8),
7598 /* or with X */
7599 BPF_STMT(BPF_ALU | BPF_OR | BPF_X, 0),
7600 /* put result into X */
7601 BPF_STMT(BPF_MISC| BPF_TAX, 0),
7602
7603 /*
7604 * Allow management frames through, this also gives us those
7605 * management frames that we sent ourselves with status
7606 */
7607 /* load the lower byte of the IEEE 802.11 frame control field */
7608 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
7609 /* mask off frame type and version */
7610 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0xF),
7611 /* accept frame if it's both 0, fall through otherwise */
7612 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0, PASS, 0),
7613
7614 /*
7615 * TODO: add a bit to radiotap RX flags that indicates
7616 * that the sending station is not associated, then
7617 * add a filter here that filters on our DA and that flag
7618 * to allow us to deauth frames to that bad station.
7619 *
7620 * For now allow all To DS data frames through.
7621 */
7622 /* load the IEEE 802.11 frame control field */
7623 BPF_STMT(BPF_LD | BPF_H | BPF_IND, 0),
7624 /* mask off frame type, version and DS status */
7625 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0F03),
7626 /* accept frame if version 0, type 2 and To DS, fall through otherwise
7627 */
7628 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0801, PASS, 0),
7629
7630#if 0
7631 /*
7632 * drop non-data frames
7633 */
7634 /* load the lower byte of the frame control field */
7635 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
7636 /* mask off QoS bit */
7637 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0c),
7638 /* drop non-data frames */
7639 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 8, 0, FAIL),
7640#endif
7641 /* load the upper byte of the frame control field */
7642 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 1),
7643 /* mask off toDS/fromDS */
7644 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x03),
7645 /* accept WDS frames */
7646 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 3, PASS, 0),
7647
7648 /*
7649 * add header length to index
7650 */
7651 /* load the lower byte of the frame control field */
7652 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
7653 /* mask off QoS bit */
7654 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x80),
7655 /* right shift it by 6 to give 0 or 2 */
7656 BPF_STMT(BPF_ALU | BPF_RSH | BPF_K, 6),
7657 /* add data frame header length */
7658 BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 24),
7659 /* add index, was start of 802.11 header */
7660 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
7661 /* move to index, now start of LL header */
7662 BPF_STMT(BPF_MISC | BPF_TAX, 0),
7663
7664 /*
7665 * Accept empty data frames, we use those for
7666 * polling activity.
7667 */
7668 BPF_STMT(BPF_LD | BPF_W | BPF_LEN, 0),
7669 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_X, 0, PASS, 0),
7670
7671 /*
7672 * Accept EAPOL frames
7673 */
7674 BPF_STMT(BPF_LD | BPF_W | BPF_IND, 0),
7675 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0xAAAA0300, 0, FAIL),
7676 BPF_STMT(BPF_LD | BPF_W | BPF_IND, 4),
7677 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0000888E, PASS, FAIL),
7678
7679 /* keep these last two statements or change the code below */
7680 /* return 0 == "DROP" */
7681 BPF_STMT(BPF_RET | BPF_K, 0),
7682 /* return ~0 == "keep all" */
7683 BPF_STMT(BPF_RET | BPF_K, ~0),
7684};
7685
7686static struct sock_fprog msock_filter = {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007687 .len = ARRAY_SIZE(msock_filter_insns),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007688 .filter = msock_filter_insns,
7689};
7690
7691
7692static int add_monitor_filter(int s)
7693{
7694 int idx;
7695
7696 /* rewrite all PASS/FAIL jump offsets */
7697 for (idx = 0; idx < msock_filter.len; idx++) {
7698 struct sock_filter *insn = &msock_filter_insns[idx];
7699
7700 if (BPF_CLASS(insn->code) == BPF_JMP) {
7701 if (insn->code == (BPF_JMP|BPF_JA)) {
7702 if (insn->k == PASS)
7703 insn->k = msock_filter.len - idx - 2;
7704 else if (insn->k == FAIL)
7705 insn->k = msock_filter.len - idx - 3;
7706 }
7707
7708 if (insn->jt == PASS)
7709 insn->jt = msock_filter.len - idx - 2;
7710 else if (insn->jt == FAIL)
7711 insn->jt = msock_filter.len - idx - 3;
7712
7713 if (insn->jf == PASS)
7714 insn->jf = msock_filter.len - idx - 2;
7715 else if (insn->jf == FAIL)
7716 insn->jf = msock_filter.len - idx - 3;
7717 }
7718 }
7719
7720 if (setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER,
7721 &msock_filter, sizeof(msock_filter))) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007722 wpa_printf(MSG_ERROR, "nl80211: setsockopt(SO_ATTACH_FILTER) failed: %s",
7723 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007724 return -1;
7725 }
7726
7727 return 0;
7728}
7729
7730
7731static void nl80211_remove_monitor_interface(
7732 struct wpa_driver_nl80211_data *drv)
7733{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007734 if (drv->monitor_refcount > 0)
7735 drv->monitor_refcount--;
7736 wpa_printf(MSG_DEBUG, "nl80211: Remove monitor interface: refcount=%d",
7737 drv->monitor_refcount);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007738 if (drv->monitor_refcount > 0)
7739 return;
7740
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007741 if (drv->monitor_ifidx >= 0) {
7742 nl80211_remove_iface(drv, drv->monitor_ifidx);
7743 drv->monitor_ifidx = -1;
7744 }
7745 if (drv->monitor_sock >= 0) {
7746 eloop_unregister_read_sock(drv->monitor_sock);
7747 close(drv->monitor_sock);
7748 drv->monitor_sock = -1;
7749 }
7750}
7751
7752
7753static int
7754nl80211_create_monitor_interface(struct wpa_driver_nl80211_data *drv)
7755{
7756 char buf[IFNAMSIZ];
7757 struct sockaddr_ll ll;
7758 int optval;
7759 socklen_t optlen;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007760
7761 if (drv->monitor_ifidx >= 0) {
7762 drv->monitor_refcount++;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007763 wpa_printf(MSG_DEBUG, "nl80211: Re-use existing monitor interface: refcount=%d",
7764 drv->monitor_refcount);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007765 return 0;
7766 }
7767
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007768 if (os_strncmp(drv->first_bss->ifname, "p2p-", 4) == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007769 /*
7770 * P2P interface name is of the format p2p-%s-%d. For monitor
7771 * interface name corresponding to P2P GO, replace "p2p-" with
7772 * "mon-" to retain the same interface name length and to
7773 * indicate that it is a monitor interface.
7774 */
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007775 snprintf(buf, IFNAMSIZ, "mon-%s", drv->first_bss->ifname + 4);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007776 } else {
7777 /* Non-P2P interface with AP functionality. */
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007778 snprintf(buf, IFNAMSIZ, "mon.%s", drv->first_bss->ifname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007779 }
7780
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007781 buf[IFNAMSIZ - 1] = '\0';
7782
7783 drv->monitor_ifidx =
7784 nl80211_create_iface(drv, buf, NL80211_IFTYPE_MONITOR, NULL,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007785 0, NULL, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007786
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07007787 if (drv->monitor_ifidx == -EOPNOTSUPP) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007788 /*
7789 * This is backward compatibility for a few versions of
7790 * the kernel only that didn't advertise the right
7791 * attributes for the only driver that then supported
7792 * AP mode w/o monitor -- ath6kl.
7793 */
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07007794 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support "
7795 "monitor interface type - try to run without it");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007796 drv->device_ap_sme = 1;
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07007797 }
7798
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007799 if (drv->monitor_ifidx < 0)
7800 return -1;
7801
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007802 if (linux_set_iface_flags(drv->global->ioctl_sock, buf, 1))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007803 goto error;
7804
7805 memset(&ll, 0, sizeof(ll));
7806 ll.sll_family = AF_PACKET;
7807 ll.sll_ifindex = drv->monitor_ifidx;
7808 drv->monitor_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
7809 if (drv->monitor_sock < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007810 wpa_printf(MSG_ERROR, "nl80211: socket[PF_PACKET,SOCK_RAW] failed: %s",
7811 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007812 goto error;
7813 }
7814
7815 if (add_monitor_filter(drv->monitor_sock)) {
7816 wpa_printf(MSG_INFO, "Failed to set socket filter for monitor "
7817 "interface; do filtering in user space");
7818 /* This works, but will cost in performance. */
7819 }
7820
7821 if (bind(drv->monitor_sock, (struct sockaddr *) &ll, sizeof(ll)) < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007822 wpa_printf(MSG_ERROR, "nl80211: monitor socket bind failed: %s",
7823 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007824 goto error;
7825 }
7826
7827 optlen = sizeof(optval);
7828 optval = 20;
7829 if (setsockopt
7830 (drv->monitor_sock, SOL_SOCKET, SO_PRIORITY, &optval, optlen)) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007831 wpa_printf(MSG_ERROR, "nl80211: Failed to set socket priority: %s",
7832 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007833 goto error;
7834 }
7835
7836 if (eloop_register_read_sock(drv->monitor_sock, handle_monitor_read,
7837 drv, NULL)) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007838 wpa_printf(MSG_INFO, "nl80211: Could not register monitor read socket");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007839 goto error;
7840 }
7841
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007842 drv->monitor_refcount++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007843 return 0;
7844 error:
7845 nl80211_remove_monitor_interface(drv);
7846 return -1;
7847}
7848
7849
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007850static int nl80211_setup_ap(struct i802_bss *bss)
7851{
7852 struct wpa_driver_nl80211_data *drv = bss->drv;
7853
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007854 wpa_printf(MSG_DEBUG, "nl80211: Setup AP(%s) - device_ap_sme=%d use_monitor=%d",
7855 bss->ifname, drv->device_ap_sme, drv->use_monitor);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007856
7857 /*
7858 * Disable Probe Request reporting unless we need it in this way for
7859 * devices that include the AP SME, in the other case (unless using
7860 * monitor iface) we'll get it through the nl_mgmt socket instead.
7861 */
7862 if (!drv->device_ap_sme)
7863 wpa_driver_nl80211_probe_req_report(bss, 0);
7864
7865 if (!drv->device_ap_sme && !drv->use_monitor)
7866 if (nl80211_mgmt_subscribe_ap(bss))
7867 return -1;
7868
7869 if (drv->device_ap_sme && !drv->use_monitor)
7870 if (nl80211_mgmt_subscribe_ap_dev_sme(bss))
7871 return -1;
7872
7873 if (!drv->device_ap_sme && drv->use_monitor &&
7874 nl80211_create_monitor_interface(drv) &&
7875 !drv->device_ap_sme)
Dmitry Shmidt04949592012-07-19 12:16:46 -07007876 return -1;
7877
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007878 if (drv->device_ap_sme &&
7879 wpa_driver_nl80211_probe_req_report(bss, 1) < 0) {
7880 wpa_printf(MSG_DEBUG, "nl80211: Failed to enable "
7881 "Probe Request frame reporting in AP mode");
7882 /* Try to survive without this */
7883 }
7884
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007885 return 0;
7886}
7887
7888
7889static void nl80211_teardown_ap(struct i802_bss *bss)
7890{
7891 struct wpa_driver_nl80211_data *drv = bss->drv;
7892
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007893 wpa_printf(MSG_DEBUG, "nl80211: Teardown AP(%s) - device_ap_sme=%d use_monitor=%d",
7894 bss->ifname, drv->device_ap_sme, drv->use_monitor);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007895 if (drv->device_ap_sme) {
7896 wpa_driver_nl80211_probe_req_report(bss, 0);
7897 if (!drv->use_monitor)
7898 nl80211_mgmt_unsubscribe(bss, "AP teardown (dev SME)");
7899 } else if (drv->use_monitor)
7900 nl80211_remove_monitor_interface(drv);
7901 else
7902 nl80211_mgmt_unsubscribe(bss, "AP teardown");
7903
7904 bss->beacon_set = 0;
7905}
7906
7907
7908static int nl80211_send_eapol_data(struct i802_bss *bss,
7909 const u8 *addr, const u8 *data,
7910 size_t data_len)
7911{
7912 struct sockaddr_ll ll;
7913 int ret;
7914
7915 if (bss->drv->eapol_tx_sock < 0) {
7916 wpa_printf(MSG_DEBUG, "nl80211: No socket to send EAPOL");
7917 return -1;
7918 }
7919
7920 os_memset(&ll, 0, sizeof(ll));
7921 ll.sll_family = AF_PACKET;
7922 ll.sll_ifindex = bss->ifindex;
7923 ll.sll_protocol = htons(ETH_P_PAE);
7924 ll.sll_halen = ETH_ALEN;
7925 os_memcpy(ll.sll_addr, addr, ETH_ALEN);
7926 ret = sendto(bss->drv->eapol_tx_sock, data, data_len, 0,
7927 (struct sockaddr *) &ll, sizeof(ll));
7928 if (ret < 0)
7929 wpa_printf(MSG_ERROR, "nl80211: EAPOL TX: %s",
7930 strerror(errno));
7931
7932 return ret;
7933}
7934
7935
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007936static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
7937
7938static int wpa_driver_nl80211_hapd_send_eapol(
7939 void *priv, const u8 *addr, const u8 *data,
7940 size_t data_len, int encrypt, const u8 *own_addr, u32 flags)
7941{
7942 struct i802_bss *bss = priv;
7943 struct wpa_driver_nl80211_data *drv = bss->drv;
7944 struct ieee80211_hdr *hdr;
7945 size_t len;
7946 u8 *pos;
7947 int res;
7948 int qos = flags & WPA_STA_WMM;
Dmitry Shmidt641185e2013-11-06 15:17:13 -08007949
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007950 if (drv->device_ap_sme || !drv->use_monitor)
7951 return nl80211_send_eapol_data(bss, addr, data, data_len);
7952
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007953 len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
7954 data_len;
7955 hdr = os_zalloc(len);
7956 if (hdr == NULL) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007957 wpa_printf(MSG_INFO, "nl80211: Failed to allocate EAPOL buffer(len=%lu)",
7958 (unsigned long) len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007959 return -1;
7960 }
7961
7962 hdr->frame_control =
7963 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
7964 hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
7965 if (encrypt)
7966 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
7967 if (qos) {
7968 hdr->frame_control |=
7969 host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
7970 }
7971
7972 memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
7973 memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
7974 memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
7975 pos = (u8 *) (hdr + 1);
7976
7977 if (qos) {
Dmitry Shmidtaa532512012-09-24 10:35:31 -07007978 /* Set highest priority in QoS header */
7979 pos[0] = 7;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007980 pos[1] = 0;
7981 pos += 2;
7982 }
7983
7984 memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
7985 pos += sizeof(rfc1042_header);
7986 WPA_PUT_BE16(pos, ETH_P_PAE);
7987 pos += 2;
7988 memcpy(pos, data, data_len);
7989
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007990 res = wpa_driver_nl80211_send_frame(bss, (u8 *) hdr, len, encrypt, 0,
7991 0, 0, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007992 if (res < 0) {
7993 wpa_printf(MSG_ERROR, "i802_send_eapol - packet len: %lu - "
7994 "failed: %d (%s)",
7995 (unsigned long) len, errno, strerror(errno));
7996 }
7997 os_free(hdr);
7998
7999 return res;
8000}
8001
8002
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008003static int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr,
8004 int total_flags,
8005 int flags_or, int flags_and)
8006{
8007 struct i802_bss *bss = priv;
8008 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008009 struct nl_msg *msg;
8010 struct nlattr *flags;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008011 struct nl80211_sta_flag_update upd;
8012
8013 msg = nlmsg_alloc();
8014 if (!msg)
8015 return -ENOMEM;
8016
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008017 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008018
8019 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
8020 if_nametoindex(bss->ifname));
8021 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
8022
8023 /*
8024 * Backwards compatibility version using NL80211_ATTR_STA_FLAGS. This
8025 * can be removed eventually.
8026 */
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008027 flags = nla_nest_start(msg, NL80211_ATTR_STA_FLAGS);
8028 if (!flags)
8029 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008030 if (total_flags & WPA_STA_AUTHORIZED)
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008031 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_AUTHORIZED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008032
8033 if (total_flags & WPA_STA_WMM)
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008034 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_WME);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008035
8036 if (total_flags & WPA_STA_SHORT_PREAMBLE)
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008037 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_SHORT_PREAMBLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008038
8039 if (total_flags & WPA_STA_MFP)
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008040 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_MFP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008041
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008042 if (total_flags & WPA_STA_TDLS_PEER)
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008043 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_TDLS_PEER);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008044
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008045 nla_nest_end(msg, flags);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008046
8047 os_memset(&upd, 0, sizeof(upd));
8048 upd.mask = sta_flags_nl80211(flags_or | ~flags_and);
8049 upd.set = sta_flags_nl80211(flags_or);
8050 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
8051
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008052 return send_and_recv_msgs(drv, msg, NULL, NULL);
8053 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008054 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008055 return -ENOBUFS;
8056}
8057
8058
8059static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv,
8060 struct wpa_driver_associate_params *params)
8061{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008062 enum nl80211_iftype nlmode, old_mode;
8063 struct hostapd_freq_params freq = {
8064 .freq = params->freq,
8065 };
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008066
8067 if (params->p2p) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008068 wpa_printf(MSG_DEBUG, "nl80211: Setup AP operations for P2P "
8069 "group (GO)");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008070 nlmode = NL80211_IFTYPE_P2P_GO;
8071 } else
8072 nlmode = NL80211_IFTYPE_AP;
8073
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008074 old_mode = drv->nlmode;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008075 if (wpa_driver_nl80211_set_mode(drv->first_bss, nlmode)) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008076 nl80211_remove_monitor_interface(drv);
8077 return -1;
8078 }
8079
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008080 if (wpa_driver_nl80211_set_freq(drv->first_bss, &freq)) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008081 if (old_mode != nlmode)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008082 wpa_driver_nl80211_set_mode(drv->first_bss, old_mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008083 nl80211_remove_monitor_interface(drv);
8084 return -1;
8085 }
8086
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008087 return 0;
8088}
8089
8090
8091static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv)
8092{
8093 struct nl_msg *msg;
8094 int ret = -1;
8095
8096 msg = nlmsg_alloc();
8097 if (!msg)
8098 return -1;
8099
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008100 nl80211_cmd(drv, msg, 0, NL80211_CMD_LEAVE_IBSS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008101 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
8102 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8103 msg = NULL;
8104 if (ret) {
8105 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS failed: ret=%d "
8106 "(%s)", ret, strerror(-ret));
8107 goto nla_put_failure;
8108 }
8109
8110 ret = 0;
8111 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS request sent successfully");
8112
8113nla_put_failure:
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008114 if (wpa_driver_nl80211_set_mode(drv->first_bss,
Dmitry Shmidt56052862013-10-04 10:23:25 -07008115 NL80211_IFTYPE_STATION)) {
8116 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
8117 "station mode");
8118 }
8119
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008120 nlmsg_free(msg);
8121 return ret;
8122}
8123
8124
8125static int wpa_driver_nl80211_ibss(struct wpa_driver_nl80211_data *drv,
8126 struct wpa_driver_associate_params *params)
8127{
8128 struct nl_msg *msg;
8129 int ret = -1;
8130 int count = 0;
8131
8132 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS (ifindex=%d)", drv->ifindex);
8133
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008134 if (wpa_driver_nl80211_set_mode(drv->first_bss,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008135 NL80211_IFTYPE_ADHOC)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008136 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
8137 "IBSS mode");
8138 return -1;
8139 }
8140
8141retry:
8142 msg = nlmsg_alloc();
8143 if (!msg)
8144 return -1;
8145
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008146 nl80211_cmd(drv, msg, 0, NL80211_CMD_JOIN_IBSS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008147 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
8148
8149 if (params->ssid == NULL || params->ssid_len > sizeof(drv->ssid))
8150 goto nla_put_failure;
8151
8152 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
8153 params->ssid, params->ssid_len);
8154 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
8155 params->ssid);
8156 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
8157 drv->ssid_len = params->ssid_len;
8158
8159 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
8160 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
8161
8162 ret = nl80211_set_conn_keys(params, msg);
8163 if (ret)
8164 goto nla_put_failure;
8165
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008166 if (params->bssid && params->fixed_bssid) {
8167 wpa_printf(MSG_DEBUG, " * BSSID=" MACSTR,
8168 MAC2STR(params->bssid));
8169 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
8170 }
8171
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008172 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
8173 params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
8174 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
8175 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008176 wpa_printf(MSG_DEBUG, " * control port");
8177 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT);
8178 }
8179
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008180 if (params->wpa_ie) {
8181 wpa_hexdump(MSG_DEBUG,
8182 " * Extra IEs for Beacon/Probe Response frames",
8183 params->wpa_ie, params->wpa_ie_len);
8184 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
8185 params->wpa_ie);
8186 }
8187
8188 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8189 msg = NULL;
8190 if (ret) {
8191 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS failed: ret=%d (%s)",
8192 ret, strerror(-ret));
8193 count++;
8194 if (ret == -EALREADY && count == 1) {
8195 wpa_printf(MSG_DEBUG, "nl80211: Retry IBSS join after "
8196 "forced leave");
8197 nl80211_leave_ibss(drv);
8198 nlmsg_free(msg);
8199 goto retry;
8200 }
8201
8202 goto nla_put_failure;
8203 }
8204 ret = 0;
8205 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS request sent successfully");
8206
8207nla_put_failure:
8208 nlmsg_free(msg);
8209 return ret;
8210}
8211
8212
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008213static int nl80211_connect_common(struct wpa_driver_nl80211_data *drv,
8214 struct wpa_driver_associate_params *params,
8215 struct nl_msg *msg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008216{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008217 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008218
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008219 if (params->bssid) {
8220 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
8221 MAC2STR(params->bssid));
8222 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
8223 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008224
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008225 if (params->freq) {
8226 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
8227 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008228 drv->assoc_freq = params->freq;
8229 } else
8230 drv->assoc_freq = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008231
Dmitry Shmidt04949592012-07-19 12:16:46 -07008232 if (params->bg_scan_period >= 0) {
8233 wpa_printf(MSG_DEBUG, " * bg scan period=%d",
8234 params->bg_scan_period);
8235 NLA_PUT_U16(msg, NL80211_ATTR_BG_SCAN_PERIOD,
8236 params->bg_scan_period);
8237 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008238
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008239 if (params->ssid) {
8240 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
8241 params->ssid, params->ssid_len);
8242 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
8243 params->ssid);
8244 if (params->ssid_len > sizeof(drv->ssid))
8245 goto nla_put_failure;
8246 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
8247 drv->ssid_len = params->ssid_len;
8248 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008249
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008250 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
8251 if (params->wpa_ie)
8252 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
8253 params->wpa_ie);
8254
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008255 if (params->wpa_proto) {
8256 enum nl80211_wpa_versions ver = 0;
8257
8258 if (params->wpa_proto & WPA_PROTO_WPA)
8259 ver |= NL80211_WPA_VERSION_1;
8260 if (params->wpa_proto & WPA_PROTO_RSN)
8261 ver |= NL80211_WPA_VERSION_2;
8262
8263 wpa_printf(MSG_DEBUG, " * WPA Versions 0x%x", ver);
8264 NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver);
8265 }
8266
8267 if (params->pairwise_suite != WPA_CIPHER_NONE) {
8268 u32 cipher = wpa_cipher_to_cipher_suite(params->pairwise_suite);
8269 wpa_printf(MSG_DEBUG, " * pairwise=0x%x", cipher);
8270 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE, cipher);
8271 }
8272
8273 if (params->group_suite != WPA_CIPHER_NONE) {
8274 u32 cipher = wpa_cipher_to_cipher_suite(params->group_suite);
8275 wpa_printf(MSG_DEBUG, " * group=0x%x", cipher);
8276 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher);
8277 }
8278
8279 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
8280 params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
8281 params->key_mgmt_suite == WPA_KEY_MGMT_FT_IEEE8021X ||
8282 params->key_mgmt_suite == WPA_KEY_MGMT_FT_PSK ||
8283 params->key_mgmt_suite == WPA_KEY_MGMT_CCKM) {
8284 int mgmt = WLAN_AKM_SUITE_PSK;
8285
8286 switch (params->key_mgmt_suite) {
8287 case WPA_KEY_MGMT_CCKM:
8288 mgmt = WLAN_AKM_SUITE_CCKM;
8289 break;
8290 case WPA_KEY_MGMT_IEEE8021X:
8291 mgmt = WLAN_AKM_SUITE_8021X;
8292 break;
8293 case WPA_KEY_MGMT_FT_IEEE8021X:
8294 mgmt = WLAN_AKM_SUITE_FT_8021X;
8295 break;
8296 case WPA_KEY_MGMT_FT_PSK:
8297 mgmt = WLAN_AKM_SUITE_FT_PSK;
8298 break;
8299 case WPA_KEY_MGMT_PSK:
8300 default:
8301 mgmt = WLAN_AKM_SUITE_PSK;
8302 break;
8303 }
8304 NLA_PUT_U32(msg, NL80211_ATTR_AKM_SUITES, mgmt);
8305 }
8306
8307 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT);
8308
8309 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED)
8310 NLA_PUT_U32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED);
8311
8312 if (params->disable_ht)
8313 NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_HT);
8314
8315 if (params->htcaps && params->htcaps_mask) {
8316 int sz = sizeof(struct ieee80211_ht_capabilities);
8317 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY, sz, params->htcaps);
8318 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY_MASK, sz,
8319 params->htcaps_mask);
8320 }
8321
8322#ifdef CONFIG_VHT_OVERRIDES
8323 if (params->disable_vht) {
8324 wpa_printf(MSG_DEBUG, " * VHT disabled");
8325 NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_VHT);
8326 }
8327
8328 if (params->vhtcaps && params->vhtcaps_mask) {
8329 int sz = sizeof(struct ieee80211_vht_capabilities);
8330 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY, sz, params->vhtcaps);
8331 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY_MASK, sz,
8332 params->vhtcaps_mask);
8333 }
8334#endif /* CONFIG_VHT_OVERRIDES */
8335
8336 if (params->p2p)
8337 wpa_printf(MSG_DEBUG, " * P2P group");
8338
8339 return 0;
8340nla_put_failure:
8341 return -1;
8342}
8343
8344
8345static int wpa_driver_nl80211_try_connect(
8346 struct wpa_driver_nl80211_data *drv,
8347 struct wpa_driver_associate_params *params)
8348{
8349 struct nl_msg *msg;
8350 enum nl80211_auth_type type;
8351 int ret;
8352 int algs;
8353
8354 msg = nlmsg_alloc();
8355 if (!msg)
8356 return -1;
8357
8358 wpa_printf(MSG_DEBUG, "nl80211: Connect (ifindex=%d)", drv->ifindex);
8359 nl80211_cmd(drv, msg, 0, NL80211_CMD_CONNECT);
8360
8361 ret = nl80211_connect_common(drv, params, msg);
8362 if (ret)
8363 goto nla_put_failure;
8364
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008365 algs = 0;
8366 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
8367 algs++;
8368 if (params->auth_alg & WPA_AUTH_ALG_SHARED)
8369 algs++;
8370 if (params->auth_alg & WPA_AUTH_ALG_LEAP)
8371 algs++;
8372 if (algs > 1) {
8373 wpa_printf(MSG_DEBUG, " * Leave out Auth Type for automatic "
8374 "selection");
8375 goto skip_auth_type;
8376 }
8377
8378 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
8379 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
8380 else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
8381 type = NL80211_AUTHTYPE_SHARED_KEY;
8382 else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
8383 type = NL80211_AUTHTYPE_NETWORK_EAP;
8384 else if (params->auth_alg & WPA_AUTH_ALG_FT)
8385 type = NL80211_AUTHTYPE_FT;
8386 else
8387 goto nla_put_failure;
8388
8389 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
8390 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
8391
8392skip_auth_type:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008393 ret = nl80211_set_conn_keys(params, msg);
8394 if (ret)
8395 goto nla_put_failure;
8396
8397 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8398 msg = NULL;
8399 if (ret) {
8400 wpa_printf(MSG_DEBUG, "nl80211: MLME connect failed: ret=%d "
8401 "(%s)", ret, strerror(-ret));
8402 goto nla_put_failure;
8403 }
8404 ret = 0;
8405 wpa_printf(MSG_DEBUG, "nl80211: Connect request send successfully");
8406
8407nla_put_failure:
8408 nlmsg_free(msg);
8409 return ret;
8410
8411}
8412
8413
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08008414static int wpa_driver_nl80211_connect(
8415 struct wpa_driver_nl80211_data *drv,
8416 struct wpa_driver_associate_params *params)
8417{
8418 int ret = wpa_driver_nl80211_try_connect(drv, params);
8419 if (ret == -EALREADY) {
8420 /*
8421 * cfg80211 does not currently accept new connections if
8422 * we are already connected. As a workaround, force
8423 * disconnection and try again.
8424 */
8425 wpa_printf(MSG_DEBUG, "nl80211: Explicitly "
8426 "disconnecting before reassociation "
8427 "attempt");
8428 if (wpa_driver_nl80211_disconnect(
8429 drv, WLAN_REASON_PREV_AUTH_NOT_VALID))
8430 return -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08008431 ret = wpa_driver_nl80211_try_connect(drv, params);
8432 }
8433 return ret;
8434}
8435
8436
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008437static int wpa_driver_nl80211_associate(
8438 void *priv, struct wpa_driver_associate_params *params)
8439{
8440 struct i802_bss *bss = priv;
8441 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008442 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008443 struct nl_msg *msg;
8444
8445 if (params->mode == IEEE80211_MODE_AP)
8446 return wpa_driver_nl80211_ap(drv, params);
8447
8448 if (params->mode == IEEE80211_MODE_IBSS)
8449 return wpa_driver_nl80211_ibss(drv, params);
8450
8451 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008452 enum nl80211_iftype nlmode = params->p2p ?
8453 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
8454
8455 if (wpa_driver_nl80211_set_mode(priv, nlmode) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008456 return -1;
8457 return wpa_driver_nl80211_connect(drv, params);
8458 }
8459
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07008460 nl80211_mark_disconnected(drv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008461
8462 msg = nlmsg_alloc();
8463 if (!msg)
8464 return -1;
8465
8466 wpa_printf(MSG_DEBUG, "nl80211: Associate (ifindex=%d)",
8467 drv->ifindex);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008468 nl80211_cmd(drv, msg, 0, NL80211_CMD_ASSOCIATE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008469
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008470 ret = nl80211_connect_common(drv, params, msg);
8471 if (ret)
8472 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008473
8474 if (params->prev_bssid) {
8475 wpa_printf(MSG_DEBUG, " * prev_bssid=" MACSTR,
8476 MAC2STR(params->prev_bssid));
8477 NLA_PUT(msg, NL80211_ATTR_PREV_BSSID, ETH_ALEN,
8478 params->prev_bssid);
8479 }
8480
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008481 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8482 msg = NULL;
8483 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008484 wpa_dbg(drv->ctx, MSG_DEBUG,
8485 "nl80211: MLME command failed (assoc): ret=%d (%s)",
8486 ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008487 nl80211_dump_scan(drv);
8488 goto nla_put_failure;
8489 }
8490 ret = 0;
8491 wpa_printf(MSG_DEBUG, "nl80211: Association request send "
8492 "successfully");
8493
8494nla_put_failure:
8495 nlmsg_free(msg);
8496 return ret;
8497}
8498
8499
8500static int nl80211_set_mode(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008501 int ifindex, enum nl80211_iftype mode)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008502{
8503 struct nl_msg *msg;
8504 int ret = -ENOBUFS;
8505
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008506 wpa_printf(MSG_DEBUG, "nl80211: Set mode ifindex %d iftype %d (%s)",
8507 ifindex, mode, nl80211_iftype_str(mode));
8508
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008509 msg = nlmsg_alloc();
8510 if (!msg)
8511 return -ENOMEM;
8512
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008513 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_INTERFACE);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008514 if (nl80211_set_iface_id(msg, drv->first_bss) < 0)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008515 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008516 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, mode);
8517
8518 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008519 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008520 if (!ret)
8521 return 0;
8522nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008523 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008524 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface %d to mode %d:"
8525 " %d (%s)", ifindex, mode, ret, strerror(-ret));
8526 return ret;
8527}
8528
8529
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008530static int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
8531 enum nl80211_iftype nlmode)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008532{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008533 struct wpa_driver_nl80211_data *drv = bss->drv;
8534 int ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008535 int i;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008536 int was_ap = is_ap_interface(drv->nlmode);
8537 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008538
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008539 res = nl80211_set_mode(drv, drv->ifindex, nlmode);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008540 if (res && nlmode == nl80211_get_ifmode(bss))
8541 res = 0;
8542
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008543 if (res == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008544 drv->nlmode = nlmode;
8545 ret = 0;
8546 goto done;
8547 }
8548
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008549 if (res == -ENODEV)
8550 return -1;
8551
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008552 if (nlmode == drv->nlmode) {
8553 wpa_printf(MSG_DEBUG, "nl80211: Interface already in "
8554 "requested mode - ignore error");
8555 ret = 0;
8556 goto done; /* Already in the requested mode */
8557 }
8558
8559 /* mac80211 doesn't allow mode changes while the device is up, so
8560 * take the device down, try to set the mode again, and bring the
8561 * device back up.
8562 */
8563 wpa_printf(MSG_DEBUG, "nl80211: Try mode change after setting "
8564 "interface down");
8565 for (i = 0; i < 10; i++) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008566 res = i802_set_iface_flags(bss, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008567 if (res == -EACCES || res == -ENODEV)
8568 break;
8569 if (res == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008570 /* Try to set the mode again while the interface is
8571 * down */
8572 ret = nl80211_set_mode(drv, drv->ifindex, nlmode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008573 if (ret == -EACCES)
8574 break;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008575 res = i802_set_iface_flags(bss, 1);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008576 if (res && !ret)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008577 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008578 else if (ret != -EBUSY)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008579 break;
8580 } else
8581 wpa_printf(MSG_DEBUG, "nl80211: Failed to set "
8582 "interface down");
8583 os_sleep(0, 100000);
8584 }
8585
8586 if (!ret) {
8587 wpa_printf(MSG_DEBUG, "nl80211: Mode change succeeded while "
8588 "interface is down");
8589 drv->nlmode = nlmode;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008590 drv->ignore_if_down_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008591 }
8592
8593done:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008594 if (ret) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008595 wpa_printf(MSG_DEBUG, "nl80211: Interface mode change to %d "
8596 "from %d failed", nlmode, drv->nlmode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008597 return ret;
8598 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008599
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008600 if (is_p2p_net_interface(nlmode))
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008601 nl80211_disable_11b_rates(drv, drv->ifindex, 1);
8602 else if (drv->disabled_11b_rates)
8603 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
8604
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008605 if (is_ap_interface(nlmode)) {
8606 nl80211_mgmt_unsubscribe(bss, "start AP");
8607 /* Setup additional AP mode functionality if needed */
8608 if (nl80211_setup_ap(bss))
8609 return -1;
8610 } else if (was_ap) {
8611 /* Remove additional AP mode functionality */
8612 nl80211_teardown_ap(bss);
8613 } else {
8614 nl80211_mgmt_unsubscribe(bss, "mode change");
8615 }
8616
Dmitry Shmidt04949592012-07-19 12:16:46 -07008617 if (!bss->in_deinit && !is_ap_interface(nlmode) &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008618 nl80211_mgmt_subscribe_non_ap(bss) < 0)
8619 wpa_printf(MSG_DEBUG, "nl80211: Failed to register Action "
8620 "frame processing - ignore for now");
8621
8622 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008623}
8624
8625
8626static int wpa_driver_nl80211_get_capa(void *priv,
8627 struct wpa_driver_capa *capa)
8628{
8629 struct i802_bss *bss = priv;
8630 struct wpa_driver_nl80211_data *drv = bss->drv;
8631 if (!drv->has_capability)
8632 return -1;
8633 os_memcpy(capa, &drv->capa, sizeof(*capa));
Dmitry Shmidt444d5672013-04-01 13:08:44 -07008634 if (drv->extended_capa && drv->extended_capa_mask) {
8635 capa->extended_capa = drv->extended_capa;
8636 capa->extended_capa_mask = drv->extended_capa_mask;
8637 capa->extended_capa_len = drv->extended_capa_len;
8638 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008639
8640 if ((capa->flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
8641 !drv->allow_p2p_device) {
8642 wpa_printf(MSG_DEBUG, "nl80211: Do not indicate P2P_DEVICE support (p2p_device=1 driver param not specified)");
8643 capa->flags &= ~WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE;
8644 }
8645
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008646 return 0;
8647}
8648
8649
8650static int wpa_driver_nl80211_set_operstate(void *priv, int state)
8651{
8652 struct i802_bss *bss = priv;
8653 struct wpa_driver_nl80211_data *drv = bss->drv;
8654
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008655 wpa_printf(MSG_DEBUG, "nl80211: Set %s operstate %d->%d (%s)",
8656 bss->ifname, drv->operstate, state,
8657 state ? "UP" : "DORMANT");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008658 drv->operstate = state;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008659 return netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, -1,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008660 state ? IF_OPER_UP : IF_OPER_DORMANT);
8661}
8662
8663
8664static int wpa_driver_nl80211_set_supp_port(void *priv, int authorized)
8665{
8666 struct i802_bss *bss = priv;
8667 struct wpa_driver_nl80211_data *drv = bss->drv;
8668 struct nl_msg *msg;
8669 struct nl80211_sta_flag_update upd;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008670 int ret = -ENOBUFS;
8671
8672 if (!drv->associated && is_zero_ether_addr(drv->bssid) && !authorized) {
8673 wpa_printf(MSG_DEBUG, "nl80211: Skip set_supp_port(unauthorized) while not associated");
8674 return 0;
8675 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008676
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07008677 wpa_printf(MSG_DEBUG, "nl80211: Set supplicant port %sauthorized for "
8678 MACSTR, authorized ? "" : "un", MAC2STR(drv->bssid));
8679
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008680 msg = nlmsg_alloc();
8681 if (!msg)
8682 return -ENOMEM;
8683
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008684 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008685
8686 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
8687 if_nametoindex(bss->ifname));
8688 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid);
8689
8690 os_memset(&upd, 0, sizeof(upd));
8691 upd.mask = BIT(NL80211_STA_FLAG_AUTHORIZED);
8692 if (authorized)
8693 upd.set = BIT(NL80211_STA_FLAG_AUTHORIZED);
8694 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
8695
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008696 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8697 msg = NULL;
8698 if (!ret)
8699 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008700 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008701 nlmsg_free(msg);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008702 wpa_printf(MSG_DEBUG, "nl80211: Failed to set STA flag: %d (%s)",
8703 ret, strerror(-ret));
8704 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008705}
8706
8707
Jouni Malinen75ecf522011-06-27 15:19:46 -07008708/* Set kernel driver on given frequency (MHz) */
8709static int i802_set_freq(void *priv, struct hostapd_freq_params *freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008710{
Jouni Malinen75ecf522011-06-27 15:19:46 -07008711 struct i802_bss *bss = priv;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008712 return wpa_driver_nl80211_set_freq(bss, freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008713}
8714
8715
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008716static inline int min_int(int a, int b)
8717{
8718 if (a < b)
8719 return a;
8720 return b;
8721}
8722
8723
8724static int get_key_handler(struct nl_msg *msg, void *arg)
8725{
8726 struct nlattr *tb[NL80211_ATTR_MAX + 1];
8727 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
8728
8729 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
8730 genlmsg_attrlen(gnlh, 0), NULL);
8731
8732 /*
8733 * TODO: validate the key index and mac address!
8734 * Otherwise, there's a race condition as soon as
8735 * the kernel starts sending key notifications.
8736 */
8737
8738 if (tb[NL80211_ATTR_KEY_SEQ])
8739 memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]),
8740 min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6));
8741 return NL_SKIP;
8742}
8743
8744
8745static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
8746 int idx, u8 *seq)
8747{
8748 struct i802_bss *bss = priv;
8749 struct wpa_driver_nl80211_data *drv = bss->drv;
8750 struct nl_msg *msg;
8751
8752 msg = nlmsg_alloc();
8753 if (!msg)
8754 return -ENOMEM;
8755
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008756 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_KEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008757
8758 if (addr)
8759 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
8760 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, idx);
8761 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
8762
8763 memset(seq, 0, 6);
8764
8765 return send_and_recv_msgs(drv, msg, get_key_handler, seq);
8766 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008767 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008768 return -ENOBUFS;
8769}
8770
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008771
8772static int i802_set_rts(void *priv, int rts)
8773{
8774 struct i802_bss *bss = priv;
8775 struct wpa_driver_nl80211_data *drv = bss->drv;
8776 struct nl_msg *msg;
8777 int ret = -ENOBUFS;
8778 u32 val;
8779
8780 msg = nlmsg_alloc();
8781 if (!msg)
8782 return -ENOMEM;
8783
8784 if (rts >= 2347)
8785 val = (u32) -1;
8786 else
8787 val = rts;
8788
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008789 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008790 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
8791 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD, val);
8792
8793 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008794 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008795 if (!ret)
8796 return 0;
8797nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008798 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008799 wpa_printf(MSG_DEBUG, "nl80211: Failed to set RTS threshold %d: "
8800 "%d (%s)", rts, ret, strerror(-ret));
8801 return ret;
8802}
8803
8804
8805static int i802_set_frag(void *priv, int frag)
8806{
8807 struct i802_bss *bss = priv;
8808 struct wpa_driver_nl80211_data *drv = bss->drv;
8809 struct nl_msg *msg;
8810 int ret = -ENOBUFS;
8811 u32 val;
8812
8813 msg = nlmsg_alloc();
8814 if (!msg)
8815 return -ENOMEM;
8816
8817 if (frag >= 2346)
8818 val = (u32) -1;
8819 else
8820 val = frag;
8821
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008822 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008823 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
8824 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD, val);
8825
8826 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008827 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008828 if (!ret)
8829 return 0;
8830nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008831 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008832 wpa_printf(MSG_DEBUG, "nl80211: Failed to set fragmentation threshold "
8833 "%d: %d (%s)", frag, ret, strerror(-ret));
8834 return ret;
8835}
8836
8837
8838static int i802_flush(void *priv)
8839{
8840 struct i802_bss *bss = priv;
8841 struct wpa_driver_nl80211_data *drv = bss->drv;
8842 struct nl_msg *msg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008843 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008844
8845 msg = nlmsg_alloc();
8846 if (!msg)
8847 return -1;
8848
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008849 wpa_printf(MSG_DEBUG, "nl80211: flush -> DEL_STATION %s (all)",
8850 bss->ifname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008851 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008852
8853 /*
8854 * XXX: FIX! this needs to flush all VLANs too
8855 */
8856 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
8857 if_nametoindex(bss->ifname));
8858
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008859 res = send_and_recv_msgs(drv, msg, NULL, NULL);
8860 if (res) {
8861 wpa_printf(MSG_DEBUG, "nl80211: Station flush failed: ret=%d "
8862 "(%s)", res, strerror(-res));
8863 }
8864 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008865 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008866 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008867 return -ENOBUFS;
8868}
8869
8870
8871static int get_sta_handler(struct nl_msg *msg, void *arg)
8872{
8873 struct nlattr *tb[NL80211_ATTR_MAX + 1];
8874 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
8875 struct hostap_sta_driver_data *data = arg;
8876 struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
8877 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
8878 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
8879 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
8880 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
8881 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
8882 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03008883 [NL80211_STA_INFO_TX_FAILED] = { .type = NLA_U32 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008884 };
8885
8886 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
8887 genlmsg_attrlen(gnlh, 0), NULL);
8888
8889 /*
8890 * TODO: validate the interface and mac address!
8891 * Otherwise, there's a race condition as soon as
8892 * the kernel starts sending station notifications.
8893 */
8894
8895 if (!tb[NL80211_ATTR_STA_INFO]) {
8896 wpa_printf(MSG_DEBUG, "sta stats missing!");
8897 return NL_SKIP;
8898 }
8899 if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
8900 tb[NL80211_ATTR_STA_INFO],
8901 stats_policy)) {
8902 wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
8903 return NL_SKIP;
8904 }
8905
8906 if (stats[NL80211_STA_INFO_INACTIVE_TIME])
8907 data->inactive_msec =
8908 nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
8909 if (stats[NL80211_STA_INFO_RX_BYTES])
8910 data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
8911 if (stats[NL80211_STA_INFO_TX_BYTES])
8912 data->tx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
8913 if (stats[NL80211_STA_INFO_RX_PACKETS])
8914 data->rx_packets =
8915 nla_get_u32(stats[NL80211_STA_INFO_RX_PACKETS]);
8916 if (stats[NL80211_STA_INFO_TX_PACKETS])
8917 data->tx_packets =
8918 nla_get_u32(stats[NL80211_STA_INFO_TX_PACKETS]);
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03008919 if (stats[NL80211_STA_INFO_TX_FAILED])
8920 data->tx_retry_failed =
8921 nla_get_u32(stats[NL80211_STA_INFO_TX_FAILED]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008922
8923 return NL_SKIP;
8924}
8925
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008926static int i802_read_sta_data(struct i802_bss *bss,
8927 struct hostap_sta_driver_data *data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008928 const u8 *addr)
8929{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008930 struct wpa_driver_nl80211_data *drv = bss->drv;
8931 struct nl_msg *msg;
8932
8933 os_memset(data, 0, sizeof(*data));
8934 msg = nlmsg_alloc();
8935 if (!msg)
8936 return -ENOMEM;
8937
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008938 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008939
8940 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
8941 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
8942
8943 return send_and_recv_msgs(drv, msg, get_sta_handler, data);
8944 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008945 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008946 return -ENOBUFS;
8947}
8948
8949
8950static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
8951 int cw_min, int cw_max, int burst_time)
8952{
8953 struct i802_bss *bss = priv;
8954 struct wpa_driver_nl80211_data *drv = bss->drv;
8955 struct nl_msg *msg;
8956 struct nlattr *txq, *params;
8957
8958 msg = nlmsg_alloc();
8959 if (!msg)
8960 return -1;
8961
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008962 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008963
8964 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
8965
8966 txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS);
8967 if (!txq)
8968 goto nla_put_failure;
8969
8970 /* We are only sending parameters for a single TXQ at a time */
8971 params = nla_nest_start(msg, 1);
8972 if (!params)
8973 goto nla_put_failure;
8974
8975 switch (queue) {
8976 case 0:
8977 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VO);
8978 break;
8979 case 1:
8980 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VI);
8981 break;
8982 case 2:
8983 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BE);
8984 break;
8985 case 3:
8986 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BK);
8987 break;
8988 }
8989 /* Burst time is configured in units of 0.1 msec and TXOP parameter in
8990 * 32 usec, so need to convert the value here. */
8991 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_TXOP, (burst_time * 100 + 16) / 32);
8992 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min);
8993 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max);
8994 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_AIFS, aifs);
8995
8996 nla_nest_end(msg, params);
8997
8998 nla_nest_end(msg, txq);
8999
9000 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
9001 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009002 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009003 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009004 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009005 return -1;
9006}
9007
9008
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009009static int i802_set_sta_vlan(struct i802_bss *bss, const u8 *addr,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009010 const char *ifname, int vlan_id)
9011{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009012 struct wpa_driver_nl80211_data *drv = bss->drv;
9013 struct nl_msg *msg;
9014 int ret = -ENOBUFS;
9015
9016 msg = nlmsg_alloc();
9017 if (!msg)
9018 return -ENOMEM;
9019
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009020 wpa_printf(MSG_DEBUG, "nl80211: %s[%d]: set_sta_vlan(" MACSTR
9021 ", ifname=%s[%d], vlan_id=%d)",
9022 bss->ifname, if_nametoindex(bss->ifname),
9023 MAC2STR(addr), ifname, if_nametoindex(ifname), vlan_id);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009024 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009025
9026 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
9027 if_nametoindex(bss->ifname));
9028 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
9029 NLA_PUT_U32(msg, NL80211_ATTR_STA_VLAN,
9030 if_nametoindex(ifname));
9031
9032 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009033 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009034 if (ret < 0) {
9035 wpa_printf(MSG_ERROR, "nl80211: NL80211_ATTR_STA_VLAN (addr="
9036 MACSTR " ifname=%s vlan_id=%d) failed: %d (%s)",
9037 MAC2STR(addr), ifname, vlan_id, ret,
9038 strerror(-ret));
9039 }
9040 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009041 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009042 return ret;
9043}
9044
9045
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009046static int i802_get_inact_sec(void *priv, const u8 *addr)
9047{
9048 struct hostap_sta_driver_data data;
9049 int ret;
9050
9051 data.inactive_msec = (unsigned long) -1;
9052 ret = i802_read_sta_data(priv, &data, addr);
9053 if (ret || data.inactive_msec == (unsigned long) -1)
9054 return -1;
9055 return data.inactive_msec / 1000;
9056}
9057
9058
9059static int i802_sta_clear_stats(void *priv, const u8 *addr)
9060{
9061#if 0
9062 /* TODO */
9063#endif
9064 return 0;
9065}
9066
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009067
9068static int i802_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
9069 int reason)
9070{
9071 struct i802_bss *bss = priv;
Dmitry Shmidt04949592012-07-19 12:16:46 -07009072 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009073 struct ieee80211_mgmt mgmt;
9074
Dmitry Shmidt04949592012-07-19 12:16:46 -07009075 if (drv->device_ap_sme)
9076 return wpa_driver_nl80211_sta_remove(bss, addr);
9077
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009078 memset(&mgmt, 0, sizeof(mgmt));
9079 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
9080 WLAN_FC_STYPE_DEAUTH);
9081 memcpy(mgmt.da, addr, ETH_ALEN);
9082 memcpy(mgmt.sa, own_addr, ETH_ALEN);
9083 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
9084 mgmt.u.deauth.reason_code = host_to_le16(reason);
9085 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
9086 IEEE80211_HDRLEN +
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009087 sizeof(mgmt.u.deauth), 0, 0, 0, 0,
9088 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009089}
9090
9091
9092static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
9093 int reason)
9094{
9095 struct i802_bss *bss = priv;
Dmitry Shmidt04949592012-07-19 12:16:46 -07009096 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009097 struct ieee80211_mgmt mgmt;
9098
Dmitry Shmidt04949592012-07-19 12:16:46 -07009099 if (drv->device_ap_sme)
9100 return wpa_driver_nl80211_sta_remove(bss, addr);
9101
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009102 memset(&mgmt, 0, sizeof(mgmt));
9103 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
9104 WLAN_FC_STYPE_DISASSOC);
9105 memcpy(mgmt.da, addr, ETH_ALEN);
9106 memcpy(mgmt.sa, own_addr, ETH_ALEN);
9107 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
9108 mgmt.u.disassoc.reason_code = host_to_le16(reason);
9109 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
9110 IEEE80211_HDRLEN +
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009111 sizeof(mgmt.u.disassoc), 0, 0, 0, 0,
9112 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009113}
9114
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009115
Jouni Malinen75ecf522011-06-27 15:19:46 -07009116static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
9117{
9118 int i;
9119 int *old;
9120
9121 wpa_printf(MSG_DEBUG, "nl80211: Add own interface ifindex %d",
9122 ifidx);
9123 for (i = 0; i < drv->num_if_indices; i++) {
9124 if (drv->if_indices[i] == 0) {
9125 drv->if_indices[i] = ifidx;
9126 return;
9127 }
9128 }
9129
9130 if (drv->if_indices != drv->default_if_indices)
9131 old = drv->if_indices;
9132 else
9133 old = NULL;
9134
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07009135 drv->if_indices = os_realloc_array(old, drv->num_if_indices + 1,
9136 sizeof(int));
Jouni Malinen75ecf522011-06-27 15:19:46 -07009137 if (!drv->if_indices) {
9138 if (!old)
9139 drv->if_indices = drv->default_if_indices;
9140 else
9141 drv->if_indices = old;
9142 wpa_printf(MSG_ERROR, "Failed to reallocate memory for "
9143 "interfaces");
9144 wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx);
9145 return;
9146 } else if (!old)
9147 os_memcpy(drv->if_indices, drv->default_if_indices,
9148 sizeof(drv->default_if_indices));
9149 drv->if_indices[drv->num_if_indices] = ifidx;
9150 drv->num_if_indices++;
9151}
9152
9153
9154static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
9155{
9156 int i;
9157
9158 for (i = 0; i < drv->num_if_indices; i++) {
9159 if (drv->if_indices[i] == ifidx) {
9160 drv->if_indices[i] = 0;
9161 break;
9162 }
9163 }
9164}
9165
9166
9167static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
9168{
9169 int i;
9170
9171 for (i = 0; i < drv->num_if_indices; i++)
9172 if (drv->if_indices[i] == ifidx)
9173 return 1;
9174
9175 return 0;
9176}
9177
9178
9179static int i802_set_wds_sta(void *priv, const u8 *addr, int aid, int val,
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07009180 const char *bridge_ifname, char *ifname_wds)
Jouni Malinen75ecf522011-06-27 15:19:46 -07009181{
9182 struct i802_bss *bss = priv;
9183 struct wpa_driver_nl80211_data *drv = bss->drv;
9184 char name[IFNAMSIZ + 1];
9185
9186 os_snprintf(name, sizeof(name), "%s.sta%d", bss->ifname, aid);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07009187 if (ifname_wds)
9188 os_strlcpy(ifname_wds, name, IFNAMSIZ + 1);
9189
Jouni Malinen75ecf522011-06-27 15:19:46 -07009190 wpa_printf(MSG_DEBUG, "nl80211: Set WDS STA addr=" MACSTR
9191 " aid=%d val=%d name=%s", MAC2STR(addr), aid, val, name);
9192 if (val) {
9193 if (!if_nametoindex(name)) {
9194 if (nl80211_create_iface(drv, name,
9195 NL80211_IFTYPE_AP_VLAN,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009196 bss->addr, 1, NULL, NULL, 0) <
9197 0)
Jouni Malinen75ecf522011-06-27 15:19:46 -07009198 return -1;
9199 if (bridge_ifname &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009200 linux_br_add_if(drv->global->ioctl_sock,
9201 bridge_ifname, name) < 0)
Jouni Malinen75ecf522011-06-27 15:19:46 -07009202 return -1;
9203 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07009204 if (linux_set_iface_flags(drv->global->ioctl_sock, name, 1)) {
9205 wpa_printf(MSG_ERROR, "nl80211: Failed to set WDS STA "
9206 "interface %s up", name);
9207 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07009208 return i802_set_sta_vlan(priv, addr, name, 0);
9209 } else {
Dmitry Shmidtaa532512012-09-24 10:35:31 -07009210 if (bridge_ifname)
9211 linux_br_del_if(drv->global->ioctl_sock, bridge_ifname,
9212 name);
9213
Jouni Malinen75ecf522011-06-27 15:19:46 -07009214 i802_set_sta_vlan(priv, addr, bss->ifname, 0);
9215 return wpa_driver_nl80211_if_remove(priv, WPA_IF_AP_VLAN,
9216 name);
9217 }
9218}
9219
9220
9221static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
9222{
9223 struct wpa_driver_nl80211_data *drv = eloop_ctx;
9224 struct sockaddr_ll lladdr;
9225 unsigned char buf[3000];
9226 int len;
9227 socklen_t fromlen = sizeof(lladdr);
9228
9229 len = recvfrom(sock, buf, sizeof(buf), 0,
9230 (struct sockaddr *)&lladdr, &fromlen);
9231 if (len < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07009232 wpa_printf(MSG_ERROR, "nl80211: EAPOL recv failed: %s",
9233 strerror(errno));
Jouni Malinen75ecf522011-06-27 15:19:46 -07009234 return;
9235 }
9236
9237 if (have_ifidx(drv, lladdr.sll_ifindex))
9238 drv_event_eapol_rx(drv->ctx, lladdr.sll_addr, buf, len);
9239}
9240
9241
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009242static int i802_check_bridge(struct wpa_driver_nl80211_data *drv,
9243 struct i802_bss *bss,
9244 const char *brname, const char *ifname)
9245{
9246 int ifindex;
9247 char in_br[IFNAMSIZ];
9248
9249 os_strlcpy(bss->brname, brname, IFNAMSIZ);
9250 ifindex = if_nametoindex(brname);
9251 if (ifindex == 0) {
9252 /*
9253 * Bridge was configured, but the bridge device does
9254 * not exist. Try to add it now.
9255 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009256 if (linux_br_add(drv->global->ioctl_sock, brname) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009257 wpa_printf(MSG_ERROR, "nl80211: Failed to add the "
9258 "bridge interface %s: %s",
9259 brname, strerror(errno));
9260 return -1;
9261 }
9262 bss->added_bridge = 1;
9263 add_ifidx(drv, if_nametoindex(brname));
9264 }
9265
9266 if (linux_br_get(in_br, ifname) == 0) {
9267 if (os_strcmp(in_br, brname) == 0)
9268 return 0; /* already in the bridge */
9269
9270 wpa_printf(MSG_DEBUG, "nl80211: Removing interface %s from "
9271 "bridge %s", ifname, in_br);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009272 if (linux_br_del_if(drv->global->ioctl_sock, in_br, ifname) <
9273 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009274 wpa_printf(MSG_ERROR, "nl80211: Failed to "
9275 "remove interface %s from bridge "
9276 "%s: %s",
9277 ifname, brname, strerror(errno));
9278 return -1;
9279 }
9280 }
9281
9282 wpa_printf(MSG_DEBUG, "nl80211: Adding interface %s into bridge %s",
9283 ifname, brname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009284 if (linux_br_add_if(drv->global->ioctl_sock, brname, ifname) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009285 wpa_printf(MSG_ERROR, "nl80211: Failed to add interface %s "
9286 "into bridge %s: %s",
9287 ifname, brname, strerror(errno));
9288 return -1;
9289 }
9290 bss->added_if_into_bridge = 1;
9291
9292 return 0;
9293}
9294
9295
9296static void *i802_init(struct hostapd_data *hapd,
9297 struct wpa_init_params *params)
9298{
9299 struct wpa_driver_nl80211_data *drv;
9300 struct i802_bss *bss;
9301 size_t i;
9302 char brname[IFNAMSIZ];
9303 int ifindex, br_ifindex;
9304 int br_added = 0;
9305
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08009306 bss = wpa_driver_nl80211_drv_init(hapd, params->ifname,
9307 params->global_priv, 1,
9308 params->bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009309 if (bss == NULL)
9310 return NULL;
9311
9312 drv = bss->drv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009313
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009314 if (linux_br_get(brname, params->ifname) == 0) {
9315 wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in bridge %s",
9316 params->ifname, brname);
9317 br_ifindex = if_nametoindex(brname);
9318 } else {
9319 brname[0] = '\0';
9320 br_ifindex = 0;
9321 }
9322
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009323 for (i = 0; i < params->num_bridge; i++) {
9324 if (params->bridge[i]) {
9325 ifindex = if_nametoindex(params->bridge[i]);
9326 if (ifindex)
9327 add_ifidx(drv, ifindex);
9328 if (ifindex == br_ifindex)
9329 br_added = 1;
9330 }
9331 }
9332 if (!br_added && br_ifindex &&
9333 (params->num_bridge == 0 || !params->bridge[0]))
9334 add_ifidx(drv, br_ifindex);
9335
9336 /* start listening for EAPOL on the default AP interface */
9337 add_ifidx(drv, drv->ifindex);
9338
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009339 if (params->num_bridge && params->bridge[0] &&
9340 i802_check_bridge(drv, bss, params->bridge[0], params->ifname) < 0)
9341 goto failed;
9342
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009343 drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
9344 if (drv->eapol_sock < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07009345 wpa_printf(MSG_ERROR, "nl80211: socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE) failed: %s",
9346 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009347 goto failed;
9348 }
9349
9350 if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
9351 {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07009352 wpa_printf(MSG_INFO, "nl80211: Could not register read socket for eapol");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009353 goto failed;
9354 }
9355
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009356 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
9357 params->own_addr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009358 goto failed;
9359
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009360 memcpy(bss->addr, params->own_addr, ETH_ALEN);
9361
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009362 return bss;
9363
9364failed:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009365 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009366 return NULL;
9367}
9368
9369
9370static void i802_deinit(void *priv)
9371{
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009372 struct i802_bss *bss = priv;
9373 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009374}
9375
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009376
9377static enum nl80211_iftype wpa_driver_nl80211_if_type(
9378 enum wpa_driver_if_type type)
9379{
9380 switch (type) {
9381 case WPA_IF_STATION:
9382 return NL80211_IFTYPE_STATION;
9383 case WPA_IF_P2P_CLIENT:
9384 case WPA_IF_P2P_GROUP:
9385 return NL80211_IFTYPE_P2P_CLIENT;
9386 case WPA_IF_AP_VLAN:
9387 return NL80211_IFTYPE_AP_VLAN;
9388 case WPA_IF_AP_BSS:
9389 return NL80211_IFTYPE_AP;
9390 case WPA_IF_P2P_GO:
9391 return NL80211_IFTYPE_P2P_GO;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009392 case WPA_IF_P2P_DEVICE:
9393 return NL80211_IFTYPE_P2P_DEVICE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009394 }
9395 return -1;
9396}
9397
9398
9399#ifdef CONFIG_P2P
9400
9401static int nl80211_addr_in_use(struct nl80211_global *global, const u8 *addr)
9402{
9403 struct wpa_driver_nl80211_data *drv;
9404 dl_list_for_each(drv, &global->interfaces,
9405 struct wpa_driver_nl80211_data, list) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009406 if (os_memcmp(addr, drv->first_bss->addr, ETH_ALEN) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009407 return 1;
9408 }
9409 return 0;
9410}
9411
9412
9413static int nl80211_p2p_interface_addr(struct wpa_driver_nl80211_data *drv,
9414 u8 *new_addr)
9415{
9416 unsigned int idx;
9417
9418 if (!drv->global)
9419 return -1;
9420
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009421 os_memcpy(new_addr, drv->first_bss->addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009422 for (idx = 0; idx < 64; idx++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009423 new_addr[0] = drv->first_bss->addr[0] | 0x02;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009424 new_addr[0] ^= idx << 2;
9425 if (!nl80211_addr_in_use(drv->global, new_addr))
9426 break;
9427 }
9428 if (idx == 64)
9429 return -1;
9430
9431 wpa_printf(MSG_DEBUG, "nl80211: Assigned new P2P Interface Address "
9432 MACSTR, MAC2STR(new_addr));
9433
9434 return 0;
9435}
9436
9437#endif /* CONFIG_P2P */
9438
9439
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009440struct wdev_info {
9441 u64 wdev_id;
9442 int wdev_id_set;
9443 u8 macaddr[ETH_ALEN];
9444};
9445
9446static int nl80211_wdev_handler(struct nl_msg *msg, void *arg)
9447{
9448 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
9449 struct nlattr *tb[NL80211_ATTR_MAX + 1];
9450 struct wdev_info *wi = arg;
9451
9452 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9453 genlmsg_attrlen(gnlh, 0), NULL);
9454 if (tb[NL80211_ATTR_WDEV]) {
9455 wi->wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
9456 wi->wdev_id_set = 1;
9457 }
9458
9459 if (tb[NL80211_ATTR_MAC])
9460 os_memcpy(wi->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
9461 ETH_ALEN);
9462
9463 return NL_SKIP;
9464}
9465
9466
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009467static int wpa_driver_nl80211_if_add(void *priv, enum wpa_driver_if_type type,
9468 const char *ifname, const u8 *addr,
9469 void *bss_ctx, void **drv_priv,
9470 char *force_ifname, u8 *if_addr,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009471 const char *bridge, int use_existing)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009472{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009473 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009474 struct i802_bss *bss = priv;
9475 struct wpa_driver_nl80211_data *drv = bss->drv;
9476 int ifidx;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009477 int added = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009478
9479 if (addr)
9480 os_memcpy(if_addr, addr, ETH_ALEN);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009481 nlmode = wpa_driver_nl80211_if_type(type);
9482 if (nlmode == NL80211_IFTYPE_P2P_DEVICE) {
9483 struct wdev_info p2pdev_info;
9484
9485 os_memset(&p2pdev_info, 0, sizeof(p2pdev_info));
9486 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
9487 0, nl80211_wdev_handler,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009488 &p2pdev_info, use_existing);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009489 if (!p2pdev_info.wdev_id_set || ifidx != 0) {
9490 wpa_printf(MSG_ERROR, "nl80211: Failed to create a P2P Device interface %s",
9491 ifname);
9492 return -1;
9493 }
9494
9495 drv->global->if_add_wdevid = p2pdev_info.wdev_id;
9496 drv->global->if_add_wdevid_set = p2pdev_info.wdev_id_set;
9497 if (!is_zero_ether_addr(p2pdev_info.macaddr))
9498 os_memcpy(if_addr, p2pdev_info.macaddr, ETH_ALEN);
9499 wpa_printf(MSG_DEBUG, "nl80211: New P2P Device interface %s (0x%llx) created",
9500 ifname,
9501 (long long unsigned int) p2pdev_info.wdev_id);
9502 } else {
9503 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009504 0, NULL, NULL, use_existing);
9505 if (use_existing && ifidx == -ENFILE) {
9506 added = 0;
9507 ifidx = if_nametoindex(ifname);
9508 } else if (ifidx < 0) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009509 return -1;
9510 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009511 }
9512
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009513 if (!addr) {
9514 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
9515 os_memcpy(if_addr, bss->addr, ETH_ALEN);
9516 else if (linux_get_ifhwaddr(drv->global->ioctl_sock,
9517 bss->ifname, if_addr) < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009518 if (added)
9519 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009520 return -1;
9521 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009522 }
9523
9524#ifdef CONFIG_P2P
9525 if (!addr &&
9526 (type == WPA_IF_P2P_CLIENT || type == WPA_IF_P2P_GROUP ||
9527 type == WPA_IF_P2P_GO)) {
9528 /* Enforce unique P2P Interface Address */
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009529 u8 new_addr[ETH_ALEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009530
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009531 if (linux_get_ifhwaddr(drv->global->ioctl_sock, ifname,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009532 new_addr) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009533 nl80211_remove_iface(drv, ifidx);
9534 return -1;
9535 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009536 if (nl80211_addr_in_use(drv->global, new_addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009537 wpa_printf(MSG_DEBUG, "nl80211: Allocate new address "
9538 "for P2P group interface");
9539 if (nl80211_p2p_interface_addr(drv, new_addr) < 0) {
9540 nl80211_remove_iface(drv, ifidx);
9541 return -1;
9542 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009543 if (linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009544 new_addr) < 0) {
9545 nl80211_remove_iface(drv, ifidx);
9546 return -1;
9547 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009548 }
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07009549 os_memcpy(if_addr, new_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009550 }
9551#endif /* CONFIG_P2P */
9552
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009553 if (type == WPA_IF_AP_BSS) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07009554 struct i802_bss *new_bss = os_zalloc(sizeof(*new_bss));
9555 if (new_bss == NULL) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009556 if (added)
9557 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07009558 return -1;
9559 }
9560
9561 if (bridge &&
9562 i802_check_bridge(drv, new_bss, bridge, ifname) < 0) {
9563 wpa_printf(MSG_ERROR, "nl80211: Failed to add the new "
9564 "interface %s to a bridge %s",
9565 ifname, bridge);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009566 if (added)
9567 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07009568 os_free(new_bss);
9569 return -1;
9570 }
9571
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009572 if (linux_set_iface_flags(drv->global->ioctl_sock, ifname, 1))
9573 {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009574 nl80211_remove_iface(drv, ifidx);
9575 os_free(new_bss);
9576 return -1;
9577 }
9578 os_strlcpy(new_bss->ifname, ifname, IFNAMSIZ);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009579 os_memcpy(new_bss->addr, if_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009580 new_bss->ifindex = ifidx;
9581 new_bss->drv = drv;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009582 new_bss->next = drv->first_bss->next;
9583 new_bss->freq = drv->first_bss->freq;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08009584 new_bss->ctx = bss_ctx;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009585 new_bss->added_if = added;
9586 drv->first_bss->next = new_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009587 if (drv_priv)
9588 *drv_priv = new_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009589 nl80211_init_bss(new_bss);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08009590
9591 /* Subscribe management frames for this WPA_IF_AP_BSS */
9592 if (nl80211_setup_ap(new_bss))
9593 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009594 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009595
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009596 if (drv->global)
9597 drv->global->if_add_ifindex = ifidx;
9598
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009599 return 0;
9600}
9601
9602
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009603static int wpa_driver_nl80211_if_remove(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009604 enum wpa_driver_if_type type,
9605 const char *ifname)
9606{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009607 struct wpa_driver_nl80211_data *drv = bss->drv;
9608 int ifindex = if_nametoindex(ifname);
9609
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009610 wpa_printf(MSG_DEBUG, "nl80211: %s(type=%d ifname=%s) ifindex=%d added_if=%d",
9611 __func__, type, ifname, ifindex, bss->added_if);
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08009612 if (ifindex > 0 && (bss->added_if || bss->ifindex != ifindex))
Dmitry Shmidt051af732013-10-22 13:52:46 -07009613 nl80211_remove_iface(drv, ifindex);
Dmitry Shmidtaa532512012-09-24 10:35:31 -07009614
Dmitry Shmidtaa532512012-09-24 10:35:31 -07009615 if (type != WPA_IF_AP_BSS)
9616 return 0;
9617
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009618 if (bss->added_if_into_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009619 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
9620 bss->ifname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009621 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
9622 "interface %s from bridge %s: %s",
9623 bss->ifname, bss->brname, strerror(errno));
9624 }
9625 if (bss->added_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009626 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009627 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
9628 "bridge %s: %s",
9629 bss->brname, strerror(errno));
9630 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009631
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009632 if (bss != drv->first_bss) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009633 struct i802_bss *tbss;
9634
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009635 wpa_printf(MSG_DEBUG, "nl80211: Not the first BSS - remove it");
9636 for (tbss = drv->first_bss; tbss; tbss = tbss->next) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009637 if (tbss->next == bss) {
9638 tbss->next = bss->next;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08009639 /* Unsubscribe management frames */
9640 nl80211_teardown_ap(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009641 nl80211_destroy_bss(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009642 os_free(bss);
9643 bss = NULL;
9644 break;
9645 }
9646 }
9647 if (bss)
9648 wpa_printf(MSG_INFO, "nl80211: %s - could not find "
9649 "BSS %p in the list", __func__, bss);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009650 } else {
9651 wpa_printf(MSG_DEBUG, "nl80211: First BSS - reassign context");
9652 nl80211_teardown_ap(bss);
9653 if (!bss->added_if && !drv->first_bss->next)
9654 wpa_driver_nl80211_del_beacon(drv);
9655 nl80211_destroy_bss(bss);
9656 if (!bss->added_if)
9657 i802_set_iface_flags(bss, 0);
9658 if (drv->first_bss->next) {
9659 drv->first_bss = drv->first_bss->next;
9660 drv->ctx = drv->first_bss->ctx;
9661 os_free(bss);
9662 } else {
9663 wpa_printf(MSG_DEBUG, "nl80211: No second BSS to reassign context to");
9664 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009665 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009666
9667 return 0;
9668}
9669
9670
9671static int cookie_handler(struct nl_msg *msg, void *arg)
9672{
9673 struct nlattr *tb[NL80211_ATTR_MAX + 1];
9674 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
9675 u64 *cookie = arg;
9676 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9677 genlmsg_attrlen(gnlh, 0), NULL);
9678 if (tb[NL80211_ATTR_COOKIE])
9679 *cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
9680 return NL_SKIP;
9681}
9682
9683
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009684static int nl80211_send_frame_cmd(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009685 unsigned int freq, unsigned int wait,
9686 const u8 *buf, size_t buf_len,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009687 u64 *cookie_out, int no_cck, int no_ack,
9688 int offchanok)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009689{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009690 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009691 struct nl_msg *msg;
9692 u64 cookie;
9693 int ret = -1;
9694
9695 msg = nlmsg_alloc();
9696 if (!msg)
9697 return -1;
9698
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07009699 wpa_printf(MSG_MSGDUMP, "nl80211: CMD_FRAME freq=%u wait=%u no_cck=%d "
Dmitry Shmidt04949592012-07-19 12:16:46 -07009700 "no_ack=%d offchanok=%d",
9701 freq, wait, no_cck, no_ack, offchanok);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07009702 wpa_hexdump(MSG_MSGDUMP, "CMD_FRAME", buf, buf_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009703 nl80211_cmd(drv, msg, 0, NL80211_CMD_FRAME);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009704
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009705 if (nl80211_set_iface_id(msg, bss) < 0)
9706 goto nla_put_failure;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07009707 if (freq)
9708 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07009709 if (wait)
9710 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, wait);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08009711 if (offchanok && (drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009712 NLA_PUT_FLAG(msg, NL80211_ATTR_OFFCHANNEL_TX_OK);
9713 if (no_cck)
9714 NLA_PUT_FLAG(msg, NL80211_ATTR_TX_NO_CCK_RATE);
9715 if (no_ack)
9716 NLA_PUT_FLAG(msg, NL80211_ATTR_DONT_WAIT_FOR_ACK);
9717
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009718 NLA_PUT(msg, NL80211_ATTR_FRAME, buf_len, buf);
9719
9720 cookie = 0;
9721 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
9722 msg = NULL;
9723 if (ret) {
9724 wpa_printf(MSG_DEBUG, "nl80211: Frame command failed: ret=%d "
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07009725 "(%s) (freq=%u wait=%u)", ret, strerror(-ret),
9726 freq, wait);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009727 goto nla_put_failure;
9728 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07009729 wpa_printf(MSG_MSGDUMP, "nl80211: Frame TX command accepted%s; "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009730 "cookie 0x%llx", no_ack ? " (no ACK)" : "",
9731 (long long unsigned int) cookie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009732
9733 if (cookie_out)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009734 *cookie_out = no_ack ? (u64) -1 : cookie;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009735
9736nla_put_failure:
9737 nlmsg_free(msg);
9738 return ret;
9739}
9740
9741
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009742static int wpa_driver_nl80211_send_action(struct i802_bss *bss,
9743 unsigned int freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009744 unsigned int wait_time,
9745 const u8 *dst, const u8 *src,
9746 const u8 *bssid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009747 const u8 *data, size_t data_len,
9748 int no_cck)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009749{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009750 struct wpa_driver_nl80211_data *drv = bss->drv;
9751 int ret = -1;
9752 u8 *buf;
9753 struct ieee80211_hdr *hdr;
9754
9755 wpa_printf(MSG_DEBUG, "nl80211: Send Action frame (ifindex=%d, "
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08009756 "freq=%u MHz wait=%d ms no_cck=%d)",
9757 drv->ifindex, freq, wait_time, no_cck);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009758
9759 buf = os_zalloc(24 + data_len);
9760 if (buf == NULL)
9761 return ret;
9762 os_memcpy(buf + 24, data, data_len);
9763 hdr = (struct ieee80211_hdr *) buf;
9764 hdr->frame_control =
9765 IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_ACTION);
9766 os_memcpy(hdr->addr1, dst, ETH_ALEN);
9767 os_memcpy(hdr->addr2, src, ETH_ALEN);
9768 os_memcpy(hdr->addr3, bssid, ETH_ALEN);
9769
Dmitry Shmidt56052862013-10-04 10:23:25 -07009770 if (is_ap_interface(drv->nlmode) &&
9771 (!(drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
9772 (int) freq == bss->freq || drv->device_ap_sme ||
9773 !drv->use_monitor))
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009774 ret = wpa_driver_nl80211_send_mlme(bss, buf, 24 + data_len,
9775 0, freq, no_cck, 1,
9776 wait_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009777 else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009778 ret = nl80211_send_frame_cmd(bss, freq, wait_time, buf,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009779 24 + data_len,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009780 &drv->send_action_cookie,
9781 no_cck, 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009782
9783 os_free(buf);
9784 return ret;
9785}
9786
9787
9788static void wpa_driver_nl80211_send_action_cancel_wait(void *priv)
9789{
9790 struct i802_bss *bss = priv;
9791 struct wpa_driver_nl80211_data *drv = bss->drv;
9792 struct nl_msg *msg;
9793 int ret;
9794
9795 msg = nlmsg_alloc();
9796 if (!msg)
9797 return;
9798
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08009799 wpa_printf(MSG_DEBUG, "nl80211: Cancel TX frame wait: cookie=0x%llx",
9800 (long long unsigned int) drv->send_action_cookie);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009801 nl80211_cmd(drv, msg, 0, NL80211_CMD_FRAME_WAIT_CANCEL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009802
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009803 if (nl80211_set_iface_id(msg, bss) < 0)
9804 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009805 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->send_action_cookie);
9806
9807 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9808 msg = NULL;
9809 if (ret)
9810 wpa_printf(MSG_DEBUG, "nl80211: wait cancel failed: ret=%d "
9811 "(%s)", ret, strerror(-ret));
9812
9813 nla_put_failure:
9814 nlmsg_free(msg);
9815}
9816
9817
9818static int wpa_driver_nl80211_remain_on_channel(void *priv, unsigned int freq,
9819 unsigned int duration)
9820{
9821 struct i802_bss *bss = priv;
9822 struct wpa_driver_nl80211_data *drv = bss->drv;
9823 struct nl_msg *msg;
9824 int ret;
9825 u64 cookie;
9826
9827 msg = nlmsg_alloc();
9828 if (!msg)
9829 return -1;
9830
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009831 nl80211_cmd(drv, msg, 0, NL80211_CMD_REMAIN_ON_CHANNEL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009832
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009833 if (nl80211_set_iface_id(msg, bss) < 0)
9834 goto nla_put_failure;
9835
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009836 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
9837 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
9838
9839 cookie = 0;
9840 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009841 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009842 if (ret == 0) {
9843 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel cookie "
9844 "0x%llx for freq=%u MHz duration=%u",
9845 (long long unsigned int) cookie, freq, duration);
9846 drv->remain_on_chan_cookie = cookie;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009847 drv->pending_remain_on_chan = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009848 return 0;
9849 }
9850 wpa_printf(MSG_DEBUG, "nl80211: Failed to request remain-on-channel "
9851 "(freq=%d duration=%u): %d (%s)",
9852 freq, duration, ret, strerror(-ret));
9853nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009854 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009855 return -1;
9856}
9857
9858
9859static int wpa_driver_nl80211_cancel_remain_on_channel(void *priv)
9860{
9861 struct i802_bss *bss = priv;
9862 struct wpa_driver_nl80211_data *drv = bss->drv;
9863 struct nl_msg *msg;
9864 int ret;
9865
9866 if (!drv->pending_remain_on_chan) {
9867 wpa_printf(MSG_DEBUG, "nl80211: No pending remain-on-channel "
9868 "to cancel");
9869 return -1;
9870 }
9871
9872 wpa_printf(MSG_DEBUG, "nl80211: Cancel remain-on-channel with cookie "
9873 "0x%llx",
9874 (long long unsigned int) drv->remain_on_chan_cookie);
9875
9876 msg = nlmsg_alloc();
9877 if (!msg)
9878 return -1;
9879
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009880 nl80211_cmd(drv, msg, 0, NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009881
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009882 if (nl80211_set_iface_id(msg, bss) < 0)
9883 goto nla_put_failure;
9884
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009885 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->remain_on_chan_cookie);
9886
9887 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009888 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009889 if (ret == 0)
9890 return 0;
9891 wpa_printf(MSG_DEBUG, "nl80211: Failed to cancel remain-on-channel: "
9892 "%d (%s)", ret, strerror(-ret));
9893nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009894 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009895 return -1;
9896}
9897
9898
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009899static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss, int report)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009900{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009901 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07009902
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009903 if (!report) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009904 if (bss->nl_preq && drv->device_ap_sme &&
9905 is_ap_interface(drv->nlmode)) {
9906 /*
9907 * Do not disable Probe Request reporting that was
9908 * enabled in nl80211_setup_ap().
9909 */
9910 wpa_printf(MSG_DEBUG, "nl80211: Skip disabling of "
9911 "Probe Request reporting nl_preq=%p while "
9912 "in AP mode", bss->nl_preq);
9913 } else if (bss->nl_preq) {
9914 wpa_printf(MSG_DEBUG, "nl80211: Disable Probe Request "
9915 "reporting nl_preq=%p", bss->nl_preq);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07009916 nl80211_destroy_eloop_handle(&bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009917 }
9918 return 0;
9919 }
9920
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009921 if (bss->nl_preq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009922 wpa_printf(MSG_DEBUG, "nl80211: Probe Request reporting "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009923 "already on! nl_preq=%p", bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009924 return 0;
9925 }
9926
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009927 bss->nl_preq = nl_create_handle(drv->global->nl_cb, "preq");
9928 if (bss->nl_preq == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009929 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009930 wpa_printf(MSG_DEBUG, "nl80211: Enable Probe Request "
9931 "reporting nl_preq=%p", bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009932
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009933 if (nl80211_register_frame(bss, bss->nl_preq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009934 (WLAN_FC_TYPE_MGMT << 2) |
9935 (WLAN_FC_STYPE_PROBE_REQ << 4),
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009936 NULL, 0) < 0)
9937 goto out_err;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07009938
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07009939 nl80211_register_eloop_read(&bss->nl_preq,
9940 wpa_driver_nl80211_event_receive,
9941 bss->nl_cb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009942
9943 return 0;
9944
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009945 out_err:
9946 nl_destroy_handles(&bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009947 return -1;
9948}
9949
9950
9951static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
9952 int ifindex, int disabled)
9953{
9954 struct nl_msg *msg;
9955 struct nlattr *bands, *band;
9956 int ret;
9957
9958 msg = nlmsg_alloc();
9959 if (!msg)
9960 return -1;
9961
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009962 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_TX_BITRATE_MASK);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009963 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
9964
9965 bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES);
9966 if (!bands)
9967 goto nla_put_failure;
9968
9969 /*
9970 * Disable 2 GHz rates 1, 2, 5.5, 11 Mbps by masking out everything
9971 * else apart from 6, 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS
9972 * rates. All 5 GHz rates are left enabled.
9973 */
9974 band = nla_nest_start(msg, NL80211_BAND_2GHZ);
9975 if (!band)
9976 goto nla_put_failure;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009977 if (disabled) {
9978 NLA_PUT(msg, NL80211_TXRATE_LEGACY, 8,
9979 "\x0c\x12\x18\x24\x30\x48\x60\x6c");
9980 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009981 nla_nest_end(msg, band);
9982
9983 nla_nest_end(msg, bands);
9984
9985 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9986 msg = NULL;
9987 if (ret) {
9988 wpa_printf(MSG_DEBUG, "nl80211: Set TX rates failed: ret=%d "
9989 "(%s)", ret, strerror(-ret));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07009990 } else
9991 drv->disabled_11b_rates = disabled;
9992
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009993 return ret;
9994
9995nla_put_failure:
9996 nlmsg_free(msg);
9997 return -1;
9998}
9999
10000
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010001static int wpa_driver_nl80211_deinit_ap(void *priv)
10002{
10003 struct i802_bss *bss = priv;
10004 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010005 if (!is_ap_interface(drv->nlmode))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010006 return -1;
10007 wpa_driver_nl80211_del_beacon(drv);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070010008
10009 /*
10010 * If the P2P GO interface was dynamically added, then it is
10011 * possible that the interface change to station is not possible.
10012 */
10013 if (drv->nlmode == NL80211_IFTYPE_P2P_GO && bss->if_dynamic)
10014 return 0;
10015
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010016 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010017}
10018
10019
Dmitry Shmidtea69e842013-05-13 14:52:28 -070010020static int wpa_driver_nl80211_stop_ap(void *priv)
10021{
10022 struct i802_bss *bss = priv;
10023 struct wpa_driver_nl80211_data *drv = bss->drv;
10024 if (!is_ap_interface(drv->nlmode))
10025 return -1;
10026 wpa_driver_nl80211_del_beacon(drv);
10027 bss->beacon_set = 0;
10028 return 0;
10029}
10030
10031
Dmitry Shmidt04949592012-07-19 12:16:46 -070010032static int wpa_driver_nl80211_deinit_p2p_cli(void *priv)
10033{
10034 struct i802_bss *bss = priv;
10035 struct wpa_driver_nl80211_data *drv = bss->drv;
10036 if (drv->nlmode != NL80211_IFTYPE_P2P_CLIENT)
10037 return -1;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070010038
10039 /*
10040 * If the P2P Client interface was dynamically added, then it is
10041 * possible that the interface change to station is not possible.
10042 */
10043 if (bss->if_dynamic)
10044 return 0;
10045
Dmitry Shmidt04949592012-07-19 12:16:46 -070010046 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
10047}
10048
10049
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010050static void wpa_driver_nl80211_resume(void *priv)
10051{
10052 struct i802_bss *bss = priv;
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010053
10054 if (i802_set_iface_flags(bss, 1))
10055 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface up on resume event");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010056}
10057
10058
10059static int nl80211_send_ft_action(void *priv, u8 action, const u8 *target_ap,
10060 const u8 *ies, size_t ies_len)
10061{
10062 struct i802_bss *bss = priv;
10063 struct wpa_driver_nl80211_data *drv = bss->drv;
10064 int ret;
10065 u8 *data, *pos;
10066 size_t data_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010067 const u8 *own_addr = bss->addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010068
10069 if (action != 1) {
10070 wpa_printf(MSG_ERROR, "nl80211: Unsupported send_ft_action "
10071 "action %d", action);
10072 return -1;
10073 }
10074
10075 /*
10076 * Action frame payload:
10077 * Category[1] = 6 (Fast BSS Transition)
10078 * Action[1] = 1 (Fast BSS Transition Request)
10079 * STA Address
10080 * Target AP Address
10081 * FT IEs
10082 */
10083
10084 data_len = 2 + 2 * ETH_ALEN + ies_len;
10085 data = os_malloc(data_len);
10086 if (data == NULL)
10087 return -1;
10088 pos = data;
10089 *pos++ = 0x06; /* FT Action category */
10090 *pos++ = action;
10091 os_memcpy(pos, own_addr, ETH_ALEN);
10092 pos += ETH_ALEN;
10093 os_memcpy(pos, target_ap, ETH_ALEN);
10094 pos += ETH_ALEN;
10095 os_memcpy(pos, ies, ies_len);
10096
10097 ret = wpa_driver_nl80211_send_action(bss, drv->assoc_freq, 0,
10098 drv->bssid, own_addr, drv->bssid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010099 data, data_len, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010100 os_free(data);
10101
10102 return ret;
10103}
10104
10105
10106static int nl80211_signal_monitor(void *priv, int threshold, int hysteresis)
10107{
10108 struct i802_bss *bss = priv;
10109 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -070010110 struct nl_msg *msg;
10111 struct nlattr *cqm;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070010112 int ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010113
10114 wpa_printf(MSG_DEBUG, "nl80211: Signal monitor threshold=%d "
10115 "hysteresis=%d", threshold, hysteresis);
10116
10117 msg = nlmsg_alloc();
10118 if (!msg)
10119 return -1;
10120
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010121 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_CQM);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010122
10123 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
10124
Dmitry Shmidt8da800a2013-04-24 12:57:01 -070010125 cqm = nla_nest_start(msg, NL80211_ATTR_CQM);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010126 if (cqm == NULL)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070010127 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010128
Dmitry Shmidt8da800a2013-04-24 12:57:01 -070010129 NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_THOLD, threshold);
10130 NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_HYST, hysteresis);
10131 nla_nest_end(msg, cqm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010132
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070010133 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010134 msg = NULL;
10135
10136nla_put_failure:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010137 nlmsg_free(msg);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070010138 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010139}
10140
10141
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010142static int get_channel_width(struct nl_msg *msg, void *arg)
10143{
10144 struct nlattr *tb[NL80211_ATTR_MAX + 1];
10145 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
10146 struct wpa_signal_info *sig_change = arg;
10147
10148 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
10149 genlmsg_attrlen(gnlh, 0), NULL);
10150
10151 sig_change->center_frq1 = -1;
10152 sig_change->center_frq2 = -1;
10153 sig_change->chanwidth = CHAN_WIDTH_UNKNOWN;
10154
10155 if (tb[NL80211_ATTR_CHANNEL_WIDTH]) {
10156 sig_change->chanwidth = convert2width(
10157 nla_get_u32(tb[NL80211_ATTR_CHANNEL_WIDTH]));
10158 if (tb[NL80211_ATTR_CENTER_FREQ1])
10159 sig_change->center_frq1 =
10160 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]);
10161 if (tb[NL80211_ATTR_CENTER_FREQ2])
10162 sig_change->center_frq2 =
10163 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]);
10164 }
10165
10166 return NL_SKIP;
10167}
10168
10169
10170static int nl80211_get_channel_width(struct wpa_driver_nl80211_data *drv,
10171 struct wpa_signal_info *sig)
10172{
10173 struct nl_msg *msg;
10174
10175 msg = nlmsg_alloc();
10176 if (!msg)
10177 return -ENOMEM;
10178
10179 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_INTERFACE);
10180 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
10181
10182 return send_and_recv_msgs(drv, msg, get_channel_width, sig);
10183
10184nla_put_failure:
10185 nlmsg_free(msg);
10186 return -ENOBUFS;
10187}
10188
10189
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010190static int nl80211_signal_poll(void *priv, struct wpa_signal_info *si)
10191{
10192 struct i802_bss *bss = priv;
10193 struct wpa_driver_nl80211_data *drv = bss->drv;
10194 int res;
10195
10196 os_memset(si, 0, sizeof(*si));
10197 res = nl80211_get_link_signal(drv, si);
10198 if (res != 0)
10199 return res;
10200
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010201 res = nl80211_get_channel_width(drv, si);
10202 if (res != 0)
10203 return res;
10204
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010205 return nl80211_get_link_noise(drv, si);
10206}
10207
10208
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010209static int wpa_driver_nl80211_shared_freq(void *priv)
10210{
10211 struct i802_bss *bss = priv;
10212 struct wpa_driver_nl80211_data *drv = bss->drv;
10213 struct wpa_driver_nl80211_data *driver;
10214 int freq = 0;
10215
10216 /*
10217 * If the same PHY is in connected state with some other interface,
10218 * then retrieve the assoc freq.
10219 */
10220 wpa_printf(MSG_DEBUG, "nl80211: Get shared freq for PHY %s",
10221 drv->phyname);
10222
10223 dl_list_for_each(driver, &drv->global->interfaces,
10224 struct wpa_driver_nl80211_data, list) {
10225 if (drv == driver ||
10226 os_strcmp(drv->phyname, driver->phyname) != 0 ||
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010227 !driver->associated)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010228 continue;
10229
10230 wpa_printf(MSG_DEBUG, "nl80211: Found a match for PHY %s - %s "
10231 MACSTR,
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010232 driver->phyname, driver->first_bss->ifname,
10233 MAC2STR(driver->first_bss->addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -070010234 if (is_ap_interface(driver->nlmode))
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010235 freq = driver->first_bss->freq;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010236 else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010237 freq = nl80211_get_assoc_freq(driver);
10238 wpa_printf(MSG_DEBUG, "nl80211: Shared freq for PHY %s: %d",
10239 drv->phyname, freq);
10240 }
10241
10242 if (!freq)
10243 wpa_printf(MSG_DEBUG, "nl80211: No shared interface for "
10244 "PHY (%s) in associated state", drv->phyname);
10245
10246 return freq;
10247}
10248
10249
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010250static int nl80211_send_frame(void *priv, const u8 *data, size_t data_len,
10251 int encrypt)
10252{
10253 struct i802_bss *bss = priv;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080010254 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt, 0,
10255 0, 0, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010256}
10257
10258
10259static int nl80211_set_param(void *priv, const char *param)
10260{
10261 wpa_printf(MSG_DEBUG, "nl80211: driver param='%s'", param);
10262 if (param == NULL)
10263 return 0;
10264
10265#ifdef CONFIG_P2P
10266 if (os_strstr(param, "use_p2p_group_interface=1")) {
10267 struct i802_bss *bss = priv;
10268 struct wpa_driver_nl80211_data *drv = bss->drv;
10269
10270 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
10271 "interface");
10272 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
10273 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
10274 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010275
10276 if (os_strstr(param, "p2p_device=1")) {
10277 struct i802_bss *bss = priv;
10278 struct wpa_driver_nl80211_data *drv = bss->drv;
10279 drv->allow_p2p_device = 1;
10280 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010281#endif /* CONFIG_P2P */
10282
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080010283 if (os_strstr(param, "use_monitor=1")) {
10284 struct i802_bss *bss = priv;
10285 struct wpa_driver_nl80211_data *drv = bss->drv;
10286 drv->use_monitor = 1;
10287 }
10288
10289 if (os_strstr(param, "force_connect_cmd=1")) {
10290 struct i802_bss *bss = priv;
10291 struct wpa_driver_nl80211_data *drv = bss->drv;
10292 drv->capa.flags &= ~WPA_DRIVER_FLAGS_SME;
10293 }
10294
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010295 return 0;
10296}
10297
10298
10299static void * nl80211_global_init(void)
10300{
10301 struct nl80211_global *global;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010302 struct netlink_config *cfg;
10303
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010304 global = os_zalloc(sizeof(*global));
10305 if (global == NULL)
10306 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010307 global->ioctl_sock = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010308 dl_list_init(&global->interfaces);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010309 global->if_add_ifindex = -1;
10310
10311 cfg = os_zalloc(sizeof(*cfg));
10312 if (cfg == NULL)
10313 goto err;
10314
10315 cfg->ctx = global;
10316 cfg->newlink_cb = wpa_driver_nl80211_event_rtm_newlink;
10317 cfg->dellink_cb = wpa_driver_nl80211_event_rtm_dellink;
10318 global->netlink = netlink_init(cfg);
10319 if (global->netlink == NULL) {
10320 os_free(cfg);
10321 goto err;
10322 }
10323
10324 if (wpa_driver_nl80211_init_nl_global(global) < 0)
10325 goto err;
10326
10327 global->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
10328 if (global->ioctl_sock < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070010329 wpa_printf(MSG_ERROR, "nl80211: socket(PF_INET,SOCK_DGRAM) failed: %s",
10330 strerror(errno));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010331 goto err;
10332 }
10333
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010334 return global;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010335
10336err:
10337 nl80211_global_deinit(global);
10338 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010339}
10340
10341
10342static void nl80211_global_deinit(void *priv)
10343{
10344 struct nl80211_global *global = priv;
10345 if (global == NULL)
10346 return;
10347 if (!dl_list_empty(&global->interfaces)) {
10348 wpa_printf(MSG_ERROR, "nl80211: %u interface(s) remain at "
10349 "nl80211_global_deinit",
10350 dl_list_len(&global->interfaces));
10351 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010352
10353 if (global->netlink)
10354 netlink_deinit(global->netlink);
10355
10356 nl_destroy_handles(&global->nl);
10357
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070010358 if (global->nl_event)
10359 nl80211_destroy_eloop_handle(&global->nl_event);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010360
10361 nl_cb_put(global->nl_cb);
10362
10363 if (global->ioctl_sock >= 0)
10364 close(global->ioctl_sock);
10365
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010366 os_free(global);
10367}
10368
10369
10370static const char * nl80211_get_radio_name(void *priv)
10371{
10372 struct i802_bss *bss = priv;
10373 struct wpa_driver_nl80211_data *drv = bss->drv;
10374 return drv->phyname;
10375}
10376
10377
Jouni Malinen75ecf522011-06-27 15:19:46 -070010378static int nl80211_pmkid(struct i802_bss *bss, int cmd, const u8 *bssid,
10379 const u8 *pmkid)
10380{
10381 struct nl_msg *msg;
10382
10383 msg = nlmsg_alloc();
10384 if (!msg)
10385 return -ENOMEM;
10386
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010387 nl80211_cmd(bss->drv, msg, 0, cmd);
Jouni Malinen75ecf522011-06-27 15:19:46 -070010388
10389 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
10390 if (pmkid)
10391 NLA_PUT(msg, NL80211_ATTR_PMKID, 16, pmkid);
10392 if (bssid)
10393 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
10394
10395 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
10396 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010397 nlmsg_free(msg);
Jouni Malinen75ecf522011-06-27 15:19:46 -070010398 return -ENOBUFS;
10399}
10400
10401
10402static int nl80211_add_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
10403{
10404 struct i802_bss *bss = priv;
10405 wpa_printf(MSG_DEBUG, "nl80211: Add PMKID for " MACSTR, MAC2STR(bssid));
10406 return nl80211_pmkid(bss, NL80211_CMD_SET_PMKSA, bssid, pmkid);
10407}
10408
10409
10410static int nl80211_remove_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
10411{
10412 struct i802_bss *bss = priv;
10413 wpa_printf(MSG_DEBUG, "nl80211: Delete PMKID for " MACSTR,
10414 MAC2STR(bssid));
10415 return nl80211_pmkid(bss, NL80211_CMD_DEL_PMKSA, bssid, pmkid);
10416}
10417
10418
10419static int nl80211_flush_pmkid(void *priv)
10420{
10421 struct i802_bss *bss = priv;
10422 wpa_printf(MSG_DEBUG, "nl80211: Flush PMKIDs");
10423 return nl80211_pmkid(bss, NL80211_CMD_FLUSH_PMKSA, NULL, NULL);
10424}
10425
10426
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070010427static void clean_survey_results(struct survey_results *survey_results)
10428{
10429 struct freq_survey *survey, *tmp;
10430
10431 if (dl_list_empty(&survey_results->survey_list))
10432 return;
10433
10434 dl_list_for_each_safe(survey, tmp, &survey_results->survey_list,
10435 struct freq_survey, list) {
10436 dl_list_del(&survey->list);
10437 os_free(survey);
10438 }
10439}
10440
10441
10442static void add_survey(struct nlattr **sinfo, u32 ifidx,
10443 struct dl_list *survey_list)
10444{
10445 struct freq_survey *survey;
10446
10447 survey = os_zalloc(sizeof(struct freq_survey));
10448 if (!survey)
10449 return;
10450
10451 survey->ifidx = ifidx;
10452 survey->freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
10453 survey->filled = 0;
10454
10455 if (sinfo[NL80211_SURVEY_INFO_NOISE]) {
10456 survey->nf = (int8_t)
10457 nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
10458 survey->filled |= SURVEY_HAS_NF;
10459 }
10460
10461 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]) {
10462 survey->channel_time =
10463 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]);
10464 survey->filled |= SURVEY_HAS_CHAN_TIME;
10465 }
10466
10467 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]) {
10468 survey->channel_time_busy =
10469 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]);
10470 survey->filled |= SURVEY_HAS_CHAN_TIME_BUSY;
10471 }
10472
10473 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]) {
10474 survey->channel_time_rx =
10475 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]);
10476 survey->filled |= SURVEY_HAS_CHAN_TIME_RX;
10477 }
10478
10479 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]) {
10480 survey->channel_time_tx =
10481 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]);
10482 survey->filled |= SURVEY_HAS_CHAN_TIME_TX;
10483 }
10484
10485 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)",
10486 survey->freq,
10487 survey->nf,
10488 (unsigned long int) survey->channel_time,
10489 (unsigned long int) survey->channel_time_busy,
10490 (unsigned long int) survey->channel_time_tx,
10491 (unsigned long int) survey->channel_time_rx,
10492 survey->filled);
10493
10494 dl_list_add_tail(survey_list, &survey->list);
10495}
10496
10497
10498static int check_survey_ok(struct nlattr **sinfo, u32 surveyed_freq,
10499 unsigned int freq_filter)
10500{
10501 if (!freq_filter)
10502 return 1;
10503
10504 return freq_filter == surveyed_freq;
10505}
10506
10507
10508static int survey_handler(struct nl_msg *msg, void *arg)
10509{
10510 struct nlattr *tb[NL80211_ATTR_MAX + 1];
10511 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
10512 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
10513 struct survey_results *survey_results;
10514 u32 surveyed_freq = 0;
10515 u32 ifidx;
10516
10517 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
10518 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
10519 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
10520 };
10521
10522 survey_results = (struct survey_results *) arg;
10523
10524 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
10525 genlmsg_attrlen(gnlh, 0), NULL);
10526
10527 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
10528
10529 if (!tb[NL80211_ATTR_SURVEY_INFO])
10530 return NL_SKIP;
10531
10532 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
10533 tb[NL80211_ATTR_SURVEY_INFO],
10534 survey_policy))
10535 return NL_SKIP;
10536
10537 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY]) {
10538 wpa_printf(MSG_ERROR, "nl80211: Invalid survey data");
10539 return NL_SKIP;
10540 }
10541
10542 surveyed_freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
10543
10544 if (!check_survey_ok(sinfo, surveyed_freq,
10545 survey_results->freq_filter))
10546 return NL_SKIP;
10547
10548 if (survey_results->freq_filter &&
10549 survey_results->freq_filter != surveyed_freq) {
10550 wpa_printf(MSG_EXCESSIVE, "nl80211: Ignoring survey data for freq %d MHz",
10551 surveyed_freq);
10552 return NL_SKIP;
10553 }
10554
10555 add_survey(sinfo, ifidx, &survey_results->survey_list);
10556
10557 return NL_SKIP;
10558}
10559
10560
10561static int wpa_driver_nl80211_get_survey(void *priv, unsigned int freq)
10562{
10563 struct i802_bss *bss = priv;
10564 struct wpa_driver_nl80211_data *drv = bss->drv;
10565 struct nl_msg *msg;
10566 int err = -ENOBUFS;
10567 union wpa_event_data data;
10568 struct survey_results *survey_results;
10569
10570 os_memset(&data, 0, sizeof(data));
10571 survey_results = &data.survey_results;
10572
10573 dl_list_init(&survey_results->survey_list);
10574
10575 msg = nlmsg_alloc();
10576 if (!msg)
10577 goto nla_put_failure;
10578
10579 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
10580
10581 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
10582
10583 if (freq)
10584 data.survey_results.freq_filter = freq;
10585
10586 do {
10587 wpa_printf(MSG_DEBUG, "nl80211: Fetch survey data");
10588 err = send_and_recv_msgs(drv, msg, survey_handler,
10589 survey_results);
10590 } while (err > 0);
10591
10592 if (err) {
10593 wpa_printf(MSG_ERROR, "nl80211: Failed to process survey data");
10594 goto out_clean;
10595 }
10596
10597 wpa_supplicant_event(drv->ctx, EVENT_SURVEY, &data);
10598
10599out_clean:
10600 clean_survey_results(survey_results);
10601nla_put_failure:
10602 return err;
10603}
10604
10605
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010606static void nl80211_set_rekey_info(void *priv, const u8 *kek, const u8 *kck,
10607 const u8 *replay_ctr)
10608{
10609 struct i802_bss *bss = priv;
10610 struct wpa_driver_nl80211_data *drv = bss->drv;
10611 struct nlattr *replay_nested;
10612 struct nl_msg *msg;
10613
10614 msg = nlmsg_alloc();
10615 if (!msg)
10616 return;
10617
10618 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
10619
10620 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
10621
10622 replay_nested = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
10623 if (!replay_nested)
10624 goto nla_put_failure;
10625
10626 NLA_PUT(msg, NL80211_REKEY_DATA_KEK, NL80211_KEK_LEN, kek);
10627 NLA_PUT(msg, NL80211_REKEY_DATA_KCK, NL80211_KCK_LEN, kck);
10628 NLA_PUT(msg, NL80211_REKEY_DATA_REPLAY_CTR, NL80211_REPLAY_CTR_LEN,
10629 replay_ctr);
10630
10631 nla_nest_end(msg, replay_nested);
10632
10633 send_and_recv_msgs(drv, msg, NULL, NULL);
10634 return;
10635 nla_put_failure:
10636 nlmsg_free(msg);
10637}
10638
10639
10640static void nl80211_send_null_frame(struct i802_bss *bss, const u8 *own_addr,
10641 const u8 *addr, int qos)
10642{
10643 /* send data frame to poll STA and check whether
10644 * this frame is ACKed */
10645 struct {
10646 struct ieee80211_hdr hdr;
10647 u16 qos_ctl;
10648 } STRUCT_PACKED nulldata;
10649 size_t size;
10650
10651 /* Send data frame to poll STA and check whether this frame is ACKed */
10652
10653 os_memset(&nulldata, 0, sizeof(nulldata));
10654
10655 if (qos) {
10656 nulldata.hdr.frame_control =
10657 IEEE80211_FC(WLAN_FC_TYPE_DATA,
10658 WLAN_FC_STYPE_QOS_NULL);
10659 size = sizeof(nulldata);
10660 } else {
10661 nulldata.hdr.frame_control =
10662 IEEE80211_FC(WLAN_FC_TYPE_DATA,
10663 WLAN_FC_STYPE_NULLFUNC);
10664 size = sizeof(struct ieee80211_hdr);
10665 }
10666
10667 nulldata.hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS);
10668 os_memcpy(nulldata.hdr.IEEE80211_DA_FROMDS, addr, ETH_ALEN);
10669 os_memcpy(nulldata.hdr.IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
10670 os_memcpy(nulldata.hdr.IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
10671
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010672 if (wpa_driver_nl80211_send_mlme(bss, (u8 *) &nulldata, size, 0, 0, 0,
10673 0, 0) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010674 wpa_printf(MSG_DEBUG, "nl80211_send_null_frame: Failed to "
10675 "send poll frame");
10676}
10677
10678static void nl80211_poll_client(void *priv, const u8 *own_addr, const u8 *addr,
10679 int qos)
10680{
10681 struct i802_bss *bss = priv;
10682 struct wpa_driver_nl80211_data *drv = bss->drv;
10683 struct nl_msg *msg;
10684
10685 if (!drv->poll_command_supported) {
10686 nl80211_send_null_frame(bss, own_addr, addr, qos);
10687 return;
10688 }
10689
10690 msg = nlmsg_alloc();
10691 if (!msg)
10692 return;
10693
10694 nl80211_cmd(drv, msg, 0, NL80211_CMD_PROBE_CLIENT);
10695
10696 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
10697 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
10698
10699 send_and_recv_msgs(drv, msg, NULL, NULL);
10700 return;
10701 nla_put_failure:
10702 nlmsg_free(msg);
10703}
10704
10705
10706static int nl80211_set_power_save(struct i802_bss *bss, int enabled)
10707{
10708 struct nl_msg *msg;
10709
10710 msg = nlmsg_alloc();
10711 if (!msg)
10712 return -ENOMEM;
10713
10714 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_SET_POWER_SAVE);
10715 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
10716 NLA_PUT_U32(msg, NL80211_ATTR_PS_STATE,
10717 enabled ? NL80211_PS_ENABLED : NL80211_PS_DISABLED);
10718 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
10719nla_put_failure:
10720 nlmsg_free(msg);
10721 return -ENOBUFS;
10722}
10723
10724
10725static int nl80211_set_p2p_powersave(void *priv, int legacy_ps, int opp_ps,
10726 int ctwindow)
10727{
10728 struct i802_bss *bss = priv;
10729
10730 wpa_printf(MSG_DEBUG, "nl80211: set_p2p_powersave (legacy_ps=%d "
10731 "opp_ps=%d ctwindow=%d)", legacy_ps, opp_ps, ctwindow);
10732
Dmitry Shmidt292b0c32013-11-22 12:54:42 -080010733 if (opp_ps != -1 || ctwindow != -1) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080010734#ifdef ANDROID_P2P
10735 wpa_driver_set_p2p_ps(priv, legacy_ps, opp_ps, ctwindow);
Dmitry Shmidt292b0c32013-11-22 12:54:42 -080010736#else /* ANDROID_P2P */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010737 return -1; /* Not yet supported */
Dmitry Shmidt292b0c32013-11-22 12:54:42 -080010738#endif /* ANDROID_P2P */
10739 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010740
10741 if (legacy_ps == -1)
10742 return 0;
10743 if (legacy_ps != 0 && legacy_ps != 1)
10744 return -1; /* Not yet supported */
10745
10746 return nl80211_set_power_save(bss, legacy_ps);
10747}
10748
10749
Dmitry Shmidt051af732013-10-22 13:52:46 -070010750static int nl80211_start_radar_detection(void *priv,
10751 struct hostapd_freq_params *freq)
Dmitry Shmidtea69e842013-05-13 14:52:28 -070010752{
10753 struct i802_bss *bss = priv;
10754 struct wpa_driver_nl80211_data *drv = bss->drv;
10755 struct nl_msg *msg;
10756 int ret;
10757
Dmitry Shmidt051af732013-10-22 13:52:46 -070010758 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)",
10759 freq->freq, freq->ht_enabled, freq->vht_enabled,
10760 freq->bandwidth, freq->center_freq1, freq->center_freq2);
10761
Dmitry Shmidtea69e842013-05-13 14:52:28 -070010762 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_RADAR)) {
10763 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support radar "
10764 "detection");
10765 return -1;
10766 }
10767
10768 msg = nlmsg_alloc();
10769 if (!msg)
10770 return -1;
10771
10772 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_RADAR_DETECT);
10773 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
Dmitry Shmidt051af732013-10-22 13:52:46 -070010774 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq->freq);
Dmitry Shmidtea69e842013-05-13 14:52:28 -070010775
Dmitry Shmidt051af732013-10-22 13:52:46 -070010776 if (freq->vht_enabled) {
10777 switch (freq->bandwidth) {
10778 case 20:
10779 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
10780 NL80211_CHAN_WIDTH_20);
10781 break;
10782 case 40:
10783 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
10784 NL80211_CHAN_WIDTH_40);
10785 break;
10786 case 80:
10787 if (freq->center_freq2)
10788 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
10789 NL80211_CHAN_WIDTH_80P80);
10790 else
10791 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
10792 NL80211_CHAN_WIDTH_80);
10793 break;
10794 case 160:
10795 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
10796 NL80211_CHAN_WIDTH_160);
10797 break;
10798 default:
10799 return -1;
10800 }
10801 NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ1, freq->center_freq1);
10802 if (freq->center_freq2)
10803 NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ2,
10804 freq->center_freq2);
10805 } else if (freq->ht_enabled) {
10806 switch (freq->sec_channel_offset) {
10807 case -1:
10808 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
10809 NL80211_CHAN_HT40MINUS);
10810 break;
10811 case 1:
10812 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
10813 NL80211_CHAN_HT40PLUS);
10814 break;
10815 default:
10816 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
10817 NL80211_CHAN_HT20);
10818 break;
10819 }
10820 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -070010821
10822 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
10823 if (ret == 0)
10824 return 0;
10825 wpa_printf(MSG_DEBUG, "nl80211: Failed to start radar detection: "
10826 "%d (%s)", ret, strerror(-ret));
10827nla_put_failure:
10828 return -1;
10829}
10830
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010831#ifdef CONFIG_TDLS
10832
10833static int nl80211_send_tdls_mgmt(void *priv, const u8 *dst, u8 action_code,
10834 u8 dialog_token, u16 status_code,
10835 const u8 *buf, size_t len)
10836{
10837 struct i802_bss *bss = priv;
10838 struct wpa_driver_nl80211_data *drv = bss->drv;
10839 struct nl_msg *msg;
10840
10841 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
10842 return -EOPNOTSUPP;
10843
10844 if (!dst)
10845 return -EINVAL;
10846
10847 msg = nlmsg_alloc();
10848 if (!msg)
10849 return -ENOMEM;
10850
10851 nl80211_cmd(drv, msg, 0, NL80211_CMD_TDLS_MGMT);
10852 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
10853 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
10854 NLA_PUT_U8(msg, NL80211_ATTR_TDLS_ACTION, action_code);
10855 NLA_PUT_U8(msg, NL80211_ATTR_TDLS_DIALOG_TOKEN, dialog_token);
10856 NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status_code);
10857 NLA_PUT(msg, NL80211_ATTR_IE, len, buf);
10858
10859 return send_and_recv_msgs(drv, msg, NULL, NULL);
10860
10861nla_put_failure:
10862 nlmsg_free(msg);
10863 return -ENOBUFS;
10864}
10865
10866
10867static int nl80211_tdls_oper(void *priv, enum tdls_oper oper, const u8 *peer)
10868{
10869 struct i802_bss *bss = priv;
10870 struct wpa_driver_nl80211_data *drv = bss->drv;
10871 struct nl_msg *msg;
10872 enum nl80211_tdls_operation nl80211_oper;
10873
10874 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
10875 return -EOPNOTSUPP;
10876
10877 switch (oper) {
10878 case TDLS_DISCOVERY_REQ:
10879 nl80211_oper = NL80211_TDLS_DISCOVERY_REQ;
10880 break;
10881 case TDLS_SETUP:
10882 nl80211_oper = NL80211_TDLS_SETUP;
10883 break;
10884 case TDLS_TEARDOWN:
10885 nl80211_oper = NL80211_TDLS_TEARDOWN;
10886 break;
10887 case TDLS_ENABLE_LINK:
10888 nl80211_oper = NL80211_TDLS_ENABLE_LINK;
10889 break;
10890 case TDLS_DISABLE_LINK:
10891 nl80211_oper = NL80211_TDLS_DISABLE_LINK;
10892 break;
10893 case TDLS_ENABLE:
10894 return 0;
10895 case TDLS_DISABLE:
10896 return 0;
10897 default:
10898 return -EINVAL;
10899 }
10900
10901 msg = nlmsg_alloc();
10902 if (!msg)
10903 return -ENOMEM;
10904
10905 nl80211_cmd(drv, msg, 0, NL80211_CMD_TDLS_OPER);
10906 NLA_PUT_U8(msg, NL80211_ATTR_TDLS_OPERATION, nl80211_oper);
10907 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
10908 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, peer);
10909
10910 return send_and_recv_msgs(drv, msg, NULL, NULL);
10911
10912nla_put_failure:
10913 nlmsg_free(msg);
10914 return -ENOBUFS;
10915}
10916
10917#endif /* CONFIG TDLS */
10918
10919
10920#ifdef ANDROID
10921
10922typedef struct android_wifi_priv_cmd {
10923 char *buf;
10924 int used_len;
10925 int total_len;
10926} android_wifi_priv_cmd;
10927
10928static int drv_errors = 0;
10929
10930static void wpa_driver_send_hang_msg(struct wpa_driver_nl80211_data *drv)
10931{
10932 drv_errors++;
10933 if (drv_errors > DRV_NUMBER_SEQUENTIAL_ERRORS) {
10934 drv_errors = 0;
10935 wpa_msg(drv->ctx, MSG_INFO, WPA_EVENT_DRIVER_STATE "HANGED");
10936 }
10937}
10938
10939
10940static int android_priv_cmd(struct i802_bss *bss, const char *cmd)
10941{
10942 struct wpa_driver_nl80211_data *drv = bss->drv;
10943 struct ifreq ifr;
10944 android_wifi_priv_cmd priv_cmd;
10945 char buf[MAX_DRV_CMD_SIZE];
10946 int ret;
10947
10948 os_memset(&ifr, 0, sizeof(ifr));
10949 os_memset(&priv_cmd, 0, sizeof(priv_cmd));
10950 os_strlcpy(ifr.ifr_name, bss->ifname, IFNAMSIZ);
10951
10952 os_memset(buf, 0, sizeof(buf));
10953 os_strlcpy(buf, cmd, sizeof(buf));
10954
10955 priv_cmd.buf = buf;
10956 priv_cmd.used_len = sizeof(buf);
10957 priv_cmd.total_len = sizeof(buf);
10958 ifr.ifr_data = &priv_cmd;
10959
10960 ret = ioctl(drv->global->ioctl_sock, SIOCDEVPRIVATE + 1, &ifr);
10961 if (ret < 0) {
10962 wpa_printf(MSG_ERROR, "%s: failed to issue private commands",
10963 __func__);
10964 wpa_driver_send_hang_msg(drv);
10965 return ret;
10966 }
10967
10968 drv_errors = 0;
10969 return 0;
10970}
10971
10972
10973static int android_pno_start(struct i802_bss *bss,
10974 struct wpa_driver_scan_params *params)
10975{
10976 struct wpa_driver_nl80211_data *drv = bss->drv;
10977 struct ifreq ifr;
10978 android_wifi_priv_cmd priv_cmd;
10979 int ret = 0, i = 0, bp;
10980 char buf[WEXT_PNO_MAX_COMMAND_SIZE];
10981
10982 bp = WEXT_PNOSETUP_HEADER_SIZE;
10983 os_memcpy(buf, WEXT_PNOSETUP_HEADER, bp);
10984 buf[bp++] = WEXT_PNO_TLV_PREFIX;
10985 buf[bp++] = WEXT_PNO_TLV_VERSION;
10986 buf[bp++] = WEXT_PNO_TLV_SUBVERSION;
10987 buf[bp++] = WEXT_PNO_TLV_RESERVED;
10988
10989 while (i < WEXT_PNO_AMOUNT && (size_t) i < params->num_ssids) {
10990 /* Check that there is enough space needed for 1 more SSID, the
10991 * other sections and null termination */
10992 if ((bp + WEXT_PNO_SSID_HEADER_SIZE + MAX_SSID_LEN +
10993 WEXT_PNO_NONSSID_SECTIONS_SIZE + 1) >= (int) sizeof(buf))
10994 break;
10995 wpa_hexdump_ascii(MSG_DEBUG, "For PNO Scan",
10996 params->ssids[i].ssid,
10997 params->ssids[i].ssid_len);
10998 buf[bp++] = WEXT_PNO_SSID_SECTION;
10999 buf[bp++] = params->ssids[i].ssid_len;
11000 os_memcpy(&buf[bp], params->ssids[i].ssid,
11001 params->ssids[i].ssid_len);
11002 bp += params->ssids[i].ssid_len;
11003 i++;
11004 }
11005
11006 buf[bp++] = WEXT_PNO_SCAN_INTERVAL_SECTION;
11007 os_snprintf(&buf[bp], WEXT_PNO_SCAN_INTERVAL_LENGTH + 1, "%x",
11008 WEXT_PNO_SCAN_INTERVAL);
11009 bp += WEXT_PNO_SCAN_INTERVAL_LENGTH;
11010
11011 buf[bp++] = WEXT_PNO_REPEAT_SECTION;
11012 os_snprintf(&buf[bp], WEXT_PNO_REPEAT_LENGTH + 1, "%x",
11013 WEXT_PNO_REPEAT);
11014 bp += WEXT_PNO_REPEAT_LENGTH;
11015
11016 buf[bp++] = WEXT_PNO_MAX_REPEAT_SECTION;
11017 os_snprintf(&buf[bp], WEXT_PNO_MAX_REPEAT_LENGTH + 1, "%x",
11018 WEXT_PNO_MAX_REPEAT);
11019 bp += WEXT_PNO_MAX_REPEAT_LENGTH + 1;
11020
11021 memset(&ifr, 0, sizeof(ifr));
11022 memset(&priv_cmd, 0, sizeof(priv_cmd));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070011023 os_strlcpy(ifr.ifr_name, bss->ifname, IFNAMSIZ);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011024
11025 priv_cmd.buf = buf;
11026 priv_cmd.used_len = bp;
11027 priv_cmd.total_len = bp;
11028 ifr.ifr_data = &priv_cmd;
11029
11030 ret = ioctl(drv->global->ioctl_sock, SIOCDEVPRIVATE + 1, &ifr);
11031
11032 if (ret < 0) {
11033 wpa_printf(MSG_ERROR, "ioctl[SIOCSIWPRIV] (pnosetup): %d",
11034 ret);
11035 wpa_driver_send_hang_msg(drv);
11036 return ret;
11037 }
11038
11039 drv_errors = 0;
11040
11041 return android_priv_cmd(bss, "PNOFORCE 1");
11042}
11043
11044
11045static int android_pno_stop(struct i802_bss *bss)
11046{
11047 return android_priv_cmd(bss, "PNOFORCE 0");
11048}
11049
11050#endif /* ANDROID */
11051
11052
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011053static int driver_nl80211_set_key(const char *ifname, void *priv,
11054 enum wpa_alg alg, const u8 *addr,
11055 int key_idx, int set_tx,
11056 const u8 *seq, size_t seq_len,
11057 const u8 *key, size_t key_len)
11058{
11059 struct i802_bss *bss = priv;
11060 return wpa_driver_nl80211_set_key(ifname, bss, alg, addr, key_idx,
11061 set_tx, seq, seq_len, key, key_len);
11062}
11063
11064
11065static int driver_nl80211_scan2(void *priv,
11066 struct wpa_driver_scan_params *params)
11067{
11068 struct i802_bss *bss = priv;
11069 return wpa_driver_nl80211_scan(bss, params);
11070}
11071
11072
11073static int driver_nl80211_deauthenticate(void *priv, const u8 *addr,
11074 int reason_code)
11075{
11076 struct i802_bss *bss = priv;
11077 return wpa_driver_nl80211_deauthenticate(bss, addr, reason_code);
11078}
11079
11080
11081static int driver_nl80211_authenticate(void *priv,
11082 struct wpa_driver_auth_params *params)
11083{
11084 struct i802_bss *bss = priv;
11085 return wpa_driver_nl80211_authenticate(bss, params);
11086}
11087
11088
11089static void driver_nl80211_deinit(void *priv)
11090{
11091 struct i802_bss *bss = priv;
11092 wpa_driver_nl80211_deinit(bss);
11093}
11094
11095
11096static int driver_nl80211_if_remove(void *priv, enum wpa_driver_if_type type,
11097 const char *ifname)
11098{
11099 struct i802_bss *bss = priv;
11100 return wpa_driver_nl80211_if_remove(bss, type, ifname);
11101}
11102
11103
11104static int driver_nl80211_send_mlme(void *priv, const u8 *data,
11105 size_t data_len, int noack)
11106{
11107 struct i802_bss *bss = priv;
11108 return wpa_driver_nl80211_send_mlme(bss, data, data_len, noack,
11109 0, 0, 0, 0);
11110}
11111
11112
11113static int driver_nl80211_sta_remove(void *priv, const u8 *addr)
11114{
11115 struct i802_bss *bss = priv;
11116 return wpa_driver_nl80211_sta_remove(bss, addr);
11117}
11118
11119
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011120static int driver_nl80211_set_sta_vlan(void *priv, const u8 *addr,
11121 const char *ifname, int vlan_id)
11122{
11123 struct i802_bss *bss = priv;
11124 return i802_set_sta_vlan(bss, addr, ifname, vlan_id);
11125}
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011126
11127
11128static int driver_nl80211_read_sta_data(void *priv,
11129 struct hostap_sta_driver_data *data,
11130 const u8 *addr)
11131{
11132 struct i802_bss *bss = priv;
11133 return i802_read_sta_data(bss, data, addr);
11134}
11135
11136
11137static int driver_nl80211_send_action(void *priv, unsigned int freq,
11138 unsigned int wait_time,
11139 const u8 *dst, const u8 *src,
11140 const u8 *bssid,
11141 const u8 *data, size_t data_len,
11142 int no_cck)
11143{
11144 struct i802_bss *bss = priv;
11145 return wpa_driver_nl80211_send_action(bss, freq, wait_time, dst, src,
11146 bssid, data, data_len, no_cck);
11147}
11148
11149
11150static int driver_nl80211_probe_req_report(void *priv, int report)
11151{
11152 struct i802_bss *bss = priv;
11153 return wpa_driver_nl80211_probe_req_report(bss, report);
11154}
11155
11156
Dmitry Shmidt700a1372013-03-15 14:14:44 -070011157static int wpa_driver_nl80211_update_ft_ies(void *priv, const u8 *md,
11158 const u8 *ies, size_t ies_len)
11159{
11160 int ret;
11161 struct nl_msg *msg;
11162 struct i802_bss *bss = priv;
11163 struct wpa_driver_nl80211_data *drv = bss->drv;
11164 u16 mdid = WPA_GET_LE16(md);
11165
11166 msg = nlmsg_alloc();
11167 if (!msg)
11168 return -ENOMEM;
11169
11170 wpa_printf(MSG_DEBUG, "nl80211: Updating FT IEs");
11171 nl80211_cmd(drv, msg, 0, NL80211_CMD_UPDATE_FT_IES);
11172 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
11173 NLA_PUT(msg, NL80211_ATTR_IE, ies_len, ies);
11174 NLA_PUT_U16(msg, NL80211_ATTR_MDID, mdid);
11175
11176 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
11177 if (ret) {
11178 wpa_printf(MSG_DEBUG, "nl80211: update_ft_ies failed "
11179 "err=%d (%s)", ret, strerror(-ret));
11180 }
11181
11182 return ret;
11183
11184nla_put_failure:
11185 nlmsg_free(msg);
11186 return -ENOBUFS;
11187}
11188
11189
Dmitry Shmidt34af3062013-07-11 10:46:32 -070011190const u8 * wpa_driver_nl80211_get_macaddr(void *priv)
11191{
11192 struct i802_bss *bss = priv;
11193 struct wpa_driver_nl80211_data *drv = bss->drv;
11194
11195 if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE)
11196 return NULL;
11197
11198 return bss->addr;
11199}
11200
11201
Dmitry Shmidt56052862013-10-04 10:23:25 -070011202static const char * scan_state_str(enum scan_states scan_state)
11203{
11204 switch (scan_state) {
11205 case NO_SCAN:
11206 return "NO_SCAN";
11207 case SCAN_REQUESTED:
11208 return "SCAN_REQUESTED";
11209 case SCAN_STARTED:
11210 return "SCAN_STARTED";
11211 case SCAN_COMPLETED:
11212 return "SCAN_COMPLETED";
11213 case SCAN_ABORTED:
11214 return "SCAN_ABORTED";
11215 case SCHED_SCAN_STARTED:
11216 return "SCHED_SCAN_STARTED";
11217 case SCHED_SCAN_STOPPED:
11218 return "SCHED_SCAN_STOPPED";
11219 case SCHED_SCAN_RESULTS:
11220 return "SCHED_SCAN_RESULTS";
11221 }
11222
11223 return "??";
11224}
11225
11226
11227static int wpa_driver_nl80211_status(void *priv, char *buf, size_t buflen)
11228{
11229 struct i802_bss *bss = priv;
11230 struct wpa_driver_nl80211_data *drv = bss->drv;
11231 int res;
11232 char *pos, *end;
11233
11234 pos = buf;
11235 end = buf + buflen;
11236
11237 res = os_snprintf(pos, end - pos,
11238 "ifindex=%d\n"
11239 "ifname=%s\n"
11240 "brname=%s\n"
11241 "addr=" MACSTR "\n"
11242 "freq=%d\n"
11243 "%s%s%s%s%s",
11244 bss->ifindex,
11245 bss->ifname,
11246 bss->brname,
11247 MAC2STR(bss->addr),
11248 bss->freq,
11249 bss->beacon_set ? "beacon_set=1\n" : "",
11250 bss->added_if_into_bridge ?
11251 "added_if_into_bridge=1\n" : "",
11252 bss->added_bridge ? "added_bridge=1\n" : "",
11253 bss->in_deinit ? "in_deinit=1\n" : "",
11254 bss->if_dynamic ? "if_dynamic=1\n" : "");
11255 if (res < 0 || res >= end - pos)
11256 return pos - buf;
11257 pos += res;
11258
11259 if (bss->wdev_id_set) {
11260 res = os_snprintf(pos, end - pos, "wdev_id=%llu\n",
11261 (unsigned long long) bss->wdev_id);
11262 if (res < 0 || res >= end - pos)
11263 return pos - buf;
11264 pos += res;
11265 }
11266
11267 res = os_snprintf(pos, end - pos,
11268 "phyname=%s\n"
11269 "drv_ifindex=%d\n"
11270 "operstate=%d\n"
11271 "scan_state=%s\n"
11272 "auth_bssid=" MACSTR "\n"
11273 "auth_attempt_bssid=" MACSTR "\n"
11274 "bssid=" MACSTR "\n"
11275 "prev_bssid=" MACSTR "\n"
11276 "associated=%d\n"
11277 "assoc_freq=%u\n"
11278 "monitor_sock=%d\n"
11279 "monitor_ifidx=%d\n"
11280 "monitor_refcount=%d\n"
11281 "last_mgmt_freq=%u\n"
11282 "eapol_tx_sock=%d\n"
11283 "%s%s%s%s%s%s%s%s%s%s%s%s%s",
11284 drv->phyname,
11285 drv->ifindex,
11286 drv->operstate,
11287 scan_state_str(drv->scan_state),
11288 MAC2STR(drv->auth_bssid),
11289 MAC2STR(drv->auth_attempt_bssid),
11290 MAC2STR(drv->bssid),
11291 MAC2STR(drv->prev_bssid),
11292 drv->associated,
11293 drv->assoc_freq,
11294 drv->monitor_sock,
11295 drv->monitor_ifidx,
11296 drv->monitor_refcount,
11297 drv->last_mgmt_freq,
11298 drv->eapol_tx_sock,
11299 drv->ignore_if_down_event ?
11300 "ignore_if_down_event=1\n" : "",
11301 drv->scan_complete_events ?
11302 "scan_complete_events=1\n" : "",
11303 drv->disabled_11b_rates ?
11304 "disabled_11b_rates=1\n" : "",
11305 drv->pending_remain_on_chan ?
11306 "pending_remain_on_chan=1\n" : "",
11307 drv->in_interface_list ? "in_interface_list=1\n" : "",
11308 drv->device_ap_sme ? "device_ap_sme=1\n" : "",
11309 drv->poll_command_supported ?
11310 "poll_command_supported=1\n" : "",
11311 drv->data_tx_status ? "data_tx_status=1\n" : "",
11312 drv->scan_for_auth ? "scan_for_auth=1\n" : "",
11313 drv->retry_auth ? "retry_auth=1\n" : "",
11314 drv->use_monitor ? "use_monitor=1\n" : "",
11315 drv->ignore_next_local_disconnect ?
11316 "ignore_next_local_disconnect=1\n" : "",
11317 drv->allow_p2p_device ? "allow_p2p_device=1\n" : "");
11318 if (res < 0 || res >= end - pos)
11319 return pos - buf;
11320 pos += res;
11321
11322 if (drv->has_capability) {
11323 res = os_snprintf(pos, end - pos,
11324 "capa.key_mgmt=0x%x\n"
11325 "capa.enc=0x%x\n"
11326 "capa.auth=0x%x\n"
11327 "capa.flags=0x%x\n"
11328 "capa.max_scan_ssids=%d\n"
11329 "capa.max_sched_scan_ssids=%d\n"
11330 "capa.sched_scan_supported=%d\n"
11331 "capa.max_match_sets=%d\n"
11332 "capa.max_remain_on_chan=%u\n"
11333 "capa.max_stations=%u\n"
11334 "capa.probe_resp_offloads=0x%x\n"
11335 "capa.max_acl_mac_addrs=%u\n"
11336 "capa.num_multichan_concurrent=%u\n",
11337 drv->capa.key_mgmt,
11338 drv->capa.enc,
11339 drv->capa.auth,
11340 drv->capa.flags,
11341 drv->capa.max_scan_ssids,
11342 drv->capa.max_sched_scan_ssids,
11343 drv->capa.sched_scan_supported,
11344 drv->capa.max_match_sets,
11345 drv->capa.max_remain_on_chan,
11346 drv->capa.max_stations,
11347 drv->capa.probe_resp_offloads,
11348 drv->capa.max_acl_mac_addrs,
11349 drv->capa.num_multichan_concurrent);
11350 if (res < 0 || res >= end - pos)
11351 return pos - buf;
11352 pos += res;
11353 }
11354
11355 return pos - buf;
11356}
11357
11358
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080011359static int set_beacon_data(struct nl_msg *msg, struct beacon_data *settings)
11360{
11361 if (settings->head)
11362 NLA_PUT(msg, NL80211_ATTR_BEACON_HEAD,
11363 settings->head_len, settings->head);
11364
11365 if (settings->tail)
11366 NLA_PUT(msg, NL80211_ATTR_BEACON_TAIL,
11367 settings->tail_len, settings->tail);
11368
11369 if (settings->beacon_ies)
11370 NLA_PUT(msg, NL80211_ATTR_IE,
11371 settings->beacon_ies_len, settings->beacon_ies);
11372
11373 if (settings->proberesp_ies)
11374 NLA_PUT(msg, NL80211_ATTR_IE_PROBE_RESP,
11375 settings->proberesp_ies_len, settings->proberesp_ies);
11376
11377 if (settings->assocresp_ies)
11378 NLA_PUT(msg,
11379 NL80211_ATTR_IE_ASSOC_RESP,
11380 settings->assocresp_ies_len, settings->assocresp_ies);
11381
11382 if (settings->probe_resp)
11383 NLA_PUT(msg, NL80211_ATTR_PROBE_RESP,
11384 settings->probe_resp_len, settings->probe_resp);
11385
11386 return 0;
11387
11388nla_put_failure:
11389 return -ENOBUFS;
11390}
11391
11392
11393static int nl80211_switch_channel(void *priv, struct csa_settings *settings)
11394{
11395 struct nl_msg *msg;
11396 struct i802_bss *bss = priv;
11397 struct wpa_driver_nl80211_data *drv = bss->drv;
11398 struct nlattr *beacon_csa;
11399 int ret = -ENOBUFS;
11400
Dmitry Shmidt04f534e2013-12-09 15:50:16 -080011401 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 -080011402 settings->cs_count, settings->block_tx,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -080011403 settings->freq_params.freq, settings->freq_params.bandwidth,
11404 settings->freq_params.center_freq1,
11405 settings->freq_params.center_freq2);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080011406
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080011407 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_AP_CSA)) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080011408 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support channel switch command");
11409 return -EOPNOTSUPP;
11410 }
11411
11412 if ((drv->nlmode != NL80211_IFTYPE_AP) &&
11413 (drv->nlmode != NL80211_IFTYPE_P2P_GO))
11414 return -EOPNOTSUPP;
11415
11416 /* check settings validity */
11417 if (!settings->beacon_csa.tail ||
11418 ((settings->beacon_csa.tail_len <=
11419 settings->counter_offset_beacon) ||
11420 (settings->beacon_csa.tail[settings->counter_offset_beacon] !=
11421 settings->cs_count)))
11422 return -EINVAL;
11423
11424 if (settings->beacon_csa.probe_resp &&
11425 ((settings->beacon_csa.probe_resp_len <=
11426 settings->counter_offset_presp) ||
11427 (settings->beacon_csa.probe_resp[settings->counter_offset_presp] !=
11428 settings->cs_count)))
11429 return -EINVAL;
11430
11431 msg = nlmsg_alloc();
11432 if (!msg)
11433 return -ENOMEM;
11434
11435 nl80211_cmd(drv, msg, 0, NL80211_CMD_CHANNEL_SWITCH);
11436 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
11437 NLA_PUT_U32(msg, NL80211_ATTR_CH_SWITCH_COUNT, settings->cs_count);
11438 ret = nl80211_put_freq_params(msg, &settings->freq_params);
11439 if (ret)
11440 goto error;
11441
11442 if (settings->block_tx)
11443 NLA_PUT_FLAG(msg, NL80211_ATTR_CH_SWITCH_BLOCK_TX);
11444
11445 /* beacon_after params */
11446 ret = set_beacon_data(msg, &settings->beacon_after);
11447 if (ret)
11448 goto error;
11449
11450 /* beacon_csa params */
11451 beacon_csa = nla_nest_start(msg, NL80211_ATTR_CSA_IES);
11452 if (!beacon_csa)
11453 goto nla_put_failure;
11454
11455 ret = set_beacon_data(msg, &settings->beacon_csa);
11456 if (ret)
11457 goto error;
11458
11459 NLA_PUT_U16(msg, NL80211_ATTR_CSA_C_OFF_BEACON,
11460 settings->counter_offset_beacon);
11461
11462 if (settings->beacon_csa.probe_resp)
11463 NLA_PUT_U16(msg, NL80211_ATTR_CSA_C_OFF_PRESP,
11464 settings->counter_offset_presp);
11465
11466 nla_nest_end(msg, beacon_csa);
11467 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
11468 if (ret) {
11469 wpa_printf(MSG_DEBUG, "nl80211: switch_channel failed err=%d (%s)",
11470 ret, strerror(-ret));
11471 }
11472 return ret;
11473
11474nla_put_failure:
11475 ret = -ENOBUFS;
11476error:
11477 nlmsg_free(msg);
11478 wpa_printf(MSG_DEBUG, "nl80211: Could not build channel switch request");
11479 return ret;
11480}
11481
11482
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080011483static int nl80211_set_qos_map(void *priv, const u8 *qos_map_set,
11484 u8 qos_map_set_len)
11485{
11486 struct i802_bss *bss = priv;
11487 struct wpa_driver_nl80211_data *drv = bss->drv;
11488 struct nl_msg *msg;
11489 int ret;
11490
11491 msg = nlmsg_alloc();
11492 if (!msg)
11493 return -ENOMEM;
11494
11495 wpa_hexdump(MSG_DEBUG, "nl80211: Setting QoS Map",
11496 qos_map_set, qos_map_set_len);
11497
11498 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_QOS_MAP);
11499 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
11500 NLA_PUT(msg, NL80211_ATTR_QOS_MAP, qos_map_set_len, qos_map_set);
11501
11502 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
11503 if (ret)
11504 wpa_printf(MSG_DEBUG, "nl80211: Setting QoS Map failed");
11505
11506 return ret;
11507
11508nla_put_failure:
11509 nlmsg_free(msg);
11510 return -ENOBUFS;
11511}
11512
11513
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011514const struct wpa_driver_ops wpa_driver_nl80211_ops = {
11515 .name = "nl80211",
11516 .desc = "Linux nl80211/cfg80211",
11517 .get_bssid = wpa_driver_nl80211_get_bssid,
11518 .get_ssid = wpa_driver_nl80211_get_ssid,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011519 .set_key = driver_nl80211_set_key,
11520 .scan2 = driver_nl80211_scan2,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011521 .sched_scan = wpa_driver_nl80211_sched_scan,
11522 .stop_sched_scan = wpa_driver_nl80211_stop_sched_scan,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011523 .get_scan_results2 = wpa_driver_nl80211_get_scan_results,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011524 .deauthenticate = driver_nl80211_deauthenticate,
11525 .authenticate = driver_nl80211_authenticate,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011526 .associate = wpa_driver_nl80211_associate,
11527 .global_init = nl80211_global_init,
11528 .global_deinit = nl80211_global_deinit,
11529 .init2 = wpa_driver_nl80211_init,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011530 .deinit = driver_nl80211_deinit,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011531 .get_capa = wpa_driver_nl80211_get_capa,
11532 .set_operstate = wpa_driver_nl80211_set_operstate,
11533 .set_supp_port = wpa_driver_nl80211_set_supp_port,
11534 .set_country = wpa_driver_nl80211_set_country,
Dmitry Shmidtcce06662013-11-04 18:44:24 -080011535 .get_country = wpa_driver_nl80211_get_country,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011536 .set_ap = wpa_driver_nl80211_set_ap,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -070011537 .set_acl = wpa_driver_nl80211_set_acl,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011538 .if_add = wpa_driver_nl80211_if_add,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011539 .if_remove = driver_nl80211_if_remove,
11540 .send_mlme = driver_nl80211_send_mlme,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011541 .get_hw_feature_data = wpa_driver_nl80211_get_hw_feature_data,
11542 .sta_add = wpa_driver_nl80211_sta_add,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011543 .sta_remove = driver_nl80211_sta_remove,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011544 .hapd_send_eapol = wpa_driver_nl80211_hapd_send_eapol,
11545 .sta_set_flags = wpa_driver_nl80211_sta_set_flags,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011546 .hapd_init = i802_init,
11547 .hapd_deinit = i802_deinit,
Jouni Malinen75ecf522011-06-27 15:19:46 -070011548 .set_wds_sta = i802_set_wds_sta,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011549 .get_seqnum = i802_get_seqnum,
11550 .flush = i802_flush,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011551 .get_inact_sec = i802_get_inact_sec,
11552 .sta_clear_stats = i802_sta_clear_stats,
11553 .set_rts = i802_set_rts,
11554 .set_frag = i802_set_frag,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011555 .set_tx_queue_params = i802_set_tx_queue_params,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011556 .set_sta_vlan = driver_nl80211_set_sta_vlan,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011557 .sta_deauth = i802_sta_deauth,
11558 .sta_disassoc = i802_sta_disassoc,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011559 .read_sta_data = driver_nl80211_read_sta_data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011560 .set_freq = i802_set_freq,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011561 .send_action = driver_nl80211_send_action,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011562 .send_action_cancel_wait = wpa_driver_nl80211_send_action_cancel_wait,
11563 .remain_on_channel = wpa_driver_nl80211_remain_on_channel,
11564 .cancel_remain_on_channel =
11565 wpa_driver_nl80211_cancel_remain_on_channel,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011566 .probe_req_report = driver_nl80211_probe_req_report,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011567 .deinit_ap = wpa_driver_nl80211_deinit_ap,
Dmitry Shmidt04949592012-07-19 12:16:46 -070011568 .deinit_p2p_cli = wpa_driver_nl80211_deinit_p2p_cli,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011569 .resume = wpa_driver_nl80211_resume,
11570 .send_ft_action = nl80211_send_ft_action,
11571 .signal_monitor = nl80211_signal_monitor,
11572 .signal_poll = nl80211_signal_poll,
11573 .send_frame = nl80211_send_frame,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011574 .shared_freq = wpa_driver_nl80211_shared_freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011575 .set_param = nl80211_set_param,
11576 .get_radio_name = nl80211_get_radio_name,
Jouni Malinen75ecf522011-06-27 15:19:46 -070011577 .add_pmkid = nl80211_add_pmkid,
11578 .remove_pmkid = nl80211_remove_pmkid,
11579 .flush_pmkid = nl80211_flush_pmkid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011580 .set_rekey_info = nl80211_set_rekey_info,
11581 .poll_client = nl80211_poll_client,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011582 .set_p2p_powersave = nl80211_set_p2p_powersave,
Dmitry Shmidtea69e842013-05-13 14:52:28 -070011583 .start_dfs_cac = nl80211_start_radar_detection,
11584 .stop_ap = wpa_driver_nl80211_stop_ap,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011585#ifdef CONFIG_TDLS
11586 .send_tdls_mgmt = nl80211_send_tdls_mgmt,
11587 .tdls_oper = nl80211_tdls_oper,
11588#endif /* CONFIG_TDLS */
Dmitry Shmidt700a1372013-03-15 14:14:44 -070011589 .update_ft_ies = wpa_driver_nl80211_update_ft_ies,
Dmitry Shmidt34af3062013-07-11 10:46:32 -070011590 .get_mac_addr = wpa_driver_nl80211_get_macaddr,
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070011591 .get_survey = wpa_driver_nl80211_get_survey,
Dmitry Shmidt56052862013-10-04 10:23:25 -070011592 .status = wpa_driver_nl80211_status,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080011593 .switch_channel = nl80211_switch_channel,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011594#ifdef ANDROID_P2P
Dmitry Shmidt6e933c12011-09-27 12:29:26 -070011595 .set_noa = wpa_driver_set_p2p_noa,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011596 .get_noa = wpa_driver_get_p2p_noa,
Dmitry Shmidt6e933c12011-09-27 12:29:26 -070011597 .set_ap_wps_ie = wpa_driver_set_ap_wps_p2p_ie,
Dmitry Shmidt292b0c32013-11-22 12:54:42 -080011598#endif /* ANDROID_P2P */
Dmitry Shmidt738a26e2011-07-07 14:22:14 -070011599#ifdef ANDROID
11600 .driver_cmd = wpa_driver_nl80211_driver_cmd,
Dmitry Shmidt292b0c32013-11-22 12:54:42 -080011601#endif /* ANDROID */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080011602 .set_qos_map = nl80211_set_qos_map,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011603};