blob: 42578b61ea339bcf26f0a6d5db542e05dfa39235 [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 Shmidt8d520ff2011-05-09 14:06:53 -0700304
305 u64 remain_on_chan_cookie;
306 u64 send_action_cookie;
307
308 unsigned int last_mgmt_freq;
309
310 struct wpa_driver_scan_filter *filter_ssids;
311 size_t num_filter_ssids;
312
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800313 struct i802_bss *first_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700314
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800315 int eapol_tx_sock;
316
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700317 int eapol_sock; /* socket for EAPOL frames */
318
319 int default_if_indices[16];
320 int *if_indices;
321 int num_if_indices;
322
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800323 /* From failed authentication command */
324 int auth_freq;
325 u8 auth_bssid_[ETH_ALEN];
326 u8 auth_ssid[32];
327 size_t auth_ssid_len;
328 int auth_alg;
329 u8 *auth_ie;
330 size_t auth_ie_len;
331 u8 auth_wep_key[4][16];
332 size_t auth_wep_key_len[4];
333 int auth_wep_tx_keyidx;
334 int auth_local_state_change;
335 int auth_p2p;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700336};
337
338
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800339static void wpa_driver_nl80211_deinit(struct i802_bss *bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700340static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx,
341 void *timeout_ctx);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800342static int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
343 enum nl80211_iftype nlmode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700344static int
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800345wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv,
346 const u8 *set_addr, int first);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700347static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
348 const u8 *addr, int cmd, u16 reason_code,
349 int local_state_change);
350static void nl80211_remove_monitor_interface(
351 struct wpa_driver_nl80211_data *drv);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800352static int nl80211_send_frame_cmd(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700353 unsigned int freq, unsigned int wait,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800354 const u8 *buf, size_t buf_len, u64 *cookie,
355 int no_cck, int no_ack, int offchanok);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700356static int nl80211_register_frame(struct i802_bss *bss,
357 struct nl_handle *hl_handle,
358 u16 type, const u8 *match, size_t match_len);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800359static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss,
360 int report);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800361#ifdef ANDROID
362static int android_pno_start(struct i802_bss *bss,
363 struct wpa_driver_scan_params *params);
364static int android_pno_stop(struct i802_bss *bss);
Dmitry Shmidt292b0c32013-11-22 12:54:42 -0800365extern int wpa_driver_nl80211_driver_cmd(void *priv, char *cmd, char *buf,
366 size_t buf_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800367#endif /* ANDROID */
368#ifdef ANDROID_P2P
Dmitry Shmidt6e933c12011-09-27 12:29:26 -0700369int wpa_driver_set_p2p_noa(void *priv, u8 count, int start, int duration);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800370int wpa_driver_get_p2p_noa(void *priv, u8 *buf, size_t len);
Dmitry Shmidt6e933c12011-09-27 12:29:26 -0700371int wpa_driver_set_p2p_ps(void *priv, int legacy_ps, int opp_ps, int ctwindow);
372int wpa_driver_set_ap_wps_p2p_ie(void *priv, const struct wpabuf *beacon,
Dmitry Shmidt292b0c32013-11-22 12:54:42 -0800373 const struct wpabuf *proberesp,
374 const struct wpabuf *assocresp);
375#endif /* ANDROID_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700376
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700377static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
378static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
379static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800380static int wpa_driver_nl80211_if_remove(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700381 enum wpa_driver_if_type type,
382 const char *ifname);
Dmitry Shmidt738a26e2011-07-07 14:22:14 -0700383
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800384static int wpa_driver_nl80211_set_freq(struct i802_bss *bss,
385 struct hostapd_freq_params *freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700386static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
387 int ifindex, int disabled);
388
389static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800390static int wpa_driver_nl80211_authenticate_retry(
391 struct wpa_driver_nl80211_data *drv);
392
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700393static int i802_set_iface_flags(struct i802_bss *bss, int up);
394
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800395
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700396static const char * nl80211_command_to_string(enum nl80211_commands cmd)
397{
398#define C2S(x) case x: return #x;
399 switch (cmd) {
400 C2S(NL80211_CMD_UNSPEC)
401 C2S(NL80211_CMD_GET_WIPHY)
402 C2S(NL80211_CMD_SET_WIPHY)
403 C2S(NL80211_CMD_NEW_WIPHY)
404 C2S(NL80211_CMD_DEL_WIPHY)
405 C2S(NL80211_CMD_GET_INTERFACE)
406 C2S(NL80211_CMD_SET_INTERFACE)
407 C2S(NL80211_CMD_NEW_INTERFACE)
408 C2S(NL80211_CMD_DEL_INTERFACE)
409 C2S(NL80211_CMD_GET_KEY)
410 C2S(NL80211_CMD_SET_KEY)
411 C2S(NL80211_CMD_NEW_KEY)
412 C2S(NL80211_CMD_DEL_KEY)
413 C2S(NL80211_CMD_GET_BEACON)
414 C2S(NL80211_CMD_SET_BEACON)
415 C2S(NL80211_CMD_START_AP)
416 C2S(NL80211_CMD_STOP_AP)
417 C2S(NL80211_CMD_GET_STATION)
418 C2S(NL80211_CMD_SET_STATION)
419 C2S(NL80211_CMD_NEW_STATION)
420 C2S(NL80211_CMD_DEL_STATION)
421 C2S(NL80211_CMD_GET_MPATH)
422 C2S(NL80211_CMD_SET_MPATH)
423 C2S(NL80211_CMD_NEW_MPATH)
424 C2S(NL80211_CMD_DEL_MPATH)
425 C2S(NL80211_CMD_SET_BSS)
426 C2S(NL80211_CMD_SET_REG)
427 C2S(NL80211_CMD_REQ_SET_REG)
428 C2S(NL80211_CMD_GET_MESH_CONFIG)
429 C2S(NL80211_CMD_SET_MESH_CONFIG)
430 C2S(NL80211_CMD_SET_MGMT_EXTRA_IE)
431 C2S(NL80211_CMD_GET_REG)
432 C2S(NL80211_CMD_GET_SCAN)
433 C2S(NL80211_CMD_TRIGGER_SCAN)
434 C2S(NL80211_CMD_NEW_SCAN_RESULTS)
435 C2S(NL80211_CMD_SCAN_ABORTED)
436 C2S(NL80211_CMD_REG_CHANGE)
437 C2S(NL80211_CMD_AUTHENTICATE)
438 C2S(NL80211_CMD_ASSOCIATE)
439 C2S(NL80211_CMD_DEAUTHENTICATE)
440 C2S(NL80211_CMD_DISASSOCIATE)
441 C2S(NL80211_CMD_MICHAEL_MIC_FAILURE)
442 C2S(NL80211_CMD_REG_BEACON_HINT)
443 C2S(NL80211_CMD_JOIN_IBSS)
444 C2S(NL80211_CMD_LEAVE_IBSS)
445 C2S(NL80211_CMD_TESTMODE)
446 C2S(NL80211_CMD_CONNECT)
447 C2S(NL80211_CMD_ROAM)
448 C2S(NL80211_CMD_DISCONNECT)
449 C2S(NL80211_CMD_SET_WIPHY_NETNS)
450 C2S(NL80211_CMD_GET_SURVEY)
451 C2S(NL80211_CMD_NEW_SURVEY_RESULTS)
452 C2S(NL80211_CMD_SET_PMKSA)
453 C2S(NL80211_CMD_DEL_PMKSA)
454 C2S(NL80211_CMD_FLUSH_PMKSA)
455 C2S(NL80211_CMD_REMAIN_ON_CHANNEL)
456 C2S(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL)
457 C2S(NL80211_CMD_SET_TX_BITRATE_MASK)
458 C2S(NL80211_CMD_REGISTER_FRAME)
459 C2S(NL80211_CMD_FRAME)
460 C2S(NL80211_CMD_FRAME_TX_STATUS)
461 C2S(NL80211_CMD_SET_POWER_SAVE)
462 C2S(NL80211_CMD_GET_POWER_SAVE)
463 C2S(NL80211_CMD_SET_CQM)
464 C2S(NL80211_CMD_NOTIFY_CQM)
465 C2S(NL80211_CMD_SET_CHANNEL)
466 C2S(NL80211_CMD_SET_WDS_PEER)
467 C2S(NL80211_CMD_FRAME_WAIT_CANCEL)
468 C2S(NL80211_CMD_JOIN_MESH)
469 C2S(NL80211_CMD_LEAVE_MESH)
470 C2S(NL80211_CMD_UNPROT_DEAUTHENTICATE)
471 C2S(NL80211_CMD_UNPROT_DISASSOCIATE)
472 C2S(NL80211_CMD_NEW_PEER_CANDIDATE)
473 C2S(NL80211_CMD_GET_WOWLAN)
474 C2S(NL80211_CMD_SET_WOWLAN)
475 C2S(NL80211_CMD_START_SCHED_SCAN)
476 C2S(NL80211_CMD_STOP_SCHED_SCAN)
477 C2S(NL80211_CMD_SCHED_SCAN_RESULTS)
478 C2S(NL80211_CMD_SCHED_SCAN_STOPPED)
479 C2S(NL80211_CMD_SET_REKEY_OFFLOAD)
480 C2S(NL80211_CMD_PMKSA_CANDIDATE)
481 C2S(NL80211_CMD_TDLS_OPER)
482 C2S(NL80211_CMD_TDLS_MGMT)
483 C2S(NL80211_CMD_UNEXPECTED_FRAME)
484 C2S(NL80211_CMD_PROBE_CLIENT)
485 C2S(NL80211_CMD_REGISTER_BEACONS)
486 C2S(NL80211_CMD_UNEXPECTED_4ADDR_FRAME)
487 C2S(NL80211_CMD_SET_NOACK_MAP)
488 C2S(NL80211_CMD_CH_SWITCH_NOTIFY)
489 C2S(NL80211_CMD_START_P2P_DEVICE)
490 C2S(NL80211_CMD_STOP_P2P_DEVICE)
491 C2S(NL80211_CMD_CONN_FAILED)
492 C2S(NL80211_CMD_SET_MCAST_RATE)
493 C2S(NL80211_CMD_SET_MAC_ACL)
494 C2S(NL80211_CMD_RADAR_DETECT)
495 C2S(NL80211_CMD_GET_PROTOCOL_FEATURES)
496 C2S(NL80211_CMD_UPDATE_FT_IES)
497 C2S(NL80211_CMD_FT_EVENT)
498 C2S(NL80211_CMD_CRIT_PROTOCOL_START)
499 C2S(NL80211_CMD_CRIT_PROTOCOL_STOP)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800500 C2S(NL80211_CMD_GET_COALESCE)
501 C2S(NL80211_CMD_SET_COALESCE)
502 C2S(NL80211_CMD_CHANNEL_SWITCH)
503 C2S(NL80211_CMD_VENDOR)
504 C2S(NL80211_CMD_SET_QOS_MAP)
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700505 default:
506 return "NL80211_CMD_UNKNOWN";
507 }
508#undef C2S
509}
510
511
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800512/* Converts nl80211_chan_width to a common format */
513static enum chan_width convert2width(int width)
514{
515 switch (width) {
516 case NL80211_CHAN_WIDTH_20_NOHT:
517 return CHAN_WIDTH_20_NOHT;
518 case NL80211_CHAN_WIDTH_20:
519 return CHAN_WIDTH_20;
520 case NL80211_CHAN_WIDTH_40:
521 return CHAN_WIDTH_40;
522 case NL80211_CHAN_WIDTH_80:
523 return CHAN_WIDTH_80;
524 case NL80211_CHAN_WIDTH_80P80:
525 return CHAN_WIDTH_80P80;
526 case NL80211_CHAN_WIDTH_160:
527 return CHAN_WIDTH_160;
528 }
529 return CHAN_WIDTH_UNKNOWN;
530}
531
532
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800533static int is_ap_interface(enum nl80211_iftype nlmode)
534{
535 return (nlmode == NL80211_IFTYPE_AP ||
536 nlmode == NL80211_IFTYPE_P2P_GO);
537}
538
539
540static int is_sta_interface(enum nl80211_iftype nlmode)
541{
542 return (nlmode == NL80211_IFTYPE_STATION ||
543 nlmode == NL80211_IFTYPE_P2P_CLIENT);
544}
545
546
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700547static int is_p2p_net_interface(enum nl80211_iftype nlmode)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800548{
549 return (nlmode == NL80211_IFTYPE_P2P_CLIENT ||
550 nlmode == NL80211_IFTYPE_P2P_GO);
551}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700552
553
Dmitry Shmidt8bae4132013-06-06 11:25:10 -0700554static void nl80211_mark_disconnected(struct wpa_driver_nl80211_data *drv)
555{
556 if (drv->associated)
557 os_memcpy(drv->prev_bssid, drv->bssid, ETH_ALEN);
558 drv->associated = 0;
559 os_memset(drv->bssid, 0, ETH_ALEN);
560}
561
562
Jouni Malinen87fd2792011-05-16 18:35:42 +0300563struct nl80211_bss_info_arg {
564 struct wpa_driver_nl80211_data *drv;
565 struct wpa_scan_results *res;
566 unsigned int assoc_freq;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800567 u8 assoc_bssid[ETH_ALEN];
Jouni Malinen87fd2792011-05-16 18:35:42 +0300568};
569
570static int bss_info_handler(struct nl_msg *msg, void *arg);
571
572
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700573/* nl80211 code */
574static int ack_handler(struct nl_msg *msg, void *arg)
575{
576 int *err = arg;
577 *err = 0;
578 return NL_STOP;
579}
580
581static int finish_handler(struct nl_msg *msg, void *arg)
582{
583 int *ret = arg;
584 *ret = 0;
585 return NL_SKIP;
586}
587
588static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
589 void *arg)
590{
591 int *ret = arg;
592 *ret = err->error;
593 return NL_SKIP;
594}
595
596
597static int no_seq_check(struct nl_msg *msg, void *arg)
598{
599 return NL_OK;
600}
601
602
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800603static int send_and_recv(struct nl80211_global *global,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700604 struct nl_handle *nl_handle, struct nl_msg *msg,
605 int (*valid_handler)(struct nl_msg *, void *),
606 void *valid_data)
607{
608 struct nl_cb *cb;
609 int err = -ENOMEM;
610
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800611 cb = nl_cb_clone(global->nl_cb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700612 if (!cb)
613 goto out;
614
615 err = nl_send_auto_complete(nl_handle, msg);
616 if (err < 0)
617 goto out;
618
619 err = 1;
620
621 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
622 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
623 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
624
625 if (valid_handler)
626 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM,
627 valid_handler, valid_data);
628
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700629 while (err > 0) {
630 int res = nl_recvmsgs(nl_handle, cb);
631 if (res) {
632 wpa_printf(MSG_INFO,
633 "nl80211: %s->nl_recvmsgs failed: %d",
634 __func__, res);
635 }
636 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700637 out:
638 nl_cb_put(cb);
639 nlmsg_free(msg);
640 return err;
641}
642
643
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800644static int send_and_recv_msgs_global(struct nl80211_global *global,
645 struct nl_msg *msg,
646 int (*valid_handler)(struct nl_msg *, void *),
647 void *valid_data)
648{
649 return send_and_recv(global, global->nl, msg, valid_handler,
650 valid_data);
651}
652
Dmitry Shmidt04949592012-07-19 12:16:46 -0700653
Dmitry Shmidt641185e2013-11-06 15:17:13 -0800654static int send_and_recv_msgs(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700655 struct nl_msg *msg,
656 int (*valid_handler)(struct nl_msg *, void *),
657 void *valid_data)
658{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800659 return send_and_recv(drv->global, drv->global->nl, msg,
660 valid_handler, valid_data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700661}
662
663
664struct family_data {
665 const char *group;
666 int id;
667};
668
669
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700670static int nl80211_set_iface_id(struct nl_msg *msg, struct i802_bss *bss)
671{
672 if (bss->wdev_id_set)
673 NLA_PUT_U64(msg, NL80211_ATTR_WDEV, bss->wdev_id);
674 else
675 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
676 return 0;
677
678nla_put_failure:
679 return -1;
680}
681
682
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700683static int family_handler(struct nl_msg *msg, void *arg)
684{
685 struct family_data *res = arg;
686 struct nlattr *tb[CTRL_ATTR_MAX + 1];
687 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
688 struct nlattr *mcgrp;
689 int i;
690
691 nla_parse(tb, CTRL_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
692 genlmsg_attrlen(gnlh, 0), NULL);
693 if (!tb[CTRL_ATTR_MCAST_GROUPS])
694 return NL_SKIP;
695
696 nla_for_each_nested(mcgrp, tb[CTRL_ATTR_MCAST_GROUPS], i) {
697 struct nlattr *tb2[CTRL_ATTR_MCAST_GRP_MAX + 1];
698 nla_parse(tb2, CTRL_ATTR_MCAST_GRP_MAX, nla_data(mcgrp),
699 nla_len(mcgrp), NULL);
700 if (!tb2[CTRL_ATTR_MCAST_GRP_NAME] ||
701 !tb2[CTRL_ATTR_MCAST_GRP_ID] ||
702 os_strncmp(nla_data(tb2[CTRL_ATTR_MCAST_GRP_NAME]),
703 res->group,
704 nla_len(tb2[CTRL_ATTR_MCAST_GRP_NAME])) != 0)
705 continue;
706 res->id = nla_get_u32(tb2[CTRL_ATTR_MCAST_GRP_ID]);
707 break;
708 };
709
710 return NL_SKIP;
711}
712
713
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800714static int nl_get_multicast_id(struct nl80211_global *global,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700715 const char *family, const char *group)
716{
717 struct nl_msg *msg;
718 int ret = -1;
719 struct family_data res = { group, -ENOENT };
720
721 msg = nlmsg_alloc();
722 if (!msg)
723 return -ENOMEM;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800724 genlmsg_put(msg, 0, 0, genl_ctrl_resolve(global->nl, "nlctrl"),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700725 0, 0, CTRL_CMD_GETFAMILY, 0);
726 NLA_PUT_STRING(msg, CTRL_ATTR_FAMILY_NAME, family);
727
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800728 ret = send_and_recv_msgs_global(global, msg, family_handler, &res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700729 msg = NULL;
730 if (ret == 0)
731 ret = res.id;
732
733nla_put_failure:
734 nlmsg_free(msg);
735 return ret;
736}
737
738
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800739static void * nl80211_cmd(struct wpa_driver_nl80211_data *drv,
740 struct nl_msg *msg, int flags, uint8_t cmd)
741{
742 return genlmsg_put(msg, 0, 0, drv->global->nl80211_id,
743 0, flags, cmd, 0);
744}
745
746
747struct wiphy_idx_data {
748 int wiphy_idx;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700749 enum nl80211_iftype nlmode;
750 u8 *macaddr;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800751};
752
753
754static int netdev_info_handler(struct nl_msg *msg, void *arg)
755{
756 struct nlattr *tb[NL80211_ATTR_MAX + 1];
757 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
758 struct wiphy_idx_data *info = arg;
759
760 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
761 genlmsg_attrlen(gnlh, 0), NULL);
762
763 if (tb[NL80211_ATTR_WIPHY])
764 info->wiphy_idx = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
765
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700766 if (tb[NL80211_ATTR_IFTYPE])
767 info->nlmode = nla_get_u32(tb[NL80211_ATTR_IFTYPE]);
768
769 if (tb[NL80211_ATTR_MAC] && info->macaddr)
770 os_memcpy(info->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
771 ETH_ALEN);
772
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800773 return NL_SKIP;
774}
775
776
777static int nl80211_get_wiphy_index(struct i802_bss *bss)
778{
779 struct nl_msg *msg;
780 struct wiphy_idx_data data = {
781 .wiphy_idx = -1,
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700782 .macaddr = NULL,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800783 };
784
785 msg = nlmsg_alloc();
786 if (!msg)
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700787 return NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800788
789 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_GET_INTERFACE);
790
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700791 if (nl80211_set_iface_id(msg, bss) < 0)
792 goto nla_put_failure;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800793
794 if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0)
795 return data.wiphy_idx;
796 msg = NULL;
797nla_put_failure:
798 nlmsg_free(msg);
799 return -1;
800}
801
802
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700803static enum nl80211_iftype nl80211_get_ifmode(struct i802_bss *bss)
804{
805 struct nl_msg *msg;
806 struct wiphy_idx_data data = {
807 .nlmode = NL80211_IFTYPE_UNSPECIFIED,
808 .macaddr = NULL,
809 };
810
811 msg = nlmsg_alloc();
812 if (!msg)
813 return -1;
814
815 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_GET_INTERFACE);
816
817 if (nl80211_set_iface_id(msg, bss) < 0)
818 goto nla_put_failure;
819
820 if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0)
821 return data.nlmode;
822 msg = NULL;
823nla_put_failure:
824 nlmsg_free(msg);
825 return NL80211_IFTYPE_UNSPECIFIED;
826}
827
828
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700829static int nl80211_get_macaddr(struct i802_bss *bss)
830{
831 struct nl_msg *msg;
832 struct wiphy_idx_data data = {
833 .macaddr = bss->addr,
834 };
835
836 msg = nlmsg_alloc();
837 if (!msg)
838 return NL80211_IFTYPE_UNSPECIFIED;
839
840 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_GET_INTERFACE);
841 if (nl80211_set_iface_id(msg, bss) < 0)
842 goto nla_put_failure;
843
844 return send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data);
845
846nla_put_failure:
847 nlmsg_free(msg);
848 return NL80211_IFTYPE_UNSPECIFIED;
849}
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700850
851
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800852static int nl80211_register_beacons(struct wpa_driver_nl80211_data *drv,
853 struct nl80211_wiphy_data *w)
854{
855 struct nl_msg *msg;
856 int ret = -1;
857
858 msg = nlmsg_alloc();
859 if (!msg)
860 return -1;
861
862 nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_BEACONS);
863
864 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, w->wiphy_idx);
865
866 ret = send_and_recv(drv->global, w->nl_beacons, msg, NULL, NULL);
867 msg = NULL;
868 if (ret) {
869 wpa_printf(MSG_DEBUG, "nl80211: Register beacons command "
870 "failed: ret=%d (%s)",
871 ret, strerror(-ret));
872 goto nla_put_failure;
873 }
874 ret = 0;
875nla_put_failure:
876 nlmsg_free(msg);
877 return ret;
878}
879
880
881static void nl80211_recv_beacons(int sock, void *eloop_ctx, void *handle)
882{
883 struct nl80211_wiphy_data *w = eloop_ctx;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700884 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800885
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800886 wpa_printf(MSG_EXCESSIVE, "nl80211: Beacon event message available");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800887
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700888 res = nl_recvmsgs(handle, w->nl_cb);
889 if (res) {
890 wpa_printf(MSG_INFO, "nl80211: %s->nl_recvmsgs failed: %d",
891 __func__, res);
892 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800893}
894
895
896static int process_beacon_event(struct nl_msg *msg, void *arg)
897{
898 struct nl80211_wiphy_data *w = arg;
899 struct wpa_driver_nl80211_data *drv;
900 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
901 struct nlattr *tb[NL80211_ATTR_MAX + 1];
902 union wpa_event_data event;
903
904 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
905 genlmsg_attrlen(gnlh, 0), NULL);
906
907 if (gnlh->cmd != NL80211_CMD_FRAME) {
908 wpa_printf(MSG_DEBUG, "nl80211: Unexpected beacon event? (%d)",
909 gnlh->cmd);
910 return NL_SKIP;
911 }
912
913 if (!tb[NL80211_ATTR_FRAME])
914 return NL_SKIP;
915
916 dl_list_for_each(drv, &w->drvs, struct wpa_driver_nl80211_data,
917 wiphy_list) {
918 os_memset(&event, 0, sizeof(event));
919 event.rx_mgmt.frame = nla_data(tb[NL80211_ATTR_FRAME]);
920 event.rx_mgmt.frame_len = nla_len(tb[NL80211_ATTR_FRAME]);
921 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
922 }
923
924 return NL_SKIP;
925}
926
927
928static struct nl80211_wiphy_data *
929nl80211_get_wiphy_data_ap(struct i802_bss *bss)
930{
931 static DEFINE_DL_LIST(nl80211_wiphys);
932 struct nl80211_wiphy_data *w;
933 int wiphy_idx, found = 0;
934 struct i802_bss *tmp_bss;
935
936 if (bss->wiphy_data != NULL)
937 return bss->wiphy_data;
938
939 wiphy_idx = nl80211_get_wiphy_index(bss);
940
941 dl_list_for_each(w, &nl80211_wiphys, struct nl80211_wiphy_data, list) {
942 if (w->wiphy_idx == wiphy_idx)
943 goto add;
944 }
945
946 /* alloc new one */
947 w = os_zalloc(sizeof(*w));
948 if (w == NULL)
949 return NULL;
950 w->wiphy_idx = wiphy_idx;
951 dl_list_init(&w->bsss);
952 dl_list_init(&w->drvs);
953
954 w->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
955 if (!w->nl_cb) {
956 os_free(w);
957 return NULL;
958 }
959 nl_cb_set(w->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL);
960 nl_cb_set(w->nl_cb, NL_CB_VALID, NL_CB_CUSTOM, process_beacon_event,
961 w);
962
963 w->nl_beacons = nl_create_handle(bss->drv->global->nl_cb,
964 "wiphy beacons");
965 if (w->nl_beacons == NULL) {
966 os_free(w);
967 return NULL;
968 }
969
970 if (nl80211_register_beacons(bss->drv, w)) {
971 nl_destroy_handles(&w->nl_beacons);
972 os_free(w);
973 return NULL;
974 }
975
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700976 nl80211_register_eloop_read(&w->nl_beacons, nl80211_recv_beacons, w);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800977
978 dl_list_add(&nl80211_wiphys, &w->list);
979
980add:
981 /* drv entry for this bss already there? */
982 dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
983 if (tmp_bss->drv == bss->drv) {
984 found = 1;
985 break;
986 }
987 }
988 /* if not add it */
989 if (!found)
990 dl_list_add(&w->drvs, &bss->drv->wiphy_list);
991
992 dl_list_add(&w->bsss, &bss->wiphy_list);
993 bss->wiphy_data = w;
994 return w;
995}
996
997
998static void nl80211_put_wiphy_data_ap(struct i802_bss *bss)
999{
1000 struct nl80211_wiphy_data *w = bss->wiphy_data;
1001 struct i802_bss *tmp_bss;
1002 int found = 0;
1003
1004 if (w == NULL)
1005 return;
1006 bss->wiphy_data = NULL;
1007 dl_list_del(&bss->wiphy_list);
1008
1009 /* still any for this drv present? */
1010 dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
1011 if (tmp_bss->drv == bss->drv) {
1012 found = 1;
1013 break;
1014 }
1015 }
1016 /* if not remove it */
1017 if (!found)
1018 dl_list_del(&bss->drv->wiphy_list);
1019
1020 if (!dl_list_empty(&w->bsss))
1021 return;
1022
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001023 nl80211_destroy_eloop_handle(&w->nl_beacons);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001024
1025 nl_cb_put(w->nl_cb);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001026 dl_list_del(&w->list);
1027 os_free(w);
1028}
1029
1030
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001031static int wpa_driver_nl80211_get_bssid(void *priv, u8 *bssid)
1032{
1033 struct i802_bss *bss = priv;
1034 struct wpa_driver_nl80211_data *drv = bss->drv;
1035 if (!drv->associated)
1036 return -1;
1037 os_memcpy(bssid, drv->bssid, ETH_ALEN);
1038 return 0;
1039}
1040
1041
1042static int wpa_driver_nl80211_get_ssid(void *priv, u8 *ssid)
1043{
1044 struct i802_bss *bss = priv;
1045 struct wpa_driver_nl80211_data *drv = bss->drv;
1046 if (!drv->associated)
1047 return -1;
1048 os_memcpy(ssid, drv->ssid, drv->ssid_len);
1049 return drv->ssid_len;
1050}
1051
1052
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001053static void wpa_driver_nl80211_event_newlink(
1054 struct wpa_driver_nl80211_data *drv, char *ifname)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001055{
1056 union wpa_event_data event;
1057
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001058 if (os_strcmp(drv->first_bss->ifname, ifname) == 0) {
1059 if (if_nametoindex(drv->first_bss->ifname) == 0) {
1060 wpa_printf(MSG_DEBUG, "nl80211: Interface %s does not exist - ignore RTM_NEWLINK",
1061 drv->first_bss->ifname);
1062 return;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001063 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001064 if (!drv->if_removed)
1065 return;
1066 wpa_printf(MSG_DEBUG, "nl80211: Mark if_removed=0 for %s based on RTM_NEWLINK event",
1067 drv->first_bss->ifname);
1068 drv->if_removed = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001069 }
1070
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001071 os_memset(&event, 0, sizeof(event));
1072 os_strlcpy(event.interface_status.ifname, ifname,
1073 sizeof(event.interface_status.ifname));
1074 event.interface_status.ievent = EVENT_INTERFACE_ADDED;
1075 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
1076}
1077
1078
1079static void wpa_driver_nl80211_event_dellink(
1080 struct wpa_driver_nl80211_data *drv, char *ifname)
1081{
1082 union wpa_event_data event;
1083
1084 if (os_strcmp(drv->first_bss->ifname, ifname) == 0) {
1085 if (drv->if_removed) {
1086 wpa_printf(MSG_DEBUG, "nl80211: if_removed already set - ignore RTM_DELLINK event for %s",
1087 ifname);
1088 return;
1089 }
1090 wpa_printf(MSG_DEBUG, "RTM_DELLINK: Interface '%s' removed - mark if_removed=1",
1091 ifname);
1092 drv->if_removed = 1;
1093 } else {
1094 wpa_printf(MSG_DEBUG, "RTM_DELLINK: Interface '%s' removed",
1095 ifname);
1096 }
1097
1098 os_memset(&event, 0, sizeof(event));
1099 os_strlcpy(event.interface_status.ifname, ifname,
1100 sizeof(event.interface_status.ifname));
1101 event.interface_status.ievent = EVENT_INTERFACE_REMOVED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001102 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
1103}
1104
1105
1106static int wpa_driver_nl80211_own_ifname(struct wpa_driver_nl80211_data *drv,
1107 u8 *buf, size_t len)
1108{
1109 int attrlen, rta_len;
1110 struct rtattr *attr;
1111
1112 attrlen = len;
1113 attr = (struct rtattr *) buf;
1114
1115 rta_len = RTA_ALIGN(sizeof(struct rtattr));
1116 while (RTA_OK(attr, attrlen)) {
1117 if (attr->rta_type == IFLA_IFNAME) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001118 if (os_strcmp(((char *) attr) + rta_len,
1119 drv->first_bss->ifname) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001120 return 1;
1121 else
1122 break;
1123 }
1124 attr = RTA_NEXT(attr, attrlen);
1125 }
1126
1127 return 0;
1128}
1129
1130
1131static int wpa_driver_nl80211_own_ifindex(struct wpa_driver_nl80211_data *drv,
1132 int ifindex, u8 *buf, size_t len)
1133{
1134 if (drv->ifindex == ifindex)
1135 return 1;
1136
1137 if (drv->if_removed && wpa_driver_nl80211_own_ifname(drv, buf, len)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001138 wpa_printf(MSG_DEBUG, "nl80211: Update ifindex for a removed "
1139 "interface");
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001140 wpa_driver_nl80211_finish_drv_init(drv, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001141 return 1;
1142 }
1143
1144 return 0;
1145}
1146
1147
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001148static struct wpa_driver_nl80211_data *
1149nl80211_find_drv(struct nl80211_global *global, int idx, u8 *buf, size_t len)
1150{
1151 struct wpa_driver_nl80211_data *drv;
1152 dl_list_for_each(drv, &global->interfaces,
1153 struct wpa_driver_nl80211_data, list) {
1154 if (wpa_driver_nl80211_own_ifindex(drv, idx, buf, len) ||
1155 have_ifidx(drv, idx))
1156 return drv;
1157 }
1158 return NULL;
1159}
1160
1161
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001162static void wpa_driver_nl80211_event_rtm_newlink(void *ctx,
1163 struct ifinfomsg *ifi,
1164 u8 *buf, size_t len)
1165{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001166 struct nl80211_global *global = ctx;
1167 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001168 int attrlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001169 struct rtattr *attr;
1170 u32 brid = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001171 char namebuf[IFNAMSIZ];
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001172 char ifname[IFNAMSIZ + 1];
1173 char extra[100], *pos, *end;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001174
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001175 drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
1176 if (!drv) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001177 wpa_printf(MSG_DEBUG, "nl80211: Ignore RTM_NEWLINK event for foreign ifindex %d",
1178 ifi->ifi_index);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001179 return;
1180 }
1181
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001182 extra[0] = '\0';
1183 pos = extra;
1184 end = pos + sizeof(extra);
1185 ifname[0] = '\0';
1186
1187 attrlen = len;
1188 attr = (struct rtattr *) buf;
1189 while (RTA_OK(attr, attrlen)) {
1190 switch (attr->rta_type) {
1191 case IFLA_IFNAME:
1192 if (RTA_PAYLOAD(attr) >= IFNAMSIZ)
1193 break;
1194 os_memcpy(ifname, RTA_DATA(attr), RTA_PAYLOAD(attr));
1195 ifname[RTA_PAYLOAD(attr)] = '\0';
1196 break;
1197 case IFLA_MASTER:
1198 brid = nla_get_u32((struct nlattr *) attr);
1199 pos += os_snprintf(pos, end - pos, " master=%u", brid);
1200 break;
1201 case IFLA_WIRELESS:
1202 pos += os_snprintf(pos, end - pos, " wext");
1203 break;
1204 case IFLA_OPERSTATE:
1205 pos += os_snprintf(pos, end - pos, " operstate=%u",
1206 nla_get_u32((struct nlattr *) attr));
1207 break;
1208 case IFLA_LINKMODE:
1209 pos += os_snprintf(pos, end - pos, " linkmode=%u",
1210 nla_get_u32((struct nlattr *) attr));
1211 break;
1212 }
1213 attr = RTA_NEXT(attr, attrlen);
1214 }
1215 extra[sizeof(extra) - 1] = '\0';
1216
1217 wpa_printf(MSG_DEBUG, "RTM_NEWLINK: ifi_index=%d ifname=%s%s ifi_flags=0x%x (%s%s%s%s)",
1218 ifi->ifi_index, ifname, extra, ifi->ifi_flags,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001219 (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
1220 (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
1221 (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
1222 (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
1223
1224 if (!drv->if_disabled && !(ifi->ifi_flags & IFF_UP)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001225 if (if_indextoname(ifi->ifi_index, namebuf) &&
1226 linux_iface_up(drv->global->ioctl_sock,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001227 drv->first_bss->ifname) > 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001228 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
1229 "event since interface %s is up", namebuf);
1230 return;
1231 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001232 wpa_printf(MSG_DEBUG, "nl80211: Interface down");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001233 if (drv->ignore_if_down_event) {
1234 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
1235 "event generated by mode change");
1236 drv->ignore_if_down_event = 0;
1237 } else {
1238 drv->if_disabled = 1;
1239 wpa_supplicant_event(drv->ctx,
1240 EVENT_INTERFACE_DISABLED, NULL);
1241 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001242 }
1243
1244 if (drv->if_disabled && (ifi->ifi_flags & IFF_UP)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001245 if (if_indextoname(ifi->ifi_index, namebuf) &&
1246 linux_iface_up(drv->global->ioctl_sock,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001247 drv->first_bss->ifname) == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001248 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
1249 "event since interface %s is down",
1250 namebuf);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001251 } else if (if_nametoindex(drv->first_bss->ifname) == 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001252 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
1253 "event since interface %s does not exist",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001254 drv->first_bss->ifname);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001255 } else if (drv->if_removed) {
1256 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
1257 "event since interface %s is marked "
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001258 "removed", drv->first_bss->ifname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001259 } else {
1260 wpa_printf(MSG_DEBUG, "nl80211: Interface up");
1261 drv->if_disabled = 0;
1262 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_ENABLED,
1263 NULL);
1264 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001265 }
1266
1267 /*
1268 * Some drivers send the association event before the operup event--in
1269 * this case, lifting operstate in wpa_driver_nl80211_set_operstate()
1270 * fails. This will hit us when wpa_supplicant does not need to do
1271 * IEEE 802.1X authentication
1272 */
1273 if (drv->operstate == 1 &&
1274 (ifi->ifi_flags & (IFF_LOWER_UP | IFF_DORMANT)) == IFF_LOWER_UP &&
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001275 !(ifi->ifi_flags & IFF_RUNNING)) {
1276 wpa_printf(MSG_DEBUG, "nl80211: Set IF_OPER_UP again based on ifi_flags and expected operstate");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001277 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001278 -1, IF_OPER_UP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001279 }
1280
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001281 if (ifname[0])
1282 wpa_driver_nl80211_event_newlink(drv, ifname);
1283
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001284 if (ifi->ifi_family == AF_BRIDGE && brid) {
1285 /* device has been added to bridge */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001286 if_indextoname(brid, namebuf);
1287 wpa_printf(MSG_DEBUG, "nl80211: Add ifindex %u for bridge %s",
1288 brid, namebuf);
1289 add_ifidx(drv, brid);
1290 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001291}
1292
1293
1294static void wpa_driver_nl80211_event_rtm_dellink(void *ctx,
1295 struct ifinfomsg *ifi,
1296 u8 *buf, size_t len)
1297{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001298 struct nl80211_global *global = ctx;
1299 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001300 int attrlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001301 struct rtattr *attr;
1302 u32 brid = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001303 char ifname[IFNAMSIZ + 1];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001304
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001305 drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
1306 if (!drv) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001307 wpa_printf(MSG_DEBUG, "nl80211: Ignore RTM_DELLINK event for foreign ifindex %d",
1308 ifi->ifi_index);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001309 return;
1310 }
1311
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001312 ifname[0] = '\0';
1313
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001314 attrlen = len;
1315 attr = (struct rtattr *) buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001316 while (RTA_OK(attr, attrlen)) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001317 switch (attr->rta_type) {
1318 case IFLA_IFNAME:
1319 if (RTA_PAYLOAD(attr) >= IFNAMSIZ)
1320 break;
1321 os_memcpy(ifname, RTA_DATA(attr), RTA_PAYLOAD(attr));
1322 ifname[RTA_PAYLOAD(attr)] = '\0';
1323 break;
1324 case IFLA_MASTER:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001325 brid = nla_get_u32((struct nlattr *) attr);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001326 break;
1327 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001328 attr = RTA_NEXT(attr, attrlen);
1329 }
1330
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001331 if (ifname[0])
1332 wpa_driver_nl80211_event_dellink(drv, ifname);
1333
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001334 if (ifi->ifi_family == AF_BRIDGE && brid) {
1335 /* device has been removed from bridge */
1336 char namebuf[IFNAMSIZ];
1337 if_indextoname(brid, namebuf);
1338 wpa_printf(MSG_DEBUG, "nl80211: Remove ifindex %u for bridge "
1339 "%s", brid, namebuf);
1340 del_ifidx(drv, brid);
1341 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001342}
1343
1344
1345static void mlme_event_auth(struct wpa_driver_nl80211_data *drv,
1346 const u8 *frame, size_t len)
1347{
1348 const struct ieee80211_mgmt *mgmt;
1349 union wpa_event_data event;
1350
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001351 wpa_printf(MSG_DEBUG, "nl80211: Authenticate event");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001352 mgmt = (const struct ieee80211_mgmt *) frame;
1353 if (len < 24 + sizeof(mgmt->u.auth)) {
1354 wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
1355 "frame");
1356 return;
1357 }
1358
1359 os_memcpy(drv->auth_bssid, mgmt->sa, ETH_ALEN);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001360 os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001361 os_memset(&event, 0, sizeof(event));
1362 os_memcpy(event.auth.peer, mgmt->sa, ETH_ALEN);
1363 event.auth.auth_type = le_to_host16(mgmt->u.auth.auth_alg);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001364 event.auth.auth_transaction =
1365 le_to_host16(mgmt->u.auth.auth_transaction);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001366 event.auth.status_code = le_to_host16(mgmt->u.auth.status_code);
1367 if (len > 24 + sizeof(mgmt->u.auth)) {
1368 event.auth.ies = mgmt->u.auth.variable;
1369 event.auth.ies_len = len - 24 - sizeof(mgmt->u.auth);
1370 }
1371
1372 wpa_supplicant_event(drv->ctx, EVENT_AUTH, &event);
1373}
1374
1375
Jouni Malinen87fd2792011-05-16 18:35:42 +03001376static unsigned int nl80211_get_assoc_freq(struct wpa_driver_nl80211_data *drv)
1377{
1378 struct nl_msg *msg;
1379 int ret;
1380 struct nl80211_bss_info_arg arg;
1381
1382 os_memset(&arg, 0, sizeof(arg));
1383 msg = nlmsg_alloc();
1384 if (!msg)
1385 goto nla_put_failure;
1386
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001387 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
Jouni Malinen87fd2792011-05-16 18:35:42 +03001388 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
1389
1390 arg.drv = drv;
1391 ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
1392 msg = NULL;
1393 if (ret == 0) {
1394 wpa_printf(MSG_DEBUG, "nl80211: Operating frequency for the "
1395 "associated BSS from scan results: %u MHz",
1396 arg.assoc_freq);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001397 if (arg.assoc_freq)
1398 drv->assoc_freq = arg.assoc_freq;
1399 return drv->assoc_freq;
Jouni Malinen87fd2792011-05-16 18:35:42 +03001400 }
1401 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
1402 "(%s)", ret, strerror(-ret));
1403nla_put_failure:
1404 nlmsg_free(msg);
1405 return drv->assoc_freq;
1406}
1407
1408
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001409static void mlme_event_assoc(struct wpa_driver_nl80211_data *drv,
1410 const u8 *frame, size_t len)
1411{
1412 const struct ieee80211_mgmt *mgmt;
1413 union wpa_event_data event;
1414 u16 status;
1415
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001416 wpa_printf(MSG_DEBUG, "nl80211: Associate event");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001417 mgmt = (const struct ieee80211_mgmt *) frame;
1418 if (len < 24 + sizeof(mgmt->u.assoc_resp)) {
1419 wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
1420 "frame");
1421 return;
1422 }
1423
1424 status = le_to_host16(mgmt->u.assoc_resp.status_code);
1425 if (status != WLAN_STATUS_SUCCESS) {
1426 os_memset(&event, 0, sizeof(event));
1427 event.assoc_reject.bssid = mgmt->bssid;
1428 if (len > 24 + sizeof(mgmt->u.assoc_resp)) {
1429 event.assoc_reject.resp_ies =
1430 (u8 *) mgmt->u.assoc_resp.variable;
1431 event.assoc_reject.resp_ies_len =
1432 len - 24 - sizeof(mgmt->u.assoc_resp);
1433 }
1434 event.assoc_reject.status_code = status;
1435
1436 wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
1437 return;
1438 }
1439
1440 drv->associated = 1;
1441 os_memcpy(drv->bssid, mgmt->sa, ETH_ALEN);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001442 os_memcpy(drv->prev_bssid, mgmt->sa, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001443
1444 os_memset(&event, 0, sizeof(event));
1445 if (len > 24 + sizeof(mgmt->u.assoc_resp)) {
1446 event.assoc_info.resp_ies = (u8 *) mgmt->u.assoc_resp.variable;
1447 event.assoc_info.resp_ies_len =
1448 len - 24 - sizeof(mgmt->u.assoc_resp);
1449 }
1450
1451 event.assoc_info.freq = drv->assoc_freq;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001452
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001453 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
1454}
1455
1456
1457static void mlme_event_connect(struct wpa_driver_nl80211_data *drv,
1458 enum nl80211_commands cmd, struct nlattr *status,
1459 struct nlattr *addr, struct nlattr *req_ie,
1460 struct nlattr *resp_ie)
1461{
1462 union wpa_event_data event;
1463
1464 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
1465 /*
1466 * Avoid reporting two association events that would confuse
1467 * the core code.
1468 */
1469 wpa_printf(MSG_DEBUG, "nl80211: Ignore connect event (cmd=%d) "
1470 "when using userspace SME", cmd);
1471 return;
1472 }
1473
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001474 if (cmd == NL80211_CMD_CONNECT)
1475 wpa_printf(MSG_DEBUG, "nl80211: Connect event");
1476 else if (cmd == NL80211_CMD_ROAM)
1477 wpa_printf(MSG_DEBUG, "nl80211: Roam event");
1478
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001479 os_memset(&event, 0, sizeof(event));
1480 if (cmd == NL80211_CMD_CONNECT &&
1481 nla_get_u16(status) != WLAN_STATUS_SUCCESS) {
1482 if (addr)
1483 event.assoc_reject.bssid = nla_data(addr);
1484 if (resp_ie) {
1485 event.assoc_reject.resp_ies = nla_data(resp_ie);
1486 event.assoc_reject.resp_ies_len = nla_len(resp_ie);
1487 }
1488 event.assoc_reject.status_code = nla_get_u16(status);
1489 wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
1490 return;
1491 }
1492
1493 drv->associated = 1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001494 if (addr) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001495 os_memcpy(drv->bssid, nla_data(addr), ETH_ALEN);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001496 os_memcpy(drv->prev_bssid, drv->bssid, ETH_ALEN);
1497 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001498
1499 if (req_ie) {
1500 event.assoc_info.req_ies = nla_data(req_ie);
1501 event.assoc_info.req_ies_len = nla_len(req_ie);
1502 }
1503 if (resp_ie) {
1504 event.assoc_info.resp_ies = nla_data(resp_ie);
1505 event.assoc_info.resp_ies_len = nla_len(resp_ie);
1506 }
1507
Jouni Malinen87fd2792011-05-16 18:35:42 +03001508 event.assoc_info.freq = nl80211_get_assoc_freq(drv);
1509
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001510 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
1511}
1512
1513
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001514static void mlme_event_disconnect(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001515 struct nlattr *reason, struct nlattr *addr,
1516 struct nlattr *by_ap)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001517{
1518 union wpa_event_data data;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001519 unsigned int locally_generated = by_ap == NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001520
1521 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
1522 /*
1523 * Avoid reporting two disassociation events that could
1524 * confuse the core code.
1525 */
1526 wpa_printf(MSG_DEBUG, "nl80211: Ignore disconnect "
1527 "event when using userspace SME");
1528 return;
1529 }
1530
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001531 if (drv->ignore_next_local_disconnect) {
1532 drv->ignore_next_local_disconnect = 0;
1533 if (locally_generated) {
1534 wpa_printf(MSG_DEBUG, "nl80211: Ignore disconnect "
1535 "event triggered during reassociation");
1536 return;
1537 }
1538 wpa_printf(MSG_WARNING, "nl80211: Was expecting local "
1539 "disconnect but got another disconnect "
1540 "event first");
1541 }
1542
1543 wpa_printf(MSG_DEBUG, "nl80211: Disconnect event");
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001544 nl80211_mark_disconnected(drv);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001545 os_memset(&data, 0, sizeof(data));
1546 if (reason)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001547 data.deauth_info.reason_code = nla_get_u16(reason);
1548 data.deauth_info.locally_generated = by_ap == NULL;
1549 wpa_supplicant_event(drv->ctx, EVENT_DEAUTH, &data);
1550}
1551
1552
Dmitry Shmidt97672262014-02-03 13:02:54 -08001553static int calculate_chan_offset(int width, int freq, int cf1, int cf2)
1554{
1555 int freq1 = 0;
1556
1557 switch (convert2width(width)) {
1558 case CHAN_WIDTH_20_NOHT:
1559 case CHAN_WIDTH_20:
1560 return 0;
1561 case CHAN_WIDTH_40:
1562 freq1 = cf1 - 10;
1563 break;
1564 case CHAN_WIDTH_80:
1565 freq1 = cf1 - 30;
1566 break;
1567 case CHAN_WIDTH_160:
1568 freq1 = cf1 - 70;
1569 break;
1570 case CHAN_WIDTH_UNKNOWN:
1571 case CHAN_WIDTH_80P80:
1572 /* FIXME: implement this */
1573 return 0;
1574 }
1575
1576 return (abs(freq - freq1) / 20) % 2 == 0 ? 1 : -1;
1577}
1578
1579
Dmitry Shmidt04949592012-07-19 12:16:46 -07001580static void mlme_event_ch_switch(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001581 struct nlattr *ifindex, struct nlattr *freq,
1582 struct nlattr *type, struct nlattr *bw,
1583 struct nlattr *cf1, struct nlattr *cf2)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001584{
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001585 struct i802_bss *bss;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001586 union wpa_event_data data;
1587 int ht_enabled = 1;
1588 int chan_offset = 0;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001589 int ifidx;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001590
1591 wpa_printf(MSG_DEBUG, "nl80211: Channel switch event");
1592
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001593 if (!freq)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001594 return;
1595
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001596 ifidx = nla_get_u32(ifindex);
1597 for (bss = drv->first_bss; bss; bss = bss->next)
1598 if (bss->ifindex == ifidx)
1599 break;
1600
1601 if (bss == NULL) {
1602 wpa_printf(MSG_WARNING, "nl80211: Unknown ifindex (%d) for channel switch, ignoring",
1603 ifidx);
1604 return;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001605 }
1606
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001607 if (type) {
1608 switch (nla_get_u32(type)) {
1609 case NL80211_CHAN_NO_HT:
1610 ht_enabled = 0;
1611 break;
1612 case NL80211_CHAN_HT20:
1613 break;
1614 case NL80211_CHAN_HT40PLUS:
1615 chan_offset = 1;
1616 break;
1617 case NL80211_CHAN_HT40MINUS:
1618 chan_offset = -1;
1619 break;
1620 }
Dmitry Shmidt97672262014-02-03 13:02:54 -08001621 } else if (bw && cf1) {
1622 /* This can happen for example with VHT80 ch switch */
1623 chan_offset = calculate_chan_offset(nla_get_u32(bw),
1624 nla_get_u32(freq),
1625 nla_get_u32(cf1),
1626 cf2 ? nla_get_u32(cf2) : 0);
1627 } else {
1628 wpa_printf(MSG_WARNING, "nl80211: Unknown secondary channel information - following channel definition calculations may fail");
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001629 }
1630
1631 os_memset(&data, 0, sizeof(data));
Dmitry Shmidt04949592012-07-19 12:16:46 -07001632 data.ch_switch.freq = nla_get_u32(freq);
1633 data.ch_switch.ht_enabled = ht_enabled;
1634 data.ch_switch.ch_offset = chan_offset;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001635 if (bw)
1636 data.ch_switch.ch_width = convert2width(nla_get_u32(bw));
1637 if (cf1)
1638 data.ch_switch.cf1 = nla_get_u32(cf1);
1639 if (cf2)
1640 data.ch_switch.cf2 = nla_get_u32(cf2);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001641
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001642 bss->freq = data.ch_switch.freq;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001643
Dmitry Shmidt04949592012-07-19 12:16:46 -07001644 wpa_supplicant_event(drv->ctx, EVENT_CH_SWITCH, &data);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001645}
1646
1647
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001648static void mlme_timeout_event(struct wpa_driver_nl80211_data *drv,
1649 enum nl80211_commands cmd, struct nlattr *addr)
1650{
1651 union wpa_event_data event;
1652 enum wpa_event_type ev;
1653
1654 if (nla_len(addr) != ETH_ALEN)
1655 return;
1656
1657 wpa_printf(MSG_DEBUG, "nl80211: MLME event %d; timeout with " MACSTR,
1658 cmd, MAC2STR((u8 *) nla_data(addr)));
1659
1660 if (cmd == NL80211_CMD_AUTHENTICATE)
1661 ev = EVENT_AUTH_TIMED_OUT;
1662 else if (cmd == NL80211_CMD_ASSOCIATE)
1663 ev = EVENT_ASSOC_TIMED_OUT;
1664 else
1665 return;
1666
1667 os_memset(&event, 0, sizeof(event));
1668 os_memcpy(event.timeout_event.addr, nla_data(addr), ETH_ALEN);
1669 wpa_supplicant_event(drv->ctx, ev, &event);
1670}
1671
1672
1673static void mlme_event_mgmt(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001674 struct nlattr *freq, struct nlattr *sig,
1675 const u8 *frame, size_t len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001676{
1677 const struct ieee80211_mgmt *mgmt;
1678 union wpa_event_data event;
1679 u16 fc, stype;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001680 int ssi_signal = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001681 int rx_freq = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001682
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001683 wpa_printf(MSG_MSGDUMP, "nl80211: Frame event");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001684 mgmt = (const struct ieee80211_mgmt *) frame;
1685 if (len < 24) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001686 wpa_printf(MSG_DEBUG, "nl80211: Too short management frame");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001687 return;
1688 }
1689
1690 fc = le_to_host16(mgmt->frame_control);
1691 stype = WLAN_FC_GET_STYPE(fc);
1692
Dmitry Shmidt04949592012-07-19 12:16:46 -07001693 if (sig)
1694 ssi_signal = (s32) nla_get_u32(sig);
1695
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001696 os_memset(&event, 0, sizeof(event));
1697 if (freq) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001698 event.rx_mgmt.freq = nla_get_u32(freq);
1699 rx_freq = drv->last_mgmt_freq = event.rx_mgmt.freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001700 }
Dmitry Shmidt56052862013-10-04 10:23:25 -07001701 wpa_printf(MSG_DEBUG,
1702 "nl80211: RX frame freq=%d ssi_signal=%d stype=%u len=%u",
1703 rx_freq, ssi_signal, stype, (unsigned int) len);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001704 event.rx_mgmt.frame = frame;
1705 event.rx_mgmt.frame_len = len;
1706 event.rx_mgmt.ssi_signal = ssi_signal;
1707 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001708}
1709
1710
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001711static void mlme_event_mgmt_tx_status(struct wpa_driver_nl80211_data *drv,
1712 struct nlattr *cookie, const u8 *frame,
1713 size_t len, struct nlattr *ack)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001714{
1715 union wpa_event_data event;
1716 const struct ieee80211_hdr *hdr;
1717 u16 fc;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001718
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001719 wpa_printf(MSG_DEBUG, "nl80211: Frame TX status event");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001720 if (!is_ap_interface(drv->nlmode)) {
1721 u64 cookie_val;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001722
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001723 if (!cookie)
1724 return;
1725
1726 cookie_val = nla_get_u64(cookie);
1727 wpa_printf(MSG_DEBUG, "nl80211: Action TX status:"
1728 " cookie=0%llx%s (ack=%d)",
1729 (long long unsigned int) cookie_val,
1730 cookie_val == drv->send_action_cookie ?
1731 " (match)" : " (unknown)", ack != NULL);
1732 if (cookie_val != drv->send_action_cookie)
1733 return;
1734 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001735
1736 hdr = (const struct ieee80211_hdr *) frame;
1737 fc = le_to_host16(hdr->frame_control);
1738
1739 os_memset(&event, 0, sizeof(event));
1740 event.tx_status.type = WLAN_FC_GET_TYPE(fc);
1741 event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
1742 event.tx_status.dst = hdr->addr1;
1743 event.tx_status.data = frame;
1744 event.tx_status.data_len = len;
1745 event.tx_status.ack = ack != NULL;
1746 wpa_supplicant_event(drv->ctx, EVENT_TX_STATUS, &event);
1747}
1748
1749
1750static void mlme_event_deauth_disassoc(struct wpa_driver_nl80211_data *drv,
1751 enum wpa_event_type type,
1752 const u8 *frame, size_t len)
1753{
1754 const struct ieee80211_mgmt *mgmt;
1755 union wpa_event_data event;
1756 const u8 *bssid = NULL;
1757 u16 reason_code = 0;
1758
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001759 if (type == EVENT_DEAUTH)
1760 wpa_printf(MSG_DEBUG, "nl80211: Deauthenticate event");
1761 else
1762 wpa_printf(MSG_DEBUG, "nl80211: Disassociate event");
1763
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001764 mgmt = (const struct ieee80211_mgmt *) frame;
1765 if (len >= 24) {
1766 bssid = mgmt->bssid;
1767
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001768 if ((drv->capa.flags & WPA_DRIVER_FLAGS_SME) &&
1769 !drv->associated &&
1770 os_memcmp(bssid, drv->auth_bssid, ETH_ALEN) != 0 &&
1771 os_memcmp(bssid, drv->auth_attempt_bssid, ETH_ALEN) != 0 &&
1772 os_memcmp(bssid, drv->prev_bssid, ETH_ALEN) == 0) {
1773 /*
1774 * Avoid issues with some roaming cases where
1775 * disconnection event for the old AP may show up after
1776 * we have started connection with the new AP.
1777 */
1778 wpa_printf(MSG_DEBUG, "nl80211: Ignore deauth/disassoc event from old AP " MACSTR " when already authenticating with " MACSTR,
1779 MAC2STR(bssid),
1780 MAC2STR(drv->auth_attempt_bssid));
1781 return;
1782 }
1783
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001784 if (drv->associated != 0 &&
1785 os_memcmp(bssid, drv->bssid, ETH_ALEN) != 0 &&
1786 os_memcmp(bssid, drv->auth_bssid, ETH_ALEN) != 0) {
1787 /*
1788 * We have presumably received this deauth as a
1789 * response to a clear_state_mismatch() outgoing
1790 * deauth. Don't let it take us offline!
1791 */
1792 wpa_printf(MSG_DEBUG, "nl80211: Deauth received "
1793 "from Unknown BSSID " MACSTR " -- ignoring",
1794 MAC2STR(bssid));
1795 return;
1796 }
1797 }
1798
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001799 nl80211_mark_disconnected(drv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001800 os_memset(&event, 0, sizeof(event));
1801
1802 /* Note: Same offset for Reason Code in both frame subtypes */
1803 if (len >= 24 + sizeof(mgmt->u.deauth))
1804 reason_code = le_to_host16(mgmt->u.deauth.reason_code);
1805
1806 if (type == EVENT_DISASSOC) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001807 event.disassoc_info.locally_generated =
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001808 !os_memcmp(mgmt->sa, drv->first_bss->addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001809 event.disassoc_info.addr = bssid;
1810 event.disassoc_info.reason_code = reason_code;
1811 if (frame + len > mgmt->u.disassoc.variable) {
1812 event.disassoc_info.ie = mgmt->u.disassoc.variable;
1813 event.disassoc_info.ie_len = frame + len -
1814 mgmt->u.disassoc.variable;
1815 }
1816 } else {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001817 event.deauth_info.locally_generated =
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001818 !os_memcmp(mgmt->sa, drv->first_bss->addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001819 event.deauth_info.addr = bssid;
1820 event.deauth_info.reason_code = reason_code;
1821 if (frame + len > mgmt->u.deauth.variable) {
1822 event.deauth_info.ie = mgmt->u.deauth.variable;
1823 event.deauth_info.ie_len = frame + len -
1824 mgmt->u.deauth.variable;
1825 }
1826 }
1827
1828 wpa_supplicant_event(drv->ctx, type, &event);
1829}
1830
1831
1832static void mlme_event_unprot_disconnect(struct wpa_driver_nl80211_data *drv,
1833 enum wpa_event_type type,
1834 const u8 *frame, size_t len)
1835{
1836 const struct ieee80211_mgmt *mgmt;
1837 union wpa_event_data event;
1838 u16 reason_code = 0;
1839
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001840 if (type == EVENT_UNPROT_DEAUTH)
1841 wpa_printf(MSG_DEBUG, "nl80211: Unprot Deauthenticate event");
1842 else
1843 wpa_printf(MSG_DEBUG, "nl80211: Unprot Disassociate event");
1844
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001845 if (len < 24)
1846 return;
1847
1848 mgmt = (const struct ieee80211_mgmt *) frame;
1849
1850 os_memset(&event, 0, sizeof(event));
1851 /* Note: Same offset for Reason Code in both frame subtypes */
1852 if (len >= 24 + sizeof(mgmt->u.deauth))
1853 reason_code = le_to_host16(mgmt->u.deauth.reason_code);
1854
1855 if (type == EVENT_UNPROT_DISASSOC) {
1856 event.unprot_disassoc.sa = mgmt->sa;
1857 event.unprot_disassoc.da = mgmt->da;
1858 event.unprot_disassoc.reason_code = reason_code;
1859 } else {
1860 event.unprot_deauth.sa = mgmt->sa;
1861 event.unprot_deauth.da = mgmt->da;
1862 event.unprot_deauth.reason_code = reason_code;
1863 }
1864
1865 wpa_supplicant_event(drv->ctx, type, &event);
1866}
1867
1868
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001869static void mlme_event(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001870 enum nl80211_commands cmd, struct nlattr *frame,
1871 struct nlattr *addr, struct nlattr *timed_out,
1872 struct nlattr *freq, struct nlattr *ack,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001873 struct nlattr *cookie, struct nlattr *sig)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001874{
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001875 struct wpa_driver_nl80211_data *drv = bss->drv;
1876 const u8 *data;
1877 size_t len;
1878
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001879 if (timed_out && addr) {
1880 mlme_timeout_event(drv, cmd, addr);
1881 return;
1882 }
1883
1884 if (frame == NULL) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001885 wpa_printf(MSG_DEBUG,
1886 "nl80211: MLME event %d (%s) without frame data",
1887 cmd, nl80211_command_to_string(cmd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001888 return;
1889 }
1890
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001891 data = nla_data(frame);
1892 len = nla_len(frame);
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001893 if (len < 4 + 2 * ETH_ALEN) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001894 wpa_printf(MSG_MSGDUMP, "nl80211: MLME event %d (%s) on %s("
1895 MACSTR ") - too short",
1896 cmd, nl80211_command_to_string(cmd), bss->ifname,
1897 MAC2STR(bss->addr));
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001898 return;
1899 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001900 wpa_printf(MSG_MSGDUMP, "nl80211: MLME event %d (%s) on %s(" MACSTR
1901 ") A1=" MACSTR " A2=" MACSTR, cmd,
1902 nl80211_command_to_string(cmd), bss->ifname,
1903 MAC2STR(bss->addr), MAC2STR(data + 4),
1904 MAC2STR(data + 4 + ETH_ALEN));
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001905 if (cmd != NL80211_CMD_FRAME_TX_STATUS && !(data[4] & 0x01) &&
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001906 os_memcmp(bss->addr, data + 4, ETH_ALEN) != 0 &&
1907 os_memcmp(bss->addr, data + 4 + ETH_ALEN, ETH_ALEN) != 0) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001908 wpa_printf(MSG_MSGDUMP, "nl80211: %s: Ignore MLME frame event "
1909 "for foreign address", bss->ifname);
1910 return;
1911 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001912 wpa_hexdump(MSG_MSGDUMP, "nl80211: MLME event frame",
1913 nla_data(frame), nla_len(frame));
1914
1915 switch (cmd) {
1916 case NL80211_CMD_AUTHENTICATE:
1917 mlme_event_auth(drv, nla_data(frame), nla_len(frame));
1918 break;
1919 case NL80211_CMD_ASSOCIATE:
1920 mlme_event_assoc(drv, nla_data(frame), nla_len(frame));
1921 break;
1922 case NL80211_CMD_DEAUTHENTICATE:
1923 mlme_event_deauth_disassoc(drv, EVENT_DEAUTH,
1924 nla_data(frame), nla_len(frame));
1925 break;
1926 case NL80211_CMD_DISASSOCIATE:
1927 mlme_event_deauth_disassoc(drv, EVENT_DISASSOC,
1928 nla_data(frame), nla_len(frame));
1929 break;
1930 case NL80211_CMD_FRAME:
Dmitry Shmidt04949592012-07-19 12:16:46 -07001931 mlme_event_mgmt(drv, freq, sig, nla_data(frame),
1932 nla_len(frame));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001933 break;
1934 case NL80211_CMD_FRAME_TX_STATUS:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001935 mlme_event_mgmt_tx_status(drv, cookie, nla_data(frame),
1936 nla_len(frame), ack);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001937 break;
1938 case NL80211_CMD_UNPROT_DEAUTHENTICATE:
1939 mlme_event_unprot_disconnect(drv, EVENT_UNPROT_DEAUTH,
1940 nla_data(frame), nla_len(frame));
1941 break;
1942 case NL80211_CMD_UNPROT_DISASSOCIATE:
1943 mlme_event_unprot_disconnect(drv, EVENT_UNPROT_DISASSOC,
1944 nla_data(frame), nla_len(frame));
1945 break;
1946 default:
1947 break;
1948 }
1949}
1950
1951
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001952static void mlme_event_michael_mic_failure(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001953 struct nlattr *tb[])
1954{
1955 union wpa_event_data data;
1956
1957 wpa_printf(MSG_DEBUG, "nl80211: MLME event Michael MIC failure");
1958 os_memset(&data, 0, sizeof(data));
1959 if (tb[NL80211_ATTR_MAC]) {
1960 wpa_hexdump(MSG_DEBUG, "nl80211: Source MAC address",
1961 nla_data(tb[NL80211_ATTR_MAC]),
1962 nla_len(tb[NL80211_ATTR_MAC]));
1963 data.michael_mic_failure.src = nla_data(tb[NL80211_ATTR_MAC]);
1964 }
1965 if (tb[NL80211_ATTR_KEY_SEQ]) {
1966 wpa_hexdump(MSG_DEBUG, "nl80211: TSC",
1967 nla_data(tb[NL80211_ATTR_KEY_SEQ]),
1968 nla_len(tb[NL80211_ATTR_KEY_SEQ]));
1969 }
1970 if (tb[NL80211_ATTR_KEY_TYPE]) {
1971 enum nl80211_key_type key_type =
1972 nla_get_u32(tb[NL80211_ATTR_KEY_TYPE]);
1973 wpa_printf(MSG_DEBUG, "nl80211: Key Type %d", key_type);
1974 if (key_type == NL80211_KEYTYPE_PAIRWISE)
1975 data.michael_mic_failure.unicast = 1;
1976 } else
1977 data.michael_mic_failure.unicast = 1;
1978
1979 if (tb[NL80211_ATTR_KEY_IDX]) {
1980 u8 key_id = nla_get_u8(tb[NL80211_ATTR_KEY_IDX]);
1981 wpa_printf(MSG_DEBUG, "nl80211: Key Id %d", key_id);
1982 }
1983
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001984 wpa_supplicant_event(bss->ctx, EVENT_MICHAEL_MIC_FAILURE, &data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001985}
1986
1987
1988static void mlme_event_join_ibss(struct wpa_driver_nl80211_data *drv,
1989 struct nlattr *tb[])
1990{
1991 if (tb[NL80211_ATTR_MAC] == NULL) {
1992 wpa_printf(MSG_DEBUG, "nl80211: No address in IBSS joined "
1993 "event");
1994 return;
1995 }
1996 os_memcpy(drv->bssid, nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07001997
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001998 drv->associated = 1;
1999 wpa_printf(MSG_DEBUG, "nl80211: IBSS " MACSTR " joined",
2000 MAC2STR(drv->bssid));
2001
2002 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, NULL);
2003}
2004
2005
2006static void mlme_event_remain_on_channel(struct wpa_driver_nl80211_data *drv,
2007 int cancel_event, struct nlattr *tb[])
2008{
2009 unsigned int freq, chan_type, duration;
2010 union wpa_event_data data;
2011 u64 cookie;
2012
2013 if (tb[NL80211_ATTR_WIPHY_FREQ])
2014 freq = nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]);
2015 else
2016 freq = 0;
2017
2018 if (tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE])
2019 chan_type = nla_get_u32(tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
2020 else
2021 chan_type = 0;
2022
2023 if (tb[NL80211_ATTR_DURATION])
2024 duration = nla_get_u32(tb[NL80211_ATTR_DURATION]);
2025 else
2026 duration = 0;
2027
2028 if (tb[NL80211_ATTR_COOKIE])
2029 cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
2030 else
2031 cookie = 0;
2032
2033 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel event (cancel=%d "
2034 "freq=%u channel_type=%u duration=%u cookie=0x%llx (%s))",
2035 cancel_event, freq, chan_type, duration,
2036 (long long unsigned int) cookie,
2037 cookie == drv->remain_on_chan_cookie ? "match" : "unknown");
2038
2039 if (cookie != drv->remain_on_chan_cookie)
2040 return; /* not for us */
2041
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002042 if (cancel_event)
2043 drv->pending_remain_on_chan = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002044
2045 os_memset(&data, 0, sizeof(data));
2046 data.remain_on_channel.freq = freq;
2047 data.remain_on_channel.duration = duration;
2048 wpa_supplicant_event(drv->ctx, cancel_event ?
2049 EVENT_CANCEL_REMAIN_ON_CHANNEL :
2050 EVENT_REMAIN_ON_CHANNEL, &data);
2051}
2052
2053
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002054static void mlme_event_ft_event(struct wpa_driver_nl80211_data *drv,
2055 struct nlattr *tb[])
2056{
2057 union wpa_event_data data;
2058
2059 os_memset(&data, 0, sizeof(data));
2060
2061 if (tb[NL80211_ATTR_IE]) {
2062 data.ft_ies.ies = nla_data(tb[NL80211_ATTR_IE]);
2063 data.ft_ies.ies_len = nla_len(tb[NL80211_ATTR_IE]);
2064 }
2065
2066 if (tb[NL80211_ATTR_IE_RIC]) {
2067 data.ft_ies.ric_ies = nla_data(tb[NL80211_ATTR_IE_RIC]);
2068 data.ft_ies.ric_ies_len = nla_len(tb[NL80211_ATTR_IE_RIC]);
2069 }
2070
2071 if (tb[NL80211_ATTR_MAC])
2072 os_memcpy(data.ft_ies.target_ap,
2073 nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
2074
2075 wpa_printf(MSG_DEBUG, "nl80211: FT event target_ap " MACSTR,
2076 MAC2STR(data.ft_ies.target_ap));
2077
2078 wpa_supplicant_event(drv->ctx, EVENT_FT_RESPONSE, &data);
2079}
2080
2081
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002082static void send_scan_event(struct wpa_driver_nl80211_data *drv, int aborted,
2083 struct nlattr *tb[])
2084{
2085 union wpa_event_data event;
2086 struct nlattr *nl;
2087 int rem;
2088 struct scan_info *info;
2089#define MAX_REPORT_FREQS 50
2090 int freqs[MAX_REPORT_FREQS];
2091 int num_freqs = 0;
2092
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002093 if (drv->scan_for_auth) {
2094 drv->scan_for_auth = 0;
2095 wpa_printf(MSG_DEBUG, "nl80211: Scan results for missing "
2096 "cfg80211 BSS entry");
2097 wpa_driver_nl80211_authenticate_retry(drv);
2098 return;
2099 }
2100
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002101 os_memset(&event, 0, sizeof(event));
2102 info = &event.scan_info;
2103 info->aborted = aborted;
2104
2105 if (tb[NL80211_ATTR_SCAN_SSIDS]) {
2106 nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_SSIDS], rem) {
2107 struct wpa_driver_scan_ssid *s =
2108 &info->ssids[info->num_ssids];
2109 s->ssid = nla_data(nl);
2110 s->ssid_len = nla_len(nl);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002111 wpa_printf(MSG_DEBUG, "nl80211: Scan probed for SSID '%s'",
2112 wpa_ssid_txt(s->ssid, s->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002113 info->num_ssids++;
2114 if (info->num_ssids == WPAS_MAX_SCAN_SSIDS)
2115 break;
2116 }
2117 }
2118 if (tb[NL80211_ATTR_SCAN_FREQUENCIES]) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002119 char msg[200], *pos, *end;
2120 int res;
2121
2122 pos = msg;
2123 end = pos + sizeof(msg);
2124 *pos = '\0';
2125
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002126 nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_FREQUENCIES], rem)
2127 {
2128 freqs[num_freqs] = nla_get_u32(nl);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002129 res = os_snprintf(pos, end - pos, " %d",
2130 freqs[num_freqs]);
2131 if (res > 0 && end - pos > res)
2132 pos += res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002133 num_freqs++;
2134 if (num_freqs == MAX_REPORT_FREQS - 1)
2135 break;
2136 }
2137 info->freqs = freqs;
2138 info->num_freqs = num_freqs;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002139 wpa_printf(MSG_DEBUG, "nl80211: Scan included frequencies:%s",
2140 msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002141 }
2142 wpa_supplicant_event(drv->ctx, EVENT_SCAN_RESULTS, &event);
2143}
2144
2145
2146static int get_link_signal(struct nl_msg *msg, void *arg)
2147{
2148 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2149 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2150 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
2151 static struct nla_policy policy[NL80211_STA_INFO_MAX + 1] = {
2152 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002153 [NL80211_STA_INFO_SIGNAL_AVG] = { .type = NLA_U8 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002154 };
2155 struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
2156 static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
2157 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
2158 [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
2159 [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
2160 [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
2161 };
2162 struct wpa_signal_info *sig_change = arg;
2163
2164 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2165 genlmsg_attrlen(gnlh, 0), NULL);
2166 if (!tb[NL80211_ATTR_STA_INFO] ||
2167 nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
2168 tb[NL80211_ATTR_STA_INFO], policy))
2169 return NL_SKIP;
2170 if (!sinfo[NL80211_STA_INFO_SIGNAL])
2171 return NL_SKIP;
2172
2173 sig_change->current_signal =
2174 (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
2175
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002176 if (sinfo[NL80211_STA_INFO_SIGNAL_AVG])
2177 sig_change->avg_signal =
2178 (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL_AVG]);
2179 else
2180 sig_change->avg_signal = 0;
2181
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002182 if (sinfo[NL80211_STA_INFO_TX_BITRATE]) {
2183 if (nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
2184 sinfo[NL80211_STA_INFO_TX_BITRATE],
2185 rate_policy)) {
2186 sig_change->current_txrate = 0;
2187 } else {
2188 if (rinfo[NL80211_RATE_INFO_BITRATE]) {
2189 sig_change->current_txrate =
2190 nla_get_u16(rinfo[
2191 NL80211_RATE_INFO_BITRATE]) * 100;
2192 }
2193 }
2194 }
2195
2196 return NL_SKIP;
2197}
2198
2199
2200static int nl80211_get_link_signal(struct wpa_driver_nl80211_data *drv,
2201 struct wpa_signal_info *sig)
2202{
2203 struct nl_msg *msg;
2204
2205 sig->current_signal = -9999;
2206 sig->current_txrate = 0;
2207
2208 msg = nlmsg_alloc();
2209 if (!msg)
2210 return -ENOMEM;
2211
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002212 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002213
2214 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
2215 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid);
2216
2217 return send_and_recv_msgs(drv, msg, get_link_signal, sig);
2218 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002219 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002220 return -ENOBUFS;
2221}
2222
2223
2224static int get_link_noise(struct nl_msg *msg, void *arg)
2225{
2226 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2227 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2228 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
2229 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
2230 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
2231 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
2232 };
2233 struct wpa_signal_info *sig_change = arg;
2234
2235 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2236 genlmsg_attrlen(gnlh, 0), NULL);
2237
2238 if (!tb[NL80211_ATTR_SURVEY_INFO]) {
2239 wpa_printf(MSG_DEBUG, "nl80211: survey data missing!");
2240 return NL_SKIP;
2241 }
2242
2243 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
2244 tb[NL80211_ATTR_SURVEY_INFO],
2245 survey_policy)) {
2246 wpa_printf(MSG_DEBUG, "nl80211: failed to parse nested "
2247 "attributes!");
2248 return NL_SKIP;
2249 }
2250
2251 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
2252 return NL_SKIP;
2253
2254 if (nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
2255 sig_change->frequency)
2256 return NL_SKIP;
2257
2258 if (!sinfo[NL80211_SURVEY_INFO_NOISE])
2259 return NL_SKIP;
2260
2261 sig_change->current_noise =
2262 (s8) nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
2263
2264 return NL_SKIP;
2265}
2266
2267
2268static int nl80211_get_link_noise(struct wpa_driver_nl80211_data *drv,
2269 struct wpa_signal_info *sig_change)
2270{
2271 struct nl_msg *msg;
2272
2273 sig_change->current_noise = 9999;
2274 sig_change->frequency = drv->assoc_freq;
2275
2276 msg = nlmsg_alloc();
2277 if (!msg)
2278 return -ENOMEM;
2279
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002280 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002281
2282 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
2283
2284 return send_and_recv_msgs(drv, msg, get_link_noise, sig_change);
2285 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002286 nlmsg_free(msg);
2287 return -ENOBUFS;
2288}
2289
2290
2291static int get_noise_for_scan_results(struct nl_msg *msg, void *arg)
2292{
2293 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2294 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2295 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
2296 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
2297 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
2298 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
2299 };
2300 struct wpa_scan_results *scan_results = arg;
2301 struct wpa_scan_res *scan_res;
2302 size_t i;
2303
2304 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2305 genlmsg_attrlen(gnlh, 0), NULL);
2306
2307 if (!tb[NL80211_ATTR_SURVEY_INFO]) {
2308 wpa_printf(MSG_DEBUG, "nl80211: Survey data missing");
2309 return NL_SKIP;
2310 }
2311
2312 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
2313 tb[NL80211_ATTR_SURVEY_INFO],
2314 survey_policy)) {
2315 wpa_printf(MSG_DEBUG, "nl80211: Failed to parse nested "
2316 "attributes");
2317 return NL_SKIP;
2318 }
2319
2320 if (!sinfo[NL80211_SURVEY_INFO_NOISE])
2321 return NL_SKIP;
2322
2323 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
2324 return NL_SKIP;
2325
2326 for (i = 0; i < scan_results->num; ++i) {
2327 scan_res = scan_results->res[i];
2328 if (!scan_res)
2329 continue;
2330 if ((int) nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
2331 scan_res->freq)
2332 continue;
2333 if (!(scan_res->flags & WPA_SCAN_NOISE_INVALID))
2334 continue;
2335 scan_res->noise = (s8)
2336 nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
2337 scan_res->flags &= ~WPA_SCAN_NOISE_INVALID;
2338 }
2339
2340 return NL_SKIP;
2341}
2342
2343
2344static int nl80211_get_noise_for_scan_results(
2345 struct wpa_driver_nl80211_data *drv,
2346 struct wpa_scan_results *scan_res)
2347{
2348 struct nl_msg *msg;
2349
2350 msg = nlmsg_alloc();
2351 if (!msg)
2352 return -ENOMEM;
2353
2354 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
2355
2356 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
2357
2358 return send_and_recv_msgs(drv, msg, get_noise_for_scan_results,
2359 scan_res);
2360 nla_put_failure:
2361 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002362 return -ENOBUFS;
2363}
2364
2365
2366static void nl80211_cqm_event(struct wpa_driver_nl80211_data *drv,
2367 struct nlattr *tb[])
2368{
2369 static struct nla_policy cqm_policy[NL80211_ATTR_CQM_MAX + 1] = {
2370 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
2371 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U8 },
2372 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
2373 [NL80211_ATTR_CQM_PKT_LOSS_EVENT] = { .type = NLA_U32 },
2374 };
2375 struct nlattr *cqm[NL80211_ATTR_CQM_MAX + 1];
2376 enum nl80211_cqm_rssi_threshold_event event;
2377 union wpa_event_data ed;
2378 struct wpa_signal_info sig;
2379 int res;
2380
2381 if (tb[NL80211_ATTR_CQM] == NULL ||
2382 nla_parse_nested(cqm, NL80211_ATTR_CQM_MAX, tb[NL80211_ATTR_CQM],
2383 cqm_policy)) {
2384 wpa_printf(MSG_DEBUG, "nl80211: Ignore invalid CQM event");
2385 return;
2386 }
2387
2388 os_memset(&ed, 0, sizeof(ed));
2389
2390 if (cqm[NL80211_ATTR_CQM_PKT_LOSS_EVENT]) {
2391 if (!tb[NL80211_ATTR_MAC])
2392 return;
2393 os_memcpy(ed.low_ack.addr, nla_data(tb[NL80211_ATTR_MAC]),
2394 ETH_ALEN);
2395 wpa_supplicant_event(drv->ctx, EVENT_STATION_LOW_ACK, &ed);
2396 return;
2397 }
2398
2399 if (cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] == NULL)
2400 return;
2401 event = nla_get_u32(cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT]);
2402
2403 if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH) {
2404 wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor "
2405 "event: RSSI high");
2406 ed.signal_change.above_threshold = 1;
2407 } else if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW) {
2408 wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor "
2409 "event: RSSI low");
2410 ed.signal_change.above_threshold = 0;
2411 } else
2412 return;
2413
2414 res = nl80211_get_link_signal(drv, &sig);
2415 if (res == 0) {
2416 ed.signal_change.current_signal = sig.current_signal;
2417 ed.signal_change.current_txrate = sig.current_txrate;
2418 wpa_printf(MSG_DEBUG, "nl80211: Signal: %d dBm txrate: %d",
2419 sig.current_signal, sig.current_txrate);
2420 }
2421
2422 res = nl80211_get_link_noise(drv, &sig);
2423 if (res == 0) {
2424 ed.signal_change.current_noise = sig.current_noise;
2425 wpa_printf(MSG_DEBUG, "nl80211: Noise: %d dBm",
2426 sig.current_noise);
2427 }
2428
2429 wpa_supplicant_event(drv->ctx, EVENT_SIGNAL_CHANGE, &ed);
2430}
2431
2432
2433static void nl80211_new_station_event(struct wpa_driver_nl80211_data *drv,
2434 struct nlattr **tb)
2435{
2436 u8 *addr;
2437 union wpa_event_data data;
2438
2439 if (tb[NL80211_ATTR_MAC] == NULL)
2440 return;
2441 addr = nla_data(tb[NL80211_ATTR_MAC]);
2442 wpa_printf(MSG_DEBUG, "nl80211: New station " MACSTR, MAC2STR(addr));
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07002443
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002444 if (is_ap_interface(drv->nlmode) && drv->device_ap_sme) {
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07002445 u8 *ies = NULL;
2446 size_t ies_len = 0;
2447 if (tb[NL80211_ATTR_IE]) {
2448 ies = nla_data(tb[NL80211_ATTR_IE]);
2449 ies_len = nla_len(tb[NL80211_ATTR_IE]);
2450 }
2451 wpa_hexdump(MSG_DEBUG, "nl80211: Assoc Req IEs", ies, ies_len);
2452 drv_event_assoc(drv->ctx, addr, ies, ies_len, 0);
2453 return;
2454 }
2455
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002456 if (drv->nlmode != NL80211_IFTYPE_ADHOC)
2457 return;
2458
2459 os_memset(&data, 0, sizeof(data));
2460 os_memcpy(data.ibss_rsn_start.peer, addr, ETH_ALEN);
2461 wpa_supplicant_event(drv->ctx, EVENT_IBSS_RSN_START, &data);
2462}
2463
2464
2465static void nl80211_del_station_event(struct wpa_driver_nl80211_data *drv,
2466 struct nlattr **tb)
2467{
2468 u8 *addr;
2469 union wpa_event_data data;
2470
2471 if (tb[NL80211_ATTR_MAC] == NULL)
2472 return;
2473 addr = nla_data(tb[NL80211_ATTR_MAC]);
2474 wpa_printf(MSG_DEBUG, "nl80211: Delete station " MACSTR,
2475 MAC2STR(addr));
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07002476
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002477 if (is_ap_interface(drv->nlmode) && drv->device_ap_sme) {
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07002478 drv_event_disassoc(drv->ctx, addr);
2479 return;
2480 }
2481
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002482 if (drv->nlmode != NL80211_IFTYPE_ADHOC)
2483 return;
2484
2485 os_memset(&data, 0, sizeof(data));
2486 os_memcpy(data.ibss_peer_lost.peer, addr, ETH_ALEN);
2487 wpa_supplicant_event(drv->ctx, EVENT_IBSS_PEER_LOST, &data);
2488}
2489
2490
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002491static void nl80211_rekey_offload_event(struct wpa_driver_nl80211_data *drv,
2492 struct nlattr **tb)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002493{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002494 struct nlattr *rekey_info[NUM_NL80211_REKEY_DATA];
2495 static struct nla_policy rekey_policy[NUM_NL80211_REKEY_DATA] = {
2496 [NL80211_REKEY_DATA_KEK] = {
2497 .minlen = NL80211_KEK_LEN,
2498 .maxlen = NL80211_KEK_LEN,
2499 },
2500 [NL80211_REKEY_DATA_KCK] = {
2501 .minlen = NL80211_KCK_LEN,
2502 .maxlen = NL80211_KCK_LEN,
2503 },
2504 [NL80211_REKEY_DATA_REPLAY_CTR] = {
2505 .minlen = NL80211_REPLAY_CTR_LEN,
2506 .maxlen = NL80211_REPLAY_CTR_LEN,
2507 },
2508 };
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002509 union wpa_event_data data;
2510
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002511 if (!tb[NL80211_ATTR_MAC])
2512 return;
2513 if (!tb[NL80211_ATTR_REKEY_DATA])
2514 return;
2515 if (nla_parse_nested(rekey_info, MAX_NL80211_REKEY_DATA,
2516 tb[NL80211_ATTR_REKEY_DATA], rekey_policy))
2517 return;
2518 if (!rekey_info[NL80211_REKEY_DATA_REPLAY_CTR])
2519 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002520
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002521 os_memset(&data, 0, sizeof(data));
2522 data.driver_gtk_rekey.bssid = nla_data(tb[NL80211_ATTR_MAC]);
2523 wpa_printf(MSG_DEBUG, "nl80211: Rekey offload event for BSSID " MACSTR,
2524 MAC2STR(data.driver_gtk_rekey.bssid));
2525 data.driver_gtk_rekey.replay_ctr =
2526 nla_data(rekey_info[NL80211_REKEY_DATA_REPLAY_CTR]);
2527 wpa_hexdump(MSG_DEBUG, "nl80211: Rekey offload - Replay Counter",
2528 data.driver_gtk_rekey.replay_ctr, NL80211_REPLAY_CTR_LEN);
2529 wpa_supplicant_event(drv->ctx, EVENT_DRIVER_GTK_REKEY, &data);
2530}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002531
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002532
2533static void nl80211_pmksa_candidate_event(struct wpa_driver_nl80211_data *drv,
2534 struct nlattr **tb)
2535{
2536 struct nlattr *cand[NUM_NL80211_PMKSA_CANDIDATE];
2537 static struct nla_policy cand_policy[NUM_NL80211_PMKSA_CANDIDATE] = {
2538 [NL80211_PMKSA_CANDIDATE_INDEX] = { .type = NLA_U32 },
2539 [NL80211_PMKSA_CANDIDATE_BSSID] = {
2540 .minlen = ETH_ALEN,
2541 .maxlen = ETH_ALEN,
2542 },
2543 [NL80211_PMKSA_CANDIDATE_PREAUTH] = { .type = NLA_FLAG },
2544 };
2545 union wpa_event_data data;
2546
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002547 wpa_printf(MSG_DEBUG, "nl80211: PMKSA candidate event");
2548
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002549 if (!tb[NL80211_ATTR_PMKSA_CANDIDATE])
2550 return;
2551 if (nla_parse_nested(cand, MAX_NL80211_PMKSA_CANDIDATE,
2552 tb[NL80211_ATTR_PMKSA_CANDIDATE], cand_policy))
2553 return;
2554 if (!cand[NL80211_PMKSA_CANDIDATE_INDEX] ||
2555 !cand[NL80211_PMKSA_CANDIDATE_BSSID])
2556 return;
2557
2558 os_memset(&data, 0, sizeof(data));
2559 os_memcpy(data.pmkid_candidate.bssid,
2560 nla_data(cand[NL80211_PMKSA_CANDIDATE_BSSID]), ETH_ALEN);
2561 data.pmkid_candidate.index =
2562 nla_get_u32(cand[NL80211_PMKSA_CANDIDATE_INDEX]);
2563 data.pmkid_candidate.preauth =
2564 cand[NL80211_PMKSA_CANDIDATE_PREAUTH] != NULL;
2565 wpa_supplicant_event(drv->ctx, EVENT_PMKID_CANDIDATE, &data);
2566}
2567
2568
2569static void nl80211_client_probe_event(struct wpa_driver_nl80211_data *drv,
2570 struct nlattr **tb)
2571{
2572 union wpa_event_data data;
2573
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002574 wpa_printf(MSG_DEBUG, "nl80211: Probe client event");
2575
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002576 if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_ACK])
2577 return;
2578
2579 os_memset(&data, 0, sizeof(data));
2580 os_memcpy(data.client_poll.addr,
2581 nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
2582
2583 wpa_supplicant_event(drv->ctx, EVENT_DRIVER_CLIENT_POLL_OK, &data);
2584}
2585
2586
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002587static void nl80211_tdls_oper_event(struct wpa_driver_nl80211_data *drv,
2588 struct nlattr **tb)
2589{
2590 union wpa_event_data data;
2591
2592 wpa_printf(MSG_DEBUG, "nl80211: TDLS operation event");
2593
2594 if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_TDLS_OPERATION])
2595 return;
2596
2597 os_memset(&data, 0, sizeof(data));
2598 os_memcpy(data.tdls.peer, nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
2599 switch (nla_get_u8(tb[NL80211_ATTR_TDLS_OPERATION])) {
2600 case NL80211_TDLS_SETUP:
2601 wpa_printf(MSG_DEBUG, "nl80211: TDLS setup request for peer "
2602 MACSTR, MAC2STR(data.tdls.peer));
2603 data.tdls.oper = TDLS_REQUEST_SETUP;
2604 break;
2605 case NL80211_TDLS_TEARDOWN:
2606 wpa_printf(MSG_DEBUG, "nl80211: TDLS teardown request for peer "
2607 MACSTR, MAC2STR(data.tdls.peer));
2608 data.tdls.oper = TDLS_REQUEST_TEARDOWN;
2609 break;
2610 default:
2611 wpa_printf(MSG_DEBUG, "nl80211: Unsupported TDLS operatione "
2612 "event");
2613 return;
2614 }
2615 if (tb[NL80211_ATTR_REASON_CODE]) {
2616 data.tdls.reason_code =
2617 nla_get_u16(tb[NL80211_ATTR_REASON_CODE]);
2618 }
2619
2620 wpa_supplicant_event(drv->ctx, EVENT_TDLS, &data);
2621}
2622
2623
Dmitry Shmidt5393a0f2013-08-08 11:23:34 -07002624static void nl80211_stop_ap(struct wpa_driver_nl80211_data *drv,
2625 struct nlattr **tb)
2626{
2627 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_UNAVAILABLE, NULL);
2628}
2629
2630
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002631static void nl80211_connect_failed_event(struct wpa_driver_nl80211_data *drv,
2632 struct nlattr **tb)
2633{
2634 union wpa_event_data data;
2635 u32 reason;
2636
2637 wpa_printf(MSG_DEBUG, "nl80211: Connect failed event");
2638
2639 if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_CONN_FAILED_REASON])
2640 return;
2641
2642 os_memset(&data, 0, sizeof(data));
2643 os_memcpy(data.connect_failed_reason.addr,
2644 nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
2645
2646 reason = nla_get_u32(tb[NL80211_ATTR_CONN_FAILED_REASON]);
2647 switch (reason) {
2648 case NL80211_CONN_FAIL_MAX_CLIENTS:
2649 wpa_printf(MSG_DEBUG, "nl80211: Max client reached");
2650 data.connect_failed_reason.code = MAX_CLIENT_REACHED;
2651 break;
2652 case NL80211_CONN_FAIL_BLOCKED_CLIENT:
2653 wpa_printf(MSG_DEBUG, "nl80211: Blocked client " MACSTR
2654 " tried to connect",
2655 MAC2STR(data.connect_failed_reason.addr));
2656 data.connect_failed_reason.code = BLOCKED_CLIENT;
2657 break;
2658 default:
2659 wpa_printf(MSG_DEBUG, "nl8021l: Unknown connect failed reason "
2660 "%u", reason);
2661 return;
2662 }
2663
2664 wpa_supplicant_event(drv->ctx, EVENT_CONNECT_FAILED_REASON, &data);
2665}
2666
2667
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002668static void nl80211_radar_event(struct wpa_driver_nl80211_data *drv,
2669 struct nlattr **tb)
2670{
2671 union wpa_event_data data;
2672 enum nl80211_radar_event event_type;
2673
2674 if (!tb[NL80211_ATTR_WIPHY_FREQ] || !tb[NL80211_ATTR_RADAR_EVENT])
2675 return;
2676
2677 os_memset(&data, 0, sizeof(data));
Dmitry Shmidt051af732013-10-22 13:52:46 -07002678 data.dfs_event.freq = nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]);
2679 event_type = nla_get_u32(tb[NL80211_ATTR_RADAR_EVENT]);
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002680
Dmitry Shmidt051af732013-10-22 13:52:46 -07002681 /* Check HT params */
2682 if (tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
2683 data.dfs_event.ht_enabled = 1;
2684 data.dfs_event.chan_offset = 0;
2685
2686 switch (nla_get_u32(tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE])) {
2687 case NL80211_CHAN_NO_HT:
2688 data.dfs_event.ht_enabled = 0;
2689 break;
2690 case NL80211_CHAN_HT20:
2691 break;
2692 case NL80211_CHAN_HT40PLUS:
2693 data.dfs_event.chan_offset = 1;
2694 break;
2695 case NL80211_CHAN_HT40MINUS:
2696 data.dfs_event.chan_offset = -1;
2697 break;
2698 }
2699 }
2700
2701 /* Get VHT params */
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002702 if (tb[NL80211_ATTR_CHANNEL_WIDTH])
2703 data.dfs_event.chan_width =
2704 convert2width(nla_get_u32(
2705 tb[NL80211_ATTR_CHANNEL_WIDTH]));
2706 if (tb[NL80211_ATTR_CENTER_FREQ1])
2707 data.dfs_event.cf1 = nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002708 if (tb[NL80211_ATTR_CENTER_FREQ2])
2709 data.dfs_event.cf2 = nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]);
2710
2711 wpa_printf(MSG_DEBUG, "nl80211: DFS event on freq %d MHz, ht: %d, offset: %d, width: %d, cf1: %dMHz, cf2: %dMHz",
2712 data.dfs_event.freq, data.dfs_event.ht_enabled,
2713 data.dfs_event.chan_offset, data.dfs_event.chan_width,
2714 data.dfs_event.cf1, data.dfs_event.cf2);
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002715
2716 switch (event_type) {
2717 case NL80211_RADAR_DETECTED:
2718 wpa_supplicant_event(drv->ctx, EVENT_DFS_RADAR_DETECTED, &data);
2719 break;
2720 case NL80211_RADAR_CAC_FINISHED:
2721 wpa_supplicant_event(drv->ctx, EVENT_DFS_CAC_FINISHED, &data);
2722 break;
2723 case NL80211_RADAR_CAC_ABORTED:
2724 wpa_supplicant_event(drv->ctx, EVENT_DFS_CAC_ABORTED, &data);
2725 break;
2726 case NL80211_RADAR_NOP_FINISHED:
2727 wpa_supplicant_event(drv->ctx, EVENT_DFS_NOP_FINISHED, &data);
2728 break;
2729 default:
2730 wpa_printf(MSG_DEBUG, "nl80211: Unknown radar event %d "
2731 "received", event_type);
2732 break;
2733 }
2734}
2735
2736
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002737static void nl80211_spurious_frame(struct i802_bss *bss, struct nlattr **tb,
2738 int wds)
2739{
2740 struct wpa_driver_nl80211_data *drv = bss->drv;
2741 union wpa_event_data event;
2742
2743 if (!tb[NL80211_ATTR_MAC])
2744 return;
2745
2746 os_memset(&event, 0, sizeof(event));
2747 event.rx_from_unknown.bssid = bss->addr;
2748 event.rx_from_unknown.addr = nla_data(tb[NL80211_ATTR_MAC]);
2749 event.rx_from_unknown.wds = wds;
2750
2751 wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event);
2752}
2753
2754
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002755static void qca_nl80211_avoid_freq(struct wpa_driver_nl80211_data *drv,
2756 const u8 *data, size_t len)
2757{
2758 u32 i, count;
2759 union wpa_event_data event;
2760 struct wpa_freq_range *range = NULL;
2761 const struct qca_avoid_freq_list *freq_range;
2762
2763 freq_range = (const struct qca_avoid_freq_list *) data;
2764 if (len < sizeof(freq_range->count))
2765 return;
2766
2767 count = freq_range->count;
2768 if (len < sizeof(freq_range->count) +
2769 count * sizeof(struct qca_avoid_freq_range)) {
2770 wpa_printf(MSG_DEBUG, "nl80211: Ignored too short avoid frequency list (len=%u)",
2771 (unsigned int) len);
2772 return;
2773 }
2774
2775 if (count > 0) {
2776 range = os_calloc(count, sizeof(struct wpa_freq_range));
2777 if (range == NULL)
2778 return;
2779 }
2780
2781 os_memset(&event, 0, sizeof(event));
2782 for (i = 0; i < count; i++) {
2783 unsigned int idx = event.freq_range.num;
2784 range[idx].min = freq_range->range[i].start_freq;
2785 range[idx].max = freq_range->range[i].end_freq;
2786 wpa_printf(MSG_DEBUG, "nl80211: Avoid frequency range: %u-%u",
2787 range[idx].min, range[idx].max);
2788 if (range[idx].min > range[idx].max) {
2789 wpa_printf(MSG_DEBUG, "nl80211: Ignore invalid frequency range");
2790 continue;
2791 }
2792 event.freq_range.num++;
2793 }
2794 event.freq_range.range = range;
2795
2796 wpa_supplicant_event(drv->ctx, EVENT_AVOID_FREQUENCIES, &event);
2797
2798 os_free(range);
2799}
2800
2801
2802static void nl80211_vendor_event_qca(struct wpa_driver_nl80211_data *drv,
2803 u32 subcmd, u8 *data, size_t len)
2804{
2805 switch (subcmd) {
2806 case QCA_NL80211_VENDOR_SUBCMD_AVOID_FREQUENCY:
2807 qca_nl80211_avoid_freq(drv, data, len);
2808 break;
2809 default:
2810 wpa_printf(MSG_DEBUG,
2811 "nl80211: Ignore unsupported QCA vendor event %u",
2812 subcmd);
2813 break;
2814 }
2815}
2816
2817
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002818static void nl80211_vendor_event(struct wpa_driver_nl80211_data *drv,
2819 struct nlattr **tb)
2820{
2821 u32 vendor_id, subcmd, wiphy = 0;
2822 int wiphy_idx;
2823 u8 *data = NULL;
2824 size_t len = 0;
2825
2826 if (!tb[NL80211_ATTR_VENDOR_ID] ||
2827 !tb[NL80211_ATTR_VENDOR_SUBCMD])
2828 return;
2829
2830 vendor_id = nla_get_u32(tb[NL80211_ATTR_VENDOR_ID]);
2831 subcmd = nla_get_u32(tb[NL80211_ATTR_VENDOR_SUBCMD]);
2832
2833 if (tb[NL80211_ATTR_WIPHY])
2834 wiphy = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
2835
2836 wpa_printf(MSG_DEBUG, "nl80211: Vendor event: wiphy=%u vendor_id=0x%x subcmd=%u",
2837 wiphy, vendor_id, subcmd);
2838
2839 if (tb[NL80211_ATTR_VENDOR_DATA]) {
2840 data = nla_data(tb[NL80211_ATTR_VENDOR_DATA]);
2841 len = nla_len(tb[NL80211_ATTR_VENDOR_DATA]);
2842 wpa_hexdump(MSG_MSGDUMP, "nl80211: Vendor data", data, len);
2843 }
2844
2845 wiphy_idx = nl80211_get_wiphy_index(drv->first_bss);
2846 if (wiphy_idx >= 0 && wiphy_idx != (int) wiphy) {
2847 wpa_printf(MSG_DEBUG, "nl80211: Ignore vendor event for foreign wiphy %u (own: %d)",
2848 wiphy, wiphy_idx);
2849 return;
2850 }
2851
2852 switch (vendor_id) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002853 case OUI_QCA:
2854 nl80211_vendor_event_qca(drv, subcmd, data, len);
2855 break;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002856 default:
2857 wpa_printf(MSG_DEBUG, "nl80211: Ignore unsupported vendor event");
2858 break;
2859 }
2860}
2861
2862
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002863static void do_process_drv_event(struct i802_bss *bss, int cmd,
2864 struct nlattr **tb)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002865{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002866 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002867 union wpa_event_data data;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002868
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002869 wpa_printf(MSG_DEBUG, "nl80211: Drv Event %d (%s) received for %s",
2870 cmd, nl80211_command_to_string(cmd), bss->ifname);
2871
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002872 if (drv->ap_scan_as_station != NL80211_IFTYPE_UNSPECIFIED &&
2873 (cmd == NL80211_CMD_NEW_SCAN_RESULTS ||
2874 cmd == NL80211_CMD_SCAN_ABORTED)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002875 wpa_driver_nl80211_set_mode(drv->first_bss,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002876 drv->ap_scan_as_station);
2877 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002878 }
2879
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002880 switch (cmd) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002881 case NL80211_CMD_TRIGGER_SCAN:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002882 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Scan trigger");
Dmitry Shmidt56052862013-10-04 10:23:25 -07002883 drv->scan_state = SCAN_STARTED;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002884 wpa_supplicant_event(drv->ctx, EVENT_SCAN_STARTED, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002885 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002886 case NL80211_CMD_START_SCHED_SCAN:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002887 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Sched scan started");
Dmitry Shmidt56052862013-10-04 10:23:25 -07002888 drv->scan_state = SCHED_SCAN_STARTED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002889 break;
2890 case NL80211_CMD_SCHED_SCAN_STOPPED:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002891 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Sched scan stopped");
Dmitry Shmidt56052862013-10-04 10:23:25 -07002892 drv->scan_state = SCHED_SCAN_STOPPED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002893 wpa_supplicant_event(drv->ctx, EVENT_SCHED_SCAN_STOPPED, NULL);
2894 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002895 case NL80211_CMD_NEW_SCAN_RESULTS:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002896 wpa_dbg(drv->ctx, MSG_DEBUG,
2897 "nl80211: New scan results available");
Dmitry Shmidt56052862013-10-04 10:23:25 -07002898 drv->scan_state = SCAN_COMPLETED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002899 drv->scan_complete_events = 1;
2900 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
2901 drv->ctx);
2902 send_scan_event(drv, 0, tb);
2903 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002904 case NL80211_CMD_SCHED_SCAN_RESULTS:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002905 wpa_dbg(drv->ctx, MSG_DEBUG,
2906 "nl80211: New sched scan results available");
Dmitry Shmidt56052862013-10-04 10:23:25 -07002907 drv->scan_state = SCHED_SCAN_RESULTS;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002908 send_scan_event(drv, 0, tb);
2909 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002910 case NL80211_CMD_SCAN_ABORTED:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002911 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Scan aborted");
Dmitry Shmidt56052862013-10-04 10:23:25 -07002912 drv->scan_state = SCAN_ABORTED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002913 /*
2914 * Need to indicate that scan results are available in order
2915 * not to make wpa_supplicant stop its scanning.
2916 */
2917 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
2918 drv->ctx);
2919 send_scan_event(drv, 1, tb);
2920 break;
2921 case NL80211_CMD_AUTHENTICATE:
2922 case NL80211_CMD_ASSOCIATE:
2923 case NL80211_CMD_DEAUTHENTICATE:
2924 case NL80211_CMD_DISASSOCIATE:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002925 case NL80211_CMD_FRAME_TX_STATUS:
2926 case NL80211_CMD_UNPROT_DEAUTHENTICATE:
2927 case NL80211_CMD_UNPROT_DISASSOCIATE:
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002928 mlme_event(bss, cmd, tb[NL80211_ATTR_FRAME],
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002929 tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT],
2930 tb[NL80211_ATTR_WIPHY_FREQ], tb[NL80211_ATTR_ACK],
Dmitry Shmidt04949592012-07-19 12:16:46 -07002931 tb[NL80211_ATTR_COOKIE],
2932 tb[NL80211_ATTR_RX_SIGNAL_DBM]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002933 break;
2934 case NL80211_CMD_CONNECT:
2935 case NL80211_CMD_ROAM:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002936 mlme_event_connect(drv, cmd,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002937 tb[NL80211_ATTR_STATUS_CODE],
2938 tb[NL80211_ATTR_MAC],
2939 tb[NL80211_ATTR_REQ_IE],
2940 tb[NL80211_ATTR_RESP_IE]);
2941 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002942 case NL80211_CMD_CH_SWITCH_NOTIFY:
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002943 mlme_event_ch_switch(drv,
2944 tb[NL80211_ATTR_IFINDEX],
2945 tb[NL80211_ATTR_WIPHY_FREQ],
2946 tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE],
2947 tb[NL80211_ATTR_CHANNEL_WIDTH],
2948 tb[NL80211_ATTR_CENTER_FREQ1],
2949 tb[NL80211_ATTR_CENTER_FREQ2]);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002950 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002951 case NL80211_CMD_DISCONNECT:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002952 mlme_event_disconnect(drv, tb[NL80211_ATTR_REASON_CODE],
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002953 tb[NL80211_ATTR_MAC],
2954 tb[NL80211_ATTR_DISCONNECTED_BY_AP]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002955 break;
2956 case NL80211_CMD_MICHAEL_MIC_FAILURE:
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002957 mlme_event_michael_mic_failure(bss, tb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002958 break;
2959 case NL80211_CMD_JOIN_IBSS:
2960 mlme_event_join_ibss(drv, tb);
2961 break;
2962 case NL80211_CMD_REMAIN_ON_CHANNEL:
2963 mlme_event_remain_on_channel(drv, 0, tb);
2964 break;
2965 case NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL:
2966 mlme_event_remain_on_channel(drv, 1, tb);
2967 break;
2968 case NL80211_CMD_NOTIFY_CQM:
2969 nl80211_cqm_event(drv, tb);
2970 break;
2971 case NL80211_CMD_REG_CHANGE:
2972 wpa_printf(MSG_DEBUG, "nl80211: Regulatory domain change");
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002973 if (tb[NL80211_ATTR_REG_INITIATOR] == NULL)
2974 break;
2975 os_memset(&data, 0, sizeof(data));
2976 switch (nla_get_u8(tb[NL80211_ATTR_REG_INITIATOR])) {
2977 case NL80211_REGDOM_SET_BY_CORE:
2978 data.channel_list_changed.initiator =
2979 REGDOM_SET_BY_CORE;
2980 break;
2981 case NL80211_REGDOM_SET_BY_USER:
2982 data.channel_list_changed.initiator =
2983 REGDOM_SET_BY_USER;
2984 break;
2985 case NL80211_REGDOM_SET_BY_DRIVER:
2986 data.channel_list_changed.initiator =
2987 REGDOM_SET_BY_DRIVER;
2988 break;
2989 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
2990 data.channel_list_changed.initiator =
2991 REGDOM_SET_BY_COUNTRY_IE;
2992 break;
2993 default:
2994 wpa_printf(MSG_DEBUG, "nl80211: Unknown reg change initiator %d received",
2995 nla_get_u8(tb[NL80211_ATTR_REG_INITIATOR]));
2996 break;
2997 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002998 wpa_supplicant_event(drv->ctx, EVENT_CHANNEL_LIST_CHANGED,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002999 &data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003000 break;
3001 case NL80211_CMD_REG_BEACON_HINT:
3002 wpa_printf(MSG_DEBUG, "nl80211: Regulatory beacon hint");
Dmitry Shmidt97672262014-02-03 13:02:54 -08003003 os_memset(&data, 0, sizeof(data));
3004 data.channel_list_changed.initiator = REGDOM_BEACON_HINT;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003005 wpa_supplicant_event(drv->ctx, EVENT_CHANNEL_LIST_CHANGED,
Dmitry Shmidt97672262014-02-03 13:02:54 -08003006 &data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003007 break;
3008 case NL80211_CMD_NEW_STATION:
3009 nl80211_new_station_event(drv, tb);
3010 break;
3011 case NL80211_CMD_DEL_STATION:
3012 nl80211_del_station_event(drv, tb);
3013 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003014 case NL80211_CMD_SET_REKEY_OFFLOAD:
3015 nl80211_rekey_offload_event(drv, tb);
3016 break;
3017 case NL80211_CMD_PMKSA_CANDIDATE:
3018 nl80211_pmksa_candidate_event(drv, tb);
3019 break;
3020 case NL80211_CMD_PROBE_CLIENT:
3021 nl80211_client_probe_event(drv, tb);
3022 break;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003023 case NL80211_CMD_TDLS_OPER:
3024 nl80211_tdls_oper_event(drv, tb);
3025 break;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003026 case NL80211_CMD_CONN_FAILED:
3027 nl80211_connect_failed_event(drv, tb);
3028 break;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003029 case NL80211_CMD_FT_EVENT:
3030 mlme_event_ft_event(drv, tb);
3031 break;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07003032 case NL80211_CMD_RADAR_DETECT:
3033 nl80211_radar_event(drv, tb);
3034 break;
Dmitry Shmidt5393a0f2013-08-08 11:23:34 -07003035 case NL80211_CMD_STOP_AP:
3036 nl80211_stop_ap(drv, tb);
3037 break;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003038 case NL80211_CMD_VENDOR:
3039 nl80211_vendor_event(drv, tb);
3040 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003041 default:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003042 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Ignored unknown event "
3043 "(cmd=%d)", cmd);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003044 break;
3045 }
3046}
3047
3048
3049static int process_drv_event(struct nl_msg *msg, void *arg)
3050{
3051 struct wpa_driver_nl80211_data *drv = arg;
3052 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3053 struct nlattr *tb[NL80211_ATTR_MAX + 1];
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003054 struct i802_bss *bss;
3055 int ifidx = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003056
3057 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3058 genlmsg_attrlen(gnlh, 0), NULL);
3059
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003060 if (tb[NL80211_ATTR_IFINDEX]) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003061 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
3062
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003063 for (bss = drv->first_bss; bss; bss = bss->next)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003064 if (ifidx == -1 || ifidx == bss->ifindex) {
3065 do_process_drv_event(bss, gnlh->cmd, tb);
3066 return NL_SKIP;
3067 }
3068 wpa_printf(MSG_DEBUG,
3069 "nl80211: Ignored event (cmd=%d) for foreign interface (ifindex %d)",
3070 gnlh->cmd, ifidx);
3071 } else if (tb[NL80211_ATTR_WDEV]) {
3072 u64 wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
3073 wpa_printf(MSG_DEBUG, "nl80211: Process event on P2P device");
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003074 for (bss = drv->first_bss; bss; bss = bss->next) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003075 if (bss->wdev_id_set && wdev_id == bss->wdev_id) {
3076 do_process_drv_event(bss, gnlh->cmd, tb);
3077 return NL_SKIP;
3078 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003079 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003080 wpa_printf(MSG_DEBUG,
3081 "nl80211: Ignored event (cmd=%d) for foreign interface (wdev 0x%llx)",
3082 gnlh->cmd, (long long unsigned int) wdev_id);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003083 }
3084
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003085 return NL_SKIP;
3086}
3087
3088
3089static int process_global_event(struct nl_msg *msg, void *arg)
3090{
3091 struct nl80211_global *global = arg;
3092 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3093 struct nlattr *tb[NL80211_ATTR_MAX + 1];
Dmitry Shmidt04949592012-07-19 12:16:46 -07003094 struct wpa_driver_nl80211_data *drv, *tmp;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003095 int ifidx = -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003096 struct i802_bss *bss;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003097 u64 wdev_id = 0;
3098 int wdev_id_set = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003099
3100 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3101 genlmsg_attrlen(gnlh, 0), NULL);
3102
3103 if (tb[NL80211_ATTR_IFINDEX])
3104 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003105 else if (tb[NL80211_ATTR_WDEV]) {
3106 wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
3107 wdev_id_set = 1;
3108 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003109
Dmitry Shmidt04949592012-07-19 12:16:46 -07003110 dl_list_for_each_safe(drv, tmp, &global->interfaces,
3111 struct wpa_driver_nl80211_data, list) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003112 for (bss = drv->first_bss; bss; bss = bss->next) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003113 if ((ifidx == -1 && !wdev_id_set) ||
3114 ifidx == bss->ifindex ||
3115 (wdev_id_set && bss->wdev_id_set &&
3116 wdev_id == bss->wdev_id)) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003117 do_process_drv_event(bss, gnlh->cmd, tb);
3118 return NL_SKIP;
3119 }
3120 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003121 }
3122
3123 return NL_SKIP;
3124}
3125
3126
3127static int process_bss_event(struct nl_msg *msg, void *arg)
3128{
3129 struct i802_bss *bss = arg;
3130 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3131 struct nlattr *tb[NL80211_ATTR_MAX + 1];
3132
3133 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3134 genlmsg_attrlen(gnlh, 0), NULL);
3135
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003136 wpa_printf(MSG_DEBUG, "nl80211: BSS Event %d (%s) received for %s",
3137 gnlh->cmd, nl80211_command_to_string(gnlh->cmd),
3138 bss->ifname);
3139
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003140 switch (gnlh->cmd) {
3141 case NL80211_CMD_FRAME:
3142 case NL80211_CMD_FRAME_TX_STATUS:
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003143 mlme_event(bss, gnlh->cmd, tb[NL80211_ATTR_FRAME],
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003144 tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT],
3145 tb[NL80211_ATTR_WIPHY_FREQ], tb[NL80211_ATTR_ACK],
Dmitry Shmidt04949592012-07-19 12:16:46 -07003146 tb[NL80211_ATTR_COOKIE],
3147 tb[NL80211_ATTR_RX_SIGNAL_DBM]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003148 break;
3149 case NL80211_CMD_UNEXPECTED_FRAME:
3150 nl80211_spurious_frame(bss, tb, 0);
3151 break;
3152 case NL80211_CMD_UNEXPECTED_4ADDR_FRAME:
3153 nl80211_spurious_frame(bss, tb, 1);
3154 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003155 default:
3156 wpa_printf(MSG_DEBUG, "nl80211: Ignored unknown event "
3157 "(cmd=%d)", gnlh->cmd);
3158 break;
3159 }
3160
3161 return NL_SKIP;
3162}
3163
3164
3165static void wpa_driver_nl80211_event_receive(int sock, void *eloop_ctx,
3166 void *handle)
3167{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003168 struct nl_cb *cb = eloop_ctx;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003169 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003170
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003171 wpa_printf(MSG_MSGDUMP, "nl80211: Event message available");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003172
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003173 res = nl_recvmsgs(handle, cb);
3174 if (res) {
3175 wpa_printf(MSG_INFO, "nl80211: %s->nl_recvmsgs failed: %d",
3176 __func__, res);
3177 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003178}
3179
3180
3181/**
3182 * wpa_driver_nl80211_set_country - ask nl80211 to set the regulatory domain
3183 * @priv: driver_nl80211 private data
3184 * @alpha2_arg: country to which to switch to
3185 * Returns: 0 on success, -1 on failure
3186 *
3187 * This asks nl80211 to set the regulatory domain for given
3188 * country ISO / IEC alpha2.
3189 */
3190static int wpa_driver_nl80211_set_country(void *priv, const char *alpha2_arg)
3191{
3192 struct i802_bss *bss = priv;
3193 struct wpa_driver_nl80211_data *drv = bss->drv;
3194 char alpha2[3];
3195 struct nl_msg *msg;
3196
3197 msg = nlmsg_alloc();
3198 if (!msg)
3199 return -ENOMEM;
3200
3201 alpha2[0] = alpha2_arg[0];
3202 alpha2[1] = alpha2_arg[1];
3203 alpha2[2] = '\0';
3204
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003205 nl80211_cmd(drv, msg, 0, NL80211_CMD_REQ_SET_REG);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003206
3207 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, alpha2);
3208 if (send_and_recv_msgs(drv, msg, NULL, NULL))
3209 return -EINVAL;
3210 return 0;
3211nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003212 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003213 return -EINVAL;
3214}
3215
3216
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003217static int nl80211_get_country(struct nl_msg *msg, void *arg)
3218{
3219 char *alpha2 = arg;
3220 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
3221 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3222
3223 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3224 genlmsg_attrlen(gnlh, 0), NULL);
3225 if (!tb_msg[NL80211_ATTR_REG_ALPHA2]) {
3226 wpa_printf(MSG_DEBUG, "nl80211: No country information available");
3227 return NL_SKIP;
3228 }
3229 os_strlcpy(alpha2, nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]), 3);
3230 return NL_SKIP;
3231}
3232
3233
3234static int wpa_driver_nl80211_get_country(void *priv, char *alpha2)
3235{
3236 struct i802_bss *bss = priv;
3237 struct wpa_driver_nl80211_data *drv = bss->drv;
3238 struct nl_msg *msg;
3239 int ret;
3240
3241 msg = nlmsg_alloc();
3242 if (!msg)
3243 return -ENOMEM;
3244
3245 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG);
3246 alpha2[0] = '\0';
3247 ret = send_and_recv_msgs(drv, msg, nl80211_get_country, alpha2);
3248 if (!alpha2[0])
3249 ret = -1;
3250
3251 return ret;
3252}
3253
3254
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003255static int protocol_feature_handler(struct nl_msg *msg, void *arg)
3256{
3257 u32 *feat = arg;
3258 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
3259 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3260
3261 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3262 genlmsg_attrlen(gnlh, 0), NULL);
3263
3264 if (tb_msg[NL80211_ATTR_PROTOCOL_FEATURES])
3265 *feat = nla_get_u32(tb_msg[NL80211_ATTR_PROTOCOL_FEATURES]);
3266
3267 return NL_SKIP;
3268}
3269
3270
3271static u32 get_nl80211_protocol_features(struct wpa_driver_nl80211_data *drv)
3272{
3273 u32 feat = 0;
3274 struct nl_msg *msg;
3275
3276 msg = nlmsg_alloc();
3277 if (!msg)
3278 goto nla_put_failure;
3279
3280 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_PROTOCOL_FEATURES);
3281 if (send_and_recv_msgs(drv, msg, protocol_feature_handler, &feat) == 0)
3282 return feat;
3283
3284 msg = NULL;
3285nla_put_failure:
3286 nlmsg_free(msg);
3287 return 0;
3288}
3289
3290
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003291struct wiphy_info_data {
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003292 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003293 struct wpa_driver_capa *capa;
3294
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003295 unsigned int num_multichan_concurrent;
3296
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003297 unsigned int error:1;
3298 unsigned int device_ap_sme:1;
3299 unsigned int poll_command_supported:1;
3300 unsigned int data_tx_status:1;
3301 unsigned int monitor_supported:1;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003302 unsigned int auth_supported:1;
3303 unsigned int connect_supported:1;
3304 unsigned int p2p_go_supported:1;
3305 unsigned int p2p_client_supported:1;
3306 unsigned int p2p_concurrent:1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003307 unsigned int channel_switch_supported:1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003308 unsigned int set_qos_map_supported:1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003309};
3310
3311
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003312static unsigned int probe_resp_offload_support(int supp_protocols)
3313{
3314 unsigned int prot = 0;
3315
3316 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS)
3317 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS;
3318 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2)
3319 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS2;
3320 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P)
3321 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_P2P;
3322 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_80211U)
3323 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_INTERWORKING;
3324
3325 return prot;
3326}
3327
3328
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003329static void wiphy_info_supported_iftypes(struct wiphy_info_data *info,
3330 struct nlattr *tb)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003331{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003332 struct nlattr *nl_mode;
3333 int i;
3334
3335 if (tb == NULL)
3336 return;
3337
3338 nla_for_each_nested(nl_mode, tb, i) {
3339 switch (nla_type(nl_mode)) {
3340 case NL80211_IFTYPE_AP:
3341 info->capa->flags |= WPA_DRIVER_FLAGS_AP;
3342 break;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003343 case NL80211_IFTYPE_ADHOC:
3344 info->capa->flags |= WPA_DRIVER_FLAGS_IBSS;
3345 break;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003346 case NL80211_IFTYPE_P2P_DEVICE:
3347 info->capa->flags |=
3348 WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE;
3349 break;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003350 case NL80211_IFTYPE_P2P_GO:
3351 info->p2p_go_supported = 1;
3352 break;
3353 case NL80211_IFTYPE_P2P_CLIENT:
3354 info->p2p_client_supported = 1;
3355 break;
3356 case NL80211_IFTYPE_MONITOR:
3357 info->monitor_supported = 1;
3358 break;
3359 }
3360 }
3361}
3362
3363
3364static int wiphy_info_iface_comb_process(struct wiphy_info_data *info,
3365 struct nlattr *nl_combi)
3366{
3367 struct nlattr *tb_comb[NUM_NL80211_IFACE_COMB];
3368 struct nlattr *tb_limit[NUM_NL80211_IFACE_LIMIT];
3369 struct nlattr *nl_limit, *nl_mode;
3370 int err, rem_limit, rem_mode;
3371 int combination_has_p2p = 0, combination_has_mgd = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003372 static struct nla_policy
3373 iface_combination_policy[NUM_NL80211_IFACE_COMB] = {
3374 [NL80211_IFACE_COMB_LIMITS] = { .type = NLA_NESTED },
3375 [NL80211_IFACE_COMB_MAXNUM] = { .type = NLA_U32 },
3376 [NL80211_IFACE_COMB_STA_AP_BI_MATCH] = { .type = NLA_FLAG },
3377 [NL80211_IFACE_COMB_NUM_CHANNELS] = { .type = NLA_U32 },
Dmitry Shmidtea69e842013-05-13 14:52:28 -07003378 [NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS] = { .type = NLA_U32 },
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003379 },
3380 iface_limit_policy[NUM_NL80211_IFACE_LIMIT] = {
3381 [NL80211_IFACE_LIMIT_TYPES] = { .type = NLA_NESTED },
3382 [NL80211_IFACE_LIMIT_MAX] = { .type = NLA_U32 },
3383 };
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003384
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003385 err = nla_parse_nested(tb_comb, MAX_NL80211_IFACE_COMB,
3386 nl_combi, iface_combination_policy);
3387 if (err || !tb_comb[NL80211_IFACE_COMB_LIMITS] ||
3388 !tb_comb[NL80211_IFACE_COMB_MAXNUM] ||
3389 !tb_comb[NL80211_IFACE_COMB_NUM_CHANNELS])
3390 return 0; /* broken combination */
3391
Dmitry Shmidtea69e842013-05-13 14:52:28 -07003392 if (tb_comb[NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS])
3393 info->capa->flags |= WPA_DRIVER_FLAGS_RADAR;
3394
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003395 nla_for_each_nested(nl_limit, tb_comb[NL80211_IFACE_COMB_LIMITS],
3396 rem_limit) {
3397 err = nla_parse_nested(tb_limit, MAX_NL80211_IFACE_LIMIT,
3398 nl_limit, iface_limit_policy);
3399 if (err || !tb_limit[NL80211_IFACE_LIMIT_TYPES])
3400 return 0; /* broken combination */
3401
3402 nla_for_each_nested(nl_mode,
3403 tb_limit[NL80211_IFACE_LIMIT_TYPES],
3404 rem_mode) {
3405 int ift = nla_type(nl_mode);
3406 if (ift == NL80211_IFTYPE_P2P_GO ||
3407 ift == NL80211_IFTYPE_P2P_CLIENT)
3408 combination_has_p2p = 1;
3409 if (ift == NL80211_IFTYPE_STATION)
3410 combination_has_mgd = 1;
3411 }
3412 if (combination_has_p2p && combination_has_mgd)
3413 break;
3414 }
3415
3416 if (combination_has_p2p && combination_has_mgd) {
3417 info->p2p_concurrent = 1;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003418 info->num_multichan_concurrent =
3419 nla_get_u32(tb_comb[NL80211_IFACE_COMB_NUM_CHANNELS]);
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003420 return 1;
3421 }
3422
3423 return 0;
3424}
3425
3426
3427static void wiphy_info_iface_comb(struct wiphy_info_data *info,
3428 struct nlattr *tb)
3429{
3430 struct nlattr *nl_combi;
3431 int rem_combi;
3432
3433 if (tb == NULL)
3434 return;
3435
3436 nla_for_each_nested(nl_combi, tb, rem_combi) {
3437 if (wiphy_info_iface_comb_process(info, nl_combi) > 0)
3438 break;
3439 }
3440}
3441
3442
3443static void wiphy_info_supp_cmds(struct wiphy_info_data *info,
3444 struct nlattr *tb)
3445{
3446 struct nlattr *nl_cmd;
3447 int i;
3448
3449 if (tb == NULL)
3450 return;
3451
3452 nla_for_each_nested(nl_cmd, tb, i) {
3453 switch (nla_get_u32(nl_cmd)) {
3454 case NL80211_CMD_AUTHENTICATE:
3455 info->auth_supported = 1;
3456 break;
3457 case NL80211_CMD_CONNECT:
3458 info->connect_supported = 1;
3459 break;
3460 case NL80211_CMD_START_SCHED_SCAN:
3461 info->capa->sched_scan_supported = 1;
3462 break;
3463 case NL80211_CMD_PROBE_CLIENT:
3464 info->poll_command_supported = 1;
3465 break;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003466 case NL80211_CMD_CHANNEL_SWITCH:
3467 info->channel_switch_supported = 1;
3468 break;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003469 case NL80211_CMD_SET_QOS_MAP:
3470 info->set_qos_map_supported = 1;
3471 break;
3472 }
3473 }
3474}
3475
3476
3477static void wiphy_info_cipher_suites(struct wiphy_info_data *info,
3478 struct nlattr *tb)
3479{
3480 int i, num;
3481 u32 *ciphers;
3482
3483 if (tb == NULL)
3484 return;
3485
3486 num = nla_len(tb) / sizeof(u32);
3487 ciphers = nla_data(tb);
3488 for (i = 0; i < num; i++) {
3489 u32 c = ciphers[i];
3490
3491 wpa_printf(MSG_DEBUG, "nl80211: Supported cipher %02x-%02x-%02x:%d",
3492 c >> 24, (c >> 16) & 0xff,
3493 (c >> 8) & 0xff, c & 0xff);
3494 switch (c) {
3495 case WLAN_CIPHER_SUITE_CCMP_256:
3496 info->capa->enc |= WPA_DRIVER_CAPA_ENC_CCMP_256;
3497 break;
3498 case WLAN_CIPHER_SUITE_GCMP_256:
3499 info->capa->enc |= WPA_DRIVER_CAPA_ENC_GCMP_256;
3500 break;
3501 case WLAN_CIPHER_SUITE_CCMP:
3502 info->capa->enc |= WPA_DRIVER_CAPA_ENC_CCMP;
3503 break;
3504 case WLAN_CIPHER_SUITE_GCMP:
3505 info->capa->enc |= WPA_DRIVER_CAPA_ENC_GCMP;
3506 break;
3507 case WLAN_CIPHER_SUITE_TKIP:
3508 info->capa->enc |= WPA_DRIVER_CAPA_ENC_TKIP;
3509 break;
3510 case WLAN_CIPHER_SUITE_WEP104:
3511 info->capa->enc |= WPA_DRIVER_CAPA_ENC_WEP104;
3512 break;
3513 case WLAN_CIPHER_SUITE_WEP40:
3514 info->capa->enc |= WPA_DRIVER_CAPA_ENC_WEP40;
3515 break;
3516 case WLAN_CIPHER_SUITE_AES_CMAC:
3517 info->capa->enc |= WPA_DRIVER_CAPA_ENC_BIP;
3518 break;
3519 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
3520 info->capa->enc |= WPA_DRIVER_CAPA_ENC_BIP_GMAC_128;
3521 break;
3522 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
3523 info->capa->enc |= WPA_DRIVER_CAPA_ENC_BIP_GMAC_256;
3524 break;
3525 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
3526 info->capa->enc |= WPA_DRIVER_CAPA_ENC_BIP_CMAC_256;
3527 break;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003528 case WLAN_CIPHER_SUITE_NO_GROUP_ADDR:
3529 info->capa->enc |= WPA_DRIVER_CAPA_ENC_GTK_NOT_USED;
3530 break;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003531 }
3532 }
3533}
3534
3535
3536static void wiphy_info_max_roc(struct wpa_driver_capa *capa,
3537 struct nlattr *tb)
3538{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003539 if (tb)
3540 capa->max_remain_on_chan = nla_get_u32(tb);
3541}
3542
3543
3544static void wiphy_info_tdls(struct wpa_driver_capa *capa, struct nlattr *tdls,
3545 struct nlattr *ext_setup)
3546{
3547 if (tdls == NULL)
3548 return;
3549
3550 wpa_printf(MSG_DEBUG, "nl80211: TDLS supported");
3551 capa->flags |= WPA_DRIVER_FLAGS_TDLS_SUPPORT;
3552
3553 if (ext_setup) {
3554 wpa_printf(MSG_DEBUG, "nl80211: TDLS external setup");
3555 capa->flags |= WPA_DRIVER_FLAGS_TDLS_EXTERNAL_SETUP;
3556 }
3557}
3558
3559
3560static void wiphy_info_feature_flags(struct wiphy_info_data *info,
3561 struct nlattr *tb)
3562{
3563 u32 flags;
3564 struct wpa_driver_capa *capa = info->capa;
3565
3566 if (tb == NULL)
3567 return;
3568
3569 flags = nla_get_u32(tb);
3570
3571 if (flags & NL80211_FEATURE_SK_TX_STATUS)
3572 info->data_tx_status = 1;
3573
3574 if (flags & NL80211_FEATURE_INACTIVITY_TIMER)
3575 capa->flags |= WPA_DRIVER_FLAGS_INACTIVITY_TIMER;
3576
3577 if (flags & NL80211_FEATURE_SAE)
3578 capa->flags |= WPA_DRIVER_FLAGS_SAE;
3579
3580 if (flags & NL80211_FEATURE_NEED_OBSS_SCAN)
3581 capa->flags |= WPA_DRIVER_FLAGS_OBSS_SCAN;
3582}
3583
3584
3585static void wiphy_info_probe_resp_offload(struct wpa_driver_capa *capa,
3586 struct nlattr *tb)
3587{
3588 u32 protocols;
3589
3590 if (tb == NULL)
3591 return;
3592
3593 protocols = nla_get_u32(tb);
3594 wpa_printf(MSG_DEBUG, "nl80211: Supports Probe Response offload in AP "
3595 "mode");
3596 capa->flags |= WPA_DRIVER_FLAGS_PROBE_RESP_OFFLOAD;
3597 capa->probe_resp_offloads = probe_resp_offload_support(protocols);
3598}
3599
3600
3601static int wiphy_info_handler(struct nl_msg *msg, void *arg)
3602{
3603 struct nlattr *tb[NL80211_ATTR_MAX + 1];
3604 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3605 struct wiphy_info_data *info = arg;
3606 struct wpa_driver_capa *capa = info->capa;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003607 struct wpa_driver_nl80211_data *drv = info->drv;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003608
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003609 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3610 genlmsg_attrlen(gnlh, 0), NULL);
3611
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003612 if (tb[NL80211_ATTR_WIPHY_NAME])
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003613 os_strlcpy(drv->phyname,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003614 nla_get_string(tb[NL80211_ATTR_WIPHY_NAME]),
3615 sizeof(drv->phyname));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003616 if (tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS])
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003617 capa->max_scan_ssids =
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003618 nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS]);
3619
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003620 if (tb[NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS])
3621 capa->max_sched_scan_ssids =
3622 nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS]);
3623
3624 if (tb[NL80211_ATTR_MAX_MATCH_SETS])
3625 capa->max_match_sets =
3626 nla_get_u8(tb[NL80211_ATTR_MAX_MATCH_SETS]);
3627
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003628 if (tb[NL80211_ATTR_MAC_ACL_MAX])
3629 capa->max_acl_mac_addrs =
3630 nla_get_u8(tb[NL80211_ATTR_MAC_ACL_MAX]);
3631
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003632 wiphy_info_supported_iftypes(info, tb[NL80211_ATTR_SUPPORTED_IFTYPES]);
3633 wiphy_info_iface_comb(info, tb[NL80211_ATTR_INTERFACE_COMBINATIONS]);
3634 wiphy_info_supp_cmds(info, tb[NL80211_ATTR_SUPPORTED_COMMANDS]);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003635 wiphy_info_cipher_suites(info, tb[NL80211_ATTR_CIPHER_SUITES]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003636
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003637 if (tb[NL80211_ATTR_OFFCHANNEL_TX_OK]) {
3638 wpa_printf(MSG_DEBUG, "nl80211: Using driver-based "
3639 "off-channel TX");
3640 capa->flags |= WPA_DRIVER_FLAGS_OFFCHANNEL_TX;
3641 }
3642
3643 if (tb[NL80211_ATTR_ROAM_SUPPORT]) {
3644 wpa_printf(MSG_DEBUG, "nl80211: Using driver-based roaming");
3645 capa->flags |= WPA_DRIVER_FLAGS_BSS_SELECTION;
3646 }
3647
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003648 wiphy_info_max_roc(capa,
3649 tb[NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003650
3651 if (tb[NL80211_ATTR_SUPPORT_AP_UAPSD])
3652 capa->flags |= WPA_DRIVER_FLAGS_AP_UAPSD;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003653
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003654 wiphy_info_tdls(capa, tb[NL80211_ATTR_TDLS_SUPPORT],
3655 tb[NL80211_ATTR_TDLS_EXTERNAL_SETUP]);
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003656
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003657 if (tb[NL80211_ATTR_DEVICE_AP_SME])
3658 info->device_ap_sme = 1;
3659
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003660 wiphy_info_feature_flags(info, tb[NL80211_ATTR_FEATURE_FLAGS]);
3661 wiphy_info_probe_resp_offload(capa,
3662 tb[NL80211_ATTR_PROBE_RESP_OFFLOAD]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003663
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003664 if (tb[NL80211_ATTR_EXT_CAPA] && tb[NL80211_ATTR_EXT_CAPA_MASK] &&
3665 drv->extended_capa == NULL) {
3666 drv->extended_capa =
3667 os_malloc(nla_len(tb[NL80211_ATTR_EXT_CAPA]));
3668 if (drv->extended_capa) {
3669 os_memcpy(drv->extended_capa,
3670 nla_data(tb[NL80211_ATTR_EXT_CAPA]),
3671 nla_len(tb[NL80211_ATTR_EXT_CAPA]));
3672 drv->extended_capa_len =
3673 nla_len(tb[NL80211_ATTR_EXT_CAPA]);
3674 }
3675 drv->extended_capa_mask =
3676 os_malloc(nla_len(tb[NL80211_ATTR_EXT_CAPA]));
3677 if (drv->extended_capa_mask) {
3678 os_memcpy(drv->extended_capa_mask,
3679 nla_data(tb[NL80211_ATTR_EXT_CAPA]),
3680 nla_len(tb[NL80211_ATTR_EXT_CAPA]));
3681 } else {
3682 os_free(drv->extended_capa);
3683 drv->extended_capa = NULL;
3684 drv->extended_capa_len = 0;
3685 }
3686 }
3687
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003688 if (tb[NL80211_ATTR_VENDOR_DATA]) {
3689 struct nlattr *nl;
3690 int rem;
3691
3692 nla_for_each_nested(nl, tb[NL80211_ATTR_VENDOR_DATA], rem) {
3693 struct nl80211_vendor_cmd_info *vinfo;
Dmitry Shmidt18463232014-01-24 12:29:41 -08003694 if (nla_len(nl) != sizeof(*vinfo)) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003695 wpa_printf(MSG_DEBUG, "nl80211: Unexpected vendor data info");
3696 continue;
3697 }
3698 vinfo = nla_data(nl);
3699 wpa_printf(MSG_DEBUG, "nl80211: Supported vendor command: vendor_id=0x%x subcmd=%u",
3700 vinfo->vendor_id, vinfo->subcmd);
3701 }
3702 }
3703
3704 if (tb[NL80211_ATTR_VENDOR_EVENTS]) {
3705 struct nlattr *nl;
3706 int rem;
3707
3708 nla_for_each_nested(nl, tb[NL80211_ATTR_VENDOR_EVENTS], rem) {
3709 struct nl80211_vendor_cmd_info *vinfo;
Dmitry Shmidt18463232014-01-24 12:29:41 -08003710 if (nla_len(nl) != sizeof(*vinfo)) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003711 wpa_printf(MSG_DEBUG, "nl80211: Unexpected vendor data info");
3712 continue;
3713 }
3714 vinfo = nla_data(nl);
3715 wpa_printf(MSG_DEBUG, "nl80211: Supported vendor event: vendor_id=0x%x subcmd=%u",
3716 vinfo->vendor_id, vinfo->subcmd);
3717 }
3718 }
3719
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003720 return NL_SKIP;
3721}
3722
3723
3724static int wpa_driver_nl80211_get_info(struct wpa_driver_nl80211_data *drv,
3725 struct wiphy_info_data *info)
3726{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003727 u32 feat;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003728 struct nl_msg *msg;
3729
3730 os_memset(info, 0, sizeof(*info));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003731 info->capa = &drv->capa;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003732 info->drv = drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003733
3734 msg = nlmsg_alloc();
3735 if (!msg)
3736 return -1;
3737
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003738 feat = get_nl80211_protocol_features(drv);
3739 if (feat & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP)
3740 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_WIPHY);
3741 else
3742 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003743
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003744 NLA_PUT_FLAG(msg, NL80211_ATTR_SPLIT_WIPHY_DUMP);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003745 if (nl80211_set_iface_id(msg, drv->first_bss) < 0)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003746 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003747
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003748 if (send_and_recv_msgs(drv, msg, wiphy_info_handler, info))
3749 return -1;
3750
3751 if (info->auth_supported)
3752 drv->capa.flags |= WPA_DRIVER_FLAGS_SME;
3753 else if (!info->connect_supported) {
3754 wpa_printf(MSG_INFO, "nl80211: Driver does not support "
3755 "authentication/association or connect commands");
3756 info->error = 1;
3757 }
3758
3759 if (info->p2p_go_supported && info->p2p_client_supported)
3760 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CAPABLE;
3761 if (info->p2p_concurrent) {
3762 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
3763 "interface (driver advertised support)");
3764 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
3765 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
3766 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003767 if (info->num_multichan_concurrent > 1) {
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003768 wpa_printf(MSG_DEBUG, "nl80211: Enable multi-channel "
3769 "concurrent (driver advertised support)");
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003770 drv->capa.num_multichan_concurrent =
3771 info->num_multichan_concurrent;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003772 }
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07003773
3774 /* default to 5000 since early versions of mac80211 don't set it */
3775 if (!drv->capa.max_remain_on_chan)
3776 drv->capa.max_remain_on_chan = 5000;
3777
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003778 if (info->channel_switch_supported)
3779 drv->capa.flags |= WPA_DRIVER_FLAGS_AP_CSA;
3780
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003781 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003782nla_put_failure:
3783 nlmsg_free(msg);
3784 return -1;
3785}
3786
3787
3788static int wpa_driver_nl80211_capa(struct wpa_driver_nl80211_data *drv)
3789{
3790 struct wiphy_info_data info;
3791 if (wpa_driver_nl80211_get_info(drv, &info))
3792 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003793
3794 if (info.error)
3795 return -1;
3796
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003797 drv->has_capability = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003798 drv->capa.key_mgmt = WPA_DRIVER_CAPA_KEY_MGMT_WPA |
3799 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
3800 WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
3801 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003802 drv->capa.auth = WPA_DRIVER_AUTH_OPEN |
3803 WPA_DRIVER_AUTH_SHARED |
3804 WPA_DRIVER_AUTH_LEAP;
3805
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003806 drv->capa.flags |= WPA_DRIVER_FLAGS_SANE_ERROR_CODES;
3807 drv->capa.flags |= WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003808 drv->capa.flags |= WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003809
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003810 if (!info.device_ap_sme) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07003811 drv->capa.flags |= WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003812
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003813 /*
3814 * No AP SME is currently assumed to also indicate no AP MLME
3815 * in the driver/firmware.
3816 */
3817 drv->capa.flags |= WPA_DRIVER_FLAGS_AP_MLME;
3818 }
3819
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003820 drv->device_ap_sme = info.device_ap_sme;
3821 drv->poll_command_supported = info.poll_command_supported;
3822 drv->data_tx_status = info.data_tx_status;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003823 if (info.set_qos_map_supported)
3824 drv->capa.flags |= WPA_DRIVER_FLAGS_QOS_MAPPING;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003825
3826 /*
Dmitry Shmidtaa532512012-09-24 10:35:31 -07003827 * If poll command and tx status are supported, mac80211 is new enough
3828 * to have everything we need to not need monitor interfaces.
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003829 */
Dmitry Shmidtaa532512012-09-24 10:35:31 -07003830 drv->use_monitor = !info.poll_command_supported || !info.data_tx_status;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003831
3832 if (drv->device_ap_sme && drv->use_monitor) {
3833 /*
3834 * Non-mac80211 drivers may not support monitor interface.
3835 * Make sure we do not get stuck with incorrect capability here
3836 * by explicitly testing this.
3837 */
3838 if (!info.monitor_supported) {
3839 wpa_printf(MSG_DEBUG, "nl80211: Disable use_monitor "
3840 "with device_ap_sme since no monitor mode "
3841 "support detected");
3842 drv->use_monitor = 0;
3843 }
3844 }
3845
3846 /*
3847 * If we aren't going to use monitor interfaces, but the
3848 * driver doesn't support data TX status, we won't get TX
3849 * status for EAPOL frames.
3850 */
3851 if (!drv->use_monitor && !info.data_tx_status)
3852 drv->capa.flags &= ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003853
3854 return 0;
3855}
3856
3857
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003858#ifdef ANDROID
3859static int android_genl_ctrl_resolve(struct nl_handle *handle,
3860 const char *name)
3861{
3862 /*
3863 * Android ICS has very minimal genl_ctrl_resolve() implementation, so
3864 * need to work around that.
3865 */
3866 struct nl_cache *cache = NULL;
3867 struct genl_family *nl80211 = NULL;
3868 int id = -1;
3869
3870 if (genl_ctrl_alloc_cache(handle, &cache) < 0) {
3871 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic "
3872 "netlink cache");
3873 goto fail;
3874 }
3875
3876 nl80211 = genl_ctrl_search_by_name(cache, name);
3877 if (nl80211 == NULL)
3878 goto fail;
3879
3880 id = genl_family_get_id(nl80211);
3881
3882fail:
3883 if (nl80211)
3884 genl_family_put(nl80211);
3885 if (cache)
3886 nl_cache_free(cache);
3887
3888 return id;
3889}
3890#define genl_ctrl_resolve android_genl_ctrl_resolve
3891#endif /* ANDROID */
3892
3893
3894static int wpa_driver_nl80211_init_nl_global(struct nl80211_global *global)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003895{
3896 int ret;
3897
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003898 global->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
3899 if (global->nl_cb == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003900 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
3901 "callbacks");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003902 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003903 }
3904
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003905 global->nl = nl_create_handle(global->nl_cb, "nl");
3906 if (global->nl == NULL)
3907 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003908
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003909 global->nl80211_id = genl_ctrl_resolve(global->nl, "nl80211");
3910 if (global->nl80211_id < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003911 wpa_printf(MSG_ERROR, "nl80211: 'nl80211' generic netlink not "
3912 "found");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003913 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003914 }
3915
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003916 global->nl_event = nl_create_handle(global->nl_cb, "event");
3917 if (global->nl_event == NULL)
3918 goto err;
3919
3920 ret = nl_get_multicast_id(global, "nl80211", "scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003921 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003922 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003923 if (ret < 0) {
3924 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
3925 "membership for scan events: %d (%s)",
3926 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003927 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003928 }
3929
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003930 ret = nl_get_multicast_id(global, "nl80211", "mlme");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003931 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003932 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003933 if (ret < 0) {
3934 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
3935 "membership for mlme events: %d (%s)",
3936 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003937 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003938 }
3939
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003940 ret = nl_get_multicast_id(global, "nl80211", "regulatory");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003941 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003942 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003943 if (ret < 0) {
3944 wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
3945 "membership for regulatory events: %d (%s)",
3946 ret, strerror(-ret));
3947 /* Continue without regulatory events */
3948 }
3949
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003950 ret = nl_get_multicast_id(global, "nl80211", "vendor");
3951 if (ret >= 0)
3952 ret = nl_socket_add_membership(global->nl_event, ret);
3953 if (ret < 0) {
3954 wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
3955 "membership for vendor events: %d (%s)",
3956 ret, strerror(-ret));
3957 /* Continue without vendor events */
3958 }
3959
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003960 nl_cb_set(global->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
3961 no_seq_check, NULL);
3962 nl_cb_set(global->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
3963 process_global_event, global);
3964
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003965 nl80211_register_eloop_read(&global->nl_event,
3966 wpa_driver_nl80211_event_receive,
3967 global->nl_cb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003968
3969 return 0;
3970
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003971err:
3972 nl_destroy_handles(&global->nl_event);
3973 nl_destroy_handles(&global->nl);
3974 nl_cb_put(global->nl_cb);
3975 global->nl_cb = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003976 return -1;
3977}
3978
3979
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003980static int wpa_driver_nl80211_init_nl(struct wpa_driver_nl80211_data *drv)
3981{
3982 drv->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
3983 if (!drv->nl_cb) {
3984 wpa_printf(MSG_ERROR, "nl80211: Failed to alloc cb struct");
3985 return -1;
3986 }
3987
3988 nl_cb_set(drv->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
3989 no_seq_check, NULL);
3990 nl_cb_set(drv->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
3991 process_drv_event, drv);
3992
3993 return 0;
3994}
3995
3996
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003997static void wpa_driver_nl80211_rfkill_blocked(void *ctx)
3998{
3999 wpa_printf(MSG_DEBUG, "nl80211: RFKILL blocked");
4000 /*
4001 * This may be for any interface; use ifdown event to disable
4002 * interface.
4003 */
4004}
4005
4006
4007static void wpa_driver_nl80211_rfkill_unblocked(void *ctx)
4008{
4009 struct wpa_driver_nl80211_data *drv = ctx;
4010 wpa_printf(MSG_DEBUG, "nl80211: RFKILL unblocked");
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004011 if (i802_set_iface_flags(drv->first_bss, 1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004012 wpa_printf(MSG_DEBUG, "nl80211: Could not set interface UP "
4013 "after rfkill unblock");
4014 return;
4015 }
4016 /* rtnetlink ifup handler will report interface as enabled */
4017}
4018
4019
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004020static void wpa_driver_nl80211_handle_eapol_tx_status(int sock,
4021 void *eloop_ctx,
4022 void *handle)
4023{
4024 struct wpa_driver_nl80211_data *drv = eloop_ctx;
4025 u8 data[2048];
4026 struct msghdr msg;
4027 struct iovec entry;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004028 u8 control[512];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004029 struct cmsghdr *cmsg;
4030 int res, found_ee = 0, found_wifi = 0, acked = 0;
4031 union wpa_event_data event;
4032
4033 memset(&msg, 0, sizeof(msg));
4034 msg.msg_iov = &entry;
4035 msg.msg_iovlen = 1;
4036 entry.iov_base = data;
4037 entry.iov_len = sizeof(data);
4038 msg.msg_control = &control;
4039 msg.msg_controllen = sizeof(control);
4040
4041 res = recvmsg(sock, &msg, MSG_ERRQUEUE);
4042 /* if error or not fitting 802.3 header, return */
4043 if (res < 14)
4044 return;
4045
4046 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg))
4047 {
4048 if (cmsg->cmsg_level == SOL_SOCKET &&
4049 cmsg->cmsg_type == SCM_WIFI_STATUS) {
4050 int *ack;
4051
4052 found_wifi = 1;
4053 ack = (void *)CMSG_DATA(cmsg);
4054 acked = *ack;
4055 }
4056
4057 if (cmsg->cmsg_level == SOL_PACKET &&
4058 cmsg->cmsg_type == PACKET_TX_TIMESTAMP) {
4059 struct sock_extended_err *err =
4060 (struct sock_extended_err *)CMSG_DATA(cmsg);
4061
4062 if (err->ee_origin == SO_EE_ORIGIN_TXSTATUS)
4063 found_ee = 1;
4064 }
4065 }
4066
4067 if (!found_ee || !found_wifi)
4068 return;
4069
4070 memset(&event, 0, sizeof(event));
4071 event.eapol_tx_status.dst = data;
4072 event.eapol_tx_status.data = data + 14;
4073 event.eapol_tx_status.data_len = res - 14;
4074 event.eapol_tx_status.ack = acked;
4075 wpa_supplicant_event(drv->ctx, EVENT_EAPOL_TX_STATUS, &event);
4076}
4077
4078
4079static int nl80211_init_bss(struct i802_bss *bss)
4080{
4081 bss->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
4082 if (!bss->nl_cb)
4083 return -1;
4084
4085 nl_cb_set(bss->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
4086 no_seq_check, NULL);
4087 nl_cb_set(bss->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
4088 process_bss_event, bss);
4089
4090 return 0;
4091}
4092
4093
4094static void nl80211_destroy_bss(struct i802_bss *bss)
4095{
4096 nl_cb_put(bss->nl_cb);
4097 bss->nl_cb = NULL;
4098}
4099
4100
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004101static void * wpa_driver_nl80211_drv_init(void *ctx, const char *ifname,
4102 void *global_priv, int hostapd,
4103 const u8 *set_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004104{
4105 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004106 struct rfkill_config *rcfg;
4107 struct i802_bss *bss;
4108
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004109 if (global_priv == NULL)
4110 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004111 drv = os_zalloc(sizeof(*drv));
4112 if (drv == NULL)
4113 return NULL;
4114 drv->global = global_priv;
4115 drv->ctx = ctx;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004116 drv->hostapd = !!hostapd;
4117 drv->eapol_sock = -1;
4118 drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int);
4119 drv->if_indices = drv->default_if_indices;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004120
4121 drv->first_bss = os_zalloc(sizeof(*drv->first_bss));
4122 if (!drv->first_bss) {
4123 os_free(drv);
4124 return NULL;
4125 }
4126 bss = drv->first_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004127 bss->drv = drv;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004128 bss->ctx = ctx;
4129
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004130 os_strlcpy(bss->ifname, ifname, sizeof(bss->ifname));
4131 drv->monitor_ifidx = -1;
4132 drv->monitor_sock = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004133 drv->eapol_tx_sock = -1;
4134 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004135
4136 if (wpa_driver_nl80211_init_nl(drv)) {
4137 os_free(drv);
4138 return NULL;
4139 }
4140
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004141 if (nl80211_init_bss(bss))
4142 goto failed;
4143
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004144 rcfg = os_zalloc(sizeof(*rcfg));
4145 if (rcfg == NULL)
4146 goto failed;
4147 rcfg->ctx = drv;
4148 os_strlcpy(rcfg->ifname, ifname, sizeof(rcfg->ifname));
4149 rcfg->blocked_cb = wpa_driver_nl80211_rfkill_blocked;
4150 rcfg->unblocked_cb = wpa_driver_nl80211_rfkill_unblocked;
4151 drv->rfkill = rfkill_init(rcfg);
4152 if (drv->rfkill == NULL) {
4153 wpa_printf(MSG_DEBUG, "nl80211: RFKILL status not available");
4154 os_free(rcfg);
4155 }
4156
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004157 if (linux_iface_up(drv->global->ioctl_sock, ifname) > 0)
4158 drv->start_iface_up = 1;
4159
4160 if (wpa_driver_nl80211_finish_drv_init(drv, set_addr, 1))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004161 goto failed;
4162
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004163 drv->eapol_tx_sock = socket(PF_PACKET, SOCK_DGRAM, 0);
4164 if (drv->eapol_tx_sock < 0)
4165 goto failed;
4166
4167 if (drv->data_tx_status) {
4168 int enabled = 1;
4169
4170 if (setsockopt(drv->eapol_tx_sock, SOL_SOCKET, SO_WIFI_STATUS,
4171 &enabled, sizeof(enabled)) < 0) {
4172 wpa_printf(MSG_DEBUG,
4173 "nl80211: wifi status sockopt failed\n");
4174 drv->data_tx_status = 0;
4175 if (!drv->use_monitor)
4176 drv->capa.flags &=
4177 ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
4178 } else {
4179 eloop_register_read_sock(drv->eapol_tx_sock,
4180 wpa_driver_nl80211_handle_eapol_tx_status,
4181 drv, NULL);
4182 }
4183 }
4184
4185 if (drv->global) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004186 dl_list_add(&drv->global->interfaces, &drv->list);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004187 drv->in_interface_list = 1;
4188 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004189
4190 return bss;
4191
4192failed:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004193 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004194 return NULL;
4195}
4196
4197
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004198/**
4199 * wpa_driver_nl80211_init - Initialize nl80211 driver interface
4200 * @ctx: context to be used when calling wpa_supplicant functions,
4201 * e.g., wpa_supplicant_event()
4202 * @ifname: interface name, e.g., wlan0
4203 * @global_priv: private driver global data from global_init()
4204 * Returns: Pointer to private data, %NULL on failure
4205 */
4206static void * wpa_driver_nl80211_init(void *ctx, const char *ifname,
4207 void *global_priv)
4208{
4209 return wpa_driver_nl80211_drv_init(ctx, ifname, global_priv, 0, NULL);
4210}
4211
4212
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004213static int nl80211_register_frame(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004214 struct nl_handle *nl_handle,
4215 u16 type, const u8 *match, size_t match_len)
4216{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004217 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004218 struct nl_msg *msg;
4219 int ret = -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004220 char buf[30];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004221
4222 msg = nlmsg_alloc();
4223 if (!msg)
4224 return -1;
4225
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004226 buf[0] = '\0';
4227 wpa_snprintf_hex(buf, sizeof(buf), match, match_len);
4228 wpa_printf(MSG_DEBUG, "nl80211: Register frame type=0x%x nl_handle=%p match=%s",
4229 type, nl_handle, buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004230
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004231 nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_ACTION);
4232
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004233 if (nl80211_set_iface_id(msg, bss) < 0)
4234 goto nla_put_failure;
4235
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004236 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE, type);
4237 NLA_PUT(msg, NL80211_ATTR_FRAME_MATCH, match_len, match);
4238
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004239 ret = send_and_recv(drv->global, nl_handle, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004240 msg = NULL;
4241 if (ret) {
4242 wpa_printf(MSG_DEBUG, "nl80211: Register frame command "
4243 "failed (type=%u): ret=%d (%s)",
4244 type, ret, strerror(-ret));
4245 wpa_hexdump(MSG_DEBUG, "nl80211: Register frame match",
4246 match, match_len);
4247 goto nla_put_failure;
4248 }
4249 ret = 0;
4250nla_put_failure:
4251 nlmsg_free(msg);
4252 return ret;
4253}
4254
4255
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004256static int nl80211_alloc_mgmt_handle(struct i802_bss *bss)
4257{
4258 struct wpa_driver_nl80211_data *drv = bss->drv;
4259
4260 if (bss->nl_mgmt) {
4261 wpa_printf(MSG_DEBUG, "nl80211: Mgmt reporting "
4262 "already on! (nl_mgmt=%p)", bss->nl_mgmt);
4263 return -1;
4264 }
4265
4266 bss->nl_mgmt = nl_create_handle(drv->nl_cb, "mgmt");
4267 if (bss->nl_mgmt == NULL)
4268 return -1;
4269
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004270 return 0;
4271}
4272
4273
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004274static void nl80211_mgmt_handle_register_eloop(struct i802_bss *bss)
4275{
4276 nl80211_register_eloop_read(&bss->nl_mgmt,
4277 wpa_driver_nl80211_event_receive,
4278 bss->nl_cb);
4279}
4280
4281
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004282static int nl80211_register_action_frame(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004283 const u8 *match, size_t match_len)
4284{
4285 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_ACTION << 4);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004286 return nl80211_register_frame(bss, bss->nl_mgmt,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004287 type, match, match_len);
4288}
4289
4290
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004291static int nl80211_mgmt_subscribe_non_ap(struct i802_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004292{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004293 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004294 int ret = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004295
4296 if (nl80211_alloc_mgmt_handle(bss))
4297 return -1;
4298 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with non-AP "
4299 "handle %p", bss->nl_mgmt);
4300
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004301 if (drv->nlmode == NL80211_IFTYPE_ADHOC) {
4302 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_AUTH << 4);
4303
4304 /* register for any AUTH message */
4305 nl80211_register_frame(bss, bss->nl_mgmt, type, NULL, 0);
4306 }
4307
Dmitry Shmidt051af732013-10-22 13:52:46 -07004308#ifdef CONFIG_INTERWORKING
4309 /* QoS Map Configure */
4310 if (nl80211_register_action_frame(bss, (u8 *) "\x01\x04", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004311 ret = -1;
Dmitry Shmidt051af732013-10-22 13:52:46 -07004312#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004313#if defined(CONFIG_P2P) || defined(CONFIG_INTERWORKING)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004314 /* GAS Initial Request */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004315 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0a", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004316 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004317 /* GAS Initial Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004318 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0b", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004319 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004320 /* GAS Comeback Request */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004321 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0c", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004322 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004323 /* GAS Comeback Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004324 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0d", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004325 ret = -1;
Dmitry Shmidt18463232014-01-24 12:29:41 -08004326 /* Protected GAS Initial Request */
4327 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0a", 2) < 0)
4328 ret = -1;
4329 /* Protected GAS Initial Response */
4330 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0b", 2) < 0)
4331 ret = -1;
4332 /* Protected GAS Comeback Request */
4333 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0c", 2) < 0)
4334 ret = -1;
4335 /* Protected GAS Comeback Response */
4336 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0d", 2) < 0)
4337 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004338#endif /* CONFIG_P2P || CONFIG_INTERWORKING */
4339#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004340 /* P2P Public Action */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004341 if (nl80211_register_action_frame(bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004342 (u8 *) "\x04\x09\x50\x6f\x9a\x09",
4343 6) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004344 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004345 /* P2P Action */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004346 if (nl80211_register_action_frame(bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004347 (u8 *) "\x7f\x50\x6f\x9a\x09",
4348 5) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004349 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004350#endif /* CONFIG_P2P */
4351#ifdef CONFIG_IEEE80211W
4352 /* SA Query Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004353 if (nl80211_register_action_frame(bss, (u8 *) "\x08\x01", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004354 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004355#endif /* CONFIG_IEEE80211W */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004356#ifdef CONFIG_TDLS
4357 if ((drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT)) {
4358 /* TDLS Discovery Response */
4359 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0e", 2) <
4360 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004361 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004362 }
4363#endif /* CONFIG_TDLS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004364
4365 /* FT Action frames */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004366 if (nl80211_register_action_frame(bss, (u8 *) "\x06", 1) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004367 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004368 else
4369 drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_FT |
4370 WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK;
4371
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004372 /* WNM - BSS Transition Management Request */
4373 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x07", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004374 ret = -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004375 /* WNM-Sleep Mode Response */
4376 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x11", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004377 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004378
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08004379#ifdef CONFIG_HS20
4380 /* WNM-Notification */
4381 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x1a", 2) < 0)
4382 return -1;
4383#endif /* CONFIG_HS20 */
4384
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004385 nl80211_mgmt_handle_register_eloop(bss);
4386
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004387 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004388}
4389
4390
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004391static int nl80211_register_spurious_class3(struct i802_bss *bss)
4392{
4393 struct wpa_driver_nl80211_data *drv = bss->drv;
4394 struct nl_msg *msg;
4395 int ret = -1;
4396
4397 msg = nlmsg_alloc();
4398 if (!msg)
4399 return -1;
4400
4401 nl80211_cmd(drv, msg, 0, NL80211_CMD_UNEXPECTED_FRAME);
4402
4403 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
4404
4405 ret = send_and_recv(drv->global, bss->nl_mgmt, msg, NULL, NULL);
4406 msg = NULL;
4407 if (ret) {
4408 wpa_printf(MSG_DEBUG, "nl80211: Register spurious class3 "
4409 "failed: ret=%d (%s)",
4410 ret, strerror(-ret));
4411 goto nla_put_failure;
4412 }
4413 ret = 0;
4414nla_put_failure:
4415 nlmsg_free(msg);
4416 return ret;
4417}
4418
4419
4420static int nl80211_mgmt_subscribe_ap(struct i802_bss *bss)
4421{
4422 static const int stypes[] = {
4423 WLAN_FC_STYPE_AUTH,
4424 WLAN_FC_STYPE_ASSOC_REQ,
4425 WLAN_FC_STYPE_REASSOC_REQ,
4426 WLAN_FC_STYPE_DISASSOC,
4427 WLAN_FC_STYPE_DEAUTH,
4428 WLAN_FC_STYPE_ACTION,
4429 WLAN_FC_STYPE_PROBE_REQ,
4430/* Beacon doesn't work as mac80211 doesn't currently allow
4431 * it, but it wouldn't really be the right thing anyway as
4432 * it isn't per interface ... maybe just dump the scan
4433 * results periodically for OLBC?
4434 */
4435// WLAN_FC_STYPE_BEACON,
4436 };
4437 unsigned int i;
4438
4439 if (nl80211_alloc_mgmt_handle(bss))
4440 return -1;
4441 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
4442 "handle %p", bss->nl_mgmt);
4443
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004444 for (i = 0; i < ARRAY_SIZE(stypes); i++) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004445 if (nl80211_register_frame(bss, bss->nl_mgmt,
4446 (WLAN_FC_TYPE_MGMT << 2) |
4447 (stypes[i] << 4),
4448 NULL, 0) < 0) {
4449 goto out_err;
4450 }
4451 }
4452
4453 if (nl80211_register_spurious_class3(bss))
4454 goto out_err;
4455
4456 if (nl80211_get_wiphy_data_ap(bss) == NULL)
4457 goto out_err;
4458
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004459 nl80211_mgmt_handle_register_eloop(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004460 return 0;
4461
4462out_err:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004463 nl_destroy_handles(&bss->nl_mgmt);
4464 return -1;
4465}
4466
4467
4468static int nl80211_mgmt_subscribe_ap_dev_sme(struct i802_bss *bss)
4469{
4470 if (nl80211_alloc_mgmt_handle(bss))
4471 return -1;
4472 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
4473 "handle %p (device SME)", bss->nl_mgmt);
4474
4475 if (nl80211_register_frame(bss, bss->nl_mgmt,
4476 (WLAN_FC_TYPE_MGMT << 2) |
4477 (WLAN_FC_STYPE_ACTION << 4),
4478 NULL, 0) < 0)
4479 goto out_err;
4480
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004481 nl80211_mgmt_handle_register_eloop(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004482 return 0;
4483
4484out_err:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004485 nl_destroy_handles(&bss->nl_mgmt);
4486 return -1;
4487}
4488
4489
4490static void nl80211_mgmt_unsubscribe(struct i802_bss *bss, const char *reason)
4491{
4492 if (bss->nl_mgmt == NULL)
4493 return;
4494 wpa_printf(MSG_DEBUG, "nl80211: Unsubscribe mgmt frames handle %p "
4495 "(%s)", bss->nl_mgmt, reason);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004496 nl80211_destroy_eloop_handle(&bss->nl_mgmt);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004497
4498 nl80211_put_wiphy_data_ap(bss);
4499}
4500
4501
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004502static void wpa_driver_nl80211_send_rfkill(void *eloop_ctx, void *timeout_ctx)
4503{
4504 wpa_supplicant_event(timeout_ctx, EVENT_INTERFACE_DISABLED, NULL);
4505}
4506
4507
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004508static void nl80211_del_p2pdev(struct i802_bss *bss)
4509{
4510 struct wpa_driver_nl80211_data *drv = bss->drv;
4511 struct nl_msg *msg;
4512 int ret;
4513
4514 msg = nlmsg_alloc();
4515 if (!msg)
4516 return;
4517
4518 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_INTERFACE);
4519 NLA_PUT_U64(msg, NL80211_ATTR_WDEV, bss->wdev_id);
4520
4521 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4522 msg = NULL;
4523
4524 wpa_printf(MSG_DEBUG, "nl80211: Delete P2P Device %s (0x%llx): %s",
4525 bss->ifname, (long long unsigned int) bss->wdev_id,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004526 strerror(-ret));
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004527
4528nla_put_failure:
4529 nlmsg_free(msg);
4530}
4531
4532
4533static int nl80211_set_p2pdev(struct i802_bss *bss, int start)
4534{
4535 struct wpa_driver_nl80211_data *drv = bss->drv;
4536 struct nl_msg *msg;
4537 int ret = -1;
4538
4539 msg = nlmsg_alloc();
4540 if (!msg)
4541 return -1;
4542
4543 if (start)
4544 nl80211_cmd(drv, msg, 0, NL80211_CMD_START_P2P_DEVICE);
4545 else
4546 nl80211_cmd(drv, msg, 0, NL80211_CMD_STOP_P2P_DEVICE);
4547
4548 NLA_PUT_U64(msg, NL80211_ATTR_WDEV, bss->wdev_id);
4549
4550 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4551 msg = NULL;
4552
4553 wpa_printf(MSG_DEBUG, "nl80211: %s P2P Device %s (0x%llx): %s",
4554 start ? "Start" : "Stop",
4555 bss->ifname, (long long unsigned int) bss->wdev_id,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004556 strerror(-ret));
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004557
4558nla_put_failure:
4559 nlmsg_free(msg);
4560 return ret;
4561}
4562
4563
4564static int i802_set_iface_flags(struct i802_bss *bss, int up)
4565{
4566 enum nl80211_iftype nlmode;
4567
4568 nlmode = nl80211_get_ifmode(bss);
4569 if (nlmode != NL80211_IFTYPE_P2P_DEVICE) {
4570 return linux_set_iface_flags(bss->drv->global->ioctl_sock,
4571 bss->ifname, up);
4572 }
4573
4574 /* P2P Device has start/stop which is equivalent */
4575 return nl80211_set_p2pdev(bss, up);
4576}
4577
4578
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004579static int
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004580wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv,
4581 const u8 *set_addr, int first)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004582{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004583 struct i802_bss *bss = drv->first_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004584 int send_rfkill_event = 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004585 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004586
4587 drv->ifindex = if_nametoindex(bss->ifname);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004588 bss->ifindex = drv->ifindex;
4589 bss->wdev_id = drv->global->if_add_wdevid;
4590 bss->wdev_id_set = drv->global->if_add_wdevid_set;
4591
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004592 bss->if_dynamic = drv->ifindex == drv->global->if_add_ifindex;
4593 bss->if_dynamic = bss->if_dynamic || drv->global->if_add_wdevid_set;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004594 drv->global->if_add_wdevid_set = 0;
4595
4596 if (wpa_driver_nl80211_capa(drv))
4597 return -1;
4598
4599 wpa_printf(MSG_DEBUG, "nl80211: interface %s in phy %s",
4600 bss->ifname, drv->phyname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004601
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004602 if (set_addr &&
4603 (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0) ||
4604 linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
4605 set_addr)))
4606 return -1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004607
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004608 if (first && nl80211_get_ifmode(bss) == NL80211_IFTYPE_AP)
4609 drv->start_mode_ap = 1;
4610
4611 if (drv->hostapd)
4612 nlmode = NL80211_IFTYPE_AP;
4613 else if (bss->if_dynamic)
4614 nlmode = nl80211_get_ifmode(bss);
4615 else
4616 nlmode = NL80211_IFTYPE_STATION;
4617
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004618 if (wpa_driver_nl80211_set_mode(bss, nlmode) < 0) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004619 wpa_printf(MSG_ERROR, "nl80211: Could not configure driver mode");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004620 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004621 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004622
4623 if (nlmode == NL80211_IFTYPE_P2P_DEVICE) {
4624 int ret = nl80211_set_p2pdev(bss, 1);
4625 if (ret < 0)
4626 wpa_printf(MSG_ERROR, "nl80211: Could not start P2P device");
4627 nl80211_get_macaddr(bss);
4628 return ret;
4629 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004630
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004631 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004632 if (rfkill_is_blocked(drv->rfkill)) {
4633 wpa_printf(MSG_DEBUG, "nl80211: Could not yet enable "
4634 "interface '%s' due to rfkill",
4635 bss->ifname);
4636 drv->if_disabled = 1;
4637 send_rfkill_event = 1;
4638 } else {
4639 wpa_printf(MSG_ERROR, "nl80211: Could not set "
4640 "interface '%s' UP", bss->ifname);
4641 return -1;
4642 }
4643 }
4644
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004645 if (!drv->hostapd)
4646 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
4647 1, IF_OPER_DORMANT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004648
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004649 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
4650 bss->addr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004651 return -1;
4652
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004653 if (send_rfkill_event) {
4654 eloop_register_timeout(0, 0, wpa_driver_nl80211_send_rfkill,
4655 drv, drv->ctx);
4656 }
4657
4658 return 0;
4659}
4660
4661
4662static int wpa_driver_nl80211_del_beacon(struct wpa_driver_nl80211_data *drv)
4663{
4664 struct nl_msg *msg;
4665
4666 msg = nlmsg_alloc();
4667 if (!msg)
4668 return -ENOMEM;
4669
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004670 wpa_printf(MSG_DEBUG, "nl80211: Remove beacon (ifindex=%d)",
4671 drv->ifindex);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004672 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_BEACON);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004673 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
4674
4675 return send_and_recv_msgs(drv, msg, NULL, NULL);
4676 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004677 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004678 return -ENOBUFS;
4679}
4680
4681
4682/**
4683 * wpa_driver_nl80211_deinit - Deinitialize nl80211 driver interface
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004684 * @bss: Pointer to private nl80211 data from wpa_driver_nl80211_init()
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004685 *
4686 * Shut down driver interface and processing of driver events. Free
4687 * private data buffer if one was allocated in wpa_driver_nl80211_init().
4688 */
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004689static void wpa_driver_nl80211_deinit(struct i802_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004690{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004691 struct wpa_driver_nl80211_data *drv = bss->drv;
4692
Dmitry Shmidt04949592012-07-19 12:16:46 -07004693 bss->in_deinit = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004694 if (drv->data_tx_status)
4695 eloop_unregister_read_sock(drv->eapol_tx_sock);
4696 if (drv->eapol_tx_sock >= 0)
4697 close(drv->eapol_tx_sock);
4698
4699 if (bss->nl_preq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004700 wpa_driver_nl80211_probe_req_report(bss, 0);
4701 if (bss->added_if_into_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004702 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
4703 bss->ifname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004704 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
4705 "interface %s from bridge %s: %s",
4706 bss->ifname, bss->brname, strerror(errno));
4707 }
4708 if (bss->added_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004709 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004710 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
4711 "bridge %s: %s",
4712 bss->brname, strerror(errno));
4713 }
4714
4715 nl80211_remove_monitor_interface(drv);
4716
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004717 if (is_ap_interface(drv->nlmode))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004718 wpa_driver_nl80211_del_beacon(drv);
4719
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004720 if (drv->eapol_sock >= 0) {
4721 eloop_unregister_read_sock(drv->eapol_sock);
4722 close(drv->eapol_sock);
4723 }
4724
4725 if (drv->if_indices != drv->default_if_indices)
4726 os_free(drv->if_indices);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004727
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004728 if (drv->disabled_11b_rates)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004729 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
4730
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004731 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, 0,
4732 IF_OPER_UP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004733 rfkill_deinit(drv->rfkill);
4734
4735 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
4736
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004737 if (!drv->start_iface_up)
4738 (void) i802_set_iface_flags(bss, 0);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004739 if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004740 if (!drv->hostapd || !drv->start_mode_ap)
4741 wpa_driver_nl80211_set_mode(bss,
4742 NL80211_IFTYPE_STATION);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004743 nl80211_mgmt_unsubscribe(bss, "deinit");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004744 } else {
4745 nl80211_mgmt_unsubscribe(bss, "deinit");
4746 nl80211_del_p2pdev(bss);
4747 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004748 nl_cb_put(drv->nl_cb);
4749
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004750 nl80211_destroy_bss(drv->first_bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004751
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004752 os_free(drv->filter_ssids);
4753
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004754 os_free(drv->auth_ie);
4755
4756 if (drv->in_interface_list)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004757 dl_list_del(&drv->list);
4758
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004759 os_free(drv->extended_capa);
4760 os_free(drv->extended_capa_mask);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004761 os_free(drv->first_bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004762 os_free(drv);
4763}
4764
4765
4766/**
4767 * wpa_driver_nl80211_scan_timeout - Scan timeout to report scan completion
4768 * @eloop_ctx: Driver private data
4769 * @timeout_ctx: ctx argument given to wpa_driver_nl80211_init()
4770 *
4771 * This function can be used as registered timeout when starting a scan to
4772 * generate a scan completed event if the driver does not report this.
4773 */
4774static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx, void *timeout_ctx)
4775{
4776 struct wpa_driver_nl80211_data *drv = eloop_ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004777 if (drv->ap_scan_as_station != NL80211_IFTYPE_UNSPECIFIED) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004778 wpa_driver_nl80211_set_mode(drv->first_bss,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004779 drv->ap_scan_as_station);
4780 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004781 }
4782 wpa_printf(MSG_DEBUG, "Scan timeout - try to get results");
4783 wpa_supplicant_event(timeout_ctx, EVENT_SCAN_RESULTS, NULL);
4784}
4785
4786
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004787static struct nl_msg *
4788nl80211_scan_common(struct wpa_driver_nl80211_data *drv, u8 cmd,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004789 struct wpa_driver_scan_params *params, u64 *wdev_id)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004790{
4791 struct nl_msg *msg;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004792 size_t i;
4793
4794 msg = nlmsg_alloc();
4795 if (!msg)
4796 return NULL;
4797
4798 nl80211_cmd(drv, msg, 0, cmd);
4799
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004800 if (!wdev_id)
4801 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
4802 else
4803 NLA_PUT_U64(msg, NL80211_ATTR_WDEV, *wdev_id);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004804
4805 if (params->num_ssids) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004806 struct nlattr *ssids;
4807
4808 ssids = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004809 if (ssids == NULL)
4810 goto fail;
4811 for (i = 0; i < params->num_ssids; i++) {
4812 wpa_hexdump_ascii(MSG_MSGDUMP, "nl80211: Scan SSID",
4813 params->ssids[i].ssid,
4814 params->ssids[i].ssid_len);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004815 if (nla_put(msg, i + 1, params->ssids[i].ssid_len,
4816 params->ssids[i].ssid) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004817 goto fail;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004818 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004819 nla_nest_end(msg, ssids);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004820 }
4821
4822 if (params->extra_ies) {
4823 wpa_hexdump(MSG_MSGDUMP, "nl80211: Scan extra IEs",
4824 params->extra_ies, params->extra_ies_len);
4825 if (nla_put(msg, NL80211_ATTR_IE, params->extra_ies_len,
4826 params->extra_ies) < 0)
4827 goto fail;
4828 }
4829
4830 if (params->freqs) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004831 struct nlattr *freqs;
4832 freqs = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004833 if (freqs == NULL)
4834 goto fail;
4835 for (i = 0; params->freqs[i]; i++) {
4836 wpa_printf(MSG_MSGDUMP, "nl80211: Scan frequency %u "
4837 "MHz", params->freqs[i]);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004838 if (nla_put_u32(msg, i + 1, params->freqs[i]) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004839 goto fail;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004840 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004841 nla_nest_end(msg, freqs);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004842 }
4843
4844 os_free(drv->filter_ssids);
4845 drv->filter_ssids = params->filter_ssids;
4846 params->filter_ssids = NULL;
4847 drv->num_filter_ssids = params->num_filter_ssids;
4848
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004849 if (params->only_new_results) {
4850 wpa_printf(MSG_DEBUG, "nl80211: Add NL80211_SCAN_FLAG_FLUSH");
4851 NLA_PUT_U32(msg, NL80211_ATTR_SCAN_FLAGS,
4852 NL80211_SCAN_FLAG_FLUSH);
4853 }
4854
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004855 return msg;
4856
4857fail:
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004858nla_put_failure:
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004859 nlmsg_free(msg);
4860 return NULL;
4861}
4862
4863
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004864/**
4865 * wpa_driver_nl80211_scan - Request the driver to initiate scan
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004866 * @bss: Pointer to private driver data from wpa_driver_nl80211_init()
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004867 * @params: Scan parameters
4868 * Returns: 0 on success, -1 on failure
4869 */
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004870static int wpa_driver_nl80211_scan(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004871 struct wpa_driver_scan_params *params)
4872{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004873 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004874 int ret = -1, timeout;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004875 struct nl_msg *msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004876
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004877 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: scan request");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004878 drv->scan_for_auth = 0;
4879
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004880 msg = nl80211_scan_common(drv, NL80211_CMD_TRIGGER_SCAN, params,
4881 bss->wdev_id_set ? &bss->wdev_id : NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004882 if (!msg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004883 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004884
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004885 if (params->p2p_probe) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004886 struct nlattr *rates;
4887
Dmitry Shmidt04949592012-07-19 12:16:46 -07004888 wpa_printf(MSG_DEBUG, "nl80211: P2P probe - mask SuppRates");
4889
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004890 rates = nla_nest_start(msg, NL80211_ATTR_SCAN_SUPP_RATES);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004891 if (rates == NULL)
4892 goto nla_put_failure;
4893
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004894 /*
4895 * Remove 2.4 GHz rates 1, 2, 5.5, 11 Mbps from supported rates
4896 * by masking out everything else apart from the OFDM rates 6,
4897 * 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS rates. All 5 GHz
4898 * rates are left enabled.
4899 */
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004900 NLA_PUT(msg, NL80211_BAND_2GHZ, 8,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004901 "\x0c\x12\x18\x24\x30\x48\x60\x6c");
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004902 nla_nest_end(msg, rates);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004903
4904 NLA_PUT_FLAG(msg, NL80211_ATTR_TX_NO_CCK_RATE);
4905 }
4906
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004907 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4908 msg = NULL;
4909 if (ret) {
4910 wpa_printf(MSG_DEBUG, "nl80211: Scan trigger failed: ret=%d "
4911 "(%s)", ret, strerror(-ret));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004912 if (drv->hostapd && is_ap_interface(drv->nlmode)) {
Dmitry Shmidted003d22014-02-06 10:09:12 -08004913 enum nl80211_iftype old_mode = drv->nlmode;
4914
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004915 /*
4916 * mac80211 does not allow scan requests in AP mode, so
4917 * try to do this in station mode.
4918 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004919 if (wpa_driver_nl80211_set_mode(
4920 bss, NL80211_IFTYPE_STATION))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004921 goto nla_put_failure;
4922
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004923 if (wpa_driver_nl80211_scan(bss, params)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004924 wpa_driver_nl80211_set_mode(bss, drv->nlmode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004925 goto nla_put_failure;
4926 }
4927
4928 /* Restore AP mode when processing scan results */
Dmitry Shmidted003d22014-02-06 10:09:12 -08004929 drv->ap_scan_as_station = old_mode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004930 ret = 0;
4931 } else
4932 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004933 }
4934
Dmitry Shmidt56052862013-10-04 10:23:25 -07004935 drv->scan_state = SCAN_REQUESTED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004936 /* Not all drivers generate "scan completed" wireless event, so try to
4937 * read results after a timeout. */
4938 timeout = 10;
4939 if (drv->scan_complete_events) {
4940 /*
4941 * The driver seems to deliver events to notify when scan is
4942 * complete, so use longer timeout to avoid race conditions
4943 * with scanning and following association request.
4944 */
4945 timeout = 30;
4946 }
4947 wpa_printf(MSG_DEBUG, "Scan requested (ret=%d) - scan timeout %d "
4948 "seconds", ret, timeout);
4949 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
4950 eloop_register_timeout(timeout, 0, wpa_driver_nl80211_scan_timeout,
4951 drv, drv->ctx);
4952
4953nla_put_failure:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004954 nlmsg_free(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004955 return ret;
4956}
4957
4958
4959/**
4960 * wpa_driver_nl80211_sched_scan - Initiate a scheduled scan
4961 * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
4962 * @params: Scan parameters
4963 * @interval: Interval between scan cycles in milliseconds
4964 * Returns: 0 on success, -1 on failure or if not supported
4965 */
4966static int wpa_driver_nl80211_sched_scan(void *priv,
4967 struct wpa_driver_scan_params *params,
4968 u32 interval)
4969{
4970 struct i802_bss *bss = priv;
4971 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004972 int ret = -1;
4973 struct nl_msg *msg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004974 size_t i;
4975
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004976 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: sched_scan request");
4977
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004978#ifdef ANDROID
4979 if (!drv->capa.sched_scan_supported)
4980 return android_pno_start(bss, params);
4981#endif /* ANDROID */
4982
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004983 msg = nl80211_scan_common(drv, NL80211_CMD_START_SCHED_SCAN, params,
4984 bss->wdev_id_set ? &bss->wdev_id : NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004985 if (!msg)
4986 goto nla_put_failure;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004987
4988 NLA_PUT_U32(msg, NL80211_ATTR_SCHED_SCAN_INTERVAL, interval);
4989
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004990 if ((drv->num_filter_ssids &&
4991 (int) drv->num_filter_ssids <= drv->capa.max_match_sets) ||
4992 params->filter_rssi) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004993 struct nlattr *match_sets;
4994 match_sets = nla_nest_start(msg, NL80211_ATTR_SCHED_SCAN_MATCH);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004995 if (match_sets == NULL)
4996 goto nla_put_failure;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004997
4998 for (i = 0; i < drv->num_filter_ssids; i++) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004999 struct nlattr *match_set_ssid;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005000 wpa_hexdump_ascii(MSG_MSGDUMP,
5001 "nl80211: Sched scan filter SSID",
5002 drv->filter_ssids[i].ssid,
5003 drv->filter_ssids[i].ssid_len);
5004
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005005 match_set_ssid = nla_nest_start(msg, i + 1);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005006 if (match_set_ssid == NULL)
5007 goto nla_put_failure;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005008 NLA_PUT(msg, NL80211_ATTR_SCHED_SCAN_MATCH_SSID,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005009 drv->filter_ssids[i].ssid_len,
5010 drv->filter_ssids[i].ssid);
Dmitry Shmidt97672262014-02-03 13:02:54 -08005011 if (params->filter_rssi)
5012 NLA_PUT_U32(msg,
5013 NL80211_SCHED_SCAN_MATCH_ATTR_RSSI,
5014 params->filter_rssi);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005015
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005016 nla_nest_end(msg, match_set_ssid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005017 }
5018
Dmitry Shmidt97672262014-02-03 13:02:54 -08005019 /*
5020 * Due to backward compatibility code, newer kernels treat this
5021 * matchset (with only an RSSI filter) as the default for all
5022 * other matchsets, unless it's the only one, in which case the
5023 * matchset will actually allow all SSIDs above the RSSI.
5024 */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005025 if (params->filter_rssi) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005026 struct nlattr *match_set_rssi;
5027 match_set_rssi = nla_nest_start(msg, 0);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005028 if (match_set_rssi == NULL)
5029 goto nla_put_failure;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005030 NLA_PUT_U32(msg, NL80211_SCHED_SCAN_MATCH_ATTR_RSSI,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005031 params->filter_rssi);
5032 wpa_printf(MSG_MSGDUMP,
5033 "nl80211: Sched scan RSSI filter %d dBm",
5034 params->filter_rssi);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005035 nla_nest_end(msg, match_set_rssi);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005036 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005037
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005038 nla_nest_end(msg, match_sets);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005039 }
5040
5041 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5042
5043 /* TODO: if we get an error here, we should fall back to normal scan */
5044
5045 msg = NULL;
5046 if (ret) {
5047 wpa_printf(MSG_DEBUG, "nl80211: Sched scan start failed: "
5048 "ret=%d (%s)", ret, strerror(-ret));
5049 goto nla_put_failure;
5050 }
5051
5052 wpa_printf(MSG_DEBUG, "nl80211: Sched scan requested (ret=%d) - "
5053 "scan interval %d msec", ret, interval);
5054
5055nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005056 nlmsg_free(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005057 return ret;
5058}
5059
5060
5061/**
5062 * wpa_driver_nl80211_stop_sched_scan - Stop a scheduled scan
5063 * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
5064 * Returns: 0 on success, -1 on failure or if not supported
5065 */
5066static int wpa_driver_nl80211_stop_sched_scan(void *priv)
5067{
5068 struct i802_bss *bss = priv;
5069 struct wpa_driver_nl80211_data *drv = bss->drv;
5070 int ret = 0;
5071 struct nl_msg *msg;
5072
5073#ifdef ANDROID
5074 if (!drv->capa.sched_scan_supported)
5075 return android_pno_stop(bss);
5076#endif /* ANDROID */
5077
5078 msg = nlmsg_alloc();
5079 if (!msg)
5080 return -1;
5081
5082 nl80211_cmd(drv, msg, 0, NL80211_CMD_STOP_SCHED_SCAN);
5083
5084 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
5085
5086 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5087 msg = NULL;
5088 if (ret) {
5089 wpa_printf(MSG_DEBUG, "nl80211: Sched scan stop failed: "
5090 "ret=%d (%s)", ret, strerror(-ret));
5091 goto nla_put_failure;
5092 }
5093
5094 wpa_printf(MSG_DEBUG, "nl80211: Sched scan stop sent (ret=%d)", ret);
5095
5096nla_put_failure:
5097 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005098 return ret;
5099}
5100
5101
5102static const u8 * nl80211_get_ie(const u8 *ies, size_t ies_len, u8 ie)
5103{
5104 const u8 *end, *pos;
5105
5106 if (ies == NULL)
5107 return NULL;
5108
5109 pos = ies;
5110 end = ies + ies_len;
5111
5112 while (pos + 1 < end) {
5113 if (pos + 2 + pos[1] > end)
5114 break;
5115 if (pos[0] == ie)
5116 return pos;
5117 pos += 2 + pos[1];
5118 }
5119
5120 return NULL;
5121}
5122
5123
5124static int nl80211_scan_filtered(struct wpa_driver_nl80211_data *drv,
5125 const u8 *ie, size_t ie_len)
5126{
5127 const u8 *ssid;
5128 size_t i;
5129
5130 if (drv->filter_ssids == NULL)
5131 return 0;
5132
5133 ssid = nl80211_get_ie(ie, ie_len, WLAN_EID_SSID);
5134 if (ssid == NULL)
5135 return 1;
5136
5137 for (i = 0; i < drv->num_filter_ssids; i++) {
5138 if (ssid[1] == drv->filter_ssids[i].ssid_len &&
5139 os_memcmp(ssid + 2, drv->filter_ssids[i].ssid, ssid[1]) ==
5140 0)
5141 return 0;
5142 }
5143
5144 return 1;
5145}
5146
5147
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005148static int bss_info_handler(struct nl_msg *msg, void *arg)
5149{
5150 struct nlattr *tb[NL80211_ATTR_MAX + 1];
5151 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
5152 struct nlattr *bss[NL80211_BSS_MAX + 1];
5153 static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
5154 [NL80211_BSS_BSSID] = { .type = NLA_UNSPEC },
5155 [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
5156 [NL80211_BSS_TSF] = { .type = NLA_U64 },
5157 [NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 },
5158 [NL80211_BSS_CAPABILITY] = { .type = NLA_U16 },
5159 [NL80211_BSS_INFORMATION_ELEMENTS] = { .type = NLA_UNSPEC },
5160 [NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 },
5161 [NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 },
5162 [NL80211_BSS_STATUS] = { .type = NLA_U32 },
5163 [NL80211_BSS_SEEN_MS_AGO] = { .type = NLA_U32 },
5164 [NL80211_BSS_BEACON_IES] = { .type = NLA_UNSPEC },
5165 };
5166 struct nl80211_bss_info_arg *_arg = arg;
5167 struct wpa_scan_results *res = _arg->res;
5168 struct wpa_scan_res **tmp;
5169 struct wpa_scan_res *r;
5170 const u8 *ie, *beacon_ie;
5171 size_t ie_len, beacon_ie_len;
5172 u8 *pos;
Jouni Malinen87fd2792011-05-16 18:35:42 +03005173 size_t i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005174
5175 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
5176 genlmsg_attrlen(gnlh, 0), NULL);
5177 if (!tb[NL80211_ATTR_BSS])
5178 return NL_SKIP;
5179 if (nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS],
5180 bss_policy))
5181 return NL_SKIP;
Jouni Malinen87fd2792011-05-16 18:35:42 +03005182 if (bss[NL80211_BSS_STATUS]) {
5183 enum nl80211_bss_status status;
5184 status = nla_get_u32(bss[NL80211_BSS_STATUS]);
5185 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
5186 bss[NL80211_BSS_FREQUENCY]) {
5187 _arg->assoc_freq =
5188 nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
5189 wpa_printf(MSG_DEBUG, "nl80211: Associated on %u MHz",
5190 _arg->assoc_freq);
5191 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005192 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
5193 bss[NL80211_BSS_BSSID]) {
5194 os_memcpy(_arg->assoc_bssid,
5195 nla_data(bss[NL80211_BSS_BSSID]), ETH_ALEN);
5196 wpa_printf(MSG_DEBUG, "nl80211: Associated with "
5197 MACSTR, MAC2STR(_arg->assoc_bssid));
5198 }
Jouni Malinen87fd2792011-05-16 18:35:42 +03005199 }
5200 if (!res)
5201 return NL_SKIP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005202 if (bss[NL80211_BSS_INFORMATION_ELEMENTS]) {
5203 ie = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
5204 ie_len = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
5205 } else {
5206 ie = NULL;
5207 ie_len = 0;
5208 }
5209 if (bss[NL80211_BSS_BEACON_IES]) {
5210 beacon_ie = nla_data(bss[NL80211_BSS_BEACON_IES]);
5211 beacon_ie_len = nla_len(bss[NL80211_BSS_BEACON_IES]);
5212 } else {
5213 beacon_ie = NULL;
5214 beacon_ie_len = 0;
5215 }
5216
5217 if (nl80211_scan_filtered(_arg->drv, ie ? ie : beacon_ie,
5218 ie ? ie_len : beacon_ie_len))
5219 return NL_SKIP;
5220
5221 r = os_zalloc(sizeof(*r) + ie_len + beacon_ie_len);
5222 if (r == NULL)
5223 return NL_SKIP;
5224 if (bss[NL80211_BSS_BSSID])
5225 os_memcpy(r->bssid, nla_data(bss[NL80211_BSS_BSSID]),
5226 ETH_ALEN);
5227 if (bss[NL80211_BSS_FREQUENCY])
5228 r->freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
5229 if (bss[NL80211_BSS_BEACON_INTERVAL])
5230 r->beacon_int = nla_get_u16(bss[NL80211_BSS_BEACON_INTERVAL]);
5231 if (bss[NL80211_BSS_CAPABILITY])
5232 r->caps = nla_get_u16(bss[NL80211_BSS_CAPABILITY]);
5233 r->flags |= WPA_SCAN_NOISE_INVALID;
5234 if (bss[NL80211_BSS_SIGNAL_MBM]) {
5235 r->level = nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]);
5236 r->level /= 100; /* mBm to dBm */
5237 r->flags |= WPA_SCAN_LEVEL_DBM | WPA_SCAN_QUAL_INVALID;
5238 } else if (bss[NL80211_BSS_SIGNAL_UNSPEC]) {
5239 r->level = nla_get_u8(bss[NL80211_BSS_SIGNAL_UNSPEC]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005240 r->flags |= WPA_SCAN_QUAL_INVALID;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005241 } else
5242 r->flags |= WPA_SCAN_LEVEL_INVALID | WPA_SCAN_QUAL_INVALID;
5243 if (bss[NL80211_BSS_TSF])
5244 r->tsf = nla_get_u64(bss[NL80211_BSS_TSF]);
5245 if (bss[NL80211_BSS_SEEN_MS_AGO])
5246 r->age = nla_get_u32(bss[NL80211_BSS_SEEN_MS_AGO]);
5247 r->ie_len = ie_len;
5248 pos = (u8 *) (r + 1);
5249 if (ie) {
5250 os_memcpy(pos, ie, ie_len);
5251 pos += ie_len;
5252 }
5253 r->beacon_ie_len = beacon_ie_len;
5254 if (beacon_ie)
5255 os_memcpy(pos, beacon_ie, beacon_ie_len);
5256
5257 if (bss[NL80211_BSS_STATUS]) {
5258 enum nl80211_bss_status status;
5259 status = nla_get_u32(bss[NL80211_BSS_STATUS]);
5260 switch (status) {
5261 case NL80211_BSS_STATUS_AUTHENTICATED:
5262 r->flags |= WPA_SCAN_AUTHENTICATED;
5263 break;
5264 case NL80211_BSS_STATUS_ASSOCIATED:
5265 r->flags |= WPA_SCAN_ASSOCIATED;
5266 break;
5267 default:
5268 break;
5269 }
5270 }
5271
Jouni Malinen87fd2792011-05-16 18:35:42 +03005272 /*
5273 * cfg80211 maintains separate BSS table entries for APs if the same
5274 * BSSID,SSID pair is seen on multiple channels. wpa_supplicant does
5275 * not use frequency as a separate key in the BSS table, so filter out
5276 * duplicated entries. Prefer associated BSS entry in such a case in
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005277 * order to get the correct frequency into the BSS table. Similarly,
5278 * prefer newer entries over older.
Jouni Malinen87fd2792011-05-16 18:35:42 +03005279 */
5280 for (i = 0; i < res->num; i++) {
5281 const u8 *s1, *s2;
5282 if (os_memcmp(res->res[i]->bssid, r->bssid, ETH_ALEN) != 0)
5283 continue;
5284
5285 s1 = nl80211_get_ie((u8 *) (res->res[i] + 1),
5286 res->res[i]->ie_len, WLAN_EID_SSID);
5287 s2 = nl80211_get_ie((u8 *) (r + 1), r->ie_len, WLAN_EID_SSID);
5288 if (s1 == NULL || s2 == NULL || s1[1] != s2[1] ||
5289 os_memcmp(s1, s2, 2 + s1[1]) != 0)
5290 continue;
5291
5292 /* Same BSSID,SSID was already included in scan results */
5293 wpa_printf(MSG_DEBUG, "nl80211: Remove duplicated scan result "
5294 "for " MACSTR, MAC2STR(r->bssid));
5295
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005296 if (((r->flags & WPA_SCAN_ASSOCIATED) &&
5297 !(res->res[i]->flags & WPA_SCAN_ASSOCIATED)) ||
5298 r->age < res->res[i]->age) {
Jouni Malinen87fd2792011-05-16 18:35:42 +03005299 os_free(res->res[i]);
5300 res->res[i] = r;
5301 } else
5302 os_free(r);
5303 return NL_SKIP;
5304 }
5305
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005306 tmp = os_realloc_array(res->res, res->num + 1,
5307 sizeof(struct wpa_scan_res *));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005308 if (tmp == NULL) {
5309 os_free(r);
5310 return NL_SKIP;
5311 }
5312 tmp[res->num++] = r;
5313 res->res = tmp;
5314
5315 return NL_SKIP;
5316}
5317
5318
5319static void clear_state_mismatch(struct wpa_driver_nl80211_data *drv,
5320 const u8 *addr)
5321{
5322 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
5323 wpa_printf(MSG_DEBUG, "nl80211: Clear possible state "
5324 "mismatch (" MACSTR ")", MAC2STR(addr));
5325 wpa_driver_nl80211_mlme(drv, addr,
5326 NL80211_CMD_DEAUTHENTICATE,
5327 WLAN_REASON_PREV_AUTH_NOT_VALID, 1);
5328 }
5329}
5330
5331
5332static void wpa_driver_nl80211_check_bss_status(
5333 struct wpa_driver_nl80211_data *drv, struct wpa_scan_results *res)
5334{
5335 size_t i;
5336
5337 for (i = 0; i < res->num; i++) {
5338 struct wpa_scan_res *r = res->res[i];
5339 if (r->flags & WPA_SCAN_AUTHENTICATED) {
5340 wpa_printf(MSG_DEBUG, "nl80211: Scan results "
5341 "indicates BSS status with " MACSTR
5342 " as authenticated",
5343 MAC2STR(r->bssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005344 if (is_sta_interface(drv->nlmode) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005345 os_memcmp(r->bssid, drv->bssid, ETH_ALEN) != 0 &&
5346 os_memcmp(r->bssid, drv->auth_bssid, ETH_ALEN) !=
5347 0) {
5348 wpa_printf(MSG_DEBUG, "nl80211: Unknown BSSID"
5349 " in local state (auth=" MACSTR
5350 " assoc=" MACSTR ")",
5351 MAC2STR(drv->auth_bssid),
5352 MAC2STR(drv->bssid));
5353 clear_state_mismatch(drv, r->bssid);
5354 }
5355 }
5356
5357 if (r->flags & WPA_SCAN_ASSOCIATED) {
5358 wpa_printf(MSG_DEBUG, "nl80211: Scan results "
5359 "indicate BSS status with " MACSTR
5360 " as associated",
5361 MAC2STR(r->bssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005362 if (is_sta_interface(drv->nlmode) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005363 !drv->associated) {
5364 wpa_printf(MSG_DEBUG, "nl80211: Local state "
5365 "(not associated) does not match "
5366 "with BSS state");
5367 clear_state_mismatch(drv, r->bssid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005368 } else if (is_sta_interface(drv->nlmode) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005369 os_memcmp(drv->bssid, r->bssid, ETH_ALEN) !=
5370 0) {
5371 wpa_printf(MSG_DEBUG, "nl80211: Local state "
5372 "(associated with " MACSTR ") does "
5373 "not match with BSS state",
5374 MAC2STR(drv->bssid));
5375 clear_state_mismatch(drv, r->bssid);
5376 clear_state_mismatch(drv, drv->bssid);
5377 }
5378 }
5379 }
5380}
5381
5382
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005383static struct wpa_scan_results *
5384nl80211_get_scan_results(struct wpa_driver_nl80211_data *drv)
5385{
5386 struct nl_msg *msg;
5387 struct wpa_scan_results *res;
5388 int ret;
5389 struct nl80211_bss_info_arg arg;
5390
5391 res = os_zalloc(sizeof(*res));
5392 if (res == NULL)
5393 return NULL;
5394 msg = nlmsg_alloc();
5395 if (!msg)
5396 goto nla_put_failure;
5397
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005398 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005399 if (nl80211_set_iface_id(msg, drv->first_bss) < 0)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005400 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005401
5402 arg.drv = drv;
5403 arg.res = res;
5404 ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
5405 msg = NULL;
5406 if (ret == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005407 wpa_printf(MSG_DEBUG, "nl80211: Received scan results (%lu "
5408 "BSSes)", (unsigned long) res->num);
5409 nl80211_get_noise_for_scan_results(drv, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005410 return res;
5411 }
5412 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
5413 "(%s)", ret, strerror(-ret));
5414nla_put_failure:
5415 nlmsg_free(msg);
5416 wpa_scan_results_free(res);
5417 return NULL;
5418}
5419
5420
5421/**
5422 * wpa_driver_nl80211_get_scan_results - Fetch the latest scan results
5423 * @priv: Pointer to private wext data from wpa_driver_nl80211_init()
5424 * Returns: Scan results on success, -1 on failure
5425 */
5426static struct wpa_scan_results *
5427wpa_driver_nl80211_get_scan_results(void *priv)
5428{
5429 struct i802_bss *bss = priv;
5430 struct wpa_driver_nl80211_data *drv = bss->drv;
5431 struct wpa_scan_results *res;
5432
5433 res = nl80211_get_scan_results(drv);
5434 if (res)
5435 wpa_driver_nl80211_check_bss_status(drv, res);
5436 return res;
5437}
5438
5439
5440static void nl80211_dump_scan(struct wpa_driver_nl80211_data *drv)
5441{
5442 struct wpa_scan_results *res;
5443 size_t i;
5444
5445 res = nl80211_get_scan_results(drv);
5446 if (res == NULL) {
5447 wpa_printf(MSG_DEBUG, "nl80211: Failed to get scan results");
5448 return;
5449 }
5450
5451 wpa_printf(MSG_DEBUG, "nl80211: Scan result dump");
5452 for (i = 0; i < res->num; i++) {
5453 struct wpa_scan_res *r = res->res[i];
5454 wpa_printf(MSG_DEBUG, "nl80211: %d/%d " MACSTR "%s%s",
5455 (int) i, (int) res->num, MAC2STR(r->bssid),
5456 r->flags & WPA_SCAN_AUTHENTICATED ? " [auth]" : "",
5457 r->flags & WPA_SCAN_ASSOCIATED ? " [assoc]" : "");
5458 }
5459
5460 wpa_scan_results_free(res);
5461}
5462
5463
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005464static u32 wpa_alg_to_cipher_suite(enum wpa_alg alg, size_t key_len)
5465{
5466 switch (alg) {
5467 case WPA_ALG_WEP:
5468 if (key_len == 5)
5469 return WLAN_CIPHER_SUITE_WEP40;
5470 return WLAN_CIPHER_SUITE_WEP104;
5471 case WPA_ALG_TKIP:
5472 return WLAN_CIPHER_SUITE_TKIP;
5473 case WPA_ALG_CCMP:
5474 return WLAN_CIPHER_SUITE_CCMP;
5475 case WPA_ALG_GCMP:
5476 return WLAN_CIPHER_SUITE_GCMP;
5477 case WPA_ALG_CCMP_256:
5478 return WLAN_CIPHER_SUITE_CCMP_256;
5479 case WPA_ALG_GCMP_256:
5480 return WLAN_CIPHER_SUITE_GCMP_256;
5481 case WPA_ALG_IGTK:
5482 return WLAN_CIPHER_SUITE_AES_CMAC;
5483 case WPA_ALG_BIP_GMAC_128:
5484 return WLAN_CIPHER_SUITE_BIP_GMAC_128;
5485 case WPA_ALG_BIP_GMAC_256:
5486 return WLAN_CIPHER_SUITE_BIP_GMAC_256;
5487 case WPA_ALG_BIP_CMAC_256:
5488 return WLAN_CIPHER_SUITE_BIP_CMAC_256;
5489 case WPA_ALG_SMS4:
5490 return WLAN_CIPHER_SUITE_SMS4;
5491 case WPA_ALG_KRK:
5492 return WLAN_CIPHER_SUITE_KRK;
5493 case WPA_ALG_NONE:
5494 case WPA_ALG_PMK:
5495 wpa_printf(MSG_ERROR, "nl80211: Unexpected encryption algorithm %d",
5496 alg);
5497 return 0;
5498 }
5499
5500 wpa_printf(MSG_ERROR, "nl80211: Unsupported encryption algorithm %d",
5501 alg);
5502 return 0;
5503}
5504
5505
5506static u32 wpa_cipher_to_cipher_suite(unsigned int cipher)
5507{
5508 switch (cipher) {
5509 case WPA_CIPHER_CCMP_256:
5510 return WLAN_CIPHER_SUITE_CCMP_256;
5511 case WPA_CIPHER_GCMP_256:
5512 return WLAN_CIPHER_SUITE_GCMP_256;
5513 case WPA_CIPHER_CCMP:
5514 return WLAN_CIPHER_SUITE_CCMP;
5515 case WPA_CIPHER_GCMP:
5516 return WLAN_CIPHER_SUITE_GCMP;
5517 case WPA_CIPHER_TKIP:
5518 return WLAN_CIPHER_SUITE_TKIP;
5519 case WPA_CIPHER_WEP104:
5520 return WLAN_CIPHER_SUITE_WEP104;
5521 case WPA_CIPHER_WEP40:
5522 return WLAN_CIPHER_SUITE_WEP40;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08005523 case WPA_CIPHER_GTK_NOT_USED:
5524 return WLAN_CIPHER_SUITE_NO_GROUP_ADDR;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005525 }
5526
5527 return 0;
5528}
5529
5530
5531static int wpa_cipher_to_cipher_suites(unsigned int ciphers, u32 suites[],
5532 int max_suites)
5533{
5534 int num_suites = 0;
5535
5536 if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP_256)
5537 suites[num_suites++] = WLAN_CIPHER_SUITE_CCMP_256;
5538 if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP_256)
5539 suites[num_suites++] = WLAN_CIPHER_SUITE_GCMP_256;
5540 if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP)
5541 suites[num_suites++] = WLAN_CIPHER_SUITE_CCMP;
5542 if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP)
5543 suites[num_suites++] = WLAN_CIPHER_SUITE_GCMP;
5544 if (num_suites < max_suites && ciphers & WPA_CIPHER_TKIP)
5545 suites[num_suites++] = WLAN_CIPHER_SUITE_TKIP;
5546 if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP104)
5547 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP104;
5548 if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP40)
5549 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP40;
5550
5551 return num_suites;
5552}
5553
5554
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005555static int wpa_driver_nl80211_set_key(const char *ifname, struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005556 enum wpa_alg alg, const u8 *addr,
5557 int key_idx, int set_tx,
5558 const u8 *seq, size_t seq_len,
5559 const u8 *key, size_t key_len)
5560{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005561 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005562 int ifindex;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005563 struct nl_msg *msg;
5564 int ret;
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07005565 int tdls = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005566
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005567 /* Ignore for P2P Device */
5568 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
5569 return 0;
5570
5571 ifindex = if_nametoindex(ifname);
5572 wpa_printf(MSG_DEBUG, "%s: ifindex=%d (%s) alg=%d addr=%p key_idx=%d "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005573 "set_tx=%d seq_len=%lu key_len=%lu",
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005574 __func__, ifindex, ifname, alg, addr, key_idx, set_tx,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005575 (unsigned long) seq_len, (unsigned long) key_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005576#ifdef CONFIG_TDLS
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07005577 if (key_idx == -1) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005578 key_idx = 0;
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07005579 tdls = 1;
5580 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005581#endif /* CONFIG_TDLS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005582
5583 msg = nlmsg_alloc();
5584 if (!msg)
5585 return -ENOMEM;
5586
5587 if (alg == WPA_ALG_NONE) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005588 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_KEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005589 } else {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005590 nl80211_cmd(drv, msg, 0, NL80211_CMD_NEW_KEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005591 NLA_PUT(msg, NL80211_ATTR_KEY_DATA, key_len, key);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005592 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
5593 wpa_alg_to_cipher_suite(alg, key_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005594 }
5595
5596 if (seq && seq_len)
5597 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, seq_len, seq);
5598
5599 if (addr && !is_broadcast_ether_addr(addr)) {
5600 wpa_printf(MSG_DEBUG, " addr=" MACSTR, MAC2STR(addr));
5601 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
5602
5603 if (alg != WPA_ALG_WEP && key_idx && !set_tx) {
5604 wpa_printf(MSG_DEBUG, " RSN IBSS RX GTK");
5605 NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE,
5606 NL80211_KEYTYPE_GROUP);
5607 }
5608 } else if (addr && is_broadcast_ether_addr(addr)) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005609 struct nlattr *types;
5610
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005611 wpa_printf(MSG_DEBUG, " broadcast key");
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005612
5613 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005614 if (!types)
5615 goto nla_put_failure;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005616 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST);
5617 nla_nest_end(msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005618 }
5619 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
5620 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
5621
5622 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5623 if ((ret == -ENOENT || ret == -ENOLINK) && alg == WPA_ALG_NONE)
5624 ret = 0;
5625 if (ret)
5626 wpa_printf(MSG_DEBUG, "nl80211: set_key failed; err=%d %s)",
5627 ret, strerror(-ret));
5628
5629 /*
5630 * If we failed or don't need to set the default TX key (below),
5631 * we're done here.
5632 */
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07005633 if (ret || !set_tx || alg == WPA_ALG_NONE || tdls)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005634 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005635 if (is_ap_interface(drv->nlmode) && addr &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005636 !is_broadcast_ether_addr(addr))
5637 return ret;
5638
5639 msg = nlmsg_alloc();
5640 if (!msg)
5641 return -ENOMEM;
5642
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005643 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_KEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005644 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
5645 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
5646 if (alg == WPA_ALG_IGTK)
5647 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT_MGMT);
5648 else
5649 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT);
5650 if (addr && is_broadcast_ether_addr(addr)) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005651 struct nlattr *types;
5652
5653 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005654 if (!types)
5655 goto nla_put_failure;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005656 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST);
5657 nla_nest_end(msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005658 } else if (addr) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005659 struct nlattr *types;
5660
5661 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005662 if (!types)
5663 goto nla_put_failure;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005664 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_UNICAST);
5665 nla_nest_end(msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005666 }
5667
5668 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5669 if (ret == -ENOENT)
5670 ret = 0;
5671 if (ret)
5672 wpa_printf(MSG_DEBUG, "nl80211: set_key default failed; "
5673 "err=%d %s)", ret, strerror(-ret));
5674 return ret;
5675
5676nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005677 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005678 return -ENOBUFS;
5679}
5680
5681
5682static int nl_add_key(struct nl_msg *msg, enum wpa_alg alg,
5683 int key_idx, int defkey,
5684 const u8 *seq, size_t seq_len,
5685 const u8 *key, size_t key_len)
5686{
5687 struct nlattr *key_attr = nla_nest_start(msg, NL80211_ATTR_KEY);
5688 if (!key_attr)
5689 return -1;
5690
5691 if (defkey && alg == WPA_ALG_IGTK)
5692 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_MGMT);
5693 else if (defkey)
5694 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
5695
5696 NLA_PUT_U8(msg, NL80211_KEY_IDX, key_idx);
5697
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005698 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
5699 wpa_alg_to_cipher_suite(alg, key_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005700
5701 if (seq && seq_len)
5702 NLA_PUT(msg, NL80211_KEY_SEQ, seq_len, seq);
5703
5704 NLA_PUT(msg, NL80211_KEY_DATA, key_len, key);
5705
5706 nla_nest_end(msg, key_attr);
5707
5708 return 0;
5709 nla_put_failure:
5710 return -1;
5711}
5712
5713
5714static int nl80211_set_conn_keys(struct wpa_driver_associate_params *params,
5715 struct nl_msg *msg)
5716{
5717 int i, privacy = 0;
5718 struct nlattr *nl_keys, *nl_key;
5719
5720 for (i = 0; i < 4; i++) {
5721 if (!params->wep_key[i])
5722 continue;
5723 privacy = 1;
5724 break;
5725 }
5726 if (params->wps == WPS_MODE_PRIVACY)
5727 privacy = 1;
5728 if (params->pairwise_suite &&
5729 params->pairwise_suite != WPA_CIPHER_NONE)
5730 privacy = 1;
5731
5732 if (!privacy)
5733 return 0;
5734
5735 NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
5736
5737 nl_keys = nla_nest_start(msg, NL80211_ATTR_KEYS);
5738 if (!nl_keys)
5739 goto nla_put_failure;
5740
5741 for (i = 0; i < 4; i++) {
5742 if (!params->wep_key[i])
5743 continue;
5744
5745 nl_key = nla_nest_start(msg, i);
5746 if (!nl_key)
5747 goto nla_put_failure;
5748
5749 NLA_PUT(msg, NL80211_KEY_DATA, params->wep_key_len[i],
5750 params->wep_key[i]);
5751 if (params->wep_key_len[i] == 5)
5752 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
5753 WLAN_CIPHER_SUITE_WEP40);
5754 else
5755 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
5756 WLAN_CIPHER_SUITE_WEP104);
5757
5758 NLA_PUT_U8(msg, NL80211_KEY_IDX, i);
5759
5760 if (i == params->wep_tx_keyidx)
5761 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
5762
5763 nla_nest_end(msg, nl_key);
5764 }
5765 nla_nest_end(msg, nl_keys);
5766
5767 return 0;
5768
5769nla_put_failure:
5770 return -ENOBUFS;
5771}
5772
5773
5774static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
5775 const u8 *addr, int cmd, u16 reason_code,
5776 int local_state_change)
5777{
5778 int ret = -1;
5779 struct nl_msg *msg;
5780
5781 msg = nlmsg_alloc();
5782 if (!msg)
5783 return -1;
5784
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005785 nl80211_cmd(drv, msg, 0, cmd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005786
5787 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
5788 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason_code);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005789 if (addr)
5790 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005791 if (local_state_change)
5792 NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE);
5793
5794 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5795 msg = NULL;
5796 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005797 wpa_dbg(drv->ctx, MSG_DEBUG,
5798 "nl80211: MLME command failed: reason=%u ret=%d (%s)",
5799 reason_code, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005800 goto nla_put_failure;
5801 }
5802 ret = 0;
5803
5804nla_put_failure:
5805 nlmsg_free(msg);
5806 return ret;
5807}
5808
5809
5810static int wpa_driver_nl80211_disconnect(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005811 int reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005812{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005813 int ret;
5814
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005815 wpa_printf(MSG_DEBUG, "%s(reason_code=%d)", __func__, reason_code);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005816 nl80211_mark_disconnected(drv);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005817 /* Disconnect command doesn't need BSSID - it uses cached value */
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005818 ret = wpa_driver_nl80211_mlme(drv, NULL, NL80211_CMD_DISCONNECT,
5819 reason_code, 0);
5820 /*
5821 * For locally generated disconnect, supplicant already generates a
5822 * DEAUTH event, so ignore the event from NL80211.
5823 */
5824 drv->ignore_next_local_disconnect = ret == 0;
5825
5826 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005827}
5828
5829
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005830static int wpa_driver_nl80211_deauthenticate(struct i802_bss *bss,
5831 const u8 *addr, int reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005832{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005833 struct wpa_driver_nl80211_data *drv = bss->drv;
5834 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME))
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005835 return wpa_driver_nl80211_disconnect(drv, reason_code);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005836 wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " reason_code=%d)",
5837 __func__, MAC2STR(addr), reason_code);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005838 nl80211_mark_disconnected(drv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005839 if (drv->nlmode == NL80211_IFTYPE_ADHOC)
5840 return nl80211_leave_ibss(drv);
5841 return wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DEAUTHENTICATE,
5842 reason_code, 0);
5843}
5844
5845
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005846static void nl80211_copy_auth_params(struct wpa_driver_nl80211_data *drv,
5847 struct wpa_driver_auth_params *params)
5848{
5849 int i;
5850
5851 drv->auth_freq = params->freq;
5852 drv->auth_alg = params->auth_alg;
5853 drv->auth_wep_tx_keyidx = params->wep_tx_keyidx;
5854 drv->auth_local_state_change = params->local_state_change;
5855 drv->auth_p2p = params->p2p;
5856
5857 if (params->bssid)
5858 os_memcpy(drv->auth_bssid_, params->bssid, ETH_ALEN);
5859 else
5860 os_memset(drv->auth_bssid_, 0, ETH_ALEN);
5861
5862 if (params->ssid) {
5863 os_memcpy(drv->auth_ssid, params->ssid, params->ssid_len);
5864 drv->auth_ssid_len = params->ssid_len;
5865 } else
5866 drv->auth_ssid_len = 0;
5867
5868
5869 os_free(drv->auth_ie);
5870 drv->auth_ie = NULL;
5871 drv->auth_ie_len = 0;
5872 if (params->ie) {
5873 drv->auth_ie = os_malloc(params->ie_len);
5874 if (drv->auth_ie) {
5875 os_memcpy(drv->auth_ie, params->ie, params->ie_len);
5876 drv->auth_ie_len = params->ie_len;
5877 }
5878 }
5879
5880 for (i = 0; i < 4; i++) {
5881 if (params->wep_key[i] && params->wep_key_len[i] &&
5882 params->wep_key_len[i] <= 16) {
5883 os_memcpy(drv->auth_wep_key[i], params->wep_key[i],
5884 params->wep_key_len[i]);
5885 drv->auth_wep_key_len[i] = params->wep_key_len[i];
5886 } else
5887 drv->auth_wep_key_len[i] = 0;
5888 }
5889}
5890
5891
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005892static int wpa_driver_nl80211_authenticate(
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005893 struct i802_bss *bss, struct wpa_driver_auth_params *params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005894{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005895 struct wpa_driver_nl80211_data *drv = bss->drv;
5896 int ret = -1, i;
5897 struct nl_msg *msg;
5898 enum nl80211_auth_type type;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005899 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005900 int count = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005901 int is_retry;
5902
5903 is_retry = drv->retry_auth;
5904 drv->retry_auth = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005905
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005906 nl80211_mark_disconnected(drv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005907 os_memset(drv->auth_bssid, 0, ETH_ALEN);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005908 if (params->bssid)
5909 os_memcpy(drv->auth_attempt_bssid, params->bssid, ETH_ALEN);
5910 else
5911 os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005912 /* FIX: IBSS mode */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005913 nlmode = params->p2p ?
5914 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
5915 if (drv->nlmode != nlmode &&
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005916 wpa_driver_nl80211_set_mode(bss, nlmode) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005917 return -1;
5918
5919retry:
5920 msg = nlmsg_alloc();
5921 if (!msg)
5922 return -1;
5923
5924 wpa_printf(MSG_DEBUG, "nl80211: Authenticate (ifindex=%d)",
5925 drv->ifindex);
5926
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005927 nl80211_cmd(drv, msg, 0, NL80211_CMD_AUTHENTICATE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005928
5929 for (i = 0; i < 4; i++) {
5930 if (!params->wep_key[i])
5931 continue;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005932 wpa_driver_nl80211_set_key(bss->ifname, bss, WPA_ALG_WEP,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005933 NULL, i,
5934 i == params->wep_tx_keyidx, NULL, 0,
5935 params->wep_key[i],
5936 params->wep_key_len[i]);
5937 if (params->wep_tx_keyidx != i)
5938 continue;
5939 if (nl_add_key(msg, WPA_ALG_WEP, i, 1, NULL, 0,
5940 params->wep_key[i], params->wep_key_len[i])) {
5941 nlmsg_free(msg);
5942 return -1;
5943 }
5944 }
5945
5946 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
5947 if (params->bssid) {
5948 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
5949 MAC2STR(params->bssid));
5950 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
5951 }
5952 if (params->freq) {
5953 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
5954 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
5955 }
5956 if (params->ssid) {
5957 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
5958 params->ssid, params->ssid_len);
5959 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
5960 params->ssid);
5961 }
5962 wpa_hexdump(MSG_DEBUG, " * IEs", params->ie, params->ie_len);
5963 if (params->ie)
5964 NLA_PUT(msg, NL80211_ATTR_IE, params->ie_len, params->ie);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005965 if (params->sae_data) {
5966 wpa_hexdump(MSG_DEBUG, " * SAE data", params->sae_data,
5967 params->sae_data_len);
5968 NLA_PUT(msg, NL80211_ATTR_SAE_DATA, params->sae_data_len,
5969 params->sae_data);
5970 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005971 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
5972 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
5973 else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
5974 type = NL80211_AUTHTYPE_SHARED_KEY;
5975 else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
5976 type = NL80211_AUTHTYPE_NETWORK_EAP;
5977 else if (params->auth_alg & WPA_AUTH_ALG_FT)
5978 type = NL80211_AUTHTYPE_FT;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005979 else if (params->auth_alg & WPA_AUTH_ALG_SAE)
5980 type = NL80211_AUTHTYPE_SAE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005981 else
5982 goto nla_put_failure;
5983 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
5984 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
5985 if (params->local_state_change) {
5986 wpa_printf(MSG_DEBUG, " * Local state change only");
5987 NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE);
5988 }
5989
5990 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5991 msg = NULL;
5992 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005993 wpa_dbg(drv->ctx, MSG_DEBUG,
5994 "nl80211: MLME command failed (auth): ret=%d (%s)",
5995 ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005996 count++;
5997 if (ret == -EALREADY && count == 1 && params->bssid &&
5998 !params->local_state_change) {
5999 /*
6000 * mac80211 does not currently accept new
6001 * authentication if we are already authenticated. As a
6002 * workaround, force deauthentication and try again.
6003 */
6004 wpa_printf(MSG_DEBUG, "nl80211: Retry authentication "
6005 "after forced deauthentication");
6006 wpa_driver_nl80211_deauthenticate(
6007 bss, params->bssid,
6008 WLAN_REASON_PREV_AUTH_NOT_VALID);
6009 nlmsg_free(msg);
6010 goto retry;
6011 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006012
6013 if (ret == -ENOENT && params->freq && !is_retry) {
6014 /*
6015 * cfg80211 has likely expired the BSS entry even
6016 * though it was previously available in our internal
6017 * BSS table. To recover quickly, start a single
6018 * channel scan on the specified channel.
6019 */
6020 struct wpa_driver_scan_params scan;
6021 int freqs[2];
6022
6023 os_memset(&scan, 0, sizeof(scan));
6024 scan.num_ssids = 1;
6025 if (params->ssid) {
6026 scan.ssids[0].ssid = params->ssid;
6027 scan.ssids[0].ssid_len = params->ssid_len;
6028 }
6029 freqs[0] = params->freq;
6030 freqs[1] = 0;
6031 scan.freqs = freqs;
6032 wpa_printf(MSG_DEBUG, "nl80211: Trigger single "
6033 "channel scan to refresh cfg80211 BSS "
6034 "entry");
6035 ret = wpa_driver_nl80211_scan(bss, &scan);
6036 if (ret == 0) {
6037 nl80211_copy_auth_params(drv, params);
6038 drv->scan_for_auth = 1;
6039 }
6040 } else if (is_retry) {
6041 /*
6042 * Need to indicate this with an event since the return
6043 * value from the retry is not delivered to core code.
6044 */
6045 union wpa_event_data event;
6046 wpa_printf(MSG_DEBUG, "nl80211: Authentication retry "
6047 "failed");
6048 os_memset(&event, 0, sizeof(event));
6049 os_memcpy(event.timeout_event.addr, drv->auth_bssid_,
6050 ETH_ALEN);
6051 wpa_supplicant_event(drv->ctx, EVENT_AUTH_TIMED_OUT,
6052 &event);
6053 }
6054
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006055 goto nla_put_failure;
6056 }
6057 ret = 0;
6058 wpa_printf(MSG_DEBUG, "nl80211: Authentication request send "
6059 "successfully");
6060
6061nla_put_failure:
6062 nlmsg_free(msg);
6063 return ret;
6064}
6065
6066
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006067static int wpa_driver_nl80211_authenticate_retry(
6068 struct wpa_driver_nl80211_data *drv)
6069{
6070 struct wpa_driver_auth_params params;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006071 struct i802_bss *bss = drv->first_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006072 int i;
6073
6074 wpa_printf(MSG_DEBUG, "nl80211: Try to authenticate again");
6075
6076 os_memset(&params, 0, sizeof(params));
6077 params.freq = drv->auth_freq;
6078 params.auth_alg = drv->auth_alg;
6079 params.wep_tx_keyidx = drv->auth_wep_tx_keyidx;
6080 params.local_state_change = drv->auth_local_state_change;
6081 params.p2p = drv->auth_p2p;
6082
6083 if (!is_zero_ether_addr(drv->auth_bssid_))
6084 params.bssid = drv->auth_bssid_;
6085
6086 if (drv->auth_ssid_len) {
6087 params.ssid = drv->auth_ssid;
6088 params.ssid_len = drv->auth_ssid_len;
6089 }
6090
6091 params.ie = drv->auth_ie;
6092 params.ie_len = drv->auth_ie_len;
6093
6094 for (i = 0; i < 4; i++) {
6095 if (drv->auth_wep_key_len[i]) {
6096 params.wep_key[i] = drv->auth_wep_key[i];
6097 params.wep_key_len[i] = drv->auth_wep_key_len[i];
6098 }
6099 }
6100
6101 drv->retry_auth = 1;
6102 return wpa_driver_nl80211_authenticate(bss, &params);
6103}
6104
6105
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006106struct phy_info_arg {
6107 u16 *num_modes;
6108 struct hostapd_hw_modes *modes;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006109 int last_mode, last_chan_idx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006110};
6111
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006112static void phy_info_ht_capa(struct hostapd_hw_modes *mode, struct nlattr *capa,
6113 struct nlattr *ampdu_factor,
6114 struct nlattr *ampdu_density,
6115 struct nlattr *mcs_set)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006116{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006117 if (capa)
6118 mode->ht_capab = nla_get_u16(capa);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006119
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006120 if (ampdu_factor)
6121 mode->a_mpdu_params |= nla_get_u8(ampdu_factor) & 0x03;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006122
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006123 if (ampdu_density)
6124 mode->a_mpdu_params |= nla_get_u8(ampdu_density) << 2;
6125
6126 if (mcs_set && nla_len(mcs_set) >= 16) {
6127 u8 *mcs;
6128 mcs = nla_data(mcs_set);
6129 os_memcpy(mode->mcs_set, mcs, 16);
6130 }
6131}
6132
6133
6134static void phy_info_vht_capa(struct hostapd_hw_modes *mode,
6135 struct nlattr *capa,
6136 struct nlattr *mcs_set)
6137{
6138 if (capa)
6139 mode->vht_capab = nla_get_u32(capa);
6140
6141 if (mcs_set && nla_len(mcs_set) >= 8) {
6142 u8 *mcs;
6143 mcs = nla_data(mcs_set);
6144 os_memcpy(mode->vht_mcs_set, mcs, 8);
6145 }
6146}
6147
6148
6149static void phy_info_freq(struct hostapd_hw_modes *mode,
6150 struct hostapd_channel_data *chan,
6151 struct nlattr *tb_freq[])
6152{
Dmitry Shmidt4b060592013-04-29 16:42:49 -07006153 u8 channel;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006154 chan->freq = nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_FREQ]);
6155 chan->flag = 0;
Dmitry Shmidt4b060592013-04-29 16:42:49 -07006156 if (ieee80211_freq_to_chan(chan->freq, &channel) != NUM_HOSTAPD_MODES)
6157 chan->chan = channel;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006158
6159 if (tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
6160 chan->flag |= HOSTAPD_CHAN_DISABLED;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006161 if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IR])
6162 chan->flag |= HOSTAPD_CHAN_PASSIVE_SCAN | HOSTAPD_CHAN_NO_IBSS;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006163 if (tb_freq[NL80211_FREQUENCY_ATTR_RADAR])
6164 chan->flag |= HOSTAPD_CHAN_RADAR;
6165
Dmitry Shmidtea69e842013-05-13 14:52:28 -07006166 if (tb_freq[NL80211_FREQUENCY_ATTR_DFS_STATE]) {
6167 enum nl80211_dfs_state state =
6168 nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_DFS_STATE]);
6169
6170 switch (state) {
6171 case NL80211_DFS_USABLE:
6172 chan->flag |= HOSTAPD_CHAN_DFS_USABLE;
6173 break;
6174 case NL80211_DFS_AVAILABLE:
6175 chan->flag |= HOSTAPD_CHAN_DFS_AVAILABLE;
6176 break;
6177 case NL80211_DFS_UNAVAILABLE:
6178 chan->flag |= HOSTAPD_CHAN_DFS_UNAVAILABLE;
6179 break;
6180 }
6181 }
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006182}
6183
6184
6185static int phy_info_freqs(struct phy_info_arg *phy_info,
6186 struct hostapd_hw_modes *mode, struct nlattr *tb)
6187{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006188 static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
6189 [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
6190 [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006191 [NL80211_FREQUENCY_ATTR_NO_IR] = { .type = NLA_FLAG },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006192 [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
6193 [NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32 },
Dmitry Shmidtea69e842013-05-13 14:52:28 -07006194 [NL80211_FREQUENCY_ATTR_DFS_STATE] = { .type = NLA_U32 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006195 };
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006196 int new_channels = 0;
6197 struct hostapd_channel_data *channel;
6198 struct nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006199 struct nlattr *nl_freq;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006200 int rem_freq, idx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006201
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006202 if (tb == NULL)
6203 return NL_OK;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006204
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006205 nla_for_each_nested(nl_freq, tb, rem_freq) {
6206 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX,
6207 nla_data(nl_freq), nla_len(nl_freq), freq_policy);
6208 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
6209 continue;
6210 new_channels++;
6211 }
6212
6213 channel = os_realloc_array(mode->channels,
6214 mode->num_channels + new_channels,
6215 sizeof(struct hostapd_channel_data));
6216 if (!channel)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006217 return NL_SKIP;
6218
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006219 mode->channels = channel;
6220 mode->num_channels += new_channels;
6221
6222 idx = phy_info->last_chan_idx;
6223
6224 nla_for_each_nested(nl_freq, tb, rem_freq) {
6225 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX,
6226 nla_data(nl_freq), nla_len(nl_freq), freq_policy);
6227 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
6228 continue;
6229 phy_info_freq(mode, &mode->channels[idx], tb_freq);
6230 idx++;
6231 }
6232 phy_info->last_chan_idx = idx;
6233
6234 return NL_OK;
6235}
6236
6237
6238static int phy_info_rates(struct hostapd_hw_modes *mode, struct nlattr *tb)
6239{
6240 static struct nla_policy rate_policy[NL80211_BITRATE_ATTR_MAX + 1] = {
6241 [NL80211_BITRATE_ATTR_RATE] = { .type = NLA_U32 },
6242 [NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE] =
6243 { .type = NLA_FLAG },
6244 };
6245 struct nlattr *tb_rate[NL80211_BITRATE_ATTR_MAX + 1];
6246 struct nlattr *nl_rate;
6247 int rem_rate, idx;
6248
6249 if (tb == NULL)
6250 return NL_OK;
6251
6252 nla_for_each_nested(nl_rate, tb, rem_rate) {
6253 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX,
6254 nla_data(nl_rate), nla_len(nl_rate),
6255 rate_policy);
6256 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
6257 continue;
6258 mode->num_rates++;
6259 }
6260
6261 mode->rates = os_calloc(mode->num_rates, sizeof(int));
6262 if (!mode->rates)
6263 return NL_SKIP;
6264
6265 idx = 0;
6266
6267 nla_for_each_nested(nl_rate, tb, rem_rate) {
6268 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX,
6269 nla_data(nl_rate), nla_len(nl_rate),
6270 rate_policy);
6271 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
6272 continue;
6273 mode->rates[idx] = nla_get_u32(
6274 tb_rate[NL80211_BITRATE_ATTR_RATE]);
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006275 idx++;
6276 }
6277
6278 return NL_OK;
6279}
6280
6281
6282static int phy_info_band(struct phy_info_arg *phy_info, struct nlattr *nl_band)
6283{
6284 struct nlattr *tb_band[NL80211_BAND_ATTR_MAX + 1];
6285 struct hostapd_hw_modes *mode;
6286 int ret;
6287
6288 if (phy_info->last_mode != nl_band->nla_type) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006289 mode = os_realloc_array(phy_info->modes,
6290 *phy_info->num_modes + 1,
6291 sizeof(*mode));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006292 if (!mode)
6293 return NL_SKIP;
6294 phy_info->modes = mode;
6295
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006296 mode = &phy_info->modes[*(phy_info->num_modes)];
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006297 os_memset(mode, 0, sizeof(*mode));
6298 mode->mode = NUM_HOSTAPD_MODES;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006299 mode->flags = HOSTAPD_MODE_FLAG_HT_INFO_KNOWN |
6300 HOSTAPD_MODE_FLAG_VHT_INFO_KNOWN;
6301
6302 /*
6303 * Unsupported VHT MCS stream is defined as value 3, so the VHT
6304 * MCS RX/TX map must be initialized with 0xffff to mark all 8
6305 * possible streams as unsupported. This will be overridden if
6306 * driver advertises VHT support.
6307 */
6308 mode->vht_mcs_set[0] = 0xff;
6309 mode->vht_mcs_set[1] = 0xff;
6310 mode->vht_mcs_set[4] = 0xff;
6311 mode->vht_mcs_set[5] = 0xff;
6312
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006313 *(phy_info->num_modes) += 1;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006314 phy_info->last_mode = nl_band->nla_type;
6315 phy_info->last_chan_idx = 0;
6316 } else
6317 mode = &phy_info->modes[*(phy_info->num_modes) - 1];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006318
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006319 nla_parse(tb_band, NL80211_BAND_ATTR_MAX, nla_data(nl_band),
6320 nla_len(nl_band), NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006321
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006322 phy_info_ht_capa(mode, tb_band[NL80211_BAND_ATTR_HT_CAPA],
6323 tb_band[NL80211_BAND_ATTR_HT_AMPDU_FACTOR],
6324 tb_band[NL80211_BAND_ATTR_HT_AMPDU_DENSITY],
6325 tb_band[NL80211_BAND_ATTR_HT_MCS_SET]);
6326 phy_info_vht_capa(mode, tb_band[NL80211_BAND_ATTR_VHT_CAPA],
6327 tb_band[NL80211_BAND_ATTR_VHT_MCS_SET]);
6328 ret = phy_info_freqs(phy_info, mode, tb_band[NL80211_BAND_ATTR_FREQS]);
6329 if (ret != NL_OK)
6330 return ret;
6331 ret = phy_info_rates(mode, tb_band[NL80211_BAND_ATTR_RATES]);
6332 if (ret != NL_OK)
6333 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006334
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006335 return NL_OK;
6336}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006337
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006338
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006339static int phy_info_handler(struct nl_msg *msg, void *arg)
6340{
6341 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
6342 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6343 struct phy_info_arg *phy_info = arg;
6344 struct nlattr *nl_band;
6345 int rem_band;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006346
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006347 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6348 genlmsg_attrlen(gnlh, 0), NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006349
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006350 if (!tb_msg[NL80211_ATTR_WIPHY_BANDS])
6351 return NL_SKIP;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006352
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006353 nla_for_each_nested(nl_band, tb_msg[NL80211_ATTR_WIPHY_BANDS], rem_band)
6354 {
6355 int res = phy_info_band(phy_info, nl_band);
6356 if (res != NL_OK)
6357 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006358 }
6359
6360 return NL_SKIP;
6361}
6362
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006363
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006364static struct hostapd_hw_modes *
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07006365wpa_driver_nl80211_postprocess_modes(struct hostapd_hw_modes *modes,
6366 u16 *num_modes)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006367{
6368 u16 m;
6369 struct hostapd_hw_modes *mode11g = NULL, *nmodes, *mode;
6370 int i, mode11g_idx = -1;
6371
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07006372 /* heuristic to set up modes */
6373 for (m = 0; m < *num_modes; m++) {
6374 if (!modes[m].num_channels)
6375 continue;
6376 if (modes[m].channels[0].freq < 4000) {
6377 modes[m].mode = HOSTAPD_MODE_IEEE80211B;
6378 for (i = 0; i < modes[m].num_rates; i++) {
6379 if (modes[m].rates[i] > 200) {
6380 modes[m].mode = HOSTAPD_MODE_IEEE80211G;
6381 break;
6382 }
6383 }
6384 } else if (modes[m].channels[0].freq > 50000)
6385 modes[m].mode = HOSTAPD_MODE_IEEE80211AD;
6386 else
6387 modes[m].mode = HOSTAPD_MODE_IEEE80211A;
6388 }
6389
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006390 /* If only 802.11g mode is included, use it to construct matching
6391 * 802.11b mode data. */
6392
6393 for (m = 0; m < *num_modes; m++) {
6394 if (modes[m].mode == HOSTAPD_MODE_IEEE80211B)
6395 return modes; /* 802.11b already included */
6396 if (modes[m].mode == HOSTAPD_MODE_IEEE80211G)
6397 mode11g_idx = m;
6398 }
6399
6400 if (mode11g_idx < 0)
6401 return modes; /* 2.4 GHz band not supported at all */
6402
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006403 nmodes = os_realloc_array(modes, *num_modes + 1, sizeof(*nmodes));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006404 if (nmodes == NULL)
6405 return modes; /* Could not add 802.11b mode */
6406
6407 mode = &nmodes[*num_modes];
6408 os_memset(mode, 0, sizeof(*mode));
6409 (*num_modes)++;
6410 modes = nmodes;
6411
6412 mode->mode = HOSTAPD_MODE_IEEE80211B;
6413
6414 mode11g = &modes[mode11g_idx];
6415 mode->num_channels = mode11g->num_channels;
6416 mode->channels = os_malloc(mode11g->num_channels *
6417 sizeof(struct hostapd_channel_data));
6418 if (mode->channels == NULL) {
6419 (*num_modes)--;
6420 return modes; /* Could not add 802.11b mode */
6421 }
6422 os_memcpy(mode->channels, mode11g->channels,
6423 mode11g->num_channels * sizeof(struct hostapd_channel_data));
6424
6425 mode->num_rates = 0;
6426 mode->rates = os_malloc(4 * sizeof(int));
6427 if (mode->rates == NULL) {
6428 os_free(mode->channels);
6429 (*num_modes)--;
6430 return modes; /* Could not add 802.11b mode */
6431 }
6432
6433 for (i = 0; i < mode11g->num_rates; i++) {
6434 if (mode11g->rates[i] != 10 && mode11g->rates[i] != 20 &&
6435 mode11g->rates[i] != 55 && mode11g->rates[i] != 110)
6436 continue;
6437 mode->rates[mode->num_rates] = mode11g->rates[i];
6438 mode->num_rates++;
6439 if (mode->num_rates == 4)
6440 break;
6441 }
6442
6443 if (mode->num_rates == 0) {
6444 os_free(mode->channels);
6445 os_free(mode->rates);
6446 (*num_modes)--;
6447 return modes; /* No 802.11b rates */
6448 }
6449
6450 wpa_printf(MSG_DEBUG, "nl80211: Added 802.11b mode based on 802.11g "
6451 "information");
6452
6453 return modes;
6454}
6455
6456
6457static void nl80211_set_ht40_mode(struct hostapd_hw_modes *mode, int start,
6458 int end)
6459{
6460 int c;
6461
6462 for (c = 0; c < mode->num_channels; c++) {
6463 struct hostapd_channel_data *chan = &mode->channels[c];
6464 if (chan->freq - 10 >= start && chan->freq + 10 <= end)
6465 chan->flag |= HOSTAPD_CHAN_HT40;
6466 }
6467}
6468
6469
6470static void nl80211_set_ht40_mode_sec(struct hostapd_hw_modes *mode, int start,
6471 int end)
6472{
6473 int c;
6474
6475 for (c = 0; c < mode->num_channels; c++) {
6476 struct hostapd_channel_data *chan = &mode->channels[c];
6477 if (!(chan->flag & HOSTAPD_CHAN_HT40))
6478 continue;
6479 if (chan->freq - 30 >= start && chan->freq - 10 <= end)
6480 chan->flag |= HOSTAPD_CHAN_HT40MINUS;
6481 if (chan->freq + 10 >= start && chan->freq + 30 <= end)
6482 chan->flag |= HOSTAPD_CHAN_HT40PLUS;
6483 }
6484}
6485
6486
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006487static void nl80211_reg_rule_max_eirp(u32 start, u32 end, u32 max_eirp,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006488 struct phy_info_arg *results)
6489{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006490 u16 m;
6491
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006492 for (m = 0; m < *results->num_modes; m++) {
6493 int c;
6494 struct hostapd_hw_modes *mode = &results->modes[m];
6495
6496 for (c = 0; c < mode->num_channels; c++) {
6497 struct hostapd_channel_data *chan = &mode->channels[c];
6498 if ((u32) chan->freq - 10 >= start &&
6499 (u32) chan->freq + 10 <= end)
6500 chan->max_tx_power = max_eirp;
6501 }
6502 }
6503}
6504
6505
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006506static void nl80211_reg_rule_ht40(u32 start, u32 end,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006507 struct phy_info_arg *results)
6508{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006509 u16 m;
6510
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006511 for (m = 0; m < *results->num_modes; m++) {
6512 if (!(results->modes[m].ht_capab &
6513 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
6514 continue;
6515 nl80211_set_ht40_mode(&results->modes[m], start, end);
6516 }
6517}
6518
6519
6520static void nl80211_reg_rule_sec(struct nlattr *tb[],
6521 struct phy_info_arg *results)
6522{
6523 u32 start, end, max_bw;
6524 u16 m;
6525
6526 if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
6527 tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
6528 tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
6529 return;
6530
6531 start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
6532 end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
6533 max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
6534
6535 if (max_bw < 20)
6536 return;
6537
6538 for (m = 0; m < *results->num_modes; m++) {
6539 if (!(results->modes[m].ht_capab &
6540 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
6541 continue;
6542 nl80211_set_ht40_mode_sec(&results->modes[m], start, end);
6543 }
6544}
6545
6546
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006547static void nl80211_set_vht_mode(struct hostapd_hw_modes *mode, int start,
6548 int end)
6549{
6550 int c;
6551
6552 for (c = 0; c < mode->num_channels; c++) {
6553 struct hostapd_channel_data *chan = &mode->channels[c];
6554 if (chan->freq - 10 >= start && chan->freq + 70 <= end)
6555 chan->flag |= HOSTAPD_CHAN_VHT_10_70;
6556
6557 if (chan->freq - 30 >= start && chan->freq + 50 <= end)
6558 chan->flag |= HOSTAPD_CHAN_VHT_30_50;
6559
6560 if (chan->freq - 50 >= start && chan->freq + 30 <= end)
6561 chan->flag |= HOSTAPD_CHAN_VHT_50_30;
6562
6563 if (chan->freq - 70 >= start && chan->freq + 10 <= end)
6564 chan->flag |= HOSTAPD_CHAN_VHT_70_10;
6565 }
6566}
6567
6568
6569static void nl80211_reg_rule_vht(struct nlattr *tb[],
6570 struct phy_info_arg *results)
6571{
6572 u32 start, end, max_bw;
6573 u16 m;
6574
6575 if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
6576 tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
6577 tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
6578 return;
6579
6580 start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
6581 end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
6582 max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
6583
6584 if (max_bw < 80)
6585 return;
6586
6587 for (m = 0; m < *results->num_modes; m++) {
6588 if (!(results->modes[m].ht_capab &
6589 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
6590 continue;
6591 /* TODO: use a real VHT support indication */
6592 if (!results->modes[m].vht_capab)
6593 continue;
6594
6595 nl80211_set_vht_mode(&results->modes[m], start, end);
6596 }
6597}
6598
6599
Dmitry Shmidt97672262014-02-03 13:02:54 -08006600static const char * dfs_domain_name(enum nl80211_dfs_regions region)
6601{
6602 switch (region) {
6603 case NL80211_DFS_UNSET:
6604 return "DFS-UNSET";
6605 case NL80211_DFS_FCC:
6606 return "DFS-FCC";
6607 case NL80211_DFS_ETSI:
6608 return "DFS-ETSI";
6609 case NL80211_DFS_JP:
6610 return "DFS-JP";
6611 default:
6612 return "DFS-invalid";
6613 }
6614}
6615
6616
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006617static int nl80211_get_reg(struct nl_msg *msg, void *arg)
6618{
6619 struct phy_info_arg *results = arg;
6620 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
6621 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6622 struct nlattr *nl_rule;
6623 struct nlattr *tb_rule[NL80211_FREQUENCY_ATTR_MAX + 1];
6624 int rem_rule;
6625 static struct nla_policy reg_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
6626 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
6627 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
6628 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
6629 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
6630 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
6631 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
6632 };
6633
6634 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6635 genlmsg_attrlen(gnlh, 0), NULL);
6636 if (!tb_msg[NL80211_ATTR_REG_ALPHA2] ||
6637 !tb_msg[NL80211_ATTR_REG_RULES]) {
6638 wpa_printf(MSG_DEBUG, "nl80211: No regulatory information "
6639 "available");
6640 return NL_SKIP;
6641 }
6642
Dmitry Shmidt97672262014-02-03 13:02:54 -08006643 if (tb_msg[NL80211_ATTR_DFS_REGION]) {
6644 enum nl80211_dfs_regions dfs_domain;
6645 dfs_domain = nla_get_u8(tb_msg[NL80211_ATTR_DFS_REGION]);
6646 wpa_printf(MSG_DEBUG, "nl80211: Regulatory information - country=%s (%s)",
6647 (char *) nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]),
6648 dfs_domain_name(dfs_domain));
6649 } else {
6650 wpa_printf(MSG_DEBUG, "nl80211: Regulatory information - country=%s",
6651 (char *) nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]));
6652 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006653
6654 nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
6655 {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08006656 u32 start, end, max_eirp = 0, max_bw = 0, flags = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006657 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
6658 nla_data(nl_rule), nla_len(nl_rule), reg_policy);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006659 if (tb_rule[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
6660 tb_rule[NL80211_ATTR_FREQ_RANGE_END] == NULL)
6661 continue;
6662 start = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
6663 end = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
6664 if (tb_rule[NL80211_ATTR_POWER_RULE_MAX_EIRP])
6665 max_eirp = nla_get_u32(tb_rule[NL80211_ATTR_POWER_RULE_MAX_EIRP]) / 100;
6666 if (tb_rule[NL80211_ATTR_FREQ_RANGE_MAX_BW])
6667 max_bw = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08006668 if (tb_rule[NL80211_ATTR_REG_RULE_FLAGS])
6669 flags = nla_get_u32(tb_rule[NL80211_ATTR_REG_RULE_FLAGS]);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006670
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08006671 wpa_printf(MSG_DEBUG, "nl80211: %u-%u @ %u MHz %u mBm%s%s%s%s%s%s%s%s",
6672 start, end, max_bw, max_eirp,
6673 flags & NL80211_RRF_NO_OFDM ? " (no OFDM)" : "",
6674 flags & NL80211_RRF_NO_CCK ? " (no CCK)" : "",
6675 flags & NL80211_RRF_NO_INDOOR ? " (no indoor)" : "",
6676 flags & NL80211_RRF_NO_OUTDOOR ? " (no outdoor)" :
6677 "",
6678 flags & NL80211_RRF_DFS ? " (DFS)" : "",
6679 flags & NL80211_RRF_PTP_ONLY ? " (PTP only)" : "",
6680 flags & NL80211_RRF_PTMP_ONLY ? " (PTMP only)" : "",
6681 flags & NL80211_RRF_NO_IR ? " (no IR)" : "");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006682 if (max_bw >= 40)
6683 nl80211_reg_rule_ht40(start, end, results);
6684 if (tb_rule[NL80211_ATTR_POWER_RULE_MAX_EIRP])
6685 nl80211_reg_rule_max_eirp(start, end, max_eirp,
6686 results);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006687 }
6688
6689 nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
6690 {
6691 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
6692 nla_data(nl_rule), nla_len(nl_rule), reg_policy);
6693 nl80211_reg_rule_sec(tb_rule, results);
6694 }
6695
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006696 nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
6697 {
6698 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
6699 nla_data(nl_rule), nla_len(nl_rule), reg_policy);
6700 nl80211_reg_rule_vht(tb_rule, results);
6701 }
6702
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006703 return NL_SKIP;
6704}
6705
6706
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006707static int nl80211_set_regulatory_flags(struct wpa_driver_nl80211_data *drv,
6708 struct phy_info_arg *results)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006709{
6710 struct nl_msg *msg;
6711
6712 msg = nlmsg_alloc();
6713 if (!msg)
6714 return -ENOMEM;
6715
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006716 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006717 return send_and_recv_msgs(drv, msg, nl80211_get_reg, results);
6718}
6719
6720
6721static struct hostapd_hw_modes *
6722wpa_driver_nl80211_get_hw_feature_data(void *priv, u16 *num_modes, u16 *flags)
6723{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006724 u32 feat;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006725 struct i802_bss *bss = priv;
6726 struct wpa_driver_nl80211_data *drv = bss->drv;
6727 struct nl_msg *msg;
6728 struct phy_info_arg result = {
6729 .num_modes = num_modes,
6730 .modes = NULL,
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006731 .last_mode = -1,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006732 };
6733
6734 *num_modes = 0;
6735 *flags = 0;
6736
6737 msg = nlmsg_alloc();
6738 if (!msg)
6739 return NULL;
6740
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006741 feat = get_nl80211_protocol_features(drv);
6742 if (feat & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP)
6743 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_WIPHY);
6744 else
6745 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006746
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006747 NLA_PUT_FLAG(msg, NL80211_ATTR_SPLIT_WIPHY_DUMP);
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08006748 if (nl80211_set_iface_id(msg, bss) < 0)
6749 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006750
6751 if (send_and_recv_msgs(drv, msg, phy_info_handler, &result) == 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006752 nl80211_set_regulatory_flags(drv, &result);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07006753 return wpa_driver_nl80211_postprocess_modes(result.modes,
6754 num_modes);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006755 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006756 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006757 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006758 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006759 return NULL;
6760}
6761
6762
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006763static int wpa_driver_nl80211_send_mntr(struct wpa_driver_nl80211_data *drv,
6764 const void *data, size_t len,
6765 int encrypt, int noack)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006766{
6767 __u8 rtap_hdr[] = {
6768 0x00, 0x00, /* radiotap version */
6769 0x0e, 0x00, /* radiotap length */
6770 0x02, 0xc0, 0x00, 0x00, /* bmap: flags, tx and rx flags */
6771 IEEE80211_RADIOTAP_F_FRAG, /* F_FRAG (fragment if required) */
6772 0x00, /* padding */
6773 0x00, 0x00, /* RX and TX flags to indicate that */
6774 0x00, 0x00, /* this is the injected frame directly */
6775 };
6776 struct iovec iov[2] = {
6777 {
6778 .iov_base = &rtap_hdr,
6779 .iov_len = sizeof(rtap_hdr),
6780 },
6781 {
6782 .iov_base = (void *) data,
6783 .iov_len = len,
6784 }
6785 };
6786 struct msghdr msg = {
6787 .msg_name = NULL,
6788 .msg_namelen = 0,
6789 .msg_iov = iov,
6790 .msg_iovlen = 2,
6791 .msg_control = NULL,
6792 .msg_controllen = 0,
6793 .msg_flags = 0,
6794 };
6795 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006796 u16 txflags = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006797
6798 if (encrypt)
6799 rtap_hdr[8] |= IEEE80211_RADIOTAP_F_WEP;
6800
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07006801 if (drv->monitor_sock < 0) {
6802 wpa_printf(MSG_DEBUG, "nl80211: No monitor socket available "
6803 "for %s", __func__);
6804 return -1;
6805 }
6806
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006807 if (noack)
6808 txflags |= IEEE80211_RADIOTAP_F_TX_NOACK;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006809 WPA_PUT_LE16(&rtap_hdr[12], txflags);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006810
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006811 res = sendmsg(drv->monitor_sock, &msg, 0);
6812 if (res < 0) {
6813 wpa_printf(MSG_INFO, "nl80211: sendmsg: %s", strerror(errno));
6814 return -1;
6815 }
6816 return 0;
6817}
6818
6819
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006820static int wpa_driver_nl80211_send_frame(struct i802_bss *bss,
6821 const void *data, size_t len,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006822 int encrypt, int noack,
6823 unsigned int freq, int no_cck,
6824 int offchanok, unsigned int wait_time)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006825{
6826 struct wpa_driver_nl80211_data *drv = bss->drv;
6827 u64 cookie;
Dmitry Shmidt051af732013-10-22 13:52:46 -07006828 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006829
Dmitry Shmidt56052862013-10-04 10:23:25 -07006830 if (freq == 0) {
6831 wpa_printf(MSG_DEBUG, "nl80211: send_frame - Use bss->freq=%u",
6832 bss->freq);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006833 freq = bss->freq;
Dmitry Shmidt56052862013-10-04 10:23:25 -07006834 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006835
Dmitry Shmidt56052862013-10-04 10:23:25 -07006836 if (drv->use_monitor) {
6837 wpa_printf(MSG_DEBUG, "nl80211: send_frame(freq=%u bss->freq=%u) -> send_mntr",
6838 freq, bss->freq);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006839 return wpa_driver_nl80211_send_mntr(drv, data, len,
6840 encrypt, noack);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006841 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006842
Dmitry Shmidt56052862013-10-04 10:23:25 -07006843 wpa_printf(MSG_DEBUG, "nl80211: send_frame -> send_frame_cmd");
Dmitry Shmidt051af732013-10-22 13:52:46 -07006844 res = nl80211_send_frame_cmd(bss, freq, wait_time, data, len,
6845 &cookie, no_cck, noack, offchanok);
6846 if (res == 0 && !noack) {
6847 const struct ieee80211_mgmt *mgmt;
6848 u16 fc;
6849
6850 mgmt = (const struct ieee80211_mgmt *) data;
6851 fc = le_to_host16(mgmt->frame_control);
6852 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
6853 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_ACTION) {
6854 wpa_printf(MSG_MSGDUMP,
6855 "nl80211: Update send_action_cookie from 0x%llx to 0x%llx",
6856 (long long unsigned int)
6857 drv->send_action_cookie,
6858 (long long unsigned int) cookie);
6859 drv->send_action_cookie = cookie;
6860 }
6861 }
6862
6863 return res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006864}
6865
6866
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006867static int wpa_driver_nl80211_send_mlme(struct i802_bss *bss, const u8 *data,
6868 size_t data_len, int noack,
6869 unsigned int freq, int no_cck,
6870 int offchanok,
6871 unsigned int wait_time)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006872{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006873 struct wpa_driver_nl80211_data *drv = bss->drv;
6874 struct ieee80211_mgmt *mgmt;
6875 int encrypt = 1;
6876 u16 fc;
6877
6878 mgmt = (struct ieee80211_mgmt *) data;
6879 fc = le_to_host16(mgmt->frame_control);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006880 wpa_printf(MSG_DEBUG, "nl80211: send_mlme - noack=%d freq=%u no_cck=%d offchanok=%d wait_time=%u fc=0x%x nlmode=%d",
6881 noack, freq, no_cck, offchanok, wait_time, fc, drv->nlmode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006882
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006883 if ((is_sta_interface(drv->nlmode) ||
6884 drv->nlmode == NL80211_IFTYPE_P2P_DEVICE) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006885 WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
6886 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_RESP) {
6887 /*
6888 * The use of last_mgmt_freq is a bit of a hack,
6889 * but it works due to the single-threaded nature
6890 * of wpa_supplicant.
6891 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07006892 if (freq == 0) {
6893 wpa_printf(MSG_DEBUG, "nl80211: Use last_mgmt_freq=%d",
6894 drv->last_mgmt_freq);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006895 freq = drv->last_mgmt_freq;
Dmitry Shmidt56052862013-10-04 10:23:25 -07006896 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006897 return nl80211_send_frame_cmd(bss, freq, 0,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006898 data, data_len, NULL, 1, noack,
6899 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006900 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006901
6902 if (drv->device_ap_sme && is_ap_interface(drv->nlmode)) {
Dmitry Shmidt56052862013-10-04 10:23:25 -07006903 if (freq == 0) {
6904 wpa_printf(MSG_DEBUG, "nl80211: Use bss->freq=%d",
6905 bss->freq);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006906 freq = bss->freq;
Dmitry Shmidt56052862013-10-04 10:23:25 -07006907 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07006908 return nl80211_send_frame_cmd(bss, freq,
6909 (int) freq == bss->freq ? 0 :
6910 wait_time,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006911 data, data_len,
6912 &drv->send_action_cookie,
6913 no_cck, noack, offchanok);
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07006914 }
Dmitry Shmidtb638fe72012-03-20 12:51:25 -07006915
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006916 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
6917 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) {
6918 /*
6919 * Only one of the authentication frame types is encrypted.
6920 * In order for static WEP encryption to work properly (i.e.,
6921 * to not encrypt the frame), we need to tell mac80211 about
6922 * the frames that must not be encrypted.
6923 */
6924 u16 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
6925 u16 auth_trans = le_to_host16(mgmt->u.auth.auth_transaction);
6926 if (auth_alg != WLAN_AUTH_SHARED_KEY || auth_trans != 3)
6927 encrypt = 0;
6928 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006929
Dmitry Shmidt56052862013-10-04 10:23:25 -07006930 wpa_printf(MSG_DEBUG, "nl80211: send_mlme -> send_frame");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006931 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006932 noack, freq, no_cck, offchanok,
6933 wait_time);
6934}
6935
6936
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006937static int nl80211_set_bss(struct i802_bss *bss, int cts, int preamble,
6938 int slot, int ht_opmode, int ap_isolate,
6939 int *basic_rates)
6940{
6941 struct wpa_driver_nl80211_data *drv = bss->drv;
6942 struct nl_msg *msg;
6943
6944 msg = nlmsg_alloc();
6945 if (!msg)
6946 return -ENOMEM;
6947
6948 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_BSS);
6949
6950 if (cts >= 0)
6951 NLA_PUT_U8(msg, NL80211_ATTR_BSS_CTS_PROT, cts);
6952 if (preamble >= 0)
6953 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble);
6954 if (slot >= 0)
6955 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot);
6956 if (ht_opmode >= 0)
6957 NLA_PUT_U16(msg, NL80211_ATTR_BSS_HT_OPMODE, ht_opmode);
6958 if (ap_isolate >= 0)
6959 NLA_PUT_U8(msg, NL80211_ATTR_AP_ISOLATE, ap_isolate);
6960
6961 if (basic_rates) {
6962 u8 rates[NL80211_MAX_SUPP_RATES];
6963 u8 rates_len = 0;
6964 int i;
6965
6966 for (i = 0; i < NL80211_MAX_SUPP_RATES && basic_rates[i] >= 0;
6967 i++)
6968 rates[rates_len++] = basic_rates[i] / 5;
6969
6970 NLA_PUT(msg, NL80211_ATTR_BSS_BASIC_RATES, rates_len, rates);
6971 }
6972
6973 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
6974
6975 return send_and_recv_msgs(drv, msg, NULL, NULL);
6976 nla_put_failure:
6977 nlmsg_free(msg);
6978 return -ENOBUFS;
6979}
6980
6981
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006982static int wpa_driver_nl80211_set_acl(void *priv,
6983 struct hostapd_acl_params *params)
6984{
6985 struct i802_bss *bss = priv;
6986 struct wpa_driver_nl80211_data *drv = bss->drv;
6987 struct nl_msg *msg;
6988 struct nlattr *acl;
6989 unsigned int i;
6990 int ret = 0;
6991
6992 if (!(drv->capa.max_acl_mac_addrs))
6993 return -ENOTSUP;
6994
6995 if (params->num_mac_acl > drv->capa.max_acl_mac_addrs)
6996 return -ENOTSUP;
6997
6998 msg = nlmsg_alloc();
6999 if (!msg)
7000 return -ENOMEM;
7001
7002 wpa_printf(MSG_DEBUG, "nl80211: Set %s ACL (num_mac_acl=%u)",
7003 params->acl_policy ? "Accept" : "Deny", params->num_mac_acl);
7004
7005 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_MAC_ACL);
7006
7007 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
7008
7009 NLA_PUT_U32(msg, NL80211_ATTR_ACL_POLICY, params->acl_policy ?
7010 NL80211_ACL_POLICY_DENY_UNLESS_LISTED :
7011 NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED);
7012
7013 acl = nla_nest_start(msg, NL80211_ATTR_MAC_ADDRS);
7014 if (acl == NULL)
7015 goto nla_put_failure;
7016
7017 for (i = 0; i < params->num_mac_acl; i++)
7018 NLA_PUT(msg, i + 1, ETH_ALEN, params->mac_acl[i].addr);
7019
7020 nla_nest_end(msg, acl);
7021
7022 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7023 msg = NULL;
7024 if (ret) {
7025 wpa_printf(MSG_DEBUG, "nl80211: Failed to set MAC ACL: %d (%s)",
7026 ret, strerror(-ret));
7027 }
7028
7029nla_put_failure:
7030 nlmsg_free(msg);
7031
7032 return ret;
7033}
7034
7035
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007036static int wpa_driver_nl80211_set_ap(void *priv,
7037 struct wpa_driver_ap_params *params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007038{
7039 struct i802_bss *bss = priv;
7040 struct wpa_driver_nl80211_data *drv = bss->drv;
7041 struct nl_msg *msg;
7042 u8 cmd = NL80211_CMD_NEW_BEACON;
7043 int ret;
7044 int beacon_set;
7045 int ifindex = if_nametoindex(bss->ifname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007046 int num_suites;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007047 u32 suites[10], suite;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007048 u32 ver;
7049
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07007050 beacon_set = bss->beacon_set;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007051
7052 msg = nlmsg_alloc();
7053 if (!msg)
7054 return -ENOMEM;
7055
7056 wpa_printf(MSG_DEBUG, "nl80211: Set beacon (beacon_set=%d)",
7057 beacon_set);
7058 if (beacon_set)
7059 cmd = NL80211_CMD_SET_BEACON;
7060
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007061 nl80211_cmd(drv, msg, 0, cmd);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007062 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon head",
7063 params->head, params->head_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007064 NLA_PUT(msg, NL80211_ATTR_BEACON_HEAD, params->head_len, params->head);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007065 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon tail",
7066 params->tail, params->tail_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007067 NLA_PUT(msg, NL80211_ATTR_BEACON_TAIL, params->tail_len, params->tail);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007068 wpa_printf(MSG_DEBUG, "nl80211: ifindex=%d", ifindex);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007069 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007070 wpa_printf(MSG_DEBUG, "nl80211: beacon_int=%d", params->beacon_int);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007071 NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL, params->beacon_int);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007072 wpa_printf(MSG_DEBUG, "nl80211: dtim_period=%d", params->dtim_period);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007073 NLA_PUT_U32(msg, NL80211_ATTR_DTIM_PERIOD, params->dtim_period);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007074 wpa_hexdump_ascii(MSG_DEBUG, "nl80211: ssid",
7075 params->ssid, params->ssid_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007076 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
7077 params->ssid);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007078 if (params->proberesp && params->proberesp_len) {
7079 wpa_hexdump(MSG_DEBUG, "nl80211: proberesp (offload)",
7080 params->proberesp, params->proberesp_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007081 NLA_PUT(msg, NL80211_ATTR_PROBE_RESP, params->proberesp_len,
7082 params->proberesp);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007083 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007084 switch (params->hide_ssid) {
7085 case NO_SSID_HIDING:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007086 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID not in use");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007087 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
7088 NL80211_HIDDEN_SSID_NOT_IN_USE);
7089 break;
7090 case HIDDEN_SSID_ZERO_LEN:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007091 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero len");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007092 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
7093 NL80211_HIDDEN_SSID_ZERO_LEN);
7094 break;
7095 case HIDDEN_SSID_ZERO_CONTENTS:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007096 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero contents");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007097 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
7098 NL80211_HIDDEN_SSID_ZERO_CONTENTS);
7099 break;
7100 }
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007101 wpa_printf(MSG_DEBUG, "nl80211: privacy=%d", params->privacy);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007102 if (params->privacy)
7103 NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007104 wpa_printf(MSG_DEBUG, "nl80211: auth_algs=0x%x", params->auth_algs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007105 if ((params->auth_algs & (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) ==
7106 (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) {
7107 /* Leave out the attribute */
7108 } else if (params->auth_algs & WPA_AUTH_ALG_SHARED)
7109 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE,
7110 NL80211_AUTHTYPE_SHARED_KEY);
7111 else
7112 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE,
7113 NL80211_AUTHTYPE_OPEN_SYSTEM);
7114
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007115 wpa_printf(MSG_DEBUG, "nl80211: wpa_version=0x%x", params->wpa_version);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007116 ver = 0;
7117 if (params->wpa_version & WPA_PROTO_WPA)
7118 ver |= NL80211_WPA_VERSION_1;
7119 if (params->wpa_version & WPA_PROTO_RSN)
7120 ver |= NL80211_WPA_VERSION_2;
7121 if (ver)
7122 NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver);
7123
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007124 wpa_printf(MSG_DEBUG, "nl80211: key_mgmt_suites=0x%x",
7125 params->key_mgmt_suites);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007126 num_suites = 0;
7127 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X)
7128 suites[num_suites++] = WLAN_AKM_SUITE_8021X;
7129 if (params->key_mgmt_suites & WPA_KEY_MGMT_PSK)
7130 suites[num_suites++] = WLAN_AKM_SUITE_PSK;
7131 if (num_suites) {
7132 NLA_PUT(msg, NL80211_ATTR_AKM_SUITES,
7133 num_suites * sizeof(u32), suites);
7134 }
7135
7136 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X &&
7137 params->pairwise_ciphers & (WPA_CIPHER_WEP104 | WPA_CIPHER_WEP40))
7138 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT);
7139
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007140 wpa_printf(MSG_DEBUG, "nl80211: pairwise_ciphers=0x%x",
7141 params->pairwise_ciphers);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007142 num_suites = wpa_cipher_to_cipher_suites(params->pairwise_ciphers,
7143 suites, ARRAY_SIZE(suites));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007144 if (num_suites) {
7145 NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
7146 num_suites * sizeof(u32), suites);
7147 }
7148
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007149 wpa_printf(MSG_DEBUG, "nl80211: group_cipher=0x%x",
7150 params->group_cipher);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007151 suite = wpa_cipher_to_cipher_suite(params->group_cipher);
7152 if (suite)
7153 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, suite);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007154
7155 if (params->beacon_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007156 wpa_hexdump_buf(MSG_DEBUG, "nl80211: beacon_ies",
7157 params->beacon_ies);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007158 NLA_PUT(msg, NL80211_ATTR_IE, wpabuf_len(params->beacon_ies),
7159 wpabuf_head(params->beacon_ies));
7160 }
7161 if (params->proberesp_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007162 wpa_hexdump_buf(MSG_DEBUG, "nl80211: proberesp_ies",
7163 params->proberesp_ies);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007164 NLA_PUT(msg, NL80211_ATTR_IE_PROBE_RESP,
7165 wpabuf_len(params->proberesp_ies),
7166 wpabuf_head(params->proberesp_ies));
7167 }
7168 if (params->assocresp_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007169 wpa_hexdump_buf(MSG_DEBUG, "nl80211: assocresp_ies",
7170 params->assocresp_ies);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007171 NLA_PUT(msg, NL80211_ATTR_IE_ASSOC_RESP,
7172 wpabuf_len(params->assocresp_ies),
7173 wpabuf_head(params->assocresp_ies));
7174 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007175
Dmitry Shmidt04949592012-07-19 12:16:46 -07007176 if (drv->capa.flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007177 wpa_printf(MSG_DEBUG, "nl80211: ap_max_inactivity=%d",
7178 params->ap_max_inactivity);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007179 NLA_PUT_U16(msg, NL80211_ATTR_INACTIVITY_TIMEOUT,
7180 params->ap_max_inactivity);
7181 }
7182
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007183 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7184 if (ret) {
7185 wpa_printf(MSG_DEBUG, "nl80211: Beacon set failed: %d (%s)",
7186 ret, strerror(-ret));
7187 } else {
7188 bss->beacon_set = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007189 nl80211_set_bss(bss, params->cts_protect, params->preamble,
7190 params->short_slot_time, params->ht_opmode,
7191 params->isolate, params->basic_rates);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007192 }
7193 return ret;
7194 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007195 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007196 return -ENOBUFS;
7197}
7198
7199
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007200static int nl80211_put_freq_params(struct nl_msg *msg,
7201 struct hostapd_freq_params *freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007202{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007203 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq->freq);
7204 if (freq->vht_enabled) {
7205 switch (freq->bandwidth) {
7206 case 20:
7207 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
7208 NL80211_CHAN_WIDTH_20);
7209 break;
7210 case 40:
7211 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
7212 NL80211_CHAN_WIDTH_40);
7213 break;
7214 case 80:
7215 if (freq->center_freq2)
7216 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
7217 NL80211_CHAN_WIDTH_80P80);
7218 else
7219 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
7220 NL80211_CHAN_WIDTH_80);
7221 break;
7222 case 160:
7223 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
7224 NL80211_CHAN_WIDTH_160);
7225 break;
7226 default:
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007227 return -EINVAL;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007228 }
7229 NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ1, freq->center_freq1);
7230 if (freq->center_freq2)
7231 NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ2,
7232 freq->center_freq2);
7233 } else if (freq->ht_enabled) {
7234 switch (freq->sec_channel_offset) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007235 case -1:
7236 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
7237 NL80211_CHAN_HT40MINUS);
7238 break;
7239 case 1:
7240 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
7241 NL80211_CHAN_HT40PLUS);
7242 break;
7243 default:
7244 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
7245 NL80211_CHAN_HT20);
7246 break;
7247 }
7248 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007249 return 0;
7250
7251nla_put_failure:
7252 return -ENOBUFS;
7253}
7254
7255
7256static int wpa_driver_nl80211_set_freq(struct i802_bss *bss,
7257 struct hostapd_freq_params *freq)
7258{
7259 struct wpa_driver_nl80211_data *drv = bss->drv;
7260 struct nl_msg *msg;
7261 int ret;
7262
7263 wpa_printf(MSG_DEBUG,
7264 "nl80211: Set freq %d (ht_enabled=%d, vht_enabled=%d, bandwidth=%d MHz, cf1=%d MHz, cf2=%d MHz)",
7265 freq->freq, freq->ht_enabled, freq->vht_enabled,
7266 freq->bandwidth, freq->center_freq1, freq->center_freq2);
7267 msg = nlmsg_alloc();
7268 if (!msg)
7269 return -1;
7270
7271 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
7272
7273 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
7274 if (nl80211_put_freq_params(msg, freq) < 0)
7275 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007276
7277 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007278 msg = NULL;
7279 if (ret == 0) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007280 bss->freq = freq->freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007281 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007282 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007283 wpa_printf(MSG_DEBUG, "nl80211: Failed to set channel (freq=%d): "
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007284 "%d (%s)", freq->freq, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007285nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007286 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007287 return -1;
7288}
7289
7290
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007291static u32 sta_flags_nl80211(int flags)
7292{
7293 u32 f = 0;
7294
7295 if (flags & WPA_STA_AUTHORIZED)
7296 f |= BIT(NL80211_STA_FLAG_AUTHORIZED);
7297 if (flags & WPA_STA_WMM)
7298 f |= BIT(NL80211_STA_FLAG_WME);
7299 if (flags & WPA_STA_SHORT_PREAMBLE)
7300 f |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
7301 if (flags & WPA_STA_MFP)
7302 f |= BIT(NL80211_STA_FLAG_MFP);
7303 if (flags & WPA_STA_TDLS_PEER)
7304 f |= BIT(NL80211_STA_FLAG_TDLS_PEER);
7305
7306 return f;
7307}
7308
7309
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007310static int wpa_driver_nl80211_sta_add(void *priv,
7311 struct hostapd_sta_add_params *params)
7312{
7313 struct i802_bss *bss = priv;
7314 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007315 struct nl_msg *msg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007316 struct nl80211_sta_flag_update upd;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007317 int ret = -ENOBUFS;
7318
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007319 if ((params->flags & WPA_STA_TDLS_PEER) &&
7320 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
7321 return -EOPNOTSUPP;
7322
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007323 msg = nlmsg_alloc();
7324 if (!msg)
7325 return -ENOMEM;
7326
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007327 wpa_printf(MSG_DEBUG, "nl80211: %s STA " MACSTR,
7328 params->set ? "Set" : "Add", MAC2STR(params->addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007329 nl80211_cmd(drv, msg, 0, params->set ? NL80211_CMD_SET_STATION :
7330 NL80211_CMD_NEW_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007331
7332 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
7333 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007334 NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_RATES, params->supp_rates_len,
7335 params->supp_rates);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007336 wpa_hexdump(MSG_DEBUG, " * supported rates", params->supp_rates,
7337 params->supp_rates_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007338 if (!params->set) {
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07007339 if (params->aid) {
7340 wpa_printf(MSG_DEBUG, " * aid=%u", params->aid);
7341 NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, params->aid);
7342 } else {
7343 /*
7344 * cfg80211 validates that AID is non-zero, so we have
7345 * to make this a non-zero value for the TDLS case where
7346 * a dummy STA entry is used for now.
7347 */
7348 wpa_printf(MSG_DEBUG, " * aid=1 (TDLS workaround)");
7349 NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, 1);
7350 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007351 wpa_printf(MSG_DEBUG, " * listen_interval=%u",
7352 params->listen_interval);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007353 NLA_PUT_U16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
7354 params->listen_interval);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07007355 } else if (params->aid && (params->flags & WPA_STA_TDLS_PEER)) {
7356 wpa_printf(MSG_DEBUG, " * peer_aid=%u", params->aid);
7357 NLA_PUT_U16(msg, NL80211_ATTR_PEER_AID, params->aid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007358 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007359 if (params->ht_capabilities) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007360 wpa_hexdump(MSG_DEBUG, " * ht_capabilities",
7361 (u8 *) params->ht_capabilities,
7362 sizeof(*params->ht_capabilities));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007363 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY,
7364 sizeof(*params->ht_capabilities),
7365 params->ht_capabilities);
7366 }
7367
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007368 if (params->vht_capabilities) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007369 wpa_hexdump(MSG_DEBUG, " * vht_capabilities",
7370 (u8 *) params->vht_capabilities,
7371 sizeof(*params->vht_capabilities));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007372 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY,
7373 sizeof(*params->vht_capabilities),
7374 params->vht_capabilities);
7375 }
7376
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08007377 if (params->vht_opmode_enabled) {
7378 wpa_printf(MSG_DEBUG, " * opmode=%u", params->vht_opmode);
7379 NLA_PUT_U8(msg, NL80211_ATTR_OPMODE_NOTIF,
7380 params->vht_opmode);
7381 }
7382
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007383 wpa_printf(MSG_DEBUG, " * capability=0x%x", params->capability);
7384 NLA_PUT_U16(msg, NL80211_ATTR_STA_CAPABILITY, params->capability);
7385
7386 if (params->ext_capab) {
7387 wpa_hexdump(MSG_DEBUG, " * ext_capab",
7388 params->ext_capab, params->ext_capab_len);
7389 NLA_PUT(msg, NL80211_ATTR_STA_EXT_CAPABILITY,
7390 params->ext_capab_len, params->ext_capab);
7391 }
7392
Dmitry Shmidt344abd32014-01-14 13:17:00 -08007393 if (params->supp_channels) {
7394 wpa_hexdump(MSG_DEBUG, " * supported channels",
7395 params->supp_channels, params->supp_channels_len);
7396 NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_CHANNELS,
7397 params->supp_channels_len, params->supp_channels);
7398 }
7399
7400 if (params->supp_oper_classes) {
7401 wpa_hexdump(MSG_DEBUG, " * supported operating classes",
7402 params->supp_oper_classes,
7403 params->supp_oper_classes_len);
7404 NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES,
7405 params->supp_oper_classes_len,
7406 params->supp_oper_classes);
7407 }
7408
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007409 os_memset(&upd, 0, sizeof(upd));
7410 upd.mask = sta_flags_nl80211(params->flags);
7411 upd.set = upd.mask;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007412 wpa_printf(MSG_DEBUG, " * flags set=0x%x mask=0x%x",
7413 upd.set, upd.mask);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007414 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
7415
7416 if (params->flags & WPA_STA_WMM) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007417 struct nlattr *wme = nla_nest_start(msg, NL80211_ATTR_STA_WME);
7418
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007419 if (!wme)
7420 goto nla_put_failure;
7421
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007422 wpa_printf(MSG_DEBUG, " * qosinfo=0x%x", params->qosinfo);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007423 NLA_PUT_U8(msg, NL80211_STA_WME_UAPSD_QUEUES,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007424 params->qosinfo & WMM_QOSINFO_STA_AC_MASK);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007425 NLA_PUT_U8(msg, NL80211_STA_WME_MAX_SP,
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007426 (params->qosinfo >> WMM_QOSINFO_STA_SP_SHIFT) &
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007427 WMM_QOSINFO_STA_SP_MASK);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007428 nla_nest_end(msg, wme);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007429 }
7430
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007431 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007432 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007433 if (ret)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007434 wpa_printf(MSG_DEBUG, "nl80211: NL80211_CMD_%s_STATION "
7435 "result: %d (%s)", params->set ? "SET" : "NEW", ret,
7436 strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007437 if (ret == -EEXIST)
7438 ret = 0;
7439 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007440 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007441 return ret;
7442}
7443
7444
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007445static int wpa_driver_nl80211_sta_remove(struct i802_bss *bss, const u8 *addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007446{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007447 struct wpa_driver_nl80211_data *drv = bss->drv;
7448 struct nl_msg *msg;
7449 int ret;
7450
7451 msg = nlmsg_alloc();
7452 if (!msg)
7453 return -ENOMEM;
7454
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007455 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007456
7457 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
7458 if_nametoindex(bss->ifname));
7459 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
7460
7461 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007462 wpa_printf(MSG_DEBUG, "nl80211: sta_remove -> DEL_STATION %s " MACSTR
7463 " --> %d (%s)",
7464 bss->ifname, MAC2STR(addr), ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007465 if (ret == -ENOENT)
7466 return 0;
7467 return ret;
7468 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007469 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007470 return -ENOBUFS;
7471}
7472
7473
7474static void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv,
7475 int ifidx)
7476{
7477 struct nl_msg *msg;
7478
7479 wpa_printf(MSG_DEBUG, "nl80211: Remove interface ifindex=%d", ifidx);
7480
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007481 /* stop listening for EAPOL on this interface */
7482 del_ifidx(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007483
7484 msg = nlmsg_alloc();
7485 if (!msg)
7486 goto nla_put_failure;
7487
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007488 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_INTERFACE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007489 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifidx);
7490
7491 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
7492 return;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007493 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007494 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007495 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007496 wpa_printf(MSG_ERROR, "Failed to remove interface (ifidx=%d)", ifidx);
7497}
7498
7499
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007500static const char * nl80211_iftype_str(enum nl80211_iftype mode)
7501{
7502 switch (mode) {
7503 case NL80211_IFTYPE_ADHOC:
7504 return "ADHOC";
7505 case NL80211_IFTYPE_STATION:
7506 return "STATION";
7507 case NL80211_IFTYPE_AP:
7508 return "AP";
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07007509 case NL80211_IFTYPE_AP_VLAN:
7510 return "AP_VLAN";
7511 case NL80211_IFTYPE_WDS:
7512 return "WDS";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007513 case NL80211_IFTYPE_MONITOR:
7514 return "MONITOR";
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07007515 case NL80211_IFTYPE_MESH_POINT:
7516 return "MESH_POINT";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007517 case NL80211_IFTYPE_P2P_CLIENT:
7518 return "P2P_CLIENT";
7519 case NL80211_IFTYPE_P2P_GO:
7520 return "P2P_GO";
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007521 case NL80211_IFTYPE_P2P_DEVICE:
7522 return "P2P_DEVICE";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007523 default:
7524 return "unknown";
7525 }
7526}
7527
7528
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007529static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv,
7530 const char *ifname,
7531 enum nl80211_iftype iftype,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007532 const u8 *addr, int wds,
7533 int (*handler)(struct nl_msg *, void *),
7534 void *arg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007535{
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007536 struct nl_msg *msg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007537 int ifidx;
7538 int ret = -ENOBUFS;
7539
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007540 wpa_printf(MSG_DEBUG, "nl80211: Create interface iftype %d (%s)",
7541 iftype, nl80211_iftype_str(iftype));
7542
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007543 msg = nlmsg_alloc();
7544 if (!msg)
7545 return -1;
7546
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007547 nl80211_cmd(drv, msg, 0, NL80211_CMD_NEW_INTERFACE);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007548 if (nl80211_set_iface_id(msg, drv->first_bss) < 0)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007549 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007550 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, ifname);
7551 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, iftype);
7552
7553 if (iftype == NL80211_IFTYPE_MONITOR) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007554 struct nlattr *flags;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007555
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007556 flags = nla_nest_start(msg, NL80211_ATTR_MNTR_FLAGS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007557 if (!flags)
7558 goto nla_put_failure;
7559
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007560 NLA_PUT_FLAG(msg, NL80211_MNTR_FLAG_COOK_FRAMES);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007561
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007562 nla_nest_end(msg, flags);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007563 } else if (wds) {
7564 NLA_PUT_U8(msg, NL80211_ATTR_4ADDR, wds);
7565 }
7566
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007567 ret = send_and_recv_msgs(drv, msg, handler, arg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007568 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007569 if (ret) {
7570 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007571 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007572 wpa_printf(MSG_ERROR, "Failed to create interface %s: %d (%s)",
7573 ifname, ret, strerror(-ret));
7574 return ret;
7575 }
7576
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007577 if (iftype == NL80211_IFTYPE_P2P_DEVICE)
7578 return 0;
7579
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007580 ifidx = if_nametoindex(ifname);
7581 wpa_printf(MSG_DEBUG, "nl80211: New interface %s created: ifindex=%d",
7582 ifname, ifidx);
7583
7584 if (ifidx <= 0)
7585 return -1;
7586
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007587 /* start listening for EAPOL on this interface */
7588 add_ifidx(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007589
7590 if (addr && iftype != NL80211_IFTYPE_MONITOR &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007591 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname, addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007592 nl80211_remove_iface(drv, ifidx);
7593 return -1;
7594 }
7595
7596 return ifidx;
7597}
7598
7599
7600static int nl80211_create_iface(struct wpa_driver_nl80211_data *drv,
7601 const char *ifname, enum nl80211_iftype iftype,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007602 const u8 *addr, int wds,
7603 int (*handler)(struct nl_msg *, void *),
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007604 void *arg, int use_existing)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007605{
7606 int ret;
7607
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007608 ret = nl80211_create_iface_once(drv, ifname, iftype, addr, wds, handler,
7609 arg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007610
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007611 /* if error occurred and interface exists already */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007612 if (ret == -ENFILE && if_nametoindex(ifname)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007613 if (use_existing) {
7614 wpa_printf(MSG_DEBUG, "nl80211: Continue using existing interface %s",
7615 ifname);
7616 return -ENFILE;
7617 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007618 wpa_printf(MSG_INFO, "Try to remove and re-create %s", ifname);
7619
7620 /* Try to remove the interface that was already there. */
7621 nl80211_remove_iface(drv, if_nametoindex(ifname));
7622
7623 /* Try to create the interface again */
7624 ret = nl80211_create_iface_once(drv, ifname, iftype, addr,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007625 wds, handler, arg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007626 }
7627
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007628 if (ret >= 0 && is_p2p_net_interface(iftype))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007629 nl80211_disable_11b_rates(drv, ret, 1);
7630
7631 return ret;
7632}
7633
7634
7635static void handle_tx_callback(void *ctx, u8 *buf, size_t len, int ok)
7636{
7637 struct ieee80211_hdr *hdr;
7638 u16 fc;
7639 union wpa_event_data event;
7640
7641 hdr = (struct ieee80211_hdr *) buf;
7642 fc = le_to_host16(hdr->frame_control);
7643
7644 os_memset(&event, 0, sizeof(event));
7645 event.tx_status.type = WLAN_FC_GET_TYPE(fc);
7646 event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
7647 event.tx_status.dst = hdr->addr1;
7648 event.tx_status.data = buf;
7649 event.tx_status.data_len = len;
7650 event.tx_status.ack = ok;
7651 wpa_supplicant_event(ctx, EVENT_TX_STATUS, &event);
7652}
7653
7654
7655static void from_unknown_sta(struct wpa_driver_nl80211_data *drv,
7656 u8 *buf, size_t len)
7657{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007658 struct ieee80211_hdr *hdr = (void *)buf;
7659 u16 fc;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007660 union wpa_event_data event;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007661
7662 if (len < sizeof(*hdr))
7663 return;
7664
7665 fc = le_to_host16(hdr->frame_control);
7666
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007667 os_memset(&event, 0, sizeof(event));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007668 event.rx_from_unknown.bssid = get_hdr_bssid(hdr, len);
7669 event.rx_from_unknown.addr = hdr->addr2;
7670 event.rx_from_unknown.wds = (fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) ==
7671 (WLAN_FC_FROMDS | WLAN_FC_TODS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007672 wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event);
7673}
7674
7675
7676static void handle_frame(struct wpa_driver_nl80211_data *drv,
7677 u8 *buf, size_t len, int datarate, int ssi_signal)
7678{
7679 struct ieee80211_hdr *hdr;
7680 u16 fc;
7681 union wpa_event_data event;
7682
7683 hdr = (struct ieee80211_hdr *) buf;
7684 fc = le_to_host16(hdr->frame_control);
7685
7686 switch (WLAN_FC_GET_TYPE(fc)) {
7687 case WLAN_FC_TYPE_MGMT:
7688 os_memset(&event, 0, sizeof(event));
7689 event.rx_mgmt.frame = buf;
7690 event.rx_mgmt.frame_len = len;
7691 event.rx_mgmt.datarate = datarate;
7692 event.rx_mgmt.ssi_signal = ssi_signal;
7693 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
7694 break;
7695 case WLAN_FC_TYPE_CTRL:
7696 /* can only get here with PS-Poll frames */
7697 wpa_printf(MSG_DEBUG, "CTRL");
7698 from_unknown_sta(drv, buf, len);
7699 break;
7700 case WLAN_FC_TYPE_DATA:
7701 from_unknown_sta(drv, buf, len);
7702 break;
7703 }
7704}
7705
7706
7707static void handle_monitor_read(int sock, void *eloop_ctx, void *sock_ctx)
7708{
7709 struct wpa_driver_nl80211_data *drv = eloop_ctx;
7710 int len;
7711 unsigned char buf[3000];
7712 struct ieee80211_radiotap_iterator iter;
7713 int ret;
7714 int datarate = 0, ssi_signal = 0;
7715 int injected = 0, failed = 0, rxflags = 0;
7716
7717 len = recv(sock, buf, sizeof(buf), 0);
7718 if (len < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007719 wpa_printf(MSG_ERROR, "nl80211: Monitor socket recv failed: %s",
7720 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007721 return;
7722 }
7723
7724 if (ieee80211_radiotap_iterator_init(&iter, (void*)buf, len)) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007725 wpa_printf(MSG_INFO, "nl80211: received invalid radiotap frame");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007726 return;
7727 }
7728
7729 while (1) {
7730 ret = ieee80211_radiotap_iterator_next(&iter);
7731 if (ret == -ENOENT)
7732 break;
7733 if (ret) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007734 wpa_printf(MSG_INFO, "nl80211: received invalid radiotap frame (%d)",
7735 ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007736 return;
7737 }
7738 switch (iter.this_arg_index) {
7739 case IEEE80211_RADIOTAP_FLAGS:
7740 if (*iter.this_arg & IEEE80211_RADIOTAP_F_FCS)
7741 len -= 4;
7742 break;
7743 case IEEE80211_RADIOTAP_RX_FLAGS:
7744 rxflags = 1;
7745 break;
7746 case IEEE80211_RADIOTAP_TX_FLAGS:
7747 injected = 1;
7748 failed = le_to_host16((*(uint16_t *) iter.this_arg)) &
7749 IEEE80211_RADIOTAP_F_TX_FAIL;
7750 break;
7751 case IEEE80211_RADIOTAP_DATA_RETRIES:
7752 break;
7753 case IEEE80211_RADIOTAP_CHANNEL:
7754 /* TODO: convert from freq/flags to channel number */
7755 break;
7756 case IEEE80211_RADIOTAP_RATE:
7757 datarate = *iter.this_arg * 5;
7758 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007759 case IEEE80211_RADIOTAP_DBM_ANTSIGNAL:
7760 ssi_signal = (s8) *iter.this_arg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007761 break;
7762 }
7763 }
7764
7765 if (rxflags && injected)
7766 return;
7767
7768 if (!injected)
7769 handle_frame(drv, buf + iter.max_length,
7770 len - iter.max_length, datarate, ssi_signal);
7771 else
7772 handle_tx_callback(drv->ctx, buf + iter.max_length,
7773 len - iter.max_length, !failed);
7774}
7775
7776
7777/*
7778 * we post-process the filter code later and rewrite
7779 * this to the offset to the last instruction
7780 */
7781#define PASS 0xFF
7782#define FAIL 0xFE
7783
7784static struct sock_filter msock_filter_insns[] = {
7785 /*
7786 * do a little-endian load of the radiotap length field
7787 */
7788 /* load lower byte into A */
7789 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 2),
7790 /* put it into X (== index register) */
7791 BPF_STMT(BPF_MISC| BPF_TAX, 0),
7792 /* load upper byte into A */
7793 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 3),
7794 /* left-shift it by 8 */
7795 BPF_STMT(BPF_ALU | BPF_LSH | BPF_K, 8),
7796 /* or with X */
7797 BPF_STMT(BPF_ALU | BPF_OR | BPF_X, 0),
7798 /* put result into X */
7799 BPF_STMT(BPF_MISC| BPF_TAX, 0),
7800
7801 /*
7802 * Allow management frames through, this also gives us those
7803 * management frames that we sent ourselves with status
7804 */
7805 /* load the lower byte of the IEEE 802.11 frame control field */
7806 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
7807 /* mask off frame type and version */
7808 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0xF),
7809 /* accept frame if it's both 0, fall through otherwise */
7810 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0, PASS, 0),
7811
7812 /*
7813 * TODO: add a bit to radiotap RX flags that indicates
7814 * that the sending station is not associated, then
7815 * add a filter here that filters on our DA and that flag
7816 * to allow us to deauth frames to that bad station.
7817 *
7818 * For now allow all To DS data frames through.
7819 */
7820 /* load the IEEE 802.11 frame control field */
7821 BPF_STMT(BPF_LD | BPF_H | BPF_IND, 0),
7822 /* mask off frame type, version and DS status */
7823 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0F03),
7824 /* accept frame if version 0, type 2 and To DS, fall through otherwise
7825 */
7826 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0801, PASS, 0),
7827
7828#if 0
7829 /*
7830 * drop non-data frames
7831 */
7832 /* load the lower byte of the frame control field */
7833 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
7834 /* mask off QoS bit */
7835 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0c),
7836 /* drop non-data frames */
7837 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 8, 0, FAIL),
7838#endif
7839 /* load the upper byte of the frame control field */
7840 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 1),
7841 /* mask off toDS/fromDS */
7842 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x03),
7843 /* accept WDS frames */
7844 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 3, PASS, 0),
7845
7846 /*
7847 * add header length to index
7848 */
7849 /* load the lower byte of the frame control field */
7850 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
7851 /* mask off QoS bit */
7852 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x80),
7853 /* right shift it by 6 to give 0 or 2 */
7854 BPF_STMT(BPF_ALU | BPF_RSH | BPF_K, 6),
7855 /* add data frame header length */
7856 BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 24),
7857 /* add index, was start of 802.11 header */
7858 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
7859 /* move to index, now start of LL header */
7860 BPF_STMT(BPF_MISC | BPF_TAX, 0),
7861
7862 /*
7863 * Accept empty data frames, we use those for
7864 * polling activity.
7865 */
7866 BPF_STMT(BPF_LD | BPF_W | BPF_LEN, 0),
7867 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_X, 0, PASS, 0),
7868
7869 /*
7870 * Accept EAPOL frames
7871 */
7872 BPF_STMT(BPF_LD | BPF_W | BPF_IND, 0),
7873 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0xAAAA0300, 0, FAIL),
7874 BPF_STMT(BPF_LD | BPF_W | BPF_IND, 4),
7875 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0000888E, PASS, FAIL),
7876
7877 /* keep these last two statements or change the code below */
7878 /* return 0 == "DROP" */
7879 BPF_STMT(BPF_RET | BPF_K, 0),
7880 /* return ~0 == "keep all" */
7881 BPF_STMT(BPF_RET | BPF_K, ~0),
7882};
7883
7884static struct sock_fprog msock_filter = {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007885 .len = ARRAY_SIZE(msock_filter_insns),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007886 .filter = msock_filter_insns,
7887};
7888
7889
7890static int add_monitor_filter(int s)
7891{
7892 int idx;
7893
7894 /* rewrite all PASS/FAIL jump offsets */
7895 for (idx = 0; idx < msock_filter.len; idx++) {
7896 struct sock_filter *insn = &msock_filter_insns[idx];
7897
7898 if (BPF_CLASS(insn->code) == BPF_JMP) {
7899 if (insn->code == (BPF_JMP|BPF_JA)) {
7900 if (insn->k == PASS)
7901 insn->k = msock_filter.len - idx - 2;
7902 else if (insn->k == FAIL)
7903 insn->k = msock_filter.len - idx - 3;
7904 }
7905
7906 if (insn->jt == PASS)
7907 insn->jt = msock_filter.len - idx - 2;
7908 else if (insn->jt == FAIL)
7909 insn->jt = msock_filter.len - idx - 3;
7910
7911 if (insn->jf == PASS)
7912 insn->jf = msock_filter.len - idx - 2;
7913 else if (insn->jf == FAIL)
7914 insn->jf = msock_filter.len - idx - 3;
7915 }
7916 }
7917
7918 if (setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER,
7919 &msock_filter, sizeof(msock_filter))) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007920 wpa_printf(MSG_ERROR, "nl80211: setsockopt(SO_ATTACH_FILTER) failed: %s",
7921 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007922 return -1;
7923 }
7924
7925 return 0;
7926}
7927
7928
7929static void nl80211_remove_monitor_interface(
7930 struct wpa_driver_nl80211_data *drv)
7931{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007932 if (drv->monitor_refcount > 0)
7933 drv->monitor_refcount--;
7934 wpa_printf(MSG_DEBUG, "nl80211: Remove monitor interface: refcount=%d",
7935 drv->monitor_refcount);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007936 if (drv->monitor_refcount > 0)
7937 return;
7938
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007939 if (drv->monitor_ifidx >= 0) {
7940 nl80211_remove_iface(drv, drv->monitor_ifidx);
7941 drv->monitor_ifidx = -1;
7942 }
7943 if (drv->monitor_sock >= 0) {
7944 eloop_unregister_read_sock(drv->monitor_sock);
7945 close(drv->monitor_sock);
7946 drv->monitor_sock = -1;
7947 }
7948}
7949
7950
7951static int
7952nl80211_create_monitor_interface(struct wpa_driver_nl80211_data *drv)
7953{
7954 char buf[IFNAMSIZ];
7955 struct sockaddr_ll ll;
7956 int optval;
7957 socklen_t optlen;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007958
7959 if (drv->monitor_ifidx >= 0) {
7960 drv->monitor_refcount++;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007961 wpa_printf(MSG_DEBUG, "nl80211: Re-use existing monitor interface: refcount=%d",
7962 drv->monitor_refcount);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007963 return 0;
7964 }
7965
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007966 if (os_strncmp(drv->first_bss->ifname, "p2p-", 4) == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007967 /*
7968 * P2P interface name is of the format p2p-%s-%d. For monitor
7969 * interface name corresponding to P2P GO, replace "p2p-" with
7970 * "mon-" to retain the same interface name length and to
7971 * indicate that it is a monitor interface.
7972 */
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007973 snprintf(buf, IFNAMSIZ, "mon-%s", drv->first_bss->ifname + 4);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007974 } else {
7975 /* Non-P2P interface with AP functionality. */
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007976 snprintf(buf, IFNAMSIZ, "mon.%s", drv->first_bss->ifname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007977 }
7978
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007979 buf[IFNAMSIZ - 1] = '\0';
7980
7981 drv->monitor_ifidx =
7982 nl80211_create_iface(drv, buf, NL80211_IFTYPE_MONITOR, NULL,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007983 0, NULL, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007984
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07007985 if (drv->monitor_ifidx == -EOPNOTSUPP) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007986 /*
7987 * This is backward compatibility for a few versions of
7988 * the kernel only that didn't advertise the right
7989 * attributes for the only driver that then supported
7990 * AP mode w/o monitor -- ath6kl.
7991 */
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07007992 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support "
7993 "monitor interface type - try to run without it");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007994 drv->device_ap_sme = 1;
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07007995 }
7996
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007997 if (drv->monitor_ifidx < 0)
7998 return -1;
7999
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008000 if (linux_set_iface_flags(drv->global->ioctl_sock, buf, 1))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008001 goto error;
8002
8003 memset(&ll, 0, sizeof(ll));
8004 ll.sll_family = AF_PACKET;
8005 ll.sll_ifindex = drv->monitor_ifidx;
8006 drv->monitor_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
8007 if (drv->monitor_sock < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008008 wpa_printf(MSG_ERROR, "nl80211: socket[PF_PACKET,SOCK_RAW] failed: %s",
8009 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008010 goto error;
8011 }
8012
8013 if (add_monitor_filter(drv->monitor_sock)) {
8014 wpa_printf(MSG_INFO, "Failed to set socket filter for monitor "
8015 "interface; do filtering in user space");
8016 /* This works, but will cost in performance. */
8017 }
8018
8019 if (bind(drv->monitor_sock, (struct sockaddr *) &ll, sizeof(ll)) < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008020 wpa_printf(MSG_ERROR, "nl80211: monitor socket bind failed: %s",
8021 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008022 goto error;
8023 }
8024
8025 optlen = sizeof(optval);
8026 optval = 20;
8027 if (setsockopt
8028 (drv->monitor_sock, SOL_SOCKET, SO_PRIORITY, &optval, optlen)) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008029 wpa_printf(MSG_ERROR, "nl80211: Failed to set socket priority: %s",
8030 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008031 goto error;
8032 }
8033
8034 if (eloop_register_read_sock(drv->monitor_sock, handle_monitor_read,
8035 drv, NULL)) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008036 wpa_printf(MSG_INFO, "nl80211: Could not register monitor read socket");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008037 goto error;
8038 }
8039
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008040 drv->monitor_refcount++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008041 return 0;
8042 error:
8043 nl80211_remove_monitor_interface(drv);
8044 return -1;
8045}
8046
8047
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008048static int nl80211_setup_ap(struct i802_bss *bss)
8049{
8050 struct wpa_driver_nl80211_data *drv = bss->drv;
8051
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008052 wpa_printf(MSG_DEBUG, "nl80211: Setup AP(%s) - device_ap_sme=%d use_monitor=%d",
8053 bss->ifname, drv->device_ap_sme, drv->use_monitor);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008054
8055 /*
8056 * Disable Probe Request reporting unless we need it in this way for
8057 * devices that include the AP SME, in the other case (unless using
8058 * monitor iface) we'll get it through the nl_mgmt socket instead.
8059 */
8060 if (!drv->device_ap_sme)
8061 wpa_driver_nl80211_probe_req_report(bss, 0);
8062
8063 if (!drv->device_ap_sme && !drv->use_monitor)
8064 if (nl80211_mgmt_subscribe_ap(bss))
8065 return -1;
8066
8067 if (drv->device_ap_sme && !drv->use_monitor)
8068 if (nl80211_mgmt_subscribe_ap_dev_sme(bss))
8069 return -1;
8070
8071 if (!drv->device_ap_sme && drv->use_monitor &&
8072 nl80211_create_monitor_interface(drv) &&
8073 !drv->device_ap_sme)
Dmitry Shmidt04949592012-07-19 12:16:46 -07008074 return -1;
8075
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008076 if (drv->device_ap_sme &&
8077 wpa_driver_nl80211_probe_req_report(bss, 1) < 0) {
8078 wpa_printf(MSG_DEBUG, "nl80211: Failed to enable "
8079 "Probe Request frame reporting in AP mode");
8080 /* Try to survive without this */
8081 }
8082
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008083 return 0;
8084}
8085
8086
8087static void nl80211_teardown_ap(struct i802_bss *bss)
8088{
8089 struct wpa_driver_nl80211_data *drv = bss->drv;
8090
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008091 wpa_printf(MSG_DEBUG, "nl80211: Teardown AP(%s) - device_ap_sme=%d use_monitor=%d",
8092 bss->ifname, drv->device_ap_sme, drv->use_monitor);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008093 if (drv->device_ap_sme) {
8094 wpa_driver_nl80211_probe_req_report(bss, 0);
8095 if (!drv->use_monitor)
8096 nl80211_mgmt_unsubscribe(bss, "AP teardown (dev SME)");
8097 } else if (drv->use_monitor)
8098 nl80211_remove_monitor_interface(drv);
8099 else
8100 nl80211_mgmt_unsubscribe(bss, "AP teardown");
8101
8102 bss->beacon_set = 0;
8103}
8104
8105
8106static int nl80211_send_eapol_data(struct i802_bss *bss,
8107 const u8 *addr, const u8 *data,
8108 size_t data_len)
8109{
8110 struct sockaddr_ll ll;
8111 int ret;
8112
8113 if (bss->drv->eapol_tx_sock < 0) {
8114 wpa_printf(MSG_DEBUG, "nl80211: No socket to send EAPOL");
8115 return -1;
8116 }
8117
8118 os_memset(&ll, 0, sizeof(ll));
8119 ll.sll_family = AF_PACKET;
8120 ll.sll_ifindex = bss->ifindex;
8121 ll.sll_protocol = htons(ETH_P_PAE);
8122 ll.sll_halen = ETH_ALEN;
8123 os_memcpy(ll.sll_addr, addr, ETH_ALEN);
8124 ret = sendto(bss->drv->eapol_tx_sock, data, data_len, 0,
8125 (struct sockaddr *) &ll, sizeof(ll));
8126 if (ret < 0)
8127 wpa_printf(MSG_ERROR, "nl80211: EAPOL TX: %s",
8128 strerror(errno));
8129
8130 return ret;
8131}
8132
8133
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008134static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
8135
8136static int wpa_driver_nl80211_hapd_send_eapol(
8137 void *priv, const u8 *addr, const u8 *data,
8138 size_t data_len, int encrypt, const u8 *own_addr, u32 flags)
8139{
8140 struct i802_bss *bss = priv;
8141 struct wpa_driver_nl80211_data *drv = bss->drv;
8142 struct ieee80211_hdr *hdr;
8143 size_t len;
8144 u8 *pos;
8145 int res;
8146 int qos = flags & WPA_STA_WMM;
Dmitry Shmidt641185e2013-11-06 15:17:13 -08008147
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008148 if (drv->device_ap_sme || !drv->use_monitor)
8149 return nl80211_send_eapol_data(bss, addr, data, data_len);
8150
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008151 len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
8152 data_len;
8153 hdr = os_zalloc(len);
8154 if (hdr == NULL) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008155 wpa_printf(MSG_INFO, "nl80211: Failed to allocate EAPOL buffer(len=%lu)",
8156 (unsigned long) len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008157 return -1;
8158 }
8159
8160 hdr->frame_control =
8161 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
8162 hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
8163 if (encrypt)
8164 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
8165 if (qos) {
8166 hdr->frame_control |=
8167 host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
8168 }
8169
8170 memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
8171 memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
8172 memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
8173 pos = (u8 *) (hdr + 1);
8174
8175 if (qos) {
Dmitry Shmidtaa532512012-09-24 10:35:31 -07008176 /* Set highest priority in QoS header */
8177 pos[0] = 7;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008178 pos[1] = 0;
8179 pos += 2;
8180 }
8181
8182 memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
8183 pos += sizeof(rfc1042_header);
8184 WPA_PUT_BE16(pos, ETH_P_PAE);
8185 pos += 2;
8186 memcpy(pos, data, data_len);
8187
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008188 res = wpa_driver_nl80211_send_frame(bss, (u8 *) hdr, len, encrypt, 0,
8189 0, 0, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008190 if (res < 0) {
8191 wpa_printf(MSG_ERROR, "i802_send_eapol - packet len: %lu - "
8192 "failed: %d (%s)",
8193 (unsigned long) len, errno, strerror(errno));
8194 }
8195 os_free(hdr);
8196
8197 return res;
8198}
8199
8200
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008201static int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr,
8202 int total_flags,
8203 int flags_or, int flags_and)
8204{
8205 struct i802_bss *bss = priv;
8206 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008207 struct nl_msg *msg;
8208 struct nlattr *flags;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008209 struct nl80211_sta_flag_update upd;
8210
8211 msg = nlmsg_alloc();
8212 if (!msg)
8213 return -ENOMEM;
8214
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008215 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008216
8217 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
8218 if_nametoindex(bss->ifname));
8219 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
8220
8221 /*
8222 * Backwards compatibility version using NL80211_ATTR_STA_FLAGS. This
8223 * can be removed eventually.
8224 */
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008225 flags = nla_nest_start(msg, NL80211_ATTR_STA_FLAGS);
8226 if (!flags)
8227 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008228 if (total_flags & WPA_STA_AUTHORIZED)
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008229 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_AUTHORIZED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008230
8231 if (total_flags & WPA_STA_WMM)
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008232 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_WME);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008233
8234 if (total_flags & WPA_STA_SHORT_PREAMBLE)
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008235 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_SHORT_PREAMBLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008236
8237 if (total_flags & WPA_STA_MFP)
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008238 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_MFP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008239
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008240 if (total_flags & WPA_STA_TDLS_PEER)
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008241 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_TDLS_PEER);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008242
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008243 nla_nest_end(msg, flags);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008244
8245 os_memset(&upd, 0, sizeof(upd));
8246 upd.mask = sta_flags_nl80211(flags_or | ~flags_and);
8247 upd.set = sta_flags_nl80211(flags_or);
8248 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
8249
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008250 return send_and_recv_msgs(drv, msg, NULL, NULL);
8251 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008252 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008253 return -ENOBUFS;
8254}
8255
8256
8257static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv,
8258 struct wpa_driver_associate_params *params)
8259{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008260 enum nl80211_iftype nlmode, old_mode;
8261 struct hostapd_freq_params freq = {
8262 .freq = params->freq,
8263 };
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008264
8265 if (params->p2p) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008266 wpa_printf(MSG_DEBUG, "nl80211: Setup AP operations for P2P "
8267 "group (GO)");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008268 nlmode = NL80211_IFTYPE_P2P_GO;
8269 } else
8270 nlmode = NL80211_IFTYPE_AP;
8271
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008272 old_mode = drv->nlmode;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008273 if (wpa_driver_nl80211_set_mode(drv->first_bss, nlmode)) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008274 nl80211_remove_monitor_interface(drv);
8275 return -1;
8276 }
8277
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008278 if (wpa_driver_nl80211_set_freq(drv->first_bss, &freq)) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008279 if (old_mode != nlmode)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008280 wpa_driver_nl80211_set_mode(drv->first_bss, old_mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008281 nl80211_remove_monitor_interface(drv);
8282 return -1;
8283 }
8284
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008285 return 0;
8286}
8287
8288
8289static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv)
8290{
8291 struct nl_msg *msg;
8292 int ret = -1;
8293
8294 msg = nlmsg_alloc();
8295 if (!msg)
8296 return -1;
8297
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008298 nl80211_cmd(drv, msg, 0, NL80211_CMD_LEAVE_IBSS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008299 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
8300 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8301 msg = NULL;
8302 if (ret) {
8303 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS failed: ret=%d "
8304 "(%s)", ret, strerror(-ret));
8305 goto nla_put_failure;
8306 }
8307
8308 ret = 0;
8309 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS request sent successfully");
8310
8311nla_put_failure:
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008312 if (wpa_driver_nl80211_set_mode(drv->first_bss,
Dmitry Shmidt56052862013-10-04 10:23:25 -07008313 NL80211_IFTYPE_STATION)) {
8314 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
8315 "station mode");
8316 }
8317
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008318 nlmsg_free(msg);
8319 return ret;
8320}
8321
8322
8323static int wpa_driver_nl80211_ibss(struct wpa_driver_nl80211_data *drv,
8324 struct wpa_driver_associate_params *params)
8325{
8326 struct nl_msg *msg;
8327 int ret = -1;
8328 int count = 0;
8329
8330 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS (ifindex=%d)", drv->ifindex);
8331
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008332 if (wpa_driver_nl80211_set_mode(drv->first_bss,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008333 NL80211_IFTYPE_ADHOC)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008334 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
8335 "IBSS mode");
8336 return -1;
8337 }
8338
8339retry:
8340 msg = nlmsg_alloc();
8341 if (!msg)
8342 return -1;
8343
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008344 nl80211_cmd(drv, msg, 0, NL80211_CMD_JOIN_IBSS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008345 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
8346
8347 if (params->ssid == NULL || params->ssid_len > sizeof(drv->ssid))
8348 goto nla_put_failure;
8349
8350 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
8351 params->ssid, params->ssid_len);
8352 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
8353 params->ssid);
8354 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
8355 drv->ssid_len = params->ssid_len;
8356
8357 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
8358 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
8359
8360 ret = nl80211_set_conn_keys(params, msg);
8361 if (ret)
8362 goto nla_put_failure;
8363
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008364 if (params->bssid && params->fixed_bssid) {
8365 wpa_printf(MSG_DEBUG, " * BSSID=" MACSTR,
8366 MAC2STR(params->bssid));
8367 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
8368 }
8369
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008370 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
8371 params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
8372 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
8373 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008374 wpa_printf(MSG_DEBUG, " * control port");
8375 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT);
8376 }
8377
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008378 if (params->wpa_ie) {
8379 wpa_hexdump(MSG_DEBUG,
8380 " * Extra IEs for Beacon/Probe Response frames",
8381 params->wpa_ie, params->wpa_ie_len);
8382 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
8383 params->wpa_ie);
8384 }
8385
8386 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8387 msg = NULL;
8388 if (ret) {
8389 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS failed: ret=%d (%s)",
8390 ret, strerror(-ret));
8391 count++;
8392 if (ret == -EALREADY && count == 1) {
8393 wpa_printf(MSG_DEBUG, "nl80211: Retry IBSS join after "
8394 "forced leave");
8395 nl80211_leave_ibss(drv);
8396 nlmsg_free(msg);
8397 goto retry;
8398 }
8399
8400 goto nla_put_failure;
8401 }
8402 ret = 0;
8403 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS request sent successfully");
8404
8405nla_put_failure:
8406 nlmsg_free(msg);
8407 return ret;
8408}
8409
8410
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008411static int nl80211_connect_common(struct wpa_driver_nl80211_data *drv,
8412 struct wpa_driver_associate_params *params,
8413 struct nl_msg *msg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008414{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008415 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008416
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008417 if (params->bssid) {
8418 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
8419 MAC2STR(params->bssid));
8420 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
8421 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008422
Dmitry Shmidt96be6222014-02-13 10:16:51 -08008423 if (params->bssid_hint) {
8424 wpa_printf(MSG_DEBUG, " * bssid_hint=" MACSTR,
8425 MAC2STR(params->bssid_hint));
8426 NLA_PUT(msg, NL80211_ATTR_MAC_HINT, ETH_ALEN,
8427 params->bssid_hint);
8428 }
8429
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008430 if (params->freq) {
8431 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
8432 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008433 drv->assoc_freq = params->freq;
8434 } else
8435 drv->assoc_freq = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008436
Dmitry Shmidt96be6222014-02-13 10:16:51 -08008437 if (params->freq_hint) {
8438 wpa_printf(MSG_DEBUG, " * freq_hint=%d", params->freq_hint);
8439 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ_HINT,
8440 params->freq_hint);
8441 }
8442
Dmitry Shmidt04949592012-07-19 12:16:46 -07008443 if (params->bg_scan_period >= 0) {
8444 wpa_printf(MSG_DEBUG, " * bg scan period=%d",
8445 params->bg_scan_period);
8446 NLA_PUT_U16(msg, NL80211_ATTR_BG_SCAN_PERIOD,
8447 params->bg_scan_period);
8448 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008449
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008450 if (params->ssid) {
8451 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
8452 params->ssid, params->ssid_len);
8453 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
8454 params->ssid);
8455 if (params->ssid_len > sizeof(drv->ssid))
8456 goto nla_put_failure;
8457 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
8458 drv->ssid_len = params->ssid_len;
8459 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008460
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008461 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
8462 if (params->wpa_ie)
8463 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
8464 params->wpa_ie);
8465
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008466 if (params->wpa_proto) {
8467 enum nl80211_wpa_versions ver = 0;
8468
8469 if (params->wpa_proto & WPA_PROTO_WPA)
8470 ver |= NL80211_WPA_VERSION_1;
8471 if (params->wpa_proto & WPA_PROTO_RSN)
8472 ver |= NL80211_WPA_VERSION_2;
8473
8474 wpa_printf(MSG_DEBUG, " * WPA Versions 0x%x", ver);
8475 NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver);
8476 }
8477
8478 if (params->pairwise_suite != WPA_CIPHER_NONE) {
8479 u32 cipher = wpa_cipher_to_cipher_suite(params->pairwise_suite);
8480 wpa_printf(MSG_DEBUG, " * pairwise=0x%x", cipher);
8481 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE, cipher);
8482 }
8483
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08008484 if (params->group_suite == WPA_CIPHER_GTK_NOT_USED &&
8485 !(drv->capa.enc & WPA_DRIVER_CAPA_ENC_GTK_NOT_USED)) {
8486 /*
8487 * This is likely to work even though many drivers do not
8488 * advertise support for operations without GTK.
8489 */
8490 wpa_printf(MSG_DEBUG, " * skip group cipher configuration for GTK_NOT_USED due to missing driver support advertisement");
8491 } else if (params->group_suite != WPA_CIPHER_NONE) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008492 u32 cipher = wpa_cipher_to_cipher_suite(params->group_suite);
8493 wpa_printf(MSG_DEBUG, " * group=0x%x", cipher);
8494 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher);
8495 }
8496
8497 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
8498 params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
8499 params->key_mgmt_suite == WPA_KEY_MGMT_FT_IEEE8021X ||
8500 params->key_mgmt_suite == WPA_KEY_MGMT_FT_PSK ||
8501 params->key_mgmt_suite == WPA_KEY_MGMT_CCKM) {
8502 int mgmt = WLAN_AKM_SUITE_PSK;
8503
8504 switch (params->key_mgmt_suite) {
8505 case WPA_KEY_MGMT_CCKM:
8506 mgmt = WLAN_AKM_SUITE_CCKM;
8507 break;
8508 case WPA_KEY_MGMT_IEEE8021X:
8509 mgmt = WLAN_AKM_SUITE_8021X;
8510 break;
8511 case WPA_KEY_MGMT_FT_IEEE8021X:
8512 mgmt = WLAN_AKM_SUITE_FT_8021X;
8513 break;
8514 case WPA_KEY_MGMT_FT_PSK:
8515 mgmt = WLAN_AKM_SUITE_FT_PSK;
8516 break;
8517 case WPA_KEY_MGMT_PSK:
8518 default:
8519 mgmt = WLAN_AKM_SUITE_PSK;
8520 break;
8521 }
8522 NLA_PUT_U32(msg, NL80211_ATTR_AKM_SUITES, mgmt);
8523 }
8524
8525 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT);
8526
8527 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED)
8528 NLA_PUT_U32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED);
8529
8530 if (params->disable_ht)
8531 NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_HT);
8532
8533 if (params->htcaps && params->htcaps_mask) {
8534 int sz = sizeof(struct ieee80211_ht_capabilities);
8535 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY, sz, params->htcaps);
8536 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY_MASK, sz,
8537 params->htcaps_mask);
8538 }
8539
8540#ifdef CONFIG_VHT_OVERRIDES
8541 if (params->disable_vht) {
8542 wpa_printf(MSG_DEBUG, " * VHT disabled");
8543 NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_VHT);
8544 }
8545
8546 if (params->vhtcaps && params->vhtcaps_mask) {
8547 int sz = sizeof(struct ieee80211_vht_capabilities);
8548 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY, sz, params->vhtcaps);
8549 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY_MASK, sz,
8550 params->vhtcaps_mask);
8551 }
8552#endif /* CONFIG_VHT_OVERRIDES */
8553
8554 if (params->p2p)
8555 wpa_printf(MSG_DEBUG, " * P2P group");
8556
8557 return 0;
8558nla_put_failure:
8559 return -1;
8560}
8561
8562
8563static int wpa_driver_nl80211_try_connect(
8564 struct wpa_driver_nl80211_data *drv,
8565 struct wpa_driver_associate_params *params)
8566{
8567 struct nl_msg *msg;
8568 enum nl80211_auth_type type;
8569 int ret;
8570 int algs;
8571
8572 msg = nlmsg_alloc();
8573 if (!msg)
8574 return -1;
8575
8576 wpa_printf(MSG_DEBUG, "nl80211: Connect (ifindex=%d)", drv->ifindex);
8577 nl80211_cmd(drv, msg, 0, NL80211_CMD_CONNECT);
8578
8579 ret = nl80211_connect_common(drv, params, msg);
8580 if (ret)
8581 goto nla_put_failure;
8582
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008583 algs = 0;
8584 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
8585 algs++;
8586 if (params->auth_alg & WPA_AUTH_ALG_SHARED)
8587 algs++;
8588 if (params->auth_alg & WPA_AUTH_ALG_LEAP)
8589 algs++;
8590 if (algs > 1) {
8591 wpa_printf(MSG_DEBUG, " * Leave out Auth Type for automatic "
8592 "selection");
8593 goto skip_auth_type;
8594 }
8595
8596 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
8597 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
8598 else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
8599 type = NL80211_AUTHTYPE_SHARED_KEY;
8600 else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
8601 type = NL80211_AUTHTYPE_NETWORK_EAP;
8602 else if (params->auth_alg & WPA_AUTH_ALG_FT)
8603 type = NL80211_AUTHTYPE_FT;
8604 else
8605 goto nla_put_failure;
8606
8607 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
8608 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
8609
8610skip_auth_type:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008611 ret = nl80211_set_conn_keys(params, msg);
8612 if (ret)
8613 goto nla_put_failure;
8614
8615 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8616 msg = NULL;
8617 if (ret) {
8618 wpa_printf(MSG_DEBUG, "nl80211: MLME connect failed: ret=%d "
8619 "(%s)", ret, strerror(-ret));
8620 goto nla_put_failure;
8621 }
8622 ret = 0;
8623 wpa_printf(MSG_DEBUG, "nl80211: Connect request send successfully");
8624
8625nla_put_failure:
8626 nlmsg_free(msg);
8627 return ret;
8628
8629}
8630
8631
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08008632static int wpa_driver_nl80211_connect(
8633 struct wpa_driver_nl80211_data *drv,
8634 struct wpa_driver_associate_params *params)
8635{
8636 int ret = wpa_driver_nl80211_try_connect(drv, params);
8637 if (ret == -EALREADY) {
8638 /*
8639 * cfg80211 does not currently accept new connections if
8640 * we are already connected. As a workaround, force
8641 * disconnection and try again.
8642 */
8643 wpa_printf(MSG_DEBUG, "nl80211: Explicitly "
8644 "disconnecting before reassociation "
8645 "attempt");
8646 if (wpa_driver_nl80211_disconnect(
8647 drv, WLAN_REASON_PREV_AUTH_NOT_VALID))
8648 return -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08008649 ret = wpa_driver_nl80211_try_connect(drv, params);
8650 }
8651 return ret;
8652}
8653
8654
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008655static int wpa_driver_nl80211_associate(
8656 void *priv, struct wpa_driver_associate_params *params)
8657{
8658 struct i802_bss *bss = priv;
8659 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008660 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008661 struct nl_msg *msg;
8662
8663 if (params->mode == IEEE80211_MODE_AP)
8664 return wpa_driver_nl80211_ap(drv, params);
8665
8666 if (params->mode == IEEE80211_MODE_IBSS)
8667 return wpa_driver_nl80211_ibss(drv, params);
8668
8669 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008670 enum nl80211_iftype nlmode = params->p2p ?
8671 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
8672
8673 if (wpa_driver_nl80211_set_mode(priv, nlmode) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008674 return -1;
8675 return wpa_driver_nl80211_connect(drv, params);
8676 }
8677
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07008678 nl80211_mark_disconnected(drv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008679
8680 msg = nlmsg_alloc();
8681 if (!msg)
8682 return -1;
8683
8684 wpa_printf(MSG_DEBUG, "nl80211: Associate (ifindex=%d)",
8685 drv->ifindex);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008686 nl80211_cmd(drv, msg, 0, NL80211_CMD_ASSOCIATE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008687
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008688 ret = nl80211_connect_common(drv, params, msg);
8689 if (ret)
8690 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008691
8692 if (params->prev_bssid) {
8693 wpa_printf(MSG_DEBUG, " * prev_bssid=" MACSTR,
8694 MAC2STR(params->prev_bssid));
8695 NLA_PUT(msg, NL80211_ATTR_PREV_BSSID, ETH_ALEN,
8696 params->prev_bssid);
8697 }
8698
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008699 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8700 msg = NULL;
8701 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008702 wpa_dbg(drv->ctx, MSG_DEBUG,
8703 "nl80211: MLME command failed (assoc): ret=%d (%s)",
8704 ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008705 nl80211_dump_scan(drv);
8706 goto nla_put_failure;
8707 }
8708 ret = 0;
8709 wpa_printf(MSG_DEBUG, "nl80211: Association request send "
8710 "successfully");
8711
8712nla_put_failure:
8713 nlmsg_free(msg);
8714 return ret;
8715}
8716
8717
8718static int nl80211_set_mode(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008719 int ifindex, enum nl80211_iftype mode)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008720{
8721 struct nl_msg *msg;
8722 int ret = -ENOBUFS;
8723
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008724 wpa_printf(MSG_DEBUG, "nl80211: Set mode ifindex %d iftype %d (%s)",
8725 ifindex, mode, nl80211_iftype_str(mode));
8726
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008727 msg = nlmsg_alloc();
8728 if (!msg)
8729 return -ENOMEM;
8730
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008731 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_INTERFACE);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008732 if (nl80211_set_iface_id(msg, drv->first_bss) < 0)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008733 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008734 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, mode);
8735
8736 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008737 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008738 if (!ret)
8739 return 0;
8740nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008741 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008742 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface %d to mode %d:"
8743 " %d (%s)", ifindex, mode, ret, strerror(-ret));
8744 return ret;
8745}
8746
8747
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008748static int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
8749 enum nl80211_iftype nlmode)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008750{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008751 struct wpa_driver_nl80211_data *drv = bss->drv;
8752 int ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008753 int i;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008754 int was_ap = is_ap_interface(drv->nlmode);
8755 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008756
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008757 res = nl80211_set_mode(drv, drv->ifindex, nlmode);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008758 if (res && nlmode == nl80211_get_ifmode(bss))
8759 res = 0;
8760
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008761 if (res == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008762 drv->nlmode = nlmode;
8763 ret = 0;
8764 goto done;
8765 }
8766
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008767 if (res == -ENODEV)
8768 return -1;
8769
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008770 if (nlmode == drv->nlmode) {
8771 wpa_printf(MSG_DEBUG, "nl80211: Interface already in "
8772 "requested mode - ignore error");
8773 ret = 0;
8774 goto done; /* Already in the requested mode */
8775 }
8776
8777 /* mac80211 doesn't allow mode changes while the device is up, so
8778 * take the device down, try to set the mode again, and bring the
8779 * device back up.
8780 */
8781 wpa_printf(MSG_DEBUG, "nl80211: Try mode change after setting "
8782 "interface down");
8783 for (i = 0; i < 10; i++) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008784 res = i802_set_iface_flags(bss, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008785 if (res == -EACCES || res == -ENODEV)
8786 break;
8787 if (res == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008788 /* Try to set the mode again while the interface is
8789 * down */
8790 ret = nl80211_set_mode(drv, drv->ifindex, nlmode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008791 if (ret == -EACCES)
8792 break;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008793 res = i802_set_iface_flags(bss, 1);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008794 if (res && !ret)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008795 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008796 else if (ret != -EBUSY)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008797 break;
8798 } else
8799 wpa_printf(MSG_DEBUG, "nl80211: Failed to set "
8800 "interface down");
8801 os_sleep(0, 100000);
8802 }
8803
8804 if (!ret) {
8805 wpa_printf(MSG_DEBUG, "nl80211: Mode change succeeded while "
8806 "interface is down");
8807 drv->nlmode = nlmode;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008808 drv->ignore_if_down_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008809 }
8810
8811done:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008812 if (ret) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008813 wpa_printf(MSG_DEBUG, "nl80211: Interface mode change to %d "
8814 "from %d failed", nlmode, drv->nlmode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008815 return ret;
8816 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008817
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008818 if (is_p2p_net_interface(nlmode))
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008819 nl80211_disable_11b_rates(drv, drv->ifindex, 1);
8820 else if (drv->disabled_11b_rates)
8821 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
8822
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008823 if (is_ap_interface(nlmode)) {
8824 nl80211_mgmt_unsubscribe(bss, "start AP");
8825 /* Setup additional AP mode functionality if needed */
8826 if (nl80211_setup_ap(bss))
8827 return -1;
8828 } else if (was_ap) {
8829 /* Remove additional AP mode functionality */
8830 nl80211_teardown_ap(bss);
8831 } else {
8832 nl80211_mgmt_unsubscribe(bss, "mode change");
8833 }
8834
Dmitry Shmidt04949592012-07-19 12:16:46 -07008835 if (!bss->in_deinit && !is_ap_interface(nlmode) &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008836 nl80211_mgmt_subscribe_non_ap(bss) < 0)
8837 wpa_printf(MSG_DEBUG, "nl80211: Failed to register Action "
8838 "frame processing - ignore for now");
8839
8840 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008841}
8842
8843
8844static int wpa_driver_nl80211_get_capa(void *priv,
8845 struct wpa_driver_capa *capa)
8846{
8847 struct i802_bss *bss = priv;
8848 struct wpa_driver_nl80211_data *drv = bss->drv;
8849 if (!drv->has_capability)
8850 return -1;
8851 os_memcpy(capa, &drv->capa, sizeof(*capa));
Dmitry Shmidt444d5672013-04-01 13:08:44 -07008852 if (drv->extended_capa && drv->extended_capa_mask) {
8853 capa->extended_capa = drv->extended_capa;
8854 capa->extended_capa_mask = drv->extended_capa_mask;
8855 capa->extended_capa_len = drv->extended_capa_len;
8856 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008857
8858 if ((capa->flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
8859 !drv->allow_p2p_device) {
8860 wpa_printf(MSG_DEBUG, "nl80211: Do not indicate P2P_DEVICE support (p2p_device=1 driver param not specified)");
8861 capa->flags &= ~WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE;
8862 }
8863
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008864 return 0;
8865}
8866
8867
8868static int wpa_driver_nl80211_set_operstate(void *priv, int state)
8869{
8870 struct i802_bss *bss = priv;
8871 struct wpa_driver_nl80211_data *drv = bss->drv;
8872
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008873 wpa_printf(MSG_DEBUG, "nl80211: Set %s operstate %d->%d (%s)",
8874 bss->ifname, drv->operstate, state,
8875 state ? "UP" : "DORMANT");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008876 drv->operstate = state;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008877 return netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, -1,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008878 state ? IF_OPER_UP : IF_OPER_DORMANT);
8879}
8880
8881
8882static int wpa_driver_nl80211_set_supp_port(void *priv, int authorized)
8883{
8884 struct i802_bss *bss = priv;
8885 struct wpa_driver_nl80211_data *drv = bss->drv;
8886 struct nl_msg *msg;
8887 struct nl80211_sta_flag_update upd;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008888 int ret = -ENOBUFS;
8889
8890 if (!drv->associated && is_zero_ether_addr(drv->bssid) && !authorized) {
8891 wpa_printf(MSG_DEBUG, "nl80211: Skip set_supp_port(unauthorized) while not associated");
8892 return 0;
8893 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008894
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07008895 wpa_printf(MSG_DEBUG, "nl80211: Set supplicant port %sauthorized for "
8896 MACSTR, authorized ? "" : "un", MAC2STR(drv->bssid));
8897
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008898 msg = nlmsg_alloc();
8899 if (!msg)
8900 return -ENOMEM;
8901
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008902 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008903
8904 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
8905 if_nametoindex(bss->ifname));
8906 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid);
8907
8908 os_memset(&upd, 0, sizeof(upd));
8909 upd.mask = BIT(NL80211_STA_FLAG_AUTHORIZED);
8910 if (authorized)
8911 upd.set = BIT(NL80211_STA_FLAG_AUTHORIZED);
8912 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
8913
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008914 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8915 msg = NULL;
8916 if (!ret)
8917 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008918 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008919 nlmsg_free(msg);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008920 wpa_printf(MSG_DEBUG, "nl80211: Failed to set STA flag: %d (%s)",
8921 ret, strerror(-ret));
8922 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008923}
8924
8925
Jouni Malinen75ecf522011-06-27 15:19:46 -07008926/* Set kernel driver on given frequency (MHz) */
8927static int i802_set_freq(void *priv, struct hostapd_freq_params *freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008928{
Jouni Malinen75ecf522011-06-27 15:19:46 -07008929 struct i802_bss *bss = priv;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008930 return wpa_driver_nl80211_set_freq(bss, freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008931}
8932
8933
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008934static inline int min_int(int a, int b)
8935{
8936 if (a < b)
8937 return a;
8938 return b;
8939}
8940
8941
8942static int get_key_handler(struct nl_msg *msg, void *arg)
8943{
8944 struct nlattr *tb[NL80211_ATTR_MAX + 1];
8945 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
8946
8947 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
8948 genlmsg_attrlen(gnlh, 0), NULL);
8949
8950 /*
8951 * TODO: validate the key index and mac address!
8952 * Otherwise, there's a race condition as soon as
8953 * the kernel starts sending key notifications.
8954 */
8955
8956 if (tb[NL80211_ATTR_KEY_SEQ])
8957 memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]),
8958 min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6));
8959 return NL_SKIP;
8960}
8961
8962
8963static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
8964 int idx, u8 *seq)
8965{
8966 struct i802_bss *bss = priv;
8967 struct wpa_driver_nl80211_data *drv = bss->drv;
8968 struct nl_msg *msg;
8969
8970 msg = nlmsg_alloc();
8971 if (!msg)
8972 return -ENOMEM;
8973
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008974 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_KEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008975
8976 if (addr)
8977 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
8978 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, idx);
8979 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
8980
8981 memset(seq, 0, 6);
8982
8983 return send_and_recv_msgs(drv, msg, get_key_handler, seq);
8984 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008985 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008986 return -ENOBUFS;
8987}
8988
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008989
8990static int i802_set_rts(void *priv, int rts)
8991{
8992 struct i802_bss *bss = priv;
8993 struct wpa_driver_nl80211_data *drv = bss->drv;
8994 struct nl_msg *msg;
8995 int ret = -ENOBUFS;
8996 u32 val;
8997
8998 msg = nlmsg_alloc();
8999 if (!msg)
9000 return -ENOMEM;
9001
9002 if (rts >= 2347)
9003 val = (u32) -1;
9004 else
9005 val = rts;
9006
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009007 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009008 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
9009 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD, val);
9010
9011 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009012 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009013 if (!ret)
9014 return 0;
9015nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009016 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009017 wpa_printf(MSG_DEBUG, "nl80211: Failed to set RTS threshold %d: "
9018 "%d (%s)", rts, ret, strerror(-ret));
9019 return ret;
9020}
9021
9022
9023static int i802_set_frag(void *priv, int frag)
9024{
9025 struct i802_bss *bss = priv;
9026 struct wpa_driver_nl80211_data *drv = bss->drv;
9027 struct nl_msg *msg;
9028 int ret = -ENOBUFS;
9029 u32 val;
9030
9031 msg = nlmsg_alloc();
9032 if (!msg)
9033 return -ENOMEM;
9034
9035 if (frag >= 2346)
9036 val = (u32) -1;
9037 else
9038 val = frag;
9039
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009040 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009041 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
9042 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD, val);
9043
9044 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009045 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009046 if (!ret)
9047 return 0;
9048nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009049 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009050 wpa_printf(MSG_DEBUG, "nl80211: Failed to set fragmentation threshold "
9051 "%d: %d (%s)", frag, ret, strerror(-ret));
9052 return ret;
9053}
9054
9055
9056static int i802_flush(void *priv)
9057{
9058 struct i802_bss *bss = priv;
9059 struct wpa_driver_nl80211_data *drv = bss->drv;
9060 struct nl_msg *msg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009061 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009062
9063 msg = nlmsg_alloc();
9064 if (!msg)
9065 return -1;
9066
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07009067 wpa_printf(MSG_DEBUG, "nl80211: flush -> DEL_STATION %s (all)",
9068 bss->ifname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009069 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009070
9071 /*
9072 * XXX: FIX! this needs to flush all VLANs too
9073 */
9074 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
9075 if_nametoindex(bss->ifname));
9076
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009077 res = send_and_recv_msgs(drv, msg, NULL, NULL);
9078 if (res) {
9079 wpa_printf(MSG_DEBUG, "nl80211: Station flush failed: ret=%d "
9080 "(%s)", res, strerror(-res));
9081 }
9082 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009083 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009084 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009085 return -ENOBUFS;
9086}
9087
9088
9089static int get_sta_handler(struct nl_msg *msg, void *arg)
9090{
9091 struct nlattr *tb[NL80211_ATTR_MAX + 1];
9092 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
9093 struct hostap_sta_driver_data *data = arg;
9094 struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
9095 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
9096 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
9097 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
9098 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
9099 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
9100 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03009101 [NL80211_STA_INFO_TX_FAILED] = { .type = NLA_U32 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009102 };
9103
9104 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9105 genlmsg_attrlen(gnlh, 0), NULL);
9106
9107 /*
9108 * TODO: validate the interface and mac address!
9109 * Otherwise, there's a race condition as soon as
9110 * the kernel starts sending station notifications.
9111 */
9112
9113 if (!tb[NL80211_ATTR_STA_INFO]) {
9114 wpa_printf(MSG_DEBUG, "sta stats missing!");
9115 return NL_SKIP;
9116 }
9117 if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
9118 tb[NL80211_ATTR_STA_INFO],
9119 stats_policy)) {
9120 wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
9121 return NL_SKIP;
9122 }
9123
9124 if (stats[NL80211_STA_INFO_INACTIVE_TIME])
9125 data->inactive_msec =
9126 nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
9127 if (stats[NL80211_STA_INFO_RX_BYTES])
9128 data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
9129 if (stats[NL80211_STA_INFO_TX_BYTES])
9130 data->tx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
9131 if (stats[NL80211_STA_INFO_RX_PACKETS])
9132 data->rx_packets =
9133 nla_get_u32(stats[NL80211_STA_INFO_RX_PACKETS]);
9134 if (stats[NL80211_STA_INFO_TX_PACKETS])
9135 data->tx_packets =
9136 nla_get_u32(stats[NL80211_STA_INFO_TX_PACKETS]);
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03009137 if (stats[NL80211_STA_INFO_TX_FAILED])
9138 data->tx_retry_failed =
9139 nla_get_u32(stats[NL80211_STA_INFO_TX_FAILED]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009140
9141 return NL_SKIP;
9142}
9143
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009144static int i802_read_sta_data(struct i802_bss *bss,
9145 struct hostap_sta_driver_data *data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009146 const u8 *addr)
9147{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009148 struct wpa_driver_nl80211_data *drv = bss->drv;
9149 struct nl_msg *msg;
9150
9151 os_memset(data, 0, sizeof(*data));
9152 msg = nlmsg_alloc();
9153 if (!msg)
9154 return -ENOMEM;
9155
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009156 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009157
9158 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
9159 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
9160
9161 return send_and_recv_msgs(drv, msg, get_sta_handler, data);
9162 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009163 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009164 return -ENOBUFS;
9165}
9166
9167
9168static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
9169 int cw_min, int cw_max, int burst_time)
9170{
9171 struct i802_bss *bss = priv;
9172 struct wpa_driver_nl80211_data *drv = bss->drv;
9173 struct nl_msg *msg;
9174 struct nlattr *txq, *params;
9175
9176 msg = nlmsg_alloc();
9177 if (!msg)
9178 return -1;
9179
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009180 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009181
9182 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
9183
9184 txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS);
9185 if (!txq)
9186 goto nla_put_failure;
9187
9188 /* We are only sending parameters for a single TXQ at a time */
9189 params = nla_nest_start(msg, 1);
9190 if (!params)
9191 goto nla_put_failure;
9192
9193 switch (queue) {
9194 case 0:
9195 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VO);
9196 break;
9197 case 1:
9198 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VI);
9199 break;
9200 case 2:
9201 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BE);
9202 break;
9203 case 3:
9204 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BK);
9205 break;
9206 }
9207 /* Burst time is configured in units of 0.1 msec and TXOP parameter in
9208 * 32 usec, so need to convert the value here. */
9209 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_TXOP, (burst_time * 100 + 16) / 32);
9210 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min);
9211 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max);
9212 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_AIFS, aifs);
9213
9214 nla_nest_end(msg, params);
9215
9216 nla_nest_end(msg, txq);
9217
9218 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
9219 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009220 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009221 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009222 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009223 return -1;
9224}
9225
9226
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009227static int i802_set_sta_vlan(struct i802_bss *bss, const u8 *addr,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009228 const char *ifname, int vlan_id)
9229{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009230 struct wpa_driver_nl80211_data *drv = bss->drv;
9231 struct nl_msg *msg;
9232 int ret = -ENOBUFS;
9233
9234 msg = nlmsg_alloc();
9235 if (!msg)
9236 return -ENOMEM;
9237
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009238 wpa_printf(MSG_DEBUG, "nl80211: %s[%d]: set_sta_vlan(" MACSTR
9239 ", ifname=%s[%d], vlan_id=%d)",
9240 bss->ifname, if_nametoindex(bss->ifname),
9241 MAC2STR(addr), ifname, if_nametoindex(ifname), vlan_id);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009242 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009243
9244 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
9245 if_nametoindex(bss->ifname));
9246 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
9247 NLA_PUT_U32(msg, NL80211_ATTR_STA_VLAN,
9248 if_nametoindex(ifname));
9249
9250 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009251 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009252 if (ret < 0) {
9253 wpa_printf(MSG_ERROR, "nl80211: NL80211_ATTR_STA_VLAN (addr="
9254 MACSTR " ifname=%s vlan_id=%d) failed: %d (%s)",
9255 MAC2STR(addr), ifname, vlan_id, ret,
9256 strerror(-ret));
9257 }
9258 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009259 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009260 return ret;
9261}
9262
9263
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009264static int i802_get_inact_sec(void *priv, const u8 *addr)
9265{
9266 struct hostap_sta_driver_data data;
9267 int ret;
9268
9269 data.inactive_msec = (unsigned long) -1;
9270 ret = i802_read_sta_data(priv, &data, addr);
9271 if (ret || data.inactive_msec == (unsigned long) -1)
9272 return -1;
9273 return data.inactive_msec / 1000;
9274}
9275
9276
9277static int i802_sta_clear_stats(void *priv, const u8 *addr)
9278{
9279#if 0
9280 /* TODO */
9281#endif
9282 return 0;
9283}
9284
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009285
9286static int i802_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
9287 int reason)
9288{
9289 struct i802_bss *bss = priv;
Dmitry Shmidt04949592012-07-19 12:16:46 -07009290 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009291 struct ieee80211_mgmt mgmt;
9292
Dmitry Shmidt04949592012-07-19 12:16:46 -07009293 if (drv->device_ap_sme)
9294 return wpa_driver_nl80211_sta_remove(bss, addr);
9295
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009296 memset(&mgmt, 0, sizeof(mgmt));
9297 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
9298 WLAN_FC_STYPE_DEAUTH);
9299 memcpy(mgmt.da, addr, ETH_ALEN);
9300 memcpy(mgmt.sa, own_addr, ETH_ALEN);
9301 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
9302 mgmt.u.deauth.reason_code = host_to_le16(reason);
9303 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
9304 IEEE80211_HDRLEN +
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009305 sizeof(mgmt.u.deauth), 0, 0, 0, 0,
9306 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009307}
9308
9309
9310static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
9311 int reason)
9312{
9313 struct i802_bss *bss = priv;
Dmitry Shmidt04949592012-07-19 12:16:46 -07009314 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009315 struct ieee80211_mgmt mgmt;
9316
Dmitry Shmidt04949592012-07-19 12:16:46 -07009317 if (drv->device_ap_sme)
9318 return wpa_driver_nl80211_sta_remove(bss, addr);
9319
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009320 memset(&mgmt, 0, sizeof(mgmt));
9321 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
9322 WLAN_FC_STYPE_DISASSOC);
9323 memcpy(mgmt.da, addr, ETH_ALEN);
9324 memcpy(mgmt.sa, own_addr, ETH_ALEN);
9325 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
9326 mgmt.u.disassoc.reason_code = host_to_le16(reason);
9327 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
9328 IEEE80211_HDRLEN +
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009329 sizeof(mgmt.u.disassoc), 0, 0, 0, 0,
9330 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009331}
9332
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009333
Jouni Malinen75ecf522011-06-27 15:19:46 -07009334static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
9335{
9336 int i;
9337 int *old;
9338
9339 wpa_printf(MSG_DEBUG, "nl80211: Add own interface ifindex %d",
9340 ifidx);
9341 for (i = 0; i < drv->num_if_indices; i++) {
9342 if (drv->if_indices[i] == 0) {
9343 drv->if_indices[i] = ifidx;
9344 return;
9345 }
9346 }
9347
9348 if (drv->if_indices != drv->default_if_indices)
9349 old = drv->if_indices;
9350 else
9351 old = NULL;
9352
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07009353 drv->if_indices = os_realloc_array(old, drv->num_if_indices + 1,
9354 sizeof(int));
Jouni Malinen75ecf522011-06-27 15:19:46 -07009355 if (!drv->if_indices) {
9356 if (!old)
9357 drv->if_indices = drv->default_if_indices;
9358 else
9359 drv->if_indices = old;
9360 wpa_printf(MSG_ERROR, "Failed to reallocate memory for "
9361 "interfaces");
9362 wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx);
9363 return;
9364 } else if (!old)
9365 os_memcpy(drv->if_indices, drv->default_if_indices,
9366 sizeof(drv->default_if_indices));
9367 drv->if_indices[drv->num_if_indices] = ifidx;
9368 drv->num_if_indices++;
9369}
9370
9371
9372static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
9373{
9374 int i;
9375
9376 for (i = 0; i < drv->num_if_indices; i++) {
9377 if (drv->if_indices[i] == ifidx) {
9378 drv->if_indices[i] = 0;
9379 break;
9380 }
9381 }
9382}
9383
9384
9385static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
9386{
9387 int i;
9388
9389 for (i = 0; i < drv->num_if_indices; i++)
9390 if (drv->if_indices[i] == ifidx)
9391 return 1;
9392
9393 return 0;
9394}
9395
9396
9397static int i802_set_wds_sta(void *priv, const u8 *addr, int aid, int val,
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07009398 const char *bridge_ifname, char *ifname_wds)
Jouni Malinen75ecf522011-06-27 15:19:46 -07009399{
9400 struct i802_bss *bss = priv;
9401 struct wpa_driver_nl80211_data *drv = bss->drv;
9402 char name[IFNAMSIZ + 1];
9403
9404 os_snprintf(name, sizeof(name), "%s.sta%d", bss->ifname, aid);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07009405 if (ifname_wds)
9406 os_strlcpy(ifname_wds, name, IFNAMSIZ + 1);
9407
Jouni Malinen75ecf522011-06-27 15:19:46 -07009408 wpa_printf(MSG_DEBUG, "nl80211: Set WDS STA addr=" MACSTR
9409 " aid=%d val=%d name=%s", MAC2STR(addr), aid, val, name);
9410 if (val) {
9411 if (!if_nametoindex(name)) {
9412 if (nl80211_create_iface(drv, name,
9413 NL80211_IFTYPE_AP_VLAN,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009414 bss->addr, 1, NULL, NULL, 0) <
9415 0)
Jouni Malinen75ecf522011-06-27 15:19:46 -07009416 return -1;
9417 if (bridge_ifname &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009418 linux_br_add_if(drv->global->ioctl_sock,
9419 bridge_ifname, name) < 0)
Jouni Malinen75ecf522011-06-27 15:19:46 -07009420 return -1;
9421 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07009422 if (linux_set_iface_flags(drv->global->ioctl_sock, name, 1)) {
9423 wpa_printf(MSG_ERROR, "nl80211: Failed to set WDS STA "
9424 "interface %s up", name);
9425 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07009426 return i802_set_sta_vlan(priv, addr, name, 0);
9427 } else {
Dmitry Shmidtaa532512012-09-24 10:35:31 -07009428 if (bridge_ifname)
9429 linux_br_del_if(drv->global->ioctl_sock, bridge_ifname,
9430 name);
9431
Jouni Malinen75ecf522011-06-27 15:19:46 -07009432 i802_set_sta_vlan(priv, addr, bss->ifname, 0);
9433 return wpa_driver_nl80211_if_remove(priv, WPA_IF_AP_VLAN,
9434 name);
9435 }
9436}
9437
9438
9439static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
9440{
9441 struct wpa_driver_nl80211_data *drv = eloop_ctx;
9442 struct sockaddr_ll lladdr;
9443 unsigned char buf[3000];
9444 int len;
9445 socklen_t fromlen = sizeof(lladdr);
9446
9447 len = recvfrom(sock, buf, sizeof(buf), 0,
9448 (struct sockaddr *)&lladdr, &fromlen);
9449 if (len < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07009450 wpa_printf(MSG_ERROR, "nl80211: EAPOL recv failed: %s",
9451 strerror(errno));
Jouni Malinen75ecf522011-06-27 15:19:46 -07009452 return;
9453 }
9454
9455 if (have_ifidx(drv, lladdr.sll_ifindex))
9456 drv_event_eapol_rx(drv->ctx, lladdr.sll_addr, buf, len);
9457}
9458
9459
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009460static int i802_check_bridge(struct wpa_driver_nl80211_data *drv,
9461 struct i802_bss *bss,
9462 const char *brname, const char *ifname)
9463{
9464 int ifindex;
9465 char in_br[IFNAMSIZ];
9466
9467 os_strlcpy(bss->brname, brname, IFNAMSIZ);
9468 ifindex = if_nametoindex(brname);
9469 if (ifindex == 0) {
9470 /*
9471 * Bridge was configured, but the bridge device does
9472 * not exist. Try to add it now.
9473 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009474 if (linux_br_add(drv->global->ioctl_sock, brname) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009475 wpa_printf(MSG_ERROR, "nl80211: Failed to add the "
9476 "bridge interface %s: %s",
9477 brname, strerror(errno));
9478 return -1;
9479 }
9480 bss->added_bridge = 1;
9481 add_ifidx(drv, if_nametoindex(brname));
9482 }
9483
9484 if (linux_br_get(in_br, ifname) == 0) {
9485 if (os_strcmp(in_br, brname) == 0)
9486 return 0; /* already in the bridge */
9487
9488 wpa_printf(MSG_DEBUG, "nl80211: Removing interface %s from "
9489 "bridge %s", ifname, in_br);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009490 if (linux_br_del_if(drv->global->ioctl_sock, in_br, ifname) <
9491 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009492 wpa_printf(MSG_ERROR, "nl80211: Failed to "
9493 "remove interface %s from bridge "
9494 "%s: %s",
9495 ifname, brname, strerror(errno));
9496 return -1;
9497 }
9498 }
9499
9500 wpa_printf(MSG_DEBUG, "nl80211: Adding interface %s into bridge %s",
9501 ifname, brname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009502 if (linux_br_add_if(drv->global->ioctl_sock, brname, ifname) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009503 wpa_printf(MSG_ERROR, "nl80211: Failed to add interface %s "
9504 "into bridge %s: %s",
9505 ifname, brname, strerror(errno));
9506 return -1;
9507 }
9508 bss->added_if_into_bridge = 1;
9509
9510 return 0;
9511}
9512
9513
9514static void *i802_init(struct hostapd_data *hapd,
9515 struct wpa_init_params *params)
9516{
9517 struct wpa_driver_nl80211_data *drv;
9518 struct i802_bss *bss;
9519 size_t i;
9520 char brname[IFNAMSIZ];
9521 int ifindex, br_ifindex;
9522 int br_added = 0;
9523
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08009524 bss = wpa_driver_nl80211_drv_init(hapd, params->ifname,
9525 params->global_priv, 1,
9526 params->bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009527 if (bss == NULL)
9528 return NULL;
9529
9530 drv = bss->drv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009531
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009532 if (linux_br_get(brname, params->ifname) == 0) {
9533 wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in bridge %s",
9534 params->ifname, brname);
9535 br_ifindex = if_nametoindex(brname);
9536 } else {
9537 brname[0] = '\0';
9538 br_ifindex = 0;
9539 }
9540
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009541 for (i = 0; i < params->num_bridge; i++) {
9542 if (params->bridge[i]) {
9543 ifindex = if_nametoindex(params->bridge[i]);
9544 if (ifindex)
9545 add_ifidx(drv, ifindex);
9546 if (ifindex == br_ifindex)
9547 br_added = 1;
9548 }
9549 }
9550 if (!br_added && br_ifindex &&
9551 (params->num_bridge == 0 || !params->bridge[0]))
9552 add_ifidx(drv, br_ifindex);
9553
9554 /* start listening for EAPOL on the default AP interface */
9555 add_ifidx(drv, drv->ifindex);
9556
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009557 if (params->num_bridge && params->bridge[0] &&
9558 i802_check_bridge(drv, bss, params->bridge[0], params->ifname) < 0)
9559 goto failed;
9560
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009561 drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
9562 if (drv->eapol_sock < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07009563 wpa_printf(MSG_ERROR, "nl80211: socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE) failed: %s",
9564 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009565 goto failed;
9566 }
9567
9568 if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
9569 {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07009570 wpa_printf(MSG_INFO, "nl80211: Could not register read socket for eapol");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009571 goto failed;
9572 }
9573
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009574 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
9575 params->own_addr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009576 goto failed;
9577
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009578 memcpy(bss->addr, params->own_addr, ETH_ALEN);
9579
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009580 return bss;
9581
9582failed:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009583 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009584 return NULL;
9585}
9586
9587
9588static void i802_deinit(void *priv)
9589{
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009590 struct i802_bss *bss = priv;
9591 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009592}
9593
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009594
9595static enum nl80211_iftype wpa_driver_nl80211_if_type(
9596 enum wpa_driver_if_type type)
9597{
9598 switch (type) {
9599 case WPA_IF_STATION:
9600 return NL80211_IFTYPE_STATION;
9601 case WPA_IF_P2P_CLIENT:
9602 case WPA_IF_P2P_GROUP:
9603 return NL80211_IFTYPE_P2P_CLIENT;
9604 case WPA_IF_AP_VLAN:
9605 return NL80211_IFTYPE_AP_VLAN;
9606 case WPA_IF_AP_BSS:
9607 return NL80211_IFTYPE_AP;
9608 case WPA_IF_P2P_GO:
9609 return NL80211_IFTYPE_P2P_GO;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009610 case WPA_IF_P2P_DEVICE:
9611 return NL80211_IFTYPE_P2P_DEVICE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009612 }
9613 return -1;
9614}
9615
9616
9617#ifdef CONFIG_P2P
9618
9619static int nl80211_addr_in_use(struct nl80211_global *global, const u8 *addr)
9620{
9621 struct wpa_driver_nl80211_data *drv;
9622 dl_list_for_each(drv, &global->interfaces,
9623 struct wpa_driver_nl80211_data, list) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009624 if (os_memcmp(addr, drv->first_bss->addr, ETH_ALEN) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009625 return 1;
9626 }
9627 return 0;
9628}
9629
9630
9631static int nl80211_p2p_interface_addr(struct wpa_driver_nl80211_data *drv,
9632 u8 *new_addr)
9633{
9634 unsigned int idx;
9635
9636 if (!drv->global)
9637 return -1;
9638
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009639 os_memcpy(new_addr, drv->first_bss->addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009640 for (idx = 0; idx < 64; idx++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009641 new_addr[0] = drv->first_bss->addr[0] | 0x02;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009642 new_addr[0] ^= idx << 2;
9643 if (!nl80211_addr_in_use(drv->global, new_addr))
9644 break;
9645 }
9646 if (idx == 64)
9647 return -1;
9648
9649 wpa_printf(MSG_DEBUG, "nl80211: Assigned new P2P Interface Address "
9650 MACSTR, MAC2STR(new_addr));
9651
9652 return 0;
9653}
9654
9655#endif /* CONFIG_P2P */
9656
9657
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009658struct wdev_info {
9659 u64 wdev_id;
9660 int wdev_id_set;
9661 u8 macaddr[ETH_ALEN];
9662};
9663
9664static int nl80211_wdev_handler(struct nl_msg *msg, void *arg)
9665{
9666 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
9667 struct nlattr *tb[NL80211_ATTR_MAX + 1];
9668 struct wdev_info *wi = arg;
9669
9670 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9671 genlmsg_attrlen(gnlh, 0), NULL);
9672 if (tb[NL80211_ATTR_WDEV]) {
9673 wi->wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
9674 wi->wdev_id_set = 1;
9675 }
9676
9677 if (tb[NL80211_ATTR_MAC])
9678 os_memcpy(wi->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
9679 ETH_ALEN);
9680
9681 return NL_SKIP;
9682}
9683
9684
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009685static int wpa_driver_nl80211_if_add(void *priv, enum wpa_driver_if_type type,
9686 const char *ifname, const u8 *addr,
9687 void *bss_ctx, void **drv_priv,
9688 char *force_ifname, u8 *if_addr,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009689 const char *bridge, int use_existing)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009690{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009691 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009692 struct i802_bss *bss = priv;
9693 struct wpa_driver_nl80211_data *drv = bss->drv;
9694 int ifidx;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009695 int added = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009696
9697 if (addr)
9698 os_memcpy(if_addr, addr, ETH_ALEN);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009699 nlmode = wpa_driver_nl80211_if_type(type);
9700 if (nlmode == NL80211_IFTYPE_P2P_DEVICE) {
9701 struct wdev_info p2pdev_info;
9702
9703 os_memset(&p2pdev_info, 0, sizeof(p2pdev_info));
9704 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
9705 0, nl80211_wdev_handler,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009706 &p2pdev_info, use_existing);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009707 if (!p2pdev_info.wdev_id_set || ifidx != 0) {
9708 wpa_printf(MSG_ERROR, "nl80211: Failed to create a P2P Device interface %s",
9709 ifname);
9710 return -1;
9711 }
9712
9713 drv->global->if_add_wdevid = p2pdev_info.wdev_id;
9714 drv->global->if_add_wdevid_set = p2pdev_info.wdev_id_set;
9715 if (!is_zero_ether_addr(p2pdev_info.macaddr))
9716 os_memcpy(if_addr, p2pdev_info.macaddr, ETH_ALEN);
9717 wpa_printf(MSG_DEBUG, "nl80211: New P2P Device interface %s (0x%llx) created",
9718 ifname,
9719 (long long unsigned int) p2pdev_info.wdev_id);
9720 } else {
9721 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009722 0, NULL, NULL, use_existing);
9723 if (use_existing && ifidx == -ENFILE) {
9724 added = 0;
9725 ifidx = if_nametoindex(ifname);
9726 } else if (ifidx < 0) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009727 return -1;
9728 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009729 }
9730
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009731 if (!addr) {
9732 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
9733 os_memcpy(if_addr, bss->addr, ETH_ALEN);
9734 else if (linux_get_ifhwaddr(drv->global->ioctl_sock,
9735 bss->ifname, if_addr) < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009736 if (added)
9737 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009738 return -1;
9739 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009740 }
9741
9742#ifdef CONFIG_P2P
9743 if (!addr &&
9744 (type == WPA_IF_P2P_CLIENT || type == WPA_IF_P2P_GROUP ||
9745 type == WPA_IF_P2P_GO)) {
9746 /* Enforce unique P2P Interface Address */
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009747 u8 new_addr[ETH_ALEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009748
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009749 if (linux_get_ifhwaddr(drv->global->ioctl_sock, ifname,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009750 new_addr) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009751 nl80211_remove_iface(drv, ifidx);
9752 return -1;
9753 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009754 if (nl80211_addr_in_use(drv->global, new_addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009755 wpa_printf(MSG_DEBUG, "nl80211: Allocate new address "
9756 "for P2P group interface");
9757 if (nl80211_p2p_interface_addr(drv, new_addr) < 0) {
9758 nl80211_remove_iface(drv, ifidx);
9759 return -1;
9760 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009761 if (linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009762 new_addr) < 0) {
9763 nl80211_remove_iface(drv, ifidx);
9764 return -1;
9765 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009766 }
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07009767 os_memcpy(if_addr, new_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009768 }
9769#endif /* CONFIG_P2P */
9770
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009771 if (type == WPA_IF_AP_BSS) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07009772 struct i802_bss *new_bss = os_zalloc(sizeof(*new_bss));
9773 if (new_bss == NULL) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009774 if (added)
9775 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07009776 return -1;
9777 }
9778
9779 if (bridge &&
9780 i802_check_bridge(drv, new_bss, bridge, ifname) < 0) {
9781 wpa_printf(MSG_ERROR, "nl80211: Failed to add the new "
9782 "interface %s to a bridge %s",
9783 ifname, bridge);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009784 if (added)
9785 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07009786 os_free(new_bss);
9787 return -1;
9788 }
9789
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009790 if (linux_set_iface_flags(drv->global->ioctl_sock, ifname, 1))
9791 {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009792 nl80211_remove_iface(drv, ifidx);
9793 os_free(new_bss);
9794 return -1;
9795 }
9796 os_strlcpy(new_bss->ifname, ifname, IFNAMSIZ);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009797 os_memcpy(new_bss->addr, if_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009798 new_bss->ifindex = ifidx;
9799 new_bss->drv = drv;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009800 new_bss->next = drv->first_bss->next;
9801 new_bss->freq = drv->first_bss->freq;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08009802 new_bss->ctx = bss_ctx;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009803 new_bss->added_if = added;
9804 drv->first_bss->next = new_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009805 if (drv_priv)
9806 *drv_priv = new_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009807 nl80211_init_bss(new_bss);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08009808
9809 /* Subscribe management frames for this WPA_IF_AP_BSS */
9810 if (nl80211_setup_ap(new_bss))
9811 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009812 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009813
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009814 if (drv->global)
9815 drv->global->if_add_ifindex = ifidx;
9816
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009817 return 0;
9818}
9819
9820
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009821static int wpa_driver_nl80211_if_remove(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009822 enum wpa_driver_if_type type,
9823 const char *ifname)
9824{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009825 struct wpa_driver_nl80211_data *drv = bss->drv;
9826 int ifindex = if_nametoindex(ifname);
9827
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009828 wpa_printf(MSG_DEBUG, "nl80211: %s(type=%d ifname=%s) ifindex=%d added_if=%d",
9829 __func__, type, ifname, ifindex, bss->added_if);
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08009830 if (ifindex > 0 && (bss->added_if || bss->ifindex != ifindex))
Dmitry Shmidt051af732013-10-22 13:52:46 -07009831 nl80211_remove_iface(drv, ifindex);
Dmitry Shmidtaa532512012-09-24 10:35:31 -07009832
Dmitry Shmidtaa532512012-09-24 10:35:31 -07009833 if (type != WPA_IF_AP_BSS)
9834 return 0;
9835
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009836 if (bss->added_if_into_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009837 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
9838 bss->ifname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009839 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
9840 "interface %s from bridge %s: %s",
9841 bss->ifname, bss->brname, strerror(errno));
9842 }
9843 if (bss->added_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009844 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009845 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
9846 "bridge %s: %s",
9847 bss->brname, strerror(errno));
9848 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009849
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009850 if (bss != drv->first_bss) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009851 struct i802_bss *tbss;
9852
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009853 wpa_printf(MSG_DEBUG, "nl80211: Not the first BSS - remove it");
9854 for (tbss = drv->first_bss; tbss; tbss = tbss->next) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009855 if (tbss->next == bss) {
9856 tbss->next = bss->next;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08009857 /* Unsubscribe management frames */
9858 nl80211_teardown_ap(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009859 nl80211_destroy_bss(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009860 os_free(bss);
9861 bss = NULL;
9862 break;
9863 }
9864 }
9865 if (bss)
9866 wpa_printf(MSG_INFO, "nl80211: %s - could not find "
9867 "BSS %p in the list", __func__, bss);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009868 } else {
9869 wpa_printf(MSG_DEBUG, "nl80211: First BSS - reassign context");
9870 nl80211_teardown_ap(bss);
9871 if (!bss->added_if && !drv->first_bss->next)
9872 wpa_driver_nl80211_del_beacon(drv);
9873 nl80211_destroy_bss(bss);
9874 if (!bss->added_if)
9875 i802_set_iface_flags(bss, 0);
9876 if (drv->first_bss->next) {
9877 drv->first_bss = drv->first_bss->next;
9878 drv->ctx = drv->first_bss->ctx;
9879 os_free(bss);
9880 } else {
9881 wpa_printf(MSG_DEBUG, "nl80211: No second BSS to reassign context to");
9882 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009883 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009884
9885 return 0;
9886}
9887
9888
9889static int cookie_handler(struct nl_msg *msg, void *arg)
9890{
9891 struct nlattr *tb[NL80211_ATTR_MAX + 1];
9892 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
9893 u64 *cookie = arg;
9894 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9895 genlmsg_attrlen(gnlh, 0), NULL);
9896 if (tb[NL80211_ATTR_COOKIE])
9897 *cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
9898 return NL_SKIP;
9899}
9900
9901
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009902static int nl80211_send_frame_cmd(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009903 unsigned int freq, unsigned int wait,
9904 const u8 *buf, size_t buf_len,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009905 u64 *cookie_out, int no_cck, int no_ack,
9906 int offchanok)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009907{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009908 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009909 struct nl_msg *msg;
9910 u64 cookie;
9911 int ret = -1;
9912
9913 msg = nlmsg_alloc();
9914 if (!msg)
9915 return -1;
9916
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07009917 wpa_printf(MSG_MSGDUMP, "nl80211: CMD_FRAME freq=%u wait=%u no_cck=%d "
Dmitry Shmidt04949592012-07-19 12:16:46 -07009918 "no_ack=%d offchanok=%d",
9919 freq, wait, no_cck, no_ack, offchanok);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07009920 wpa_hexdump(MSG_MSGDUMP, "CMD_FRAME", buf, buf_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009921 nl80211_cmd(drv, msg, 0, NL80211_CMD_FRAME);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009922
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009923 if (nl80211_set_iface_id(msg, bss) < 0)
9924 goto nla_put_failure;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07009925 if (freq)
9926 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07009927 if (wait)
9928 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, wait);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08009929 if (offchanok && (drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009930 NLA_PUT_FLAG(msg, NL80211_ATTR_OFFCHANNEL_TX_OK);
9931 if (no_cck)
9932 NLA_PUT_FLAG(msg, NL80211_ATTR_TX_NO_CCK_RATE);
9933 if (no_ack)
9934 NLA_PUT_FLAG(msg, NL80211_ATTR_DONT_WAIT_FOR_ACK);
9935
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009936 NLA_PUT(msg, NL80211_ATTR_FRAME, buf_len, buf);
9937
9938 cookie = 0;
9939 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
9940 msg = NULL;
9941 if (ret) {
9942 wpa_printf(MSG_DEBUG, "nl80211: Frame command failed: ret=%d "
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07009943 "(%s) (freq=%u wait=%u)", ret, strerror(-ret),
9944 freq, wait);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009945 goto nla_put_failure;
9946 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07009947 wpa_printf(MSG_MSGDUMP, "nl80211: Frame TX command accepted%s; "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009948 "cookie 0x%llx", no_ack ? " (no ACK)" : "",
9949 (long long unsigned int) cookie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009950
9951 if (cookie_out)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009952 *cookie_out = no_ack ? (u64) -1 : cookie;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009953
9954nla_put_failure:
9955 nlmsg_free(msg);
9956 return ret;
9957}
9958
9959
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009960static int wpa_driver_nl80211_send_action(struct i802_bss *bss,
9961 unsigned int freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009962 unsigned int wait_time,
9963 const u8 *dst, const u8 *src,
9964 const u8 *bssid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009965 const u8 *data, size_t data_len,
9966 int no_cck)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009967{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009968 struct wpa_driver_nl80211_data *drv = bss->drv;
9969 int ret = -1;
9970 u8 *buf;
9971 struct ieee80211_hdr *hdr;
9972
9973 wpa_printf(MSG_DEBUG, "nl80211: Send Action frame (ifindex=%d, "
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08009974 "freq=%u MHz wait=%d ms no_cck=%d)",
9975 drv->ifindex, freq, wait_time, no_cck);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009976
9977 buf = os_zalloc(24 + data_len);
9978 if (buf == NULL)
9979 return ret;
9980 os_memcpy(buf + 24, data, data_len);
9981 hdr = (struct ieee80211_hdr *) buf;
9982 hdr->frame_control =
9983 IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_ACTION);
9984 os_memcpy(hdr->addr1, dst, ETH_ALEN);
9985 os_memcpy(hdr->addr2, src, ETH_ALEN);
9986 os_memcpy(hdr->addr3, bssid, ETH_ALEN);
9987
Dmitry Shmidt56052862013-10-04 10:23:25 -07009988 if (is_ap_interface(drv->nlmode) &&
9989 (!(drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
9990 (int) freq == bss->freq || drv->device_ap_sme ||
9991 !drv->use_monitor))
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009992 ret = wpa_driver_nl80211_send_mlme(bss, buf, 24 + data_len,
9993 0, freq, no_cck, 1,
9994 wait_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009995 else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009996 ret = nl80211_send_frame_cmd(bss, freq, wait_time, buf,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009997 24 + data_len,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009998 &drv->send_action_cookie,
9999 no_cck, 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010000
10001 os_free(buf);
10002 return ret;
10003}
10004
10005
10006static void wpa_driver_nl80211_send_action_cancel_wait(void *priv)
10007{
10008 struct i802_bss *bss = priv;
10009 struct wpa_driver_nl80211_data *drv = bss->drv;
10010 struct nl_msg *msg;
10011 int ret;
10012
10013 msg = nlmsg_alloc();
10014 if (!msg)
10015 return;
10016
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -080010017 wpa_printf(MSG_DEBUG, "nl80211: Cancel TX frame wait: cookie=0x%llx",
10018 (long long unsigned int) drv->send_action_cookie);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010019 nl80211_cmd(drv, msg, 0, NL80211_CMD_FRAME_WAIT_CANCEL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010020
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010021 if (nl80211_set_iface_id(msg, bss) < 0)
10022 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010023 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->send_action_cookie);
10024
10025 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
10026 msg = NULL;
10027 if (ret)
10028 wpa_printf(MSG_DEBUG, "nl80211: wait cancel failed: ret=%d "
10029 "(%s)", ret, strerror(-ret));
10030
10031 nla_put_failure:
10032 nlmsg_free(msg);
10033}
10034
10035
10036static int wpa_driver_nl80211_remain_on_channel(void *priv, unsigned int freq,
10037 unsigned int duration)
10038{
10039 struct i802_bss *bss = priv;
10040 struct wpa_driver_nl80211_data *drv = bss->drv;
10041 struct nl_msg *msg;
10042 int ret;
10043 u64 cookie;
10044
10045 msg = nlmsg_alloc();
10046 if (!msg)
10047 return -1;
10048
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010049 nl80211_cmd(drv, msg, 0, NL80211_CMD_REMAIN_ON_CHANNEL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010050
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010051 if (nl80211_set_iface_id(msg, bss) < 0)
10052 goto nla_put_failure;
10053
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010054 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
10055 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
10056
10057 cookie = 0;
10058 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010059 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010060 if (ret == 0) {
10061 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel cookie "
10062 "0x%llx for freq=%u MHz duration=%u",
10063 (long long unsigned int) cookie, freq, duration);
10064 drv->remain_on_chan_cookie = cookie;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010065 drv->pending_remain_on_chan = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010066 return 0;
10067 }
10068 wpa_printf(MSG_DEBUG, "nl80211: Failed to request remain-on-channel "
10069 "(freq=%d duration=%u): %d (%s)",
10070 freq, duration, ret, strerror(-ret));
10071nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010072 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010073 return -1;
10074}
10075
10076
10077static int wpa_driver_nl80211_cancel_remain_on_channel(void *priv)
10078{
10079 struct i802_bss *bss = priv;
10080 struct wpa_driver_nl80211_data *drv = bss->drv;
10081 struct nl_msg *msg;
10082 int ret;
10083
10084 if (!drv->pending_remain_on_chan) {
10085 wpa_printf(MSG_DEBUG, "nl80211: No pending remain-on-channel "
10086 "to cancel");
10087 return -1;
10088 }
10089
10090 wpa_printf(MSG_DEBUG, "nl80211: Cancel remain-on-channel with cookie "
10091 "0x%llx",
10092 (long long unsigned int) drv->remain_on_chan_cookie);
10093
10094 msg = nlmsg_alloc();
10095 if (!msg)
10096 return -1;
10097
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010098 nl80211_cmd(drv, msg, 0, NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010099
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010100 if (nl80211_set_iface_id(msg, bss) < 0)
10101 goto nla_put_failure;
10102
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010103 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->remain_on_chan_cookie);
10104
10105 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010106 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010107 if (ret == 0)
10108 return 0;
10109 wpa_printf(MSG_DEBUG, "nl80211: Failed to cancel remain-on-channel: "
10110 "%d (%s)", ret, strerror(-ret));
10111nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010112 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010113 return -1;
10114}
10115
10116
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010117static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss, int report)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010118{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010119 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt6e933c12011-09-27 12:29:26 -070010120
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010121 if (!report) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010122 if (bss->nl_preq && drv->device_ap_sme &&
10123 is_ap_interface(drv->nlmode)) {
10124 /*
10125 * Do not disable Probe Request reporting that was
10126 * enabled in nl80211_setup_ap().
10127 */
10128 wpa_printf(MSG_DEBUG, "nl80211: Skip disabling of "
10129 "Probe Request reporting nl_preq=%p while "
10130 "in AP mode", bss->nl_preq);
10131 } else if (bss->nl_preq) {
10132 wpa_printf(MSG_DEBUG, "nl80211: Disable Probe Request "
10133 "reporting nl_preq=%p", bss->nl_preq);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070010134 nl80211_destroy_eloop_handle(&bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010135 }
10136 return 0;
10137 }
10138
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010139 if (bss->nl_preq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010140 wpa_printf(MSG_DEBUG, "nl80211: Probe Request reporting "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010141 "already on! nl_preq=%p", bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010142 return 0;
10143 }
10144
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010145 bss->nl_preq = nl_create_handle(drv->global->nl_cb, "preq");
10146 if (bss->nl_preq == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010147 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010148 wpa_printf(MSG_DEBUG, "nl80211: Enable Probe Request "
10149 "reporting nl_preq=%p", bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010150
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010151 if (nl80211_register_frame(bss, bss->nl_preq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010152 (WLAN_FC_TYPE_MGMT << 2) |
10153 (WLAN_FC_STYPE_PROBE_REQ << 4),
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010154 NULL, 0) < 0)
10155 goto out_err;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -070010156
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070010157 nl80211_register_eloop_read(&bss->nl_preq,
10158 wpa_driver_nl80211_event_receive,
10159 bss->nl_cb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010160
10161 return 0;
10162
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010163 out_err:
10164 nl_destroy_handles(&bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010165 return -1;
10166}
10167
10168
10169static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
10170 int ifindex, int disabled)
10171{
10172 struct nl_msg *msg;
10173 struct nlattr *bands, *band;
10174 int ret;
10175
10176 msg = nlmsg_alloc();
10177 if (!msg)
10178 return -1;
10179
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010180 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_TX_BITRATE_MASK);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010181 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
10182
10183 bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES);
10184 if (!bands)
10185 goto nla_put_failure;
10186
10187 /*
10188 * Disable 2 GHz rates 1, 2, 5.5, 11 Mbps by masking out everything
10189 * else apart from 6, 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS
10190 * rates. All 5 GHz rates are left enabled.
10191 */
10192 band = nla_nest_start(msg, NL80211_BAND_2GHZ);
10193 if (!band)
10194 goto nla_put_failure;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010195 if (disabled) {
10196 NLA_PUT(msg, NL80211_TXRATE_LEGACY, 8,
10197 "\x0c\x12\x18\x24\x30\x48\x60\x6c");
10198 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010199 nla_nest_end(msg, band);
10200
10201 nla_nest_end(msg, bands);
10202
10203 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
10204 msg = NULL;
10205 if (ret) {
10206 wpa_printf(MSG_DEBUG, "nl80211: Set TX rates failed: ret=%d "
10207 "(%s)", ret, strerror(-ret));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070010208 } else
10209 drv->disabled_11b_rates = disabled;
10210
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010211 return ret;
10212
10213nla_put_failure:
10214 nlmsg_free(msg);
10215 return -1;
10216}
10217
10218
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010219static int wpa_driver_nl80211_deinit_ap(void *priv)
10220{
10221 struct i802_bss *bss = priv;
10222 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010223 if (!is_ap_interface(drv->nlmode))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010224 return -1;
10225 wpa_driver_nl80211_del_beacon(drv);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070010226
10227 /*
10228 * If the P2P GO interface was dynamically added, then it is
10229 * possible that the interface change to station is not possible.
10230 */
10231 if (drv->nlmode == NL80211_IFTYPE_P2P_GO && bss->if_dynamic)
10232 return 0;
10233
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010234 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010235}
10236
10237
Dmitry Shmidtea69e842013-05-13 14:52:28 -070010238static int wpa_driver_nl80211_stop_ap(void *priv)
10239{
10240 struct i802_bss *bss = priv;
10241 struct wpa_driver_nl80211_data *drv = bss->drv;
10242 if (!is_ap_interface(drv->nlmode))
10243 return -1;
10244 wpa_driver_nl80211_del_beacon(drv);
10245 bss->beacon_set = 0;
10246 return 0;
10247}
10248
10249
Dmitry Shmidt04949592012-07-19 12:16:46 -070010250static int wpa_driver_nl80211_deinit_p2p_cli(void *priv)
10251{
10252 struct i802_bss *bss = priv;
10253 struct wpa_driver_nl80211_data *drv = bss->drv;
10254 if (drv->nlmode != NL80211_IFTYPE_P2P_CLIENT)
10255 return -1;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070010256
10257 /*
10258 * If the P2P Client interface was dynamically added, then it is
10259 * possible that the interface change to station is not possible.
10260 */
10261 if (bss->if_dynamic)
10262 return 0;
10263
Dmitry Shmidt04949592012-07-19 12:16:46 -070010264 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
10265}
10266
10267
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010268static void wpa_driver_nl80211_resume(void *priv)
10269{
10270 struct i802_bss *bss = priv;
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010271
10272 if (i802_set_iface_flags(bss, 1))
10273 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface up on resume event");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010274}
10275
10276
10277static int nl80211_send_ft_action(void *priv, u8 action, const u8 *target_ap,
10278 const u8 *ies, size_t ies_len)
10279{
10280 struct i802_bss *bss = priv;
10281 struct wpa_driver_nl80211_data *drv = bss->drv;
10282 int ret;
10283 u8 *data, *pos;
10284 size_t data_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010285 const u8 *own_addr = bss->addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010286
10287 if (action != 1) {
10288 wpa_printf(MSG_ERROR, "nl80211: Unsupported send_ft_action "
10289 "action %d", action);
10290 return -1;
10291 }
10292
10293 /*
10294 * Action frame payload:
10295 * Category[1] = 6 (Fast BSS Transition)
10296 * Action[1] = 1 (Fast BSS Transition Request)
10297 * STA Address
10298 * Target AP Address
10299 * FT IEs
10300 */
10301
10302 data_len = 2 + 2 * ETH_ALEN + ies_len;
10303 data = os_malloc(data_len);
10304 if (data == NULL)
10305 return -1;
10306 pos = data;
10307 *pos++ = 0x06; /* FT Action category */
10308 *pos++ = action;
10309 os_memcpy(pos, own_addr, ETH_ALEN);
10310 pos += ETH_ALEN;
10311 os_memcpy(pos, target_ap, ETH_ALEN);
10312 pos += ETH_ALEN;
10313 os_memcpy(pos, ies, ies_len);
10314
10315 ret = wpa_driver_nl80211_send_action(bss, drv->assoc_freq, 0,
10316 drv->bssid, own_addr, drv->bssid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010317 data, data_len, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010318 os_free(data);
10319
10320 return ret;
10321}
10322
10323
10324static int nl80211_signal_monitor(void *priv, int threshold, int hysteresis)
10325{
10326 struct i802_bss *bss = priv;
10327 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -070010328 struct nl_msg *msg;
10329 struct nlattr *cqm;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070010330 int ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010331
10332 wpa_printf(MSG_DEBUG, "nl80211: Signal monitor threshold=%d "
10333 "hysteresis=%d", threshold, hysteresis);
10334
10335 msg = nlmsg_alloc();
10336 if (!msg)
10337 return -1;
10338
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010339 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_CQM);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010340
10341 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
10342
Dmitry Shmidt8da800a2013-04-24 12:57:01 -070010343 cqm = nla_nest_start(msg, NL80211_ATTR_CQM);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010344 if (cqm == NULL)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070010345 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010346
Dmitry Shmidt8da800a2013-04-24 12:57:01 -070010347 NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_THOLD, threshold);
10348 NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_HYST, hysteresis);
10349 nla_nest_end(msg, cqm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010350
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070010351 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010352 msg = NULL;
10353
10354nla_put_failure:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010355 nlmsg_free(msg);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070010356 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010357}
10358
10359
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010360static int get_channel_width(struct nl_msg *msg, void *arg)
10361{
10362 struct nlattr *tb[NL80211_ATTR_MAX + 1];
10363 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
10364 struct wpa_signal_info *sig_change = arg;
10365
10366 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
10367 genlmsg_attrlen(gnlh, 0), NULL);
10368
10369 sig_change->center_frq1 = -1;
10370 sig_change->center_frq2 = -1;
10371 sig_change->chanwidth = CHAN_WIDTH_UNKNOWN;
10372
10373 if (tb[NL80211_ATTR_CHANNEL_WIDTH]) {
10374 sig_change->chanwidth = convert2width(
10375 nla_get_u32(tb[NL80211_ATTR_CHANNEL_WIDTH]));
10376 if (tb[NL80211_ATTR_CENTER_FREQ1])
10377 sig_change->center_frq1 =
10378 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]);
10379 if (tb[NL80211_ATTR_CENTER_FREQ2])
10380 sig_change->center_frq2 =
10381 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]);
10382 }
10383
10384 return NL_SKIP;
10385}
10386
10387
10388static int nl80211_get_channel_width(struct wpa_driver_nl80211_data *drv,
10389 struct wpa_signal_info *sig)
10390{
10391 struct nl_msg *msg;
10392
10393 msg = nlmsg_alloc();
10394 if (!msg)
10395 return -ENOMEM;
10396
10397 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_INTERFACE);
10398 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
10399
10400 return send_and_recv_msgs(drv, msg, get_channel_width, sig);
10401
10402nla_put_failure:
10403 nlmsg_free(msg);
10404 return -ENOBUFS;
10405}
10406
10407
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010408static int nl80211_signal_poll(void *priv, struct wpa_signal_info *si)
10409{
10410 struct i802_bss *bss = priv;
10411 struct wpa_driver_nl80211_data *drv = bss->drv;
10412 int res;
10413
10414 os_memset(si, 0, sizeof(*si));
10415 res = nl80211_get_link_signal(drv, si);
10416 if (res != 0)
10417 return res;
10418
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010419 res = nl80211_get_channel_width(drv, si);
10420 if (res != 0)
10421 return res;
10422
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010423 return nl80211_get_link_noise(drv, si);
10424}
10425
10426
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010427static int wpa_driver_nl80211_shared_freq(void *priv)
10428{
10429 struct i802_bss *bss = priv;
10430 struct wpa_driver_nl80211_data *drv = bss->drv;
10431 struct wpa_driver_nl80211_data *driver;
10432 int freq = 0;
10433
10434 /*
10435 * If the same PHY is in connected state with some other interface,
10436 * then retrieve the assoc freq.
10437 */
10438 wpa_printf(MSG_DEBUG, "nl80211: Get shared freq for PHY %s",
10439 drv->phyname);
10440
10441 dl_list_for_each(driver, &drv->global->interfaces,
10442 struct wpa_driver_nl80211_data, list) {
10443 if (drv == driver ||
10444 os_strcmp(drv->phyname, driver->phyname) != 0 ||
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010445 !driver->associated)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010446 continue;
10447
10448 wpa_printf(MSG_DEBUG, "nl80211: Found a match for PHY %s - %s "
10449 MACSTR,
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010450 driver->phyname, driver->first_bss->ifname,
10451 MAC2STR(driver->first_bss->addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -070010452 if (is_ap_interface(driver->nlmode))
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010453 freq = driver->first_bss->freq;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010454 else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010455 freq = nl80211_get_assoc_freq(driver);
10456 wpa_printf(MSG_DEBUG, "nl80211: Shared freq for PHY %s: %d",
10457 drv->phyname, freq);
10458 }
10459
10460 if (!freq)
10461 wpa_printf(MSG_DEBUG, "nl80211: No shared interface for "
10462 "PHY (%s) in associated state", drv->phyname);
10463
10464 return freq;
10465}
10466
10467
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010468static int nl80211_send_frame(void *priv, const u8 *data, size_t data_len,
10469 int encrypt)
10470{
10471 struct i802_bss *bss = priv;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080010472 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt, 0,
10473 0, 0, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010474}
10475
10476
10477static int nl80211_set_param(void *priv, const char *param)
10478{
10479 wpa_printf(MSG_DEBUG, "nl80211: driver param='%s'", param);
10480 if (param == NULL)
10481 return 0;
10482
10483#ifdef CONFIG_P2P
10484 if (os_strstr(param, "use_p2p_group_interface=1")) {
10485 struct i802_bss *bss = priv;
10486 struct wpa_driver_nl80211_data *drv = bss->drv;
10487
10488 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
10489 "interface");
10490 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
10491 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
10492 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010493
10494 if (os_strstr(param, "p2p_device=1")) {
10495 struct i802_bss *bss = priv;
10496 struct wpa_driver_nl80211_data *drv = bss->drv;
10497 drv->allow_p2p_device = 1;
10498 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010499#endif /* CONFIG_P2P */
10500
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080010501 if (os_strstr(param, "use_monitor=1")) {
10502 struct i802_bss *bss = priv;
10503 struct wpa_driver_nl80211_data *drv = bss->drv;
10504 drv->use_monitor = 1;
10505 }
10506
10507 if (os_strstr(param, "force_connect_cmd=1")) {
10508 struct i802_bss *bss = priv;
10509 struct wpa_driver_nl80211_data *drv = bss->drv;
10510 drv->capa.flags &= ~WPA_DRIVER_FLAGS_SME;
10511 }
10512
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010513 return 0;
10514}
10515
10516
10517static void * nl80211_global_init(void)
10518{
10519 struct nl80211_global *global;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010520 struct netlink_config *cfg;
10521
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010522 global = os_zalloc(sizeof(*global));
10523 if (global == NULL)
10524 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010525 global->ioctl_sock = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010526 dl_list_init(&global->interfaces);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010527 global->if_add_ifindex = -1;
10528
10529 cfg = os_zalloc(sizeof(*cfg));
10530 if (cfg == NULL)
10531 goto err;
10532
10533 cfg->ctx = global;
10534 cfg->newlink_cb = wpa_driver_nl80211_event_rtm_newlink;
10535 cfg->dellink_cb = wpa_driver_nl80211_event_rtm_dellink;
10536 global->netlink = netlink_init(cfg);
10537 if (global->netlink == NULL) {
10538 os_free(cfg);
10539 goto err;
10540 }
10541
10542 if (wpa_driver_nl80211_init_nl_global(global) < 0)
10543 goto err;
10544
10545 global->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
10546 if (global->ioctl_sock < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070010547 wpa_printf(MSG_ERROR, "nl80211: socket(PF_INET,SOCK_DGRAM) failed: %s",
10548 strerror(errno));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010549 goto err;
10550 }
10551
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010552 return global;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010553
10554err:
10555 nl80211_global_deinit(global);
10556 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010557}
10558
10559
10560static void nl80211_global_deinit(void *priv)
10561{
10562 struct nl80211_global *global = priv;
10563 if (global == NULL)
10564 return;
10565 if (!dl_list_empty(&global->interfaces)) {
10566 wpa_printf(MSG_ERROR, "nl80211: %u interface(s) remain at "
10567 "nl80211_global_deinit",
10568 dl_list_len(&global->interfaces));
10569 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010570
10571 if (global->netlink)
10572 netlink_deinit(global->netlink);
10573
10574 nl_destroy_handles(&global->nl);
10575
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070010576 if (global->nl_event)
10577 nl80211_destroy_eloop_handle(&global->nl_event);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010578
10579 nl_cb_put(global->nl_cb);
10580
10581 if (global->ioctl_sock >= 0)
10582 close(global->ioctl_sock);
10583
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010584 os_free(global);
10585}
10586
10587
10588static const char * nl80211_get_radio_name(void *priv)
10589{
10590 struct i802_bss *bss = priv;
10591 struct wpa_driver_nl80211_data *drv = bss->drv;
10592 return drv->phyname;
10593}
10594
10595
Jouni Malinen75ecf522011-06-27 15:19:46 -070010596static int nl80211_pmkid(struct i802_bss *bss, int cmd, const u8 *bssid,
10597 const u8 *pmkid)
10598{
10599 struct nl_msg *msg;
10600
10601 msg = nlmsg_alloc();
10602 if (!msg)
10603 return -ENOMEM;
10604
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010605 nl80211_cmd(bss->drv, msg, 0, cmd);
Jouni Malinen75ecf522011-06-27 15:19:46 -070010606
10607 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
10608 if (pmkid)
10609 NLA_PUT(msg, NL80211_ATTR_PMKID, 16, pmkid);
10610 if (bssid)
10611 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
10612
10613 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
10614 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010615 nlmsg_free(msg);
Jouni Malinen75ecf522011-06-27 15:19:46 -070010616 return -ENOBUFS;
10617}
10618
10619
10620static int nl80211_add_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
10621{
10622 struct i802_bss *bss = priv;
10623 wpa_printf(MSG_DEBUG, "nl80211: Add PMKID for " MACSTR, MAC2STR(bssid));
10624 return nl80211_pmkid(bss, NL80211_CMD_SET_PMKSA, bssid, pmkid);
10625}
10626
10627
10628static int nl80211_remove_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
10629{
10630 struct i802_bss *bss = priv;
10631 wpa_printf(MSG_DEBUG, "nl80211: Delete PMKID for " MACSTR,
10632 MAC2STR(bssid));
10633 return nl80211_pmkid(bss, NL80211_CMD_DEL_PMKSA, bssid, pmkid);
10634}
10635
10636
10637static int nl80211_flush_pmkid(void *priv)
10638{
10639 struct i802_bss *bss = priv;
10640 wpa_printf(MSG_DEBUG, "nl80211: Flush PMKIDs");
10641 return nl80211_pmkid(bss, NL80211_CMD_FLUSH_PMKSA, NULL, NULL);
10642}
10643
10644
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070010645static void clean_survey_results(struct survey_results *survey_results)
10646{
10647 struct freq_survey *survey, *tmp;
10648
10649 if (dl_list_empty(&survey_results->survey_list))
10650 return;
10651
10652 dl_list_for_each_safe(survey, tmp, &survey_results->survey_list,
10653 struct freq_survey, list) {
10654 dl_list_del(&survey->list);
10655 os_free(survey);
10656 }
10657}
10658
10659
10660static void add_survey(struct nlattr **sinfo, u32 ifidx,
10661 struct dl_list *survey_list)
10662{
10663 struct freq_survey *survey;
10664
10665 survey = os_zalloc(sizeof(struct freq_survey));
10666 if (!survey)
10667 return;
10668
10669 survey->ifidx = ifidx;
10670 survey->freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
10671 survey->filled = 0;
10672
10673 if (sinfo[NL80211_SURVEY_INFO_NOISE]) {
10674 survey->nf = (int8_t)
10675 nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
10676 survey->filled |= SURVEY_HAS_NF;
10677 }
10678
10679 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]) {
10680 survey->channel_time =
10681 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]);
10682 survey->filled |= SURVEY_HAS_CHAN_TIME;
10683 }
10684
10685 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]) {
10686 survey->channel_time_busy =
10687 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]);
10688 survey->filled |= SURVEY_HAS_CHAN_TIME_BUSY;
10689 }
10690
10691 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]) {
10692 survey->channel_time_rx =
10693 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]);
10694 survey->filled |= SURVEY_HAS_CHAN_TIME_RX;
10695 }
10696
10697 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]) {
10698 survey->channel_time_tx =
10699 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]);
10700 survey->filled |= SURVEY_HAS_CHAN_TIME_TX;
10701 }
10702
10703 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)",
10704 survey->freq,
10705 survey->nf,
10706 (unsigned long int) survey->channel_time,
10707 (unsigned long int) survey->channel_time_busy,
10708 (unsigned long int) survey->channel_time_tx,
10709 (unsigned long int) survey->channel_time_rx,
10710 survey->filled);
10711
10712 dl_list_add_tail(survey_list, &survey->list);
10713}
10714
10715
10716static int check_survey_ok(struct nlattr **sinfo, u32 surveyed_freq,
10717 unsigned int freq_filter)
10718{
10719 if (!freq_filter)
10720 return 1;
10721
10722 return freq_filter == surveyed_freq;
10723}
10724
10725
10726static int survey_handler(struct nl_msg *msg, void *arg)
10727{
10728 struct nlattr *tb[NL80211_ATTR_MAX + 1];
10729 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
10730 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
10731 struct survey_results *survey_results;
10732 u32 surveyed_freq = 0;
10733 u32 ifidx;
10734
10735 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
10736 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
10737 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
10738 };
10739
10740 survey_results = (struct survey_results *) arg;
10741
10742 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
10743 genlmsg_attrlen(gnlh, 0), NULL);
10744
Dmitry Shmidt97672262014-02-03 13:02:54 -080010745 if (!tb[NL80211_ATTR_IFINDEX])
10746 return NL_SKIP;
10747
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070010748 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
10749
10750 if (!tb[NL80211_ATTR_SURVEY_INFO])
10751 return NL_SKIP;
10752
10753 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
10754 tb[NL80211_ATTR_SURVEY_INFO],
10755 survey_policy))
10756 return NL_SKIP;
10757
10758 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY]) {
10759 wpa_printf(MSG_ERROR, "nl80211: Invalid survey data");
10760 return NL_SKIP;
10761 }
10762
10763 surveyed_freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
10764
10765 if (!check_survey_ok(sinfo, surveyed_freq,
10766 survey_results->freq_filter))
10767 return NL_SKIP;
10768
10769 if (survey_results->freq_filter &&
10770 survey_results->freq_filter != surveyed_freq) {
10771 wpa_printf(MSG_EXCESSIVE, "nl80211: Ignoring survey data for freq %d MHz",
10772 surveyed_freq);
10773 return NL_SKIP;
10774 }
10775
10776 add_survey(sinfo, ifidx, &survey_results->survey_list);
10777
10778 return NL_SKIP;
10779}
10780
10781
10782static int wpa_driver_nl80211_get_survey(void *priv, unsigned int freq)
10783{
10784 struct i802_bss *bss = priv;
10785 struct wpa_driver_nl80211_data *drv = bss->drv;
10786 struct nl_msg *msg;
10787 int err = -ENOBUFS;
10788 union wpa_event_data data;
10789 struct survey_results *survey_results;
10790
10791 os_memset(&data, 0, sizeof(data));
10792 survey_results = &data.survey_results;
10793
10794 dl_list_init(&survey_results->survey_list);
10795
10796 msg = nlmsg_alloc();
10797 if (!msg)
10798 goto nla_put_failure;
10799
10800 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
10801
10802 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
10803
10804 if (freq)
10805 data.survey_results.freq_filter = freq;
10806
10807 do {
10808 wpa_printf(MSG_DEBUG, "nl80211: Fetch survey data");
10809 err = send_and_recv_msgs(drv, msg, survey_handler,
10810 survey_results);
10811 } while (err > 0);
10812
10813 if (err) {
10814 wpa_printf(MSG_ERROR, "nl80211: Failed to process survey data");
10815 goto out_clean;
10816 }
10817
10818 wpa_supplicant_event(drv->ctx, EVENT_SURVEY, &data);
10819
10820out_clean:
10821 clean_survey_results(survey_results);
10822nla_put_failure:
10823 return err;
10824}
10825
10826
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010827static void nl80211_set_rekey_info(void *priv, const u8 *kek, const u8 *kck,
10828 const u8 *replay_ctr)
10829{
10830 struct i802_bss *bss = priv;
10831 struct wpa_driver_nl80211_data *drv = bss->drv;
10832 struct nlattr *replay_nested;
10833 struct nl_msg *msg;
10834
10835 msg = nlmsg_alloc();
10836 if (!msg)
10837 return;
10838
10839 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
10840
10841 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
10842
10843 replay_nested = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
10844 if (!replay_nested)
10845 goto nla_put_failure;
10846
10847 NLA_PUT(msg, NL80211_REKEY_DATA_KEK, NL80211_KEK_LEN, kek);
10848 NLA_PUT(msg, NL80211_REKEY_DATA_KCK, NL80211_KCK_LEN, kck);
10849 NLA_PUT(msg, NL80211_REKEY_DATA_REPLAY_CTR, NL80211_REPLAY_CTR_LEN,
10850 replay_ctr);
10851
10852 nla_nest_end(msg, replay_nested);
10853
10854 send_and_recv_msgs(drv, msg, NULL, NULL);
10855 return;
10856 nla_put_failure:
10857 nlmsg_free(msg);
10858}
10859
10860
10861static void nl80211_send_null_frame(struct i802_bss *bss, const u8 *own_addr,
10862 const u8 *addr, int qos)
10863{
10864 /* send data frame to poll STA and check whether
10865 * this frame is ACKed */
10866 struct {
10867 struct ieee80211_hdr hdr;
10868 u16 qos_ctl;
10869 } STRUCT_PACKED nulldata;
10870 size_t size;
10871
10872 /* Send data frame to poll STA and check whether this frame is ACKed */
10873
10874 os_memset(&nulldata, 0, sizeof(nulldata));
10875
10876 if (qos) {
10877 nulldata.hdr.frame_control =
10878 IEEE80211_FC(WLAN_FC_TYPE_DATA,
10879 WLAN_FC_STYPE_QOS_NULL);
10880 size = sizeof(nulldata);
10881 } else {
10882 nulldata.hdr.frame_control =
10883 IEEE80211_FC(WLAN_FC_TYPE_DATA,
10884 WLAN_FC_STYPE_NULLFUNC);
10885 size = sizeof(struct ieee80211_hdr);
10886 }
10887
10888 nulldata.hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS);
10889 os_memcpy(nulldata.hdr.IEEE80211_DA_FROMDS, addr, ETH_ALEN);
10890 os_memcpy(nulldata.hdr.IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
10891 os_memcpy(nulldata.hdr.IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
10892
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010893 if (wpa_driver_nl80211_send_mlme(bss, (u8 *) &nulldata, size, 0, 0, 0,
10894 0, 0) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010895 wpa_printf(MSG_DEBUG, "nl80211_send_null_frame: Failed to "
10896 "send poll frame");
10897}
10898
10899static void nl80211_poll_client(void *priv, const u8 *own_addr, const u8 *addr,
10900 int qos)
10901{
10902 struct i802_bss *bss = priv;
10903 struct wpa_driver_nl80211_data *drv = bss->drv;
10904 struct nl_msg *msg;
10905
10906 if (!drv->poll_command_supported) {
10907 nl80211_send_null_frame(bss, own_addr, addr, qos);
10908 return;
10909 }
10910
10911 msg = nlmsg_alloc();
10912 if (!msg)
10913 return;
10914
10915 nl80211_cmd(drv, msg, 0, NL80211_CMD_PROBE_CLIENT);
10916
10917 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
10918 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
10919
10920 send_and_recv_msgs(drv, msg, NULL, NULL);
10921 return;
10922 nla_put_failure:
10923 nlmsg_free(msg);
10924}
10925
10926
10927static int nl80211_set_power_save(struct i802_bss *bss, int enabled)
10928{
10929 struct nl_msg *msg;
10930
10931 msg = nlmsg_alloc();
10932 if (!msg)
10933 return -ENOMEM;
10934
10935 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_SET_POWER_SAVE);
10936 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
10937 NLA_PUT_U32(msg, NL80211_ATTR_PS_STATE,
10938 enabled ? NL80211_PS_ENABLED : NL80211_PS_DISABLED);
10939 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
10940nla_put_failure:
10941 nlmsg_free(msg);
10942 return -ENOBUFS;
10943}
10944
10945
10946static int nl80211_set_p2p_powersave(void *priv, int legacy_ps, int opp_ps,
10947 int ctwindow)
10948{
10949 struct i802_bss *bss = priv;
10950
10951 wpa_printf(MSG_DEBUG, "nl80211: set_p2p_powersave (legacy_ps=%d "
10952 "opp_ps=%d ctwindow=%d)", legacy_ps, opp_ps, ctwindow);
10953
Dmitry Shmidt292b0c32013-11-22 12:54:42 -080010954 if (opp_ps != -1 || ctwindow != -1) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080010955#ifdef ANDROID_P2P
10956 wpa_driver_set_p2p_ps(priv, legacy_ps, opp_ps, ctwindow);
Dmitry Shmidt292b0c32013-11-22 12:54:42 -080010957#else /* ANDROID_P2P */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010958 return -1; /* Not yet supported */
Dmitry Shmidt292b0c32013-11-22 12:54:42 -080010959#endif /* ANDROID_P2P */
10960 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010961
10962 if (legacy_ps == -1)
10963 return 0;
10964 if (legacy_ps != 0 && legacy_ps != 1)
10965 return -1; /* Not yet supported */
10966
10967 return nl80211_set_power_save(bss, legacy_ps);
10968}
10969
10970
Dmitry Shmidt051af732013-10-22 13:52:46 -070010971static int nl80211_start_radar_detection(void *priv,
10972 struct hostapd_freq_params *freq)
Dmitry Shmidtea69e842013-05-13 14:52:28 -070010973{
10974 struct i802_bss *bss = priv;
10975 struct wpa_driver_nl80211_data *drv = bss->drv;
10976 struct nl_msg *msg;
10977 int ret;
10978
Dmitry Shmidt051af732013-10-22 13:52:46 -070010979 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)",
10980 freq->freq, freq->ht_enabled, freq->vht_enabled,
10981 freq->bandwidth, freq->center_freq1, freq->center_freq2);
10982
Dmitry Shmidtea69e842013-05-13 14:52:28 -070010983 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_RADAR)) {
10984 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support radar "
10985 "detection");
10986 return -1;
10987 }
10988
10989 msg = nlmsg_alloc();
10990 if (!msg)
10991 return -1;
10992
10993 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_RADAR_DETECT);
10994 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
Dmitry Shmidt051af732013-10-22 13:52:46 -070010995 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq->freq);
Dmitry Shmidtea69e842013-05-13 14:52:28 -070010996
Dmitry Shmidt051af732013-10-22 13:52:46 -070010997 if (freq->vht_enabled) {
10998 switch (freq->bandwidth) {
10999 case 20:
11000 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
11001 NL80211_CHAN_WIDTH_20);
11002 break;
11003 case 40:
11004 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
11005 NL80211_CHAN_WIDTH_40);
11006 break;
11007 case 80:
11008 if (freq->center_freq2)
11009 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
11010 NL80211_CHAN_WIDTH_80P80);
11011 else
11012 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
11013 NL80211_CHAN_WIDTH_80);
11014 break;
11015 case 160:
11016 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
11017 NL80211_CHAN_WIDTH_160);
11018 break;
11019 default:
11020 return -1;
11021 }
11022 NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ1, freq->center_freq1);
11023 if (freq->center_freq2)
11024 NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ2,
11025 freq->center_freq2);
11026 } else if (freq->ht_enabled) {
11027 switch (freq->sec_channel_offset) {
11028 case -1:
11029 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
11030 NL80211_CHAN_HT40MINUS);
11031 break;
11032 case 1:
11033 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
11034 NL80211_CHAN_HT40PLUS);
11035 break;
11036 default:
11037 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
11038 NL80211_CHAN_HT20);
11039 break;
11040 }
11041 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -070011042
11043 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
11044 if (ret == 0)
11045 return 0;
11046 wpa_printf(MSG_DEBUG, "nl80211: Failed to start radar detection: "
11047 "%d (%s)", ret, strerror(-ret));
11048nla_put_failure:
11049 return -1;
11050}
11051
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011052#ifdef CONFIG_TDLS
11053
11054static int nl80211_send_tdls_mgmt(void *priv, const u8 *dst, u8 action_code,
11055 u8 dialog_token, u16 status_code,
11056 const u8 *buf, size_t len)
11057{
11058 struct i802_bss *bss = priv;
11059 struct wpa_driver_nl80211_data *drv = bss->drv;
11060 struct nl_msg *msg;
11061
11062 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
11063 return -EOPNOTSUPP;
11064
11065 if (!dst)
11066 return -EINVAL;
11067
11068 msg = nlmsg_alloc();
11069 if (!msg)
11070 return -ENOMEM;
11071
11072 nl80211_cmd(drv, msg, 0, NL80211_CMD_TDLS_MGMT);
11073 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
11074 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
11075 NLA_PUT_U8(msg, NL80211_ATTR_TDLS_ACTION, action_code);
11076 NLA_PUT_U8(msg, NL80211_ATTR_TDLS_DIALOG_TOKEN, dialog_token);
11077 NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status_code);
11078 NLA_PUT(msg, NL80211_ATTR_IE, len, buf);
11079
11080 return send_and_recv_msgs(drv, msg, NULL, NULL);
11081
11082nla_put_failure:
11083 nlmsg_free(msg);
11084 return -ENOBUFS;
11085}
11086
11087
11088static int nl80211_tdls_oper(void *priv, enum tdls_oper oper, const u8 *peer)
11089{
11090 struct i802_bss *bss = priv;
11091 struct wpa_driver_nl80211_data *drv = bss->drv;
11092 struct nl_msg *msg;
11093 enum nl80211_tdls_operation nl80211_oper;
11094
11095 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
11096 return -EOPNOTSUPP;
11097
11098 switch (oper) {
11099 case TDLS_DISCOVERY_REQ:
11100 nl80211_oper = NL80211_TDLS_DISCOVERY_REQ;
11101 break;
11102 case TDLS_SETUP:
11103 nl80211_oper = NL80211_TDLS_SETUP;
11104 break;
11105 case TDLS_TEARDOWN:
11106 nl80211_oper = NL80211_TDLS_TEARDOWN;
11107 break;
11108 case TDLS_ENABLE_LINK:
11109 nl80211_oper = NL80211_TDLS_ENABLE_LINK;
11110 break;
11111 case TDLS_DISABLE_LINK:
11112 nl80211_oper = NL80211_TDLS_DISABLE_LINK;
11113 break;
11114 case TDLS_ENABLE:
11115 return 0;
11116 case TDLS_DISABLE:
11117 return 0;
11118 default:
11119 return -EINVAL;
11120 }
11121
11122 msg = nlmsg_alloc();
11123 if (!msg)
11124 return -ENOMEM;
11125
11126 nl80211_cmd(drv, msg, 0, NL80211_CMD_TDLS_OPER);
11127 NLA_PUT_U8(msg, NL80211_ATTR_TDLS_OPERATION, nl80211_oper);
11128 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
11129 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, peer);
11130
11131 return send_and_recv_msgs(drv, msg, NULL, NULL);
11132
11133nla_put_failure:
11134 nlmsg_free(msg);
11135 return -ENOBUFS;
11136}
11137
11138#endif /* CONFIG TDLS */
11139
11140
11141#ifdef ANDROID
11142
11143typedef struct android_wifi_priv_cmd {
11144 char *buf;
11145 int used_len;
11146 int total_len;
11147} android_wifi_priv_cmd;
11148
11149static int drv_errors = 0;
11150
11151static void wpa_driver_send_hang_msg(struct wpa_driver_nl80211_data *drv)
11152{
11153 drv_errors++;
11154 if (drv_errors > DRV_NUMBER_SEQUENTIAL_ERRORS) {
11155 drv_errors = 0;
11156 wpa_msg(drv->ctx, MSG_INFO, WPA_EVENT_DRIVER_STATE "HANGED");
11157 }
11158}
11159
11160
11161static int android_priv_cmd(struct i802_bss *bss, const char *cmd)
11162{
11163 struct wpa_driver_nl80211_data *drv = bss->drv;
11164 struct ifreq ifr;
11165 android_wifi_priv_cmd priv_cmd;
11166 char buf[MAX_DRV_CMD_SIZE];
11167 int ret;
11168
11169 os_memset(&ifr, 0, sizeof(ifr));
11170 os_memset(&priv_cmd, 0, sizeof(priv_cmd));
11171 os_strlcpy(ifr.ifr_name, bss->ifname, IFNAMSIZ);
11172
11173 os_memset(buf, 0, sizeof(buf));
11174 os_strlcpy(buf, cmd, sizeof(buf));
11175
11176 priv_cmd.buf = buf;
11177 priv_cmd.used_len = sizeof(buf);
11178 priv_cmd.total_len = sizeof(buf);
11179 ifr.ifr_data = &priv_cmd;
11180
11181 ret = ioctl(drv->global->ioctl_sock, SIOCDEVPRIVATE + 1, &ifr);
11182 if (ret < 0) {
11183 wpa_printf(MSG_ERROR, "%s: failed to issue private commands",
11184 __func__);
11185 wpa_driver_send_hang_msg(drv);
11186 return ret;
11187 }
11188
11189 drv_errors = 0;
11190 return 0;
11191}
11192
11193
11194static int android_pno_start(struct i802_bss *bss,
11195 struct wpa_driver_scan_params *params)
11196{
11197 struct wpa_driver_nl80211_data *drv = bss->drv;
11198 struct ifreq ifr;
11199 android_wifi_priv_cmd priv_cmd;
11200 int ret = 0, i = 0, bp;
11201 char buf[WEXT_PNO_MAX_COMMAND_SIZE];
11202
11203 bp = WEXT_PNOSETUP_HEADER_SIZE;
11204 os_memcpy(buf, WEXT_PNOSETUP_HEADER, bp);
11205 buf[bp++] = WEXT_PNO_TLV_PREFIX;
11206 buf[bp++] = WEXT_PNO_TLV_VERSION;
11207 buf[bp++] = WEXT_PNO_TLV_SUBVERSION;
11208 buf[bp++] = WEXT_PNO_TLV_RESERVED;
11209
11210 while (i < WEXT_PNO_AMOUNT && (size_t) i < params->num_ssids) {
11211 /* Check that there is enough space needed for 1 more SSID, the
11212 * other sections and null termination */
11213 if ((bp + WEXT_PNO_SSID_HEADER_SIZE + MAX_SSID_LEN +
11214 WEXT_PNO_NONSSID_SECTIONS_SIZE + 1) >= (int) sizeof(buf))
11215 break;
11216 wpa_hexdump_ascii(MSG_DEBUG, "For PNO Scan",
11217 params->ssids[i].ssid,
11218 params->ssids[i].ssid_len);
11219 buf[bp++] = WEXT_PNO_SSID_SECTION;
11220 buf[bp++] = params->ssids[i].ssid_len;
11221 os_memcpy(&buf[bp], params->ssids[i].ssid,
11222 params->ssids[i].ssid_len);
11223 bp += params->ssids[i].ssid_len;
11224 i++;
11225 }
11226
11227 buf[bp++] = WEXT_PNO_SCAN_INTERVAL_SECTION;
11228 os_snprintf(&buf[bp], WEXT_PNO_SCAN_INTERVAL_LENGTH + 1, "%x",
11229 WEXT_PNO_SCAN_INTERVAL);
11230 bp += WEXT_PNO_SCAN_INTERVAL_LENGTH;
11231
11232 buf[bp++] = WEXT_PNO_REPEAT_SECTION;
11233 os_snprintf(&buf[bp], WEXT_PNO_REPEAT_LENGTH + 1, "%x",
11234 WEXT_PNO_REPEAT);
11235 bp += WEXT_PNO_REPEAT_LENGTH;
11236
11237 buf[bp++] = WEXT_PNO_MAX_REPEAT_SECTION;
11238 os_snprintf(&buf[bp], WEXT_PNO_MAX_REPEAT_LENGTH + 1, "%x",
11239 WEXT_PNO_MAX_REPEAT);
11240 bp += WEXT_PNO_MAX_REPEAT_LENGTH + 1;
11241
11242 memset(&ifr, 0, sizeof(ifr));
11243 memset(&priv_cmd, 0, sizeof(priv_cmd));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070011244 os_strlcpy(ifr.ifr_name, bss->ifname, IFNAMSIZ);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011245
11246 priv_cmd.buf = buf;
11247 priv_cmd.used_len = bp;
11248 priv_cmd.total_len = bp;
11249 ifr.ifr_data = &priv_cmd;
11250
11251 ret = ioctl(drv->global->ioctl_sock, SIOCDEVPRIVATE + 1, &ifr);
11252
11253 if (ret < 0) {
11254 wpa_printf(MSG_ERROR, "ioctl[SIOCSIWPRIV] (pnosetup): %d",
11255 ret);
11256 wpa_driver_send_hang_msg(drv);
11257 return ret;
11258 }
11259
11260 drv_errors = 0;
11261
11262 return android_priv_cmd(bss, "PNOFORCE 1");
11263}
11264
11265
11266static int android_pno_stop(struct i802_bss *bss)
11267{
11268 return android_priv_cmd(bss, "PNOFORCE 0");
11269}
11270
11271#endif /* ANDROID */
11272
11273
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011274static int driver_nl80211_set_key(const char *ifname, void *priv,
11275 enum wpa_alg alg, const u8 *addr,
11276 int key_idx, int set_tx,
11277 const u8 *seq, size_t seq_len,
11278 const u8 *key, size_t key_len)
11279{
11280 struct i802_bss *bss = priv;
11281 return wpa_driver_nl80211_set_key(ifname, bss, alg, addr, key_idx,
11282 set_tx, seq, seq_len, key, key_len);
11283}
11284
11285
11286static int driver_nl80211_scan2(void *priv,
11287 struct wpa_driver_scan_params *params)
11288{
11289 struct i802_bss *bss = priv;
11290 return wpa_driver_nl80211_scan(bss, params);
11291}
11292
11293
11294static int driver_nl80211_deauthenticate(void *priv, const u8 *addr,
11295 int reason_code)
11296{
11297 struct i802_bss *bss = priv;
11298 return wpa_driver_nl80211_deauthenticate(bss, addr, reason_code);
11299}
11300
11301
11302static int driver_nl80211_authenticate(void *priv,
11303 struct wpa_driver_auth_params *params)
11304{
11305 struct i802_bss *bss = priv;
11306 return wpa_driver_nl80211_authenticate(bss, params);
11307}
11308
11309
11310static void driver_nl80211_deinit(void *priv)
11311{
11312 struct i802_bss *bss = priv;
11313 wpa_driver_nl80211_deinit(bss);
11314}
11315
11316
11317static int driver_nl80211_if_remove(void *priv, enum wpa_driver_if_type type,
11318 const char *ifname)
11319{
11320 struct i802_bss *bss = priv;
11321 return wpa_driver_nl80211_if_remove(bss, type, ifname);
11322}
11323
11324
11325static int driver_nl80211_send_mlme(void *priv, const u8 *data,
11326 size_t data_len, int noack)
11327{
11328 struct i802_bss *bss = priv;
11329 return wpa_driver_nl80211_send_mlme(bss, data, data_len, noack,
11330 0, 0, 0, 0);
11331}
11332
11333
11334static int driver_nl80211_sta_remove(void *priv, const u8 *addr)
11335{
11336 struct i802_bss *bss = priv;
11337 return wpa_driver_nl80211_sta_remove(bss, addr);
11338}
11339
11340
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011341static int driver_nl80211_set_sta_vlan(void *priv, const u8 *addr,
11342 const char *ifname, int vlan_id)
11343{
11344 struct i802_bss *bss = priv;
11345 return i802_set_sta_vlan(bss, addr, ifname, vlan_id);
11346}
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011347
11348
11349static int driver_nl80211_read_sta_data(void *priv,
11350 struct hostap_sta_driver_data *data,
11351 const u8 *addr)
11352{
11353 struct i802_bss *bss = priv;
11354 return i802_read_sta_data(bss, data, addr);
11355}
11356
11357
11358static int driver_nl80211_send_action(void *priv, unsigned int freq,
11359 unsigned int wait_time,
11360 const u8 *dst, const u8 *src,
11361 const u8 *bssid,
11362 const u8 *data, size_t data_len,
11363 int no_cck)
11364{
11365 struct i802_bss *bss = priv;
11366 return wpa_driver_nl80211_send_action(bss, freq, wait_time, dst, src,
11367 bssid, data, data_len, no_cck);
11368}
11369
11370
11371static int driver_nl80211_probe_req_report(void *priv, int report)
11372{
11373 struct i802_bss *bss = priv;
11374 return wpa_driver_nl80211_probe_req_report(bss, report);
11375}
11376
11377
Dmitry Shmidt700a1372013-03-15 14:14:44 -070011378static int wpa_driver_nl80211_update_ft_ies(void *priv, const u8 *md,
11379 const u8 *ies, size_t ies_len)
11380{
11381 int ret;
11382 struct nl_msg *msg;
11383 struct i802_bss *bss = priv;
11384 struct wpa_driver_nl80211_data *drv = bss->drv;
11385 u16 mdid = WPA_GET_LE16(md);
11386
11387 msg = nlmsg_alloc();
11388 if (!msg)
11389 return -ENOMEM;
11390
11391 wpa_printf(MSG_DEBUG, "nl80211: Updating FT IEs");
11392 nl80211_cmd(drv, msg, 0, NL80211_CMD_UPDATE_FT_IES);
11393 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
11394 NLA_PUT(msg, NL80211_ATTR_IE, ies_len, ies);
11395 NLA_PUT_U16(msg, NL80211_ATTR_MDID, mdid);
11396
11397 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
11398 if (ret) {
11399 wpa_printf(MSG_DEBUG, "nl80211: update_ft_ies failed "
11400 "err=%d (%s)", ret, strerror(-ret));
11401 }
11402
11403 return ret;
11404
11405nla_put_failure:
11406 nlmsg_free(msg);
11407 return -ENOBUFS;
11408}
11409
11410
Dmitry Shmidt34af3062013-07-11 10:46:32 -070011411const u8 * wpa_driver_nl80211_get_macaddr(void *priv)
11412{
11413 struct i802_bss *bss = priv;
11414 struct wpa_driver_nl80211_data *drv = bss->drv;
11415
11416 if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE)
11417 return NULL;
11418
11419 return bss->addr;
11420}
11421
11422
Dmitry Shmidt56052862013-10-04 10:23:25 -070011423static const char * scan_state_str(enum scan_states scan_state)
11424{
11425 switch (scan_state) {
11426 case NO_SCAN:
11427 return "NO_SCAN";
11428 case SCAN_REQUESTED:
11429 return "SCAN_REQUESTED";
11430 case SCAN_STARTED:
11431 return "SCAN_STARTED";
11432 case SCAN_COMPLETED:
11433 return "SCAN_COMPLETED";
11434 case SCAN_ABORTED:
11435 return "SCAN_ABORTED";
11436 case SCHED_SCAN_STARTED:
11437 return "SCHED_SCAN_STARTED";
11438 case SCHED_SCAN_STOPPED:
11439 return "SCHED_SCAN_STOPPED";
11440 case SCHED_SCAN_RESULTS:
11441 return "SCHED_SCAN_RESULTS";
11442 }
11443
11444 return "??";
11445}
11446
11447
11448static int wpa_driver_nl80211_status(void *priv, char *buf, size_t buflen)
11449{
11450 struct i802_bss *bss = priv;
11451 struct wpa_driver_nl80211_data *drv = bss->drv;
11452 int res;
11453 char *pos, *end;
11454
11455 pos = buf;
11456 end = buf + buflen;
11457
11458 res = os_snprintf(pos, end - pos,
11459 "ifindex=%d\n"
11460 "ifname=%s\n"
11461 "brname=%s\n"
11462 "addr=" MACSTR "\n"
11463 "freq=%d\n"
11464 "%s%s%s%s%s",
11465 bss->ifindex,
11466 bss->ifname,
11467 bss->brname,
11468 MAC2STR(bss->addr),
11469 bss->freq,
11470 bss->beacon_set ? "beacon_set=1\n" : "",
11471 bss->added_if_into_bridge ?
11472 "added_if_into_bridge=1\n" : "",
11473 bss->added_bridge ? "added_bridge=1\n" : "",
11474 bss->in_deinit ? "in_deinit=1\n" : "",
11475 bss->if_dynamic ? "if_dynamic=1\n" : "");
11476 if (res < 0 || res >= end - pos)
11477 return pos - buf;
11478 pos += res;
11479
11480 if (bss->wdev_id_set) {
11481 res = os_snprintf(pos, end - pos, "wdev_id=%llu\n",
11482 (unsigned long long) bss->wdev_id);
11483 if (res < 0 || res >= end - pos)
11484 return pos - buf;
11485 pos += res;
11486 }
11487
11488 res = os_snprintf(pos, end - pos,
11489 "phyname=%s\n"
11490 "drv_ifindex=%d\n"
11491 "operstate=%d\n"
11492 "scan_state=%s\n"
11493 "auth_bssid=" MACSTR "\n"
11494 "auth_attempt_bssid=" MACSTR "\n"
11495 "bssid=" MACSTR "\n"
11496 "prev_bssid=" MACSTR "\n"
11497 "associated=%d\n"
11498 "assoc_freq=%u\n"
11499 "monitor_sock=%d\n"
11500 "monitor_ifidx=%d\n"
11501 "monitor_refcount=%d\n"
11502 "last_mgmt_freq=%u\n"
11503 "eapol_tx_sock=%d\n"
11504 "%s%s%s%s%s%s%s%s%s%s%s%s%s",
11505 drv->phyname,
11506 drv->ifindex,
11507 drv->operstate,
11508 scan_state_str(drv->scan_state),
11509 MAC2STR(drv->auth_bssid),
11510 MAC2STR(drv->auth_attempt_bssid),
11511 MAC2STR(drv->bssid),
11512 MAC2STR(drv->prev_bssid),
11513 drv->associated,
11514 drv->assoc_freq,
11515 drv->monitor_sock,
11516 drv->monitor_ifidx,
11517 drv->monitor_refcount,
11518 drv->last_mgmt_freq,
11519 drv->eapol_tx_sock,
11520 drv->ignore_if_down_event ?
11521 "ignore_if_down_event=1\n" : "",
11522 drv->scan_complete_events ?
11523 "scan_complete_events=1\n" : "",
11524 drv->disabled_11b_rates ?
11525 "disabled_11b_rates=1\n" : "",
11526 drv->pending_remain_on_chan ?
11527 "pending_remain_on_chan=1\n" : "",
11528 drv->in_interface_list ? "in_interface_list=1\n" : "",
11529 drv->device_ap_sme ? "device_ap_sme=1\n" : "",
11530 drv->poll_command_supported ?
11531 "poll_command_supported=1\n" : "",
11532 drv->data_tx_status ? "data_tx_status=1\n" : "",
11533 drv->scan_for_auth ? "scan_for_auth=1\n" : "",
11534 drv->retry_auth ? "retry_auth=1\n" : "",
11535 drv->use_monitor ? "use_monitor=1\n" : "",
11536 drv->ignore_next_local_disconnect ?
11537 "ignore_next_local_disconnect=1\n" : "",
11538 drv->allow_p2p_device ? "allow_p2p_device=1\n" : "");
11539 if (res < 0 || res >= end - pos)
11540 return pos - buf;
11541 pos += res;
11542
11543 if (drv->has_capability) {
11544 res = os_snprintf(pos, end - pos,
11545 "capa.key_mgmt=0x%x\n"
11546 "capa.enc=0x%x\n"
11547 "capa.auth=0x%x\n"
11548 "capa.flags=0x%x\n"
11549 "capa.max_scan_ssids=%d\n"
11550 "capa.max_sched_scan_ssids=%d\n"
11551 "capa.sched_scan_supported=%d\n"
11552 "capa.max_match_sets=%d\n"
11553 "capa.max_remain_on_chan=%u\n"
11554 "capa.max_stations=%u\n"
11555 "capa.probe_resp_offloads=0x%x\n"
11556 "capa.max_acl_mac_addrs=%u\n"
11557 "capa.num_multichan_concurrent=%u\n",
11558 drv->capa.key_mgmt,
11559 drv->capa.enc,
11560 drv->capa.auth,
11561 drv->capa.flags,
11562 drv->capa.max_scan_ssids,
11563 drv->capa.max_sched_scan_ssids,
11564 drv->capa.sched_scan_supported,
11565 drv->capa.max_match_sets,
11566 drv->capa.max_remain_on_chan,
11567 drv->capa.max_stations,
11568 drv->capa.probe_resp_offloads,
11569 drv->capa.max_acl_mac_addrs,
11570 drv->capa.num_multichan_concurrent);
11571 if (res < 0 || res >= end - pos)
11572 return pos - buf;
11573 pos += res;
11574 }
11575
11576 return pos - buf;
11577}
11578
11579
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080011580static int set_beacon_data(struct nl_msg *msg, struct beacon_data *settings)
11581{
11582 if (settings->head)
11583 NLA_PUT(msg, NL80211_ATTR_BEACON_HEAD,
11584 settings->head_len, settings->head);
11585
11586 if (settings->tail)
11587 NLA_PUT(msg, NL80211_ATTR_BEACON_TAIL,
11588 settings->tail_len, settings->tail);
11589
11590 if (settings->beacon_ies)
11591 NLA_PUT(msg, NL80211_ATTR_IE,
11592 settings->beacon_ies_len, settings->beacon_ies);
11593
11594 if (settings->proberesp_ies)
11595 NLA_PUT(msg, NL80211_ATTR_IE_PROBE_RESP,
11596 settings->proberesp_ies_len, settings->proberesp_ies);
11597
11598 if (settings->assocresp_ies)
11599 NLA_PUT(msg,
11600 NL80211_ATTR_IE_ASSOC_RESP,
11601 settings->assocresp_ies_len, settings->assocresp_ies);
11602
11603 if (settings->probe_resp)
11604 NLA_PUT(msg, NL80211_ATTR_PROBE_RESP,
11605 settings->probe_resp_len, settings->probe_resp);
11606
11607 return 0;
11608
11609nla_put_failure:
11610 return -ENOBUFS;
11611}
11612
11613
11614static int nl80211_switch_channel(void *priv, struct csa_settings *settings)
11615{
11616 struct nl_msg *msg;
11617 struct i802_bss *bss = priv;
11618 struct wpa_driver_nl80211_data *drv = bss->drv;
11619 struct nlattr *beacon_csa;
11620 int ret = -ENOBUFS;
11621
Dmitry Shmidt04f534e2013-12-09 15:50:16 -080011622 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 -080011623 settings->cs_count, settings->block_tx,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -080011624 settings->freq_params.freq, settings->freq_params.bandwidth,
11625 settings->freq_params.center_freq1,
11626 settings->freq_params.center_freq2);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080011627
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080011628 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_AP_CSA)) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080011629 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support channel switch command");
11630 return -EOPNOTSUPP;
11631 }
11632
11633 if ((drv->nlmode != NL80211_IFTYPE_AP) &&
11634 (drv->nlmode != NL80211_IFTYPE_P2P_GO))
11635 return -EOPNOTSUPP;
11636
11637 /* check settings validity */
11638 if (!settings->beacon_csa.tail ||
11639 ((settings->beacon_csa.tail_len <=
11640 settings->counter_offset_beacon) ||
11641 (settings->beacon_csa.tail[settings->counter_offset_beacon] !=
11642 settings->cs_count)))
11643 return -EINVAL;
11644
11645 if (settings->beacon_csa.probe_resp &&
11646 ((settings->beacon_csa.probe_resp_len <=
11647 settings->counter_offset_presp) ||
11648 (settings->beacon_csa.probe_resp[settings->counter_offset_presp] !=
11649 settings->cs_count)))
11650 return -EINVAL;
11651
11652 msg = nlmsg_alloc();
11653 if (!msg)
11654 return -ENOMEM;
11655
11656 nl80211_cmd(drv, msg, 0, NL80211_CMD_CHANNEL_SWITCH);
11657 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
11658 NLA_PUT_U32(msg, NL80211_ATTR_CH_SWITCH_COUNT, settings->cs_count);
11659 ret = nl80211_put_freq_params(msg, &settings->freq_params);
11660 if (ret)
11661 goto error;
11662
11663 if (settings->block_tx)
11664 NLA_PUT_FLAG(msg, NL80211_ATTR_CH_SWITCH_BLOCK_TX);
11665
11666 /* beacon_after params */
11667 ret = set_beacon_data(msg, &settings->beacon_after);
11668 if (ret)
11669 goto error;
11670
11671 /* beacon_csa params */
11672 beacon_csa = nla_nest_start(msg, NL80211_ATTR_CSA_IES);
11673 if (!beacon_csa)
11674 goto nla_put_failure;
11675
11676 ret = set_beacon_data(msg, &settings->beacon_csa);
11677 if (ret)
11678 goto error;
11679
11680 NLA_PUT_U16(msg, NL80211_ATTR_CSA_C_OFF_BEACON,
11681 settings->counter_offset_beacon);
11682
11683 if (settings->beacon_csa.probe_resp)
11684 NLA_PUT_U16(msg, NL80211_ATTR_CSA_C_OFF_PRESP,
11685 settings->counter_offset_presp);
11686
11687 nla_nest_end(msg, beacon_csa);
11688 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
11689 if (ret) {
11690 wpa_printf(MSG_DEBUG, "nl80211: switch_channel failed err=%d (%s)",
11691 ret, strerror(-ret));
11692 }
11693 return ret;
11694
11695nla_put_failure:
11696 ret = -ENOBUFS;
11697error:
11698 nlmsg_free(msg);
11699 wpa_printf(MSG_DEBUG, "nl80211: Could not build channel switch request");
11700 return ret;
11701}
11702
11703
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080011704static int nl80211_set_qos_map(void *priv, const u8 *qos_map_set,
11705 u8 qos_map_set_len)
11706{
11707 struct i802_bss *bss = priv;
11708 struct wpa_driver_nl80211_data *drv = bss->drv;
11709 struct nl_msg *msg;
11710 int ret;
11711
11712 msg = nlmsg_alloc();
11713 if (!msg)
11714 return -ENOMEM;
11715
11716 wpa_hexdump(MSG_DEBUG, "nl80211: Setting QoS Map",
11717 qos_map_set, qos_map_set_len);
11718
11719 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_QOS_MAP);
11720 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
11721 NLA_PUT(msg, NL80211_ATTR_QOS_MAP, qos_map_set_len, qos_map_set);
11722
11723 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
11724 if (ret)
11725 wpa_printf(MSG_DEBUG, "nl80211: Setting QoS Map failed");
11726
11727 return ret;
11728
11729nla_put_failure:
11730 nlmsg_free(msg);
11731 return -ENOBUFS;
11732}
11733
11734
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011735const struct wpa_driver_ops wpa_driver_nl80211_ops = {
11736 .name = "nl80211",
11737 .desc = "Linux nl80211/cfg80211",
11738 .get_bssid = wpa_driver_nl80211_get_bssid,
11739 .get_ssid = wpa_driver_nl80211_get_ssid,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011740 .set_key = driver_nl80211_set_key,
11741 .scan2 = driver_nl80211_scan2,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011742 .sched_scan = wpa_driver_nl80211_sched_scan,
11743 .stop_sched_scan = wpa_driver_nl80211_stop_sched_scan,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011744 .get_scan_results2 = wpa_driver_nl80211_get_scan_results,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011745 .deauthenticate = driver_nl80211_deauthenticate,
11746 .authenticate = driver_nl80211_authenticate,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011747 .associate = wpa_driver_nl80211_associate,
11748 .global_init = nl80211_global_init,
11749 .global_deinit = nl80211_global_deinit,
11750 .init2 = wpa_driver_nl80211_init,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011751 .deinit = driver_nl80211_deinit,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011752 .get_capa = wpa_driver_nl80211_get_capa,
11753 .set_operstate = wpa_driver_nl80211_set_operstate,
11754 .set_supp_port = wpa_driver_nl80211_set_supp_port,
11755 .set_country = wpa_driver_nl80211_set_country,
Dmitry Shmidtcce06662013-11-04 18:44:24 -080011756 .get_country = wpa_driver_nl80211_get_country,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011757 .set_ap = wpa_driver_nl80211_set_ap,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -070011758 .set_acl = wpa_driver_nl80211_set_acl,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011759 .if_add = wpa_driver_nl80211_if_add,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011760 .if_remove = driver_nl80211_if_remove,
11761 .send_mlme = driver_nl80211_send_mlme,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011762 .get_hw_feature_data = wpa_driver_nl80211_get_hw_feature_data,
11763 .sta_add = wpa_driver_nl80211_sta_add,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011764 .sta_remove = driver_nl80211_sta_remove,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011765 .hapd_send_eapol = wpa_driver_nl80211_hapd_send_eapol,
11766 .sta_set_flags = wpa_driver_nl80211_sta_set_flags,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011767 .hapd_init = i802_init,
11768 .hapd_deinit = i802_deinit,
Jouni Malinen75ecf522011-06-27 15:19:46 -070011769 .set_wds_sta = i802_set_wds_sta,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011770 .get_seqnum = i802_get_seqnum,
11771 .flush = i802_flush,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011772 .get_inact_sec = i802_get_inact_sec,
11773 .sta_clear_stats = i802_sta_clear_stats,
11774 .set_rts = i802_set_rts,
11775 .set_frag = i802_set_frag,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011776 .set_tx_queue_params = i802_set_tx_queue_params,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011777 .set_sta_vlan = driver_nl80211_set_sta_vlan,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011778 .sta_deauth = i802_sta_deauth,
11779 .sta_disassoc = i802_sta_disassoc,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011780 .read_sta_data = driver_nl80211_read_sta_data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011781 .set_freq = i802_set_freq,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011782 .send_action = driver_nl80211_send_action,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011783 .send_action_cancel_wait = wpa_driver_nl80211_send_action_cancel_wait,
11784 .remain_on_channel = wpa_driver_nl80211_remain_on_channel,
11785 .cancel_remain_on_channel =
11786 wpa_driver_nl80211_cancel_remain_on_channel,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011787 .probe_req_report = driver_nl80211_probe_req_report,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011788 .deinit_ap = wpa_driver_nl80211_deinit_ap,
Dmitry Shmidt04949592012-07-19 12:16:46 -070011789 .deinit_p2p_cli = wpa_driver_nl80211_deinit_p2p_cli,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011790 .resume = wpa_driver_nl80211_resume,
11791 .send_ft_action = nl80211_send_ft_action,
11792 .signal_monitor = nl80211_signal_monitor,
11793 .signal_poll = nl80211_signal_poll,
11794 .send_frame = nl80211_send_frame,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011795 .shared_freq = wpa_driver_nl80211_shared_freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011796 .set_param = nl80211_set_param,
11797 .get_radio_name = nl80211_get_radio_name,
Jouni Malinen75ecf522011-06-27 15:19:46 -070011798 .add_pmkid = nl80211_add_pmkid,
11799 .remove_pmkid = nl80211_remove_pmkid,
11800 .flush_pmkid = nl80211_flush_pmkid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011801 .set_rekey_info = nl80211_set_rekey_info,
11802 .poll_client = nl80211_poll_client,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011803 .set_p2p_powersave = nl80211_set_p2p_powersave,
Dmitry Shmidtea69e842013-05-13 14:52:28 -070011804 .start_dfs_cac = nl80211_start_radar_detection,
11805 .stop_ap = wpa_driver_nl80211_stop_ap,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011806#ifdef CONFIG_TDLS
11807 .send_tdls_mgmt = nl80211_send_tdls_mgmt,
11808 .tdls_oper = nl80211_tdls_oper,
11809#endif /* CONFIG_TDLS */
Dmitry Shmidt700a1372013-03-15 14:14:44 -070011810 .update_ft_ies = wpa_driver_nl80211_update_ft_ies,
Dmitry Shmidt34af3062013-07-11 10:46:32 -070011811 .get_mac_addr = wpa_driver_nl80211_get_macaddr,
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070011812 .get_survey = wpa_driver_nl80211_get_survey,
Dmitry Shmidt56052862013-10-04 10:23:25 -070011813 .status = wpa_driver_nl80211_status,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080011814 .switch_channel = nl80211_switch_channel,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011815#ifdef ANDROID_P2P
Dmitry Shmidt6e933c12011-09-27 12:29:26 -070011816 .set_noa = wpa_driver_set_p2p_noa,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011817 .get_noa = wpa_driver_get_p2p_noa,
Dmitry Shmidt6e933c12011-09-27 12:29:26 -070011818 .set_ap_wps_ie = wpa_driver_set_ap_wps_p2p_ie,
Dmitry Shmidt292b0c32013-11-22 12:54:42 -080011819#endif /* ANDROID_P2P */
Dmitry Shmidt738a26e2011-07-07 14:22:14 -070011820#ifdef ANDROID
11821 .driver_cmd = wpa_driver_nl80211_driver_cmd,
Dmitry Shmidt292b0c32013-11-22 12:54:42 -080011822#endif /* ANDROID */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080011823 .set_qos_map = nl80211_set_qos_map,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011824};