18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (c) 2008, 2009 open80211s Ltd.
48c2ecf20Sopenharmony_ci * Copyright (C) 2018 - 2020 Intel Corporation
58c2ecf20Sopenharmony_ci * Authors:    Luis Carlos Cobo <luisca@cozybit.com>
68c2ecf20Sopenharmony_ci * 	       Javier Cardona <javier@cozybit.com>
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <linux/slab.h>
108c2ecf20Sopenharmony_ci#include <asm/unaligned.h>
118c2ecf20Sopenharmony_ci#include "ieee80211_i.h"
128c2ecf20Sopenharmony_ci#include "mesh.h"
138c2ecf20Sopenharmony_ci#include "driver-ops.h"
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_cistatic int mesh_allocated;
168c2ecf20Sopenharmony_cistatic struct kmem_cache *rm_cache;
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_cibool mesh_action_is_path_sel(struct ieee80211_mgmt *mgmt)
198c2ecf20Sopenharmony_ci{
208c2ecf20Sopenharmony_ci	return (mgmt->u.action.u.mesh_action.action_code ==
218c2ecf20Sopenharmony_ci			WLAN_MESH_ACTION_HWMP_PATH_SELECTION);
228c2ecf20Sopenharmony_ci}
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_civoid ieee80211s_init(void)
258c2ecf20Sopenharmony_ci{
268c2ecf20Sopenharmony_ci	mesh_allocated = 1;
278c2ecf20Sopenharmony_ci	rm_cache = kmem_cache_create("mesh_rmc", sizeof(struct rmc_entry),
288c2ecf20Sopenharmony_ci				     0, 0, NULL);
298c2ecf20Sopenharmony_ci}
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_civoid ieee80211s_stop(void)
328c2ecf20Sopenharmony_ci{
338c2ecf20Sopenharmony_ci	if (!mesh_allocated)
348c2ecf20Sopenharmony_ci		return;
358c2ecf20Sopenharmony_ci	kmem_cache_destroy(rm_cache);
368c2ecf20Sopenharmony_ci}
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_cistatic void ieee80211_mesh_housekeeping_timer(struct timer_list *t)
398c2ecf20Sopenharmony_ci{
408c2ecf20Sopenharmony_ci	struct ieee80211_sub_if_data *sdata =
418c2ecf20Sopenharmony_ci		from_timer(sdata, t, u.mesh.housekeeping_timer);
428c2ecf20Sopenharmony_ci	struct ieee80211_local *local = sdata->local;
438c2ecf20Sopenharmony_ci	struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci	set_bit(MESH_WORK_HOUSEKEEPING, &ifmsh->wrkq_flags);
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci	ieee80211_queue_work(&local->hw, &sdata->work);
488c2ecf20Sopenharmony_ci}
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci/**
518c2ecf20Sopenharmony_ci * mesh_matches_local - check if the config of a mesh point matches ours
528c2ecf20Sopenharmony_ci *
538c2ecf20Sopenharmony_ci * @sdata: local mesh subif
548c2ecf20Sopenharmony_ci * @ie: information elements of a management frame from the mesh peer
558c2ecf20Sopenharmony_ci *
568c2ecf20Sopenharmony_ci * This function checks if the mesh configuration of a mesh point matches the
578c2ecf20Sopenharmony_ci * local mesh configuration, i.e. if both nodes belong to the same mesh network.
588c2ecf20Sopenharmony_ci */
598c2ecf20Sopenharmony_cibool mesh_matches_local(struct ieee80211_sub_if_data *sdata,
608c2ecf20Sopenharmony_ci			struct ieee802_11_elems *ie)
618c2ecf20Sopenharmony_ci{
628c2ecf20Sopenharmony_ci	struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
638c2ecf20Sopenharmony_ci	u32 basic_rates = 0;
648c2ecf20Sopenharmony_ci	struct cfg80211_chan_def sta_chan_def;
658c2ecf20Sopenharmony_ci	struct ieee80211_supported_band *sband;
668c2ecf20Sopenharmony_ci	u32 vht_cap_info = 0;
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci	/*
698c2ecf20Sopenharmony_ci	 * As support for each feature is added, check for matching
708c2ecf20Sopenharmony_ci	 * - On mesh config capabilities
718c2ecf20Sopenharmony_ci	 *   - Power Save Support En
728c2ecf20Sopenharmony_ci	 *   - Sync support enabled
738c2ecf20Sopenharmony_ci	 *   - Sync support active
748c2ecf20Sopenharmony_ci	 *   - Sync support required from peer
758c2ecf20Sopenharmony_ci	 *   - MDA enabled
768c2ecf20Sopenharmony_ci	 * - Power management control on fc
778c2ecf20Sopenharmony_ci	 */
788c2ecf20Sopenharmony_ci	if (!(ifmsh->mesh_id_len == ie->mesh_id_len &&
798c2ecf20Sopenharmony_ci	     memcmp(ifmsh->mesh_id, ie->mesh_id, ie->mesh_id_len) == 0 &&
808c2ecf20Sopenharmony_ci	     (ifmsh->mesh_pp_id == ie->mesh_config->meshconf_psel) &&
818c2ecf20Sopenharmony_ci	     (ifmsh->mesh_pm_id == ie->mesh_config->meshconf_pmetric) &&
828c2ecf20Sopenharmony_ci	     (ifmsh->mesh_cc_id == ie->mesh_config->meshconf_congest) &&
838c2ecf20Sopenharmony_ci	     (ifmsh->mesh_sp_id == ie->mesh_config->meshconf_synch) &&
848c2ecf20Sopenharmony_ci	     (ifmsh->mesh_auth_id == ie->mesh_config->meshconf_auth)))
858c2ecf20Sopenharmony_ci		return false;
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci	sband = ieee80211_get_sband(sdata);
888c2ecf20Sopenharmony_ci	if (!sband)
898c2ecf20Sopenharmony_ci		return false;
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci	ieee80211_sta_get_rates(sdata, ie, sband->band,
928c2ecf20Sopenharmony_ci				&basic_rates);
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci	if (sdata->vif.bss_conf.basic_rates != basic_rates)
958c2ecf20Sopenharmony_ci		return false;
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci	cfg80211_chandef_create(&sta_chan_def, sdata->vif.bss_conf.chandef.chan,
988c2ecf20Sopenharmony_ci				NL80211_CHAN_NO_HT);
998c2ecf20Sopenharmony_ci	ieee80211_chandef_ht_oper(ie->ht_operation, &sta_chan_def);
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	if (ie->vht_cap_elem)
1028c2ecf20Sopenharmony_ci		vht_cap_info = le32_to_cpu(ie->vht_cap_elem->vht_cap_info);
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	ieee80211_chandef_vht_oper(&sdata->local->hw, vht_cap_info,
1058c2ecf20Sopenharmony_ci				   ie->vht_operation, ie->ht_operation,
1068c2ecf20Sopenharmony_ci				   &sta_chan_def);
1078c2ecf20Sopenharmony_ci	ieee80211_chandef_he_6ghz_oper(sdata, ie->he_operation, &sta_chan_def);
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci	if (!cfg80211_chandef_compatible(&sdata->vif.bss_conf.chandef,
1108c2ecf20Sopenharmony_ci					 &sta_chan_def))
1118c2ecf20Sopenharmony_ci		return false;
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	return true;
1148c2ecf20Sopenharmony_ci}
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci/**
1178c2ecf20Sopenharmony_ci * mesh_peer_accepts_plinks - check if an mp is willing to establish peer links
1188c2ecf20Sopenharmony_ci *
1198c2ecf20Sopenharmony_ci * @ie: information elements of a management frame from the mesh peer
1208c2ecf20Sopenharmony_ci */
1218c2ecf20Sopenharmony_cibool mesh_peer_accepts_plinks(struct ieee802_11_elems *ie)
1228c2ecf20Sopenharmony_ci{
1238c2ecf20Sopenharmony_ci	return (ie->mesh_config->meshconf_cap &
1248c2ecf20Sopenharmony_ci			IEEE80211_MESHCONF_CAPAB_ACCEPT_PLINKS) != 0;
1258c2ecf20Sopenharmony_ci}
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci/**
1288c2ecf20Sopenharmony_ci * mesh_accept_plinks_update - update accepting_plink in local mesh beacons
1298c2ecf20Sopenharmony_ci *
1308c2ecf20Sopenharmony_ci * @sdata: mesh interface in which mesh beacons are going to be updated
1318c2ecf20Sopenharmony_ci *
1328c2ecf20Sopenharmony_ci * Returns: beacon changed flag if the beacon content changed.
1338c2ecf20Sopenharmony_ci */
1348c2ecf20Sopenharmony_ciu32 mesh_accept_plinks_update(struct ieee80211_sub_if_data *sdata)
1358c2ecf20Sopenharmony_ci{
1368c2ecf20Sopenharmony_ci	bool free_plinks;
1378c2ecf20Sopenharmony_ci	u32 changed = 0;
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	/* In case mesh_plink_free_count > 0 and mesh_plinktbl_capacity == 0,
1408c2ecf20Sopenharmony_ci	 * the mesh interface might be able to establish plinks with peers that
1418c2ecf20Sopenharmony_ci	 * are already on the table but are not on PLINK_ESTAB state. However,
1428c2ecf20Sopenharmony_ci	 * in general the mesh interface is not accepting peer link requests
1438c2ecf20Sopenharmony_ci	 * from new peers, and that must be reflected in the beacon
1448c2ecf20Sopenharmony_ci	 */
1458c2ecf20Sopenharmony_ci	free_plinks = mesh_plink_availables(sdata);
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci	if (free_plinks != sdata->u.mesh.accepting_plinks) {
1488c2ecf20Sopenharmony_ci		sdata->u.mesh.accepting_plinks = free_plinks;
1498c2ecf20Sopenharmony_ci		changed = BSS_CHANGED_BEACON;
1508c2ecf20Sopenharmony_ci	}
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci	return changed;
1538c2ecf20Sopenharmony_ci}
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci/*
1568c2ecf20Sopenharmony_ci * mesh_sta_cleanup - clean up any mesh sta state
1578c2ecf20Sopenharmony_ci *
1588c2ecf20Sopenharmony_ci * @sta: mesh sta to clean up.
1598c2ecf20Sopenharmony_ci */
1608c2ecf20Sopenharmony_civoid mesh_sta_cleanup(struct sta_info *sta)
1618c2ecf20Sopenharmony_ci{
1628c2ecf20Sopenharmony_ci	struct ieee80211_sub_if_data *sdata = sta->sdata;
1638c2ecf20Sopenharmony_ci	u32 changed = mesh_plink_deactivate(sta);
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	if (changed)
1668c2ecf20Sopenharmony_ci		ieee80211_mbss_info_change_notify(sdata, changed);
1678c2ecf20Sopenharmony_ci}
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ciint mesh_rmc_init(struct ieee80211_sub_if_data *sdata)
1708c2ecf20Sopenharmony_ci{
1718c2ecf20Sopenharmony_ci	int i;
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci	sdata->u.mesh.rmc = kmalloc(sizeof(struct mesh_rmc), GFP_KERNEL);
1748c2ecf20Sopenharmony_ci	if (!sdata->u.mesh.rmc)
1758c2ecf20Sopenharmony_ci		return -ENOMEM;
1768c2ecf20Sopenharmony_ci	sdata->u.mesh.rmc->idx_mask = RMC_BUCKETS - 1;
1778c2ecf20Sopenharmony_ci	for (i = 0; i < RMC_BUCKETS; i++)
1788c2ecf20Sopenharmony_ci		INIT_HLIST_HEAD(&sdata->u.mesh.rmc->bucket[i]);
1798c2ecf20Sopenharmony_ci	return 0;
1808c2ecf20Sopenharmony_ci}
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_civoid mesh_rmc_free(struct ieee80211_sub_if_data *sdata)
1838c2ecf20Sopenharmony_ci{
1848c2ecf20Sopenharmony_ci	struct mesh_rmc *rmc = sdata->u.mesh.rmc;
1858c2ecf20Sopenharmony_ci	struct rmc_entry *p;
1868c2ecf20Sopenharmony_ci	struct hlist_node *n;
1878c2ecf20Sopenharmony_ci	int i;
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci	if (!sdata->u.mesh.rmc)
1908c2ecf20Sopenharmony_ci		return;
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci	for (i = 0; i < RMC_BUCKETS; i++) {
1938c2ecf20Sopenharmony_ci		hlist_for_each_entry_safe(p, n, &rmc->bucket[i], list) {
1948c2ecf20Sopenharmony_ci			hlist_del(&p->list);
1958c2ecf20Sopenharmony_ci			kmem_cache_free(rm_cache, p);
1968c2ecf20Sopenharmony_ci		}
1978c2ecf20Sopenharmony_ci	}
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci	kfree(rmc);
2008c2ecf20Sopenharmony_ci	sdata->u.mesh.rmc = NULL;
2018c2ecf20Sopenharmony_ci}
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ci/**
2048c2ecf20Sopenharmony_ci * mesh_rmc_check - Check frame in recent multicast cache and add if absent.
2058c2ecf20Sopenharmony_ci *
2068c2ecf20Sopenharmony_ci * @sdata:	interface
2078c2ecf20Sopenharmony_ci * @sa:		source address
2088c2ecf20Sopenharmony_ci * @mesh_hdr:	mesh_header
2098c2ecf20Sopenharmony_ci *
2108c2ecf20Sopenharmony_ci * Returns: 0 if the frame is not in the cache, nonzero otherwise.
2118c2ecf20Sopenharmony_ci *
2128c2ecf20Sopenharmony_ci * Checks using the source address and the mesh sequence number if we have
2138c2ecf20Sopenharmony_ci * received this frame lately. If the frame is not in the cache, it is added to
2148c2ecf20Sopenharmony_ci * it.
2158c2ecf20Sopenharmony_ci */
2168c2ecf20Sopenharmony_ciint mesh_rmc_check(struct ieee80211_sub_if_data *sdata,
2178c2ecf20Sopenharmony_ci		   const u8 *sa, struct ieee80211s_hdr *mesh_hdr)
2188c2ecf20Sopenharmony_ci{
2198c2ecf20Sopenharmony_ci	struct mesh_rmc *rmc = sdata->u.mesh.rmc;
2208c2ecf20Sopenharmony_ci	u32 seqnum = 0;
2218c2ecf20Sopenharmony_ci	int entries = 0;
2228c2ecf20Sopenharmony_ci	u8 idx;
2238c2ecf20Sopenharmony_ci	struct rmc_entry *p;
2248c2ecf20Sopenharmony_ci	struct hlist_node *n;
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci	if (!rmc)
2278c2ecf20Sopenharmony_ci		return -1;
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci	/* Don't care about endianness since only match matters */
2308c2ecf20Sopenharmony_ci	memcpy(&seqnum, &mesh_hdr->seqnum, sizeof(mesh_hdr->seqnum));
2318c2ecf20Sopenharmony_ci	idx = le32_to_cpu(mesh_hdr->seqnum) & rmc->idx_mask;
2328c2ecf20Sopenharmony_ci	hlist_for_each_entry_safe(p, n, &rmc->bucket[idx], list) {
2338c2ecf20Sopenharmony_ci		++entries;
2348c2ecf20Sopenharmony_ci		if (time_after(jiffies, p->exp_time) ||
2358c2ecf20Sopenharmony_ci		    entries == RMC_QUEUE_MAX_LEN) {
2368c2ecf20Sopenharmony_ci			hlist_del(&p->list);
2378c2ecf20Sopenharmony_ci			kmem_cache_free(rm_cache, p);
2388c2ecf20Sopenharmony_ci			--entries;
2398c2ecf20Sopenharmony_ci		} else if ((seqnum == p->seqnum) && ether_addr_equal(sa, p->sa))
2408c2ecf20Sopenharmony_ci			return -1;
2418c2ecf20Sopenharmony_ci	}
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ci	p = kmem_cache_alloc(rm_cache, GFP_ATOMIC);
2448c2ecf20Sopenharmony_ci	if (!p)
2458c2ecf20Sopenharmony_ci		return 0;
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci	p->seqnum = seqnum;
2488c2ecf20Sopenharmony_ci	p->exp_time = jiffies + RMC_TIMEOUT;
2498c2ecf20Sopenharmony_ci	memcpy(p->sa, sa, ETH_ALEN);
2508c2ecf20Sopenharmony_ci	hlist_add_head(&p->list, &rmc->bucket[idx]);
2518c2ecf20Sopenharmony_ci	return 0;
2528c2ecf20Sopenharmony_ci}
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ciint mesh_add_meshconf_ie(struct ieee80211_sub_if_data *sdata,
2558c2ecf20Sopenharmony_ci			 struct sk_buff *skb)
2568c2ecf20Sopenharmony_ci{
2578c2ecf20Sopenharmony_ci	struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
2588c2ecf20Sopenharmony_ci	u8 *pos, neighbors;
2598c2ecf20Sopenharmony_ci	u8 meshconf_len = sizeof(struct ieee80211_meshconf_ie);
2608c2ecf20Sopenharmony_ci	bool is_connected_to_gate = ifmsh->num_gates > 0 ||
2618c2ecf20Sopenharmony_ci		ifmsh->mshcfg.dot11MeshGateAnnouncementProtocol ||
2628c2ecf20Sopenharmony_ci		ifmsh->mshcfg.dot11MeshConnectedToMeshGate;
2638c2ecf20Sopenharmony_ci	bool is_connected_to_as = ifmsh->mshcfg.dot11MeshConnectedToAuthServer;
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ci	if (skb_tailroom(skb) < 2 + meshconf_len)
2668c2ecf20Sopenharmony_ci		return -ENOMEM;
2678c2ecf20Sopenharmony_ci
2688c2ecf20Sopenharmony_ci	pos = skb_put(skb, 2 + meshconf_len);
2698c2ecf20Sopenharmony_ci	*pos++ = WLAN_EID_MESH_CONFIG;
2708c2ecf20Sopenharmony_ci	*pos++ = meshconf_len;
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_ci	/* save a pointer for quick updates in pre-tbtt */
2738c2ecf20Sopenharmony_ci	ifmsh->meshconf_offset = pos - skb->data;
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_ci	/* Active path selection protocol ID */
2768c2ecf20Sopenharmony_ci	*pos++ = ifmsh->mesh_pp_id;
2778c2ecf20Sopenharmony_ci	/* Active path selection metric ID   */
2788c2ecf20Sopenharmony_ci	*pos++ = ifmsh->mesh_pm_id;
2798c2ecf20Sopenharmony_ci	/* Congestion control mode identifier */
2808c2ecf20Sopenharmony_ci	*pos++ = ifmsh->mesh_cc_id;
2818c2ecf20Sopenharmony_ci	/* Synchronization protocol identifier */
2828c2ecf20Sopenharmony_ci	*pos++ = ifmsh->mesh_sp_id;
2838c2ecf20Sopenharmony_ci	/* Authentication Protocol identifier */
2848c2ecf20Sopenharmony_ci	*pos++ = ifmsh->mesh_auth_id;
2858c2ecf20Sopenharmony_ci	/* Mesh Formation Info - number of neighbors */
2868c2ecf20Sopenharmony_ci	neighbors = atomic_read(&ifmsh->estab_plinks);
2878c2ecf20Sopenharmony_ci	neighbors = min_t(int, neighbors, IEEE80211_MAX_MESH_PEERINGS);
2888c2ecf20Sopenharmony_ci	*pos++ = (is_connected_to_as << 7) |
2898c2ecf20Sopenharmony_ci		 (neighbors << 1) |
2908c2ecf20Sopenharmony_ci		 is_connected_to_gate;
2918c2ecf20Sopenharmony_ci	/* Mesh capability */
2928c2ecf20Sopenharmony_ci	*pos = 0x00;
2938c2ecf20Sopenharmony_ci	*pos |= ifmsh->mshcfg.dot11MeshForwarding ?
2948c2ecf20Sopenharmony_ci			IEEE80211_MESHCONF_CAPAB_FORWARDING : 0x00;
2958c2ecf20Sopenharmony_ci	*pos |= ifmsh->accepting_plinks ?
2968c2ecf20Sopenharmony_ci			IEEE80211_MESHCONF_CAPAB_ACCEPT_PLINKS : 0x00;
2978c2ecf20Sopenharmony_ci	/* Mesh PS mode. See IEEE802.11-2012 8.4.2.100.8 */
2988c2ecf20Sopenharmony_ci	*pos |= ifmsh->ps_peers_deep_sleep ?
2998c2ecf20Sopenharmony_ci			IEEE80211_MESHCONF_CAPAB_POWER_SAVE_LEVEL : 0x00;
3008c2ecf20Sopenharmony_ci	return 0;
3018c2ecf20Sopenharmony_ci}
3028c2ecf20Sopenharmony_ci
3038c2ecf20Sopenharmony_ciint mesh_add_meshid_ie(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb)
3048c2ecf20Sopenharmony_ci{
3058c2ecf20Sopenharmony_ci	struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
3068c2ecf20Sopenharmony_ci	u8 *pos;
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_ci	if (skb_tailroom(skb) < 2 + ifmsh->mesh_id_len)
3098c2ecf20Sopenharmony_ci		return -ENOMEM;
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_ci	pos = skb_put(skb, 2 + ifmsh->mesh_id_len);
3128c2ecf20Sopenharmony_ci	*pos++ = WLAN_EID_MESH_ID;
3138c2ecf20Sopenharmony_ci	*pos++ = ifmsh->mesh_id_len;
3148c2ecf20Sopenharmony_ci	if (ifmsh->mesh_id_len)
3158c2ecf20Sopenharmony_ci		memcpy(pos, ifmsh->mesh_id, ifmsh->mesh_id_len);
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_ci	return 0;
3188c2ecf20Sopenharmony_ci}
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_cistatic int mesh_add_awake_window_ie(struct ieee80211_sub_if_data *sdata,
3218c2ecf20Sopenharmony_ci				    struct sk_buff *skb)
3228c2ecf20Sopenharmony_ci{
3238c2ecf20Sopenharmony_ci	struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
3248c2ecf20Sopenharmony_ci	u8 *pos;
3258c2ecf20Sopenharmony_ci
3268c2ecf20Sopenharmony_ci	/* see IEEE802.11-2012 13.14.6 */
3278c2ecf20Sopenharmony_ci	if (ifmsh->ps_peers_light_sleep == 0 &&
3288c2ecf20Sopenharmony_ci	    ifmsh->ps_peers_deep_sleep == 0 &&
3298c2ecf20Sopenharmony_ci	    ifmsh->nonpeer_pm == NL80211_MESH_POWER_ACTIVE)
3308c2ecf20Sopenharmony_ci		return 0;
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_ci	if (skb_tailroom(skb) < 4)
3338c2ecf20Sopenharmony_ci		return -ENOMEM;
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_ci	pos = skb_put(skb, 2 + 2);
3368c2ecf20Sopenharmony_ci	*pos++ = WLAN_EID_MESH_AWAKE_WINDOW;
3378c2ecf20Sopenharmony_ci	*pos++ = 2;
3388c2ecf20Sopenharmony_ci	put_unaligned_le16(ifmsh->mshcfg.dot11MeshAwakeWindowDuration, pos);
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_ci	return 0;
3418c2ecf20Sopenharmony_ci}
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_ciint mesh_add_vendor_ies(struct ieee80211_sub_if_data *sdata,
3448c2ecf20Sopenharmony_ci			struct sk_buff *skb)
3458c2ecf20Sopenharmony_ci{
3468c2ecf20Sopenharmony_ci	struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
3478c2ecf20Sopenharmony_ci	u8 offset, len;
3488c2ecf20Sopenharmony_ci	const u8 *data;
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_ci	if (!ifmsh->ie || !ifmsh->ie_len)
3518c2ecf20Sopenharmony_ci		return 0;
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_ci	/* fast-forward to vendor IEs */
3548c2ecf20Sopenharmony_ci	offset = ieee80211_ie_split_vendor(ifmsh->ie, ifmsh->ie_len, 0);
3558c2ecf20Sopenharmony_ci
3568c2ecf20Sopenharmony_ci	if (offset < ifmsh->ie_len) {
3578c2ecf20Sopenharmony_ci		len = ifmsh->ie_len - offset;
3588c2ecf20Sopenharmony_ci		data = ifmsh->ie + offset;
3598c2ecf20Sopenharmony_ci		if (skb_tailroom(skb) < len)
3608c2ecf20Sopenharmony_ci			return -ENOMEM;
3618c2ecf20Sopenharmony_ci		skb_put_data(skb, data, len);
3628c2ecf20Sopenharmony_ci	}
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_ci	return 0;
3658c2ecf20Sopenharmony_ci}
3668c2ecf20Sopenharmony_ci
3678c2ecf20Sopenharmony_ciint mesh_add_rsn_ie(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb)
3688c2ecf20Sopenharmony_ci{
3698c2ecf20Sopenharmony_ci	struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
3708c2ecf20Sopenharmony_ci	u8 len = 0;
3718c2ecf20Sopenharmony_ci	const u8 *data;
3728c2ecf20Sopenharmony_ci
3738c2ecf20Sopenharmony_ci	if (!ifmsh->ie || !ifmsh->ie_len)
3748c2ecf20Sopenharmony_ci		return 0;
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_ci	/* find RSN IE */
3778c2ecf20Sopenharmony_ci	data = cfg80211_find_ie(WLAN_EID_RSN, ifmsh->ie, ifmsh->ie_len);
3788c2ecf20Sopenharmony_ci	if (!data)
3798c2ecf20Sopenharmony_ci		return 0;
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_ci	len = data[1] + 2;
3828c2ecf20Sopenharmony_ci
3838c2ecf20Sopenharmony_ci	if (skb_tailroom(skb) < len)
3848c2ecf20Sopenharmony_ci		return -ENOMEM;
3858c2ecf20Sopenharmony_ci	skb_put_data(skb, data, len);
3868c2ecf20Sopenharmony_ci
3878c2ecf20Sopenharmony_ci	return 0;
3888c2ecf20Sopenharmony_ci}
3898c2ecf20Sopenharmony_ci
3908c2ecf20Sopenharmony_cistatic int mesh_add_ds_params_ie(struct ieee80211_sub_if_data *sdata,
3918c2ecf20Sopenharmony_ci				 struct sk_buff *skb)
3928c2ecf20Sopenharmony_ci{
3938c2ecf20Sopenharmony_ci	struct ieee80211_chanctx_conf *chanctx_conf;
3948c2ecf20Sopenharmony_ci	struct ieee80211_channel *chan;
3958c2ecf20Sopenharmony_ci	u8 *pos;
3968c2ecf20Sopenharmony_ci
3978c2ecf20Sopenharmony_ci	if (skb_tailroom(skb) < 3)
3988c2ecf20Sopenharmony_ci		return -ENOMEM;
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_ci	rcu_read_lock();
4018c2ecf20Sopenharmony_ci	chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
4028c2ecf20Sopenharmony_ci	if (WARN_ON(!chanctx_conf)) {
4038c2ecf20Sopenharmony_ci		rcu_read_unlock();
4048c2ecf20Sopenharmony_ci		return -EINVAL;
4058c2ecf20Sopenharmony_ci	}
4068c2ecf20Sopenharmony_ci	chan = chanctx_conf->def.chan;
4078c2ecf20Sopenharmony_ci	rcu_read_unlock();
4088c2ecf20Sopenharmony_ci
4098c2ecf20Sopenharmony_ci	pos = skb_put(skb, 2 + 1);
4108c2ecf20Sopenharmony_ci	*pos++ = WLAN_EID_DS_PARAMS;
4118c2ecf20Sopenharmony_ci	*pos++ = 1;
4128c2ecf20Sopenharmony_ci	*pos++ = ieee80211_frequency_to_channel(chan->center_freq);
4138c2ecf20Sopenharmony_ci
4148c2ecf20Sopenharmony_ci	return 0;
4158c2ecf20Sopenharmony_ci}
4168c2ecf20Sopenharmony_ci
4178c2ecf20Sopenharmony_ciint mesh_add_ht_cap_ie(struct ieee80211_sub_if_data *sdata,
4188c2ecf20Sopenharmony_ci		       struct sk_buff *skb)
4198c2ecf20Sopenharmony_ci{
4208c2ecf20Sopenharmony_ci	struct ieee80211_supported_band *sband;
4218c2ecf20Sopenharmony_ci	u8 *pos;
4228c2ecf20Sopenharmony_ci
4238c2ecf20Sopenharmony_ci	sband = ieee80211_get_sband(sdata);
4248c2ecf20Sopenharmony_ci	if (!sband)
4258c2ecf20Sopenharmony_ci		return -EINVAL;
4268c2ecf20Sopenharmony_ci
4278c2ecf20Sopenharmony_ci	/* HT not allowed in 6 GHz */
4288c2ecf20Sopenharmony_ci	if (sband->band == NL80211_BAND_6GHZ)
4298c2ecf20Sopenharmony_ci		return 0;
4308c2ecf20Sopenharmony_ci
4318c2ecf20Sopenharmony_ci	if (!sband->ht_cap.ht_supported ||
4328c2ecf20Sopenharmony_ci	    sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_20_NOHT ||
4338c2ecf20Sopenharmony_ci	    sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_5 ||
4348c2ecf20Sopenharmony_ci	    sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_10)
4358c2ecf20Sopenharmony_ci		return 0;
4368c2ecf20Sopenharmony_ci
4378c2ecf20Sopenharmony_ci	if (skb_tailroom(skb) < 2 + sizeof(struct ieee80211_ht_cap))
4388c2ecf20Sopenharmony_ci		return -ENOMEM;
4398c2ecf20Sopenharmony_ci
4408c2ecf20Sopenharmony_ci	pos = skb_put(skb, 2 + sizeof(struct ieee80211_ht_cap));
4418c2ecf20Sopenharmony_ci	ieee80211_ie_build_ht_cap(pos, &sband->ht_cap, sband->ht_cap.cap);
4428c2ecf20Sopenharmony_ci
4438c2ecf20Sopenharmony_ci	return 0;
4448c2ecf20Sopenharmony_ci}
4458c2ecf20Sopenharmony_ci
4468c2ecf20Sopenharmony_ciint mesh_add_ht_oper_ie(struct ieee80211_sub_if_data *sdata,
4478c2ecf20Sopenharmony_ci			struct sk_buff *skb)
4488c2ecf20Sopenharmony_ci{
4498c2ecf20Sopenharmony_ci	struct ieee80211_local *local = sdata->local;
4508c2ecf20Sopenharmony_ci	struct ieee80211_chanctx_conf *chanctx_conf;
4518c2ecf20Sopenharmony_ci	struct ieee80211_channel *channel;
4528c2ecf20Sopenharmony_ci	struct ieee80211_supported_band *sband;
4538c2ecf20Sopenharmony_ci	struct ieee80211_sta_ht_cap *ht_cap;
4548c2ecf20Sopenharmony_ci	u8 *pos;
4558c2ecf20Sopenharmony_ci
4568c2ecf20Sopenharmony_ci	rcu_read_lock();
4578c2ecf20Sopenharmony_ci	chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
4588c2ecf20Sopenharmony_ci	if (WARN_ON(!chanctx_conf)) {
4598c2ecf20Sopenharmony_ci		rcu_read_unlock();
4608c2ecf20Sopenharmony_ci		return -EINVAL;
4618c2ecf20Sopenharmony_ci	}
4628c2ecf20Sopenharmony_ci	channel = chanctx_conf->def.chan;
4638c2ecf20Sopenharmony_ci	rcu_read_unlock();
4648c2ecf20Sopenharmony_ci
4658c2ecf20Sopenharmony_ci	sband = local->hw.wiphy->bands[channel->band];
4668c2ecf20Sopenharmony_ci	ht_cap = &sband->ht_cap;
4678c2ecf20Sopenharmony_ci
4688c2ecf20Sopenharmony_ci	/* HT not allowed in 6 GHz */
4698c2ecf20Sopenharmony_ci	if (sband->band == NL80211_BAND_6GHZ)
4708c2ecf20Sopenharmony_ci		return 0;
4718c2ecf20Sopenharmony_ci
4728c2ecf20Sopenharmony_ci	if (!ht_cap->ht_supported ||
4738c2ecf20Sopenharmony_ci	    sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_20_NOHT ||
4748c2ecf20Sopenharmony_ci	    sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_5 ||
4758c2ecf20Sopenharmony_ci	    sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_10)
4768c2ecf20Sopenharmony_ci		return 0;
4778c2ecf20Sopenharmony_ci
4788c2ecf20Sopenharmony_ci	if (skb_tailroom(skb) < 2 + sizeof(struct ieee80211_ht_operation))
4798c2ecf20Sopenharmony_ci		return -ENOMEM;
4808c2ecf20Sopenharmony_ci
4818c2ecf20Sopenharmony_ci	pos = skb_put(skb, 2 + sizeof(struct ieee80211_ht_operation));
4828c2ecf20Sopenharmony_ci	ieee80211_ie_build_ht_oper(pos, ht_cap, &sdata->vif.bss_conf.chandef,
4838c2ecf20Sopenharmony_ci				   sdata->vif.bss_conf.ht_operation_mode,
4848c2ecf20Sopenharmony_ci				   false);
4858c2ecf20Sopenharmony_ci
4868c2ecf20Sopenharmony_ci	return 0;
4878c2ecf20Sopenharmony_ci}
4888c2ecf20Sopenharmony_ci
4898c2ecf20Sopenharmony_ciint mesh_add_vht_cap_ie(struct ieee80211_sub_if_data *sdata,
4908c2ecf20Sopenharmony_ci			struct sk_buff *skb)
4918c2ecf20Sopenharmony_ci{
4928c2ecf20Sopenharmony_ci	struct ieee80211_supported_band *sband;
4938c2ecf20Sopenharmony_ci	u8 *pos;
4948c2ecf20Sopenharmony_ci
4958c2ecf20Sopenharmony_ci	sband = ieee80211_get_sband(sdata);
4968c2ecf20Sopenharmony_ci	if (!sband)
4978c2ecf20Sopenharmony_ci		return -EINVAL;
4988c2ecf20Sopenharmony_ci
4998c2ecf20Sopenharmony_ci	/* VHT not allowed in 6 GHz */
5008c2ecf20Sopenharmony_ci	if (sband->band == NL80211_BAND_6GHZ)
5018c2ecf20Sopenharmony_ci		return 0;
5028c2ecf20Sopenharmony_ci
5038c2ecf20Sopenharmony_ci	if (!sband->vht_cap.vht_supported ||
5048c2ecf20Sopenharmony_ci	    sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_20_NOHT ||
5058c2ecf20Sopenharmony_ci	    sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_5 ||
5068c2ecf20Sopenharmony_ci	    sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_10)
5078c2ecf20Sopenharmony_ci		return 0;
5088c2ecf20Sopenharmony_ci
5098c2ecf20Sopenharmony_ci	if (skb_tailroom(skb) < 2 + sizeof(struct ieee80211_vht_cap))
5108c2ecf20Sopenharmony_ci		return -ENOMEM;
5118c2ecf20Sopenharmony_ci
5128c2ecf20Sopenharmony_ci	pos = skb_put(skb, 2 + sizeof(struct ieee80211_vht_cap));
5138c2ecf20Sopenharmony_ci	ieee80211_ie_build_vht_cap(pos, &sband->vht_cap, sband->vht_cap.cap);
5148c2ecf20Sopenharmony_ci
5158c2ecf20Sopenharmony_ci	return 0;
5168c2ecf20Sopenharmony_ci}
5178c2ecf20Sopenharmony_ci
5188c2ecf20Sopenharmony_ciint mesh_add_vht_oper_ie(struct ieee80211_sub_if_data *sdata,
5198c2ecf20Sopenharmony_ci			 struct sk_buff *skb)
5208c2ecf20Sopenharmony_ci{
5218c2ecf20Sopenharmony_ci	struct ieee80211_local *local = sdata->local;
5228c2ecf20Sopenharmony_ci	struct ieee80211_chanctx_conf *chanctx_conf;
5238c2ecf20Sopenharmony_ci	struct ieee80211_channel *channel;
5248c2ecf20Sopenharmony_ci	struct ieee80211_supported_band *sband;
5258c2ecf20Sopenharmony_ci	struct ieee80211_sta_vht_cap *vht_cap;
5268c2ecf20Sopenharmony_ci	u8 *pos;
5278c2ecf20Sopenharmony_ci
5288c2ecf20Sopenharmony_ci	rcu_read_lock();
5298c2ecf20Sopenharmony_ci	chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
5308c2ecf20Sopenharmony_ci	if (WARN_ON(!chanctx_conf)) {
5318c2ecf20Sopenharmony_ci		rcu_read_unlock();
5328c2ecf20Sopenharmony_ci		return -EINVAL;
5338c2ecf20Sopenharmony_ci	}
5348c2ecf20Sopenharmony_ci	channel = chanctx_conf->def.chan;
5358c2ecf20Sopenharmony_ci	rcu_read_unlock();
5368c2ecf20Sopenharmony_ci
5378c2ecf20Sopenharmony_ci	sband = local->hw.wiphy->bands[channel->band];
5388c2ecf20Sopenharmony_ci	vht_cap = &sband->vht_cap;
5398c2ecf20Sopenharmony_ci
5408c2ecf20Sopenharmony_ci	/* VHT not allowed in 6 GHz */
5418c2ecf20Sopenharmony_ci	if (sband->band == NL80211_BAND_6GHZ)
5428c2ecf20Sopenharmony_ci		return 0;
5438c2ecf20Sopenharmony_ci
5448c2ecf20Sopenharmony_ci	if (!vht_cap->vht_supported ||
5458c2ecf20Sopenharmony_ci	    sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_20_NOHT ||
5468c2ecf20Sopenharmony_ci	    sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_5 ||
5478c2ecf20Sopenharmony_ci	    sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_10)
5488c2ecf20Sopenharmony_ci		return 0;
5498c2ecf20Sopenharmony_ci
5508c2ecf20Sopenharmony_ci	if (skb_tailroom(skb) < 2 + sizeof(struct ieee80211_vht_operation))
5518c2ecf20Sopenharmony_ci		return -ENOMEM;
5528c2ecf20Sopenharmony_ci
5538c2ecf20Sopenharmony_ci	pos = skb_put(skb, 2 + sizeof(struct ieee80211_vht_operation));
5548c2ecf20Sopenharmony_ci	ieee80211_ie_build_vht_oper(pos, vht_cap,
5558c2ecf20Sopenharmony_ci				    &sdata->vif.bss_conf.chandef);
5568c2ecf20Sopenharmony_ci
5578c2ecf20Sopenharmony_ci	return 0;
5588c2ecf20Sopenharmony_ci}
5598c2ecf20Sopenharmony_ci
5608c2ecf20Sopenharmony_ciint mesh_add_he_cap_ie(struct ieee80211_sub_if_data *sdata,
5618c2ecf20Sopenharmony_ci		       struct sk_buff *skb, u8 ie_len)
5628c2ecf20Sopenharmony_ci{
5638c2ecf20Sopenharmony_ci	const struct ieee80211_sta_he_cap *he_cap;
5648c2ecf20Sopenharmony_ci	struct ieee80211_supported_band *sband;
5658c2ecf20Sopenharmony_ci	u8 *pos;
5668c2ecf20Sopenharmony_ci
5678c2ecf20Sopenharmony_ci	sband = ieee80211_get_sband(sdata);
5688c2ecf20Sopenharmony_ci	if (!sband)
5698c2ecf20Sopenharmony_ci		return -EINVAL;
5708c2ecf20Sopenharmony_ci
5718c2ecf20Sopenharmony_ci	he_cap = ieee80211_get_he_iftype_cap(sband, NL80211_IFTYPE_MESH_POINT);
5728c2ecf20Sopenharmony_ci
5738c2ecf20Sopenharmony_ci	if (!he_cap ||
5748c2ecf20Sopenharmony_ci	    sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_20_NOHT ||
5758c2ecf20Sopenharmony_ci	    sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_5 ||
5768c2ecf20Sopenharmony_ci	    sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_10)
5778c2ecf20Sopenharmony_ci		return 0;
5788c2ecf20Sopenharmony_ci
5798c2ecf20Sopenharmony_ci	if (skb_tailroom(skb) < ie_len)
5808c2ecf20Sopenharmony_ci		return -ENOMEM;
5818c2ecf20Sopenharmony_ci
5828c2ecf20Sopenharmony_ci	pos = skb_put(skb, ie_len);
5838c2ecf20Sopenharmony_ci	ieee80211_ie_build_he_cap(pos, he_cap, pos + ie_len);
5848c2ecf20Sopenharmony_ci
5858c2ecf20Sopenharmony_ci	return 0;
5868c2ecf20Sopenharmony_ci}
5878c2ecf20Sopenharmony_ci
5888c2ecf20Sopenharmony_ciint mesh_add_he_oper_ie(struct ieee80211_sub_if_data *sdata,
5898c2ecf20Sopenharmony_ci			struct sk_buff *skb)
5908c2ecf20Sopenharmony_ci{
5918c2ecf20Sopenharmony_ci	const struct ieee80211_sta_he_cap *he_cap;
5928c2ecf20Sopenharmony_ci	struct ieee80211_supported_band *sband;
5938c2ecf20Sopenharmony_ci	u32 len;
5948c2ecf20Sopenharmony_ci	u8 *pos;
5958c2ecf20Sopenharmony_ci
5968c2ecf20Sopenharmony_ci	sband = ieee80211_get_sband(sdata);
5978c2ecf20Sopenharmony_ci	if (!sband)
5988c2ecf20Sopenharmony_ci		return -EINVAL;
5998c2ecf20Sopenharmony_ci
6008c2ecf20Sopenharmony_ci	he_cap = ieee80211_get_he_iftype_cap(sband, NL80211_IFTYPE_MESH_POINT);
6018c2ecf20Sopenharmony_ci	if (!he_cap ||
6028c2ecf20Sopenharmony_ci	    sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_20_NOHT ||
6038c2ecf20Sopenharmony_ci	    sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_5 ||
6048c2ecf20Sopenharmony_ci	    sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_10)
6058c2ecf20Sopenharmony_ci		return 0;
6068c2ecf20Sopenharmony_ci
6078c2ecf20Sopenharmony_ci	len = 2 + 1 + sizeof(struct ieee80211_he_operation);
6088c2ecf20Sopenharmony_ci	if (sdata->vif.bss_conf.chandef.chan->band == NL80211_BAND_6GHZ)
6098c2ecf20Sopenharmony_ci		len += sizeof(struct ieee80211_he_6ghz_oper);
6108c2ecf20Sopenharmony_ci
6118c2ecf20Sopenharmony_ci	if (skb_tailroom(skb) < len)
6128c2ecf20Sopenharmony_ci		return -ENOMEM;
6138c2ecf20Sopenharmony_ci
6148c2ecf20Sopenharmony_ci	pos = skb_put(skb, len);
6158c2ecf20Sopenharmony_ci	ieee80211_ie_build_he_oper(pos, &sdata->vif.bss_conf.chandef);
6168c2ecf20Sopenharmony_ci
6178c2ecf20Sopenharmony_ci	return 0;
6188c2ecf20Sopenharmony_ci}
6198c2ecf20Sopenharmony_ci
6208c2ecf20Sopenharmony_ciint mesh_add_he_6ghz_cap_ie(struct ieee80211_sub_if_data *sdata,
6218c2ecf20Sopenharmony_ci			    struct sk_buff *skb)
6228c2ecf20Sopenharmony_ci{
6238c2ecf20Sopenharmony_ci	struct ieee80211_supported_band *sband;
6248c2ecf20Sopenharmony_ci	const struct ieee80211_sband_iftype_data *iftd;
6258c2ecf20Sopenharmony_ci
6268c2ecf20Sopenharmony_ci	sband = ieee80211_get_sband(sdata);
6278c2ecf20Sopenharmony_ci	if (!sband)
6288c2ecf20Sopenharmony_ci		return -EINVAL;
6298c2ecf20Sopenharmony_ci
6308c2ecf20Sopenharmony_ci	iftd = ieee80211_get_sband_iftype_data(sband,
6318c2ecf20Sopenharmony_ci					       NL80211_IFTYPE_MESH_POINT);
6328c2ecf20Sopenharmony_ci	/* The device doesn't support HE in mesh mode or at all */
6338c2ecf20Sopenharmony_ci	if (!iftd)
6348c2ecf20Sopenharmony_ci		return 0;
6358c2ecf20Sopenharmony_ci
6368c2ecf20Sopenharmony_ci	ieee80211_ie_build_he_6ghz_cap(sdata, skb);
6378c2ecf20Sopenharmony_ci	return 0;
6388c2ecf20Sopenharmony_ci}
6398c2ecf20Sopenharmony_ci
6408c2ecf20Sopenharmony_cistatic void ieee80211_mesh_path_timer(struct timer_list *t)
6418c2ecf20Sopenharmony_ci{
6428c2ecf20Sopenharmony_ci	struct ieee80211_sub_if_data *sdata =
6438c2ecf20Sopenharmony_ci		from_timer(sdata, t, u.mesh.mesh_path_timer);
6448c2ecf20Sopenharmony_ci
6458c2ecf20Sopenharmony_ci	ieee80211_queue_work(&sdata->local->hw, &sdata->work);
6468c2ecf20Sopenharmony_ci}
6478c2ecf20Sopenharmony_ci
6488c2ecf20Sopenharmony_cistatic void ieee80211_mesh_path_root_timer(struct timer_list *t)
6498c2ecf20Sopenharmony_ci{
6508c2ecf20Sopenharmony_ci	struct ieee80211_sub_if_data *sdata =
6518c2ecf20Sopenharmony_ci		from_timer(sdata, t, u.mesh.mesh_path_root_timer);
6528c2ecf20Sopenharmony_ci	struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
6538c2ecf20Sopenharmony_ci
6548c2ecf20Sopenharmony_ci	set_bit(MESH_WORK_ROOT, &ifmsh->wrkq_flags);
6558c2ecf20Sopenharmony_ci
6568c2ecf20Sopenharmony_ci	ieee80211_queue_work(&sdata->local->hw, &sdata->work);
6578c2ecf20Sopenharmony_ci}
6588c2ecf20Sopenharmony_ci
6598c2ecf20Sopenharmony_civoid ieee80211_mesh_root_setup(struct ieee80211_if_mesh *ifmsh)
6608c2ecf20Sopenharmony_ci{
6618c2ecf20Sopenharmony_ci	if (ifmsh->mshcfg.dot11MeshHWMPRootMode > IEEE80211_ROOTMODE_ROOT)
6628c2ecf20Sopenharmony_ci		set_bit(MESH_WORK_ROOT, &ifmsh->wrkq_flags);
6638c2ecf20Sopenharmony_ci	else {
6648c2ecf20Sopenharmony_ci		clear_bit(MESH_WORK_ROOT, &ifmsh->wrkq_flags);
6658c2ecf20Sopenharmony_ci		/* stop running timer */
6668c2ecf20Sopenharmony_ci		del_timer_sync(&ifmsh->mesh_path_root_timer);
6678c2ecf20Sopenharmony_ci	}
6688c2ecf20Sopenharmony_ci}
6698c2ecf20Sopenharmony_ci
6708c2ecf20Sopenharmony_ci/**
6718c2ecf20Sopenharmony_ci * ieee80211_fill_mesh_addresses - fill addresses of a locally originated mesh frame
6728c2ecf20Sopenharmony_ci * @hdr:	802.11 frame header
6738c2ecf20Sopenharmony_ci * @fc:		frame control field
6748c2ecf20Sopenharmony_ci * @meshda:	destination address in the mesh
6758c2ecf20Sopenharmony_ci * @meshsa:	source address in the mesh.  Same as TA, as frame is
6768c2ecf20Sopenharmony_ci *              locally originated.
6778c2ecf20Sopenharmony_ci *
6788c2ecf20Sopenharmony_ci * Return the length of the 802.11 (does not include a mesh control header)
6798c2ecf20Sopenharmony_ci */
6808c2ecf20Sopenharmony_ciint ieee80211_fill_mesh_addresses(struct ieee80211_hdr *hdr, __le16 *fc,
6818c2ecf20Sopenharmony_ci				  const u8 *meshda, const u8 *meshsa)
6828c2ecf20Sopenharmony_ci{
6838c2ecf20Sopenharmony_ci	if (is_multicast_ether_addr(meshda)) {
6848c2ecf20Sopenharmony_ci		*fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
6858c2ecf20Sopenharmony_ci		/* DA TA SA */
6868c2ecf20Sopenharmony_ci		memcpy(hdr->addr1, meshda, ETH_ALEN);
6878c2ecf20Sopenharmony_ci		memcpy(hdr->addr2, meshsa, ETH_ALEN);
6888c2ecf20Sopenharmony_ci		memcpy(hdr->addr3, meshsa, ETH_ALEN);
6898c2ecf20Sopenharmony_ci		return 24;
6908c2ecf20Sopenharmony_ci	} else {
6918c2ecf20Sopenharmony_ci		*fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
6928c2ecf20Sopenharmony_ci		/* RA TA DA SA */
6938c2ecf20Sopenharmony_ci		eth_zero_addr(hdr->addr1);   /* RA is resolved later */
6948c2ecf20Sopenharmony_ci		memcpy(hdr->addr2, meshsa, ETH_ALEN);
6958c2ecf20Sopenharmony_ci		memcpy(hdr->addr3, meshda, ETH_ALEN);
6968c2ecf20Sopenharmony_ci		memcpy(hdr->addr4, meshsa, ETH_ALEN);
6978c2ecf20Sopenharmony_ci		return 30;
6988c2ecf20Sopenharmony_ci	}
6998c2ecf20Sopenharmony_ci}
7008c2ecf20Sopenharmony_ci
7018c2ecf20Sopenharmony_ci/**
7028c2ecf20Sopenharmony_ci * ieee80211_new_mesh_header - create a new mesh header
7038c2ecf20Sopenharmony_ci * @sdata:	mesh interface to be used
7048c2ecf20Sopenharmony_ci * @meshhdr:    uninitialized mesh header
7058c2ecf20Sopenharmony_ci * @addr4or5:   1st address in the ae header, which may correspond to address 4
7068c2ecf20Sopenharmony_ci *              (if addr6 is NULL) or address 5 (if addr6 is present). It may
7078c2ecf20Sopenharmony_ci *              be NULL.
7088c2ecf20Sopenharmony_ci * @addr6:	2nd address in the ae header, which corresponds to addr6 of the
7098c2ecf20Sopenharmony_ci *              mesh frame
7108c2ecf20Sopenharmony_ci *
7118c2ecf20Sopenharmony_ci * Return the header length.
7128c2ecf20Sopenharmony_ci */
7138c2ecf20Sopenharmony_ciunsigned int ieee80211_new_mesh_header(struct ieee80211_sub_if_data *sdata,
7148c2ecf20Sopenharmony_ci				       struct ieee80211s_hdr *meshhdr,
7158c2ecf20Sopenharmony_ci				       const char *addr4or5, const char *addr6)
7168c2ecf20Sopenharmony_ci{
7178c2ecf20Sopenharmony_ci	if (WARN_ON(!addr4or5 && addr6))
7188c2ecf20Sopenharmony_ci		return 0;
7198c2ecf20Sopenharmony_ci
7208c2ecf20Sopenharmony_ci	memset(meshhdr, 0, sizeof(*meshhdr));
7218c2ecf20Sopenharmony_ci
7228c2ecf20Sopenharmony_ci	meshhdr->ttl = sdata->u.mesh.mshcfg.dot11MeshTTL;
7238c2ecf20Sopenharmony_ci
7248c2ecf20Sopenharmony_ci	/* FIXME: racy -- TX on multiple queues can be concurrent */
7258c2ecf20Sopenharmony_ci	put_unaligned(cpu_to_le32(sdata->u.mesh.mesh_seqnum), &meshhdr->seqnum);
7268c2ecf20Sopenharmony_ci	sdata->u.mesh.mesh_seqnum++;
7278c2ecf20Sopenharmony_ci
7288c2ecf20Sopenharmony_ci	if (addr4or5 && !addr6) {
7298c2ecf20Sopenharmony_ci		meshhdr->flags |= MESH_FLAGS_AE_A4;
7308c2ecf20Sopenharmony_ci		memcpy(meshhdr->eaddr1, addr4or5, ETH_ALEN);
7318c2ecf20Sopenharmony_ci		return 2 * ETH_ALEN;
7328c2ecf20Sopenharmony_ci	} else if (addr4or5 && addr6) {
7338c2ecf20Sopenharmony_ci		meshhdr->flags |= MESH_FLAGS_AE_A5_A6;
7348c2ecf20Sopenharmony_ci		memcpy(meshhdr->eaddr1, addr4or5, ETH_ALEN);
7358c2ecf20Sopenharmony_ci		memcpy(meshhdr->eaddr2, addr6, ETH_ALEN);
7368c2ecf20Sopenharmony_ci		return 3 * ETH_ALEN;
7378c2ecf20Sopenharmony_ci	}
7388c2ecf20Sopenharmony_ci
7398c2ecf20Sopenharmony_ci	return ETH_ALEN;
7408c2ecf20Sopenharmony_ci}
7418c2ecf20Sopenharmony_ci
7428c2ecf20Sopenharmony_cistatic void ieee80211_mesh_housekeeping(struct ieee80211_sub_if_data *sdata)
7438c2ecf20Sopenharmony_ci{
7448c2ecf20Sopenharmony_ci	struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
7458c2ecf20Sopenharmony_ci	u32 changed;
7468c2ecf20Sopenharmony_ci
7478c2ecf20Sopenharmony_ci	if (ifmsh->mshcfg.plink_timeout > 0)
7488c2ecf20Sopenharmony_ci		ieee80211_sta_expire(sdata, ifmsh->mshcfg.plink_timeout * HZ);
7498c2ecf20Sopenharmony_ci	mesh_path_expire(sdata);
7508c2ecf20Sopenharmony_ci
7518c2ecf20Sopenharmony_ci	changed = mesh_accept_plinks_update(sdata);
7528c2ecf20Sopenharmony_ci	ieee80211_mbss_info_change_notify(sdata, changed);
7538c2ecf20Sopenharmony_ci
7548c2ecf20Sopenharmony_ci	mod_timer(&ifmsh->housekeeping_timer,
7558c2ecf20Sopenharmony_ci		  round_jiffies(jiffies +
7568c2ecf20Sopenharmony_ci				IEEE80211_MESH_HOUSEKEEPING_INTERVAL));
7578c2ecf20Sopenharmony_ci}
7588c2ecf20Sopenharmony_ci
7598c2ecf20Sopenharmony_cistatic void ieee80211_mesh_rootpath(struct ieee80211_sub_if_data *sdata)
7608c2ecf20Sopenharmony_ci{
7618c2ecf20Sopenharmony_ci	struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
7628c2ecf20Sopenharmony_ci	u32 interval;
7638c2ecf20Sopenharmony_ci
7648c2ecf20Sopenharmony_ci	mesh_path_tx_root_frame(sdata);
7658c2ecf20Sopenharmony_ci
7668c2ecf20Sopenharmony_ci	if (ifmsh->mshcfg.dot11MeshHWMPRootMode == IEEE80211_PROACTIVE_RANN)
7678c2ecf20Sopenharmony_ci		interval = ifmsh->mshcfg.dot11MeshHWMPRannInterval;
7688c2ecf20Sopenharmony_ci	else
7698c2ecf20Sopenharmony_ci		interval = ifmsh->mshcfg.dot11MeshHWMProotInterval;
7708c2ecf20Sopenharmony_ci
7718c2ecf20Sopenharmony_ci	mod_timer(&ifmsh->mesh_path_root_timer,
7728c2ecf20Sopenharmony_ci		  round_jiffies(TU_TO_EXP_TIME(interval)));
7738c2ecf20Sopenharmony_ci}
7748c2ecf20Sopenharmony_ci
7758c2ecf20Sopenharmony_cistatic int
7768c2ecf20Sopenharmony_ciieee80211_mesh_build_beacon(struct ieee80211_if_mesh *ifmsh)
7778c2ecf20Sopenharmony_ci{
7788c2ecf20Sopenharmony_ci	struct beacon_data *bcn;
7798c2ecf20Sopenharmony_ci	int head_len, tail_len;
7808c2ecf20Sopenharmony_ci	struct sk_buff *skb;
7818c2ecf20Sopenharmony_ci	struct ieee80211_mgmt *mgmt;
7828c2ecf20Sopenharmony_ci	struct ieee80211_chanctx_conf *chanctx_conf;
7838c2ecf20Sopenharmony_ci	struct mesh_csa_settings *csa;
7848c2ecf20Sopenharmony_ci	enum nl80211_band band;
7858c2ecf20Sopenharmony_ci	u8 ie_len_he_cap;
7868c2ecf20Sopenharmony_ci	u8 *pos;
7878c2ecf20Sopenharmony_ci	struct ieee80211_sub_if_data *sdata;
7888c2ecf20Sopenharmony_ci	int hdr_len = offsetofend(struct ieee80211_mgmt, u.beacon);
7898c2ecf20Sopenharmony_ci
7908c2ecf20Sopenharmony_ci	sdata = container_of(ifmsh, struct ieee80211_sub_if_data, u.mesh);
7918c2ecf20Sopenharmony_ci	rcu_read_lock();
7928c2ecf20Sopenharmony_ci	chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
7938c2ecf20Sopenharmony_ci	band = chanctx_conf->def.chan->band;
7948c2ecf20Sopenharmony_ci	rcu_read_unlock();
7958c2ecf20Sopenharmony_ci
7968c2ecf20Sopenharmony_ci	ie_len_he_cap = ieee80211_ie_len_he_cap(sdata,
7978c2ecf20Sopenharmony_ci						NL80211_IFTYPE_MESH_POINT);
7988c2ecf20Sopenharmony_ci	head_len = hdr_len +
7998c2ecf20Sopenharmony_ci		   2 + /* NULL SSID */
8008c2ecf20Sopenharmony_ci		   /* Channel Switch Announcement */
8018c2ecf20Sopenharmony_ci		   2 + sizeof(struct ieee80211_channel_sw_ie) +
8028c2ecf20Sopenharmony_ci		   /* Mesh Channel Switch Parameters */
8038c2ecf20Sopenharmony_ci		   2 + sizeof(struct ieee80211_mesh_chansw_params_ie) +
8048c2ecf20Sopenharmony_ci		   /* Channel Switch Wrapper + Wide Bandwidth CSA IE */
8058c2ecf20Sopenharmony_ci		   2 + 2 + sizeof(struct ieee80211_wide_bw_chansw_ie) +
8068c2ecf20Sopenharmony_ci		   2 + sizeof(struct ieee80211_sec_chan_offs_ie) +
8078c2ecf20Sopenharmony_ci		   2 + 8 + /* supported rates */
8088c2ecf20Sopenharmony_ci		   2 + 3; /* DS params */
8098c2ecf20Sopenharmony_ci	tail_len = 2 + (IEEE80211_MAX_SUPP_RATES - 8) +
8108c2ecf20Sopenharmony_ci		   2 + sizeof(struct ieee80211_ht_cap) +
8118c2ecf20Sopenharmony_ci		   2 + sizeof(struct ieee80211_ht_operation) +
8128c2ecf20Sopenharmony_ci		   2 + ifmsh->mesh_id_len +
8138c2ecf20Sopenharmony_ci		   2 + sizeof(struct ieee80211_meshconf_ie) +
8148c2ecf20Sopenharmony_ci		   2 + sizeof(__le16) + /* awake window */
8158c2ecf20Sopenharmony_ci		   2 + sizeof(struct ieee80211_vht_cap) +
8168c2ecf20Sopenharmony_ci		   2 + sizeof(struct ieee80211_vht_operation) +
8178c2ecf20Sopenharmony_ci		   ie_len_he_cap +
8188c2ecf20Sopenharmony_ci		   2 + 1 + sizeof(struct ieee80211_he_operation) +
8198c2ecf20Sopenharmony_ci			   sizeof(struct ieee80211_he_6ghz_oper) +
8208c2ecf20Sopenharmony_ci		   2 + 1 + sizeof(struct ieee80211_he_6ghz_capa) +
8218c2ecf20Sopenharmony_ci		   ifmsh->ie_len;
8228c2ecf20Sopenharmony_ci
8238c2ecf20Sopenharmony_ci	bcn = kzalloc(sizeof(*bcn) + head_len + tail_len, GFP_KERNEL);
8248c2ecf20Sopenharmony_ci	/* need an skb for IE builders to operate on */
8258c2ecf20Sopenharmony_ci	skb = dev_alloc_skb(max(head_len, tail_len));
8268c2ecf20Sopenharmony_ci
8278c2ecf20Sopenharmony_ci	if (!bcn || !skb)
8288c2ecf20Sopenharmony_ci		goto out_free;
8298c2ecf20Sopenharmony_ci
8308c2ecf20Sopenharmony_ci	/*
8318c2ecf20Sopenharmony_ci	 * pointers go into the block we allocated,
8328c2ecf20Sopenharmony_ci	 * memory is | beacon_data | head | tail |
8338c2ecf20Sopenharmony_ci	 */
8348c2ecf20Sopenharmony_ci	bcn->head = ((u8 *) bcn) + sizeof(*bcn);
8358c2ecf20Sopenharmony_ci
8368c2ecf20Sopenharmony_ci	/* fill in the head */
8378c2ecf20Sopenharmony_ci	mgmt = skb_put_zero(skb, hdr_len);
8388c2ecf20Sopenharmony_ci	mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
8398c2ecf20Sopenharmony_ci					  IEEE80211_STYPE_BEACON);
8408c2ecf20Sopenharmony_ci	eth_broadcast_addr(mgmt->da);
8418c2ecf20Sopenharmony_ci	memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
8428c2ecf20Sopenharmony_ci	memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
8438c2ecf20Sopenharmony_ci	ieee80211_mps_set_frame_flags(sdata, NULL, (void *) mgmt);
8448c2ecf20Sopenharmony_ci	mgmt->u.beacon.beacon_int =
8458c2ecf20Sopenharmony_ci		cpu_to_le16(sdata->vif.bss_conf.beacon_int);
8468c2ecf20Sopenharmony_ci	mgmt->u.beacon.capab_info |= cpu_to_le16(
8478c2ecf20Sopenharmony_ci		sdata->u.mesh.security ? WLAN_CAPABILITY_PRIVACY : 0);
8488c2ecf20Sopenharmony_ci
8498c2ecf20Sopenharmony_ci	pos = skb_put(skb, 2);
8508c2ecf20Sopenharmony_ci	*pos++ = WLAN_EID_SSID;
8518c2ecf20Sopenharmony_ci	*pos++ = 0x0;
8528c2ecf20Sopenharmony_ci
8538c2ecf20Sopenharmony_ci	rcu_read_lock();
8548c2ecf20Sopenharmony_ci	csa = rcu_dereference(ifmsh->csa);
8558c2ecf20Sopenharmony_ci	if (csa) {
8568c2ecf20Sopenharmony_ci		enum nl80211_channel_type ct;
8578c2ecf20Sopenharmony_ci		struct cfg80211_chan_def *chandef;
8588c2ecf20Sopenharmony_ci		int ie_len = 2 + sizeof(struct ieee80211_channel_sw_ie) +
8598c2ecf20Sopenharmony_ci			     2 + sizeof(struct ieee80211_mesh_chansw_params_ie);
8608c2ecf20Sopenharmony_ci
8618c2ecf20Sopenharmony_ci		pos = skb_put_zero(skb, ie_len);
8628c2ecf20Sopenharmony_ci		*pos++ = WLAN_EID_CHANNEL_SWITCH;
8638c2ecf20Sopenharmony_ci		*pos++ = 3;
8648c2ecf20Sopenharmony_ci		*pos++ = 0x0;
8658c2ecf20Sopenharmony_ci		*pos++ = ieee80211_frequency_to_channel(
8668c2ecf20Sopenharmony_ci				csa->settings.chandef.chan->center_freq);
8678c2ecf20Sopenharmony_ci		bcn->cntdwn_current_counter = csa->settings.count;
8688c2ecf20Sopenharmony_ci		bcn->cntdwn_counter_offsets[0] = hdr_len + 6;
8698c2ecf20Sopenharmony_ci		*pos++ = csa->settings.count;
8708c2ecf20Sopenharmony_ci		*pos++ = WLAN_EID_CHAN_SWITCH_PARAM;
8718c2ecf20Sopenharmony_ci		*pos++ = 6;
8728c2ecf20Sopenharmony_ci		if (ifmsh->csa_role == IEEE80211_MESH_CSA_ROLE_INIT) {
8738c2ecf20Sopenharmony_ci			*pos++ = ifmsh->mshcfg.dot11MeshTTL;
8748c2ecf20Sopenharmony_ci			*pos |= WLAN_EID_CHAN_SWITCH_PARAM_INITIATOR;
8758c2ecf20Sopenharmony_ci		} else {
8768c2ecf20Sopenharmony_ci			*pos++ = ifmsh->chsw_ttl;
8778c2ecf20Sopenharmony_ci		}
8788c2ecf20Sopenharmony_ci		*pos++ |= csa->settings.block_tx ?
8798c2ecf20Sopenharmony_ci			  WLAN_EID_CHAN_SWITCH_PARAM_TX_RESTRICT : 0x00;
8808c2ecf20Sopenharmony_ci		put_unaligned_le16(WLAN_REASON_MESH_CHAN, pos);
8818c2ecf20Sopenharmony_ci		pos += 2;
8828c2ecf20Sopenharmony_ci		put_unaligned_le16(ifmsh->pre_value, pos);
8838c2ecf20Sopenharmony_ci		pos += 2;
8848c2ecf20Sopenharmony_ci
8858c2ecf20Sopenharmony_ci		switch (csa->settings.chandef.width) {
8868c2ecf20Sopenharmony_ci		case NL80211_CHAN_WIDTH_40:
8878c2ecf20Sopenharmony_ci			ie_len = 2 + sizeof(struct ieee80211_sec_chan_offs_ie);
8888c2ecf20Sopenharmony_ci			pos = skb_put_zero(skb, ie_len);
8898c2ecf20Sopenharmony_ci
8908c2ecf20Sopenharmony_ci			*pos++ = WLAN_EID_SECONDARY_CHANNEL_OFFSET; /* EID */
8918c2ecf20Sopenharmony_ci			*pos++ = 1;				    /* len */
8928c2ecf20Sopenharmony_ci			ct = cfg80211_get_chandef_type(&csa->settings.chandef);
8938c2ecf20Sopenharmony_ci			if (ct == NL80211_CHAN_HT40PLUS)
8948c2ecf20Sopenharmony_ci				*pos++ = IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
8958c2ecf20Sopenharmony_ci			else
8968c2ecf20Sopenharmony_ci				*pos++ = IEEE80211_HT_PARAM_CHA_SEC_BELOW;
8978c2ecf20Sopenharmony_ci			break;
8988c2ecf20Sopenharmony_ci		case NL80211_CHAN_WIDTH_80:
8998c2ecf20Sopenharmony_ci		case NL80211_CHAN_WIDTH_80P80:
9008c2ecf20Sopenharmony_ci		case NL80211_CHAN_WIDTH_160:
9018c2ecf20Sopenharmony_ci			/* Channel Switch Wrapper + Wide Bandwidth CSA IE */
9028c2ecf20Sopenharmony_ci			ie_len = 2 + 2 +
9038c2ecf20Sopenharmony_ci				 sizeof(struct ieee80211_wide_bw_chansw_ie);
9048c2ecf20Sopenharmony_ci			pos = skb_put_zero(skb, ie_len);
9058c2ecf20Sopenharmony_ci
9068c2ecf20Sopenharmony_ci			*pos++ = WLAN_EID_CHANNEL_SWITCH_WRAPPER; /* EID */
9078c2ecf20Sopenharmony_ci			*pos++ = 5;				  /* len */
9088c2ecf20Sopenharmony_ci			/* put sub IE */
9098c2ecf20Sopenharmony_ci			chandef = &csa->settings.chandef;
9108c2ecf20Sopenharmony_ci			ieee80211_ie_build_wide_bw_cs(pos, chandef);
9118c2ecf20Sopenharmony_ci			break;
9128c2ecf20Sopenharmony_ci		default:
9138c2ecf20Sopenharmony_ci			break;
9148c2ecf20Sopenharmony_ci		}
9158c2ecf20Sopenharmony_ci	}
9168c2ecf20Sopenharmony_ci	rcu_read_unlock();
9178c2ecf20Sopenharmony_ci
9188c2ecf20Sopenharmony_ci	if (ieee80211_add_srates_ie(sdata, skb, true, band) ||
9198c2ecf20Sopenharmony_ci	    mesh_add_ds_params_ie(sdata, skb))
9208c2ecf20Sopenharmony_ci		goto out_free;
9218c2ecf20Sopenharmony_ci
9228c2ecf20Sopenharmony_ci	bcn->head_len = skb->len;
9238c2ecf20Sopenharmony_ci	memcpy(bcn->head, skb->data, bcn->head_len);
9248c2ecf20Sopenharmony_ci
9258c2ecf20Sopenharmony_ci	/* now the tail */
9268c2ecf20Sopenharmony_ci	skb_trim(skb, 0);
9278c2ecf20Sopenharmony_ci	bcn->tail = bcn->head + bcn->head_len;
9288c2ecf20Sopenharmony_ci
9298c2ecf20Sopenharmony_ci	if (ieee80211_add_ext_srates_ie(sdata, skb, true, band) ||
9308c2ecf20Sopenharmony_ci	    mesh_add_rsn_ie(sdata, skb) ||
9318c2ecf20Sopenharmony_ci	    mesh_add_ht_cap_ie(sdata, skb) ||
9328c2ecf20Sopenharmony_ci	    mesh_add_ht_oper_ie(sdata, skb) ||
9338c2ecf20Sopenharmony_ci	    mesh_add_meshid_ie(sdata, skb) ||
9348c2ecf20Sopenharmony_ci	    mesh_add_meshconf_ie(sdata, skb) ||
9358c2ecf20Sopenharmony_ci	    mesh_add_awake_window_ie(sdata, skb) ||
9368c2ecf20Sopenharmony_ci	    mesh_add_vht_cap_ie(sdata, skb) ||
9378c2ecf20Sopenharmony_ci	    mesh_add_vht_oper_ie(sdata, skb) ||
9388c2ecf20Sopenharmony_ci	    mesh_add_he_cap_ie(sdata, skb, ie_len_he_cap) ||
9398c2ecf20Sopenharmony_ci	    mesh_add_he_oper_ie(sdata, skb) ||
9408c2ecf20Sopenharmony_ci	    mesh_add_he_6ghz_cap_ie(sdata, skb) ||
9418c2ecf20Sopenharmony_ci	    mesh_add_vendor_ies(sdata, skb))
9428c2ecf20Sopenharmony_ci		goto out_free;
9438c2ecf20Sopenharmony_ci
9448c2ecf20Sopenharmony_ci	bcn->tail_len = skb->len;
9458c2ecf20Sopenharmony_ci	memcpy(bcn->tail, skb->data, bcn->tail_len);
9468c2ecf20Sopenharmony_ci	bcn->meshconf = (struct ieee80211_meshconf_ie *)
9478c2ecf20Sopenharmony_ci					(bcn->tail + ifmsh->meshconf_offset);
9488c2ecf20Sopenharmony_ci
9498c2ecf20Sopenharmony_ci	dev_kfree_skb(skb);
9508c2ecf20Sopenharmony_ci	rcu_assign_pointer(ifmsh->beacon, bcn);
9518c2ecf20Sopenharmony_ci	return 0;
9528c2ecf20Sopenharmony_ciout_free:
9538c2ecf20Sopenharmony_ci	kfree(bcn);
9548c2ecf20Sopenharmony_ci	dev_kfree_skb(skb);
9558c2ecf20Sopenharmony_ci	return -ENOMEM;
9568c2ecf20Sopenharmony_ci}
9578c2ecf20Sopenharmony_ci
9588c2ecf20Sopenharmony_cistatic int
9598c2ecf20Sopenharmony_ciieee80211_mesh_rebuild_beacon(struct ieee80211_sub_if_data *sdata)
9608c2ecf20Sopenharmony_ci{
9618c2ecf20Sopenharmony_ci	struct beacon_data *old_bcn;
9628c2ecf20Sopenharmony_ci	int ret;
9638c2ecf20Sopenharmony_ci
9648c2ecf20Sopenharmony_ci	old_bcn = rcu_dereference_protected(sdata->u.mesh.beacon,
9658c2ecf20Sopenharmony_ci					    lockdep_is_held(&sdata->wdev.mtx));
9668c2ecf20Sopenharmony_ci	ret = ieee80211_mesh_build_beacon(&sdata->u.mesh);
9678c2ecf20Sopenharmony_ci	if (ret)
9688c2ecf20Sopenharmony_ci		/* just reuse old beacon */
9698c2ecf20Sopenharmony_ci		return ret;
9708c2ecf20Sopenharmony_ci
9718c2ecf20Sopenharmony_ci	if (old_bcn)
9728c2ecf20Sopenharmony_ci		kfree_rcu(old_bcn, rcu_head);
9738c2ecf20Sopenharmony_ci	return 0;
9748c2ecf20Sopenharmony_ci}
9758c2ecf20Sopenharmony_ci
9768c2ecf20Sopenharmony_civoid ieee80211_mbss_info_change_notify(struct ieee80211_sub_if_data *sdata,
9778c2ecf20Sopenharmony_ci				       u32 changed)
9788c2ecf20Sopenharmony_ci{
9798c2ecf20Sopenharmony_ci	struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
9808c2ecf20Sopenharmony_ci	unsigned long bits = changed;
9818c2ecf20Sopenharmony_ci	u32 bit;
9828c2ecf20Sopenharmony_ci
9838c2ecf20Sopenharmony_ci	if (!bits)
9848c2ecf20Sopenharmony_ci		return;
9858c2ecf20Sopenharmony_ci
9868c2ecf20Sopenharmony_ci	/* if we race with running work, worst case this work becomes a noop */
9878c2ecf20Sopenharmony_ci	for_each_set_bit(bit, &bits, sizeof(changed) * BITS_PER_BYTE)
9888c2ecf20Sopenharmony_ci		set_bit(bit, &ifmsh->mbss_changed);
9898c2ecf20Sopenharmony_ci	set_bit(MESH_WORK_MBSS_CHANGED, &ifmsh->wrkq_flags);
9908c2ecf20Sopenharmony_ci	ieee80211_queue_work(&sdata->local->hw, &sdata->work);
9918c2ecf20Sopenharmony_ci}
9928c2ecf20Sopenharmony_ci
9938c2ecf20Sopenharmony_ciint ieee80211_start_mesh(struct ieee80211_sub_if_data *sdata)
9948c2ecf20Sopenharmony_ci{
9958c2ecf20Sopenharmony_ci	struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
9968c2ecf20Sopenharmony_ci	struct ieee80211_local *local = sdata->local;
9978c2ecf20Sopenharmony_ci	u32 changed = BSS_CHANGED_BEACON |
9988c2ecf20Sopenharmony_ci		      BSS_CHANGED_BEACON_ENABLED |
9998c2ecf20Sopenharmony_ci		      BSS_CHANGED_HT |
10008c2ecf20Sopenharmony_ci		      BSS_CHANGED_BASIC_RATES |
10018c2ecf20Sopenharmony_ci		      BSS_CHANGED_BEACON_INT |
10028c2ecf20Sopenharmony_ci		      BSS_CHANGED_MCAST_RATE;
10038c2ecf20Sopenharmony_ci
10048c2ecf20Sopenharmony_ci	local->fif_other_bss++;
10058c2ecf20Sopenharmony_ci	/* mesh ifaces must set allmulti to forward mcast traffic */
10068c2ecf20Sopenharmony_ci	atomic_inc(&local->iff_allmultis);
10078c2ecf20Sopenharmony_ci	ieee80211_configure_filter(local);
10088c2ecf20Sopenharmony_ci
10098c2ecf20Sopenharmony_ci	ifmsh->mesh_cc_id = 0;	/* Disabled */
10108c2ecf20Sopenharmony_ci	/* register sync ops from extensible synchronization framework */
10118c2ecf20Sopenharmony_ci	ifmsh->sync_ops = ieee80211_mesh_sync_ops_get(ifmsh->mesh_sp_id);
10128c2ecf20Sopenharmony_ci	ifmsh->sync_offset_clockdrift_max = 0;
10138c2ecf20Sopenharmony_ci	set_bit(MESH_WORK_HOUSEKEEPING, &ifmsh->wrkq_flags);
10148c2ecf20Sopenharmony_ci	ieee80211_mesh_root_setup(ifmsh);
10158c2ecf20Sopenharmony_ci	ieee80211_queue_work(&local->hw, &sdata->work);
10168c2ecf20Sopenharmony_ci	sdata->vif.bss_conf.ht_operation_mode =
10178c2ecf20Sopenharmony_ci				ifmsh->mshcfg.ht_opmode;
10188c2ecf20Sopenharmony_ci	sdata->vif.bss_conf.enable_beacon = true;
10198c2ecf20Sopenharmony_ci
10208c2ecf20Sopenharmony_ci	changed |= ieee80211_mps_local_status_update(sdata);
10218c2ecf20Sopenharmony_ci
10228c2ecf20Sopenharmony_ci	if (ieee80211_mesh_build_beacon(ifmsh)) {
10238c2ecf20Sopenharmony_ci		ieee80211_stop_mesh(sdata);
10248c2ecf20Sopenharmony_ci		return -ENOMEM;
10258c2ecf20Sopenharmony_ci	}
10268c2ecf20Sopenharmony_ci
10278c2ecf20Sopenharmony_ci	ieee80211_recalc_dtim(local, sdata);
10288c2ecf20Sopenharmony_ci	ieee80211_bss_info_change_notify(sdata, changed);
10298c2ecf20Sopenharmony_ci
10308c2ecf20Sopenharmony_ci	netif_carrier_on(sdata->dev);
10318c2ecf20Sopenharmony_ci	return 0;
10328c2ecf20Sopenharmony_ci}
10338c2ecf20Sopenharmony_ci
10348c2ecf20Sopenharmony_civoid ieee80211_stop_mesh(struct ieee80211_sub_if_data *sdata)
10358c2ecf20Sopenharmony_ci{
10368c2ecf20Sopenharmony_ci	struct ieee80211_local *local = sdata->local;
10378c2ecf20Sopenharmony_ci	struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
10388c2ecf20Sopenharmony_ci	struct beacon_data *bcn;
10398c2ecf20Sopenharmony_ci
10408c2ecf20Sopenharmony_ci	netif_carrier_off(sdata->dev);
10418c2ecf20Sopenharmony_ci
10428c2ecf20Sopenharmony_ci	/* flush STAs and mpaths on this iface */
10438c2ecf20Sopenharmony_ci	sta_info_flush(sdata);
10448c2ecf20Sopenharmony_ci	ieee80211_free_keys(sdata, true);
10458c2ecf20Sopenharmony_ci	mesh_path_flush_by_iface(sdata);
10468c2ecf20Sopenharmony_ci
10478c2ecf20Sopenharmony_ci	/* stop the beacon */
10488c2ecf20Sopenharmony_ci	ifmsh->mesh_id_len = 0;
10498c2ecf20Sopenharmony_ci	sdata->vif.bss_conf.enable_beacon = false;
10508c2ecf20Sopenharmony_ci	sdata->beacon_rate_set = false;
10518c2ecf20Sopenharmony_ci	clear_bit(SDATA_STATE_OFFCHANNEL_BEACON_STOPPED, &sdata->state);
10528c2ecf20Sopenharmony_ci	ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON_ENABLED);
10538c2ecf20Sopenharmony_ci
10548c2ecf20Sopenharmony_ci	/* remove beacon */
10558c2ecf20Sopenharmony_ci	bcn = rcu_dereference_protected(ifmsh->beacon,
10568c2ecf20Sopenharmony_ci					lockdep_is_held(&sdata->wdev.mtx));
10578c2ecf20Sopenharmony_ci	RCU_INIT_POINTER(ifmsh->beacon, NULL);
10588c2ecf20Sopenharmony_ci	kfree_rcu(bcn, rcu_head);
10598c2ecf20Sopenharmony_ci
10608c2ecf20Sopenharmony_ci	/* free all potentially still buffered group-addressed frames */
10618c2ecf20Sopenharmony_ci	local->total_ps_buffered -= skb_queue_len(&ifmsh->ps.bc_buf);
10628c2ecf20Sopenharmony_ci	skb_queue_purge(&ifmsh->ps.bc_buf);
10638c2ecf20Sopenharmony_ci
10648c2ecf20Sopenharmony_ci	del_timer_sync(&sdata->u.mesh.housekeeping_timer);
10658c2ecf20Sopenharmony_ci	del_timer_sync(&sdata->u.mesh.mesh_path_root_timer);
10668c2ecf20Sopenharmony_ci	del_timer_sync(&sdata->u.mesh.mesh_path_timer);
10678c2ecf20Sopenharmony_ci
10688c2ecf20Sopenharmony_ci	/* clear any mesh work (for next join) we may have accrued */
10698c2ecf20Sopenharmony_ci	ifmsh->wrkq_flags = 0;
10708c2ecf20Sopenharmony_ci	ifmsh->mbss_changed = 0;
10718c2ecf20Sopenharmony_ci
10728c2ecf20Sopenharmony_ci	local->fif_other_bss--;
10738c2ecf20Sopenharmony_ci	atomic_dec(&local->iff_allmultis);
10748c2ecf20Sopenharmony_ci	ieee80211_configure_filter(local);
10758c2ecf20Sopenharmony_ci}
10768c2ecf20Sopenharmony_ci
10778c2ecf20Sopenharmony_cistatic void ieee80211_mesh_csa_mark_radar(struct ieee80211_sub_if_data *sdata)
10788c2ecf20Sopenharmony_ci{
10798c2ecf20Sopenharmony_ci	int err;
10808c2ecf20Sopenharmony_ci
10818c2ecf20Sopenharmony_ci	/* if the current channel is a DFS channel, mark the channel as
10828c2ecf20Sopenharmony_ci	 * unavailable.
10838c2ecf20Sopenharmony_ci	 */
10848c2ecf20Sopenharmony_ci	err = cfg80211_chandef_dfs_required(sdata->local->hw.wiphy,
10858c2ecf20Sopenharmony_ci					    &sdata->vif.bss_conf.chandef,
10868c2ecf20Sopenharmony_ci					    NL80211_IFTYPE_MESH_POINT);
10878c2ecf20Sopenharmony_ci	if (err > 0)
10888c2ecf20Sopenharmony_ci		cfg80211_radar_event(sdata->local->hw.wiphy,
10898c2ecf20Sopenharmony_ci				     &sdata->vif.bss_conf.chandef, GFP_ATOMIC);
10908c2ecf20Sopenharmony_ci}
10918c2ecf20Sopenharmony_ci
10928c2ecf20Sopenharmony_cistatic bool
10938c2ecf20Sopenharmony_ciieee80211_mesh_process_chnswitch(struct ieee80211_sub_if_data *sdata,
10948c2ecf20Sopenharmony_ci				 struct ieee802_11_elems *elems, bool beacon)
10958c2ecf20Sopenharmony_ci{
10968c2ecf20Sopenharmony_ci	struct cfg80211_csa_settings params;
10978c2ecf20Sopenharmony_ci	struct ieee80211_csa_ie csa_ie;
10988c2ecf20Sopenharmony_ci	struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
10998c2ecf20Sopenharmony_ci	struct ieee80211_supported_band *sband;
11008c2ecf20Sopenharmony_ci	int err;
11018c2ecf20Sopenharmony_ci	u32 sta_flags, vht_cap_info = 0;
11028c2ecf20Sopenharmony_ci
11038c2ecf20Sopenharmony_ci	sdata_assert_lock(sdata);
11048c2ecf20Sopenharmony_ci
11058c2ecf20Sopenharmony_ci	sband = ieee80211_get_sband(sdata);
11068c2ecf20Sopenharmony_ci	if (!sband)
11078c2ecf20Sopenharmony_ci		return false;
11088c2ecf20Sopenharmony_ci
11098c2ecf20Sopenharmony_ci	sta_flags = 0;
11108c2ecf20Sopenharmony_ci	switch (sdata->vif.bss_conf.chandef.width) {
11118c2ecf20Sopenharmony_ci	case NL80211_CHAN_WIDTH_20_NOHT:
11128c2ecf20Sopenharmony_ci		sta_flags |= IEEE80211_STA_DISABLE_HT;
11138c2ecf20Sopenharmony_ci		fallthrough;
11148c2ecf20Sopenharmony_ci	case NL80211_CHAN_WIDTH_20:
11158c2ecf20Sopenharmony_ci		sta_flags |= IEEE80211_STA_DISABLE_40MHZ;
11168c2ecf20Sopenharmony_ci		fallthrough;
11178c2ecf20Sopenharmony_ci	case NL80211_CHAN_WIDTH_40:
11188c2ecf20Sopenharmony_ci		sta_flags |= IEEE80211_STA_DISABLE_VHT;
11198c2ecf20Sopenharmony_ci		break;
11208c2ecf20Sopenharmony_ci	default:
11218c2ecf20Sopenharmony_ci		break;
11228c2ecf20Sopenharmony_ci	}
11238c2ecf20Sopenharmony_ci
11248c2ecf20Sopenharmony_ci	if (elems->vht_cap_elem)
11258c2ecf20Sopenharmony_ci		vht_cap_info =
11268c2ecf20Sopenharmony_ci			le32_to_cpu(elems->vht_cap_elem->vht_cap_info);
11278c2ecf20Sopenharmony_ci
11288c2ecf20Sopenharmony_ci	memset(&params, 0, sizeof(params));
11298c2ecf20Sopenharmony_ci	err = ieee80211_parse_ch_switch_ie(sdata, elems, sband->band,
11308c2ecf20Sopenharmony_ci					   vht_cap_info,
11318c2ecf20Sopenharmony_ci					   sta_flags, sdata->vif.addr,
11328c2ecf20Sopenharmony_ci					   &csa_ie);
11338c2ecf20Sopenharmony_ci	if (err < 0)
11348c2ecf20Sopenharmony_ci		return false;
11358c2ecf20Sopenharmony_ci	if (err)
11368c2ecf20Sopenharmony_ci		return false;
11378c2ecf20Sopenharmony_ci
11388c2ecf20Sopenharmony_ci	/* Mark the channel unavailable if the reason for the switch is
11398c2ecf20Sopenharmony_ci	 * regulatory.
11408c2ecf20Sopenharmony_ci	 */
11418c2ecf20Sopenharmony_ci	if (csa_ie.reason_code == WLAN_REASON_MESH_CHAN_REGULATORY)
11428c2ecf20Sopenharmony_ci		ieee80211_mesh_csa_mark_radar(sdata);
11438c2ecf20Sopenharmony_ci
11448c2ecf20Sopenharmony_ci	params.chandef = csa_ie.chandef;
11458c2ecf20Sopenharmony_ci	params.count = csa_ie.count;
11468c2ecf20Sopenharmony_ci
11478c2ecf20Sopenharmony_ci	if (!cfg80211_chandef_usable(sdata->local->hw.wiphy, &params.chandef,
11488c2ecf20Sopenharmony_ci				     IEEE80211_CHAN_DISABLED) ||
11498c2ecf20Sopenharmony_ci	    !cfg80211_reg_can_beacon(sdata->local->hw.wiphy, &params.chandef,
11508c2ecf20Sopenharmony_ci				     NL80211_IFTYPE_MESH_POINT)) {
11518c2ecf20Sopenharmony_ci		sdata_info(sdata,
11528c2ecf20Sopenharmony_ci			   "mesh STA %pM switches to unsupported channel (%d MHz, width:%d, CF1/2: %d/%d MHz), aborting\n",
11538c2ecf20Sopenharmony_ci			   sdata->vif.addr,
11548c2ecf20Sopenharmony_ci			   params.chandef.chan->center_freq,
11558c2ecf20Sopenharmony_ci			   params.chandef.width,
11568c2ecf20Sopenharmony_ci			   params.chandef.center_freq1,
11578c2ecf20Sopenharmony_ci			   params.chandef.center_freq2);
11588c2ecf20Sopenharmony_ci		return false;
11598c2ecf20Sopenharmony_ci	}
11608c2ecf20Sopenharmony_ci
11618c2ecf20Sopenharmony_ci	err = cfg80211_chandef_dfs_required(sdata->local->hw.wiphy,
11628c2ecf20Sopenharmony_ci					    &params.chandef,
11638c2ecf20Sopenharmony_ci					    NL80211_IFTYPE_MESH_POINT);
11648c2ecf20Sopenharmony_ci	if (err < 0)
11658c2ecf20Sopenharmony_ci		return false;
11668c2ecf20Sopenharmony_ci	if (err > 0 && !ifmsh->userspace_handles_dfs) {
11678c2ecf20Sopenharmony_ci		sdata_info(sdata,
11688c2ecf20Sopenharmony_ci			   "mesh STA %pM switches to channel requiring DFS (%d MHz, width:%d, CF1/2: %d/%d MHz), aborting\n",
11698c2ecf20Sopenharmony_ci			   sdata->vif.addr,
11708c2ecf20Sopenharmony_ci			   params.chandef.chan->center_freq,
11718c2ecf20Sopenharmony_ci			   params.chandef.width,
11728c2ecf20Sopenharmony_ci			   params.chandef.center_freq1,
11738c2ecf20Sopenharmony_ci			   params.chandef.center_freq2);
11748c2ecf20Sopenharmony_ci		return false;
11758c2ecf20Sopenharmony_ci	}
11768c2ecf20Sopenharmony_ci
11778c2ecf20Sopenharmony_ci	params.radar_required = err;
11788c2ecf20Sopenharmony_ci
11798c2ecf20Sopenharmony_ci	if (cfg80211_chandef_identical(&params.chandef,
11808c2ecf20Sopenharmony_ci				       &sdata->vif.bss_conf.chandef)) {
11818c2ecf20Sopenharmony_ci		mcsa_dbg(sdata,
11828c2ecf20Sopenharmony_ci			 "received csa with an identical chandef, ignoring\n");
11838c2ecf20Sopenharmony_ci		return true;
11848c2ecf20Sopenharmony_ci	}
11858c2ecf20Sopenharmony_ci
11868c2ecf20Sopenharmony_ci	mcsa_dbg(sdata,
11878c2ecf20Sopenharmony_ci		 "received channel switch announcement to go to channel %d MHz\n",
11888c2ecf20Sopenharmony_ci		 params.chandef.chan->center_freq);
11898c2ecf20Sopenharmony_ci
11908c2ecf20Sopenharmony_ci	params.block_tx = csa_ie.mode & WLAN_EID_CHAN_SWITCH_PARAM_TX_RESTRICT;
11918c2ecf20Sopenharmony_ci	if (beacon) {
11928c2ecf20Sopenharmony_ci		ifmsh->chsw_ttl = csa_ie.ttl - 1;
11938c2ecf20Sopenharmony_ci		if (ifmsh->pre_value >= csa_ie.pre_value)
11948c2ecf20Sopenharmony_ci			return false;
11958c2ecf20Sopenharmony_ci		ifmsh->pre_value = csa_ie.pre_value;
11968c2ecf20Sopenharmony_ci	}
11978c2ecf20Sopenharmony_ci
11988c2ecf20Sopenharmony_ci	if (ifmsh->chsw_ttl >= ifmsh->mshcfg.dot11MeshTTL)
11998c2ecf20Sopenharmony_ci		return false;
12008c2ecf20Sopenharmony_ci
12018c2ecf20Sopenharmony_ci	ifmsh->csa_role = IEEE80211_MESH_CSA_ROLE_REPEATER;
12028c2ecf20Sopenharmony_ci
12038c2ecf20Sopenharmony_ci	if (ieee80211_channel_switch(sdata->local->hw.wiphy, sdata->dev,
12048c2ecf20Sopenharmony_ci				     &params) < 0)
12058c2ecf20Sopenharmony_ci		return false;
12068c2ecf20Sopenharmony_ci
12078c2ecf20Sopenharmony_ci	return true;
12088c2ecf20Sopenharmony_ci}
12098c2ecf20Sopenharmony_ci
12108c2ecf20Sopenharmony_cistatic void
12118c2ecf20Sopenharmony_ciieee80211_mesh_rx_probe_req(struct ieee80211_sub_if_data *sdata,
12128c2ecf20Sopenharmony_ci			    struct ieee80211_mgmt *mgmt, size_t len)
12138c2ecf20Sopenharmony_ci{
12148c2ecf20Sopenharmony_ci	struct ieee80211_local *local = sdata->local;
12158c2ecf20Sopenharmony_ci	struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
12168c2ecf20Sopenharmony_ci	struct sk_buff *presp;
12178c2ecf20Sopenharmony_ci	struct beacon_data *bcn;
12188c2ecf20Sopenharmony_ci	struct ieee80211_mgmt *hdr;
12198c2ecf20Sopenharmony_ci	struct ieee802_11_elems elems;
12208c2ecf20Sopenharmony_ci	size_t baselen;
12218c2ecf20Sopenharmony_ci	u8 *pos;
12228c2ecf20Sopenharmony_ci
12238c2ecf20Sopenharmony_ci	pos = mgmt->u.probe_req.variable;
12248c2ecf20Sopenharmony_ci	baselen = (u8 *) pos - (u8 *) mgmt;
12258c2ecf20Sopenharmony_ci	if (baselen > len)
12268c2ecf20Sopenharmony_ci		return;
12278c2ecf20Sopenharmony_ci
12288c2ecf20Sopenharmony_ci	ieee802_11_parse_elems(pos, len - baselen, false, &elems, mgmt->bssid,
12298c2ecf20Sopenharmony_ci			       NULL);
12308c2ecf20Sopenharmony_ci
12318c2ecf20Sopenharmony_ci	if (!elems.mesh_id)
12328c2ecf20Sopenharmony_ci		return;
12338c2ecf20Sopenharmony_ci
12348c2ecf20Sopenharmony_ci	/* 802.11-2012 10.1.4.3.2 */
12358c2ecf20Sopenharmony_ci	if ((!ether_addr_equal(mgmt->da, sdata->vif.addr) &&
12368c2ecf20Sopenharmony_ci	     !is_broadcast_ether_addr(mgmt->da)) ||
12378c2ecf20Sopenharmony_ci	    elems.ssid_len != 0)
12388c2ecf20Sopenharmony_ci		return;
12398c2ecf20Sopenharmony_ci
12408c2ecf20Sopenharmony_ci	if (elems.mesh_id_len != 0 &&
12418c2ecf20Sopenharmony_ci	    (elems.mesh_id_len != ifmsh->mesh_id_len ||
12428c2ecf20Sopenharmony_ci	     memcmp(elems.mesh_id, ifmsh->mesh_id, ifmsh->mesh_id_len)))
12438c2ecf20Sopenharmony_ci		return;
12448c2ecf20Sopenharmony_ci
12458c2ecf20Sopenharmony_ci	rcu_read_lock();
12468c2ecf20Sopenharmony_ci	bcn = rcu_dereference(ifmsh->beacon);
12478c2ecf20Sopenharmony_ci
12488c2ecf20Sopenharmony_ci	if (!bcn)
12498c2ecf20Sopenharmony_ci		goto out;
12508c2ecf20Sopenharmony_ci
12518c2ecf20Sopenharmony_ci	presp = dev_alloc_skb(local->tx_headroom +
12528c2ecf20Sopenharmony_ci			      bcn->head_len + bcn->tail_len);
12538c2ecf20Sopenharmony_ci	if (!presp)
12548c2ecf20Sopenharmony_ci		goto out;
12558c2ecf20Sopenharmony_ci
12568c2ecf20Sopenharmony_ci	skb_reserve(presp, local->tx_headroom);
12578c2ecf20Sopenharmony_ci	skb_put_data(presp, bcn->head, bcn->head_len);
12588c2ecf20Sopenharmony_ci	skb_put_data(presp, bcn->tail, bcn->tail_len);
12598c2ecf20Sopenharmony_ci	hdr = (struct ieee80211_mgmt *) presp->data;
12608c2ecf20Sopenharmony_ci	hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
12618c2ecf20Sopenharmony_ci					 IEEE80211_STYPE_PROBE_RESP);
12628c2ecf20Sopenharmony_ci	memcpy(hdr->da, mgmt->sa, ETH_ALEN);
12638c2ecf20Sopenharmony_ci	IEEE80211_SKB_CB(presp)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
12648c2ecf20Sopenharmony_ci	ieee80211_tx_skb(sdata, presp);
12658c2ecf20Sopenharmony_ciout:
12668c2ecf20Sopenharmony_ci	rcu_read_unlock();
12678c2ecf20Sopenharmony_ci}
12688c2ecf20Sopenharmony_ci
12698c2ecf20Sopenharmony_cistatic void ieee80211_mesh_rx_bcn_presp(struct ieee80211_sub_if_data *sdata,
12708c2ecf20Sopenharmony_ci					u16 stype,
12718c2ecf20Sopenharmony_ci					struct ieee80211_mgmt *mgmt,
12728c2ecf20Sopenharmony_ci					size_t len,
12738c2ecf20Sopenharmony_ci					struct ieee80211_rx_status *rx_status)
12748c2ecf20Sopenharmony_ci{
12758c2ecf20Sopenharmony_ci	struct ieee80211_local *local = sdata->local;
12768c2ecf20Sopenharmony_ci	struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
12778c2ecf20Sopenharmony_ci	struct ieee802_11_elems elems;
12788c2ecf20Sopenharmony_ci	struct ieee80211_channel *channel;
12798c2ecf20Sopenharmony_ci	size_t baselen;
12808c2ecf20Sopenharmony_ci	int freq;
12818c2ecf20Sopenharmony_ci	enum nl80211_band band = rx_status->band;
12828c2ecf20Sopenharmony_ci
12838c2ecf20Sopenharmony_ci	/* ignore ProbeResp to foreign address */
12848c2ecf20Sopenharmony_ci	if (stype == IEEE80211_STYPE_PROBE_RESP &&
12858c2ecf20Sopenharmony_ci	    !ether_addr_equal(mgmt->da, sdata->vif.addr))
12868c2ecf20Sopenharmony_ci		return;
12878c2ecf20Sopenharmony_ci
12888c2ecf20Sopenharmony_ci	baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
12898c2ecf20Sopenharmony_ci	if (baselen > len)
12908c2ecf20Sopenharmony_ci		return;
12918c2ecf20Sopenharmony_ci
12928c2ecf20Sopenharmony_ci	ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
12938c2ecf20Sopenharmony_ci			       false, &elems, mgmt->bssid, NULL);
12948c2ecf20Sopenharmony_ci
12958c2ecf20Sopenharmony_ci	/* ignore non-mesh or secure / unsecure mismatch */
12968c2ecf20Sopenharmony_ci	if ((!elems.mesh_id || !elems.mesh_config) ||
12978c2ecf20Sopenharmony_ci	    (elems.rsn && sdata->u.mesh.security == IEEE80211_MESH_SEC_NONE) ||
12988c2ecf20Sopenharmony_ci	    (!elems.rsn && sdata->u.mesh.security != IEEE80211_MESH_SEC_NONE))
12998c2ecf20Sopenharmony_ci		return;
13008c2ecf20Sopenharmony_ci
13018c2ecf20Sopenharmony_ci	if (elems.ds_params)
13028c2ecf20Sopenharmony_ci		freq = ieee80211_channel_to_frequency(elems.ds_params[0], band);
13038c2ecf20Sopenharmony_ci	else
13048c2ecf20Sopenharmony_ci		freq = rx_status->freq;
13058c2ecf20Sopenharmony_ci
13068c2ecf20Sopenharmony_ci	channel = ieee80211_get_channel(local->hw.wiphy, freq);
13078c2ecf20Sopenharmony_ci
13088c2ecf20Sopenharmony_ci	if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
13098c2ecf20Sopenharmony_ci		return;
13108c2ecf20Sopenharmony_ci
13118c2ecf20Sopenharmony_ci	if (mesh_matches_local(sdata, &elems)) {
13128c2ecf20Sopenharmony_ci		mpl_dbg(sdata, "rssi_threshold=%d,rx_status->signal=%d\n",
13138c2ecf20Sopenharmony_ci			sdata->u.mesh.mshcfg.rssi_threshold, rx_status->signal);
13148c2ecf20Sopenharmony_ci		if (!sdata->u.mesh.user_mpm ||
13158c2ecf20Sopenharmony_ci		    sdata->u.mesh.mshcfg.rssi_threshold == 0 ||
13168c2ecf20Sopenharmony_ci		    sdata->u.mesh.mshcfg.rssi_threshold < rx_status->signal)
13178c2ecf20Sopenharmony_ci			mesh_neighbour_update(sdata, mgmt->sa, &elems,
13188c2ecf20Sopenharmony_ci					      rx_status);
13198c2ecf20Sopenharmony_ci
13208c2ecf20Sopenharmony_ci		if (ifmsh->csa_role != IEEE80211_MESH_CSA_ROLE_INIT &&
13218c2ecf20Sopenharmony_ci		    !sdata->vif.csa_active)
13228c2ecf20Sopenharmony_ci			ieee80211_mesh_process_chnswitch(sdata, &elems, true);
13238c2ecf20Sopenharmony_ci	}
13248c2ecf20Sopenharmony_ci
13258c2ecf20Sopenharmony_ci	if (ifmsh->sync_ops)
13268c2ecf20Sopenharmony_ci		ifmsh->sync_ops->rx_bcn_presp(sdata,
13278c2ecf20Sopenharmony_ci			stype, mgmt, &elems, rx_status);
13288c2ecf20Sopenharmony_ci}
13298c2ecf20Sopenharmony_ci
13308c2ecf20Sopenharmony_ciint ieee80211_mesh_finish_csa(struct ieee80211_sub_if_data *sdata)
13318c2ecf20Sopenharmony_ci{
13328c2ecf20Sopenharmony_ci	struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
13338c2ecf20Sopenharmony_ci	struct mesh_csa_settings *tmp_csa_settings;
13348c2ecf20Sopenharmony_ci	int ret = 0;
13358c2ecf20Sopenharmony_ci	int changed = 0;
13368c2ecf20Sopenharmony_ci
13378c2ecf20Sopenharmony_ci	/* Reset the TTL value and Initiator flag */
13388c2ecf20Sopenharmony_ci	ifmsh->csa_role = IEEE80211_MESH_CSA_ROLE_NONE;
13398c2ecf20Sopenharmony_ci	ifmsh->chsw_ttl = 0;
13408c2ecf20Sopenharmony_ci
13418c2ecf20Sopenharmony_ci	/* Remove the CSA and MCSP elements from the beacon */
13428c2ecf20Sopenharmony_ci	tmp_csa_settings = rcu_dereference_protected(ifmsh->csa,
13438c2ecf20Sopenharmony_ci					    lockdep_is_held(&sdata->wdev.mtx));
13448c2ecf20Sopenharmony_ci	RCU_INIT_POINTER(ifmsh->csa, NULL);
13458c2ecf20Sopenharmony_ci	if (tmp_csa_settings)
13468c2ecf20Sopenharmony_ci		kfree_rcu(tmp_csa_settings, rcu_head);
13478c2ecf20Sopenharmony_ci	ret = ieee80211_mesh_rebuild_beacon(sdata);
13488c2ecf20Sopenharmony_ci	if (ret)
13498c2ecf20Sopenharmony_ci		return -EINVAL;
13508c2ecf20Sopenharmony_ci
13518c2ecf20Sopenharmony_ci	changed |= BSS_CHANGED_BEACON;
13528c2ecf20Sopenharmony_ci
13538c2ecf20Sopenharmony_ci	mcsa_dbg(sdata, "complete switching to center freq %d MHz",
13548c2ecf20Sopenharmony_ci		 sdata->vif.bss_conf.chandef.chan->center_freq);
13558c2ecf20Sopenharmony_ci	return changed;
13568c2ecf20Sopenharmony_ci}
13578c2ecf20Sopenharmony_ci
13588c2ecf20Sopenharmony_ciint ieee80211_mesh_csa_beacon(struct ieee80211_sub_if_data *sdata,
13598c2ecf20Sopenharmony_ci			      struct cfg80211_csa_settings *csa_settings)
13608c2ecf20Sopenharmony_ci{
13618c2ecf20Sopenharmony_ci	struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
13628c2ecf20Sopenharmony_ci	struct mesh_csa_settings *tmp_csa_settings;
13638c2ecf20Sopenharmony_ci	int ret = 0;
13648c2ecf20Sopenharmony_ci
13658c2ecf20Sopenharmony_ci	lockdep_assert_held(&sdata->wdev.mtx);
13668c2ecf20Sopenharmony_ci
13678c2ecf20Sopenharmony_ci	tmp_csa_settings = kmalloc(sizeof(*tmp_csa_settings),
13688c2ecf20Sopenharmony_ci				   GFP_ATOMIC);
13698c2ecf20Sopenharmony_ci	if (!tmp_csa_settings)
13708c2ecf20Sopenharmony_ci		return -ENOMEM;
13718c2ecf20Sopenharmony_ci
13728c2ecf20Sopenharmony_ci	memcpy(&tmp_csa_settings->settings, csa_settings,
13738c2ecf20Sopenharmony_ci	       sizeof(struct cfg80211_csa_settings));
13748c2ecf20Sopenharmony_ci
13758c2ecf20Sopenharmony_ci	rcu_assign_pointer(ifmsh->csa, tmp_csa_settings);
13768c2ecf20Sopenharmony_ci
13778c2ecf20Sopenharmony_ci	ret = ieee80211_mesh_rebuild_beacon(sdata);
13788c2ecf20Sopenharmony_ci	if (ret) {
13798c2ecf20Sopenharmony_ci		tmp_csa_settings = rcu_dereference(ifmsh->csa);
13808c2ecf20Sopenharmony_ci		RCU_INIT_POINTER(ifmsh->csa, NULL);
13818c2ecf20Sopenharmony_ci		kfree_rcu(tmp_csa_settings, rcu_head);
13828c2ecf20Sopenharmony_ci		return ret;
13838c2ecf20Sopenharmony_ci	}
13848c2ecf20Sopenharmony_ci
13858c2ecf20Sopenharmony_ci	return BSS_CHANGED_BEACON;
13868c2ecf20Sopenharmony_ci}
13878c2ecf20Sopenharmony_ci
13888c2ecf20Sopenharmony_cistatic int mesh_fwd_csa_frame(struct ieee80211_sub_if_data *sdata,
13898c2ecf20Sopenharmony_ci			       struct ieee80211_mgmt *mgmt, size_t len,
13908c2ecf20Sopenharmony_ci			       struct ieee802_11_elems *elems)
13918c2ecf20Sopenharmony_ci{
13928c2ecf20Sopenharmony_ci	struct ieee80211_mgmt *mgmt_fwd;
13938c2ecf20Sopenharmony_ci	struct sk_buff *skb;
13948c2ecf20Sopenharmony_ci	struct ieee80211_local *local = sdata->local;
13958c2ecf20Sopenharmony_ci
13968c2ecf20Sopenharmony_ci	skb = dev_alloc_skb(local->tx_headroom + len);
13978c2ecf20Sopenharmony_ci	if (!skb)
13988c2ecf20Sopenharmony_ci		return -ENOMEM;
13998c2ecf20Sopenharmony_ci	skb_reserve(skb, local->tx_headroom);
14008c2ecf20Sopenharmony_ci	mgmt_fwd = skb_put(skb, len);
14018c2ecf20Sopenharmony_ci
14028c2ecf20Sopenharmony_ci	elems->mesh_chansw_params_ie->mesh_ttl--;
14038c2ecf20Sopenharmony_ci	elems->mesh_chansw_params_ie->mesh_flags &=
14048c2ecf20Sopenharmony_ci		~WLAN_EID_CHAN_SWITCH_PARAM_INITIATOR;
14058c2ecf20Sopenharmony_ci
14068c2ecf20Sopenharmony_ci	memcpy(mgmt_fwd, mgmt, len);
14078c2ecf20Sopenharmony_ci	eth_broadcast_addr(mgmt_fwd->da);
14088c2ecf20Sopenharmony_ci	memcpy(mgmt_fwd->sa, sdata->vif.addr, ETH_ALEN);
14098c2ecf20Sopenharmony_ci	memcpy(mgmt_fwd->bssid, sdata->vif.addr, ETH_ALEN);
14108c2ecf20Sopenharmony_ci
14118c2ecf20Sopenharmony_ci	ieee80211_tx_skb(sdata, skb);
14128c2ecf20Sopenharmony_ci	return 0;
14138c2ecf20Sopenharmony_ci}
14148c2ecf20Sopenharmony_ci
14158c2ecf20Sopenharmony_cistatic void mesh_rx_csa_frame(struct ieee80211_sub_if_data *sdata,
14168c2ecf20Sopenharmony_ci			      struct ieee80211_mgmt *mgmt, size_t len)
14178c2ecf20Sopenharmony_ci{
14188c2ecf20Sopenharmony_ci	struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
14198c2ecf20Sopenharmony_ci	struct ieee802_11_elems elems;
14208c2ecf20Sopenharmony_ci	u16 pre_value;
14218c2ecf20Sopenharmony_ci	bool fwd_csa = true;
14228c2ecf20Sopenharmony_ci	size_t baselen;
14238c2ecf20Sopenharmony_ci	u8 *pos;
14248c2ecf20Sopenharmony_ci
14258c2ecf20Sopenharmony_ci	if (mgmt->u.action.u.measurement.action_code !=
14268c2ecf20Sopenharmony_ci	    WLAN_ACTION_SPCT_CHL_SWITCH)
14278c2ecf20Sopenharmony_ci		return;
14288c2ecf20Sopenharmony_ci
14298c2ecf20Sopenharmony_ci	pos = mgmt->u.action.u.chan_switch.variable;
14308c2ecf20Sopenharmony_ci	baselen = offsetof(struct ieee80211_mgmt,
14318c2ecf20Sopenharmony_ci			   u.action.u.chan_switch.variable);
14328c2ecf20Sopenharmony_ci	ieee802_11_parse_elems(pos, len - baselen, true, &elems,
14338c2ecf20Sopenharmony_ci			       mgmt->bssid, NULL);
14348c2ecf20Sopenharmony_ci
14358c2ecf20Sopenharmony_ci	if (!mesh_matches_local(sdata, &elems))
14368c2ecf20Sopenharmony_ci		return;
14378c2ecf20Sopenharmony_ci
14388c2ecf20Sopenharmony_ci	ifmsh->chsw_ttl = elems.mesh_chansw_params_ie->mesh_ttl;
14398c2ecf20Sopenharmony_ci	if (!--ifmsh->chsw_ttl)
14408c2ecf20Sopenharmony_ci		fwd_csa = false;
14418c2ecf20Sopenharmony_ci
14428c2ecf20Sopenharmony_ci	pre_value = le16_to_cpu(elems.mesh_chansw_params_ie->mesh_pre_value);
14438c2ecf20Sopenharmony_ci	if (ifmsh->pre_value >= pre_value)
14448c2ecf20Sopenharmony_ci		return;
14458c2ecf20Sopenharmony_ci
14468c2ecf20Sopenharmony_ci	ifmsh->pre_value = pre_value;
14478c2ecf20Sopenharmony_ci
14488c2ecf20Sopenharmony_ci	if (!sdata->vif.csa_active &&
14498c2ecf20Sopenharmony_ci	    !ieee80211_mesh_process_chnswitch(sdata, &elems, false)) {
14508c2ecf20Sopenharmony_ci		mcsa_dbg(sdata, "Failed to process CSA action frame");
14518c2ecf20Sopenharmony_ci		return;
14528c2ecf20Sopenharmony_ci	}
14538c2ecf20Sopenharmony_ci
14548c2ecf20Sopenharmony_ci	/* forward or re-broadcast the CSA frame */
14558c2ecf20Sopenharmony_ci	if (fwd_csa) {
14568c2ecf20Sopenharmony_ci		if (mesh_fwd_csa_frame(sdata, mgmt, len, &elems) < 0)
14578c2ecf20Sopenharmony_ci			mcsa_dbg(sdata, "Failed to forward the CSA frame");
14588c2ecf20Sopenharmony_ci	}
14598c2ecf20Sopenharmony_ci}
14608c2ecf20Sopenharmony_ci
14618c2ecf20Sopenharmony_cistatic void ieee80211_mesh_rx_mgmt_action(struct ieee80211_sub_if_data *sdata,
14628c2ecf20Sopenharmony_ci					  struct ieee80211_mgmt *mgmt,
14638c2ecf20Sopenharmony_ci					  size_t len,
14648c2ecf20Sopenharmony_ci					  struct ieee80211_rx_status *rx_status)
14658c2ecf20Sopenharmony_ci{
14668c2ecf20Sopenharmony_ci	switch (mgmt->u.action.category) {
14678c2ecf20Sopenharmony_ci	case WLAN_CATEGORY_SELF_PROTECTED:
14688c2ecf20Sopenharmony_ci		switch (mgmt->u.action.u.self_prot.action_code) {
14698c2ecf20Sopenharmony_ci		case WLAN_SP_MESH_PEERING_OPEN:
14708c2ecf20Sopenharmony_ci		case WLAN_SP_MESH_PEERING_CLOSE:
14718c2ecf20Sopenharmony_ci		case WLAN_SP_MESH_PEERING_CONFIRM:
14728c2ecf20Sopenharmony_ci			mesh_rx_plink_frame(sdata, mgmt, len, rx_status);
14738c2ecf20Sopenharmony_ci			break;
14748c2ecf20Sopenharmony_ci		}
14758c2ecf20Sopenharmony_ci		break;
14768c2ecf20Sopenharmony_ci	case WLAN_CATEGORY_MESH_ACTION:
14778c2ecf20Sopenharmony_ci		if (mesh_action_is_path_sel(mgmt))
14788c2ecf20Sopenharmony_ci			mesh_rx_path_sel_frame(sdata, mgmt, len);
14798c2ecf20Sopenharmony_ci		break;
14808c2ecf20Sopenharmony_ci	case WLAN_CATEGORY_SPECTRUM_MGMT:
14818c2ecf20Sopenharmony_ci		mesh_rx_csa_frame(sdata, mgmt, len);
14828c2ecf20Sopenharmony_ci		break;
14838c2ecf20Sopenharmony_ci	}
14848c2ecf20Sopenharmony_ci}
14858c2ecf20Sopenharmony_ci
14868c2ecf20Sopenharmony_civoid ieee80211_mesh_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
14878c2ecf20Sopenharmony_ci				   struct sk_buff *skb)
14888c2ecf20Sopenharmony_ci{
14898c2ecf20Sopenharmony_ci	struct ieee80211_rx_status *rx_status;
14908c2ecf20Sopenharmony_ci	struct ieee80211_mgmt *mgmt;
14918c2ecf20Sopenharmony_ci	u16 stype;
14928c2ecf20Sopenharmony_ci
14938c2ecf20Sopenharmony_ci	sdata_lock(sdata);
14948c2ecf20Sopenharmony_ci
14958c2ecf20Sopenharmony_ci	/* mesh already went down */
14968c2ecf20Sopenharmony_ci	if (!sdata->u.mesh.mesh_id_len)
14978c2ecf20Sopenharmony_ci		goto out;
14988c2ecf20Sopenharmony_ci
14998c2ecf20Sopenharmony_ci	rx_status = IEEE80211_SKB_RXCB(skb);
15008c2ecf20Sopenharmony_ci	mgmt = (struct ieee80211_mgmt *) skb->data;
15018c2ecf20Sopenharmony_ci	stype = le16_to_cpu(mgmt->frame_control) & IEEE80211_FCTL_STYPE;
15028c2ecf20Sopenharmony_ci
15038c2ecf20Sopenharmony_ci	switch (stype) {
15048c2ecf20Sopenharmony_ci	case IEEE80211_STYPE_PROBE_RESP:
15058c2ecf20Sopenharmony_ci	case IEEE80211_STYPE_BEACON:
15068c2ecf20Sopenharmony_ci		ieee80211_mesh_rx_bcn_presp(sdata, stype, mgmt, skb->len,
15078c2ecf20Sopenharmony_ci					    rx_status);
15088c2ecf20Sopenharmony_ci		break;
15098c2ecf20Sopenharmony_ci	case IEEE80211_STYPE_PROBE_REQ:
15108c2ecf20Sopenharmony_ci		ieee80211_mesh_rx_probe_req(sdata, mgmt, skb->len);
15118c2ecf20Sopenharmony_ci		break;
15128c2ecf20Sopenharmony_ci	case IEEE80211_STYPE_ACTION:
15138c2ecf20Sopenharmony_ci		ieee80211_mesh_rx_mgmt_action(sdata, mgmt, skb->len, rx_status);
15148c2ecf20Sopenharmony_ci		break;
15158c2ecf20Sopenharmony_ci	}
15168c2ecf20Sopenharmony_ciout:
15178c2ecf20Sopenharmony_ci	sdata_unlock(sdata);
15188c2ecf20Sopenharmony_ci}
15198c2ecf20Sopenharmony_ci
15208c2ecf20Sopenharmony_cistatic void mesh_bss_info_changed(struct ieee80211_sub_if_data *sdata)
15218c2ecf20Sopenharmony_ci{
15228c2ecf20Sopenharmony_ci	struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
15238c2ecf20Sopenharmony_ci	u32 bit, changed = 0;
15248c2ecf20Sopenharmony_ci
15258c2ecf20Sopenharmony_ci	for_each_set_bit(bit, &ifmsh->mbss_changed,
15268c2ecf20Sopenharmony_ci			 sizeof(changed) * BITS_PER_BYTE) {
15278c2ecf20Sopenharmony_ci		clear_bit(bit, &ifmsh->mbss_changed);
15288c2ecf20Sopenharmony_ci		changed |= BIT(bit);
15298c2ecf20Sopenharmony_ci	}
15308c2ecf20Sopenharmony_ci
15318c2ecf20Sopenharmony_ci	if (sdata->vif.bss_conf.enable_beacon &&
15328c2ecf20Sopenharmony_ci	    (changed & (BSS_CHANGED_BEACON |
15338c2ecf20Sopenharmony_ci			BSS_CHANGED_HT |
15348c2ecf20Sopenharmony_ci			BSS_CHANGED_BASIC_RATES |
15358c2ecf20Sopenharmony_ci			BSS_CHANGED_BEACON_INT)))
15368c2ecf20Sopenharmony_ci		if (ieee80211_mesh_rebuild_beacon(sdata))
15378c2ecf20Sopenharmony_ci			return;
15388c2ecf20Sopenharmony_ci
15398c2ecf20Sopenharmony_ci	ieee80211_bss_info_change_notify(sdata, changed);
15408c2ecf20Sopenharmony_ci}
15418c2ecf20Sopenharmony_ci
15428c2ecf20Sopenharmony_civoid ieee80211_mesh_work(struct ieee80211_sub_if_data *sdata)
15438c2ecf20Sopenharmony_ci{
15448c2ecf20Sopenharmony_ci	struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
15458c2ecf20Sopenharmony_ci
15468c2ecf20Sopenharmony_ci	sdata_lock(sdata);
15478c2ecf20Sopenharmony_ci
15488c2ecf20Sopenharmony_ci	/* mesh already went down */
15498c2ecf20Sopenharmony_ci	if (!sdata->u.mesh.mesh_id_len)
15508c2ecf20Sopenharmony_ci		goto out;
15518c2ecf20Sopenharmony_ci
15528c2ecf20Sopenharmony_ci	if (ifmsh->preq_queue_len &&
15538c2ecf20Sopenharmony_ci	    time_after(jiffies,
15548c2ecf20Sopenharmony_ci		       ifmsh->last_preq + msecs_to_jiffies(ifmsh->mshcfg.dot11MeshHWMPpreqMinInterval)))
15558c2ecf20Sopenharmony_ci		mesh_path_start_discovery(sdata);
15568c2ecf20Sopenharmony_ci
15578c2ecf20Sopenharmony_ci	if (test_and_clear_bit(MESH_WORK_HOUSEKEEPING, &ifmsh->wrkq_flags))
15588c2ecf20Sopenharmony_ci		ieee80211_mesh_housekeeping(sdata);
15598c2ecf20Sopenharmony_ci
15608c2ecf20Sopenharmony_ci	if (test_and_clear_bit(MESH_WORK_ROOT, &ifmsh->wrkq_flags))
15618c2ecf20Sopenharmony_ci		ieee80211_mesh_rootpath(sdata);
15628c2ecf20Sopenharmony_ci
15638c2ecf20Sopenharmony_ci	if (test_and_clear_bit(MESH_WORK_DRIFT_ADJUST, &ifmsh->wrkq_flags))
15648c2ecf20Sopenharmony_ci		mesh_sync_adjust_tsf(sdata);
15658c2ecf20Sopenharmony_ci
15668c2ecf20Sopenharmony_ci	if (test_and_clear_bit(MESH_WORK_MBSS_CHANGED, &ifmsh->wrkq_flags))
15678c2ecf20Sopenharmony_ci		mesh_bss_info_changed(sdata);
15688c2ecf20Sopenharmony_ciout:
15698c2ecf20Sopenharmony_ci	sdata_unlock(sdata);
15708c2ecf20Sopenharmony_ci}
15718c2ecf20Sopenharmony_ci
15728c2ecf20Sopenharmony_ci
15738c2ecf20Sopenharmony_civoid ieee80211_mesh_init_sdata(struct ieee80211_sub_if_data *sdata)
15748c2ecf20Sopenharmony_ci{
15758c2ecf20Sopenharmony_ci	struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
15768c2ecf20Sopenharmony_ci	static u8 zero_addr[ETH_ALEN] = {};
15778c2ecf20Sopenharmony_ci
15788c2ecf20Sopenharmony_ci	timer_setup(&ifmsh->housekeeping_timer,
15798c2ecf20Sopenharmony_ci		    ieee80211_mesh_housekeeping_timer, 0);
15808c2ecf20Sopenharmony_ci
15818c2ecf20Sopenharmony_ci	ifmsh->accepting_plinks = true;
15828c2ecf20Sopenharmony_ci	atomic_set(&ifmsh->mpaths, 0);
15838c2ecf20Sopenharmony_ci	mesh_rmc_init(sdata);
15848c2ecf20Sopenharmony_ci	ifmsh->last_preq = jiffies;
15858c2ecf20Sopenharmony_ci	ifmsh->next_perr = jiffies;
15868c2ecf20Sopenharmony_ci	ifmsh->csa_role = IEEE80211_MESH_CSA_ROLE_NONE;
15878c2ecf20Sopenharmony_ci	/* Allocate all mesh structures when creating the first mesh interface. */
15888c2ecf20Sopenharmony_ci	if (!mesh_allocated)
15898c2ecf20Sopenharmony_ci		ieee80211s_init();
15908c2ecf20Sopenharmony_ci
15918c2ecf20Sopenharmony_ci	mesh_pathtbl_init(sdata);
15928c2ecf20Sopenharmony_ci
15938c2ecf20Sopenharmony_ci	timer_setup(&ifmsh->mesh_path_timer, ieee80211_mesh_path_timer, 0);
15948c2ecf20Sopenharmony_ci	timer_setup(&ifmsh->mesh_path_root_timer,
15958c2ecf20Sopenharmony_ci		    ieee80211_mesh_path_root_timer, 0);
15968c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&ifmsh->preq_queue.list);
15978c2ecf20Sopenharmony_ci	skb_queue_head_init(&ifmsh->ps.bc_buf);
15988c2ecf20Sopenharmony_ci	spin_lock_init(&ifmsh->mesh_preq_queue_lock);
15998c2ecf20Sopenharmony_ci	spin_lock_init(&ifmsh->sync_offset_lock);
16008c2ecf20Sopenharmony_ci	RCU_INIT_POINTER(ifmsh->beacon, NULL);
16018c2ecf20Sopenharmony_ci
16028c2ecf20Sopenharmony_ci	sdata->vif.bss_conf.bssid = zero_addr;
16038c2ecf20Sopenharmony_ci}
16048c2ecf20Sopenharmony_ci
16058c2ecf20Sopenharmony_civoid ieee80211_mesh_teardown_sdata(struct ieee80211_sub_if_data *sdata)
16068c2ecf20Sopenharmony_ci{
16078c2ecf20Sopenharmony_ci	mesh_rmc_free(sdata);
16088c2ecf20Sopenharmony_ci	mesh_pathtbl_unregister(sdata);
16098c2ecf20Sopenharmony_ci}
1610