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