blob: 61250154e9f75b1d2da46753406acef154ad2f8d [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * Driver interaction with Linux nl80211/cfg80211
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003 * Copyright (c) 2002-2012, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 * Copyright (c) 2003-2004, Instant802 Networks, Inc.
5 * Copyright (c) 2005-2006, Devicescape Software, Inc.
6 * Copyright (c) 2007, Johannes Berg <johannes@sipsolutions.net>
7 * Copyright (c) 2009-2010, Atheros Communications
8 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08009 * This software may be distributed under the terms of the BSD license.
10 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011 */
12
13#include "includes.h"
14#include <sys/ioctl.h>
15#include <sys/types.h>
16#include <sys/stat.h>
17#include <fcntl.h>
18#include <net/if.h>
19#include <netlink/genl/genl.h>
20#include <netlink/genl/family.h>
21#include <netlink/genl/ctrl.h>
22#include <linux/rtnetlink.h>
23#include <netpacket/packet.h>
24#include <linux/filter.h>
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080025#include <linux/errqueue.h>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070026#include "nl80211_copy.h"
27
28#include "common.h"
29#include "eloop.h"
30#include "utils/list.h"
31#include "common/ieee802_11_defs.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080032#include "common/ieee802_11_common.h"
33#include "l2_packet/l2_packet.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070034#include "netlink.h"
35#include "linux_ioctl.h"
36#include "radiotap.h"
37#include "radiotap_iter.h"
38#include "rfkill.h"
39#include "driver.h"
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080040
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080041#ifndef SO_WIFI_STATUS
42# if defined(__sparc__)
43# define SO_WIFI_STATUS 0x0025
44# elif defined(__parisc__)
45# define SO_WIFI_STATUS 0x4022
46# else
47# define SO_WIFI_STATUS 41
48# endif
49
50# define SCM_WIFI_STATUS SO_WIFI_STATUS
51#endif
52
53#ifndef SO_EE_ORIGIN_TXSTATUS
54#define SO_EE_ORIGIN_TXSTATUS 4
55#endif
56
57#ifndef PACKET_TX_TIMESTAMP
58#define PACKET_TX_TIMESTAMP 16
59#endif
60
61#ifdef ANDROID
62#include "android_drv.h"
63#endif /* ANDROID */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070064#ifdef CONFIG_LIBNL20
65/* libnl 2.0 compatibility code */
66#define nl_handle nl_sock
67#define nl80211_handle_alloc nl_socket_alloc_cb
68#define nl80211_handle_destroy nl_socket_free
69#else
70/*
71 * libnl 1.1 has a bug, it tries to allocate socket numbers densely
72 * but when you free a socket again it will mess up its bitmap and
73 * and use the wrong number the next time it needs a socket ID.
74 * Therefore, we wrap the handle alloc/destroy and add our own pid
75 * accounting.
76 */
77static uint32_t port_bitmap[32] = { 0 };
78
79static struct nl_handle *nl80211_handle_alloc(void *cb)
80{
81 struct nl_handle *handle;
82 uint32_t pid = getpid() & 0x3FFFFF;
83 int i;
84
85 handle = nl_handle_alloc_cb(cb);
86
87 for (i = 0; i < 1024; i++) {
88 if (port_bitmap[i / 32] & (1 << (i % 32)))
89 continue;
90 port_bitmap[i / 32] |= 1 << (i % 32);
91 pid += i << 22;
92 break;
93 }
94
95 nl_socket_set_local_port(handle, pid);
96
97 return handle;
98}
99
100static void nl80211_handle_destroy(struct nl_handle *handle)
101{
102 uint32_t port = nl_socket_get_local_port(handle);
103
104 port >>= 22;
105 port_bitmap[port / 32] &= ~(1 << (port % 32));
106
107 nl_handle_destroy(handle);
108}
109#endif /* CONFIG_LIBNL20 */
110
111
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800112static struct nl_handle * nl_create_handle(struct nl_cb *cb, const char *dbg)
113{
114 struct nl_handle *handle;
115
116 handle = nl80211_handle_alloc(cb);
117 if (handle == NULL) {
118 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
119 "callbacks (%s)", dbg);
120 return NULL;
121 }
122
123 if (genl_connect(handle)) {
124 wpa_printf(MSG_ERROR, "nl80211: Failed to connect to generic "
125 "netlink (%s)", dbg);
126 nl80211_handle_destroy(handle);
127 return NULL;
128 }
129
130 return handle;
131}
132
133
134static void nl_destroy_handles(struct nl_handle **handle)
135{
136 if (*handle == NULL)
137 return;
138 nl80211_handle_destroy(*handle);
139 *handle = NULL;
140}
141
142
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700143#ifndef IFF_LOWER_UP
144#define IFF_LOWER_UP 0x10000 /* driver signals L1 up */
145#endif
146#ifndef IFF_DORMANT
147#define IFF_DORMANT 0x20000 /* driver signals dormant */
148#endif
149
150#ifndef IF_OPER_DORMANT
151#define IF_OPER_DORMANT 5
152#endif
153#ifndef IF_OPER_UP
154#define IF_OPER_UP 6
155#endif
156
157struct nl80211_global {
158 struct dl_list interfaces;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800159 int if_add_ifindex;
160 struct netlink_data *netlink;
161 struct nl_cb *nl_cb;
162 struct nl_handle *nl;
163 int nl80211_id;
164 int ioctl_sock; /* socket for ioctl() use */
165
166 struct nl_handle *nl_event;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700167};
168
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800169struct nl80211_wiphy_data {
170 struct dl_list list;
171 struct dl_list bsss;
172 struct dl_list drvs;
173
174 struct nl_handle *nl_beacons;
175 struct nl_cb *nl_cb;
176
177 int wiphy_idx;
178};
179
180static void nl80211_global_deinit(void *priv);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800181
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700182struct i802_bss {
183 struct wpa_driver_nl80211_data *drv;
184 struct i802_bss *next;
185 int ifindex;
186 char ifname[IFNAMSIZ + 1];
187 char brname[IFNAMSIZ];
188 unsigned int beacon_set:1;
189 unsigned int added_if_into_bridge:1;
190 unsigned int added_bridge:1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700191 unsigned int in_deinit:1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800192
193 u8 addr[ETH_ALEN];
194
195 int freq;
196
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800197 void *ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800198 struct nl_handle *nl_preq, *nl_mgmt;
199 struct nl_cb *nl_cb;
200
201 struct nl80211_wiphy_data *wiphy_data;
202 struct dl_list wiphy_list;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700203};
204
205struct wpa_driver_nl80211_data {
206 struct nl80211_global *global;
207 struct dl_list list;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800208 struct dl_list wiphy_list;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700209 char phyname[32];
210 void *ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700211 int ifindex;
212 int if_removed;
213 int if_disabled;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800214 int ignore_if_down_event;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700215 struct rfkill_data *rfkill;
216 struct wpa_driver_capa capa;
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700217 u8 *extended_capa, *extended_capa_mask;
218 unsigned int extended_capa_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700219 int has_capability;
220
221 int operstate;
222
223 int scan_complete_events;
224
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700225 struct nl_cb *nl_cb;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700226
227 u8 auth_bssid[ETH_ALEN];
228 u8 bssid[ETH_ALEN];
229 int associated;
230 u8 ssid[32];
231 size_t ssid_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800232 enum nl80211_iftype nlmode;
233 enum nl80211_iftype ap_scan_as_station;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700234 unsigned int assoc_freq;
235
236 int monitor_sock;
237 int monitor_ifidx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800238 int monitor_refcount;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700239
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800240 unsigned int disabled_11b_rates:1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700241 unsigned int pending_remain_on_chan:1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800242 unsigned int in_interface_list:1;
243 unsigned int device_ap_sme:1;
244 unsigned int poll_command_supported:1;
245 unsigned int data_tx_status:1;
246 unsigned int scan_for_auth:1;
247 unsigned int retry_auth:1;
248 unsigned int use_monitor:1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800249 unsigned int ignore_next_local_disconnect:1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700250
251 u64 remain_on_chan_cookie;
252 u64 send_action_cookie;
253
254 unsigned int last_mgmt_freq;
255
256 struct wpa_driver_scan_filter *filter_ssids;
257 size_t num_filter_ssids;
258
259 struct i802_bss first_bss;
260
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800261 int eapol_tx_sock;
262
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700263#ifdef HOSTAPD
264 int eapol_sock; /* socket for EAPOL frames */
265
266 int default_if_indices[16];
267 int *if_indices;
268 int num_if_indices;
269
270 int last_freq;
271 int last_freq_ht;
272#endif /* HOSTAPD */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800273
274 /* From failed authentication command */
275 int auth_freq;
276 u8 auth_bssid_[ETH_ALEN];
277 u8 auth_ssid[32];
278 size_t auth_ssid_len;
279 int auth_alg;
280 u8 *auth_ie;
281 size_t auth_ie_len;
282 u8 auth_wep_key[4][16];
283 size_t auth_wep_key_len[4];
284 int auth_wep_tx_keyidx;
285 int auth_local_state_change;
286 int auth_p2p;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700287};
288
289
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800290static void wpa_driver_nl80211_deinit(struct i802_bss *bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700291static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx,
292 void *timeout_ctx);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800293static int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
294 enum nl80211_iftype nlmode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700295static int
296wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv);
297static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
298 const u8 *addr, int cmd, u16 reason_code,
299 int local_state_change);
300static void nl80211_remove_monitor_interface(
301 struct wpa_driver_nl80211_data *drv);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800302static int nl80211_send_frame_cmd(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700303 unsigned int freq, unsigned int wait,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800304 const u8 *buf, size_t buf_len, u64 *cookie,
305 int no_cck, int no_ack, int offchanok);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800306static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss,
307 int report);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800308#ifdef ANDROID
309static int android_pno_start(struct i802_bss *bss,
310 struct wpa_driver_scan_params *params);
311static int android_pno_stop(struct i802_bss *bss);
312#endif /* ANDROID */
313#ifdef ANDROID_P2P
Dmitry Shmidt6e933c12011-09-27 12:29:26 -0700314int wpa_driver_set_p2p_noa(void *priv, u8 count, int start, int duration);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800315int wpa_driver_get_p2p_noa(void *priv, u8 *buf, size_t len);
Dmitry Shmidt6e933c12011-09-27 12:29:26 -0700316int wpa_driver_set_p2p_ps(void *priv, int legacy_ps, int opp_ps, int ctwindow);
317int wpa_driver_set_ap_wps_p2p_ie(void *priv, const struct wpabuf *beacon,
318 const struct wpabuf *proberesp,
319 const struct wpabuf *assocresp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700320
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800321#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700322#ifdef HOSTAPD
323static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
324static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
325static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800326static int wpa_driver_nl80211_if_remove(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700327 enum wpa_driver_if_type type,
328 const char *ifname);
329#else /* HOSTAPD */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800330static inline void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
331{
332}
333
334static inline void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
335{
336}
337
338static inline int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700339{
340 return 0;
341}
342#endif /* HOSTAPD */
Dmitry Shmidt738a26e2011-07-07 14:22:14 -0700343#ifdef ANDROID
344extern int wpa_driver_nl80211_driver_cmd(void *priv, char *cmd, char *buf,
345 size_t buf_len);
346#endif
347
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800348static int wpa_driver_nl80211_set_freq(struct i802_bss *bss,
349 struct hostapd_freq_params *freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700350static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
351 int ifindex, int disabled);
352
353static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800354static int wpa_driver_nl80211_authenticate_retry(
355 struct wpa_driver_nl80211_data *drv);
356
357
358static int is_ap_interface(enum nl80211_iftype nlmode)
359{
360 return (nlmode == NL80211_IFTYPE_AP ||
361 nlmode == NL80211_IFTYPE_P2P_GO);
362}
363
364
365static int is_sta_interface(enum nl80211_iftype nlmode)
366{
367 return (nlmode == NL80211_IFTYPE_STATION ||
368 nlmode == NL80211_IFTYPE_P2P_CLIENT);
369}
370
371
372static int is_p2p_interface(enum nl80211_iftype nlmode)
373{
374 return (nlmode == NL80211_IFTYPE_P2P_CLIENT ||
375 nlmode == NL80211_IFTYPE_P2P_GO);
376}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700377
378
Jouni Malinen87fd2792011-05-16 18:35:42 +0300379struct nl80211_bss_info_arg {
380 struct wpa_driver_nl80211_data *drv;
381 struct wpa_scan_results *res;
382 unsigned int assoc_freq;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800383 u8 assoc_bssid[ETH_ALEN];
Jouni Malinen87fd2792011-05-16 18:35:42 +0300384};
385
386static int bss_info_handler(struct nl_msg *msg, void *arg);
387
388
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700389/* nl80211 code */
390static int ack_handler(struct nl_msg *msg, void *arg)
391{
392 int *err = arg;
393 *err = 0;
394 return NL_STOP;
395}
396
397static int finish_handler(struct nl_msg *msg, void *arg)
398{
399 int *ret = arg;
400 *ret = 0;
401 return NL_SKIP;
402}
403
404static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
405 void *arg)
406{
407 int *ret = arg;
408 *ret = err->error;
409 return NL_SKIP;
410}
411
412
413static int no_seq_check(struct nl_msg *msg, void *arg)
414{
415 return NL_OK;
416}
417
418
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800419static int send_and_recv(struct nl80211_global *global,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700420 struct nl_handle *nl_handle, struct nl_msg *msg,
421 int (*valid_handler)(struct nl_msg *, void *),
422 void *valid_data)
423{
424 struct nl_cb *cb;
425 int err = -ENOMEM;
426
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800427 cb = nl_cb_clone(global->nl_cb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700428 if (!cb)
429 goto out;
430
431 err = nl_send_auto_complete(nl_handle, msg);
432 if (err < 0)
433 goto out;
434
435 err = 1;
436
437 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
438 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
439 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
440
441 if (valid_handler)
442 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM,
443 valid_handler, valid_data);
444
445 while (err > 0)
446 nl_recvmsgs(nl_handle, cb);
447 out:
448 nl_cb_put(cb);
449 nlmsg_free(msg);
450 return err;
451}
452
453
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800454static int send_and_recv_msgs_global(struct nl80211_global *global,
455 struct nl_msg *msg,
456 int (*valid_handler)(struct nl_msg *, void *),
457 void *valid_data)
458{
459 return send_and_recv(global, global->nl, msg, valid_handler,
460 valid_data);
461}
462
Dmitry Shmidt04949592012-07-19 12:16:46 -0700463
Jouni Malinen80da0422012-08-09 15:29:25 -0700464#ifndef ANDROID
465static
466#endif
Dmitry Shmidt738a26e2011-07-07 14:22:14 -0700467int send_and_recv_msgs(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700468 struct nl_msg *msg,
469 int (*valid_handler)(struct nl_msg *, void *),
470 void *valid_data)
471{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800472 return send_and_recv(drv->global, drv->global->nl, msg,
473 valid_handler, valid_data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700474}
475
476
477struct family_data {
478 const char *group;
479 int id;
480};
481
482
483static int family_handler(struct nl_msg *msg, void *arg)
484{
485 struct family_data *res = arg;
486 struct nlattr *tb[CTRL_ATTR_MAX + 1];
487 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
488 struct nlattr *mcgrp;
489 int i;
490
491 nla_parse(tb, CTRL_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
492 genlmsg_attrlen(gnlh, 0), NULL);
493 if (!tb[CTRL_ATTR_MCAST_GROUPS])
494 return NL_SKIP;
495
496 nla_for_each_nested(mcgrp, tb[CTRL_ATTR_MCAST_GROUPS], i) {
497 struct nlattr *tb2[CTRL_ATTR_MCAST_GRP_MAX + 1];
498 nla_parse(tb2, CTRL_ATTR_MCAST_GRP_MAX, nla_data(mcgrp),
499 nla_len(mcgrp), NULL);
500 if (!tb2[CTRL_ATTR_MCAST_GRP_NAME] ||
501 !tb2[CTRL_ATTR_MCAST_GRP_ID] ||
502 os_strncmp(nla_data(tb2[CTRL_ATTR_MCAST_GRP_NAME]),
503 res->group,
504 nla_len(tb2[CTRL_ATTR_MCAST_GRP_NAME])) != 0)
505 continue;
506 res->id = nla_get_u32(tb2[CTRL_ATTR_MCAST_GRP_ID]);
507 break;
508 };
509
510 return NL_SKIP;
511}
512
513
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800514static int nl_get_multicast_id(struct nl80211_global *global,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700515 const char *family, const char *group)
516{
517 struct nl_msg *msg;
518 int ret = -1;
519 struct family_data res = { group, -ENOENT };
520
521 msg = nlmsg_alloc();
522 if (!msg)
523 return -ENOMEM;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800524 genlmsg_put(msg, 0, 0, genl_ctrl_resolve(global->nl, "nlctrl"),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700525 0, 0, CTRL_CMD_GETFAMILY, 0);
526 NLA_PUT_STRING(msg, CTRL_ATTR_FAMILY_NAME, family);
527
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800528 ret = send_and_recv_msgs_global(global, msg, family_handler, &res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700529 msg = NULL;
530 if (ret == 0)
531 ret = res.id;
532
533nla_put_failure:
534 nlmsg_free(msg);
535 return ret;
536}
537
538
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800539static void * nl80211_cmd(struct wpa_driver_nl80211_data *drv,
540 struct nl_msg *msg, int flags, uint8_t cmd)
541{
542 return genlmsg_put(msg, 0, 0, drv->global->nl80211_id,
543 0, flags, cmd, 0);
544}
545
546
547struct wiphy_idx_data {
548 int wiphy_idx;
549};
550
551
552static int netdev_info_handler(struct nl_msg *msg, void *arg)
553{
554 struct nlattr *tb[NL80211_ATTR_MAX + 1];
555 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
556 struct wiphy_idx_data *info = arg;
557
558 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
559 genlmsg_attrlen(gnlh, 0), NULL);
560
561 if (tb[NL80211_ATTR_WIPHY])
562 info->wiphy_idx = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
563
564 return NL_SKIP;
565}
566
567
568static int nl80211_get_wiphy_index(struct i802_bss *bss)
569{
570 struct nl_msg *msg;
571 struct wiphy_idx_data data = {
572 .wiphy_idx = -1,
573 };
574
575 msg = nlmsg_alloc();
576 if (!msg)
577 return -1;
578
579 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_GET_INTERFACE);
580
581 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
582
583 if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0)
584 return data.wiphy_idx;
585 msg = NULL;
586nla_put_failure:
587 nlmsg_free(msg);
588 return -1;
589}
590
591
592static int nl80211_register_beacons(struct wpa_driver_nl80211_data *drv,
593 struct nl80211_wiphy_data *w)
594{
595 struct nl_msg *msg;
596 int ret = -1;
597
598 msg = nlmsg_alloc();
599 if (!msg)
600 return -1;
601
602 nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_BEACONS);
603
604 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, w->wiphy_idx);
605
606 ret = send_and_recv(drv->global, w->nl_beacons, msg, NULL, NULL);
607 msg = NULL;
608 if (ret) {
609 wpa_printf(MSG_DEBUG, "nl80211: Register beacons command "
610 "failed: ret=%d (%s)",
611 ret, strerror(-ret));
612 goto nla_put_failure;
613 }
614 ret = 0;
615nla_put_failure:
616 nlmsg_free(msg);
617 return ret;
618}
619
620
621static void nl80211_recv_beacons(int sock, void *eloop_ctx, void *handle)
622{
623 struct nl80211_wiphy_data *w = eloop_ctx;
624
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800625 wpa_printf(MSG_EXCESSIVE, "nl80211: Beacon event message available");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800626
627 nl_recvmsgs(handle, w->nl_cb);
628}
629
630
631static int process_beacon_event(struct nl_msg *msg, void *arg)
632{
633 struct nl80211_wiphy_data *w = arg;
634 struct wpa_driver_nl80211_data *drv;
635 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
636 struct nlattr *tb[NL80211_ATTR_MAX + 1];
637 union wpa_event_data event;
638
639 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
640 genlmsg_attrlen(gnlh, 0), NULL);
641
642 if (gnlh->cmd != NL80211_CMD_FRAME) {
643 wpa_printf(MSG_DEBUG, "nl80211: Unexpected beacon event? (%d)",
644 gnlh->cmd);
645 return NL_SKIP;
646 }
647
648 if (!tb[NL80211_ATTR_FRAME])
649 return NL_SKIP;
650
651 dl_list_for_each(drv, &w->drvs, struct wpa_driver_nl80211_data,
652 wiphy_list) {
653 os_memset(&event, 0, sizeof(event));
654 event.rx_mgmt.frame = nla_data(tb[NL80211_ATTR_FRAME]);
655 event.rx_mgmt.frame_len = nla_len(tb[NL80211_ATTR_FRAME]);
656 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
657 }
658
659 return NL_SKIP;
660}
661
662
663static struct nl80211_wiphy_data *
664nl80211_get_wiphy_data_ap(struct i802_bss *bss)
665{
666 static DEFINE_DL_LIST(nl80211_wiphys);
667 struct nl80211_wiphy_data *w;
668 int wiphy_idx, found = 0;
669 struct i802_bss *tmp_bss;
670
671 if (bss->wiphy_data != NULL)
672 return bss->wiphy_data;
673
674 wiphy_idx = nl80211_get_wiphy_index(bss);
675
676 dl_list_for_each(w, &nl80211_wiphys, struct nl80211_wiphy_data, list) {
677 if (w->wiphy_idx == wiphy_idx)
678 goto add;
679 }
680
681 /* alloc new one */
682 w = os_zalloc(sizeof(*w));
683 if (w == NULL)
684 return NULL;
685 w->wiphy_idx = wiphy_idx;
686 dl_list_init(&w->bsss);
687 dl_list_init(&w->drvs);
688
689 w->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
690 if (!w->nl_cb) {
691 os_free(w);
692 return NULL;
693 }
694 nl_cb_set(w->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL);
695 nl_cb_set(w->nl_cb, NL_CB_VALID, NL_CB_CUSTOM, process_beacon_event,
696 w);
697
698 w->nl_beacons = nl_create_handle(bss->drv->global->nl_cb,
699 "wiphy beacons");
700 if (w->nl_beacons == NULL) {
701 os_free(w);
702 return NULL;
703 }
704
705 if (nl80211_register_beacons(bss->drv, w)) {
706 nl_destroy_handles(&w->nl_beacons);
707 os_free(w);
708 return NULL;
709 }
710
711 eloop_register_read_sock(nl_socket_get_fd(w->nl_beacons),
712 nl80211_recv_beacons, w, w->nl_beacons);
713
714 dl_list_add(&nl80211_wiphys, &w->list);
715
716add:
717 /* drv entry for this bss already there? */
718 dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
719 if (tmp_bss->drv == bss->drv) {
720 found = 1;
721 break;
722 }
723 }
724 /* if not add it */
725 if (!found)
726 dl_list_add(&w->drvs, &bss->drv->wiphy_list);
727
728 dl_list_add(&w->bsss, &bss->wiphy_list);
729 bss->wiphy_data = w;
730 return w;
731}
732
733
734static void nl80211_put_wiphy_data_ap(struct i802_bss *bss)
735{
736 struct nl80211_wiphy_data *w = bss->wiphy_data;
737 struct i802_bss *tmp_bss;
738 int found = 0;
739
740 if (w == NULL)
741 return;
742 bss->wiphy_data = NULL;
743 dl_list_del(&bss->wiphy_list);
744
745 /* still any for this drv present? */
746 dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
747 if (tmp_bss->drv == bss->drv) {
748 found = 1;
749 break;
750 }
751 }
752 /* if not remove it */
753 if (!found)
754 dl_list_del(&bss->drv->wiphy_list);
755
756 if (!dl_list_empty(&w->bsss))
757 return;
758
759 eloop_unregister_read_sock(nl_socket_get_fd(w->nl_beacons));
760
761 nl_cb_put(w->nl_cb);
762 nl_destroy_handles(&w->nl_beacons);
763 dl_list_del(&w->list);
764 os_free(w);
765}
766
767
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700768static int wpa_driver_nl80211_get_bssid(void *priv, u8 *bssid)
769{
770 struct i802_bss *bss = priv;
771 struct wpa_driver_nl80211_data *drv = bss->drv;
772 if (!drv->associated)
773 return -1;
774 os_memcpy(bssid, drv->bssid, ETH_ALEN);
775 return 0;
776}
777
778
779static int wpa_driver_nl80211_get_ssid(void *priv, u8 *ssid)
780{
781 struct i802_bss *bss = priv;
782 struct wpa_driver_nl80211_data *drv = bss->drv;
783 if (!drv->associated)
784 return -1;
785 os_memcpy(ssid, drv->ssid, drv->ssid_len);
786 return drv->ssid_len;
787}
788
789
790static void wpa_driver_nl80211_event_link(struct wpa_driver_nl80211_data *drv,
791 char *buf, size_t len, int del)
792{
793 union wpa_event_data event;
794
795 os_memset(&event, 0, sizeof(event));
796 if (len > sizeof(event.interface_status.ifname))
797 len = sizeof(event.interface_status.ifname) - 1;
798 os_memcpy(event.interface_status.ifname, buf, len);
799 event.interface_status.ievent = del ? EVENT_INTERFACE_REMOVED :
800 EVENT_INTERFACE_ADDED;
801
802 wpa_printf(MSG_DEBUG, "RTM_%sLINK, IFLA_IFNAME: Interface '%s' %s",
803 del ? "DEL" : "NEW",
804 event.interface_status.ifname,
805 del ? "removed" : "added");
806
807 if (os_strcmp(drv->first_bss.ifname, event.interface_status.ifname) == 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700808 if (del) {
809 if (drv->if_removed) {
810 wpa_printf(MSG_DEBUG, "nl80211: if_removed "
811 "already set - ignore event");
812 return;
813 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700814 drv->if_removed = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700815 } else {
816 if (if_nametoindex(drv->first_bss.ifname) == 0) {
817 wpa_printf(MSG_DEBUG, "nl80211: Interface %s "
818 "does not exist - ignore "
819 "RTM_NEWLINK",
820 drv->first_bss.ifname);
821 return;
822 }
823 if (!drv->if_removed) {
824 wpa_printf(MSG_DEBUG, "nl80211: if_removed "
825 "already cleared - ignore event");
826 return;
827 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700828 drv->if_removed = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700829 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700830 }
831
832 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
833}
834
835
836static int wpa_driver_nl80211_own_ifname(struct wpa_driver_nl80211_data *drv,
837 u8 *buf, size_t len)
838{
839 int attrlen, rta_len;
840 struct rtattr *attr;
841
842 attrlen = len;
843 attr = (struct rtattr *) buf;
844
845 rta_len = RTA_ALIGN(sizeof(struct rtattr));
846 while (RTA_OK(attr, attrlen)) {
847 if (attr->rta_type == IFLA_IFNAME) {
848 if (os_strcmp(((char *) attr) + rta_len, drv->first_bss.ifname)
849 == 0)
850 return 1;
851 else
852 break;
853 }
854 attr = RTA_NEXT(attr, attrlen);
855 }
856
857 return 0;
858}
859
860
861static int wpa_driver_nl80211_own_ifindex(struct wpa_driver_nl80211_data *drv,
862 int ifindex, u8 *buf, size_t len)
863{
864 if (drv->ifindex == ifindex)
865 return 1;
866
867 if (drv->if_removed && wpa_driver_nl80211_own_ifname(drv, buf, len)) {
868 drv->first_bss.ifindex = if_nametoindex(drv->first_bss.ifname);
869 wpa_printf(MSG_DEBUG, "nl80211: Update ifindex for a removed "
870 "interface");
871 wpa_driver_nl80211_finish_drv_init(drv);
872 return 1;
873 }
874
875 return 0;
876}
877
878
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800879static struct wpa_driver_nl80211_data *
880nl80211_find_drv(struct nl80211_global *global, int idx, u8 *buf, size_t len)
881{
882 struct wpa_driver_nl80211_data *drv;
883 dl_list_for_each(drv, &global->interfaces,
884 struct wpa_driver_nl80211_data, list) {
885 if (wpa_driver_nl80211_own_ifindex(drv, idx, buf, len) ||
886 have_ifidx(drv, idx))
887 return drv;
888 }
889 return NULL;
890}
891
892
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700893static void wpa_driver_nl80211_event_rtm_newlink(void *ctx,
894 struct ifinfomsg *ifi,
895 u8 *buf, size_t len)
896{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800897 struct nl80211_global *global = ctx;
898 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700899 int attrlen, rta_len;
900 struct rtattr *attr;
901 u32 brid = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800902 char namebuf[IFNAMSIZ];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700903
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800904 drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
905 if (!drv) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700906 wpa_printf(MSG_DEBUG, "nl80211: Ignore event for foreign "
907 "ifindex %d", ifi->ifi_index);
908 return;
909 }
910
911 wpa_printf(MSG_DEBUG, "RTM_NEWLINK: operstate=%d ifi_flags=0x%x "
912 "(%s%s%s%s)",
913 drv->operstate, ifi->ifi_flags,
914 (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
915 (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
916 (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
917 (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
918
919 if (!drv->if_disabled && !(ifi->ifi_flags & IFF_UP)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800920 if (if_indextoname(ifi->ifi_index, namebuf) &&
921 linux_iface_up(drv->global->ioctl_sock,
922 drv->first_bss.ifname) > 0) {
923 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
924 "event since interface %s is up", namebuf);
925 return;
926 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700927 wpa_printf(MSG_DEBUG, "nl80211: Interface down");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800928 if (drv->ignore_if_down_event) {
929 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
930 "event generated by mode change");
931 drv->ignore_if_down_event = 0;
932 } else {
933 drv->if_disabled = 1;
934 wpa_supplicant_event(drv->ctx,
935 EVENT_INTERFACE_DISABLED, NULL);
936 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700937 }
938
939 if (drv->if_disabled && (ifi->ifi_flags & IFF_UP)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800940 if (if_indextoname(ifi->ifi_index, namebuf) &&
941 linux_iface_up(drv->global->ioctl_sock,
942 drv->first_bss.ifname) == 0) {
943 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
944 "event since interface %s is down",
945 namebuf);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700946 } else if (if_nametoindex(drv->first_bss.ifname) == 0) {
947 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
948 "event since interface %s does not exist",
949 drv->first_bss.ifname);
950 } else if (drv->if_removed) {
951 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
952 "event since interface %s is marked "
953 "removed", drv->first_bss.ifname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800954 } else {
955 wpa_printf(MSG_DEBUG, "nl80211: Interface up");
956 drv->if_disabled = 0;
957 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_ENABLED,
958 NULL);
959 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700960 }
961
962 /*
963 * Some drivers send the association event before the operup event--in
964 * this case, lifting operstate in wpa_driver_nl80211_set_operstate()
965 * fails. This will hit us when wpa_supplicant does not need to do
966 * IEEE 802.1X authentication
967 */
968 if (drv->operstate == 1 &&
969 (ifi->ifi_flags & (IFF_LOWER_UP | IFF_DORMANT)) == IFF_LOWER_UP &&
970 !(ifi->ifi_flags & IFF_RUNNING))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800971 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700972 -1, IF_OPER_UP);
973
974 attrlen = len;
975 attr = (struct rtattr *) buf;
976 rta_len = RTA_ALIGN(sizeof(struct rtattr));
977 while (RTA_OK(attr, attrlen)) {
978 if (attr->rta_type == IFLA_IFNAME) {
979 wpa_driver_nl80211_event_link(
980 drv,
981 ((char *) attr) + rta_len,
982 attr->rta_len - rta_len, 0);
983 } else if (attr->rta_type == IFLA_MASTER)
984 brid = nla_get_u32((struct nlattr *) attr);
985 attr = RTA_NEXT(attr, attrlen);
986 }
987
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700988 if (ifi->ifi_family == AF_BRIDGE && brid) {
989 /* device has been added to bridge */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700990 if_indextoname(brid, namebuf);
991 wpa_printf(MSG_DEBUG, "nl80211: Add ifindex %u for bridge %s",
992 brid, namebuf);
993 add_ifidx(drv, brid);
994 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700995}
996
997
998static void wpa_driver_nl80211_event_rtm_dellink(void *ctx,
999 struct ifinfomsg *ifi,
1000 u8 *buf, size_t len)
1001{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001002 struct nl80211_global *global = ctx;
1003 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001004 int attrlen, rta_len;
1005 struct rtattr *attr;
1006 u32 brid = 0;
1007
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001008 drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
1009 if (!drv) {
1010 wpa_printf(MSG_DEBUG, "nl80211: Ignore dellink event for "
1011 "foreign ifindex %d", ifi->ifi_index);
1012 return;
1013 }
1014
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001015 attrlen = len;
1016 attr = (struct rtattr *) buf;
1017
1018 rta_len = RTA_ALIGN(sizeof(struct rtattr));
1019 while (RTA_OK(attr, attrlen)) {
1020 if (attr->rta_type == IFLA_IFNAME) {
1021 wpa_driver_nl80211_event_link(
1022 drv,
1023 ((char *) attr) + rta_len,
1024 attr->rta_len - rta_len, 1);
1025 } else if (attr->rta_type == IFLA_MASTER)
1026 brid = nla_get_u32((struct nlattr *) attr);
1027 attr = RTA_NEXT(attr, attrlen);
1028 }
1029
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001030 if (ifi->ifi_family == AF_BRIDGE && brid) {
1031 /* device has been removed from bridge */
1032 char namebuf[IFNAMSIZ];
1033 if_indextoname(brid, namebuf);
1034 wpa_printf(MSG_DEBUG, "nl80211: Remove ifindex %u for bridge "
1035 "%s", brid, namebuf);
1036 del_ifidx(drv, brid);
1037 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001038}
1039
1040
1041static void mlme_event_auth(struct wpa_driver_nl80211_data *drv,
1042 const u8 *frame, size_t len)
1043{
1044 const struct ieee80211_mgmt *mgmt;
1045 union wpa_event_data event;
1046
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001047 wpa_printf(MSG_DEBUG, "nl80211: Authenticate event");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001048 mgmt = (const struct ieee80211_mgmt *) frame;
1049 if (len < 24 + sizeof(mgmt->u.auth)) {
1050 wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
1051 "frame");
1052 return;
1053 }
1054
1055 os_memcpy(drv->auth_bssid, mgmt->sa, ETH_ALEN);
1056 os_memset(&event, 0, sizeof(event));
1057 os_memcpy(event.auth.peer, mgmt->sa, ETH_ALEN);
1058 event.auth.auth_type = le_to_host16(mgmt->u.auth.auth_alg);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001059 event.auth.auth_transaction =
1060 le_to_host16(mgmt->u.auth.auth_transaction);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001061 event.auth.status_code = le_to_host16(mgmt->u.auth.status_code);
1062 if (len > 24 + sizeof(mgmt->u.auth)) {
1063 event.auth.ies = mgmt->u.auth.variable;
1064 event.auth.ies_len = len - 24 - sizeof(mgmt->u.auth);
1065 }
1066
1067 wpa_supplicant_event(drv->ctx, EVENT_AUTH, &event);
1068}
1069
1070
Jouni Malinen87fd2792011-05-16 18:35:42 +03001071static unsigned int nl80211_get_assoc_freq(struct wpa_driver_nl80211_data *drv)
1072{
1073 struct nl_msg *msg;
1074 int ret;
1075 struct nl80211_bss_info_arg arg;
1076
1077 os_memset(&arg, 0, sizeof(arg));
1078 msg = nlmsg_alloc();
1079 if (!msg)
1080 goto nla_put_failure;
1081
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001082 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
Jouni Malinen87fd2792011-05-16 18:35:42 +03001083 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
1084
1085 arg.drv = drv;
1086 ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
1087 msg = NULL;
1088 if (ret == 0) {
1089 wpa_printf(MSG_DEBUG, "nl80211: Operating frequency for the "
1090 "associated BSS from scan results: %u MHz",
1091 arg.assoc_freq);
1092 return arg.assoc_freq ? arg.assoc_freq : drv->assoc_freq;
1093 }
1094 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
1095 "(%s)", ret, strerror(-ret));
1096nla_put_failure:
1097 nlmsg_free(msg);
1098 return drv->assoc_freq;
1099}
1100
1101
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001102static void mlme_event_assoc(struct wpa_driver_nl80211_data *drv,
1103 const u8 *frame, size_t len)
1104{
1105 const struct ieee80211_mgmt *mgmt;
1106 union wpa_event_data event;
1107 u16 status;
1108
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001109 wpa_printf(MSG_DEBUG, "nl80211: Associate event");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001110 mgmt = (const struct ieee80211_mgmt *) frame;
1111 if (len < 24 + sizeof(mgmt->u.assoc_resp)) {
1112 wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
1113 "frame");
1114 return;
1115 }
1116
1117 status = le_to_host16(mgmt->u.assoc_resp.status_code);
1118 if (status != WLAN_STATUS_SUCCESS) {
1119 os_memset(&event, 0, sizeof(event));
1120 event.assoc_reject.bssid = mgmt->bssid;
1121 if (len > 24 + sizeof(mgmt->u.assoc_resp)) {
1122 event.assoc_reject.resp_ies =
1123 (u8 *) mgmt->u.assoc_resp.variable;
1124 event.assoc_reject.resp_ies_len =
1125 len - 24 - sizeof(mgmt->u.assoc_resp);
1126 }
1127 event.assoc_reject.status_code = status;
1128
1129 wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
1130 return;
1131 }
1132
1133 drv->associated = 1;
1134 os_memcpy(drv->bssid, mgmt->sa, ETH_ALEN);
1135
1136 os_memset(&event, 0, sizeof(event));
1137 if (len > 24 + sizeof(mgmt->u.assoc_resp)) {
1138 event.assoc_info.resp_ies = (u8 *) mgmt->u.assoc_resp.variable;
1139 event.assoc_info.resp_ies_len =
1140 len - 24 - sizeof(mgmt->u.assoc_resp);
1141 }
1142
1143 event.assoc_info.freq = drv->assoc_freq;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001144
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001145 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
1146}
1147
1148
1149static void mlme_event_connect(struct wpa_driver_nl80211_data *drv,
1150 enum nl80211_commands cmd, struct nlattr *status,
1151 struct nlattr *addr, struct nlattr *req_ie,
1152 struct nlattr *resp_ie)
1153{
1154 union wpa_event_data event;
1155
1156 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
1157 /*
1158 * Avoid reporting two association events that would confuse
1159 * the core code.
1160 */
1161 wpa_printf(MSG_DEBUG, "nl80211: Ignore connect event (cmd=%d) "
1162 "when using userspace SME", cmd);
1163 return;
1164 }
1165
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001166 if (cmd == NL80211_CMD_CONNECT)
1167 wpa_printf(MSG_DEBUG, "nl80211: Connect event");
1168 else if (cmd == NL80211_CMD_ROAM)
1169 wpa_printf(MSG_DEBUG, "nl80211: Roam event");
1170
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001171 os_memset(&event, 0, sizeof(event));
1172 if (cmd == NL80211_CMD_CONNECT &&
1173 nla_get_u16(status) != WLAN_STATUS_SUCCESS) {
1174 if (addr)
1175 event.assoc_reject.bssid = nla_data(addr);
1176 if (resp_ie) {
1177 event.assoc_reject.resp_ies = nla_data(resp_ie);
1178 event.assoc_reject.resp_ies_len = nla_len(resp_ie);
1179 }
1180 event.assoc_reject.status_code = nla_get_u16(status);
1181 wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
1182 return;
1183 }
1184
1185 drv->associated = 1;
1186 if (addr)
1187 os_memcpy(drv->bssid, nla_data(addr), ETH_ALEN);
1188
1189 if (req_ie) {
1190 event.assoc_info.req_ies = nla_data(req_ie);
1191 event.assoc_info.req_ies_len = nla_len(req_ie);
1192 }
1193 if (resp_ie) {
1194 event.assoc_info.resp_ies = nla_data(resp_ie);
1195 event.assoc_info.resp_ies_len = nla_len(resp_ie);
1196 }
1197
Jouni Malinen87fd2792011-05-16 18:35:42 +03001198 event.assoc_info.freq = nl80211_get_assoc_freq(drv);
1199
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001200 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
1201}
1202
1203
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001204static void mlme_event_disconnect(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001205 struct nlattr *reason, struct nlattr *addr,
1206 struct nlattr *by_ap)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001207{
1208 union wpa_event_data data;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001209 unsigned int locally_generated = by_ap == NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001210
1211 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
1212 /*
1213 * Avoid reporting two disassociation events that could
1214 * confuse the core code.
1215 */
1216 wpa_printf(MSG_DEBUG, "nl80211: Ignore disconnect "
1217 "event when using userspace SME");
1218 return;
1219 }
1220
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001221 if (drv->ignore_next_local_disconnect) {
1222 drv->ignore_next_local_disconnect = 0;
1223 if (locally_generated) {
1224 wpa_printf(MSG_DEBUG, "nl80211: Ignore disconnect "
1225 "event triggered during reassociation");
1226 return;
1227 }
1228 wpa_printf(MSG_WARNING, "nl80211: Was expecting local "
1229 "disconnect but got another disconnect "
1230 "event first");
1231 }
1232
1233 wpa_printf(MSG_DEBUG, "nl80211: Disconnect event");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001234 drv->associated = 0;
1235 os_memset(&data, 0, sizeof(data));
1236 if (reason)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001237 data.deauth_info.reason_code = nla_get_u16(reason);
1238 data.deauth_info.locally_generated = by_ap == NULL;
1239 wpa_supplicant_event(drv->ctx, EVENT_DEAUTH, &data);
1240}
1241
1242
1243static void mlme_event_ch_switch(struct wpa_driver_nl80211_data *drv,
1244 struct nlattr *freq, struct nlattr *type)
1245{
1246 union wpa_event_data data;
1247 int ht_enabled = 1;
1248 int chan_offset = 0;
1249
1250 wpa_printf(MSG_DEBUG, "nl80211: Channel switch event");
1251
1252 if (!freq || !type)
1253 return;
1254
1255 switch (nla_get_u32(type)) {
1256 case NL80211_CHAN_NO_HT:
1257 ht_enabled = 0;
1258 break;
1259 case NL80211_CHAN_HT20:
1260 break;
1261 case NL80211_CHAN_HT40PLUS:
1262 chan_offset = 1;
1263 break;
1264 case NL80211_CHAN_HT40MINUS:
1265 chan_offset = -1;
1266 break;
1267 }
1268
1269 data.ch_switch.freq = nla_get_u32(freq);
1270 data.ch_switch.ht_enabled = ht_enabled;
1271 data.ch_switch.ch_offset = chan_offset;
1272
1273 wpa_supplicant_event(drv->ctx, EVENT_CH_SWITCH, &data);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001274}
1275
1276
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001277static void mlme_timeout_event(struct wpa_driver_nl80211_data *drv,
1278 enum nl80211_commands cmd, struct nlattr *addr)
1279{
1280 union wpa_event_data event;
1281 enum wpa_event_type ev;
1282
1283 if (nla_len(addr) != ETH_ALEN)
1284 return;
1285
1286 wpa_printf(MSG_DEBUG, "nl80211: MLME event %d; timeout with " MACSTR,
1287 cmd, MAC2STR((u8 *) nla_data(addr)));
1288
1289 if (cmd == NL80211_CMD_AUTHENTICATE)
1290 ev = EVENT_AUTH_TIMED_OUT;
1291 else if (cmd == NL80211_CMD_ASSOCIATE)
1292 ev = EVENT_ASSOC_TIMED_OUT;
1293 else
1294 return;
1295
1296 os_memset(&event, 0, sizeof(event));
1297 os_memcpy(event.timeout_event.addr, nla_data(addr), ETH_ALEN);
1298 wpa_supplicant_event(drv->ctx, ev, &event);
1299}
1300
1301
1302static void mlme_event_mgmt(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001303 struct nlattr *freq, struct nlattr *sig,
1304 const u8 *frame, size_t len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001305{
1306 const struct ieee80211_mgmt *mgmt;
1307 union wpa_event_data event;
1308 u16 fc, stype;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001309 int ssi_signal = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001310
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001311 wpa_printf(MSG_MSGDUMP, "nl80211: Frame event");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001312 mgmt = (const struct ieee80211_mgmt *) frame;
1313 if (len < 24) {
1314 wpa_printf(MSG_DEBUG, "nl80211: Too short action frame");
1315 return;
1316 }
1317
1318 fc = le_to_host16(mgmt->frame_control);
1319 stype = WLAN_FC_GET_STYPE(fc);
1320
Dmitry Shmidt04949592012-07-19 12:16:46 -07001321 if (sig)
1322 ssi_signal = (s32) nla_get_u32(sig);
1323
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001324 os_memset(&event, 0, sizeof(event));
1325 if (freq) {
1326 event.rx_action.freq = nla_get_u32(freq);
1327 drv->last_mgmt_freq = event.rx_action.freq;
1328 }
1329 if (stype == WLAN_FC_STYPE_ACTION) {
1330 event.rx_action.da = mgmt->da;
1331 event.rx_action.sa = mgmt->sa;
1332 event.rx_action.bssid = mgmt->bssid;
1333 event.rx_action.category = mgmt->u.action.category;
1334 event.rx_action.data = &mgmt->u.action.category + 1;
1335 event.rx_action.len = frame + len - event.rx_action.data;
1336 wpa_supplicant_event(drv->ctx, EVENT_RX_ACTION, &event);
1337 } else {
1338 event.rx_mgmt.frame = frame;
1339 event.rx_mgmt.frame_len = len;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001340 event.rx_mgmt.ssi_signal = ssi_signal;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001341 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
1342 }
1343}
1344
1345
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001346static void mlme_event_mgmt_tx_status(struct wpa_driver_nl80211_data *drv,
1347 struct nlattr *cookie, const u8 *frame,
1348 size_t len, struct nlattr *ack)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001349{
1350 union wpa_event_data event;
1351 const struct ieee80211_hdr *hdr;
1352 u16 fc;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001353
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001354 wpa_printf(MSG_DEBUG, "nl80211: Frame TX status event");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001355 if (!is_ap_interface(drv->nlmode)) {
1356 u64 cookie_val;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001357
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001358 if (!cookie)
1359 return;
1360
1361 cookie_val = nla_get_u64(cookie);
1362 wpa_printf(MSG_DEBUG, "nl80211: Action TX status:"
1363 " cookie=0%llx%s (ack=%d)",
1364 (long long unsigned int) cookie_val,
1365 cookie_val == drv->send_action_cookie ?
1366 " (match)" : " (unknown)", ack != NULL);
1367 if (cookie_val != drv->send_action_cookie)
1368 return;
1369 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001370
1371 hdr = (const struct ieee80211_hdr *) frame;
1372 fc = le_to_host16(hdr->frame_control);
1373
1374 os_memset(&event, 0, sizeof(event));
1375 event.tx_status.type = WLAN_FC_GET_TYPE(fc);
1376 event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
1377 event.tx_status.dst = hdr->addr1;
1378 event.tx_status.data = frame;
1379 event.tx_status.data_len = len;
1380 event.tx_status.ack = ack != NULL;
1381 wpa_supplicant_event(drv->ctx, EVENT_TX_STATUS, &event);
1382}
1383
1384
1385static void mlme_event_deauth_disassoc(struct wpa_driver_nl80211_data *drv,
1386 enum wpa_event_type type,
1387 const u8 *frame, size_t len)
1388{
1389 const struct ieee80211_mgmt *mgmt;
1390 union wpa_event_data event;
1391 const u8 *bssid = NULL;
1392 u16 reason_code = 0;
1393
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001394 if (type == EVENT_DEAUTH)
1395 wpa_printf(MSG_DEBUG, "nl80211: Deauthenticate event");
1396 else
1397 wpa_printf(MSG_DEBUG, "nl80211: Disassociate event");
1398
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001399 mgmt = (const struct ieee80211_mgmt *) frame;
1400 if (len >= 24) {
1401 bssid = mgmt->bssid;
1402
1403 if (drv->associated != 0 &&
1404 os_memcmp(bssid, drv->bssid, ETH_ALEN) != 0 &&
1405 os_memcmp(bssid, drv->auth_bssid, ETH_ALEN) != 0) {
1406 /*
1407 * We have presumably received this deauth as a
1408 * response to a clear_state_mismatch() outgoing
1409 * deauth. Don't let it take us offline!
1410 */
1411 wpa_printf(MSG_DEBUG, "nl80211: Deauth received "
1412 "from Unknown BSSID " MACSTR " -- ignoring",
1413 MAC2STR(bssid));
1414 return;
1415 }
1416 }
1417
1418 drv->associated = 0;
1419 os_memset(&event, 0, sizeof(event));
1420
1421 /* Note: Same offset for Reason Code in both frame subtypes */
1422 if (len >= 24 + sizeof(mgmt->u.deauth))
1423 reason_code = le_to_host16(mgmt->u.deauth.reason_code);
1424
1425 if (type == EVENT_DISASSOC) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001426 event.disassoc_info.locally_generated =
1427 !os_memcmp(mgmt->sa, drv->first_bss.addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001428 event.disassoc_info.addr = bssid;
1429 event.disassoc_info.reason_code = reason_code;
1430 if (frame + len > mgmt->u.disassoc.variable) {
1431 event.disassoc_info.ie = mgmt->u.disassoc.variable;
1432 event.disassoc_info.ie_len = frame + len -
1433 mgmt->u.disassoc.variable;
1434 }
1435 } else {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001436 event.deauth_info.locally_generated =
1437 !os_memcmp(mgmt->sa, drv->first_bss.addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001438 event.deauth_info.addr = bssid;
1439 event.deauth_info.reason_code = reason_code;
1440 if (frame + len > mgmt->u.deauth.variable) {
1441 event.deauth_info.ie = mgmt->u.deauth.variable;
1442 event.deauth_info.ie_len = frame + len -
1443 mgmt->u.deauth.variable;
1444 }
1445 }
1446
1447 wpa_supplicant_event(drv->ctx, type, &event);
1448}
1449
1450
1451static void mlme_event_unprot_disconnect(struct wpa_driver_nl80211_data *drv,
1452 enum wpa_event_type type,
1453 const u8 *frame, size_t len)
1454{
1455 const struct ieee80211_mgmt *mgmt;
1456 union wpa_event_data event;
1457 u16 reason_code = 0;
1458
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001459 if (type == EVENT_UNPROT_DEAUTH)
1460 wpa_printf(MSG_DEBUG, "nl80211: Unprot Deauthenticate event");
1461 else
1462 wpa_printf(MSG_DEBUG, "nl80211: Unprot Disassociate event");
1463
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001464 if (len < 24)
1465 return;
1466
1467 mgmt = (const struct ieee80211_mgmt *) frame;
1468
1469 os_memset(&event, 0, sizeof(event));
1470 /* Note: Same offset for Reason Code in both frame subtypes */
1471 if (len >= 24 + sizeof(mgmt->u.deauth))
1472 reason_code = le_to_host16(mgmt->u.deauth.reason_code);
1473
1474 if (type == EVENT_UNPROT_DISASSOC) {
1475 event.unprot_disassoc.sa = mgmt->sa;
1476 event.unprot_disassoc.da = mgmt->da;
1477 event.unprot_disassoc.reason_code = reason_code;
1478 } else {
1479 event.unprot_deauth.sa = mgmt->sa;
1480 event.unprot_deauth.da = mgmt->da;
1481 event.unprot_deauth.reason_code = reason_code;
1482 }
1483
1484 wpa_supplicant_event(drv->ctx, type, &event);
1485}
1486
1487
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001488static void mlme_event(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001489 enum nl80211_commands cmd, struct nlattr *frame,
1490 struct nlattr *addr, struct nlattr *timed_out,
1491 struct nlattr *freq, struct nlattr *ack,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001492 struct nlattr *cookie, struct nlattr *sig)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001493{
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001494 struct wpa_driver_nl80211_data *drv = bss->drv;
1495 const u8 *data;
1496 size_t len;
1497
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001498 if (timed_out && addr) {
1499 mlme_timeout_event(drv, cmd, addr);
1500 return;
1501 }
1502
1503 if (frame == NULL) {
1504 wpa_printf(MSG_DEBUG, "nl80211: MLME event %d without frame "
1505 "data", cmd);
1506 return;
1507 }
1508
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001509 data = nla_data(frame);
1510 len = nla_len(frame);
1511 if (len < 4 + ETH_ALEN) {
1512 wpa_printf(MSG_MSGDUMP, "nl80211: MLME event %d on %s(" MACSTR
1513 ") - too short",
1514 cmd, bss->ifname, MAC2STR(bss->addr));
1515 return;
1516 }
1517 wpa_printf(MSG_MSGDUMP, "nl80211: MLME event %d on %s(" MACSTR ") A1="
1518 MACSTR, cmd, bss->ifname, MAC2STR(bss->addr),
1519 MAC2STR(data + 4));
1520 if (cmd != NL80211_CMD_FRAME_TX_STATUS && !(data[4] & 0x01) &&
1521 os_memcmp(bss->addr, data + 4, ETH_ALEN) != 0) {
1522 wpa_printf(MSG_MSGDUMP, "nl80211: %s: Ignore MLME frame event "
1523 "for foreign address", bss->ifname);
1524 return;
1525 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001526 wpa_hexdump(MSG_MSGDUMP, "nl80211: MLME event frame",
1527 nla_data(frame), nla_len(frame));
1528
1529 switch (cmd) {
1530 case NL80211_CMD_AUTHENTICATE:
1531 mlme_event_auth(drv, nla_data(frame), nla_len(frame));
1532 break;
1533 case NL80211_CMD_ASSOCIATE:
1534 mlme_event_assoc(drv, nla_data(frame), nla_len(frame));
1535 break;
1536 case NL80211_CMD_DEAUTHENTICATE:
1537 mlme_event_deauth_disassoc(drv, EVENT_DEAUTH,
1538 nla_data(frame), nla_len(frame));
1539 break;
1540 case NL80211_CMD_DISASSOCIATE:
1541 mlme_event_deauth_disassoc(drv, EVENT_DISASSOC,
1542 nla_data(frame), nla_len(frame));
1543 break;
1544 case NL80211_CMD_FRAME:
Dmitry Shmidt04949592012-07-19 12:16:46 -07001545 mlme_event_mgmt(drv, freq, sig, nla_data(frame),
1546 nla_len(frame));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001547 break;
1548 case NL80211_CMD_FRAME_TX_STATUS:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001549 mlme_event_mgmt_tx_status(drv, cookie, nla_data(frame),
1550 nla_len(frame), ack);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001551 break;
1552 case NL80211_CMD_UNPROT_DEAUTHENTICATE:
1553 mlme_event_unprot_disconnect(drv, EVENT_UNPROT_DEAUTH,
1554 nla_data(frame), nla_len(frame));
1555 break;
1556 case NL80211_CMD_UNPROT_DISASSOCIATE:
1557 mlme_event_unprot_disconnect(drv, EVENT_UNPROT_DISASSOC,
1558 nla_data(frame), nla_len(frame));
1559 break;
1560 default:
1561 break;
1562 }
1563}
1564
1565
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001566static void mlme_event_michael_mic_failure(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001567 struct nlattr *tb[])
1568{
1569 union wpa_event_data data;
1570
1571 wpa_printf(MSG_DEBUG, "nl80211: MLME event Michael MIC failure");
1572 os_memset(&data, 0, sizeof(data));
1573 if (tb[NL80211_ATTR_MAC]) {
1574 wpa_hexdump(MSG_DEBUG, "nl80211: Source MAC address",
1575 nla_data(tb[NL80211_ATTR_MAC]),
1576 nla_len(tb[NL80211_ATTR_MAC]));
1577 data.michael_mic_failure.src = nla_data(tb[NL80211_ATTR_MAC]);
1578 }
1579 if (tb[NL80211_ATTR_KEY_SEQ]) {
1580 wpa_hexdump(MSG_DEBUG, "nl80211: TSC",
1581 nla_data(tb[NL80211_ATTR_KEY_SEQ]),
1582 nla_len(tb[NL80211_ATTR_KEY_SEQ]));
1583 }
1584 if (tb[NL80211_ATTR_KEY_TYPE]) {
1585 enum nl80211_key_type key_type =
1586 nla_get_u32(tb[NL80211_ATTR_KEY_TYPE]);
1587 wpa_printf(MSG_DEBUG, "nl80211: Key Type %d", key_type);
1588 if (key_type == NL80211_KEYTYPE_PAIRWISE)
1589 data.michael_mic_failure.unicast = 1;
1590 } else
1591 data.michael_mic_failure.unicast = 1;
1592
1593 if (tb[NL80211_ATTR_KEY_IDX]) {
1594 u8 key_id = nla_get_u8(tb[NL80211_ATTR_KEY_IDX]);
1595 wpa_printf(MSG_DEBUG, "nl80211: Key Id %d", key_id);
1596 }
1597
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001598 wpa_supplicant_event(bss->ctx, EVENT_MICHAEL_MIC_FAILURE, &data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001599}
1600
1601
1602static void mlme_event_join_ibss(struct wpa_driver_nl80211_data *drv,
1603 struct nlattr *tb[])
1604{
1605 if (tb[NL80211_ATTR_MAC] == NULL) {
1606 wpa_printf(MSG_DEBUG, "nl80211: No address in IBSS joined "
1607 "event");
1608 return;
1609 }
1610 os_memcpy(drv->bssid, nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
1611 drv->associated = 1;
1612 wpa_printf(MSG_DEBUG, "nl80211: IBSS " MACSTR " joined",
1613 MAC2STR(drv->bssid));
1614
1615 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, NULL);
1616}
1617
1618
1619static void mlme_event_remain_on_channel(struct wpa_driver_nl80211_data *drv,
1620 int cancel_event, struct nlattr *tb[])
1621{
1622 unsigned int freq, chan_type, duration;
1623 union wpa_event_data data;
1624 u64 cookie;
1625
1626 if (tb[NL80211_ATTR_WIPHY_FREQ])
1627 freq = nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]);
1628 else
1629 freq = 0;
1630
1631 if (tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE])
1632 chan_type = nla_get_u32(tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1633 else
1634 chan_type = 0;
1635
1636 if (tb[NL80211_ATTR_DURATION])
1637 duration = nla_get_u32(tb[NL80211_ATTR_DURATION]);
1638 else
1639 duration = 0;
1640
1641 if (tb[NL80211_ATTR_COOKIE])
1642 cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
1643 else
1644 cookie = 0;
1645
1646 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel event (cancel=%d "
1647 "freq=%u channel_type=%u duration=%u cookie=0x%llx (%s))",
1648 cancel_event, freq, chan_type, duration,
1649 (long long unsigned int) cookie,
1650 cookie == drv->remain_on_chan_cookie ? "match" : "unknown");
1651
1652 if (cookie != drv->remain_on_chan_cookie)
1653 return; /* not for us */
1654
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001655 if (cancel_event)
1656 drv->pending_remain_on_chan = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001657
1658 os_memset(&data, 0, sizeof(data));
1659 data.remain_on_channel.freq = freq;
1660 data.remain_on_channel.duration = duration;
1661 wpa_supplicant_event(drv->ctx, cancel_event ?
1662 EVENT_CANCEL_REMAIN_ON_CHANNEL :
1663 EVENT_REMAIN_ON_CHANNEL, &data);
1664}
1665
1666
Dmitry Shmidt700a1372013-03-15 14:14:44 -07001667static void mlme_event_ft_event(struct wpa_driver_nl80211_data *drv,
1668 struct nlattr *tb[])
1669{
1670 union wpa_event_data data;
1671
1672 os_memset(&data, 0, sizeof(data));
1673
1674 if (tb[NL80211_ATTR_IE]) {
1675 data.ft_ies.ies = nla_data(tb[NL80211_ATTR_IE]);
1676 data.ft_ies.ies_len = nla_len(tb[NL80211_ATTR_IE]);
1677 }
1678
1679 if (tb[NL80211_ATTR_IE_RIC]) {
1680 data.ft_ies.ric_ies = nla_data(tb[NL80211_ATTR_IE_RIC]);
1681 data.ft_ies.ric_ies_len = nla_len(tb[NL80211_ATTR_IE_RIC]);
1682 }
1683
1684 if (tb[NL80211_ATTR_MAC])
1685 os_memcpy(data.ft_ies.target_ap,
1686 nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
1687
1688 wpa_printf(MSG_DEBUG, "nl80211: FT event target_ap " MACSTR,
1689 MAC2STR(data.ft_ies.target_ap));
1690
1691 wpa_supplicant_event(drv->ctx, EVENT_FT_RESPONSE, &data);
1692}
1693
1694
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001695static void send_scan_event(struct wpa_driver_nl80211_data *drv, int aborted,
1696 struct nlattr *tb[])
1697{
1698 union wpa_event_data event;
1699 struct nlattr *nl;
1700 int rem;
1701 struct scan_info *info;
1702#define MAX_REPORT_FREQS 50
1703 int freqs[MAX_REPORT_FREQS];
1704 int num_freqs = 0;
1705
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001706 if (drv->scan_for_auth) {
1707 drv->scan_for_auth = 0;
1708 wpa_printf(MSG_DEBUG, "nl80211: Scan results for missing "
1709 "cfg80211 BSS entry");
1710 wpa_driver_nl80211_authenticate_retry(drv);
1711 return;
1712 }
1713
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001714 os_memset(&event, 0, sizeof(event));
1715 info = &event.scan_info;
1716 info->aborted = aborted;
1717
1718 if (tb[NL80211_ATTR_SCAN_SSIDS]) {
1719 nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_SSIDS], rem) {
1720 struct wpa_driver_scan_ssid *s =
1721 &info->ssids[info->num_ssids];
1722 s->ssid = nla_data(nl);
1723 s->ssid_len = nla_len(nl);
1724 info->num_ssids++;
1725 if (info->num_ssids == WPAS_MAX_SCAN_SSIDS)
1726 break;
1727 }
1728 }
1729 if (tb[NL80211_ATTR_SCAN_FREQUENCIES]) {
1730 nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_FREQUENCIES], rem)
1731 {
1732 freqs[num_freqs] = nla_get_u32(nl);
1733 num_freqs++;
1734 if (num_freqs == MAX_REPORT_FREQS - 1)
1735 break;
1736 }
1737 info->freqs = freqs;
1738 info->num_freqs = num_freqs;
1739 }
1740 wpa_supplicant_event(drv->ctx, EVENT_SCAN_RESULTS, &event);
1741}
1742
1743
1744static int get_link_signal(struct nl_msg *msg, void *arg)
1745{
1746 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1747 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1748 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
1749 static struct nla_policy policy[NL80211_STA_INFO_MAX + 1] = {
1750 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
1751 };
1752 struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
1753 static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
1754 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
1755 [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
1756 [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
1757 [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
1758 };
1759 struct wpa_signal_info *sig_change = arg;
1760
1761 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1762 genlmsg_attrlen(gnlh, 0), NULL);
1763 if (!tb[NL80211_ATTR_STA_INFO] ||
1764 nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
1765 tb[NL80211_ATTR_STA_INFO], policy))
1766 return NL_SKIP;
1767 if (!sinfo[NL80211_STA_INFO_SIGNAL])
1768 return NL_SKIP;
1769
1770 sig_change->current_signal =
1771 (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
1772
1773 if (sinfo[NL80211_STA_INFO_TX_BITRATE]) {
1774 if (nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
1775 sinfo[NL80211_STA_INFO_TX_BITRATE],
1776 rate_policy)) {
1777 sig_change->current_txrate = 0;
1778 } else {
1779 if (rinfo[NL80211_RATE_INFO_BITRATE]) {
1780 sig_change->current_txrate =
1781 nla_get_u16(rinfo[
1782 NL80211_RATE_INFO_BITRATE]) * 100;
1783 }
1784 }
1785 }
1786
1787 return NL_SKIP;
1788}
1789
1790
1791static int nl80211_get_link_signal(struct wpa_driver_nl80211_data *drv,
1792 struct wpa_signal_info *sig)
1793{
1794 struct nl_msg *msg;
1795
1796 sig->current_signal = -9999;
1797 sig->current_txrate = 0;
1798
1799 msg = nlmsg_alloc();
1800 if (!msg)
1801 return -ENOMEM;
1802
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001803 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001804
1805 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
1806 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid);
1807
1808 return send_and_recv_msgs(drv, msg, get_link_signal, sig);
1809 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001810 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001811 return -ENOBUFS;
1812}
1813
1814
1815static int get_link_noise(struct nl_msg *msg, void *arg)
1816{
1817 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1818 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1819 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
1820 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
1821 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
1822 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
1823 };
1824 struct wpa_signal_info *sig_change = arg;
1825
1826 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1827 genlmsg_attrlen(gnlh, 0), NULL);
1828
1829 if (!tb[NL80211_ATTR_SURVEY_INFO]) {
1830 wpa_printf(MSG_DEBUG, "nl80211: survey data missing!");
1831 return NL_SKIP;
1832 }
1833
1834 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
1835 tb[NL80211_ATTR_SURVEY_INFO],
1836 survey_policy)) {
1837 wpa_printf(MSG_DEBUG, "nl80211: failed to parse nested "
1838 "attributes!");
1839 return NL_SKIP;
1840 }
1841
1842 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
1843 return NL_SKIP;
1844
1845 if (nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
1846 sig_change->frequency)
1847 return NL_SKIP;
1848
1849 if (!sinfo[NL80211_SURVEY_INFO_NOISE])
1850 return NL_SKIP;
1851
1852 sig_change->current_noise =
1853 (s8) nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
1854
1855 return NL_SKIP;
1856}
1857
1858
1859static int nl80211_get_link_noise(struct wpa_driver_nl80211_data *drv,
1860 struct wpa_signal_info *sig_change)
1861{
1862 struct nl_msg *msg;
1863
1864 sig_change->current_noise = 9999;
1865 sig_change->frequency = drv->assoc_freq;
1866
1867 msg = nlmsg_alloc();
1868 if (!msg)
1869 return -ENOMEM;
1870
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001871 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001872
1873 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
1874
1875 return send_and_recv_msgs(drv, msg, get_link_noise, sig_change);
1876 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001877 nlmsg_free(msg);
1878 return -ENOBUFS;
1879}
1880
1881
1882static int get_noise_for_scan_results(struct nl_msg *msg, void *arg)
1883{
1884 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1885 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1886 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
1887 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
1888 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
1889 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
1890 };
1891 struct wpa_scan_results *scan_results = arg;
1892 struct wpa_scan_res *scan_res;
1893 size_t i;
1894
1895 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1896 genlmsg_attrlen(gnlh, 0), NULL);
1897
1898 if (!tb[NL80211_ATTR_SURVEY_INFO]) {
1899 wpa_printf(MSG_DEBUG, "nl80211: Survey data missing");
1900 return NL_SKIP;
1901 }
1902
1903 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
1904 tb[NL80211_ATTR_SURVEY_INFO],
1905 survey_policy)) {
1906 wpa_printf(MSG_DEBUG, "nl80211: Failed to parse nested "
1907 "attributes");
1908 return NL_SKIP;
1909 }
1910
1911 if (!sinfo[NL80211_SURVEY_INFO_NOISE])
1912 return NL_SKIP;
1913
1914 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
1915 return NL_SKIP;
1916
1917 for (i = 0; i < scan_results->num; ++i) {
1918 scan_res = scan_results->res[i];
1919 if (!scan_res)
1920 continue;
1921 if ((int) nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
1922 scan_res->freq)
1923 continue;
1924 if (!(scan_res->flags & WPA_SCAN_NOISE_INVALID))
1925 continue;
1926 scan_res->noise = (s8)
1927 nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
1928 scan_res->flags &= ~WPA_SCAN_NOISE_INVALID;
1929 }
1930
1931 return NL_SKIP;
1932}
1933
1934
1935static int nl80211_get_noise_for_scan_results(
1936 struct wpa_driver_nl80211_data *drv,
1937 struct wpa_scan_results *scan_res)
1938{
1939 struct nl_msg *msg;
1940
1941 msg = nlmsg_alloc();
1942 if (!msg)
1943 return -ENOMEM;
1944
1945 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
1946
1947 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
1948
1949 return send_and_recv_msgs(drv, msg, get_noise_for_scan_results,
1950 scan_res);
1951 nla_put_failure:
1952 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001953 return -ENOBUFS;
1954}
1955
1956
1957static void nl80211_cqm_event(struct wpa_driver_nl80211_data *drv,
1958 struct nlattr *tb[])
1959{
1960 static struct nla_policy cqm_policy[NL80211_ATTR_CQM_MAX + 1] = {
1961 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
1962 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U8 },
1963 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
1964 [NL80211_ATTR_CQM_PKT_LOSS_EVENT] = { .type = NLA_U32 },
1965 };
1966 struct nlattr *cqm[NL80211_ATTR_CQM_MAX + 1];
1967 enum nl80211_cqm_rssi_threshold_event event;
1968 union wpa_event_data ed;
1969 struct wpa_signal_info sig;
1970 int res;
1971
1972 if (tb[NL80211_ATTR_CQM] == NULL ||
1973 nla_parse_nested(cqm, NL80211_ATTR_CQM_MAX, tb[NL80211_ATTR_CQM],
1974 cqm_policy)) {
1975 wpa_printf(MSG_DEBUG, "nl80211: Ignore invalid CQM event");
1976 return;
1977 }
1978
1979 os_memset(&ed, 0, sizeof(ed));
1980
1981 if (cqm[NL80211_ATTR_CQM_PKT_LOSS_EVENT]) {
1982 if (!tb[NL80211_ATTR_MAC])
1983 return;
1984 os_memcpy(ed.low_ack.addr, nla_data(tb[NL80211_ATTR_MAC]),
1985 ETH_ALEN);
1986 wpa_supplicant_event(drv->ctx, EVENT_STATION_LOW_ACK, &ed);
1987 return;
1988 }
1989
1990 if (cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] == NULL)
1991 return;
1992 event = nla_get_u32(cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT]);
1993
1994 if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH) {
1995 wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor "
1996 "event: RSSI high");
1997 ed.signal_change.above_threshold = 1;
1998 } else if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW) {
1999 wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor "
2000 "event: RSSI low");
2001 ed.signal_change.above_threshold = 0;
2002 } else
2003 return;
2004
2005 res = nl80211_get_link_signal(drv, &sig);
2006 if (res == 0) {
2007 ed.signal_change.current_signal = sig.current_signal;
2008 ed.signal_change.current_txrate = sig.current_txrate;
2009 wpa_printf(MSG_DEBUG, "nl80211: Signal: %d dBm txrate: %d",
2010 sig.current_signal, sig.current_txrate);
2011 }
2012
2013 res = nl80211_get_link_noise(drv, &sig);
2014 if (res == 0) {
2015 ed.signal_change.current_noise = sig.current_noise;
2016 wpa_printf(MSG_DEBUG, "nl80211: Noise: %d dBm",
2017 sig.current_noise);
2018 }
2019
2020 wpa_supplicant_event(drv->ctx, EVENT_SIGNAL_CHANGE, &ed);
2021}
2022
2023
2024static void nl80211_new_station_event(struct wpa_driver_nl80211_data *drv,
2025 struct nlattr **tb)
2026{
2027 u8 *addr;
2028 union wpa_event_data data;
2029
2030 if (tb[NL80211_ATTR_MAC] == NULL)
2031 return;
2032 addr = nla_data(tb[NL80211_ATTR_MAC]);
2033 wpa_printf(MSG_DEBUG, "nl80211: New station " MACSTR, MAC2STR(addr));
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07002034
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002035 if (is_ap_interface(drv->nlmode) && drv->device_ap_sme) {
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07002036 u8 *ies = NULL;
2037 size_t ies_len = 0;
2038 if (tb[NL80211_ATTR_IE]) {
2039 ies = nla_data(tb[NL80211_ATTR_IE]);
2040 ies_len = nla_len(tb[NL80211_ATTR_IE]);
2041 }
2042 wpa_hexdump(MSG_DEBUG, "nl80211: Assoc Req IEs", ies, ies_len);
2043 drv_event_assoc(drv->ctx, addr, ies, ies_len, 0);
2044 return;
2045 }
2046
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002047 if (drv->nlmode != NL80211_IFTYPE_ADHOC)
2048 return;
2049
2050 os_memset(&data, 0, sizeof(data));
2051 os_memcpy(data.ibss_rsn_start.peer, addr, ETH_ALEN);
2052 wpa_supplicant_event(drv->ctx, EVENT_IBSS_RSN_START, &data);
2053}
2054
2055
2056static void nl80211_del_station_event(struct wpa_driver_nl80211_data *drv,
2057 struct nlattr **tb)
2058{
2059 u8 *addr;
2060 union wpa_event_data data;
2061
2062 if (tb[NL80211_ATTR_MAC] == NULL)
2063 return;
2064 addr = nla_data(tb[NL80211_ATTR_MAC]);
2065 wpa_printf(MSG_DEBUG, "nl80211: Delete station " MACSTR,
2066 MAC2STR(addr));
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07002067
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002068 if (is_ap_interface(drv->nlmode) && drv->device_ap_sme) {
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07002069 drv_event_disassoc(drv->ctx, addr);
2070 return;
2071 }
2072
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002073 if (drv->nlmode != NL80211_IFTYPE_ADHOC)
2074 return;
2075
2076 os_memset(&data, 0, sizeof(data));
2077 os_memcpy(data.ibss_peer_lost.peer, addr, ETH_ALEN);
2078 wpa_supplicant_event(drv->ctx, EVENT_IBSS_PEER_LOST, &data);
2079}
2080
2081
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002082static void nl80211_rekey_offload_event(struct wpa_driver_nl80211_data *drv,
2083 struct nlattr **tb)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002084{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002085 struct nlattr *rekey_info[NUM_NL80211_REKEY_DATA];
2086 static struct nla_policy rekey_policy[NUM_NL80211_REKEY_DATA] = {
2087 [NL80211_REKEY_DATA_KEK] = {
2088 .minlen = NL80211_KEK_LEN,
2089 .maxlen = NL80211_KEK_LEN,
2090 },
2091 [NL80211_REKEY_DATA_KCK] = {
2092 .minlen = NL80211_KCK_LEN,
2093 .maxlen = NL80211_KCK_LEN,
2094 },
2095 [NL80211_REKEY_DATA_REPLAY_CTR] = {
2096 .minlen = NL80211_REPLAY_CTR_LEN,
2097 .maxlen = NL80211_REPLAY_CTR_LEN,
2098 },
2099 };
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002100 union wpa_event_data data;
2101
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002102 if (!tb[NL80211_ATTR_MAC])
2103 return;
2104 if (!tb[NL80211_ATTR_REKEY_DATA])
2105 return;
2106 if (nla_parse_nested(rekey_info, MAX_NL80211_REKEY_DATA,
2107 tb[NL80211_ATTR_REKEY_DATA], rekey_policy))
2108 return;
2109 if (!rekey_info[NL80211_REKEY_DATA_REPLAY_CTR])
2110 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002111
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002112 os_memset(&data, 0, sizeof(data));
2113 data.driver_gtk_rekey.bssid = nla_data(tb[NL80211_ATTR_MAC]);
2114 wpa_printf(MSG_DEBUG, "nl80211: Rekey offload event for BSSID " MACSTR,
2115 MAC2STR(data.driver_gtk_rekey.bssid));
2116 data.driver_gtk_rekey.replay_ctr =
2117 nla_data(rekey_info[NL80211_REKEY_DATA_REPLAY_CTR]);
2118 wpa_hexdump(MSG_DEBUG, "nl80211: Rekey offload - Replay Counter",
2119 data.driver_gtk_rekey.replay_ctr, NL80211_REPLAY_CTR_LEN);
2120 wpa_supplicant_event(drv->ctx, EVENT_DRIVER_GTK_REKEY, &data);
2121}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002122
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002123
2124static void nl80211_pmksa_candidate_event(struct wpa_driver_nl80211_data *drv,
2125 struct nlattr **tb)
2126{
2127 struct nlattr *cand[NUM_NL80211_PMKSA_CANDIDATE];
2128 static struct nla_policy cand_policy[NUM_NL80211_PMKSA_CANDIDATE] = {
2129 [NL80211_PMKSA_CANDIDATE_INDEX] = { .type = NLA_U32 },
2130 [NL80211_PMKSA_CANDIDATE_BSSID] = {
2131 .minlen = ETH_ALEN,
2132 .maxlen = ETH_ALEN,
2133 },
2134 [NL80211_PMKSA_CANDIDATE_PREAUTH] = { .type = NLA_FLAG },
2135 };
2136 union wpa_event_data data;
2137
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002138 wpa_printf(MSG_DEBUG, "nl80211: PMKSA candidate event");
2139
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002140 if (!tb[NL80211_ATTR_PMKSA_CANDIDATE])
2141 return;
2142 if (nla_parse_nested(cand, MAX_NL80211_PMKSA_CANDIDATE,
2143 tb[NL80211_ATTR_PMKSA_CANDIDATE], cand_policy))
2144 return;
2145 if (!cand[NL80211_PMKSA_CANDIDATE_INDEX] ||
2146 !cand[NL80211_PMKSA_CANDIDATE_BSSID])
2147 return;
2148
2149 os_memset(&data, 0, sizeof(data));
2150 os_memcpy(data.pmkid_candidate.bssid,
2151 nla_data(cand[NL80211_PMKSA_CANDIDATE_BSSID]), ETH_ALEN);
2152 data.pmkid_candidate.index =
2153 nla_get_u32(cand[NL80211_PMKSA_CANDIDATE_INDEX]);
2154 data.pmkid_candidate.preauth =
2155 cand[NL80211_PMKSA_CANDIDATE_PREAUTH] != NULL;
2156 wpa_supplicant_event(drv->ctx, EVENT_PMKID_CANDIDATE, &data);
2157}
2158
2159
2160static void nl80211_client_probe_event(struct wpa_driver_nl80211_data *drv,
2161 struct nlattr **tb)
2162{
2163 union wpa_event_data data;
2164
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002165 wpa_printf(MSG_DEBUG, "nl80211: Probe client event");
2166
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002167 if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_ACK])
2168 return;
2169
2170 os_memset(&data, 0, sizeof(data));
2171 os_memcpy(data.client_poll.addr,
2172 nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
2173
2174 wpa_supplicant_event(drv->ctx, EVENT_DRIVER_CLIENT_POLL_OK, &data);
2175}
2176
2177
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002178static void nl80211_tdls_oper_event(struct wpa_driver_nl80211_data *drv,
2179 struct nlattr **tb)
2180{
2181 union wpa_event_data data;
2182
2183 wpa_printf(MSG_DEBUG, "nl80211: TDLS operation event");
2184
2185 if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_TDLS_OPERATION])
2186 return;
2187
2188 os_memset(&data, 0, sizeof(data));
2189 os_memcpy(data.tdls.peer, nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
2190 switch (nla_get_u8(tb[NL80211_ATTR_TDLS_OPERATION])) {
2191 case NL80211_TDLS_SETUP:
2192 wpa_printf(MSG_DEBUG, "nl80211: TDLS setup request for peer "
2193 MACSTR, MAC2STR(data.tdls.peer));
2194 data.tdls.oper = TDLS_REQUEST_SETUP;
2195 break;
2196 case NL80211_TDLS_TEARDOWN:
2197 wpa_printf(MSG_DEBUG, "nl80211: TDLS teardown request for peer "
2198 MACSTR, MAC2STR(data.tdls.peer));
2199 data.tdls.oper = TDLS_REQUEST_TEARDOWN;
2200 break;
2201 default:
2202 wpa_printf(MSG_DEBUG, "nl80211: Unsupported TDLS operatione "
2203 "event");
2204 return;
2205 }
2206 if (tb[NL80211_ATTR_REASON_CODE]) {
2207 data.tdls.reason_code =
2208 nla_get_u16(tb[NL80211_ATTR_REASON_CODE]);
2209 }
2210
2211 wpa_supplicant_event(drv->ctx, EVENT_TDLS, &data);
2212}
2213
2214
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002215static void nl80211_connect_failed_event(struct wpa_driver_nl80211_data *drv,
2216 struct nlattr **tb)
2217{
2218 union wpa_event_data data;
2219 u32 reason;
2220
2221 wpa_printf(MSG_DEBUG, "nl80211: Connect failed event");
2222
2223 if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_CONN_FAILED_REASON])
2224 return;
2225
2226 os_memset(&data, 0, sizeof(data));
2227 os_memcpy(data.connect_failed_reason.addr,
2228 nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
2229
2230 reason = nla_get_u32(tb[NL80211_ATTR_CONN_FAILED_REASON]);
2231 switch (reason) {
2232 case NL80211_CONN_FAIL_MAX_CLIENTS:
2233 wpa_printf(MSG_DEBUG, "nl80211: Max client reached");
2234 data.connect_failed_reason.code = MAX_CLIENT_REACHED;
2235 break;
2236 case NL80211_CONN_FAIL_BLOCKED_CLIENT:
2237 wpa_printf(MSG_DEBUG, "nl80211: Blocked client " MACSTR
2238 " tried to connect",
2239 MAC2STR(data.connect_failed_reason.addr));
2240 data.connect_failed_reason.code = BLOCKED_CLIENT;
2241 break;
2242 default:
2243 wpa_printf(MSG_DEBUG, "nl8021l: Unknown connect failed reason "
2244 "%u", reason);
2245 return;
2246 }
2247
2248 wpa_supplicant_event(drv->ctx, EVENT_CONNECT_FAILED_REASON, &data);
2249}
2250
2251
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002252static void nl80211_spurious_frame(struct i802_bss *bss, struct nlattr **tb,
2253 int wds)
2254{
2255 struct wpa_driver_nl80211_data *drv = bss->drv;
2256 union wpa_event_data event;
2257
2258 if (!tb[NL80211_ATTR_MAC])
2259 return;
2260
2261 os_memset(&event, 0, sizeof(event));
2262 event.rx_from_unknown.bssid = bss->addr;
2263 event.rx_from_unknown.addr = nla_data(tb[NL80211_ATTR_MAC]);
2264 event.rx_from_unknown.wds = wds;
2265
2266 wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event);
2267}
2268
2269
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002270static void do_process_drv_event(struct i802_bss *bss, int cmd,
2271 struct nlattr **tb)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002272{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002273 struct wpa_driver_nl80211_data *drv = bss->drv;
2274
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002275 if (drv->ap_scan_as_station != NL80211_IFTYPE_UNSPECIFIED &&
2276 (cmd == NL80211_CMD_NEW_SCAN_RESULTS ||
2277 cmd == NL80211_CMD_SCAN_ABORTED)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002278 wpa_driver_nl80211_set_mode(&drv->first_bss,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002279 drv->ap_scan_as_station);
2280 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002281 }
2282
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002283 switch (cmd) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002284 case NL80211_CMD_TRIGGER_SCAN:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002285 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Scan trigger");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002286 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002287 case NL80211_CMD_START_SCHED_SCAN:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002288 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Sched scan started");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002289 break;
2290 case NL80211_CMD_SCHED_SCAN_STOPPED:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002291 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Sched scan stopped");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002292 wpa_supplicant_event(drv->ctx, EVENT_SCHED_SCAN_STOPPED, NULL);
2293 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002294 case NL80211_CMD_NEW_SCAN_RESULTS:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002295 wpa_dbg(drv->ctx, MSG_DEBUG,
2296 "nl80211: New scan results available");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002297 drv->scan_complete_events = 1;
2298 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
2299 drv->ctx);
2300 send_scan_event(drv, 0, tb);
2301 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002302 case NL80211_CMD_SCHED_SCAN_RESULTS:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002303 wpa_dbg(drv->ctx, MSG_DEBUG,
2304 "nl80211: New sched scan results available");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002305 send_scan_event(drv, 0, tb);
2306 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002307 case NL80211_CMD_SCAN_ABORTED:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002308 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Scan aborted");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002309 /*
2310 * Need to indicate that scan results are available in order
2311 * not to make wpa_supplicant stop its scanning.
2312 */
2313 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
2314 drv->ctx);
2315 send_scan_event(drv, 1, tb);
2316 break;
2317 case NL80211_CMD_AUTHENTICATE:
2318 case NL80211_CMD_ASSOCIATE:
2319 case NL80211_CMD_DEAUTHENTICATE:
2320 case NL80211_CMD_DISASSOCIATE:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002321 case NL80211_CMD_FRAME_TX_STATUS:
2322 case NL80211_CMD_UNPROT_DEAUTHENTICATE:
2323 case NL80211_CMD_UNPROT_DISASSOCIATE:
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002324 mlme_event(bss, cmd, tb[NL80211_ATTR_FRAME],
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002325 tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT],
2326 tb[NL80211_ATTR_WIPHY_FREQ], tb[NL80211_ATTR_ACK],
Dmitry Shmidt04949592012-07-19 12:16:46 -07002327 tb[NL80211_ATTR_COOKIE],
2328 tb[NL80211_ATTR_RX_SIGNAL_DBM]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002329 break;
2330 case NL80211_CMD_CONNECT:
2331 case NL80211_CMD_ROAM:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002332 mlme_event_connect(drv, cmd,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002333 tb[NL80211_ATTR_STATUS_CODE],
2334 tb[NL80211_ATTR_MAC],
2335 tb[NL80211_ATTR_REQ_IE],
2336 tb[NL80211_ATTR_RESP_IE]);
2337 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002338 case NL80211_CMD_CH_SWITCH_NOTIFY:
2339 mlme_event_ch_switch(drv, tb[NL80211_ATTR_WIPHY_FREQ],
2340 tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
2341 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002342 case NL80211_CMD_DISCONNECT:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002343 mlme_event_disconnect(drv, tb[NL80211_ATTR_REASON_CODE],
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002344 tb[NL80211_ATTR_MAC],
2345 tb[NL80211_ATTR_DISCONNECTED_BY_AP]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002346 break;
2347 case NL80211_CMD_MICHAEL_MIC_FAILURE:
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002348 mlme_event_michael_mic_failure(bss, tb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002349 break;
2350 case NL80211_CMD_JOIN_IBSS:
2351 mlme_event_join_ibss(drv, tb);
2352 break;
2353 case NL80211_CMD_REMAIN_ON_CHANNEL:
2354 mlme_event_remain_on_channel(drv, 0, tb);
2355 break;
2356 case NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL:
2357 mlme_event_remain_on_channel(drv, 1, tb);
2358 break;
2359 case NL80211_CMD_NOTIFY_CQM:
2360 nl80211_cqm_event(drv, tb);
2361 break;
2362 case NL80211_CMD_REG_CHANGE:
2363 wpa_printf(MSG_DEBUG, "nl80211: Regulatory domain change");
2364 wpa_supplicant_event(drv->ctx, EVENT_CHANNEL_LIST_CHANGED,
2365 NULL);
2366 break;
2367 case NL80211_CMD_REG_BEACON_HINT:
2368 wpa_printf(MSG_DEBUG, "nl80211: Regulatory beacon hint");
2369 wpa_supplicant_event(drv->ctx, EVENT_CHANNEL_LIST_CHANGED,
2370 NULL);
2371 break;
2372 case NL80211_CMD_NEW_STATION:
2373 nl80211_new_station_event(drv, tb);
2374 break;
2375 case NL80211_CMD_DEL_STATION:
2376 nl80211_del_station_event(drv, tb);
2377 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002378 case NL80211_CMD_SET_REKEY_OFFLOAD:
2379 nl80211_rekey_offload_event(drv, tb);
2380 break;
2381 case NL80211_CMD_PMKSA_CANDIDATE:
2382 nl80211_pmksa_candidate_event(drv, tb);
2383 break;
2384 case NL80211_CMD_PROBE_CLIENT:
2385 nl80211_client_probe_event(drv, tb);
2386 break;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002387 case NL80211_CMD_TDLS_OPER:
2388 nl80211_tdls_oper_event(drv, tb);
2389 break;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002390 case NL80211_CMD_CONN_FAILED:
2391 nl80211_connect_failed_event(drv, tb);
2392 break;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002393 case NL80211_CMD_FT_EVENT:
2394 mlme_event_ft_event(drv, tb);
2395 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002396 default:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002397 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Ignored unknown event "
2398 "(cmd=%d)", cmd);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002399 break;
2400 }
2401}
2402
2403
2404static int process_drv_event(struct nl_msg *msg, void *arg)
2405{
2406 struct wpa_driver_nl80211_data *drv = arg;
2407 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2408 struct nlattr *tb[NL80211_ATTR_MAX + 1];
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002409 struct i802_bss *bss;
2410 int ifidx = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002411
2412 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2413 genlmsg_attrlen(gnlh, 0), NULL);
2414
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002415 if (tb[NL80211_ATTR_IFINDEX])
2416 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
2417
2418 for (bss = &drv->first_bss; bss; bss = bss->next) {
2419 if (ifidx == -1 || ifidx == bss->ifindex) {
2420 do_process_drv_event(bss, gnlh->cmd, tb);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002421 return NL_SKIP;
2422 }
2423 }
2424
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002425 wpa_printf(MSG_DEBUG, "nl80211: Ignored event (cmd=%d) for foreign "
2426 "interface (ifindex %d)", gnlh->cmd, ifidx);
2427
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002428 return NL_SKIP;
2429}
2430
2431
2432static int process_global_event(struct nl_msg *msg, void *arg)
2433{
2434 struct nl80211_global *global = arg;
2435 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2436 struct nlattr *tb[NL80211_ATTR_MAX + 1];
Dmitry Shmidt04949592012-07-19 12:16:46 -07002437 struct wpa_driver_nl80211_data *drv, *tmp;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002438 int ifidx = -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002439 struct i802_bss *bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002440
2441 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2442 genlmsg_attrlen(gnlh, 0), NULL);
2443
2444 if (tb[NL80211_ATTR_IFINDEX])
2445 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
2446
Dmitry Shmidt04949592012-07-19 12:16:46 -07002447 dl_list_for_each_safe(drv, tmp, &global->interfaces,
2448 struct wpa_driver_nl80211_data, list) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002449 for (bss = &drv->first_bss; bss; bss = bss->next) {
2450 if (ifidx == -1 || ifidx == bss->ifindex) {
2451 do_process_drv_event(bss, gnlh->cmd, tb);
2452 return NL_SKIP;
2453 }
2454 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002455 }
2456
2457 return NL_SKIP;
2458}
2459
2460
2461static int process_bss_event(struct nl_msg *msg, void *arg)
2462{
2463 struct i802_bss *bss = arg;
2464 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2465 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2466
2467 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2468 genlmsg_attrlen(gnlh, 0), NULL);
2469
2470 switch (gnlh->cmd) {
2471 case NL80211_CMD_FRAME:
2472 case NL80211_CMD_FRAME_TX_STATUS:
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002473 mlme_event(bss, gnlh->cmd, tb[NL80211_ATTR_FRAME],
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002474 tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT],
2475 tb[NL80211_ATTR_WIPHY_FREQ], tb[NL80211_ATTR_ACK],
Dmitry Shmidt04949592012-07-19 12:16:46 -07002476 tb[NL80211_ATTR_COOKIE],
2477 tb[NL80211_ATTR_RX_SIGNAL_DBM]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002478 break;
2479 case NL80211_CMD_UNEXPECTED_FRAME:
2480 nl80211_spurious_frame(bss, tb, 0);
2481 break;
2482 case NL80211_CMD_UNEXPECTED_4ADDR_FRAME:
2483 nl80211_spurious_frame(bss, tb, 1);
2484 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002485 default:
2486 wpa_printf(MSG_DEBUG, "nl80211: Ignored unknown event "
2487 "(cmd=%d)", gnlh->cmd);
2488 break;
2489 }
2490
2491 return NL_SKIP;
2492}
2493
2494
2495static void wpa_driver_nl80211_event_receive(int sock, void *eloop_ctx,
2496 void *handle)
2497{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002498 struct nl_cb *cb = eloop_ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002499
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002500 wpa_printf(MSG_MSGDUMP, "nl80211: Event message available");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002501
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002502 nl_recvmsgs(handle, cb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002503}
2504
2505
2506/**
2507 * wpa_driver_nl80211_set_country - ask nl80211 to set the regulatory domain
2508 * @priv: driver_nl80211 private data
2509 * @alpha2_arg: country to which to switch to
2510 * Returns: 0 on success, -1 on failure
2511 *
2512 * This asks nl80211 to set the regulatory domain for given
2513 * country ISO / IEC alpha2.
2514 */
2515static int wpa_driver_nl80211_set_country(void *priv, const char *alpha2_arg)
2516{
2517 struct i802_bss *bss = priv;
2518 struct wpa_driver_nl80211_data *drv = bss->drv;
2519 char alpha2[3];
2520 struct nl_msg *msg;
2521
2522 msg = nlmsg_alloc();
2523 if (!msg)
2524 return -ENOMEM;
2525
2526 alpha2[0] = alpha2_arg[0];
2527 alpha2[1] = alpha2_arg[1];
2528 alpha2[2] = '\0';
2529
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002530 nl80211_cmd(drv, msg, 0, NL80211_CMD_REQ_SET_REG);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002531
2532 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, alpha2);
2533 if (send_and_recv_msgs(drv, msg, NULL, NULL))
2534 return -EINVAL;
2535 return 0;
2536nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002537 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002538 return -EINVAL;
2539}
2540
2541
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002542static int protocol_feature_handler(struct nl_msg *msg, void *arg)
2543{
2544 u32 *feat = arg;
2545 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
2546 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2547
2548 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2549 genlmsg_attrlen(gnlh, 0), NULL);
2550
2551 if (tb_msg[NL80211_ATTR_PROTOCOL_FEATURES])
2552 *feat = nla_get_u32(tb_msg[NL80211_ATTR_PROTOCOL_FEATURES]);
2553
2554 return NL_SKIP;
2555}
2556
2557
2558static u32 get_nl80211_protocol_features(struct wpa_driver_nl80211_data *drv)
2559{
2560 u32 feat = 0;
2561 struct nl_msg *msg;
2562
2563 msg = nlmsg_alloc();
2564 if (!msg)
2565 goto nla_put_failure;
2566
2567 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_PROTOCOL_FEATURES);
2568 if (send_and_recv_msgs(drv, msg, protocol_feature_handler, &feat) == 0)
2569 return feat;
2570
2571 msg = NULL;
2572nla_put_failure:
2573 nlmsg_free(msg);
2574 return 0;
2575}
2576
2577
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002578struct wiphy_info_data {
Dmitry Shmidt444d5672013-04-01 13:08:44 -07002579 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002580 struct wpa_driver_capa *capa;
2581
2582 unsigned int error:1;
2583 unsigned int device_ap_sme:1;
2584 unsigned int poll_command_supported:1;
2585 unsigned int data_tx_status:1;
2586 unsigned int monitor_supported:1;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002587 unsigned int auth_supported:1;
2588 unsigned int connect_supported:1;
2589 unsigned int p2p_go_supported:1;
2590 unsigned int p2p_client_supported:1;
2591 unsigned int p2p_concurrent:1;
2592 unsigned int p2p_multichan_concurrent:1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002593};
2594
2595
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002596static unsigned int probe_resp_offload_support(int supp_protocols)
2597{
2598 unsigned int prot = 0;
2599
2600 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS)
2601 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS;
2602 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2)
2603 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS2;
2604 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P)
2605 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_P2P;
2606 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_80211U)
2607 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_INTERWORKING;
2608
2609 return prot;
2610}
2611
2612
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002613static void wiphy_info_supported_iftypes(struct wiphy_info_data *info,
2614 struct nlattr *tb)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002615{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002616 struct nlattr *nl_mode;
2617 int i;
2618
2619 if (tb == NULL)
2620 return;
2621
2622 nla_for_each_nested(nl_mode, tb, i) {
2623 switch (nla_type(nl_mode)) {
2624 case NL80211_IFTYPE_AP:
2625 info->capa->flags |= WPA_DRIVER_FLAGS_AP;
2626 break;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002627 case NL80211_IFTYPE_ADHOC:
2628 info->capa->flags |= WPA_DRIVER_FLAGS_IBSS;
2629 break;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002630 case NL80211_IFTYPE_P2P_GO:
2631 info->p2p_go_supported = 1;
2632 break;
2633 case NL80211_IFTYPE_P2P_CLIENT:
2634 info->p2p_client_supported = 1;
2635 break;
2636 case NL80211_IFTYPE_MONITOR:
2637 info->monitor_supported = 1;
2638 break;
2639 }
2640 }
2641}
2642
2643
2644static int wiphy_info_iface_comb_process(struct wiphy_info_data *info,
2645 struct nlattr *nl_combi)
2646{
2647 struct nlattr *tb_comb[NUM_NL80211_IFACE_COMB];
2648 struct nlattr *tb_limit[NUM_NL80211_IFACE_LIMIT];
2649 struct nlattr *nl_limit, *nl_mode;
2650 int err, rem_limit, rem_mode;
2651 int combination_has_p2p = 0, combination_has_mgd = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002652 static struct nla_policy
2653 iface_combination_policy[NUM_NL80211_IFACE_COMB] = {
2654 [NL80211_IFACE_COMB_LIMITS] = { .type = NLA_NESTED },
2655 [NL80211_IFACE_COMB_MAXNUM] = { .type = NLA_U32 },
2656 [NL80211_IFACE_COMB_STA_AP_BI_MATCH] = { .type = NLA_FLAG },
2657 [NL80211_IFACE_COMB_NUM_CHANNELS] = { .type = NLA_U32 },
2658 },
2659 iface_limit_policy[NUM_NL80211_IFACE_LIMIT] = {
2660 [NL80211_IFACE_LIMIT_TYPES] = { .type = NLA_NESTED },
2661 [NL80211_IFACE_LIMIT_MAX] = { .type = NLA_U32 },
2662 };
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002663
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002664 err = nla_parse_nested(tb_comb, MAX_NL80211_IFACE_COMB,
2665 nl_combi, iface_combination_policy);
2666 if (err || !tb_comb[NL80211_IFACE_COMB_LIMITS] ||
2667 !tb_comb[NL80211_IFACE_COMB_MAXNUM] ||
2668 !tb_comb[NL80211_IFACE_COMB_NUM_CHANNELS])
2669 return 0; /* broken combination */
2670
2671 nla_for_each_nested(nl_limit, tb_comb[NL80211_IFACE_COMB_LIMITS],
2672 rem_limit) {
2673 err = nla_parse_nested(tb_limit, MAX_NL80211_IFACE_LIMIT,
2674 nl_limit, iface_limit_policy);
2675 if (err || !tb_limit[NL80211_IFACE_LIMIT_TYPES])
2676 return 0; /* broken combination */
2677
2678 nla_for_each_nested(nl_mode,
2679 tb_limit[NL80211_IFACE_LIMIT_TYPES],
2680 rem_mode) {
2681 int ift = nla_type(nl_mode);
2682 if (ift == NL80211_IFTYPE_P2P_GO ||
2683 ift == NL80211_IFTYPE_P2P_CLIENT)
2684 combination_has_p2p = 1;
2685 if (ift == NL80211_IFTYPE_STATION)
2686 combination_has_mgd = 1;
2687 }
2688 if (combination_has_p2p && combination_has_mgd)
2689 break;
2690 }
2691
2692 if (combination_has_p2p && combination_has_mgd) {
2693 info->p2p_concurrent = 1;
2694 if (nla_get_u32(tb_comb[NL80211_IFACE_COMB_NUM_CHANNELS]) > 1)
2695 info->p2p_multichan_concurrent = 1;
2696 return 1;
2697 }
2698
2699 return 0;
2700}
2701
2702
2703static void wiphy_info_iface_comb(struct wiphy_info_data *info,
2704 struct nlattr *tb)
2705{
2706 struct nlattr *nl_combi;
2707 int rem_combi;
2708
2709 if (tb == NULL)
2710 return;
2711
2712 nla_for_each_nested(nl_combi, tb, rem_combi) {
2713 if (wiphy_info_iface_comb_process(info, nl_combi) > 0)
2714 break;
2715 }
2716}
2717
2718
2719static void wiphy_info_supp_cmds(struct wiphy_info_data *info,
2720 struct nlattr *tb)
2721{
2722 struct nlattr *nl_cmd;
2723 int i;
2724
2725 if (tb == NULL)
2726 return;
2727
2728 nla_for_each_nested(nl_cmd, tb, i) {
2729 switch (nla_get_u32(nl_cmd)) {
2730 case NL80211_CMD_AUTHENTICATE:
2731 info->auth_supported = 1;
2732 break;
2733 case NL80211_CMD_CONNECT:
2734 info->connect_supported = 1;
2735 break;
2736 case NL80211_CMD_START_SCHED_SCAN:
2737 info->capa->sched_scan_supported = 1;
2738 break;
2739 case NL80211_CMD_PROBE_CLIENT:
2740 info->poll_command_supported = 1;
2741 break;
2742 }
2743 }
2744}
2745
2746
2747static void wiphy_info_max_roc(struct wpa_driver_capa *capa,
2748 struct nlattr *tb)
2749{
2750 /* default to 5000 since early versions of mac80211 don't set it */
2751 capa->max_remain_on_chan = 5000;
2752
2753 if (tb)
2754 capa->max_remain_on_chan = nla_get_u32(tb);
2755}
2756
2757
2758static void wiphy_info_tdls(struct wpa_driver_capa *capa, struct nlattr *tdls,
2759 struct nlattr *ext_setup)
2760{
2761 if (tdls == NULL)
2762 return;
2763
2764 wpa_printf(MSG_DEBUG, "nl80211: TDLS supported");
2765 capa->flags |= WPA_DRIVER_FLAGS_TDLS_SUPPORT;
2766
2767 if (ext_setup) {
2768 wpa_printf(MSG_DEBUG, "nl80211: TDLS external setup");
2769 capa->flags |= WPA_DRIVER_FLAGS_TDLS_EXTERNAL_SETUP;
2770 }
2771}
2772
2773
2774static void wiphy_info_feature_flags(struct wiphy_info_data *info,
2775 struct nlattr *tb)
2776{
2777 u32 flags;
2778 struct wpa_driver_capa *capa = info->capa;
2779
2780 if (tb == NULL)
2781 return;
2782
2783 flags = nla_get_u32(tb);
2784
2785 if (flags & NL80211_FEATURE_SK_TX_STATUS)
2786 info->data_tx_status = 1;
2787
2788 if (flags & NL80211_FEATURE_INACTIVITY_TIMER)
2789 capa->flags |= WPA_DRIVER_FLAGS_INACTIVITY_TIMER;
2790
2791 if (flags & NL80211_FEATURE_SAE)
2792 capa->flags |= WPA_DRIVER_FLAGS_SAE;
2793
2794 if (flags & NL80211_FEATURE_NEED_OBSS_SCAN)
2795 capa->flags |= WPA_DRIVER_FLAGS_OBSS_SCAN;
2796}
2797
2798
2799static void wiphy_info_probe_resp_offload(struct wpa_driver_capa *capa,
2800 struct nlattr *tb)
2801{
2802 u32 protocols;
2803
2804 if (tb == NULL)
2805 return;
2806
2807 protocols = nla_get_u32(tb);
2808 wpa_printf(MSG_DEBUG, "nl80211: Supports Probe Response offload in AP "
2809 "mode");
2810 capa->flags |= WPA_DRIVER_FLAGS_PROBE_RESP_OFFLOAD;
2811 capa->probe_resp_offloads = probe_resp_offload_support(protocols);
2812}
2813
2814
2815static int wiphy_info_handler(struct nl_msg *msg, void *arg)
2816{
2817 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2818 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2819 struct wiphy_info_data *info = arg;
2820 struct wpa_driver_capa *capa = info->capa;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07002821 struct wpa_driver_nl80211_data *drv = info->drv;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002822
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002823 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2824 genlmsg_attrlen(gnlh, 0), NULL);
2825
2826 if (tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS])
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002827 capa->max_scan_ssids =
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002828 nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS]);
2829
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002830 if (tb[NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS])
2831 capa->max_sched_scan_ssids =
2832 nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS]);
2833
2834 if (tb[NL80211_ATTR_MAX_MATCH_SETS])
2835 capa->max_match_sets =
2836 nla_get_u8(tb[NL80211_ATTR_MAX_MATCH_SETS]);
2837
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002838 wiphy_info_supported_iftypes(info, tb[NL80211_ATTR_SUPPORTED_IFTYPES]);
2839 wiphy_info_iface_comb(info, tb[NL80211_ATTR_INTERFACE_COMBINATIONS]);
2840 wiphy_info_supp_cmds(info, tb[NL80211_ATTR_SUPPORTED_COMMANDS]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002841
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002842 if (tb[NL80211_ATTR_OFFCHANNEL_TX_OK]) {
2843 wpa_printf(MSG_DEBUG, "nl80211: Using driver-based "
2844 "off-channel TX");
2845 capa->flags |= WPA_DRIVER_FLAGS_OFFCHANNEL_TX;
2846 }
2847
2848 if (tb[NL80211_ATTR_ROAM_SUPPORT]) {
2849 wpa_printf(MSG_DEBUG, "nl80211: Using driver-based roaming");
2850 capa->flags |= WPA_DRIVER_FLAGS_BSS_SELECTION;
2851 }
2852
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002853 wiphy_info_max_roc(capa,
2854 tb[NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002855
2856 if (tb[NL80211_ATTR_SUPPORT_AP_UAPSD])
2857 capa->flags |= WPA_DRIVER_FLAGS_AP_UAPSD;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002858
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002859 wiphy_info_tdls(capa, tb[NL80211_ATTR_TDLS_SUPPORT],
2860 tb[NL80211_ATTR_TDLS_EXTERNAL_SETUP]);
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07002861
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002862 if (tb[NL80211_ATTR_DEVICE_AP_SME])
2863 info->device_ap_sme = 1;
2864
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002865 wiphy_info_feature_flags(info, tb[NL80211_ATTR_FEATURE_FLAGS]);
2866 wiphy_info_probe_resp_offload(capa,
2867 tb[NL80211_ATTR_PROBE_RESP_OFFLOAD]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002868
Dmitry Shmidt444d5672013-04-01 13:08:44 -07002869 if (tb[NL80211_ATTR_EXT_CAPA] && tb[NL80211_ATTR_EXT_CAPA_MASK] &&
2870 drv->extended_capa == NULL) {
2871 drv->extended_capa =
2872 os_malloc(nla_len(tb[NL80211_ATTR_EXT_CAPA]));
2873 if (drv->extended_capa) {
2874 os_memcpy(drv->extended_capa,
2875 nla_data(tb[NL80211_ATTR_EXT_CAPA]),
2876 nla_len(tb[NL80211_ATTR_EXT_CAPA]));
2877 drv->extended_capa_len =
2878 nla_len(tb[NL80211_ATTR_EXT_CAPA]);
2879 }
2880 drv->extended_capa_mask =
2881 os_malloc(nla_len(tb[NL80211_ATTR_EXT_CAPA]));
2882 if (drv->extended_capa_mask) {
2883 os_memcpy(drv->extended_capa_mask,
2884 nla_data(tb[NL80211_ATTR_EXT_CAPA]),
2885 nla_len(tb[NL80211_ATTR_EXT_CAPA]));
2886 } else {
2887 os_free(drv->extended_capa);
2888 drv->extended_capa = NULL;
2889 drv->extended_capa_len = 0;
2890 }
2891 }
2892
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002893 return NL_SKIP;
2894}
2895
2896
2897static int wpa_driver_nl80211_get_info(struct wpa_driver_nl80211_data *drv,
2898 struct wiphy_info_data *info)
2899{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002900 u32 feat;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002901 struct nl_msg *msg;
2902
2903 os_memset(info, 0, sizeof(*info));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002904 info->capa = &drv->capa;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07002905 info->drv = drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002906
2907 msg = nlmsg_alloc();
2908 if (!msg)
2909 return -1;
2910
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002911 feat = get_nl80211_protocol_features(drv);
2912 if (feat & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP)
2913 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_WIPHY);
2914 else
2915 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002916
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002917 NLA_PUT_FLAG(msg, NL80211_ATTR_SPLIT_WIPHY_DUMP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002918 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->first_bss.ifindex);
2919
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002920 if (send_and_recv_msgs(drv, msg, wiphy_info_handler, info))
2921 return -1;
2922
2923 if (info->auth_supported)
2924 drv->capa.flags |= WPA_DRIVER_FLAGS_SME;
2925 else if (!info->connect_supported) {
2926 wpa_printf(MSG_INFO, "nl80211: Driver does not support "
2927 "authentication/association or connect commands");
2928 info->error = 1;
2929 }
2930
2931 if (info->p2p_go_supported && info->p2p_client_supported)
2932 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CAPABLE;
2933 if (info->p2p_concurrent) {
2934 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
2935 "interface (driver advertised support)");
2936 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
2937 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
2938 }
2939 if (info->p2p_multichan_concurrent) {
2940 wpa_printf(MSG_DEBUG, "nl80211: Enable multi-channel "
2941 "concurrent (driver advertised support)");
2942 drv->capa.flags |= WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT;
2943 }
2944 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002945nla_put_failure:
2946 nlmsg_free(msg);
2947 return -1;
2948}
2949
2950
2951static int wpa_driver_nl80211_capa(struct wpa_driver_nl80211_data *drv)
2952{
2953 struct wiphy_info_data info;
2954 if (wpa_driver_nl80211_get_info(drv, &info))
2955 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002956
2957 if (info.error)
2958 return -1;
2959
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002960 drv->has_capability = 1;
2961 /* For now, assume TKIP, CCMP, WPA, WPA2 are supported */
2962 drv->capa.key_mgmt = WPA_DRIVER_CAPA_KEY_MGMT_WPA |
2963 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
2964 WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
2965 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK;
2966 drv->capa.enc = WPA_DRIVER_CAPA_ENC_WEP40 |
2967 WPA_DRIVER_CAPA_ENC_WEP104 |
2968 WPA_DRIVER_CAPA_ENC_TKIP |
2969 WPA_DRIVER_CAPA_ENC_CCMP;
2970 drv->capa.auth = WPA_DRIVER_AUTH_OPEN |
2971 WPA_DRIVER_AUTH_SHARED |
2972 WPA_DRIVER_AUTH_LEAP;
2973
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002974 drv->capa.flags |= WPA_DRIVER_FLAGS_SANE_ERROR_CODES;
2975 drv->capa.flags |= WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002976 drv->capa.flags |= WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07002977
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002978 if (!info.device_ap_sme) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002979 drv->capa.flags |= WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002980
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002981 /*
2982 * No AP SME is currently assumed to also indicate no AP MLME
2983 * in the driver/firmware.
2984 */
2985 drv->capa.flags |= WPA_DRIVER_FLAGS_AP_MLME;
2986 }
2987
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002988 drv->device_ap_sme = info.device_ap_sme;
2989 drv->poll_command_supported = info.poll_command_supported;
2990 drv->data_tx_status = info.data_tx_status;
2991
Dmitry Shmidt04949592012-07-19 12:16:46 -07002992#ifdef ANDROID_P2P
2993 if(drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) {
2994 /* Driver is new enough to support monitorless mode*/
2995 wpa_printf(MSG_DEBUG, "nl80211: Driver is new "
2996 "enough to support monitor-less mode");
2997 drv->use_monitor = 0;
2998 }
2999#else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003000 /*
Dmitry Shmidtaa532512012-09-24 10:35:31 -07003001 * If poll command and tx status are supported, mac80211 is new enough
3002 * to have everything we need to not need monitor interfaces.
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003003 */
Dmitry Shmidtaa532512012-09-24 10:35:31 -07003004 drv->use_monitor = !info.poll_command_supported || !info.data_tx_status;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003005#endif
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003006
3007 if (drv->device_ap_sme && drv->use_monitor) {
3008 /*
3009 * Non-mac80211 drivers may not support monitor interface.
3010 * Make sure we do not get stuck with incorrect capability here
3011 * by explicitly testing this.
3012 */
3013 if (!info.monitor_supported) {
3014 wpa_printf(MSG_DEBUG, "nl80211: Disable use_monitor "
3015 "with device_ap_sme since no monitor mode "
3016 "support detected");
3017 drv->use_monitor = 0;
3018 }
3019 }
3020
3021 /*
3022 * If we aren't going to use monitor interfaces, but the
3023 * driver doesn't support data TX status, we won't get TX
3024 * status for EAPOL frames.
3025 */
3026 if (!drv->use_monitor && !info.data_tx_status)
3027 drv->capa.flags &= ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003028
3029 return 0;
3030}
3031
3032
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003033#ifdef ANDROID
3034static int android_genl_ctrl_resolve(struct nl_handle *handle,
3035 const char *name)
3036{
3037 /*
3038 * Android ICS has very minimal genl_ctrl_resolve() implementation, so
3039 * need to work around that.
3040 */
3041 struct nl_cache *cache = NULL;
3042 struct genl_family *nl80211 = NULL;
3043 int id = -1;
3044
3045 if (genl_ctrl_alloc_cache(handle, &cache) < 0) {
3046 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic "
3047 "netlink cache");
3048 goto fail;
3049 }
3050
3051 nl80211 = genl_ctrl_search_by_name(cache, name);
3052 if (nl80211 == NULL)
3053 goto fail;
3054
3055 id = genl_family_get_id(nl80211);
3056
3057fail:
3058 if (nl80211)
3059 genl_family_put(nl80211);
3060 if (cache)
3061 nl_cache_free(cache);
3062
3063 return id;
3064}
3065#define genl_ctrl_resolve android_genl_ctrl_resolve
3066#endif /* ANDROID */
3067
3068
3069static int wpa_driver_nl80211_init_nl_global(struct nl80211_global *global)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003070{
3071 int ret;
3072
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003073 global->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
3074 if (global->nl_cb == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003075 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
3076 "callbacks");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003077 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003078 }
3079
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003080 global->nl = nl_create_handle(global->nl_cb, "nl");
3081 if (global->nl == NULL)
3082 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003083
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003084 global->nl80211_id = genl_ctrl_resolve(global->nl, "nl80211");
3085 if (global->nl80211_id < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003086 wpa_printf(MSG_ERROR, "nl80211: 'nl80211' generic netlink not "
3087 "found");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003088 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003089 }
3090
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003091 global->nl_event = nl_create_handle(global->nl_cb, "event");
3092 if (global->nl_event == NULL)
3093 goto err;
3094
3095 ret = nl_get_multicast_id(global, "nl80211", "scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003096 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003097 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003098 if (ret < 0) {
3099 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
3100 "membership for scan events: %d (%s)",
3101 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003102 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003103 }
3104
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003105 ret = nl_get_multicast_id(global, "nl80211", "mlme");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003106 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003107 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003108 if (ret < 0) {
3109 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
3110 "membership for mlme events: %d (%s)",
3111 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003112 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003113 }
3114
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003115 ret = nl_get_multicast_id(global, "nl80211", "regulatory");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003116 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003117 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003118 if (ret < 0) {
3119 wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
3120 "membership for regulatory events: %d (%s)",
3121 ret, strerror(-ret));
3122 /* Continue without regulatory events */
3123 }
3124
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003125 nl_cb_set(global->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
3126 no_seq_check, NULL);
3127 nl_cb_set(global->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
3128 process_global_event, global);
3129
3130 eloop_register_read_sock(nl_socket_get_fd(global->nl_event),
3131 wpa_driver_nl80211_event_receive,
3132 global->nl_cb, global->nl_event);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003133
3134 return 0;
3135
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003136err:
3137 nl_destroy_handles(&global->nl_event);
3138 nl_destroy_handles(&global->nl);
3139 nl_cb_put(global->nl_cb);
3140 global->nl_cb = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003141 return -1;
3142}
3143
3144
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003145static int wpa_driver_nl80211_init_nl(struct wpa_driver_nl80211_data *drv)
3146{
3147 drv->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
3148 if (!drv->nl_cb) {
3149 wpa_printf(MSG_ERROR, "nl80211: Failed to alloc cb struct");
3150 return -1;
3151 }
3152
3153 nl_cb_set(drv->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
3154 no_seq_check, NULL);
3155 nl_cb_set(drv->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
3156 process_drv_event, drv);
3157
3158 return 0;
3159}
3160
3161
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003162static void wpa_driver_nl80211_rfkill_blocked(void *ctx)
3163{
3164 wpa_printf(MSG_DEBUG, "nl80211: RFKILL blocked");
3165 /*
3166 * This may be for any interface; use ifdown event to disable
3167 * interface.
3168 */
3169}
3170
3171
3172static void wpa_driver_nl80211_rfkill_unblocked(void *ctx)
3173{
3174 struct wpa_driver_nl80211_data *drv = ctx;
3175 wpa_printf(MSG_DEBUG, "nl80211: RFKILL unblocked");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003176 if (linux_set_iface_flags(drv->global->ioctl_sock,
3177 drv->first_bss.ifname, 1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003178 wpa_printf(MSG_DEBUG, "nl80211: Could not set interface UP "
3179 "after rfkill unblock");
3180 return;
3181 }
3182 /* rtnetlink ifup handler will report interface as enabled */
3183}
3184
3185
3186static void nl80211_get_phy_name(struct wpa_driver_nl80211_data *drv)
3187{
3188 /* Find phy (radio) to which this interface belongs */
3189 char buf[90], *pos;
3190 int f, rv;
3191
3192 drv->phyname[0] = '\0';
3193 snprintf(buf, sizeof(buf) - 1, "/sys/class/net/%s/phy80211/name",
3194 drv->first_bss.ifname);
3195 f = open(buf, O_RDONLY);
3196 if (f < 0) {
3197 wpa_printf(MSG_DEBUG, "Could not open file %s: %s",
3198 buf, strerror(errno));
3199 return;
3200 }
3201
3202 rv = read(f, drv->phyname, sizeof(drv->phyname) - 1);
3203 close(f);
3204 if (rv < 0) {
3205 wpa_printf(MSG_DEBUG, "Could not read file %s: %s",
3206 buf, strerror(errno));
3207 return;
3208 }
3209
3210 drv->phyname[rv] = '\0';
3211 pos = os_strchr(drv->phyname, '\n');
3212 if (pos)
3213 *pos = '\0';
3214 wpa_printf(MSG_DEBUG, "nl80211: interface %s in phy %s",
3215 drv->first_bss.ifname, drv->phyname);
3216}
3217
3218
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003219static void wpa_driver_nl80211_handle_eapol_tx_status(int sock,
3220 void *eloop_ctx,
3221 void *handle)
3222{
3223 struct wpa_driver_nl80211_data *drv = eloop_ctx;
3224 u8 data[2048];
3225 struct msghdr msg;
3226 struct iovec entry;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003227 u8 control[512];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003228 struct cmsghdr *cmsg;
3229 int res, found_ee = 0, found_wifi = 0, acked = 0;
3230 union wpa_event_data event;
3231
3232 memset(&msg, 0, sizeof(msg));
3233 msg.msg_iov = &entry;
3234 msg.msg_iovlen = 1;
3235 entry.iov_base = data;
3236 entry.iov_len = sizeof(data);
3237 msg.msg_control = &control;
3238 msg.msg_controllen = sizeof(control);
3239
3240 res = recvmsg(sock, &msg, MSG_ERRQUEUE);
3241 /* if error or not fitting 802.3 header, return */
3242 if (res < 14)
3243 return;
3244
3245 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg))
3246 {
3247 if (cmsg->cmsg_level == SOL_SOCKET &&
3248 cmsg->cmsg_type == SCM_WIFI_STATUS) {
3249 int *ack;
3250
3251 found_wifi = 1;
3252 ack = (void *)CMSG_DATA(cmsg);
3253 acked = *ack;
3254 }
3255
3256 if (cmsg->cmsg_level == SOL_PACKET &&
3257 cmsg->cmsg_type == PACKET_TX_TIMESTAMP) {
3258 struct sock_extended_err *err =
3259 (struct sock_extended_err *)CMSG_DATA(cmsg);
3260
3261 if (err->ee_origin == SO_EE_ORIGIN_TXSTATUS)
3262 found_ee = 1;
3263 }
3264 }
3265
3266 if (!found_ee || !found_wifi)
3267 return;
3268
3269 memset(&event, 0, sizeof(event));
3270 event.eapol_tx_status.dst = data;
3271 event.eapol_tx_status.data = data + 14;
3272 event.eapol_tx_status.data_len = res - 14;
3273 event.eapol_tx_status.ack = acked;
3274 wpa_supplicant_event(drv->ctx, EVENT_EAPOL_TX_STATUS, &event);
3275}
3276
3277
3278static int nl80211_init_bss(struct i802_bss *bss)
3279{
3280 bss->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
3281 if (!bss->nl_cb)
3282 return -1;
3283
3284 nl_cb_set(bss->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
3285 no_seq_check, NULL);
3286 nl_cb_set(bss->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
3287 process_bss_event, bss);
3288
3289 return 0;
3290}
3291
3292
3293static void nl80211_destroy_bss(struct i802_bss *bss)
3294{
3295 nl_cb_put(bss->nl_cb);
3296 bss->nl_cb = NULL;
3297}
3298
3299
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003300/**
3301 * wpa_driver_nl80211_init - Initialize nl80211 driver interface
3302 * @ctx: context to be used when calling wpa_supplicant functions,
3303 * e.g., wpa_supplicant_event()
3304 * @ifname: interface name, e.g., wlan0
3305 * @global_priv: private driver global data from global_init()
3306 * Returns: Pointer to private data, %NULL on failure
3307 */
3308static void * wpa_driver_nl80211_init(void *ctx, const char *ifname,
3309 void *global_priv)
3310{
3311 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003312 struct rfkill_config *rcfg;
3313 struct i802_bss *bss;
3314
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003315 if (global_priv == NULL)
3316 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003317 drv = os_zalloc(sizeof(*drv));
3318 if (drv == NULL)
3319 return NULL;
3320 drv->global = global_priv;
3321 drv->ctx = ctx;
3322 bss = &drv->first_bss;
3323 bss->drv = drv;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003324 bss->ctx = ctx;
3325
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003326 os_strlcpy(bss->ifname, ifname, sizeof(bss->ifname));
3327 drv->monitor_ifidx = -1;
3328 drv->monitor_sock = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003329 drv->eapol_tx_sock = -1;
3330 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003331
3332 if (wpa_driver_nl80211_init_nl(drv)) {
3333 os_free(drv);
3334 return NULL;
3335 }
3336
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003337 if (nl80211_init_bss(bss))
3338 goto failed;
3339
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003340 nl80211_get_phy_name(drv);
3341
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003342 rcfg = os_zalloc(sizeof(*rcfg));
3343 if (rcfg == NULL)
3344 goto failed;
3345 rcfg->ctx = drv;
3346 os_strlcpy(rcfg->ifname, ifname, sizeof(rcfg->ifname));
3347 rcfg->blocked_cb = wpa_driver_nl80211_rfkill_blocked;
3348 rcfg->unblocked_cb = wpa_driver_nl80211_rfkill_unblocked;
3349 drv->rfkill = rfkill_init(rcfg);
3350 if (drv->rfkill == NULL) {
3351 wpa_printf(MSG_DEBUG, "nl80211: RFKILL status not available");
3352 os_free(rcfg);
3353 }
3354
3355 if (wpa_driver_nl80211_finish_drv_init(drv))
3356 goto failed;
3357
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003358 drv->eapol_tx_sock = socket(PF_PACKET, SOCK_DGRAM, 0);
3359 if (drv->eapol_tx_sock < 0)
3360 goto failed;
3361
3362 if (drv->data_tx_status) {
3363 int enabled = 1;
3364
3365 if (setsockopt(drv->eapol_tx_sock, SOL_SOCKET, SO_WIFI_STATUS,
3366 &enabled, sizeof(enabled)) < 0) {
3367 wpa_printf(MSG_DEBUG,
3368 "nl80211: wifi status sockopt failed\n");
3369 drv->data_tx_status = 0;
3370 if (!drv->use_monitor)
3371 drv->capa.flags &=
3372 ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
3373 } else {
3374 eloop_register_read_sock(drv->eapol_tx_sock,
3375 wpa_driver_nl80211_handle_eapol_tx_status,
3376 drv, NULL);
3377 }
3378 }
3379
3380 if (drv->global) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003381 dl_list_add(&drv->global->interfaces, &drv->list);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003382 drv->in_interface_list = 1;
3383 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003384
3385 return bss;
3386
3387failed:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003388 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003389 return NULL;
3390}
3391
3392
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003393static int nl80211_register_frame(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003394 struct nl_handle *nl_handle,
3395 u16 type, const u8 *match, size_t match_len)
3396{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003397 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003398 struct nl_msg *msg;
3399 int ret = -1;
3400
3401 msg = nlmsg_alloc();
3402 if (!msg)
3403 return -1;
3404
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003405 wpa_printf(MSG_DEBUG, "nl80211: Register frame type=0x%x nl_handle=%p",
3406 type, nl_handle);
3407 wpa_hexdump(MSG_DEBUG, "nl80211: Register frame match",
3408 match, match_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003409
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003410 nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_ACTION);
3411
3412 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003413 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE, type);
3414 NLA_PUT(msg, NL80211_ATTR_FRAME_MATCH, match_len, match);
3415
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003416 ret = send_and_recv(drv->global, nl_handle, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003417 msg = NULL;
3418 if (ret) {
3419 wpa_printf(MSG_DEBUG, "nl80211: Register frame command "
3420 "failed (type=%u): ret=%d (%s)",
3421 type, ret, strerror(-ret));
3422 wpa_hexdump(MSG_DEBUG, "nl80211: Register frame match",
3423 match, match_len);
3424 goto nla_put_failure;
3425 }
3426 ret = 0;
3427nla_put_failure:
3428 nlmsg_free(msg);
3429 return ret;
3430}
3431
3432
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003433static int nl80211_alloc_mgmt_handle(struct i802_bss *bss)
3434{
3435 struct wpa_driver_nl80211_data *drv = bss->drv;
3436
3437 if (bss->nl_mgmt) {
3438 wpa_printf(MSG_DEBUG, "nl80211: Mgmt reporting "
3439 "already on! (nl_mgmt=%p)", bss->nl_mgmt);
3440 return -1;
3441 }
3442
3443 bss->nl_mgmt = nl_create_handle(drv->nl_cb, "mgmt");
3444 if (bss->nl_mgmt == NULL)
3445 return -1;
3446
3447 eloop_register_read_sock(nl_socket_get_fd(bss->nl_mgmt),
3448 wpa_driver_nl80211_event_receive, bss->nl_cb,
3449 bss->nl_mgmt);
3450
3451 return 0;
3452}
3453
3454
3455static int nl80211_register_action_frame(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003456 const u8 *match, size_t match_len)
3457{
3458 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_ACTION << 4);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003459 return nl80211_register_frame(bss, bss->nl_mgmt,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003460 type, match, match_len);
3461}
3462
3463
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003464static int nl80211_mgmt_subscribe_non_ap(struct i802_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003465{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003466 struct wpa_driver_nl80211_data *drv = bss->drv;
3467
3468 if (nl80211_alloc_mgmt_handle(bss))
3469 return -1;
3470 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with non-AP "
3471 "handle %p", bss->nl_mgmt);
3472
3473#if defined(CONFIG_P2P) || defined(CONFIG_INTERWORKING)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003474 /* GAS Initial Request */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003475 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0a", 2) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003476 return -1;
3477 /* GAS Initial Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003478 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0b", 2) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003479 return -1;
3480 /* GAS Comeback Request */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003481 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0c", 2) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003482 return -1;
3483 /* GAS Comeback Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003484 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0d", 2) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003485 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003486#endif /* CONFIG_P2P || CONFIG_INTERWORKING */
3487#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003488 /* P2P Public Action */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003489 if (nl80211_register_action_frame(bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003490 (u8 *) "\x04\x09\x50\x6f\x9a\x09",
3491 6) < 0)
3492 return -1;
3493 /* P2P Action */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003494 if (nl80211_register_action_frame(bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003495 (u8 *) "\x7f\x50\x6f\x9a\x09",
3496 5) < 0)
3497 return -1;
3498#endif /* CONFIG_P2P */
3499#ifdef CONFIG_IEEE80211W
3500 /* SA Query Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003501 if (nl80211_register_action_frame(bss, (u8 *) "\x08\x01", 2) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003502 return -1;
3503#endif /* CONFIG_IEEE80211W */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003504#ifdef CONFIG_TDLS
3505 if ((drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT)) {
3506 /* TDLS Discovery Response */
3507 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0e", 2) <
3508 0)
3509 return -1;
3510 }
3511#endif /* CONFIG_TDLS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003512
3513 /* FT Action frames */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003514 if (nl80211_register_action_frame(bss, (u8 *) "\x06", 1) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003515 return -1;
3516 else
3517 drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_FT |
3518 WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK;
3519
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003520 /* WNM - BSS Transition Management Request */
3521 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x07", 2) < 0)
3522 return -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003523 /* WNM-Sleep Mode Response */
3524 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x11", 2) < 0)
3525 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003526
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003527 return 0;
3528}
3529
3530
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003531static int nl80211_register_spurious_class3(struct i802_bss *bss)
3532{
3533 struct wpa_driver_nl80211_data *drv = bss->drv;
3534 struct nl_msg *msg;
3535 int ret = -1;
3536
3537 msg = nlmsg_alloc();
3538 if (!msg)
3539 return -1;
3540
3541 nl80211_cmd(drv, msg, 0, NL80211_CMD_UNEXPECTED_FRAME);
3542
3543 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
3544
3545 ret = send_and_recv(drv->global, bss->nl_mgmt, msg, NULL, NULL);
3546 msg = NULL;
3547 if (ret) {
3548 wpa_printf(MSG_DEBUG, "nl80211: Register spurious class3 "
3549 "failed: ret=%d (%s)",
3550 ret, strerror(-ret));
3551 goto nla_put_failure;
3552 }
3553 ret = 0;
3554nla_put_failure:
3555 nlmsg_free(msg);
3556 return ret;
3557}
3558
3559
3560static int nl80211_mgmt_subscribe_ap(struct i802_bss *bss)
3561{
3562 static const int stypes[] = {
3563 WLAN_FC_STYPE_AUTH,
3564 WLAN_FC_STYPE_ASSOC_REQ,
3565 WLAN_FC_STYPE_REASSOC_REQ,
3566 WLAN_FC_STYPE_DISASSOC,
3567 WLAN_FC_STYPE_DEAUTH,
3568 WLAN_FC_STYPE_ACTION,
3569 WLAN_FC_STYPE_PROBE_REQ,
3570/* Beacon doesn't work as mac80211 doesn't currently allow
3571 * it, but it wouldn't really be the right thing anyway as
3572 * it isn't per interface ... maybe just dump the scan
3573 * results periodically for OLBC?
3574 */
3575// WLAN_FC_STYPE_BEACON,
3576 };
3577 unsigned int i;
3578
3579 if (nl80211_alloc_mgmt_handle(bss))
3580 return -1;
3581 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
3582 "handle %p", bss->nl_mgmt);
3583
3584 for (i = 0; i < sizeof(stypes) / sizeof(stypes[0]); i++) {
3585 if (nl80211_register_frame(bss, bss->nl_mgmt,
3586 (WLAN_FC_TYPE_MGMT << 2) |
3587 (stypes[i] << 4),
3588 NULL, 0) < 0) {
3589 goto out_err;
3590 }
3591 }
3592
3593 if (nl80211_register_spurious_class3(bss))
3594 goto out_err;
3595
3596 if (nl80211_get_wiphy_data_ap(bss) == NULL)
3597 goto out_err;
3598
3599 return 0;
3600
3601out_err:
3602 eloop_unregister_read_sock(nl_socket_get_fd(bss->nl_mgmt));
3603 nl_destroy_handles(&bss->nl_mgmt);
3604 return -1;
3605}
3606
3607
3608static int nl80211_mgmt_subscribe_ap_dev_sme(struct i802_bss *bss)
3609{
3610 if (nl80211_alloc_mgmt_handle(bss))
3611 return -1;
3612 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
3613 "handle %p (device SME)", bss->nl_mgmt);
3614
3615 if (nl80211_register_frame(bss, bss->nl_mgmt,
3616 (WLAN_FC_TYPE_MGMT << 2) |
3617 (WLAN_FC_STYPE_ACTION << 4),
3618 NULL, 0) < 0)
3619 goto out_err;
3620
3621 return 0;
3622
3623out_err:
3624 eloop_unregister_read_sock(nl_socket_get_fd(bss->nl_mgmt));
3625 nl_destroy_handles(&bss->nl_mgmt);
3626 return -1;
3627}
3628
3629
3630static void nl80211_mgmt_unsubscribe(struct i802_bss *bss, const char *reason)
3631{
3632 if (bss->nl_mgmt == NULL)
3633 return;
3634 wpa_printf(MSG_DEBUG, "nl80211: Unsubscribe mgmt frames handle %p "
3635 "(%s)", bss->nl_mgmt, reason);
3636 eloop_unregister_read_sock(nl_socket_get_fd(bss->nl_mgmt));
3637 nl_destroy_handles(&bss->nl_mgmt);
3638
3639 nl80211_put_wiphy_data_ap(bss);
3640}
3641
3642
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003643static void wpa_driver_nl80211_send_rfkill(void *eloop_ctx, void *timeout_ctx)
3644{
3645 wpa_supplicant_event(timeout_ctx, EVENT_INTERFACE_DISABLED, NULL);
3646}
3647
3648
3649static int
3650wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv)
3651{
3652 struct i802_bss *bss = &drv->first_bss;
3653 int send_rfkill_event = 0;
3654
3655 drv->ifindex = if_nametoindex(bss->ifname);
3656 drv->first_bss.ifindex = drv->ifindex;
3657
3658#ifndef HOSTAPD
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003659 /*
3660 * Make sure the interface starts up in station mode unless this is a
3661 * dynamically added interface (e.g., P2P) that was already configured
3662 * with proper iftype.
3663 */
3664 if (drv->ifindex != drv->global->if_add_ifindex &&
3665 wpa_driver_nl80211_set_mode(bss, NL80211_IFTYPE_STATION) < 0) {
3666 wpa_printf(MSG_ERROR, "nl80211: Could not configure driver to "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003667 "use managed mode");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003668 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003669 }
3670
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003671 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003672 if (rfkill_is_blocked(drv->rfkill)) {
3673 wpa_printf(MSG_DEBUG, "nl80211: Could not yet enable "
3674 "interface '%s' due to rfkill",
3675 bss->ifname);
3676 drv->if_disabled = 1;
3677 send_rfkill_event = 1;
3678 } else {
3679 wpa_printf(MSG_ERROR, "nl80211: Could not set "
3680 "interface '%s' UP", bss->ifname);
3681 return -1;
3682 }
3683 }
3684
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003685 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003686 1, IF_OPER_DORMANT);
3687#endif /* HOSTAPD */
3688
3689 if (wpa_driver_nl80211_capa(drv))
3690 return -1;
3691
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003692 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
3693 bss->addr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003694 return -1;
3695
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003696 if (send_rfkill_event) {
3697 eloop_register_timeout(0, 0, wpa_driver_nl80211_send_rfkill,
3698 drv, drv->ctx);
3699 }
3700
3701 return 0;
3702}
3703
3704
3705static int wpa_driver_nl80211_del_beacon(struct wpa_driver_nl80211_data *drv)
3706{
3707 struct nl_msg *msg;
3708
3709 msg = nlmsg_alloc();
3710 if (!msg)
3711 return -ENOMEM;
3712
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003713 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_BEACON);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003714 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
3715
3716 return send_and_recv_msgs(drv, msg, NULL, NULL);
3717 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003718 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003719 return -ENOBUFS;
3720}
3721
3722
3723/**
3724 * wpa_driver_nl80211_deinit - Deinitialize nl80211 driver interface
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003725 * @bss: Pointer to private nl80211 data from wpa_driver_nl80211_init()
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003726 *
3727 * Shut down driver interface and processing of driver events. Free
3728 * private data buffer if one was allocated in wpa_driver_nl80211_init().
3729 */
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003730static void wpa_driver_nl80211_deinit(struct i802_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003731{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003732 struct wpa_driver_nl80211_data *drv = bss->drv;
3733
Dmitry Shmidt04949592012-07-19 12:16:46 -07003734 bss->in_deinit = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003735 if (drv->data_tx_status)
3736 eloop_unregister_read_sock(drv->eapol_tx_sock);
3737 if (drv->eapol_tx_sock >= 0)
3738 close(drv->eapol_tx_sock);
3739
3740 if (bss->nl_preq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003741 wpa_driver_nl80211_probe_req_report(bss, 0);
3742 if (bss->added_if_into_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003743 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
3744 bss->ifname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003745 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
3746 "interface %s from bridge %s: %s",
3747 bss->ifname, bss->brname, strerror(errno));
3748 }
3749 if (bss->added_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003750 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003751 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
3752 "bridge %s: %s",
3753 bss->brname, strerror(errno));
3754 }
3755
3756 nl80211_remove_monitor_interface(drv);
3757
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003758 if (is_ap_interface(drv->nlmode))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003759 wpa_driver_nl80211_del_beacon(drv);
3760
3761#ifdef HOSTAPD
3762 if (drv->last_freq_ht) {
3763 /* Clear HT flags from the driver */
3764 struct hostapd_freq_params freq;
3765 os_memset(&freq, 0, sizeof(freq));
3766 freq.freq = drv->last_freq;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003767 wpa_driver_nl80211_set_freq(bss, &freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003768 }
3769
3770 if (drv->eapol_sock >= 0) {
3771 eloop_unregister_read_sock(drv->eapol_sock);
3772 close(drv->eapol_sock);
3773 }
3774
3775 if (drv->if_indices != drv->default_if_indices)
3776 os_free(drv->if_indices);
3777#endif /* HOSTAPD */
3778
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003779 if (drv->disabled_11b_rates)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003780 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
3781
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003782 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, 0,
3783 IF_OPER_UP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003784 rfkill_deinit(drv->rfkill);
3785
3786 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
3787
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003788 (void) linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0);
3789 wpa_driver_nl80211_set_mode(bss, NL80211_IFTYPE_STATION);
3790 nl80211_mgmt_unsubscribe(bss, "deinit");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003791
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003792 nl_cb_put(drv->nl_cb);
3793
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003794 nl80211_destroy_bss(&drv->first_bss);
3795
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003796 os_free(drv->filter_ssids);
3797
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003798 os_free(drv->auth_ie);
3799
3800 if (drv->in_interface_list)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003801 dl_list_del(&drv->list);
3802
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003803 os_free(drv->extended_capa);
3804 os_free(drv->extended_capa_mask);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003805 os_free(drv);
3806}
3807
3808
3809/**
3810 * wpa_driver_nl80211_scan_timeout - Scan timeout to report scan completion
3811 * @eloop_ctx: Driver private data
3812 * @timeout_ctx: ctx argument given to wpa_driver_nl80211_init()
3813 *
3814 * This function can be used as registered timeout when starting a scan to
3815 * generate a scan completed event if the driver does not report this.
3816 */
3817static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx, void *timeout_ctx)
3818{
3819 struct wpa_driver_nl80211_data *drv = eloop_ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003820 if (drv->ap_scan_as_station != NL80211_IFTYPE_UNSPECIFIED) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003821 wpa_driver_nl80211_set_mode(&drv->first_bss,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003822 drv->ap_scan_as_station);
3823 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003824 }
3825 wpa_printf(MSG_DEBUG, "Scan timeout - try to get results");
3826 wpa_supplicant_event(timeout_ctx, EVENT_SCAN_RESULTS, NULL);
3827}
3828
3829
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003830static struct nl_msg *
3831nl80211_scan_common(struct wpa_driver_nl80211_data *drv, u8 cmd,
3832 struct wpa_driver_scan_params *params)
3833{
3834 struct nl_msg *msg;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003835 size_t i;
3836
3837 msg = nlmsg_alloc();
3838 if (!msg)
3839 return NULL;
3840
3841 nl80211_cmd(drv, msg, 0, cmd);
3842
3843 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, drv->ifindex) < 0)
3844 goto fail;
3845
3846 if (params->num_ssids) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003847 struct nlattr *ssids;
3848
3849 ssids = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003850 if (ssids == NULL)
3851 goto fail;
3852 for (i = 0; i < params->num_ssids; i++) {
3853 wpa_hexdump_ascii(MSG_MSGDUMP, "nl80211: Scan SSID",
3854 params->ssids[i].ssid,
3855 params->ssids[i].ssid_len);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003856 if (nla_put(msg, i + 1, params->ssids[i].ssid_len,
3857 params->ssids[i].ssid) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003858 goto fail;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003859 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003860 nla_nest_end(msg, ssids);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003861 }
3862
3863 if (params->extra_ies) {
3864 wpa_hexdump(MSG_MSGDUMP, "nl80211: Scan extra IEs",
3865 params->extra_ies, params->extra_ies_len);
3866 if (nla_put(msg, NL80211_ATTR_IE, params->extra_ies_len,
3867 params->extra_ies) < 0)
3868 goto fail;
3869 }
3870
3871 if (params->freqs) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003872 struct nlattr *freqs;
3873 freqs = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003874 if (freqs == NULL)
3875 goto fail;
3876 for (i = 0; params->freqs[i]; i++) {
3877 wpa_printf(MSG_MSGDUMP, "nl80211: Scan frequency %u "
3878 "MHz", params->freqs[i]);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003879 if (nla_put_u32(msg, i + 1, params->freqs[i]) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003880 goto fail;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003881 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003882 nla_nest_end(msg, freqs);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003883 }
3884
3885 os_free(drv->filter_ssids);
3886 drv->filter_ssids = params->filter_ssids;
3887 params->filter_ssids = NULL;
3888 drv->num_filter_ssids = params->num_filter_ssids;
3889
3890 return msg;
3891
3892fail:
3893 nlmsg_free(msg);
3894 return NULL;
3895}
3896
3897
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003898/**
3899 * wpa_driver_nl80211_scan - Request the driver to initiate scan
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003900 * @bss: Pointer to private driver data from wpa_driver_nl80211_init()
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003901 * @params: Scan parameters
3902 * Returns: 0 on success, -1 on failure
3903 */
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003904static int wpa_driver_nl80211_scan(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003905 struct wpa_driver_scan_params *params)
3906{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003907 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003908 int ret = -1, timeout;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003909 struct nl_msg *msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003910
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003911 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: scan request");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003912 drv->scan_for_auth = 0;
3913
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003914 msg = nl80211_scan_common(drv, NL80211_CMD_TRIGGER_SCAN, params);
3915 if (!msg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003916 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003917
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003918 if (params->p2p_probe) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003919 struct nlattr *rates;
3920
Dmitry Shmidt04949592012-07-19 12:16:46 -07003921 wpa_printf(MSG_DEBUG, "nl80211: P2P probe - mask SuppRates");
3922
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003923 rates = nla_nest_start(msg, NL80211_ATTR_SCAN_SUPP_RATES);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003924 if (rates == NULL)
3925 goto nla_put_failure;
3926
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003927 /*
3928 * Remove 2.4 GHz rates 1, 2, 5.5, 11 Mbps from supported rates
3929 * by masking out everything else apart from the OFDM rates 6,
3930 * 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS rates. All 5 GHz
3931 * rates are left enabled.
3932 */
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003933 NLA_PUT(msg, NL80211_BAND_2GHZ, 8,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003934 "\x0c\x12\x18\x24\x30\x48\x60\x6c");
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003935 nla_nest_end(msg, rates);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003936
3937 NLA_PUT_FLAG(msg, NL80211_ATTR_TX_NO_CCK_RATE);
3938 }
3939
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003940 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3941 msg = NULL;
3942 if (ret) {
3943 wpa_printf(MSG_DEBUG, "nl80211: Scan trigger failed: ret=%d "
3944 "(%s)", ret, strerror(-ret));
3945#ifdef HOSTAPD
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003946 if (is_ap_interface(drv->nlmode)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003947 /*
3948 * mac80211 does not allow scan requests in AP mode, so
3949 * try to do this in station mode.
3950 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003951 if (wpa_driver_nl80211_set_mode(
3952 bss, NL80211_IFTYPE_STATION))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003953 goto nla_put_failure;
3954
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003955 if (wpa_driver_nl80211_scan(bss, params)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003956 wpa_driver_nl80211_set_mode(bss, drv->nlmode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003957 goto nla_put_failure;
3958 }
3959
3960 /* Restore AP mode when processing scan results */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003961 drv->ap_scan_as_station = drv->nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003962 ret = 0;
3963 } else
3964 goto nla_put_failure;
3965#else /* HOSTAPD */
3966 goto nla_put_failure;
3967#endif /* HOSTAPD */
3968 }
3969
3970 /* Not all drivers generate "scan completed" wireless event, so try to
3971 * read results after a timeout. */
3972 timeout = 10;
3973 if (drv->scan_complete_events) {
3974 /*
3975 * The driver seems to deliver events to notify when scan is
3976 * complete, so use longer timeout to avoid race conditions
3977 * with scanning and following association request.
3978 */
3979 timeout = 30;
3980 }
3981 wpa_printf(MSG_DEBUG, "Scan requested (ret=%d) - scan timeout %d "
3982 "seconds", ret, timeout);
3983 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
3984 eloop_register_timeout(timeout, 0, wpa_driver_nl80211_scan_timeout,
3985 drv, drv->ctx);
3986
3987nla_put_failure:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003988 nlmsg_free(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003989 return ret;
3990}
3991
3992
3993/**
3994 * wpa_driver_nl80211_sched_scan - Initiate a scheduled scan
3995 * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
3996 * @params: Scan parameters
3997 * @interval: Interval between scan cycles in milliseconds
3998 * Returns: 0 on success, -1 on failure or if not supported
3999 */
4000static int wpa_driver_nl80211_sched_scan(void *priv,
4001 struct wpa_driver_scan_params *params,
4002 u32 interval)
4003{
4004 struct i802_bss *bss = priv;
4005 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004006 int ret = -1;
4007 struct nl_msg *msg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004008 size_t i;
4009
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004010 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: sched_scan request");
4011
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004012#ifdef ANDROID
4013 if (!drv->capa.sched_scan_supported)
4014 return android_pno_start(bss, params);
4015#endif /* ANDROID */
4016
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004017 msg = nl80211_scan_common(drv, NL80211_CMD_START_SCHED_SCAN, params);
4018 if (!msg)
4019 goto nla_put_failure;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004020
4021 NLA_PUT_U32(msg, NL80211_ATTR_SCHED_SCAN_INTERVAL, interval);
4022
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004023 if ((drv->num_filter_ssids &&
4024 (int) drv->num_filter_ssids <= drv->capa.max_match_sets) ||
4025 params->filter_rssi) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004026 struct nlattr *match_sets;
4027 match_sets = nla_nest_start(msg, NL80211_ATTR_SCHED_SCAN_MATCH);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004028 if (match_sets == NULL)
4029 goto nla_put_failure;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004030
4031 for (i = 0; i < drv->num_filter_ssids; i++) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004032 struct nlattr *match_set_ssid;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004033 wpa_hexdump_ascii(MSG_MSGDUMP,
4034 "nl80211: Sched scan filter SSID",
4035 drv->filter_ssids[i].ssid,
4036 drv->filter_ssids[i].ssid_len);
4037
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004038 match_set_ssid = nla_nest_start(msg, i + 1);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004039 if (match_set_ssid == NULL)
4040 goto nla_put_failure;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004041 NLA_PUT(msg, NL80211_ATTR_SCHED_SCAN_MATCH_SSID,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004042 drv->filter_ssids[i].ssid_len,
4043 drv->filter_ssids[i].ssid);
4044
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004045 nla_nest_end(msg, match_set_ssid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004046 }
4047
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004048 if (params->filter_rssi) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004049 struct nlattr *match_set_rssi;
4050 match_set_rssi = nla_nest_start(msg, 0);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004051 if (match_set_rssi == NULL)
4052 goto nla_put_failure;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004053 NLA_PUT_U32(msg, NL80211_SCHED_SCAN_MATCH_ATTR_RSSI,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004054 params->filter_rssi);
4055 wpa_printf(MSG_MSGDUMP,
4056 "nl80211: Sched scan RSSI filter %d dBm",
4057 params->filter_rssi);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004058 nla_nest_end(msg, match_set_rssi);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004059 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004060
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004061 nla_nest_end(msg, match_sets);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004062 }
4063
4064 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4065
4066 /* TODO: if we get an error here, we should fall back to normal scan */
4067
4068 msg = NULL;
4069 if (ret) {
4070 wpa_printf(MSG_DEBUG, "nl80211: Sched scan start failed: "
4071 "ret=%d (%s)", ret, strerror(-ret));
4072 goto nla_put_failure;
4073 }
4074
4075 wpa_printf(MSG_DEBUG, "nl80211: Sched scan requested (ret=%d) - "
4076 "scan interval %d msec", ret, interval);
4077
4078nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004079 nlmsg_free(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004080 return ret;
4081}
4082
4083
4084/**
4085 * wpa_driver_nl80211_stop_sched_scan - Stop a scheduled scan
4086 * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
4087 * Returns: 0 on success, -1 on failure or if not supported
4088 */
4089static int wpa_driver_nl80211_stop_sched_scan(void *priv)
4090{
4091 struct i802_bss *bss = priv;
4092 struct wpa_driver_nl80211_data *drv = bss->drv;
4093 int ret = 0;
4094 struct nl_msg *msg;
4095
4096#ifdef ANDROID
4097 if (!drv->capa.sched_scan_supported)
4098 return android_pno_stop(bss);
4099#endif /* ANDROID */
4100
4101 msg = nlmsg_alloc();
4102 if (!msg)
4103 return -1;
4104
4105 nl80211_cmd(drv, msg, 0, NL80211_CMD_STOP_SCHED_SCAN);
4106
4107 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
4108
4109 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4110 msg = NULL;
4111 if (ret) {
4112 wpa_printf(MSG_DEBUG, "nl80211: Sched scan stop failed: "
4113 "ret=%d (%s)", ret, strerror(-ret));
4114 goto nla_put_failure;
4115 }
4116
4117 wpa_printf(MSG_DEBUG, "nl80211: Sched scan stop sent (ret=%d)", ret);
4118
4119nla_put_failure:
4120 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004121 return ret;
4122}
4123
4124
4125static const u8 * nl80211_get_ie(const u8 *ies, size_t ies_len, u8 ie)
4126{
4127 const u8 *end, *pos;
4128
4129 if (ies == NULL)
4130 return NULL;
4131
4132 pos = ies;
4133 end = ies + ies_len;
4134
4135 while (pos + 1 < end) {
4136 if (pos + 2 + pos[1] > end)
4137 break;
4138 if (pos[0] == ie)
4139 return pos;
4140 pos += 2 + pos[1];
4141 }
4142
4143 return NULL;
4144}
4145
4146
4147static int nl80211_scan_filtered(struct wpa_driver_nl80211_data *drv,
4148 const u8 *ie, size_t ie_len)
4149{
4150 const u8 *ssid;
4151 size_t i;
4152
4153 if (drv->filter_ssids == NULL)
4154 return 0;
4155
4156 ssid = nl80211_get_ie(ie, ie_len, WLAN_EID_SSID);
4157 if (ssid == NULL)
4158 return 1;
4159
4160 for (i = 0; i < drv->num_filter_ssids; i++) {
4161 if (ssid[1] == drv->filter_ssids[i].ssid_len &&
4162 os_memcmp(ssid + 2, drv->filter_ssids[i].ssid, ssid[1]) ==
4163 0)
4164 return 0;
4165 }
4166
4167 return 1;
4168}
4169
4170
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004171static int bss_info_handler(struct nl_msg *msg, void *arg)
4172{
4173 struct nlattr *tb[NL80211_ATTR_MAX + 1];
4174 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
4175 struct nlattr *bss[NL80211_BSS_MAX + 1];
4176 static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
4177 [NL80211_BSS_BSSID] = { .type = NLA_UNSPEC },
4178 [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
4179 [NL80211_BSS_TSF] = { .type = NLA_U64 },
4180 [NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 },
4181 [NL80211_BSS_CAPABILITY] = { .type = NLA_U16 },
4182 [NL80211_BSS_INFORMATION_ELEMENTS] = { .type = NLA_UNSPEC },
4183 [NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 },
4184 [NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 },
4185 [NL80211_BSS_STATUS] = { .type = NLA_U32 },
4186 [NL80211_BSS_SEEN_MS_AGO] = { .type = NLA_U32 },
4187 [NL80211_BSS_BEACON_IES] = { .type = NLA_UNSPEC },
4188 };
4189 struct nl80211_bss_info_arg *_arg = arg;
4190 struct wpa_scan_results *res = _arg->res;
4191 struct wpa_scan_res **tmp;
4192 struct wpa_scan_res *r;
4193 const u8 *ie, *beacon_ie;
4194 size_t ie_len, beacon_ie_len;
4195 u8 *pos;
Jouni Malinen87fd2792011-05-16 18:35:42 +03004196 size_t i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004197
4198 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
4199 genlmsg_attrlen(gnlh, 0), NULL);
4200 if (!tb[NL80211_ATTR_BSS])
4201 return NL_SKIP;
4202 if (nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS],
4203 bss_policy))
4204 return NL_SKIP;
Jouni Malinen87fd2792011-05-16 18:35:42 +03004205 if (bss[NL80211_BSS_STATUS]) {
4206 enum nl80211_bss_status status;
4207 status = nla_get_u32(bss[NL80211_BSS_STATUS]);
4208 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
4209 bss[NL80211_BSS_FREQUENCY]) {
4210 _arg->assoc_freq =
4211 nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
4212 wpa_printf(MSG_DEBUG, "nl80211: Associated on %u MHz",
4213 _arg->assoc_freq);
4214 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004215 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
4216 bss[NL80211_BSS_BSSID]) {
4217 os_memcpy(_arg->assoc_bssid,
4218 nla_data(bss[NL80211_BSS_BSSID]), ETH_ALEN);
4219 wpa_printf(MSG_DEBUG, "nl80211: Associated with "
4220 MACSTR, MAC2STR(_arg->assoc_bssid));
4221 }
Jouni Malinen87fd2792011-05-16 18:35:42 +03004222 }
4223 if (!res)
4224 return NL_SKIP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004225 if (bss[NL80211_BSS_INFORMATION_ELEMENTS]) {
4226 ie = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
4227 ie_len = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
4228 } else {
4229 ie = NULL;
4230 ie_len = 0;
4231 }
4232 if (bss[NL80211_BSS_BEACON_IES]) {
4233 beacon_ie = nla_data(bss[NL80211_BSS_BEACON_IES]);
4234 beacon_ie_len = nla_len(bss[NL80211_BSS_BEACON_IES]);
4235 } else {
4236 beacon_ie = NULL;
4237 beacon_ie_len = 0;
4238 }
4239
4240 if (nl80211_scan_filtered(_arg->drv, ie ? ie : beacon_ie,
4241 ie ? ie_len : beacon_ie_len))
4242 return NL_SKIP;
4243
4244 r = os_zalloc(sizeof(*r) + ie_len + beacon_ie_len);
4245 if (r == NULL)
4246 return NL_SKIP;
4247 if (bss[NL80211_BSS_BSSID])
4248 os_memcpy(r->bssid, nla_data(bss[NL80211_BSS_BSSID]),
4249 ETH_ALEN);
4250 if (bss[NL80211_BSS_FREQUENCY])
4251 r->freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
4252 if (bss[NL80211_BSS_BEACON_INTERVAL])
4253 r->beacon_int = nla_get_u16(bss[NL80211_BSS_BEACON_INTERVAL]);
4254 if (bss[NL80211_BSS_CAPABILITY])
4255 r->caps = nla_get_u16(bss[NL80211_BSS_CAPABILITY]);
4256 r->flags |= WPA_SCAN_NOISE_INVALID;
4257 if (bss[NL80211_BSS_SIGNAL_MBM]) {
4258 r->level = nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]);
4259 r->level /= 100; /* mBm to dBm */
4260 r->flags |= WPA_SCAN_LEVEL_DBM | WPA_SCAN_QUAL_INVALID;
4261 } else if (bss[NL80211_BSS_SIGNAL_UNSPEC]) {
4262 r->level = nla_get_u8(bss[NL80211_BSS_SIGNAL_UNSPEC]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004263 r->flags |= WPA_SCAN_QUAL_INVALID;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004264 } else
4265 r->flags |= WPA_SCAN_LEVEL_INVALID | WPA_SCAN_QUAL_INVALID;
4266 if (bss[NL80211_BSS_TSF])
4267 r->tsf = nla_get_u64(bss[NL80211_BSS_TSF]);
4268 if (bss[NL80211_BSS_SEEN_MS_AGO])
4269 r->age = nla_get_u32(bss[NL80211_BSS_SEEN_MS_AGO]);
4270 r->ie_len = ie_len;
4271 pos = (u8 *) (r + 1);
4272 if (ie) {
4273 os_memcpy(pos, ie, ie_len);
4274 pos += ie_len;
4275 }
4276 r->beacon_ie_len = beacon_ie_len;
4277 if (beacon_ie)
4278 os_memcpy(pos, beacon_ie, beacon_ie_len);
4279
4280 if (bss[NL80211_BSS_STATUS]) {
4281 enum nl80211_bss_status status;
4282 status = nla_get_u32(bss[NL80211_BSS_STATUS]);
4283 switch (status) {
4284 case NL80211_BSS_STATUS_AUTHENTICATED:
4285 r->flags |= WPA_SCAN_AUTHENTICATED;
4286 break;
4287 case NL80211_BSS_STATUS_ASSOCIATED:
4288 r->flags |= WPA_SCAN_ASSOCIATED;
4289 break;
4290 default:
4291 break;
4292 }
4293 }
4294
Jouni Malinen87fd2792011-05-16 18:35:42 +03004295 /*
4296 * cfg80211 maintains separate BSS table entries for APs if the same
4297 * BSSID,SSID pair is seen on multiple channels. wpa_supplicant does
4298 * not use frequency as a separate key in the BSS table, so filter out
4299 * duplicated entries. Prefer associated BSS entry in such a case in
4300 * order to get the correct frequency into the BSS table.
4301 */
4302 for (i = 0; i < res->num; i++) {
4303 const u8 *s1, *s2;
4304 if (os_memcmp(res->res[i]->bssid, r->bssid, ETH_ALEN) != 0)
4305 continue;
4306
4307 s1 = nl80211_get_ie((u8 *) (res->res[i] + 1),
4308 res->res[i]->ie_len, WLAN_EID_SSID);
4309 s2 = nl80211_get_ie((u8 *) (r + 1), r->ie_len, WLAN_EID_SSID);
4310 if (s1 == NULL || s2 == NULL || s1[1] != s2[1] ||
4311 os_memcmp(s1, s2, 2 + s1[1]) != 0)
4312 continue;
4313
4314 /* Same BSSID,SSID was already included in scan results */
4315 wpa_printf(MSG_DEBUG, "nl80211: Remove duplicated scan result "
4316 "for " MACSTR, MAC2STR(r->bssid));
4317
4318 if ((r->flags & WPA_SCAN_ASSOCIATED) &&
4319 !(res->res[i]->flags & WPA_SCAN_ASSOCIATED)) {
4320 os_free(res->res[i]);
4321 res->res[i] = r;
4322 } else
4323 os_free(r);
4324 return NL_SKIP;
4325 }
4326
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004327 tmp = os_realloc_array(res->res, res->num + 1,
4328 sizeof(struct wpa_scan_res *));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004329 if (tmp == NULL) {
4330 os_free(r);
4331 return NL_SKIP;
4332 }
4333 tmp[res->num++] = r;
4334 res->res = tmp;
4335
4336 return NL_SKIP;
4337}
4338
4339
4340static void clear_state_mismatch(struct wpa_driver_nl80211_data *drv,
4341 const u8 *addr)
4342{
4343 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
4344 wpa_printf(MSG_DEBUG, "nl80211: Clear possible state "
4345 "mismatch (" MACSTR ")", MAC2STR(addr));
4346 wpa_driver_nl80211_mlme(drv, addr,
4347 NL80211_CMD_DEAUTHENTICATE,
4348 WLAN_REASON_PREV_AUTH_NOT_VALID, 1);
4349 }
4350}
4351
4352
4353static void wpa_driver_nl80211_check_bss_status(
4354 struct wpa_driver_nl80211_data *drv, struct wpa_scan_results *res)
4355{
4356 size_t i;
4357
4358 for (i = 0; i < res->num; i++) {
4359 struct wpa_scan_res *r = res->res[i];
4360 if (r->flags & WPA_SCAN_AUTHENTICATED) {
4361 wpa_printf(MSG_DEBUG, "nl80211: Scan results "
4362 "indicates BSS status with " MACSTR
4363 " as authenticated",
4364 MAC2STR(r->bssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004365 if (is_sta_interface(drv->nlmode) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004366 os_memcmp(r->bssid, drv->bssid, ETH_ALEN) != 0 &&
4367 os_memcmp(r->bssid, drv->auth_bssid, ETH_ALEN) !=
4368 0) {
4369 wpa_printf(MSG_DEBUG, "nl80211: Unknown BSSID"
4370 " in local state (auth=" MACSTR
4371 " assoc=" MACSTR ")",
4372 MAC2STR(drv->auth_bssid),
4373 MAC2STR(drv->bssid));
4374 clear_state_mismatch(drv, r->bssid);
4375 }
4376 }
4377
4378 if (r->flags & WPA_SCAN_ASSOCIATED) {
4379 wpa_printf(MSG_DEBUG, "nl80211: Scan results "
4380 "indicate BSS status with " MACSTR
4381 " as associated",
4382 MAC2STR(r->bssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004383 if (is_sta_interface(drv->nlmode) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004384 !drv->associated) {
4385 wpa_printf(MSG_DEBUG, "nl80211: Local state "
4386 "(not associated) does not match "
4387 "with BSS state");
4388 clear_state_mismatch(drv, r->bssid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004389 } else if (is_sta_interface(drv->nlmode) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004390 os_memcmp(drv->bssid, r->bssid, ETH_ALEN) !=
4391 0) {
4392 wpa_printf(MSG_DEBUG, "nl80211: Local state "
4393 "(associated with " MACSTR ") does "
4394 "not match with BSS state",
4395 MAC2STR(drv->bssid));
4396 clear_state_mismatch(drv, r->bssid);
4397 clear_state_mismatch(drv, drv->bssid);
4398 }
4399 }
4400 }
4401}
4402
4403
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004404static struct wpa_scan_results *
4405nl80211_get_scan_results(struct wpa_driver_nl80211_data *drv)
4406{
4407 struct nl_msg *msg;
4408 struct wpa_scan_results *res;
4409 int ret;
4410 struct nl80211_bss_info_arg arg;
4411
4412 res = os_zalloc(sizeof(*res));
4413 if (res == NULL)
4414 return NULL;
4415 msg = nlmsg_alloc();
4416 if (!msg)
4417 goto nla_put_failure;
4418
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004419 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004420 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
4421
4422 arg.drv = drv;
4423 arg.res = res;
4424 ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
4425 msg = NULL;
4426 if (ret == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004427 wpa_printf(MSG_DEBUG, "nl80211: Received scan results (%lu "
4428 "BSSes)", (unsigned long) res->num);
4429 nl80211_get_noise_for_scan_results(drv, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004430 return res;
4431 }
4432 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
4433 "(%s)", ret, strerror(-ret));
4434nla_put_failure:
4435 nlmsg_free(msg);
4436 wpa_scan_results_free(res);
4437 return NULL;
4438}
4439
4440
4441/**
4442 * wpa_driver_nl80211_get_scan_results - Fetch the latest scan results
4443 * @priv: Pointer to private wext data from wpa_driver_nl80211_init()
4444 * Returns: Scan results on success, -1 on failure
4445 */
4446static struct wpa_scan_results *
4447wpa_driver_nl80211_get_scan_results(void *priv)
4448{
4449 struct i802_bss *bss = priv;
4450 struct wpa_driver_nl80211_data *drv = bss->drv;
4451 struct wpa_scan_results *res;
4452
4453 res = nl80211_get_scan_results(drv);
4454 if (res)
4455 wpa_driver_nl80211_check_bss_status(drv, res);
4456 return res;
4457}
4458
4459
4460static void nl80211_dump_scan(struct wpa_driver_nl80211_data *drv)
4461{
4462 struct wpa_scan_results *res;
4463 size_t i;
4464
4465 res = nl80211_get_scan_results(drv);
4466 if (res == NULL) {
4467 wpa_printf(MSG_DEBUG, "nl80211: Failed to get scan results");
4468 return;
4469 }
4470
4471 wpa_printf(MSG_DEBUG, "nl80211: Scan result dump");
4472 for (i = 0; i < res->num; i++) {
4473 struct wpa_scan_res *r = res->res[i];
4474 wpa_printf(MSG_DEBUG, "nl80211: %d/%d " MACSTR "%s%s",
4475 (int) i, (int) res->num, MAC2STR(r->bssid),
4476 r->flags & WPA_SCAN_AUTHENTICATED ? " [auth]" : "",
4477 r->flags & WPA_SCAN_ASSOCIATED ? " [assoc]" : "");
4478 }
4479
4480 wpa_scan_results_free(res);
4481}
4482
4483
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004484static int wpa_driver_nl80211_set_key(const char *ifname, struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004485 enum wpa_alg alg, const u8 *addr,
4486 int key_idx, int set_tx,
4487 const u8 *seq, size_t seq_len,
4488 const u8 *key, size_t key_len)
4489{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004490 struct wpa_driver_nl80211_data *drv = bss->drv;
4491 int ifindex = if_nametoindex(ifname);
4492 struct nl_msg *msg;
4493 int ret;
4494
4495 wpa_printf(MSG_DEBUG, "%s: ifindex=%d alg=%d addr=%p key_idx=%d "
4496 "set_tx=%d seq_len=%lu key_len=%lu",
4497 __func__, ifindex, alg, addr, key_idx, set_tx,
4498 (unsigned long) seq_len, (unsigned long) key_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004499#ifdef CONFIG_TDLS
4500 if (key_idx == -1)
4501 key_idx = 0;
4502#endif /* CONFIG_TDLS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004503
4504 msg = nlmsg_alloc();
4505 if (!msg)
4506 return -ENOMEM;
4507
4508 if (alg == WPA_ALG_NONE) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004509 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_KEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004510 } else {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004511 nl80211_cmd(drv, msg, 0, NL80211_CMD_NEW_KEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004512 NLA_PUT(msg, NL80211_ATTR_KEY_DATA, key_len, key);
4513 switch (alg) {
4514 case WPA_ALG_WEP:
4515 if (key_len == 5)
4516 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
4517 WLAN_CIPHER_SUITE_WEP40);
4518 else
4519 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
4520 WLAN_CIPHER_SUITE_WEP104);
4521 break;
4522 case WPA_ALG_TKIP:
4523 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
4524 WLAN_CIPHER_SUITE_TKIP);
4525 break;
4526 case WPA_ALG_CCMP:
4527 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
4528 WLAN_CIPHER_SUITE_CCMP);
4529 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004530 case WPA_ALG_GCMP:
4531 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
4532 WLAN_CIPHER_SUITE_GCMP);
4533 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004534 case WPA_ALG_IGTK:
4535 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
4536 WLAN_CIPHER_SUITE_AES_CMAC);
4537 break;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004538 case WPA_ALG_SMS4:
4539 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
4540 WLAN_CIPHER_SUITE_SMS4);
4541 break;
4542 case WPA_ALG_KRK:
4543 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
4544 WLAN_CIPHER_SUITE_KRK);
4545 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004546 default:
4547 wpa_printf(MSG_ERROR, "%s: Unsupported encryption "
4548 "algorithm %d", __func__, alg);
4549 nlmsg_free(msg);
4550 return -1;
4551 }
4552 }
4553
4554 if (seq && seq_len)
4555 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, seq_len, seq);
4556
4557 if (addr && !is_broadcast_ether_addr(addr)) {
4558 wpa_printf(MSG_DEBUG, " addr=" MACSTR, MAC2STR(addr));
4559 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
4560
4561 if (alg != WPA_ALG_WEP && key_idx && !set_tx) {
4562 wpa_printf(MSG_DEBUG, " RSN IBSS RX GTK");
4563 NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE,
4564 NL80211_KEYTYPE_GROUP);
4565 }
4566 } else if (addr && is_broadcast_ether_addr(addr)) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004567 struct nlattr *types;
4568
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004569 wpa_printf(MSG_DEBUG, " broadcast key");
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004570
4571 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004572 if (!types)
4573 goto nla_put_failure;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004574 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST);
4575 nla_nest_end(msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004576 }
4577 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
4578 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
4579
4580 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4581 if ((ret == -ENOENT || ret == -ENOLINK) && alg == WPA_ALG_NONE)
4582 ret = 0;
4583 if (ret)
4584 wpa_printf(MSG_DEBUG, "nl80211: set_key failed; err=%d %s)",
4585 ret, strerror(-ret));
4586
4587 /*
4588 * If we failed or don't need to set the default TX key (below),
4589 * we're done here.
4590 */
4591 if (ret || !set_tx || alg == WPA_ALG_NONE)
4592 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004593 if (is_ap_interface(drv->nlmode) && addr &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004594 !is_broadcast_ether_addr(addr))
4595 return ret;
4596
4597 msg = nlmsg_alloc();
4598 if (!msg)
4599 return -ENOMEM;
4600
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004601 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_KEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004602 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
4603 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
4604 if (alg == WPA_ALG_IGTK)
4605 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT_MGMT);
4606 else
4607 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT);
4608 if (addr && is_broadcast_ether_addr(addr)) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004609 struct nlattr *types;
4610
4611 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004612 if (!types)
4613 goto nla_put_failure;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004614 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST);
4615 nla_nest_end(msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004616 } else if (addr) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004617 struct nlattr *types;
4618
4619 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004620 if (!types)
4621 goto nla_put_failure;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004622 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_UNICAST);
4623 nla_nest_end(msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004624 }
4625
4626 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4627 if (ret == -ENOENT)
4628 ret = 0;
4629 if (ret)
4630 wpa_printf(MSG_DEBUG, "nl80211: set_key default failed; "
4631 "err=%d %s)", ret, strerror(-ret));
4632 return ret;
4633
4634nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004635 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004636 return -ENOBUFS;
4637}
4638
4639
4640static int nl_add_key(struct nl_msg *msg, enum wpa_alg alg,
4641 int key_idx, int defkey,
4642 const u8 *seq, size_t seq_len,
4643 const u8 *key, size_t key_len)
4644{
4645 struct nlattr *key_attr = nla_nest_start(msg, NL80211_ATTR_KEY);
4646 if (!key_attr)
4647 return -1;
4648
4649 if (defkey && alg == WPA_ALG_IGTK)
4650 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_MGMT);
4651 else if (defkey)
4652 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
4653
4654 NLA_PUT_U8(msg, NL80211_KEY_IDX, key_idx);
4655
4656 switch (alg) {
4657 case WPA_ALG_WEP:
4658 if (key_len == 5)
4659 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
4660 WLAN_CIPHER_SUITE_WEP40);
4661 else
4662 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
4663 WLAN_CIPHER_SUITE_WEP104);
4664 break;
4665 case WPA_ALG_TKIP:
4666 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, WLAN_CIPHER_SUITE_TKIP);
4667 break;
4668 case WPA_ALG_CCMP:
4669 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, WLAN_CIPHER_SUITE_CCMP);
4670 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004671 case WPA_ALG_GCMP:
4672 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, WLAN_CIPHER_SUITE_GCMP);
4673 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004674 case WPA_ALG_IGTK:
4675 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
4676 WLAN_CIPHER_SUITE_AES_CMAC);
4677 break;
4678 default:
4679 wpa_printf(MSG_ERROR, "%s: Unsupported encryption "
4680 "algorithm %d", __func__, alg);
4681 return -1;
4682 }
4683
4684 if (seq && seq_len)
4685 NLA_PUT(msg, NL80211_KEY_SEQ, seq_len, seq);
4686
4687 NLA_PUT(msg, NL80211_KEY_DATA, key_len, key);
4688
4689 nla_nest_end(msg, key_attr);
4690
4691 return 0;
4692 nla_put_failure:
4693 return -1;
4694}
4695
4696
4697static int nl80211_set_conn_keys(struct wpa_driver_associate_params *params,
4698 struct nl_msg *msg)
4699{
4700 int i, privacy = 0;
4701 struct nlattr *nl_keys, *nl_key;
4702
4703 for (i = 0; i < 4; i++) {
4704 if (!params->wep_key[i])
4705 continue;
4706 privacy = 1;
4707 break;
4708 }
4709 if (params->wps == WPS_MODE_PRIVACY)
4710 privacy = 1;
4711 if (params->pairwise_suite &&
4712 params->pairwise_suite != WPA_CIPHER_NONE)
4713 privacy = 1;
4714
4715 if (!privacy)
4716 return 0;
4717
4718 NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
4719
4720 nl_keys = nla_nest_start(msg, NL80211_ATTR_KEYS);
4721 if (!nl_keys)
4722 goto nla_put_failure;
4723
4724 for (i = 0; i < 4; i++) {
4725 if (!params->wep_key[i])
4726 continue;
4727
4728 nl_key = nla_nest_start(msg, i);
4729 if (!nl_key)
4730 goto nla_put_failure;
4731
4732 NLA_PUT(msg, NL80211_KEY_DATA, params->wep_key_len[i],
4733 params->wep_key[i]);
4734 if (params->wep_key_len[i] == 5)
4735 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
4736 WLAN_CIPHER_SUITE_WEP40);
4737 else
4738 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
4739 WLAN_CIPHER_SUITE_WEP104);
4740
4741 NLA_PUT_U8(msg, NL80211_KEY_IDX, i);
4742
4743 if (i == params->wep_tx_keyidx)
4744 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
4745
4746 nla_nest_end(msg, nl_key);
4747 }
4748 nla_nest_end(msg, nl_keys);
4749
4750 return 0;
4751
4752nla_put_failure:
4753 return -ENOBUFS;
4754}
4755
4756
4757static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
4758 const u8 *addr, int cmd, u16 reason_code,
4759 int local_state_change)
4760{
4761 int ret = -1;
4762 struct nl_msg *msg;
4763
4764 msg = nlmsg_alloc();
4765 if (!msg)
4766 return -1;
4767
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004768 nl80211_cmd(drv, msg, 0, cmd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004769
4770 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
4771 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason_code);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004772 if (addr)
4773 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004774 if (local_state_change)
4775 NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE);
4776
4777 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4778 msg = NULL;
4779 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004780 wpa_dbg(drv->ctx, MSG_DEBUG,
4781 "nl80211: MLME command failed: reason=%u ret=%d (%s)",
4782 reason_code, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004783 goto nla_put_failure;
4784 }
4785 ret = 0;
4786
4787nla_put_failure:
4788 nlmsg_free(msg);
4789 return ret;
4790}
4791
4792
4793static int wpa_driver_nl80211_disconnect(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004794 int reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004795{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004796 wpa_printf(MSG_DEBUG, "%s(reason_code=%d)", __func__, reason_code);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004797 drv->associated = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004798 drv->ignore_next_local_disconnect = 0;
4799 /* Disconnect command doesn't need BSSID - it uses cached value */
4800 return wpa_driver_nl80211_mlme(drv, NULL, NL80211_CMD_DISCONNECT,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004801 reason_code, 0);
4802}
4803
4804
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004805static int wpa_driver_nl80211_deauthenticate(struct i802_bss *bss,
4806 const u8 *addr, int reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004807{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004808 struct wpa_driver_nl80211_data *drv = bss->drv;
4809 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME))
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004810 return wpa_driver_nl80211_disconnect(drv, reason_code);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004811 wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " reason_code=%d)",
4812 __func__, MAC2STR(addr), reason_code);
4813 drv->associated = 0;
4814 if (drv->nlmode == NL80211_IFTYPE_ADHOC)
4815 return nl80211_leave_ibss(drv);
4816 return wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DEAUTHENTICATE,
4817 reason_code, 0);
4818}
4819
4820
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004821static void nl80211_copy_auth_params(struct wpa_driver_nl80211_data *drv,
4822 struct wpa_driver_auth_params *params)
4823{
4824 int i;
4825
4826 drv->auth_freq = params->freq;
4827 drv->auth_alg = params->auth_alg;
4828 drv->auth_wep_tx_keyidx = params->wep_tx_keyidx;
4829 drv->auth_local_state_change = params->local_state_change;
4830 drv->auth_p2p = params->p2p;
4831
4832 if (params->bssid)
4833 os_memcpy(drv->auth_bssid_, params->bssid, ETH_ALEN);
4834 else
4835 os_memset(drv->auth_bssid_, 0, ETH_ALEN);
4836
4837 if (params->ssid) {
4838 os_memcpy(drv->auth_ssid, params->ssid, params->ssid_len);
4839 drv->auth_ssid_len = params->ssid_len;
4840 } else
4841 drv->auth_ssid_len = 0;
4842
4843
4844 os_free(drv->auth_ie);
4845 drv->auth_ie = NULL;
4846 drv->auth_ie_len = 0;
4847 if (params->ie) {
4848 drv->auth_ie = os_malloc(params->ie_len);
4849 if (drv->auth_ie) {
4850 os_memcpy(drv->auth_ie, params->ie, params->ie_len);
4851 drv->auth_ie_len = params->ie_len;
4852 }
4853 }
4854
4855 for (i = 0; i < 4; i++) {
4856 if (params->wep_key[i] && params->wep_key_len[i] &&
4857 params->wep_key_len[i] <= 16) {
4858 os_memcpy(drv->auth_wep_key[i], params->wep_key[i],
4859 params->wep_key_len[i]);
4860 drv->auth_wep_key_len[i] = params->wep_key_len[i];
4861 } else
4862 drv->auth_wep_key_len[i] = 0;
4863 }
4864}
4865
4866
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004867static int wpa_driver_nl80211_authenticate(
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004868 struct i802_bss *bss, struct wpa_driver_auth_params *params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004869{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004870 struct wpa_driver_nl80211_data *drv = bss->drv;
4871 int ret = -1, i;
4872 struct nl_msg *msg;
4873 enum nl80211_auth_type type;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004874 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004875 int count = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004876 int is_retry;
4877
4878 is_retry = drv->retry_auth;
4879 drv->retry_auth = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004880
4881 drv->associated = 0;
4882 os_memset(drv->auth_bssid, 0, ETH_ALEN);
4883 /* FIX: IBSS mode */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004884 nlmode = params->p2p ?
4885 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
4886 if (drv->nlmode != nlmode &&
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004887 wpa_driver_nl80211_set_mode(bss, nlmode) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004888 return -1;
4889
4890retry:
4891 msg = nlmsg_alloc();
4892 if (!msg)
4893 return -1;
4894
4895 wpa_printf(MSG_DEBUG, "nl80211: Authenticate (ifindex=%d)",
4896 drv->ifindex);
4897
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004898 nl80211_cmd(drv, msg, 0, NL80211_CMD_AUTHENTICATE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004899
4900 for (i = 0; i < 4; i++) {
4901 if (!params->wep_key[i])
4902 continue;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004903 wpa_driver_nl80211_set_key(bss->ifname, bss, WPA_ALG_WEP,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004904 NULL, i,
4905 i == params->wep_tx_keyidx, NULL, 0,
4906 params->wep_key[i],
4907 params->wep_key_len[i]);
4908 if (params->wep_tx_keyidx != i)
4909 continue;
4910 if (nl_add_key(msg, WPA_ALG_WEP, i, 1, NULL, 0,
4911 params->wep_key[i], params->wep_key_len[i])) {
4912 nlmsg_free(msg);
4913 return -1;
4914 }
4915 }
4916
4917 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
4918 if (params->bssid) {
4919 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
4920 MAC2STR(params->bssid));
4921 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
4922 }
4923 if (params->freq) {
4924 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
4925 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
4926 }
4927 if (params->ssid) {
4928 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
4929 params->ssid, params->ssid_len);
4930 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
4931 params->ssid);
4932 }
4933 wpa_hexdump(MSG_DEBUG, " * IEs", params->ie, params->ie_len);
4934 if (params->ie)
4935 NLA_PUT(msg, NL80211_ATTR_IE, params->ie_len, params->ie);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004936 if (params->sae_data) {
4937 wpa_hexdump(MSG_DEBUG, " * SAE data", params->sae_data,
4938 params->sae_data_len);
4939 NLA_PUT(msg, NL80211_ATTR_SAE_DATA, params->sae_data_len,
4940 params->sae_data);
4941 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004942 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
4943 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
4944 else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
4945 type = NL80211_AUTHTYPE_SHARED_KEY;
4946 else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
4947 type = NL80211_AUTHTYPE_NETWORK_EAP;
4948 else if (params->auth_alg & WPA_AUTH_ALG_FT)
4949 type = NL80211_AUTHTYPE_FT;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004950 else if (params->auth_alg & WPA_AUTH_ALG_SAE)
4951 type = NL80211_AUTHTYPE_SAE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004952 else
4953 goto nla_put_failure;
4954 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
4955 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
4956 if (params->local_state_change) {
4957 wpa_printf(MSG_DEBUG, " * Local state change only");
4958 NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE);
4959 }
4960
4961 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4962 msg = NULL;
4963 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004964 wpa_dbg(drv->ctx, MSG_DEBUG,
4965 "nl80211: MLME command failed (auth): ret=%d (%s)",
4966 ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004967 count++;
4968 if (ret == -EALREADY && count == 1 && params->bssid &&
4969 !params->local_state_change) {
4970 /*
4971 * mac80211 does not currently accept new
4972 * authentication if we are already authenticated. As a
4973 * workaround, force deauthentication and try again.
4974 */
4975 wpa_printf(MSG_DEBUG, "nl80211: Retry authentication "
4976 "after forced deauthentication");
4977 wpa_driver_nl80211_deauthenticate(
4978 bss, params->bssid,
4979 WLAN_REASON_PREV_AUTH_NOT_VALID);
4980 nlmsg_free(msg);
4981 goto retry;
4982 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004983
4984 if (ret == -ENOENT && params->freq && !is_retry) {
4985 /*
4986 * cfg80211 has likely expired the BSS entry even
4987 * though it was previously available in our internal
4988 * BSS table. To recover quickly, start a single
4989 * channel scan on the specified channel.
4990 */
4991 struct wpa_driver_scan_params scan;
4992 int freqs[2];
4993
4994 os_memset(&scan, 0, sizeof(scan));
4995 scan.num_ssids = 1;
4996 if (params->ssid) {
4997 scan.ssids[0].ssid = params->ssid;
4998 scan.ssids[0].ssid_len = params->ssid_len;
4999 }
5000 freqs[0] = params->freq;
5001 freqs[1] = 0;
5002 scan.freqs = freqs;
5003 wpa_printf(MSG_DEBUG, "nl80211: Trigger single "
5004 "channel scan to refresh cfg80211 BSS "
5005 "entry");
5006 ret = wpa_driver_nl80211_scan(bss, &scan);
5007 if (ret == 0) {
5008 nl80211_copy_auth_params(drv, params);
5009 drv->scan_for_auth = 1;
5010 }
5011 } else if (is_retry) {
5012 /*
5013 * Need to indicate this with an event since the return
5014 * value from the retry is not delivered to core code.
5015 */
5016 union wpa_event_data event;
5017 wpa_printf(MSG_DEBUG, "nl80211: Authentication retry "
5018 "failed");
5019 os_memset(&event, 0, sizeof(event));
5020 os_memcpy(event.timeout_event.addr, drv->auth_bssid_,
5021 ETH_ALEN);
5022 wpa_supplicant_event(drv->ctx, EVENT_AUTH_TIMED_OUT,
5023 &event);
5024 }
5025
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005026 goto nla_put_failure;
5027 }
5028 ret = 0;
5029 wpa_printf(MSG_DEBUG, "nl80211: Authentication request send "
5030 "successfully");
5031
5032nla_put_failure:
5033 nlmsg_free(msg);
5034 return ret;
5035}
5036
5037
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005038static int wpa_driver_nl80211_authenticate_retry(
5039 struct wpa_driver_nl80211_data *drv)
5040{
5041 struct wpa_driver_auth_params params;
5042 struct i802_bss *bss = &drv->first_bss;
5043 int i;
5044
5045 wpa_printf(MSG_DEBUG, "nl80211: Try to authenticate again");
5046
5047 os_memset(&params, 0, sizeof(params));
5048 params.freq = drv->auth_freq;
5049 params.auth_alg = drv->auth_alg;
5050 params.wep_tx_keyidx = drv->auth_wep_tx_keyidx;
5051 params.local_state_change = drv->auth_local_state_change;
5052 params.p2p = drv->auth_p2p;
5053
5054 if (!is_zero_ether_addr(drv->auth_bssid_))
5055 params.bssid = drv->auth_bssid_;
5056
5057 if (drv->auth_ssid_len) {
5058 params.ssid = drv->auth_ssid;
5059 params.ssid_len = drv->auth_ssid_len;
5060 }
5061
5062 params.ie = drv->auth_ie;
5063 params.ie_len = drv->auth_ie_len;
5064
5065 for (i = 0; i < 4; i++) {
5066 if (drv->auth_wep_key_len[i]) {
5067 params.wep_key[i] = drv->auth_wep_key[i];
5068 params.wep_key_len[i] = drv->auth_wep_key_len[i];
5069 }
5070 }
5071
5072 drv->retry_auth = 1;
5073 return wpa_driver_nl80211_authenticate(bss, &params);
5074}
5075
5076
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005077struct phy_info_arg {
5078 u16 *num_modes;
5079 struct hostapd_hw_modes *modes;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005080 int last_mode, last_chan_idx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005081};
5082
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005083static void phy_info_ht_capa(struct hostapd_hw_modes *mode, struct nlattr *capa,
5084 struct nlattr *ampdu_factor,
5085 struct nlattr *ampdu_density,
5086 struct nlattr *mcs_set)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005087{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005088 if (capa)
5089 mode->ht_capab = nla_get_u16(capa);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005090
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005091 if (ampdu_factor)
5092 mode->a_mpdu_params |= nla_get_u8(ampdu_factor) & 0x03;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005093
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005094 if (ampdu_density)
5095 mode->a_mpdu_params |= nla_get_u8(ampdu_density) << 2;
5096
5097 if (mcs_set && nla_len(mcs_set) >= 16) {
5098 u8 *mcs;
5099 mcs = nla_data(mcs_set);
5100 os_memcpy(mode->mcs_set, mcs, 16);
5101 }
5102}
5103
5104
5105static void phy_info_vht_capa(struct hostapd_hw_modes *mode,
5106 struct nlattr *capa,
5107 struct nlattr *mcs_set)
5108{
5109 if (capa)
5110 mode->vht_capab = nla_get_u32(capa);
5111
5112 if (mcs_set && nla_len(mcs_set) >= 8) {
5113 u8 *mcs;
5114 mcs = nla_data(mcs_set);
5115 os_memcpy(mode->vht_mcs_set, mcs, 8);
5116 }
5117}
5118
5119
5120static void phy_info_freq(struct hostapd_hw_modes *mode,
5121 struct hostapd_channel_data *chan,
5122 struct nlattr *tb_freq[])
5123{
Dmitry Shmidt4b060592013-04-29 16:42:49 -07005124 u8 channel;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005125 chan->freq = nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_FREQ]);
5126 chan->flag = 0;
Dmitry Shmidt4b060592013-04-29 16:42:49 -07005127 if (ieee80211_freq_to_chan(chan->freq, &channel) != NUM_HOSTAPD_MODES)
5128 chan->chan = channel;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005129
5130 if (tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
5131 chan->flag |= HOSTAPD_CHAN_DISABLED;
5132 if (tb_freq[NL80211_FREQUENCY_ATTR_PASSIVE_SCAN])
5133 chan->flag |= HOSTAPD_CHAN_PASSIVE_SCAN;
5134 if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IBSS])
5135 chan->flag |= HOSTAPD_CHAN_NO_IBSS;
5136 if (tb_freq[NL80211_FREQUENCY_ATTR_RADAR])
5137 chan->flag |= HOSTAPD_CHAN_RADAR;
5138
5139 if (tb_freq[NL80211_FREQUENCY_ATTR_MAX_TX_POWER] &&
5140 !tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
5141 chan->max_tx_power = nla_get_u32(
5142 tb_freq[NL80211_FREQUENCY_ATTR_MAX_TX_POWER]) / 100;
5143}
5144
5145
5146static int phy_info_freqs(struct phy_info_arg *phy_info,
5147 struct hostapd_hw_modes *mode, struct nlattr *tb)
5148{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005149 static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
5150 [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
5151 [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
5152 [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] = { .type = NLA_FLAG },
5153 [NL80211_FREQUENCY_ATTR_NO_IBSS] = { .type = NLA_FLAG },
5154 [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
5155 [NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32 },
5156 };
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005157 int new_channels = 0;
5158 struct hostapd_channel_data *channel;
5159 struct nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005160 struct nlattr *nl_freq;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005161 int rem_freq, idx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005162
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005163 if (tb == NULL)
5164 return NL_OK;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005165
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005166 nla_for_each_nested(nl_freq, tb, rem_freq) {
5167 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX,
5168 nla_data(nl_freq), nla_len(nl_freq), freq_policy);
5169 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
5170 continue;
5171 new_channels++;
5172 }
5173
5174 channel = os_realloc_array(mode->channels,
5175 mode->num_channels + new_channels,
5176 sizeof(struct hostapd_channel_data));
5177 if (!channel)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005178 return NL_SKIP;
5179
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005180 mode->channels = channel;
5181 mode->num_channels += new_channels;
5182
5183 idx = phy_info->last_chan_idx;
5184
5185 nla_for_each_nested(nl_freq, tb, rem_freq) {
5186 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX,
5187 nla_data(nl_freq), nla_len(nl_freq), freq_policy);
5188 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
5189 continue;
5190 phy_info_freq(mode, &mode->channels[idx], tb_freq);
5191 idx++;
5192 }
5193 phy_info->last_chan_idx = idx;
5194
5195 return NL_OK;
5196}
5197
5198
5199static int phy_info_rates(struct hostapd_hw_modes *mode, struct nlattr *tb)
5200{
5201 static struct nla_policy rate_policy[NL80211_BITRATE_ATTR_MAX + 1] = {
5202 [NL80211_BITRATE_ATTR_RATE] = { .type = NLA_U32 },
5203 [NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE] =
5204 { .type = NLA_FLAG },
5205 };
5206 struct nlattr *tb_rate[NL80211_BITRATE_ATTR_MAX + 1];
5207 struct nlattr *nl_rate;
5208 int rem_rate, idx;
5209
5210 if (tb == NULL)
5211 return NL_OK;
5212
5213 nla_for_each_nested(nl_rate, tb, rem_rate) {
5214 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX,
5215 nla_data(nl_rate), nla_len(nl_rate),
5216 rate_policy);
5217 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
5218 continue;
5219 mode->num_rates++;
5220 }
5221
5222 mode->rates = os_calloc(mode->num_rates, sizeof(int));
5223 if (!mode->rates)
5224 return NL_SKIP;
5225
5226 idx = 0;
5227
5228 nla_for_each_nested(nl_rate, tb, rem_rate) {
5229 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX,
5230 nla_data(nl_rate), nla_len(nl_rate),
5231 rate_policy);
5232 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
5233 continue;
5234 mode->rates[idx] = nla_get_u32(
5235 tb_rate[NL80211_BITRATE_ATTR_RATE]);
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005236 idx++;
5237 }
5238
5239 return NL_OK;
5240}
5241
5242
5243static int phy_info_band(struct phy_info_arg *phy_info, struct nlattr *nl_band)
5244{
5245 struct nlattr *tb_band[NL80211_BAND_ATTR_MAX + 1];
5246 struct hostapd_hw_modes *mode;
5247 int ret;
5248
5249 if (phy_info->last_mode != nl_band->nla_type) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005250 mode = os_realloc_array(phy_info->modes,
5251 *phy_info->num_modes + 1,
5252 sizeof(*mode));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005253 if (!mode)
5254 return NL_SKIP;
5255 phy_info->modes = mode;
5256
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005257 mode = &phy_info->modes[*(phy_info->num_modes)];
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005258 os_memset(mode, 0, sizeof(*mode));
5259 mode->mode = NUM_HOSTAPD_MODES;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005260 mode->flags = HOSTAPD_MODE_FLAG_HT_INFO_KNOWN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005261 *(phy_info->num_modes) += 1;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005262 phy_info->last_mode = nl_band->nla_type;
5263 phy_info->last_chan_idx = 0;
5264 } else
5265 mode = &phy_info->modes[*(phy_info->num_modes) - 1];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005266
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005267 nla_parse(tb_band, NL80211_BAND_ATTR_MAX, nla_data(nl_band),
5268 nla_len(nl_band), NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005269
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005270 phy_info_ht_capa(mode, tb_band[NL80211_BAND_ATTR_HT_CAPA],
5271 tb_band[NL80211_BAND_ATTR_HT_AMPDU_FACTOR],
5272 tb_band[NL80211_BAND_ATTR_HT_AMPDU_DENSITY],
5273 tb_band[NL80211_BAND_ATTR_HT_MCS_SET]);
5274 phy_info_vht_capa(mode, tb_band[NL80211_BAND_ATTR_VHT_CAPA],
5275 tb_band[NL80211_BAND_ATTR_VHT_MCS_SET]);
5276 ret = phy_info_freqs(phy_info, mode, tb_band[NL80211_BAND_ATTR_FREQS]);
5277 if (ret != NL_OK)
5278 return ret;
5279 ret = phy_info_rates(mode, tb_band[NL80211_BAND_ATTR_RATES]);
5280 if (ret != NL_OK)
5281 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005282
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005283 return NL_OK;
5284}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005285
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005286
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005287static int phy_info_handler(struct nl_msg *msg, void *arg)
5288{
5289 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
5290 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
5291 struct phy_info_arg *phy_info = arg;
5292 struct nlattr *nl_band;
5293 int rem_band;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005294
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005295 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
5296 genlmsg_attrlen(gnlh, 0), NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005297
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005298 if (!tb_msg[NL80211_ATTR_WIPHY_BANDS])
5299 return NL_SKIP;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005300
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005301 nla_for_each_nested(nl_band, tb_msg[NL80211_ATTR_WIPHY_BANDS], rem_band)
5302 {
5303 int res = phy_info_band(phy_info, nl_band);
5304 if (res != NL_OK)
5305 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005306 }
5307
5308 return NL_SKIP;
5309}
5310
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005311
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005312static struct hostapd_hw_modes *
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005313wpa_driver_nl80211_postprocess_modes(struct hostapd_hw_modes *modes,
5314 u16 *num_modes)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005315{
5316 u16 m;
5317 struct hostapd_hw_modes *mode11g = NULL, *nmodes, *mode;
5318 int i, mode11g_idx = -1;
5319
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005320 /* heuristic to set up modes */
5321 for (m = 0; m < *num_modes; m++) {
5322 if (!modes[m].num_channels)
5323 continue;
5324 if (modes[m].channels[0].freq < 4000) {
5325 modes[m].mode = HOSTAPD_MODE_IEEE80211B;
5326 for (i = 0; i < modes[m].num_rates; i++) {
5327 if (modes[m].rates[i] > 200) {
5328 modes[m].mode = HOSTAPD_MODE_IEEE80211G;
5329 break;
5330 }
5331 }
5332 } else if (modes[m].channels[0].freq > 50000)
5333 modes[m].mode = HOSTAPD_MODE_IEEE80211AD;
5334 else
5335 modes[m].mode = HOSTAPD_MODE_IEEE80211A;
5336 }
5337
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005338 /* If only 802.11g mode is included, use it to construct matching
5339 * 802.11b mode data. */
5340
5341 for (m = 0; m < *num_modes; m++) {
5342 if (modes[m].mode == HOSTAPD_MODE_IEEE80211B)
5343 return modes; /* 802.11b already included */
5344 if (modes[m].mode == HOSTAPD_MODE_IEEE80211G)
5345 mode11g_idx = m;
5346 }
5347
5348 if (mode11g_idx < 0)
5349 return modes; /* 2.4 GHz band not supported at all */
5350
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005351 nmodes = os_realloc_array(modes, *num_modes + 1, sizeof(*nmodes));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005352 if (nmodes == NULL)
5353 return modes; /* Could not add 802.11b mode */
5354
5355 mode = &nmodes[*num_modes];
5356 os_memset(mode, 0, sizeof(*mode));
5357 (*num_modes)++;
5358 modes = nmodes;
5359
5360 mode->mode = HOSTAPD_MODE_IEEE80211B;
5361
5362 mode11g = &modes[mode11g_idx];
5363 mode->num_channels = mode11g->num_channels;
5364 mode->channels = os_malloc(mode11g->num_channels *
5365 sizeof(struct hostapd_channel_data));
5366 if (mode->channels == NULL) {
5367 (*num_modes)--;
5368 return modes; /* Could not add 802.11b mode */
5369 }
5370 os_memcpy(mode->channels, mode11g->channels,
5371 mode11g->num_channels * sizeof(struct hostapd_channel_data));
5372
5373 mode->num_rates = 0;
5374 mode->rates = os_malloc(4 * sizeof(int));
5375 if (mode->rates == NULL) {
5376 os_free(mode->channels);
5377 (*num_modes)--;
5378 return modes; /* Could not add 802.11b mode */
5379 }
5380
5381 for (i = 0; i < mode11g->num_rates; i++) {
5382 if (mode11g->rates[i] != 10 && mode11g->rates[i] != 20 &&
5383 mode11g->rates[i] != 55 && mode11g->rates[i] != 110)
5384 continue;
5385 mode->rates[mode->num_rates] = mode11g->rates[i];
5386 mode->num_rates++;
5387 if (mode->num_rates == 4)
5388 break;
5389 }
5390
5391 if (mode->num_rates == 0) {
5392 os_free(mode->channels);
5393 os_free(mode->rates);
5394 (*num_modes)--;
5395 return modes; /* No 802.11b rates */
5396 }
5397
5398 wpa_printf(MSG_DEBUG, "nl80211: Added 802.11b mode based on 802.11g "
5399 "information");
5400
5401 return modes;
5402}
5403
5404
5405static void nl80211_set_ht40_mode(struct hostapd_hw_modes *mode, int start,
5406 int end)
5407{
5408 int c;
5409
5410 for (c = 0; c < mode->num_channels; c++) {
5411 struct hostapd_channel_data *chan = &mode->channels[c];
5412 if (chan->freq - 10 >= start && chan->freq + 10 <= end)
5413 chan->flag |= HOSTAPD_CHAN_HT40;
5414 }
5415}
5416
5417
5418static void nl80211_set_ht40_mode_sec(struct hostapd_hw_modes *mode, int start,
5419 int end)
5420{
5421 int c;
5422
5423 for (c = 0; c < mode->num_channels; c++) {
5424 struct hostapd_channel_data *chan = &mode->channels[c];
5425 if (!(chan->flag & HOSTAPD_CHAN_HT40))
5426 continue;
5427 if (chan->freq - 30 >= start && chan->freq - 10 <= end)
5428 chan->flag |= HOSTAPD_CHAN_HT40MINUS;
5429 if (chan->freq + 10 >= start && chan->freq + 30 <= end)
5430 chan->flag |= HOSTAPD_CHAN_HT40PLUS;
5431 }
5432}
5433
5434
5435static void nl80211_reg_rule_ht40(struct nlattr *tb[],
5436 struct phy_info_arg *results)
5437{
5438 u32 start, end, max_bw;
5439 u16 m;
5440
5441 if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
5442 tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
5443 tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
5444 return;
5445
5446 start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
5447 end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
5448 max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
5449
5450 wpa_printf(MSG_DEBUG, "nl80211: %u-%u @ %u MHz",
5451 start, end, max_bw);
5452 if (max_bw < 40)
5453 return;
5454
5455 for (m = 0; m < *results->num_modes; m++) {
5456 if (!(results->modes[m].ht_capab &
5457 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
5458 continue;
5459 nl80211_set_ht40_mode(&results->modes[m], start, end);
5460 }
5461}
5462
5463
5464static void nl80211_reg_rule_sec(struct nlattr *tb[],
5465 struct phy_info_arg *results)
5466{
5467 u32 start, end, max_bw;
5468 u16 m;
5469
5470 if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
5471 tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
5472 tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
5473 return;
5474
5475 start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
5476 end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
5477 max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
5478
5479 if (max_bw < 20)
5480 return;
5481
5482 for (m = 0; m < *results->num_modes; m++) {
5483 if (!(results->modes[m].ht_capab &
5484 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
5485 continue;
5486 nl80211_set_ht40_mode_sec(&results->modes[m], start, end);
5487 }
5488}
5489
5490
5491static int nl80211_get_reg(struct nl_msg *msg, void *arg)
5492{
5493 struct phy_info_arg *results = arg;
5494 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
5495 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
5496 struct nlattr *nl_rule;
5497 struct nlattr *tb_rule[NL80211_FREQUENCY_ATTR_MAX + 1];
5498 int rem_rule;
5499 static struct nla_policy reg_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
5500 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
5501 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
5502 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
5503 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
5504 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
5505 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
5506 };
5507
5508 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
5509 genlmsg_attrlen(gnlh, 0), NULL);
5510 if (!tb_msg[NL80211_ATTR_REG_ALPHA2] ||
5511 !tb_msg[NL80211_ATTR_REG_RULES]) {
5512 wpa_printf(MSG_DEBUG, "nl80211: No regulatory information "
5513 "available");
5514 return NL_SKIP;
5515 }
5516
5517 wpa_printf(MSG_DEBUG, "nl80211: Regulatory information - country=%s",
5518 (char *) nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]));
5519
5520 nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
5521 {
5522 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
5523 nla_data(nl_rule), nla_len(nl_rule), reg_policy);
5524 nl80211_reg_rule_ht40(tb_rule, results);
5525 }
5526
5527 nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
5528 {
5529 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
5530 nla_data(nl_rule), nla_len(nl_rule), reg_policy);
5531 nl80211_reg_rule_sec(tb_rule, results);
5532 }
5533
5534 return NL_SKIP;
5535}
5536
5537
5538static int nl80211_set_ht40_flags(struct wpa_driver_nl80211_data *drv,
5539 struct phy_info_arg *results)
5540{
5541 struct nl_msg *msg;
5542
5543 msg = nlmsg_alloc();
5544 if (!msg)
5545 return -ENOMEM;
5546
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005547 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005548 return send_and_recv_msgs(drv, msg, nl80211_get_reg, results);
5549}
5550
5551
5552static struct hostapd_hw_modes *
5553wpa_driver_nl80211_get_hw_feature_data(void *priv, u16 *num_modes, u16 *flags)
5554{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005555 u32 feat;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005556 struct i802_bss *bss = priv;
5557 struct wpa_driver_nl80211_data *drv = bss->drv;
5558 struct nl_msg *msg;
5559 struct phy_info_arg result = {
5560 .num_modes = num_modes,
5561 .modes = NULL,
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005562 .last_mode = -1,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005563 };
5564
5565 *num_modes = 0;
5566 *flags = 0;
5567
5568 msg = nlmsg_alloc();
5569 if (!msg)
5570 return NULL;
5571
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005572 feat = get_nl80211_protocol_features(drv);
5573 if (feat & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP)
5574 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_WIPHY);
5575 else
5576 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005577
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005578 NLA_PUT_FLAG(msg, NL80211_ATTR_SPLIT_WIPHY_DUMP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005579 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
5580
5581 if (send_and_recv_msgs(drv, msg, phy_info_handler, &result) == 0) {
5582 nl80211_set_ht40_flags(drv, &result);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005583 return wpa_driver_nl80211_postprocess_modes(result.modes,
5584 num_modes);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005585 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005586 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005587 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005588 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005589 return NULL;
5590}
5591
5592
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005593static int wpa_driver_nl80211_send_mntr(struct wpa_driver_nl80211_data *drv,
5594 const void *data, size_t len,
5595 int encrypt, int noack)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005596{
5597 __u8 rtap_hdr[] = {
5598 0x00, 0x00, /* radiotap version */
5599 0x0e, 0x00, /* radiotap length */
5600 0x02, 0xc0, 0x00, 0x00, /* bmap: flags, tx and rx flags */
5601 IEEE80211_RADIOTAP_F_FRAG, /* F_FRAG (fragment if required) */
5602 0x00, /* padding */
5603 0x00, 0x00, /* RX and TX flags to indicate that */
5604 0x00, 0x00, /* this is the injected frame directly */
5605 };
5606 struct iovec iov[2] = {
5607 {
5608 .iov_base = &rtap_hdr,
5609 .iov_len = sizeof(rtap_hdr),
5610 },
5611 {
5612 .iov_base = (void *) data,
5613 .iov_len = len,
5614 }
5615 };
5616 struct msghdr msg = {
5617 .msg_name = NULL,
5618 .msg_namelen = 0,
5619 .msg_iov = iov,
5620 .msg_iovlen = 2,
5621 .msg_control = NULL,
5622 .msg_controllen = 0,
5623 .msg_flags = 0,
5624 };
5625 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005626 u16 txflags = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005627
5628 if (encrypt)
5629 rtap_hdr[8] |= IEEE80211_RADIOTAP_F_WEP;
5630
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07005631 if (drv->monitor_sock < 0) {
5632 wpa_printf(MSG_DEBUG, "nl80211: No monitor socket available "
5633 "for %s", __func__);
5634 return -1;
5635 }
5636
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005637 if (noack)
5638 txflags |= IEEE80211_RADIOTAP_F_TX_NOACK;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005639 WPA_PUT_LE16(&rtap_hdr[12], txflags);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005640
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005641 res = sendmsg(drv->monitor_sock, &msg, 0);
5642 if (res < 0) {
5643 wpa_printf(MSG_INFO, "nl80211: sendmsg: %s", strerror(errno));
5644 return -1;
5645 }
5646 return 0;
5647}
5648
5649
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005650static int wpa_driver_nl80211_send_frame(struct i802_bss *bss,
5651 const void *data, size_t len,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005652 int encrypt, int noack,
5653 unsigned int freq, int no_cck,
5654 int offchanok, unsigned int wait_time)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005655{
5656 struct wpa_driver_nl80211_data *drv = bss->drv;
5657 u64 cookie;
5658
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005659 if (freq == 0)
5660 freq = bss->freq;
5661
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005662 if (drv->use_monitor)
5663 return wpa_driver_nl80211_send_mntr(drv, data, len,
5664 encrypt, noack);
5665
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005666 return nl80211_send_frame_cmd(bss, freq, wait_time, data, len,
5667 &cookie, no_cck, noack, offchanok);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005668}
5669
5670
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005671static int wpa_driver_nl80211_send_mlme(struct i802_bss *bss, const u8 *data,
5672 size_t data_len, int noack,
5673 unsigned int freq, int no_cck,
5674 int offchanok,
5675 unsigned int wait_time)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005676{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005677 struct wpa_driver_nl80211_data *drv = bss->drv;
5678 struct ieee80211_mgmt *mgmt;
5679 int encrypt = 1;
5680 u16 fc;
5681
5682 mgmt = (struct ieee80211_mgmt *) data;
5683 fc = le_to_host16(mgmt->frame_control);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005684
5685 if (is_sta_interface(drv->nlmode) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005686 WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
5687 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_RESP) {
5688 /*
5689 * The use of last_mgmt_freq is a bit of a hack,
5690 * but it works due to the single-threaded nature
5691 * of wpa_supplicant.
5692 */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005693 if (freq == 0)
5694 freq = drv->last_mgmt_freq;
5695 return nl80211_send_frame_cmd(bss, freq, 0,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005696 data, data_len, NULL, 1, noack,
5697 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005698 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005699
5700 if (drv->device_ap_sme && is_ap_interface(drv->nlmode)) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005701 if (freq == 0)
5702 freq = bss->freq;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005703 return nl80211_send_frame_cmd(bss, freq,
5704 (int) freq == bss->freq ? 0 :
5705 wait_time,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005706 data, data_len,
5707 &drv->send_action_cookie,
5708 no_cck, noack, offchanok);
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07005709 }
Dmitry Shmidtb638fe72012-03-20 12:51:25 -07005710
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005711 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
5712 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) {
5713 /*
5714 * Only one of the authentication frame types is encrypted.
5715 * In order for static WEP encryption to work properly (i.e.,
5716 * to not encrypt the frame), we need to tell mac80211 about
5717 * the frames that must not be encrypted.
5718 */
5719 u16 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
5720 u16 auth_trans = le_to_host16(mgmt->u.auth.auth_transaction);
5721 if (auth_alg != WLAN_AUTH_SHARED_KEY || auth_trans != 3)
5722 encrypt = 0;
5723 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005724
5725 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005726 noack, freq, no_cck, offchanok,
5727 wait_time);
5728}
5729
5730
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005731static int nl80211_set_bss(struct i802_bss *bss, int cts, int preamble,
5732 int slot, int ht_opmode, int ap_isolate,
5733 int *basic_rates)
5734{
5735 struct wpa_driver_nl80211_data *drv = bss->drv;
5736 struct nl_msg *msg;
5737
5738 msg = nlmsg_alloc();
5739 if (!msg)
5740 return -ENOMEM;
5741
5742 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_BSS);
5743
5744 if (cts >= 0)
5745 NLA_PUT_U8(msg, NL80211_ATTR_BSS_CTS_PROT, cts);
5746 if (preamble >= 0)
5747 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble);
5748 if (slot >= 0)
5749 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot);
5750 if (ht_opmode >= 0)
5751 NLA_PUT_U16(msg, NL80211_ATTR_BSS_HT_OPMODE, ht_opmode);
5752 if (ap_isolate >= 0)
5753 NLA_PUT_U8(msg, NL80211_ATTR_AP_ISOLATE, ap_isolate);
5754
5755 if (basic_rates) {
5756 u8 rates[NL80211_MAX_SUPP_RATES];
5757 u8 rates_len = 0;
5758 int i;
5759
5760 for (i = 0; i < NL80211_MAX_SUPP_RATES && basic_rates[i] >= 0;
5761 i++)
5762 rates[rates_len++] = basic_rates[i] / 5;
5763
5764 NLA_PUT(msg, NL80211_ATTR_BSS_BASIC_RATES, rates_len, rates);
5765 }
5766
5767 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
5768
5769 return send_and_recv_msgs(drv, msg, NULL, NULL);
5770 nla_put_failure:
5771 nlmsg_free(msg);
5772 return -ENOBUFS;
5773}
5774
5775
5776static int wpa_driver_nl80211_set_ap(void *priv,
5777 struct wpa_driver_ap_params *params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005778{
5779 struct i802_bss *bss = priv;
5780 struct wpa_driver_nl80211_data *drv = bss->drv;
5781 struct nl_msg *msg;
5782 u8 cmd = NL80211_CMD_NEW_BEACON;
5783 int ret;
5784 int beacon_set;
5785 int ifindex = if_nametoindex(bss->ifname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005786 int num_suites;
5787 u32 suites[10];
5788 u32 ver;
5789
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07005790 beacon_set = bss->beacon_set;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005791
5792 msg = nlmsg_alloc();
5793 if (!msg)
5794 return -ENOMEM;
5795
5796 wpa_printf(MSG_DEBUG, "nl80211: Set beacon (beacon_set=%d)",
5797 beacon_set);
5798 if (beacon_set)
5799 cmd = NL80211_CMD_SET_BEACON;
5800
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005801 nl80211_cmd(drv, msg, 0, cmd);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005802 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon head",
5803 params->head, params->head_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005804 NLA_PUT(msg, NL80211_ATTR_BEACON_HEAD, params->head_len, params->head);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005805 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon tail",
5806 params->tail, params->tail_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005807 NLA_PUT(msg, NL80211_ATTR_BEACON_TAIL, params->tail_len, params->tail);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005808 wpa_printf(MSG_DEBUG, "nl80211: ifindex=%d", ifindex);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005809 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005810 wpa_printf(MSG_DEBUG, "nl80211: beacon_int=%d", params->beacon_int);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005811 NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL, params->beacon_int);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005812 wpa_printf(MSG_DEBUG, "nl80211: dtim_period=%d", params->dtim_period);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005813 NLA_PUT_U32(msg, NL80211_ATTR_DTIM_PERIOD, params->dtim_period);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005814 wpa_hexdump_ascii(MSG_DEBUG, "nl80211: ssid",
5815 params->ssid, params->ssid_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005816 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
5817 params->ssid);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005818 if (params->proberesp && params->proberesp_len) {
5819 wpa_hexdump(MSG_DEBUG, "nl80211: proberesp (offload)",
5820 params->proberesp, params->proberesp_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005821 NLA_PUT(msg, NL80211_ATTR_PROBE_RESP, params->proberesp_len,
5822 params->proberesp);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005823 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005824 switch (params->hide_ssid) {
5825 case NO_SSID_HIDING:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005826 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID not in use");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005827 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
5828 NL80211_HIDDEN_SSID_NOT_IN_USE);
5829 break;
5830 case HIDDEN_SSID_ZERO_LEN:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005831 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero len");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005832 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
5833 NL80211_HIDDEN_SSID_ZERO_LEN);
5834 break;
5835 case HIDDEN_SSID_ZERO_CONTENTS:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005836 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero contents");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005837 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
5838 NL80211_HIDDEN_SSID_ZERO_CONTENTS);
5839 break;
5840 }
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005841 wpa_printf(MSG_DEBUG, "nl80211: privacy=%d", params->privacy);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005842 if (params->privacy)
5843 NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005844 wpa_printf(MSG_DEBUG, "nl80211: auth_algs=0x%x", params->auth_algs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005845 if ((params->auth_algs & (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) ==
5846 (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) {
5847 /* Leave out the attribute */
5848 } else if (params->auth_algs & WPA_AUTH_ALG_SHARED)
5849 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE,
5850 NL80211_AUTHTYPE_SHARED_KEY);
5851 else
5852 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE,
5853 NL80211_AUTHTYPE_OPEN_SYSTEM);
5854
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005855 wpa_printf(MSG_DEBUG, "nl80211: wpa_version=0x%x", params->wpa_version);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005856 ver = 0;
5857 if (params->wpa_version & WPA_PROTO_WPA)
5858 ver |= NL80211_WPA_VERSION_1;
5859 if (params->wpa_version & WPA_PROTO_RSN)
5860 ver |= NL80211_WPA_VERSION_2;
5861 if (ver)
5862 NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver);
5863
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005864 wpa_printf(MSG_DEBUG, "nl80211: key_mgmt_suites=0x%x",
5865 params->key_mgmt_suites);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005866 num_suites = 0;
5867 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X)
5868 suites[num_suites++] = WLAN_AKM_SUITE_8021X;
5869 if (params->key_mgmt_suites & WPA_KEY_MGMT_PSK)
5870 suites[num_suites++] = WLAN_AKM_SUITE_PSK;
5871 if (num_suites) {
5872 NLA_PUT(msg, NL80211_ATTR_AKM_SUITES,
5873 num_suites * sizeof(u32), suites);
5874 }
5875
5876 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X &&
5877 params->pairwise_ciphers & (WPA_CIPHER_WEP104 | WPA_CIPHER_WEP40))
5878 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT);
5879
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005880 wpa_printf(MSG_DEBUG, "nl80211: pairwise_ciphers=0x%x",
5881 params->pairwise_ciphers);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005882 num_suites = 0;
5883 if (params->pairwise_ciphers & WPA_CIPHER_CCMP)
5884 suites[num_suites++] = WLAN_CIPHER_SUITE_CCMP;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005885 if (params->pairwise_ciphers & WPA_CIPHER_GCMP)
5886 suites[num_suites++] = WLAN_CIPHER_SUITE_GCMP;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005887 if (params->pairwise_ciphers & WPA_CIPHER_TKIP)
5888 suites[num_suites++] = WLAN_CIPHER_SUITE_TKIP;
5889 if (params->pairwise_ciphers & WPA_CIPHER_WEP104)
5890 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP104;
5891 if (params->pairwise_ciphers & WPA_CIPHER_WEP40)
5892 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP40;
5893 if (num_suites) {
5894 NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
5895 num_suites * sizeof(u32), suites);
5896 }
5897
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005898 wpa_printf(MSG_DEBUG, "nl80211: group_cipher=0x%x",
5899 params->group_cipher);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005900 switch (params->group_cipher) {
5901 case WPA_CIPHER_CCMP:
5902 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
5903 WLAN_CIPHER_SUITE_CCMP);
5904 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005905 case WPA_CIPHER_GCMP:
5906 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
5907 WLAN_CIPHER_SUITE_GCMP);
5908 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005909 case WPA_CIPHER_TKIP:
5910 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
5911 WLAN_CIPHER_SUITE_TKIP);
5912 break;
5913 case WPA_CIPHER_WEP104:
5914 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
5915 WLAN_CIPHER_SUITE_WEP104);
5916 break;
5917 case WPA_CIPHER_WEP40:
5918 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
5919 WLAN_CIPHER_SUITE_WEP40);
5920 break;
5921 }
5922
5923 if (params->beacon_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005924 wpa_hexdump_buf(MSG_DEBUG, "nl80211: beacon_ies",
5925 params->beacon_ies);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005926 NLA_PUT(msg, NL80211_ATTR_IE, wpabuf_len(params->beacon_ies),
5927 wpabuf_head(params->beacon_ies));
5928 }
5929 if (params->proberesp_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005930 wpa_hexdump_buf(MSG_DEBUG, "nl80211: proberesp_ies",
5931 params->proberesp_ies);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005932 NLA_PUT(msg, NL80211_ATTR_IE_PROBE_RESP,
5933 wpabuf_len(params->proberesp_ies),
5934 wpabuf_head(params->proberesp_ies));
5935 }
5936 if (params->assocresp_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005937 wpa_hexdump_buf(MSG_DEBUG, "nl80211: assocresp_ies",
5938 params->assocresp_ies);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005939 NLA_PUT(msg, NL80211_ATTR_IE_ASSOC_RESP,
5940 wpabuf_len(params->assocresp_ies),
5941 wpabuf_head(params->assocresp_ies));
5942 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005943
Dmitry Shmidt04949592012-07-19 12:16:46 -07005944 if (drv->capa.flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005945 wpa_printf(MSG_DEBUG, "nl80211: ap_max_inactivity=%d",
5946 params->ap_max_inactivity);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005947 NLA_PUT_U16(msg, NL80211_ATTR_INACTIVITY_TIMEOUT,
5948 params->ap_max_inactivity);
5949 }
5950
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005951 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5952 if (ret) {
5953 wpa_printf(MSG_DEBUG, "nl80211: Beacon set failed: %d (%s)",
5954 ret, strerror(-ret));
5955 } else {
5956 bss->beacon_set = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005957 nl80211_set_bss(bss, params->cts_protect, params->preamble,
5958 params->short_slot_time, params->ht_opmode,
5959 params->isolate, params->basic_rates);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005960 }
5961 return ret;
5962 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005963 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005964 return -ENOBUFS;
5965}
5966
5967
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005968static int wpa_driver_nl80211_set_freq(struct i802_bss *bss,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005969 struct hostapd_freq_params *freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005970{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005971 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005972 struct nl_msg *msg;
5973 int ret;
5974
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005975 wpa_printf(MSG_DEBUG, "nl80211: Set freq %d (ht_enabled=%d, vht_enabled=%d,"
5976 " bandwidth=%d MHz, cf1=%d MHz, cf2=%d MHz)",
5977 freq->freq, freq->ht_enabled, freq->vht_enabled,
5978 freq->bandwidth, freq->center_freq1, freq->center_freq2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005979 msg = nlmsg_alloc();
5980 if (!msg)
5981 return -1;
5982
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005983 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005984
5985 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005986 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq->freq);
5987 if (freq->vht_enabled) {
5988 switch (freq->bandwidth) {
5989 case 20:
5990 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
5991 NL80211_CHAN_WIDTH_20);
5992 break;
5993 case 40:
5994 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
5995 NL80211_CHAN_WIDTH_40);
5996 break;
5997 case 80:
5998 if (freq->center_freq2)
5999 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
6000 NL80211_CHAN_WIDTH_80P80);
6001 else
6002 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
6003 NL80211_CHAN_WIDTH_80);
6004 break;
6005 case 160:
6006 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
6007 NL80211_CHAN_WIDTH_160);
6008 break;
6009 default:
6010 return -1;
6011 }
6012 NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ1, freq->center_freq1);
6013 if (freq->center_freq2)
6014 NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ2,
6015 freq->center_freq2);
6016 } else if (freq->ht_enabled) {
6017 switch (freq->sec_channel_offset) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006018 case -1:
6019 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
6020 NL80211_CHAN_HT40MINUS);
6021 break;
6022 case 1:
6023 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
6024 NL80211_CHAN_HT40PLUS);
6025 break;
6026 default:
6027 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
6028 NL80211_CHAN_HT20);
6029 break;
6030 }
6031 }
6032
6033 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006034 msg = NULL;
6035 if (ret == 0) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006036 bss->freq = freq->freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006037 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006038 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006039 wpa_printf(MSG_DEBUG, "nl80211: Failed to set channel (freq=%d): "
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006040 "%d (%s)", freq->freq, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006041nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006042 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006043 return -1;
6044}
6045
6046
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006047static u32 sta_flags_nl80211(int flags)
6048{
6049 u32 f = 0;
6050
6051 if (flags & WPA_STA_AUTHORIZED)
6052 f |= BIT(NL80211_STA_FLAG_AUTHORIZED);
6053 if (flags & WPA_STA_WMM)
6054 f |= BIT(NL80211_STA_FLAG_WME);
6055 if (flags & WPA_STA_SHORT_PREAMBLE)
6056 f |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
6057 if (flags & WPA_STA_MFP)
6058 f |= BIT(NL80211_STA_FLAG_MFP);
6059 if (flags & WPA_STA_TDLS_PEER)
6060 f |= BIT(NL80211_STA_FLAG_TDLS_PEER);
6061
6062 return f;
6063}
6064
6065
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006066static int wpa_driver_nl80211_sta_add(void *priv,
6067 struct hostapd_sta_add_params *params)
6068{
6069 struct i802_bss *bss = priv;
6070 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006071 struct nl_msg *msg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006072 struct nl80211_sta_flag_update upd;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006073 int ret = -ENOBUFS;
6074
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006075 if ((params->flags & WPA_STA_TDLS_PEER) &&
6076 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
6077 return -EOPNOTSUPP;
6078
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006079 msg = nlmsg_alloc();
6080 if (!msg)
6081 return -ENOMEM;
6082
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006083 wpa_printf(MSG_DEBUG, "nl80211: %s STA " MACSTR,
6084 params->set ? "Set" : "Add", MAC2STR(params->addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006085 nl80211_cmd(drv, msg, 0, params->set ? NL80211_CMD_SET_STATION :
6086 NL80211_CMD_NEW_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006087
6088 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
6089 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006090 NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_RATES, params->supp_rates_len,
6091 params->supp_rates);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006092 wpa_hexdump(MSG_DEBUG, " * supported rates", params->supp_rates,
6093 params->supp_rates_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006094 if (!params->set) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006095 wpa_printf(MSG_DEBUG, " * aid=%u", params->aid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006096 NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, params->aid);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006097 wpa_printf(MSG_DEBUG, " * listen_interval=%u",
6098 params->listen_interval);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006099 NLA_PUT_U16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
6100 params->listen_interval);
6101 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006102 if (params->ht_capabilities) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006103 wpa_hexdump(MSG_DEBUG, " * ht_capabilities",
6104 (u8 *) params->ht_capabilities,
6105 sizeof(*params->ht_capabilities));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006106 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY,
6107 sizeof(*params->ht_capabilities),
6108 params->ht_capabilities);
6109 }
6110
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006111 if (params->vht_capabilities) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006112 wpa_hexdump(MSG_DEBUG, " * vht_capabilities",
6113 (u8 *) params->vht_capabilities,
6114 sizeof(*params->vht_capabilities));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006115 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY,
6116 sizeof(*params->vht_capabilities),
6117 params->vht_capabilities);
6118 }
6119
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006120 wpa_printf(MSG_DEBUG, " * capability=0x%x", params->capability);
6121 NLA_PUT_U16(msg, NL80211_ATTR_STA_CAPABILITY, params->capability);
6122
6123 if (params->ext_capab) {
6124 wpa_hexdump(MSG_DEBUG, " * ext_capab",
6125 params->ext_capab, params->ext_capab_len);
6126 NLA_PUT(msg, NL80211_ATTR_STA_EXT_CAPABILITY,
6127 params->ext_capab_len, params->ext_capab);
6128 }
6129
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006130 os_memset(&upd, 0, sizeof(upd));
6131 upd.mask = sta_flags_nl80211(params->flags);
6132 upd.set = upd.mask;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006133 wpa_printf(MSG_DEBUG, " * flags set=0x%x mask=0x%x",
6134 upd.set, upd.mask);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006135 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
6136
6137 if (params->flags & WPA_STA_WMM) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006138 struct nlattr *wme = nla_nest_start(msg, NL80211_ATTR_STA_WME);
6139
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006140 if (!wme)
6141 goto nla_put_failure;
6142
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006143 wpa_printf(MSG_DEBUG, " * qosinfo=0x%x", params->qosinfo);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006144 NLA_PUT_U8(msg, NL80211_STA_WME_UAPSD_QUEUES,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006145 params->qosinfo & WMM_QOSINFO_STA_AC_MASK);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006146 NLA_PUT_U8(msg, NL80211_STA_WME_MAX_SP,
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006147 (params->qosinfo >> WMM_QOSINFO_STA_SP_SHIFT) &
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006148 WMM_QOSINFO_STA_SP_MASK);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006149 nla_nest_end(msg, wme);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006150 }
6151
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006152 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006153 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006154 if (ret)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006155 wpa_printf(MSG_DEBUG, "nl80211: NL80211_CMD_%s_STATION "
6156 "result: %d (%s)", params->set ? "SET" : "NEW", ret,
6157 strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006158 if (ret == -EEXIST)
6159 ret = 0;
6160 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006161 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006162 return ret;
6163}
6164
6165
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006166static int wpa_driver_nl80211_sta_remove(struct i802_bss *bss, const u8 *addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006167{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006168 struct wpa_driver_nl80211_data *drv = bss->drv;
6169 struct nl_msg *msg;
6170 int ret;
6171
6172 msg = nlmsg_alloc();
6173 if (!msg)
6174 return -ENOMEM;
6175
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006176 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006177
6178 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
6179 if_nametoindex(bss->ifname));
6180 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
6181
6182 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6183 if (ret == -ENOENT)
6184 return 0;
6185 return ret;
6186 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006187 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006188 return -ENOBUFS;
6189}
6190
6191
6192static void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv,
6193 int ifidx)
6194{
6195 struct nl_msg *msg;
6196
6197 wpa_printf(MSG_DEBUG, "nl80211: Remove interface ifindex=%d", ifidx);
6198
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006199 /* stop listening for EAPOL on this interface */
6200 del_ifidx(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006201
6202 msg = nlmsg_alloc();
6203 if (!msg)
6204 goto nla_put_failure;
6205
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006206 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_INTERFACE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006207 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifidx);
6208
6209 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
6210 return;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006211 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006212 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006213 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006214 wpa_printf(MSG_ERROR, "Failed to remove interface (ifidx=%d)", ifidx);
6215}
6216
6217
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006218static const char * nl80211_iftype_str(enum nl80211_iftype mode)
6219{
6220 switch (mode) {
6221 case NL80211_IFTYPE_ADHOC:
6222 return "ADHOC";
6223 case NL80211_IFTYPE_STATION:
6224 return "STATION";
6225 case NL80211_IFTYPE_AP:
6226 return "AP";
6227 case NL80211_IFTYPE_MONITOR:
6228 return "MONITOR";
6229 case NL80211_IFTYPE_P2P_CLIENT:
6230 return "P2P_CLIENT";
6231 case NL80211_IFTYPE_P2P_GO:
6232 return "P2P_GO";
6233 default:
6234 return "unknown";
6235 }
6236}
6237
6238
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006239static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv,
6240 const char *ifname,
6241 enum nl80211_iftype iftype,
6242 const u8 *addr, int wds)
6243{
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006244 struct nl_msg *msg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006245 int ifidx;
6246 int ret = -ENOBUFS;
6247
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006248 wpa_printf(MSG_DEBUG, "nl80211: Create interface iftype %d (%s)",
6249 iftype, nl80211_iftype_str(iftype));
6250
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006251 msg = nlmsg_alloc();
6252 if (!msg)
6253 return -1;
6254
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006255 nl80211_cmd(drv, msg, 0, NL80211_CMD_NEW_INTERFACE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006256 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
6257 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, ifname);
6258 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, iftype);
6259
6260 if (iftype == NL80211_IFTYPE_MONITOR) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006261 struct nlattr *flags;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006262
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006263 flags = nla_nest_start(msg, NL80211_ATTR_MNTR_FLAGS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006264 if (!flags)
6265 goto nla_put_failure;
6266
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006267 NLA_PUT_FLAG(msg, NL80211_MNTR_FLAG_COOK_FRAMES);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006268
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006269 nla_nest_end(msg, flags);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006270 } else if (wds) {
6271 NLA_PUT_U8(msg, NL80211_ATTR_4ADDR, wds);
6272 }
6273
6274 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006275 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006276 if (ret) {
6277 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006278 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006279 wpa_printf(MSG_ERROR, "Failed to create interface %s: %d (%s)",
6280 ifname, ret, strerror(-ret));
6281 return ret;
6282 }
6283
6284 ifidx = if_nametoindex(ifname);
6285 wpa_printf(MSG_DEBUG, "nl80211: New interface %s created: ifindex=%d",
6286 ifname, ifidx);
6287
6288 if (ifidx <= 0)
6289 return -1;
6290
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006291 /* start listening for EAPOL on this interface */
6292 add_ifidx(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006293
6294 if (addr && iftype != NL80211_IFTYPE_MONITOR &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006295 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname, addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006296 nl80211_remove_iface(drv, ifidx);
6297 return -1;
6298 }
6299
6300 return ifidx;
6301}
6302
6303
6304static int nl80211_create_iface(struct wpa_driver_nl80211_data *drv,
6305 const char *ifname, enum nl80211_iftype iftype,
6306 const u8 *addr, int wds)
6307{
6308 int ret;
6309
6310 ret = nl80211_create_iface_once(drv, ifname, iftype, addr, wds);
6311
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006312 /* if error occurred and interface exists already */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006313 if (ret == -ENFILE && if_nametoindex(ifname)) {
6314 wpa_printf(MSG_INFO, "Try to remove and re-create %s", ifname);
6315
6316 /* Try to remove the interface that was already there. */
6317 nl80211_remove_iface(drv, if_nametoindex(ifname));
6318
6319 /* Try to create the interface again */
6320 ret = nl80211_create_iface_once(drv, ifname, iftype, addr,
6321 wds);
6322 }
6323
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006324 if (ret >= 0 && is_p2p_interface(iftype))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006325 nl80211_disable_11b_rates(drv, ret, 1);
6326
6327 return ret;
6328}
6329
6330
6331static void handle_tx_callback(void *ctx, u8 *buf, size_t len, int ok)
6332{
6333 struct ieee80211_hdr *hdr;
6334 u16 fc;
6335 union wpa_event_data event;
6336
6337 hdr = (struct ieee80211_hdr *) buf;
6338 fc = le_to_host16(hdr->frame_control);
6339
6340 os_memset(&event, 0, sizeof(event));
6341 event.tx_status.type = WLAN_FC_GET_TYPE(fc);
6342 event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
6343 event.tx_status.dst = hdr->addr1;
6344 event.tx_status.data = buf;
6345 event.tx_status.data_len = len;
6346 event.tx_status.ack = ok;
6347 wpa_supplicant_event(ctx, EVENT_TX_STATUS, &event);
6348}
6349
6350
6351static void from_unknown_sta(struct wpa_driver_nl80211_data *drv,
6352 u8 *buf, size_t len)
6353{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006354 struct ieee80211_hdr *hdr = (void *)buf;
6355 u16 fc;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006356 union wpa_event_data event;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006357
6358 if (len < sizeof(*hdr))
6359 return;
6360
6361 fc = le_to_host16(hdr->frame_control);
6362
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006363 os_memset(&event, 0, sizeof(event));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006364 event.rx_from_unknown.bssid = get_hdr_bssid(hdr, len);
6365 event.rx_from_unknown.addr = hdr->addr2;
6366 event.rx_from_unknown.wds = (fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) ==
6367 (WLAN_FC_FROMDS | WLAN_FC_TODS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006368 wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event);
6369}
6370
6371
6372static void handle_frame(struct wpa_driver_nl80211_data *drv,
6373 u8 *buf, size_t len, int datarate, int ssi_signal)
6374{
6375 struct ieee80211_hdr *hdr;
6376 u16 fc;
6377 union wpa_event_data event;
6378
6379 hdr = (struct ieee80211_hdr *) buf;
6380 fc = le_to_host16(hdr->frame_control);
6381
6382 switch (WLAN_FC_GET_TYPE(fc)) {
6383 case WLAN_FC_TYPE_MGMT:
6384 os_memset(&event, 0, sizeof(event));
6385 event.rx_mgmt.frame = buf;
6386 event.rx_mgmt.frame_len = len;
6387 event.rx_mgmt.datarate = datarate;
6388 event.rx_mgmt.ssi_signal = ssi_signal;
6389 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
6390 break;
6391 case WLAN_FC_TYPE_CTRL:
6392 /* can only get here with PS-Poll frames */
6393 wpa_printf(MSG_DEBUG, "CTRL");
6394 from_unknown_sta(drv, buf, len);
6395 break;
6396 case WLAN_FC_TYPE_DATA:
6397 from_unknown_sta(drv, buf, len);
6398 break;
6399 }
6400}
6401
6402
6403static void handle_monitor_read(int sock, void *eloop_ctx, void *sock_ctx)
6404{
6405 struct wpa_driver_nl80211_data *drv = eloop_ctx;
6406 int len;
6407 unsigned char buf[3000];
6408 struct ieee80211_radiotap_iterator iter;
6409 int ret;
6410 int datarate = 0, ssi_signal = 0;
6411 int injected = 0, failed = 0, rxflags = 0;
6412
6413 len = recv(sock, buf, sizeof(buf), 0);
6414 if (len < 0) {
6415 perror("recv");
6416 return;
6417 }
6418
6419 if (ieee80211_radiotap_iterator_init(&iter, (void*)buf, len)) {
6420 printf("received invalid radiotap frame\n");
6421 return;
6422 }
6423
6424 while (1) {
6425 ret = ieee80211_radiotap_iterator_next(&iter);
6426 if (ret == -ENOENT)
6427 break;
6428 if (ret) {
6429 printf("received invalid radiotap frame (%d)\n", ret);
6430 return;
6431 }
6432 switch (iter.this_arg_index) {
6433 case IEEE80211_RADIOTAP_FLAGS:
6434 if (*iter.this_arg & IEEE80211_RADIOTAP_F_FCS)
6435 len -= 4;
6436 break;
6437 case IEEE80211_RADIOTAP_RX_FLAGS:
6438 rxflags = 1;
6439 break;
6440 case IEEE80211_RADIOTAP_TX_FLAGS:
6441 injected = 1;
6442 failed = le_to_host16((*(uint16_t *) iter.this_arg)) &
6443 IEEE80211_RADIOTAP_F_TX_FAIL;
6444 break;
6445 case IEEE80211_RADIOTAP_DATA_RETRIES:
6446 break;
6447 case IEEE80211_RADIOTAP_CHANNEL:
6448 /* TODO: convert from freq/flags to channel number */
6449 break;
6450 case IEEE80211_RADIOTAP_RATE:
6451 datarate = *iter.this_arg * 5;
6452 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006453 case IEEE80211_RADIOTAP_DBM_ANTSIGNAL:
6454 ssi_signal = (s8) *iter.this_arg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006455 break;
6456 }
6457 }
6458
6459 if (rxflags && injected)
6460 return;
6461
6462 if (!injected)
6463 handle_frame(drv, buf + iter.max_length,
6464 len - iter.max_length, datarate, ssi_signal);
6465 else
6466 handle_tx_callback(drv->ctx, buf + iter.max_length,
6467 len - iter.max_length, !failed);
6468}
6469
6470
6471/*
6472 * we post-process the filter code later and rewrite
6473 * this to the offset to the last instruction
6474 */
6475#define PASS 0xFF
6476#define FAIL 0xFE
6477
6478static struct sock_filter msock_filter_insns[] = {
6479 /*
6480 * do a little-endian load of the radiotap length field
6481 */
6482 /* load lower byte into A */
6483 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 2),
6484 /* put it into X (== index register) */
6485 BPF_STMT(BPF_MISC| BPF_TAX, 0),
6486 /* load upper byte into A */
6487 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 3),
6488 /* left-shift it by 8 */
6489 BPF_STMT(BPF_ALU | BPF_LSH | BPF_K, 8),
6490 /* or with X */
6491 BPF_STMT(BPF_ALU | BPF_OR | BPF_X, 0),
6492 /* put result into X */
6493 BPF_STMT(BPF_MISC| BPF_TAX, 0),
6494
6495 /*
6496 * Allow management frames through, this also gives us those
6497 * management frames that we sent ourselves with status
6498 */
6499 /* load the lower byte of the IEEE 802.11 frame control field */
6500 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
6501 /* mask off frame type and version */
6502 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0xF),
6503 /* accept frame if it's both 0, fall through otherwise */
6504 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0, PASS, 0),
6505
6506 /*
6507 * TODO: add a bit to radiotap RX flags that indicates
6508 * that the sending station is not associated, then
6509 * add a filter here that filters on our DA and that flag
6510 * to allow us to deauth frames to that bad station.
6511 *
6512 * For now allow all To DS data frames through.
6513 */
6514 /* load the IEEE 802.11 frame control field */
6515 BPF_STMT(BPF_LD | BPF_H | BPF_IND, 0),
6516 /* mask off frame type, version and DS status */
6517 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0F03),
6518 /* accept frame if version 0, type 2 and To DS, fall through otherwise
6519 */
6520 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0801, PASS, 0),
6521
6522#if 0
6523 /*
6524 * drop non-data frames
6525 */
6526 /* load the lower byte of the frame control field */
6527 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
6528 /* mask off QoS bit */
6529 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0c),
6530 /* drop non-data frames */
6531 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 8, 0, FAIL),
6532#endif
6533 /* load the upper byte of the frame control field */
6534 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 1),
6535 /* mask off toDS/fromDS */
6536 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x03),
6537 /* accept WDS frames */
6538 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 3, PASS, 0),
6539
6540 /*
6541 * add header length to index
6542 */
6543 /* load the lower byte of the frame control field */
6544 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
6545 /* mask off QoS bit */
6546 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x80),
6547 /* right shift it by 6 to give 0 or 2 */
6548 BPF_STMT(BPF_ALU | BPF_RSH | BPF_K, 6),
6549 /* add data frame header length */
6550 BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 24),
6551 /* add index, was start of 802.11 header */
6552 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
6553 /* move to index, now start of LL header */
6554 BPF_STMT(BPF_MISC | BPF_TAX, 0),
6555
6556 /*
6557 * Accept empty data frames, we use those for
6558 * polling activity.
6559 */
6560 BPF_STMT(BPF_LD | BPF_W | BPF_LEN, 0),
6561 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_X, 0, PASS, 0),
6562
6563 /*
6564 * Accept EAPOL frames
6565 */
6566 BPF_STMT(BPF_LD | BPF_W | BPF_IND, 0),
6567 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0xAAAA0300, 0, FAIL),
6568 BPF_STMT(BPF_LD | BPF_W | BPF_IND, 4),
6569 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0000888E, PASS, FAIL),
6570
6571 /* keep these last two statements or change the code below */
6572 /* return 0 == "DROP" */
6573 BPF_STMT(BPF_RET | BPF_K, 0),
6574 /* return ~0 == "keep all" */
6575 BPF_STMT(BPF_RET | BPF_K, ~0),
6576};
6577
6578static struct sock_fprog msock_filter = {
6579 .len = sizeof(msock_filter_insns)/sizeof(msock_filter_insns[0]),
6580 .filter = msock_filter_insns,
6581};
6582
6583
6584static int add_monitor_filter(int s)
6585{
6586 int idx;
6587
6588 /* rewrite all PASS/FAIL jump offsets */
6589 for (idx = 0; idx < msock_filter.len; idx++) {
6590 struct sock_filter *insn = &msock_filter_insns[idx];
6591
6592 if (BPF_CLASS(insn->code) == BPF_JMP) {
6593 if (insn->code == (BPF_JMP|BPF_JA)) {
6594 if (insn->k == PASS)
6595 insn->k = msock_filter.len - idx - 2;
6596 else if (insn->k == FAIL)
6597 insn->k = msock_filter.len - idx - 3;
6598 }
6599
6600 if (insn->jt == PASS)
6601 insn->jt = msock_filter.len - idx - 2;
6602 else if (insn->jt == FAIL)
6603 insn->jt = msock_filter.len - idx - 3;
6604
6605 if (insn->jf == PASS)
6606 insn->jf = msock_filter.len - idx - 2;
6607 else if (insn->jf == FAIL)
6608 insn->jf = msock_filter.len - idx - 3;
6609 }
6610 }
6611
6612 if (setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER,
6613 &msock_filter, sizeof(msock_filter))) {
6614 perror("SO_ATTACH_FILTER");
6615 return -1;
6616 }
6617
6618 return 0;
6619}
6620
6621
6622static void nl80211_remove_monitor_interface(
6623 struct wpa_driver_nl80211_data *drv)
6624{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006625 drv->monitor_refcount--;
6626 if (drv->monitor_refcount > 0)
6627 return;
6628
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006629 if (drv->monitor_ifidx >= 0) {
6630 nl80211_remove_iface(drv, drv->monitor_ifidx);
6631 drv->monitor_ifidx = -1;
6632 }
6633 if (drv->monitor_sock >= 0) {
6634 eloop_unregister_read_sock(drv->monitor_sock);
6635 close(drv->monitor_sock);
6636 drv->monitor_sock = -1;
6637 }
6638}
6639
6640
6641static int
6642nl80211_create_monitor_interface(struct wpa_driver_nl80211_data *drv)
6643{
6644 char buf[IFNAMSIZ];
6645 struct sockaddr_ll ll;
6646 int optval;
6647 socklen_t optlen;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006648
6649 if (drv->monitor_ifidx >= 0) {
6650 drv->monitor_refcount++;
6651 return 0;
6652 }
6653
6654 if (os_strncmp(drv->first_bss.ifname, "p2p-", 4) == 0) {
6655 /*
6656 * P2P interface name is of the format p2p-%s-%d. For monitor
6657 * interface name corresponding to P2P GO, replace "p2p-" with
6658 * "mon-" to retain the same interface name length and to
6659 * indicate that it is a monitor interface.
6660 */
6661 snprintf(buf, IFNAMSIZ, "mon-%s", drv->first_bss.ifname + 4);
6662 } else {
6663 /* Non-P2P interface with AP functionality. */
6664 snprintf(buf, IFNAMSIZ, "mon.%s", drv->first_bss.ifname);
6665 }
6666
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006667 buf[IFNAMSIZ - 1] = '\0';
6668
6669 drv->monitor_ifidx =
6670 nl80211_create_iface(drv, buf, NL80211_IFTYPE_MONITOR, NULL,
6671 0);
6672
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07006673 if (drv->monitor_ifidx == -EOPNOTSUPP) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006674 /*
6675 * This is backward compatibility for a few versions of
6676 * the kernel only that didn't advertise the right
6677 * attributes for the only driver that then supported
6678 * AP mode w/o monitor -- ath6kl.
6679 */
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07006680 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support "
6681 "monitor interface type - try to run without it");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006682 drv->device_ap_sme = 1;
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07006683 }
6684
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006685 if (drv->monitor_ifidx < 0)
6686 return -1;
6687
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006688 if (linux_set_iface_flags(drv->global->ioctl_sock, buf, 1))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006689 goto error;
6690
6691 memset(&ll, 0, sizeof(ll));
6692 ll.sll_family = AF_PACKET;
6693 ll.sll_ifindex = drv->monitor_ifidx;
6694 drv->monitor_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
6695 if (drv->monitor_sock < 0) {
6696 perror("socket[PF_PACKET,SOCK_RAW]");
6697 goto error;
6698 }
6699
6700 if (add_monitor_filter(drv->monitor_sock)) {
6701 wpa_printf(MSG_INFO, "Failed to set socket filter for monitor "
6702 "interface; do filtering in user space");
6703 /* This works, but will cost in performance. */
6704 }
6705
6706 if (bind(drv->monitor_sock, (struct sockaddr *) &ll, sizeof(ll)) < 0) {
6707 perror("monitor socket bind");
6708 goto error;
6709 }
6710
6711 optlen = sizeof(optval);
6712 optval = 20;
6713 if (setsockopt
6714 (drv->monitor_sock, SOL_SOCKET, SO_PRIORITY, &optval, optlen)) {
6715 perror("Failed to set socket priority");
6716 goto error;
6717 }
6718
6719 if (eloop_register_read_sock(drv->monitor_sock, handle_monitor_read,
6720 drv, NULL)) {
6721 printf("Could not register monitor read socket\n");
6722 goto error;
6723 }
6724
6725 return 0;
6726 error:
6727 nl80211_remove_monitor_interface(drv);
6728 return -1;
6729}
6730
6731
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006732static int nl80211_setup_ap(struct i802_bss *bss)
6733{
6734 struct wpa_driver_nl80211_data *drv = bss->drv;
6735
6736 wpa_printf(MSG_DEBUG, "nl80211: Setup AP - device_ap_sme=%d "
6737 "use_monitor=%d", drv->device_ap_sme, drv->use_monitor);
6738
6739 /*
6740 * Disable Probe Request reporting unless we need it in this way for
6741 * devices that include the AP SME, in the other case (unless using
6742 * monitor iface) we'll get it through the nl_mgmt socket instead.
6743 */
6744 if (!drv->device_ap_sme)
6745 wpa_driver_nl80211_probe_req_report(bss, 0);
6746
6747 if (!drv->device_ap_sme && !drv->use_monitor)
6748 if (nl80211_mgmt_subscribe_ap(bss))
6749 return -1;
6750
6751 if (drv->device_ap_sme && !drv->use_monitor)
6752 if (nl80211_mgmt_subscribe_ap_dev_sme(bss))
6753 return -1;
6754
6755 if (!drv->device_ap_sme && drv->use_monitor &&
6756 nl80211_create_monitor_interface(drv) &&
6757 !drv->device_ap_sme)
Dmitry Shmidt04949592012-07-19 12:16:46 -07006758 return -1;
6759
6760#ifdef ANDROID_P2P
6761 if (drv->device_ap_sme && drv->use_monitor)
Dmitry Shmidtb638fe72012-03-20 12:51:25 -07006762 if (nl80211_mgmt_subscribe_ap_dev_sme(bss))
6763 return -1;
6764
6765 if (drv->use_monitor &&
6766 nl80211_create_monitor_interface(drv))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006767 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006768#endif
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006769
6770 if (drv->device_ap_sme &&
6771 wpa_driver_nl80211_probe_req_report(bss, 1) < 0) {
6772 wpa_printf(MSG_DEBUG, "nl80211: Failed to enable "
6773 "Probe Request frame reporting in AP mode");
6774 /* Try to survive without this */
6775 }
6776
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006777 return 0;
6778}
6779
6780
6781static void nl80211_teardown_ap(struct i802_bss *bss)
6782{
6783 struct wpa_driver_nl80211_data *drv = bss->drv;
6784
6785 if (drv->device_ap_sme) {
6786 wpa_driver_nl80211_probe_req_report(bss, 0);
6787 if (!drv->use_monitor)
6788 nl80211_mgmt_unsubscribe(bss, "AP teardown (dev SME)");
6789 } else if (drv->use_monitor)
6790 nl80211_remove_monitor_interface(drv);
6791 else
6792 nl80211_mgmt_unsubscribe(bss, "AP teardown");
6793
6794 bss->beacon_set = 0;
6795}
6796
6797
6798static int nl80211_send_eapol_data(struct i802_bss *bss,
6799 const u8 *addr, const u8 *data,
6800 size_t data_len)
6801{
6802 struct sockaddr_ll ll;
6803 int ret;
6804
6805 if (bss->drv->eapol_tx_sock < 0) {
6806 wpa_printf(MSG_DEBUG, "nl80211: No socket to send EAPOL");
6807 return -1;
6808 }
6809
6810 os_memset(&ll, 0, sizeof(ll));
6811 ll.sll_family = AF_PACKET;
6812 ll.sll_ifindex = bss->ifindex;
6813 ll.sll_protocol = htons(ETH_P_PAE);
6814 ll.sll_halen = ETH_ALEN;
6815 os_memcpy(ll.sll_addr, addr, ETH_ALEN);
6816 ret = sendto(bss->drv->eapol_tx_sock, data, data_len, 0,
6817 (struct sockaddr *) &ll, sizeof(ll));
6818 if (ret < 0)
6819 wpa_printf(MSG_ERROR, "nl80211: EAPOL TX: %s",
6820 strerror(errno));
6821
6822 return ret;
6823}
6824
6825
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006826static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
6827
6828static int wpa_driver_nl80211_hapd_send_eapol(
6829 void *priv, const u8 *addr, const u8 *data,
6830 size_t data_len, int encrypt, const u8 *own_addr, u32 flags)
6831{
6832 struct i802_bss *bss = priv;
6833 struct wpa_driver_nl80211_data *drv = bss->drv;
6834 struct ieee80211_hdr *hdr;
6835 size_t len;
6836 u8 *pos;
6837 int res;
6838 int qos = flags & WPA_STA_WMM;
Dmitry Shmidtb638fe72012-03-20 12:51:25 -07006839#ifndef ANDROID_P2P
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006840 if (drv->device_ap_sme || !drv->use_monitor)
Dmitry Shmidtb638fe72012-03-20 12:51:25 -07006841#else
6842 if (drv->device_ap_sme && !drv->use_monitor)
6843#endif
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006844 return nl80211_send_eapol_data(bss, addr, data, data_len);
6845
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006846 len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
6847 data_len;
6848 hdr = os_zalloc(len);
6849 if (hdr == NULL) {
6850 printf("malloc() failed for i802_send_data(len=%lu)\n",
6851 (unsigned long) len);
6852 return -1;
6853 }
6854
6855 hdr->frame_control =
6856 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
6857 hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
6858 if (encrypt)
6859 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
6860 if (qos) {
6861 hdr->frame_control |=
6862 host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
6863 }
6864
6865 memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
6866 memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
6867 memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
6868 pos = (u8 *) (hdr + 1);
6869
6870 if (qos) {
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006871 /* Set highest priority in QoS header */
6872 pos[0] = 7;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006873 pos[1] = 0;
6874 pos += 2;
6875 }
6876
6877 memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
6878 pos += sizeof(rfc1042_header);
6879 WPA_PUT_BE16(pos, ETH_P_PAE);
6880 pos += 2;
6881 memcpy(pos, data, data_len);
6882
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006883 res = wpa_driver_nl80211_send_frame(bss, (u8 *) hdr, len, encrypt, 0,
6884 0, 0, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006885 if (res < 0) {
6886 wpa_printf(MSG_ERROR, "i802_send_eapol - packet len: %lu - "
6887 "failed: %d (%s)",
6888 (unsigned long) len, errno, strerror(errno));
6889 }
6890 os_free(hdr);
6891
6892 return res;
6893}
6894
6895
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006896static int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr,
6897 int total_flags,
6898 int flags_or, int flags_and)
6899{
6900 struct i802_bss *bss = priv;
6901 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006902 struct nl_msg *msg;
6903 struct nlattr *flags;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006904 struct nl80211_sta_flag_update upd;
6905
6906 msg = nlmsg_alloc();
6907 if (!msg)
6908 return -ENOMEM;
6909
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006910 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006911
6912 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
6913 if_nametoindex(bss->ifname));
6914 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
6915
6916 /*
6917 * Backwards compatibility version using NL80211_ATTR_STA_FLAGS. This
6918 * can be removed eventually.
6919 */
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006920 flags = nla_nest_start(msg, NL80211_ATTR_STA_FLAGS);
6921 if (!flags)
6922 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006923 if (total_flags & WPA_STA_AUTHORIZED)
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006924 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_AUTHORIZED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006925
6926 if (total_flags & WPA_STA_WMM)
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006927 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_WME);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006928
6929 if (total_flags & WPA_STA_SHORT_PREAMBLE)
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006930 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_SHORT_PREAMBLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006931
6932 if (total_flags & WPA_STA_MFP)
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006933 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_MFP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006934
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006935 if (total_flags & WPA_STA_TDLS_PEER)
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006936 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_TDLS_PEER);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006937
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006938 nla_nest_end(msg, flags);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006939
6940 os_memset(&upd, 0, sizeof(upd));
6941 upd.mask = sta_flags_nl80211(flags_or | ~flags_and);
6942 upd.set = sta_flags_nl80211(flags_or);
6943 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
6944
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006945 return send_and_recv_msgs(drv, msg, NULL, NULL);
6946 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006947 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006948 return -ENOBUFS;
6949}
6950
6951
6952static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv,
6953 struct wpa_driver_associate_params *params)
6954{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006955 enum nl80211_iftype nlmode, old_mode;
6956 struct hostapd_freq_params freq = {
6957 .freq = params->freq,
6958 };
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006959
6960 if (params->p2p) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006961 wpa_printf(MSG_DEBUG, "nl80211: Setup AP operations for P2P "
6962 "group (GO)");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006963 nlmode = NL80211_IFTYPE_P2P_GO;
6964 } else
6965 nlmode = NL80211_IFTYPE_AP;
6966
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006967 old_mode = drv->nlmode;
6968 if (wpa_driver_nl80211_set_mode(&drv->first_bss, nlmode)) {
6969 nl80211_remove_monitor_interface(drv);
6970 return -1;
6971 }
6972
6973 if (wpa_driver_nl80211_set_freq(&drv->first_bss, &freq)) {
6974 if (old_mode != nlmode)
6975 wpa_driver_nl80211_set_mode(&drv->first_bss, old_mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006976 nl80211_remove_monitor_interface(drv);
6977 return -1;
6978 }
6979
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006980 return 0;
6981}
6982
6983
6984static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv)
6985{
6986 struct nl_msg *msg;
6987 int ret = -1;
6988
6989 msg = nlmsg_alloc();
6990 if (!msg)
6991 return -1;
6992
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006993 nl80211_cmd(drv, msg, 0, NL80211_CMD_LEAVE_IBSS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006994 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
6995 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6996 msg = NULL;
6997 if (ret) {
6998 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS failed: ret=%d "
6999 "(%s)", ret, strerror(-ret));
7000 goto nla_put_failure;
7001 }
7002
7003 ret = 0;
7004 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS request sent successfully");
7005
7006nla_put_failure:
7007 nlmsg_free(msg);
7008 return ret;
7009}
7010
7011
7012static int wpa_driver_nl80211_ibss(struct wpa_driver_nl80211_data *drv,
7013 struct wpa_driver_associate_params *params)
7014{
7015 struct nl_msg *msg;
7016 int ret = -1;
7017 int count = 0;
7018
7019 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS (ifindex=%d)", drv->ifindex);
7020
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007021 if (wpa_driver_nl80211_set_mode(&drv->first_bss,
7022 NL80211_IFTYPE_ADHOC)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007023 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
7024 "IBSS mode");
7025 return -1;
7026 }
7027
7028retry:
7029 msg = nlmsg_alloc();
7030 if (!msg)
7031 return -1;
7032
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007033 nl80211_cmd(drv, msg, 0, NL80211_CMD_JOIN_IBSS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007034 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
7035
7036 if (params->ssid == NULL || params->ssid_len > sizeof(drv->ssid))
7037 goto nla_put_failure;
7038
7039 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
7040 params->ssid, params->ssid_len);
7041 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
7042 params->ssid);
7043 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
7044 drv->ssid_len = params->ssid_len;
7045
7046 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
7047 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
7048
7049 ret = nl80211_set_conn_keys(params, msg);
7050 if (ret)
7051 goto nla_put_failure;
7052
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007053 if (params->bssid && params->fixed_bssid) {
7054 wpa_printf(MSG_DEBUG, " * BSSID=" MACSTR,
7055 MAC2STR(params->bssid));
7056 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
7057 }
7058
7059 if (params->key_mgmt_suite == KEY_MGMT_802_1X ||
7060 params->key_mgmt_suite == KEY_MGMT_PSK ||
7061 params->key_mgmt_suite == KEY_MGMT_802_1X_SHA256 ||
7062 params->key_mgmt_suite == KEY_MGMT_PSK_SHA256) {
7063 wpa_printf(MSG_DEBUG, " * control port");
7064 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT);
7065 }
7066
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007067 if (params->wpa_ie) {
7068 wpa_hexdump(MSG_DEBUG,
7069 " * Extra IEs for Beacon/Probe Response frames",
7070 params->wpa_ie, params->wpa_ie_len);
7071 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
7072 params->wpa_ie);
7073 }
7074
7075 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7076 msg = NULL;
7077 if (ret) {
7078 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS failed: ret=%d (%s)",
7079 ret, strerror(-ret));
7080 count++;
7081 if (ret == -EALREADY && count == 1) {
7082 wpa_printf(MSG_DEBUG, "nl80211: Retry IBSS join after "
7083 "forced leave");
7084 nl80211_leave_ibss(drv);
7085 nlmsg_free(msg);
7086 goto retry;
7087 }
7088
7089 goto nla_put_failure;
7090 }
7091 ret = 0;
7092 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS request sent successfully");
7093
7094nla_put_failure:
7095 nlmsg_free(msg);
7096 return ret;
7097}
7098
7099
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007100static int wpa_driver_nl80211_try_connect(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007101 struct wpa_driver_nl80211_data *drv,
7102 struct wpa_driver_associate_params *params)
7103{
7104 struct nl_msg *msg;
7105 enum nl80211_auth_type type;
7106 int ret = 0;
7107 int algs;
7108
7109 msg = nlmsg_alloc();
7110 if (!msg)
7111 return -1;
7112
7113 wpa_printf(MSG_DEBUG, "nl80211: Connect (ifindex=%d)", drv->ifindex);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007114 nl80211_cmd(drv, msg, 0, NL80211_CMD_CONNECT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007115
7116 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
7117 if (params->bssid) {
7118 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
7119 MAC2STR(params->bssid));
7120 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
7121 }
7122 if (params->freq) {
7123 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
7124 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
7125 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07007126 if (params->bg_scan_period >= 0) {
7127 wpa_printf(MSG_DEBUG, " * bg scan period=%d",
7128 params->bg_scan_period);
7129 NLA_PUT_U16(msg, NL80211_ATTR_BG_SCAN_PERIOD,
7130 params->bg_scan_period);
7131 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007132 if (params->ssid) {
7133 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
7134 params->ssid, params->ssid_len);
7135 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
7136 params->ssid);
7137 if (params->ssid_len > sizeof(drv->ssid))
7138 goto nla_put_failure;
7139 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
7140 drv->ssid_len = params->ssid_len;
7141 }
7142 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
7143 if (params->wpa_ie)
7144 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
7145 params->wpa_ie);
7146
7147 algs = 0;
7148 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
7149 algs++;
7150 if (params->auth_alg & WPA_AUTH_ALG_SHARED)
7151 algs++;
7152 if (params->auth_alg & WPA_AUTH_ALG_LEAP)
7153 algs++;
7154 if (algs > 1) {
7155 wpa_printf(MSG_DEBUG, " * Leave out Auth Type for automatic "
7156 "selection");
7157 goto skip_auth_type;
7158 }
7159
7160 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
7161 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
7162 else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
7163 type = NL80211_AUTHTYPE_SHARED_KEY;
7164 else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
7165 type = NL80211_AUTHTYPE_NETWORK_EAP;
7166 else if (params->auth_alg & WPA_AUTH_ALG_FT)
7167 type = NL80211_AUTHTYPE_FT;
7168 else
7169 goto nla_put_failure;
7170
7171 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
7172 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
7173
7174skip_auth_type:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007175 if (params->wpa_proto) {
7176 enum nl80211_wpa_versions ver = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007177
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007178 if (params->wpa_proto & WPA_PROTO_WPA)
7179 ver |= NL80211_WPA_VERSION_1;
7180 if (params->wpa_proto & WPA_PROTO_RSN)
7181 ver |= NL80211_WPA_VERSION_2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007182
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007183 wpa_printf(MSG_DEBUG, " * WPA Versions 0x%x", ver);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007184 NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver);
7185 }
7186
7187 if (params->pairwise_suite != CIPHER_NONE) {
7188 int cipher;
7189
7190 switch (params->pairwise_suite) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007191 case CIPHER_SMS4:
7192 cipher = WLAN_CIPHER_SUITE_SMS4;
7193 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007194 case CIPHER_WEP40:
7195 cipher = WLAN_CIPHER_SUITE_WEP40;
7196 break;
7197 case CIPHER_WEP104:
7198 cipher = WLAN_CIPHER_SUITE_WEP104;
7199 break;
7200 case CIPHER_CCMP:
7201 cipher = WLAN_CIPHER_SUITE_CCMP;
7202 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007203 case CIPHER_GCMP:
7204 cipher = WLAN_CIPHER_SUITE_GCMP;
7205 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007206 case CIPHER_TKIP:
7207 default:
7208 cipher = WLAN_CIPHER_SUITE_TKIP;
7209 break;
7210 }
7211 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE, cipher);
7212 }
7213
7214 if (params->group_suite != CIPHER_NONE) {
7215 int cipher;
7216
7217 switch (params->group_suite) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007218 case CIPHER_SMS4:
7219 cipher = WLAN_CIPHER_SUITE_SMS4;
7220 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007221 case CIPHER_WEP40:
7222 cipher = WLAN_CIPHER_SUITE_WEP40;
7223 break;
7224 case CIPHER_WEP104:
7225 cipher = WLAN_CIPHER_SUITE_WEP104;
7226 break;
7227 case CIPHER_CCMP:
7228 cipher = WLAN_CIPHER_SUITE_CCMP;
7229 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007230 case CIPHER_GCMP:
7231 cipher = WLAN_CIPHER_SUITE_GCMP;
7232 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007233 case CIPHER_TKIP:
7234 default:
7235 cipher = WLAN_CIPHER_SUITE_TKIP;
7236 break;
7237 }
7238 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher);
7239 }
7240
7241 if (params->key_mgmt_suite == KEY_MGMT_802_1X ||
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007242 params->key_mgmt_suite == KEY_MGMT_PSK ||
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007243 params->key_mgmt_suite == KEY_MGMT_FT_802_1X ||
7244 params->key_mgmt_suite == KEY_MGMT_FT_PSK ||
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007245 params->key_mgmt_suite == KEY_MGMT_CCKM) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007246 int mgmt = WLAN_AKM_SUITE_PSK;
7247
7248 switch (params->key_mgmt_suite) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007249 case KEY_MGMT_CCKM:
7250 mgmt = WLAN_AKM_SUITE_CCKM;
7251 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007252 case KEY_MGMT_802_1X:
7253 mgmt = WLAN_AKM_SUITE_8021X;
7254 break;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007255 case KEY_MGMT_FT_802_1X:
7256 mgmt = WLAN_AKM_SUITE_FT_8021X;
7257 break;
7258 case KEY_MGMT_FT_PSK:
7259 mgmt = WLAN_AKM_SUITE_FT_PSK;
7260 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007261 case KEY_MGMT_PSK:
7262 default:
7263 mgmt = WLAN_AKM_SUITE_PSK;
7264 break;
7265 }
7266 NLA_PUT_U32(msg, NL80211_ATTR_AKM_SUITES, mgmt);
7267 }
7268
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007269#ifdef CONFIG_IEEE80211W
7270 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED)
7271 NLA_PUT_U32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED);
7272#endif /* CONFIG_IEEE80211W */
7273
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007274 if (params->disable_ht)
7275 NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_HT);
7276
7277 if (params->htcaps && params->htcaps_mask) {
7278 int sz = sizeof(struct ieee80211_ht_capabilities);
7279 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY, sz, params->htcaps);
7280 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY_MASK, sz,
7281 params->htcaps_mask);
7282 }
7283
Dmitry Shmidt2f023192013-03-12 12:44:17 -07007284#ifdef CONFIG_VHT_OVERRIDES
7285 if (params->disable_vht) {
7286 wpa_printf(MSG_DEBUG, " * VHT disabled");
7287 NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_VHT);
7288 }
7289
7290 if (params->vhtcaps && params->vhtcaps_mask) {
7291 int sz = sizeof(struct ieee80211_vht_capabilities);
7292 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY, sz, params->vhtcaps);
7293 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY_MASK, sz,
7294 params->vhtcaps_mask);
7295 }
7296#endif /* CONFIG_VHT_OVERRIDES */
7297
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007298 ret = nl80211_set_conn_keys(params, msg);
7299 if (ret)
7300 goto nla_put_failure;
7301
7302 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7303 msg = NULL;
7304 if (ret) {
7305 wpa_printf(MSG_DEBUG, "nl80211: MLME connect failed: ret=%d "
7306 "(%s)", ret, strerror(-ret));
7307 goto nla_put_failure;
7308 }
7309 ret = 0;
7310 wpa_printf(MSG_DEBUG, "nl80211: Connect request send successfully");
7311
7312nla_put_failure:
7313 nlmsg_free(msg);
7314 return ret;
7315
7316}
7317
7318
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007319static int wpa_driver_nl80211_connect(
7320 struct wpa_driver_nl80211_data *drv,
7321 struct wpa_driver_associate_params *params)
7322{
7323 int ret = wpa_driver_nl80211_try_connect(drv, params);
7324 if (ret == -EALREADY) {
7325 /*
7326 * cfg80211 does not currently accept new connections if
7327 * we are already connected. As a workaround, force
7328 * disconnection and try again.
7329 */
7330 wpa_printf(MSG_DEBUG, "nl80211: Explicitly "
7331 "disconnecting before reassociation "
7332 "attempt");
7333 if (wpa_driver_nl80211_disconnect(
7334 drv, WLAN_REASON_PREV_AUTH_NOT_VALID))
7335 return -1;
7336 /* Ignore the next local disconnect message. */
7337 drv->ignore_next_local_disconnect = 1;
7338 ret = wpa_driver_nl80211_try_connect(drv, params);
7339 }
7340 return ret;
7341}
7342
7343
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007344static int wpa_driver_nl80211_associate(
7345 void *priv, struct wpa_driver_associate_params *params)
7346{
7347 struct i802_bss *bss = priv;
7348 struct wpa_driver_nl80211_data *drv = bss->drv;
7349 int ret = -1;
7350 struct nl_msg *msg;
7351
7352 if (params->mode == IEEE80211_MODE_AP)
7353 return wpa_driver_nl80211_ap(drv, params);
7354
7355 if (params->mode == IEEE80211_MODE_IBSS)
7356 return wpa_driver_nl80211_ibss(drv, params);
7357
7358 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007359 enum nl80211_iftype nlmode = params->p2p ?
7360 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
7361
7362 if (wpa_driver_nl80211_set_mode(priv, nlmode) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007363 return -1;
7364 return wpa_driver_nl80211_connect(drv, params);
7365 }
7366
7367 drv->associated = 0;
7368
7369 msg = nlmsg_alloc();
7370 if (!msg)
7371 return -1;
7372
7373 wpa_printf(MSG_DEBUG, "nl80211: Associate (ifindex=%d)",
7374 drv->ifindex);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007375 nl80211_cmd(drv, msg, 0, NL80211_CMD_ASSOCIATE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007376
7377 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
7378 if (params->bssid) {
7379 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
7380 MAC2STR(params->bssid));
7381 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
7382 }
7383 if (params->freq) {
7384 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
7385 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
7386 drv->assoc_freq = params->freq;
7387 } else
7388 drv->assoc_freq = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007389 if (params->bg_scan_period >= 0) {
7390 wpa_printf(MSG_DEBUG, " * bg scan period=%d",
7391 params->bg_scan_period);
7392 NLA_PUT_U16(msg, NL80211_ATTR_BG_SCAN_PERIOD,
7393 params->bg_scan_period);
7394 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007395 if (params->ssid) {
7396 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
7397 params->ssid, params->ssid_len);
7398 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
7399 params->ssid);
7400 if (params->ssid_len > sizeof(drv->ssid))
7401 goto nla_put_failure;
7402 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
7403 drv->ssid_len = params->ssid_len;
7404 }
7405 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
7406 if (params->wpa_ie)
7407 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
7408 params->wpa_ie);
7409
7410 if (params->pairwise_suite != CIPHER_NONE) {
7411 int cipher;
7412
7413 switch (params->pairwise_suite) {
7414 case CIPHER_WEP40:
7415 cipher = WLAN_CIPHER_SUITE_WEP40;
7416 break;
7417 case CIPHER_WEP104:
7418 cipher = WLAN_CIPHER_SUITE_WEP104;
7419 break;
7420 case CIPHER_CCMP:
7421 cipher = WLAN_CIPHER_SUITE_CCMP;
7422 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007423 case CIPHER_GCMP:
7424 cipher = WLAN_CIPHER_SUITE_GCMP;
7425 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007426 case CIPHER_TKIP:
7427 default:
7428 cipher = WLAN_CIPHER_SUITE_TKIP;
7429 break;
7430 }
7431 wpa_printf(MSG_DEBUG, " * pairwise=0x%x", cipher);
7432 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE, cipher);
7433 }
7434
7435 if (params->group_suite != CIPHER_NONE) {
7436 int cipher;
7437
7438 switch (params->group_suite) {
7439 case CIPHER_WEP40:
7440 cipher = WLAN_CIPHER_SUITE_WEP40;
7441 break;
7442 case CIPHER_WEP104:
7443 cipher = WLAN_CIPHER_SUITE_WEP104;
7444 break;
7445 case CIPHER_CCMP:
7446 cipher = WLAN_CIPHER_SUITE_CCMP;
7447 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007448 case CIPHER_GCMP:
7449 cipher = WLAN_CIPHER_SUITE_GCMP;
7450 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007451 case CIPHER_TKIP:
7452 default:
7453 cipher = WLAN_CIPHER_SUITE_TKIP;
7454 break;
7455 }
7456 wpa_printf(MSG_DEBUG, " * group=0x%x", cipher);
7457 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher);
7458 }
7459
7460#ifdef CONFIG_IEEE80211W
7461 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED)
7462 NLA_PUT_U32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED);
7463#endif /* CONFIG_IEEE80211W */
7464
7465 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT);
7466
7467 if (params->prev_bssid) {
7468 wpa_printf(MSG_DEBUG, " * prev_bssid=" MACSTR,
7469 MAC2STR(params->prev_bssid));
7470 NLA_PUT(msg, NL80211_ATTR_PREV_BSSID, ETH_ALEN,
7471 params->prev_bssid);
7472 }
7473
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007474 if (params->disable_ht)
7475 NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_HT);
7476
7477 if (params->htcaps && params->htcaps_mask) {
7478 int sz = sizeof(struct ieee80211_ht_capabilities);
7479 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY, sz, params->htcaps);
7480 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY_MASK, sz,
7481 params->htcaps_mask);
7482 }
7483
Dmitry Shmidt2f023192013-03-12 12:44:17 -07007484#ifdef CONFIG_VHT_OVERRIDES
7485 if (params->disable_vht) {
7486 wpa_printf(MSG_DEBUG, " * VHT disabled");
7487 NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_VHT);
7488 }
7489
7490 if (params->vhtcaps && params->vhtcaps_mask) {
7491 int sz = sizeof(struct ieee80211_vht_capabilities);
7492 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY, sz, params->vhtcaps);
7493 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY_MASK, sz,
7494 params->vhtcaps_mask);
7495 }
7496#endif /* CONFIG_VHT_OVERRIDES */
7497
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007498 if (params->p2p)
7499 wpa_printf(MSG_DEBUG, " * P2P group");
7500
7501 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7502 msg = NULL;
7503 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007504 wpa_dbg(drv->ctx, MSG_DEBUG,
7505 "nl80211: MLME command failed (assoc): ret=%d (%s)",
7506 ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007507 nl80211_dump_scan(drv);
7508 goto nla_put_failure;
7509 }
7510 ret = 0;
7511 wpa_printf(MSG_DEBUG, "nl80211: Association request send "
7512 "successfully");
7513
7514nla_put_failure:
7515 nlmsg_free(msg);
7516 return ret;
7517}
7518
7519
7520static int nl80211_set_mode(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007521 int ifindex, enum nl80211_iftype mode)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007522{
7523 struct nl_msg *msg;
7524 int ret = -ENOBUFS;
7525
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007526 wpa_printf(MSG_DEBUG, "nl80211: Set mode ifindex %d iftype %d (%s)",
7527 ifindex, mode, nl80211_iftype_str(mode));
7528
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007529 msg = nlmsg_alloc();
7530 if (!msg)
7531 return -ENOMEM;
7532
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007533 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_INTERFACE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007534 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
7535 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, mode);
7536
7537 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007538 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007539 if (!ret)
7540 return 0;
7541nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007542 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007543 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface %d to mode %d:"
7544 " %d (%s)", ifindex, mode, ret, strerror(-ret));
7545 return ret;
7546}
7547
7548
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007549static int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
7550 enum nl80211_iftype nlmode)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007551{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007552 struct wpa_driver_nl80211_data *drv = bss->drv;
7553 int ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007554 int i;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007555 int was_ap = is_ap_interface(drv->nlmode);
7556 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007557
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007558 res = nl80211_set_mode(drv, drv->ifindex, nlmode);
7559 if (res == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007560 drv->nlmode = nlmode;
7561 ret = 0;
7562 goto done;
7563 }
7564
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007565 if (res == -ENODEV)
7566 return -1;
7567
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007568 if (nlmode == drv->nlmode) {
7569 wpa_printf(MSG_DEBUG, "nl80211: Interface already in "
7570 "requested mode - ignore error");
7571 ret = 0;
7572 goto done; /* Already in the requested mode */
7573 }
7574
7575 /* mac80211 doesn't allow mode changes while the device is up, so
7576 * take the device down, try to set the mode again, and bring the
7577 * device back up.
7578 */
7579 wpa_printf(MSG_DEBUG, "nl80211: Try mode change after setting "
7580 "interface down");
7581 for (i = 0; i < 10; i++) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007582 res = linux_set_iface_flags(drv->global->ioctl_sock,
7583 bss->ifname, 0);
7584 if (res == -EACCES || res == -ENODEV)
7585 break;
7586 if (res == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007587 /* Try to set the mode again while the interface is
7588 * down */
7589 ret = nl80211_set_mode(drv, drv->ifindex, nlmode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007590 if (ret == -EACCES)
7591 break;
7592 res = linux_set_iface_flags(drv->global->ioctl_sock,
7593 bss->ifname, 1);
7594 if (res && !ret)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007595 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007596 else if (ret != -EBUSY)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007597 break;
7598 } else
7599 wpa_printf(MSG_DEBUG, "nl80211: Failed to set "
7600 "interface down");
7601 os_sleep(0, 100000);
7602 }
7603
7604 if (!ret) {
7605 wpa_printf(MSG_DEBUG, "nl80211: Mode change succeeded while "
7606 "interface is down");
7607 drv->nlmode = nlmode;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007608 drv->ignore_if_down_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007609 }
7610
7611done:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007612 if (ret) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007613 wpa_printf(MSG_DEBUG, "nl80211: Interface mode change to %d "
7614 "from %d failed", nlmode, drv->nlmode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007615 return ret;
7616 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007617
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007618 if (is_p2p_interface(nlmode))
7619 nl80211_disable_11b_rates(drv, drv->ifindex, 1);
7620 else if (drv->disabled_11b_rates)
7621 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
7622
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007623 if (is_ap_interface(nlmode)) {
7624 nl80211_mgmt_unsubscribe(bss, "start AP");
7625 /* Setup additional AP mode functionality if needed */
7626 if (nl80211_setup_ap(bss))
7627 return -1;
7628 } else if (was_ap) {
7629 /* Remove additional AP mode functionality */
7630 nl80211_teardown_ap(bss);
7631 } else {
7632 nl80211_mgmt_unsubscribe(bss, "mode change");
7633 }
7634
Dmitry Shmidt04949592012-07-19 12:16:46 -07007635 if (!bss->in_deinit && !is_ap_interface(nlmode) &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007636 nl80211_mgmt_subscribe_non_ap(bss) < 0)
7637 wpa_printf(MSG_DEBUG, "nl80211: Failed to register Action "
7638 "frame processing - ignore for now");
7639
7640 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007641}
7642
7643
7644static int wpa_driver_nl80211_get_capa(void *priv,
7645 struct wpa_driver_capa *capa)
7646{
7647 struct i802_bss *bss = priv;
7648 struct wpa_driver_nl80211_data *drv = bss->drv;
7649 if (!drv->has_capability)
7650 return -1;
7651 os_memcpy(capa, &drv->capa, sizeof(*capa));
Dmitry Shmidt444d5672013-04-01 13:08:44 -07007652 if (drv->extended_capa && drv->extended_capa_mask) {
7653 capa->extended_capa = drv->extended_capa;
7654 capa->extended_capa_mask = drv->extended_capa_mask;
7655 capa->extended_capa_len = drv->extended_capa_len;
7656 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007657 return 0;
7658}
7659
7660
7661static int wpa_driver_nl80211_set_operstate(void *priv, int state)
7662{
7663 struct i802_bss *bss = priv;
7664 struct wpa_driver_nl80211_data *drv = bss->drv;
7665
7666 wpa_printf(MSG_DEBUG, "%s: operstate %d->%d (%s)",
7667 __func__, drv->operstate, state, state ? "UP" : "DORMANT");
7668 drv->operstate = state;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007669 return netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, -1,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007670 state ? IF_OPER_UP : IF_OPER_DORMANT);
7671}
7672
7673
7674static int wpa_driver_nl80211_set_supp_port(void *priv, int authorized)
7675{
7676 struct i802_bss *bss = priv;
7677 struct wpa_driver_nl80211_data *drv = bss->drv;
7678 struct nl_msg *msg;
7679 struct nl80211_sta_flag_update upd;
7680
7681 msg = nlmsg_alloc();
7682 if (!msg)
7683 return -ENOMEM;
7684
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007685 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007686
7687 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
7688 if_nametoindex(bss->ifname));
7689 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid);
7690
7691 os_memset(&upd, 0, sizeof(upd));
7692 upd.mask = BIT(NL80211_STA_FLAG_AUTHORIZED);
7693 if (authorized)
7694 upd.set = BIT(NL80211_STA_FLAG_AUTHORIZED);
7695 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
7696
7697 return send_and_recv_msgs(drv, msg, NULL, NULL);
7698 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007699 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007700 return -ENOBUFS;
7701}
7702
7703
Jouni Malinen75ecf522011-06-27 15:19:46 -07007704/* Set kernel driver on given frequency (MHz) */
7705static int i802_set_freq(void *priv, struct hostapd_freq_params *freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007706{
Jouni Malinen75ecf522011-06-27 15:19:46 -07007707 struct i802_bss *bss = priv;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007708 return wpa_driver_nl80211_set_freq(bss, freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007709}
7710
7711
Jouni Malinen75ecf522011-06-27 15:19:46 -07007712#if defined(HOSTAPD) || defined(CONFIG_AP)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007713
7714static inline int min_int(int a, int b)
7715{
7716 if (a < b)
7717 return a;
7718 return b;
7719}
7720
7721
7722static int get_key_handler(struct nl_msg *msg, void *arg)
7723{
7724 struct nlattr *tb[NL80211_ATTR_MAX + 1];
7725 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
7726
7727 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
7728 genlmsg_attrlen(gnlh, 0), NULL);
7729
7730 /*
7731 * TODO: validate the key index and mac address!
7732 * Otherwise, there's a race condition as soon as
7733 * the kernel starts sending key notifications.
7734 */
7735
7736 if (tb[NL80211_ATTR_KEY_SEQ])
7737 memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]),
7738 min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6));
7739 return NL_SKIP;
7740}
7741
7742
7743static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
7744 int idx, u8 *seq)
7745{
7746 struct i802_bss *bss = priv;
7747 struct wpa_driver_nl80211_data *drv = bss->drv;
7748 struct nl_msg *msg;
7749
7750 msg = nlmsg_alloc();
7751 if (!msg)
7752 return -ENOMEM;
7753
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007754 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_KEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007755
7756 if (addr)
7757 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
7758 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, idx);
7759 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
7760
7761 memset(seq, 0, 6);
7762
7763 return send_and_recv_msgs(drv, msg, get_key_handler, seq);
7764 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007765 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007766 return -ENOBUFS;
7767}
7768
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007769
7770static int i802_set_rts(void *priv, int rts)
7771{
7772 struct i802_bss *bss = priv;
7773 struct wpa_driver_nl80211_data *drv = bss->drv;
7774 struct nl_msg *msg;
7775 int ret = -ENOBUFS;
7776 u32 val;
7777
7778 msg = nlmsg_alloc();
7779 if (!msg)
7780 return -ENOMEM;
7781
7782 if (rts >= 2347)
7783 val = (u32) -1;
7784 else
7785 val = rts;
7786
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007787 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007788 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
7789 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD, val);
7790
7791 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007792 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007793 if (!ret)
7794 return 0;
7795nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007796 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007797 wpa_printf(MSG_DEBUG, "nl80211: Failed to set RTS threshold %d: "
7798 "%d (%s)", rts, ret, strerror(-ret));
7799 return ret;
7800}
7801
7802
7803static int i802_set_frag(void *priv, int frag)
7804{
7805 struct i802_bss *bss = priv;
7806 struct wpa_driver_nl80211_data *drv = bss->drv;
7807 struct nl_msg *msg;
7808 int ret = -ENOBUFS;
7809 u32 val;
7810
7811 msg = nlmsg_alloc();
7812 if (!msg)
7813 return -ENOMEM;
7814
7815 if (frag >= 2346)
7816 val = (u32) -1;
7817 else
7818 val = frag;
7819
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007820 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007821 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
7822 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD, val);
7823
7824 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007825 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007826 if (!ret)
7827 return 0;
7828nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007829 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007830 wpa_printf(MSG_DEBUG, "nl80211: Failed to set fragmentation threshold "
7831 "%d: %d (%s)", frag, ret, strerror(-ret));
7832 return ret;
7833}
7834
7835
7836static int i802_flush(void *priv)
7837{
7838 struct i802_bss *bss = priv;
7839 struct wpa_driver_nl80211_data *drv = bss->drv;
7840 struct nl_msg *msg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007841 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007842
7843 msg = nlmsg_alloc();
7844 if (!msg)
7845 return -1;
7846
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007847 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007848
7849 /*
7850 * XXX: FIX! this needs to flush all VLANs too
7851 */
7852 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
7853 if_nametoindex(bss->ifname));
7854
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007855 res = send_and_recv_msgs(drv, msg, NULL, NULL);
7856 if (res) {
7857 wpa_printf(MSG_DEBUG, "nl80211: Station flush failed: ret=%d "
7858 "(%s)", res, strerror(-res));
7859 }
7860 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007861 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007862 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007863 return -ENOBUFS;
7864}
7865
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03007866#endif /* HOSTAPD || CONFIG_AP */
7867
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007868
7869static int get_sta_handler(struct nl_msg *msg, void *arg)
7870{
7871 struct nlattr *tb[NL80211_ATTR_MAX + 1];
7872 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
7873 struct hostap_sta_driver_data *data = arg;
7874 struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
7875 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
7876 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
7877 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
7878 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
7879 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
7880 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03007881 [NL80211_STA_INFO_TX_FAILED] = { .type = NLA_U32 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007882 };
7883
7884 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
7885 genlmsg_attrlen(gnlh, 0), NULL);
7886
7887 /*
7888 * TODO: validate the interface and mac address!
7889 * Otherwise, there's a race condition as soon as
7890 * the kernel starts sending station notifications.
7891 */
7892
7893 if (!tb[NL80211_ATTR_STA_INFO]) {
7894 wpa_printf(MSG_DEBUG, "sta stats missing!");
7895 return NL_SKIP;
7896 }
7897 if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
7898 tb[NL80211_ATTR_STA_INFO],
7899 stats_policy)) {
7900 wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
7901 return NL_SKIP;
7902 }
7903
7904 if (stats[NL80211_STA_INFO_INACTIVE_TIME])
7905 data->inactive_msec =
7906 nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
7907 if (stats[NL80211_STA_INFO_RX_BYTES])
7908 data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
7909 if (stats[NL80211_STA_INFO_TX_BYTES])
7910 data->tx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
7911 if (stats[NL80211_STA_INFO_RX_PACKETS])
7912 data->rx_packets =
7913 nla_get_u32(stats[NL80211_STA_INFO_RX_PACKETS]);
7914 if (stats[NL80211_STA_INFO_TX_PACKETS])
7915 data->tx_packets =
7916 nla_get_u32(stats[NL80211_STA_INFO_TX_PACKETS]);
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03007917 if (stats[NL80211_STA_INFO_TX_FAILED])
7918 data->tx_retry_failed =
7919 nla_get_u32(stats[NL80211_STA_INFO_TX_FAILED]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007920
7921 return NL_SKIP;
7922}
7923
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007924static int i802_read_sta_data(struct i802_bss *bss,
7925 struct hostap_sta_driver_data *data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007926 const u8 *addr)
7927{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007928 struct wpa_driver_nl80211_data *drv = bss->drv;
7929 struct nl_msg *msg;
7930
7931 os_memset(data, 0, sizeof(*data));
7932 msg = nlmsg_alloc();
7933 if (!msg)
7934 return -ENOMEM;
7935
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007936 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007937
7938 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
7939 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
7940
7941 return send_and_recv_msgs(drv, msg, get_sta_handler, data);
7942 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007943 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007944 return -ENOBUFS;
7945}
7946
7947
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03007948#if defined(HOSTAPD) || defined(CONFIG_AP)
7949
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007950static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
7951 int cw_min, int cw_max, int burst_time)
7952{
7953 struct i802_bss *bss = priv;
7954 struct wpa_driver_nl80211_data *drv = bss->drv;
7955 struct nl_msg *msg;
7956 struct nlattr *txq, *params;
7957
7958 msg = nlmsg_alloc();
7959 if (!msg)
7960 return -1;
7961
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007962 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007963
7964 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
7965
7966 txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS);
7967 if (!txq)
7968 goto nla_put_failure;
7969
7970 /* We are only sending parameters for a single TXQ at a time */
7971 params = nla_nest_start(msg, 1);
7972 if (!params)
7973 goto nla_put_failure;
7974
7975 switch (queue) {
7976 case 0:
7977 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VO);
7978 break;
7979 case 1:
7980 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VI);
7981 break;
7982 case 2:
7983 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BE);
7984 break;
7985 case 3:
7986 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BK);
7987 break;
7988 }
7989 /* Burst time is configured in units of 0.1 msec and TXOP parameter in
7990 * 32 usec, so need to convert the value here. */
7991 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_TXOP, (burst_time * 100 + 16) / 32);
7992 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min);
7993 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max);
7994 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_AIFS, aifs);
7995
7996 nla_nest_end(msg, params);
7997
7998 nla_nest_end(msg, txq);
7999
8000 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
8001 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008002 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008003 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008004 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008005 return -1;
8006}
8007
8008
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008009static int i802_set_sta_vlan(struct i802_bss *bss, const u8 *addr,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008010 const char *ifname, int vlan_id)
8011{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008012 struct wpa_driver_nl80211_data *drv = bss->drv;
8013 struct nl_msg *msg;
8014 int ret = -ENOBUFS;
8015
8016 msg = nlmsg_alloc();
8017 if (!msg)
8018 return -ENOMEM;
8019
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008020 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008021
8022 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
8023 if_nametoindex(bss->ifname));
8024 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
8025 NLA_PUT_U32(msg, NL80211_ATTR_STA_VLAN,
8026 if_nametoindex(ifname));
8027
8028 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008029 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008030 if (ret < 0) {
8031 wpa_printf(MSG_ERROR, "nl80211: NL80211_ATTR_STA_VLAN (addr="
8032 MACSTR " ifname=%s vlan_id=%d) failed: %d (%s)",
8033 MAC2STR(addr), ifname, vlan_id, ret,
8034 strerror(-ret));
8035 }
8036 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008037 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008038 return ret;
8039}
8040
8041
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008042static int i802_get_inact_sec(void *priv, const u8 *addr)
8043{
8044 struct hostap_sta_driver_data data;
8045 int ret;
8046
8047 data.inactive_msec = (unsigned long) -1;
8048 ret = i802_read_sta_data(priv, &data, addr);
8049 if (ret || data.inactive_msec == (unsigned long) -1)
8050 return -1;
8051 return data.inactive_msec / 1000;
8052}
8053
8054
8055static int i802_sta_clear_stats(void *priv, const u8 *addr)
8056{
8057#if 0
8058 /* TODO */
8059#endif
8060 return 0;
8061}
8062
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008063
8064static int i802_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
8065 int reason)
8066{
8067 struct i802_bss *bss = priv;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008068 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008069 struct ieee80211_mgmt mgmt;
8070
Dmitry Shmidt04949592012-07-19 12:16:46 -07008071 if (drv->device_ap_sme)
8072 return wpa_driver_nl80211_sta_remove(bss, addr);
8073
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008074 memset(&mgmt, 0, sizeof(mgmt));
8075 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
8076 WLAN_FC_STYPE_DEAUTH);
8077 memcpy(mgmt.da, addr, ETH_ALEN);
8078 memcpy(mgmt.sa, own_addr, ETH_ALEN);
8079 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
8080 mgmt.u.deauth.reason_code = host_to_le16(reason);
8081 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
8082 IEEE80211_HDRLEN +
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008083 sizeof(mgmt.u.deauth), 0, 0, 0, 0,
8084 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008085}
8086
8087
8088static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
8089 int reason)
8090{
8091 struct i802_bss *bss = priv;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008092 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008093 struct ieee80211_mgmt mgmt;
8094
Dmitry Shmidt04949592012-07-19 12:16:46 -07008095 if (drv->device_ap_sme)
8096 return wpa_driver_nl80211_sta_remove(bss, addr);
8097
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008098 memset(&mgmt, 0, sizeof(mgmt));
8099 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
8100 WLAN_FC_STYPE_DISASSOC);
8101 memcpy(mgmt.da, addr, ETH_ALEN);
8102 memcpy(mgmt.sa, own_addr, ETH_ALEN);
8103 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
8104 mgmt.u.disassoc.reason_code = host_to_le16(reason);
8105 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
8106 IEEE80211_HDRLEN +
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008107 sizeof(mgmt.u.disassoc), 0, 0, 0, 0,
8108 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008109}
8110
8111#endif /* HOSTAPD || CONFIG_AP */
8112
8113#ifdef HOSTAPD
8114
Jouni Malinen75ecf522011-06-27 15:19:46 -07008115static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
8116{
8117 int i;
8118 int *old;
8119
8120 wpa_printf(MSG_DEBUG, "nl80211: Add own interface ifindex %d",
8121 ifidx);
8122 for (i = 0; i < drv->num_if_indices; i++) {
8123 if (drv->if_indices[i] == 0) {
8124 drv->if_indices[i] = ifidx;
8125 return;
8126 }
8127 }
8128
8129 if (drv->if_indices != drv->default_if_indices)
8130 old = drv->if_indices;
8131 else
8132 old = NULL;
8133
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008134 drv->if_indices = os_realloc_array(old, drv->num_if_indices + 1,
8135 sizeof(int));
Jouni Malinen75ecf522011-06-27 15:19:46 -07008136 if (!drv->if_indices) {
8137 if (!old)
8138 drv->if_indices = drv->default_if_indices;
8139 else
8140 drv->if_indices = old;
8141 wpa_printf(MSG_ERROR, "Failed to reallocate memory for "
8142 "interfaces");
8143 wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx);
8144 return;
8145 } else if (!old)
8146 os_memcpy(drv->if_indices, drv->default_if_indices,
8147 sizeof(drv->default_if_indices));
8148 drv->if_indices[drv->num_if_indices] = ifidx;
8149 drv->num_if_indices++;
8150}
8151
8152
8153static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
8154{
8155 int i;
8156
8157 for (i = 0; i < drv->num_if_indices; i++) {
8158 if (drv->if_indices[i] == ifidx) {
8159 drv->if_indices[i] = 0;
8160 break;
8161 }
8162 }
8163}
8164
8165
8166static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
8167{
8168 int i;
8169
8170 for (i = 0; i < drv->num_if_indices; i++)
8171 if (drv->if_indices[i] == ifidx)
8172 return 1;
8173
8174 return 0;
8175}
8176
8177
8178static int i802_set_wds_sta(void *priv, const u8 *addr, int aid, int val,
8179 const char *bridge_ifname)
8180{
8181 struct i802_bss *bss = priv;
8182 struct wpa_driver_nl80211_data *drv = bss->drv;
8183 char name[IFNAMSIZ + 1];
8184
8185 os_snprintf(name, sizeof(name), "%s.sta%d", bss->ifname, aid);
8186 wpa_printf(MSG_DEBUG, "nl80211: Set WDS STA addr=" MACSTR
8187 " aid=%d val=%d name=%s", MAC2STR(addr), aid, val, name);
8188 if (val) {
8189 if (!if_nametoindex(name)) {
8190 if (nl80211_create_iface(drv, name,
8191 NL80211_IFTYPE_AP_VLAN,
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08008192 bss->addr, 1) < 0)
Jouni Malinen75ecf522011-06-27 15:19:46 -07008193 return -1;
8194 if (bridge_ifname &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008195 linux_br_add_if(drv->global->ioctl_sock,
8196 bridge_ifname, name) < 0)
Jouni Malinen75ecf522011-06-27 15:19:46 -07008197 return -1;
8198 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008199 if (linux_set_iface_flags(drv->global->ioctl_sock, name, 1)) {
8200 wpa_printf(MSG_ERROR, "nl80211: Failed to set WDS STA "
8201 "interface %s up", name);
8202 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07008203 return i802_set_sta_vlan(priv, addr, name, 0);
8204 } else {
Dmitry Shmidtaa532512012-09-24 10:35:31 -07008205 if (bridge_ifname)
8206 linux_br_del_if(drv->global->ioctl_sock, bridge_ifname,
8207 name);
8208
Jouni Malinen75ecf522011-06-27 15:19:46 -07008209 i802_set_sta_vlan(priv, addr, bss->ifname, 0);
8210 return wpa_driver_nl80211_if_remove(priv, WPA_IF_AP_VLAN,
8211 name);
8212 }
8213}
8214
8215
8216static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
8217{
8218 struct wpa_driver_nl80211_data *drv = eloop_ctx;
8219 struct sockaddr_ll lladdr;
8220 unsigned char buf[3000];
8221 int len;
8222 socklen_t fromlen = sizeof(lladdr);
8223
8224 len = recvfrom(sock, buf, sizeof(buf), 0,
8225 (struct sockaddr *)&lladdr, &fromlen);
8226 if (len < 0) {
8227 perror("recv");
8228 return;
8229 }
8230
8231 if (have_ifidx(drv, lladdr.sll_ifindex))
8232 drv_event_eapol_rx(drv->ctx, lladdr.sll_addr, buf, len);
8233}
8234
8235
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008236static int i802_check_bridge(struct wpa_driver_nl80211_data *drv,
8237 struct i802_bss *bss,
8238 const char *brname, const char *ifname)
8239{
8240 int ifindex;
8241 char in_br[IFNAMSIZ];
8242
8243 os_strlcpy(bss->brname, brname, IFNAMSIZ);
8244 ifindex = if_nametoindex(brname);
8245 if (ifindex == 0) {
8246 /*
8247 * Bridge was configured, but the bridge device does
8248 * not exist. Try to add it now.
8249 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008250 if (linux_br_add(drv->global->ioctl_sock, brname) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008251 wpa_printf(MSG_ERROR, "nl80211: Failed to add the "
8252 "bridge interface %s: %s",
8253 brname, strerror(errno));
8254 return -1;
8255 }
8256 bss->added_bridge = 1;
8257 add_ifidx(drv, if_nametoindex(brname));
8258 }
8259
8260 if (linux_br_get(in_br, ifname) == 0) {
8261 if (os_strcmp(in_br, brname) == 0)
8262 return 0; /* already in the bridge */
8263
8264 wpa_printf(MSG_DEBUG, "nl80211: Removing interface %s from "
8265 "bridge %s", ifname, in_br);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008266 if (linux_br_del_if(drv->global->ioctl_sock, in_br, ifname) <
8267 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008268 wpa_printf(MSG_ERROR, "nl80211: Failed to "
8269 "remove interface %s from bridge "
8270 "%s: %s",
8271 ifname, brname, strerror(errno));
8272 return -1;
8273 }
8274 }
8275
8276 wpa_printf(MSG_DEBUG, "nl80211: Adding interface %s into bridge %s",
8277 ifname, brname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008278 if (linux_br_add_if(drv->global->ioctl_sock, brname, ifname) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008279 wpa_printf(MSG_ERROR, "nl80211: Failed to add interface %s "
8280 "into bridge %s: %s",
8281 ifname, brname, strerror(errno));
8282 return -1;
8283 }
8284 bss->added_if_into_bridge = 1;
8285
8286 return 0;
8287}
8288
8289
8290static void *i802_init(struct hostapd_data *hapd,
8291 struct wpa_init_params *params)
8292{
8293 struct wpa_driver_nl80211_data *drv;
8294 struct i802_bss *bss;
8295 size_t i;
8296 char brname[IFNAMSIZ];
8297 int ifindex, br_ifindex;
8298 int br_added = 0;
8299
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008300 bss = wpa_driver_nl80211_init(hapd, params->ifname,
8301 params->global_priv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008302 if (bss == NULL)
8303 return NULL;
8304
8305 drv = bss->drv;
8306 drv->nlmode = NL80211_IFTYPE_AP;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008307 drv->eapol_sock = -1;
8308
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008309 if (linux_br_get(brname, params->ifname) == 0) {
8310 wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in bridge %s",
8311 params->ifname, brname);
8312 br_ifindex = if_nametoindex(brname);
8313 } else {
8314 brname[0] = '\0';
8315 br_ifindex = 0;
8316 }
8317
8318 drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int);
8319 drv->if_indices = drv->default_if_indices;
8320 for (i = 0; i < params->num_bridge; i++) {
8321 if (params->bridge[i]) {
8322 ifindex = if_nametoindex(params->bridge[i]);
8323 if (ifindex)
8324 add_ifidx(drv, ifindex);
8325 if (ifindex == br_ifindex)
8326 br_added = 1;
8327 }
8328 }
8329 if (!br_added && br_ifindex &&
8330 (params->num_bridge == 0 || !params->bridge[0]))
8331 add_ifidx(drv, br_ifindex);
8332
8333 /* start listening for EAPOL on the default AP interface */
8334 add_ifidx(drv, drv->ifindex);
8335
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008336 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008337 goto failed;
8338
8339 if (params->bssid) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008340 if (linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008341 params->bssid))
8342 goto failed;
8343 }
8344
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008345 if (wpa_driver_nl80211_set_mode(bss, drv->nlmode)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008346 wpa_printf(MSG_ERROR, "nl80211: Failed to set interface %s "
8347 "into AP mode", bss->ifname);
8348 goto failed;
8349 }
8350
8351 if (params->num_bridge && params->bridge[0] &&
8352 i802_check_bridge(drv, bss, params->bridge[0], params->ifname) < 0)
8353 goto failed;
8354
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008355 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008356 goto failed;
8357
8358 drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
8359 if (drv->eapol_sock < 0) {
8360 perror("socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE)");
8361 goto failed;
8362 }
8363
8364 if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
8365 {
8366 printf("Could not register read socket for eapol\n");
8367 goto failed;
8368 }
8369
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008370 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
8371 params->own_addr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008372 goto failed;
8373
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008374 memcpy(bss->addr, params->own_addr, ETH_ALEN);
8375
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008376 return bss;
8377
8378failed:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008379 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008380 return NULL;
8381}
8382
8383
8384static void i802_deinit(void *priv)
8385{
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008386 struct i802_bss *bss = priv;
8387 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008388}
8389
8390#endif /* HOSTAPD */
8391
8392
8393static enum nl80211_iftype wpa_driver_nl80211_if_type(
8394 enum wpa_driver_if_type type)
8395{
8396 switch (type) {
8397 case WPA_IF_STATION:
8398 return NL80211_IFTYPE_STATION;
8399 case WPA_IF_P2P_CLIENT:
8400 case WPA_IF_P2P_GROUP:
8401 return NL80211_IFTYPE_P2P_CLIENT;
8402 case WPA_IF_AP_VLAN:
8403 return NL80211_IFTYPE_AP_VLAN;
8404 case WPA_IF_AP_BSS:
8405 return NL80211_IFTYPE_AP;
8406 case WPA_IF_P2P_GO:
8407 return NL80211_IFTYPE_P2P_GO;
8408 }
8409 return -1;
8410}
8411
8412
8413#ifdef CONFIG_P2P
8414
8415static int nl80211_addr_in_use(struct nl80211_global *global, const u8 *addr)
8416{
8417 struct wpa_driver_nl80211_data *drv;
8418 dl_list_for_each(drv, &global->interfaces,
8419 struct wpa_driver_nl80211_data, list) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008420 if (os_memcmp(addr, drv->first_bss.addr, ETH_ALEN) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008421 return 1;
8422 }
8423 return 0;
8424}
8425
8426
8427static int nl80211_p2p_interface_addr(struct wpa_driver_nl80211_data *drv,
8428 u8 *new_addr)
8429{
8430 unsigned int idx;
8431
8432 if (!drv->global)
8433 return -1;
8434
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008435 os_memcpy(new_addr, drv->first_bss.addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008436 for (idx = 0; idx < 64; idx++) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008437 new_addr[0] = drv->first_bss.addr[0] | 0x02;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008438 new_addr[0] ^= idx << 2;
8439 if (!nl80211_addr_in_use(drv->global, new_addr))
8440 break;
8441 }
8442 if (idx == 64)
8443 return -1;
8444
8445 wpa_printf(MSG_DEBUG, "nl80211: Assigned new P2P Interface Address "
8446 MACSTR, MAC2STR(new_addr));
8447
8448 return 0;
8449}
8450
8451#endif /* CONFIG_P2P */
8452
8453
8454static int wpa_driver_nl80211_if_add(void *priv, enum wpa_driver_if_type type,
8455 const char *ifname, const u8 *addr,
8456 void *bss_ctx, void **drv_priv,
8457 char *force_ifname, u8 *if_addr,
8458 const char *bridge)
8459{
8460 struct i802_bss *bss = priv;
8461 struct wpa_driver_nl80211_data *drv = bss->drv;
8462 int ifidx;
8463#ifdef HOSTAPD
8464 struct i802_bss *new_bss = NULL;
8465
8466 if (type == WPA_IF_AP_BSS) {
8467 new_bss = os_zalloc(sizeof(*new_bss));
8468 if (new_bss == NULL)
8469 return -1;
8470 }
8471#endif /* HOSTAPD */
8472
8473 if (addr)
8474 os_memcpy(if_addr, addr, ETH_ALEN);
8475 ifidx = nl80211_create_iface(drv, ifname,
8476 wpa_driver_nl80211_if_type(type), addr,
8477 0);
8478 if (ifidx < 0) {
8479#ifdef HOSTAPD
8480 os_free(new_bss);
8481#endif /* HOSTAPD */
8482 return -1;
8483 }
8484
8485 if (!addr &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008486 linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
8487 if_addr) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008488 nl80211_remove_iface(drv, ifidx);
8489 return -1;
8490 }
8491
8492#ifdef CONFIG_P2P
8493 if (!addr &&
8494 (type == WPA_IF_P2P_CLIENT || type == WPA_IF_P2P_GROUP ||
8495 type == WPA_IF_P2P_GO)) {
8496 /* Enforce unique P2P Interface Address */
8497 u8 new_addr[ETH_ALEN], own_addr[ETH_ALEN];
8498
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008499 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
8500 own_addr) < 0 ||
8501 linux_get_ifhwaddr(drv->global->ioctl_sock, ifname,
8502 new_addr) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008503 nl80211_remove_iface(drv, ifidx);
8504 return -1;
8505 }
8506 if (os_memcmp(own_addr, new_addr, ETH_ALEN) == 0) {
8507 wpa_printf(MSG_DEBUG, "nl80211: Allocate new address "
8508 "for P2P group interface");
8509 if (nl80211_p2p_interface_addr(drv, new_addr) < 0) {
8510 nl80211_remove_iface(drv, ifidx);
8511 return -1;
8512 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008513 if (linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008514 new_addr) < 0) {
8515 nl80211_remove_iface(drv, ifidx);
8516 return -1;
8517 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008518 }
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07008519 os_memcpy(if_addr, new_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008520 }
8521#endif /* CONFIG_P2P */
8522
8523#ifdef HOSTAPD
8524 if (bridge &&
8525 i802_check_bridge(drv, new_bss, bridge, ifname) < 0) {
8526 wpa_printf(MSG_ERROR, "nl80211: Failed to add the new "
8527 "interface %s to a bridge %s", ifname, bridge);
8528 nl80211_remove_iface(drv, ifidx);
8529 os_free(new_bss);
8530 return -1;
8531 }
8532
8533 if (type == WPA_IF_AP_BSS) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008534 if (linux_set_iface_flags(drv->global->ioctl_sock, ifname, 1))
8535 {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008536 nl80211_remove_iface(drv, ifidx);
8537 os_free(new_bss);
8538 return -1;
8539 }
8540 os_strlcpy(new_bss->ifname, ifname, IFNAMSIZ);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008541 os_memcpy(new_bss->addr, if_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008542 new_bss->ifindex = ifidx;
8543 new_bss->drv = drv;
8544 new_bss->next = drv->first_bss.next;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008545 new_bss->freq = drv->first_bss.freq;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008546 new_bss->ctx = bss_ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008547 drv->first_bss.next = new_bss;
8548 if (drv_priv)
8549 *drv_priv = new_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008550 nl80211_init_bss(new_bss);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008551
8552 /* Subscribe management frames for this WPA_IF_AP_BSS */
8553 if (nl80211_setup_ap(new_bss))
8554 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008555 }
8556#endif /* HOSTAPD */
8557
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008558 if (drv->global)
8559 drv->global->if_add_ifindex = ifidx;
8560
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008561 return 0;
8562}
8563
8564
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008565static int wpa_driver_nl80211_if_remove(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008566 enum wpa_driver_if_type type,
8567 const char *ifname)
8568{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008569 struct wpa_driver_nl80211_data *drv = bss->drv;
8570 int ifindex = if_nametoindex(ifname);
8571
8572 wpa_printf(MSG_DEBUG, "nl80211: %s(type=%d ifname=%s) ifindex=%d",
8573 __func__, type, ifname, ifindex);
8574 if (ifindex <= 0)
8575 return -1;
8576
Dmitry Shmidtaa532512012-09-24 10:35:31 -07008577 nl80211_remove_iface(drv, ifindex);
8578
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008579#ifdef HOSTAPD
Dmitry Shmidtaa532512012-09-24 10:35:31 -07008580 if (type != WPA_IF_AP_BSS)
8581 return 0;
8582
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008583 if (bss->added_if_into_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008584 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
8585 bss->ifname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008586 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
8587 "interface %s from bridge %s: %s",
8588 bss->ifname, bss->brname, strerror(errno));
8589 }
8590 if (bss->added_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008591 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008592 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
8593 "bridge %s: %s",
8594 bss->brname, strerror(errno));
8595 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008596
8597 if (bss != &drv->first_bss) {
8598 struct i802_bss *tbss;
8599
8600 for (tbss = &drv->first_bss; tbss; tbss = tbss->next) {
8601 if (tbss->next == bss) {
8602 tbss->next = bss->next;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008603 /* Unsubscribe management frames */
8604 nl80211_teardown_ap(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008605 nl80211_destroy_bss(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008606 os_free(bss);
8607 bss = NULL;
8608 break;
8609 }
8610 }
8611 if (bss)
8612 wpa_printf(MSG_INFO, "nl80211: %s - could not find "
8613 "BSS %p in the list", __func__, bss);
8614 }
8615#endif /* HOSTAPD */
8616
8617 return 0;
8618}
8619
8620
8621static int cookie_handler(struct nl_msg *msg, void *arg)
8622{
8623 struct nlattr *tb[NL80211_ATTR_MAX + 1];
8624 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
8625 u64 *cookie = arg;
8626 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
8627 genlmsg_attrlen(gnlh, 0), NULL);
8628 if (tb[NL80211_ATTR_COOKIE])
8629 *cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
8630 return NL_SKIP;
8631}
8632
8633
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008634static int nl80211_send_frame_cmd(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008635 unsigned int freq, unsigned int wait,
8636 const u8 *buf, size_t buf_len,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008637 u64 *cookie_out, int no_cck, int no_ack,
8638 int offchanok)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008639{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008640 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008641 struct nl_msg *msg;
8642 u64 cookie;
8643 int ret = -1;
8644
8645 msg = nlmsg_alloc();
8646 if (!msg)
8647 return -1;
8648
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008649 wpa_printf(MSG_MSGDUMP, "nl80211: CMD_FRAME freq=%u wait=%u no_cck=%d "
Dmitry Shmidt04949592012-07-19 12:16:46 -07008650 "no_ack=%d offchanok=%d",
8651 freq, wait, no_cck, no_ack, offchanok);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008652 nl80211_cmd(drv, msg, 0, NL80211_CMD_FRAME);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008653
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008654 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008655 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07008656 if (wait)
8657 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, wait);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008658 if (offchanok && (drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008659 NLA_PUT_FLAG(msg, NL80211_ATTR_OFFCHANNEL_TX_OK);
8660 if (no_cck)
8661 NLA_PUT_FLAG(msg, NL80211_ATTR_TX_NO_CCK_RATE);
8662 if (no_ack)
8663 NLA_PUT_FLAG(msg, NL80211_ATTR_DONT_WAIT_FOR_ACK);
8664
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008665 NLA_PUT(msg, NL80211_ATTR_FRAME, buf_len, buf);
8666
8667 cookie = 0;
8668 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
8669 msg = NULL;
8670 if (ret) {
8671 wpa_printf(MSG_DEBUG, "nl80211: Frame command failed: ret=%d "
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07008672 "(%s) (freq=%u wait=%u)", ret, strerror(-ret),
8673 freq, wait);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008674 goto nla_put_failure;
8675 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008676 wpa_printf(MSG_MSGDUMP, "nl80211: Frame TX command accepted%s; "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008677 "cookie 0x%llx", no_ack ? " (no ACK)" : "",
8678 (long long unsigned int) cookie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008679
8680 if (cookie_out)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008681 *cookie_out = no_ack ? (u64) -1 : cookie;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008682
8683nla_put_failure:
8684 nlmsg_free(msg);
8685 return ret;
8686}
8687
8688
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008689static int wpa_driver_nl80211_send_action(struct i802_bss *bss,
8690 unsigned int freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008691 unsigned int wait_time,
8692 const u8 *dst, const u8 *src,
8693 const u8 *bssid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008694 const u8 *data, size_t data_len,
8695 int no_cck)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008696{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008697 struct wpa_driver_nl80211_data *drv = bss->drv;
8698 int ret = -1;
8699 u8 *buf;
8700 struct ieee80211_hdr *hdr;
8701
8702 wpa_printf(MSG_DEBUG, "nl80211: Send Action frame (ifindex=%d, "
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008703 "freq=%u MHz wait=%d ms no_cck=%d)",
8704 drv->ifindex, freq, wait_time, no_cck);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008705
8706 buf = os_zalloc(24 + data_len);
8707 if (buf == NULL)
8708 return ret;
8709 os_memcpy(buf + 24, data, data_len);
8710 hdr = (struct ieee80211_hdr *) buf;
8711 hdr->frame_control =
8712 IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_ACTION);
8713 os_memcpy(hdr->addr1, dst, ETH_ALEN);
8714 os_memcpy(hdr->addr2, src, ETH_ALEN);
8715 os_memcpy(hdr->addr3, bssid, ETH_ALEN);
8716
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008717 if (is_ap_interface(drv->nlmode))
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008718 ret = wpa_driver_nl80211_send_mlme(bss, buf, 24 + data_len,
8719 0, freq, no_cck, 1,
8720 wait_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008721 else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008722 ret = nl80211_send_frame_cmd(bss, freq, wait_time, buf,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008723 24 + data_len,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008724 &drv->send_action_cookie,
8725 no_cck, 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008726
8727 os_free(buf);
8728 return ret;
8729}
8730
8731
8732static void wpa_driver_nl80211_send_action_cancel_wait(void *priv)
8733{
8734 struct i802_bss *bss = priv;
8735 struct wpa_driver_nl80211_data *drv = bss->drv;
8736 struct nl_msg *msg;
8737 int ret;
8738
8739 msg = nlmsg_alloc();
8740 if (!msg)
8741 return;
8742
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08008743 wpa_printf(MSG_DEBUG, "nl80211: Cancel TX frame wait: cookie=0x%llx",
8744 (long long unsigned int) drv->send_action_cookie);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008745 nl80211_cmd(drv, msg, 0, NL80211_CMD_FRAME_WAIT_CANCEL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008746
8747 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
8748 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->send_action_cookie);
8749
8750 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8751 msg = NULL;
8752 if (ret)
8753 wpa_printf(MSG_DEBUG, "nl80211: wait cancel failed: ret=%d "
8754 "(%s)", ret, strerror(-ret));
8755
8756 nla_put_failure:
8757 nlmsg_free(msg);
8758}
8759
8760
8761static int wpa_driver_nl80211_remain_on_channel(void *priv, unsigned int freq,
8762 unsigned int duration)
8763{
8764 struct i802_bss *bss = priv;
8765 struct wpa_driver_nl80211_data *drv = bss->drv;
8766 struct nl_msg *msg;
8767 int ret;
8768 u64 cookie;
8769
8770 msg = nlmsg_alloc();
8771 if (!msg)
8772 return -1;
8773
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008774 nl80211_cmd(drv, msg, 0, NL80211_CMD_REMAIN_ON_CHANNEL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008775
8776 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
8777 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
8778 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
8779
8780 cookie = 0;
8781 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008782 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008783 if (ret == 0) {
8784 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel cookie "
8785 "0x%llx for freq=%u MHz duration=%u",
8786 (long long unsigned int) cookie, freq, duration);
8787 drv->remain_on_chan_cookie = cookie;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008788 drv->pending_remain_on_chan = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008789 return 0;
8790 }
8791 wpa_printf(MSG_DEBUG, "nl80211: Failed to request remain-on-channel "
8792 "(freq=%d duration=%u): %d (%s)",
8793 freq, duration, ret, strerror(-ret));
8794nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008795 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008796 return -1;
8797}
8798
8799
8800static int wpa_driver_nl80211_cancel_remain_on_channel(void *priv)
8801{
8802 struct i802_bss *bss = priv;
8803 struct wpa_driver_nl80211_data *drv = bss->drv;
8804 struct nl_msg *msg;
8805 int ret;
8806
8807 if (!drv->pending_remain_on_chan) {
8808 wpa_printf(MSG_DEBUG, "nl80211: No pending remain-on-channel "
8809 "to cancel");
8810 return -1;
8811 }
8812
8813 wpa_printf(MSG_DEBUG, "nl80211: Cancel remain-on-channel with cookie "
8814 "0x%llx",
8815 (long long unsigned int) drv->remain_on_chan_cookie);
8816
8817 msg = nlmsg_alloc();
8818 if (!msg)
8819 return -1;
8820
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008821 nl80211_cmd(drv, msg, 0, NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008822
8823 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
8824 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->remain_on_chan_cookie);
8825
8826 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008827 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008828 if (ret == 0)
8829 return 0;
8830 wpa_printf(MSG_DEBUG, "nl80211: Failed to cancel remain-on-channel: "
8831 "%d (%s)", ret, strerror(-ret));
8832nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008833 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008834 return -1;
8835}
8836
8837
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008838static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss, int report)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008839{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008840 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07008841
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008842 if (!report) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008843 if (bss->nl_preq && drv->device_ap_sme &&
8844 is_ap_interface(drv->nlmode)) {
8845 /*
8846 * Do not disable Probe Request reporting that was
8847 * enabled in nl80211_setup_ap().
8848 */
8849 wpa_printf(MSG_DEBUG, "nl80211: Skip disabling of "
8850 "Probe Request reporting nl_preq=%p while "
8851 "in AP mode", bss->nl_preq);
8852 } else if (bss->nl_preq) {
8853 wpa_printf(MSG_DEBUG, "nl80211: Disable Probe Request "
8854 "reporting nl_preq=%p", bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008855 eloop_unregister_read_sock(
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008856 nl_socket_get_fd(bss->nl_preq));
8857 nl_destroy_handles(&bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008858 }
8859 return 0;
8860 }
8861
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008862 if (bss->nl_preq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008863 wpa_printf(MSG_DEBUG, "nl80211: Probe Request reporting "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008864 "already on! nl_preq=%p", bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008865 return 0;
8866 }
8867
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008868 bss->nl_preq = nl_create_handle(drv->global->nl_cb, "preq");
8869 if (bss->nl_preq == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008870 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008871 wpa_printf(MSG_DEBUG, "nl80211: Enable Probe Request "
8872 "reporting nl_preq=%p", bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008873
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008874 if (nl80211_register_frame(bss, bss->nl_preq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008875 (WLAN_FC_TYPE_MGMT << 2) |
8876 (WLAN_FC_STYPE_PROBE_REQ << 4),
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008877 NULL, 0) < 0)
8878 goto out_err;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07008879
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008880 eloop_register_read_sock(nl_socket_get_fd(bss->nl_preq),
8881 wpa_driver_nl80211_event_receive, bss->nl_cb,
8882 bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008883
8884 return 0;
8885
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008886 out_err:
8887 nl_destroy_handles(&bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008888 return -1;
8889}
8890
8891
8892static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
8893 int ifindex, int disabled)
8894{
8895 struct nl_msg *msg;
8896 struct nlattr *bands, *band;
8897 int ret;
8898
8899 msg = nlmsg_alloc();
8900 if (!msg)
8901 return -1;
8902
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008903 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_TX_BITRATE_MASK);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008904 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
8905
8906 bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES);
8907 if (!bands)
8908 goto nla_put_failure;
8909
8910 /*
8911 * Disable 2 GHz rates 1, 2, 5.5, 11 Mbps by masking out everything
8912 * else apart from 6, 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS
8913 * rates. All 5 GHz rates are left enabled.
8914 */
8915 band = nla_nest_start(msg, NL80211_BAND_2GHZ);
8916 if (!band)
8917 goto nla_put_failure;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008918 if (disabled) {
8919 NLA_PUT(msg, NL80211_TXRATE_LEGACY, 8,
8920 "\x0c\x12\x18\x24\x30\x48\x60\x6c");
8921 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008922 nla_nest_end(msg, band);
8923
8924 nla_nest_end(msg, bands);
8925
8926 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8927 msg = NULL;
8928 if (ret) {
8929 wpa_printf(MSG_DEBUG, "nl80211: Set TX rates failed: ret=%d "
8930 "(%s)", ret, strerror(-ret));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008931 } else
8932 drv->disabled_11b_rates = disabled;
8933
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008934 return ret;
8935
8936nla_put_failure:
8937 nlmsg_free(msg);
8938 return -1;
8939}
8940
8941
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008942static int wpa_driver_nl80211_deinit_ap(void *priv)
8943{
8944 struct i802_bss *bss = priv;
8945 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008946 if (!is_ap_interface(drv->nlmode))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008947 return -1;
8948 wpa_driver_nl80211_del_beacon(drv);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008949 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008950}
8951
8952
Dmitry Shmidt04949592012-07-19 12:16:46 -07008953static int wpa_driver_nl80211_deinit_p2p_cli(void *priv)
8954{
8955 struct i802_bss *bss = priv;
8956 struct wpa_driver_nl80211_data *drv = bss->drv;
8957 if (drv->nlmode != NL80211_IFTYPE_P2P_CLIENT)
8958 return -1;
8959 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
8960}
8961
8962
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008963static void wpa_driver_nl80211_resume(void *priv)
8964{
8965 struct i802_bss *bss = priv;
8966 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008967 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008968 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface up on "
8969 "resume event");
8970 }
8971}
8972
8973
8974static int nl80211_send_ft_action(void *priv, u8 action, const u8 *target_ap,
8975 const u8 *ies, size_t ies_len)
8976{
8977 struct i802_bss *bss = priv;
8978 struct wpa_driver_nl80211_data *drv = bss->drv;
8979 int ret;
8980 u8 *data, *pos;
8981 size_t data_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008982 const u8 *own_addr = bss->addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008983
8984 if (action != 1) {
8985 wpa_printf(MSG_ERROR, "nl80211: Unsupported send_ft_action "
8986 "action %d", action);
8987 return -1;
8988 }
8989
8990 /*
8991 * Action frame payload:
8992 * Category[1] = 6 (Fast BSS Transition)
8993 * Action[1] = 1 (Fast BSS Transition Request)
8994 * STA Address
8995 * Target AP Address
8996 * FT IEs
8997 */
8998
8999 data_len = 2 + 2 * ETH_ALEN + ies_len;
9000 data = os_malloc(data_len);
9001 if (data == NULL)
9002 return -1;
9003 pos = data;
9004 *pos++ = 0x06; /* FT Action category */
9005 *pos++ = action;
9006 os_memcpy(pos, own_addr, ETH_ALEN);
9007 pos += ETH_ALEN;
9008 os_memcpy(pos, target_ap, ETH_ALEN);
9009 pos += ETH_ALEN;
9010 os_memcpy(pos, ies, ies_len);
9011
9012 ret = wpa_driver_nl80211_send_action(bss, drv->assoc_freq, 0,
9013 drv->bssid, own_addr, drv->bssid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009014 data, data_len, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009015 os_free(data);
9016
9017 return ret;
9018}
9019
9020
9021static int nl80211_signal_monitor(void *priv, int threshold, int hysteresis)
9022{
9023 struct i802_bss *bss = priv;
9024 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07009025 struct nl_msg *msg;
9026 struct nlattr *cqm;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07009027 int ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009028
9029 wpa_printf(MSG_DEBUG, "nl80211: Signal monitor threshold=%d "
9030 "hysteresis=%d", threshold, hysteresis);
9031
9032 msg = nlmsg_alloc();
9033 if (!msg)
9034 return -1;
9035
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009036 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_CQM);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009037
9038 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
9039
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07009040 cqm = nla_nest_start(msg, NL80211_ATTR_CQM);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009041 if (cqm == NULL)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07009042 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009043
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07009044 NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_THOLD, threshold);
9045 NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_HYST, hysteresis);
9046 nla_nest_end(msg, cqm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009047
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07009048 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009049 msg = NULL;
9050
9051nla_put_failure:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009052 nlmsg_free(msg);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07009053 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009054}
9055
9056
9057static int nl80211_signal_poll(void *priv, struct wpa_signal_info *si)
9058{
9059 struct i802_bss *bss = priv;
9060 struct wpa_driver_nl80211_data *drv = bss->drv;
9061 int res;
9062
9063 os_memset(si, 0, sizeof(*si));
9064 res = nl80211_get_link_signal(drv, si);
9065 if (res != 0)
9066 return res;
9067
9068 return nl80211_get_link_noise(drv, si);
9069}
9070
9071
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009072static int wpa_driver_nl80211_shared_freq(void *priv)
9073{
9074 struct i802_bss *bss = priv;
9075 struct wpa_driver_nl80211_data *drv = bss->drv;
9076 struct wpa_driver_nl80211_data *driver;
9077 int freq = 0;
9078
9079 /*
9080 * If the same PHY is in connected state with some other interface,
9081 * then retrieve the assoc freq.
9082 */
9083 wpa_printf(MSG_DEBUG, "nl80211: Get shared freq for PHY %s",
9084 drv->phyname);
9085
9086 dl_list_for_each(driver, &drv->global->interfaces,
9087 struct wpa_driver_nl80211_data, list) {
9088 if (drv == driver ||
9089 os_strcmp(drv->phyname, driver->phyname) != 0 ||
9090#ifdef ANDROID_P2P
9091 (!driver->associated && !is_ap_interface(driver->nlmode)))
9092#else
9093 !driver->associated)
9094#endif
9095 continue;
9096
9097 wpa_printf(MSG_DEBUG, "nl80211: Found a match for PHY %s - %s "
9098 MACSTR,
9099 driver->phyname, driver->first_bss.ifname,
9100 MAC2STR(driver->first_bss.addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07009101 if (is_ap_interface(driver->nlmode))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009102 freq = driver->first_bss.freq;
9103 else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009104 freq = nl80211_get_assoc_freq(driver);
9105 wpa_printf(MSG_DEBUG, "nl80211: Shared freq for PHY %s: %d",
9106 drv->phyname, freq);
9107 }
9108
9109 if (!freq)
9110 wpa_printf(MSG_DEBUG, "nl80211: No shared interface for "
9111 "PHY (%s) in associated state", drv->phyname);
9112
9113 return freq;
9114}
9115
9116
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009117static int nl80211_send_frame(void *priv, const u8 *data, size_t data_len,
9118 int encrypt)
9119{
9120 struct i802_bss *bss = priv;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08009121 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt, 0,
9122 0, 0, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009123}
9124
9125
9126static int nl80211_set_param(void *priv, const char *param)
9127{
9128 wpa_printf(MSG_DEBUG, "nl80211: driver param='%s'", param);
9129 if (param == NULL)
9130 return 0;
9131
9132#ifdef CONFIG_P2P
9133 if (os_strstr(param, "use_p2p_group_interface=1")) {
9134 struct i802_bss *bss = priv;
9135 struct wpa_driver_nl80211_data *drv = bss->drv;
9136
9137 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
9138 "interface");
9139 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
9140 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
9141 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009142#ifdef ANDROID_P2P
9143 if(os_strstr(param, "use_multi_chan_concurrent=1")) {
9144 struct i802_bss *bss = priv;
9145 struct wpa_driver_nl80211_data *drv = bss->drv;
9146 wpa_printf(MSG_DEBUG, "nl80211: Use Multi channel "
9147 "concurrency");
9148 drv->capa.flags |= WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT;
9149 }
9150#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009151#endif /* CONFIG_P2P */
9152
9153 return 0;
9154}
9155
9156
9157static void * nl80211_global_init(void)
9158{
9159 struct nl80211_global *global;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009160 struct netlink_config *cfg;
9161
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009162 global = os_zalloc(sizeof(*global));
9163 if (global == NULL)
9164 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009165 global->ioctl_sock = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009166 dl_list_init(&global->interfaces);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009167 global->if_add_ifindex = -1;
9168
9169 cfg = os_zalloc(sizeof(*cfg));
9170 if (cfg == NULL)
9171 goto err;
9172
9173 cfg->ctx = global;
9174 cfg->newlink_cb = wpa_driver_nl80211_event_rtm_newlink;
9175 cfg->dellink_cb = wpa_driver_nl80211_event_rtm_dellink;
9176 global->netlink = netlink_init(cfg);
9177 if (global->netlink == NULL) {
9178 os_free(cfg);
9179 goto err;
9180 }
9181
9182 if (wpa_driver_nl80211_init_nl_global(global) < 0)
9183 goto err;
9184
9185 global->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
9186 if (global->ioctl_sock < 0) {
9187 perror("socket(PF_INET,SOCK_DGRAM)");
9188 goto err;
9189 }
9190
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009191 return global;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009192
9193err:
9194 nl80211_global_deinit(global);
9195 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009196}
9197
9198
9199static void nl80211_global_deinit(void *priv)
9200{
9201 struct nl80211_global *global = priv;
9202 if (global == NULL)
9203 return;
9204 if (!dl_list_empty(&global->interfaces)) {
9205 wpa_printf(MSG_ERROR, "nl80211: %u interface(s) remain at "
9206 "nl80211_global_deinit",
9207 dl_list_len(&global->interfaces));
9208 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009209
9210 if (global->netlink)
9211 netlink_deinit(global->netlink);
9212
9213 nl_destroy_handles(&global->nl);
9214
9215 if (global->nl_event) {
9216 eloop_unregister_read_sock(
9217 nl_socket_get_fd(global->nl_event));
9218 nl_destroy_handles(&global->nl_event);
9219 }
9220
9221 nl_cb_put(global->nl_cb);
9222
9223 if (global->ioctl_sock >= 0)
9224 close(global->ioctl_sock);
9225
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009226 os_free(global);
9227}
9228
9229
9230static const char * nl80211_get_radio_name(void *priv)
9231{
9232 struct i802_bss *bss = priv;
9233 struct wpa_driver_nl80211_data *drv = bss->drv;
9234 return drv->phyname;
9235}
9236
9237
Jouni Malinen75ecf522011-06-27 15:19:46 -07009238static int nl80211_pmkid(struct i802_bss *bss, int cmd, const u8 *bssid,
9239 const u8 *pmkid)
9240{
9241 struct nl_msg *msg;
9242
9243 msg = nlmsg_alloc();
9244 if (!msg)
9245 return -ENOMEM;
9246
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009247 nl80211_cmd(bss->drv, msg, 0, cmd);
Jouni Malinen75ecf522011-06-27 15:19:46 -07009248
9249 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
9250 if (pmkid)
9251 NLA_PUT(msg, NL80211_ATTR_PMKID, 16, pmkid);
9252 if (bssid)
9253 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
9254
9255 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
9256 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009257 nlmsg_free(msg);
Jouni Malinen75ecf522011-06-27 15:19:46 -07009258 return -ENOBUFS;
9259}
9260
9261
9262static int nl80211_add_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
9263{
9264 struct i802_bss *bss = priv;
9265 wpa_printf(MSG_DEBUG, "nl80211: Add PMKID for " MACSTR, MAC2STR(bssid));
9266 return nl80211_pmkid(bss, NL80211_CMD_SET_PMKSA, bssid, pmkid);
9267}
9268
9269
9270static int nl80211_remove_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
9271{
9272 struct i802_bss *bss = priv;
9273 wpa_printf(MSG_DEBUG, "nl80211: Delete PMKID for " MACSTR,
9274 MAC2STR(bssid));
9275 return nl80211_pmkid(bss, NL80211_CMD_DEL_PMKSA, bssid, pmkid);
9276}
9277
9278
9279static int nl80211_flush_pmkid(void *priv)
9280{
9281 struct i802_bss *bss = priv;
9282 wpa_printf(MSG_DEBUG, "nl80211: Flush PMKIDs");
9283 return nl80211_pmkid(bss, NL80211_CMD_FLUSH_PMKSA, NULL, NULL);
9284}
9285
9286
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009287static void nl80211_set_rekey_info(void *priv, const u8 *kek, const u8 *kck,
9288 const u8 *replay_ctr)
9289{
9290 struct i802_bss *bss = priv;
9291 struct wpa_driver_nl80211_data *drv = bss->drv;
9292 struct nlattr *replay_nested;
9293 struct nl_msg *msg;
9294
9295 msg = nlmsg_alloc();
9296 if (!msg)
9297 return;
9298
9299 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
9300
9301 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
9302
9303 replay_nested = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
9304 if (!replay_nested)
9305 goto nla_put_failure;
9306
9307 NLA_PUT(msg, NL80211_REKEY_DATA_KEK, NL80211_KEK_LEN, kek);
9308 NLA_PUT(msg, NL80211_REKEY_DATA_KCK, NL80211_KCK_LEN, kck);
9309 NLA_PUT(msg, NL80211_REKEY_DATA_REPLAY_CTR, NL80211_REPLAY_CTR_LEN,
9310 replay_ctr);
9311
9312 nla_nest_end(msg, replay_nested);
9313
9314 send_and_recv_msgs(drv, msg, NULL, NULL);
9315 return;
9316 nla_put_failure:
9317 nlmsg_free(msg);
9318}
9319
9320
9321static void nl80211_send_null_frame(struct i802_bss *bss, const u8 *own_addr,
9322 const u8 *addr, int qos)
9323{
9324 /* send data frame to poll STA and check whether
9325 * this frame is ACKed */
9326 struct {
9327 struct ieee80211_hdr hdr;
9328 u16 qos_ctl;
9329 } STRUCT_PACKED nulldata;
9330 size_t size;
9331
9332 /* Send data frame to poll STA and check whether this frame is ACKed */
9333
9334 os_memset(&nulldata, 0, sizeof(nulldata));
9335
9336 if (qos) {
9337 nulldata.hdr.frame_control =
9338 IEEE80211_FC(WLAN_FC_TYPE_DATA,
9339 WLAN_FC_STYPE_QOS_NULL);
9340 size = sizeof(nulldata);
9341 } else {
9342 nulldata.hdr.frame_control =
9343 IEEE80211_FC(WLAN_FC_TYPE_DATA,
9344 WLAN_FC_STYPE_NULLFUNC);
9345 size = sizeof(struct ieee80211_hdr);
9346 }
9347
9348 nulldata.hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS);
9349 os_memcpy(nulldata.hdr.IEEE80211_DA_FROMDS, addr, ETH_ALEN);
9350 os_memcpy(nulldata.hdr.IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
9351 os_memcpy(nulldata.hdr.IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
9352
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009353 if (wpa_driver_nl80211_send_mlme(bss, (u8 *) &nulldata, size, 0, 0, 0,
9354 0, 0) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009355 wpa_printf(MSG_DEBUG, "nl80211_send_null_frame: Failed to "
9356 "send poll frame");
9357}
9358
9359static void nl80211_poll_client(void *priv, const u8 *own_addr, const u8 *addr,
9360 int qos)
9361{
9362 struct i802_bss *bss = priv;
9363 struct wpa_driver_nl80211_data *drv = bss->drv;
9364 struct nl_msg *msg;
9365
9366 if (!drv->poll_command_supported) {
9367 nl80211_send_null_frame(bss, own_addr, addr, qos);
9368 return;
9369 }
9370
9371 msg = nlmsg_alloc();
9372 if (!msg)
9373 return;
9374
9375 nl80211_cmd(drv, msg, 0, NL80211_CMD_PROBE_CLIENT);
9376
9377 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
9378 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
9379
9380 send_and_recv_msgs(drv, msg, NULL, NULL);
9381 return;
9382 nla_put_failure:
9383 nlmsg_free(msg);
9384}
9385
9386
9387static int nl80211_set_power_save(struct i802_bss *bss, int enabled)
9388{
9389 struct nl_msg *msg;
9390
9391 msg = nlmsg_alloc();
9392 if (!msg)
9393 return -ENOMEM;
9394
9395 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_SET_POWER_SAVE);
9396 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
9397 NLA_PUT_U32(msg, NL80211_ATTR_PS_STATE,
9398 enabled ? NL80211_PS_ENABLED : NL80211_PS_DISABLED);
9399 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
9400nla_put_failure:
9401 nlmsg_free(msg);
9402 return -ENOBUFS;
9403}
9404
9405
9406static int nl80211_set_p2p_powersave(void *priv, int legacy_ps, int opp_ps,
9407 int ctwindow)
9408{
9409 struct i802_bss *bss = priv;
9410
9411 wpa_printf(MSG_DEBUG, "nl80211: set_p2p_powersave (legacy_ps=%d "
9412 "opp_ps=%d ctwindow=%d)", legacy_ps, opp_ps, ctwindow);
9413
9414 if (opp_ps != -1 || ctwindow != -1)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08009415#ifdef ANDROID_P2P
9416 wpa_driver_set_p2p_ps(priv, legacy_ps, opp_ps, ctwindow);
9417#else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009418 return -1; /* Not yet supported */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08009419#endif
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009420
9421 if (legacy_ps == -1)
9422 return 0;
9423 if (legacy_ps != 0 && legacy_ps != 1)
9424 return -1; /* Not yet supported */
9425
9426 return nl80211_set_power_save(bss, legacy_ps);
9427}
9428
9429
9430#ifdef CONFIG_TDLS
9431
9432static int nl80211_send_tdls_mgmt(void *priv, const u8 *dst, u8 action_code,
9433 u8 dialog_token, u16 status_code,
9434 const u8 *buf, size_t len)
9435{
9436 struct i802_bss *bss = priv;
9437 struct wpa_driver_nl80211_data *drv = bss->drv;
9438 struct nl_msg *msg;
9439
9440 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
9441 return -EOPNOTSUPP;
9442
9443 if (!dst)
9444 return -EINVAL;
9445
9446 msg = nlmsg_alloc();
9447 if (!msg)
9448 return -ENOMEM;
9449
9450 nl80211_cmd(drv, msg, 0, NL80211_CMD_TDLS_MGMT);
9451 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
9452 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
9453 NLA_PUT_U8(msg, NL80211_ATTR_TDLS_ACTION, action_code);
9454 NLA_PUT_U8(msg, NL80211_ATTR_TDLS_DIALOG_TOKEN, dialog_token);
9455 NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status_code);
9456 NLA_PUT(msg, NL80211_ATTR_IE, len, buf);
9457
9458 return send_and_recv_msgs(drv, msg, NULL, NULL);
9459
9460nla_put_failure:
9461 nlmsg_free(msg);
9462 return -ENOBUFS;
9463}
9464
9465
9466static int nl80211_tdls_oper(void *priv, enum tdls_oper oper, const u8 *peer)
9467{
9468 struct i802_bss *bss = priv;
9469 struct wpa_driver_nl80211_data *drv = bss->drv;
9470 struct nl_msg *msg;
9471 enum nl80211_tdls_operation nl80211_oper;
9472
9473 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
9474 return -EOPNOTSUPP;
9475
9476 switch (oper) {
9477 case TDLS_DISCOVERY_REQ:
9478 nl80211_oper = NL80211_TDLS_DISCOVERY_REQ;
9479 break;
9480 case TDLS_SETUP:
9481 nl80211_oper = NL80211_TDLS_SETUP;
9482 break;
9483 case TDLS_TEARDOWN:
9484 nl80211_oper = NL80211_TDLS_TEARDOWN;
9485 break;
9486 case TDLS_ENABLE_LINK:
9487 nl80211_oper = NL80211_TDLS_ENABLE_LINK;
9488 break;
9489 case TDLS_DISABLE_LINK:
9490 nl80211_oper = NL80211_TDLS_DISABLE_LINK;
9491 break;
9492 case TDLS_ENABLE:
9493 return 0;
9494 case TDLS_DISABLE:
9495 return 0;
9496 default:
9497 return -EINVAL;
9498 }
9499
9500 msg = nlmsg_alloc();
9501 if (!msg)
9502 return -ENOMEM;
9503
9504 nl80211_cmd(drv, msg, 0, NL80211_CMD_TDLS_OPER);
9505 NLA_PUT_U8(msg, NL80211_ATTR_TDLS_OPERATION, nl80211_oper);
9506 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
9507 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, peer);
9508
9509 return send_and_recv_msgs(drv, msg, NULL, NULL);
9510
9511nla_put_failure:
9512 nlmsg_free(msg);
9513 return -ENOBUFS;
9514}
9515
9516#endif /* CONFIG TDLS */
9517
9518
9519#ifdef ANDROID
9520
9521typedef struct android_wifi_priv_cmd {
9522 char *buf;
9523 int used_len;
9524 int total_len;
9525} android_wifi_priv_cmd;
9526
9527static int drv_errors = 0;
9528
9529static void wpa_driver_send_hang_msg(struct wpa_driver_nl80211_data *drv)
9530{
9531 drv_errors++;
9532 if (drv_errors > DRV_NUMBER_SEQUENTIAL_ERRORS) {
9533 drv_errors = 0;
9534 wpa_msg(drv->ctx, MSG_INFO, WPA_EVENT_DRIVER_STATE "HANGED");
9535 }
9536}
9537
9538
9539static int android_priv_cmd(struct i802_bss *bss, const char *cmd)
9540{
9541 struct wpa_driver_nl80211_data *drv = bss->drv;
9542 struct ifreq ifr;
9543 android_wifi_priv_cmd priv_cmd;
9544 char buf[MAX_DRV_CMD_SIZE];
9545 int ret;
9546
9547 os_memset(&ifr, 0, sizeof(ifr));
9548 os_memset(&priv_cmd, 0, sizeof(priv_cmd));
9549 os_strlcpy(ifr.ifr_name, bss->ifname, IFNAMSIZ);
9550
9551 os_memset(buf, 0, sizeof(buf));
9552 os_strlcpy(buf, cmd, sizeof(buf));
9553
9554 priv_cmd.buf = buf;
9555 priv_cmd.used_len = sizeof(buf);
9556 priv_cmd.total_len = sizeof(buf);
9557 ifr.ifr_data = &priv_cmd;
9558
9559 ret = ioctl(drv->global->ioctl_sock, SIOCDEVPRIVATE + 1, &ifr);
9560 if (ret < 0) {
9561 wpa_printf(MSG_ERROR, "%s: failed to issue private commands",
9562 __func__);
9563 wpa_driver_send_hang_msg(drv);
9564 return ret;
9565 }
9566
9567 drv_errors = 0;
9568 return 0;
9569}
9570
9571
9572static int android_pno_start(struct i802_bss *bss,
9573 struct wpa_driver_scan_params *params)
9574{
9575 struct wpa_driver_nl80211_data *drv = bss->drv;
9576 struct ifreq ifr;
9577 android_wifi_priv_cmd priv_cmd;
9578 int ret = 0, i = 0, bp;
9579 char buf[WEXT_PNO_MAX_COMMAND_SIZE];
9580
9581 bp = WEXT_PNOSETUP_HEADER_SIZE;
9582 os_memcpy(buf, WEXT_PNOSETUP_HEADER, bp);
9583 buf[bp++] = WEXT_PNO_TLV_PREFIX;
9584 buf[bp++] = WEXT_PNO_TLV_VERSION;
9585 buf[bp++] = WEXT_PNO_TLV_SUBVERSION;
9586 buf[bp++] = WEXT_PNO_TLV_RESERVED;
9587
9588 while (i < WEXT_PNO_AMOUNT && (size_t) i < params->num_ssids) {
9589 /* Check that there is enough space needed for 1 more SSID, the
9590 * other sections and null termination */
9591 if ((bp + WEXT_PNO_SSID_HEADER_SIZE + MAX_SSID_LEN +
9592 WEXT_PNO_NONSSID_SECTIONS_SIZE + 1) >= (int) sizeof(buf))
9593 break;
9594 wpa_hexdump_ascii(MSG_DEBUG, "For PNO Scan",
9595 params->ssids[i].ssid,
9596 params->ssids[i].ssid_len);
9597 buf[bp++] = WEXT_PNO_SSID_SECTION;
9598 buf[bp++] = params->ssids[i].ssid_len;
9599 os_memcpy(&buf[bp], params->ssids[i].ssid,
9600 params->ssids[i].ssid_len);
9601 bp += params->ssids[i].ssid_len;
9602 i++;
9603 }
9604
9605 buf[bp++] = WEXT_PNO_SCAN_INTERVAL_SECTION;
9606 os_snprintf(&buf[bp], WEXT_PNO_SCAN_INTERVAL_LENGTH + 1, "%x",
9607 WEXT_PNO_SCAN_INTERVAL);
9608 bp += WEXT_PNO_SCAN_INTERVAL_LENGTH;
9609
9610 buf[bp++] = WEXT_PNO_REPEAT_SECTION;
9611 os_snprintf(&buf[bp], WEXT_PNO_REPEAT_LENGTH + 1, "%x",
9612 WEXT_PNO_REPEAT);
9613 bp += WEXT_PNO_REPEAT_LENGTH;
9614
9615 buf[bp++] = WEXT_PNO_MAX_REPEAT_SECTION;
9616 os_snprintf(&buf[bp], WEXT_PNO_MAX_REPEAT_LENGTH + 1, "%x",
9617 WEXT_PNO_MAX_REPEAT);
9618 bp += WEXT_PNO_MAX_REPEAT_LENGTH + 1;
9619
9620 memset(&ifr, 0, sizeof(ifr));
9621 memset(&priv_cmd, 0, sizeof(priv_cmd));
9622 os_strncpy(ifr.ifr_name, bss->ifname, IFNAMSIZ);
9623
9624 priv_cmd.buf = buf;
9625 priv_cmd.used_len = bp;
9626 priv_cmd.total_len = bp;
9627 ifr.ifr_data = &priv_cmd;
9628
9629 ret = ioctl(drv->global->ioctl_sock, SIOCDEVPRIVATE + 1, &ifr);
9630
9631 if (ret < 0) {
9632 wpa_printf(MSG_ERROR, "ioctl[SIOCSIWPRIV] (pnosetup): %d",
9633 ret);
9634 wpa_driver_send_hang_msg(drv);
9635 return ret;
9636 }
9637
9638 drv_errors = 0;
9639
9640 return android_priv_cmd(bss, "PNOFORCE 1");
9641}
9642
9643
9644static int android_pno_stop(struct i802_bss *bss)
9645{
9646 return android_priv_cmd(bss, "PNOFORCE 0");
9647}
9648
9649#endif /* ANDROID */
9650
9651
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009652static int driver_nl80211_set_key(const char *ifname, void *priv,
9653 enum wpa_alg alg, const u8 *addr,
9654 int key_idx, int set_tx,
9655 const u8 *seq, size_t seq_len,
9656 const u8 *key, size_t key_len)
9657{
9658 struct i802_bss *bss = priv;
9659 return wpa_driver_nl80211_set_key(ifname, bss, alg, addr, key_idx,
9660 set_tx, seq, seq_len, key, key_len);
9661}
9662
9663
9664static int driver_nl80211_scan2(void *priv,
9665 struct wpa_driver_scan_params *params)
9666{
9667 struct i802_bss *bss = priv;
9668 return wpa_driver_nl80211_scan(bss, params);
9669}
9670
9671
9672static int driver_nl80211_deauthenticate(void *priv, const u8 *addr,
9673 int reason_code)
9674{
9675 struct i802_bss *bss = priv;
9676 return wpa_driver_nl80211_deauthenticate(bss, addr, reason_code);
9677}
9678
9679
9680static int driver_nl80211_authenticate(void *priv,
9681 struct wpa_driver_auth_params *params)
9682{
9683 struct i802_bss *bss = priv;
9684 return wpa_driver_nl80211_authenticate(bss, params);
9685}
9686
9687
9688static void driver_nl80211_deinit(void *priv)
9689{
9690 struct i802_bss *bss = priv;
9691 wpa_driver_nl80211_deinit(bss);
9692}
9693
9694
9695static int driver_nl80211_if_remove(void *priv, enum wpa_driver_if_type type,
9696 const char *ifname)
9697{
9698 struct i802_bss *bss = priv;
9699 return wpa_driver_nl80211_if_remove(bss, type, ifname);
9700}
9701
9702
9703static int driver_nl80211_send_mlme(void *priv, const u8 *data,
9704 size_t data_len, int noack)
9705{
9706 struct i802_bss *bss = priv;
9707 return wpa_driver_nl80211_send_mlme(bss, data, data_len, noack,
9708 0, 0, 0, 0);
9709}
9710
9711
9712static int driver_nl80211_sta_remove(void *priv, const u8 *addr)
9713{
9714 struct i802_bss *bss = priv;
9715 return wpa_driver_nl80211_sta_remove(bss, addr);
9716}
9717
9718
9719#if defined(HOSTAPD) || defined(CONFIG_AP)
9720static int driver_nl80211_set_sta_vlan(void *priv, const u8 *addr,
9721 const char *ifname, int vlan_id)
9722{
9723 struct i802_bss *bss = priv;
9724 return i802_set_sta_vlan(bss, addr, ifname, vlan_id);
9725}
9726#endif /* HOSTAPD || CONFIG_AP */
9727
9728
9729static int driver_nl80211_read_sta_data(void *priv,
9730 struct hostap_sta_driver_data *data,
9731 const u8 *addr)
9732{
9733 struct i802_bss *bss = priv;
9734 return i802_read_sta_data(bss, data, addr);
9735}
9736
9737
9738static int driver_nl80211_send_action(void *priv, unsigned int freq,
9739 unsigned int wait_time,
9740 const u8 *dst, const u8 *src,
9741 const u8 *bssid,
9742 const u8 *data, size_t data_len,
9743 int no_cck)
9744{
9745 struct i802_bss *bss = priv;
9746 return wpa_driver_nl80211_send_action(bss, freq, wait_time, dst, src,
9747 bssid, data, data_len, no_cck);
9748}
9749
9750
9751static int driver_nl80211_probe_req_report(void *priv, int report)
9752{
9753 struct i802_bss *bss = priv;
9754 return wpa_driver_nl80211_probe_req_report(bss, report);
9755}
9756
9757
Dmitry Shmidt700a1372013-03-15 14:14:44 -07009758static int wpa_driver_nl80211_update_ft_ies(void *priv, const u8 *md,
9759 const u8 *ies, size_t ies_len)
9760{
9761 int ret;
9762 struct nl_msg *msg;
9763 struct i802_bss *bss = priv;
9764 struct wpa_driver_nl80211_data *drv = bss->drv;
9765 u16 mdid = WPA_GET_LE16(md);
9766
9767 msg = nlmsg_alloc();
9768 if (!msg)
9769 return -ENOMEM;
9770
9771 wpa_printf(MSG_DEBUG, "nl80211: Updating FT IEs");
9772 nl80211_cmd(drv, msg, 0, NL80211_CMD_UPDATE_FT_IES);
9773 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
9774 NLA_PUT(msg, NL80211_ATTR_IE, ies_len, ies);
9775 NLA_PUT_U16(msg, NL80211_ATTR_MDID, mdid);
9776
9777 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9778 if (ret) {
9779 wpa_printf(MSG_DEBUG, "nl80211: update_ft_ies failed "
9780 "err=%d (%s)", ret, strerror(-ret));
9781 }
9782
9783 return ret;
9784
9785nla_put_failure:
9786 nlmsg_free(msg);
9787 return -ENOBUFS;
9788}
9789
9790
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009791const struct wpa_driver_ops wpa_driver_nl80211_ops = {
9792 .name = "nl80211",
9793 .desc = "Linux nl80211/cfg80211",
9794 .get_bssid = wpa_driver_nl80211_get_bssid,
9795 .get_ssid = wpa_driver_nl80211_get_ssid,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009796 .set_key = driver_nl80211_set_key,
9797 .scan2 = driver_nl80211_scan2,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009798 .sched_scan = wpa_driver_nl80211_sched_scan,
9799 .stop_sched_scan = wpa_driver_nl80211_stop_sched_scan,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009800 .get_scan_results2 = wpa_driver_nl80211_get_scan_results,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009801 .deauthenticate = driver_nl80211_deauthenticate,
9802 .authenticate = driver_nl80211_authenticate,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009803 .associate = wpa_driver_nl80211_associate,
9804 .global_init = nl80211_global_init,
9805 .global_deinit = nl80211_global_deinit,
9806 .init2 = wpa_driver_nl80211_init,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009807 .deinit = driver_nl80211_deinit,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009808 .get_capa = wpa_driver_nl80211_get_capa,
9809 .set_operstate = wpa_driver_nl80211_set_operstate,
9810 .set_supp_port = wpa_driver_nl80211_set_supp_port,
9811 .set_country = wpa_driver_nl80211_set_country,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009812 .set_ap = wpa_driver_nl80211_set_ap,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009813 .if_add = wpa_driver_nl80211_if_add,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009814 .if_remove = driver_nl80211_if_remove,
9815 .send_mlme = driver_nl80211_send_mlme,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009816 .get_hw_feature_data = wpa_driver_nl80211_get_hw_feature_data,
9817 .sta_add = wpa_driver_nl80211_sta_add,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009818 .sta_remove = driver_nl80211_sta_remove,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009819 .hapd_send_eapol = wpa_driver_nl80211_hapd_send_eapol,
9820 .sta_set_flags = wpa_driver_nl80211_sta_set_flags,
9821#ifdef HOSTAPD
9822 .hapd_init = i802_init,
9823 .hapd_deinit = i802_deinit,
Jouni Malinen75ecf522011-06-27 15:19:46 -07009824 .set_wds_sta = i802_set_wds_sta,
9825#endif /* HOSTAPD */
9826#if defined(HOSTAPD) || defined(CONFIG_AP)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009827 .get_seqnum = i802_get_seqnum,
9828 .flush = i802_flush,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009829 .get_inact_sec = i802_get_inact_sec,
9830 .sta_clear_stats = i802_sta_clear_stats,
9831 .set_rts = i802_set_rts,
9832 .set_frag = i802_set_frag,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009833 .set_tx_queue_params = i802_set_tx_queue_params,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009834 .set_sta_vlan = driver_nl80211_set_sta_vlan,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009835 .sta_deauth = i802_sta_deauth,
9836 .sta_disassoc = i802_sta_disassoc,
9837#endif /* HOSTAPD || CONFIG_AP */
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009838 .read_sta_data = driver_nl80211_read_sta_data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009839 .set_freq = i802_set_freq,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009840 .send_action = driver_nl80211_send_action,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009841 .send_action_cancel_wait = wpa_driver_nl80211_send_action_cancel_wait,
9842 .remain_on_channel = wpa_driver_nl80211_remain_on_channel,
9843 .cancel_remain_on_channel =
9844 wpa_driver_nl80211_cancel_remain_on_channel,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009845 .probe_req_report = driver_nl80211_probe_req_report,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009846 .deinit_ap = wpa_driver_nl80211_deinit_ap,
Dmitry Shmidt04949592012-07-19 12:16:46 -07009847 .deinit_p2p_cli = wpa_driver_nl80211_deinit_p2p_cli,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009848 .resume = wpa_driver_nl80211_resume,
9849 .send_ft_action = nl80211_send_ft_action,
9850 .signal_monitor = nl80211_signal_monitor,
9851 .signal_poll = nl80211_signal_poll,
9852 .send_frame = nl80211_send_frame,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009853 .shared_freq = wpa_driver_nl80211_shared_freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009854 .set_param = nl80211_set_param,
9855 .get_radio_name = nl80211_get_radio_name,
Jouni Malinen75ecf522011-06-27 15:19:46 -07009856 .add_pmkid = nl80211_add_pmkid,
9857 .remove_pmkid = nl80211_remove_pmkid,
9858 .flush_pmkid = nl80211_flush_pmkid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009859 .set_rekey_info = nl80211_set_rekey_info,
9860 .poll_client = nl80211_poll_client,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009861 .set_p2p_powersave = nl80211_set_p2p_powersave,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009862#ifdef CONFIG_TDLS
9863 .send_tdls_mgmt = nl80211_send_tdls_mgmt,
9864 .tdls_oper = nl80211_tdls_oper,
9865#endif /* CONFIG_TDLS */
Dmitry Shmidt700a1372013-03-15 14:14:44 -07009866 .update_ft_ies = wpa_driver_nl80211_update_ft_ies,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009867#ifdef ANDROID_P2P
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07009868 .set_noa = wpa_driver_set_p2p_noa,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009869 .get_noa = wpa_driver_get_p2p_noa,
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07009870 .set_ap_wps_ie = wpa_driver_set_ap_wps_p2p_ie,
9871#endif
Dmitry Shmidt738a26e2011-07-07 14:22:14 -07009872#ifdef ANDROID
9873 .driver_cmd = wpa_driver_nl80211_driver_cmd,
9874#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009875};