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