blob: d0ec953fc4f59b8b5ca92489dd5374efc0aebae6 [file] [log] [blame]
/*
* Copyright (c) 2010 Broadcom Corporation
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/* Toplevel file. Relies on dhd_linux.c to send commands to the dongle. */
#include "cfg80211.h"
#include <stdlib.h>
#include <threads.h>
#include <zircon/errors.h>
#include <zircon/status.h>
#include <algorithm>
#include <optional>
#include <vector>
#include <ddk/hw/wlan/wlaninfo.h>
#include <ddk/metadata.h>
#include <ddk/protocol/wlanif.h>
#include <ddk/protocol/wlanphyimpl.h>
#include <wifi/wifi-config.h>
#include <wlan/common/macaddr.h>
#include <wlan/common/phy.h>
#include <wlan/common/status_code.h>
#include <wlan/protocol/ieee80211.h>
#include <wlan/protocol/mac.h>
#include "bits.h"
#include "brcmu_d11.h"
#include "brcmu_utils.h"
#include "brcmu_wifi.h"
#include "btcoex.h"
#include "common.h"
#include "core.h"
#include "debug.h"
#include "defs.h"
#include "feature.h"
#include "fweh.h"
#include "fwil.h"
#include "fwil_types.h"
#include "linuxisms.h"
#include "macros.h"
#include "netbuf.h"
#include "pno.h"
#include "proto.h"
#include "third_party/bcmdhd/crossdriver/dhd.h"
#include "workqueue.h"
#define BRCMF_SCAN_JOIN_ACTIVE_DWELL_TIME_MS 320
#define BRCMF_SCAN_JOIN_PASSIVE_DWELL_TIME_MS 400
#define BRCMF_SCAN_JOIN_PROBE_INTERVAL_MS 20
#define BRCMF_SCAN_CHANNEL_TIME 40
#define BRCMF_SCAN_UNASSOC_TIME 40
#define BRCMF_SCAN_PASSIVE_TIME 120
#define BRCMF_ND_INFO_TIMEOUT_MSEC 2000
#define EXEC_TIMEOUT_WORKER(worker) \
{ \
if (brcmf_bus_get_bus_type(cfg->pub->bus_if) == BRCMF_BUS_TYPE_SIM) { \
(*cfg->worker.handler)(&cfg->worker); \
} else { \
WorkQueue::ScheduleDefault(&cfg->worker); \
} \
}
static bool check_vif_up(struct brcmf_cfg80211_vif* vif) {
if (!brcmf_test_bit_in_array(BRCMF_VIF_STATUS_READY, &vif->sme_state)) {
BRCMF_DBG(INFO, "device is not ready : status (%lu)\n", vif->sme_state.load());
return false;
}
return true;
}
static uint16_t __wl_rates[] = {
BRCM_RATE_1M, BRCM_RATE_2M, BRCM_RATE_5M5, BRCM_RATE_11M, BRCM_RATE_6M, BRCM_RATE_9M,
BRCM_RATE_12M, BRCM_RATE_18M, BRCM_RATE_24M, BRCM_RATE_36M, BRCM_RATE_48M, BRCM_RATE_54M,
};
#define wl_g_rates (__wl_rates + 0)
#define wl_g_rates_size countof(__wl_rates)
#define wl_a_rates (__wl_rates + 4)
#define wl_a_rates_size ((size_t)(wl_g_rates_size - 4))
/* Vendor specific ie. id = 221, oui and type defines exact ie */
struct brcmf_vs_tlv {
uint8_t id;
uint8_t len;
uint8_t oui[3];
uint8_t oui_type;
};
struct parsed_vndr_ie_info {
uint8_t* ie_ptr;
uint32_t ie_len; /* total length including id & length field */
struct brcmf_vs_tlv vndrie;
};
struct parsed_vndr_ies {
uint32_t count;
struct parsed_vndr_ie_info ie_info[VNDR_IE_PARSE_LIMIT];
};
#define BRCMF_CONNECT_STATUS_LIST \
X(BRCMF_CONNECT_STATUS_CONNECTED) \
X(BRCMF_CONNECT_STATUS_DEAUTHENTICATING) \
X(BRCMF_CONNECT_STATUS_DISASSOCIATING) \
X(BRCMF_CONNECT_STATUS_NO_NETWORK) \
X(BRCMF_CONNECT_STATUS_LINK_FAILED) \
X(BRCMF_CONNECT_STATUS_CONNECTING_TIMEOUT) \
X(BRCMF_CONNECT_STATUS_AUTHENTICATION_FAILED) \
X(BRCMF_CONNECT_STATUS_ASSOC_REQ_FAILED)
#define X(CONNECT_STATUS) CONNECT_STATUS,
enum brcmf_connect_status_t : uint8_t { BRCMF_CONNECT_STATUS_LIST };
#undef X
#define X(CONNECT_STATUS) \
case CONNECT_STATUS: \
return #CONNECT_STATUS;
const char* brcmf_get_connect_status_str(brcmf_connect_status_t connect_status) {
switch (connect_status) { BRCMF_CONNECT_STATUS_LIST };
}
#undef X
static inline void fill_with_broadcast_addr(uint8_t* address) { memset(address, 0xff, ETH_ALEN); }
/* Traverse a string of 1-byte tag/1-byte length/variable-length value
* triples, returning a pointer to the substring whose first element
* matches tag
*/
static const struct brcmf_tlv* brcmf_parse_tlvs(const void* buf, int buflen, uint key) {
const struct brcmf_tlv* elt = static_cast<decltype(elt)>(buf);
int totlen = buflen;
/* find tagged parameter */
while (totlen >= TLV_HDR_LEN) {
int len = elt->len;
/* validate remaining totlen */
if ((elt->id == key) && (totlen >= (len + TLV_HDR_LEN))) {
return elt;
}
elt = (struct brcmf_tlv*)((uint8_t*)elt + (len + TLV_HDR_LEN));
totlen -= (len + TLV_HDR_LEN);
}
return nullptr;
}
static zx_status_t brcmf_vif_change_validate(struct brcmf_cfg80211_info* cfg,
struct brcmf_cfg80211_vif* vif, uint16_t new_type) {
struct brcmf_cfg80211_vif* pos;
bool check_combos = false;
zx_status_t ret = ZX_OK;
struct iface_combination_params params = {
.num_different_channels = 1,
};
list_for_every_entry (&cfg->vif_list, pos, struct brcmf_cfg80211_vif, list) {
if (pos == vif) {
params.iftype_num[new_type]++;
} else {
/* concurrent interfaces so need check combinations */
check_combos = true;
params.iftype_num[pos->wdev.iftype]++;
}
}
if (check_combos) {
ret = cfg80211_check_combinations(cfg, &params);
}
return ret;
}
static zx_status_t brcmf_vif_add_validate(struct brcmf_cfg80211_info* cfg,
wlan_info_mac_role_t new_type) {
struct brcmf_cfg80211_vif* pos;
struct iface_combination_params params = {
.num_different_channels = 1,
};
list_for_every_entry (&cfg->vif_list, pos, struct brcmf_cfg80211_vif, list) {
params.iftype_num[pos->wdev.iftype]++;
}
params.iftype_num[new_type]++;
return cfg80211_check_combinations(cfg, &params);
}
static void convert_key_from_CPU(struct brcmf_wsec_key* key, struct brcmf_wsec_key_le* key_le) {
key_le->index = key->index;
key_le->len = key->len;
key_le->algo = key->algo;
key_le->flags = key->flags;
key_le->rxiv.hi = key->rxiv.hi;
key_le->rxiv.lo = key->rxiv.lo;
key_le->iv_initialized = key->iv_initialized;
memcpy(key_le->data, key->data, sizeof(key->data));
memcpy(key_le->ea, key->ea, sizeof(key->ea));
}
static zx_status_t send_key_to_dongle(struct brcmf_if* ifp, struct brcmf_wsec_key* key) {
zx_status_t err;
struct brcmf_wsec_key_le key_le;
convert_key_from_CPU(key, &key_le);
brcmf_netdev_wait_pend8021x(ifp);
err = brcmf_fil_bsscfg_data_set(ifp, "wsec_key", &key_le, sizeof(key_le));
if (err != ZX_OK) {
BRCMF_ERR("wsec_key error (%d)", err);
}
return err;
}
static void brcmf_cfg80211_update_proto_addr_mode(struct wireless_dev* wdev) {
struct brcmf_cfg80211_vif* vif;
struct brcmf_if* ifp;
vif = containerof(wdev, struct brcmf_cfg80211_vif, wdev);
ifp = vif->ifp;
if (wdev->iftype == WLAN_INFO_MAC_ROLE_AP) {
brcmf_proto_configure_addr_mode(ifp->drvr, ifp->ifidx, ADDR_DIRECT);
} else {
brcmf_proto_configure_addr_mode(ifp->drvr, ifp->ifidx, ADDR_INDIRECT);
}
}
static int32_t brcmf_get_first_free_bsscfgidx(struct brcmf_pub* drvr) {
int bsscfgidx;
for (bsscfgidx = 0; bsscfgidx < BRCMF_MAX_IFS; bsscfgidx++) {
/* bsscfgidx 1 is reserved for legacy P2P */
if (bsscfgidx == 1) {
continue;
}
if (!drvr->iflist[bsscfgidx]) {
return bsscfgidx;
}
}
return -1;
}
static int32_t brcmf_get_prealloced_bsscfgidx(struct brcmf_pub* drvr) {
int bsscfgidx;
net_device* ndev;
for (bsscfgidx = 0; bsscfgidx < BRCMF_MAX_IFS; bsscfgidx++) {
/* bsscfgidx 1 is reserved for legacy P2P */
if (bsscfgidx == 1) {
continue;
}
if (drvr->iflist[bsscfgidx]) {
ndev = drvr->iflist[bsscfgidx]->ndev;
if (ndev && ndev->needs_free_net_device) {
return bsscfgidx;
}
}
}
return -1;
}
static zx_status_t brcmf_cfg80211_request_ap_if(struct brcmf_if* ifp) {
struct brcmf_mbss_ssid_le mbss_ssid_le;
int bsscfgidx;
zx_status_t err;
memset(&mbss_ssid_le, 0, sizeof(mbss_ssid_le));
bsscfgidx = brcmf_get_first_free_bsscfgidx(ifp->drvr);
if (bsscfgidx < 0) {
return ZX_ERR_NO_MEMORY;
}
mbss_ssid_le.bsscfgidx = bsscfgidx;
mbss_ssid_le.SSID_len = 5;
sprintf((char*)mbss_ssid_le.SSID, "ssid%d", bsscfgidx);
err = brcmf_fil_bsscfg_data_set(ifp, "bsscfg:ssid", &mbss_ssid_le, sizeof(mbss_ssid_le));
if (err != ZX_OK) {
BRCMF_ERR("setting ssid failed %d", err);
}
return err;
}
/*For now this function should always be called when adding iface*/
static zx_status_t brcmf_set_iface_macaddr(net_device* ndev,
const wlan::common::MacAddr& mac_addr) {
struct brcmf_if* ifp = ndev_to_if(ndev);
bcme_status_t fw_err = BCME_OK;
zx_status_t err = ZX_OK;
BRCMF_DBG(TRACE, "Enter");
// If the existing mac_addr of this iface is the same as it is, just return success.
if (!memcmp(ifp->mac_addr, mac_addr.byte, ETH_ALEN)) {
return ZX_OK;
}
err = brcmf_fil_iovar_data_set(ifp, "cur_etheraddr", mac_addr.byte, ETH_ALEN, &fw_err);
if (err != ZX_OK) {
BRCMF_ERR("Setting mac address failed: %s, fw err %s\n", zx_status_get_string(err),
brcmf_fil_get_errstr(fw_err));
return err;
}
BRCMF_INFO("Setting mac address of ndev:%s to %s\n", ifp->ndev->name, MACSTR(mac_addr));
memcpy(ifp->mac_addr, mac_addr.byte, sizeof(ifp->mac_addr));
return err;
}
// Derive the mac address for the SoftAP interface from the system mac address
// (which is used for the client interface).
zx_status_t brcmf_gen_ap_macaddr(struct brcmf_if* ifp, wlan::common::MacAddr& out_mac_addr) {
bcme_status_t fw_err = BCME_OK;
uint8_t gen_mac_addr[ETH_ALEN];
zx_status_t err = brcmf_fil_iovar_data_get(ifp, "cur_etheraddr", gen_mac_addr, ETH_ALEN, &fw_err);
if (err != ZX_OK) {
BRCMF_ERR("Retrieving mac address from firmware failed: %s, fw err %s",
zx_status_get_string(err), brcmf_fil_get_errstr(fw_err));
return err;
}
// Modify the mac address as follows:
// Mark the address as unicast and locally administered. In addition, modify
// byte 5 (increment) to ensure that it is different from the original address
gen_mac_addr[0] &= 0xfe; // bit 0: 0 = unicast
gen_mac_addr[0] |= 0x02; // bit 1: 1 = locally-administered
gen_mac_addr[5]++;
out_mac_addr.Set(gen_mac_addr);
return ZX_OK;
}
static zx_status_t brcmf_set_ap_macaddr(struct brcmf_if* ifp,
const std::optional<wlan::common::MacAddr>& in_mac_addr) {
wlan::common::MacAddr mac_addr;
zx_status_t err = ZX_OK;
// Use the provided mac_addr if it passed.
if (in_mac_addr) {
mac_addr = *in_mac_addr;
} else {
// If MAC address is not provided, we generate one using the current MAC address.
// By default it is derived from the system mac address set during init.
err = brcmf_gen_ap_macaddr(ifp, mac_addr);
if (err != ZX_OK) {
BRCMF_ERR("Failed to generate MAC address for AP iface netdev: %s", ifp->ndev->name);
return err;
}
}
err = brcmf_set_iface_macaddr(ifp->ndev, mac_addr);
if (err != ZX_OK) {
BRCMF_ERR("Failed to set MAC address %s for AP iface netdev: %s", MACSTR(mac_addr),
ifp->ndev->name);
return err;
}
return ZX_OK;
}
static zx_status_t brcmf_cfg80211_change_iface(struct brcmf_cfg80211_info* cfg,
struct net_device* ndev, wlan_info_mac_role_t type,
struct vif_params* params) {
struct brcmf_if* ifp = ndev_to_if(ndev);
struct brcmf_cfg80211_vif* vif = ifp->vif;
int32_t infra = 0;
int32_t ap = 0;
zx_status_t err = ZX_OK;
bcme_status_t fw_err = BCME_OK;
BRCMF_DBG(TRACE, "Enter");
err = brcmf_vif_change_validate(cfg, vif, type);
if (err != ZX_OK) {
BRCMF_ERR("iface validation failed: err=%d", err);
return err;
}
switch (type) {
case WLAN_INFO_MAC_ROLE_CLIENT:
infra = 1;
break;
case WLAN_INFO_MAC_ROLE_AP:
ap = 1;
break;
default:
err = ZX_ERR_OUT_OF_RANGE;
goto done;
}
if (ap) {
BRCMF_DBG(INFO, "IF Type = AP");
} else {
err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_INFRA, infra, &fw_err);
if (err != ZX_OK) {
BRCMF_ERR("WLC_SET_INFRA error: %s, fw err %s", zx_status_get_string(err),
brcmf_fil_get_errstr(fw_err));
err = ZX_ERR_UNAVAILABLE;
goto done;
}
BRCMF_DBG(INFO, "IF Type = Infra");
}
vif->wdev.iftype = type;
brcmf_cfg80211_update_proto_addr_mode(&vif->wdev);
done:
BRCMF_DBG(TRACE, "Exit");
return err;
}
/**
* brcmf_ap_add_vif() - create a new AP virtual interface for multiple BSS
*
* @cfg: config of new interface.
* @name: name of the new interface.
* @dev_out: address of wireless dev pointer
*/
static zx_status_t brcmf_ap_add_vif(struct brcmf_cfg80211_info* cfg, const char* name,
const std::optional<wlan::common::MacAddr>& mac_addr,
struct wireless_dev** dev_out) {
struct brcmf_if* ifp = cfg_to_if(cfg);
struct brcmf_cfg80211_vif* vif;
zx_status_t err;
// We need to create the SoftAP IF if we are not operating with manufacturing FW.
if (!brcmf_feat_is_enabled(ifp, BRCMF_FEAT_MFG)) {
if (brcmf_cfg80211_vif_event_armed(cfg)) {
return ZX_ERR_UNAVAILABLE;
}
BRCMF_DBG(INFO, "Adding vif \"%s\"", name);
err = brcmf_alloc_vif(cfg, WLAN_INFO_MAC_ROLE_AP, &vif);
if (err != ZX_OK) {
if (dev_out) {
*dev_out = nullptr;
}
return err;
}
brcmf_cfg80211_arm_vif_event(cfg, vif, BRCMF_E_IF_ADD);
err = brcmf_cfg80211_request_ap_if(ifp);
if (err != ZX_OK) {
brcmf_cfg80211_disarm_vif_event(cfg);
goto fail;
}
/* wait for firmware event */
err = brcmf_cfg80211_wait_vif_event(cfg, ZX_MSEC(BRCMF_VIF_EVENT_TIMEOUT_MSEC));
brcmf_cfg80211_disarm_vif_event(cfg);
if (err != ZX_OK) {
BRCMF_ERR("timeout occurred");
err = ZX_ERR_IO;
goto fail;
}
} else {
// Else reuse the existing IF itself but change its type
vif = ifp->vif;
vif->ifp = ifp;
err = brcmf_cfg80211_change_iface(cfg, ifp->ndev, WLAN_INFO_MAC_ROLE_AP, nullptr);
if (err != ZX_OK) {
BRCMF_ERR("Unable to change IF type err: %u", err);
err = ZX_ERR_IO;
goto fail;
}
}
/* interface created in firmware */
ifp = vif->ifp;
if (!ifp) {
BRCMF_ERR("no if pointer provided");
err = ZX_ERR_INVALID_ARGS;
goto fail;
}
strncpy(ifp->ndev->name, name, sizeof(ifp->ndev->name) - 1);
err = brcmf_net_attach(ifp, true);
if (err != ZX_OK) {
BRCMF_ERR("Registering netdevice failed");
brcmf_free_net_device(ifp->ndev);
goto fail;
}
err = brcmf_set_ap_macaddr(ifp, mac_addr);
if (err != ZX_OK) {
BRCMF_ERR("unable to set mac address of ap if");
goto fail;
}
if (dev_out) {
*dev_out = &ifp->vif->wdev;
}
return ZX_OK;
fail:
brcmf_free_vif(vif);
if (dev_out) {
*dev_out = nullptr;
}
return err;
}
static bool brcmf_is_apmode(struct brcmf_cfg80211_vif* vif) {
uint16_t iftype;
iftype = vif->wdev.iftype;
return iftype == WLAN_INFO_MAC_ROLE_AP;
}
static bool brcmf_is_existing_macaddr(brcmf_pub* drvr, const uint8_t mac_addr[ETH_ALEN],
bool is_ap) {
if (is_ap) {
for (const auto& iface : drvr->iflist) {
if (iface != nullptr && !memcmp(iface->mac_addr, mac_addr, ETH_ALEN)) {
return true;
}
}
} else {
for (const auto& iface : drvr->iflist) {
if (iface != nullptr && iface->vif->wdev.iftype != WLAN_INFO_MAC_ROLE_CLIENT &&
!memcmp(iface->mac_addr, mac_addr, ETH_ALEN)) {
return true;
}
}
}
return false;
}
zx_status_t brcmf_cfg80211_add_iface(brcmf_pub* drvr, const char* name, struct vif_params* params,
const wlanphy_impl_create_iface_req_t* req,
struct wireless_dev** wdev_out) {
zx_status_t err;
net_device* ndev;
wireless_dev* wdev;
int32_t bsscfgidx;
BRCMF_DBG(TRACE, "enter: %s type %d", name, req->role);
if (wdev_out == nullptr) {
BRCMF_ERR("cannot write wdev to nullptr");
return ZX_ERR_INVALID_ARGS;
}
err = brcmf_vif_add_validate(drvr->config, req->role);
if (err != ZX_OK) {
BRCMF_ERR("iface validation failed: err=%d", err);
return err;
}
struct brcmf_if* ifp;
const char* iface_role_name;
std::optional<wlan::common::MacAddr> mac_addr;
if (req->has_init_mac_addr) {
mac_addr.emplace(req->init_mac_addr);
}
switch (req->role) {
case WLAN_INFO_MAC_ROLE_AP:
iface_role_name = "ap";
if (mac_addr && brcmf_is_existing_macaddr(drvr, mac_addr->byte, true)) {
return ZX_ERR_ALREADY_EXISTS;
}
err = brcmf_ap_add_vif(drvr->config, name, mac_addr, &wdev);
if (err != ZX_OK) {
BRCMF_ERR("add iface %s type %d failed: err=%d", name, req->role, err);
return err;
}
brcmf_cfg80211_update_proto_addr_mode(wdev);
ndev = wdev->netdev;
wdev->iftype = req->role;
ndev->sme_channel = zx::channel(req->sme_channel);
break;
case WLAN_INFO_MAC_ROLE_CLIENT: {
iface_role_name = "client";
if (mac_addr && brcmf_is_existing_macaddr(drvr, mac_addr->byte, false)) {
return ZX_ERR_ALREADY_EXISTS;
}
bsscfgidx = brcmf_get_prealloced_bsscfgidx(drvr);
if (bsscfgidx < 0) {
return ZX_ERR_NO_MEMORY;
}
ndev = drvr->iflist[bsscfgidx]->ndev;
if (strncmp(ndev->name, name, sizeof(ndev->name))) {
BRCMF_INFO("Reusing netdev:%s for new client iface, but changing its name to netdev:%s.",
ndev->name, name);
brcmf_write_net_device_name(ndev, name);
}
ifp = brcmf_get_ifp(drvr, 0);
if (brcmf_feat_is_enabled(ifp, BRCMF_FEAT_MFG)) {
// Since a single IF is shared when operating with manufacturing FW, change
// IF type.
err = brcmf_cfg80211_change_iface(drvr->config, ifp->ndev, WLAN_INFO_MAC_ROLE_CLIENT,
nullptr);
if (err != ZX_OK) {
BRCMF_ERR("Unable to change iface to client");
return err;
}
}
wdev = &drvr->iflist[bsscfgidx]->vif->wdev;
wdev->iftype = req->role;
ndev->sme_channel = zx::channel(req->sme_channel);
ndev->needs_free_net_device = false;
// Use input mac_addr if it's provided. Otherwise, fallback to the bootloader
// MAC address. Note that this fallback MAC address is intended for client ifaces only.
wlan::common::MacAddr client_mac_addr;
if (mac_addr) {
client_mac_addr = *mac_addr;
} else {
err = brcmf_bus_get_bootloader_macaddr(drvr->bus_if, client_mac_addr.byte);
if (err != ZX_OK || client_mac_addr.IsZero() || client_mac_addr.IsBcast()) {
BRCMF_ERR("Failed to get valid mac address from bootloader: %s",
(err != ZX_OK) ? zx_status_get_string(err) : MACSTR(client_mac_addr));
err = brcmf_gen_random_mac_addr(client_mac_addr.byte);
if (err != ZX_OK) {
BRCMF_ERR("Failed to generate random MAC address.");
return err;
}
BRCMF_ERR("Falling back to random mac address: %s", MACSTR(client_mac_addr));
} else {
BRCMF_DBG(INFO, "Retrieved bootloader wifi MAC addresss: %s", MACSTR(client_mac_addr));
}
}
err = brcmf_set_iface_macaddr(ndev, client_mac_addr);
if (err != ZX_OK) {
BRCMF_ERR("Failed to set MAC address %s for client iface netdev:%s",
MACSTR(client_mac_addr), ndev->name);
return err;
}
break;
}
default:
return ZX_ERR_INVALID_ARGS;
}
*wdev_out = wdev;
return ZX_OK;
}
static void brcmf_scan_config_mpc(struct brcmf_if* ifp, int mpc) {
if (brcmf_feat_is_quirk_enabled(ifp, BRCMF_FEAT_QUIRK_NEED_MPC)) {
brcmf_enable_mpc(ifp, mpc);
}
}
// This function set "mpc" to the requested value only if SoftAP
// has not been started. Else it sets "mpc" to 0.
void brcmf_enable_mpc(struct brcmf_if* ifp, int mpc) {
zx_status_t err = ZX_OK;
bcme_status_t fw_err = BCME_OK;
struct brcmf_cfg80211_info* cfg = ifp->drvr->config;
// If AP has been started, mpc is always 0
if (cfg->ap_started) {
mpc = 0;
}
err = brcmf_fil_iovar_int_set(ifp, "mpc", mpc, &fw_err);
if (err != ZX_OK) {
BRCMF_ERR("fail to set mpc: %s, fw err %s", zx_status_get_string(err),
brcmf_fil_get_errstr(fw_err));
return;
}
BRCMF_DBG(INFO, "MPC : %d", mpc);
}
static void brcmf_signal_scan_end(struct net_device* ndev, uint64_t txn_id,
uint8_t scan_result_code) {
wlanif_scan_end_t args;
args.txn_id = txn_id;
args.code = scan_result_code;
std::shared_lock<std::shared_mutex> guard(ndev->if_proto_lock);
if (ndev->if_proto.ops == nullptr) {
BRCMF_DBG(WLANIF, "interface stopped -- skipping signal scan end callback");
} else {
BRCMF_DBG(SCAN, "Signaling on_scan_end with txn_id %ld and code %d", args.txn_id, args.code);
BRCMF_DBG(WLANIF,
"Sending scan end event to SME. txn_id: %" PRIu64
", result: %s"
", APs seen: %" PRIu32 "",
args.txn_id,
args.code == WLAN_SCAN_RESULT_SUCCESS ? "success"
: args.code == WLAN_SCAN_RESULT_NOT_SUPPORTED ? "not supported"
: args.code == WLAN_SCAN_RESULT_INVALID_ARGS ? "invalid args"
: args.code == WLAN_SCAN_RESULT_INTERNAL_ERROR ? "internal error"
: "unknown",
ndev->scan_num_results);
wlanif_impl_ifc_on_scan_end(&ndev->if_proto, &args);
}
}
zx_status_t brcmf_notify_escan_complete(struct brcmf_cfg80211_info* cfg, struct brcmf_if* ifp,
bool aborted, bool fw_abort) {
struct brcmf_scan_params_le params_le;
const wlanif_scan_req_t* scan_request;
uint64_t reqid;
uint32_t bucket;
zx_status_t err = ZX_OK;
BRCMF_DBG(SCAN, "Enter");
/* clear scan request, because the FW abort can cause a second call */
/* to this function and might cause a double signal_scan_end */
scan_request = cfg->scan_request;
cfg->scan_request = nullptr;
// Canceling if it's inactive is OK. Checking if it's active just invites race conditions.
cfg->escan_timer->Stop();
if (fw_abort) {
/* Do a scan abort to stop the driver's scan engine */
BRCMF_DBG(SCAN, "ABORT scan in firmware");
memset(&params_le, 0, sizeof(params_le));
fill_with_broadcast_addr(params_le.bssid);
params_le.bss_type = DOT11_BSSTYPE_ANY;
params_le.scan_type = 0;
params_le.channel_num = 1;
params_le.nprobes = 1;
params_le.active_time = -1;
params_le.passive_time = -1;
params_le.home_time = -1;
/* Scan is aborted by setting channel_list[0] to -1 */
params_le.channel_list[0] = -1;
/* E-Scan (or anyother type) can be aborted by SCAN */
bcme_status_t fwerr = BCME_OK;
err = brcmf_fil_cmd_data_set(ifp, BRCMF_C_SCAN, &params_le, sizeof(params_le), &fwerr);
if (err != ZX_OK) {
BRCMF_ERR("Scan abort failed: %s (fw err %s)", zx_status_get_string(err),
brcmf_fil_get_errstr(fwerr));
}
}
brcmf_scan_config_mpc(ifp, 1);
/*
* e-scan can be initiated internally
* which takes precedence.
*/
struct net_device* ndev = cfg_to_ndev(cfg);
if (cfg->int_escan_map) {
BRCMF_DBG(SCAN, "scheduled scan completed (%x)", cfg->int_escan_map);
while (cfg->int_escan_map) {
bucket = ffs(cfg->int_escan_map) - 1; // ffs() index is 1-based
cfg->int_escan_map &= ~BIT(bucket);
reqid = brcmf_pno_find_reqid_by_bucket(cfg->pno, bucket);
if (!aborted) {
// TODO(cphoenix): Figure out how to use internal reqid infrastructure, rather
// than storing it separately in wiphy->scan_txn_id.
BRCMF_DBG(SCAN, " * * report scan results: internal reqid=%lu", reqid);
brcmf_signal_scan_end(ndev, ndev->scan_txn_id, WLAN_SCAN_RESULT_SUCCESS);
}
}
} else if (scan_request) {
BRCMF_DBG(WLANIF, "ESCAN Completed scan: %s", aborted ? "Aborted" : "Done");
brcmf_signal_scan_end(ndev, ndev->scan_txn_id,
aborted ? WLAN_SCAN_RESULT_INTERNAL_ERROR : WLAN_SCAN_RESULT_SUCCESS);
}
if (!brcmf_test_and_clear_bit_in_array(BRCMF_SCAN_STATUS_BUSY, &cfg->scan_status)) {
BRCMF_DBG(SCAN, "Scan complete, probably P2P scan");
}
return err;
}
static zx_status_t brcmf_cfg80211_del_ap_iface(struct brcmf_cfg80211_info* cfg,
struct wireless_dev* wdev) {
struct net_device* ndev = wdev->netdev;
struct brcmf_if* ifp = nullptr;
zx_status_t err;
if (ndev)
ifp = ndev_to_if(ndev);
else {
BRCMF_ERR("Net device is nullptr");
return ZX_ERR_IO;
}
if (brcmf_feat_is_enabled(ifp, BRCMF_FEAT_MFG)) {
// If we are operating with manufacturing FW, we just have a single IF. Pretend like it was
// deleted.
return ZX_OK;
}
brcmf_cfg80211_arm_vif_event(cfg, ifp->vif, BRCMF_E_IF_DEL);
err = brcmf_fil_bsscfg_data_set(ifp, "interface_remove", nullptr, 0);
if (err != ZX_OK) {
BRCMF_ERR("interface_remove interface %d failed %d", ifp->ifidx, err);
goto err_unarm;
}
/* wait for firmware event */
err = brcmf_cfg80211_wait_vif_event(cfg, ZX_MSEC(BRCMF_VIF_EVENT_TIMEOUT_MSEC));
if (err != ZX_OK) {
BRCMF_ERR("BRCMF_VIF_EVENT timeout occurred");
err = ZX_ERR_IO;
goto err_unarm;
}
brcmf_remove_interface(ifp, true);
err_unarm:
brcmf_cfg80211_disarm_vif_event(cfg);
return err;
}
static zx_status_t brcmf_dev_escan_set_randmac(struct brcmf_if* ifp) {
struct brcmf_pno_macaddr_le pfn_mac = {};
zx_status_t err = ZX_OK;
bcme_status_t fw_err = BCME_OK;
pfn_mac.version = BRCMF_PFN_MACADDR_CFG_VER;
pfn_mac.flags = BRCMF_PFN_USE_FULL_MACADDR;
err = brcmf_gen_random_mac_addr(pfn_mac.mac);
if (err != ZX_OK) {
return err;
}
err = brcmf_fil_iovar_data_set(ifp, "pfn_macaddr", &pfn_mac, sizeof(pfn_mac), &fw_err);
if (err)
BRCMF_ERR("set escan randmac failed, err=%d, fw_err=%d", err, fw_err);
return err;
}
static zx_status_t brcmf_escan_prep(struct brcmf_cfg80211_info* cfg,
struct brcmf_scan_params_le* params_le,
const wlanif_scan_req_t* request) {
uint32_t n_ssids;
uint32_t n_channels;
int32_t i;
int32_t offset;
uint16_t chanspec;
char* ptr;
struct brcmf_ssid_le ssid_le;
fill_with_broadcast_addr(params_le->bssid);
params_le->bss_type = DOT11_BSSTYPE_ANY;
if (request->scan_type == WLAN_SCAN_TYPE_ACTIVE) {
params_le->scan_type = BRCMF_SCANTYPE_ACTIVE;
params_le->active_time = request->min_channel_time;
params_le->nprobes = BRCMF_ACTIVE_SCAN_NUM_PROBES;
params_le->passive_time = -1;
} else {
params_le->scan_type = BRCMF_SCANTYPE_PASSIVE;
params_le->passive_time = request->min_channel_time;
params_le->active_time = -1;
}
params_le->channel_num = 0;
params_le->home_time = -1;
if (request->ssid.len > IEEE80211_MAX_SSID_LEN) {
BRCMF_ERR("Scan request SSID too long(no longer than %d bytes)", IEEE80211_MAX_SSID_LEN);
return ZX_ERR_INVALID_ARGS;
}
params_le->ssid_le.SSID_len = request->ssid.len;
memcpy(params_le->ssid_le.SSID, request->ssid.data, request->ssid.len);
n_ssids = request->num_ssids;
n_channels = request->num_channels;
/* Copy channel array if applicable */
BRCMF_DBG(SCAN, "### List of channelspecs to scan ### %d", n_channels);
if (n_channels > 0) {
for (i = 0; i < (int32_t)n_channels; i++) {
wlan_channel_t wlan_chan;
wlan_chan.primary = request->channel_list[i];
wlan_chan.cbw = WLAN_CHANNEL_BANDWIDTH__20;
wlan_chan.secondary80 = 0;
chanspec = channel_to_chanspec(&cfg->d11inf, &wlan_chan);
BRCMF_DBG(SCAN, "Chan : %d, Channel spec: %x", request->channel_list[i], chanspec);
params_le->channel_list[i] = chanspec;
}
} else {
BRCMF_DBG(SCAN, "Scanning all channels");
}
/* Copy ssid array if applicable */
BRCMF_DBG(SCAN, "### List of SSIDs to scan ### %d", n_ssids);
if (n_ssids > 0) {
if (params_le->scan_type == BRCMF_SCANTYPE_ACTIVE) {
offset = offsetof(struct brcmf_scan_params_le, channel_list) + n_channels * sizeof(uint16_t);
offset = roundup(offset, sizeof(uint32_t));
ptr = (char*)params_le + offset;
for (i = 0; i < (int32_t)n_ssids; i++) {
if (request->ssid_list[i].len > IEEE80211_MAX_SSID_LEN) {
BRCMF_ERR("SSID in scan request SSID list too long(no longer than %d bytes)",
IEEE80211_MAX_SSID_LEN);
return ZX_ERR_INVALID_ARGS;
}
memset(&ssid_le, 0, sizeof(ssid_le));
ssid_le.SSID_len = request->ssid_list[i].len;
memcpy(ssid_le.SSID, request->ssid_list[i].data, request->ssid_list[i].len);
if (!ssid_le.SSID_len) {
BRCMF_DBG(SCAN, "%d: Broadcast scan", i);
} else {
BRCMF_DBG(SCAN, "%d: scan for %.32s size=%d", i, ssid_le.SSID, ssid_le.SSID_len);
}
memcpy(ptr, &ssid_le, sizeof(ssid_le));
ptr += sizeof(ssid_le);
}
}
}
/* Adding mask to channel numbers */
params_le->channel_num =
(n_ssids << BRCMF_SCAN_PARAMS_NSSID_SHIFT) | (n_channels & BRCMF_SCAN_PARAMS_COUNT_MASK);
return ZX_OK;
}
// Calculate the amount of memory needed to hold the escan parameters for a firmware request
static size_t brcmf_escan_params_size(size_t num_channels, size_t num_ssids) {
size_t size = BRCMF_SCAN_PARAMS_FIXED_SIZE;
// escan params headers
size += offsetof(struct brcmf_escan_params_le, params_le);
// Channel specs
size += sizeof(uint32_t) * ((num_channels + 1) / 2);
// SSIDs
size += sizeof(struct brcmf_ssid_le) * num_ssids;
return size;
}
static zx_status_t brcmf_run_escan(struct brcmf_cfg80211_info* cfg, struct brcmf_if* ifp,
const wlanif_scan_req_t* request) {
if (request == nullptr) {
return ZX_ERR_INVALID_ARGS;
}
// Validate dwell times
if (request->min_channel_time == 0 || request->max_channel_time < request->min_channel_time) {
BRCMF_ERR("Invalid dwell times in escan request min: %u max: %u", request->min_channel_time,
request->max_channel_time);
return ZX_ERR_INVALID_ARGS;
}
// Validate channel count
if (request->num_channels > WLAN_INFO_CHANNEL_LIST_MAX_CHANNELS) {
BRCMF_ERR("Number of channels in escan request (%zu) exceeds maximum (%d)",
request->num_channels, WLAN_INFO_CHANNEL_LIST_MAX_CHANNELS);
return ZX_ERR_INVALID_ARGS;
}
// Validate ssid count
if (request->num_ssids > WLAN_SCAN_MAX_SSIDS) {
BRCMF_ERR("Number of SSIDs in escan request (%zu) exceeds maximum (%d)", request->num_ssids,
WLAN_SCAN_MAX_SSIDS);
return ZX_ERR_INVALID_ARGS;
}
// Calculate space needed for parameters
size_t params_size = brcmf_escan_params_size(request->num_channels, request->num_ssids);
// Validate command size
size_t total_cmd_size = params_size + sizeof("escan");
if (total_cmd_size >= BRCMF_DCMD_MEDLEN) {
BRCMF_ERR("Escan params size (%zu) exceeds command max capacity (%d)", total_cmd_size,
BRCMF_DCMD_MEDLEN);
return ZX_ERR_INVALID_ARGS;
}
zx_status_t err = ZX_OK;
bcme_status_t fw_err = BCME_OK;
BRCMF_DBG(SCAN, "E-SCAN START");
struct brcmf_escan_params_le* params = static_cast<decltype(params)>(calloc(1, params_size));
if (!params) {
err = ZX_ERR_NO_MEMORY;
goto exit;
}
err = brcmf_escan_prep(cfg, &params->params_le, request);
if (err != ZX_OK) {
BRCMF_ERR("escan preparation failed");
goto exit;
}
params->version = BRCMF_ESCAN_REQ_VERSION;
params->action = WL_ESCAN_ACTION_START;
params->sync_id = 0x1234;
if (brcmf_feat_is_enabled(ifp, BRCMF_FEAT_SCAN_RANDOM_MAC) &&
(params->params_le.scan_type == BRCMF_SCANTYPE_ACTIVE) &&
!brcmf_test_bit_in_array(BRCMF_VIF_STATUS_CONNECTED, &ifp->vif->sme_state)) {
if ((err = brcmf_dev_escan_set_randmac(ifp)) != ZX_OK) {
BRCMF_ERR("Failed to set random mac for active scan (%s), using interface mac",
zx_status_get_string(err));
}
}
err = brcmf_fil_iovar_data_set(ifp, "escan", params, params_size, &fw_err);
if (err != ZX_OK) {
if (err == ZX_ERR_UNAVAILABLE) {
BRCMF_ERR("system busy : escan canceled sme state: 0x%lx\n",
atomic_load(&ifp->vif->sme_state));
} else {
BRCMF_ERR("escan failed: %s, fw err %s\n", zx_status_get_string(err),
brcmf_fil_get_errstr(fw_err));
}
}
exit:
free(params);
return err;
}
static zx_status_t brcmf_do_escan(struct brcmf_if* ifp, const wlanif_scan_req_t* req) {
struct brcmf_cfg80211_info* cfg = ifp->drvr->config;
zx_status_t err;
struct escan_info* escan = &cfg->escan_info;
BRCMF_DBG(SCAN, "Enter");
escan->ifp = ifp;
escan->escan_state = WL_ESCAN_STATE_SCANNING;
brcmf_scan_config_mpc(ifp, 0);
err = escan->run(cfg, ifp, req);
if (err != ZX_OK) {
brcmf_scan_config_mpc(ifp, 1);
}
return err;
}
zx_status_t brcmf_cfg80211_scan(struct net_device* ndev, const wlanif_scan_req_t* req) {
zx_status_t err;
BRCMF_DBG(TRACE, "Enter");
struct wireless_dev* wdev = ndev_to_wdev(ndev);
struct brcmf_cfg80211_vif* vif = containerof(wdev, struct brcmf_cfg80211_vif, wdev);
if (!check_vif_up(vif)) {
BRCMF_DBG(TEMP, "Vif not up");
return ZX_ERR_IO;
}
struct brcmf_cfg80211_info* cfg = ndev_to_if(ndev)->drvr->config;
struct net_device* softap_ndev = cfg_to_softap_ndev(cfg);
if (brcmf_test_bit_in_array(BRCMF_SCAN_STATUS_BUSY, &cfg->scan_status)) {
BRCMF_ERR("Scanning already: status (%lu)\n", cfg->scan_status.load());
return ZX_ERR_UNAVAILABLE;
}
if (brcmf_test_bit_in_array(BRCMF_SCAN_STATUS_ABORT, &cfg->scan_status)) {
BRCMF_ERR("Scanning being aborted: status (%lu)\n", cfg->scan_status.load());
return ZX_ERR_UNAVAILABLE;
}
if (brcmf_test_bit_in_array(BRCMF_SCAN_STATUS_SUPPRESS, &cfg->scan_status)) {
BRCMF_ERR("Scanning suppressed: status (%lu)\n", cfg->scan_status.load());
return ZX_ERR_UNAVAILABLE;
}
if (brcmf_test_bit_in_array(BRCMF_VIF_STATUS_CONNECTING, &vif->sme_state)) {
BRCMF_ERR("Scan request suppressed: connect in progress (status: %lu)\n",
vif->sme_state.load());
return ZX_ERR_UNAVAILABLE;
}
if (softap_ndev != nullptr && brcmf_test_bit_in_array(BRCMF_VIF_STATUS_AP_START_PENDING,
&ndev_to_vif(softap_ndev)->sme_state)) {
BRCMF_INFO("AP start request in progress, rejecting scan request, a retry is expected.");
return ZX_ERR_UNAVAILABLE;
}
BRCMF_DBG(SCAN, "START ESCAN\n");
cfg->scan_request = req;
brcmf_set_bit_in_array(BRCMF_SCAN_STATUS_BUSY, &cfg->scan_status);
cfg->escan_info.run = brcmf_run_escan;
err = brcmf_do_escan(vif->ifp, req);
if (err != ZX_OK) {
goto scan_out;
}
/* Arm scan timeout timer */
cfg->escan_timer->Start(ZX_MSEC(BRCMF_ESCAN_TIMER_INTERVAL_MS));
return ZX_OK;
scan_out:
BRCMF_ERR("scan error (%d)", err);
brcmf_clear_bit_in_array(BRCMF_SCAN_STATUS_BUSY, &cfg->scan_status);
cfg->scan_request = nullptr;
return err;
}
static void brcmf_init_prof(struct brcmf_cfg80211_profile* prof) { memset(prof, 0, sizeof(*prof)); }
static zx_status_t brcmf_set_pmk(struct brcmf_if* ifp, const uint8_t* pmk_data, uint16_t pmk_len) {
struct brcmf_wsec_pmk_le pmk;
int i;
zx_status_t err;
/* convert to firmware key format */
pmk.key_len = pmk_len << 1;
pmk.flags = BRCMF_WSEC_PASSPHRASE;
for (i = 0; i < pmk_len; i++) {
// TODO(cphoenix): Make sure handling of pmk keys is consistent with their being
// binary values, not ASCII chars.
snprintf((char*)&pmk.key[2 * i], 3, "%02x", pmk_data[i]);
}
/* store psk in firmware */
err = brcmf_fil_cmd_data_set(ifp, BRCMF_C_SET_WSEC_PMK, &pmk, sizeof(pmk), nullptr);
if (err != ZX_OK) {
BRCMF_ERR("failed to change PSK in firmware (len=%u)", pmk_len);
}
return err;
}
static void brcmf_notify_deauth(struct net_device* ndev, const uint8_t peer_sta_address[ETH_ALEN]) {
std::shared_lock<std::shared_mutex> guard(ndev->if_proto_lock);
if (ndev->if_proto.ops == nullptr) {
BRCMF_DBG(WLANIF, "interface stopped -- skipping deauth confirm callback");
return;
}
wlanif_deauth_confirm_t resp = {};
memcpy(resp.peer_sta_address, peer_sta_address, ETH_ALEN);
BRCMF_DBG(WLANIF, "Sending deauth confirm to SME. address: " MAC_FMT_STR "",
MAC_FMT_ARGS(peer_sta_address));
wlanif_impl_ifc_deauth_conf(&ndev->if_proto, &resp);
}
static void brcmf_notify_disassoc(struct net_device* ndev, zx_status_t status) {
std::shared_lock<std::shared_mutex> guard(ndev->if_proto_lock);
if (ndev->if_proto.ops == nullptr) {
BRCMF_DBG(WLANIF, "interface stopped -- skipping disassoc confirm callback");
return;
}
wlanif_disassoc_confirm_t resp = {};
resp.status = status;
BRCMF_DBG(WLANIF, "Sending disassoc confirm to SME. status: %" PRIu32 "", status);
wlanif_impl_ifc_disassoc_conf(&ndev->if_proto, &resp);
}
// Send deauth_ind to SME (can be from client or softap)
static void brcmf_notify_deauth_ind(net_device* ndev, const uint8_t mac_addr[ETH_ALEN],
uint16_t reason, bool locally_initiated) {
std::shared_lock<std::shared_mutex> guard(ndev->if_proto_lock);
if (ndev->if_proto.ops == nullptr) {
BRCMF_DBG(WLANIF, "interface stopped -- skipping deauth ind callback");
return;
}
wlanif_deauth_indication_t ind = {};
BRCMF_DBG(WLANIF,
"Link Down: Sending deauth ind to SME. address: " MAC_FMT_STR
", "
"reason: %" PRIu16,
MAC_FMT_ARGS(mac_addr), reason);
memcpy(ind.peer_sta_address, mac_addr, ETH_ALEN);
ind.reason_code = reason;
ind.locally_initiated = locally_initiated;
wlanif_impl_ifc_deauth_ind(&ndev->if_proto, &ind);
}
// Send disassoc_ind to SME (can be from client or softap)
static void brcmf_notify_disassoc_ind(net_device* ndev, const uint8_t mac_addr[ETH_ALEN],
uint16_t reason, bool locally_initiated) {
std::shared_lock<std::shared_mutex> guard(ndev->if_proto_lock);
if (ndev->if_proto.ops == nullptr) {
BRCMF_DBG(WLANIF, "interface stopped -- skipping disassoc ind callback");
return;
}
wlanif_disassoc_indication_t ind = {};
BRCMF_DBG(WLANIF,
"Link Down: Sending disassoc ind to SME. address: " MAC_FMT_STR
", "
"reason: %" PRIu16,
MAC_FMT_ARGS(mac_addr), reason);
memcpy(ind.peer_sta_address, mac_addr, ETH_ALEN);
ind.reason_code = reason;
ind.locally_initiated = locally_initiated;
wlanif_impl_ifc_disassoc_ind(&ndev->if_proto, &ind);
}
static void cfg80211_disconnected(struct brcmf_cfg80211_vif* vif, uint16_t event_reason,
uint16_t event_code) {
struct net_device* ndev = vif->wdev.netdev;
std::shared_lock<std::shared_mutex> guard(ndev->if_proto_lock);
if (ndev->if_proto.ops == nullptr) {
BRCMF_DBG(WLANIF, "interface stopped -- skipping link down callback");
return;
}
struct brcmf_cfg80211_info* cfg = vif->ifp->drvr->config;
BRCMF_DBG(CONN, "Link Down: address: " MAC_FMT_STR ", SME reason: %d",
MAC_FMT_ARGS(vif->profile.bssid), event_reason);
const bool sme_initiated_deauth =
cfg->disconnect_mode == BRCMF_DISCONNECT_DEAUTH &&
(event_code == BRCMF_E_DEAUTH || event_code == BRCMF_E_DISASSOC);
const bool sme_initiated_disassoc =
cfg->disconnect_mode == BRCMF_DISCONNECT_DISASSOC &&
(event_code == BRCMF_E_DEAUTH || event_code == BRCMF_E_DISASSOC);
if (sme_initiated_deauth) {
brcmf_notify_deauth(ndev, vif->profile.bssid);
} else if (sme_initiated_disassoc) {
brcmf_notify_disassoc(ndev, ZX_OK);
} else {
const bool locally_initiated = event_code == BRCMF_E_DEAUTH || event_code == BRCMF_E_DISASSOC ||
event_code == BRCMF_E_LINK;
// BRCMF_E_DEAUTH is unlikely if not SME-initiated
if (event_code == BRCMF_E_DEAUTH || event_code == BRCMF_E_DEAUTH_IND) {
brcmf_notify_deauth_ind(ndev, vif->profile.bssid, event_reason, locally_initiated);
} else {
// This is a catch-all case - could be E_DISASSOC, E_DISASSOC_IND, E_LINK or IF delete
brcmf_notify_disassoc_ind(ndev, vif->profile.bssid, event_reason, locally_initiated);
}
}
cfg->disconnect_mode = BRCMF_DISCONNECT_NONE;
}
static void brcmf_link_down(struct brcmf_cfg80211_vif* vif, uint16_t event_reason,
uint16_t event_code) {
struct brcmf_cfg80211_info* cfg = vif->ifp->drvr->config;
zx_status_t err = ZX_OK;
BRCMF_DBG(TRACE, "Enter\n");
if (brcmf_test_and_clear_bit_in_array(BRCMF_VIF_STATUS_CONNECTED, &vif->sme_state)) {
BRCMF_DBG(INFO, "Call WLC_DISASSOC to stop excess roaming\n ");
bcme_status_t fwerr = BCME_OK;
err = brcmf_fil_cmd_data_set(vif->ifp, BRCMF_C_DISASSOC, nullptr, 0, &fwerr);
if (err != ZX_OK) {
BRCMF_ERR("WLC_DISASSOC failed: %s, fw err %s\n", zx_status_get_string(err),
brcmf_fil_get_errstr(fwerr));
}
if (vif->wdev.iftype == WLAN_INFO_MAC_ROLE_CLIENT) {
cfg80211_disconnected(vif, event_reason, event_code);
}
}
brcmf_clear_bit_in_array(BRCMF_VIF_STATUS_CONNECTING, &vif->sme_state);
brcmf_clear_bit_in_array(BRCMF_VIF_STATUS_CONNECTED, &vif->sme_state);
brcmf_clear_bit_in_array(BRCMF_SCAN_STATUS_SUPPRESS, &cfg->scan_status);
brcmf_btcoex_set_mode(vif, BRCMF_BTCOEX_ENABLED, 0);
if (vif->profile.use_fwsup != BRCMF_PROFILE_FWSUP_NONE) {
brcmf_set_pmk(vif->ifp, nullptr, 0);
vif->profile.use_fwsup = BRCMF_PROFILE_FWSUP_NONE;
}
BRCMF_DBG(TRACE, "Exit");
}
static zx_status_t brcmf_set_auth_type(struct net_device* ndev, uint8_t auth_type) {
brcmf_if* ifp = ndev_to_if(ndev);
int32_t val = 0;
zx_status_t status = ZX_OK;
switch (auth_type) {
case WLAN_AUTH_TYPE_OPEN_SYSTEM:
val = BRCMF_AUTH_MODE_OPEN;
break;
case WLAN_AUTH_TYPE_SHARED_KEY:
// When asked to use a shared key (which should only happen for WEP), we will direct the
// firmware to use auto-detect, which will fall back on open WEP if shared WEP fails to
// succeed. This was chosen to allow us to avoid implementing WEP auto-detection at higher
// levels of the wlan stack.
val = BRCMF_AUTH_MODE_AUTO;
break;
case WLAN_AUTH_TYPE_SAE:
val = BRCMF_AUTH_MODE_SAE;
break;
default:
return ZX_ERR_NOT_SUPPORTED;
}
BRCMF_DBG(CONN, "setting auth to %d", val);
status = brcmf_fil_bsscfg_int_set(ifp, "auth", val);
if (status != ZX_OK) {
BRCMF_ERR("set auth failed (%s)", zx_status_get_string(status));
}
return status;
}
static bool brcmf_valid_wpa_oui(uint8_t* oui, bool is_rsn_ie) {
if (is_rsn_ie) {
return (memcmp(oui, RSN_OUI, TLV_OUI_LEN) == 0);
}
return (memcmp(oui, MSFT_OUI, TLV_OUI_LEN) == 0);
}
static zx_status_t brcmf_configure_wpaie(struct brcmf_if* ifp, const struct brcmf_vs_tlv* wpa_ie,
bool is_rsn_ie, bool is_ap) {
uint16_t count;
zx_status_t err = ZX_OK;
int32_t len;
uint32_t i;
uint32_t wsec;
uint32_t pval = 0;
uint32_t gval = 0;
uint32_t wpa_auth = 0;
uint32_t offset;
uint8_t* data;
uint16_t rsn_cap;
uint32_t wme_bss_disable;
uint32_t mfp;
BRCMF_DBG(TRACE, "Enter");
if (wpa_ie == nullptr) {
goto exit;
}
len = wpa_ie->len + TLV_HDR_LEN;
data = (uint8_t*)wpa_ie;
offset = TLV_HDR_LEN;
if (!is_rsn_ie) {
offset += VS_IE_FIXED_HDR_LEN;
} else {
offset += WPA_IE_VERSION_LEN;
}
/* check for multicast cipher suite */
if ((int32_t)offset + WPA_IE_MIN_OUI_LEN > len) {
err = ZX_ERR_INVALID_ARGS;
BRCMF_ERR("no multicast cipher suite");
goto exit;
}
if (!brcmf_valid_wpa_oui(&data[offset], is_rsn_ie)) {
err = ZX_ERR_INVALID_ARGS;
BRCMF_ERR("invalid OUI");
goto exit;
}
offset += TLV_OUI_LEN;
/* pick up multicast cipher */
switch (data[offset]) {
case WPA_CIPHER_NONE:
BRCMF_DBG(CONN, "MCAST WPA CIPHER NONE");
gval = WSEC_NONE;
break;
case WPA_CIPHER_WEP_40:
case WPA_CIPHER_WEP_104:
BRCMF_DBG(CONN, "MCAST WPA CIPHER WEP40/104");
gval = WEP_ENABLED;
break;
case WPA_CIPHER_TKIP:
BRCMF_DBG(CONN, "MCAST WPA CIPHER TKIP");
gval = TKIP_ENABLED;
break;
case WPA_CIPHER_CCMP_128:
BRCMF_DBG(CONN, "MCAST WPA CIPHER CCMP 128");
gval = AES_ENABLED;
break;
default:
err = ZX_ERR_INVALID_ARGS;
BRCMF_ERR("Invalid multi cast cipher info");
goto exit;
}
offset++;
/* walk thru unicast cipher list and pick up what we recognize */
count = data[offset] + (data[offset + 1] << 8);
offset += WPA_IE_SUITE_COUNT_LEN;
/* Check for unicast suite(s) */
if ((int32_t)(offset + (WPA_IE_MIN_OUI_LEN * count)) > len) {
err = ZX_ERR_INVALID_ARGS;
BRCMF_ERR("no unicast cipher suite");
goto exit;
}
for (i = 0; i < count; i++) {
if (!brcmf_valid_wpa_oui(&data[offset], is_rsn_ie)) {
err = ZX_ERR_INVALID_ARGS;
BRCMF_ERR("ivalid OUI");
goto exit;
}
offset += TLV_OUI_LEN;
switch (data[offset]) {
case WPA_CIPHER_NONE:
BRCMF_DBG(CONN, "UCAST WPA CIPHER NONE");
break;
case WPA_CIPHER_WEP_40:
case WPA_CIPHER_WEP_104:
BRCMF_DBG(CONN, "UCAST WPA CIPHER WEP 40/104");
pval |= WEP_ENABLED;
break;
case WPA_CIPHER_TKIP:
BRCMF_DBG(CONN, "UCAST WPA CIPHER TKIP");
pval |= TKIP_ENABLED;
break;
case WPA_CIPHER_CCMP_128:
BRCMF_DBG(CONN, "UCAST WPA CIPHER CCMP 128");
pval |= AES_ENABLED;
break;
default:
BRCMF_DBG(CONN, "Invalid unicast security info");
}
offset++;
}
/* walk thru auth management suite list and pick up what we recognize */
count = data[offset] + (data[offset + 1] << 8);
offset += WPA_IE_SUITE_COUNT_LEN;
/* Check for auth key management suite(s) */
if ((int32_t)(offset + (WPA_IE_MIN_OUI_LEN * count)) > len) {
err = ZX_ERR_INVALID_ARGS;
BRCMF_ERR("no auth key mgmt suite");
goto exit;
}
for (i = 0; i < count; i++) {
if (!brcmf_valid_wpa_oui(&data[offset], is_rsn_ie)) {
err = ZX_ERR_INVALID_ARGS;
BRCMF_ERR("ivalid OUI");
goto exit;
}
offset += TLV_OUI_LEN;
switch (data[offset]) {
case RSN_AKM_NONE:
BRCMF_DBG(CONN, "RSN_AKM_NONE");
wpa_auth |= WPA_AUTH_NONE;
break;
case RSN_AKM_UNSPECIFIED:
BRCMF_DBG(CONN, "RSN_AKM_UNSPECIFIED");
is_rsn_ie ? (wpa_auth |= WPA2_AUTH_UNSPECIFIED) : (wpa_auth |= WPA_AUTH_UNSPECIFIED);
break;
case RSN_AKM_PSK:
BRCMF_DBG(CONN, "RSN_AKM_PSK");
is_rsn_ie ? (wpa_auth |= WPA2_AUTH_PSK) : (wpa_auth |= WPA_AUTH_PSK);
break;
case RSN_AKM_SHA256_PSK:
BRCMF_DBG(CONN, "RSN_AKM_MFP_PSK");
wpa_auth |= WPA2_AUTH_PSK_SHA256;
break;
case RSN_AKM_SHA256_1X:
BRCMF_DBG(CONN, "RSN_AKM_MFP_1X");
wpa_auth |= WPA2_AUTH_1X_SHA256;
break;
case RSN_AKM_SAE_PSK:
BRCMF_DBG(CONN, "RSN_AKM_SAE");
wpa_auth |= WPA3_AUTH_SAE_PSK;
break;
default:
BRCMF_DBG(CONN, "Invalid key mgmt info, the auth mgmt suite is %u", data[offset]);
}
offset++;
}
/* Don't set SES_OW_ENABLED for now (since we don't support WPS yet) */
wsec = (pval | gval);
BRCMF_INFO("WSEC: 0x%x WPA AUTH: 0x%x", wsec, wpa_auth);
/* set wsec */
err = brcmf_fil_bsscfg_int_set(ifp, "wsec", wsec);
if (err != ZX_OK) {
BRCMF_ERR("wsec error %d", err);
goto exit;
}
mfp = BRCMF_MFP_NONE;
if (is_rsn_ie) {
if (is_ap) {
wme_bss_disable = 1;
if (((int32_t)offset + RSN_CAP_LEN) <= len) {
rsn_cap = data[offset] + (data[offset + 1] << 8);
if (rsn_cap & RSN_CAP_PTK_REPLAY_CNTR_MASK) {
wme_bss_disable = 0;
}
if (rsn_cap & RSN_CAP_MFPR_MASK) {
BRCMF_DBG(TRACE, "MFP Required");
mfp = BRCMF_MFP_REQUIRED;
/* Firmware only supports mfp required in
* combination with WPA2_AUTH_PSK_SHA256 or
* WPA2_AUTH_1X_SHA256.
*/
if (!(wpa_auth & (WPA2_AUTH_PSK_SHA256 | WPA2_AUTH_1X_SHA256))) {
err = ZX_ERR_INVALID_ARGS;
goto exit;
}
/* Firmware has requirement that WPA2_AUTH_PSK/
* WPA2_AUTH_UNSPECIFIED be set, if SHA256 OUI
* is to be included in the rsn ie.
*/
if (wpa_auth & WPA2_AUTH_PSK_SHA256) {
wpa_auth |= WPA2_AUTH_PSK;
} else if (wpa_auth & WPA2_AUTH_1X_SHA256) {
wpa_auth |= WPA2_AUTH_UNSPECIFIED;
}
} else if (rsn_cap & RSN_CAP_MFPC_MASK) {
BRCMF_DBG(TRACE, "MFP Capable");
mfp = BRCMF_MFP_CAPABLE;
}
}
offset += RSN_CAP_LEN;
/* set wme_bss_disable to sync RSN Capabilities */
err = brcmf_fil_bsscfg_int_set(ifp, "wme_bss_disable", wme_bss_disable);
if (err != ZX_OK) {
BRCMF_ERR("wme_bss_disable error %d", err);
goto exit;
}
/* Skip PMKID cnt as it is know to be 0 for AP. */
offset += RSN_PMKID_COUNT_LEN;
/* See if there is BIP wpa suite left for MFP */
if (brcmf_feat_is_enabled(ifp, BRCMF_FEAT_MFP) &&
((int32_t)(offset + WPA_IE_MIN_OUI_LEN) <= len)) {
err = brcmf_fil_bsscfg_data_set(ifp, "bip", &data[offset], WPA_IE_MIN_OUI_LEN);
if (err != ZX_OK) {
BRCMF_ERR("bip error %d", err);
goto exit;
}
}
} else if (wpa_auth & WPA3_AUTH_SAE_PSK) {
// Set mfp to capable if it's a wpa3 assocation.
mfp = BRCMF_MFP_CAPABLE;
}
}
/* Configure MFP, just a reminder, this needs to go after wsec otherwise the wsec command
* will overwrite the values set by MFP
*/
if (brcmf_feat_is_enabled(ifp, BRCMF_FEAT_MFP)) {
err = brcmf_fil_bsscfg_int_set(ifp, "mfp", mfp);
if (err != ZX_OK) {
BRCMF_ERR("mfp error %s", zx_status_get_string(err));
goto exit;
}
}
/* set upper-layer auth */
err = brcmf_fil_bsscfg_int_set(ifp, "wpa_auth", wpa_auth);
if (err != ZX_OK) {
BRCMF_ERR("wpa_auth error %d", err);
goto exit;
}
exit:
return err;
}
static zx_status_t brcmf_configure_opensecurity(struct brcmf_if* ifp) {
zx_status_t err;
int32_t wpa_val;
/* set wsec */
BRCMF_DBG(CONN, "Setting wsec to 0");
err = brcmf_fil_bsscfg_int_set(ifp, "wsec", 0);
if (err != ZX_OK) {
BRCMF_ERR("wsec error %d", err);
return err;
}
/* set upper-layer auth */
wpa_val = WPA_AUTH_DISABLED;
BRCMF_DBG(CONN, "Setting wpa_auth to %d", wpa_val);
err = brcmf_fil_bsscfg_int_set(ifp, "wpa_auth", wpa_val);
if (err != ZX_OK) {
BRCMF_ERR("wpa_auth error %d", err);
return err;
}
return ZX_OK;
}
// Retrieve information about the station with the specified MAC address. Note that
// association ID is only available when operating in AP mode (for our clients).
static zx_status_t brcmf_cfg80211_get_station(struct net_device* ndev, const uint8_t* mac,
struct brcmf_sta_info_le* sta_info_le) {
struct brcmf_if* ifp = ndev_to_if(ndev);
zx_status_t err = ZX_OK;
BRCMF_DBG(TRACE, "Enter, MAC " MAC_FMT_STR, MAC_FMT_ARGS(mac));
if (!check_vif_up(ifp->vif)) {
return ZX_ERR_IO;
}
memset(sta_info_le, 0, sizeof(*sta_info_le));
memcpy(sta_info_le, mac, ETH_ALEN);
// First, see if we have a TDLS peer
err = brcmf_fil_iovar_data_get(ifp, "tdls_sta_info", sta_info_le, sizeof(*sta_info_le), nullptr);
if (err != ZX_OK) {
bcme_status_t fw_err = BCME_OK;
err = brcmf_fil_iovar_data_get(ifp, "sta_info", sta_info_le, sizeof(*sta_info_le), &fw_err);
if (err != ZX_OK) {
BRCMF_ERR("GET STA INFO failed: %s, fw err %s", zx_status_get_string(err),
brcmf_fil_get_errstr(fw_err));
}
}
BRCMF_DBG(TRACE, "Exit");
return err;
}
static inline bool brcmf_tlv_ie_has_msft_type(const uint8_t* ie, uint8_t oui_type) {
return (ie[TLV_LEN_OFF] >= TLV_OUI_LEN + TLV_OUI_TYPE_LEN &&
!memcmp(&ie[TLV_BODY_OFF], MSFT_OUI, TLV_OUI_LEN) &&
// The byte after OUI is OUI type
ie[TLV_BODY_OFF + TLV_OUI_LEN] == oui_type);
}
static struct brcmf_vs_tlv* brcmf_find_wpaie(const uint8_t* ie_buf, uint32_t ie_len) {
size_t offset = 0;
while (offset < ie_len) {
uint8_t type = ie_buf[offset];
uint8_t length = ie_buf[offset + TLV_LEN_OFF];
if (type == WLAN_IE_TYPE_VENDOR_SPECIFIC) {
if (brcmf_tlv_ie_has_msft_type(ie_buf + offset, WPA_OUI_TYPE)) {
BRCMF_DBG(CONN, "Found WPA IE");
return (struct brcmf_vs_tlv*)(ie_buf + offset);
}
}
offset += length + TLV_HDR_LEN;
}
return nullptr;
}
void set_assoc_conf_wmm_param(const brcmf_cfg80211_info* cfg, wlanif_assoc_confirm_t* confirm) {
confirm->wmm_param_present = false;
uint8_t* assoc_resp_ie = cfg->conn_info.resp_ie;
size_t assoc_resp_ie_len =
(size_t)cfg->conn_info.resp_ie_len >= 0 ? cfg->conn_info.resp_ie_len : 0;
size_t offset = 0;
while (offset < assoc_resp_ie_len) {
uint8_t type = assoc_resp_ie[offset];
uint8_t len = assoc_resp_ie[offset + TLV_LEN_OFF];
if (type == WLAN_IE_TYPE_VENDOR_SPECIFIC) {
uint8_t wmm_param_hdr[] = {
0x00, 0x50, 0xf2, // MSFT OUI
0x02, // WMM OUI type
0x01, 0x01, // WMM param subtype & version
};
if (len >= sizeof(wmm_param_hdr) &&
!memcmp(assoc_resp_ie + offset + TLV_HDR_LEN, wmm_param_hdr, sizeof(wmm_param_hdr))) {
if (len - sizeof(wmm_param_hdr) == WLAN_WMM_PARAM_LEN &&
offset + TLV_HDR_LEN + len <= assoc_resp_ie_len) {
memcpy(&confirm->wmm_param, &assoc_resp_ie[offset + TLV_HDR_LEN + sizeof(wmm_param_hdr)],
WLAN_WMM_PARAM_LEN);
confirm->wmm_param_present = true;
break;
}
}
}
offset += len + TLV_HDR_LEN;
}
}
void brcmf_return_assoc_result(struct net_device* ndev, uint8_t result_code) {
std::shared_lock<std::shared_mutex> guard(ndev->if_proto_lock);
if (ndev->if_proto.ops == nullptr) {
BRCMF_DBG(WLANIF, "interface stopped -- skipping association callback");
return;
}
struct brcmf_if* ifp = ndev_to_if(ndev);
struct brcmf_cfg80211_info* cfg = ifp->drvr->config;
wlanif_assoc_confirm_t conf;
conf.result_code = result_code;
BRCMF_DBG(TEMP, " * Hard-coding association_id to 42; this will likely break something!");
conf.association_id = 42; // TODO: Use brcmf_cfg80211_get_station() to get aid
set_assoc_conf_wmm_param(cfg, &conf);
BRCMF_DBG(WLANIF, "Sending assoc result to SME. result: %" PRIu8 ", aid: %" PRIu16,
conf.result_code, conf.association_id);
wlanif_impl_ifc_assoc_conf(&ndev->if_proto, &conf);
}
zx_status_t brcmf_cfg80211_connect(struct net_device* ndev, const wlanif_assoc_req_t* req) {
struct brcmf_if* ifp = ndev_to_if(ndev);
struct brcmf_cfg80211_info* cfg = ifp->drvr->config;
struct brcmf_join_params join_params;
uint16_t chanspec;
size_t join_params_size = 0;
const void* ie;
uint32_t ie_len;
zx_status_t err = ZX_OK;
uint32_t ssid_len = 0;
const struct brcmf_vs_tlv* wpa_ie;
bcme_status_t fw_err = BCME_OK;
bool is_rsn_ie = true;
BRCMF_DBG(TRACE, "Enter");
if (!check_vif_up(ifp->vif)) {
return ZX_ERR_IO;
}
// Firmware is already processing a join request. Don't clear the CONNECTING bit because the
// operation is still expected to complete.
if (brcmf_test_bit_in_array(BRCMF_VIF_STATUS_CONNECTING, &ifp->vif->sme_state)) {
err = ZX_ERR_BAD_STATE;
BRCMF_WARN("Connection not possible. Another connection attempt in progress.");
brcmf_return_assoc_result(ndev, WLAN_ASSOC_RESULT_REFUSED_REASON_UNSPECIFIED);
goto done;
}
if (req->rsne_len) {
BRCMF_DBG(CONN, "using RSNE rsn len: %zu", req->rsne_len);
// Pass RSNE to firmware
ie_len = req->rsne_len;
ie = req->rsne;
} else if (req->vendor_ie_len) {
BRCMF_DBG(CONN, "using WPA1 vendor_ie len: %zu", req->vendor_ie_len);
wpa_ie = brcmf_find_wpaie(req->vendor_ie, req->vendor_ie_len);
if (!wpa_ie) {
BRCMF_ERR("No WPA IE found");
return ZX_ERR_INVALID_ARGS;
}
BRCMF_DBG(CONN, "Found WPA IE, len: %d", wpa_ie->len);
is_rsn_ie = false;
ie_len = wpa_ie->len + TLV_HDR_LEN;
ie = wpa_ie;
} else {
// Neither RSNE or WPA1 is set
ie = nullptr;
ie_len = 0;
}
if (ie) {
// Set wpaie only if ie is set
err = brcmf_fil_iovar_data_set(ifp, "wpaie", ie, ie_len, &fw_err);
if (err != ZX_OK) {
BRCMF_ERR("wpaie failed: %s, fw err %s", zx_status_get_string(err),
brcmf_fil_get_errstr(fw_err));
goto fail;
}
}
// TODO(fxbug.dev/29354): We should be getting the IEs from SME. Passing a null entry seems
// to work for now, presumably because the firmware uses its defaults.
err = brcmf_vif_set_mgmt_ie(ifp->vif, BRCMF_VNDR_IE_ASSOCREQ_FLAG, nullptr, 0);
if (err != ZX_OK) {
BRCMF_ERR("Set Assoc REQ IE Failed");
} else {
BRCMF_DBG(TRACE, "Applied Vndr IEs for Assoc request");
}
if (ie_len > 0) {
struct brcmf_vs_tlv* tmp_ie = (struct brcmf_vs_tlv*)ie;
err = brcmf_configure_wpaie(ifp, tmp_ie, is_rsn_ie, false);
if (err != ZX_OK) {
BRCMF_ERR("Failed to install RSNE: %s", zx_status_get_string(err));
goto fail;
}
}
brcmf_set_bit_in_array(BRCMF_VIF_STATUS_CONNECTING, &ifp->vif->sme_state);
chanspec = channel_to_chanspec(&cfg->d11inf, &ifp->bss.chan);
cfg->channel = chanspec;
ssid_len = std::min<uint32_t>(ifp->bss.ssid.len, WLAN_MAX_SSID_LEN);
join_params_size = sizeof(join_params);
memset(&join_params, 0, join_params_size);
memcpy(&join_params.ssid_le.SSID, ifp->bss.ssid.data, ssid_len);
join_params.ssid_le.SSID_len = ssid_len;
memcpy(join_params.params_le.bssid, ifp->bss.bssid, ETH_ALEN);
join_params.params_le.chanspec_num = 1;
join_params.params_le.chanspec_list[0] = chanspec;
BRCMF_DBG(CONN, "Sending join request");
err = brcmf_fil_cmd_data_set(ifp, BRCMF_C_SET_SSID, &join_params, join_params_size, &fw_err);
if (err != ZX_OK) {
BRCMF_ERR("join failed (%d)", err);
} else {
cfg->connect_timer->Start(BRCMF_CONNECT_TIMER_DUR_MS);
}
fail:
if (err != ZX_OK) {
brcmf_clear_bit_in_array(BRCMF_VIF_STATUS_CONNECTING, &ifp->vif->sme_state);
BRCMF_DBG(CONN, "Failed during join: %s", zx_status_get_string(err));
brcmf_return_assoc_result(ndev, WLAN_ASSOC_RESULT_REFUSED_REASON_UNSPECIFIED);
}
done:
BRCMF_DBG(TRACE, "Exit");
return err;
}
static void brcmf_disconnect_done(struct brcmf_cfg80211_info* cfg) {
struct net_device* ndev = cfg_to_ndev(cfg);
struct brcmf_if* ifp = ndev_to_if(ndev);
struct brcmf_cfg80211_profile* profile = &ifp->vif->profile;
BRCMF_DBG(TRACE, "Enter");
if (brcmf_test_and_clear_bit_in_array(BRCMF_VIF_STATUS_DISCONNECTING, &ifp->vif->sme_state)) {
cfg->disconnect_timer->Stop();
if (cfg->disconnect_mode == BRCMF_DISCONNECT_DEAUTH) {
brcmf_notify_deauth(ndev, profile->bssid);
} else {
brcmf_notify_disassoc(ndev, ZX_OK);
}
}
if (!brcmf_feat_is_enabled(ifp, BRCMF_FEAT_MFG)) {
cfg->signal_report_timer->Stop();
ndev->stats = {};
}
BRCMF_DBG(TRACE, "Exit");
}
static zx_status_t brcmf_get_rssi_snr(net_device* ndev, int8_t* rssi_dbm, int8_t* snr_db) {
struct brcmf_if* ifp = ndev_to_if(ndev);
bcme_status_t fw_err = BCME_OK;
int32_t rssi, snr;
*rssi_dbm = *snr_db = 0;
zx_status_t status = brcmf_fil_cmd_data_get(ifp, BRCMF_C_GET_RSSI, &rssi, sizeof(rssi), &fw_err);
if (status != ZX_OK) {
BRCMF_ERR("could not get rssi: %s, fw err %s", zx_status_get_string(status),
brcmf_fil_get_errstr(fw_err));
return status;
}
status = brcmf_fil_iovar_data_get(ifp, "snr", &snr, sizeof(snr), &fw_err);
if (status != ZX_OK) {
BRCMF_ERR("could not get snr: %s, fw err %s", zx_status_get_string(status),
brcmf_fil_get_errstr(fw_err));
return status;
}
*rssi_dbm = rssi;
*snr_db = snr;
return status;
}
static void cfg80211_signal_ind(net_device* ndev) {
struct brcmf_if* ifp = ndev_to_if(ndev);
brcmf_cfg80211_info* cfg = ifp->drvr->config;
std::shared_lock<std::shared_mutex> guard(ndev->if_proto_lock);
if (ndev->if_proto.ops == nullptr) {
BRCMF_DBG(WLANIF, "interface stopped -- skipping signal report indication callback");
// Stop the timer
cfg->signal_report_timer->Stop();
return;
}
// Send signal report indication only if client is in connected state
if (brcmf_test_bit_in_array(BRCMF_VIF_STATUS_CONNECTED, &ifp->vif->sme_state)) {
wlanif_signal_report_indication signal_ind;
int8_t rssi, snr;
if (brcmf_get_rssi_snr(ndev, &rssi, &snr) == ZX_OK) {
signal_ind.rssi_dbm = rssi;
signal_ind.snr_db = snr;
// Store the value in ndev (dumped out when link goes down)
ndev->last_known_rssi_dbm = rssi;
ndev->last_known_snr_db = snr;
wlanif_impl_ifc_signal_report(&ndev->if_proto, &signal_ind);
}
} else if (!brcmf_feat_is_enabled(ifp, BRCMF_FEAT_MFG)) {
// If client is not connected, stop the timer
cfg->signal_report_timer->Stop();
}
}
static void brcmf_connect_timeout(struct brcmf_cfg80211_info* cfg) {
cfg->pub->irq_callback_lock.lock();
BRCMF_DBG(TRACE, "Enter");
EXEC_TIMEOUT_WORKER(connect_timeout_work);
cfg->pub->irq_callback_lock.unlock();
}
static void brcmf_signal_report_worker(WorkItem* work) {
struct brcmf_cfg80211_info* cfg =
containerof(work, struct brcmf_cfg80211_info, signal_report_work);
struct net_device* ndev = cfg_to_ndev(cfg);
cfg80211_signal_ind(ndev);
}
static void brcmf_signal_report_timeout(struct brcmf_cfg80211_info* cfg) {
cfg->pub->irq_callback_lock.lock();
BRCMF_DBG(TRACE, "Enter");
// If it's for SIM tests, won't enqueue.
EXEC_TIMEOUT_WORKER(signal_report_work);
cfg->pub->irq_callback_lock.unlock();
}
static void brcmf_disconnect_timeout_worker(WorkItem* work) {
struct brcmf_cfg80211_info* cfg =
containerof(work, struct brcmf_cfg80211_info, disconnect_timeout_work);
brcmf_disconnect_done(cfg);
}
static void brcmf_disconnect_timeout(struct brcmf_cfg80211_info* cfg) {
cfg->pub->irq_callback_lock.lock();
BRCMF_DBG(TRACE, "Enter");
// If it's for SIM tests, won't enqueue.
EXEC_TIMEOUT_WORKER(disconnect_timeout_work);
cfg->pub->irq_callback_lock.unlock();
}
static zx_status_t brcmf_cfg80211_disconnect(struct net_device* ndev,
const uint8_t peer_sta_address[ETH_ALEN],
uint16_t reason_code, bool deauthenticate) {
struct brcmf_if* ifp = ndev_to_if(ndev);
struct brcmf_cfg80211_profile* profile = &ifp->vif->profile;
struct brcmf_cfg80211_info* cfg = ifp->drvr->config;
struct brcmf_scb_val_le scbval;
zx_status_t status = ZX_OK;
bcme_status_t fw_err = BCME_OK;
BRCMF_DBG(TRACE, "Enter. Reason code = %d", reason_code);
if (!check_vif_up(ifp->vif)) {
status = ZX_ERR_IO;
goto done;
}
if (!brcmf_test_bit_in_array(BRCMF_VIF_STATUS_CONNECTED, &ifp->vif->sme_state) &&
!brcmf_test_bit_in_array(BRCMF_VIF_STATUS_CONNECTING, &ifp->vif->sme_state)) {
status = ZX_ERR_BAD_STATE;
goto done;
}
if (memcmp(peer_sta_address, profile->bssid, ETH_ALEN)) {
BRCMF_ERR(
"peer_sta_address is not matching bssid in brcmf_cfg80211_profile. "
"peer_sta_address:" MAC_FMT_STR ", bssid in profile:" MAC_FMT_STR "",
MAC_FMT_ARGS(peer_sta_address), MAC_FMT_ARGS(profile->bssid));
status = ZX_ERR_INVALID_ARGS;
goto done;
}
brcmf_clear_bit_in_array(BRCMF_VIF_STATUS_CONNECTED, &ifp->vif->sme_state);
brcmf_clear_bit_in_array(BRCMF_VIF_STATUS_CONNECTING, &ifp->vif->sme_state);
BRCMF_DBG(CONN, "Disconnecting");
// Set the timer before notifying firmware as this thread might get preempted to
// handle the response event back from firmware. Timer can be stopped if the command
// fails.
cfg->disconnect_timer->Start(BRCMF_DISCONNECT_TIMER_DUR_MS);
memcpy(&scbval.ea, peer_sta_address, ETH_ALEN);
scbval.val = reason_code;
cfg->disconnect_mode = deauthenticate ? BRCMF_DISCONNECT_DEAUTH : BRCMF_DISCONNECT_DISASSOC;
brcmf_set_bit_in_array(BRCMF_VIF_STATUS_DISCONNECTING, &ifp->vif->sme_state);
status = brcmf_fil_cmd_data_set(ifp, BRCMF_C_DISASSOC, &scbval, sizeof(scbval), &fw_err);
if (status != ZX_OK) {
BRCMF_ERR("Failed to disassociate: %s, fw err %s", zx_status_get_string(status),
brcmf_fil_get_errstr(fw_err));
cfg->disconnect_timer->Stop();
}
done:
BRCMF_DBG(TRACE, "Exit");
return status;
}
static zx_status_t brcmf_cfg80211_del_key(struct net_device* ndev, uint8_t key_idx) {
struct brcmf_if* ifp = ndev_to_if(ndev);
struct brcmf_wsec_key* key;
zx_status_t err;
BRCMF_DBG(TRACE, "Enter");
BRCMF_DBG(CONN, "key index (%d)", key_idx);
if (!check_vif_up(ifp->vif)) {
return ZX_ERR_IO;
}
if (key_idx >= BRCMF_MAX_DEFAULT_KEYS) {
/* we ignore this key index in this case */
return ZX_ERR_INVALID_ARGS;
}
key = &ifp->vif->profile.key[key_idx];
if (key->algo == CRYPTO_ALGO_OFF) {
BRCMF_DBG(CONN, "Ignore clearing of (never configured) key");
return ZX_ERR_BAD_STATE;
}
memset(key, 0, sizeof(*key));
key->index = (uint32_t)key_idx;
key->flags = BRCMF_PRIMARY_KEY;
/* Clear the key/index */
err = send_key_to_dongle(ifp, key);
BRCMF_DBG(TRACE, "Exit");
return err;
}
static zx_status_t brcmf_cfg80211_add_key(struct net_device* ndev,
const set_key_descriptor_t* req) {
struct brcmf_if* ifp = ndev_to_if(ndev);
struct brcmf_wsec_key* key;
int32_t val;
int32_t wsec;
zx_status_t err;
bool ext_key;
uint8_t key_idx = req->key_id;
const uint8_t* mac_addr = req->address;
BRCMF_DBG(TRACE, "Enter");
BRCMF_DBG(CONN, "key index (%d)", key_idx);
if (!check_vif_up(ifp->vif)) {
return ZX_ERR_IO;
}
if (key_idx >= BRCMF_MAX_DEFAULT_KEYS) {
/* we ignore this key index in this case */
BRCMF_ERR("invalid key index (%d)", key_idx);
return ZX_ERR_INVALID_ARGS;
}
if (req->key_count == 0) {
return brcmf_cfg80211_del_key(ndev, key_idx);
}
if (req->key_count > sizeof(key->data)) {
BRCMF_ERR("Too long key length (%zu)", req->key_count);
return ZX_ERR_INVALID_ARGS;
}
ext_key = false;
if (mac_addr && !address_is_multicast(mac_addr) &&
(req->cipher_suite_type != WPA_CIPHER_WEP_40) &&
(req->cipher_suite_type != WPA_CIPHER_WEP_104)) {
BRCMF_DBG(TRACE, "Ext key, mac " MAC_FMT_STR, MAC_FMT_ARGS(mac_addr));
ext_key = true;
}
key = &ifp->vif->profile.key[key_idx];
memset(key, 0, sizeof(*key));
if ((ext_key) && (!address_is_multicast(mac_addr))) {
memcpy((char*)&key->ea, (void*)mac_addr, ETH_ALEN);
}
key->len = req->key_count;
key->index = key_idx;
memcpy(key->data, req->key_list, key->len);
if (!ext_key) {
key->flags = BRCMF_PRIMARY_KEY;
}
switch (req->cipher_suite_type) {
case WPA_CIPHER_WEP_40:
key->algo = CRYPTO_ALGO_WEP1;
val = WEP_ENABLED;
BRCMF_DBG(CONN, "WPA_CIPHER_WEP_40");
break;
case WPA_CIPHER_WEP_104:
key->algo = CRYPTO_ALGO_WEP128;
val = WEP_ENABLED;
BRCMF_DBG(CONN, "WPA_CIPHER_WEP_104");
break;
case WPA_CIPHER_TKIP:
/* Note: Linux swaps the Tx and Rx MICs in client mode, but this doesn't work for us (see
fxbug.dev/28642). It's unclear why this would be necessary. */
key->algo = CRYPTO_ALGO_TKIP;
val = TKIP_ENABLED;
BRCMF_DBG(CONN, "WPA_CIPHER_TKIP");
break;
case WPA_CIPHER_CMAC_128:
key->algo = CRYPTO_ALGO_AES_CCM;
val = AES_ENABLED;
BRCMF_DBG(CONN, "WPA_CIPHER_CMAC_128");
break;
case WPA_CIPHER_CCMP_128:
key->algo = CRYPTO_ALGO_AES_CCM;
val = AES_ENABLED;
BRCMF_DBG(CONN, "WPA_CIPHER_CCMP_128");
break;
default:
BRCMF_ERR("Unsupported cipher (0x%x)", req->cipher_suite_type);
err = ZX_ERR_INVALID_ARGS;
goto done;
}
BRCMF_DBG(CONN, "key length (%d) key index (%d) algo (%d) flags (%d)", key->len, key->index,
key->algo, key->flags);
err = send_key_to_dongle(ifp, key);
if (err != ZX_OK) {
goto done;
}
if (ext_key) {
goto done;
}
err = brcmf_fil_bsscfg_int_get(ifp, "wsec", (uint32_t*)&wsec); // TODO(cphoenix): This cast?!?
if (err != ZX_OK) {
BRCMF_ERR("get wsec error (%d)", err);
goto done;
}
wsec |= val;
BRCMF_DBG(CONN, "setting wsec to 0x%x", wsec);
err = brcmf_fil_bsscfg_int_set(ifp, "wsec", wsec);
if (err != ZX_OK) {
BRCMF_ERR("set wsec error (%d)", err);
goto done;
}
done:
BRCMF_DBG(TRACE, "Exit");
return err;
}
// EAPOL frames are queued up along with event notifications to ensure processing order.
void brcmf_cfg80211_handle_eapol_frame(struct brcmf_if* ifp, const void* data, size_t size) {
struct net_device* ndev = ifp->ndev;
std::shared_lock<std::shared_mutex> guard(ndev->if_proto_lock);
if (ndev->if_proto.ops == nullptr) {
BRCMF_DBG(WLANIF, "interface stopped -- skipping eapol frame callback");
return;
}
const char* const data_bytes = reinterpret_cast<const char*>(data);
wlanif_eapol_indication_t eapol_ind;
// IEEE Std. 802.1X-2010, 11.3, Figure 11-1
memcpy(&eapol_ind.dst_addr, data_bytes, ETH_ALEN);
memcpy(&eapol_ind.src_addr, data_bytes + 6, ETH_ALEN);
eapol_ind.data_count = size - 14;
eapol_ind.data_list = reinterpret_cast<const uint8_t*>(data_bytes + 14);
BRCMF_DBG(WLANIF, "Sending EAPOL frame to SME. data_len: %zu", eapol_ind.data_count);
wlanif_impl_ifc_eapol_ind(&ndev->if_proto, &eapol_ind);
}
#define EAPOL_ETHERNET_TYPE_UINT16 0x8e88
void brcmf_cfg80211_rx(struct brcmf_if* ifp, const void* data, size_t size) {
struct net_device* ndev = ifp->ndev;
std::shared_lock<std::shared_mutex> guard(ndev->if_proto_lock);
if (ndev->if_proto.ops == nullptr) {
BRCMF_DBG(WLANIF, "interface stopped -- skipping data recv");
return;
}
THROTTLE(10, BRCMF_DBG_HEX_DUMP(BRCMF_IS_ON(BYTES) && BRCMF_IS_ON(DATA), data,
std::min<size_t>(size, 64u),
"Data received (%zu bytes, max 64 shown):", size));
// IEEE Std. 802.3-2015, 3.1.1
const uint16_t eth_type = ((uint16_t*)(data))[6];
if (eth_type == EAPOL_ETHERNET_TYPE_UINT16) {
// queue up the eapol frame along with events to ensure processing order
brcmf_fweh_queue_eapol_frame(ifp, data, size);
} else {
wlanif_impl_ifc_data_recv(&ndev->if_proto, data, size, 0);
}
}
uint8_t brcmf_cfg80211_classify8021d(const uint8_t* data, size_t size) {
// Make sure packet is sufficiently large to contain the DS field
const size_t kDsFieldLength = 2;
if (size < sizeof(ethhdr) + kDsFieldLength) {
return 0;
}
auto* eh = (struct ethhdr*)data;
uint8_t ds_field = 0;
const uint8_t* eth_body = data + sizeof(ethhdr);
if (eh->h_proto == htobe16(ETH_P_IP)) {
ds_field = eth_body[1];
} else if (eh->h_proto == htobe16(ETH_P_IPV6)) {
ds_field = ((eth_body[0] & 0x0f) << 4) | ((eth_body[1] & 0xf0) >> 4);
}
// DSCP is the 6 most significant bits of the DS field
uint8_t dscp = ds_field >> 2;
// Given the 6-bit DSCP from IPv4 or IPv6 header, convert it to UP
// This follows RFC 8325 - https://tools.ietf.org/html/rfc8325#section-4.3
// For list of DSCP, see https://www.iana.org/assignments/dscp-registry/dscp-registry.xhtml
switch (dscp) {
// Network Control - CS6, CS7
case 0b110000:
case 0b111000:
return 7;
// Telephony - EF
case 0b101110:
// VOICE-ADMIT - VA
case 0b101100:
return 6;
// Signaling - CS5
case 0b101000:
return 5;
// Multimedia Conferencing - AF41, AF42, AF43
case 0b100010:
case 0b100100:
case 0b100110:
// Real-Time Interactive - CS4
case 0b100000:
// Multimedia Streaming - AF31, AF32, AF33
case 0b011010:
case 0b011100:
case 0b011110:
// Broadcast Video - CS3
case 0b011000:
return 4;
// Low-Latency Data - AF21, AF22, AF23
case 0b010010:
case 0b010100:
case 0b010110:
return 3;
// Low-Priority Data - CS1
case 0b001000:
return 1;
// OAM, High-Throughput Data, Standard, and unused code points
default:
return 0;
}
}
// Extract the provided information elements into the BSS description.
// Note: be careful if you call this function more than once on the same bss description struct.
// For example, if you provide an ie buffer that has an SSID IE in the first call, and then provide
// an ie buffer that does not have an SSID IE in the second call, the end result will be that the
// bss description struct still has the SSID that was extracted in the first call.
void brcmf_extract_ies(const uint8_t* ie, size_t ie_len, wlanif_bss_description_t* bss) {
bss->vendor_ie_len = 0;
size_t offset = 0;
bool wpa_ie_extracted = false;
bool wsc_ie_extracted = false;
while (offset < ie_len) {
uint8_t type = ie[offset];
uint8_t length = ie[offset + TLV_LEN_OFF];
switch (type) {
case WLAN_IE_TYPE_SSID: {
uint8_t ssid_len = std::min<uint8_t>(length, sizeof(bss->ssid.data));
memcpy(bss->ssid.data, ie + offset + TLV_HDR_LEN, ssid_len);
bss->ssid.len = ssid_len;
break;
}
case WLAN_IE_TYPE_SUPP_RATES: {
uint8_t num_supp_rates = std::min<uint8_t>(length, WLAN_MAC_MAX_SUPP_RATES);
memcpy(bss->rates, ie + offset + TLV_HDR_LEN, num_supp_rates);
bss->num_rates = num_supp_rates;
break;
}
case WLAN_IE_TYPE_EXT_SUPP_RATES: {
uint8_t num_ext_supp_rates = std::min<uint8_t>(length, WLAN_MAC_MAX_EXT_RATES);
memcpy(bss->rates + bss->num_rates, ie + offset + TLV_HDR_LEN, num_ext_supp_rates);
bss->num_rates += num_ext_supp_rates;
break;
}
case WLAN_IE_TYPE_COUNTRY: {
bss->country_len = length;
memcpy(bss->country, ie + offset + TLV_HDR_LEN, bss->country_len);
break;
}
case WLAN_IE_TYPE_RSNE: {
bss->rsne_len = length + TLV_HDR_LEN;
memcpy(bss->rsne, ie + offset, bss->rsne_len);
break;
}
case WLAN_IE_TYPE_VENDOR_SPECIFIC: {
bool extract_wpa_ie =
!wpa_ie_extracted && brcmf_tlv_ie_has_msft_type(ie + offset, WPA_OUI_TYPE);
bool extract_wsc_ie =
!wsc_ie_extracted && brcmf_tlv_ie_has_msft_type(ie + offset, WSC_OUI_TYPE);
if (extract_wpa_ie || extract_wsc_ie) {
size_t this_ie_len = length + TLV_HDR_LEN;
if (bss->vendor_ie_len + this_ie_len < WLAN_VIE_MAX_LEN) {
memcpy(bss->vendor_ie + bss->vendor_ie_len, ie + offset, this_ie_len);
bss->vendor_ie_len += this_ie_len;
if (extract_wpa_ie) {
wpa_ie_extracted = true;
}
if (extract_wsc_ie) {
wsc_ie_extracted = true;
}
}
}
break;
}
default:
break;
}
offset += length + TLV_HDR_LEN;
}
}
static void brcmf_iedump(uint8_t* ies, size_t total_len) {
size_t offset = 0;
while (offset + TLV_HDR_LEN <= total_len) {
uint8_t elem_type = ies[offset];
uint8_t elem_len = ies[offset + TLV_LEN_OFF];
offset += TLV_HDR_LEN;
if (offset + elem_len > total_len) {
break;
}
if (elem_type == 0) {
BRCMF_DBG_STRING_DUMP(true, ies + offset, elem_len, "IE 0 (name), len %d:", elem_len);
} else {
BRCMF_DBG_HEX_DUMP(true, ies + offset, elem_len, "IE %d, len %d:", elem_type, elem_len);
}
offset += elem_len;
}
if (offset != total_len) {
BRCMF_DBG(ALL, " * * Offset %ld didn't match length %ld", offset, total_len);
}
}
static void brcmf_return_scan_result(struct net_device* ndev, uint16_t channel,
const uint8_t* bssid, uint16_t capability, uint16_t interval,
uint8_t* ie, size_t ie_len, int16_t rssi_dbm) {
std::shared_lock<std::shared_mutex> guard(ndev->if_proto_lock);
struct brcmf_cfg80211_info* cfg = ndev_to_if(ndev)->drvr->config;
if (ndev->if_proto.ops == nullptr) {
BRCMF_DBG(WLANIF, "interface stopped -- skipping scan result callback");
return;
}
if (!brcmf_test_bit_in_array(BRCMF_SCAN_STATUS_BUSY, &cfg->scan_status)) {
return;
}
wlanif_scan_result_t result = {};
result.txn_id = ndev->scan_txn_id;
memcpy(result.bss.bssid, bssid, ETH_ALEN);
brcmf_extract_ies(ie, ie_len, &result.bss);
result.bss.bss_type = WLAN_BSS_TYPE_ANY_BSS;
result.bss.beacon_period = 0;
result.bss.dtim_period = 0;
result.bss.timestamp = 0;
result.bss.local_time = 0;
result.bss.cap = capability;
result.bss.chan.primary = (uint8_t)channel;
result.bss.chan.cbw = WLAN_CHANNEL_BANDWIDTH__20; // TODO(cphoenix): Don't hard-code this.
result.bss.rssi_dbm = std::min<int16_t>(0, std::max<int16_t>(-255, rssi_dbm));
BRCMF_DBG(SCAN, "Returning scan result %.*s, channel %d, dbm %d, id %lu", result.bss.ssid.len,
result.bss.ssid.data, channel, result.bss.rssi_dbm, result.txn_id);
ndev->scan_num_results++;
wlanif_impl_ifc_on_scan_result(&ndev->if_proto, &result);
}
static zx_status_t brcmf_inform_single_bss(struct net_device* ndev, struct brcmf_cfg80211_info* cfg,
struct brcmf_bss_info_le* bi) {
struct brcmu_chan ch;
uint16_t channel;
uint16_t notify_capability;
uint16_t notify_interval;
uint8_t* notify_ie;
size_t notify_ielen;
int16_t notify_rssi_dbm;
if (bi->length > WL_BSS_INFO_MAX) {
BRCMF_ERR("Bss info is larger than buffer. Discarding");
BRCMF_DBG(TEMP, "Early return, due to length.");
return ZX_OK;
}
if (!bi->ctl_ch) {
ch.chspec = bi->chanspec;
cfg->d11inf.decchspec(&ch);
bi->ctl_ch = ch.control_ch_num;
}
channel = bi->ctl_ch;
notify_capability = bi->capability;
notify_interval = bi->beacon_period;
notify_ie = (uint8_t*)bi + bi->ie_offset;
notify_ielen = bi->ie_length;
notify_rssi_dbm = (int16_t)bi->RSSI;
BRCMF_DBG(CONN,
"Scan result received BSS: " MAC_FMT_STR
" Channel: %3d Capability: %#6x Beacon interval: %5d Signal: %4d",
MAC_FMT_ARGS(bi->BSSID), channel, notify_capability, notify_interval, notify_rssi_dbm);
if (BRCMF_IS_ON(CONN) && BRCMF_IS_ON(BYTES)) {
brcmf_iedump(notify_ie, notify_ielen);
}
brcmf_return_scan_result(ndev, (uint8_t)channel, (const uint8_t*)bi->BSSID, notify_capability,
notify_interval, notify_ie, notify_ielen, notify_rssi_dbm);
return ZX_OK;
}
void brcmf_abort_scanning(struct brcmf_cfg80211_info* cfg) {
struct escan_info* escan = &cfg->escan_info;
brcmf_set_bit_in_array(BRCMF_SCAN_STATUS_ABORT, &cfg->scan_status);
if (cfg->int_escan_map || cfg->scan_request) {
escan->escan_state = WL_ESCAN_STATE_IDLE;
brcmf_notify_escan_complete(cfg, escan->ifp, true, true);
}
brcmf_clear_bit_in_array(BRCMF_SCAN_STATUS_BUSY, &cfg->scan_status);
brcmf_clear_bit_in_array(BRCMF_SCAN_STATUS_ABORT, &cfg->scan_status);
}
static void brcmf_cfg80211_escan_timeout_worker(WorkItem* work) {
struct brcmf_cfg80211_info* cfg =
containerof(work, struct brcmf_cfg80211_info, escan_timeout_work);
BRCMF_WARN("Scan timed out, sending notification of aborted scan");
brcmf_notify_escan_complete(cfg, cfg->escan_info.ifp, true, true);
}
static void brcmf_escan_timeout(struct brcmf_cfg80211_info* cfg) {
cfg->pub->irq_callback_lock.lock();
if (cfg->int_escan_map || cfg->scan_request) {
BRCMF_ERR("scan timer expired");
// If it's for SIM tests, won't enqueue.
EXEC_TIMEOUT_WORKER(escan_timeout_work);
}
cfg->pub->irq_callback_lock.unlock();
}
static zx_status_t brcmf_cfg80211_escan_handler(struct brcmf_if* ifp,
const struct brcmf_event_msg* e, void* data) {
struct brcmf_cfg80211_info* cfg = ifp->drvr->config;
struct net_device* ndev = cfg_to_ndev(cfg);
brcmf_fweh_event_status_t status = e->status;
struct brcmf_escan_result_le* escan_result_le;
uint32_t escan_buflen;
struct brcmf_bss_info_le* bss_info_le;
bool aborted;
BRCMF_DBG_EVENT(ifp, e, "%d", [](uint32_t reason) { return reason; });
if (status == BRCMF_E_STATUS_ABORT) {
BRCMF_WARN("Firmware aborted escan: %d", e->reason);
goto chk_scan_end;
}
if (!brcmf_test_bit_in_array(BRCMF_SCAN_STATUS_BUSY, &cfg->scan_status)) {
BRCMF_ERR("scan not ready, bsscfgidx=%d", ifp->bsscfgidx);
return ZX_ERR_UNAVAILABLE;
}
escan_result_le = static_cast<decltype(escan_result_le)>(data);
if (!escan_result_le) {
BRCMF_ERR("Invalid escan result (nullptr)");
goto chk_scan_end;
}
bss_info_le = &escan_result_le->bss_info_le;
if (e->datalen < sizeof(*escan_result_le)) {
// Print the error only if the scan result is partial (as end of scan may not
// contain a scan result)
if (status == BRCMF_E_STATUS_PARTIAL) {
BRCMF_ERR("Insufficient escan result data exp: %lu got: %d", sizeof(*escan_result_le),
e->datalen);
}
goto chk_scan_end;
}
escan_buflen = escan_result_le->buflen;
if (escan_buflen > BRCMF_ESCAN_BUF_SIZE || escan_buflen > e->datalen ||
escan_buflen < sizeof(*escan_result_le)) {
BRCMF_ERR("Invalid escan buffer length: %d", escan_buflen);
goto chk_scan_end;
}
if (escan_result_le->bss_count != 1) {
BRCMF_ERR("Invalid bss_count %d: ignoring", escan_result_le->bss_count);
goto chk_scan_end;
}
if (!cfg->int_escan_map && !cfg->scan_request) {
BRCMF_DBG(SCAN, "result without cfg80211 request");
goto chk_scan_end;
}
if (bss_info_le->length != escan_buflen - WL_ESCAN_RESULTS_FIXED_SIZE) {
BRCMF_ERR("Ignoring invalid bss_info length: %d", bss_info_le->length);
goto chk_scan_end;
}
brcmf_inform_single_bss(ndev, cfg, bss_info_le);
if (status == BRCMF_E_STATUS_PARTIAL) {
BRCMF_DBG(SCAN, "ESCAN Partial result");
goto done;
}
chk_scan_end:
// If this is not a partial notification, indicate scan complete to wlanstack
if (status != BRCMF_E_STATUS_PARTIAL) {
cfg->escan_info.escan_state = WL_ESCAN_STATE_IDLE;
if (cfg->int_escan_map || cfg->scan_request) {
aborted = status != BRCMF_E_STATUS_SUCCESS;
if (aborted) {
BRCMF_WARN("Sending notification of aborted scan: %d", status);
}
brcmf_notify_escan_complete(cfg, ifp, aborted, false);
} else {
BRCMF_DBG(SCAN, "Ignored scan complete result 0x%x", status);
}
}
done:
return ZX_OK;
}
static void brcmf_init_escan(struct brcmf_cfg80211_info* cfg) {
brcmf_fweh_register(cfg->pub, BRCMF_E_ESCAN_RESULT, brcmf_cfg80211_escan_handler);
cfg->escan_info.escan_state = WL_ESCAN_STATE_IDLE;
/* Init scan_timeout timer */
cfg->escan_timer = new Timer(cfg->pub, std::bind(brcmf_escan_timeout, cfg), false);
cfg->escan_timeout_work = WorkItem(brcmf_cfg80211_escan_timeout_worker);
}
static wlanif_scan_req_t* brcmf_alloc_internal_escan_request(void) {
return static_cast<wlanif_scan_req_t*>(calloc(1, sizeof(wlanif_scan_req_t)));
}
static zx_status_t brcmf_internal_escan_add_info(wlanif_scan_req_t* req, uint8_t* ssid,
uint8_t ssid_len, uint8_t channel) {
size_t i;
for (i = 0; i < req->num_channels; i++) {
if (req->channel_list[i] == channel) {
break;
}
}
if (i == req->num_channels) {
if (req->num_channels < WLAN_INFO_CHANNEL_LIST_MAX_CHANNELS) {
req->channel_list[req->num_channels++] = channel;
} else {
BRCMF_ERR("escan channel list full, suppressing channel %d", channel);
}
}
for (i = 0; i < req->num_ssids; i++) {
if (req->ssid_list[i].len == ssid_len && !memcmp(req->ssid_list[i].data, ssid, ssid_len)) {
break;
}
}
if (i == req->num_ssids) {
if