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