18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (c) 2008, 2009 open80211s Ltd.
48c2ecf20Sopenharmony_ci * Copyright (C) 2019 Intel Corporation
58c2ecf20Sopenharmony_ci * Author:     Luis Carlos Cobo <luisca@cozybit.com>
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <linux/slab.h>
98c2ecf20Sopenharmony_ci#include <linux/etherdevice.h>
108c2ecf20Sopenharmony_ci#include <asm/unaligned.h>
118c2ecf20Sopenharmony_ci#include "wme.h"
128c2ecf20Sopenharmony_ci#include "mesh.h"
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#define TEST_FRAME_LEN	8192
158c2ecf20Sopenharmony_ci#define MAX_METRIC	0xffffffff
168c2ecf20Sopenharmony_ci#define ARITH_SHIFT	8
178c2ecf20Sopenharmony_ci#define LINK_FAIL_THRESH 95
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci#define MAX_PREQ_QUEUE_LEN	64
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_cistatic void mesh_queue_preq(struct mesh_path *, u8);
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_cistatic inline u32 u32_field_get(const u8 *preq_elem, int offset, bool ae)
248c2ecf20Sopenharmony_ci{
258c2ecf20Sopenharmony_ci	if (ae)
268c2ecf20Sopenharmony_ci		offset += 6;
278c2ecf20Sopenharmony_ci	return get_unaligned_le32(preq_elem + offset);
288c2ecf20Sopenharmony_ci}
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_cistatic inline u16 u16_field_get(const u8 *preq_elem, int offset, bool ae)
318c2ecf20Sopenharmony_ci{
328c2ecf20Sopenharmony_ci	if (ae)
338c2ecf20Sopenharmony_ci		offset += 6;
348c2ecf20Sopenharmony_ci	return get_unaligned_le16(preq_elem + offset);
358c2ecf20Sopenharmony_ci}
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci/* HWMP IE processing macros */
388c2ecf20Sopenharmony_ci#define AE_F			(1<<6)
398c2ecf20Sopenharmony_ci#define AE_F_SET(x)		(*x & AE_F)
408c2ecf20Sopenharmony_ci#define PREQ_IE_FLAGS(x)	(*(x))
418c2ecf20Sopenharmony_ci#define PREQ_IE_HOPCOUNT(x)	(*(x + 1))
428c2ecf20Sopenharmony_ci#define PREQ_IE_TTL(x)		(*(x + 2))
438c2ecf20Sopenharmony_ci#define PREQ_IE_PREQ_ID(x)	u32_field_get(x, 3, 0)
448c2ecf20Sopenharmony_ci#define PREQ_IE_ORIG_ADDR(x)	(x + 7)
458c2ecf20Sopenharmony_ci#define PREQ_IE_ORIG_SN(x)	u32_field_get(x, 13, 0)
468c2ecf20Sopenharmony_ci#define PREQ_IE_LIFETIME(x)	u32_field_get(x, 17, AE_F_SET(x))
478c2ecf20Sopenharmony_ci#define PREQ_IE_METRIC(x) 	u32_field_get(x, 21, AE_F_SET(x))
488c2ecf20Sopenharmony_ci#define PREQ_IE_TARGET_F(x)	(*(AE_F_SET(x) ? x + 32 : x + 26))
498c2ecf20Sopenharmony_ci#define PREQ_IE_TARGET_ADDR(x) 	(AE_F_SET(x) ? x + 33 : x + 27)
508c2ecf20Sopenharmony_ci#define PREQ_IE_TARGET_SN(x) 	u32_field_get(x, 33, AE_F_SET(x))
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci#define PREP_IE_FLAGS(x)	PREQ_IE_FLAGS(x)
548c2ecf20Sopenharmony_ci#define PREP_IE_HOPCOUNT(x)	PREQ_IE_HOPCOUNT(x)
558c2ecf20Sopenharmony_ci#define PREP_IE_TTL(x)		PREQ_IE_TTL(x)
568c2ecf20Sopenharmony_ci#define PREP_IE_ORIG_ADDR(x)	(AE_F_SET(x) ? x + 27 : x + 21)
578c2ecf20Sopenharmony_ci#define PREP_IE_ORIG_SN(x)	u32_field_get(x, 27, AE_F_SET(x))
588c2ecf20Sopenharmony_ci#define PREP_IE_LIFETIME(x)	u32_field_get(x, 13, AE_F_SET(x))
598c2ecf20Sopenharmony_ci#define PREP_IE_METRIC(x)	u32_field_get(x, 17, AE_F_SET(x))
608c2ecf20Sopenharmony_ci#define PREP_IE_TARGET_ADDR(x)	(x + 3)
618c2ecf20Sopenharmony_ci#define PREP_IE_TARGET_SN(x)	u32_field_get(x, 9, 0)
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci#define PERR_IE_TTL(x)		(*(x))
648c2ecf20Sopenharmony_ci#define PERR_IE_TARGET_FLAGS(x)	(*(x + 2))
658c2ecf20Sopenharmony_ci#define PERR_IE_TARGET_ADDR(x)	(x + 3)
668c2ecf20Sopenharmony_ci#define PERR_IE_TARGET_SN(x)	u32_field_get(x, 9, 0)
678c2ecf20Sopenharmony_ci#define PERR_IE_TARGET_RCODE(x)	u16_field_get(x, 13, 0)
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci#define MSEC_TO_TU(x) (x*1000/1024)
708c2ecf20Sopenharmony_ci#define SN_GT(x, y) ((s32)(y - x) < 0)
718c2ecf20Sopenharmony_ci#define SN_LT(x, y) ((s32)(x - y) < 0)
728c2ecf20Sopenharmony_ci#define MAX_SANE_SN_DELTA 32
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_cistatic inline u32 SN_DELTA(u32 x, u32 y)
758c2ecf20Sopenharmony_ci{
768c2ecf20Sopenharmony_ci	return x >= y ? x - y : y - x;
778c2ecf20Sopenharmony_ci}
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci#define net_traversal_jiffies(s) \
808c2ecf20Sopenharmony_ci	msecs_to_jiffies(s->u.mesh.mshcfg.dot11MeshHWMPnetDiameterTraversalTime)
818c2ecf20Sopenharmony_ci#define default_lifetime(s) \
828c2ecf20Sopenharmony_ci	MSEC_TO_TU(s->u.mesh.mshcfg.dot11MeshHWMPactivePathTimeout)
838c2ecf20Sopenharmony_ci#define min_preq_int_jiff(s) \
848c2ecf20Sopenharmony_ci	(msecs_to_jiffies(s->u.mesh.mshcfg.dot11MeshHWMPpreqMinInterval))
858c2ecf20Sopenharmony_ci#define max_preq_retries(s) (s->u.mesh.mshcfg.dot11MeshHWMPmaxPREQretries)
868c2ecf20Sopenharmony_ci#define disc_timeout_jiff(s) \
878c2ecf20Sopenharmony_ci	msecs_to_jiffies(sdata->u.mesh.mshcfg.min_discovery_timeout)
888c2ecf20Sopenharmony_ci#define root_path_confirmation_jiffies(s) \
898c2ecf20Sopenharmony_ci	msecs_to_jiffies(sdata->u.mesh.mshcfg.dot11MeshHWMPconfirmationInterval)
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_cienum mpath_frame_type {
928c2ecf20Sopenharmony_ci	MPATH_PREQ = 0,
938c2ecf20Sopenharmony_ci	MPATH_PREP,
948c2ecf20Sopenharmony_ci	MPATH_PERR,
958c2ecf20Sopenharmony_ci	MPATH_RANN
968c2ecf20Sopenharmony_ci};
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_cistatic const u8 broadcast_addr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_cistatic int mesh_path_sel_frame_tx(enum mpath_frame_type action, u8 flags,
1018c2ecf20Sopenharmony_ci				  const u8 *orig_addr, u32 orig_sn,
1028c2ecf20Sopenharmony_ci				  u8 target_flags, const u8 *target,
1038c2ecf20Sopenharmony_ci				  u32 target_sn, const u8 *da,
1048c2ecf20Sopenharmony_ci				  u8 hop_count, u8 ttl,
1058c2ecf20Sopenharmony_ci				  u32 lifetime, u32 metric, u32 preq_id,
1068c2ecf20Sopenharmony_ci				  struct ieee80211_sub_if_data *sdata)
1078c2ecf20Sopenharmony_ci{
1088c2ecf20Sopenharmony_ci	struct ieee80211_local *local = sdata->local;
1098c2ecf20Sopenharmony_ci	struct sk_buff *skb;
1108c2ecf20Sopenharmony_ci	struct ieee80211_mgmt *mgmt;
1118c2ecf20Sopenharmony_ci	u8 *pos, ie_len;
1128c2ecf20Sopenharmony_ci	int hdr_len = offsetofend(struct ieee80211_mgmt,
1138c2ecf20Sopenharmony_ci				  u.action.u.mesh_action);
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci	skb = dev_alloc_skb(local->tx_headroom +
1168c2ecf20Sopenharmony_ci			    hdr_len +
1178c2ecf20Sopenharmony_ci			    2 + 37); /* max HWMP IE */
1188c2ecf20Sopenharmony_ci	if (!skb)
1198c2ecf20Sopenharmony_ci		return -1;
1208c2ecf20Sopenharmony_ci	skb_reserve(skb, local->tx_headroom);
1218c2ecf20Sopenharmony_ci	mgmt = skb_put_zero(skb, hdr_len);
1228c2ecf20Sopenharmony_ci	mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1238c2ecf20Sopenharmony_ci					  IEEE80211_STYPE_ACTION);
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	memcpy(mgmt->da, da, ETH_ALEN);
1268c2ecf20Sopenharmony_ci	memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
1278c2ecf20Sopenharmony_ci	/* BSSID == SA */
1288c2ecf20Sopenharmony_ci	memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
1298c2ecf20Sopenharmony_ci	mgmt->u.action.category = WLAN_CATEGORY_MESH_ACTION;
1308c2ecf20Sopenharmony_ci	mgmt->u.action.u.mesh_action.action_code =
1318c2ecf20Sopenharmony_ci					WLAN_MESH_ACTION_HWMP_PATH_SELECTION;
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci	switch (action) {
1348c2ecf20Sopenharmony_ci	case MPATH_PREQ:
1358c2ecf20Sopenharmony_ci		mhwmp_dbg(sdata, "sending PREQ to %pM\n", target);
1368c2ecf20Sopenharmony_ci		ie_len = 37;
1378c2ecf20Sopenharmony_ci		pos = skb_put(skb, 2 + ie_len);
1388c2ecf20Sopenharmony_ci		*pos++ = WLAN_EID_PREQ;
1398c2ecf20Sopenharmony_ci		break;
1408c2ecf20Sopenharmony_ci	case MPATH_PREP:
1418c2ecf20Sopenharmony_ci		mhwmp_dbg(sdata, "sending PREP to %pM\n", orig_addr);
1428c2ecf20Sopenharmony_ci		ie_len = 31;
1438c2ecf20Sopenharmony_ci		pos = skb_put(skb, 2 + ie_len);
1448c2ecf20Sopenharmony_ci		*pos++ = WLAN_EID_PREP;
1458c2ecf20Sopenharmony_ci		break;
1468c2ecf20Sopenharmony_ci	case MPATH_RANN:
1478c2ecf20Sopenharmony_ci		mhwmp_dbg(sdata, "sending RANN from %pM\n", orig_addr);
1488c2ecf20Sopenharmony_ci		ie_len = sizeof(struct ieee80211_rann_ie);
1498c2ecf20Sopenharmony_ci		pos = skb_put(skb, 2 + ie_len);
1508c2ecf20Sopenharmony_ci		*pos++ = WLAN_EID_RANN;
1518c2ecf20Sopenharmony_ci		break;
1528c2ecf20Sopenharmony_ci	default:
1538c2ecf20Sopenharmony_ci		kfree_skb(skb);
1548c2ecf20Sopenharmony_ci		return -ENOTSUPP;
1558c2ecf20Sopenharmony_ci	}
1568c2ecf20Sopenharmony_ci	*pos++ = ie_len;
1578c2ecf20Sopenharmony_ci	*pos++ = flags;
1588c2ecf20Sopenharmony_ci	*pos++ = hop_count;
1598c2ecf20Sopenharmony_ci	*pos++ = ttl;
1608c2ecf20Sopenharmony_ci	if (action == MPATH_PREP) {
1618c2ecf20Sopenharmony_ci		memcpy(pos, target, ETH_ALEN);
1628c2ecf20Sopenharmony_ci		pos += ETH_ALEN;
1638c2ecf20Sopenharmony_ci		put_unaligned_le32(target_sn, pos);
1648c2ecf20Sopenharmony_ci		pos += 4;
1658c2ecf20Sopenharmony_ci	} else {
1668c2ecf20Sopenharmony_ci		if (action == MPATH_PREQ) {
1678c2ecf20Sopenharmony_ci			put_unaligned_le32(preq_id, pos);
1688c2ecf20Sopenharmony_ci			pos += 4;
1698c2ecf20Sopenharmony_ci		}
1708c2ecf20Sopenharmony_ci		memcpy(pos, orig_addr, ETH_ALEN);
1718c2ecf20Sopenharmony_ci		pos += ETH_ALEN;
1728c2ecf20Sopenharmony_ci		put_unaligned_le32(orig_sn, pos);
1738c2ecf20Sopenharmony_ci		pos += 4;
1748c2ecf20Sopenharmony_ci	}
1758c2ecf20Sopenharmony_ci	put_unaligned_le32(lifetime, pos); /* interval for RANN */
1768c2ecf20Sopenharmony_ci	pos += 4;
1778c2ecf20Sopenharmony_ci	put_unaligned_le32(metric, pos);
1788c2ecf20Sopenharmony_ci	pos += 4;
1798c2ecf20Sopenharmony_ci	if (action == MPATH_PREQ) {
1808c2ecf20Sopenharmony_ci		*pos++ = 1; /* destination count */
1818c2ecf20Sopenharmony_ci		*pos++ = target_flags;
1828c2ecf20Sopenharmony_ci		memcpy(pos, target, ETH_ALEN);
1838c2ecf20Sopenharmony_ci		pos += ETH_ALEN;
1848c2ecf20Sopenharmony_ci		put_unaligned_le32(target_sn, pos);
1858c2ecf20Sopenharmony_ci		pos += 4;
1868c2ecf20Sopenharmony_ci	} else if (action == MPATH_PREP) {
1878c2ecf20Sopenharmony_ci		memcpy(pos, orig_addr, ETH_ALEN);
1888c2ecf20Sopenharmony_ci		pos += ETH_ALEN;
1898c2ecf20Sopenharmony_ci		put_unaligned_le32(orig_sn, pos);
1908c2ecf20Sopenharmony_ci		pos += 4;
1918c2ecf20Sopenharmony_ci	}
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci	ieee80211_tx_skb(sdata, skb);
1948c2ecf20Sopenharmony_ci	return 0;
1958c2ecf20Sopenharmony_ci}
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci/*  Headroom is not adjusted.  Caller should ensure that skb has sufficient
1998c2ecf20Sopenharmony_ci *  headroom in case the frame is encrypted. */
2008c2ecf20Sopenharmony_cistatic void prepare_frame_for_deferred_tx(struct ieee80211_sub_if_data *sdata,
2018c2ecf20Sopenharmony_ci		struct sk_buff *skb)
2028c2ecf20Sopenharmony_ci{
2038c2ecf20Sopenharmony_ci	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2048c2ecf20Sopenharmony_ci	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci	skb_reset_mac_header(skb);
2078c2ecf20Sopenharmony_ci	skb_reset_network_header(skb);
2088c2ecf20Sopenharmony_ci	skb_reset_transport_header(skb);
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci	/* Send all internal mgmt frames on VO. Accordingly set TID to 7. */
2118c2ecf20Sopenharmony_ci	skb_set_queue_mapping(skb, IEEE80211_AC_VO);
2128c2ecf20Sopenharmony_ci	skb->priority = 7;
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci	info->control.vif = &sdata->vif;
2158c2ecf20Sopenharmony_ci	info->control.flags |= IEEE80211_TX_INTCFL_NEED_TXPROCESSING;
2168c2ecf20Sopenharmony_ci	ieee80211_set_qos_hdr(sdata, skb);
2178c2ecf20Sopenharmony_ci	ieee80211_mps_set_frame_flags(sdata, NULL, hdr);
2188c2ecf20Sopenharmony_ci}
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci/**
2218c2ecf20Sopenharmony_ci * mesh_path_error_tx - Sends a PERR mesh management frame
2228c2ecf20Sopenharmony_ci *
2238c2ecf20Sopenharmony_ci * @ttl: allowed remaining hops
2248c2ecf20Sopenharmony_ci * @target: broken destination
2258c2ecf20Sopenharmony_ci * @target_sn: SN of the broken destination
2268c2ecf20Sopenharmony_ci * @target_rcode: reason code for this PERR
2278c2ecf20Sopenharmony_ci * @ra: node this frame is addressed to
2288c2ecf20Sopenharmony_ci * @sdata: local mesh subif
2298c2ecf20Sopenharmony_ci *
2308c2ecf20Sopenharmony_ci * Note: This function may be called with driver locks taken that the driver
2318c2ecf20Sopenharmony_ci * also acquires in the TX path.  To avoid a deadlock we don't transmit the
2328c2ecf20Sopenharmony_ci * frame directly but add it to the pending queue instead.
2338c2ecf20Sopenharmony_ci */
2348c2ecf20Sopenharmony_ciint mesh_path_error_tx(struct ieee80211_sub_if_data *sdata,
2358c2ecf20Sopenharmony_ci		       u8 ttl, const u8 *target, u32 target_sn,
2368c2ecf20Sopenharmony_ci		       u16 target_rcode, const u8 *ra)
2378c2ecf20Sopenharmony_ci{
2388c2ecf20Sopenharmony_ci	struct ieee80211_local *local = sdata->local;
2398c2ecf20Sopenharmony_ci	struct sk_buff *skb;
2408c2ecf20Sopenharmony_ci	struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
2418c2ecf20Sopenharmony_ci	struct ieee80211_mgmt *mgmt;
2428c2ecf20Sopenharmony_ci	u8 *pos, ie_len;
2438c2ecf20Sopenharmony_ci	int hdr_len = offsetofend(struct ieee80211_mgmt,
2448c2ecf20Sopenharmony_ci				  u.action.u.mesh_action);
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci	if (time_before(jiffies, ifmsh->next_perr))
2478c2ecf20Sopenharmony_ci		return -EAGAIN;
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_ci	skb = dev_alloc_skb(local->tx_headroom +
2508c2ecf20Sopenharmony_ci			    sdata->encrypt_headroom +
2518c2ecf20Sopenharmony_ci			    IEEE80211_ENCRYPT_TAILROOM +
2528c2ecf20Sopenharmony_ci			    hdr_len +
2538c2ecf20Sopenharmony_ci			    2 + 15 /* PERR IE */);
2548c2ecf20Sopenharmony_ci	if (!skb)
2558c2ecf20Sopenharmony_ci		return -1;
2568c2ecf20Sopenharmony_ci	skb_reserve(skb, local->tx_headroom + sdata->encrypt_headroom);
2578c2ecf20Sopenharmony_ci	mgmt = skb_put_zero(skb, hdr_len);
2588c2ecf20Sopenharmony_ci	mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
2598c2ecf20Sopenharmony_ci					  IEEE80211_STYPE_ACTION);
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_ci	memcpy(mgmt->da, ra, ETH_ALEN);
2628c2ecf20Sopenharmony_ci	memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
2638c2ecf20Sopenharmony_ci	/* BSSID == SA */
2648c2ecf20Sopenharmony_ci	memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
2658c2ecf20Sopenharmony_ci	mgmt->u.action.category = WLAN_CATEGORY_MESH_ACTION;
2668c2ecf20Sopenharmony_ci	mgmt->u.action.u.mesh_action.action_code =
2678c2ecf20Sopenharmony_ci					WLAN_MESH_ACTION_HWMP_PATH_SELECTION;
2688c2ecf20Sopenharmony_ci	ie_len = 15;
2698c2ecf20Sopenharmony_ci	pos = skb_put(skb, 2 + ie_len);
2708c2ecf20Sopenharmony_ci	*pos++ = WLAN_EID_PERR;
2718c2ecf20Sopenharmony_ci	*pos++ = ie_len;
2728c2ecf20Sopenharmony_ci	/* ttl */
2738c2ecf20Sopenharmony_ci	*pos++ = ttl;
2748c2ecf20Sopenharmony_ci	/* number of destinations */
2758c2ecf20Sopenharmony_ci	*pos++ = 1;
2768c2ecf20Sopenharmony_ci	/* Flags field has AE bit only as defined in
2778c2ecf20Sopenharmony_ci	 * sec 8.4.2.117 IEEE802.11-2012
2788c2ecf20Sopenharmony_ci	 */
2798c2ecf20Sopenharmony_ci	*pos = 0;
2808c2ecf20Sopenharmony_ci	pos++;
2818c2ecf20Sopenharmony_ci	memcpy(pos, target, ETH_ALEN);
2828c2ecf20Sopenharmony_ci	pos += ETH_ALEN;
2838c2ecf20Sopenharmony_ci	put_unaligned_le32(target_sn, pos);
2848c2ecf20Sopenharmony_ci	pos += 4;
2858c2ecf20Sopenharmony_ci	put_unaligned_le16(target_rcode, pos);
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_ci	/* see note in function header */
2888c2ecf20Sopenharmony_ci	prepare_frame_for_deferred_tx(sdata, skb);
2898c2ecf20Sopenharmony_ci	ifmsh->next_perr = TU_TO_EXP_TIME(
2908c2ecf20Sopenharmony_ci				   ifmsh->mshcfg.dot11MeshHWMPperrMinInterval);
2918c2ecf20Sopenharmony_ci	ieee80211_add_pending_skb(local, skb);
2928c2ecf20Sopenharmony_ci	return 0;
2938c2ecf20Sopenharmony_ci}
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_civoid ieee80211s_update_metric(struct ieee80211_local *local,
2968c2ecf20Sopenharmony_ci			      struct sta_info *sta,
2978c2ecf20Sopenharmony_ci			      struct ieee80211_tx_status *st)
2988c2ecf20Sopenharmony_ci{
2998c2ecf20Sopenharmony_ci	struct ieee80211_tx_info *txinfo = st->info;
3008c2ecf20Sopenharmony_ci	int failed;
3018c2ecf20Sopenharmony_ci	struct rate_info rinfo;
3028c2ecf20Sopenharmony_ci
3038c2ecf20Sopenharmony_ci	failed = !(txinfo->flags & IEEE80211_TX_STAT_ACK);
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_ci	/* moving average, scaled to 100.
3068c2ecf20Sopenharmony_ci	 * feed failure as 100 and success as 0
3078c2ecf20Sopenharmony_ci	 */
3088c2ecf20Sopenharmony_ci	ewma_mesh_fail_avg_add(&sta->mesh->fail_avg, failed * 100);
3098c2ecf20Sopenharmony_ci	if (ewma_mesh_fail_avg_read(&sta->mesh->fail_avg) >
3108c2ecf20Sopenharmony_ci			LINK_FAIL_THRESH)
3118c2ecf20Sopenharmony_ci		mesh_plink_broken(sta);
3128c2ecf20Sopenharmony_ci
3138c2ecf20Sopenharmony_ci	sta_set_rate_info_tx(sta, &sta->tx_stats.last_rate, &rinfo);
3148c2ecf20Sopenharmony_ci	ewma_mesh_tx_rate_avg_add(&sta->mesh->tx_rate_avg,
3158c2ecf20Sopenharmony_ci				  cfg80211_calculate_bitrate(&rinfo));
3168c2ecf20Sopenharmony_ci}
3178c2ecf20Sopenharmony_ci
3188c2ecf20Sopenharmony_ciu32 airtime_link_metric_get(struct ieee80211_local *local,
3198c2ecf20Sopenharmony_ci			    struct sta_info *sta)
3208c2ecf20Sopenharmony_ci{
3218c2ecf20Sopenharmony_ci	/* This should be adjusted for each device */
3228c2ecf20Sopenharmony_ci	int device_constant = 1 << ARITH_SHIFT;
3238c2ecf20Sopenharmony_ci	int test_frame_len = TEST_FRAME_LEN << ARITH_SHIFT;
3248c2ecf20Sopenharmony_ci	int s_unit = 1 << ARITH_SHIFT;
3258c2ecf20Sopenharmony_ci	int rate, err;
3268c2ecf20Sopenharmony_ci	u32 tx_time, estimated_retx;
3278c2ecf20Sopenharmony_ci	u64 result;
3288c2ecf20Sopenharmony_ci	unsigned long fail_avg =
3298c2ecf20Sopenharmony_ci		ewma_mesh_fail_avg_read(&sta->mesh->fail_avg);
3308c2ecf20Sopenharmony_ci
3318c2ecf20Sopenharmony_ci	if (sta->mesh->plink_state != NL80211_PLINK_ESTAB)
3328c2ecf20Sopenharmony_ci		return MAX_METRIC;
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_ci	/* Try to get rate based on HW/SW RC algorithm.
3358c2ecf20Sopenharmony_ci	 * Rate is returned in units of Kbps, correct this
3368c2ecf20Sopenharmony_ci	 * to comply with airtime calculation units
3378c2ecf20Sopenharmony_ci	 * Round up in case we get rate < 100Kbps
3388c2ecf20Sopenharmony_ci	 */
3398c2ecf20Sopenharmony_ci	rate = DIV_ROUND_UP(sta_get_expected_throughput(sta), 100);
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_ci	if (rate) {
3428c2ecf20Sopenharmony_ci		err = 0;
3438c2ecf20Sopenharmony_ci	} else {
3448c2ecf20Sopenharmony_ci		if (fail_avg > LINK_FAIL_THRESH)
3458c2ecf20Sopenharmony_ci			return MAX_METRIC;
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_ci		rate = ewma_mesh_tx_rate_avg_read(&sta->mesh->tx_rate_avg);
3488c2ecf20Sopenharmony_ci		if (WARN_ON(!rate))
3498c2ecf20Sopenharmony_ci			return MAX_METRIC;
3508c2ecf20Sopenharmony_ci
3518c2ecf20Sopenharmony_ci		err = (fail_avg << ARITH_SHIFT) / 100;
3528c2ecf20Sopenharmony_ci	}
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_ci	/* bitrate is in units of 100 Kbps, while we need rate in units of
3558c2ecf20Sopenharmony_ci	 * 1Mbps. This will be corrected on tx_time computation.
3568c2ecf20Sopenharmony_ci	 */
3578c2ecf20Sopenharmony_ci	tx_time = (device_constant + 10 * test_frame_len / rate);
3588c2ecf20Sopenharmony_ci	estimated_retx = ((1 << (2 * ARITH_SHIFT)) / (s_unit - err));
3598c2ecf20Sopenharmony_ci	result = ((u64)tx_time * estimated_retx) >> (2 * ARITH_SHIFT);
3608c2ecf20Sopenharmony_ci	return (u32)result;
3618c2ecf20Sopenharmony_ci}
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_ci/**
3648c2ecf20Sopenharmony_ci * hwmp_route_info_get - Update routing info to originator and transmitter
3658c2ecf20Sopenharmony_ci *
3668c2ecf20Sopenharmony_ci * @sdata: local mesh subif
3678c2ecf20Sopenharmony_ci * @mgmt: mesh management frame
3688c2ecf20Sopenharmony_ci * @hwmp_ie: hwmp information element (PREP or PREQ)
3698c2ecf20Sopenharmony_ci * @action: type of hwmp ie
3708c2ecf20Sopenharmony_ci *
3718c2ecf20Sopenharmony_ci * This function updates the path routing information to the originator and the
3728c2ecf20Sopenharmony_ci * transmitter of a HWMP PREQ or PREP frame.
3738c2ecf20Sopenharmony_ci *
3748c2ecf20Sopenharmony_ci * Returns: metric to frame originator or 0 if the frame should not be further
3758c2ecf20Sopenharmony_ci * processed
3768c2ecf20Sopenharmony_ci *
3778c2ecf20Sopenharmony_ci * Notes: this function is the only place (besides user-provided info) where
3788c2ecf20Sopenharmony_ci * path routing information is updated.
3798c2ecf20Sopenharmony_ci */
3808c2ecf20Sopenharmony_cistatic u32 hwmp_route_info_get(struct ieee80211_sub_if_data *sdata,
3818c2ecf20Sopenharmony_ci			       struct ieee80211_mgmt *mgmt,
3828c2ecf20Sopenharmony_ci			       const u8 *hwmp_ie, enum mpath_frame_type action)
3838c2ecf20Sopenharmony_ci{
3848c2ecf20Sopenharmony_ci	struct ieee80211_local *local = sdata->local;
3858c2ecf20Sopenharmony_ci	struct mesh_path *mpath;
3868c2ecf20Sopenharmony_ci	struct sta_info *sta;
3878c2ecf20Sopenharmony_ci	bool fresh_info;
3888c2ecf20Sopenharmony_ci	const u8 *orig_addr, *ta;
3898c2ecf20Sopenharmony_ci	u32 orig_sn, orig_metric;
3908c2ecf20Sopenharmony_ci	unsigned long orig_lifetime, exp_time;
3918c2ecf20Sopenharmony_ci	u32 last_hop_metric, new_metric;
3928c2ecf20Sopenharmony_ci	bool process = true;
3938c2ecf20Sopenharmony_ci	u8 hopcount;
3948c2ecf20Sopenharmony_ci
3958c2ecf20Sopenharmony_ci	rcu_read_lock();
3968c2ecf20Sopenharmony_ci	sta = sta_info_get(sdata, mgmt->sa);
3978c2ecf20Sopenharmony_ci	if (!sta) {
3988c2ecf20Sopenharmony_ci		rcu_read_unlock();
3998c2ecf20Sopenharmony_ci		return 0;
4008c2ecf20Sopenharmony_ci	}
4018c2ecf20Sopenharmony_ci
4028c2ecf20Sopenharmony_ci	last_hop_metric = airtime_link_metric_get(local, sta);
4038c2ecf20Sopenharmony_ci	/* Update and check originator routing info */
4048c2ecf20Sopenharmony_ci	fresh_info = true;
4058c2ecf20Sopenharmony_ci
4068c2ecf20Sopenharmony_ci	switch (action) {
4078c2ecf20Sopenharmony_ci	case MPATH_PREQ:
4088c2ecf20Sopenharmony_ci		orig_addr = PREQ_IE_ORIG_ADDR(hwmp_ie);
4098c2ecf20Sopenharmony_ci		orig_sn = PREQ_IE_ORIG_SN(hwmp_ie);
4108c2ecf20Sopenharmony_ci		orig_lifetime = PREQ_IE_LIFETIME(hwmp_ie);
4118c2ecf20Sopenharmony_ci		orig_metric = PREQ_IE_METRIC(hwmp_ie);
4128c2ecf20Sopenharmony_ci		hopcount = PREQ_IE_HOPCOUNT(hwmp_ie) + 1;
4138c2ecf20Sopenharmony_ci		break;
4148c2ecf20Sopenharmony_ci	case MPATH_PREP:
4158c2ecf20Sopenharmony_ci		/* Originator here refers to the MP that was the target in the
4168c2ecf20Sopenharmony_ci		 * Path Request. We divert from the nomenclature in the draft
4178c2ecf20Sopenharmony_ci		 * so that we can easily use a single function to gather path
4188c2ecf20Sopenharmony_ci		 * information from both PREQ and PREP frames.
4198c2ecf20Sopenharmony_ci		 */
4208c2ecf20Sopenharmony_ci		orig_addr = PREP_IE_TARGET_ADDR(hwmp_ie);
4218c2ecf20Sopenharmony_ci		orig_sn = PREP_IE_TARGET_SN(hwmp_ie);
4228c2ecf20Sopenharmony_ci		orig_lifetime = PREP_IE_LIFETIME(hwmp_ie);
4238c2ecf20Sopenharmony_ci		orig_metric = PREP_IE_METRIC(hwmp_ie);
4248c2ecf20Sopenharmony_ci		hopcount = PREP_IE_HOPCOUNT(hwmp_ie) + 1;
4258c2ecf20Sopenharmony_ci		break;
4268c2ecf20Sopenharmony_ci	default:
4278c2ecf20Sopenharmony_ci		rcu_read_unlock();
4288c2ecf20Sopenharmony_ci		return 0;
4298c2ecf20Sopenharmony_ci	}
4308c2ecf20Sopenharmony_ci	new_metric = orig_metric + last_hop_metric;
4318c2ecf20Sopenharmony_ci	if (new_metric < orig_metric)
4328c2ecf20Sopenharmony_ci		new_metric = MAX_METRIC;
4338c2ecf20Sopenharmony_ci	exp_time = TU_TO_EXP_TIME(orig_lifetime);
4348c2ecf20Sopenharmony_ci
4358c2ecf20Sopenharmony_ci	if (ether_addr_equal(orig_addr, sdata->vif.addr)) {
4368c2ecf20Sopenharmony_ci		/* This MP is the originator, we are not interested in this
4378c2ecf20Sopenharmony_ci		 * frame, except for updating transmitter's path info.
4388c2ecf20Sopenharmony_ci		 */
4398c2ecf20Sopenharmony_ci		process = false;
4408c2ecf20Sopenharmony_ci		fresh_info = false;
4418c2ecf20Sopenharmony_ci	} else {
4428c2ecf20Sopenharmony_ci		mpath = mesh_path_lookup(sdata, orig_addr);
4438c2ecf20Sopenharmony_ci		if (mpath) {
4448c2ecf20Sopenharmony_ci			spin_lock_bh(&mpath->state_lock);
4458c2ecf20Sopenharmony_ci			if (mpath->flags & MESH_PATH_FIXED)
4468c2ecf20Sopenharmony_ci				fresh_info = false;
4478c2ecf20Sopenharmony_ci			else if ((mpath->flags & MESH_PATH_ACTIVE) &&
4488c2ecf20Sopenharmony_ci			    (mpath->flags & MESH_PATH_SN_VALID)) {
4498c2ecf20Sopenharmony_ci				if (SN_GT(mpath->sn, orig_sn) ||
4508c2ecf20Sopenharmony_ci				    (mpath->sn == orig_sn &&
4518c2ecf20Sopenharmony_ci				     (rcu_access_pointer(mpath->next_hop) !=
4528c2ecf20Sopenharmony_ci						      sta ?
4538c2ecf20Sopenharmony_ci					      mult_frac(new_metric, 10, 9) :
4548c2ecf20Sopenharmony_ci					      new_metric) >= mpath->metric)) {
4558c2ecf20Sopenharmony_ci					process = false;
4568c2ecf20Sopenharmony_ci					fresh_info = false;
4578c2ecf20Sopenharmony_ci				}
4588c2ecf20Sopenharmony_ci			} else if (!(mpath->flags & MESH_PATH_ACTIVE)) {
4598c2ecf20Sopenharmony_ci				bool have_sn, newer_sn, bounced;
4608c2ecf20Sopenharmony_ci
4618c2ecf20Sopenharmony_ci				have_sn = mpath->flags & MESH_PATH_SN_VALID;
4628c2ecf20Sopenharmony_ci				newer_sn = have_sn && SN_GT(orig_sn, mpath->sn);
4638c2ecf20Sopenharmony_ci				bounced = have_sn &&
4648c2ecf20Sopenharmony_ci					  (SN_DELTA(orig_sn, mpath->sn) >
4658c2ecf20Sopenharmony_ci							MAX_SANE_SN_DELTA);
4668c2ecf20Sopenharmony_ci
4678c2ecf20Sopenharmony_ci				if (!have_sn || newer_sn) {
4688c2ecf20Sopenharmony_ci					/* if SN is newer than what we had
4698c2ecf20Sopenharmony_ci					 * then we can take it */;
4708c2ecf20Sopenharmony_ci				} else if (bounced) {
4718c2ecf20Sopenharmony_ci					/* if SN is way different than what
4728c2ecf20Sopenharmony_ci					 * we had then assume the other side
4738c2ecf20Sopenharmony_ci					 * rebooted or restarted */;
4748c2ecf20Sopenharmony_ci				} else {
4758c2ecf20Sopenharmony_ci					process = false;
4768c2ecf20Sopenharmony_ci					fresh_info = false;
4778c2ecf20Sopenharmony_ci				}
4788c2ecf20Sopenharmony_ci			}
4798c2ecf20Sopenharmony_ci		} else {
4808c2ecf20Sopenharmony_ci			mpath = mesh_path_add(sdata, orig_addr);
4818c2ecf20Sopenharmony_ci			if (IS_ERR(mpath)) {
4828c2ecf20Sopenharmony_ci				rcu_read_unlock();
4838c2ecf20Sopenharmony_ci				return 0;
4848c2ecf20Sopenharmony_ci			}
4858c2ecf20Sopenharmony_ci			spin_lock_bh(&mpath->state_lock);
4868c2ecf20Sopenharmony_ci		}
4878c2ecf20Sopenharmony_ci
4888c2ecf20Sopenharmony_ci		if (fresh_info) {
4898c2ecf20Sopenharmony_ci			if (rcu_access_pointer(mpath->next_hop) != sta)
4908c2ecf20Sopenharmony_ci				mpath->path_change_count++;
4918c2ecf20Sopenharmony_ci			mesh_path_assign_nexthop(mpath, sta);
4928c2ecf20Sopenharmony_ci			mpath->flags |= MESH_PATH_SN_VALID;
4938c2ecf20Sopenharmony_ci			mpath->metric = new_metric;
4948c2ecf20Sopenharmony_ci			mpath->sn = orig_sn;
4958c2ecf20Sopenharmony_ci			mpath->exp_time = time_after(mpath->exp_time, exp_time)
4968c2ecf20Sopenharmony_ci					  ?  mpath->exp_time : exp_time;
4978c2ecf20Sopenharmony_ci			mpath->hop_count = hopcount;
4988c2ecf20Sopenharmony_ci			mesh_path_activate(mpath);
4998c2ecf20Sopenharmony_ci			spin_unlock_bh(&mpath->state_lock);
5008c2ecf20Sopenharmony_ci			ewma_mesh_fail_avg_init(&sta->mesh->fail_avg);
5018c2ecf20Sopenharmony_ci			/* init it at a low value - 0 start is tricky */
5028c2ecf20Sopenharmony_ci			ewma_mesh_fail_avg_add(&sta->mesh->fail_avg, 1);
5038c2ecf20Sopenharmony_ci			mesh_path_tx_pending(mpath);
5048c2ecf20Sopenharmony_ci			/* draft says preq_id should be saved to, but there does
5058c2ecf20Sopenharmony_ci			 * not seem to be any use for it, skipping by now
5068c2ecf20Sopenharmony_ci			 */
5078c2ecf20Sopenharmony_ci		} else
5088c2ecf20Sopenharmony_ci			spin_unlock_bh(&mpath->state_lock);
5098c2ecf20Sopenharmony_ci	}
5108c2ecf20Sopenharmony_ci
5118c2ecf20Sopenharmony_ci	/* Update and check transmitter routing info */
5128c2ecf20Sopenharmony_ci	ta = mgmt->sa;
5138c2ecf20Sopenharmony_ci	if (ether_addr_equal(orig_addr, ta))
5148c2ecf20Sopenharmony_ci		fresh_info = false;
5158c2ecf20Sopenharmony_ci	else {
5168c2ecf20Sopenharmony_ci		fresh_info = true;
5178c2ecf20Sopenharmony_ci
5188c2ecf20Sopenharmony_ci		mpath = mesh_path_lookup(sdata, ta);
5198c2ecf20Sopenharmony_ci		if (mpath) {
5208c2ecf20Sopenharmony_ci			spin_lock_bh(&mpath->state_lock);
5218c2ecf20Sopenharmony_ci			if ((mpath->flags & MESH_PATH_FIXED) ||
5228c2ecf20Sopenharmony_ci			    ((mpath->flags & MESH_PATH_ACTIVE) &&
5238c2ecf20Sopenharmony_ci			     ((rcu_access_pointer(mpath->next_hop) != sta ?
5248c2ecf20Sopenharmony_ci				       mult_frac(last_hop_metric, 10, 9) :
5258c2ecf20Sopenharmony_ci				       last_hop_metric) > mpath->metric)))
5268c2ecf20Sopenharmony_ci				fresh_info = false;
5278c2ecf20Sopenharmony_ci		} else {
5288c2ecf20Sopenharmony_ci			mpath = mesh_path_add(sdata, ta);
5298c2ecf20Sopenharmony_ci			if (IS_ERR(mpath)) {
5308c2ecf20Sopenharmony_ci				rcu_read_unlock();
5318c2ecf20Sopenharmony_ci				return 0;
5328c2ecf20Sopenharmony_ci			}
5338c2ecf20Sopenharmony_ci			spin_lock_bh(&mpath->state_lock);
5348c2ecf20Sopenharmony_ci		}
5358c2ecf20Sopenharmony_ci
5368c2ecf20Sopenharmony_ci		if (fresh_info) {
5378c2ecf20Sopenharmony_ci			if (rcu_access_pointer(mpath->next_hop) != sta)
5388c2ecf20Sopenharmony_ci				mpath->path_change_count++;
5398c2ecf20Sopenharmony_ci			mesh_path_assign_nexthop(mpath, sta);
5408c2ecf20Sopenharmony_ci			mpath->metric = last_hop_metric;
5418c2ecf20Sopenharmony_ci			mpath->exp_time = time_after(mpath->exp_time, exp_time)
5428c2ecf20Sopenharmony_ci					  ?  mpath->exp_time : exp_time;
5438c2ecf20Sopenharmony_ci			mpath->hop_count = 1;
5448c2ecf20Sopenharmony_ci			mesh_path_activate(mpath);
5458c2ecf20Sopenharmony_ci			spin_unlock_bh(&mpath->state_lock);
5468c2ecf20Sopenharmony_ci			ewma_mesh_fail_avg_init(&sta->mesh->fail_avg);
5478c2ecf20Sopenharmony_ci			/* init it at a low value - 0 start is tricky */
5488c2ecf20Sopenharmony_ci			ewma_mesh_fail_avg_add(&sta->mesh->fail_avg, 1);
5498c2ecf20Sopenharmony_ci			mesh_path_tx_pending(mpath);
5508c2ecf20Sopenharmony_ci		} else
5518c2ecf20Sopenharmony_ci			spin_unlock_bh(&mpath->state_lock);
5528c2ecf20Sopenharmony_ci	}
5538c2ecf20Sopenharmony_ci
5548c2ecf20Sopenharmony_ci	rcu_read_unlock();
5558c2ecf20Sopenharmony_ci
5568c2ecf20Sopenharmony_ci	return process ? new_metric : 0;
5578c2ecf20Sopenharmony_ci}
5588c2ecf20Sopenharmony_ci
5598c2ecf20Sopenharmony_cistatic void hwmp_preq_frame_process(struct ieee80211_sub_if_data *sdata,
5608c2ecf20Sopenharmony_ci				    struct ieee80211_mgmt *mgmt,
5618c2ecf20Sopenharmony_ci				    const u8 *preq_elem, u32 orig_metric)
5628c2ecf20Sopenharmony_ci{
5638c2ecf20Sopenharmony_ci	struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
5648c2ecf20Sopenharmony_ci	struct mesh_path *mpath = NULL;
5658c2ecf20Sopenharmony_ci	const u8 *target_addr, *orig_addr;
5668c2ecf20Sopenharmony_ci	const u8 *da;
5678c2ecf20Sopenharmony_ci	u8 target_flags, ttl, flags;
5688c2ecf20Sopenharmony_ci	u32 orig_sn, target_sn, lifetime, target_metric = 0;
5698c2ecf20Sopenharmony_ci	bool reply = false;
5708c2ecf20Sopenharmony_ci	bool forward = true;
5718c2ecf20Sopenharmony_ci	bool root_is_gate;
5728c2ecf20Sopenharmony_ci
5738c2ecf20Sopenharmony_ci	/* Update target SN, if present */
5748c2ecf20Sopenharmony_ci	target_addr = PREQ_IE_TARGET_ADDR(preq_elem);
5758c2ecf20Sopenharmony_ci	orig_addr = PREQ_IE_ORIG_ADDR(preq_elem);
5768c2ecf20Sopenharmony_ci	target_sn = PREQ_IE_TARGET_SN(preq_elem);
5778c2ecf20Sopenharmony_ci	orig_sn = PREQ_IE_ORIG_SN(preq_elem);
5788c2ecf20Sopenharmony_ci	target_flags = PREQ_IE_TARGET_F(preq_elem);
5798c2ecf20Sopenharmony_ci	/* Proactive PREQ gate announcements */
5808c2ecf20Sopenharmony_ci	flags = PREQ_IE_FLAGS(preq_elem);
5818c2ecf20Sopenharmony_ci	root_is_gate = !!(flags & RANN_FLAG_IS_GATE);
5828c2ecf20Sopenharmony_ci
5838c2ecf20Sopenharmony_ci	mhwmp_dbg(sdata, "received PREQ from %pM\n", orig_addr);
5848c2ecf20Sopenharmony_ci
5858c2ecf20Sopenharmony_ci	if (ether_addr_equal(target_addr, sdata->vif.addr)) {
5868c2ecf20Sopenharmony_ci		mhwmp_dbg(sdata, "PREQ is for us\n");
5878c2ecf20Sopenharmony_ci		forward = false;
5888c2ecf20Sopenharmony_ci		reply = true;
5898c2ecf20Sopenharmony_ci		target_metric = 0;
5908c2ecf20Sopenharmony_ci
5918c2ecf20Sopenharmony_ci		if (SN_GT(target_sn, ifmsh->sn))
5928c2ecf20Sopenharmony_ci			ifmsh->sn = target_sn;
5938c2ecf20Sopenharmony_ci
5948c2ecf20Sopenharmony_ci		if (time_after(jiffies, ifmsh->last_sn_update +
5958c2ecf20Sopenharmony_ci					net_traversal_jiffies(sdata)) ||
5968c2ecf20Sopenharmony_ci		    time_before(jiffies, ifmsh->last_sn_update)) {
5978c2ecf20Sopenharmony_ci			++ifmsh->sn;
5988c2ecf20Sopenharmony_ci			ifmsh->last_sn_update = jiffies;
5998c2ecf20Sopenharmony_ci		}
6008c2ecf20Sopenharmony_ci		target_sn = ifmsh->sn;
6018c2ecf20Sopenharmony_ci	} else if (is_broadcast_ether_addr(target_addr) &&
6028c2ecf20Sopenharmony_ci		   (target_flags & IEEE80211_PREQ_TO_FLAG)) {
6038c2ecf20Sopenharmony_ci		rcu_read_lock();
6048c2ecf20Sopenharmony_ci		mpath = mesh_path_lookup(sdata, orig_addr);
6058c2ecf20Sopenharmony_ci		if (mpath) {
6068c2ecf20Sopenharmony_ci			if (flags & IEEE80211_PREQ_PROACTIVE_PREP_FLAG) {
6078c2ecf20Sopenharmony_ci				reply = true;
6088c2ecf20Sopenharmony_ci				target_addr = sdata->vif.addr;
6098c2ecf20Sopenharmony_ci				target_sn = ++ifmsh->sn;
6108c2ecf20Sopenharmony_ci				target_metric = 0;
6118c2ecf20Sopenharmony_ci				ifmsh->last_sn_update = jiffies;
6128c2ecf20Sopenharmony_ci			}
6138c2ecf20Sopenharmony_ci			if (root_is_gate)
6148c2ecf20Sopenharmony_ci				mesh_path_add_gate(mpath);
6158c2ecf20Sopenharmony_ci		}
6168c2ecf20Sopenharmony_ci		rcu_read_unlock();
6178c2ecf20Sopenharmony_ci	} else {
6188c2ecf20Sopenharmony_ci		rcu_read_lock();
6198c2ecf20Sopenharmony_ci		mpath = mesh_path_lookup(sdata, target_addr);
6208c2ecf20Sopenharmony_ci		if (mpath) {
6218c2ecf20Sopenharmony_ci			if ((!(mpath->flags & MESH_PATH_SN_VALID)) ||
6228c2ecf20Sopenharmony_ci					SN_LT(mpath->sn, target_sn)) {
6238c2ecf20Sopenharmony_ci				mpath->sn = target_sn;
6248c2ecf20Sopenharmony_ci				mpath->flags |= MESH_PATH_SN_VALID;
6258c2ecf20Sopenharmony_ci			} else if ((!(target_flags & IEEE80211_PREQ_TO_FLAG)) &&
6268c2ecf20Sopenharmony_ci					(mpath->flags & MESH_PATH_ACTIVE)) {
6278c2ecf20Sopenharmony_ci				reply = true;
6288c2ecf20Sopenharmony_ci				target_metric = mpath->metric;
6298c2ecf20Sopenharmony_ci				target_sn = mpath->sn;
6308c2ecf20Sopenharmony_ci				/* Case E2 of sec 13.10.9.3 IEEE 802.11-2012*/
6318c2ecf20Sopenharmony_ci				target_flags |= IEEE80211_PREQ_TO_FLAG;
6328c2ecf20Sopenharmony_ci			}
6338c2ecf20Sopenharmony_ci		}
6348c2ecf20Sopenharmony_ci		rcu_read_unlock();
6358c2ecf20Sopenharmony_ci	}
6368c2ecf20Sopenharmony_ci
6378c2ecf20Sopenharmony_ci	if (reply) {
6388c2ecf20Sopenharmony_ci		lifetime = PREQ_IE_LIFETIME(preq_elem);
6398c2ecf20Sopenharmony_ci		ttl = ifmsh->mshcfg.element_ttl;
6408c2ecf20Sopenharmony_ci		if (ttl != 0) {
6418c2ecf20Sopenharmony_ci			mhwmp_dbg(sdata, "replying to the PREQ\n");
6428c2ecf20Sopenharmony_ci			mesh_path_sel_frame_tx(MPATH_PREP, 0, orig_addr,
6438c2ecf20Sopenharmony_ci					       orig_sn, 0, target_addr,
6448c2ecf20Sopenharmony_ci					       target_sn, mgmt->sa, 0, ttl,
6458c2ecf20Sopenharmony_ci					       lifetime, target_metric, 0,
6468c2ecf20Sopenharmony_ci					       sdata);
6478c2ecf20Sopenharmony_ci		} else {
6488c2ecf20Sopenharmony_ci			ifmsh->mshstats.dropped_frames_ttl++;
6498c2ecf20Sopenharmony_ci		}
6508c2ecf20Sopenharmony_ci	}
6518c2ecf20Sopenharmony_ci
6528c2ecf20Sopenharmony_ci	if (forward && ifmsh->mshcfg.dot11MeshForwarding) {
6538c2ecf20Sopenharmony_ci		u32 preq_id;
6548c2ecf20Sopenharmony_ci		u8 hopcount;
6558c2ecf20Sopenharmony_ci
6568c2ecf20Sopenharmony_ci		ttl = PREQ_IE_TTL(preq_elem);
6578c2ecf20Sopenharmony_ci		lifetime = PREQ_IE_LIFETIME(preq_elem);
6588c2ecf20Sopenharmony_ci		if (ttl <= 1) {
6598c2ecf20Sopenharmony_ci			ifmsh->mshstats.dropped_frames_ttl++;
6608c2ecf20Sopenharmony_ci			return;
6618c2ecf20Sopenharmony_ci		}
6628c2ecf20Sopenharmony_ci		mhwmp_dbg(sdata, "forwarding the PREQ from %pM\n", orig_addr);
6638c2ecf20Sopenharmony_ci		--ttl;
6648c2ecf20Sopenharmony_ci		preq_id = PREQ_IE_PREQ_ID(preq_elem);
6658c2ecf20Sopenharmony_ci		hopcount = PREQ_IE_HOPCOUNT(preq_elem) + 1;
6668c2ecf20Sopenharmony_ci		da = (mpath && mpath->is_root) ?
6678c2ecf20Sopenharmony_ci			mpath->rann_snd_addr : broadcast_addr;
6688c2ecf20Sopenharmony_ci
6698c2ecf20Sopenharmony_ci		if (flags & IEEE80211_PREQ_PROACTIVE_PREP_FLAG) {
6708c2ecf20Sopenharmony_ci			target_addr = PREQ_IE_TARGET_ADDR(preq_elem);
6718c2ecf20Sopenharmony_ci			target_sn = PREQ_IE_TARGET_SN(preq_elem);
6728c2ecf20Sopenharmony_ci		}
6738c2ecf20Sopenharmony_ci
6748c2ecf20Sopenharmony_ci		mesh_path_sel_frame_tx(MPATH_PREQ, flags, orig_addr,
6758c2ecf20Sopenharmony_ci				       orig_sn, target_flags, target_addr,
6768c2ecf20Sopenharmony_ci				       target_sn, da, hopcount, ttl, lifetime,
6778c2ecf20Sopenharmony_ci				       orig_metric, preq_id, sdata);
6788c2ecf20Sopenharmony_ci		if (!is_multicast_ether_addr(da))
6798c2ecf20Sopenharmony_ci			ifmsh->mshstats.fwded_unicast++;
6808c2ecf20Sopenharmony_ci		else
6818c2ecf20Sopenharmony_ci			ifmsh->mshstats.fwded_mcast++;
6828c2ecf20Sopenharmony_ci		ifmsh->mshstats.fwded_frames++;
6838c2ecf20Sopenharmony_ci	}
6848c2ecf20Sopenharmony_ci}
6858c2ecf20Sopenharmony_ci
6868c2ecf20Sopenharmony_ci
6878c2ecf20Sopenharmony_cistatic inline struct sta_info *
6888c2ecf20Sopenharmony_cinext_hop_deref_protected(struct mesh_path *mpath)
6898c2ecf20Sopenharmony_ci{
6908c2ecf20Sopenharmony_ci	return rcu_dereference_protected(mpath->next_hop,
6918c2ecf20Sopenharmony_ci					 lockdep_is_held(&mpath->state_lock));
6928c2ecf20Sopenharmony_ci}
6938c2ecf20Sopenharmony_ci
6948c2ecf20Sopenharmony_ci
6958c2ecf20Sopenharmony_cistatic void hwmp_prep_frame_process(struct ieee80211_sub_if_data *sdata,
6968c2ecf20Sopenharmony_ci				    struct ieee80211_mgmt *mgmt,
6978c2ecf20Sopenharmony_ci				    const u8 *prep_elem, u32 metric)
6988c2ecf20Sopenharmony_ci{
6998c2ecf20Sopenharmony_ci	struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
7008c2ecf20Sopenharmony_ci	struct mesh_path *mpath;
7018c2ecf20Sopenharmony_ci	const u8 *target_addr, *orig_addr;
7028c2ecf20Sopenharmony_ci	u8 ttl, hopcount, flags;
7038c2ecf20Sopenharmony_ci	u8 next_hop[ETH_ALEN];
7048c2ecf20Sopenharmony_ci	u32 target_sn, orig_sn, lifetime;
7058c2ecf20Sopenharmony_ci
7068c2ecf20Sopenharmony_ci	mhwmp_dbg(sdata, "received PREP from %pM\n",
7078c2ecf20Sopenharmony_ci		  PREP_IE_TARGET_ADDR(prep_elem));
7088c2ecf20Sopenharmony_ci
7098c2ecf20Sopenharmony_ci	orig_addr = PREP_IE_ORIG_ADDR(prep_elem);
7108c2ecf20Sopenharmony_ci	if (ether_addr_equal(orig_addr, sdata->vif.addr))
7118c2ecf20Sopenharmony_ci		/* destination, no forwarding required */
7128c2ecf20Sopenharmony_ci		return;
7138c2ecf20Sopenharmony_ci
7148c2ecf20Sopenharmony_ci	if (!ifmsh->mshcfg.dot11MeshForwarding)
7158c2ecf20Sopenharmony_ci		return;
7168c2ecf20Sopenharmony_ci
7178c2ecf20Sopenharmony_ci	ttl = PREP_IE_TTL(prep_elem);
7188c2ecf20Sopenharmony_ci	if (ttl <= 1) {
7198c2ecf20Sopenharmony_ci		sdata->u.mesh.mshstats.dropped_frames_ttl++;
7208c2ecf20Sopenharmony_ci		return;
7218c2ecf20Sopenharmony_ci	}
7228c2ecf20Sopenharmony_ci
7238c2ecf20Sopenharmony_ci	rcu_read_lock();
7248c2ecf20Sopenharmony_ci	mpath = mesh_path_lookup(sdata, orig_addr);
7258c2ecf20Sopenharmony_ci	if (mpath)
7268c2ecf20Sopenharmony_ci		spin_lock_bh(&mpath->state_lock);
7278c2ecf20Sopenharmony_ci	else
7288c2ecf20Sopenharmony_ci		goto fail;
7298c2ecf20Sopenharmony_ci	if (!(mpath->flags & MESH_PATH_ACTIVE)) {
7308c2ecf20Sopenharmony_ci		spin_unlock_bh(&mpath->state_lock);
7318c2ecf20Sopenharmony_ci		goto fail;
7328c2ecf20Sopenharmony_ci	}
7338c2ecf20Sopenharmony_ci	memcpy(next_hop, next_hop_deref_protected(mpath)->sta.addr, ETH_ALEN);
7348c2ecf20Sopenharmony_ci	spin_unlock_bh(&mpath->state_lock);
7358c2ecf20Sopenharmony_ci	--ttl;
7368c2ecf20Sopenharmony_ci	flags = PREP_IE_FLAGS(prep_elem);
7378c2ecf20Sopenharmony_ci	lifetime = PREP_IE_LIFETIME(prep_elem);
7388c2ecf20Sopenharmony_ci	hopcount = PREP_IE_HOPCOUNT(prep_elem) + 1;
7398c2ecf20Sopenharmony_ci	target_addr = PREP_IE_TARGET_ADDR(prep_elem);
7408c2ecf20Sopenharmony_ci	target_sn = PREP_IE_TARGET_SN(prep_elem);
7418c2ecf20Sopenharmony_ci	orig_sn = PREP_IE_ORIG_SN(prep_elem);
7428c2ecf20Sopenharmony_ci
7438c2ecf20Sopenharmony_ci	mesh_path_sel_frame_tx(MPATH_PREP, flags, orig_addr, orig_sn, 0,
7448c2ecf20Sopenharmony_ci			       target_addr, target_sn, next_hop, hopcount,
7458c2ecf20Sopenharmony_ci			       ttl, lifetime, metric, 0, sdata);
7468c2ecf20Sopenharmony_ci	rcu_read_unlock();
7478c2ecf20Sopenharmony_ci
7488c2ecf20Sopenharmony_ci	sdata->u.mesh.mshstats.fwded_unicast++;
7498c2ecf20Sopenharmony_ci	sdata->u.mesh.mshstats.fwded_frames++;
7508c2ecf20Sopenharmony_ci	return;
7518c2ecf20Sopenharmony_ci
7528c2ecf20Sopenharmony_cifail:
7538c2ecf20Sopenharmony_ci	rcu_read_unlock();
7548c2ecf20Sopenharmony_ci	sdata->u.mesh.mshstats.dropped_frames_no_route++;
7558c2ecf20Sopenharmony_ci}
7568c2ecf20Sopenharmony_ci
7578c2ecf20Sopenharmony_cistatic void hwmp_perr_frame_process(struct ieee80211_sub_if_data *sdata,
7588c2ecf20Sopenharmony_ci				    struct ieee80211_mgmt *mgmt,
7598c2ecf20Sopenharmony_ci				    const u8 *perr_elem)
7608c2ecf20Sopenharmony_ci{
7618c2ecf20Sopenharmony_ci	struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
7628c2ecf20Sopenharmony_ci	struct mesh_path *mpath;
7638c2ecf20Sopenharmony_ci	u8 ttl;
7648c2ecf20Sopenharmony_ci	const u8 *ta, *target_addr;
7658c2ecf20Sopenharmony_ci	u32 target_sn;
7668c2ecf20Sopenharmony_ci	u16 target_rcode;
7678c2ecf20Sopenharmony_ci
7688c2ecf20Sopenharmony_ci	ta = mgmt->sa;
7698c2ecf20Sopenharmony_ci	ttl = PERR_IE_TTL(perr_elem);
7708c2ecf20Sopenharmony_ci	if (ttl <= 1) {
7718c2ecf20Sopenharmony_ci		ifmsh->mshstats.dropped_frames_ttl++;
7728c2ecf20Sopenharmony_ci		return;
7738c2ecf20Sopenharmony_ci	}
7748c2ecf20Sopenharmony_ci	ttl--;
7758c2ecf20Sopenharmony_ci	target_addr = PERR_IE_TARGET_ADDR(perr_elem);
7768c2ecf20Sopenharmony_ci	target_sn = PERR_IE_TARGET_SN(perr_elem);
7778c2ecf20Sopenharmony_ci	target_rcode = PERR_IE_TARGET_RCODE(perr_elem);
7788c2ecf20Sopenharmony_ci
7798c2ecf20Sopenharmony_ci	rcu_read_lock();
7808c2ecf20Sopenharmony_ci	mpath = mesh_path_lookup(sdata, target_addr);
7818c2ecf20Sopenharmony_ci	if (mpath) {
7828c2ecf20Sopenharmony_ci		struct sta_info *sta;
7838c2ecf20Sopenharmony_ci
7848c2ecf20Sopenharmony_ci		spin_lock_bh(&mpath->state_lock);
7858c2ecf20Sopenharmony_ci		sta = next_hop_deref_protected(mpath);
7868c2ecf20Sopenharmony_ci		if (mpath->flags & MESH_PATH_ACTIVE &&
7878c2ecf20Sopenharmony_ci		    ether_addr_equal(ta, sta->sta.addr) &&
7888c2ecf20Sopenharmony_ci		    !(mpath->flags & MESH_PATH_FIXED) &&
7898c2ecf20Sopenharmony_ci		    (!(mpath->flags & MESH_PATH_SN_VALID) ||
7908c2ecf20Sopenharmony_ci		    SN_GT(target_sn, mpath->sn)  || target_sn == 0)) {
7918c2ecf20Sopenharmony_ci			mpath->flags &= ~MESH_PATH_ACTIVE;
7928c2ecf20Sopenharmony_ci			if (target_sn != 0)
7938c2ecf20Sopenharmony_ci				mpath->sn = target_sn;
7948c2ecf20Sopenharmony_ci			else
7958c2ecf20Sopenharmony_ci				mpath->sn += 1;
7968c2ecf20Sopenharmony_ci			spin_unlock_bh(&mpath->state_lock);
7978c2ecf20Sopenharmony_ci			if (!ifmsh->mshcfg.dot11MeshForwarding)
7988c2ecf20Sopenharmony_ci				goto endperr;
7998c2ecf20Sopenharmony_ci			mesh_path_error_tx(sdata, ttl, target_addr,
8008c2ecf20Sopenharmony_ci					   target_sn, target_rcode,
8018c2ecf20Sopenharmony_ci					   broadcast_addr);
8028c2ecf20Sopenharmony_ci		} else
8038c2ecf20Sopenharmony_ci			spin_unlock_bh(&mpath->state_lock);
8048c2ecf20Sopenharmony_ci	}
8058c2ecf20Sopenharmony_ciendperr:
8068c2ecf20Sopenharmony_ci	rcu_read_unlock();
8078c2ecf20Sopenharmony_ci}
8088c2ecf20Sopenharmony_ci
8098c2ecf20Sopenharmony_cistatic void hwmp_rann_frame_process(struct ieee80211_sub_if_data *sdata,
8108c2ecf20Sopenharmony_ci				    struct ieee80211_mgmt *mgmt,
8118c2ecf20Sopenharmony_ci				    const struct ieee80211_rann_ie *rann)
8128c2ecf20Sopenharmony_ci{
8138c2ecf20Sopenharmony_ci	struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
8148c2ecf20Sopenharmony_ci	struct ieee80211_local *local = sdata->local;
8158c2ecf20Sopenharmony_ci	struct sta_info *sta;
8168c2ecf20Sopenharmony_ci	struct mesh_path *mpath;
8178c2ecf20Sopenharmony_ci	u8 ttl, flags, hopcount;
8188c2ecf20Sopenharmony_ci	const u8 *orig_addr;
8198c2ecf20Sopenharmony_ci	u32 orig_sn, new_metric, orig_metric, last_hop_metric, interval;
8208c2ecf20Sopenharmony_ci	bool root_is_gate;
8218c2ecf20Sopenharmony_ci
8228c2ecf20Sopenharmony_ci	ttl = rann->rann_ttl;
8238c2ecf20Sopenharmony_ci	flags = rann->rann_flags;
8248c2ecf20Sopenharmony_ci	root_is_gate = !!(flags & RANN_FLAG_IS_GATE);
8258c2ecf20Sopenharmony_ci	orig_addr = rann->rann_addr;
8268c2ecf20Sopenharmony_ci	orig_sn = le32_to_cpu(rann->rann_seq);
8278c2ecf20Sopenharmony_ci	interval = le32_to_cpu(rann->rann_interval);
8288c2ecf20Sopenharmony_ci	hopcount = rann->rann_hopcount;
8298c2ecf20Sopenharmony_ci	hopcount++;
8308c2ecf20Sopenharmony_ci	orig_metric = le32_to_cpu(rann->rann_metric);
8318c2ecf20Sopenharmony_ci
8328c2ecf20Sopenharmony_ci	/*  Ignore our own RANNs */
8338c2ecf20Sopenharmony_ci	if (ether_addr_equal(orig_addr, sdata->vif.addr))
8348c2ecf20Sopenharmony_ci		return;
8358c2ecf20Sopenharmony_ci
8368c2ecf20Sopenharmony_ci	mhwmp_dbg(sdata,
8378c2ecf20Sopenharmony_ci		  "received RANN from %pM via neighbour %pM (is_gate=%d)\n",
8388c2ecf20Sopenharmony_ci		  orig_addr, mgmt->sa, root_is_gate);
8398c2ecf20Sopenharmony_ci
8408c2ecf20Sopenharmony_ci	rcu_read_lock();
8418c2ecf20Sopenharmony_ci	sta = sta_info_get(sdata, mgmt->sa);
8428c2ecf20Sopenharmony_ci	if (!sta) {
8438c2ecf20Sopenharmony_ci		rcu_read_unlock();
8448c2ecf20Sopenharmony_ci		return;
8458c2ecf20Sopenharmony_ci	}
8468c2ecf20Sopenharmony_ci
8478c2ecf20Sopenharmony_ci	last_hop_metric = airtime_link_metric_get(local, sta);
8488c2ecf20Sopenharmony_ci	new_metric = orig_metric + last_hop_metric;
8498c2ecf20Sopenharmony_ci	if (new_metric < orig_metric)
8508c2ecf20Sopenharmony_ci		new_metric = MAX_METRIC;
8518c2ecf20Sopenharmony_ci
8528c2ecf20Sopenharmony_ci	mpath = mesh_path_lookup(sdata, orig_addr);
8538c2ecf20Sopenharmony_ci	if (!mpath) {
8548c2ecf20Sopenharmony_ci		mpath = mesh_path_add(sdata, orig_addr);
8558c2ecf20Sopenharmony_ci		if (IS_ERR(mpath)) {
8568c2ecf20Sopenharmony_ci			rcu_read_unlock();
8578c2ecf20Sopenharmony_ci			sdata->u.mesh.mshstats.dropped_frames_no_route++;
8588c2ecf20Sopenharmony_ci			return;
8598c2ecf20Sopenharmony_ci		}
8608c2ecf20Sopenharmony_ci	}
8618c2ecf20Sopenharmony_ci
8628c2ecf20Sopenharmony_ci	if (!(SN_LT(mpath->sn, orig_sn)) &&
8638c2ecf20Sopenharmony_ci	    !(mpath->sn == orig_sn && new_metric < mpath->rann_metric)) {
8648c2ecf20Sopenharmony_ci		rcu_read_unlock();
8658c2ecf20Sopenharmony_ci		return;
8668c2ecf20Sopenharmony_ci	}
8678c2ecf20Sopenharmony_ci
8688c2ecf20Sopenharmony_ci	if ((!(mpath->flags & (MESH_PATH_ACTIVE | MESH_PATH_RESOLVING)) ||
8698c2ecf20Sopenharmony_ci	     (time_after(jiffies, mpath->last_preq_to_root +
8708c2ecf20Sopenharmony_ci				  root_path_confirmation_jiffies(sdata)) ||
8718c2ecf20Sopenharmony_ci	     time_before(jiffies, mpath->last_preq_to_root))) &&
8728c2ecf20Sopenharmony_ci	     !(mpath->flags & MESH_PATH_FIXED) && (ttl != 0)) {
8738c2ecf20Sopenharmony_ci		mhwmp_dbg(sdata,
8748c2ecf20Sopenharmony_ci			  "time to refresh root mpath %pM\n",
8758c2ecf20Sopenharmony_ci			  orig_addr);
8768c2ecf20Sopenharmony_ci		mesh_queue_preq(mpath, PREQ_Q_F_START | PREQ_Q_F_REFRESH);
8778c2ecf20Sopenharmony_ci		mpath->last_preq_to_root = jiffies;
8788c2ecf20Sopenharmony_ci	}
8798c2ecf20Sopenharmony_ci
8808c2ecf20Sopenharmony_ci	mpath->sn = orig_sn;
8818c2ecf20Sopenharmony_ci	mpath->rann_metric = new_metric;
8828c2ecf20Sopenharmony_ci	mpath->is_root = true;
8838c2ecf20Sopenharmony_ci	/* Recording RANNs sender address to send individually
8848c2ecf20Sopenharmony_ci	 * addressed PREQs destined for root mesh STA */
8858c2ecf20Sopenharmony_ci	memcpy(mpath->rann_snd_addr, mgmt->sa, ETH_ALEN);
8868c2ecf20Sopenharmony_ci
8878c2ecf20Sopenharmony_ci	if (root_is_gate)
8888c2ecf20Sopenharmony_ci		mesh_path_add_gate(mpath);
8898c2ecf20Sopenharmony_ci
8908c2ecf20Sopenharmony_ci	if (ttl <= 1) {
8918c2ecf20Sopenharmony_ci		ifmsh->mshstats.dropped_frames_ttl++;
8928c2ecf20Sopenharmony_ci		rcu_read_unlock();
8938c2ecf20Sopenharmony_ci		return;
8948c2ecf20Sopenharmony_ci	}
8958c2ecf20Sopenharmony_ci	ttl--;
8968c2ecf20Sopenharmony_ci
8978c2ecf20Sopenharmony_ci	if (ifmsh->mshcfg.dot11MeshForwarding) {
8988c2ecf20Sopenharmony_ci		mesh_path_sel_frame_tx(MPATH_RANN, flags, orig_addr,
8998c2ecf20Sopenharmony_ci				       orig_sn, 0, NULL, 0, broadcast_addr,
9008c2ecf20Sopenharmony_ci				       hopcount, ttl, interval,
9018c2ecf20Sopenharmony_ci				       new_metric, 0, sdata);
9028c2ecf20Sopenharmony_ci	}
9038c2ecf20Sopenharmony_ci
9048c2ecf20Sopenharmony_ci	rcu_read_unlock();
9058c2ecf20Sopenharmony_ci}
9068c2ecf20Sopenharmony_ci
9078c2ecf20Sopenharmony_ci
9088c2ecf20Sopenharmony_civoid mesh_rx_path_sel_frame(struct ieee80211_sub_if_data *sdata,
9098c2ecf20Sopenharmony_ci			    struct ieee80211_mgmt *mgmt, size_t len)
9108c2ecf20Sopenharmony_ci{
9118c2ecf20Sopenharmony_ci	struct ieee802_11_elems elems;
9128c2ecf20Sopenharmony_ci	size_t baselen;
9138c2ecf20Sopenharmony_ci	u32 path_metric;
9148c2ecf20Sopenharmony_ci	struct sta_info *sta;
9158c2ecf20Sopenharmony_ci
9168c2ecf20Sopenharmony_ci	/* need action_code */
9178c2ecf20Sopenharmony_ci	if (len < IEEE80211_MIN_ACTION_SIZE + 1)
9188c2ecf20Sopenharmony_ci		return;
9198c2ecf20Sopenharmony_ci
9208c2ecf20Sopenharmony_ci	rcu_read_lock();
9218c2ecf20Sopenharmony_ci	sta = sta_info_get(sdata, mgmt->sa);
9228c2ecf20Sopenharmony_ci	if (!sta || sta->mesh->plink_state != NL80211_PLINK_ESTAB) {
9238c2ecf20Sopenharmony_ci		rcu_read_unlock();
9248c2ecf20Sopenharmony_ci		return;
9258c2ecf20Sopenharmony_ci	}
9268c2ecf20Sopenharmony_ci	rcu_read_unlock();
9278c2ecf20Sopenharmony_ci
9288c2ecf20Sopenharmony_ci	baselen = (u8 *) mgmt->u.action.u.mesh_action.variable - (u8 *) mgmt;
9298c2ecf20Sopenharmony_ci	ieee802_11_parse_elems(mgmt->u.action.u.mesh_action.variable,
9308c2ecf20Sopenharmony_ci			       len - baselen, false, &elems, mgmt->bssid, NULL);
9318c2ecf20Sopenharmony_ci
9328c2ecf20Sopenharmony_ci	if (elems.preq) {
9338c2ecf20Sopenharmony_ci		if (elems.preq_len != 37)
9348c2ecf20Sopenharmony_ci			/* Right now we support just 1 destination and no AE */
9358c2ecf20Sopenharmony_ci			return;
9368c2ecf20Sopenharmony_ci		path_metric = hwmp_route_info_get(sdata, mgmt, elems.preq,
9378c2ecf20Sopenharmony_ci						  MPATH_PREQ);
9388c2ecf20Sopenharmony_ci		if (path_metric)
9398c2ecf20Sopenharmony_ci			hwmp_preq_frame_process(sdata, mgmt, elems.preq,
9408c2ecf20Sopenharmony_ci						path_metric);
9418c2ecf20Sopenharmony_ci	}
9428c2ecf20Sopenharmony_ci	if (elems.prep) {
9438c2ecf20Sopenharmony_ci		if (elems.prep_len != 31)
9448c2ecf20Sopenharmony_ci			/* Right now we support no AE */
9458c2ecf20Sopenharmony_ci			return;
9468c2ecf20Sopenharmony_ci		path_metric = hwmp_route_info_get(sdata, mgmt, elems.prep,
9478c2ecf20Sopenharmony_ci						  MPATH_PREP);
9488c2ecf20Sopenharmony_ci		if (path_metric)
9498c2ecf20Sopenharmony_ci			hwmp_prep_frame_process(sdata, mgmt, elems.prep,
9508c2ecf20Sopenharmony_ci						path_metric);
9518c2ecf20Sopenharmony_ci	}
9528c2ecf20Sopenharmony_ci	if (elems.perr) {
9538c2ecf20Sopenharmony_ci		if (elems.perr_len != 15)
9548c2ecf20Sopenharmony_ci			/* Right now we support only one destination per PERR */
9558c2ecf20Sopenharmony_ci			return;
9568c2ecf20Sopenharmony_ci		hwmp_perr_frame_process(sdata, mgmt, elems.perr);
9578c2ecf20Sopenharmony_ci	}
9588c2ecf20Sopenharmony_ci	if (elems.rann)
9598c2ecf20Sopenharmony_ci		hwmp_rann_frame_process(sdata, mgmt, elems.rann);
9608c2ecf20Sopenharmony_ci}
9618c2ecf20Sopenharmony_ci
9628c2ecf20Sopenharmony_ci/**
9638c2ecf20Sopenharmony_ci * mesh_queue_preq - queue a PREQ to a given destination
9648c2ecf20Sopenharmony_ci *
9658c2ecf20Sopenharmony_ci * @mpath: mesh path to discover
9668c2ecf20Sopenharmony_ci * @flags: special attributes of the PREQ to be sent
9678c2ecf20Sopenharmony_ci *
9688c2ecf20Sopenharmony_ci * Locking: the function must be called from within a rcu read lock block.
9698c2ecf20Sopenharmony_ci *
9708c2ecf20Sopenharmony_ci */
9718c2ecf20Sopenharmony_cistatic void mesh_queue_preq(struct mesh_path *mpath, u8 flags)
9728c2ecf20Sopenharmony_ci{
9738c2ecf20Sopenharmony_ci	struct ieee80211_sub_if_data *sdata = mpath->sdata;
9748c2ecf20Sopenharmony_ci	struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
9758c2ecf20Sopenharmony_ci	struct mesh_preq_queue *preq_node;
9768c2ecf20Sopenharmony_ci
9778c2ecf20Sopenharmony_ci	preq_node = kmalloc(sizeof(struct mesh_preq_queue), GFP_ATOMIC);
9788c2ecf20Sopenharmony_ci	if (!preq_node) {
9798c2ecf20Sopenharmony_ci		mhwmp_dbg(sdata, "could not allocate PREQ node\n");
9808c2ecf20Sopenharmony_ci		return;
9818c2ecf20Sopenharmony_ci	}
9828c2ecf20Sopenharmony_ci
9838c2ecf20Sopenharmony_ci	spin_lock_bh(&ifmsh->mesh_preq_queue_lock);
9848c2ecf20Sopenharmony_ci	if (ifmsh->preq_queue_len == MAX_PREQ_QUEUE_LEN) {
9858c2ecf20Sopenharmony_ci		spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
9868c2ecf20Sopenharmony_ci		kfree(preq_node);
9878c2ecf20Sopenharmony_ci		if (printk_ratelimit())
9888c2ecf20Sopenharmony_ci			mhwmp_dbg(sdata, "PREQ node queue full\n");
9898c2ecf20Sopenharmony_ci		return;
9908c2ecf20Sopenharmony_ci	}
9918c2ecf20Sopenharmony_ci
9928c2ecf20Sopenharmony_ci	spin_lock(&mpath->state_lock);
9938c2ecf20Sopenharmony_ci	if (mpath->flags & MESH_PATH_REQ_QUEUED) {
9948c2ecf20Sopenharmony_ci		spin_unlock(&mpath->state_lock);
9958c2ecf20Sopenharmony_ci		spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
9968c2ecf20Sopenharmony_ci		kfree(preq_node);
9978c2ecf20Sopenharmony_ci		return;
9988c2ecf20Sopenharmony_ci	}
9998c2ecf20Sopenharmony_ci
10008c2ecf20Sopenharmony_ci	memcpy(preq_node->dst, mpath->dst, ETH_ALEN);
10018c2ecf20Sopenharmony_ci	preq_node->flags = flags;
10028c2ecf20Sopenharmony_ci
10038c2ecf20Sopenharmony_ci	mpath->flags |= MESH_PATH_REQ_QUEUED;
10048c2ecf20Sopenharmony_ci	spin_unlock(&mpath->state_lock);
10058c2ecf20Sopenharmony_ci
10068c2ecf20Sopenharmony_ci	list_add_tail(&preq_node->list, &ifmsh->preq_queue.list);
10078c2ecf20Sopenharmony_ci	++ifmsh->preq_queue_len;
10088c2ecf20Sopenharmony_ci	spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
10098c2ecf20Sopenharmony_ci
10108c2ecf20Sopenharmony_ci	if (time_after(jiffies, ifmsh->last_preq + min_preq_int_jiff(sdata)))
10118c2ecf20Sopenharmony_ci		ieee80211_queue_work(&sdata->local->hw, &sdata->work);
10128c2ecf20Sopenharmony_ci
10138c2ecf20Sopenharmony_ci	else if (time_before(jiffies, ifmsh->last_preq)) {
10148c2ecf20Sopenharmony_ci		/* avoid long wait if did not send preqs for a long time
10158c2ecf20Sopenharmony_ci		 * and jiffies wrapped around
10168c2ecf20Sopenharmony_ci		 */
10178c2ecf20Sopenharmony_ci		ifmsh->last_preq = jiffies - min_preq_int_jiff(sdata) - 1;
10188c2ecf20Sopenharmony_ci		ieee80211_queue_work(&sdata->local->hw, &sdata->work);
10198c2ecf20Sopenharmony_ci	} else
10208c2ecf20Sopenharmony_ci		mod_timer(&ifmsh->mesh_path_timer, ifmsh->last_preq +
10218c2ecf20Sopenharmony_ci						min_preq_int_jiff(sdata));
10228c2ecf20Sopenharmony_ci}
10238c2ecf20Sopenharmony_ci
10248c2ecf20Sopenharmony_ci/**
10258c2ecf20Sopenharmony_ci * mesh_path_start_discovery - launch a path discovery from the PREQ queue
10268c2ecf20Sopenharmony_ci *
10278c2ecf20Sopenharmony_ci * @sdata: local mesh subif
10288c2ecf20Sopenharmony_ci */
10298c2ecf20Sopenharmony_civoid mesh_path_start_discovery(struct ieee80211_sub_if_data *sdata)
10308c2ecf20Sopenharmony_ci{
10318c2ecf20Sopenharmony_ci	struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
10328c2ecf20Sopenharmony_ci	struct mesh_preq_queue *preq_node;
10338c2ecf20Sopenharmony_ci	struct mesh_path *mpath;
10348c2ecf20Sopenharmony_ci	u8 ttl, target_flags = 0;
10358c2ecf20Sopenharmony_ci	const u8 *da;
10368c2ecf20Sopenharmony_ci	u32 lifetime;
10378c2ecf20Sopenharmony_ci
10388c2ecf20Sopenharmony_ci	spin_lock_bh(&ifmsh->mesh_preq_queue_lock);
10398c2ecf20Sopenharmony_ci	if (!ifmsh->preq_queue_len ||
10408c2ecf20Sopenharmony_ci		time_before(jiffies, ifmsh->last_preq +
10418c2ecf20Sopenharmony_ci				min_preq_int_jiff(sdata))) {
10428c2ecf20Sopenharmony_ci		spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
10438c2ecf20Sopenharmony_ci		return;
10448c2ecf20Sopenharmony_ci	}
10458c2ecf20Sopenharmony_ci
10468c2ecf20Sopenharmony_ci	preq_node = list_first_entry(&ifmsh->preq_queue.list,
10478c2ecf20Sopenharmony_ci			struct mesh_preq_queue, list);
10488c2ecf20Sopenharmony_ci	list_del(&preq_node->list);
10498c2ecf20Sopenharmony_ci	--ifmsh->preq_queue_len;
10508c2ecf20Sopenharmony_ci	spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
10518c2ecf20Sopenharmony_ci
10528c2ecf20Sopenharmony_ci	rcu_read_lock();
10538c2ecf20Sopenharmony_ci	mpath = mesh_path_lookup(sdata, preq_node->dst);
10548c2ecf20Sopenharmony_ci	if (!mpath)
10558c2ecf20Sopenharmony_ci		goto enddiscovery;
10568c2ecf20Sopenharmony_ci
10578c2ecf20Sopenharmony_ci	spin_lock_bh(&mpath->state_lock);
10588c2ecf20Sopenharmony_ci	if (mpath->flags & (MESH_PATH_DELETED | MESH_PATH_FIXED)) {
10598c2ecf20Sopenharmony_ci		spin_unlock_bh(&mpath->state_lock);
10608c2ecf20Sopenharmony_ci		goto enddiscovery;
10618c2ecf20Sopenharmony_ci	}
10628c2ecf20Sopenharmony_ci	mpath->flags &= ~MESH_PATH_REQ_QUEUED;
10638c2ecf20Sopenharmony_ci	if (preq_node->flags & PREQ_Q_F_START) {
10648c2ecf20Sopenharmony_ci		if (mpath->flags & MESH_PATH_RESOLVING) {
10658c2ecf20Sopenharmony_ci			spin_unlock_bh(&mpath->state_lock);
10668c2ecf20Sopenharmony_ci			goto enddiscovery;
10678c2ecf20Sopenharmony_ci		} else {
10688c2ecf20Sopenharmony_ci			mpath->flags &= ~MESH_PATH_RESOLVED;
10698c2ecf20Sopenharmony_ci			mpath->flags |= MESH_PATH_RESOLVING;
10708c2ecf20Sopenharmony_ci			mpath->discovery_retries = 0;
10718c2ecf20Sopenharmony_ci			mpath->discovery_timeout = disc_timeout_jiff(sdata);
10728c2ecf20Sopenharmony_ci		}
10738c2ecf20Sopenharmony_ci	} else if (!(mpath->flags & MESH_PATH_RESOLVING) ||
10748c2ecf20Sopenharmony_ci			mpath->flags & MESH_PATH_RESOLVED) {
10758c2ecf20Sopenharmony_ci		mpath->flags &= ~MESH_PATH_RESOLVING;
10768c2ecf20Sopenharmony_ci		spin_unlock_bh(&mpath->state_lock);
10778c2ecf20Sopenharmony_ci		goto enddiscovery;
10788c2ecf20Sopenharmony_ci	}
10798c2ecf20Sopenharmony_ci
10808c2ecf20Sopenharmony_ci	ifmsh->last_preq = jiffies;
10818c2ecf20Sopenharmony_ci
10828c2ecf20Sopenharmony_ci	if (time_after(jiffies, ifmsh->last_sn_update +
10838c2ecf20Sopenharmony_ci				net_traversal_jiffies(sdata)) ||
10848c2ecf20Sopenharmony_ci	    time_before(jiffies, ifmsh->last_sn_update)) {
10858c2ecf20Sopenharmony_ci		++ifmsh->sn;
10868c2ecf20Sopenharmony_ci		sdata->u.mesh.last_sn_update = jiffies;
10878c2ecf20Sopenharmony_ci	}
10888c2ecf20Sopenharmony_ci	lifetime = default_lifetime(sdata);
10898c2ecf20Sopenharmony_ci	ttl = sdata->u.mesh.mshcfg.element_ttl;
10908c2ecf20Sopenharmony_ci	if (ttl == 0) {
10918c2ecf20Sopenharmony_ci		sdata->u.mesh.mshstats.dropped_frames_ttl++;
10928c2ecf20Sopenharmony_ci		spin_unlock_bh(&mpath->state_lock);
10938c2ecf20Sopenharmony_ci		goto enddiscovery;
10948c2ecf20Sopenharmony_ci	}
10958c2ecf20Sopenharmony_ci
10968c2ecf20Sopenharmony_ci	if (preq_node->flags & PREQ_Q_F_REFRESH)
10978c2ecf20Sopenharmony_ci		target_flags |= IEEE80211_PREQ_TO_FLAG;
10988c2ecf20Sopenharmony_ci	else
10998c2ecf20Sopenharmony_ci		target_flags &= ~IEEE80211_PREQ_TO_FLAG;
11008c2ecf20Sopenharmony_ci
11018c2ecf20Sopenharmony_ci	spin_unlock_bh(&mpath->state_lock);
11028c2ecf20Sopenharmony_ci	da = (mpath->is_root) ? mpath->rann_snd_addr : broadcast_addr;
11038c2ecf20Sopenharmony_ci	mesh_path_sel_frame_tx(MPATH_PREQ, 0, sdata->vif.addr, ifmsh->sn,
11048c2ecf20Sopenharmony_ci			       target_flags, mpath->dst, mpath->sn, da, 0,
11058c2ecf20Sopenharmony_ci			       ttl, lifetime, 0, ifmsh->preq_id++, sdata);
11068c2ecf20Sopenharmony_ci
11078c2ecf20Sopenharmony_ci	spin_lock_bh(&mpath->state_lock);
11088c2ecf20Sopenharmony_ci	if (!(mpath->flags & MESH_PATH_DELETED))
11098c2ecf20Sopenharmony_ci		mod_timer(&mpath->timer, jiffies + mpath->discovery_timeout);
11108c2ecf20Sopenharmony_ci	spin_unlock_bh(&mpath->state_lock);
11118c2ecf20Sopenharmony_ci
11128c2ecf20Sopenharmony_cienddiscovery:
11138c2ecf20Sopenharmony_ci	rcu_read_unlock();
11148c2ecf20Sopenharmony_ci	kfree(preq_node);
11158c2ecf20Sopenharmony_ci}
11168c2ecf20Sopenharmony_ci
11178c2ecf20Sopenharmony_ci/**
11188c2ecf20Sopenharmony_ci * mesh_nexthop_resolve - lookup next hop; conditionally start path discovery
11198c2ecf20Sopenharmony_ci *
11208c2ecf20Sopenharmony_ci * @skb: 802.11 frame to be sent
11218c2ecf20Sopenharmony_ci * @sdata: network subif the frame will be sent through
11228c2ecf20Sopenharmony_ci *
11238c2ecf20Sopenharmony_ci * Lookup next hop for given skb and start path discovery if no
11248c2ecf20Sopenharmony_ci * forwarding information is found.
11258c2ecf20Sopenharmony_ci *
11268c2ecf20Sopenharmony_ci * Returns: 0 if the next hop was found and -ENOENT if the frame was queued.
11278c2ecf20Sopenharmony_ci * skb is freeed here if no mpath could be allocated.
11288c2ecf20Sopenharmony_ci */
11298c2ecf20Sopenharmony_ciint mesh_nexthop_resolve(struct ieee80211_sub_if_data *sdata,
11308c2ecf20Sopenharmony_ci			 struct sk_buff *skb)
11318c2ecf20Sopenharmony_ci{
11328c2ecf20Sopenharmony_ci	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
11338c2ecf20Sopenharmony_ci	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
11348c2ecf20Sopenharmony_ci	struct mesh_path *mpath;
11358c2ecf20Sopenharmony_ci	struct sk_buff *skb_to_free = NULL;
11368c2ecf20Sopenharmony_ci	u8 *target_addr = hdr->addr3;
11378c2ecf20Sopenharmony_ci
11388c2ecf20Sopenharmony_ci	/* Nulls are only sent to peers for PS and should be pre-addressed */
11398c2ecf20Sopenharmony_ci	if (ieee80211_is_qos_nullfunc(hdr->frame_control))
11408c2ecf20Sopenharmony_ci		return 0;
11418c2ecf20Sopenharmony_ci
11428c2ecf20Sopenharmony_ci	/* Allow injected packets to bypass mesh routing */
11438c2ecf20Sopenharmony_ci	if (info->control.flags & IEEE80211_TX_CTRL_SKIP_MPATH_LOOKUP)
11448c2ecf20Sopenharmony_ci		return 0;
11458c2ecf20Sopenharmony_ci
11468c2ecf20Sopenharmony_ci	if (!mesh_nexthop_lookup(sdata, skb))
11478c2ecf20Sopenharmony_ci		return 0;
11488c2ecf20Sopenharmony_ci
11498c2ecf20Sopenharmony_ci	/* no nexthop found, start resolving */
11508c2ecf20Sopenharmony_ci	mpath = mesh_path_lookup(sdata, target_addr);
11518c2ecf20Sopenharmony_ci	if (!mpath) {
11528c2ecf20Sopenharmony_ci		mpath = mesh_path_add(sdata, target_addr);
11538c2ecf20Sopenharmony_ci		if (IS_ERR(mpath)) {
11548c2ecf20Sopenharmony_ci			mesh_path_discard_frame(sdata, skb);
11558c2ecf20Sopenharmony_ci			return PTR_ERR(mpath);
11568c2ecf20Sopenharmony_ci		}
11578c2ecf20Sopenharmony_ci	}
11588c2ecf20Sopenharmony_ci
11598c2ecf20Sopenharmony_ci	if (!(mpath->flags & MESH_PATH_RESOLVING) &&
11608c2ecf20Sopenharmony_ci	    mesh_path_sel_is_hwmp(sdata))
11618c2ecf20Sopenharmony_ci		mesh_queue_preq(mpath, PREQ_Q_F_START);
11628c2ecf20Sopenharmony_ci
11638c2ecf20Sopenharmony_ci	if (skb_queue_len(&mpath->frame_queue) >= MESH_FRAME_QUEUE_LEN)
11648c2ecf20Sopenharmony_ci		skb_to_free = skb_dequeue(&mpath->frame_queue);
11658c2ecf20Sopenharmony_ci
11668c2ecf20Sopenharmony_ci	info->control.flags |= IEEE80211_TX_INTCFL_NEED_TXPROCESSING;
11678c2ecf20Sopenharmony_ci	ieee80211_set_qos_hdr(sdata, skb);
11688c2ecf20Sopenharmony_ci	skb_queue_tail(&mpath->frame_queue, skb);
11698c2ecf20Sopenharmony_ci	if (skb_to_free)
11708c2ecf20Sopenharmony_ci		mesh_path_discard_frame(sdata, skb_to_free);
11718c2ecf20Sopenharmony_ci
11728c2ecf20Sopenharmony_ci	return -ENOENT;
11738c2ecf20Sopenharmony_ci}
11748c2ecf20Sopenharmony_ci
11758c2ecf20Sopenharmony_ci/**
11768c2ecf20Sopenharmony_ci * mesh_nexthop_lookup_nolearn - try to set next hop without path discovery
11778c2ecf20Sopenharmony_ci * @skb: 802.11 frame to be sent
11788c2ecf20Sopenharmony_ci * @sdata: network subif the frame will be sent through
11798c2ecf20Sopenharmony_ci *
11808c2ecf20Sopenharmony_ci * Check if the meshDA (addr3) of a unicast frame is a direct neighbor.
11818c2ecf20Sopenharmony_ci * And if so, set the RA (addr1) to it to transmit to this node directly,
11828c2ecf20Sopenharmony_ci * avoiding PREQ/PREP path discovery.
11838c2ecf20Sopenharmony_ci *
11848c2ecf20Sopenharmony_ci * Returns: 0 if the next hop was found and -ENOENT otherwise.
11858c2ecf20Sopenharmony_ci */
11868c2ecf20Sopenharmony_cistatic int mesh_nexthop_lookup_nolearn(struct ieee80211_sub_if_data *sdata,
11878c2ecf20Sopenharmony_ci				       struct sk_buff *skb)
11888c2ecf20Sopenharmony_ci{
11898c2ecf20Sopenharmony_ci	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
11908c2ecf20Sopenharmony_ci	struct sta_info *sta;
11918c2ecf20Sopenharmony_ci
11928c2ecf20Sopenharmony_ci	if (is_multicast_ether_addr(hdr->addr1))
11938c2ecf20Sopenharmony_ci		return -ENOENT;
11948c2ecf20Sopenharmony_ci
11958c2ecf20Sopenharmony_ci	rcu_read_lock();
11968c2ecf20Sopenharmony_ci	sta = sta_info_get(sdata, hdr->addr3);
11978c2ecf20Sopenharmony_ci
11988c2ecf20Sopenharmony_ci	if (!sta || sta->mesh->plink_state != NL80211_PLINK_ESTAB) {
11998c2ecf20Sopenharmony_ci		rcu_read_unlock();
12008c2ecf20Sopenharmony_ci		return -ENOENT;
12018c2ecf20Sopenharmony_ci	}
12028c2ecf20Sopenharmony_ci	rcu_read_unlock();
12038c2ecf20Sopenharmony_ci
12048c2ecf20Sopenharmony_ci	memcpy(hdr->addr1, hdr->addr3, ETH_ALEN);
12058c2ecf20Sopenharmony_ci	memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN);
12068c2ecf20Sopenharmony_ci	return 0;
12078c2ecf20Sopenharmony_ci}
12088c2ecf20Sopenharmony_ci
12098c2ecf20Sopenharmony_ci/**
12108c2ecf20Sopenharmony_ci * mesh_nexthop_lookup - put the appropriate next hop on a mesh frame. Calling
12118c2ecf20Sopenharmony_ci * this function is considered "using" the associated mpath, so preempt a path
12128c2ecf20Sopenharmony_ci * refresh if this mpath expires soon.
12138c2ecf20Sopenharmony_ci *
12148c2ecf20Sopenharmony_ci * @skb: 802.11 frame to be sent
12158c2ecf20Sopenharmony_ci * @sdata: network subif the frame will be sent through
12168c2ecf20Sopenharmony_ci *
12178c2ecf20Sopenharmony_ci * Returns: 0 if the next hop was found. Nonzero otherwise.
12188c2ecf20Sopenharmony_ci */
12198c2ecf20Sopenharmony_ciint mesh_nexthop_lookup(struct ieee80211_sub_if_data *sdata,
12208c2ecf20Sopenharmony_ci			struct sk_buff *skb)
12218c2ecf20Sopenharmony_ci{
12228c2ecf20Sopenharmony_ci	struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
12238c2ecf20Sopenharmony_ci	struct mesh_path *mpath;
12248c2ecf20Sopenharmony_ci	struct sta_info *next_hop;
12258c2ecf20Sopenharmony_ci	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
12268c2ecf20Sopenharmony_ci	u8 *target_addr = hdr->addr3;
12278c2ecf20Sopenharmony_ci
12288c2ecf20Sopenharmony_ci	if (ifmsh->mshcfg.dot11MeshNolearn &&
12298c2ecf20Sopenharmony_ci	    !mesh_nexthop_lookup_nolearn(sdata, skb))
12308c2ecf20Sopenharmony_ci		return 0;
12318c2ecf20Sopenharmony_ci
12328c2ecf20Sopenharmony_ci	mpath = mesh_path_lookup(sdata, target_addr);
12338c2ecf20Sopenharmony_ci	if (!mpath || !(mpath->flags & MESH_PATH_ACTIVE))
12348c2ecf20Sopenharmony_ci		return -ENOENT;
12358c2ecf20Sopenharmony_ci
12368c2ecf20Sopenharmony_ci	if (time_after(jiffies,
12378c2ecf20Sopenharmony_ci		       mpath->exp_time -
12388c2ecf20Sopenharmony_ci		       msecs_to_jiffies(sdata->u.mesh.mshcfg.path_refresh_time)) &&
12398c2ecf20Sopenharmony_ci	    ether_addr_equal(sdata->vif.addr, hdr->addr4) &&
12408c2ecf20Sopenharmony_ci	    !(mpath->flags & MESH_PATH_RESOLVING) &&
12418c2ecf20Sopenharmony_ci	    !(mpath->flags & MESH_PATH_FIXED))
12428c2ecf20Sopenharmony_ci		mesh_queue_preq(mpath, PREQ_Q_F_START | PREQ_Q_F_REFRESH);
12438c2ecf20Sopenharmony_ci
12448c2ecf20Sopenharmony_ci	next_hop = rcu_dereference(mpath->next_hop);
12458c2ecf20Sopenharmony_ci	if (next_hop) {
12468c2ecf20Sopenharmony_ci		memcpy(hdr->addr1, next_hop->sta.addr, ETH_ALEN);
12478c2ecf20Sopenharmony_ci		memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN);
12488c2ecf20Sopenharmony_ci		ieee80211_mps_set_frame_flags(sdata, next_hop, hdr);
12498c2ecf20Sopenharmony_ci		return 0;
12508c2ecf20Sopenharmony_ci	}
12518c2ecf20Sopenharmony_ci
12528c2ecf20Sopenharmony_ci	return -ENOENT;
12538c2ecf20Sopenharmony_ci}
12548c2ecf20Sopenharmony_ci
12558c2ecf20Sopenharmony_civoid mesh_path_timer(struct timer_list *t)
12568c2ecf20Sopenharmony_ci{
12578c2ecf20Sopenharmony_ci	struct mesh_path *mpath = from_timer(mpath, t, timer);
12588c2ecf20Sopenharmony_ci	struct ieee80211_sub_if_data *sdata = mpath->sdata;
12598c2ecf20Sopenharmony_ci	int ret;
12608c2ecf20Sopenharmony_ci
12618c2ecf20Sopenharmony_ci	if (sdata->local->quiescing)
12628c2ecf20Sopenharmony_ci		return;
12638c2ecf20Sopenharmony_ci
12648c2ecf20Sopenharmony_ci	spin_lock_bh(&mpath->state_lock);
12658c2ecf20Sopenharmony_ci	if (mpath->flags & MESH_PATH_RESOLVED ||
12668c2ecf20Sopenharmony_ci			(!(mpath->flags & MESH_PATH_RESOLVING))) {
12678c2ecf20Sopenharmony_ci		mpath->flags &= ~(MESH_PATH_RESOLVING | MESH_PATH_RESOLVED);
12688c2ecf20Sopenharmony_ci		spin_unlock_bh(&mpath->state_lock);
12698c2ecf20Sopenharmony_ci	} else if (mpath->discovery_retries < max_preq_retries(sdata)) {
12708c2ecf20Sopenharmony_ci		++mpath->discovery_retries;
12718c2ecf20Sopenharmony_ci		mpath->discovery_timeout *= 2;
12728c2ecf20Sopenharmony_ci		mpath->flags &= ~MESH_PATH_REQ_QUEUED;
12738c2ecf20Sopenharmony_ci		spin_unlock_bh(&mpath->state_lock);
12748c2ecf20Sopenharmony_ci		mesh_queue_preq(mpath, 0);
12758c2ecf20Sopenharmony_ci	} else {
12768c2ecf20Sopenharmony_ci		mpath->flags &= ~(MESH_PATH_RESOLVING |
12778c2ecf20Sopenharmony_ci				  MESH_PATH_RESOLVED |
12788c2ecf20Sopenharmony_ci				  MESH_PATH_REQ_QUEUED);
12798c2ecf20Sopenharmony_ci		mpath->exp_time = jiffies;
12808c2ecf20Sopenharmony_ci		spin_unlock_bh(&mpath->state_lock);
12818c2ecf20Sopenharmony_ci		if (!mpath->is_gate && mesh_gate_num(sdata) > 0) {
12828c2ecf20Sopenharmony_ci			ret = mesh_path_send_to_gates(mpath);
12838c2ecf20Sopenharmony_ci			if (ret)
12848c2ecf20Sopenharmony_ci				mhwmp_dbg(sdata, "no gate was reachable\n");
12858c2ecf20Sopenharmony_ci		} else
12868c2ecf20Sopenharmony_ci			mesh_path_flush_pending(mpath);
12878c2ecf20Sopenharmony_ci	}
12888c2ecf20Sopenharmony_ci}
12898c2ecf20Sopenharmony_ci
12908c2ecf20Sopenharmony_civoid mesh_path_tx_root_frame(struct ieee80211_sub_if_data *sdata)
12918c2ecf20Sopenharmony_ci{
12928c2ecf20Sopenharmony_ci	struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
12938c2ecf20Sopenharmony_ci	u32 interval = ifmsh->mshcfg.dot11MeshHWMPRannInterval;
12948c2ecf20Sopenharmony_ci	u8 flags, target_flags = 0;
12958c2ecf20Sopenharmony_ci
12968c2ecf20Sopenharmony_ci	flags = (ifmsh->mshcfg.dot11MeshGateAnnouncementProtocol)
12978c2ecf20Sopenharmony_ci			? RANN_FLAG_IS_GATE : 0;
12988c2ecf20Sopenharmony_ci
12998c2ecf20Sopenharmony_ci	switch (ifmsh->mshcfg.dot11MeshHWMPRootMode) {
13008c2ecf20Sopenharmony_ci	case IEEE80211_PROACTIVE_RANN:
13018c2ecf20Sopenharmony_ci		mesh_path_sel_frame_tx(MPATH_RANN, flags, sdata->vif.addr,
13028c2ecf20Sopenharmony_ci				       ++ifmsh->sn, 0, NULL, 0, broadcast_addr,
13038c2ecf20Sopenharmony_ci				       0, ifmsh->mshcfg.element_ttl,
13048c2ecf20Sopenharmony_ci				       interval, 0, 0, sdata);
13058c2ecf20Sopenharmony_ci		break;
13068c2ecf20Sopenharmony_ci	case IEEE80211_PROACTIVE_PREQ_WITH_PREP:
13078c2ecf20Sopenharmony_ci		flags |= IEEE80211_PREQ_PROACTIVE_PREP_FLAG;
13088c2ecf20Sopenharmony_ci		fallthrough;
13098c2ecf20Sopenharmony_ci	case IEEE80211_PROACTIVE_PREQ_NO_PREP:
13108c2ecf20Sopenharmony_ci		interval = ifmsh->mshcfg.dot11MeshHWMPactivePathToRootTimeout;
13118c2ecf20Sopenharmony_ci		target_flags |= IEEE80211_PREQ_TO_FLAG |
13128c2ecf20Sopenharmony_ci				IEEE80211_PREQ_USN_FLAG;
13138c2ecf20Sopenharmony_ci		mesh_path_sel_frame_tx(MPATH_PREQ, flags, sdata->vif.addr,
13148c2ecf20Sopenharmony_ci				       ++ifmsh->sn, target_flags,
13158c2ecf20Sopenharmony_ci				       (u8 *) broadcast_addr, 0, broadcast_addr,
13168c2ecf20Sopenharmony_ci				       0, ifmsh->mshcfg.element_ttl, interval,
13178c2ecf20Sopenharmony_ci				       0, ifmsh->preq_id++, sdata);
13188c2ecf20Sopenharmony_ci		break;
13198c2ecf20Sopenharmony_ci	default:
13208c2ecf20Sopenharmony_ci		mhwmp_dbg(sdata, "Proactive mechanism not supported\n");
13218c2ecf20Sopenharmony_ci		return;
13228c2ecf20Sopenharmony_ci	}
13238c2ecf20Sopenharmony_ci}
1324