1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Copyright (c) 2008, 2009 open80211s Ltd.
4*4882a593Smuzhiyun * Copyright (C) 2019 Intel Corporation
5*4882a593Smuzhiyun * Author: Luis Carlos Cobo <luisca@cozybit.com>
6*4882a593Smuzhiyun */
7*4882a593Smuzhiyun
8*4882a593Smuzhiyun #include <linux/slab.h>
9*4882a593Smuzhiyun #include <linux/etherdevice.h>
10*4882a593Smuzhiyun #include <asm/unaligned.h>
11*4882a593Smuzhiyun #include "wme.h"
12*4882a593Smuzhiyun #include "mesh.h"
13*4882a593Smuzhiyun
14*4882a593Smuzhiyun #define TEST_FRAME_LEN 8192
15*4882a593Smuzhiyun #define MAX_METRIC 0xffffffff
16*4882a593Smuzhiyun #define ARITH_SHIFT 8
17*4882a593Smuzhiyun #define LINK_FAIL_THRESH 95
18*4882a593Smuzhiyun
19*4882a593Smuzhiyun #define MAX_PREQ_QUEUE_LEN 64
20*4882a593Smuzhiyun
21*4882a593Smuzhiyun static void mesh_queue_preq(struct mesh_path *, u8);
22*4882a593Smuzhiyun
u32_field_get(const u8 * preq_elem,int offset,bool ae)23*4882a593Smuzhiyun static inline u32 u32_field_get(const u8 *preq_elem, int offset, bool ae)
24*4882a593Smuzhiyun {
25*4882a593Smuzhiyun if (ae)
26*4882a593Smuzhiyun offset += 6;
27*4882a593Smuzhiyun return get_unaligned_le32(preq_elem + offset);
28*4882a593Smuzhiyun }
29*4882a593Smuzhiyun
u16_field_get(const u8 * preq_elem,int offset,bool ae)30*4882a593Smuzhiyun static inline u16 u16_field_get(const u8 *preq_elem, int offset, bool ae)
31*4882a593Smuzhiyun {
32*4882a593Smuzhiyun if (ae)
33*4882a593Smuzhiyun offset += 6;
34*4882a593Smuzhiyun return get_unaligned_le16(preq_elem + offset);
35*4882a593Smuzhiyun }
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun /* HWMP IE processing macros */
38*4882a593Smuzhiyun #define AE_F (1<<6)
39*4882a593Smuzhiyun #define AE_F_SET(x) (*x & AE_F)
40*4882a593Smuzhiyun #define PREQ_IE_FLAGS(x) (*(x))
41*4882a593Smuzhiyun #define PREQ_IE_HOPCOUNT(x) (*(x + 1))
42*4882a593Smuzhiyun #define PREQ_IE_TTL(x) (*(x + 2))
43*4882a593Smuzhiyun #define PREQ_IE_PREQ_ID(x) u32_field_get(x, 3, 0)
44*4882a593Smuzhiyun #define PREQ_IE_ORIG_ADDR(x) (x + 7)
45*4882a593Smuzhiyun #define PREQ_IE_ORIG_SN(x) u32_field_get(x, 13, 0)
46*4882a593Smuzhiyun #define PREQ_IE_LIFETIME(x) u32_field_get(x, 17, AE_F_SET(x))
47*4882a593Smuzhiyun #define PREQ_IE_METRIC(x) u32_field_get(x, 21, AE_F_SET(x))
48*4882a593Smuzhiyun #define PREQ_IE_TARGET_F(x) (*(AE_F_SET(x) ? x + 32 : x + 26))
49*4882a593Smuzhiyun #define PREQ_IE_TARGET_ADDR(x) (AE_F_SET(x) ? x + 33 : x + 27)
50*4882a593Smuzhiyun #define PREQ_IE_TARGET_SN(x) u32_field_get(x, 33, AE_F_SET(x))
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun #define PREP_IE_FLAGS(x) PREQ_IE_FLAGS(x)
54*4882a593Smuzhiyun #define PREP_IE_HOPCOUNT(x) PREQ_IE_HOPCOUNT(x)
55*4882a593Smuzhiyun #define PREP_IE_TTL(x) PREQ_IE_TTL(x)
56*4882a593Smuzhiyun #define PREP_IE_ORIG_ADDR(x) (AE_F_SET(x) ? x + 27 : x + 21)
57*4882a593Smuzhiyun #define PREP_IE_ORIG_SN(x) u32_field_get(x, 27, AE_F_SET(x))
58*4882a593Smuzhiyun #define PREP_IE_LIFETIME(x) u32_field_get(x, 13, AE_F_SET(x))
59*4882a593Smuzhiyun #define PREP_IE_METRIC(x) u32_field_get(x, 17, AE_F_SET(x))
60*4882a593Smuzhiyun #define PREP_IE_TARGET_ADDR(x) (x + 3)
61*4882a593Smuzhiyun #define PREP_IE_TARGET_SN(x) u32_field_get(x, 9, 0)
62*4882a593Smuzhiyun
63*4882a593Smuzhiyun #define PERR_IE_TTL(x) (*(x))
64*4882a593Smuzhiyun #define PERR_IE_TARGET_FLAGS(x) (*(x + 2))
65*4882a593Smuzhiyun #define PERR_IE_TARGET_ADDR(x) (x + 3)
66*4882a593Smuzhiyun #define PERR_IE_TARGET_SN(x) u32_field_get(x, 9, 0)
67*4882a593Smuzhiyun #define PERR_IE_TARGET_RCODE(x) u16_field_get(x, 13, 0)
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun #define MSEC_TO_TU(x) (x*1000/1024)
70*4882a593Smuzhiyun #define SN_GT(x, y) ((s32)(y - x) < 0)
71*4882a593Smuzhiyun #define SN_LT(x, y) ((s32)(x - y) < 0)
72*4882a593Smuzhiyun #define MAX_SANE_SN_DELTA 32
73*4882a593Smuzhiyun
SN_DELTA(u32 x,u32 y)74*4882a593Smuzhiyun static inline u32 SN_DELTA(u32 x, u32 y)
75*4882a593Smuzhiyun {
76*4882a593Smuzhiyun return x >= y ? x - y : y - x;
77*4882a593Smuzhiyun }
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun #define net_traversal_jiffies(s) \
80*4882a593Smuzhiyun msecs_to_jiffies(s->u.mesh.mshcfg.dot11MeshHWMPnetDiameterTraversalTime)
81*4882a593Smuzhiyun #define default_lifetime(s) \
82*4882a593Smuzhiyun MSEC_TO_TU(s->u.mesh.mshcfg.dot11MeshHWMPactivePathTimeout)
83*4882a593Smuzhiyun #define min_preq_int_jiff(s) \
84*4882a593Smuzhiyun (msecs_to_jiffies(s->u.mesh.mshcfg.dot11MeshHWMPpreqMinInterval))
85*4882a593Smuzhiyun #define max_preq_retries(s) (s->u.mesh.mshcfg.dot11MeshHWMPmaxPREQretries)
86*4882a593Smuzhiyun #define disc_timeout_jiff(s) \
87*4882a593Smuzhiyun msecs_to_jiffies(sdata->u.mesh.mshcfg.min_discovery_timeout)
88*4882a593Smuzhiyun #define root_path_confirmation_jiffies(s) \
89*4882a593Smuzhiyun msecs_to_jiffies(sdata->u.mesh.mshcfg.dot11MeshHWMPconfirmationInterval)
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun enum mpath_frame_type {
92*4882a593Smuzhiyun MPATH_PREQ = 0,
93*4882a593Smuzhiyun MPATH_PREP,
94*4882a593Smuzhiyun MPATH_PERR,
95*4882a593Smuzhiyun MPATH_RANN
96*4882a593Smuzhiyun };
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun static const u8 broadcast_addr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
99*4882a593Smuzhiyun
mesh_path_sel_frame_tx(enum mpath_frame_type action,u8 flags,const u8 * orig_addr,u32 orig_sn,u8 target_flags,const u8 * target,u32 target_sn,const u8 * da,u8 hop_count,u8 ttl,u32 lifetime,u32 metric,u32 preq_id,struct ieee80211_sub_if_data * sdata)100*4882a593Smuzhiyun static int mesh_path_sel_frame_tx(enum mpath_frame_type action, u8 flags,
101*4882a593Smuzhiyun const u8 *orig_addr, u32 orig_sn,
102*4882a593Smuzhiyun u8 target_flags, const u8 *target,
103*4882a593Smuzhiyun u32 target_sn, const u8 *da,
104*4882a593Smuzhiyun u8 hop_count, u8 ttl,
105*4882a593Smuzhiyun u32 lifetime, u32 metric, u32 preq_id,
106*4882a593Smuzhiyun struct ieee80211_sub_if_data *sdata)
107*4882a593Smuzhiyun {
108*4882a593Smuzhiyun struct ieee80211_local *local = sdata->local;
109*4882a593Smuzhiyun struct sk_buff *skb;
110*4882a593Smuzhiyun struct ieee80211_mgmt *mgmt;
111*4882a593Smuzhiyun u8 *pos, ie_len;
112*4882a593Smuzhiyun int hdr_len = offsetofend(struct ieee80211_mgmt,
113*4882a593Smuzhiyun u.action.u.mesh_action);
114*4882a593Smuzhiyun
115*4882a593Smuzhiyun skb = dev_alloc_skb(local->tx_headroom +
116*4882a593Smuzhiyun hdr_len +
117*4882a593Smuzhiyun 2 + 37); /* max HWMP IE */
118*4882a593Smuzhiyun if (!skb)
119*4882a593Smuzhiyun return -1;
120*4882a593Smuzhiyun skb_reserve(skb, local->tx_headroom);
121*4882a593Smuzhiyun mgmt = skb_put_zero(skb, hdr_len);
122*4882a593Smuzhiyun mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
123*4882a593Smuzhiyun IEEE80211_STYPE_ACTION);
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun memcpy(mgmt->da, da, ETH_ALEN);
126*4882a593Smuzhiyun memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
127*4882a593Smuzhiyun /* BSSID == SA */
128*4882a593Smuzhiyun memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
129*4882a593Smuzhiyun mgmt->u.action.category = WLAN_CATEGORY_MESH_ACTION;
130*4882a593Smuzhiyun mgmt->u.action.u.mesh_action.action_code =
131*4882a593Smuzhiyun WLAN_MESH_ACTION_HWMP_PATH_SELECTION;
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun switch (action) {
134*4882a593Smuzhiyun case MPATH_PREQ:
135*4882a593Smuzhiyun mhwmp_dbg(sdata, "sending PREQ to %pM\n", target);
136*4882a593Smuzhiyun ie_len = 37;
137*4882a593Smuzhiyun pos = skb_put(skb, 2 + ie_len);
138*4882a593Smuzhiyun *pos++ = WLAN_EID_PREQ;
139*4882a593Smuzhiyun break;
140*4882a593Smuzhiyun case MPATH_PREP:
141*4882a593Smuzhiyun mhwmp_dbg(sdata, "sending PREP to %pM\n", orig_addr);
142*4882a593Smuzhiyun ie_len = 31;
143*4882a593Smuzhiyun pos = skb_put(skb, 2 + ie_len);
144*4882a593Smuzhiyun *pos++ = WLAN_EID_PREP;
145*4882a593Smuzhiyun break;
146*4882a593Smuzhiyun case MPATH_RANN:
147*4882a593Smuzhiyun mhwmp_dbg(sdata, "sending RANN from %pM\n", orig_addr);
148*4882a593Smuzhiyun ie_len = sizeof(struct ieee80211_rann_ie);
149*4882a593Smuzhiyun pos = skb_put(skb, 2 + ie_len);
150*4882a593Smuzhiyun *pos++ = WLAN_EID_RANN;
151*4882a593Smuzhiyun break;
152*4882a593Smuzhiyun default:
153*4882a593Smuzhiyun kfree_skb(skb);
154*4882a593Smuzhiyun return -ENOTSUPP;
155*4882a593Smuzhiyun }
156*4882a593Smuzhiyun *pos++ = ie_len;
157*4882a593Smuzhiyun *pos++ = flags;
158*4882a593Smuzhiyun *pos++ = hop_count;
159*4882a593Smuzhiyun *pos++ = ttl;
160*4882a593Smuzhiyun if (action == MPATH_PREP) {
161*4882a593Smuzhiyun memcpy(pos, target, ETH_ALEN);
162*4882a593Smuzhiyun pos += ETH_ALEN;
163*4882a593Smuzhiyun put_unaligned_le32(target_sn, pos);
164*4882a593Smuzhiyun pos += 4;
165*4882a593Smuzhiyun } else {
166*4882a593Smuzhiyun if (action == MPATH_PREQ) {
167*4882a593Smuzhiyun put_unaligned_le32(preq_id, pos);
168*4882a593Smuzhiyun pos += 4;
169*4882a593Smuzhiyun }
170*4882a593Smuzhiyun memcpy(pos, orig_addr, ETH_ALEN);
171*4882a593Smuzhiyun pos += ETH_ALEN;
172*4882a593Smuzhiyun put_unaligned_le32(orig_sn, pos);
173*4882a593Smuzhiyun pos += 4;
174*4882a593Smuzhiyun }
175*4882a593Smuzhiyun put_unaligned_le32(lifetime, pos); /* interval for RANN */
176*4882a593Smuzhiyun pos += 4;
177*4882a593Smuzhiyun put_unaligned_le32(metric, pos);
178*4882a593Smuzhiyun pos += 4;
179*4882a593Smuzhiyun if (action == MPATH_PREQ) {
180*4882a593Smuzhiyun *pos++ = 1; /* destination count */
181*4882a593Smuzhiyun *pos++ = target_flags;
182*4882a593Smuzhiyun memcpy(pos, target, ETH_ALEN);
183*4882a593Smuzhiyun pos += ETH_ALEN;
184*4882a593Smuzhiyun put_unaligned_le32(target_sn, pos);
185*4882a593Smuzhiyun pos += 4;
186*4882a593Smuzhiyun } else if (action == MPATH_PREP) {
187*4882a593Smuzhiyun memcpy(pos, orig_addr, ETH_ALEN);
188*4882a593Smuzhiyun pos += ETH_ALEN;
189*4882a593Smuzhiyun put_unaligned_le32(orig_sn, pos);
190*4882a593Smuzhiyun pos += 4;
191*4882a593Smuzhiyun }
192*4882a593Smuzhiyun
193*4882a593Smuzhiyun ieee80211_tx_skb(sdata, skb);
194*4882a593Smuzhiyun return 0;
195*4882a593Smuzhiyun }
196*4882a593Smuzhiyun
197*4882a593Smuzhiyun
198*4882a593Smuzhiyun /* Headroom is not adjusted. Caller should ensure that skb has sufficient
199*4882a593Smuzhiyun * headroom in case the frame is encrypted. */
prepare_frame_for_deferred_tx(struct ieee80211_sub_if_data * sdata,struct sk_buff * skb)200*4882a593Smuzhiyun static void prepare_frame_for_deferred_tx(struct ieee80211_sub_if_data *sdata,
201*4882a593Smuzhiyun struct sk_buff *skb)
202*4882a593Smuzhiyun {
203*4882a593Smuzhiyun struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
204*4882a593Smuzhiyun struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
205*4882a593Smuzhiyun
206*4882a593Smuzhiyun skb_reset_mac_header(skb);
207*4882a593Smuzhiyun skb_reset_network_header(skb);
208*4882a593Smuzhiyun skb_reset_transport_header(skb);
209*4882a593Smuzhiyun
210*4882a593Smuzhiyun /* Send all internal mgmt frames on VO. Accordingly set TID to 7. */
211*4882a593Smuzhiyun skb_set_queue_mapping(skb, IEEE80211_AC_VO);
212*4882a593Smuzhiyun skb->priority = 7;
213*4882a593Smuzhiyun
214*4882a593Smuzhiyun info->control.vif = &sdata->vif;
215*4882a593Smuzhiyun info->control.flags |= IEEE80211_TX_INTCFL_NEED_TXPROCESSING;
216*4882a593Smuzhiyun ieee80211_set_qos_hdr(sdata, skb);
217*4882a593Smuzhiyun ieee80211_mps_set_frame_flags(sdata, NULL, hdr);
218*4882a593Smuzhiyun }
219*4882a593Smuzhiyun
220*4882a593Smuzhiyun /**
221*4882a593Smuzhiyun * mesh_path_error_tx - Sends a PERR mesh management frame
222*4882a593Smuzhiyun *
223*4882a593Smuzhiyun * @ttl: allowed remaining hops
224*4882a593Smuzhiyun * @target: broken destination
225*4882a593Smuzhiyun * @target_sn: SN of the broken destination
226*4882a593Smuzhiyun * @target_rcode: reason code for this PERR
227*4882a593Smuzhiyun * @ra: node this frame is addressed to
228*4882a593Smuzhiyun * @sdata: local mesh subif
229*4882a593Smuzhiyun *
230*4882a593Smuzhiyun * Note: This function may be called with driver locks taken that the driver
231*4882a593Smuzhiyun * also acquires in the TX path. To avoid a deadlock we don't transmit the
232*4882a593Smuzhiyun * frame directly but add it to the pending queue instead.
233*4882a593Smuzhiyun */
mesh_path_error_tx(struct ieee80211_sub_if_data * sdata,u8 ttl,const u8 * target,u32 target_sn,u16 target_rcode,const u8 * ra)234*4882a593Smuzhiyun int mesh_path_error_tx(struct ieee80211_sub_if_data *sdata,
235*4882a593Smuzhiyun u8 ttl, const u8 *target, u32 target_sn,
236*4882a593Smuzhiyun u16 target_rcode, const u8 *ra)
237*4882a593Smuzhiyun {
238*4882a593Smuzhiyun struct ieee80211_local *local = sdata->local;
239*4882a593Smuzhiyun struct sk_buff *skb;
240*4882a593Smuzhiyun struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
241*4882a593Smuzhiyun struct ieee80211_mgmt *mgmt;
242*4882a593Smuzhiyun u8 *pos, ie_len;
243*4882a593Smuzhiyun int hdr_len = offsetofend(struct ieee80211_mgmt,
244*4882a593Smuzhiyun u.action.u.mesh_action);
245*4882a593Smuzhiyun
246*4882a593Smuzhiyun if (time_before(jiffies, ifmsh->next_perr))
247*4882a593Smuzhiyun return -EAGAIN;
248*4882a593Smuzhiyun
249*4882a593Smuzhiyun skb = dev_alloc_skb(local->tx_headroom +
250*4882a593Smuzhiyun sdata->encrypt_headroom +
251*4882a593Smuzhiyun IEEE80211_ENCRYPT_TAILROOM +
252*4882a593Smuzhiyun hdr_len +
253*4882a593Smuzhiyun 2 + 15 /* PERR IE */);
254*4882a593Smuzhiyun if (!skb)
255*4882a593Smuzhiyun return -1;
256*4882a593Smuzhiyun skb_reserve(skb, local->tx_headroom + sdata->encrypt_headroom);
257*4882a593Smuzhiyun mgmt = skb_put_zero(skb, hdr_len);
258*4882a593Smuzhiyun mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
259*4882a593Smuzhiyun IEEE80211_STYPE_ACTION);
260*4882a593Smuzhiyun
261*4882a593Smuzhiyun memcpy(mgmt->da, ra, ETH_ALEN);
262*4882a593Smuzhiyun memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
263*4882a593Smuzhiyun /* BSSID == SA */
264*4882a593Smuzhiyun memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
265*4882a593Smuzhiyun mgmt->u.action.category = WLAN_CATEGORY_MESH_ACTION;
266*4882a593Smuzhiyun mgmt->u.action.u.mesh_action.action_code =
267*4882a593Smuzhiyun WLAN_MESH_ACTION_HWMP_PATH_SELECTION;
268*4882a593Smuzhiyun ie_len = 15;
269*4882a593Smuzhiyun pos = skb_put(skb, 2 + ie_len);
270*4882a593Smuzhiyun *pos++ = WLAN_EID_PERR;
271*4882a593Smuzhiyun *pos++ = ie_len;
272*4882a593Smuzhiyun /* ttl */
273*4882a593Smuzhiyun *pos++ = ttl;
274*4882a593Smuzhiyun /* number of destinations */
275*4882a593Smuzhiyun *pos++ = 1;
276*4882a593Smuzhiyun /* Flags field has AE bit only as defined in
277*4882a593Smuzhiyun * sec 8.4.2.117 IEEE802.11-2012
278*4882a593Smuzhiyun */
279*4882a593Smuzhiyun *pos = 0;
280*4882a593Smuzhiyun pos++;
281*4882a593Smuzhiyun memcpy(pos, target, ETH_ALEN);
282*4882a593Smuzhiyun pos += ETH_ALEN;
283*4882a593Smuzhiyun put_unaligned_le32(target_sn, pos);
284*4882a593Smuzhiyun pos += 4;
285*4882a593Smuzhiyun put_unaligned_le16(target_rcode, pos);
286*4882a593Smuzhiyun
287*4882a593Smuzhiyun /* see note in function header */
288*4882a593Smuzhiyun prepare_frame_for_deferred_tx(sdata, skb);
289*4882a593Smuzhiyun ifmsh->next_perr = TU_TO_EXP_TIME(
290*4882a593Smuzhiyun ifmsh->mshcfg.dot11MeshHWMPperrMinInterval);
291*4882a593Smuzhiyun ieee80211_add_pending_skb(local, skb);
292*4882a593Smuzhiyun return 0;
293*4882a593Smuzhiyun }
294*4882a593Smuzhiyun
ieee80211s_update_metric(struct ieee80211_local * local,struct sta_info * sta,struct ieee80211_tx_status * st)295*4882a593Smuzhiyun void ieee80211s_update_metric(struct ieee80211_local *local,
296*4882a593Smuzhiyun struct sta_info *sta,
297*4882a593Smuzhiyun struct ieee80211_tx_status *st)
298*4882a593Smuzhiyun {
299*4882a593Smuzhiyun struct ieee80211_tx_info *txinfo = st->info;
300*4882a593Smuzhiyun int failed;
301*4882a593Smuzhiyun struct rate_info rinfo;
302*4882a593Smuzhiyun
303*4882a593Smuzhiyun failed = !(txinfo->flags & IEEE80211_TX_STAT_ACK);
304*4882a593Smuzhiyun
305*4882a593Smuzhiyun /* moving average, scaled to 100.
306*4882a593Smuzhiyun * feed failure as 100 and success as 0
307*4882a593Smuzhiyun */
308*4882a593Smuzhiyun ewma_mesh_fail_avg_add(&sta->mesh->fail_avg, failed * 100);
309*4882a593Smuzhiyun if (ewma_mesh_fail_avg_read(&sta->mesh->fail_avg) >
310*4882a593Smuzhiyun LINK_FAIL_THRESH)
311*4882a593Smuzhiyun mesh_plink_broken(sta);
312*4882a593Smuzhiyun
313*4882a593Smuzhiyun sta_set_rate_info_tx(sta, &sta->tx_stats.last_rate, &rinfo);
314*4882a593Smuzhiyun ewma_mesh_tx_rate_avg_add(&sta->mesh->tx_rate_avg,
315*4882a593Smuzhiyun cfg80211_calculate_bitrate(&rinfo));
316*4882a593Smuzhiyun }
317*4882a593Smuzhiyun
airtime_link_metric_get(struct ieee80211_local * local,struct sta_info * sta)318*4882a593Smuzhiyun u32 airtime_link_metric_get(struct ieee80211_local *local,
319*4882a593Smuzhiyun struct sta_info *sta)
320*4882a593Smuzhiyun {
321*4882a593Smuzhiyun /* This should be adjusted for each device */
322*4882a593Smuzhiyun int device_constant = 1 << ARITH_SHIFT;
323*4882a593Smuzhiyun int test_frame_len = TEST_FRAME_LEN << ARITH_SHIFT;
324*4882a593Smuzhiyun int s_unit = 1 << ARITH_SHIFT;
325*4882a593Smuzhiyun int rate, err;
326*4882a593Smuzhiyun u32 tx_time, estimated_retx;
327*4882a593Smuzhiyun u64 result;
328*4882a593Smuzhiyun unsigned long fail_avg =
329*4882a593Smuzhiyun ewma_mesh_fail_avg_read(&sta->mesh->fail_avg);
330*4882a593Smuzhiyun
331*4882a593Smuzhiyun if (sta->mesh->plink_state != NL80211_PLINK_ESTAB)
332*4882a593Smuzhiyun return MAX_METRIC;
333*4882a593Smuzhiyun
334*4882a593Smuzhiyun /* Try to get rate based on HW/SW RC algorithm.
335*4882a593Smuzhiyun * Rate is returned in units of Kbps, correct this
336*4882a593Smuzhiyun * to comply with airtime calculation units
337*4882a593Smuzhiyun * Round up in case we get rate < 100Kbps
338*4882a593Smuzhiyun */
339*4882a593Smuzhiyun rate = DIV_ROUND_UP(sta_get_expected_throughput(sta), 100);
340*4882a593Smuzhiyun
341*4882a593Smuzhiyun if (rate) {
342*4882a593Smuzhiyun err = 0;
343*4882a593Smuzhiyun } else {
344*4882a593Smuzhiyun if (fail_avg > LINK_FAIL_THRESH)
345*4882a593Smuzhiyun return MAX_METRIC;
346*4882a593Smuzhiyun
347*4882a593Smuzhiyun rate = ewma_mesh_tx_rate_avg_read(&sta->mesh->tx_rate_avg);
348*4882a593Smuzhiyun if (WARN_ON(!rate))
349*4882a593Smuzhiyun return MAX_METRIC;
350*4882a593Smuzhiyun
351*4882a593Smuzhiyun err = (fail_avg << ARITH_SHIFT) / 100;
352*4882a593Smuzhiyun }
353*4882a593Smuzhiyun
354*4882a593Smuzhiyun /* bitrate is in units of 100 Kbps, while we need rate in units of
355*4882a593Smuzhiyun * 1Mbps. This will be corrected on tx_time computation.
356*4882a593Smuzhiyun */
357*4882a593Smuzhiyun tx_time = (device_constant + 10 * test_frame_len / rate);
358*4882a593Smuzhiyun estimated_retx = ((1 << (2 * ARITH_SHIFT)) / (s_unit - err));
359*4882a593Smuzhiyun result = ((u64)tx_time * estimated_retx) >> (2 * ARITH_SHIFT);
360*4882a593Smuzhiyun return (u32)result;
361*4882a593Smuzhiyun }
362*4882a593Smuzhiyun
363*4882a593Smuzhiyun /**
364*4882a593Smuzhiyun * hwmp_route_info_get - Update routing info to originator and transmitter
365*4882a593Smuzhiyun *
366*4882a593Smuzhiyun * @sdata: local mesh subif
367*4882a593Smuzhiyun * @mgmt: mesh management frame
368*4882a593Smuzhiyun * @hwmp_ie: hwmp information element (PREP or PREQ)
369*4882a593Smuzhiyun * @action: type of hwmp ie
370*4882a593Smuzhiyun *
371*4882a593Smuzhiyun * This function updates the path routing information to the originator and the
372*4882a593Smuzhiyun * transmitter of a HWMP PREQ or PREP frame.
373*4882a593Smuzhiyun *
374*4882a593Smuzhiyun * Returns: metric to frame originator or 0 if the frame should not be further
375*4882a593Smuzhiyun * processed
376*4882a593Smuzhiyun *
377*4882a593Smuzhiyun * Notes: this function is the only place (besides user-provided info) where
378*4882a593Smuzhiyun * path routing information is updated.
379*4882a593Smuzhiyun */
hwmp_route_info_get(struct ieee80211_sub_if_data * sdata,struct ieee80211_mgmt * mgmt,const u8 * hwmp_ie,enum mpath_frame_type action)380*4882a593Smuzhiyun static u32 hwmp_route_info_get(struct ieee80211_sub_if_data *sdata,
381*4882a593Smuzhiyun struct ieee80211_mgmt *mgmt,
382*4882a593Smuzhiyun const u8 *hwmp_ie, enum mpath_frame_type action)
383*4882a593Smuzhiyun {
384*4882a593Smuzhiyun struct ieee80211_local *local = sdata->local;
385*4882a593Smuzhiyun struct mesh_path *mpath;
386*4882a593Smuzhiyun struct sta_info *sta;
387*4882a593Smuzhiyun bool fresh_info;
388*4882a593Smuzhiyun const u8 *orig_addr, *ta;
389*4882a593Smuzhiyun u32 orig_sn, orig_metric;
390*4882a593Smuzhiyun unsigned long orig_lifetime, exp_time;
391*4882a593Smuzhiyun u32 last_hop_metric, new_metric;
392*4882a593Smuzhiyun bool process = true;
393*4882a593Smuzhiyun u8 hopcount;
394*4882a593Smuzhiyun
395*4882a593Smuzhiyun rcu_read_lock();
396*4882a593Smuzhiyun sta = sta_info_get(sdata, mgmt->sa);
397*4882a593Smuzhiyun if (!sta) {
398*4882a593Smuzhiyun rcu_read_unlock();
399*4882a593Smuzhiyun return 0;
400*4882a593Smuzhiyun }
401*4882a593Smuzhiyun
402*4882a593Smuzhiyun last_hop_metric = airtime_link_metric_get(local, sta);
403*4882a593Smuzhiyun /* Update and check originator routing info */
404*4882a593Smuzhiyun fresh_info = true;
405*4882a593Smuzhiyun
406*4882a593Smuzhiyun switch (action) {
407*4882a593Smuzhiyun case MPATH_PREQ:
408*4882a593Smuzhiyun orig_addr = PREQ_IE_ORIG_ADDR(hwmp_ie);
409*4882a593Smuzhiyun orig_sn = PREQ_IE_ORIG_SN(hwmp_ie);
410*4882a593Smuzhiyun orig_lifetime = PREQ_IE_LIFETIME(hwmp_ie);
411*4882a593Smuzhiyun orig_metric = PREQ_IE_METRIC(hwmp_ie);
412*4882a593Smuzhiyun hopcount = PREQ_IE_HOPCOUNT(hwmp_ie) + 1;
413*4882a593Smuzhiyun break;
414*4882a593Smuzhiyun case MPATH_PREP:
415*4882a593Smuzhiyun /* Originator here refers to the MP that was the target in the
416*4882a593Smuzhiyun * Path Request. We divert from the nomenclature in the draft
417*4882a593Smuzhiyun * so that we can easily use a single function to gather path
418*4882a593Smuzhiyun * information from both PREQ and PREP frames.
419*4882a593Smuzhiyun */
420*4882a593Smuzhiyun orig_addr = PREP_IE_TARGET_ADDR(hwmp_ie);
421*4882a593Smuzhiyun orig_sn = PREP_IE_TARGET_SN(hwmp_ie);
422*4882a593Smuzhiyun orig_lifetime = PREP_IE_LIFETIME(hwmp_ie);
423*4882a593Smuzhiyun orig_metric = PREP_IE_METRIC(hwmp_ie);
424*4882a593Smuzhiyun hopcount = PREP_IE_HOPCOUNT(hwmp_ie) + 1;
425*4882a593Smuzhiyun break;
426*4882a593Smuzhiyun default:
427*4882a593Smuzhiyun rcu_read_unlock();
428*4882a593Smuzhiyun return 0;
429*4882a593Smuzhiyun }
430*4882a593Smuzhiyun new_metric = orig_metric + last_hop_metric;
431*4882a593Smuzhiyun if (new_metric < orig_metric)
432*4882a593Smuzhiyun new_metric = MAX_METRIC;
433*4882a593Smuzhiyun exp_time = TU_TO_EXP_TIME(orig_lifetime);
434*4882a593Smuzhiyun
435*4882a593Smuzhiyun if (ether_addr_equal(orig_addr, sdata->vif.addr)) {
436*4882a593Smuzhiyun /* This MP is the originator, we are not interested in this
437*4882a593Smuzhiyun * frame, except for updating transmitter's path info.
438*4882a593Smuzhiyun */
439*4882a593Smuzhiyun process = false;
440*4882a593Smuzhiyun fresh_info = false;
441*4882a593Smuzhiyun } else {
442*4882a593Smuzhiyun mpath = mesh_path_lookup(sdata, orig_addr);
443*4882a593Smuzhiyun if (mpath) {
444*4882a593Smuzhiyun spin_lock_bh(&mpath->state_lock);
445*4882a593Smuzhiyun if (mpath->flags & MESH_PATH_FIXED)
446*4882a593Smuzhiyun fresh_info = false;
447*4882a593Smuzhiyun else if ((mpath->flags & MESH_PATH_ACTIVE) &&
448*4882a593Smuzhiyun (mpath->flags & MESH_PATH_SN_VALID)) {
449*4882a593Smuzhiyun if (SN_GT(mpath->sn, orig_sn) ||
450*4882a593Smuzhiyun (mpath->sn == orig_sn &&
451*4882a593Smuzhiyun (rcu_access_pointer(mpath->next_hop) !=
452*4882a593Smuzhiyun sta ?
453*4882a593Smuzhiyun mult_frac(new_metric, 10, 9) :
454*4882a593Smuzhiyun new_metric) >= mpath->metric)) {
455*4882a593Smuzhiyun process = false;
456*4882a593Smuzhiyun fresh_info = false;
457*4882a593Smuzhiyun }
458*4882a593Smuzhiyun } else if (!(mpath->flags & MESH_PATH_ACTIVE)) {
459*4882a593Smuzhiyun bool have_sn, newer_sn, bounced;
460*4882a593Smuzhiyun
461*4882a593Smuzhiyun have_sn = mpath->flags & MESH_PATH_SN_VALID;
462*4882a593Smuzhiyun newer_sn = have_sn && SN_GT(orig_sn, mpath->sn);
463*4882a593Smuzhiyun bounced = have_sn &&
464*4882a593Smuzhiyun (SN_DELTA(orig_sn, mpath->sn) >
465*4882a593Smuzhiyun MAX_SANE_SN_DELTA);
466*4882a593Smuzhiyun
467*4882a593Smuzhiyun if (!have_sn || newer_sn) {
468*4882a593Smuzhiyun /* if SN is newer than what we had
469*4882a593Smuzhiyun * then we can take it */;
470*4882a593Smuzhiyun } else if (bounced) {
471*4882a593Smuzhiyun /* if SN is way different than what
472*4882a593Smuzhiyun * we had then assume the other side
473*4882a593Smuzhiyun * rebooted or restarted */;
474*4882a593Smuzhiyun } else {
475*4882a593Smuzhiyun process = false;
476*4882a593Smuzhiyun fresh_info = false;
477*4882a593Smuzhiyun }
478*4882a593Smuzhiyun }
479*4882a593Smuzhiyun } else {
480*4882a593Smuzhiyun mpath = mesh_path_add(sdata, orig_addr);
481*4882a593Smuzhiyun if (IS_ERR(mpath)) {
482*4882a593Smuzhiyun rcu_read_unlock();
483*4882a593Smuzhiyun return 0;
484*4882a593Smuzhiyun }
485*4882a593Smuzhiyun spin_lock_bh(&mpath->state_lock);
486*4882a593Smuzhiyun }
487*4882a593Smuzhiyun
488*4882a593Smuzhiyun if (fresh_info) {
489*4882a593Smuzhiyun if (rcu_access_pointer(mpath->next_hop) != sta)
490*4882a593Smuzhiyun mpath->path_change_count++;
491*4882a593Smuzhiyun mesh_path_assign_nexthop(mpath, sta);
492*4882a593Smuzhiyun mpath->flags |= MESH_PATH_SN_VALID;
493*4882a593Smuzhiyun mpath->metric = new_metric;
494*4882a593Smuzhiyun mpath->sn = orig_sn;
495*4882a593Smuzhiyun mpath->exp_time = time_after(mpath->exp_time, exp_time)
496*4882a593Smuzhiyun ? mpath->exp_time : exp_time;
497*4882a593Smuzhiyun mpath->hop_count = hopcount;
498*4882a593Smuzhiyun mesh_path_activate(mpath);
499*4882a593Smuzhiyun spin_unlock_bh(&mpath->state_lock);
500*4882a593Smuzhiyun ewma_mesh_fail_avg_init(&sta->mesh->fail_avg);
501*4882a593Smuzhiyun /* init it at a low value - 0 start is tricky */
502*4882a593Smuzhiyun ewma_mesh_fail_avg_add(&sta->mesh->fail_avg, 1);
503*4882a593Smuzhiyun mesh_path_tx_pending(mpath);
504*4882a593Smuzhiyun /* draft says preq_id should be saved to, but there does
505*4882a593Smuzhiyun * not seem to be any use for it, skipping by now
506*4882a593Smuzhiyun */
507*4882a593Smuzhiyun } else
508*4882a593Smuzhiyun spin_unlock_bh(&mpath->state_lock);
509*4882a593Smuzhiyun }
510*4882a593Smuzhiyun
511*4882a593Smuzhiyun /* Update and check transmitter routing info */
512*4882a593Smuzhiyun ta = mgmt->sa;
513*4882a593Smuzhiyun if (ether_addr_equal(orig_addr, ta))
514*4882a593Smuzhiyun fresh_info = false;
515*4882a593Smuzhiyun else {
516*4882a593Smuzhiyun fresh_info = true;
517*4882a593Smuzhiyun
518*4882a593Smuzhiyun mpath = mesh_path_lookup(sdata, ta);
519*4882a593Smuzhiyun if (mpath) {
520*4882a593Smuzhiyun spin_lock_bh(&mpath->state_lock);
521*4882a593Smuzhiyun if ((mpath->flags & MESH_PATH_FIXED) ||
522*4882a593Smuzhiyun ((mpath->flags & MESH_PATH_ACTIVE) &&
523*4882a593Smuzhiyun ((rcu_access_pointer(mpath->next_hop) != sta ?
524*4882a593Smuzhiyun mult_frac(last_hop_metric, 10, 9) :
525*4882a593Smuzhiyun last_hop_metric) > mpath->metric)))
526*4882a593Smuzhiyun fresh_info = false;
527*4882a593Smuzhiyun } else {
528*4882a593Smuzhiyun mpath = mesh_path_add(sdata, ta);
529*4882a593Smuzhiyun if (IS_ERR(mpath)) {
530*4882a593Smuzhiyun rcu_read_unlock();
531*4882a593Smuzhiyun return 0;
532*4882a593Smuzhiyun }
533*4882a593Smuzhiyun spin_lock_bh(&mpath->state_lock);
534*4882a593Smuzhiyun }
535*4882a593Smuzhiyun
536*4882a593Smuzhiyun if (fresh_info) {
537*4882a593Smuzhiyun if (rcu_access_pointer(mpath->next_hop) != sta)
538*4882a593Smuzhiyun mpath->path_change_count++;
539*4882a593Smuzhiyun mesh_path_assign_nexthop(mpath, sta);
540*4882a593Smuzhiyun mpath->metric = last_hop_metric;
541*4882a593Smuzhiyun mpath->exp_time = time_after(mpath->exp_time, exp_time)
542*4882a593Smuzhiyun ? mpath->exp_time : exp_time;
543*4882a593Smuzhiyun mpath->hop_count = 1;
544*4882a593Smuzhiyun mesh_path_activate(mpath);
545*4882a593Smuzhiyun spin_unlock_bh(&mpath->state_lock);
546*4882a593Smuzhiyun ewma_mesh_fail_avg_init(&sta->mesh->fail_avg);
547*4882a593Smuzhiyun /* init it at a low value - 0 start is tricky */
548*4882a593Smuzhiyun ewma_mesh_fail_avg_add(&sta->mesh->fail_avg, 1);
549*4882a593Smuzhiyun mesh_path_tx_pending(mpath);
550*4882a593Smuzhiyun } else
551*4882a593Smuzhiyun spin_unlock_bh(&mpath->state_lock);
552*4882a593Smuzhiyun }
553*4882a593Smuzhiyun
554*4882a593Smuzhiyun rcu_read_unlock();
555*4882a593Smuzhiyun
556*4882a593Smuzhiyun return process ? new_metric : 0;
557*4882a593Smuzhiyun }
558*4882a593Smuzhiyun
hwmp_preq_frame_process(struct ieee80211_sub_if_data * sdata,struct ieee80211_mgmt * mgmt,const u8 * preq_elem,u32 orig_metric)559*4882a593Smuzhiyun static void hwmp_preq_frame_process(struct ieee80211_sub_if_data *sdata,
560*4882a593Smuzhiyun struct ieee80211_mgmt *mgmt,
561*4882a593Smuzhiyun const u8 *preq_elem, u32 orig_metric)
562*4882a593Smuzhiyun {
563*4882a593Smuzhiyun struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
564*4882a593Smuzhiyun struct mesh_path *mpath = NULL;
565*4882a593Smuzhiyun const u8 *target_addr, *orig_addr;
566*4882a593Smuzhiyun const u8 *da;
567*4882a593Smuzhiyun u8 target_flags, ttl, flags;
568*4882a593Smuzhiyun u32 orig_sn, target_sn, lifetime, target_metric = 0;
569*4882a593Smuzhiyun bool reply = false;
570*4882a593Smuzhiyun bool forward = true;
571*4882a593Smuzhiyun bool root_is_gate;
572*4882a593Smuzhiyun
573*4882a593Smuzhiyun /* Update target SN, if present */
574*4882a593Smuzhiyun target_addr = PREQ_IE_TARGET_ADDR(preq_elem);
575*4882a593Smuzhiyun orig_addr = PREQ_IE_ORIG_ADDR(preq_elem);
576*4882a593Smuzhiyun target_sn = PREQ_IE_TARGET_SN(preq_elem);
577*4882a593Smuzhiyun orig_sn = PREQ_IE_ORIG_SN(preq_elem);
578*4882a593Smuzhiyun target_flags = PREQ_IE_TARGET_F(preq_elem);
579*4882a593Smuzhiyun /* Proactive PREQ gate announcements */
580*4882a593Smuzhiyun flags = PREQ_IE_FLAGS(preq_elem);
581*4882a593Smuzhiyun root_is_gate = !!(flags & RANN_FLAG_IS_GATE);
582*4882a593Smuzhiyun
583*4882a593Smuzhiyun mhwmp_dbg(sdata, "received PREQ from %pM\n", orig_addr);
584*4882a593Smuzhiyun
585*4882a593Smuzhiyun if (ether_addr_equal(target_addr, sdata->vif.addr)) {
586*4882a593Smuzhiyun mhwmp_dbg(sdata, "PREQ is for us\n");
587*4882a593Smuzhiyun forward = false;
588*4882a593Smuzhiyun reply = true;
589*4882a593Smuzhiyun target_metric = 0;
590*4882a593Smuzhiyun
591*4882a593Smuzhiyun if (SN_GT(target_sn, ifmsh->sn))
592*4882a593Smuzhiyun ifmsh->sn = target_sn;
593*4882a593Smuzhiyun
594*4882a593Smuzhiyun if (time_after(jiffies, ifmsh->last_sn_update +
595*4882a593Smuzhiyun net_traversal_jiffies(sdata)) ||
596*4882a593Smuzhiyun time_before(jiffies, ifmsh->last_sn_update)) {
597*4882a593Smuzhiyun ++ifmsh->sn;
598*4882a593Smuzhiyun ifmsh->last_sn_update = jiffies;
599*4882a593Smuzhiyun }
600*4882a593Smuzhiyun target_sn = ifmsh->sn;
601*4882a593Smuzhiyun } else if (is_broadcast_ether_addr(target_addr) &&
602*4882a593Smuzhiyun (target_flags & IEEE80211_PREQ_TO_FLAG)) {
603*4882a593Smuzhiyun rcu_read_lock();
604*4882a593Smuzhiyun mpath = mesh_path_lookup(sdata, orig_addr);
605*4882a593Smuzhiyun if (mpath) {
606*4882a593Smuzhiyun if (flags & IEEE80211_PREQ_PROACTIVE_PREP_FLAG) {
607*4882a593Smuzhiyun reply = true;
608*4882a593Smuzhiyun target_addr = sdata->vif.addr;
609*4882a593Smuzhiyun target_sn = ++ifmsh->sn;
610*4882a593Smuzhiyun target_metric = 0;
611*4882a593Smuzhiyun ifmsh->last_sn_update = jiffies;
612*4882a593Smuzhiyun }
613*4882a593Smuzhiyun if (root_is_gate)
614*4882a593Smuzhiyun mesh_path_add_gate(mpath);
615*4882a593Smuzhiyun }
616*4882a593Smuzhiyun rcu_read_unlock();
617*4882a593Smuzhiyun } else {
618*4882a593Smuzhiyun rcu_read_lock();
619*4882a593Smuzhiyun mpath = mesh_path_lookup(sdata, target_addr);
620*4882a593Smuzhiyun if (mpath) {
621*4882a593Smuzhiyun if ((!(mpath->flags & MESH_PATH_SN_VALID)) ||
622*4882a593Smuzhiyun SN_LT(mpath->sn, target_sn)) {
623*4882a593Smuzhiyun mpath->sn = target_sn;
624*4882a593Smuzhiyun mpath->flags |= MESH_PATH_SN_VALID;
625*4882a593Smuzhiyun } else if ((!(target_flags & IEEE80211_PREQ_TO_FLAG)) &&
626*4882a593Smuzhiyun (mpath->flags & MESH_PATH_ACTIVE)) {
627*4882a593Smuzhiyun reply = true;
628*4882a593Smuzhiyun target_metric = mpath->metric;
629*4882a593Smuzhiyun target_sn = mpath->sn;
630*4882a593Smuzhiyun /* Case E2 of sec 13.10.9.3 IEEE 802.11-2012*/
631*4882a593Smuzhiyun target_flags |= IEEE80211_PREQ_TO_FLAG;
632*4882a593Smuzhiyun }
633*4882a593Smuzhiyun }
634*4882a593Smuzhiyun rcu_read_unlock();
635*4882a593Smuzhiyun }
636*4882a593Smuzhiyun
637*4882a593Smuzhiyun if (reply) {
638*4882a593Smuzhiyun lifetime = PREQ_IE_LIFETIME(preq_elem);
639*4882a593Smuzhiyun ttl = ifmsh->mshcfg.element_ttl;
640*4882a593Smuzhiyun if (ttl != 0) {
641*4882a593Smuzhiyun mhwmp_dbg(sdata, "replying to the PREQ\n");
642*4882a593Smuzhiyun mesh_path_sel_frame_tx(MPATH_PREP, 0, orig_addr,
643*4882a593Smuzhiyun orig_sn, 0, target_addr,
644*4882a593Smuzhiyun target_sn, mgmt->sa, 0, ttl,
645*4882a593Smuzhiyun lifetime, target_metric, 0,
646*4882a593Smuzhiyun sdata);
647*4882a593Smuzhiyun } else {
648*4882a593Smuzhiyun ifmsh->mshstats.dropped_frames_ttl++;
649*4882a593Smuzhiyun }
650*4882a593Smuzhiyun }
651*4882a593Smuzhiyun
652*4882a593Smuzhiyun if (forward && ifmsh->mshcfg.dot11MeshForwarding) {
653*4882a593Smuzhiyun u32 preq_id;
654*4882a593Smuzhiyun u8 hopcount;
655*4882a593Smuzhiyun
656*4882a593Smuzhiyun ttl = PREQ_IE_TTL(preq_elem);
657*4882a593Smuzhiyun lifetime = PREQ_IE_LIFETIME(preq_elem);
658*4882a593Smuzhiyun if (ttl <= 1) {
659*4882a593Smuzhiyun ifmsh->mshstats.dropped_frames_ttl++;
660*4882a593Smuzhiyun return;
661*4882a593Smuzhiyun }
662*4882a593Smuzhiyun mhwmp_dbg(sdata, "forwarding the PREQ from %pM\n", orig_addr);
663*4882a593Smuzhiyun --ttl;
664*4882a593Smuzhiyun preq_id = PREQ_IE_PREQ_ID(preq_elem);
665*4882a593Smuzhiyun hopcount = PREQ_IE_HOPCOUNT(preq_elem) + 1;
666*4882a593Smuzhiyun da = (mpath && mpath->is_root) ?
667*4882a593Smuzhiyun mpath->rann_snd_addr : broadcast_addr;
668*4882a593Smuzhiyun
669*4882a593Smuzhiyun if (flags & IEEE80211_PREQ_PROACTIVE_PREP_FLAG) {
670*4882a593Smuzhiyun target_addr = PREQ_IE_TARGET_ADDR(preq_elem);
671*4882a593Smuzhiyun target_sn = PREQ_IE_TARGET_SN(preq_elem);
672*4882a593Smuzhiyun }
673*4882a593Smuzhiyun
674*4882a593Smuzhiyun mesh_path_sel_frame_tx(MPATH_PREQ, flags, orig_addr,
675*4882a593Smuzhiyun orig_sn, target_flags, target_addr,
676*4882a593Smuzhiyun target_sn, da, hopcount, ttl, lifetime,
677*4882a593Smuzhiyun orig_metric, preq_id, sdata);
678*4882a593Smuzhiyun if (!is_multicast_ether_addr(da))
679*4882a593Smuzhiyun ifmsh->mshstats.fwded_unicast++;
680*4882a593Smuzhiyun else
681*4882a593Smuzhiyun ifmsh->mshstats.fwded_mcast++;
682*4882a593Smuzhiyun ifmsh->mshstats.fwded_frames++;
683*4882a593Smuzhiyun }
684*4882a593Smuzhiyun }
685*4882a593Smuzhiyun
686*4882a593Smuzhiyun
687*4882a593Smuzhiyun static inline struct sta_info *
next_hop_deref_protected(struct mesh_path * mpath)688*4882a593Smuzhiyun next_hop_deref_protected(struct mesh_path *mpath)
689*4882a593Smuzhiyun {
690*4882a593Smuzhiyun return rcu_dereference_protected(mpath->next_hop,
691*4882a593Smuzhiyun lockdep_is_held(&mpath->state_lock));
692*4882a593Smuzhiyun }
693*4882a593Smuzhiyun
694*4882a593Smuzhiyun
hwmp_prep_frame_process(struct ieee80211_sub_if_data * sdata,struct ieee80211_mgmt * mgmt,const u8 * prep_elem,u32 metric)695*4882a593Smuzhiyun static void hwmp_prep_frame_process(struct ieee80211_sub_if_data *sdata,
696*4882a593Smuzhiyun struct ieee80211_mgmt *mgmt,
697*4882a593Smuzhiyun const u8 *prep_elem, u32 metric)
698*4882a593Smuzhiyun {
699*4882a593Smuzhiyun struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
700*4882a593Smuzhiyun struct mesh_path *mpath;
701*4882a593Smuzhiyun const u8 *target_addr, *orig_addr;
702*4882a593Smuzhiyun u8 ttl, hopcount, flags;
703*4882a593Smuzhiyun u8 next_hop[ETH_ALEN];
704*4882a593Smuzhiyun u32 target_sn, orig_sn, lifetime;
705*4882a593Smuzhiyun
706*4882a593Smuzhiyun mhwmp_dbg(sdata, "received PREP from %pM\n",
707*4882a593Smuzhiyun PREP_IE_TARGET_ADDR(prep_elem));
708*4882a593Smuzhiyun
709*4882a593Smuzhiyun orig_addr = PREP_IE_ORIG_ADDR(prep_elem);
710*4882a593Smuzhiyun if (ether_addr_equal(orig_addr, sdata->vif.addr))
711*4882a593Smuzhiyun /* destination, no forwarding required */
712*4882a593Smuzhiyun return;
713*4882a593Smuzhiyun
714*4882a593Smuzhiyun if (!ifmsh->mshcfg.dot11MeshForwarding)
715*4882a593Smuzhiyun return;
716*4882a593Smuzhiyun
717*4882a593Smuzhiyun ttl = PREP_IE_TTL(prep_elem);
718*4882a593Smuzhiyun if (ttl <= 1) {
719*4882a593Smuzhiyun sdata->u.mesh.mshstats.dropped_frames_ttl++;
720*4882a593Smuzhiyun return;
721*4882a593Smuzhiyun }
722*4882a593Smuzhiyun
723*4882a593Smuzhiyun rcu_read_lock();
724*4882a593Smuzhiyun mpath = mesh_path_lookup(sdata, orig_addr);
725*4882a593Smuzhiyun if (mpath)
726*4882a593Smuzhiyun spin_lock_bh(&mpath->state_lock);
727*4882a593Smuzhiyun else
728*4882a593Smuzhiyun goto fail;
729*4882a593Smuzhiyun if (!(mpath->flags & MESH_PATH_ACTIVE)) {
730*4882a593Smuzhiyun spin_unlock_bh(&mpath->state_lock);
731*4882a593Smuzhiyun goto fail;
732*4882a593Smuzhiyun }
733*4882a593Smuzhiyun memcpy(next_hop, next_hop_deref_protected(mpath)->sta.addr, ETH_ALEN);
734*4882a593Smuzhiyun spin_unlock_bh(&mpath->state_lock);
735*4882a593Smuzhiyun --ttl;
736*4882a593Smuzhiyun flags = PREP_IE_FLAGS(prep_elem);
737*4882a593Smuzhiyun lifetime = PREP_IE_LIFETIME(prep_elem);
738*4882a593Smuzhiyun hopcount = PREP_IE_HOPCOUNT(prep_elem) + 1;
739*4882a593Smuzhiyun target_addr = PREP_IE_TARGET_ADDR(prep_elem);
740*4882a593Smuzhiyun target_sn = PREP_IE_TARGET_SN(prep_elem);
741*4882a593Smuzhiyun orig_sn = PREP_IE_ORIG_SN(prep_elem);
742*4882a593Smuzhiyun
743*4882a593Smuzhiyun mesh_path_sel_frame_tx(MPATH_PREP, flags, orig_addr, orig_sn, 0,
744*4882a593Smuzhiyun target_addr, target_sn, next_hop, hopcount,
745*4882a593Smuzhiyun ttl, lifetime, metric, 0, sdata);
746*4882a593Smuzhiyun rcu_read_unlock();
747*4882a593Smuzhiyun
748*4882a593Smuzhiyun sdata->u.mesh.mshstats.fwded_unicast++;
749*4882a593Smuzhiyun sdata->u.mesh.mshstats.fwded_frames++;
750*4882a593Smuzhiyun return;
751*4882a593Smuzhiyun
752*4882a593Smuzhiyun fail:
753*4882a593Smuzhiyun rcu_read_unlock();
754*4882a593Smuzhiyun sdata->u.mesh.mshstats.dropped_frames_no_route++;
755*4882a593Smuzhiyun }
756*4882a593Smuzhiyun
hwmp_perr_frame_process(struct ieee80211_sub_if_data * sdata,struct ieee80211_mgmt * mgmt,const u8 * perr_elem)757*4882a593Smuzhiyun static void hwmp_perr_frame_process(struct ieee80211_sub_if_data *sdata,
758*4882a593Smuzhiyun struct ieee80211_mgmt *mgmt,
759*4882a593Smuzhiyun const u8 *perr_elem)
760*4882a593Smuzhiyun {
761*4882a593Smuzhiyun struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
762*4882a593Smuzhiyun struct mesh_path *mpath;
763*4882a593Smuzhiyun u8 ttl;
764*4882a593Smuzhiyun const u8 *ta, *target_addr;
765*4882a593Smuzhiyun u32 target_sn;
766*4882a593Smuzhiyun u16 target_rcode;
767*4882a593Smuzhiyun
768*4882a593Smuzhiyun ta = mgmt->sa;
769*4882a593Smuzhiyun ttl = PERR_IE_TTL(perr_elem);
770*4882a593Smuzhiyun if (ttl <= 1) {
771*4882a593Smuzhiyun ifmsh->mshstats.dropped_frames_ttl++;
772*4882a593Smuzhiyun return;
773*4882a593Smuzhiyun }
774*4882a593Smuzhiyun ttl--;
775*4882a593Smuzhiyun target_addr = PERR_IE_TARGET_ADDR(perr_elem);
776*4882a593Smuzhiyun target_sn = PERR_IE_TARGET_SN(perr_elem);
777*4882a593Smuzhiyun target_rcode = PERR_IE_TARGET_RCODE(perr_elem);
778*4882a593Smuzhiyun
779*4882a593Smuzhiyun rcu_read_lock();
780*4882a593Smuzhiyun mpath = mesh_path_lookup(sdata, target_addr);
781*4882a593Smuzhiyun if (mpath) {
782*4882a593Smuzhiyun struct sta_info *sta;
783*4882a593Smuzhiyun
784*4882a593Smuzhiyun spin_lock_bh(&mpath->state_lock);
785*4882a593Smuzhiyun sta = next_hop_deref_protected(mpath);
786*4882a593Smuzhiyun if (mpath->flags & MESH_PATH_ACTIVE &&
787*4882a593Smuzhiyun ether_addr_equal(ta, sta->sta.addr) &&
788*4882a593Smuzhiyun !(mpath->flags & MESH_PATH_FIXED) &&
789*4882a593Smuzhiyun (!(mpath->flags & MESH_PATH_SN_VALID) ||
790*4882a593Smuzhiyun SN_GT(target_sn, mpath->sn) || target_sn == 0)) {
791*4882a593Smuzhiyun mpath->flags &= ~MESH_PATH_ACTIVE;
792*4882a593Smuzhiyun if (target_sn != 0)
793*4882a593Smuzhiyun mpath->sn = target_sn;
794*4882a593Smuzhiyun else
795*4882a593Smuzhiyun mpath->sn += 1;
796*4882a593Smuzhiyun spin_unlock_bh(&mpath->state_lock);
797*4882a593Smuzhiyun if (!ifmsh->mshcfg.dot11MeshForwarding)
798*4882a593Smuzhiyun goto endperr;
799*4882a593Smuzhiyun mesh_path_error_tx(sdata, ttl, target_addr,
800*4882a593Smuzhiyun target_sn, target_rcode,
801*4882a593Smuzhiyun broadcast_addr);
802*4882a593Smuzhiyun } else
803*4882a593Smuzhiyun spin_unlock_bh(&mpath->state_lock);
804*4882a593Smuzhiyun }
805*4882a593Smuzhiyun endperr:
806*4882a593Smuzhiyun rcu_read_unlock();
807*4882a593Smuzhiyun }
808*4882a593Smuzhiyun
hwmp_rann_frame_process(struct ieee80211_sub_if_data * sdata,struct ieee80211_mgmt * mgmt,const struct ieee80211_rann_ie * rann)809*4882a593Smuzhiyun static void hwmp_rann_frame_process(struct ieee80211_sub_if_data *sdata,
810*4882a593Smuzhiyun struct ieee80211_mgmt *mgmt,
811*4882a593Smuzhiyun const struct ieee80211_rann_ie *rann)
812*4882a593Smuzhiyun {
813*4882a593Smuzhiyun struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
814*4882a593Smuzhiyun struct ieee80211_local *local = sdata->local;
815*4882a593Smuzhiyun struct sta_info *sta;
816*4882a593Smuzhiyun struct mesh_path *mpath;
817*4882a593Smuzhiyun u8 ttl, flags, hopcount;
818*4882a593Smuzhiyun const u8 *orig_addr;
819*4882a593Smuzhiyun u32 orig_sn, new_metric, orig_metric, last_hop_metric, interval;
820*4882a593Smuzhiyun bool root_is_gate;
821*4882a593Smuzhiyun
822*4882a593Smuzhiyun ttl = rann->rann_ttl;
823*4882a593Smuzhiyun flags = rann->rann_flags;
824*4882a593Smuzhiyun root_is_gate = !!(flags & RANN_FLAG_IS_GATE);
825*4882a593Smuzhiyun orig_addr = rann->rann_addr;
826*4882a593Smuzhiyun orig_sn = le32_to_cpu(rann->rann_seq);
827*4882a593Smuzhiyun interval = le32_to_cpu(rann->rann_interval);
828*4882a593Smuzhiyun hopcount = rann->rann_hopcount;
829*4882a593Smuzhiyun hopcount++;
830*4882a593Smuzhiyun orig_metric = le32_to_cpu(rann->rann_metric);
831*4882a593Smuzhiyun
832*4882a593Smuzhiyun /* Ignore our own RANNs */
833*4882a593Smuzhiyun if (ether_addr_equal(orig_addr, sdata->vif.addr))
834*4882a593Smuzhiyun return;
835*4882a593Smuzhiyun
836*4882a593Smuzhiyun mhwmp_dbg(sdata,
837*4882a593Smuzhiyun "received RANN from %pM via neighbour %pM (is_gate=%d)\n",
838*4882a593Smuzhiyun orig_addr, mgmt->sa, root_is_gate);
839*4882a593Smuzhiyun
840*4882a593Smuzhiyun rcu_read_lock();
841*4882a593Smuzhiyun sta = sta_info_get(sdata, mgmt->sa);
842*4882a593Smuzhiyun if (!sta) {
843*4882a593Smuzhiyun rcu_read_unlock();
844*4882a593Smuzhiyun return;
845*4882a593Smuzhiyun }
846*4882a593Smuzhiyun
847*4882a593Smuzhiyun last_hop_metric = airtime_link_metric_get(local, sta);
848*4882a593Smuzhiyun new_metric = orig_metric + last_hop_metric;
849*4882a593Smuzhiyun if (new_metric < orig_metric)
850*4882a593Smuzhiyun new_metric = MAX_METRIC;
851*4882a593Smuzhiyun
852*4882a593Smuzhiyun mpath = mesh_path_lookup(sdata, orig_addr);
853*4882a593Smuzhiyun if (!mpath) {
854*4882a593Smuzhiyun mpath = mesh_path_add(sdata, orig_addr);
855*4882a593Smuzhiyun if (IS_ERR(mpath)) {
856*4882a593Smuzhiyun rcu_read_unlock();
857*4882a593Smuzhiyun sdata->u.mesh.mshstats.dropped_frames_no_route++;
858*4882a593Smuzhiyun return;
859*4882a593Smuzhiyun }
860*4882a593Smuzhiyun }
861*4882a593Smuzhiyun
862*4882a593Smuzhiyun if (!(SN_LT(mpath->sn, orig_sn)) &&
863*4882a593Smuzhiyun !(mpath->sn == orig_sn && new_metric < mpath->rann_metric)) {
864*4882a593Smuzhiyun rcu_read_unlock();
865*4882a593Smuzhiyun return;
866*4882a593Smuzhiyun }
867*4882a593Smuzhiyun
868*4882a593Smuzhiyun if ((!(mpath->flags & (MESH_PATH_ACTIVE | MESH_PATH_RESOLVING)) ||
869*4882a593Smuzhiyun (time_after(jiffies, mpath->last_preq_to_root +
870*4882a593Smuzhiyun root_path_confirmation_jiffies(sdata)) ||
871*4882a593Smuzhiyun time_before(jiffies, mpath->last_preq_to_root))) &&
872*4882a593Smuzhiyun !(mpath->flags & MESH_PATH_FIXED) && (ttl != 0)) {
873*4882a593Smuzhiyun mhwmp_dbg(sdata,
874*4882a593Smuzhiyun "time to refresh root mpath %pM\n",
875*4882a593Smuzhiyun orig_addr);
876*4882a593Smuzhiyun mesh_queue_preq(mpath, PREQ_Q_F_START | PREQ_Q_F_REFRESH);
877*4882a593Smuzhiyun mpath->last_preq_to_root = jiffies;
878*4882a593Smuzhiyun }
879*4882a593Smuzhiyun
880*4882a593Smuzhiyun mpath->sn = orig_sn;
881*4882a593Smuzhiyun mpath->rann_metric = new_metric;
882*4882a593Smuzhiyun mpath->is_root = true;
883*4882a593Smuzhiyun /* Recording RANNs sender address to send individually
884*4882a593Smuzhiyun * addressed PREQs destined for root mesh STA */
885*4882a593Smuzhiyun memcpy(mpath->rann_snd_addr, mgmt->sa, ETH_ALEN);
886*4882a593Smuzhiyun
887*4882a593Smuzhiyun if (root_is_gate)
888*4882a593Smuzhiyun mesh_path_add_gate(mpath);
889*4882a593Smuzhiyun
890*4882a593Smuzhiyun if (ttl <= 1) {
891*4882a593Smuzhiyun ifmsh->mshstats.dropped_frames_ttl++;
892*4882a593Smuzhiyun rcu_read_unlock();
893*4882a593Smuzhiyun return;
894*4882a593Smuzhiyun }
895*4882a593Smuzhiyun ttl--;
896*4882a593Smuzhiyun
897*4882a593Smuzhiyun if (ifmsh->mshcfg.dot11MeshForwarding) {
898*4882a593Smuzhiyun mesh_path_sel_frame_tx(MPATH_RANN, flags, orig_addr,
899*4882a593Smuzhiyun orig_sn, 0, NULL, 0, broadcast_addr,
900*4882a593Smuzhiyun hopcount, ttl, interval,
901*4882a593Smuzhiyun new_metric, 0, sdata);
902*4882a593Smuzhiyun }
903*4882a593Smuzhiyun
904*4882a593Smuzhiyun rcu_read_unlock();
905*4882a593Smuzhiyun }
906*4882a593Smuzhiyun
907*4882a593Smuzhiyun
mesh_rx_path_sel_frame(struct ieee80211_sub_if_data * sdata,struct ieee80211_mgmt * mgmt,size_t len)908*4882a593Smuzhiyun void mesh_rx_path_sel_frame(struct ieee80211_sub_if_data *sdata,
909*4882a593Smuzhiyun struct ieee80211_mgmt *mgmt, size_t len)
910*4882a593Smuzhiyun {
911*4882a593Smuzhiyun struct ieee802_11_elems elems;
912*4882a593Smuzhiyun size_t baselen;
913*4882a593Smuzhiyun u32 path_metric;
914*4882a593Smuzhiyun struct sta_info *sta;
915*4882a593Smuzhiyun
916*4882a593Smuzhiyun /* need action_code */
917*4882a593Smuzhiyun if (len < IEEE80211_MIN_ACTION_SIZE + 1)
918*4882a593Smuzhiyun return;
919*4882a593Smuzhiyun
920*4882a593Smuzhiyun rcu_read_lock();
921*4882a593Smuzhiyun sta = sta_info_get(sdata, mgmt->sa);
922*4882a593Smuzhiyun if (!sta || sta->mesh->plink_state != NL80211_PLINK_ESTAB) {
923*4882a593Smuzhiyun rcu_read_unlock();
924*4882a593Smuzhiyun return;
925*4882a593Smuzhiyun }
926*4882a593Smuzhiyun rcu_read_unlock();
927*4882a593Smuzhiyun
928*4882a593Smuzhiyun baselen = (u8 *) mgmt->u.action.u.mesh_action.variable - (u8 *) mgmt;
929*4882a593Smuzhiyun ieee802_11_parse_elems(mgmt->u.action.u.mesh_action.variable,
930*4882a593Smuzhiyun len - baselen, false, &elems, mgmt->bssid, NULL);
931*4882a593Smuzhiyun
932*4882a593Smuzhiyun if (elems.preq) {
933*4882a593Smuzhiyun if (elems.preq_len != 37)
934*4882a593Smuzhiyun /* Right now we support just 1 destination and no AE */
935*4882a593Smuzhiyun return;
936*4882a593Smuzhiyun path_metric = hwmp_route_info_get(sdata, mgmt, elems.preq,
937*4882a593Smuzhiyun MPATH_PREQ);
938*4882a593Smuzhiyun if (path_metric)
939*4882a593Smuzhiyun hwmp_preq_frame_process(sdata, mgmt, elems.preq,
940*4882a593Smuzhiyun path_metric);
941*4882a593Smuzhiyun }
942*4882a593Smuzhiyun if (elems.prep) {
943*4882a593Smuzhiyun if (elems.prep_len != 31)
944*4882a593Smuzhiyun /* Right now we support no AE */
945*4882a593Smuzhiyun return;
946*4882a593Smuzhiyun path_metric = hwmp_route_info_get(sdata, mgmt, elems.prep,
947*4882a593Smuzhiyun MPATH_PREP);
948*4882a593Smuzhiyun if (path_metric)
949*4882a593Smuzhiyun hwmp_prep_frame_process(sdata, mgmt, elems.prep,
950*4882a593Smuzhiyun path_metric);
951*4882a593Smuzhiyun }
952*4882a593Smuzhiyun if (elems.perr) {
953*4882a593Smuzhiyun if (elems.perr_len != 15)
954*4882a593Smuzhiyun /* Right now we support only one destination per PERR */
955*4882a593Smuzhiyun return;
956*4882a593Smuzhiyun hwmp_perr_frame_process(sdata, mgmt, elems.perr);
957*4882a593Smuzhiyun }
958*4882a593Smuzhiyun if (elems.rann)
959*4882a593Smuzhiyun hwmp_rann_frame_process(sdata, mgmt, elems.rann);
960*4882a593Smuzhiyun }
961*4882a593Smuzhiyun
962*4882a593Smuzhiyun /**
963*4882a593Smuzhiyun * mesh_queue_preq - queue a PREQ to a given destination
964*4882a593Smuzhiyun *
965*4882a593Smuzhiyun * @mpath: mesh path to discover
966*4882a593Smuzhiyun * @flags: special attributes of the PREQ to be sent
967*4882a593Smuzhiyun *
968*4882a593Smuzhiyun * Locking: the function must be called from within a rcu read lock block.
969*4882a593Smuzhiyun *
970*4882a593Smuzhiyun */
mesh_queue_preq(struct mesh_path * mpath,u8 flags)971*4882a593Smuzhiyun static void mesh_queue_preq(struct mesh_path *mpath, u8 flags)
972*4882a593Smuzhiyun {
973*4882a593Smuzhiyun struct ieee80211_sub_if_data *sdata = mpath->sdata;
974*4882a593Smuzhiyun struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
975*4882a593Smuzhiyun struct mesh_preq_queue *preq_node;
976*4882a593Smuzhiyun
977*4882a593Smuzhiyun preq_node = kmalloc(sizeof(struct mesh_preq_queue), GFP_ATOMIC);
978*4882a593Smuzhiyun if (!preq_node) {
979*4882a593Smuzhiyun mhwmp_dbg(sdata, "could not allocate PREQ node\n");
980*4882a593Smuzhiyun return;
981*4882a593Smuzhiyun }
982*4882a593Smuzhiyun
983*4882a593Smuzhiyun spin_lock_bh(&ifmsh->mesh_preq_queue_lock);
984*4882a593Smuzhiyun if (ifmsh->preq_queue_len == MAX_PREQ_QUEUE_LEN) {
985*4882a593Smuzhiyun spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
986*4882a593Smuzhiyun kfree(preq_node);
987*4882a593Smuzhiyun if (printk_ratelimit())
988*4882a593Smuzhiyun mhwmp_dbg(sdata, "PREQ node queue full\n");
989*4882a593Smuzhiyun return;
990*4882a593Smuzhiyun }
991*4882a593Smuzhiyun
992*4882a593Smuzhiyun spin_lock(&mpath->state_lock);
993*4882a593Smuzhiyun if (mpath->flags & MESH_PATH_REQ_QUEUED) {
994*4882a593Smuzhiyun spin_unlock(&mpath->state_lock);
995*4882a593Smuzhiyun spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
996*4882a593Smuzhiyun kfree(preq_node);
997*4882a593Smuzhiyun return;
998*4882a593Smuzhiyun }
999*4882a593Smuzhiyun
1000*4882a593Smuzhiyun memcpy(preq_node->dst, mpath->dst, ETH_ALEN);
1001*4882a593Smuzhiyun preq_node->flags = flags;
1002*4882a593Smuzhiyun
1003*4882a593Smuzhiyun mpath->flags |= MESH_PATH_REQ_QUEUED;
1004*4882a593Smuzhiyun spin_unlock(&mpath->state_lock);
1005*4882a593Smuzhiyun
1006*4882a593Smuzhiyun list_add_tail(&preq_node->list, &ifmsh->preq_queue.list);
1007*4882a593Smuzhiyun ++ifmsh->preq_queue_len;
1008*4882a593Smuzhiyun spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
1009*4882a593Smuzhiyun
1010*4882a593Smuzhiyun if (time_after(jiffies, ifmsh->last_preq + min_preq_int_jiff(sdata)))
1011*4882a593Smuzhiyun ieee80211_queue_work(&sdata->local->hw, &sdata->work);
1012*4882a593Smuzhiyun
1013*4882a593Smuzhiyun else if (time_before(jiffies, ifmsh->last_preq)) {
1014*4882a593Smuzhiyun /* avoid long wait if did not send preqs for a long time
1015*4882a593Smuzhiyun * and jiffies wrapped around
1016*4882a593Smuzhiyun */
1017*4882a593Smuzhiyun ifmsh->last_preq = jiffies - min_preq_int_jiff(sdata) - 1;
1018*4882a593Smuzhiyun ieee80211_queue_work(&sdata->local->hw, &sdata->work);
1019*4882a593Smuzhiyun } else
1020*4882a593Smuzhiyun mod_timer(&ifmsh->mesh_path_timer, ifmsh->last_preq +
1021*4882a593Smuzhiyun min_preq_int_jiff(sdata));
1022*4882a593Smuzhiyun }
1023*4882a593Smuzhiyun
1024*4882a593Smuzhiyun /**
1025*4882a593Smuzhiyun * mesh_path_start_discovery - launch a path discovery from the PREQ queue
1026*4882a593Smuzhiyun *
1027*4882a593Smuzhiyun * @sdata: local mesh subif
1028*4882a593Smuzhiyun */
mesh_path_start_discovery(struct ieee80211_sub_if_data * sdata)1029*4882a593Smuzhiyun void mesh_path_start_discovery(struct ieee80211_sub_if_data *sdata)
1030*4882a593Smuzhiyun {
1031*4882a593Smuzhiyun struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
1032*4882a593Smuzhiyun struct mesh_preq_queue *preq_node;
1033*4882a593Smuzhiyun struct mesh_path *mpath;
1034*4882a593Smuzhiyun u8 ttl, target_flags = 0;
1035*4882a593Smuzhiyun const u8 *da;
1036*4882a593Smuzhiyun u32 lifetime;
1037*4882a593Smuzhiyun
1038*4882a593Smuzhiyun spin_lock_bh(&ifmsh->mesh_preq_queue_lock);
1039*4882a593Smuzhiyun if (!ifmsh->preq_queue_len ||
1040*4882a593Smuzhiyun time_before(jiffies, ifmsh->last_preq +
1041*4882a593Smuzhiyun min_preq_int_jiff(sdata))) {
1042*4882a593Smuzhiyun spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
1043*4882a593Smuzhiyun return;
1044*4882a593Smuzhiyun }
1045*4882a593Smuzhiyun
1046*4882a593Smuzhiyun preq_node = list_first_entry(&ifmsh->preq_queue.list,
1047*4882a593Smuzhiyun struct mesh_preq_queue, list);
1048*4882a593Smuzhiyun list_del(&preq_node->list);
1049*4882a593Smuzhiyun --ifmsh->preq_queue_len;
1050*4882a593Smuzhiyun spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
1051*4882a593Smuzhiyun
1052*4882a593Smuzhiyun rcu_read_lock();
1053*4882a593Smuzhiyun mpath = mesh_path_lookup(sdata, preq_node->dst);
1054*4882a593Smuzhiyun if (!mpath)
1055*4882a593Smuzhiyun goto enddiscovery;
1056*4882a593Smuzhiyun
1057*4882a593Smuzhiyun spin_lock_bh(&mpath->state_lock);
1058*4882a593Smuzhiyun if (mpath->flags & (MESH_PATH_DELETED | MESH_PATH_FIXED)) {
1059*4882a593Smuzhiyun spin_unlock_bh(&mpath->state_lock);
1060*4882a593Smuzhiyun goto enddiscovery;
1061*4882a593Smuzhiyun }
1062*4882a593Smuzhiyun mpath->flags &= ~MESH_PATH_REQ_QUEUED;
1063*4882a593Smuzhiyun if (preq_node->flags & PREQ_Q_F_START) {
1064*4882a593Smuzhiyun if (mpath->flags & MESH_PATH_RESOLVING) {
1065*4882a593Smuzhiyun spin_unlock_bh(&mpath->state_lock);
1066*4882a593Smuzhiyun goto enddiscovery;
1067*4882a593Smuzhiyun } else {
1068*4882a593Smuzhiyun mpath->flags &= ~MESH_PATH_RESOLVED;
1069*4882a593Smuzhiyun mpath->flags |= MESH_PATH_RESOLVING;
1070*4882a593Smuzhiyun mpath->discovery_retries = 0;
1071*4882a593Smuzhiyun mpath->discovery_timeout = disc_timeout_jiff(sdata);
1072*4882a593Smuzhiyun }
1073*4882a593Smuzhiyun } else if (!(mpath->flags & MESH_PATH_RESOLVING) ||
1074*4882a593Smuzhiyun mpath->flags & MESH_PATH_RESOLVED) {
1075*4882a593Smuzhiyun mpath->flags &= ~MESH_PATH_RESOLVING;
1076*4882a593Smuzhiyun spin_unlock_bh(&mpath->state_lock);
1077*4882a593Smuzhiyun goto enddiscovery;
1078*4882a593Smuzhiyun }
1079*4882a593Smuzhiyun
1080*4882a593Smuzhiyun ifmsh->last_preq = jiffies;
1081*4882a593Smuzhiyun
1082*4882a593Smuzhiyun if (time_after(jiffies, ifmsh->last_sn_update +
1083*4882a593Smuzhiyun net_traversal_jiffies(sdata)) ||
1084*4882a593Smuzhiyun time_before(jiffies, ifmsh->last_sn_update)) {
1085*4882a593Smuzhiyun ++ifmsh->sn;
1086*4882a593Smuzhiyun sdata->u.mesh.last_sn_update = jiffies;
1087*4882a593Smuzhiyun }
1088*4882a593Smuzhiyun lifetime = default_lifetime(sdata);
1089*4882a593Smuzhiyun ttl = sdata->u.mesh.mshcfg.element_ttl;
1090*4882a593Smuzhiyun if (ttl == 0) {
1091*4882a593Smuzhiyun sdata->u.mesh.mshstats.dropped_frames_ttl++;
1092*4882a593Smuzhiyun spin_unlock_bh(&mpath->state_lock);
1093*4882a593Smuzhiyun goto enddiscovery;
1094*4882a593Smuzhiyun }
1095*4882a593Smuzhiyun
1096*4882a593Smuzhiyun if (preq_node->flags & PREQ_Q_F_REFRESH)
1097*4882a593Smuzhiyun target_flags |= IEEE80211_PREQ_TO_FLAG;
1098*4882a593Smuzhiyun else
1099*4882a593Smuzhiyun target_flags &= ~IEEE80211_PREQ_TO_FLAG;
1100*4882a593Smuzhiyun
1101*4882a593Smuzhiyun spin_unlock_bh(&mpath->state_lock);
1102*4882a593Smuzhiyun da = (mpath->is_root) ? mpath->rann_snd_addr : broadcast_addr;
1103*4882a593Smuzhiyun mesh_path_sel_frame_tx(MPATH_PREQ, 0, sdata->vif.addr, ifmsh->sn,
1104*4882a593Smuzhiyun target_flags, mpath->dst, mpath->sn, da, 0,
1105*4882a593Smuzhiyun ttl, lifetime, 0, ifmsh->preq_id++, sdata);
1106*4882a593Smuzhiyun
1107*4882a593Smuzhiyun spin_lock_bh(&mpath->state_lock);
1108*4882a593Smuzhiyun if (!(mpath->flags & MESH_PATH_DELETED))
1109*4882a593Smuzhiyun mod_timer(&mpath->timer, jiffies + mpath->discovery_timeout);
1110*4882a593Smuzhiyun spin_unlock_bh(&mpath->state_lock);
1111*4882a593Smuzhiyun
1112*4882a593Smuzhiyun enddiscovery:
1113*4882a593Smuzhiyun rcu_read_unlock();
1114*4882a593Smuzhiyun kfree(preq_node);
1115*4882a593Smuzhiyun }
1116*4882a593Smuzhiyun
1117*4882a593Smuzhiyun /**
1118*4882a593Smuzhiyun * mesh_nexthop_resolve - lookup next hop; conditionally start path discovery
1119*4882a593Smuzhiyun *
1120*4882a593Smuzhiyun * @skb: 802.11 frame to be sent
1121*4882a593Smuzhiyun * @sdata: network subif the frame will be sent through
1122*4882a593Smuzhiyun *
1123*4882a593Smuzhiyun * Lookup next hop for given skb and start path discovery if no
1124*4882a593Smuzhiyun * forwarding information is found.
1125*4882a593Smuzhiyun *
1126*4882a593Smuzhiyun * Returns: 0 if the next hop was found and -ENOENT if the frame was queued.
1127*4882a593Smuzhiyun * skb is freeed here if no mpath could be allocated.
1128*4882a593Smuzhiyun */
mesh_nexthop_resolve(struct ieee80211_sub_if_data * sdata,struct sk_buff * skb)1129*4882a593Smuzhiyun int mesh_nexthop_resolve(struct ieee80211_sub_if_data *sdata,
1130*4882a593Smuzhiyun struct sk_buff *skb)
1131*4882a593Smuzhiyun {
1132*4882a593Smuzhiyun struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1133*4882a593Smuzhiyun struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1134*4882a593Smuzhiyun struct mesh_path *mpath;
1135*4882a593Smuzhiyun struct sk_buff *skb_to_free = NULL;
1136*4882a593Smuzhiyun u8 *target_addr = hdr->addr3;
1137*4882a593Smuzhiyun
1138*4882a593Smuzhiyun /* Nulls are only sent to peers for PS and should be pre-addressed */
1139*4882a593Smuzhiyun if (ieee80211_is_qos_nullfunc(hdr->frame_control))
1140*4882a593Smuzhiyun return 0;
1141*4882a593Smuzhiyun
1142*4882a593Smuzhiyun /* Allow injected packets to bypass mesh routing */
1143*4882a593Smuzhiyun if (info->control.flags & IEEE80211_TX_CTRL_SKIP_MPATH_LOOKUP)
1144*4882a593Smuzhiyun return 0;
1145*4882a593Smuzhiyun
1146*4882a593Smuzhiyun if (!mesh_nexthop_lookup(sdata, skb))
1147*4882a593Smuzhiyun return 0;
1148*4882a593Smuzhiyun
1149*4882a593Smuzhiyun /* no nexthop found, start resolving */
1150*4882a593Smuzhiyun mpath = mesh_path_lookup(sdata, target_addr);
1151*4882a593Smuzhiyun if (!mpath) {
1152*4882a593Smuzhiyun mpath = mesh_path_add(sdata, target_addr);
1153*4882a593Smuzhiyun if (IS_ERR(mpath)) {
1154*4882a593Smuzhiyun mesh_path_discard_frame(sdata, skb);
1155*4882a593Smuzhiyun return PTR_ERR(mpath);
1156*4882a593Smuzhiyun }
1157*4882a593Smuzhiyun }
1158*4882a593Smuzhiyun
1159*4882a593Smuzhiyun if (!(mpath->flags & MESH_PATH_RESOLVING) &&
1160*4882a593Smuzhiyun mesh_path_sel_is_hwmp(sdata))
1161*4882a593Smuzhiyun mesh_queue_preq(mpath, PREQ_Q_F_START);
1162*4882a593Smuzhiyun
1163*4882a593Smuzhiyun if (skb_queue_len(&mpath->frame_queue) >= MESH_FRAME_QUEUE_LEN)
1164*4882a593Smuzhiyun skb_to_free = skb_dequeue(&mpath->frame_queue);
1165*4882a593Smuzhiyun
1166*4882a593Smuzhiyun info->control.flags |= IEEE80211_TX_INTCFL_NEED_TXPROCESSING;
1167*4882a593Smuzhiyun ieee80211_set_qos_hdr(sdata, skb);
1168*4882a593Smuzhiyun skb_queue_tail(&mpath->frame_queue, skb);
1169*4882a593Smuzhiyun if (skb_to_free)
1170*4882a593Smuzhiyun mesh_path_discard_frame(sdata, skb_to_free);
1171*4882a593Smuzhiyun
1172*4882a593Smuzhiyun return -ENOENT;
1173*4882a593Smuzhiyun }
1174*4882a593Smuzhiyun
1175*4882a593Smuzhiyun /**
1176*4882a593Smuzhiyun * mesh_nexthop_lookup_nolearn - try to set next hop without path discovery
1177*4882a593Smuzhiyun * @skb: 802.11 frame to be sent
1178*4882a593Smuzhiyun * @sdata: network subif the frame will be sent through
1179*4882a593Smuzhiyun *
1180*4882a593Smuzhiyun * Check if the meshDA (addr3) of a unicast frame is a direct neighbor.
1181*4882a593Smuzhiyun * And if so, set the RA (addr1) to it to transmit to this node directly,
1182*4882a593Smuzhiyun * avoiding PREQ/PREP path discovery.
1183*4882a593Smuzhiyun *
1184*4882a593Smuzhiyun * Returns: 0 if the next hop was found and -ENOENT otherwise.
1185*4882a593Smuzhiyun */
mesh_nexthop_lookup_nolearn(struct ieee80211_sub_if_data * sdata,struct sk_buff * skb)1186*4882a593Smuzhiyun static int mesh_nexthop_lookup_nolearn(struct ieee80211_sub_if_data *sdata,
1187*4882a593Smuzhiyun struct sk_buff *skb)
1188*4882a593Smuzhiyun {
1189*4882a593Smuzhiyun struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1190*4882a593Smuzhiyun struct sta_info *sta;
1191*4882a593Smuzhiyun
1192*4882a593Smuzhiyun if (is_multicast_ether_addr(hdr->addr1))
1193*4882a593Smuzhiyun return -ENOENT;
1194*4882a593Smuzhiyun
1195*4882a593Smuzhiyun rcu_read_lock();
1196*4882a593Smuzhiyun sta = sta_info_get(sdata, hdr->addr3);
1197*4882a593Smuzhiyun
1198*4882a593Smuzhiyun if (!sta || sta->mesh->plink_state != NL80211_PLINK_ESTAB) {
1199*4882a593Smuzhiyun rcu_read_unlock();
1200*4882a593Smuzhiyun return -ENOENT;
1201*4882a593Smuzhiyun }
1202*4882a593Smuzhiyun rcu_read_unlock();
1203*4882a593Smuzhiyun
1204*4882a593Smuzhiyun memcpy(hdr->addr1, hdr->addr3, ETH_ALEN);
1205*4882a593Smuzhiyun memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN);
1206*4882a593Smuzhiyun return 0;
1207*4882a593Smuzhiyun }
1208*4882a593Smuzhiyun
1209*4882a593Smuzhiyun /**
1210*4882a593Smuzhiyun * mesh_nexthop_lookup - put the appropriate next hop on a mesh frame. Calling
1211*4882a593Smuzhiyun * this function is considered "using" the associated mpath, so preempt a path
1212*4882a593Smuzhiyun * refresh if this mpath expires soon.
1213*4882a593Smuzhiyun *
1214*4882a593Smuzhiyun * @skb: 802.11 frame to be sent
1215*4882a593Smuzhiyun * @sdata: network subif the frame will be sent through
1216*4882a593Smuzhiyun *
1217*4882a593Smuzhiyun * Returns: 0 if the next hop was found. Nonzero otherwise.
1218*4882a593Smuzhiyun */
mesh_nexthop_lookup(struct ieee80211_sub_if_data * sdata,struct sk_buff * skb)1219*4882a593Smuzhiyun int mesh_nexthop_lookup(struct ieee80211_sub_if_data *sdata,
1220*4882a593Smuzhiyun struct sk_buff *skb)
1221*4882a593Smuzhiyun {
1222*4882a593Smuzhiyun struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
1223*4882a593Smuzhiyun struct mesh_path *mpath;
1224*4882a593Smuzhiyun struct sta_info *next_hop;
1225*4882a593Smuzhiyun struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1226*4882a593Smuzhiyun u8 *target_addr = hdr->addr3;
1227*4882a593Smuzhiyun
1228*4882a593Smuzhiyun if (ifmsh->mshcfg.dot11MeshNolearn &&
1229*4882a593Smuzhiyun !mesh_nexthop_lookup_nolearn(sdata, skb))
1230*4882a593Smuzhiyun return 0;
1231*4882a593Smuzhiyun
1232*4882a593Smuzhiyun mpath = mesh_path_lookup(sdata, target_addr);
1233*4882a593Smuzhiyun if (!mpath || !(mpath->flags & MESH_PATH_ACTIVE))
1234*4882a593Smuzhiyun return -ENOENT;
1235*4882a593Smuzhiyun
1236*4882a593Smuzhiyun if (time_after(jiffies,
1237*4882a593Smuzhiyun mpath->exp_time -
1238*4882a593Smuzhiyun msecs_to_jiffies(sdata->u.mesh.mshcfg.path_refresh_time)) &&
1239*4882a593Smuzhiyun ether_addr_equal(sdata->vif.addr, hdr->addr4) &&
1240*4882a593Smuzhiyun !(mpath->flags & MESH_PATH_RESOLVING) &&
1241*4882a593Smuzhiyun !(mpath->flags & MESH_PATH_FIXED))
1242*4882a593Smuzhiyun mesh_queue_preq(mpath, PREQ_Q_F_START | PREQ_Q_F_REFRESH);
1243*4882a593Smuzhiyun
1244*4882a593Smuzhiyun next_hop = rcu_dereference(mpath->next_hop);
1245*4882a593Smuzhiyun if (next_hop) {
1246*4882a593Smuzhiyun memcpy(hdr->addr1, next_hop->sta.addr, ETH_ALEN);
1247*4882a593Smuzhiyun memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN);
1248*4882a593Smuzhiyun ieee80211_mps_set_frame_flags(sdata, next_hop, hdr);
1249*4882a593Smuzhiyun return 0;
1250*4882a593Smuzhiyun }
1251*4882a593Smuzhiyun
1252*4882a593Smuzhiyun return -ENOENT;
1253*4882a593Smuzhiyun }
1254*4882a593Smuzhiyun
mesh_path_timer(struct timer_list * t)1255*4882a593Smuzhiyun void mesh_path_timer(struct timer_list *t)
1256*4882a593Smuzhiyun {
1257*4882a593Smuzhiyun struct mesh_path *mpath = from_timer(mpath, t, timer);
1258*4882a593Smuzhiyun struct ieee80211_sub_if_data *sdata = mpath->sdata;
1259*4882a593Smuzhiyun int ret;
1260*4882a593Smuzhiyun
1261*4882a593Smuzhiyun if (sdata->local->quiescing)
1262*4882a593Smuzhiyun return;
1263*4882a593Smuzhiyun
1264*4882a593Smuzhiyun spin_lock_bh(&mpath->state_lock);
1265*4882a593Smuzhiyun if (mpath->flags & MESH_PATH_RESOLVED ||
1266*4882a593Smuzhiyun (!(mpath->flags & MESH_PATH_RESOLVING))) {
1267*4882a593Smuzhiyun mpath->flags &= ~(MESH_PATH_RESOLVING | MESH_PATH_RESOLVED);
1268*4882a593Smuzhiyun spin_unlock_bh(&mpath->state_lock);
1269*4882a593Smuzhiyun } else if (mpath->discovery_retries < max_preq_retries(sdata)) {
1270*4882a593Smuzhiyun ++mpath->discovery_retries;
1271*4882a593Smuzhiyun mpath->discovery_timeout *= 2;
1272*4882a593Smuzhiyun mpath->flags &= ~MESH_PATH_REQ_QUEUED;
1273*4882a593Smuzhiyun spin_unlock_bh(&mpath->state_lock);
1274*4882a593Smuzhiyun mesh_queue_preq(mpath, 0);
1275*4882a593Smuzhiyun } else {
1276*4882a593Smuzhiyun mpath->flags &= ~(MESH_PATH_RESOLVING |
1277*4882a593Smuzhiyun MESH_PATH_RESOLVED |
1278*4882a593Smuzhiyun MESH_PATH_REQ_QUEUED);
1279*4882a593Smuzhiyun mpath->exp_time = jiffies;
1280*4882a593Smuzhiyun spin_unlock_bh(&mpath->state_lock);
1281*4882a593Smuzhiyun if (!mpath->is_gate && mesh_gate_num(sdata) > 0) {
1282*4882a593Smuzhiyun ret = mesh_path_send_to_gates(mpath);
1283*4882a593Smuzhiyun if (ret)
1284*4882a593Smuzhiyun mhwmp_dbg(sdata, "no gate was reachable\n");
1285*4882a593Smuzhiyun } else
1286*4882a593Smuzhiyun mesh_path_flush_pending(mpath);
1287*4882a593Smuzhiyun }
1288*4882a593Smuzhiyun }
1289*4882a593Smuzhiyun
mesh_path_tx_root_frame(struct ieee80211_sub_if_data * sdata)1290*4882a593Smuzhiyun void mesh_path_tx_root_frame(struct ieee80211_sub_if_data *sdata)
1291*4882a593Smuzhiyun {
1292*4882a593Smuzhiyun struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
1293*4882a593Smuzhiyun u32 interval = ifmsh->mshcfg.dot11MeshHWMPRannInterval;
1294*4882a593Smuzhiyun u8 flags, target_flags = 0;
1295*4882a593Smuzhiyun
1296*4882a593Smuzhiyun flags = (ifmsh->mshcfg.dot11MeshGateAnnouncementProtocol)
1297*4882a593Smuzhiyun ? RANN_FLAG_IS_GATE : 0;
1298*4882a593Smuzhiyun
1299*4882a593Smuzhiyun switch (ifmsh->mshcfg.dot11MeshHWMPRootMode) {
1300*4882a593Smuzhiyun case IEEE80211_PROACTIVE_RANN:
1301*4882a593Smuzhiyun mesh_path_sel_frame_tx(MPATH_RANN, flags, sdata->vif.addr,
1302*4882a593Smuzhiyun ++ifmsh->sn, 0, NULL, 0, broadcast_addr,
1303*4882a593Smuzhiyun 0, ifmsh->mshcfg.element_ttl,
1304*4882a593Smuzhiyun interval, 0, 0, sdata);
1305*4882a593Smuzhiyun break;
1306*4882a593Smuzhiyun case IEEE80211_PROACTIVE_PREQ_WITH_PREP:
1307*4882a593Smuzhiyun flags |= IEEE80211_PREQ_PROACTIVE_PREP_FLAG;
1308*4882a593Smuzhiyun fallthrough;
1309*4882a593Smuzhiyun case IEEE80211_PROACTIVE_PREQ_NO_PREP:
1310*4882a593Smuzhiyun interval = ifmsh->mshcfg.dot11MeshHWMPactivePathToRootTimeout;
1311*4882a593Smuzhiyun target_flags |= IEEE80211_PREQ_TO_FLAG |
1312*4882a593Smuzhiyun IEEE80211_PREQ_USN_FLAG;
1313*4882a593Smuzhiyun mesh_path_sel_frame_tx(MPATH_PREQ, flags, sdata->vif.addr,
1314*4882a593Smuzhiyun ++ifmsh->sn, target_flags,
1315*4882a593Smuzhiyun (u8 *) broadcast_addr, 0, broadcast_addr,
1316*4882a593Smuzhiyun 0, ifmsh->mshcfg.element_ttl, interval,
1317*4882a593Smuzhiyun 0, ifmsh->preq_id++, sdata);
1318*4882a593Smuzhiyun break;
1319*4882a593Smuzhiyun default:
1320*4882a593Smuzhiyun mhwmp_dbg(sdata, "Proactive mechanism not supported\n");
1321*4882a593Smuzhiyun return;
1322*4882a593Smuzhiyun }
1323*4882a593Smuzhiyun }
1324