blob: c5091705c409e75fa219687a0fe6046be879a5ae [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * Driver interaction with Linux nl80211/cfg80211
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003 * Copyright (c) 2002-2014, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 * Copyright (c) 2003-2004, Instant802 Networks, Inc.
5 * Copyright (c) 2005-2006, Devicescape Software, Inc.
6 * Copyright (c) 2007, Johannes Berg <johannes@sipsolutions.net>
7 * Copyright (c) 2009-2010, Atheros Communications
8 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08009 * This software may be distributed under the terms of the BSD license.
10 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011 */
12
13#include "includes.h"
14#include <sys/ioctl.h>
15#include <sys/types.h>
16#include <sys/stat.h>
17#include <fcntl.h>
18#include <net/if.h>
19#include <netlink/genl/genl.h>
20#include <netlink/genl/family.h>
21#include <netlink/genl/ctrl.h>
22#include <linux/rtnetlink.h>
23#include <netpacket/packet.h>
24#include <linux/filter.h>
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080025#include <linux/errqueue.h>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070026#include "nl80211_copy.h"
27
28#include "common.h"
29#include "eloop.h"
30#include "utils/list.h"
Dmitry Shmidtcf32e602014-01-28 10:57:39 -080031#include "common/qca-vendor.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070032#include "common/ieee802_11_defs.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080033#include "common/ieee802_11_common.h"
34#include "l2_packet/l2_packet.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070035#include "netlink.h"
36#include "linux_ioctl.h"
37#include "radiotap.h"
38#include "radiotap_iter.h"
39#include "rfkill.h"
40#include "driver.h"
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080041
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080042#ifndef SO_WIFI_STATUS
43# if defined(__sparc__)
44# define SO_WIFI_STATUS 0x0025
45# elif defined(__parisc__)
46# define SO_WIFI_STATUS 0x4022
47# else
48# define SO_WIFI_STATUS 41
49# endif
50
51# define SCM_WIFI_STATUS SO_WIFI_STATUS
52#endif
53
54#ifndef SO_EE_ORIGIN_TXSTATUS
55#define SO_EE_ORIGIN_TXSTATUS 4
56#endif
57
58#ifndef PACKET_TX_TIMESTAMP
59#define PACKET_TX_TIMESTAMP 16
60#endif
61
62#ifdef ANDROID
63#include "android_drv.h"
64#endif /* ANDROID */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070065#ifdef CONFIG_LIBNL20
66/* libnl 2.0 compatibility code */
67#define nl_handle nl_sock
68#define nl80211_handle_alloc nl_socket_alloc_cb
69#define nl80211_handle_destroy nl_socket_free
70#else
71/*
72 * libnl 1.1 has a bug, it tries to allocate socket numbers densely
73 * but when you free a socket again it will mess up its bitmap and
74 * and use the wrong number the next time it needs a socket ID.
75 * Therefore, we wrap the handle alloc/destroy and add our own pid
76 * accounting.
77 */
78static uint32_t port_bitmap[32] = { 0 };
79
80static struct nl_handle *nl80211_handle_alloc(void *cb)
81{
82 struct nl_handle *handle;
83 uint32_t pid = getpid() & 0x3FFFFF;
84 int i;
85
86 handle = nl_handle_alloc_cb(cb);
87
88 for (i = 0; i < 1024; i++) {
89 if (port_bitmap[i / 32] & (1 << (i % 32)))
90 continue;
91 port_bitmap[i / 32] |= 1 << (i % 32);
92 pid += i << 22;
93 break;
94 }
95
96 nl_socket_set_local_port(handle, pid);
97
98 return handle;
99}
100
101static void nl80211_handle_destroy(struct nl_handle *handle)
102{
103 uint32_t port = nl_socket_get_local_port(handle);
104
105 port >>= 22;
106 port_bitmap[port / 32] &= ~(1 << (port % 32));
107
108 nl_handle_destroy(handle);
109}
110#endif /* CONFIG_LIBNL20 */
111
112
Dmitry Shmidt54605472013-11-08 11:10:19 -0800113#ifdef ANDROID
114/* system/core/libnl_2 does not include nl_socket_set_nonblocking() */
115static int android_nl_socket_set_nonblocking(struct nl_handle *handle)
116{
117 return fcntl(nl_socket_get_fd(handle), F_SETFL, O_NONBLOCK);
118}
119#undef nl_socket_set_nonblocking
120#define nl_socket_set_nonblocking(h) android_nl_socket_set_nonblocking(h)
121#endif /* ANDROID */
122
123
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800124static struct nl_handle * nl_create_handle(struct nl_cb *cb, const char *dbg)
125{
126 struct nl_handle *handle;
127
128 handle = nl80211_handle_alloc(cb);
129 if (handle == NULL) {
130 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
131 "callbacks (%s)", dbg);
132 return NULL;
133 }
134
135 if (genl_connect(handle)) {
136 wpa_printf(MSG_ERROR, "nl80211: Failed to connect to generic "
137 "netlink (%s)", dbg);
138 nl80211_handle_destroy(handle);
139 return NULL;
140 }
141
142 return handle;
143}
144
145
146static void nl_destroy_handles(struct nl_handle **handle)
147{
148 if (*handle == NULL)
149 return;
150 nl80211_handle_destroy(*handle);
151 *handle = NULL;
152}
153
154
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700155#if __WORDSIZE == 64
156#define ELOOP_SOCKET_INVALID (intptr_t) 0x8888888888888889ULL
157#else
158#define ELOOP_SOCKET_INVALID (intptr_t) 0x88888889ULL
159#endif
160
161static void nl80211_register_eloop_read(struct nl_handle **handle,
162 eloop_sock_handler handler,
163 void *eloop_data)
164{
165 nl_socket_set_nonblocking(*handle);
166 eloop_register_read_sock(nl_socket_get_fd(*handle), handler,
167 eloop_data, *handle);
168 *handle = (void *) (((intptr_t) *handle) ^ ELOOP_SOCKET_INVALID);
169}
170
171
172static void nl80211_destroy_eloop_handle(struct nl_handle **handle)
173{
174 *handle = (void *) (((intptr_t) *handle) ^ ELOOP_SOCKET_INVALID);
175 eloop_unregister_read_sock(nl_socket_get_fd(*handle));
176 nl_destroy_handles(handle);
177}
178
179
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700180#ifndef IFF_LOWER_UP
181#define IFF_LOWER_UP 0x10000 /* driver signals L1 up */
182#endif
183#ifndef IFF_DORMANT
184#define IFF_DORMANT 0x20000 /* driver signals dormant */
185#endif
186
187#ifndef IF_OPER_DORMANT
188#define IF_OPER_DORMANT 5
189#endif
190#ifndef IF_OPER_UP
191#define IF_OPER_UP 6
192#endif
193
194struct nl80211_global {
195 struct dl_list interfaces;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800196 int if_add_ifindex;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700197 u64 if_add_wdevid;
198 int if_add_wdevid_set;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800199 struct netlink_data *netlink;
200 struct nl_cb *nl_cb;
201 struct nl_handle *nl;
202 int nl80211_id;
203 int ioctl_sock; /* socket for ioctl() use */
204
205 struct nl_handle *nl_event;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700206};
207
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800208struct nl80211_wiphy_data {
209 struct dl_list list;
210 struct dl_list bsss;
211 struct dl_list drvs;
212
213 struct nl_handle *nl_beacons;
214 struct nl_cb *nl_cb;
215
216 int wiphy_idx;
217};
218
219static void nl80211_global_deinit(void *priv);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800220
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700221struct i802_bss {
222 struct wpa_driver_nl80211_data *drv;
223 struct i802_bss *next;
224 int ifindex;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700225 u64 wdev_id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700226 char ifname[IFNAMSIZ + 1];
227 char brname[IFNAMSIZ];
228 unsigned int beacon_set:1;
229 unsigned int added_if_into_bridge:1;
230 unsigned int added_bridge:1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700231 unsigned int in_deinit:1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700232 unsigned int wdev_id_set:1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800233 unsigned int added_if:1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800234
235 u8 addr[ETH_ALEN];
236
237 int freq;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700238 int if_dynamic;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800239
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800240 void *ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800241 struct nl_handle *nl_preq, *nl_mgmt;
242 struct nl_cb *nl_cb;
243
244 struct nl80211_wiphy_data *wiphy_data;
245 struct dl_list wiphy_list;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700246};
247
248struct wpa_driver_nl80211_data {
249 struct nl80211_global *global;
250 struct dl_list list;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800251 struct dl_list wiphy_list;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700252 char phyname[32];
253 void *ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700254 int ifindex;
255 int if_removed;
256 int if_disabled;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800257 int ignore_if_down_event;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700258 struct rfkill_data *rfkill;
259 struct wpa_driver_capa capa;
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700260 u8 *extended_capa, *extended_capa_mask;
261 unsigned int extended_capa_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700262 int has_capability;
263
264 int operstate;
265
266 int scan_complete_events;
Dmitry Shmidt56052862013-10-04 10:23:25 -0700267 enum scan_states {
268 NO_SCAN, SCAN_REQUESTED, SCAN_STARTED, SCAN_COMPLETED,
269 SCAN_ABORTED, SCHED_SCAN_STARTED, SCHED_SCAN_STOPPED,
270 SCHED_SCAN_RESULTS
271 } scan_state;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700272
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700273 struct nl_cb *nl_cb;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700274
275 u8 auth_bssid[ETH_ALEN];
Dmitry Shmidt8bae4132013-06-06 11:25:10 -0700276 u8 auth_attempt_bssid[ETH_ALEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700277 u8 bssid[ETH_ALEN];
Dmitry Shmidt8bae4132013-06-06 11:25:10 -0700278 u8 prev_bssid[ETH_ALEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700279 int associated;
280 u8 ssid[32];
281 size_t ssid_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800282 enum nl80211_iftype nlmode;
283 enum nl80211_iftype ap_scan_as_station;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700284 unsigned int assoc_freq;
285
286 int monitor_sock;
287 int monitor_ifidx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800288 int monitor_refcount;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700289
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800290 unsigned int disabled_11b_rates:1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700291 unsigned int pending_remain_on_chan:1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800292 unsigned int in_interface_list:1;
293 unsigned int device_ap_sme:1;
294 unsigned int poll_command_supported:1;
295 unsigned int data_tx_status:1;
296 unsigned int scan_for_auth:1;
297 unsigned int retry_auth:1;
298 unsigned int use_monitor:1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800299 unsigned int ignore_next_local_disconnect:1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700300 unsigned int allow_p2p_device:1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800301 unsigned int hostapd:1;
302 unsigned int start_mode_ap:1;
303 unsigned int start_iface_up:1;
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800304 unsigned int test_use_roc_tx:1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700305
306 u64 remain_on_chan_cookie;
307 u64 send_action_cookie;
308
309 unsigned int last_mgmt_freq;
310
311 struct wpa_driver_scan_filter *filter_ssids;
312 size_t num_filter_ssids;
313
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800314 struct i802_bss *first_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700315
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800316 int eapol_tx_sock;
317
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700318 int eapol_sock; /* socket for EAPOL frames */
319
320 int default_if_indices[16];
321 int *if_indices;
322 int num_if_indices;
323
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800324 /* 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
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800346wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv,
347 const u8 *set_addr, int first);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700348static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
349 const u8 *addr, int cmd, u16 reason_code,
350 int local_state_change);
351static void nl80211_remove_monitor_interface(
352 struct wpa_driver_nl80211_data *drv);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800353static int nl80211_send_frame_cmd(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700354 unsigned int freq, unsigned int wait,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800355 const u8 *buf, size_t buf_len, u64 *cookie,
356 int no_cck, int no_ack, int offchanok);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700357static int nl80211_register_frame(struct i802_bss *bss,
358 struct nl_handle *hl_handle,
359 u16 type, const u8 *match, size_t match_len);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800360static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss,
361 int report);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800362#ifdef ANDROID
363static int android_pno_start(struct i802_bss *bss,
364 struct wpa_driver_scan_params *params);
365static int android_pno_stop(struct i802_bss *bss);
Dmitry Shmidt292b0c32013-11-22 12:54:42 -0800366extern int wpa_driver_nl80211_driver_cmd(void *priv, char *cmd, char *buf,
367 size_t buf_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800368#endif /* ANDROID */
369#ifdef ANDROID_P2P
Dmitry Shmidt6e933c12011-09-27 12:29:26 -0700370int wpa_driver_set_p2p_noa(void *priv, u8 count, int start, int duration);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800371int wpa_driver_get_p2p_noa(void *priv, u8 *buf, size_t len);
Dmitry Shmidt6e933c12011-09-27 12:29:26 -0700372int wpa_driver_set_p2p_ps(void *priv, int legacy_ps, int opp_ps, int ctwindow);
373int wpa_driver_set_ap_wps_p2p_ie(void *priv, const struct wpabuf *beacon,
Dmitry Shmidt292b0c32013-11-22 12:54:42 -0800374 const struct wpabuf *proberesp,
375 const struct wpabuf *assocresp);
376#endif /* ANDROID_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700377
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700378static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
379static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
380static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800381static int wpa_driver_nl80211_if_remove(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700382 enum wpa_driver_if_type type,
383 const char *ifname);
Dmitry Shmidt738a26e2011-07-07 14:22:14 -0700384
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800385static int wpa_driver_nl80211_set_freq(struct i802_bss *bss,
386 struct hostapd_freq_params *freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700387static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
388 int ifindex, int disabled);
389
390static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800391static int wpa_driver_nl80211_authenticate_retry(
392 struct wpa_driver_nl80211_data *drv);
393
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700394static int i802_set_iface_flags(struct i802_bss *bss, int up);
395
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800396
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700397static const char * nl80211_command_to_string(enum nl80211_commands cmd)
398{
399#define C2S(x) case x: return #x;
400 switch (cmd) {
401 C2S(NL80211_CMD_UNSPEC)
402 C2S(NL80211_CMD_GET_WIPHY)
403 C2S(NL80211_CMD_SET_WIPHY)
404 C2S(NL80211_CMD_NEW_WIPHY)
405 C2S(NL80211_CMD_DEL_WIPHY)
406 C2S(NL80211_CMD_GET_INTERFACE)
407 C2S(NL80211_CMD_SET_INTERFACE)
408 C2S(NL80211_CMD_NEW_INTERFACE)
409 C2S(NL80211_CMD_DEL_INTERFACE)
410 C2S(NL80211_CMD_GET_KEY)
411 C2S(NL80211_CMD_SET_KEY)
412 C2S(NL80211_CMD_NEW_KEY)
413 C2S(NL80211_CMD_DEL_KEY)
414 C2S(NL80211_CMD_GET_BEACON)
415 C2S(NL80211_CMD_SET_BEACON)
416 C2S(NL80211_CMD_START_AP)
417 C2S(NL80211_CMD_STOP_AP)
418 C2S(NL80211_CMD_GET_STATION)
419 C2S(NL80211_CMD_SET_STATION)
420 C2S(NL80211_CMD_NEW_STATION)
421 C2S(NL80211_CMD_DEL_STATION)
422 C2S(NL80211_CMD_GET_MPATH)
423 C2S(NL80211_CMD_SET_MPATH)
424 C2S(NL80211_CMD_NEW_MPATH)
425 C2S(NL80211_CMD_DEL_MPATH)
426 C2S(NL80211_CMD_SET_BSS)
427 C2S(NL80211_CMD_SET_REG)
428 C2S(NL80211_CMD_REQ_SET_REG)
429 C2S(NL80211_CMD_GET_MESH_CONFIG)
430 C2S(NL80211_CMD_SET_MESH_CONFIG)
431 C2S(NL80211_CMD_SET_MGMT_EXTRA_IE)
432 C2S(NL80211_CMD_GET_REG)
433 C2S(NL80211_CMD_GET_SCAN)
434 C2S(NL80211_CMD_TRIGGER_SCAN)
435 C2S(NL80211_CMD_NEW_SCAN_RESULTS)
436 C2S(NL80211_CMD_SCAN_ABORTED)
437 C2S(NL80211_CMD_REG_CHANGE)
438 C2S(NL80211_CMD_AUTHENTICATE)
439 C2S(NL80211_CMD_ASSOCIATE)
440 C2S(NL80211_CMD_DEAUTHENTICATE)
441 C2S(NL80211_CMD_DISASSOCIATE)
442 C2S(NL80211_CMD_MICHAEL_MIC_FAILURE)
443 C2S(NL80211_CMD_REG_BEACON_HINT)
444 C2S(NL80211_CMD_JOIN_IBSS)
445 C2S(NL80211_CMD_LEAVE_IBSS)
446 C2S(NL80211_CMD_TESTMODE)
447 C2S(NL80211_CMD_CONNECT)
448 C2S(NL80211_CMD_ROAM)
449 C2S(NL80211_CMD_DISCONNECT)
450 C2S(NL80211_CMD_SET_WIPHY_NETNS)
451 C2S(NL80211_CMD_GET_SURVEY)
452 C2S(NL80211_CMD_NEW_SURVEY_RESULTS)
453 C2S(NL80211_CMD_SET_PMKSA)
454 C2S(NL80211_CMD_DEL_PMKSA)
455 C2S(NL80211_CMD_FLUSH_PMKSA)
456 C2S(NL80211_CMD_REMAIN_ON_CHANNEL)
457 C2S(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL)
458 C2S(NL80211_CMD_SET_TX_BITRATE_MASK)
459 C2S(NL80211_CMD_REGISTER_FRAME)
460 C2S(NL80211_CMD_FRAME)
461 C2S(NL80211_CMD_FRAME_TX_STATUS)
462 C2S(NL80211_CMD_SET_POWER_SAVE)
463 C2S(NL80211_CMD_GET_POWER_SAVE)
464 C2S(NL80211_CMD_SET_CQM)
465 C2S(NL80211_CMD_NOTIFY_CQM)
466 C2S(NL80211_CMD_SET_CHANNEL)
467 C2S(NL80211_CMD_SET_WDS_PEER)
468 C2S(NL80211_CMD_FRAME_WAIT_CANCEL)
469 C2S(NL80211_CMD_JOIN_MESH)
470 C2S(NL80211_CMD_LEAVE_MESH)
471 C2S(NL80211_CMD_UNPROT_DEAUTHENTICATE)
472 C2S(NL80211_CMD_UNPROT_DISASSOCIATE)
473 C2S(NL80211_CMD_NEW_PEER_CANDIDATE)
474 C2S(NL80211_CMD_GET_WOWLAN)
475 C2S(NL80211_CMD_SET_WOWLAN)
476 C2S(NL80211_CMD_START_SCHED_SCAN)
477 C2S(NL80211_CMD_STOP_SCHED_SCAN)
478 C2S(NL80211_CMD_SCHED_SCAN_RESULTS)
479 C2S(NL80211_CMD_SCHED_SCAN_STOPPED)
480 C2S(NL80211_CMD_SET_REKEY_OFFLOAD)
481 C2S(NL80211_CMD_PMKSA_CANDIDATE)
482 C2S(NL80211_CMD_TDLS_OPER)
483 C2S(NL80211_CMD_TDLS_MGMT)
484 C2S(NL80211_CMD_UNEXPECTED_FRAME)
485 C2S(NL80211_CMD_PROBE_CLIENT)
486 C2S(NL80211_CMD_REGISTER_BEACONS)
487 C2S(NL80211_CMD_UNEXPECTED_4ADDR_FRAME)
488 C2S(NL80211_CMD_SET_NOACK_MAP)
489 C2S(NL80211_CMD_CH_SWITCH_NOTIFY)
490 C2S(NL80211_CMD_START_P2P_DEVICE)
491 C2S(NL80211_CMD_STOP_P2P_DEVICE)
492 C2S(NL80211_CMD_CONN_FAILED)
493 C2S(NL80211_CMD_SET_MCAST_RATE)
494 C2S(NL80211_CMD_SET_MAC_ACL)
495 C2S(NL80211_CMD_RADAR_DETECT)
496 C2S(NL80211_CMD_GET_PROTOCOL_FEATURES)
497 C2S(NL80211_CMD_UPDATE_FT_IES)
498 C2S(NL80211_CMD_FT_EVENT)
499 C2S(NL80211_CMD_CRIT_PROTOCOL_START)
500 C2S(NL80211_CMD_CRIT_PROTOCOL_STOP)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800501 C2S(NL80211_CMD_GET_COALESCE)
502 C2S(NL80211_CMD_SET_COALESCE)
503 C2S(NL80211_CMD_CHANNEL_SWITCH)
504 C2S(NL80211_CMD_VENDOR)
505 C2S(NL80211_CMD_SET_QOS_MAP)
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700506 default:
507 return "NL80211_CMD_UNKNOWN";
508 }
509#undef C2S
510}
511
512
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800513/* Converts nl80211_chan_width to a common format */
514static enum chan_width convert2width(int width)
515{
516 switch (width) {
517 case NL80211_CHAN_WIDTH_20_NOHT:
518 return CHAN_WIDTH_20_NOHT;
519 case NL80211_CHAN_WIDTH_20:
520 return CHAN_WIDTH_20;
521 case NL80211_CHAN_WIDTH_40:
522 return CHAN_WIDTH_40;
523 case NL80211_CHAN_WIDTH_80:
524 return CHAN_WIDTH_80;
525 case NL80211_CHAN_WIDTH_80P80:
526 return CHAN_WIDTH_80P80;
527 case NL80211_CHAN_WIDTH_160:
528 return CHAN_WIDTH_160;
529 }
530 return CHAN_WIDTH_UNKNOWN;
531}
532
533
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800534static int is_ap_interface(enum nl80211_iftype nlmode)
535{
536 return (nlmode == NL80211_IFTYPE_AP ||
537 nlmode == NL80211_IFTYPE_P2P_GO);
538}
539
540
541static int is_sta_interface(enum nl80211_iftype nlmode)
542{
543 return (nlmode == NL80211_IFTYPE_STATION ||
544 nlmode == NL80211_IFTYPE_P2P_CLIENT);
545}
546
547
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700548static int is_p2p_net_interface(enum nl80211_iftype nlmode)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800549{
550 return (nlmode == NL80211_IFTYPE_P2P_CLIENT ||
551 nlmode == NL80211_IFTYPE_P2P_GO);
552}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700553
554
Dmitry Shmidt8bae4132013-06-06 11:25:10 -0700555static void nl80211_mark_disconnected(struct wpa_driver_nl80211_data *drv)
556{
557 if (drv->associated)
558 os_memcpy(drv->prev_bssid, drv->bssid, ETH_ALEN);
559 drv->associated = 0;
560 os_memset(drv->bssid, 0, ETH_ALEN);
561}
562
563
Jouni Malinen87fd2792011-05-16 18:35:42 +0300564struct nl80211_bss_info_arg {
565 struct wpa_driver_nl80211_data *drv;
566 struct wpa_scan_results *res;
567 unsigned int assoc_freq;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800568 u8 assoc_bssid[ETH_ALEN];
Jouni Malinen87fd2792011-05-16 18:35:42 +0300569};
570
571static int bss_info_handler(struct nl_msg *msg, void *arg);
572
573
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700574/* nl80211 code */
575static int ack_handler(struct nl_msg *msg, void *arg)
576{
577 int *err = arg;
578 *err = 0;
579 return NL_STOP;
580}
581
582static int finish_handler(struct nl_msg *msg, void *arg)
583{
584 int *ret = arg;
585 *ret = 0;
586 return NL_SKIP;
587}
588
589static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
590 void *arg)
591{
592 int *ret = arg;
593 *ret = err->error;
594 return NL_SKIP;
595}
596
597
598static int no_seq_check(struct nl_msg *msg, void *arg)
599{
600 return NL_OK;
601}
602
603
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800604static int send_and_recv(struct nl80211_global *global,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700605 struct nl_handle *nl_handle, struct nl_msg *msg,
606 int (*valid_handler)(struct nl_msg *, void *),
607 void *valid_data)
608{
609 struct nl_cb *cb;
610 int err = -ENOMEM;
611
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800612 cb = nl_cb_clone(global->nl_cb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700613 if (!cb)
614 goto out;
615
616 err = nl_send_auto_complete(nl_handle, msg);
617 if (err < 0)
618 goto out;
619
620 err = 1;
621
622 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
623 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
624 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
625
626 if (valid_handler)
627 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM,
628 valid_handler, valid_data);
629
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700630 while (err > 0) {
631 int res = nl_recvmsgs(nl_handle, cb);
632 if (res) {
633 wpa_printf(MSG_INFO,
634 "nl80211: %s->nl_recvmsgs failed: %d",
635 __func__, res);
636 }
637 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700638 out:
639 nl_cb_put(cb);
640 nlmsg_free(msg);
641 return err;
642}
643
644
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800645static int send_and_recv_msgs_global(struct nl80211_global *global,
646 struct nl_msg *msg,
647 int (*valid_handler)(struct nl_msg *, void *),
648 void *valid_data)
649{
650 return send_and_recv(global, global->nl, msg, valid_handler,
651 valid_data);
652}
653
Dmitry Shmidt04949592012-07-19 12:16:46 -0700654
Dmitry Shmidt641185e2013-11-06 15:17:13 -0800655static int send_and_recv_msgs(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700656 struct nl_msg *msg,
657 int (*valid_handler)(struct nl_msg *, void *),
658 void *valid_data)
659{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800660 return send_and_recv(drv->global, drv->global->nl, msg,
661 valid_handler, valid_data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700662}
663
664
665struct family_data {
666 const char *group;
667 int id;
668};
669
670
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700671static int nl80211_set_iface_id(struct nl_msg *msg, struct i802_bss *bss)
672{
673 if (bss->wdev_id_set)
674 NLA_PUT_U64(msg, NL80211_ATTR_WDEV, bss->wdev_id);
675 else
676 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
677 return 0;
678
679nla_put_failure:
680 return -1;
681}
682
683
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700684static int family_handler(struct nl_msg *msg, void *arg)
685{
686 struct family_data *res = arg;
687 struct nlattr *tb[CTRL_ATTR_MAX + 1];
688 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
689 struct nlattr *mcgrp;
690 int i;
691
692 nla_parse(tb, CTRL_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
693 genlmsg_attrlen(gnlh, 0), NULL);
694 if (!tb[CTRL_ATTR_MCAST_GROUPS])
695 return NL_SKIP;
696
697 nla_for_each_nested(mcgrp, tb[CTRL_ATTR_MCAST_GROUPS], i) {
698 struct nlattr *tb2[CTRL_ATTR_MCAST_GRP_MAX + 1];
699 nla_parse(tb2, CTRL_ATTR_MCAST_GRP_MAX, nla_data(mcgrp),
700 nla_len(mcgrp), NULL);
701 if (!tb2[CTRL_ATTR_MCAST_GRP_NAME] ||
702 !tb2[CTRL_ATTR_MCAST_GRP_ID] ||
703 os_strncmp(nla_data(tb2[CTRL_ATTR_MCAST_GRP_NAME]),
704 res->group,
705 nla_len(tb2[CTRL_ATTR_MCAST_GRP_NAME])) != 0)
706 continue;
707 res->id = nla_get_u32(tb2[CTRL_ATTR_MCAST_GRP_ID]);
708 break;
709 };
710
711 return NL_SKIP;
712}
713
714
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800715static int nl_get_multicast_id(struct nl80211_global *global,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700716 const char *family, const char *group)
717{
718 struct nl_msg *msg;
719 int ret = -1;
720 struct family_data res = { group, -ENOENT };
721
722 msg = nlmsg_alloc();
723 if (!msg)
724 return -ENOMEM;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800725 genlmsg_put(msg, 0, 0, genl_ctrl_resolve(global->nl, "nlctrl"),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700726 0, 0, CTRL_CMD_GETFAMILY, 0);
727 NLA_PUT_STRING(msg, CTRL_ATTR_FAMILY_NAME, family);
728
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800729 ret = send_and_recv_msgs_global(global, msg, family_handler, &res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700730 msg = NULL;
731 if (ret == 0)
732 ret = res.id;
733
734nla_put_failure:
735 nlmsg_free(msg);
736 return ret;
737}
738
739
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800740static void * nl80211_cmd(struct wpa_driver_nl80211_data *drv,
741 struct nl_msg *msg, int flags, uint8_t cmd)
742{
743 return genlmsg_put(msg, 0, 0, drv->global->nl80211_id,
744 0, flags, cmd, 0);
745}
746
747
748struct wiphy_idx_data {
749 int wiphy_idx;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700750 enum nl80211_iftype nlmode;
751 u8 *macaddr;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800752};
753
754
755static int netdev_info_handler(struct nl_msg *msg, void *arg)
756{
757 struct nlattr *tb[NL80211_ATTR_MAX + 1];
758 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
759 struct wiphy_idx_data *info = arg;
760
761 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
762 genlmsg_attrlen(gnlh, 0), NULL);
763
764 if (tb[NL80211_ATTR_WIPHY])
765 info->wiphy_idx = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
766
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700767 if (tb[NL80211_ATTR_IFTYPE])
768 info->nlmode = nla_get_u32(tb[NL80211_ATTR_IFTYPE]);
769
770 if (tb[NL80211_ATTR_MAC] && info->macaddr)
771 os_memcpy(info->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
772 ETH_ALEN);
773
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800774 return NL_SKIP;
775}
776
777
778static int nl80211_get_wiphy_index(struct i802_bss *bss)
779{
780 struct nl_msg *msg;
781 struct wiphy_idx_data data = {
782 .wiphy_idx = -1,
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700783 .macaddr = NULL,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800784 };
785
786 msg = nlmsg_alloc();
787 if (!msg)
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700788 return NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800789
790 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_GET_INTERFACE);
791
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700792 if (nl80211_set_iface_id(msg, bss) < 0)
793 goto nla_put_failure;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800794
795 if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0)
796 return data.wiphy_idx;
797 msg = NULL;
798nla_put_failure:
799 nlmsg_free(msg);
800 return -1;
801}
802
803
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700804static enum nl80211_iftype nl80211_get_ifmode(struct i802_bss *bss)
805{
806 struct nl_msg *msg;
807 struct wiphy_idx_data data = {
808 .nlmode = NL80211_IFTYPE_UNSPECIFIED,
809 .macaddr = NULL,
810 };
811
812 msg = nlmsg_alloc();
813 if (!msg)
814 return -1;
815
816 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_GET_INTERFACE);
817
818 if (nl80211_set_iface_id(msg, bss) < 0)
819 goto nla_put_failure;
820
821 if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0)
822 return data.nlmode;
823 msg = NULL;
824nla_put_failure:
825 nlmsg_free(msg);
826 return NL80211_IFTYPE_UNSPECIFIED;
827}
828
829
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700830static int nl80211_get_macaddr(struct i802_bss *bss)
831{
832 struct nl_msg *msg;
833 struct wiphy_idx_data data = {
834 .macaddr = bss->addr,
835 };
836
837 msg = nlmsg_alloc();
838 if (!msg)
839 return NL80211_IFTYPE_UNSPECIFIED;
840
841 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_GET_INTERFACE);
842 if (nl80211_set_iface_id(msg, bss) < 0)
843 goto nla_put_failure;
844
845 return send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data);
846
847nla_put_failure:
848 nlmsg_free(msg);
849 return NL80211_IFTYPE_UNSPECIFIED;
850}
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700851
852
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800853static int nl80211_register_beacons(struct wpa_driver_nl80211_data *drv,
854 struct nl80211_wiphy_data *w)
855{
856 struct nl_msg *msg;
857 int ret = -1;
858
859 msg = nlmsg_alloc();
860 if (!msg)
861 return -1;
862
863 nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_BEACONS);
864
865 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, w->wiphy_idx);
866
867 ret = send_and_recv(drv->global, w->nl_beacons, msg, NULL, NULL);
868 msg = NULL;
869 if (ret) {
870 wpa_printf(MSG_DEBUG, "nl80211: Register beacons command "
871 "failed: ret=%d (%s)",
872 ret, strerror(-ret));
873 goto nla_put_failure;
874 }
875 ret = 0;
876nla_put_failure:
877 nlmsg_free(msg);
878 return ret;
879}
880
881
882static void nl80211_recv_beacons(int sock, void *eloop_ctx, void *handle)
883{
884 struct nl80211_wiphy_data *w = eloop_ctx;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700885 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800886
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800887 wpa_printf(MSG_EXCESSIVE, "nl80211: Beacon event message available");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800888
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700889 res = nl_recvmsgs(handle, w->nl_cb);
890 if (res) {
891 wpa_printf(MSG_INFO, "nl80211: %s->nl_recvmsgs failed: %d",
892 __func__, res);
893 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800894}
895
896
897static int process_beacon_event(struct nl_msg *msg, void *arg)
898{
899 struct nl80211_wiphy_data *w = arg;
900 struct wpa_driver_nl80211_data *drv;
901 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
902 struct nlattr *tb[NL80211_ATTR_MAX + 1];
903 union wpa_event_data event;
904
905 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
906 genlmsg_attrlen(gnlh, 0), NULL);
907
908 if (gnlh->cmd != NL80211_CMD_FRAME) {
909 wpa_printf(MSG_DEBUG, "nl80211: Unexpected beacon event? (%d)",
910 gnlh->cmd);
911 return NL_SKIP;
912 }
913
914 if (!tb[NL80211_ATTR_FRAME])
915 return NL_SKIP;
916
917 dl_list_for_each(drv, &w->drvs, struct wpa_driver_nl80211_data,
918 wiphy_list) {
919 os_memset(&event, 0, sizeof(event));
920 event.rx_mgmt.frame = nla_data(tb[NL80211_ATTR_FRAME]);
921 event.rx_mgmt.frame_len = nla_len(tb[NL80211_ATTR_FRAME]);
922 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
923 }
924
925 return NL_SKIP;
926}
927
928
929static struct nl80211_wiphy_data *
930nl80211_get_wiphy_data_ap(struct i802_bss *bss)
931{
932 static DEFINE_DL_LIST(nl80211_wiphys);
933 struct nl80211_wiphy_data *w;
934 int wiphy_idx, found = 0;
935 struct i802_bss *tmp_bss;
936
937 if (bss->wiphy_data != NULL)
938 return bss->wiphy_data;
939
940 wiphy_idx = nl80211_get_wiphy_index(bss);
941
942 dl_list_for_each(w, &nl80211_wiphys, struct nl80211_wiphy_data, list) {
943 if (w->wiphy_idx == wiphy_idx)
944 goto add;
945 }
946
947 /* alloc new one */
948 w = os_zalloc(sizeof(*w));
949 if (w == NULL)
950 return NULL;
951 w->wiphy_idx = wiphy_idx;
952 dl_list_init(&w->bsss);
953 dl_list_init(&w->drvs);
954
955 w->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
956 if (!w->nl_cb) {
957 os_free(w);
958 return NULL;
959 }
960 nl_cb_set(w->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL);
961 nl_cb_set(w->nl_cb, NL_CB_VALID, NL_CB_CUSTOM, process_beacon_event,
962 w);
963
964 w->nl_beacons = nl_create_handle(bss->drv->global->nl_cb,
965 "wiphy beacons");
966 if (w->nl_beacons == NULL) {
967 os_free(w);
968 return NULL;
969 }
970
971 if (nl80211_register_beacons(bss->drv, w)) {
972 nl_destroy_handles(&w->nl_beacons);
973 os_free(w);
974 return NULL;
975 }
976
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700977 nl80211_register_eloop_read(&w->nl_beacons, nl80211_recv_beacons, w);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800978
979 dl_list_add(&nl80211_wiphys, &w->list);
980
981add:
982 /* drv entry for this bss already there? */
983 dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
984 if (tmp_bss->drv == bss->drv) {
985 found = 1;
986 break;
987 }
988 }
989 /* if not add it */
990 if (!found)
991 dl_list_add(&w->drvs, &bss->drv->wiphy_list);
992
993 dl_list_add(&w->bsss, &bss->wiphy_list);
994 bss->wiphy_data = w;
995 return w;
996}
997
998
999static void nl80211_put_wiphy_data_ap(struct i802_bss *bss)
1000{
1001 struct nl80211_wiphy_data *w = bss->wiphy_data;
1002 struct i802_bss *tmp_bss;
1003 int found = 0;
1004
1005 if (w == NULL)
1006 return;
1007 bss->wiphy_data = NULL;
1008 dl_list_del(&bss->wiphy_list);
1009
1010 /* still any for this drv present? */
1011 dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
1012 if (tmp_bss->drv == bss->drv) {
1013 found = 1;
1014 break;
1015 }
1016 }
1017 /* if not remove it */
1018 if (!found)
1019 dl_list_del(&bss->drv->wiphy_list);
1020
1021 if (!dl_list_empty(&w->bsss))
1022 return;
1023
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001024 nl80211_destroy_eloop_handle(&w->nl_beacons);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001025
1026 nl_cb_put(w->nl_cb);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001027 dl_list_del(&w->list);
1028 os_free(w);
1029}
1030
1031
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001032static int wpa_driver_nl80211_get_bssid(void *priv, u8 *bssid)
1033{
1034 struct i802_bss *bss = priv;
1035 struct wpa_driver_nl80211_data *drv = bss->drv;
1036 if (!drv->associated)
1037 return -1;
1038 os_memcpy(bssid, drv->bssid, ETH_ALEN);
1039 return 0;
1040}
1041
1042
1043static int wpa_driver_nl80211_get_ssid(void *priv, u8 *ssid)
1044{
1045 struct i802_bss *bss = priv;
1046 struct wpa_driver_nl80211_data *drv = bss->drv;
1047 if (!drv->associated)
1048 return -1;
1049 os_memcpy(ssid, drv->ssid, drv->ssid_len);
1050 return drv->ssid_len;
1051}
1052
1053
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001054static void wpa_driver_nl80211_event_newlink(
1055 struct wpa_driver_nl80211_data *drv, char *ifname)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001056{
1057 union wpa_event_data event;
1058
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001059 if (os_strcmp(drv->first_bss->ifname, ifname) == 0) {
1060 if (if_nametoindex(drv->first_bss->ifname) == 0) {
1061 wpa_printf(MSG_DEBUG, "nl80211: Interface %s does not exist - ignore RTM_NEWLINK",
1062 drv->first_bss->ifname);
1063 return;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001064 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001065 if (!drv->if_removed)
1066 return;
1067 wpa_printf(MSG_DEBUG, "nl80211: Mark if_removed=0 for %s based on RTM_NEWLINK event",
1068 drv->first_bss->ifname);
1069 drv->if_removed = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001070 }
1071
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001072 os_memset(&event, 0, sizeof(event));
1073 os_strlcpy(event.interface_status.ifname, ifname,
1074 sizeof(event.interface_status.ifname));
1075 event.interface_status.ievent = EVENT_INTERFACE_ADDED;
1076 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
1077}
1078
1079
1080static void wpa_driver_nl80211_event_dellink(
1081 struct wpa_driver_nl80211_data *drv, char *ifname)
1082{
1083 union wpa_event_data event;
1084
1085 if (os_strcmp(drv->first_bss->ifname, ifname) == 0) {
1086 if (drv->if_removed) {
1087 wpa_printf(MSG_DEBUG, "nl80211: if_removed already set - ignore RTM_DELLINK event for %s",
1088 ifname);
1089 return;
1090 }
1091 wpa_printf(MSG_DEBUG, "RTM_DELLINK: Interface '%s' removed - mark if_removed=1",
1092 ifname);
1093 drv->if_removed = 1;
1094 } else {
1095 wpa_printf(MSG_DEBUG, "RTM_DELLINK: Interface '%s' removed",
1096 ifname);
1097 }
1098
1099 os_memset(&event, 0, sizeof(event));
1100 os_strlcpy(event.interface_status.ifname, ifname,
1101 sizeof(event.interface_status.ifname));
1102 event.interface_status.ievent = EVENT_INTERFACE_REMOVED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001103 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
1104}
1105
1106
1107static int wpa_driver_nl80211_own_ifname(struct wpa_driver_nl80211_data *drv,
1108 u8 *buf, size_t len)
1109{
1110 int attrlen, rta_len;
1111 struct rtattr *attr;
1112
1113 attrlen = len;
1114 attr = (struct rtattr *) buf;
1115
1116 rta_len = RTA_ALIGN(sizeof(struct rtattr));
1117 while (RTA_OK(attr, attrlen)) {
1118 if (attr->rta_type == IFLA_IFNAME) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001119 if (os_strcmp(((char *) attr) + rta_len,
1120 drv->first_bss->ifname) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001121 return 1;
1122 else
1123 break;
1124 }
1125 attr = RTA_NEXT(attr, attrlen);
1126 }
1127
1128 return 0;
1129}
1130
1131
1132static int wpa_driver_nl80211_own_ifindex(struct wpa_driver_nl80211_data *drv,
1133 int ifindex, u8 *buf, size_t len)
1134{
1135 if (drv->ifindex == ifindex)
1136 return 1;
1137
1138 if (drv->if_removed && wpa_driver_nl80211_own_ifname(drv, buf, len)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001139 wpa_printf(MSG_DEBUG, "nl80211: Update ifindex for a removed "
1140 "interface");
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001141 wpa_driver_nl80211_finish_drv_init(drv, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001142 return 1;
1143 }
1144
1145 return 0;
1146}
1147
1148
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001149static struct wpa_driver_nl80211_data *
1150nl80211_find_drv(struct nl80211_global *global, int idx, u8 *buf, size_t len)
1151{
1152 struct wpa_driver_nl80211_data *drv;
1153 dl_list_for_each(drv, &global->interfaces,
1154 struct wpa_driver_nl80211_data, list) {
1155 if (wpa_driver_nl80211_own_ifindex(drv, idx, buf, len) ||
1156 have_ifidx(drv, idx))
1157 return drv;
1158 }
1159 return NULL;
1160}
1161
1162
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001163static void wpa_driver_nl80211_event_rtm_newlink(void *ctx,
1164 struct ifinfomsg *ifi,
1165 u8 *buf, size_t len)
1166{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001167 struct nl80211_global *global = ctx;
1168 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001169 int attrlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001170 struct rtattr *attr;
1171 u32 brid = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001172 char namebuf[IFNAMSIZ];
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001173 char ifname[IFNAMSIZ + 1];
1174 char extra[100], *pos, *end;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001175
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001176 drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
1177 if (!drv) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001178 wpa_printf(MSG_DEBUG, "nl80211: Ignore RTM_NEWLINK event for foreign ifindex %d",
1179 ifi->ifi_index);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001180 return;
1181 }
1182
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001183 extra[0] = '\0';
1184 pos = extra;
1185 end = pos + sizeof(extra);
1186 ifname[0] = '\0';
1187
1188 attrlen = len;
1189 attr = (struct rtattr *) buf;
1190 while (RTA_OK(attr, attrlen)) {
1191 switch (attr->rta_type) {
1192 case IFLA_IFNAME:
1193 if (RTA_PAYLOAD(attr) >= IFNAMSIZ)
1194 break;
1195 os_memcpy(ifname, RTA_DATA(attr), RTA_PAYLOAD(attr));
1196 ifname[RTA_PAYLOAD(attr)] = '\0';
1197 break;
1198 case IFLA_MASTER:
1199 brid = nla_get_u32((struct nlattr *) attr);
1200 pos += os_snprintf(pos, end - pos, " master=%u", brid);
1201 break;
1202 case IFLA_WIRELESS:
1203 pos += os_snprintf(pos, end - pos, " wext");
1204 break;
1205 case IFLA_OPERSTATE:
1206 pos += os_snprintf(pos, end - pos, " operstate=%u",
1207 nla_get_u32((struct nlattr *) attr));
1208 break;
1209 case IFLA_LINKMODE:
1210 pos += os_snprintf(pos, end - pos, " linkmode=%u",
1211 nla_get_u32((struct nlattr *) attr));
1212 break;
1213 }
1214 attr = RTA_NEXT(attr, attrlen);
1215 }
1216 extra[sizeof(extra) - 1] = '\0';
1217
1218 wpa_printf(MSG_DEBUG, "RTM_NEWLINK: ifi_index=%d ifname=%s%s ifi_flags=0x%x (%s%s%s%s)",
1219 ifi->ifi_index, ifname, extra, ifi->ifi_flags,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001220 (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
1221 (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
1222 (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
1223 (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
1224
1225 if (!drv->if_disabled && !(ifi->ifi_flags & IFF_UP)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001226 if (if_indextoname(ifi->ifi_index, namebuf) &&
1227 linux_iface_up(drv->global->ioctl_sock,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001228 drv->first_bss->ifname) > 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001229 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
1230 "event since interface %s is up", namebuf);
1231 return;
1232 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001233 wpa_printf(MSG_DEBUG, "nl80211: Interface down");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001234 if (drv->ignore_if_down_event) {
1235 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
1236 "event generated by mode change");
1237 drv->ignore_if_down_event = 0;
1238 } else {
1239 drv->if_disabled = 1;
1240 wpa_supplicant_event(drv->ctx,
1241 EVENT_INTERFACE_DISABLED, NULL);
1242 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001243 }
1244
1245 if (drv->if_disabled && (ifi->ifi_flags & IFF_UP)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001246 if (if_indextoname(ifi->ifi_index, namebuf) &&
1247 linux_iface_up(drv->global->ioctl_sock,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001248 drv->first_bss->ifname) == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001249 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
1250 "event since interface %s is down",
1251 namebuf);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001252 } else if (if_nametoindex(drv->first_bss->ifname) == 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001253 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
1254 "event since interface %s does not exist",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001255 drv->first_bss->ifname);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001256 } else if (drv->if_removed) {
1257 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
1258 "event since interface %s is marked "
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001259 "removed", drv->first_bss->ifname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001260 } else {
1261 wpa_printf(MSG_DEBUG, "nl80211: Interface up");
1262 drv->if_disabled = 0;
1263 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_ENABLED,
1264 NULL);
1265 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001266 }
1267
1268 /*
1269 * Some drivers send the association event before the operup event--in
1270 * this case, lifting operstate in wpa_driver_nl80211_set_operstate()
1271 * fails. This will hit us when wpa_supplicant does not need to do
1272 * IEEE 802.1X authentication
1273 */
1274 if (drv->operstate == 1 &&
1275 (ifi->ifi_flags & (IFF_LOWER_UP | IFF_DORMANT)) == IFF_LOWER_UP &&
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001276 !(ifi->ifi_flags & IFF_RUNNING)) {
1277 wpa_printf(MSG_DEBUG, "nl80211: Set IF_OPER_UP again based on ifi_flags and expected operstate");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001278 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001279 -1, IF_OPER_UP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001280 }
1281
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001282 if (ifname[0])
1283 wpa_driver_nl80211_event_newlink(drv, ifname);
1284
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001285 if (ifi->ifi_family == AF_BRIDGE && brid) {
1286 /* device has been added to bridge */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001287 if_indextoname(brid, namebuf);
1288 wpa_printf(MSG_DEBUG, "nl80211: Add ifindex %u for bridge %s",
1289 brid, namebuf);
1290 add_ifidx(drv, brid);
1291 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001292}
1293
1294
1295static void wpa_driver_nl80211_event_rtm_dellink(void *ctx,
1296 struct ifinfomsg *ifi,
1297 u8 *buf, size_t len)
1298{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001299 struct nl80211_global *global = ctx;
1300 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001301 int attrlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001302 struct rtattr *attr;
1303 u32 brid = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001304 char ifname[IFNAMSIZ + 1];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001305
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001306 drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
1307 if (!drv) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001308 wpa_printf(MSG_DEBUG, "nl80211: Ignore RTM_DELLINK event for foreign ifindex %d",
1309 ifi->ifi_index);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001310 return;
1311 }
1312
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001313 ifname[0] = '\0';
1314
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001315 attrlen = len;
1316 attr = (struct rtattr *) buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001317 while (RTA_OK(attr, attrlen)) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001318 switch (attr->rta_type) {
1319 case IFLA_IFNAME:
1320 if (RTA_PAYLOAD(attr) >= IFNAMSIZ)
1321 break;
1322 os_memcpy(ifname, RTA_DATA(attr), RTA_PAYLOAD(attr));
1323 ifname[RTA_PAYLOAD(attr)] = '\0';
1324 break;
1325 case IFLA_MASTER:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001326 brid = nla_get_u32((struct nlattr *) attr);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001327 break;
1328 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001329 attr = RTA_NEXT(attr, attrlen);
1330 }
1331
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001332 if (ifname[0])
1333 wpa_driver_nl80211_event_dellink(drv, ifname);
1334
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001335 if (ifi->ifi_family == AF_BRIDGE && brid) {
1336 /* device has been removed from bridge */
1337 char namebuf[IFNAMSIZ];
1338 if_indextoname(brid, namebuf);
1339 wpa_printf(MSG_DEBUG, "nl80211: Remove ifindex %u for bridge "
1340 "%s", brid, namebuf);
1341 del_ifidx(drv, brid);
1342 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001343}
1344
1345
1346static void mlme_event_auth(struct wpa_driver_nl80211_data *drv,
1347 const u8 *frame, size_t len)
1348{
1349 const struct ieee80211_mgmt *mgmt;
1350 union wpa_event_data event;
1351
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001352 wpa_printf(MSG_DEBUG, "nl80211: Authenticate event");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001353 mgmt = (const struct ieee80211_mgmt *) frame;
1354 if (len < 24 + sizeof(mgmt->u.auth)) {
1355 wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
1356 "frame");
1357 return;
1358 }
1359
1360 os_memcpy(drv->auth_bssid, mgmt->sa, ETH_ALEN);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001361 os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001362 os_memset(&event, 0, sizeof(event));
1363 os_memcpy(event.auth.peer, mgmt->sa, ETH_ALEN);
1364 event.auth.auth_type = le_to_host16(mgmt->u.auth.auth_alg);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001365 event.auth.auth_transaction =
1366 le_to_host16(mgmt->u.auth.auth_transaction);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001367 event.auth.status_code = le_to_host16(mgmt->u.auth.status_code);
1368 if (len > 24 + sizeof(mgmt->u.auth)) {
1369 event.auth.ies = mgmt->u.auth.variable;
1370 event.auth.ies_len = len - 24 - sizeof(mgmt->u.auth);
1371 }
1372
1373 wpa_supplicant_event(drv->ctx, EVENT_AUTH, &event);
1374}
1375
1376
Jouni Malinen87fd2792011-05-16 18:35:42 +03001377static unsigned int nl80211_get_assoc_freq(struct wpa_driver_nl80211_data *drv)
1378{
1379 struct nl_msg *msg;
1380 int ret;
1381 struct nl80211_bss_info_arg arg;
1382
1383 os_memset(&arg, 0, sizeof(arg));
1384 msg = nlmsg_alloc();
1385 if (!msg)
1386 goto nla_put_failure;
1387
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001388 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
Jouni Malinen87fd2792011-05-16 18:35:42 +03001389 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
1390
1391 arg.drv = drv;
1392 ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
1393 msg = NULL;
1394 if (ret == 0) {
1395 wpa_printf(MSG_DEBUG, "nl80211: Operating frequency for the "
1396 "associated BSS from scan results: %u MHz",
1397 arg.assoc_freq);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001398 if (arg.assoc_freq)
1399 drv->assoc_freq = arg.assoc_freq;
1400 return drv->assoc_freq;
Jouni Malinen87fd2792011-05-16 18:35:42 +03001401 }
1402 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
1403 "(%s)", ret, strerror(-ret));
1404nla_put_failure:
1405 nlmsg_free(msg);
1406 return drv->assoc_freq;
1407}
1408
1409
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001410static void mlme_event_assoc(struct wpa_driver_nl80211_data *drv,
1411 const u8 *frame, size_t len)
1412{
1413 const struct ieee80211_mgmt *mgmt;
1414 union wpa_event_data event;
1415 u16 status;
1416
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001417 wpa_printf(MSG_DEBUG, "nl80211: Associate event");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001418 mgmt = (const struct ieee80211_mgmt *) frame;
1419 if (len < 24 + sizeof(mgmt->u.assoc_resp)) {
1420 wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
1421 "frame");
1422 return;
1423 }
1424
1425 status = le_to_host16(mgmt->u.assoc_resp.status_code);
1426 if (status != WLAN_STATUS_SUCCESS) {
1427 os_memset(&event, 0, sizeof(event));
1428 event.assoc_reject.bssid = mgmt->bssid;
1429 if (len > 24 + sizeof(mgmt->u.assoc_resp)) {
1430 event.assoc_reject.resp_ies =
1431 (u8 *) mgmt->u.assoc_resp.variable;
1432 event.assoc_reject.resp_ies_len =
1433 len - 24 - sizeof(mgmt->u.assoc_resp);
1434 }
1435 event.assoc_reject.status_code = status;
1436
1437 wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
1438 return;
1439 }
1440
1441 drv->associated = 1;
1442 os_memcpy(drv->bssid, mgmt->sa, ETH_ALEN);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001443 os_memcpy(drv->prev_bssid, mgmt->sa, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001444
1445 os_memset(&event, 0, sizeof(event));
1446 if (len > 24 + sizeof(mgmt->u.assoc_resp)) {
1447 event.assoc_info.resp_ies = (u8 *) mgmt->u.assoc_resp.variable;
1448 event.assoc_info.resp_ies_len =
1449 len - 24 - sizeof(mgmt->u.assoc_resp);
1450 }
1451
1452 event.assoc_info.freq = drv->assoc_freq;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001453
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001454 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
1455}
1456
1457
1458static void mlme_event_connect(struct wpa_driver_nl80211_data *drv,
1459 enum nl80211_commands cmd, struct nlattr *status,
1460 struct nlattr *addr, struct nlattr *req_ie,
1461 struct nlattr *resp_ie)
1462{
1463 union wpa_event_data event;
1464
1465 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
1466 /*
1467 * Avoid reporting two association events that would confuse
1468 * the core code.
1469 */
1470 wpa_printf(MSG_DEBUG, "nl80211: Ignore connect event (cmd=%d) "
1471 "when using userspace SME", cmd);
1472 return;
1473 }
1474
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001475 if (cmd == NL80211_CMD_CONNECT)
1476 wpa_printf(MSG_DEBUG, "nl80211: Connect event");
1477 else if (cmd == NL80211_CMD_ROAM)
1478 wpa_printf(MSG_DEBUG, "nl80211: Roam event");
1479
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001480 os_memset(&event, 0, sizeof(event));
1481 if (cmd == NL80211_CMD_CONNECT &&
1482 nla_get_u16(status) != WLAN_STATUS_SUCCESS) {
1483 if (addr)
1484 event.assoc_reject.bssid = nla_data(addr);
1485 if (resp_ie) {
1486 event.assoc_reject.resp_ies = nla_data(resp_ie);
1487 event.assoc_reject.resp_ies_len = nla_len(resp_ie);
1488 }
1489 event.assoc_reject.status_code = nla_get_u16(status);
1490 wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
1491 return;
1492 }
1493
1494 drv->associated = 1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001495 if (addr) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001496 os_memcpy(drv->bssid, nla_data(addr), ETH_ALEN);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001497 os_memcpy(drv->prev_bssid, drv->bssid, ETH_ALEN);
1498 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001499
1500 if (req_ie) {
1501 event.assoc_info.req_ies = nla_data(req_ie);
1502 event.assoc_info.req_ies_len = nla_len(req_ie);
1503 }
1504 if (resp_ie) {
1505 event.assoc_info.resp_ies = nla_data(resp_ie);
1506 event.assoc_info.resp_ies_len = nla_len(resp_ie);
1507 }
1508
Jouni Malinen87fd2792011-05-16 18:35:42 +03001509 event.assoc_info.freq = nl80211_get_assoc_freq(drv);
1510
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001511 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
1512}
1513
1514
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001515static void mlme_event_disconnect(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001516 struct nlattr *reason, struct nlattr *addr,
1517 struct nlattr *by_ap)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001518{
1519 union wpa_event_data data;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001520 unsigned int locally_generated = by_ap == NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001521
1522 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
1523 /*
1524 * Avoid reporting two disassociation events that could
1525 * confuse the core code.
1526 */
1527 wpa_printf(MSG_DEBUG, "nl80211: Ignore disconnect "
1528 "event when using userspace SME");
1529 return;
1530 }
1531
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001532 if (drv->ignore_next_local_disconnect) {
1533 drv->ignore_next_local_disconnect = 0;
1534 if (locally_generated) {
1535 wpa_printf(MSG_DEBUG, "nl80211: Ignore disconnect "
1536 "event triggered during reassociation");
1537 return;
1538 }
1539 wpa_printf(MSG_WARNING, "nl80211: Was expecting local "
1540 "disconnect but got another disconnect "
1541 "event first");
1542 }
1543
1544 wpa_printf(MSG_DEBUG, "nl80211: Disconnect event");
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001545 nl80211_mark_disconnected(drv);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001546 os_memset(&data, 0, sizeof(data));
1547 if (reason)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001548 data.deauth_info.reason_code = nla_get_u16(reason);
1549 data.deauth_info.locally_generated = by_ap == NULL;
1550 wpa_supplicant_event(drv->ctx, EVENT_DEAUTH, &data);
1551}
1552
1553
Dmitry Shmidt97672262014-02-03 13:02:54 -08001554static int calculate_chan_offset(int width, int freq, int cf1, int cf2)
1555{
1556 int freq1 = 0;
1557
1558 switch (convert2width(width)) {
1559 case CHAN_WIDTH_20_NOHT:
1560 case CHAN_WIDTH_20:
1561 return 0;
1562 case CHAN_WIDTH_40:
1563 freq1 = cf1 - 10;
1564 break;
1565 case CHAN_WIDTH_80:
1566 freq1 = cf1 - 30;
1567 break;
1568 case CHAN_WIDTH_160:
1569 freq1 = cf1 - 70;
1570 break;
1571 case CHAN_WIDTH_UNKNOWN:
1572 case CHAN_WIDTH_80P80:
1573 /* FIXME: implement this */
1574 return 0;
1575 }
1576
1577 return (abs(freq - freq1) / 20) % 2 == 0 ? 1 : -1;
1578}
1579
1580
Dmitry Shmidt04949592012-07-19 12:16:46 -07001581static void mlme_event_ch_switch(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001582 struct nlattr *ifindex, struct nlattr *freq,
1583 struct nlattr *type, struct nlattr *bw,
1584 struct nlattr *cf1, struct nlattr *cf2)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001585{
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001586 struct i802_bss *bss;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001587 union wpa_event_data data;
1588 int ht_enabled = 1;
1589 int chan_offset = 0;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001590 int ifidx;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001591
1592 wpa_printf(MSG_DEBUG, "nl80211: Channel switch event");
1593
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001594 if (!freq)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001595 return;
1596
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001597 ifidx = nla_get_u32(ifindex);
1598 for (bss = drv->first_bss; bss; bss = bss->next)
1599 if (bss->ifindex == ifidx)
1600 break;
1601
1602 if (bss == NULL) {
1603 wpa_printf(MSG_WARNING, "nl80211: Unknown ifindex (%d) for channel switch, ignoring",
1604 ifidx);
1605 return;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001606 }
1607
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001608 if (type) {
1609 switch (nla_get_u32(type)) {
1610 case NL80211_CHAN_NO_HT:
1611 ht_enabled = 0;
1612 break;
1613 case NL80211_CHAN_HT20:
1614 break;
1615 case NL80211_CHAN_HT40PLUS:
1616 chan_offset = 1;
1617 break;
1618 case NL80211_CHAN_HT40MINUS:
1619 chan_offset = -1;
1620 break;
1621 }
Dmitry Shmidt97672262014-02-03 13:02:54 -08001622 } else if (bw && cf1) {
1623 /* This can happen for example with VHT80 ch switch */
1624 chan_offset = calculate_chan_offset(nla_get_u32(bw),
1625 nla_get_u32(freq),
1626 nla_get_u32(cf1),
1627 cf2 ? nla_get_u32(cf2) : 0);
1628 } else {
1629 wpa_printf(MSG_WARNING, "nl80211: Unknown secondary channel information - following channel definition calculations may fail");
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001630 }
1631
1632 os_memset(&data, 0, sizeof(data));
Dmitry Shmidt04949592012-07-19 12:16:46 -07001633 data.ch_switch.freq = nla_get_u32(freq);
1634 data.ch_switch.ht_enabled = ht_enabled;
1635 data.ch_switch.ch_offset = chan_offset;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001636 if (bw)
1637 data.ch_switch.ch_width = convert2width(nla_get_u32(bw));
1638 if (cf1)
1639 data.ch_switch.cf1 = nla_get_u32(cf1);
1640 if (cf2)
1641 data.ch_switch.cf2 = nla_get_u32(cf2);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001642
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001643 bss->freq = data.ch_switch.freq;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001644
Dmitry Shmidt04949592012-07-19 12:16:46 -07001645 wpa_supplicant_event(drv->ctx, EVENT_CH_SWITCH, &data);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001646}
1647
1648
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001649static void mlme_timeout_event(struct wpa_driver_nl80211_data *drv,
1650 enum nl80211_commands cmd, struct nlattr *addr)
1651{
1652 union wpa_event_data event;
1653 enum wpa_event_type ev;
1654
1655 if (nla_len(addr) != ETH_ALEN)
1656 return;
1657
1658 wpa_printf(MSG_DEBUG, "nl80211: MLME event %d; timeout with " MACSTR,
1659 cmd, MAC2STR((u8 *) nla_data(addr)));
1660
1661 if (cmd == NL80211_CMD_AUTHENTICATE)
1662 ev = EVENT_AUTH_TIMED_OUT;
1663 else if (cmd == NL80211_CMD_ASSOCIATE)
1664 ev = EVENT_ASSOC_TIMED_OUT;
1665 else
1666 return;
1667
1668 os_memset(&event, 0, sizeof(event));
1669 os_memcpy(event.timeout_event.addr, nla_data(addr), ETH_ALEN);
1670 wpa_supplicant_event(drv->ctx, ev, &event);
1671}
1672
1673
1674static void mlme_event_mgmt(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001675 struct nlattr *freq, struct nlattr *sig,
1676 const u8 *frame, size_t len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001677{
1678 const struct ieee80211_mgmt *mgmt;
1679 union wpa_event_data event;
1680 u16 fc, stype;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001681 int ssi_signal = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001682 int rx_freq = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001683
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001684 wpa_printf(MSG_MSGDUMP, "nl80211: Frame event");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001685 mgmt = (const struct ieee80211_mgmt *) frame;
1686 if (len < 24) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001687 wpa_printf(MSG_DEBUG, "nl80211: Too short management frame");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001688 return;
1689 }
1690
1691 fc = le_to_host16(mgmt->frame_control);
1692 stype = WLAN_FC_GET_STYPE(fc);
1693
Dmitry Shmidt04949592012-07-19 12:16:46 -07001694 if (sig)
1695 ssi_signal = (s32) nla_get_u32(sig);
1696
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001697 os_memset(&event, 0, sizeof(event));
1698 if (freq) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001699 event.rx_mgmt.freq = nla_get_u32(freq);
1700 rx_freq = drv->last_mgmt_freq = event.rx_mgmt.freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001701 }
Dmitry Shmidt56052862013-10-04 10:23:25 -07001702 wpa_printf(MSG_DEBUG,
1703 "nl80211: RX frame freq=%d ssi_signal=%d stype=%u len=%u",
1704 rx_freq, ssi_signal, stype, (unsigned int) len);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001705 event.rx_mgmt.frame = frame;
1706 event.rx_mgmt.frame_len = len;
1707 event.rx_mgmt.ssi_signal = ssi_signal;
1708 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001709}
1710
1711
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001712static void mlme_event_mgmt_tx_status(struct wpa_driver_nl80211_data *drv,
1713 struct nlattr *cookie, const u8 *frame,
1714 size_t len, struct nlattr *ack)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001715{
1716 union wpa_event_data event;
1717 const struct ieee80211_hdr *hdr;
1718 u16 fc;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001719
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001720 wpa_printf(MSG_DEBUG, "nl80211: Frame TX status event");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001721 if (!is_ap_interface(drv->nlmode)) {
1722 u64 cookie_val;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001723
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001724 if (!cookie)
1725 return;
1726
1727 cookie_val = nla_get_u64(cookie);
1728 wpa_printf(MSG_DEBUG, "nl80211: Action TX status:"
1729 " cookie=0%llx%s (ack=%d)",
1730 (long long unsigned int) cookie_val,
1731 cookie_val == drv->send_action_cookie ?
1732 " (match)" : " (unknown)", ack != NULL);
1733 if (cookie_val != drv->send_action_cookie)
1734 return;
1735 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001736
1737 hdr = (const struct ieee80211_hdr *) frame;
1738 fc = le_to_host16(hdr->frame_control);
1739
1740 os_memset(&event, 0, sizeof(event));
1741 event.tx_status.type = WLAN_FC_GET_TYPE(fc);
1742 event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
1743 event.tx_status.dst = hdr->addr1;
1744 event.tx_status.data = frame;
1745 event.tx_status.data_len = len;
1746 event.tx_status.ack = ack != NULL;
1747 wpa_supplicant_event(drv->ctx, EVENT_TX_STATUS, &event);
1748}
1749
1750
1751static void mlme_event_deauth_disassoc(struct wpa_driver_nl80211_data *drv,
1752 enum wpa_event_type type,
1753 const u8 *frame, size_t len)
1754{
1755 const struct ieee80211_mgmt *mgmt;
1756 union wpa_event_data event;
1757 const u8 *bssid = NULL;
1758 u16 reason_code = 0;
1759
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001760 if (type == EVENT_DEAUTH)
1761 wpa_printf(MSG_DEBUG, "nl80211: Deauthenticate event");
1762 else
1763 wpa_printf(MSG_DEBUG, "nl80211: Disassociate event");
1764
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001765 mgmt = (const struct ieee80211_mgmt *) frame;
1766 if (len >= 24) {
1767 bssid = mgmt->bssid;
1768
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001769 if ((drv->capa.flags & WPA_DRIVER_FLAGS_SME) &&
1770 !drv->associated &&
1771 os_memcmp(bssid, drv->auth_bssid, ETH_ALEN) != 0 &&
1772 os_memcmp(bssid, drv->auth_attempt_bssid, ETH_ALEN) != 0 &&
1773 os_memcmp(bssid, drv->prev_bssid, ETH_ALEN) == 0) {
1774 /*
1775 * Avoid issues with some roaming cases where
1776 * disconnection event for the old AP may show up after
1777 * we have started connection with the new AP.
1778 */
1779 wpa_printf(MSG_DEBUG, "nl80211: Ignore deauth/disassoc event from old AP " MACSTR " when already authenticating with " MACSTR,
1780 MAC2STR(bssid),
1781 MAC2STR(drv->auth_attempt_bssid));
1782 return;
1783 }
1784
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001785 if (drv->associated != 0 &&
1786 os_memcmp(bssid, drv->bssid, ETH_ALEN) != 0 &&
1787 os_memcmp(bssid, drv->auth_bssid, ETH_ALEN) != 0) {
1788 /*
1789 * We have presumably received this deauth as a
1790 * response to a clear_state_mismatch() outgoing
1791 * deauth. Don't let it take us offline!
1792 */
1793 wpa_printf(MSG_DEBUG, "nl80211: Deauth received "
1794 "from Unknown BSSID " MACSTR " -- ignoring",
1795 MAC2STR(bssid));
1796 return;
1797 }
1798 }
1799
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001800 nl80211_mark_disconnected(drv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001801 os_memset(&event, 0, sizeof(event));
1802
1803 /* Note: Same offset for Reason Code in both frame subtypes */
1804 if (len >= 24 + sizeof(mgmt->u.deauth))
1805 reason_code = le_to_host16(mgmt->u.deauth.reason_code);
1806
1807 if (type == EVENT_DISASSOC) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001808 event.disassoc_info.locally_generated =
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001809 !os_memcmp(mgmt->sa, drv->first_bss->addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001810 event.disassoc_info.addr = bssid;
1811 event.disassoc_info.reason_code = reason_code;
1812 if (frame + len > mgmt->u.disassoc.variable) {
1813 event.disassoc_info.ie = mgmt->u.disassoc.variable;
1814 event.disassoc_info.ie_len = frame + len -
1815 mgmt->u.disassoc.variable;
1816 }
1817 } else {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001818 event.deauth_info.locally_generated =
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001819 !os_memcmp(mgmt->sa, drv->first_bss->addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001820 event.deauth_info.addr = bssid;
1821 event.deauth_info.reason_code = reason_code;
1822 if (frame + len > mgmt->u.deauth.variable) {
1823 event.deauth_info.ie = mgmt->u.deauth.variable;
1824 event.deauth_info.ie_len = frame + len -
1825 mgmt->u.deauth.variable;
1826 }
1827 }
1828
1829 wpa_supplicant_event(drv->ctx, type, &event);
1830}
1831
1832
1833static void mlme_event_unprot_disconnect(struct wpa_driver_nl80211_data *drv,
1834 enum wpa_event_type type,
1835 const u8 *frame, size_t len)
1836{
1837 const struct ieee80211_mgmt *mgmt;
1838 union wpa_event_data event;
1839 u16 reason_code = 0;
1840
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001841 if (type == EVENT_UNPROT_DEAUTH)
1842 wpa_printf(MSG_DEBUG, "nl80211: Unprot Deauthenticate event");
1843 else
1844 wpa_printf(MSG_DEBUG, "nl80211: Unprot Disassociate event");
1845
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001846 if (len < 24)
1847 return;
1848
1849 mgmt = (const struct ieee80211_mgmt *) frame;
1850
1851 os_memset(&event, 0, sizeof(event));
1852 /* Note: Same offset for Reason Code in both frame subtypes */
1853 if (len >= 24 + sizeof(mgmt->u.deauth))
1854 reason_code = le_to_host16(mgmt->u.deauth.reason_code);
1855
1856 if (type == EVENT_UNPROT_DISASSOC) {
1857 event.unprot_disassoc.sa = mgmt->sa;
1858 event.unprot_disassoc.da = mgmt->da;
1859 event.unprot_disassoc.reason_code = reason_code;
1860 } else {
1861 event.unprot_deauth.sa = mgmt->sa;
1862 event.unprot_deauth.da = mgmt->da;
1863 event.unprot_deauth.reason_code = reason_code;
1864 }
1865
1866 wpa_supplicant_event(drv->ctx, type, &event);
1867}
1868
1869
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001870static void mlme_event(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001871 enum nl80211_commands cmd, struct nlattr *frame,
1872 struct nlattr *addr, struct nlattr *timed_out,
1873 struct nlattr *freq, struct nlattr *ack,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001874 struct nlattr *cookie, struct nlattr *sig)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001875{
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001876 struct wpa_driver_nl80211_data *drv = bss->drv;
1877 const u8 *data;
1878 size_t len;
1879
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001880 if (timed_out && addr) {
1881 mlme_timeout_event(drv, cmd, addr);
1882 return;
1883 }
1884
1885 if (frame == NULL) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001886 wpa_printf(MSG_DEBUG,
1887 "nl80211: MLME event %d (%s) without frame data",
1888 cmd, nl80211_command_to_string(cmd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001889 return;
1890 }
1891
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001892 data = nla_data(frame);
1893 len = nla_len(frame);
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001894 if (len < 4 + 2 * ETH_ALEN) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001895 wpa_printf(MSG_MSGDUMP, "nl80211: MLME event %d (%s) on %s("
1896 MACSTR ") - too short",
1897 cmd, nl80211_command_to_string(cmd), bss->ifname,
1898 MAC2STR(bss->addr));
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001899 return;
1900 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001901 wpa_printf(MSG_MSGDUMP, "nl80211: MLME event %d (%s) on %s(" MACSTR
1902 ") A1=" MACSTR " A2=" MACSTR, cmd,
1903 nl80211_command_to_string(cmd), bss->ifname,
1904 MAC2STR(bss->addr), MAC2STR(data + 4),
1905 MAC2STR(data + 4 + ETH_ALEN));
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001906 if (cmd != NL80211_CMD_FRAME_TX_STATUS && !(data[4] & 0x01) &&
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001907 os_memcmp(bss->addr, data + 4, ETH_ALEN) != 0 &&
1908 os_memcmp(bss->addr, data + 4 + ETH_ALEN, ETH_ALEN) != 0) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001909 wpa_printf(MSG_MSGDUMP, "nl80211: %s: Ignore MLME frame event "
1910 "for foreign address", bss->ifname);
1911 return;
1912 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001913 wpa_hexdump(MSG_MSGDUMP, "nl80211: MLME event frame",
1914 nla_data(frame), nla_len(frame));
1915
1916 switch (cmd) {
1917 case NL80211_CMD_AUTHENTICATE:
1918 mlme_event_auth(drv, nla_data(frame), nla_len(frame));
1919 break;
1920 case NL80211_CMD_ASSOCIATE:
1921 mlme_event_assoc(drv, nla_data(frame), nla_len(frame));
1922 break;
1923 case NL80211_CMD_DEAUTHENTICATE:
1924 mlme_event_deauth_disassoc(drv, EVENT_DEAUTH,
1925 nla_data(frame), nla_len(frame));
1926 break;
1927 case NL80211_CMD_DISASSOCIATE:
1928 mlme_event_deauth_disassoc(drv, EVENT_DISASSOC,
1929 nla_data(frame), nla_len(frame));
1930 break;
1931 case NL80211_CMD_FRAME:
Dmitry Shmidt04949592012-07-19 12:16:46 -07001932 mlme_event_mgmt(drv, freq, sig, nla_data(frame),
1933 nla_len(frame));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001934 break;
1935 case NL80211_CMD_FRAME_TX_STATUS:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001936 mlme_event_mgmt_tx_status(drv, cookie, nla_data(frame),
1937 nla_len(frame), ack);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001938 break;
1939 case NL80211_CMD_UNPROT_DEAUTHENTICATE:
1940 mlme_event_unprot_disconnect(drv, EVENT_UNPROT_DEAUTH,
1941 nla_data(frame), nla_len(frame));
1942 break;
1943 case NL80211_CMD_UNPROT_DISASSOCIATE:
1944 mlme_event_unprot_disconnect(drv, EVENT_UNPROT_DISASSOC,
1945 nla_data(frame), nla_len(frame));
1946 break;
1947 default:
1948 break;
1949 }
1950}
1951
1952
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001953static void mlme_event_michael_mic_failure(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001954 struct nlattr *tb[])
1955{
1956 union wpa_event_data data;
1957
1958 wpa_printf(MSG_DEBUG, "nl80211: MLME event Michael MIC failure");
1959 os_memset(&data, 0, sizeof(data));
1960 if (tb[NL80211_ATTR_MAC]) {
1961 wpa_hexdump(MSG_DEBUG, "nl80211: Source MAC address",
1962 nla_data(tb[NL80211_ATTR_MAC]),
1963 nla_len(tb[NL80211_ATTR_MAC]));
1964 data.michael_mic_failure.src = nla_data(tb[NL80211_ATTR_MAC]);
1965 }
1966 if (tb[NL80211_ATTR_KEY_SEQ]) {
1967 wpa_hexdump(MSG_DEBUG, "nl80211: TSC",
1968 nla_data(tb[NL80211_ATTR_KEY_SEQ]),
1969 nla_len(tb[NL80211_ATTR_KEY_SEQ]));
1970 }
1971 if (tb[NL80211_ATTR_KEY_TYPE]) {
1972 enum nl80211_key_type key_type =
1973 nla_get_u32(tb[NL80211_ATTR_KEY_TYPE]);
1974 wpa_printf(MSG_DEBUG, "nl80211: Key Type %d", key_type);
1975 if (key_type == NL80211_KEYTYPE_PAIRWISE)
1976 data.michael_mic_failure.unicast = 1;
1977 } else
1978 data.michael_mic_failure.unicast = 1;
1979
1980 if (tb[NL80211_ATTR_KEY_IDX]) {
1981 u8 key_id = nla_get_u8(tb[NL80211_ATTR_KEY_IDX]);
1982 wpa_printf(MSG_DEBUG, "nl80211: Key Id %d", key_id);
1983 }
1984
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001985 wpa_supplicant_event(bss->ctx, EVENT_MICHAEL_MIC_FAILURE, &data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001986}
1987
1988
1989static void mlme_event_join_ibss(struct wpa_driver_nl80211_data *drv,
1990 struct nlattr *tb[])
1991{
1992 if (tb[NL80211_ATTR_MAC] == NULL) {
1993 wpa_printf(MSG_DEBUG, "nl80211: No address in IBSS joined "
1994 "event");
1995 return;
1996 }
1997 os_memcpy(drv->bssid, nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07001998
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001999 drv->associated = 1;
2000 wpa_printf(MSG_DEBUG, "nl80211: IBSS " MACSTR " joined",
2001 MAC2STR(drv->bssid));
2002
2003 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, NULL);
2004}
2005
2006
2007static void mlme_event_remain_on_channel(struct wpa_driver_nl80211_data *drv,
2008 int cancel_event, struct nlattr *tb[])
2009{
2010 unsigned int freq, chan_type, duration;
2011 union wpa_event_data data;
2012 u64 cookie;
2013
2014 if (tb[NL80211_ATTR_WIPHY_FREQ])
2015 freq = nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]);
2016 else
2017 freq = 0;
2018
2019 if (tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE])
2020 chan_type = nla_get_u32(tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
2021 else
2022 chan_type = 0;
2023
2024 if (tb[NL80211_ATTR_DURATION])
2025 duration = nla_get_u32(tb[NL80211_ATTR_DURATION]);
2026 else
2027 duration = 0;
2028
2029 if (tb[NL80211_ATTR_COOKIE])
2030 cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
2031 else
2032 cookie = 0;
2033
2034 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel event (cancel=%d "
2035 "freq=%u channel_type=%u duration=%u cookie=0x%llx (%s))",
2036 cancel_event, freq, chan_type, duration,
2037 (long long unsigned int) cookie,
2038 cookie == drv->remain_on_chan_cookie ? "match" : "unknown");
2039
2040 if (cookie != drv->remain_on_chan_cookie)
2041 return; /* not for us */
2042
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002043 if (cancel_event)
2044 drv->pending_remain_on_chan = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002045
2046 os_memset(&data, 0, sizeof(data));
2047 data.remain_on_channel.freq = freq;
2048 data.remain_on_channel.duration = duration;
2049 wpa_supplicant_event(drv->ctx, cancel_event ?
2050 EVENT_CANCEL_REMAIN_ON_CHANNEL :
2051 EVENT_REMAIN_ON_CHANNEL, &data);
2052}
2053
2054
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002055static void mlme_event_ft_event(struct wpa_driver_nl80211_data *drv,
2056 struct nlattr *tb[])
2057{
2058 union wpa_event_data data;
2059
2060 os_memset(&data, 0, sizeof(data));
2061
2062 if (tb[NL80211_ATTR_IE]) {
2063 data.ft_ies.ies = nla_data(tb[NL80211_ATTR_IE]);
2064 data.ft_ies.ies_len = nla_len(tb[NL80211_ATTR_IE]);
2065 }
2066
2067 if (tb[NL80211_ATTR_IE_RIC]) {
2068 data.ft_ies.ric_ies = nla_data(tb[NL80211_ATTR_IE_RIC]);
2069 data.ft_ies.ric_ies_len = nla_len(tb[NL80211_ATTR_IE_RIC]);
2070 }
2071
2072 if (tb[NL80211_ATTR_MAC])
2073 os_memcpy(data.ft_ies.target_ap,
2074 nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
2075
2076 wpa_printf(MSG_DEBUG, "nl80211: FT event target_ap " MACSTR,
2077 MAC2STR(data.ft_ies.target_ap));
2078
2079 wpa_supplicant_event(drv->ctx, EVENT_FT_RESPONSE, &data);
2080}
2081
2082
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002083static void send_scan_event(struct wpa_driver_nl80211_data *drv, int aborted,
2084 struct nlattr *tb[])
2085{
2086 union wpa_event_data event;
2087 struct nlattr *nl;
2088 int rem;
2089 struct scan_info *info;
2090#define MAX_REPORT_FREQS 50
2091 int freqs[MAX_REPORT_FREQS];
2092 int num_freqs = 0;
2093
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002094 if (drv->scan_for_auth) {
2095 drv->scan_for_auth = 0;
2096 wpa_printf(MSG_DEBUG, "nl80211: Scan results for missing "
2097 "cfg80211 BSS entry");
2098 wpa_driver_nl80211_authenticate_retry(drv);
2099 return;
2100 }
2101
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002102 os_memset(&event, 0, sizeof(event));
2103 info = &event.scan_info;
2104 info->aborted = aborted;
2105
2106 if (tb[NL80211_ATTR_SCAN_SSIDS]) {
2107 nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_SSIDS], rem) {
2108 struct wpa_driver_scan_ssid *s =
2109 &info->ssids[info->num_ssids];
2110 s->ssid = nla_data(nl);
2111 s->ssid_len = nla_len(nl);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002112 wpa_printf(MSG_DEBUG, "nl80211: Scan probed for SSID '%s'",
2113 wpa_ssid_txt(s->ssid, s->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002114 info->num_ssids++;
2115 if (info->num_ssids == WPAS_MAX_SCAN_SSIDS)
2116 break;
2117 }
2118 }
2119 if (tb[NL80211_ATTR_SCAN_FREQUENCIES]) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002120 char msg[200], *pos, *end;
2121 int res;
2122
2123 pos = msg;
2124 end = pos + sizeof(msg);
2125 *pos = '\0';
2126
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002127 nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_FREQUENCIES], rem)
2128 {
2129 freqs[num_freqs] = nla_get_u32(nl);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002130 res = os_snprintf(pos, end - pos, " %d",
2131 freqs[num_freqs]);
2132 if (res > 0 && end - pos > res)
2133 pos += res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002134 num_freqs++;
2135 if (num_freqs == MAX_REPORT_FREQS - 1)
2136 break;
2137 }
2138 info->freqs = freqs;
2139 info->num_freqs = num_freqs;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002140 wpa_printf(MSG_DEBUG, "nl80211: Scan included frequencies:%s",
2141 msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002142 }
2143 wpa_supplicant_event(drv->ctx, EVENT_SCAN_RESULTS, &event);
2144}
2145
2146
2147static int get_link_signal(struct nl_msg *msg, void *arg)
2148{
2149 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2150 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2151 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
2152 static struct nla_policy policy[NL80211_STA_INFO_MAX + 1] = {
2153 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002154 [NL80211_STA_INFO_SIGNAL_AVG] = { .type = NLA_U8 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002155 };
2156 struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
2157 static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
2158 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
2159 [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
2160 [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
2161 [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
2162 };
2163 struct wpa_signal_info *sig_change = arg;
2164
2165 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2166 genlmsg_attrlen(gnlh, 0), NULL);
2167 if (!tb[NL80211_ATTR_STA_INFO] ||
2168 nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
2169 tb[NL80211_ATTR_STA_INFO], policy))
2170 return NL_SKIP;
2171 if (!sinfo[NL80211_STA_INFO_SIGNAL])
2172 return NL_SKIP;
2173
2174 sig_change->current_signal =
2175 (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
2176
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002177 if (sinfo[NL80211_STA_INFO_SIGNAL_AVG])
2178 sig_change->avg_signal =
2179 (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL_AVG]);
2180 else
2181 sig_change->avg_signal = 0;
2182
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002183 if (sinfo[NL80211_STA_INFO_TX_BITRATE]) {
2184 if (nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
2185 sinfo[NL80211_STA_INFO_TX_BITRATE],
2186 rate_policy)) {
2187 sig_change->current_txrate = 0;
2188 } else {
2189 if (rinfo[NL80211_RATE_INFO_BITRATE]) {
2190 sig_change->current_txrate =
2191 nla_get_u16(rinfo[
2192 NL80211_RATE_INFO_BITRATE]) * 100;
2193 }
2194 }
2195 }
2196
2197 return NL_SKIP;
2198}
2199
2200
2201static int nl80211_get_link_signal(struct wpa_driver_nl80211_data *drv,
2202 struct wpa_signal_info *sig)
2203{
2204 struct nl_msg *msg;
2205
2206 sig->current_signal = -9999;
2207 sig->current_txrate = 0;
2208
2209 msg = nlmsg_alloc();
2210 if (!msg)
2211 return -ENOMEM;
2212
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002213 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002214
2215 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
2216 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid);
2217
2218 return send_and_recv_msgs(drv, msg, get_link_signal, sig);
2219 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002220 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002221 return -ENOBUFS;
2222}
2223
2224
2225static int get_link_noise(struct nl_msg *msg, void *arg)
2226{
2227 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2228 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2229 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
2230 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
2231 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
2232 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
2233 };
2234 struct wpa_signal_info *sig_change = arg;
2235
2236 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2237 genlmsg_attrlen(gnlh, 0), NULL);
2238
2239 if (!tb[NL80211_ATTR_SURVEY_INFO]) {
2240 wpa_printf(MSG_DEBUG, "nl80211: survey data missing!");
2241 return NL_SKIP;
2242 }
2243
2244 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
2245 tb[NL80211_ATTR_SURVEY_INFO],
2246 survey_policy)) {
2247 wpa_printf(MSG_DEBUG, "nl80211: failed to parse nested "
2248 "attributes!");
2249 return NL_SKIP;
2250 }
2251
2252 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
2253 return NL_SKIP;
2254
2255 if (nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
2256 sig_change->frequency)
2257 return NL_SKIP;
2258
2259 if (!sinfo[NL80211_SURVEY_INFO_NOISE])
2260 return NL_SKIP;
2261
2262 sig_change->current_noise =
2263 (s8) nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
2264
2265 return NL_SKIP;
2266}
2267
2268
2269static int nl80211_get_link_noise(struct wpa_driver_nl80211_data *drv,
2270 struct wpa_signal_info *sig_change)
2271{
2272 struct nl_msg *msg;
2273
2274 sig_change->current_noise = 9999;
2275 sig_change->frequency = drv->assoc_freq;
2276
2277 msg = nlmsg_alloc();
2278 if (!msg)
2279 return -ENOMEM;
2280
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002281 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002282
2283 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
2284
2285 return send_and_recv_msgs(drv, msg, get_link_noise, sig_change);
2286 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002287 nlmsg_free(msg);
2288 return -ENOBUFS;
2289}
2290
2291
2292static int get_noise_for_scan_results(struct nl_msg *msg, void *arg)
2293{
2294 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2295 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2296 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
2297 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
2298 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
2299 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
2300 };
2301 struct wpa_scan_results *scan_results = arg;
2302 struct wpa_scan_res *scan_res;
2303 size_t i;
2304
2305 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2306 genlmsg_attrlen(gnlh, 0), NULL);
2307
2308 if (!tb[NL80211_ATTR_SURVEY_INFO]) {
2309 wpa_printf(MSG_DEBUG, "nl80211: Survey data missing");
2310 return NL_SKIP;
2311 }
2312
2313 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
2314 tb[NL80211_ATTR_SURVEY_INFO],
2315 survey_policy)) {
2316 wpa_printf(MSG_DEBUG, "nl80211: Failed to parse nested "
2317 "attributes");
2318 return NL_SKIP;
2319 }
2320
2321 if (!sinfo[NL80211_SURVEY_INFO_NOISE])
2322 return NL_SKIP;
2323
2324 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
2325 return NL_SKIP;
2326
2327 for (i = 0; i < scan_results->num; ++i) {
2328 scan_res = scan_results->res[i];
2329 if (!scan_res)
2330 continue;
2331 if ((int) nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
2332 scan_res->freq)
2333 continue;
2334 if (!(scan_res->flags & WPA_SCAN_NOISE_INVALID))
2335 continue;
2336 scan_res->noise = (s8)
2337 nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
2338 scan_res->flags &= ~WPA_SCAN_NOISE_INVALID;
2339 }
2340
2341 return NL_SKIP;
2342}
2343
2344
2345static int nl80211_get_noise_for_scan_results(
2346 struct wpa_driver_nl80211_data *drv,
2347 struct wpa_scan_results *scan_res)
2348{
2349 struct nl_msg *msg;
2350
2351 msg = nlmsg_alloc();
2352 if (!msg)
2353 return -ENOMEM;
2354
2355 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
2356
2357 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
2358
2359 return send_and_recv_msgs(drv, msg, get_noise_for_scan_results,
2360 scan_res);
2361 nla_put_failure:
2362 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002363 return -ENOBUFS;
2364}
2365
2366
2367static void nl80211_cqm_event(struct wpa_driver_nl80211_data *drv,
2368 struct nlattr *tb[])
2369{
2370 static struct nla_policy cqm_policy[NL80211_ATTR_CQM_MAX + 1] = {
2371 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
2372 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U8 },
2373 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
2374 [NL80211_ATTR_CQM_PKT_LOSS_EVENT] = { .type = NLA_U32 },
2375 };
2376 struct nlattr *cqm[NL80211_ATTR_CQM_MAX + 1];
2377 enum nl80211_cqm_rssi_threshold_event event;
2378 union wpa_event_data ed;
2379 struct wpa_signal_info sig;
2380 int res;
2381
2382 if (tb[NL80211_ATTR_CQM] == NULL ||
2383 nla_parse_nested(cqm, NL80211_ATTR_CQM_MAX, tb[NL80211_ATTR_CQM],
2384 cqm_policy)) {
2385 wpa_printf(MSG_DEBUG, "nl80211: Ignore invalid CQM event");
2386 return;
2387 }
2388
2389 os_memset(&ed, 0, sizeof(ed));
2390
2391 if (cqm[NL80211_ATTR_CQM_PKT_LOSS_EVENT]) {
2392 if (!tb[NL80211_ATTR_MAC])
2393 return;
2394 os_memcpy(ed.low_ack.addr, nla_data(tb[NL80211_ATTR_MAC]),
2395 ETH_ALEN);
2396 wpa_supplicant_event(drv->ctx, EVENT_STATION_LOW_ACK, &ed);
2397 return;
2398 }
2399
2400 if (cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] == NULL)
2401 return;
2402 event = nla_get_u32(cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT]);
2403
2404 if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH) {
2405 wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor "
2406 "event: RSSI high");
2407 ed.signal_change.above_threshold = 1;
2408 } else if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW) {
2409 wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor "
2410 "event: RSSI low");
2411 ed.signal_change.above_threshold = 0;
2412 } else
2413 return;
2414
2415 res = nl80211_get_link_signal(drv, &sig);
2416 if (res == 0) {
2417 ed.signal_change.current_signal = sig.current_signal;
2418 ed.signal_change.current_txrate = sig.current_txrate;
2419 wpa_printf(MSG_DEBUG, "nl80211: Signal: %d dBm txrate: %d",
2420 sig.current_signal, sig.current_txrate);
2421 }
2422
2423 res = nl80211_get_link_noise(drv, &sig);
2424 if (res == 0) {
2425 ed.signal_change.current_noise = sig.current_noise;
2426 wpa_printf(MSG_DEBUG, "nl80211: Noise: %d dBm",
2427 sig.current_noise);
2428 }
2429
2430 wpa_supplicant_event(drv->ctx, EVENT_SIGNAL_CHANGE, &ed);
2431}
2432
2433
2434static void nl80211_new_station_event(struct wpa_driver_nl80211_data *drv,
2435 struct nlattr **tb)
2436{
2437 u8 *addr;
2438 union wpa_event_data data;
2439
2440 if (tb[NL80211_ATTR_MAC] == NULL)
2441 return;
2442 addr = nla_data(tb[NL80211_ATTR_MAC]);
2443 wpa_printf(MSG_DEBUG, "nl80211: New station " MACSTR, MAC2STR(addr));
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07002444
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002445 if (is_ap_interface(drv->nlmode) && drv->device_ap_sme) {
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07002446 u8 *ies = NULL;
2447 size_t ies_len = 0;
2448 if (tb[NL80211_ATTR_IE]) {
2449 ies = nla_data(tb[NL80211_ATTR_IE]);
2450 ies_len = nla_len(tb[NL80211_ATTR_IE]);
2451 }
2452 wpa_hexdump(MSG_DEBUG, "nl80211: Assoc Req IEs", ies, ies_len);
2453 drv_event_assoc(drv->ctx, addr, ies, ies_len, 0);
2454 return;
2455 }
2456
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002457 if (drv->nlmode != NL80211_IFTYPE_ADHOC)
2458 return;
2459
2460 os_memset(&data, 0, sizeof(data));
2461 os_memcpy(data.ibss_rsn_start.peer, addr, ETH_ALEN);
2462 wpa_supplicant_event(drv->ctx, EVENT_IBSS_RSN_START, &data);
2463}
2464
2465
2466static void nl80211_del_station_event(struct wpa_driver_nl80211_data *drv,
2467 struct nlattr **tb)
2468{
2469 u8 *addr;
2470 union wpa_event_data data;
2471
2472 if (tb[NL80211_ATTR_MAC] == NULL)
2473 return;
2474 addr = nla_data(tb[NL80211_ATTR_MAC]);
2475 wpa_printf(MSG_DEBUG, "nl80211: Delete station " MACSTR,
2476 MAC2STR(addr));
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07002477
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002478 if (is_ap_interface(drv->nlmode) && drv->device_ap_sme) {
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07002479 drv_event_disassoc(drv->ctx, addr);
2480 return;
2481 }
2482
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002483 if (drv->nlmode != NL80211_IFTYPE_ADHOC)
2484 return;
2485
2486 os_memset(&data, 0, sizeof(data));
2487 os_memcpy(data.ibss_peer_lost.peer, addr, ETH_ALEN);
2488 wpa_supplicant_event(drv->ctx, EVENT_IBSS_PEER_LOST, &data);
2489}
2490
2491
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002492static void nl80211_rekey_offload_event(struct wpa_driver_nl80211_data *drv,
2493 struct nlattr **tb)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002494{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002495 struct nlattr *rekey_info[NUM_NL80211_REKEY_DATA];
2496 static struct nla_policy rekey_policy[NUM_NL80211_REKEY_DATA] = {
2497 [NL80211_REKEY_DATA_KEK] = {
2498 .minlen = NL80211_KEK_LEN,
2499 .maxlen = NL80211_KEK_LEN,
2500 },
2501 [NL80211_REKEY_DATA_KCK] = {
2502 .minlen = NL80211_KCK_LEN,
2503 .maxlen = NL80211_KCK_LEN,
2504 },
2505 [NL80211_REKEY_DATA_REPLAY_CTR] = {
2506 .minlen = NL80211_REPLAY_CTR_LEN,
2507 .maxlen = NL80211_REPLAY_CTR_LEN,
2508 },
2509 };
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002510 union wpa_event_data data;
2511
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002512 if (!tb[NL80211_ATTR_MAC])
2513 return;
2514 if (!tb[NL80211_ATTR_REKEY_DATA])
2515 return;
2516 if (nla_parse_nested(rekey_info, MAX_NL80211_REKEY_DATA,
2517 tb[NL80211_ATTR_REKEY_DATA], rekey_policy))
2518 return;
2519 if (!rekey_info[NL80211_REKEY_DATA_REPLAY_CTR])
2520 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002521
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002522 os_memset(&data, 0, sizeof(data));
2523 data.driver_gtk_rekey.bssid = nla_data(tb[NL80211_ATTR_MAC]);
2524 wpa_printf(MSG_DEBUG, "nl80211: Rekey offload event for BSSID " MACSTR,
2525 MAC2STR(data.driver_gtk_rekey.bssid));
2526 data.driver_gtk_rekey.replay_ctr =
2527 nla_data(rekey_info[NL80211_REKEY_DATA_REPLAY_CTR]);
2528 wpa_hexdump(MSG_DEBUG, "nl80211: Rekey offload - Replay Counter",
2529 data.driver_gtk_rekey.replay_ctr, NL80211_REPLAY_CTR_LEN);
2530 wpa_supplicant_event(drv->ctx, EVENT_DRIVER_GTK_REKEY, &data);
2531}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002532
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002533
2534static void nl80211_pmksa_candidate_event(struct wpa_driver_nl80211_data *drv,
2535 struct nlattr **tb)
2536{
2537 struct nlattr *cand[NUM_NL80211_PMKSA_CANDIDATE];
2538 static struct nla_policy cand_policy[NUM_NL80211_PMKSA_CANDIDATE] = {
2539 [NL80211_PMKSA_CANDIDATE_INDEX] = { .type = NLA_U32 },
2540 [NL80211_PMKSA_CANDIDATE_BSSID] = {
2541 .minlen = ETH_ALEN,
2542 .maxlen = ETH_ALEN,
2543 },
2544 [NL80211_PMKSA_CANDIDATE_PREAUTH] = { .type = NLA_FLAG },
2545 };
2546 union wpa_event_data data;
2547
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002548 wpa_printf(MSG_DEBUG, "nl80211: PMKSA candidate event");
2549
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002550 if (!tb[NL80211_ATTR_PMKSA_CANDIDATE])
2551 return;
2552 if (nla_parse_nested(cand, MAX_NL80211_PMKSA_CANDIDATE,
2553 tb[NL80211_ATTR_PMKSA_CANDIDATE], cand_policy))
2554 return;
2555 if (!cand[NL80211_PMKSA_CANDIDATE_INDEX] ||
2556 !cand[NL80211_PMKSA_CANDIDATE_BSSID])
2557 return;
2558
2559 os_memset(&data, 0, sizeof(data));
2560 os_memcpy(data.pmkid_candidate.bssid,
2561 nla_data(cand[NL80211_PMKSA_CANDIDATE_BSSID]), ETH_ALEN);
2562 data.pmkid_candidate.index =
2563 nla_get_u32(cand[NL80211_PMKSA_CANDIDATE_INDEX]);
2564 data.pmkid_candidate.preauth =
2565 cand[NL80211_PMKSA_CANDIDATE_PREAUTH] != NULL;
2566 wpa_supplicant_event(drv->ctx, EVENT_PMKID_CANDIDATE, &data);
2567}
2568
2569
2570static void nl80211_client_probe_event(struct wpa_driver_nl80211_data *drv,
2571 struct nlattr **tb)
2572{
2573 union wpa_event_data data;
2574
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002575 wpa_printf(MSG_DEBUG, "nl80211: Probe client event");
2576
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002577 if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_ACK])
2578 return;
2579
2580 os_memset(&data, 0, sizeof(data));
2581 os_memcpy(data.client_poll.addr,
2582 nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
2583
2584 wpa_supplicant_event(drv->ctx, EVENT_DRIVER_CLIENT_POLL_OK, &data);
2585}
2586
2587
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002588static void nl80211_tdls_oper_event(struct wpa_driver_nl80211_data *drv,
2589 struct nlattr **tb)
2590{
2591 union wpa_event_data data;
2592
2593 wpa_printf(MSG_DEBUG, "nl80211: TDLS operation event");
2594
2595 if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_TDLS_OPERATION])
2596 return;
2597
2598 os_memset(&data, 0, sizeof(data));
2599 os_memcpy(data.tdls.peer, nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
2600 switch (nla_get_u8(tb[NL80211_ATTR_TDLS_OPERATION])) {
2601 case NL80211_TDLS_SETUP:
2602 wpa_printf(MSG_DEBUG, "nl80211: TDLS setup request for peer "
2603 MACSTR, MAC2STR(data.tdls.peer));
2604 data.tdls.oper = TDLS_REQUEST_SETUP;
2605 break;
2606 case NL80211_TDLS_TEARDOWN:
2607 wpa_printf(MSG_DEBUG, "nl80211: TDLS teardown request for peer "
2608 MACSTR, MAC2STR(data.tdls.peer));
2609 data.tdls.oper = TDLS_REQUEST_TEARDOWN;
2610 break;
2611 default:
2612 wpa_printf(MSG_DEBUG, "nl80211: Unsupported TDLS operatione "
2613 "event");
2614 return;
2615 }
2616 if (tb[NL80211_ATTR_REASON_CODE]) {
2617 data.tdls.reason_code =
2618 nla_get_u16(tb[NL80211_ATTR_REASON_CODE]);
2619 }
2620
2621 wpa_supplicant_event(drv->ctx, EVENT_TDLS, &data);
2622}
2623
2624
Dmitry Shmidt5393a0f2013-08-08 11:23:34 -07002625static void nl80211_stop_ap(struct wpa_driver_nl80211_data *drv,
2626 struct nlattr **tb)
2627{
2628 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_UNAVAILABLE, NULL);
2629}
2630
2631
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002632static void nl80211_connect_failed_event(struct wpa_driver_nl80211_data *drv,
2633 struct nlattr **tb)
2634{
2635 union wpa_event_data data;
2636 u32 reason;
2637
2638 wpa_printf(MSG_DEBUG, "nl80211: Connect failed event");
2639
2640 if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_CONN_FAILED_REASON])
2641 return;
2642
2643 os_memset(&data, 0, sizeof(data));
2644 os_memcpy(data.connect_failed_reason.addr,
2645 nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
2646
2647 reason = nla_get_u32(tb[NL80211_ATTR_CONN_FAILED_REASON]);
2648 switch (reason) {
2649 case NL80211_CONN_FAIL_MAX_CLIENTS:
2650 wpa_printf(MSG_DEBUG, "nl80211: Max client reached");
2651 data.connect_failed_reason.code = MAX_CLIENT_REACHED;
2652 break;
2653 case NL80211_CONN_FAIL_BLOCKED_CLIENT:
2654 wpa_printf(MSG_DEBUG, "nl80211: Blocked client " MACSTR
2655 " tried to connect",
2656 MAC2STR(data.connect_failed_reason.addr));
2657 data.connect_failed_reason.code = BLOCKED_CLIENT;
2658 break;
2659 default:
2660 wpa_printf(MSG_DEBUG, "nl8021l: Unknown connect failed reason "
2661 "%u", reason);
2662 return;
2663 }
2664
2665 wpa_supplicant_event(drv->ctx, EVENT_CONNECT_FAILED_REASON, &data);
2666}
2667
2668
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002669static void nl80211_radar_event(struct wpa_driver_nl80211_data *drv,
2670 struct nlattr **tb)
2671{
2672 union wpa_event_data data;
2673 enum nl80211_radar_event event_type;
2674
2675 if (!tb[NL80211_ATTR_WIPHY_FREQ] || !tb[NL80211_ATTR_RADAR_EVENT])
2676 return;
2677
2678 os_memset(&data, 0, sizeof(data));
Dmitry Shmidt051af732013-10-22 13:52:46 -07002679 data.dfs_event.freq = nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]);
2680 event_type = nla_get_u32(tb[NL80211_ATTR_RADAR_EVENT]);
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002681
Dmitry Shmidt051af732013-10-22 13:52:46 -07002682 /* Check HT params */
2683 if (tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
2684 data.dfs_event.ht_enabled = 1;
2685 data.dfs_event.chan_offset = 0;
2686
2687 switch (nla_get_u32(tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE])) {
2688 case NL80211_CHAN_NO_HT:
2689 data.dfs_event.ht_enabled = 0;
2690 break;
2691 case NL80211_CHAN_HT20:
2692 break;
2693 case NL80211_CHAN_HT40PLUS:
2694 data.dfs_event.chan_offset = 1;
2695 break;
2696 case NL80211_CHAN_HT40MINUS:
2697 data.dfs_event.chan_offset = -1;
2698 break;
2699 }
2700 }
2701
2702 /* Get VHT params */
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002703 if (tb[NL80211_ATTR_CHANNEL_WIDTH])
2704 data.dfs_event.chan_width =
2705 convert2width(nla_get_u32(
2706 tb[NL80211_ATTR_CHANNEL_WIDTH]));
2707 if (tb[NL80211_ATTR_CENTER_FREQ1])
2708 data.dfs_event.cf1 = nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002709 if (tb[NL80211_ATTR_CENTER_FREQ2])
2710 data.dfs_event.cf2 = nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]);
2711
2712 wpa_printf(MSG_DEBUG, "nl80211: DFS event on freq %d MHz, ht: %d, offset: %d, width: %d, cf1: %dMHz, cf2: %dMHz",
2713 data.dfs_event.freq, data.dfs_event.ht_enabled,
2714 data.dfs_event.chan_offset, data.dfs_event.chan_width,
2715 data.dfs_event.cf1, data.dfs_event.cf2);
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002716
2717 switch (event_type) {
2718 case NL80211_RADAR_DETECTED:
2719 wpa_supplicant_event(drv->ctx, EVENT_DFS_RADAR_DETECTED, &data);
2720 break;
2721 case NL80211_RADAR_CAC_FINISHED:
2722 wpa_supplicant_event(drv->ctx, EVENT_DFS_CAC_FINISHED, &data);
2723 break;
2724 case NL80211_RADAR_CAC_ABORTED:
2725 wpa_supplicant_event(drv->ctx, EVENT_DFS_CAC_ABORTED, &data);
2726 break;
2727 case NL80211_RADAR_NOP_FINISHED:
2728 wpa_supplicant_event(drv->ctx, EVENT_DFS_NOP_FINISHED, &data);
2729 break;
2730 default:
2731 wpa_printf(MSG_DEBUG, "nl80211: Unknown radar event %d "
2732 "received", event_type);
2733 break;
2734 }
2735}
2736
2737
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002738static void nl80211_spurious_frame(struct i802_bss *bss, struct nlattr **tb,
2739 int wds)
2740{
2741 struct wpa_driver_nl80211_data *drv = bss->drv;
2742 union wpa_event_data event;
2743
2744 if (!tb[NL80211_ATTR_MAC])
2745 return;
2746
2747 os_memset(&event, 0, sizeof(event));
2748 event.rx_from_unknown.bssid = bss->addr;
2749 event.rx_from_unknown.addr = nla_data(tb[NL80211_ATTR_MAC]);
2750 event.rx_from_unknown.wds = wds;
2751
2752 wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event);
2753}
2754
2755
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002756static void qca_nl80211_avoid_freq(struct wpa_driver_nl80211_data *drv,
2757 const u8 *data, size_t len)
2758{
2759 u32 i, count;
2760 union wpa_event_data event;
2761 struct wpa_freq_range *range = NULL;
2762 const struct qca_avoid_freq_list *freq_range;
2763
2764 freq_range = (const struct qca_avoid_freq_list *) data;
2765 if (len < sizeof(freq_range->count))
2766 return;
2767
2768 count = freq_range->count;
2769 if (len < sizeof(freq_range->count) +
2770 count * sizeof(struct qca_avoid_freq_range)) {
2771 wpa_printf(MSG_DEBUG, "nl80211: Ignored too short avoid frequency list (len=%u)",
2772 (unsigned int) len);
2773 return;
2774 }
2775
2776 if (count > 0) {
2777 range = os_calloc(count, sizeof(struct wpa_freq_range));
2778 if (range == NULL)
2779 return;
2780 }
2781
2782 os_memset(&event, 0, sizeof(event));
2783 for (i = 0; i < count; i++) {
2784 unsigned int idx = event.freq_range.num;
2785 range[idx].min = freq_range->range[i].start_freq;
2786 range[idx].max = freq_range->range[i].end_freq;
2787 wpa_printf(MSG_DEBUG, "nl80211: Avoid frequency range: %u-%u",
2788 range[idx].min, range[idx].max);
2789 if (range[idx].min > range[idx].max) {
2790 wpa_printf(MSG_DEBUG, "nl80211: Ignore invalid frequency range");
2791 continue;
2792 }
2793 event.freq_range.num++;
2794 }
2795 event.freq_range.range = range;
2796
2797 wpa_supplicant_event(drv->ctx, EVENT_AVOID_FREQUENCIES, &event);
2798
2799 os_free(range);
2800}
2801
2802
2803static void nl80211_vendor_event_qca(struct wpa_driver_nl80211_data *drv,
2804 u32 subcmd, u8 *data, size_t len)
2805{
2806 switch (subcmd) {
2807 case QCA_NL80211_VENDOR_SUBCMD_AVOID_FREQUENCY:
2808 qca_nl80211_avoid_freq(drv, data, len);
2809 break;
2810 default:
2811 wpa_printf(MSG_DEBUG,
2812 "nl80211: Ignore unsupported QCA vendor event %u",
2813 subcmd);
2814 break;
2815 }
2816}
2817
2818
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002819static void nl80211_vendor_event(struct wpa_driver_nl80211_data *drv,
2820 struct nlattr **tb)
2821{
2822 u32 vendor_id, subcmd, wiphy = 0;
2823 int wiphy_idx;
2824 u8 *data = NULL;
2825 size_t len = 0;
2826
2827 if (!tb[NL80211_ATTR_VENDOR_ID] ||
2828 !tb[NL80211_ATTR_VENDOR_SUBCMD])
2829 return;
2830
2831 vendor_id = nla_get_u32(tb[NL80211_ATTR_VENDOR_ID]);
2832 subcmd = nla_get_u32(tb[NL80211_ATTR_VENDOR_SUBCMD]);
2833
2834 if (tb[NL80211_ATTR_WIPHY])
2835 wiphy = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
2836
2837 wpa_printf(MSG_DEBUG, "nl80211: Vendor event: wiphy=%u vendor_id=0x%x subcmd=%u",
2838 wiphy, vendor_id, subcmd);
2839
2840 if (tb[NL80211_ATTR_VENDOR_DATA]) {
2841 data = nla_data(tb[NL80211_ATTR_VENDOR_DATA]);
2842 len = nla_len(tb[NL80211_ATTR_VENDOR_DATA]);
2843 wpa_hexdump(MSG_MSGDUMP, "nl80211: Vendor data", data, len);
2844 }
2845
2846 wiphy_idx = nl80211_get_wiphy_index(drv->first_bss);
2847 if (wiphy_idx >= 0 && wiphy_idx != (int) wiphy) {
2848 wpa_printf(MSG_DEBUG, "nl80211: Ignore vendor event for foreign wiphy %u (own: %d)",
2849 wiphy, wiphy_idx);
2850 return;
2851 }
2852
2853 switch (vendor_id) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002854 case OUI_QCA:
2855 nl80211_vendor_event_qca(drv, subcmd, data, len);
2856 break;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002857 default:
2858 wpa_printf(MSG_DEBUG, "nl80211: Ignore unsupported vendor event");
2859 break;
2860 }
2861}
2862
2863
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002864static void do_process_drv_event(struct i802_bss *bss, int cmd,
2865 struct nlattr **tb)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002866{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002867 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002868 union wpa_event_data data;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002869
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002870 wpa_printf(MSG_DEBUG, "nl80211: Drv Event %d (%s) received for %s",
2871 cmd, nl80211_command_to_string(cmd), bss->ifname);
2872
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002873 if (drv->ap_scan_as_station != NL80211_IFTYPE_UNSPECIFIED &&
2874 (cmd == NL80211_CMD_NEW_SCAN_RESULTS ||
2875 cmd == NL80211_CMD_SCAN_ABORTED)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002876 wpa_driver_nl80211_set_mode(drv->first_bss,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002877 drv->ap_scan_as_station);
2878 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002879 }
2880
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002881 switch (cmd) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002882 case NL80211_CMD_TRIGGER_SCAN:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002883 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Scan trigger");
Dmitry Shmidt56052862013-10-04 10:23:25 -07002884 drv->scan_state = SCAN_STARTED;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002885 wpa_supplicant_event(drv->ctx, EVENT_SCAN_STARTED, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002886 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002887 case NL80211_CMD_START_SCHED_SCAN:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002888 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Sched scan started");
Dmitry Shmidt56052862013-10-04 10:23:25 -07002889 drv->scan_state = SCHED_SCAN_STARTED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002890 break;
2891 case NL80211_CMD_SCHED_SCAN_STOPPED:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002892 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Sched scan stopped");
Dmitry Shmidt56052862013-10-04 10:23:25 -07002893 drv->scan_state = SCHED_SCAN_STOPPED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002894 wpa_supplicant_event(drv->ctx, EVENT_SCHED_SCAN_STOPPED, NULL);
2895 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002896 case NL80211_CMD_NEW_SCAN_RESULTS:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002897 wpa_dbg(drv->ctx, MSG_DEBUG,
2898 "nl80211: New scan results available");
Dmitry Shmidt56052862013-10-04 10:23:25 -07002899 drv->scan_state = SCAN_COMPLETED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002900 drv->scan_complete_events = 1;
2901 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
2902 drv->ctx);
2903 send_scan_event(drv, 0, tb);
2904 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002905 case NL80211_CMD_SCHED_SCAN_RESULTS:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002906 wpa_dbg(drv->ctx, MSG_DEBUG,
2907 "nl80211: New sched scan results available");
Dmitry Shmidt56052862013-10-04 10:23:25 -07002908 drv->scan_state = SCHED_SCAN_RESULTS;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002909 send_scan_event(drv, 0, tb);
2910 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002911 case NL80211_CMD_SCAN_ABORTED:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002912 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Scan aborted");
Dmitry Shmidt56052862013-10-04 10:23:25 -07002913 drv->scan_state = SCAN_ABORTED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002914 /*
2915 * Need to indicate that scan results are available in order
2916 * not to make wpa_supplicant stop its scanning.
2917 */
2918 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
2919 drv->ctx);
2920 send_scan_event(drv, 1, tb);
2921 break;
2922 case NL80211_CMD_AUTHENTICATE:
2923 case NL80211_CMD_ASSOCIATE:
2924 case NL80211_CMD_DEAUTHENTICATE:
2925 case NL80211_CMD_DISASSOCIATE:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002926 case NL80211_CMD_FRAME_TX_STATUS:
2927 case NL80211_CMD_UNPROT_DEAUTHENTICATE:
2928 case NL80211_CMD_UNPROT_DISASSOCIATE:
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002929 mlme_event(bss, cmd, tb[NL80211_ATTR_FRAME],
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002930 tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT],
2931 tb[NL80211_ATTR_WIPHY_FREQ], tb[NL80211_ATTR_ACK],
Dmitry Shmidt04949592012-07-19 12:16:46 -07002932 tb[NL80211_ATTR_COOKIE],
2933 tb[NL80211_ATTR_RX_SIGNAL_DBM]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002934 break;
2935 case NL80211_CMD_CONNECT:
2936 case NL80211_CMD_ROAM:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002937 mlme_event_connect(drv, cmd,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002938 tb[NL80211_ATTR_STATUS_CODE],
2939 tb[NL80211_ATTR_MAC],
2940 tb[NL80211_ATTR_REQ_IE],
2941 tb[NL80211_ATTR_RESP_IE]);
2942 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002943 case NL80211_CMD_CH_SWITCH_NOTIFY:
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002944 mlme_event_ch_switch(drv,
2945 tb[NL80211_ATTR_IFINDEX],
2946 tb[NL80211_ATTR_WIPHY_FREQ],
2947 tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE],
2948 tb[NL80211_ATTR_CHANNEL_WIDTH],
2949 tb[NL80211_ATTR_CENTER_FREQ1],
2950 tb[NL80211_ATTR_CENTER_FREQ2]);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002951 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002952 case NL80211_CMD_DISCONNECT:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002953 mlme_event_disconnect(drv, tb[NL80211_ATTR_REASON_CODE],
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002954 tb[NL80211_ATTR_MAC],
2955 tb[NL80211_ATTR_DISCONNECTED_BY_AP]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002956 break;
2957 case NL80211_CMD_MICHAEL_MIC_FAILURE:
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002958 mlme_event_michael_mic_failure(bss, tb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002959 break;
2960 case NL80211_CMD_JOIN_IBSS:
2961 mlme_event_join_ibss(drv, tb);
2962 break;
2963 case NL80211_CMD_REMAIN_ON_CHANNEL:
2964 mlme_event_remain_on_channel(drv, 0, tb);
2965 break;
2966 case NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL:
2967 mlme_event_remain_on_channel(drv, 1, tb);
2968 break;
2969 case NL80211_CMD_NOTIFY_CQM:
2970 nl80211_cqm_event(drv, tb);
2971 break;
2972 case NL80211_CMD_REG_CHANGE:
2973 wpa_printf(MSG_DEBUG, "nl80211: Regulatory domain change");
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002974 if (tb[NL80211_ATTR_REG_INITIATOR] == NULL)
2975 break;
2976 os_memset(&data, 0, sizeof(data));
2977 switch (nla_get_u8(tb[NL80211_ATTR_REG_INITIATOR])) {
2978 case NL80211_REGDOM_SET_BY_CORE:
2979 data.channel_list_changed.initiator =
2980 REGDOM_SET_BY_CORE;
2981 break;
2982 case NL80211_REGDOM_SET_BY_USER:
2983 data.channel_list_changed.initiator =
2984 REGDOM_SET_BY_USER;
2985 break;
2986 case NL80211_REGDOM_SET_BY_DRIVER:
2987 data.channel_list_changed.initiator =
2988 REGDOM_SET_BY_DRIVER;
2989 break;
2990 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
2991 data.channel_list_changed.initiator =
2992 REGDOM_SET_BY_COUNTRY_IE;
2993 break;
2994 default:
2995 wpa_printf(MSG_DEBUG, "nl80211: Unknown reg change initiator %d received",
2996 nla_get_u8(tb[NL80211_ATTR_REG_INITIATOR]));
2997 break;
2998 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002999 wpa_supplicant_event(drv->ctx, EVENT_CHANNEL_LIST_CHANGED,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003000 &data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003001 break;
3002 case NL80211_CMD_REG_BEACON_HINT:
3003 wpa_printf(MSG_DEBUG, "nl80211: Regulatory beacon hint");
Dmitry Shmidt97672262014-02-03 13:02:54 -08003004 os_memset(&data, 0, sizeof(data));
3005 data.channel_list_changed.initiator = REGDOM_BEACON_HINT;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003006 wpa_supplicant_event(drv->ctx, EVENT_CHANNEL_LIST_CHANGED,
Dmitry Shmidt97672262014-02-03 13:02:54 -08003007 &data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003008 break;
3009 case NL80211_CMD_NEW_STATION:
3010 nl80211_new_station_event(drv, tb);
3011 break;
3012 case NL80211_CMD_DEL_STATION:
3013 nl80211_del_station_event(drv, tb);
3014 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003015 case NL80211_CMD_SET_REKEY_OFFLOAD:
3016 nl80211_rekey_offload_event(drv, tb);
3017 break;
3018 case NL80211_CMD_PMKSA_CANDIDATE:
3019 nl80211_pmksa_candidate_event(drv, tb);
3020 break;
3021 case NL80211_CMD_PROBE_CLIENT:
3022 nl80211_client_probe_event(drv, tb);
3023 break;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003024 case NL80211_CMD_TDLS_OPER:
3025 nl80211_tdls_oper_event(drv, tb);
3026 break;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003027 case NL80211_CMD_CONN_FAILED:
3028 nl80211_connect_failed_event(drv, tb);
3029 break;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003030 case NL80211_CMD_FT_EVENT:
3031 mlme_event_ft_event(drv, tb);
3032 break;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07003033 case NL80211_CMD_RADAR_DETECT:
3034 nl80211_radar_event(drv, tb);
3035 break;
Dmitry Shmidt5393a0f2013-08-08 11:23:34 -07003036 case NL80211_CMD_STOP_AP:
3037 nl80211_stop_ap(drv, tb);
3038 break;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003039 case NL80211_CMD_VENDOR:
3040 nl80211_vendor_event(drv, tb);
3041 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003042 default:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003043 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Ignored unknown event "
3044 "(cmd=%d)", cmd);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003045 break;
3046 }
3047}
3048
3049
3050static int process_drv_event(struct nl_msg *msg, void *arg)
3051{
3052 struct wpa_driver_nl80211_data *drv = arg;
3053 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3054 struct nlattr *tb[NL80211_ATTR_MAX + 1];
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003055 struct i802_bss *bss;
3056 int ifidx = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003057
3058 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3059 genlmsg_attrlen(gnlh, 0), NULL);
3060
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003061 if (tb[NL80211_ATTR_IFINDEX]) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003062 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
3063
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003064 for (bss = drv->first_bss; bss; bss = bss->next)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003065 if (ifidx == -1 || ifidx == bss->ifindex) {
3066 do_process_drv_event(bss, gnlh->cmd, tb);
3067 return NL_SKIP;
3068 }
3069 wpa_printf(MSG_DEBUG,
3070 "nl80211: Ignored event (cmd=%d) for foreign interface (ifindex %d)",
3071 gnlh->cmd, ifidx);
3072 } else if (tb[NL80211_ATTR_WDEV]) {
3073 u64 wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
3074 wpa_printf(MSG_DEBUG, "nl80211: Process event on P2P device");
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003075 for (bss = drv->first_bss; bss; bss = bss->next) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003076 if (bss->wdev_id_set && wdev_id == bss->wdev_id) {
3077 do_process_drv_event(bss, gnlh->cmd, tb);
3078 return NL_SKIP;
3079 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003080 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003081 wpa_printf(MSG_DEBUG,
3082 "nl80211: Ignored event (cmd=%d) for foreign interface (wdev 0x%llx)",
3083 gnlh->cmd, (long long unsigned int) wdev_id);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003084 }
3085
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003086 return NL_SKIP;
3087}
3088
3089
3090static int process_global_event(struct nl_msg *msg, void *arg)
3091{
3092 struct nl80211_global *global = arg;
3093 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3094 struct nlattr *tb[NL80211_ATTR_MAX + 1];
Dmitry Shmidt04949592012-07-19 12:16:46 -07003095 struct wpa_driver_nl80211_data *drv, *tmp;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003096 int ifidx = -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003097 struct i802_bss *bss;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003098 u64 wdev_id = 0;
3099 int wdev_id_set = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003100
3101 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3102 genlmsg_attrlen(gnlh, 0), NULL);
3103
3104 if (tb[NL80211_ATTR_IFINDEX])
3105 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003106 else if (tb[NL80211_ATTR_WDEV]) {
3107 wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
3108 wdev_id_set = 1;
3109 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003110
Dmitry Shmidt04949592012-07-19 12:16:46 -07003111 dl_list_for_each_safe(drv, tmp, &global->interfaces,
3112 struct wpa_driver_nl80211_data, list) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003113 for (bss = drv->first_bss; bss; bss = bss->next) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003114 if ((ifidx == -1 && !wdev_id_set) ||
3115 ifidx == bss->ifindex ||
3116 (wdev_id_set && bss->wdev_id_set &&
3117 wdev_id == bss->wdev_id)) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003118 do_process_drv_event(bss, gnlh->cmd, tb);
3119 return NL_SKIP;
3120 }
3121 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003122 }
3123
3124 return NL_SKIP;
3125}
3126
3127
3128static int process_bss_event(struct nl_msg *msg, void *arg)
3129{
3130 struct i802_bss *bss = arg;
3131 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3132 struct nlattr *tb[NL80211_ATTR_MAX + 1];
3133
3134 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3135 genlmsg_attrlen(gnlh, 0), NULL);
3136
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003137 wpa_printf(MSG_DEBUG, "nl80211: BSS Event %d (%s) received for %s",
3138 gnlh->cmd, nl80211_command_to_string(gnlh->cmd),
3139 bss->ifname);
3140
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003141 switch (gnlh->cmd) {
3142 case NL80211_CMD_FRAME:
3143 case NL80211_CMD_FRAME_TX_STATUS:
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003144 mlme_event(bss, gnlh->cmd, tb[NL80211_ATTR_FRAME],
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003145 tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT],
3146 tb[NL80211_ATTR_WIPHY_FREQ], tb[NL80211_ATTR_ACK],
Dmitry Shmidt04949592012-07-19 12:16:46 -07003147 tb[NL80211_ATTR_COOKIE],
3148 tb[NL80211_ATTR_RX_SIGNAL_DBM]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003149 break;
3150 case NL80211_CMD_UNEXPECTED_FRAME:
3151 nl80211_spurious_frame(bss, tb, 0);
3152 break;
3153 case NL80211_CMD_UNEXPECTED_4ADDR_FRAME:
3154 nl80211_spurious_frame(bss, tb, 1);
3155 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003156 default:
3157 wpa_printf(MSG_DEBUG, "nl80211: Ignored unknown event "
3158 "(cmd=%d)", gnlh->cmd);
3159 break;
3160 }
3161
3162 return NL_SKIP;
3163}
3164
3165
3166static void wpa_driver_nl80211_event_receive(int sock, void *eloop_ctx,
3167 void *handle)
3168{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003169 struct nl_cb *cb = eloop_ctx;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003170 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003171
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003172 wpa_printf(MSG_MSGDUMP, "nl80211: Event message available");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003173
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003174 res = nl_recvmsgs(handle, cb);
3175 if (res) {
3176 wpa_printf(MSG_INFO, "nl80211: %s->nl_recvmsgs failed: %d",
3177 __func__, res);
3178 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003179}
3180
3181
3182/**
3183 * wpa_driver_nl80211_set_country - ask nl80211 to set the regulatory domain
3184 * @priv: driver_nl80211 private data
3185 * @alpha2_arg: country to which to switch to
3186 * Returns: 0 on success, -1 on failure
3187 *
3188 * This asks nl80211 to set the regulatory domain for given
3189 * country ISO / IEC alpha2.
3190 */
3191static int wpa_driver_nl80211_set_country(void *priv, const char *alpha2_arg)
3192{
3193 struct i802_bss *bss = priv;
3194 struct wpa_driver_nl80211_data *drv = bss->drv;
3195 char alpha2[3];
3196 struct nl_msg *msg;
3197
3198 msg = nlmsg_alloc();
3199 if (!msg)
3200 return -ENOMEM;
3201
3202 alpha2[0] = alpha2_arg[0];
3203 alpha2[1] = alpha2_arg[1];
3204 alpha2[2] = '\0';
3205
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003206 nl80211_cmd(drv, msg, 0, NL80211_CMD_REQ_SET_REG);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003207
3208 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, alpha2);
3209 if (send_and_recv_msgs(drv, msg, NULL, NULL))
3210 return -EINVAL;
3211 return 0;
3212nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003213 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003214 return -EINVAL;
3215}
3216
3217
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003218static int nl80211_get_country(struct nl_msg *msg, void *arg)
3219{
3220 char *alpha2 = arg;
3221 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
3222 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3223
3224 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3225 genlmsg_attrlen(gnlh, 0), NULL);
3226 if (!tb_msg[NL80211_ATTR_REG_ALPHA2]) {
3227 wpa_printf(MSG_DEBUG, "nl80211: No country information available");
3228 return NL_SKIP;
3229 }
3230 os_strlcpy(alpha2, nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]), 3);
3231 return NL_SKIP;
3232}
3233
3234
3235static int wpa_driver_nl80211_get_country(void *priv, char *alpha2)
3236{
3237 struct i802_bss *bss = priv;
3238 struct wpa_driver_nl80211_data *drv = bss->drv;
3239 struct nl_msg *msg;
3240 int ret;
3241
3242 msg = nlmsg_alloc();
3243 if (!msg)
3244 return -ENOMEM;
3245
3246 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG);
3247 alpha2[0] = '\0';
3248 ret = send_and_recv_msgs(drv, msg, nl80211_get_country, alpha2);
3249 if (!alpha2[0])
3250 ret = -1;
3251
3252 return ret;
3253}
3254
3255
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003256static int protocol_feature_handler(struct nl_msg *msg, void *arg)
3257{
3258 u32 *feat = arg;
3259 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
3260 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3261
3262 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3263 genlmsg_attrlen(gnlh, 0), NULL);
3264
3265 if (tb_msg[NL80211_ATTR_PROTOCOL_FEATURES])
3266 *feat = nla_get_u32(tb_msg[NL80211_ATTR_PROTOCOL_FEATURES]);
3267
3268 return NL_SKIP;
3269}
3270
3271
3272static u32 get_nl80211_protocol_features(struct wpa_driver_nl80211_data *drv)
3273{
3274 u32 feat = 0;
3275 struct nl_msg *msg;
3276
3277 msg = nlmsg_alloc();
3278 if (!msg)
3279 goto nla_put_failure;
3280
3281 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_PROTOCOL_FEATURES);
3282 if (send_and_recv_msgs(drv, msg, protocol_feature_handler, &feat) == 0)
3283 return feat;
3284
3285 msg = NULL;
3286nla_put_failure:
3287 nlmsg_free(msg);
3288 return 0;
3289}
3290
3291
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003292struct wiphy_info_data {
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003293 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003294 struct wpa_driver_capa *capa;
3295
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003296 unsigned int num_multichan_concurrent;
3297
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003298 unsigned int error:1;
3299 unsigned int device_ap_sme:1;
3300 unsigned int poll_command_supported:1;
3301 unsigned int data_tx_status:1;
3302 unsigned int monitor_supported:1;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003303 unsigned int auth_supported:1;
3304 unsigned int connect_supported:1;
3305 unsigned int p2p_go_supported:1;
3306 unsigned int p2p_client_supported:1;
3307 unsigned int p2p_concurrent:1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003308 unsigned int channel_switch_supported:1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003309 unsigned int set_qos_map_supported:1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003310};
3311
3312
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003313static unsigned int probe_resp_offload_support(int supp_protocols)
3314{
3315 unsigned int prot = 0;
3316
3317 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS)
3318 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS;
3319 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2)
3320 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS2;
3321 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P)
3322 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_P2P;
3323 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_80211U)
3324 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_INTERWORKING;
3325
3326 return prot;
3327}
3328
3329
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003330static void wiphy_info_supported_iftypes(struct wiphy_info_data *info,
3331 struct nlattr *tb)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003332{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003333 struct nlattr *nl_mode;
3334 int i;
3335
3336 if (tb == NULL)
3337 return;
3338
3339 nla_for_each_nested(nl_mode, tb, i) {
3340 switch (nla_type(nl_mode)) {
3341 case NL80211_IFTYPE_AP:
3342 info->capa->flags |= WPA_DRIVER_FLAGS_AP;
3343 break;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003344 case NL80211_IFTYPE_ADHOC:
3345 info->capa->flags |= WPA_DRIVER_FLAGS_IBSS;
3346 break;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003347 case NL80211_IFTYPE_P2P_DEVICE:
3348 info->capa->flags |=
3349 WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE;
3350 break;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003351 case NL80211_IFTYPE_P2P_GO:
3352 info->p2p_go_supported = 1;
3353 break;
3354 case NL80211_IFTYPE_P2P_CLIENT:
3355 info->p2p_client_supported = 1;
3356 break;
3357 case NL80211_IFTYPE_MONITOR:
3358 info->monitor_supported = 1;
3359 break;
3360 }
3361 }
3362}
3363
3364
3365static int wiphy_info_iface_comb_process(struct wiphy_info_data *info,
3366 struct nlattr *nl_combi)
3367{
3368 struct nlattr *tb_comb[NUM_NL80211_IFACE_COMB];
3369 struct nlattr *tb_limit[NUM_NL80211_IFACE_LIMIT];
3370 struct nlattr *nl_limit, *nl_mode;
3371 int err, rem_limit, rem_mode;
3372 int combination_has_p2p = 0, combination_has_mgd = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003373 static struct nla_policy
3374 iface_combination_policy[NUM_NL80211_IFACE_COMB] = {
3375 [NL80211_IFACE_COMB_LIMITS] = { .type = NLA_NESTED },
3376 [NL80211_IFACE_COMB_MAXNUM] = { .type = NLA_U32 },
3377 [NL80211_IFACE_COMB_STA_AP_BI_MATCH] = { .type = NLA_FLAG },
3378 [NL80211_IFACE_COMB_NUM_CHANNELS] = { .type = NLA_U32 },
Dmitry Shmidtea69e842013-05-13 14:52:28 -07003379 [NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS] = { .type = NLA_U32 },
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003380 },
3381 iface_limit_policy[NUM_NL80211_IFACE_LIMIT] = {
3382 [NL80211_IFACE_LIMIT_TYPES] = { .type = NLA_NESTED },
3383 [NL80211_IFACE_LIMIT_MAX] = { .type = NLA_U32 },
3384 };
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003385
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003386 err = nla_parse_nested(tb_comb, MAX_NL80211_IFACE_COMB,
3387 nl_combi, iface_combination_policy);
3388 if (err || !tb_comb[NL80211_IFACE_COMB_LIMITS] ||
3389 !tb_comb[NL80211_IFACE_COMB_MAXNUM] ||
3390 !tb_comb[NL80211_IFACE_COMB_NUM_CHANNELS])
3391 return 0; /* broken combination */
3392
Dmitry Shmidtea69e842013-05-13 14:52:28 -07003393 if (tb_comb[NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS])
3394 info->capa->flags |= WPA_DRIVER_FLAGS_RADAR;
3395
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003396 nla_for_each_nested(nl_limit, tb_comb[NL80211_IFACE_COMB_LIMITS],
3397 rem_limit) {
3398 err = nla_parse_nested(tb_limit, MAX_NL80211_IFACE_LIMIT,
3399 nl_limit, iface_limit_policy);
3400 if (err || !tb_limit[NL80211_IFACE_LIMIT_TYPES])
3401 return 0; /* broken combination */
3402
3403 nla_for_each_nested(nl_mode,
3404 tb_limit[NL80211_IFACE_LIMIT_TYPES],
3405 rem_mode) {
3406 int ift = nla_type(nl_mode);
3407 if (ift == NL80211_IFTYPE_P2P_GO ||
3408 ift == NL80211_IFTYPE_P2P_CLIENT)
3409 combination_has_p2p = 1;
3410 if (ift == NL80211_IFTYPE_STATION)
3411 combination_has_mgd = 1;
3412 }
3413 if (combination_has_p2p && combination_has_mgd)
3414 break;
3415 }
3416
3417 if (combination_has_p2p && combination_has_mgd) {
3418 info->p2p_concurrent = 1;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003419 info->num_multichan_concurrent =
3420 nla_get_u32(tb_comb[NL80211_IFACE_COMB_NUM_CHANNELS]);
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003421 return 1;
3422 }
3423
3424 return 0;
3425}
3426
3427
3428static void wiphy_info_iface_comb(struct wiphy_info_data *info,
3429 struct nlattr *tb)
3430{
3431 struct nlattr *nl_combi;
3432 int rem_combi;
3433
3434 if (tb == NULL)
3435 return;
3436
3437 nla_for_each_nested(nl_combi, tb, rem_combi) {
3438 if (wiphy_info_iface_comb_process(info, nl_combi) > 0)
3439 break;
3440 }
3441}
3442
3443
3444static void wiphy_info_supp_cmds(struct wiphy_info_data *info,
3445 struct nlattr *tb)
3446{
3447 struct nlattr *nl_cmd;
3448 int i;
3449
3450 if (tb == NULL)
3451 return;
3452
3453 nla_for_each_nested(nl_cmd, tb, i) {
3454 switch (nla_get_u32(nl_cmd)) {
3455 case NL80211_CMD_AUTHENTICATE:
3456 info->auth_supported = 1;
3457 break;
3458 case NL80211_CMD_CONNECT:
3459 info->connect_supported = 1;
3460 break;
3461 case NL80211_CMD_START_SCHED_SCAN:
3462 info->capa->sched_scan_supported = 1;
3463 break;
3464 case NL80211_CMD_PROBE_CLIENT:
3465 info->poll_command_supported = 1;
3466 break;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003467 case NL80211_CMD_CHANNEL_SWITCH:
3468 info->channel_switch_supported = 1;
3469 break;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003470 case NL80211_CMD_SET_QOS_MAP:
3471 info->set_qos_map_supported = 1;
3472 break;
3473 }
3474 }
3475}
3476
3477
3478static void wiphy_info_cipher_suites(struct wiphy_info_data *info,
3479 struct nlattr *tb)
3480{
3481 int i, num;
3482 u32 *ciphers;
3483
3484 if (tb == NULL)
3485 return;
3486
3487 num = nla_len(tb) / sizeof(u32);
3488 ciphers = nla_data(tb);
3489 for (i = 0; i < num; i++) {
3490 u32 c = ciphers[i];
3491
3492 wpa_printf(MSG_DEBUG, "nl80211: Supported cipher %02x-%02x-%02x:%d",
3493 c >> 24, (c >> 16) & 0xff,
3494 (c >> 8) & 0xff, c & 0xff);
3495 switch (c) {
3496 case WLAN_CIPHER_SUITE_CCMP_256:
3497 info->capa->enc |= WPA_DRIVER_CAPA_ENC_CCMP_256;
3498 break;
3499 case WLAN_CIPHER_SUITE_GCMP_256:
3500 info->capa->enc |= WPA_DRIVER_CAPA_ENC_GCMP_256;
3501 break;
3502 case WLAN_CIPHER_SUITE_CCMP:
3503 info->capa->enc |= WPA_DRIVER_CAPA_ENC_CCMP;
3504 break;
3505 case WLAN_CIPHER_SUITE_GCMP:
3506 info->capa->enc |= WPA_DRIVER_CAPA_ENC_GCMP;
3507 break;
3508 case WLAN_CIPHER_SUITE_TKIP:
3509 info->capa->enc |= WPA_DRIVER_CAPA_ENC_TKIP;
3510 break;
3511 case WLAN_CIPHER_SUITE_WEP104:
3512 info->capa->enc |= WPA_DRIVER_CAPA_ENC_WEP104;
3513 break;
3514 case WLAN_CIPHER_SUITE_WEP40:
3515 info->capa->enc |= WPA_DRIVER_CAPA_ENC_WEP40;
3516 break;
3517 case WLAN_CIPHER_SUITE_AES_CMAC:
3518 info->capa->enc |= WPA_DRIVER_CAPA_ENC_BIP;
3519 break;
3520 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
3521 info->capa->enc |= WPA_DRIVER_CAPA_ENC_BIP_GMAC_128;
3522 break;
3523 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
3524 info->capa->enc |= WPA_DRIVER_CAPA_ENC_BIP_GMAC_256;
3525 break;
3526 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
3527 info->capa->enc |= WPA_DRIVER_CAPA_ENC_BIP_CMAC_256;
3528 break;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003529 case WLAN_CIPHER_SUITE_NO_GROUP_ADDR:
3530 info->capa->enc |= WPA_DRIVER_CAPA_ENC_GTK_NOT_USED;
3531 break;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003532 }
3533 }
3534}
3535
3536
3537static void wiphy_info_max_roc(struct wpa_driver_capa *capa,
3538 struct nlattr *tb)
3539{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003540 if (tb)
3541 capa->max_remain_on_chan = nla_get_u32(tb);
3542}
3543
3544
3545static void wiphy_info_tdls(struct wpa_driver_capa *capa, struct nlattr *tdls,
3546 struct nlattr *ext_setup)
3547{
3548 if (tdls == NULL)
3549 return;
3550
3551 wpa_printf(MSG_DEBUG, "nl80211: TDLS supported");
3552 capa->flags |= WPA_DRIVER_FLAGS_TDLS_SUPPORT;
3553
3554 if (ext_setup) {
3555 wpa_printf(MSG_DEBUG, "nl80211: TDLS external setup");
3556 capa->flags |= WPA_DRIVER_FLAGS_TDLS_EXTERNAL_SETUP;
3557 }
3558}
3559
3560
3561static void wiphy_info_feature_flags(struct wiphy_info_data *info,
3562 struct nlattr *tb)
3563{
3564 u32 flags;
3565 struct wpa_driver_capa *capa = info->capa;
3566
3567 if (tb == NULL)
3568 return;
3569
3570 flags = nla_get_u32(tb);
3571
3572 if (flags & NL80211_FEATURE_SK_TX_STATUS)
3573 info->data_tx_status = 1;
3574
3575 if (flags & NL80211_FEATURE_INACTIVITY_TIMER)
3576 capa->flags |= WPA_DRIVER_FLAGS_INACTIVITY_TIMER;
3577
3578 if (flags & NL80211_FEATURE_SAE)
3579 capa->flags |= WPA_DRIVER_FLAGS_SAE;
3580
3581 if (flags & NL80211_FEATURE_NEED_OBSS_SCAN)
3582 capa->flags |= WPA_DRIVER_FLAGS_OBSS_SCAN;
3583}
3584
3585
3586static void wiphy_info_probe_resp_offload(struct wpa_driver_capa *capa,
3587 struct nlattr *tb)
3588{
3589 u32 protocols;
3590
3591 if (tb == NULL)
3592 return;
3593
3594 protocols = nla_get_u32(tb);
3595 wpa_printf(MSG_DEBUG, "nl80211: Supports Probe Response offload in AP "
3596 "mode");
3597 capa->flags |= WPA_DRIVER_FLAGS_PROBE_RESP_OFFLOAD;
3598 capa->probe_resp_offloads = probe_resp_offload_support(protocols);
3599}
3600
3601
3602static int wiphy_info_handler(struct nl_msg *msg, void *arg)
3603{
3604 struct nlattr *tb[NL80211_ATTR_MAX + 1];
3605 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3606 struct wiphy_info_data *info = arg;
3607 struct wpa_driver_capa *capa = info->capa;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003608 struct wpa_driver_nl80211_data *drv = info->drv;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003609
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003610 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3611 genlmsg_attrlen(gnlh, 0), NULL);
3612
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003613 if (tb[NL80211_ATTR_WIPHY_NAME])
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003614 os_strlcpy(drv->phyname,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003615 nla_get_string(tb[NL80211_ATTR_WIPHY_NAME]),
3616 sizeof(drv->phyname));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003617 if (tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS])
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003618 capa->max_scan_ssids =
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003619 nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS]);
3620
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003621 if (tb[NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS])
3622 capa->max_sched_scan_ssids =
3623 nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS]);
3624
3625 if (tb[NL80211_ATTR_MAX_MATCH_SETS])
3626 capa->max_match_sets =
3627 nla_get_u8(tb[NL80211_ATTR_MAX_MATCH_SETS]);
3628
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003629 if (tb[NL80211_ATTR_MAC_ACL_MAX])
3630 capa->max_acl_mac_addrs =
3631 nla_get_u8(tb[NL80211_ATTR_MAC_ACL_MAX]);
3632
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003633 wiphy_info_supported_iftypes(info, tb[NL80211_ATTR_SUPPORTED_IFTYPES]);
3634 wiphy_info_iface_comb(info, tb[NL80211_ATTR_INTERFACE_COMBINATIONS]);
3635 wiphy_info_supp_cmds(info, tb[NL80211_ATTR_SUPPORTED_COMMANDS]);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003636 wiphy_info_cipher_suites(info, tb[NL80211_ATTR_CIPHER_SUITES]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003637
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003638 if (tb[NL80211_ATTR_OFFCHANNEL_TX_OK]) {
3639 wpa_printf(MSG_DEBUG, "nl80211: Using driver-based "
3640 "off-channel TX");
3641 capa->flags |= WPA_DRIVER_FLAGS_OFFCHANNEL_TX;
3642 }
3643
3644 if (tb[NL80211_ATTR_ROAM_SUPPORT]) {
3645 wpa_printf(MSG_DEBUG, "nl80211: Using driver-based roaming");
3646 capa->flags |= WPA_DRIVER_FLAGS_BSS_SELECTION;
3647 }
3648
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003649 wiphy_info_max_roc(capa,
3650 tb[NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003651
3652 if (tb[NL80211_ATTR_SUPPORT_AP_UAPSD])
3653 capa->flags |= WPA_DRIVER_FLAGS_AP_UAPSD;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003654
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003655 wiphy_info_tdls(capa, tb[NL80211_ATTR_TDLS_SUPPORT],
3656 tb[NL80211_ATTR_TDLS_EXTERNAL_SETUP]);
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003657
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003658 if (tb[NL80211_ATTR_DEVICE_AP_SME])
3659 info->device_ap_sme = 1;
3660
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003661 wiphy_info_feature_flags(info, tb[NL80211_ATTR_FEATURE_FLAGS]);
3662 wiphy_info_probe_resp_offload(capa,
3663 tb[NL80211_ATTR_PROBE_RESP_OFFLOAD]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003664
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003665 if (tb[NL80211_ATTR_EXT_CAPA] && tb[NL80211_ATTR_EXT_CAPA_MASK] &&
3666 drv->extended_capa == NULL) {
3667 drv->extended_capa =
3668 os_malloc(nla_len(tb[NL80211_ATTR_EXT_CAPA]));
3669 if (drv->extended_capa) {
3670 os_memcpy(drv->extended_capa,
3671 nla_data(tb[NL80211_ATTR_EXT_CAPA]),
3672 nla_len(tb[NL80211_ATTR_EXT_CAPA]));
3673 drv->extended_capa_len =
3674 nla_len(tb[NL80211_ATTR_EXT_CAPA]);
3675 }
3676 drv->extended_capa_mask =
3677 os_malloc(nla_len(tb[NL80211_ATTR_EXT_CAPA]));
3678 if (drv->extended_capa_mask) {
3679 os_memcpy(drv->extended_capa_mask,
3680 nla_data(tb[NL80211_ATTR_EXT_CAPA]),
3681 nla_len(tb[NL80211_ATTR_EXT_CAPA]));
3682 } else {
3683 os_free(drv->extended_capa);
3684 drv->extended_capa = NULL;
3685 drv->extended_capa_len = 0;
3686 }
3687 }
3688
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003689 if (tb[NL80211_ATTR_VENDOR_DATA]) {
3690 struct nlattr *nl;
3691 int rem;
3692
3693 nla_for_each_nested(nl, tb[NL80211_ATTR_VENDOR_DATA], rem) {
3694 struct nl80211_vendor_cmd_info *vinfo;
Dmitry Shmidt18463232014-01-24 12:29:41 -08003695 if (nla_len(nl) != sizeof(*vinfo)) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003696 wpa_printf(MSG_DEBUG, "nl80211: Unexpected vendor data info");
3697 continue;
3698 }
3699 vinfo = nla_data(nl);
3700 wpa_printf(MSG_DEBUG, "nl80211: Supported vendor command: vendor_id=0x%x subcmd=%u",
3701 vinfo->vendor_id, vinfo->subcmd);
3702 }
3703 }
3704
3705 if (tb[NL80211_ATTR_VENDOR_EVENTS]) {
3706 struct nlattr *nl;
3707 int rem;
3708
3709 nla_for_each_nested(nl, tb[NL80211_ATTR_VENDOR_EVENTS], rem) {
3710 struct nl80211_vendor_cmd_info *vinfo;
Dmitry Shmidt18463232014-01-24 12:29:41 -08003711 if (nla_len(nl) != sizeof(*vinfo)) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003712 wpa_printf(MSG_DEBUG, "nl80211: Unexpected vendor data info");
3713 continue;
3714 }
3715 vinfo = nla_data(nl);
3716 wpa_printf(MSG_DEBUG, "nl80211: Supported vendor event: vendor_id=0x%x subcmd=%u",
3717 vinfo->vendor_id, vinfo->subcmd);
3718 }
3719 }
3720
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003721 return NL_SKIP;
3722}
3723
3724
3725static int wpa_driver_nl80211_get_info(struct wpa_driver_nl80211_data *drv,
3726 struct wiphy_info_data *info)
3727{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003728 u32 feat;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003729 struct nl_msg *msg;
3730
3731 os_memset(info, 0, sizeof(*info));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003732 info->capa = &drv->capa;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003733 info->drv = drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003734
3735 msg = nlmsg_alloc();
3736 if (!msg)
3737 return -1;
3738
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003739 feat = get_nl80211_protocol_features(drv);
3740 if (feat & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP)
3741 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_WIPHY);
3742 else
3743 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003744
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003745 NLA_PUT_FLAG(msg, NL80211_ATTR_SPLIT_WIPHY_DUMP);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003746 if (nl80211_set_iface_id(msg, drv->first_bss) < 0)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003747 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003748
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003749 if (send_and_recv_msgs(drv, msg, wiphy_info_handler, info))
3750 return -1;
3751
3752 if (info->auth_supported)
3753 drv->capa.flags |= WPA_DRIVER_FLAGS_SME;
3754 else if (!info->connect_supported) {
3755 wpa_printf(MSG_INFO, "nl80211: Driver does not support "
3756 "authentication/association or connect commands");
3757 info->error = 1;
3758 }
3759
3760 if (info->p2p_go_supported && info->p2p_client_supported)
3761 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CAPABLE;
3762 if (info->p2p_concurrent) {
3763 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
3764 "interface (driver advertised support)");
3765 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
3766 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
3767 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003768 if (info->num_multichan_concurrent > 1) {
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003769 wpa_printf(MSG_DEBUG, "nl80211: Enable multi-channel "
3770 "concurrent (driver advertised support)");
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003771 drv->capa.num_multichan_concurrent =
3772 info->num_multichan_concurrent;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003773 }
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07003774
3775 /* default to 5000 since early versions of mac80211 don't set it */
3776 if (!drv->capa.max_remain_on_chan)
3777 drv->capa.max_remain_on_chan = 5000;
3778
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003779 if (info->channel_switch_supported)
3780 drv->capa.flags |= WPA_DRIVER_FLAGS_AP_CSA;
3781
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003782 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003783nla_put_failure:
3784 nlmsg_free(msg);
3785 return -1;
3786}
3787
3788
3789static int wpa_driver_nl80211_capa(struct wpa_driver_nl80211_data *drv)
3790{
3791 struct wiphy_info_data info;
3792 if (wpa_driver_nl80211_get_info(drv, &info))
3793 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003794
3795 if (info.error)
3796 return -1;
3797
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003798 drv->has_capability = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003799 drv->capa.key_mgmt = WPA_DRIVER_CAPA_KEY_MGMT_WPA |
3800 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
3801 WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
3802 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003803 drv->capa.auth = WPA_DRIVER_AUTH_OPEN |
3804 WPA_DRIVER_AUTH_SHARED |
3805 WPA_DRIVER_AUTH_LEAP;
3806
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003807 drv->capa.flags |= WPA_DRIVER_FLAGS_SANE_ERROR_CODES;
3808 drv->capa.flags |= WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003809 drv->capa.flags |= WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003810
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003811 if (!info.device_ap_sme) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07003812 drv->capa.flags |= WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003813
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003814 /*
3815 * No AP SME is currently assumed to also indicate no AP MLME
3816 * in the driver/firmware.
3817 */
3818 drv->capa.flags |= WPA_DRIVER_FLAGS_AP_MLME;
3819 }
3820
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003821 drv->device_ap_sme = info.device_ap_sme;
3822 drv->poll_command_supported = info.poll_command_supported;
3823 drv->data_tx_status = info.data_tx_status;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003824 if (info.set_qos_map_supported)
3825 drv->capa.flags |= WPA_DRIVER_FLAGS_QOS_MAPPING;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003826
3827 /*
Dmitry Shmidtaa532512012-09-24 10:35:31 -07003828 * If poll command and tx status are supported, mac80211 is new enough
3829 * to have everything we need to not need monitor interfaces.
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003830 */
Dmitry Shmidtaa532512012-09-24 10:35:31 -07003831 drv->use_monitor = !info.poll_command_supported || !info.data_tx_status;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003832
3833 if (drv->device_ap_sme && drv->use_monitor) {
3834 /*
3835 * Non-mac80211 drivers may not support monitor interface.
3836 * Make sure we do not get stuck with incorrect capability here
3837 * by explicitly testing this.
3838 */
3839 if (!info.monitor_supported) {
3840 wpa_printf(MSG_DEBUG, "nl80211: Disable use_monitor "
3841 "with device_ap_sme since no monitor mode "
3842 "support detected");
3843 drv->use_monitor = 0;
3844 }
3845 }
3846
3847 /*
3848 * If we aren't going to use monitor interfaces, but the
3849 * driver doesn't support data TX status, we won't get TX
3850 * status for EAPOL frames.
3851 */
3852 if (!drv->use_monitor && !info.data_tx_status)
3853 drv->capa.flags &= ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003854
3855 return 0;
3856}
3857
3858
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003859#ifdef ANDROID
3860static int android_genl_ctrl_resolve(struct nl_handle *handle,
3861 const char *name)
3862{
3863 /*
3864 * Android ICS has very minimal genl_ctrl_resolve() implementation, so
3865 * need to work around that.
3866 */
3867 struct nl_cache *cache = NULL;
3868 struct genl_family *nl80211 = NULL;
3869 int id = -1;
3870
3871 if (genl_ctrl_alloc_cache(handle, &cache) < 0) {
3872 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic "
3873 "netlink cache");
3874 goto fail;
3875 }
3876
3877 nl80211 = genl_ctrl_search_by_name(cache, name);
3878 if (nl80211 == NULL)
3879 goto fail;
3880
3881 id = genl_family_get_id(nl80211);
3882
3883fail:
3884 if (nl80211)
3885 genl_family_put(nl80211);
3886 if (cache)
3887 nl_cache_free(cache);
3888
3889 return id;
3890}
3891#define genl_ctrl_resolve android_genl_ctrl_resolve
3892#endif /* ANDROID */
3893
3894
3895static int wpa_driver_nl80211_init_nl_global(struct nl80211_global *global)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003896{
3897 int ret;
3898
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003899 global->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
3900 if (global->nl_cb == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003901 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
3902 "callbacks");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003903 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003904 }
3905
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003906 global->nl = nl_create_handle(global->nl_cb, "nl");
3907 if (global->nl == NULL)
3908 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003909
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003910 global->nl80211_id = genl_ctrl_resolve(global->nl, "nl80211");
3911 if (global->nl80211_id < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003912 wpa_printf(MSG_ERROR, "nl80211: 'nl80211' generic netlink not "
3913 "found");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003914 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003915 }
3916
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003917 global->nl_event = nl_create_handle(global->nl_cb, "event");
3918 if (global->nl_event == NULL)
3919 goto err;
3920
3921 ret = nl_get_multicast_id(global, "nl80211", "scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003922 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003923 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003924 if (ret < 0) {
3925 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
3926 "membership for scan events: %d (%s)",
3927 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003928 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003929 }
3930
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003931 ret = nl_get_multicast_id(global, "nl80211", "mlme");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003932 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003933 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003934 if (ret < 0) {
3935 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
3936 "membership for mlme events: %d (%s)",
3937 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003938 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003939 }
3940
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003941 ret = nl_get_multicast_id(global, "nl80211", "regulatory");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003942 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003943 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003944 if (ret < 0) {
3945 wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
3946 "membership for regulatory events: %d (%s)",
3947 ret, strerror(-ret));
3948 /* Continue without regulatory events */
3949 }
3950
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003951 ret = nl_get_multicast_id(global, "nl80211", "vendor");
3952 if (ret >= 0)
3953 ret = nl_socket_add_membership(global->nl_event, ret);
3954 if (ret < 0) {
3955 wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
3956 "membership for vendor events: %d (%s)",
3957 ret, strerror(-ret));
3958 /* Continue without vendor events */
3959 }
3960
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003961 nl_cb_set(global->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
3962 no_seq_check, NULL);
3963 nl_cb_set(global->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
3964 process_global_event, global);
3965
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003966 nl80211_register_eloop_read(&global->nl_event,
3967 wpa_driver_nl80211_event_receive,
3968 global->nl_cb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003969
3970 return 0;
3971
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003972err:
3973 nl_destroy_handles(&global->nl_event);
3974 nl_destroy_handles(&global->nl);
3975 nl_cb_put(global->nl_cb);
3976 global->nl_cb = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003977 return -1;
3978}
3979
3980
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003981static int wpa_driver_nl80211_init_nl(struct wpa_driver_nl80211_data *drv)
3982{
3983 drv->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
3984 if (!drv->nl_cb) {
3985 wpa_printf(MSG_ERROR, "nl80211: Failed to alloc cb struct");
3986 return -1;
3987 }
3988
3989 nl_cb_set(drv->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
3990 no_seq_check, NULL);
3991 nl_cb_set(drv->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
3992 process_drv_event, drv);
3993
3994 return 0;
3995}
3996
3997
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003998static void wpa_driver_nl80211_rfkill_blocked(void *ctx)
3999{
4000 wpa_printf(MSG_DEBUG, "nl80211: RFKILL blocked");
4001 /*
4002 * This may be for any interface; use ifdown event to disable
4003 * interface.
4004 */
4005}
4006
4007
4008static void wpa_driver_nl80211_rfkill_unblocked(void *ctx)
4009{
4010 struct wpa_driver_nl80211_data *drv = ctx;
4011 wpa_printf(MSG_DEBUG, "nl80211: RFKILL unblocked");
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004012 if (i802_set_iface_flags(drv->first_bss, 1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004013 wpa_printf(MSG_DEBUG, "nl80211: Could not set interface UP "
4014 "after rfkill unblock");
4015 return;
4016 }
4017 /* rtnetlink ifup handler will report interface as enabled */
4018}
4019
4020
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004021static void wpa_driver_nl80211_handle_eapol_tx_status(int sock,
4022 void *eloop_ctx,
4023 void *handle)
4024{
4025 struct wpa_driver_nl80211_data *drv = eloop_ctx;
4026 u8 data[2048];
4027 struct msghdr msg;
4028 struct iovec entry;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004029 u8 control[512];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004030 struct cmsghdr *cmsg;
4031 int res, found_ee = 0, found_wifi = 0, acked = 0;
4032 union wpa_event_data event;
4033
4034 memset(&msg, 0, sizeof(msg));
4035 msg.msg_iov = &entry;
4036 msg.msg_iovlen = 1;
4037 entry.iov_base = data;
4038 entry.iov_len = sizeof(data);
4039 msg.msg_control = &control;
4040 msg.msg_controllen = sizeof(control);
4041
4042 res = recvmsg(sock, &msg, MSG_ERRQUEUE);
4043 /* if error or not fitting 802.3 header, return */
4044 if (res < 14)
4045 return;
4046
4047 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg))
4048 {
4049 if (cmsg->cmsg_level == SOL_SOCKET &&
4050 cmsg->cmsg_type == SCM_WIFI_STATUS) {
4051 int *ack;
4052
4053 found_wifi = 1;
4054 ack = (void *)CMSG_DATA(cmsg);
4055 acked = *ack;
4056 }
4057
4058 if (cmsg->cmsg_level == SOL_PACKET &&
4059 cmsg->cmsg_type == PACKET_TX_TIMESTAMP) {
4060 struct sock_extended_err *err =
4061 (struct sock_extended_err *)CMSG_DATA(cmsg);
4062
4063 if (err->ee_origin == SO_EE_ORIGIN_TXSTATUS)
4064 found_ee = 1;
4065 }
4066 }
4067
4068 if (!found_ee || !found_wifi)
4069 return;
4070
4071 memset(&event, 0, sizeof(event));
4072 event.eapol_tx_status.dst = data;
4073 event.eapol_tx_status.data = data + 14;
4074 event.eapol_tx_status.data_len = res - 14;
4075 event.eapol_tx_status.ack = acked;
4076 wpa_supplicant_event(drv->ctx, EVENT_EAPOL_TX_STATUS, &event);
4077}
4078
4079
4080static int nl80211_init_bss(struct i802_bss *bss)
4081{
4082 bss->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
4083 if (!bss->nl_cb)
4084 return -1;
4085
4086 nl_cb_set(bss->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
4087 no_seq_check, NULL);
4088 nl_cb_set(bss->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
4089 process_bss_event, bss);
4090
4091 return 0;
4092}
4093
4094
4095static void nl80211_destroy_bss(struct i802_bss *bss)
4096{
4097 nl_cb_put(bss->nl_cb);
4098 bss->nl_cb = NULL;
4099}
4100
4101
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004102static void * wpa_driver_nl80211_drv_init(void *ctx, const char *ifname,
4103 void *global_priv, int hostapd,
4104 const u8 *set_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004105{
4106 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004107 struct rfkill_config *rcfg;
4108 struct i802_bss *bss;
4109
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004110 if (global_priv == NULL)
4111 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004112 drv = os_zalloc(sizeof(*drv));
4113 if (drv == NULL)
4114 return NULL;
4115 drv->global = global_priv;
4116 drv->ctx = ctx;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004117 drv->hostapd = !!hostapd;
4118 drv->eapol_sock = -1;
4119 drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int);
4120 drv->if_indices = drv->default_if_indices;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004121
4122 drv->first_bss = os_zalloc(sizeof(*drv->first_bss));
4123 if (!drv->first_bss) {
4124 os_free(drv);
4125 return NULL;
4126 }
4127 bss = drv->first_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004128 bss->drv = drv;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004129 bss->ctx = ctx;
4130
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004131 os_strlcpy(bss->ifname, ifname, sizeof(bss->ifname));
4132 drv->monitor_ifidx = -1;
4133 drv->monitor_sock = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004134 drv->eapol_tx_sock = -1;
4135 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004136
4137 if (wpa_driver_nl80211_init_nl(drv)) {
4138 os_free(drv);
4139 return NULL;
4140 }
4141
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004142 if (nl80211_init_bss(bss))
4143 goto failed;
4144
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004145 rcfg = os_zalloc(sizeof(*rcfg));
4146 if (rcfg == NULL)
4147 goto failed;
4148 rcfg->ctx = drv;
4149 os_strlcpy(rcfg->ifname, ifname, sizeof(rcfg->ifname));
4150 rcfg->blocked_cb = wpa_driver_nl80211_rfkill_blocked;
4151 rcfg->unblocked_cb = wpa_driver_nl80211_rfkill_unblocked;
4152 drv->rfkill = rfkill_init(rcfg);
4153 if (drv->rfkill == NULL) {
4154 wpa_printf(MSG_DEBUG, "nl80211: RFKILL status not available");
4155 os_free(rcfg);
4156 }
4157
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004158 if (linux_iface_up(drv->global->ioctl_sock, ifname) > 0)
4159 drv->start_iface_up = 1;
4160
4161 if (wpa_driver_nl80211_finish_drv_init(drv, set_addr, 1))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004162 goto failed;
4163
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004164 drv->eapol_tx_sock = socket(PF_PACKET, SOCK_DGRAM, 0);
4165 if (drv->eapol_tx_sock < 0)
4166 goto failed;
4167
4168 if (drv->data_tx_status) {
4169 int enabled = 1;
4170
4171 if (setsockopt(drv->eapol_tx_sock, SOL_SOCKET, SO_WIFI_STATUS,
4172 &enabled, sizeof(enabled)) < 0) {
4173 wpa_printf(MSG_DEBUG,
4174 "nl80211: wifi status sockopt failed\n");
4175 drv->data_tx_status = 0;
4176 if (!drv->use_monitor)
4177 drv->capa.flags &=
4178 ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
4179 } else {
4180 eloop_register_read_sock(drv->eapol_tx_sock,
4181 wpa_driver_nl80211_handle_eapol_tx_status,
4182 drv, NULL);
4183 }
4184 }
4185
4186 if (drv->global) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004187 dl_list_add(&drv->global->interfaces, &drv->list);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004188 drv->in_interface_list = 1;
4189 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004190
4191 return bss;
4192
4193failed:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004194 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004195 return NULL;
4196}
4197
4198
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004199/**
4200 * wpa_driver_nl80211_init - Initialize nl80211 driver interface
4201 * @ctx: context to be used when calling wpa_supplicant functions,
4202 * e.g., wpa_supplicant_event()
4203 * @ifname: interface name, e.g., wlan0
4204 * @global_priv: private driver global data from global_init()
4205 * Returns: Pointer to private data, %NULL on failure
4206 */
4207static void * wpa_driver_nl80211_init(void *ctx, const char *ifname,
4208 void *global_priv)
4209{
4210 return wpa_driver_nl80211_drv_init(ctx, ifname, global_priv, 0, NULL);
4211}
4212
4213
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004214static int nl80211_register_frame(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004215 struct nl_handle *nl_handle,
4216 u16 type, const u8 *match, size_t match_len)
4217{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004218 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004219 struct nl_msg *msg;
4220 int ret = -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004221 char buf[30];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004222
4223 msg = nlmsg_alloc();
4224 if (!msg)
4225 return -1;
4226
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004227 buf[0] = '\0';
4228 wpa_snprintf_hex(buf, sizeof(buf), match, match_len);
4229 wpa_printf(MSG_DEBUG, "nl80211: Register frame type=0x%x nl_handle=%p match=%s",
4230 type, nl_handle, buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004231
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004232 nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_ACTION);
4233
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004234 if (nl80211_set_iface_id(msg, bss) < 0)
4235 goto nla_put_failure;
4236
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004237 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE, type);
4238 NLA_PUT(msg, NL80211_ATTR_FRAME_MATCH, match_len, match);
4239
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004240 ret = send_and_recv(drv->global, nl_handle, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004241 msg = NULL;
4242 if (ret) {
4243 wpa_printf(MSG_DEBUG, "nl80211: Register frame command "
4244 "failed (type=%u): ret=%d (%s)",
4245 type, ret, strerror(-ret));
4246 wpa_hexdump(MSG_DEBUG, "nl80211: Register frame match",
4247 match, match_len);
4248 goto nla_put_failure;
4249 }
4250 ret = 0;
4251nla_put_failure:
4252 nlmsg_free(msg);
4253 return ret;
4254}
4255
4256
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004257static int nl80211_alloc_mgmt_handle(struct i802_bss *bss)
4258{
4259 struct wpa_driver_nl80211_data *drv = bss->drv;
4260
4261 if (bss->nl_mgmt) {
4262 wpa_printf(MSG_DEBUG, "nl80211: Mgmt reporting "
4263 "already on! (nl_mgmt=%p)", bss->nl_mgmt);
4264 return -1;
4265 }
4266
4267 bss->nl_mgmt = nl_create_handle(drv->nl_cb, "mgmt");
4268 if (bss->nl_mgmt == NULL)
4269 return -1;
4270
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004271 return 0;
4272}
4273
4274
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004275static void nl80211_mgmt_handle_register_eloop(struct i802_bss *bss)
4276{
4277 nl80211_register_eloop_read(&bss->nl_mgmt,
4278 wpa_driver_nl80211_event_receive,
4279 bss->nl_cb);
4280}
4281
4282
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004283static int nl80211_register_action_frame(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004284 const u8 *match, size_t match_len)
4285{
4286 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_ACTION << 4);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004287 return nl80211_register_frame(bss, bss->nl_mgmt,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004288 type, match, match_len);
4289}
4290
4291
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004292static int nl80211_mgmt_subscribe_non_ap(struct i802_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004293{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004294 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004295 int ret = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004296
4297 if (nl80211_alloc_mgmt_handle(bss))
4298 return -1;
4299 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with non-AP "
4300 "handle %p", bss->nl_mgmt);
4301
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004302 if (drv->nlmode == NL80211_IFTYPE_ADHOC) {
4303 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_AUTH << 4);
4304
4305 /* register for any AUTH message */
4306 nl80211_register_frame(bss, bss->nl_mgmt, type, NULL, 0);
4307 }
4308
Dmitry Shmidt051af732013-10-22 13:52:46 -07004309#ifdef CONFIG_INTERWORKING
4310 /* QoS Map Configure */
4311 if (nl80211_register_action_frame(bss, (u8 *) "\x01\x04", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004312 ret = -1;
Dmitry Shmidt051af732013-10-22 13:52:46 -07004313#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004314#if defined(CONFIG_P2P) || defined(CONFIG_INTERWORKING)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004315 /* GAS Initial Request */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004316 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0a", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004317 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004318 /* GAS Initial Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004319 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0b", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004320 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004321 /* GAS Comeback Request */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004322 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0c", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004323 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004324 /* GAS Comeback Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004325 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0d", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004326 ret = -1;
Dmitry Shmidt18463232014-01-24 12:29:41 -08004327 /* Protected GAS Initial Request */
4328 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0a", 2) < 0)
4329 ret = -1;
4330 /* Protected GAS Initial Response */
4331 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0b", 2) < 0)
4332 ret = -1;
4333 /* Protected GAS Comeback Request */
4334 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0c", 2) < 0)
4335 ret = -1;
4336 /* Protected GAS Comeback Response */
4337 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0d", 2) < 0)
4338 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004339#endif /* CONFIG_P2P || CONFIG_INTERWORKING */
4340#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004341 /* P2P Public Action */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004342 if (nl80211_register_action_frame(bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004343 (u8 *) "\x04\x09\x50\x6f\x9a\x09",
4344 6) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004345 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004346 /* P2P Action */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004347 if (nl80211_register_action_frame(bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004348 (u8 *) "\x7f\x50\x6f\x9a\x09",
4349 5) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004350 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004351#endif /* CONFIG_P2P */
4352#ifdef CONFIG_IEEE80211W
4353 /* SA Query Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004354 if (nl80211_register_action_frame(bss, (u8 *) "\x08\x01", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004355 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004356#endif /* CONFIG_IEEE80211W */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004357#ifdef CONFIG_TDLS
4358 if ((drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT)) {
4359 /* TDLS Discovery Response */
4360 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0e", 2) <
4361 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004362 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004363 }
4364#endif /* CONFIG_TDLS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004365
4366 /* FT Action frames */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004367 if (nl80211_register_action_frame(bss, (u8 *) "\x06", 1) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004368 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004369 else
4370 drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_FT |
4371 WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK;
4372
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004373 /* WNM - BSS Transition Management Request */
4374 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x07", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004375 ret = -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004376 /* WNM-Sleep Mode Response */
4377 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x11", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004378 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004379
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08004380#ifdef CONFIG_HS20
4381 /* WNM-Notification */
4382 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x1a", 2) < 0)
4383 return -1;
4384#endif /* CONFIG_HS20 */
4385
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004386 nl80211_mgmt_handle_register_eloop(bss);
4387
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004388 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004389}
4390
4391
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004392static int nl80211_register_spurious_class3(struct i802_bss *bss)
4393{
4394 struct wpa_driver_nl80211_data *drv = bss->drv;
4395 struct nl_msg *msg;
4396 int ret = -1;
4397
4398 msg = nlmsg_alloc();
4399 if (!msg)
4400 return -1;
4401
4402 nl80211_cmd(drv, msg, 0, NL80211_CMD_UNEXPECTED_FRAME);
4403
4404 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
4405
4406 ret = send_and_recv(drv->global, bss->nl_mgmt, msg, NULL, NULL);
4407 msg = NULL;
4408 if (ret) {
4409 wpa_printf(MSG_DEBUG, "nl80211: Register spurious class3 "
4410 "failed: ret=%d (%s)",
4411 ret, strerror(-ret));
4412 goto nla_put_failure;
4413 }
4414 ret = 0;
4415nla_put_failure:
4416 nlmsg_free(msg);
4417 return ret;
4418}
4419
4420
4421static int nl80211_mgmt_subscribe_ap(struct i802_bss *bss)
4422{
4423 static const int stypes[] = {
4424 WLAN_FC_STYPE_AUTH,
4425 WLAN_FC_STYPE_ASSOC_REQ,
4426 WLAN_FC_STYPE_REASSOC_REQ,
4427 WLAN_FC_STYPE_DISASSOC,
4428 WLAN_FC_STYPE_DEAUTH,
4429 WLAN_FC_STYPE_ACTION,
4430 WLAN_FC_STYPE_PROBE_REQ,
4431/* Beacon doesn't work as mac80211 doesn't currently allow
4432 * it, but it wouldn't really be the right thing anyway as
4433 * it isn't per interface ... maybe just dump the scan
4434 * results periodically for OLBC?
4435 */
4436// WLAN_FC_STYPE_BEACON,
4437 };
4438 unsigned int i;
4439
4440 if (nl80211_alloc_mgmt_handle(bss))
4441 return -1;
4442 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
4443 "handle %p", bss->nl_mgmt);
4444
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004445 for (i = 0; i < ARRAY_SIZE(stypes); i++) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004446 if (nl80211_register_frame(bss, bss->nl_mgmt,
4447 (WLAN_FC_TYPE_MGMT << 2) |
4448 (stypes[i] << 4),
4449 NULL, 0) < 0) {
4450 goto out_err;
4451 }
4452 }
4453
4454 if (nl80211_register_spurious_class3(bss))
4455 goto out_err;
4456
4457 if (nl80211_get_wiphy_data_ap(bss) == NULL)
4458 goto out_err;
4459
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004460 nl80211_mgmt_handle_register_eloop(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004461 return 0;
4462
4463out_err:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004464 nl_destroy_handles(&bss->nl_mgmt);
4465 return -1;
4466}
4467
4468
4469static int nl80211_mgmt_subscribe_ap_dev_sme(struct i802_bss *bss)
4470{
4471 if (nl80211_alloc_mgmt_handle(bss))
4472 return -1;
4473 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
4474 "handle %p (device SME)", bss->nl_mgmt);
4475
4476 if (nl80211_register_frame(bss, bss->nl_mgmt,
4477 (WLAN_FC_TYPE_MGMT << 2) |
4478 (WLAN_FC_STYPE_ACTION << 4),
4479 NULL, 0) < 0)
4480 goto out_err;
4481
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004482 nl80211_mgmt_handle_register_eloop(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004483 return 0;
4484
4485out_err:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004486 nl_destroy_handles(&bss->nl_mgmt);
4487 return -1;
4488}
4489
4490
4491static void nl80211_mgmt_unsubscribe(struct i802_bss *bss, const char *reason)
4492{
4493 if (bss->nl_mgmt == NULL)
4494 return;
4495 wpa_printf(MSG_DEBUG, "nl80211: Unsubscribe mgmt frames handle %p "
4496 "(%s)", bss->nl_mgmt, reason);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004497 nl80211_destroy_eloop_handle(&bss->nl_mgmt);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004498
4499 nl80211_put_wiphy_data_ap(bss);
4500}
4501
4502
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004503static void wpa_driver_nl80211_send_rfkill(void *eloop_ctx, void *timeout_ctx)
4504{
4505 wpa_supplicant_event(timeout_ctx, EVENT_INTERFACE_DISABLED, NULL);
4506}
4507
4508
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004509static void nl80211_del_p2pdev(struct i802_bss *bss)
4510{
4511 struct wpa_driver_nl80211_data *drv = bss->drv;
4512 struct nl_msg *msg;
4513 int ret;
4514
4515 msg = nlmsg_alloc();
4516 if (!msg)
4517 return;
4518
4519 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_INTERFACE);
4520 NLA_PUT_U64(msg, NL80211_ATTR_WDEV, bss->wdev_id);
4521
4522 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4523 msg = NULL;
4524
4525 wpa_printf(MSG_DEBUG, "nl80211: Delete P2P Device %s (0x%llx): %s",
4526 bss->ifname, (long long unsigned int) bss->wdev_id,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004527 strerror(-ret));
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004528
4529nla_put_failure:
4530 nlmsg_free(msg);
4531}
4532
4533
4534static int nl80211_set_p2pdev(struct i802_bss *bss, int start)
4535{
4536 struct wpa_driver_nl80211_data *drv = bss->drv;
4537 struct nl_msg *msg;
4538 int ret = -1;
4539
4540 msg = nlmsg_alloc();
4541 if (!msg)
4542 return -1;
4543
4544 if (start)
4545 nl80211_cmd(drv, msg, 0, NL80211_CMD_START_P2P_DEVICE);
4546 else
4547 nl80211_cmd(drv, msg, 0, NL80211_CMD_STOP_P2P_DEVICE);
4548
4549 NLA_PUT_U64(msg, NL80211_ATTR_WDEV, bss->wdev_id);
4550
4551 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4552 msg = NULL;
4553
4554 wpa_printf(MSG_DEBUG, "nl80211: %s P2P Device %s (0x%llx): %s",
4555 start ? "Start" : "Stop",
4556 bss->ifname, (long long unsigned int) bss->wdev_id,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004557 strerror(-ret));
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004558
4559nla_put_failure:
4560 nlmsg_free(msg);
4561 return ret;
4562}
4563
4564
4565static int i802_set_iface_flags(struct i802_bss *bss, int up)
4566{
4567 enum nl80211_iftype nlmode;
4568
4569 nlmode = nl80211_get_ifmode(bss);
4570 if (nlmode != NL80211_IFTYPE_P2P_DEVICE) {
4571 return linux_set_iface_flags(bss->drv->global->ioctl_sock,
4572 bss->ifname, up);
4573 }
4574
4575 /* P2P Device has start/stop which is equivalent */
4576 return nl80211_set_p2pdev(bss, up);
4577}
4578
4579
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004580static int
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004581wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv,
4582 const u8 *set_addr, int first)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004583{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004584 struct i802_bss *bss = drv->first_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004585 int send_rfkill_event = 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004586 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004587
4588 drv->ifindex = if_nametoindex(bss->ifname);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004589 bss->ifindex = drv->ifindex;
4590 bss->wdev_id = drv->global->if_add_wdevid;
4591 bss->wdev_id_set = drv->global->if_add_wdevid_set;
4592
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004593 bss->if_dynamic = drv->ifindex == drv->global->if_add_ifindex;
4594 bss->if_dynamic = bss->if_dynamic || drv->global->if_add_wdevid_set;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004595 drv->global->if_add_wdevid_set = 0;
4596
4597 if (wpa_driver_nl80211_capa(drv))
4598 return -1;
4599
4600 wpa_printf(MSG_DEBUG, "nl80211: interface %s in phy %s",
4601 bss->ifname, drv->phyname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004602
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004603 if (set_addr &&
4604 (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0) ||
4605 linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
4606 set_addr)))
4607 return -1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004608
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004609 if (first && nl80211_get_ifmode(bss) == NL80211_IFTYPE_AP)
4610 drv->start_mode_ap = 1;
4611
4612 if (drv->hostapd)
4613 nlmode = NL80211_IFTYPE_AP;
4614 else if (bss->if_dynamic)
4615 nlmode = nl80211_get_ifmode(bss);
4616 else
4617 nlmode = NL80211_IFTYPE_STATION;
4618
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004619 if (wpa_driver_nl80211_set_mode(bss, nlmode) < 0) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004620 wpa_printf(MSG_ERROR, "nl80211: Could not configure driver mode");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004621 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004622 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004623
4624 if (nlmode == NL80211_IFTYPE_P2P_DEVICE) {
4625 int ret = nl80211_set_p2pdev(bss, 1);
4626 if (ret < 0)
4627 wpa_printf(MSG_ERROR, "nl80211: Could not start P2P device");
4628 nl80211_get_macaddr(bss);
4629 return ret;
4630 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004631
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004632 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004633 if (rfkill_is_blocked(drv->rfkill)) {
4634 wpa_printf(MSG_DEBUG, "nl80211: Could not yet enable "
4635 "interface '%s' due to rfkill",
4636 bss->ifname);
4637 drv->if_disabled = 1;
4638 send_rfkill_event = 1;
4639 } else {
4640 wpa_printf(MSG_ERROR, "nl80211: Could not set "
4641 "interface '%s' UP", bss->ifname);
4642 return -1;
4643 }
4644 }
4645
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004646 if (!drv->hostapd)
4647 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
4648 1, IF_OPER_DORMANT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004649
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004650 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
4651 bss->addr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004652 return -1;
4653
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004654 if (send_rfkill_event) {
4655 eloop_register_timeout(0, 0, wpa_driver_nl80211_send_rfkill,
4656 drv, drv->ctx);
4657 }
4658
4659 return 0;
4660}
4661
4662
4663static int wpa_driver_nl80211_del_beacon(struct wpa_driver_nl80211_data *drv)
4664{
4665 struct nl_msg *msg;
4666
4667 msg = nlmsg_alloc();
4668 if (!msg)
4669 return -ENOMEM;
4670
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004671 wpa_printf(MSG_DEBUG, "nl80211: Remove beacon (ifindex=%d)",
4672 drv->ifindex);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004673 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_BEACON);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004674 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
4675
4676 return send_and_recv_msgs(drv, msg, NULL, NULL);
4677 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004678 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004679 return -ENOBUFS;
4680}
4681
4682
4683/**
4684 * wpa_driver_nl80211_deinit - Deinitialize nl80211 driver interface
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004685 * @bss: Pointer to private nl80211 data from wpa_driver_nl80211_init()
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004686 *
4687 * Shut down driver interface and processing of driver events. Free
4688 * private data buffer if one was allocated in wpa_driver_nl80211_init().
4689 */
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004690static void wpa_driver_nl80211_deinit(struct i802_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004691{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004692 struct wpa_driver_nl80211_data *drv = bss->drv;
4693
Dmitry Shmidt04949592012-07-19 12:16:46 -07004694 bss->in_deinit = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004695 if (drv->data_tx_status)
4696 eloop_unregister_read_sock(drv->eapol_tx_sock);
4697 if (drv->eapol_tx_sock >= 0)
4698 close(drv->eapol_tx_sock);
4699
4700 if (bss->nl_preq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004701 wpa_driver_nl80211_probe_req_report(bss, 0);
4702 if (bss->added_if_into_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004703 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
4704 bss->ifname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004705 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
4706 "interface %s from bridge %s: %s",
4707 bss->ifname, bss->brname, strerror(errno));
4708 }
4709 if (bss->added_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004710 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004711 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
4712 "bridge %s: %s",
4713 bss->brname, strerror(errno));
4714 }
4715
4716 nl80211_remove_monitor_interface(drv);
4717
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004718 if (is_ap_interface(drv->nlmode))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004719 wpa_driver_nl80211_del_beacon(drv);
4720
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004721 if (drv->eapol_sock >= 0) {
4722 eloop_unregister_read_sock(drv->eapol_sock);
4723 close(drv->eapol_sock);
4724 }
4725
4726 if (drv->if_indices != drv->default_if_indices)
4727 os_free(drv->if_indices);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004728
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004729 if (drv->disabled_11b_rates)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004730 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
4731
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004732 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, 0,
4733 IF_OPER_UP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004734 rfkill_deinit(drv->rfkill);
4735
4736 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
4737
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004738 if (!drv->start_iface_up)
4739 (void) i802_set_iface_flags(bss, 0);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004740 if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004741 if (!drv->hostapd || !drv->start_mode_ap)
4742 wpa_driver_nl80211_set_mode(bss,
4743 NL80211_IFTYPE_STATION);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004744 nl80211_mgmt_unsubscribe(bss, "deinit");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004745 } else {
4746 nl80211_mgmt_unsubscribe(bss, "deinit");
4747 nl80211_del_p2pdev(bss);
4748 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004749 nl_cb_put(drv->nl_cb);
4750
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004751 nl80211_destroy_bss(drv->first_bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004752
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004753 os_free(drv->filter_ssids);
4754
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004755 os_free(drv->auth_ie);
4756
4757 if (drv->in_interface_list)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004758 dl_list_del(&drv->list);
4759
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004760 os_free(drv->extended_capa);
4761 os_free(drv->extended_capa_mask);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004762 os_free(drv->first_bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004763 os_free(drv);
4764}
4765
4766
4767/**
4768 * wpa_driver_nl80211_scan_timeout - Scan timeout to report scan completion
4769 * @eloop_ctx: Driver private data
4770 * @timeout_ctx: ctx argument given to wpa_driver_nl80211_init()
4771 *
4772 * This function can be used as registered timeout when starting a scan to
4773 * generate a scan completed event if the driver does not report this.
4774 */
4775static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx, void *timeout_ctx)
4776{
4777 struct wpa_driver_nl80211_data *drv = eloop_ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004778 if (drv->ap_scan_as_station != NL80211_IFTYPE_UNSPECIFIED) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004779 wpa_driver_nl80211_set_mode(drv->first_bss,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004780 drv->ap_scan_as_station);
4781 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004782 }
4783 wpa_printf(MSG_DEBUG, "Scan timeout - try to get results");
4784 wpa_supplicant_event(timeout_ctx, EVENT_SCAN_RESULTS, NULL);
4785}
4786
4787
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004788static struct nl_msg *
4789nl80211_scan_common(struct wpa_driver_nl80211_data *drv, u8 cmd,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004790 struct wpa_driver_scan_params *params, u64 *wdev_id)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004791{
4792 struct nl_msg *msg;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004793 size_t i;
4794
4795 msg = nlmsg_alloc();
4796 if (!msg)
4797 return NULL;
4798
4799 nl80211_cmd(drv, msg, 0, cmd);
4800
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004801 if (!wdev_id)
4802 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
4803 else
4804 NLA_PUT_U64(msg, NL80211_ATTR_WDEV, *wdev_id);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004805
4806 if (params->num_ssids) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004807 struct nlattr *ssids;
4808
4809 ssids = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004810 if (ssids == NULL)
4811 goto fail;
4812 for (i = 0; i < params->num_ssids; i++) {
4813 wpa_hexdump_ascii(MSG_MSGDUMP, "nl80211: Scan SSID",
4814 params->ssids[i].ssid,
4815 params->ssids[i].ssid_len);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004816 if (nla_put(msg, i + 1, params->ssids[i].ssid_len,
4817 params->ssids[i].ssid) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004818 goto fail;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004819 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004820 nla_nest_end(msg, ssids);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004821 }
4822
4823 if (params->extra_ies) {
4824 wpa_hexdump(MSG_MSGDUMP, "nl80211: Scan extra IEs",
4825 params->extra_ies, params->extra_ies_len);
4826 if (nla_put(msg, NL80211_ATTR_IE, params->extra_ies_len,
4827 params->extra_ies) < 0)
4828 goto fail;
4829 }
4830
4831 if (params->freqs) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004832 struct nlattr *freqs;
4833 freqs = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004834 if (freqs == NULL)
4835 goto fail;
4836 for (i = 0; params->freqs[i]; i++) {
4837 wpa_printf(MSG_MSGDUMP, "nl80211: Scan frequency %u "
4838 "MHz", params->freqs[i]);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004839 if (nla_put_u32(msg, i + 1, params->freqs[i]) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004840 goto fail;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004841 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004842 nla_nest_end(msg, freqs);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004843 }
4844
4845 os_free(drv->filter_ssids);
4846 drv->filter_ssids = params->filter_ssids;
4847 params->filter_ssids = NULL;
4848 drv->num_filter_ssids = params->num_filter_ssids;
4849
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004850 if (params->only_new_results) {
4851 wpa_printf(MSG_DEBUG, "nl80211: Add NL80211_SCAN_FLAG_FLUSH");
4852 NLA_PUT_U32(msg, NL80211_ATTR_SCAN_FLAGS,
4853 NL80211_SCAN_FLAG_FLUSH);
4854 }
4855
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004856 return msg;
4857
4858fail:
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004859nla_put_failure:
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004860 nlmsg_free(msg);
4861 return NULL;
4862}
4863
4864
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004865/**
4866 * wpa_driver_nl80211_scan - Request the driver to initiate scan
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004867 * @bss: Pointer to private driver data from wpa_driver_nl80211_init()
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004868 * @params: Scan parameters
4869 * Returns: 0 on success, -1 on failure
4870 */
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004871static int wpa_driver_nl80211_scan(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004872 struct wpa_driver_scan_params *params)
4873{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004874 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004875 int ret = -1, timeout;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004876 struct nl_msg *msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004877
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004878 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: scan request");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004879 drv->scan_for_auth = 0;
4880
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004881 msg = nl80211_scan_common(drv, NL80211_CMD_TRIGGER_SCAN, params,
4882 bss->wdev_id_set ? &bss->wdev_id : NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004883 if (!msg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004884 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004885
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004886 if (params->p2p_probe) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004887 struct nlattr *rates;
4888
Dmitry Shmidt04949592012-07-19 12:16:46 -07004889 wpa_printf(MSG_DEBUG, "nl80211: P2P probe - mask SuppRates");
4890
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004891 rates = nla_nest_start(msg, NL80211_ATTR_SCAN_SUPP_RATES);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004892 if (rates == NULL)
4893 goto nla_put_failure;
4894
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004895 /*
4896 * Remove 2.4 GHz rates 1, 2, 5.5, 11 Mbps from supported rates
4897 * by masking out everything else apart from the OFDM rates 6,
4898 * 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS rates. All 5 GHz
4899 * rates are left enabled.
4900 */
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004901 NLA_PUT(msg, NL80211_BAND_2GHZ, 8,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004902 "\x0c\x12\x18\x24\x30\x48\x60\x6c");
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004903 nla_nest_end(msg, rates);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004904
4905 NLA_PUT_FLAG(msg, NL80211_ATTR_TX_NO_CCK_RATE);
4906 }
4907
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004908 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4909 msg = NULL;
4910 if (ret) {
4911 wpa_printf(MSG_DEBUG, "nl80211: Scan trigger failed: ret=%d "
4912 "(%s)", ret, strerror(-ret));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004913 if (drv->hostapd && is_ap_interface(drv->nlmode)) {
Dmitry Shmidted003d22014-02-06 10:09:12 -08004914 enum nl80211_iftype old_mode = drv->nlmode;
4915
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004916 /*
4917 * mac80211 does not allow scan requests in AP mode, so
4918 * try to do this in station mode.
4919 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004920 if (wpa_driver_nl80211_set_mode(
4921 bss, NL80211_IFTYPE_STATION))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004922 goto nla_put_failure;
4923
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004924 if (wpa_driver_nl80211_scan(bss, params)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004925 wpa_driver_nl80211_set_mode(bss, drv->nlmode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004926 goto nla_put_failure;
4927 }
4928
4929 /* Restore AP mode when processing scan results */
Dmitry Shmidted003d22014-02-06 10:09:12 -08004930 drv->ap_scan_as_station = old_mode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004931 ret = 0;
4932 } else
4933 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004934 }
4935
Dmitry Shmidt56052862013-10-04 10:23:25 -07004936 drv->scan_state = SCAN_REQUESTED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004937 /* Not all drivers generate "scan completed" wireless event, so try to
4938 * read results after a timeout. */
4939 timeout = 10;
4940 if (drv->scan_complete_events) {
4941 /*
4942 * The driver seems to deliver events to notify when scan is
4943 * complete, so use longer timeout to avoid race conditions
4944 * with scanning and following association request.
4945 */
4946 timeout = 30;
4947 }
4948 wpa_printf(MSG_DEBUG, "Scan requested (ret=%d) - scan timeout %d "
4949 "seconds", ret, timeout);
4950 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
4951 eloop_register_timeout(timeout, 0, wpa_driver_nl80211_scan_timeout,
4952 drv, drv->ctx);
4953
4954nla_put_failure:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004955 nlmsg_free(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004956 return ret;
4957}
4958
4959
4960/**
4961 * wpa_driver_nl80211_sched_scan - Initiate a scheduled scan
4962 * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
4963 * @params: Scan parameters
4964 * @interval: Interval between scan cycles in milliseconds
4965 * Returns: 0 on success, -1 on failure or if not supported
4966 */
4967static int wpa_driver_nl80211_sched_scan(void *priv,
4968 struct wpa_driver_scan_params *params,
4969 u32 interval)
4970{
4971 struct i802_bss *bss = priv;
4972 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004973 int ret = -1;
4974 struct nl_msg *msg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004975 size_t i;
4976
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004977 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: sched_scan request");
4978
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004979#ifdef ANDROID
4980 if (!drv->capa.sched_scan_supported)
4981 return android_pno_start(bss, params);
4982#endif /* ANDROID */
4983
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004984 msg = nl80211_scan_common(drv, NL80211_CMD_START_SCHED_SCAN, params,
4985 bss->wdev_id_set ? &bss->wdev_id : NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004986 if (!msg)
4987 goto nla_put_failure;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004988
4989 NLA_PUT_U32(msg, NL80211_ATTR_SCHED_SCAN_INTERVAL, interval);
4990
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004991 if ((drv->num_filter_ssids &&
4992 (int) drv->num_filter_ssids <= drv->capa.max_match_sets) ||
4993 params->filter_rssi) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004994 struct nlattr *match_sets;
4995 match_sets = nla_nest_start(msg, NL80211_ATTR_SCHED_SCAN_MATCH);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004996 if (match_sets == NULL)
4997 goto nla_put_failure;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004998
4999 for (i = 0; i < drv->num_filter_ssids; i++) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005000 struct nlattr *match_set_ssid;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005001 wpa_hexdump_ascii(MSG_MSGDUMP,
5002 "nl80211: Sched scan filter SSID",
5003 drv->filter_ssids[i].ssid,
5004 drv->filter_ssids[i].ssid_len);
5005
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005006 match_set_ssid = nla_nest_start(msg, i + 1);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005007 if (match_set_ssid == NULL)
5008 goto nla_put_failure;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005009 NLA_PUT(msg, NL80211_ATTR_SCHED_SCAN_MATCH_SSID,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005010 drv->filter_ssids[i].ssid_len,
5011 drv->filter_ssids[i].ssid);
Dmitry Shmidt97672262014-02-03 13:02:54 -08005012 if (params->filter_rssi)
5013 NLA_PUT_U32(msg,
5014 NL80211_SCHED_SCAN_MATCH_ATTR_RSSI,
5015 params->filter_rssi);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005016
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005017 nla_nest_end(msg, match_set_ssid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005018 }
5019
Dmitry Shmidt97672262014-02-03 13:02:54 -08005020 /*
5021 * Due to backward compatibility code, newer kernels treat this
5022 * matchset (with only an RSSI filter) as the default for all
5023 * other matchsets, unless it's the only one, in which case the
5024 * matchset will actually allow all SSIDs above the RSSI.
5025 */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005026 if (params->filter_rssi) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005027 struct nlattr *match_set_rssi;
5028 match_set_rssi = nla_nest_start(msg, 0);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005029 if (match_set_rssi == NULL)
5030 goto nla_put_failure;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005031 NLA_PUT_U32(msg, NL80211_SCHED_SCAN_MATCH_ATTR_RSSI,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005032 params->filter_rssi);
5033 wpa_printf(MSG_MSGDUMP,
5034 "nl80211: Sched scan RSSI filter %d dBm",
5035 params->filter_rssi);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005036 nla_nest_end(msg, match_set_rssi);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005037 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005038
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005039 nla_nest_end(msg, match_sets);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005040 }
5041
5042 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5043
5044 /* TODO: if we get an error here, we should fall back to normal scan */
5045
5046 msg = NULL;
5047 if (ret) {
5048 wpa_printf(MSG_DEBUG, "nl80211: Sched scan start failed: "
5049 "ret=%d (%s)", ret, strerror(-ret));
5050 goto nla_put_failure;
5051 }
5052
5053 wpa_printf(MSG_DEBUG, "nl80211: Sched scan requested (ret=%d) - "
5054 "scan interval %d msec", ret, interval);
5055
5056nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005057 nlmsg_free(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005058 return ret;
5059}
5060
5061
5062/**
5063 * wpa_driver_nl80211_stop_sched_scan - Stop a scheduled scan
5064 * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
5065 * Returns: 0 on success, -1 on failure or if not supported
5066 */
5067static int wpa_driver_nl80211_stop_sched_scan(void *priv)
5068{
5069 struct i802_bss *bss = priv;
5070 struct wpa_driver_nl80211_data *drv = bss->drv;
5071 int ret = 0;
5072 struct nl_msg *msg;
5073
5074#ifdef ANDROID
5075 if (!drv->capa.sched_scan_supported)
5076 return android_pno_stop(bss);
5077#endif /* ANDROID */
5078
5079 msg = nlmsg_alloc();
5080 if (!msg)
5081 return -1;
5082
5083 nl80211_cmd(drv, msg, 0, NL80211_CMD_STOP_SCHED_SCAN);
5084
5085 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
5086
5087 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5088 msg = NULL;
5089 if (ret) {
5090 wpa_printf(MSG_DEBUG, "nl80211: Sched scan stop failed: "
5091 "ret=%d (%s)", ret, strerror(-ret));
5092 goto nla_put_failure;
5093 }
5094
5095 wpa_printf(MSG_DEBUG, "nl80211: Sched scan stop sent (ret=%d)", ret);
5096
5097nla_put_failure:
5098 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005099 return ret;
5100}
5101
5102
5103static const u8 * nl80211_get_ie(const u8 *ies, size_t ies_len, u8 ie)
5104{
5105 const u8 *end, *pos;
5106
5107 if (ies == NULL)
5108 return NULL;
5109
5110 pos = ies;
5111 end = ies + ies_len;
5112
5113 while (pos + 1 < end) {
5114 if (pos + 2 + pos[1] > end)
5115 break;
5116 if (pos[0] == ie)
5117 return pos;
5118 pos += 2 + pos[1];
5119 }
5120
5121 return NULL;
5122}
5123
5124
5125static int nl80211_scan_filtered(struct wpa_driver_nl80211_data *drv,
5126 const u8 *ie, size_t ie_len)
5127{
5128 const u8 *ssid;
5129 size_t i;
5130
5131 if (drv->filter_ssids == NULL)
5132 return 0;
5133
5134 ssid = nl80211_get_ie(ie, ie_len, WLAN_EID_SSID);
5135 if (ssid == NULL)
5136 return 1;
5137
5138 for (i = 0; i < drv->num_filter_ssids; i++) {
5139 if (ssid[1] == drv->filter_ssids[i].ssid_len &&
5140 os_memcmp(ssid + 2, drv->filter_ssids[i].ssid, ssid[1]) ==
5141 0)
5142 return 0;
5143 }
5144
5145 return 1;
5146}
5147
5148
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005149static int bss_info_handler(struct nl_msg *msg, void *arg)
5150{
5151 struct nlattr *tb[NL80211_ATTR_MAX + 1];
5152 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
5153 struct nlattr *bss[NL80211_BSS_MAX + 1];
5154 static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
5155 [NL80211_BSS_BSSID] = { .type = NLA_UNSPEC },
5156 [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
5157 [NL80211_BSS_TSF] = { .type = NLA_U64 },
5158 [NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 },
5159 [NL80211_BSS_CAPABILITY] = { .type = NLA_U16 },
5160 [NL80211_BSS_INFORMATION_ELEMENTS] = { .type = NLA_UNSPEC },
5161 [NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 },
5162 [NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 },
5163 [NL80211_BSS_STATUS] = { .type = NLA_U32 },
5164 [NL80211_BSS_SEEN_MS_AGO] = { .type = NLA_U32 },
5165 [NL80211_BSS_BEACON_IES] = { .type = NLA_UNSPEC },
5166 };
5167 struct nl80211_bss_info_arg *_arg = arg;
5168 struct wpa_scan_results *res = _arg->res;
5169 struct wpa_scan_res **tmp;
5170 struct wpa_scan_res *r;
5171 const u8 *ie, *beacon_ie;
5172 size_t ie_len, beacon_ie_len;
5173 u8 *pos;
Jouni Malinen87fd2792011-05-16 18:35:42 +03005174 size_t i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005175
5176 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
5177 genlmsg_attrlen(gnlh, 0), NULL);
5178 if (!tb[NL80211_ATTR_BSS])
5179 return NL_SKIP;
5180 if (nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS],
5181 bss_policy))
5182 return NL_SKIP;
Jouni Malinen87fd2792011-05-16 18:35:42 +03005183 if (bss[NL80211_BSS_STATUS]) {
5184 enum nl80211_bss_status status;
5185 status = nla_get_u32(bss[NL80211_BSS_STATUS]);
5186 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
5187 bss[NL80211_BSS_FREQUENCY]) {
5188 _arg->assoc_freq =
5189 nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
5190 wpa_printf(MSG_DEBUG, "nl80211: Associated on %u MHz",
5191 _arg->assoc_freq);
5192 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005193 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
5194 bss[NL80211_BSS_BSSID]) {
5195 os_memcpy(_arg->assoc_bssid,
5196 nla_data(bss[NL80211_BSS_BSSID]), ETH_ALEN);
5197 wpa_printf(MSG_DEBUG, "nl80211: Associated with "
5198 MACSTR, MAC2STR(_arg->assoc_bssid));
5199 }
Jouni Malinen87fd2792011-05-16 18:35:42 +03005200 }
5201 if (!res)
5202 return NL_SKIP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005203 if (bss[NL80211_BSS_INFORMATION_ELEMENTS]) {
5204 ie = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
5205 ie_len = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
5206 } else {
5207 ie = NULL;
5208 ie_len = 0;
5209 }
5210 if (bss[NL80211_BSS_BEACON_IES]) {
5211 beacon_ie = nla_data(bss[NL80211_BSS_BEACON_IES]);
5212 beacon_ie_len = nla_len(bss[NL80211_BSS_BEACON_IES]);
5213 } else {
5214 beacon_ie = NULL;
5215 beacon_ie_len = 0;
5216 }
5217
5218 if (nl80211_scan_filtered(_arg->drv, ie ? ie : beacon_ie,
5219 ie ? ie_len : beacon_ie_len))
5220 return NL_SKIP;
5221
5222 r = os_zalloc(sizeof(*r) + ie_len + beacon_ie_len);
5223 if (r == NULL)
5224 return NL_SKIP;
5225 if (bss[NL80211_BSS_BSSID])
5226 os_memcpy(r->bssid, nla_data(bss[NL80211_BSS_BSSID]),
5227 ETH_ALEN);
5228 if (bss[NL80211_BSS_FREQUENCY])
5229 r->freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
5230 if (bss[NL80211_BSS_BEACON_INTERVAL])
5231 r->beacon_int = nla_get_u16(bss[NL80211_BSS_BEACON_INTERVAL]);
5232 if (bss[NL80211_BSS_CAPABILITY])
5233 r->caps = nla_get_u16(bss[NL80211_BSS_CAPABILITY]);
5234 r->flags |= WPA_SCAN_NOISE_INVALID;
5235 if (bss[NL80211_BSS_SIGNAL_MBM]) {
5236 r->level = nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]);
5237 r->level /= 100; /* mBm to dBm */
5238 r->flags |= WPA_SCAN_LEVEL_DBM | WPA_SCAN_QUAL_INVALID;
5239 } else if (bss[NL80211_BSS_SIGNAL_UNSPEC]) {
5240 r->level = nla_get_u8(bss[NL80211_BSS_SIGNAL_UNSPEC]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005241 r->flags |= WPA_SCAN_QUAL_INVALID;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005242 } else
5243 r->flags |= WPA_SCAN_LEVEL_INVALID | WPA_SCAN_QUAL_INVALID;
5244 if (bss[NL80211_BSS_TSF])
5245 r->tsf = nla_get_u64(bss[NL80211_BSS_TSF]);
5246 if (bss[NL80211_BSS_SEEN_MS_AGO])
5247 r->age = nla_get_u32(bss[NL80211_BSS_SEEN_MS_AGO]);
5248 r->ie_len = ie_len;
5249 pos = (u8 *) (r + 1);
5250 if (ie) {
5251 os_memcpy(pos, ie, ie_len);
5252 pos += ie_len;
5253 }
5254 r->beacon_ie_len = beacon_ie_len;
5255 if (beacon_ie)
5256 os_memcpy(pos, beacon_ie, beacon_ie_len);
5257
5258 if (bss[NL80211_BSS_STATUS]) {
5259 enum nl80211_bss_status status;
5260 status = nla_get_u32(bss[NL80211_BSS_STATUS]);
5261 switch (status) {
5262 case NL80211_BSS_STATUS_AUTHENTICATED:
5263 r->flags |= WPA_SCAN_AUTHENTICATED;
5264 break;
5265 case NL80211_BSS_STATUS_ASSOCIATED:
5266 r->flags |= WPA_SCAN_ASSOCIATED;
5267 break;
5268 default:
5269 break;
5270 }
5271 }
5272
Jouni Malinen87fd2792011-05-16 18:35:42 +03005273 /*
5274 * cfg80211 maintains separate BSS table entries for APs if the same
5275 * BSSID,SSID pair is seen on multiple channels. wpa_supplicant does
5276 * not use frequency as a separate key in the BSS table, so filter out
5277 * duplicated entries. Prefer associated BSS entry in such a case in
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005278 * order to get the correct frequency into the BSS table. Similarly,
5279 * prefer newer entries over older.
Jouni Malinen87fd2792011-05-16 18:35:42 +03005280 */
5281 for (i = 0; i < res->num; i++) {
5282 const u8 *s1, *s2;
5283 if (os_memcmp(res->res[i]->bssid, r->bssid, ETH_ALEN) != 0)
5284 continue;
5285
5286 s1 = nl80211_get_ie((u8 *) (res->res[i] + 1),
5287 res->res[i]->ie_len, WLAN_EID_SSID);
5288 s2 = nl80211_get_ie((u8 *) (r + 1), r->ie_len, WLAN_EID_SSID);
5289 if (s1 == NULL || s2 == NULL || s1[1] != s2[1] ||
5290 os_memcmp(s1, s2, 2 + s1[1]) != 0)
5291 continue;
5292
5293 /* Same BSSID,SSID was already included in scan results */
5294 wpa_printf(MSG_DEBUG, "nl80211: Remove duplicated scan result "
5295 "for " MACSTR, MAC2STR(r->bssid));
5296
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005297 if (((r->flags & WPA_SCAN_ASSOCIATED) &&
5298 !(res->res[i]->flags & WPA_SCAN_ASSOCIATED)) ||
5299 r->age < res->res[i]->age) {
Jouni Malinen87fd2792011-05-16 18:35:42 +03005300 os_free(res->res[i]);
5301 res->res[i] = r;
5302 } else
5303 os_free(r);
5304 return NL_SKIP;
5305 }
5306
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005307 tmp = os_realloc_array(res->res, res->num + 1,
5308 sizeof(struct wpa_scan_res *));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005309 if (tmp == NULL) {
5310 os_free(r);
5311 return NL_SKIP;
5312 }
5313 tmp[res->num++] = r;
5314 res->res = tmp;
5315
5316 return NL_SKIP;
5317}
5318
5319
5320static void clear_state_mismatch(struct wpa_driver_nl80211_data *drv,
5321 const u8 *addr)
5322{
5323 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
5324 wpa_printf(MSG_DEBUG, "nl80211: Clear possible state "
5325 "mismatch (" MACSTR ")", MAC2STR(addr));
5326 wpa_driver_nl80211_mlme(drv, addr,
5327 NL80211_CMD_DEAUTHENTICATE,
5328 WLAN_REASON_PREV_AUTH_NOT_VALID, 1);
5329 }
5330}
5331
5332
5333static void wpa_driver_nl80211_check_bss_status(
5334 struct wpa_driver_nl80211_data *drv, struct wpa_scan_results *res)
5335{
5336 size_t i;
5337
5338 for (i = 0; i < res->num; i++) {
5339 struct wpa_scan_res *r = res->res[i];
5340 if (r->flags & WPA_SCAN_AUTHENTICATED) {
5341 wpa_printf(MSG_DEBUG, "nl80211: Scan results "
5342 "indicates BSS status with " MACSTR
5343 " as authenticated",
5344 MAC2STR(r->bssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005345 if (is_sta_interface(drv->nlmode) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005346 os_memcmp(r->bssid, drv->bssid, ETH_ALEN) != 0 &&
5347 os_memcmp(r->bssid, drv->auth_bssid, ETH_ALEN) !=
5348 0) {
5349 wpa_printf(MSG_DEBUG, "nl80211: Unknown BSSID"
5350 " in local state (auth=" MACSTR
5351 " assoc=" MACSTR ")",
5352 MAC2STR(drv->auth_bssid),
5353 MAC2STR(drv->bssid));
5354 clear_state_mismatch(drv, r->bssid);
5355 }
5356 }
5357
5358 if (r->flags & WPA_SCAN_ASSOCIATED) {
5359 wpa_printf(MSG_DEBUG, "nl80211: Scan results "
5360 "indicate BSS status with " MACSTR
5361 " as associated",
5362 MAC2STR(r->bssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005363 if (is_sta_interface(drv->nlmode) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005364 !drv->associated) {
5365 wpa_printf(MSG_DEBUG, "nl80211: Local state "
5366 "(not associated) does not match "
5367 "with BSS state");
5368 clear_state_mismatch(drv, r->bssid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005369 } else if (is_sta_interface(drv->nlmode) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005370 os_memcmp(drv->bssid, r->bssid, ETH_ALEN) !=
5371 0) {
5372 wpa_printf(MSG_DEBUG, "nl80211: Local state "
5373 "(associated with " MACSTR ") does "
5374 "not match with BSS state",
5375 MAC2STR(drv->bssid));
5376 clear_state_mismatch(drv, r->bssid);
5377 clear_state_mismatch(drv, drv->bssid);
5378 }
5379 }
5380 }
5381}
5382
5383
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005384static struct wpa_scan_results *
5385nl80211_get_scan_results(struct wpa_driver_nl80211_data *drv)
5386{
5387 struct nl_msg *msg;
5388 struct wpa_scan_results *res;
5389 int ret;
5390 struct nl80211_bss_info_arg arg;
5391
5392 res = os_zalloc(sizeof(*res));
5393 if (res == NULL)
5394 return NULL;
5395 msg = nlmsg_alloc();
5396 if (!msg)
5397 goto nla_put_failure;
5398
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005399 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005400 if (nl80211_set_iface_id(msg, drv->first_bss) < 0)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005401 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005402
5403 arg.drv = drv;
5404 arg.res = res;
5405 ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
5406 msg = NULL;
5407 if (ret == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005408 wpa_printf(MSG_DEBUG, "nl80211: Received scan results (%lu "
5409 "BSSes)", (unsigned long) res->num);
5410 nl80211_get_noise_for_scan_results(drv, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005411 return res;
5412 }
5413 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
5414 "(%s)", ret, strerror(-ret));
5415nla_put_failure:
5416 nlmsg_free(msg);
5417 wpa_scan_results_free(res);
5418 return NULL;
5419}
5420
5421
5422/**
5423 * wpa_driver_nl80211_get_scan_results - Fetch the latest scan results
5424 * @priv: Pointer to private wext data from wpa_driver_nl80211_init()
5425 * Returns: Scan results on success, -1 on failure
5426 */
5427static struct wpa_scan_results *
5428wpa_driver_nl80211_get_scan_results(void *priv)
5429{
5430 struct i802_bss *bss = priv;
5431 struct wpa_driver_nl80211_data *drv = bss->drv;
5432 struct wpa_scan_results *res;
5433
5434 res = nl80211_get_scan_results(drv);
5435 if (res)
5436 wpa_driver_nl80211_check_bss_status(drv, res);
5437 return res;
5438}
5439
5440
5441static void nl80211_dump_scan(struct wpa_driver_nl80211_data *drv)
5442{
5443 struct wpa_scan_results *res;
5444 size_t i;
5445
5446 res = nl80211_get_scan_results(drv);
5447 if (res == NULL) {
5448 wpa_printf(MSG_DEBUG, "nl80211: Failed to get scan results");
5449 return;
5450 }
5451
5452 wpa_printf(MSG_DEBUG, "nl80211: Scan result dump");
5453 for (i = 0; i < res->num; i++) {
5454 struct wpa_scan_res *r = res->res[i];
5455 wpa_printf(MSG_DEBUG, "nl80211: %d/%d " MACSTR "%s%s",
5456 (int) i, (int) res->num, MAC2STR(r->bssid),
5457 r->flags & WPA_SCAN_AUTHENTICATED ? " [auth]" : "",
5458 r->flags & WPA_SCAN_ASSOCIATED ? " [assoc]" : "");
5459 }
5460
5461 wpa_scan_results_free(res);
5462}
5463
5464
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005465static u32 wpa_alg_to_cipher_suite(enum wpa_alg alg, size_t key_len)
5466{
5467 switch (alg) {
5468 case WPA_ALG_WEP:
5469 if (key_len == 5)
5470 return WLAN_CIPHER_SUITE_WEP40;
5471 return WLAN_CIPHER_SUITE_WEP104;
5472 case WPA_ALG_TKIP:
5473 return WLAN_CIPHER_SUITE_TKIP;
5474 case WPA_ALG_CCMP:
5475 return WLAN_CIPHER_SUITE_CCMP;
5476 case WPA_ALG_GCMP:
5477 return WLAN_CIPHER_SUITE_GCMP;
5478 case WPA_ALG_CCMP_256:
5479 return WLAN_CIPHER_SUITE_CCMP_256;
5480 case WPA_ALG_GCMP_256:
5481 return WLAN_CIPHER_SUITE_GCMP_256;
5482 case WPA_ALG_IGTK:
5483 return WLAN_CIPHER_SUITE_AES_CMAC;
5484 case WPA_ALG_BIP_GMAC_128:
5485 return WLAN_CIPHER_SUITE_BIP_GMAC_128;
5486 case WPA_ALG_BIP_GMAC_256:
5487 return WLAN_CIPHER_SUITE_BIP_GMAC_256;
5488 case WPA_ALG_BIP_CMAC_256:
5489 return WLAN_CIPHER_SUITE_BIP_CMAC_256;
5490 case WPA_ALG_SMS4:
5491 return WLAN_CIPHER_SUITE_SMS4;
5492 case WPA_ALG_KRK:
5493 return WLAN_CIPHER_SUITE_KRK;
5494 case WPA_ALG_NONE:
5495 case WPA_ALG_PMK:
5496 wpa_printf(MSG_ERROR, "nl80211: Unexpected encryption algorithm %d",
5497 alg);
5498 return 0;
5499 }
5500
5501 wpa_printf(MSG_ERROR, "nl80211: Unsupported encryption algorithm %d",
5502 alg);
5503 return 0;
5504}
5505
5506
5507static u32 wpa_cipher_to_cipher_suite(unsigned int cipher)
5508{
5509 switch (cipher) {
5510 case WPA_CIPHER_CCMP_256:
5511 return WLAN_CIPHER_SUITE_CCMP_256;
5512 case WPA_CIPHER_GCMP_256:
5513 return WLAN_CIPHER_SUITE_GCMP_256;
5514 case WPA_CIPHER_CCMP:
5515 return WLAN_CIPHER_SUITE_CCMP;
5516 case WPA_CIPHER_GCMP:
5517 return WLAN_CIPHER_SUITE_GCMP;
5518 case WPA_CIPHER_TKIP:
5519 return WLAN_CIPHER_SUITE_TKIP;
5520 case WPA_CIPHER_WEP104:
5521 return WLAN_CIPHER_SUITE_WEP104;
5522 case WPA_CIPHER_WEP40:
5523 return WLAN_CIPHER_SUITE_WEP40;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08005524 case WPA_CIPHER_GTK_NOT_USED:
5525 return WLAN_CIPHER_SUITE_NO_GROUP_ADDR;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005526 }
5527
5528 return 0;
5529}
5530
5531
5532static int wpa_cipher_to_cipher_suites(unsigned int ciphers, u32 suites[],
5533 int max_suites)
5534{
5535 int num_suites = 0;
5536
5537 if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP_256)
5538 suites[num_suites++] = WLAN_CIPHER_SUITE_CCMP_256;
5539 if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP_256)
5540 suites[num_suites++] = WLAN_CIPHER_SUITE_GCMP_256;
5541 if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP)
5542 suites[num_suites++] = WLAN_CIPHER_SUITE_CCMP;
5543 if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP)
5544 suites[num_suites++] = WLAN_CIPHER_SUITE_GCMP;
5545 if (num_suites < max_suites && ciphers & WPA_CIPHER_TKIP)
5546 suites[num_suites++] = WLAN_CIPHER_SUITE_TKIP;
5547 if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP104)
5548 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP104;
5549 if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP40)
5550 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP40;
5551
5552 return num_suites;
5553}
5554
5555
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005556static int wpa_driver_nl80211_set_key(const char *ifname, struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005557 enum wpa_alg alg, const u8 *addr,
5558 int key_idx, int set_tx,
5559 const u8 *seq, size_t seq_len,
5560 const u8 *key, size_t key_len)
5561{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005562 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005563 int ifindex;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005564 struct nl_msg *msg;
5565 int ret;
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07005566 int tdls = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005567
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005568 /* Ignore for P2P Device */
5569 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
5570 return 0;
5571
5572 ifindex = if_nametoindex(ifname);
5573 wpa_printf(MSG_DEBUG, "%s: ifindex=%d (%s) alg=%d addr=%p key_idx=%d "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005574 "set_tx=%d seq_len=%lu key_len=%lu",
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005575 __func__, ifindex, ifname, alg, addr, key_idx, set_tx,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005576 (unsigned long) seq_len, (unsigned long) key_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005577#ifdef CONFIG_TDLS
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07005578 if (key_idx == -1) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005579 key_idx = 0;
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07005580 tdls = 1;
5581 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005582#endif /* CONFIG_TDLS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005583
5584 msg = nlmsg_alloc();
5585 if (!msg)
5586 return -ENOMEM;
5587
5588 if (alg == WPA_ALG_NONE) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005589 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_KEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005590 } else {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005591 nl80211_cmd(drv, msg, 0, NL80211_CMD_NEW_KEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005592 NLA_PUT(msg, NL80211_ATTR_KEY_DATA, key_len, key);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005593 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
5594 wpa_alg_to_cipher_suite(alg, key_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005595 }
5596
5597 if (seq && seq_len)
5598 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, seq_len, seq);
5599
5600 if (addr && !is_broadcast_ether_addr(addr)) {
5601 wpa_printf(MSG_DEBUG, " addr=" MACSTR, MAC2STR(addr));
5602 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
5603
5604 if (alg != WPA_ALG_WEP && key_idx && !set_tx) {
5605 wpa_printf(MSG_DEBUG, " RSN IBSS RX GTK");
5606 NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE,
5607 NL80211_KEYTYPE_GROUP);
5608 }
5609 } else if (addr && is_broadcast_ether_addr(addr)) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005610 struct nlattr *types;
5611
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005612 wpa_printf(MSG_DEBUG, " broadcast key");
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005613
5614 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005615 if (!types)
5616 goto nla_put_failure;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005617 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST);
5618 nla_nest_end(msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005619 }
5620 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
5621 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
5622
5623 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5624 if ((ret == -ENOENT || ret == -ENOLINK) && alg == WPA_ALG_NONE)
5625 ret = 0;
5626 if (ret)
5627 wpa_printf(MSG_DEBUG, "nl80211: set_key failed; err=%d %s)",
5628 ret, strerror(-ret));
5629
5630 /*
5631 * If we failed or don't need to set the default TX key (below),
5632 * we're done here.
5633 */
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07005634 if (ret || !set_tx || alg == WPA_ALG_NONE || tdls)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005635 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005636 if (is_ap_interface(drv->nlmode) && addr &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005637 !is_broadcast_ether_addr(addr))
5638 return ret;
5639
5640 msg = nlmsg_alloc();
5641 if (!msg)
5642 return -ENOMEM;
5643
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005644 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_KEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005645 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
5646 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
5647 if (alg == WPA_ALG_IGTK)
5648 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT_MGMT);
5649 else
5650 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT);
5651 if (addr && is_broadcast_ether_addr(addr)) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005652 struct nlattr *types;
5653
5654 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005655 if (!types)
5656 goto nla_put_failure;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005657 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST);
5658 nla_nest_end(msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005659 } else if (addr) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005660 struct nlattr *types;
5661
5662 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005663 if (!types)
5664 goto nla_put_failure;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005665 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_UNICAST);
5666 nla_nest_end(msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005667 }
5668
5669 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5670 if (ret == -ENOENT)
5671 ret = 0;
5672 if (ret)
5673 wpa_printf(MSG_DEBUG, "nl80211: set_key default failed; "
5674 "err=%d %s)", ret, strerror(-ret));
5675 return ret;
5676
5677nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005678 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005679 return -ENOBUFS;
5680}
5681
5682
5683static int nl_add_key(struct nl_msg *msg, enum wpa_alg alg,
5684 int key_idx, int defkey,
5685 const u8 *seq, size_t seq_len,
5686 const u8 *key, size_t key_len)
5687{
5688 struct nlattr *key_attr = nla_nest_start(msg, NL80211_ATTR_KEY);
5689 if (!key_attr)
5690 return -1;
5691
5692 if (defkey && alg == WPA_ALG_IGTK)
5693 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_MGMT);
5694 else if (defkey)
5695 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
5696
5697 NLA_PUT_U8(msg, NL80211_KEY_IDX, key_idx);
5698
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005699 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
5700 wpa_alg_to_cipher_suite(alg, key_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005701
5702 if (seq && seq_len)
5703 NLA_PUT(msg, NL80211_KEY_SEQ, seq_len, seq);
5704
5705 NLA_PUT(msg, NL80211_KEY_DATA, key_len, key);
5706
5707 nla_nest_end(msg, key_attr);
5708
5709 return 0;
5710 nla_put_failure:
5711 return -1;
5712}
5713
5714
5715static int nl80211_set_conn_keys(struct wpa_driver_associate_params *params,
5716 struct nl_msg *msg)
5717{
5718 int i, privacy = 0;
5719 struct nlattr *nl_keys, *nl_key;
5720
5721 for (i = 0; i < 4; i++) {
5722 if (!params->wep_key[i])
5723 continue;
5724 privacy = 1;
5725 break;
5726 }
5727 if (params->wps == WPS_MODE_PRIVACY)
5728 privacy = 1;
5729 if (params->pairwise_suite &&
5730 params->pairwise_suite != WPA_CIPHER_NONE)
5731 privacy = 1;
5732
5733 if (!privacy)
5734 return 0;
5735
5736 NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
5737
5738 nl_keys = nla_nest_start(msg, NL80211_ATTR_KEYS);
5739 if (!nl_keys)
5740 goto nla_put_failure;
5741
5742 for (i = 0; i < 4; i++) {
5743 if (!params->wep_key[i])
5744 continue;
5745
5746 nl_key = nla_nest_start(msg, i);
5747 if (!nl_key)
5748 goto nla_put_failure;
5749
5750 NLA_PUT(msg, NL80211_KEY_DATA, params->wep_key_len[i],
5751 params->wep_key[i]);
5752 if (params->wep_key_len[i] == 5)
5753 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
5754 WLAN_CIPHER_SUITE_WEP40);
5755 else
5756 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
5757 WLAN_CIPHER_SUITE_WEP104);
5758
5759 NLA_PUT_U8(msg, NL80211_KEY_IDX, i);
5760
5761 if (i == params->wep_tx_keyidx)
5762 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
5763
5764 nla_nest_end(msg, nl_key);
5765 }
5766 nla_nest_end(msg, nl_keys);
5767
5768 return 0;
5769
5770nla_put_failure:
5771 return -ENOBUFS;
5772}
5773
5774
5775static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
5776 const u8 *addr, int cmd, u16 reason_code,
5777 int local_state_change)
5778{
5779 int ret = -1;
5780 struct nl_msg *msg;
5781
5782 msg = nlmsg_alloc();
5783 if (!msg)
5784 return -1;
5785
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005786 nl80211_cmd(drv, msg, 0, cmd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005787
5788 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
5789 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason_code);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005790 if (addr)
5791 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005792 if (local_state_change)
5793 NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE);
5794
5795 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5796 msg = NULL;
5797 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005798 wpa_dbg(drv->ctx, MSG_DEBUG,
5799 "nl80211: MLME command failed: reason=%u ret=%d (%s)",
5800 reason_code, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005801 goto nla_put_failure;
5802 }
5803 ret = 0;
5804
5805nla_put_failure:
5806 nlmsg_free(msg);
5807 return ret;
5808}
5809
5810
5811static int wpa_driver_nl80211_disconnect(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005812 int reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005813{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005814 int ret;
5815
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005816 wpa_printf(MSG_DEBUG, "%s(reason_code=%d)", __func__, reason_code);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005817 nl80211_mark_disconnected(drv);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005818 /* Disconnect command doesn't need BSSID - it uses cached value */
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005819 ret = wpa_driver_nl80211_mlme(drv, NULL, NL80211_CMD_DISCONNECT,
5820 reason_code, 0);
5821 /*
5822 * For locally generated disconnect, supplicant already generates a
5823 * DEAUTH event, so ignore the event from NL80211.
5824 */
5825 drv->ignore_next_local_disconnect = ret == 0;
5826
5827 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005828}
5829
5830
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005831static int wpa_driver_nl80211_deauthenticate(struct i802_bss *bss,
5832 const u8 *addr, int reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005833{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005834 struct wpa_driver_nl80211_data *drv = bss->drv;
5835 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME))
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005836 return wpa_driver_nl80211_disconnect(drv, reason_code);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005837 wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " reason_code=%d)",
5838 __func__, MAC2STR(addr), reason_code);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005839 nl80211_mark_disconnected(drv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005840 if (drv->nlmode == NL80211_IFTYPE_ADHOC)
5841 return nl80211_leave_ibss(drv);
5842 return wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DEAUTHENTICATE,
5843 reason_code, 0);
5844}
5845
5846
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005847static void nl80211_copy_auth_params(struct wpa_driver_nl80211_data *drv,
5848 struct wpa_driver_auth_params *params)
5849{
5850 int i;
5851
5852 drv->auth_freq = params->freq;
5853 drv->auth_alg = params->auth_alg;
5854 drv->auth_wep_tx_keyidx = params->wep_tx_keyidx;
5855 drv->auth_local_state_change = params->local_state_change;
5856 drv->auth_p2p = params->p2p;
5857
5858 if (params->bssid)
5859 os_memcpy(drv->auth_bssid_, params->bssid, ETH_ALEN);
5860 else
5861 os_memset(drv->auth_bssid_, 0, ETH_ALEN);
5862
5863 if (params->ssid) {
5864 os_memcpy(drv->auth_ssid, params->ssid, params->ssid_len);
5865 drv->auth_ssid_len = params->ssid_len;
5866 } else
5867 drv->auth_ssid_len = 0;
5868
5869
5870 os_free(drv->auth_ie);
5871 drv->auth_ie = NULL;
5872 drv->auth_ie_len = 0;
5873 if (params->ie) {
5874 drv->auth_ie = os_malloc(params->ie_len);
5875 if (drv->auth_ie) {
5876 os_memcpy(drv->auth_ie, params->ie, params->ie_len);
5877 drv->auth_ie_len = params->ie_len;
5878 }
5879 }
5880
5881 for (i = 0; i < 4; i++) {
5882 if (params->wep_key[i] && params->wep_key_len[i] &&
5883 params->wep_key_len[i] <= 16) {
5884 os_memcpy(drv->auth_wep_key[i], params->wep_key[i],
5885 params->wep_key_len[i]);
5886 drv->auth_wep_key_len[i] = params->wep_key_len[i];
5887 } else
5888 drv->auth_wep_key_len[i] = 0;
5889 }
5890}
5891
5892
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005893static int wpa_driver_nl80211_authenticate(
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005894 struct i802_bss *bss, struct wpa_driver_auth_params *params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005895{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005896 struct wpa_driver_nl80211_data *drv = bss->drv;
5897 int ret = -1, i;
5898 struct nl_msg *msg;
5899 enum nl80211_auth_type type;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005900 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005901 int count = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005902 int is_retry;
5903
5904 is_retry = drv->retry_auth;
5905 drv->retry_auth = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005906
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005907 nl80211_mark_disconnected(drv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005908 os_memset(drv->auth_bssid, 0, ETH_ALEN);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005909 if (params->bssid)
5910 os_memcpy(drv->auth_attempt_bssid, params->bssid, ETH_ALEN);
5911 else
5912 os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005913 /* FIX: IBSS mode */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005914 nlmode = params->p2p ?
5915 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
5916 if (drv->nlmode != nlmode &&
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005917 wpa_driver_nl80211_set_mode(bss, nlmode) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005918 return -1;
5919
5920retry:
5921 msg = nlmsg_alloc();
5922 if (!msg)
5923 return -1;
5924
5925 wpa_printf(MSG_DEBUG, "nl80211: Authenticate (ifindex=%d)",
5926 drv->ifindex);
5927
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005928 nl80211_cmd(drv, msg, 0, NL80211_CMD_AUTHENTICATE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005929
5930 for (i = 0; i < 4; i++) {
5931 if (!params->wep_key[i])
5932 continue;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005933 wpa_driver_nl80211_set_key(bss->ifname, bss, WPA_ALG_WEP,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005934 NULL, i,
5935 i == params->wep_tx_keyidx, NULL, 0,
5936 params->wep_key[i],
5937 params->wep_key_len[i]);
5938 if (params->wep_tx_keyidx != i)
5939 continue;
5940 if (nl_add_key(msg, WPA_ALG_WEP, i, 1, NULL, 0,
5941 params->wep_key[i], params->wep_key_len[i])) {
5942 nlmsg_free(msg);
5943 return -1;
5944 }
5945 }
5946
5947 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
5948 if (params->bssid) {
5949 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
5950 MAC2STR(params->bssid));
5951 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
5952 }
5953 if (params->freq) {
5954 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
5955 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
5956 }
5957 if (params->ssid) {
5958 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
5959 params->ssid, params->ssid_len);
5960 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
5961 params->ssid);
5962 }
5963 wpa_hexdump(MSG_DEBUG, " * IEs", params->ie, params->ie_len);
5964 if (params->ie)
5965 NLA_PUT(msg, NL80211_ATTR_IE, params->ie_len, params->ie);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005966 if (params->sae_data) {
5967 wpa_hexdump(MSG_DEBUG, " * SAE data", params->sae_data,
5968 params->sae_data_len);
5969 NLA_PUT(msg, NL80211_ATTR_SAE_DATA, params->sae_data_len,
5970 params->sae_data);
5971 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005972 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
5973 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
5974 else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
5975 type = NL80211_AUTHTYPE_SHARED_KEY;
5976 else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
5977 type = NL80211_AUTHTYPE_NETWORK_EAP;
5978 else if (params->auth_alg & WPA_AUTH_ALG_FT)
5979 type = NL80211_AUTHTYPE_FT;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005980 else if (params->auth_alg & WPA_AUTH_ALG_SAE)
5981 type = NL80211_AUTHTYPE_SAE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005982 else
5983 goto nla_put_failure;
5984 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
5985 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
5986 if (params->local_state_change) {
5987 wpa_printf(MSG_DEBUG, " * Local state change only");
5988 NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE);
5989 }
5990
5991 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5992 msg = NULL;
5993 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005994 wpa_dbg(drv->ctx, MSG_DEBUG,
5995 "nl80211: MLME command failed (auth): ret=%d (%s)",
5996 ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005997 count++;
5998 if (ret == -EALREADY && count == 1 && params->bssid &&
5999 !params->local_state_change) {
6000 /*
6001 * mac80211 does not currently accept new
6002 * authentication if we are already authenticated. As a
6003 * workaround, force deauthentication and try again.
6004 */
6005 wpa_printf(MSG_DEBUG, "nl80211: Retry authentication "
6006 "after forced deauthentication");
6007 wpa_driver_nl80211_deauthenticate(
6008 bss, params->bssid,
6009 WLAN_REASON_PREV_AUTH_NOT_VALID);
6010 nlmsg_free(msg);
6011 goto retry;
6012 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006013
6014 if (ret == -ENOENT && params->freq && !is_retry) {
6015 /*
6016 * cfg80211 has likely expired the BSS entry even
6017 * though it was previously available in our internal
6018 * BSS table. To recover quickly, start a single
6019 * channel scan on the specified channel.
6020 */
6021 struct wpa_driver_scan_params scan;
6022 int freqs[2];
6023
6024 os_memset(&scan, 0, sizeof(scan));
6025 scan.num_ssids = 1;
6026 if (params->ssid) {
6027 scan.ssids[0].ssid = params->ssid;
6028 scan.ssids[0].ssid_len = params->ssid_len;
6029 }
6030 freqs[0] = params->freq;
6031 freqs[1] = 0;
6032 scan.freqs = freqs;
6033 wpa_printf(MSG_DEBUG, "nl80211: Trigger single "
6034 "channel scan to refresh cfg80211 BSS "
6035 "entry");
6036 ret = wpa_driver_nl80211_scan(bss, &scan);
6037 if (ret == 0) {
6038 nl80211_copy_auth_params(drv, params);
6039 drv->scan_for_auth = 1;
6040 }
6041 } else if (is_retry) {
6042 /*
6043 * Need to indicate this with an event since the return
6044 * value from the retry is not delivered to core code.
6045 */
6046 union wpa_event_data event;
6047 wpa_printf(MSG_DEBUG, "nl80211: Authentication retry "
6048 "failed");
6049 os_memset(&event, 0, sizeof(event));
6050 os_memcpy(event.timeout_event.addr, drv->auth_bssid_,
6051 ETH_ALEN);
6052 wpa_supplicant_event(drv->ctx, EVENT_AUTH_TIMED_OUT,
6053 &event);
6054 }
6055
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006056 goto nla_put_failure;
6057 }
6058 ret = 0;
6059 wpa_printf(MSG_DEBUG, "nl80211: Authentication request send "
6060 "successfully");
6061
6062nla_put_failure:
6063 nlmsg_free(msg);
6064 return ret;
6065}
6066
6067
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006068static int wpa_driver_nl80211_authenticate_retry(
6069 struct wpa_driver_nl80211_data *drv)
6070{
6071 struct wpa_driver_auth_params params;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006072 struct i802_bss *bss = drv->first_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006073 int i;
6074
6075 wpa_printf(MSG_DEBUG, "nl80211: Try to authenticate again");
6076
6077 os_memset(&params, 0, sizeof(params));
6078 params.freq = drv->auth_freq;
6079 params.auth_alg = drv->auth_alg;
6080 params.wep_tx_keyidx = drv->auth_wep_tx_keyidx;
6081 params.local_state_change = drv->auth_local_state_change;
6082 params.p2p = drv->auth_p2p;
6083
6084 if (!is_zero_ether_addr(drv->auth_bssid_))
6085 params.bssid = drv->auth_bssid_;
6086
6087 if (drv->auth_ssid_len) {
6088 params.ssid = drv->auth_ssid;
6089 params.ssid_len = drv->auth_ssid_len;
6090 }
6091
6092 params.ie = drv->auth_ie;
6093 params.ie_len = drv->auth_ie_len;
6094
6095 for (i = 0; i < 4; i++) {
6096 if (drv->auth_wep_key_len[i]) {
6097 params.wep_key[i] = drv->auth_wep_key[i];
6098 params.wep_key_len[i] = drv->auth_wep_key_len[i];
6099 }
6100 }
6101
6102 drv->retry_auth = 1;
6103 return wpa_driver_nl80211_authenticate(bss, &params);
6104}
6105
6106
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006107struct phy_info_arg {
6108 u16 *num_modes;
6109 struct hostapd_hw_modes *modes;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006110 int last_mode, last_chan_idx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006111};
6112
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006113static void phy_info_ht_capa(struct hostapd_hw_modes *mode, struct nlattr *capa,
6114 struct nlattr *ampdu_factor,
6115 struct nlattr *ampdu_density,
6116 struct nlattr *mcs_set)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006117{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006118 if (capa)
6119 mode->ht_capab = nla_get_u16(capa);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006120
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006121 if (ampdu_factor)
6122 mode->a_mpdu_params |= nla_get_u8(ampdu_factor) & 0x03;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006123
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006124 if (ampdu_density)
6125 mode->a_mpdu_params |= nla_get_u8(ampdu_density) << 2;
6126
6127 if (mcs_set && nla_len(mcs_set) >= 16) {
6128 u8 *mcs;
6129 mcs = nla_data(mcs_set);
6130 os_memcpy(mode->mcs_set, mcs, 16);
6131 }
6132}
6133
6134
6135static void phy_info_vht_capa(struct hostapd_hw_modes *mode,
6136 struct nlattr *capa,
6137 struct nlattr *mcs_set)
6138{
6139 if (capa)
6140 mode->vht_capab = nla_get_u32(capa);
6141
6142 if (mcs_set && nla_len(mcs_set) >= 8) {
6143 u8 *mcs;
6144 mcs = nla_data(mcs_set);
6145 os_memcpy(mode->vht_mcs_set, mcs, 8);
6146 }
6147}
6148
6149
6150static void phy_info_freq(struct hostapd_hw_modes *mode,
6151 struct hostapd_channel_data *chan,
6152 struct nlattr *tb_freq[])
6153{
Dmitry Shmidt4b060592013-04-29 16:42:49 -07006154 u8 channel;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006155 chan->freq = nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_FREQ]);
6156 chan->flag = 0;
Dmitry Shmidt4b060592013-04-29 16:42:49 -07006157 if (ieee80211_freq_to_chan(chan->freq, &channel) != NUM_HOSTAPD_MODES)
6158 chan->chan = channel;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006159
6160 if (tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
6161 chan->flag |= HOSTAPD_CHAN_DISABLED;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006162 if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IR])
6163 chan->flag |= HOSTAPD_CHAN_PASSIVE_SCAN | HOSTAPD_CHAN_NO_IBSS;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006164 if (tb_freq[NL80211_FREQUENCY_ATTR_RADAR])
6165 chan->flag |= HOSTAPD_CHAN_RADAR;
6166
Dmitry Shmidtea69e842013-05-13 14:52:28 -07006167 if (tb_freq[NL80211_FREQUENCY_ATTR_DFS_STATE]) {
6168 enum nl80211_dfs_state state =
6169 nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_DFS_STATE]);
6170
6171 switch (state) {
6172 case NL80211_DFS_USABLE:
6173 chan->flag |= HOSTAPD_CHAN_DFS_USABLE;
6174 break;
6175 case NL80211_DFS_AVAILABLE:
6176 chan->flag |= HOSTAPD_CHAN_DFS_AVAILABLE;
6177 break;
6178 case NL80211_DFS_UNAVAILABLE:
6179 chan->flag |= HOSTAPD_CHAN_DFS_UNAVAILABLE;
6180 break;
6181 }
6182 }
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006183}
6184
6185
6186static int phy_info_freqs(struct phy_info_arg *phy_info,
6187 struct hostapd_hw_modes *mode, struct nlattr *tb)
6188{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006189 static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
6190 [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
6191 [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006192 [NL80211_FREQUENCY_ATTR_NO_IR] = { .type = NLA_FLAG },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006193 [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
6194 [NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32 },
Dmitry Shmidtea69e842013-05-13 14:52:28 -07006195 [NL80211_FREQUENCY_ATTR_DFS_STATE] = { .type = NLA_U32 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006196 };
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006197 int new_channels = 0;
6198 struct hostapd_channel_data *channel;
6199 struct nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006200 struct nlattr *nl_freq;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006201 int rem_freq, idx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006202
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006203 if (tb == NULL)
6204 return NL_OK;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006205
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006206 nla_for_each_nested(nl_freq, tb, rem_freq) {
6207 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX,
6208 nla_data(nl_freq), nla_len(nl_freq), freq_policy);
6209 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
6210 continue;
6211 new_channels++;
6212 }
6213
6214 channel = os_realloc_array(mode->channels,
6215 mode->num_channels + new_channels,
6216 sizeof(struct hostapd_channel_data));
6217 if (!channel)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006218 return NL_SKIP;
6219
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006220 mode->channels = channel;
6221 mode->num_channels += new_channels;
6222
6223 idx = phy_info->last_chan_idx;
6224
6225 nla_for_each_nested(nl_freq, tb, rem_freq) {
6226 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX,
6227 nla_data(nl_freq), nla_len(nl_freq), freq_policy);
6228 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
6229 continue;
6230 phy_info_freq(mode, &mode->channels[idx], tb_freq);
6231 idx++;
6232 }
6233 phy_info->last_chan_idx = idx;
6234
6235 return NL_OK;
6236}
6237
6238
6239static int phy_info_rates(struct hostapd_hw_modes *mode, struct nlattr *tb)
6240{
6241 static struct nla_policy rate_policy[NL80211_BITRATE_ATTR_MAX + 1] = {
6242 [NL80211_BITRATE_ATTR_RATE] = { .type = NLA_U32 },
6243 [NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE] =
6244 { .type = NLA_FLAG },
6245 };
6246 struct nlattr *tb_rate[NL80211_BITRATE_ATTR_MAX + 1];
6247 struct nlattr *nl_rate;
6248 int rem_rate, idx;
6249
6250 if (tb == NULL)
6251 return NL_OK;
6252
6253 nla_for_each_nested(nl_rate, tb, rem_rate) {
6254 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX,
6255 nla_data(nl_rate), nla_len(nl_rate),
6256 rate_policy);
6257 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
6258 continue;
6259 mode->num_rates++;
6260 }
6261
6262 mode->rates = os_calloc(mode->num_rates, sizeof(int));
6263 if (!mode->rates)
6264 return NL_SKIP;
6265
6266 idx = 0;
6267
6268 nla_for_each_nested(nl_rate, tb, rem_rate) {
6269 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX,
6270 nla_data(nl_rate), nla_len(nl_rate),
6271 rate_policy);
6272 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
6273 continue;
6274 mode->rates[idx] = nla_get_u32(
6275 tb_rate[NL80211_BITRATE_ATTR_RATE]);
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006276 idx++;
6277 }
6278
6279 return NL_OK;
6280}
6281
6282
6283static int phy_info_band(struct phy_info_arg *phy_info, struct nlattr *nl_band)
6284{
6285 struct nlattr *tb_band[NL80211_BAND_ATTR_MAX + 1];
6286 struct hostapd_hw_modes *mode;
6287 int ret;
6288
6289 if (phy_info->last_mode != nl_band->nla_type) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006290 mode = os_realloc_array(phy_info->modes,
6291 *phy_info->num_modes + 1,
6292 sizeof(*mode));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006293 if (!mode)
6294 return NL_SKIP;
6295 phy_info->modes = mode;
6296
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006297 mode = &phy_info->modes[*(phy_info->num_modes)];
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006298 os_memset(mode, 0, sizeof(*mode));
6299 mode->mode = NUM_HOSTAPD_MODES;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006300 mode->flags = HOSTAPD_MODE_FLAG_HT_INFO_KNOWN |
6301 HOSTAPD_MODE_FLAG_VHT_INFO_KNOWN;
6302
6303 /*
6304 * Unsupported VHT MCS stream is defined as value 3, so the VHT
6305 * MCS RX/TX map must be initialized with 0xffff to mark all 8
6306 * possible streams as unsupported. This will be overridden if
6307 * driver advertises VHT support.
6308 */
6309 mode->vht_mcs_set[0] = 0xff;
6310 mode->vht_mcs_set[1] = 0xff;
6311 mode->vht_mcs_set[4] = 0xff;
6312 mode->vht_mcs_set[5] = 0xff;
6313
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006314 *(phy_info->num_modes) += 1;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006315 phy_info->last_mode = nl_band->nla_type;
6316 phy_info->last_chan_idx = 0;
6317 } else
6318 mode = &phy_info->modes[*(phy_info->num_modes) - 1];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006319
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006320 nla_parse(tb_band, NL80211_BAND_ATTR_MAX, nla_data(nl_band),
6321 nla_len(nl_band), NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006322
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006323 phy_info_ht_capa(mode, tb_band[NL80211_BAND_ATTR_HT_CAPA],
6324 tb_band[NL80211_BAND_ATTR_HT_AMPDU_FACTOR],
6325 tb_band[NL80211_BAND_ATTR_HT_AMPDU_DENSITY],
6326 tb_band[NL80211_BAND_ATTR_HT_MCS_SET]);
6327 phy_info_vht_capa(mode, tb_band[NL80211_BAND_ATTR_VHT_CAPA],
6328 tb_band[NL80211_BAND_ATTR_VHT_MCS_SET]);
6329 ret = phy_info_freqs(phy_info, mode, tb_band[NL80211_BAND_ATTR_FREQS]);
6330 if (ret != NL_OK)
6331 return ret;
6332 ret = phy_info_rates(mode, tb_band[NL80211_BAND_ATTR_RATES]);
6333 if (ret != NL_OK)
6334 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006335
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006336 return NL_OK;
6337}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006338
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006339
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006340static int phy_info_handler(struct nl_msg *msg, void *arg)
6341{
6342 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
6343 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6344 struct phy_info_arg *phy_info = arg;
6345 struct nlattr *nl_band;
6346 int rem_band;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006347
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006348 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6349 genlmsg_attrlen(gnlh, 0), NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006350
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006351 if (!tb_msg[NL80211_ATTR_WIPHY_BANDS])
6352 return NL_SKIP;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006353
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006354 nla_for_each_nested(nl_band, tb_msg[NL80211_ATTR_WIPHY_BANDS], rem_band)
6355 {
6356 int res = phy_info_band(phy_info, nl_band);
6357 if (res != NL_OK)
6358 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006359 }
6360
6361 return NL_SKIP;
6362}
6363
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006364
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006365static struct hostapd_hw_modes *
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07006366wpa_driver_nl80211_postprocess_modes(struct hostapd_hw_modes *modes,
6367 u16 *num_modes)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006368{
6369 u16 m;
6370 struct hostapd_hw_modes *mode11g = NULL, *nmodes, *mode;
6371 int i, mode11g_idx = -1;
6372
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07006373 /* heuristic to set up modes */
6374 for (m = 0; m < *num_modes; m++) {
6375 if (!modes[m].num_channels)
6376 continue;
6377 if (modes[m].channels[0].freq < 4000) {
6378 modes[m].mode = HOSTAPD_MODE_IEEE80211B;
6379 for (i = 0; i < modes[m].num_rates; i++) {
6380 if (modes[m].rates[i] > 200) {
6381 modes[m].mode = HOSTAPD_MODE_IEEE80211G;
6382 break;
6383 }
6384 }
6385 } else if (modes[m].channels[0].freq > 50000)
6386 modes[m].mode = HOSTAPD_MODE_IEEE80211AD;
6387 else
6388 modes[m].mode = HOSTAPD_MODE_IEEE80211A;
6389 }
6390
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006391 /* If only 802.11g mode is included, use it to construct matching
6392 * 802.11b mode data. */
6393
6394 for (m = 0; m < *num_modes; m++) {
6395 if (modes[m].mode == HOSTAPD_MODE_IEEE80211B)
6396 return modes; /* 802.11b already included */
6397 if (modes[m].mode == HOSTAPD_MODE_IEEE80211G)
6398 mode11g_idx = m;
6399 }
6400
6401 if (mode11g_idx < 0)
6402 return modes; /* 2.4 GHz band not supported at all */
6403
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006404 nmodes = os_realloc_array(modes, *num_modes + 1, sizeof(*nmodes));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006405 if (nmodes == NULL)
6406 return modes; /* Could not add 802.11b mode */
6407
6408 mode = &nmodes[*num_modes];
6409 os_memset(mode, 0, sizeof(*mode));
6410 (*num_modes)++;
6411 modes = nmodes;
6412
6413 mode->mode = HOSTAPD_MODE_IEEE80211B;
6414
6415 mode11g = &modes[mode11g_idx];
6416 mode->num_channels = mode11g->num_channels;
6417 mode->channels = os_malloc(mode11g->num_channels *
6418 sizeof(struct hostapd_channel_data));
6419 if (mode->channels == NULL) {
6420 (*num_modes)--;
6421 return modes; /* Could not add 802.11b mode */
6422 }
6423 os_memcpy(mode->channels, mode11g->channels,
6424 mode11g->num_channels * sizeof(struct hostapd_channel_data));
6425
6426 mode->num_rates = 0;
6427 mode->rates = os_malloc(4 * sizeof(int));
6428 if (mode->rates == NULL) {
6429 os_free(mode->channels);
6430 (*num_modes)--;
6431 return modes; /* Could not add 802.11b mode */
6432 }
6433
6434 for (i = 0; i < mode11g->num_rates; i++) {
6435 if (mode11g->rates[i] != 10 && mode11g->rates[i] != 20 &&
6436 mode11g->rates[i] != 55 && mode11g->rates[i] != 110)
6437 continue;
6438 mode->rates[mode->num_rates] = mode11g->rates[i];
6439 mode->num_rates++;
6440 if (mode->num_rates == 4)
6441 break;
6442 }
6443
6444 if (mode->num_rates == 0) {
6445 os_free(mode->channels);
6446 os_free(mode->rates);
6447 (*num_modes)--;
6448 return modes; /* No 802.11b rates */
6449 }
6450
6451 wpa_printf(MSG_DEBUG, "nl80211: Added 802.11b mode based on 802.11g "
6452 "information");
6453
6454 return modes;
6455}
6456
6457
6458static void nl80211_set_ht40_mode(struct hostapd_hw_modes *mode, int start,
6459 int end)
6460{
6461 int c;
6462
6463 for (c = 0; c < mode->num_channels; c++) {
6464 struct hostapd_channel_data *chan = &mode->channels[c];
6465 if (chan->freq - 10 >= start && chan->freq + 10 <= end)
6466 chan->flag |= HOSTAPD_CHAN_HT40;
6467 }
6468}
6469
6470
6471static void nl80211_set_ht40_mode_sec(struct hostapd_hw_modes *mode, int start,
6472 int end)
6473{
6474 int c;
6475
6476 for (c = 0; c < mode->num_channels; c++) {
6477 struct hostapd_channel_data *chan = &mode->channels[c];
6478 if (!(chan->flag & HOSTAPD_CHAN_HT40))
6479 continue;
6480 if (chan->freq - 30 >= start && chan->freq - 10 <= end)
6481 chan->flag |= HOSTAPD_CHAN_HT40MINUS;
6482 if (chan->freq + 10 >= start && chan->freq + 30 <= end)
6483 chan->flag |= HOSTAPD_CHAN_HT40PLUS;
6484 }
6485}
6486
6487
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006488static void nl80211_reg_rule_max_eirp(u32 start, u32 end, u32 max_eirp,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006489 struct phy_info_arg *results)
6490{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006491 u16 m;
6492
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006493 for (m = 0; m < *results->num_modes; m++) {
6494 int c;
6495 struct hostapd_hw_modes *mode = &results->modes[m];
6496
6497 for (c = 0; c < mode->num_channels; c++) {
6498 struct hostapd_channel_data *chan = &mode->channels[c];
6499 if ((u32) chan->freq - 10 >= start &&
6500 (u32) chan->freq + 10 <= end)
6501 chan->max_tx_power = max_eirp;
6502 }
6503 }
6504}
6505
6506
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006507static void nl80211_reg_rule_ht40(u32 start, u32 end,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006508 struct phy_info_arg *results)
6509{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006510 u16 m;
6511
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006512 for (m = 0; m < *results->num_modes; m++) {
6513 if (!(results->modes[m].ht_capab &
6514 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
6515 continue;
6516 nl80211_set_ht40_mode(&results->modes[m], start, end);
6517 }
6518}
6519
6520
6521static void nl80211_reg_rule_sec(struct nlattr *tb[],
6522 struct phy_info_arg *results)
6523{
6524 u32 start, end, max_bw;
6525 u16 m;
6526
6527 if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
6528 tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
6529 tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
6530 return;
6531
6532 start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
6533 end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
6534 max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
6535
6536 if (max_bw < 20)
6537 return;
6538
6539 for (m = 0; m < *results->num_modes; m++) {
6540 if (!(results->modes[m].ht_capab &
6541 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
6542 continue;
6543 nl80211_set_ht40_mode_sec(&results->modes[m], start, end);
6544 }
6545}
6546
6547
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006548static void nl80211_set_vht_mode(struct hostapd_hw_modes *mode, int start,
6549 int end)
6550{
6551 int c;
6552
6553 for (c = 0; c < mode->num_channels; c++) {
6554 struct hostapd_channel_data *chan = &mode->channels[c];
6555 if (chan->freq - 10 >= start && chan->freq + 70 <= end)
6556 chan->flag |= HOSTAPD_CHAN_VHT_10_70;
6557
6558 if (chan->freq - 30 >= start && chan->freq + 50 <= end)
6559 chan->flag |= HOSTAPD_CHAN_VHT_30_50;
6560
6561 if (chan->freq - 50 >= start && chan->freq + 30 <= end)
6562 chan->flag |= HOSTAPD_CHAN_VHT_50_30;
6563
6564 if (chan->freq - 70 >= start && chan->freq + 10 <= end)
6565 chan->flag |= HOSTAPD_CHAN_VHT_70_10;
6566 }
6567}
6568
6569
6570static void nl80211_reg_rule_vht(struct nlattr *tb[],
6571 struct phy_info_arg *results)
6572{
6573 u32 start, end, max_bw;
6574 u16 m;
6575
6576 if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
6577 tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
6578 tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
6579 return;
6580
6581 start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
6582 end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
6583 max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
6584
6585 if (max_bw < 80)
6586 return;
6587
6588 for (m = 0; m < *results->num_modes; m++) {
6589 if (!(results->modes[m].ht_capab &
6590 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
6591 continue;
6592 /* TODO: use a real VHT support indication */
6593 if (!results->modes[m].vht_capab)
6594 continue;
6595
6596 nl80211_set_vht_mode(&results->modes[m], start, end);
6597 }
6598}
6599
6600
Dmitry Shmidt97672262014-02-03 13:02:54 -08006601static const char * dfs_domain_name(enum nl80211_dfs_regions region)
6602{
6603 switch (region) {
6604 case NL80211_DFS_UNSET:
6605 return "DFS-UNSET";
6606 case NL80211_DFS_FCC:
6607 return "DFS-FCC";
6608 case NL80211_DFS_ETSI:
6609 return "DFS-ETSI";
6610 case NL80211_DFS_JP:
6611 return "DFS-JP";
6612 default:
6613 return "DFS-invalid";
6614 }
6615}
6616
6617
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006618static int nl80211_get_reg(struct nl_msg *msg, void *arg)
6619{
6620 struct phy_info_arg *results = arg;
6621 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
6622 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6623 struct nlattr *nl_rule;
6624 struct nlattr *tb_rule[NL80211_FREQUENCY_ATTR_MAX + 1];
6625 int rem_rule;
6626 static struct nla_policy reg_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
6627 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
6628 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
6629 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
6630 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
6631 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
6632 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
6633 };
6634
6635 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6636 genlmsg_attrlen(gnlh, 0), NULL);
6637 if (!tb_msg[NL80211_ATTR_REG_ALPHA2] ||
6638 !tb_msg[NL80211_ATTR_REG_RULES]) {
6639 wpa_printf(MSG_DEBUG, "nl80211: No regulatory information "
6640 "available");
6641 return NL_SKIP;
6642 }
6643
Dmitry Shmidt97672262014-02-03 13:02:54 -08006644 if (tb_msg[NL80211_ATTR_DFS_REGION]) {
6645 enum nl80211_dfs_regions dfs_domain;
6646 dfs_domain = nla_get_u8(tb_msg[NL80211_ATTR_DFS_REGION]);
6647 wpa_printf(MSG_DEBUG, "nl80211: Regulatory information - country=%s (%s)",
6648 (char *) nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]),
6649 dfs_domain_name(dfs_domain));
6650 } else {
6651 wpa_printf(MSG_DEBUG, "nl80211: Regulatory information - country=%s",
6652 (char *) nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]));
6653 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006654
6655 nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
6656 {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08006657 u32 start, end, max_eirp = 0, max_bw = 0, flags = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006658 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
6659 nla_data(nl_rule), nla_len(nl_rule), reg_policy);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006660 if (tb_rule[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
6661 tb_rule[NL80211_ATTR_FREQ_RANGE_END] == NULL)
6662 continue;
6663 start = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
6664 end = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
6665 if (tb_rule[NL80211_ATTR_POWER_RULE_MAX_EIRP])
6666 max_eirp = nla_get_u32(tb_rule[NL80211_ATTR_POWER_RULE_MAX_EIRP]) / 100;
6667 if (tb_rule[NL80211_ATTR_FREQ_RANGE_MAX_BW])
6668 max_bw = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08006669 if (tb_rule[NL80211_ATTR_REG_RULE_FLAGS])
6670 flags = nla_get_u32(tb_rule[NL80211_ATTR_REG_RULE_FLAGS]);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006671
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08006672 wpa_printf(MSG_DEBUG, "nl80211: %u-%u @ %u MHz %u mBm%s%s%s%s%s%s%s%s",
6673 start, end, max_bw, max_eirp,
6674 flags & NL80211_RRF_NO_OFDM ? " (no OFDM)" : "",
6675 flags & NL80211_RRF_NO_CCK ? " (no CCK)" : "",
6676 flags & NL80211_RRF_NO_INDOOR ? " (no indoor)" : "",
6677 flags & NL80211_RRF_NO_OUTDOOR ? " (no outdoor)" :
6678 "",
6679 flags & NL80211_RRF_DFS ? " (DFS)" : "",
6680 flags & NL80211_RRF_PTP_ONLY ? " (PTP only)" : "",
6681 flags & NL80211_RRF_PTMP_ONLY ? " (PTMP only)" : "",
6682 flags & NL80211_RRF_NO_IR ? " (no IR)" : "");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006683 if (max_bw >= 40)
6684 nl80211_reg_rule_ht40(start, end, results);
6685 if (tb_rule[NL80211_ATTR_POWER_RULE_MAX_EIRP])
6686 nl80211_reg_rule_max_eirp(start, end, max_eirp,
6687 results);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006688 }
6689
6690 nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
6691 {
6692 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
6693 nla_data(nl_rule), nla_len(nl_rule), reg_policy);
6694 nl80211_reg_rule_sec(tb_rule, results);
6695 }
6696
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006697 nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
6698 {
6699 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
6700 nla_data(nl_rule), nla_len(nl_rule), reg_policy);
6701 nl80211_reg_rule_vht(tb_rule, results);
6702 }
6703
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006704 return NL_SKIP;
6705}
6706
6707
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006708static int nl80211_set_regulatory_flags(struct wpa_driver_nl80211_data *drv,
6709 struct phy_info_arg *results)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006710{
6711 struct nl_msg *msg;
6712
6713 msg = nlmsg_alloc();
6714 if (!msg)
6715 return -ENOMEM;
6716
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006717 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006718 return send_and_recv_msgs(drv, msg, nl80211_get_reg, results);
6719}
6720
6721
6722static struct hostapd_hw_modes *
6723wpa_driver_nl80211_get_hw_feature_data(void *priv, u16 *num_modes, u16 *flags)
6724{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006725 u32 feat;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006726 struct i802_bss *bss = priv;
6727 struct wpa_driver_nl80211_data *drv = bss->drv;
6728 struct nl_msg *msg;
6729 struct phy_info_arg result = {
6730 .num_modes = num_modes,
6731 .modes = NULL,
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006732 .last_mode = -1,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006733 };
6734
6735 *num_modes = 0;
6736 *flags = 0;
6737
6738 msg = nlmsg_alloc();
6739 if (!msg)
6740 return NULL;
6741
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006742 feat = get_nl80211_protocol_features(drv);
6743 if (feat & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP)
6744 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_WIPHY);
6745 else
6746 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006747
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006748 NLA_PUT_FLAG(msg, NL80211_ATTR_SPLIT_WIPHY_DUMP);
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08006749 if (nl80211_set_iface_id(msg, bss) < 0)
6750 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006751
6752 if (send_and_recv_msgs(drv, msg, phy_info_handler, &result) == 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006753 nl80211_set_regulatory_flags(drv, &result);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07006754 return wpa_driver_nl80211_postprocess_modes(result.modes,
6755 num_modes);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006756 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006757 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006758 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006759 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006760 return NULL;
6761}
6762
6763
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006764static int wpa_driver_nl80211_send_mntr(struct wpa_driver_nl80211_data *drv,
6765 const void *data, size_t len,
6766 int encrypt, int noack)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006767{
6768 __u8 rtap_hdr[] = {
6769 0x00, 0x00, /* radiotap version */
6770 0x0e, 0x00, /* radiotap length */
6771 0x02, 0xc0, 0x00, 0x00, /* bmap: flags, tx and rx flags */
6772 IEEE80211_RADIOTAP_F_FRAG, /* F_FRAG (fragment if required) */
6773 0x00, /* padding */
6774 0x00, 0x00, /* RX and TX flags to indicate that */
6775 0x00, 0x00, /* this is the injected frame directly */
6776 };
6777 struct iovec iov[2] = {
6778 {
6779 .iov_base = &rtap_hdr,
6780 .iov_len = sizeof(rtap_hdr),
6781 },
6782 {
6783 .iov_base = (void *) data,
6784 .iov_len = len,
6785 }
6786 };
6787 struct msghdr msg = {
6788 .msg_name = NULL,
6789 .msg_namelen = 0,
6790 .msg_iov = iov,
6791 .msg_iovlen = 2,
6792 .msg_control = NULL,
6793 .msg_controllen = 0,
6794 .msg_flags = 0,
6795 };
6796 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006797 u16 txflags = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006798
6799 if (encrypt)
6800 rtap_hdr[8] |= IEEE80211_RADIOTAP_F_WEP;
6801
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07006802 if (drv->monitor_sock < 0) {
6803 wpa_printf(MSG_DEBUG, "nl80211: No monitor socket available "
6804 "for %s", __func__);
6805 return -1;
6806 }
6807
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006808 if (noack)
6809 txflags |= IEEE80211_RADIOTAP_F_TX_NOACK;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006810 WPA_PUT_LE16(&rtap_hdr[12], txflags);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006811
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006812 res = sendmsg(drv->monitor_sock, &msg, 0);
6813 if (res < 0) {
6814 wpa_printf(MSG_INFO, "nl80211: sendmsg: %s", strerror(errno));
6815 return -1;
6816 }
6817 return 0;
6818}
6819
6820
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006821static int wpa_driver_nl80211_send_frame(struct i802_bss *bss,
6822 const void *data, size_t len,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006823 int encrypt, int noack,
6824 unsigned int freq, int no_cck,
6825 int offchanok, unsigned int wait_time)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006826{
6827 struct wpa_driver_nl80211_data *drv = bss->drv;
6828 u64 cookie;
Dmitry Shmidt051af732013-10-22 13:52:46 -07006829 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006830
Dmitry Shmidt56052862013-10-04 10:23:25 -07006831 if (freq == 0) {
6832 wpa_printf(MSG_DEBUG, "nl80211: send_frame - Use bss->freq=%u",
6833 bss->freq);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006834 freq = bss->freq;
Dmitry Shmidt56052862013-10-04 10:23:25 -07006835 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006836
Dmitry Shmidt56052862013-10-04 10:23:25 -07006837 if (drv->use_monitor) {
6838 wpa_printf(MSG_DEBUG, "nl80211: send_frame(freq=%u bss->freq=%u) -> send_mntr",
6839 freq, bss->freq);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006840 return wpa_driver_nl80211_send_mntr(drv, data, len,
6841 encrypt, noack);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006842 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006843
Dmitry Shmidt56052862013-10-04 10:23:25 -07006844 wpa_printf(MSG_DEBUG, "nl80211: send_frame -> send_frame_cmd");
Dmitry Shmidt051af732013-10-22 13:52:46 -07006845 res = nl80211_send_frame_cmd(bss, freq, wait_time, data, len,
6846 &cookie, no_cck, noack, offchanok);
6847 if (res == 0 && !noack) {
6848 const struct ieee80211_mgmt *mgmt;
6849 u16 fc;
6850
6851 mgmt = (const struct ieee80211_mgmt *) data;
6852 fc = le_to_host16(mgmt->frame_control);
6853 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
6854 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_ACTION) {
6855 wpa_printf(MSG_MSGDUMP,
6856 "nl80211: Update send_action_cookie from 0x%llx to 0x%llx",
6857 (long long unsigned int)
6858 drv->send_action_cookie,
6859 (long long unsigned int) cookie);
6860 drv->send_action_cookie = cookie;
6861 }
6862 }
6863
6864 return res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006865}
6866
6867
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006868static int wpa_driver_nl80211_send_mlme(struct i802_bss *bss, const u8 *data,
6869 size_t data_len, int noack,
6870 unsigned int freq, int no_cck,
6871 int offchanok,
6872 unsigned int wait_time)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006873{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006874 struct wpa_driver_nl80211_data *drv = bss->drv;
6875 struct ieee80211_mgmt *mgmt;
6876 int encrypt = 1;
6877 u16 fc;
6878
6879 mgmt = (struct ieee80211_mgmt *) data;
6880 fc = le_to_host16(mgmt->frame_control);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006881 wpa_printf(MSG_DEBUG, "nl80211: send_mlme - noack=%d freq=%u no_cck=%d offchanok=%d wait_time=%u fc=0x%x nlmode=%d",
6882 noack, freq, no_cck, offchanok, wait_time, fc, drv->nlmode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006883
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006884 if ((is_sta_interface(drv->nlmode) ||
6885 drv->nlmode == NL80211_IFTYPE_P2P_DEVICE) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006886 WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
6887 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_RESP) {
6888 /*
6889 * The use of last_mgmt_freq is a bit of a hack,
6890 * but it works due to the single-threaded nature
6891 * of wpa_supplicant.
6892 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07006893 if (freq == 0) {
6894 wpa_printf(MSG_DEBUG, "nl80211: Use last_mgmt_freq=%d",
6895 drv->last_mgmt_freq);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006896 freq = drv->last_mgmt_freq;
Dmitry Shmidt56052862013-10-04 10:23:25 -07006897 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006898 return nl80211_send_frame_cmd(bss, freq, 0,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006899 data, data_len, NULL, 1, noack,
6900 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006901 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006902
6903 if (drv->device_ap_sme && is_ap_interface(drv->nlmode)) {
Dmitry Shmidt56052862013-10-04 10:23:25 -07006904 if (freq == 0) {
6905 wpa_printf(MSG_DEBUG, "nl80211: Use bss->freq=%d",
6906 bss->freq);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006907 freq = bss->freq;
Dmitry Shmidt56052862013-10-04 10:23:25 -07006908 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07006909 return nl80211_send_frame_cmd(bss, freq,
6910 (int) freq == bss->freq ? 0 :
6911 wait_time,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006912 data, data_len,
6913 &drv->send_action_cookie,
6914 no_cck, noack, offchanok);
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07006915 }
Dmitry Shmidtb638fe72012-03-20 12:51:25 -07006916
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006917 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
6918 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) {
6919 /*
6920 * Only one of the authentication frame types is encrypted.
6921 * In order for static WEP encryption to work properly (i.e.,
6922 * to not encrypt the frame), we need to tell mac80211 about
6923 * the frames that must not be encrypted.
6924 */
6925 u16 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
6926 u16 auth_trans = le_to_host16(mgmt->u.auth.auth_transaction);
6927 if (auth_alg != WLAN_AUTH_SHARED_KEY || auth_trans != 3)
6928 encrypt = 0;
6929 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006930
Dmitry Shmidt56052862013-10-04 10:23:25 -07006931 wpa_printf(MSG_DEBUG, "nl80211: send_mlme -> send_frame");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006932 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006933 noack, freq, no_cck, offchanok,
6934 wait_time);
6935}
6936
6937
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006938static int nl80211_set_bss(struct i802_bss *bss, int cts, int preamble,
6939 int slot, int ht_opmode, int ap_isolate,
6940 int *basic_rates)
6941{
6942 struct wpa_driver_nl80211_data *drv = bss->drv;
6943 struct nl_msg *msg;
6944
6945 msg = nlmsg_alloc();
6946 if (!msg)
6947 return -ENOMEM;
6948
6949 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_BSS);
6950
6951 if (cts >= 0)
6952 NLA_PUT_U8(msg, NL80211_ATTR_BSS_CTS_PROT, cts);
6953 if (preamble >= 0)
6954 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble);
6955 if (slot >= 0)
6956 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot);
6957 if (ht_opmode >= 0)
6958 NLA_PUT_U16(msg, NL80211_ATTR_BSS_HT_OPMODE, ht_opmode);
6959 if (ap_isolate >= 0)
6960 NLA_PUT_U8(msg, NL80211_ATTR_AP_ISOLATE, ap_isolate);
6961
6962 if (basic_rates) {
6963 u8 rates[NL80211_MAX_SUPP_RATES];
6964 u8 rates_len = 0;
6965 int i;
6966
6967 for (i = 0; i < NL80211_MAX_SUPP_RATES && basic_rates[i] >= 0;
6968 i++)
6969 rates[rates_len++] = basic_rates[i] / 5;
6970
6971 NLA_PUT(msg, NL80211_ATTR_BSS_BASIC_RATES, rates_len, rates);
6972 }
6973
6974 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
6975
6976 return send_and_recv_msgs(drv, msg, NULL, NULL);
6977 nla_put_failure:
6978 nlmsg_free(msg);
6979 return -ENOBUFS;
6980}
6981
6982
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006983static int wpa_driver_nl80211_set_acl(void *priv,
6984 struct hostapd_acl_params *params)
6985{
6986 struct i802_bss *bss = priv;
6987 struct wpa_driver_nl80211_data *drv = bss->drv;
6988 struct nl_msg *msg;
6989 struct nlattr *acl;
6990 unsigned int i;
6991 int ret = 0;
6992
6993 if (!(drv->capa.max_acl_mac_addrs))
6994 return -ENOTSUP;
6995
6996 if (params->num_mac_acl > drv->capa.max_acl_mac_addrs)
6997 return -ENOTSUP;
6998
6999 msg = nlmsg_alloc();
7000 if (!msg)
7001 return -ENOMEM;
7002
7003 wpa_printf(MSG_DEBUG, "nl80211: Set %s ACL (num_mac_acl=%u)",
7004 params->acl_policy ? "Accept" : "Deny", params->num_mac_acl);
7005
7006 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_MAC_ACL);
7007
7008 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
7009
7010 NLA_PUT_U32(msg, NL80211_ATTR_ACL_POLICY, params->acl_policy ?
7011 NL80211_ACL_POLICY_DENY_UNLESS_LISTED :
7012 NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED);
7013
7014 acl = nla_nest_start(msg, NL80211_ATTR_MAC_ADDRS);
7015 if (acl == NULL)
7016 goto nla_put_failure;
7017
7018 for (i = 0; i < params->num_mac_acl; i++)
7019 NLA_PUT(msg, i + 1, ETH_ALEN, params->mac_acl[i].addr);
7020
7021 nla_nest_end(msg, acl);
7022
7023 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7024 msg = NULL;
7025 if (ret) {
7026 wpa_printf(MSG_DEBUG, "nl80211: Failed to set MAC ACL: %d (%s)",
7027 ret, strerror(-ret));
7028 }
7029
7030nla_put_failure:
7031 nlmsg_free(msg);
7032
7033 return ret;
7034}
7035
7036
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007037static int wpa_driver_nl80211_set_ap(void *priv,
7038 struct wpa_driver_ap_params *params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007039{
7040 struct i802_bss *bss = priv;
7041 struct wpa_driver_nl80211_data *drv = bss->drv;
7042 struct nl_msg *msg;
7043 u8 cmd = NL80211_CMD_NEW_BEACON;
7044 int ret;
7045 int beacon_set;
7046 int ifindex = if_nametoindex(bss->ifname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007047 int num_suites;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007048 u32 suites[10], suite;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007049 u32 ver;
7050
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07007051 beacon_set = bss->beacon_set;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007052
7053 msg = nlmsg_alloc();
7054 if (!msg)
7055 return -ENOMEM;
7056
7057 wpa_printf(MSG_DEBUG, "nl80211: Set beacon (beacon_set=%d)",
7058 beacon_set);
7059 if (beacon_set)
7060 cmd = NL80211_CMD_SET_BEACON;
7061
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007062 nl80211_cmd(drv, msg, 0, cmd);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007063 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon head",
7064 params->head, params->head_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007065 NLA_PUT(msg, NL80211_ATTR_BEACON_HEAD, params->head_len, params->head);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007066 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon tail",
7067 params->tail, params->tail_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007068 NLA_PUT(msg, NL80211_ATTR_BEACON_TAIL, params->tail_len, params->tail);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007069 wpa_printf(MSG_DEBUG, "nl80211: ifindex=%d", ifindex);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007070 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007071 wpa_printf(MSG_DEBUG, "nl80211: beacon_int=%d", params->beacon_int);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007072 NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL, params->beacon_int);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007073 wpa_printf(MSG_DEBUG, "nl80211: dtim_period=%d", params->dtim_period);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007074 NLA_PUT_U32(msg, NL80211_ATTR_DTIM_PERIOD, params->dtim_period);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007075 wpa_hexdump_ascii(MSG_DEBUG, "nl80211: ssid",
7076 params->ssid, params->ssid_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007077 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
7078 params->ssid);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007079 if (params->proberesp && params->proberesp_len) {
7080 wpa_hexdump(MSG_DEBUG, "nl80211: proberesp (offload)",
7081 params->proberesp, params->proberesp_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007082 NLA_PUT(msg, NL80211_ATTR_PROBE_RESP, params->proberesp_len,
7083 params->proberesp);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007084 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007085 switch (params->hide_ssid) {
7086 case NO_SSID_HIDING:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007087 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID not in use");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007088 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
7089 NL80211_HIDDEN_SSID_NOT_IN_USE);
7090 break;
7091 case HIDDEN_SSID_ZERO_LEN:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007092 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero len");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007093 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
7094 NL80211_HIDDEN_SSID_ZERO_LEN);
7095 break;
7096 case HIDDEN_SSID_ZERO_CONTENTS:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007097 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero contents");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007098 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
7099 NL80211_HIDDEN_SSID_ZERO_CONTENTS);
7100 break;
7101 }
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007102 wpa_printf(MSG_DEBUG, "nl80211: privacy=%d", params->privacy);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007103 if (params->privacy)
7104 NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007105 wpa_printf(MSG_DEBUG, "nl80211: auth_algs=0x%x", params->auth_algs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007106 if ((params->auth_algs & (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) ==
7107 (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) {
7108 /* Leave out the attribute */
7109 } else if (params->auth_algs & WPA_AUTH_ALG_SHARED)
7110 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE,
7111 NL80211_AUTHTYPE_SHARED_KEY);
7112 else
7113 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE,
7114 NL80211_AUTHTYPE_OPEN_SYSTEM);
7115
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007116 wpa_printf(MSG_DEBUG, "nl80211: wpa_version=0x%x", params->wpa_version);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007117 ver = 0;
7118 if (params->wpa_version & WPA_PROTO_WPA)
7119 ver |= NL80211_WPA_VERSION_1;
7120 if (params->wpa_version & WPA_PROTO_RSN)
7121 ver |= NL80211_WPA_VERSION_2;
7122 if (ver)
7123 NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver);
7124
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007125 wpa_printf(MSG_DEBUG, "nl80211: key_mgmt_suites=0x%x",
7126 params->key_mgmt_suites);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007127 num_suites = 0;
7128 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X)
7129 suites[num_suites++] = WLAN_AKM_SUITE_8021X;
7130 if (params->key_mgmt_suites & WPA_KEY_MGMT_PSK)
7131 suites[num_suites++] = WLAN_AKM_SUITE_PSK;
7132 if (num_suites) {
7133 NLA_PUT(msg, NL80211_ATTR_AKM_SUITES,
7134 num_suites * sizeof(u32), suites);
7135 }
7136
7137 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X &&
7138 params->pairwise_ciphers & (WPA_CIPHER_WEP104 | WPA_CIPHER_WEP40))
7139 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT);
7140
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007141 wpa_printf(MSG_DEBUG, "nl80211: pairwise_ciphers=0x%x",
7142 params->pairwise_ciphers);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007143 num_suites = wpa_cipher_to_cipher_suites(params->pairwise_ciphers,
7144 suites, ARRAY_SIZE(suites));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007145 if (num_suites) {
7146 NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
7147 num_suites * sizeof(u32), suites);
7148 }
7149
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007150 wpa_printf(MSG_DEBUG, "nl80211: group_cipher=0x%x",
7151 params->group_cipher);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007152 suite = wpa_cipher_to_cipher_suite(params->group_cipher);
7153 if (suite)
7154 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, suite);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007155
7156 if (params->beacon_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007157 wpa_hexdump_buf(MSG_DEBUG, "nl80211: beacon_ies",
7158 params->beacon_ies);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007159 NLA_PUT(msg, NL80211_ATTR_IE, wpabuf_len(params->beacon_ies),
7160 wpabuf_head(params->beacon_ies));
7161 }
7162 if (params->proberesp_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007163 wpa_hexdump_buf(MSG_DEBUG, "nl80211: proberesp_ies",
7164 params->proberesp_ies);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007165 NLA_PUT(msg, NL80211_ATTR_IE_PROBE_RESP,
7166 wpabuf_len(params->proberesp_ies),
7167 wpabuf_head(params->proberesp_ies));
7168 }
7169 if (params->assocresp_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007170 wpa_hexdump_buf(MSG_DEBUG, "nl80211: assocresp_ies",
7171 params->assocresp_ies);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007172 NLA_PUT(msg, NL80211_ATTR_IE_ASSOC_RESP,
7173 wpabuf_len(params->assocresp_ies),
7174 wpabuf_head(params->assocresp_ies));
7175 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007176
Dmitry Shmidt04949592012-07-19 12:16:46 -07007177 if (drv->capa.flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007178 wpa_printf(MSG_DEBUG, "nl80211: ap_max_inactivity=%d",
7179 params->ap_max_inactivity);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007180 NLA_PUT_U16(msg, NL80211_ATTR_INACTIVITY_TIMEOUT,
7181 params->ap_max_inactivity);
7182 }
7183
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007184 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7185 if (ret) {
7186 wpa_printf(MSG_DEBUG, "nl80211: Beacon set failed: %d (%s)",
7187 ret, strerror(-ret));
7188 } else {
7189 bss->beacon_set = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007190 nl80211_set_bss(bss, params->cts_protect, params->preamble,
7191 params->short_slot_time, params->ht_opmode,
7192 params->isolate, params->basic_rates);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007193 }
7194 return ret;
7195 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007196 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007197 return -ENOBUFS;
7198}
7199
7200
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007201static int nl80211_put_freq_params(struct nl_msg *msg,
7202 struct hostapd_freq_params *freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007203{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007204 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq->freq);
7205 if (freq->vht_enabled) {
7206 switch (freq->bandwidth) {
7207 case 20:
7208 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
7209 NL80211_CHAN_WIDTH_20);
7210 break;
7211 case 40:
7212 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
7213 NL80211_CHAN_WIDTH_40);
7214 break;
7215 case 80:
7216 if (freq->center_freq2)
7217 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
7218 NL80211_CHAN_WIDTH_80P80);
7219 else
7220 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
7221 NL80211_CHAN_WIDTH_80);
7222 break;
7223 case 160:
7224 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
7225 NL80211_CHAN_WIDTH_160);
7226 break;
7227 default:
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007228 return -EINVAL;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007229 }
7230 NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ1, freq->center_freq1);
7231 if (freq->center_freq2)
7232 NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ2,
7233 freq->center_freq2);
7234 } else if (freq->ht_enabled) {
7235 switch (freq->sec_channel_offset) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007236 case -1:
7237 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
7238 NL80211_CHAN_HT40MINUS);
7239 break;
7240 case 1:
7241 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
7242 NL80211_CHAN_HT40PLUS);
7243 break;
7244 default:
7245 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
7246 NL80211_CHAN_HT20);
7247 break;
7248 }
7249 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007250 return 0;
7251
7252nla_put_failure:
7253 return -ENOBUFS;
7254}
7255
7256
7257static int wpa_driver_nl80211_set_freq(struct i802_bss *bss,
7258 struct hostapd_freq_params *freq)
7259{
7260 struct wpa_driver_nl80211_data *drv = bss->drv;
7261 struct nl_msg *msg;
7262 int ret;
7263
7264 wpa_printf(MSG_DEBUG,
7265 "nl80211: Set freq %d (ht_enabled=%d, vht_enabled=%d, bandwidth=%d MHz, cf1=%d MHz, cf2=%d MHz)",
7266 freq->freq, freq->ht_enabled, freq->vht_enabled,
7267 freq->bandwidth, freq->center_freq1, freq->center_freq2);
7268 msg = nlmsg_alloc();
7269 if (!msg)
7270 return -1;
7271
7272 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
7273
7274 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
7275 if (nl80211_put_freq_params(msg, freq) < 0)
7276 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007277
7278 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007279 msg = NULL;
7280 if (ret == 0) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007281 bss->freq = freq->freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007282 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007283 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007284 wpa_printf(MSG_DEBUG, "nl80211: Failed to set channel (freq=%d): "
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007285 "%d (%s)", freq->freq, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007286nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007287 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007288 return -1;
7289}
7290
7291
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007292static u32 sta_flags_nl80211(int flags)
7293{
7294 u32 f = 0;
7295
7296 if (flags & WPA_STA_AUTHORIZED)
7297 f |= BIT(NL80211_STA_FLAG_AUTHORIZED);
7298 if (flags & WPA_STA_WMM)
7299 f |= BIT(NL80211_STA_FLAG_WME);
7300 if (flags & WPA_STA_SHORT_PREAMBLE)
7301 f |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
7302 if (flags & WPA_STA_MFP)
7303 f |= BIT(NL80211_STA_FLAG_MFP);
7304 if (flags & WPA_STA_TDLS_PEER)
7305 f |= BIT(NL80211_STA_FLAG_TDLS_PEER);
7306
7307 return f;
7308}
7309
7310
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007311static int wpa_driver_nl80211_sta_add(void *priv,
7312 struct hostapd_sta_add_params *params)
7313{
7314 struct i802_bss *bss = priv;
7315 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007316 struct nl_msg *msg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007317 struct nl80211_sta_flag_update upd;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007318 int ret = -ENOBUFS;
7319
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007320 if ((params->flags & WPA_STA_TDLS_PEER) &&
7321 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
7322 return -EOPNOTSUPP;
7323
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007324 msg = nlmsg_alloc();
7325 if (!msg)
7326 return -ENOMEM;
7327
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007328 wpa_printf(MSG_DEBUG, "nl80211: %s STA " MACSTR,
7329 params->set ? "Set" : "Add", MAC2STR(params->addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007330 nl80211_cmd(drv, msg, 0, params->set ? NL80211_CMD_SET_STATION :
7331 NL80211_CMD_NEW_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007332
7333 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
7334 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007335 NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_RATES, params->supp_rates_len,
7336 params->supp_rates);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007337 wpa_hexdump(MSG_DEBUG, " * supported rates", params->supp_rates,
7338 params->supp_rates_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007339 if (!params->set) {
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07007340 if (params->aid) {
7341 wpa_printf(MSG_DEBUG, " * aid=%u", params->aid);
7342 NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, params->aid);
7343 } else {
7344 /*
7345 * cfg80211 validates that AID is non-zero, so we have
7346 * to make this a non-zero value for the TDLS case where
7347 * a dummy STA entry is used for now.
7348 */
7349 wpa_printf(MSG_DEBUG, " * aid=1 (TDLS workaround)");
7350 NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, 1);
7351 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007352 wpa_printf(MSG_DEBUG, " * listen_interval=%u",
7353 params->listen_interval);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007354 NLA_PUT_U16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
7355 params->listen_interval);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07007356 } else if (params->aid && (params->flags & WPA_STA_TDLS_PEER)) {
7357 wpa_printf(MSG_DEBUG, " * peer_aid=%u", params->aid);
7358 NLA_PUT_U16(msg, NL80211_ATTR_PEER_AID, params->aid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007359 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007360 if (params->ht_capabilities) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007361 wpa_hexdump(MSG_DEBUG, " * ht_capabilities",
7362 (u8 *) params->ht_capabilities,
7363 sizeof(*params->ht_capabilities));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007364 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY,
7365 sizeof(*params->ht_capabilities),
7366 params->ht_capabilities);
7367 }
7368
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007369 if (params->vht_capabilities) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007370 wpa_hexdump(MSG_DEBUG, " * vht_capabilities",
7371 (u8 *) params->vht_capabilities,
7372 sizeof(*params->vht_capabilities));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007373 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY,
7374 sizeof(*params->vht_capabilities),
7375 params->vht_capabilities);
7376 }
7377
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08007378 if (params->vht_opmode_enabled) {
7379 wpa_printf(MSG_DEBUG, " * opmode=%u", params->vht_opmode);
7380 NLA_PUT_U8(msg, NL80211_ATTR_OPMODE_NOTIF,
7381 params->vht_opmode);
7382 }
7383
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007384 wpa_printf(MSG_DEBUG, " * capability=0x%x", params->capability);
7385 NLA_PUT_U16(msg, NL80211_ATTR_STA_CAPABILITY, params->capability);
7386
7387 if (params->ext_capab) {
7388 wpa_hexdump(MSG_DEBUG, " * ext_capab",
7389 params->ext_capab, params->ext_capab_len);
7390 NLA_PUT(msg, NL80211_ATTR_STA_EXT_CAPABILITY,
7391 params->ext_capab_len, params->ext_capab);
7392 }
7393
Dmitry Shmidt344abd32014-01-14 13:17:00 -08007394 if (params->supp_channels) {
7395 wpa_hexdump(MSG_DEBUG, " * supported channels",
7396 params->supp_channels, params->supp_channels_len);
7397 NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_CHANNELS,
7398 params->supp_channels_len, params->supp_channels);
7399 }
7400
7401 if (params->supp_oper_classes) {
7402 wpa_hexdump(MSG_DEBUG, " * supported operating classes",
7403 params->supp_oper_classes,
7404 params->supp_oper_classes_len);
7405 NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES,
7406 params->supp_oper_classes_len,
7407 params->supp_oper_classes);
7408 }
7409
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007410 os_memset(&upd, 0, sizeof(upd));
7411 upd.mask = sta_flags_nl80211(params->flags);
7412 upd.set = upd.mask;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007413 wpa_printf(MSG_DEBUG, " * flags set=0x%x mask=0x%x",
7414 upd.set, upd.mask);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007415 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
7416
7417 if (params->flags & WPA_STA_WMM) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007418 struct nlattr *wme = nla_nest_start(msg, NL80211_ATTR_STA_WME);
7419
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007420 if (!wme)
7421 goto nla_put_failure;
7422
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007423 wpa_printf(MSG_DEBUG, " * qosinfo=0x%x", params->qosinfo);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007424 NLA_PUT_U8(msg, NL80211_STA_WME_UAPSD_QUEUES,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007425 params->qosinfo & WMM_QOSINFO_STA_AC_MASK);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007426 NLA_PUT_U8(msg, NL80211_STA_WME_MAX_SP,
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007427 (params->qosinfo >> WMM_QOSINFO_STA_SP_SHIFT) &
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007428 WMM_QOSINFO_STA_SP_MASK);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007429 nla_nest_end(msg, wme);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007430 }
7431
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007432 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007433 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007434 if (ret)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007435 wpa_printf(MSG_DEBUG, "nl80211: NL80211_CMD_%s_STATION "
7436 "result: %d (%s)", params->set ? "SET" : "NEW", ret,
7437 strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007438 if (ret == -EEXIST)
7439 ret = 0;
7440 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007441 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007442 return ret;
7443}
7444
7445
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007446static int wpa_driver_nl80211_sta_remove(struct i802_bss *bss, const u8 *addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007447{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007448 struct wpa_driver_nl80211_data *drv = bss->drv;
7449 struct nl_msg *msg;
7450 int ret;
7451
7452 msg = nlmsg_alloc();
7453 if (!msg)
7454 return -ENOMEM;
7455
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007456 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007457
7458 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
7459 if_nametoindex(bss->ifname));
7460 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
7461
7462 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007463 wpa_printf(MSG_DEBUG, "nl80211: sta_remove -> DEL_STATION %s " MACSTR
7464 " --> %d (%s)",
7465 bss->ifname, MAC2STR(addr), ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007466 if (ret == -ENOENT)
7467 return 0;
7468 return ret;
7469 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007470 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007471 return -ENOBUFS;
7472}
7473
7474
7475static void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv,
7476 int ifidx)
7477{
7478 struct nl_msg *msg;
7479
7480 wpa_printf(MSG_DEBUG, "nl80211: Remove interface ifindex=%d", ifidx);
7481
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007482 /* stop listening for EAPOL on this interface */
7483 del_ifidx(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007484
7485 msg = nlmsg_alloc();
7486 if (!msg)
7487 goto nla_put_failure;
7488
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007489 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_INTERFACE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007490 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifidx);
7491
7492 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
7493 return;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007494 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007495 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007496 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007497 wpa_printf(MSG_ERROR, "Failed to remove interface (ifidx=%d)", ifidx);
7498}
7499
7500
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007501static const char * nl80211_iftype_str(enum nl80211_iftype mode)
7502{
7503 switch (mode) {
7504 case NL80211_IFTYPE_ADHOC:
7505 return "ADHOC";
7506 case NL80211_IFTYPE_STATION:
7507 return "STATION";
7508 case NL80211_IFTYPE_AP:
7509 return "AP";
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07007510 case NL80211_IFTYPE_AP_VLAN:
7511 return "AP_VLAN";
7512 case NL80211_IFTYPE_WDS:
7513 return "WDS";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007514 case NL80211_IFTYPE_MONITOR:
7515 return "MONITOR";
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07007516 case NL80211_IFTYPE_MESH_POINT:
7517 return "MESH_POINT";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007518 case NL80211_IFTYPE_P2P_CLIENT:
7519 return "P2P_CLIENT";
7520 case NL80211_IFTYPE_P2P_GO:
7521 return "P2P_GO";
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007522 case NL80211_IFTYPE_P2P_DEVICE:
7523 return "P2P_DEVICE";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007524 default:
7525 return "unknown";
7526 }
7527}
7528
7529
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007530static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv,
7531 const char *ifname,
7532 enum nl80211_iftype iftype,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007533 const u8 *addr, int wds,
7534 int (*handler)(struct nl_msg *, void *),
7535 void *arg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007536{
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007537 struct nl_msg *msg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007538 int ifidx;
7539 int ret = -ENOBUFS;
7540
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007541 wpa_printf(MSG_DEBUG, "nl80211: Create interface iftype %d (%s)",
7542 iftype, nl80211_iftype_str(iftype));
7543
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007544 msg = nlmsg_alloc();
7545 if (!msg)
7546 return -1;
7547
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007548 nl80211_cmd(drv, msg, 0, NL80211_CMD_NEW_INTERFACE);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007549 if (nl80211_set_iface_id(msg, drv->first_bss) < 0)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007550 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007551 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, ifname);
7552 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, iftype);
7553
7554 if (iftype == NL80211_IFTYPE_MONITOR) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007555 struct nlattr *flags;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007556
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007557 flags = nla_nest_start(msg, NL80211_ATTR_MNTR_FLAGS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007558 if (!flags)
7559 goto nla_put_failure;
7560
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007561 NLA_PUT_FLAG(msg, NL80211_MNTR_FLAG_COOK_FRAMES);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007562
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007563 nla_nest_end(msg, flags);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007564 } else if (wds) {
7565 NLA_PUT_U8(msg, NL80211_ATTR_4ADDR, wds);
7566 }
7567
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007568 ret = send_and_recv_msgs(drv, msg, handler, arg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007569 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007570 if (ret) {
7571 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007572 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007573 wpa_printf(MSG_ERROR, "Failed to create interface %s: %d (%s)",
7574 ifname, ret, strerror(-ret));
7575 return ret;
7576 }
7577
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007578 if (iftype == NL80211_IFTYPE_P2P_DEVICE)
7579 return 0;
7580
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007581 ifidx = if_nametoindex(ifname);
7582 wpa_printf(MSG_DEBUG, "nl80211: New interface %s created: ifindex=%d",
7583 ifname, ifidx);
7584
7585 if (ifidx <= 0)
7586 return -1;
7587
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007588 /* start listening for EAPOL on this interface */
7589 add_ifidx(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007590
7591 if (addr && iftype != NL80211_IFTYPE_MONITOR &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007592 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname, addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007593 nl80211_remove_iface(drv, ifidx);
7594 return -1;
7595 }
7596
7597 return ifidx;
7598}
7599
7600
7601static int nl80211_create_iface(struct wpa_driver_nl80211_data *drv,
7602 const char *ifname, enum nl80211_iftype iftype,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007603 const u8 *addr, int wds,
7604 int (*handler)(struct nl_msg *, void *),
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007605 void *arg, int use_existing)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007606{
7607 int ret;
7608
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007609 ret = nl80211_create_iface_once(drv, ifname, iftype, addr, wds, handler,
7610 arg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007611
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007612 /* if error occurred and interface exists already */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007613 if (ret == -ENFILE && if_nametoindex(ifname)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007614 if (use_existing) {
7615 wpa_printf(MSG_DEBUG, "nl80211: Continue using existing interface %s",
7616 ifname);
7617 return -ENFILE;
7618 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007619 wpa_printf(MSG_INFO, "Try to remove and re-create %s", ifname);
7620
7621 /* Try to remove the interface that was already there. */
7622 nl80211_remove_iface(drv, if_nametoindex(ifname));
7623
7624 /* Try to create the interface again */
7625 ret = nl80211_create_iface_once(drv, ifname, iftype, addr,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007626 wds, handler, arg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007627 }
7628
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007629 if (ret >= 0 && is_p2p_net_interface(iftype))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007630 nl80211_disable_11b_rates(drv, ret, 1);
7631
7632 return ret;
7633}
7634
7635
7636static void handle_tx_callback(void *ctx, u8 *buf, size_t len, int ok)
7637{
7638 struct ieee80211_hdr *hdr;
7639 u16 fc;
7640 union wpa_event_data event;
7641
7642 hdr = (struct ieee80211_hdr *) buf;
7643 fc = le_to_host16(hdr->frame_control);
7644
7645 os_memset(&event, 0, sizeof(event));
7646 event.tx_status.type = WLAN_FC_GET_TYPE(fc);
7647 event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
7648 event.tx_status.dst = hdr->addr1;
7649 event.tx_status.data = buf;
7650 event.tx_status.data_len = len;
7651 event.tx_status.ack = ok;
7652 wpa_supplicant_event(ctx, EVENT_TX_STATUS, &event);
7653}
7654
7655
7656static void from_unknown_sta(struct wpa_driver_nl80211_data *drv,
7657 u8 *buf, size_t len)
7658{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007659 struct ieee80211_hdr *hdr = (void *)buf;
7660 u16 fc;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007661 union wpa_event_data event;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007662
7663 if (len < sizeof(*hdr))
7664 return;
7665
7666 fc = le_to_host16(hdr->frame_control);
7667
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007668 os_memset(&event, 0, sizeof(event));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007669 event.rx_from_unknown.bssid = get_hdr_bssid(hdr, len);
7670 event.rx_from_unknown.addr = hdr->addr2;
7671 event.rx_from_unknown.wds = (fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) ==
7672 (WLAN_FC_FROMDS | WLAN_FC_TODS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007673 wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event);
7674}
7675
7676
7677static void handle_frame(struct wpa_driver_nl80211_data *drv,
7678 u8 *buf, size_t len, int datarate, int ssi_signal)
7679{
7680 struct ieee80211_hdr *hdr;
7681 u16 fc;
7682 union wpa_event_data event;
7683
7684 hdr = (struct ieee80211_hdr *) buf;
7685 fc = le_to_host16(hdr->frame_control);
7686
7687 switch (WLAN_FC_GET_TYPE(fc)) {
7688 case WLAN_FC_TYPE_MGMT:
7689 os_memset(&event, 0, sizeof(event));
7690 event.rx_mgmt.frame = buf;
7691 event.rx_mgmt.frame_len = len;
7692 event.rx_mgmt.datarate = datarate;
7693 event.rx_mgmt.ssi_signal = ssi_signal;
7694 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
7695 break;
7696 case WLAN_FC_TYPE_CTRL:
7697 /* can only get here with PS-Poll frames */
7698 wpa_printf(MSG_DEBUG, "CTRL");
7699 from_unknown_sta(drv, buf, len);
7700 break;
7701 case WLAN_FC_TYPE_DATA:
7702 from_unknown_sta(drv, buf, len);
7703 break;
7704 }
7705}
7706
7707
7708static void handle_monitor_read(int sock, void *eloop_ctx, void *sock_ctx)
7709{
7710 struct wpa_driver_nl80211_data *drv = eloop_ctx;
7711 int len;
7712 unsigned char buf[3000];
7713 struct ieee80211_radiotap_iterator iter;
7714 int ret;
7715 int datarate = 0, ssi_signal = 0;
7716 int injected = 0, failed = 0, rxflags = 0;
7717
7718 len = recv(sock, buf, sizeof(buf), 0);
7719 if (len < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007720 wpa_printf(MSG_ERROR, "nl80211: Monitor socket recv failed: %s",
7721 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007722 return;
7723 }
7724
7725 if (ieee80211_radiotap_iterator_init(&iter, (void*)buf, len)) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007726 wpa_printf(MSG_INFO, "nl80211: received invalid radiotap frame");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007727 return;
7728 }
7729
7730 while (1) {
7731 ret = ieee80211_radiotap_iterator_next(&iter);
7732 if (ret == -ENOENT)
7733 break;
7734 if (ret) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007735 wpa_printf(MSG_INFO, "nl80211: received invalid radiotap frame (%d)",
7736 ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007737 return;
7738 }
7739 switch (iter.this_arg_index) {
7740 case IEEE80211_RADIOTAP_FLAGS:
7741 if (*iter.this_arg & IEEE80211_RADIOTAP_F_FCS)
7742 len -= 4;
7743 break;
7744 case IEEE80211_RADIOTAP_RX_FLAGS:
7745 rxflags = 1;
7746 break;
7747 case IEEE80211_RADIOTAP_TX_FLAGS:
7748 injected = 1;
7749 failed = le_to_host16((*(uint16_t *) iter.this_arg)) &
7750 IEEE80211_RADIOTAP_F_TX_FAIL;
7751 break;
7752 case IEEE80211_RADIOTAP_DATA_RETRIES:
7753 break;
7754 case IEEE80211_RADIOTAP_CHANNEL:
7755 /* TODO: convert from freq/flags to channel number */
7756 break;
7757 case IEEE80211_RADIOTAP_RATE:
7758 datarate = *iter.this_arg * 5;
7759 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007760 case IEEE80211_RADIOTAP_DBM_ANTSIGNAL:
7761 ssi_signal = (s8) *iter.this_arg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007762 break;
7763 }
7764 }
7765
7766 if (rxflags && injected)
7767 return;
7768
7769 if (!injected)
7770 handle_frame(drv, buf + iter.max_length,
7771 len - iter.max_length, datarate, ssi_signal);
7772 else
7773 handle_tx_callback(drv->ctx, buf + iter.max_length,
7774 len - iter.max_length, !failed);
7775}
7776
7777
7778/*
7779 * we post-process the filter code later and rewrite
7780 * this to the offset to the last instruction
7781 */
7782#define PASS 0xFF
7783#define FAIL 0xFE
7784
7785static struct sock_filter msock_filter_insns[] = {
7786 /*
7787 * do a little-endian load of the radiotap length field
7788 */
7789 /* load lower byte into A */
7790 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 2),
7791 /* put it into X (== index register) */
7792 BPF_STMT(BPF_MISC| BPF_TAX, 0),
7793 /* load upper byte into A */
7794 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 3),
7795 /* left-shift it by 8 */
7796 BPF_STMT(BPF_ALU | BPF_LSH | BPF_K, 8),
7797 /* or with X */
7798 BPF_STMT(BPF_ALU | BPF_OR | BPF_X, 0),
7799 /* put result into X */
7800 BPF_STMT(BPF_MISC| BPF_TAX, 0),
7801
7802 /*
7803 * Allow management frames through, this also gives us those
7804 * management frames that we sent ourselves with status
7805 */
7806 /* load the lower byte of the IEEE 802.11 frame control field */
7807 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
7808 /* mask off frame type and version */
7809 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0xF),
7810 /* accept frame if it's both 0, fall through otherwise */
7811 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0, PASS, 0),
7812
7813 /*
7814 * TODO: add a bit to radiotap RX flags that indicates
7815 * that the sending station is not associated, then
7816 * add a filter here that filters on our DA and that flag
7817 * to allow us to deauth frames to that bad station.
7818 *
7819 * For now allow all To DS data frames through.
7820 */
7821 /* load the IEEE 802.11 frame control field */
7822 BPF_STMT(BPF_LD | BPF_H | BPF_IND, 0),
7823 /* mask off frame type, version and DS status */
7824 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0F03),
7825 /* accept frame if version 0, type 2 and To DS, fall through otherwise
7826 */
7827 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0801, PASS, 0),
7828
7829#if 0
7830 /*
7831 * drop non-data frames
7832 */
7833 /* load the lower byte of the frame control field */
7834 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
7835 /* mask off QoS bit */
7836 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0c),
7837 /* drop non-data frames */
7838 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 8, 0, FAIL),
7839#endif
7840 /* load the upper byte of the frame control field */
7841 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 1),
7842 /* mask off toDS/fromDS */
7843 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x03),
7844 /* accept WDS frames */
7845 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 3, PASS, 0),
7846
7847 /*
7848 * add header length to index
7849 */
7850 /* load the lower byte of the frame control field */
7851 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
7852 /* mask off QoS bit */
7853 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x80),
7854 /* right shift it by 6 to give 0 or 2 */
7855 BPF_STMT(BPF_ALU | BPF_RSH | BPF_K, 6),
7856 /* add data frame header length */
7857 BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 24),
7858 /* add index, was start of 802.11 header */
7859 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
7860 /* move to index, now start of LL header */
7861 BPF_STMT(BPF_MISC | BPF_TAX, 0),
7862
7863 /*
7864 * Accept empty data frames, we use those for
7865 * polling activity.
7866 */
7867 BPF_STMT(BPF_LD | BPF_W | BPF_LEN, 0),
7868 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_X, 0, PASS, 0),
7869
7870 /*
7871 * Accept EAPOL frames
7872 */
7873 BPF_STMT(BPF_LD | BPF_W | BPF_IND, 0),
7874 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0xAAAA0300, 0, FAIL),
7875 BPF_STMT(BPF_LD | BPF_W | BPF_IND, 4),
7876 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0000888E, PASS, FAIL),
7877
7878 /* keep these last two statements or change the code below */
7879 /* return 0 == "DROP" */
7880 BPF_STMT(BPF_RET | BPF_K, 0),
7881 /* return ~0 == "keep all" */
7882 BPF_STMT(BPF_RET | BPF_K, ~0),
7883};
7884
7885static struct sock_fprog msock_filter = {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007886 .len = ARRAY_SIZE(msock_filter_insns),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007887 .filter = msock_filter_insns,
7888};
7889
7890
7891static int add_monitor_filter(int s)
7892{
7893 int idx;
7894
7895 /* rewrite all PASS/FAIL jump offsets */
7896 for (idx = 0; idx < msock_filter.len; idx++) {
7897 struct sock_filter *insn = &msock_filter_insns[idx];
7898
7899 if (BPF_CLASS(insn->code) == BPF_JMP) {
7900 if (insn->code == (BPF_JMP|BPF_JA)) {
7901 if (insn->k == PASS)
7902 insn->k = msock_filter.len - idx - 2;
7903 else if (insn->k == FAIL)
7904 insn->k = msock_filter.len - idx - 3;
7905 }
7906
7907 if (insn->jt == PASS)
7908 insn->jt = msock_filter.len - idx - 2;
7909 else if (insn->jt == FAIL)
7910 insn->jt = msock_filter.len - idx - 3;
7911
7912 if (insn->jf == PASS)
7913 insn->jf = msock_filter.len - idx - 2;
7914 else if (insn->jf == FAIL)
7915 insn->jf = msock_filter.len - idx - 3;
7916 }
7917 }
7918
7919 if (setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER,
7920 &msock_filter, sizeof(msock_filter))) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007921 wpa_printf(MSG_ERROR, "nl80211: setsockopt(SO_ATTACH_FILTER) failed: %s",
7922 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007923 return -1;
7924 }
7925
7926 return 0;
7927}
7928
7929
7930static void nl80211_remove_monitor_interface(
7931 struct wpa_driver_nl80211_data *drv)
7932{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007933 if (drv->monitor_refcount > 0)
7934 drv->monitor_refcount--;
7935 wpa_printf(MSG_DEBUG, "nl80211: Remove monitor interface: refcount=%d",
7936 drv->monitor_refcount);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007937 if (drv->monitor_refcount > 0)
7938 return;
7939
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007940 if (drv->monitor_ifidx >= 0) {
7941 nl80211_remove_iface(drv, drv->monitor_ifidx);
7942 drv->monitor_ifidx = -1;
7943 }
7944 if (drv->monitor_sock >= 0) {
7945 eloop_unregister_read_sock(drv->monitor_sock);
7946 close(drv->monitor_sock);
7947 drv->monitor_sock = -1;
7948 }
7949}
7950
7951
7952static int
7953nl80211_create_monitor_interface(struct wpa_driver_nl80211_data *drv)
7954{
7955 char buf[IFNAMSIZ];
7956 struct sockaddr_ll ll;
7957 int optval;
7958 socklen_t optlen;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007959
7960 if (drv->monitor_ifidx >= 0) {
7961 drv->monitor_refcount++;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007962 wpa_printf(MSG_DEBUG, "nl80211: Re-use existing monitor interface: refcount=%d",
7963 drv->monitor_refcount);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007964 return 0;
7965 }
7966
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007967 if (os_strncmp(drv->first_bss->ifname, "p2p-", 4) == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007968 /*
7969 * P2P interface name is of the format p2p-%s-%d. For monitor
7970 * interface name corresponding to P2P GO, replace "p2p-" with
7971 * "mon-" to retain the same interface name length and to
7972 * indicate that it is a monitor interface.
7973 */
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007974 snprintf(buf, IFNAMSIZ, "mon-%s", drv->first_bss->ifname + 4);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007975 } else {
7976 /* Non-P2P interface with AP functionality. */
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007977 snprintf(buf, IFNAMSIZ, "mon.%s", drv->first_bss->ifname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007978 }
7979
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007980 buf[IFNAMSIZ - 1] = '\0';
7981
7982 drv->monitor_ifidx =
7983 nl80211_create_iface(drv, buf, NL80211_IFTYPE_MONITOR, NULL,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007984 0, NULL, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007985
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07007986 if (drv->monitor_ifidx == -EOPNOTSUPP) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007987 /*
7988 * This is backward compatibility for a few versions of
7989 * the kernel only that didn't advertise the right
7990 * attributes for the only driver that then supported
7991 * AP mode w/o monitor -- ath6kl.
7992 */
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07007993 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support "
7994 "monitor interface type - try to run without it");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007995 drv->device_ap_sme = 1;
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07007996 }
7997
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007998 if (drv->monitor_ifidx < 0)
7999 return -1;
8000
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008001 if (linux_set_iface_flags(drv->global->ioctl_sock, buf, 1))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008002 goto error;
8003
8004 memset(&ll, 0, sizeof(ll));
8005 ll.sll_family = AF_PACKET;
8006 ll.sll_ifindex = drv->monitor_ifidx;
8007 drv->monitor_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
8008 if (drv->monitor_sock < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008009 wpa_printf(MSG_ERROR, "nl80211: socket[PF_PACKET,SOCK_RAW] failed: %s",
8010 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008011 goto error;
8012 }
8013
8014 if (add_monitor_filter(drv->monitor_sock)) {
8015 wpa_printf(MSG_INFO, "Failed to set socket filter for monitor "
8016 "interface; do filtering in user space");
8017 /* This works, but will cost in performance. */
8018 }
8019
8020 if (bind(drv->monitor_sock, (struct sockaddr *) &ll, sizeof(ll)) < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008021 wpa_printf(MSG_ERROR, "nl80211: monitor socket bind failed: %s",
8022 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008023 goto error;
8024 }
8025
8026 optlen = sizeof(optval);
8027 optval = 20;
8028 if (setsockopt
8029 (drv->monitor_sock, SOL_SOCKET, SO_PRIORITY, &optval, optlen)) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008030 wpa_printf(MSG_ERROR, "nl80211: Failed to set socket priority: %s",
8031 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008032 goto error;
8033 }
8034
8035 if (eloop_register_read_sock(drv->monitor_sock, handle_monitor_read,
8036 drv, NULL)) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008037 wpa_printf(MSG_INFO, "nl80211: Could not register monitor read socket");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008038 goto error;
8039 }
8040
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008041 drv->monitor_refcount++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008042 return 0;
8043 error:
8044 nl80211_remove_monitor_interface(drv);
8045 return -1;
8046}
8047
8048
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008049static int nl80211_setup_ap(struct i802_bss *bss)
8050{
8051 struct wpa_driver_nl80211_data *drv = bss->drv;
8052
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008053 wpa_printf(MSG_DEBUG, "nl80211: Setup AP(%s) - device_ap_sme=%d use_monitor=%d",
8054 bss->ifname, drv->device_ap_sme, drv->use_monitor);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008055
8056 /*
8057 * Disable Probe Request reporting unless we need it in this way for
8058 * devices that include the AP SME, in the other case (unless using
8059 * monitor iface) we'll get it through the nl_mgmt socket instead.
8060 */
8061 if (!drv->device_ap_sme)
8062 wpa_driver_nl80211_probe_req_report(bss, 0);
8063
8064 if (!drv->device_ap_sme && !drv->use_monitor)
8065 if (nl80211_mgmt_subscribe_ap(bss))
8066 return -1;
8067
8068 if (drv->device_ap_sme && !drv->use_monitor)
8069 if (nl80211_mgmt_subscribe_ap_dev_sme(bss))
8070 return -1;
8071
8072 if (!drv->device_ap_sme && drv->use_monitor &&
8073 nl80211_create_monitor_interface(drv) &&
8074 !drv->device_ap_sme)
Dmitry Shmidt04949592012-07-19 12:16:46 -07008075 return -1;
8076
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008077 if (drv->device_ap_sme &&
8078 wpa_driver_nl80211_probe_req_report(bss, 1) < 0) {
8079 wpa_printf(MSG_DEBUG, "nl80211: Failed to enable "
8080 "Probe Request frame reporting in AP mode");
8081 /* Try to survive without this */
8082 }
8083
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008084 return 0;
8085}
8086
8087
8088static void nl80211_teardown_ap(struct i802_bss *bss)
8089{
8090 struct wpa_driver_nl80211_data *drv = bss->drv;
8091
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008092 wpa_printf(MSG_DEBUG, "nl80211: Teardown AP(%s) - device_ap_sme=%d use_monitor=%d",
8093 bss->ifname, drv->device_ap_sme, drv->use_monitor);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008094 if (drv->device_ap_sme) {
8095 wpa_driver_nl80211_probe_req_report(bss, 0);
8096 if (!drv->use_monitor)
8097 nl80211_mgmt_unsubscribe(bss, "AP teardown (dev SME)");
8098 } else if (drv->use_monitor)
8099 nl80211_remove_monitor_interface(drv);
8100 else
8101 nl80211_mgmt_unsubscribe(bss, "AP teardown");
8102
8103 bss->beacon_set = 0;
8104}
8105
8106
8107static int nl80211_send_eapol_data(struct i802_bss *bss,
8108 const u8 *addr, const u8 *data,
8109 size_t data_len)
8110{
8111 struct sockaddr_ll ll;
8112 int ret;
8113
8114 if (bss->drv->eapol_tx_sock < 0) {
8115 wpa_printf(MSG_DEBUG, "nl80211: No socket to send EAPOL");
8116 return -1;
8117 }
8118
8119 os_memset(&ll, 0, sizeof(ll));
8120 ll.sll_family = AF_PACKET;
8121 ll.sll_ifindex = bss->ifindex;
8122 ll.sll_protocol = htons(ETH_P_PAE);
8123 ll.sll_halen = ETH_ALEN;
8124 os_memcpy(ll.sll_addr, addr, ETH_ALEN);
8125 ret = sendto(bss->drv->eapol_tx_sock, data, data_len, 0,
8126 (struct sockaddr *) &ll, sizeof(ll));
8127 if (ret < 0)
8128 wpa_printf(MSG_ERROR, "nl80211: EAPOL TX: %s",
8129 strerror(errno));
8130
8131 return ret;
8132}
8133
8134
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008135static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
8136
8137static int wpa_driver_nl80211_hapd_send_eapol(
8138 void *priv, const u8 *addr, const u8 *data,
8139 size_t data_len, int encrypt, const u8 *own_addr, u32 flags)
8140{
8141 struct i802_bss *bss = priv;
8142 struct wpa_driver_nl80211_data *drv = bss->drv;
8143 struct ieee80211_hdr *hdr;
8144 size_t len;
8145 u8 *pos;
8146 int res;
8147 int qos = flags & WPA_STA_WMM;
Dmitry Shmidt641185e2013-11-06 15:17:13 -08008148
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008149 if (drv->device_ap_sme || !drv->use_monitor)
8150 return nl80211_send_eapol_data(bss, addr, data, data_len);
8151
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008152 len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
8153 data_len;
8154 hdr = os_zalloc(len);
8155 if (hdr == NULL) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008156 wpa_printf(MSG_INFO, "nl80211: Failed to allocate EAPOL buffer(len=%lu)",
8157 (unsigned long) len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008158 return -1;
8159 }
8160
8161 hdr->frame_control =
8162 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
8163 hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
8164 if (encrypt)
8165 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
8166 if (qos) {
8167 hdr->frame_control |=
8168 host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
8169 }
8170
8171 memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
8172 memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
8173 memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
8174 pos = (u8 *) (hdr + 1);
8175
8176 if (qos) {
Dmitry Shmidtaa532512012-09-24 10:35:31 -07008177 /* Set highest priority in QoS header */
8178 pos[0] = 7;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008179 pos[1] = 0;
8180 pos += 2;
8181 }
8182
8183 memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
8184 pos += sizeof(rfc1042_header);
8185 WPA_PUT_BE16(pos, ETH_P_PAE);
8186 pos += 2;
8187 memcpy(pos, data, data_len);
8188
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008189 res = wpa_driver_nl80211_send_frame(bss, (u8 *) hdr, len, encrypt, 0,
8190 0, 0, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008191 if (res < 0) {
8192 wpa_printf(MSG_ERROR, "i802_send_eapol - packet len: %lu - "
8193 "failed: %d (%s)",
8194 (unsigned long) len, errno, strerror(errno));
8195 }
8196 os_free(hdr);
8197
8198 return res;
8199}
8200
8201
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008202static int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr,
8203 int total_flags,
8204 int flags_or, int flags_and)
8205{
8206 struct i802_bss *bss = priv;
8207 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008208 struct nl_msg *msg;
8209 struct nlattr *flags;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008210 struct nl80211_sta_flag_update upd;
8211
8212 msg = nlmsg_alloc();
8213 if (!msg)
8214 return -ENOMEM;
8215
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008216 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008217
8218 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
8219 if_nametoindex(bss->ifname));
8220 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
8221
8222 /*
8223 * Backwards compatibility version using NL80211_ATTR_STA_FLAGS. This
8224 * can be removed eventually.
8225 */
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008226 flags = nla_nest_start(msg, NL80211_ATTR_STA_FLAGS);
8227 if (!flags)
8228 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008229 if (total_flags & WPA_STA_AUTHORIZED)
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008230 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_AUTHORIZED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008231
8232 if (total_flags & WPA_STA_WMM)
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008233 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_WME);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008234
8235 if (total_flags & WPA_STA_SHORT_PREAMBLE)
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008236 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_SHORT_PREAMBLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008237
8238 if (total_flags & WPA_STA_MFP)
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008239 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_MFP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008240
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008241 if (total_flags & WPA_STA_TDLS_PEER)
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008242 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_TDLS_PEER);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008243
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008244 nla_nest_end(msg, flags);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008245
8246 os_memset(&upd, 0, sizeof(upd));
8247 upd.mask = sta_flags_nl80211(flags_or | ~flags_and);
8248 upd.set = sta_flags_nl80211(flags_or);
8249 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
8250
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008251 return send_and_recv_msgs(drv, msg, NULL, NULL);
8252 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008253 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008254 return -ENOBUFS;
8255}
8256
8257
8258static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv,
8259 struct wpa_driver_associate_params *params)
8260{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008261 enum nl80211_iftype nlmode, old_mode;
8262 struct hostapd_freq_params freq = {
8263 .freq = params->freq,
8264 };
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008265
8266 if (params->p2p) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008267 wpa_printf(MSG_DEBUG, "nl80211: Setup AP operations for P2P "
8268 "group (GO)");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008269 nlmode = NL80211_IFTYPE_P2P_GO;
8270 } else
8271 nlmode = NL80211_IFTYPE_AP;
8272
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008273 old_mode = drv->nlmode;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008274 if (wpa_driver_nl80211_set_mode(drv->first_bss, nlmode)) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008275 nl80211_remove_monitor_interface(drv);
8276 return -1;
8277 }
8278
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008279 if (wpa_driver_nl80211_set_freq(drv->first_bss, &freq)) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008280 if (old_mode != nlmode)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008281 wpa_driver_nl80211_set_mode(drv->first_bss, old_mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008282 nl80211_remove_monitor_interface(drv);
8283 return -1;
8284 }
8285
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008286 return 0;
8287}
8288
8289
8290static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv)
8291{
8292 struct nl_msg *msg;
8293 int ret = -1;
8294
8295 msg = nlmsg_alloc();
8296 if (!msg)
8297 return -1;
8298
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008299 nl80211_cmd(drv, msg, 0, NL80211_CMD_LEAVE_IBSS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008300 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
8301 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8302 msg = NULL;
8303 if (ret) {
8304 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS failed: ret=%d "
8305 "(%s)", ret, strerror(-ret));
8306 goto nla_put_failure;
8307 }
8308
8309 ret = 0;
8310 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS request sent successfully");
8311
8312nla_put_failure:
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008313 if (wpa_driver_nl80211_set_mode(drv->first_bss,
Dmitry Shmidt56052862013-10-04 10:23:25 -07008314 NL80211_IFTYPE_STATION)) {
8315 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
8316 "station mode");
8317 }
8318
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008319 nlmsg_free(msg);
8320 return ret;
8321}
8322
8323
8324static int wpa_driver_nl80211_ibss(struct wpa_driver_nl80211_data *drv,
8325 struct wpa_driver_associate_params *params)
8326{
8327 struct nl_msg *msg;
8328 int ret = -1;
8329 int count = 0;
8330
8331 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS (ifindex=%d)", drv->ifindex);
8332
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008333 if (wpa_driver_nl80211_set_mode(drv->first_bss,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008334 NL80211_IFTYPE_ADHOC)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008335 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
8336 "IBSS mode");
8337 return -1;
8338 }
8339
8340retry:
8341 msg = nlmsg_alloc();
8342 if (!msg)
8343 return -1;
8344
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008345 nl80211_cmd(drv, msg, 0, NL80211_CMD_JOIN_IBSS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008346 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
8347
8348 if (params->ssid == NULL || params->ssid_len > sizeof(drv->ssid))
8349 goto nla_put_failure;
8350
8351 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
8352 params->ssid, params->ssid_len);
8353 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
8354 params->ssid);
8355 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
8356 drv->ssid_len = params->ssid_len;
8357
8358 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
8359 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
8360
8361 ret = nl80211_set_conn_keys(params, msg);
8362 if (ret)
8363 goto nla_put_failure;
8364
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008365 if (params->bssid && params->fixed_bssid) {
8366 wpa_printf(MSG_DEBUG, " * BSSID=" MACSTR,
8367 MAC2STR(params->bssid));
8368 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
8369 }
8370
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008371 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
8372 params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
8373 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
8374 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008375 wpa_printf(MSG_DEBUG, " * control port");
8376 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT);
8377 }
8378
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008379 if (params->wpa_ie) {
8380 wpa_hexdump(MSG_DEBUG,
8381 " * Extra IEs for Beacon/Probe Response frames",
8382 params->wpa_ie, params->wpa_ie_len);
8383 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
8384 params->wpa_ie);
8385 }
8386
8387 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8388 msg = NULL;
8389 if (ret) {
8390 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS failed: ret=%d (%s)",
8391 ret, strerror(-ret));
8392 count++;
8393 if (ret == -EALREADY && count == 1) {
8394 wpa_printf(MSG_DEBUG, "nl80211: Retry IBSS join after "
8395 "forced leave");
8396 nl80211_leave_ibss(drv);
8397 nlmsg_free(msg);
8398 goto retry;
8399 }
8400
8401 goto nla_put_failure;
8402 }
8403 ret = 0;
8404 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS request sent successfully");
8405
8406nla_put_failure:
8407 nlmsg_free(msg);
8408 return ret;
8409}
8410
8411
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008412static int nl80211_connect_common(struct wpa_driver_nl80211_data *drv,
8413 struct wpa_driver_associate_params *params,
8414 struct nl_msg *msg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008415{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008416 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008417
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008418 if (params->bssid) {
8419 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
8420 MAC2STR(params->bssid));
8421 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
8422 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008423
Dmitry Shmidt96be6222014-02-13 10:16:51 -08008424 if (params->bssid_hint) {
8425 wpa_printf(MSG_DEBUG, " * bssid_hint=" MACSTR,
8426 MAC2STR(params->bssid_hint));
8427 NLA_PUT(msg, NL80211_ATTR_MAC_HINT, ETH_ALEN,
8428 params->bssid_hint);
8429 }
8430
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008431 if (params->freq) {
8432 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
8433 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008434 drv->assoc_freq = params->freq;
8435 } else
8436 drv->assoc_freq = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008437
Dmitry Shmidt96be6222014-02-13 10:16:51 -08008438 if (params->freq_hint) {
8439 wpa_printf(MSG_DEBUG, " * freq_hint=%d", params->freq_hint);
8440 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ_HINT,
8441 params->freq_hint);
8442 }
8443
Dmitry Shmidt04949592012-07-19 12:16:46 -07008444 if (params->bg_scan_period >= 0) {
8445 wpa_printf(MSG_DEBUG, " * bg scan period=%d",
8446 params->bg_scan_period);
8447 NLA_PUT_U16(msg, NL80211_ATTR_BG_SCAN_PERIOD,
8448 params->bg_scan_period);
8449 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008450
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008451 if (params->ssid) {
8452 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
8453 params->ssid, params->ssid_len);
8454 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
8455 params->ssid);
8456 if (params->ssid_len > sizeof(drv->ssid))
8457 goto nla_put_failure;
8458 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
8459 drv->ssid_len = params->ssid_len;
8460 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008461
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008462 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
8463 if (params->wpa_ie)
8464 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
8465 params->wpa_ie);
8466
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008467 if (params->wpa_proto) {
8468 enum nl80211_wpa_versions ver = 0;
8469
8470 if (params->wpa_proto & WPA_PROTO_WPA)
8471 ver |= NL80211_WPA_VERSION_1;
8472 if (params->wpa_proto & WPA_PROTO_RSN)
8473 ver |= NL80211_WPA_VERSION_2;
8474
8475 wpa_printf(MSG_DEBUG, " * WPA Versions 0x%x", ver);
8476 NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver);
8477 }
8478
8479 if (params->pairwise_suite != WPA_CIPHER_NONE) {
8480 u32 cipher = wpa_cipher_to_cipher_suite(params->pairwise_suite);
8481 wpa_printf(MSG_DEBUG, " * pairwise=0x%x", cipher);
8482 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE, cipher);
8483 }
8484
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08008485 if (params->group_suite == WPA_CIPHER_GTK_NOT_USED &&
8486 !(drv->capa.enc & WPA_DRIVER_CAPA_ENC_GTK_NOT_USED)) {
8487 /*
8488 * This is likely to work even though many drivers do not
8489 * advertise support for operations without GTK.
8490 */
8491 wpa_printf(MSG_DEBUG, " * skip group cipher configuration for GTK_NOT_USED due to missing driver support advertisement");
8492 } else if (params->group_suite != WPA_CIPHER_NONE) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008493 u32 cipher = wpa_cipher_to_cipher_suite(params->group_suite);
8494 wpa_printf(MSG_DEBUG, " * group=0x%x", cipher);
8495 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher);
8496 }
8497
8498 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
8499 params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
8500 params->key_mgmt_suite == WPA_KEY_MGMT_FT_IEEE8021X ||
8501 params->key_mgmt_suite == WPA_KEY_MGMT_FT_PSK ||
8502 params->key_mgmt_suite == WPA_KEY_MGMT_CCKM) {
8503 int mgmt = WLAN_AKM_SUITE_PSK;
8504
8505 switch (params->key_mgmt_suite) {
8506 case WPA_KEY_MGMT_CCKM:
8507 mgmt = WLAN_AKM_SUITE_CCKM;
8508 break;
8509 case WPA_KEY_MGMT_IEEE8021X:
8510 mgmt = WLAN_AKM_SUITE_8021X;
8511 break;
8512 case WPA_KEY_MGMT_FT_IEEE8021X:
8513 mgmt = WLAN_AKM_SUITE_FT_8021X;
8514 break;
8515 case WPA_KEY_MGMT_FT_PSK:
8516 mgmt = WLAN_AKM_SUITE_FT_PSK;
8517 break;
8518 case WPA_KEY_MGMT_PSK:
8519 default:
8520 mgmt = WLAN_AKM_SUITE_PSK;
8521 break;
8522 }
8523 NLA_PUT_U32(msg, NL80211_ATTR_AKM_SUITES, mgmt);
8524 }
8525
8526 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT);
8527
8528 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED)
8529 NLA_PUT_U32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED);
8530
8531 if (params->disable_ht)
8532 NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_HT);
8533
8534 if (params->htcaps && params->htcaps_mask) {
8535 int sz = sizeof(struct ieee80211_ht_capabilities);
8536 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY, sz, params->htcaps);
8537 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY_MASK, sz,
8538 params->htcaps_mask);
8539 }
8540
8541#ifdef CONFIG_VHT_OVERRIDES
8542 if (params->disable_vht) {
8543 wpa_printf(MSG_DEBUG, " * VHT disabled");
8544 NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_VHT);
8545 }
8546
8547 if (params->vhtcaps && params->vhtcaps_mask) {
8548 int sz = sizeof(struct ieee80211_vht_capabilities);
8549 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY, sz, params->vhtcaps);
8550 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY_MASK, sz,
8551 params->vhtcaps_mask);
8552 }
8553#endif /* CONFIG_VHT_OVERRIDES */
8554
8555 if (params->p2p)
8556 wpa_printf(MSG_DEBUG, " * P2P group");
8557
8558 return 0;
8559nla_put_failure:
8560 return -1;
8561}
8562
8563
8564static int wpa_driver_nl80211_try_connect(
8565 struct wpa_driver_nl80211_data *drv,
8566 struct wpa_driver_associate_params *params)
8567{
8568 struct nl_msg *msg;
8569 enum nl80211_auth_type type;
8570 int ret;
8571 int algs;
8572
8573 msg = nlmsg_alloc();
8574 if (!msg)
8575 return -1;
8576
8577 wpa_printf(MSG_DEBUG, "nl80211: Connect (ifindex=%d)", drv->ifindex);
8578 nl80211_cmd(drv, msg, 0, NL80211_CMD_CONNECT);
8579
8580 ret = nl80211_connect_common(drv, params, msg);
8581 if (ret)
8582 goto nla_put_failure;
8583
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008584 algs = 0;
8585 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
8586 algs++;
8587 if (params->auth_alg & WPA_AUTH_ALG_SHARED)
8588 algs++;
8589 if (params->auth_alg & WPA_AUTH_ALG_LEAP)
8590 algs++;
8591 if (algs > 1) {
8592 wpa_printf(MSG_DEBUG, " * Leave out Auth Type for automatic "
8593 "selection");
8594 goto skip_auth_type;
8595 }
8596
8597 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
8598 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
8599 else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
8600 type = NL80211_AUTHTYPE_SHARED_KEY;
8601 else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
8602 type = NL80211_AUTHTYPE_NETWORK_EAP;
8603 else if (params->auth_alg & WPA_AUTH_ALG_FT)
8604 type = NL80211_AUTHTYPE_FT;
8605 else
8606 goto nla_put_failure;
8607
8608 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
8609 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
8610
8611skip_auth_type:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008612 ret = nl80211_set_conn_keys(params, msg);
8613 if (ret)
8614 goto nla_put_failure;
8615
8616 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8617 msg = NULL;
8618 if (ret) {
8619 wpa_printf(MSG_DEBUG, "nl80211: MLME connect failed: ret=%d "
8620 "(%s)", ret, strerror(-ret));
8621 goto nla_put_failure;
8622 }
8623 ret = 0;
8624 wpa_printf(MSG_DEBUG, "nl80211: Connect request send successfully");
8625
8626nla_put_failure:
8627 nlmsg_free(msg);
8628 return ret;
8629
8630}
8631
8632
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08008633static int wpa_driver_nl80211_connect(
8634 struct wpa_driver_nl80211_data *drv,
8635 struct wpa_driver_associate_params *params)
8636{
8637 int ret = wpa_driver_nl80211_try_connect(drv, params);
8638 if (ret == -EALREADY) {
8639 /*
8640 * cfg80211 does not currently accept new connections if
8641 * we are already connected. As a workaround, force
8642 * disconnection and try again.
8643 */
8644 wpa_printf(MSG_DEBUG, "nl80211: Explicitly "
8645 "disconnecting before reassociation "
8646 "attempt");
8647 if (wpa_driver_nl80211_disconnect(
8648 drv, WLAN_REASON_PREV_AUTH_NOT_VALID))
8649 return -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08008650 ret = wpa_driver_nl80211_try_connect(drv, params);
8651 }
8652 return ret;
8653}
8654
8655
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008656static int wpa_driver_nl80211_associate(
8657 void *priv, struct wpa_driver_associate_params *params)
8658{
8659 struct i802_bss *bss = priv;
8660 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008661 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008662 struct nl_msg *msg;
8663
8664 if (params->mode == IEEE80211_MODE_AP)
8665 return wpa_driver_nl80211_ap(drv, params);
8666
8667 if (params->mode == IEEE80211_MODE_IBSS)
8668 return wpa_driver_nl80211_ibss(drv, params);
8669
8670 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008671 enum nl80211_iftype nlmode = params->p2p ?
8672 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
8673
8674 if (wpa_driver_nl80211_set_mode(priv, nlmode) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008675 return -1;
8676 return wpa_driver_nl80211_connect(drv, params);
8677 }
8678
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07008679 nl80211_mark_disconnected(drv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008680
8681 msg = nlmsg_alloc();
8682 if (!msg)
8683 return -1;
8684
8685 wpa_printf(MSG_DEBUG, "nl80211: Associate (ifindex=%d)",
8686 drv->ifindex);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008687 nl80211_cmd(drv, msg, 0, NL80211_CMD_ASSOCIATE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008688
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008689 ret = nl80211_connect_common(drv, params, msg);
8690 if (ret)
8691 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008692
8693 if (params->prev_bssid) {
8694 wpa_printf(MSG_DEBUG, " * prev_bssid=" MACSTR,
8695 MAC2STR(params->prev_bssid));
8696 NLA_PUT(msg, NL80211_ATTR_PREV_BSSID, ETH_ALEN,
8697 params->prev_bssid);
8698 }
8699
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008700 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8701 msg = NULL;
8702 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008703 wpa_dbg(drv->ctx, MSG_DEBUG,
8704 "nl80211: MLME command failed (assoc): ret=%d (%s)",
8705 ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008706 nl80211_dump_scan(drv);
8707 goto nla_put_failure;
8708 }
8709 ret = 0;
8710 wpa_printf(MSG_DEBUG, "nl80211: Association request send "
8711 "successfully");
8712
8713nla_put_failure:
8714 nlmsg_free(msg);
8715 return ret;
8716}
8717
8718
8719static int nl80211_set_mode(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008720 int ifindex, enum nl80211_iftype mode)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008721{
8722 struct nl_msg *msg;
8723 int ret = -ENOBUFS;
8724
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008725 wpa_printf(MSG_DEBUG, "nl80211: Set mode ifindex %d iftype %d (%s)",
8726 ifindex, mode, nl80211_iftype_str(mode));
8727
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008728 msg = nlmsg_alloc();
8729 if (!msg)
8730 return -ENOMEM;
8731
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008732 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_INTERFACE);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008733 if (nl80211_set_iface_id(msg, drv->first_bss) < 0)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008734 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008735 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, mode);
8736
8737 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008738 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008739 if (!ret)
8740 return 0;
8741nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008742 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008743 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface %d to mode %d:"
8744 " %d (%s)", ifindex, mode, ret, strerror(-ret));
8745 return ret;
8746}
8747
8748
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008749static int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
8750 enum nl80211_iftype nlmode)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008751{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008752 struct wpa_driver_nl80211_data *drv = bss->drv;
8753 int ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008754 int i;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008755 int was_ap = is_ap_interface(drv->nlmode);
8756 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008757
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008758 res = nl80211_set_mode(drv, drv->ifindex, nlmode);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008759 if (res && nlmode == nl80211_get_ifmode(bss))
8760 res = 0;
8761
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008762 if (res == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008763 drv->nlmode = nlmode;
8764 ret = 0;
8765 goto done;
8766 }
8767
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008768 if (res == -ENODEV)
8769 return -1;
8770
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008771 if (nlmode == drv->nlmode) {
8772 wpa_printf(MSG_DEBUG, "nl80211: Interface already in "
8773 "requested mode - ignore error");
8774 ret = 0;
8775 goto done; /* Already in the requested mode */
8776 }
8777
8778 /* mac80211 doesn't allow mode changes while the device is up, so
8779 * take the device down, try to set the mode again, and bring the
8780 * device back up.
8781 */
8782 wpa_printf(MSG_DEBUG, "nl80211: Try mode change after setting "
8783 "interface down");
8784 for (i = 0; i < 10; i++) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008785 res = i802_set_iface_flags(bss, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008786 if (res == -EACCES || res == -ENODEV)
8787 break;
8788 if (res == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008789 /* Try to set the mode again while the interface is
8790 * down */
8791 ret = nl80211_set_mode(drv, drv->ifindex, nlmode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008792 if (ret == -EACCES)
8793 break;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008794 res = i802_set_iface_flags(bss, 1);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008795 if (res && !ret)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008796 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008797 else if (ret != -EBUSY)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008798 break;
8799 } else
8800 wpa_printf(MSG_DEBUG, "nl80211: Failed to set "
8801 "interface down");
8802 os_sleep(0, 100000);
8803 }
8804
8805 if (!ret) {
8806 wpa_printf(MSG_DEBUG, "nl80211: Mode change succeeded while "
8807 "interface is down");
8808 drv->nlmode = nlmode;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008809 drv->ignore_if_down_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008810 }
8811
8812done:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008813 if (ret) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008814 wpa_printf(MSG_DEBUG, "nl80211: Interface mode change to %d "
8815 "from %d failed", nlmode, drv->nlmode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008816 return ret;
8817 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008818
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008819 if (is_p2p_net_interface(nlmode))
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008820 nl80211_disable_11b_rates(drv, drv->ifindex, 1);
8821 else if (drv->disabled_11b_rates)
8822 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
8823
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008824 if (is_ap_interface(nlmode)) {
8825 nl80211_mgmt_unsubscribe(bss, "start AP");
8826 /* Setup additional AP mode functionality if needed */
8827 if (nl80211_setup_ap(bss))
8828 return -1;
8829 } else if (was_ap) {
8830 /* Remove additional AP mode functionality */
8831 nl80211_teardown_ap(bss);
8832 } else {
8833 nl80211_mgmt_unsubscribe(bss, "mode change");
8834 }
8835
Dmitry Shmidt04949592012-07-19 12:16:46 -07008836 if (!bss->in_deinit && !is_ap_interface(nlmode) &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008837 nl80211_mgmt_subscribe_non_ap(bss) < 0)
8838 wpa_printf(MSG_DEBUG, "nl80211: Failed to register Action "
8839 "frame processing - ignore for now");
8840
8841 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008842}
8843
8844
8845static int wpa_driver_nl80211_get_capa(void *priv,
8846 struct wpa_driver_capa *capa)
8847{
8848 struct i802_bss *bss = priv;
8849 struct wpa_driver_nl80211_data *drv = bss->drv;
8850 if (!drv->has_capability)
8851 return -1;
8852 os_memcpy(capa, &drv->capa, sizeof(*capa));
Dmitry Shmidt444d5672013-04-01 13:08:44 -07008853 if (drv->extended_capa && drv->extended_capa_mask) {
8854 capa->extended_capa = drv->extended_capa;
8855 capa->extended_capa_mask = drv->extended_capa_mask;
8856 capa->extended_capa_len = drv->extended_capa_len;
8857 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008858
8859 if ((capa->flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
8860 !drv->allow_p2p_device) {
8861 wpa_printf(MSG_DEBUG, "nl80211: Do not indicate P2P_DEVICE support (p2p_device=1 driver param not specified)");
8862 capa->flags &= ~WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE;
8863 }
8864
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008865 return 0;
8866}
8867
8868
8869static int wpa_driver_nl80211_set_operstate(void *priv, int state)
8870{
8871 struct i802_bss *bss = priv;
8872 struct wpa_driver_nl80211_data *drv = bss->drv;
8873
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008874 wpa_printf(MSG_DEBUG, "nl80211: Set %s operstate %d->%d (%s)",
8875 bss->ifname, drv->operstate, state,
8876 state ? "UP" : "DORMANT");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008877 drv->operstate = state;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008878 return netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, -1,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008879 state ? IF_OPER_UP : IF_OPER_DORMANT);
8880}
8881
8882
8883static int wpa_driver_nl80211_set_supp_port(void *priv, int authorized)
8884{
8885 struct i802_bss *bss = priv;
8886 struct wpa_driver_nl80211_data *drv = bss->drv;
8887 struct nl_msg *msg;
8888 struct nl80211_sta_flag_update upd;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008889 int ret = -ENOBUFS;
8890
8891 if (!drv->associated && is_zero_ether_addr(drv->bssid) && !authorized) {
8892 wpa_printf(MSG_DEBUG, "nl80211: Skip set_supp_port(unauthorized) while not associated");
8893 return 0;
8894 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008895
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07008896 wpa_printf(MSG_DEBUG, "nl80211: Set supplicant port %sauthorized for "
8897 MACSTR, authorized ? "" : "un", MAC2STR(drv->bssid));
8898
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008899 msg = nlmsg_alloc();
8900 if (!msg)
8901 return -ENOMEM;
8902
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008903 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008904
8905 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
8906 if_nametoindex(bss->ifname));
8907 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid);
8908
8909 os_memset(&upd, 0, sizeof(upd));
8910 upd.mask = BIT(NL80211_STA_FLAG_AUTHORIZED);
8911 if (authorized)
8912 upd.set = BIT(NL80211_STA_FLAG_AUTHORIZED);
8913 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
8914
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008915 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8916 msg = NULL;
8917 if (!ret)
8918 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008919 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008920 nlmsg_free(msg);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008921 wpa_printf(MSG_DEBUG, "nl80211: Failed to set STA flag: %d (%s)",
8922 ret, strerror(-ret));
8923 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008924}
8925
8926
Jouni Malinen75ecf522011-06-27 15:19:46 -07008927/* Set kernel driver on given frequency (MHz) */
8928static int i802_set_freq(void *priv, struct hostapd_freq_params *freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008929{
Jouni Malinen75ecf522011-06-27 15:19:46 -07008930 struct i802_bss *bss = priv;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008931 return wpa_driver_nl80211_set_freq(bss, freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008932}
8933
8934
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008935static inline int min_int(int a, int b)
8936{
8937 if (a < b)
8938 return a;
8939 return b;
8940}
8941
8942
8943static int get_key_handler(struct nl_msg *msg, void *arg)
8944{
8945 struct nlattr *tb[NL80211_ATTR_MAX + 1];
8946 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
8947
8948 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
8949 genlmsg_attrlen(gnlh, 0), NULL);
8950
8951 /*
8952 * TODO: validate the key index and mac address!
8953 * Otherwise, there's a race condition as soon as
8954 * the kernel starts sending key notifications.
8955 */
8956
8957 if (tb[NL80211_ATTR_KEY_SEQ])
8958 memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]),
8959 min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6));
8960 return NL_SKIP;
8961}
8962
8963
8964static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
8965 int idx, u8 *seq)
8966{
8967 struct i802_bss *bss = priv;
8968 struct wpa_driver_nl80211_data *drv = bss->drv;
8969 struct nl_msg *msg;
8970
8971 msg = nlmsg_alloc();
8972 if (!msg)
8973 return -ENOMEM;
8974
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008975 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_KEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008976
8977 if (addr)
8978 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
8979 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, idx);
8980 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
8981
8982 memset(seq, 0, 6);
8983
8984 return send_and_recv_msgs(drv, msg, get_key_handler, seq);
8985 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008986 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008987 return -ENOBUFS;
8988}
8989
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008990
8991static int i802_set_rts(void *priv, int rts)
8992{
8993 struct i802_bss *bss = priv;
8994 struct wpa_driver_nl80211_data *drv = bss->drv;
8995 struct nl_msg *msg;
8996 int ret = -ENOBUFS;
8997 u32 val;
8998
8999 msg = nlmsg_alloc();
9000 if (!msg)
9001 return -ENOMEM;
9002
9003 if (rts >= 2347)
9004 val = (u32) -1;
9005 else
9006 val = rts;
9007
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009008 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009009 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
9010 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD, val);
9011
9012 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009013 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009014 if (!ret)
9015 return 0;
9016nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009017 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009018 wpa_printf(MSG_DEBUG, "nl80211: Failed to set RTS threshold %d: "
9019 "%d (%s)", rts, ret, strerror(-ret));
9020 return ret;
9021}
9022
9023
9024static int i802_set_frag(void *priv, int frag)
9025{
9026 struct i802_bss *bss = priv;
9027 struct wpa_driver_nl80211_data *drv = bss->drv;
9028 struct nl_msg *msg;
9029 int ret = -ENOBUFS;
9030 u32 val;
9031
9032 msg = nlmsg_alloc();
9033 if (!msg)
9034 return -ENOMEM;
9035
9036 if (frag >= 2346)
9037 val = (u32) -1;
9038 else
9039 val = frag;
9040
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009041 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009042 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
9043 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD, val);
9044
9045 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009046 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009047 if (!ret)
9048 return 0;
9049nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009050 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009051 wpa_printf(MSG_DEBUG, "nl80211: Failed to set fragmentation threshold "
9052 "%d: %d (%s)", frag, ret, strerror(-ret));
9053 return ret;
9054}
9055
9056
9057static int i802_flush(void *priv)
9058{
9059 struct i802_bss *bss = priv;
9060 struct wpa_driver_nl80211_data *drv = bss->drv;
9061 struct nl_msg *msg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009062 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009063
9064 msg = nlmsg_alloc();
9065 if (!msg)
9066 return -1;
9067
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07009068 wpa_printf(MSG_DEBUG, "nl80211: flush -> DEL_STATION %s (all)",
9069 bss->ifname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009070 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009071
9072 /*
9073 * XXX: FIX! this needs to flush all VLANs too
9074 */
9075 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
9076 if_nametoindex(bss->ifname));
9077
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009078 res = send_and_recv_msgs(drv, msg, NULL, NULL);
9079 if (res) {
9080 wpa_printf(MSG_DEBUG, "nl80211: Station flush failed: ret=%d "
9081 "(%s)", res, strerror(-res));
9082 }
9083 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009084 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009085 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009086 return -ENOBUFS;
9087}
9088
9089
9090static int get_sta_handler(struct nl_msg *msg, void *arg)
9091{
9092 struct nlattr *tb[NL80211_ATTR_MAX + 1];
9093 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
9094 struct hostap_sta_driver_data *data = arg;
9095 struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
9096 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
9097 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
9098 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
9099 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
9100 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
9101 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03009102 [NL80211_STA_INFO_TX_FAILED] = { .type = NLA_U32 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009103 };
9104
9105 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9106 genlmsg_attrlen(gnlh, 0), NULL);
9107
9108 /*
9109 * TODO: validate the interface and mac address!
9110 * Otherwise, there's a race condition as soon as
9111 * the kernel starts sending station notifications.
9112 */
9113
9114 if (!tb[NL80211_ATTR_STA_INFO]) {
9115 wpa_printf(MSG_DEBUG, "sta stats missing!");
9116 return NL_SKIP;
9117 }
9118 if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
9119 tb[NL80211_ATTR_STA_INFO],
9120 stats_policy)) {
9121 wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
9122 return NL_SKIP;
9123 }
9124
9125 if (stats[NL80211_STA_INFO_INACTIVE_TIME])
9126 data->inactive_msec =
9127 nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
9128 if (stats[NL80211_STA_INFO_RX_BYTES])
9129 data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
9130 if (stats[NL80211_STA_INFO_TX_BYTES])
9131 data->tx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
9132 if (stats[NL80211_STA_INFO_RX_PACKETS])
9133 data->rx_packets =
9134 nla_get_u32(stats[NL80211_STA_INFO_RX_PACKETS]);
9135 if (stats[NL80211_STA_INFO_TX_PACKETS])
9136 data->tx_packets =
9137 nla_get_u32(stats[NL80211_STA_INFO_TX_PACKETS]);
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03009138 if (stats[NL80211_STA_INFO_TX_FAILED])
9139 data->tx_retry_failed =
9140 nla_get_u32(stats[NL80211_STA_INFO_TX_FAILED]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009141
9142 return NL_SKIP;
9143}
9144
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009145static int i802_read_sta_data(struct i802_bss *bss,
9146 struct hostap_sta_driver_data *data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009147 const u8 *addr)
9148{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009149 struct wpa_driver_nl80211_data *drv = bss->drv;
9150 struct nl_msg *msg;
9151
9152 os_memset(data, 0, sizeof(*data));
9153 msg = nlmsg_alloc();
9154 if (!msg)
9155 return -ENOMEM;
9156
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009157 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009158
9159 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
9160 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
9161
9162 return send_and_recv_msgs(drv, msg, get_sta_handler, data);
9163 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009164 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009165 return -ENOBUFS;
9166}
9167
9168
9169static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
9170 int cw_min, int cw_max, int burst_time)
9171{
9172 struct i802_bss *bss = priv;
9173 struct wpa_driver_nl80211_data *drv = bss->drv;
9174 struct nl_msg *msg;
9175 struct nlattr *txq, *params;
9176
9177 msg = nlmsg_alloc();
9178 if (!msg)
9179 return -1;
9180
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009181 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009182
9183 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
9184
9185 txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS);
9186 if (!txq)
9187 goto nla_put_failure;
9188
9189 /* We are only sending parameters for a single TXQ at a time */
9190 params = nla_nest_start(msg, 1);
9191 if (!params)
9192 goto nla_put_failure;
9193
9194 switch (queue) {
9195 case 0:
9196 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VO);
9197 break;
9198 case 1:
9199 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VI);
9200 break;
9201 case 2:
9202 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BE);
9203 break;
9204 case 3:
9205 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BK);
9206 break;
9207 }
9208 /* Burst time is configured in units of 0.1 msec and TXOP parameter in
9209 * 32 usec, so need to convert the value here. */
9210 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_TXOP, (burst_time * 100 + 16) / 32);
9211 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min);
9212 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max);
9213 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_AIFS, aifs);
9214
9215 nla_nest_end(msg, params);
9216
9217 nla_nest_end(msg, txq);
9218
9219 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
9220 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009221 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009222 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009223 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009224 return -1;
9225}
9226
9227
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009228static int i802_set_sta_vlan(struct i802_bss *bss, const u8 *addr,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009229 const char *ifname, int vlan_id)
9230{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009231 struct wpa_driver_nl80211_data *drv = bss->drv;
9232 struct nl_msg *msg;
9233 int ret = -ENOBUFS;
9234
9235 msg = nlmsg_alloc();
9236 if (!msg)
9237 return -ENOMEM;
9238
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009239 wpa_printf(MSG_DEBUG, "nl80211: %s[%d]: set_sta_vlan(" MACSTR
9240 ", ifname=%s[%d], vlan_id=%d)",
9241 bss->ifname, if_nametoindex(bss->ifname),
9242 MAC2STR(addr), ifname, if_nametoindex(ifname), vlan_id);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009243 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009244
9245 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
9246 if_nametoindex(bss->ifname));
9247 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
9248 NLA_PUT_U32(msg, NL80211_ATTR_STA_VLAN,
9249 if_nametoindex(ifname));
9250
9251 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009252 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009253 if (ret < 0) {
9254 wpa_printf(MSG_ERROR, "nl80211: NL80211_ATTR_STA_VLAN (addr="
9255 MACSTR " ifname=%s vlan_id=%d) failed: %d (%s)",
9256 MAC2STR(addr), ifname, vlan_id, ret,
9257 strerror(-ret));
9258 }
9259 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009260 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009261 return ret;
9262}
9263
9264
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009265static int i802_get_inact_sec(void *priv, const u8 *addr)
9266{
9267 struct hostap_sta_driver_data data;
9268 int ret;
9269
9270 data.inactive_msec = (unsigned long) -1;
9271 ret = i802_read_sta_data(priv, &data, addr);
9272 if (ret || data.inactive_msec == (unsigned long) -1)
9273 return -1;
9274 return data.inactive_msec / 1000;
9275}
9276
9277
9278static int i802_sta_clear_stats(void *priv, const u8 *addr)
9279{
9280#if 0
9281 /* TODO */
9282#endif
9283 return 0;
9284}
9285
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009286
9287static int i802_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
9288 int reason)
9289{
9290 struct i802_bss *bss = priv;
Dmitry Shmidt04949592012-07-19 12:16:46 -07009291 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009292 struct ieee80211_mgmt mgmt;
9293
Dmitry Shmidt04949592012-07-19 12:16:46 -07009294 if (drv->device_ap_sme)
9295 return wpa_driver_nl80211_sta_remove(bss, addr);
9296
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009297 memset(&mgmt, 0, sizeof(mgmt));
9298 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
9299 WLAN_FC_STYPE_DEAUTH);
9300 memcpy(mgmt.da, addr, ETH_ALEN);
9301 memcpy(mgmt.sa, own_addr, ETH_ALEN);
9302 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
9303 mgmt.u.deauth.reason_code = host_to_le16(reason);
9304 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
9305 IEEE80211_HDRLEN +
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009306 sizeof(mgmt.u.deauth), 0, 0, 0, 0,
9307 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009308}
9309
9310
9311static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
9312 int reason)
9313{
9314 struct i802_bss *bss = priv;
Dmitry Shmidt04949592012-07-19 12:16:46 -07009315 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009316 struct ieee80211_mgmt mgmt;
9317
Dmitry Shmidt04949592012-07-19 12:16:46 -07009318 if (drv->device_ap_sme)
9319 return wpa_driver_nl80211_sta_remove(bss, addr);
9320
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009321 memset(&mgmt, 0, sizeof(mgmt));
9322 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
9323 WLAN_FC_STYPE_DISASSOC);
9324 memcpy(mgmt.da, addr, ETH_ALEN);
9325 memcpy(mgmt.sa, own_addr, ETH_ALEN);
9326 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
9327 mgmt.u.disassoc.reason_code = host_to_le16(reason);
9328 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
9329 IEEE80211_HDRLEN +
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009330 sizeof(mgmt.u.disassoc), 0, 0, 0, 0,
9331 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009332}
9333
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009334
Jouni Malinen75ecf522011-06-27 15:19:46 -07009335static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
9336{
9337 int i;
9338 int *old;
9339
9340 wpa_printf(MSG_DEBUG, "nl80211: Add own interface ifindex %d",
9341 ifidx);
9342 for (i = 0; i < drv->num_if_indices; i++) {
9343 if (drv->if_indices[i] == 0) {
9344 drv->if_indices[i] = ifidx;
9345 return;
9346 }
9347 }
9348
9349 if (drv->if_indices != drv->default_if_indices)
9350 old = drv->if_indices;
9351 else
9352 old = NULL;
9353
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07009354 drv->if_indices = os_realloc_array(old, drv->num_if_indices + 1,
9355 sizeof(int));
Jouni Malinen75ecf522011-06-27 15:19:46 -07009356 if (!drv->if_indices) {
9357 if (!old)
9358 drv->if_indices = drv->default_if_indices;
9359 else
9360 drv->if_indices = old;
9361 wpa_printf(MSG_ERROR, "Failed to reallocate memory for "
9362 "interfaces");
9363 wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx);
9364 return;
9365 } else if (!old)
9366 os_memcpy(drv->if_indices, drv->default_if_indices,
9367 sizeof(drv->default_if_indices));
9368 drv->if_indices[drv->num_if_indices] = ifidx;
9369 drv->num_if_indices++;
9370}
9371
9372
9373static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
9374{
9375 int i;
9376
9377 for (i = 0; i < drv->num_if_indices; i++) {
9378 if (drv->if_indices[i] == ifidx) {
9379 drv->if_indices[i] = 0;
9380 break;
9381 }
9382 }
9383}
9384
9385
9386static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
9387{
9388 int i;
9389
9390 for (i = 0; i < drv->num_if_indices; i++)
9391 if (drv->if_indices[i] == ifidx)
9392 return 1;
9393
9394 return 0;
9395}
9396
9397
9398static int i802_set_wds_sta(void *priv, const u8 *addr, int aid, int val,
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07009399 const char *bridge_ifname, char *ifname_wds)
Jouni Malinen75ecf522011-06-27 15:19:46 -07009400{
9401 struct i802_bss *bss = priv;
9402 struct wpa_driver_nl80211_data *drv = bss->drv;
9403 char name[IFNAMSIZ + 1];
9404
9405 os_snprintf(name, sizeof(name), "%s.sta%d", bss->ifname, aid);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07009406 if (ifname_wds)
9407 os_strlcpy(ifname_wds, name, IFNAMSIZ + 1);
9408
Jouni Malinen75ecf522011-06-27 15:19:46 -07009409 wpa_printf(MSG_DEBUG, "nl80211: Set WDS STA addr=" MACSTR
9410 " aid=%d val=%d name=%s", MAC2STR(addr), aid, val, name);
9411 if (val) {
9412 if (!if_nametoindex(name)) {
9413 if (nl80211_create_iface(drv, name,
9414 NL80211_IFTYPE_AP_VLAN,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009415 bss->addr, 1, NULL, NULL, 0) <
9416 0)
Jouni Malinen75ecf522011-06-27 15:19:46 -07009417 return -1;
9418 if (bridge_ifname &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009419 linux_br_add_if(drv->global->ioctl_sock,
9420 bridge_ifname, name) < 0)
Jouni Malinen75ecf522011-06-27 15:19:46 -07009421 return -1;
9422 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07009423 if (linux_set_iface_flags(drv->global->ioctl_sock, name, 1)) {
9424 wpa_printf(MSG_ERROR, "nl80211: Failed to set WDS STA "
9425 "interface %s up", name);
9426 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07009427 return i802_set_sta_vlan(priv, addr, name, 0);
9428 } else {
Dmitry Shmidtaa532512012-09-24 10:35:31 -07009429 if (bridge_ifname)
9430 linux_br_del_if(drv->global->ioctl_sock, bridge_ifname,
9431 name);
9432
Jouni Malinen75ecf522011-06-27 15:19:46 -07009433 i802_set_sta_vlan(priv, addr, bss->ifname, 0);
9434 return wpa_driver_nl80211_if_remove(priv, WPA_IF_AP_VLAN,
9435 name);
9436 }
9437}
9438
9439
9440static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
9441{
9442 struct wpa_driver_nl80211_data *drv = eloop_ctx;
9443 struct sockaddr_ll lladdr;
9444 unsigned char buf[3000];
9445 int len;
9446 socklen_t fromlen = sizeof(lladdr);
9447
9448 len = recvfrom(sock, buf, sizeof(buf), 0,
9449 (struct sockaddr *)&lladdr, &fromlen);
9450 if (len < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07009451 wpa_printf(MSG_ERROR, "nl80211: EAPOL recv failed: %s",
9452 strerror(errno));
Jouni Malinen75ecf522011-06-27 15:19:46 -07009453 return;
9454 }
9455
9456 if (have_ifidx(drv, lladdr.sll_ifindex))
9457 drv_event_eapol_rx(drv->ctx, lladdr.sll_addr, buf, len);
9458}
9459
9460
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009461static int i802_check_bridge(struct wpa_driver_nl80211_data *drv,
9462 struct i802_bss *bss,
9463 const char *brname, const char *ifname)
9464{
9465 int ifindex;
9466 char in_br[IFNAMSIZ];
9467
9468 os_strlcpy(bss->brname, brname, IFNAMSIZ);
9469 ifindex = if_nametoindex(brname);
9470 if (ifindex == 0) {
9471 /*
9472 * Bridge was configured, but the bridge device does
9473 * not exist. Try to add it now.
9474 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009475 if (linux_br_add(drv->global->ioctl_sock, brname) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009476 wpa_printf(MSG_ERROR, "nl80211: Failed to add the "
9477 "bridge interface %s: %s",
9478 brname, strerror(errno));
9479 return -1;
9480 }
9481 bss->added_bridge = 1;
9482 add_ifidx(drv, if_nametoindex(brname));
9483 }
9484
9485 if (linux_br_get(in_br, ifname) == 0) {
9486 if (os_strcmp(in_br, brname) == 0)
9487 return 0; /* already in the bridge */
9488
9489 wpa_printf(MSG_DEBUG, "nl80211: Removing interface %s from "
9490 "bridge %s", ifname, in_br);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009491 if (linux_br_del_if(drv->global->ioctl_sock, in_br, ifname) <
9492 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009493 wpa_printf(MSG_ERROR, "nl80211: Failed to "
9494 "remove interface %s from bridge "
9495 "%s: %s",
9496 ifname, brname, strerror(errno));
9497 return -1;
9498 }
9499 }
9500
9501 wpa_printf(MSG_DEBUG, "nl80211: Adding interface %s into bridge %s",
9502 ifname, brname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009503 if (linux_br_add_if(drv->global->ioctl_sock, brname, ifname) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009504 wpa_printf(MSG_ERROR, "nl80211: Failed to add interface %s "
9505 "into bridge %s: %s",
9506 ifname, brname, strerror(errno));
9507 return -1;
9508 }
9509 bss->added_if_into_bridge = 1;
9510
9511 return 0;
9512}
9513
9514
9515static void *i802_init(struct hostapd_data *hapd,
9516 struct wpa_init_params *params)
9517{
9518 struct wpa_driver_nl80211_data *drv;
9519 struct i802_bss *bss;
9520 size_t i;
9521 char brname[IFNAMSIZ];
9522 int ifindex, br_ifindex;
9523 int br_added = 0;
9524
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08009525 bss = wpa_driver_nl80211_drv_init(hapd, params->ifname,
9526 params->global_priv, 1,
9527 params->bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009528 if (bss == NULL)
9529 return NULL;
9530
9531 drv = bss->drv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009532
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009533 if (linux_br_get(brname, params->ifname) == 0) {
9534 wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in bridge %s",
9535 params->ifname, brname);
9536 br_ifindex = if_nametoindex(brname);
9537 } else {
9538 brname[0] = '\0';
9539 br_ifindex = 0;
9540 }
9541
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009542 for (i = 0; i < params->num_bridge; i++) {
9543 if (params->bridge[i]) {
9544 ifindex = if_nametoindex(params->bridge[i]);
9545 if (ifindex)
9546 add_ifidx(drv, ifindex);
9547 if (ifindex == br_ifindex)
9548 br_added = 1;
9549 }
9550 }
9551 if (!br_added && br_ifindex &&
9552 (params->num_bridge == 0 || !params->bridge[0]))
9553 add_ifidx(drv, br_ifindex);
9554
9555 /* start listening for EAPOL on the default AP interface */
9556 add_ifidx(drv, drv->ifindex);
9557
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009558 if (params->num_bridge && params->bridge[0] &&
9559 i802_check_bridge(drv, bss, params->bridge[0], params->ifname) < 0)
9560 goto failed;
9561
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009562 drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
9563 if (drv->eapol_sock < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07009564 wpa_printf(MSG_ERROR, "nl80211: socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE) failed: %s",
9565 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009566 goto failed;
9567 }
9568
9569 if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
9570 {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07009571 wpa_printf(MSG_INFO, "nl80211: Could not register read socket for eapol");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009572 goto failed;
9573 }
9574
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009575 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
9576 params->own_addr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009577 goto failed;
9578
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009579 memcpy(bss->addr, params->own_addr, ETH_ALEN);
9580
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009581 return bss;
9582
9583failed:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009584 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009585 return NULL;
9586}
9587
9588
9589static void i802_deinit(void *priv)
9590{
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009591 struct i802_bss *bss = priv;
9592 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009593}
9594
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009595
9596static enum nl80211_iftype wpa_driver_nl80211_if_type(
9597 enum wpa_driver_if_type type)
9598{
9599 switch (type) {
9600 case WPA_IF_STATION:
9601 return NL80211_IFTYPE_STATION;
9602 case WPA_IF_P2P_CLIENT:
9603 case WPA_IF_P2P_GROUP:
9604 return NL80211_IFTYPE_P2P_CLIENT;
9605 case WPA_IF_AP_VLAN:
9606 return NL80211_IFTYPE_AP_VLAN;
9607 case WPA_IF_AP_BSS:
9608 return NL80211_IFTYPE_AP;
9609 case WPA_IF_P2P_GO:
9610 return NL80211_IFTYPE_P2P_GO;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009611 case WPA_IF_P2P_DEVICE:
9612 return NL80211_IFTYPE_P2P_DEVICE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009613 }
9614 return -1;
9615}
9616
9617
9618#ifdef CONFIG_P2P
9619
9620static int nl80211_addr_in_use(struct nl80211_global *global, const u8 *addr)
9621{
9622 struct wpa_driver_nl80211_data *drv;
9623 dl_list_for_each(drv, &global->interfaces,
9624 struct wpa_driver_nl80211_data, list) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009625 if (os_memcmp(addr, drv->first_bss->addr, ETH_ALEN) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009626 return 1;
9627 }
9628 return 0;
9629}
9630
9631
9632static int nl80211_p2p_interface_addr(struct wpa_driver_nl80211_data *drv,
9633 u8 *new_addr)
9634{
9635 unsigned int idx;
9636
9637 if (!drv->global)
9638 return -1;
9639
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009640 os_memcpy(new_addr, drv->first_bss->addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009641 for (idx = 0; idx < 64; idx++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009642 new_addr[0] = drv->first_bss->addr[0] | 0x02;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009643 new_addr[0] ^= idx << 2;
9644 if (!nl80211_addr_in_use(drv->global, new_addr))
9645 break;
9646 }
9647 if (idx == 64)
9648 return -1;
9649
9650 wpa_printf(MSG_DEBUG, "nl80211: Assigned new P2P Interface Address "
9651 MACSTR, MAC2STR(new_addr));
9652
9653 return 0;
9654}
9655
9656#endif /* CONFIG_P2P */
9657
9658
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009659struct wdev_info {
9660 u64 wdev_id;
9661 int wdev_id_set;
9662 u8 macaddr[ETH_ALEN];
9663};
9664
9665static int nl80211_wdev_handler(struct nl_msg *msg, void *arg)
9666{
9667 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
9668 struct nlattr *tb[NL80211_ATTR_MAX + 1];
9669 struct wdev_info *wi = arg;
9670
9671 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9672 genlmsg_attrlen(gnlh, 0), NULL);
9673 if (tb[NL80211_ATTR_WDEV]) {
9674 wi->wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
9675 wi->wdev_id_set = 1;
9676 }
9677
9678 if (tb[NL80211_ATTR_MAC])
9679 os_memcpy(wi->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
9680 ETH_ALEN);
9681
9682 return NL_SKIP;
9683}
9684
9685
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009686static int wpa_driver_nl80211_if_add(void *priv, enum wpa_driver_if_type type,
9687 const char *ifname, const u8 *addr,
9688 void *bss_ctx, void **drv_priv,
9689 char *force_ifname, u8 *if_addr,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009690 const char *bridge, int use_existing)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009691{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009692 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009693 struct i802_bss *bss = priv;
9694 struct wpa_driver_nl80211_data *drv = bss->drv;
9695 int ifidx;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009696 int added = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009697
9698 if (addr)
9699 os_memcpy(if_addr, addr, ETH_ALEN);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009700 nlmode = wpa_driver_nl80211_if_type(type);
9701 if (nlmode == NL80211_IFTYPE_P2P_DEVICE) {
9702 struct wdev_info p2pdev_info;
9703
9704 os_memset(&p2pdev_info, 0, sizeof(p2pdev_info));
9705 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
9706 0, nl80211_wdev_handler,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009707 &p2pdev_info, use_existing);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009708 if (!p2pdev_info.wdev_id_set || ifidx != 0) {
9709 wpa_printf(MSG_ERROR, "nl80211: Failed to create a P2P Device interface %s",
9710 ifname);
9711 return -1;
9712 }
9713
9714 drv->global->if_add_wdevid = p2pdev_info.wdev_id;
9715 drv->global->if_add_wdevid_set = p2pdev_info.wdev_id_set;
9716 if (!is_zero_ether_addr(p2pdev_info.macaddr))
9717 os_memcpy(if_addr, p2pdev_info.macaddr, ETH_ALEN);
9718 wpa_printf(MSG_DEBUG, "nl80211: New P2P Device interface %s (0x%llx) created",
9719 ifname,
9720 (long long unsigned int) p2pdev_info.wdev_id);
9721 } else {
9722 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009723 0, NULL, NULL, use_existing);
9724 if (use_existing && ifidx == -ENFILE) {
9725 added = 0;
9726 ifidx = if_nametoindex(ifname);
9727 } else if (ifidx < 0) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009728 return -1;
9729 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009730 }
9731
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009732 if (!addr) {
9733 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
9734 os_memcpy(if_addr, bss->addr, ETH_ALEN);
9735 else if (linux_get_ifhwaddr(drv->global->ioctl_sock,
9736 bss->ifname, if_addr) < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009737 if (added)
9738 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009739 return -1;
9740 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009741 }
9742
9743#ifdef CONFIG_P2P
9744 if (!addr &&
9745 (type == WPA_IF_P2P_CLIENT || type == WPA_IF_P2P_GROUP ||
9746 type == WPA_IF_P2P_GO)) {
9747 /* Enforce unique P2P Interface Address */
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009748 u8 new_addr[ETH_ALEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009749
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009750 if (linux_get_ifhwaddr(drv->global->ioctl_sock, ifname,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009751 new_addr) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009752 nl80211_remove_iface(drv, ifidx);
9753 return -1;
9754 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009755 if (nl80211_addr_in_use(drv->global, new_addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009756 wpa_printf(MSG_DEBUG, "nl80211: Allocate new address "
9757 "for P2P group interface");
9758 if (nl80211_p2p_interface_addr(drv, new_addr) < 0) {
9759 nl80211_remove_iface(drv, ifidx);
9760 return -1;
9761 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009762 if (linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009763 new_addr) < 0) {
9764 nl80211_remove_iface(drv, ifidx);
9765 return -1;
9766 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009767 }
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07009768 os_memcpy(if_addr, new_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009769 }
9770#endif /* CONFIG_P2P */
9771
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009772 if (type == WPA_IF_AP_BSS) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07009773 struct i802_bss *new_bss = os_zalloc(sizeof(*new_bss));
9774 if (new_bss == NULL) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009775 if (added)
9776 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07009777 return -1;
9778 }
9779
9780 if (bridge &&
9781 i802_check_bridge(drv, new_bss, bridge, ifname) < 0) {
9782 wpa_printf(MSG_ERROR, "nl80211: Failed to add the new "
9783 "interface %s to a bridge %s",
9784 ifname, bridge);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009785 if (added)
9786 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07009787 os_free(new_bss);
9788 return -1;
9789 }
9790
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009791 if (linux_set_iface_flags(drv->global->ioctl_sock, ifname, 1))
9792 {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009793 nl80211_remove_iface(drv, ifidx);
9794 os_free(new_bss);
9795 return -1;
9796 }
9797 os_strlcpy(new_bss->ifname, ifname, IFNAMSIZ);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009798 os_memcpy(new_bss->addr, if_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009799 new_bss->ifindex = ifidx;
9800 new_bss->drv = drv;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009801 new_bss->next = drv->first_bss->next;
9802 new_bss->freq = drv->first_bss->freq;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08009803 new_bss->ctx = bss_ctx;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009804 new_bss->added_if = added;
9805 drv->first_bss->next = new_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009806 if (drv_priv)
9807 *drv_priv = new_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009808 nl80211_init_bss(new_bss);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08009809
9810 /* Subscribe management frames for this WPA_IF_AP_BSS */
9811 if (nl80211_setup_ap(new_bss))
9812 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009813 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009814
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009815 if (drv->global)
9816 drv->global->if_add_ifindex = ifidx;
9817
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009818 return 0;
9819}
9820
9821
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009822static int wpa_driver_nl80211_if_remove(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009823 enum wpa_driver_if_type type,
9824 const char *ifname)
9825{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009826 struct wpa_driver_nl80211_data *drv = bss->drv;
9827 int ifindex = if_nametoindex(ifname);
9828
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009829 wpa_printf(MSG_DEBUG, "nl80211: %s(type=%d ifname=%s) ifindex=%d added_if=%d",
9830 __func__, type, ifname, ifindex, bss->added_if);
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08009831 if (ifindex > 0 && (bss->added_if || bss->ifindex != ifindex))
Dmitry Shmidt051af732013-10-22 13:52:46 -07009832 nl80211_remove_iface(drv, ifindex);
Dmitry Shmidtaa532512012-09-24 10:35:31 -07009833
Dmitry Shmidtaa532512012-09-24 10:35:31 -07009834 if (type != WPA_IF_AP_BSS)
9835 return 0;
9836
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009837 if (bss->added_if_into_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009838 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
9839 bss->ifname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009840 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
9841 "interface %s from bridge %s: %s",
9842 bss->ifname, bss->brname, strerror(errno));
9843 }
9844 if (bss->added_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009845 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009846 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
9847 "bridge %s: %s",
9848 bss->brname, strerror(errno));
9849 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009850
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009851 if (bss != drv->first_bss) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009852 struct i802_bss *tbss;
9853
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009854 wpa_printf(MSG_DEBUG, "nl80211: Not the first BSS - remove it");
9855 for (tbss = drv->first_bss; tbss; tbss = tbss->next) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009856 if (tbss->next == bss) {
9857 tbss->next = bss->next;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08009858 /* Unsubscribe management frames */
9859 nl80211_teardown_ap(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009860 nl80211_destroy_bss(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009861 os_free(bss);
9862 bss = NULL;
9863 break;
9864 }
9865 }
9866 if (bss)
9867 wpa_printf(MSG_INFO, "nl80211: %s - could not find "
9868 "BSS %p in the list", __func__, bss);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009869 } else {
9870 wpa_printf(MSG_DEBUG, "nl80211: First BSS - reassign context");
9871 nl80211_teardown_ap(bss);
9872 if (!bss->added_if && !drv->first_bss->next)
9873 wpa_driver_nl80211_del_beacon(drv);
9874 nl80211_destroy_bss(bss);
9875 if (!bss->added_if)
9876 i802_set_iface_flags(bss, 0);
9877 if (drv->first_bss->next) {
9878 drv->first_bss = drv->first_bss->next;
9879 drv->ctx = drv->first_bss->ctx;
9880 os_free(bss);
9881 } else {
9882 wpa_printf(MSG_DEBUG, "nl80211: No second BSS to reassign context to");
9883 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009884 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009885
9886 return 0;
9887}
9888
9889
9890static int cookie_handler(struct nl_msg *msg, void *arg)
9891{
9892 struct nlattr *tb[NL80211_ATTR_MAX + 1];
9893 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
9894 u64 *cookie = arg;
9895 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9896 genlmsg_attrlen(gnlh, 0), NULL);
9897 if (tb[NL80211_ATTR_COOKIE])
9898 *cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
9899 return NL_SKIP;
9900}
9901
9902
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009903static int nl80211_send_frame_cmd(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009904 unsigned int freq, unsigned int wait,
9905 const u8 *buf, size_t buf_len,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009906 u64 *cookie_out, int no_cck, int no_ack,
9907 int offchanok)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009908{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009909 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009910 struct nl_msg *msg;
9911 u64 cookie;
9912 int ret = -1;
9913
9914 msg = nlmsg_alloc();
9915 if (!msg)
9916 return -1;
9917
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07009918 wpa_printf(MSG_MSGDUMP, "nl80211: CMD_FRAME freq=%u wait=%u no_cck=%d "
Dmitry Shmidt04949592012-07-19 12:16:46 -07009919 "no_ack=%d offchanok=%d",
9920 freq, wait, no_cck, no_ack, offchanok);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07009921 wpa_hexdump(MSG_MSGDUMP, "CMD_FRAME", buf, buf_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009922 nl80211_cmd(drv, msg, 0, NL80211_CMD_FRAME);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009923
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009924 if (nl80211_set_iface_id(msg, bss) < 0)
9925 goto nla_put_failure;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07009926 if (freq)
9927 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07009928 if (wait)
9929 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, wait);
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08009930 if (offchanok && ((drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
9931 drv->test_use_roc_tx))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009932 NLA_PUT_FLAG(msg, NL80211_ATTR_OFFCHANNEL_TX_OK);
9933 if (no_cck)
9934 NLA_PUT_FLAG(msg, NL80211_ATTR_TX_NO_CCK_RATE);
9935 if (no_ack)
9936 NLA_PUT_FLAG(msg, NL80211_ATTR_DONT_WAIT_FOR_ACK);
9937
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009938 NLA_PUT(msg, NL80211_ATTR_FRAME, buf_len, buf);
9939
9940 cookie = 0;
9941 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
9942 msg = NULL;
9943 if (ret) {
9944 wpa_printf(MSG_DEBUG, "nl80211: Frame command failed: ret=%d "
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07009945 "(%s) (freq=%u wait=%u)", ret, strerror(-ret),
9946 freq, wait);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009947 goto nla_put_failure;
9948 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07009949 wpa_printf(MSG_MSGDUMP, "nl80211: Frame TX command accepted%s; "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009950 "cookie 0x%llx", no_ack ? " (no ACK)" : "",
9951 (long long unsigned int) cookie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009952
9953 if (cookie_out)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009954 *cookie_out = no_ack ? (u64) -1 : cookie;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009955
9956nla_put_failure:
9957 nlmsg_free(msg);
9958 return ret;
9959}
9960
9961
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009962static int wpa_driver_nl80211_send_action(struct i802_bss *bss,
9963 unsigned int freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009964 unsigned int wait_time,
9965 const u8 *dst, const u8 *src,
9966 const u8 *bssid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009967 const u8 *data, size_t data_len,
9968 int no_cck)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009969{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009970 struct wpa_driver_nl80211_data *drv = bss->drv;
9971 int ret = -1;
9972 u8 *buf;
9973 struct ieee80211_hdr *hdr;
9974
9975 wpa_printf(MSG_DEBUG, "nl80211: Send Action frame (ifindex=%d, "
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08009976 "freq=%u MHz wait=%d ms no_cck=%d)",
9977 drv->ifindex, freq, wait_time, no_cck);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009978
9979 buf = os_zalloc(24 + data_len);
9980 if (buf == NULL)
9981 return ret;
9982 os_memcpy(buf + 24, data, data_len);
9983 hdr = (struct ieee80211_hdr *) buf;
9984 hdr->frame_control =
9985 IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_ACTION);
9986 os_memcpy(hdr->addr1, dst, ETH_ALEN);
9987 os_memcpy(hdr->addr2, src, ETH_ALEN);
9988 os_memcpy(hdr->addr3, bssid, ETH_ALEN);
9989
Dmitry Shmidt56052862013-10-04 10:23:25 -07009990 if (is_ap_interface(drv->nlmode) &&
9991 (!(drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
9992 (int) freq == bss->freq || drv->device_ap_sme ||
9993 !drv->use_monitor))
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009994 ret = wpa_driver_nl80211_send_mlme(bss, buf, 24 + data_len,
9995 0, freq, no_cck, 1,
9996 wait_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009997 else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009998 ret = nl80211_send_frame_cmd(bss, freq, wait_time, buf,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009999 24 + data_len,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010000 &drv->send_action_cookie,
10001 no_cck, 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010002
10003 os_free(buf);
10004 return ret;
10005}
10006
10007
10008static void wpa_driver_nl80211_send_action_cancel_wait(void *priv)
10009{
10010 struct i802_bss *bss = priv;
10011 struct wpa_driver_nl80211_data *drv = bss->drv;
10012 struct nl_msg *msg;
10013 int ret;
10014
10015 msg = nlmsg_alloc();
10016 if (!msg)
10017 return;
10018
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -080010019 wpa_printf(MSG_DEBUG, "nl80211: Cancel TX frame wait: cookie=0x%llx",
10020 (long long unsigned int) drv->send_action_cookie);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010021 nl80211_cmd(drv, msg, 0, NL80211_CMD_FRAME_WAIT_CANCEL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010022
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010023 if (nl80211_set_iface_id(msg, bss) < 0)
10024 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010025 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->send_action_cookie);
10026
10027 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
10028 msg = NULL;
10029 if (ret)
10030 wpa_printf(MSG_DEBUG, "nl80211: wait cancel failed: ret=%d "
10031 "(%s)", ret, strerror(-ret));
10032
10033 nla_put_failure:
10034 nlmsg_free(msg);
10035}
10036
10037
10038static int wpa_driver_nl80211_remain_on_channel(void *priv, unsigned int freq,
10039 unsigned int duration)
10040{
10041 struct i802_bss *bss = priv;
10042 struct wpa_driver_nl80211_data *drv = bss->drv;
10043 struct nl_msg *msg;
10044 int ret;
10045 u64 cookie;
10046
10047 msg = nlmsg_alloc();
10048 if (!msg)
10049 return -1;
10050
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010051 nl80211_cmd(drv, msg, 0, NL80211_CMD_REMAIN_ON_CHANNEL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010052
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010053 if (nl80211_set_iface_id(msg, bss) < 0)
10054 goto nla_put_failure;
10055
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010056 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
10057 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
10058
10059 cookie = 0;
10060 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010061 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010062 if (ret == 0) {
10063 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel cookie "
10064 "0x%llx for freq=%u MHz duration=%u",
10065 (long long unsigned int) cookie, freq, duration);
10066 drv->remain_on_chan_cookie = cookie;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010067 drv->pending_remain_on_chan = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010068 return 0;
10069 }
10070 wpa_printf(MSG_DEBUG, "nl80211: Failed to request remain-on-channel "
10071 "(freq=%d duration=%u): %d (%s)",
10072 freq, duration, ret, strerror(-ret));
10073nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010074 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010075 return -1;
10076}
10077
10078
10079static int wpa_driver_nl80211_cancel_remain_on_channel(void *priv)
10080{
10081 struct i802_bss *bss = priv;
10082 struct wpa_driver_nl80211_data *drv = bss->drv;
10083 struct nl_msg *msg;
10084 int ret;
10085
10086 if (!drv->pending_remain_on_chan) {
10087 wpa_printf(MSG_DEBUG, "nl80211: No pending remain-on-channel "
10088 "to cancel");
10089 return -1;
10090 }
10091
10092 wpa_printf(MSG_DEBUG, "nl80211: Cancel remain-on-channel with cookie "
10093 "0x%llx",
10094 (long long unsigned int) drv->remain_on_chan_cookie);
10095
10096 msg = nlmsg_alloc();
10097 if (!msg)
10098 return -1;
10099
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010100 nl80211_cmd(drv, msg, 0, NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010101
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010102 if (nl80211_set_iface_id(msg, bss) < 0)
10103 goto nla_put_failure;
10104
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010105 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->remain_on_chan_cookie);
10106
10107 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010108 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010109 if (ret == 0)
10110 return 0;
10111 wpa_printf(MSG_DEBUG, "nl80211: Failed to cancel remain-on-channel: "
10112 "%d (%s)", ret, strerror(-ret));
10113nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010114 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010115 return -1;
10116}
10117
10118
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010119static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss, int report)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010120{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010121 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt6e933c12011-09-27 12:29:26 -070010122
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010123 if (!report) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010124 if (bss->nl_preq && drv->device_ap_sme &&
10125 is_ap_interface(drv->nlmode)) {
10126 /*
10127 * Do not disable Probe Request reporting that was
10128 * enabled in nl80211_setup_ap().
10129 */
10130 wpa_printf(MSG_DEBUG, "nl80211: Skip disabling of "
10131 "Probe Request reporting nl_preq=%p while "
10132 "in AP mode", bss->nl_preq);
10133 } else if (bss->nl_preq) {
10134 wpa_printf(MSG_DEBUG, "nl80211: Disable Probe Request "
10135 "reporting nl_preq=%p", bss->nl_preq);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070010136 nl80211_destroy_eloop_handle(&bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010137 }
10138 return 0;
10139 }
10140
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010141 if (bss->nl_preq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010142 wpa_printf(MSG_DEBUG, "nl80211: Probe Request reporting "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010143 "already on! nl_preq=%p", bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010144 return 0;
10145 }
10146
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010147 bss->nl_preq = nl_create_handle(drv->global->nl_cb, "preq");
10148 if (bss->nl_preq == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010149 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010150 wpa_printf(MSG_DEBUG, "nl80211: Enable Probe Request "
10151 "reporting nl_preq=%p", bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010152
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010153 if (nl80211_register_frame(bss, bss->nl_preq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010154 (WLAN_FC_TYPE_MGMT << 2) |
10155 (WLAN_FC_STYPE_PROBE_REQ << 4),
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010156 NULL, 0) < 0)
10157 goto out_err;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -070010158
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070010159 nl80211_register_eloop_read(&bss->nl_preq,
10160 wpa_driver_nl80211_event_receive,
10161 bss->nl_cb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010162
10163 return 0;
10164
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010165 out_err:
10166 nl_destroy_handles(&bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010167 return -1;
10168}
10169
10170
10171static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
10172 int ifindex, int disabled)
10173{
10174 struct nl_msg *msg;
10175 struct nlattr *bands, *band;
10176 int ret;
10177
10178 msg = nlmsg_alloc();
10179 if (!msg)
10180 return -1;
10181
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010182 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_TX_BITRATE_MASK);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010183 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
10184
10185 bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES);
10186 if (!bands)
10187 goto nla_put_failure;
10188
10189 /*
10190 * Disable 2 GHz rates 1, 2, 5.5, 11 Mbps by masking out everything
10191 * else apart from 6, 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS
10192 * rates. All 5 GHz rates are left enabled.
10193 */
10194 band = nla_nest_start(msg, NL80211_BAND_2GHZ);
10195 if (!band)
10196 goto nla_put_failure;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010197 if (disabled) {
10198 NLA_PUT(msg, NL80211_TXRATE_LEGACY, 8,
10199 "\x0c\x12\x18\x24\x30\x48\x60\x6c");
10200 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010201 nla_nest_end(msg, band);
10202
10203 nla_nest_end(msg, bands);
10204
10205 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
10206 msg = NULL;
10207 if (ret) {
10208 wpa_printf(MSG_DEBUG, "nl80211: Set TX rates failed: ret=%d "
10209 "(%s)", ret, strerror(-ret));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070010210 } else
10211 drv->disabled_11b_rates = disabled;
10212
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010213 return ret;
10214
10215nla_put_failure:
10216 nlmsg_free(msg);
10217 return -1;
10218}
10219
10220
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010221static int wpa_driver_nl80211_deinit_ap(void *priv)
10222{
10223 struct i802_bss *bss = priv;
10224 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010225 if (!is_ap_interface(drv->nlmode))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010226 return -1;
10227 wpa_driver_nl80211_del_beacon(drv);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070010228
10229 /*
10230 * If the P2P GO interface was dynamically added, then it is
10231 * possible that the interface change to station is not possible.
10232 */
10233 if (drv->nlmode == NL80211_IFTYPE_P2P_GO && bss->if_dynamic)
10234 return 0;
10235
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010236 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010237}
10238
10239
Dmitry Shmidtea69e842013-05-13 14:52:28 -070010240static int wpa_driver_nl80211_stop_ap(void *priv)
10241{
10242 struct i802_bss *bss = priv;
10243 struct wpa_driver_nl80211_data *drv = bss->drv;
10244 if (!is_ap_interface(drv->nlmode))
10245 return -1;
10246 wpa_driver_nl80211_del_beacon(drv);
10247 bss->beacon_set = 0;
10248 return 0;
10249}
10250
10251
Dmitry Shmidt04949592012-07-19 12:16:46 -070010252static int wpa_driver_nl80211_deinit_p2p_cli(void *priv)
10253{
10254 struct i802_bss *bss = priv;
10255 struct wpa_driver_nl80211_data *drv = bss->drv;
10256 if (drv->nlmode != NL80211_IFTYPE_P2P_CLIENT)
10257 return -1;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070010258
10259 /*
10260 * If the P2P Client interface was dynamically added, then it is
10261 * possible that the interface change to station is not possible.
10262 */
10263 if (bss->if_dynamic)
10264 return 0;
10265
Dmitry Shmidt04949592012-07-19 12:16:46 -070010266 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
10267}
10268
10269
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010270static void wpa_driver_nl80211_resume(void *priv)
10271{
10272 struct i802_bss *bss = priv;
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010273
10274 if (i802_set_iface_flags(bss, 1))
10275 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface up on resume event");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010276}
10277
10278
10279static int nl80211_send_ft_action(void *priv, u8 action, const u8 *target_ap,
10280 const u8 *ies, size_t ies_len)
10281{
10282 struct i802_bss *bss = priv;
10283 struct wpa_driver_nl80211_data *drv = bss->drv;
10284 int ret;
10285 u8 *data, *pos;
10286 size_t data_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010287 const u8 *own_addr = bss->addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010288
10289 if (action != 1) {
10290 wpa_printf(MSG_ERROR, "nl80211: Unsupported send_ft_action "
10291 "action %d", action);
10292 return -1;
10293 }
10294
10295 /*
10296 * Action frame payload:
10297 * Category[1] = 6 (Fast BSS Transition)
10298 * Action[1] = 1 (Fast BSS Transition Request)
10299 * STA Address
10300 * Target AP Address
10301 * FT IEs
10302 */
10303
10304 data_len = 2 + 2 * ETH_ALEN + ies_len;
10305 data = os_malloc(data_len);
10306 if (data == NULL)
10307 return -1;
10308 pos = data;
10309 *pos++ = 0x06; /* FT Action category */
10310 *pos++ = action;
10311 os_memcpy(pos, own_addr, ETH_ALEN);
10312 pos += ETH_ALEN;
10313 os_memcpy(pos, target_ap, ETH_ALEN);
10314 pos += ETH_ALEN;
10315 os_memcpy(pos, ies, ies_len);
10316
10317 ret = wpa_driver_nl80211_send_action(bss, drv->assoc_freq, 0,
10318 drv->bssid, own_addr, drv->bssid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010319 data, data_len, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010320 os_free(data);
10321
10322 return ret;
10323}
10324
10325
10326static int nl80211_signal_monitor(void *priv, int threshold, int hysteresis)
10327{
10328 struct i802_bss *bss = priv;
10329 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -070010330 struct nl_msg *msg;
10331 struct nlattr *cqm;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070010332 int ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010333
10334 wpa_printf(MSG_DEBUG, "nl80211: Signal monitor threshold=%d "
10335 "hysteresis=%d", threshold, hysteresis);
10336
10337 msg = nlmsg_alloc();
10338 if (!msg)
10339 return -1;
10340
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010341 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_CQM);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010342
10343 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
10344
Dmitry Shmidt8da800a2013-04-24 12:57:01 -070010345 cqm = nla_nest_start(msg, NL80211_ATTR_CQM);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010346 if (cqm == NULL)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070010347 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010348
Dmitry Shmidt8da800a2013-04-24 12:57:01 -070010349 NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_THOLD, threshold);
10350 NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_HYST, hysteresis);
10351 nla_nest_end(msg, cqm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010352
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070010353 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010354 msg = NULL;
10355
10356nla_put_failure:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010357 nlmsg_free(msg);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070010358 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010359}
10360
10361
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010362static int get_channel_width(struct nl_msg *msg, void *arg)
10363{
10364 struct nlattr *tb[NL80211_ATTR_MAX + 1];
10365 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
10366 struct wpa_signal_info *sig_change = arg;
10367
10368 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
10369 genlmsg_attrlen(gnlh, 0), NULL);
10370
10371 sig_change->center_frq1 = -1;
10372 sig_change->center_frq2 = -1;
10373 sig_change->chanwidth = CHAN_WIDTH_UNKNOWN;
10374
10375 if (tb[NL80211_ATTR_CHANNEL_WIDTH]) {
10376 sig_change->chanwidth = convert2width(
10377 nla_get_u32(tb[NL80211_ATTR_CHANNEL_WIDTH]));
10378 if (tb[NL80211_ATTR_CENTER_FREQ1])
10379 sig_change->center_frq1 =
10380 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]);
10381 if (tb[NL80211_ATTR_CENTER_FREQ2])
10382 sig_change->center_frq2 =
10383 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]);
10384 }
10385
10386 return NL_SKIP;
10387}
10388
10389
10390static int nl80211_get_channel_width(struct wpa_driver_nl80211_data *drv,
10391 struct wpa_signal_info *sig)
10392{
10393 struct nl_msg *msg;
10394
10395 msg = nlmsg_alloc();
10396 if (!msg)
10397 return -ENOMEM;
10398
10399 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_INTERFACE);
10400 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
10401
10402 return send_and_recv_msgs(drv, msg, get_channel_width, sig);
10403
10404nla_put_failure:
10405 nlmsg_free(msg);
10406 return -ENOBUFS;
10407}
10408
10409
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010410static int nl80211_signal_poll(void *priv, struct wpa_signal_info *si)
10411{
10412 struct i802_bss *bss = priv;
10413 struct wpa_driver_nl80211_data *drv = bss->drv;
10414 int res;
10415
10416 os_memset(si, 0, sizeof(*si));
10417 res = nl80211_get_link_signal(drv, si);
10418 if (res != 0)
10419 return res;
10420
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010421 res = nl80211_get_channel_width(drv, si);
10422 if (res != 0)
10423 return res;
10424
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010425 return nl80211_get_link_noise(drv, si);
10426}
10427
10428
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010429static int wpa_driver_nl80211_shared_freq(void *priv)
10430{
10431 struct i802_bss *bss = priv;
10432 struct wpa_driver_nl80211_data *drv = bss->drv;
10433 struct wpa_driver_nl80211_data *driver;
10434 int freq = 0;
10435
10436 /*
10437 * If the same PHY is in connected state with some other interface,
10438 * then retrieve the assoc freq.
10439 */
10440 wpa_printf(MSG_DEBUG, "nl80211: Get shared freq for PHY %s",
10441 drv->phyname);
10442
10443 dl_list_for_each(driver, &drv->global->interfaces,
10444 struct wpa_driver_nl80211_data, list) {
10445 if (drv == driver ||
10446 os_strcmp(drv->phyname, driver->phyname) != 0 ||
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010447 !driver->associated)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010448 continue;
10449
10450 wpa_printf(MSG_DEBUG, "nl80211: Found a match for PHY %s - %s "
10451 MACSTR,
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010452 driver->phyname, driver->first_bss->ifname,
10453 MAC2STR(driver->first_bss->addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -070010454 if (is_ap_interface(driver->nlmode))
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010455 freq = driver->first_bss->freq;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010456 else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010457 freq = nl80211_get_assoc_freq(driver);
10458 wpa_printf(MSG_DEBUG, "nl80211: Shared freq for PHY %s: %d",
10459 drv->phyname, freq);
10460 }
10461
10462 if (!freq)
10463 wpa_printf(MSG_DEBUG, "nl80211: No shared interface for "
10464 "PHY (%s) in associated state", drv->phyname);
10465
10466 return freq;
10467}
10468
10469
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010470static int nl80211_send_frame(void *priv, const u8 *data, size_t data_len,
10471 int encrypt)
10472{
10473 struct i802_bss *bss = priv;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080010474 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt, 0,
10475 0, 0, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010476}
10477
10478
10479static int nl80211_set_param(void *priv, const char *param)
10480{
10481 wpa_printf(MSG_DEBUG, "nl80211: driver param='%s'", param);
10482 if (param == NULL)
10483 return 0;
10484
10485#ifdef CONFIG_P2P
10486 if (os_strstr(param, "use_p2p_group_interface=1")) {
10487 struct i802_bss *bss = priv;
10488 struct wpa_driver_nl80211_data *drv = bss->drv;
10489
10490 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
10491 "interface");
10492 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
10493 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
10494 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010495
10496 if (os_strstr(param, "p2p_device=1")) {
10497 struct i802_bss *bss = priv;
10498 struct wpa_driver_nl80211_data *drv = bss->drv;
10499 drv->allow_p2p_device = 1;
10500 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010501#endif /* CONFIG_P2P */
10502
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080010503 if (os_strstr(param, "use_monitor=1")) {
10504 struct i802_bss *bss = priv;
10505 struct wpa_driver_nl80211_data *drv = bss->drv;
10506 drv->use_monitor = 1;
10507 }
10508
10509 if (os_strstr(param, "force_connect_cmd=1")) {
10510 struct i802_bss *bss = priv;
10511 struct wpa_driver_nl80211_data *drv = bss->drv;
10512 drv->capa.flags &= ~WPA_DRIVER_FLAGS_SME;
10513 }
10514
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -080010515 if (os_strstr(param, "no_offchannel_tx=1")) {
10516 struct i802_bss *bss = priv;
10517 struct wpa_driver_nl80211_data *drv = bss->drv;
10518 drv->capa.flags &= ~WPA_DRIVER_FLAGS_OFFCHANNEL_TX;
10519 drv->test_use_roc_tx = 1;
10520 }
10521
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010522 return 0;
10523}
10524
10525
10526static void * nl80211_global_init(void)
10527{
10528 struct nl80211_global *global;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010529 struct netlink_config *cfg;
10530
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010531 global = os_zalloc(sizeof(*global));
10532 if (global == NULL)
10533 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010534 global->ioctl_sock = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010535 dl_list_init(&global->interfaces);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010536 global->if_add_ifindex = -1;
10537
10538 cfg = os_zalloc(sizeof(*cfg));
10539 if (cfg == NULL)
10540 goto err;
10541
10542 cfg->ctx = global;
10543 cfg->newlink_cb = wpa_driver_nl80211_event_rtm_newlink;
10544 cfg->dellink_cb = wpa_driver_nl80211_event_rtm_dellink;
10545 global->netlink = netlink_init(cfg);
10546 if (global->netlink == NULL) {
10547 os_free(cfg);
10548 goto err;
10549 }
10550
10551 if (wpa_driver_nl80211_init_nl_global(global) < 0)
10552 goto err;
10553
10554 global->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
10555 if (global->ioctl_sock < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070010556 wpa_printf(MSG_ERROR, "nl80211: socket(PF_INET,SOCK_DGRAM) failed: %s",
10557 strerror(errno));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010558 goto err;
10559 }
10560
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010561 return global;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010562
10563err:
10564 nl80211_global_deinit(global);
10565 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010566}
10567
10568
10569static void nl80211_global_deinit(void *priv)
10570{
10571 struct nl80211_global *global = priv;
10572 if (global == NULL)
10573 return;
10574 if (!dl_list_empty(&global->interfaces)) {
10575 wpa_printf(MSG_ERROR, "nl80211: %u interface(s) remain at "
10576 "nl80211_global_deinit",
10577 dl_list_len(&global->interfaces));
10578 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010579
10580 if (global->netlink)
10581 netlink_deinit(global->netlink);
10582
10583 nl_destroy_handles(&global->nl);
10584
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070010585 if (global->nl_event)
10586 nl80211_destroy_eloop_handle(&global->nl_event);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010587
10588 nl_cb_put(global->nl_cb);
10589
10590 if (global->ioctl_sock >= 0)
10591 close(global->ioctl_sock);
10592
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010593 os_free(global);
10594}
10595
10596
10597static const char * nl80211_get_radio_name(void *priv)
10598{
10599 struct i802_bss *bss = priv;
10600 struct wpa_driver_nl80211_data *drv = bss->drv;
10601 return drv->phyname;
10602}
10603
10604
Jouni Malinen75ecf522011-06-27 15:19:46 -070010605static int nl80211_pmkid(struct i802_bss *bss, int cmd, const u8 *bssid,
10606 const u8 *pmkid)
10607{
10608 struct nl_msg *msg;
10609
10610 msg = nlmsg_alloc();
10611 if (!msg)
10612 return -ENOMEM;
10613
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010614 nl80211_cmd(bss->drv, msg, 0, cmd);
Jouni Malinen75ecf522011-06-27 15:19:46 -070010615
10616 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
10617 if (pmkid)
10618 NLA_PUT(msg, NL80211_ATTR_PMKID, 16, pmkid);
10619 if (bssid)
10620 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
10621
10622 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
10623 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010624 nlmsg_free(msg);
Jouni Malinen75ecf522011-06-27 15:19:46 -070010625 return -ENOBUFS;
10626}
10627
10628
10629static int nl80211_add_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
10630{
10631 struct i802_bss *bss = priv;
10632 wpa_printf(MSG_DEBUG, "nl80211: Add PMKID for " MACSTR, MAC2STR(bssid));
10633 return nl80211_pmkid(bss, NL80211_CMD_SET_PMKSA, bssid, pmkid);
10634}
10635
10636
10637static int nl80211_remove_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
10638{
10639 struct i802_bss *bss = priv;
10640 wpa_printf(MSG_DEBUG, "nl80211: Delete PMKID for " MACSTR,
10641 MAC2STR(bssid));
10642 return nl80211_pmkid(bss, NL80211_CMD_DEL_PMKSA, bssid, pmkid);
10643}
10644
10645
10646static int nl80211_flush_pmkid(void *priv)
10647{
10648 struct i802_bss *bss = priv;
10649 wpa_printf(MSG_DEBUG, "nl80211: Flush PMKIDs");
10650 return nl80211_pmkid(bss, NL80211_CMD_FLUSH_PMKSA, NULL, NULL);
10651}
10652
10653
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070010654static void clean_survey_results(struct survey_results *survey_results)
10655{
10656 struct freq_survey *survey, *tmp;
10657
10658 if (dl_list_empty(&survey_results->survey_list))
10659 return;
10660
10661 dl_list_for_each_safe(survey, tmp, &survey_results->survey_list,
10662 struct freq_survey, list) {
10663 dl_list_del(&survey->list);
10664 os_free(survey);
10665 }
10666}
10667
10668
10669static void add_survey(struct nlattr **sinfo, u32 ifidx,
10670 struct dl_list *survey_list)
10671{
10672 struct freq_survey *survey;
10673
10674 survey = os_zalloc(sizeof(struct freq_survey));
10675 if (!survey)
10676 return;
10677
10678 survey->ifidx = ifidx;
10679 survey->freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
10680 survey->filled = 0;
10681
10682 if (sinfo[NL80211_SURVEY_INFO_NOISE]) {
10683 survey->nf = (int8_t)
10684 nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
10685 survey->filled |= SURVEY_HAS_NF;
10686 }
10687
10688 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]) {
10689 survey->channel_time =
10690 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]);
10691 survey->filled |= SURVEY_HAS_CHAN_TIME;
10692 }
10693
10694 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]) {
10695 survey->channel_time_busy =
10696 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]);
10697 survey->filled |= SURVEY_HAS_CHAN_TIME_BUSY;
10698 }
10699
10700 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]) {
10701 survey->channel_time_rx =
10702 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]);
10703 survey->filled |= SURVEY_HAS_CHAN_TIME_RX;
10704 }
10705
10706 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]) {
10707 survey->channel_time_tx =
10708 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]);
10709 survey->filled |= SURVEY_HAS_CHAN_TIME_TX;
10710 }
10711
10712 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)",
10713 survey->freq,
10714 survey->nf,
10715 (unsigned long int) survey->channel_time,
10716 (unsigned long int) survey->channel_time_busy,
10717 (unsigned long int) survey->channel_time_tx,
10718 (unsigned long int) survey->channel_time_rx,
10719 survey->filled);
10720
10721 dl_list_add_tail(survey_list, &survey->list);
10722}
10723
10724
10725static int check_survey_ok(struct nlattr **sinfo, u32 surveyed_freq,
10726 unsigned int freq_filter)
10727{
10728 if (!freq_filter)
10729 return 1;
10730
10731 return freq_filter == surveyed_freq;
10732}
10733
10734
10735static int survey_handler(struct nl_msg *msg, void *arg)
10736{
10737 struct nlattr *tb[NL80211_ATTR_MAX + 1];
10738 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
10739 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
10740 struct survey_results *survey_results;
10741 u32 surveyed_freq = 0;
10742 u32 ifidx;
10743
10744 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
10745 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
10746 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
10747 };
10748
10749 survey_results = (struct survey_results *) arg;
10750
10751 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
10752 genlmsg_attrlen(gnlh, 0), NULL);
10753
Dmitry Shmidt97672262014-02-03 13:02:54 -080010754 if (!tb[NL80211_ATTR_IFINDEX])
10755 return NL_SKIP;
10756
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070010757 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
10758
10759 if (!tb[NL80211_ATTR_SURVEY_INFO])
10760 return NL_SKIP;
10761
10762 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
10763 tb[NL80211_ATTR_SURVEY_INFO],
10764 survey_policy))
10765 return NL_SKIP;
10766
10767 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY]) {
10768 wpa_printf(MSG_ERROR, "nl80211: Invalid survey data");
10769 return NL_SKIP;
10770 }
10771
10772 surveyed_freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
10773
10774 if (!check_survey_ok(sinfo, surveyed_freq,
10775 survey_results->freq_filter))
10776 return NL_SKIP;
10777
10778 if (survey_results->freq_filter &&
10779 survey_results->freq_filter != surveyed_freq) {
10780 wpa_printf(MSG_EXCESSIVE, "nl80211: Ignoring survey data for freq %d MHz",
10781 surveyed_freq);
10782 return NL_SKIP;
10783 }
10784
10785 add_survey(sinfo, ifidx, &survey_results->survey_list);
10786
10787 return NL_SKIP;
10788}
10789
10790
10791static int wpa_driver_nl80211_get_survey(void *priv, unsigned int freq)
10792{
10793 struct i802_bss *bss = priv;
10794 struct wpa_driver_nl80211_data *drv = bss->drv;
10795 struct nl_msg *msg;
10796 int err = -ENOBUFS;
10797 union wpa_event_data data;
10798 struct survey_results *survey_results;
10799
10800 os_memset(&data, 0, sizeof(data));
10801 survey_results = &data.survey_results;
10802
10803 dl_list_init(&survey_results->survey_list);
10804
10805 msg = nlmsg_alloc();
10806 if (!msg)
10807 goto nla_put_failure;
10808
10809 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
10810
10811 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
10812
10813 if (freq)
10814 data.survey_results.freq_filter = freq;
10815
10816 do {
10817 wpa_printf(MSG_DEBUG, "nl80211: Fetch survey data");
10818 err = send_and_recv_msgs(drv, msg, survey_handler,
10819 survey_results);
10820 } while (err > 0);
10821
10822 if (err) {
10823 wpa_printf(MSG_ERROR, "nl80211: Failed to process survey data");
10824 goto out_clean;
10825 }
10826
10827 wpa_supplicant_event(drv->ctx, EVENT_SURVEY, &data);
10828
10829out_clean:
10830 clean_survey_results(survey_results);
10831nla_put_failure:
10832 return err;
10833}
10834
10835
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010836static void nl80211_set_rekey_info(void *priv, const u8 *kek, const u8 *kck,
10837 const u8 *replay_ctr)
10838{
10839 struct i802_bss *bss = priv;
10840 struct wpa_driver_nl80211_data *drv = bss->drv;
10841 struct nlattr *replay_nested;
10842 struct nl_msg *msg;
10843
10844 msg = nlmsg_alloc();
10845 if (!msg)
10846 return;
10847
10848 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
10849
10850 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
10851
10852 replay_nested = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
10853 if (!replay_nested)
10854 goto nla_put_failure;
10855
10856 NLA_PUT(msg, NL80211_REKEY_DATA_KEK, NL80211_KEK_LEN, kek);
10857 NLA_PUT(msg, NL80211_REKEY_DATA_KCK, NL80211_KCK_LEN, kck);
10858 NLA_PUT(msg, NL80211_REKEY_DATA_REPLAY_CTR, NL80211_REPLAY_CTR_LEN,
10859 replay_ctr);
10860
10861 nla_nest_end(msg, replay_nested);
10862
10863 send_and_recv_msgs(drv, msg, NULL, NULL);
10864 return;
10865 nla_put_failure:
10866 nlmsg_free(msg);
10867}
10868
10869
10870static void nl80211_send_null_frame(struct i802_bss *bss, const u8 *own_addr,
10871 const u8 *addr, int qos)
10872{
10873 /* send data frame to poll STA and check whether
10874 * this frame is ACKed */
10875 struct {
10876 struct ieee80211_hdr hdr;
10877 u16 qos_ctl;
10878 } STRUCT_PACKED nulldata;
10879 size_t size;
10880
10881 /* Send data frame to poll STA and check whether this frame is ACKed */
10882
10883 os_memset(&nulldata, 0, sizeof(nulldata));
10884
10885 if (qos) {
10886 nulldata.hdr.frame_control =
10887 IEEE80211_FC(WLAN_FC_TYPE_DATA,
10888 WLAN_FC_STYPE_QOS_NULL);
10889 size = sizeof(nulldata);
10890 } else {
10891 nulldata.hdr.frame_control =
10892 IEEE80211_FC(WLAN_FC_TYPE_DATA,
10893 WLAN_FC_STYPE_NULLFUNC);
10894 size = sizeof(struct ieee80211_hdr);
10895 }
10896
10897 nulldata.hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS);
10898 os_memcpy(nulldata.hdr.IEEE80211_DA_FROMDS, addr, ETH_ALEN);
10899 os_memcpy(nulldata.hdr.IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
10900 os_memcpy(nulldata.hdr.IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
10901
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010902 if (wpa_driver_nl80211_send_mlme(bss, (u8 *) &nulldata, size, 0, 0, 0,
10903 0, 0) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010904 wpa_printf(MSG_DEBUG, "nl80211_send_null_frame: Failed to "
10905 "send poll frame");
10906}
10907
10908static void nl80211_poll_client(void *priv, const u8 *own_addr, const u8 *addr,
10909 int qos)
10910{
10911 struct i802_bss *bss = priv;
10912 struct wpa_driver_nl80211_data *drv = bss->drv;
10913 struct nl_msg *msg;
10914
10915 if (!drv->poll_command_supported) {
10916 nl80211_send_null_frame(bss, own_addr, addr, qos);
10917 return;
10918 }
10919
10920 msg = nlmsg_alloc();
10921 if (!msg)
10922 return;
10923
10924 nl80211_cmd(drv, msg, 0, NL80211_CMD_PROBE_CLIENT);
10925
10926 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
10927 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
10928
10929 send_and_recv_msgs(drv, msg, NULL, NULL);
10930 return;
10931 nla_put_failure:
10932 nlmsg_free(msg);
10933}
10934
10935
10936static int nl80211_set_power_save(struct i802_bss *bss, int enabled)
10937{
10938 struct nl_msg *msg;
10939
10940 msg = nlmsg_alloc();
10941 if (!msg)
10942 return -ENOMEM;
10943
10944 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_SET_POWER_SAVE);
10945 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
10946 NLA_PUT_U32(msg, NL80211_ATTR_PS_STATE,
10947 enabled ? NL80211_PS_ENABLED : NL80211_PS_DISABLED);
10948 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
10949nla_put_failure:
10950 nlmsg_free(msg);
10951 return -ENOBUFS;
10952}
10953
10954
10955static int nl80211_set_p2p_powersave(void *priv, int legacy_ps, int opp_ps,
10956 int ctwindow)
10957{
10958 struct i802_bss *bss = priv;
10959
10960 wpa_printf(MSG_DEBUG, "nl80211: set_p2p_powersave (legacy_ps=%d "
10961 "opp_ps=%d ctwindow=%d)", legacy_ps, opp_ps, ctwindow);
10962
Dmitry Shmidt292b0c32013-11-22 12:54:42 -080010963 if (opp_ps != -1 || ctwindow != -1) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080010964#ifdef ANDROID_P2P
10965 wpa_driver_set_p2p_ps(priv, legacy_ps, opp_ps, ctwindow);
Dmitry Shmidt292b0c32013-11-22 12:54:42 -080010966#else /* ANDROID_P2P */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010967 return -1; /* Not yet supported */
Dmitry Shmidt292b0c32013-11-22 12:54:42 -080010968#endif /* ANDROID_P2P */
10969 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010970
10971 if (legacy_ps == -1)
10972 return 0;
10973 if (legacy_ps != 0 && legacy_ps != 1)
10974 return -1; /* Not yet supported */
10975
10976 return nl80211_set_power_save(bss, legacy_ps);
10977}
10978
10979
Dmitry Shmidt051af732013-10-22 13:52:46 -070010980static int nl80211_start_radar_detection(void *priv,
10981 struct hostapd_freq_params *freq)
Dmitry Shmidtea69e842013-05-13 14:52:28 -070010982{
10983 struct i802_bss *bss = priv;
10984 struct wpa_driver_nl80211_data *drv = bss->drv;
10985 struct nl_msg *msg;
10986 int ret;
10987
Dmitry Shmidt051af732013-10-22 13:52:46 -070010988 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)",
10989 freq->freq, freq->ht_enabled, freq->vht_enabled,
10990 freq->bandwidth, freq->center_freq1, freq->center_freq2);
10991
Dmitry Shmidtea69e842013-05-13 14:52:28 -070010992 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_RADAR)) {
10993 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support radar "
10994 "detection");
10995 return -1;
10996 }
10997
10998 msg = nlmsg_alloc();
10999 if (!msg)
11000 return -1;
11001
11002 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_RADAR_DETECT);
11003 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
Dmitry Shmidt051af732013-10-22 13:52:46 -070011004 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq->freq);
Dmitry Shmidtea69e842013-05-13 14:52:28 -070011005
Dmitry Shmidt051af732013-10-22 13:52:46 -070011006 if (freq->vht_enabled) {
11007 switch (freq->bandwidth) {
11008 case 20:
11009 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
11010 NL80211_CHAN_WIDTH_20);
11011 break;
11012 case 40:
11013 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
11014 NL80211_CHAN_WIDTH_40);
11015 break;
11016 case 80:
11017 if (freq->center_freq2)
11018 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
11019 NL80211_CHAN_WIDTH_80P80);
11020 else
11021 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
11022 NL80211_CHAN_WIDTH_80);
11023 break;
11024 case 160:
11025 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
11026 NL80211_CHAN_WIDTH_160);
11027 break;
11028 default:
11029 return -1;
11030 }
11031 NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ1, freq->center_freq1);
11032 if (freq->center_freq2)
11033 NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ2,
11034 freq->center_freq2);
11035 } else if (freq->ht_enabled) {
11036 switch (freq->sec_channel_offset) {
11037 case -1:
11038 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
11039 NL80211_CHAN_HT40MINUS);
11040 break;
11041 case 1:
11042 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
11043 NL80211_CHAN_HT40PLUS);
11044 break;
11045 default:
11046 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
11047 NL80211_CHAN_HT20);
11048 break;
11049 }
11050 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -070011051
11052 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
11053 if (ret == 0)
11054 return 0;
11055 wpa_printf(MSG_DEBUG, "nl80211: Failed to start radar detection: "
11056 "%d (%s)", ret, strerror(-ret));
11057nla_put_failure:
11058 return -1;
11059}
11060
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011061#ifdef CONFIG_TDLS
11062
11063static int nl80211_send_tdls_mgmt(void *priv, const u8 *dst, u8 action_code,
11064 u8 dialog_token, u16 status_code,
11065 const u8 *buf, size_t len)
11066{
11067 struct i802_bss *bss = priv;
11068 struct wpa_driver_nl80211_data *drv = bss->drv;
11069 struct nl_msg *msg;
11070
11071 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
11072 return -EOPNOTSUPP;
11073
11074 if (!dst)
11075 return -EINVAL;
11076
11077 msg = nlmsg_alloc();
11078 if (!msg)
11079 return -ENOMEM;
11080
11081 nl80211_cmd(drv, msg, 0, NL80211_CMD_TDLS_MGMT);
11082 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
11083 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
11084 NLA_PUT_U8(msg, NL80211_ATTR_TDLS_ACTION, action_code);
11085 NLA_PUT_U8(msg, NL80211_ATTR_TDLS_DIALOG_TOKEN, dialog_token);
11086 NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status_code);
11087 NLA_PUT(msg, NL80211_ATTR_IE, len, buf);
11088
11089 return send_and_recv_msgs(drv, msg, NULL, NULL);
11090
11091nla_put_failure:
11092 nlmsg_free(msg);
11093 return -ENOBUFS;
11094}
11095
11096
11097static int nl80211_tdls_oper(void *priv, enum tdls_oper oper, const u8 *peer)
11098{
11099 struct i802_bss *bss = priv;
11100 struct wpa_driver_nl80211_data *drv = bss->drv;
11101 struct nl_msg *msg;
11102 enum nl80211_tdls_operation nl80211_oper;
11103
11104 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
11105 return -EOPNOTSUPP;
11106
11107 switch (oper) {
11108 case TDLS_DISCOVERY_REQ:
11109 nl80211_oper = NL80211_TDLS_DISCOVERY_REQ;
11110 break;
11111 case TDLS_SETUP:
11112 nl80211_oper = NL80211_TDLS_SETUP;
11113 break;
11114 case TDLS_TEARDOWN:
11115 nl80211_oper = NL80211_TDLS_TEARDOWN;
11116 break;
11117 case TDLS_ENABLE_LINK:
11118 nl80211_oper = NL80211_TDLS_ENABLE_LINK;
11119 break;
11120 case TDLS_DISABLE_LINK:
11121 nl80211_oper = NL80211_TDLS_DISABLE_LINK;
11122 break;
11123 case TDLS_ENABLE:
11124 return 0;
11125 case TDLS_DISABLE:
11126 return 0;
11127 default:
11128 return -EINVAL;
11129 }
11130
11131 msg = nlmsg_alloc();
11132 if (!msg)
11133 return -ENOMEM;
11134
11135 nl80211_cmd(drv, msg, 0, NL80211_CMD_TDLS_OPER);
11136 NLA_PUT_U8(msg, NL80211_ATTR_TDLS_OPERATION, nl80211_oper);
11137 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
11138 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, peer);
11139
11140 return send_and_recv_msgs(drv, msg, NULL, NULL);
11141
11142nla_put_failure:
11143 nlmsg_free(msg);
11144 return -ENOBUFS;
11145}
11146
11147#endif /* CONFIG TDLS */
11148
11149
11150#ifdef ANDROID
11151
11152typedef struct android_wifi_priv_cmd {
11153 char *buf;
11154 int used_len;
11155 int total_len;
11156} android_wifi_priv_cmd;
11157
11158static int drv_errors = 0;
11159
11160static void wpa_driver_send_hang_msg(struct wpa_driver_nl80211_data *drv)
11161{
11162 drv_errors++;
11163 if (drv_errors > DRV_NUMBER_SEQUENTIAL_ERRORS) {
11164 drv_errors = 0;
11165 wpa_msg(drv->ctx, MSG_INFO, WPA_EVENT_DRIVER_STATE "HANGED");
11166 }
11167}
11168
11169
11170static int android_priv_cmd(struct i802_bss *bss, const char *cmd)
11171{
11172 struct wpa_driver_nl80211_data *drv = bss->drv;
11173 struct ifreq ifr;
11174 android_wifi_priv_cmd priv_cmd;
11175 char buf[MAX_DRV_CMD_SIZE];
11176 int ret;
11177
11178 os_memset(&ifr, 0, sizeof(ifr));
11179 os_memset(&priv_cmd, 0, sizeof(priv_cmd));
11180 os_strlcpy(ifr.ifr_name, bss->ifname, IFNAMSIZ);
11181
11182 os_memset(buf, 0, sizeof(buf));
11183 os_strlcpy(buf, cmd, sizeof(buf));
11184
11185 priv_cmd.buf = buf;
11186 priv_cmd.used_len = sizeof(buf);
11187 priv_cmd.total_len = sizeof(buf);
11188 ifr.ifr_data = &priv_cmd;
11189
11190 ret = ioctl(drv->global->ioctl_sock, SIOCDEVPRIVATE + 1, &ifr);
11191 if (ret < 0) {
11192 wpa_printf(MSG_ERROR, "%s: failed to issue private commands",
11193 __func__);
11194 wpa_driver_send_hang_msg(drv);
11195 return ret;
11196 }
11197
11198 drv_errors = 0;
11199 return 0;
11200}
11201
11202
11203static int android_pno_start(struct i802_bss *bss,
11204 struct wpa_driver_scan_params *params)
11205{
11206 struct wpa_driver_nl80211_data *drv = bss->drv;
11207 struct ifreq ifr;
11208 android_wifi_priv_cmd priv_cmd;
11209 int ret = 0, i = 0, bp;
11210 char buf[WEXT_PNO_MAX_COMMAND_SIZE];
11211
11212 bp = WEXT_PNOSETUP_HEADER_SIZE;
11213 os_memcpy(buf, WEXT_PNOSETUP_HEADER, bp);
11214 buf[bp++] = WEXT_PNO_TLV_PREFIX;
11215 buf[bp++] = WEXT_PNO_TLV_VERSION;
11216 buf[bp++] = WEXT_PNO_TLV_SUBVERSION;
11217 buf[bp++] = WEXT_PNO_TLV_RESERVED;
11218
11219 while (i < WEXT_PNO_AMOUNT && (size_t) i < params->num_ssids) {
11220 /* Check that there is enough space needed for 1 more SSID, the
11221 * other sections and null termination */
11222 if ((bp + WEXT_PNO_SSID_HEADER_SIZE + MAX_SSID_LEN +
11223 WEXT_PNO_NONSSID_SECTIONS_SIZE + 1) >= (int) sizeof(buf))
11224 break;
11225 wpa_hexdump_ascii(MSG_DEBUG, "For PNO Scan",
11226 params->ssids[i].ssid,
11227 params->ssids[i].ssid_len);
11228 buf[bp++] = WEXT_PNO_SSID_SECTION;
11229 buf[bp++] = params->ssids[i].ssid_len;
11230 os_memcpy(&buf[bp], params->ssids[i].ssid,
11231 params->ssids[i].ssid_len);
11232 bp += params->ssids[i].ssid_len;
11233 i++;
11234 }
11235
11236 buf[bp++] = WEXT_PNO_SCAN_INTERVAL_SECTION;
11237 os_snprintf(&buf[bp], WEXT_PNO_SCAN_INTERVAL_LENGTH + 1, "%x",
11238 WEXT_PNO_SCAN_INTERVAL);
11239 bp += WEXT_PNO_SCAN_INTERVAL_LENGTH;
11240
11241 buf[bp++] = WEXT_PNO_REPEAT_SECTION;
11242 os_snprintf(&buf[bp], WEXT_PNO_REPEAT_LENGTH + 1, "%x",
11243 WEXT_PNO_REPEAT);
11244 bp += WEXT_PNO_REPEAT_LENGTH;
11245
11246 buf[bp++] = WEXT_PNO_MAX_REPEAT_SECTION;
11247 os_snprintf(&buf[bp], WEXT_PNO_MAX_REPEAT_LENGTH + 1, "%x",
11248 WEXT_PNO_MAX_REPEAT);
11249 bp += WEXT_PNO_MAX_REPEAT_LENGTH + 1;
11250
11251 memset(&ifr, 0, sizeof(ifr));
11252 memset(&priv_cmd, 0, sizeof(priv_cmd));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070011253 os_strlcpy(ifr.ifr_name, bss->ifname, IFNAMSIZ);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011254
11255 priv_cmd.buf = buf;
11256 priv_cmd.used_len = bp;
11257 priv_cmd.total_len = bp;
11258 ifr.ifr_data = &priv_cmd;
11259
11260 ret = ioctl(drv->global->ioctl_sock, SIOCDEVPRIVATE + 1, &ifr);
11261
11262 if (ret < 0) {
11263 wpa_printf(MSG_ERROR, "ioctl[SIOCSIWPRIV] (pnosetup): %d",
11264 ret);
11265 wpa_driver_send_hang_msg(drv);
11266 return ret;
11267 }
11268
11269 drv_errors = 0;
11270
11271 return android_priv_cmd(bss, "PNOFORCE 1");
11272}
11273
11274
11275static int android_pno_stop(struct i802_bss *bss)
11276{
11277 return android_priv_cmd(bss, "PNOFORCE 0");
11278}
11279
11280#endif /* ANDROID */
11281
11282
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011283static int driver_nl80211_set_key(const char *ifname, void *priv,
11284 enum wpa_alg alg, const u8 *addr,
11285 int key_idx, int set_tx,
11286 const u8 *seq, size_t seq_len,
11287 const u8 *key, size_t key_len)
11288{
11289 struct i802_bss *bss = priv;
11290 return wpa_driver_nl80211_set_key(ifname, bss, alg, addr, key_idx,
11291 set_tx, seq, seq_len, key, key_len);
11292}
11293
11294
11295static int driver_nl80211_scan2(void *priv,
11296 struct wpa_driver_scan_params *params)
11297{
11298 struct i802_bss *bss = priv;
11299 return wpa_driver_nl80211_scan(bss, params);
11300}
11301
11302
11303static int driver_nl80211_deauthenticate(void *priv, const u8 *addr,
11304 int reason_code)
11305{
11306 struct i802_bss *bss = priv;
11307 return wpa_driver_nl80211_deauthenticate(bss, addr, reason_code);
11308}
11309
11310
11311static int driver_nl80211_authenticate(void *priv,
11312 struct wpa_driver_auth_params *params)
11313{
11314 struct i802_bss *bss = priv;
11315 return wpa_driver_nl80211_authenticate(bss, params);
11316}
11317
11318
11319static void driver_nl80211_deinit(void *priv)
11320{
11321 struct i802_bss *bss = priv;
11322 wpa_driver_nl80211_deinit(bss);
11323}
11324
11325
11326static int driver_nl80211_if_remove(void *priv, enum wpa_driver_if_type type,
11327 const char *ifname)
11328{
11329 struct i802_bss *bss = priv;
11330 return wpa_driver_nl80211_if_remove(bss, type, ifname);
11331}
11332
11333
11334static int driver_nl80211_send_mlme(void *priv, const u8 *data,
11335 size_t data_len, int noack)
11336{
11337 struct i802_bss *bss = priv;
11338 return wpa_driver_nl80211_send_mlme(bss, data, data_len, noack,
11339 0, 0, 0, 0);
11340}
11341
11342
11343static int driver_nl80211_sta_remove(void *priv, const u8 *addr)
11344{
11345 struct i802_bss *bss = priv;
11346 return wpa_driver_nl80211_sta_remove(bss, addr);
11347}
11348
11349
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011350static int driver_nl80211_set_sta_vlan(void *priv, const u8 *addr,
11351 const char *ifname, int vlan_id)
11352{
11353 struct i802_bss *bss = priv;
11354 return i802_set_sta_vlan(bss, addr, ifname, vlan_id);
11355}
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011356
11357
11358static int driver_nl80211_read_sta_data(void *priv,
11359 struct hostap_sta_driver_data *data,
11360 const u8 *addr)
11361{
11362 struct i802_bss *bss = priv;
11363 return i802_read_sta_data(bss, data, addr);
11364}
11365
11366
11367static int driver_nl80211_send_action(void *priv, unsigned int freq,
11368 unsigned int wait_time,
11369 const u8 *dst, const u8 *src,
11370 const u8 *bssid,
11371 const u8 *data, size_t data_len,
11372 int no_cck)
11373{
11374 struct i802_bss *bss = priv;
11375 return wpa_driver_nl80211_send_action(bss, freq, wait_time, dst, src,
11376 bssid, data, data_len, no_cck);
11377}
11378
11379
11380static int driver_nl80211_probe_req_report(void *priv, int report)
11381{
11382 struct i802_bss *bss = priv;
11383 return wpa_driver_nl80211_probe_req_report(bss, report);
11384}
11385
11386
Dmitry Shmidt700a1372013-03-15 14:14:44 -070011387static int wpa_driver_nl80211_update_ft_ies(void *priv, const u8 *md,
11388 const u8 *ies, size_t ies_len)
11389{
11390 int ret;
11391 struct nl_msg *msg;
11392 struct i802_bss *bss = priv;
11393 struct wpa_driver_nl80211_data *drv = bss->drv;
11394 u16 mdid = WPA_GET_LE16(md);
11395
11396 msg = nlmsg_alloc();
11397 if (!msg)
11398 return -ENOMEM;
11399
11400 wpa_printf(MSG_DEBUG, "nl80211: Updating FT IEs");
11401 nl80211_cmd(drv, msg, 0, NL80211_CMD_UPDATE_FT_IES);
11402 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
11403 NLA_PUT(msg, NL80211_ATTR_IE, ies_len, ies);
11404 NLA_PUT_U16(msg, NL80211_ATTR_MDID, mdid);
11405
11406 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
11407 if (ret) {
11408 wpa_printf(MSG_DEBUG, "nl80211: update_ft_ies failed "
11409 "err=%d (%s)", ret, strerror(-ret));
11410 }
11411
11412 return ret;
11413
11414nla_put_failure:
11415 nlmsg_free(msg);
11416 return -ENOBUFS;
11417}
11418
11419
Dmitry Shmidt34af3062013-07-11 10:46:32 -070011420const u8 * wpa_driver_nl80211_get_macaddr(void *priv)
11421{
11422 struct i802_bss *bss = priv;
11423 struct wpa_driver_nl80211_data *drv = bss->drv;
11424
11425 if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE)
11426 return NULL;
11427
11428 return bss->addr;
11429}
11430
11431
Dmitry Shmidt56052862013-10-04 10:23:25 -070011432static const char * scan_state_str(enum scan_states scan_state)
11433{
11434 switch (scan_state) {
11435 case NO_SCAN:
11436 return "NO_SCAN";
11437 case SCAN_REQUESTED:
11438 return "SCAN_REQUESTED";
11439 case SCAN_STARTED:
11440 return "SCAN_STARTED";
11441 case SCAN_COMPLETED:
11442 return "SCAN_COMPLETED";
11443 case SCAN_ABORTED:
11444 return "SCAN_ABORTED";
11445 case SCHED_SCAN_STARTED:
11446 return "SCHED_SCAN_STARTED";
11447 case SCHED_SCAN_STOPPED:
11448 return "SCHED_SCAN_STOPPED";
11449 case SCHED_SCAN_RESULTS:
11450 return "SCHED_SCAN_RESULTS";
11451 }
11452
11453 return "??";
11454}
11455
11456
11457static int wpa_driver_nl80211_status(void *priv, char *buf, size_t buflen)
11458{
11459 struct i802_bss *bss = priv;
11460 struct wpa_driver_nl80211_data *drv = bss->drv;
11461 int res;
11462 char *pos, *end;
11463
11464 pos = buf;
11465 end = buf + buflen;
11466
11467 res = os_snprintf(pos, end - pos,
11468 "ifindex=%d\n"
11469 "ifname=%s\n"
11470 "brname=%s\n"
11471 "addr=" MACSTR "\n"
11472 "freq=%d\n"
11473 "%s%s%s%s%s",
11474 bss->ifindex,
11475 bss->ifname,
11476 bss->brname,
11477 MAC2STR(bss->addr),
11478 bss->freq,
11479 bss->beacon_set ? "beacon_set=1\n" : "",
11480 bss->added_if_into_bridge ?
11481 "added_if_into_bridge=1\n" : "",
11482 bss->added_bridge ? "added_bridge=1\n" : "",
11483 bss->in_deinit ? "in_deinit=1\n" : "",
11484 bss->if_dynamic ? "if_dynamic=1\n" : "");
11485 if (res < 0 || res >= end - pos)
11486 return pos - buf;
11487 pos += res;
11488
11489 if (bss->wdev_id_set) {
11490 res = os_snprintf(pos, end - pos, "wdev_id=%llu\n",
11491 (unsigned long long) bss->wdev_id);
11492 if (res < 0 || res >= end - pos)
11493 return pos - buf;
11494 pos += res;
11495 }
11496
11497 res = os_snprintf(pos, end - pos,
11498 "phyname=%s\n"
11499 "drv_ifindex=%d\n"
11500 "operstate=%d\n"
11501 "scan_state=%s\n"
11502 "auth_bssid=" MACSTR "\n"
11503 "auth_attempt_bssid=" MACSTR "\n"
11504 "bssid=" MACSTR "\n"
11505 "prev_bssid=" MACSTR "\n"
11506 "associated=%d\n"
11507 "assoc_freq=%u\n"
11508 "monitor_sock=%d\n"
11509 "monitor_ifidx=%d\n"
11510 "monitor_refcount=%d\n"
11511 "last_mgmt_freq=%u\n"
11512 "eapol_tx_sock=%d\n"
11513 "%s%s%s%s%s%s%s%s%s%s%s%s%s",
11514 drv->phyname,
11515 drv->ifindex,
11516 drv->operstate,
11517 scan_state_str(drv->scan_state),
11518 MAC2STR(drv->auth_bssid),
11519 MAC2STR(drv->auth_attempt_bssid),
11520 MAC2STR(drv->bssid),
11521 MAC2STR(drv->prev_bssid),
11522 drv->associated,
11523 drv->assoc_freq,
11524 drv->monitor_sock,
11525 drv->monitor_ifidx,
11526 drv->monitor_refcount,
11527 drv->last_mgmt_freq,
11528 drv->eapol_tx_sock,
11529 drv->ignore_if_down_event ?
11530 "ignore_if_down_event=1\n" : "",
11531 drv->scan_complete_events ?
11532 "scan_complete_events=1\n" : "",
11533 drv->disabled_11b_rates ?
11534 "disabled_11b_rates=1\n" : "",
11535 drv->pending_remain_on_chan ?
11536 "pending_remain_on_chan=1\n" : "",
11537 drv->in_interface_list ? "in_interface_list=1\n" : "",
11538 drv->device_ap_sme ? "device_ap_sme=1\n" : "",
11539 drv->poll_command_supported ?
11540 "poll_command_supported=1\n" : "",
11541 drv->data_tx_status ? "data_tx_status=1\n" : "",
11542 drv->scan_for_auth ? "scan_for_auth=1\n" : "",
11543 drv->retry_auth ? "retry_auth=1\n" : "",
11544 drv->use_monitor ? "use_monitor=1\n" : "",
11545 drv->ignore_next_local_disconnect ?
11546 "ignore_next_local_disconnect=1\n" : "",
11547 drv->allow_p2p_device ? "allow_p2p_device=1\n" : "");
11548 if (res < 0 || res >= end - pos)
11549 return pos - buf;
11550 pos += res;
11551
11552 if (drv->has_capability) {
11553 res = os_snprintf(pos, end - pos,
11554 "capa.key_mgmt=0x%x\n"
11555 "capa.enc=0x%x\n"
11556 "capa.auth=0x%x\n"
11557 "capa.flags=0x%x\n"
11558 "capa.max_scan_ssids=%d\n"
11559 "capa.max_sched_scan_ssids=%d\n"
11560 "capa.sched_scan_supported=%d\n"
11561 "capa.max_match_sets=%d\n"
11562 "capa.max_remain_on_chan=%u\n"
11563 "capa.max_stations=%u\n"
11564 "capa.probe_resp_offloads=0x%x\n"
11565 "capa.max_acl_mac_addrs=%u\n"
11566 "capa.num_multichan_concurrent=%u\n",
11567 drv->capa.key_mgmt,
11568 drv->capa.enc,
11569 drv->capa.auth,
11570 drv->capa.flags,
11571 drv->capa.max_scan_ssids,
11572 drv->capa.max_sched_scan_ssids,
11573 drv->capa.sched_scan_supported,
11574 drv->capa.max_match_sets,
11575 drv->capa.max_remain_on_chan,
11576 drv->capa.max_stations,
11577 drv->capa.probe_resp_offloads,
11578 drv->capa.max_acl_mac_addrs,
11579 drv->capa.num_multichan_concurrent);
11580 if (res < 0 || res >= end - pos)
11581 return pos - buf;
11582 pos += res;
11583 }
11584
11585 return pos - buf;
11586}
11587
11588
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080011589static int set_beacon_data(struct nl_msg *msg, struct beacon_data *settings)
11590{
11591 if (settings->head)
11592 NLA_PUT(msg, NL80211_ATTR_BEACON_HEAD,
11593 settings->head_len, settings->head);
11594
11595 if (settings->tail)
11596 NLA_PUT(msg, NL80211_ATTR_BEACON_TAIL,
11597 settings->tail_len, settings->tail);
11598
11599 if (settings->beacon_ies)
11600 NLA_PUT(msg, NL80211_ATTR_IE,
11601 settings->beacon_ies_len, settings->beacon_ies);
11602
11603 if (settings->proberesp_ies)
11604 NLA_PUT(msg, NL80211_ATTR_IE_PROBE_RESP,
11605 settings->proberesp_ies_len, settings->proberesp_ies);
11606
11607 if (settings->assocresp_ies)
11608 NLA_PUT(msg,
11609 NL80211_ATTR_IE_ASSOC_RESP,
11610 settings->assocresp_ies_len, settings->assocresp_ies);
11611
11612 if (settings->probe_resp)
11613 NLA_PUT(msg, NL80211_ATTR_PROBE_RESP,
11614 settings->probe_resp_len, settings->probe_resp);
11615
11616 return 0;
11617
11618nla_put_failure:
11619 return -ENOBUFS;
11620}
11621
11622
11623static int nl80211_switch_channel(void *priv, struct csa_settings *settings)
11624{
11625 struct nl_msg *msg;
11626 struct i802_bss *bss = priv;
11627 struct wpa_driver_nl80211_data *drv = bss->drv;
11628 struct nlattr *beacon_csa;
11629 int ret = -ENOBUFS;
11630
Dmitry Shmidt04f534e2013-12-09 15:50:16 -080011631 wpa_printf(MSG_DEBUG, "nl80211: Channel switch request (cs_count=%u block_tx=%u freq=%d width=%d cf1=%d cf2=%d)",
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080011632 settings->cs_count, settings->block_tx,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -080011633 settings->freq_params.freq, settings->freq_params.bandwidth,
11634 settings->freq_params.center_freq1,
11635 settings->freq_params.center_freq2);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080011636
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080011637 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_AP_CSA)) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080011638 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support channel switch command");
11639 return -EOPNOTSUPP;
11640 }
11641
11642 if ((drv->nlmode != NL80211_IFTYPE_AP) &&
11643 (drv->nlmode != NL80211_IFTYPE_P2P_GO))
11644 return -EOPNOTSUPP;
11645
11646 /* check settings validity */
11647 if (!settings->beacon_csa.tail ||
11648 ((settings->beacon_csa.tail_len <=
11649 settings->counter_offset_beacon) ||
11650 (settings->beacon_csa.tail[settings->counter_offset_beacon] !=
11651 settings->cs_count)))
11652 return -EINVAL;
11653
11654 if (settings->beacon_csa.probe_resp &&
11655 ((settings->beacon_csa.probe_resp_len <=
11656 settings->counter_offset_presp) ||
11657 (settings->beacon_csa.probe_resp[settings->counter_offset_presp] !=
11658 settings->cs_count)))
11659 return -EINVAL;
11660
11661 msg = nlmsg_alloc();
11662 if (!msg)
11663 return -ENOMEM;
11664
11665 nl80211_cmd(drv, msg, 0, NL80211_CMD_CHANNEL_SWITCH);
11666 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
11667 NLA_PUT_U32(msg, NL80211_ATTR_CH_SWITCH_COUNT, settings->cs_count);
11668 ret = nl80211_put_freq_params(msg, &settings->freq_params);
11669 if (ret)
11670 goto error;
11671
11672 if (settings->block_tx)
11673 NLA_PUT_FLAG(msg, NL80211_ATTR_CH_SWITCH_BLOCK_TX);
11674
11675 /* beacon_after params */
11676 ret = set_beacon_data(msg, &settings->beacon_after);
11677 if (ret)
11678 goto error;
11679
11680 /* beacon_csa params */
11681 beacon_csa = nla_nest_start(msg, NL80211_ATTR_CSA_IES);
11682 if (!beacon_csa)
11683 goto nla_put_failure;
11684
11685 ret = set_beacon_data(msg, &settings->beacon_csa);
11686 if (ret)
11687 goto error;
11688
11689 NLA_PUT_U16(msg, NL80211_ATTR_CSA_C_OFF_BEACON,
11690 settings->counter_offset_beacon);
11691
11692 if (settings->beacon_csa.probe_resp)
11693 NLA_PUT_U16(msg, NL80211_ATTR_CSA_C_OFF_PRESP,
11694 settings->counter_offset_presp);
11695
11696 nla_nest_end(msg, beacon_csa);
11697 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
11698 if (ret) {
11699 wpa_printf(MSG_DEBUG, "nl80211: switch_channel failed err=%d (%s)",
11700 ret, strerror(-ret));
11701 }
11702 return ret;
11703
11704nla_put_failure:
11705 ret = -ENOBUFS;
11706error:
11707 nlmsg_free(msg);
11708 wpa_printf(MSG_DEBUG, "nl80211: Could not build channel switch request");
11709 return ret;
11710}
11711
11712
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080011713static int nl80211_set_qos_map(void *priv, const u8 *qos_map_set,
11714 u8 qos_map_set_len)
11715{
11716 struct i802_bss *bss = priv;
11717 struct wpa_driver_nl80211_data *drv = bss->drv;
11718 struct nl_msg *msg;
11719 int ret;
11720
11721 msg = nlmsg_alloc();
11722 if (!msg)
11723 return -ENOMEM;
11724
11725 wpa_hexdump(MSG_DEBUG, "nl80211: Setting QoS Map",
11726 qos_map_set, qos_map_set_len);
11727
11728 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_QOS_MAP);
11729 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
11730 NLA_PUT(msg, NL80211_ATTR_QOS_MAP, qos_map_set_len, qos_map_set);
11731
11732 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
11733 if (ret)
11734 wpa_printf(MSG_DEBUG, "nl80211: Setting QoS Map failed");
11735
11736 return ret;
11737
11738nla_put_failure:
11739 nlmsg_free(msg);
11740 return -ENOBUFS;
11741}
11742
11743
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011744const struct wpa_driver_ops wpa_driver_nl80211_ops = {
11745 .name = "nl80211",
11746 .desc = "Linux nl80211/cfg80211",
11747 .get_bssid = wpa_driver_nl80211_get_bssid,
11748 .get_ssid = wpa_driver_nl80211_get_ssid,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011749 .set_key = driver_nl80211_set_key,
11750 .scan2 = driver_nl80211_scan2,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011751 .sched_scan = wpa_driver_nl80211_sched_scan,
11752 .stop_sched_scan = wpa_driver_nl80211_stop_sched_scan,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011753 .get_scan_results2 = wpa_driver_nl80211_get_scan_results,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011754 .deauthenticate = driver_nl80211_deauthenticate,
11755 .authenticate = driver_nl80211_authenticate,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011756 .associate = wpa_driver_nl80211_associate,
11757 .global_init = nl80211_global_init,
11758 .global_deinit = nl80211_global_deinit,
11759 .init2 = wpa_driver_nl80211_init,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011760 .deinit = driver_nl80211_deinit,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011761 .get_capa = wpa_driver_nl80211_get_capa,
11762 .set_operstate = wpa_driver_nl80211_set_operstate,
11763 .set_supp_port = wpa_driver_nl80211_set_supp_port,
11764 .set_country = wpa_driver_nl80211_set_country,
Dmitry Shmidtcce06662013-11-04 18:44:24 -080011765 .get_country = wpa_driver_nl80211_get_country,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011766 .set_ap = wpa_driver_nl80211_set_ap,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -070011767 .set_acl = wpa_driver_nl80211_set_acl,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011768 .if_add = wpa_driver_nl80211_if_add,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011769 .if_remove = driver_nl80211_if_remove,
11770 .send_mlme = driver_nl80211_send_mlme,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011771 .get_hw_feature_data = wpa_driver_nl80211_get_hw_feature_data,
11772 .sta_add = wpa_driver_nl80211_sta_add,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011773 .sta_remove = driver_nl80211_sta_remove,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011774 .hapd_send_eapol = wpa_driver_nl80211_hapd_send_eapol,
11775 .sta_set_flags = wpa_driver_nl80211_sta_set_flags,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011776 .hapd_init = i802_init,
11777 .hapd_deinit = i802_deinit,
Jouni Malinen75ecf522011-06-27 15:19:46 -070011778 .set_wds_sta = i802_set_wds_sta,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011779 .get_seqnum = i802_get_seqnum,
11780 .flush = i802_flush,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011781 .get_inact_sec = i802_get_inact_sec,
11782 .sta_clear_stats = i802_sta_clear_stats,
11783 .set_rts = i802_set_rts,
11784 .set_frag = i802_set_frag,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011785 .set_tx_queue_params = i802_set_tx_queue_params,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011786 .set_sta_vlan = driver_nl80211_set_sta_vlan,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011787 .sta_deauth = i802_sta_deauth,
11788 .sta_disassoc = i802_sta_disassoc,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011789 .read_sta_data = driver_nl80211_read_sta_data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011790 .set_freq = i802_set_freq,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011791 .send_action = driver_nl80211_send_action,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011792 .send_action_cancel_wait = wpa_driver_nl80211_send_action_cancel_wait,
11793 .remain_on_channel = wpa_driver_nl80211_remain_on_channel,
11794 .cancel_remain_on_channel =
11795 wpa_driver_nl80211_cancel_remain_on_channel,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011796 .probe_req_report = driver_nl80211_probe_req_report,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011797 .deinit_ap = wpa_driver_nl80211_deinit_ap,
Dmitry Shmidt04949592012-07-19 12:16:46 -070011798 .deinit_p2p_cli = wpa_driver_nl80211_deinit_p2p_cli,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011799 .resume = wpa_driver_nl80211_resume,
11800 .send_ft_action = nl80211_send_ft_action,
11801 .signal_monitor = nl80211_signal_monitor,
11802 .signal_poll = nl80211_signal_poll,
11803 .send_frame = nl80211_send_frame,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011804 .shared_freq = wpa_driver_nl80211_shared_freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011805 .set_param = nl80211_set_param,
11806 .get_radio_name = nl80211_get_radio_name,
Jouni Malinen75ecf522011-06-27 15:19:46 -070011807 .add_pmkid = nl80211_add_pmkid,
11808 .remove_pmkid = nl80211_remove_pmkid,
11809 .flush_pmkid = nl80211_flush_pmkid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011810 .set_rekey_info = nl80211_set_rekey_info,
11811 .poll_client = nl80211_poll_client,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011812 .set_p2p_powersave = nl80211_set_p2p_powersave,
Dmitry Shmidtea69e842013-05-13 14:52:28 -070011813 .start_dfs_cac = nl80211_start_radar_detection,
11814 .stop_ap = wpa_driver_nl80211_stop_ap,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011815#ifdef CONFIG_TDLS
11816 .send_tdls_mgmt = nl80211_send_tdls_mgmt,
11817 .tdls_oper = nl80211_tdls_oper,
11818#endif /* CONFIG_TDLS */
Dmitry Shmidt700a1372013-03-15 14:14:44 -070011819 .update_ft_ies = wpa_driver_nl80211_update_ft_ies,
Dmitry Shmidt34af3062013-07-11 10:46:32 -070011820 .get_mac_addr = wpa_driver_nl80211_get_macaddr,
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070011821 .get_survey = wpa_driver_nl80211_get_survey,
Dmitry Shmidt56052862013-10-04 10:23:25 -070011822 .status = wpa_driver_nl80211_status,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080011823 .switch_channel = nl80211_switch_channel,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011824#ifdef ANDROID_P2P
Dmitry Shmidt6e933c12011-09-27 12:29:26 -070011825 .set_noa = wpa_driver_set_p2p_noa,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011826 .get_noa = wpa_driver_get_p2p_noa,
Dmitry Shmidt6e933c12011-09-27 12:29:26 -070011827 .set_ap_wps_ie = wpa_driver_set_ap_wps_p2p_ie,
Dmitry Shmidt292b0c32013-11-22 12:54:42 -080011828#endif /* ANDROID_P2P */
Dmitry Shmidt738a26e2011-07-07 14:22:14 -070011829#ifdef ANDROID
11830 .driver_cmd = wpa_driver_nl80211_driver_cmd,
Dmitry Shmidt292b0c32013-11-22 12:54:42 -080011831#endif /* ANDROID */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080011832 .set_qos_map = nl80211_set_qos_map,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011833};