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