]> git.ipfire.org Git - thirdparty/hostap.git/blame - src/drivers/driver_nl80211.c
P2P: Allow channels to be removed from P2P use
[thirdparty/hostap.git] / src / drivers / driver_nl80211.c
CommitLineData
3f5285e8 1/*
c5121837 2 * Driver interaction with Linux nl80211/cfg80211
3d9975d5 3 * Copyright (c) 2002-2012, Jouni Malinen <j@w1.fi>
072ad14c
JM
4 * Copyright (c) 2003-2004, Instant802 Networks, Inc.
5 * Copyright (c) 2005-2006, Devicescape Software, Inc.
6 * Copyright (c) 2007, Johannes Berg <johannes@sipsolutions.net>
58f6fbe0 7 * Copyright (c) 2009-2010, Atheros Communications
3f5285e8 8 *
0f3d578e
JM
9 * This software may be distributed under the terms of the BSD license.
10 * See README for more details.
3f5285e8
JM
11 */
12
13#include "includes.h"
14#include <sys/ioctl.h>
6859f1cb
BG
15#include <sys/types.h>
16#include <sys/stat.h>
17#include <fcntl.h>
37b7d082 18#include <net/if.h>
3f5285e8
JM
19#include <netlink/genl/genl.h>
20#include <netlink/genl/family.h>
21#include <netlink/genl/ctrl.h>
8602b0f2 22#include <linux/rtnetlink.h>
1b648c7e
JM
23#include <netpacket/packet.h>
24#include <linux/filter.h>
32ab4855 25#include <linux/errqueue.h>
7e45830a 26#include "nl80211_copy.h"
625f587b 27
3f5285e8 28#include "common.h"
1b648c7e 29#include "eloop.h"
f2ed8023 30#include "utils/list.h"
1b648c7e 31#include "common/ieee802_11_defs.h"
9b90955e 32#include "common/ieee802_11_common.h"
f10bfc9a 33#include "l2_packet/l2_packet.h"
e2d02c29 34#include "netlink.h"
34f2f814 35#include "linux_ioctl.h"
e2d02c29
JM
36#include "radiotap.h"
37#include "radiotap_iter.h"
8401a6b0 38#include "rfkill.h"
1b648c7e 39#include "driver.h"
0915d02c 40
32ab4855
JB
41#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
216eede8
DS
61#ifdef ANDROID
62#include "android_drv.h"
63#endif /* ANDROID */
c5121837
JM
64#ifdef CONFIG_LIBNL20
65/* libnl 2.0 compatibility code */
2e8eac2d 66#define nl_handle nl_sock
a65a9aed
JB
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}
c5121837
JM
109#endif /* CONFIG_LIBNL20 */
110
c5121837 111
481234cf 112static struct nl_handle * nl_create_handle(struct nl_cb *cb, const char *dbg)
a92dfde8 113{
481234cf 114 struct nl_handle *handle;
a92dfde8 115
481234cf
JM
116 handle = nl80211_handle_alloc(cb);
117 if (handle == NULL) {
a92dfde8
JB
118 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
119 "callbacks (%s)", dbg);
481234cf 120 return NULL;
a92dfde8
JB
121 }
122
481234cf 123 if (genl_connect(handle)) {
a92dfde8
JB
124 wpa_printf(MSG_ERROR, "nl80211: Failed to connect to generic "
125 "netlink (%s)", dbg);
481234cf
JM
126 nl80211_handle_destroy(handle);
127 return NULL;
a92dfde8
JB
128 }
129
481234cf 130 return handle;
a92dfde8
JB
131}
132
133
481234cf 134static void nl_destroy_handles(struct nl_handle **handle)
a92dfde8 135{
481234cf 136 if (*handle == NULL)
a92dfde8 137 return;
481234cf
JM
138 nl80211_handle_destroy(*handle);
139 *handle = NULL;
a92dfde8
JB
140}
141
142
3f5285e8
JM
143#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
f2ed8023
JM
157struct nl80211_global {
158 struct dl_list interfaces;
ff6a158b 159 int if_add_ifindex;
36d84860 160 struct netlink_data *netlink;
2a7b66f5 161 struct nl_cb *nl_cb;
481234cf 162 struct nl_handle *nl;
335d42b1 163 int nl80211_id;
c81eff1a 164 int ioctl_sock; /* socket for ioctl() use */
d6c9aab8 165
481234cf 166 struct nl_handle *nl_event;
f2ed8023
JM
167};
168
e32ad281
JB
169struct nl80211_wiphy_data {
170 struct dl_list list;
171 struct dl_list bsss;
172 struct dl_list drvs;
173
481234cf 174 struct nl_handle *nl_beacons;
e32ad281
JB
175 struct nl_cb *nl_cb;
176
177 int wiphy_idx;
178};
179
36d84860 180static void nl80211_global_deinit(void *priv);
dac12351 181static void wpa_driver_nl80211_deinit(void *priv);
36d84860 182
c5121837 183struct i802_bss {
a2e40bb6 184 struct wpa_driver_nl80211_data *drv;
c5121837 185 struct i802_bss *next;
b4fd6fab 186 int ifindex;
a2e40bb6 187 char ifname[IFNAMSIZ + 1];
e17a2477 188 char brname[IFNAMSIZ];
c5121837 189 unsigned int beacon_set:1;
e17a2477
JM
190 unsigned int added_if_into_bridge:1;
191 unsigned int added_bridge:1;
221a59c9 192
341eebee
JB
193 u8 addr[ETH_ALEN];
194
e4fb2167
JB
195 int freq;
196
481234cf 197 struct nl_handle *nl_preq, *nl_mgmt;
cc7a48d1 198 struct nl_cb *nl_cb;
e32ad281
JB
199
200 struct nl80211_wiphy_data *wiphy_data;
201 struct dl_list wiphy_list;
c5121837 202};
3f5285e8
JM
203
204struct wpa_driver_nl80211_data {
f2ed8023
JM
205 struct nl80211_global *global;
206 struct dl_list list;
e32ad281 207 struct dl_list wiphy_list;
6859f1cb 208 char phyname[32];
3f5285e8 209 void *ctx;
3f5285e8 210 int ifindex;
7524cfb1 211 int if_removed;
a63063b4 212 int if_disabled;
7d9c3698 213 int ignore_if_down_event;
8401a6b0 214 struct rfkill_data *rfkill;
c2a04078
JM
215 struct wpa_driver_capa capa;
216 int has_capability;
c2a04078 217
3f5285e8
JM
218 int operstate;
219
3f5285e8
JM
220 int scan_complete_events;
221
1afc986d 222 struct nl_cb *nl_cb;
1c873584 223
e6b8efeb 224 u8 auth_bssid[ETH_ALEN];
c2a04078
JM
225 u8 bssid[ETH_ALEN];
226 int associated;
fd05d64e
JM
227 u8 ssid[32];
228 size_t ssid_len;
b1f625e0
EP
229 enum nl80211_iftype nlmode;
230 enum nl80211_iftype ap_scan_as_station;
4832ecd7 231 unsigned int assoc_freq;
d2440ba0 232
0915d02c
JM
233 int monitor_sock;
234 int monitor_ifidx;
3fd1cefb 235 int monitor_refcount;
7da3abe7 236
b3af99d2 237 unsigned int disabled_11b_rates:1;
55777702 238 unsigned int pending_remain_on_chan:1;
dac12351 239 unsigned int in_interface_list:1;
61cbe2ff 240 unsigned int device_ap_sme:1;
39718852 241 unsigned int poll_command_supported:1;
32ab4855 242 unsigned int data_tx_status:1;
536fd62d
JM
243 unsigned int scan_for_auth:1;
244 unsigned int retry_auth:1;
a11241fa 245 unsigned int use_monitor:1;
55777702
JM
246
247 u64 remain_on_chan_cookie;
58f6fbe0 248 u64 send_action_cookie;
c5121837 249
5582a5d1
JB
250 unsigned int last_mgmt_freq;
251
3812464c
JM
252 struct wpa_driver_scan_filter *filter_ssids;
253 size_t num_filter_ssids;
254
a2e40bb6
FF
255 struct i802_bss first_bss;
256
d12dab4c 257 int eapol_tx_sock;
f10bfc9a 258
c5121837 259#ifdef HOSTAPD
c5121837 260 int eapol_sock; /* socket for EAPOL frames */
c5121837
JM
261
262 int default_if_indices[16];
263 int *if_indices;
264 int num_if_indices;
265
c5121837
JM
266 int last_freq;
267 int last_freq_ht;
c5121837 268#endif /* HOSTAPD */
536fd62d
JM
269
270 /* From failed authentication command */
271 int auth_freq;
272 u8 auth_bssid_[ETH_ALEN];
273 u8 auth_ssid[32];
274 size_t auth_ssid_len;
275 int auth_alg;
276 u8 *auth_ie;
277 size_t auth_ie_len;
278 u8 auth_wep_key[4][16];
279 size_t auth_wep_key_len[4];
280 int auth_wep_tx_keyidx;
281 int auth_local_state_change;
282 int auth_p2p;
3f5285e8
JM
283};
284
285
286static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx,
287 void *timeout_ctx);
b1f625e0
EP
288static int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
289 enum nl80211_iftype nlmode);
362f781e 290static int
7524cfb1 291wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv);
d72aad94 292static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
77339912
JM
293 const u8 *addr, int cmd, u16 reason_code,
294 int local_state_change);
460456f8
JM
295static void nl80211_remove_monitor_interface(
296 struct wpa_driver_nl80211_data *drv);
88df0ef7 297static int nl80211_send_frame_cmd(struct i802_bss *bss,
5dfca53f 298 unsigned int freq, unsigned int wait,
b106173a 299 const u8 *buf, size_t buf_len, u64 *cookie,
88df0ef7 300 int no_cck, int no_ack, int offchanok);
5582a5d1 301static int wpa_driver_nl80211_probe_req_report(void *priv, int report);
216eede8
DS
302#ifdef ANDROID
303static int android_pno_start(struct i802_bss *bss,
304 struct wpa_driver_scan_params *params);
305static int android_pno_stop(struct i802_bss *bss);
306#endif /* ANDROID */
0915d02c 307
072ad14c 308#ifdef HOSTAPD
2135f224
JM
309static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
310static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
97cfcf64 311static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
fbbfcbac
FF
312static int wpa_driver_nl80211_if_remove(void *priv,
313 enum wpa_driver_if_type type,
314 const char *ifname);
37eb8da9 315#else /* HOSTAPD */
bd2df892
JM
316static inline void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
317{
318}
319
320static inline void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
321{
322}
323
324static inline int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
37eb8da9
JM
325{
326 return 0;
327}
072ad14c
JM
328#endif /* HOSTAPD */
329
e3802622 330static int i802_set_freq(void *priv, struct hostapd_freq_params *freq);
4e5cb1a3
JM
331static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
332 int ifindex, int disabled);
504e905c 333
21bdbe38 334static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv);
536fd62d
JM
335static int wpa_driver_nl80211_authenticate_retry(
336 struct wpa_driver_nl80211_data *drv);
21bdbe38 337
3f5285e8 338
b1f625e0
EP
339static int is_ap_interface(enum nl80211_iftype nlmode)
340{
341 return (nlmode == NL80211_IFTYPE_AP ||
342 nlmode == NL80211_IFTYPE_P2P_GO);
343}
344
345
346static int is_sta_interface(enum nl80211_iftype nlmode)
347{
348 return (nlmode == NL80211_IFTYPE_STATION ||
349 nlmode == NL80211_IFTYPE_P2P_CLIENT);
350}
351
352
b3af99d2
JM
353static int is_p2p_interface(enum nl80211_iftype nlmode)
354{
355 return (nlmode == NL80211_IFTYPE_P2P_CLIENT ||
356 nlmode == NL80211_IFTYPE_P2P_GO);
357}
358
359
f5a8d422
JM
360struct nl80211_bss_info_arg {
361 struct wpa_driver_nl80211_data *drv;
362 struct wpa_scan_results *res;
363 unsigned int assoc_freq;
20f5a4c2 364 u8 assoc_bssid[ETH_ALEN];
f5a8d422
JM
365};
366
367static int bss_info_handler(struct nl_msg *msg, void *arg);
368
369
6241fcb1
JM
370/* nl80211 code */
371static int ack_handler(struct nl_msg *msg, void *arg)
372{
373 int *err = arg;
374 *err = 0;
375 return NL_STOP;
376}
377
378static int finish_handler(struct nl_msg *msg, void *arg)
379{
8e8df255
JM
380 int *ret = arg;
381 *ret = 0;
6241fcb1
JM
382 return NL_SKIP;
383}
384
385static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
386 void *arg)
387{
388 int *ret = arg;
389 *ret = err->error;
390 return NL_SKIP;
391}
392
5b7b85f6
JM
393
394static int no_seq_check(struct nl_msg *msg, void *arg)
395{
396 return NL_OK;
397}
398
399
d6c9aab8 400static int send_and_recv(struct nl80211_global *global,
58f6fbe0
JM
401 struct nl_handle *nl_handle, struct nl_msg *msg,
402 int (*valid_handler)(struct nl_msg *, void *),
403 void *valid_data)
6241fcb1
JM
404{
405 struct nl_cb *cb;
406 int err = -ENOMEM;
407
d6c9aab8 408 cb = nl_cb_clone(global->nl_cb);
6241fcb1
JM
409 if (!cb)
410 goto out;
411
58f6fbe0 412 err = nl_send_auto_complete(nl_handle, msg);
6241fcb1
JM
413 if (err < 0)
414 goto out;
415
416 err = 1;
417
418 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
8e8df255 419 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
6241fcb1
JM
420 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
421
422 if (valid_handler)
423 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM,
424 valid_handler, valid_data);
425
426 while (err > 0)
58f6fbe0 427 nl_recvmsgs(nl_handle, cb);
6241fcb1
JM
428 out:
429 nl_cb_put(cb);
430 nlmsg_free(msg);
431 return err;
432}
433
434
d6c9aab8
JB
435static int send_and_recv_msgs_global(struct nl80211_global *global,
436 struct nl_msg *msg,
437 int (*valid_handler)(struct nl_msg *, void *),
438 void *valid_data)
439{
481234cf 440 return send_and_recv(global, global->nl, msg, valid_handler,
d6c9aab8
JB
441 valid_data);
442}
443
444
58f6fbe0
JM
445static int send_and_recv_msgs(struct wpa_driver_nl80211_data *drv,
446 struct nl_msg *msg,
447 int (*valid_handler)(struct nl_msg *, void *),
448 void *valid_data)
449{
481234cf 450 return send_and_recv(drv->global, drv->global->nl, msg,
d6c9aab8 451 valid_handler, valid_data);
58f6fbe0
JM
452}
453
454
97865538
JM
455struct family_data {
456 const char *group;
457 int id;
458};
459
460
461static int family_handler(struct nl_msg *msg, void *arg)
462{
463 struct family_data *res = arg;
464 struct nlattr *tb[CTRL_ATTR_MAX + 1];
465 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
466 struct nlattr *mcgrp;
467 int i;
468
469 nla_parse(tb, CTRL_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
470 genlmsg_attrlen(gnlh, 0), NULL);
471 if (!tb[CTRL_ATTR_MCAST_GROUPS])
472 return NL_SKIP;
473
474 nla_for_each_nested(mcgrp, tb[CTRL_ATTR_MCAST_GROUPS], i) {
475 struct nlattr *tb2[CTRL_ATTR_MCAST_GRP_MAX + 1];
476 nla_parse(tb2, CTRL_ATTR_MCAST_GRP_MAX, nla_data(mcgrp),
477 nla_len(mcgrp), NULL);
478 if (!tb2[CTRL_ATTR_MCAST_GRP_NAME] ||
479 !tb2[CTRL_ATTR_MCAST_GRP_ID] ||
480 os_strncmp(nla_data(tb2[CTRL_ATTR_MCAST_GRP_NAME]),
481 res->group,
482 nla_len(tb2[CTRL_ATTR_MCAST_GRP_NAME])) != 0)
483 continue;
484 res->id = nla_get_u32(tb2[CTRL_ATTR_MCAST_GRP_ID]);
485 break;
486 };
487
488 return NL_SKIP;
489}
490
491
d6c9aab8 492static int nl_get_multicast_id(struct nl80211_global *global,
97865538
JM
493 const char *family, const char *group)
494{
495 struct nl_msg *msg;
496 int ret = -1;
497 struct family_data res = { group, -ENOENT };
498
499 msg = nlmsg_alloc();
500 if (!msg)
501 return -ENOMEM;
481234cf 502 genlmsg_put(msg, 0, 0, genl_ctrl_resolve(global->nl, "nlctrl"),
97865538
JM
503 0, 0, CTRL_CMD_GETFAMILY, 0);
504 NLA_PUT_STRING(msg, CTRL_ATTR_FAMILY_NAME, family);
505
d6c9aab8 506 ret = send_and_recv_msgs_global(global, msg, family_handler, &res);
97865538
JM
507 msg = NULL;
508 if (ret == 0)
509 ret = res.id;
510
511nla_put_failure:
512 nlmsg_free(msg);
513 return ret;
514}
515
516
9fb04070
JM
517static void * nl80211_cmd(struct wpa_driver_nl80211_data *drv,
518 struct nl_msg *msg, int flags, uint8_t cmd)
519{
335d42b1 520 return genlmsg_put(msg, 0, 0, drv->global->nl80211_id,
276e2d67 521 0, flags, cmd, 0);
9fb04070
JM
522}
523
524
e32ad281
JB
525struct wiphy_idx_data {
526 int wiphy_idx;
527};
528
529
530static int netdev_info_handler(struct nl_msg *msg, void *arg)
531{
532 struct nlattr *tb[NL80211_ATTR_MAX + 1];
533 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
534 struct wiphy_idx_data *info = arg;
535
536 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
537 genlmsg_attrlen(gnlh, 0), NULL);
538
539 if (tb[NL80211_ATTR_WIPHY])
540 info->wiphy_idx = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
541
542 return NL_SKIP;
543}
544
545
546static int nl80211_get_wiphy_index(struct i802_bss *bss)
547{
548 struct nl_msg *msg;
549 struct wiphy_idx_data data = {
550 .wiphy_idx = -1,
551 };
552
553 msg = nlmsg_alloc();
554 if (!msg)
555 return -1;
556
557 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_GET_INTERFACE);
558
559 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
560
561 if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0)
562 return data.wiphy_idx;
563 msg = NULL;
564nla_put_failure:
565 nlmsg_free(msg);
566 return -1;
567}
568
569
570static int nl80211_register_beacons(struct wpa_driver_nl80211_data *drv,
571 struct nl80211_wiphy_data *w)
572{
573 struct nl_msg *msg;
574 int ret = -1;
575
576 msg = nlmsg_alloc();
577 if (!msg)
578 return -1;
579
580 nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_BEACONS);
581
582 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, w->wiphy_idx);
583
481234cf 584 ret = send_and_recv(drv->global, w->nl_beacons, msg, NULL, NULL);
e32ad281
JB
585 msg = NULL;
586 if (ret) {
587 wpa_printf(MSG_DEBUG, "nl80211: Register beacons command "
588 "failed: ret=%d (%s)",
589 ret, strerror(-ret));
590 goto nla_put_failure;
591 }
592 ret = 0;
593nla_put_failure:
594 nlmsg_free(msg);
595 return ret;
596}
597
598
599static void nl80211_recv_beacons(int sock, void *eloop_ctx, void *handle)
600{
601 struct nl80211_wiphy_data *w = eloop_ctx;
602
ee9fc67a 603 wpa_printf(MSG_EXCESSIVE, "nl80211: Beacon event message available");
e32ad281
JB
604
605 nl_recvmsgs(handle, w->nl_cb);
606}
607
608
609static int process_beacon_event(struct nl_msg *msg, void *arg)
610{
611 struct nl80211_wiphy_data *w = arg;
612 struct wpa_driver_nl80211_data *drv;
613 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
614 struct nlattr *tb[NL80211_ATTR_MAX + 1];
615 union wpa_event_data event;
616
617 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
618 genlmsg_attrlen(gnlh, 0), NULL);
619
620 if (gnlh->cmd != NL80211_CMD_FRAME) {
621 wpa_printf(MSG_DEBUG, "nl80211: Unexpected beacon event? (%d)",
622 gnlh->cmd);
623 return NL_SKIP;
624 }
625
626 if (!tb[NL80211_ATTR_FRAME])
627 return NL_SKIP;
628
629 dl_list_for_each(drv, &w->drvs, struct wpa_driver_nl80211_data,
630 wiphy_list) {
631 os_memset(&event, 0, sizeof(event));
632 event.rx_mgmt.frame = nla_data(tb[NL80211_ATTR_FRAME]);
633 event.rx_mgmt.frame_len = nla_len(tb[NL80211_ATTR_FRAME]);
634 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
635 }
636
637 return NL_SKIP;
638}
639
640
641static struct nl80211_wiphy_data *
642nl80211_get_wiphy_data_ap(struct i802_bss *bss)
643{
644 static DEFINE_DL_LIST(nl80211_wiphys);
645 struct nl80211_wiphy_data *w;
646 int wiphy_idx, found = 0;
647 struct i802_bss *tmp_bss;
648
649 if (bss->wiphy_data != NULL)
650 return bss->wiphy_data;
651
652 wiphy_idx = nl80211_get_wiphy_index(bss);
653
654 dl_list_for_each(w, &nl80211_wiphys, struct nl80211_wiphy_data, list) {
655 if (w->wiphy_idx == wiphy_idx)
656 goto add;
657 }
658
659 /* alloc new one */
660 w = os_zalloc(sizeof(*w));
661 if (w == NULL)
662 return NULL;
663 w->wiphy_idx = wiphy_idx;
664 dl_list_init(&w->bsss);
665 dl_list_init(&w->drvs);
666
667 w->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
668 if (!w->nl_cb) {
669 os_free(w);
670 return NULL;
671 }
672 nl_cb_set(w->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL);
673 nl_cb_set(w->nl_cb, NL_CB_VALID, NL_CB_CUSTOM, process_beacon_event,
674 w);
675
481234cf
JM
676 w->nl_beacons = nl_create_handle(bss->drv->global->nl_cb,
677 "wiphy beacons");
678 if (w->nl_beacons == NULL) {
e32ad281
JB
679 os_free(w);
680 return NULL;
681 }
682
683 if (nl80211_register_beacons(bss->drv, w)) {
684 nl_destroy_handles(&w->nl_beacons);
685 os_free(w);
686 return NULL;
687 }
688
481234cf
JM
689 eloop_register_read_sock(nl_socket_get_fd(w->nl_beacons),
690 nl80211_recv_beacons, w, w->nl_beacons);
e32ad281
JB
691
692 dl_list_add(&nl80211_wiphys, &w->list);
693
694add:
695 /* drv entry for this bss already there? */
696 dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
697 if (tmp_bss->drv == bss->drv) {
698 found = 1;
699 break;
700 }
701 }
702 /* if not add it */
703 if (!found)
704 dl_list_add(&w->drvs, &bss->drv->wiphy_list);
705
706 dl_list_add(&w->bsss, &bss->wiphy_list);
707 bss->wiphy_data = w;
708 return w;
709}
710
711
712static void nl80211_put_wiphy_data_ap(struct i802_bss *bss)
713{
714 struct nl80211_wiphy_data *w = bss->wiphy_data;
715 struct i802_bss *tmp_bss;
716 int found = 0;
717
718 if (w == NULL)
719 return;
720 bss->wiphy_data = NULL;
721 dl_list_del(&bss->wiphy_list);
722
723 /* still any for this drv present? */
724 dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
725 if (tmp_bss->drv == bss->drv) {
726 found = 1;
727 break;
728 }
729 }
730 /* if not remove it */
731 if (!found)
732 dl_list_del(&bss->drv->wiphy_list);
733
734 if (!dl_list_empty(&w->bsss))
735 return;
736
481234cf 737 eloop_unregister_read_sock(nl_socket_get_fd(w->nl_beacons));
e32ad281
JB
738
739 nl_cb_put(w->nl_cb);
740 nl_destroy_handles(&w->nl_beacons);
741 dl_list_del(&w->list);
742 os_free(w);
743}
744
745
3f5285e8
JM
746static int wpa_driver_nl80211_get_bssid(void *priv, u8 *bssid)
747{
a2e40bb6
FF
748 struct i802_bss *bss = priv;
749 struct wpa_driver_nl80211_data *drv = bss->drv;
c2a04078
JM
750 if (!drv->associated)
751 return -1;
752 os_memcpy(bssid, drv->bssid, ETH_ALEN);
753 return 0;
3f5285e8
JM
754}
755
756
3f5285e8
JM
757static int wpa_driver_nl80211_get_ssid(void *priv, u8 *ssid)
758{
a2e40bb6
FF
759 struct i802_bss *bss = priv;
760 struct wpa_driver_nl80211_data *drv = bss->drv;
fd05d64e
JM
761 if (!drv->associated)
762 return -1;
763 os_memcpy(ssid, drv->ssid, drv->ssid_len);
764 return drv->ssid_len;
3f5285e8
JM
765}
766
767
7524cfb1 768static void wpa_driver_nl80211_event_link(struct wpa_driver_nl80211_data *drv,
08063178 769 char *buf, size_t len, int del)
3f5285e8
JM
770{
771 union wpa_event_data event;
772
773 os_memset(&event, 0, sizeof(event));
774 if (len > sizeof(event.interface_status.ifname))
775 len = sizeof(event.interface_status.ifname) - 1;
776 os_memcpy(event.interface_status.ifname, buf, len);
777 event.interface_status.ievent = del ? EVENT_INTERFACE_REMOVED :
778 EVENT_INTERFACE_ADDED;
779
780 wpa_printf(MSG_DEBUG, "RTM_%sLINK, IFLA_IFNAME: Interface '%s' %s",
781 del ? "DEL" : "NEW",
782 event.interface_status.ifname,
783 del ? "removed" : "added");
784
a2e40bb6 785 if (os_strcmp(drv->first_bss.ifname, event.interface_status.ifname) == 0) {
7524cfb1
JM
786 if (del)
787 drv->if_removed = 1;
788 else
789 drv->if_removed = 0;
790 }
791
08063178 792 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
3f5285e8
JM
793}
794
795
7524cfb1 796static int wpa_driver_nl80211_own_ifname(struct wpa_driver_nl80211_data *drv,
62d680c3 797 u8 *buf, size_t len)
7524cfb1 798{
62d680c3 799 int attrlen, rta_len;
7524cfb1
JM
800 struct rtattr *attr;
801
62d680c3
JM
802 attrlen = len;
803 attr = (struct rtattr *) buf;
7524cfb1
JM
804
805 rta_len = RTA_ALIGN(sizeof(struct rtattr));
806 while (RTA_OK(attr, attrlen)) {
807 if (attr->rta_type == IFLA_IFNAME) {
a2e40bb6 808 if (os_strcmp(((char *) attr) + rta_len, drv->first_bss.ifname)
7524cfb1
JM
809 == 0)
810 return 1;
811 else
812 break;
813 }
814 attr = RTA_NEXT(attr, attrlen);
815 }
816
817 return 0;
818}
819
820
821static int wpa_driver_nl80211_own_ifindex(struct wpa_driver_nl80211_data *drv,
62d680c3 822 int ifindex, u8 *buf, size_t len)
7524cfb1
JM
823{
824 if (drv->ifindex == ifindex)
825 return 1;
826
62d680c3 827 if (drv->if_removed && wpa_driver_nl80211_own_ifname(drv, buf, len)) {
a2e40bb6 828 drv->first_bss.ifindex = if_nametoindex(drv->first_bss.ifname);
7524cfb1
JM
829 wpa_printf(MSG_DEBUG, "nl80211: Update ifindex for a removed "
830 "interface");
831 wpa_driver_nl80211_finish_drv_init(drv);
832 return 1;
833 }
834
835 return 0;
836}
837
838
36d84860
BG
839static struct wpa_driver_nl80211_data *
840nl80211_find_drv(struct nl80211_global *global, int idx, u8 *buf, size_t len)
841{
842 struct wpa_driver_nl80211_data *drv;
843 dl_list_for_each(drv, &global->interfaces,
844 struct wpa_driver_nl80211_data, list) {
845 if (wpa_driver_nl80211_own_ifindex(drv, idx, buf, len) ||
846 have_ifidx(drv, idx))
847 return drv;
848 }
849 return NULL;
850}
851
852
62d680c3
JM
853static void wpa_driver_nl80211_event_rtm_newlink(void *ctx,
854 struct ifinfomsg *ifi,
855 u8 *buf, size_t len)
3f5285e8 856{
36d84860
BG
857 struct nl80211_global *global = ctx;
858 struct wpa_driver_nl80211_data *drv;
62d680c3
JM
859 int attrlen, rta_len;
860 struct rtattr *attr;
97cfcf64 861 u32 brid = 0;
aef85ba2 862 char namebuf[IFNAMSIZ];
3f5285e8 863
36d84860
BG
864 drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
865 if (!drv) {
97cfcf64
B
866 wpa_printf(MSG_DEBUG, "nl80211: Ignore event for foreign "
867 "ifindex %d", ifi->ifi_index);
3f5285e8
JM
868 return;
869 }
870
871 wpa_printf(MSG_DEBUG, "RTM_NEWLINK: operstate=%d ifi_flags=0x%x "
872 "(%s%s%s%s)",
873 drv->operstate, ifi->ifi_flags,
874 (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
875 (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
876 (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
877 (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
a63063b4
JM
878
879 if (!drv->if_disabled && !(ifi->ifi_flags & IFF_UP)) {
59d24925
JM
880 if (if_indextoname(ifi->ifi_index, namebuf) &&
881 linux_iface_up(drv->global->ioctl_sock,
882 drv->first_bss.ifname) > 0) {
883 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
884 "event since interface %s is up", namebuf);
885 return;
886 }
a63063b4 887 wpa_printf(MSG_DEBUG, "nl80211: Interface down");
7d9c3698
JM
888 if (drv->ignore_if_down_event) {
889 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
890 "event generated by mode change");
891 drv->ignore_if_down_event = 0;
892 } else {
893 drv->if_disabled = 1;
894 wpa_supplicant_event(drv->ctx,
895 EVENT_INTERFACE_DISABLED, NULL);
896 }
a63063b4
JM
897 }
898
899 if (drv->if_disabled && (ifi->ifi_flags & IFF_UP)) {
aef85ba2
JM
900 if (if_indextoname(ifi->ifi_index, namebuf) &&
901 linux_iface_up(drv->global->ioctl_sock,
902 drv->first_bss.ifname) == 0) {
903 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
904 "event since interface %s is down",
905 namebuf);
906 } else {
907 wpa_printf(MSG_DEBUG, "nl80211: Interface up");
908 drv->if_disabled = 0;
909 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_ENABLED,
910 NULL);
911 }
a63063b4
JM
912 }
913
3f5285e8
JM
914 /*
915 * Some drivers send the association event before the operup event--in
916 * this case, lifting operstate in wpa_driver_nl80211_set_operstate()
917 * fails. This will hit us when wpa_supplicant does not need to do
918 * IEEE 802.1X authentication
919 */
920 if (drv->operstate == 1 &&
921 (ifi->ifi_flags & (IFF_LOWER_UP | IFF_DORMANT)) == IFF_LOWER_UP &&
922 !(ifi->ifi_flags & IFF_RUNNING))
36d84860 923 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
e2d02c29 924 -1, IF_OPER_UP);
3f5285e8 925
62d680c3
JM
926 attrlen = len;
927 attr = (struct rtattr *) buf;
3f5285e8
JM
928 rta_len = RTA_ALIGN(sizeof(struct rtattr));
929 while (RTA_OK(attr, attrlen)) {
d8816397 930 if (attr->rta_type == IFLA_IFNAME) {
7524cfb1 931 wpa_driver_nl80211_event_link(
08063178 932 drv,
7524cfb1
JM
933 ((char *) attr) + rta_len,
934 attr->rta_len - rta_len, 0);
97cfcf64
B
935 } else if (attr->rta_type == IFLA_MASTER)
936 brid = nla_get_u32((struct nlattr *) attr);
3f5285e8
JM
937 attr = RTA_NEXT(attr, attrlen);
938 }
97cfcf64
B
939
940 if (ifi->ifi_family == AF_BRIDGE && brid) {
941 /* device has been added to bridge */
97cfcf64
B
942 if_indextoname(brid, namebuf);
943 wpa_printf(MSG_DEBUG, "nl80211: Add ifindex %u for bridge %s",
944 brid, namebuf);
945 add_ifidx(drv, brid);
946 }
3f5285e8
JM
947}
948
949
62d680c3
JM
950static void wpa_driver_nl80211_event_rtm_dellink(void *ctx,
951 struct ifinfomsg *ifi,
952 u8 *buf, size_t len)
3f5285e8 953{
36d84860
BG
954 struct nl80211_global *global = ctx;
955 struct wpa_driver_nl80211_data *drv;
62d680c3
JM
956 int attrlen, rta_len;
957 struct rtattr *attr;
97cfcf64 958 u32 brid = 0;
3f5285e8 959
36d84860
BG
960 drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
961 if (!drv) {
962 wpa_printf(MSG_DEBUG, "nl80211: Ignore dellink event for "
963 "foreign ifindex %d", ifi->ifi_index);
964 return;
965 }
966
62d680c3
JM
967 attrlen = len;
968 attr = (struct rtattr *) buf;
3f5285e8
JM
969
970 rta_len = RTA_ALIGN(sizeof(struct rtattr));
971 while (RTA_OK(attr, attrlen)) {
972 if (attr->rta_type == IFLA_IFNAME) {
7524cfb1 973 wpa_driver_nl80211_event_link(
08063178 974 drv,
7524cfb1
JM
975 ((char *) attr) + rta_len,
976 attr->rta_len - rta_len, 1);
97cfcf64
B
977 } else if (attr->rta_type == IFLA_MASTER)
978 brid = nla_get_u32((struct nlattr *) attr);
3f5285e8
JM
979 attr = RTA_NEXT(attr, attrlen);
980 }
97cfcf64
B
981
982 if (ifi->ifi_family == AF_BRIDGE && brid) {
983 /* device has been removed from bridge */
984 char namebuf[IFNAMSIZ];
985 if_indextoname(brid, namebuf);
986 wpa_printf(MSG_DEBUG, "nl80211: Remove ifindex %u for bridge "
987 "%s", brid, namebuf);
988 del_ifidx(drv, brid);
989 }
3f5285e8
JM
990}
991
992
c2a04078
JM
993static void mlme_event_auth(struct wpa_driver_nl80211_data *drv,
994 const u8 *frame, size_t len)
995{
996 const struct ieee80211_mgmt *mgmt;
997 union wpa_event_data event;
998
999 mgmt = (const struct ieee80211_mgmt *) frame;
1000 if (len < 24 + sizeof(mgmt->u.auth)) {
1001 wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
1002 "frame");
1003 return;
1004 }
1005
e6b8efeb 1006 os_memcpy(drv->auth_bssid, mgmt->sa, ETH_ALEN);
c2a04078
JM
1007 os_memset(&event, 0, sizeof(event));
1008 os_memcpy(event.auth.peer, mgmt->sa, ETH_ALEN);
1009 event.auth.auth_type = le_to_host16(mgmt->u.auth.auth_alg);
1010 event.auth.status_code = le_to_host16(mgmt->u.auth.status_code);
1011 if (len > 24 + sizeof(mgmt->u.auth)) {
1012 event.auth.ies = mgmt->u.auth.variable;
1013 event.auth.ies_len = len - 24 - sizeof(mgmt->u.auth);
1014 }
1015
1016 wpa_supplicant_event(drv->ctx, EVENT_AUTH, &event);
1017}
1018
1019
f5a8d422
JM
1020static unsigned int nl80211_get_assoc_freq(struct wpa_driver_nl80211_data *drv)
1021{
1022 struct nl_msg *msg;
1023 int ret;
1024 struct nl80211_bss_info_arg arg;
1025
1026 os_memset(&arg, 0, sizeof(arg));
1027 msg = nlmsg_alloc();
1028 if (!msg)
1029 goto nla_put_failure;
1030
9fb04070 1031 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
f5a8d422
JM
1032 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
1033
1034 arg.drv = drv;
1035 ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
1036 msg = NULL;
1037 if (ret == 0) {
1038 wpa_printf(MSG_DEBUG, "nl80211: Operating frequency for the "
1039 "associated BSS from scan results: %u MHz",
1040 arg.assoc_freq);
1041 return arg.assoc_freq ? arg.assoc_freq : drv->assoc_freq;
1042 }
1043 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
1044 "(%s)", ret, strerror(-ret));
1045nla_put_failure:
1046 nlmsg_free(msg);
1047 return drv->assoc_freq;
1048}
1049
1050
c2a04078
JM
1051static void mlme_event_assoc(struct wpa_driver_nl80211_data *drv,
1052 const u8 *frame, size_t len)
1053{
1054 const struct ieee80211_mgmt *mgmt;
1055 union wpa_event_data event;
1056 u16 status;
1057
1058 mgmt = (const struct ieee80211_mgmt *) frame;
1059 if (len < 24 + sizeof(mgmt->u.assoc_resp)) {
1060 wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
1061 "frame");
1062 return;
1063 }
1064
1065 status = le_to_host16(mgmt->u.assoc_resp.status_code);
1066 if (status != WLAN_STATUS_SUCCESS) {
efa46078 1067 os_memset(&event, 0, sizeof(event));
59ddf221 1068 event.assoc_reject.bssid = mgmt->bssid;
efa46078
JM
1069 if (len > 24 + sizeof(mgmt->u.assoc_resp)) {
1070 event.assoc_reject.resp_ies =
1071 (u8 *) mgmt->u.assoc_resp.variable;
1072 event.assoc_reject.resp_ies_len =
1073 len - 24 - sizeof(mgmt->u.assoc_resp);
1074 }
1075 event.assoc_reject.status_code = status;
1076
1077 wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
c2a04078
JM
1078 return;
1079 }
1080
1081 drv->associated = 1;
1082 os_memcpy(drv->bssid, mgmt->sa, ETH_ALEN);
1083
1084 os_memset(&event, 0, sizeof(event));
1085 if (len > 24 + sizeof(mgmt->u.assoc_resp)) {
1086 event.assoc_info.resp_ies = (u8 *) mgmt->u.assoc_resp.variable;
1087 event.assoc_info.resp_ies_len =
efa46078 1088 len - 24 - sizeof(mgmt->u.assoc_resp);
c2a04078
JM
1089 }
1090
4832ecd7
JM
1091 event.assoc_info.freq = drv->assoc_freq;
1092
c2a04078
JM
1093 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
1094}
1095
c1bb3e0a 1096
da72a1c1
ZY
1097static void mlme_event_connect(struct wpa_driver_nl80211_data *drv,
1098 enum nl80211_commands cmd, struct nlattr *status,
1099 struct nlattr *addr, struct nlattr *req_ie,
1100 struct nlattr *resp_ie)
1101{
1102 union wpa_event_data event;
1103
7da2c527
JM
1104 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
1105 /*
1106 * Avoid reporting two association events that would confuse
1107 * the core code.
1108 */
1109 wpa_printf(MSG_DEBUG, "nl80211: Ignore connect event (cmd=%d) "
1110 "when using userspace SME", cmd);
1111 return;
1112 }
1113
da72a1c1
ZY
1114 os_memset(&event, 0, sizeof(event));
1115 if (cmd == NL80211_CMD_CONNECT &&
1116 nla_get_u16(status) != WLAN_STATUS_SUCCESS) {
df89c1c8
JM
1117 if (addr)
1118 event.assoc_reject.bssid = nla_data(addr);
da72a1c1
ZY
1119 if (resp_ie) {
1120 event.assoc_reject.resp_ies = nla_data(resp_ie);
1121 event.assoc_reject.resp_ies_len = nla_len(resp_ie);
1122 }
1123 event.assoc_reject.status_code = nla_get_u16(status);
1124 wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
1125 return;
1126 }
1127
1128 drv->associated = 1;
1129 if (addr)
1130 os_memcpy(drv->bssid, nla_data(addr), ETH_ALEN);
1131
1132 if (req_ie) {
1133 event.assoc_info.req_ies = nla_data(req_ie);
1134 event.assoc_info.req_ies_len = nla_len(req_ie);
1135 }
1136 if (resp_ie) {
1137 event.assoc_info.resp_ies = nla_data(resp_ie);
1138 event.assoc_info.resp_ies_len = nla_len(resp_ie);
1139 }
1140
f5a8d422
JM
1141 event.assoc_info.freq = nl80211_get_assoc_freq(drv);
1142
da72a1c1
ZY
1143 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
1144}
c2a04078 1145
c1bb3e0a 1146
20f5a4c2 1147static void mlme_event_disconnect(struct wpa_driver_nl80211_data *drv,
3d9975d5
JM
1148 struct nlattr *reason, struct nlattr *addr,
1149 struct nlattr *by_ap)
20f5a4c2
JM
1150{
1151 union wpa_event_data data;
1152
1153 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
1154 /*
1155 * Avoid reporting two disassociation events that could
1156 * confuse the core code.
1157 */
1158 wpa_printf(MSG_DEBUG, "nl80211: Ignore disconnect "
1159 "event when using userspace SME");
1160 return;
1161 }
1162
1163 drv->associated = 0;
1164 os_memset(&data, 0, sizeof(data));
1165 if (reason)
1166 data.disassoc_info.reason_code = nla_get_u16(reason);
3d9975d5 1167 data.disassoc_info.locally_generated = by_ap == NULL;
20f5a4c2
JM
1168 wpa_supplicant_event(drv->ctx, EVENT_DISASSOC, &data);
1169}
1170
1171
da1fb17c
JM
1172static void mlme_timeout_event(struct wpa_driver_nl80211_data *drv,
1173 enum nl80211_commands cmd, struct nlattr *addr)
1174{
1175 union wpa_event_data event;
1176 enum wpa_event_type ev;
1177
1178 if (nla_len(addr) != ETH_ALEN)
1179 return;
1180
1181 wpa_printf(MSG_DEBUG, "nl80211: MLME event %d; timeout with " MACSTR,
1182 cmd, MAC2STR((u8 *) nla_data(addr)));
1183
1184 if (cmd == NL80211_CMD_AUTHENTICATE)
1185 ev = EVENT_AUTH_TIMED_OUT;
1186 else if (cmd == NL80211_CMD_ASSOCIATE)
1187 ev = EVENT_ASSOC_TIMED_OUT;
1188 else
1189 return;
1190
1191 os_memset(&event, 0, sizeof(event));
1192 os_memcpy(event.timeout_event.addr, nla_data(addr), ETH_ALEN);
1193 wpa_supplicant_event(drv->ctx, ev, &event);
1194}
1195
1196
5582a5d1
JB
1197static void mlme_event_mgmt(struct wpa_driver_nl80211_data *drv,
1198 struct nlattr *freq, const u8 *frame, size_t len)
58f6fbe0
JM
1199{
1200 const struct ieee80211_mgmt *mgmt;
1201 union wpa_event_data event;
1202 u16 fc, stype;
1203
1204 mgmt = (const struct ieee80211_mgmt *) frame;
1205 if (len < 24) {
1206 wpa_printf(MSG_DEBUG, "nl80211: Too short action frame");
1207 return;
1208 }
1209
1210 fc = le_to_host16(mgmt->frame_control);
1211 stype = WLAN_FC_GET_STYPE(fc);
1212
1213 os_memset(&event, 0, sizeof(event));
5582a5d1 1214 if (freq) {
58f6fbe0 1215 event.rx_action.freq = nla_get_u32(freq);
5582a5d1
JB
1216 drv->last_mgmt_freq = event.rx_action.freq;
1217 }
1218 if (stype == WLAN_FC_STYPE_ACTION) {
1219 event.rx_action.da = mgmt->da;
1220 event.rx_action.sa = mgmt->sa;
1221 event.rx_action.bssid = mgmt->bssid;
1222 event.rx_action.category = mgmt->u.action.category;
1223 event.rx_action.data = &mgmt->u.action.category + 1;
1224 event.rx_action.len = frame + len - event.rx_action.data;
1225 wpa_supplicant_event(drv->ctx, EVENT_RX_ACTION, &event);
1226 } else {
1227 event.rx_mgmt.frame = frame;
1228 event.rx_mgmt.frame_len = len;
1229 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
1230 }
58f6fbe0
JM
1231}
1232
1233
a11241fa
JB
1234static void mlme_event_mgmt_tx_status(struct wpa_driver_nl80211_data *drv,
1235 struct nlattr *cookie, const u8 *frame,
1236 size_t len, struct nlattr *ack)
58f6fbe0
JM
1237{
1238 union wpa_event_data event;
1239 const struct ieee80211_hdr *hdr;
1240 u16 fc;
58f6fbe0 1241
a11241fa
JB
1242 if (!is_ap_interface(drv->nlmode)) {
1243 u64 cookie_val;
58f6fbe0 1244
a11241fa
JB
1245 if (!cookie)
1246 return;
1247
1248 cookie_val = nla_get_u64(cookie);
1249 wpa_printf(MSG_DEBUG, "nl80211: Action TX status:"
1250 " cookie=0%llx%s (ack=%d)",
1251 (long long unsigned int) cookie_val,
1252 cookie_val == drv->send_action_cookie ?
1253 " (match)" : " (unknown)", ack != NULL);
1254 if (cookie_val != drv->send_action_cookie)
1255 return;
1256 }
58f6fbe0
JM
1257
1258 hdr = (const struct ieee80211_hdr *) frame;
1259 fc = le_to_host16(hdr->frame_control);
1260
1261 os_memset(&event, 0, sizeof(event));
1262 event.tx_status.type = WLAN_FC_GET_TYPE(fc);
1263 event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
1264 event.tx_status.dst = hdr->addr1;
1265 event.tx_status.data = frame;
1266 event.tx_status.data_len = len;
1267 event.tx_status.ack = ack != NULL;
1268 wpa_supplicant_event(drv->ctx, EVENT_TX_STATUS, &event);
1269}
1270
1271
0544b242
JM
1272static void mlme_event_deauth_disassoc(struct wpa_driver_nl80211_data *drv,
1273 enum wpa_event_type type,
1274 const u8 *frame, size_t len)
1275{
1276 const struct ieee80211_mgmt *mgmt;
1277 union wpa_event_data event;
1278 const u8 *bssid = NULL;
1279 u16 reason_code = 0;
1280
cb30b297
PS
1281 mgmt = (const struct ieee80211_mgmt *) frame;
1282 if (len >= 24) {
1283 bssid = mgmt->bssid;
1284
1285 if (drv->associated != 0 &&
1286 os_memcmp(bssid, drv->bssid, ETH_ALEN) != 0 &&
1287 os_memcmp(bssid, drv->auth_bssid, ETH_ALEN) != 0) {
1288 /*
1289 * We have presumably received this deauth as a
1290 * response to a clear_state_mismatch() outgoing
1291 * deauth. Don't let it take us offline!
1292 */
1293 wpa_printf(MSG_DEBUG, "nl80211: Deauth received "
1294 "from Unknown BSSID " MACSTR " -- ignoring",
1295 MAC2STR(bssid));
1296 return;
1297 }
1298 }
1299
0544b242
JM
1300 drv->associated = 0;
1301 os_memset(&event, 0, sizeof(event));
1302
0544b242
JM
1303 /* Note: Same offset for Reason Code in both frame subtypes */
1304 if (len >= 24 + sizeof(mgmt->u.deauth))
1305 reason_code = le_to_host16(mgmt->u.deauth.reason_code);
1306
1307 if (type == EVENT_DISASSOC) {
3d9975d5
JM
1308 event.disassoc_info.locally_generated =
1309 !os_memcmp(mgmt->sa, drv->first_bss.addr, ETH_ALEN);
0544b242
JM
1310 event.disassoc_info.addr = bssid;
1311 event.disassoc_info.reason_code = reason_code;
046b26a2
JM
1312 if (frame + len > mgmt->u.disassoc.variable) {
1313 event.disassoc_info.ie = mgmt->u.disassoc.variable;
1314 event.disassoc_info.ie_len = frame + len -
1315 mgmt->u.disassoc.variable;
1316 }
0544b242 1317 } else {
3d9975d5
JM
1318 event.deauth_info.locally_generated =
1319 !os_memcmp(mgmt->sa, drv->first_bss.addr, ETH_ALEN);
0544b242
JM
1320 event.deauth_info.addr = bssid;
1321 event.deauth_info.reason_code = reason_code;
046b26a2
JM
1322 if (frame + len > mgmt->u.deauth.variable) {
1323 event.deauth_info.ie = mgmt->u.deauth.variable;
1324 event.deauth_info.ie_len = frame + len -
1325 mgmt->u.deauth.variable;
1326 }
0544b242
JM
1327 }
1328
1329 wpa_supplicant_event(drv->ctx, type, &event);
1330}
1331
1332
7d878ca7
JM
1333static void mlme_event_unprot_disconnect(struct wpa_driver_nl80211_data *drv,
1334 enum wpa_event_type type,
1335 const u8 *frame, size_t len)
1336{
1337 const struct ieee80211_mgmt *mgmt;
1338 union wpa_event_data event;
1339 u16 reason_code = 0;
1340
1341 if (len < 24)
1342 return;
1343
1344 mgmt = (const struct ieee80211_mgmt *) frame;
1345
1346 os_memset(&event, 0, sizeof(event));
1347 /* Note: Same offset for Reason Code in both frame subtypes */
1348 if (len >= 24 + sizeof(mgmt->u.deauth))
1349 reason_code = le_to_host16(mgmt->u.deauth.reason_code);
1350
1351 if (type == EVENT_UNPROT_DISASSOC) {
1352 event.unprot_disassoc.sa = mgmt->sa;
1353 event.unprot_disassoc.da = mgmt->da;
1354 event.unprot_disassoc.reason_code = reason_code;
1355 } else {
1356 event.unprot_deauth.sa = mgmt->sa;
1357 event.unprot_deauth.da = mgmt->da;
1358 event.unprot_deauth.reason_code = reason_code;
1359 }
1360
1361 wpa_supplicant_event(drv->ctx, type, &event);
1362}
1363
1364
c2a04078 1365static void mlme_event(struct wpa_driver_nl80211_data *drv,
da1fb17c 1366 enum nl80211_commands cmd, struct nlattr *frame,
58f6fbe0
JM
1367 struct nlattr *addr, struct nlattr *timed_out,
1368 struct nlattr *freq, struct nlattr *ack,
1369 struct nlattr *cookie)
c2a04078 1370{
da1fb17c
JM
1371 if (timed_out && addr) {
1372 mlme_timeout_event(drv, cmd, addr);
1373 return;
1374 }
1375
c2a04078
JM
1376 if (frame == NULL) {
1377 wpa_printf(MSG_DEBUG, "nl80211: MLME event %d without frame "
1378 "data", cmd);
1379 return;
1380 }
1381
1382 wpa_printf(MSG_DEBUG, "nl80211: MLME event %d", cmd);
1383 wpa_hexdump(MSG_MSGDUMP, "nl80211: MLME event frame",
1384 nla_data(frame), nla_len(frame));
1385
1386 switch (cmd) {
1387 case NL80211_CMD_AUTHENTICATE:
1388 mlme_event_auth(drv, nla_data(frame), nla_len(frame));
1389 break;
1390 case NL80211_CMD_ASSOCIATE:
1391 mlme_event_assoc(drv, nla_data(frame), nla_len(frame));
1392 break;
1393 case NL80211_CMD_DEAUTHENTICATE:
0544b242
JM
1394 mlme_event_deauth_disassoc(drv, EVENT_DEAUTH,
1395 nla_data(frame), nla_len(frame));
c2a04078
JM
1396 break;
1397 case NL80211_CMD_DISASSOCIATE:
0544b242
JM
1398 mlme_event_deauth_disassoc(drv, EVENT_DISASSOC,
1399 nla_data(frame), nla_len(frame));
c2a04078 1400 break;
bd94971e 1401 case NL80211_CMD_FRAME:
5582a5d1 1402 mlme_event_mgmt(drv, freq, nla_data(frame), nla_len(frame));
58f6fbe0 1403 break;
bd94971e 1404 case NL80211_CMD_FRAME_TX_STATUS:
a11241fa
JB
1405 mlme_event_mgmt_tx_status(drv, cookie, nla_data(frame),
1406 nla_len(frame), ack);
58f6fbe0 1407 break;
7d878ca7
JM
1408 case NL80211_CMD_UNPROT_DEAUTHENTICATE:
1409 mlme_event_unprot_disconnect(drv, EVENT_UNPROT_DEAUTH,
1410 nla_data(frame), nla_len(frame));
1411 break;
1412 case NL80211_CMD_UNPROT_DISASSOCIATE:
1413 mlme_event_unprot_disconnect(drv, EVENT_UNPROT_DISASSOC,
1414 nla_data(frame), nla_len(frame));
1415 break;
c2a04078
JM
1416 default:
1417 break;
1418 }
1419}
1420
1421
35583f3f
JM
1422static void mlme_event_michael_mic_failure(struct wpa_driver_nl80211_data *drv,
1423 struct nlattr *tb[])
1424{
1425 union wpa_event_data data;
1426
1427 wpa_printf(MSG_DEBUG, "nl80211: MLME event Michael MIC failure");
1428 os_memset(&data, 0, sizeof(data));
1429 if (tb[NL80211_ATTR_MAC]) {
1430 wpa_hexdump(MSG_DEBUG, "nl80211: Source MAC address",
1431 nla_data(tb[NL80211_ATTR_MAC]),
1432 nla_len(tb[NL80211_ATTR_MAC]));
ad1e68e6 1433 data.michael_mic_failure.src = nla_data(tb[NL80211_ATTR_MAC]);
35583f3f
JM
1434 }
1435 if (tb[NL80211_ATTR_KEY_SEQ]) {
1436 wpa_hexdump(MSG_DEBUG, "nl80211: TSC",
1437 nla_data(tb[NL80211_ATTR_KEY_SEQ]),
1438 nla_len(tb[NL80211_ATTR_KEY_SEQ]));
1439 }
1440 if (tb[NL80211_ATTR_KEY_TYPE]) {
1441 enum nl80211_key_type key_type =
1442 nla_get_u32(tb[NL80211_ATTR_KEY_TYPE]);
1443 wpa_printf(MSG_DEBUG, "nl80211: Key Type %d", key_type);
1444 if (key_type == NL80211_KEYTYPE_PAIRWISE)
1445 data.michael_mic_failure.unicast = 1;
1446 } else
1447 data.michael_mic_failure.unicast = 1;
1448
1449 if (tb[NL80211_ATTR_KEY_IDX]) {
1450 u8 key_id = nla_get_u8(tb[NL80211_ATTR_KEY_IDX]);
1451 wpa_printf(MSG_DEBUG, "nl80211: Key Id %d", key_id);
1452 }
1453
1454 wpa_supplicant_event(drv->ctx, EVENT_MICHAEL_MIC_FAILURE, &data);
1455}
1456
1457
5cc4d64b
JM
1458static void mlme_event_join_ibss(struct wpa_driver_nl80211_data *drv,
1459 struct nlattr *tb[])
1460{
1461 if (tb[NL80211_ATTR_MAC] == NULL) {
1462 wpa_printf(MSG_DEBUG, "nl80211: No address in IBSS joined "
1463 "event");
1464 return;
1465 }
1466 os_memcpy(drv->bssid, nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
1467 drv->associated = 1;
1468 wpa_printf(MSG_DEBUG, "nl80211: IBSS " MACSTR " joined",
1469 MAC2STR(drv->bssid));
1470
1471 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, NULL);
1472}
1473
1474
55777702
JM
1475static void mlme_event_remain_on_channel(struct wpa_driver_nl80211_data *drv,
1476 int cancel_event, struct nlattr *tb[])
1477{
1478 unsigned int freq, chan_type, duration;
1479 union wpa_event_data data;
1480 u64 cookie;
1481
1482 if (tb[NL80211_ATTR_WIPHY_FREQ])
1483 freq = nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]);
1484 else
1485 freq = 0;
1486
1487 if (tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE])
1488 chan_type = nla_get_u32(tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1489 else
1490 chan_type = 0;
1491
1492 if (tb[NL80211_ATTR_DURATION])
1493 duration = nla_get_u32(tb[NL80211_ATTR_DURATION]);
1494 else
1495 duration = 0;
1496
1497 if (tb[NL80211_ATTR_COOKIE])
1498 cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
1499 else
1500 cookie = 0;
1501
1502 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel event (cancel=%d "
1503 "freq=%u channel_type=%u duration=%u cookie=0x%llx (%s))",
1504 cancel_event, freq, chan_type, duration,
1505 (long long unsigned int) cookie,
1506 cookie == drv->remain_on_chan_cookie ? "match" : "unknown");
1507
1508 if (cookie != drv->remain_on_chan_cookie)
1509 return; /* not for us */
1510
531f0331
JB
1511 if (cancel_event)
1512 drv->pending_remain_on_chan = 0;
55777702
JM
1513
1514 os_memset(&data, 0, sizeof(data));
1515 data.remain_on_channel.freq = freq;
1516 data.remain_on_channel.duration = duration;
1517 wpa_supplicant_event(drv->ctx, cancel_event ?
1518 EVENT_CANCEL_REMAIN_ON_CHANNEL :
1519 EVENT_REMAIN_ON_CHANNEL, &data);
1520}
1521
1522
8d923a4a
JM
1523static void send_scan_event(struct wpa_driver_nl80211_data *drv, int aborted,
1524 struct nlattr *tb[])
1525{
1526 union wpa_event_data event;
1527 struct nlattr *nl;
1528 int rem;
1529 struct scan_info *info;
1530#define MAX_REPORT_FREQS 50
1531 int freqs[MAX_REPORT_FREQS];
1532 int num_freqs = 0;
1533
536fd62d
JM
1534 if (drv->scan_for_auth) {
1535 drv->scan_for_auth = 0;
1536 wpa_printf(MSG_DEBUG, "nl80211: Scan results for missing "
1537 "cfg80211 BSS entry");
1538 wpa_driver_nl80211_authenticate_retry(drv);
1539 return;
1540 }
1541
8d923a4a
JM
1542 os_memset(&event, 0, sizeof(event));
1543 info = &event.scan_info;
1544 info->aborted = aborted;
1545
1546 if (tb[NL80211_ATTR_SCAN_SSIDS]) {
1547 nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_SSIDS], rem) {
1548 struct wpa_driver_scan_ssid *s =
1549 &info->ssids[info->num_ssids];
1550 s->ssid = nla_data(nl);
1551 s->ssid_len = nla_len(nl);
1552 info->num_ssids++;
1553 if (info->num_ssids == WPAS_MAX_SCAN_SSIDS)
1554 break;
1555 }
1556 }
1557 if (tb[NL80211_ATTR_SCAN_FREQUENCIES]) {
1558 nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_FREQUENCIES], rem)
1559 {
1560 freqs[num_freqs] = nla_get_u32(nl);
1561 num_freqs++;
1562 if (num_freqs == MAX_REPORT_FREQS - 1)
1563 break;
1564 }
1565 info->freqs = freqs;
1566 info->num_freqs = num_freqs;
1567 }
1568 wpa_supplicant_event(drv->ctx, EVENT_SCAN_RESULTS, &event);
1569}
1570
1571
60a972a6
JM
1572static int get_link_signal(struct nl_msg *msg, void *arg)
1573{
1574 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1575 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1576 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
1577 static struct nla_policy policy[NL80211_STA_INFO_MAX + 1] = {
1578 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
1579 };
7ee35bf3
PS
1580 struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
1581 static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
1582 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
1583 [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
1584 [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
1585 [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
1586 };
1c5c7273 1587 struct wpa_signal_info *sig_change = arg;
60a972a6
JM
1588
1589 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1590 genlmsg_attrlen(gnlh, 0), NULL);
1591 if (!tb[NL80211_ATTR_STA_INFO] ||
1592 nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
1593 tb[NL80211_ATTR_STA_INFO], policy))
1594 return NL_SKIP;
1595 if (!sinfo[NL80211_STA_INFO_SIGNAL])
1596 return NL_SKIP;
1597
7ee35bf3
PS
1598 sig_change->current_signal =
1599 (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
1600
1601 if (sinfo[NL80211_STA_INFO_TX_BITRATE]) {
1602 if (nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
1603 sinfo[NL80211_STA_INFO_TX_BITRATE],
1604 rate_policy)) {
1605 sig_change->current_txrate = 0;
1606 } else {
1607 if (rinfo[NL80211_RATE_INFO_BITRATE]) {
1608 sig_change->current_txrate =
1609 nla_get_u16(rinfo[
1610 NL80211_RATE_INFO_BITRATE]) * 100;
1611 }
1612 }
1613 }
1614
60a972a6
JM
1615 return NL_SKIP;
1616}
1617
1618
1619static int nl80211_get_link_signal(struct wpa_driver_nl80211_data *drv,
1c5c7273 1620 struct wpa_signal_info *sig)
60a972a6
JM
1621{
1622 struct nl_msg *msg;
1623
7ee35bf3
PS
1624 sig->current_signal = -9999;
1625 sig->current_txrate = 0;
60a972a6
JM
1626
1627 msg = nlmsg_alloc();
1628 if (!msg)
1629 return -ENOMEM;
1630
9fb04070 1631 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_STATION);
60a972a6
JM
1632
1633 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
1634 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid);
1635
1636 return send_and_recv_msgs(drv, msg, get_link_signal, sig);
1637 nla_put_failure:
5883168a 1638 nlmsg_free(msg);
60a972a6
JM
1639 return -ENOBUFS;
1640}
1641
1642
7ee35bf3
PS
1643static int get_link_noise(struct nl_msg *msg, void *arg)
1644{
1645 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1646 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1647 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
1648 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
1649 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
1650 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
1651 };
1c5c7273 1652 struct wpa_signal_info *sig_change = arg;
7ee35bf3
PS
1653
1654 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1655 genlmsg_attrlen(gnlh, 0), NULL);
1656
1657 if (!tb[NL80211_ATTR_SURVEY_INFO]) {
1658 wpa_printf(MSG_DEBUG, "nl80211: survey data missing!");
1659 return NL_SKIP;
1660 }
1661
1662 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
1663 tb[NL80211_ATTR_SURVEY_INFO],
1664 survey_policy)) {
1665 wpa_printf(MSG_DEBUG, "nl80211: failed to parse nested "
1666 "attributes!");
1667 return NL_SKIP;
1668 }
1669
1670 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
1671 return NL_SKIP;
1672
1673 if (nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
1674 sig_change->frequency)
1675 return NL_SKIP;
1676
1677 if (!sinfo[NL80211_SURVEY_INFO_NOISE])
1678 return NL_SKIP;
1679
1680 sig_change->current_noise =
1681 (s8) nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
1682
1683 return NL_SKIP;
1684}
1685
1686
1687static int nl80211_get_link_noise(struct wpa_driver_nl80211_data *drv,
1c5c7273 1688 struct wpa_signal_info *sig_change)
7ee35bf3
PS
1689{
1690 struct nl_msg *msg;
1691
1692 sig_change->current_noise = 9999;
1693 sig_change->frequency = drv->assoc_freq;
1694
1695 msg = nlmsg_alloc();
1696 if (!msg)
1697 return -ENOMEM;
1698
9fb04070 1699 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
7ee35bf3
PS
1700
1701 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
1702
1703 return send_and_recv_msgs(drv, msg, get_link_noise, sig_change);
1704 nla_put_failure:
5883168a 1705 nlmsg_free(msg);
7ee35bf3
PS
1706 return -ENOBUFS;
1707}
1708
1709
577db0ae
GM
1710static int get_noise_for_scan_results(struct nl_msg *msg, void *arg)
1711{
1712 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1713 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1714 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
1715 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
1716 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
1717 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
1718 };
1719 struct wpa_scan_results *scan_results = arg;
1720 struct wpa_scan_res *scan_res;
1721 size_t i;
1722
1723 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1724 genlmsg_attrlen(gnlh, 0), NULL);
1725
1726 if (!tb[NL80211_ATTR_SURVEY_INFO]) {
1727 wpa_printf(MSG_DEBUG, "nl80211: Survey data missing");
1728 return NL_SKIP;
1729 }
1730
1731 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
1732 tb[NL80211_ATTR_SURVEY_INFO],
1733 survey_policy)) {
1734 wpa_printf(MSG_DEBUG, "nl80211: Failed to parse nested "
1735 "attributes");
1736 return NL_SKIP;
1737 }
1738
1739 if (!sinfo[NL80211_SURVEY_INFO_NOISE])
1740 return NL_SKIP;
1741
1742 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
1743 return NL_SKIP;
1744
1745 for (i = 0; i < scan_results->num; ++i) {
1746 scan_res = scan_results->res[i];
1747 if (!scan_res)
1748 continue;
1749 if ((int) nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
1750 scan_res->freq)
1751 continue;
1752 if (!(scan_res->flags & WPA_SCAN_NOISE_INVALID))
1753 continue;
1754 scan_res->noise = (s8)
1755 nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
1756 scan_res->flags &= ~WPA_SCAN_NOISE_INVALID;
1757 }
1758
1759 return NL_SKIP;
1760}
1761
1762
1763static int nl80211_get_noise_for_scan_results(
1764 struct wpa_driver_nl80211_data *drv,
1765 struct wpa_scan_results *scan_res)
1766{
1767 struct nl_msg *msg;
1768
1769 msg = nlmsg_alloc();
1770 if (!msg)
1771 return -ENOMEM;
1772
1773 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
1774
1775 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
1776
1777 return send_and_recv_msgs(drv, msg, get_noise_for_scan_results,
1778 scan_res);
1779 nla_put_failure:
9e088e74 1780 nlmsg_free(msg);
577db0ae
GM
1781 return -ENOBUFS;
1782}
1783
1784
93910401
JM
1785static void nl80211_cqm_event(struct wpa_driver_nl80211_data *drv,
1786 struct nlattr *tb[])
1787{
1788 static struct nla_policy cqm_policy[NL80211_ATTR_CQM_MAX + 1] = {
1789 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
1790 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U8 },
1791 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
0d7e5a3a 1792 [NL80211_ATTR_CQM_PKT_LOSS_EVENT] = { .type = NLA_U32 },
93910401
JM
1793 };
1794 struct nlattr *cqm[NL80211_ATTR_CQM_MAX + 1];
1795 enum nl80211_cqm_rssi_threshold_event event;
b625473c 1796 union wpa_event_data ed;
1c5c7273 1797 struct wpa_signal_info sig;
7ee35bf3 1798 int res;
93910401
JM
1799
1800 if (tb[NL80211_ATTR_CQM] == NULL ||
1801 nla_parse_nested(cqm, NL80211_ATTR_CQM_MAX, tb[NL80211_ATTR_CQM],
1802 cqm_policy)) {
1803 wpa_printf(MSG_DEBUG, "nl80211: Ignore invalid CQM event");
1804 return;
1805 }
1806
0d7e5a3a
JB
1807 os_memset(&ed, 0, sizeof(ed));
1808
1809 if (cqm[NL80211_ATTR_CQM_PKT_LOSS_EVENT]) {
1810 if (!tb[NL80211_ATTR_MAC])
1811 return;
1812 os_memcpy(ed.low_ack.addr, nla_data(tb[NL80211_ATTR_MAC]),
1813 ETH_ALEN);
1814 wpa_supplicant_event(drv->ctx, EVENT_STATION_LOW_ACK, &ed);
1815 return;
1816 }
1817
93910401
JM
1818 if (cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] == NULL)
1819 return;
1820 event = nla_get_u32(cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT]);
b625473c 1821
93910401
JM
1822 if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH) {
1823 wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor "
1824 "event: RSSI high");
b625473c 1825 ed.signal_change.above_threshold = 1;
93910401
JM
1826 } else if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW) {
1827 wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor "
1828 "event: RSSI low");
b625473c
JM
1829 ed.signal_change.above_threshold = 0;
1830 } else
1831 return;
1832
60a972a6
JM
1833 res = nl80211_get_link_signal(drv, &sig);
1834 if (res == 0) {
7ee35bf3
PS
1835 ed.signal_change.current_signal = sig.current_signal;
1836 ed.signal_change.current_txrate = sig.current_txrate;
1837 wpa_printf(MSG_DEBUG, "nl80211: Signal: %d dBm txrate: %d",
1838 sig.current_signal, sig.current_txrate);
1839 }
1840
1841 res = nl80211_get_link_noise(drv, &sig);
1842 if (res == 0) {
1843 ed.signal_change.current_noise = sig.current_noise;
1844 wpa_printf(MSG_DEBUG, "nl80211: Noise: %d dBm",
1845 sig.current_noise);
60a972a6
JM
1846 }
1847
b625473c 1848 wpa_supplicant_event(drv->ctx, EVENT_SIGNAL_CHANGE, &ed);
93910401
JM
1849}
1850
1851
18d2ba08
JM
1852static void nl80211_new_station_event(struct wpa_driver_nl80211_data *drv,
1853 struct nlattr **tb)
1854{
1855 u8 *addr;
1856 union wpa_event_data data;
1857
1858 if (tb[NL80211_ATTR_MAC] == NULL)
1859 return;
1860 addr = nla_data(tb[NL80211_ATTR_MAC]);
1861 wpa_printf(MSG_DEBUG, "nl80211: New station " MACSTR, MAC2STR(addr));
5f310a9e 1862
61cbe2ff 1863 if (is_ap_interface(drv->nlmode) && drv->device_ap_sme) {
5f310a9e
JM
1864 u8 *ies = NULL;
1865 size_t ies_len = 0;
1866 if (tb[NL80211_ATTR_IE]) {
1867 ies = nla_data(tb[NL80211_ATTR_IE]);
1868 ies_len = nla_len(tb[NL80211_ATTR_IE]);
1869 }
1870 wpa_hexdump(MSG_DEBUG, "nl80211: Assoc Req IEs", ies, ies_len);
1871 drv_event_assoc(drv->ctx, addr, ies, ies_len, 0);
1872 return;
1873 }
1874
18d2ba08
JM
1875 if (drv->nlmode != NL80211_IFTYPE_ADHOC)
1876 return;
1877
1878 os_memset(&data, 0, sizeof(data));
1879 os_memcpy(data.ibss_rsn_start.peer, addr, ETH_ALEN);
1880 wpa_supplicant_event(drv->ctx, EVENT_IBSS_RSN_START, &data);
1881}
1882
1883
ef985058
JM
1884static void nl80211_del_station_event(struct wpa_driver_nl80211_data *drv,
1885 struct nlattr **tb)
1886{
1887 u8 *addr;
1888 union wpa_event_data data;
1889
1890 if (tb[NL80211_ATTR_MAC] == NULL)
1891 return;
1892 addr = nla_data(tb[NL80211_ATTR_MAC]);
1893 wpa_printf(MSG_DEBUG, "nl80211: Delete station " MACSTR,
1894 MAC2STR(addr));
5f310a9e 1895
61cbe2ff 1896 if (is_ap_interface(drv->nlmode) && drv->device_ap_sme) {
5f310a9e
JM
1897 drv_event_disassoc(drv->ctx, addr);
1898 return;
1899 }
1900
ef985058
JM
1901 if (drv->nlmode != NL80211_IFTYPE_ADHOC)
1902 return;
1903
1904 os_memset(&data, 0, sizeof(data));
1905 os_memcpy(data.ibss_peer_lost.peer, addr, ETH_ALEN);
1906 wpa_supplicant_event(drv->ctx, EVENT_IBSS_PEER_LOST, &data);
1907}
1908
1909
b14a210c
JB
1910static void nl80211_rekey_offload_event(struct wpa_driver_nl80211_data *drv,
1911 struct nlattr **tb)
1912{
1913 struct nlattr *rekey_info[NUM_NL80211_REKEY_DATA];
1914 static struct nla_policy rekey_policy[NUM_NL80211_REKEY_DATA] = {
1915 [NL80211_REKEY_DATA_KEK] = {
1916 .minlen = NL80211_KEK_LEN,
1917 .maxlen = NL80211_KEK_LEN,
1918 },
1919 [NL80211_REKEY_DATA_KCK] = {
1920 .minlen = NL80211_KCK_LEN,
1921 .maxlen = NL80211_KCK_LEN,
1922 },
1923 [NL80211_REKEY_DATA_REPLAY_CTR] = {
1924 .minlen = NL80211_REPLAY_CTR_LEN,
1925 .maxlen = NL80211_REPLAY_CTR_LEN,
1926 },
1927 };
1928 union wpa_event_data data;
1929
1930 if (!tb[NL80211_ATTR_MAC])
1931 return;
1932 if (!tb[NL80211_ATTR_REKEY_DATA])
1933 return;
1934 if (nla_parse_nested(rekey_info, MAX_NL80211_REKEY_DATA,
1935 tb[NL80211_ATTR_REKEY_DATA], rekey_policy))
1936 return;
1937 if (!rekey_info[NL80211_REKEY_DATA_REPLAY_CTR])
1938 return;
1939
1940 os_memset(&data, 0, sizeof(data));
1941 data.driver_gtk_rekey.bssid = nla_data(tb[NL80211_ATTR_MAC]);
1942 wpa_printf(MSG_DEBUG, "nl80211: Rekey offload event for BSSID " MACSTR,
1943 MAC2STR(data.driver_gtk_rekey.bssid));
1944 data.driver_gtk_rekey.replay_ctr =
1945 nla_data(rekey_info[NL80211_REKEY_DATA_REPLAY_CTR]);
1946 wpa_hexdump(MSG_DEBUG, "nl80211: Rekey offload - Replay Counter",
1947 data.driver_gtk_rekey.replay_ctr, NL80211_REPLAY_CTR_LEN);
1948 wpa_supplicant_event(drv->ctx, EVENT_DRIVER_GTK_REKEY, &data);
1949}
1950
1951
c36d5242
JM
1952static void nl80211_pmksa_candidate_event(struct wpa_driver_nl80211_data *drv,
1953 struct nlattr **tb)
1954{
1955 struct nlattr *cand[NUM_NL80211_PMKSA_CANDIDATE];
1956 static struct nla_policy cand_policy[NUM_NL80211_PMKSA_CANDIDATE] = {
1957 [NL80211_PMKSA_CANDIDATE_INDEX] = { .type = NLA_U32 },
1958 [NL80211_PMKSA_CANDIDATE_BSSID] = {
1959 .minlen = ETH_ALEN,
1960 .maxlen = ETH_ALEN,
1961 },
1962 [NL80211_PMKSA_CANDIDATE_PREAUTH] = { .type = NLA_FLAG },
1963 };
1964 union wpa_event_data data;
1965
1966 if (!tb[NL80211_ATTR_PMKSA_CANDIDATE])
1967 return;
1968 if (nla_parse_nested(cand, MAX_NL80211_PMKSA_CANDIDATE,
1969 tb[NL80211_ATTR_PMKSA_CANDIDATE], cand_policy))
1970 return;
1971 if (!cand[NL80211_PMKSA_CANDIDATE_INDEX] ||
1972 !cand[NL80211_PMKSA_CANDIDATE_BSSID])
1973 return;
1974
1975 os_memset(&data, 0, sizeof(data));
1976 os_memcpy(data.pmkid_candidate.bssid,
1977 nla_data(cand[NL80211_PMKSA_CANDIDATE_BSSID]), ETH_ALEN);
1978 data.pmkid_candidate.index =
1979 nla_get_u32(cand[NL80211_PMKSA_CANDIDATE_INDEX]);
1980 data.pmkid_candidate.preauth =
1981 cand[NL80211_PMKSA_CANDIDATE_PREAUTH] != NULL;
1982 wpa_supplicant_event(drv->ctx, EVENT_PMKID_CANDIDATE, &data);
1983}
1984
1985
39718852
JB
1986static void nl80211_client_probe_event(struct wpa_driver_nl80211_data *drv,
1987 struct nlattr **tb)
1988{
1989 union wpa_event_data data;
1990
1991 if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_ACK])
1992 return;
1993
1994 os_memset(&data, 0, sizeof(data));
1995 os_memcpy(data.client_poll.addr,
1996 nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
1997
1998 wpa_supplicant_event(drv->ctx, EVENT_DRIVER_CLIENT_POLL_OK, &data);
1999}
2000
2001
3088e4e5
JB
2002static void nl80211_spurious_frame(struct i802_bss *bss, struct nlattr **tb,
2003 int wds)
02bb32c3
JB
2004{
2005 struct wpa_driver_nl80211_data *drv = bss->drv;
2006 union wpa_event_data event;
02bb32c3
JB
2007
2008 if (!tb[NL80211_ATTR_MAC])
2009 return;
2010
02bb32c3 2011 os_memset(&event, 0, sizeof(event));
341eebee 2012 event.rx_from_unknown.bssid = bss->addr;
02bb32c3 2013 event.rx_from_unknown.addr = nla_data(tb[NL80211_ATTR_MAC]);
3088e4e5 2014 event.rx_from_unknown.wds = wds;
02bb32c3
JB
2015
2016 wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event);
2017}
2018
2019
d6c9aab8
JB
2020static void do_process_drv_event(struct wpa_driver_nl80211_data *drv,
2021 int cmd, struct nlattr **tb)
97865538 2022{
b1f625e0 2023 if (drv->ap_scan_as_station != NL80211_IFTYPE_UNSPECIFIED &&
d6c9aab8
JB
2024 (cmd == NL80211_CMD_NEW_SCAN_RESULTS ||
2025 cmd == NL80211_CMD_SCAN_ABORTED)) {
a2e40bb6 2026 wpa_driver_nl80211_set_mode(&drv->first_bss,
b1f625e0
EP
2027 drv->ap_scan_as_station);
2028 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
ad1e68e6
JM
2029 }
2030
d6c9aab8 2031 switch (cmd) {
d942a79e
JM
2032 case NL80211_CMD_TRIGGER_SCAN:
2033 wpa_printf(MSG_DEBUG, "nl80211: Scan trigger");
2034 break;
d21c63b9
LC
2035 case NL80211_CMD_START_SCHED_SCAN:
2036 wpa_printf(MSG_DEBUG, "nl80211: Sched scan started");
2037 break;
2038 case NL80211_CMD_SCHED_SCAN_STOPPED:
2039 wpa_printf(MSG_DEBUG, "nl80211: Sched scan stopped");
2040 wpa_supplicant_event(drv->ctx, EVENT_SCHED_SCAN_STOPPED, NULL);
2041 break;
97865538
JM
2042 case NL80211_CMD_NEW_SCAN_RESULTS:
2043 wpa_printf(MSG_DEBUG, "nl80211: New scan results available");
2044 drv->scan_complete_events = 1;
2045 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
2046 drv->ctx);
8d923a4a 2047 send_scan_event(drv, 0, tb);
97865538 2048 break;
d21c63b9
LC
2049 case NL80211_CMD_SCHED_SCAN_RESULTS:
2050 wpa_printf(MSG_DEBUG,
2051 "nl80211: New sched scan results available");
2052 send_scan_event(drv, 0, tb);
2053 break;
97865538
JM
2054 case NL80211_CMD_SCAN_ABORTED:
2055 wpa_printf(MSG_DEBUG, "nl80211: Scan aborted");
2056 /*
2057 * Need to indicate that scan results are available in order
2058 * not to make wpa_supplicant stop its scanning.
2059 */
2060 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
2061 drv->ctx);
8d923a4a 2062 send_scan_event(drv, 1, tb);
97865538 2063 break;
c2a04078
JM
2064 case NL80211_CMD_AUTHENTICATE:
2065 case NL80211_CMD_ASSOCIATE:
2066 case NL80211_CMD_DEAUTHENTICATE:
2067 case NL80211_CMD_DISASSOCIATE:
bd94971e 2068 case NL80211_CMD_FRAME_TX_STATUS:
7d878ca7
JM
2069 case NL80211_CMD_UNPROT_DEAUTHENTICATE:
2070 case NL80211_CMD_UNPROT_DISASSOCIATE:
d6c9aab8 2071 mlme_event(drv, cmd, tb[NL80211_ATTR_FRAME],
58f6fbe0
JM
2072 tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT],
2073 tb[NL80211_ATTR_WIPHY_FREQ], tb[NL80211_ATTR_ACK],
2074 tb[NL80211_ATTR_COOKIE]);
c2a04078 2075 break;
da72a1c1
ZY
2076 case NL80211_CMD_CONNECT:
2077 case NL80211_CMD_ROAM:
d6c9aab8 2078 mlme_event_connect(drv, cmd,
da72a1c1
ZY
2079 tb[NL80211_ATTR_STATUS_CODE],
2080 tb[NL80211_ATTR_MAC],
2081 tb[NL80211_ATTR_REQ_IE],
2082 tb[NL80211_ATTR_RESP_IE]);
2083 break;
2084 case NL80211_CMD_DISCONNECT:
20f5a4c2 2085 mlme_event_disconnect(drv, tb[NL80211_ATTR_REASON_CODE],
3d9975d5
JM
2086 tb[NL80211_ATTR_MAC],
2087 tb[NL80211_ATTR_DISCONNECTED_BY_AP]);
da72a1c1 2088 break;
35583f3f
JM
2089 case NL80211_CMD_MICHAEL_MIC_FAILURE:
2090 mlme_event_michael_mic_failure(drv, tb);
2091 break;
5cc4d64b
JM
2092 case NL80211_CMD_JOIN_IBSS:
2093 mlme_event_join_ibss(drv, tb);
2094 break;
55777702
JM
2095 case NL80211_CMD_REMAIN_ON_CHANNEL:
2096 mlme_event_remain_on_channel(drv, 0, tb);
2097 break;
2098 case NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL:
2099 mlme_event_remain_on_channel(drv, 1, tb);
2100 break;
93910401
JM
2101 case NL80211_CMD_NOTIFY_CQM:
2102 nl80211_cqm_event(drv, tb);
2103 break;
33c5deb8
JM
2104 case NL80211_CMD_REG_CHANGE:
2105 wpa_printf(MSG_DEBUG, "nl80211: Regulatory domain change");
2106 wpa_supplicant_event(drv->ctx, EVENT_CHANNEL_LIST_CHANGED,
2107 NULL);
2108 break;
2109 case NL80211_CMD_REG_BEACON_HINT:
2110 wpa_printf(MSG_DEBUG, "nl80211: Regulatory beacon hint");
2111 wpa_supplicant_event(drv->ctx, EVENT_CHANNEL_LIST_CHANGED,
2112 NULL);
2113 break;
18d2ba08
JM
2114 case NL80211_CMD_NEW_STATION:
2115 nl80211_new_station_event(drv, tb);
2116 break;
ef985058
JM
2117 case NL80211_CMD_DEL_STATION:
2118 nl80211_del_station_event(drv, tb);
2119 break;
b14a210c
JB
2120 case NL80211_CMD_SET_REKEY_OFFLOAD:
2121 nl80211_rekey_offload_event(drv, tb);
2122 break;
c36d5242
JM
2123 case NL80211_CMD_PMKSA_CANDIDATE:
2124 nl80211_pmksa_candidate_event(drv, tb);
2125 break;
39718852
JB
2126 case NL80211_CMD_PROBE_CLIENT:
2127 nl80211_client_probe_event(drv, tb);
2128 break;
97865538 2129 default:
c2a04078 2130 wpa_printf(MSG_DEBUG, "nl80211: Ignored unknown event "
d6c9aab8 2131 "(cmd=%d)", cmd);
97865538
JM
2132 break;
2133 }
d6c9aab8
JB
2134}
2135
2136
2137static int process_drv_event(struct nl_msg *msg, void *arg)
2138{
2139 struct wpa_driver_nl80211_data *drv = arg;
2140 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2141 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2142
2143 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2144 genlmsg_attrlen(gnlh, 0), NULL);
2145
2146 if (tb[NL80211_ATTR_IFINDEX]) {
2147 int ifindex = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
2148 if (ifindex != drv->ifindex && !have_ifidx(drv, ifindex)) {
2149 wpa_printf(MSG_DEBUG, "nl80211: Ignored event (cmd=%d)"
2150 " for foreign interface (ifindex %d)",
2151 gnlh->cmd, ifindex);
2152 return NL_SKIP;
2153 }
2154 }
2155
2156 do_process_drv_event(drv, gnlh->cmd, tb);
2157 return NL_SKIP;
2158}
2159
2160
2161static int process_global_event(struct nl_msg *msg, void *arg)
2162{
2163 struct nl80211_global *global = arg;
2164 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2165 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2166 struct wpa_driver_nl80211_data *drv;
2167 int ifidx = -1;
2168
2169 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2170 genlmsg_attrlen(gnlh, 0), NULL);
2171
2172 if (tb[NL80211_ATTR_IFINDEX])
2173 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
2174
2175 dl_list_for_each(drv, &global->interfaces,
2176 struct wpa_driver_nl80211_data, list) {
2177 if (ifidx == -1 || ifidx == drv->ifindex ||
2178 have_ifidx(drv, ifidx))
2179 do_process_drv_event(drv, gnlh->cmd, tb);
2180 }
97865538
JM
2181
2182 return NL_SKIP;
2183}
2184
2185
cc7a48d1
JB
2186static int process_bss_event(struct nl_msg *msg, void *arg)
2187{
a11241fa 2188 struct i802_bss *bss = arg;
cc7a48d1
JB
2189 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2190 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2191
2192 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2193 genlmsg_attrlen(gnlh, 0), NULL);
2194
2195 switch (gnlh->cmd) {
a11241fa
JB
2196 case NL80211_CMD_FRAME:
2197 case NL80211_CMD_FRAME_TX_STATUS:
2198 mlme_event(bss->drv, gnlh->cmd, tb[NL80211_ATTR_FRAME],
2199 tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT],
2200 tb[NL80211_ATTR_WIPHY_FREQ], tb[NL80211_ATTR_ACK],
2201 tb[NL80211_ATTR_COOKIE]);
2202 break;
02bb32c3 2203 case NL80211_CMD_UNEXPECTED_FRAME:
3088e4e5
JB
2204 nl80211_spurious_frame(bss, tb, 0);
2205 break;
2206 case NL80211_CMD_UNEXPECTED_4ADDR_FRAME:
2207 nl80211_spurious_frame(bss, tb, 1);
02bb32c3 2208 break;
cc7a48d1
JB
2209 default:
2210 wpa_printf(MSG_DEBUG, "nl80211: Ignored unknown event "
2211 "(cmd=%d)", gnlh->cmd);
2212 break;
2213 }
2214
2215 return NL_SKIP;
2216}
2217
2218
97865538 2219static void wpa_driver_nl80211_event_receive(int sock, void *eloop_ctx,
5582a5d1 2220 void *handle)
97865538 2221{
a4ae123c 2222 struct nl_cb *cb = eloop_ctx;
97865538
JM
2223
2224 wpa_printf(MSG_DEBUG, "nl80211: Event message available");
2225
a4ae123c 2226 nl_recvmsgs(handle, cb);
97865538
JM
2227}
2228
2229
6d158490
LR
2230/**
2231 * wpa_driver_nl80211_set_country - ask nl80211 to set the regulatory domain
2232 * @priv: driver_nl80211 private data
2233 * @alpha2_arg: country to which to switch to
2234 * Returns: 0 on success, -1 on failure
2235 *
2236 * This asks nl80211 to set the regulatory domain for given
2237 * country ISO / IEC alpha2.
2238 */
2239static int wpa_driver_nl80211_set_country(void *priv, const char *alpha2_arg)
2240{
a2e40bb6
FF
2241 struct i802_bss *bss = priv;
2242 struct wpa_driver_nl80211_data *drv = bss->drv;
6d158490
LR
2243 char alpha2[3];
2244 struct nl_msg *msg;
2245
2246 msg = nlmsg_alloc();
2247 if (!msg)
e785c2ba 2248 return -ENOMEM;
6d158490
LR
2249
2250 alpha2[0] = alpha2_arg[0];
2251 alpha2[1] = alpha2_arg[1];
2252 alpha2[2] = '\0';
2253
9fb04070 2254 nl80211_cmd(drv, msg, 0, NL80211_CMD_REQ_SET_REG);
6d158490
LR
2255
2256 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, alpha2);
2257 if (send_and_recv_msgs(drv, msg, NULL, NULL))
2258 return -EINVAL;
2259 return 0;
2260nla_put_failure:
5883168a 2261 nlmsg_free(msg);
6d158490
LR
2262 return -EINVAL;
2263}
2264
2265
80bc75f1 2266struct wiphy_info_data {
e8b5e24e
JB
2267 struct wpa_driver_capa *capa;
2268
2269 unsigned int error:1;
61cbe2ff 2270 unsigned int device_ap_sme:1;
39718852 2271 unsigned int poll_command_supported:1;
32ab4855 2272 unsigned int data_tx_status:1;
536062f2 2273 unsigned int monitor_supported:1;
80bc75f1
JM
2274};
2275
2276
562c9d97
AN
2277static unsigned int probe_resp_offload_support(int supp_protocols)
2278{
2279 unsigned int prot = 0;
2280
2281 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS)
2282 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS;
2283 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2)
2284 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS2;
2285 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P)
2286 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_P2P;
2287 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_80211U)
2288 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_INTERWORKING;
2289
2290 return prot;
2291}
2292
2293
80bc75f1
JM
2294static int wiphy_info_handler(struct nl_msg *msg, void *arg)
2295{
2296 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2297 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2298 struct wiphy_info_data *info = arg;
9f51b113 2299 int p2p_go_supported = 0, p2p_client_supported = 0;
e8b5e24e
JB
2300 int p2p_concurrent = 0;
2301 int auth_supported = 0, connect_supported = 0;
2302 struct wpa_driver_capa *capa = info->capa;
7626850d
JB
2303 static struct nla_policy
2304 iface_combination_policy[NUM_NL80211_IFACE_COMB] = {
2305 [NL80211_IFACE_COMB_LIMITS] = { .type = NLA_NESTED },
2306 [NL80211_IFACE_COMB_MAXNUM] = { .type = NLA_U32 },
2307 [NL80211_IFACE_COMB_STA_AP_BI_MATCH] = { .type = NLA_FLAG },
2308 [NL80211_IFACE_COMB_NUM_CHANNELS] = { .type = NLA_U32 },
2309 },
2310 iface_limit_policy[NUM_NL80211_IFACE_LIMIT] = {
2311 [NL80211_IFACE_LIMIT_TYPES] = { .type = NLA_NESTED },
2312 [NL80211_IFACE_LIMIT_MAX] = { .type = NLA_U32 },
2313 };
80bc75f1
JM
2314
2315 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2316 genlmsg_attrlen(gnlh, 0), NULL);
2317
2318 if (tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS])
e8b5e24e 2319 capa->max_scan_ssids =
80bc75f1
JM
2320 nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS]);
2321
d21c63b9 2322 if (tb[NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS])
e8b5e24e 2323 capa->max_sched_scan_ssids =
d21c63b9
LC
2324 nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS]);
2325
bd525934 2326 if (tb[NL80211_ATTR_MAX_MATCH_SETS])
e8b5e24e 2327 capa->max_match_sets =
bd525934
LC
2328 nla_get_u8(tb[NL80211_ATTR_MAX_MATCH_SETS]);
2329
1581b38b
JM
2330 if (tb[NL80211_ATTR_SUPPORTED_IFTYPES]) {
2331 struct nlattr *nl_mode;
2332 int i;
2333 nla_for_each_nested(nl_mode,
2334 tb[NL80211_ATTR_SUPPORTED_IFTYPES], i) {
58708b3b 2335 switch (nla_type(nl_mode)) {
9f51b113 2336 case NL80211_IFTYPE_AP:
e8b5e24e 2337 capa->flags |= WPA_DRIVER_FLAGS_AP;
1581b38b 2338 break;
9f51b113
JB
2339 case NL80211_IFTYPE_P2P_GO:
2340 p2p_go_supported = 1;
2341 break;
2342 case NL80211_IFTYPE_P2P_CLIENT:
2343 p2p_client_supported = 1;
2344 break;
536062f2
JM
2345 case NL80211_IFTYPE_MONITOR:
2346 info->monitor_supported = 1;
2347 break;
1581b38b
JM
2348 }
2349 }
2350 }
2351
7626850d
JB
2352 if (tb[NL80211_ATTR_INTERFACE_COMBINATIONS]) {
2353 struct nlattr *nl_combi;
2354 int rem_combi;
2355
2356 nla_for_each_nested(nl_combi,
2357 tb[NL80211_ATTR_INTERFACE_COMBINATIONS],
2358 rem_combi) {
2359 struct nlattr *tb_comb[NUM_NL80211_IFACE_COMB];
2360 struct nlattr *tb_limit[NUM_NL80211_IFACE_LIMIT];
2361 struct nlattr *nl_limit, *nl_mode;
2362 int err, rem_limit, rem_mode;
2363 int combination_has_p2p = 0, combination_has_mgd = 0;
2364
2365 err = nla_parse_nested(tb_comb, MAX_NL80211_IFACE_COMB,
2366 nl_combi,
2367 iface_combination_policy);
2368 if (err || !tb_comb[NL80211_IFACE_COMB_LIMITS] ||
2369 !tb_comb[NL80211_IFACE_COMB_MAXNUM] ||
2370 !tb_comb[NL80211_IFACE_COMB_NUM_CHANNELS])
2371 goto broken_combination;
2372
2373 nla_for_each_nested(nl_limit,
2374 tb_comb[NL80211_IFACE_COMB_LIMITS],
2375 rem_limit) {
2376 err = nla_parse_nested(tb_limit,
2377 MAX_NL80211_IFACE_LIMIT,
2378 nl_limit,
2379 iface_limit_policy);
2380 if (err ||
2381 !tb_limit[NL80211_IFACE_LIMIT_TYPES])
2382 goto broken_combination;
2383
2384 nla_for_each_nested(
2385 nl_mode,
2386 tb_limit[NL80211_IFACE_LIMIT_TYPES],
2387 rem_mode) {
2388 int ift = nla_type(nl_mode);
2389 if (ift == NL80211_IFTYPE_P2P_GO ||
2390 ift == NL80211_IFTYPE_P2P_CLIENT)
2391 combination_has_p2p = 1;
2392 if (ift == NL80211_IFTYPE_STATION)
2393 combination_has_mgd = 1;
2394 }
2395 if (combination_has_p2p && combination_has_mgd)
2396 break;
2397 }
2398
2399 if (combination_has_p2p && combination_has_mgd) {
e8b5e24e 2400 p2p_concurrent = 1;
7626850d
JB
2401 break;
2402 }
2403
2404broken_combination:
2405 ;
2406 }
2407 }
2408
93d11400
ZY
2409 if (tb[NL80211_ATTR_SUPPORTED_COMMANDS]) {
2410 struct nlattr *nl_cmd;
2411 int i;
2412
2413 nla_for_each_nested(nl_cmd,
2414 tb[NL80211_ATTR_SUPPORTED_COMMANDS], i) {
e8b5e24e
JB
2415 switch (nla_get_u32(nl_cmd)) {
2416 case NL80211_CMD_AUTHENTICATE:
2417 auth_supported = 1;
2418 break;
2419 case NL80211_CMD_CONNECT:
2420 connect_supported = 1;
2421 break;
2422 case NL80211_CMD_START_SCHED_SCAN:
2423 capa->sched_scan_supported = 1;
2424 break;
39718852
JB
2425 case NL80211_CMD_PROBE_CLIENT:
2426 info->poll_command_supported = 1;
2427 break;
e8b5e24e 2428 }
93d11400
ZY
2429 }
2430 }
2431
e8b5e24e
JB
2432 if (tb[NL80211_ATTR_OFFCHANNEL_TX_OK]) {
2433 wpa_printf(MSG_DEBUG, "nl80211: Using driver-based "
2434 "off-channel TX");
2435 capa->flags |= WPA_DRIVER_FLAGS_OFFCHANNEL_TX;
2436 }
2437
2438 if (tb[NL80211_ATTR_ROAM_SUPPORT]) {
2439 wpa_printf(MSG_DEBUG, "nl80211: Using driver-based roaming");
2440 capa->flags |= WPA_DRIVER_FLAGS_BSS_SELECTION;
2441 }
5dfca53f 2442
e8b5e24e
JB
2443 /* default to 5000 since early versions of mac80211 don't set it */
2444 capa->max_remain_on_chan = 5000;
004ba773 2445
70619a5d
EP
2446 if (tb[NL80211_ATTR_SUPPORT_AP_UAPSD])
2447 capa->flags |= WPA_DRIVER_FLAGS_AP_UAPSD;
2448
89e07afb 2449 if (tb[NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION])
e8b5e24e 2450 capa->max_remain_on_chan =
89e07afb
JB
2451 nla_get_u32(tb[NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION]);
2452
e8b5e24e
JB
2453 if (auth_supported)
2454 capa->flags |= WPA_DRIVER_FLAGS_SME;
2455 else if (!connect_supported) {
2456 wpa_printf(MSG_INFO, "nl80211: Driver does not support "
2457 "authentication/association or connect commands");
2458 info->error = 1;
2459 }
2460
2461 if (p2p_go_supported && p2p_client_supported)
2462 capa->flags |= WPA_DRIVER_FLAGS_P2P_CAPABLE;
2463 if (p2p_concurrent) {
2464 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
2465 "interface (driver advertised support)");
2466 capa->flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
2467 capa->flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
2468 }
2469
03ea1786
AN
2470 if (tb[NL80211_ATTR_TDLS_SUPPORT]) {
2471 wpa_printf(MSG_DEBUG, "nl80211: TDLS supported");
2472 capa->flags |= WPA_DRIVER_FLAGS_TDLS_SUPPORT;
2473
2474 if (tb[NL80211_ATTR_TDLS_EXTERNAL_SETUP]) {
2475 wpa_printf(MSG_DEBUG, "nl80211: TDLS external setup");
2476 capa->flags |=
2477 WPA_DRIVER_FLAGS_TDLS_EXTERNAL_SETUP;
2478 }
2479 }
2480
61cbe2ff
JB
2481 if (tb[NL80211_ATTR_DEVICE_AP_SME])
2482 info->device_ap_sme = 1;
2483
32ab4855
JB
2484 if (tb[NL80211_ATTR_FEATURE_FLAGS]) {
2485 u32 flags = nla_get_u32(tb[NL80211_ATTR_FEATURE_FLAGS]);
2486
2487 if (flags & NL80211_FEATURE_SK_TX_STATUS)
2488 info->data_tx_status = 1;
2489 }
2490
562c9d97
AN
2491 if (tb[NL80211_ATTR_PROBE_RESP_OFFLOAD]) {
2492 int protocols =
2493 nla_get_u32(tb[NL80211_ATTR_PROBE_RESP_OFFLOAD]);
2494 wpa_printf(MSG_DEBUG, "nl80211: Supports Probe Response "
2495 "offload in AP mode");
2496 capa->flags |= WPA_DRIVER_FLAGS_PROBE_RESP_OFFLOAD;
2497 capa->probe_resp_offloads =
2498 probe_resp_offload_support(protocols);
2499 }
2500
80bc75f1
JM
2501 return NL_SKIP;
2502}
2503
2504
2505static int wpa_driver_nl80211_get_info(struct wpa_driver_nl80211_data *drv,
2506 struct wiphy_info_data *info)
2507{
2508 struct nl_msg *msg;
2509
2510 os_memset(info, 0, sizeof(*info));
e8b5e24e 2511 info->capa = &drv->capa;
89e07afb 2512
80bc75f1
JM
2513 msg = nlmsg_alloc();
2514 if (!msg)
2515 return -1;
2516
9fb04070 2517 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_WIPHY);
80bc75f1 2518
a2e40bb6 2519 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->first_bss.ifindex);
80bc75f1
JM
2520
2521 if (send_and_recv_msgs(drv, msg, wiphy_info_handler, info) == 0)
2522 return 0;
2523 msg = NULL;
2524nla_put_failure:
2525 nlmsg_free(msg);
2526 return -1;
2527}
2528
2529
93d11400 2530static int wpa_driver_nl80211_capa(struct wpa_driver_nl80211_data *drv)
80bc75f1
JM
2531{
2532 struct wiphy_info_data info;
2533 if (wpa_driver_nl80211_get_info(drv, &info))
93d11400 2534 return -1;
e8b5e24e
JB
2535
2536 if (info.error)
2537 return -1;
2538
80bc75f1 2539 drv->has_capability = 1;
1b2a72e8
JM
2540 /* For now, assume TKIP, CCMP, WPA, WPA2 are supported */
2541 drv->capa.key_mgmt = WPA_DRIVER_CAPA_KEY_MGMT_WPA |
2542 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
2543 WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
2544 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK;
2545 drv->capa.enc = WPA_DRIVER_CAPA_ENC_WEP40 |
2546 WPA_DRIVER_CAPA_ENC_WEP104 |
2547 WPA_DRIVER_CAPA_ENC_TKIP |
2548 WPA_DRIVER_CAPA_ENC_CCMP;
291b6068
JM
2549 drv->capa.auth = WPA_DRIVER_AUTH_OPEN |
2550 WPA_DRIVER_AUTH_SHARED |
2551 WPA_DRIVER_AUTH_LEAP;
1b2a72e8 2552
871f4dd0 2553 drv->capa.flags |= WPA_DRIVER_FLAGS_SANE_ERROR_CODES;
0194fedb 2554 drv->capa.flags |= WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE;
2fee890a 2555 drv->capa.flags |= WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
4dc03726 2556 drv->capa.flags |= WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS;
0194fedb 2557
61cbe2ff 2558 drv->device_ap_sme = info.device_ap_sme;
39718852 2559 drv->poll_command_supported = info.poll_command_supported;
32ab4855 2560 drv->data_tx_status = info.data_tx_status;
61cbe2ff 2561
a11241fa
JB
2562 /*
2563 * If poll command is supported mac80211 is new enough to
2564 * have everything we need to not need monitor interfaces.
2565 */
2566 drv->use_monitor = !info.poll_command_supported;
2567
536062f2
JM
2568 if (drv->device_ap_sme && drv->use_monitor) {
2569 /*
2570 * Non-mac80211 drivers may not support monitor interface.
2571 * Make sure we do not get stuck with incorrect capability here
2572 * by explicitly testing this.
2573 */
2574 if (!info.monitor_supported) {
2575 wpa_printf(MSG_DEBUG, "nl80211: Disable use_monitor "
2576 "with device_ap_sme since no monitor mode "
2577 "support detected");
2578 drv->use_monitor = 0;
2579 }
2580 }
2581
a11241fa
JB
2582 /*
2583 * If we aren't going to use monitor interfaces, but the
2584 * driver doesn't support data TX status, we won't get TX
2585 * status for EAPOL frames.
2586 */
2587 if (!drv->use_monitor && !info.data_tx_status)
2588 drv->capa.flags &= ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
2589
93d11400 2590 return 0;
80bc75f1
JM
2591}
2592
2593
b088cf82
JM
2594#ifdef ANDROID
2595static int android_genl_ctrl_resolve(struct nl_handle *handle,
2596 const char *name)
2597{
2598 /*
2599 * Android ICS has very minimal genl_ctrl_resolve() implementation, so
2600 * need to work around that.
2601 */
2602 struct nl_cache *cache = NULL;
2603 struct genl_family *nl80211 = NULL;
2604 int id = -1;
2605
2606 if (genl_ctrl_alloc_cache(handle, &cache) < 0) {
2607 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic "
2608 "netlink cache");
2609 goto fail;
2610 }
2611
2612 nl80211 = genl_ctrl_search_by_name(cache, name);
2613 if (nl80211 == NULL)
2614 goto fail;
2615
2616 id = genl_family_get_id(nl80211);
2617
2618fail:
2619 if (nl80211)
2620 genl_family_put(nl80211);
2621 if (cache)
2622 nl_cache_free(cache);
2623
2624 return id;
2625}
2626#define genl_ctrl_resolve android_genl_ctrl_resolve
2627#endif /* ANDROID */
2628
2629
2a7b66f5
BG
2630static int wpa_driver_nl80211_init_nl_global(struct nl80211_global *global)
2631{
d6c9aab8
JB
2632 int ret;
2633
2a7b66f5
BG
2634 global->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
2635 if (global->nl_cb == NULL) {
2636 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
2637 "callbacks");
2638 return -1;
2639 }
2640
481234cf
JM
2641 global->nl = nl_create_handle(global->nl_cb, "nl");
2642 if (global->nl == NULL)
d6c9aab8 2643 goto err;
276e2d67 2644
481234cf 2645 global->nl80211_id = genl_ctrl_resolve(global->nl, "nl80211");
335d42b1 2646 if (global->nl80211_id < 0) {
276e2d67
BG
2647 wpa_printf(MSG_ERROR, "nl80211: 'nl80211' generic netlink not "
2648 "found");
d6c9aab8 2649 goto err;
276e2d67
BG
2650 }
2651
481234cf
JM
2652 global->nl_event = nl_create_handle(global->nl_cb, "event");
2653 if (global->nl_event == NULL)
d6c9aab8 2654 goto err;
9fff9fdc 2655
d6c9aab8 2656 ret = nl_get_multicast_id(global, "nl80211", "scan");
97865538 2657 if (ret >= 0)
481234cf 2658 ret = nl_socket_add_membership(global->nl_event, ret);
97865538
JM
2659 if (ret < 0) {
2660 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
2661 "membership for scan events: %d (%s)",
2662 ret, strerror(-ret));
d6c9aab8 2663 goto err;
97865538 2664 }
c2a04078 2665
d6c9aab8 2666 ret = nl_get_multicast_id(global, "nl80211", "mlme");
c2a04078 2667 if (ret >= 0)
481234cf 2668 ret = nl_socket_add_membership(global->nl_event, ret);
c2a04078
JM
2669 if (ret < 0) {
2670 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
2671 "membership for mlme events: %d (%s)",
2672 ret, strerror(-ret));
d6c9aab8 2673 goto err;
c2a04078 2674 }
c2a04078 2675
d6c9aab8 2676 ret = nl_get_multicast_id(global, "nl80211", "regulatory");
33c5deb8 2677 if (ret >= 0)
481234cf 2678 ret = nl_socket_add_membership(global->nl_event, ret);
33c5deb8
JM
2679 if (ret < 0) {
2680 wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
2681 "membership for regulatory events: %d (%s)",
2682 ret, strerror(-ret));
2683 /* Continue without regulatory events */
2684 }
2685
d6c9aab8
JB
2686 nl_cb_set(global->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
2687 no_seq_check, NULL);
2688 nl_cb_set(global->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
2689 process_global_event, global);
2690
481234cf 2691 eloop_register_read_sock(nl_socket_get_fd(global->nl_event),
d6c9aab8 2692 wpa_driver_nl80211_event_receive,
481234cf 2693 global->nl_cb, global->nl_event);
d6c9aab8
JB
2694
2695 return 0;
2696
2697err:
2698 nl_destroy_handles(&global->nl_event);
2699 nl_destroy_handles(&global->nl);
2700 nl_cb_put(global->nl_cb);
671a5039 2701 global->nl_cb = NULL;
d6c9aab8
JB
2702 return -1;
2703}
2704
2705
2706static int wpa_driver_nl80211_init_nl(struct wpa_driver_nl80211_data *drv)
2707{
1afc986d
JB
2708 drv->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
2709 if (!drv->nl_cb) {
2710 wpa_printf(MSG_ERROR, "nl80211: Failed to alloc cb struct");
d6c9aab8 2711 return -1;
1afc986d
JB
2712 }
2713
2714 nl_cb_set(drv->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
2715 no_seq_check, NULL);
f06aedd9
JB
2716 nl_cb_set(drv->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
2717 process_drv_event, drv);
1afc986d 2718
9fff9fdc 2719 return 0;
9fff9fdc
JM
2720}
2721
2722
8401a6b0
JM
2723static void wpa_driver_nl80211_rfkill_blocked(void *ctx)
2724{
8401a6b0 2725 wpa_printf(MSG_DEBUG, "nl80211: RFKILL blocked");
a63063b4
JM
2726 /*
2727 * This may be for any interface; use ifdown event to disable
2728 * interface.
2729 */
8401a6b0
JM
2730}
2731
2732
2733static void wpa_driver_nl80211_rfkill_unblocked(void *ctx)
2734{
2735 struct wpa_driver_nl80211_data *drv = ctx;
2736 wpa_printf(MSG_DEBUG, "nl80211: RFKILL unblocked");
c81eff1a
BG
2737 if (linux_set_iface_flags(drv->global->ioctl_sock,
2738 drv->first_bss.ifname, 1)) {
8401a6b0
JM
2739 wpa_printf(MSG_DEBUG, "nl80211: Could not set interface UP "
2740 "after rfkill unblock");
2741 return;
2742 }
a63063b4 2743 /* rtnetlink ifup handler will report interface as enabled */
8401a6b0
JM
2744}
2745
2746
6859f1cb
BG
2747static void nl80211_get_phy_name(struct wpa_driver_nl80211_data *drv)
2748{
2749 /* Find phy (radio) to which this interface belongs */
2750 char buf[90], *pos;
2751 int f, rv;
2752
2753 drv->phyname[0] = '\0';
2754 snprintf(buf, sizeof(buf) - 1, "/sys/class/net/%s/phy80211/name",
2755 drv->first_bss.ifname);
2756 f = open(buf, O_RDONLY);
2757 if (f < 0) {
2758 wpa_printf(MSG_DEBUG, "Could not open file %s: %s",
2759 buf, strerror(errno));
2760 return;
2761 }
2762
2763 rv = read(f, drv->phyname, sizeof(drv->phyname) - 1);
2764 close(f);
2765 if (rv < 0) {
2766 wpa_printf(MSG_DEBUG, "Could not read file %s: %s",
2767 buf, strerror(errno));
2768 return;
2769 }
2770
2771 drv->phyname[rv] = '\0';
2772 pos = os_strchr(drv->phyname, '\n');
2773 if (pos)
2774 *pos = '\0';
2775 wpa_printf(MSG_DEBUG, "nl80211: interface %s in phy %s",
2776 drv->first_bss.ifname, drv->phyname);
2777}
2778
2779
32ab4855
JB
2780static void wpa_driver_nl80211_handle_eapol_tx_status(int sock,
2781 void *eloop_ctx,
2782 void *handle)
2783{
2784 struct wpa_driver_nl80211_data *drv = eloop_ctx;
2785 u8 data[2048];
2786 struct msghdr msg;
2787 struct iovec entry;
2788 struct {
2789 struct cmsghdr cm;
2790 char control[512];
2791 } control;
2792 struct cmsghdr *cmsg;
2793 int res, found_ee = 0, found_wifi = 0, acked = 0;
2794 union wpa_event_data event;
2795
2796 memset(&msg, 0, sizeof(msg));
2797 msg.msg_iov = &entry;
2798 msg.msg_iovlen = 1;
2799 entry.iov_base = data;
2800 entry.iov_len = sizeof(data);
2801 msg.msg_control = &control;
2802 msg.msg_controllen = sizeof(control);
2803
2804 res = recvmsg(sock, &msg, MSG_ERRQUEUE);
2805 /* if error or not fitting 802.3 header, return */
2806 if (res < 14)
2807 return;
2808
2809 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg))
2810 {
2811 if (cmsg->cmsg_level == SOL_SOCKET &&
2812 cmsg->cmsg_type == SCM_WIFI_STATUS) {
2813 int *ack;
2814
2815 found_wifi = 1;
2816 ack = (void *)CMSG_DATA(cmsg);
2817 acked = *ack;
2818 }
2819
2820 if (cmsg->cmsg_level == SOL_PACKET &&
2821 cmsg->cmsg_type == PACKET_TX_TIMESTAMP) {
2822 struct sock_extended_err *err =
2823 (struct sock_extended_err *)CMSG_DATA(cmsg);
2824
2825 if (err->ee_origin == SO_EE_ORIGIN_TXSTATUS)
2826 found_ee = 1;
2827 }
2828 }
2829
2830 if (!found_ee || !found_wifi)
2831 return;
2832
2833 memset(&event, 0, sizeof(event));
2834 event.eapol_tx_status.dst = data;
2835 event.eapol_tx_status.data = data + 14;
2836 event.eapol_tx_status.data_len = res - 14;
2837 event.eapol_tx_status.ack = acked;
2838 wpa_supplicant_event(drv->ctx, EVENT_EAPOL_TX_STATUS, &event);
2839}
2840
2841
cc7a48d1
JB
2842static int nl80211_init_bss(struct i802_bss *bss)
2843{
2844 bss->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
2845 if (!bss->nl_cb)
2846 return -1;
2847
2848 nl_cb_set(bss->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
2849 no_seq_check, NULL);
2850 nl_cb_set(bss->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
2851 process_bss_event, bss);
2852
2853 return 0;
2854}
2855
2856
2857static void nl80211_destroy_bss(struct i802_bss *bss)
2858{
2859 nl_cb_put(bss->nl_cb);
2860 bss->nl_cb = NULL;
2861}
2862
2863
9fff9fdc
JM
2864/**
2865 * wpa_driver_nl80211_init - Initialize nl80211 driver interface
2866 * @ctx: context to be used when calling wpa_supplicant functions,
2867 * e.g., wpa_supplicant_event()
2868 * @ifname: interface name, e.g., wlan0
f2ed8023 2869 * @global_priv: private driver global data from global_init()
9fff9fdc
JM
2870 * Returns: Pointer to private data, %NULL on failure
2871 */
f2ed8023
JM
2872static void * wpa_driver_nl80211_init(void *ctx, const char *ifname,
2873 void *global_priv)
9fff9fdc 2874{
9fff9fdc 2875 struct wpa_driver_nl80211_data *drv;
8401a6b0 2876 struct rfkill_config *rcfg;
a2e40bb6 2877 struct i802_bss *bss;
9fff9fdc 2878
a5c696ad
JM
2879 if (global_priv == NULL)
2880 return NULL;
9fff9fdc
JM
2881 drv = os_zalloc(sizeof(*drv));
2882 if (drv == NULL)
2883 return NULL;
f2ed8023 2884 drv->global = global_priv;
9fff9fdc 2885 drv->ctx = ctx;
a2e40bb6
FF
2886 bss = &drv->first_bss;
2887 bss->drv = drv;
2888 os_strlcpy(bss->ifname, ifname, sizeof(bss->ifname));
9fff9fdc
JM
2889 drv->monitor_ifidx = -1;
2890 drv->monitor_sock = -1;
d12dab4c 2891 drv->eapol_tx_sock = -1;
b1f625e0 2892 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
9fff9fdc 2893
ba2d0d7d 2894 if (wpa_driver_nl80211_init_nl(drv)) {
bbaf0837
JM
2895 os_free(drv);
2896 return NULL;
2897 }
9fff9fdc 2898
cc7a48d1
JB
2899 if (nl80211_init_bss(bss))
2900 goto failed;
2901
6859f1cb
BG
2902 nl80211_get_phy_name(drv);
2903
8401a6b0
JM
2904 rcfg = os_zalloc(sizeof(*rcfg));
2905 if (rcfg == NULL)
2906 goto failed;
2907 rcfg->ctx = drv;
2908 os_strlcpy(rcfg->ifname, ifname, sizeof(rcfg->ifname));
2909 rcfg->blocked_cb = wpa_driver_nl80211_rfkill_blocked;
2910 rcfg->unblocked_cb = wpa_driver_nl80211_rfkill_unblocked;
2911 drv->rfkill = rfkill_init(rcfg);
52169389 2912 if (drv->rfkill == NULL) {
8401a6b0 2913 wpa_printf(MSG_DEBUG, "nl80211: RFKILL status not available");
52169389
JM
2914 os_free(rcfg);
2915 }
8401a6b0 2916
08063178 2917 if (wpa_driver_nl80211_finish_drv_init(drv))
bbaf0837 2918 goto failed;
7524cfb1 2919
d12dab4c 2920 drv->eapol_tx_sock = socket(PF_PACKET, SOCK_DGRAM, 0);
32ab4855
JB
2921 if (drv->eapol_tx_sock < 0)
2922 goto failed;
2923
2924 if (drv->data_tx_status) {
2925 int enabled = 1;
2926
2927 if (setsockopt(drv->eapol_tx_sock, SOL_SOCKET, SO_WIFI_STATUS,
2928 &enabled, sizeof(enabled)) < 0) {
2929 wpa_printf(MSG_DEBUG,
2930 "nl80211: wifi status sockopt failed\n");
2931 drv->data_tx_status = 0;
a11241fa
JB
2932 if (!drv->use_monitor)
2933 drv->capa.flags &=
2934 ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
32ab4855
JB
2935 } else {
2936 eloop_register_read_sock(drv->eapol_tx_sock,
2937 wpa_driver_nl80211_handle_eapol_tx_status,
2938 drv, NULL);
2939 }
2940 }
f10bfc9a 2941
dac12351 2942 if (drv->global) {
c4bb8817 2943 dl_list_add(&drv->global->interfaces, &drv->list);
dac12351
BG
2944 drv->in_interface_list = 1;
2945 }
c4bb8817 2946
a2e40bb6 2947 return bss;
7524cfb1 2948
bbaf0837 2949failed:
dac12351 2950 wpa_driver_nl80211_deinit(bss);
7524cfb1
JM
2951 return NULL;
2952}
2953
2954
a11241fa 2955static int nl80211_register_frame(struct i802_bss *bss,
5582a5d1 2956 struct nl_handle *nl_handle,
bd94971e 2957 u16 type, const u8 *match, size_t match_len)
58f6fbe0 2958{
a11241fa 2959 struct wpa_driver_nl80211_data *drv = bss->drv;
58f6fbe0
JM
2960 struct nl_msg *msg;
2961 int ret = -1;
2962
2963 msg = nlmsg_alloc();
2964 if (!msg)
2965 return -1;
2966
36488c05
JM
2967 wpa_printf(MSG_DEBUG, "nl80211: Register frame type=0x%x nl_handle=%p",
2968 type, nl_handle);
2969 wpa_hexdump(MSG_DEBUG, "nl80211: Register frame match",
2970 match, match_len);
2971
9fb04070 2972 nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_ACTION);
58f6fbe0 2973
a11241fa 2974 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
bd94971e 2975 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE, type);
58f6fbe0
JM
2976 NLA_PUT(msg, NL80211_ATTR_FRAME_MATCH, match_len, match);
2977
d6c9aab8 2978 ret = send_and_recv(drv->global, nl_handle, msg, NULL, NULL);
58f6fbe0
JM
2979 msg = NULL;
2980 if (ret) {
5582a5d1
JB
2981 wpa_printf(MSG_DEBUG, "nl80211: Register frame command "
2982 "failed (type=%u): ret=%d (%s)",
2983 type, ret, strerror(-ret));
2984 wpa_hexdump(MSG_DEBUG, "nl80211: Register frame match",
58f6fbe0
JM
2985 match, match_len);
2986 goto nla_put_failure;
2987 }
2988 ret = 0;
2989nla_put_failure:
2990 nlmsg_free(msg);
2991 return ret;
2992}
2993
2994
a11241fa
JB
2995static int nl80211_alloc_mgmt_handle(struct i802_bss *bss)
2996{
2997 struct wpa_driver_nl80211_data *drv = bss->drv;
2998
481234cf 2999 if (bss->nl_mgmt) {
a11241fa 3000 wpa_printf(MSG_DEBUG, "nl80211: Mgmt reporting "
36488c05 3001 "already on! (nl_mgmt=%p)", bss->nl_mgmt);
a11241fa
JB
3002 return -1;
3003 }
3004
481234cf
JM
3005 bss->nl_mgmt = nl_create_handle(drv->nl_cb, "mgmt");
3006 if (bss->nl_mgmt == NULL)
a11241fa
JB
3007 return -1;
3008
481234cf 3009 eloop_register_read_sock(nl_socket_get_fd(bss->nl_mgmt),
a11241fa 3010 wpa_driver_nl80211_event_receive, bss->nl_cb,
481234cf 3011 bss->nl_mgmt);
a11241fa
JB
3012
3013 return 0;
3014}
3015
3016
3017static int nl80211_register_action_frame(struct i802_bss *bss,
bd94971e
JB
3018 const u8 *match, size_t match_len)
3019{
3020 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_ACTION << 4);
481234cf 3021 return nl80211_register_frame(bss, bss->nl_mgmt,
5582a5d1 3022 type, match, match_len);
bd94971e
JB
3023}
3024
3025
a11241fa 3026static int nl80211_mgmt_subscribe_non_ap(struct i802_bss *bss)
58f6fbe0 3027{
a11241fa
JB
3028 struct wpa_driver_nl80211_data *drv = bss->drv;
3029
3030 if (nl80211_alloc_mgmt_handle(bss))
3031 return -1;
36488c05
JM
3032 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with non-AP "
3033 "handle %p", bss->nl_mgmt);
a11241fa 3034
4fe9fa0d 3035#if defined(CONFIG_P2P) || defined(CONFIG_INTERWORKING)
046b26a2 3036 /* GAS Initial Request */
a11241fa 3037 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0a", 2) < 0)
046b26a2
JM
3038 return -1;
3039 /* GAS Initial Response */
a11241fa 3040 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0b", 2) < 0)
046b26a2 3041 return -1;
18708aad 3042 /* GAS Comeback Request */
a11241fa 3043 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0c", 2) < 0)
18708aad
JM
3044 return -1;
3045 /* GAS Comeback Response */
a11241fa 3046 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0d", 2) < 0)
18708aad 3047 return -1;
4fe9fa0d
JM
3048#endif /* CONFIG_P2P || CONFIG_INTERWORKING */
3049#ifdef CONFIG_P2P
046b26a2 3050 /* P2P Public Action */
a11241fa 3051 if (nl80211_register_action_frame(bss,
046b26a2
JM
3052 (u8 *) "\x04\x09\x50\x6f\x9a\x09",
3053 6) < 0)
3054 return -1;
3055 /* P2P Action */
a11241fa 3056 if (nl80211_register_action_frame(bss,
046b26a2
JM
3057 (u8 *) "\x7f\x50\x6f\x9a\x09",
3058 5) < 0)
3059 return -1;
3060#endif /* CONFIG_P2P */
7d878ca7
JM
3061#ifdef CONFIG_IEEE80211W
3062 /* SA Query Response */
a11241fa 3063 if (nl80211_register_action_frame(bss, (u8 *) "\x08\x01", 2) < 0)
7d878ca7
JM
3064 return -1;
3065#endif /* CONFIG_IEEE80211W */
35287637
AN
3066#ifdef CONFIG_TDLS
3067 if ((drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT)) {
3068 /* TDLS Discovery Response */
aa543c0c 3069 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0e", 2) <
35287637
AN
3070 0)
3071 return -1;
3072 }
3073#endif /* CONFIG_TDLS */
046b26a2 3074
7b90c16a 3075 /* FT Action frames */
a11241fa 3076 if (nl80211_register_action_frame(bss, (u8 *) "\x06", 1) < 0)
7b90c16a
JM
3077 return -1;
3078 else
3079 drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_FT |
3080 WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK;
3081
71269b37 3082 /* WNM - BSS Transition Management Request */
a11241fa
JB
3083 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x07", 2) < 0)
3084 return -1;
3085
3086 return 0;
3087}
3088
3089
02bb32c3
JB
3090static int nl80211_register_spurious_class3(struct i802_bss *bss)
3091{
3092 struct wpa_driver_nl80211_data *drv = bss->drv;
3093 struct nl_msg *msg;
3094 int ret = -1;
3095
3096 msg = nlmsg_alloc();
3097 if (!msg)
3098 return -1;
3099
3100 nl80211_cmd(drv, msg, 0, NL80211_CMD_UNEXPECTED_FRAME);
3101
3102 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
3103
481234cf 3104 ret = send_and_recv(drv->global, bss->nl_mgmt, msg, NULL, NULL);
02bb32c3
JB
3105 msg = NULL;
3106 if (ret) {
3107 wpa_printf(MSG_DEBUG, "nl80211: Register spurious class3 "
3108 "failed: ret=%d (%s)",
3109 ret, strerror(-ret));
3110 goto nla_put_failure;
3111 }
3112 ret = 0;
3113nla_put_failure:
3114 nlmsg_free(msg);
3115 return ret;
3116}
3117
3118
a11241fa
JB
3119static int nl80211_mgmt_subscribe_ap(struct i802_bss *bss)
3120{
3121 static const int stypes[] = {
3122 WLAN_FC_STYPE_AUTH,
3123 WLAN_FC_STYPE_ASSOC_REQ,
3124 WLAN_FC_STYPE_REASSOC_REQ,
3125 WLAN_FC_STYPE_DISASSOC,
3126 WLAN_FC_STYPE_DEAUTH,
3127 WLAN_FC_STYPE_ACTION,
3128 WLAN_FC_STYPE_PROBE_REQ,
3129/* Beacon doesn't work as mac80211 doesn't currently allow
3130 * it, but it wouldn't really be the right thing anyway as
3131 * it isn't per interface ... maybe just dump the scan
3132 * results periodically for OLBC?
3133 */
3134// WLAN_FC_STYPE_BEACON,
3135 };
3136 unsigned int i;
3137
3138 if (nl80211_alloc_mgmt_handle(bss))
71269b37 3139 return -1;
36488c05
JM
3140 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
3141 "handle %p", bss->nl_mgmt);
71269b37 3142
a11241fa 3143 for (i = 0; i < sizeof(stypes) / sizeof(stypes[0]); i++) {
481234cf 3144 if (nl80211_register_frame(bss, bss->nl_mgmt,
a11241fa
JB
3145 (WLAN_FC_TYPE_MGMT << 2) |
3146 (stypes[i] << 4),
3147 NULL, 0) < 0) {
3148 goto out_err;
3149 }
3150 }
3151
02bb32c3
JB
3152 if (nl80211_register_spurious_class3(bss))
3153 goto out_err;
3154
e32ad281
JB
3155 if (nl80211_get_wiphy_data_ap(bss) == NULL)
3156 goto out_err;
3157
58f6fbe0 3158 return 0;
a11241fa
JB
3159
3160out_err:
481234cf 3161 eloop_unregister_read_sock(nl_socket_get_fd(bss->nl_mgmt));
a11241fa
JB
3162 nl_destroy_handles(&bss->nl_mgmt);
3163 return -1;
3164}
3165
3166
a6cc0602
JM
3167static int nl80211_mgmt_subscribe_ap_dev_sme(struct i802_bss *bss)
3168{
3169 if (nl80211_alloc_mgmt_handle(bss))
3170 return -1;
3171 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
3172 "handle %p (device SME)", bss->nl_mgmt);
3173
3174 if (nl80211_register_frame(bss, bss->nl_mgmt,
3175 (WLAN_FC_TYPE_MGMT << 2) |
3176 (WLAN_FC_STYPE_ACTION << 4),
3177 NULL, 0) < 0)
3178 goto out_err;
3179
3180 return 0;
3181
3182out_err:
3183 eloop_unregister_read_sock(nl_socket_get_fd(bss->nl_mgmt));
3184 nl_destroy_handles(&bss->nl_mgmt);
3185 return -1;
3186}
3187
3188
36488c05 3189static void nl80211_mgmt_unsubscribe(struct i802_bss *bss, const char *reason)
a11241fa 3190{
481234cf 3191 if (bss->nl_mgmt == NULL)
a11241fa 3192 return;
36488c05
JM
3193 wpa_printf(MSG_DEBUG, "nl80211: Unsubscribe mgmt frames handle %p "
3194 "(%s)", bss->nl_mgmt, reason);
481234cf 3195 eloop_unregister_read_sock(nl_socket_get_fd(bss->nl_mgmt));
a11241fa 3196 nl_destroy_handles(&bss->nl_mgmt);
e32ad281
JB
3197
3198 nl80211_put_wiphy_data_ap(bss);
58f6fbe0
JM
3199}
3200
3201
8401a6b0
JM
3202static void wpa_driver_nl80211_send_rfkill(void *eloop_ctx, void *timeout_ctx)
3203{
3204 wpa_supplicant_event(timeout_ctx, EVENT_INTERFACE_DISABLED, NULL);
3205}
3206
3207
362f781e 3208static int
7524cfb1
JM
3209wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv)
3210{
a2e40bb6 3211 struct i802_bss *bss = &drv->first_bss;
8401a6b0 3212 int send_rfkill_event = 0;
a2e40bb6
FF
3213
3214 drv->ifindex = if_nametoindex(bss->ifname);
3215 drv->first_bss.ifindex = drv->ifindex;
a87c9d96 3216
bbaf0837 3217#ifndef HOSTAPD
ff6a158b
JM
3218 /*
3219 * Make sure the interface starts up in station mode unless this is a
3220 * dynamically added interface (e.g., P2P) that was already configured
3221 * with proper iftype.
3222 */
a5c696ad 3223 if (drv->ifindex != drv->global->if_add_ifindex &&
b1f625e0 3224 wpa_driver_nl80211_set_mode(bss, NL80211_IFTYPE_STATION) < 0) {
6e8183d7 3225 wpa_printf(MSG_ERROR, "nl80211: Could not configure driver to "
a87c9d96 3226 "use managed mode");
6e8183d7 3227 return -1;
a87c9d96
JM
3228 }
3229
c81eff1a 3230 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1)) {
8401a6b0
JM
3231 if (rfkill_is_blocked(drv->rfkill)) {
3232 wpa_printf(MSG_DEBUG, "nl80211: Could not yet enable "
3233 "interface '%s' due to rfkill",
3234 bss->ifname);
a63063b4 3235 drv->if_disabled = 1;
8401a6b0
JM
3236 send_rfkill_event = 1;
3237 } else {
3238 wpa_printf(MSG_ERROR, "nl80211: Could not set "
3239 "interface '%s' UP", bss->ifname);
3240 return -1;
3241 }
362f781e 3242 }
3f5285e8 3243
36d84860 3244 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
e2d02c29 3245 1, IF_OPER_DORMANT);
bbaf0837 3246#endif /* HOSTAPD */
362f781e 3247
2fee890a
JM
3248 if (wpa_driver_nl80211_capa(drv))
3249 return -1;
3250
c81eff1a 3251 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
341eebee 3252 bss->addr))
2136f480 3253 return -1;
f2ed8023 3254
8401a6b0
JM
3255 if (send_rfkill_event) {
3256 eloop_register_timeout(0, 0, wpa_driver_nl80211_send_rfkill,
3257 drv, drv->ctx);
3258 }
3259
362f781e 3260 return 0;
3f5285e8
JM
3261}
3262
3263
8a27af5c
JM
3264static int wpa_driver_nl80211_del_beacon(struct wpa_driver_nl80211_data *drv)
3265{
3266 struct nl_msg *msg;
3267
3268 msg = nlmsg_alloc();
3269 if (!msg)
3270 return -ENOMEM;
3271
9fb04070 3272 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_BEACON);
8a27af5c
JM
3273 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
3274
3275 return send_and_recv_msgs(drv, msg, NULL, NULL);
3276 nla_put_failure:
5883168a 3277 nlmsg_free(msg);
8a27af5c
JM
3278 return -ENOBUFS;
3279}
8a27af5c
JM
3280
3281
3f5285e8 3282/**
7e5ba1b9
JM
3283 * wpa_driver_nl80211_deinit - Deinitialize nl80211 driver interface
3284 * @priv: Pointer to private nl80211 data from wpa_driver_nl80211_init()
3f5285e8
JM
3285 *
3286 * Shut down driver interface and processing of driver events. Free
3287 * private data buffer if one was allocated in wpa_driver_nl80211_init().
3288 */
7e5ba1b9 3289static void wpa_driver_nl80211_deinit(void *priv)
3f5285e8 3290{
a2e40bb6
FF
3291 struct i802_bss *bss = priv;
3292 struct wpa_driver_nl80211_data *drv = bss->drv;
3f5285e8 3293
32ab4855
JB
3294 if (drv->data_tx_status)
3295 eloop_unregister_read_sock(drv->eapol_tx_sock);
d12dab4c
JB
3296 if (drv->eapol_tx_sock >= 0)
3297 close(drv->eapol_tx_sock);
f10bfc9a 3298
481234cf 3299 if (bss->nl_preq)
5582a5d1 3300 wpa_driver_nl80211_probe_req_report(bss, 0);
e17a2477 3301 if (bss->added_if_into_bridge) {
c81eff1a
BG
3302 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
3303 bss->ifname) < 0)
94627f6c
JM
3304 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
3305 "interface %s from bridge %s: %s",
e17a2477 3306 bss->ifname, bss->brname, strerror(errno));
94627f6c 3307 }
e17a2477 3308 if (bss->added_bridge) {
c81eff1a 3309 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
94627f6c
JM
3310 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
3311 "bridge %s: %s",
e17a2477 3312 bss->brname, strerror(errno));
94627f6c
JM
3313 }
3314
460456f8 3315 nl80211_remove_monitor_interface(drv);
8a27af5c 3316
b1f625e0 3317 if (is_ap_interface(drv->nlmode))
8a27af5c 3318 wpa_driver_nl80211_del_beacon(drv);
0915d02c 3319
bbaf0837
JM
3320#ifdef HOSTAPD
3321 if (drv->last_freq_ht) {
3322 /* Clear HT flags from the driver */
3323 struct hostapd_freq_params freq;
3324 os_memset(&freq, 0, sizeof(freq));
3325 freq.freq = drv->last_freq;
3326 i802_set_freq(priv, &freq);
3327 }
3328
bbaf0837
JM
3329 if (drv->eapol_sock >= 0) {
3330 eloop_unregister_read_sock(drv->eapol_sock);
3331 close(drv->eapol_sock);
3332 }
3333
3334 if (drv->if_indices != drv->default_if_indices)
3335 os_free(drv->if_indices);
1b648c7e 3336#endif /* HOSTAPD */
3f5285e8 3337
b3af99d2 3338 if (drv->disabled_11b_rates)
4e5cb1a3
JM
3339 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
3340
36d84860
BG
3341 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, 0,
3342 IF_OPER_UP);
8401a6b0 3343 rfkill_deinit(drv->rfkill);
3f5285e8 3344
bbaf0837
JM
3345 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
3346
c81eff1a 3347 (void) linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0);
b1f625e0 3348 wpa_driver_nl80211_set_mode(bss, NL80211_IFTYPE_STATION);
36488c05 3349 nl80211_mgmt_unsubscribe(bss, "deinit");
3f5285e8 3350
1afc986d 3351 nl_cb_put(drv->nl_cb);
3f5285e8 3352
cc7a48d1
JB
3353 nl80211_destroy_bss(&drv->first_bss);
3354
3812464c
JM
3355 os_free(drv->filter_ssids);
3356
536fd62d
JM
3357 os_free(drv->auth_ie);
3358
dac12351 3359 if (drv->in_interface_list)
f2ed8023
JM
3360 dl_list_del(&drv->list);
3361
3f5285e8
JM
3362 os_free(drv);
3363}
3364
3365
3366/**
3367 * wpa_driver_nl80211_scan_timeout - Scan timeout to report scan completion
ad1e68e6 3368 * @eloop_ctx: Driver private data
3f5285e8
JM
3369 * @timeout_ctx: ctx argument given to wpa_driver_nl80211_init()
3370 *
3371 * This function can be used as registered timeout when starting a scan to
3372 * generate a scan completed event if the driver does not report this.
3373 */
3374static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx, void *timeout_ctx)
3375{
ad1e68e6 3376 struct wpa_driver_nl80211_data *drv = eloop_ctx;
b1f625e0 3377 if (drv->ap_scan_as_station != NL80211_IFTYPE_UNSPECIFIED) {
a2e40bb6 3378 wpa_driver_nl80211_set_mode(&drv->first_bss,
b1f625e0
EP
3379 drv->ap_scan_as_station);
3380 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
ad1e68e6 3381 }
3f5285e8
JM
3382 wpa_printf(MSG_DEBUG, "Scan timeout - try to get results");
3383 wpa_supplicant_event(timeout_ctx, EVENT_SCAN_RESULTS, NULL);
3384}
3385
3386
3387/**
3388 * wpa_driver_nl80211_scan - Request the driver to initiate scan
ad1e68e6 3389 * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
6a1063e0 3390 * @params: Scan parameters
3f5285e8
JM
3391 * Returns: 0 on success, -1 on failure
3392 */
6a1063e0
JM
3393static int wpa_driver_nl80211_scan(void *priv,
3394 struct wpa_driver_scan_params *params)
3f5285e8 3395{
a2e40bb6
FF
3396 struct i802_bss *bss = priv;
3397 struct wpa_driver_nl80211_data *drv = bss->drv;
3f5285e8 3398 int ret = 0, timeout;
47185fc7 3399 struct nl_msg *msg, *ssids, *freqs, *rates;
6a1063e0 3400 size_t i;
3f5285e8 3401
536fd62d
JM
3402 drv->scan_for_auth = 0;
3403
0e75527f
JM
3404 msg = nlmsg_alloc();
3405 ssids = nlmsg_alloc();
d3a98225 3406 freqs = nlmsg_alloc();
47185fc7
RM
3407 rates = nlmsg_alloc();
3408 if (!msg || !ssids || !freqs || !rates) {
0e75527f
JM
3409 nlmsg_free(msg);
3410 nlmsg_free(ssids);
d3a98225 3411 nlmsg_free(freqs);
47185fc7 3412 nlmsg_free(rates);
3f5285e8
JM
3413 return -1;
3414 }
3415
3812464c
JM
3416 os_free(drv->filter_ssids);
3417 drv->filter_ssids = params->filter_ssids;
3418 params->filter_ssids = NULL;
3419 drv->num_filter_ssids = params->num_filter_ssids;
3420
9fb04070 3421 nl80211_cmd(drv, msg, 0, NL80211_CMD_TRIGGER_SCAN);
0e75527f
JM
3422
3423 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
3f5285e8 3424
6a1063e0 3425 for (i = 0; i < params->num_ssids; i++) {
9fad706c
JM
3426 wpa_hexdump_ascii(MSG_MSGDUMP, "nl80211: Scan SSID",
3427 params->ssids[i].ssid,
3428 params->ssids[i].ssid_len);
6a1063e0
JM
3429 NLA_PUT(ssids, i + 1, params->ssids[i].ssid_len,
3430 params->ssids[i].ssid);
3f5285e8 3431 }
6a1063e0
JM
3432 if (params->num_ssids)
3433 nla_put_nested(msg, NL80211_ATTR_SCAN_SSIDS, ssids);
3f5285e8 3434
d173df52 3435 if (params->extra_ies) {
3b1c7bfd
JM
3436 wpa_hexdump(MSG_MSGDUMP, "nl80211: Scan extra IEs",
3437 params->extra_ies, params->extra_ies_len);
d173df52
JM
3438 NLA_PUT(msg, NL80211_ATTR_IE, params->extra_ies_len,
3439 params->extra_ies);
3440 }
3441
d3a98225 3442 if (params->freqs) {
9fad706c
JM
3443 for (i = 0; params->freqs[i]; i++) {
3444 wpa_printf(MSG_MSGDUMP, "nl80211: Scan frequency %u "
3445 "MHz", params->freqs[i]);
d3a98225 3446 NLA_PUT_U32(freqs, i + 1, params->freqs[i]);
9fad706c 3447 }
d3a98225
JM
3448 nla_put_nested(msg, NL80211_ATTR_SCAN_FREQUENCIES, freqs);
3449 }
3450
47185fc7
RM
3451 if (params->p2p_probe) {
3452 /*
3453 * Remove 2.4 GHz rates 1, 2, 5.5, 11 Mbps from supported rates
3454 * by masking out everything else apart from the OFDM rates 6,
3455 * 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS rates. All 5 GHz
3456 * rates are left enabled.
3457 */
3458 NLA_PUT(rates, NL80211_BAND_2GHZ, 8,
3459 "\x0c\x12\x18\x24\x30\x48\x60\x6c");
3460 nla_put_nested(msg, NL80211_ATTR_SCAN_SUPP_RATES, rates);
970fa12e
RM
3461
3462 NLA_PUT_FLAG(msg, NL80211_ATTR_TX_NO_CCK_RATE);
47185fc7
RM
3463 }
3464
0e75527f
JM
3465 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3466 msg = NULL;
3467 if (ret) {
3468 wpa_printf(MSG_DEBUG, "nl80211: Scan trigger failed: ret=%d "
3469 "(%s)", ret, strerror(-ret));
ad1e68e6 3470#ifdef HOSTAPD
b1f625e0 3471 if (is_ap_interface(drv->nlmode)) {
ad1e68e6
JM
3472 /*
3473 * mac80211 does not allow scan requests in AP mode, so
3474 * try to do this in station mode.
3475 */
b1f625e0
EP
3476 if (wpa_driver_nl80211_set_mode(
3477 bss, NL80211_IFTYPE_STATION))
ad1e68e6
JM
3478 goto nla_put_failure;
3479
3480 if (wpa_driver_nl80211_scan(drv, params)) {
b1f625e0 3481 wpa_driver_nl80211_set_mode(bss, drv->nlmode);
ad1e68e6
JM
3482 goto nla_put_failure;
3483 }
3484
3485 /* Restore AP mode when processing scan results */
b1f625e0 3486 drv->ap_scan_as_station = drv->nlmode;
ad1e68e6
JM
3487 ret = 0;
3488 } else
3489 goto nla_put_failure;
3490#else /* HOSTAPD */
0e75527f 3491 goto nla_put_failure;
ad1e68e6 3492#endif /* HOSTAPD */
3f5285e8
JM
3493 }
3494
3495 /* Not all drivers generate "scan completed" wireless event, so try to
3496 * read results after a timeout. */
0e75527f 3497 timeout = 10;
3f5285e8
JM
3498 if (drv->scan_complete_events) {
3499 /*
d173df52
JM
3500 * The driver seems to deliver events to notify when scan is
3501 * complete, so use longer timeout to avoid race conditions
3502 * with scanning and following association request.
3f5285e8
JM
3503 */
3504 timeout = 30;
3505 }
3506 wpa_printf(MSG_DEBUG, "Scan requested (ret=%d) - scan timeout %d "
3507 "seconds", ret, timeout);
3508 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
0e75527f
JM
3509 eloop_register_timeout(timeout, 0, wpa_driver_nl80211_scan_timeout,
3510 drv, drv->ctx);
3f5285e8 3511
0e75527f
JM
3512nla_put_failure:
3513 nlmsg_free(ssids);
3514 nlmsg_free(msg);
d3a98225 3515 nlmsg_free(freqs);
47185fc7 3516 nlmsg_free(rates);
3f5285e8
JM
3517 return ret;
3518}
3519
3520
d21c63b9
LC
3521/**
3522 * wpa_driver_nl80211_sched_scan - Initiate a scheduled scan
3523 * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
3524 * @params: Scan parameters
3525 * @interval: Interval between scan cycles in milliseconds
3526 * Returns: 0 on success, -1 on failure or if not supported
3527 */
3528static int wpa_driver_nl80211_sched_scan(void *priv,
3529 struct wpa_driver_scan_params *params,
3530 u32 interval)
3531{
3532 struct i802_bss *bss = priv;
3533 struct wpa_driver_nl80211_data *drv = bss->drv;
3534 int ret = 0;
bd525934 3535 struct nl_msg *msg, *ssids, *freqs, *match_set_ssid, *match_sets;
d21c63b9
LC
3536 size_t i;
3537
216eede8
DS
3538#ifdef ANDROID
3539 if (!drv->capa.sched_scan_supported)
3540 return android_pno_start(bss, params);
3541#endif /* ANDROID */
3542
d21c63b9
LC
3543 msg = nlmsg_alloc();
3544 ssids = nlmsg_alloc();
3545 freqs = nlmsg_alloc();
3546 if (!msg || !ssids || !freqs) {
3547 nlmsg_free(msg);
3548 nlmsg_free(ssids);
3549 nlmsg_free(freqs);
3550 return -1;
3551 }
3552
3553 os_free(drv->filter_ssids);
3554 drv->filter_ssids = params->filter_ssids;
3555 params->filter_ssids = NULL;
3556 drv->num_filter_ssids = params->num_filter_ssids;
3557
9fb04070 3558 nl80211_cmd(drv, msg, 0, NL80211_CMD_START_SCHED_SCAN);
d21c63b9
LC
3559
3560 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
3561
3562 NLA_PUT_U32(msg, NL80211_ATTR_SCHED_SCAN_INTERVAL, interval);
3563
fb67eec6
JM
3564 if (drv->num_filter_ssids &&
3565 (int) drv->num_filter_ssids <= drv->capa.max_match_sets) {
bd525934
LC
3566 match_sets = nlmsg_alloc();
3567
3568 for (i = 0; i < drv->num_filter_ssids; i++) {
3569 wpa_hexdump_ascii(MSG_MSGDUMP,
3570 "nl80211: Sched scan filter SSID",
3571 drv->filter_ssids[i].ssid,
3572 drv->filter_ssids[i].ssid_len);
3573
3574 match_set_ssid = nlmsg_alloc();
3575 nla_put(match_set_ssid,
3576 NL80211_ATTR_SCHED_SCAN_MATCH_SSID,
3577 drv->filter_ssids[i].ssid_len,
3578 drv->filter_ssids[i].ssid);
3579
3580 nla_put_nested(match_sets, i + 1, match_set_ssid);
3581
3582 nlmsg_free(match_set_ssid);
3583 }
3584
3585 nla_put_nested(msg, NL80211_ATTR_SCHED_SCAN_MATCH,
3586 match_sets);
3587 nlmsg_free(match_sets);
3588 }
3589
d21c63b9
LC
3590 for (i = 0; i < params->num_ssids; i++) {
3591 wpa_hexdump_ascii(MSG_MSGDUMP, "nl80211: Sched scan SSID",
3592 params->ssids[i].ssid,
3593 params->ssids[i].ssid_len);
3594 NLA_PUT(ssids, i + 1, params->ssids[i].ssid_len,
3595 params->ssids[i].ssid);
3596 }
3597 if (params->num_ssids)
3598 nla_put_nested(msg, NL80211_ATTR_SCAN_SSIDS, ssids);
3599
3600 if (params->extra_ies) {
3601 wpa_hexdump_ascii(MSG_MSGDUMP, "nl80211: Sched scan extra IEs",
3602 params->extra_ies, params->extra_ies_len);
3603 NLA_PUT(msg, NL80211_ATTR_IE, params->extra_ies_len,
3604 params->extra_ies);
3605 }
3606
3607 if (params->freqs) {
3608 for (i = 0; params->freqs[i]; i++) {
3609 wpa_printf(MSG_MSGDUMP, "nl80211: Scan frequency %u "
3610 "MHz", params->freqs[i]);
3611 NLA_PUT_U32(freqs, i + 1, params->freqs[i]);
3612 }
3613 nla_put_nested(msg, NL80211_ATTR_SCAN_FREQUENCIES, freqs);
3614 }
3615
3616 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3617
3618 /* TODO: if we get an error here, we should fall back to normal scan */
3619
3620 msg = NULL;
3621 if (ret) {
3622 wpa_printf(MSG_DEBUG, "nl80211: Sched scan start failed: "
3623 "ret=%d (%s)", ret, strerror(-ret));
3624 goto nla_put_failure;
3625 }
3626
3627 wpa_printf(MSG_DEBUG, "nl80211: Sched scan requested (ret=%d) - "
3628 "scan interval %d msec", ret, interval);
3629
3630nla_put_failure:
3631 nlmsg_free(ssids);
3632 nlmsg_free(msg);
3633 nlmsg_free(freqs);
3634 return ret;
3635}
3636
3637
3638/**
3639 * wpa_driver_nl80211_stop_sched_scan - Stop a scheduled scan
3640 * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
3641 * Returns: 0 on success, -1 on failure or if not supported
3642 */
3643static int wpa_driver_nl80211_stop_sched_scan(void *priv)
3644{
3645 struct i802_bss *bss = priv;
3646 struct wpa_driver_nl80211_data *drv = bss->drv;
3647 int ret = 0;
3648 struct nl_msg *msg;
3649
216eede8
DS
3650#ifdef ANDROID
3651 if (!drv->capa.sched_scan_supported)
3652 return android_pno_stop(bss);
3653#endif /* ANDROID */
3654
d21c63b9
LC
3655 msg = nlmsg_alloc();
3656 if (!msg)
3657 return -1;
3658
9fb04070 3659 nl80211_cmd(drv, msg, 0, NL80211_CMD_STOP_SCHED_SCAN);
d21c63b9
LC
3660
3661 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
3662
3663 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3664 msg = NULL;
3665 if (ret) {
3666 wpa_printf(MSG_DEBUG, "nl80211: Sched scan stop failed: "
3667 "ret=%d (%s)", ret, strerror(-ret));
3668 goto nla_put_failure;
3669 }
3670
3671 wpa_printf(MSG_DEBUG, "nl80211: Sched scan stop sent (ret=%d)", ret);
3672
3673nla_put_failure:
3674 nlmsg_free(msg);
3675 return ret;
3676}
3677
3678
3812464c
JM
3679static const u8 * nl80211_get_ie(const u8 *ies, size_t ies_len, u8 ie)
3680{
3681 const u8 *end, *pos;
3682
3683 if (ies == NULL)
3684 return NULL;
3685
3686 pos = ies;
3687 end = ies + ies_len;
3688
3689 while (pos + 1 < end) {
3690 if (pos + 2 + pos[1] > end)
3691 break;
3692 if (pos[0] == ie)
3693 return pos;
3694 pos += 2 + pos[1];
3695 }
3696
3697 return NULL;
3698}
3699
3700
3701static int nl80211_scan_filtered(struct wpa_driver_nl80211_data *drv,
3702 const u8 *ie, size_t ie_len)
3703{
3704 const u8 *ssid;
3705 size_t i;
3706
3707 if (drv->filter_ssids == NULL)
3708 return 0;
3709
3710 ssid = nl80211_get_ie(ie, ie_len, WLAN_EID_SSID);
3711 if (ssid == NULL)
3712 return 1;
3713
3714 for (i = 0; i < drv->num_filter_ssids; i++) {
3715 if (ssid[1] == drv->filter_ssids[i].ssid_len &&
3716 os_memcmp(ssid + 2, drv->filter_ssids[i].ssid, ssid[1]) ==
3717 0)
3718 return 0;
3719 }
3720
3721 return 1;
3722}
3723
3724
b3db1e1c 3725static int bss_info_handler(struct nl_msg *msg, void *arg)
3f5285e8 3726{
b3db1e1c
JM
3727 struct nlattr *tb[NL80211_ATTR_MAX + 1];
3728 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3729 struct nlattr *bss[NL80211_BSS_MAX + 1];
3730 static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
3731 [NL80211_BSS_BSSID] = { .type = NLA_UNSPEC },
3732 [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
3733 [NL80211_BSS_TSF] = { .type = NLA_U64 },
3734 [NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 },
3735 [NL80211_BSS_CAPABILITY] = { .type = NLA_U16 },
3736 [NL80211_BSS_INFORMATION_ELEMENTS] = { .type = NLA_UNSPEC },
3737 [NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 },
3738 [NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 },
e6b8efeb 3739 [NL80211_BSS_STATUS] = { .type = NLA_U32 },
b3ad11bb 3740 [NL80211_BSS_SEEN_MS_AGO] = { .type = NLA_U32 },
8c090654 3741 [NL80211_BSS_BEACON_IES] = { .type = NLA_UNSPEC },
b3db1e1c 3742 };
3812464c
JM
3743 struct nl80211_bss_info_arg *_arg = arg;
3744 struct wpa_scan_results *res = _arg->res;
3f5285e8
JM
3745 struct wpa_scan_res **tmp;
3746 struct wpa_scan_res *r;
8c090654
JM
3747 const u8 *ie, *beacon_ie;
3748 size_t ie_len, beacon_ie_len;
3749 u8 *pos;
46957a9b 3750 size_t i;
3f5285e8 3751
b3db1e1c
JM
3752 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3753 genlmsg_attrlen(gnlh, 0), NULL);
3754 if (!tb[NL80211_ATTR_BSS])
3755 return NL_SKIP;
3756 if (nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS],
3757 bss_policy))
3758 return NL_SKIP;
f5a8d422
JM
3759 if (bss[NL80211_BSS_STATUS]) {
3760 enum nl80211_bss_status status;
3761 status = nla_get_u32(bss[NL80211_BSS_STATUS]);
3762 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
3763 bss[NL80211_BSS_FREQUENCY]) {
3764 _arg->assoc_freq =
3765 nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
3766 wpa_printf(MSG_DEBUG, "nl80211: Associated on %u MHz",
3767 _arg->assoc_freq);
3768 }
20f5a4c2
JM
3769 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
3770 bss[NL80211_BSS_BSSID]) {
3771 os_memcpy(_arg->assoc_bssid,
3772 nla_data(bss[NL80211_BSS_BSSID]), ETH_ALEN);
3773 wpa_printf(MSG_DEBUG, "nl80211: Associated with "
3774 MACSTR, MAC2STR(_arg->assoc_bssid));
3775 }
f5a8d422
JM
3776 }
3777 if (!res)
3778 return NL_SKIP;
b3db1e1c
JM
3779 if (bss[NL80211_BSS_INFORMATION_ELEMENTS]) {
3780 ie = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
3781 ie_len = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
3782 } else {
3783 ie = NULL;
3784 ie_len = 0;
3785 }
8c090654
JM
3786 if (bss[NL80211_BSS_BEACON_IES]) {
3787 beacon_ie = nla_data(bss[NL80211_BSS_BEACON_IES]);
3788 beacon_ie_len = nla_len(bss[NL80211_BSS_BEACON_IES]);
3789 } else {
3790 beacon_ie = NULL;
3791 beacon_ie_len = 0;
3792 }
3f5285e8 3793
3812464c
JM
3794 if (nl80211_scan_filtered(_arg->drv, ie ? ie : beacon_ie,
3795 ie ? ie_len : beacon_ie_len))
3796 return NL_SKIP;
3797
8c090654 3798 r = os_zalloc(sizeof(*r) + ie_len + beacon_ie_len);
3f5285e8 3799 if (r == NULL)
b3db1e1c
JM
3800 return NL_SKIP;
3801 if (bss[NL80211_BSS_BSSID])
3802 os_memcpy(r->bssid, nla_data(bss[NL80211_BSS_BSSID]),
3803 ETH_ALEN);
3804 if (bss[NL80211_BSS_FREQUENCY])
3805 r->freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
3806 if (bss[NL80211_BSS_BEACON_INTERVAL])
3807 r->beacon_int = nla_get_u16(bss[NL80211_BSS_BEACON_INTERVAL]);
3808 if (bss[NL80211_BSS_CAPABILITY])
3809 r->caps = nla_get_u16(bss[NL80211_BSS_CAPABILITY]);
7c2849d2
JM
3810 r->flags |= WPA_SCAN_NOISE_INVALID;
3811 if (bss[NL80211_BSS_SIGNAL_MBM]) {
b3db1e1c 3812 r->level = nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]);
7c2849d2
JM
3813 r->level /= 100; /* mBm to dBm */
3814 r->flags |= WPA_SCAN_LEVEL_DBM | WPA_SCAN_QUAL_INVALID;
3815 } else if (bss[NL80211_BSS_SIGNAL_UNSPEC]) {
3816 r->level = nla_get_u8(bss[NL80211_BSS_SIGNAL_UNSPEC]);
98ac6763 3817 r->flags |= WPA_SCAN_QUAL_INVALID;
7c2849d2
JM
3818 } else
3819 r->flags |= WPA_SCAN_LEVEL_INVALID | WPA_SCAN_QUAL_INVALID;
b3db1e1c
JM
3820 if (bss[NL80211_BSS_TSF])
3821 r->tsf = nla_get_u64(bss[NL80211_BSS_TSF]);
b3ad11bb
JM
3822 if (bss[NL80211_BSS_SEEN_MS_AGO])
3823 r->age = nla_get_u32(bss[NL80211_BSS_SEEN_MS_AGO]);
b3db1e1c 3824 r->ie_len = ie_len;
8c090654
JM
3825 pos = (u8 *) (r + 1);
3826 if (ie) {
3827 os_memcpy(pos, ie, ie_len);
3828 pos += ie_len;
3829 }
3830 r->beacon_ie_len = beacon_ie_len;
3831 if (beacon_ie)
3832 os_memcpy(pos, beacon_ie, beacon_ie_len);
3f5285e8 3833
e6b8efeb
JM
3834 if (bss[NL80211_BSS_STATUS]) {
3835 enum nl80211_bss_status status;
3836 status = nla_get_u32(bss[NL80211_BSS_STATUS]);
3837 switch (status) {
3838 case NL80211_BSS_STATUS_AUTHENTICATED:
3839 r->flags |= WPA_SCAN_AUTHENTICATED;
3840 break;
3841 case NL80211_BSS_STATUS_ASSOCIATED:
3842 r->flags |= WPA_SCAN_ASSOCIATED;
3843 break;
3844 default:
3845 break;
3846 }
3847 }
3848
46957a9b
JM
3849 /*
3850 * cfg80211 maintains separate BSS table entries for APs if the same
3851 * BSSID,SSID pair is seen on multiple channels. wpa_supplicant does
3852 * not use frequency as a separate key in the BSS table, so filter out
3853 * duplicated entries. Prefer associated BSS entry in such a case in
3854 * order to get the correct frequency into the BSS table.
3855 */
3856 for (i = 0; i < res->num; i++) {
3857 const u8 *s1, *s2;
3858 if (os_memcmp(res->res[i]->bssid, r->bssid, ETH_ALEN) != 0)
3859 continue;
3860
3861 s1 = nl80211_get_ie((u8 *) (res->res[i] + 1),
3862 res->res[i]->ie_len, WLAN_EID_SSID);
3863 s2 = nl80211_get_ie((u8 *) (r + 1), r->ie_len, WLAN_EID_SSID);
3864 if (s1 == NULL || s2 == NULL || s1[1] != s2[1] ||
3865 os_memcmp(s1, s2, 2 + s1[1]) != 0)
3866 continue;
3867
3868 /* Same BSSID,SSID was already included in scan results */
3869 wpa_printf(MSG_DEBUG, "nl80211: Remove duplicated scan result "
3870 "for " MACSTR, MAC2STR(r->bssid));
3871
3872 if ((r->flags & WPA_SCAN_ASSOCIATED) &&
3873 !(res->res[i]->flags & WPA_SCAN_ASSOCIATED)) {
3874 os_free(res->res[i]);
3875 res->res[i] = r;
3876 } else
3877 os_free(r);
3878 return NL_SKIP;
3879 }
3880
3f5285e8
JM
3881 tmp = os_realloc(res->res,
3882 (res->num + 1) * sizeof(struct wpa_scan_res *));
3883 if (tmp == NULL) {
3884 os_free(r);
b3db1e1c 3885 return NL_SKIP;
3f5285e8
JM
3886 }
3887 tmp[res->num++] = r;
3888 res->res = tmp;
b3db1e1c
JM
3889
3890 return NL_SKIP;
3f5285e8 3891}
b3db1e1c 3892
3f5285e8 3893
d72aad94
JM
3894static void clear_state_mismatch(struct wpa_driver_nl80211_data *drv,
3895 const u8 *addr)
3896{
3897 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
3898 wpa_printf(MSG_DEBUG, "nl80211: Clear possible state "
582507be 3899 "mismatch (" MACSTR ")", MAC2STR(addr));
d72aad94
JM
3900 wpa_driver_nl80211_mlme(drv, addr,
3901 NL80211_CMD_DEAUTHENTICATE,
77339912 3902 WLAN_REASON_PREV_AUTH_NOT_VALID, 1);
d72aad94
JM
3903 }
3904}
3905
3906
e6b8efeb
JM
3907static void wpa_driver_nl80211_check_bss_status(
3908 struct wpa_driver_nl80211_data *drv, struct wpa_scan_results *res)
3909{
3910 size_t i;
3911
3912 for (i = 0; i < res->num; i++) {
3913 struct wpa_scan_res *r = res->res[i];
3914 if (r->flags & WPA_SCAN_AUTHENTICATED) {
3915 wpa_printf(MSG_DEBUG, "nl80211: Scan results "
3916 "indicates BSS status with " MACSTR
3917 " as authenticated",
3918 MAC2STR(r->bssid));
b1f625e0 3919 if (is_sta_interface(drv->nlmode) &&
e6b8efeb
JM
3920 os_memcmp(r->bssid, drv->bssid, ETH_ALEN) != 0 &&
3921 os_memcmp(r->bssid, drv->auth_bssid, ETH_ALEN) !=
3922 0) {
3923 wpa_printf(MSG_DEBUG, "nl80211: Unknown BSSID"
3924 " in local state (auth=" MACSTR
3925 " assoc=" MACSTR ")",
3926 MAC2STR(drv->auth_bssid),
3927 MAC2STR(drv->bssid));
582507be 3928 clear_state_mismatch(drv, r->bssid);
e6b8efeb
JM
3929 }
3930 }
3931
3932 if (r->flags & WPA_SCAN_ASSOCIATED) {
3933 wpa_printf(MSG_DEBUG, "nl80211: Scan results "
3934 "indicate BSS status with " MACSTR
3935 " as associated",
3936 MAC2STR(r->bssid));
b1f625e0 3937 if (is_sta_interface(drv->nlmode) &&
e6b8efeb
JM
3938 !drv->associated) {
3939 wpa_printf(MSG_DEBUG, "nl80211: Local state "
3940 "(not associated) does not match "
3941 "with BSS state");
d72aad94 3942 clear_state_mismatch(drv, r->bssid);
b1f625e0 3943 } else if (is_sta_interface(drv->nlmode) &&
e6b8efeb
JM
3944 os_memcmp(drv->bssid, r->bssid, ETH_ALEN) !=
3945 0) {
3946 wpa_printf(MSG_DEBUG, "nl80211: Local state "
3947 "(associated with " MACSTR ") does "
3948 "not match with BSS state",
d72aad94
JM
3949 MAC2STR(drv->bssid));
3950 clear_state_mismatch(drv, r->bssid);
3951 clear_state_mismatch(drv, drv->bssid);
e6b8efeb
JM
3952 }
3953 }
3954 }
3955}
3956
3957
7e5ba1b9 3958static struct wpa_scan_results *
8856462d 3959nl80211_get_scan_results(struct wpa_driver_nl80211_data *drv)
3f5285e8 3960{
b3db1e1c 3961 struct nl_msg *msg;
3f5285e8 3962 struct wpa_scan_results *res;
b3db1e1c 3963 int ret;
3812464c 3964 struct nl80211_bss_info_arg arg;
3f5285e8
JM
3965
3966 res = os_zalloc(sizeof(*res));
b3db1e1c 3967 if (res == NULL)
8e2c104f 3968 return NULL;
b3db1e1c
JM
3969 msg = nlmsg_alloc();
3970 if (!msg)
3971 goto nla_put_failure;
3f5285e8 3972
9fb04070 3973 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
b3db1e1c 3974 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
3f5285e8 3975
3812464c
JM
3976 arg.drv = drv;
3977 arg.res = res;
3978 ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
b3db1e1c
JM
3979 msg = NULL;
3980 if (ret == 0) {
577db0ae
GM
3981 wpa_printf(MSG_DEBUG, "nl80211: Received scan results (%lu "
3982 "BSSes)", (unsigned long) res->num);
3983 nl80211_get_noise_for_scan_results(drv, res);
b3db1e1c 3984 return res;
3f5285e8 3985 }
b3db1e1c
JM
3986 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
3987 "(%s)", ret, strerror(-ret));
3988nla_put_failure:
3989 nlmsg_free(msg);
3990 wpa_scan_results_free(res);
3991 return NULL;
3f5285e8
JM
3992}
3993
3994
8856462d
JM
3995/**
3996 * wpa_driver_nl80211_get_scan_results - Fetch the latest scan results
3997 * @priv: Pointer to private wext data from wpa_driver_nl80211_init()
3998 * Returns: Scan results on success, -1 on failure
3999 */
4000static struct wpa_scan_results *
4001wpa_driver_nl80211_get_scan_results(void *priv)
4002{
a2e40bb6
FF
4003 struct i802_bss *bss = priv;
4004 struct wpa_driver_nl80211_data *drv = bss->drv;
8856462d
JM
4005 struct wpa_scan_results *res;
4006
4007 res = nl80211_get_scan_results(drv);
4008 if (res)
4009 wpa_driver_nl80211_check_bss_status(drv, res);
4010 return res;
4011}
4012
4013
4014static void nl80211_dump_scan(struct wpa_driver_nl80211_data *drv)
4015{
4016 struct wpa_scan_results *res;
4017 size_t i;
4018
4019 res = nl80211_get_scan_results(drv);
4020 if (res == NULL) {
4021 wpa_printf(MSG_DEBUG, "nl80211: Failed to get scan results");
4022 return;
4023 }
4024
4025 wpa_printf(MSG_DEBUG, "nl80211: Scan result dump");
4026 for (i = 0; i < res->num; i++) {
4027 struct wpa_scan_res *r = res->res[i];
4028 wpa_printf(MSG_DEBUG, "nl80211: %d/%d " MACSTR "%s%s",
4029 (int) i, (int) res->num, MAC2STR(r->bssid),
4030 r->flags & WPA_SCAN_AUTHENTICATED ? " [auth]" : "",
4031 r->flags & WPA_SCAN_ASSOCIATED ? " [assoc]" : "");
4032 }
4033
4034 wpa_scan_results_free(res);
4035}
4036
4037
642187d6 4038static int wpa_driver_nl80211_set_key(const char *ifname, void *priv,
71934751
JM
4039 enum wpa_alg alg, const u8 *addr,
4040 int key_idx, int set_tx,
642187d6
JM
4041 const u8 *seq, size_t seq_len,
4042 const u8 *key, size_t key_len)
3f5285e8 4043{
a2e40bb6
FF
4044 struct i802_bss *bss = priv;
4045 struct wpa_driver_nl80211_data *drv = bss->drv;
642187d6 4046 int ifindex = if_nametoindex(ifname);
3f5285e8 4047 struct nl_msg *msg;
1ad1cdc2 4048 int ret;
3f5285e8 4049
1ad1cdc2
JM
4050 wpa_printf(MSG_DEBUG, "%s: ifindex=%d alg=%d addr=%p key_idx=%d "
4051 "set_tx=%d seq_len=%lu key_len=%lu",
4052 __func__, ifindex, alg, addr, key_idx, set_tx,
3f5285e8 4053 (unsigned long) seq_len, (unsigned long) key_len);
8c66e185
JM
4054#ifdef CONFIG_TDLS
4055 if (key_idx == -1)
4056 key_idx = 0;
4057#endif /* CONFIG_TDLS */
3f5285e8
JM
4058
4059 msg = nlmsg_alloc();
1ad1cdc2
JM
4060 if (!msg)
4061 return -ENOMEM;
3f5285e8
JM
4062
4063 if (alg == WPA_ALG_NONE) {
9fb04070 4064 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_KEY);
3f5285e8 4065 } else {
9fb04070 4066 nl80211_cmd(drv, msg, 0, NL80211_CMD_NEW_KEY);
3f5285e8 4067 NLA_PUT(msg, NL80211_ATTR_KEY_DATA, key_len, key);
d723bab4
JM
4068 switch (alg) {
4069 case WPA_ALG_WEP:
4070 if (key_len == 5)
4071 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
4072 WLAN_CIPHER_SUITE_WEP40);
4073 else
4074 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
4075 WLAN_CIPHER_SUITE_WEP104);
4076 break;
4077 case WPA_ALG_TKIP:
4078 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
4079 WLAN_CIPHER_SUITE_TKIP);
4080 break;
4081 case WPA_ALG_CCMP:
4082 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
4083 WLAN_CIPHER_SUITE_CCMP);
4084 break;
4085 case WPA_ALG_IGTK:
4086 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
4087 WLAN_CIPHER_SUITE_AES_CMAC);
4088 break;
4089 default:
4090 wpa_printf(MSG_ERROR, "%s: Unsupported encryption "
4091 "algorithm %d", __func__, alg);
3f5285e8
JM
4092 nlmsg_free(msg);
4093 return -1;
4094 }
4095 }
4096
849ef835 4097 if (seq && seq_len)
1ad1cdc2
JM
4098 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, seq_len, seq);
4099
0382097e 4100 if (addr && !is_broadcast_ether_addr(addr)) {
3f5285e8
JM
4101 wpa_printf(MSG_DEBUG, " addr=" MACSTR, MAC2STR(addr));
4102 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
89c38e32
JM
4103
4104 if (alg != WPA_ALG_WEP && key_idx && !set_tx) {
4105 wpa_printf(MSG_DEBUG, " RSN IBSS RX GTK");
4106 NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE,
4107 NL80211_KEYTYPE_GROUP);
4108 }
60ea8187
JM
4109 } else if (addr && is_broadcast_ether_addr(addr)) {
4110 struct nl_msg *types;
4111 int err;
4112 wpa_printf(MSG_DEBUG, " broadcast key");
4113 types = nlmsg_alloc();
4114 if (!types)
4115 goto nla_put_failure;
4116 NLA_PUT_FLAG(types, NL80211_KEY_DEFAULT_TYPE_MULTICAST);
4117 err = nla_put_nested(msg, NL80211_ATTR_KEY_DEFAULT_TYPES,
4118 types);
4119 nlmsg_free(types);
4120 if (err)
4121 goto nla_put_failure;
3f5285e8
JM
4122 }
4123 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
1ad1cdc2 4124 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
3f5285e8 4125
1ad1cdc2 4126 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
15664ad0 4127 if ((ret == -ENOENT || ret == -ENOLINK) && alg == WPA_ALG_NONE)
1ad1cdc2
JM
4128 ret = 0;
4129 if (ret)
4130 wpa_printf(MSG_DEBUG, "nl80211: set_key failed; err=%d %s)",
4131 ret, strerror(-ret));
3f5285e8 4132
1ad1cdc2
JM
4133 /*
4134 * If we failed or don't need to set the default TX key (below),
4135 * we're done here.
4136 */
4137 if (ret || !set_tx || alg == WPA_ALG_NONE)
4138 return ret;
b1f625e0 4139 if (is_ap_interface(drv->nlmode) && addr &&
0382097e 4140 !is_broadcast_ether_addr(addr))
de12717a 4141 return ret;
3f5285e8 4142
1ad1cdc2
JM
4143 msg = nlmsg_alloc();
4144 if (!msg)
4145 return -ENOMEM;
3f5285e8 4146
9fb04070 4147 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_KEY);
1ad1cdc2
JM
4148 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
4149 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
4150 if (alg == WPA_ALG_IGTK)
4151 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT_MGMT);
4152 else
4153 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT);
60ea8187
JM
4154 if (addr && is_broadcast_ether_addr(addr)) {
4155 struct nl_msg *types;
4156 int err;
4157 types = nlmsg_alloc();
4158 if (!types)
4159 goto nla_put_failure;
4160 NLA_PUT_FLAG(types, NL80211_KEY_DEFAULT_TYPE_MULTICAST);
4161 err = nla_put_nested(msg, NL80211_ATTR_KEY_DEFAULT_TYPES,
4162 types);
4163 nlmsg_free(types);
4164 if (err)
4165 goto nla_put_failure;
4166 } else if (addr) {
4167 struct nl_msg *types;
4168 int err;
4169 types = nlmsg_alloc();
4170 if (!types)
4171 goto nla_put_failure;
4172 NLA_PUT_FLAG(types, NL80211_KEY_DEFAULT_TYPE_UNICAST);
4173 err = nla_put_nested(msg, NL80211_ATTR_KEY_DEFAULT_TYPES,
4174 types);
4175 nlmsg_free(types);
4176 if (err)
4177 goto nla_put_failure;
4178 }
3f5285e8 4179
1ad1cdc2
JM
4180 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4181 if (ret == -ENOENT)
4182 ret = 0;
4183 if (ret)
4184 wpa_printf(MSG_DEBUG, "nl80211: set_key default failed; "
4185 "err=%d %s)", ret, strerror(-ret));
4186 return ret;
3f5285e8
JM
4187
4188nla_put_failure:
5883168a 4189 nlmsg_free(msg);
6241fcb1 4190 return -ENOBUFS;
3f5285e8
JM
4191}
4192
4193
71934751 4194static int nl_add_key(struct nl_msg *msg, enum wpa_alg alg,
0194fedb
JB
4195 int key_idx, int defkey,
4196 const u8 *seq, size_t seq_len,
4197 const u8 *key, size_t key_len)
4198{
4199 struct nlattr *key_attr = nla_nest_start(msg, NL80211_ATTR_KEY);
4200 if (!key_attr)
4201 return -1;
4202
4203 if (defkey && alg == WPA_ALG_IGTK)
4204 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_MGMT);
4205 else if (defkey)
4206 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
4207
4208 NLA_PUT_U8(msg, NL80211_KEY_IDX, key_idx);
4209
d723bab4
JM
4210 switch (alg) {
4211 case WPA_ALG_WEP:
4212 if (key_len == 5)
2aa5f847
JM
4213 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
4214 WLAN_CIPHER_SUITE_WEP40);
d723bab4 4215 else
2aa5f847
JM
4216 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
4217 WLAN_CIPHER_SUITE_WEP104);
d723bab4
JM
4218 break;
4219 case WPA_ALG_TKIP:
2aa5f847 4220 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, WLAN_CIPHER_SUITE_TKIP);
d723bab4
JM
4221 break;
4222 case WPA_ALG_CCMP:
2aa5f847 4223 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, WLAN_CIPHER_SUITE_CCMP);
d723bab4
JM
4224 break;
4225 case WPA_ALG_IGTK:
2aa5f847
JM
4226 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
4227 WLAN_CIPHER_SUITE_AES_CMAC);
d723bab4
JM
4228 break;
4229 default:
4230 wpa_printf(MSG_ERROR, "%s: Unsupported encryption "
4231 "algorithm %d", __func__, alg);
0194fedb 4232 return -1;
d723bab4 4233 }
0194fedb
JB
4234
4235 if (seq && seq_len)
4236 NLA_PUT(msg, NL80211_KEY_SEQ, seq_len, seq);
4237
4238 NLA_PUT(msg, NL80211_KEY_DATA, key_len, key);
4239
4240 nla_nest_end(msg, key_attr);
4241
4242 return 0;
4243 nla_put_failure:
4244 return -1;
4245}
4246
c811d5bc 4247
cfaab580
ZY
4248static int nl80211_set_conn_keys(struct wpa_driver_associate_params *params,
4249 struct nl_msg *msg)
4250{
4251 int i, privacy = 0;
4252 struct nlattr *nl_keys, *nl_key;
4253
4254 for (i = 0; i < 4; i++) {
4255 if (!params->wep_key[i])
4256 continue;
4257 privacy = 1;
4258 break;
4259 }
ce04af5a
JM
4260 if (params->wps == WPS_MODE_PRIVACY)
4261 privacy = 1;
4262 if (params->pairwise_suite &&
4263 params->pairwise_suite != WPA_CIPHER_NONE)
4264 privacy = 1;
4265
cfaab580
ZY
4266 if (!privacy)
4267 return 0;
4268
4269 NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
4270
4271 nl_keys = nla_nest_start(msg, NL80211_ATTR_KEYS);
4272 if (!nl_keys)
4273 goto nla_put_failure;
4274
4275 for (i = 0; i < 4; i++) {
4276 if (!params->wep_key[i])
4277 continue;
4278
4279 nl_key = nla_nest_start(msg, i);
4280 if (!nl_key)
4281 goto nla_put_failure;
4282
4283 NLA_PUT(msg, NL80211_KEY_DATA, params->wep_key_len[i],
4284 params->wep_key[i]);
4285 if (params->wep_key_len[i] == 5)
4286 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
4287 WLAN_CIPHER_SUITE_WEP40);
4288 else
4289 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
4290 WLAN_CIPHER_SUITE_WEP104);
4291
4292 NLA_PUT_U8(msg, NL80211_KEY_IDX, i);
4293
4294 if (i == params->wep_tx_keyidx)
4295 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
4296
4297 nla_nest_end(msg, nl_key);
4298 }
4299 nla_nest_end(msg, nl_keys);
4300
4301 return 0;
4302
4303nla_put_failure:
4304 return -ENOBUFS;
4305}
4306
4307
c2a04078 4308static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
77339912
JM
4309 const u8 *addr, int cmd, u16 reason_code,
4310 int local_state_change)
c2a04078
JM
4311{
4312 int ret = -1;
4313 struct nl_msg *msg;
4314
4315 msg = nlmsg_alloc();
4316 if (!msg)
4317 return -1;
4318
9fb04070 4319 nl80211_cmd(drv, msg, 0, cmd);
c2a04078
JM
4320
4321 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
4322 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason_code);
4323 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
77339912
JM
4324 if (local_state_change)
4325 NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE);
c2a04078
JM
4326
4327 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4328 msg = NULL;
4329 if (ret) {
3b7ea880
BG
4330 wpa_dbg(drv->ctx, MSG_DEBUG,
4331 "nl80211: MLME command failed: reason=%u ret=%d (%s)",
4332 reason_code, ret, strerror(-ret));
c2a04078
JM
4333 goto nla_put_failure;
4334 }
4335 ret = 0;
4336
4337nla_put_failure:
4338 nlmsg_free(msg);
4339 return ret;
4340}
3f5285e8
JM
4341
4342
cfaab580
ZY
4343static int wpa_driver_nl80211_disconnect(struct wpa_driver_nl80211_data *drv,
4344 const u8 *addr, int reason_code)
4345{
2e75a2b3
JM
4346 wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " reason_code=%d)",
4347 __func__, MAC2STR(addr), reason_code);
cfaab580
ZY
4348 drv->associated = 0;
4349 return wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DISCONNECT,
77339912 4350 reason_code, 0);
cfaab580
ZY
4351}
4352
4353
3f5285e8 4354static int wpa_driver_nl80211_deauthenticate(void *priv, const u8 *addr,
c2a04078 4355 int reason_code)
3f5285e8 4356{
a2e40bb6
FF
4357 struct i802_bss *bss = priv;
4358 struct wpa_driver_nl80211_data *drv = bss->drv;
cfaab580
ZY
4359 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME))
4360 return wpa_driver_nl80211_disconnect(drv, addr, reason_code);
2e75a2b3
JM
4361 wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " reason_code=%d)",
4362 __func__, MAC2STR(addr), reason_code);
13405f35 4363 drv->associated = 0;
21bdbe38
JM
4364 if (drv->nlmode == NL80211_IFTYPE_ADHOC)
4365 return nl80211_leave_ibss(drv);
c2a04078 4366 return wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DEAUTHENTICATE,
77339912 4367 reason_code, 0);
3f5285e8
JM
4368}
4369
4370
4371static int wpa_driver_nl80211_disassociate(void *priv, const u8 *addr,
c2a04078 4372 int reason_code)
3f5285e8 4373{
a2e40bb6
FF
4374 struct i802_bss *bss = priv;
4375 struct wpa_driver_nl80211_data *drv = bss->drv;
cfaab580
ZY
4376 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME))
4377 return wpa_driver_nl80211_disconnect(drv, addr, reason_code);
c2a04078 4378 wpa_printf(MSG_DEBUG, "%s", __func__);
13405f35 4379 drv->associated = 0;
c2a04078 4380 return wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DISASSOCIATE,
77339912 4381 reason_code, 0);
3f5285e8
JM
4382}
4383
4384
536fd62d
JM
4385static void nl80211_copy_auth_params(struct wpa_driver_nl80211_data *drv,
4386 struct wpa_driver_auth_params *params)
4387{
4388 int i;
4389
4390 drv->auth_freq = params->freq;
4391 drv->auth_alg = params->auth_alg;
4392 drv->auth_wep_tx_keyidx = params->wep_tx_keyidx;
4393 drv->auth_local_state_change = params->local_state_change;
4394 drv->auth_p2p = params->p2p;
4395
4396 if (params->bssid)
4397 os_memcpy(drv->auth_bssid_, params->bssid, ETH_ALEN);
4398 else
4399 os_memset(drv->auth_bssid_, 0, ETH_ALEN);
4400
4401 if (params->ssid) {
4402 os_memcpy(drv->auth_ssid, params->ssid, params->ssid_len);
4403 drv->auth_ssid_len = params->ssid_len;
4404 } else
4405 drv->auth_ssid_len = 0;
4406
4407
4408 os_free(drv->auth_ie);
4409 drv->auth_ie = NULL;
4410 drv->auth_ie_len = 0;
4411 if (params->ie) {
4412 drv->auth_ie = os_malloc(params->ie_len);
4413 if (drv->auth_ie) {
4414 os_memcpy(drv->auth_ie, params->ie, params->ie_len);
4415 drv->auth_ie_len = params->ie_len;
4416 }
4417 }
4418
4419 for (i = 0; i < 4; i++) {
4420 if (params->wep_key[i] && params->wep_key_len[i] &&
4421 params->wep_key_len[i] <= 16) {
4422 os_memcpy(drv->auth_wep_key[i], params->wep_key[i],
4423 params->wep_key_len[i]);
4424 drv->auth_wep_key_len[i] = params->wep_key_len[i];
4425 } else
4426 drv->auth_wep_key_len[i] = 0;
4427 }
4428}
4429
4430
c2a04078
JM
4431static int wpa_driver_nl80211_authenticate(
4432 void *priv, struct wpa_driver_auth_params *params)
4433{
a2e40bb6
FF
4434 struct i802_bss *bss = priv;
4435 struct wpa_driver_nl80211_data *drv = bss->drv;
a0b2f99b 4436 int ret = -1, i;
c2a04078
JM
4437 struct nl_msg *msg;
4438 enum nl80211_auth_type type;
2f4f73b1 4439 enum nl80211_iftype nlmode;
6d6f4bb8 4440 int count = 0;
536fd62d
JM
4441 int is_retry;
4442
4443 is_retry = drv->retry_auth;
4444 drv->retry_auth = 0;
c2a04078
JM
4445
4446 drv->associated = 0;
e6b8efeb 4447 os_memset(drv->auth_bssid, 0, ETH_ALEN);
af473088 4448 /* FIX: IBSS mode */
2f4f73b1
EP
4449 nlmode = params->p2p ?
4450 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
4451 if (drv->nlmode != nlmode &&
4452 wpa_driver_nl80211_set_mode(priv, nlmode) < 0)
4a867032
JM
4453 return -1;
4454
6d6f4bb8 4455retry:
c2a04078
JM
4456 msg = nlmsg_alloc();
4457 if (!msg)
4458 return -1;
4459
4460 wpa_printf(MSG_DEBUG, "nl80211: Authenticate (ifindex=%d)",
4461 drv->ifindex);
a0b2f99b 4462
9fb04070 4463 nl80211_cmd(drv, msg, 0, NL80211_CMD_AUTHENTICATE);
0194fedb 4464
a0b2f99b
JM
4465 for (i = 0; i < 4; i++) {
4466 if (!params->wep_key[i])
4467 continue;
2ea2fcc7
HS
4468 wpa_driver_nl80211_set_key(bss->ifname, priv, WPA_ALG_WEP,
4469 NULL, i,
a0b2f99b
JM
4470 i == params->wep_tx_keyidx, NULL, 0,
4471 params->wep_key[i],
4472 params->wep_key_len[i]);
0194fedb
JB
4473 if (params->wep_tx_keyidx != i)
4474 continue;
4475 if (nl_add_key(msg, WPA_ALG_WEP, i, 1, NULL, 0,
4476 params->wep_key[i], params->wep_key_len[i])) {
4477 nlmsg_free(msg);
4478 return -1;
4479 }
a0b2f99b
JM
4480 }
4481
c2a04078
JM
4482 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
4483 if (params->bssid) {
4484 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
4485 MAC2STR(params->bssid));
4486 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
4487 }
4488 if (params->freq) {
4489 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
4490 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
4491 }
4492 if (params->ssid) {
4493 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
4494 params->ssid, params->ssid_len);
4495 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
4496 params->ssid);
4497 }
4498 wpa_hexdump(MSG_DEBUG, " * IEs", params->ie, params->ie_len);
4499 if (params->ie)
4500 NLA_PUT(msg, NL80211_ATTR_IE, params->ie_len, params->ie);
abd9fafa 4501 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
c2a04078 4502 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
abd9fafa 4503 else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
c2a04078 4504 type = NL80211_AUTHTYPE_SHARED_KEY;
abd9fafa 4505 else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
c2a04078 4506 type = NL80211_AUTHTYPE_NETWORK_EAP;
abd9fafa 4507 else if (params->auth_alg & WPA_AUTH_ALG_FT)
c2a04078
JM
4508 type = NL80211_AUTHTYPE_FT;
4509 else
4510 goto nla_put_failure;
4511 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
4512 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
77339912
JM
4513 if (params->local_state_change) {
4514 wpa_printf(MSG_DEBUG, " * Local state change only");
4515 NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE);
4516 }
c2a04078
JM
4517
4518 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4519 msg = NULL;
4520 if (ret) {
3b7ea880
BG
4521 wpa_dbg(drv->ctx, MSG_DEBUG,
4522 "nl80211: MLME command failed (auth): ret=%d (%s)",
4523 ret, strerror(-ret));
6d6f4bb8 4524 count++;
77339912
JM
4525 if (ret == -EALREADY && count == 1 && params->bssid &&
4526 !params->local_state_change) {
6d6f4bb8
JM
4527 /*
4528 * mac80211 does not currently accept new
4529 * authentication if we are already authenticated. As a
4530 * workaround, force deauthentication and try again.
4531 */
4532 wpa_printf(MSG_DEBUG, "nl80211: Retry authentication "
4533 "after forced deauthentication");
4534 wpa_driver_nl80211_deauthenticate(
5205c4f9 4535 bss, params->bssid,
6d6f4bb8
JM
4536 WLAN_REASON_PREV_AUTH_NOT_VALID);
4537 nlmsg_free(msg);
4538 goto retry;
4539 }
536fd62d
JM
4540
4541 if (ret == -ENOENT && params->freq && !is_retry) {
4542 /*
4543 * cfg80211 has likely expired the BSS entry even
4544 * though it was previously available in our internal
4545 * BSS table. To recover quickly, start a single
4546 * channel scan on the specified channel.
4547 */
4548 struct wpa_driver_scan_params scan;
4549 int freqs[2];
4550
4551 os_memset(&scan, 0, sizeof(scan));
4552 scan.num_ssids = 1;
4553 if (params->ssid) {
4554 scan.ssids[0].ssid = params->ssid;
4555 scan.ssids[0].ssid_len = params->ssid_len;
4556 }
4557 freqs[0] = params->freq;
4558 freqs[1] = 0;
4559 scan.freqs = freqs;
4560 wpa_printf(MSG_DEBUG, "nl80211: Trigger single "
4561 "channel scan to refresh cfg80211 BSS "
4562 "entry");
4563 ret = wpa_driver_nl80211_scan(bss, &scan);
4564 if (ret == 0) {
4565 nl80211_copy_auth_params(drv, params);
4566 drv->scan_for_auth = 1;
4567 }
8c3ba078
JM
4568 } else if (is_retry) {
4569 /*
4570 * Need to indicate this with an event since the return
4571 * value from the retry is not delivered to core code.
4572 */
4573 union wpa_event_data event;
4574 wpa_printf(MSG_DEBUG, "nl80211: Authentication retry "
4575 "failed");
4576 os_memset(&event, 0, sizeof(event));
4577 os_memcpy(event.timeout_event.addr, drv->auth_bssid_,
4578 ETH_ALEN);
4579 wpa_supplicant_event(drv->ctx, EVENT_AUTH_TIMED_OUT,
4580 &event);
536fd62d
JM
4581 }
4582
c2a04078
JM
4583 goto nla_put_failure;
4584 }
4585 ret = 0;
4586 wpa_printf(MSG_DEBUG, "nl80211: Authentication request send "
4587 "successfully");
4588
4589nla_put_failure:
4590 nlmsg_free(msg);
4591 return ret;
4592}
4593
4594
536fd62d
JM
4595static int wpa_driver_nl80211_authenticate_retry(
4596 struct wpa_driver_nl80211_data *drv)
4597{
4598 struct wpa_driver_auth_params params;
4599 struct i802_bss *bss = &drv->first_bss;
4600 int i;
4601
4602 wpa_printf(MSG_DEBUG, "nl80211: Try to authenticate again");
4603
4604 os_memset(&params, 0, sizeof(params));
4605 params.freq = drv->auth_freq;
4606 params.auth_alg = drv->auth_alg;
4607 params.wep_tx_keyidx = drv->auth_wep_tx_keyidx;
4608 params.local_state_change = drv->auth_local_state_change;
4609 params.p2p = drv->auth_p2p;
4610
4611 if (!is_zero_ether_addr(drv->auth_bssid_))
4612 params.bssid = drv->auth_bssid_;
4613
4614 if (drv->auth_ssid_len) {
4615 params.ssid = drv->auth_ssid;
4616 params.ssid_len = drv->auth_ssid_len;
4617 }
4618
4619 params.ie = drv->auth_ie;
4620 params.ie_len = drv->auth_ie_len;
4621
4622 for (i = 0; i < 4; i++) {
4623 if (drv->auth_wep_key_len[i]) {
4624 params.wep_key[i] = drv->auth_wep_key[i];
4625 params.wep_key_len[i] = drv->auth_wep_key_len[i];
4626 }
4627 }
4628
4629 drv->retry_auth = 1;
4630 return wpa_driver_nl80211_authenticate(bss, &params);
4631}
4632
4633
282d5590
JM
4634struct phy_info_arg {
4635 u16 *num_modes;
4636 struct hostapd_hw_modes *modes;
4637};
4638
4639static int phy_info_handler(struct nl_msg *msg, void *arg)
4640{
4641 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
4642 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
4643 struct phy_info_arg *phy_info = arg;
4644
4645 struct nlattr *tb_band[NL80211_BAND_ATTR_MAX + 1];
4646
4647 struct nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1];
4648 static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
4649 [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
4650 [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
4651 [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] = { .type = NLA_FLAG },
4652 [NL80211_FREQUENCY_ATTR_NO_IBSS] = { .type = NLA_FLAG },
4653 [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
4654 [NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32 },
4655 };
4656
4657 struct nlattr *tb_rate[NL80211_BITRATE_ATTR_MAX + 1];
4658 static struct nla_policy rate_policy[NL80211_BITRATE_ATTR_MAX + 1] = {
4659 [NL80211_BITRATE_ATTR_RATE] = { .type = NLA_U32 },
4660 [NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE] = { .type = NLA_FLAG },
4661 };
4662
4663 struct nlattr *nl_band;
4664 struct nlattr *nl_freq;
4665 struct nlattr *nl_rate;
4666 int rem_band, rem_freq, rem_rate;
4667 struct hostapd_hw_modes *mode;
4668 int idx, mode_is_set;
4669
4670 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
4671 genlmsg_attrlen(gnlh, 0), NULL);
4672
4673 if (!tb_msg[NL80211_ATTR_WIPHY_BANDS])
4674 return NL_SKIP;
4675
4676 nla_for_each_nested(nl_band, tb_msg[NL80211_ATTR_WIPHY_BANDS], rem_band) {
e62fb0a0 4677 mode = os_realloc(phy_info->modes, (*phy_info->num_modes + 1) * sizeof(*mode));
282d5590
JM
4678 if (!mode)
4679 return NL_SKIP;
4680 phy_info->modes = mode;
4681
4682 mode_is_set = 0;
4683
4684 mode = &phy_info->modes[*(phy_info->num_modes)];
4685 memset(mode, 0, sizeof(*mode));
e3b473eb 4686 mode->flags = HOSTAPD_MODE_FLAG_HT_INFO_KNOWN;
282d5590
JM
4687 *(phy_info->num_modes) += 1;
4688
4689 nla_parse(tb_band, NL80211_BAND_ATTR_MAX, nla_data(nl_band),
4690 nla_len(nl_band), NULL);
4691
4692 if (tb_band[NL80211_BAND_ATTR_HT_CAPA]) {
4693 mode->ht_capab = nla_get_u16(
4694 tb_band[NL80211_BAND_ATTR_HT_CAPA]);
4695 }
4696
be8eb8ab
JM
4697 if (tb_band[NL80211_BAND_ATTR_HT_AMPDU_FACTOR]) {
4698 mode->a_mpdu_params |= nla_get_u8(
4699 tb_band[NL80211_BAND_ATTR_HT_AMPDU_FACTOR]) &
4700 0x03;
4701 }
4702
4703 if (tb_band[NL80211_BAND_ATTR_HT_AMPDU_DENSITY]) {
4704 mode->a_mpdu_params |= nla_get_u8(
4705 tb_band[NL80211_BAND_ATTR_HT_AMPDU_DENSITY]) <<
4706 2;
4707 }
4708
08eb154d
JM
4709 if (tb_band[NL80211_BAND_ATTR_HT_MCS_SET] &&
4710 nla_len(tb_band[NL80211_BAND_ATTR_HT_MCS_SET])) {
4711 u8 *mcs;
4712 mcs = nla_data(tb_band[NL80211_BAND_ATTR_HT_MCS_SET]);
4713 os_memcpy(mode->mcs_set, mcs, 16);
4714 }
4715
282d5590
JM
4716 nla_for_each_nested(nl_freq, tb_band[NL80211_BAND_ATTR_FREQS], rem_freq) {
4717 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX, nla_data(nl_freq),
4718 nla_len(nl_freq), freq_policy);
4719 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
4720 continue;
4721 mode->num_channels++;
4722 }
4723
e62fb0a0 4724 mode->channels = os_zalloc(mode->num_channels * sizeof(struct hostapd_channel_data));
282d5590
JM
4725 if (!mode->channels)
4726 return NL_SKIP;
4727
4728 idx = 0;
4729
4730 nla_for_each_nested(nl_freq, tb_band[NL80211_BAND_ATTR_FREQS], rem_freq) {
4731 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX, nla_data(nl_freq),
4732 nla_len(nl_freq), freq_policy);
4733 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
4734 continue;
4735
4736 mode->channels[idx].freq = nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_FREQ]);
4737 mode->channels[idx].flag = 0;
4738
4739 if (!mode_is_set) {
4740 /* crude heuristic */
4741 if (mode->channels[idx].freq < 4000)
4742 mode->mode = HOSTAPD_MODE_IEEE80211B;
4743 else
4744 mode->mode = HOSTAPD_MODE_IEEE80211A;
4745 mode_is_set = 1;
4746 }
4747
4748 /* crude heuristic */
4749 if (mode->channels[idx].freq < 4000)
5a0ffb5f 4750 if (mode->channels[idx].freq == 2484)
282d5590
JM
4751 mode->channels[idx].chan = 14;
4752 else
4753 mode->channels[idx].chan = (mode->channels[idx].freq - 2407) / 5;
4754 else
4755 mode->channels[idx].chan = mode->channels[idx].freq/5 - 1000;
4756
4757 if (tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
4758 mode->channels[idx].flag |=
4759 HOSTAPD_CHAN_DISABLED;
4760 if (tb_freq[NL80211_FREQUENCY_ATTR_PASSIVE_SCAN])
4761 mode->channels[idx].flag |=
4762 HOSTAPD_CHAN_PASSIVE_SCAN;
4763 if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IBSS])
4764 mode->channels[idx].flag |=
4765 HOSTAPD_CHAN_NO_IBSS;
4766 if (tb_freq[NL80211_FREQUENCY_ATTR_RADAR])
4767 mode->channels[idx].flag |=
4768 HOSTAPD_CHAN_RADAR;
4769
4770 if (tb_freq[NL80211_FREQUENCY_ATTR_MAX_TX_POWER] &&
4771 !tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
4772 mode->channels[idx].max_tx_power =
4773 nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_MAX_TX_POWER]) / 100;
4774
4775 idx++;
4776 }
4777
4778 nla_for_each_nested(nl_rate, tb_band[NL80211_BAND_ATTR_RATES], rem_rate) {
4779 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX, nla_data(nl_rate),
4780 nla_len(nl_rate), rate_policy);
4781 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
4782 continue;
4783 mode->num_rates++;
4784 }
4785
e62fb0a0 4786 mode->rates = os_zalloc(mode->num_rates * sizeof(int));
282d5590
JM
4787 if (!mode->rates)
4788 return NL_SKIP;
4789
4790 idx = 0;
4791
4792 nla_for_each_nested(nl_rate, tb_band[NL80211_BAND_ATTR_RATES], rem_rate) {
4793 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX, nla_data(nl_rate),
4794 nla_len(nl_rate), rate_policy);
4795 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
4796 continue;
fb7842aa 4797 mode->rates[idx] = nla_get_u32(tb_rate[NL80211_BITRATE_ATTR_RATE]);
282d5590
JM
4798
4799 /* crude heuristic */
4800 if (mode->mode == HOSTAPD_MODE_IEEE80211B &&
fb7842aa 4801 mode->rates[idx] > 200)
282d5590
JM
4802 mode->mode = HOSTAPD_MODE_IEEE80211G;
4803
282d5590
JM
4804 idx++;
4805 }
4806 }
4807
4808 return NL_SKIP;
4809}
4810
4811static struct hostapd_hw_modes *
4812wpa_driver_nl80211_add_11b(struct hostapd_hw_modes *modes, u16 *num_modes)
4813{
4814 u16 m;
4815 struct hostapd_hw_modes *mode11g = NULL, *nmodes, *mode;
4816 int i, mode11g_idx = -1;
4817
4818 /* If only 802.11g mode is included, use it to construct matching
4819 * 802.11b mode data. */
4820
4821 for (m = 0; m < *num_modes; m++) {
4822 if (modes[m].mode == HOSTAPD_MODE_IEEE80211B)
4823 return modes; /* 802.11b already included */
4824 if (modes[m].mode == HOSTAPD_MODE_IEEE80211G)
4825 mode11g_idx = m;
4826 }
4827
4828 if (mode11g_idx < 0)
4829 return modes; /* 2.4 GHz band not supported at all */
4830
4831 nmodes = os_realloc(modes, (*num_modes + 1) * sizeof(*nmodes));
4832 if (nmodes == NULL)
4833 return modes; /* Could not add 802.11b mode */
4834
4835 mode = &nmodes[*num_modes];
4836 os_memset(mode, 0, sizeof(*mode));
4837 (*num_modes)++;
4838 modes = nmodes;
4839
4840 mode->mode = HOSTAPD_MODE_IEEE80211B;
4841
4842 mode11g = &modes[mode11g_idx];
4843 mode->num_channels = mode11g->num_channels;
4844 mode->channels = os_malloc(mode11g->num_channels *
4845 sizeof(struct hostapd_channel_data));
4846 if (mode->channels == NULL) {
4847 (*num_modes)--;
4848 return modes; /* Could not add 802.11b mode */
4849 }
4850 os_memcpy(mode->channels, mode11g->channels,
4851 mode11g->num_channels * sizeof(struct hostapd_channel_data));
4852
4853 mode->num_rates = 0;
fb7842aa 4854 mode->rates = os_malloc(4 * sizeof(int));
282d5590
JM
4855 if (mode->rates == NULL) {
4856 os_free(mode->channels);
4857 (*num_modes)--;
4858 return modes; /* Could not add 802.11b mode */
4859 }
4860
4861 for (i = 0; i < mode11g->num_rates; i++) {
fb7842aa
JM
4862 if (mode11g->rates[i] != 10 && mode11g->rates[i] != 20 &&
4863 mode11g->rates[i] != 55 && mode11g->rates[i] != 110)
282d5590
JM
4864 continue;
4865 mode->rates[mode->num_rates] = mode11g->rates[i];
4866 mode->num_rates++;
4867 if (mode->num_rates == 4)
4868 break;
4869 }
4870
4871 if (mode->num_rates == 0) {
4872 os_free(mode->channels);
4873 os_free(mode->rates);
4874 (*num_modes)--;
4875 return modes; /* No 802.11b rates */
4876 }
4877
4878 wpa_printf(MSG_DEBUG, "nl80211: Added 802.11b mode based on 802.11g "
4879 "information");
4880
4881 return modes;
4882}
4883
4884
d8e66e80
JM
4885static void nl80211_set_ht40_mode(struct hostapd_hw_modes *mode, int start,
4886 int end)
4887{
4888 int c;
4889
4890 for (c = 0; c < mode->num_channels; c++) {
4891 struct hostapd_channel_data *chan = &mode->channels[c];
4892 if (chan->freq - 10 >= start && chan->freq + 10 <= end)
4893 chan->flag |= HOSTAPD_CHAN_HT40;
4894 }
4895}
4896
4897
4898static void nl80211_set_ht40_mode_sec(struct hostapd_hw_modes *mode, int start,
4899 int end)
4900{
4901 int c;
4902
4903 for (c = 0; c < mode->num_channels; c++) {
4904 struct hostapd_channel_data *chan = &mode->channels[c];
4905 if (!(chan->flag & HOSTAPD_CHAN_HT40))
4906 continue;
4907 if (chan->freq - 30 >= start && chan->freq - 10 <= end)
4908 chan->flag |= HOSTAPD_CHAN_HT40MINUS;
4909 if (chan->freq + 10 >= start && chan->freq + 30 <= end)
4910 chan->flag |= HOSTAPD_CHAN_HT40PLUS;
4911 }
4912}
4913
4914
4915static void nl80211_reg_rule_ht40(struct nlattr *tb[],
4916 struct phy_info_arg *results)
4917{
4918 u32 start, end, max_bw;
4919 u16 m;
4920
4921 if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
4922 tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
4923 tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
4924 return;
4925
4926 start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
4927 end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
4928 max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
4929
4930 wpa_printf(MSG_DEBUG, "nl80211: %u-%u @ %u MHz",
4931 start, end, max_bw);
4932 if (max_bw < 40)
4933 return;
4934
4935 for (m = 0; m < *results->num_modes; m++) {
4936 if (!(results->modes[m].ht_capab &
4937 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
4938 continue;
4939 nl80211_set_ht40_mode(&results->modes[m], start, end);
4940 }
4941}
4942
4943
4944static void nl80211_reg_rule_sec(struct nlattr *tb[],
4945 struct phy_info_arg *results)
4946{
4947 u32 start, end, max_bw;
4948 u16 m;
4949
4950 if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
4951 tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
4952 tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
4953 return;
4954
4955 start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
4956 end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
4957 max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
4958
4959 if (max_bw < 20)
4960 return;
4961
4962 for (m = 0; m < *results->num_modes; m++) {
4963 if (!(results->modes[m].ht_capab &
4964 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
4965 continue;
4966 nl80211_set_ht40_mode_sec(&results->modes[m], start, end);
4967 }
4968}
4969
4970
4971static int nl80211_get_reg(struct nl_msg *msg, void *arg)
4972{
4973 struct phy_info_arg *results = arg;
4974 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
4975 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
4976 struct nlattr *nl_rule;
4977 struct nlattr *tb_rule[NL80211_FREQUENCY_ATTR_MAX + 1];
4978 int rem_rule;
4979 static struct nla_policy reg_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
4980 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
4981 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
4982 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
4983 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
4984 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
4985 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
4986 };
4987
4988 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
4989 genlmsg_attrlen(gnlh, 0), NULL);
4990 if (!tb_msg[NL80211_ATTR_REG_ALPHA2] ||
4991 !tb_msg[NL80211_ATTR_REG_RULES]) {
4992 wpa_printf(MSG_DEBUG, "nl80211: No regulatory information "
4993 "available");
4994 return NL_SKIP;
4995 }
4996
4997 wpa_printf(MSG_DEBUG, "nl80211: Regulatory information - country=%s",
4998 (char *) nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]));
4999
5000 nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
5001 {
5002 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
5003 nla_data(nl_rule), nla_len(nl_rule), reg_policy);
5004 nl80211_reg_rule_ht40(tb_rule, results);
5005 }
5006
5007 nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
5008 {
5009 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
5010 nla_data(nl_rule), nla_len(nl_rule), reg_policy);
5011 nl80211_reg_rule_sec(tb_rule, results);
5012 }
5013
5014 return NL_SKIP;
5015}
5016
5017
5018static int nl80211_set_ht40_flags(struct wpa_driver_nl80211_data *drv,
5019 struct phy_info_arg *results)
5020{
5021 struct nl_msg *msg;
5022
5023 msg = nlmsg_alloc();
5024 if (!msg)
5025 return -ENOMEM;
5026
9fb04070 5027 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG);
d8e66e80
JM
5028 return send_and_recv_msgs(drv, msg, nl80211_get_reg, results);
5029}
5030
5031
282d5590
JM
5032static struct hostapd_hw_modes *
5033wpa_driver_nl80211_get_hw_feature_data(void *priv, u16 *num_modes, u16 *flags)
5034{
a2e40bb6
FF
5035 struct i802_bss *bss = priv;
5036 struct wpa_driver_nl80211_data *drv = bss->drv;
282d5590
JM
5037 struct nl_msg *msg;
5038 struct phy_info_arg result = {
5039 .num_modes = num_modes,
5040 .modes = NULL,
5041 };
5042
5043 *num_modes = 0;
5044 *flags = 0;
5045
5046 msg = nlmsg_alloc();
5047 if (!msg)
5048 return NULL;
5049
9fb04070 5050 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_WIPHY);
282d5590
JM
5051
5052 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
5053
d8e66e80
JM
5054 if (send_and_recv_msgs(drv, msg, phy_info_handler, &result) == 0) {
5055 nl80211_set_ht40_flags(drv, &result);
282d5590 5056 return wpa_driver_nl80211_add_11b(result.modes, num_modes);
d8e66e80 5057 }
5883168a 5058 msg = NULL;
282d5590 5059 nla_put_failure:
5883168a 5060 nlmsg_free(msg);
282d5590
JM
5061 return NULL;
5062}
5063
5064
a11241fa
JB
5065static int wpa_driver_nl80211_send_mntr(struct wpa_driver_nl80211_data *drv,
5066 const void *data, size_t len,
5067 int encrypt, int noack)
2c2010ac
JM
5068{
5069 __u8 rtap_hdr[] = {
5070 0x00, 0x00, /* radiotap version */
5071 0x0e, 0x00, /* radiotap length */
5072 0x02, 0xc0, 0x00, 0x00, /* bmap: flags, tx and rx flags */
5073 IEEE80211_RADIOTAP_F_FRAG, /* F_FRAG (fragment if required) */
5074 0x00, /* padding */
5075 0x00, 0x00, /* RX and TX flags to indicate that */
5076 0x00, 0x00, /* this is the injected frame directly */
5077 };
5078 struct iovec iov[2] = {
5079 {
5080 .iov_base = &rtap_hdr,
5081 .iov_len = sizeof(rtap_hdr),
5082 },
5083 {
5084 .iov_base = (void *) data,
5085 .iov_len = len,
5086 }
5087 };
5088 struct msghdr msg = {
5089 .msg_name = NULL,
5090 .msg_namelen = 0,
5091 .msg_iov = iov,
5092 .msg_iovlen = 2,
5093 .msg_control = NULL,
5094 .msg_controllen = 0,
5095 .msg_flags = 0,
5096 };
ebbec8b2 5097 int res;
fab25336 5098 u16 txflags = 0;
2c2010ac
JM
5099
5100 if (encrypt)
5101 rtap_hdr[8] |= IEEE80211_RADIOTAP_F_WEP;
5102
866af8b6
JM
5103 if (drv->monitor_sock < 0) {
5104 wpa_printf(MSG_DEBUG, "nl80211: No monitor socket available "
5105 "for %s", __func__);
5106 return -1;
5107 }
5108
fab25336
HS
5109 if (noack)
5110 txflags |= IEEE80211_RADIOTAP_F_TX_NOACK;
5111 *(le16 *) &rtap_hdr[12] = host_to_le16(txflags);
5112
ebbec8b2
JM
5113 res = sendmsg(drv->monitor_sock, &msg, 0);
5114 if (res < 0) {
5115 wpa_printf(MSG_INFO, "nl80211: sendmsg: %s", strerror(errno));
5116 return -1;
5117 }
5118 return 0;
2c2010ac
JM
5119}
5120
5121
a11241fa
JB
5122static int wpa_driver_nl80211_send_frame(struct i802_bss *bss,
5123 const void *data, size_t len,
55231068
JM
5124 int encrypt, int noack,
5125 unsigned int freq, int no_cck,
5126 int offchanok, unsigned int wait_time)
a11241fa
JB
5127{
5128 struct wpa_driver_nl80211_data *drv = bss->drv;
5129 u64 cookie;
5130
55231068
JM
5131 if (freq == 0)
5132 freq = bss->freq;
5133
a11241fa
JB
5134 if (drv->use_monitor)
5135 return wpa_driver_nl80211_send_mntr(drv, data, len,
5136 encrypt, noack);
5137
55231068
JM
5138 return nl80211_send_frame_cmd(bss, freq, wait_time, data, len,
5139 &cookie, no_cck, noack, offchanok);
a11241fa
JB
5140}
5141
5142
55231068
JM
5143static int wpa_driver_nl80211_send_mlme_freq(struct i802_bss *bss,
5144 const u8 *data,
5145 size_t data_len, int noack,
5146 unsigned int freq, int no_cck,
5147 int offchanok,
5148 unsigned int wait_time)
2c2010ac 5149{
a2e40bb6 5150 struct wpa_driver_nl80211_data *drv = bss->drv;
2c2010ac 5151 struct ieee80211_mgmt *mgmt;
7a47d567 5152 int encrypt = 1;
2c2010ac
JM
5153 u16 fc;
5154
5155 mgmt = (struct ieee80211_mgmt *) data;
5156 fc = le_to_host16(mgmt->frame_control);
5157
b1f625e0 5158 if (is_sta_interface(drv->nlmode) &&
5582a5d1
JB
5159 WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
5160 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_RESP) {
5161 /*
5162 * The use of last_mgmt_freq is a bit of a hack,
5163 * but it works due to the single-threaded nature
5164 * of wpa_supplicant.
5165 */
55231068
JM
5166 if (freq == 0)
5167 freq = drv->last_mgmt_freq;
5168 return nl80211_send_frame_cmd(bss, freq, 0,
88df0ef7
JB
5169 data, data_len, NULL, 1, noack,
5170 1);
5582a5d1
JB
5171 }
5172
61cbe2ff 5173 if (drv->device_ap_sme && is_ap_interface(drv->nlmode)) {
55231068
JM
5174 if (freq == 0)
5175 freq = bss->freq;
5176 return nl80211_send_frame_cmd(bss, freq, 0,
d8d6b32e
DG
5177 data, data_len,
5178 &drv->send_action_cookie,
55231068 5179 no_cck, noack, offchanok);
86957e62
JM
5180 }
5181
2c2010ac
JM
5182 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
5183 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) {
5184 /*
5185 * Only one of the authentication frame types is encrypted.
5186 * In order for static WEP encryption to work properly (i.e.,
5187 * to not encrypt the frame), we need to tell mac80211 about
5188 * the frames that must not be encrypted.
5189 */
5190 u16 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
5191 u16 auth_trans = le_to_host16(mgmt->u.auth.auth_transaction);
7a47d567
JB
5192 if (auth_alg != WLAN_AUTH_SHARED_KEY || auth_trans != 3)
5193 encrypt = 0;
2c2010ac
JM
5194 }
5195
a11241fa 5196 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt,
55231068
JM
5197 noack, freq, no_cck, offchanok,
5198 wait_time);
5199}
5200
5201
5202static int wpa_driver_nl80211_send_mlme(void *priv, const u8 *data,
5203 size_t data_len, int noack)
5204{
5205 struct i802_bss *bss = priv;
5206 return wpa_driver_nl80211_send_mlme_freq(bss, data, data_len, noack,
5207 0, 0, 0, 0);
2c2010ac
JM
5208}
5209
5210
31357268 5211static int nl80211_set_bss(struct i802_bss *bss, int cts, int preamble,
e5693c47
JM
5212 int slot, int ht_opmode, int ap_isolate,
5213 int *basic_rates)
31357268
JM
5214{
5215 struct wpa_driver_nl80211_data *drv = bss->drv;
5216 struct nl_msg *msg;
5217
5218 msg = nlmsg_alloc();
5219 if (!msg)
5220 return -ENOMEM;
5221
9fb04070 5222 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_BSS);
31357268
JM
5223
5224 if (cts >= 0)
5225 NLA_PUT_U8(msg, NL80211_ATTR_BSS_CTS_PROT, cts);
5226 if (preamble >= 0)
5227 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble);
5228 if (slot >= 0)
5229 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot);
5230 if (ht_opmode >= 0)
5231 NLA_PUT_U16(msg, NL80211_ATTR_BSS_HT_OPMODE, ht_opmode);
d03e8d11
JM
5232 if (ap_isolate >= 0)
5233 NLA_PUT_U8(msg, NL80211_ATTR_AP_ISOLATE, ap_isolate);
e5693c47
JM
5234
5235 if (basic_rates) {
5236 u8 rates[NL80211_MAX_SUPP_RATES];
5237 u8 rates_len = 0;
5238 int i;
5239
5240 for (i = 0; i < NL80211_MAX_SUPP_RATES && basic_rates[i] >= 0;
5241 i++)
5242 rates[rates_len++] = basic_rates[i] / 5;
5243
5244 NLA_PUT(msg, NL80211_ATTR_BSS_BASIC_RATES, rates_len, rates);
5245 }
5246
31357268
JM
5247 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
5248
5249 return send_and_recv_msgs(drv, msg, NULL, NULL);
5250 nla_put_failure:
5883168a 5251 nlmsg_free(msg);
31357268
JM
5252 return -ENOBUFS;
5253}
5254
5255
19c3b566
JM
5256static int wpa_driver_nl80211_set_ap(void *priv,
5257 struct wpa_driver_ap_params *params)
d2440ba0 5258{
a2e40bb6
FF
5259 struct i802_bss *bss = priv;
5260 struct wpa_driver_nl80211_data *drv = bss->drv;
d2440ba0
JM
5261 struct nl_msg *msg;
5262 u8 cmd = NL80211_CMD_NEW_BEACON;
5263 int ret;
b4fd6fab 5264 int beacon_set;
8b897f5a 5265 int ifindex = if_nametoindex(bss->ifname);
b11d1d64
JM
5266 int num_suites;
5267 u32 suites[10];
5268 u32 ver;
b4fd6fab 5269
b4fd6fab 5270 beacon_set = bss->beacon_set;
d2440ba0
JM
5271
5272 msg = nlmsg_alloc();
5273 if (!msg)
5274 return -ENOMEM;
5275
5276 wpa_printf(MSG_DEBUG, "nl80211: Set beacon (beacon_set=%d)",
b4fd6fab
JM
5277 beacon_set);
5278 if (beacon_set)
d2440ba0
JM
5279 cmd = NL80211_CMD_SET_BEACON;
5280
9fb04070 5281 nl80211_cmd(drv, msg, 0, cmd);
19c3b566
JM
5282 NLA_PUT(msg, NL80211_ATTR_BEACON_HEAD, params->head_len, params->head);
5283 NLA_PUT(msg, NL80211_ATTR_BEACON_TAIL, params->tail_len, params->tail);
b4fd6fab 5284 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
19c3b566
JM
5285 NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL, params->beacon_int);
5286 NLA_PUT_U32(msg, NL80211_ATTR_DTIM_PERIOD, params->dtim_period);
ccb941e6
JM
5287 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
5288 params->ssid);
5ed33546
AN
5289 if (params->proberesp && params->proberesp_len)
5290 NLA_PUT(msg, NL80211_ATTR_PROBE_RESP, params->proberesp_len,
5291 params->proberesp);
97a7a0b5
JM
5292 switch (params->hide_ssid) {
5293 case NO_SSID_HIDING:
5294 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
5295 NL80211_HIDDEN_SSID_NOT_IN_USE);
5296 break;
5297 case HIDDEN_SSID_ZERO_LEN:
5298 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
5299 NL80211_HIDDEN_SSID_ZERO_LEN);
5300 break;
5301 case HIDDEN_SSID_ZERO_CONTENTS:
5302 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
5303 NL80211_HIDDEN_SSID_ZERO_CONTENTS);
5304 break;
5305 }
b11d1d64
JM
5306 if (params->privacy)
5307 NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
5308 if ((params->auth_algs & (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) ==
5309 (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) {
5310 /* Leave out the attribute */
5311 } else if (params->auth_algs & WPA_AUTH_ALG_SHARED)
5312 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE,
5313 NL80211_AUTHTYPE_SHARED_KEY);
5314 else
5315 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE,
5316 NL80211_AUTHTYPE_OPEN_SYSTEM);
5317
5318 ver = 0;
5319 if (params->wpa_version & WPA_PROTO_WPA)
5320 ver |= NL80211_WPA_VERSION_1;
5321 if (params->wpa_version & WPA_PROTO_RSN)
5322 ver |= NL80211_WPA_VERSION_2;
5323 if (ver)
5324 NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver);
5325
5326 num_suites = 0;
5327 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X)
5328 suites[num_suites++] = WLAN_AKM_SUITE_8021X;
5329 if (params->key_mgmt_suites & WPA_KEY_MGMT_PSK)
5330 suites[num_suites++] = WLAN_AKM_SUITE_PSK;
5331 if (num_suites) {
5332 NLA_PUT(msg, NL80211_ATTR_AKM_SUITES,
5333 num_suites * sizeof(u32), suites);
5334 }
5335
9f12614b
JB
5336 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X &&
5337 params->pairwise_ciphers & (WPA_CIPHER_WEP104 | WPA_CIPHER_WEP40))
5338 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT);
5339
b11d1d64
JM
5340 num_suites = 0;
5341 if (params->pairwise_ciphers & WPA_CIPHER_CCMP)
5342 suites[num_suites++] = WLAN_CIPHER_SUITE_CCMP;
5343 if (params->pairwise_ciphers & WPA_CIPHER_TKIP)
5344 suites[num_suites++] = WLAN_CIPHER_SUITE_TKIP;
5345 if (params->pairwise_ciphers & WPA_CIPHER_WEP104)
5346 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP104;
5347 if (params->pairwise_ciphers & WPA_CIPHER_WEP40)
5348 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP40;
5349 if (num_suites) {
5350 NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
5351 num_suites * sizeof(u32), suites);
5352 }
5353
5354 switch (params->group_cipher) {
5355 case WPA_CIPHER_CCMP:
5356 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
5357 WLAN_CIPHER_SUITE_CCMP);
5358 break;
5359 case WPA_CIPHER_TKIP:
5360 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
5361 WLAN_CIPHER_SUITE_TKIP);
5362 break;
5363 case WPA_CIPHER_WEP104:
5364 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
5365 WLAN_CIPHER_SUITE_WEP104);
5366 break;
5367 case WPA_CIPHER_WEP40:
5368 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
5369 WLAN_CIPHER_SUITE_WEP40);
5370 break;
5371 }
d2440ba0 5372
fb91db56
JM
5373 if (params->beacon_ies) {
5374 NLA_PUT(msg, NL80211_ATTR_IE, wpabuf_len(params->beacon_ies),
5375 wpabuf_head(params->beacon_ies));
5376 }
5377 if (params->proberesp_ies) {
5378 NLA_PUT(msg, NL80211_ATTR_IE_PROBE_RESP,
5379 wpabuf_len(params->proberesp_ies),
5380 wpabuf_head(params->proberesp_ies));
5381 }
5382 if (params->assocresp_ies) {
5383 NLA_PUT(msg, NL80211_ATTR_IE_ASSOC_RESP,
5384 wpabuf_len(params->assocresp_ies),
5385 wpabuf_head(params->assocresp_ies));
5386 }
5387
d2440ba0
JM
5388 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5389 if (ret) {
5390 wpa_printf(MSG_DEBUG, "nl80211: Beacon set failed: %d (%s)",
5391 ret, strerror(-ret));
b4fd6fab 5392 } else {
b4fd6fab 5393 bss->beacon_set = 1;
31357268 5394 nl80211_set_bss(bss, params->cts_protect, params->preamble,
d03e8d11 5395 params->short_slot_time, params->ht_opmode,
e5693c47 5396 params->isolate, params->basic_rates);
b4fd6fab 5397 }
d2440ba0
JM
5398 return ret;
5399 nla_put_failure:
5883168a 5400 nlmsg_free(msg);
d2440ba0
JM
5401 return -ENOBUFS;
5402}
5403
5404
e4fb2167 5405static int wpa_driver_nl80211_set_freq(struct i802_bss *bss,
f019981a
JM
5406 int freq, int ht_enabled,
5407 int sec_channel_offset)
1581b38b 5408{
e4fb2167 5409 struct wpa_driver_nl80211_data *drv = bss->drv;
1581b38b 5410 struct nl_msg *msg;
d2440ba0 5411 int ret;
1581b38b 5412
44dc872e
JM
5413 wpa_printf(MSG_DEBUG, "nl80211: Set freq %d (ht_enabled=%d "
5414 "sec_channel_offset=%d)",
5415 freq, ht_enabled, sec_channel_offset);
1581b38b
JM
5416 msg = nlmsg_alloc();
5417 if (!msg)
5418 return -1;
5419
9fb04070 5420 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
1581b38b
JM
5421
5422 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
f019981a
JM
5423 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
5424 if (ht_enabled) {
5425 switch (sec_channel_offset) {
5426 case -1:
5427 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
5428 NL80211_CHAN_HT40MINUS);
5429 break;
5430 case 1:
5431 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
5432 NL80211_CHAN_HT40PLUS);
5433 break;
5434 default:
5435 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
5436 NL80211_CHAN_HT20);
5437 break;
5438 }
5439 }
1581b38b 5440
d2440ba0 5441 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5883168a 5442 msg = NULL;
e4fb2167
JB
5443 if (ret == 0) {
5444 bss->freq = freq;
1581b38b 5445 return 0;
e4fb2167 5446 }
f019981a
JM
5447 wpa_printf(MSG_DEBUG, "nl80211: Failed to set channel (freq=%d): "
5448 "%d (%s)", freq, ret, strerror(-ret));
1581b38b 5449nla_put_failure:
5883168a 5450 nlmsg_free(msg);
1581b38b
JM
5451 return -1;
5452}
5453
0f4e8b4f 5454
95ab6063
AN
5455static u32 sta_flags_nl80211(int flags)
5456{
5457 u32 f = 0;
5458
5459 if (flags & WPA_STA_AUTHORIZED)
5460 f |= BIT(NL80211_STA_FLAG_AUTHORIZED);
5461 if (flags & WPA_STA_WMM)
5462 f |= BIT(NL80211_STA_FLAG_WME);
5463 if (flags & WPA_STA_SHORT_PREAMBLE)
5464 f |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
5465 if (flags & WPA_STA_MFP)
5466 f |= BIT(NL80211_STA_FLAG_MFP);
45b722f1
AN
5467 if (flags & WPA_STA_TDLS_PEER)
5468 f |= BIT(NL80211_STA_FLAG_TDLS_PEER);
95ab6063
AN
5469
5470 return f;
5471}
5472
5473
62847751 5474static int wpa_driver_nl80211_sta_add(void *priv,
0f4e8b4f
JM
5475 struct hostapd_sta_add_params *params)
5476{
a2e40bb6
FF
5477 struct i802_bss *bss = priv;
5478 struct wpa_driver_nl80211_data *drv = bss->drv;
774bfa62 5479 struct nl_msg *msg, *wme = NULL;
95ab6063 5480 struct nl80211_sta_flag_update upd;
0f4e8b4f
JM
5481 int ret = -ENOBUFS;
5482
45b722f1
AN
5483 if ((params->flags & WPA_STA_TDLS_PEER) &&
5484 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
5485 return -EOPNOTSUPP;
5486
0f4e8b4f
JM
5487 msg = nlmsg_alloc();
5488 if (!msg)
5489 return -ENOMEM;
5490
45b722f1
AN
5491 nl80211_cmd(drv, msg, 0, params->set ? NL80211_CMD_SET_STATION :
5492 NL80211_CMD_NEW_STATION);
0f4e8b4f 5493
62847751 5494 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
0f4e8b4f 5495 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr);
0f4e8b4f
JM
5496 NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_RATES, params->supp_rates_len,
5497 params->supp_rates);
45b722f1
AN
5498 if (!params->set) {
5499 NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, params->aid);
5500 NLA_PUT_U16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
5501 params->listen_interval);
5502 }
0f4e8b4f
JM
5503 if (params->ht_capabilities) {
5504 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY,
fc4e2d95
JM
5505 sizeof(*params->ht_capabilities),
5506 params->ht_capabilities);
0f4e8b4f 5507 }
0f4e8b4f 5508
95ab6063
AN
5509 os_memset(&upd, 0, sizeof(upd));
5510 upd.mask = sta_flags_nl80211(params->flags);
5511 upd.set = upd.mask;
5512 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
5513
774bfa62
EP
5514 if (params->flags & WPA_STA_WMM) {
5515 wme = nlmsg_alloc();
5516 if (!wme)
5517 goto nla_put_failure;
5518
5519 NLA_PUT_U8(wme, NL80211_STA_WME_UAPSD_QUEUES,
5d061637
JY
5520 params->qosinfo & WMM_QOSINFO_STA_AC_MASK);
5521 NLA_PUT_U8(wme, NL80211_STA_WME_MAX_SP,
5522 (params->qosinfo > WMM_QOSINFO_STA_SP_SHIFT) &
5523 WMM_QOSINFO_STA_SP_MASK);
774bfa62 5524 nla_put_nested(msg, NL80211_ATTR_STA_WME, wme);
774bfa62
EP
5525 }
5526
0f4e8b4f 5527 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5883168a 5528 msg = NULL;
0f4e8b4f 5529 if (ret)
45b722f1
AN
5530 wpa_printf(MSG_DEBUG, "nl80211: NL80211_CMD_%s_STATION "
5531 "result: %d (%s)", params->set ? "SET" : "NEW", ret,
5532 strerror(-ret));
0f4e8b4f
JM
5533 if (ret == -EEXIST)
5534 ret = 0;
5535 nla_put_failure:
5d061637 5536 nlmsg_free(wme);
5883168a 5537 nlmsg_free(msg);
0f4e8b4f
JM
5538 return ret;
5539}
5540
5541
5542static int wpa_driver_nl80211_sta_remove(void *priv, const u8 *addr)
5543{
a2e40bb6
FF
5544 struct i802_bss *bss = priv;
5545 struct wpa_driver_nl80211_data *drv = bss->drv;
0f4e8b4f
JM
5546 struct nl_msg *msg;
5547 int ret;
5548
5549 msg = nlmsg_alloc();
5550 if (!msg)
5551 return -ENOMEM;
5552
9fb04070 5553 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_STATION);
0f4e8b4f
JM
5554
5555 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
a2e40bb6 5556 if_nametoindex(bss->ifname));
0f4e8b4f
JM
5557 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
5558
5559 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5560 if (ret == -ENOENT)
5561 return 0;
5562 return ret;
5563 nla_put_failure:
5883168a 5564 nlmsg_free(msg);
0f4e8b4f
JM
5565 return -ENOBUFS;
5566}
5567
1581b38b 5568
0915d02c
JM
5569static void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv,
5570 int ifidx)
5571{
5572 struct nl_msg *msg;
5573
c6e8e8e4
JM
5574 wpa_printf(MSG_DEBUG, "nl80211: Remove interface ifindex=%d", ifidx);
5575
2135f224
JM
5576 /* stop listening for EAPOL on this interface */
5577 del_ifidx(drv, ifidx);
2135f224 5578
0915d02c
JM
5579 msg = nlmsg_alloc();
5580 if (!msg)
5581 goto nla_put_failure;
5582
9fb04070 5583 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_INTERFACE);
0915d02c
JM
5584 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifidx);
5585
5586 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
5587 return;
5883168a 5588 msg = NULL;
0915d02c 5589 nla_put_failure:
5883168a 5590 nlmsg_free(msg);
e748062b 5591 wpa_printf(MSG_ERROR, "Failed to remove interface (ifidx=%d)", ifidx);
0915d02c
JM
5592}
5593
5594
a1922f93
JM
5595static const char * nl80211_iftype_str(enum nl80211_iftype mode)
5596{
5597 switch (mode) {
5598 case NL80211_IFTYPE_ADHOC:
5599 return "ADHOC";
5600 case NL80211_IFTYPE_STATION:
5601 return "STATION";
5602 case NL80211_IFTYPE_AP:
5603 return "AP";
5604 case NL80211_IFTYPE_MONITOR:
5605 return "MONITOR";
5606 case NL80211_IFTYPE_P2P_CLIENT:
5607 return "P2P_CLIENT";
5608 case NL80211_IFTYPE_P2P_GO:
5609 return "P2P_GO";
5610 default:
5611 return "unknown";
5612 }
5613}
5614
5615
a35187e7
KH
5616static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv,
5617 const char *ifname,
5618 enum nl80211_iftype iftype,
fbbfcbac 5619 const u8 *addr, int wds)
0915d02c
JM
5620{
5621 struct nl_msg *msg, *flags = NULL;
5622 int ifidx;
5623 int ret = -ENOBUFS;
5624
a1922f93
JM
5625 wpa_printf(MSG_DEBUG, "nl80211: Create interface iftype %d (%s)",
5626 iftype, nl80211_iftype_str(iftype));
5627
0915d02c
JM
5628 msg = nlmsg_alloc();
5629 if (!msg)
5630 return -1;
5631
9fb04070 5632 nl80211_cmd(drv, msg, 0, NL80211_CMD_NEW_INTERFACE);
0915d02c
JM
5633 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
5634 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, ifname);
5635 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, iftype);
5636
5637 if (iftype == NL80211_IFTYPE_MONITOR) {
5638 int err;
5639
5640 flags = nlmsg_alloc();
5641 if (!flags)
5642 goto nla_put_failure;
5643
5644 NLA_PUT_FLAG(flags, NL80211_MNTR_FLAG_COOK_FRAMES);
5645
5646 err = nla_put_nested(msg, NL80211_ATTR_MNTR_FLAGS, flags);
5647
5648 nlmsg_free(flags);
5649
5650 if (err)
5651 goto nla_put_failure;
fbbfcbac
FF
5652 } else if (wds) {
5653 NLA_PUT_U8(msg, NL80211_ATTR_4ADDR, wds);
0915d02c
JM
5654 }
5655
5656 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5883168a 5657 msg = NULL;
0915d02c
JM
5658 if (ret) {
5659 nla_put_failure:
5883168a 5660 nlmsg_free(msg);
a35187e7
KH
5661 wpa_printf(MSG_ERROR, "Failed to create interface %s: %d (%s)",
5662 ifname, ret, strerror(-ret));
0915d02c
JM
5663 return ret;
5664 }
5665
5666 ifidx = if_nametoindex(ifname);
c6e8e8e4
JM
5667 wpa_printf(MSG_DEBUG, "nl80211: New interface %s created: ifindex=%d",
5668 ifname, ifidx);
0915d02c
JM
5669
5670 if (ifidx <= 0)
5671 return -1;
5672
2135f224
JM
5673 /* start listening for EAPOL on this interface */
5674 add_ifidx(drv, ifidx);
5675
7bfc47c3 5676 if (addr && iftype != NL80211_IFTYPE_MONITOR &&
c81eff1a 5677 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname, addr)) {
41d931ee
JM
5678 nl80211_remove_iface(drv, ifidx);
5679 return -1;
2135f224 5680 }
2135f224 5681
0915d02c
JM
5682 return ifidx;
5683}
22a7c9d7
JM
5684
5685
a35187e7
KH
5686static int nl80211_create_iface(struct wpa_driver_nl80211_data *drv,
5687 const char *ifname, enum nl80211_iftype iftype,
fbbfcbac 5688 const u8 *addr, int wds)
a35187e7
KH
5689{
5690 int ret;
5691
fbbfcbac 5692 ret = nl80211_create_iface_once(drv, ifname, iftype, addr, wds);
a35187e7 5693
ffbf1eaa 5694 /* if error occurred and interface exists already */
a35187e7
KH
5695 if (ret == -ENFILE && if_nametoindex(ifname)) {
5696 wpa_printf(MSG_INFO, "Try to remove and re-create %s", ifname);
5697
5698 /* Try to remove the interface that was already there. */
5699 nl80211_remove_iface(drv, if_nametoindex(ifname));
5700
5701 /* Try to create the interface again */
fbbfcbac
FF
5702 ret = nl80211_create_iface_once(drv, ifname, iftype, addr,
5703 wds);
a35187e7
KH
5704 }
5705
b3af99d2 5706 if (ret >= 0 && is_p2p_interface(iftype))
4e5cb1a3
JM
5707 nl80211_disable_11b_rates(drv, ret, 1);
5708
a35187e7
KH
5709 return ret;
5710}
0915d02c 5711
2135f224 5712
0915d02c
JM
5713static void handle_tx_callback(void *ctx, u8 *buf, size_t len, int ok)
5714{
5715 struct ieee80211_hdr *hdr;
f8b1f695
JM
5716 u16 fc;
5717 union wpa_event_data event;
0915d02c
JM
5718
5719 hdr = (struct ieee80211_hdr *) buf;
5720 fc = le_to_host16(hdr->frame_control);
5721
f8b1f695
JM
5722 os_memset(&event, 0, sizeof(event));
5723 event.tx_status.type = WLAN_FC_GET_TYPE(fc);
5724 event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
5725 event.tx_status.dst = hdr->addr1;
5726 event.tx_status.data = buf;
5727 event.tx_status.data_len = len;
5728 event.tx_status.ack = ok;
5729 wpa_supplicant_event(ctx, EVENT_TX_STATUS, &event);
0915d02c
JM
5730}
5731
5732
4b9841d3 5733static void from_unknown_sta(struct wpa_driver_nl80211_data *drv,
0d9fc3d8 5734 u8 *buf, size_t len)
0915d02c 5735{
9b90955e
JB
5736 struct ieee80211_hdr *hdr = (void *)buf;
5737 u16 fc;
f8b1f695 5738 union wpa_event_data event;
9b90955e
JB
5739
5740 if (len < sizeof(*hdr))
5741 return;
5742
5743 fc = le_to_host16(hdr->frame_control);
5744
f8b1f695 5745 os_memset(&event, 0, sizeof(event));
9b90955e
JB
5746 event.rx_from_unknown.bssid = get_hdr_bssid(hdr, len);
5747 event.rx_from_unknown.addr = hdr->addr2;
5748 event.rx_from_unknown.wds = (fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) ==
5749 (WLAN_FC_FROMDS | WLAN_FC_TODS);
f8b1f695 5750 wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event);
4b9841d3 5751}
0915d02c 5752
4b9841d3
JM
5753
5754static void handle_frame(struct wpa_driver_nl80211_data *drv,
2a8b7416 5755 u8 *buf, size_t len, int datarate, int ssi_signal)
4b9841d3
JM
5756{
5757 struct ieee80211_hdr *hdr;
f8b1f695
JM
5758 u16 fc;
5759 union wpa_event_data event;
0915d02c
JM
5760
5761 hdr = (struct ieee80211_hdr *) buf;
5762 fc = le_to_host16(hdr->frame_control);
0915d02c 5763
4b9841d3 5764 switch (WLAN_FC_GET_TYPE(fc)) {
0915d02c 5765 case WLAN_FC_TYPE_MGMT:
f8b1f695
JM
5766 os_memset(&event, 0, sizeof(event));
5767 event.rx_mgmt.frame = buf;
5768 event.rx_mgmt.frame_len = len;
2a8b7416
JM
5769 event.rx_mgmt.datarate = datarate;
5770 event.rx_mgmt.ssi_signal = ssi_signal;
f8b1f695 5771 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
0915d02c
JM
5772 break;
5773 case WLAN_FC_TYPE_CTRL:
5774 /* can only get here with PS-Poll frames */
5775 wpa_printf(MSG_DEBUG, "CTRL");
0d9fc3d8 5776 from_unknown_sta(drv, buf, len);
0915d02c
JM
5777 break;
5778 case WLAN_FC_TYPE_DATA:
0d9fc3d8 5779 from_unknown_sta(drv, buf, len);
0915d02c
JM
5780 break;
5781 }
5782}
5783
5784
5785static void handle_monitor_read(int sock, void *eloop_ctx, void *sock_ctx)
5786{
5787 struct wpa_driver_nl80211_data *drv = eloop_ctx;
5788 int len;
5789 unsigned char buf[3000];
5790 struct ieee80211_radiotap_iterator iter;
5791 int ret;
2a8b7416 5792 int datarate = 0, ssi_signal = 0;
4b9841d3 5793 int injected = 0, failed = 0, rxflags = 0;
0915d02c
JM
5794
5795 len = recv(sock, buf, sizeof(buf), 0);
5796 if (len < 0) {
5797 perror("recv");
5798 return;
5799 }
5800
5801 if (ieee80211_radiotap_iterator_init(&iter, (void*)buf, len)) {
5802 printf("received invalid radiotap frame\n");
5803 return;
5804 }
5805
0915d02c
JM
5806 while (1) {
5807 ret = ieee80211_radiotap_iterator_next(&iter);
5808 if (ret == -ENOENT)
5809 break;
5810 if (ret) {
5811 printf("received invalid radiotap frame (%d)\n", ret);
5812 return;
5813 }
5814 switch (iter.this_arg_index) {
5815 case IEEE80211_RADIOTAP_FLAGS:
5816 if (*iter.this_arg & IEEE80211_RADIOTAP_F_FCS)
5817 len -= 4;
5818 break;
5819 case IEEE80211_RADIOTAP_RX_FLAGS:
5820 rxflags = 1;
5821 break;
5822 case IEEE80211_RADIOTAP_TX_FLAGS:
5823 injected = 1;
5824 failed = le_to_host16((*(uint16_t *) iter.this_arg)) &
5825 IEEE80211_RADIOTAP_F_TX_FAIL;
5826 break;
5827 case IEEE80211_RADIOTAP_DATA_RETRIES:
5828 break;
5829 case IEEE80211_RADIOTAP_CHANNEL:
2a8b7416 5830 /* TODO: convert from freq/flags to channel number */
0915d02c
JM
5831 break;
5832 case IEEE80211_RADIOTAP_RATE:
2a8b7416 5833 datarate = *iter.this_arg * 5;
0915d02c
JM
5834 break;
5835 case IEEE80211_RADIOTAP_DB_ANTSIGNAL:
2a8b7416 5836 ssi_signal = *iter.this_arg;
0915d02c
JM
5837 break;
5838 }
5839 }
5840
5841 if (rxflags && injected)
5842 return;
5843
5844 if (!injected)
4b9841d3 5845 handle_frame(drv, buf + iter.max_length,
2a8b7416 5846 len - iter.max_length, datarate, ssi_signal);
0915d02c 5847 else
4b9841d3
JM
5848 handle_tx_callback(drv->ctx, buf + iter.max_length,
5849 len - iter.max_length, !failed);
0915d02c
JM
5850}
5851
5852
5853/*
5854 * we post-process the filter code later and rewrite
5855 * this to the offset to the last instruction
5856 */
5857#define PASS 0xFF
5858#define FAIL 0xFE
5859
5860static struct sock_filter msock_filter_insns[] = {
5861 /*
5862 * do a little-endian load of the radiotap length field
5863 */
5864 /* load lower byte into A */
5865 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 2),
5866 /* put it into X (== index register) */
5867 BPF_STMT(BPF_MISC| BPF_TAX, 0),
5868 /* load upper byte into A */
5869 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 3),
5870 /* left-shift it by 8 */
5871 BPF_STMT(BPF_ALU | BPF_LSH | BPF_K, 8),
5872 /* or with X */
5873 BPF_STMT(BPF_ALU | BPF_OR | BPF_X, 0),
5874 /* put result into X */
5875 BPF_STMT(BPF_MISC| BPF_TAX, 0),
5876
5877 /*
5878 * Allow management frames through, this also gives us those
5879 * management frames that we sent ourselves with status
5880 */
5881 /* load the lower byte of the IEEE 802.11 frame control field */
5882 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
5883 /* mask off frame type and version */
5884 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0xF),
5885 /* accept frame if it's both 0, fall through otherwise */
5886 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0, PASS, 0),
5887
5888 /*
5889 * TODO: add a bit to radiotap RX flags that indicates
5890 * that the sending station is not associated, then
5891 * add a filter here that filters on our DA and that flag
5892 * to allow us to deauth frames to that bad station.
5893 *
65ae1afd 5894 * For now allow all To DS data frames through.
0915d02c 5895 */
65ae1afd
HS
5896 /* load the IEEE 802.11 frame control field */
5897 BPF_STMT(BPF_LD | BPF_H | BPF_IND, 0),
5898 /* mask off frame type, version and DS status */
5899 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0F03),
5900 /* accept frame if version 0, type 2 and To DS, fall through otherwise
5901 */
5902 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0801, PASS, 0),
0915d02c
JM
5903
5904#if 0
5905 /*
fbbfcbac 5906 * drop non-data frames
0915d02c
JM
5907 */
5908 /* load the lower byte of the frame control field */
5909 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
5910 /* mask off QoS bit */
5911 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0c),
5912 /* drop non-data frames */
5913 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 8, 0, FAIL),
fbbfcbac 5914#endif
0915d02c 5915 /* load the upper byte of the frame control field */
fbbfcbac 5916 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 1),
0915d02c
JM
5917 /* mask off toDS/fromDS */
5918 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x03),
fbbfcbac
FF
5919 /* accept WDS frames */
5920 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 3, PASS, 0),
0915d02c
JM
5921
5922 /*
5923 * add header length to index
5924 */
5925 /* load the lower byte of the frame control field */
5926 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
5927 /* mask off QoS bit */
5928 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x80),
5929 /* right shift it by 6 to give 0 or 2 */
5930 BPF_STMT(BPF_ALU | BPF_RSH | BPF_K, 6),
5931 /* add data frame header length */
5932 BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 24),
5933 /* add index, was start of 802.11 header */
5934 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
5935 /* move to index, now start of LL header */
5936 BPF_STMT(BPF_MISC | BPF_TAX, 0),
5937
5938 /*
5939 * Accept empty data frames, we use those for
5940 * polling activity.
5941 */
5942 BPF_STMT(BPF_LD | BPF_W | BPF_LEN, 0),
5943 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_X, 0, PASS, 0),
5944
5945 /*
5946 * Accept EAPOL frames
5947 */
5948 BPF_STMT(BPF_LD | BPF_W | BPF_IND, 0),
5949 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0xAAAA0300, 0, FAIL),
5950 BPF_STMT(BPF_LD | BPF_W | BPF_IND, 4),
5951 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0000888E, PASS, FAIL),
5952
5953 /* keep these last two statements or change the code below */
5954 /* return 0 == "DROP" */
5955 BPF_STMT(BPF_RET | BPF_K, 0),
5956 /* return ~0 == "keep all" */
5957 BPF_STMT(BPF_RET | BPF_K, ~0),
5958};
5959
5960static struct sock_fprog msock_filter = {
5961 .len = sizeof(msock_filter_insns)/sizeof(msock_filter_insns[0]),
5962 .filter = msock_filter_insns,
5963};
5964
5965
5966static int add_monitor_filter(int s)
5967{
5968 int idx;
5969
5970 /* rewrite all PASS/FAIL jump offsets */
5971 for (idx = 0; idx < msock_filter.len; idx++) {
5972 struct sock_filter *insn = &msock_filter_insns[idx];
5973
5974 if (BPF_CLASS(insn->code) == BPF_JMP) {
5975 if (insn->code == (BPF_JMP|BPF_JA)) {
5976 if (insn->k == PASS)
5977 insn->k = msock_filter.len - idx - 2;
5978 else if (insn->k == FAIL)
5979 insn->k = msock_filter.len - idx - 3;
5980 }
5981
5982 if (insn->jt == PASS)
5983 insn->jt = msock_filter.len - idx - 2;
5984 else if (insn->jt == FAIL)
5985 insn->jt = msock_filter.len - idx - 3;
5986
5987 if (insn->jf == PASS)
5988 insn->jf = msock_filter.len - idx - 2;
5989 else if (insn->jf == FAIL)
5990 insn->jf = msock_filter.len - idx - 3;
5991 }
5992 }
5993
5994 if (setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER,
5995 &msock_filter, sizeof(msock_filter))) {
5996 perror("SO_ATTACH_FILTER");
5997 return -1;
5998 }
5999
6000 return 0;
6001}
6002
6003
460456f8
JM
6004static void nl80211_remove_monitor_interface(
6005 struct wpa_driver_nl80211_data *drv)
6006{
3fd1cefb
JB
6007 drv->monitor_refcount--;
6008 if (drv->monitor_refcount > 0)
6009 return;
6010
460456f8
JM
6011 if (drv->monitor_ifidx >= 0) {
6012 nl80211_remove_iface(drv, drv->monitor_ifidx);
6013 drv->monitor_ifidx = -1;
6014 }
504e905c
JM
6015 if (drv->monitor_sock >= 0) {
6016 eloop_unregister_read_sock(drv->monitor_sock);
6017 close(drv->monitor_sock);
6018 drv->monitor_sock = -1;
6019 }
460456f8
JM
6020}
6021
6022
0915d02c
JM
6023static int
6024nl80211_create_monitor_interface(struct wpa_driver_nl80211_data *drv)
6025{
6026 char buf[IFNAMSIZ];
6027 struct sockaddr_ll ll;
6028 int optval;
6029 socklen_t optlen;
0915d02c 6030
3fd1cefb
JB
6031 if (drv->monitor_ifidx >= 0) {
6032 drv->monitor_refcount++;
6033 return 0;
6034 }
6035
6758b167
JJ
6036 if (os_strncmp(drv->first_bss.ifname, "p2p-", 4) == 0) {
6037 /*
6038 * P2P interface name is of the format p2p-%s-%d. For monitor
6039 * interface name corresponding to P2P GO, replace "p2p-" with
6040 * "mon-" to retain the same interface name length and to
6041 * indicate that it is a monitor interface.
6042 */
6043 snprintf(buf, IFNAMSIZ, "mon-%s", drv->first_bss.ifname + 4);
6044 } else {
6045 /* Non-P2P interface with AP functionality. */
6046 snprintf(buf, IFNAMSIZ, "mon.%s", drv->first_bss.ifname);
6047 }
6048
0915d02c
JM
6049 buf[IFNAMSIZ - 1] = '\0';
6050
6051 drv->monitor_ifidx =
fbbfcbac
FF
6052 nl80211_create_iface(drv, buf, NL80211_IFTYPE_MONITOR, NULL,
6053 0);
0915d02c 6054
866af8b6 6055 if (drv->monitor_ifidx == -EOPNOTSUPP) {
61cbe2ff
JB
6056 /*
6057 * This is backward compatibility for a few versions of
6058 * the kernel only that didn't advertise the right
6059 * attributes for the only driver that then supported
6060 * AP mode w/o monitor -- ath6kl.
6061 */
866af8b6
JM
6062 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support "
6063 "monitor interface type - try to run without it");
61cbe2ff 6064 drv->device_ap_sme = 1;
866af8b6
JM
6065 }
6066
0915d02c
JM
6067 if (drv->monitor_ifidx < 0)
6068 return -1;
6069
c81eff1a 6070 if (linux_set_iface_flags(drv->global->ioctl_sock, buf, 1))
0915d02c 6071 goto error;
0915d02c
JM
6072
6073 memset(&ll, 0, sizeof(ll));
6074 ll.sll_family = AF_PACKET;
6075 ll.sll_ifindex = drv->monitor_ifidx;
6076 drv->monitor_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
6077 if (drv->monitor_sock < 0) {
6078 perror("socket[PF_PACKET,SOCK_RAW]");
6079 goto error;
6080 }
6081
6082 if (add_monitor_filter(drv->monitor_sock)) {
6083 wpa_printf(MSG_INFO, "Failed to set socket filter for monitor "
6084 "interface; do filtering in user space");
6085 /* This works, but will cost in performance. */
6086 }
6087
2135f224 6088 if (bind(drv->monitor_sock, (struct sockaddr *) &ll, sizeof(ll)) < 0) {
0915d02c
JM
6089 perror("monitor socket bind");
6090 goto error;
6091 }
6092
6093 optlen = sizeof(optval);
6094 optval = 20;
6095 if (setsockopt
6096 (drv->monitor_sock, SOL_SOCKET, SO_PRIORITY, &optval, optlen)) {
6097 perror("Failed to set socket priority");
6098 goto error;
6099 }
6100
6101 if (eloop_register_read_sock(drv->monitor_sock, handle_monitor_read,
6102 drv, NULL)) {
6103 printf("Could not register monitor read socket\n");
6104 goto error;
6105 }
6106
6107 return 0;
6108 error:
460456f8 6109 nl80211_remove_monitor_interface(drv);
0915d02c
JM
6110 return -1;
6111}
6112
db149ac9 6113
3fd1cefb
JB
6114static int nl80211_setup_ap(struct i802_bss *bss)
6115{
6116 struct wpa_driver_nl80211_data *drv = bss->drv;
6117
36488c05
JM
6118 wpa_printf(MSG_DEBUG, "nl80211: Setup AP - device_ap_sme=%d "
6119 "use_monitor=%d", drv->device_ap_sme, drv->use_monitor);
6120
a11241fa
JB
6121 /*
6122 * Disable Probe Request reporting unless we need it in this way for
6123 * devices that include the AP SME, in the other case (unless using
6124 * monitor iface) we'll get it through the nl_mgmt socket instead.
6125 */
6126 if (!drv->device_ap_sme)
6127 wpa_driver_nl80211_probe_req_report(bss, 0);
6128
6129 if (!drv->device_ap_sme && !drv->use_monitor)
6130 if (nl80211_mgmt_subscribe_ap(bss))
6131 return -1;
6132
a6cc0602
JM
6133 if (drv->device_ap_sme && !drv->use_monitor)
6134 if (nl80211_mgmt_subscribe_ap_dev_sme(bss))
6135 return -1;
6136
a11241fa 6137 if (!drv->device_ap_sme && drv->use_monitor &&
3fd1cefb
JB
6138 nl80211_create_monitor_interface(drv) &&
6139 !drv->device_ap_sme)
6140 return -1;
6141
6142 if (drv->device_ap_sme &&
6143 wpa_driver_nl80211_probe_req_report(bss, 1) < 0) {
6144 wpa_printf(MSG_DEBUG, "nl80211: Failed to enable "
6145 "Probe Request frame reporting in AP mode");
6146 /* Try to survive without this */
6147 }
6148
6149 return 0;
6150}
6151
6152
6153static void nl80211_teardown_ap(struct i802_bss *bss)
6154{
6155 struct wpa_driver_nl80211_data *drv = bss->drv;
6156
a6cc0602 6157 if (drv->device_ap_sme) {
3fd1cefb 6158 wpa_driver_nl80211_probe_req_report(bss, 0);
a6cc0602
JM
6159 if (!drv->use_monitor)
6160 nl80211_mgmt_unsubscribe(bss, "AP teardown (dev SME)");
6161 } else if (drv->use_monitor)
3fd1cefb 6162 nl80211_remove_monitor_interface(drv);
a11241fa 6163 else
36488c05 6164 nl80211_mgmt_unsubscribe(bss, "AP teardown");
a11241fa 6165
3fd1cefb
JB
6166 bss->beacon_set = 0;
6167}
6168
6169
f10bfc9a
JM
6170static int nl80211_send_eapol_data(struct i802_bss *bss,
6171 const u8 *addr, const u8 *data,
d12dab4c 6172 size_t data_len)
f10bfc9a 6173{
d12dab4c
JB
6174 struct sockaddr_ll ll;
6175 int ret;
6176
6177 if (bss->drv->eapol_tx_sock < 0) {
6178 wpa_printf(MSG_DEBUG, "nl80211: No socket to send EAPOL");
f10bfc9a
JM
6179 return -1;
6180 }
6181
d12dab4c
JB
6182 os_memset(&ll, 0, sizeof(ll));
6183 ll.sll_family = AF_PACKET;
6184 ll.sll_ifindex = bss->ifindex;
6185 ll.sll_protocol = htons(ETH_P_PAE);
6186 ll.sll_halen = ETH_ALEN;
6187 os_memcpy(ll.sll_addr, addr, ETH_ALEN);
6188 ret = sendto(bss->drv->eapol_tx_sock, data, data_len, 0,
6189 (struct sockaddr *) &ll, sizeof(ll));
6190 if (ret < 0)
6191 wpa_printf(MSG_ERROR, "nl80211: EAPOL TX: %s",
6192 strerror(errno));
6193
6194 return ret;
f10bfc9a 6195}
5fb1a232 6196
f10bfc9a 6197
db149ac9
JM
6198static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
6199
6200static int wpa_driver_nl80211_hapd_send_eapol(
6201 void *priv, const u8 *addr, const u8 *data,
4378fc14 6202 size_t data_len, int encrypt, const u8 *own_addr, u32 flags)
db149ac9 6203{
a2e40bb6
FF
6204 struct i802_bss *bss = priv;
6205 struct wpa_driver_nl80211_data *drv = bss->drv;
db149ac9
JM
6206 struct ieee80211_hdr *hdr;
6207 size_t len;
6208 u8 *pos;
6209 int res;
4378fc14 6210 int qos = flags & WPA_STA_WMM;
db149ac9 6211
a11241fa 6212 if (drv->device_ap_sme || !drv->use_monitor)
d12dab4c 6213 return nl80211_send_eapol_data(bss, addr, data, data_len);
f10bfc9a 6214
db149ac9
JM
6215 len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
6216 data_len;
6217 hdr = os_zalloc(len);
6218 if (hdr == NULL) {
6219 printf("malloc() failed for i802_send_data(len=%lu)\n",
6220 (unsigned long) len);
6221 return -1;
6222 }
6223
6224 hdr->frame_control =
6225 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
6226 hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
6227 if (encrypt)
6228 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
db149ac9
JM
6229 if (qos) {
6230 hdr->frame_control |=
6231 host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
6232 }
db149ac9
JM
6233
6234 memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
6235 memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
6236 memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
6237 pos = (u8 *) (hdr + 1);
6238
db149ac9
JM
6239 if (qos) {
6240 /* add an empty QoS header if needed */
6241 pos[0] = 0;
6242 pos[1] = 0;
6243 pos += 2;
6244 }
db149ac9
JM
6245
6246 memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
6247 pos += sizeof(rfc1042_header);
6248 WPA_PUT_BE16(pos, ETH_P_PAE);
6249 pos += 2;
6250 memcpy(pos, data, data_len);
6251
55231068
JM
6252 res = wpa_driver_nl80211_send_frame(bss, (u8 *) hdr, len, encrypt, 0,
6253 0, 0, 0, 0);
db149ac9
JM
6254 if (res < 0) {
6255 wpa_printf(MSG_ERROR, "i802_send_eapol - packet len: %lu - "
6256 "failed: %d (%s)",
6257 (unsigned long) len, errno, strerror(errno));
6258 }
7bf12757 6259 os_free(hdr);
db149ac9
JM
6260
6261 return res;
6262}
6263
a8d6ffa4 6264
3234cba4
JM
6265static int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr,
6266 int total_flags,
4c32757d 6267 int flags_or, int flags_and)
a8d6ffa4 6268{
a2e40bb6
FF
6269 struct i802_bss *bss = priv;
6270 struct wpa_driver_nl80211_data *drv = bss->drv;
a8d6ffa4 6271 struct nl_msg *msg, *flags = NULL;
7e76ee9c 6272 struct nl80211_sta_flag_update upd;
a8d6ffa4
JM
6273
6274 msg = nlmsg_alloc();
6275 if (!msg)
6276 return -ENOMEM;
6277
6278 flags = nlmsg_alloc();
6279 if (!flags) {
6280 nlmsg_free(msg);
6281 return -ENOMEM;
6282 }
6283
9fb04070 6284 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
a8d6ffa4
JM
6285
6286 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
3234cba4 6287 if_nametoindex(bss->ifname));
a8d6ffa4
JM
6288 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
6289
7e76ee9c
JM
6290 /*
6291 * Backwards compatibility version using NL80211_ATTR_STA_FLAGS. This
6292 * can be removed eventually.
6293 */
0de39516 6294 if (total_flags & WPA_STA_AUTHORIZED)
a8d6ffa4
JM
6295 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_AUTHORIZED);
6296
0de39516 6297 if (total_flags & WPA_STA_WMM)
a8d6ffa4
JM
6298 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_WME);
6299
0de39516 6300 if (total_flags & WPA_STA_SHORT_PREAMBLE)
a8d6ffa4
JM
6301 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_SHORT_PREAMBLE);
6302
0de39516 6303 if (total_flags & WPA_STA_MFP)
a8d6ffa4
JM
6304 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_MFP);
6305
45b722f1
AN
6306 if (total_flags & WPA_STA_TDLS_PEER)
6307 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_TDLS_PEER);
6308
a8d6ffa4
JM
6309 if (nla_put_nested(msg, NL80211_ATTR_STA_FLAGS, flags))
6310 goto nla_put_failure;
6311
7e76ee9c
JM
6312 os_memset(&upd, 0, sizeof(upd));
6313 upd.mask = sta_flags_nl80211(flags_or | ~flags_and);
6314 upd.set = sta_flags_nl80211(flags_or);
6315 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
6316
a8d6ffa4
JM
6317 nlmsg_free(flags);
6318
6319 return send_and_recv_msgs(drv, msg, NULL, NULL);
6320 nla_put_failure:
5883168a 6321 nlmsg_free(msg);
a8d6ffa4
JM
6322 nlmsg_free(flags);
6323 return -ENOBUFS;
6324}
6325
0915d02c 6326
1581b38b
JM
6327static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv,
6328 struct wpa_driver_associate_params *params)
6329{
b1f625e0
EP
6330 enum nl80211_iftype nlmode;
6331
6332 if (params->p2p) {
046b26a2
JM
6333 wpa_printf(MSG_DEBUG, "nl80211: Setup AP operations for P2P "
6334 "group (GO)");
b1f625e0
EP
6335 nlmode = NL80211_IFTYPE_P2P_GO;
6336 } else
6337 nlmode = NL80211_IFTYPE_AP;
6338
6339 if (wpa_driver_nl80211_set_mode(&drv->first_bss, nlmode) ||
e4fb2167 6340 wpa_driver_nl80211_set_freq(&drv->first_bss, params->freq, 0, 0)) {
460456f8 6341 nl80211_remove_monitor_interface(drv);
1581b38b 6342 return -1;
0915d02c 6343 }
1581b38b 6344
1581b38b
JM
6345 return 0;
6346}
1581b38b
JM
6347
6348
5cc4d64b
JM
6349static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv)
6350{
6351 struct nl_msg *msg;
6352 int ret = -1;
6353
6354 msg = nlmsg_alloc();
6355 if (!msg)
6356 return -1;
6357
9fb04070 6358 nl80211_cmd(drv, msg, 0, NL80211_CMD_LEAVE_IBSS);
5cc4d64b
JM
6359 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
6360 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6361 msg = NULL;
6362 if (ret) {
6363 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS failed: ret=%d "
6364 "(%s)", ret, strerror(-ret));
6365 goto nla_put_failure;
6366 }
6367
6368 ret = 0;
6369 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS request sent successfully");
6370
6371nla_put_failure:
6372 nlmsg_free(msg);
6373 return ret;
6374}
6375
6376
6377static int wpa_driver_nl80211_ibss(struct wpa_driver_nl80211_data *drv,
6378 struct wpa_driver_associate_params *params)
6379{
6380 struct nl_msg *msg;
6381 int ret = -1;
6382 int count = 0;
6383
6384 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS (ifindex=%d)", drv->ifindex);
6385
b1f625e0
EP
6386 if (wpa_driver_nl80211_set_mode(&drv->first_bss,
6387 NL80211_IFTYPE_ADHOC)) {
5cc4d64b
JM
6388 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
6389 "IBSS mode");
6390 return -1;
6391 }
6392
6393retry:
6394 msg = nlmsg_alloc();
6395 if (!msg)
6396 return -1;
6397
9fb04070 6398 nl80211_cmd(drv, msg, 0, NL80211_CMD_JOIN_IBSS);
5cc4d64b
JM
6399 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
6400
6401 if (params->ssid == NULL || params->ssid_len > sizeof(drv->ssid))
6402 goto nla_put_failure;
6403
6404 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
6405 params->ssid, params->ssid_len);
6406 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
6407 params->ssid);
6408 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
6409 drv->ssid_len = params->ssid_len;
6410
6411 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
6412 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
6413
6414 ret = nl80211_set_conn_keys(params, msg);
6415 if (ret)
6416 goto nla_put_failure;
6417
913e3cf7
NC
6418 if (params->bssid && params->fixed_bssid) {
6419 wpa_printf(MSG_DEBUG, " * BSSID=" MACSTR,
6420 MAC2STR(params->bssid));
6421 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
6422 }
6423
e640888c
AQ
6424 if (params->key_mgmt_suite == KEY_MGMT_802_1X ||
6425 params->key_mgmt_suite == KEY_MGMT_PSK ||
6426 params->key_mgmt_suite == KEY_MGMT_802_1X_SHA256 ||
6427 params->key_mgmt_suite == KEY_MGMT_PSK_SHA256) {
6428 wpa_printf(MSG_DEBUG, " * control port");
6429 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT);
6430 }
6431
a95795ad
JM
6432 if (params->wpa_ie) {
6433 wpa_hexdump(MSG_DEBUG,
6434 " * Extra IEs for Beacon/Probe Response frames",
6435 params->wpa_ie, params->wpa_ie_len);
6436 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
6437 params->wpa_ie);
6438 }
6439
5cc4d64b
JM
6440 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6441 msg = NULL;
6442 if (ret) {
6443 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS failed: ret=%d (%s)",
6444 ret, strerror(-ret));
6445 count++;
6446 if (ret == -EALREADY && count == 1) {
6447 wpa_printf(MSG_DEBUG, "nl80211: Retry IBSS join after "
6448 "forced leave");
6449 nl80211_leave_ibss(drv);
6450 nlmsg_free(msg);
6451 goto retry;
6452 }
6453
6454 goto nla_put_failure;
6455 }
6456 ret = 0;
6457 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS request sent successfully");
6458
6459nla_put_failure:
6460 nlmsg_free(msg);
6461 return ret;
6462}
6463
6464
20f5a4c2
JM
6465static unsigned int nl80211_get_assoc_bssid(struct wpa_driver_nl80211_data *drv,
6466 u8 *bssid)
6467{
6468 struct nl_msg *msg;
6469 int ret;
6470 struct nl80211_bss_info_arg arg;
6471
6472 os_memset(&arg, 0, sizeof(arg));
6473 msg = nlmsg_alloc();
6474 if (!msg)
6475 goto nla_put_failure;
6476
9fb04070 6477 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
20f5a4c2
JM
6478 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
6479
6480 arg.drv = drv;
6481 ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
6482 msg = NULL;
6483 if (ret == 0) {
6484 if (is_zero_ether_addr(arg.assoc_bssid))
6485 return -ENOTCONN;
6486 os_memcpy(bssid, arg.assoc_bssid, ETH_ALEN);
6487 return 0;
6488 }
6489 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
6490 "(%s)", ret, strerror(-ret));
6491nla_put_failure:
6492 nlmsg_free(msg);
6493 return drv->assoc_freq;
6494}
6495
6496
6497static int nl80211_disconnect(struct wpa_driver_nl80211_data *drv,
6498 const u8 *bssid)
6499{
6500 u8 addr[ETH_ALEN];
6501
6502 if (bssid == NULL) {
6503 int res = nl80211_get_assoc_bssid(drv, addr);
6504 if (res)
6505 return res;
6506 bssid = addr;
6507 }
6508
6509 return wpa_driver_nl80211_disconnect(drv, bssid,
6510 WLAN_REASON_PREV_AUTH_NOT_VALID);
6511}
6512
6513
cfaab580
ZY
6514static int wpa_driver_nl80211_connect(
6515 struct wpa_driver_nl80211_data *drv,
6516 struct wpa_driver_associate_params *params)
6517{
6518 struct nl_msg *msg;
6519 enum nl80211_auth_type type;
6520 int ret = 0;
3f360238 6521 int algs;
cfaab580
ZY
6522
6523 msg = nlmsg_alloc();
6524 if (!msg)
6525 return -1;
6526
6527 wpa_printf(MSG_DEBUG, "nl80211: Connect (ifindex=%d)", drv->ifindex);
9fb04070 6528 nl80211_cmd(drv, msg, 0, NL80211_CMD_CONNECT);
cfaab580
ZY
6529
6530 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
6531 if (params->bssid) {
6532 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
6533 MAC2STR(params->bssid));
6534 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
6535 }
6536 if (params->freq) {
6537 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
6538 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
6539 }
6540 if (params->ssid) {
6541 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
6542 params->ssid, params->ssid_len);
6543 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
6544 params->ssid);
6545 if (params->ssid_len > sizeof(drv->ssid))
6546 goto nla_put_failure;
6547 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
6548 drv->ssid_len = params->ssid_len;
6549 }
6550 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
6551 if (params->wpa_ie)
6552 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
6553 params->wpa_ie);
6554
3f360238
JM
6555 algs = 0;
6556 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
6557 algs++;
6558 if (params->auth_alg & WPA_AUTH_ALG_SHARED)
6559 algs++;
6560 if (params->auth_alg & WPA_AUTH_ALG_LEAP)
6561 algs++;
6562 if (algs > 1) {
6563 wpa_printf(MSG_DEBUG, " * Leave out Auth Type for automatic "
6564 "selection");
6565 goto skip_auth_type;
6566 }
6567
abd9fafa 6568 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
cfaab580 6569 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
abd9fafa 6570 else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
cfaab580 6571 type = NL80211_AUTHTYPE_SHARED_KEY;
abd9fafa 6572 else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
cfaab580 6573 type = NL80211_AUTHTYPE_NETWORK_EAP;
abd9fafa 6574 else if (params->auth_alg & WPA_AUTH_ALG_FT)
cfaab580
ZY
6575 type = NL80211_AUTHTYPE_FT;
6576 else
6577 goto nla_put_failure;
6578
6579 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
6580 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
6581
3f360238 6582skip_auth_type:
64fa840a
JM
6583 if (params->wpa_proto) {
6584 enum nl80211_wpa_versions ver = 0;
cfaab580 6585
64fa840a
JM
6586 if (params->wpa_proto & WPA_PROTO_WPA)
6587 ver |= NL80211_WPA_VERSION_1;
6588 if (params->wpa_proto & WPA_PROTO_RSN)
6589 ver |= NL80211_WPA_VERSION_2;
cfaab580 6590
64fa840a 6591 wpa_printf(MSG_DEBUG, " * WPA Versions 0x%x", ver);
cfaab580
ZY
6592 NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver);
6593 }
6594
6595 if (params->pairwise_suite != CIPHER_NONE) {
c1bb3e0a 6596 int cipher;
cfaab580
ZY
6597
6598 switch (params->pairwise_suite) {
6599 case CIPHER_WEP40:
6600 cipher = WLAN_CIPHER_SUITE_WEP40;
6601 break;
6602 case CIPHER_WEP104:
6603 cipher = WLAN_CIPHER_SUITE_WEP104;
6604 break;
6605 case CIPHER_CCMP:
6606 cipher = WLAN_CIPHER_SUITE_CCMP;
6607 break;
6608 case CIPHER_TKIP:
6609 default:
6610 cipher = WLAN_CIPHER_SUITE_TKIP;
6611 break;
6612 }
6613 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE, cipher);
6614 }
6615
6616 if (params->group_suite != CIPHER_NONE) {
c1bb3e0a 6617 int cipher;
cfaab580
ZY
6618
6619 switch (params->group_suite) {
6620 case CIPHER_WEP40:
6621 cipher = WLAN_CIPHER_SUITE_WEP40;
6622 break;
6623 case CIPHER_WEP104:
6624 cipher = WLAN_CIPHER_SUITE_WEP104;
6625 break;
6626 case CIPHER_CCMP:
6627 cipher = WLAN_CIPHER_SUITE_CCMP;
6628 break;
6629 case CIPHER_TKIP:
6630 default:
6631 cipher = WLAN_CIPHER_SUITE_TKIP;
6632 break;
6633 }
6634 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher);
6635 }
6636
6637 if (params->key_mgmt_suite == KEY_MGMT_802_1X ||
6638 params->key_mgmt_suite == KEY_MGMT_PSK) {
6639 int mgmt = WLAN_AKM_SUITE_PSK;
6640
6641 switch (params->key_mgmt_suite) {
6642 case KEY_MGMT_802_1X:
6643 mgmt = WLAN_AKM_SUITE_8021X;
6644 break;
6645 case KEY_MGMT_PSK:
6646 default:
6647 mgmt = WLAN_AKM_SUITE_PSK;
6648 break;
6649 }
6650 NLA_PUT_U32(msg, NL80211_ATTR_AKM_SUITES, mgmt);
6651 }
6652
80e8a5ee
BG
6653 if (params->disable_ht)
6654 NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_HT);
6655
6656 if (params->htcaps && params->htcaps_mask) {
6657 int sz = sizeof(struct ieee80211_ht_capabilities);
6658 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY, sz, params->htcaps);
6659 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY_MASK, sz,
6660 params->htcaps_mask);
6661 }
6662
cfaab580
ZY
6663 ret = nl80211_set_conn_keys(params, msg);
6664 if (ret)
6665 goto nla_put_failure;
6666
6667 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6668 msg = NULL;
6669 if (ret) {
6670 wpa_printf(MSG_DEBUG, "nl80211: MLME connect failed: ret=%d "
6671 "(%s)", ret, strerror(-ret));
20f5a4c2
JM
6672 /*
6673 * cfg80211 does not currently accept new connection if we are
6674 * already connected. As a workaround, force disconnection and
6675 * try again once the driver indicates it completed
6676 * disconnection.
6677 */
6678 if (ret == -EALREADY)
6679 nl80211_disconnect(drv, params->bssid);
cfaab580
ZY
6680 goto nla_put_failure;
6681 }
6682 ret = 0;
6683 wpa_printf(MSG_DEBUG, "nl80211: Connect request send successfully");
6684
6685nla_put_failure:
6686 nlmsg_free(msg);
6687 return ret;
6688
6689}
6690
6691
c2a04078
JM
6692static int wpa_driver_nl80211_associate(
6693 void *priv, struct wpa_driver_associate_params *params)
6694{
a2e40bb6
FF
6695 struct i802_bss *bss = priv;
6696 struct wpa_driver_nl80211_data *drv = bss->drv;
c2a04078
JM
6697 int ret = -1;
6698 struct nl_msg *msg;
6699
5cc4d64b 6700 if (params->mode == IEEE80211_MODE_AP)
1581b38b 6701 return wpa_driver_nl80211_ap(drv, params);
1581b38b 6702
5cc4d64b
JM
6703 if (params->mode == IEEE80211_MODE_IBSS)
6704 return wpa_driver_nl80211_ibss(drv, params);
6705
4a867032 6706 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
b1f625e0
EP
6707 enum nl80211_iftype nlmode = params->p2p ?
6708 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
6709
6710 if (wpa_driver_nl80211_set_mode(priv, nlmode) < 0)
4a867032 6711 return -1;
cfaab580 6712 return wpa_driver_nl80211_connect(drv, params);
4a867032 6713 }
cfaab580 6714
c2a04078
JM
6715 drv->associated = 0;
6716
6717 msg = nlmsg_alloc();
6718 if (!msg)
6719 return -1;
6720
6721 wpa_printf(MSG_DEBUG, "nl80211: Associate (ifindex=%d)",
6722 drv->ifindex);
9fb04070 6723 nl80211_cmd(drv, msg, 0, NL80211_CMD_ASSOCIATE);
c2a04078
JM
6724
6725 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
6726 if (params->bssid) {
6727 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
6728 MAC2STR(params->bssid));
6729 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
6730 }
6731 if (params->freq) {
6732 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
6733 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
4832ecd7
JM
6734 drv->assoc_freq = params->freq;
6735 } else
6736 drv->assoc_freq = 0;
c2a04078
JM
6737 if (params->ssid) {
6738 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
6739 params->ssid, params->ssid_len);
6740 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
6741 params->ssid);
fd05d64e
JM
6742 if (params->ssid_len > sizeof(drv->ssid))
6743 goto nla_put_failure;
6744 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
6745 drv->ssid_len = params->ssid_len;
c2a04078
JM
6746 }
6747 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
6748 if (params->wpa_ie)
6749 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
6750 params->wpa_ie);
6751
aca01605
JM
6752 if (params->pairwise_suite != CIPHER_NONE) {
6753 int cipher;
6754
6755 switch (params->pairwise_suite) {
6756 case CIPHER_WEP40:
6757 cipher = WLAN_CIPHER_SUITE_WEP40;
6758 break;
6759 case CIPHER_WEP104:
6760 cipher = WLAN_CIPHER_SUITE_WEP104;
6761 break;
6762 case CIPHER_CCMP:
6763 cipher = WLAN_CIPHER_SUITE_CCMP;
6764 break;
6765 case CIPHER_TKIP:
6766 default:
6767 cipher = WLAN_CIPHER_SUITE_TKIP;
6768 break;
6769 }
79c3124c 6770 wpa_printf(MSG_DEBUG, " * pairwise=0x%x", cipher);
aca01605
JM
6771 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE, cipher);
6772 }
6773
6774 if (params->group_suite != CIPHER_NONE) {
6775 int cipher;
6776
6777 switch (params->group_suite) {
6778 case CIPHER_WEP40:
6779 cipher = WLAN_CIPHER_SUITE_WEP40;
6780 break;
6781 case CIPHER_WEP104:
6782 cipher = WLAN_CIPHER_SUITE_WEP104;
6783 break;
6784 case CIPHER_CCMP:
6785 cipher = WLAN_CIPHER_SUITE_CCMP;
6786 break;
6787 case CIPHER_TKIP:
6788 default:
6789 cipher = WLAN_CIPHER_SUITE_TKIP;
6790 break;
6791 }
79c3124c 6792 wpa_printf(MSG_DEBUG, " * group=0x%x", cipher);
aca01605
JM
6793 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher);
6794 }
6795
e572fa33
JM
6796#ifdef CONFIG_IEEE80211W
6797 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED)
6798 NLA_PUT_U32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED);
6799#endif /* CONFIG_IEEE80211W */
6800
01652550
JM
6801 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT);
6802
62fa124c
JM
6803 if (params->prev_bssid) {
6804 wpa_printf(MSG_DEBUG, " * prev_bssid=" MACSTR,
6805 MAC2STR(params->prev_bssid));
6806 NLA_PUT(msg, NL80211_ATTR_PREV_BSSID, ETH_ALEN,
6807 params->prev_bssid);
6808 }
6809
80e8a5ee
BG
6810 if (params->disable_ht)
6811 NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_HT);
6812
6813 if (params->htcaps && params->htcaps_mask) {
6814 int sz = sizeof(struct ieee80211_ht_capabilities);
6815 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY, sz, params->htcaps);
6816 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY_MASK, sz,
6817 params->htcaps_mask);
6818 }
6819
046b26a2
JM
6820 if (params->p2p)
6821 wpa_printf(MSG_DEBUG, " * P2P group");
6822
c2a04078
JM
6823 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6824 msg = NULL;
6825 if (ret) {
3b7ea880
BG
6826 wpa_dbg(drv->ctx, MSG_DEBUG,
6827 "nl80211: MLME command failed (assoc): ret=%d (%s)",
6828 ret, strerror(-ret));
8856462d 6829 nl80211_dump_scan(drv);
c2a04078
JM
6830 goto nla_put_failure;
6831 }
6832 ret = 0;
6833 wpa_printf(MSG_DEBUG, "nl80211: Association request send "
6834 "successfully");
6835
6836nla_put_failure:
6837 nlmsg_free(msg);
6838 return ret;
6839}
3f5285e8
JM
6840
6841
ad1e68e6 6842static int nl80211_set_mode(struct wpa_driver_nl80211_data *drv,
a1922f93 6843 int ifindex, enum nl80211_iftype mode)
3f5285e8 6844{
3f5285e8 6845 struct nl_msg *msg;
ad1e68e6
JM
6846 int ret = -ENOBUFS;
6847
a1922f93
JM
6848 wpa_printf(MSG_DEBUG, "nl80211: Set mode ifindex %d iftype %d (%s)",
6849 ifindex, mode, nl80211_iftype_str(mode));
6850
ad1e68e6
JM
6851 msg = nlmsg_alloc();
6852 if (!msg)
6853 return -ENOMEM;
6854
9fb04070 6855 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_INTERFACE);
ad1e68e6
JM
6856 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
6857 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, mode);
6858
6859 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5883168a 6860 msg = NULL;
ad1e68e6
JM
6861 if (!ret)
6862 return 0;
6863nla_put_failure:
5883168a 6864 nlmsg_free(msg);
ad1e68e6
JM
6865 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface %d to mode %d:"
6866 " %d (%s)", ifindex, mode, ret, strerror(-ret));
6867 return ret;
6868}
6869
6870
b1f625e0
EP
6871static int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
6872 enum nl80211_iftype nlmode)
ad1e68e6 6873{
a2e40bb6 6874 struct wpa_driver_nl80211_data *drv = bss->drv;
ad1e68e6 6875 int ret = -1;
26af9dca 6876 int i;
86957e62 6877 int was_ap = is_ap_interface(drv->nlmode);
671a5039 6878 int res;
1581b38b 6879
671a5039
JM
6880 res = nl80211_set_mode(drv, drv->ifindex, nlmode);
6881 if (res == 0) {
ad1e68e6 6882 drv->nlmode = nlmode;
460456f8
JM
6883 ret = 0;
6884 goto done;
ad1e68e6 6885 }
3f5285e8 6886
671a5039
JM
6887 if (res == -ENODEV)
6888 return -1;
6889
460456f8 6890 if (nlmode == drv->nlmode) {
c6e8e8e4
JM
6891 wpa_printf(MSG_DEBUG, "nl80211: Interface already in "
6892 "requested mode - ignore error");
460456f8
JM
6893 ret = 0;
6894 goto done; /* Already in the requested mode */
6895 }
3f5285e8 6896
3f5285e8
JM
6897 /* mac80211 doesn't allow mode changes while the device is up, so
6898 * take the device down, try to set the mode again, and bring the
6899 * device back up.
6900 */
26af9dca
JM
6901 wpa_printf(MSG_DEBUG, "nl80211: Try mode change after setting "
6902 "interface down");
6903 for (i = 0; i < 10; i++) {
c81eff1a
BG
6904 res = linux_set_iface_flags(drv->global->ioctl_sock,
6905 bss->ifname, 0);
6e8183d7
JM
6906 if (res == -EACCES || res == -ENODEV)
6907 break;
6908 if (res == 0) {
26af9dca
JM
6909 /* Try to set the mode again while the interface is
6910 * down */
6911 ret = nl80211_set_mode(drv, drv->ifindex, nlmode);
6e8183d7
JM
6912 if (ret == -EACCES)
6913 break;
c81eff1a 6914 res = linux_set_iface_flags(drv->global->ioctl_sock,
6e8183d7
JM
6915 bss->ifname, 1);
6916 if (res && !ret)
26af9dca 6917 ret = -1;
6e8183d7 6918 else if (ret != -EBUSY)
26af9dca
JM
6919 break;
6920 } else
6921 wpa_printf(MSG_DEBUG, "nl80211: Failed to set "
6922 "interface down");
6923 os_sleep(0, 100000);
3f5285e8
JM
6924 }
6925
c6e8e8e4
JM
6926 if (!ret) {
6927 wpa_printf(MSG_DEBUG, "nl80211: Mode change succeeded while "
6928 "interface is down");
ad1e68e6 6929 drv->nlmode = nlmode;
7d9c3698 6930 drv->ignore_if_down_event = 1;
c6e8e8e4 6931 }
ad1e68e6 6932
460456f8 6933done:
3fd1cefb
JB
6934 if (ret) {
6935 wpa_printf(MSG_DEBUG, "nl80211: Interface mode change to %d "
6936 "from %d failed", nlmode, drv->nlmode);
6937 return ret;
6938 }
6939
6940 if (is_ap_interface(nlmode)) {
36488c05 6941 nl80211_mgmt_unsubscribe(bss, "start AP");
460456f8 6942 /* Setup additional AP mode functionality if needed */
3fd1cefb 6943 if (nl80211_setup_ap(bss))
460456f8 6944 return -1;
3fd1cefb 6945 } else if (was_ap) {
460456f8 6946 /* Remove additional AP mode functionality */
3fd1cefb 6947 nl80211_teardown_ap(bss);
a11241fa 6948 } else {
36488c05 6949 nl80211_mgmt_unsubscribe(bss, "mode change");
460456f8 6950 }
460456f8 6951
a11241fa
JB
6952 if (!is_ap_interface(nlmode) &&
6953 nl80211_mgmt_subscribe_non_ap(bss) < 0)
6954 wpa_printf(MSG_DEBUG, "nl80211: Failed to register Action "
6955 "frame processing - ignore for now");
08359050 6956
3fd1cefb 6957 return 0;
3f5285e8
JM
6958}
6959
6960
3f5285e8
JM
6961static int wpa_driver_nl80211_get_capa(void *priv,
6962 struct wpa_driver_capa *capa)
6963{
a2e40bb6
FF
6964 struct i802_bss *bss = priv;
6965 struct wpa_driver_nl80211_data *drv = bss->drv;
3f5285e8
JM
6966 if (!drv->has_capability)
6967 return -1;
6968 os_memcpy(capa, &drv->capa, sizeof(*capa));
6969 return 0;
6970}
6971
6972
6973static int wpa_driver_nl80211_set_operstate(void *priv, int state)
6974{
a2e40bb6
FF
6975 struct i802_bss *bss = priv;
6976 struct wpa_driver_nl80211_data *drv = bss->drv;
3f5285e8
JM
6977
6978 wpa_printf(MSG_DEBUG, "%s: operstate %d->%d (%s)",
6979 __func__, drv->operstate, state, state ? "UP" : "DORMANT");
6980 drv->operstate = state;
36d84860 6981 return netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, -1,
e2d02c29 6982 state ? IF_OPER_UP : IF_OPER_DORMANT);
3f5285e8
JM
6983}
6984
01652550
JM
6985
6986static int wpa_driver_nl80211_set_supp_port(void *priv, int authorized)
6987{
a2e40bb6
FF
6988 struct i802_bss *bss = priv;
6989 struct wpa_driver_nl80211_data *drv = bss->drv;
01652550
JM
6990 struct nl_msg *msg;
6991 struct nl80211_sta_flag_update upd;
6992
6993 msg = nlmsg_alloc();
6994 if (!msg)
6995 return -ENOMEM;
6996
9fb04070 6997 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
01652550
JM
6998
6999 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
a2e40bb6 7000 if_nametoindex(bss->ifname));
01652550
JM
7001 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid);
7002
7003 os_memset(&upd, 0, sizeof(upd));
7004 upd.mask = BIT(NL80211_STA_FLAG_AUTHORIZED);
7005 if (authorized)
7006 upd.set = BIT(NL80211_STA_FLAG_AUTHORIZED);
7007 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
7008
7009 return send_and_recv_msgs(drv, msg, NULL, NULL);
7010 nla_put_failure:
5883168a 7011 nlmsg_free(msg);
01652550
JM
7012 return -ENOBUFS;
7013}
7014
3f5285e8 7015
f07ead6a
JM
7016/* Set kernel driver on given frequency (MHz) */
7017static int i802_set_freq(void *priv, struct hostapd_freq_params *freq)
c5121837 7018{
f07ead6a 7019 struct i802_bss *bss = priv;
e4fb2167 7020 return wpa_driver_nl80211_set_freq(bss, freq->freq, freq->ht_enabled,
f07ead6a 7021 freq->sec_channel_offset);
c5121837
JM
7022}
7023
f7b3920c
JM
7024
7025#if defined(HOSTAPD) || defined(CONFIG_AP)
c5121837 7026
c5121837
JM
7027static inline int min_int(int a, int b)
7028{
7029 if (a < b)
7030 return a;
7031 return b;
7032}
7033
7034
7035static int get_key_handler(struct nl_msg *msg, void *arg)
7036{
7037 struct nlattr *tb[NL80211_ATTR_MAX + 1];
7038 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
7039
7040 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
7041 genlmsg_attrlen(gnlh, 0), NULL);
7042
7043 /*
7044 * TODO: validate the key index and mac address!
7045 * Otherwise, there's a race condition as soon as
7046 * the kernel starts sending key notifications.
7047 */
7048
7049 if (tb[NL80211_ATTR_KEY_SEQ])
7050 memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]),
7051 min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6));
7052 return NL_SKIP;
7053}
7054
7055
7056static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
7057 int idx, u8 *seq)
7058{
a2e40bb6
FF
7059 struct i802_bss *bss = priv;
7060 struct wpa_driver_nl80211_data *drv = bss->drv;
c5121837
JM
7061 struct nl_msg *msg;
7062
7063 msg = nlmsg_alloc();
7064 if (!msg)
7065 return -ENOMEM;
7066
9fb04070 7067 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_KEY);
c5121837
JM
7068
7069 if (addr)
7070 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
7071 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, idx);
7072 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
7073
7074 memset(seq, 0, 6);
7075
7076 return send_and_recv_msgs(drv, msg, get_key_handler, seq);
7077 nla_put_failure:
9e088e74 7078 nlmsg_free(msg);
c5121837
JM
7079 return -ENOBUFS;
7080}
7081
7082
c5121837
JM
7083static int i802_set_rts(void *priv, int rts)
7084{
a2e40bb6
FF
7085 struct i802_bss *bss = priv;
7086 struct wpa_driver_nl80211_data *drv = bss->drv;
ad649451
JM
7087 struct nl_msg *msg;
7088 int ret = -ENOBUFS;
7089 u32 val;
c5121837 7090
ad649451
JM
7091 msg = nlmsg_alloc();
7092 if (!msg)
7093 return -ENOMEM;
c5121837 7094
ad649451
JM
7095 if (rts >= 2347)
7096 val = (u32) -1;
7097 else
7098 val = rts;
c5121837 7099
9fb04070 7100 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
ad649451
JM
7101 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
7102 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD, val);
7103
7104 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5883168a 7105 msg = NULL;
ad649451
JM
7106 if (!ret)
7107 return 0;
7108nla_put_failure:
5883168a 7109 nlmsg_free(msg);
ad649451
JM
7110 wpa_printf(MSG_DEBUG, "nl80211: Failed to set RTS threshold %d: "
7111 "%d (%s)", rts, ret, strerror(-ret));
7112 return ret;
c5121837
JM
7113}
7114
7115
7116static int i802_set_frag(void *priv, int frag)
7117{
a2e40bb6
FF
7118 struct i802_bss *bss = priv;
7119 struct wpa_driver_nl80211_data *drv = bss->drv;
ad649451
JM
7120 struct nl_msg *msg;
7121 int ret = -ENOBUFS;
7122 u32 val;
c5121837 7123
ad649451
JM
7124 msg = nlmsg_alloc();
7125 if (!msg)
7126 return -ENOMEM;
c5121837 7127
ad649451
JM
7128 if (frag >= 2346)
7129 val = (u32) -1;
7130 else
7131 val = frag;
c5121837 7132
9fb04070 7133 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
ad649451
JM
7134 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
7135 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD, val);
7136
7137 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5883168a 7138 msg = NULL;
ad649451
JM
7139 if (!ret)
7140 return 0;
7141nla_put_failure:
5883168a 7142 nlmsg_free(msg);
ad649451
JM
7143 wpa_printf(MSG_DEBUG, "nl80211: Failed to set fragmentation threshold "
7144 "%d: %d (%s)", frag, ret, strerror(-ret));
7145 return ret;
c5121837
JM
7146}
7147
7148
c5121837
JM
7149static int i802_flush(void *priv)
7150{
a2e40bb6
FF
7151 struct i802_bss *bss = priv;
7152 struct wpa_driver_nl80211_data *drv = bss->drv;
c5121837 7153 struct nl_msg *msg;
82554b10 7154 int res;
c5121837
JM
7155
7156 msg = nlmsg_alloc();
7157 if (!msg)
7158 return -1;
7159
9fb04070 7160 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_STATION);
c5121837
JM
7161
7162 /*
7163 * XXX: FIX! this needs to flush all VLANs too
7164 */
7165 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
a2e40bb6 7166 if_nametoindex(bss->ifname));
c5121837 7167
82554b10
JM
7168 res = send_and_recv_msgs(drv, msg, NULL, NULL);
7169 if (res) {
7170 wpa_printf(MSG_DEBUG, "nl80211: Station flush failed: ret=%d "
7171 "(%s)", res, strerror(-res));
7172 }
7173 return res;
c5121837 7174 nla_put_failure:
9e088e74 7175 nlmsg_free(msg);
c5121837
JM
7176 return -ENOBUFS;
7177}
7178
7179
7180static int get_sta_handler(struct nl_msg *msg, void *arg)
7181{
7182 struct nlattr *tb[NL80211_ATTR_MAX + 1];
7183 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
7184 struct hostap_sta_driver_data *data = arg;
7185 struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
7186 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
7187 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
7188 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
7189 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
7190 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
7191 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
7192 };
7193
7194 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
7195 genlmsg_attrlen(gnlh, 0), NULL);
7196
7197 /*
7198 * TODO: validate the interface and mac address!
7199 * Otherwise, there's a race condition as soon as
7200 * the kernel starts sending station notifications.
7201 */
7202
7203 if (!tb[NL80211_ATTR_STA_INFO]) {
7204 wpa_printf(MSG_DEBUG, "sta stats missing!");
7205 return NL_SKIP;
7206 }
7207 if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
7208 tb[NL80211_ATTR_STA_INFO],
7209 stats_policy)) {
7210 wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
7211 return NL_SKIP;
7212 }
7213
7214 if (stats[NL80211_STA_INFO_INACTIVE_TIME])
7215 data->inactive_msec =
7216 nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
7217 if (stats[NL80211_STA_INFO_RX_BYTES])
7218 data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
7219 if (stats[NL80211_STA_INFO_TX_BYTES])
7220 data->tx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
7221 if (stats[NL80211_STA_INFO_RX_PACKETS])
7222 data->rx_packets =
7223 nla_get_u32(stats[NL80211_STA_INFO_RX_PACKETS]);
7224 if (stats[NL80211_STA_INFO_TX_PACKETS])
7225 data->tx_packets =
7226 nla_get_u32(stats[NL80211_STA_INFO_TX_PACKETS]);
7227
7228 return NL_SKIP;
7229}
7230
7231static int i802_read_sta_data(void *priv, struct hostap_sta_driver_data *data,
7232 const u8 *addr)
7233{
a2e40bb6
FF
7234 struct i802_bss *bss = priv;
7235 struct wpa_driver_nl80211_data *drv = bss->drv;
c5121837
JM
7236 struct nl_msg *msg;
7237
7238 os_memset(data, 0, sizeof(*data));
7239 msg = nlmsg_alloc();
7240 if (!msg)
7241 return -ENOMEM;
7242
9fb04070 7243 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_STATION);
c5121837
JM
7244
7245 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
a2e40bb6 7246 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
c5121837
JM
7247
7248 return send_and_recv_msgs(drv, msg, get_sta_handler, data);
7249 nla_put_failure:
9e088e74 7250 nlmsg_free(msg);
c5121837
JM
7251 return -ENOBUFS;
7252}
7253
7254
c5121837
JM
7255static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
7256 int cw_min, int cw_max, int burst_time)
7257{
a2e40bb6
FF
7258 struct i802_bss *bss = priv;
7259 struct wpa_driver_nl80211_data *drv = bss->drv;
c5121837
JM
7260 struct nl_msg *msg;
7261 struct nlattr *txq, *params;
7262
7263 msg = nlmsg_alloc();
7264 if (!msg)
7265 return -1;
7266
9fb04070 7267 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
c5121837 7268
a2e40bb6 7269 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
c5121837
JM
7270
7271 txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS);
7272 if (!txq)
7273 goto nla_put_failure;
7274
7275 /* We are only sending parameters for a single TXQ at a time */
7276 params = nla_nest_start(msg, 1);
7277 if (!params)
7278 goto nla_put_failure;
7279
7e3c1781
JM
7280 switch (queue) {
7281 case 0:
7282 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VO);
7283 break;
7284 case 1:
7285 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VI);
7286 break;
7287 case 2:
7288 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BE);
7289 break;
7290 case 3:
7291 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BK);
7292 break;
7293 }
c5121837
JM
7294 /* Burst time is configured in units of 0.1 msec and TXOP parameter in
7295 * 32 usec, so need to convert the value here. */
7296 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_TXOP, (burst_time * 100 + 16) / 32);
7297 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min);
7298 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max);
7299 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_AIFS, aifs);
7300
7301 nla_nest_end(msg, params);
7302
7303 nla_nest_end(msg, txq);
7304
7305 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
7306 return 0;
9e088e74 7307 msg = NULL;
c5121837 7308 nla_put_failure:
9e088e74 7309 nlmsg_free(msg);
c5121837
JM
7310 return -1;
7311}
7312
7313
c5121837
JM
7314static int i802_set_sta_vlan(void *priv, const u8 *addr,
7315 const char *ifname, int vlan_id)
7316{
a2e40bb6
FF
7317 struct i802_bss *bss = priv;
7318 struct wpa_driver_nl80211_data *drv = bss->drv;
c5121837 7319 struct nl_msg *msg;
cd1d72c1 7320 int ret = -ENOBUFS;
c5121837
JM
7321
7322 msg = nlmsg_alloc();
7323 if (!msg)
7324 return -ENOMEM;
7325
9fb04070 7326 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
c5121837
JM
7327
7328 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
a2e40bb6 7329 if_nametoindex(bss->ifname));
c5121837 7330 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
1c766b09 7331 NLA_PUT_U32(msg, NL80211_ATTR_STA_VLAN,
c5121837
JM
7332 if_nametoindex(ifname));
7333
cd1d72c1 7334 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9e088e74 7335 msg = NULL;
cd1d72c1
JM
7336 if (ret < 0) {
7337 wpa_printf(MSG_ERROR, "nl80211: NL80211_ATTR_STA_VLAN (addr="
7338 MACSTR " ifname=%s vlan_id=%d) failed: %d (%s)",
7339 MAC2STR(addr), ifname, vlan_id, ret,
7340 strerror(-ret));
7341 }
c5121837 7342 nla_put_failure:
9e088e74 7343 nlmsg_free(msg);
cd1d72c1 7344 return ret;
c5121837
JM
7345}
7346
fbbfcbac 7347
c5121837
JM
7348static int i802_get_inact_sec(void *priv, const u8 *addr)
7349{
7350 struct hostap_sta_driver_data data;
7351 int ret;
7352
7353 data.inactive_msec = (unsigned long) -1;
7354 ret = i802_read_sta_data(priv, &data, addr);
7355 if (ret || data.inactive_msec == (unsigned long) -1)
7356 return -1;
7357 return data.inactive_msec / 1000;
7358}
7359
7360
7361static int i802_sta_clear_stats(void *priv, const u8 *addr)
7362{
7363#if 0
7364 /* TODO */
7365#endif
7366 return 0;
7367}
7368
7369
731723a5
JM
7370static int i802_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
7371 int reason)
c5121837 7372{
a2e40bb6 7373 struct i802_bss *bss = priv;
c5121837
JM
7374 struct ieee80211_mgmt mgmt;
7375
7376 memset(&mgmt, 0, sizeof(mgmt));
7377 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
7378 WLAN_FC_STYPE_DEAUTH);
7379 memcpy(mgmt.da, addr, ETH_ALEN);
731723a5
JM
7380 memcpy(mgmt.sa, own_addr, ETH_ALEN);
7381 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
c5121837 7382 mgmt.u.deauth.reason_code = host_to_le16(reason);
a2e40bb6 7383 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
9f324b61 7384 IEEE80211_HDRLEN +
8cfa3527 7385 sizeof(mgmt.u.deauth), 0);
c5121837
JM
7386}
7387
7388
731723a5
JM
7389static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
7390 int reason)
c5121837 7391{
a2e40bb6 7392 struct i802_bss *bss = priv;
c5121837
JM
7393 struct ieee80211_mgmt mgmt;
7394
7395 memset(&mgmt, 0, sizeof(mgmt));
7396 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
7397 WLAN_FC_STYPE_DISASSOC);
7398 memcpy(mgmt.da, addr, ETH_ALEN);
731723a5
JM
7399 memcpy(mgmt.sa, own_addr, ETH_ALEN);
7400 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
c5121837 7401 mgmt.u.disassoc.reason_code = host_to_le16(reason);
a2e40bb6 7402 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
9f324b61 7403 IEEE80211_HDRLEN +
8cfa3527 7404 sizeof(mgmt.u.disassoc), 0);
c5121837
JM
7405}
7406
ee7ab173
JB
7407#endif /* HOSTAPD || CONFIG_AP */
7408
7409#ifdef HOSTAPD
c5121837 7410
f07ead6a
JM
7411static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
7412{
7413 int i;
7414 int *old;
7415
7416 wpa_printf(MSG_DEBUG, "nl80211: Add own interface ifindex %d",
7417 ifidx);
7418 for (i = 0; i < drv->num_if_indices; i++) {
7419 if (drv->if_indices[i] == 0) {
7420 drv->if_indices[i] = ifidx;
7421 return;
7422 }
7423 }
7424
7425 if (drv->if_indices != drv->default_if_indices)
7426 old = drv->if_indices;
7427 else
7428 old = NULL;
7429
7430 drv->if_indices = os_realloc(old,
7431 sizeof(int) * (drv->num_if_indices + 1));
7432 if (!drv->if_indices) {
7433 if (!old)
7434 drv->if_indices = drv->default_if_indices;
7435 else
7436 drv->if_indices = old;
7437 wpa_printf(MSG_ERROR, "Failed to reallocate memory for "
7438 "interfaces");
7439 wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx);
7440 return;
7441 } else if (!old)
7442 os_memcpy(drv->if_indices, drv->default_if_indices,
7443 sizeof(drv->default_if_indices));
7444 drv->if_indices[drv->num_if_indices] = ifidx;
7445 drv->num_if_indices++;
7446}
7447
7448
7449static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
7450{
7451 int i;
7452
7453 for (i = 0; i < drv->num_if_indices; i++) {
7454 if (drv->if_indices[i] == ifidx) {
7455 drv->if_indices[i] = 0;
7456 break;
7457 }
7458 }
7459}
7460
7461
7462static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
7463{
7464 int i;
7465
7466 for (i = 0; i < drv->num_if_indices; i++)
7467 if (drv->if_indices[i] == ifidx)
7468 return 1;
7469
7470 return 0;
7471}
7472
7473
7474static int i802_set_wds_sta(void *priv, const u8 *addr, int aid, int val,
7475 const char *bridge_ifname)
7476{
7477 struct i802_bss *bss = priv;
7478 struct wpa_driver_nl80211_data *drv = bss->drv;
7479 char name[IFNAMSIZ + 1];
7480
7481 os_snprintf(name, sizeof(name), "%s.sta%d", bss->ifname, aid);
7482 wpa_printf(MSG_DEBUG, "nl80211: Set WDS STA addr=" MACSTR
7483 " aid=%d val=%d name=%s", MAC2STR(addr), aid, val, name);
7484 if (val) {
7485 if (!if_nametoindex(name)) {
7486 if (nl80211_create_iface(drv, name,
7487 NL80211_IFTYPE_AP_VLAN,
7488 NULL, 1) < 0)
7489 return -1;
7490 if (bridge_ifname &&
c81eff1a
BG
7491 linux_br_add_if(drv->global->ioctl_sock,
7492 bridge_ifname, name) < 0)
f07ead6a
JM
7493 return -1;
7494 }
c81eff1a 7495 linux_set_iface_flags(drv->global->ioctl_sock, name, 1);
f07ead6a
JM
7496 return i802_set_sta_vlan(priv, addr, name, 0);
7497 } else {
7498 i802_set_sta_vlan(priv, addr, bss->ifname, 0);
7499 return wpa_driver_nl80211_if_remove(priv, WPA_IF_AP_VLAN,
7500 name);
7501 }
7502}
7503
7504
7505static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
7506{
7507 struct wpa_driver_nl80211_data *drv = eloop_ctx;
7508 struct sockaddr_ll lladdr;
7509 unsigned char buf[3000];
7510 int len;
7511 socklen_t fromlen = sizeof(lladdr);
7512
7513 len = recvfrom(sock, buf, sizeof(buf), 0,
7514 (struct sockaddr *)&lladdr, &fromlen);
7515 if (len < 0) {
7516 perror("recv");
7517 return;
7518 }
7519
7520 if (have_ifidx(drv, lladdr.sll_ifindex))
7521 drv_event_eapol_rx(drv->ctx, lladdr.sll_addr, buf, len);
7522}
7523
7524
94627f6c 7525static int i802_check_bridge(struct wpa_driver_nl80211_data *drv,
e17a2477 7526 struct i802_bss *bss,
94627f6c
JM
7527 const char *brname, const char *ifname)
7528{
7529 int ifindex;
7530 char in_br[IFNAMSIZ];
7531
e17a2477 7532 os_strlcpy(bss->brname, brname, IFNAMSIZ);
94627f6c
JM
7533 ifindex = if_nametoindex(brname);
7534 if (ifindex == 0) {
7535 /*
7536 * Bridge was configured, but the bridge device does
7537 * not exist. Try to add it now.
7538 */
c81eff1a 7539 if (linux_br_add(drv->global->ioctl_sock, brname) < 0) {
94627f6c
JM
7540 wpa_printf(MSG_ERROR, "nl80211: Failed to add the "
7541 "bridge interface %s: %s",
7542 brname, strerror(errno));
7543 return -1;
7544 }
e17a2477 7545 bss->added_bridge = 1;
94627f6c
JM
7546 add_ifidx(drv, if_nametoindex(brname));
7547 }
7548
7549 if (linux_br_get(in_br, ifname) == 0) {
7550 if (os_strcmp(in_br, brname) == 0)
7551 return 0; /* already in the bridge */
7552
7553 wpa_printf(MSG_DEBUG, "nl80211: Removing interface %s from "
7554 "bridge %s", ifname, in_br);
c81eff1a
BG
7555 if (linux_br_del_if(drv->global->ioctl_sock, in_br, ifname) <
7556 0) {
94627f6c
JM
7557 wpa_printf(MSG_ERROR, "nl80211: Failed to "
7558 "remove interface %s from bridge "
7559 "%s: %s",
7560 ifname, brname, strerror(errno));
7561 return -1;
7562 }
7563 }
7564
7565 wpa_printf(MSG_DEBUG, "nl80211: Adding interface %s into bridge %s",
7566 ifname, brname);
c81eff1a 7567 if (linux_br_add_if(drv->global->ioctl_sock, brname, ifname) < 0) {
94627f6c
JM
7568 wpa_printf(MSG_ERROR, "nl80211: Failed to add interface %s "
7569 "into bridge %s: %s",
7570 ifname, brname, strerror(errno));
7571 return -1;
7572 }
e17a2477 7573 bss->added_if_into_bridge = 1;
94627f6c
JM
7574
7575 return 0;
7576}
7577
7578
92f475b4
JM
7579static void *i802_init(struct hostapd_data *hapd,
7580 struct wpa_init_params *params)
c5121837
JM
7581{
7582 struct wpa_driver_nl80211_data *drv;
a2e40bb6 7583 struct i802_bss *bss;
c5121837 7584 size_t i;
94627f6c
JM
7585 char brname[IFNAMSIZ];
7586 int ifindex, br_ifindex;
7587 int br_added = 0;
c5121837 7588
4b24282a
JM
7589 bss = wpa_driver_nl80211_init(hapd, params->ifname,
7590 params->global_priv);
a2e40bb6 7591 if (bss == NULL)
c5121837 7592 return NULL;
c5121837 7593
a2e40bb6 7594 drv = bss->drv;
0382097e 7595 drv->nlmode = NL80211_IFTYPE_AP;
7635bfb0
JB
7596 drv->eapol_sock = -1;
7597
94627f6c
JM
7598 if (linux_br_get(brname, params->ifname) == 0) {
7599 wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in bridge %s",
7600 params->ifname, brname);
7601 br_ifindex = if_nametoindex(brname);
7602 } else {
7603 brname[0] = '\0';
7604 br_ifindex = 0;
7605 }
7606
c5121837
JM
7607 drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int);
7608 drv->if_indices = drv->default_if_indices;
92f475b4 7609 for (i = 0; i < params->num_bridge; i++) {
94627f6c
JM
7610 if (params->bridge[i]) {
7611 ifindex = if_nametoindex(params->bridge[i]);
7612 if (ifindex)
7613 add_ifidx(drv, ifindex);
7614 if (ifindex == br_ifindex)
7615 br_added = 1;
7616 }
c5121837 7617 }
94627f6c
JM
7618 if (!br_added && br_ifindex &&
7619 (params->num_bridge == 0 || !params->bridge[0]))
7620 add_ifidx(drv, br_ifindex);
c5121837 7621
ad1e68e6
JM
7622 /* start listening for EAPOL on the default AP interface */
7623 add_ifidx(drv, drv->ifindex);
7624
c81eff1a 7625 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0))
6980c191 7626 goto failed;
ad1e68e6 7627
6980c191 7628 if (params->bssid) {
c81eff1a 7629 if (linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
2ac9688e 7630 params->bssid))
460456f8
JM
7631 goto failed;
7632 }
ad1e68e6 7633
b1f625e0 7634 if (wpa_driver_nl80211_set_mode(bss, drv->nlmode)) {
ad1e68e6 7635 wpa_printf(MSG_ERROR, "nl80211: Failed to set interface %s "
a2e40bb6 7636 "into AP mode", bss->ifname);
bbaf0837 7637 goto failed;
ad1e68e6
JM
7638 }
7639
94627f6c 7640 if (params->num_bridge && params->bridge[0] &&
e17a2477 7641 i802_check_bridge(drv, bss, params->bridge[0], params->ifname) < 0)
94627f6c
JM
7642 goto failed;
7643
c81eff1a 7644 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1))
bbaf0837 7645 goto failed;
ad1e68e6
JM
7646
7647 drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
7648 if (drv->eapol_sock < 0) {
7649 perror("socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE)");
bbaf0837 7650 goto failed;
ad1e68e6
JM
7651 }
7652
7653 if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
7654 {
7655 printf("Could not register read socket for eapol\n");
c5121837 7656 goto failed;
ad1e68e6
JM
7657 }
7658
c81eff1a
BG
7659 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
7660 params->own_addr))
bbaf0837 7661 goto failed;
c5121837 7662
341eebee
JB
7663 memcpy(bss->addr, params->own_addr, ETH_ALEN);
7664
a2e40bb6 7665 return bss;
c5121837
JM
7666
7667failed:
7635bfb0 7668 wpa_driver_nl80211_deinit(bss);
bbaf0837
JM
7669 return NULL;
7670}
c5121837 7671
c5121837 7672
bbaf0837
JM
7673static void i802_deinit(void *priv)
7674{
7675 wpa_driver_nl80211_deinit(priv);
c5121837
JM
7676}
7677
7678#endif /* HOSTAPD */
7679
7680
22a7c9d7
JM
7681static enum nl80211_iftype wpa_driver_nl80211_if_type(
7682 enum wpa_driver_if_type type)
7683{
7684 switch (type) {
7685 case WPA_IF_STATION:
9f51b113 7686 return NL80211_IFTYPE_STATION;
75bde05d
JM
7687 case WPA_IF_P2P_CLIENT:
7688 case WPA_IF_P2P_GROUP:
9f51b113 7689 return NL80211_IFTYPE_P2P_CLIENT;
22a7c9d7
JM
7690 case WPA_IF_AP_VLAN:
7691 return NL80211_IFTYPE_AP_VLAN;
7692 case WPA_IF_AP_BSS:
7693 return NL80211_IFTYPE_AP;
9f51b113
JB
7694 case WPA_IF_P2P_GO:
7695 return NL80211_IFTYPE_P2P_GO;
22a7c9d7
JM
7696 }
7697 return -1;
7698}
7699
7700
482856c8
JM
7701#ifdef CONFIG_P2P
7702
f2ed8023
JM
7703static int nl80211_addr_in_use(struct nl80211_global *global, const u8 *addr)
7704{
7705 struct wpa_driver_nl80211_data *drv;
7706 dl_list_for_each(drv, &global->interfaces,
7707 struct wpa_driver_nl80211_data, list) {
341eebee 7708 if (os_memcmp(addr, drv->first_bss.addr, ETH_ALEN) == 0)
f2ed8023
JM
7709 return 1;
7710 }
7711 return 0;
7712}
7713
7714
7715static int nl80211_p2p_interface_addr(struct wpa_driver_nl80211_data *drv,
7716 u8 *new_addr)
7717{
7718 unsigned int idx;
7719
7720 if (!drv->global)
7721 return -1;
7722
341eebee 7723 os_memcpy(new_addr, drv->first_bss.addr, ETH_ALEN);
f2ed8023 7724 for (idx = 0; idx < 64; idx++) {
341eebee 7725 new_addr[0] = drv->first_bss.addr[0] | 0x02;
f2ed8023
JM
7726 new_addr[0] ^= idx << 2;
7727 if (!nl80211_addr_in_use(drv->global, new_addr))
7728 break;
7729 }
7730 if (idx == 64)
7731 return -1;
7732
7733 wpa_printf(MSG_DEBUG, "nl80211: Assigned new P2P Interface Address "
7734 MACSTR, MAC2STR(new_addr));
7735
7736 return 0;
7737}
7738
482856c8
JM
7739#endif /* CONFIG_P2P */
7740
f2ed8023 7741
7ab68865 7742static int wpa_driver_nl80211_if_add(void *priv, enum wpa_driver_if_type type,
8043e725 7743 const char *ifname, const u8 *addr,
f3585c8a 7744 void *bss_ctx, void **drv_priv,
e17a2477
JM
7745 char *force_ifname, u8 *if_addr,
7746 const char *bridge)
22a7c9d7 7747{
a2e40bb6
FF
7748 struct i802_bss *bss = priv;
7749 struct wpa_driver_nl80211_data *drv = bss->drv;
22a7c9d7
JM
7750 int ifidx;
7751#ifdef HOSTAPD
a2e40bb6 7752 struct i802_bss *new_bss = NULL;
22a7c9d7
JM
7753
7754 if (type == WPA_IF_AP_BSS) {
a2e40bb6
FF
7755 new_bss = os_zalloc(sizeof(*new_bss));
7756 if (new_bss == NULL)
22a7c9d7
JM
7757 return -1;
7758 }
7759#endif /* HOSTAPD */
7760
f3585c8a
JM
7761 if (addr)
7762 os_memcpy(if_addr, addr, ETH_ALEN);
22a7c9d7 7763 ifidx = nl80211_create_iface(drv, ifname,
fbbfcbac
FF
7764 wpa_driver_nl80211_if_type(type), addr,
7765 0);
22a7c9d7
JM
7766 if (ifidx < 0) {
7767#ifdef HOSTAPD
a2e40bb6 7768 os_free(new_bss);
22a7c9d7
JM
7769#endif /* HOSTAPD */
7770 return -1;
7771 }
7772
f3585c8a 7773 if (!addr &&
c81eff1a
BG
7774 linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
7775 if_addr) < 0) {
c55f774d 7776 nl80211_remove_iface(drv, ifidx);
f3585c8a 7777 return -1;
c55f774d
JM
7778 }
7779
7780#ifdef CONFIG_P2P
7781 if (!addr &&
7782 (type == WPA_IF_P2P_CLIENT || type == WPA_IF_P2P_GROUP ||
7783 type == WPA_IF_P2P_GO)) {
7784 /* Enforce unique P2P Interface Address */
7785 u8 new_addr[ETH_ALEN], own_addr[ETH_ALEN];
7786
c81eff1a
BG
7787 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
7788 own_addr) < 0 ||
7789 linux_get_ifhwaddr(drv->global->ioctl_sock, ifname,
7790 new_addr) < 0) {
c55f774d
JM
7791 nl80211_remove_iface(drv, ifidx);
7792 return -1;
7793 }
7794 if (os_memcmp(own_addr, new_addr, ETH_ALEN) == 0) {
7795 wpa_printf(MSG_DEBUG, "nl80211: Allocate new address "
7796 "for P2P group interface");
f2ed8023 7797 if (nl80211_p2p_interface_addr(drv, new_addr) < 0) {
c55f774d
JM
7798 nl80211_remove_iface(drv, ifidx);
7799 return -1;
7800 }
c81eff1a 7801 if (linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
c55f774d
JM
7802 new_addr) < 0) {
7803 nl80211_remove_iface(drv, ifidx);
7804 return -1;
7805 }
c55f774d 7806 }
f67eeb5c 7807 os_memcpy(if_addr, new_addr, ETH_ALEN);
c55f774d
JM
7808 }
7809#endif /* CONFIG_P2P */
f3585c8a 7810
22a7c9d7 7811#ifdef HOSTAPD
e17a2477
JM
7812 if (bridge &&
7813 i802_check_bridge(drv, new_bss, bridge, ifname) < 0) {
7814 wpa_printf(MSG_ERROR, "nl80211: Failed to add the new "
7815 "interface %s to a bridge %s", ifname, bridge);
7816 nl80211_remove_iface(drv, ifidx);
7817 os_free(new_bss);
7818 return -1;
7819 }
7820
22a7c9d7 7821 if (type == WPA_IF_AP_BSS) {
c81eff1a
BG
7822 if (linux_set_iface_flags(drv->global->ioctl_sock, ifname, 1))
7823 {
34f2f814 7824 nl80211_remove_iface(drv, ifidx);
07179987 7825 os_free(new_bss);
22a7c9d7
JM
7826 return -1;
7827 }
a2e40bb6 7828 os_strlcpy(new_bss->ifname, ifname, IFNAMSIZ);
341eebee 7829 os_memcpy(new_bss->addr, if_addr, ETH_ALEN);
a2e40bb6
FF
7830 new_bss->ifindex = ifidx;
7831 new_bss->drv = drv;
7832 new_bss->next = drv->first_bss.next;
a7a6af4c 7833 new_bss->freq = drv->first_bss.freq;
a2e40bb6
FF
7834 drv->first_bss.next = new_bss;
7835 if (drv_priv)
7836 *drv_priv = new_bss;
cc7a48d1 7837 nl80211_init_bss(new_bss);
3dd1d890
YAP
7838
7839 /* Subscribe management frames for this WPA_IF_AP_BSS */
7840 if (nl80211_setup_ap(new_bss))
7841 return -1;
22a7c9d7
JM
7842 }
7843#endif /* HOSTAPD */
7844
ff6a158b
JM
7845 if (drv->global)
7846 drv->global->if_add_ifindex = ifidx;
7847
22a7c9d7
JM
7848 return 0;
7849}
7850
7851
7852static int wpa_driver_nl80211_if_remove(void *priv,
7853 enum wpa_driver_if_type type,
7854 const char *ifname)
7855{
a2e40bb6
FF
7856 struct i802_bss *bss = priv;
7857 struct wpa_driver_nl80211_data *drv = bss->drv;
22a7c9d7
JM
7858 int ifindex = if_nametoindex(ifname);
7859
e748062b
JM
7860 wpa_printf(MSG_DEBUG, "nl80211: %s(type=%d ifname=%s) ifindex=%d",
7861 __func__, type, ifname, ifindex);
7862 if (ifindex <= 0)
7863 return -1;
e17a2477
JM
7864
7865#ifdef HOSTAPD
7866 if (bss->added_if_into_bridge) {
c81eff1a
BG
7867 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
7868 bss->ifname) < 0)
e17a2477
JM
7869 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
7870 "interface %s from bridge %s: %s",
7871 bss->ifname, bss->brname, strerror(errno));
7872 }
7873 if (bss->added_bridge) {
c81eff1a 7874 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
e17a2477
JM
7875 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
7876 "bridge %s: %s",
7877 bss->brname, strerror(errno));
7878 }
7879#endif /* HOSTAPD */
7880
22a7c9d7
JM
7881 nl80211_remove_iface(drv, ifindex);
7882
7883#ifdef HOSTAPD
a2e40bb6
FF
7884 if (type != WPA_IF_AP_BSS)
7885 return 0;
7886
7887 if (bss != &drv->first_bss) {
8546ea19 7888 struct i802_bss *tbss;
a2e40bb6 7889
8546ea19
JM
7890 for (tbss = &drv->first_bss; tbss; tbss = tbss->next) {
7891 if (tbss->next == bss) {
7892 tbss->next = bss->next;
3dd1d890
YAP
7893 /* Unsubscribe management frames */
7894 nl80211_teardown_ap(bss);
cc7a48d1 7895 nl80211_destroy_bss(bss);
8546ea19
JM
7896 os_free(bss);
7897 bss = NULL;
7898 break;
7899 }
22a7c9d7 7900 }
8546ea19
JM
7901 if (bss)
7902 wpa_printf(MSG_INFO, "nl80211: %s - could not find "
7903 "BSS %p in the list", __func__, bss);
22a7c9d7
JM
7904 }
7905#endif /* HOSTAPD */
7906
7907 return 0;
7908}
7909
7910
55777702
JM
7911static int cookie_handler(struct nl_msg *msg, void *arg)
7912{
7913 struct nlattr *tb[NL80211_ATTR_MAX + 1];
7914 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
7915 u64 *cookie = arg;
7916 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
7917 genlmsg_attrlen(gnlh, 0), NULL);
7918 if (tb[NL80211_ATTR_COOKIE])
7919 *cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
7920 return NL_SKIP;
7921}
7922
7923
88df0ef7 7924static int nl80211_send_frame_cmd(struct i802_bss *bss,
5dfca53f
JB
7925 unsigned int freq, unsigned int wait,
7926 const u8 *buf, size_t buf_len,
88df0ef7
JB
7927 u64 *cookie_out, int no_cck, int no_ack,
7928 int offchanok)
9884f9cc 7929{
88df0ef7 7930 struct wpa_driver_nl80211_data *drv = bss->drv;
9884f9cc
JB
7931 struct nl_msg *msg;
7932 u64 cookie;
7933 int ret = -1;
7934
7935 msg = nlmsg_alloc();
7936 if (!msg)
7937 return -1;
7938
9fb04070 7939 nl80211_cmd(drv, msg, 0, NL80211_CMD_FRAME);
9884f9cc 7940
88df0ef7 7941 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
9884f9cc 7942 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
9db931ed
JM
7943 if (wait)
7944 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, wait);
b9fd8ce8 7945 if (offchanok && (drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX))
88df0ef7 7946 NLA_PUT_FLAG(msg, NL80211_ATTR_OFFCHANNEL_TX_OK);
b106173a
JM
7947 if (no_cck)
7948 NLA_PUT_FLAG(msg, NL80211_ATTR_TX_NO_CCK_RATE);
ddc53271
JM
7949 if (no_ack)
7950 NLA_PUT_FLAG(msg, NL80211_ATTR_DONT_WAIT_FOR_ACK);
970fa12e 7951
9884f9cc
JB
7952 NLA_PUT(msg, NL80211_ATTR_FRAME, buf_len, buf);
7953
7954 cookie = 0;
7955 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
7956 msg = NULL;
7957 if (ret) {
7958 wpa_printf(MSG_DEBUG, "nl80211: Frame command failed: ret=%d "
a05225c8
JM
7959 "(%s) (freq=%u wait=%u)", ret, strerror(-ret),
7960 freq, wait);
9884f9cc
JB
7961 goto nla_put_failure;
7962 }
ddc53271
JM
7963 wpa_printf(MSG_DEBUG, "nl80211: Frame TX command accepted%s; "
7964 "cookie 0x%llx", no_ack ? " (no ACK)" : "",
7965 (long long unsigned int) cookie);
9884f9cc
JB
7966
7967 if (cookie_out)
ddc53271 7968 *cookie_out = no_ack ? (u64) -1 : cookie;
9884f9cc
JB
7969
7970nla_put_failure:
7971 nlmsg_free(msg);
7972 return ret;
7973}
7974
7975
58f6fbe0 7976static int wpa_driver_nl80211_send_action(void *priv, unsigned int freq,
190b9062 7977 unsigned int wait_time,
58f6fbe0
JM
7978 const u8 *dst, const u8 *src,
7979 const u8 *bssid,
b106173a
JM
7980 const u8 *data, size_t data_len,
7981 int no_cck)
58f6fbe0 7982{
a2e40bb6
FF
7983 struct i802_bss *bss = priv;
7984 struct wpa_driver_nl80211_data *drv = bss->drv;
58f6fbe0 7985 int ret = -1;
58f6fbe0
JM
7986 u8 *buf;
7987 struct ieee80211_hdr *hdr;
58f6fbe0 7988
5dfca53f 7989 wpa_printf(MSG_DEBUG, "nl80211: Send Action frame (ifindex=%d, "
55231068
JM
7990 "freq=%u MHz wait=%d ms no_cck=%d)",
7991 drv->ifindex, freq, wait_time, no_cck);
58f6fbe0
JM
7992
7993 buf = os_zalloc(24 + data_len);
7994 if (buf == NULL)
7995 return ret;
7996 os_memcpy(buf + 24, data, data_len);
7997 hdr = (struct ieee80211_hdr *) buf;
7998 hdr->frame_control =
7999 IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_ACTION);
8000 os_memcpy(hdr->addr1, dst, ETH_ALEN);
8001 os_memcpy(hdr->addr2, src, ETH_ALEN);
8002 os_memcpy(hdr->addr3, bssid, ETH_ALEN);
8003
b1f625e0 8004 if (is_ap_interface(drv->nlmode))
55231068
JM
8005 ret = wpa_driver_nl80211_send_mlme_freq(priv, buf,
8006 24 + data_len,
8007 0, freq, no_cck, 1,
8008 wait_time);
9884f9cc 8009 else
88df0ef7 8010 ret = nl80211_send_frame_cmd(bss, freq, wait_time, buf,
5dfca53f 8011 24 + data_len,
b106173a 8012 &drv->send_action_cookie,
88df0ef7 8013 no_cck, 0, 1);
58f6fbe0 8014
f8bf1421 8015 os_free(buf);
58f6fbe0
JM
8016 return ret;
8017}
8018
8019
5dfca53f
JB
8020static void wpa_driver_nl80211_send_action_cancel_wait(void *priv)
8021{
8022 struct i802_bss *bss = priv;
8023 struct wpa_driver_nl80211_data *drv = bss->drv;
8024 struct nl_msg *msg;
8025 int ret;
8026
8027 msg = nlmsg_alloc();
8028 if (!msg)
8029 return;
8030
9fb04070 8031 nl80211_cmd(drv, msg, 0, NL80211_CMD_FRAME_WAIT_CANCEL);
5dfca53f
JB
8032
8033 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
8034 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->send_action_cookie);
8035
8036 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8037 msg = NULL;
8038 if (ret)
8039 wpa_printf(MSG_DEBUG, "nl80211: wait cancel failed: ret=%d "
8040 "(%s)", ret, strerror(-ret));
8041
8042 nla_put_failure:
8043 nlmsg_free(msg);
8044}
8045
8046
55777702
JM
8047static int wpa_driver_nl80211_remain_on_channel(void *priv, unsigned int freq,
8048 unsigned int duration)
8049{
a2e40bb6
FF
8050 struct i802_bss *bss = priv;
8051 struct wpa_driver_nl80211_data *drv = bss->drv;
55777702
JM
8052 struct nl_msg *msg;
8053 int ret;
8054 u64 cookie;
8055
8056 msg = nlmsg_alloc();
8057 if (!msg)
8058 return -1;
8059
9fb04070 8060 nl80211_cmd(drv, msg, 0, NL80211_CMD_REMAIN_ON_CHANNEL);
55777702
JM
8061
8062 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
8063 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
8064 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
8065
8066 cookie = 0;
8067 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
5883168a 8068 msg = NULL;
55777702
JM
8069 if (ret == 0) {
8070 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel cookie "
8071 "0x%llx for freq=%u MHz duration=%u",
8072 (long long unsigned int) cookie, freq, duration);
8073 drv->remain_on_chan_cookie = cookie;
531f0331 8074 drv->pending_remain_on_chan = 1;
55777702
JM
8075 return 0;
8076 }
8077 wpa_printf(MSG_DEBUG, "nl80211: Failed to request remain-on-channel "
15ed5535
JM
8078 "(freq=%d duration=%u): %d (%s)",
8079 freq, duration, ret, strerror(-ret));
55777702 8080nla_put_failure:
5883168a 8081 nlmsg_free(msg);
55777702
JM
8082 return -1;
8083}
8084
8085
8086static int wpa_driver_nl80211_cancel_remain_on_channel(void *priv)
8087{
a2e40bb6
FF
8088 struct i802_bss *bss = priv;
8089 struct wpa_driver_nl80211_data *drv = bss->drv;
55777702
JM
8090 struct nl_msg *msg;
8091 int ret;
8092
8093 if (!drv->pending_remain_on_chan) {
8094 wpa_printf(MSG_DEBUG, "nl80211: No pending remain-on-channel "
8095 "to cancel");
8096 return -1;
8097 }
8098
8099 wpa_printf(MSG_DEBUG, "nl80211: Cancel remain-on-channel with cookie "
8100 "0x%llx",
8101 (long long unsigned int) drv->remain_on_chan_cookie);
8102
8103 msg = nlmsg_alloc();
8104 if (!msg)
8105 return -1;
8106
9fb04070 8107 nl80211_cmd(drv, msg, 0, NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL);
55777702
JM
8108
8109 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
8110 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->remain_on_chan_cookie);
8111
8112 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5883168a 8113 msg = NULL;
55777702
JM
8114 if (ret == 0)
8115 return 0;
8116 wpa_printf(MSG_DEBUG, "nl80211: Failed to cancel remain-on-channel: "
8117 "%d (%s)", ret, strerror(-ret));
8118nla_put_failure:
5883168a 8119 nlmsg_free(msg);
55777702
JM
8120 return -1;
8121}
8122
8123
504e905c
JM
8124static int wpa_driver_nl80211_probe_req_report(void *priv, int report)
8125{
a2e40bb6
FF
8126 struct i802_bss *bss = priv;
8127 struct wpa_driver_nl80211_data *drv = bss->drv;
504e905c 8128
5582a5d1 8129 if (!report) {
0d891981
JM
8130 if (bss->nl_preq && drv->device_ap_sme &&
8131 is_ap_interface(drv->nlmode)) {
8132 /*
8133 * Do not disable Probe Request reporting that was
8134 * enabled in nl80211_setup_ap().
8135 */
8136 wpa_printf(MSG_DEBUG, "nl80211: Skip disabling of "
8137 "Probe Request reporting nl_preq=%p while "
8138 "in AP mode", bss->nl_preq);
8139 } else if (bss->nl_preq) {
36488c05
JM
8140 wpa_printf(MSG_DEBUG, "nl80211: Disable Probe Request "
8141 "reporting nl_preq=%p", bss->nl_preq);
5582a5d1 8142 eloop_unregister_read_sock(
481234cf 8143 nl_socket_get_fd(bss->nl_preq));
221a59c9 8144 nl_destroy_handles(&bss->nl_preq);
5582a5d1
JB
8145 }
8146 return 0;
8147 }
8148
481234cf 8149 if (bss->nl_preq) {
5582a5d1 8150 wpa_printf(MSG_DEBUG, "nl80211: Probe Request reporting "
36488c05 8151 "already on! nl_preq=%p", bss->nl_preq);
5582a5d1
JB
8152 return 0;
8153 }
8154
481234cf
JM
8155 bss->nl_preq = nl_create_handle(drv->global->nl_cb, "preq");
8156 if (bss->nl_preq == NULL)
5582a5d1 8157 return -1;
36488c05
JM
8158 wpa_printf(MSG_DEBUG, "nl80211: Enable Probe Request "
8159 "reporting nl_preq=%p", bss->nl_preq);
5582a5d1 8160
481234cf 8161 if (nl80211_register_frame(bss, bss->nl_preq,
5582a5d1
JB
8162 (WLAN_FC_TYPE_MGMT << 2) |
8163 (WLAN_FC_STYPE_PROBE_REQ << 4),
a92dfde8
JB
8164 NULL, 0) < 0)
8165 goto out_err;
5582a5d1 8166
481234cf 8167 eloop_register_read_sock(nl_socket_get_fd(bss->nl_preq),
a11241fa 8168 wpa_driver_nl80211_event_receive, bss->nl_cb,
481234cf 8169 bss->nl_preq);
5582a5d1 8170
504e905c 8171 return 0;
5582a5d1 8172
a92dfde8 8173 out_err:
221a59c9 8174 nl_destroy_handles(&bss->nl_preq);
5582a5d1 8175 return -1;
504e905c
JM
8176}
8177
8178
4e5cb1a3
JM
8179static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
8180 int ifindex, int disabled)
8181{
8182 struct nl_msg *msg;
8183 struct nlattr *bands, *band;
8184 int ret;
8185
8186 msg = nlmsg_alloc();
8187 if (!msg)
8188 return -1;
8189
9fb04070 8190 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_TX_BITRATE_MASK);
4e5cb1a3
JM
8191 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
8192
8193 bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES);
8194 if (!bands)
8195 goto nla_put_failure;
8196
8197 /*
8198 * Disable 2 GHz rates 1, 2, 5.5, 11 Mbps by masking out everything
8199 * else apart from 6, 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS
8200 * rates. All 5 GHz rates are left enabled.
8201 */
8202 band = nla_nest_start(msg, NL80211_BAND_2GHZ);
8203 if (!band)
8204 goto nla_put_failure;
1dea5882
JM
8205 if (disabled) {
8206 NLA_PUT(msg, NL80211_TXRATE_LEGACY, 8,
8207 "\x0c\x12\x18\x24\x30\x48\x60\x6c");
8208 }
4e5cb1a3
JM
8209 nla_nest_end(msg, band);
8210
8211 nla_nest_end(msg, bands);
8212
8213 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8214 msg = NULL;
8215 if (ret) {
8216 wpa_printf(MSG_DEBUG, "nl80211: Set TX rates failed: ret=%d "
8217 "(%s)", ret, strerror(-ret));
8218 }
8219
8220 return ret;
8221
8222nla_put_failure:
8223 nlmsg_free(msg);
8224 return -1;
8225}
8226
8227
af473088
JM
8228static int wpa_driver_nl80211_deinit_ap(void *priv)
8229{
a2e40bb6
FF
8230 struct i802_bss *bss = priv;
8231 struct wpa_driver_nl80211_data *drv = bss->drv;
b1f625e0 8232 if (!is_ap_interface(drv->nlmode))
af473088
JM
8233 return -1;
8234 wpa_driver_nl80211_del_beacon(drv);
b1f625e0 8235 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
af473088
JM
8236}
8237
8238
207ef3fb
JM
8239static void wpa_driver_nl80211_resume(void *priv)
8240{
a2e40bb6
FF
8241 struct i802_bss *bss = priv;
8242 struct wpa_driver_nl80211_data *drv = bss->drv;
c81eff1a 8243 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1)) {
207ef3fb
JM
8244 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface up on "
8245 "resume event");
8246 }
8247}
8248
8249
7b90c16a
JM
8250static int nl80211_send_ft_action(void *priv, u8 action, const u8 *target_ap,
8251 const u8 *ies, size_t ies_len)
8252{
8253 struct i802_bss *bss = priv;
8254 struct wpa_driver_nl80211_data *drv = bss->drv;
8255 int ret;
8256 u8 *data, *pos;
8257 size_t data_len;
341eebee 8258 const u8 *own_addr = bss->addr;
7b90c16a
JM
8259
8260 if (action != 1) {
8261 wpa_printf(MSG_ERROR, "nl80211: Unsupported send_ft_action "
8262 "action %d", action);
8263 return -1;
8264 }
8265
8266 /*
8267 * Action frame payload:
8268 * Category[1] = 6 (Fast BSS Transition)
8269 * Action[1] = 1 (Fast BSS Transition Request)
8270 * STA Address
8271 * Target AP Address
8272 * FT IEs
8273 */
8274
73fc617d
JM
8275 data_len = 2 + 2 * ETH_ALEN + ies_len;
8276 data = os_malloc(data_len);
7b90c16a
JM
8277 if (data == NULL)
8278 return -1;
8279 pos = data;
8280 *pos++ = 0x06; /* FT Action category */
8281 *pos++ = action;
8282 os_memcpy(pos, own_addr, ETH_ALEN);
8283 pos += ETH_ALEN;
8284 os_memcpy(pos, target_ap, ETH_ALEN);
8285 pos += ETH_ALEN;
8286 os_memcpy(pos, ies, ies_len);
8287
190b9062
JB
8288 ret = wpa_driver_nl80211_send_action(bss, drv->assoc_freq, 0,
8289 drv->bssid, own_addr, drv->bssid,
b106173a 8290 data, data_len, 0);
7b90c16a
JM
8291 os_free(data);
8292
8293 return ret;
8294}
8295
8296
b625473c
JM
8297static int nl80211_signal_monitor(void *priv, int threshold, int hysteresis)
8298{
8299 struct i802_bss *bss = priv;
8300 struct wpa_driver_nl80211_data *drv = bss->drv;
8301 struct nl_msg *msg, *cqm = NULL;
8302
8303 wpa_printf(MSG_DEBUG, "nl80211: Signal monitor threshold=%d "
8304 "hysteresis=%d", threshold, hysteresis);
8305
8306 msg = nlmsg_alloc();
8307 if (!msg)
8308 return -1;
8309
9fb04070 8310 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_CQM);
b625473c
JM
8311
8312 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
8313
8314 cqm = nlmsg_alloc();
8315 if (cqm == NULL)
8316 return -1;
8317
8318 NLA_PUT_U32(cqm, NL80211_ATTR_CQM_RSSI_THOLD, threshold);
8319 NLA_PUT_U32(cqm, NL80211_ATTR_CQM_RSSI_HYST, hysteresis);
8320 nla_put_nested(msg, NL80211_ATTR_CQM, cqm);
8321
8322 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
8323 return 0;
8324 msg = NULL;
8325
8326nla_put_failure:
5883168a 8327 nlmsg_free(cqm);
b625473c
JM
8328 nlmsg_free(msg);
8329 return -1;
8330}
8331
8332
1c5c7273
PS
8333static int nl80211_signal_poll(void *priv, struct wpa_signal_info *si)
8334{
8335 struct i802_bss *bss = priv;
8336 struct wpa_driver_nl80211_data *drv = bss->drv;
8337 int res;
8338
8339 os_memset(si, 0, sizeof(*si));
8340 res = nl80211_get_link_signal(drv, si);
8341 if (res != 0)
8342 return res;
8343
8344 return nl80211_get_link_noise(drv, si);
8345}
8346
8347
57ebba59
JJ
8348static int wpa_driver_nl80211_shared_freq(void *priv)
8349{
8350 struct i802_bss *bss = priv;
8351 struct wpa_driver_nl80211_data *drv = bss->drv;
8352 struct wpa_driver_nl80211_data *driver;
8353 int freq = 0;
8354
8355 /*
8356 * If the same PHY is in connected state with some other interface,
8357 * then retrieve the assoc freq.
8358 */
8359 wpa_printf(MSG_DEBUG, "nl80211: Get shared freq for PHY %s",
8360 drv->phyname);
8361
8362 dl_list_for_each(driver, &drv->global->interfaces,
8363 struct wpa_driver_nl80211_data, list) {
8364 if (drv == driver ||
8365 os_strcmp(drv->phyname, driver->phyname) != 0 ||
8366 !driver->associated)
8367 continue;
8368
8369 wpa_printf(MSG_DEBUG, "nl80211: Found a match for PHY %s - %s "
8370 MACSTR,
8371 driver->phyname, driver->first_bss.ifname,
341eebee 8372 MAC2STR(driver->first_bss.addr));
57ebba59
JJ
8373 freq = nl80211_get_assoc_freq(driver);
8374 wpa_printf(MSG_DEBUG, "nl80211: Shared freq for PHY %s: %d",
8375 drv->phyname, freq);
8376 }
8377
8378 if (!freq)
8379 wpa_printf(MSG_DEBUG, "nl80211: No shared interface for "
8380 "PHY (%s) in associated state", drv->phyname);
8381
8382 return freq;
8383}
8384
8385
b91ab76e
JM
8386static int nl80211_send_frame(void *priv, const u8 *data, size_t data_len,
8387 int encrypt)
8388{
8389 struct i802_bss *bss = priv;
55231068
JM
8390 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt, 0,
8391 0, 0, 0, 0);
b91ab76e
JM
8392}
8393
8394
c55f774d
JM
8395static int nl80211_set_param(void *priv, const char *param)
8396{
c55f774d
JM
8397 wpa_printf(MSG_DEBUG, "nl80211: driver param='%s'", param);
8398 if (param == NULL)
8399 return 0;
8400
8401#ifdef CONFIG_P2P
8402 if (os_strstr(param, "use_p2p_group_interface=1")) {
482856c8
JM
8403 struct i802_bss *bss = priv;
8404 struct wpa_driver_nl80211_data *drv = bss->drv;
8405
c55f774d
JM
8406 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
8407 "interface");
8408 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
8409 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
8410 }
8411#endif /* CONFIG_P2P */
8412
8413 return 0;
8414}
8415
8416
f2ed8023
JM
8417static void * nl80211_global_init(void)
8418{
8419 struct nl80211_global *global;
36d84860
BG
8420 struct netlink_config *cfg;
8421
f2ed8023
JM
8422 global = os_zalloc(sizeof(*global));
8423 if (global == NULL)
8424 return NULL;
c81eff1a 8425 global->ioctl_sock = -1;
f2ed8023 8426 dl_list_init(&global->interfaces);
ff6a158b 8427 global->if_add_ifindex = -1;
36d84860
BG
8428
8429 cfg = os_zalloc(sizeof(*cfg));
8430 if (cfg == NULL)
8431 goto err;
8432
8433 cfg->ctx = global;
8434 cfg->newlink_cb = wpa_driver_nl80211_event_rtm_newlink;
8435 cfg->dellink_cb = wpa_driver_nl80211_event_rtm_dellink;
8436 global->netlink = netlink_init(cfg);
8437 if (global->netlink == NULL) {
8438 os_free(cfg);
8439 goto err;
8440 }
8441
2a7b66f5
BG
8442 if (wpa_driver_nl80211_init_nl_global(global) < 0)
8443 goto err;
8444
c81eff1a
BG
8445 global->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
8446 if (global->ioctl_sock < 0) {
8447 perror("socket(PF_INET,SOCK_DGRAM)");
8448 goto err;
8449 }
8450
f2ed8023 8451 return global;
36d84860
BG
8452
8453err:
8454 nl80211_global_deinit(global);
8455 return NULL;
f2ed8023
JM
8456}
8457
8458
8459static void nl80211_global_deinit(void *priv)
8460{
8461 struct nl80211_global *global = priv;
8462 if (global == NULL)
8463 return;
8464 if (!dl_list_empty(&global->interfaces)) {
8465 wpa_printf(MSG_ERROR, "nl80211: %u interface(s) remain at "
8466 "nl80211_global_deinit",
8467 dl_list_len(&global->interfaces));
8468 }
36d84860
BG
8469
8470 if (global->netlink)
8471 netlink_deinit(global->netlink);
8472
276e2d67
BG
8473 nl_destroy_handles(&global->nl);
8474
481234cf 8475 if (global->nl_event) {
d6c9aab8 8476 eloop_unregister_read_sock(
481234cf 8477 nl_socket_get_fd(global->nl_event));
d6c9aab8
JB
8478 nl_destroy_handles(&global->nl_event);
8479 }
8480
8481 nl_cb_put(global->nl_cb);
2a7b66f5 8482
c81eff1a
BG
8483 if (global->ioctl_sock >= 0)
8484 close(global->ioctl_sock);
8485
f2ed8023
JM
8486 os_free(global);
8487}
8488
8489
6859f1cb
BG
8490static const char * nl80211_get_radio_name(void *priv)
8491{
8492 struct i802_bss *bss = priv;
8493 struct wpa_driver_nl80211_data *drv = bss->drv;
8494 return drv->phyname;
8495}
8496
8497
a6efc65d
JM
8498static int nl80211_pmkid(struct i802_bss *bss, int cmd, const u8 *bssid,
8499 const u8 *pmkid)
8500{
8501 struct nl_msg *msg;
8502
8503 msg = nlmsg_alloc();
8504 if (!msg)
8505 return -ENOMEM;
8506
9fb04070 8507 nl80211_cmd(bss->drv, msg, 0, cmd);
a6efc65d
JM
8508
8509 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
8510 if (pmkid)
8511 NLA_PUT(msg, NL80211_ATTR_PMKID, 16, pmkid);
8512 if (bssid)
8513 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
8514
8515 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
8516 nla_put_failure:
9e088e74 8517 nlmsg_free(msg);
a6efc65d
JM
8518 return -ENOBUFS;
8519}
8520
8521
8522static int nl80211_add_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
8523{
8524 struct i802_bss *bss = priv;
8525 wpa_printf(MSG_DEBUG, "nl80211: Add PMKID for " MACSTR, MAC2STR(bssid));
8526 return nl80211_pmkid(bss, NL80211_CMD_SET_PMKSA, bssid, pmkid);
8527}
8528
8529
8530static int nl80211_remove_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
8531{
8532 struct i802_bss *bss = priv;
8533 wpa_printf(MSG_DEBUG, "nl80211: Delete PMKID for " MACSTR,
8534 MAC2STR(bssid));
8535 return nl80211_pmkid(bss, NL80211_CMD_DEL_PMKSA, bssid, pmkid);
8536}
8537
8538
8539static int nl80211_flush_pmkid(void *priv)
8540{
8541 struct i802_bss *bss = priv;
8542 wpa_printf(MSG_DEBUG, "nl80211: Flush PMKIDs");
8543 return nl80211_pmkid(bss, NL80211_CMD_FLUSH_PMKSA, NULL, NULL);
8544}
8545
8546
b14a210c
JB
8547static void nl80211_set_rekey_info(void *priv, const u8 *kek, const u8 *kck,
8548 const u8 *replay_ctr)
8549{
8550 struct i802_bss *bss = priv;
8551 struct wpa_driver_nl80211_data *drv = bss->drv;
8552 struct nlattr *replay_nested;
8553 struct nl_msg *msg;
8554
8555 msg = nlmsg_alloc();
8556 if (!msg)
8557 return;
8558
9fb04070 8559 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
b14a210c
JB
8560
8561 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
8562
8563 replay_nested = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
8564 if (!replay_nested)
8565 goto nla_put_failure;
8566
8567 NLA_PUT(msg, NL80211_REKEY_DATA_KEK, NL80211_KEK_LEN, kek);
8568 NLA_PUT(msg, NL80211_REKEY_DATA_KCK, NL80211_KCK_LEN, kck);
8569 NLA_PUT(msg, NL80211_REKEY_DATA_REPLAY_CTR, NL80211_REPLAY_CTR_LEN,
8570 replay_ctr);
8571
8572 nla_nest_end(msg, replay_nested);
8573
8574 send_and_recv_msgs(drv, msg, NULL, NULL);
8575 return;
8576 nla_put_failure:
8577 nlmsg_free(msg);
8578}
8579
8580
39718852
JB
8581static void nl80211_send_null_frame(struct i802_bss *bss, const u8 *own_addr,
8582 const u8 *addr, int qos)
bcf24348 8583{
39718852
JB
8584 /* send data frame to poll STA and check whether
8585 * this frame is ACKed */
bcf24348
JB
8586 struct {
8587 struct ieee80211_hdr hdr;
8588 u16 qos_ctl;
8589 } STRUCT_PACKED nulldata;
8590 size_t size;
8591
8592 /* Send data frame to poll STA and check whether this frame is ACKed */
8593
8594 os_memset(&nulldata, 0, sizeof(nulldata));
8595
8596 if (qos) {
8597 nulldata.hdr.frame_control =
8598 IEEE80211_FC(WLAN_FC_TYPE_DATA,
8599 WLAN_FC_STYPE_QOS_NULL);
8600 size = sizeof(nulldata);
8601 } else {
8602 nulldata.hdr.frame_control =
8603 IEEE80211_FC(WLAN_FC_TYPE_DATA,
8604 WLAN_FC_STYPE_NULLFUNC);
8605 size = sizeof(struct ieee80211_hdr);
8606 }
8607
8608 nulldata.hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS);
8609 os_memcpy(nulldata.hdr.IEEE80211_DA_FROMDS, addr, ETH_ALEN);
8610 os_memcpy(nulldata.hdr.IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
8611 os_memcpy(nulldata.hdr.IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
8612
8cfa3527 8613 if (wpa_driver_nl80211_send_mlme(bss, (u8 *) &nulldata, size, 0) < 0)
bcf24348
JB
8614 wpa_printf(MSG_DEBUG, "nl80211_send_null_frame: Failed to "
8615 "send poll frame");
8616}
8617
39718852
JB
8618static void nl80211_poll_client(void *priv, const u8 *own_addr, const u8 *addr,
8619 int qos)
8620{
8621 struct i802_bss *bss = priv;
8622 struct wpa_driver_nl80211_data *drv = bss->drv;
8623 struct nl_msg *msg;
8624
8625 if (!drv->poll_command_supported) {
8626 nl80211_send_null_frame(bss, own_addr, addr, qos);
8627 return;
8628 }
8629
8630 msg = nlmsg_alloc();
8631 if (!msg)
8632 return;
8633
8634 nl80211_cmd(drv, msg, 0, NL80211_CMD_PROBE_CLIENT);
8635
8636 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
8637 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
8638
8639 send_and_recv_msgs(drv, msg, NULL, NULL);
8640 return;
8641 nla_put_failure:
8642 nlmsg_free(msg);
8643}
8644
bcf24348 8645
29f338af
JM
8646static int nl80211_set_power_save(struct i802_bss *bss, int enabled)
8647{
8648 struct nl_msg *msg;
8649
8650 msg = nlmsg_alloc();
8651 if (!msg)
8652 return -ENOMEM;
8653
8654 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_SET_POWER_SAVE);
8655 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
8656 NLA_PUT_U32(msg, NL80211_ATTR_PS_STATE,
8657 enabled ? NL80211_PS_ENABLED : NL80211_PS_DISABLED);
8658 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
8659nla_put_failure:
8660 nlmsg_free(msg);
8661 return -ENOBUFS;
8662}
8663
8664
8665static int nl80211_set_p2p_powersave(void *priv, int legacy_ps, int opp_ps,
8666 int ctwindow)
8667{
8668 struct i802_bss *bss = priv;
8669
8670 wpa_printf(MSG_DEBUG, "nl80211: set_p2p_powersave (legacy_ps=%d "
8671 "opp_ps=%d ctwindow=%d)", legacy_ps, opp_ps, ctwindow);
8672
8673 if (opp_ps != -1 || ctwindow != -1)
8674 return -1; /* Not yet supported */
8675
8676 if (legacy_ps == -1)
8677 return 0;
8678 if (legacy_ps != 0 && legacy_ps != 1)
8679 return -1; /* Not yet supported */
8680
8681 return nl80211_set_power_save(bss, legacy_ps);
8682}
8683
8684
03ea1786
AN
8685#ifdef CONFIG_TDLS
8686
8687static int nl80211_send_tdls_mgmt(void *priv, const u8 *dst, u8 action_code,
8688 u8 dialog_token, u16 status_code,
8689 const u8 *buf, size_t len)
8690{
8691 struct i802_bss *bss = priv;
8692 struct wpa_driver_nl80211_data *drv = bss->drv;
8693 struct nl_msg *msg;
8694
8695 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
8696 return -EOPNOTSUPP;
8697
8698 if (!dst)
8699 return -EINVAL;
8700
8701 msg = nlmsg_alloc();
8702 if (!msg)
8703 return -ENOMEM;
8704
8705 nl80211_cmd(drv, msg, 0, NL80211_CMD_TDLS_MGMT);
8706 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
8707 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
8708 NLA_PUT_U8(msg, NL80211_ATTR_TDLS_ACTION, action_code);
8709 NLA_PUT_U8(msg, NL80211_ATTR_TDLS_DIALOG_TOKEN, dialog_token);
8710 NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status_code);
8711 NLA_PUT(msg, NL80211_ATTR_IE, len, buf);
8712
8713 return send_and_recv_msgs(drv, msg, NULL, NULL);
8714
8715nla_put_failure:
8716 nlmsg_free(msg);
8717 return -ENOBUFS;
8718}
8719
8720
8721static int nl80211_tdls_oper(void *priv, enum tdls_oper oper, const u8 *peer)
8722{
8723 struct i802_bss *bss = priv;
8724 struct wpa_driver_nl80211_data *drv = bss->drv;
8725 struct nl_msg *msg;
8726 enum nl80211_tdls_operation nl80211_oper;
8727
8728 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
8729 return -EOPNOTSUPP;
8730
8731 switch (oper) {
8732 case TDLS_DISCOVERY_REQ:
8733 nl80211_oper = NL80211_TDLS_DISCOVERY_REQ;
8734 break;
8735 case TDLS_SETUP:
8736 nl80211_oper = NL80211_TDLS_SETUP;
8737 break;
8738 case TDLS_TEARDOWN:
8739 nl80211_oper = NL80211_TDLS_TEARDOWN;
8740 break;
8741 case TDLS_ENABLE_LINK:
8742 nl80211_oper = NL80211_TDLS_ENABLE_LINK;
8743 break;
8744 case TDLS_DISABLE_LINK:
8745 nl80211_oper = NL80211_TDLS_DISABLE_LINK;
8746 break;
8747 case TDLS_ENABLE:
8748 return 0;
8749 case TDLS_DISABLE:
8750 return 0;
8751 default:
8752 return -EINVAL;
8753 }
8754
8755 msg = nlmsg_alloc();
8756 if (!msg)
8757 return -ENOMEM;
8758
8759 nl80211_cmd(drv, msg, 0, NL80211_CMD_TDLS_OPER);
8760 NLA_PUT_U8(msg, NL80211_ATTR_TDLS_OPERATION, nl80211_oper);
8761 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
8762 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, peer);
8763
8764 return send_and_recv_msgs(drv, msg, NULL, NULL);
8765
8766nla_put_failure:
8767 nlmsg_free(msg);
8768 return -ENOBUFS;
8769}
8770
8771#endif /* CONFIG TDLS */
8772
8773
216eede8
DS
8774#ifdef ANDROID
8775
8776typedef struct android_wifi_priv_cmd {
8777 char *buf;
8778 int used_len;
8779 int total_len;
8780} android_wifi_priv_cmd;
8781
8782static int drv_errors = 0;
8783
8784static void wpa_driver_send_hang_msg(struct wpa_driver_nl80211_data *drv)
8785{
8786 drv_errors++;
8787 if (drv_errors > DRV_NUMBER_SEQUENTIAL_ERRORS) {
8788 drv_errors = 0;
8789 wpa_msg(drv->ctx, MSG_INFO, WPA_EVENT_DRIVER_STATE "HANGED");
8790 }
8791}
8792
8793
8794static int android_priv_cmd(struct i802_bss *bss, const char *cmd)
8795{
8796 struct wpa_driver_nl80211_data *drv = bss->drv;
8797 struct ifreq ifr;
8798 android_wifi_priv_cmd priv_cmd;
8799 char buf[MAX_DRV_CMD_SIZE];
8800 int ret;
8801
8802 os_memset(&ifr, 0, sizeof(ifr));
8803 os_memset(&priv_cmd, 0, sizeof(priv_cmd));
8804 os_strlcpy(ifr.ifr_name, bss->ifname, IFNAMSIZ);
8805
8806 os_memset(buf, 0, sizeof(buf));
8807 os_strlcpy(buf, cmd, sizeof(buf));
8808
8809 priv_cmd.buf = buf;
8810 priv_cmd.used_len = sizeof(buf);
8811 priv_cmd.total_len = sizeof(buf);
8812 ifr.ifr_data = &priv_cmd;
8813
8814 ret = ioctl(drv->global->ioctl_sock, SIOCDEVPRIVATE + 1, &ifr);
8815 if (ret < 0) {
8816 wpa_printf(MSG_ERROR, "%s: failed to issue private commands",
8817 __func__);
8818 wpa_driver_send_hang_msg(drv);
8819 return ret;
8820 }
8821
8822 drv_errors = 0;
8823 return 0;
8824}
8825
8826
8827static int android_pno_start(struct i802_bss *bss,
8828 struct wpa_driver_scan_params *params)
8829{
8830 struct wpa_driver_nl80211_data *drv = bss->drv;
8831 struct ifreq ifr;
8832 android_wifi_priv_cmd priv_cmd;
8833 int ret = 0, i = 0, bp;
8834 char buf[WEXT_PNO_MAX_COMMAND_SIZE];
8835
8836 bp = WEXT_PNOSETUP_HEADER_SIZE;
8837 os_memcpy(buf, WEXT_PNOSETUP_HEADER, bp);
8838 buf[bp++] = WEXT_PNO_TLV_PREFIX;
8839 buf[bp++] = WEXT_PNO_TLV_VERSION;
8840 buf[bp++] = WEXT_PNO_TLV_SUBVERSION;
8841 buf[bp++] = WEXT_PNO_TLV_RESERVED;
8842
8843 while (i < WEXT_PNO_AMOUNT && (size_t) i < params->num_ssids) {
8844 /* Check that there is enough space needed for 1 more SSID, the
8845 * other sections and null termination */
8846 if ((bp + WEXT_PNO_SSID_HEADER_SIZE + MAX_SSID_LEN +
8847 WEXT_PNO_NONSSID_SECTIONS_SIZE + 1) >= (int) sizeof(buf))
8848 break;
8849 wpa_hexdump_ascii(MSG_DEBUG, "For PNO Scan",
a97bde0a
JM
8850 params->ssids[i].ssid,
8851 params->ssids[i].ssid_len);
216eede8
DS
8852 buf[bp++] = WEXT_PNO_SSID_SECTION;
8853 buf[bp++] = params->ssids[i].ssid_len;
8854 os_memcpy(&buf[bp], params->ssids[i].ssid,
8855 params->ssids[i].ssid_len);
8856 bp += params->ssids[i].ssid_len;
8857 i++;
8858 }
8859
8860 buf[bp++] = WEXT_PNO_SCAN_INTERVAL_SECTION;
8861 os_snprintf(&buf[bp], WEXT_PNO_SCAN_INTERVAL_LENGTH + 1, "%x",
8862 WEXT_PNO_SCAN_INTERVAL);
8863 bp += WEXT_PNO_SCAN_INTERVAL_LENGTH;
8864
8865 buf[bp++] = WEXT_PNO_REPEAT_SECTION;
8866 os_snprintf(&buf[bp], WEXT_PNO_REPEAT_LENGTH + 1, "%x",
8867 WEXT_PNO_REPEAT);
8868 bp += WEXT_PNO_REPEAT_LENGTH;
8869
8870 buf[bp++] = WEXT_PNO_MAX_REPEAT_SECTION;
8871 os_snprintf(&buf[bp], WEXT_PNO_MAX_REPEAT_LENGTH + 1, "%x",
8872 WEXT_PNO_MAX_REPEAT);
8873 bp += WEXT_PNO_MAX_REPEAT_LENGTH + 1;
8874
8875 memset(&ifr, 0, sizeof(ifr));
8876 memset(&priv_cmd, 0, sizeof(priv_cmd));
8877 os_strncpy(ifr.ifr_name, bss->ifname, IFNAMSIZ);
8878
8879 priv_cmd.buf = buf;
8880 priv_cmd.used_len = bp;
8881 priv_cmd.total_len = bp;
8882 ifr.ifr_data = &priv_cmd;
8883
8884 ret = ioctl(drv->global->ioctl_sock, SIOCDEVPRIVATE + 1, &ifr);
8885
8886 if (ret < 0) {
8887 wpa_printf(MSG_ERROR, "ioctl[SIOCSIWPRIV] (pnosetup): %d",
8888 ret);
8889 wpa_driver_send_hang_msg(drv);
8890 return ret;
8891 }
8892
8893 drv_errors = 0;
8894
8895 return android_priv_cmd(bss, "PNOFORCE 1");
8896}
8897
8898
8899static int android_pno_stop(struct i802_bss *bss)
8900{
8901 return android_priv_cmd(bss, "PNOFORCE 0");
8902}
8903
8904#endif /* ANDROID */
8905
8906
3f5285e8
JM
8907const struct wpa_driver_ops wpa_driver_nl80211_ops = {
8908 .name = "nl80211",
8909 .desc = "Linux nl80211/cfg80211",
8910 .get_bssid = wpa_driver_nl80211_get_bssid,
8911 .get_ssid = wpa_driver_nl80211_get_ssid,
3f5285e8 8912 .set_key = wpa_driver_nl80211_set_key,
6a1063e0 8913 .scan2 = wpa_driver_nl80211_scan,
d21c63b9
LC
8914 .sched_scan = wpa_driver_nl80211_sched_scan,
8915 .stop_sched_scan = wpa_driver_nl80211_stop_sched_scan,
3f5285e8
JM
8916 .get_scan_results2 = wpa_driver_nl80211_get_scan_results,
8917 .deauthenticate = wpa_driver_nl80211_deauthenticate,
8918 .disassociate = wpa_driver_nl80211_disassociate,
c2a04078 8919 .authenticate = wpa_driver_nl80211_authenticate,
3f5285e8 8920 .associate = wpa_driver_nl80211_associate,
f2ed8023
JM
8921 .global_init = nl80211_global_init,
8922 .global_deinit = nl80211_global_deinit,
8923 .init2 = wpa_driver_nl80211_init,
3f5285e8 8924 .deinit = wpa_driver_nl80211_deinit,
3f5285e8
JM
8925 .get_capa = wpa_driver_nl80211_get_capa,
8926 .set_operstate = wpa_driver_nl80211_set_operstate,
01652550 8927 .set_supp_port = wpa_driver_nl80211_set_supp_port,
6d158490 8928 .set_country = wpa_driver_nl80211_set_country,
19c3b566 8929 .set_ap = wpa_driver_nl80211_set_ap,
22a7c9d7
JM
8930 .if_add = wpa_driver_nl80211_if_add,
8931 .if_remove = wpa_driver_nl80211_if_remove,
071f8ac4 8932 .send_mlme = wpa_driver_nl80211_send_mlme,
c3965310 8933 .get_hw_feature_data = wpa_driver_nl80211_get_hw_feature_data,
0f4e8b4f
JM
8934 .sta_add = wpa_driver_nl80211_sta_add,
8935 .sta_remove = wpa_driver_nl80211_sta_remove,
db149ac9 8936 .hapd_send_eapol = wpa_driver_nl80211_hapd_send_eapol,
a8d6ffa4 8937 .sta_set_flags = wpa_driver_nl80211_sta_set_flags,
c5121837
JM
8938#ifdef HOSTAPD
8939 .hapd_init = i802_init,
c5121837 8940 .hapd_deinit = i802_deinit,
f7b3920c
JM
8941 .set_wds_sta = i802_set_wds_sta,
8942#endif /* HOSTAPD */
8943#if defined(HOSTAPD) || defined(CONFIG_AP)
c5121837
JM
8944 .get_seqnum = i802_get_seqnum,
8945 .flush = i802_flush,
8946 .read_sta_data = i802_read_sta_data,
c5121837
JM
8947 .get_inact_sec = i802_get_inact_sec,
8948 .sta_clear_stats = i802_sta_clear_stats,
c5121837
JM
8949 .set_rts = i802_set_rts,
8950 .set_frag = i802_set_frag,
c5121837 8951 .set_tx_queue_params = i802_set_tx_queue_params,
c5121837 8952 .set_sta_vlan = i802_set_sta_vlan,
ee7ab173
JB
8953 .sta_deauth = i802_sta_deauth,
8954 .sta_disassoc = i802_sta_disassoc,
8955#endif /* HOSTAPD || CONFIG_AP */
e3802622 8956 .set_freq = i802_set_freq,
58f6fbe0 8957 .send_action = wpa_driver_nl80211_send_action,
5dfca53f 8958 .send_action_cancel_wait = wpa_driver_nl80211_send_action_cancel_wait,
55777702
JM
8959 .remain_on_channel = wpa_driver_nl80211_remain_on_channel,
8960 .cancel_remain_on_channel =
8961 wpa_driver_nl80211_cancel_remain_on_channel,
504e905c 8962 .probe_req_report = wpa_driver_nl80211_probe_req_report,
af473088 8963 .deinit_ap = wpa_driver_nl80211_deinit_ap,
207ef3fb 8964 .resume = wpa_driver_nl80211_resume,
7b90c16a 8965 .send_ft_action = nl80211_send_ft_action,
b625473c 8966 .signal_monitor = nl80211_signal_monitor,
1c5c7273 8967 .signal_poll = nl80211_signal_poll,
b91ab76e 8968 .send_frame = nl80211_send_frame,
57ebba59 8969 .shared_freq = wpa_driver_nl80211_shared_freq,
c55f774d 8970 .set_param = nl80211_set_param,
6859f1cb 8971 .get_radio_name = nl80211_get_radio_name,
a6efc65d
JM
8972 .add_pmkid = nl80211_add_pmkid,
8973 .remove_pmkid = nl80211_remove_pmkid,
8974 .flush_pmkid = nl80211_flush_pmkid,
b14a210c 8975 .set_rekey_info = nl80211_set_rekey_info,
bcf24348 8976 .poll_client = nl80211_poll_client,
29f338af 8977 .set_p2p_powersave = nl80211_set_p2p_powersave,
03ea1786
AN
8978#ifdef CONFIG_TDLS
8979 .send_tdls_mgmt = nl80211_send_tdls_mgmt,
8980 .tdls_oper = nl80211_tdls_oper,
8981#endif /* CONFIG_TDLS */
3f5285e8 8982};