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