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