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