blob: b9503f18f2917eac3acae2e67f4ba12afd5ba5be [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);
181static void wpa_driver_nl80211_deinit(void *priv);
182
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700183struct i802_bss {
184 struct wpa_driver_nl80211_data *drv;
185 struct i802_bss *next;
186 int ifindex;
187 char ifname[IFNAMSIZ + 1];
188 char brname[IFNAMSIZ];
189 unsigned int beacon_set:1;
190 unsigned int added_if_into_bridge:1;
191 unsigned int added_bridge:1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700192 unsigned int in_deinit:1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800193
194 u8 addr[ETH_ALEN];
195
196 int freq;
197
198 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;
217 int has_capability;
218
219 int operstate;
220
221 int scan_complete_events;
222
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700223 struct nl_cb *nl_cb;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700224
225 u8 auth_bssid[ETH_ALEN];
226 u8 bssid[ETH_ALEN];
227 int associated;
228 u8 ssid[32];
229 size_t ssid_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800230 enum nl80211_iftype nlmode;
231 enum nl80211_iftype ap_scan_as_station;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700232 unsigned int assoc_freq;
233
234 int monitor_sock;
235 int monitor_ifidx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800236 int monitor_refcount;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700237
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800238 unsigned int disabled_11b_rates:1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700239 unsigned int pending_remain_on_chan:1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800240 unsigned int in_interface_list:1;
241 unsigned int device_ap_sme:1;
242 unsigned int poll_command_supported:1;
243 unsigned int data_tx_status:1;
244 unsigned int scan_for_auth:1;
245 unsigned int retry_auth:1;
246 unsigned int use_monitor:1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700247
248 u64 remain_on_chan_cookie;
249 u64 send_action_cookie;
250
251 unsigned int last_mgmt_freq;
252
253 struct wpa_driver_scan_filter *filter_ssids;
254 size_t num_filter_ssids;
255
256 struct i802_bss first_bss;
257
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800258 int eapol_tx_sock;
259
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700260#ifdef HOSTAPD
261 int eapol_sock; /* socket for EAPOL frames */
262
263 int default_if_indices[16];
264 int *if_indices;
265 int num_if_indices;
266
267 int last_freq;
268 int last_freq_ht;
269#endif /* HOSTAPD */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800270
271 /* From failed authentication command */
272 int auth_freq;
273 u8 auth_bssid_[ETH_ALEN];
274 u8 auth_ssid[32];
275 size_t auth_ssid_len;
276 int auth_alg;
277 u8 *auth_ie;
278 size_t auth_ie_len;
279 u8 auth_wep_key[4][16];
280 size_t auth_wep_key_len[4];
281 int auth_wep_tx_keyidx;
282 int auth_local_state_change;
283 int auth_p2p;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700284};
285
286
287static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx,
288 void *timeout_ctx);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800289static int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
290 enum nl80211_iftype nlmode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700291static int
292wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv);
293static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
294 const u8 *addr, int cmd, u16 reason_code,
295 int local_state_change);
296static void nl80211_remove_monitor_interface(
297 struct wpa_driver_nl80211_data *drv);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800298static int nl80211_send_frame_cmd(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700299 unsigned int freq, unsigned int wait,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800300 const u8 *buf, size_t buf_len, u64 *cookie,
301 int no_cck, int no_ack, int offchanok);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700302static int wpa_driver_nl80211_probe_req_report(void *priv, int report);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800303#ifdef ANDROID
304static int android_pno_start(struct i802_bss *bss,
305 struct wpa_driver_scan_params *params);
306static int android_pno_stop(struct i802_bss *bss);
307#endif /* ANDROID */
308#ifdef ANDROID_P2P
Dmitry Shmidt6e933c12011-09-27 12:29:26 -0700309int wpa_driver_set_p2p_noa(void *priv, u8 count, int start, int duration);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800310int wpa_driver_get_p2p_noa(void *priv, u8 *buf, size_t len);
Dmitry Shmidt6e933c12011-09-27 12:29:26 -0700311int wpa_driver_set_p2p_ps(void *priv, int legacy_ps, int opp_ps, int ctwindow);
312int wpa_driver_set_ap_wps_p2p_ie(void *priv, const struct wpabuf *beacon,
313 const struct wpabuf *proberesp,
314 const struct wpabuf *assocresp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700315
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800316#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700317#ifdef HOSTAPD
318static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
319static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
320static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
321static int wpa_driver_nl80211_if_remove(void *priv,
322 enum wpa_driver_if_type type,
323 const char *ifname);
324#else /* HOSTAPD */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800325static inline void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
326{
327}
328
329static inline void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
330{
331}
332
333static inline int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700334{
335 return 0;
336}
337#endif /* HOSTAPD */
Dmitry Shmidt738a26e2011-07-07 14:22:14 -0700338#ifdef ANDROID
339extern int wpa_driver_nl80211_driver_cmd(void *priv, char *cmd, char *buf,
340 size_t buf_len);
341#endif
342
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700343static int i802_set_freq(void *priv, struct hostapd_freq_params *freq);
344static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
345 int ifindex, int disabled);
346
347static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800348static int wpa_driver_nl80211_authenticate_retry(
349 struct wpa_driver_nl80211_data *drv);
350
351
352static int is_ap_interface(enum nl80211_iftype nlmode)
353{
354 return (nlmode == NL80211_IFTYPE_AP ||
355 nlmode == NL80211_IFTYPE_P2P_GO);
356}
357
358
359static int is_sta_interface(enum nl80211_iftype nlmode)
360{
361 return (nlmode == NL80211_IFTYPE_STATION ||
362 nlmode == NL80211_IFTYPE_P2P_CLIENT);
363}
364
365
366static int is_p2p_interface(enum nl80211_iftype nlmode)
367{
368 return (nlmode == NL80211_IFTYPE_P2P_CLIENT ||
369 nlmode == NL80211_IFTYPE_P2P_GO);
370}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700371
372
Jouni Malinen87fd2792011-05-16 18:35:42 +0300373struct nl80211_bss_info_arg {
374 struct wpa_driver_nl80211_data *drv;
375 struct wpa_scan_results *res;
376 unsigned int assoc_freq;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800377 u8 assoc_bssid[ETH_ALEN];
Jouni Malinen87fd2792011-05-16 18:35:42 +0300378};
379
380static int bss_info_handler(struct nl_msg *msg, void *arg);
381
382
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700383/* nl80211 code */
384static int ack_handler(struct nl_msg *msg, void *arg)
385{
386 int *err = arg;
387 *err = 0;
388 return NL_STOP;
389}
390
391static int finish_handler(struct nl_msg *msg, void *arg)
392{
393 int *ret = arg;
394 *ret = 0;
395 return NL_SKIP;
396}
397
398static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
399 void *arg)
400{
401 int *ret = arg;
402 *ret = err->error;
403 return NL_SKIP;
404}
405
406
407static int no_seq_check(struct nl_msg *msg, void *arg)
408{
409 return NL_OK;
410}
411
412
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800413static int send_and_recv(struct nl80211_global *global,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700414 struct nl_handle *nl_handle, struct nl_msg *msg,
415 int (*valid_handler)(struct nl_msg *, void *),
416 void *valid_data)
417{
418 struct nl_cb *cb;
419 int err = -ENOMEM;
420
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800421 cb = nl_cb_clone(global->nl_cb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700422 if (!cb)
423 goto out;
424
425 err = nl_send_auto_complete(nl_handle, msg);
426 if (err < 0)
427 goto out;
428
429 err = 1;
430
431 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
432 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
433 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
434
435 if (valid_handler)
436 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM,
437 valid_handler, valid_data);
438
439 while (err > 0)
440 nl_recvmsgs(nl_handle, cb);
441 out:
442 nl_cb_put(cb);
443 nlmsg_free(msg);
444 return err;
445}
446
447
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800448static int send_and_recv_msgs_global(struct nl80211_global *global,
449 struct nl_msg *msg,
450 int (*valid_handler)(struct nl_msg *, void *),
451 void *valid_data)
452{
453 return send_and_recv(global, global->nl, msg, valid_handler,
454 valid_data);
455}
456
Dmitry Shmidt04949592012-07-19 12:16:46 -0700457
Jouni Malinen80da0422012-08-09 15:29:25 -0700458#ifndef ANDROID
459static
460#endif
Dmitry Shmidt738a26e2011-07-07 14:22:14 -0700461int send_and_recv_msgs(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700462 struct nl_msg *msg,
463 int (*valid_handler)(struct nl_msg *, void *),
464 void *valid_data)
465{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800466 return send_and_recv(drv->global, drv->global->nl, msg,
467 valid_handler, valid_data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700468}
469
470
471struct family_data {
472 const char *group;
473 int id;
474};
475
476
477static int family_handler(struct nl_msg *msg, void *arg)
478{
479 struct family_data *res = arg;
480 struct nlattr *tb[CTRL_ATTR_MAX + 1];
481 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
482 struct nlattr *mcgrp;
483 int i;
484
485 nla_parse(tb, CTRL_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
486 genlmsg_attrlen(gnlh, 0), NULL);
487 if (!tb[CTRL_ATTR_MCAST_GROUPS])
488 return NL_SKIP;
489
490 nla_for_each_nested(mcgrp, tb[CTRL_ATTR_MCAST_GROUPS], i) {
491 struct nlattr *tb2[CTRL_ATTR_MCAST_GRP_MAX + 1];
492 nla_parse(tb2, CTRL_ATTR_MCAST_GRP_MAX, nla_data(mcgrp),
493 nla_len(mcgrp), NULL);
494 if (!tb2[CTRL_ATTR_MCAST_GRP_NAME] ||
495 !tb2[CTRL_ATTR_MCAST_GRP_ID] ||
496 os_strncmp(nla_data(tb2[CTRL_ATTR_MCAST_GRP_NAME]),
497 res->group,
498 nla_len(tb2[CTRL_ATTR_MCAST_GRP_NAME])) != 0)
499 continue;
500 res->id = nla_get_u32(tb2[CTRL_ATTR_MCAST_GRP_ID]);
501 break;
502 };
503
504 return NL_SKIP;
505}
506
507
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800508static int nl_get_multicast_id(struct nl80211_global *global,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700509 const char *family, const char *group)
510{
511 struct nl_msg *msg;
512 int ret = -1;
513 struct family_data res = { group, -ENOENT };
514
515 msg = nlmsg_alloc();
516 if (!msg)
517 return -ENOMEM;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800518 genlmsg_put(msg, 0, 0, genl_ctrl_resolve(global->nl, "nlctrl"),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700519 0, 0, CTRL_CMD_GETFAMILY, 0);
520 NLA_PUT_STRING(msg, CTRL_ATTR_FAMILY_NAME, family);
521
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800522 ret = send_and_recv_msgs_global(global, msg, family_handler, &res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700523 msg = NULL;
524 if (ret == 0)
525 ret = res.id;
526
527nla_put_failure:
528 nlmsg_free(msg);
529 return ret;
530}
531
532
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800533static void * nl80211_cmd(struct wpa_driver_nl80211_data *drv,
534 struct nl_msg *msg, int flags, uint8_t cmd)
535{
536 return genlmsg_put(msg, 0, 0, drv->global->nl80211_id,
537 0, flags, cmd, 0);
538}
539
540
541struct wiphy_idx_data {
542 int wiphy_idx;
543};
544
545
546static int netdev_info_handler(struct nl_msg *msg, void *arg)
547{
548 struct nlattr *tb[NL80211_ATTR_MAX + 1];
549 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
550 struct wiphy_idx_data *info = arg;
551
552 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
553 genlmsg_attrlen(gnlh, 0), NULL);
554
555 if (tb[NL80211_ATTR_WIPHY])
556 info->wiphy_idx = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
557
558 return NL_SKIP;
559}
560
561
562static int nl80211_get_wiphy_index(struct i802_bss *bss)
563{
564 struct nl_msg *msg;
565 struct wiphy_idx_data data = {
566 .wiphy_idx = -1,
567 };
568
569 msg = nlmsg_alloc();
570 if (!msg)
571 return -1;
572
573 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_GET_INTERFACE);
574
575 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
576
577 if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0)
578 return data.wiphy_idx;
579 msg = NULL;
580nla_put_failure:
581 nlmsg_free(msg);
582 return -1;
583}
584
585
586static int nl80211_register_beacons(struct wpa_driver_nl80211_data *drv,
587 struct nl80211_wiphy_data *w)
588{
589 struct nl_msg *msg;
590 int ret = -1;
591
592 msg = nlmsg_alloc();
593 if (!msg)
594 return -1;
595
596 nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_BEACONS);
597
598 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, w->wiphy_idx);
599
600 ret = send_and_recv(drv->global, w->nl_beacons, msg, NULL, NULL);
601 msg = NULL;
602 if (ret) {
603 wpa_printf(MSG_DEBUG, "nl80211: Register beacons command "
604 "failed: ret=%d (%s)",
605 ret, strerror(-ret));
606 goto nla_put_failure;
607 }
608 ret = 0;
609nla_put_failure:
610 nlmsg_free(msg);
611 return ret;
612}
613
614
615static void nl80211_recv_beacons(int sock, void *eloop_ctx, void *handle)
616{
617 struct nl80211_wiphy_data *w = eloop_ctx;
618
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800619 wpa_printf(MSG_EXCESSIVE, "nl80211: Beacon event message available");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800620
621 nl_recvmsgs(handle, w->nl_cb);
622}
623
624
625static int process_beacon_event(struct nl_msg *msg, void *arg)
626{
627 struct nl80211_wiphy_data *w = arg;
628 struct wpa_driver_nl80211_data *drv;
629 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
630 struct nlattr *tb[NL80211_ATTR_MAX + 1];
631 union wpa_event_data event;
632
633 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
634 genlmsg_attrlen(gnlh, 0), NULL);
635
636 if (gnlh->cmd != NL80211_CMD_FRAME) {
637 wpa_printf(MSG_DEBUG, "nl80211: Unexpected beacon event? (%d)",
638 gnlh->cmd);
639 return NL_SKIP;
640 }
641
642 if (!tb[NL80211_ATTR_FRAME])
643 return NL_SKIP;
644
645 dl_list_for_each(drv, &w->drvs, struct wpa_driver_nl80211_data,
646 wiphy_list) {
647 os_memset(&event, 0, sizeof(event));
648 event.rx_mgmt.frame = nla_data(tb[NL80211_ATTR_FRAME]);
649 event.rx_mgmt.frame_len = nla_len(tb[NL80211_ATTR_FRAME]);
650 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
651 }
652
653 return NL_SKIP;
654}
655
656
657static struct nl80211_wiphy_data *
658nl80211_get_wiphy_data_ap(struct i802_bss *bss)
659{
660 static DEFINE_DL_LIST(nl80211_wiphys);
661 struct nl80211_wiphy_data *w;
662 int wiphy_idx, found = 0;
663 struct i802_bss *tmp_bss;
664
665 if (bss->wiphy_data != NULL)
666 return bss->wiphy_data;
667
668 wiphy_idx = nl80211_get_wiphy_index(bss);
669
670 dl_list_for_each(w, &nl80211_wiphys, struct nl80211_wiphy_data, list) {
671 if (w->wiphy_idx == wiphy_idx)
672 goto add;
673 }
674
675 /* alloc new one */
676 w = os_zalloc(sizeof(*w));
677 if (w == NULL)
678 return NULL;
679 w->wiphy_idx = wiphy_idx;
680 dl_list_init(&w->bsss);
681 dl_list_init(&w->drvs);
682
683 w->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
684 if (!w->nl_cb) {
685 os_free(w);
686 return NULL;
687 }
688 nl_cb_set(w->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL);
689 nl_cb_set(w->nl_cb, NL_CB_VALID, NL_CB_CUSTOM, process_beacon_event,
690 w);
691
692 w->nl_beacons = nl_create_handle(bss->drv->global->nl_cb,
693 "wiphy beacons");
694 if (w->nl_beacons == NULL) {
695 os_free(w);
696 return NULL;
697 }
698
699 if (nl80211_register_beacons(bss->drv, w)) {
700 nl_destroy_handles(&w->nl_beacons);
701 os_free(w);
702 return NULL;
703 }
704
705 eloop_register_read_sock(nl_socket_get_fd(w->nl_beacons),
706 nl80211_recv_beacons, w, w->nl_beacons);
707
708 dl_list_add(&nl80211_wiphys, &w->list);
709
710add:
711 /* drv entry for this bss already there? */
712 dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
713 if (tmp_bss->drv == bss->drv) {
714 found = 1;
715 break;
716 }
717 }
718 /* if not add it */
719 if (!found)
720 dl_list_add(&w->drvs, &bss->drv->wiphy_list);
721
722 dl_list_add(&w->bsss, &bss->wiphy_list);
723 bss->wiphy_data = w;
724 return w;
725}
726
727
728static void nl80211_put_wiphy_data_ap(struct i802_bss *bss)
729{
730 struct nl80211_wiphy_data *w = bss->wiphy_data;
731 struct i802_bss *tmp_bss;
732 int found = 0;
733
734 if (w == NULL)
735 return;
736 bss->wiphy_data = NULL;
737 dl_list_del(&bss->wiphy_list);
738
739 /* still any for this drv present? */
740 dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
741 if (tmp_bss->drv == bss->drv) {
742 found = 1;
743 break;
744 }
745 }
746 /* if not remove it */
747 if (!found)
748 dl_list_del(&bss->drv->wiphy_list);
749
750 if (!dl_list_empty(&w->bsss))
751 return;
752
753 eloop_unregister_read_sock(nl_socket_get_fd(w->nl_beacons));
754
755 nl_cb_put(w->nl_cb);
756 nl_destroy_handles(&w->nl_beacons);
757 dl_list_del(&w->list);
758 os_free(w);
759}
760
761
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700762static int wpa_driver_nl80211_get_bssid(void *priv, u8 *bssid)
763{
764 struct i802_bss *bss = priv;
765 struct wpa_driver_nl80211_data *drv = bss->drv;
766 if (!drv->associated)
767 return -1;
768 os_memcpy(bssid, drv->bssid, ETH_ALEN);
769 return 0;
770}
771
772
773static int wpa_driver_nl80211_get_ssid(void *priv, u8 *ssid)
774{
775 struct i802_bss *bss = priv;
776 struct wpa_driver_nl80211_data *drv = bss->drv;
777 if (!drv->associated)
778 return -1;
779 os_memcpy(ssid, drv->ssid, drv->ssid_len);
780 return drv->ssid_len;
781}
782
783
784static void wpa_driver_nl80211_event_link(struct wpa_driver_nl80211_data *drv,
785 char *buf, size_t len, int del)
786{
787 union wpa_event_data event;
788
789 os_memset(&event, 0, sizeof(event));
790 if (len > sizeof(event.interface_status.ifname))
791 len = sizeof(event.interface_status.ifname) - 1;
792 os_memcpy(event.interface_status.ifname, buf, len);
793 event.interface_status.ievent = del ? EVENT_INTERFACE_REMOVED :
794 EVENT_INTERFACE_ADDED;
795
796 wpa_printf(MSG_DEBUG, "RTM_%sLINK, IFLA_IFNAME: Interface '%s' %s",
797 del ? "DEL" : "NEW",
798 event.interface_status.ifname,
799 del ? "removed" : "added");
800
801 if (os_strcmp(drv->first_bss.ifname, event.interface_status.ifname) == 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700802 if (del) {
803 if (drv->if_removed) {
804 wpa_printf(MSG_DEBUG, "nl80211: if_removed "
805 "already set - ignore event");
806 return;
807 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700808 drv->if_removed = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700809 } else {
810 if (if_nametoindex(drv->first_bss.ifname) == 0) {
811 wpa_printf(MSG_DEBUG, "nl80211: Interface %s "
812 "does not exist - ignore "
813 "RTM_NEWLINK",
814 drv->first_bss.ifname);
815 return;
816 }
817 if (!drv->if_removed) {
818 wpa_printf(MSG_DEBUG, "nl80211: if_removed "
819 "already cleared - ignore event");
820 return;
821 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700822 drv->if_removed = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700823 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700824 }
825
826 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
827}
828
829
830static int wpa_driver_nl80211_own_ifname(struct wpa_driver_nl80211_data *drv,
831 u8 *buf, size_t len)
832{
833 int attrlen, rta_len;
834 struct rtattr *attr;
835
836 attrlen = len;
837 attr = (struct rtattr *) buf;
838
839 rta_len = RTA_ALIGN(sizeof(struct rtattr));
840 while (RTA_OK(attr, attrlen)) {
841 if (attr->rta_type == IFLA_IFNAME) {
842 if (os_strcmp(((char *) attr) + rta_len, drv->first_bss.ifname)
843 == 0)
844 return 1;
845 else
846 break;
847 }
848 attr = RTA_NEXT(attr, attrlen);
849 }
850
851 return 0;
852}
853
854
855static int wpa_driver_nl80211_own_ifindex(struct wpa_driver_nl80211_data *drv,
856 int ifindex, u8 *buf, size_t len)
857{
858 if (drv->ifindex == ifindex)
859 return 1;
860
861 if (drv->if_removed && wpa_driver_nl80211_own_ifname(drv, buf, len)) {
862 drv->first_bss.ifindex = if_nametoindex(drv->first_bss.ifname);
863 wpa_printf(MSG_DEBUG, "nl80211: Update ifindex for a removed "
864 "interface");
865 wpa_driver_nl80211_finish_drv_init(drv);
866 return 1;
867 }
868
869 return 0;
870}
871
872
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800873static struct wpa_driver_nl80211_data *
874nl80211_find_drv(struct nl80211_global *global, int idx, u8 *buf, size_t len)
875{
876 struct wpa_driver_nl80211_data *drv;
877 dl_list_for_each(drv, &global->interfaces,
878 struct wpa_driver_nl80211_data, list) {
879 if (wpa_driver_nl80211_own_ifindex(drv, idx, buf, len) ||
880 have_ifidx(drv, idx))
881 return drv;
882 }
883 return NULL;
884}
885
886
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700887static void wpa_driver_nl80211_event_rtm_newlink(void *ctx,
888 struct ifinfomsg *ifi,
889 u8 *buf, size_t len)
890{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800891 struct nl80211_global *global = ctx;
892 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700893 int attrlen, rta_len;
894 struct rtattr *attr;
895 u32 brid = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800896 char namebuf[IFNAMSIZ];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700897
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800898 drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
899 if (!drv) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700900 wpa_printf(MSG_DEBUG, "nl80211: Ignore event for foreign "
901 "ifindex %d", ifi->ifi_index);
902 return;
903 }
904
905 wpa_printf(MSG_DEBUG, "RTM_NEWLINK: operstate=%d ifi_flags=0x%x "
906 "(%s%s%s%s)",
907 drv->operstate, ifi->ifi_flags,
908 (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
909 (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
910 (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
911 (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
912
913 if (!drv->if_disabled && !(ifi->ifi_flags & IFF_UP)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800914 if (if_indextoname(ifi->ifi_index, namebuf) &&
915 linux_iface_up(drv->global->ioctl_sock,
916 drv->first_bss.ifname) > 0) {
917 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
918 "event since interface %s is up", namebuf);
919 return;
920 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700921 wpa_printf(MSG_DEBUG, "nl80211: Interface down");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800922 if (drv->ignore_if_down_event) {
923 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
924 "event generated by mode change");
925 drv->ignore_if_down_event = 0;
926 } else {
927 drv->if_disabled = 1;
928 wpa_supplicant_event(drv->ctx,
929 EVENT_INTERFACE_DISABLED, NULL);
930 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700931 }
932
933 if (drv->if_disabled && (ifi->ifi_flags & IFF_UP)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800934 if (if_indextoname(ifi->ifi_index, namebuf) &&
935 linux_iface_up(drv->global->ioctl_sock,
936 drv->first_bss.ifname) == 0) {
937 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
938 "event since interface %s is down",
939 namebuf);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700940 } else if (if_nametoindex(drv->first_bss.ifname) == 0) {
941 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
942 "event since interface %s does not exist",
943 drv->first_bss.ifname);
944 } else if (drv->if_removed) {
945 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
946 "event since interface %s is marked "
947 "removed", drv->first_bss.ifname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800948 } else {
949 wpa_printf(MSG_DEBUG, "nl80211: Interface up");
950 drv->if_disabled = 0;
951 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_ENABLED,
952 NULL);
953 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700954 }
955
956 /*
957 * Some drivers send the association event before the operup event--in
958 * this case, lifting operstate in wpa_driver_nl80211_set_operstate()
959 * fails. This will hit us when wpa_supplicant does not need to do
960 * IEEE 802.1X authentication
961 */
962 if (drv->operstate == 1 &&
963 (ifi->ifi_flags & (IFF_LOWER_UP | IFF_DORMANT)) == IFF_LOWER_UP &&
964 !(ifi->ifi_flags & IFF_RUNNING))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800965 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700966 -1, IF_OPER_UP);
967
968 attrlen = len;
969 attr = (struct rtattr *) buf;
970 rta_len = RTA_ALIGN(sizeof(struct rtattr));
971 while (RTA_OK(attr, attrlen)) {
972 if (attr->rta_type == IFLA_IFNAME) {
973 wpa_driver_nl80211_event_link(
974 drv,
975 ((char *) attr) + rta_len,
976 attr->rta_len - rta_len, 0);
977 } else if (attr->rta_type == IFLA_MASTER)
978 brid = nla_get_u32((struct nlattr *) attr);
979 attr = RTA_NEXT(attr, attrlen);
980 }
981
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700982 if (ifi->ifi_family == AF_BRIDGE && brid) {
983 /* device has been added to bridge */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700984 if_indextoname(brid, namebuf);
985 wpa_printf(MSG_DEBUG, "nl80211: Add ifindex %u for bridge %s",
986 brid, namebuf);
987 add_ifidx(drv, brid);
988 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700989}
990
991
992static void wpa_driver_nl80211_event_rtm_dellink(void *ctx,
993 struct ifinfomsg *ifi,
994 u8 *buf, size_t len)
995{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800996 struct nl80211_global *global = ctx;
997 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700998 int attrlen, rta_len;
999 struct rtattr *attr;
1000 u32 brid = 0;
1001
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001002 drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
1003 if (!drv) {
1004 wpa_printf(MSG_DEBUG, "nl80211: Ignore dellink event for "
1005 "foreign ifindex %d", ifi->ifi_index);
1006 return;
1007 }
1008
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001009 attrlen = len;
1010 attr = (struct rtattr *) buf;
1011
1012 rta_len = RTA_ALIGN(sizeof(struct rtattr));
1013 while (RTA_OK(attr, attrlen)) {
1014 if (attr->rta_type == IFLA_IFNAME) {
1015 wpa_driver_nl80211_event_link(
1016 drv,
1017 ((char *) attr) + rta_len,
1018 attr->rta_len - rta_len, 1);
1019 } else if (attr->rta_type == IFLA_MASTER)
1020 brid = nla_get_u32((struct nlattr *) attr);
1021 attr = RTA_NEXT(attr, attrlen);
1022 }
1023
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001024 if (ifi->ifi_family == AF_BRIDGE && brid) {
1025 /* device has been removed from bridge */
1026 char namebuf[IFNAMSIZ];
1027 if_indextoname(brid, namebuf);
1028 wpa_printf(MSG_DEBUG, "nl80211: Remove ifindex %u for bridge "
1029 "%s", brid, namebuf);
1030 del_ifidx(drv, brid);
1031 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001032}
1033
1034
1035static void mlme_event_auth(struct wpa_driver_nl80211_data *drv,
1036 const u8 *frame, size_t len)
1037{
1038 const struct ieee80211_mgmt *mgmt;
1039 union wpa_event_data event;
1040
1041 mgmt = (const struct ieee80211_mgmt *) frame;
1042 if (len < 24 + sizeof(mgmt->u.auth)) {
1043 wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
1044 "frame");
1045 return;
1046 }
1047
1048 os_memcpy(drv->auth_bssid, mgmt->sa, ETH_ALEN);
1049 os_memset(&event, 0, sizeof(event));
1050 os_memcpy(event.auth.peer, mgmt->sa, ETH_ALEN);
1051 event.auth.auth_type = le_to_host16(mgmt->u.auth.auth_alg);
1052 event.auth.status_code = le_to_host16(mgmt->u.auth.status_code);
1053 if (len > 24 + sizeof(mgmt->u.auth)) {
1054 event.auth.ies = mgmt->u.auth.variable;
1055 event.auth.ies_len = len - 24 - sizeof(mgmt->u.auth);
1056 }
1057
1058 wpa_supplicant_event(drv->ctx, EVENT_AUTH, &event);
1059}
1060
1061
Jouni Malinen87fd2792011-05-16 18:35:42 +03001062static unsigned int nl80211_get_assoc_freq(struct wpa_driver_nl80211_data *drv)
1063{
1064 struct nl_msg *msg;
1065 int ret;
1066 struct nl80211_bss_info_arg arg;
1067
1068 os_memset(&arg, 0, sizeof(arg));
1069 msg = nlmsg_alloc();
1070 if (!msg)
1071 goto nla_put_failure;
1072
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001073 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
Jouni Malinen87fd2792011-05-16 18:35:42 +03001074 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
1075
1076 arg.drv = drv;
1077 ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
1078 msg = NULL;
1079 if (ret == 0) {
1080 wpa_printf(MSG_DEBUG, "nl80211: Operating frequency for the "
1081 "associated BSS from scan results: %u MHz",
1082 arg.assoc_freq);
1083 return arg.assoc_freq ? arg.assoc_freq : drv->assoc_freq;
1084 }
1085 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
1086 "(%s)", ret, strerror(-ret));
1087nla_put_failure:
1088 nlmsg_free(msg);
1089 return drv->assoc_freq;
1090}
1091
1092
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001093static void mlme_event_assoc(struct wpa_driver_nl80211_data *drv,
1094 const u8 *frame, size_t len)
1095{
1096 const struct ieee80211_mgmt *mgmt;
1097 union wpa_event_data event;
1098 u16 status;
1099
1100 mgmt = (const struct ieee80211_mgmt *) frame;
1101 if (len < 24 + sizeof(mgmt->u.assoc_resp)) {
1102 wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
1103 "frame");
1104 return;
1105 }
1106
1107 status = le_to_host16(mgmt->u.assoc_resp.status_code);
1108 if (status != WLAN_STATUS_SUCCESS) {
1109 os_memset(&event, 0, sizeof(event));
1110 event.assoc_reject.bssid = mgmt->bssid;
1111 if (len > 24 + sizeof(mgmt->u.assoc_resp)) {
1112 event.assoc_reject.resp_ies =
1113 (u8 *) mgmt->u.assoc_resp.variable;
1114 event.assoc_reject.resp_ies_len =
1115 len - 24 - sizeof(mgmt->u.assoc_resp);
1116 }
1117 event.assoc_reject.status_code = status;
1118
1119 wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
1120 return;
1121 }
1122
1123 drv->associated = 1;
1124 os_memcpy(drv->bssid, mgmt->sa, ETH_ALEN);
1125
1126 os_memset(&event, 0, sizeof(event));
1127 if (len > 24 + sizeof(mgmt->u.assoc_resp)) {
1128 event.assoc_info.resp_ies = (u8 *) mgmt->u.assoc_resp.variable;
1129 event.assoc_info.resp_ies_len =
1130 len - 24 - sizeof(mgmt->u.assoc_resp);
1131 }
1132
1133 event.assoc_info.freq = drv->assoc_freq;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001134
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001135 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
1136}
1137
1138
1139static void mlme_event_connect(struct wpa_driver_nl80211_data *drv,
1140 enum nl80211_commands cmd, struct nlattr *status,
1141 struct nlattr *addr, struct nlattr *req_ie,
1142 struct nlattr *resp_ie)
1143{
1144 union wpa_event_data event;
1145
1146 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
1147 /*
1148 * Avoid reporting two association events that would confuse
1149 * the core code.
1150 */
1151 wpa_printf(MSG_DEBUG, "nl80211: Ignore connect event (cmd=%d) "
1152 "when using userspace SME", cmd);
1153 return;
1154 }
1155
1156 os_memset(&event, 0, sizeof(event));
1157 if (cmd == NL80211_CMD_CONNECT &&
1158 nla_get_u16(status) != WLAN_STATUS_SUCCESS) {
1159 if (addr)
1160 event.assoc_reject.bssid = nla_data(addr);
1161 if (resp_ie) {
1162 event.assoc_reject.resp_ies = nla_data(resp_ie);
1163 event.assoc_reject.resp_ies_len = nla_len(resp_ie);
1164 }
1165 event.assoc_reject.status_code = nla_get_u16(status);
1166 wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
1167 return;
1168 }
1169
1170 drv->associated = 1;
1171 if (addr)
1172 os_memcpy(drv->bssid, nla_data(addr), ETH_ALEN);
1173
1174 if (req_ie) {
1175 event.assoc_info.req_ies = nla_data(req_ie);
1176 event.assoc_info.req_ies_len = nla_len(req_ie);
1177 }
1178 if (resp_ie) {
1179 event.assoc_info.resp_ies = nla_data(resp_ie);
1180 event.assoc_info.resp_ies_len = nla_len(resp_ie);
1181 }
1182
Jouni Malinen87fd2792011-05-16 18:35:42 +03001183 event.assoc_info.freq = nl80211_get_assoc_freq(drv);
1184
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001185 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
1186}
1187
1188
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001189static void mlme_event_disconnect(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001190 struct nlattr *reason, struct nlattr *addr,
1191 struct nlattr *by_ap)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001192{
1193 union wpa_event_data data;
1194
1195 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
1196 /*
1197 * Avoid reporting two disassociation events that could
1198 * confuse the core code.
1199 */
1200 wpa_printf(MSG_DEBUG, "nl80211: Ignore disconnect "
1201 "event when using userspace SME");
1202 return;
1203 }
1204
1205 drv->associated = 0;
1206 os_memset(&data, 0, sizeof(data));
1207 if (reason)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001208 data.deauth_info.reason_code = nla_get_u16(reason);
1209 data.deauth_info.locally_generated = by_ap == NULL;
1210 wpa_supplicant_event(drv->ctx, EVENT_DEAUTH, &data);
1211}
1212
1213
1214static void mlme_event_ch_switch(struct wpa_driver_nl80211_data *drv,
1215 struct nlattr *freq, struct nlattr *type)
1216{
1217 union wpa_event_data data;
1218 int ht_enabled = 1;
1219 int chan_offset = 0;
1220
1221 wpa_printf(MSG_DEBUG, "nl80211: Channel switch event");
1222
1223 if (!freq || !type)
1224 return;
1225
1226 switch (nla_get_u32(type)) {
1227 case NL80211_CHAN_NO_HT:
1228 ht_enabled = 0;
1229 break;
1230 case NL80211_CHAN_HT20:
1231 break;
1232 case NL80211_CHAN_HT40PLUS:
1233 chan_offset = 1;
1234 break;
1235 case NL80211_CHAN_HT40MINUS:
1236 chan_offset = -1;
1237 break;
1238 }
1239
1240 data.ch_switch.freq = nla_get_u32(freq);
1241 data.ch_switch.ht_enabled = ht_enabled;
1242 data.ch_switch.ch_offset = chan_offset;
1243
1244 wpa_supplicant_event(drv->ctx, EVENT_CH_SWITCH, &data);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001245}
1246
1247
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001248static void mlme_timeout_event(struct wpa_driver_nl80211_data *drv,
1249 enum nl80211_commands cmd, struct nlattr *addr)
1250{
1251 union wpa_event_data event;
1252 enum wpa_event_type ev;
1253
1254 if (nla_len(addr) != ETH_ALEN)
1255 return;
1256
1257 wpa_printf(MSG_DEBUG, "nl80211: MLME event %d; timeout with " MACSTR,
1258 cmd, MAC2STR((u8 *) nla_data(addr)));
1259
1260 if (cmd == NL80211_CMD_AUTHENTICATE)
1261 ev = EVENT_AUTH_TIMED_OUT;
1262 else if (cmd == NL80211_CMD_ASSOCIATE)
1263 ev = EVENT_ASSOC_TIMED_OUT;
1264 else
1265 return;
1266
1267 os_memset(&event, 0, sizeof(event));
1268 os_memcpy(event.timeout_event.addr, nla_data(addr), ETH_ALEN);
1269 wpa_supplicant_event(drv->ctx, ev, &event);
1270}
1271
1272
1273static void mlme_event_mgmt(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001274 struct nlattr *freq, struct nlattr *sig,
1275 const u8 *frame, size_t len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001276{
1277 const struct ieee80211_mgmt *mgmt;
1278 union wpa_event_data event;
1279 u16 fc, stype;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001280 int ssi_signal = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001281
1282 mgmt = (const struct ieee80211_mgmt *) frame;
1283 if (len < 24) {
1284 wpa_printf(MSG_DEBUG, "nl80211: Too short action frame");
1285 return;
1286 }
1287
1288 fc = le_to_host16(mgmt->frame_control);
1289 stype = WLAN_FC_GET_STYPE(fc);
1290
Dmitry Shmidt04949592012-07-19 12:16:46 -07001291 if (sig)
1292 ssi_signal = (s32) nla_get_u32(sig);
1293
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001294 os_memset(&event, 0, sizeof(event));
1295 if (freq) {
1296 event.rx_action.freq = nla_get_u32(freq);
1297 drv->last_mgmt_freq = event.rx_action.freq;
1298 }
1299 if (stype == WLAN_FC_STYPE_ACTION) {
1300 event.rx_action.da = mgmt->da;
1301 event.rx_action.sa = mgmt->sa;
1302 event.rx_action.bssid = mgmt->bssid;
1303 event.rx_action.category = mgmt->u.action.category;
1304 event.rx_action.data = &mgmt->u.action.category + 1;
1305 event.rx_action.len = frame + len - event.rx_action.data;
1306 wpa_supplicant_event(drv->ctx, EVENT_RX_ACTION, &event);
1307 } else {
1308 event.rx_mgmt.frame = frame;
1309 event.rx_mgmt.frame_len = len;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001310 event.rx_mgmt.ssi_signal = ssi_signal;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001311 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
1312 }
1313}
1314
1315
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001316static void mlme_event_mgmt_tx_status(struct wpa_driver_nl80211_data *drv,
1317 struct nlattr *cookie, const u8 *frame,
1318 size_t len, struct nlattr *ack)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001319{
1320 union wpa_event_data event;
1321 const struct ieee80211_hdr *hdr;
1322 u16 fc;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001323
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001324 if (!is_ap_interface(drv->nlmode)) {
1325 u64 cookie_val;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001326
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001327 if (!cookie)
1328 return;
1329
1330 cookie_val = nla_get_u64(cookie);
1331 wpa_printf(MSG_DEBUG, "nl80211: Action TX status:"
1332 " cookie=0%llx%s (ack=%d)",
1333 (long long unsigned int) cookie_val,
1334 cookie_val == drv->send_action_cookie ?
1335 " (match)" : " (unknown)", ack != NULL);
1336 if (cookie_val != drv->send_action_cookie)
1337 return;
1338 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001339
1340 hdr = (const struct ieee80211_hdr *) frame;
1341 fc = le_to_host16(hdr->frame_control);
1342
1343 os_memset(&event, 0, sizeof(event));
1344 event.tx_status.type = WLAN_FC_GET_TYPE(fc);
1345 event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
1346 event.tx_status.dst = hdr->addr1;
1347 event.tx_status.data = frame;
1348 event.tx_status.data_len = len;
1349 event.tx_status.ack = ack != NULL;
1350 wpa_supplicant_event(drv->ctx, EVENT_TX_STATUS, &event);
1351}
1352
1353
1354static void mlme_event_deauth_disassoc(struct wpa_driver_nl80211_data *drv,
1355 enum wpa_event_type type,
1356 const u8 *frame, size_t len)
1357{
1358 const struct ieee80211_mgmt *mgmt;
1359 union wpa_event_data event;
1360 const u8 *bssid = NULL;
1361 u16 reason_code = 0;
1362
1363 mgmt = (const struct ieee80211_mgmt *) frame;
1364 if (len >= 24) {
1365 bssid = mgmt->bssid;
1366
1367 if (drv->associated != 0 &&
1368 os_memcmp(bssid, drv->bssid, ETH_ALEN) != 0 &&
1369 os_memcmp(bssid, drv->auth_bssid, ETH_ALEN) != 0) {
1370 /*
1371 * We have presumably received this deauth as a
1372 * response to a clear_state_mismatch() outgoing
1373 * deauth. Don't let it take us offline!
1374 */
1375 wpa_printf(MSG_DEBUG, "nl80211: Deauth received "
1376 "from Unknown BSSID " MACSTR " -- ignoring",
1377 MAC2STR(bssid));
1378 return;
1379 }
1380 }
1381
1382 drv->associated = 0;
1383 os_memset(&event, 0, sizeof(event));
1384
1385 /* Note: Same offset for Reason Code in both frame subtypes */
1386 if (len >= 24 + sizeof(mgmt->u.deauth))
1387 reason_code = le_to_host16(mgmt->u.deauth.reason_code);
1388
1389 if (type == EVENT_DISASSOC) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001390 event.disassoc_info.locally_generated =
1391 !os_memcmp(mgmt->sa, drv->first_bss.addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001392 event.disassoc_info.addr = bssid;
1393 event.disassoc_info.reason_code = reason_code;
1394 if (frame + len > mgmt->u.disassoc.variable) {
1395 event.disassoc_info.ie = mgmt->u.disassoc.variable;
1396 event.disassoc_info.ie_len = frame + len -
1397 mgmt->u.disassoc.variable;
1398 }
1399 } else {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001400 event.deauth_info.locally_generated =
1401 !os_memcmp(mgmt->sa, drv->first_bss.addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001402 event.deauth_info.addr = bssid;
1403 event.deauth_info.reason_code = reason_code;
1404 if (frame + len > mgmt->u.deauth.variable) {
1405 event.deauth_info.ie = mgmt->u.deauth.variable;
1406 event.deauth_info.ie_len = frame + len -
1407 mgmt->u.deauth.variable;
1408 }
1409 }
1410
1411 wpa_supplicant_event(drv->ctx, type, &event);
1412}
1413
1414
1415static void mlme_event_unprot_disconnect(struct wpa_driver_nl80211_data *drv,
1416 enum wpa_event_type type,
1417 const u8 *frame, size_t len)
1418{
1419 const struct ieee80211_mgmt *mgmt;
1420 union wpa_event_data event;
1421 u16 reason_code = 0;
1422
1423 if (len < 24)
1424 return;
1425
1426 mgmt = (const struct ieee80211_mgmt *) frame;
1427
1428 os_memset(&event, 0, sizeof(event));
1429 /* Note: Same offset for Reason Code in both frame subtypes */
1430 if (len >= 24 + sizeof(mgmt->u.deauth))
1431 reason_code = le_to_host16(mgmt->u.deauth.reason_code);
1432
1433 if (type == EVENT_UNPROT_DISASSOC) {
1434 event.unprot_disassoc.sa = mgmt->sa;
1435 event.unprot_disassoc.da = mgmt->da;
1436 event.unprot_disassoc.reason_code = reason_code;
1437 } else {
1438 event.unprot_deauth.sa = mgmt->sa;
1439 event.unprot_deauth.da = mgmt->da;
1440 event.unprot_deauth.reason_code = reason_code;
1441 }
1442
1443 wpa_supplicant_event(drv->ctx, type, &event);
1444}
1445
1446
1447static void mlme_event(struct wpa_driver_nl80211_data *drv,
1448 enum nl80211_commands cmd, struct nlattr *frame,
1449 struct nlattr *addr, struct nlattr *timed_out,
1450 struct nlattr *freq, struct nlattr *ack,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001451 struct nlattr *cookie, struct nlattr *sig)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001452{
1453 if (timed_out && addr) {
1454 mlme_timeout_event(drv, cmd, addr);
1455 return;
1456 }
1457
1458 if (frame == NULL) {
1459 wpa_printf(MSG_DEBUG, "nl80211: MLME event %d without frame "
1460 "data", cmd);
1461 return;
1462 }
1463
1464 wpa_printf(MSG_DEBUG, "nl80211: MLME event %d", cmd);
1465 wpa_hexdump(MSG_MSGDUMP, "nl80211: MLME event frame",
1466 nla_data(frame), nla_len(frame));
1467
1468 switch (cmd) {
1469 case NL80211_CMD_AUTHENTICATE:
1470 mlme_event_auth(drv, nla_data(frame), nla_len(frame));
1471 break;
1472 case NL80211_CMD_ASSOCIATE:
1473 mlme_event_assoc(drv, nla_data(frame), nla_len(frame));
1474 break;
1475 case NL80211_CMD_DEAUTHENTICATE:
1476 mlme_event_deauth_disassoc(drv, EVENT_DEAUTH,
1477 nla_data(frame), nla_len(frame));
1478 break;
1479 case NL80211_CMD_DISASSOCIATE:
1480 mlme_event_deauth_disassoc(drv, EVENT_DISASSOC,
1481 nla_data(frame), nla_len(frame));
1482 break;
1483 case NL80211_CMD_FRAME:
Dmitry Shmidt04949592012-07-19 12:16:46 -07001484 mlme_event_mgmt(drv, freq, sig, nla_data(frame),
1485 nla_len(frame));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001486 break;
1487 case NL80211_CMD_FRAME_TX_STATUS:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001488 mlme_event_mgmt_tx_status(drv, cookie, nla_data(frame),
1489 nla_len(frame), ack);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001490 break;
1491 case NL80211_CMD_UNPROT_DEAUTHENTICATE:
1492 mlme_event_unprot_disconnect(drv, EVENT_UNPROT_DEAUTH,
1493 nla_data(frame), nla_len(frame));
1494 break;
1495 case NL80211_CMD_UNPROT_DISASSOCIATE:
1496 mlme_event_unprot_disconnect(drv, EVENT_UNPROT_DISASSOC,
1497 nla_data(frame), nla_len(frame));
1498 break;
1499 default:
1500 break;
1501 }
1502}
1503
1504
1505static void mlme_event_michael_mic_failure(struct wpa_driver_nl80211_data *drv,
1506 struct nlattr *tb[])
1507{
1508 union wpa_event_data data;
1509
1510 wpa_printf(MSG_DEBUG, "nl80211: MLME event Michael MIC failure");
1511 os_memset(&data, 0, sizeof(data));
1512 if (tb[NL80211_ATTR_MAC]) {
1513 wpa_hexdump(MSG_DEBUG, "nl80211: Source MAC address",
1514 nla_data(tb[NL80211_ATTR_MAC]),
1515 nla_len(tb[NL80211_ATTR_MAC]));
1516 data.michael_mic_failure.src = nla_data(tb[NL80211_ATTR_MAC]);
1517 }
1518 if (tb[NL80211_ATTR_KEY_SEQ]) {
1519 wpa_hexdump(MSG_DEBUG, "nl80211: TSC",
1520 nla_data(tb[NL80211_ATTR_KEY_SEQ]),
1521 nla_len(tb[NL80211_ATTR_KEY_SEQ]));
1522 }
1523 if (tb[NL80211_ATTR_KEY_TYPE]) {
1524 enum nl80211_key_type key_type =
1525 nla_get_u32(tb[NL80211_ATTR_KEY_TYPE]);
1526 wpa_printf(MSG_DEBUG, "nl80211: Key Type %d", key_type);
1527 if (key_type == NL80211_KEYTYPE_PAIRWISE)
1528 data.michael_mic_failure.unicast = 1;
1529 } else
1530 data.michael_mic_failure.unicast = 1;
1531
1532 if (tb[NL80211_ATTR_KEY_IDX]) {
1533 u8 key_id = nla_get_u8(tb[NL80211_ATTR_KEY_IDX]);
1534 wpa_printf(MSG_DEBUG, "nl80211: Key Id %d", key_id);
1535 }
1536
1537 wpa_supplicant_event(drv->ctx, EVENT_MICHAEL_MIC_FAILURE, &data);
1538}
1539
1540
1541static void mlme_event_join_ibss(struct wpa_driver_nl80211_data *drv,
1542 struct nlattr *tb[])
1543{
1544 if (tb[NL80211_ATTR_MAC] == NULL) {
1545 wpa_printf(MSG_DEBUG, "nl80211: No address in IBSS joined "
1546 "event");
1547 return;
1548 }
1549 os_memcpy(drv->bssid, nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
1550 drv->associated = 1;
1551 wpa_printf(MSG_DEBUG, "nl80211: IBSS " MACSTR " joined",
1552 MAC2STR(drv->bssid));
1553
1554 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, NULL);
1555}
1556
1557
1558static void mlme_event_remain_on_channel(struct wpa_driver_nl80211_data *drv,
1559 int cancel_event, struct nlattr *tb[])
1560{
1561 unsigned int freq, chan_type, duration;
1562 union wpa_event_data data;
1563 u64 cookie;
1564
1565 if (tb[NL80211_ATTR_WIPHY_FREQ])
1566 freq = nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]);
1567 else
1568 freq = 0;
1569
1570 if (tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE])
1571 chan_type = nla_get_u32(tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1572 else
1573 chan_type = 0;
1574
1575 if (tb[NL80211_ATTR_DURATION])
1576 duration = nla_get_u32(tb[NL80211_ATTR_DURATION]);
1577 else
1578 duration = 0;
1579
1580 if (tb[NL80211_ATTR_COOKIE])
1581 cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
1582 else
1583 cookie = 0;
1584
1585 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel event (cancel=%d "
1586 "freq=%u channel_type=%u duration=%u cookie=0x%llx (%s))",
1587 cancel_event, freq, chan_type, duration,
1588 (long long unsigned int) cookie,
1589 cookie == drv->remain_on_chan_cookie ? "match" : "unknown");
1590
1591 if (cookie != drv->remain_on_chan_cookie)
1592 return; /* not for us */
1593
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001594 if (cancel_event)
1595 drv->pending_remain_on_chan = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001596
1597 os_memset(&data, 0, sizeof(data));
1598 data.remain_on_channel.freq = freq;
1599 data.remain_on_channel.duration = duration;
1600 wpa_supplicant_event(drv->ctx, cancel_event ?
1601 EVENT_CANCEL_REMAIN_ON_CHANNEL :
1602 EVENT_REMAIN_ON_CHANNEL, &data);
1603}
1604
1605
1606static void send_scan_event(struct wpa_driver_nl80211_data *drv, int aborted,
1607 struct nlattr *tb[])
1608{
1609 union wpa_event_data event;
1610 struct nlattr *nl;
1611 int rem;
1612 struct scan_info *info;
1613#define MAX_REPORT_FREQS 50
1614 int freqs[MAX_REPORT_FREQS];
1615 int num_freqs = 0;
1616
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001617 if (drv->scan_for_auth) {
1618 drv->scan_for_auth = 0;
1619 wpa_printf(MSG_DEBUG, "nl80211: Scan results for missing "
1620 "cfg80211 BSS entry");
1621 wpa_driver_nl80211_authenticate_retry(drv);
1622 return;
1623 }
1624
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001625 os_memset(&event, 0, sizeof(event));
1626 info = &event.scan_info;
1627 info->aborted = aborted;
1628
1629 if (tb[NL80211_ATTR_SCAN_SSIDS]) {
1630 nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_SSIDS], rem) {
1631 struct wpa_driver_scan_ssid *s =
1632 &info->ssids[info->num_ssids];
1633 s->ssid = nla_data(nl);
1634 s->ssid_len = nla_len(nl);
1635 info->num_ssids++;
1636 if (info->num_ssids == WPAS_MAX_SCAN_SSIDS)
1637 break;
1638 }
1639 }
1640 if (tb[NL80211_ATTR_SCAN_FREQUENCIES]) {
1641 nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_FREQUENCIES], rem)
1642 {
1643 freqs[num_freqs] = nla_get_u32(nl);
1644 num_freqs++;
1645 if (num_freqs == MAX_REPORT_FREQS - 1)
1646 break;
1647 }
1648 info->freqs = freqs;
1649 info->num_freqs = num_freqs;
1650 }
1651 wpa_supplicant_event(drv->ctx, EVENT_SCAN_RESULTS, &event);
1652}
1653
1654
1655static int get_link_signal(struct nl_msg *msg, void *arg)
1656{
1657 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1658 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1659 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
1660 static struct nla_policy policy[NL80211_STA_INFO_MAX + 1] = {
1661 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
1662 };
1663 struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
1664 static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
1665 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
1666 [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
1667 [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
1668 [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
1669 };
1670 struct wpa_signal_info *sig_change = arg;
1671
1672 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1673 genlmsg_attrlen(gnlh, 0), NULL);
1674 if (!tb[NL80211_ATTR_STA_INFO] ||
1675 nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
1676 tb[NL80211_ATTR_STA_INFO], policy))
1677 return NL_SKIP;
1678 if (!sinfo[NL80211_STA_INFO_SIGNAL])
1679 return NL_SKIP;
1680
1681 sig_change->current_signal =
1682 (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
1683
1684 if (sinfo[NL80211_STA_INFO_TX_BITRATE]) {
1685 if (nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
1686 sinfo[NL80211_STA_INFO_TX_BITRATE],
1687 rate_policy)) {
1688 sig_change->current_txrate = 0;
1689 } else {
1690 if (rinfo[NL80211_RATE_INFO_BITRATE]) {
1691 sig_change->current_txrate =
1692 nla_get_u16(rinfo[
1693 NL80211_RATE_INFO_BITRATE]) * 100;
1694 }
1695 }
1696 }
1697
1698 return NL_SKIP;
1699}
1700
1701
1702static int nl80211_get_link_signal(struct wpa_driver_nl80211_data *drv,
1703 struct wpa_signal_info *sig)
1704{
1705 struct nl_msg *msg;
1706
1707 sig->current_signal = -9999;
1708 sig->current_txrate = 0;
1709
1710 msg = nlmsg_alloc();
1711 if (!msg)
1712 return -ENOMEM;
1713
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001714 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001715
1716 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
1717 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid);
1718
1719 return send_and_recv_msgs(drv, msg, get_link_signal, sig);
1720 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001721 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001722 return -ENOBUFS;
1723}
1724
1725
1726static int get_link_noise(struct nl_msg *msg, void *arg)
1727{
1728 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1729 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1730 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
1731 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
1732 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
1733 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
1734 };
1735 struct wpa_signal_info *sig_change = arg;
1736
1737 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1738 genlmsg_attrlen(gnlh, 0), NULL);
1739
1740 if (!tb[NL80211_ATTR_SURVEY_INFO]) {
1741 wpa_printf(MSG_DEBUG, "nl80211: survey data missing!");
1742 return NL_SKIP;
1743 }
1744
1745 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
1746 tb[NL80211_ATTR_SURVEY_INFO],
1747 survey_policy)) {
1748 wpa_printf(MSG_DEBUG, "nl80211: failed to parse nested "
1749 "attributes!");
1750 return NL_SKIP;
1751 }
1752
1753 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
1754 return NL_SKIP;
1755
1756 if (nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
1757 sig_change->frequency)
1758 return NL_SKIP;
1759
1760 if (!sinfo[NL80211_SURVEY_INFO_NOISE])
1761 return NL_SKIP;
1762
1763 sig_change->current_noise =
1764 (s8) nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
1765
1766 return NL_SKIP;
1767}
1768
1769
1770static int nl80211_get_link_noise(struct wpa_driver_nl80211_data *drv,
1771 struct wpa_signal_info *sig_change)
1772{
1773 struct nl_msg *msg;
1774
1775 sig_change->current_noise = 9999;
1776 sig_change->frequency = drv->assoc_freq;
1777
1778 msg = nlmsg_alloc();
1779 if (!msg)
1780 return -ENOMEM;
1781
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001782 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001783
1784 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
1785
1786 return send_and_recv_msgs(drv, msg, get_link_noise, sig_change);
1787 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001788 nlmsg_free(msg);
1789 return -ENOBUFS;
1790}
1791
1792
1793static int get_noise_for_scan_results(struct nl_msg *msg, void *arg)
1794{
1795 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1796 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1797 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
1798 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
1799 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
1800 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
1801 };
1802 struct wpa_scan_results *scan_results = arg;
1803 struct wpa_scan_res *scan_res;
1804 size_t i;
1805
1806 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1807 genlmsg_attrlen(gnlh, 0), NULL);
1808
1809 if (!tb[NL80211_ATTR_SURVEY_INFO]) {
1810 wpa_printf(MSG_DEBUG, "nl80211: Survey data missing");
1811 return NL_SKIP;
1812 }
1813
1814 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
1815 tb[NL80211_ATTR_SURVEY_INFO],
1816 survey_policy)) {
1817 wpa_printf(MSG_DEBUG, "nl80211: Failed to parse nested "
1818 "attributes");
1819 return NL_SKIP;
1820 }
1821
1822 if (!sinfo[NL80211_SURVEY_INFO_NOISE])
1823 return NL_SKIP;
1824
1825 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
1826 return NL_SKIP;
1827
1828 for (i = 0; i < scan_results->num; ++i) {
1829 scan_res = scan_results->res[i];
1830 if (!scan_res)
1831 continue;
1832 if ((int) nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
1833 scan_res->freq)
1834 continue;
1835 if (!(scan_res->flags & WPA_SCAN_NOISE_INVALID))
1836 continue;
1837 scan_res->noise = (s8)
1838 nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
1839 scan_res->flags &= ~WPA_SCAN_NOISE_INVALID;
1840 }
1841
1842 return NL_SKIP;
1843}
1844
1845
1846static int nl80211_get_noise_for_scan_results(
1847 struct wpa_driver_nl80211_data *drv,
1848 struct wpa_scan_results *scan_res)
1849{
1850 struct nl_msg *msg;
1851
1852 msg = nlmsg_alloc();
1853 if (!msg)
1854 return -ENOMEM;
1855
1856 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
1857
1858 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
1859
1860 return send_and_recv_msgs(drv, msg, get_noise_for_scan_results,
1861 scan_res);
1862 nla_put_failure:
1863 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001864 return -ENOBUFS;
1865}
1866
1867
1868static void nl80211_cqm_event(struct wpa_driver_nl80211_data *drv,
1869 struct nlattr *tb[])
1870{
1871 static struct nla_policy cqm_policy[NL80211_ATTR_CQM_MAX + 1] = {
1872 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
1873 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U8 },
1874 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
1875 [NL80211_ATTR_CQM_PKT_LOSS_EVENT] = { .type = NLA_U32 },
1876 };
1877 struct nlattr *cqm[NL80211_ATTR_CQM_MAX + 1];
1878 enum nl80211_cqm_rssi_threshold_event event;
1879 union wpa_event_data ed;
1880 struct wpa_signal_info sig;
1881 int res;
1882
1883 if (tb[NL80211_ATTR_CQM] == NULL ||
1884 nla_parse_nested(cqm, NL80211_ATTR_CQM_MAX, tb[NL80211_ATTR_CQM],
1885 cqm_policy)) {
1886 wpa_printf(MSG_DEBUG, "nl80211: Ignore invalid CQM event");
1887 return;
1888 }
1889
1890 os_memset(&ed, 0, sizeof(ed));
1891
1892 if (cqm[NL80211_ATTR_CQM_PKT_LOSS_EVENT]) {
1893 if (!tb[NL80211_ATTR_MAC])
1894 return;
1895 os_memcpy(ed.low_ack.addr, nla_data(tb[NL80211_ATTR_MAC]),
1896 ETH_ALEN);
1897 wpa_supplicant_event(drv->ctx, EVENT_STATION_LOW_ACK, &ed);
1898 return;
1899 }
1900
1901 if (cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] == NULL)
1902 return;
1903 event = nla_get_u32(cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT]);
1904
1905 if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH) {
1906 wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor "
1907 "event: RSSI high");
1908 ed.signal_change.above_threshold = 1;
1909 } else if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW) {
1910 wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor "
1911 "event: RSSI low");
1912 ed.signal_change.above_threshold = 0;
1913 } else
1914 return;
1915
1916 res = nl80211_get_link_signal(drv, &sig);
1917 if (res == 0) {
1918 ed.signal_change.current_signal = sig.current_signal;
1919 ed.signal_change.current_txrate = sig.current_txrate;
1920 wpa_printf(MSG_DEBUG, "nl80211: Signal: %d dBm txrate: %d",
1921 sig.current_signal, sig.current_txrate);
1922 }
1923
1924 res = nl80211_get_link_noise(drv, &sig);
1925 if (res == 0) {
1926 ed.signal_change.current_noise = sig.current_noise;
1927 wpa_printf(MSG_DEBUG, "nl80211: Noise: %d dBm",
1928 sig.current_noise);
1929 }
1930
1931 wpa_supplicant_event(drv->ctx, EVENT_SIGNAL_CHANGE, &ed);
1932}
1933
1934
1935static void nl80211_new_station_event(struct wpa_driver_nl80211_data *drv,
1936 struct nlattr **tb)
1937{
1938 u8 *addr;
1939 union wpa_event_data data;
1940
1941 if (tb[NL80211_ATTR_MAC] == NULL)
1942 return;
1943 addr = nla_data(tb[NL80211_ATTR_MAC]);
1944 wpa_printf(MSG_DEBUG, "nl80211: New station " MACSTR, MAC2STR(addr));
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07001945
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001946 if (is_ap_interface(drv->nlmode) && drv->device_ap_sme) {
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07001947 u8 *ies = NULL;
1948 size_t ies_len = 0;
1949 if (tb[NL80211_ATTR_IE]) {
1950 ies = nla_data(tb[NL80211_ATTR_IE]);
1951 ies_len = nla_len(tb[NL80211_ATTR_IE]);
1952 }
1953 wpa_hexdump(MSG_DEBUG, "nl80211: Assoc Req IEs", ies, ies_len);
1954 drv_event_assoc(drv->ctx, addr, ies, ies_len, 0);
1955 return;
1956 }
1957
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001958 if (drv->nlmode != NL80211_IFTYPE_ADHOC)
1959 return;
1960
1961 os_memset(&data, 0, sizeof(data));
1962 os_memcpy(data.ibss_rsn_start.peer, addr, ETH_ALEN);
1963 wpa_supplicant_event(drv->ctx, EVENT_IBSS_RSN_START, &data);
1964}
1965
1966
1967static void nl80211_del_station_event(struct wpa_driver_nl80211_data *drv,
1968 struct nlattr **tb)
1969{
1970 u8 *addr;
1971 union wpa_event_data data;
1972
1973 if (tb[NL80211_ATTR_MAC] == NULL)
1974 return;
1975 addr = nla_data(tb[NL80211_ATTR_MAC]);
1976 wpa_printf(MSG_DEBUG, "nl80211: Delete station " MACSTR,
1977 MAC2STR(addr));
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07001978
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001979 if (is_ap_interface(drv->nlmode) && drv->device_ap_sme) {
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07001980 drv_event_disassoc(drv->ctx, addr);
1981 return;
1982 }
1983
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001984 if (drv->nlmode != NL80211_IFTYPE_ADHOC)
1985 return;
1986
1987 os_memset(&data, 0, sizeof(data));
1988 os_memcpy(data.ibss_peer_lost.peer, addr, ETH_ALEN);
1989 wpa_supplicant_event(drv->ctx, EVENT_IBSS_PEER_LOST, &data);
1990}
1991
1992
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001993static void nl80211_rekey_offload_event(struct wpa_driver_nl80211_data *drv,
1994 struct nlattr **tb)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001995{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001996 struct nlattr *rekey_info[NUM_NL80211_REKEY_DATA];
1997 static struct nla_policy rekey_policy[NUM_NL80211_REKEY_DATA] = {
1998 [NL80211_REKEY_DATA_KEK] = {
1999 .minlen = NL80211_KEK_LEN,
2000 .maxlen = NL80211_KEK_LEN,
2001 },
2002 [NL80211_REKEY_DATA_KCK] = {
2003 .minlen = NL80211_KCK_LEN,
2004 .maxlen = NL80211_KCK_LEN,
2005 },
2006 [NL80211_REKEY_DATA_REPLAY_CTR] = {
2007 .minlen = NL80211_REPLAY_CTR_LEN,
2008 .maxlen = NL80211_REPLAY_CTR_LEN,
2009 },
2010 };
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002011 union wpa_event_data data;
2012
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002013 if (!tb[NL80211_ATTR_MAC])
2014 return;
2015 if (!tb[NL80211_ATTR_REKEY_DATA])
2016 return;
2017 if (nla_parse_nested(rekey_info, MAX_NL80211_REKEY_DATA,
2018 tb[NL80211_ATTR_REKEY_DATA], rekey_policy))
2019 return;
2020 if (!rekey_info[NL80211_REKEY_DATA_REPLAY_CTR])
2021 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002022
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002023 os_memset(&data, 0, sizeof(data));
2024 data.driver_gtk_rekey.bssid = nla_data(tb[NL80211_ATTR_MAC]);
2025 wpa_printf(MSG_DEBUG, "nl80211: Rekey offload event for BSSID " MACSTR,
2026 MAC2STR(data.driver_gtk_rekey.bssid));
2027 data.driver_gtk_rekey.replay_ctr =
2028 nla_data(rekey_info[NL80211_REKEY_DATA_REPLAY_CTR]);
2029 wpa_hexdump(MSG_DEBUG, "nl80211: Rekey offload - Replay Counter",
2030 data.driver_gtk_rekey.replay_ctr, NL80211_REPLAY_CTR_LEN);
2031 wpa_supplicant_event(drv->ctx, EVENT_DRIVER_GTK_REKEY, &data);
2032}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002033
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002034
2035static void nl80211_pmksa_candidate_event(struct wpa_driver_nl80211_data *drv,
2036 struct nlattr **tb)
2037{
2038 struct nlattr *cand[NUM_NL80211_PMKSA_CANDIDATE];
2039 static struct nla_policy cand_policy[NUM_NL80211_PMKSA_CANDIDATE] = {
2040 [NL80211_PMKSA_CANDIDATE_INDEX] = { .type = NLA_U32 },
2041 [NL80211_PMKSA_CANDIDATE_BSSID] = {
2042 .minlen = ETH_ALEN,
2043 .maxlen = ETH_ALEN,
2044 },
2045 [NL80211_PMKSA_CANDIDATE_PREAUTH] = { .type = NLA_FLAG },
2046 };
2047 union wpa_event_data data;
2048
2049 if (!tb[NL80211_ATTR_PMKSA_CANDIDATE])
2050 return;
2051 if (nla_parse_nested(cand, MAX_NL80211_PMKSA_CANDIDATE,
2052 tb[NL80211_ATTR_PMKSA_CANDIDATE], cand_policy))
2053 return;
2054 if (!cand[NL80211_PMKSA_CANDIDATE_INDEX] ||
2055 !cand[NL80211_PMKSA_CANDIDATE_BSSID])
2056 return;
2057
2058 os_memset(&data, 0, sizeof(data));
2059 os_memcpy(data.pmkid_candidate.bssid,
2060 nla_data(cand[NL80211_PMKSA_CANDIDATE_BSSID]), ETH_ALEN);
2061 data.pmkid_candidate.index =
2062 nla_get_u32(cand[NL80211_PMKSA_CANDIDATE_INDEX]);
2063 data.pmkid_candidate.preauth =
2064 cand[NL80211_PMKSA_CANDIDATE_PREAUTH] != NULL;
2065 wpa_supplicant_event(drv->ctx, EVENT_PMKID_CANDIDATE, &data);
2066}
2067
2068
2069static void nl80211_client_probe_event(struct wpa_driver_nl80211_data *drv,
2070 struct nlattr **tb)
2071{
2072 union wpa_event_data data;
2073
2074 if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_ACK])
2075 return;
2076
2077 os_memset(&data, 0, sizeof(data));
2078 os_memcpy(data.client_poll.addr,
2079 nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
2080
2081 wpa_supplicant_event(drv->ctx, EVENT_DRIVER_CLIENT_POLL_OK, &data);
2082}
2083
2084
2085static void nl80211_spurious_frame(struct i802_bss *bss, struct nlattr **tb,
2086 int wds)
2087{
2088 struct wpa_driver_nl80211_data *drv = bss->drv;
2089 union wpa_event_data event;
2090
2091 if (!tb[NL80211_ATTR_MAC])
2092 return;
2093
2094 os_memset(&event, 0, sizeof(event));
2095 event.rx_from_unknown.bssid = bss->addr;
2096 event.rx_from_unknown.addr = nla_data(tb[NL80211_ATTR_MAC]);
2097 event.rx_from_unknown.wds = wds;
2098
2099 wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event);
2100}
2101
2102
2103static void do_process_drv_event(struct wpa_driver_nl80211_data *drv,
2104 int cmd, struct nlattr **tb)
2105{
2106 if (drv->ap_scan_as_station != NL80211_IFTYPE_UNSPECIFIED &&
2107 (cmd == NL80211_CMD_NEW_SCAN_RESULTS ||
2108 cmd == NL80211_CMD_SCAN_ABORTED)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002109 wpa_driver_nl80211_set_mode(&drv->first_bss,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002110 drv->ap_scan_as_station);
2111 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002112 }
2113
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002114 switch (cmd) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002115 case NL80211_CMD_TRIGGER_SCAN:
2116 wpa_printf(MSG_DEBUG, "nl80211: Scan trigger");
2117 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002118 case NL80211_CMD_START_SCHED_SCAN:
2119 wpa_printf(MSG_DEBUG, "nl80211: Sched scan started");
2120 break;
2121 case NL80211_CMD_SCHED_SCAN_STOPPED:
2122 wpa_printf(MSG_DEBUG, "nl80211: Sched scan stopped");
2123 wpa_supplicant_event(drv->ctx, EVENT_SCHED_SCAN_STOPPED, NULL);
2124 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002125 case NL80211_CMD_NEW_SCAN_RESULTS:
2126 wpa_printf(MSG_DEBUG, "nl80211: New scan results available");
2127 drv->scan_complete_events = 1;
2128 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
2129 drv->ctx);
2130 send_scan_event(drv, 0, tb);
2131 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002132 case NL80211_CMD_SCHED_SCAN_RESULTS:
2133 wpa_printf(MSG_DEBUG,
2134 "nl80211: New sched scan results available");
2135 send_scan_event(drv, 0, tb);
2136 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002137 case NL80211_CMD_SCAN_ABORTED:
2138 wpa_printf(MSG_DEBUG, "nl80211: Scan aborted");
2139 /*
2140 * Need to indicate that scan results are available in order
2141 * not to make wpa_supplicant stop its scanning.
2142 */
2143 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
2144 drv->ctx);
2145 send_scan_event(drv, 1, tb);
2146 break;
2147 case NL80211_CMD_AUTHENTICATE:
2148 case NL80211_CMD_ASSOCIATE:
2149 case NL80211_CMD_DEAUTHENTICATE:
2150 case NL80211_CMD_DISASSOCIATE:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002151 case NL80211_CMD_FRAME_TX_STATUS:
2152 case NL80211_CMD_UNPROT_DEAUTHENTICATE:
2153 case NL80211_CMD_UNPROT_DISASSOCIATE:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002154 mlme_event(drv, cmd, tb[NL80211_ATTR_FRAME],
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002155 tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT],
2156 tb[NL80211_ATTR_WIPHY_FREQ], tb[NL80211_ATTR_ACK],
Dmitry Shmidt04949592012-07-19 12:16:46 -07002157 tb[NL80211_ATTR_COOKIE],
2158 tb[NL80211_ATTR_RX_SIGNAL_DBM]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002159 break;
2160 case NL80211_CMD_CONNECT:
2161 case NL80211_CMD_ROAM:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002162 mlme_event_connect(drv, cmd,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002163 tb[NL80211_ATTR_STATUS_CODE],
2164 tb[NL80211_ATTR_MAC],
2165 tb[NL80211_ATTR_REQ_IE],
2166 tb[NL80211_ATTR_RESP_IE]);
2167 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002168 case NL80211_CMD_CH_SWITCH_NOTIFY:
2169 mlme_event_ch_switch(drv, tb[NL80211_ATTR_WIPHY_FREQ],
2170 tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
2171 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002172 case NL80211_CMD_DISCONNECT:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002173 mlme_event_disconnect(drv, tb[NL80211_ATTR_REASON_CODE],
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002174 tb[NL80211_ATTR_MAC],
2175 tb[NL80211_ATTR_DISCONNECTED_BY_AP]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002176 break;
2177 case NL80211_CMD_MICHAEL_MIC_FAILURE:
2178 mlme_event_michael_mic_failure(drv, tb);
2179 break;
2180 case NL80211_CMD_JOIN_IBSS:
2181 mlme_event_join_ibss(drv, tb);
2182 break;
2183 case NL80211_CMD_REMAIN_ON_CHANNEL:
2184 mlme_event_remain_on_channel(drv, 0, tb);
2185 break;
2186 case NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL:
2187 mlme_event_remain_on_channel(drv, 1, tb);
2188 break;
2189 case NL80211_CMD_NOTIFY_CQM:
2190 nl80211_cqm_event(drv, tb);
2191 break;
2192 case NL80211_CMD_REG_CHANGE:
2193 wpa_printf(MSG_DEBUG, "nl80211: Regulatory domain change");
2194 wpa_supplicant_event(drv->ctx, EVENT_CHANNEL_LIST_CHANGED,
2195 NULL);
2196 break;
2197 case NL80211_CMD_REG_BEACON_HINT:
2198 wpa_printf(MSG_DEBUG, "nl80211: Regulatory beacon hint");
2199 wpa_supplicant_event(drv->ctx, EVENT_CHANNEL_LIST_CHANGED,
2200 NULL);
2201 break;
2202 case NL80211_CMD_NEW_STATION:
2203 nl80211_new_station_event(drv, tb);
2204 break;
2205 case NL80211_CMD_DEL_STATION:
2206 nl80211_del_station_event(drv, tb);
2207 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002208 case NL80211_CMD_SET_REKEY_OFFLOAD:
2209 nl80211_rekey_offload_event(drv, tb);
2210 break;
2211 case NL80211_CMD_PMKSA_CANDIDATE:
2212 nl80211_pmksa_candidate_event(drv, tb);
2213 break;
2214 case NL80211_CMD_PROBE_CLIENT:
2215 nl80211_client_probe_event(drv, tb);
2216 break;
2217 default:
2218 wpa_printf(MSG_DEBUG, "nl80211: Ignored unknown event "
2219 "(cmd=%d)", cmd);
2220 break;
2221 }
2222}
2223
2224
2225static int process_drv_event(struct nl_msg *msg, void *arg)
2226{
2227 struct wpa_driver_nl80211_data *drv = arg;
2228 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2229 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2230
2231 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2232 genlmsg_attrlen(gnlh, 0), NULL);
2233
2234 if (tb[NL80211_ATTR_IFINDEX]) {
2235 int ifindex = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
2236 if (ifindex != drv->ifindex && !have_ifidx(drv, ifindex)) {
2237 wpa_printf(MSG_DEBUG, "nl80211: Ignored event (cmd=%d)"
2238 " for foreign interface (ifindex %d)",
2239 gnlh->cmd, ifindex);
2240 return NL_SKIP;
2241 }
2242 }
2243
2244 do_process_drv_event(drv, gnlh->cmd, tb);
2245 return NL_SKIP;
2246}
2247
2248
2249static int process_global_event(struct nl_msg *msg, void *arg)
2250{
2251 struct nl80211_global *global = arg;
2252 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2253 struct nlattr *tb[NL80211_ATTR_MAX + 1];
Dmitry Shmidt04949592012-07-19 12:16:46 -07002254 struct wpa_driver_nl80211_data *drv, *tmp;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002255 int ifidx = -1;
2256
2257 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2258 genlmsg_attrlen(gnlh, 0), NULL);
2259
2260 if (tb[NL80211_ATTR_IFINDEX])
2261 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
2262
Dmitry Shmidt04949592012-07-19 12:16:46 -07002263 dl_list_for_each_safe(drv, tmp, &global->interfaces,
2264 struct wpa_driver_nl80211_data, list) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002265 if (ifidx == -1 || ifidx == drv->ifindex ||
Dmitry Shmidt04949592012-07-19 12:16:46 -07002266 have_ifidx(drv, ifidx))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002267 do_process_drv_event(drv, gnlh->cmd, tb);
2268 }
2269
2270 return NL_SKIP;
2271}
2272
2273
2274static int process_bss_event(struct nl_msg *msg, void *arg)
2275{
2276 struct i802_bss *bss = arg;
2277 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2278 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2279
2280 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2281 genlmsg_attrlen(gnlh, 0), NULL);
2282
2283 switch (gnlh->cmd) {
2284 case NL80211_CMD_FRAME:
2285 case NL80211_CMD_FRAME_TX_STATUS:
2286 mlme_event(bss->drv, gnlh->cmd, tb[NL80211_ATTR_FRAME],
2287 tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT],
2288 tb[NL80211_ATTR_WIPHY_FREQ], tb[NL80211_ATTR_ACK],
Dmitry Shmidt04949592012-07-19 12:16:46 -07002289 tb[NL80211_ATTR_COOKIE],
2290 tb[NL80211_ATTR_RX_SIGNAL_DBM]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002291 break;
2292 case NL80211_CMD_UNEXPECTED_FRAME:
2293 nl80211_spurious_frame(bss, tb, 0);
2294 break;
2295 case NL80211_CMD_UNEXPECTED_4ADDR_FRAME:
2296 nl80211_spurious_frame(bss, tb, 1);
2297 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002298 default:
2299 wpa_printf(MSG_DEBUG, "nl80211: Ignored unknown event "
2300 "(cmd=%d)", gnlh->cmd);
2301 break;
2302 }
2303
2304 return NL_SKIP;
2305}
2306
2307
2308static void wpa_driver_nl80211_event_receive(int sock, void *eloop_ctx,
2309 void *handle)
2310{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002311 struct nl_cb *cb = eloop_ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002312
2313 wpa_printf(MSG_DEBUG, "nl80211: Event message available");
2314
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002315 nl_recvmsgs(handle, cb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002316}
2317
2318
2319/**
2320 * wpa_driver_nl80211_set_country - ask nl80211 to set the regulatory domain
2321 * @priv: driver_nl80211 private data
2322 * @alpha2_arg: country to which to switch to
2323 * Returns: 0 on success, -1 on failure
2324 *
2325 * This asks nl80211 to set the regulatory domain for given
2326 * country ISO / IEC alpha2.
2327 */
2328static int wpa_driver_nl80211_set_country(void *priv, const char *alpha2_arg)
2329{
2330 struct i802_bss *bss = priv;
2331 struct wpa_driver_nl80211_data *drv = bss->drv;
2332 char alpha2[3];
2333 struct nl_msg *msg;
2334
2335 msg = nlmsg_alloc();
2336 if (!msg)
2337 return -ENOMEM;
2338
2339 alpha2[0] = alpha2_arg[0];
2340 alpha2[1] = alpha2_arg[1];
2341 alpha2[2] = '\0';
2342
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002343 nl80211_cmd(drv, msg, 0, NL80211_CMD_REQ_SET_REG);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002344
2345 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, alpha2);
2346 if (send_and_recv_msgs(drv, msg, NULL, NULL))
2347 return -EINVAL;
2348 return 0;
2349nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002350 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002351 return -EINVAL;
2352}
2353
2354
2355struct wiphy_info_data {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002356 struct wpa_driver_capa *capa;
2357
2358 unsigned int error:1;
2359 unsigned int device_ap_sme:1;
2360 unsigned int poll_command_supported:1;
2361 unsigned int data_tx_status:1;
2362 unsigned int monitor_supported:1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002363};
2364
2365
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002366static unsigned int probe_resp_offload_support(int supp_protocols)
2367{
2368 unsigned int prot = 0;
2369
2370 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS)
2371 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS;
2372 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2)
2373 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS2;
2374 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P)
2375 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_P2P;
2376 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_80211U)
2377 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_INTERWORKING;
2378
2379 return prot;
2380}
2381
2382
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002383static int wiphy_info_handler(struct nl_msg *msg, void *arg)
2384{
2385 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2386 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2387 struct wiphy_info_data *info = arg;
2388 int p2p_go_supported = 0, p2p_client_supported = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002389 int p2p_concurrent = 0;
2390 int auth_supported = 0, connect_supported = 0;
2391 struct wpa_driver_capa *capa = info->capa;
2392 static struct nla_policy
2393 iface_combination_policy[NUM_NL80211_IFACE_COMB] = {
2394 [NL80211_IFACE_COMB_LIMITS] = { .type = NLA_NESTED },
2395 [NL80211_IFACE_COMB_MAXNUM] = { .type = NLA_U32 },
2396 [NL80211_IFACE_COMB_STA_AP_BI_MATCH] = { .type = NLA_FLAG },
2397 [NL80211_IFACE_COMB_NUM_CHANNELS] = { .type = NLA_U32 },
2398 },
2399 iface_limit_policy[NUM_NL80211_IFACE_LIMIT] = {
2400 [NL80211_IFACE_LIMIT_TYPES] = { .type = NLA_NESTED },
2401 [NL80211_IFACE_LIMIT_MAX] = { .type = NLA_U32 },
2402 };
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002403
2404 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2405 genlmsg_attrlen(gnlh, 0), NULL);
2406
2407 if (tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS])
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002408 capa->max_scan_ssids =
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002409 nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS]);
2410
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002411 if (tb[NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS])
2412 capa->max_sched_scan_ssids =
2413 nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS]);
2414
2415 if (tb[NL80211_ATTR_MAX_MATCH_SETS])
2416 capa->max_match_sets =
2417 nla_get_u8(tb[NL80211_ATTR_MAX_MATCH_SETS]);
2418
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002419 if (tb[NL80211_ATTR_SUPPORTED_IFTYPES]) {
2420 struct nlattr *nl_mode;
2421 int i;
2422 nla_for_each_nested(nl_mode,
2423 tb[NL80211_ATTR_SUPPORTED_IFTYPES], i) {
2424 switch (nla_type(nl_mode)) {
2425 case NL80211_IFTYPE_AP:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002426 capa->flags |= WPA_DRIVER_FLAGS_AP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002427 break;
2428 case NL80211_IFTYPE_P2P_GO:
2429 p2p_go_supported = 1;
2430 break;
2431 case NL80211_IFTYPE_P2P_CLIENT:
2432 p2p_client_supported = 1;
2433 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002434 case NL80211_IFTYPE_MONITOR:
2435 info->monitor_supported = 1;
2436 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002437 }
2438 }
2439 }
2440
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002441 if (tb[NL80211_ATTR_INTERFACE_COMBINATIONS]) {
2442 struct nlattr *nl_combi;
2443 int rem_combi;
2444
2445 nla_for_each_nested(nl_combi,
2446 tb[NL80211_ATTR_INTERFACE_COMBINATIONS],
2447 rem_combi) {
2448 struct nlattr *tb_comb[NUM_NL80211_IFACE_COMB];
2449 struct nlattr *tb_limit[NUM_NL80211_IFACE_LIMIT];
2450 struct nlattr *nl_limit, *nl_mode;
2451 int err, rem_limit, rem_mode;
2452 int combination_has_p2p = 0, combination_has_mgd = 0;
2453
2454 err = nla_parse_nested(tb_comb, MAX_NL80211_IFACE_COMB,
2455 nl_combi,
2456 iface_combination_policy);
2457 if (err || !tb_comb[NL80211_IFACE_COMB_LIMITS] ||
2458 !tb_comb[NL80211_IFACE_COMB_MAXNUM] ||
2459 !tb_comb[NL80211_IFACE_COMB_NUM_CHANNELS])
2460 goto broken_combination;
2461
2462 nla_for_each_nested(nl_limit,
2463 tb_comb[NL80211_IFACE_COMB_LIMITS],
2464 rem_limit) {
2465 err = nla_parse_nested(tb_limit,
2466 MAX_NL80211_IFACE_LIMIT,
2467 nl_limit,
2468 iface_limit_policy);
2469 if (err ||
2470 !tb_limit[NL80211_IFACE_LIMIT_TYPES])
2471 goto broken_combination;
2472
2473 nla_for_each_nested(
2474 nl_mode,
2475 tb_limit[NL80211_IFACE_LIMIT_TYPES],
2476 rem_mode) {
2477 int ift = nla_type(nl_mode);
2478 if (ift == NL80211_IFTYPE_P2P_GO ||
2479 ift == NL80211_IFTYPE_P2P_CLIENT)
2480 combination_has_p2p = 1;
2481 if (ift == NL80211_IFTYPE_STATION)
2482 combination_has_mgd = 1;
2483 }
2484 if (combination_has_p2p && combination_has_mgd)
2485 break;
2486 }
2487
2488 if (combination_has_p2p && combination_has_mgd) {
2489 p2p_concurrent = 1;
2490 break;
2491 }
2492
2493broken_combination:
2494 ;
2495 }
2496 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002497
2498 if (tb[NL80211_ATTR_SUPPORTED_COMMANDS]) {
2499 struct nlattr *nl_cmd;
2500 int i;
2501
2502 nla_for_each_nested(nl_cmd,
2503 tb[NL80211_ATTR_SUPPORTED_COMMANDS], i) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002504 switch (nla_get_u32(nl_cmd)) {
2505 case NL80211_CMD_AUTHENTICATE:
2506 auth_supported = 1;
2507 break;
2508 case NL80211_CMD_CONNECT:
2509 connect_supported = 1;
2510 break;
2511 case NL80211_CMD_START_SCHED_SCAN:
2512 capa->sched_scan_supported = 1;
2513 break;
2514 case NL80211_CMD_PROBE_CLIENT:
2515 info->poll_command_supported = 1;
2516 break;
2517 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002518 }
2519 }
2520
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002521 if (tb[NL80211_ATTR_OFFCHANNEL_TX_OK]) {
2522 wpa_printf(MSG_DEBUG, "nl80211: Using driver-based "
2523 "off-channel TX");
2524 capa->flags |= WPA_DRIVER_FLAGS_OFFCHANNEL_TX;
2525 }
2526
2527 if (tb[NL80211_ATTR_ROAM_SUPPORT]) {
2528 wpa_printf(MSG_DEBUG, "nl80211: Using driver-based roaming");
2529 capa->flags |= WPA_DRIVER_FLAGS_BSS_SELECTION;
2530 }
2531
2532 /* default to 5000 since early versions of mac80211 don't set it */
2533 capa->max_remain_on_chan = 5000;
2534
2535 if (tb[NL80211_ATTR_SUPPORT_AP_UAPSD])
2536 capa->flags |= WPA_DRIVER_FLAGS_AP_UAPSD;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002537
2538 if (tb[NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION])
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002539 capa->max_remain_on_chan =
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002540 nla_get_u32(tb[NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION]);
2541
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002542 if (auth_supported)
2543 capa->flags |= WPA_DRIVER_FLAGS_SME;
2544 else if (!connect_supported) {
2545 wpa_printf(MSG_INFO, "nl80211: Driver does not support "
2546 "authentication/association or connect commands");
2547 info->error = 1;
2548 }
2549
2550 if (p2p_go_supported && p2p_client_supported)
2551 capa->flags |= WPA_DRIVER_FLAGS_P2P_CAPABLE;
2552 if (p2p_concurrent) {
2553 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
2554 "interface (driver advertised support)");
2555 capa->flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
2556 capa->flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
2557 }
2558
2559 if (tb[NL80211_ATTR_TDLS_SUPPORT]) {
2560 wpa_printf(MSG_DEBUG, "nl80211: TDLS supported");
2561 capa->flags |= WPA_DRIVER_FLAGS_TDLS_SUPPORT;
2562
2563 if (tb[NL80211_ATTR_TDLS_EXTERNAL_SETUP]) {
2564 wpa_printf(MSG_DEBUG, "nl80211: TDLS external setup");
2565 capa->flags |=
2566 WPA_DRIVER_FLAGS_TDLS_EXTERNAL_SETUP;
2567 }
2568 }
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07002569
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002570 if (tb[NL80211_ATTR_DEVICE_AP_SME])
2571 info->device_ap_sme = 1;
2572
2573 if (tb[NL80211_ATTR_FEATURE_FLAGS]) {
2574 u32 flags = nla_get_u32(tb[NL80211_ATTR_FEATURE_FLAGS]);
2575
2576 if (flags & NL80211_FEATURE_SK_TX_STATUS)
2577 info->data_tx_status = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002578
2579 if (flags & NL80211_FEATURE_INACTIVITY_TIMER)
2580 capa->flags |= WPA_DRIVER_FLAGS_INACTIVITY_TIMER;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002581 }
2582
2583 if (tb[NL80211_ATTR_PROBE_RESP_OFFLOAD]) {
2584 int protocols =
2585 nla_get_u32(tb[NL80211_ATTR_PROBE_RESP_OFFLOAD]);
2586 wpa_printf(MSG_DEBUG, "nl80211: Supports Probe Response "
2587 "offload in AP mode");
2588 capa->flags |= WPA_DRIVER_FLAGS_PROBE_RESP_OFFLOAD;
2589 capa->probe_resp_offloads =
2590 probe_resp_offload_support(protocols);
2591 }
2592
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002593 return NL_SKIP;
2594}
2595
2596
2597static int wpa_driver_nl80211_get_info(struct wpa_driver_nl80211_data *drv,
2598 struct wiphy_info_data *info)
2599{
2600 struct nl_msg *msg;
2601
2602 os_memset(info, 0, sizeof(*info));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002603 info->capa = &drv->capa;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002604
2605 msg = nlmsg_alloc();
2606 if (!msg)
2607 return -1;
2608
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002609 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002610
2611 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->first_bss.ifindex);
2612
2613 if (send_and_recv_msgs(drv, msg, wiphy_info_handler, info) == 0)
2614 return 0;
2615 msg = NULL;
2616nla_put_failure:
2617 nlmsg_free(msg);
2618 return -1;
2619}
2620
2621
2622static int wpa_driver_nl80211_capa(struct wpa_driver_nl80211_data *drv)
2623{
2624 struct wiphy_info_data info;
2625 if (wpa_driver_nl80211_get_info(drv, &info))
2626 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002627
2628 if (info.error)
2629 return -1;
2630
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002631 drv->has_capability = 1;
2632 /* For now, assume TKIP, CCMP, WPA, WPA2 are supported */
2633 drv->capa.key_mgmt = WPA_DRIVER_CAPA_KEY_MGMT_WPA |
2634 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
2635 WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
2636 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK;
2637 drv->capa.enc = WPA_DRIVER_CAPA_ENC_WEP40 |
2638 WPA_DRIVER_CAPA_ENC_WEP104 |
2639 WPA_DRIVER_CAPA_ENC_TKIP |
2640 WPA_DRIVER_CAPA_ENC_CCMP;
2641 drv->capa.auth = WPA_DRIVER_AUTH_OPEN |
2642 WPA_DRIVER_AUTH_SHARED |
2643 WPA_DRIVER_AUTH_LEAP;
2644
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002645 drv->capa.flags |= WPA_DRIVER_FLAGS_SANE_ERROR_CODES;
2646 drv->capa.flags |= WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002647 drv->capa.flags |= WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07002648
Dmitry Shmidt04949592012-07-19 12:16:46 -07002649 if (!info.device_ap_sme)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002650 drv->capa.flags |= WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002651
2652 drv->device_ap_sme = info.device_ap_sme;
2653 drv->poll_command_supported = info.poll_command_supported;
2654 drv->data_tx_status = info.data_tx_status;
2655
Dmitry Shmidt04949592012-07-19 12:16:46 -07002656#ifdef ANDROID_P2P
2657 if(drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) {
2658 /* Driver is new enough to support monitorless mode*/
2659 wpa_printf(MSG_DEBUG, "nl80211: Driver is new "
2660 "enough to support monitor-less mode");
2661 drv->use_monitor = 0;
2662 }
2663#else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002664 /*
2665 * If poll command is supported mac80211 is new enough to
2666 * have everything we need to not need monitor interfaces.
2667 */
2668 drv->use_monitor = !info.poll_command_supported;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002669#endif
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002670
2671 if (drv->device_ap_sme && drv->use_monitor) {
2672 /*
2673 * Non-mac80211 drivers may not support monitor interface.
2674 * Make sure we do not get stuck with incorrect capability here
2675 * by explicitly testing this.
2676 */
2677 if (!info.monitor_supported) {
2678 wpa_printf(MSG_DEBUG, "nl80211: Disable use_monitor "
2679 "with device_ap_sme since no monitor mode "
2680 "support detected");
2681 drv->use_monitor = 0;
2682 }
2683 }
2684
2685 /*
2686 * If we aren't going to use monitor interfaces, but the
2687 * driver doesn't support data TX status, we won't get TX
2688 * status for EAPOL frames.
2689 */
2690 if (!drv->use_monitor && !info.data_tx_status)
2691 drv->capa.flags &= ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002692
2693 return 0;
2694}
2695
2696
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002697#ifdef ANDROID
2698static int android_genl_ctrl_resolve(struct nl_handle *handle,
2699 const char *name)
2700{
2701 /*
2702 * Android ICS has very minimal genl_ctrl_resolve() implementation, so
2703 * need to work around that.
2704 */
2705 struct nl_cache *cache = NULL;
2706 struct genl_family *nl80211 = NULL;
2707 int id = -1;
2708
2709 if (genl_ctrl_alloc_cache(handle, &cache) < 0) {
2710 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic "
2711 "netlink cache");
2712 goto fail;
2713 }
2714
2715 nl80211 = genl_ctrl_search_by_name(cache, name);
2716 if (nl80211 == NULL)
2717 goto fail;
2718
2719 id = genl_family_get_id(nl80211);
2720
2721fail:
2722 if (nl80211)
2723 genl_family_put(nl80211);
2724 if (cache)
2725 nl_cache_free(cache);
2726
2727 return id;
2728}
2729#define genl_ctrl_resolve android_genl_ctrl_resolve
2730#endif /* ANDROID */
2731
2732
2733static int wpa_driver_nl80211_init_nl_global(struct nl80211_global *global)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002734{
2735 int ret;
2736
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002737 global->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
2738 if (global->nl_cb == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002739 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
2740 "callbacks");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002741 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002742 }
2743
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002744 global->nl = nl_create_handle(global->nl_cb, "nl");
2745 if (global->nl == NULL)
2746 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002747
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002748 global->nl80211_id = genl_ctrl_resolve(global->nl, "nl80211");
2749 if (global->nl80211_id < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002750 wpa_printf(MSG_ERROR, "nl80211: 'nl80211' generic netlink not "
2751 "found");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002752 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002753 }
2754
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002755 global->nl_event = nl_create_handle(global->nl_cb, "event");
2756 if (global->nl_event == NULL)
2757 goto err;
2758
2759 ret = nl_get_multicast_id(global, "nl80211", "scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002760 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002761 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002762 if (ret < 0) {
2763 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
2764 "membership for scan events: %d (%s)",
2765 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002766 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002767 }
2768
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002769 ret = nl_get_multicast_id(global, "nl80211", "mlme");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002770 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002771 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002772 if (ret < 0) {
2773 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
2774 "membership for mlme events: %d (%s)",
2775 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002776 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002777 }
2778
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002779 ret = nl_get_multicast_id(global, "nl80211", "regulatory");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002780 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002781 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002782 if (ret < 0) {
2783 wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
2784 "membership for regulatory events: %d (%s)",
2785 ret, strerror(-ret));
2786 /* Continue without regulatory events */
2787 }
2788
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002789 nl_cb_set(global->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
2790 no_seq_check, NULL);
2791 nl_cb_set(global->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
2792 process_global_event, global);
2793
2794 eloop_register_read_sock(nl_socket_get_fd(global->nl_event),
2795 wpa_driver_nl80211_event_receive,
2796 global->nl_cb, global->nl_event);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002797
2798 return 0;
2799
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002800err:
2801 nl_destroy_handles(&global->nl_event);
2802 nl_destroy_handles(&global->nl);
2803 nl_cb_put(global->nl_cb);
2804 global->nl_cb = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002805 return -1;
2806}
2807
2808
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002809static int wpa_driver_nl80211_init_nl(struct wpa_driver_nl80211_data *drv)
2810{
2811 drv->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
2812 if (!drv->nl_cb) {
2813 wpa_printf(MSG_ERROR, "nl80211: Failed to alloc cb struct");
2814 return -1;
2815 }
2816
2817 nl_cb_set(drv->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
2818 no_seq_check, NULL);
2819 nl_cb_set(drv->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
2820 process_drv_event, drv);
2821
2822 return 0;
2823}
2824
2825
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002826static void wpa_driver_nl80211_rfkill_blocked(void *ctx)
2827{
2828 wpa_printf(MSG_DEBUG, "nl80211: RFKILL blocked");
2829 /*
2830 * This may be for any interface; use ifdown event to disable
2831 * interface.
2832 */
2833}
2834
2835
2836static void wpa_driver_nl80211_rfkill_unblocked(void *ctx)
2837{
2838 struct wpa_driver_nl80211_data *drv = ctx;
2839 wpa_printf(MSG_DEBUG, "nl80211: RFKILL unblocked");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002840 if (linux_set_iface_flags(drv->global->ioctl_sock,
2841 drv->first_bss.ifname, 1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002842 wpa_printf(MSG_DEBUG, "nl80211: Could not set interface UP "
2843 "after rfkill unblock");
2844 return;
2845 }
2846 /* rtnetlink ifup handler will report interface as enabled */
2847}
2848
2849
2850static void nl80211_get_phy_name(struct wpa_driver_nl80211_data *drv)
2851{
2852 /* Find phy (radio) to which this interface belongs */
2853 char buf[90], *pos;
2854 int f, rv;
2855
2856 drv->phyname[0] = '\0';
2857 snprintf(buf, sizeof(buf) - 1, "/sys/class/net/%s/phy80211/name",
2858 drv->first_bss.ifname);
2859 f = open(buf, O_RDONLY);
2860 if (f < 0) {
2861 wpa_printf(MSG_DEBUG, "Could not open file %s: %s",
2862 buf, strerror(errno));
2863 return;
2864 }
2865
2866 rv = read(f, drv->phyname, sizeof(drv->phyname) - 1);
2867 close(f);
2868 if (rv < 0) {
2869 wpa_printf(MSG_DEBUG, "Could not read file %s: %s",
2870 buf, strerror(errno));
2871 return;
2872 }
2873
2874 drv->phyname[rv] = '\0';
2875 pos = os_strchr(drv->phyname, '\n');
2876 if (pos)
2877 *pos = '\0';
2878 wpa_printf(MSG_DEBUG, "nl80211: interface %s in phy %s",
2879 drv->first_bss.ifname, drv->phyname);
2880}
2881
2882
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002883static void wpa_driver_nl80211_handle_eapol_tx_status(int sock,
2884 void *eloop_ctx,
2885 void *handle)
2886{
2887 struct wpa_driver_nl80211_data *drv = eloop_ctx;
2888 u8 data[2048];
2889 struct msghdr msg;
2890 struct iovec entry;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002891 u8 control[512];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002892 struct cmsghdr *cmsg;
2893 int res, found_ee = 0, found_wifi = 0, acked = 0;
2894 union wpa_event_data event;
2895
2896 memset(&msg, 0, sizeof(msg));
2897 msg.msg_iov = &entry;
2898 msg.msg_iovlen = 1;
2899 entry.iov_base = data;
2900 entry.iov_len = sizeof(data);
2901 msg.msg_control = &control;
2902 msg.msg_controllen = sizeof(control);
2903
2904 res = recvmsg(sock, &msg, MSG_ERRQUEUE);
2905 /* if error or not fitting 802.3 header, return */
2906 if (res < 14)
2907 return;
2908
2909 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg))
2910 {
2911 if (cmsg->cmsg_level == SOL_SOCKET &&
2912 cmsg->cmsg_type == SCM_WIFI_STATUS) {
2913 int *ack;
2914
2915 found_wifi = 1;
2916 ack = (void *)CMSG_DATA(cmsg);
2917 acked = *ack;
2918 }
2919
2920 if (cmsg->cmsg_level == SOL_PACKET &&
2921 cmsg->cmsg_type == PACKET_TX_TIMESTAMP) {
2922 struct sock_extended_err *err =
2923 (struct sock_extended_err *)CMSG_DATA(cmsg);
2924
2925 if (err->ee_origin == SO_EE_ORIGIN_TXSTATUS)
2926 found_ee = 1;
2927 }
2928 }
2929
2930 if (!found_ee || !found_wifi)
2931 return;
2932
2933 memset(&event, 0, sizeof(event));
2934 event.eapol_tx_status.dst = data;
2935 event.eapol_tx_status.data = data + 14;
2936 event.eapol_tx_status.data_len = res - 14;
2937 event.eapol_tx_status.ack = acked;
2938 wpa_supplicant_event(drv->ctx, EVENT_EAPOL_TX_STATUS, &event);
2939}
2940
2941
2942static int nl80211_init_bss(struct i802_bss *bss)
2943{
2944 bss->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
2945 if (!bss->nl_cb)
2946 return -1;
2947
2948 nl_cb_set(bss->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
2949 no_seq_check, NULL);
2950 nl_cb_set(bss->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
2951 process_bss_event, bss);
2952
2953 return 0;
2954}
2955
2956
2957static void nl80211_destroy_bss(struct i802_bss *bss)
2958{
2959 nl_cb_put(bss->nl_cb);
2960 bss->nl_cb = NULL;
2961}
2962
2963
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002964/**
2965 * wpa_driver_nl80211_init - Initialize nl80211 driver interface
2966 * @ctx: context to be used when calling wpa_supplicant functions,
2967 * e.g., wpa_supplicant_event()
2968 * @ifname: interface name, e.g., wlan0
2969 * @global_priv: private driver global data from global_init()
2970 * Returns: Pointer to private data, %NULL on failure
2971 */
2972static void * wpa_driver_nl80211_init(void *ctx, const char *ifname,
2973 void *global_priv)
2974{
2975 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002976 struct rfkill_config *rcfg;
2977 struct i802_bss *bss;
2978
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002979 if (global_priv == NULL)
2980 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002981 drv = os_zalloc(sizeof(*drv));
2982 if (drv == NULL)
2983 return NULL;
2984 drv->global = global_priv;
2985 drv->ctx = ctx;
2986 bss = &drv->first_bss;
2987 bss->drv = drv;
2988 os_strlcpy(bss->ifname, ifname, sizeof(bss->ifname));
2989 drv->monitor_ifidx = -1;
2990 drv->monitor_sock = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002991 drv->eapol_tx_sock = -1;
2992 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002993
2994 if (wpa_driver_nl80211_init_nl(drv)) {
2995 os_free(drv);
2996 return NULL;
2997 }
2998
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002999 if (nl80211_init_bss(bss))
3000 goto failed;
3001
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003002 nl80211_get_phy_name(drv);
3003
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003004 rcfg = os_zalloc(sizeof(*rcfg));
3005 if (rcfg == NULL)
3006 goto failed;
3007 rcfg->ctx = drv;
3008 os_strlcpy(rcfg->ifname, ifname, sizeof(rcfg->ifname));
3009 rcfg->blocked_cb = wpa_driver_nl80211_rfkill_blocked;
3010 rcfg->unblocked_cb = wpa_driver_nl80211_rfkill_unblocked;
3011 drv->rfkill = rfkill_init(rcfg);
3012 if (drv->rfkill == NULL) {
3013 wpa_printf(MSG_DEBUG, "nl80211: RFKILL status not available");
3014 os_free(rcfg);
3015 }
3016
3017 if (wpa_driver_nl80211_finish_drv_init(drv))
3018 goto failed;
3019
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003020 drv->eapol_tx_sock = socket(PF_PACKET, SOCK_DGRAM, 0);
3021 if (drv->eapol_tx_sock < 0)
3022 goto failed;
3023
3024 if (drv->data_tx_status) {
3025 int enabled = 1;
3026
3027 if (setsockopt(drv->eapol_tx_sock, SOL_SOCKET, SO_WIFI_STATUS,
3028 &enabled, sizeof(enabled)) < 0) {
3029 wpa_printf(MSG_DEBUG,
3030 "nl80211: wifi status sockopt failed\n");
3031 drv->data_tx_status = 0;
3032 if (!drv->use_monitor)
3033 drv->capa.flags &=
3034 ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
3035 } else {
3036 eloop_register_read_sock(drv->eapol_tx_sock,
3037 wpa_driver_nl80211_handle_eapol_tx_status,
3038 drv, NULL);
3039 }
3040 }
3041
3042 if (drv->global) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003043 dl_list_add(&drv->global->interfaces, &drv->list);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003044 drv->in_interface_list = 1;
3045 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003046
3047 return bss;
3048
3049failed:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003050 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003051 return NULL;
3052}
3053
3054
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003055static int nl80211_register_frame(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003056 struct nl_handle *nl_handle,
3057 u16 type, const u8 *match, size_t match_len)
3058{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003059 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003060 struct nl_msg *msg;
3061 int ret = -1;
3062
3063 msg = nlmsg_alloc();
3064 if (!msg)
3065 return -1;
3066
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003067 wpa_printf(MSG_DEBUG, "nl80211: Register frame type=0x%x nl_handle=%p",
3068 type, nl_handle);
3069 wpa_hexdump(MSG_DEBUG, "nl80211: Register frame match",
3070 match, match_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003071
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003072 nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_ACTION);
3073
3074 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003075 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE, type);
3076 NLA_PUT(msg, NL80211_ATTR_FRAME_MATCH, match_len, match);
3077
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003078 ret = send_and_recv(drv->global, nl_handle, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003079 msg = NULL;
3080 if (ret) {
3081 wpa_printf(MSG_DEBUG, "nl80211: Register frame command "
3082 "failed (type=%u): ret=%d (%s)",
3083 type, ret, strerror(-ret));
3084 wpa_hexdump(MSG_DEBUG, "nl80211: Register frame match",
3085 match, match_len);
3086 goto nla_put_failure;
3087 }
3088 ret = 0;
3089nla_put_failure:
3090 nlmsg_free(msg);
3091 return ret;
3092}
3093
3094
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003095static int nl80211_alloc_mgmt_handle(struct i802_bss *bss)
3096{
3097 struct wpa_driver_nl80211_data *drv = bss->drv;
3098
3099 if (bss->nl_mgmt) {
3100 wpa_printf(MSG_DEBUG, "nl80211: Mgmt reporting "
3101 "already on! (nl_mgmt=%p)", bss->nl_mgmt);
3102 return -1;
3103 }
3104
3105 bss->nl_mgmt = nl_create_handle(drv->nl_cb, "mgmt");
3106 if (bss->nl_mgmt == NULL)
3107 return -1;
3108
3109 eloop_register_read_sock(nl_socket_get_fd(bss->nl_mgmt),
3110 wpa_driver_nl80211_event_receive, bss->nl_cb,
3111 bss->nl_mgmt);
3112
3113 return 0;
3114}
3115
3116
3117static int nl80211_register_action_frame(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003118 const u8 *match, size_t match_len)
3119{
3120 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_ACTION << 4);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003121 return nl80211_register_frame(bss, bss->nl_mgmt,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003122 type, match, match_len);
3123}
3124
3125
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003126static int nl80211_mgmt_subscribe_non_ap(struct i802_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003127{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003128 struct wpa_driver_nl80211_data *drv = bss->drv;
3129
3130 if (nl80211_alloc_mgmt_handle(bss))
3131 return -1;
3132 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with non-AP "
3133 "handle %p", bss->nl_mgmt);
3134
3135#if defined(CONFIG_P2P) || defined(CONFIG_INTERWORKING)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003136 /* GAS Initial Request */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003137 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0a", 2) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003138 return -1;
3139 /* GAS Initial Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003140 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0b", 2) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003141 return -1;
3142 /* GAS Comeback Request */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003143 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0c", 2) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003144 return -1;
3145 /* GAS Comeback Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003146 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0d", 2) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003147 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003148#endif /* CONFIG_P2P || CONFIG_INTERWORKING */
3149#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003150 /* P2P Public Action */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003151 if (nl80211_register_action_frame(bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003152 (u8 *) "\x04\x09\x50\x6f\x9a\x09",
3153 6) < 0)
3154 return -1;
3155 /* P2P Action */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003156 if (nl80211_register_action_frame(bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003157 (u8 *) "\x7f\x50\x6f\x9a\x09",
3158 5) < 0)
3159 return -1;
3160#endif /* CONFIG_P2P */
3161#ifdef CONFIG_IEEE80211W
3162 /* SA Query Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003163 if (nl80211_register_action_frame(bss, (u8 *) "\x08\x01", 2) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003164 return -1;
3165#endif /* CONFIG_IEEE80211W */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003166#ifdef CONFIG_TDLS
3167 if ((drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT)) {
3168 /* TDLS Discovery Response */
3169 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0e", 2) <
3170 0)
3171 return -1;
3172 }
3173#endif /* CONFIG_TDLS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003174
3175 /* FT Action frames */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003176 if (nl80211_register_action_frame(bss, (u8 *) "\x06", 1) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003177 return -1;
3178 else
3179 drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_FT |
3180 WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK;
3181
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003182 /* WNM - BSS Transition Management Request */
3183 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x07", 2) < 0)
3184 return -1;
3185
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003186 return 0;
3187}
3188
3189
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003190static int nl80211_register_spurious_class3(struct i802_bss *bss)
3191{
3192 struct wpa_driver_nl80211_data *drv = bss->drv;
3193 struct nl_msg *msg;
3194 int ret = -1;
3195
3196 msg = nlmsg_alloc();
3197 if (!msg)
3198 return -1;
3199
3200 nl80211_cmd(drv, msg, 0, NL80211_CMD_UNEXPECTED_FRAME);
3201
3202 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
3203
3204 ret = send_and_recv(drv->global, bss->nl_mgmt, msg, NULL, NULL);
3205 msg = NULL;
3206 if (ret) {
3207 wpa_printf(MSG_DEBUG, "nl80211: Register spurious class3 "
3208 "failed: ret=%d (%s)",
3209 ret, strerror(-ret));
3210 goto nla_put_failure;
3211 }
3212 ret = 0;
3213nla_put_failure:
3214 nlmsg_free(msg);
3215 return ret;
3216}
3217
3218
3219static int nl80211_mgmt_subscribe_ap(struct i802_bss *bss)
3220{
3221 static const int stypes[] = {
3222 WLAN_FC_STYPE_AUTH,
3223 WLAN_FC_STYPE_ASSOC_REQ,
3224 WLAN_FC_STYPE_REASSOC_REQ,
3225 WLAN_FC_STYPE_DISASSOC,
3226 WLAN_FC_STYPE_DEAUTH,
3227 WLAN_FC_STYPE_ACTION,
3228 WLAN_FC_STYPE_PROBE_REQ,
3229/* Beacon doesn't work as mac80211 doesn't currently allow
3230 * it, but it wouldn't really be the right thing anyway as
3231 * it isn't per interface ... maybe just dump the scan
3232 * results periodically for OLBC?
3233 */
3234// WLAN_FC_STYPE_BEACON,
3235 };
3236 unsigned int i;
3237
3238 if (nl80211_alloc_mgmt_handle(bss))
3239 return -1;
3240 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
3241 "handle %p", bss->nl_mgmt);
3242
3243 for (i = 0; i < sizeof(stypes) / sizeof(stypes[0]); i++) {
3244 if (nl80211_register_frame(bss, bss->nl_mgmt,
3245 (WLAN_FC_TYPE_MGMT << 2) |
3246 (stypes[i] << 4),
3247 NULL, 0) < 0) {
3248 goto out_err;
3249 }
3250 }
3251
3252 if (nl80211_register_spurious_class3(bss))
3253 goto out_err;
3254
3255 if (nl80211_get_wiphy_data_ap(bss) == NULL)
3256 goto out_err;
3257
3258 return 0;
3259
3260out_err:
3261 eloop_unregister_read_sock(nl_socket_get_fd(bss->nl_mgmt));
3262 nl_destroy_handles(&bss->nl_mgmt);
3263 return -1;
3264}
3265
3266
3267static int nl80211_mgmt_subscribe_ap_dev_sme(struct i802_bss *bss)
3268{
3269 if (nl80211_alloc_mgmt_handle(bss))
3270 return -1;
3271 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
3272 "handle %p (device SME)", bss->nl_mgmt);
3273
3274 if (nl80211_register_frame(bss, bss->nl_mgmt,
3275 (WLAN_FC_TYPE_MGMT << 2) |
3276 (WLAN_FC_STYPE_ACTION << 4),
3277 NULL, 0) < 0)
3278 goto out_err;
3279
3280 return 0;
3281
3282out_err:
3283 eloop_unregister_read_sock(nl_socket_get_fd(bss->nl_mgmt));
3284 nl_destroy_handles(&bss->nl_mgmt);
3285 return -1;
3286}
3287
3288
3289static void nl80211_mgmt_unsubscribe(struct i802_bss *bss, const char *reason)
3290{
3291 if (bss->nl_mgmt == NULL)
3292 return;
3293 wpa_printf(MSG_DEBUG, "nl80211: Unsubscribe mgmt frames handle %p "
3294 "(%s)", bss->nl_mgmt, reason);
3295 eloop_unregister_read_sock(nl_socket_get_fd(bss->nl_mgmt));
3296 nl_destroy_handles(&bss->nl_mgmt);
3297
3298 nl80211_put_wiphy_data_ap(bss);
3299}
3300
3301
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003302static void wpa_driver_nl80211_send_rfkill(void *eloop_ctx, void *timeout_ctx)
3303{
3304 wpa_supplicant_event(timeout_ctx, EVENT_INTERFACE_DISABLED, NULL);
3305}
3306
3307
3308static int
3309wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv)
3310{
3311 struct i802_bss *bss = &drv->first_bss;
3312 int send_rfkill_event = 0;
3313
3314 drv->ifindex = if_nametoindex(bss->ifname);
3315 drv->first_bss.ifindex = drv->ifindex;
3316
3317#ifndef HOSTAPD
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003318 /*
3319 * Make sure the interface starts up in station mode unless this is a
3320 * dynamically added interface (e.g., P2P) that was already configured
3321 * with proper iftype.
3322 */
3323 if (drv->ifindex != drv->global->if_add_ifindex &&
3324 wpa_driver_nl80211_set_mode(bss, NL80211_IFTYPE_STATION) < 0) {
3325 wpa_printf(MSG_ERROR, "nl80211: Could not configure driver to "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003326 "use managed mode");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003327 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003328 }
3329
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003330 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003331 if (rfkill_is_blocked(drv->rfkill)) {
3332 wpa_printf(MSG_DEBUG, "nl80211: Could not yet enable "
3333 "interface '%s' due to rfkill",
3334 bss->ifname);
3335 drv->if_disabled = 1;
3336 send_rfkill_event = 1;
3337 } else {
3338 wpa_printf(MSG_ERROR, "nl80211: Could not set "
3339 "interface '%s' UP", bss->ifname);
3340 return -1;
3341 }
3342 }
3343
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003344 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003345 1, IF_OPER_DORMANT);
3346#endif /* HOSTAPD */
3347
3348 if (wpa_driver_nl80211_capa(drv))
3349 return -1;
3350
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003351 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
3352 bss->addr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003353 return -1;
3354
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003355 if (send_rfkill_event) {
3356 eloop_register_timeout(0, 0, wpa_driver_nl80211_send_rfkill,
3357 drv, drv->ctx);
3358 }
3359
3360 return 0;
3361}
3362
3363
3364static int wpa_driver_nl80211_del_beacon(struct wpa_driver_nl80211_data *drv)
3365{
3366 struct nl_msg *msg;
3367
3368 msg = nlmsg_alloc();
3369 if (!msg)
3370 return -ENOMEM;
3371
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003372 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_BEACON);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003373 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
3374
3375 return send_and_recv_msgs(drv, msg, NULL, NULL);
3376 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003377 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003378 return -ENOBUFS;
3379}
3380
3381
3382/**
3383 * wpa_driver_nl80211_deinit - Deinitialize nl80211 driver interface
3384 * @priv: Pointer to private nl80211 data from wpa_driver_nl80211_init()
3385 *
3386 * Shut down driver interface and processing of driver events. Free
3387 * private data buffer if one was allocated in wpa_driver_nl80211_init().
3388 */
3389static void wpa_driver_nl80211_deinit(void *priv)
3390{
3391 struct i802_bss *bss = priv;
3392 struct wpa_driver_nl80211_data *drv = bss->drv;
3393
Dmitry Shmidt04949592012-07-19 12:16:46 -07003394 bss->in_deinit = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003395 if (drv->data_tx_status)
3396 eloop_unregister_read_sock(drv->eapol_tx_sock);
3397 if (drv->eapol_tx_sock >= 0)
3398 close(drv->eapol_tx_sock);
3399
3400 if (bss->nl_preq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003401 wpa_driver_nl80211_probe_req_report(bss, 0);
3402 if (bss->added_if_into_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003403 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
3404 bss->ifname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003405 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
3406 "interface %s from bridge %s: %s",
3407 bss->ifname, bss->brname, strerror(errno));
3408 }
3409 if (bss->added_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003410 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003411 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
3412 "bridge %s: %s",
3413 bss->brname, strerror(errno));
3414 }
3415
3416 nl80211_remove_monitor_interface(drv);
3417
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003418 if (is_ap_interface(drv->nlmode))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003419 wpa_driver_nl80211_del_beacon(drv);
3420
3421#ifdef HOSTAPD
3422 if (drv->last_freq_ht) {
3423 /* Clear HT flags from the driver */
3424 struct hostapd_freq_params freq;
3425 os_memset(&freq, 0, sizeof(freq));
3426 freq.freq = drv->last_freq;
3427 i802_set_freq(priv, &freq);
3428 }
3429
3430 if (drv->eapol_sock >= 0) {
3431 eloop_unregister_read_sock(drv->eapol_sock);
3432 close(drv->eapol_sock);
3433 }
3434
3435 if (drv->if_indices != drv->default_if_indices)
3436 os_free(drv->if_indices);
3437#endif /* HOSTAPD */
3438
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003439 if (drv->disabled_11b_rates)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003440 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
3441
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003442 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, 0,
3443 IF_OPER_UP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003444 rfkill_deinit(drv->rfkill);
3445
3446 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
3447
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003448 (void) linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0);
3449 wpa_driver_nl80211_set_mode(bss, NL80211_IFTYPE_STATION);
3450 nl80211_mgmt_unsubscribe(bss, "deinit");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003451
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003452 nl_cb_put(drv->nl_cb);
3453
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003454 nl80211_destroy_bss(&drv->first_bss);
3455
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003456 os_free(drv->filter_ssids);
3457
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003458 os_free(drv->auth_ie);
3459
3460 if (drv->in_interface_list)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003461 dl_list_del(&drv->list);
3462
3463 os_free(drv);
3464}
3465
3466
3467/**
3468 * wpa_driver_nl80211_scan_timeout - Scan timeout to report scan completion
3469 * @eloop_ctx: Driver private data
3470 * @timeout_ctx: ctx argument given to wpa_driver_nl80211_init()
3471 *
3472 * This function can be used as registered timeout when starting a scan to
3473 * generate a scan completed event if the driver does not report this.
3474 */
3475static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx, void *timeout_ctx)
3476{
3477 struct wpa_driver_nl80211_data *drv = eloop_ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003478 if (drv->ap_scan_as_station != NL80211_IFTYPE_UNSPECIFIED) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003479 wpa_driver_nl80211_set_mode(&drv->first_bss,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003480 drv->ap_scan_as_station);
3481 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003482 }
3483 wpa_printf(MSG_DEBUG, "Scan timeout - try to get results");
3484 wpa_supplicant_event(timeout_ctx, EVENT_SCAN_RESULTS, NULL);
3485}
3486
3487
3488/**
3489 * wpa_driver_nl80211_scan - Request the driver to initiate scan
3490 * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
3491 * @params: Scan parameters
3492 * Returns: 0 on success, -1 on failure
3493 */
3494static int wpa_driver_nl80211_scan(void *priv,
3495 struct wpa_driver_scan_params *params)
3496{
3497 struct i802_bss *bss = priv;
3498 struct wpa_driver_nl80211_data *drv = bss->drv;
3499 int ret = 0, timeout;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003500 struct nl_msg *msg, *ssids, *freqs, *rates;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003501 size_t i;
3502
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003503 drv->scan_for_auth = 0;
3504
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003505 msg = nlmsg_alloc();
3506 ssids = nlmsg_alloc();
3507 freqs = nlmsg_alloc();
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003508 rates = nlmsg_alloc();
3509 if (!msg || !ssids || !freqs || !rates) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003510 nlmsg_free(msg);
3511 nlmsg_free(ssids);
3512 nlmsg_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003513 nlmsg_free(rates);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003514 return -1;
3515 }
3516
3517 os_free(drv->filter_ssids);
3518 drv->filter_ssids = params->filter_ssids;
3519 params->filter_ssids = NULL;
3520 drv->num_filter_ssids = params->num_filter_ssids;
3521
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003522 nl80211_cmd(drv, msg, 0, NL80211_CMD_TRIGGER_SCAN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003523
3524 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
3525
3526 for (i = 0; i < params->num_ssids; i++) {
3527 wpa_hexdump_ascii(MSG_MSGDUMP, "nl80211: Scan SSID",
3528 params->ssids[i].ssid,
3529 params->ssids[i].ssid_len);
3530 NLA_PUT(ssids, i + 1, params->ssids[i].ssid_len,
3531 params->ssids[i].ssid);
3532 }
3533 if (params->num_ssids)
3534 nla_put_nested(msg, NL80211_ATTR_SCAN_SSIDS, ssids);
3535
3536 if (params->extra_ies) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003537 wpa_hexdump(MSG_MSGDUMP, "nl80211: Scan extra IEs",
3538 params->extra_ies, params->extra_ies_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003539 NLA_PUT(msg, NL80211_ATTR_IE, params->extra_ies_len,
3540 params->extra_ies);
3541 }
3542
3543 if (params->freqs) {
3544 for (i = 0; params->freqs[i]; i++) {
3545 wpa_printf(MSG_MSGDUMP, "nl80211: Scan frequency %u "
3546 "MHz", params->freqs[i]);
3547 NLA_PUT_U32(freqs, i + 1, params->freqs[i]);
3548 }
3549 nla_put_nested(msg, NL80211_ATTR_SCAN_FREQUENCIES, freqs);
3550 }
3551
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003552 if (params->p2p_probe) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07003553 wpa_printf(MSG_DEBUG, "nl80211: P2P probe - mask SuppRates");
3554
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003555 /*
3556 * Remove 2.4 GHz rates 1, 2, 5.5, 11 Mbps from supported rates
3557 * by masking out everything else apart from the OFDM rates 6,
3558 * 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS rates. All 5 GHz
3559 * rates are left enabled.
3560 */
3561 NLA_PUT(rates, NL80211_BAND_2GHZ, 8,
3562 "\x0c\x12\x18\x24\x30\x48\x60\x6c");
3563 nla_put_nested(msg, NL80211_ATTR_SCAN_SUPP_RATES, rates);
3564
3565 NLA_PUT_FLAG(msg, NL80211_ATTR_TX_NO_CCK_RATE);
3566 }
3567
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003568 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3569 msg = NULL;
3570 if (ret) {
3571 wpa_printf(MSG_DEBUG, "nl80211: Scan trigger failed: ret=%d "
3572 "(%s)", ret, strerror(-ret));
3573#ifdef HOSTAPD
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003574 if (is_ap_interface(drv->nlmode)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003575 /*
3576 * mac80211 does not allow scan requests in AP mode, so
3577 * try to do this in station mode.
3578 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003579 if (wpa_driver_nl80211_set_mode(
3580 bss, NL80211_IFTYPE_STATION))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003581 goto nla_put_failure;
3582
3583 if (wpa_driver_nl80211_scan(drv, params)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003584 wpa_driver_nl80211_set_mode(bss, drv->nlmode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003585 goto nla_put_failure;
3586 }
3587
3588 /* Restore AP mode when processing scan results */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003589 drv->ap_scan_as_station = drv->nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003590 ret = 0;
3591 } else
3592 goto nla_put_failure;
3593#else /* HOSTAPD */
3594 goto nla_put_failure;
3595#endif /* HOSTAPD */
3596 }
3597
3598 /* Not all drivers generate "scan completed" wireless event, so try to
3599 * read results after a timeout. */
3600 timeout = 10;
3601 if (drv->scan_complete_events) {
3602 /*
3603 * The driver seems to deliver events to notify when scan is
3604 * complete, so use longer timeout to avoid race conditions
3605 * with scanning and following association request.
3606 */
3607 timeout = 30;
3608 }
3609 wpa_printf(MSG_DEBUG, "Scan requested (ret=%d) - scan timeout %d "
3610 "seconds", ret, timeout);
3611 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
3612 eloop_register_timeout(timeout, 0, wpa_driver_nl80211_scan_timeout,
3613 drv, drv->ctx);
3614
3615nla_put_failure:
3616 nlmsg_free(ssids);
3617 nlmsg_free(msg);
3618 nlmsg_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003619 nlmsg_free(rates);
3620 return ret;
3621}
3622
3623
3624/**
3625 * wpa_driver_nl80211_sched_scan - Initiate a scheduled scan
3626 * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
3627 * @params: Scan parameters
3628 * @interval: Interval between scan cycles in milliseconds
3629 * Returns: 0 on success, -1 on failure or if not supported
3630 */
3631static int wpa_driver_nl80211_sched_scan(void *priv,
3632 struct wpa_driver_scan_params *params,
3633 u32 interval)
3634{
3635 struct i802_bss *bss = priv;
3636 struct wpa_driver_nl80211_data *drv = bss->drv;
3637 int ret = 0;
3638 struct nl_msg *msg, *ssids, *freqs, *match_set_ssid, *match_sets;
3639 size_t i;
3640
3641#ifdef ANDROID
3642 if (!drv->capa.sched_scan_supported)
3643 return android_pno_start(bss, params);
3644#endif /* ANDROID */
3645
3646 msg = nlmsg_alloc();
3647 ssids = nlmsg_alloc();
3648 freqs = nlmsg_alloc();
3649 if (!msg || !ssids || !freqs) {
3650 nlmsg_free(msg);
3651 nlmsg_free(ssids);
3652 nlmsg_free(freqs);
3653 return -1;
3654 }
3655
3656 os_free(drv->filter_ssids);
3657 drv->filter_ssids = params->filter_ssids;
3658 params->filter_ssids = NULL;
3659 drv->num_filter_ssids = params->num_filter_ssids;
3660
3661 nl80211_cmd(drv, msg, 0, NL80211_CMD_START_SCHED_SCAN);
3662
3663 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
3664
3665 NLA_PUT_U32(msg, NL80211_ATTR_SCHED_SCAN_INTERVAL, interval);
3666
3667 if (drv->num_filter_ssids &&
3668 (int) drv->num_filter_ssids <= drv->capa.max_match_sets) {
3669 match_sets = nlmsg_alloc();
3670
3671 for (i = 0; i < drv->num_filter_ssids; i++) {
3672 wpa_hexdump_ascii(MSG_MSGDUMP,
3673 "nl80211: Sched scan filter SSID",
3674 drv->filter_ssids[i].ssid,
3675 drv->filter_ssids[i].ssid_len);
3676
3677 match_set_ssid = nlmsg_alloc();
3678 nla_put(match_set_ssid,
3679 NL80211_ATTR_SCHED_SCAN_MATCH_SSID,
3680 drv->filter_ssids[i].ssid_len,
3681 drv->filter_ssids[i].ssid);
3682
3683 nla_put_nested(match_sets, i + 1, match_set_ssid);
3684
3685 nlmsg_free(match_set_ssid);
3686 }
3687
3688 nla_put_nested(msg, NL80211_ATTR_SCHED_SCAN_MATCH,
3689 match_sets);
3690 nlmsg_free(match_sets);
3691 }
3692
3693 for (i = 0; i < params->num_ssids; i++) {
3694 wpa_hexdump_ascii(MSG_MSGDUMP, "nl80211: Sched scan SSID",
3695 params->ssids[i].ssid,
3696 params->ssids[i].ssid_len);
3697 NLA_PUT(ssids, i + 1, params->ssids[i].ssid_len,
3698 params->ssids[i].ssid);
3699 }
3700 if (params->num_ssids)
3701 nla_put_nested(msg, NL80211_ATTR_SCAN_SSIDS, ssids);
3702
3703 if (params->extra_ies) {
3704 wpa_hexdump_ascii(MSG_MSGDUMP, "nl80211: Sched scan extra IEs",
3705 params->extra_ies, params->extra_ies_len);
3706 NLA_PUT(msg, NL80211_ATTR_IE, params->extra_ies_len,
3707 params->extra_ies);
3708 }
3709
3710 if (params->freqs) {
3711 for (i = 0; params->freqs[i]; i++) {
3712 wpa_printf(MSG_MSGDUMP, "nl80211: Scan frequency %u "
3713 "MHz", params->freqs[i]);
3714 NLA_PUT_U32(freqs, i + 1, params->freqs[i]);
3715 }
3716 nla_put_nested(msg, NL80211_ATTR_SCAN_FREQUENCIES, freqs);
3717 }
3718
3719 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3720
3721 /* TODO: if we get an error here, we should fall back to normal scan */
3722
3723 msg = NULL;
3724 if (ret) {
3725 wpa_printf(MSG_DEBUG, "nl80211: Sched scan start failed: "
3726 "ret=%d (%s)", ret, strerror(-ret));
3727 goto nla_put_failure;
3728 }
3729
3730 wpa_printf(MSG_DEBUG, "nl80211: Sched scan requested (ret=%d) - "
3731 "scan interval %d msec", ret, interval);
3732
3733nla_put_failure:
3734 nlmsg_free(ssids);
3735 nlmsg_free(msg);
3736 nlmsg_free(freqs);
3737 return ret;
3738}
3739
3740
3741/**
3742 * wpa_driver_nl80211_stop_sched_scan - Stop a scheduled scan
3743 * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
3744 * Returns: 0 on success, -1 on failure or if not supported
3745 */
3746static int wpa_driver_nl80211_stop_sched_scan(void *priv)
3747{
3748 struct i802_bss *bss = priv;
3749 struct wpa_driver_nl80211_data *drv = bss->drv;
3750 int ret = 0;
3751 struct nl_msg *msg;
3752
3753#ifdef ANDROID
3754 if (!drv->capa.sched_scan_supported)
3755 return android_pno_stop(bss);
3756#endif /* ANDROID */
3757
3758 msg = nlmsg_alloc();
3759 if (!msg)
3760 return -1;
3761
3762 nl80211_cmd(drv, msg, 0, NL80211_CMD_STOP_SCHED_SCAN);
3763
3764 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
3765
3766 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3767 msg = NULL;
3768 if (ret) {
3769 wpa_printf(MSG_DEBUG, "nl80211: Sched scan stop failed: "
3770 "ret=%d (%s)", ret, strerror(-ret));
3771 goto nla_put_failure;
3772 }
3773
3774 wpa_printf(MSG_DEBUG, "nl80211: Sched scan stop sent (ret=%d)", ret);
3775
3776nla_put_failure:
3777 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003778 return ret;
3779}
3780
3781
3782static const u8 * nl80211_get_ie(const u8 *ies, size_t ies_len, u8 ie)
3783{
3784 const u8 *end, *pos;
3785
3786 if (ies == NULL)
3787 return NULL;
3788
3789 pos = ies;
3790 end = ies + ies_len;
3791
3792 while (pos + 1 < end) {
3793 if (pos + 2 + pos[1] > end)
3794 break;
3795 if (pos[0] == ie)
3796 return pos;
3797 pos += 2 + pos[1];
3798 }
3799
3800 return NULL;
3801}
3802
3803
3804static int nl80211_scan_filtered(struct wpa_driver_nl80211_data *drv,
3805 const u8 *ie, size_t ie_len)
3806{
3807 const u8 *ssid;
3808 size_t i;
3809
3810 if (drv->filter_ssids == NULL)
3811 return 0;
3812
3813 ssid = nl80211_get_ie(ie, ie_len, WLAN_EID_SSID);
3814 if (ssid == NULL)
3815 return 1;
3816
3817 for (i = 0; i < drv->num_filter_ssids; i++) {
3818 if (ssid[1] == drv->filter_ssids[i].ssid_len &&
3819 os_memcmp(ssid + 2, drv->filter_ssids[i].ssid, ssid[1]) ==
3820 0)
3821 return 0;
3822 }
3823
3824 return 1;
3825}
3826
3827
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003828static int bss_info_handler(struct nl_msg *msg, void *arg)
3829{
3830 struct nlattr *tb[NL80211_ATTR_MAX + 1];
3831 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3832 struct nlattr *bss[NL80211_BSS_MAX + 1];
3833 static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
3834 [NL80211_BSS_BSSID] = { .type = NLA_UNSPEC },
3835 [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
3836 [NL80211_BSS_TSF] = { .type = NLA_U64 },
3837 [NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 },
3838 [NL80211_BSS_CAPABILITY] = { .type = NLA_U16 },
3839 [NL80211_BSS_INFORMATION_ELEMENTS] = { .type = NLA_UNSPEC },
3840 [NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 },
3841 [NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 },
3842 [NL80211_BSS_STATUS] = { .type = NLA_U32 },
3843 [NL80211_BSS_SEEN_MS_AGO] = { .type = NLA_U32 },
3844 [NL80211_BSS_BEACON_IES] = { .type = NLA_UNSPEC },
3845 };
3846 struct nl80211_bss_info_arg *_arg = arg;
3847 struct wpa_scan_results *res = _arg->res;
3848 struct wpa_scan_res **tmp;
3849 struct wpa_scan_res *r;
3850 const u8 *ie, *beacon_ie;
3851 size_t ie_len, beacon_ie_len;
3852 u8 *pos;
Jouni Malinen87fd2792011-05-16 18:35:42 +03003853 size_t i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003854
3855 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3856 genlmsg_attrlen(gnlh, 0), NULL);
3857 if (!tb[NL80211_ATTR_BSS])
3858 return NL_SKIP;
3859 if (nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS],
3860 bss_policy))
3861 return NL_SKIP;
Jouni Malinen87fd2792011-05-16 18:35:42 +03003862 if (bss[NL80211_BSS_STATUS]) {
3863 enum nl80211_bss_status status;
3864 status = nla_get_u32(bss[NL80211_BSS_STATUS]);
3865 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
3866 bss[NL80211_BSS_FREQUENCY]) {
3867 _arg->assoc_freq =
3868 nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
3869 wpa_printf(MSG_DEBUG, "nl80211: Associated on %u MHz",
3870 _arg->assoc_freq);
3871 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003872 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
3873 bss[NL80211_BSS_BSSID]) {
3874 os_memcpy(_arg->assoc_bssid,
3875 nla_data(bss[NL80211_BSS_BSSID]), ETH_ALEN);
3876 wpa_printf(MSG_DEBUG, "nl80211: Associated with "
3877 MACSTR, MAC2STR(_arg->assoc_bssid));
3878 }
Jouni Malinen87fd2792011-05-16 18:35:42 +03003879 }
3880 if (!res)
3881 return NL_SKIP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003882 if (bss[NL80211_BSS_INFORMATION_ELEMENTS]) {
3883 ie = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
3884 ie_len = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
3885 } else {
3886 ie = NULL;
3887 ie_len = 0;
3888 }
3889 if (bss[NL80211_BSS_BEACON_IES]) {
3890 beacon_ie = nla_data(bss[NL80211_BSS_BEACON_IES]);
3891 beacon_ie_len = nla_len(bss[NL80211_BSS_BEACON_IES]);
3892 } else {
3893 beacon_ie = NULL;
3894 beacon_ie_len = 0;
3895 }
3896
3897 if (nl80211_scan_filtered(_arg->drv, ie ? ie : beacon_ie,
3898 ie ? ie_len : beacon_ie_len))
3899 return NL_SKIP;
3900
3901 r = os_zalloc(sizeof(*r) + ie_len + beacon_ie_len);
3902 if (r == NULL)
3903 return NL_SKIP;
3904 if (bss[NL80211_BSS_BSSID])
3905 os_memcpy(r->bssid, nla_data(bss[NL80211_BSS_BSSID]),
3906 ETH_ALEN);
3907 if (bss[NL80211_BSS_FREQUENCY])
3908 r->freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
3909 if (bss[NL80211_BSS_BEACON_INTERVAL])
3910 r->beacon_int = nla_get_u16(bss[NL80211_BSS_BEACON_INTERVAL]);
3911 if (bss[NL80211_BSS_CAPABILITY])
3912 r->caps = nla_get_u16(bss[NL80211_BSS_CAPABILITY]);
3913 r->flags |= WPA_SCAN_NOISE_INVALID;
3914 if (bss[NL80211_BSS_SIGNAL_MBM]) {
3915 r->level = nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]);
3916 r->level /= 100; /* mBm to dBm */
3917 r->flags |= WPA_SCAN_LEVEL_DBM | WPA_SCAN_QUAL_INVALID;
3918 } else if (bss[NL80211_BSS_SIGNAL_UNSPEC]) {
3919 r->level = nla_get_u8(bss[NL80211_BSS_SIGNAL_UNSPEC]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003920 r->flags |= WPA_SCAN_QUAL_INVALID;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003921 } else
3922 r->flags |= WPA_SCAN_LEVEL_INVALID | WPA_SCAN_QUAL_INVALID;
3923 if (bss[NL80211_BSS_TSF])
3924 r->tsf = nla_get_u64(bss[NL80211_BSS_TSF]);
3925 if (bss[NL80211_BSS_SEEN_MS_AGO])
3926 r->age = nla_get_u32(bss[NL80211_BSS_SEEN_MS_AGO]);
3927 r->ie_len = ie_len;
3928 pos = (u8 *) (r + 1);
3929 if (ie) {
3930 os_memcpy(pos, ie, ie_len);
3931 pos += ie_len;
3932 }
3933 r->beacon_ie_len = beacon_ie_len;
3934 if (beacon_ie)
3935 os_memcpy(pos, beacon_ie, beacon_ie_len);
3936
3937 if (bss[NL80211_BSS_STATUS]) {
3938 enum nl80211_bss_status status;
3939 status = nla_get_u32(bss[NL80211_BSS_STATUS]);
3940 switch (status) {
3941 case NL80211_BSS_STATUS_AUTHENTICATED:
3942 r->flags |= WPA_SCAN_AUTHENTICATED;
3943 break;
3944 case NL80211_BSS_STATUS_ASSOCIATED:
3945 r->flags |= WPA_SCAN_ASSOCIATED;
3946 break;
3947 default:
3948 break;
3949 }
3950 }
3951
Jouni Malinen87fd2792011-05-16 18:35:42 +03003952 /*
3953 * cfg80211 maintains separate BSS table entries for APs if the same
3954 * BSSID,SSID pair is seen on multiple channels. wpa_supplicant does
3955 * not use frequency as a separate key in the BSS table, so filter out
3956 * duplicated entries. Prefer associated BSS entry in such a case in
3957 * order to get the correct frequency into the BSS table.
3958 */
3959 for (i = 0; i < res->num; i++) {
3960 const u8 *s1, *s2;
3961 if (os_memcmp(res->res[i]->bssid, r->bssid, ETH_ALEN) != 0)
3962 continue;
3963
3964 s1 = nl80211_get_ie((u8 *) (res->res[i] + 1),
3965 res->res[i]->ie_len, WLAN_EID_SSID);
3966 s2 = nl80211_get_ie((u8 *) (r + 1), r->ie_len, WLAN_EID_SSID);
3967 if (s1 == NULL || s2 == NULL || s1[1] != s2[1] ||
3968 os_memcmp(s1, s2, 2 + s1[1]) != 0)
3969 continue;
3970
3971 /* Same BSSID,SSID was already included in scan results */
3972 wpa_printf(MSG_DEBUG, "nl80211: Remove duplicated scan result "
3973 "for " MACSTR, MAC2STR(r->bssid));
3974
3975 if ((r->flags & WPA_SCAN_ASSOCIATED) &&
3976 !(res->res[i]->flags & WPA_SCAN_ASSOCIATED)) {
3977 os_free(res->res[i]);
3978 res->res[i] = r;
3979 } else
3980 os_free(r);
3981 return NL_SKIP;
3982 }
3983
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003984 tmp = os_realloc(res->res,
3985 (res->num + 1) * sizeof(struct wpa_scan_res *));
3986 if (tmp == NULL) {
3987 os_free(r);
3988 return NL_SKIP;
3989 }
3990 tmp[res->num++] = r;
3991 res->res = tmp;
3992
3993 return NL_SKIP;
3994}
3995
3996
3997static void clear_state_mismatch(struct wpa_driver_nl80211_data *drv,
3998 const u8 *addr)
3999{
4000 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
4001 wpa_printf(MSG_DEBUG, "nl80211: Clear possible state "
4002 "mismatch (" MACSTR ")", MAC2STR(addr));
4003 wpa_driver_nl80211_mlme(drv, addr,
4004 NL80211_CMD_DEAUTHENTICATE,
4005 WLAN_REASON_PREV_AUTH_NOT_VALID, 1);
4006 }
4007}
4008
4009
4010static void wpa_driver_nl80211_check_bss_status(
4011 struct wpa_driver_nl80211_data *drv, struct wpa_scan_results *res)
4012{
4013 size_t i;
4014
4015 for (i = 0; i < res->num; i++) {
4016 struct wpa_scan_res *r = res->res[i];
4017 if (r->flags & WPA_SCAN_AUTHENTICATED) {
4018 wpa_printf(MSG_DEBUG, "nl80211: Scan results "
4019 "indicates BSS status with " MACSTR
4020 " as authenticated",
4021 MAC2STR(r->bssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004022 if (is_sta_interface(drv->nlmode) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004023 os_memcmp(r->bssid, drv->bssid, ETH_ALEN) != 0 &&
4024 os_memcmp(r->bssid, drv->auth_bssid, ETH_ALEN) !=
4025 0) {
4026 wpa_printf(MSG_DEBUG, "nl80211: Unknown BSSID"
4027 " in local state (auth=" MACSTR
4028 " assoc=" MACSTR ")",
4029 MAC2STR(drv->auth_bssid),
4030 MAC2STR(drv->bssid));
4031 clear_state_mismatch(drv, r->bssid);
4032 }
4033 }
4034
4035 if (r->flags & WPA_SCAN_ASSOCIATED) {
4036 wpa_printf(MSG_DEBUG, "nl80211: Scan results "
4037 "indicate BSS status with " MACSTR
4038 " as associated",
4039 MAC2STR(r->bssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004040 if (is_sta_interface(drv->nlmode) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004041 !drv->associated) {
4042 wpa_printf(MSG_DEBUG, "nl80211: Local state "
4043 "(not associated) does not match "
4044 "with BSS state");
4045 clear_state_mismatch(drv, r->bssid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004046 } else if (is_sta_interface(drv->nlmode) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004047 os_memcmp(drv->bssid, r->bssid, ETH_ALEN) !=
4048 0) {
4049 wpa_printf(MSG_DEBUG, "nl80211: Local state "
4050 "(associated with " MACSTR ") does "
4051 "not match with BSS state",
4052 MAC2STR(drv->bssid));
4053 clear_state_mismatch(drv, r->bssid);
4054 clear_state_mismatch(drv, drv->bssid);
4055 }
4056 }
4057 }
4058}
4059
4060
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004061static struct wpa_scan_results *
4062nl80211_get_scan_results(struct wpa_driver_nl80211_data *drv)
4063{
4064 struct nl_msg *msg;
4065 struct wpa_scan_results *res;
4066 int ret;
4067 struct nl80211_bss_info_arg arg;
4068
4069 res = os_zalloc(sizeof(*res));
4070 if (res == NULL)
4071 return NULL;
4072 msg = nlmsg_alloc();
4073 if (!msg)
4074 goto nla_put_failure;
4075
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004076 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004077 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
4078
4079 arg.drv = drv;
4080 arg.res = res;
4081 ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
4082 msg = NULL;
4083 if (ret == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004084 wpa_printf(MSG_DEBUG, "nl80211: Received scan results (%lu "
4085 "BSSes)", (unsigned long) res->num);
4086 nl80211_get_noise_for_scan_results(drv, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004087 return res;
4088 }
4089 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
4090 "(%s)", ret, strerror(-ret));
4091nla_put_failure:
4092 nlmsg_free(msg);
4093 wpa_scan_results_free(res);
4094 return NULL;
4095}
4096
4097
4098/**
4099 * wpa_driver_nl80211_get_scan_results - Fetch the latest scan results
4100 * @priv: Pointer to private wext data from wpa_driver_nl80211_init()
4101 * Returns: Scan results on success, -1 on failure
4102 */
4103static struct wpa_scan_results *
4104wpa_driver_nl80211_get_scan_results(void *priv)
4105{
4106 struct i802_bss *bss = priv;
4107 struct wpa_driver_nl80211_data *drv = bss->drv;
4108 struct wpa_scan_results *res;
4109
4110 res = nl80211_get_scan_results(drv);
4111 if (res)
4112 wpa_driver_nl80211_check_bss_status(drv, res);
4113 return res;
4114}
4115
4116
4117static void nl80211_dump_scan(struct wpa_driver_nl80211_data *drv)
4118{
4119 struct wpa_scan_results *res;
4120 size_t i;
4121
4122 res = nl80211_get_scan_results(drv);
4123 if (res == NULL) {
4124 wpa_printf(MSG_DEBUG, "nl80211: Failed to get scan results");
4125 return;
4126 }
4127
4128 wpa_printf(MSG_DEBUG, "nl80211: Scan result dump");
4129 for (i = 0; i < res->num; i++) {
4130 struct wpa_scan_res *r = res->res[i];
4131 wpa_printf(MSG_DEBUG, "nl80211: %d/%d " MACSTR "%s%s",
4132 (int) i, (int) res->num, MAC2STR(r->bssid),
4133 r->flags & WPA_SCAN_AUTHENTICATED ? " [auth]" : "",
4134 r->flags & WPA_SCAN_ASSOCIATED ? " [assoc]" : "");
4135 }
4136
4137 wpa_scan_results_free(res);
4138}
4139
4140
4141static int wpa_driver_nl80211_set_key(const char *ifname, void *priv,
4142 enum wpa_alg alg, const u8 *addr,
4143 int key_idx, int set_tx,
4144 const u8 *seq, size_t seq_len,
4145 const u8 *key, size_t key_len)
4146{
4147 struct i802_bss *bss = priv;
4148 struct wpa_driver_nl80211_data *drv = bss->drv;
4149 int ifindex = if_nametoindex(ifname);
4150 struct nl_msg *msg;
4151 int ret;
4152
4153 wpa_printf(MSG_DEBUG, "%s: ifindex=%d alg=%d addr=%p key_idx=%d "
4154 "set_tx=%d seq_len=%lu key_len=%lu",
4155 __func__, ifindex, alg, addr, key_idx, set_tx,
4156 (unsigned long) seq_len, (unsigned long) key_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004157#ifdef CONFIG_TDLS
4158 if (key_idx == -1)
4159 key_idx = 0;
4160#endif /* CONFIG_TDLS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004161
4162 msg = nlmsg_alloc();
4163 if (!msg)
4164 return -ENOMEM;
4165
4166 if (alg == WPA_ALG_NONE) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004167 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_KEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004168 } else {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004169 nl80211_cmd(drv, msg, 0, NL80211_CMD_NEW_KEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004170 NLA_PUT(msg, NL80211_ATTR_KEY_DATA, key_len, key);
4171 switch (alg) {
4172 case WPA_ALG_WEP:
4173 if (key_len == 5)
4174 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
4175 WLAN_CIPHER_SUITE_WEP40);
4176 else
4177 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
4178 WLAN_CIPHER_SUITE_WEP104);
4179 break;
4180 case WPA_ALG_TKIP:
4181 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
4182 WLAN_CIPHER_SUITE_TKIP);
4183 break;
4184 case WPA_ALG_CCMP:
4185 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
4186 WLAN_CIPHER_SUITE_CCMP);
4187 break;
4188 case WPA_ALG_IGTK:
4189 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
4190 WLAN_CIPHER_SUITE_AES_CMAC);
4191 break;
4192 default:
4193 wpa_printf(MSG_ERROR, "%s: Unsupported encryption "
4194 "algorithm %d", __func__, alg);
4195 nlmsg_free(msg);
4196 return -1;
4197 }
4198 }
4199
4200 if (seq && seq_len)
4201 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, seq_len, seq);
4202
4203 if (addr && !is_broadcast_ether_addr(addr)) {
4204 wpa_printf(MSG_DEBUG, " addr=" MACSTR, MAC2STR(addr));
4205 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
4206
4207 if (alg != WPA_ALG_WEP && key_idx && !set_tx) {
4208 wpa_printf(MSG_DEBUG, " RSN IBSS RX GTK");
4209 NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE,
4210 NL80211_KEYTYPE_GROUP);
4211 }
4212 } else if (addr && is_broadcast_ether_addr(addr)) {
4213 struct nl_msg *types;
4214 int err;
4215 wpa_printf(MSG_DEBUG, " broadcast key");
4216 types = nlmsg_alloc();
4217 if (!types)
4218 goto nla_put_failure;
4219 NLA_PUT_FLAG(types, NL80211_KEY_DEFAULT_TYPE_MULTICAST);
4220 err = nla_put_nested(msg, NL80211_ATTR_KEY_DEFAULT_TYPES,
4221 types);
4222 nlmsg_free(types);
4223 if (err)
4224 goto nla_put_failure;
4225 }
4226 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
4227 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
4228
4229 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4230 if ((ret == -ENOENT || ret == -ENOLINK) && alg == WPA_ALG_NONE)
4231 ret = 0;
4232 if (ret)
4233 wpa_printf(MSG_DEBUG, "nl80211: set_key failed; err=%d %s)",
4234 ret, strerror(-ret));
4235
4236 /*
4237 * If we failed or don't need to set the default TX key (below),
4238 * we're done here.
4239 */
4240 if (ret || !set_tx || alg == WPA_ALG_NONE)
4241 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004242 if (is_ap_interface(drv->nlmode) && addr &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004243 !is_broadcast_ether_addr(addr))
4244 return ret;
4245
4246 msg = nlmsg_alloc();
4247 if (!msg)
4248 return -ENOMEM;
4249
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004250 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_KEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004251 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
4252 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
4253 if (alg == WPA_ALG_IGTK)
4254 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT_MGMT);
4255 else
4256 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT);
4257 if (addr && is_broadcast_ether_addr(addr)) {
4258 struct nl_msg *types;
4259 int err;
4260 types = nlmsg_alloc();
4261 if (!types)
4262 goto nla_put_failure;
4263 NLA_PUT_FLAG(types, NL80211_KEY_DEFAULT_TYPE_MULTICAST);
4264 err = nla_put_nested(msg, NL80211_ATTR_KEY_DEFAULT_TYPES,
4265 types);
4266 nlmsg_free(types);
4267 if (err)
4268 goto nla_put_failure;
4269 } else if (addr) {
4270 struct nl_msg *types;
4271 int err;
4272 types = nlmsg_alloc();
4273 if (!types)
4274 goto nla_put_failure;
4275 NLA_PUT_FLAG(types, NL80211_KEY_DEFAULT_TYPE_UNICAST);
4276 err = nla_put_nested(msg, NL80211_ATTR_KEY_DEFAULT_TYPES,
4277 types);
4278 nlmsg_free(types);
4279 if (err)
4280 goto nla_put_failure;
4281 }
4282
4283 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4284 if (ret == -ENOENT)
4285 ret = 0;
4286 if (ret)
4287 wpa_printf(MSG_DEBUG, "nl80211: set_key default failed; "
4288 "err=%d %s)", ret, strerror(-ret));
4289 return ret;
4290
4291nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004292 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004293 return -ENOBUFS;
4294}
4295
4296
4297static int nl_add_key(struct nl_msg *msg, enum wpa_alg alg,
4298 int key_idx, int defkey,
4299 const u8 *seq, size_t seq_len,
4300 const u8 *key, size_t key_len)
4301{
4302 struct nlattr *key_attr = nla_nest_start(msg, NL80211_ATTR_KEY);
4303 if (!key_attr)
4304 return -1;
4305
4306 if (defkey && alg == WPA_ALG_IGTK)
4307 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_MGMT);
4308 else if (defkey)
4309 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
4310
4311 NLA_PUT_U8(msg, NL80211_KEY_IDX, key_idx);
4312
4313 switch (alg) {
4314 case WPA_ALG_WEP:
4315 if (key_len == 5)
4316 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
4317 WLAN_CIPHER_SUITE_WEP40);
4318 else
4319 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
4320 WLAN_CIPHER_SUITE_WEP104);
4321 break;
4322 case WPA_ALG_TKIP:
4323 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, WLAN_CIPHER_SUITE_TKIP);
4324 break;
4325 case WPA_ALG_CCMP:
4326 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, WLAN_CIPHER_SUITE_CCMP);
4327 break;
4328 case WPA_ALG_IGTK:
4329 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
4330 WLAN_CIPHER_SUITE_AES_CMAC);
4331 break;
4332 default:
4333 wpa_printf(MSG_ERROR, "%s: Unsupported encryption "
4334 "algorithm %d", __func__, alg);
4335 return -1;
4336 }
4337
4338 if (seq && seq_len)
4339 NLA_PUT(msg, NL80211_KEY_SEQ, seq_len, seq);
4340
4341 NLA_PUT(msg, NL80211_KEY_DATA, key_len, key);
4342
4343 nla_nest_end(msg, key_attr);
4344
4345 return 0;
4346 nla_put_failure:
4347 return -1;
4348}
4349
4350
4351static int nl80211_set_conn_keys(struct wpa_driver_associate_params *params,
4352 struct nl_msg *msg)
4353{
4354 int i, privacy = 0;
4355 struct nlattr *nl_keys, *nl_key;
4356
4357 for (i = 0; i < 4; i++) {
4358 if (!params->wep_key[i])
4359 continue;
4360 privacy = 1;
4361 break;
4362 }
4363 if (params->wps == WPS_MODE_PRIVACY)
4364 privacy = 1;
4365 if (params->pairwise_suite &&
4366 params->pairwise_suite != WPA_CIPHER_NONE)
4367 privacy = 1;
4368
4369 if (!privacy)
4370 return 0;
4371
4372 NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
4373
4374 nl_keys = nla_nest_start(msg, NL80211_ATTR_KEYS);
4375 if (!nl_keys)
4376 goto nla_put_failure;
4377
4378 for (i = 0; i < 4; i++) {
4379 if (!params->wep_key[i])
4380 continue;
4381
4382 nl_key = nla_nest_start(msg, i);
4383 if (!nl_key)
4384 goto nla_put_failure;
4385
4386 NLA_PUT(msg, NL80211_KEY_DATA, params->wep_key_len[i],
4387 params->wep_key[i]);
4388 if (params->wep_key_len[i] == 5)
4389 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
4390 WLAN_CIPHER_SUITE_WEP40);
4391 else
4392 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
4393 WLAN_CIPHER_SUITE_WEP104);
4394
4395 NLA_PUT_U8(msg, NL80211_KEY_IDX, i);
4396
4397 if (i == params->wep_tx_keyidx)
4398 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
4399
4400 nla_nest_end(msg, nl_key);
4401 }
4402 nla_nest_end(msg, nl_keys);
4403
4404 return 0;
4405
4406nla_put_failure:
4407 return -ENOBUFS;
4408}
4409
4410
4411static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
4412 const u8 *addr, int cmd, u16 reason_code,
4413 int local_state_change)
4414{
4415 int ret = -1;
4416 struct nl_msg *msg;
4417
4418 msg = nlmsg_alloc();
4419 if (!msg)
4420 return -1;
4421
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004422 nl80211_cmd(drv, msg, 0, cmd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004423
4424 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
4425 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason_code);
4426 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
4427 if (local_state_change)
4428 NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE);
4429
4430 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4431 msg = NULL;
4432 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004433 wpa_dbg(drv->ctx, MSG_DEBUG,
4434 "nl80211: MLME command failed: reason=%u ret=%d (%s)",
4435 reason_code, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004436 goto nla_put_failure;
4437 }
4438 ret = 0;
4439
4440nla_put_failure:
4441 nlmsg_free(msg);
4442 return ret;
4443}
4444
4445
4446static int wpa_driver_nl80211_disconnect(struct wpa_driver_nl80211_data *drv,
4447 const u8 *addr, int reason_code)
4448{
4449 wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " reason_code=%d)",
4450 __func__, MAC2STR(addr), reason_code);
4451 drv->associated = 0;
4452 return wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DISCONNECT,
4453 reason_code, 0);
4454}
4455
4456
4457static int wpa_driver_nl80211_deauthenticate(void *priv, const u8 *addr,
4458 int reason_code)
4459{
4460 struct i802_bss *bss = priv;
4461 struct wpa_driver_nl80211_data *drv = bss->drv;
4462 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME))
4463 return wpa_driver_nl80211_disconnect(drv, addr, reason_code);
4464 wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " reason_code=%d)",
4465 __func__, MAC2STR(addr), reason_code);
4466 drv->associated = 0;
4467 if (drv->nlmode == NL80211_IFTYPE_ADHOC)
4468 return nl80211_leave_ibss(drv);
4469 return wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DEAUTHENTICATE,
4470 reason_code, 0);
4471}
4472
4473
4474static int wpa_driver_nl80211_disassociate(void *priv, const u8 *addr,
4475 int reason_code)
4476{
4477 struct i802_bss *bss = priv;
4478 struct wpa_driver_nl80211_data *drv = bss->drv;
4479 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME))
4480 return wpa_driver_nl80211_disconnect(drv, addr, reason_code);
4481 wpa_printf(MSG_DEBUG, "%s", __func__);
4482 drv->associated = 0;
4483 return wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DISASSOCIATE,
4484 reason_code, 0);
4485}
4486
4487
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004488static void nl80211_copy_auth_params(struct wpa_driver_nl80211_data *drv,
4489 struct wpa_driver_auth_params *params)
4490{
4491 int i;
4492
4493 drv->auth_freq = params->freq;
4494 drv->auth_alg = params->auth_alg;
4495 drv->auth_wep_tx_keyidx = params->wep_tx_keyidx;
4496 drv->auth_local_state_change = params->local_state_change;
4497 drv->auth_p2p = params->p2p;
4498
4499 if (params->bssid)
4500 os_memcpy(drv->auth_bssid_, params->bssid, ETH_ALEN);
4501 else
4502 os_memset(drv->auth_bssid_, 0, ETH_ALEN);
4503
4504 if (params->ssid) {
4505 os_memcpy(drv->auth_ssid, params->ssid, params->ssid_len);
4506 drv->auth_ssid_len = params->ssid_len;
4507 } else
4508 drv->auth_ssid_len = 0;
4509
4510
4511 os_free(drv->auth_ie);
4512 drv->auth_ie = NULL;
4513 drv->auth_ie_len = 0;
4514 if (params->ie) {
4515 drv->auth_ie = os_malloc(params->ie_len);
4516 if (drv->auth_ie) {
4517 os_memcpy(drv->auth_ie, params->ie, params->ie_len);
4518 drv->auth_ie_len = params->ie_len;
4519 }
4520 }
4521
4522 for (i = 0; i < 4; i++) {
4523 if (params->wep_key[i] && params->wep_key_len[i] &&
4524 params->wep_key_len[i] <= 16) {
4525 os_memcpy(drv->auth_wep_key[i], params->wep_key[i],
4526 params->wep_key_len[i]);
4527 drv->auth_wep_key_len[i] = params->wep_key_len[i];
4528 } else
4529 drv->auth_wep_key_len[i] = 0;
4530 }
4531}
4532
4533
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004534static int wpa_driver_nl80211_authenticate(
4535 void *priv, struct wpa_driver_auth_params *params)
4536{
4537 struct i802_bss *bss = priv;
4538 struct wpa_driver_nl80211_data *drv = bss->drv;
4539 int ret = -1, i;
4540 struct nl_msg *msg;
4541 enum nl80211_auth_type type;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004542 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004543 int count = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004544 int is_retry;
4545
4546 is_retry = drv->retry_auth;
4547 drv->retry_auth = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004548
4549 drv->associated = 0;
4550 os_memset(drv->auth_bssid, 0, ETH_ALEN);
4551 /* FIX: IBSS mode */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004552 nlmode = params->p2p ?
4553 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
4554 if (drv->nlmode != nlmode &&
4555 wpa_driver_nl80211_set_mode(priv, nlmode) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004556 return -1;
4557
4558retry:
4559 msg = nlmsg_alloc();
4560 if (!msg)
4561 return -1;
4562
4563 wpa_printf(MSG_DEBUG, "nl80211: Authenticate (ifindex=%d)",
4564 drv->ifindex);
4565
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004566 nl80211_cmd(drv, msg, 0, NL80211_CMD_AUTHENTICATE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004567
4568 for (i = 0; i < 4; i++) {
4569 if (!params->wep_key[i])
4570 continue;
4571 wpa_driver_nl80211_set_key(bss->ifname, priv, WPA_ALG_WEP,
4572 NULL, i,
4573 i == params->wep_tx_keyidx, NULL, 0,
4574 params->wep_key[i],
4575 params->wep_key_len[i]);
4576 if (params->wep_tx_keyidx != i)
4577 continue;
4578 if (nl_add_key(msg, WPA_ALG_WEP, i, 1, NULL, 0,
4579 params->wep_key[i], params->wep_key_len[i])) {
4580 nlmsg_free(msg);
4581 return -1;
4582 }
4583 }
4584
4585 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
4586 if (params->bssid) {
4587 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
4588 MAC2STR(params->bssid));
4589 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
4590 }
4591 if (params->freq) {
4592 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
4593 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
4594 }
4595 if (params->ssid) {
4596 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
4597 params->ssid, params->ssid_len);
4598 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
4599 params->ssid);
4600 }
4601 wpa_hexdump(MSG_DEBUG, " * IEs", params->ie, params->ie_len);
4602 if (params->ie)
4603 NLA_PUT(msg, NL80211_ATTR_IE, params->ie_len, params->ie);
4604 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
4605 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
4606 else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
4607 type = NL80211_AUTHTYPE_SHARED_KEY;
4608 else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
4609 type = NL80211_AUTHTYPE_NETWORK_EAP;
4610 else if (params->auth_alg & WPA_AUTH_ALG_FT)
4611 type = NL80211_AUTHTYPE_FT;
4612 else
4613 goto nla_put_failure;
4614 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
4615 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
4616 if (params->local_state_change) {
4617 wpa_printf(MSG_DEBUG, " * Local state change only");
4618 NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE);
4619 }
4620
4621 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4622 msg = NULL;
4623 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004624 wpa_dbg(drv->ctx, MSG_DEBUG,
4625 "nl80211: MLME command failed (auth): ret=%d (%s)",
4626 ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004627 count++;
4628 if (ret == -EALREADY && count == 1 && params->bssid &&
4629 !params->local_state_change) {
4630 /*
4631 * mac80211 does not currently accept new
4632 * authentication if we are already authenticated. As a
4633 * workaround, force deauthentication and try again.
4634 */
4635 wpa_printf(MSG_DEBUG, "nl80211: Retry authentication "
4636 "after forced deauthentication");
4637 wpa_driver_nl80211_deauthenticate(
4638 bss, params->bssid,
4639 WLAN_REASON_PREV_AUTH_NOT_VALID);
4640 nlmsg_free(msg);
4641 goto retry;
4642 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004643
4644 if (ret == -ENOENT && params->freq && !is_retry) {
4645 /*
4646 * cfg80211 has likely expired the BSS entry even
4647 * though it was previously available in our internal
4648 * BSS table. To recover quickly, start a single
4649 * channel scan on the specified channel.
4650 */
4651 struct wpa_driver_scan_params scan;
4652 int freqs[2];
4653
4654 os_memset(&scan, 0, sizeof(scan));
4655 scan.num_ssids = 1;
4656 if (params->ssid) {
4657 scan.ssids[0].ssid = params->ssid;
4658 scan.ssids[0].ssid_len = params->ssid_len;
4659 }
4660 freqs[0] = params->freq;
4661 freqs[1] = 0;
4662 scan.freqs = freqs;
4663 wpa_printf(MSG_DEBUG, "nl80211: Trigger single "
4664 "channel scan to refresh cfg80211 BSS "
4665 "entry");
4666 ret = wpa_driver_nl80211_scan(bss, &scan);
4667 if (ret == 0) {
4668 nl80211_copy_auth_params(drv, params);
4669 drv->scan_for_auth = 1;
4670 }
4671 } else if (is_retry) {
4672 /*
4673 * Need to indicate this with an event since the return
4674 * value from the retry is not delivered to core code.
4675 */
4676 union wpa_event_data event;
4677 wpa_printf(MSG_DEBUG, "nl80211: Authentication retry "
4678 "failed");
4679 os_memset(&event, 0, sizeof(event));
4680 os_memcpy(event.timeout_event.addr, drv->auth_bssid_,
4681 ETH_ALEN);
4682 wpa_supplicant_event(drv->ctx, EVENT_AUTH_TIMED_OUT,
4683 &event);
4684 }
4685
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004686 goto nla_put_failure;
4687 }
4688 ret = 0;
4689 wpa_printf(MSG_DEBUG, "nl80211: Authentication request send "
4690 "successfully");
4691
4692nla_put_failure:
4693 nlmsg_free(msg);
4694 return ret;
4695}
4696
4697
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004698static int wpa_driver_nl80211_authenticate_retry(
4699 struct wpa_driver_nl80211_data *drv)
4700{
4701 struct wpa_driver_auth_params params;
4702 struct i802_bss *bss = &drv->first_bss;
4703 int i;
4704
4705 wpa_printf(MSG_DEBUG, "nl80211: Try to authenticate again");
4706
4707 os_memset(&params, 0, sizeof(params));
4708 params.freq = drv->auth_freq;
4709 params.auth_alg = drv->auth_alg;
4710 params.wep_tx_keyidx = drv->auth_wep_tx_keyidx;
4711 params.local_state_change = drv->auth_local_state_change;
4712 params.p2p = drv->auth_p2p;
4713
4714 if (!is_zero_ether_addr(drv->auth_bssid_))
4715 params.bssid = drv->auth_bssid_;
4716
4717 if (drv->auth_ssid_len) {
4718 params.ssid = drv->auth_ssid;
4719 params.ssid_len = drv->auth_ssid_len;
4720 }
4721
4722 params.ie = drv->auth_ie;
4723 params.ie_len = drv->auth_ie_len;
4724
4725 for (i = 0; i < 4; i++) {
4726 if (drv->auth_wep_key_len[i]) {
4727 params.wep_key[i] = drv->auth_wep_key[i];
4728 params.wep_key_len[i] = drv->auth_wep_key_len[i];
4729 }
4730 }
4731
4732 drv->retry_auth = 1;
4733 return wpa_driver_nl80211_authenticate(bss, &params);
4734}
4735
4736
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004737struct phy_info_arg {
4738 u16 *num_modes;
4739 struct hostapd_hw_modes *modes;
4740};
4741
4742static int phy_info_handler(struct nl_msg *msg, void *arg)
4743{
4744 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
4745 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
4746 struct phy_info_arg *phy_info = arg;
4747
4748 struct nlattr *tb_band[NL80211_BAND_ATTR_MAX + 1];
4749
4750 struct nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1];
4751 static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
4752 [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
4753 [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
4754 [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] = { .type = NLA_FLAG },
4755 [NL80211_FREQUENCY_ATTR_NO_IBSS] = { .type = NLA_FLAG },
4756 [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
4757 [NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32 },
4758 };
4759
4760 struct nlattr *tb_rate[NL80211_BITRATE_ATTR_MAX + 1];
4761 static struct nla_policy rate_policy[NL80211_BITRATE_ATTR_MAX + 1] = {
4762 [NL80211_BITRATE_ATTR_RATE] = { .type = NLA_U32 },
4763 [NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE] = { .type = NLA_FLAG },
4764 };
4765
4766 struct nlattr *nl_band;
4767 struct nlattr *nl_freq;
4768 struct nlattr *nl_rate;
4769 int rem_band, rem_freq, rem_rate;
4770 struct hostapd_hw_modes *mode;
4771 int idx, mode_is_set;
4772
4773 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
4774 genlmsg_attrlen(gnlh, 0), NULL);
4775
4776 if (!tb_msg[NL80211_ATTR_WIPHY_BANDS])
4777 return NL_SKIP;
4778
4779 nla_for_each_nested(nl_band, tb_msg[NL80211_ATTR_WIPHY_BANDS], rem_band) {
4780 mode = os_realloc(phy_info->modes, (*phy_info->num_modes + 1) * sizeof(*mode));
4781 if (!mode)
4782 return NL_SKIP;
4783 phy_info->modes = mode;
4784
4785 mode_is_set = 0;
4786
4787 mode = &phy_info->modes[*(phy_info->num_modes)];
4788 memset(mode, 0, sizeof(*mode));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004789 mode->flags = HOSTAPD_MODE_FLAG_HT_INFO_KNOWN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004790 *(phy_info->num_modes) += 1;
4791
4792 nla_parse(tb_band, NL80211_BAND_ATTR_MAX, nla_data(nl_band),
4793 nla_len(nl_band), NULL);
4794
4795 if (tb_band[NL80211_BAND_ATTR_HT_CAPA]) {
4796 mode->ht_capab = nla_get_u16(
4797 tb_band[NL80211_BAND_ATTR_HT_CAPA]);
4798 }
4799
4800 if (tb_band[NL80211_BAND_ATTR_HT_AMPDU_FACTOR]) {
4801 mode->a_mpdu_params |= nla_get_u8(
4802 tb_band[NL80211_BAND_ATTR_HT_AMPDU_FACTOR]) &
4803 0x03;
4804 }
4805
4806 if (tb_band[NL80211_BAND_ATTR_HT_AMPDU_DENSITY]) {
4807 mode->a_mpdu_params |= nla_get_u8(
4808 tb_band[NL80211_BAND_ATTR_HT_AMPDU_DENSITY]) <<
4809 2;
4810 }
4811
4812 if (tb_band[NL80211_BAND_ATTR_HT_MCS_SET] &&
4813 nla_len(tb_band[NL80211_BAND_ATTR_HT_MCS_SET])) {
4814 u8 *mcs;
4815 mcs = nla_data(tb_band[NL80211_BAND_ATTR_HT_MCS_SET]);
4816 os_memcpy(mode->mcs_set, mcs, 16);
4817 }
4818
Dmitry Shmidt04949592012-07-19 12:16:46 -07004819 if (tb_band[NL80211_BAND_ATTR_VHT_CAPA]) {
4820 mode->vht_capab = nla_get_u32(
4821 tb_band[NL80211_BAND_ATTR_VHT_CAPA]);
4822 }
4823
4824 if (tb_band[NL80211_BAND_ATTR_VHT_MCS_SET] &&
4825 nla_len(tb_band[NL80211_BAND_ATTR_VHT_MCS_SET])) {
4826 u8 *mcs;
4827 mcs = nla_data(tb_band[NL80211_BAND_ATTR_VHT_MCS_SET]);
4828 os_memcpy(mode->vht_mcs_set, mcs, 8);
4829 }
4830
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004831 nla_for_each_nested(nl_freq, tb_band[NL80211_BAND_ATTR_FREQS], rem_freq) {
4832 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX, nla_data(nl_freq),
4833 nla_len(nl_freq), freq_policy);
4834 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
4835 continue;
4836 mode->num_channels++;
4837 }
4838
4839 mode->channels = os_zalloc(mode->num_channels * sizeof(struct hostapd_channel_data));
4840 if (!mode->channels)
4841 return NL_SKIP;
4842
4843 idx = 0;
4844
4845 nla_for_each_nested(nl_freq, tb_band[NL80211_BAND_ATTR_FREQS], rem_freq) {
4846 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX, nla_data(nl_freq),
4847 nla_len(nl_freq), freq_policy);
4848 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
4849 continue;
4850
4851 mode->channels[idx].freq = nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_FREQ]);
4852 mode->channels[idx].flag = 0;
4853
4854 if (!mode_is_set) {
4855 /* crude heuristic */
4856 if (mode->channels[idx].freq < 4000)
4857 mode->mode = HOSTAPD_MODE_IEEE80211B;
4858 else
4859 mode->mode = HOSTAPD_MODE_IEEE80211A;
4860 mode_is_set = 1;
4861 }
4862
4863 /* crude heuristic */
4864 if (mode->channels[idx].freq < 4000)
4865 if (mode->channels[idx].freq == 2484)
4866 mode->channels[idx].chan = 14;
4867 else
4868 mode->channels[idx].chan = (mode->channels[idx].freq - 2407) / 5;
4869 else
4870 mode->channels[idx].chan = mode->channels[idx].freq/5 - 1000;
4871
4872 if (tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
4873 mode->channels[idx].flag |=
4874 HOSTAPD_CHAN_DISABLED;
4875 if (tb_freq[NL80211_FREQUENCY_ATTR_PASSIVE_SCAN])
4876 mode->channels[idx].flag |=
4877 HOSTAPD_CHAN_PASSIVE_SCAN;
4878 if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IBSS])
4879 mode->channels[idx].flag |=
4880 HOSTAPD_CHAN_NO_IBSS;
4881 if (tb_freq[NL80211_FREQUENCY_ATTR_RADAR])
4882 mode->channels[idx].flag |=
4883 HOSTAPD_CHAN_RADAR;
4884
4885 if (tb_freq[NL80211_FREQUENCY_ATTR_MAX_TX_POWER] &&
4886 !tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
4887 mode->channels[idx].max_tx_power =
4888 nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_MAX_TX_POWER]) / 100;
4889
4890 idx++;
4891 }
4892
4893 nla_for_each_nested(nl_rate, tb_band[NL80211_BAND_ATTR_RATES], rem_rate) {
4894 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX, nla_data(nl_rate),
4895 nla_len(nl_rate), rate_policy);
4896 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
4897 continue;
4898 mode->num_rates++;
4899 }
4900
4901 mode->rates = os_zalloc(mode->num_rates * sizeof(int));
4902 if (!mode->rates)
4903 return NL_SKIP;
4904
4905 idx = 0;
4906
4907 nla_for_each_nested(nl_rate, tb_band[NL80211_BAND_ATTR_RATES], rem_rate) {
4908 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX, nla_data(nl_rate),
4909 nla_len(nl_rate), rate_policy);
4910 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
4911 continue;
4912 mode->rates[idx] = nla_get_u32(tb_rate[NL80211_BITRATE_ATTR_RATE]);
4913
4914 /* crude heuristic */
4915 if (mode->mode == HOSTAPD_MODE_IEEE80211B &&
4916 mode->rates[idx] > 200)
4917 mode->mode = HOSTAPD_MODE_IEEE80211G;
4918
4919 idx++;
4920 }
4921 }
4922
4923 return NL_SKIP;
4924}
4925
4926static struct hostapd_hw_modes *
4927wpa_driver_nl80211_add_11b(struct hostapd_hw_modes *modes, u16 *num_modes)
4928{
4929 u16 m;
4930 struct hostapd_hw_modes *mode11g = NULL, *nmodes, *mode;
4931 int i, mode11g_idx = -1;
4932
4933 /* If only 802.11g mode is included, use it to construct matching
4934 * 802.11b mode data. */
4935
4936 for (m = 0; m < *num_modes; m++) {
4937 if (modes[m].mode == HOSTAPD_MODE_IEEE80211B)
4938 return modes; /* 802.11b already included */
4939 if (modes[m].mode == HOSTAPD_MODE_IEEE80211G)
4940 mode11g_idx = m;
4941 }
4942
4943 if (mode11g_idx < 0)
4944 return modes; /* 2.4 GHz band not supported at all */
4945
4946 nmodes = os_realloc(modes, (*num_modes + 1) * sizeof(*nmodes));
4947 if (nmodes == NULL)
4948 return modes; /* Could not add 802.11b mode */
4949
4950 mode = &nmodes[*num_modes];
4951 os_memset(mode, 0, sizeof(*mode));
4952 (*num_modes)++;
4953 modes = nmodes;
4954
4955 mode->mode = HOSTAPD_MODE_IEEE80211B;
4956
4957 mode11g = &modes[mode11g_idx];
4958 mode->num_channels = mode11g->num_channels;
4959 mode->channels = os_malloc(mode11g->num_channels *
4960 sizeof(struct hostapd_channel_data));
4961 if (mode->channels == NULL) {
4962 (*num_modes)--;
4963 return modes; /* Could not add 802.11b mode */
4964 }
4965 os_memcpy(mode->channels, mode11g->channels,
4966 mode11g->num_channels * sizeof(struct hostapd_channel_data));
4967
4968 mode->num_rates = 0;
4969 mode->rates = os_malloc(4 * sizeof(int));
4970 if (mode->rates == NULL) {
4971 os_free(mode->channels);
4972 (*num_modes)--;
4973 return modes; /* Could not add 802.11b mode */
4974 }
4975
4976 for (i = 0; i < mode11g->num_rates; i++) {
4977 if (mode11g->rates[i] != 10 && mode11g->rates[i] != 20 &&
4978 mode11g->rates[i] != 55 && mode11g->rates[i] != 110)
4979 continue;
4980 mode->rates[mode->num_rates] = mode11g->rates[i];
4981 mode->num_rates++;
4982 if (mode->num_rates == 4)
4983 break;
4984 }
4985
4986 if (mode->num_rates == 0) {
4987 os_free(mode->channels);
4988 os_free(mode->rates);
4989 (*num_modes)--;
4990 return modes; /* No 802.11b rates */
4991 }
4992
4993 wpa_printf(MSG_DEBUG, "nl80211: Added 802.11b mode based on 802.11g "
4994 "information");
4995
4996 return modes;
4997}
4998
4999
5000static void nl80211_set_ht40_mode(struct hostapd_hw_modes *mode, int start,
5001 int end)
5002{
5003 int c;
5004
5005 for (c = 0; c < mode->num_channels; c++) {
5006 struct hostapd_channel_data *chan = &mode->channels[c];
5007 if (chan->freq - 10 >= start && chan->freq + 10 <= end)
5008 chan->flag |= HOSTAPD_CHAN_HT40;
5009 }
5010}
5011
5012
5013static void nl80211_set_ht40_mode_sec(struct hostapd_hw_modes *mode, int start,
5014 int end)
5015{
5016 int c;
5017
5018 for (c = 0; c < mode->num_channels; c++) {
5019 struct hostapd_channel_data *chan = &mode->channels[c];
5020 if (!(chan->flag & HOSTAPD_CHAN_HT40))
5021 continue;
5022 if (chan->freq - 30 >= start && chan->freq - 10 <= end)
5023 chan->flag |= HOSTAPD_CHAN_HT40MINUS;
5024 if (chan->freq + 10 >= start && chan->freq + 30 <= end)
5025 chan->flag |= HOSTAPD_CHAN_HT40PLUS;
5026 }
5027}
5028
5029
5030static void nl80211_reg_rule_ht40(struct nlattr *tb[],
5031 struct phy_info_arg *results)
5032{
5033 u32 start, end, max_bw;
5034 u16 m;
5035
5036 if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
5037 tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
5038 tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
5039 return;
5040
5041 start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
5042 end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
5043 max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
5044
5045 wpa_printf(MSG_DEBUG, "nl80211: %u-%u @ %u MHz",
5046 start, end, max_bw);
5047 if (max_bw < 40)
5048 return;
5049
5050 for (m = 0; m < *results->num_modes; m++) {
5051 if (!(results->modes[m].ht_capab &
5052 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
5053 continue;
5054 nl80211_set_ht40_mode(&results->modes[m], start, end);
5055 }
5056}
5057
5058
5059static void nl80211_reg_rule_sec(struct nlattr *tb[],
5060 struct phy_info_arg *results)
5061{
5062 u32 start, end, max_bw;
5063 u16 m;
5064
5065 if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
5066 tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
5067 tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
5068 return;
5069
5070 start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
5071 end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
5072 max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
5073
5074 if (max_bw < 20)
5075 return;
5076
5077 for (m = 0; m < *results->num_modes; m++) {
5078 if (!(results->modes[m].ht_capab &
5079 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
5080 continue;
5081 nl80211_set_ht40_mode_sec(&results->modes[m], start, end);
5082 }
5083}
5084
5085
5086static int nl80211_get_reg(struct nl_msg *msg, void *arg)
5087{
5088 struct phy_info_arg *results = arg;
5089 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
5090 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
5091 struct nlattr *nl_rule;
5092 struct nlattr *tb_rule[NL80211_FREQUENCY_ATTR_MAX + 1];
5093 int rem_rule;
5094 static struct nla_policy reg_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
5095 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
5096 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
5097 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
5098 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
5099 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
5100 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
5101 };
5102
5103 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
5104 genlmsg_attrlen(gnlh, 0), NULL);
5105 if (!tb_msg[NL80211_ATTR_REG_ALPHA2] ||
5106 !tb_msg[NL80211_ATTR_REG_RULES]) {
5107 wpa_printf(MSG_DEBUG, "nl80211: No regulatory information "
5108 "available");
5109 return NL_SKIP;
5110 }
5111
5112 wpa_printf(MSG_DEBUG, "nl80211: Regulatory information - country=%s",
5113 (char *) nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]));
5114
5115 nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
5116 {
5117 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
5118 nla_data(nl_rule), nla_len(nl_rule), reg_policy);
5119 nl80211_reg_rule_ht40(tb_rule, results);
5120 }
5121
5122 nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
5123 {
5124 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
5125 nla_data(nl_rule), nla_len(nl_rule), reg_policy);
5126 nl80211_reg_rule_sec(tb_rule, results);
5127 }
5128
5129 return NL_SKIP;
5130}
5131
5132
5133static int nl80211_set_ht40_flags(struct wpa_driver_nl80211_data *drv,
5134 struct phy_info_arg *results)
5135{
5136 struct nl_msg *msg;
5137
5138 msg = nlmsg_alloc();
5139 if (!msg)
5140 return -ENOMEM;
5141
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005142 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005143 return send_and_recv_msgs(drv, msg, nl80211_get_reg, results);
5144}
5145
5146
5147static struct hostapd_hw_modes *
5148wpa_driver_nl80211_get_hw_feature_data(void *priv, u16 *num_modes, u16 *flags)
5149{
5150 struct i802_bss *bss = priv;
5151 struct wpa_driver_nl80211_data *drv = bss->drv;
5152 struct nl_msg *msg;
5153 struct phy_info_arg result = {
5154 .num_modes = num_modes,
5155 .modes = NULL,
5156 };
5157
5158 *num_modes = 0;
5159 *flags = 0;
5160
5161 msg = nlmsg_alloc();
5162 if (!msg)
5163 return NULL;
5164
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005165 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005166
5167 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
5168
5169 if (send_and_recv_msgs(drv, msg, phy_info_handler, &result) == 0) {
5170 nl80211_set_ht40_flags(drv, &result);
5171 return wpa_driver_nl80211_add_11b(result.modes, num_modes);
5172 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005173 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005174 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005175 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005176 return NULL;
5177}
5178
5179
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005180static int wpa_driver_nl80211_send_mntr(struct wpa_driver_nl80211_data *drv,
5181 const void *data, size_t len,
5182 int encrypt, int noack)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005183{
5184 __u8 rtap_hdr[] = {
5185 0x00, 0x00, /* radiotap version */
5186 0x0e, 0x00, /* radiotap length */
5187 0x02, 0xc0, 0x00, 0x00, /* bmap: flags, tx and rx flags */
5188 IEEE80211_RADIOTAP_F_FRAG, /* F_FRAG (fragment if required) */
5189 0x00, /* padding */
5190 0x00, 0x00, /* RX and TX flags to indicate that */
5191 0x00, 0x00, /* this is the injected frame directly */
5192 };
5193 struct iovec iov[2] = {
5194 {
5195 .iov_base = &rtap_hdr,
5196 .iov_len = sizeof(rtap_hdr),
5197 },
5198 {
5199 .iov_base = (void *) data,
5200 .iov_len = len,
5201 }
5202 };
5203 struct msghdr msg = {
5204 .msg_name = NULL,
5205 .msg_namelen = 0,
5206 .msg_iov = iov,
5207 .msg_iovlen = 2,
5208 .msg_control = NULL,
5209 .msg_controllen = 0,
5210 .msg_flags = 0,
5211 };
5212 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005213 u16 txflags = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005214
5215 if (encrypt)
5216 rtap_hdr[8] |= IEEE80211_RADIOTAP_F_WEP;
5217
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07005218 if (drv->monitor_sock < 0) {
5219 wpa_printf(MSG_DEBUG, "nl80211: No monitor socket available "
5220 "for %s", __func__);
5221 return -1;
5222 }
5223
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005224 if (noack)
5225 txflags |= IEEE80211_RADIOTAP_F_TX_NOACK;
5226 *(le16 *) &rtap_hdr[12] = host_to_le16(txflags);
5227
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005228 res = sendmsg(drv->monitor_sock, &msg, 0);
5229 if (res < 0) {
5230 wpa_printf(MSG_INFO, "nl80211: sendmsg: %s", strerror(errno));
5231 return -1;
5232 }
5233 return 0;
5234}
5235
5236
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005237static int wpa_driver_nl80211_send_frame(struct i802_bss *bss,
5238 const void *data, size_t len,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005239 int encrypt, int noack,
5240 unsigned int freq, int no_cck,
5241 int offchanok, unsigned int wait_time)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005242{
5243 struct wpa_driver_nl80211_data *drv = bss->drv;
5244 u64 cookie;
5245
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005246 if (freq == 0)
5247 freq = bss->freq;
5248
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005249 if (drv->use_monitor)
5250 return wpa_driver_nl80211_send_mntr(drv, data, len,
5251 encrypt, noack);
5252
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005253 return nl80211_send_frame_cmd(bss, freq, wait_time, data, len,
5254 &cookie, no_cck, noack, offchanok);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005255}
5256
5257
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005258static int wpa_driver_nl80211_send_mlme_freq(struct i802_bss *bss,
5259 const u8 *data,
5260 size_t data_len, int noack,
5261 unsigned int freq, int no_cck,
5262 int offchanok,
5263 unsigned int wait_time)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005264{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005265 struct wpa_driver_nl80211_data *drv = bss->drv;
5266 struct ieee80211_mgmt *mgmt;
5267 int encrypt = 1;
5268 u16 fc;
5269
5270 mgmt = (struct ieee80211_mgmt *) data;
5271 fc = le_to_host16(mgmt->frame_control);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005272
5273 if (is_sta_interface(drv->nlmode) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005274 WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
5275 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_RESP) {
5276 /*
5277 * The use of last_mgmt_freq is a bit of a hack,
5278 * but it works due to the single-threaded nature
5279 * of wpa_supplicant.
5280 */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005281 if (freq == 0)
5282 freq = drv->last_mgmt_freq;
5283 return nl80211_send_frame_cmd(bss, freq, 0,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005284 data, data_len, NULL, 1, noack,
5285 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005286 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005287
5288 if (drv->device_ap_sme && is_ap_interface(drv->nlmode)) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005289 if (freq == 0)
5290 freq = bss->freq;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005291 return nl80211_send_frame_cmd(bss, freq,
5292 (int) freq == bss->freq ? 0 :
5293 wait_time,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005294 data, data_len,
5295 &drv->send_action_cookie,
5296 no_cck, noack, offchanok);
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07005297 }
Dmitry Shmidtb638fe72012-03-20 12:51:25 -07005298
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005299 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
5300 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) {
5301 /*
5302 * Only one of the authentication frame types is encrypted.
5303 * In order for static WEP encryption to work properly (i.e.,
5304 * to not encrypt the frame), we need to tell mac80211 about
5305 * the frames that must not be encrypted.
5306 */
5307 u16 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
5308 u16 auth_trans = le_to_host16(mgmt->u.auth.auth_transaction);
5309 if (auth_alg != WLAN_AUTH_SHARED_KEY || auth_trans != 3)
5310 encrypt = 0;
5311 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005312
5313 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005314 noack, freq, no_cck, offchanok,
5315 wait_time);
5316}
5317
5318
5319static int wpa_driver_nl80211_send_mlme(void *priv, const u8 *data,
5320 size_t data_len, int noack)
5321{
5322 struct i802_bss *bss = priv;
5323 return wpa_driver_nl80211_send_mlme_freq(bss, data, data_len, noack,
5324 0, 0, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005325}
5326
5327
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005328static int nl80211_set_bss(struct i802_bss *bss, int cts, int preamble,
5329 int slot, int ht_opmode, int ap_isolate,
5330 int *basic_rates)
5331{
5332 struct wpa_driver_nl80211_data *drv = bss->drv;
5333 struct nl_msg *msg;
5334
5335 msg = nlmsg_alloc();
5336 if (!msg)
5337 return -ENOMEM;
5338
5339 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_BSS);
5340
5341 if (cts >= 0)
5342 NLA_PUT_U8(msg, NL80211_ATTR_BSS_CTS_PROT, cts);
5343 if (preamble >= 0)
5344 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble);
5345 if (slot >= 0)
5346 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot);
5347 if (ht_opmode >= 0)
5348 NLA_PUT_U16(msg, NL80211_ATTR_BSS_HT_OPMODE, ht_opmode);
5349 if (ap_isolate >= 0)
5350 NLA_PUT_U8(msg, NL80211_ATTR_AP_ISOLATE, ap_isolate);
5351
5352 if (basic_rates) {
5353 u8 rates[NL80211_MAX_SUPP_RATES];
5354 u8 rates_len = 0;
5355 int i;
5356
5357 for (i = 0; i < NL80211_MAX_SUPP_RATES && basic_rates[i] >= 0;
5358 i++)
5359 rates[rates_len++] = basic_rates[i] / 5;
5360
5361 NLA_PUT(msg, NL80211_ATTR_BSS_BASIC_RATES, rates_len, rates);
5362 }
5363
5364 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
5365
5366 return send_and_recv_msgs(drv, msg, NULL, NULL);
5367 nla_put_failure:
5368 nlmsg_free(msg);
5369 return -ENOBUFS;
5370}
5371
5372
5373static int wpa_driver_nl80211_set_ap(void *priv,
5374 struct wpa_driver_ap_params *params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005375{
5376 struct i802_bss *bss = priv;
5377 struct wpa_driver_nl80211_data *drv = bss->drv;
5378 struct nl_msg *msg;
5379 u8 cmd = NL80211_CMD_NEW_BEACON;
5380 int ret;
5381 int beacon_set;
5382 int ifindex = if_nametoindex(bss->ifname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005383 int num_suites;
5384 u32 suites[10];
5385 u32 ver;
5386
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07005387 beacon_set = bss->beacon_set;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005388
5389 msg = nlmsg_alloc();
5390 if (!msg)
5391 return -ENOMEM;
5392
5393 wpa_printf(MSG_DEBUG, "nl80211: Set beacon (beacon_set=%d)",
5394 beacon_set);
5395 if (beacon_set)
5396 cmd = NL80211_CMD_SET_BEACON;
5397
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005398 nl80211_cmd(drv, msg, 0, cmd);
5399 NLA_PUT(msg, NL80211_ATTR_BEACON_HEAD, params->head_len, params->head);
5400 NLA_PUT(msg, NL80211_ATTR_BEACON_TAIL, params->tail_len, params->tail);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005401 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005402 NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL, params->beacon_int);
5403 NLA_PUT_U32(msg, NL80211_ATTR_DTIM_PERIOD, params->dtim_period);
5404 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
5405 params->ssid);
5406 if (params->proberesp && params->proberesp_len)
5407 NLA_PUT(msg, NL80211_ATTR_PROBE_RESP, params->proberesp_len,
5408 params->proberesp);
5409 switch (params->hide_ssid) {
5410 case NO_SSID_HIDING:
5411 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
5412 NL80211_HIDDEN_SSID_NOT_IN_USE);
5413 break;
5414 case HIDDEN_SSID_ZERO_LEN:
5415 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
5416 NL80211_HIDDEN_SSID_ZERO_LEN);
5417 break;
5418 case HIDDEN_SSID_ZERO_CONTENTS:
5419 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
5420 NL80211_HIDDEN_SSID_ZERO_CONTENTS);
5421 break;
5422 }
5423 if (params->privacy)
5424 NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
5425 if ((params->auth_algs & (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) ==
5426 (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) {
5427 /* Leave out the attribute */
5428 } else if (params->auth_algs & WPA_AUTH_ALG_SHARED)
5429 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE,
5430 NL80211_AUTHTYPE_SHARED_KEY);
5431 else
5432 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE,
5433 NL80211_AUTHTYPE_OPEN_SYSTEM);
5434
5435 ver = 0;
5436 if (params->wpa_version & WPA_PROTO_WPA)
5437 ver |= NL80211_WPA_VERSION_1;
5438 if (params->wpa_version & WPA_PROTO_RSN)
5439 ver |= NL80211_WPA_VERSION_2;
5440 if (ver)
5441 NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver);
5442
5443 num_suites = 0;
5444 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X)
5445 suites[num_suites++] = WLAN_AKM_SUITE_8021X;
5446 if (params->key_mgmt_suites & WPA_KEY_MGMT_PSK)
5447 suites[num_suites++] = WLAN_AKM_SUITE_PSK;
5448 if (num_suites) {
5449 NLA_PUT(msg, NL80211_ATTR_AKM_SUITES,
5450 num_suites * sizeof(u32), suites);
5451 }
5452
5453 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X &&
5454 params->pairwise_ciphers & (WPA_CIPHER_WEP104 | WPA_CIPHER_WEP40))
5455 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT);
5456
5457 num_suites = 0;
5458 if (params->pairwise_ciphers & WPA_CIPHER_CCMP)
5459 suites[num_suites++] = WLAN_CIPHER_SUITE_CCMP;
5460 if (params->pairwise_ciphers & WPA_CIPHER_TKIP)
5461 suites[num_suites++] = WLAN_CIPHER_SUITE_TKIP;
5462 if (params->pairwise_ciphers & WPA_CIPHER_WEP104)
5463 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP104;
5464 if (params->pairwise_ciphers & WPA_CIPHER_WEP40)
5465 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP40;
5466 if (num_suites) {
5467 NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
5468 num_suites * sizeof(u32), suites);
5469 }
5470
5471 switch (params->group_cipher) {
5472 case WPA_CIPHER_CCMP:
5473 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
5474 WLAN_CIPHER_SUITE_CCMP);
5475 break;
5476 case WPA_CIPHER_TKIP:
5477 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
5478 WLAN_CIPHER_SUITE_TKIP);
5479 break;
5480 case WPA_CIPHER_WEP104:
5481 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
5482 WLAN_CIPHER_SUITE_WEP104);
5483 break;
5484 case WPA_CIPHER_WEP40:
5485 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
5486 WLAN_CIPHER_SUITE_WEP40);
5487 break;
5488 }
5489
5490 if (params->beacon_ies) {
5491 NLA_PUT(msg, NL80211_ATTR_IE, wpabuf_len(params->beacon_ies),
5492 wpabuf_head(params->beacon_ies));
5493 }
5494 if (params->proberesp_ies) {
5495 NLA_PUT(msg, NL80211_ATTR_IE_PROBE_RESP,
5496 wpabuf_len(params->proberesp_ies),
5497 wpabuf_head(params->proberesp_ies));
5498 }
5499 if (params->assocresp_ies) {
5500 NLA_PUT(msg, NL80211_ATTR_IE_ASSOC_RESP,
5501 wpabuf_len(params->assocresp_ies),
5502 wpabuf_head(params->assocresp_ies));
5503 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005504
Dmitry Shmidt04949592012-07-19 12:16:46 -07005505 if (drv->capa.flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER) {
5506 NLA_PUT_U16(msg, NL80211_ATTR_INACTIVITY_TIMEOUT,
5507 params->ap_max_inactivity);
5508 }
5509
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005510 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5511 if (ret) {
5512 wpa_printf(MSG_DEBUG, "nl80211: Beacon set failed: %d (%s)",
5513 ret, strerror(-ret));
5514 } else {
5515 bss->beacon_set = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005516 nl80211_set_bss(bss, params->cts_protect, params->preamble,
5517 params->short_slot_time, params->ht_opmode,
5518 params->isolate, params->basic_rates);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005519 }
5520 return ret;
5521 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005522 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005523 return -ENOBUFS;
5524}
5525
5526
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005527static int wpa_driver_nl80211_set_freq(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005528 int freq, int ht_enabled,
5529 int sec_channel_offset)
5530{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005531 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005532 struct nl_msg *msg;
5533 int ret;
5534
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005535 wpa_printf(MSG_DEBUG, "nl80211: Set freq %d (ht_enabled=%d "
5536 "sec_channel_offset=%d)",
5537 freq, ht_enabled, sec_channel_offset);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005538 msg = nlmsg_alloc();
5539 if (!msg)
5540 return -1;
5541
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005542 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005543
5544 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
5545 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
5546 if (ht_enabled) {
5547 switch (sec_channel_offset) {
5548 case -1:
5549 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
5550 NL80211_CHAN_HT40MINUS);
5551 break;
5552 case 1:
5553 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
5554 NL80211_CHAN_HT40PLUS);
5555 break;
5556 default:
5557 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
5558 NL80211_CHAN_HT20);
5559 break;
5560 }
5561 }
5562
5563 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005564 msg = NULL;
5565 if (ret == 0) {
5566 bss->freq = freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005567 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005568 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005569 wpa_printf(MSG_DEBUG, "nl80211: Failed to set channel (freq=%d): "
5570 "%d (%s)", freq, ret, strerror(-ret));
5571nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005572 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005573 return -1;
5574}
5575
5576
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005577static u32 sta_flags_nl80211(int flags)
5578{
5579 u32 f = 0;
5580
5581 if (flags & WPA_STA_AUTHORIZED)
5582 f |= BIT(NL80211_STA_FLAG_AUTHORIZED);
5583 if (flags & WPA_STA_WMM)
5584 f |= BIT(NL80211_STA_FLAG_WME);
5585 if (flags & WPA_STA_SHORT_PREAMBLE)
5586 f |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
5587 if (flags & WPA_STA_MFP)
5588 f |= BIT(NL80211_STA_FLAG_MFP);
5589 if (flags & WPA_STA_TDLS_PEER)
5590 f |= BIT(NL80211_STA_FLAG_TDLS_PEER);
5591
5592 return f;
5593}
5594
5595
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005596static int wpa_driver_nl80211_sta_add(void *priv,
5597 struct hostapd_sta_add_params *params)
5598{
5599 struct i802_bss *bss = priv;
5600 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005601 struct nl_msg *msg, *wme = NULL;
5602 struct nl80211_sta_flag_update upd;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005603 int ret = -ENOBUFS;
5604
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005605 if ((params->flags & WPA_STA_TDLS_PEER) &&
5606 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
5607 return -EOPNOTSUPP;
5608
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005609 msg = nlmsg_alloc();
5610 if (!msg)
5611 return -ENOMEM;
5612
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005613 nl80211_cmd(drv, msg, 0, params->set ? NL80211_CMD_SET_STATION :
5614 NL80211_CMD_NEW_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005615
5616 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
5617 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005618 NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_RATES, params->supp_rates_len,
5619 params->supp_rates);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005620 if (!params->set) {
5621 NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, params->aid);
5622 NLA_PUT_U16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
5623 params->listen_interval);
5624 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005625 if (params->ht_capabilities) {
5626 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY,
5627 sizeof(*params->ht_capabilities),
5628 params->ht_capabilities);
5629 }
5630
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005631 os_memset(&upd, 0, sizeof(upd));
5632 upd.mask = sta_flags_nl80211(params->flags);
5633 upd.set = upd.mask;
5634 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
5635
5636 if (params->flags & WPA_STA_WMM) {
5637 wme = nlmsg_alloc();
5638 if (!wme)
5639 goto nla_put_failure;
5640
5641 NLA_PUT_U8(wme, NL80211_STA_WME_UAPSD_QUEUES,
5642 params->qosinfo & WMM_QOSINFO_STA_AC_MASK);
5643 NLA_PUT_U8(wme, NL80211_STA_WME_MAX_SP,
5644 (params->qosinfo > WMM_QOSINFO_STA_SP_SHIFT) &
5645 WMM_QOSINFO_STA_SP_MASK);
5646 nla_put_nested(msg, NL80211_ATTR_STA_WME, wme);
5647 }
5648
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005649 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005650 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005651 if (ret)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005652 wpa_printf(MSG_DEBUG, "nl80211: NL80211_CMD_%s_STATION "
5653 "result: %d (%s)", params->set ? "SET" : "NEW", ret,
5654 strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005655 if (ret == -EEXIST)
5656 ret = 0;
5657 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005658 nlmsg_free(wme);
5659 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005660 return ret;
5661}
5662
5663
5664static int wpa_driver_nl80211_sta_remove(void *priv, const u8 *addr)
5665{
5666 struct i802_bss *bss = priv;
5667 struct wpa_driver_nl80211_data *drv = bss->drv;
5668 struct nl_msg *msg;
5669 int ret;
5670
5671 msg = nlmsg_alloc();
5672 if (!msg)
5673 return -ENOMEM;
5674
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005675 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005676
5677 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
5678 if_nametoindex(bss->ifname));
5679 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
5680
5681 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5682 if (ret == -ENOENT)
5683 return 0;
5684 return ret;
5685 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005686 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005687 return -ENOBUFS;
5688}
5689
5690
5691static void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv,
5692 int ifidx)
5693{
5694 struct nl_msg *msg;
5695
5696 wpa_printf(MSG_DEBUG, "nl80211: Remove interface ifindex=%d", ifidx);
5697
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005698 /* stop listening for EAPOL on this interface */
5699 del_ifidx(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005700
5701 msg = nlmsg_alloc();
5702 if (!msg)
5703 goto nla_put_failure;
5704
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005705 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_INTERFACE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005706 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifidx);
5707
5708 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
5709 return;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005710 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005711 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005712 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005713 wpa_printf(MSG_ERROR, "Failed to remove interface (ifidx=%d)", ifidx);
5714}
5715
5716
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005717static const char * nl80211_iftype_str(enum nl80211_iftype mode)
5718{
5719 switch (mode) {
5720 case NL80211_IFTYPE_ADHOC:
5721 return "ADHOC";
5722 case NL80211_IFTYPE_STATION:
5723 return "STATION";
5724 case NL80211_IFTYPE_AP:
5725 return "AP";
5726 case NL80211_IFTYPE_MONITOR:
5727 return "MONITOR";
5728 case NL80211_IFTYPE_P2P_CLIENT:
5729 return "P2P_CLIENT";
5730 case NL80211_IFTYPE_P2P_GO:
5731 return "P2P_GO";
5732 default:
5733 return "unknown";
5734 }
5735}
5736
5737
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005738static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv,
5739 const char *ifname,
5740 enum nl80211_iftype iftype,
5741 const u8 *addr, int wds)
5742{
5743 struct nl_msg *msg, *flags = NULL;
5744 int ifidx;
5745 int ret = -ENOBUFS;
5746
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005747 wpa_printf(MSG_DEBUG, "nl80211: Create interface iftype %d (%s)",
5748 iftype, nl80211_iftype_str(iftype));
5749
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005750 msg = nlmsg_alloc();
5751 if (!msg)
5752 return -1;
5753
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005754 nl80211_cmd(drv, msg, 0, NL80211_CMD_NEW_INTERFACE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005755 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
5756 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, ifname);
5757 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, iftype);
5758
5759 if (iftype == NL80211_IFTYPE_MONITOR) {
5760 int err;
5761
5762 flags = nlmsg_alloc();
5763 if (!flags)
5764 goto nla_put_failure;
5765
5766 NLA_PUT_FLAG(flags, NL80211_MNTR_FLAG_COOK_FRAMES);
5767
5768 err = nla_put_nested(msg, NL80211_ATTR_MNTR_FLAGS, flags);
5769
5770 nlmsg_free(flags);
5771
5772 if (err)
5773 goto nla_put_failure;
5774 } else if (wds) {
5775 NLA_PUT_U8(msg, NL80211_ATTR_4ADDR, wds);
5776 }
5777
5778 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005779 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005780 if (ret) {
5781 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005782 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005783 wpa_printf(MSG_ERROR, "Failed to create interface %s: %d (%s)",
5784 ifname, ret, strerror(-ret));
5785 return ret;
5786 }
5787
5788 ifidx = if_nametoindex(ifname);
5789 wpa_printf(MSG_DEBUG, "nl80211: New interface %s created: ifindex=%d",
5790 ifname, ifidx);
5791
5792 if (ifidx <= 0)
5793 return -1;
5794
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005795 /* start listening for EAPOL on this interface */
5796 add_ifidx(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005797
5798 if (addr && iftype != NL80211_IFTYPE_MONITOR &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005799 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname, addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005800 nl80211_remove_iface(drv, ifidx);
5801 return -1;
5802 }
5803
5804 return ifidx;
5805}
5806
5807
5808static int nl80211_create_iface(struct wpa_driver_nl80211_data *drv,
5809 const char *ifname, enum nl80211_iftype iftype,
5810 const u8 *addr, int wds)
5811{
5812 int ret;
5813
5814 ret = nl80211_create_iface_once(drv, ifname, iftype, addr, wds);
5815
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005816 /* if error occurred and interface exists already */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005817 if (ret == -ENFILE && if_nametoindex(ifname)) {
5818 wpa_printf(MSG_INFO, "Try to remove and re-create %s", ifname);
5819
5820 /* Try to remove the interface that was already there. */
5821 nl80211_remove_iface(drv, if_nametoindex(ifname));
5822
5823 /* Try to create the interface again */
5824 ret = nl80211_create_iface_once(drv, ifname, iftype, addr,
5825 wds);
5826 }
5827
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005828 if (ret >= 0 && is_p2p_interface(iftype))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005829 nl80211_disable_11b_rates(drv, ret, 1);
5830
5831 return ret;
5832}
5833
5834
5835static void handle_tx_callback(void *ctx, u8 *buf, size_t len, int ok)
5836{
5837 struct ieee80211_hdr *hdr;
5838 u16 fc;
5839 union wpa_event_data event;
5840
5841 hdr = (struct ieee80211_hdr *) buf;
5842 fc = le_to_host16(hdr->frame_control);
5843
5844 os_memset(&event, 0, sizeof(event));
5845 event.tx_status.type = WLAN_FC_GET_TYPE(fc);
5846 event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
5847 event.tx_status.dst = hdr->addr1;
5848 event.tx_status.data = buf;
5849 event.tx_status.data_len = len;
5850 event.tx_status.ack = ok;
5851 wpa_supplicant_event(ctx, EVENT_TX_STATUS, &event);
5852}
5853
5854
5855static void from_unknown_sta(struct wpa_driver_nl80211_data *drv,
5856 u8 *buf, size_t len)
5857{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005858 struct ieee80211_hdr *hdr = (void *)buf;
5859 u16 fc;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005860 union wpa_event_data event;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005861
5862 if (len < sizeof(*hdr))
5863 return;
5864
5865 fc = le_to_host16(hdr->frame_control);
5866
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005867 os_memset(&event, 0, sizeof(event));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005868 event.rx_from_unknown.bssid = get_hdr_bssid(hdr, len);
5869 event.rx_from_unknown.addr = hdr->addr2;
5870 event.rx_from_unknown.wds = (fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) ==
5871 (WLAN_FC_FROMDS | WLAN_FC_TODS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005872 wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event);
5873}
5874
5875
5876static void handle_frame(struct wpa_driver_nl80211_data *drv,
5877 u8 *buf, size_t len, int datarate, int ssi_signal)
5878{
5879 struct ieee80211_hdr *hdr;
5880 u16 fc;
5881 union wpa_event_data event;
5882
5883 hdr = (struct ieee80211_hdr *) buf;
5884 fc = le_to_host16(hdr->frame_control);
5885
5886 switch (WLAN_FC_GET_TYPE(fc)) {
5887 case WLAN_FC_TYPE_MGMT:
5888 os_memset(&event, 0, sizeof(event));
5889 event.rx_mgmt.frame = buf;
5890 event.rx_mgmt.frame_len = len;
5891 event.rx_mgmt.datarate = datarate;
5892 event.rx_mgmt.ssi_signal = ssi_signal;
5893 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
5894 break;
5895 case WLAN_FC_TYPE_CTRL:
5896 /* can only get here with PS-Poll frames */
5897 wpa_printf(MSG_DEBUG, "CTRL");
5898 from_unknown_sta(drv, buf, len);
5899 break;
5900 case WLAN_FC_TYPE_DATA:
5901 from_unknown_sta(drv, buf, len);
5902 break;
5903 }
5904}
5905
5906
5907static void handle_monitor_read(int sock, void *eloop_ctx, void *sock_ctx)
5908{
5909 struct wpa_driver_nl80211_data *drv = eloop_ctx;
5910 int len;
5911 unsigned char buf[3000];
5912 struct ieee80211_radiotap_iterator iter;
5913 int ret;
5914 int datarate = 0, ssi_signal = 0;
5915 int injected = 0, failed = 0, rxflags = 0;
5916
5917 len = recv(sock, buf, sizeof(buf), 0);
5918 if (len < 0) {
5919 perror("recv");
5920 return;
5921 }
5922
5923 if (ieee80211_radiotap_iterator_init(&iter, (void*)buf, len)) {
5924 printf("received invalid radiotap frame\n");
5925 return;
5926 }
5927
5928 while (1) {
5929 ret = ieee80211_radiotap_iterator_next(&iter);
5930 if (ret == -ENOENT)
5931 break;
5932 if (ret) {
5933 printf("received invalid radiotap frame (%d)\n", ret);
5934 return;
5935 }
5936 switch (iter.this_arg_index) {
5937 case IEEE80211_RADIOTAP_FLAGS:
5938 if (*iter.this_arg & IEEE80211_RADIOTAP_F_FCS)
5939 len -= 4;
5940 break;
5941 case IEEE80211_RADIOTAP_RX_FLAGS:
5942 rxflags = 1;
5943 break;
5944 case IEEE80211_RADIOTAP_TX_FLAGS:
5945 injected = 1;
5946 failed = le_to_host16((*(uint16_t *) iter.this_arg)) &
5947 IEEE80211_RADIOTAP_F_TX_FAIL;
5948 break;
5949 case IEEE80211_RADIOTAP_DATA_RETRIES:
5950 break;
5951 case IEEE80211_RADIOTAP_CHANNEL:
5952 /* TODO: convert from freq/flags to channel number */
5953 break;
5954 case IEEE80211_RADIOTAP_RATE:
5955 datarate = *iter.this_arg * 5;
5956 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005957 case IEEE80211_RADIOTAP_DBM_ANTSIGNAL:
5958 ssi_signal = (s8) *iter.this_arg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005959 break;
5960 }
5961 }
5962
5963 if (rxflags && injected)
5964 return;
5965
5966 if (!injected)
5967 handle_frame(drv, buf + iter.max_length,
5968 len - iter.max_length, datarate, ssi_signal);
5969 else
5970 handle_tx_callback(drv->ctx, buf + iter.max_length,
5971 len - iter.max_length, !failed);
5972}
5973
5974
5975/*
5976 * we post-process the filter code later and rewrite
5977 * this to the offset to the last instruction
5978 */
5979#define PASS 0xFF
5980#define FAIL 0xFE
5981
5982static struct sock_filter msock_filter_insns[] = {
5983 /*
5984 * do a little-endian load of the radiotap length field
5985 */
5986 /* load lower byte into A */
5987 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 2),
5988 /* put it into X (== index register) */
5989 BPF_STMT(BPF_MISC| BPF_TAX, 0),
5990 /* load upper byte into A */
5991 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 3),
5992 /* left-shift it by 8 */
5993 BPF_STMT(BPF_ALU | BPF_LSH | BPF_K, 8),
5994 /* or with X */
5995 BPF_STMT(BPF_ALU | BPF_OR | BPF_X, 0),
5996 /* put result into X */
5997 BPF_STMT(BPF_MISC| BPF_TAX, 0),
5998
5999 /*
6000 * Allow management frames through, this also gives us those
6001 * management frames that we sent ourselves with status
6002 */
6003 /* load the lower byte of the IEEE 802.11 frame control field */
6004 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
6005 /* mask off frame type and version */
6006 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0xF),
6007 /* accept frame if it's both 0, fall through otherwise */
6008 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0, PASS, 0),
6009
6010 /*
6011 * TODO: add a bit to radiotap RX flags that indicates
6012 * that the sending station is not associated, then
6013 * add a filter here that filters on our DA and that flag
6014 * to allow us to deauth frames to that bad station.
6015 *
6016 * For now allow all To DS data frames through.
6017 */
6018 /* load the IEEE 802.11 frame control field */
6019 BPF_STMT(BPF_LD | BPF_H | BPF_IND, 0),
6020 /* mask off frame type, version and DS status */
6021 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0F03),
6022 /* accept frame if version 0, type 2 and To DS, fall through otherwise
6023 */
6024 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0801, PASS, 0),
6025
6026#if 0
6027 /*
6028 * drop non-data frames
6029 */
6030 /* load the lower byte of the frame control field */
6031 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
6032 /* mask off QoS bit */
6033 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0c),
6034 /* drop non-data frames */
6035 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 8, 0, FAIL),
6036#endif
6037 /* load the upper byte of the frame control field */
6038 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 1),
6039 /* mask off toDS/fromDS */
6040 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x03),
6041 /* accept WDS frames */
6042 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 3, PASS, 0),
6043
6044 /*
6045 * add header length to index
6046 */
6047 /* load the lower byte of the frame control field */
6048 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
6049 /* mask off QoS bit */
6050 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x80),
6051 /* right shift it by 6 to give 0 or 2 */
6052 BPF_STMT(BPF_ALU | BPF_RSH | BPF_K, 6),
6053 /* add data frame header length */
6054 BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 24),
6055 /* add index, was start of 802.11 header */
6056 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
6057 /* move to index, now start of LL header */
6058 BPF_STMT(BPF_MISC | BPF_TAX, 0),
6059
6060 /*
6061 * Accept empty data frames, we use those for
6062 * polling activity.
6063 */
6064 BPF_STMT(BPF_LD | BPF_W | BPF_LEN, 0),
6065 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_X, 0, PASS, 0),
6066
6067 /*
6068 * Accept EAPOL frames
6069 */
6070 BPF_STMT(BPF_LD | BPF_W | BPF_IND, 0),
6071 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0xAAAA0300, 0, FAIL),
6072 BPF_STMT(BPF_LD | BPF_W | BPF_IND, 4),
6073 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0000888E, PASS, FAIL),
6074
6075 /* keep these last two statements or change the code below */
6076 /* return 0 == "DROP" */
6077 BPF_STMT(BPF_RET | BPF_K, 0),
6078 /* return ~0 == "keep all" */
6079 BPF_STMT(BPF_RET | BPF_K, ~0),
6080};
6081
6082static struct sock_fprog msock_filter = {
6083 .len = sizeof(msock_filter_insns)/sizeof(msock_filter_insns[0]),
6084 .filter = msock_filter_insns,
6085};
6086
6087
6088static int add_monitor_filter(int s)
6089{
6090 int idx;
6091
6092 /* rewrite all PASS/FAIL jump offsets */
6093 for (idx = 0; idx < msock_filter.len; idx++) {
6094 struct sock_filter *insn = &msock_filter_insns[idx];
6095
6096 if (BPF_CLASS(insn->code) == BPF_JMP) {
6097 if (insn->code == (BPF_JMP|BPF_JA)) {
6098 if (insn->k == PASS)
6099 insn->k = msock_filter.len - idx - 2;
6100 else if (insn->k == FAIL)
6101 insn->k = msock_filter.len - idx - 3;
6102 }
6103
6104 if (insn->jt == PASS)
6105 insn->jt = msock_filter.len - idx - 2;
6106 else if (insn->jt == FAIL)
6107 insn->jt = msock_filter.len - idx - 3;
6108
6109 if (insn->jf == PASS)
6110 insn->jf = msock_filter.len - idx - 2;
6111 else if (insn->jf == FAIL)
6112 insn->jf = msock_filter.len - idx - 3;
6113 }
6114 }
6115
6116 if (setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER,
6117 &msock_filter, sizeof(msock_filter))) {
6118 perror("SO_ATTACH_FILTER");
6119 return -1;
6120 }
6121
6122 return 0;
6123}
6124
6125
6126static void nl80211_remove_monitor_interface(
6127 struct wpa_driver_nl80211_data *drv)
6128{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006129 drv->monitor_refcount--;
6130 if (drv->monitor_refcount > 0)
6131 return;
6132
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006133 if (drv->monitor_ifidx >= 0) {
6134 nl80211_remove_iface(drv, drv->monitor_ifidx);
6135 drv->monitor_ifidx = -1;
6136 }
6137 if (drv->monitor_sock >= 0) {
6138 eloop_unregister_read_sock(drv->monitor_sock);
6139 close(drv->monitor_sock);
6140 drv->monitor_sock = -1;
6141 }
6142}
6143
6144
6145static int
6146nl80211_create_monitor_interface(struct wpa_driver_nl80211_data *drv)
6147{
6148 char buf[IFNAMSIZ];
6149 struct sockaddr_ll ll;
6150 int optval;
6151 socklen_t optlen;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006152
6153 if (drv->monitor_ifidx >= 0) {
6154 drv->monitor_refcount++;
6155 return 0;
6156 }
6157
6158 if (os_strncmp(drv->first_bss.ifname, "p2p-", 4) == 0) {
6159 /*
6160 * P2P interface name is of the format p2p-%s-%d. For monitor
6161 * interface name corresponding to P2P GO, replace "p2p-" with
6162 * "mon-" to retain the same interface name length and to
6163 * indicate that it is a monitor interface.
6164 */
6165 snprintf(buf, IFNAMSIZ, "mon-%s", drv->first_bss.ifname + 4);
6166 } else {
6167 /* Non-P2P interface with AP functionality. */
6168 snprintf(buf, IFNAMSIZ, "mon.%s", drv->first_bss.ifname);
6169 }
6170
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006171 buf[IFNAMSIZ - 1] = '\0';
6172
6173 drv->monitor_ifidx =
6174 nl80211_create_iface(drv, buf, NL80211_IFTYPE_MONITOR, NULL,
6175 0);
6176
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07006177 if (drv->monitor_ifidx == -EOPNOTSUPP) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006178 /*
6179 * This is backward compatibility for a few versions of
6180 * the kernel only that didn't advertise the right
6181 * attributes for the only driver that then supported
6182 * AP mode w/o monitor -- ath6kl.
6183 */
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07006184 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support "
6185 "monitor interface type - try to run without it");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006186 drv->device_ap_sme = 1;
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07006187 }
6188
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006189 if (drv->monitor_ifidx < 0)
6190 return -1;
6191
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006192 if (linux_set_iface_flags(drv->global->ioctl_sock, buf, 1))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006193 goto error;
6194
6195 memset(&ll, 0, sizeof(ll));
6196 ll.sll_family = AF_PACKET;
6197 ll.sll_ifindex = drv->monitor_ifidx;
6198 drv->monitor_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
6199 if (drv->monitor_sock < 0) {
6200 perror("socket[PF_PACKET,SOCK_RAW]");
6201 goto error;
6202 }
6203
6204 if (add_monitor_filter(drv->monitor_sock)) {
6205 wpa_printf(MSG_INFO, "Failed to set socket filter for monitor "
6206 "interface; do filtering in user space");
6207 /* This works, but will cost in performance. */
6208 }
6209
6210 if (bind(drv->monitor_sock, (struct sockaddr *) &ll, sizeof(ll)) < 0) {
6211 perror("monitor socket bind");
6212 goto error;
6213 }
6214
6215 optlen = sizeof(optval);
6216 optval = 20;
6217 if (setsockopt
6218 (drv->monitor_sock, SOL_SOCKET, SO_PRIORITY, &optval, optlen)) {
6219 perror("Failed to set socket priority");
6220 goto error;
6221 }
6222
6223 if (eloop_register_read_sock(drv->monitor_sock, handle_monitor_read,
6224 drv, NULL)) {
6225 printf("Could not register monitor read socket\n");
6226 goto error;
6227 }
6228
6229 return 0;
6230 error:
6231 nl80211_remove_monitor_interface(drv);
6232 return -1;
6233}
6234
6235
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006236static int nl80211_setup_ap(struct i802_bss *bss)
6237{
6238 struct wpa_driver_nl80211_data *drv = bss->drv;
6239
6240 wpa_printf(MSG_DEBUG, "nl80211: Setup AP - device_ap_sme=%d "
6241 "use_monitor=%d", drv->device_ap_sme, drv->use_monitor);
6242
6243 /*
6244 * Disable Probe Request reporting unless we need it in this way for
6245 * devices that include the AP SME, in the other case (unless using
6246 * monitor iface) we'll get it through the nl_mgmt socket instead.
6247 */
6248 if (!drv->device_ap_sme)
6249 wpa_driver_nl80211_probe_req_report(bss, 0);
6250
6251 if (!drv->device_ap_sme && !drv->use_monitor)
6252 if (nl80211_mgmt_subscribe_ap(bss))
6253 return -1;
6254
6255 if (drv->device_ap_sme && !drv->use_monitor)
6256 if (nl80211_mgmt_subscribe_ap_dev_sme(bss))
6257 return -1;
6258
6259 if (!drv->device_ap_sme && drv->use_monitor &&
6260 nl80211_create_monitor_interface(drv) &&
6261 !drv->device_ap_sme)
Dmitry Shmidt04949592012-07-19 12:16:46 -07006262 return -1;
6263
6264#ifdef ANDROID_P2P
6265 if (drv->device_ap_sme && drv->use_monitor)
Dmitry Shmidtb638fe72012-03-20 12:51:25 -07006266 if (nl80211_mgmt_subscribe_ap_dev_sme(bss))
6267 return -1;
6268
6269 if (drv->use_monitor &&
6270 nl80211_create_monitor_interface(drv))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006271 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006272#endif
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006273
6274 if (drv->device_ap_sme &&
6275 wpa_driver_nl80211_probe_req_report(bss, 1) < 0) {
6276 wpa_printf(MSG_DEBUG, "nl80211: Failed to enable "
6277 "Probe Request frame reporting in AP mode");
6278 /* Try to survive without this */
6279 }
6280
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006281 return 0;
6282}
6283
6284
6285static void nl80211_teardown_ap(struct i802_bss *bss)
6286{
6287 struct wpa_driver_nl80211_data *drv = bss->drv;
6288
6289 if (drv->device_ap_sme) {
6290 wpa_driver_nl80211_probe_req_report(bss, 0);
6291 if (!drv->use_monitor)
6292 nl80211_mgmt_unsubscribe(bss, "AP teardown (dev SME)");
6293 } else if (drv->use_monitor)
6294 nl80211_remove_monitor_interface(drv);
6295 else
6296 nl80211_mgmt_unsubscribe(bss, "AP teardown");
6297
6298 bss->beacon_set = 0;
6299}
6300
6301
6302static int nl80211_send_eapol_data(struct i802_bss *bss,
6303 const u8 *addr, const u8 *data,
6304 size_t data_len)
6305{
6306 struct sockaddr_ll ll;
6307 int ret;
6308
6309 if (bss->drv->eapol_tx_sock < 0) {
6310 wpa_printf(MSG_DEBUG, "nl80211: No socket to send EAPOL");
6311 return -1;
6312 }
6313
6314 os_memset(&ll, 0, sizeof(ll));
6315 ll.sll_family = AF_PACKET;
6316 ll.sll_ifindex = bss->ifindex;
6317 ll.sll_protocol = htons(ETH_P_PAE);
6318 ll.sll_halen = ETH_ALEN;
6319 os_memcpy(ll.sll_addr, addr, ETH_ALEN);
6320 ret = sendto(bss->drv->eapol_tx_sock, data, data_len, 0,
6321 (struct sockaddr *) &ll, sizeof(ll));
6322 if (ret < 0)
6323 wpa_printf(MSG_ERROR, "nl80211: EAPOL TX: %s",
6324 strerror(errno));
6325
6326 return ret;
6327}
6328
6329
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006330static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
6331
6332static int wpa_driver_nl80211_hapd_send_eapol(
6333 void *priv, const u8 *addr, const u8 *data,
6334 size_t data_len, int encrypt, const u8 *own_addr, u32 flags)
6335{
6336 struct i802_bss *bss = priv;
6337 struct wpa_driver_nl80211_data *drv = bss->drv;
6338 struct ieee80211_hdr *hdr;
6339 size_t len;
6340 u8 *pos;
6341 int res;
6342 int qos = flags & WPA_STA_WMM;
Dmitry Shmidtb638fe72012-03-20 12:51:25 -07006343#ifndef ANDROID_P2P
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006344 if (drv->device_ap_sme || !drv->use_monitor)
Dmitry Shmidtb638fe72012-03-20 12:51:25 -07006345#else
6346 if (drv->device_ap_sme && !drv->use_monitor)
6347#endif
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006348 return nl80211_send_eapol_data(bss, addr, data, data_len);
6349
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006350 len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
6351 data_len;
6352 hdr = os_zalloc(len);
6353 if (hdr == NULL) {
6354 printf("malloc() failed for i802_send_data(len=%lu)\n",
6355 (unsigned long) len);
6356 return -1;
6357 }
6358
6359 hdr->frame_control =
6360 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
6361 hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
6362 if (encrypt)
6363 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
6364 if (qos) {
6365 hdr->frame_control |=
6366 host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
6367 }
6368
6369 memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
6370 memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
6371 memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
6372 pos = (u8 *) (hdr + 1);
6373
6374 if (qos) {
6375 /* add an empty QoS header if needed */
6376 pos[0] = 0;
6377 pos[1] = 0;
6378 pos += 2;
6379 }
6380
6381 memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
6382 pos += sizeof(rfc1042_header);
6383 WPA_PUT_BE16(pos, ETH_P_PAE);
6384 pos += 2;
6385 memcpy(pos, data, data_len);
6386
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006387 res = wpa_driver_nl80211_send_frame(bss, (u8 *) hdr, len, encrypt, 0,
6388 0, 0, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006389 if (res < 0) {
6390 wpa_printf(MSG_ERROR, "i802_send_eapol - packet len: %lu - "
6391 "failed: %d (%s)",
6392 (unsigned long) len, errno, strerror(errno));
6393 }
6394 os_free(hdr);
6395
6396 return res;
6397}
6398
6399
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006400static int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr,
6401 int total_flags,
6402 int flags_or, int flags_and)
6403{
6404 struct i802_bss *bss = priv;
6405 struct wpa_driver_nl80211_data *drv = bss->drv;
6406 struct nl_msg *msg, *flags = NULL;
6407 struct nl80211_sta_flag_update upd;
6408
6409 msg = nlmsg_alloc();
6410 if (!msg)
6411 return -ENOMEM;
6412
6413 flags = nlmsg_alloc();
6414 if (!flags) {
6415 nlmsg_free(msg);
6416 return -ENOMEM;
6417 }
6418
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006419 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006420
6421 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
6422 if_nametoindex(bss->ifname));
6423 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
6424
6425 /*
6426 * Backwards compatibility version using NL80211_ATTR_STA_FLAGS. This
6427 * can be removed eventually.
6428 */
6429 if (total_flags & WPA_STA_AUTHORIZED)
6430 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_AUTHORIZED);
6431
6432 if (total_flags & WPA_STA_WMM)
6433 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_WME);
6434
6435 if (total_flags & WPA_STA_SHORT_PREAMBLE)
6436 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_SHORT_PREAMBLE);
6437
6438 if (total_flags & WPA_STA_MFP)
6439 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_MFP);
6440
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006441 if (total_flags & WPA_STA_TDLS_PEER)
6442 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_TDLS_PEER);
6443
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006444 if (nla_put_nested(msg, NL80211_ATTR_STA_FLAGS, flags))
6445 goto nla_put_failure;
6446
6447 os_memset(&upd, 0, sizeof(upd));
6448 upd.mask = sta_flags_nl80211(flags_or | ~flags_and);
6449 upd.set = sta_flags_nl80211(flags_or);
6450 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
6451
6452 nlmsg_free(flags);
6453
6454 return send_and_recv_msgs(drv, msg, NULL, NULL);
6455 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006456 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006457 nlmsg_free(flags);
6458 return -ENOBUFS;
6459}
6460
6461
6462static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv,
6463 struct wpa_driver_associate_params *params)
6464{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006465 enum nl80211_iftype nlmode;
6466
6467 if (params->p2p) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006468 wpa_printf(MSG_DEBUG, "nl80211: Setup AP operations for P2P "
6469 "group (GO)");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006470 nlmode = NL80211_IFTYPE_P2P_GO;
6471 } else
6472 nlmode = NL80211_IFTYPE_AP;
6473
6474 if (wpa_driver_nl80211_set_mode(&drv->first_bss, nlmode) ||
6475 wpa_driver_nl80211_set_freq(&drv->first_bss, params->freq, 0, 0)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006476 nl80211_remove_monitor_interface(drv);
6477 return -1;
6478 }
6479
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006480 return 0;
6481}
6482
6483
6484static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv)
6485{
6486 struct nl_msg *msg;
6487 int ret = -1;
6488
6489 msg = nlmsg_alloc();
6490 if (!msg)
6491 return -1;
6492
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006493 nl80211_cmd(drv, msg, 0, NL80211_CMD_LEAVE_IBSS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006494 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
6495 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6496 msg = NULL;
6497 if (ret) {
6498 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS failed: ret=%d "
6499 "(%s)", ret, strerror(-ret));
6500 goto nla_put_failure;
6501 }
6502
6503 ret = 0;
6504 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS request sent successfully");
6505
6506nla_put_failure:
6507 nlmsg_free(msg);
6508 return ret;
6509}
6510
6511
6512static int wpa_driver_nl80211_ibss(struct wpa_driver_nl80211_data *drv,
6513 struct wpa_driver_associate_params *params)
6514{
6515 struct nl_msg *msg;
6516 int ret = -1;
6517 int count = 0;
6518
6519 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS (ifindex=%d)", drv->ifindex);
6520
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006521 if (wpa_driver_nl80211_set_mode(&drv->first_bss,
6522 NL80211_IFTYPE_ADHOC)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006523 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
6524 "IBSS mode");
6525 return -1;
6526 }
6527
6528retry:
6529 msg = nlmsg_alloc();
6530 if (!msg)
6531 return -1;
6532
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006533 nl80211_cmd(drv, msg, 0, NL80211_CMD_JOIN_IBSS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006534 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
6535
6536 if (params->ssid == NULL || params->ssid_len > sizeof(drv->ssid))
6537 goto nla_put_failure;
6538
6539 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
6540 params->ssid, params->ssid_len);
6541 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
6542 params->ssid);
6543 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
6544 drv->ssid_len = params->ssid_len;
6545
6546 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
6547 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
6548
6549 ret = nl80211_set_conn_keys(params, msg);
6550 if (ret)
6551 goto nla_put_failure;
6552
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006553 if (params->bssid && params->fixed_bssid) {
6554 wpa_printf(MSG_DEBUG, " * BSSID=" MACSTR,
6555 MAC2STR(params->bssid));
6556 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
6557 }
6558
6559 if (params->key_mgmt_suite == KEY_MGMT_802_1X ||
6560 params->key_mgmt_suite == KEY_MGMT_PSK ||
6561 params->key_mgmt_suite == KEY_MGMT_802_1X_SHA256 ||
6562 params->key_mgmt_suite == KEY_MGMT_PSK_SHA256) {
6563 wpa_printf(MSG_DEBUG, " * control port");
6564 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT);
6565 }
6566
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006567 if (params->wpa_ie) {
6568 wpa_hexdump(MSG_DEBUG,
6569 " * Extra IEs for Beacon/Probe Response frames",
6570 params->wpa_ie, params->wpa_ie_len);
6571 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
6572 params->wpa_ie);
6573 }
6574
6575 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6576 msg = NULL;
6577 if (ret) {
6578 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS failed: ret=%d (%s)",
6579 ret, strerror(-ret));
6580 count++;
6581 if (ret == -EALREADY && count == 1) {
6582 wpa_printf(MSG_DEBUG, "nl80211: Retry IBSS join after "
6583 "forced leave");
6584 nl80211_leave_ibss(drv);
6585 nlmsg_free(msg);
6586 goto retry;
6587 }
6588
6589 goto nla_put_failure;
6590 }
6591 ret = 0;
6592 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS request sent successfully");
6593
6594nla_put_failure:
6595 nlmsg_free(msg);
6596 return ret;
6597}
6598
6599
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006600static unsigned int nl80211_get_assoc_bssid(struct wpa_driver_nl80211_data *drv,
6601 u8 *bssid)
6602{
6603 struct nl_msg *msg;
6604 int ret;
6605 struct nl80211_bss_info_arg arg;
6606
6607 os_memset(&arg, 0, sizeof(arg));
6608 msg = nlmsg_alloc();
6609 if (!msg)
6610 goto nla_put_failure;
6611
6612 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
6613 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
6614
6615 arg.drv = drv;
6616 ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
6617 msg = NULL;
6618 if (ret == 0) {
6619 if (is_zero_ether_addr(arg.assoc_bssid))
6620 return -ENOTCONN;
6621 os_memcpy(bssid, arg.assoc_bssid, ETH_ALEN);
6622 return 0;
6623 }
6624 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
6625 "(%s)", ret, strerror(-ret));
6626nla_put_failure:
6627 nlmsg_free(msg);
6628 return drv->assoc_freq;
6629}
6630
6631
6632static int nl80211_disconnect(struct wpa_driver_nl80211_data *drv,
6633 const u8 *bssid)
6634{
6635 u8 addr[ETH_ALEN];
6636
6637 if (bssid == NULL) {
6638 int res = nl80211_get_assoc_bssid(drv, addr);
6639 if (res)
6640 return res;
6641 bssid = addr;
6642 }
6643
6644 return wpa_driver_nl80211_disconnect(drv, bssid,
6645 WLAN_REASON_PREV_AUTH_NOT_VALID);
6646}
6647
6648
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006649static int wpa_driver_nl80211_connect(
6650 struct wpa_driver_nl80211_data *drv,
6651 struct wpa_driver_associate_params *params)
6652{
6653 struct nl_msg *msg;
6654 enum nl80211_auth_type type;
6655 int ret = 0;
6656 int algs;
6657
6658 msg = nlmsg_alloc();
6659 if (!msg)
6660 return -1;
6661
6662 wpa_printf(MSG_DEBUG, "nl80211: Connect (ifindex=%d)", drv->ifindex);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006663 nl80211_cmd(drv, msg, 0, NL80211_CMD_CONNECT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006664
6665 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
6666 if (params->bssid) {
6667 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
6668 MAC2STR(params->bssid));
6669 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
6670 }
6671 if (params->freq) {
6672 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
6673 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
6674 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07006675 if (params->bg_scan_period >= 0) {
6676 wpa_printf(MSG_DEBUG, " * bg scan period=%d",
6677 params->bg_scan_period);
6678 NLA_PUT_U16(msg, NL80211_ATTR_BG_SCAN_PERIOD,
6679 params->bg_scan_period);
6680 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006681 if (params->ssid) {
6682 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
6683 params->ssid, params->ssid_len);
6684 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
6685 params->ssid);
6686 if (params->ssid_len > sizeof(drv->ssid))
6687 goto nla_put_failure;
6688 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
6689 drv->ssid_len = params->ssid_len;
6690 }
6691 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
6692 if (params->wpa_ie)
6693 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
6694 params->wpa_ie);
6695
6696 algs = 0;
6697 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
6698 algs++;
6699 if (params->auth_alg & WPA_AUTH_ALG_SHARED)
6700 algs++;
6701 if (params->auth_alg & WPA_AUTH_ALG_LEAP)
6702 algs++;
6703 if (algs > 1) {
6704 wpa_printf(MSG_DEBUG, " * Leave out Auth Type for automatic "
6705 "selection");
6706 goto skip_auth_type;
6707 }
6708
6709 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
6710 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
6711 else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
6712 type = NL80211_AUTHTYPE_SHARED_KEY;
6713 else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
6714 type = NL80211_AUTHTYPE_NETWORK_EAP;
6715 else if (params->auth_alg & WPA_AUTH_ALG_FT)
6716 type = NL80211_AUTHTYPE_FT;
6717 else
6718 goto nla_put_failure;
6719
6720 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
6721 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
6722
6723skip_auth_type:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006724 if (params->wpa_proto) {
6725 enum nl80211_wpa_versions ver = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006726
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006727 if (params->wpa_proto & WPA_PROTO_WPA)
6728 ver |= NL80211_WPA_VERSION_1;
6729 if (params->wpa_proto & WPA_PROTO_RSN)
6730 ver |= NL80211_WPA_VERSION_2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006731
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006732 wpa_printf(MSG_DEBUG, " * WPA Versions 0x%x", ver);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006733 NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver);
6734 }
6735
6736 if (params->pairwise_suite != CIPHER_NONE) {
6737 int cipher;
6738
6739 switch (params->pairwise_suite) {
6740 case CIPHER_WEP40:
6741 cipher = WLAN_CIPHER_SUITE_WEP40;
6742 break;
6743 case CIPHER_WEP104:
6744 cipher = WLAN_CIPHER_SUITE_WEP104;
6745 break;
6746 case CIPHER_CCMP:
6747 cipher = WLAN_CIPHER_SUITE_CCMP;
6748 break;
6749 case CIPHER_TKIP:
6750 default:
6751 cipher = WLAN_CIPHER_SUITE_TKIP;
6752 break;
6753 }
6754 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE, cipher);
6755 }
6756
6757 if (params->group_suite != CIPHER_NONE) {
6758 int cipher;
6759
6760 switch (params->group_suite) {
6761 case CIPHER_WEP40:
6762 cipher = WLAN_CIPHER_SUITE_WEP40;
6763 break;
6764 case CIPHER_WEP104:
6765 cipher = WLAN_CIPHER_SUITE_WEP104;
6766 break;
6767 case CIPHER_CCMP:
6768 cipher = WLAN_CIPHER_SUITE_CCMP;
6769 break;
6770 case CIPHER_TKIP:
6771 default:
6772 cipher = WLAN_CIPHER_SUITE_TKIP;
6773 break;
6774 }
6775 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher);
6776 }
6777
6778 if (params->key_mgmt_suite == KEY_MGMT_802_1X ||
6779 params->key_mgmt_suite == KEY_MGMT_PSK) {
6780 int mgmt = WLAN_AKM_SUITE_PSK;
6781
6782 switch (params->key_mgmt_suite) {
6783 case KEY_MGMT_802_1X:
6784 mgmt = WLAN_AKM_SUITE_8021X;
6785 break;
6786 case KEY_MGMT_PSK:
6787 default:
6788 mgmt = WLAN_AKM_SUITE_PSK;
6789 break;
6790 }
6791 NLA_PUT_U32(msg, NL80211_ATTR_AKM_SUITES, mgmt);
6792 }
6793
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006794 if (params->disable_ht)
6795 NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_HT);
6796
6797 if (params->htcaps && params->htcaps_mask) {
6798 int sz = sizeof(struct ieee80211_ht_capabilities);
6799 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY, sz, params->htcaps);
6800 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY_MASK, sz,
6801 params->htcaps_mask);
6802 }
6803
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006804 ret = nl80211_set_conn_keys(params, msg);
6805 if (ret)
6806 goto nla_put_failure;
6807
6808 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6809 msg = NULL;
6810 if (ret) {
6811 wpa_printf(MSG_DEBUG, "nl80211: MLME connect failed: ret=%d "
6812 "(%s)", ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006813 /*
6814 * cfg80211 does not currently accept new connection if we are
6815 * already connected. As a workaround, force disconnection and
6816 * try again once the driver indicates it completed
6817 * disconnection.
6818 */
6819 if (ret == -EALREADY)
6820 nl80211_disconnect(drv, params->bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006821 goto nla_put_failure;
6822 }
6823 ret = 0;
6824 wpa_printf(MSG_DEBUG, "nl80211: Connect request send successfully");
6825
6826nla_put_failure:
6827 nlmsg_free(msg);
6828 return ret;
6829
6830}
6831
6832
6833static int wpa_driver_nl80211_associate(
6834 void *priv, struct wpa_driver_associate_params *params)
6835{
6836 struct i802_bss *bss = priv;
6837 struct wpa_driver_nl80211_data *drv = bss->drv;
6838 int ret = -1;
6839 struct nl_msg *msg;
6840
6841 if (params->mode == IEEE80211_MODE_AP)
6842 return wpa_driver_nl80211_ap(drv, params);
6843
6844 if (params->mode == IEEE80211_MODE_IBSS)
6845 return wpa_driver_nl80211_ibss(drv, params);
6846
6847 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006848 enum nl80211_iftype nlmode = params->p2p ?
6849 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
6850
6851 if (wpa_driver_nl80211_set_mode(priv, nlmode) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006852 return -1;
6853 return wpa_driver_nl80211_connect(drv, params);
6854 }
6855
6856 drv->associated = 0;
6857
6858 msg = nlmsg_alloc();
6859 if (!msg)
6860 return -1;
6861
6862 wpa_printf(MSG_DEBUG, "nl80211: Associate (ifindex=%d)",
6863 drv->ifindex);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006864 nl80211_cmd(drv, msg, 0, NL80211_CMD_ASSOCIATE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006865
6866 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
6867 if (params->bssid) {
6868 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
6869 MAC2STR(params->bssid));
6870 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
6871 }
6872 if (params->freq) {
6873 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
6874 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
6875 drv->assoc_freq = params->freq;
6876 } else
6877 drv->assoc_freq = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006878 if (params->bg_scan_period >= 0) {
6879 wpa_printf(MSG_DEBUG, " * bg scan period=%d",
6880 params->bg_scan_period);
6881 NLA_PUT_U16(msg, NL80211_ATTR_BG_SCAN_PERIOD,
6882 params->bg_scan_period);
6883 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006884 if (params->ssid) {
6885 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
6886 params->ssid, params->ssid_len);
6887 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
6888 params->ssid);
6889 if (params->ssid_len > sizeof(drv->ssid))
6890 goto nla_put_failure;
6891 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
6892 drv->ssid_len = params->ssid_len;
6893 }
6894 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
6895 if (params->wpa_ie)
6896 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
6897 params->wpa_ie);
6898
6899 if (params->pairwise_suite != CIPHER_NONE) {
6900 int cipher;
6901
6902 switch (params->pairwise_suite) {
6903 case CIPHER_WEP40:
6904 cipher = WLAN_CIPHER_SUITE_WEP40;
6905 break;
6906 case CIPHER_WEP104:
6907 cipher = WLAN_CIPHER_SUITE_WEP104;
6908 break;
6909 case CIPHER_CCMP:
6910 cipher = WLAN_CIPHER_SUITE_CCMP;
6911 break;
6912 case CIPHER_TKIP:
6913 default:
6914 cipher = WLAN_CIPHER_SUITE_TKIP;
6915 break;
6916 }
6917 wpa_printf(MSG_DEBUG, " * pairwise=0x%x", cipher);
6918 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE, cipher);
6919 }
6920
6921 if (params->group_suite != CIPHER_NONE) {
6922 int cipher;
6923
6924 switch (params->group_suite) {
6925 case CIPHER_WEP40:
6926 cipher = WLAN_CIPHER_SUITE_WEP40;
6927 break;
6928 case CIPHER_WEP104:
6929 cipher = WLAN_CIPHER_SUITE_WEP104;
6930 break;
6931 case CIPHER_CCMP:
6932 cipher = WLAN_CIPHER_SUITE_CCMP;
6933 break;
6934 case CIPHER_TKIP:
6935 default:
6936 cipher = WLAN_CIPHER_SUITE_TKIP;
6937 break;
6938 }
6939 wpa_printf(MSG_DEBUG, " * group=0x%x", cipher);
6940 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher);
6941 }
6942
6943#ifdef CONFIG_IEEE80211W
6944 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED)
6945 NLA_PUT_U32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED);
6946#endif /* CONFIG_IEEE80211W */
6947
6948 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT);
6949
6950 if (params->prev_bssid) {
6951 wpa_printf(MSG_DEBUG, " * prev_bssid=" MACSTR,
6952 MAC2STR(params->prev_bssid));
6953 NLA_PUT(msg, NL80211_ATTR_PREV_BSSID, ETH_ALEN,
6954 params->prev_bssid);
6955 }
6956
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006957 if (params->disable_ht)
6958 NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_HT);
6959
6960 if (params->htcaps && params->htcaps_mask) {
6961 int sz = sizeof(struct ieee80211_ht_capabilities);
6962 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY, sz, params->htcaps);
6963 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY_MASK, sz,
6964 params->htcaps_mask);
6965 }
6966
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006967 if (params->p2p)
6968 wpa_printf(MSG_DEBUG, " * P2P group");
6969
6970 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6971 msg = NULL;
6972 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006973 wpa_dbg(drv->ctx, MSG_DEBUG,
6974 "nl80211: MLME command failed (assoc): ret=%d (%s)",
6975 ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006976 nl80211_dump_scan(drv);
6977 goto nla_put_failure;
6978 }
6979 ret = 0;
6980 wpa_printf(MSG_DEBUG, "nl80211: Association request send "
6981 "successfully");
6982
6983nla_put_failure:
6984 nlmsg_free(msg);
6985 return ret;
6986}
6987
6988
6989static int nl80211_set_mode(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006990 int ifindex, enum nl80211_iftype mode)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006991{
6992 struct nl_msg *msg;
6993 int ret = -ENOBUFS;
6994
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006995 wpa_printf(MSG_DEBUG, "nl80211: Set mode ifindex %d iftype %d (%s)",
6996 ifindex, mode, nl80211_iftype_str(mode));
6997
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006998 msg = nlmsg_alloc();
6999 if (!msg)
7000 return -ENOMEM;
7001
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007002 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_INTERFACE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007003 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
7004 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, mode);
7005
7006 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007007 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007008 if (!ret)
7009 return 0;
7010nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007011 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007012 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface %d to mode %d:"
7013 " %d (%s)", ifindex, mode, ret, strerror(-ret));
7014 return ret;
7015}
7016
7017
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007018static int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
7019 enum nl80211_iftype nlmode)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007020{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007021 struct wpa_driver_nl80211_data *drv = bss->drv;
7022 int ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007023 int i;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007024 int was_ap = is_ap_interface(drv->nlmode);
7025 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007026
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007027 res = nl80211_set_mode(drv, drv->ifindex, nlmode);
7028 if (res == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007029 drv->nlmode = nlmode;
7030 ret = 0;
7031 goto done;
7032 }
7033
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007034 if (res == -ENODEV)
7035 return -1;
7036
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007037 if (nlmode == drv->nlmode) {
7038 wpa_printf(MSG_DEBUG, "nl80211: Interface already in "
7039 "requested mode - ignore error");
7040 ret = 0;
7041 goto done; /* Already in the requested mode */
7042 }
7043
7044 /* mac80211 doesn't allow mode changes while the device is up, so
7045 * take the device down, try to set the mode again, and bring the
7046 * device back up.
7047 */
7048 wpa_printf(MSG_DEBUG, "nl80211: Try mode change after setting "
7049 "interface down");
7050 for (i = 0; i < 10; i++) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007051 res = linux_set_iface_flags(drv->global->ioctl_sock,
7052 bss->ifname, 0);
7053 if (res == -EACCES || res == -ENODEV)
7054 break;
7055 if (res == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007056 /* Try to set the mode again while the interface is
7057 * down */
7058 ret = nl80211_set_mode(drv, drv->ifindex, nlmode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007059 if (ret == -EACCES)
7060 break;
7061 res = linux_set_iface_flags(drv->global->ioctl_sock,
7062 bss->ifname, 1);
7063 if (res && !ret)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007064 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007065 else if (ret != -EBUSY)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007066 break;
7067 } else
7068 wpa_printf(MSG_DEBUG, "nl80211: Failed to set "
7069 "interface down");
7070 os_sleep(0, 100000);
7071 }
7072
7073 if (!ret) {
7074 wpa_printf(MSG_DEBUG, "nl80211: Mode change succeeded while "
7075 "interface is down");
7076 drv->nlmode = nlmode;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007077 drv->ignore_if_down_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007078 }
7079
7080done:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007081 if (ret) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007082 wpa_printf(MSG_DEBUG, "nl80211: Interface mode change to %d "
7083 "from %d failed", nlmode, drv->nlmode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007084 return ret;
7085 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007086
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007087 if (is_ap_interface(nlmode)) {
7088 nl80211_mgmt_unsubscribe(bss, "start AP");
7089 /* Setup additional AP mode functionality if needed */
7090 if (nl80211_setup_ap(bss))
7091 return -1;
7092 } else if (was_ap) {
7093 /* Remove additional AP mode functionality */
7094 nl80211_teardown_ap(bss);
7095 } else {
7096 nl80211_mgmt_unsubscribe(bss, "mode change");
7097 }
7098
Dmitry Shmidt04949592012-07-19 12:16:46 -07007099 if (!bss->in_deinit && !is_ap_interface(nlmode) &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007100 nl80211_mgmt_subscribe_non_ap(bss) < 0)
7101 wpa_printf(MSG_DEBUG, "nl80211: Failed to register Action "
7102 "frame processing - ignore for now");
7103
7104 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007105}
7106
7107
7108static int wpa_driver_nl80211_get_capa(void *priv,
7109 struct wpa_driver_capa *capa)
7110{
7111 struct i802_bss *bss = priv;
7112 struct wpa_driver_nl80211_data *drv = bss->drv;
7113 if (!drv->has_capability)
7114 return -1;
7115 os_memcpy(capa, &drv->capa, sizeof(*capa));
7116 return 0;
7117}
7118
7119
7120static int wpa_driver_nl80211_set_operstate(void *priv, int state)
7121{
7122 struct i802_bss *bss = priv;
7123 struct wpa_driver_nl80211_data *drv = bss->drv;
7124
7125 wpa_printf(MSG_DEBUG, "%s: operstate %d->%d (%s)",
7126 __func__, drv->operstate, state, state ? "UP" : "DORMANT");
7127 drv->operstate = state;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007128 return netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, -1,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007129 state ? IF_OPER_UP : IF_OPER_DORMANT);
7130}
7131
7132
7133static int wpa_driver_nl80211_set_supp_port(void *priv, int authorized)
7134{
7135 struct i802_bss *bss = priv;
7136 struct wpa_driver_nl80211_data *drv = bss->drv;
7137 struct nl_msg *msg;
7138 struct nl80211_sta_flag_update upd;
7139
7140 msg = nlmsg_alloc();
7141 if (!msg)
7142 return -ENOMEM;
7143
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007144 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007145
7146 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
7147 if_nametoindex(bss->ifname));
7148 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid);
7149
7150 os_memset(&upd, 0, sizeof(upd));
7151 upd.mask = BIT(NL80211_STA_FLAG_AUTHORIZED);
7152 if (authorized)
7153 upd.set = BIT(NL80211_STA_FLAG_AUTHORIZED);
7154 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
7155
7156 return send_and_recv_msgs(drv, msg, NULL, NULL);
7157 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007158 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007159 return -ENOBUFS;
7160}
7161
7162
Jouni Malinen75ecf522011-06-27 15:19:46 -07007163/* Set kernel driver on given frequency (MHz) */
7164static int i802_set_freq(void *priv, struct hostapd_freq_params *freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007165{
Jouni Malinen75ecf522011-06-27 15:19:46 -07007166 struct i802_bss *bss = priv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007167 return wpa_driver_nl80211_set_freq(bss, freq->freq, freq->ht_enabled,
Jouni Malinen75ecf522011-06-27 15:19:46 -07007168 freq->sec_channel_offset);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007169}
7170
7171
Jouni Malinen75ecf522011-06-27 15:19:46 -07007172#if defined(HOSTAPD) || defined(CONFIG_AP)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007173
7174static inline int min_int(int a, int b)
7175{
7176 if (a < b)
7177 return a;
7178 return b;
7179}
7180
7181
7182static int get_key_handler(struct nl_msg *msg, void *arg)
7183{
7184 struct nlattr *tb[NL80211_ATTR_MAX + 1];
7185 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
7186
7187 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
7188 genlmsg_attrlen(gnlh, 0), NULL);
7189
7190 /*
7191 * TODO: validate the key index and mac address!
7192 * Otherwise, there's a race condition as soon as
7193 * the kernel starts sending key notifications.
7194 */
7195
7196 if (tb[NL80211_ATTR_KEY_SEQ])
7197 memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]),
7198 min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6));
7199 return NL_SKIP;
7200}
7201
7202
7203static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
7204 int idx, u8 *seq)
7205{
7206 struct i802_bss *bss = priv;
7207 struct wpa_driver_nl80211_data *drv = bss->drv;
7208 struct nl_msg *msg;
7209
7210 msg = nlmsg_alloc();
7211 if (!msg)
7212 return -ENOMEM;
7213
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007214 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_KEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007215
7216 if (addr)
7217 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
7218 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, idx);
7219 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
7220
7221 memset(seq, 0, 6);
7222
7223 return send_and_recv_msgs(drv, msg, get_key_handler, seq);
7224 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007225 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007226 return -ENOBUFS;
7227}
7228
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007229
7230static int i802_set_rts(void *priv, int rts)
7231{
7232 struct i802_bss *bss = priv;
7233 struct wpa_driver_nl80211_data *drv = bss->drv;
7234 struct nl_msg *msg;
7235 int ret = -ENOBUFS;
7236 u32 val;
7237
7238 msg = nlmsg_alloc();
7239 if (!msg)
7240 return -ENOMEM;
7241
7242 if (rts >= 2347)
7243 val = (u32) -1;
7244 else
7245 val = rts;
7246
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007247 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007248 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
7249 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD, val);
7250
7251 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007252 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007253 if (!ret)
7254 return 0;
7255nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007256 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007257 wpa_printf(MSG_DEBUG, "nl80211: Failed to set RTS threshold %d: "
7258 "%d (%s)", rts, ret, strerror(-ret));
7259 return ret;
7260}
7261
7262
7263static int i802_set_frag(void *priv, int frag)
7264{
7265 struct i802_bss *bss = priv;
7266 struct wpa_driver_nl80211_data *drv = bss->drv;
7267 struct nl_msg *msg;
7268 int ret = -ENOBUFS;
7269 u32 val;
7270
7271 msg = nlmsg_alloc();
7272 if (!msg)
7273 return -ENOMEM;
7274
7275 if (frag >= 2346)
7276 val = (u32) -1;
7277 else
7278 val = frag;
7279
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007280 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007281 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
7282 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD, val);
7283
7284 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007285 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007286 if (!ret)
7287 return 0;
7288nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007289 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007290 wpa_printf(MSG_DEBUG, "nl80211: Failed to set fragmentation threshold "
7291 "%d: %d (%s)", frag, ret, strerror(-ret));
7292 return ret;
7293}
7294
7295
7296static int i802_flush(void *priv)
7297{
7298 struct i802_bss *bss = priv;
7299 struct wpa_driver_nl80211_data *drv = bss->drv;
7300 struct nl_msg *msg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007301 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007302
7303 msg = nlmsg_alloc();
7304 if (!msg)
7305 return -1;
7306
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007307 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007308
7309 /*
7310 * XXX: FIX! this needs to flush all VLANs too
7311 */
7312 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
7313 if_nametoindex(bss->ifname));
7314
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007315 res = send_and_recv_msgs(drv, msg, NULL, NULL);
7316 if (res) {
7317 wpa_printf(MSG_DEBUG, "nl80211: Station flush failed: ret=%d "
7318 "(%s)", res, strerror(-res));
7319 }
7320 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007321 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007322 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007323 return -ENOBUFS;
7324}
7325
7326
7327static int get_sta_handler(struct nl_msg *msg, void *arg)
7328{
7329 struct nlattr *tb[NL80211_ATTR_MAX + 1];
7330 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
7331 struct hostap_sta_driver_data *data = arg;
7332 struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
7333 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
7334 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
7335 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
7336 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
7337 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
7338 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
Yuhao Zhengfcd6f212012-07-27 10:37:52 -07007339 [NL80211_STA_INFO_TX_FAILED] = { .type = NLA_U32 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007340 };
7341
7342 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
7343 genlmsg_attrlen(gnlh, 0), NULL);
7344
7345 /*
7346 * TODO: validate the interface and mac address!
7347 * Otherwise, there's a race condition as soon as
7348 * the kernel starts sending station notifications.
7349 */
7350
7351 if (!tb[NL80211_ATTR_STA_INFO]) {
7352 wpa_printf(MSG_DEBUG, "sta stats missing!");
7353 return NL_SKIP;
7354 }
7355 if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
7356 tb[NL80211_ATTR_STA_INFO],
7357 stats_policy)) {
7358 wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
7359 return NL_SKIP;
7360 }
7361
7362 if (stats[NL80211_STA_INFO_INACTIVE_TIME])
7363 data->inactive_msec =
7364 nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
7365 if (stats[NL80211_STA_INFO_RX_BYTES])
7366 data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
7367 if (stats[NL80211_STA_INFO_TX_BYTES])
7368 data->tx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
7369 if (stats[NL80211_STA_INFO_RX_PACKETS])
7370 data->rx_packets =
7371 nla_get_u32(stats[NL80211_STA_INFO_RX_PACKETS]);
7372 if (stats[NL80211_STA_INFO_TX_PACKETS])
7373 data->tx_packets =
7374 nla_get_u32(stats[NL80211_STA_INFO_TX_PACKETS]);
Yuhao Zhengfcd6f212012-07-27 10:37:52 -07007375 if (stats[NL80211_STA_INFO_TX_FAILED])
7376 data->tx_retry_failed =
7377 nla_get_u32(stats[NL80211_STA_INFO_TX_FAILED]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007378
7379 return NL_SKIP;
7380}
7381
7382static int i802_read_sta_data(void *priv, struct hostap_sta_driver_data *data,
7383 const u8 *addr)
7384{
7385 struct i802_bss *bss = priv;
7386 struct wpa_driver_nl80211_data *drv = bss->drv;
7387 struct nl_msg *msg;
7388
7389 os_memset(data, 0, sizeof(*data));
7390 msg = nlmsg_alloc();
7391 if (!msg)
7392 return -ENOMEM;
7393
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007394 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007395
7396 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
7397 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
7398
7399 return send_and_recv_msgs(drv, msg, get_sta_handler, data);
7400 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007401 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007402 return -ENOBUFS;
7403}
7404
7405
7406static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
7407 int cw_min, int cw_max, int burst_time)
7408{
7409 struct i802_bss *bss = priv;
7410 struct wpa_driver_nl80211_data *drv = bss->drv;
7411 struct nl_msg *msg;
7412 struct nlattr *txq, *params;
7413
7414 msg = nlmsg_alloc();
7415 if (!msg)
7416 return -1;
7417
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007418 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007419
7420 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
7421
7422 txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS);
7423 if (!txq)
7424 goto nla_put_failure;
7425
7426 /* We are only sending parameters for a single TXQ at a time */
7427 params = nla_nest_start(msg, 1);
7428 if (!params)
7429 goto nla_put_failure;
7430
7431 switch (queue) {
7432 case 0:
7433 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VO);
7434 break;
7435 case 1:
7436 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VI);
7437 break;
7438 case 2:
7439 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BE);
7440 break;
7441 case 3:
7442 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BK);
7443 break;
7444 }
7445 /* Burst time is configured in units of 0.1 msec and TXOP parameter in
7446 * 32 usec, so need to convert the value here. */
7447 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_TXOP, (burst_time * 100 + 16) / 32);
7448 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min);
7449 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max);
7450 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_AIFS, aifs);
7451
7452 nla_nest_end(msg, params);
7453
7454 nla_nest_end(msg, txq);
7455
7456 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
7457 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007458 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007459 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007460 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007461 return -1;
7462}
7463
7464
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007465static int i802_set_sta_vlan(void *priv, const u8 *addr,
7466 const char *ifname, int vlan_id)
7467{
7468 struct i802_bss *bss = priv;
7469 struct wpa_driver_nl80211_data *drv = bss->drv;
7470 struct nl_msg *msg;
7471 int ret = -ENOBUFS;
7472
7473 msg = nlmsg_alloc();
7474 if (!msg)
7475 return -ENOMEM;
7476
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007477 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007478
7479 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
7480 if_nametoindex(bss->ifname));
7481 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
7482 NLA_PUT_U32(msg, NL80211_ATTR_STA_VLAN,
7483 if_nametoindex(ifname));
7484
7485 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007486 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007487 if (ret < 0) {
7488 wpa_printf(MSG_ERROR, "nl80211: NL80211_ATTR_STA_VLAN (addr="
7489 MACSTR " ifname=%s vlan_id=%d) failed: %d (%s)",
7490 MAC2STR(addr), ifname, vlan_id, ret,
7491 strerror(-ret));
7492 }
7493 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007494 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007495 return ret;
7496}
7497
7498
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007499static int i802_get_inact_sec(void *priv, const u8 *addr)
7500{
7501 struct hostap_sta_driver_data data;
7502 int ret;
7503
7504 data.inactive_msec = (unsigned long) -1;
7505 ret = i802_read_sta_data(priv, &data, addr);
7506 if (ret || data.inactive_msec == (unsigned long) -1)
7507 return -1;
7508 return data.inactive_msec / 1000;
7509}
7510
7511
7512static int i802_sta_clear_stats(void *priv, const u8 *addr)
7513{
7514#if 0
7515 /* TODO */
7516#endif
7517 return 0;
7518}
7519
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007520
7521static int i802_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
7522 int reason)
7523{
7524 struct i802_bss *bss = priv;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007525 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007526 struct ieee80211_mgmt mgmt;
7527
Dmitry Shmidt04949592012-07-19 12:16:46 -07007528 if (drv->device_ap_sme)
7529 return wpa_driver_nl80211_sta_remove(bss, addr);
7530
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007531 memset(&mgmt, 0, sizeof(mgmt));
7532 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
7533 WLAN_FC_STYPE_DEAUTH);
7534 memcpy(mgmt.da, addr, ETH_ALEN);
7535 memcpy(mgmt.sa, own_addr, ETH_ALEN);
7536 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
7537 mgmt.u.deauth.reason_code = host_to_le16(reason);
7538 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
7539 IEEE80211_HDRLEN +
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007540 sizeof(mgmt.u.deauth), 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007541}
7542
7543
7544static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
7545 int reason)
7546{
7547 struct i802_bss *bss = priv;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007548 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007549 struct ieee80211_mgmt mgmt;
7550
Dmitry Shmidt04949592012-07-19 12:16:46 -07007551 if (drv->device_ap_sme)
7552 return wpa_driver_nl80211_sta_remove(bss, addr);
7553
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007554 memset(&mgmt, 0, sizeof(mgmt));
7555 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
7556 WLAN_FC_STYPE_DISASSOC);
7557 memcpy(mgmt.da, addr, ETH_ALEN);
7558 memcpy(mgmt.sa, own_addr, ETH_ALEN);
7559 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
7560 mgmt.u.disassoc.reason_code = host_to_le16(reason);
7561 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
7562 IEEE80211_HDRLEN +
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007563 sizeof(mgmt.u.disassoc), 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007564}
7565
7566#endif /* HOSTAPD || CONFIG_AP */
7567
7568#ifdef HOSTAPD
7569
Jouni Malinen75ecf522011-06-27 15:19:46 -07007570static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
7571{
7572 int i;
7573 int *old;
7574
7575 wpa_printf(MSG_DEBUG, "nl80211: Add own interface ifindex %d",
7576 ifidx);
7577 for (i = 0; i < drv->num_if_indices; i++) {
7578 if (drv->if_indices[i] == 0) {
7579 drv->if_indices[i] = ifidx;
7580 return;
7581 }
7582 }
7583
7584 if (drv->if_indices != drv->default_if_indices)
7585 old = drv->if_indices;
7586 else
7587 old = NULL;
7588
7589 drv->if_indices = os_realloc(old,
7590 sizeof(int) * (drv->num_if_indices + 1));
7591 if (!drv->if_indices) {
7592 if (!old)
7593 drv->if_indices = drv->default_if_indices;
7594 else
7595 drv->if_indices = old;
7596 wpa_printf(MSG_ERROR, "Failed to reallocate memory for "
7597 "interfaces");
7598 wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx);
7599 return;
7600 } else if (!old)
7601 os_memcpy(drv->if_indices, drv->default_if_indices,
7602 sizeof(drv->default_if_indices));
7603 drv->if_indices[drv->num_if_indices] = ifidx;
7604 drv->num_if_indices++;
7605}
7606
7607
7608static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
7609{
7610 int i;
7611
7612 for (i = 0; i < drv->num_if_indices; i++) {
7613 if (drv->if_indices[i] == ifidx) {
7614 drv->if_indices[i] = 0;
7615 break;
7616 }
7617 }
7618}
7619
7620
7621static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
7622{
7623 int i;
7624
7625 for (i = 0; i < drv->num_if_indices; i++)
7626 if (drv->if_indices[i] == ifidx)
7627 return 1;
7628
7629 return 0;
7630}
7631
7632
7633static int i802_set_wds_sta(void *priv, const u8 *addr, int aid, int val,
7634 const char *bridge_ifname)
7635{
7636 struct i802_bss *bss = priv;
7637 struct wpa_driver_nl80211_data *drv = bss->drv;
7638 char name[IFNAMSIZ + 1];
7639
7640 os_snprintf(name, sizeof(name), "%s.sta%d", bss->ifname, aid);
7641 wpa_printf(MSG_DEBUG, "nl80211: Set WDS STA addr=" MACSTR
7642 " aid=%d val=%d name=%s", MAC2STR(addr), aid, val, name);
7643 if (val) {
7644 if (!if_nametoindex(name)) {
7645 if (nl80211_create_iface(drv, name,
7646 NL80211_IFTYPE_AP_VLAN,
7647 NULL, 1) < 0)
7648 return -1;
7649 if (bridge_ifname &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007650 linux_br_add_if(drv->global->ioctl_sock,
7651 bridge_ifname, name) < 0)
Jouni Malinen75ecf522011-06-27 15:19:46 -07007652 return -1;
7653 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007654 linux_set_iface_flags(drv->global->ioctl_sock, name, 1);
Jouni Malinen75ecf522011-06-27 15:19:46 -07007655 return i802_set_sta_vlan(priv, addr, name, 0);
7656 } else {
7657 i802_set_sta_vlan(priv, addr, bss->ifname, 0);
7658 return wpa_driver_nl80211_if_remove(priv, WPA_IF_AP_VLAN,
7659 name);
7660 }
7661}
7662
7663
7664static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
7665{
7666 struct wpa_driver_nl80211_data *drv = eloop_ctx;
7667 struct sockaddr_ll lladdr;
7668 unsigned char buf[3000];
7669 int len;
7670 socklen_t fromlen = sizeof(lladdr);
7671
7672 len = recvfrom(sock, buf, sizeof(buf), 0,
7673 (struct sockaddr *)&lladdr, &fromlen);
7674 if (len < 0) {
7675 perror("recv");
7676 return;
7677 }
7678
7679 if (have_ifidx(drv, lladdr.sll_ifindex))
7680 drv_event_eapol_rx(drv->ctx, lladdr.sll_addr, buf, len);
7681}
7682
7683
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007684static int i802_check_bridge(struct wpa_driver_nl80211_data *drv,
7685 struct i802_bss *bss,
7686 const char *brname, const char *ifname)
7687{
7688 int ifindex;
7689 char in_br[IFNAMSIZ];
7690
7691 os_strlcpy(bss->brname, brname, IFNAMSIZ);
7692 ifindex = if_nametoindex(brname);
7693 if (ifindex == 0) {
7694 /*
7695 * Bridge was configured, but the bridge device does
7696 * not exist. Try to add it now.
7697 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007698 if (linux_br_add(drv->global->ioctl_sock, brname) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007699 wpa_printf(MSG_ERROR, "nl80211: Failed to add the "
7700 "bridge interface %s: %s",
7701 brname, strerror(errno));
7702 return -1;
7703 }
7704 bss->added_bridge = 1;
7705 add_ifidx(drv, if_nametoindex(brname));
7706 }
7707
7708 if (linux_br_get(in_br, ifname) == 0) {
7709 if (os_strcmp(in_br, brname) == 0)
7710 return 0; /* already in the bridge */
7711
7712 wpa_printf(MSG_DEBUG, "nl80211: Removing interface %s from "
7713 "bridge %s", ifname, in_br);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007714 if (linux_br_del_if(drv->global->ioctl_sock, in_br, ifname) <
7715 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007716 wpa_printf(MSG_ERROR, "nl80211: Failed to "
7717 "remove interface %s from bridge "
7718 "%s: %s",
7719 ifname, brname, strerror(errno));
7720 return -1;
7721 }
7722 }
7723
7724 wpa_printf(MSG_DEBUG, "nl80211: Adding interface %s into bridge %s",
7725 ifname, brname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007726 if (linux_br_add_if(drv->global->ioctl_sock, brname, ifname) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007727 wpa_printf(MSG_ERROR, "nl80211: Failed to add interface %s "
7728 "into bridge %s: %s",
7729 ifname, brname, strerror(errno));
7730 return -1;
7731 }
7732 bss->added_if_into_bridge = 1;
7733
7734 return 0;
7735}
7736
7737
7738static void *i802_init(struct hostapd_data *hapd,
7739 struct wpa_init_params *params)
7740{
7741 struct wpa_driver_nl80211_data *drv;
7742 struct i802_bss *bss;
7743 size_t i;
7744 char brname[IFNAMSIZ];
7745 int ifindex, br_ifindex;
7746 int br_added = 0;
7747
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007748 bss = wpa_driver_nl80211_init(hapd, params->ifname,
7749 params->global_priv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007750 if (bss == NULL)
7751 return NULL;
7752
7753 drv = bss->drv;
7754 drv->nlmode = NL80211_IFTYPE_AP;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007755 drv->eapol_sock = -1;
7756
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007757 if (linux_br_get(brname, params->ifname) == 0) {
7758 wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in bridge %s",
7759 params->ifname, brname);
7760 br_ifindex = if_nametoindex(brname);
7761 } else {
7762 brname[0] = '\0';
7763 br_ifindex = 0;
7764 }
7765
7766 drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int);
7767 drv->if_indices = drv->default_if_indices;
7768 for (i = 0; i < params->num_bridge; i++) {
7769 if (params->bridge[i]) {
7770 ifindex = if_nametoindex(params->bridge[i]);
7771 if (ifindex)
7772 add_ifidx(drv, ifindex);
7773 if (ifindex == br_ifindex)
7774 br_added = 1;
7775 }
7776 }
7777 if (!br_added && br_ifindex &&
7778 (params->num_bridge == 0 || !params->bridge[0]))
7779 add_ifidx(drv, br_ifindex);
7780
7781 /* start listening for EAPOL on the default AP interface */
7782 add_ifidx(drv, drv->ifindex);
7783
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007784 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007785 goto failed;
7786
7787 if (params->bssid) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007788 if (linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007789 params->bssid))
7790 goto failed;
7791 }
7792
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007793 if (wpa_driver_nl80211_set_mode(bss, drv->nlmode)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007794 wpa_printf(MSG_ERROR, "nl80211: Failed to set interface %s "
7795 "into AP mode", bss->ifname);
7796 goto failed;
7797 }
7798
7799 if (params->num_bridge && params->bridge[0] &&
7800 i802_check_bridge(drv, bss, params->bridge[0], params->ifname) < 0)
7801 goto failed;
7802
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007803 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007804 goto failed;
7805
7806 drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
7807 if (drv->eapol_sock < 0) {
7808 perror("socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE)");
7809 goto failed;
7810 }
7811
7812 if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
7813 {
7814 printf("Could not register read socket for eapol\n");
7815 goto failed;
7816 }
7817
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007818 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
7819 params->own_addr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007820 goto failed;
7821
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007822 memcpy(bss->addr, params->own_addr, ETH_ALEN);
7823
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007824 return bss;
7825
7826failed:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007827 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007828 return NULL;
7829}
7830
7831
7832static void i802_deinit(void *priv)
7833{
7834 wpa_driver_nl80211_deinit(priv);
7835}
7836
7837#endif /* HOSTAPD */
7838
7839
7840static enum nl80211_iftype wpa_driver_nl80211_if_type(
7841 enum wpa_driver_if_type type)
7842{
7843 switch (type) {
7844 case WPA_IF_STATION:
7845 return NL80211_IFTYPE_STATION;
7846 case WPA_IF_P2P_CLIENT:
7847 case WPA_IF_P2P_GROUP:
7848 return NL80211_IFTYPE_P2P_CLIENT;
7849 case WPA_IF_AP_VLAN:
7850 return NL80211_IFTYPE_AP_VLAN;
7851 case WPA_IF_AP_BSS:
7852 return NL80211_IFTYPE_AP;
7853 case WPA_IF_P2P_GO:
7854 return NL80211_IFTYPE_P2P_GO;
7855 }
7856 return -1;
7857}
7858
7859
7860#ifdef CONFIG_P2P
7861
7862static int nl80211_addr_in_use(struct nl80211_global *global, const u8 *addr)
7863{
7864 struct wpa_driver_nl80211_data *drv;
7865 dl_list_for_each(drv, &global->interfaces,
7866 struct wpa_driver_nl80211_data, list) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007867 if (os_memcmp(addr, drv->first_bss.addr, ETH_ALEN) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007868 return 1;
7869 }
7870 return 0;
7871}
7872
7873
7874static int nl80211_p2p_interface_addr(struct wpa_driver_nl80211_data *drv,
7875 u8 *new_addr)
7876{
7877 unsigned int idx;
7878
7879 if (!drv->global)
7880 return -1;
7881
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007882 os_memcpy(new_addr, drv->first_bss.addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007883 for (idx = 0; idx < 64; idx++) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007884 new_addr[0] = drv->first_bss.addr[0] | 0x02;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007885 new_addr[0] ^= idx << 2;
7886 if (!nl80211_addr_in_use(drv->global, new_addr))
7887 break;
7888 }
7889 if (idx == 64)
7890 return -1;
7891
7892 wpa_printf(MSG_DEBUG, "nl80211: Assigned new P2P Interface Address "
7893 MACSTR, MAC2STR(new_addr));
7894
7895 return 0;
7896}
7897
7898#endif /* CONFIG_P2P */
7899
7900
7901static int wpa_driver_nl80211_if_add(void *priv, enum wpa_driver_if_type type,
7902 const char *ifname, const u8 *addr,
7903 void *bss_ctx, void **drv_priv,
7904 char *force_ifname, u8 *if_addr,
7905 const char *bridge)
7906{
7907 struct i802_bss *bss = priv;
7908 struct wpa_driver_nl80211_data *drv = bss->drv;
7909 int ifidx;
7910#ifdef HOSTAPD
7911 struct i802_bss *new_bss = NULL;
7912
7913 if (type == WPA_IF_AP_BSS) {
7914 new_bss = os_zalloc(sizeof(*new_bss));
7915 if (new_bss == NULL)
7916 return -1;
7917 }
7918#endif /* HOSTAPD */
7919
7920 if (addr)
7921 os_memcpy(if_addr, addr, ETH_ALEN);
7922 ifidx = nl80211_create_iface(drv, ifname,
7923 wpa_driver_nl80211_if_type(type), addr,
7924 0);
7925 if (ifidx < 0) {
7926#ifdef HOSTAPD
7927 os_free(new_bss);
7928#endif /* HOSTAPD */
7929 return -1;
7930 }
7931
7932 if (!addr &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007933 linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
7934 if_addr) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007935 nl80211_remove_iface(drv, ifidx);
7936 return -1;
7937 }
7938
7939#ifdef CONFIG_P2P
7940 if (!addr &&
7941 (type == WPA_IF_P2P_CLIENT || type == WPA_IF_P2P_GROUP ||
7942 type == WPA_IF_P2P_GO)) {
7943 /* Enforce unique P2P Interface Address */
7944 u8 new_addr[ETH_ALEN], own_addr[ETH_ALEN];
7945
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007946 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
7947 own_addr) < 0 ||
7948 linux_get_ifhwaddr(drv->global->ioctl_sock, ifname,
7949 new_addr) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007950 nl80211_remove_iface(drv, ifidx);
7951 return -1;
7952 }
7953 if (os_memcmp(own_addr, new_addr, ETH_ALEN) == 0) {
7954 wpa_printf(MSG_DEBUG, "nl80211: Allocate new address "
7955 "for P2P group interface");
7956 if (nl80211_p2p_interface_addr(drv, new_addr) < 0) {
7957 nl80211_remove_iface(drv, ifidx);
7958 return -1;
7959 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007960 if (linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007961 new_addr) < 0) {
7962 nl80211_remove_iface(drv, ifidx);
7963 return -1;
7964 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007965 }
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07007966 os_memcpy(if_addr, new_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007967 }
7968#endif /* CONFIG_P2P */
7969
7970#ifdef HOSTAPD
7971 if (bridge &&
7972 i802_check_bridge(drv, new_bss, bridge, ifname) < 0) {
7973 wpa_printf(MSG_ERROR, "nl80211: Failed to add the new "
7974 "interface %s to a bridge %s", ifname, bridge);
7975 nl80211_remove_iface(drv, ifidx);
7976 os_free(new_bss);
7977 return -1;
7978 }
7979
7980 if (type == WPA_IF_AP_BSS) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007981 if (linux_set_iface_flags(drv->global->ioctl_sock, ifname, 1))
7982 {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007983 nl80211_remove_iface(drv, ifidx);
7984 os_free(new_bss);
7985 return -1;
7986 }
7987 os_strlcpy(new_bss->ifname, ifname, IFNAMSIZ);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007988 os_memcpy(new_bss->addr, if_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007989 new_bss->ifindex = ifidx;
7990 new_bss->drv = drv;
7991 new_bss->next = drv->first_bss.next;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007992 new_bss->freq = drv->first_bss.freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007993 drv->first_bss.next = new_bss;
7994 if (drv_priv)
7995 *drv_priv = new_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007996 nl80211_init_bss(new_bss);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007997
7998 /* Subscribe management frames for this WPA_IF_AP_BSS */
7999 if (nl80211_setup_ap(new_bss))
8000 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008001 }
8002#endif /* HOSTAPD */
8003
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008004 if (drv->global)
8005 drv->global->if_add_ifindex = ifidx;
8006
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008007 return 0;
8008}
8009
8010
8011static int wpa_driver_nl80211_if_remove(void *priv,
8012 enum wpa_driver_if_type type,
8013 const char *ifname)
8014{
8015 struct i802_bss *bss = priv;
8016 struct wpa_driver_nl80211_data *drv = bss->drv;
8017 int ifindex = if_nametoindex(ifname);
8018
8019 wpa_printf(MSG_DEBUG, "nl80211: %s(type=%d ifname=%s) ifindex=%d",
8020 __func__, type, ifname, ifindex);
8021 if (ifindex <= 0)
8022 return -1;
8023
8024#ifdef HOSTAPD
8025 if (bss->added_if_into_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008026 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
8027 bss->ifname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008028 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
8029 "interface %s from bridge %s: %s",
8030 bss->ifname, bss->brname, strerror(errno));
8031 }
8032 if (bss->added_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008033 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008034 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
8035 "bridge %s: %s",
8036 bss->brname, strerror(errno));
8037 }
8038#endif /* HOSTAPD */
8039
8040 nl80211_remove_iface(drv, ifindex);
8041
8042#ifdef HOSTAPD
8043 if (type != WPA_IF_AP_BSS)
8044 return 0;
8045
8046 if (bss != &drv->first_bss) {
8047 struct i802_bss *tbss;
8048
8049 for (tbss = &drv->first_bss; tbss; tbss = tbss->next) {
8050 if (tbss->next == bss) {
8051 tbss->next = bss->next;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008052 /* Unsubscribe management frames */
8053 nl80211_teardown_ap(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008054 nl80211_destroy_bss(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008055 os_free(bss);
8056 bss = NULL;
8057 break;
8058 }
8059 }
8060 if (bss)
8061 wpa_printf(MSG_INFO, "nl80211: %s - could not find "
8062 "BSS %p in the list", __func__, bss);
8063 }
8064#endif /* HOSTAPD */
8065
8066 return 0;
8067}
8068
8069
8070static int cookie_handler(struct nl_msg *msg, void *arg)
8071{
8072 struct nlattr *tb[NL80211_ATTR_MAX + 1];
8073 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
8074 u64 *cookie = arg;
8075 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
8076 genlmsg_attrlen(gnlh, 0), NULL);
8077 if (tb[NL80211_ATTR_COOKIE])
8078 *cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
8079 return NL_SKIP;
8080}
8081
8082
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008083static int nl80211_send_frame_cmd(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008084 unsigned int freq, unsigned int wait,
8085 const u8 *buf, size_t buf_len,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008086 u64 *cookie_out, int no_cck, int no_ack,
8087 int offchanok)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008088{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008089 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008090 struct nl_msg *msg;
8091 u64 cookie;
8092 int ret = -1;
8093
8094 msg = nlmsg_alloc();
8095 if (!msg)
8096 return -1;
8097
Dmitry Shmidt04949592012-07-19 12:16:46 -07008098 wpa_printf(MSG_DEBUG, "nl80211: CMD_FRAME freq=%u wait=%u no_cck=%d "
8099 "no_ack=%d offchanok=%d",
8100 freq, wait, no_cck, no_ack, offchanok);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008101 nl80211_cmd(drv, msg, 0, NL80211_CMD_FRAME);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008102
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008103 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008104 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07008105 if (wait)
8106 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, wait);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008107 if (offchanok && (drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008108 NLA_PUT_FLAG(msg, NL80211_ATTR_OFFCHANNEL_TX_OK);
8109 if (no_cck)
8110 NLA_PUT_FLAG(msg, NL80211_ATTR_TX_NO_CCK_RATE);
8111 if (no_ack)
8112 NLA_PUT_FLAG(msg, NL80211_ATTR_DONT_WAIT_FOR_ACK);
8113
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008114 NLA_PUT(msg, NL80211_ATTR_FRAME, buf_len, buf);
8115
8116 cookie = 0;
8117 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
8118 msg = NULL;
8119 if (ret) {
8120 wpa_printf(MSG_DEBUG, "nl80211: Frame command failed: ret=%d "
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07008121 "(%s) (freq=%u wait=%u)", ret, strerror(-ret),
8122 freq, wait);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008123 goto nla_put_failure;
8124 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008125 wpa_printf(MSG_DEBUG, "nl80211: Frame TX command accepted%s; "
8126 "cookie 0x%llx", no_ack ? " (no ACK)" : "",
8127 (long long unsigned int) cookie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008128
8129 if (cookie_out)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008130 *cookie_out = no_ack ? (u64) -1 : cookie;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008131
8132nla_put_failure:
8133 nlmsg_free(msg);
8134 return ret;
8135}
8136
8137
8138static int wpa_driver_nl80211_send_action(void *priv, unsigned int freq,
8139 unsigned int wait_time,
8140 const u8 *dst, const u8 *src,
8141 const u8 *bssid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008142 const u8 *data, size_t data_len,
8143 int no_cck)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008144{
8145 struct i802_bss *bss = priv;
8146 struct wpa_driver_nl80211_data *drv = bss->drv;
8147 int ret = -1;
8148 u8 *buf;
8149 struct ieee80211_hdr *hdr;
8150
8151 wpa_printf(MSG_DEBUG, "nl80211: Send Action frame (ifindex=%d, "
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008152 "freq=%u MHz wait=%d ms no_cck=%d)",
8153 drv->ifindex, freq, wait_time, no_cck);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008154
8155 buf = os_zalloc(24 + data_len);
8156 if (buf == NULL)
8157 return ret;
8158 os_memcpy(buf + 24, data, data_len);
8159 hdr = (struct ieee80211_hdr *) buf;
8160 hdr->frame_control =
8161 IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_ACTION);
8162 os_memcpy(hdr->addr1, dst, ETH_ALEN);
8163 os_memcpy(hdr->addr2, src, ETH_ALEN);
8164 os_memcpy(hdr->addr3, bssid, ETH_ALEN);
8165
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008166 if (is_ap_interface(drv->nlmode))
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008167 ret = wpa_driver_nl80211_send_mlme_freq(priv, buf,
8168 24 + data_len,
8169 0, freq, no_cck, 1,
8170 wait_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008171 else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008172 ret = nl80211_send_frame_cmd(bss, freq, wait_time, buf,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008173 24 + data_len,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008174 &drv->send_action_cookie,
8175 no_cck, 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008176
8177 os_free(buf);
8178 return ret;
8179}
8180
8181
8182static void wpa_driver_nl80211_send_action_cancel_wait(void *priv)
8183{
8184 struct i802_bss *bss = priv;
8185 struct wpa_driver_nl80211_data *drv = bss->drv;
8186 struct nl_msg *msg;
8187 int ret;
8188
8189 msg = nlmsg_alloc();
8190 if (!msg)
8191 return;
8192
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008193 nl80211_cmd(drv, msg, 0, NL80211_CMD_FRAME_WAIT_CANCEL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008194
8195 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
8196 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->send_action_cookie);
8197
8198 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8199 msg = NULL;
8200 if (ret)
8201 wpa_printf(MSG_DEBUG, "nl80211: wait cancel failed: ret=%d "
8202 "(%s)", ret, strerror(-ret));
8203
8204 nla_put_failure:
8205 nlmsg_free(msg);
8206}
8207
8208
8209static int wpa_driver_nl80211_remain_on_channel(void *priv, unsigned int freq,
8210 unsigned int duration)
8211{
8212 struct i802_bss *bss = priv;
8213 struct wpa_driver_nl80211_data *drv = bss->drv;
8214 struct nl_msg *msg;
8215 int ret;
8216 u64 cookie;
8217
8218 msg = nlmsg_alloc();
8219 if (!msg)
8220 return -1;
8221
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008222 nl80211_cmd(drv, msg, 0, NL80211_CMD_REMAIN_ON_CHANNEL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008223
8224 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
8225 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
8226 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
8227
8228 cookie = 0;
8229 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008230 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008231 if (ret == 0) {
8232 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel cookie "
8233 "0x%llx for freq=%u MHz duration=%u",
8234 (long long unsigned int) cookie, freq, duration);
8235 drv->remain_on_chan_cookie = cookie;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008236 drv->pending_remain_on_chan = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008237 return 0;
8238 }
8239 wpa_printf(MSG_DEBUG, "nl80211: Failed to request remain-on-channel "
8240 "(freq=%d duration=%u): %d (%s)",
8241 freq, duration, ret, strerror(-ret));
8242nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008243 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008244 return -1;
8245}
8246
8247
8248static int wpa_driver_nl80211_cancel_remain_on_channel(void *priv)
8249{
8250 struct i802_bss *bss = priv;
8251 struct wpa_driver_nl80211_data *drv = bss->drv;
8252 struct nl_msg *msg;
8253 int ret;
8254
8255 if (!drv->pending_remain_on_chan) {
8256 wpa_printf(MSG_DEBUG, "nl80211: No pending remain-on-channel "
8257 "to cancel");
8258 return -1;
8259 }
8260
8261 wpa_printf(MSG_DEBUG, "nl80211: Cancel remain-on-channel with cookie "
8262 "0x%llx",
8263 (long long unsigned int) drv->remain_on_chan_cookie);
8264
8265 msg = nlmsg_alloc();
8266 if (!msg)
8267 return -1;
8268
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008269 nl80211_cmd(drv, msg, 0, NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008270
8271 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
8272 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->remain_on_chan_cookie);
8273
8274 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008275 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008276 if (ret == 0)
8277 return 0;
8278 wpa_printf(MSG_DEBUG, "nl80211: Failed to cancel remain-on-channel: "
8279 "%d (%s)", ret, strerror(-ret));
8280nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008281 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008282 return -1;
8283}
8284
8285
8286static int wpa_driver_nl80211_probe_req_report(void *priv, int report)
8287{
8288 struct i802_bss *bss = priv;
8289 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07008290
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008291 if (!report) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008292 if (bss->nl_preq && drv->device_ap_sme &&
8293 is_ap_interface(drv->nlmode)) {
8294 /*
8295 * Do not disable Probe Request reporting that was
8296 * enabled in nl80211_setup_ap().
8297 */
8298 wpa_printf(MSG_DEBUG, "nl80211: Skip disabling of "
8299 "Probe Request reporting nl_preq=%p while "
8300 "in AP mode", bss->nl_preq);
8301 } else if (bss->nl_preq) {
8302 wpa_printf(MSG_DEBUG, "nl80211: Disable Probe Request "
8303 "reporting nl_preq=%p", bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008304 eloop_unregister_read_sock(
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008305 nl_socket_get_fd(bss->nl_preq));
8306 nl_destroy_handles(&bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008307 }
8308 return 0;
8309 }
8310
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008311 if (bss->nl_preq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008312 wpa_printf(MSG_DEBUG, "nl80211: Probe Request reporting "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008313 "already on! nl_preq=%p", bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008314 return 0;
8315 }
8316
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008317 bss->nl_preq = nl_create_handle(drv->global->nl_cb, "preq");
8318 if (bss->nl_preq == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008319 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008320 wpa_printf(MSG_DEBUG, "nl80211: Enable Probe Request "
8321 "reporting nl_preq=%p", bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008322
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008323 if (nl80211_register_frame(bss, bss->nl_preq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008324 (WLAN_FC_TYPE_MGMT << 2) |
8325 (WLAN_FC_STYPE_PROBE_REQ << 4),
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008326 NULL, 0) < 0)
8327 goto out_err;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07008328
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008329 eloop_register_read_sock(nl_socket_get_fd(bss->nl_preq),
8330 wpa_driver_nl80211_event_receive, bss->nl_cb,
8331 bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008332
8333 return 0;
8334
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008335 out_err:
8336 nl_destroy_handles(&bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008337 return -1;
8338}
8339
8340
8341static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
8342 int ifindex, int disabled)
8343{
8344 struct nl_msg *msg;
8345 struct nlattr *bands, *band;
8346 int ret;
8347
8348 msg = nlmsg_alloc();
8349 if (!msg)
8350 return -1;
8351
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008352 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_TX_BITRATE_MASK);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008353 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
8354
8355 bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES);
8356 if (!bands)
8357 goto nla_put_failure;
8358
8359 /*
8360 * Disable 2 GHz rates 1, 2, 5.5, 11 Mbps by masking out everything
8361 * else apart from 6, 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS
8362 * rates. All 5 GHz rates are left enabled.
8363 */
8364 band = nla_nest_start(msg, NL80211_BAND_2GHZ);
8365 if (!band)
8366 goto nla_put_failure;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008367 if (disabled) {
8368 NLA_PUT(msg, NL80211_TXRATE_LEGACY, 8,
8369 "\x0c\x12\x18\x24\x30\x48\x60\x6c");
8370 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008371 nla_nest_end(msg, band);
8372
8373 nla_nest_end(msg, bands);
8374
8375 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8376 msg = NULL;
8377 if (ret) {
8378 wpa_printf(MSG_DEBUG, "nl80211: Set TX rates failed: ret=%d "
8379 "(%s)", ret, strerror(-ret));
8380 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008381 return ret;
8382
8383nla_put_failure:
8384 nlmsg_free(msg);
8385 return -1;
8386}
8387
8388
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008389static int wpa_driver_nl80211_deinit_ap(void *priv)
8390{
8391 struct i802_bss *bss = priv;
8392 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008393 if (!is_ap_interface(drv->nlmode))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008394 return -1;
8395 wpa_driver_nl80211_del_beacon(drv);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008396 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008397}
8398
8399
Dmitry Shmidt04949592012-07-19 12:16:46 -07008400static int wpa_driver_nl80211_deinit_p2p_cli(void *priv)
8401{
8402 struct i802_bss *bss = priv;
8403 struct wpa_driver_nl80211_data *drv = bss->drv;
8404 if (drv->nlmode != NL80211_IFTYPE_P2P_CLIENT)
8405 return -1;
8406 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
8407}
8408
8409
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008410static void wpa_driver_nl80211_resume(void *priv)
8411{
8412 struct i802_bss *bss = priv;
8413 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008414 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008415 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface up on "
8416 "resume event");
8417 }
8418}
8419
8420
8421static int nl80211_send_ft_action(void *priv, u8 action, const u8 *target_ap,
8422 const u8 *ies, size_t ies_len)
8423{
8424 struct i802_bss *bss = priv;
8425 struct wpa_driver_nl80211_data *drv = bss->drv;
8426 int ret;
8427 u8 *data, *pos;
8428 size_t data_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008429 const u8 *own_addr = bss->addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008430
8431 if (action != 1) {
8432 wpa_printf(MSG_ERROR, "nl80211: Unsupported send_ft_action "
8433 "action %d", action);
8434 return -1;
8435 }
8436
8437 /*
8438 * Action frame payload:
8439 * Category[1] = 6 (Fast BSS Transition)
8440 * Action[1] = 1 (Fast BSS Transition Request)
8441 * STA Address
8442 * Target AP Address
8443 * FT IEs
8444 */
8445
8446 data_len = 2 + 2 * ETH_ALEN + ies_len;
8447 data = os_malloc(data_len);
8448 if (data == NULL)
8449 return -1;
8450 pos = data;
8451 *pos++ = 0x06; /* FT Action category */
8452 *pos++ = action;
8453 os_memcpy(pos, own_addr, ETH_ALEN);
8454 pos += ETH_ALEN;
8455 os_memcpy(pos, target_ap, ETH_ALEN);
8456 pos += ETH_ALEN;
8457 os_memcpy(pos, ies, ies_len);
8458
8459 ret = wpa_driver_nl80211_send_action(bss, drv->assoc_freq, 0,
8460 drv->bssid, own_addr, drv->bssid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008461 data, data_len, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008462 os_free(data);
8463
8464 return ret;
8465}
8466
8467
8468static int nl80211_signal_monitor(void *priv, int threshold, int hysteresis)
8469{
8470 struct i802_bss *bss = priv;
8471 struct wpa_driver_nl80211_data *drv = bss->drv;
8472 struct nl_msg *msg, *cqm = NULL;
8473
8474 wpa_printf(MSG_DEBUG, "nl80211: Signal monitor threshold=%d "
8475 "hysteresis=%d", threshold, hysteresis);
8476
8477 msg = nlmsg_alloc();
8478 if (!msg)
8479 return -1;
8480
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008481 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_CQM);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008482
8483 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
8484
8485 cqm = nlmsg_alloc();
8486 if (cqm == NULL)
8487 return -1;
8488
8489 NLA_PUT_U32(cqm, NL80211_ATTR_CQM_RSSI_THOLD, threshold);
8490 NLA_PUT_U32(cqm, NL80211_ATTR_CQM_RSSI_HYST, hysteresis);
8491 nla_put_nested(msg, NL80211_ATTR_CQM, cqm);
8492
8493 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
8494 return 0;
8495 msg = NULL;
8496
8497nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008498 nlmsg_free(cqm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008499 nlmsg_free(msg);
8500 return -1;
8501}
8502
8503
8504static int nl80211_signal_poll(void *priv, struct wpa_signal_info *si)
8505{
8506 struct i802_bss *bss = priv;
8507 struct wpa_driver_nl80211_data *drv = bss->drv;
8508 int res;
8509
8510 os_memset(si, 0, sizeof(*si));
8511 res = nl80211_get_link_signal(drv, si);
8512 if (res != 0)
8513 return res;
8514
8515 return nl80211_get_link_noise(drv, si);
8516}
8517
8518
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008519static int wpa_driver_nl80211_shared_freq(void *priv)
8520{
8521 struct i802_bss *bss = priv;
8522 struct wpa_driver_nl80211_data *drv = bss->drv;
8523 struct wpa_driver_nl80211_data *driver;
8524 int freq = 0;
8525
8526 /*
8527 * If the same PHY is in connected state with some other interface,
8528 * then retrieve the assoc freq.
8529 */
8530 wpa_printf(MSG_DEBUG, "nl80211: Get shared freq for PHY %s",
8531 drv->phyname);
8532
8533 dl_list_for_each(driver, &drv->global->interfaces,
8534 struct wpa_driver_nl80211_data, list) {
8535 if (drv == driver ||
8536 os_strcmp(drv->phyname, driver->phyname) != 0 ||
8537#ifdef ANDROID_P2P
8538 (!driver->associated && !is_ap_interface(driver->nlmode)))
8539#else
8540 !driver->associated)
8541#endif
8542 continue;
8543
8544 wpa_printf(MSG_DEBUG, "nl80211: Found a match for PHY %s - %s "
8545 MACSTR,
8546 driver->phyname, driver->first_bss.ifname,
8547 MAC2STR(driver->first_bss.addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07008548 if (is_ap_interface(driver->nlmode))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008549 freq = driver->first_bss.freq;
8550 else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008551 freq = nl80211_get_assoc_freq(driver);
8552 wpa_printf(MSG_DEBUG, "nl80211: Shared freq for PHY %s: %d",
8553 drv->phyname, freq);
8554 }
8555
8556 if (!freq)
8557 wpa_printf(MSG_DEBUG, "nl80211: No shared interface for "
8558 "PHY (%s) in associated state", drv->phyname);
8559
8560 return freq;
8561}
8562
8563
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008564static int nl80211_send_frame(void *priv, const u8 *data, size_t data_len,
8565 int encrypt)
8566{
8567 struct i802_bss *bss = priv;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008568 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt, 0,
8569 0, 0, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008570}
8571
8572
8573static int nl80211_set_param(void *priv, const char *param)
8574{
8575 wpa_printf(MSG_DEBUG, "nl80211: driver param='%s'", param);
8576 if (param == NULL)
8577 return 0;
8578
8579#ifdef CONFIG_P2P
8580 if (os_strstr(param, "use_p2p_group_interface=1")) {
8581 struct i802_bss *bss = priv;
8582 struct wpa_driver_nl80211_data *drv = bss->drv;
8583
8584 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
8585 "interface");
8586 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
8587 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
8588 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008589#ifdef ANDROID_P2P
8590 if(os_strstr(param, "use_multi_chan_concurrent=1")) {
8591 struct i802_bss *bss = priv;
8592 struct wpa_driver_nl80211_data *drv = bss->drv;
8593 wpa_printf(MSG_DEBUG, "nl80211: Use Multi channel "
8594 "concurrency");
8595 drv->capa.flags |= WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT;
8596 }
8597#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008598#endif /* CONFIG_P2P */
8599
8600 return 0;
8601}
8602
8603
8604static void * nl80211_global_init(void)
8605{
8606 struct nl80211_global *global;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008607 struct netlink_config *cfg;
8608
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008609 global = os_zalloc(sizeof(*global));
8610 if (global == NULL)
8611 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008612 global->ioctl_sock = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008613 dl_list_init(&global->interfaces);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008614 global->if_add_ifindex = -1;
8615
8616 cfg = os_zalloc(sizeof(*cfg));
8617 if (cfg == NULL)
8618 goto err;
8619
8620 cfg->ctx = global;
8621 cfg->newlink_cb = wpa_driver_nl80211_event_rtm_newlink;
8622 cfg->dellink_cb = wpa_driver_nl80211_event_rtm_dellink;
8623 global->netlink = netlink_init(cfg);
8624 if (global->netlink == NULL) {
8625 os_free(cfg);
8626 goto err;
8627 }
8628
8629 if (wpa_driver_nl80211_init_nl_global(global) < 0)
8630 goto err;
8631
8632 global->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
8633 if (global->ioctl_sock < 0) {
8634 perror("socket(PF_INET,SOCK_DGRAM)");
8635 goto err;
8636 }
8637
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008638 return global;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008639
8640err:
8641 nl80211_global_deinit(global);
8642 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008643}
8644
8645
8646static void nl80211_global_deinit(void *priv)
8647{
8648 struct nl80211_global *global = priv;
8649 if (global == NULL)
8650 return;
8651 if (!dl_list_empty(&global->interfaces)) {
8652 wpa_printf(MSG_ERROR, "nl80211: %u interface(s) remain at "
8653 "nl80211_global_deinit",
8654 dl_list_len(&global->interfaces));
8655 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008656
8657 if (global->netlink)
8658 netlink_deinit(global->netlink);
8659
8660 nl_destroy_handles(&global->nl);
8661
8662 if (global->nl_event) {
8663 eloop_unregister_read_sock(
8664 nl_socket_get_fd(global->nl_event));
8665 nl_destroy_handles(&global->nl_event);
8666 }
8667
8668 nl_cb_put(global->nl_cb);
8669
8670 if (global->ioctl_sock >= 0)
8671 close(global->ioctl_sock);
8672
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008673 os_free(global);
8674}
8675
8676
8677static const char * nl80211_get_radio_name(void *priv)
8678{
8679 struct i802_bss *bss = priv;
8680 struct wpa_driver_nl80211_data *drv = bss->drv;
8681 return drv->phyname;
8682}
8683
8684
Jouni Malinen75ecf522011-06-27 15:19:46 -07008685static int nl80211_pmkid(struct i802_bss *bss, int cmd, const u8 *bssid,
8686 const u8 *pmkid)
8687{
8688 struct nl_msg *msg;
8689
8690 msg = nlmsg_alloc();
8691 if (!msg)
8692 return -ENOMEM;
8693
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008694 nl80211_cmd(bss->drv, msg, 0, cmd);
Jouni Malinen75ecf522011-06-27 15:19:46 -07008695
8696 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
8697 if (pmkid)
8698 NLA_PUT(msg, NL80211_ATTR_PMKID, 16, pmkid);
8699 if (bssid)
8700 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
8701
8702 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
8703 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008704 nlmsg_free(msg);
Jouni Malinen75ecf522011-06-27 15:19:46 -07008705 return -ENOBUFS;
8706}
8707
8708
8709static int nl80211_add_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
8710{
8711 struct i802_bss *bss = priv;
8712 wpa_printf(MSG_DEBUG, "nl80211: Add PMKID for " MACSTR, MAC2STR(bssid));
8713 return nl80211_pmkid(bss, NL80211_CMD_SET_PMKSA, bssid, pmkid);
8714}
8715
8716
8717static int nl80211_remove_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
8718{
8719 struct i802_bss *bss = priv;
8720 wpa_printf(MSG_DEBUG, "nl80211: Delete PMKID for " MACSTR,
8721 MAC2STR(bssid));
8722 return nl80211_pmkid(bss, NL80211_CMD_DEL_PMKSA, bssid, pmkid);
8723}
8724
8725
8726static int nl80211_flush_pmkid(void *priv)
8727{
8728 struct i802_bss *bss = priv;
8729 wpa_printf(MSG_DEBUG, "nl80211: Flush PMKIDs");
8730 return nl80211_pmkid(bss, NL80211_CMD_FLUSH_PMKSA, NULL, NULL);
8731}
8732
8733
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008734static void nl80211_set_rekey_info(void *priv, const u8 *kek, const u8 *kck,
8735 const u8 *replay_ctr)
8736{
8737 struct i802_bss *bss = priv;
8738 struct wpa_driver_nl80211_data *drv = bss->drv;
8739 struct nlattr *replay_nested;
8740 struct nl_msg *msg;
8741
8742 msg = nlmsg_alloc();
8743 if (!msg)
8744 return;
8745
8746 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
8747
8748 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
8749
8750 replay_nested = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
8751 if (!replay_nested)
8752 goto nla_put_failure;
8753
8754 NLA_PUT(msg, NL80211_REKEY_DATA_KEK, NL80211_KEK_LEN, kek);
8755 NLA_PUT(msg, NL80211_REKEY_DATA_KCK, NL80211_KCK_LEN, kck);
8756 NLA_PUT(msg, NL80211_REKEY_DATA_REPLAY_CTR, NL80211_REPLAY_CTR_LEN,
8757 replay_ctr);
8758
8759 nla_nest_end(msg, replay_nested);
8760
8761 send_and_recv_msgs(drv, msg, NULL, NULL);
8762 return;
8763 nla_put_failure:
8764 nlmsg_free(msg);
8765}
8766
8767
8768static void nl80211_send_null_frame(struct i802_bss *bss, const u8 *own_addr,
8769 const u8 *addr, int qos)
8770{
8771 /* send data frame to poll STA and check whether
8772 * this frame is ACKed */
8773 struct {
8774 struct ieee80211_hdr hdr;
8775 u16 qos_ctl;
8776 } STRUCT_PACKED nulldata;
8777 size_t size;
8778
8779 /* Send data frame to poll STA and check whether this frame is ACKed */
8780
8781 os_memset(&nulldata, 0, sizeof(nulldata));
8782
8783 if (qos) {
8784 nulldata.hdr.frame_control =
8785 IEEE80211_FC(WLAN_FC_TYPE_DATA,
8786 WLAN_FC_STYPE_QOS_NULL);
8787 size = sizeof(nulldata);
8788 } else {
8789 nulldata.hdr.frame_control =
8790 IEEE80211_FC(WLAN_FC_TYPE_DATA,
8791 WLAN_FC_STYPE_NULLFUNC);
8792 size = sizeof(struct ieee80211_hdr);
8793 }
8794
8795 nulldata.hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS);
8796 os_memcpy(nulldata.hdr.IEEE80211_DA_FROMDS, addr, ETH_ALEN);
8797 os_memcpy(nulldata.hdr.IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
8798 os_memcpy(nulldata.hdr.IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
8799
8800 if (wpa_driver_nl80211_send_mlme(bss, (u8 *) &nulldata, size, 0) < 0)
8801 wpa_printf(MSG_DEBUG, "nl80211_send_null_frame: Failed to "
8802 "send poll frame");
8803}
8804
8805static void nl80211_poll_client(void *priv, const u8 *own_addr, const u8 *addr,
8806 int qos)
8807{
8808 struct i802_bss *bss = priv;
8809 struct wpa_driver_nl80211_data *drv = bss->drv;
8810 struct nl_msg *msg;
8811
8812 if (!drv->poll_command_supported) {
8813 nl80211_send_null_frame(bss, own_addr, addr, qos);
8814 return;
8815 }
8816
8817 msg = nlmsg_alloc();
8818 if (!msg)
8819 return;
8820
8821 nl80211_cmd(drv, msg, 0, NL80211_CMD_PROBE_CLIENT);
8822
8823 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
8824 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
8825
8826 send_and_recv_msgs(drv, msg, NULL, NULL);
8827 return;
8828 nla_put_failure:
8829 nlmsg_free(msg);
8830}
8831
8832
8833static int nl80211_set_power_save(struct i802_bss *bss, int enabled)
8834{
8835 struct nl_msg *msg;
8836
8837 msg = nlmsg_alloc();
8838 if (!msg)
8839 return -ENOMEM;
8840
8841 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_SET_POWER_SAVE);
8842 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
8843 NLA_PUT_U32(msg, NL80211_ATTR_PS_STATE,
8844 enabled ? NL80211_PS_ENABLED : NL80211_PS_DISABLED);
8845 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
8846nla_put_failure:
8847 nlmsg_free(msg);
8848 return -ENOBUFS;
8849}
8850
8851
8852static int nl80211_set_p2p_powersave(void *priv, int legacy_ps, int opp_ps,
8853 int ctwindow)
8854{
8855 struct i802_bss *bss = priv;
8856
8857 wpa_printf(MSG_DEBUG, "nl80211: set_p2p_powersave (legacy_ps=%d "
8858 "opp_ps=%d ctwindow=%d)", legacy_ps, opp_ps, ctwindow);
8859
8860 if (opp_ps != -1 || ctwindow != -1)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008861#ifdef ANDROID_P2P
8862 wpa_driver_set_p2p_ps(priv, legacy_ps, opp_ps, ctwindow);
8863#else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008864 return -1; /* Not yet supported */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008865#endif
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008866
8867 if (legacy_ps == -1)
8868 return 0;
8869 if (legacy_ps != 0 && legacy_ps != 1)
8870 return -1; /* Not yet supported */
8871
8872 return nl80211_set_power_save(bss, legacy_ps);
8873}
8874
8875
8876#ifdef CONFIG_TDLS
8877
8878static int nl80211_send_tdls_mgmt(void *priv, const u8 *dst, u8 action_code,
8879 u8 dialog_token, u16 status_code,
8880 const u8 *buf, size_t len)
8881{
8882 struct i802_bss *bss = priv;
8883 struct wpa_driver_nl80211_data *drv = bss->drv;
8884 struct nl_msg *msg;
8885
8886 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
8887 return -EOPNOTSUPP;
8888
8889 if (!dst)
8890 return -EINVAL;
8891
8892 msg = nlmsg_alloc();
8893 if (!msg)
8894 return -ENOMEM;
8895
8896 nl80211_cmd(drv, msg, 0, NL80211_CMD_TDLS_MGMT);
8897 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
8898 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
8899 NLA_PUT_U8(msg, NL80211_ATTR_TDLS_ACTION, action_code);
8900 NLA_PUT_U8(msg, NL80211_ATTR_TDLS_DIALOG_TOKEN, dialog_token);
8901 NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status_code);
8902 NLA_PUT(msg, NL80211_ATTR_IE, len, buf);
8903
8904 return send_and_recv_msgs(drv, msg, NULL, NULL);
8905
8906nla_put_failure:
8907 nlmsg_free(msg);
8908 return -ENOBUFS;
8909}
8910
8911
8912static int nl80211_tdls_oper(void *priv, enum tdls_oper oper, const u8 *peer)
8913{
8914 struct i802_bss *bss = priv;
8915 struct wpa_driver_nl80211_data *drv = bss->drv;
8916 struct nl_msg *msg;
8917 enum nl80211_tdls_operation nl80211_oper;
8918
8919 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
8920 return -EOPNOTSUPP;
8921
8922 switch (oper) {
8923 case TDLS_DISCOVERY_REQ:
8924 nl80211_oper = NL80211_TDLS_DISCOVERY_REQ;
8925 break;
8926 case TDLS_SETUP:
8927 nl80211_oper = NL80211_TDLS_SETUP;
8928 break;
8929 case TDLS_TEARDOWN:
8930 nl80211_oper = NL80211_TDLS_TEARDOWN;
8931 break;
8932 case TDLS_ENABLE_LINK:
8933 nl80211_oper = NL80211_TDLS_ENABLE_LINK;
8934 break;
8935 case TDLS_DISABLE_LINK:
8936 nl80211_oper = NL80211_TDLS_DISABLE_LINK;
8937 break;
8938 case TDLS_ENABLE:
8939 return 0;
8940 case TDLS_DISABLE:
8941 return 0;
8942 default:
8943 return -EINVAL;
8944 }
8945
8946 msg = nlmsg_alloc();
8947 if (!msg)
8948 return -ENOMEM;
8949
8950 nl80211_cmd(drv, msg, 0, NL80211_CMD_TDLS_OPER);
8951 NLA_PUT_U8(msg, NL80211_ATTR_TDLS_OPERATION, nl80211_oper);
8952 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
8953 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, peer);
8954
8955 return send_and_recv_msgs(drv, msg, NULL, NULL);
8956
8957nla_put_failure:
8958 nlmsg_free(msg);
8959 return -ENOBUFS;
8960}
8961
8962#endif /* CONFIG TDLS */
8963
8964
8965#ifdef ANDROID
8966
8967typedef struct android_wifi_priv_cmd {
8968 char *buf;
8969 int used_len;
8970 int total_len;
8971} android_wifi_priv_cmd;
8972
8973static int drv_errors = 0;
8974
8975static void wpa_driver_send_hang_msg(struct wpa_driver_nl80211_data *drv)
8976{
8977 drv_errors++;
8978 if (drv_errors > DRV_NUMBER_SEQUENTIAL_ERRORS) {
8979 drv_errors = 0;
8980 wpa_msg(drv->ctx, MSG_INFO, WPA_EVENT_DRIVER_STATE "HANGED");
8981 }
8982}
8983
8984
8985static int android_priv_cmd(struct i802_bss *bss, const char *cmd)
8986{
8987 struct wpa_driver_nl80211_data *drv = bss->drv;
8988 struct ifreq ifr;
8989 android_wifi_priv_cmd priv_cmd;
8990 char buf[MAX_DRV_CMD_SIZE];
8991 int ret;
8992
8993 os_memset(&ifr, 0, sizeof(ifr));
8994 os_memset(&priv_cmd, 0, sizeof(priv_cmd));
8995 os_strlcpy(ifr.ifr_name, bss->ifname, IFNAMSIZ);
8996
8997 os_memset(buf, 0, sizeof(buf));
8998 os_strlcpy(buf, cmd, sizeof(buf));
8999
9000 priv_cmd.buf = buf;
9001 priv_cmd.used_len = sizeof(buf);
9002 priv_cmd.total_len = sizeof(buf);
9003 ifr.ifr_data = &priv_cmd;
9004
9005 ret = ioctl(drv->global->ioctl_sock, SIOCDEVPRIVATE + 1, &ifr);
9006 if (ret < 0) {
9007 wpa_printf(MSG_ERROR, "%s: failed to issue private commands",
9008 __func__);
9009 wpa_driver_send_hang_msg(drv);
9010 return ret;
9011 }
9012
9013 drv_errors = 0;
9014 return 0;
9015}
9016
9017
9018static int android_pno_start(struct i802_bss *bss,
9019 struct wpa_driver_scan_params *params)
9020{
9021 struct wpa_driver_nl80211_data *drv = bss->drv;
9022 struct ifreq ifr;
9023 android_wifi_priv_cmd priv_cmd;
9024 int ret = 0, i = 0, bp;
9025 char buf[WEXT_PNO_MAX_COMMAND_SIZE];
9026
9027 bp = WEXT_PNOSETUP_HEADER_SIZE;
9028 os_memcpy(buf, WEXT_PNOSETUP_HEADER, bp);
9029 buf[bp++] = WEXT_PNO_TLV_PREFIX;
9030 buf[bp++] = WEXT_PNO_TLV_VERSION;
9031 buf[bp++] = WEXT_PNO_TLV_SUBVERSION;
9032 buf[bp++] = WEXT_PNO_TLV_RESERVED;
9033
9034 while (i < WEXT_PNO_AMOUNT && (size_t) i < params->num_ssids) {
9035 /* Check that there is enough space needed for 1 more SSID, the
9036 * other sections and null termination */
9037 if ((bp + WEXT_PNO_SSID_HEADER_SIZE + MAX_SSID_LEN +
9038 WEXT_PNO_NONSSID_SECTIONS_SIZE + 1) >= (int) sizeof(buf))
9039 break;
9040 wpa_hexdump_ascii(MSG_DEBUG, "For PNO Scan",
9041 params->ssids[i].ssid,
9042 params->ssids[i].ssid_len);
9043 buf[bp++] = WEXT_PNO_SSID_SECTION;
9044 buf[bp++] = params->ssids[i].ssid_len;
9045 os_memcpy(&buf[bp], params->ssids[i].ssid,
9046 params->ssids[i].ssid_len);
9047 bp += params->ssids[i].ssid_len;
9048 i++;
9049 }
9050
9051 buf[bp++] = WEXT_PNO_SCAN_INTERVAL_SECTION;
9052 os_snprintf(&buf[bp], WEXT_PNO_SCAN_INTERVAL_LENGTH + 1, "%x",
9053 WEXT_PNO_SCAN_INTERVAL);
9054 bp += WEXT_PNO_SCAN_INTERVAL_LENGTH;
9055
9056 buf[bp++] = WEXT_PNO_REPEAT_SECTION;
9057 os_snprintf(&buf[bp], WEXT_PNO_REPEAT_LENGTH + 1, "%x",
9058 WEXT_PNO_REPEAT);
9059 bp += WEXT_PNO_REPEAT_LENGTH;
9060
9061 buf[bp++] = WEXT_PNO_MAX_REPEAT_SECTION;
9062 os_snprintf(&buf[bp], WEXT_PNO_MAX_REPEAT_LENGTH + 1, "%x",
9063 WEXT_PNO_MAX_REPEAT);
9064 bp += WEXT_PNO_MAX_REPEAT_LENGTH + 1;
9065
9066 memset(&ifr, 0, sizeof(ifr));
9067 memset(&priv_cmd, 0, sizeof(priv_cmd));
9068 os_strncpy(ifr.ifr_name, bss->ifname, IFNAMSIZ);
9069
9070 priv_cmd.buf = buf;
9071 priv_cmd.used_len = bp;
9072 priv_cmd.total_len = bp;
9073 ifr.ifr_data = &priv_cmd;
9074
9075 ret = ioctl(drv->global->ioctl_sock, SIOCDEVPRIVATE + 1, &ifr);
9076
9077 if (ret < 0) {
9078 wpa_printf(MSG_ERROR, "ioctl[SIOCSIWPRIV] (pnosetup): %d",
9079 ret);
9080 wpa_driver_send_hang_msg(drv);
9081 return ret;
9082 }
9083
9084 drv_errors = 0;
9085
9086 return android_priv_cmd(bss, "PNOFORCE 1");
9087}
9088
9089
9090static int android_pno_stop(struct i802_bss *bss)
9091{
9092 return android_priv_cmd(bss, "PNOFORCE 0");
9093}
9094
9095#endif /* ANDROID */
9096
9097
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009098const struct wpa_driver_ops wpa_driver_nl80211_ops = {
9099 .name = "nl80211",
9100 .desc = "Linux nl80211/cfg80211",
9101 .get_bssid = wpa_driver_nl80211_get_bssid,
9102 .get_ssid = wpa_driver_nl80211_get_ssid,
9103 .set_key = wpa_driver_nl80211_set_key,
9104 .scan2 = wpa_driver_nl80211_scan,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009105 .sched_scan = wpa_driver_nl80211_sched_scan,
9106 .stop_sched_scan = wpa_driver_nl80211_stop_sched_scan,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009107 .get_scan_results2 = wpa_driver_nl80211_get_scan_results,
9108 .deauthenticate = wpa_driver_nl80211_deauthenticate,
9109 .disassociate = wpa_driver_nl80211_disassociate,
9110 .authenticate = wpa_driver_nl80211_authenticate,
9111 .associate = wpa_driver_nl80211_associate,
9112 .global_init = nl80211_global_init,
9113 .global_deinit = nl80211_global_deinit,
9114 .init2 = wpa_driver_nl80211_init,
9115 .deinit = wpa_driver_nl80211_deinit,
9116 .get_capa = wpa_driver_nl80211_get_capa,
9117 .set_operstate = wpa_driver_nl80211_set_operstate,
9118 .set_supp_port = wpa_driver_nl80211_set_supp_port,
9119 .set_country = wpa_driver_nl80211_set_country,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009120 .set_ap = wpa_driver_nl80211_set_ap,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009121 .if_add = wpa_driver_nl80211_if_add,
9122 .if_remove = wpa_driver_nl80211_if_remove,
9123 .send_mlme = wpa_driver_nl80211_send_mlme,
9124 .get_hw_feature_data = wpa_driver_nl80211_get_hw_feature_data,
9125 .sta_add = wpa_driver_nl80211_sta_add,
9126 .sta_remove = wpa_driver_nl80211_sta_remove,
9127 .hapd_send_eapol = wpa_driver_nl80211_hapd_send_eapol,
9128 .sta_set_flags = wpa_driver_nl80211_sta_set_flags,
9129#ifdef HOSTAPD
9130 .hapd_init = i802_init,
9131 .hapd_deinit = i802_deinit,
Jouni Malinen75ecf522011-06-27 15:19:46 -07009132 .set_wds_sta = i802_set_wds_sta,
9133#endif /* HOSTAPD */
9134#if defined(HOSTAPD) || defined(CONFIG_AP)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009135 .get_seqnum = i802_get_seqnum,
9136 .flush = i802_flush,
9137 .read_sta_data = i802_read_sta_data,
9138 .get_inact_sec = i802_get_inact_sec,
9139 .sta_clear_stats = i802_sta_clear_stats,
9140 .set_rts = i802_set_rts,
9141 .set_frag = i802_set_frag,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009142 .set_tx_queue_params = i802_set_tx_queue_params,
9143 .set_sta_vlan = i802_set_sta_vlan,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009144 .sta_deauth = i802_sta_deauth,
9145 .sta_disassoc = i802_sta_disassoc,
9146#endif /* HOSTAPD || CONFIG_AP */
9147 .set_freq = i802_set_freq,
9148 .send_action = wpa_driver_nl80211_send_action,
9149 .send_action_cancel_wait = wpa_driver_nl80211_send_action_cancel_wait,
9150 .remain_on_channel = wpa_driver_nl80211_remain_on_channel,
9151 .cancel_remain_on_channel =
9152 wpa_driver_nl80211_cancel_remain_on_channel,
9153 .probe_req_report = wpa_driver_nl80211_probe_req_report,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009154 .deinit_ap = wpa_driver_nl80211_deinit_ap,
Dmitry Shmidt04949592012-07-19 12:16:46 -07009155 .deinit_p2p_cli = wpa_driver_nl80211_deinit_p2p_cli,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009156 .resume = wpa_driver_nl80211_resume,
9157 .send_ft_action = nl80211_send_ft_action,
9158 .signal_monitor = nl80211_signal_monitor,
9159 .signal_poll = nl80211_signal_poll,
9160 .send_frame = nl80211_send_frame,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009161 .shared_freq = wpa_driver_nl80211_shared_freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009162 .set_param = nl80211_set_param,
9163 .get_radio_name = nl80211_get_radio_name,
Jouni Malinen75ecf522011-06-27 15:19:46 -07009164 .add_pmkid = nl80211_add_pmkid,
9165 .remove_pmkid = nl80211_remove_pmkid,
9166 .flush_pmkid = nl80211_flush_pmkid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009167 .set_rekey_info = nl80211_set_rekey_info,
9168 .poll_client = nl80211_poll_client,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009169 .set_p2p_powersave = nl80211_set_p2p_powersave,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009170#ifdef CONFIG_TDLS
9171 .send_tdls_mgmt = nl80211_send_tdls_mgmt,
9172 .tdls_oper = nl80211_tdls_oper,
9173#endif /* CONFIG_TDLS */
9174#ifdef ANDROID_P2P
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07009175 .set_noa = wpa_driver_set_p2p_noa,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009176 .get_noa = wpa_driver_get_p2p_noa,
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07009177 .set_ap_wps_ie = wpa_driver_set_ap_wps_p2p_ie,
9178#endif
Dmitry Shmidt738a26e2011-07-07 14:22:14 -07009179#ifdef ANDROID
9180 .driver_cmd = wpa_driver_nl80211_driver_cmd,
9181#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009182};