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