blob: a1d8171450b021291ce511ccd344b94181554176 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * Driver interaction with Linux nl80211/cfg80211
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003 * Copyright (c) 2002-2012, 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 Shmidt8d520ff2011-05-09 14:06:53 -0700300
301 u64 remain_on_chan_cookie;
302 u64 send_action_cookie;
303
304 unsigned int last_mgmt_freq;
305
306 struct wpa_driver_scan_filter *filter_ssids;
307 size_t num_filter_ssids;
308
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800309 struct i802_bss *first_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700310
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800311 int eapol_tx_sock;
312
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700313#ifdef HOSTAPD
314 int eapol_sock; /* socket for EAPOL frames */
315
316 int default_if_indices[16];
317 int *if_indices;
318 int num_if_indices;
319
320 int last_freq;
321 int last_freq_ht;
322#endif /* HOSTAPD */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800323
324 /* From failed authentication command */
325 int auth_freq;
326 u8 auth_bssid_[ETH_ALEN];
327 u8 auth_ssid[32];
328 size_t auth_ssid_len;
329 int auth_alg;
330 u8 *auth_ie;
331 size_t auth_ie_len;
332 u8 auth_wep_key[4][16];
333 size_t auth_wep_key_len[4];
334 int auth_wep_tx_keyidx;
335 int auth_local_state_change;
336 int auth_p2p;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700337};
338
339
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800340static void wpa_driver_nl80211_deinit(struct i802_bss *bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700341static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx,
342 void *timeout_ctx);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800343static int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
344 enum nl80211_iftype nlmode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700345static int
346wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv);
347static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
348 const u8 *addr, int cmd, u16 reason_code,
349 int local_state_change);
350static void nl80211_remove_monitor_interface(
351 struct wpa_driver_nl80211_data *drv);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800352static int nl80211_send_frame_cmd(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700353 unsigned int freq, unsigned int wait,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800354 const u8 *buf, size_t buf_len, u64 *cookie,
355 int no_cck, int no_ack, int offchanok);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700356static int nl80211_register_frame(struct i802_bss *bss,
357 struct nl_handle *hl_handle,
358 u16 type, const u8 *match, size_t match_len);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800359static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss,
360 int report);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800361#ifdef ANDROID
362static int android_pno_start(struct i802_bss *bss,
363 struct wpa_driver_scan_params *params);
364static int android_pno_stop(struct i802_bss *bss);
365#endif /* ANDROID */
366#ifdef ANDROID_P2P
Dmitry Shmidt6e933c12011-09-27 12:29:26 -0700367int wpa_driver_set_p2p_noa(void *priv, u8 count, int start, int duration);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800368int wpa_driver_get_p2p_noa(void *priv, u8 *buf, size_t len);
Dmitry Shmidt6e933c12011-09-27 12:29:26 -0700369int wpa_driver_set_p2p_ps(void *priv, int legacy_ps, int opp_ps, int ctwindow);
370int wpa_driver_set_ap_wps_p2p_ie(void *priv, const struct wpabuf *beacon,
371 const struct wpabuf *proberesp,
372 const struct wpabuf *assocresp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700373
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800374#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700375#ifdef HOSTAPD
376static 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);
382#else /* HOSTAPD */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800383static inline void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
384{
385}
386
387static inline void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
388{
389}
390
391static inline int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700392{
393 return 0;
394}
395#endif /* HOSTAPD */
Dmitry Shmidt738a26e2011-07-07 14:22:14 -0700396#ifdef ANDROID
397extern int wpa_driver_nl80211_driver_cmd(void *priv, char *cmd, char *buf,
398 size_t buf_len);
399#endif
400
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800401static int wpa_driver_nl80211_set_freq(struct i802_bss *bss,
402 struct hostapd_freq_params *freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700403static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
404 int ifindex, int disabled);
405
406static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800407static int wpa_driver_nl80211_authenticate_retry(
408 struct wpa_driver_nl80211_data *drv);
409
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700410static int i802_set_iface_flags(struct i802_bss *bss, int up);
411
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800412
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700413static const char * nl80211_command_to_string(enum nl80211_commands cmd)
414{
415#define C2S(x) case x: return #x;
416 switch (cmd) {
417 C2S(NL80211_CMD_UNSPEC)
418 C2S(NL80211_CMD_GET_WIPHY)
419 C2S(NL80211_CMD_SET_WIPHY)
420 C2S(NL80211_CMD_NEW_WIPHY)
421 C2S(NL80211_CMD_DEL_WIPHY)
422 C2S(NL80211_CMD_GET_INTERFACE)
423 C2S(NL80211_CMD_SET_INTERFACE)
424 C2S(NL80211_CMD_NEW_INTERFACE)
425 C2S(NL80211_CMD_DEL_INTERFACE)
426 C2S(NL80211_CMD_GET_KEY)
427 C2S(NL80211_CMD_SET_KEY)
428 C2S(NL80211_CMD_NEW_KEY)
429 C2S(NL80211_CMD_DEL_KEY)
430 C2S(NL80211_CMD_GET_BEACON)
431 C2S(NL80211_CMD_SET_BEACON)
432 C2S(NL80211_CMD_START_AP)
433 C2S(NL80211_CMD_STOP_AP)
434 C2S(NL80211_CMD_GET_STATION)
435 C2S(NL80211_CMD_SET_STATION)
436 C2S(NL80211_CMD_NEW_STATION)
437 C2S(NL80211_CMD_DEL_STATION)
438 C2S(NL80211_CMD_GET_MPATH)
439 C2S(NL80211_CMD_SET_MPATH)
440 C2S(NL80211_CMD_NEW_MPATH)
441 C2S(NL80211_CMD_DEL_MPATH)
442 C2S(NL80211_CMD_SET_BSS)
443 C2S(NL80211_CMD_SET_REG)
444 C2S(NL80211_CMD_REQ_SET_REG)
445 C2S(NL80211_CMD_GET_MESH_CONFIG)
446 C2S(NL80211_CMD_SET_MESH_CONFIG)
447 C2S(NL80211_CMD_SET_MGMT_EXTRA_IE)
448 C2S(NL80211_CMD_GET_REG)
449 C2S(NL80211_CMD_GET_SCAN)
450 C2S(NL80211_CMD_TRIGGER_SCAN)
451 C2S(NL80211_CMD_NEW_SCAN_RESULTS)
452 C2S(NL80211_CMD_SCAN_ABORTED)
453 C2S(NL80211_CMD_REG_CHANGE)
454 C2S(NL80211_CMD_AUTHENTICATE)
455 C2S(NL80211_CMD_ASSOCIATE)
456 C2S(NL80211_CMD_DEAUTHENTICATE)
457 C2S(NL80211_CMD_DISASSOCIATE)
458 C2S(NL80211_CMD_MICHAEL_MIC_FAILURE)
459 C2S(NL80211_CMD_REG_BEACON_HINT)
460 C2S(NL80211_CMD_JOIN_IBSS)
461 C2S(NL80211_CMD_LEAVE_IBSS)
462 C2S(NL80211_CMD_TESTMODE)
463 C2S(NL80211_CMD_CONNECT)
464 C2S(NL80211_CMD_ROAM)
465 C2S(NL80211_CMD_DISCONNECT)
466 C2S(NL80211_CMD_SET_WIPHY_NETNS)
467 C2S(NL80211_CMD_GET_SURVEY)
468 C2S(NL80211_CMD_NEW_SURVEY_RESULTS)
469 C2S(NL80211_CMD_SET_PMKSA)
470 C2S(NL80211_CMD_DEL_PMKSA)
471 C2S(NL80211_CMD_FLUSH_PMKSA)
472 C2S(NL80211_CMD_REMAIN_ON_CHANNEL)
473 C2S(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL)
474 C2S(NL80211_CMD_SET_TX_BITRATE_MASK)
475 C2S(NL80211_CMD_REGISTER_FRAME)
476 C2S(NL80211_CMD_FRAME)
477 C2S(NL80211_CMD_FRAME_TX_STATUS)
478 C2S(NL80211_CMD_SET_POWER_SAVE)
479 C2S(NL80211_CMD_GET_POWER_SAVE)
480 C2S(NL80211_CMD_SET_CQM)
481 C2S(NL80211_CMD_NOTIFY_CQM)
482 C2S(NL80211_CMD_SET_CHANNEL)
483 C2S(NL80211_CMD_SET_WDS_PEER)
484 C2S(NL80211_CMD_FRAME_WAIT_CANCEL)
485 C2S(NL80211_CMD_JOIN_MESH)
486 C2S(NL80211_CMD_LEAVE_MESH)
487 C2S(NL80211_CMD_UNPROT_DEAUTHENTICATE)
488 C2S(NL80211_CMD_UNPROT_DISASSOCIATE)
489 C2S(NL80211_CMD_NEW_PEER_CANDIDATE)
490 C2S(NL80211_CMD_GET_WOWLAN)
491 C2S(NL80211_CMD_SET_WOWLAN)
492 C2S(NL80211_CMD_START_SCHED_SCAN)
493 C2S(NL80211_CMD_STOP_SCHED_SCAN)
494 C2S(NL80211_CMD_SCHED_SCAN_RESULTS)
495 C2S(NL80211_CMD_SCHED_SCAN_STOPPED)
496 C2S(NL80211_CMD_SET_REKEY_OFFLOAD)
497 C2S(NL80211_CMD_PMKSA_CANDIDATE)
498 C2S(NL80211_CMD_TDLS_OPER)
499 C2S(NL80211_CMD_TDLS_MGMT)
500 C2S(NL80211_CMD_UNEXPECTED_FRAME)
501 C2S(NL80211_CMD_PROBE_CLIENT)
502 C2S(NL80211_CMD_REGISTER_BEACONS)
503 C2S(NL80211_CMD_UNEXPECTED_4ADDR_FRAME)
504 C2S(NL80211_CMD_SET_NOACK_MAP)
505 C2S(NL80211_CMD_CH_SWITCH_NOTIFY)
506 C2S(NL80211_CMD_START_P2P_DEVICE)
507 C2S(NL80211_CMD_STOP_P2P_DEVICE)
508 C2S(NL80211_CMD_CONN_FAILED)
509 C2S(NL80211_CMD_SET_MCAST_RATE)
510 C2S(NL80211_CMD_SET_MAC_ACL)
511 C2S(NL80211_CMD_RADAR_DETECT)
512 C2S(NL80211_CMD_GET_PROTOCOL_FEATURES)
513 C2S(NL80211_CMD_UPDATE_FT_IES)
514 C2S(NL80211_CMD_FT_EVENT)
515 C2S(NL80211_CMD_CRIT_PROTOCOL_START)
516 C2S(NL80211_CMD_CRIT_PROTOCOL_STOP)
517 default:
518 return "NL80211_CMD_UNKNOWN";
519 }
520#undef C2S
521}
522
523
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800524static int is_ap_interface(enum nl80211_iftype nlmode)
525{
526 return (nlmode == NL80211_IFTYPE_AP ||
527 nlmode == NL80211_IFTYPE_P2P_GO);
528}
529
530
531static int is_sta_interface(enum nl80211_iftype nlmode)
532{
533 return (nlmode == NL80211_IFTYPE_STATION ||
534 nlmode == NL80211_IFTYPE_P2P_CLIENT);
535}
536
537
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700538static int is_p2p_net_interface(enum nl80211_iftype nlmode)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800539{
540 return (nlmode == NL80211_IFTYPE_P2P_CLIENT ||
541 nlmode == NL80211_IFTYPE_P2P_GO);
542}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700543
544
Dmitry Shmidt8bae4132013-06-06 11:25:10 -0700545static void nl80211_mark_disconnected(struct wpa_driver_nl80211_data *drv)
546{
547 if (drv->associated)
548 os_memcpy(drv->prev_bssid, drv->bssid, ETH_ALEN);
549 drv->associated = 0;
550 os_memset(drv->bssid, 0, ETH_ALEN);
551}
552
553
Jouni Malinen87fd2792011-05-16 18:35:42 +0300554struct nl80211_bss_info_arg {
555 struct wpa_driver_nl80211_data *drv;
556 struct wpa_scan_results *res;
557 unsigned int assoc_freq;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800558 u8 assoc_bssid[ETH_ALEN];
Jouni Malinen87fd2792011-05-16 18:35:42 +0300559};
560
561static int bss_info_handler(struct nl_msg *msg, void *arg);
562
563
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700564/* nl80211 code */
565static int ack_handler(struct nl_msg *msg, void *arg)
566{
567 int *err = arg;
568 *err = 0;
569 return NL_STOP;
570}
571
572static int finish_handler(struct nl_msg *msg, void *arg)
573{
574 int *ret = arg;
575 *ret = 0;
576 return NL_SKIP;
577}
578
579static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
580 void *arg)
581{
582 int *ret = arg;
583 *ret = err->error;
584 return NL_SKIP;
585}
586
587
588static int no_seq_check(struct nl_msg *msg, void *arg)
589{
590 return NL_OK;
591}
592
593
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800594static int send_and_recv(struct nl80211_global *global,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700595 struct nl_handle *nl_handle, struct nl_msg *msg,
596 int (*valid_handler)(struct nl_msg *, void *),
597 void *valid_data)
598{
599 struct nl_cb *cb;
600 int err = -ENOMEM;
601
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800602 cb = nl_cb_clone(global->nl_cb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700603 if (!cb)
604 goto out;
605
606 err = nl_send_auto_complete(nl_handle, msg);
607 if (err < 0)
608 goto out;
609
610 err = 1;
611
612 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
613 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
614 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
615
616 if (valid_handler)
617 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM,
618 valid_handler, valid_data);
619
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700620 while (err > 0) {
621 int res = nl_recvmsgs(nl_handle, cb);
622 if (res) {
623 wpa_printf(MSG_INFO,
624 "nl80211: %s->nl_recvmsgs failed: %d",
625 __func__, res);
626 }
627 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700628 out:
629 nl_cb_put(cb);
630 nlmsg_free(msg);
631 return err;
632}
633
634
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800635static int send_and_recv_msgs_global(struct nl80211_global *global,
636 struct nl_msg *msg,
637 int (*valid_handler)(struct nl_msg *, void *),
638 void *valid_data)
639{
640 return send_and_recv(global, global->nl, msg, valid_handler,
641 valid_data);
642}
643
Dmitry Shmidt04949592012-07-19 12:16:46 -0700644
Dmitry Shmidt641185e2013-11-06 15:17:13 -0800645static int send_and_recv_msgs(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700646 struct nl_msg *msg,
647 int (*valid_handler)(struct nl_msg *, void *),
648 void *valid_data)
649{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800650 return send_and_recv(drv->global, drv->global->nl, msg,
651 valid_handler, valid_data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700652}
653
654
655struct family_data {
656 const char *group;
657 int id;
658};
659
660
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700661static int nl80211_set_iface_id(struct nl_msg *msg, struct i802_bss *bss)
662{
663 if (bss->wdev_id_set)
664 NLA_PUT_U64(msg, NL80211_ATTR_WDEV, bss->wdev_id);
665 else
666 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
667 return 0;
668
669nla_put_failure:
670 return -1;
671}
672
673
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700674static int family_handler(struct nl_msg *msg, void *arg)
675{
676 struct family_data *res = arg;
677 struct nlattr *tb[CTRL_ATTR_MAX + 1];
678 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
679 struct nlattr *mcgrp;
680 int i;
681
682 nla_parse(tb, CTRL_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
683 genlmsg_attrlen(gnlh, 0), NULL);
684 if (!tb[CTRL_ATTR_MCAST_GROUPS])
685 return NL_SKIP;
686
687 nla_for_each_nested(mcgrp, tb[CTRL_ATTR_MCAST_GROUPS], i) {
688 struct nlattr *tb2[CTRL_ATTR_MCAST_GRP_MAX + 1];
689 nla_parse(tb2, CTRL_ATTR_MCAST_GRP_MAX, nla_data(mcgrp),
690 nla_len(mcgrp), NULL);
691 if (!tb2[CTRL_ATTR_MCAST_GRP_NAME] ||
692 !tb2[CTRL_ATTR_MCAST_GRP_ID] ||
693 os_strncmp(nla_data(tb2[CTRL_ATTR_MCAST_GRP_NAME]),
694 res->group,
695 nla_len(tb2[CTRL_ATTR_MCAST_GRP_NAME])) != 0)
696 continue;
697 res->id = nla_get_u32(tb2[CTRL_ATTR_MCAST_GRP_ID]);
698 break;
699 };
700
701 return NL_SKIP;
702}
703
704
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800705static int nl_get_multicast_id(struct nl80211_global *global,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700706 const char *family, const char *group)
707{
708 struct nl_msg *msg;
709 int ret = -1;
710 struct family_data res = { group, -ENOENT };
711
712 msg = nlmsg_alloc();
713 if (!msg)
714 return -ENOMEM;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800715 genlmsg_put(msg, 0, 0, genl_ctrl_resolve(global->nl, "nlctrl"),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700716 0, 0, CTRL_CMD_GETFAMILY, 0);
717 NLA_PUT_STRING(msg, CTRL_ATTR_FAMILY_NAME, family);
718
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800719 ret = send_and_recv_msgs_global(global, msg, family_handler, &res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700720 msg = NULL;
721 if (ret == 0)
722 ret = res.id;
723
724nla_put_failure:
725 nlmsg_free(msg);
726 return ret;
727}
728
729
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800730static void * nl80211_cmd(struct wpa_driver_nl80211_data *drv,
731 struct nl_msg *msg, int flags, uint8_t cmd)
732{
733 return genlmsg_put(msg, 0, 0, drv->global->nl80211_id,
734 0, flags, cmd, 0);
735}
736
737
738struct wiphy_idx_data {
739 int wiphy_idx;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700740 enum nl80211_iftype nlmode;
741 u8 *macaddr;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800742};
743
744
745static int netdev_info_handler(struct nl_msg *msg, void *arg)
746{
747 struct nlattr *tb[NL80211_ATTR_MAX + 1];
748 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
749 struct wiphy_idx_data *info = arg;
750
751 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
752 genlmsg_attrlen(gnlh, 0), NULL);
753
754 if (tb[NL80211_ATTR_WIPHY])
755 info->wiphy_idx = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
756
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700757 if (tb[NL80211_ATTR_IFTYPE])
758 info->nlmode = nla_get_u32(tb[NL80211_ATTR_IFTYPE]);
759
760 if (tb[NL80211_ATTR_MAC] && info->macaddr)
761 os_memcpy(info->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
762 ETH_ALEN);
763
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800764 return NL_SKIP;
765}
766
767
768static int nl80211_get_wiphy_index(struct i802_bss *bss)
769{
770 struct nl_msg *msg;
771 struct wiphy_idx_data data = {
772 .wiphy_idx = -1,
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700773 .macaddr = NULL,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800774 };
775
776 msg = nlmsg_alloc();
777 if (!msg)
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700778 return NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800779
780 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_GET_INTERFACE);
781
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700782 if (nl80211_set_iface_id(msg, bss) < 0)
783 goto nla_put_failure;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800784
785 if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0)
786 return data.wiphy_idx;
787 msg = NULL;
788nla_put_failure:
789 nlmsg_free(msg);
790 return -1;
791}
792
793
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700794static enum nl80211_iftype nl80211_get_ifmode(struct i802_bss *bss)
795{
796 struct nl_msg *msg;
797 struct wiphy_idx_data data = {
798 .nlmode = NL80211_IFTYPE_UNSPECIFIED,
799 .macaddr = NULL,
800 };
801
802 msg = nlmsg_alloc();
803 if (!msg)
804 return -1;
805
806 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_GET_INTERFACE);
807
808 if (nl80211_set_iface_id(msg, bss) < 0)
809 goto nla_put_failure;
810
811 if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0)
812 return data.nlmode;
813 msg = NULL;
814nla_put_failure:
815 nlmsg_free(msg);
816 return NL80211_IFTYPE_UNSPECIFIED;
817}
818
819
820#ifndef HOSTAPD
821static int nl80211_get_macaddr(struct i802_bss *bss)
822{
823 struct nl_msg *msg;
824 struct wiphy_idx_data data = {
825 .macaddr = bss->addr,
826 };
827
828 msg = nlmsg_alloc();
829 if (!msg)
830 return NL80211_IFTYPE_UNSPECIFIED;
831
832 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_GET_INTERFACE);
833 if (nl80211_set_iface_id(msg, bss) < 0)
834 goto nla_put_failure;
835
836 return send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data);
837
838nla_put_failure:
839 nlmsg_free(msg);
840 return NL80211_IFTYPE_UNSPECIFIED;
841}
842#endif /* HOSTAPD */
843
844
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800845static int nl80211_register_beacons(struct wpa_driver_nl80211_data *drv,
846 struct nl80211_wiphy_data *w)
847{
848 struct nl_msg *msg;
849 int ret = -1;
850
851 msg = nlmsg_alloc();
852 if (!msg)
853 return -1;
854
855 nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_BEACONS);
856
857 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, w->wiphy_idx);
858
859 ret = send_and_recv(drv->global, w->nl_beacons, msg, NULL, NULL);
860 msg = NULL;
861 if (ret) {
862 wpa_printf(MSG_DEBUG, "nl80211: Register beacons command "
863 "failed: ret=%d (%s)",
864 ret, strerror(-ret));
865 goto nla_put_failure;
866 }
867 ret = 0;
868nla_put_failure:
869 nlmsg_free(msg);
870 return ret;
871}
872
873
874static void nl80211_recv_beacons(int sock, void *eloop_ctx, void *handle)
875{
876 struct nl80211_wiphy_data *w = eloop_ctx;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700877 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800878
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800879 wpa_printf(MSG_EXCESSIVE, "nl80211: Beacon event message available");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800880
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700881 res = nl_recvmsgs(handle, w->nl_cb);
882 if (res) {
883 wpa_printf(MSG_INFO, "nl80211: %s->nl_recvmsgs failed: %d",
884 __func__, res);
885 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800886}
887
888
889static int process_beacon_event(struct nl_msg *msg, void *arg)
890{
891 struct nl80211_wiphy_data *w = arg;
892 struct wpa_driver_nl80211_data *drv;
893 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
894 struct nlattr *tb[NL80211_ATTR_MAX + 1];
895 union wpa_event_data event;
896
897 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
898 genlmsg_attrlen(gnlh, 0), NULL);
899
900 if (gnlh->cmd != NL80211_CMD_FRAME) {
901 wpa_printf(MSG_DEBUG, "nl80211: Unexpected beacon event? (%d)",
902 gnlh->cmd);
903 return NL_SKIP;
904 }
905
906 if (!tb[NL80211_ATTR_FRAME])
907 return NL_SKIP;
908
909 dl_list_for_each(drv, &w->drvs, struct wpa_driver_nl80211_data,
910 wiphy_list) {
911 os_memset(&event, 0, sizeof(event));
912 event.rx_mgmt.frame = nla_data(tb[NL80211_ATTR_FRAME]);
913 event.rx_mgmt.frame_len = nla_len(tb[NL80211_ATTR_FRAME]);
914 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
915 }
916
917 return NL_SKIP;
918}
919
920
921static struct nl80211_wiphy_data *
922nl80211_get_wiphy_data_ap(struct i802_bss *bss)
923{
924 static DEFINE_DL_LIST(nl80211_wiphys);
925 struct nl80211_wiphy_data *w;
926 int wiphy_idx, found = 0;
927 struct i802_bss *tmp_bss;
928
929 if (bss->wiphy_data != NULL)
930 return bss->wiphy_data;
931
932 wiphy_idx = nl80211_get_wiphy_index(bss);
933
934 dl_list_for_each(w, &nl80211_wiphys, struct nl80211_wiphy_data, list) {
935 if (w->wiphy_idx == wiphy_idx)
936 goto add;
937 }
938
939 /* alloc new one */
940 w = os_zalloc(sizeof(*w));
941 if (w == NULL)
942 return NULL;
943 w->wiphy_idx = wiphy_idx;
944 dl_list_init(&w->bsss);
945 dl_list_init(&w->drvs);
946
947 w->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
948 if (!w->nl_cb) {
949 os_free(w);
950 return NULL;
951 }
952 nl_cb_set(w->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL);
953 nl_cb_set(w->nl_cb, NL_CB_VALID, NL_CB_CUSTOM, process_beacon_event,
954 w);
955
956 w->nl_beacons = nl_create_handle(bss->drv->global->nl_cb,
957 "wiphy beacons");
958 if (w->nl_beacons == NULL) {
959 os_free(w);
960 return NULL;
961 }
962
963 if (nl80211_register_beacons(bss->drv, w)) {
964 nl_destroy_handles(&w->nl_beacons);
965 os_free(w);
966 return NULL;
967 }
968
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700969 nl80211_register_eloop_read(&w->nl_beacons, nl80211_recv_beacons, w);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800970
971 dl_list_add(&nl80211_wiphys, &w->list);
972
973add:
974 /* drv entry for this bss already there? */
975 dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
976 if (tmp_bss->drv == bss->drv) {
977 found = 1;
978 break;
979 }
980 }
981 /* if not add it */
982 if (!found)
983 dl_list_add(&w->drvs, &bss->drv->wiphy_list);
984
985 dl_list_add(&w->bsss, &bss->wiphy_list);
986 bss->wiphy_data = w;
987 return w;
988}
989
990
991static void nl80211_put_wiphy_data_ap(struct i802_bss *bss)
992{
993 struct nl80211_wiphy_data *w = bss->wiphy_data;
994 struct i802_bss *tmp_bss;
995 int found = 0;
996
997 if (w == NULL)
998 return;
999 bss->wiphy_data = NULL;
1000 dl_list_del(&bss->wiphy_list);
1001
1002 /* still any for this drv present? */
1003 dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
1004 if (tmp_bss->drv == bss->drv) {
1005 found = 1;
1006 break;
1007 }
1008 }
1009 /* if not remove it */
1010 if (!found)
1011 dl_list_del(&bss->drv->wiphy_list);
1012
1013 if (!dl_list_empty(&w->bsss))
1014 return;
1015
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001016 nl80211_destroy_eloop_handle(&w->nl_beacons);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001017
1018 nl_cb_put(w->nl_cb);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001019 dl_list_del(&w->list);
1020 os_free(w);
1021}
1022
1023
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001024static int wpa_driver_nl80211_get_bssid(void *priv, u8 *bssid)
1025{
1026 struct i802_bss *bss = priv;
1027 struct wpa_driver_nl80211_data *drv = bss->drv;
1028 if (!drv->associated)
1029 return -1;
1030 os_memcpy(bssid, drv->bssid, ETH_ALEN);
1031 return 0;
1032}
1033
1034
1035static int wpa_driver_nl80211_get_ssid(void *priv, u8 *ssid)
1036{
1037 struct i802_bss *bss = priv;
1038 struct wpa_driver_nl80211_data *drv = bss->drv;
1039 if (!drv->associated)
1040 return -1;
1041 os_memcpy(ssid, drv->ssid, drv->ssid_len);
1042 return drv->ssid_len;
1043}
1044
1045
1046static void wpa_driver_nl80211_event_link(struct wpa_driver_nl80211_data *drv,
1047 char *buf, size_t len, int del)
1048{
1049 union wpa_event_data event;
1050
1051 os_memset(&event, 0, sizeof(event));
1052 if (len > sizeof(event.interface_status.ifname))
1053 len = sizeof(event.interface_status.ifname) - 1;
1054 os_memcpy(event.interface_status.ifname, buf, len);
1055 event.interface_status.ievent = del ? EVENT_INTERFACE_REMOVED :
1056 EVENT_INTERFACE_ADDED;
1057
1058 wpa_printf(MSG_DEBUG, "RTM_%sLINK, IFLA_IFNAME: Interface '%s' %s",
1059 del ? "DEL" : "NEW",
1060 event.interface_status.ifname,
1061 del ? "removed" : "added");
1062
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001063 if (os_strcmp(drv->first_bss->ifname, event.interface_status.ifname) ==
1064 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001065 if (del) {
1066 if (drv->if_removed) {
1067 wpa_printf(MSG_DEBUG, "nl80211: if_removed "
1068 "already set - ignore event");
1069 return;
1070 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001071 drv->if_removed = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001072 } else {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001073 if (if_nametoindex(drv->first_bss->ifname) == 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001074 wpa_printf(MSG_DEBUG, "nl80211: Interface %s "
1075 "does not exist - ignore "
1076 "RTM_NEWLINK",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001077 drv->first_bss->ifname);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001078 return;
1079 }
1080 if (!drv->if_removed) {
1081 wpa_printf(MSG_DEBUG, "nl80211: if_removed "
1082 "already cleared - ignore event");
1083 return;
1084 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001085 drv->if_removed = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001086 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001087 }
1088
1089 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
1090}
1091
1092
1093static int wpa_driver_nl80211_own_ifname(struct wpa_driver_nl80211_data *drv,
1094 u8 *buf, size_t len)
1095{
1096 int attrlen, rta_len;
1097 struct rtattr *attr;
1098
1099 attrlen = len;
1100 attr = (struct rtattr *) buf;
1101
1102 rta_len = RTA_ALIGN(sizeof(struct rtattr));
1103 while (RTA_OK(attr, attrlen)) {
1104 if (attr->rta_type == IFLA_IFNAME) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001105 if (os_strcmp(((char *) attr) + rta_len,
1106 drv->first_bss->ifname) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001107 return 1;
1108 else
1109 break;
1110 }
1111 attr = RTA_NEXT(attr, attrlen);
1112 }
1113
1114 return 0;
1115}
1116
1117
1118static int wpa_driver_nl80211_own_ifindex(struct wpa_driver_nl80211_data *drv,
1119 int ifindex, u8 *buf, size_t len)
1120{
1121 if (drv->ifindex == ifindex)
1122 return 1;
1123
1124 if (drv->if_removed && wpa_driver_nl80211_own_ifname(drv, buf, len)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001125 wpa_printf(MSG_DEBUG, "nl80211: Update ifindex for a removed "
1126 "interface");
1127 wpa_driver_nl80211_finish_drv_init(drv);
1128 return 1;
1129 }
1130
1131 return 0;
1132}
1133
1134
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001135static struct wpa_driver_nl80211_data *
1136nl80211_find_drv(struct nl80211_global *global, int idx, u8 *buf, size_t len)
1137{
1138 struct wpa_driver_nl80211_data *drv;
1139 dl_list_for_each(drv, &global->interfaces,
1140 struct wpa_driver_nl80211_data, list) {
1141 if (wpa_driver_nl80211_own_ifindex(drv, idx, buf, len) ||
1142 have_ifidx(drv, idx))
1143 return drv;
1144 }
1145 return NULL;
1146}
1147
1148
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001149static void wpa_driver_nl80211_event_rtm_newlink(void *ctx,
1150 struct ifinfomsg *ifi,
1151 u8 *buf, size_t len)
1152{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001153 struct nl80211_global *global = ctx;
1154 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001155 int attrlen, rta_len;
1156 struct rtattr *attr;
1157 u32 brid = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001158 char namebuf[IFNAMSIZ];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001159
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001160 drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
1161 if (!drv) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001162 wpa_printf(MSG_DEBUG, "nl80211: Ignore event for foreign "
1163 "ifindex %d", ifi->ifi_index);
1164 return;
1165 }
1166
1167 wpa_printf(MSG_DEBUG, "RTM_NEWLINK: operstate=%d ifi_flags=0x%x "
1168 "(%s%s%s%s)",
1169 drv->operstate, ifi->ifi_flags,
1170 (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
1171 (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
1172 (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
1173 (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
1174
1175 if (!drv->if_disabled && !(ifi->ifi_flags & IFF_UP)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001176 if (if_indextoname(ifi->ifi_index, namebuf) &&
1177 linux_iface_up(drv->global->ioctl_sock,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001178 drv->first_bss->ifname) > 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001179 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
1180 "event since interface %s is up", namebuf);
1181 return;
1182 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001183 wpa_printf(MSG_DEBUG, "nl80211: Interface down");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001184 if (drv->ignore_if_down_event) {
1185 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
1186 "event generated by mode change");
1187 drv->ignore_if_down_event = 0;
1188 } else {
1189 drv->if_disabled = 1;
1190 wpa_supplicant_event(drv->ctx,
1191 EVENT_INTERFACE_DISABLED, NULL);
1192 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001193 }
1194
1195 if (drv->if_disabled && (ifi->ifi_flags & IFF_UP)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001196 if (if_indextoname(ifi->ifi_index, namebuf) &&
1197 linux_iface_up(drv->global->ioctl_sock,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001198 drv->first_bss->ifname) == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001199 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
1200 "event since interface %s is down",
1201 namebuf);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001202 } else if (if_nametoindex(drv->first_bss->ifname) == 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001203 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
1204 "event since interface %s does not exist",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001205 drv->first_bss->ifname);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001206 } else if (drv->if_removed) {
1207 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
1208 "event since interface %s is marked "
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001209 "removed", drv->first_bss->ifname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001210 } else {
1211 wpa_printf(MSG_DEBUG, "nl80211: Interface up");
1212 drv->if_disabled = 0;
1213 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_ENABLED,
1214 NULL);
1215 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001216 }
1217
1218 /*
1219 * Some drivers send the association event before the operup event--in
1220 * this case, lifting operstate in wpa_driver_nl80211_set_operstate()
1221 * fails. This will hit us when wpa_supplicant does not need to do
1222 * IEEE 802.1X authentication
1223 */
1224 if (drv->operstate == 1 &&
1225 (ifi->ifi_flags & (IFF_LOWER_UP | IFF_DORMANT)) == IFF_LOWER_UP &&
1226 !(ifi->ifi_flags & IFF_RUNNING))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001227 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001228 -1, IF_OPER_UP);
1229
1230 attrlen = len;
1231 attr = (struct rtattr *) buf;
1232 rta_len = RTA_ALIGN(sizeof(struct rtattr));
1233 while (RTA_OK(attr, attrlen)) {
1234 if (attr->rta_type == IFLA_IFNAME) {
1235 wpa_driver_nl80211_event_link(
1236 drv,
1237 ((char *) attr) + rta_len,
1238 attr->rta_len - rta_len, 0);
1239 } else if (attr->rta_type == IFLA_MASTER)
1240 brid = nla_get_u32((struct nlattr *) attr);
1241 attr = RTA_NEXT(attr, attrlen);
1242 }
1243
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001244 if (ifi->ifi_family == AF_BRIDGE && brid) {
1245 /* device has been added to bridge */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001246 if_indextoname(brid, namebuf);
1247 wpa_printf(MSG_DEBUG, "nl80211: Add ifindex %u for bridge %s",
1248 brid, namebuf);
1249 add_ifidx(drv, brid);
1250 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001251}
1252
1253
1254static void wpa_driver_nl80211_event_rtm_dellink(void *ctx,
1255 struct ifinfomsg *ifi,
1256 u8 *buf, size_t len)
1257{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001258 struct nl80211_global *global = ctx;
1259 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001260 int attrlen, rta_len;
1261 struct rtattr *attr;
1262 u32 brid = 0;
1263
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001264 drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
1265 if (!drv) {
1266 wpa_printf(MSG_DEBUG, "nl80211: Ignore dellink event for "
1267 "foreign ifindex %d", ifi->ifi_index);
1268 return;
1269 }
1270
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001271 attrlen = len;
1272 attr = (struct rtattr *) buf;
1273
1274 rta_len = RTA_ALIGN(sizeof(struct rtattr));
1275 while (RTA_OK(attr, attrlen)) {
1276 if (attr->rta_type == IFLA_IFNAME) {
1277 wpa_driver_nl80211_event_link(
1278 drv,
1279 ((char *) attr) + rta_len,
1280 attr->rta_len - rta_len, 1);
1281 } else if (attr->rta_type == IFLA_MASTER)
1282 brid = nla_get_u32((struct nlattr *) attr);
1283 attr = RTA_NEXT(attr, attrlen);
1284 }
1285
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001286 if (ifi->ifi_family == AF_BRIDGE && brid) {
1287 /* device has been removed from bridge */
1288 char namebuf[IFNAMSIZ];
1289 if_indextoname(brid, namebuf);
1290 wpa_printf(MSG_DEBUG, "nl80211: Remove ifindex %u for bridge "
1291 "%s", brid, namebuf);
1292 del_ifidx(drv, brid);
1293 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001294}
1295
1296
1297static void mlme_event_auth(struct wpa_driver_nl80211_data *drv,
1298 const u8 *frame, size_t len)
1299{
1300 const struct ieee80211_mgmt *mgmt;
1301 union wpa_event_data event;
1302
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001303 wpa_printf(MSG_DEBUG, "nl80211: Authenticate event");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001304 mgmt = (const struct ieee80211_mgmt *) frame;
1305 if (len < 24 + sizeof(mgmt->u.auth)) {
1306 wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
1307 "frame");
1308 return;
1309 }
1310
1311 os_memcpy(drv->auth_bssid, mgmt->sa, ETH_ALEN);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001312 os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001313 os_memset(&event, 0, sizeof(event));
1314 os_memcpy(event.auth.peer, mgmt->sa, ETH_ALEN);
1315 event.auth.auth_type = le_to_host16(mgmt->u.auth.auth_alg);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001316 event.auth.auth_transaction =
1317 le_to_host16(mgmt->u.auth.auth_transaction);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001318 event.auth.status_code = le_to_host16(mgmt->u.auth.status_code);
1319 if (len > 24 + sizeof(mgmt->u.auth)) {
1320 event.auth.ies = mgmt->u.auth.variable;
1321 event.auth.ies_len = len - 24 - sizeof(mgmt->u.auth);
1322 }
1323
1324 wpa_supplicant_event(drv->ctx, EVENT_AUTH, &event);
1325}
1326
1327
Jouni Malinen87fd2792011-05-16 18:35:42 +03001328static unsigned int nl80211_get_assoc_freq(struct wpa_driver_nl80211_data *drv)
1329{
1330 struct nl_msg *msg;
1331 int ret;
1332 struct nl80211_bss_info_arg arg;
1333
1334 os_memset(&arg, 0, sizeof(arg));
1335 msg = nlmsg_alloc();
1336 if (!msg)
1337 goto nla_put_failure;
1338
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001339 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
Jouni Malinen87fd2792011-05-16 18:35:42 +03001340 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
1341
1342 arg.drv = drv;
1343 ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
1344 msg = NULL;
1345 if (ret == 0) {
1346 wpa_printf(MSG_DEBUG, "nl80211: Operating frequency for the "
1347 "associated BSS from scan results: %u MHz",
1348 arg.assoc_freq);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001349 if (arg.assoc_freq)
1350 drv->assoc_freq = arg.assoc_freq;
1351 return drv->assoc_freq;
Jouni Malinen87fd2792011-05-16 18:35:42 +03001352 }
1353 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
1354 "(%s)", ret, strerror(-ret));
1355nla_put_failure:
1356 nlmsg_free(msg);
1357 return drv->assoc_freq;
1358}
1359
1360
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001361static void mlme_event_assoc(struct wpa_driver_nl80211_data *drv,
1362 const u8 *frame, size_t len)
1363{
1364 const struct ieee80211_mgmt *mgmt;
1365 union wpa_event_data event;
1366 u16 status;
1367
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001368 wpa_printf(MSG_DEBUG, "nl80211: Associate event");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001369 mgmt = (const struct ieee80211_mgmt *) frame;
1370 if (len < 24 + sizeof(mgmt->u.assoc_resp)) {
1371 wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
1372 "frame");
1373 return;
1374 }
1375
1376 status = le_to_host16(mgmt->u.assoc_resp.status_code);
1377 if (status != WLAN_STATUS_SUCCESS) {
1378 os_memset(&event, 0, sizeof(event));
1379 event.assoc_reject.bssid = mgmt->bssid;
1380 if (len > 24 + sizeof(mgmt->u.assoc_resp)) {
1381 event.assoc_reject.resp_ies =
1382 (u8 *) mgmt->u.assoc_resp.variable;
1383 event.assoc_reject.resp_ies_len =
1384 len - 24 - sizeof(mgmt->u.assoc_resp);
1385 }
1386 event.assoc_reject.status_code = status;
1387
1388 wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
1389 return;
1390 }
1391
1392 drv->associated = 1;
1393 os_memcpy(drv->bssid, mgmt->sa, ETH_ALEN);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001394 os_memcpy(drv->prev_bssid, mgmt->sa, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001395
1396 os_memset(&event, 0, sizeof(event));
1397 if (len > 24 + sizeof(mgmt->u.assoc_resp)) {
1398 event.assoc_info.resp_ies = (u8 *) mgmt->u.assoc_resp.variable;
1399 event.assoc_info.resp_ies_len =
1400 len - 24 - sizeof(mgmt->u.assoc_resp);
1401 }
1402
1403 event.assoc_info.freq = drv->assoc_freq;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001404
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001405 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
1406}
1407
1408
1409static void mlme_event_connect(struct wpa_driver_nl80211_data *drv,
1410 enum nl80211_commands cmd, struct nlattr *status,
1411 struct nlattr *addr, struct nlattr *req_ie,
1412 struct nlattr *resp_ie)
1413{
1414 union wpa_event_data event;
1415
1416 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
1417 /*
1418 * Avoid reporting two association events that would confuse
1419 * the core code.
1420 */
1421 wpa_printf(MSG_DEBUG, "nl80211: Ignore connect event (cmd=%d) "
1422 "when using userspace SME", cmd);
1423 return;
1424 }
1425
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001426 if (cmd == NL80211_CMD_CONNECT)
1427 wpa_printf(MSG_DEBUG, "nl80211: Connect event");
1428 else if (cmd == NL80211_CMD_ROAM)
1429 wpa_printf(MSG_DEBUG, "nl80211: Roam event");
1430
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001431 os_memset(&event, 0, sizeof(event));
1432 if (cmd == NL80211_CMD_CONNECT &&
1433 nla_get_u16(status) != WLAN_STATUS_SUCCESS) {
1434 if (addr)
1435 event.assoc_reject.bssid = nla_data(addr);
1436 if (resp_ie) {
1437 event.assoc_reject.resp_ies = nla_data(resp_ie);
1438 event.assoc_reject.resp_ies_len = nla_len(resp_ie);
1439 }
1440 event.assoc_reject.status_code = nla_get_u16(status);
1441 wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
1442 return;
1443 }
1444
1445 drv->associated = 1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001446 if (addr) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001447 os_memcpy(drv->bssid, nla_data(addr), ETH_ALEN);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001448 os_memcpy(drv->prev_bssid, drv->bssid, ETH_ALEN);
1449 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001450
1451 if (req_ie) {
1452 event.assoc_info.req_ies = nla_data(req_ie);
1453 event.assoc_info.req_ies_len = nla_len(req_ie);
1454 }
1455 if (resp_ie) {
1456 event.assoc_info.resp_ies = nla_data(resp_ie);
1457 event.assoc_info.resp_ies_len = nla_len(resp_ie);
1458 }
1459
Jouni Malinen87fd2792011-05-16 18:35:42 +03001460 event.assoc_info.freq = nl80211_get_assoc_freq(drv);
1461
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001462 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
1463}
1464
1465
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001466static void mlme_event_disconnect(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001467 struct nlattr *reason, struct nlattr *addr,
1468 struct nlattr *by_ap)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001469{
1470 union wpa_event_data data;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001471 unsigned int locally_generated = by_ap == NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001472
1473 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
1474 /*
1475 * Avoid reporting two disassociation events that could
1476 * confuse the core code.
1477 */
1478 wpa_printf(MSG_DEBUG, "nl80211: Ignore disconnect "
1479 "event when using userspace SME");
1480 return;
1481 }
1482
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001483 if (drv->ignore_next_local_disconnect) {
1484 drv->ignore_next_local_disconnect = 0;
1485 if (locally_generated) {
1486 wpa_printf(MSG_DEBUG, "nl80211: Ignore disconnect "
1487 "event triggered during reassociation");
1488 return;
1489 }
1490 wpa_printf(MSG_WARNING, "nl80211: Was expecting local "
1491 "disconnect but got another disconnect "
1492 "event first");
1493 }
1494
1495 wpa_printf(MSG_DEBUG, "nl80211: Disconnect event");
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001496 nl80211_mark_disconnected(drv);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001497 os_memset(&data, 0, sizeof(data));
1498 if (reason)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001499 data.deauth_info.reason_code = nla_get_u16(reason);
1500 data.deauth_info.locally_generated = by_ap == NULL;
1501 wpa_supplicant_event(drv->ctx, EVENT_DEAUTH, &data);
1502}
1503
1504
1505static void mlme_event_ch_switch(struct wpa_driver_nl80211_data *drv,
1506 struct nlattr *freq, struct nlattr *type)
1507{
1508 union wpa_event_data data;
1509 int ht_enabled = 1;
1510 int chan_offset = 0;
1511
1512 wpa_printf(MSG_DEBUG, "nl80211: Channel switch event");
1513
1514 if (!freq || !type)
1515 return;
1516
1517 switch (nla_get_u32(type)) {
1518 case NL80211_CHAN_NO_HT:
1519 ht_enabled = 0;
1520 break;
1521 case NL80211_CHAN_HT20:
1522 break;
1523 case NL80211_CHAN_HT40PLUS:
1524 chan_offset = 1;
1525 break;
1526 case NL80211_CHAN_HT40MINUS:
1527 chan_offset = -1;
1528 break;
1529 }
1530
1531 data.ch_switch.freq = nla_get_u32(freq);
1532 data.ch_switch.ht_enabled = ht_enabled;
1533 data.ch_switch.ch_offset = chan_offset;
1534
1535 wpa_supplicant_event(drv->ctx, EVENT_CH_SWITCH, &data);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001536}
1537
1538
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001539static void mlme_timeout_event(struct wpa_driver_nl80211_data *drv,
1540 enum nl80211_commands cmd, struct nlattr *addr)
1541{
1542 union wpa_event_data event;
1543 enum wpa_event_type ev;
1544
1545 if (nla_len(addr) != ETH_ALEN)
1546 return;
1547
1548 wpa_printf(MSG_DEBUG, "nl80211: MLME event %d; timeout with " MACSTR,
1549 cmd, MAC2STR((u8 *) nla_data(addr)));
1550
1551 if (cmd == NL80211_CMD_AUTHENTICATE)
1552 ev = EVENT_AUTH_TIMED_OUT;
1553 else if (cmd == NL80211_CMD_ASSOCIATE)
1554 ev = EVENT_ASSOC_TIMED_OUT;
1555 else
1556 return;
1557
1558 os_memset(&event, 0, sizeof(event));
1559 os_memcpy(event.timeout_event.addr, nla_data(addr), ETH_ALEN);
1560 wpa_supplicant_event(drv->ctx, ev, &event);
1561}
1562
1563
1564static void mlme_event_mgmt(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001565 struct nlattr *freq, struct nlattr *sig,
1566 const u8 *frame, size_t len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001567{
1568 const struct ieee80211_mgmt *mgmt;
1569 union wpa_event_data event;
1570 u16 fc, stype;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001571 int ssi_signal = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001572 int rx_freq = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001573
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001574 wpa_printf(MSG_MSGDUMP, "nl80211: Frame event");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001575 mgmt = (const struct ieee80211_mgmt *) frame;
1576 if (len < 24) {
1577 wpa_printf(MSG_DEBUG, "nl80211: Too short action frame");
1578 return;
1579 }
1580
1581 fc = le_to_host16(mgmt->frame_control);
1582 stype = WLAN_FC_GET_STYPE(fc);
1583
Dmitry Shmidt04949592012-07-19 12:16:46 -07001584 if (sig)
1585 ssi_signal = (s32) nla_get_u32(sig);
1586
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001587 os_memset(&event, 0, sizeof(event));
1588 if (freq) {
1589 event.rx_action.freq = nla_get_u32(freq);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001590 rx_freq = drv->last_mgmt_freq = event.rx_action.freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001591 }
Dmitry Shmidt56052862013-10-04 10:23:25 -07001592 wpa_printf(MSG_DEBUG,
1593 "nl80211: RX frame freq=%d ssi_signal=%d stype=%u len=%u",
1594 rx_freq, ssi_signal, stype, (unsigned int) len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001595 if (stype == WLAN_FC_STYPE_ACTION) {
1596 event.rx_action.da = mgmt->da;
1597 event.rx_action.sa = mgmt->sa;
1598 event.rx_action.bssid = mgmt->bssid;
1599 event.rx_action.category = mgmt->u.action.category;
1600 event.rx_action.data = &mgmt->u.action.category + 1;
1601 event.rx_action.len = frame + len - event.rx_action.data;
1602 wpa_supplicant_event(drv->ctx, EVENT_RX_ACTION, &event);
1603 } else {
1604 event.rx_mgmt.frame = frame;
1605 event.rx_mgmt.frame_len = len;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001606 event.rx_mgmt.ssi_signal = ssi_signal;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001607 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
1608 }
1609}
1610
1611
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001612static void mlme_event_mgmt_tx_status(struct wpa_driver_nl80211_data *drv,
1613 struct nlattr *cookie, const u8 *frame,
1614 size_t len, struct nlattr *ack)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001615{
1616 union wpa_event_data event;
1617 const struct ieee80211_hdr *hdr;
1618 u16 fc;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001619
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001620 wpa_printf(MSG_DEBUG, "nl80211: Frame TX status event");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001621 if (!is_ap_interface(drv->nlmode)) {
1622 u64 cookie_val;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001623
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001624 if (!cookie)
1625 return;
1626
1627 cookie_val = nla_get_u64(cookie);
1628 wpa_printf(MSG_DEBUG, "nl80211: Action TX status:"
1629 " cookie=0%llx%s (ack=%d)",
1630 (long long unsigned int) cookie_val,
1631 cookie_val == drv->send_action_cookie ?
1632 " (match)" : " (unknown)", ack != NULL);
1633 if (cookie_val != drv->send_action_cookie)
1634 return;
1635 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001636
1637 hdr = (const struct ieee80211_hdr *) frame;
1638 fc = le_to_host16(hdr->frame_control);
1639
1640 os_memset(&event, 0, sizeof(event));
1641 event.tx_status.type = WLAN_FC_GET_TYPE(fc);
1642 event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
1643 event.tx_status.dst = hdr->addr1;
1644 event.tx_status.data = frame;
1645 event.tx_status.data_len = len;
1646 event.tx_status.ack = ack != NULL;
1647 wpa_supplicant_event(drv->ctx, EVENT_TX_STATUS, &event);
1648}
1649
1650
1651static void mlme_event_deauth_disassoc(struct wpa_driver_nl80211_data *drv,
1652 enum wpa_event_type type,
1653 const u8 *frame, size_t len)
1654{
1655 const struct ieee80211_mgmt *mgmt;
1656 union wpa_event_data event;
1657 const u8 *bssid = NULL;
1658 u16 reason_code = 0;
1659
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001660 if (type == EVENT_DEAUTH)
1661 wpa_printf(MSG_DEBUG, "nl80211: Deauthenticate event");
1662 else
1663 wpa_printf(MSG_DEBUG, "nl80211: Disassociate event");
1664
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001665 mgmt = (const struct ieee80211_mgmt *) frame;
1666 if (len >= 24) {
1667 bssid = mgmt->bssid;
1668
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001669 if ((drv->capa.flags & WPA_DRIVER_FLAGS_SME) &&
1670 !drv->associated &&
1671 os_memcmp(bssid, drv->auth_bssid, ETH_ALEN) != 0 &&
1672 os_memcmp(bssid, drv->auth_attempt_bssid, ETH_ALEN) != 0 &&
1673 os_memcmp(bssid, drv->prev_bssid, ETH_ALEN) == 0) {
1674 /*
1675 * Avoid issues with some roaming cases where
1676 * disconnection event for the old AP may show up after
1677 * we have started connection with the new AP.
1678 */
1679 wpa_printf(MSG_DEBUG, "nl80211: Ignore deauth/disassoc event from old AP " MACSTR " when already authenticating with " MACSTR,
1680 MAC2STR(bssid),
1681 MAC2STR(drv->auth_attempt_bssid));
1682 return;
1683 }
1684
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001685 if (drv->associated != 0 &&
1686 os_memcmp(bssid, drv->bssid, ETH_ALEN) != 0 &&
1687 os_memcmp(bssid, drv->auth_bssid, ETH_ALEN) != 0) {
1688 /*
1689 * We have presumably received this deauth as a
1690 * response to a clear_state_mismatch() outgoing
1691 * deauth. Don't let it take us offline!
1692 */
1693 wpa_printf(MSG_DEBUG, "nl80211: Deauth received "
1694 "from Unknown BSSID " MACSTR " -- ignoring",
1695 MAC2STR(bssid));
1696 return;
1697 }
1698 }
1699
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001700 nl80211_mark_disconnected(drv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001701 os_memset(&event, 0, sizeof(event));
1702
1703 /* Note: Same offset for Reason Code in both frame subtypes */
1704 if (len >= 24 + sizeof(mgmt->u.deauth))
1705 reason_code = le_to_host16(mgmt->u.deauth.reason_code);
1706
1707 if (type == EVENT_DISASSOC) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001708 event.disassoc_info.locally_generated =
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001709 !os_memcmp(mgmt->sa, drv->first_bss->addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001710 event.disassoc_info.addr = bssid;
1711 event.disassoc_info.reason_code = reason_code;
1712 if (frame + len > mgmt->u.disassoc.variable) {
1713 event.disassoc_info.ie = mgmt->u.disassoc.variable;
1714 event.disassoc_info.ie_len = frame + len -
1715 mgmt->u.disassoc.variable;
1716 }
1717 } else {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001718 event.deauth_info.locally_generated =
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001719 !os_memcmp(mgmt->sa, drv->first_bss->addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001720 event.deauth_info.addr = bssid;
1721 event.deauth_info.reason_code = reason_code;
1722 if (frame + len > mgmt->u.deauth.variable) {
1723 event.deauth_info.ie = mgmt->u.deauth.variable;
1724 event.deauth_info.ie_len = frame + len -
1725 mgmt->u.deauth.variable;
1726 }
1727 }
1728
1729 wpa_supplicant_event(drv->ctx, type, &event);
1730}
1731
1732
1733static void mlme_event_unprot_disconnect(struct wpa_driver_nl80211_data *drv,
1734 enum wpa_event_type type,
1735 const u8 *frame, size_t len)
1736{
1737 const struct ieee80211_mgmt *mgmt;
1738 union wpa_event_data event;
1739 u16 reason_code = 0;
1740
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001741 if (type == EVENT_UNPROT_DEAUTH)
1742 wpa_printf(MSG_DEBUG, "nl80211: Unprot Deauthenticate event");
1743 else
1744 wpa_printf(MSG_DEBUG, "nl80211: Unprot Disassociate event");
1745
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001746 if (len < 24)
1747 return;
1748
1749 mgmt = (const struct ieee80211_mgmt *) frame;
1750
1751 os_memset(&event, 0, sizeof(event));
1752 /* Note: Same offset for Reason Code in both frame subtypes */
1753 if (len >= 24 + sizeof(mgmt->u.deauth))
1754 reason_code = le_to_host16(mgmt->u.deauth.reason_code);
1755
1756 if (type == EVENT_UNPROT_DISASSOC) {
1757 event.unprot_disassoc.sa = mgmt->sa;
1758 event.unprot_disassoc.da = mgmt->da;
1759 event.unprot_disassoc.reason_code = reason_code;
1760 } else {
1761 event.unprot_deauth.sa = mgmt->sa;
1762 event.unprot_deauth.da = mgmt->da;
1763 event.unprot_deauth.reason_code = reason_code;
1764 }
1765
1766 wpa_supplicant_event(drv->ctx, type, &event);
1767}
1768
1769
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001770static void mlme_event(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001771 enum nl80211_commands cmd, struct nlattr *frame,
1772 struct nlattr *addr, struct nlattr *timed_out,
1773 struct nlattr *freq, struct nlattr *ack,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001774 struct nlattr *cookie, struct nlattr *sig)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001775{
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001776 struct wpa_driver_nl80211_data *drv = bss->drv;
1777 const u8 *data;
1778 size_t len;
1779
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001780 if (timed_out && addr) {
1781 mlme_timeout_event(drv, cmd, addr);
1782 return;
1783 }
1784
1785 if (frame == NULL) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001786 wpa_printf(MSG_DEBUG,
1787 "nl80211: MLME event %d (%s) without frame data",
1788 cmd, nl80211_command_to_string(cmd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001789 return;
1790 }
1791
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001792 data = nla_data(frame);
1793 len = nla_len(frame);
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001794 if (len < 4 + 2 * ETH_ALEN) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001795 wpa_printf(MSG_MSGDUMP, "nl80211: MLME event %d (%s) on %s("
1796 MACSTR ") - too short",
1797 cmd, nl80211_command_to_string(cmd), bss->ifname,
1798 MAC2STR(bss->addr));
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001799 return;
1800 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001801 wpa_printf(MSG_MSGDUMP, "nl80211: MLME event %d (%s) on %s(" MACSTR
1802 ") A1=" MACSTR " A2=" MACSTR, cmd,
1803 nl80211_command_to_string(cmd), bss->ifname,
1804 MAC2STR(bss->addr), MAC2STR(data + 4),
1805 MAC2STR(data + 4 + ETH_ALEN));
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001806 if (cmd != NL80211_CMD_FRAME_TX_STATUS && !(data[4] & 0x01) &&
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001807 os_memcmp(bss->addr, data + 4, ETH_ALEN) != 0 &&
1808 os_memcmp(bss->addr, data + 4 + ETH_ALEN, ETH_ALEN) != 0) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001809 wpa_printf(MSG_MSGDUMP, "nl80211: %s: Ignore MLME frame event "
1810 "for foreign address", bss->ifname);
1811 return;
1812 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001813 wpa_hexdump(MSG_MSGDUMP, "nl80211: MLME event frame",
1814 nla_data(frame), nla_len(frame));
1815
1816 switch (cmd) {
1817 case NL80211_CMD_AUTHENTICATE:
1818 mlme_event_auth(drv, nla_data(frame), nla_len(frame));
1819 break;
1820 case NL80211_CMD_ASSOCIATE:
1821 mlme_event_assoc(drv, nla_data(frame), nla_len(frame));
1822 break;
1823 case NL80211_CMD_DEAUTHENTICATE:
1824 mlme_event_deauth_disassoc(drv, EVENT_DEAUTH,
1825 nla_data(frame), nla_len(frame));
1826 break;
1827 case NL80211_CMD_DISASSOCIATE:
1828 mlme_event_deauth_disassoc(drv, EVENT_DISASSOC,
1829 nla_data(frame), nla_len(frame));
1830 break;
1831 case NL80211_CMD_FRAME:
Dmitry Shmidt04949592012-07-19 12:16:46 -07001832 mlme_event_mgmt(drv, freq, sig, nla_data(frame),
1833 nla_len(frame));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001834 break;
1835 case NL80211_CMD_FRAME_TX_STATUS:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001836 mlme_event_mgmt_tx_status(drv, cookie, nla_data(frame),
1837 nla_len(frame), ack);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001838 break;
1839 case NL80211_CMD_UNPROT_DEAUTHENTICATE:
1840 mlme_event_unprot_disconnect(drv, EVENT_UNPROT_DEAUTH,
1841 nla_data(frame), nla_len(frame));
1842 break;
1843 case NL80211_CMD_UNPROT_DISASSOCIATE:
1844 mlme_event_unprot_disconnect(drv, EVENT_UNPROT_DISASSOC,
1845 nla_data(frame), nla_len(frame));
1846 break;
1847 default:
1848 break;
1849 }
1850}
1851
1852
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001853static void mlme_event_michael_mic_failure(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001854 struct nlattr *tb[])
1855{
1856 union wpa_event_data data;
1857
1858 wpa_printf(MSG_DEBUG, "nl80211: MLME event Michael MIC failure");
1859 os_memset(&data, 0, sizeof(data));
1860 if (tb[NL80211_ATTR_MAC]) {
1861 wpa_hexdump(MSG_DEBUG, "nl80211: Source MAC address",
1862 nla_data(tb[NL80211_ATTR_MAC]),
1863 nla_len(tb[NL80211_ATTR_MAC]));
1864 data.michael_mic_failure.src = nla_data(tb[NL80211_ATTR_MAC]);
1865 }
1866 if (tb[NL80211_ATTR_KEY_SEQ]) {
1867 wpa_hexdump(MSG_DEBUG, "nl80211: TSC",
1868 nla_data(tb[NL80211_ATTR_KEY_SEQ]),
1869 nla_len(tb[NL80211_ATTR_KEY_SEQ]));
1870 }
1871 if (tb[NL80211_ATTR_KEY_TYPE]) {
1872 enum nl80211_key_type key_type =
1873 nla_get_u32(tb[NL80211_ATTR_KEY_TYPE]);
1874 wpa_printf(MSG_DEBUG, "nl80211: Key Type %d", key_type);
1875 if (key_type == NL80211_KEYTYPE_PAIRWISE)
1876 data.michael_mic_failure.unicast = 1;
1877 } else
1878 data.michael_mic_failure.unicast = 1;
1879
1880 if (tb[NL80211_ATTR_KEY_IDX]) {
1881 u8 key_id = nla_get_u8(tb[NL80211_ATTR_KEY_IDX]);
1882 wpa_printf(MSG_DEBUG, "nl80211: Key Id %d", key_id);
1883 }
1884
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001885 wpa_supplicant_event(bss->ctx, EVENT_MICHAEL_MIC_FAILURE, &data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001886}
1887
1888
1889static void mlme_event_join_ibss(struct wpa_driver_nl80211_data *drv,
1890 struct nlattr *tb[])
1891{
1892 if (tb[NL80211_ATTR_MAC] == NULL) {
1893 wpa_printf(MSG_DEBUG, "nl80211: No address in IBSS joined "
1894 "event");
1895 return;
1896 }
1897 os_memcpy(drv->bssid, nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07001898
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001899 drv->associated = 1;
1900 wpa_printf(MSG_DEBUG, "nl80211: IBSS " MACSTR " joined",
1901 MAC2STR(drv->bssid));
1902
1903 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, NULL);
1904}
1905
1906
1907static void mlme_event_remain_on_channel(struct wpa_driver_nl80211_data *drv,
1908 int cancel_event, struct nlattr *tb[])
1909{
1910 unsigned int freq, chan_type, duration;
1911 union wpa_event_data data;
1912 u64 cookie;
1913
1914 if (tb[NL80211_ATTR_WIPHY_FREQ])
1915 freq = nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]);
1916 else
1917 freq = 0;
1918
1919 if (tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE])
1920 chan_type = nla_get_u32(tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1921 else
1922 chan_type = 0;
1923
1924 if (tb[NL80211_ATTR_DURATION])
1925 duration = nla_get_u32(tb[NL80211_ATTR_DURATION]);
1926 else
1927 duration = 0;
1928
1929 if (tb[NL80211_ATTR_COOKIE])
1930 cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
1931 else
1932 cookie = 0;
1933
1934 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel event (cancel=%d "
1935 "freq=%u channel_type=%u duration=%u cookie=0x%llx (%s))",
1936 cancel_event, freq, chan_type, duration,
1937 (long long unsigned int) cookie,
1938 cookie == drv->remain_on_chan_cookie ? "match" : "unknown");
1939
1940 if (cookie != drv->remain_on_chan_cookie)
1941 return; /* not for us */
1942
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001943 if (cancel_event)
1944 drv->pending_remain_on_chan = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001945
1946 os_memset(&data, 0, sizeof(data));
1947 data.remain_on_channel.freq = freq;
1948 data.remain_on_channel.duration = duration;
1949 wpa_supplicant_event(drv->ctx, cancel_event ?
1950 EVENT_CANCEL_REMAIN_ON_CHANNEL :
1951 EVENT_REMAIN_ON_CHANNEL, &data);
1952}
1953
1954
Dmitry Shmidt700a1372013-03-15 14:14:44 -07001955static void mlme_event_ft_event(struct wpa_driver_nl80211_data *drv,
1956 struct nlattr *tb[])
1957{
1958 union wpa_event_data data;
1959
1960 os_memset(&data, 0, sizeof(data));
1961
1962 if (tb[NL80211_ATTR_IE]) {
1963 data.ft_ies.ies = nla_data(tb[NL80211_ATTR_IE]);
1964 data.ft_ies.ies_len = nla_len(tb[NL80211_ATTR_IE]);
1965 }
1966
1967 if (tb[NL80211_ATTR_IE_RIC]) {
1968 data.ft_ies.ric_ies = nla_data(tb[NL80211_ATTR_IE_RIC]);
1969 data.ft_ies.ric_ies_len = nla_len(tb[NL80211_ATTR_IE_RIC]);
1970 }
1971
1972 if (tb[NL80211_ATTR_MAC])
1973 os_memcpy(data.ft_ies.target_ap,
1974 nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
1975
1976 wpa_printf(MSG_DEBUG, "nl80211: FT event target_ap " MACSTR,
1977 MAC2STR(data.ft_ies.target_ap));
1978
1979 wpa_supplicant_event(drv->ctx, EVENT_FT_RESPONSE, &data);
1980}
1981
1982
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001983static void send_scan_event(struct wpa_driver_nl80211_data *drv, int aborted,
1984 struct nlattr *tb[])
1985{
1986 union wpa_event_data event;
1987 struct nlattr *nl;
1988 int rem;
1989 struct scan_info *info;
1990#define MAX_REPORT_FREQS 50
1991 int freqs[MAX_REPORT_FREQS];
1992 int num_freqs = 0;
1993
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001994 if (drv->scan_for_auth) {
1995 drv->scan_for_auth = 0;
1996 wpa_printf(MSG_DEBUG, "nl80211: Scan results for missing "
1997 "cfg80211 BSS entry");
1998 wpa_driver_nl80211_authenticate_retry(drv);
1999 return;
2000 }
2001
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002002 os_memset(&event, 0, sizeof(event));
2003 info = &event.scan_info;
2004 info->aborted = aborted;
2005
2006 if (tb[NL80211_ATTR_SCAN_SSIDS]) {
2007 nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_SSIDS], rem) {
2008 struct wpa_driver_scan_ssid *s =
2009 &info->ssids[info->num_ssids];
2010 s->ssid = nla_data(nl);
2011 s->ssid_len = nla_len(nl);
2012 info->num_ssids++;
2013 if (info->num_ssids == WPAS_MAX_SCAN_SSIDS)
2014 break;
2015 }
2016 }
2017 if (tb[NL80211_ATTR_SCAN_FREQUENCIES]) {
2018 nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_FREQUENCIES], rem)
2019 {
2020 freqs[num_freqs] = nla_get_u32(nl);
2021 num_freqs++;
2022 if (num_freqs == MAX_REPORT_FREQS - 1)
2023 break;
2024 }
2025 info->freqs = freqs;
2026 info->num_freqs = num_freqs;
2027 }
2028 wpa_supplicant_event(drv->ctx, EVENT_SCAN_RESULTS, &event);
2029}
2030
2031
2032static int get_link_signal(struct nl_msg *msg, void *arg)
2033{
2034 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2035 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2036 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
2037 static struct nla_policy policy[NL80211_STA_INFO_MAX + 1] = {
2038 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002039 [NL80211_STA_INFO_SIGNAL_AVG] = { .type = NLA_U8 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002040 };
2041 struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
2042 static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
2043 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
2044 [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
2045 [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
2046 [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
2047 };
2048 struct wpa_signal_info *sig_change = arg;
2049
2050 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2051 genlmsg_attrlen(gnlh, 0), NULL);
2052 if (!tb[NL80211_ATTR_STA_INFO] ||
2053 nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
2054 tb[NL80211_ATTR_STA_INFO], policy))
2055 return NL_SKIP;
2056 if (!sinfo[NL80211_STA_INFO_SIGNAL])
2057 return NL_SKIP;
2058
2059 sig_change->current_signal =
2060 (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
2061
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002062 if (sinfo[NL80211_STA_INFO_SIGNAL_AVG])
2063 sig_change->avg_signal =
2064 (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL_AVG]);
2065 else
2066 sig_change->avg_signal = 0;
2067
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002068 if (sinfo[NL80211_STA_INFO_TX_BITRATE]) {
2069 if (nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
2070 sinfo[NL80211_STA_INFO_TX_BITRATE],
2071 rate_policy)) {
2072 sig_change->current_txrate = 0;
2073 } else {
2074 if (rinfo[NL80211_RATE_INFO_BITRATE]) {
2075 sig_change->current_txrate =
2076 nla_get_u16(rinfo[
2077 NL80211_RATE_INFO_BITRATE]) * 100;
2078 }
2079 }
2080 }
2081
2082 return NL_SKIP;
2083}
2084
2085
2086static int nl80211_get_link_signal(struct wpa_driver_nl80211_data *drv,
2087 struct wpa_signal_info *sig)
2088{
2089 struct nl_msg *msg;
2090
2091 sig->current_signal = -9999;
2092 sig->current_txrate = 0;
2093
2094 msg = nlmsg_alloc();
2095 if (!msg)
2096 return -ENOMEM;
2097
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002098 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002099
2100 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
2101 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid);
2102
2103 return send_and_recv_msgs(drv, msg, get_link_signal, sig);
2104 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002105 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002106 return -ENOBUFS;
2107}
2108
2109
2110static int get_link_noise(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_SURVEY_INFO_MAX + 1];
2115 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
2116 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
2117 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
2118 };
2119 struct wpa_signal_info *sig_change = arg;
2120
2121 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2122 genlmsg_attrlen(gnlh, 0), NULL);
2123
2124 if (!tb[NL80211_ATTR_SURVEY_INFO]) {
2125 wpa_printf(MSG_DEBUG, "nl80211: survey data missing!");
2126 return NL_SKIP;
2127 }
2128
2129 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
2130 tb[NL80211_ATTR_SURVEY_INFO],
2131 survey_policy)) {
2132 wpa_printf(MSG_DEBUG, "nl80211: failed to parse nested "
2133 "attributes!");
2134 return NL_SKIP;
2135 }
2136
2137 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
2138 return NL_SKIP;
2139
2140 if (nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
2141 sig_change->frequency)
2142 return NL_SKIP;
2143
2144 if (!sinfo[NL80211_SURVEY_INFO_NOISE])
2145 return NL_SKIP;
2146
2147 sig_change->current_noise =
2148 (s8) nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
2149
2150 return NL_SKIP;
2151}
2152
2153
2154static int nl80211_get_link_noise(struct wpa_driver_nl80211_data *drv,
2155 struct wpa_signal_info *sig_change)
2156{
2157 struct nl_msg *msg;
2158
2159 sig_change->current_noise = 9999;
2160 sig_change->frequency = drv->assoc_freq;
2161
2162 msg = nlmsg_alloc();
2163 if (!msg)
2164 return -ENOMEM;
2165
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002166 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002167
2168 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
2169
2170 return send_and_recv_msgs(drv, msg, get_link_noise, sig_change);
2171 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002172 nlmsg_free(msg);
2173 return -ENOBUFS;
2174}
2175
2176
2177static int get_noise_for_scan_results(struct nl_msg *msg, void *arg)
2178{
2179 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2180 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2181 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
2182 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
2183 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
2184 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
2185 };
2186 struct wpa_scan_results *scan_results = arg;
2187 struct wpa_scan_res *scan_res;
2188 size_t i;
2189
2190 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2191 genlmsg_attrlen(gnlh, 0), NULL);
2192
2193 if (!tb[NL80211_ATTR_SURVEY_INFO]) {
2194 wpa_printf(MSG_DEBUG, "nl80211: Survey data missing");
2195 return NL_SKIP;
2196 }
2197
2198 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
2199 tb[NL80211_ATTR_SURVEY_INFO],
2200 survey_policy)) {
2201 wpa_printf(MSG_DEBUG, "nl80211: Failed to parse nested "
2202 "attributes");
2203 return NL_SKIP;
2204 }
2205
2206 if (!sinfo[NL80211_SURVEY_INFO_NOISE])
2207 return NL_SKIP;
2208
2209 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
2210 return NL_SKIP;
2211
2212 for (i = 0; i < scan_results->num; ++i) {
2213 scan_res = scan_results->res[i];
2214 if (!scan_res)
2215 continue;
2216 if ((int) nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
2217 scan_res->freq)
2218 continue;
2219 if (!(scan_res->flags & WPA_SCAN_NOISE_INVALID))
2220 continue;
2221 scan_res->noise = (s8)
2222 nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
2223 scan_res->flags &= ~WPA_SCAN_NOISE_INVALID;
2224 }
2225
2226 return NL_SKIP;
2227}
2228
2229
2230static int nl80211_get_noise_for_scan_results(
2231 struct wpa_driver_nl80211_data *drv,
2232 struct wpa_scan_results *scan_res)
2233{
2234 struct nl_msg *msg;
2235
2236 msg = nlmsg_alloc();
2237 if (!msg)
2238 return -ENOMEM;
2239
2240 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
2241
2242 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
2243
2244 return send_and_recv_msgs(drv, msg, get_noise_for_scan_results,
2245 scan_res);
2246 nla_put_failure:
2247 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002248 return -ENOBUFS;
2249}
2250
2251
2252static void nl80211_cqm_event(struct wpa_driver_nl80211_data *drv,
2253 struct nlattr *tb[])
2254{
2255 static struct nla_policy cqm_policy[NL80211_ATTR_CQM_MAX + 1] = {
2256 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
2257 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U8 },
2258 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
2259 [NL80211_ATTR_CQM_PKT_LOSS_EVENT] = { .type = NLA_U32 },
2260 };
2261 struct nlattr *cqm[NL80211_ATTR_CQM_MAX + 1];
2262 enum nl80211_cqm_rssi_threshold_event event;
2263 union wpa_event_data ed;
2264 struct wpa_signal_info sig;
2265 int res;
2266
2267 if (tb[NL80211_ATTR_CQM] == NULL ||
2268 nla_parse_nested(cqm, NL80211_ATTR_CQM_MAX, tb[NL80211_ATTR_CQM],
2269 cqm_policy)) {
2270 wpa_printf(MSG_DEBUG, "nl80211: Ignore invalid CQM event");
2271 return;
2272 }
2273
2274 os_memset(&ed, 0, sizeof(ed));
2275
2276 if (cqm[NL80211_ATTR_CQM_PKT_LOSS_EVENT]) {
2277 if (!tb[NL80211_ATTR_MAC])
2278 return;
2279 os_memcpy(ed.low_ack.addr, nla_data(tb[NL80211_ATTR_MAC]),
2280 ETH_ALEN);
2281 wpa_supplicant_event(drv->ctx, EVENT_STATION_LOW_ACK, &ed);
2282 return;
2283 }
2284
2285 if (cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] == NULL)
2286 return;
2287 event = nla_get_u32(cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT]);
2288
2289 if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH) {
2290 wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor "
2291 "event: RSSI high");
2292 ed.signal_change.above_threshold = 1;
2293 } else if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW) {
2294 wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor "
2295 "event: RSSI low");
2296 ed.signal_change.above_threshold = 0;
2297 } else
2298 return;
2299
2300 res = nl80211_get_link_signal(drv, &sig);
2301 if (res == 0) {
2302 ed.signal_change.current_signal = sig.current_signal;
2303 ed.signal_change.current_txrate = sig.current_txrate;
2304 wpa_printf(MSG_DEBUG, "nl80211: Signal: %d dBm txrate: %d",
2305 sig.current_signal, sig.current_txrate);
2306 }
2307
2308 res = nl80211_get_link_noise(drv, &sig);
2309 if (res == 0) {
2310 ed.signal_change.current_noise = sig.current_noise;
2311 wpa_printf(MSG_DEBUG, "nl80211: Noise: %d dBm",
2312 sig.current_noise);
2313 }
2314
2315 wpa_supplicant_event(drv->ctx, EVENT_SIGNAL_CHANGE, &ed);
2316}
2317
2318
2319static void nl80211_new_station_event(struct wpa_driver_nl80211_data *drv,
2320 struct nlattr **tb)
2321{
2322 u8 *addr;
2323 union wpa_event_data data;
2324
2325 if (tb[NL80211_ATTR_MAC] == NULL)
2326 return;
2327 addr = nla_data(tb[NL80211_ATTR_MAC]);
2328 wpa_printf(MSG_DEBUG, "nl80211: New station " MACSTR, MAC2STR(addr));
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07002329
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002330 if (is_ap_interface(drv->nlmode) && drv->device_ap_sme) {
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07002331 u8 *ies = NULL;
2332 size_t ies_len = 0;
2333 if (tb[NL80211_ATTR_IE]) {
2334 ies = nla_data(tb[NL80211_ATTR_IE]);
2335 ies_len = nla_len(tb[NL80211_ATTR_IE]);
2336 }
2337 wpa_hexdump(MSG_DEBUG, "nl80211: Assoc Req IEs", ies, ies_len);
2338 drv_event_assoc(drv->ctx, addr, ies, ies_len, 0);
2339 return;
2340 }
2341
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002342 if (drv->nlmode != NL80211_IFTYPE_ADHOC)
2343 return;
2344
2345 os_memset(&data, 0, sizeof(data));
2346 os_memcpy(data.ibss_rsn_start.peer, addr, ETH_ALEN);
2347 wpa_supplicant_event(drv->ctx, EVENT_IBSS_RSN_START, &data);
2348}
2349
2350
2351static void nl80211_del_station_event(struct wpa_driver_nl80211_data *drv,
2352 struct nlattr **tb)
2353{
2354 u8 *addr;
2355 union wpa_event_data data;
2356
2357 if (tb[NL80211_ATTR_MAC] == NULL)
2358 return;
2359 addr = nla_data(tb[NL80211_ATTR_MAC]);
2360 wpa_printf(MSG_DEBUG, "nl80211: Delete station " MACSTR,
2361 MAC2STR(addr));
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07002362
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002363 if (is_ap_interface(drv->nlmode) && drv->device_ap_sme) {
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07002364 drv_event_disassoc(drv->ctx, addr);
2365 return;
2366 }
2367
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002368 if (drv->nlmode != NL80211_IFTYPE_ADHOC)
2369 return;
2370
2371 os_memset(&data, 0, sizeof(data));
2372 os_memcpy(data.ibss_peer_lost.peer, addr, ETH_ALEN);
2373 wpa_supplicant_event(drv->ctx, EVENT_IBSS_PEER_LOST, &data);
2374}
2375
2376
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002377static void nl80211_rekey_offload_event(struct wpa_driver_nl80211_data *drv,
2378 struct nlattr **tb)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002379{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002380 struct nlattr *rekey_info[NUM_NL80211_REKEY_DATA];
2381 static struct nla_policy rekey_policy[NUM_NL80211_REKEY_DATA] = {
2382 [NL80211_REKEY_DATA_KEK] = {
2383 .minlen = NL80211_KEK_LEN,
2384 .maxlen = NL80211_KEK_LEN,
2385 },
2386 [NL80211_REKEY_DATA_KCK] = {
2387 .minlen = NL80211_KCK_LEN,
2388 .maxlen = NL80211_KCK_LEN,
2389 },
2390 [NL80211_REKEY_DATA_REPLAY_CTR] = {
2391 .minlen = NL80211_REPLAY_CTR_LEN,
2392 .maxlen = NL80211_REPLAY_CTR_LEN,
2393 },
2394 };
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002395 union wpa_event_data data;
2396
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002397 if (!tb[NL80211_ATTR_MAC])
2398 return;
2399 if (!tb[NL80211_ATTR_REKEY_DATA])
2400 return;
2401 if (nla_parse_nested(rekey_info, MAX_NL80211_REKEY_DATA,
2402 tb[NL80211_ATTR_REKEY_DATA], rekey_policy))
2403 return;
2404 if (!rekey_info[NL80211_REKEY_DATA_REPLAY_CTR])
2405 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002406
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002407 os_memset(&data, 0, sizeof(data));
2408 data.driver_gtk_rekey.bssid = nla_data(tb[NL80211_ATTR_MAC]);
2409 wpa_printf(MSG_DEBUG, "nl80211: Rekey offload event for BSSID " MACSTR,
2410 MAC2STR(data.driver_gtk_rekey.bssid));
2411 data.driver_gtk_rekey.replay_ctr =
2412 nla_data(rekey_info[NL80211_REKEY_DATA_REPLAY_CTR]);
2413 wpa_hexdump(MSG_DEBUG, "nl80211: Rekey offload - Replay Counter",
2414 data.driver_gtk_rekey.replay_ctr, NL80211_REPLAY_CTR_LEN);
2415 wpa_supplicant_event(drv->ctx, EVENT_DRIVER_GTK_REKEY, &data);
2416}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002417
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002418
2419static void nl80211_pmksa_candidate_event(struct wpa_driver_nl80211_data *drv,
2420 struct nlattr **tb)
2421{
2422 struct nlattr *cand[NUM_NL80211_PMKSA_CANDIDATE];
2423 static struct nla_policy cand_policy[NUM_NL80211_PMKSA_CANDIDATE] = {
2424 [NL80211_PMKSA_CANDIDATE_INDEX] = { .type = NLA_U32 },
2425 [NL80211_PMKSA_CANDIDATE_BSSID] = {
2426 .minlen = ETH_ALEN,
2427 .maxlen = ETH_ALEN,
2428 },
2429 [NL80211_PMKSA_CANDIDATE_PREAUTH] = { .type = NLA_FLAG },
2430 };
2431 union wpa_event_data data;
2432
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002433 wpa_printf(MSG_DEBUG, "nl80211: PMKSA candidate event");
2434
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002435 if (!tb[NL80211_ATTR_PMKSA_CANDIDATE])
2436 return;
2437 if (nla_parse_nested(cand, MAX_NL80211_PMKSA_CANDIDATE,
2438 tb[NL80211_ATTR_PMKSA_CANDIDATE], cand_policy))
2439 return;
2440 if (!cand[NL80211_PMKSA_CANDIDATE_INDEX] ||
2441 !cand[NL80211_PMKSA_CANDIDATE_BSSID])
2442 return;
2443
2444 os_memset(&data, 0, sizeof(data));
2445 os_memcpy(data.pmkid_candidate.bssid,
2446 nla_data(cand[NL80211_PMKSA_CANDIDATE_BSSID]), ETH_ALEN);
2447 data.pmkid_candidate.index =
2448 nla_get_u32(cand[NL80211_PMKSA_CANDIDATE_INDEX]);
2449 data.pmkid_candidate.preauth =
2450 cand[NL80211_PMKSA_CANDIDATE_PREAUTH] != NULL;
2451 wpa_supplicant_event(drv->ctx, EVENT_PMKID_CANDIDATE, &data);
2452}
2453
2454
2455static void nl80211_client_probe_event(struct wpa_driver_nl80211_data *drv,
2456 struct nlattr **tb)
2457{
2458 union wpa_event_data data;
2459
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002460 wpa_printf(MSG_DEBUG, "nl80211: Probe client event");
2461
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002462 if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_ACK])
2463 return;
2464
2465 os_memset(&data, 0, sizeof(data));
2466 os_memcpy(data.client_poll.addr,
2467 nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
2468
2469 wpa_supplicant_event(drv->ctx, EVENT_DRIVER_CLIENT_POLL_OK, &data);
2470}
2471
2472
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002473static void nl80211_tdls_oper_event(struct wpa_driver_nl80211_data *drv,
2474 struct nlattr **tb)
2475{
2476 union wpa_event_data data;
2477
2478 wpa_printf(MSG_DEBUG, "nl80211: TDLS operation event");
2479
2480 if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_TDLS_OPERATION])
2481 return;
2482
2483 os_memset(&data, 0, sizeof(data));
2484 os_memcpy(data.tdls.peer, nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
2485 switch (nla_get_u8(tb[NL80211_ATTR_TDLS_OPERATION])) {
2486 case NL80211_TDLS_SETUP:
2487 wpa_printf(MSG_DEBUG, "nl80211: TDLS setup request for peer "
2488 MACSTR, MAC2STR(data.tdls.peer));
2489 data.tdls.oper = TDLS_REQUEST_SETUP;
2490 break;
2491 case NL80211_TDLS_TEARDOWN:
2492 wpa_printf(MSG_DEBUG, "nl80211: TDLS teardown request for peer "
2493 MACSTR, MAC2STR(data.tdls.peer));
2494 data.tdls.oper = TDLS_REQUEST_TEARDOWN;
2495 break;
2496 default:
2497 wpa_printf(MSG_DEBUG, "nl80211: Unsupported TDLS operatione "
2498 "event");
2499 return;
2500 }
2501 if (tb[NL80211_ATTR_REASON_CODE]) {
2502 data.tdls.reason_code =
2503 nla_get_u16(tb[NL80211_ATTR_REASON_CODE]);
2504 }
2505
2506 wpa_supplicant_event(drv->ctx, EVENT_TDLS, &data);
2507}
2508
2509
Dmitry Shmidt5393a0f2013-08-08 11:23:34 -07002510static void nl80211_stop_ap(struct wpa_driver_nl80211_data *drv,
2511 struct nlattr **tb)
2512{
2513 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_UNAVAILABLE, NULL);
2514}
2515
2516
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002517static void nl80211_connect_failed_event(struct wpa_driver_nl80211_data *drv,
2518 struct nlattr **tb)
2519{
2520 union wpa_event_data data;
2521 u32 reason;
2522
2523 wpa_printf(MSG_DEBUG, "nl80211: Connect failed event");
2524
2525 if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_CONN_FAILED_REASON])
2526 return;
2527
2528 os_memset(&data, 0, sizeof(data));
2529 os_memcpy(data.connect_failed_reason.addr,
2530 nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
2531
2532 reason = nla_get_u32(tb[NL80211_ATTR_CONN_FAILED_REASON]);
2533 switch (reason) {
2534 case NL80211_CONN_FAIL_MAX_CLIENTS:
2535 wpa_printf(MSG_DEBUG, "nl80211: Max client reached");
2536 data.connect_failed_reason.code = MAX_CLIENT_REACHED;
2537 break;
2538 case NL80211_CONN_FAIL_BLOCKED_CLIENT:
2539 wpa_printf(MSG_DEBUG, "nl80211: Blocked client " MACSTR
2540 " tried to connect",
2541 MAC2STR(data.connect_failed_reason.addr));
2542 data.connect_failed_reason.code = BLOCKED_CLIENT;
2543 break;
2544 default:
2545 wpa_printf(MSG_DEBUG, "nl8021l: Unknown connect failed reason "
2546 "%u", reason);
2547 return;
2548 }
2549
2550 wpa_supplicant_event(drv->ctx, EVENT_CONNECT_FAILED_REASON, &data);
2551}
2552
2553
Dmitry Shmidt051af732013-10-22 13:52:46 -07002554static enum chan_width convert2width(int width);
2555
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002556static void nl80211_radar_event(struct wpa_driver_nl80211_data *drv,
2557 struct nlattr **tb)
2558{
2559 union wpa_event_data data;
2560 enum nl80211_radar_event event_type;
2561
2562 if (!tb[NL80211_ATTR_WIPHY_FREQ] || !tb[NL80211_ATTR_RADAR_EVENT])
2563 return;
2564
2565 os_memset(&data, 0, sizeof(data));
Dmitry Shmidt051af732013-10-22 13:52:46 -07002566 data.dfs_event.freq = nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]);
2567 event_type = nla_get_u32(tb[NL80211_ATTR_RADAR_EVENT]);
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002568
Dmitry Shmidt051af732013-10-22 13:52:46 -07002569 /* Check HT params */
2570 if (tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
2571 data.dfs_event.ht_enabled = 1;
2572 data.dfs_event.chan_offset = 0;
2573
2574 switch (nla_get_u32(tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE])) {
2575 case NL80211_CHAN_NO_HT:
2576 data.dfs_event.ht_enabled = 0;
2577 break;
2578 case NL80211_CHAN_HT20:
2579 break;
2580 case NL80211_CHAN_HT40PLUS:
2581 data.dfs_event.chan_offset = 1;
2582 break;
2583 case NL80211_CHAN_HT40MINUS:
2584 data.dfs_event.chan_offset = -1;
2585 break;
2586 }
2587 }
2588
2589 /* Get VHT params */
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002590 if (tb[NL80211_ATTR_CHANNEL_WIDTH])
2591 data.dfs_event.chan_width =
2592 convert2width(nla_get_u32(
2593 tb[NL80211_ATTR_CHANNEL_WIDTH]));
2594 if (tb[NL80211_ATTR_CENTER_FREQ1])
2595 data.dfs_event.cf1 = nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002596 if (tb[NL80211_ATTR_CENTER_FREQ2])
2597 data.dfs_event.cf2 = nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]);
2598
2599 wpa_printf(MSG_DEBUG, "nl80211: DFS event on freq %d MHz, ht: %d, offset: %d, width: %d, cf1: %dMHz, cf2: %dMHz",
2600 data.dfs_event.freq, data.dfs_event.ht_enabled,
2601 data.dfs_event.chan_offset, data.dfs_event.chan_width,
2602 data.dfs_event.cf1, data.dfs_event.cf2);
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002603
2604 switch (event_type) {
2605 case NL80211_RADAR_DETECTED:
2606 wpa_supplicant_event(drv->ctx, EVENT_DFS_RADAR_DETECTED, &data);
2607 break;
2608 case NL80211_RADAR_CAC_FINISHED:
2609 wpa_supplicant_event(drv->ctx, EVENT_DFS_CAC_FINISHED, &data);
2610 break;
2611 case NL80211_RADAR_CAC_ABORTED:
2612 wpa_supplicant_event(drv->ctx, EVENT_DFS_CAC_ABORTED, &data);
2613 break;
2614 case NL80211_RADAR_NOP_FINISHED:
2615 wpa_supplicant_event(drv->ctx, EVENT_DFS_NOP_FINISHED, &data);
2616 break;
2617 default:
2618 wpa_printf(MSG_DEBUG, "nl80211: Unknown radar event %d "
2619 "received", event_type);
2620 break;
2621 }
2622}
2623
2624
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002625static void nl80211_spurious_frame(struct i802_bss *bss, struct nlattr **tb,
2626 int wds)
2627{
2628 struct wpa_driver_nl80211_data *drv = bss->drv;
2629 union wpa_event_data event;
2630
2631 if (!tb[NL80211_ATTR_MAC])
2632 return;
2633
2634 os_memset(&event, 0, sizeof(event));
2635 event.rx_from_unknown.bssid = bss->addr;
2636 event.rx_from_unknown.addr = nla_data(tb[NL80211_ATTR_MAC]);
2637 event.rx_from_unknown.wds = wds;
2638
2639 wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event);
2640}
2641
2642
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002643static void do_process_drv_event(struct i802_bss *bss, int cmd,
2644 struct nlattr **tb)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002645{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002646 struct wpa_driver_nl80211_data *drv = bss->drv;
2647
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002648 wpa_printf(MSG_DEBUG, "nl80211: Drv Event %d (%s) received for %s",
2649 cmd, nl80211_command_to_string(cmd), bss->ifname);
2650
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002651 if (drv->ap_scan_as_station != NL80211_IFTYPE_UNSPECIFIED &&
2652 (cmd == NL80211_CMD_NEW_SCAN_RESULTS ||
2653 cmd == NL80211_CMD_SCAN_ABORTED)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002654 wpa_driver_nl80211_set_mode(drv->first_bss,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002655 drv->ap_scan_as_station);
2656 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002657 }
2658
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002659 switch (cmd) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002660 case NL80211_CMD_TRIGGER_SCAN:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002661 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Scan trigger");
Dmitry Shmidt56052862013-10-04 10:23:25 -07002662 drv->scan_state = SCAN_STARTED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002663 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002664 case NL80211_CMD_START_SCHED_SCAN:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002665 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Sched scan started");
Dmitry Shmidt56052862013-10-04 10:23:25 -07002666 drv->scan_state = SCHED_SCAN_STARTED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002667 break;
2668 case NL80211_CMD_SCHED_SCAN_STOPPED:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002669 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Sched scan stopped");
Dmitry Shmidt56052862013-10-04 10:23:25 -07002670 drv->scan_state = SCHED_SCAN_STOPPED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002671 wpa_supplicant_event(drv->ctx, EVENT_SCHED_SCAN_STOPPED, NULL);
2672 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002673 case NL80211_CMD_NEW_SCAN_RESULTS:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002674 wpa_dbg(drv->ctx, MSG_DEBUG,
2675 "nl80211: New scan results available");
Dmitry Shmidt56052862013-10-04 10:23:25 -07002676 drv->scan_state = SCAN_COMPLETED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002677 drv->scan_complete_events = 1;
2678 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
2679 drv->ctx);
2680 send_scan_event(drv, 0, tb);
2681 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002682 case NL80211_CMD_SCHED_SCAN_RESULTS:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002683 wpa_dbg(drv->ctx, MSG_DEBUG,
2684 "nl80211: New sched scan results available");
Dmitry Shmidt56052862013-10-04 10:23:25 -07002685 drv->scan_state = SCHED_SCAN_RESULTS;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002686 send_scan_event(drv, 0, tb);
2687 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002688 case NL80211_CMD_SCAN_ABORTED:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002689 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Scan aborted");
Dmitry Shmidt56052862013-10-04 10:23:25 -07002690 drv->scan_state = SCAN_ABORTED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002691 /*
2692 * Need to indicate that scan results are available in order
2693 * not to make wpa_supplicant stop its scanning.
2694 */
2695 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
2696 drv->ctx);
2697 send_scan_event(drv, 1, tb);
2698 break;
2699 case NL80211_CMD_AUTHENTICATE:
2700 case NL80211_CMD_ASSOCIATE:
2701 case NL80211_CMD_DEAUTHENTICATE:
2702 case NL80211_CMD_DISASSOCIATE:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002703 case NL80211_CMD_FRAME_TX_STATUS:
2704 case NL80211_CMD_UNPROT_DEAUTHENTICATE:
2705 case NL80211_CMD_UNPROT_DISASSOCIATE:
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002706 mlme_event(bss, cmd, tb[NL80211_ATTR_FRAME],
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002707 tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT],
2708 tb[NL80211_ATTR_WIPHY_FREQ], tb[NL80211_ATTR_ACK],
Dmitry Shmidt04949592012-07-19 12:16:46 -07002709 tb[NL80211_ATTR_COOKIE],
2710 tb[NL80211_ATTR_RX_SIGNAL_DBM]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002711 break;
2712 case NL80211_CMD_CONNECT:
2713 case NL80211_CMD_ROAM:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002714 mlme_event_connect(drv, cmd,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002715 tb[NL80211_ATTR_STATUS_CODE],
2716 tb[NL80211_ATTR_MAC],
2717 tb[NL80211_ATTR_REQ_IE],
2718 tb[NL80211_ATTR_RESP_IE]);
2719 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002720 case NL80211_CMD_CH_SWITCH_NOTIFY:
2721 mlme_event_ch_switch(drv, tb[NL80211_ATTR_WIPHY_FREQ],
2722 tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
2723 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002724 case NL80211_CMD_DISCONNECT:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002725 mlme_event_disconnect(drv, tb[NL80211_ATTR_REASON_CODE],
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002726 tb[NL80211_ATTR_MAC],
2727 tb[NL80211_ATTR_DISCONNECTED_BY_AP]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002728 break;
2729 case NL80211_CMD_MICHAEL_MIC_FAILURE:
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002730 mlme_event_michael_mic_failure(bss, tb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002731 break;
2732 case NL80211_CMD_JOIN_IBSS:
2733 mlme_event_join_ibss(drv, tb);
2734 break;
2735 case NL80211_CMD_REMAIN_ON_CHANNEL:
2736 mlme_event_remain_on_channel(drv, 0, tb);
2737 break;
2738 case NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL:
2739 mlme_event_remain_on_channel(drv, 1, tb);
2740 break;
2741 case NL80211_CMD_NOTIFY_CQM:
2742 nl80211_cqm_event(drv, tb);
2743 break;
2744 case NL80211_CMD_REG_CHANGE:
2745 wpa_printf(MSG_DEBUG, "nl80211: Regulatory domain change");
2746 wpa_supplicant_event(drv->ctx, EVENT_CHANNEL_LIST_CHANGED,
2747 NULL);
2748 break;
2749 case NL80211_CMD_REG_BEACON_HINT:
2750 wpa_printf(MSG_DEBUG, "nl80211: Regulatory beacon hint");
2751 wpa_supplicant_event(drv->ctx, EVENT_CHANNEL_LIST_CHANGED,
2752 NULL);
2753 break;
2754 case NL80211_CMD_NEW_STATION:
2755 nl80211_new_station_event(drv, tb);
2756 break;
2757 case NL80211_CMD_DEL_STATION:
2758 nl80211_del_station_event(drv, tb);
2759 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002760 case NL80211_CMD_SET_REKEY_OFFLOAD:
2761 nl80211_rekey_offload_event(drv, tb);
2762 break;
2763 case NL80211_CMD_PMKSA_CANDIDATE:
2764 nl80211_pmksa_candidate_event(drv, tb);
2765 break;
2766 case NL80211_CMD_PROBE_CLIENT:
2767 nl80211_client_probe_event(drv, tb);
2768 break;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002769 case NL80211_CMD_TDLS_OPER:
2770 nl80211_tdls_oper_event(drv, tb);
2771 break;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002772 case NL80211_CMD_CONN_FAILED:
2773 nl80211_connect_failed_event(drv, tb);
2774 break;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002775 case NL80211_CMD_FT_EVENT:
2776 mlme_event_ft_event(drv, tb);
2777 break;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002778 case NL80211_CMD_RADAR_DETECT:
2779 nl80211_radar_event(drv, tb);
2780 break;
Dmitry Shmidt5393a0f2013-08-08 11:23:34 -07002781 case NL80211_CMD_STOP_AP:
2782 nl80211_stop_ap(drv, tb);
2783 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002784 default:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002785 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Ignored unknown event "
2786 "(cmd=%d)", cmd);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002787 break;
2788 }
2789}
2790
2791
2792static int process_drv_event(struct nl_msg *msg, void *arg)
2793{
2794 struct wpa_driver_nl80211_data *drv = arg;
2795 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2796 struct nlattr *tb[NL80211_ATTR_MAX + 1];
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002797 struct i802_bss *bss;
2798 int ifidx = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002799
2800 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2801 genlmsg_attrlen(gnlh, 0), NULL);
2802
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002803 if (tb[NL80211_ATTR_IFINDEX]) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002804 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
2805
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002806 for (bss = drv->first_bss; bss; bss = bss->next)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002807 if (ifidx == -1 || ifidx == bss->ifindex) {
2808 do_process_drv_event(bss, gnlh->cmd, tb);
2809 return NL_SKIP;
2810 }
2811 wpa_printf(MSG_DEBUG,
2812 "nl80211: Ignored event (cmd=%d) for foreign interface (ifindex %d)",
2813 gnlh->cmd, ifidx);
2814 } else if (tb[NL80211_ATTR_WDEV]) {
2815 u64 wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
2816 wpa_printf(MSG_DEBUG, "nl80211: Process event on P2P device");
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002817 for (bss = drv->first_bss; bss; bss = bss->next) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002818 if (bss->wdev_id_set && wdev_id == bss->wdev_id) {
2819 do_process_drv_event(bss, gnlh->cmd, tb);
2820 return NL_SKIP;
2821 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002822 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002823 wpa_printf(MSG_DEBUG,
2824 "nl80211: Ignored event (cmd=%d) for foreign interface (wdev 0x%llx)",
2825 gnlh->cmd, (long long unsigned int) wdev_id);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002826 }
2827
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002828 return NL_SKIP;
2829}
2830
2831
2832static int process_global_event(struct nl_msg *msg, void *arg)
2833{
2834 struct nl80211_global *global = arg;
2835 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2836 struct nlattr *tb[NL80211_ATTR_MAX + 1];
Dmitry Shmidt04949592012-07-19 12:16:46 -07002837 struct wpa_driver_nl80211_data *drv, *tmp;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002838 int ifidx = -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002839 struct i802_bss *bss;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002840 u64 wdev_id = 0;
2841 int wdev_id_set = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002842
2843 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2844 genlmsg_attrlen(gnlh, 0), NULL);
2845
2846 if (tb[NL80211_ATTR_IFINDEX])
2847 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002848 else if (tb[NL80211_ATTR_WDEV]) {
2849 wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
2850 wdev_id_set = 1;
2851 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002852
Dmitry Shmidt04949592012-07-19 12:16:46 -07002853 dl_list_for_each_safe(drv, tmp, &global->interfaces,
2854 struct wpa_driver_nl80211_data, list) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002855 for (bss = drv->first_bss; bss; bss = bss->next) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002856 if ((ifidx == -1 && !wdev_id_set) ||
2857 ifidx == bss->ifindex ||
2858 (wdev_id_set && bss->wdev_id_set &&
2859 wdev_id == bss->wdev_id)) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002860 do_process_drv_event(bss, gnlh->cmd, tb);
2861 return NL_SKIP;
2862 }
2863 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002864 }
2865
2866 return NL_SKIP;
2867}
2868
2869
2870static int process_bss_event(struct nl_msg *msg, void *arg)
2871{
2872 struct i802_bss *bss = arg;
2873 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2874 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2875
2876 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2877 genlmsg_attrlen(gnlh, 0), NULL);
2878
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002879 wpa_printf(MSG_DEBUG, "nl80211: BSS Event %d (%s) received for %s",
2880 gnlh->cmd, nl80211_command_to_string(gnlh->cmd),
2881 bss->ifname);
2882
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002883 switch (gnlh->cmd) {
2884 case NL80211_CMD_FRAME:
2885 case NL80211_CMD_FRAME_TX_STATUS:
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002886 mlme_event(bss, gnlh->cmd, tb[NL80211_ATTR_FRAME],
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002887 tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT],
2888 tb[NL80211_ATTR_WIPHY_FREQ], tb[NL80211_ATTR_ACK],
Dmitry Shmidt04949592012-07-19 12:16:46 -07002889 tb[NL80211_ATTR_COOKIE],
2890 tb[NL80211_ATTR_RX_SIGNAL_DBM]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002891 break;
2892 case NL80211_CMD_UNEXPECTED_FRAME:
2893 nl80211_spurious_frame(bss, tb, 0);
2894 break;
2895 case NL80211_CMD_UNEXPECTED_4ADDR_FRAME:
2896 nl80211_spurious_frame(bss, tb, 1);
2897 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002898 default:
2899 wpa_printf(MSG_DEBUG, "nl80211: Ignored unknown event "
2900 "(cmd=%d)", gnlh->cmd);
2901 break;
2902 }
2903
2904 return NL_SKIP;
2905}
2906
2907
2908static void wpa_driver_nl80211_event_receive(int sock, void *eloop_ctx,
2909 void *handle)
2910{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002911 struct nl_cb *cb = eloop_ctx;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002912 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002913
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002914 wpa_printf(MSG_MSGDUMP, "nl80211: Event message available");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002915
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002916 res = nl_recvmsgs(handle, cb);
2917 if (res) {
2918 wpa_printf(MSG_INFO, "nl80211: %s->nl_recvmsgs failed: %d",
2919 __func__, res);
2920 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002921}
2922
2923
2924/**
2925 * wpa_driver_nl80211_set_country - ask nl80211 to set the regulatory domain
2926 * @priv: driver_nl80211 private data
2927 * @alpha2_arg: country to which to switch to
2928 * Returns: 0 on success, -1 on failure
2929 *
2930 * This asks nl80211 to set the regulatory domain for given
2931 * country ISO / IEC alpha2.
2932 */
2933static int wpa_driver_nl80211_set_country(void *priv, const char *alpha2_arg)
2934{
2935 struct i802_bss *bss = priv;
2936 struct wpa_driver_nl80211_data *drv = bss->drv;
2937 char alpha2[3];
2938 struct nl_msg *msg;
2939
2940 msg = nlmsg_alloc();
2941 if (!msg)
2942 return -ENOMEM;
2943
2944 alpha2[0] = alpha2_arg[0];
2945 alpha2[1] = alpha2_arg[1];
2946 alpha2[2] = '\0';
2947
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002948 nl80211_cmd(drv, msg, 0, NL80211_CMD_REQ_SET_REG);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002949
2950 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, alpha2);
2951 if (send_and_recv_msgs(drv, msg, NULL, NULL))
2952 return -EINVAL;
2953 return 0;
2954nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002955 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002956 return -EINVAL;
2957}
2958
2959
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002960static int nl80211_get_country(struct nl_msg *msg, void *arg)
2961{
2962 char *alpha2 = arg;
2963 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
2964 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2965
2966 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2967 genlmsg_attrlen(gnlh, 0), NULL);
2968 if (!tb_msg[NL80211_ATTR_REG_ALPHA2]) {
2969 wpa_printf(MSG_DEBUG, "nl80211: No country information available");
2970 return NL_SKIP;
2971 }
2972 os_strlcpy(alpha2, nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]), 3);
2973 return NL_SKIP;
2974}
2975
2976
2977static int wpa_driver_nl80211_get_country(void *priv, char *alpha2)
2978{
2979 struct i802_bss *bss = priv;
2980 struct wpa_driver_nl80211_data *drv = bss->drv;
2981 struct nl_msg *msg;
2982 int ret;
2983
2984 msg = nlmsg_alloc();
2985 if (!msg)
2986 return -ENOMEM;
2987
2988 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG);
2989 alpha2[0] = '\0';
2990 ret = send_and_recv_msgs(drv, msg, nl80211_get_country, alpha2);
2991 if (!alpha2[0])
2992 ret = -1;
2993
2994 return ret;
2995}
2996
2997
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002998static int protocol_feature_handler(struct nl_msg *msg, void *arg)
2999{
3000 u32 *feat = arg;
3001 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
3002 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3003
3004 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3005 genlmsg_attrlen(gnlh, 0), NULL);
3006
3007 if (tb_msg[NL80211_ATTR_PROTOCOL_FEATURES])
3008 *feat = nla_get_u32(tb_msg[NL80211_ATTR_PROTOCOL_FEATURES]);
3009
3010 return NL_SKIP;
3011}
3012
3013
3014static u32 get_nl80211_protocol_features(struct wpa_driver_nl80211_data *drv)
3015{
3016 u32 feat = 0;
3017 struct nl_msg *msg;
3018
3019 msg = nlmsg_alloc();
3020 if (!msg)
3021 goto nla_put_failure;
3022
3023 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_PROTOCOL_FEATURES);
3024 if (send_and_recv_msgs(drv, msg, protocol_feature_handler, &feat) == 0)
3025 return feat;
3026
3027 msg = NULL;
3028nla_put_failure:
3029 nlmsg_free(msg);
3030 return 0;
3031}
3032
3033
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003034struct wiphy_info_data {
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003035 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003036 struct wpa_driver_capa *capa;
3037
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003038 unsigned int num_multichan_concurrent;
3039
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003040 unsigned int error:1;
3041 unsigned int device_ap_sme:1;
3042 unsigned int poll_command_supported:1;
3043 unsigned int data_tx_status:1;
3044 unsigned int monitor_supported:1;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003045 unsigned int auth_supported:1;
3046 unsigned int connect_supported:1;
3047 unsigned int p2p_go_supported:1;
3048 unsigned int p2p_client_supported:1;
3049 unsigned int p2p_concurrent:1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003050};
3051
3052
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003053static unsigned int probe_resp_offload_support(int supp_protocols)
3054{
3055 unsigned int prot = 0;
3056
3057 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS)
3058 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS;
3059 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2)
3060 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS2;
3061 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P)
3062 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_P2P;
3063 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_80211U)
3064 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_INTERWORKING;
3065
3066 return prot;
3067}
3068
3069
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003070static void wiphy_info_supported_iftypes(struct wiphy_info_data *info,
3071 struct nlattr *tb)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003072{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003073 struct nlattr *nl_mode;
3074 int i;
3075
3076 if (tb == NULL)
3077 return;
3078
3079 nla_for_each_nested(nl_mode, tb, i) {
3080 switch (nla_type(nl_mode)) {
3081 case NL80211_IFTYPE_AP:
3082 info->capa->flags |= WPA_DRIVER_FLAGS_AP;
3083 break;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003084 case NL80211_IFTYPE_ADHOC:
3085 info->capa->flags |= WPA_DRIVER_FLAGS_IBSS;
3086 break;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003087 case NL80211_IFTYPE_P2P_DEVICE:
3088 info->capa->flags |=
3089 WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE;
3090 break;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003091 case NL80211_IFTYPE_P2P_GO:
3092 info->p2p_go_supported = 1;
3093 break;
3094 case NL80211_IFTYPE_P2P_CLIENT:
3095 info->p2p_client_supported = 1;
3096 break;
3097 case NL80211_IFTYPE_MONITOR:
3098 info->monitor_supported = 1;
3099 break;
3100 }
3101 }
3102}
3103
3104
3105static int wiphy_info_iface_comb_process(struct wiphy_info_data *info,
3106 struct nlattr *nl_combi)
3107{
3108 struct nlattr *tb_comb[NUM_NL80211_IFACE_COMB];
3109 struct nlattr *tb_limit[NUM_NL80211_IFACE_LIMIT];
3110 struct nlattr *nl_limit, *nl_mode;
3111 int err, rem_limit, rem_mode;
3112 int combination_has_p2p = 0, combination_has_mgd = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003113 static struct nla_policy
3114 iface_combination_policy[NUM_NL80211_IFACE_COMB] = {
3115 [NL80211_IFACE_COMB_LIMITS] = { .type = NLA_NESTED },
3116 [NL80211_IFACE_COMB_MAXNUM] = { .type = NLA_U32 },
3117 [NL80211_IFACE_COMB_STA_AP_BI_MATCH] = { .type = NLA_FLAG },
3118 [NL80211_IFACE_COMB_NUM_CHANNELS] = { .type = NLA_U32 },
Dmitry Shmidtea69e842013-05-13 14:52:28 -07003119 [NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS] = { .type = NLA_U32 },
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003120 },
3121 iface_limit_policy[NUM_NL80211_IFACE_LIMIT] = {
3122 [NL80211_IFACE_LIMIT_TYPES] = { .type = NLA_NESTED },
3123 [NL80211_IFACE_LIMIT_MAX] = { .type = NLA_U32 },
3124 };
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003125
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003126 err = nla_parse_nested(tb_comb, MAX_NL80211_IFACE_COMB,
3127 nl_combi, iface_combination_policy);
3128 if (err || !tb_comb[NL80211_IFACE_COMB_LIMITS] ||
3129 !tb_comb[NL80211_IFACE_COMB_MAXNUM] ||
3130 !tb_comb[NL80211_IFACE_COMB_NUM_CHANNELS])
3131 return 0; /* broken combination */
3132
Dmitry Shmidtea69e842013-05-13 14:52:28 -07003133 if (tb_comb[NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS])
3134 info->capa->flags |= WPA_DRIVER_FLAGS_RADAR;
3135
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003136 nla_for_each_nested(nl_limit, tb_comb[NL80211_IFACE_COMB_LIMITS],
3137 rem_limit) {
3138 err = nla_parse_nested(tb_limit, MAX_NL80211_IFACE_LIMIT,
3139 nl_limit, iface_limit_policy);
3140 if (err || !tb_limit[NL80211_IFACE_LIMIT_TYPES])
3141 return 0; /* broken combination */
3142
3143 nla_for_each_nested(nl_mode,
3144 tb_limit[NL80211_IFACE_LIMIT_TYPES],
3145 rem_mode) {
3146 int ift = nla_type(nl_mode);
3147 if (ift == NL80211_IFTYPE_P2P_GO ||
3148 ift == NL80211_IFTYPE_P2P_CLIENT)
3149 combination_has_p2p = 1;
3150 if (ift == NL80211_IFTYPE_STATION)
3151 combination_has_mgd = 1;
3152 }
3153 if (combination_has_p2p && combination_has_mgd)
3154 break;
3155 }
3156
3157 if (combination_has_p2p && combination_has_mgd) {
3158 info->p2p_concurrent = 1;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003159 info->num_multichan_concurrent =
3160 nla_get_u32(tb_comb[NL80211_IFACE_COMB_NUM_CHANNELS]);
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003161 return 1;
3162 }
3163
3164 return 0;
3165}
3166
3167
3168static void wiphy_info_iface_comb(struct wiphy_info_data *info,
3169 struct nlattr *tb)
3170{
3171 struct nlattr *nl_combi;
3172 int rem_combi;
3173
3174 if (tb == NULL)
3175 return;
3176
3177 nla_for_each_nested(nl_combi, tb, rem_combi) {
3178 if (wiphy_info_iface_comb_process(info, nl_combi) > 0)
3179 break;
3180 }
3181}
3182
3183
3184static void wiphy_info_supp_cmds(struct wiphy_info_data *info,
3185 struct nlattr *tb)
3186{
3187 struct nlattr *nl_cmd;
3188 int i;
3189
3190 if (tb == NULL)
3191 return;
3192
3193 nla_for_each_nested(nl_cmd, tb, i) {
3194 switch (nla_get_u32(nl_cmd)) {
3195 case NL80211_CMD_AUTHENTICATE:
3196 info->auth_supported = 1;
3197 break;
3198 case NL80211_CMD_CONNECT:
3199 info->connect_supported = 1;
3200 break;
3201 case NL80211_CMD_START_SCHED_SCAN:
3202 info->capa->sched_scan_supported = 1;
3203 break;
3204 case NL80211_CMD_PROBE_CLIENT:
3205 info->poll_command_supported = 1;
3206 break;
3207 }
3208 }
3209}
3210
3211
3212static void wiphy_info_max_roc(struct wpa_driver_capa *capa,
3213 struct nlattr *tb)
3214{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003215 if (tb)
3216 capa->max_remain_on_chan = nla_get_u32(tb);
3217}
3218
3219
3220static void wiphy_info_tdls(struct wpa_driver_capa *capa, struct nlattr *tdls,
3221 struct nlattr *ext_setup)
3222{
3223 if (tdls == NULL)
3224 return;
3225
3226 wpa_printf(MSG_DEBUG, "nl80211: TDLS supported");
3227 capa->flags |= WPA_DRIVER_FLAGS_TDLS_SUPPORT;
3228
3229 if (ext_setup) {
3230 wpa_printf(MSG_DEBUG, "nl80211: TDLS external setup");
3231 capa->flags |= WPA_DRIVER_FLAGS_TDLS_EXTERNAL_SETUP;
3232 }
3233}
3234
3235
3236static void wiphy_info_feature_flags(struct wiphy_info_data *info,
3237 struct nlattr *tb)
3238{
3239 u32 flags;
3240 struct wpa_driver_capa *capa = info->capa;
3241
3242 if (tb == NULL)
3243 return;
3244
3245 flags = nla_get_u32(tb);
3246
3247 if (flags & NL80211_FEATURE_SK_TX_STATUS)
3248 info->data_tx_status = 1;
3249
3250 if (flags & NL80211_FEATURE_INACTIVITY_TIMER)
3251 capa->flags |= WPA_DRIVER_FLAGS_INACTIVITY_TIMER;
3252
3253 if (flags & NL80211_FEATURE_SAE)
3254 capa->flags |= WPA_DRIVER_FLAGS_SAE;
3255
3256 if (flags & NL80211_FEATURE_NEED_OBSS_SCAN)
3257 capa->flags |= WPA_DRIVER_FLAGS_OBSS_SCAN;
3258}
3259
3260
3261static void wiphy_info_probe_resp_offload(struct wpa_driver_capa *capa,
3262 struct nlattr *tb)
3263{
3264 u32 protocols;
3265
3266 if (tb == NULL)
3267 return;
3268
3269 protocols = nla_get_u32(tb);
3270 wpa_printf(MSG_DEBUG, "nl80211: Supports Probe Response offload in AP "
3271 "mode");
3272 capa->flags |= WPA_DRIVER_FLAGS_PROBE_RESP_OFFLOAD;
3273 capa->probe_resp_offloads = probe_resp_offload_support(protocols);
3274}
3275
3276
3277static int wiphy_info_handler(struct nl_msg *msg, void *arg)
3278{
3279 struct nlattr *tb[NL80211_ATTR_MAX + 1];
3280 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3281 struct wiphy_info_data *info = arg;
3282 struct wpa_driver_capa *capa = info->capa;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003283 struct wpa_driver_nl80211_data *drv = info->drv;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003284
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003285 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3286 genlmsg_attrlen(gnlh, 0), NULL);
3287
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003288 if (tb[NL80211_ATTR_WIPHY_NAME])
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003289 os_strlcpy(drv->phyname,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003290 nla_get_string(tb[NL80211_ATTR_WIPHY_NAME]),
3291 sizeof(drv->phyname));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003292 if (tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS])
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003293 capa->max_scan_ssids =
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003294 nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS]);
3295
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003296 if (tb[NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS])
3297 capa->max_sched_scan_ssids =
3298 nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS]);
3299
3300 if (tb[NL80211_ATTR_MAX_MATCH_SETS])
3301 capa->max_match_sets =
3302 nla_get_u8(tb[NL80211_ATTR_MAX_MATCH_SETS]);
3303
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003304 if (tb[NL80211_ATTR_MAC_ACL_MAX])
3305 capa->max_acl_mac_addrs =
3306 nla_get_u8(tb[NL80211_ATTR_MAC_ACL_MAX]);
3307
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003308 wiphy_info_supported_iftypes(info, tb[NL80211_ATTR_SUPPORTED_IFTYPES]);
3309 wiphy_info_iface_comb(info, tb[NL80211_ATTR_INTERFACE_COMBINATIONS]);
3310 wiphy_info_supp_cmds(info, tb[NL80211_ATTR_SUPPORTED_COMMANDS]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003311
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003312 if (tb[NL80211_ATTR_OFFCHANNEL_TX_OK]) {
3313 wpa_printf(MSG_DEBUG, "nl80211: Using driver-based "
3314 "off-channel TX");
3315 capa->flags |= WPA_DRIVER_FLAGS_OFFCHANNEL_TX;
3316 }
3317
3318 if (tb[NL80211_ATTR_ROAM_SUPPORT]) {
3319 wpa_printf(MSG_DEBUG, "nl80211: Using driver-based roaming");
3320 capa->flags |= WPA_DRIVER_FLAGS_BSS_SELECTION;
3321 }
3322
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003323 wiphy_info_max_roc(capa,
3324 tb[NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003325
3326 if (tb[NL80211_ATTR_SUPPORT_AP_UAPSD])
3327 capa->flags |= WPA_DRIVER_FLAGS_AP_UAPSD;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003328
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003329 wiphy_info_tdls(capa, tb[NL80211_ATTR_TDLS_SUPPORT],
3330 tb[NL80211_ATTR_TDLS_EXTERNAL_SETUP]);
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003331
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003332 if (tb[NL80211_ATTR_DEVICE_AP_SME])
3333 info->device_ap_sme = 1;
3334
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003335 wiphy_info_feature_flags(info, tb[NL80211_ATTR_FEATURE_FLAGS]);
3336 wiphy_info_probe_resp_offload(capa,
3337 tb[NL80211_ATTR_PROBE_RESP_OFFLOAD]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003338
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003339 if (tb[NL80211_ATTR_EXT_CAPA] && tb[NL80211_ATTR_EXT_CAPA_MASK] &&
3340 drv->extended_capa == NULL) {
3341 drv->extended_capa =
3342 os_malloc(nla_len(tb[NL80211_ATTR_EXT_CAPA]));
3343 if (drv->extended_capa) {
3344 os_memcpy(drv->extended_capa,
3345 nla_data(tb[NL80211_ATTR_EXT_CAPA]),
3346 nla_len(tb[NL80211_ATTR_EXT_CAPA]));
3347 drv->extended_capa_len =
3348 nla_len(tb[NL80211_ATTR_EXT_CAPA]);
3349 }
3350 drv->extended_capa_mask =
3351 os_malloc(nla_len(tb[NL80211_ATTR_EXT_CAPA]));
3352 if (drv->extended_capa_mask) {
3353 os_memcpy(drv->extended_capa_mask,
3354 nla_data(tb[NL80211_ATTR_EXT_CAPA]),
3355 nla_len(tb[NL80211_ATTR_EXT_CAPA]));
3356 } else {
3357 os_free(drv->extended_capa);
3358 drv->extended_capa = NULL;
3359 drv->extended_capa_len = 0;
3360 }
3361 }
3362
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003363 return NL_SKIP;
3364}
3365
3366
3367static int wpa_driver_nl80211_get_info(struct wpa_driver_nl80211_data *drv,
3368 struct wiphy_info_data *info)
3369{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003370 u32 feat;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003371 struct nl_msg *msg;
3372
3373 os_memset(info, 0, sizeof(*info));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003374 info->capa = &drv->capa;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003375 info->drv = drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003376
3377 msg = nlmsg_alloc();
3378 if (!msg)
3379 return -1;
3380
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003381 feat = get_nl80211_protocol_features(drv);
3382 if (feat & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP)
3383 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_WIPHY);
3384 else
3385 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003386
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003387 NLA_PUT_FLAG(msg, NL80211_ATTR_SPLIT_WIPHY_DUMP);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003388 if (nl80211_set_iface_id(msg, drv->first_bss) < 0)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003389 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003390
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003391 if (send_and_recv_msgs(drv, msg, wiphy_info_handler, info))
3392 return -1;
3393
3394 if (info->auth_supported)
3395 drv->capa.flags |= WPA_DRIVER_FLAGS_SME;
3396 else if (!info->connect_supported) {
3397 wpa_printf(MSG_INFO, "nl80211: Driver does not support "
3398 "authentication/association or connect commands");
3399 info->error = 1;
3400 }
3401
3402 if (info->p2p_go_supported && info->p2p_client_supported)
3403 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CAPABLE;
3404 if (info->p2p_concurrent) {
3405 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
3406 "interface (driver advertised support)");
3407 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
3408 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
3409 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003410 if (info->num_multichan_concurrent > 1) {
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003411 wpa_printf(MSG_DEBUG, "nl80211: Enable multi-channel "
3412 "concurrent (driver advertised support)");
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003413 drv->capa.num_multichan_concurrent =
3414 info->num_multichan_concurrent;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003415 }
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07003416
3417 /* default to 5000 since early versions of mac80211 don't set it */
3418 if (!drv->capa.max_remain_on_chan)
3419 drv->capa.max_remain_on_chan = 5000;
3420
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003421 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003422nla_put_failure:
3423 nlmsg_free(msg);
3424 return -1;
3425}
3426
3427
3428static int wpa_driver_nl80211_capa(struct wpa_driver_nl80211_data *drv)
3429{
3430 struct wiphy_info_data info;
3431 if (wpa_driver_nl80211_get_info(drv, &info))
3432 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003433
3434 if (info.error)
3435 return -1;
3436
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003437 drv->has_capability = 1;
3438 /* For now, assume TKIP, CCMP, WPA, WPA2 are supported */
3439 drv->capa.key_mgmt = WPA_DRIVER_CAPA_KEY_MGMT_WPA |
3440 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
3441 WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
3442 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK;
3443 drv->capa.enc = WPA_DRIVER_CAPA_ENC_WEP40 |
3444 WPA_DRIVER_CAPA_ENC_WEP104 |
3445 WPA_DRIVER_CAPA_ENC_TKIP |
3446 WPA_DRIVER_CAPA_ENC_CCMP;
3447 drv->capa.auth = WPA_DRIVER_AUTH_OPEN |
3448 WPA_DRIVER_AUTH_SHARED |
3449 WPA_DRIVER_AUTH_LEAP;
3450
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003451 drv->capa.flags |= WPA_DRIVER_FLAGS_SANE_ERROR_CODES;
3452 drv->capa.flags |= WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003453 drv->capa.flags |= WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003454
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003455 if (!info.device_ap_sme) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07003456 drv->capa.flags |= WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003457
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003458 /*
3459 * No AP SME is currently assumed to also indicate no AP MLME
3460 * in the driver/firmware.
3461 */
3462 drv->capa.flags |= WPA_DRIVER_FLAGS_AP_MLME;
3463 }
3464
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003465 drv->device_ap_sme = info.device_ap_sme;
3466 drv->poll_command_supported = info.poll_command_supported;
3467 drv->data_tx_status = info.data_tx_status;
3468
3469 /*
Dmitry Shmidtaa532512012-09-24 10:35:31 -07003470 * If poll command and tx status are supported, mac80211 is new enough
3471 * to have everything we need to not need monitor interfaces.
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003472 */
Dmitry Shmidtaa532512012-09-24 10:35:31 -07003473 drv->use_monitor = !info.poll_command_supported || !info.data_tx_status;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003474
3475 if (drv->device_ap_sme && drv->use_monitor) {
3476 /*
3477 * Non-mac80211 drivers may not support monitor interface.
3478 * Make sure we do not get stuck with incorrect capability here
3479 * by explicitly testing this.
3480 */
3481 if (!info.monitor_supported) {
3482 wpa_printf(MSG_DEBUG, "nl80211: Disable use_monitor "
3483 "with device_ap_sme since no monitor mode "
3484 "support detected");
3485 drv->use_monitor = 0;
3486 }
3487 }
3488
3489 /*
3490 * If we aren't going to use monitor interfaces, but the
3491 * driver doesn't support data TX status, we won't get TX
3492 * status for EAPOL frames.
3493 */
3494 if (!drv->use_monitor && !info.data_tx_status)
3495 drv->capa.flags &= ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003496
3497 return 0;
3498}
3499
3500
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003501#ifdef ANDROID
3502static int android_genl_ctrl_resolve(struct nl_handle *handle,
3503 const char *name)
3504{
3505 /*
3506 * Android ICS has very minimal genl_ctrl_resolve() implementation, so
3507 * need to work around that.
3508 */
3509 struct nl_cache *cache = NULL;
3510 struct genl_family *nl80211 = NULL;
3511 int id = -1;
3512
3513 if (genl_ctrl_alloc_cache(handle, &cache) < 0) {
3514 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic "
3515 "netlink cache");
3516 goto fail;
3517 }
3518
3519 nl80211 = genl_ctrl_search_by_name(cache, name);
3520 if (nl80211 == NULL)
3521 goto fail;
3522
3523 id = genl_family_get_id(nl80211);
3524
3525fail:
3526 if (nl80211)
3527 genl_family_put(nl80211);
3528 if (cache)
3529 nl_cache_free(cache);
3530
3531 return id;
3532}
3533#define genl_ctrl_resolve android_genl_ctrl_resolve
3534#endif /* ANDROID */
3535
3536
3537static int wpa_driver_nl80211_init_nl_global(struct nl80211_global *global)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003538{
3539 int ret;
3540
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003541 global->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
3542 if (global->nl_cb == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003543 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
3544 "callbacks");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003545 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003546 }
3547
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003548 global->nl = nl_create_handle(global->nl_cb, "nl");
3549 if (global->nl == NULL)
3550 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003551
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003552 global->nl80211_id = genl_ctrl_resolve(global->nl, "nl80211");
3553 if (global->nl80211_id < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003554 wpa_printf(MSG_ERROR, "nl80211: 'nl80211' generic netlink not "
3555 "found");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003556 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003557 }
3558
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003559 global->nl_event = nl_create_handle(global->nl_cb, "event");
3560 if (global->nl_event == NULL)
3561 goto err;
3562
3563 ret = nl_get_multicast_id(global, "nl80211", "scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003564 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003565 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003566 if (ret < 0) {
3567 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
3568 "membership for scan events: %d (%s)",
3569 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003570 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003571 }
3572
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003573 ret = nl_get_multicast_id(global, "nl80211", "mlme");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003574 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003575 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003576 if (ret < 0) {
3577 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
3578 "membership for mlme events: %d (%s)",
3579 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003580 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003581 }
3582
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003583 ret = nl_get_multicast_id(global, "nl80211", "regulatory");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003584 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003585 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003586 if (ret < 0) {
3587 wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
3588 "membership for regulatory events: %d (%s)",
3589 ret, strerror(-ret));
3590 /* Continue without regulatory events */
3591 }
3592
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003593 nl_cb_set(global->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
3594 no_seq_check, NULL);
3595 nl_cb_set(global->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
3596 process_global_event, global);
3597
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003598 nl80211_register_eloop_read(&global->nl_event,
3599 wpa_driver_nl80211_event_receive,
3600 global->nl_cb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003601
3602 return 0;
3603
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003604err:
3605 nl_destroy_handles(&global->nl_event);
3606 nl_destroy_handles(&global->nl);
3607 nl_cb_put(global->nl_cb);
3608 global->nl_cb = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003609 return -1;
3610}
3611
3612
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003613static int wpa_driver_nl80211_init_nl(struct wpa_driver_nl80211_data *drv)
3614{
3615 drv->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
3616 if (!drv->nl_cb) {
3617 wpa_printf(MSG_ERROR, "nl80211: Failed to alloc cb struct");
3618 return -1;
3619 }
3620
3621 nl_cb_set(drv->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
3622 no_seq_check, NULL);
3623 nl_cb_set(drv->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
3624 process_drv_event, drv);
3625
3626 return 0;
3627}
3628
3629
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003630static void wpa_driver_nl80211_rfkill_blocked(void *ctx)
3631{
3632 wpa_printf(MSG_DEBUG, "nl80211: RFKILL blocked");
3633 /*
3634 * This may be for any interface; use ifdown event to disable
3635 * interface.
3636 */
3637}
3638
3639
3640static void wpa_driver_nl80211_rfkill_unblocked(void *ctx)
3641{
3642 struct wpa_driver_nl80211_data *drv = ctx;
3643 wpa_printf(MSG_DEBUG, "nl80211: RFKILL unblocked");
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003644 if (i802_set_iface_flags(drv->first_bss, 1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003645 wpa_printf(MSG_DEBUG, "nl80211: Could not set interface UP "
3646 "after rfkill unblock");
3647 return;
3648 }
3649 /* rtnetlink ifup handler will report interface as enabled */
3650}
3651
3652
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003653static void wpa_driver_nl80211_handle_eapol_tx_status(int sock,
3654 void *eloop_ctx,
3655 void *handle)
3656{
3657 struct wpa_driver_nl80211_data *drv = eloop_ctx;
3658 u8 data[2048];
3659 struct msghdr msg;
3660 struct iovec entry;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003661 u8 control[512];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003662 struct cmsghdr *cmsg;
3663 int res, found_ee = 0, found_wifi = 0, acked = 0;
3664 union wpa_event_data event;
3665
3666 memset(&msg, 0, sizeof(msg));
3667 msg.msg_iov = &entry;
3668 msg.msg_iovlen = 1;
3669 entry.iov_base = data;
3670 entry.iov_len = sizeof(data);
3671 msg.msg_control = &control;
3672 msg.msg_controllen = sizeof(control);
3673
3674 res = recvmsg(sock, &msg, MSG_ERRQUEUE);
3675 /* if error or not fitting 802.3 header, return */
3676 if (res < 14)
3677 return;
3678
3679 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg))
3680 {
3681 if (cmsg->cmsg_level == SOL_SOCKET &&
3682 cmsg->cmsg_type == SCM_WIFI_STATUS) {
3683 int *ack;
3684
3685 found_wifi = 1;
3686 ack = (void *)CMSG_DATA(cmsg);
3687 acked = *ack;
3688 }
3689
3690 if (cmsg->cmsg_level == SOL_PACKET &&
3691 cmsg->cmsg_type == PACKET_TX_TIMESTAMP) {
3692 struct sock_extended_err *err =
3693 (struct sock_extended_err *)CMSG_DATA(cmsg);
3694
3695 if (err->ee_origin == SO_EE_ORIGIN_TXSTATUS)
3696 found_ee = 1;
3697 }
3698 }
3699
3700 if (!found_ee || !found_wifi)
3701 return;
3702
3703 memset(&event, 0, sizeof(event));
3704 event.eapol_tx_status.dst = data;
3705 event.eapol_tx_status.data = data + 14;
3706 event.eapol_tx_status.data_len = res - 14;
3707 event.eapol_tx_status.ack = acked;
3708 wpa_supplicant_event(drv->ctx, EVENT_EAPOL_TX_STATUS, &event);
3709}
3710
3711
3712static int nl80211_init_bss(struct i802_bss *bss)
3713{
3714 bss->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
3715 if (!bss->nl_cb)
3716 return -1;
3717
3718 nl_cb_set(bss->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
3719 no_seq_check, NULL);
3720 nl_cb_set(bss->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
3721 process_bss_event, bss);
3722
3723 return 0;
3724}
3725
3726
3727static void nl80211_destroy_bss(struct i802_bss *bss)
3728{
3729 nl_cb_put(bss->nl_cb);
3730 bss->nl_cb = NULL;
3731}
3732
3733
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003734/**
3735 * wpa_driver_nl80211_init - Initialize nl80211 driver interface
3736 * @ctx: context to be used when calling wpa_supplicant functions,
3737 * e.g., wpa_supplicant_event()
3738 * @ifname: interface name, e.g., wlan0
3739 * @global_priv: private driver global data from global_init()
3740 * Returns: Pointer to private data, %NULL on failure
3741 */
3742static void * wpa_driver_nl80211_init(void *ctx, const char *ifname,
3743 void *global_priv)
3744{
3745 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003746 struct rfkill_config *rcfg;
3747 struct i802_bss *bss;
3748
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003749 if (global_priv == NULL)
3750 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003751 drv = os_zalloc(sizeof(*drv));
3752 if (drv == NULL)
3753 return NULL;
3754 drv->global = global_priv;
3755 drv->ctx = ctx;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003756
3757 drv->first_bss = os_zalloc(sizeof(*drv->first_bss));
3758 if (!drv->first_bss) {
3759 os_free(drv);
3760 return NULL;
3761 }
3762 bss = drv->first_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003763 bss->drv = drv;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003764 bss->ctx = ctx;
3765
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003766 os_strlcpy(bss->ifname, ifname, sizeof(bss->ifname));
3767 drv->monitor_ifidx = -1;
3768 drv->monitor_sock = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003769 drv->eapol_tx_sock = -1;
3770 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003771
3772 if (wpa_driver_nl80211_init_nl(drv)) {
3773 os_free(drv);
3774 return NULL;
3775 }
3776
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003777 if (nl80211_init_bss(bss))
3778 goto failed;
3779
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003780 rcfg = os_zalloc(sizeof(*rcfg));
3781 if (rcfg == NULL)
3782 goto failed;
3783 rcfg->ctx = drv;
3784 os_strlcpy(rcfg->ifname, ifname, sizeof(rcfg->ifname));
3785 rcfg->blocked_cb = wpa_driver_nl80211_rfkill_blocked;
3786 rcfg->unblocked_cb = wpa_driver_nl80211_rfkill_unblocked;
3787 drv->rfkill = rfkill_init(rcfg);
3788 if (drv->rfkill == NULL) {
3789 wpa_printf(MSG_DEBUG, "nl80211: RFKILL status not available");
3790 os_free(rcfg);
3791 }
3792
3793 if (wpa_driver_nl80211_finish_drv_init(drv))
3794 goto failed;
3795
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003796 drv->eapol_tx_sock = socket(PF_PACKET, SOCK_DGRAM, 0);
3797 if (drv->eapol_tx_sock < 0)
3798 goto failed;
3799
3800 if (drv->data_tx_status) {
3801 int enabled = 1;
3802
3803 if (setsockopt(drv->eapol_tx_sock, SOL_SOCKET, SO_WIFI_STATUS,
3804 &enabled, sizeof(enabled)) < 0) {
3805 wpa_printf(MSG_DEBUG,
3806 "nl80211: wifi status sockopt failed\n");
3807 drv->data_tx_status = 0;
3808 if (!drv->use_monitor)
3809 drv->capa.flags &=
3810 ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
3811 } else {
3812 eloop_register_read_sock(drv->eapol_tx_sock,
3813 wpa_driver_nl80211_handle_eapol_tx_status,
3814 drv, NULL);
3815 }
3816 }
3817
3818 if (drv->global) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003819 dl_list_add(&drv->global->interfaces, &drv->list);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003820 drv->in_interface_list = 1;
3821 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003822
3823 return bss;
3824
3825failed:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003826 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003827 return NULL;
3828}
3829
3830
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003831static int nl80211_register_frame(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003832 struct nl_handle *nl_handle,
3833 u16 type, const u8 *match, size_t match_len)
3834{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003835 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003836 struct nl_msg *msg;
3837 int ret = -1;
3838
3839 msg = nlmsg_alloc();
3840 if (!msg)
3841 return -1;
3842
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003843 wpa_printf(MSG_DEBUG, "nl80211: Register frame type=0x%x nl_handle=%p",
3844 type, nl_handle);
3845 wpa_hexdump(MSG_DEBUG, "nl80211: Register frame match",
3846 match, match_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003847
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003848 nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_ACTION);
3849
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003850 if (nl80211_set_iface_id(msg, bss) < 0)
3851 goto nla_put_failure;
3852
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003853 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE, type);
3854 NLA_PUT(msg, NL80211_ATTR_FRAME_MATCH, match_len, match);
3855
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003856 ret = send_and_recv(drv->global, nl_handle, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003857 msg = NULL;
3858 if (ret) {
3859 wpa_printf(MSG_DEBUG, "nl80211: Register frame command "
3860 "failed (type=%u): ret=%d (%s)",
3861 type, ret, strerror(-ret));
3862 wpa_hexdump(MSG_DEBUG, "nl80211: Register frame match",
3863 match, match_len);
3864 goto nla_put_failure;
3865 }
3866 ret = 0;
3867nla_put_failure:
3868 nlmsg_free(msg);
3869 return ret;
3870}
3871
3872
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003873static int nl80211_alloc_mgmt_handle(struct i802_bss *bss)
3874{
3875 struct wpa_driver_nl80211_data *drv = bss->drv;
3876
3877 if (bss->nl_mgmt) {
3878 wpa_printf(MSG_DEBUG, "nl80211: Mgmt reporting "
3879 "already on! (nl_mgmt=%p)", bss->nl_mgmt);
3880 return -1;
3881 }
3882
3883 bss->nl_mgmt = nl_create_handle(drv->nl_cb, "mgmt");
3884 if (bss->nl_mgmt == NULL)
3885 return -1;
3886
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003887 return 0;
3888}
3889
3890
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003891static void nl80211_mgmt_handle_register_eloop(struct i802_bss *bss)
3892{
3893 nl80211_register_eloop_read(&bss->nl_mgmt,
3894 wpa_driver_nl80211_event_receive,
3895 bss->nl_cb);
3896}
3897
3898
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003899static int nl80211_register_action_frame(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003900 const u8 *match, size_t match_len)
3901{
3902 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_ACTION << 4);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003903 return nl80211_register_frame(bss, bss->nl_mgmt,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003904 type, match, match_len);
3905}
3906
3907
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003908static int nl80211_mgmt_subscribe_non_ap(struct i802_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003909{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003910 struct wpa_driver_nl80211_data *drv = bss->drv;
3911
3912 if (nl80211_alloc_mgmt_handle(bss))
3913 return -1;
3914 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with non-AP "
3915 "handle %p", bss->nl_mgmt);
3916
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003917 if (drv->nlmode == NL80211_IFTYPE_ADHOC) {
3918 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_AUTH << 4);
3919
3920 /* register for any AUTH message */
3921 nl80211_register_frame(bss, bss->nl_mgmt, type, NULL, 0);
3922 }
3923
Dmitry Shmidt051af732013-10-22 13:52:46 -07003924#ifdef CONFIG_INTERWORKING
3925 /* QoS Map Configure */
3926 if (nl80211_register_action_frame(bss, (u8 *) "\x01\x04", 2) < 0)
3927 return -1;
3928#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003929#if defined(CONFIG_P2P) || defined(CONFIG_INTERWORKING)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003930 /* GAS Initial Request */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003931 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0a", 2) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003932 return -1;
3933 /* GAS Initial Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003934 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0b", 2) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003935 return -1;
3936 /* GAS Comeback Request */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003937 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0c", 2) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003938 return -1;
3939 /* GAS Comeback Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003940 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0d", 2) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003941 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003942#endif /* CONFIG_P2P || CONFIG_INTERWORKING */
3943#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003944 /* P2P Public Action */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003945 if (nl80211_register_action_frame(bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003946 (u8 *) "\x04\x09\x50\x6f\x9a\x09",
3947 6) < 0)
3948 return -1;
3949 /* P2P Action */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003950 if (nl80211_register_action_frame(bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003951 (u8 *) "\x7f\x50\x6f\x9a\x09",
3952 5) < 0)
3953 return -1;
3954#endif /* CONFIG_P2P */
3955#ifdef CONFIG_IEEE80211W
3956 /* SA Query Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003957 if (nl80211_register_action_frame(bss, (u8 *) "\x08\x01", 2) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003958 return -1;
3959#endif /* CONFIG_IEEE80211W */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003960#ifdef CONFIG_TDLS
3961 if ((drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT)) {
3962 /* TDLS Discovery Response */
3963 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0e", 2) <
3964 0)
3965 return -1;
3966 }
3967#endif /* CONFIG_TDLS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003968
3969 /* FT Action frames */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003970 if (nl80211_register_action_frame(bss, (u8 *) "\x06", 1) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003971 return -1;
3972 else
3973 drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_FT |
3974 WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK;
3975
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003976 /* WNM - BSS Transition Management Request */
3977 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x07", 2) < 0)
3978 return -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003979 /* WNM-Sleep Mode Response */
3980 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x11", 2) < 0)
3981 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003982
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003983 nl80211_mgmt_handle_register_eloop(bss);
3984
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003985 return 0;
3986}
3987
3988
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003989static int nl80211_register_spurious_class3(struct i802_bss *bss)
3990{
3991 struct wpa_driver_nl80211_data *drv = bss->drv;
3992 struct nl_msg *msg;
3993 int ret = -1;
3994
3995 msg = nlmsg_alloc();
3996 if (!msg)
3997 return -1;
3998
3999 nl80211_cmd(drv, msg, 0, NL80211_CMD_UNEXPECTED_FRAME);
4000
4001 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
4002
4003 ret = send_and_recv(drv->global, bss->nl_mgmt, msg, NULL, NULL);
4004 msg = NULL;
4005 if (ret) {
4006 wpa_printf(MSG_DEBUG, "nl80211: Register spurious class3 "
4007 "failed: ret=%d (%s)",
4008 ret, strerror(-ret));
4009 goto nla_put_failure;
4010 }
4011 ret = 0;
4012nla_put_failure:
4013 nlmsg_free(msg);
4014 return ret;
4015}
4016
4017
4018static int nl80211_mgmt_subscribe_ap(struct i802_bss *bss)
4019{
4020 static const int stypes[] = {
4021 WLAN_FC_STYPE_AUTH,
4022 WLAN_FC_STYPE_ASSOC_REQ,
4023 WLAN_FC_STYPE_REASSOC_REQ,
4024 WLAN_FC_STYPE_DISASSOC,
4025 WLAN_FC_STYPE_DEAUTH,
4026 WLAN_FC_STYPE_ACTION,
4027 WLAN_FC_STYPE_PROBE_REQ,
4028/* Beacon doesn't work as mac80211 doesn't currently allow
4029 * it, but it wouldn't really be the right thing anyway as
4030 * it isn't per interface ... maybe just dump the scan
4031 * results periodically for OLBC?
4032 */
4033// WLAN_FC_STYPE_BEACON,
4034 };
4035 unsigned int i;
4036
4037 if (nl80211_alloc_mgmt_handle(bss))
4038 return -1;
4039 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
4040 "handle %p", bss->nl_mgmt);
4041
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004042 for (i = 0; i < ARRAY_SIZE(stypes); i++) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004043 if (nl80211_register_frame(bss, bss->nl_mgmt,
4044 (WLAN_FC_TYPE_MGMT << 2) |
4045 (stypes[i] << 4),
4046 NULL, 0) < 0) {
4047 goto out_err;
4048 }
4049 }
4050
4051 if (nl80211_register_spurious_class3(bss))
4052 goto out_err;
4053
4054 if (nl80211_get_wiphy_data_ap(bss) == NULL)
4055 goto out_err;
4056
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004057 nl80211_mgmt_handle_register_eloop(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004058 return 0;
4059
4060out_err:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004061 nl_destroy_handles(&bss->nl_mgmt);
4062 return -1;
4063}
4064
4065
4066static int nl80211_mgmt_subscribe_ap_dev_sme(struct i802_bss *bss)
4067{
4068 if (nl80211_alloc_mgmt_handle(bss))
4069 return -1;
4070 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
4071 "handle %p (device SME)", bss->nl_mgmt);
4072
4073 if (nl80211_register_frame(bss, bss->nl_mgmt,
4074 (WLAN_FC_TYPE_MGMT << 2) |
4075 (WLAN_FC_STYPE_ACTION << 4),
4076 NULL, 0) < 0)
4077 goto out_err;
4078
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004079 nl80211_mgmt_handle_register_eloop(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004080 return 0;
4081
4082out_err:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004083 nl_destroy_handles(&bss->nl_mgmt);
4084 return -1;
4085}
4086
4087
4088static void nl80211_mgmt_unsubscribe(struct i802_bss *bss, const char *reason)
4089{
4090 if (bss->nl_mgmt == NULL)
4091 return;
4092 wpa_printf(MSG_DEBUG, "nl80211: Unsubscribe mgmt frames handle %p "
4093 "(%s)", bss->nl_mgmt, reason);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004094 nl80211_destroy_eloop_handle(&bss->nl_mgmt);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004095
4096 nl80211_put_wiphy_data_ap(bss);
4097}
4098
4099
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004100static void wpa_driver_nl80211_send_rfkill(void *eloop_ctx, void *timeout_ctx)
4101{
4102 wpa_supplicant_event(timeout_ctx, EVENT_INTERFACE_DISABLED, NULL);
4103}
4104
4105
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004106static void nl80211_del_p2pdev(struct i802_bss *bss)
4107{
4108 struct wpa_driver_nl80211_data *drv = bss->drv;
4109 struct nl_msg *msg;
4110 int ret;
4111
4112 msg = nlmsg_alloc();
4113 if (!msg)
4114 return;
4115
4116 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_INTERFACE);
4117 NLA_PUT_U64(msg, NL80211_ATTR_WDEV, bss->wdev_id);
4118
4119 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4120 msg = NULL;
4121
4122 wpa_printf(MSG_DEBUG, "nl80211: Delete P2P Device %s (0x%llx): %s",
4123 bss->ifname, (long long unsigned int) bss->wdev_id,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004124 strerror(-ret));
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004125
4126nla_put_failure:
4127 nlmsg_free(msg);
4128}
4129
4130
4131static int nl80211_set_p2pdev(struct i802_bss *bss, int start)
4132{
4133 struct wpa_driver_nl80211_data *drv = bss->drv;
4134 struct nl_msg *msg;
4135 int ret = -1;
4136
4137 msg = nlmsg_alloc();
4138 if (!msg)
4139 return -1;
4140
4141 if (start)
4142 nl80211_cmd(drv, msg, 0, NL80211_CMD_START_P2P_DEVICE);
4143 else
4144 nl80211_cmd(drv, msg, 0, NL80211_CMD_STOP_P2P_DEVICE);
4145
4146 NLA_PUT_U64(msg, NL80211_ATTR_WDEV, bss->wdev_id);
4147
4148 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4149 msg = NULL;
4150
4151 wpa_printf(MSG_DEBUG, "nl80211: %s P2P Device %s (0x%llx): %s",
4152 start ? "Start" : "Stop",
4153 bss->ifname, (long long unsigned int) bss->wdev_id,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004154 strerror(-ret));
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004155
4156nla_put_failure:
4157 nlmsg_free(msg);
4158 return ret;
4159}
4160
4161
4162static int i802_set_iface_flags(struct i802_bss *bss, int up)
4163{
4164 enum nl80211_iftype nlmode;
4165
4166 nlmode = nl80211_get_ifmode(bss);
4167 if (nlmode != NL80211_IFTYPE_P2P_DEVICE) {
4168 return linux_set_iface_flags(bss->drv->global->ioctl_sock,
4169 bss->ifname, up);
4170 }
4171
4172 /* P2P Device has start/stop which is equivalent */
4173 return nl80211_set_p2pdev(bss, up);
4174}
4175
4176
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004177static int
4178wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv)
4179{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004180#ifndef HOSTAPD
4181 enum nl80211_iftype nlmode = NL80211_IFTYPE_STATION;
4182#endif /* HOSTAPD */
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004183 struct i802_bss *bss = drv->first_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004184 int send_rfkill_event = 0;
4185
4186 drv->ifindex = if_nametoindex(bss->ifname);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004187 bss->ifindex = drv->ifindex;
4188 bss->wdev_id = drv->global->if_add_wdevid;
4189 bss->wdev_id_set = drv->global->if_add_wdevid_set;
4190
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004191 bss->if_dynamic = drv->ifindex == drv->global->if_add_ifindex;
4192 bss->if_dynamic = bss->if_dynamic || drv->global->if_add_wdevid_set;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004193 drv->global->if_add_wdevid_set = 0;
4194
4195 if (wpa_driver_nl80211_capa(drv))
4196 return -1;
4197
4198 wpa_printf(MSG_DEBUG, "nl80211: interface %s in phy %s",
4199 bss->ifname, drv->phyname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004200
4201#ifndef HOSTAPD
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004202 if (bss->if_dynamic)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004203 nlmode = nl80211_get_ifmode(bss);
4204
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004205 /*
4206 * Make sure the interface starts up in station mode unless this is a
4207 * dynamically added interface (e.g., P2P) that was already configured
4208 * with proper iftype.
4209 */
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004210 if (wpa_driver_nl80211_set_mode(bss, nlmode) < 0) {
4211 wpa_printf(MSG_ERROR, "nl80211: Could not configure driver to use managed mode");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004212 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004213 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004214 drv->nlmode = nlmode;
4215
4216 if (nlmode == NL80211_IFTYPE_P2P_DEVICE) {
4217 int ret = nl80211_set_p2pdev(bss, 1);
4218 if (ret < 0)
4219 wpa_printf(MSG_ERROR, "nl80211: Could not start P2P device");
4220 nl80211_get_macaddr(bss);
4221 return ret;
4222 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004223
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004224 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004225 if (rfkill_is_blocked(drv->rfkill)) {
4226 wpa_printf(MSG_DEBUG, "nl80211: Could not yet enable "
4227 "interface '%s' due to rfkill",
4228 bss->ifname);
4229 drv->if_disabled = 1;
4230 send_rfkill_event = 1;
4231 } else {
4232 wpa_printf(MSG_ERROR, "nl80211: Could not set "
4233 "interface '%s' UP", bss->ifname);
4234 return -1;
4235 }
4236 }
4237
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004238 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004239 1, IF_OPER_DORMANT);
4240#endif /* HOSTAPD */
4241
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004242 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
4243 bss->addr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004244 return -1;
4245
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004246 if (send_rfkill_event) {
4247 eloop_register_timeout(0, 0, wpa_driver_nl80211_send_rfkill,
4248 drv, drv->ctx);
4249 }
4250
4251 return 0;
4252}
4253
4254
4255static int wpa_driver_nl80211_del_beacon(struct wpa_driver_nl80211_data *drv)
4256{
4257 struct nl_msg *msg;
4258
4259 msg = nlmsg_alloc();
4260 if (!msg)
4261 return -ENOMEM;
4262
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004263 wpa_printf(MSG_DEBUG, "nl80211: Remove beacon (ifindex=%d)",
4264 drv->ifindex);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004265 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_BEACON);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004266 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
4267
4268 return send_and_recv_msgs(drv, msg, NULL, NULL);
4269 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004270 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004271 return -ENOBUFS;
4272}
4273
4274
4275/**
4276 * wpa_driver_nl80211_deinit - Deinitialize nl80211 driver interface
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004277 * @bss: Pointer to private nl80211 data from wpa_driver_nl80211_init()
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004278 *
4279 * Shut down driver interface and processing of driver events. Free
4280 * private data buffer if one was allocated in wpa_driver_nl80211_init().
4281 */
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004282static void wpa_driver_nl80211_deinit(struct i802_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004283{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004284 struct wpa_driver_nl80211_data *drv = bss->drv;
4285
Dmitry Shmidt04949592012-07-19 12:16:46 -07004286 bss->in_deinit = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004287 if (drv->data_tx_status)
4288 eloop_unregister_read_sock(drv->eapol_tx_sock);
4289 if (drv->eapol_tx_sock >= 0)
4290 close(drv->eapol_tx_sock);
4291
4292 if (bss->nl_preq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004293 wpa_driver_nl80211_probe_req_report(bss, 0);
4294 if (bss->added_if_into_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004295 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
4296 bss->ifname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004297 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
4298 "interface %s from bridge %s: %s",
4299 bss->ifname, bss->brname, strerror(errno));
4300 }
4301 if (bss->added_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004302 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004303 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
4304 "bridge %s: %s",
4305 bss->brname, strerror(errno));
4306 }
4307
4308 nl80211_remove_monitor_interface(drv);
4309
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004310 if (is_ap_interface(drv->nlmode))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004311 wpa_driver_nl80211_del_beacon(drv);
4312
4313#ifdef HOSTAPD
4314 if (drv->last_freq_ht) {
4315 /* Clear HT flags from the driver */
4316 struct hostapd_freq_params freq;
4317 os_memset(&freq, 0, sizeof(freq));
4318 freq.freq = drv->last_freq;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004319 wpa_driver_nl80211_set_freq(bss, &freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004320 }
4321
4322 if (drv->eapol_sock >= 0) {
4323 eloop_unregister_read_sock(drv->eapol_sock);
4324 close(drv->eapol_sock);
4325 }
4326
4327 if (drv->if_indices != drv->default_if_indices)
4328 os_free(drv->if_indices);
4329#endif /* HOSTAPD */
4330
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004331 if (drv->disabled_11b_rates)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004332 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
4333
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004334 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, 0,
4335 IF_OPER_UP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004336 rfkill_deinit(drv->rfkill);
4337
4338 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
4339
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004340 (void) i802_set_iface_flags(bss, 0);
4341 if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE) {
4342 wpa_driver_nl80211_set_mode(bss, NL80211_IFTYPE_STATION);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004343 nl80211_mgmt_unsubscribe(bss, "deinit");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004344 } else {
4345 nl80211_mgmt_unsubscribe(bss, "deinit");
4346 nl80211_del_p2pdev(bss);
4347 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004348 nl_cb_put(drv->nl_cb);
4349
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004350 nl80211_destroy_bss(drv->first_bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004351
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004352 os_free(drv->filter_ssids);
4353
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004354 os_free(drv->auth_ie);
4355
4356 if (drv->in_interface_list)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004357 dl_list_del(&drv->list);
4358
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004359 os_free(drv->extended_capa);
4360 os_free(drv->extended_capa_mask);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004361 os_free(drv->first_bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004362 os_free(drv);
4363}
4364
4365
4366/**
4367 * wpa_driver_nl80211_scan_timeout - Scan timeout to report scan completion
4368 * @eloop_ctx: Driver private data
4369 * @timeout_ctx: ctx argument given to wpa_driver_nl80211_init()
4370 *
4371 * This function can be used as registered timeout when starting a scan to
4372 * generate a scan completed event if the driver does not report this.
4373 */
4374static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx, void *timeout_ctx)
4375{
4376 struct wpa_driver_nl80211_data *drv = eloop_ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004377 if (drv->ap_scan_as_station != NL80211_IFTYPE_UNSPECIFIED) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004378 wpa_driver_nl80211_set_mode(drv->first_bss,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004379 drv->ap_scan_as_station);
4380 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004381 }
4382 wpa_printf(MSG_DEBUG, "Scan timeout - try to get results");
4383 wpa_supplicant_event(timeout_ctx, EVENT_SCAN_RESULTS, NULL);
4384}
4385
4386
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004387static struct nl_msg *
4388nl80211_scan_common(struct wpa_driver_nl80211_data *drv, u8 cmd,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004389 struct wpa_driver_scan_params *params, u64 *wdev_id)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004390{
4391 struct nl_msg *msg;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004392 size_t i;
4393
4394 msg = nlmsg_alloc();
4395 if (!msg)
4396 return NULL;
4397
4398 nl80211_cmd(drv, msg, 0, cmd);
4399
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004400 if (!wdev_id)
4401 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
4402 else
4403 NLA_PUT_U64(msg, NL80211_ATTR_WDEV, *wdev_id);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004404
4405 if (params->num_ssids) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004406 struct nlattr *ssids;
4407
4408 ssids = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004409 if (ssids == NULL)
4410 goto fail;
4411 for (i = 0; i < params->num_ssids; i++) {
4412 wpa_hexdump_ascii(MSG_MSGDUMP, "nl80211: Scan SSID",
4413 params->ssids[i].ssid,
4414 params->ssids[i].ssid_len);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004415 if (nla_put(msg, i + 1, params->ssids[i].ssid_len,
4416 params->ssids[i].ssid) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004417 goto fail;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004418 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004419 nla_nest_end(msg, ssids);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004420 }
4421
4422 if (params->extra_ies) {
4423 wpa_hexdump(MSG_MSGDUMP, "nl80211: Scan extra IEs",
4424 params->extra_ies, params->extra_ies_len);
4425 if (nla_put(msg, NL80211_ATTR_IE, params->extra_ies_len,
4426 params->extra_ies) < 0)
4427 goto fail;
4428 }
4429
4430 if (params->freqs) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004431 struct nlattr *freqs;
4432 freqs = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004433 if (freqs == NULL)
4434 goto fail;
4435 for (i = 0; params->freqs[i]; i++) {
4436 wpa_printf(MSG_MSGDUMP, "nl80211: Scan frequency %u "
4437 "MHz", params->freqs[i]);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004438 if (nla_put_u32(msg, i + 1, params->freqs[i]) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004439 goto fail;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004440 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004441 nla_nest_end(msg, freqs);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004442 }
4443
4444 os_free(drv->filter_ssids);
4445 drv->filter_ssids = params->filter_ssids;
4446 params->filter_ssids = NULL;
4447 drv->num_filter_ssids = params->num_filter_ssids;
4448
4449 return msg;
4450
4451fail:
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004452nla_put_failure:
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004453 nlmsg_free(msg);
4454 return NULL;
4455}
4456
4457
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004458/**
4459 * wpa_driver_nl80211_scan - Request the driver to initiate scan
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004460 * @bss: Pointer to private driver data from wpa_driver_nl80211_init()
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004461 * @params: Scan parameters
4462 * Returns: 0 on success, -1 on failure
4463 */
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004464static int wpa_driver_nl80211_scan(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004465 struct wpa_driver_scan_params *params)
4466{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004467 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004468 int ret = -1, timeout;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004469 struct nl_msg *msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004470
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004471 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: scan request");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004472 drv->scan_for_auth = 0;
4473
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004474 msg = nl80211_scan_common(drv, NL80211_CMD_TRIGGER_SCAN, params,
4475 bss->wdev_id_set ? &bss->wdev_id : NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004476 if (!msg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004477 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004478
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004479 if (params->p2p_probe) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004480 struct nlattr *rates;
4481
Dmitry Shmidt04949592012-07-19 12:16:46 -07004482 wpa_printf(MSG_DEBUG, "nl80211: P2P probe - mask SuppRates");
4483
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004484 rates = nla_nest_start(msg, NL80211_ATTR_SCAN_SUPP_RATES);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004485 if (rates == NULL)
4486 goto nla_put_failure;
4487
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004488 /*
4489 * Remove 2.4 GHz rates 1, 2, 5.5, 11 Mbps from supported rates
4490 * by masking out everything else apart from the OFDM rates 6,
4491 * 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS rates. All 5 GHz
4492 * rates are left enabled.
4493 */
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004494 NLA_PUT(msg, NL80211_BAND_2GHZ, 8,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004495 "\x0c\x12\x18\x24\x30\x48\x60\x6c");
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004496 nla_nest_end(msg, rates);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004497
4498 NLA_PUT_FLAG(msg, NL80211_ATTR_TX_NO_CCK_RATE);
4499 }
4500
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004501 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4502 msg = NULL;
4503 if (ret) {
4504 wpa_printf(MSG_DEBUG, "nl80211: Scan trigger failed: ret=%d "
4505 "(%s)", ret, strerror(-ret));
4506#ifdef HOSTAPD
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004507 if (is_ap_interface(drv->nlmode)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004508 /*
4509 * mac80211 does not allow scan requests in AP mode, so
4510 * try to do this in station mode.
4511 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004512 if (wpa_driver_nl80211_set_mode(
4513 bss, NL80211_IFTYPE_STATION))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004514 goto nla_put_failure;
4515
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004516 if (wpa_driver_nl80211_scan(bss, params)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004517 wpa_driver_nl80211_set_mode(bss, drv->nlmode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004518 goto nla_put_failure;
4519 }
4520
4521 /* Restore AP mode when processing scan results */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004522 drv->ap_scan_as_station = drv->nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004523 ret = 0;
4524 } else
4525 goto nla_put_failure;
4526#else /* HOSTAPD */
4527 goto nla_put_failure;
4528#endif /* HOSTAPD */
4529 }
4530
Dmitry Shmidt56052862013-10-04 10:23:25 -07004531 drv->scan_state = SCAN_REQUESTED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004532 /* Not all drivers generate "scan completed" wireless event, so try to
4533 * read results after a timeout. */
4534 timeout = 10;
4535 if (drv->scan_complete_events) {
4536 /*
4537 * The driver seems to deliver events to notify when scan is
4538 * complete, so use longer timeout to avoid race conditions
4539 * with scanning and following association request.
4540 */
4541 timeout = 30;
4542 }
4543 wpa_printf(MSG_DEBUG, "Scan requested (ret=%d) - scan timeout %d "
4544 "seconds", ret, timeout);
4545 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
4546 eloop_register_timeout(timeout, 0, wpa_driver_nl80211_scan_timeout,
4547 drv, drv->ctx);
4548
4549nla_put_failure:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004550 nlmsg_free(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004551 return ret;
4552}
4553
4554
4555/**
4556 * wpa_driver_nl80211_sched_scan - Initiate a scheduled scan
4557 * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
4558 * @params: Scan parameters
4559 * @interval: Interval between scan cycles in milliseconds
4560 * Returns: 0 on success, -1 on failure or if not supported
4561 */
4562static int wpa_driver_nl80211_sched_scan(void *priv,
4563 struct wpa_driver_scan_params *params,
4564 u32 interval)
4565{
4566 struct i802_bss *bss = priv;
4567 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004568 int ret = -1;
4569 struct nl_msg *msg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004570 size_t i;
4571
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004572 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: sched_scan request");
4573
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004574#ifdef ANDROID
4575 if (!drv->capa.sched_scan_supported)
4576 return android_pno_start(bss, params);
4577#endif /* ANDROID */
4578
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004579 msg = nl80211_scan_common(drv, NL80211_CMD_START_SCHED_SCAN, params,
4580 bss->wdev_id_set ? &bss->wdev_id : NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004581 if (!msg)
4582 goto nla_put_failure;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004583
4584 NLA_PUT_U32(msg, NL80211_ATTR_SCHED_SCAN_INTERVAL, interval);
4585
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004586 if ((drv->num_filter_ssids &&
4587 (int) drv->num_filter_ssids <= drv->capa.max_match_sets) ||
4588 params->filter_rssi) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004589 struct nlattr *match_sets;
4590 match_sets = nla_nest_start(msg, NL80211_ATTR_SCHED_SCAN_MATCH);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004591 if (match_sets == NULL)
4592 goto nla_put_failure;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004593
4594 for (i = 0; i < drv->num_filter_ssids; i++) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004595 struct nlattr *match_set_ssid;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004596 wpa_hexdump_ascii(MSG_MSGDUMP,
4597 "nl80211: Sched scan filter SSID",
4598 drv->filter_ssids[i].ssid,
4599 drv->filter_ssids[i].ssid_len);
4600
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004601 match_set_ssid = nla_nest_start(msg, i + 1);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004602 if (match_set_ssid == NULL)
4603 goto nla_put_failure;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004604 NLA_PUT(msg, NL80211_ATTR_SCHED_SCAN_MATCH_SSID,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004605 drv->filter_ssids[i].ssid_len,
4606 drv->filter_ssids[i].ssid);
4607
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004608 nla_nest_end(msg, match_set_ssid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004609 }
4610
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004611 if (params->filter_rssi) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004612 struct nlattr *match_set_rssi;
4613 match_set_rssi = nla_nest_start(msg, 0);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004614 if (match_set_rssi == NULL)
4615 goto nla_put_failure;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004616 NLA_PUT_U32(msg, NL80211_SCHED_SCAN_MATCH_ATTR_RSSI,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004617 params->filter_rssi);
4618 wpa_printf(MSG_MSGDUMP,
4619 "nl80211: Sched scan RSSI filter %d dBm",
4620 params->filter_rssi);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004621 nla_nest_end(msg, match_set_rssi);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004622 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004623
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004624 nla_nest_end(msg, match_sets);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004625 }
4626
4627 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4628
4629 /* TODO: if we get an error here, we should fall back to normal scan */
4630
4631 msg = NULL;
4632 if (ret) {
4633 wpa_printf(MSG_DEBUG, "nl80211: Sched scan start failed: "
4634 "ret=%d (%s)", ret, strerror(-ret));
4635 goto nla_put_failure;
4636 }
4637
4638 wpa_printf(MSG_DEBUG, "nl80211: Sched scan requested (ret=%d) - "
4639 "scan interval %d msec", ret, interval);
4640
4641nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004642 nlmsg_free(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004643 return ret;
4644}
4645
4646
4647/**
4648 * wpa_driver_nl80211_stop_sched_scan - Stop a scheduled scan
4649 * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
4650 * Returns: 0 on success, -1 on failure or if not supported
4651 */
4652static int wpa_driver_nl80211_stop_sched_scan(void *priv)
4653{
4654 struct i802_bss *bss = priv;
4655 struct wpa_driver_nl80211_data *drv = bss->drv;
4656 int ret = 0;
4657 struct nl_msg *msg;
4658
4659#ifdef ANDROID
4660 if (!drv->capa.sched_scan_supported)
4661 return android_pno_stop(bss);
4662#endif /* ANDROID */
4663
4664 msg = nlmsg_alloc();
4665 if (!msg)
4666 return -1;
4667
4668 nl80211_cmd(drv, msg, 0, NL80211_CMD_STOP_SCHED_SCAN);
4669
4670 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
4671
4672 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4673 msg = NULL;
4674 if (ret) {
4675 wpa_printf(MSG_DEBUG, "nl80211: Sched scan stop failed: "
4676 "ret=%d (%s)", ret, strerror(-ret));
4677 goto nla_put_failure;
4678 }
4679
4680 wpa_printf(MSG_DEBUG, "nl80211: Sched scan stop sent (ret=%d)", ret);
4681
4682nla_put_failure:
4683 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004684 return ret;
4685}
4686
4687
4688static const u8 * nl80211_get_ie(const u8 *ies, size_t ies_len, u8 ie)
4689{
4690 const u8 *end, *pos;
4691
4692 if (ies == NULL)
4693 return NULL;
4694
4695 pos = ies;
4696 end = ies + ies_len;
4697
4698 while (pos + 1 < end) {
4699 if (pos + 2 + pos[1] > end)
4700 break;
4701 if (pos[0] == ie)
4702 return pos;
4703 pos += 2 + pos[1];
4704 }
4705
4706 return NULL;
4707}
4708
4709
4710static int nl80211_scan_filtered(struct wpa_driver_nl80211_data *drv,
4711 const u8 *ie, size_t ie_len)
4712{
4713 const u8 *ssid;
4714 size_t i;
4715
4716 if (drv->filter_ssids == NULL)
4717 return 0;
4718
4719 ssid = nl80211_get_ie(ie, ie_len, WLAN_EID_SSID);
4720 if (ssid == NULL)
4721 return 1;
4722
4723 for (i = 0; i < drv->num_filter_ssids; i++) {
4724 if (ssid[1] == drv->filter_ssids[i].ssid_len &&
4725 os_memcmp(ssid + 2, drv->filter_ssids[i].ssid, ssid[1]) ==
4726 0)
4727 return 0;
4728 }
4729
4730 return 1;
4731}
4732
4733
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004734static int bss_info_handler(struct nl_msg *msg, void *arg)
4735{
4736 struct nlattr *tb[NL80211_ATTR_MAX + 1];
4737 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
4738 struct nlattr *bss[NL80211_BSS_MAX + 1];
4739 static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
4740 [NL80211_BSS_BSSID] = { .type = NLA_UNSPEC },
4741 [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
4742 [NL80211_BSS_TSF] = { .type = NLA_U64 },
4743 [NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 },
4744 [NL80211_BSS_CAPABILITY] = { .type = NLA_U16 },
4745 [NL80211_BSS_INFORMATION_ELEMENTS] = { .type = NLA_UNSPEC },
4746 [NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 },
4747 [NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 },
4748 [NL80211_BSS_STATUS] = { .type = NLA_U32 },
4749 [NL80211_BSS_SEEN_MS_AGO] = { .type = NLA_U32 },
4750 [NL80211_BSS_BEACON_IES] = { .type = NLA_UNSPEC },
4751 };
4752 struct nl80211_bss_info_arg *_arg = arg;
4753 struct wpa_scan_results *res = _arg->res;
4754 struct wpa_scan_res **tmp;
4755 struct wpa_scan_res *r;
4756 const u8 *ie, *beacon_ie;
4757 size_t ie_len, beacon_ie_len;
4758 u8 *pos;
Jouni Malinen87fd2792011-05-16 18:35:42 +03004759 size_t i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004760
4761 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
4762 genlmsg_attrlen(gnlh, 0), NULL);
4763 if (!tb[NL80211_ATTR_BSS])
4764 return NL_SKIP;
4765 if (nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS],
4766 bss_policy))
4767 return NL_SKIP;
Jouni Malinen87fd2792011-05-16 18:35:42 +03004768 if (bss[NL80211_BSS_STATUS]) {
4769 enum nl80211_bss_status status;
4770 status = nla_get_u32(bss[NL80211_BSS_STATUS]);
4771 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
4772 bss[NL80211_BSS_FREQUENCY]) {
4773 _arg->assoc_freq =
4774 nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
4775 wpa_printf(MSG_DEBUG, "nl80211: Associated on %u MHz",
4776 _arg->assoc_freq);
4777 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004778 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
4779 bss[NL80211_BSS_BSSID]) {
4780 os_memcpy(_arg->assoc_bssid,
4781 nla_data(bss[NL80211_BSS_BSSID]), ETH_ALEN);
4782 wpa_printf(MSG_DEBUG, "nl80211: Associated with "
4783 MACSTR, MAC2STR(_arg->assoc_bssid));
4784 }
Jouni Malinen87fd2792011-05-16 18:35:42 +03004785 }
4786 if (!res)
4787 return NL_SKIP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004788 if (bss[NL80211_BSS_INFORMATION_ELEMENTS]) {
4789 ie = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
4790 ie_len = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
4791 } else {
4792 ie = NULL;
4793 ie_len = 0;
4794 }
4795 if (bss[NL80211_BSS_BEACON_IES]) {
4796 beacon_ie = nla_data(bss[NL80211_BSS_BEACON_IES]);
4797 beacon_ie_len = nla_len(bss[NL80211_BSS_BEACON_IES]);
4798 } else {
4799 beacon_ie = NULL;
4800 beacon_ie_len = 0;
4801 }
4802
4803 if (nl80211_scan_filtered(_arg->drv, ie ? ie : beacon_ie,
4804 ie ? ie_len : beacon_ie_len))
4805 return NL_SKIP;
4806
4807 r = os_zalloc(sizeof(*r) + ie_len + beacon_ie_len);
4808 if (r == NULL)
4809 return NL_SKIP;
4810 if (bss[NL80211_BSS_BSSID])
4811 os_memcpy(r->bssid, nla_data(bss[NL80211_BSS_BSSID]),
4812 ETH_ALEN);
4813 if (bss[NL80211_BSS_FREQUENCY])
4814 r->freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
4815 if (bss[NL80211_BSS_BEACON_INTERVAL])
4816 r->beacon_int = nla_get_u16(bss[NL80211_BSS_BEACON_INTERVAL]);
4817 if (bss[NL80211_BSS_CAPABILITY])
4818 r->caps = nla_get_u16(bss[NL80211_BSS_CAPABILITY]);
4819 r->flags |= WPA_SCAN_NOISE_INVALID;
4820 if (bss[NL80211_BSS_SIGNAL_MBM]) {
4821 r->level = nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]);
4822 r->level /= 100; /* mBm to dBm */
4823 r->flags |= WPA_SCAN_LEVEL_DBM | WPA_SCAN_QUAL_INVALID;
4824 } else if (bss[NL80211_BSS_SIGNAL_UNSPEC]) {
4825 r->level = nla_get_u8(bss[NL80211_BSS_SIGNAL_UNSPEC]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004826 r->flags |= WPA_SCAN_QUAL_INVALID;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004827 } else
4828 r->flags |= WPA_SCAN_LEVEL_INVALID | WPA_SCAN_QUAL_INVALID;
4829 if (bss[NL80211_BSS_TSF])
4830 r->tsf = nla_get_u64(bss[NL80211_BSS_TSF]);
4831 if (bss[NL80211_BSS_SEEN_MS_AGO])
4832 r->age = nla_get_u32(bss[NL80211_BSS_SEEN_MS_AGO]);
4833 r->ie_len = ie_len;
4834 pos = (u8 *) (r + 1);
4835 if (ie) {
4836 os_memcpy(pos, ie, ie_len);
4837 pos += ie_len;
4838 }
4839 r->beacon_ie_len = beacon_ie_len;
4840 if (beacon_ie)
4841 os_memcpy(pos, beacon_ie, beacon_ie_len);
4842
4843 if (bss[NL80211_BSS_STATUS]) {
4844 enum nl80211_bss_status status;
4845 status = nla_get_u32(bss[NL80211_BSS_STATUS]);
4846 switch (status) {
4847 case NL80211_BSS_STATUS_AUTHENTICATED:
4848 r->flags |= WPA_SCAN_AUTHENTICATED;
4849 break;
4850 case NL80211_BSS_STATUS_ASSOCIATED:
4851 r->flags |= WPA_SCAN_ASSOCIATED;
4852 break;
4853 default:
4854 break;
4855 }
4856 }
4857
Jouni Malinen87fd2792011-05-16 18:35:42 +03004858 /*
4859 * cfg80211 maintains separate BSS table entries for APs if the same
4860 * BSSID,SSID pair is seen on multiple channels. wpa_supplicant does
4861 * not use frequency as a separate key in the BSS table, so filter out
4862 * duplicated entries. Prefer associated BSS entry in such a case in
4863 * order to get the correct frequency into the BSS table.
4864 */
4865 for (i = 0; i < res->num; i++) {
4866 const u8 *s1, *s2;
4867 if (os_memcmp(res->res[i]->bssid, r->bssid, ETH_ALEN) != 0)
4868 continue;
4869
4870 s1 = nl80211_get_ie((u8 *) (res->res[i] + 1),
4871 res->res[i]->ie_len, WLAN_EID_SSID);
4872 s2 = nl80211_get_ie((u8 *) (r + 1), r->ie_len, WLAN_EID_SSID);
4873 if (s1 == NULL || s2 == NULL || s1[1] != s2[1] ||
4874 os_memcmp(s1, s2, 2 + s1[1]) != 0)
4875 continue;
4876
4877 /* Same BSSID,SSID was already included in scan results */
4878 wpa_printf(MSG_DEBUG, "nl80211: Remove duplicated scan result "
4879 "for " MACSTR, MAC2STR(r->bssid));
4880
4881 if ((r->flags & WPA_SCAN_ASSOCIATED) &&
4882 !(res->res[i]->flags & WPA_SCAN_ASSOCIATED)) {
4883 os_free(res->res[i]);
4884 res->res[i] = r;
4885 } else
4886 os_free(r);
4887 return NL_SKIP;
4888 }
4889
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004890 tmp = os_realloc_array(res->res, res->num + 1,
4891 sizeof(struct wpa_scan_res *));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004892 if (tmp == NULL) {
4893 os_free(r);
4894 return NL_SKIP;
4895 }
4896 tmp[res->num++] = r;
4897 res->res = tmp;
4898
4899 return NL_SKIP;
4900}
4901
4902
4903static void clear_state_mismatch(struct wpa_driver_nl80211_data *drv,
4904 const u8 *addr)
4905{
4906 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
4907 wpa_printf(MSG_DEBUG, "nl80211: Clear possible state "
4908 "mismatch (" MACSTR ")", MAC2STR(addr));
4909 wpa_driver_nl80211_mlme(drv, addr,
4910 NL80211_CMD_DEAUTHENTICATE,
4911 WLAN_REASON_PREV_AUTH_NOT_VALID, 1);
4912 }
4913}
4914
4915
4916static void wpa_driver_nl80211_check_bss_status(
4917 struct wpa_driver_nl80211_data *drv, struct wpa_scan_results *res)
4918{
4919 size_t i;
4920
4921 for (i = 0; i < res->num; i++) {
4922 struct wpa_scan_res *r = res->res[i];
4923 if (r->flags & WPA_SCAN_AUTHENTICATED) {
4924 wpa_printf(MSG_DEBUG, "nl80211: Scan results "
4925 "indicates BSS status with " MACSTR
4926 " as authenticated",
4927 MAC2STR(r->bssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004928 if (is_sta_interface(drv->nlmode) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004929 os_memcmp(r->bssid, drv->bssid, ETH_ALEN) != 0 &&
4930 os_memcmp(r->bssid, drv->auth_bssid, ETH_ALEN) !=
4931 0) {
4932 wpa_printf(MSG_DEBUG, "nl80211: Unknown BSSID"
4933 " in local state (auth=" MACSTR
4934 " assoc=" MACSTR ")",
4935 MAC2STR(drv->auth_bssid),
4936 MAC2STR(drv->bssid));
4937 clear_state_mismatch(drv, r->bssid);
4938 }
4939 }
4940
4941 if (r->flags & WPA_SCAN_ASSOCIATED) {
4942 wpa_printf(MSG_DEBUG, "nl80211: Scan results "
4943 "indicate BSS status with " MACSTR
4944 " as associated",
4945 MAC2STR(r->bssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004946 if (is_sta_interface(drv->nlmode) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004947 !drv->associated) {
4948 wpa_printf(MSG_DEBUG, "nl80211: Local state "
4949 "(not associated) does not match "
4950 "with BSS state");
4951 clear_state_mismatch(drv, r->bssid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004952 } else if (is_sta_interface(drv->nlmode) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004953 os_memcmp(drv->bssid, r->bssid, ETH_ALEN) !=
4954 0) {
4955 wpa_printf(MSG_DEBUG, "nl80211: Local state "
4956 "(associated with " MACSTR ") does "
4957 "not match with BSS state",
4958 MAC2STR(drv->bssid));
4959 clear_state_mismatch(drv, r->bssid);
4960 clear_state_mismatch(drv, drv->bssid);
4961 }
4962 }
4963 }
4964}
4965
4966
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004967static struct wpa_scan_results *
4968nl80211_get_scan_results(struct wpa_driver_nl80211_data *drv)
4969{
4970 struct nl_msg *msg;
4971 struct wpa_scan_results *res;
4972 int ret;
4973 struct nl80211_bss_info_arg arg;
4974
4975 res = os_zalloc(sizeof(*res));
4976 if (res == NULL)
4977 return NULL;
4978 msg = nlmsg_alloc();
4979 if (!msg)
4980 goto nla_put_failure;
4981
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004982 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004983 if (nl80211_set_iface_id(msg, drv->first_bss) < 0)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004984 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004985
4986 arg.drv = drv;
4987 arg.res = res;
4988 ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
4989 msg = NULL;
4990 if (ret == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004991 wpa_printf(MSG_DEBUG, "nl80211: Received scan results (%lu "
4992 "BSSes)", (unsigned long) res->num);
4993 nl80211_get_noise_for_scan_results(drv, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004994 return res;
4995 }
4996 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
4997 "(%s)", ret, strerror(-ret));
4998nla_put_failure:
4999 nlmsg_free(msg);
5000 wpa_scan_results_free(res);
5001 return NULL;
5002}
5003
5004
5005/**
5006 * wpa_driver_nl80211_get_scan_results - Fetch the latest scan results
5007 * @priv: Pointer to private wext data from wpa_driver_nl80211_init()
5008 * Returns: Scan results on success, -1 on failure
5009 */
5010static struct wpa_scan_results *
5011wpa_driver_nl80211_get_scan_results(void *priv)
5012{
5013 struct i802_bss *bss = priv;
5014 struct wpa_driver_nl80211_data *drv = bss->drv;
5015 struct wpa_scan_results *res;
5016
5017 res = nl80211_get_scan_results(drv);
5018 if (res)
5019 wpa_driver_nl80211_check_bss_status(drv, res);
5020 return res;
5021}
5022
5023
5024static void nl80211_dump_scan(struct wpa_driver_nl80211_data *drv)
5025{
5026 struct wpa_scan_results *res;
5027 size_t i;
5028
5029 res = nl80211_get_scan_results(drv);
5030 if (res == NULL) {
5031 wpa_printf(MSG_DEBUG, "nl80211: Failed to get scan results");
5032 return;
5033 }
5034
5035 wpa_printf(MSG_DEBUG, "nl80211: Scan result dump");
5036 for (i = 0; i < res->num; i++) {
5037 struct wpa_scan_res *r = res->res[i];
5038 wpa_printf(MSG_DEBUG, "nl80211: %d/%d " MACSTR "%s%s",
5039 (int) i, (int) res->num, MAC2STR(r->bssid),
5040 r->flags & WPA_SCAN_AUTHENTICATED ? " [auth]" : "",
5041 r->flags & WPA_SCAN_ASSOCIATED ? " [assoc]" : "");
5042 }
5043
5044 wpa_scan_results_free(res);
5045}
5046
5047
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005048static int wpa_driver_nl80211_set_key(const char *ifname, struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005049 enum wpa_alg alg, const u8 *addr,
5050 int key_idx, int set_tx,
5051 const u8 *seq, size_t seq_len,
5052 const u8 *key, size_t key_len)
5053{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005054 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005055 int ifindex;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005056 struct nl_msg *msg;
5057 int ret;
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07005058 int tdls = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005059
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005060 /* Ignore for P2P Device */
5061 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
5062 return 0;
5063
5064 ifindex = if_nametoindex(ifname);
5065 wpa_printf(MSG_DEBUG, "%s: ifindex=%d (%s) alg=%d addr=%p key_idx=%d "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005066 "set_tx=%d seq_len=%lu key_len=%lu",
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005067 __func__, ifindex, ifname, alg, addr, key_idx, set_tx,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005068 (unsigned long) seq_len, (unsigned long) key_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005069#ifdef CONFIG_TDLS
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07005070 if (key_idx == -1) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005071 key_idx = 0;
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07005072 tdls = 1;
5073 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005074#endif /* CONFIG_TDLS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005075
5076 msg = nlmsg_alloc();
5077 if (!msg)
5078 return -ENOMEM;
5079
5080 if (alg == WPA_ALG_NONE) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005081 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_KEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005082 } else {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005083 nl80211_cmd(drv, msg, 0, NL80211_CMD_NEW_KEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005084 NLA_PUT(msg, NL80211_ATTR_KEY_DATA, key_len, key);
5085 switch (alg) {
5086 case WPA_ALG_WEP:
5087 if (key_len == 5)
5088 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
5089 WLAN_CIPHER_SUITE_WEP40);
5090 else
5091 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
5092 WLAN_CIPHER_SUITE_WEP104);
5093 break;
5094 case WPA_ALG_TKIP:
5095 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
5096 WLAN_CIPHER_SUITE_TKIP);
5097 break;
5098 case WPA_ALG_CCMP:
5099 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
5100 WLAN_CIPHER_SUITE_CCMP);
5101 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005102 case WPA_ALG_GCMP:
5103 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
5104 WLAN_CIPHER_SUITE_GCMP);
5105 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005106 case WPA_ALG_IGTK:
5107 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
5108 WLAN_CIPHER_SUITE_AES_CMAC);
5109 break;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005110 case WPA_ALG_SMS4:
5111 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
5112 WLAN_CIPHER_SUITE_SMS4);
5113 break;
5114 case WPA_ALG_KRK:
5115 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
5116 WLAN_CIPHER_SUITE_KRK);
5117 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005118 default:
5119 wpa_printf(MSG_ERROR, "%s: Unsupported encryption "
5120 "algorithm %d", __func__, alg);
5121 nlmsg_free(msg);
5122 return -1;
5123 }
5124 }
5125
5126 if (seq && seq_len)
5127 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, seq_len, seq);
5128
5129 if (addr && !is_broadcast_ether_addr(addr)) {
5130 wpa_printf(MSG_DEBUG, " addr=" MACSTR, MAC2STR(addr));
5131 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
5132
5133 if (alg != WPA_ALG_WEP && key_idx && !set_tx) {
5134 wpa_printf(MSG_DEBUG, " RSN IBSS RX GTK");
5135 NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE,
5136 NL80211_KEYTYPE_GROUP);
5137 }
5138 } else if (addr && is_broadcast_ether_addr(addr)) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005139 struct nlattr *types;
5140
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005141 wpa_printf(MSG_DEBUG, " broadcast key");
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005142
5143 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005144 if (!types)
5145 goto nla_put_failure;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005146 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST);
5147 nla_nest_end(msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005148 }
5149 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
5150 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
5151
5152 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5153 if ((ret == -ENOENT || ret == -ENOLINK) && alg == WPA_ALG_NONE)
5154 ret = 0;
5155 if (ret)
5156 wpa_printf(MSG_DEBUG, "nl80211: set_key failed; err=%d %s)",
5157 ret, strerror(-ret));
5158
5159 /*
5160 * If we failed or don't need to set the default TX key (below),
5161 * we're done here.
5162 */
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07005163 if (ret || !set_tx || alg == WPA_ALG_NONE || tdls)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005164 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005165 if (is_ap_interface(drv->nlmode) && addr &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005166 !is_broadcast_ether_addr(addr))
5167 return ret;
5168
5169 msg = nlmsg_alloc();
5170 if (!msg)
5171 return -ENOMEM;
5172
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005173 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_KEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005174 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
5175 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
5176 if (alg == WPA_ALG_IGTK)
5177 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT_MGMT);
5178 else
5179 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT);
5180 if (addr && is_broadcast_ether_addr(addr)) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005181 struct nlattr *types;
5182
5183 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005184 if (!types)
5185 goto nla_put_failure;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005186 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST);
5187 nla_nest_end(msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005188 } else if (addr) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005189 struct nlattr *types;
5190
5191 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005192 if (!types)
5193 goto nla_put_failure;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005194 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_UNICAST);
5195 nla_nest_end(msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005196 }
5197
5198 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5199 if (ret == -ENOENT)
5200 ret = 0;
5201 if (ret)
5202 wpa_printf(MSG_DEBUG, "nl80211: set_key default failed; "
5203 "err=%d %s)", ret, strerror(-ret));
5204 return ret;
5205
5206nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005207 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005208 return -ENOBUFS;
5209}
5210
5211
5212static int nl_add_key(struct nl_msg *msg, enum wpa_alg alg,
5213 int key_idx, int defkey,
5214 const u8 *seq, size_t seq_len,
5215 const u8 *key, size_t key_len)
5216{
5217 struct nlattr *key_attr = nla_nest_start(msg, NL80211_ATTR_KEY);
5218 if (!key_attr)
5219 return -1;
5220
5221 if (defkey && alg == WPA_ALG_IGTK)
5222 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_MGMT);
5223 else if (defkey)
5224 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
5225
5226 NLA_PUT_U8(msg, NL80211_KEY_IDX, key_idx);
5227
5228 switch (alg) {
5229 case WPA_ALG_WEP:
5230 if (key_len == 5)
5231 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
5232 WLAN_CIPHER_SUITE_WEP40);
5233 else
5234 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
5235 WLAN_CIPHER_SUITE_WEP104);
5236 break;
5237 case WPA_ALG_TKIP:
5238 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, WLAN_CIPHER_SUITE_TKIP);
5239 break;
5240 case WPA_ALG_CCMP:
5241 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, WLAN_CIPHER_SUITE_CCMP);
5242 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005243 case WPA_ALG_GCMP:
5244 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, WLAN_CIPHER_SUITE_GCMP);
5245 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005246 case WPA_ALG_IGTK:
5247 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
5248 WLAN_CIPHER_SUITE_AES_CMAC);
5249 break;
5250 default:
5251 wpa_printf(MSG_ERROR, "%s: Unsupported encryption "
5252 "algorithm %d", __func__, alg);
5253 return -1;
5254 }
5255
5256 if (seq && seq_len)
5257 NLA_PUT(msg, NL80211_KEY_SEQ, seq_len, seq);
5258
5259 NLA_PUT(msg, NL80211_KEY_DATA, key_len, key);
5260
5261 nla_nest_end(msg, key_attr);
5262
5263 return 0;
5264 nla_put_failure:
5265 return -1;
5266}
5267
5268
5269static int nl80211_set_conn_keys(struct wpa_driver_associate_params *params,
5270 struct nl_msg *msg)
5271{
5272 int i, privacy = 0;
5273 struct nlattr *nl_keys, *nl_key;
5274
5275 for (i = 0; i < 4; i++) {
5276 if (!params->wep_key[i])
5277 continue;
5278 privacy = 1;
5279 break;
5280 }
5281 if (params->wps == WPS_MODE_PRIVACY)
5282 privacy = 1;
5283 if (params->pairwise_suite &&
5284 params->pairwise_suite != WPA_CIPHER_NONE)
5285 privacy = 1;
5286
5287 if (!privacy)
5288 return 0;
5289
5290 NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
5291
5292 nl_keys = nla_nest_start(msg, NL80211_ATTR_KEYS);
5293 if (!nl_keys)
5294 goto nla_put_failure;
5295
5296 for (i = 0; i < 4; i++) {
5297 if (!params->wep_key[i])
5298 continue;
5299
5300 nl_key = nla_nest_start(msg, i);
5301 if (!nl_key)
5302 goto nla_put_failure;
5303
5304 NLA_PUT(msg, NL80211_KEY_DATA, params->wep_key_len[i],
5305 params->wep_key[i]);
5306 if (params->wep_key_len[i] == 5)
5307 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
5308 WLAN_CIPHER_SUITE_WEP40);
5309 else
5310 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
5311 WLAN_CIPHER_SUITE_WEP104);
5312
5313 NLA_PUT_U8(msg, NL80211_KEY_IDX, i);
5314
5315 if (i == params->wep_tx_keyidx)
5316 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
5317
5318 nla_nest_end(msg, nl_key);
5319 }
5320 nla_nest_end(msg, nl_keys);
5321
5322 return 0;
5323
5324nla_put_failure:
5325 return -ENOBUFS;
5326}
5327
5328
5329static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
5330 const u8 *addr, int cmd, u16 reason_code,
5331 int local_state_change)
5332{
5333 int ret = -1;
5334 struct nl_msg *msg;
5335
5336 msg = nlmsg_alloc();
5337 if (!msg)
5338 return -1;
5339
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005340 nl80211_cmd(drv, msg, 0, cmd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005341
5342 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
5343 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason_code);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005344 if (addr)
5345 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005346 if (local_state_change)
5347 NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE);
5348
5349 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5350 msg = NULL;
5351 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005352 wpa_dbg(drv->ctx, MSG_DEBUG,
5353 "nl80211: MLME command failed: reason=%u ret=%d (%s)",
5354 reason_code, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005355 goto nla_put_failure;
5356 }
5357 ret = 0;
5358
5359nla_put_failure:
5360 nlmsg_free(msg);
5361 return ret;
5362}
5363
5364
5365static int wpa_driver_nl80211_disconnect(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005366 int reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005367{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005368 int ret;
5369
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005370 wpa_printf(MSG_DEBUG, "%s(reason_code=%d)", __func__, reason_code);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005371 nl80211_mark_disconnected(drv);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005372 /* Disconnect command doesn't need BSSID - it uses cached value */
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005373 ret = wpa_driver_nl80211_mlme(drv, NULL, NL80211_CMD_DISCONNECT,
5374 reason_code, 0);
5375 /*
5376 * For locally generated disconnect, supplicant already generates a
5377 * DEAUTH event, so ignore the event from NL80211.
5378 */
5379 drv->ignore_next_local_disconnect = ret == 0;
5380
5381 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005382}
5383
5384
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005385static int wpa_driver_nl80211_deauthenticate(struct i802_bss *bss,
5386 const u8 *addr, int reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005387{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005388 struct wpa_driver_nl80211_data *drv = bss->drv;
5389 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME))
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005390 return wpa_driver_nl80211_disconnect(drv, reason_code);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005391 wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " reason_code=%d)",
5392 __func__, MAC2STR(addr), reason_code);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005393 nl80211_mark_disconnected(drv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005394 if (drv->nlmode == NL80211_IFTYPE_ADHOC)
5395 return nl80211_leave_ibss(drv);
5396 return wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DEAUTHENTICATE,
5397 reason_code, 0);
5398}
5399
5400
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005401static void nl80211_copy_auth_params(struct wpa_driver_nl80211_data *drv,
5402 struct wpa_driver_auth_params *params)
5403{
5404 int i;
5405
5406 drv->auth_freq = params->freq;
5407 drv->auth_alg = params->auth_alg;
5408 drv->auth_wep_tx_keyidx = params->wep_tx_keyidx;
5409 drv->auth_local_state_change = params->local_state_change;
5410 drv->auth_p2p = params->p2p;
5411
5412 if (params->bssid)
5413 os_memcpy(drv->auth_bssid_, params->bssid, ETH_ALEN);
5414 else
5415 os_memset(drv->auth_bssid_, 0, ETH_ALEN);
5416
5417 if (params->ssid) {
5418 os_memcpy(drv->auth_ssid, params->ssid, params->ssid_len);
5419 drv->auth_ssid_len = params->ssid_len;
5420 } else
5421 drv->auth_ssid_len = 0;
5422
5423
5424 os_free(drv->auth_ie);
5425 drv->auth_ie = NULL;
5426 drv->auth_ie_len = 0;
5427 if (params->ie) {
5428 drv->auth_ie = os_malloc(params->ie_len);
5429 if (drv->auth_ie) {
5430 os_memcpy(drv->auth_ie, params->ie, params->ie_len);
5431 drv->auth_ie_len = params->ie_len;
5432 }
5433 }
5434
5435 for (i = 0; i < 4; i++) {
5436 if (params->wep_key[i] && params->wep_key_len[i] &&
5437 params->wep_key_len[i] <= 16) {
5438 os_memcpy(drv->auth_wep_key[i], params->wep_key[i],
5439 params->wep_key_len[i]);
5440 drv->auth_wep_key_len[i] = params->wep_key_len[i];
5441 } else
5442 drv->auth_wep_key_len[i] = 0;
5443 }
5444}
5445
5446
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005447static int wpa_driver_nl80211_authenticate(
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005448 struct i802_bss *bss, struct wpa_driver_auth_params *params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005449{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005450 struct wpa_driver_nl80211_data *drv = bss->drv;
5451 int ret = -1, i;
5452 struct nl_msg *msg;
5453 enum nl80211_auth_type type;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005454 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005455 int count = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005456 int is_retry;
5457
5458 is_retry = drv->retry_auth;
5459 drv->retry_auth = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005460
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005461 nl80211_mark_disconnected(drv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005462 os_memset(drv->auth_bssid, 0, ETH_ALEN);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005463 if (params->bssid)
5464 os_memcpy(drv->auth_attempt_bssid, params->bssid, ETH_ALEN);
5465 else
5466 os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005467 /* FIX: IBSS mode */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005468 nlmode = params->p2p ?
5469 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
5470 if (drv->nlmode != nlmode &&
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005471 wpa_driver_nl80211_set_mode(bss, nlmode) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005472 return -1;
5473
5474retry:
5475 msg = nlmsg_alloc();
5476 if (!msg)
5477 return -1;
5478
5479 wpa_printf(MSG_DEBUG, "nl80211: Authenticate (ifindex=%d)",
5480 drv->ifindex);
5481
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005482 nl80211_cmd(drv, msg, 0, NL80211_CMD_AUTHENTICATE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005483
5484 for (i = 0; i < 4; i++) {
5485 if (!params->wep_key[i])
5486 continue;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005487 wpa_driver_nl80211_set_key(bss->ifname, bss, WPA_ALG_WEP,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005488 NULL, i,
5489 i == params->wep_tx_keyidx, NULL, 0,
5490 params->wep_key[i],
5491 params->wep_key_len[i]);
5492 if (params->wep_tx_keyidx != i)
5493 continue;
5494 if (nl_add_key(msg, WPA_ALG_WEP, i, 1, NULL, 0,
5495 params->wep_key[i], params->wep_key_len[i])) {
5496 nlmsg_free(msg);
5497 return -1;
5498 }
5499 }
5500
5501 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
5502 if (params->bssid) {
5503 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
5504 MAC2STR(params->bssid));
5505 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
5506 }
5507 if (params->freq) {
5508 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
5509 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
5510 }
5511 if (params->ssid) {
5512 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
5513 params->ssid, params->ssid_len);
5514 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
5515 params->ssid);
5516 }
5517 wpa_hexdump(MSG_DEBUG, " * IEs", params->ie, params->ie_len);
5518 if (params->ie)
5519 NLA_PUT(msg, NL80211_ATTR_IE, params->ie_len, params->ie);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005520 if (params->sae_data) {
5521 wpa_hexdump(MSG_DEBUG, " * SAE data", params->sae_data,
5522 params->sae_data_len);
5523 NLA_PUT(msg, NL80211_ATTR_SAE_DATA, params->sae_data_len,
5524 params->sae_data);
5525 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005526 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
5527 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
5528 else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
5529 type = NL80211_AUTHTYPE_SHARED_KEY;
5530 else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
5531 type = NL80211_AUTHTYPE_NETWORK_EAP;
5532 else if (params->auth_alg & WPA_AUTH_ALG_FT)
5533 type = NL80211_AUTHTYPE_FT;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005534 else if (params->auth_alg & WPA_AUTH_ALG_SAE)
5535 type = NL80211_AUTHTYPE_SAE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005536 else
5537 goto nla_put_failure;
5538 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
5539 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
5540 if (params->local_state_change) {
5541 wpa_printf(MSG_DEBUG, " * Local state change only");
5542 NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE);
5543 }
5544
5545 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5546 msg = NULL;
5547 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005548 wpa_dbg(drv->ctx, MSG_DEBUG,
5549 "nl80211: MLME command failed (auth): ret=%d (%s)",
5550 ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005551 count++;
5552 if (ret == -EALREADY && count == 1 && params->bssid &&
5553 !params->local_state_change) {
5554 /*
5555 * mac80211 does not currently accept new
5556 * authentication if we are already authenticated. As a
5557 * workaround, force deauthentication and try again.
5558 */
5559 wpa_printf(MSG_DEBUG, "nl80211: Retry authentication "
5560 "after forced deauthentication");
5561 wpa_driver_nl80211_deauthenticate(
5562 bss, params->bssid,
5563 WLAN_REASON_PREV_AUTH_NOT_VALID);
5564 nlmsg_free(msg);
5565 goto retry;
5566 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005567
5568 if (ret == -ENOENT && params->freq && !is_retry) {
5569 /*
5570 * cfg80211 has likely expired the BSS entry even
5571 * though it was previously available in our internal
5572 * BSS table. To recover quickly, start a single
5573 * channel scan on the specified channel.
5574 */
5575 struct wpa_driver_scan_params scan;
5576 int freqs[2];
5577
5578 os_memset(&scan, 0, sizeof(scan));
5579 scan.num_ssids = 1;
5580 if (params->ssid) {
5581 scan.ssids[0].ssid = params->ssid;
5582 scan.ssids[0].ssid_len = params->ssid_len;
5583 }
5584 freqs[0] = params->freq;
5585 freqs[1] = 0;
5586 scan.freqs = freqs;
5587 wpa_printf(MSG_DEBUG, "nl80211: Trigger single "
5588 "channel scan to refresh cfg80211 BSS "
5589 "entry");
5590 ret = wpa_driver_nl80211_scan(bss, &scan);
5591 if (ret == 0) {
5592 nl80211_copy_auth_params(drv, params);
5593 drv->scan_for_auth = 1;
5594 }
5595 } else if (is_retry) {
5596 /*
5597 * Need to indicate this with an event since the return
5598 * value from the retry is not delivered to core code.
5599 */
5600 union wpa_event_data event;
5601 wpa_printf(MSG_DEBUG, "nl80211: Authentication retry "
5602 "failed");
5603 os_memset(&event, 0, sizeof(event));
5604 os_memcpy(event.timeout_event.addr, drv->auth_bssid_,
5605 ETH_ALEN);
5606 wpa_supplicant_event(drv->ctx, EVENT_AUTH_TIMED_OUT,
5607 &event);
5608 }
5609
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005610 goto nla_put_failure;
5611 }
5612 ret = 0;
5613 wpa_printf(MSG_DEBUG, "nl80211: Authentication request send "
5614 "successfully");
5615
5616nla_put_failure:
5617 nlmsg_free(msg);
5618 return ret;
5619}
5620
5621
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005622static int wpa_driver_nl80211_authenticate_retry(
5623 struct wpa_driver_nl80211_data *drv)
5624{
5625 struct wpa_driver_auth_params params;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005626 struct i802_bss *bss = drv->first_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005627 int i;
5628
5629 wpa_printf(MSG_DEBUG, "nl80211: Try to authenticate again");
5630
5631 os_memset(&params, 0, sizeof(params));
5632 params.freq = drv->auth_freq;
5633 params.auth_alg = drv->auth_alg;
5634 params.wep_tx_keyidx = drv->auth_wep_tx_keyidx;
5635 params.local_state_change = drv->auth_local_state_change;
5636 params.p2p = drv->auth_p2p;
5637
5638 if (!is_zero_ether_addr(drv->auth_bssid_))
5639 params.bssid = drv->auth_bssid_;
5640
5641 if (drv->auth_ssid_len) {
5642 params.ssid = drv->auth_ssid;
5643 params.ssid_len = drv->auth_ssid_len;
5644 }
5645
5646 params.ie = drv->auth_ie;
5647 params.ie_len = drv->auth_ie_len;
5648
5649 for (i = 0; i < 4; i++) {
5650 if (drv->auth_wep_key_len[i]) {
5651 params.wep_key[i] = drv->auth_wep_key[i];
5652 params.wep_key_len[i] = drv->auth_wep_key_len[i];
5653 }
5654 }
5655
5656 drv->retry_auth = 1;
5657 return wpa_driver_nl80211_authenticate(bss, &params);
5658}
5659
5660
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005661struct phy_info_arg {
5662 u16 *num_modes;
5663 struct hostapd_hw_modes *modes;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005664 int last_mode, last_chan_idx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005665};
5666
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005667static void phy_info_ht_capa(struct hostapd_hw_modes *mode, struct nlattr *capa,
5668 struct nlattr *ampdu_factor,
5669 struct nlattr *ampdu_density,
5670 struct nlattr *mcs_set)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005671{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005672 if (capa)
5673 mode->ht_capab = nla_get_u16(capa);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005674
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005675 if (ampdu_factor)
5676 mode->a_mpdu_params |= nla_get_u8(ampdu_factor) & 0x03;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005677
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005678 if (ampdu_density)
5679 mode->a_mpdu_params |= nla_get_u8(ampdu_density) << 2;
5680
5681 if (mcs_set && nla_len(mcs_set) >= 16) {
5682 u8 *mcs;
5683 mcs = nla_data(mcs_set);
5684 os_memcpy(mode->mcs_set, mcs, 16);
5685 }
5686}
5687
5688
5689static void phy_info_vht_capa(struct hostapd_hw_modes *mode,
5690 struct nlattr *capa,
5691 struct nlattr *mcs_set)
5692{
5693 if (capa)
5694 mode->vht_capab = nla_get_u32(capa);
5695
5696 if (mcs_set && nla_len(mcs_set) >= 8) {
5697 u8 *mcs;
5698 mcs = nla_data(mcs_set);
5699 os_memcpy(mode->vht_mcs_set, mcs, 8);
5700 }
5701}
5702
5703
5704static void phy_info_freq(struct hostapd_hw_modes *mode,
5705 struct hostapd_channel_data *chan,
5706 struct nlattr *tb_freq[])
5707{
Dmitry Shmidt4b060592013-04-29 16:42:49 -07005708 u8 channel;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005709 chan->freq = nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_FREQ]);
5710 chan->flag = 0;
Dmitry Shmidt4b060592013-04-29 16:42:49 -07005711 if (ieee80211_freq_to_chan(chan->freq, &channel) != NUM_HOSTAPD_MODES)
5712 chan->chan = channel;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005713
5714 if (tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
5715 chan->flag |= HOSTAPD_CHAN_DISABLED;
5716 if (tb_freq[NL80211_FREQUENCY_ATTR_PASSIVE_SCAN])
5717 chan->flag |= HOSTAPD_CHAN_PASSIVE_SCAN;
5718 if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IBSS])
5719 chan->flag |= HOSTAPD_CHAN_NO_IBSS;
5720 if (tb_freq[NL80211_FREQUENCY_ATTR_RADAR])
5721 chan->flag |= HOSTAPD_CHAN_RADAR;
5722
Dmitry Shmidtea69e842013-05-13 14:52:28 -07005723 if (tb_freq[NL80211_FREQUENCY_ATTR_DFS_STATE]) {
5724 enum nl80211_dfs_state state =
5725 nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_DFS_STATE]);
5726
5727 switch (state) {
5728 case NL80211_DFS_USABLE:
5729 chan->flag |= HOSTAPD_CHAN_DFS_USABLE;
5730 break;
5731 case NL80211_DFS_AVAILABLE:
5732 chan->flag |= HOSTAPD_CHAN_DFS_AVAILABLE;
5733 break;
5734 case NL80211_DFS_UNAVAILABLE:
5735 chan->flag |= HOSTAPD_CHAN_DFS_UNAVAILABLE;
5736 break;
5737 }
5738 }
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005739}
5740
5741
5742static int phy_info_freqs(struct phy_info_arg *phy_info,
5743 struct hostapd_hw_modes *mode, struct nlattr *tb)
5744{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005745 static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
5746 [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
5747 [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
5748 [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] = { .type = NLA_FLAG },
5749 [NL80211_FREQUENCY_ATTR_NO_IBSS] = { .type = NLA_FLAG },
5750 [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
5751 [NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32 },
Dmitry Shmidtea69e842013-05-13 14:52:28 -07005752 [NL80211_FREQUENCY_ATTR_DFS_STATE] = { .type = NLA_U32 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005753 };
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005754 int new_channels = 0;
5755 struct hostapd_channel_data *channel;
5756 struct nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005757 struct nlattr *nl_freq;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005758 int rem_freq, idx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005759
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005760 if (tb == NULL)
5761 return NL_OK;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005762
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005763 nla_for_each_nested(nl_freq, tb, rem_freq) {
5764 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX,
5765 nla_data(nl_freq), nla_len(nl_freq), freq_policy);
5766 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
5767 continue;
5768 new_channels++;
5769 }
5770
5771 channel = os_realloc_array(mode->channels,
5772 mode->num_channels + new_channels,
5773 sizeof(struct hostapd_channel_data));
5774 if (!channel)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005775 return NL_SKIP;
5776
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005777 mode->channels = channel;
5778 mode->num_channels += new_channels;
5779
5780 idx = phy_info->last_chan_idx;
5781
5782 nla_for_each_nested(nl_freq, tb, rem_freq) {
5783 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX,
5784 nla_data(nl_freq), nla_len(nl_freq), freq_policy);
5785 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
5786 continue;
5787 phy_info_freq(mode, &mode->channels[idx], tb_freq);
5788 idx++;
5789 }
5790 phy_info->last_chan_idx = idx;
5791
5792 return NL_OK;
5793}
5794
5795
5796static int phy_info_rates(struct hostapd_hw_modes *mode, struct nlattr *tb)
5797{
5798 static struct nla_policy rate_policy[NL80211_BITRATE_ATTR_MAX + 1] = {
5799 [NL80211_BITRATE_ATTR_RATE] = { .type = NLA_U32 },
5800 [NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE] =
5801 { .type = NLA_FLAG },
5802 };
5803 struct nlattr *tb_rate[NL80211_BITRATE_ATTR_MAX + 1];
5804 struct nlattr *nl_rate;
5805 int rem_rate, idx;
5806
5807 if (tb == NULL)
5808 return NL_OK;
5809
5810 nla_for_each_nested(nl_rate, tb, rem_rate) {
5811 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX,
5812 nla_data(nl_rate), nla_len(nl_rate),
5813 rate_policy);
5814 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
5815 continue;
5816 mode->num_rates++;
5817 }
5818
5819 mode->rates = os_calloc(mode->num_rates, sizeof(int));
5820 if (!mode->rates)
5821 return NL_SKIP;
5822
5823 idx = 0;
5824
5825 nla_for_each_nested(nl_rate, tb, rem_rate) {
5826 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX,
5827 nla_data(nl_rate), nla_len(nl_rate),
5828 rate_policy);
5829 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
5830 continue;
5831 mode->rates[idx] = nla_get_u32(
5832 tb_rate[NL80211_BITRATE_ATTR_RATE]);
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005833 idx++;
5834 }
5835
5836 return NL_OK;
5837}
5838
5839
5840static int phy_info_band(struct phy_info_arg *phy_info, struct nlattr *nl_band)
5841{
5842 struct nlattr *tb_band[NL80211_BAND_ATTR_MAX + 1];
5843 struct hostapd_hw_modes *mode;
5844 int ret;
5845
5846 if (phy_info->last_mode != nl_band->nla_type) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005847 mode = os_realloc_array(phy_info->modes,
5848 *phy_info->num_modes + 1,
5849 sizeof(*mode));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005850 if (!mode)
5851 return NL_SKIP;
5852 phy_info->modes = mode;
5853
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005854 mode = &phy_info->modes[*(phy_info->num_modes)];
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005855 os_memset(mode, 0, sizeof(*mode));
5856 mode->mode = NUM_HOSTAPD_MODES;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005857 mode->flags = HOSTAPD_MODE_FLAG_HT_INFO_KNOWN |
5858 HOSTAPD_MODE_FLAG_VHT_INFO_KNOWN;
5859
5860 /*
5861 * Unsupported VHT MCS stream is defined as value 3, so the VHT
5862 * MCS RX/TX map must be initialized with 0xffff to mark all 8
5863 * possible streams as unsupported. This will be overridden if
5864 * driver advertises VHT support.
5865 */
5866 mode->vht_mcs_set[0] = 0xff;
5867 mode->vht_mcs_set[1] = 0xff;
5868 mode->vht_mcs_set[4] = 0xff;
5869 mode->vht_mcs_set[5] = 0xff;
5870
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005871 *(phy_info->num_modes) += 1;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005872 phy_info->last_mode = nl_band->nla_type;
5873 phy_info->last_chan_idx = 0;
5874 } else
5875 mode = &phy_info->modes[*(phy_info->num_modes) - 1];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005876
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005877 nla_parse(tb_band, NL80211_BAND_ATTR_MAX, nla_data(nl_band),
5878 nla_len(nl_band), NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005879
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005880 phy_info_ht_capa(mode, tb_band[NL80211_BAND_ATTR_HT_CAPA],
5881 tb_band[NL80211_BAND_ATTR_HT_AMPDU_FACTOR],
5882 tb_band[NL80211_BAND_ATTR_HT_AMPDU_DENSITY],
5883 tb_band[NL80211_BAND_ATTR_HT_MCS_SET]);
5884 phy_info_vht_capa(mode, tb_band[NL80211_BAND_ATTR_VHT_CAPA],
5885 tb_band[NL80211_BAND_ATTR_VHT_MCS_SET]);
5886 ret = phy_info_freqs(phy_info, mode, tb_band[NL80211_BAND_ATTR_FREQS]);
5887 if (ret != NL_OK)
5888 return ret;
5889 ret = phy_info_rates(mode, tb_band[NL80211_BAND_ATTR_RATES]);
5890 if (ret != NL_OK)
5891 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005892
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005893 return NL_OK;
5894}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005895
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005896
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005897static int phy_info_handler(struct nl_msg *msg, void *arg)
5898{
5899 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
5900 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
5901 struct phy_info_arg *phy_info = arg;
5902 struct nlattr *nl_band;
5903 int rem_band;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005904
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005905 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
5906 genlmsg_attrlen(gnlh, 0), NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005907
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005908 if (!tb_msg[NL80211_ATTR_WIPHY_BANDS])
5909 return NL_SKIP;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005910
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005911 nla_for_each_nested(nl_band, tb_msg[NL80211_ATTR_WIPHY_BANDS], rem_band)
5912 {
5913 int res = phy_info_band(phy_info, nl_band);
5914 if (res != NL_OK)
5915 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005916 }
5917
5918 return NL_SKIP;
5919}
5920
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005921
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005922static struct hostapd_hw_modes *
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005923wpa_driver_nl80211_postprocess_modes(struct hostapd_hw_modes *modes,
5924 u16 *num_modes)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005925{
5926 u16 m;
5927 struct hostapd_hw_modes *mode11g = NULL, *nmodes, *mode;
5928 int i, mode11g_idx = -1;
5929
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005930 /* heuristic to set up modes */
5931 for (m = 0; m < *num_modes; m++) {
5932 if (!modes[m].num_channels)
5933 continue;
5934 if (modes[m].channels[0].freq < 4000) {
5935 modes[m].mode = HOSTAPD_MODE_IEEE80211B;
5936 for (i = 0; i < modes[m].num_rates; i++) {
5937 if (modes[m].rates[i] > 200) {
5938 modes[m].mode = HOSTAPD_MODE_IEEE80211G;
5939 break;
5940 }
5941 }
5942 } else if (modes[m].channels[0].freq > 50000)
5943 modes[m].mode = HOSTAPD_MODE_IEEE80211AD;
5944 else
5945 modes[m].mode = HOSTAPD_MODE_IEEE80211A;
5946 }
5947
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005948 /* If only 802.11g mode is included, use it to construct matching
5949 * 802.11b mode data. */
5950
5951 for (m = 0; m < *num_modes; m++) {
5952 if (modes[m].mode == HOSTAPD_MODE_IEEE80211B)
5953 return modes; /* 802.11b already included */
5954 if (modes[m].mode == HOSTAPD_MODE_IEEE80211G)
5955 mode11g_idx = m;
5956 }
5957
5958 if (mode11g_idx < 0)
5959 return modes; /* 2.4 GHz band not supported at all */
5960
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005961 nmodes = os_realloc_array(modes, *num_modes + 1, sizeof(*nmodes));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005962 if (nmodes == NULL)
5963 return modes; /* Could not add 802.11b mode */
5964
5965 mode = &nmodes[*num_modes];
5966 os_memset(mode, 0, sizeof(*mode));
5967 (*num_modes)++;
5968 modes = nmodes;
5969
5970 mode->mode = HOSTAPD_MODE_IEEE80211B;
5971
5972 mode11g = &modes[mode11g_idx];
5973 mode->num_channels = mode11g->num_channels;
5974 mode->channels = os_malloc(mode11g->num_channels *
5975 sizeof(struct hostapd_channel_data));
5976 if (mode->channels == NULL) {
5977 (*num_modes)--;
5978 return modes; /* Could not add 802.11b mode */
5979 }
5980 os_memcpy(mode->channels, mode11g->channels,
5981 mode11g->num_channels * sizeof(struct hostapd_channel_data));
5982
5983 mode->num_rates = 0;
5984 mode->rates = os_malloc(4 * sizeof(int));
5985 if (mode->rates == NULL) {
5986 os_free(mode->channels);
5987 (*num_modes)--;
5988 return modes; /* Could not add 802.11b mode */
5989 }
5990
5991 for (i = 0; i < mode11g->num_rates; i++) {
5992 if (mode11g->rates[i] != 10 && mode11g->rates[i] != 20 &&
5993 mode11g->rates[i] != 55 && mode11g->rates[i] != 110)
5994 continue;
5995 mode->rates[mode->num_rates] = mode11g->rates[i];
5996 mode->num_rates++;
5997 if (mode->num_rates == 4)
5998 break;
5999 }
6000
6001 if (mode->num_rates == 0) {
6002 os_free(mode->channels);
6003 os_free(mode->rates);
6004 (*num_modes)--;
6005 return modes; /* No 802.11b rates */
6006 }
6007
6008 wpa_printf(MSG_DEBUG, "nl80211: Added 802.11b mode based on 802.11g "
6009 "information");
6010
6011 return modes;
6012}
6013
6014
6015static void nl80211_set_ht40_mode(struct hostapd_hw_modes *mode, int start,
6016 int end)
6017{
6018 int c;
6019
6020 for (c = 0; c < mode->num_channels; c++) {
6021 struct hostapd_channel_data *chan = &mode->channels[c];
6022 if (chan->freq - 10 >= start && chan->freq + 10 <= end)
6023 chan->flag |= HOSTAPD_CHAN_HT40;
6024 }
6025}
6026
6027
6028static void nl80211_set_ht40_mode_sec(struct hostapd_hw_modes *mode, int start,
6029 int end)
6030{
6031 int c;
6032
6033 for (c = 0; c < mode->num_channels; c++) {
6034 struct hostapd_channel_data *chan = &mode->channels[c];
6035 if (!(chan->flag & HOSTAPD_CHAN_HT40))
6036 continue;
6037 if (chan->freq - 30 >= start && chan->freq - 10 <= end)
6038 chan->flag |= HOSTAPD_CHAN_HT40MINUS;
6039 if (chan->freq + 10 >= start && chan->freq + 30 <= end)
6040 chan->flag |= HOSTAPD_CHAN_HT40PLUS;
6041 }
6042}
6043
6044
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006045static void nl80211_reg_rule_max_eirp(struct nlattr *tb[],
6046 struct phy_info_arg *results)
6047{
6048 u32 start, end, max_eirp;
6049 u16 m;
6050
6051 if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
6052 tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
6053 tb[NL80211_ATTR_POWER_RULE_MAX_EIRP] == NULL)
6054 return;
6055
6056 start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
6057 end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
6058 max_eirp = nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]) / 100;
6059
6060 wpa_printf(MSG_DEBUG, "nl80211: %u-%u @ %u mBm",
6061 start, end, max_eirp);
6062
6063 for (m = 0; m < *results->num_modes; m++) {
6064 int c;
6065 struct hostapd_hw_modes *mode = &results->modes[m];
6066
6067 for (c = 0; c < mode->num_channels; c++) {
6068 struct hostapd_channel_data *chan = &mode->channels[c];
6069 if ((u32) chan->freq - 10 >= start &&
6070 (u32) chan->freq + 10 <= end)
6071 chan->max_tx_power = max_eirp;
6072 }
6073 }
6074}
6075
6076
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006077static void nl80211_reg_rule_ht40(struct nlattr *tb[],
6078 struct phy_info_arg *results)
6079{
6080 u32 start, end, max_bw;
6081 u16 m;
6082
6083 if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
6084 tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
6085 tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
6086 return;
6087
6088 start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
6089 end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
6090 max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
6091
6092 wpa_printf(MSG_DEBUG, "nl80211: %u-%u @ %u MHz",
6093 start, end, max_bw);
6094 if (max_bw < 40)
6095 return;
6096
6097 for (m = 0; m < *results->num_modes; m++) {
6098 if (!(results->modes[m].ht_capab &
6099 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
6100 continue;
6101 nl80211_set_ht40_mode(&results->modes[m], start, end);
6102 }
6103}
6104
6105
6106static void nl80211_reg_rule_sec(struct nlattr *tb[],
6107 struct phy_info_arg *results)
6108{
6109 u32 start, end, max_bw;
6110 u16 m;
6111
6112 if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
6113 tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
6114 tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
6115 return;
6116
6117 start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
6118 end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
6119 max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
6120
6121 if (max_bw < 20)
6122 return;
6123
6124 for (m = 0; m < *results->num_modes; m++) {
6125 if (!(results->modes[m].ht_capab &
6126 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
6127 continue;
6128 nl80211_set_ht40_mode_sec(&results->modes[m], start, end);
6129 }
6130}
6131
6132
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006133static void nl80211_set_vht_mode(struct hostapd_hw_modes *mode, int start,
6134 int end)
6135{
6136 int c;
6137
6138 for (c = 0; c < mode->num_channels; c++) {
6139 struct hostapd_channel_data *chan = &mode->channels[c];
6140 if (chan->freq - 10 >= start && chan->freq + 70 <= end)
6141 chan->flag |= HOSTAPD_CHAN_VHT_10_70;
6142
6143 if (chan->freq - 30 >= start && chan->freq + 50 <= end)
6144 chan->flag |= HOSTAPD_CHAN_VHT_30_50;
6145
6146 if (chan->freq - 50 >= start && chan->freq + 30 <= end)
6147 chan->flag |= HOSTAPD_CHAN_VHT_50_30;
6148
6149 if (chan->freq - 70 >= start && chan->freq + 10 <= end)
6150 chan->flag |= HOSTAPD_CHAN_VHT_70_10;
6151 }
6152}
6153
6154
6155static void nl80211_reg_rule_vht(struct nlattr *tb[],
6156 struct phy_info_arg *results)
6157{
6158 u32 start, end, max_bw;
6159 u16 m;
6160
6161 if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
6162 tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
6163 tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
6164 return;
6165
6166 start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
6167 end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
6168 max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
6169
6170 if (max_bw < 80)
6171 return;
6172
6173 for (m = 0; m < *results->num_modes; m++) {
6174 if (!(results->modes[m].ht_capab &
6175 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
6176 continue;
6177 /* TODO: use a real VHT support indication */
6178 if (!results->modes[m].vht_capab)
6179 continue;
6180
6181 nl80211_set_vht_mode(&results->modes[m], start, end);
6182 }
6183}
6184
6185
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006186static int nl80211_get_reg(struct nl_msg *msg, void *arg)
6187{
6188 struct phy_info_arg *results = arg;
6189 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
6190 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6191 struct nlattr *nl_rule;
6192 struct nlattr *tb_rule[NL80211_FREQUENCY_ATTR_MAX + 1];
6193 int rem_rule;
6194 static struct nla_policy reg_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
6195 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
6196 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
6197 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
6198 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
6199 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
6200 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
6201 };
6202
6203 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6204 genlmsg_attrlen(gnlh, 0), NULL);
6205 if (!tb_msg[NL80211_ATTR_REG_ALPHA2] ||
6206 !tb_msg[NL80211_ATTR_REG_RULES]) {
6207 wpa_printf(MSG_DEBUG, "nl80211: No regulatory information "
6208 "available");
6209 return NL_SKIP;
6210 }
6211
6212 wpa_printf(MSG_DEBUG, "nl80211: Regulatory information - country=%s",
6213 (char *) nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]));
6214
6215 nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
6216 {
6217 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
6218 nla_data(nl_rule), nla_len(nl_rule), reg_policy);
6219 nl80211_reg_rule_ht40(tb_rule, results);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006220 nl80211_reg_rule_max_eirp(tb_rule, results);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006221 }
6222
6223 nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
6224 {
6225 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
6226 nla_data(nl_rule), nla_len(nl_rule), reg_policy);
6227 nl80211_reg_rule_sec(tb_rule, results);
6228 }
6229
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006230 nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
6231 {
6232 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
6233 nla_data(nl_rule), nla_len(nl_rule), reg_policy);
6234 nl80211_reg_rule_vht(tb_rule, results);
6235 }
6236
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006237 return NL_SKIP;
6238}
6239
6240
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006241static int nl80211_set_regulatory_flags(struct wpa_driver_nl80211_data *drv,
6242 struct phy_info_arg *results)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006243{
6244 struct nl_msg *msg;
6245
6246 msg = nlmsg_alloc();
6247 if (!msg)
6248 return -ENOMEM;
6249
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006250 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006251 return send_and_recv_msgs(drv, msg, nl80211_get_reg, results);
6252}
6253
6254
6255static struct hostapd_hw_modes *
6256wpa_driver_nl80211_get_hw_feature_data(void *priv, u16 *num_modes, u16 *flags)
6257{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006258 u32 feat;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006259 struct i802_bss *bss = priv;
6260 struct wpa_driver_nl80211_data *drv = bss->drv;
6261 struct nl_msg *msg;
6262 struct phy_info_arg result = {
6263 .num_modes = num_modes,
6264 .modes = NULL,
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006265 .last_mode = -1,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006266 };
6267
6268 *num_modes = 0;
6269 *flags = 0;
6270
6271 msg = nlmsg_alloc();
6272 if (!msg)
6273 return NULL;
6274
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006275 feat = get_nl80211_protocol_features(drv);
6276 if (feat & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP)
6277 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_WIPHY);
6278 else
6279 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006280
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006281 NLA_PUT_FLAG(msg, NL80211_ATTR_SPLIT_WIPHY_DUMP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006282 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
6283
6284 if (send_and_recv_msgs(drv, msg, phy_info_handler, &result) == 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006285 nl80211_set_regulatory_flags(drv, &result);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07006286 return wpa_driver_nl80211_postprocess_modes(result.modes,
6287 num_modes);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006288 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006289 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006290 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006291 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006292 return NULL;
6293}
6294
6295
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006296static int wpa_driver_nl80211_send_mntr(struct wpa_driver_nl80211_data *drv,
6297 const void *data, size_t len,
6298 int encrypt, int noack)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006299{
6300 __u8 rtap_hdr[] = {
6301 0x00, 0x00, /* radiotap version */
6302 0x0e, 0x00, /* radiotap length */
6303 0x02, 0xc0, 0x00, 0x00, /* bmap: flags, tx and rx flags */
6304 IEEE80211_RADIOTAP_F_FRAG, /* F_FRAG (fragment if required) */
6305 0x00, /* padding */
6306 0x00, 0x00, /* RX and TX flags to indicate that */
6307 0x00, 0x00, /* this is the injected frame directly */
6308 };
6309 struct iovec iov[2] = {
6310 {
6311 .iov_base = &rtap_hdr,
6312 .iov_len = sizeof(rtap_hdr),
6313 },
6314 {
6315 .iov_base = (void *) data,
6316 .iov_len = len,
6317 }
6318 };
6319 struct msghdr msg = {
6320 .msg_name = NULL,
6321 .msg_namelen = 0,
6322 .msg_iov = iov,
6323 .msg_iovlen = 2,
6324 .msg_control = NULL,
6325 .msg_controllen = 0,
6326 .msg_flags = 0,
6327 };
6328 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006329 u16 txflags = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006330
6331 if (encrypt)
6332 rtap_hdr[8] |= IEEE80211_RADIOTAP_F_WEP;
6333
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07006334 if (drv->monitor_sock < 0) {
6335 wpa_printf(MSG_DEBUG, "nl80211: No monitor socket available "
6336 "for %s", __func__);
6337 return -1;
6338 }
6339
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006340 if (noack)
6341 txflags |= IEEE80211_RADIOTAP_F_TX_NOACK;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006342 WPA_PUT_LE16(&rtap_hdr[12], txflags);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006343
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006344 res = sendmsg(drv->monitor_sock, &msg, 0);
6345 if (res < 0) {
6346 wpa_printf(MSG_INFO, "nl80211: sendmsg: %s", strerror(errno));
6347 return -1;
6348 }
6349 return 0;
6350}
6351
6352
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006353static int wpa_driver_nl80211_send_frame(struct i802_bss *bss,
6354 const void *data, size_t len,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006355 int encrypt, int noack,
6356 unsigned int freq, int no_cck,
6357 int offchanok, unsigned int wait_time)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006358{
6359 struct wpa_driver_nl80211_data *drv = bss->drv;
6360 u64 cookie;
Dmitry Shmidt051af732013-10-22 13:52:46 -07006361 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006362
Dmitry Shmidt56052862013-10-04 10:23:25 -07006363 if (freq == 0) {
6364 wpa_printf(MSG_DEBUG, "nl80211: send_frame - Use bss->freq=%u",
6365 bss->freq);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006366 freq = bss->freq;
Dmitry Shmidt56052862013-10-04 10:23:25 -07006367 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006368
Dmitry Shmidt56052862013-10-04 10:23:25 -07006369 if (drv->use_monitor) {
6370 wpa_printf(MSG_DEBUG, "nl80211: send_frame(freq=%u bss->freq=%u) -> send_mntr",
6371 freq, bss->freq);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006372 return wpa_driver_nl80211_send_mntr(drv, data, len,
6373 encrypt, noack);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006374 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006375
Dmitry Shmidt56052862013-10-04 10:23:25 -07006376 wpa_printf(MSG_DEBUG, "nl80211: send_frame -> send_frame_cmd");
Dmitry Shmidt051af732013-10-22 13:52:46 -07006377 res = nl80211_send_frame_cmd(bss, freq, wait_time, data, len,
6378 &cookie, no_cck, noack, offchanok);
6379 if (res == 0 && !noack) {
6380 const struct ieee80211_mgmt *mgmt;
6381 u16 fc;
6382
6383 mgmt = (const struct ieee80211_mgmt *) data;
6384 fc = le_to_host16(mgmt->frame_control);
6385 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
6386 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_ACTION) {
6387 wpa_printf(MSG_MSGDUMP,
6388 "nl80211: Update send_action_cookie from 0x%llx to 0x%llx",
6389 (long long unsigned int)
6390 drv->send_action_cookie,
6391 (long long unsigned int) cookie);
6392 drv->send_action_cookie = cookie;
6393 }
6394 }
6395
6396 return res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006397}
6398
6399
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006400static int wpa_driver_nl80211_send_mlme(struct i802_bss *bss, const u8 *data,
6401 size_t data_len, int noack,
6402 unsigned int freq, int no_cck,
6403 int offchanok,
6404 unsigned int wait_time)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006405{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006406 struct wpa_driver_nl80211_data *drv = bss->drv;
6407 struct ieee80211_mgmt *mgmt;
6408 int encrypt = 1;
6409 u16 fc;
6410
6411 mgmt = (struct ieee80211_mgmt *) data;
6412 fc = le_to_host16(mgmt->frame_control);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006413 wpa_printf(MSG_DEBUG, "nl80211: send_mlme - noack=%d freq=%u no_cck=%d offchanok=%d wait_time=%u fc=0x%x nlmode=%d",
6414 noack, freq, no_cck, offchanok, wait_time, fc, drv->nlmode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006415
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006416 if ((is_sta_interface(drv->nlmode) ||
6417 drv->nlmode == NL80211_IFTYPE_P2P_DEVICE) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006418 WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
6419 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_RESP) {
6420 /*
6421 * The use of last_mgmt_freq is a bit of a hack,
6422 * but it works due to the single-threaded nature
6423 * of wpa_supplicant.
6424 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07006425 if (freq == 0) {
6426 wpa_printf(MSG_DEBUG, "nl80211: Use last_mgmt_freq=%d",
6427 drv->last_mgmt_freq);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006428 freq = drv->last_mgmt_freq;
Dmitry Shmidt56052862013-10-04 10:23:25 -07006429 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006430 return nl80211_send_frame_cmd(bss, freq, 0,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006431 data, data_len, NULL, 1, noack,
6432 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006433 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006434
6435 if (drv->device_ap_sme && is_ap_interface(drv->nlmode)) {
Dmitry Shmidt56052862013-10-04 10:23:25 -07006436 if (freq == 0) {
6437 wpa_printf(MSG_DEBUG, "nl80211: Use bss->freq=%d",
6438 bss->freq);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006439 freq = bss->freq;
Dmitry Shmidt56052862013-10-04 10:23:25 -07006440 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07006441 return nl80211_send_frame_cmd(bss, freq,
6442 (int) freq == bss->freq ? 0 :
6443 wait_time,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006444 data, data_len,
6445 &drv->send_action_cookie,
6446 no_cck, noack, offchanok);
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07006447 }
Dmitry Shmidtb638fe72012-03-20 12:51:25 -07006448
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006449 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
6450 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) {
6451 /*
6452 * Only one of the authentication frame types is encrypted.
6453 * In order for static WEP encryption to work properly (i.e.,
6454 * to not encrypt the frame), we need to tell mac80211 about
6455 * the frames that must not be encrypted.
6456 */
6457 u16 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
6458 u16 auth_trans = le_to_host16(mgmt->u.auth.auth_transaction);
6459 if (auth_alg != WLAN_AUTH_SHARED_KEY || auth_trans != 3)
6460 encrypt = 0;
6461 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006462
Dmitry Shmidt56052862013-10-04 10:23:25 -07006463 wpa_printf(MSG_DEBUG, "nl80211: send_mlme -> send_frame");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006464 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006465 noack, freq, no_cck, offchanok,
6466 wait_time);
6467}
6468
6469
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006470static int nl80211_set_bss(struct i802_bss *bss, int cts, int preamble,
6471 int slot, int ht_opmode, int ap_isolate,
6472 int *basic_rates)
6473{
6474 struct wpa_driver_nl80211_data *drv = bss->drv;
6475 struct nl_msg *msg;
6476
6477 msg = nlmsg_alloc();
6478 if (!msg)
6479 return -ENOMEM;
6480
6481 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_BSS);
6482
6483 if (cts >= 0)
6484 NLA_PUT_U8(msg, NL80211_ATTR_BSS_CTS_PROT, cts);
6485 if (preamble >= 0)
6486 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble);
6487 if (slot >= 0)
6488 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot);
6489 if (ht_opmode >= 0)
6490 NLA_PUT_U16(msg, NL80211_ATTR_BSS_HT_OPMODE, ht_opmode);
6491 if (ap_isolate >= 0)
6492 NLA_PUT_U8(msg, NL80211_ATTR_AP_ISOLATE, ap_isolate);
6493
6494 if (basic_rates) {
6495 u8 rates[NL80211_MAX_SUPP_RATES];
6496 u8 rates_len = 0;
6497 int i;
6498
6499 for (i = 0; i < NL80211_MAX_SUPP_RATES && basic_rates[i] >= 0;
6500 i++)
6501 rates[rates_len++] = basic_rates[i] / 5;
6502
6503 NLA_PUT(msg, NL80211_ATTR_BSS_BASIC_RATES, rates_len, rates);
6504 }
6505
6506 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
6507
6508 return send_and_recv_msgs(drv, msg, NULL, NULL);
6509 nla_put_failure:
6510 nlmsg_free(msg);
6511 return -ENOBUFS;
6512}
6513
6514
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006515static int wpa_driver_nl80211_set_acl(void *priv,
6516 struct hostapd_acl_params *params)
6517{
6518 struct i802_bss *bss = priv;
6519 struct wpa_driver_nl80211_data *drv = bss->drv;
6520 struct nl_msg *msg;
6521 struct nlattr *acl;
6522 unsigned int i;
6523 int ret = 0;
6524
6525 if (!(drv->capa.max_acl_mac_addrs))
6526 return -ENOTSUP;
6527
6528 if (params->num_mac_acl > drv->capa.max_acl_mac_addrs)
6529 return -ENOTSUP;
6530
6531 msg = nlmsg_alloc();
6532 if (!msg)
6533 return -ENOMEM;
6534
6535 wpa_printf(MSG_DEBUG, "nl80211: Set %s ACL (num_mac_acl=%u)",
6536 params->acl_policy ? "Accept" : "Deny", params->num_mac_acl);
6537
6538 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_MAC_ACL);
6539
6540 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
6541
6542 NLA_PUT_U32(msg, NL80211_ATTR_ACL_POLICY, params->acl_policy ?
6543 NL80211_ACL_POLICY_DENY_UNLESS_LISTED :
6544 NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED);
6545
6546 acl = nla_nest_start(msg, NL80211_ATTR_MAC_ADDRS);
6547 if (acl == NULL)
6548 goto nla_put_failure;
6549
6550 for (i = 0; i < params->num_mac_acl; i++)
6551 NLA_PUT(msg, i + 1, ETH_ALEN, params->mac_acl[i].addr);
6552
6553 nla_nest_end(msg, acl);
6554
6555 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6556 msg = NULL;
6557 if (ret) {
6558 wpa_printf(MSG_DEBUG, "nl80211: Failed to set MAC ACL: %d (%s)",
6559 ret, strerror(-ret));
6560 }
6561
6562nla_put_failure:
6563 nlmsg_free(msg);
6564
6565 return ret;
6566}
6567
6568
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006569static int wpa_driver_nl80211_set_ap(void *priv,
6570 struct wpa_driver_ap_params *params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006571{
6572 struct i802_bss *bss = priv;
6573 struct wpa_driver_nl80211_data *drv = bss->drv;
6574 struct nl_msg *msg;
6575 u8 cmd = NL80211_CMD_NEW_BEACON;
6576 int ret;
6577 int beacon_set;
6578 int ifindex = if_nametoindex(bss->ifname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006579 int num_suites;
6580 u32 suites[10];
6581 u32 ver;
6582
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07006583 beacon_set = bss->beacon_set;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006584
6585 msg = nlmsg_alloc();
6586 if (!msg)
6587 return -ENOMEM;
6588
6589 wpa_printf(MSG_DEBUG, "nl80211: Set beacon (beacon_set=%d)",
6590 beacon_set);
6591 if (beacon_set)
6592 cmd = NL80211_CMD_SET_BEACON;
6593
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006594 nl80211_cmd(drv, msg, 0, cmd);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07006595 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon head",
6596 params->head, params->head_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006597 NLA_PUT(msg, NL80211_ATTR_BEACON_HEAD, params->head_len, params->head);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07006598 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon tail",
6599 params->tail, params->tail_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006600 NLA_PUT(msg, NL80211_ATTR_BEACON_TAIL, params->tail_len, params->tail);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07006601 wpa_printf(MSG_DEBUG, "nl80211: ifindex=%d", ifindex);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006602 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07006603 wpa_printf(MSG_DEBUG, "nl80211: beacon_int=%d", params->beacon_int);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006604 NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL, params->beacon_int);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07006605 wpa_printf(MSG_DEBUG, "nl80211: dtim_period=%d", params->dtim_period);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006606 NLA_PUT_U32(msg, NL80211_ATTR_DTIM_PERIOD, params->dtim_period);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07006607 wpa_hexdump_ascii(MSG_DEBUG, "nl80211: ssid",
6608 params->ssid, params->ssid_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006609 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
6610 params->ssid);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07006611 if (params->proberesp && params->proberesp_len) {
6612 wpa_hexdump(MSG_DEBUG, "nl80211: proberesp (offload)",
6613 params->proberesp, params->proberesp_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006614 NLA_PUT(msg, NL80211_ATTR_PROBE_RESP, params->proberesp_len,
6615 params->proberesp);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07006616 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006617 switch (params->hide_ssid) {
6618 case NO_SSID_HIDING:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07006619 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID not in use");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006620 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
6621 NL80211_HIDDEN_SSID_NOT_IN_USE);
6622 break;
6623 case HIDDEN_SSID_ZERO_LEN:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07006624 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero len");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006625 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
6626 NL80211_HIDDEN_SSID_ZERO_LEN);
6627 break;
6628 case HIDDEN_SSID_ZERO_CONTENTS:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07006629 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero contents");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006630 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
6631 NL80211_HIDDEN_SSID_ZERO_CONTENTS);
6632 break;
6633 }
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07006634 wpa_printf(MSG_DEBUG, "nl80211: privacy=%d", params->privacy);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006635 if (params->privacy)
6636 NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07006637 wpa_printf(MSG_DEBUG, "nl80211: auth_algs=0x%x", params->auth_algs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006638 if ((params->auth_algs & (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) ==
6639 (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) {
6640 /* Leave out the attribute */
6641 } else if (params->auth_algs & WPA_AUTH_ALG_SHARED)
6642 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE,
6643 NL80211_AUTHTYPE_SHARED_KEY);
6644 else
6645 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE,
6646 NL80211_AUTHTYPE_OPEN_SYSTEM);
6647
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07006648 wpa_printf(MSG_DEBUG, "nl80211: wpa_version=0x%x", params->wpa_version);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006649 ver = 0;
6650 if (params->wpa_version & WPA_PROTO_WPA)
6651 ver |= NL80211_WPA_VERSION_1;
6652 if (params->wpa_version & WPA_PROTO_RSN)
6653 ver |= NL80211_WPA_VERSION_2;
6654 if (ver)
6655 NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver);
6656
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07006657 wpa_printf(MSG_DEBUG, "nl80211: key_mgmt_suites=0x%x",
6658 params->key_mgmt_suites);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006659 num_suites = 0;
6660 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X)
6661 suites[num_suites++] = WLAN_AKM_SUITE_8021X;
6662 if (params->key_mgmt_suites & WPA_KEY_MGMT_PSK)
6663 suites[num_suites++] = WLAN_AKM_SUITE_PSK;
6664 if (num_suites) {
6665 NLA_PUT(msg, NL80211_ATTR_AKM_SUITES,
6666 num_suites * sizeof(u32), suites);
6667 }
6668
6669 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X &&
6670 params->pairwise_ciphers & (WPA_CIPHER_WEP104 | WPA_CIPHER_WEP40))
6671 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT);
6672
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07006673 wpa_printf(MSG_DEBUG, "nl80211: pairwise_ciphers=0x%x",
6674 params->pairwise_ciphers);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006675 num_suites = 0;
6676 if (params->pairwise_ciphers & WPA_CIPHER_CCMP)
6677 suites[num_suites++] = WLAN_CIPHER_SUITE_CCMP;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006678 if (params->pairwise_ciphers & WPA_CIPHER_GCMP)
6679 suites[num_suites++] = WLAN_CIPHER_SUITE_GCMP;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006680 if (params->pairwise_ciphers & WPA_CIPHER_TKIP)
6681 suites[num_suites++] = WLAN_CIPHER_SUITE_TKIP;
6682 if (params->pairwise_ciphers & WPA_CIPHER_WEP104)
6683 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP104;
6684 if (params->pairwise_ciphers & WPA_CIPHER_WEP40)
6685 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP40;
6686 if (num_suites) {
6687 NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
6688 num_suites * sizeof(u32), suites);
6689 }
6690
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07006691 wpa_printf(MSG_DEBUG, "nl80211: group_cipher=0x%x",
6692 params->group_cipher);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006693 switch (params->group_cipher) {
6694 case WPA_CIPHER_CCMP:
6695 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
6696 WLAN_CIPHER_SUITE_CCMP);
6697 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006698 case WPA_CIPHER_GCMP:
6699 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
6700 WLAN_CIPHER_SUITE_GCMP);
6701 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006702 case WPA_CIPHER_TKIP:
6703 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
6704 WLAN_CIPHER_SUITE_TKIP);
6705 break;
6706 case WPA_CIPHER_WEP104:
6707 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
6708 WLAN_CIPHER_SUITE_WEP104);
6709 break;
6710 case WPA_CIPHER_WEP40:
6711 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
6712 WLAN_CIPHER_SUITE_WEP40);
6713 break;
6714 }
6715
6716 if (params->beacon_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07006717 wpa_hexdump_buf(MSG_DEBUG, "nl80211: beacon_ies",
6718 params->beacon_ies);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006719 NLA_PUT(msg, NL80211_ATTR_IE, wpabuf_len(params->beacon_ies),
6720 wpabuf_head(params->beacon_ies));
6721 }
6722 if (params->proberesp_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07006723 wpa_hexdump_buf(MSG_DEBUG, "nl80211: proberesp_ies",
6724 params->proberesp_ies);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006725 NLA_PUT(msg, NL80211_ATTR_IE_PROBE_RESP,
6726 wpabuf_len(params->proberesp_ies),
6727 wpabuf_head(params->proberesp_ies));
6728 }
6729 if (params->assocresp_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07006730 wpa_hexdump_buf(MSG_DEBUG, "nl80211: assocresp_ies",
6731 params->assocresp_ies);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006732 NLA_PUT(msg, NL80211_ATTR_IE_ASSOC_RESP,
6733 wpabuf_len(params->assocresp_ies),
6734 wpabuf_head(params->assocresp_ies));
6735 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006736
Dmitry Shmidt04949592012-07-19 12:16:46 -07006737 if (drv->capa.flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07006738 wpa_printf(MSG_DEBUG, "nl80211: ap_max_inactivity=%d",
6739 params->ap_max_inactivity);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006740 NLA_PUT_U16(msg, NL80211_ATTR_INACTIVITY_TIMEOUT,
6741 params->ap_max_inactivity);
6742 }
6743
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006744 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6745 if (ret) {
6746 wpa_printf(MSG_DEBUG, "nl80211: Beacon set failed: %d (%s)",
6747 ret, strerror(-ret));
6748 } else {
6749 bss->beacon_set = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006750 nl80211_set_bss(bss, params->cts_protect, params->preamble,
6751 params->short_slot_time, params->ht_opmode,
6752 params->isolate, params->basic_rates);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006753 }
6754 return ret;
6755 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006756 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006757 return -ENOBUFS;
6758}
6759
6760
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006761static int wpa_driver_nl80211_set_freq(struct i802_bss *bss,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006762 struct hostapd_freq_params *freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006763{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006764 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006765 struct nl_msg *msg;
6766 int ret;
6767
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006768 wpa_printf(MSG_DEBUG, "nl80211: Set freq %d (ht_enabled=%d, vht_enabled=%d,"
6769 " bandwidth=%d MHz, cf1=%d MHz, cf2=%d MHz)",
6770 freq->freq, freq->ht_enabled, freq->vht_enabled,
6771 freq->bandwidth, freq->center_freq1, freq->center_freq2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006772 msg = nlmsg_alloc();
6773 if (!msg)
6774 return -1;
6775
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006776 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006777
6778 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006779 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq->freq);
6780 if (freq->vht_enabled) {
6781 switch (freq->bandwidth) {
6782 case 20:
6783 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
6784 NL80211_CHAN_WIDTH_20);
6785 break;
6786 case 40:
6787 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
6788 NL80211_CHAN_WIDTH_40);
6789 break;
6790 case 80:
6791 if (freq->center_freq2)
6792 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
6793 NL80211_CHAN_WIDTH_80P80);
6794 else
6795 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
6796 NL80211_CHAN_WIDTH_80);
6797 break;
6798 case 160:
6799 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
6800 NL80211_CHAN_WIDTH_160);
6801 break;
6802 default:
6803 return -1;
6804 }
6805 NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ1, freq->center_freq1);
6806 if (freq->center_freq2)
6807 NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ2,
6808 freq->center_freq2);
6809 } else if (freq->ht_enabled) {
6810 switch (freq->sec_channel_offset) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006811 case -1:
6812 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
6813 NL80211_CHAN_HT40MINUS);
6814 break;
6815 case 1:
6816 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
6817 NL80211_CHAN_HT40PLUS);
6818 break;
6819 default:
6820 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
6821 NL80211_CHAN_HT20);
6822 break;
6823 }
6824 }
6825
6826 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006827 msg = NULL;
6828 if (ret == 0) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006829 bss->freq = freq->freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006830 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006831 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006832 wpa_printf(MSG_DEBUG, "nl80211: Failed to set channel (freq=%d): "
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006833 "%d (%s)", freq->freq, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006834nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006835 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006836 return -1;
6837}
6838
6839
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006840static u32 sta_flags_nl80211(int flags)
6841{
6842 u32 f = 0;
6843
6844 if (flags & WPA_STA_AUTHORIZED)
6845 f |= BIT(NL80211_STA_FLAG_AUTHORIZED);
6846 if (flags & WPA_STA_WMM)
6847 f |= BIT(NL80211_STA_FLAG_WME);
6848 if (flags & WPA_STA_SHORT_PREAMBLE)
6849 f |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
6850 if (flags & WPA_STA_MFP)
6851 f |= BIT(NL80211_STA_FLAG_MFP);
6852 if (flags & WPA_STA_TDLS_PEER)
6853 f |= BIT(NL80211_STA_FLAG_TDLS_PEER);
6854
6855 return f;
6856}
6857
6858
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006859static int wpa_driver_nl80211_sta_add(void *priv,
6860 struct hostapd_sta_add_params *params)
6861{
6862 struct i802_bss *bss = priv;
6863 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006864 struct nl_msg *msg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006865 struct nl80211_sta_flag_update upd;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006866 int ret = -ENOBUFS;
6867
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006868 if ((params->flags & WPA_STA_TDLS_PEER) &&
6869 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
6870 return -EOPNOTSUPP;
6871
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006872 msg = nlmsg_alloc();
6873 if (!msg)
6874 return -ENOMEM;
6875
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006876 wpa_printf(MSG_DEBUG, "nl80211: %s STA " MACSTR,
6877 params->set ? "Set" : "Add", MAC2STR(params->addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006878 nl80211_cmd(drv, msg, 0, params->set ? NL80211_CMD_SET_STATION :
6879 NL80211_CMD_NEW_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006880
6881 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
6882 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006883 NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_RATES, params->supp_rates_len,
6884 params->supp_rates);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006885 wpa_hexdump(MSG_DEBUG, " * supported rates", params->supp_rates,
6886 params->supp_rates_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006887 if (!params->set) {
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07006888 if (params->aid) {
6889 wpa_printf(MSG_DEBUG, " * aid=%u", params->aid);
6890 NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, params->aid);
6891 } else {
6892 /*
6893 * cfg80211 validates that AID is non-zero, so we have
6894 * to make this a non-zero value for the TDLS case where
6895 * a dummy STA entry is used for now.
6896 */
6897 wpa_printf(MSG_DEBUG, " * aid=1 (TDLS workaround)");
6898 NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, 1);
6899 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006900 wpa_printf(MSG_DEBUG, " * listen_interval=%u",
6901 params->listen_interval);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006902 NLA_PUT_U16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
6903 params->listen_interval);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006904 } else if (params->aid && (params->flags & WPA_STA_TDLS_PEER)) {
6905 wpa_printf(MSG_DEBUG, " * peer_aid=%u", params->aid);
6906 NLA_PUT_U16(msg, NL80211_ATTR_PEER_AID, params->aid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006907 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006908 if (params->ht_capabilities) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006909 wpa_hexdump(MSG_DEBUG, " * ht_capabilities",
6910 (u8 *) params->ht_capabilities,
6911 sizeof(*params->ht_capabilities));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006912 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY,
6913 sizeof(*params->ht_capabilities),
6914 params->ht_capabilities);
6915 }
6916
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006917 if (params->vht_capabilities) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006918 wpa_hexdump(MSG_DEBUG, " * vht_capabilities",
6919 (u8 *) params->vht_capabilities,
6920 sizeof(*params->vht_capabilities));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006921 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY,
6922 sizeof(*params->vht_capabilities),
6923 params->vht_capabilities);
6924 }
6925
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006926 wpa_printf(MSG_DEBUG, " * capability=0x%x", params->capability);
6927 NLA_PUT_U16(msg, NL80211_ATTR_STA_CAPABILITY, params->capability);
6928
6929 if (params->ext_capab) {
6930 wpa_hexdump(MSG_DEBUG, " * ext_capab",
6931 params->ext_capab, params->ext_capab_len);
6932 NLA_PUT(msg, NL80211_ATTR_STA_EXT_CAPABILITY,
6933 params->ext_capab_len, params->ext_capab);
6934 }
6935
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006936 os_memset(&upd, 0, sizeof(upd));
6937 upd.mask = sta_flags_nl80211(params->flags);
6938 upd.set = upd.mask;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006939 wpa_printf(MSG_DEBUG, " * flags set=0x%x mask=0x%x",
6940 upd.set, upd.mask);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006941 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
6942
6943 if (params->flags & WPA_STA_WMM) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006944 struct nlattr *wme = nla_nest_start(msg, NL80211_ATTR_STA_WME);
6945
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006946 if (!wme)
6947 goto nla_put_failure;
6948
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006949 wpa_printf(MSG_DEBUG, " * qosinfo=0x%x", params->qosinfo);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006950 NLA_PUT_U8(msg, NL80211_STA_WME_UAPSD_QUEUES,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006951 params->qosinfo & WMM_QOSINFO_STA_AC_MASK);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006952 NLA_PUT_U8(msg, NL80211_STA_WME_MAX_SP,
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006953 (params->qosinfo >> WMM_QOSINFO_STA_SP_SHIFT) &
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006954 WMM_QOSINFO_STA_SP_MASK);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006955 nla_nest_end(msg, wme);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006956 }
6957
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006958 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006959 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006960 if (ret)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006961 wpa_printf(MSG_DEBUG, "nl80211: NL80211_CMD_%s_STATION "
6962 "result: %d (%s)", params->set ? "SET" : "NEW", ret,
6963 strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006964 if (ret == -EEXIST)
6965 ret = 0;
6966 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006967 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006968 return ret;
6969}
6970
6971
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006972static int wpa_driver_nl80211_sta_remove(struct i802_bss *bss, const u8 *addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006973{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006974 struct wpa_driver_nl80211_data *drv = bss->drv;
6975 struct nl_msg *msg;
6976 int ret;
6977
6978 msg = nlmsg_alloc();
6979 if (!msg)
6980 return -ENOMEM;
6981
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006982 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006983
6984 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
6985 if_nametoindex(bss->ifname));
6986 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
6987
6988 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006989 wpa_printf(MSG_DEBUG, "nl80211: sta_remove -> DEL_STATION %s " MACSTR
6990 " --> %d (%s)",
6991 bss->ifname, MAC2STR(addr), ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006992 if (ret == -ENOENT)
6993 return 0;
6994 return ret;
6995 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006996 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006997 return -ENOBUFS;
6998}
6999
7000
7001static void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv,
7002 int ifidx)
7003{
7004 struct nl_msg *msg;
7005
7006 wpa_printf(MSG_DEBUG, "nl80211: Remove interface ifindex=%d", ifidx);
7007
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007008 /* stop listening for EAPOL on this interface */
7009 del_ifidx(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007010
7011 msg = nlmsg_alloc();
7012 if (!msg)
7013 goto nla_put_failure;
7014
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007015 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_INTERFACE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007016 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifidx);
7017
7018 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
7019 return;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007020 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007021 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007022 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007023 wpa_printf(MSG_ERROR, "Failed to remove interface (ifidx=%d)", ifidx);
7024}
7025
7026
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007027static const char * nl80211_iftype_str(enum nl80211_iftype mode)
7028{
7029 switch (mode) {
7030 case NL80211_IFTYPE_ADHOC:
7031 return "ADHOC";
7032 case NL80211_IFTYPE_STATION:
7033 return "STATION";
7034 case NL80211_IFTYPE_AP:
7035 return "AP";
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07007036 case NL80211_IFTYPE_AP_VLAN:
7037 return "AP_VLAN";
7038 case NL80211_IFTYPE_WDS:
7039 return "WDS";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007040 case NL80211_IFTYPE_MONITOR:
7041 return "MONITOR";
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07007042 case NL80211_IFTYPE_MESH_POINT:
7043 return "MESH_POINT";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007044 case NL80211_IFTYPE_P2P_CLIENT:
7045 return "P2P_CLIENT";
7046 case NL80211_IFTYPE_P2P_GO:
7047 return "P2P_GO";
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007048 case NL80211_IFTYPE_P2P_DEVICE:
7049 return "P2P_DEVICE";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007050 default:
7051 return "unknown";
7052 }
7053}
7054
7055
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007056static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv,
7057 const char *ifname,
7058 enum nl80211_iftype iftype,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007059 const u8 *addr, int wds,
7060 int (*handler)(struct nl_msg *, void *),
7061 void *arg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007062{
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007063 struct nl_msg *msg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007064 int ifidx;
7065 int ret = -ENOBUFS;
7066
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007067 wpa_printf(MSG_DEBUG, "nl80211: Create interface iftype %d (%s)",
7068 iftype, nl80211_iftype_str(iftype));
7069
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007070 msg = nlmsg_alloc();
7071 if (!msg)
7072 return -1;
7073
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007074 nl80211_cmd(drv, msg, 0, NL80211_CMD_NEW_INTERFACE);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007075 if (nl80211_set_iface_id(msg, drv->first_bss) < 0)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007076 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007077 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, ifname);
7078 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, iftype);
7079
7080 if (iftype == NL80211_IFTYPE_MONITOR) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007081 struct nlattr *flags;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007082
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007083 flags = nla_nest_start(msg, NL80211_ATTR_MNTR_FLAGS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007084 if (!flags)
7085 goto nla_put_failure;
7086
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007087 NLA_PUT_FLAG(msg, NL80211_MNTR_FLAG_COOK_FRAMES);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007088
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007089 nla_nest_end(msg, flags);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007090 } else if (wds) {
7091 NLA_PUT_U8(msg, NL80211_ATTR_4ADDR, wds);
7092 }
7093
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007094 ret = send_and_recv_msgs(drv, msg, handler, arg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007095 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007096 if (ret) {
7097 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007098 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007099 wpa_printf(MSG_ERROR, "Failed to create interface %s: %d (%s)",
7100 ifname, ret, strerror(-ret));
7101 return ret;
7102 }
7103
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007104 if (iftype == NL80211_IFTYPE_P2P_DEVICE)
7105 return 0;
7106
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007107 ifidx = if_nametoindex(ifname);
7108 wpa_printf(MSG_DEBUG, "nl80211: New interface %s created: ifindex=%d",
7109 ifname, ifidx);
7110
7111 if (ifidx <= 0)
7112 return -1;
7113
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007114 /* start listening for EAPOL on this interface */
7115 add_ifidx(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007116
7117 if (addr && iftype != NL80211_IFTYPE_MONITOR &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007118 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname, addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007119 nl80211_remove_iface(drv, ifidx);
7120 return -1;
7121 }
7122
7123 return ifidx;
7124}
7125
7126
7127static int nl80211_create_iface(struct wpa_driver_nl80211_data *drv,
7128 const char *ifname, enum nl80211_iftype iftype,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007129 const u8 *addr, int wds,
7130 int (*handler)(struct nl_msg *, void *),
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007131 void *arg, int use_existing)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007132{
7133 int ret;
7134
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007135 ret = nl80211_create_iface_once(drv, ifname, iftype, addr, wds, handler,
7136 arg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007137
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007138 /* if error occurred and interface exists already */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007139 if (ret == -ENFILE && if_nametoindex(ifname)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007140 if (use_existing) {
7141 wpa_printf(MSG_DEBUG, "nl80211: Continue using existing interface %s",
7142 ifname);
7143 return -ENFILE;
7144 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007145 wpa_printf(MSG_INFO, "Try to remove and re-create %s", ifname);
7146
7147 /* Try to remove the interface that was already there. */
7148 nl80211_remove_iface(drv, if_nametoindex(ifname));
7149
7150 /* Try to create the interface again */
7151 ret = nl80211_create_iface_once(drv, ifname, iftype, addr,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007152 wds, handler, arg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007153 }
7154
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007155 if (ret >= 0 && is_p2p_net_interface(iftype))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007156 nl80211_disable_11b_rates(drv, ret, 1);
7157
7158 return ret;
7159}
7160
7161
7162static void handle_tx_callback(void *ctx, u8 *buf, size_t len, int ok)
7163{
7164 struct ieee80211_hdr *hdr;
7165 u16 fc;
7166 union wpa_event_data event;
7167
7168 hdr = (struct ieee80211_hdr *) buf;
7169 fc = le_to_host16(hdr->frame_control);
7170
7171 os_memset(&event, 0, sizeof(event));
7172 event.tx_status.type = WLAN_FC_GET_TYPE(fc);
7173 event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
7174 event.tx_status.dst = hdr->addr1;
7175 event.tx_status.data = buf;
7176 event.tx_status.data_len = len;
7177 event.tx_status.ack = ok;
7178 wpa_supplicant_event(ctx, EVENT_TX_STATUS, &event);
7179}
7180
7181
7182static void from_unknown_sta(struct wpa_driver_nl80211_data *drv,
7183 u8 *buf, size_t len)
7184{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007185 struct ieee80211_hdr *hdr = (void *)buf;
7186 u16 fc;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007187 union wpa_event_data event;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007188
7189 if (len < sizeof(*hdr))
7190 return;
7191
7192 fc = le_to_host16(hdr->frame_control);
7193
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007194 os_memset(&event, 0, sizeof(event));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007195 event.rx_from_unknown.bssid = get_hdr_bssid(hdr, len);
7196 event.rx_from_unknown.addr = hdr->addr2;
7197 event.rx_from_unknown.wds = (fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) ==
7198 (WLAN_FC_FROMDS | WLAN_FC_TODS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007199 wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event);
7200}
7201
7202
7203static void handle_frame(struct wpa_driver_nl80211_data *drv,
7204 u8 *buf, size_t len, int datarate, int ssi_signal)
7205{
7206 struct ieee80211_hdr *hdr;
7207 u16 fc;
7208 union wpa_event_data event;
7209
7210 hdr = (struct ieee80211_hdr *) buf;
7211 fc = le_to_host16(hdr->frame_control);
7212
7213 switch (WLAN_FC_GET_TYPE(fc)) {
7214 case WLAN_FC_TYPE_MGMT:
7215 os_memset(&event, 0, sizeof(event));
7216 event.rx_mgmt.frame = buf;
7217 event.rx_mgmt.frame_len = len;
7218 event.rx_mgmt.datarate = datarate;
7219 event.rx_mgmt.ssi_signal = ssi_signal;
7220 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
7221 break;
7222 case WLAN_FC_TYPE_CTRL:
7223 /* can only get here with PS-Poll frames */
7224 wpa_printf(MSG_DEBUG, "CTRL");
7225 from_unknown_sta(drv, buf, len);
7226 break;
7227 case WLAN_FC_TYPE_DATA:
7228 from_unknown_sta(drv, buf, len);
7229 break;
7230 }
7231}
7232
7233
7234static void handle_monitor_read(int sock, void *eloop_ctx, void *sock_ctx)
7235{
7236 struct wpa_driver_nl80211_data *drv = eloop_ctx;
7237 int len;
7238 unsigned char buf[3000];
7239 struct ieee80211_radiotap_iterator iter;
7240 int ret;
7241 int datarate = 0, ssi_signal = 0;
7242 int injected = 0, failed = 0, rxflags = 0;
7243
7244 len = recv(sock, buf, sizeof(buf), 0);
7245 if (len < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007246 wpa_printf(MSG_ERROR, "nl80211: Monitor socket recv failed: %s",
7247 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007248 return;
7249 }
7250
7251 if (ieee80211_radiotap_iterator_init(&iter, (void*)buf, len)) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007252 wpa_printf(MSG_INFO, "nl80211: received invalid radiotap frame");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007253 return;
7254 }
7255
7256 while (1) {
7257 ret = ieee80211_radiotap_iterator_next(&iter);
7258 if (ret == -ENOENT)
7259 break;
7260 if (ret) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007261 wpa_printf(MSG_INFO, "nl80211: received invalid radiotap frame (%d)",
7262 ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007263 return;
7264 }
7265 switch (iter.this_arg_index) {
7266 case IEEE80211_RADIOTAP_FLAGS:
7267 if (*iter.this_arg & IEEE80211_RADIOTAP_F_FCS)
7268 len -= 4;
7269 break;
7270 case IEEE80211_RADIOTAP_RX_FLAGS:
7271 rxflags = 1;
7272 break;
7273 case IEEE80211_RADIOTAP_TX_FLAGS:
7274 injected = 1;
7275 failed = le_to_host16((*(uint16_t *) iter.this_arg)) &
7276 IEEE80211_RADIOTAP_F_TX_FAIL;
7277 break;
7278 case IEEE80211_RADIOTAP_DATA_RETRIES:
7279 break;
7280 case IEEE80211_RADIOTAP_CHANNEL:
7281 /* TODO: convert from freq/flags to channel number */
7282 break;
7283 case IEEE80211_RADIOTAP_RATE:
7284 datarate = *iter.this_arg * 5;
7285 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007286 case IEEE80211_RADIOTAP_DBM_ANTSIGNAL:
7287 ssi_signal = (s8) *iter.this_arg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007288 break;
7289 }
7290 }
7291
7292 if (rxflags && injected)
7293 return;
7294
7295 if (!injected)
7296 handle_frame(drv, buf + iter.max_length,
7297 len - iter.max_length, datarate, ssi_signal);
7298 else
7299 handle_tx_callback(drv->ctx, buf + iter.max_length,
7300 len - iter.max_length, !failed);
7301}
7302
7303
7304/*
7305 * we post-process the filter code later and rewrite
7306 * this to the offset to the last instruction
7307 */
7308#define PASS 0xFF
7309#define FAIL 0xFE
7310
7311static struct sock_filter msock_filter_insns[] = {
7312 /*
7313 * do a little-endian load of the radiotap length field
7314 */
7315 /* load lower byte into A */
7316 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 2),
7317 /* put it into X (== index register) */
7318 BPF_STMT(BPF_MISC| BPF_TAX, 0),
7319 /* load upper byte into A */
7320 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 3),
7321 /* left-shift it by 8 */
7322 BPF_STMT(BPF_ALU | BPF_LSH | BPF_K, 8),
7323 /* or with X */
7324 BPF_STMT(BPF_ALU | BPF_OR | BPF_X, 0),
7325 /* put result into X */
7326 BPF_STMT(BPF_MISC| BPF_TAX, 0),
7327
7328 /*
7329 * Allow management frames through, this also gives us those
7330 * management frames that we sent ourselves with status
7331 */
7332 /* load the lower byte of the IEEE 802.11 frame control field */
7333 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
7334 /* mask off frame type and version */
7335 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0xF),
7336 /* accept frame if it's both 0, fall through otherwise */
7337 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0, PASS, 0),
7338
7339 /*
7340 * TODO: add a bit to radiotap RX flags that indicates
7341 * that the sending station is not associated, then
7342 * add a filter here that filters on our DA and that flag
7343 * to allow us to deauth frames to that bad station.
7344 *
7345 * For now allow all To DS data frames through.
7346 */
7347 /* load the IEEE 802.11 frame control field */
7348 BPF_STMT(BPF_LD | BPF_H | BPF_IND, 0),
7349 /* mask off frame type, version and DS status */
7350 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0F03),
7351 /* accept frame if version 0, type 2 and To DS, fall through otherwise
7352 */
7353 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0801, PASS, 0),
7354
7355#if 0
7356 /*
7357 * drop non-data frames
7358 */
7359 /* load the lower byte of the frame control field */
7360 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
7361 /* mask off QoS bit */
7362 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0c),
7363 /* drop non-data frames */
7364 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 8, 0, FAIL),
7365#endif
7366 /* load the upper byte of the frame control field */
7367 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 1),
7368 /* mask off toDS/fromDS */
7369 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x03),
7370 /* accept WDS frames */
7371 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 3, PASS, 0),
7372
7373 /*
7374 * add header length to index
7375 */
7376 /* load the lower byte of the frame control field */
7377 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
7378 /* mask off QoS bit */
7379 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x80),
7380 /* right shift it by 6 to give 0 or 2 */
7381 BPF_STMT(BPF_ALU | BPF_RSH | BPF_K, 6),
7382 /* add data frame header length */
7383 BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 24),
7384 /* add index, was start of 802.11 header */
7385 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
7386 /* move to index, now start of LL header */
7387 BPF_STMT(BPF_MISC | BPF_TAX, 0),
7388
7389 /*
7390 * Accept empty data frames, we use those for
7391 * polling activity.
7392 */
7393 BPF_STMT(BPF_LD | BPF_W | BPF_LEN, 0),
7394 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_X, 0, PASS, 0),
7395
7396 /*
7397 * Accept EAPOL frames
7398 */
7399 BPF_STMT(BPF_LD | BPF_W | BPF_IND, 0),
7400 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0xAAAA0300, 0, FAIL),
7401 BPF_STMT(BPF_LD | BPF_W | BPF_IND, 4),
7402 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0000888E, PASS, FAIL),
7403
7404 /* keep these last two statements or change the code below */
7405 /* return 0 == "DROP" */
7406 BPF_STMT(BPF_RET | BPF_K, 0),
7407 /* return ~0 == "keep all" */
7408 BPF_STMT(BPF_RET | BPF_K, ~0),
7409};
7410
7411static struct sock_fprog msock_filter = {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007412 .len = ARRAY_SIZE(msock_filter_insns),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007413 .filter = msock_filter_insns,
7414};
7415
7416
7417static int add_monitor_filter(int s)
7418{
7419 int idx;
7420
7421 /* rewrite all PASS/FAIL jump offsets */
7422 for (idx = 0; idx < msock_filter.len; idx++) {
7423 struct sock_filter *insn = &msock_filter_insns[idx];
7424
7425 if (BPF_CLASS(insn->code) == BPF_JMP) {
7426 if (insn->code == (BPF_JMP|BPF_JA)) {
7427 if (insn->k == PASS)
7428 insn->k = msock_filter.len - idx - 2;
7429 else if (insn->k == FAIL)
7430 insn->k = msock_filter.len - idx - 3;
7431 }
7432
7433 if (insn->jt == PASS)
7434 insn->jt = msock_filter.len - idx - 2;
7435 else if (insn->jt == FAIL)
7436 insn->jt = msock_filter.len - idx - 3;
7437
7438 if (insn->jf == PASS)
7439 insn->jf = msock_filter.len - idx - 2;
7440 else if (insn->jf == FAIL)
7441 insn->jf = msock_filter.len - idx - 3;
7442 }
7443 }
7444
7445 if (setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER,
7446 &msock_filter, sizeof(msock_filter))) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007447 wpa_printf(MSG_ERROR, "nl80211: setsockopt(SO_ATTACH_FILTER) failed: %s",
7448 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007449 return -1;
7450 }
7451
7452 return 0;
7453}
7454
7455
7456static void nl80211_remove_monitor_interface(
7457 struct wpa_driver_nl80211_data *drv)
7458{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007459 if (drv->monitor_refcount > 0)
7460 drv->monitor_refcount--;
7461 wpa_printf(MSG_DEBUG, "nl80211: Remove monitor interface: refcount=%d",
7462 drv->monitor_refcount);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007463 if (drv->monitor_refcount > 0)
7464 return;
7465
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007466 if (drv->monitor_ifidx >= 0) {
7467 nl80211_remove_iface(drv, drv->monitor_ifidx);
7468 drv->monitor_ifidx = -1;
7469 }
7470 if (drv->monitor_sock >= 0) {
7471 eloop_unregister_read_sock(drv->monitor_sock);
7472 close(drv->monitor_sock);
7473 drv->monitor_sock = -1;
7474 }
7475}
7476
7477
7478static int
7479nl80211_create_monitor_interface(struct wpa_driver_nl80211_data *drv)
7480{
7481 char buf[IFNAMSIZ];
7482 struct sockaddr_ll ll;
7483 int optval;
7484 socklen_t optlen;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007485
7486 if (drv->monitor_ifidx >= 0) {
7487 drv->monitor_refcount++;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007488 wpa_printf(MSG_DEBUG, "nl80211: Re-use existing monitor interface: refcount=%d",
7489 drv->monitor_refcount);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007490 return 0;
7491 }
7492
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007493 if (os_strncmp(drv->first_bss->ifname, "p2p-", 4) == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007494 /*
7495 * P2P interface name is of the format p2p-%s-%d. For monitor
7496 * interface name corresponding to P2P GO, replace "p2p-" with
7497 * "mon-" to retain the same interface name length and to
7498 * indicate that it is a monitor interface.
7499 */
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007500 snprintf(buf, IFNAMSIZ, "mon-%s", drv->first_bss->ifname + 4);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007501 } else {
7502 /* Non-P2P interface with AP functionality. */
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007503 snprintf(buf, IFNAMSIZ, "mon.%s", drv->first_bss->ifname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007504 }
7505
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007506 buf[IFNAMSIZ - 1] = '\0';
7507
7508 drv->monitor_ifidx =
7509 nl80211_create_iface(drv, buf, NL80211_IFTYPE_MONITOR, NULL,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007510 0, NULL, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007511
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07007512 if (drv->monitor_ifidx == -EOPNOTSUPP) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007513 /*
7514 * This is backward compatibility for a few versions of
7515 * the kernel only that didn't advertise the right
7516 * attributes for the only driver that then supported
7517 * AP mode w/o monitor -- ath6kl.
7518 */
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07007519 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support "
7520 "monitor interface type - try to run without it");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007521 drv->device_ap_sme = 1;
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07007522 }
7523
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007524 if (drv->monitor_ifidx < 0)
7525 return -1;
7526
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007527 if (linux_set_iface_flags(drv->global->ioctl_sock, buf, 1))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007528 goto error;
7529
7530 memset(&ll, 0, sizeof(ll));
7531 ll.sll_family = AF_PACKET;
7532 ll.sll_ifindex = drv->monitor_ifidx;
7533 drv->monitor_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
7534 if (drv->monitor_sock < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007535 wpa_printf(MSG_ERROR, "nl80211: socket[PF_PACKET,SOCK_RAW] failed: %s",
7536 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007537 goto error;
7538 }
7539
7540 if (add_monitor_filter(drv->monitor_sock)) {
7541 wpa_printf(MSG_INFO, "Failed to set socket filter for monitor "
7542 "interface; do filtering in user space");
7543 /* This works, but will cost in performance. */
7544 }
7545
7546 if (bind(drv->monitor_sock, (struct sockaddr *) &ll, sizeof(ll)) < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007547 wpa_printf(MSG_ERROR, "nl80211: monitor socket bind failed: %s",
7548 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007549 goto error;
7550 }
7551
7552 optlen = sizeof(optval);
7553 optval = 20;
7554 if (setsockopt
7555 (drv->monitor_sock, SOL_SOCKET, SO_PRIORITY, &optval, optlen)) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007556 wpa_printf(MSG_ERROR, "nl80211: Failed to set socket priority: %s",
7557 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007558 goto error;
7559 }
7560
7561 if (eloop_register_read_sock(drv->monitor_sock, handle_monitor_read,
7562 drv, NULL)) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007563 wpa_printf(MSG_INFO, "nl80211: Could not register monitor read socket");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007564 goto error;
7565 }
7566
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007567 drv->monitor_refcount++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007568 return 0;
7569 error:
7570 nl80211_remove_monitor_interface(drv);
7571 return -1;
7572}
7573
7574
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007575static int nl80211_setup_ap(struct i802_bss *bss)
7576{
7577 struct wpa_driver_nl80211_data *drv = bss->drv;
7578
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007579 wpa_printf(MSG_DEBUG, "nl80211: Setup AP(%s) - device_ap_sme=%d use_monitor=%d",
7580 bss->ifname, drv->device_ap_sme, drv->use_monitor);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007581
7582 /*
7583 * Disable Probe Request reporting unless we need it in this way for
7584 * devices that include the AP SME, in the other case (unless using
7585 * monitor iface) we'll get it through the nl_mgmt socket instead.
7586 */
7587 if (!drv->device_ap_sme)
7588 wpa_driver_nl80211_probe_req_report(bss, 0);
7589
7590 if (!drv->device_ap_sme && !drv->use_monitor)
7591 if (nl80211_mgmt_subscribe_ap(bss))
7592 return -1;
7593
7594 if (drv->device_ap_sme && !drv->use_monitor)
7595 if (nl80211_mgmt_subscribe_ap_dev_sme(bss))
7596 return -1;
7597
7598 if (!drv->device_ap_sme && drv->use_monitor &&
7599 nl80211_create_monitor_interface(drv) &&
7600 !drv->device_ap_sme)
Dmitry Shmidt04949592012-07-19 12:16:46 -07007601 return -1;
7602
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007603 if (drv->device_ap_sme &&
7604 wpa_driver_nl80211_probe_req_report(bss, 1) < 0) {
7605 wpa_printf(MSG_DEBUG, "nl80211: Failed to enable "
7606 "Probe Request frame reporting in AP mode");
7607 /* Try to survive without this */
7608 }
7609
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007610 return 0;
7611}
7612
7613
7614static void nl80211_teardown_ap(struct i802_bss *bss)
7615{
7616 struct wpa_driver_nl80211_data *drv = bss->drv;
7617
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007618 wpa_printf(MSG_DEBUG, "nl80211: Teardown AP(%s) - device_ap_sme=%d use_monitor=%d",
7619 bss->ifname, drv->device_ap_sme, drv->use_monitor);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007620 if (drv->device_ap_sme) {
7621 wpa_driver_nl80211_probe_req_report(bss, 0);
7622 if (!drv->use_monitor)
7623 nl80211_mgmt_unsubscribe(bss, "AP teardown (dev SME)");
7624 } else if (drv->use_monitor)
7625 nl80211_remove_monitor_interface(drv);
7626 else
7627 nl80211_mgmt_unsubscribe(bss, "AP teardown");
7628
7629 bss->beacon_set = 0;
7630}
7631
7632
7633static int nl80211_send_eapol_data(struct i802_bss *bss,
7634 const u8 *addr, const u8 *data,
7635 size_t data_len)
7636{
7637 struct sockaddr_ll ll;
7638 int ret;
7639
7640 if (bss->drv->eapol_tx_sock < 0) {
7641 wpa_printf(MSG_DEBUG, "nl80211: No socket to send EAPOL");
7642 return -1;
7643 }
7644
7645 os_memset(&ll, 0, sizeof(ll));
7646 ll.sll_family = AF_PACKET;
7647 ll.sll_ifindex = bss->ifindex;
7648 ll.sll_protocol = htons(ETH_P_PAE);
7649 ll.sll_halen = ETH_ALEN;
7650 os_memcpy(ll.sll_addr, addr, ETH_ALEN);
7651 ret = sendto(bss->drv->eapol_tx_sock, data, data_len, 0,
7652 (struct sockaddr *) &ll, sizeof(ll));
7653 if (ret < 0)
7654 wpa_printf(MSG_ERROR, "nl80211: EAPOL TX: %s",
7655 strerror(errno));
7656
7657 return ret;
7658}
7659
7660
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007661static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
7662
7663static int wpa_driver_nl80211_hapd_send_eapol(
7664 void *priv, const u8 *addr, const u8 *data,
7665 size_t data_len, int encrypt, const u8 *own_addr, u32 flags)
7666{
7667 struct i802_bss *bss = priv;
7668 struct wpa_driver_nl80211_data *drv = bss->drv;
7669 struct ieee80211_hdr *hdr;
7670 size_t len;
7671 u8 *pos;
7672 int res;
7673 int qos = flags & WPA_STA_WMM;
Dmitry Shmidt641185e2013-11-06 15:17:13 -08007674
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007675 if (drv->device_ap_sme || !drv->use_monitor)
7676 return nl80211_send_eapol_data(bss, addr, data, data_len);
7677
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007678 len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
7679 data_len;
7680 hdr = os_zalloc(len);
7681 if (hdr == NULL) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007682 wpa_printf(MSG_INFO, "nl80211: Failed to allocate EAPOL buffer(len=%lu)",
7683 (unsigned long) len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007684 return -1;
7685 }
7686
7687 hdr->frame_control =
7688 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
7689 hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
7690 if (encrypt)
7691 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
7692 if (qos) {
7693 hdr->frame_control |=
7694 host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
7695 }
7696
7697 memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
7698 memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
7699 memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
7700 pos = (u8 *) (hdr + 1);
7701
7702 if (qos) {
Dmitry Shmidtaa532512012-09-24 10:35:31 -07007703 /* Set highest priority in QoS header */
7704 pos[0] = 7;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007705 pos[1] = 0;
7706 pos += 2;
7707 }
7708
7709 memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
7710 pos += sizeof(rfc1042_header);
7711 WPA_PUT_BE16(pos, ETH_P_PAE);
7712 pos += 2;
7713 memcpy(pos, data, data_len);
7714
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007715 res = wpa_driver_nl80211_send_frame(bss, (u8 *) hdr, len, encrypt, 0,
7716 0, 0, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007717 if (res < 0) {
7718 wpa_printf(MSG_ERROR, "i802_send_eapol - packet len: %lu - "
7719 "failed: %d (%s)",
7720 (unsigned long) len, errno, strerror(errno));
7721 }
7722 os_free(hdr);
7723
7724 return res;
7725}
7726
7727
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007728static int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr,
7729 int total_flags,
7730 int flags_or, int flags_and)
7731{
7732 struct i802_bss *bss = priv;
7733 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007734 struct nl_msg *msg;
7735 struct nlattr *flags;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007736 struct nl80211_sta_flag_update upd;
7737
7738 msg = nlmsg_alloc();
7739 if (!msg)
7740 return -ENOMEM;
7741
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007742 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007743
7744 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
7745 if_nametoindex(bss->ifname));
7746 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
7747
7748 /*
7749 * Backwards compatibility version using NL80211_ATTR_STA_FLAGS. This
7750 * can be removed eventually.
7751 */
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007752 flags = nla_nest_start(msg, NL80211_ATTR_STA_FLAGS);
7753 if (!flags)
7754 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007755 if (total_flags & WPA_STA_AUTHORIZED)
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007756 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_AUTHORIZED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007757
7758 if (total_flags & WPA_STA_WMM)
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007759 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_WME);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007760
7761 if (total_flags & WPA_STA_SHORT_PREAMBLE)
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007762 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_SHORT_PREAMBLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007763
7764 if (total_flags & WPA_STA_MFP)
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007765 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_MFP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007766
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007767 if (total_flags & WPA_STA_TDLS_PEER)
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007768 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_TDLS_PEER);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007769
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007770 nla_nest_end(msg, flags);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007771
7772 os_memset(&upd, 0, sizeof(upd));
7773 upd.mask = sta_flags_nl80211(flags_or | ~flags_and);
7774 upd.set = sta_flags_nl80211(flags_or);
7775 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
7776
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007777 return send_and_recv_msgs(drv, msg, NULL, NULL);
7778 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007779 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007780 return -ENOBUFS;
7781}
7782
7783
7784static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv,
7785 struct wpa_driver_associate_params *params)
7786{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007787 enum nl80211_iftype nlmode, old_mode;
7788 struct hostapd_freq_params freq = {
7789 .freq = params->freq,
7790 };
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007791
7792 if (params->p2p) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007793 wpa_printf(MSG_DEBUG, "nl80211: Setup AP operations for P2P "
7794 "group (GO)");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007795 nlmode = NL80211_IFTYPE_P2P_GO;
7796 } else
7797 nlmode = NL80211_IFTYPE_AP;
7798
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007799 old_mode = drv->nlmode;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007800 if (wpa_driver_nl80211_set_mode(drv->first_bss, nlmode)) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007801 nl80211_remove_monitor_interface(drv);
7802 return -1;
7803 }
7804
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007805 if (wpa_driver_nl80211_set_freq(drv->first_bss, &freq)) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007806 if (old_mode != nlmode)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007807 wpa_driver_nl80211_set_mode(drv->first_bss, old_mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007808 nl80211_remove_monitor_interface(drv);
7809 return -1;
7810 }
7811
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007812 return 0;
7813}
7814
7815
7816static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv)
7817{
7818 struct nl_msg *msg;
7819 int ret = -1;
7820
7821 msg = nlmsg_alloc();
7822 if (!msg)
7823 return -1;
7824
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007825 nl80211_cmd(drv, msg, 0, NL80211_CMD_LEAVE_IBSS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007826 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
7827 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7828 msg = NULL;
7829 if (ret) {
7830 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS failed: ret=%d "
7831 "(%s)", ret, strerror(-ret));
7832 goto nla_put_failure;
7833 }
7834
7835 ret = 0;
7836 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS request sent successfully");
7837
7838nla_put_failure:
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007839 if (wpa_driver_nl80211_set_mode(drv->first_bss,
Dmitry Shmidt56052862013-10-04 10:23:25 -07007840 NL80211_IFTYPE_STATION)) {
7841 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
7842 "station mode");
7843 }
7844
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007845 nlmsg_free(msg);
7846 return ret;
7847}
7848
7849
7850static int wpa_driver_nl80211_ibss(struct wpa_driver_nl80211_data *drv,
7851 struct wpa_driver_associate_params *params)
7852{
7853 struct nl_msg *msg;
7854 int ret = -1;
7855 int count = 0;
7856
7857 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS (ifindex=%d)", drv->ifindex);
7858
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007859 if (wpa_driver_nl80211_set_mode(drv->first_bss,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007860 NL80211_IFTYPE_ADHOC)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007861 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
7862 "IBSS mode");
7863 return -1;
7864 }
7865
7866retry:
7867 msg = nlmsg_alloc();
7868 if (!msg)
7869 return -1;
7870
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007871 nl80211_cmd(drv, msg, 0, NL80211_CMD_JOIN_IBSS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007872 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
7873
7874 if (params->ssid == NULL || params->ssid_len > sizeof(drv->ssid))
7875 goto nla_put_failure;
7876
7877 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
7878 params->ssid, params->ssid_len);
7879 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
7880 params->ssid);
7881 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
7882 drv->ssid_len = params->ssid_len;
7883
7884 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
7885 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
7886
7887 ret = nl80211_set_conn_keys(params, msg);
7888 if (ret)
7889 goto nla_put_failure;
7890
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007891 if (params->bssid && params->fixed_bssid) {
7892 wpa_printf(MSG_DEBUG, " * BSSID=" MACSTR,
7893 MAC2STR(params->bssid));
7894 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
7895 }
7896
7897 if (params->key_mgmt_suite == KEY_MGMT_802_1X ||
7898 params->key_mgmt_suite == KEY_MGMT_PSK ||
7899 params->key_mgmt_suite == KEY_MGMT_802_1X_SHA256 ||
7900 params->key_mgmt_suite == KEY_MGMT_PSK_SHA256) {
7901 wpa_printf(MSG_DEBUG, " * control port");
7902 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT);
7903 }
7904
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007905 if (params->wpa_ie) {
7906 wpa_hexdump(MSG_DEBUG,
7907 " * Extra IEs for Beacon/Probe Response frames",
7908 params->wpa_ie, params->wpa_ie_len);
7909 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
7910 params->wpa_ie);
7911 }
7912
7913 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7914 msg = NULL;
7915 if (ret) {
7916 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS failed: ret=%d (%s)",
7917 ret, strerror(-ret));
7918 count++;
7919 if (ret == -EALREADY && count == 1) {
7920 wpa_printf(MSG_DEBUG, "nl80211: Retry IBSS join after "
7921 "forced leave");
7922 nl80211_leave_ibss(drv);
7923 nlmsg_free(msg);
7924 goto retry;
7925 }
7926
7927 goto nla_put_failure;
7928 }
7929 ret = 0;
7930 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS request sent successfully");
7931
7932nla_put_failure:
7933 nlmsg_free(msg);
7934 return ret;
7935}
7936
7937
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007938static int wpa_driver_nl80211_try_connect(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007939 struct wpa_driver_nl80211_data *drv,
7940 struct wpa_driver_associate_params *params)
7941{
7942 struct nl_msg *msg;
7943 enum nl80211_auth_type type;
7944 int ret = 0;
7945 int algs;
7946
7947 msg = nlmsg_alloc();
7948 if (!msg)
7949 return -1;
7950
7951 wpa_printf(MSG_DEBUG, "nl80211: Connect (ifindex=%d)", drv->ifindex);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007952 nl80211_cmd(drv, msg, 0, NL80211_CMD_CONNECT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007953
7954 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
7955 if (params->bssid) {
7956 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
7957 MAC2STR(params->bssid));
7958 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
7959 }
7960 if (params->freq) {
7961 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
7962 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007963 drv->assoc_freq = params->freq;
7964 } else
7965 drv->assoc_freq = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007966 if (params->bg_scan_period >= 0) {
7967 wpa_printf(MSG_DEBUG, " * bg scan period=%d",
7968 params->bg_scan_period);
7969 NLA_PUT_U16(msg, NL80211_ATTR_BG_SCAN_PERIOD,
7970 params->bg_scan_period);
7971 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007972 if (params->ssid) {
7973 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
7974 params->ssid, params->ssid_len);
7975 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
7976 params->ssid);
7977 if (params->ssid_len > sizeof(drv->ssid))
7978 goto nla_put_failure;
7979 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
7980 drv->ssid_len = params->ssid_len;
7981 }
7982 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
7983 if (params->wpa_ie)
7984 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
7985 params->wpa_ie);
7986
7987 algs = 0;
7988 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
7989 algs++;
7990 if (params->auth_alg & WPA_AUTH_ALG_SHARED)
7991 algs++;
7992 if (params->auth_alg & WPA_AUTH_ALG_LEAP)
7993 algs++;
7994 if (algs > 1) {
7995 wpa_printf(MSG_DEBUG, " * Leave out Auth Type for automatic "
7996 "selection");
7997 goto skip_auth_type;
7998 }
7999
8000 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
8001 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
8002 else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
8003 type = NL80211_AUTHTYPE_SHARED_KEY;
8004 else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
8005 type = NL80211_AUTHTYPE_NETWORK_EAP;
8006 else if (params->auth_alg & WPA_AUTH_ALG_FT)
8007 type = NL80211_AUTHTYPE_FT;
8008 else
8009 goto nla_put_failure;
8010
8011 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
8012 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
8013
8014skip_auth_type:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008015 if (params->wpa_proto) {
8016 enum nl80211_wpa_versions ver = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008017
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008018 if (params->wpa_proto & WPA_PROTO_WPA)
8019 ver |= NL80211_WPA_VERSION_1;
8020 if (params->wpa_proto & WPA_PROTO_RSN)
8021 ver |= NL80211_WPA_VERSION_2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008022
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008023 wpa_printf(MSG_DEBUG, " * WPA Versions 0x%x", ver);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008024 NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver);
8025 }
8026
8027 if (params->pairwise_suite != CIPHER_NONE) {
8028 int cipher;
8029
8030 switch (params->pairwise_suite) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08008031 case CIPHER_SMS4:
8032 cipher = WLAN_CIPHER_SUITE_SMS4;
8033 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008034 case CIPHER_WEP40:
8035 cipher = WLAN_CIPHER_SUITE_WEP40;
8036 break;
8037 case CIPHER_WEP104:
8038 cipher = WLAN_CIPHER_SUITE_WEP104;
8039 break;
8040 case CIPHER_CCMP:
8041 cipher = WLAN_CIPHER_SUITE_CCMP;
8042 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008043 case CIPHER_GCMP:
8044 cipher = WLAN_CIPHER_SUITE_GCMP;
8045 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008046 case CIPHER_TKIP:
8047 default:
8048 cipher = WLAN_CIPHER_SUITE_TKIP;
8049 break;
8050 }
8051 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE, cipher);
8052 }
8053
8054 if (params->group_suite != CIPHER_NONE) {
8055 int cipher;
8056
8057 switch (params->group_suite) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08008058 case CIPHER_SMS4:
8059 cipher = WLAN_CIPHER_SUITE_SMS4;
8060 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008061 case CIPHER_WEP40:
8062 cipher = WLAN_CIPHER_SUITE_WEP40;
8063 break;
8064 case CIPHER_WEP104:
8065 cipher = WLAN_CIPHER_SUITE_WEP104;
8066 break;
8067 case CIPHER_CCMP:
8068 cipher = WLAN_CIPHER_SUITE_CCMP;
8069 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008070 case CIPHER_GCMP:
8071 cipher = WLAN_CIPHER_SUITE_GCMP;
8072 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008073 case CIPHER_TKIP:
8074 default:
8075 cipher = WLAN_CIPHER_SUITE_TKIP;
8076 break;
8077 }
8078 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher);
8079 }
8080
8081 if (params->key_mgmt_suite == KEY_MGMT_802_1X ||
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08008082 params->key_mgmt_suite == KEY_MGMT_PSK ||
Dmitry Shmidt700a1372013-03-15 14:14:44 -07008083 params->key_mgmt_suite == KEY_MGMT_FT_802_1X ||
8084 params->key_mgmt_suite == KEY_MGMT_FT_PSK ||
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08008085 params->key_mgmt_suite == KEY_MGMT_CCKM) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008086 int mgmt = WLAN_AKM_SUITE_PSK;
8087
8088 switch (params->key_mgmt_suite) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08008089 case KEY_MGMT_CCKM:
8090 mgmt = WLAN_AKM_SUITE_CCKM;
8091 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008092 case KEY_MGMT_802_1X:
8093 mgmt = WLAN_AKM_SUITE_8021X;
8094 break;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07008095 case KEY_MGMT_FT_802_1X:
8096 mgmt = WLAN_AKM_SUITE_FT_8021X;
8097 break;
8098 case KEY_MGMT_FT_PSK:
8099 mgmt = WLAN_AKM_SUITE_FT_PSK;
8100 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008101 case KEY_MGMT_PSK:
8102 default:
8103 mgmt = WLAN_AKM_SUITE_PSK;
8104 break;
8105 }
8106 NLA_PUT_U32(msg, NL80211_ATTR_AKM_SUITES, mgmt);
8107 }
8108
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008109#ifdef CONFIG_IEEE80211W
8110 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED)
8111 NLA_PUT_U32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED);
8112#endif /* CONFIG_IEEE80211W */
8113
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008114 if (params->disable_ht)
8115 NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_HT);
8116
8117 if (params->htcaps && params->htcaps_mask) {
8118 int sz = sizeof(struct ieee80211_ht_capabilities);
8119 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY, sz, params->htcaps);
8120 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY_MASK, sz,
8121 params->htcaps_mask);
8122 }
8123
Dmitry Shmidt2f023192013-03-12 12:44:17 -07008124#ifdef CONFIG_VHT_OVERRIDES
8125 if (params->disable_vht) {
8126 wpa_printf(MSG_DEBUG, " * VHT disabled");
8127 NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_VHT);
8128 }
8129
8130 if (params->vhtcaps && params->vhtcaps_mask) {
8131 int sz = sizeof(struct ieee80211_vht_capabilities);
8132 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY, sz, params->vhtcaps);
8133 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY_MASK, sz,
8134 params->vhtcaps_mask);
8135 }
8136#endif /* CONFIG_VHT_OVERRIDES */
8137
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008138 ret = nl80211_set_conn_keys(params, msg);
8139 if (ret)
8140 goto nla_put_failure;
8141
8142 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8143 msg = NULL;
8144 if (ret) {
8145 wpa_printf(MSG_DEBUG, "nl80211: MLME connect failed: ret=%d "
8146 "(%s)", ret, strerror(-ret));
8147 goto nla_put_failure;
8148 }
8149 ret = 0;
8150 wpa_printf(MSG_DEBUG, "nl80211: Connect request send successfully");
8151
8152nla_put_failure:
8153 nlmsg_free(msg);
8154 return ret;
8155
8156}
8157
8158
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08008159static int wpa_driver_nl80211_connect(
8160 struct wpa_driver_nl80211_data *drv,
8161 struct wpa_driver_associate_params *params)
8162{
8163 int ret = wpa_driver_nl80211_try_connect(drv, params);
8164 if (ret == -EALREADY) {
8165 /*
8166 * cfg80211 does not currently accept new connections if
8167 * we are already connected. As a workaround, force
8168 * disconnection and try again.
8169 */
8170 wpa_printf(MSG_DEBUG, "nl80211: Explicitly "
8171 "disconnecting before reassociation "
8172 "attempt");
8173 if (wpa_driver_nl80211_disconnect(
8174 drv, WLAN_REASON_PREV_AUTH_NOT_VALID))
8175 return -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08008176 ret = wpa_driver_nl80211_try_connect(drv, params);
8177 }
8178 return ret;
8179}
8180
8181
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008182static int wpa_driver_nl80211_associate(
8183 void *priv, struct wpa_driver_associate_params *params)
8184{
8185 struct i802_bss *bss = priv;
8186 struct wpa_driver_nl80211_data *drv = bss->drv;
8187 int ret = -1;
8188 struct nl_msg *msg;
8189
8190 if (params->mode == IEEE80211_MODE_AP)
8191 return wpa_driver_nl80211_ap(drv, params);
8192
8193 if (params->mode == IEEE80211_MODE_IBSS)
8194 return wpa_driver_nl80211_ibss(drv, params);
8195
8196 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008197 enum nl80211_iftype nlmode = params->p2p ?
8198 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
8199
8200 if (wpa_driver_nl80211_set_mode(priv, nlmode) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008201 return -1;
8202 return wpa_driver_nl80211_connect(drv, params);
8203 }
8204
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07008205 nl80211_mark_disconnected(drv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008206
8207 msg = nlmsg_alloc();
8208 if (!msg)
8209 return -1;
8210
8211 wpa_printf(MSG_DEBUG, "nl80211: Associate (ifindex=%d)",
8212 drv->ifindex);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008213 nl80211_cmd(drv, msg, 0, NL80211_CMD_ASSOCIATE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008214
8215 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
8216 if (params->bssid) {
8217 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
8218 MAC2STR(params->bssid));
8219 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
8220 }
8221 if (params->freq) {
8222 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
8223 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
8224 drv->assoc_freq = params->freq;
8225 } else
8226 drv->assoc_freq = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008227 if (params->bg_scan_period >= 0) {
8228 wpa_printf(MSG_DEBUG, " * bg scan period=%d",
8229 params->bg_scan_period);
8230 NLA_PUT_U16(msg, NL80211_ATTR_BG_SCAN_PERIOD,
8231 params->bg_scan_period);
8232 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008233 if (params->ssid) {
8234 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
8235 params->ssid, params->ssid_len);
8236 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
8237 params->ssid);
8238 if (params->ssid_len > sizeof(drv->ssid))
8239 goto nla_put_failure;
8240 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
8241 drv->ssid_len = params->ssid_len;
8242 }
8243 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
8244 if (params->wpa_ie)
8245 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
8246 params->wpa_ie);
8247
8248 if (params->pairwise_suite != CIPHER_NONE) {
8249 int cipher;
8250
8251 switch (params->pairwise_suite) {
8252 case CIPHER_WEP40:
8253 cipher = WLAN_CIPHER_SUITE_WEP40;
8254 break;
8255 case CIPHER_WEP104:
8256 cipher = WLAN_CIPHER_SUITE_WEP104;
8257 break;
8258 case CIPHER_CCMP:
8259 cipher = WLAN_CIPHER_SUITE_CCMP;
8260 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008261 case CIPHER_GCMP:
8262 cipher = WLAN_CIPHER_SUITE_GCMP;
8263 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008264 case CIPHER_TKIP:
8265 default:
8266 cipher = WLAN_CIPHER_SUITE_TKIP;
8267 break;
8268 }
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 != CIPHER_NONE) {
8274 int cipher;
8275
8276 switch (params->group_suite) {
8277 case CIPHER_WEP40:
8278 cipher = WLAN_CIPHER_SUITE_WEP40;
8279 break;
8280 case CIPHER_WEP104:
8281 cipher = WLAN_CIPHER_SUITE_WEP104;
8282 break;
8283 case CIPHER_CCMP:
8284 cipher = WLAN_CIPHER_SUITE_CCMP;
8285 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008286 case CIPHER_GCMP:
8287 cipher = WLAN_CIPHER_SUITE_GCMP;
8288 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008289 case CIPHER_TKIP:
8290 default:
8291 cipher = WLAN_CIPHER_SUITE_TKIP;
8292 break;
8293 }
8294 wpa_printf(MSG_DEBUG, " * group=0x%x", cipher);
8295 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher);
8296 }
8297
8298#ifdef CONFIG_IEEE80211W
8299 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED)
8300 NLA_PUT_U32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED);
8301#endif /* CONFIG_IEEE80211W */
8302
8303 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT);
8304
8305 if (params->prev_bssid) {
8306 wpa_printf(MSG_DEBUG, " * prev_bssid=" MACSTR,
8307 MAC2STR(params->prev_bssid));
8308 NLA_PUT(msg, NL80211_ATTR_PREV_BSSID, ETH_ALEN,
8309 params->prev_bssid);
8310 }
8311
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008312 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
Dmitry Shmidt2f023192013-03-12 12:44:17 -07008322#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
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008336 if (params->p2p)
8337 wpa_printf(MSG_DEBUG, " * P2P group");
8338
8339 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8340 msg = NULL;
8341 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008342 wpa_dbg(drv->ctx, MSG_DEBUG,
8343 "nl80211: MLME command failed (assoc): ret=%d (%s)",
8344 ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008345 nl80211_dump_scan(drv);
8346 goto nla_put_failure;
8347 }
8348 ret = 0;
8349 wpa_printf(MSG_DEBUG, "nl80211: Association request send "
8350 "successfully");
8351
8352nla_put_failure:
8353 nlmsg_free(msg);
8354 return ret;
8355}
8356
8357
8358static int nl80211_set_mode(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008359 int ifindex, enum nl80211_iftype mode)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008360{
8361 struct nl_msg *msg;
8362 int ret = -ENOBUFS;
8363
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008364 wpa_printf(MSG_DEBUG, "nl80211: Set mode ifindex %d iftype %d (%s)",
8365 ifindex, mode, nl80211_iftype_str(mode));
8366
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008367 msg = nlmsg_alloc();
8368 if (!msg)
8369 return -ENOMEM;
8370
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008371 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_INTERFACE);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008372 if (nl80211_set_iface_id(msg, drv->first_bss) < 0)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008373 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008374 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, mode);
8375
8376 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008377 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008378 if (!ret)
8379 return 0;
8380nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008381 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008382 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface %d to mode %d:"
8383 " %d (%s)", ifindex, mode, ret, strerror(-ret));
8384 return ret;
8385}
8386
8387
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008388static int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
8389 enum nl80211_iftype nlmode)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008390{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008391 struct wpa_driver_nl80211_data *drv = bss->drv;
8392 int ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008393 int i;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008394 int was_ap = is_ap_interface(drv->nlmode);
8395 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008396
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008397 res = nl80211_set_mode(drv, drv->ifindex, nlmode);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008398 if (res && nlmode == nl80211_get_ifmode(bss))
8399 res = 0;
8400
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008401 if (res == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008402 drv->nlmode = nlmode;
8403 ret = 0;
8404 goto done;
8405 }
8406
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008407 if (res == -ENODEV)
8408 return -1;
8409
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008410 if (nlmode == drv->nlmode) {
8411 wpa_printf(MSG_DEBUG, "nl80211: Interface already in "
8412 "requested mode - ignore error");
8413 ret = 0;
8414 goto done; /* Already in the requested mode */
8415 }
8416
8417 /* mac80211 doesn't allow mode changes while the device is up, so
8418 * take the device down, try to set the mode again, and bring the
8419 * device back up.
8420 */
8421 wpa_printf(MSG_DEBUG, "nl80211: Try mode change after setting "
8422 "interface down");
8423 for (i = 0; i < 10; i++) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008424 res = i802_set_iface_flags(bss, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008425 if (res == -EACCES || res == -ENODEV)
8426 break;
8427 if (res == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008428 /* Try to set the mode again while the interface is
8429 * down */
8430 ret = nl80211_set_mode(drv, drv->ifindex, nlmode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008431 if (ret == -EACCES)
8432 break;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008433 res = i802_set_iface_flags(bss, 1);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008434 if (res && !ret)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008435 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008436 else if (ret != -EBUSY)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008437 break;
8438 } else
8439 wpa_printf(MSG_DEBUG, "nl80211: Failed to set "
8440 "interface down");
8441 os_sleep(0, 100000);
8442 }
8443
8444 if (!ret) {
8445 wpa_printf(MSG_DEBUG, "nl80211: Mode change succeeded while "
8446 "interface is down");
8447 drv->nlmode = nlmode;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008448 drv->ignore_if_down_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008449 }
8450
8451done:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008452 if (ret) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008453 wpa_printf(MSG_DEBUG, "nl80211: Interface mode change to %d "
8454 "from %d failed", nlmode, drv->nlmode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008455 return ret;
8456 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008457
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008458 if (is_p2p_net_interface(nlmode))
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008459 nl80211_disable_11b_rates(drv, drv->ifindex, 1);
8460 else if (drv->disabled_11b_rates)
8461 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
8462
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008463 if (is_ap_interface(nlmode)) {
8464 nl80211_mgmt_unsubscribe(bss, "start AP");
8465 /* Setup additional AP mode functionality if needed */
8466 if (nl80211_setup_ap(bss))
8467 return -1;
8468 } else if (was_ap) {
8469 /* Remove additional AP mode functionality */
8470 nl80211_teardown_ap(bss);
8471 } else {
8472 nl80211_mgmt_unsubscribe(bss, "mode change");
8473 }
8474
Dmitry Shmidt04949592012-07-19 12:16:46 -07008475 if (!bss->in_deinit && !is_ap_interface(nlmode) &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008476 nl80211_mgmt_subscribe_non_ap(bss) < 0)
8477 wpa_printf(MSG_DEBUG, "nl80211: Failed to register Action "
8478 "frame processing - ignore for now");
8479
8480 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008481}
8482
8483
8484static int wpa_driver_nl80211_get_capa(void *priv,
8485 struct wpa_driver_capa *capa)
8486{
8487 struct i802_bss *bss = priv;
8488 struct wpa_driver_nl80211_data *drv = bss->drv;
8489 if (!drv->has_capability)
8490 return -1;
8491 os_memcpy(capa, &drv->capa, sizeof(*capa));
Dmitry Shmidt444d5672013-04-01 13:08:44 -07008492 if (drv->extended_capa && drv->extended_capa_mask) {
8493 capa->extended_capa = drv->extended_capa;
8494 capa->extended_capa_mask = drv->extended_capa_mask;
8495 capa->extended_capa_len = drv->extended_capa_len;
8496 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008497
8498 if ((capa->flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
8499 !drv->allow_p2p_device) {
8500 wpa_printf(MSG_DEBUG, "nl80211: Do not indicate P2P_DEVICE support (p2p_device=1 driver param not specified)");
8501 capa->flags &= ~WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE;
8502 }
8503
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008504 return 0;
8505}
8506
8507
8508static int wpa_driver_nl80211_set_operstate(void *priv, int state)
8509{
8510 struct i802_bss *bss = priv;
8511 struct wpa_driver_nl80211_data *drv = bss->drv;
8512
8513 wpa_printf(MSG_DEBUG, "%s: operstate %d->%d (%s)",
8514 __func__, drv->operstate, state, state ? "UP" : "DORMANT");
8515 drv->operstate = state;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008516 return netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, -1,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008517 state ? IF_OPER_UP : IF_OPER_DORMANT);
8518}
8519
8520
8521static int wpa_driver_nl80211_set_supp_port(void *priv, int authorized)
8522{
8523 struct i802_bss *bss = priv;
8524 struct wpa_driver_nl80211_data *drv = bss->drv;
8525 struct nl_msg *msg;
8526 struct nl80211_sta_flag_update upd;
8527
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07008528 wpa_printf(MSG_DEBUG, "nl80211: Set supplicant port %sauthorized for "
8529 MACSTR, authorized ? "" : "un", MAC2STR(drv->bssid));
8530
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008531 msg = nlmsg_alloc();
8532 if (!msg)
8533 return -ENOMEM;
8534
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008535 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008536
8537 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
8538 if_nametoindex(bss->ifname));
8539 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid);
8540
8541 os_memset(&upd, 0, sizeof(upd));
8542 upd.mask = BIT(NL80211_STA_FLAG_AUTHORIZED);
8543 if (authorized)
8544 upd.set = BIT(NL80211_STA_FLAG_AUTHORIZED);
8545 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
8546
8547 return send_and_recv_msgs(drv, msg, NULL, NULL);
8548 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008549 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008550 return -ENOBUFS;
8551}
8552
8553
Jouni Malinen75ecf522011-06-27 15:19:46 -07008554/* Set kernel driver on given frequency (MHz) */
8555static int i802_set_freq(void *priv, struct hostapd_freq_params *freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008556{
Jouni Malinen75ecf522011-06-27 15:19:46 -07008557 struct i802_bss *bss = priv;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008558 return wpa_driver_nl80211_set_freq(bss, freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008559}
8560
8561
Jouni Malinen75ecf522011-06-27 15:19:46 -07008562#if defined(HOSTAPD) || defined(CONFIG_AP)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008563
8564static inline int min_int(int a, int b)
8565{
8566 if (a < b)
8567 return a;
8568 return b;
8569}
8570
8571
8572static int get_key_handler(struct nl_msg *msg, void *arg)
8573{
8574 struct nlattr *tb[NL80211_ATTR_MAX + 1];
8575 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
8576
8577 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
8578 genlmsg_attrlen(gnlh, 0), NULL);
8579
8580 /*
8581 * TODO: validate the key index and mac address!
8582 * Otherwise, there's a race condition as soon as
8583 * the kernel starts sending key notifications.
8584 */
8585
8586 if (tb[NL80211_ATTR_KEY_SEQ])
8587 memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]),
8588 min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6));
8589 return NL_SKIP;
8590}
8591
8592
8593static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
8594 int idx, u8 *seq)
8595{
8596 struct i802_bss *bss = priv;
8597 struct wpa_driver_nl80211_data *drv = bss->drv;
8598 struct nl_msg *msg;
8599
8600 msg = nlmsg_alloc();
8601 if (!msg)
8602 return -ENOMEM;
8603
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008604 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_KEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008605
8606 if (addr)
8607 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
8608 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, idx);
8609 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
8610
8611 memset(seq, 0, 6);
8612
8613 return send_and_recv_msgs(drv, msg, get_key_handler, seq);
8614 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008615 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008616 return -ENOBUFS;
8617}
8618
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008619
8620static int i802_set_rts(void *priv, int rts)
8621{
8622 struct i802_bss *bss = priv;
8623 struct wpa_driver_nl80211_data *drv = bss->drv;
8624 struct nl_msg *msg;
8625 int ret = -ENOBUFS;
8626 u32 val;
8627
8628 msg = nlmsg_alloc();
8629 if (!msg)
8630 return -ENOMEM;
8631
8632 if (rts >= 2347)
8633 val = (u32) -1;
8634 else
8635 val = rts;
8636
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008637 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008638 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
8639 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD, val);
8640
8641 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008642 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008643 if (!ret)
8644 return 0;
8645nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008646 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008647 wpa_printf(MSG_DEBUG, "nl80211: Failed to set RTS threshold %d: "
8648 "%d (%s)", rts, ret, strerror(-ret));
8649 return ret;
8650}
8651
8652
8653static int i802_set_frag(void *priv, int frag)
8654{
8655 struct i802_bss *bss = priv;
8656 struct wpa_driver_nl80211_data *drv = bss->drv;
8657 struct nl_msg *msg;
8658 int ret = -ENOBUFS;
8659 u32 val;
8660
8661 msg = nlmsg_alloc();
8662 if (!msg)
8663 return -ENOMEM;
8664
8665 if (frag >= 2346)
8666 val = (u32) -1;
8667 else
8668 val = frag;
8669
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008670 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008671 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
8672 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD, val);
8673
8674 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008675 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008676 if (!ret)
8677 return 0;
8678nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008679 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008680 wpa_printf(MSG_DEBUG, "nl80211: Failed to set fragmentation threshold "
8681 "%d: %d (%s)", frag, ret, strerror(-ret));
8682 return ret;
8683}
8684
8685
8686static int i802_flush(void *priv)
8687{
8688 struct i802_bss *bss = priv;
8689 struct wpa_driver_nl80211_data *drv = bss->drv;
8690 struct nl_msg *msg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008691 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008692
8693 msg = nlmsg_alloc();
8694 if (!msg)
8695 return -1;
8696
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008697 wpa_printf(MSG_DEBUG, "nl80211: flush -> DEL_STATION %s (all)",
8698 bss->ifname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008699 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008700
8701 /*
8702 * XXX: FIX! this needs to flush all VLANs too
8703 */
8704 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
8705 if_nametoindex(bss->ifname));
8706
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008707 res = send_and_recv_msgs(drv, msg, NULL, NULL);
8708 if (res) {
8709 wpa_printf(MSG_DEBUG, "nl80211: Station flush failed: ret=%d "
8710 "(%s)", res, strerror(-res));
8711 }
8712 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008713 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008714 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008715 return -ENOBUFS;
8716}
8717
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03008718#endif /* HOSTAPD || CONFIG_AP */
8719
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008720
8721static int get_sta_handler(struct nl_msg *msg, void *arg)
8722{
8723 struct nlattr *tb[NL80211_ATTR_MAX + 1];
8724 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
8725 struct hostap_sta_driver_data *data = arg;
8726 struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
8727 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
8728 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
8729 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
8730 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
8731 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
8732 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03008733 [NL80211_STA_INFO_TX_FAILED] = { .type = NLA_U32 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008734 };
8735
8736 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
8737 genlmsg_attrlen(gnlh, 0), NULL);
8738
8739 /*
8740 * TODO: validate the interface and mac address!
8741 * Otherwise, there's a race condition as soon as
8742 * the kernel starts sending station notifications.
8743 */
8744
8745 if (!tb[NL80211_ATTR_STA_INFO]) {
8746 wpa_printf(MSG_DEBUG, "sta stats missing!");
8747 return NL_SKIP;
8748 }
8749 if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
8750 tb[NL80211_ATTR_STA_INFO],
8751 stats_policy)) {
8752 wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
8753 return NL_SKIP;
8754 }
8755
8756 if (stats[NL80211_STA_INFO_INACTIVE_TIME])
8757 data->inactive_msec =
8758 nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
8759 if (stats[NL80211_STA_INFO_RX_BYTES])
8760 data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
8761 if (stats[NL80211_STA_INFO_TX_BYTES])
8762 data->tx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
8763 if (stats[NL80211_STA_INFO_RX_PACKETS])
8764 data->rx_packets =
8765 nla_get_u32(stats[NL80211_STA_INFO_RX_PACKETS]);
8766 if (stats[NL80211_STA_INFO_TX_PACKETS])
8767 data->tx_packets =
8768 nla_get_u32(stats[NL80211_STA_INFO_TX_PACKETS]);
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03008769 if (stats[NL80211_STA_INFO_TX_FAILED])
8770 data->tx_retry_failed =
8771 nla_get_u32(stats[NL80211_STA_INFO_TX_FAILED]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008772
8773 return NL_SKIP;
8774}
8775
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008776static int i802_read_sta_data(struct i802_bss *bss,
8777 struct hostap_sta_driver_data *data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008778 const u8 *addr)
8779{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008780 struct wpa_driver_nl80211_data *drv = bss->drv;
8781 struct nl_msg *msg;
8782
8783 os_memset(data, 0, sizeof(*data));
8784 msg = nlmsg_alloc();
8785 if (!msg)
8786 return -ENOMEM;
8787
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008788 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008789
8790 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
8791 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
8792
8793 return send_and_recv_msgs(drv, msg, get_sta_handler, data);
8794 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008795 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008796 return -ENOBUFS;
8797}
8798
8799
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03008800#if defined(HOSTAPD) || defined(CONFIG_AP)
8801
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008802static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
8803 int cw_min, int cw_max, int burst_time)
8804{
8805 struct i802_bss *bss = priv;
8806 struct wpa_driver_nl80211_data *drv = bss->drv;
8807 struct nl_msg *msg;
8808 struct nlattr *txq, *params;
8809
8810 msg = nlmsg_alloc();
8811 if (!msg)
8812 return -1;
8813
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008814 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008815
8816 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
8817
8818 txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS);
8819 if (!txq)
8820 goto nla_put_failure;
8821
8822 /* We are only sending parameters for a single TXQ at a time */
8823 params = nla_nest_start(msg, 1);
8824 if (!params)
8825 goto nla_put_failure;
8826
8827 switch (queue) {
8828 case 0:
8829 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VO);
8830 break;
8831 case 1:
8832 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VI);
8833 break;
8834 case 2:
8835 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BE);
8836 break;
8837 case 3:
8838 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BK);
8839 break;
8840 }
8841 /* Burst time is configured in units of 0.1 msec and TXOP parameter in
8842 * 32 usec, so need to convert the value here. */
8843 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_TXOP, (burst_time * 100 + 16) / 32);
8844 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min);
8845 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max);
8846 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_AIFS, aifs);
8847
8848 nla_nest_end(msg, params);
8849
8850 nla_nest_end(msg, txq);
8851
8852 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
8853 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008854 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008855 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008856 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008857 return -1;
8858}
8859
8860
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008861static int i802_set_sta_vlan(struct i802_bss *bss, const u8 *addr,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008862 const char *ifname, int vlan_id)
8863{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008864 struct wpa_driver_nl80211_data *drv = bss->drv;
8865 struct nl_msg *msg;
8866 int ret = -ENOBUFS;
8867
8868 msg = nlmsg_alloc();
8869 if (!msg)
8870 return -ENOMEM;
8871
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008872 wpa_printf(MSG_DEBUG, "nl80211: %s[%d]: set_sta_vlan(" MACSTR
8873 ", ifname=%s[%d], vlan_id=%d)",
8874 bss->ifname, if_nametoindex(bss->ifname),
8875 MAC2STR(addr), ifname, if_nametoindex(ifname), vlan_id);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008876 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008877
8878 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
8879 if_nametoindex(bss->ifname));
8880 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
8881 NLA_PUT_U32(msg, NL80211_ATTR_STA_VLAN,
8882 if_nametoindex(ifname));
8883
8884 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008885 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008886 if (ret < 0) {
8887 wpa_printf(MSG_ERROR, "nl80211: NL80211_ATTR_STA_VLAN (addr="
8888 MACSTR " ifname=%s vlan_id=%d) failed: %d (%s)",
8889 MAC2STR(addr), ifname, vlan_id, ret,
8890 strerror(-ret));
8891 }
8892 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008893 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008894 return ret;
8895}
8896
8897
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008898static int i802_get_inact_sec(void *priv, const u8 *addr)
8899{
8900 struct hostap_sta_driver_data data;
8901 int ret;
8902
8903 data.inactive_msec = (unsigned long) -1;
8904 ret = i802_read_sta_data(priv, &data, addr);
8905 if (ret || data.inactive_msec == (unsigned long) -1)
8906 return -1;
8907 return data.inactive_msec / 1000;
8908}
8909
8910
8911static int i802_sta_clear_stats(void *priv, const u8 *addr)
8912{
8913#if 0
8914 /* TODO */
8915#endif
8916 return 0;
8917}
8918
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008919
8920static int i802_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
8921 int reason)
8922{
8923 struct i802_bss *bss = priv;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008924 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008925 struct ieee80211_mgmt mgmt;
8926
Dmitry Shmidt04949592012-07-19 12:16:46 -07008927 if (drv->device_ap_sme)
8928 return wpa_driver_nl80211_sta_remove(bss, addr);
8929
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008930 memset(&mgmt, 0, sizeof(mgmt));
8931 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
8932 WLAN_FC_STYPE_DEAUTH);
8933 memcpy(mgmt.da, addr, ETH_ALEN);
8934 memcpy(mgmt.sa, own_addr, ETH_ALEN);
8935 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
8936 mgmt.u.deauth.reason_code = host_to_le16(reason);
8937 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
8938 IEEE80211_HDRLEN +
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008939 sizeof(mgmt.u.deauth), 0, 0, 0, 0,
8940 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008941}
8942
8943
8944static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
8945 int reason)
8946{
8947 struct i802_bss *bss = priv;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008948 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008949 struct ieee80211_mgmt mgmt;
8950
Dmitry Shmidt04949592012-07-19 12:16:46 -07008951 if (drv->device_ap_sme)
8952 return wpa_driver_nl80211_sta_remove(bss, addr);
8953
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008954 memset(&mgmt, 0, sizeof(mgmt));
8955 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
8956 WLAN_FC_STYPE_DISASSOC);
8957 memcpy(mgmt.da, addr, ETH_ALEN);
8958 memcpy(mgmt.sa, own_addr, ETH_ALEN);
8959 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
8960 mgmt.u.disassoc.reason_code = host_to_le16(reason);
8961 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
8962 IEEE80211_HDRLEN +
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008963 sizeof(mgmt.u.disassoc), 0, 0, 0, 0,
8964 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008965}
8966
8967#endif /* HOSTAPD || CONFIG_AP */
8968
8969#ifdef HOSTAPD
8970
Jouni Malinen75ecf522011-06-27 15:19:46 -07008971static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
8972{
8973 int i;
8974 int *old;
8975
8976 wpa_printf(MSG_DEBUG, "nl80211: Add own interface ifindex %d",
8977 ifidx);
8978 for (i = 0; i < drv->num_if_indices; i++) {
8979 if (drv->if_indices[i] == 0) {
8980 drv->if_indices[i] = ifidx;
8981 return;
8982 }
8983 }
8984
8985 if (drv->if_indices != drv->default_if_indices)
8986 old = drv->if_indices;
8987 else
8988 old = NULL;
8989
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008990 drv->if_indices = os_realloc_array(old, drv->num_if_indices + 1,
8991 sizeof(int));
Jouni Malinen75ecf522011-06-27 15:19:46 -07008992 if (!drv->if_indices) {
8993 if (!old)
8994 drv->if_indices = drv->default_if_indices;
8995 else
8996 drv->if_indices = old;
8997 wpa_printf(MSG_ERROR, "Failed to reallocate memory for "
8998 "interfaces");
8999 wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx);
9000 return;
9001 } else if (!old)
9002 os_memcpy(drv->if_indices, drv->default_if_indices,
9003 sizeof(drv->default_if_indices));
9004 drv->if_indices[drv->num_if_indices] = ifidx;
9005 drv->num_if_indices++;
9006}
9007
9008
9009static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
9010{
9011 int i;
9012
9013 for (i = 0; i < drv->num_if_indices; i++) {
9014 if (drv->if_indices[i] == ifidx) {
9015 drv->if_indices[i] = 0;
9016 break;
9017 }
9018 }
9019}
9020
9021
9022static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
9023{
9024 int i;
9025
9026 for (i = 0; i < drv->num_if_indices; i++)
9027 if (drv->if_indices[i] == ifidx)
9028 return 1;
9029
9030 return 0;
9031}
9032
9033
9034static int i802_set_wds_sta(void *priv, const u8 *addr, int aid, int val,
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07009035 const char *bridge_ifname, char *ifname_wds)
Jouni Malinen75ecf522011-06-27 15:19:46 -07009036{
9037 struct i802_bss *bss = priv;
9038 struct wpa_driver_nl80211_data *drv = bss->drv;
9039 char name[IFNAMSIZ + 1];
9040
9041 os_snprintf(name, sizeof(name), "%s.sta%d", bss->ifname, aid);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07009042 if (ifname_wds)
9043 os_strlcpy(ifname_wds, name, IFNAMSIZ + 1);
9044
Jouni Malinen75ecf522011-06-27 15:19:46 -07009045 wpa_printf(MSG_DEBUG, "nl80211: Set WDS STA addr=" MACSTR
9046 " aid=%d val=%d name=%s", MAC2STR(addr), aid, val, name);
9047 if (val) {
9048 if (!if_nametoindex(name)) {
9049 if (nl80211_create_iface(drv, name,
9050 NL80211_IFTYPE_AP_VLAN,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009051 bss->addr, 1, NULL, NULL, 0) <
9052 0)
Jouni Malinen75ecf522011-06-27 15:19:46 -07009053 return -1;
9054 if (bridge_ifname &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009055 linux_br_add_if(drv->global->ioctl_sock,
9056 bridge_ifname, name) < 0)
Jouni Malinen75ecf522011-06-27 15:19:46 -07009057 return -1;
9058 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07009059 if (linux_set_iface_flags(drv->global->ioctl_sock, name, 1)) {
9060 wpa_printf(MSG_ERROR, "nl80211: Failed to set WDS STA "
9061 "interface %s up", name);
9062 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07009063 return i802_set_sta_vlan(priv, addr, name, 0);
9064 } else {
Dmitry Shmidtaa532512012-09-24 10:35:31 -07009065 if (bridge_ifname)
9066 linux_br_del_if(drv->global->ioctl_sock, bridge_ifname,
9067 name);
9068
Jouni Malinen75ecf522011-06-27 15:19:46 -07009069 i802_set_sta_vlan(priv, addr, bss->ifname, 0);
9070 return wpa_driver_nl80211_if_remove(priv, WPA_IF_AP_VLAN,
9071 name);
9072 }
9073}
9074
9075
9076static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
9077{
9078 struct wpa_driver_nl80211_data *drv = eloop_ctx;
9079 struct sockaddr_ll lladdr;
9080 unsigned char buf[3000];
9081 int len;
9082 socklen_t fromlen = sizeof(lladdr);
9083
9084 len = recvfrom(sock, buf, sizeof(buf), 0,
9085 (struct sockaddr *)&lladdr, &fromlen);
9086 if (len < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07009087 wpa_printf(MSG_ERROR, "nl80211: EAPOL recv failed: %s",
9088 strerror(errno));
Jouni Malinen75ecf522011-06-27 15:19:46 -07009089 return;
9090 }
9091
9092 if (have_ifidx(drv, lladdr.sll_ifindex))
9093 drv_event_eapol_rx(drv->ctx, lladdr.sll_addr, buf, len);
9094}
9095
9096
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009097static int i802_check_bridge(struct wpa_driver_nl80211_data *drv,
9098 struct i802_bss *bss,
9099 const char *brname, const char *ifname)
9100{
9101 int ifindex;
9102 char in_br[IFNAMSIZ];
9103
9104 os_strlcpy(bss->brname, brname, IFNAMSIZ);
9105 ifindex = if_nametoindex(brname);
9106 if (ifindex == 0) {
9107 /*
9108 * Bridge was configured, but the bridge device does
9109 * not exist. Try to add it now.
9110 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009111 if (linux_br_add(drv->global->ioctl_sock, brname) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009112 wpa_printf(MSG_ERROR, "nl80211: Failed to add the "
9113 "bridge interface %s: %s",
9114 brname, strerror(errno));
9115 return -1;
9116 }
9117 bss->added_bridge = 1;
9118 add_ifidx(drv, if_nametoindex(brname));
9119 }
9120
9121 if (linux_br_get(in_br, ifname) == 0) {
9122 if (os_strcmp(in_br, brname) == 0)
9123 return 0; /* already in the bridge */
9124
9125 wpa_printf(MSG_DEBUG, "nl80211: Removing interface %s from "
9126 "bridge %s", ifname, in_br);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009127 if (linux_br_del_if(drv->global->ioctl_sock, in_br, ifname) <
9128 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009129 wpa_printf(MSG_ERROR, "nl80211: Failed to "
9130 "remove interface %s from bridge "
9131 "%s: %s",
9132 ifname, brname, strerror(errno));
9133 return -1;
9134 }
9135 }
9136
9137 wpa_printf(MSG_DEBUG, "nl80211: Adding interface %s into bridge %s",
9138 ifname, brname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009139 if (linux_br_add_if(drv->global->ioctl_sock, brname, ifname) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009140 wpa_printf(MSG_ERROR, "nl80211: Failed to add interface %s "
9141 "into bridge %s: %s",
9142 ifname, brname, strerror(errno));
9143 return -1;
9144 }
9145 bss->added_if_into_bridge = 1;
9146
9147 return 0;
9148}
9149
9150
9151static void *i802_init(struct hostapd_data *hapd,
9152 struct wpa_init_params *params)
9153{
9154 struct wpa_driver_nl80211_data *drv;
9155 struct i802_bss *bss;
9156 size_t i;
9157 char brname[IFNAMSIZ];
9158 int ifindex, br_ifindex;
9159 int br_added = 0;
9160
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009161 bss = wpa_driver_nl80211_init(hapd, params->ifname,
9162 params->global_priv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009163 if (bss == NULL)
9164 return NULL;
9165
9166 drv = bss->drv;
9167 drv->nlmode = NL80211_IFTYPE_AP;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009168 drv->eapol_sock = -1;
9169
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009170 if (linux_br_get(brname, params->ifname) == 0) {
9171 wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in bridge %s",
9172 params->ifname, brname);
9173 br_ifindex = if_nametoindex(brname);
9174 } else {
9175 brname[0] = '\0';
9176 br_ifindex = 0;
9177 }
9178
9179 drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int);
9180 drv->if_indices = drv->default_if_indices;
9181 for (i = 0; i < params->num_bridge; i++) {
9182 if (params->bridge[i]) {
9183 ifindex = if_nametoindex(params->bridge[i]);
9184 if (ifindex)
9185 add_ifidx(drv, ifindex);
9186 if (ifindex == br_ifindex)
9187 br_added = 1;
9188 }
9189 }
9190 if (!br_added && br_ifindex &&
9191 (params->num_bridge == 0 || !params->bridge[0]))
9192 add_ifidx(drv, br_ifindex);
9193
9194 /* start listening for EAPOL on the default AP interface */
9195 add_ifidx(drv, drv->ifindex);
9196
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009197 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009198 goto failed;
9199
9200 if (params->bssid) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009201 if (linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009202 params->bssid))
9203 goto failed;
9204 }
9205
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009206 if (wpa_driver_nl80211_set_mode(bss, drv->nlmode)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009207 wpa_printf(MSG_ERROR, "nl80211: Failed to set interface %s "
9208 "into AP mode", bss->ifname);
9209 goto failed;
9210 }
9211
9212 if (params->num_bridge && params->bridge[0] &&
9213 i802_check_bridge(drv, bss, params->bridge[0], params->ifname) < 0)
9214 goto failed;
9215
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009216 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009217 goto failed;
9218
9219 drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
9220 if (drv->eapol_sock < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07009221 wpa_printf(MSG_ERROR, "nl80211: socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE) failed: %s",
9222 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009223 goto failed;
9224 }
9225
9226 if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
9227 {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07009228 wpa_printf(MSG_INFO, "nl80211: Could not register read socket for eapol");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009229 goto failed;
9230 }
9231
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009232 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
9233 params->own_addr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009234 goto failed;
9235
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009236 memcpy(bss->addr, params->own_addr, ETH_ALEN);
9237
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009238 return bss;
9239
9240failed:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009241 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009242 return NULL;
9243}
9244
9245
9246static void i802_deinit(void *priv)
9247{
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009248 struct i802_bss *bss = priv;
9249 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009250}
9251
9252#endif /* HOSTAPD */
9253
9254
9255static enum nl80211_iftype wpa_driver_nl80211_if_type(
9256 enum wpa_driver_if_type type)
9257{
9258 switch (type) {
9259 case WPA_IF_STATION:
9260 return NL80211_IFTYPE_STATION;
9261 case WPA_IF_P2P_CLIENT:
9262 case WPA_IF_P2P_GROUP:
9263 return NL80211_IFTYPE_P2P_CLIENT;
9264 case WPA_IF_AP_VLAN:
9265 return NL80211_IFTYPE_AP_VLAN;
9266 case WPA_IF_AP_BSS:
9267 return NL80211_IFTYPE_AP;
9268 case WPA_IF_P2P_GO:
9269 return NL80211_IFTYPE_P2P_GO;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009270 case WPA_IF_P2P_DEVICE:
9271 return NL80211_IFTYPE_P2P_DEVICE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009272 }
9273 return -1;
9274}
9275
9276
9277#ifdef CONFIG_P2P
9278
9279static int nl80211_addr_in_use(struct nl80211_global *global, const u8 *addr)
9280{
9281 struct wpa_driver_nl80211_data *drv;
9282 dl_list_for_each(drv, &global->interfaces,
9283 struct wpa_driver_nl80211_data, list) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009284 if (os_memcmp(addr, drv->first_bss->addr, ETH_ALEN) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009285 return 1;
9286 }
9287 return 0;
9288}
9289
9290
9291static int nl80211_p2p_interface_addr(struct wpa_driver_nl80211_data *drv,
9292 u8 *new_addr)
9293{
9294 unsigned int idx;
9295
9296 if (!drv->global)
9297 return -1;
9298
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009299 os_memcpy(new_addr, drv->first_bss->addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009300 for (idx = 0; idx < 64; idx++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009301 new_addr[0] = drv->first_bss->addr[0] | 0x02;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009302 new_addr[0] ^= idx << 2;
9303 if (!nl80211_addr_in_use(drv->global, new_addr))
9304 break;
9305 }
9306 if (idx == 64)
9307 return -1;
9308
9309 wpa_printf(MSG_DEBUG, "nl80211: Assigned new P2P Interface Address "
9310 MACSTR, MAC2STR(new_addr));
9311
9312 return 0;
9313}
9314
9315#endif /* CONFIG_P2P */
9316
9317
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009318struct wdev_info {
9319 u64 wdev_id;
9320 int wdev_id_set;
9321 u8 macaddr[ETH_ALEN];
9322};
9323
9324static int nl80211_wdev_handler(struct nl_msg *msg, void *arg)
9325{
9326 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
9327 struct nlattr *tb[NL80211_ATTR_MAX + 1];
9328 struct wdev_info *wi = arg;
9329
9330 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9331 genlmsg_attrlen(gnlh, 0), NULL);
9332 if (tb[NL80211_ATTR_WDEV]) {
9333 wi->wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
9334 wi->wdev_id_set = 1;
9335 }
9336
9337 if (tb[NL80211_ATTR_MAC])
9338 os_memcpy(wi->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
9339 ETH_ALEN);
9340
9341 return NL_SKIP;
9342}
9343
9344
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009345static int wpa_driver_nl80211_if_add(void *priv, enum wpa_driver_if_type type,
9346 const char *ifname, const u8 *addr,
9347 void *bss_ctx, void **drv_priv,
9348 char *force_ifname, u8 *if_addr,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009349 const char *bridge, int use_existing)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009350{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009351 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009352 struct i802_bss *bss = priv;
9353 struct wpa_driver_nl80211_data *drv = bss->drv;
9354 int ifidx;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009355 int added = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009356
9357 if (addr)
9358 os_memcpy(if_addr, addr, ETH_ALEN);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009359 nlmode = wpa_driver_nl80211_if_type(type);
9360 if (nlmode == NL80211_IFTYPE_P2P_DEVICE) {
9361 struct wdev_info p2pdev_info;
9362
9363 os_memset(&p2pdev_info, 0, sizeof(p2pdev_info));
9364 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
9365 0, nl80211_wdev_handler,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009366 &p2pdev_info, use_existing);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009367 if (!p2pdev_info.wdev_id_set || ifidx != 0) {
9368 wpa_printf(MSG_ERROR, "nl80211: Failed to create a P2P Device interface %s",
9369 ifname);
9370 return -1;
9371 }
9372
9373 drv->global->if_add_wdevid = p2pdev_info.wdev_id;
9374 drv->global->if_add_wdevid_set = p2pdev_info.wdev_id_set;
9375 if (!is_zero_ether_addr(p2pdev_info.macaddr))
9376 os_memcpy(if_addr, p2pdev_info.macaddr, ETH_ALEN);
9377 wpa_printf(MSG_DEBUG, "nl80211: New P2P Device interface %s (0x%llx) created",
9378 ifname,
9379 (long long unsigned int) p2pdev_info.wdev_id);
9380 } else {
9381 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009382 0, NULL, NULL, use_existing);
9383 if (use_existing && ifidx == -ENFILE) {
9384 added = 0;
9385 ifidx = if_nametoindex(ifname);
9386 } else if (ifidx < 0) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009387 return -1;
9388 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009389 }
9390
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009391 if (!addr) {
9392 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
9393 os_memcpy(if_addr, bss->addr, ETH_ALEN);
9394 else if (linux_get_ifhwaddr(drv->global->ioctl_sock,
9395 bss->ifname, if_addr) < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009396 if (added)
9397 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009398 return -1;
9399 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009400 }
9401
9402#ifdef CONFIG_P2P
9403 if (!addr &&
9404 (type == WPA_IF_P2P_CLIENT || type == WPA_IF_P2P_GROUP ||
9405 type == WPA_IF_P2P_GO)) {
9406 /* Enforce unique P2P Interface Address */
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009407 u8 new_addr[ETH_ALEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009408
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009409 if (linux_get_ifhwaddr(drv->global->ioctl_sock, ifname,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009410 new_addr) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009411 nl80211_remove_iface(drv, ifidx);
9412 return -1;
9413 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009414 if (nl80211_addr_in_use(drv->global, new_addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009415 wpa_printf(MSG_DEBUG, "nl80211: Allocate new address "
9416 "for P2P group interface");
9417 if (nl80211_p2p_interface_addr(drv, new_addr) < 0) {
9418 nl80211_remove_iface(drv, ifidx);
9419 return -1;
9420 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009421 if (linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009422 new_addr) < 0) {
9423 nl80211_remove_iface(drv, ifidx);
9424 return -1;
9425 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009426 }
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07009427 os_memcpy(if_addr, new_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009428 }
9429#endif /* CONFIG_P2P */
9430
9431#ifdef HOSTAPD
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009432 if (type == WPA_IF_AP_BSS) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07009433 struct i802_bss *new_bss = os_zalloc(sizeof(*new_bss));
9434 if (new_bss == NULL) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009435 if (added)
9436 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07009437 return -1;
9438 }
9439
9440 if (bridge &&
9441 i802_check_bridge(drv, new_bss, bridge, ifname) < 0) {
9442 wpa_printf(MSG_ERROR, "nl80211: Failed to add the new "
9443 "interface %s to a bridge %s",
9444 ifname, bridge);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009445 if (added)
9446 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07009447 os_free(new_bss);
9448 return -1;
9449 }
9450
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009451 if (linux_set_iface_flags(drv->global->ioctl_sock, ifname, 1))
9452 {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009453 nl80211_remove_iface(drv, ifidx);
9454 os_free(new_bss);
9455 return -1;
9456 }
9457 os_strlcpy(new_bss->ifname, ifname, IFNAMSIZ);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009458 os_memcpy(new_bss->addr, if_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009459 new_bss->ifindex = ifidx;
9460 new_bss->drv = drv;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009461 new_bss->next = drv->first_bss->next;
9462 new_bss->freq = drv->first_bss->freq;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08009463 new_bss->ctx = bss_ctx;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009464 new_bss->added_if = added;
9465 drv->first_bss->next = new_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009466 if (drv_priv)
9467 *drv_priv = new_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009468 nl80211_init_bss(new_bss);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08009469
9470 /* Subscribe management frames for this WPA_IF_AP_BSS */
9471 if (nl80211_setup_ap(new_bss))
9472 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009473 }
9474#endif /* HOSTAPD */
9475
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009476 if (drv->global)
9477 drv->global->if_add_ifindex = ifidx;
9478
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009479 return 0;
9480}
9481
9482
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009483static int wpa_driver_nl80211_if_remove(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009484 enum wpa_driver_if_type type,
9485 const char *ifname)
9486{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009487 struct wpa_driver_nl80211_data *drv = bss->drv;
9488 int ifindex = if_nametoindex(ifname);
9489
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009490 wpa_printf(MSG_DEBUG, "nl80211: %s(type=%d ifname=%s) ifindex=%d added_if=%d",
9491 __func__, type, ifname, ifindex, bss->added_if);
9492 if (ifindex > 0 && bss->added_if)
Dmitry Shmidt051af732013-10-22 13:52:46 -07009493 nl80211_remove_iface(drv, ifindex);
Dmitry Shmidtaa532512012-09-24 10:35:31 -07009494
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009495#ifdef HOSTAPD
Dmitry Shmidtaa532512012-09-24 10:35:31 -07009496 if (type != WPA_IF_AP_BSS)
9497 return 0;
9498
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009499 if (bss->added_if_into_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009500 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
9501 bss->ifname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009502 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
9503 "interface %s from bridge %s: %s",
9504 bss->ifname, bss->brname, strerror(errno));
9505 }
9506 if (bss->added_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009507 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009508 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
9509 "bridge %s: %s",
9510 bss->brname, strerror(errno));
9511 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009512
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009513 if (bss != drv->first_bss) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009514 struct i802_bss *tbss;
9515
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009516 wpa_printf(MSG_DEBUG, "nl80211: Not the first BSS - remove it");
9517 for (tbss = drv->first_bss; tbss; tbss = tbss->next) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009518 if (tbss->next == bss) {
9519 tbss->next = bss->next;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08009520 /* Unsubscribe management frames */
9521 nl80211_teardown_ap(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009522 nl80211_destroy_bss(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009523 os_free(bss);
9524 bss = NULL;
9525 break;
9526 }
9527 }
9528 if (bss)
9529 wpa_printf(MSG_INFO, "nl80211: %s - could not find "
9530 "BSS %p in the list", __func__, bss);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009531 } else {
9532 wpa_printf(MSG_DEBUG, "nl80211: First BSS - reassign context");
9533 nl80211_teardown_ap(bss);
9534 if (!bss->added_if && !drv->first_bss->next)
9535 wpa_driver_nl80211_del_beacon(drv);
9536 nl80211_destroy_bss(bss);
9537 if (!bss->added_if)
9538 i802_set_iface_flags(bss, 0);
9539 if (drv->first_bss->next) {
9540 drv->first_bss = drv->first_bss->next;
9541 drv->ctx = drv->first_bss->ctx;
9542 os_free(bss);
9543 } else {
9544 wpa_printf(MSG_DEBUG, "nl80211: No second BSS to reassign context to");
9545 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009546 }
9547#endif /* HOSTAPD */
9548
9549 return 0;
9550}
9551
9552
9553static int cookie_handler(struct nl_msg *msg, void *arg)
9554{
9555 struct nlattr *tb[NL80211_ATTR_MAX + 1];
9556 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
9557 u64 *cookie = arg;
9558 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9559 genlmsg_attrlen(gnlh, 0), NULL);
9560 if (tb[NL80211_ATTR_COOKIE])
9561 *cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
9562 return NL_SKIP;
9563}
9564
9565
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009566static int nl80211_send_frame_cmd(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009567 unsigned int freq, unsigned int wait,
9568 const u8 *buf, size_t buf_len,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009569 u64 *cookie_out, int no_cck, int no_ack,
9570 int offchanok)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009571{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009572 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009573 struct nl_msg *msg;
9574 u64 cookie;
9575 int ret = -1;
9576
9577 msg = nlmsg_alloc();
9578 if (!msg)
9579 return -1;
9580
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07009581 wpa_printf(MSG_MSGDUMP, "nl80211: CMD_FRAME freq=%u wait=%u no_cck=%d "
Dmitry Shmidt04949592012-07-19 12:16:46 -07009582 "no_ack=%d offchanok=%d",
9583 freq, wait, no_cck, no_ack, offchanok);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07009584 wpa_hexdump(MSG_MSGDUMP, "CMD_FRAME", buf, buf_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009585 nl80211_cmd(drv, msg, 0, NL80211_CMD_FRAME);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009586
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009587 if (nl80211_set_iface_id(msg, bss) < 0)
9588 goto nla_put_failure;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07009589 if (freq)
9590 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07009591 if (wait)
9592 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, wait);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08009593 if (offchanok && (drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009594 NLA_PUT_FLAG(msg, NL80211_ATTR_OFFCHANNEL_TX_OK);
9595 if (no_cck)
9596 NLA_PUT_FLAG(msg, NL80211_ATTR_TX_NO_CCK_RATE);
9597 if (no_ack)
9598 NLA_PUT_FLAG(msg, NL80211_ATTR_DONT_WAIT_FOR_ACK);
9599
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009600 NLA_PUT(msg, NL80211_ATTR_FRAME, buf_len, buf);
9601
9602 cookie = 0;
9603 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
9604 msg = NULL;
9605 if (ret) {
9606 wpa_printf(MSG_DEBUG, "nl80211: Frame command failed: ret=%d "
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07009607 "(%s) (freq=%u wait=%u)", ret, strerror(-ret),
9608 freq, wait);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009609 goto nla_put_failure;
9610 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07009611 wpa_printf(MSG_MSGDUMP, "nl80211: Frame TX command accepted%s; "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009612 "cookie 0x%llx", no_ack ? " (no ACK)" : "",
9613 (long long unsigned int) cookie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009614
9615 if (cookie_out)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009616 *cookie_out = no_ack ? (u64) -1 : cookie;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009617
9618nla_put_failure:
9619 nlmsg_free(msg);
9620 return ret;
9621}
9622
9623
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009624static int wpa_driver_nl80211_send_action(struct i802_bss *bss,
9625 unsigned int freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009626 unsigned int wait_time,
9627 const u8 *dst, const u8 *src,
9628 const u8 *bssid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009629 const u8 *data, size_t data_len,
9630 int no_cck)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009631{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009632 struct wpa_driver_nl80211_data *drv = bss->drv;
9633 int ret = -1;
9634 u8 *buf;
9635 struct ieee80211_hdr *hdr;
9636
9637 wpa_printf(MSG_DEBUG, "nl80211: Send Action frame (ifindex=%d, "
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08009638 "freq=%u MHz wait=%d ms no_cck=%d)",
9639 drv->ifindex, freq, wait_time, no_cck);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009640
9641 buf = os_zalloc(24 + data_len);
9642 if (buf == NULL)
9643 return ret;
9644 os_memcpy(buf + 24, data, data_len);
9645 hdr = (struct ieee80211_hdr *) buf;
9646 hdr->frame_control =
9647 IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_ACTION);
9648 os_memcpy(hdr->addr1, dst, ETH_ALEN);
9649 os_memcpy(hdr->addr2, src, ETH_ALEN);
9650 os_memcpy(hdr->addr3, bssid, ETH_ALEN);
9651
Dmitry Shmidt56052862013-10-04 10:23:25 -07009652 if (is_ap_interface(drv->nlmode) &&
9653 (!(drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
9654 (int) freq == bss->freq || drv->device_ap_sme ||
9655 !drv->use_monitor))
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009656 ret = wpa_driver_nl80211_send_mlme(bss, buf, 24 + data_len,
9657 0, freq, no_cck, 1,
9658 wait_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009659 else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009660 ret = nl80211_send_frame_cmd(bss, freq, wait_time, buf,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009661 24 + data_len,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009662 &drv->send_action_cookie,
9663 no_cck, 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009664
9665 os_free(buf);
9666 return ret;
9667}
9668
9669
9670static void wpa_driver_nl80211_send_action_cancel_wait(void *priv)
9671{
9672 struct i802_bss *bss = priv;
9673 struct wpa_driver_nl80211_data *drv = bss->drv;
9674 struct nl_msg *msg;
9675 int ret;
9676
9677 msg = nlmsg_alloc();
9678 if (!msg)
9679 return;
9680
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08009681 wpa_printf(MSG_DEBUG, "nl80211: Cancel TX frame wait: cookie=0x%llx",
9682 (long long unsigned int) drv->send_action_cookie);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009683 nl80211_cmd(drv, msg, 0, NL80211_CMD_FRAME_WAIT_CANCEL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009684
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009685 if (nl80211_set_iface_id(msg, bss) < 0)
9686 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009687 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->send_action_cookie);
9688
9689 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9690 msg = NULL;
9691 if (ret)
9692 wpa_printf(MSG_DEBUG, "nl80211: wait cancel failed: ret=%d "
9693 "(%s)", ret, strerror(-ret));
9694
9695 nla_put_failure:
9696 nlmsg_free(msg);
9697}
9698
9699
9700static int wpa_driver_nl80211_remain_on_channel(void *priv, unsigned int freq,
9701 unsigned int duration)
9702{
9703 struct i802_bss *bss = priv;
9704 struct wpa_driver_nl80211_data *drv = bss->drv;
9705 struct nl_msg *msg;
9706 int ret;
9707 u64 cookie;
9708
9709 msg = nlmsg_alloc();
9710 if (!msg)
9711 return -1;
9712
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009713 nl80211_cmd(drv, msg, 0, NL80211_CMD_REMAIN_ON_CHANNEL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009714
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009715 if (nl80211_set_iface_id(msg, bss) < 0)
9716 goto nla_put_failure;
9717
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009718 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
9719 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
9720
9721 cookie = 0;
9722 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009723 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009724 if (ret == 0) {
9725 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel cookie "
9726 "0x%llx for freq=%u MHz duration=%u",
9727 (long long unsigned int) cookie, freq, duration);
9728 drv->remain_on_chan_cookie = cookie;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009729 drv->pending_remain_on_chan = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009730 return 0;
9731 }
9732 wpa_printf(MSG_DEBUG, "nl80211: Failed to request remain-on-channel "
9733 "(freq=%d duration=%u): %d (%s)",
9734 freq, duration, ret, strerror(-ret));
9735nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009736 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009737 return -1;
9738}
9739
9740
9741static int wpa_driver_nl80211_cancel_remain_on_channel(void *priv)
9742{
9743 struct i802_bss *bss = priv;
9744 struct wpa_driver_nl80211_data *drv = bss->drv;
9745 struct nl_msg *msg;
9746 int ret;
9747
9748 if (!drv->pending_remain_on_chan) {
9749 wpa_printf(MSG_DEBUG, "nl80211: No pending remain-on-channel "
9750 "to cancel");
9751 return -1;
9752 }
9753
9754 wpa_printf(MSG_DEBUG, "nl80211: Cancel remain-on-channel with cookie "
9755 "0x%llx",
9756 (long long unsigned int) drv->remain_on_chan_cookie);
9757
9758 msg = nlmsg_alloc();
9759 if (!msg)
9760 return -1;
9761
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009762 nl80211_cmd(drv, msg, 0, NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009763
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009764 if (nl80211_set_iface_id(msg, bss) < 0)
9765 goto nla_put_failure;
9766
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009767 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->remain_on_chan_cookie);
9768
9769 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009770 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009771 if (ret == 0)
9772 return 0;
9773 wpa_printf(MSG_DEBUG, "nl80211: Failed to cancel remain-on-channel: "
9774 "%d (%s)", ret, strerror(-ret));
9775nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009776 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009777 return -1;
9778}
9779
9780
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009781static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss, int report)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009782{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009783 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07009784
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009785 if (!report) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009786 if (bss->nl_preq && drv->device_ap_sme &&
9787 is_ap_interface(drv->nlmode)) {
9788 /*
9789 * Do not disable Probe Request reporting that was
9790 * enabled in nl80211_setup_ap().
9791 */
9792 wpa_printf(MSG_DEBUG, "nl80211: Skip disabling of "
9793 "Probe Request reporting nl_preq=%p while "
9794 "in AP mode", bss->nl_preq);
9795 } else if (bss->nl_preq) {
9796 wpa_printf(MSG_DEBUG, "nl80211: Disable Probe Request "
9797 "reporting nl_preq=%p", bss->nl_preq);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07009798 nl80211_destroy_eloop_handle(&bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009799 }
9800 return 0;
9801 }
9802
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009803 if (bss->nl_preq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009804 wpa_printf(MSG_DEBUG, "nl80211: Probe Request reporting "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009805 "already on! nl_preq=%p", bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009806 return 0;
9807 }
9808
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009809 bss->nl_preq = nl_create_handle(drv->global->nl_cb, "preq");
9810 if (bss->nl_preq == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009811 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009812 wpa_printf(MSG_DEBUG, "nl80211: Enable Probe Request "
9813 "reporting nl_preq=%p", bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009814
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009815 if (nl80211_register_frame(bss, bss->nl_preq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009816 (WLAN_FC_TYPE_MGMT << 2) |
9817 (WLAN_FC_STYPE_PROBE_REQ << 4),
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009818 NULL, 0) < 0)
9819 goto out_err;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07009820
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07009821 nl80211_register_eloop_read(&bss->nl_preq,
9822 wpa_driver_nl80211_event_receive,
9823 bss->nl_cb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009824
9825 return 0;
9826
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009827 out_err:
9828 nl_destroy_handles(&bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009829 return -1;
9830}
9831
9832
9833static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
9834 int ifindex, int disabled)
9835{
9836 struct nl_msg *msg;
9837 struct nlattr *bands, *band;
9838 int ret;
9839
9840 msg = nlmsg_alloc();
9841 if (!msg)
9842 return -1;
9843
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009844 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_TX_BITRATE_MASK);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009845 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
9846
9847 bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES);
9848 if (!bands)
9849 goto nla_put_failure;
9850
9851 /*
9852 * Disable 2 GHz rates 1, 2, 5.5, 11 Mbps by masking out everything
9853 * else apart from 6, 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS
9854 * rates. All 5 GHz rates are left enabled.
9855 */
9856 band = nla_nest_start(msg, NL80211_BAND_2GHZ);
9857 if (!band)
9858 goto nla_put_failure;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009859 if (disabled) {
9860 NLA_PUT(msg, NL80211_TXRATE_LEGACY, 8,
9861 "\x0c\x12\x18\x24\x30\x48\x60\x6c");
9862 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009863 nla_nest_end(msg, band);
9864
9865 nla_nest_end(msg, bands);
9866
9867 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9868 msg = NULL;
9869 if (ret) {
9870 wpa_printf(MSG_DEBUG, "nl80211: Set TX rates failed: ret=%d "
9871 "(%s)", ret, strerror(-ret));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07009872 } else
9873 drv->disabled_11b_rates = disabled;
9874
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009875 return ret;
9876
9877nla_put_failure:
9878 nlmsg_free(msg);
9879 return -1;
9880}
9881
9882
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009883static int wpa_driver_nl80211_deinit_ap(void *priv)
9884{
9885 struct i802_bss *bss = priv;
9886 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009887 if (!is_ap_interface(drv->nlmode))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009888 return -1;
9889 wpa_driver_nl80211_del_beacon(drv);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07009890
9891 /*
9892 * If the P2P GO interface was dynamically added, then it is
9893 * possible that the interface change to station is not possible.
9894 */
9895 if (drv->nlmode == NL80211_IFTYPE_P2P_GO && bss->if_dynamic)
9896 return 0;
9897
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009898 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009899}
9900
9901
Dmitry Shmidtea69e842013-05-13 14:52:28 -07009902static int wpa_driver_nl80211_stop_ap(void *priv)
9903{
9904 struct i802_bss *bss = priv;
9905 struct wpa_driver_nl80211_data *drv = bss->drv;
9906 if (!is_ap_interface(drv->nlmode))
9907 return -1;
9908 wpa_driver_nl80211_del_beacon(drv);
9909 bss->beacon_set = 0;
9910 return 0;
9911}
9912
9913
Dmitry Shmidt04949592012-07-19 12:16:46 -07009914static int wpa_driver_nl80211_deinit_p2p_cli(void *priv)
9915{
9916 struct i802_bss *bss = priv;
9917 struct wpa_driver_nl80211_data *drv = bss->drv;
9918 if (drv->nlmode != NL80211_IFTYPE_P2P_CLIENT)
9919 return -1;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07009920
9921 /*
9922 * If the P2P Client interface was dynamically added, then it is
9923 * possible that the interface change to station is not possible.
9924 */
9925 if (bss->if_dynamic)
9926 return 0;
9927
Dmitry Shmidt04949592012-07-19 12:16:46 -07009928 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
9929}
9930
9931
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009932static void wpa_driver_nl80211_resume(void *priv)
9933{
9934 struct i802_bss *bss = priv;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009935
9936 if (i802_set_iface_flags(bss, 1))
9937 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface up on resume event");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009938}
9939
9940
9941static int nl80211_send_ft_action(void *priv, u8 action, const u8 *target_ap,
9942 const u8 *ies, size_t ies_len)
9943{
9944 struct i802_bss *bss = priv;
9945 struct wpa_driver_nl80211_data *drv = bss->drv;
9946 int ret;
9947 u8 *data, *pos;
9948 size_t data_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009949 const u8 *own_addr = bss->addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009950
9951 if (action != 1) {
9952 wpa_printf(MSG_ERROR, "nl80211: Unsupported send_ft_action "
9953 "action %d", action);
9954 return -1;
9955 }
9956
9957 /*
9958 * Action frame payload:
9959 * Category[1] = 6 (Fast BSS Transition)
9960 * Action[1] = 1 (Fast BSS Transition Request)
9961 * STA Address
9962 * Target AP Address
9963 * FT IEs
9964 */
9965
9966 data_len = 2 + 2 * ETH_ALEN + ies_len;
9967 data = os_malloc(data_len);
9968 if (data == NULL)
9969 return -1;
9970 pos = data;
9971 *pos++ = 0x06; /* FT Action category */
9972 *pos++ = action;
9973 os_memcpy(pos, own_addr, ETH_ALEN);
9974 pos += ETH_ALEN;
9975 os_memcpy(pos, target_ap, ETH_ALEN);
9976 pos += ETH_ALEN;
9977 os_memcpy(pos, ies, ies_len);
9978
9979 ret = wpa_driver_nl80211_send_action(bss, drv->assoc_freq, 0,
9980 drv->bssid, own_addr, drv->bssid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009981 data, data_len, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009982 os_free(data);
9983
9984 return ret;
9985}
9986
9987
9988static int nl80211_signal_monitor(void *priv, int threshold, int hysteresis)
9989{
9990 struct i802_bss *bss = priv;
9991 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07009992 struct nl_msg *msg;
9993 struct nlattr *cqm;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07009994 int ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009995
9996 wpa_printf(MSG_DEBUG, "nl80211: Signal monitor threshold=%d "
9997 "hysteresis=%d", threshold, hysteresis);
9998
9999 msg = nlmsg_alloc();
10000 if (!msg)
10001 return -1;
10002
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010003 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_CQM);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010004
10005 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
10006
Dmitry Shmidt8da800a2013-04-24 12:57:01 -070010007 cqm = nla_nest_start(msg, NL80211_ATTR_CQM);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010008 if (cqm == NULL)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070010009 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010010
Dmitry Shmidt8da800a2013-04-24 12:57:01 -070010011 NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_THOLD, threshold);
10012 NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_HYST, hysteresis);
10013 nla_nest_end(msg, cqm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010014
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070010015 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010016 msg = NULL;
10017
10018nla_put_failure:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010019 nlmsg_free(msg);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070010020 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010021}
10022
10023
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010024/* Converts nl80211_chan_width to a common format */
10025static enum chan_width convert2width(int width)
10026{
10027 switch (width) {
10028 case NL80211_CHAN_WIDTH_20_NOHT:
10029 return CHAN_WIDTH_20_NOHT;
10030 case NL80211_CHAN_WIDTH_20:
10031 return CHAN_WIDTH_20;
10032 case NL80211_CHAN_WIDTH_40:
10033 return CHAN_WIDTH_40;
10034 case NL80211_CHAN_WIDTH_80:
10035 return CHAN_WIDTH_80;
10036 case NL80211_CHAN_WIDTH_80P80:
10037 return CHAN_WIDTH_80P80;
10038 case NL80211_CHAN_WIDTH_160:
10039 return CHAN_WIDTH_160;
10040 }
10041 return CHAN_WIDTH_UNKNOWN;
10042}
10043
10044
10045static int get_channel_width(struct nl_msg *msg, void *arg)
10046{
10047 struct nlattr *tb[NL80211_ATTR_MAX + 1];
10048 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
10049 struct wpa_signal_info *sig_change = arg;
10050
10051 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
10052 genlmsg_attrlen(gnlh, 0), NULL);
10053
10054 sig_change->center_frq1 = -1;
10055 sig_change->center_frq2 = -1;
10056 sig_change->chanwidth = CHAN_WIDTH_UNKNOWN;
10057
10058 if (tb[NL80211_ATTR_CHANNEL_WIDTH]) {
10059 sig_change->chanwidth = convert2width(
10060 nla_get_u32(tb[NL80211_ATTR_CHANNEL_WIDTH]));
10061 if (tb[NL80211_ATTR_CENTER_FREQ1])
10062 sig_change->center_frq1 =
10063 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]);
10064 if (tb[NL80211_ATTR_CENTER_FREQ2])
10065 sig_change->center_frq2 =
10066 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]);
10067 }
10068
10069 return NL_SKIP;
10070}
10071
10072
10073static int nl80211_get_channel_width(struct wpa_driver_nl80211_data *drv,
10074 struct wpa_signal_info *sig)
10075{
10076 struct nl_msg *msg;
10077
10078 msg = nlmsg_alloc();
10079 if (!msg)
10080 return -ENOMEM;
10081
10082 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_INTERFACE);
10083 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
10084
10085 return send_and_recv_msgs(drv, msg, get_channel_width, sig);
10086
10087nla_put_failure:
10088 nlmsg_free(msg);
10089 return -ENOBUFS;
10090}
10091
10092
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010093static int nl80211_signal_poll(void *priv, struct wpa_signal_info *si)
10094{
10095 struct i802_bss *bss = priv;
10096 struct wpa_driver_nl80211_data *drv = bss->drv;
10097 int res;
10098
10099 os_memset(si, 0, sizeof(*si));
10100 res = nl80211_get_link_signal(drv, si);
10101 if (res != 0)
10102 return res;
10103
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010104 res = nl80211_get_channel_width(drv, si);
10105 if (res != 0)
10106 return res;
10107
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010108 return nl80211_get_link_noise(drv, si);
10109}
10110
10111
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010112static int wpa_driver_nl80211_shared_freq(void *priv)
10113{
10114 struct i802_bss *bss = priv;
10115 struct wpa_driver_nl80211_data *drv = bss->drv;
10116 struct wpa_driver_nl80211_data *driver;
10117 int freq = 0;
10118
10119 /*
10120 * If the same PHY is in connected state with some other interface,
10121 * then retrieve the assoc freq.
10122 */
10123 wpa_printf(MSG_DEBUG, "nl80211: Get shared freq for PHY %s",
10124 drv->phyname);
10125
10126 dl_list_for_each(driver, &drv->global->interfaces,
10127 struct wpa_driver_nl80211_data, list) {
10128 if (drv == driver ||
10129 os_strcmp(drv->phyname, driver->phyname) != 0 ||
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010130 !driver->associated)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010131 continue;
10132
10133 wpa_printf(MSG_DEBUG, "nl80211: Found a match for PHY %s - %s "
10134 MACSTR,
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010135 driver->phyname, driver->first_bss->ifname,
10136 MAC2STR(driver->first_bss->addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -070010137 if (is_ap_interface(driver->nlmode))
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010138 freq = driver->first_bss->freq;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010139 else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010140 freq = nl80211_get_assoc_freq(driver);
10141 wpa_printf(MSG_DEBUG, "nl80211: Shared freq for PHY %s: %d",
10142 drv->phyname, freq);
10143 }
10144
10145 if (!freq)
10146 wpa_printf(MSG_DEBUG, "nl80211: No shared interface for "
10147 "PHY (%s) in associated state", drv->phyname);
10148
10149 return freq;
10150}
10151
10152
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010153static int nl80211_send_frame(void *priv, const u8 *data, size_t data_len,
10154 int encrypt)
10155{
10156 struct i802_bss *bss = priv;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080010157 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt, 0,
10158 0, 0, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010159}
10160
10161
10162static int nl80211_set_param(void *priv, const char *param)
10163{
10164 wpa_printf(MSG_DEBUG, "nl80211: driver param='%s'", param);
10165 if (param == NULL)
10166 return 0;
10167
10168#ifdef CONFIG_P2P
10169 if (os_strstr(param, "use_p2p_group_interface=1")) {
10170 struct i802_bss *bss = priv;
10171 struct wpa_driver_nl80211_data *drv = bss->drv;
10172
10173 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
10174 "interface");
10175 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
10176 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
10177 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010178
10179 if (os_strstr(param, "p2p_device=1")) {
10180 struct i802_bss *bss = priv;
10181 struct wpa_driver_nl80211_data *drv = bss->drv;
10182 drv->allow_p2p_device = 1;
10183 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010184#endif /* CONFIG_P2P */
10185
10186 return 0;
10187}
10188
10189
10190static void * nl80211_global_init(void)
10191{
10192 struct nl80211_global *global;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010193 struct netlink_config *cfg;
10194
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010195 global = os_zalloc(sizeof(*global));
10196 if (global == NULL)
10197 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010198 global->ioctl_sock = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010199 dl_list_init(&global->interfaces);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010200 global->if_add_ifindex = -1;
10201
10202 cfg = os_zalloc(sizeof(*cfg));
10203 if (cfg == NULL)
10204 goto err;
10205
10206 cfg->ctx = global;
10207 cfg->newlink_cb = wpa_driver_nl80211_event_rtm_newlink;
10208 cfg->dellink_cb = wpa_driver_nl80211_event_rtm_dellink;
10209 global->netlink = netlink_init(cfg);
10210 if (global->netlink == NULL) {
10211 os_free(cfg);
10212 goto err;
10213 }
10214
10215 if (wpa_driver_nl80211_init_nl_global(global) < 0)
10216 goto err;
10217
10218 global->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
10219 if (global->ioctl_sock < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070010220 wpa_printf(MSG_ERROR, "nl80211: socket(PF_INET,SOCK_DGRAM) failed: %s",
10221 strerror(errno));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010222 goto err;
10223 }
10224
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010225 return global;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010226
10227err:
10228 nl80211_global_deinit(global);
10229 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010230}
10231
10232
10233static void nl80211_global_deinit(void *priv)
10234{
10235 struct nl80211_global *global = priv;
10236 if (global == NULL)
10237 return;
10238 if (!dl_list_empty(&global->interfaces)) {
10239 wpa_printf(MSG_ERROR, "nl80211: %u interface(s) remain at "
10240 "nl80211_global_deinit",
10241 dl_list_len(&global->interfaces));
10242 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010243
10244 if (global->netlink)
10245 netlink_deinit(global->netlink);
10246
10247 nl_destroy_handles(&global->nl);
10248
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070010249 if (global->nl_event)
10250 nl80211_destroy_eloop_handle(&global->nl_event);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010251
10252 nl_cb_put(global->nl_cb);
10253
10254 if (global->ioctl_sock >= 0)
10255 close(global->ioctl_sock);
10256
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010257 os_free(global);
10258}
10259
10260
10261static const char * nl80211_get_radio_name(void *priv)
10262{
10263 struct i802_bss *bss = priv;
10264 struct wpa_driver_nl80211_data *drv = bss->drv;
10265 return drv->phyname;
10266}
10267
10268
Jouni Malinen75ecf522011-06-27 15:19:46 -070010269static int nl80211_pmkid(struct i802_bss *bss, int cmd, const u8 *bssid,
10270 const u8 *pmkid)
10271{
10272 struct nl_msg *msg;
10273
10274 msg = nlmsg_alloc();
10275 if (!msg)
10276 return -ENOMEM;
10277
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010278 nl80211_cmd(bss->drv, msg, 0, cmd);
Jouni Malinen75ecf522011-06-27 15:19:46 -070010279
10280 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
10281 if (pmkid)
10282 NLA_PUT(msg, NL80211_ATTR_PMKID, 16, pmkid);
10283 if (bssid)
10284 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
10285
10286 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
10287 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010288 nlmsg_free(msg);
Jouni Malinen75ecf522011-06-27 15:19:46 -070010289 return -ENOBUFS;
10290}
10291
10292
10293static int nl80211_add_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
10294{
10295 struct i802_bss *bss = priv;
10296 wpa_printf(MSG_DEBUG, "nl80211: Add PMKID for " MACSTR, MAC2STR(bssid));
10297 return nl80211_pmkid(bss, NL80211_CMD_SET_PMKSA, bssid, pmkid);
10298}
10299
10300
10301static int nl80211_remove_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
10302{
10303 struct i802_bss *bss = priv;
10304 wpa_printf(MSG_DEBUG, "nl80211: Delete PMKID for " MACSTR,
10305 MAC2STR(bssid));
10306 return nl80211_pmkid(bss, NL80211_CMD_DEL_PMKSA, bssid, pmkid);
10307}
10308
10309
10310static int nl80211_flush_pmkid(void *priv)
10311{
10312 struct i802_bss *bss = priv;
10313 wpa_printf(MSG_DEBUG, "nl80211: Flush PMKIDs");
10314 return nl80211_pmkid(bss, NL80211_CMD_FLUSH_PMKSA, NULL, NULL);
10315}
10316
10317
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070010318static void clean_survey_results(struct survey_results *survey_results)
10319{
10320 struct freq_survey *survey, *tmp;
10321
10322 if (dl_list_empty(&survey_results->survey_list))
10323 return;
10324
10325 dl_list_for_each_safe(survey, tmp, &survey_results->survey_list,
10326 struct freq_survey, list) {
10327 dl_list_del(&survey->list);
10328 os_free(survey);
10329 }
10330}
10331
10332
10333static void add_survey(struct nlattr **sinfo, u32 ifidx,
10334 struct dl_list *survey_list)
10335{
10336 struct freq_survey *survey;
10337
10338 survey = os_zalloc(sizeof(struct freq_survey));
10339 if (!survey)
10340 return;
10341
10342 survey->ifidx = ifidx;
10343 survey->freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
10344 survey->filled = 0;
10345
10346 if (sinfo[NL80211_SURVEY_INFO_NOISE]) {
10347 survey->nf = (int8_t)
10348 nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
10349 survey->filled |= SURVEY_HAS_NF;
10350 }
10351
10352 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]) {
10353 survey->channel_time =
10354 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]);
10355 survey->filled |= SURVEY_HAS_CHAN_TIME;
10356 }
10357
10358 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]) {
10359 survey->channel_time_busy =
10360 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]);
10361 survey->filled |= SURVEY_HAS_CHAN_TIME_BUSY;
10362 }
10363
10364 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]) {
10365 survey->channel_time_rx =
10366 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]);
10367 survey->filled |= SURVEY_HAS_CHAN_TIME_RX;
10368 }
10369
10370 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]) {
10371 survey->channel_time_tx =
10372 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]);
10373 survey->filled |= SURVEY_HAS_CHAN_TIME_TX;
10374 }
10375
10376 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)",
10377 survey->freq,
10378 survey->nf,
10379 (unsigned long int) survey->channel_time,
10380 (unsigned long int) survey->channel_time_busy,
10381 (unsigned long int) survey->channel_time_tx,
10382 (unsigned long int) survey->channel_time_rx,
10383 survey->filled);
10384
10385 dl_list_add_tail(survey_list, &survey->list);
10386}
10387
10388
10389static int check_survey_ok(struct nlattr **sinfo, u32 surveyed_freq,
10390 unsigned int freq_filter)
10391{
10392 if (!freq_filter)
10393 return 1;
10394
10395 return freq_filter == surveyed_freq;
10396}
10397
10398
10399static int survey_handler(struct nl_msg *msg, void *arg)
10400{
10401 struct nlattr *tb[NL80211_ATTR_MAX + 1];
10402 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
10403 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
10404 struct survey_results *survey_results;
10405 u32 surveyed_freq = 0;
10406 u32 ifidx;
10407
10408 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
10409 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
10410 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
10411 };
10412
10413 survey_results = (struct survey_results *) arg;
10414
10415 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
10416 genlmsg_attrlen(gnlh, 0), NULL);
10417
10418 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
10419
10420 if (!tb[NL80211_ATTR_SURVEY_INFO])
10421 return NL_SKIP;
10422
10423 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
10424 tb[NL80211_ATTR_SURVEY_INFO],
10425 survey_policy))
10426 return NL_SKIP;
10427
10428 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY]) {
10429 wpa_printf(MSG_ERROR, "nl80211: Invalid survey data");
10430 return NL_SKIP;
10431 }
10432
10433 surveyed_freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
10434
10435 if (!check_survey_ok(sinfo, surveyed_freq,
10436 survey_results->freq_filter))
10437 return NL_SKIP;
10438
10439 if (survey_results->freq_filter &&
10440 survey_results->freq_filter != surveyed_freq) {
10441 wpa_printf(MSG_EXCESSIVE, "nl80211: Ignoring survey data for freq %d MHz",
10442 surveyed_freq);
10443 return NL_SKIP;
10444 }
10445
10446 add_survey(sinfo, ifidx, &survey_results->survey_list);
10447
10448 return NL_SKIP;
10449}
10450
10451
10452static int wpa_driver_nl80211_get_survey(void *priv, unsigned int freq)
10453{
10454 struct i802_bss *bss = priv;
10455 struct wpa_driver_nl80211_data *drv = bss->drv;
10456 struct nl_msg *msg;
10457 int err = -ENOBUFS;
10458 union wpa_event_data data;
10459 struct survey_results *survey_results;
10460
10461 os_memset(&data, 0, sizeof(data));
10462 survey_results = &data.survey_results;
10463
10464 dl_list_init(&survey_results->survey_list);
10465
10466 msg = nlmsg_alloc();
10467 if (!msg)
10468 goto nla_put_failure;
10469
10470 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
10471
10472 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
10473
10474 if (freq)
10475 data.survey_results.freq_filter = freq;
10476
10477 do {
10478 wpa_printf(MSG_DEBUG, "nl80211: Fetch survey data");
10479 err = send_and_recv_msgs(drv, msg, survey_handler,
10480 survey_results);
10481 } while (err > 0);
10482
10483 if (err) {
10484 wpa_printf(MSG_ERROR, "nl80211: Failed to process survey data");
10485 goto out_clean;
10486 }
10487
10488 wpa_supplicant_event(drv->ctx, EVENT_SURVEY, &data);
10489
10490out_clean:
10491 clean_survey_results(survey_results);
10492nla_put_failure:
10493 return err;
10494}
10495
10496
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010497static void nl80211_set_rekey_info(void *priv, const u8 *kek, const u8 *kck,
10498 const u8 *replay_ctr)
10499{
10500 struct i802_bss *bss = priv;
10501 struct wpa_driver_nl80211_data *drv = bss->drv;
10502 struct nlattr *replay_nested;
10503 struct nl_msg *msg;
10504
10505 msg = nlmsg_alloc();
10506 if (!msg)
10507 return;
10508
10509 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
10510
10511 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
10512
10513 replay_nested = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
10514 if (!replay_nested)
10515 goto nla_put_failure;
10516
10517 NLA_PUT(msg, NL80211_REKEY_DATA_KEK, NL80211_KEK_LEN, kek);
10518 NLA_PUT(msg, NL80211_REKEY_DATA_KCK, NL80211_KCK_LEN, kck);
10519 NLA_PUT(msg, NL80211_REKEY_DATA_REPLAY_CTR, NL80211_REPLAY_CTR_LEN,
10520 replay_ctr);
10521
10522 nla_nest_end(msg, replay_nested);
10523
10524 send_and_recv_msgs(drv, msg, NULL, NULL);
10525 return;
10526 nla_put_failure:
10527 nlmsg_free(msg);
10528}
10529
10530
10531static void nl80211_send_null_frame(struct i802_bss *bss, const u8 *own_addr,
10532 const u8 *addr, int qos)
10533{
10534 /* send data frame to poll STA and check whether
10535 * this frame is ACKed */
10536 struct {
10537 struct ieee80211_hdr hdr;
10538 u16 qos_ctl;
10539 } STRUCT_PACKED nulldata;
10540 size_t size;
10541
10542 /* Send data frame to poll STA and check whether this frame is ACKed */
10543
10544 os_memset(&nulldata, 0, sizeof(nulldata));
10545
10546 if (qos) {
10547 nulldata.hdr.frame_control =
10548 IEEE80211_FC(WLAN_FC_TYPE_DATA,
10549 WLAN_FC_STYPE_QOS_NULL);
10550 size = sizeof(nulldata);
10551 } else {
10552 nulldata.hdr.frame_control =
10553 IEEE80211_FC(WLAN_FC_TYPE_DATA,
10554 WLAN_FC_STYPE_NULLFUNC);
10555 size = sizeof(struct ieee80211_hdr);
10556 }
10557
10558 nulldata.hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS);
10559 os_memcpy(nulldata.hdr.IEEE80211_DA_FROMDS, addr, ETH_ALEN);
10560 os_memcpy(nulldata.hdr.IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
10561 os_memcpy(nulldata.hdr.IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
10562
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010563 if (wpa_driver_nl80211_send_mlme(bss, (u8 *) &nulldata, size, 0, 0, 0,
10564 0, 0) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010565 wpa_printf(MSG_DEBUG, "nl80211_send_null_frame: Failed to "
10566 "send poll frame");
10567}
10568
10569static void nl80211_poll_client(void *priv, const u8 *own_addr, const u8 *addr,
10570 int qos)
10571{
10572 struct i802_bss *bss = priv;
10573 struct wpa_driver_nl80211_data *drv = bss->drv;
10574 struct nl_msg *msg;
10575
10576 if (!drv->poll_command_supported) {
10577 nl80211_send_null_frame(bss, own_addr, addr, qos);
10578 return;
10579 }
10580
10581 msg = nlmsg_alloc();
10582 if (!msg)
10583 return;
10584
10585 nl80211_cmd(drv, msg, 0, NL80211_CMD_PROBE_CLIENT);
10586
10587 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
10588 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
10589
10590 send_and_recv_msgs(drv, msg, NULL, NULL);
10591 return;
10592 nla_put_failure:
10593 nlmsg_free(msg);
10594}
10595
10596
10597static int nl80211_set_power_save(struct i802_bss *bss, int enabled)
10598{
10599 struct nl_msg *msg;
10600
10601 msg = nlmsg_alloc();
10602 if (!msg)
10603 return -ENOMEM;
10604
10605 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_SET_POWER_SAVE);
10606 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
10607 NLA_PUT_U32(msg, NL80211_ATTR_PS_STATE,
10608 enabled ? NL80211_PS_ENABLED : NL80211_PS_DISABLED);
10609 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
10610nla_put_failure:
10611 nlmsg_free(msg);
10612 return -ENOBUFS;
10613}
10614
10615
10616static int nl80211_set_p2p_powersave(void *priv, int legacy_ps, int opp_ps,
10617 int ctwindow)
10618{
10619 struct i802_bss *bss = priv;
10620
10621 wpa_printf(MSG_DEBUG, "nl80211: set_p2p_powersave (legacy_ps=%d "
10622 "opp_ps=%d ctwindow=%d)", legacy_ps, opp_ps, ctwindow);
10623
10624 if (opp_ps != -1 || ctwindow != -1)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080010625#ifdef ANDROID_P2P
10626 wpa_driver_set_p2p_ps(priv, legacy_ps, opp_ps, ctwindow);
10627#else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010628 return -1; /* Not yet supported */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080010629#endif
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010630
10631 if (legacy_ps == -1)
10632 return 0;
10633 if (legacy_ps != 0 && legacy_ps != 1)
10634 return -1; /* Not yet supported */
10635
10636 return nl80211_set_power_save(bss, legacy_ps);
10637}
10638
10639
Dmitry Shmidt051af732013-10-22 13:52:46 -070010640static int nl80211_start_radar_detection(void *priv,
10641 struct hostapd_freq_params *freq)
Dmitry Shmidtea69e842013-05-13 14:52:28 -070010642{
10643 struct i802_bss *bss = priv;
10644 struct wpa_driver_nl80211_data *drv = bss->drv;
10645 struct nl_msg *msg;
10646 int ret;
10647
Dmitry Shmidt051af732013-10-22 13:52:46 -070010648 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)",
10649 freq->freq, freq->ht_enabled, freq->vht_enabled,
10650 freq->bandwidth, freq->center_freq1, freq->center_freq2);
10651
Dmitry Shmidtea69e842013-05-13 14:52:28 -070010652 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_RADAR)) {
10653 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support radar "
10654 "detection");
10655 return -1;
10656 }
10657
10658 msg = nlmsg_alloc();
10659 if (!msg)
10660 return -1;
10661
10662 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_RADAR_DETECT);
10663 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
Dmitry Shmidt051af732013-10-22 13:52:46 -070010664 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq->freq);
Dmitry Shmidtea69e842013-05-13 14:52:28 -070010665
Dmitry Shmidt051af732013-10-22 13:52:46 -070010666 if (freq->vht_enabled) {
10667 switch (freq->bandwidth) {
10668 case 20:
10669 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
10670 NL80211_CHAN_WIDTH_20);
10671 break;
10672 case 40:
10673 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
10674 NL80211_CHAN_WIDTH_40);
10675 break;
10676 case 80:
10677 if (freq->center_freq2)
10678 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
10679 NL80211_CHAN_WIDTH_80P80);
10680 else
10681 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
10682 NL80211_CHAN_WIDTH_80);
10683 break;
10684 case 160:
10685 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
10686 NL80211_CHAN_WIDTH_160);
10687 break;
10688 default:
10689 return -1;
10690 }
10691 NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ1, freq->center_freq1);
10692 if (freq->center_freq2)
10693 NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ2,
10694 freq->center_freq2);
10695 } else if (freq->ht_enabled) {
10696 switch (freq->sec_channel_offset) {
10697 case -1:
10698 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
10699 NL80211_CHAN_HT40MINUS);
10700 break;
10701 case 1:
10702 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
10703 NL80211_CHAN_HT40PLUS);
10704 break;
10705 default:
10706 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
10707 NL80211_CHAN_HT20);
10708 break;
10709 }
10710 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -070010711
10712 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
10713 if (ret == 0)
10714 return 0;
10715 wpa_printf(MSG_DEBUG, "nl80211: Failed to start radar detection: "
10716 "%d (%s)", ret, strerror(-ret));
10717nla_put_failure:
10718 return -1;
10719}
10720
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010721#ifdef CONFIG_TDLS
10722
10723static int nl80211_send_tdls_mgmt(void *priv, const u8 *dst, u8 action_code,
10724 u8 dialog_token, u16 status_code,
10725 const u8 *buf, size_t len)
10726{
10727 struct i802_bss *bss = priv;
10728 struct wpa_driver_nl80211_data *drv = bss->drv;
10729 struct nl_msg *msg;
10730
10731 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
10732 return -EOPNOTSUPP;
10733
10734 if (!dst)
10735 return -EINVAL;
10736
10737 msg = nlmsg_alloc();
10738 if (!msg)
10739 return -ENOMEM;
10740
10741 nl80211_cmd(drv, msg, 0, NL80211_CMD_TDLS_MGMT);
10742 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
10743 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
10744 NLA_PUT_U8(msg, NL80211_ATTR_TDLS_ACTION, action_code);
10745 NLA_PUT_U8(msg, NL80211_ATTR_TDLS_DIALOG_TOKEN, dialog_token);
10746 NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status_code);
10747 NLA_PUT(msg, NL80211_ATTR_IE, len, buf);
10748
10749 return send_and_recv_msgs(drv, msg, NULL, NULL);
10750
10751nla_put_failure:
10752 nlmsg_free(msg);
10753 return -ENOBUFS;
10754}
10755
10756
10757static int nl80211_tdls_oper(void *priv, enum tdls_oper oper, const u8 *peer)
10758{
10759 struct i802_bss *bss = priv;
10760 struct wpa_driver_nl80211_data *drv = bss->drv;
10761 struct nl_msg *msg;
10762 enum nl80211_tdls_operation nl80211_oper;
10763
10764 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
10765 return -EOPNOTSUPP;
10766
10767 switch (oper) {
10768 case TDLS_DISCOVERY_REQ:
10769 nl80211_oper = NL80211_TDLS_DISCOVERY_REQ;
10770 break;
10771 case TDLS_SETUP:
10772 nl80211_oper = NL80211_TDLS_SETUP;
10773 break;
10774 case TDLS_TEARDOWN:
10775 nl80211_oper = NL80211_TDLS_TEARDOWN;
10776 break;
10777 case TDLS_ENABLE_LINK:
10778 nl80211_oper = NL80211_TDLS_ENABLE_LINK;
10779 break;
10780 case TDLS_DISABLE_LINK:
10781 nl80211_oper = NL80211_TDLS_DISABLE_LINK;
10782 break;
10783 case TDLS_ENABLE:
10784 return 0;
10785 case TDLS_DISABLE:
10786 return 0;
10787 default:
10788 return -EINVAL;
10789 }
10790
10791 msg = nlmsg_alloc();
10792 if (!msg)
10793 return -ENOMEM;
10794
10795 nl80211_cmd(drv, msg, 0, NL80211_CMD_TDLS_OPER);
10796 NLA_PUT_U8(msg, NL80211_ATTR_TDLS_OPERATION, nl80211_oper);
10797 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
10798 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, peer);
10799
10800 return send_and_recv_msgs(drv, msg, NULL, NULL);
10801
10802nla_put_failure:
10803 nlmsg_free(msg);
10804 return -ENOBUFS;
10805}
10806
10807#endif /* CONFIG TDLS */
10808
10809
10810#ifdef ANDROID
10811
10812typedef struct android_wifi_priv_cmd {
10813 char *buf;
10814 int used_len;
10815 int total_len;
10816} android_wifi_priv_cmd;
10817
10818static int drv_errors = 0;
10819
10820static void wpa_driver_send_hang_msg(struct wpa_driver_nl80211_data *drv)
10821{
10822 drv_errors++;
10823 if (drv_errors > DRV_NUMBER_SEQUENTIAL_ERRORS) {
10824 drv_errors = 0;
10825 wpa_msg(drv->ctx, MSG_INFO, WPA_EVENT_DRIVER_STATE "HANGED");
10826 }
10827}
10828
10829
10830static int android_priv_cmd(struct i802_bss *bss, const char *cmd)
10831{
10832 struct wpa_driver_nl80211_data *drv = bss->drv;
10833 struct ifreq ifr;
10834 android_wifi_priv_cmd priv_cmd;
10835 char buf[MAX_DRV_CMD_SIZE];
10836 int ret;
10837
10838 os_memset(&ifr, 0, sizeof(ifr));
10839 os_memset(&priv_cmd, 0, sizeof(priv_cmd));
10840 os_strlcpy(ifr.ifr_name, bss->ifname, IFNAMSIZ);
10841
10842 os_memset(buf, 0, sizeof(buf));
10843 os_strlcpy(buf, cmd, sizeof(buf));
10844
10845 priv_cmd.buf = buf;
10846 priv_cmd.used_len = sizeof(buf);
10847 priv_cmd.total_len = sizeof(buf);
10848 ifr.ifr_data = &priv_cmd;
10849
10850 ret = ioctl(drv->global->ioctl_sock, SIOCDEVPRIVATE + 1, &ifr);
10851 if (ret < 0) {
10852 wpa_printf(MSG_ERROR, "%s: failed to issue private commands",
10853 __func__);
10854 wpa_driver_send_hang_msg(drv);
10855 return ret;
10856 }
10857
10858 drv_errors = 0;
10859 return 0;
10860}
10861
10862
10863static int android_pno_start(struct i802_bss *bss,
10864 struct wpa_driver_scan_params *params)
10865{
10866 struct wpa_driver_nl80211_data *drv = bss->drv;
10867 struct ifreq ifr;
10868 android_wifi_priv_cmd priv_cmd;
10869 int ret = 0, i = 0, bp;
10870 char buf[WEXT_PNO_MAX_COMMAND_SIZE];
10871
10872 bp = WEXT_PNOSETUP_HEADER_SIZE;
10873 os_memcpy(buf, WEXT_PNOSETUP_HEADER, bp);
10874 buf[bp++] = WEXT_PNO_TLV_PREFIX;
10875 buf[bp++] = WEXT_PNO_TLV_VERSION;
10876 buf[bp++] = WEXT_PNO_TLV_SUBVERSION;
10877 buf[bp++] = WEXT_PNO_TLV_RESERVED;
10878
10879 while (i < WEXT_PNO_AMOUNT && (size_t) i < params->num_ssids) {
10880 /* Check that there is enough space needed for 1 more SSID, the
10881 * other sections and null termination */
10882 if ((bp + WEXT_PNO_SSID_HEADER_SIZE + MAX_SSID_LEN +
10883 WEXT_PNO_NONSSID_SECTIONS_SIZE + 1) >= (int) sizeof(buf))
10884 break;
10885 wpa_hexdump_ascii(MSG_DEBUG, "For PNO Scan",
10886 params->ssids[i].ssid,
10887 params->ssids[i].ssid_len);
10888 buf[bp++] = WEXT_PNO_SSID_SECTION;
10889 buf[bp++] = params->ssids[i].ssid_len;
10890 os_memcpy(&buf[bp], params->ssids[i].ssid,
10891 params->ssids[i].ssid_len);
10892 bp += params->ssids[i].ssid_len;
10893 i++;
10894 }
10895
10896 buf[bp++] = WEXT_PNO_SCAN_INTERVAL_SECTION;
10897 os_snprintf(&buf[bp], WEXT_PNO_SCAN_INTERVAL_LENGTH + 1, "%x",
10898 WEXT_PNO_SCAN_INTERVAL);
10899 bp += WEXT_PNO_SCAN_INTERVAL_LENGTH;
10900
10901 buf[bp++] = WEXT_PNO_REPEAT_SECTION;
10902 os_snprintf(&buf[bp], WEXT_PNO_REPEAT_LENGTH + 1, "%x",
10903 WEXT_PNO_REPEAT);
10904 bp += WEXT_PNO_REPEAT_LENGTH;
10905
10906 buf[bp++] = WEXT_PNO_MAX_REPEAT_SECTION;
10907 os_snprintf(&buf[bp], WEXT_PNO_MAX_REPEAT_LENGTH + 1, "%x",
10908 WEXT_PNO_MAX_REPEAT);
10909 bp += WEXT_PNO_MAX_REPEAT_LENGTH + 1;
10910
10911 memset(&ifr, 0, sizeof(ifr));
10912 memset(&priv_cmd, 0, sizeof(priv_cmd));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070010913 os_strlcpy(ifr.ifr_name, bss->ifname, IFNAMSIZ);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010914
10915 priv_cmd.buf = buf;
10916 priv_cmd.used_len = bp;
10917 priv_cmd.total_len = bp;
10918 ifr.ifr_data = &priv_cmd;
10919
10920 ret = ioctl(drv->global->ioctl_sock, SIOCDEVPRIVATE + 1, &ifr);
10921
10922 if (ret < 0) {
10923 wpa_printf(MSG_ERROR, "ioctl[SIOCSIWPRIV] (pnosetup): %d",
10924 ret);
10925 wpa_driver_send_hang_msg(drv);
10926 return ret;
10927 }
10928
10929 drv_errors = 0;
10930
10931 return android_priv_cmd(bss, "PNOFORCE 1");
10932}
10933
10934
10935static int android_pno_stop(struct i802_bss *bss)
10936{
10937 return android_priv_cmd(bss, "PNOFORCE 0");
10938}
10939
10940#endif /* ANDROID */
10941
10942
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010943static int driver_nl80211_set_key(const char *ifname, void *priv,
10944 enum wpa_alg alg, const u8 *addr,
10945 int key_idx, int set_tx,
10946 const u8 *seq, size_t seq_len,
10947 const u8 *key, size_t key_len)
10948{
10949 struct i802_bss *bss = priv;
10950 return wpa_driver_nl80211_set_key(ifname, bss, alg, addr, key_idx,
10951 set_tx, seq, seq_len, key, key_len);
10952}
10953
10954
10955static int driver_nl80211_scan2(void *priv,
10956 struct wpa_driver_scan_params *params)
10957{
10958 struct i802_bss *bss = priv;
10959 return wpa_driver_nl80211_scan(bss, params);
10960}
10961
10962
10963static int driver_nl80211_deauthenticate(void *priv, const u8 *addr,
10964 int reason_code)
10965{
10966 struct i802_bss *bss = priv;
10967 return wpa_driver_nl80211_deauthenticate(bss, addr, reason_code);
10968}
10969
10970
10971static int driver_nl80211_authenticate(void *priv,
10972 struct wpa_driver_auth_params *params)
10973{
10974 struct i802_bss *bss = priv;
10975 return wpa_driver_nl80211_authenticate(bss, params);
10976}
10977
10978
10979static void driver_nl80211_deinit(void *priv)
10980{
10981 struct i802_bss *bss = priv;
10982 wpa_driver_nl80211_deinit(bss);
10983}
10984
10985
10986static int driver_nl80211_if_remove(void *priv, enum wpa_driver_if_type type,
10987 const char *ifname)
10988{
10989 struct i802_bss *bss = priv;
10990 return wpa_driver_nl80211_if_remove(bss, type, ifname);
10991}
10992
10993
10994static int driver_nl80211_send_mlme(void *priv, const u8 *data,
10995 size_t data_len, int noack)
10996{
10997 struct i802_bss *bss = priv;
10998 return wpa_driver_nl80211_send_mlme(bss, data, data_len, noack,
10999 0, 0, 0, 0);
11000}
11001
11002
11003static int driver_nl80211_sta_remove(void *priv, const u8 *addr)
11004{
11005 struct i802_bss *bss = priv;
11006 return wpa_driver_nl80211_sta_remove(bss, addr);
11007}
11008
11009
11010#if defined(HOSTAPD) || defined(CONFIG_AP)
11011static int driver_nl80211_set_sta_vlan(void *priv, const u8 *addr,
11012 const char *ifname, int vlan_id)
11013{
11014 struct i802_bss *bss = priv;
11015 return i802_set_sta_vlan(bss, addr, ifname, vlan_id);
11016}
11017#endif /* HOSTAPD || CONFIG_AP */
11018
11019
11020static int driver_nl80211_read_sta_data(void *priv,
11021 struct hostap_sta_driver_data *data,
11022 const u8 *addr)
11023{
11024 struct i802_bss *bss = priv;
11025 return i802_read_sta_data(bss, data, addr);
11026}
11027
11028
11029static int driver_nl80211_send_action(void *priv, unsigned int freq,
11030 unsigned int wait_time,
11031 const u8 *dst, const u8 *src,
11032 const u8 *bssid,
11033 const u8 *data, size_t data_len,
11034 int no_cck)
11035{
11036 struct i802_bss *bss = priv;
11037 return wpa_driver_nl80211_send_action(bss, freq, wait_time, dst, src,
11038 bssid, data, data_len, no_cck);
11039}
11040
11041
11042static int driver_nl80211_probe_req_report(void *priv, int report)
11043{
11044 struct i802_bss *bss = priv;
11045 return wpa_driver_nl80211_probe_req_report(bss, report);
11046}
11047
11048
Dmitry Shmidt700a1372013-03-15 14:14:44 -070011049static int wpa_driver_nl80211_update_ft_ies(void *priv, const u8 *md,
11050 const u8 *ies, size_t ies_len)
11051{
11052 int ret;
11053 struct nl_msg *msg;
11054 struct i802_bss *bss = priv;
11055 struct wpa_driver_nl80211_data *drv = bss->drv;
11056 u16 mdid = WPA_GET_LE16(md);
11057
11058 msg = nlmsg_alloc();
11059 if (!msg)
11060 return -ENOMEM;
11061
11062 wpa_printf(MSG_DEBUG, "nl80211: Updating FT IEs");
11063 nl80211_cmd(drv, msg, 0, NL80211_CMD_UPDATE_FT_IES);
11064 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
11065 NLA_PUT(msg, NL80211_ATTR_IE, ies_len, ies);
11066 NLA_PUT_U16(msg, NL80211_ATTR_MDID, mdid);
11067
11068 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
11069 if (ret) {
11070 wpa_printf(MSG_DEBUG, "nl80211: update_ft_ies failed "
11071 "err=%d (%s)", ret, strerror(-ret));
11072 }
11073
11074 return ret;
11075
11076nla_put_failure:
11077 nlmsg_free(msg);
11078 return -ENOBUFS;
11079}
11080
11081
Dmitry Shmidt34af3062013-07-11 10:46:32 -070011082const u8 * wpa_driver_nl80211_get_macaddr(void *priv)
11083{
11084 struct i802_bss *bss = priv;
11085 struct wpa_driver_nl80211_data *drv = bss->drv;
11086
11087 if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE)
11088 return NULL;
11089
11090 return bss->addr;
11091}
11092
11093
Dmitry Shmidt56052862013-10-04 10:23:25 -070011094static const char * scan_state_str(enum scan_states scan_state)
11095{
11096 switch (scan_state) {
11097 case NO_SCAN:
11098 return "NO_SCAN";
11099 case SCAN_REQUESTED:
11100 return "SCAN_REQUESTED";
11101 case SCAN_STARTED:
11102 return "SCAN_STARTED";
11103 case SCAN_COMPLETED:
11104 return "SCAN_COMPLETED";
11105 case SCAN_ABORTED:
11106 return "SCAN_ABORTED";
11107 case SCHED_SCAN_STARTED:
11108 return "SCHED_SCAN_STARTED";
11109 case SCHED_SCAN_STOPPED:
11110 return "SCHED_SCAN_STOPPED";
11111 case SCHED_SCAN_RESULTS:
11112 return "SCHED_SCAN_RESULTS";
11113 }
11114
11115 return "??";
11116}
11117
11118
11119static int wpa_driver_nl80211_status(void *priv, char *buf, size_t buflen)
11120{
11121 struct i802_bss *bss = priv;
11122 struct wpa_driver_nl80211_data *drv = bss->drv;
11123 int res;
11124 char *pos, *end;
11125
11126 pos = buf;
11127 end = buf + buflen;
11128
11129 res = os_snprintf(pos, end - pos,
11130 "ifindex=%d\n"
11131 "ifname=%s\n"
11132 "brname=%s\n"
11133 "addr=" MACSTR "\n"
11134 "freq=%d\n"
11135 "%s%s%s%s%s",
11136 bss->ifindex,
11137 bss->ifname,
11138 bss->brname,
11139 MAC2STR(bss->addr),
11140 bss->freq,
11141 bss->beacon_set ? "beacon_set=1\n" : "",
11142 bss->added_if_into_bridge ?
11143 "added_if_into_bridge=1\n" : "",
11144 bss->added_bridge ? "added_bridge=1\n" : "",
11145 bss->in_deinit ? "in_deinit=1\n" : "",
11146 bss->if_dynamic ? "if_dynamic=1\n" : "");
11147 if (res < 0 || res >= end - pos)
11148 return pos - buf;
11149 pos += res;
11150
11151 if (bss->wdev_id_set) {
11152 res = os_snprintf(pos, end - pos, "wdev_id=%llu\n",
11153 (unsigned long long) bss->wdev_id);
11154 if (res < 0 || res >= end - pos)
11155 return pos - buf;
11156 pos += res;
11157 }
11158
11159 res = os_snprintf(pos, end - pos,
11160 "phyname=%s\n"
11161 "drv_ifindex=%d\n"
11162 "operstate=%d\n"
11163 "scan_state=%s\n"
11164 "auth_bssid=" MACSTR "\n"
11165 "auth_attempt_bssid=" MACSTR "\n"
11166 "bssid=" MACSTR "\n"
11167 "prev_bssid=" MACSTR "\n"
11168 "associated=%d\n"
11169 "assoc_freq=%u\n"
11170 "monitor_sock=%d\n"
11171 "monitor_ifidx=%d\n"
11172 "monitor_refcount=%d\n"
11173 "last_mgmt_freq=%u\n"
11174 "eapol_tx_sock=%d\n"
11175 "%s%s%s%s%s%s%s%s%s%s%s%s%s",
11176 drv->phyname,
11177 drv->ifindex,
11178 drv->operstate,
11179 scan_state_str(drv->scan_state),
11180 MAC2STR(drv->auth_bssid),
11181 MAC2STR(drv->auth_attempt_bssid),
11182 MAC2STR(drv->bssid),
11183 MAC2STR(drv->prev_bssid),
11184 drv->associated,
11185 drv->assoc_freq,
11186 drv->monitor_sock,
11187 drv->monitor_ifidx,
11188 drv->monitor_refcount,
11189 drv->last_mgmt_freq,
11190 drv->eapol_tx_sock,
11191 drv->ignore_if_down_event ?
11192 "ignore_if_down_event=1\n" : "",
11193 drv->scan_complete_events ?
11194 "scan_complete_events=1\n" : "",
11195 drv->disabled_11b_rates ?
11196 "disabled_11b_rates=1\n" : "",
11197 drv->pending_remain_on_chan ?
11198 "pending_remain_on_chan=1\n" : "",
11199 drv->in_interface_list ? "in_interface_list=1\n" : "",
11200 drv->device_ap_sme ? "device_ap_sme=1\n" : "",
11201 drv->poll_command_supported ?
11202 "poll_command_supported=1\n" : "",
11203 drv->data_tx_status ? "data_tx_status=1\n" : "",
11204 drv->scan_for_auth ? "scan_for_auth=1\n" : "",
11205 drv->retry_auth ? "retry_auth=1\n" : "",
11206 drv->use_monitor ? "use_monitor=1\n" : "",
11207 drv->ignore_next_local_disconnect ?
11208 "ignore_next_local_disconnect=1\n" : "",
11209 drv->allow_p2p_device ? "allow_p2p_device=1\n" : "");
11210 if (res < 0 || res >= end - pos)
11211 return pos - buf;
11212 pos += res;
11213
11214 if (drv->has_capability) {
11215 res = os_snprintf(pos, end - pos,
11216 "capa.key_mgmt=0x%x\n"
11217 "capa.enc=0x%x\n"
11218 "capa.auth=0x%x\n"
11219 "capa.flags=0x%x\n"
11220 "capa.max_scan_ssids=%d\n"
11221 "capa.max_sched_scan_ssids=%d\n"
11222 "capa.sched_scan_supported=%d\n"
11223 "capa.max_match_sets=%d\n"
11224 "capa.max_remain_on_chan=%u\n"
11225 "capa.max_stations=%u\n"
11226 "capa.probe_resp_offloads=0x%x\n"
11227 "capa.max_acl_mac_addrs=%u\n"
11228 "capa.num_multichan_concurrent=%u\n",
11229 drv->capa.key_mgmt,
11230 drv->capa.enc,
11231 drv->capa.auth,
11232 drv->capa.flags,
11233 drv->capa.max_scan_ssids,
11234 drv->capa.max_sched_scan_ssids,
11235 drv->capa.sched_scan_supported,
11236 drv->capa.max_match_sets,
11237 drv->capa.max_remain_on_chan,
11238 drv->capa.max_stations,
11239 drv->capa.probe_resp_offloads,
11240 drv->capa.max_acl_mac_addrs,
11241 drv->capa.num_multichan_concurrent);
11242 if (res < 0 || res >= end - pos)
11243 return pos - buf;
11244 pos += res;
11245 }
11246
11247 return pos - buf;
11248}
11249
11250
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011251const struct wpa_driver_ops wpa_driver_nl80211_ops = {
11252 .name = "nl80211",
11253 .desc = "Linux nl80211/cfg80211",
11254 .get_bssid = wpa_driver_nl80211_get_bssid,
11255 .get_ssid = wpa_driver_nl80211_get_ssid,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011256 .set_key = driver_nl80211_set_key,
11257 .scan2 = driver_nl80211_scan2,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011258 .sched_scan = wpa_driver_nl80211_sched_scan,
11259 .stop_sched_scan = wpa_driver_nl80211_stop_sched_scan,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011260 .get_scan_results2 = wpa_driver_nl80211_get_scan_results,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011261 .deauthenticate = driver_nl80211_deauthenticate,
11262 .authenticate = driver_nl80211_authenticate,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011263 .associate = wpa_driver_nl80211_associate,
11264 .global_init = nl80211_global_init,
11265 .global_deinit = nl80211_global_deinit,
11266 .init2 = wpa_driver_nl80211_init,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011267 .deinit = driver_nl80211_deinit,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011268 .get_capa = wpa_driver_nl80211_get_capa,
11269 .set_operstate = wpa_driver_nl80211_set_operstate,
11270 .set_supp_port = wpa_driver_nl80211_set_supp_port,
11271 .set_country = wpa_driver_nl80211_set_country,
Dmitry Shmidtcce06662013-11-04 18:44:24 -080011272 .get_country = wpa_driver_nl80211_get_country,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011273 .set_ap = wpa_driver_nl80211_set_ap,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -070011274 .set_acl = wpa_driver_nl80211_set_acl,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011275 .if_add = wpa_driver_nl80211_if_add,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011276 .if_remove = driver_nl80211_if_remove,
11277 .send_mlme = driver_nl80211_send_mlme,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011278 .get_hw_feature_data = wpa_driver_nl80211_get_hw_feature_data,
11279 .sta_add = wpa_driver_nl80211_sta_add,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011280 .sta_remove = driver_nl80211_sta_remove,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011281 .hapd_send_eapol = wpa_driver_nl80211_hapd_send_eapol,
11282 .sta_set_flags = wpa_driver_nl80211_sta_set_flags,
11283#ifdef HOSTAPD
11284 .hapd_init = i802_init,
11285 .hapd_deinit = i802_deinit,
Jouni Malinen75ecf522011-06-27 15:19:46 -070011286 .set_wds_sta = i802_set_wds_sta,
11287#endif /* HOSTAPD */
11288#if defined(HOSTAPD) || defined(CONFIG_AP)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011289 .get_seqnum = i802_get_seqnum,
11290 .flush = i802_flush,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011291 .get_inact_sec = i802_get_inact_sec,
11292 .sta_clear_stats = i802_sta_clear_stats,
11293 .set_rts = i802_set_rts,
11294 .set_frag = i802_set_frag,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011295 .set_tx_queue_params = i802_set_tx_queue_params,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011296 .set_sta_vlan = driver_nl80211_set_sta_vlan,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011297 .sta_deauth = i802_sta_deauth,
11298 .sta_disassoc = i802_sta_disassoc,
11299#endif /* HOSTAPD || CONFIG_AP */
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011300 .read_sta_data = driver_nl80211_read_sta_data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011301 .set_freq = i802_set_freq,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011302 .send_action = driver_nl80211_send_action,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011303 .send_action_cancel_wait = wpa_driver_nl80211_send_action_cancel_wait,
11304 .remain_on_channel = wpa_driver_nl80211_remain_on_channel,
11305 .cancel_remain_on_channel =
11306 wpa_driver_nl80211_cancel_remain_on_channel,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011307 .probe_req_report = driver_nl80211_probe_req_report,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011308 .deinit_ap = wpa_driver_nl80211_deinit_ap,
Dmitry Shmidt04949592012-07-19 12:16:46 -070011309 .deinit_p2p_cli = wpa_driver_nl80211_deinit_p2p_cli,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011310 .resume = wpa_driver_nl80211_resume,
11311 .send_ft_action = nl80211_send_ft_action,
11312 .signal_monitor = nl80211_signal_monitor,
11313 .signal_poll = nl80211_signal_poll,
11314 .send_frame = nl80211_send_frame,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011315 .shared_freq = wpa_driver_nl80211_shared_freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011316 .set_param = nl80211_set_param,
11317 .get_radio_name = nl80211_get_radio_name,
Jouni Malinen75ecf522011-06-27 15:19:46 -070011318 .add_pmkid = nl80211_add_pmkid,
11319 .remove_pmkid = nl80211_remove_pmkid,
11320 .flush_pmkid = nl80211_flush_pmkid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011321 .set_rekey_info = nl80211_set_rekey_info,
11322 .poll_client = nl80211_poll_client,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011323 .set_p2p_powersave = nl80211_set_p2p_powersave,
Dmitry Shmidtea69e842013-05-13 14:52:28 -070011324 .start_dfs_cac = nl80211_start_radar_detection,
11325 .stop_ap = wpa_driver_nl80211_stop_ap,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011326#ifdef CONFIG_TDLS
11327 .send_tdls_mgmt = nl80211_send_tdls_mgmt,
11328 .tdls_oper = nl80211_tdls_oper,
11329#endif /* CONFIG_TDLS */
Dmitry Shmidt700a1372013-03-15 14:14:44 -070011330 .update_ft_ies = wpa_driver_nl80211_update_ft_ies,
Dmitry Shmidt34af3062013-07-11 10:46:32 -070011331 .get_mac_addr = wpa_driver_nl80211_get_macaddr,
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070011332 .get_survey = wpa_driver_nl80211_get_survey,
Dmitry Shmidt56052862013-10-04 10:23:25 -070011333 .status = wpa_driver_nl80211_status,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011334#ifdef ANDROID_P2P
Dmitry Shmidt6e933c12011-09-27 12:29:26 -070011335 .set_noa = wpa_driver_set_p2p_noa,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011336 .get_noa = wpa_driver_get_p2p_noa,
Dmitry Shmidt6e933c12011-09-27 12:29:26 -070011337 .set_ap_wps_ie = wpa_driver_set_ap_wps_p2p_ie,
11338#endif
Dmitry Shmidt738a26e2011-07-07 14:22:14 -070011339#ifdef ANDROID
11340 .driver_cmd = wpa_driver_nl80211_driver_cmd,
11341#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011342};