mwifiex: fix bug in wildcard scan handling
[linux-block.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;
050ac52c 516 struct mesh_path *mpath;
d19b3bf6
RP
517 u8 *target_addr, *orig_addr;
518 u8 target_flags, ttl;
519 u32 orig_sn, target_sn, lifetime;
050ac52c
LCC
520 bool reply = false;
521 bool forward = true;
522
d19b3bf6
RP
523 /* Update target SN, if present */
524 target_addr = PREQ_IE_TARGET_ADDR(preq_elem);
050ac52c 525 orig_addr = PREQ_IE_ORIG_ADDR(preq_elem);
d19b3bf6
RP
526 target_sn = PREQ_IE_TARGET_SN(preq_elem);
527 orig_sn = PREQ_IE_ORIG_SN(preq_elem);
528 target_flags = PREQ_IE_TARGET_F(preq_elem);
050ac52c 529
7646887a 530 mhwmp_dbg("received PREQ from %pM", orig_addr);
27db2e42 531
47846c9b 532 if (memcmp(target_addr, sdata->vif.addr, ETH_ALEN) == 0) {
7646887a 533 mhwmp_dbg("PREQ is for us");
050ac52c
LCC
534 forward = false;
535 reply = true;
536 metric = 0;
d19b3bf6 537 if (time_after(jiffies, ifmsh->last_sn_update +
050ac52c 538 net_traversal_jiffies(sdata)) ||
d19b3bf6
RP
539 time_before(jiffies, ifmsh->last_sn_update)) {
540 target_sn = ++ifmsh->sn;
541 ifmsh->last_sn_update = jiffies;
050ac52c
LCC
542 }
543 } else {
544 rcu_read_lock();
d19b3bf6 545 mpath = mesh_path_lookup(target_addr, sdata);
050ac52c 546 if (mpath) {
d19b3bf6
RP
547 if ((!(mpath->flags & MESH_PATH_SN_VALID)) ||
548 SN_LT(mpath->sn, target_sn)) {
549 mpath->sn = target_sn;
550 mpath->flags |= MESH_PATH_SN_VALID;
551 } else if ((!(target_flags & MP_F_DO)) &&
050ac52c
LCC
552 (mpath->flags & MESH_PATH_ACTIVE)) {
553 reply = true;
554 metric = mpath->metric;
d19b3bf6
RP
555 target_sn = mpath->sn;
556 if (target_flags & MP_F_RF)
557 target_flags |= MP_F_DO;
050ac52c
LCC
558 else
559 forward = false;
560 }
561 }
562 rcu_read_unlock();
563 }
564
565 if (reply) {
566 lifetime = PREQ_IE_LIFETIME(preq_elem);
45904f21 567 ttl = ifmsh->mshcfg.element_ttl;
27db2e42 568 if (ttl != 0) {
7646887a 569 mhwmp_dbg("replying to the PREQ");
3c26f1f6
TP
570 mesh_path_sel_frame_tx(MPATH_PREP, 0, orig_addr,
571 cpu_to_le32(orig_sn), 0, target_addr,
572 cpu_to_le32(target_sn), mgmt->sa, 0, ttl,
aa2b5928 573 cpu_to_le32(lifetime), cpu_to_le32(metric),
f698d856 574 0, sdata);
27db2e42 575 } else
472dbc45 576 ifmsh->mshstats.dropped_frames_ttl++;
050ac52c
LCC
577 }
578
94f90656 579 if (forward && ifmsh->mshcfg.dot11MeshForwarding) {
050ac52c
LCC
580 u32 preq_id;
581 u8 hopcount, flags;
582
583 ttl = PREQ_IE_TTL(preq_elem);
584 lifetime = PREQ_IE_LIFETIME(preq_elem);
585 if (ttl <= 1) {
472dbc45 586 ifmsh->mshstats.dropped_frames_ttl++;
050ac52c
LCC
587 return;
588 }
7646887a 589 mhwmp_dbg("forwarding the PREQ from %pM", orig_addr);
050ac52c
LCC
590 --ttl;
591 flags = PREQ_IE_FLAGS(preq_elem);
592 preq_id = PREQ_IE_PREQ_ID(preq_elem);
593 hopcount = PREQ_IE_HOPCOUNT(preq_elem) + 1;
594 mesh_path_sel_frame_tx(MPATH_PREQ, flags, orig_addr,
d19b3bf6 595 cpu_to_le32(orig_sn), target_flags, target_addr,
15ff6365 596 cpu_to_le32(target_sn), broadcast_addr,
aa2b5928
LCC
597 hopcount, ttl, cpu_to_le32(lifetime),
598 cpu_to_le32(metric), cpu_to_le32(preq_id),
f698d856 599 sdata);
c8a61a7d 600 ifmsh->mshstats.fwded_mcast++;
472dbc45 601 ifmsh->mshstats.fwded_frames++;
050ac52c
LCC
602 }
603}
604
605
40b275b6
JB
606static inline struct sta_info *
607next_hop_deref_protected(struct mesh_path *mpath)
608{
609 return rcu_dereference_protected(mpath->next_hop,
610 lockdep_is_held(&mpath->state_lock));
611}
612
613
f698d856 614static void hwmp_prep_frame_process(struct ieee80211_sub_if_data *sdata,
050ac52c
LCC
615 struct ieee80211_mgmt *mgmt,
616 u8 *prep_elem, u32 metric)
617{
050ac52c 618 struct mesh_path *mpath;
d19b3bf6 619 u8 *target_addr, *orig_addr;
050ac52c
LCC
620 u8 ttl, hopcount, flags;
621 u8 next_hop[ETH_ALEN];
d19b3bf6 622 u32 target_sn, orig_sn, lifetime;
050ac52c 623
7646887a 624 mhwmp_dbg("received PREP from %pM", PREP_IE_ORIG_ADDR(prep_elem));
dbb81c42 625
3c26f1f6
TP
626 orig_addr = PREP_IE_ORIG_ADDR(prep_elem);
627 if (memcmp(orig_addr, sdata->vif.addr, ETH_ALEN) == 0)
050ac52c
LCC
628 /* destination, no forwarding required */
629 return;
630
631 ttl = PREP_IE_TTL(prep_elem);
632 if (ttl <= 1) {
472dbc45 633 sdata->u.mesh.mshstats.dropped_frames_ttl++;
050ac52c
LCC
634 return;
635 }
636
637 rcu_read_lock();
3c26f1f6 638 mpath = mesh_path_lookup(orig_addr, sdata);
050ac52c
LCC
639 if (mpath)
640 spin_lock_bh(&mpath->state_lock);
641 else
642 goto fail;
643 if (!(mpath->flags & MESH_PATH_ACTIVE)) {
644 spin_unlock_bh(&mpath->state_lock);
645 goto fail;
646 }
40b275b6 647 memcpy(next_hop, next_hop_deref_protected(mpath)->sta.addr, ETH_ALEN);
050ac52c
LCC
648 spin_unlock_bh(&mpath->state_lock);
649 --ttl;
650 flags = PREP_IE_FLAGS(prep_elem);
651 lifetime = PREP_IE_LIFETIME(prep_elem);
652 hopcount = PREP_IE_HOPCOUNT(prep_elem) + 1;
3c26f1f6 653 target_addr = PREP_IE_TARGET_ADDR(prep_elem);
d19b3bf6
RP
654 target_sn = PREP_IE_TARGET_SN(prep_elem);
655 orig_sn = PREP_IE_ORIG_SN(prep_elem);
050ac52c
LCC
656
657 mesh_path_sel_frame_tx(MPATH_PREP, flags, orig_addr,
d19b3bf6 658 cpu_to_le32(orig_sn), 0, target_addr,
533866b1 659 cpu_to_le32(target_sn), next_hop, hopcount,
d19b3bf6 660 ttl, cpu_to_le32(lifetime), cpu_to_le32(metric),
f698d856 661 0, sdata);
050ac52c 662 rcu_read_unlock();
c8a61a7d
DW
663
664 sdata->u.mesh.mshstats.fwded_unicast++;
472dbc45 665 sdata->u.mesh.mshstats.fwded_frames++;
050ac52c
LCC
666 return;
667
668fail:
669 rcu_read_unlock();
472dbc45 670 sdata->u.mesh.mshstats.dropped_frames_no_route++;
050ac52c
LCC
671}
672
f698d856 673static void hwmp_perr_frame_process(struct ieee80211_sub_if_data *sdata,
050ac52c
LCC
674 struct ieee80211_mgmt *mgmt, u8 *perr_elem)
675{
d611f062 676 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
050ac52c 677 struct mesh_path *mpath;
d611f062 678 u8 ttl;
d19b3bf6 679 u8 *ta, *target_addr;
d19b3bf6
RP
680 u32 target_sn;
681 u16 target_rcode;
050ac52c
LCC
682
683 ta = mgmt->sa;
d611f062
RP
684 ttl = PERR_IE_TTL(perr_elem);
685 if (ttl <= 1) {
686 ifmsh->mshstats.dropped_frames_ttl++;
687 return;
688 }
689 ttl--;
d19b3bf6
RP
690 target_addr = PERR_IE_TARGET_ADDR(perr_elem);
691 target_sn = PERR_IE_TARGET_SN(perr_elem);
692 target_rcode = PERR_IE_TARGET_RCODE(perr_elem);
d611f062 693
050ac52c 694 rcu_read_lock();
d19b3bf6 695 mpath = mesh_path_lookup(target_addr, sdata);
050ac52c
LCC
696 if (mpath) {
697 spin_lock_bh(&mpath->state_lock);
698 if (mpath->flags & MESH_PATH_ACTIVE &&
40b275b6
JB
699 memcmp(ta, next_hop_deref_protected(mpath)->sta.addr,
700 ETH_ALEN) == 0 &&
d19b3bf6
RP
701 (!(mpath->flags & MESH_PATH_SN_VALID) ||
702 SN_GT(target_sn, mpath->sn))) {
050ac52c 703 mpath->flags &= ~MESH_PATH_ACTIVE;
d19b3bf6 704 mpath->sn = target_sn;
050ac52c 705 spin_unlock_bh(&mpath->state_lock);
d19b3bf6
RP
706 mesh_path_error_tx(ttl, target_addr, cpu_to_le32(target_sn),
707 cpu_to_le16(target_rcode),
15ff6365 708 broadcast_addr, sdata);
050ac52c
LCC
709 } else
710 spin_unlock_bh(&mpath->state_lock);
711 }
712 rcu_read_unlock();
713}
714
90a5e169
RP
715static void hwmp_rann_frame_process(struct ieee80211_sub_if_data *sdata,
716 struct ieee80211_mgmt *mgmt,
717 struct ieee80211_rann_ie *rann)
718{
719 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
720 struct mesh_path *mpath;
90a5e169
RP
721 u8 ttl, flags, hopcount;
722 u8 *orig_addr;
d19b3bf6 723 u32 orig_sn, metric;
0507e159 724 u32 interval = ifmsh->mshcfg.dot11MeshHWMPRannInterval;
5ee68e5b 725 bool root_is_gate;
90a5e169 726
90a5e169
RP
727 ttl = rann->rann_ttl;
728 if (ttl <= 1) {
729 ifmsh->mshstats.dropped_frames_ttl++;
730 return;
731 }
732 ttl--;
733 flags = rann->rann_flags;
5ee68e5b 734 root_is_gate = !!(flags & RANN_FLAG_IS_GATE);
90a5e169 735 orig_addr = rann->rann_addr;
d19b3bf6 736 orig_sn = rann->rann_seq;
90a5e169 737 hopcount = rann->rann_hopcount;
a6a58b4f 738 hopcount++;
90a5e169 739 metric = rann->rann_metric;
5ee68e5b
JC
740
741 /* Ignore our own RANNs */
742 if (memcmp(orig_addr, sdata->vif.addr, ETH_ALEN) == 0)
743 return;
744
745 mhwmp_dbg("received RANN from %pM (is_gate=%d)", orig_addr,
746 root_is_gate);
90a5e169
RP
747
748 rcu_read_lock();
749 mpath = mesh_path_lookup(orig_addr, sdata);
750 if (!mpath) {
751 mesh_path_add(orig_addr, sdata);
752 mpath = mesh_path_lookup(orig_addr, sdata);
753 if (!mpath) {
754 rcu_read_unlock();
755 sdata->u.mesh.mshstats.dropped_frames_no_route++;
756 return;
757 }
90a5e169 758 }
5ee68e5b
JC
759
760 if ((!(mpath->flags & (MESH_PATH_ACTIVE | MESH_PATH_RESOLVING)) ||
761 time_after(jiffies, mpath->exp_time - 1*HZ)) &&
762 !(mpath->flags & MESH_PATH_FIXED)) {
763 mhwmp_dbg("%s time to refresh root mpath %pM", sdata->name,
764 orig_addr);
765 mesh_queue_preq(mpath, PREQ_Q_F_START | PREQ_Q_F_REFRESH);
766 }
767
d19b3bf6 768 if (mpath->sn < orig_sn) {
90a5e169 769 mesh_path_sel_frame_tx(MPATH_RANN, flags, orig_addr,
d19b3bf6 770 cpu_to_le32(orig_sn),
15ff6365 771 0, NULL, 0, broadcast_addr,
0507e159 772 hopcount, ttl, cpu_to_le32(interval),
a6a58b4f 773 cpu_to_le32(metric + mpath->metric),
90a5e169 774 0, sdata);
d19b3bf6 775 mpath->sn = orig_sn;
90a5e169 776 }
5ee68e5b
JC
777 if (root_is_gate)
778 mesh_path_add_gate(mpath);
779
90a5e169
RP
780 rcu_read_unlock();
781}
050ac52c
LCC
782
783
f698d856 784void mesh_rx_path_sel_frame(struct ieee80211_sub_if_data *sdata,
050ac52c
LCC
785 struct ieee80211_mgmt *mgmt,
786 size_t len)
787{
788 struct ieee802_11_elems elems;
789 size_t baselen;
790 u32 last_hop_metric;
97091317 791 struct sta_info *sta;
050ac52c 792
9c80d3dc
JB
793 /* need action_code */
794 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
795 return;
796
97091317
JC
797 rcu_read_lock();
798 sta = sta_info_get(sdata, mgmt->sa);
799 if (!sta || sta->plink_state != NL80211_PLINK_ESTAB) {
800 rcu_read_unlock();
801 return;
802 }
803 rcu_read_unlock();
804
050ac52c
LCC
805 baselen = (u8 *) mgmt->u.action.u.mesh_action.variable - (u8 *) mgmt;
806 ieee802_11_parse_elems(mgmt->u.action.u.mesh_action.variable,
807 len - baselen, &elems);
808
dbb81c42
RP
809 if (elems.preq) {
810 if (elems.preq_len != 37)
050ac52c
LCC
811 /* Right now we support just 1 destination and no AE */
812 return;
dbb81c42
RP
813 last_hop_metric = hwmp_route_info_get(sdata, mgmt, elems.preq,
814 MPATH_PREQ);
815 if (last_hop_metric)
816 hwmp_preq_frame_process(sdata, mgmt, elems.preq,
817 last_hop_metric);
818 }
819 if (elems.prep) {
820 if (elems.prep_len != 31)
050ac52c
LCC
821 /* Right now we support no AE */
822 return;
dbb81c42
RP
823 last_hop_metric = hwmp_route_info_get(sdata, mgmt, elems.prep,
824 MPATH_PREP);
825 if (last_hop_metric)
826 hwmp_prep_frame_process(sdata, mgmt, elems.prep,
827 last_hop_metric);
828 }
829 if (elems.perr) {
d611f062 830 if (elems.perr_len != 15)
050ac52c
LCC
831 /* Right now we support only one destination per PERR */
832 return;
f698d856 833 hwmp_perr_frame_process(sdata, mgmt, elems.perr);
050ac52c 834 }
90a5e169
RP
835 if (elems.rann)
836 hwmp_rann_frame_process(sdata, mgmt, elems.rann);
050ac52c
LCC
837}
838
839/**
840 * mesh_queue_preq - queue a PREQ to a given destination
841 *
842 * @mpath: mesh path to discover
843 * @flags: special attributes of the PREQ to be sent
844 *
845 * Locking: the function must be called from within a rcu read lock block.
846 *
847 */
848static void mesh_queue_preq(struct mesh_path *mpath, u8 flags)
849{
f698d856 850 struct ieee80211_sub_if_data *sdata = mpath->sdata;
472dbc45 851 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
050ac52c
LCC
852 struct mesh_preq_queue *preq_node;
853
59615b5f 854 preq_node = kmalloc(sizeof(struct mesh_preq_queue), GFP_ATOMIC);
050ac52c 855 if (!preq_node) {
7646887a 856 mhwmp_dbg("could not allocate PREQ node");
050ac52c
LCC
857 return;
858 }
859
987dafad 860 spin_lock_bh(&ifmsh->mesh_preq_queue_lock);
472dbc45 861 if (ifmsh->preq_queue_len == MAX_PREQ_QUEUE_LEN) {
987dafad 862 spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
050ac52c
LCC
863 kfree(preq_node);
864 if (printk_ratelimit())
7646887a 865 mhwmp_dbg("PREQ node queue full");
050ac52c
LCC
866 return;
867 }
868
f2dc7989 869 spin_lock(&mpath->state_lock);
f3011cf9 870 if (mpath->flags & MESH_PATH_REQ_QUEUED) {
f2dc7989 871 spin_unlock(&mpath->state_lock);
f3011cf9 872 spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
88d53465 873 kfree(preq_node);
f3011cf9
JC
874 return;
875 }
876
050ac52c
LCC
877 memcpy(preq_node->dst, mpath->dst, ETH_ALEN);
878 preq_node->flags = flags;
879
f3011cf9 880 mpath->flags |= MESH_PATH_REQ_QUEUED;
f2dc7989 881 spin_unlock(&mpath->state_lock);
f3011cf9 882
472dbc45
JB
883 list_add_tail(&preq_node->list, &ifmsh->preq_queue.list);
884 ++ifmsh->preq_queue_len;
987dafad 885 spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
050ac52c 886
472dbc45 887 if (time_after(jiffies, ifmsh->last_preq + min_preq_int_jiff(sdata)))
64592c8f 888 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
050ac52c 889
472dbc45 890 else if (time_before(jiffies, ifmsh->last_preq)) {
050ac52c
LCC
891 /* avoid long wait if did not send preqs for a long time
892 * and jiffies wrapped around
893 */
472dbc45 894 ifmsh->last_preq = jiffies - min_preq_int_jiff(sdata) - 1;
64592c8f 895 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
050ac52c 896 } else
472dbc45 897 mod_timer(&ifmsh->mesh_path_timer, ifmsh->last_preq +
050ac52c
LCC
898 min_preq_int_jiff(sdata));
899}
900
901/**
902 * mesh_path_start_discovery - launch a path discovery from the PREQ queue
903 *
f698d856 904 * @sdata: local mesh subif
050ac52c 905 */
f698d856 906void mesh_path_start_discovery(struct ieee80211_sub_if_data *sdata)
050ac52c 907{
472dbc45 908 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
050ac52c
LCC
909 struct mesh_preq_queue *preq_node;
910 struct mesh_path *mpath;
d19b3bf6 911 u8 ttl, target_flags;
050ac52c
LCC
912 u32 lifetime;
913
a43816df 914 spin_lock_bh(&ifmsh->mesh_preq_queue_lock);
472dbc45
JB
915 if (!ifmsh->preq_queue_len ||
916 time_before(jiffies, ifmsh->last_preq +
050ac52c 917 min_preq_int_jiff(sdata))) {
a43816df 918 spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
050ac52c
LCC
919 return;
920 }
921
472dbc45 922 preq_node = list_first_entry(&ifmsh->preq_queue.list,
050ac52c
LCC
923 struct mesh_preq_queue, list);
924 list_del(&preq_node->list);
472dbc45 925 --ifmsh->preq_queue_len;
a43816df 926 spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
050ac52c
LCC
927
928 rcu_read_lock();
f698d856 929 mpath = mesh_path_lookup(preq_node->dst, sdata);
050ac52c
LCC
930 if (!mpath)
931 goto enddiscovery;
932
933 spin_lock_bh(&mpath->state_lock);
f3011cf9 934 mpath->flags &= ~MESH_PATH_REQ_QUEUED;
050ac52c
LCC
935 if (preq_node->flags & PREQ_Q_F_START) {
936 if (mpath->flags & MESH_PATH_RESOLVING) {
937 spin_unlock_bh(&mpath->state_lock);
938 goto enddiscovery;
939 } else {
940 mpath->flags &= ~MESH_PATH_RESOLVED;
941 mpath->flags |= MESH_PATH_RESOLVING;
942 mpath->discovery_retries = 0;
943 mpath->discovery_timeout = disc_timeout_jiff(sdata);
944 }
945 } else if (!(mpath->flags & MESH_PATH_RESOLVING) ||
946 mpath->flags & MESH_PATH_RESOLVED) {
947 mpath->flags &= ~MESH_PATH_RESOLVING;
948 spin_unlock_bh(&mpath->state_lock);
949 goto enddiscovery;
950 }
951
472dbc45 952 ifmsh->last_preq = jiffies;
050ac52c 953
d19b3bf6 954 if (time_after(jiffies, ifmsh->last_sn_update +
050ac52c 955 net_traversal_jiffies(sdata)) ||
d19b3bf6
RP
956 time_before(jiffies, ifmsh->last_sn_update)) {
957 ++ifmsh->sn;
958 sdata->u.mesh.last_sn_update = jiffies;
050ac52c
LCC
959 }
960 lifetime = default_lifetime(sdata);
45904f21 961 ttl = sdata->u.mesh.mshcfg.element_ttl;
050ac52c 962 if (ttl == 0) {
472dbc45 963 sdata->u.mesh.mshstats.dropped_frames_ttl++;
050ac52c
LCC
964 spin_unlock_bh(&mpath->state_lock);
965 goto enddiscovery;
966 }
967
968 if (preq_node->flags & PREQ_Q_F_REFRESH)
d19b3bf6 969 target_flags = MP_F_DO;
050ac52c 970 else
d19b3bf6 971 target_flags = MP_F_RF;
050ac52c
LCC
972
973 spin_unlock_bh(&mpath->state_lock);
47846c9b 974 mesh_path_sel_frame_tx(MPATH_PREQ, 0, sdata->vif.addr,
d19b3bf6 975 cpu_to_le32(ifmsh->sn), target_flags, mpath->dst,
15ff6365 976 cpu_to_le32(mpath->sn), broadcast_addr, 0,
aa2b5928 977 ttl, cpu_to_le32(lifetime), 0,
472dbc45 978 cpu_to_le32(ifmsh->preq_id++), sdata);
050ac52c
LCC
979 mod_timer(&mpath->timer, jiffies + mpath->discovery_timeout);
980
981enddiscovery:
982 rcu_read_unlock();
983 kfree(preq_node);
984}
985
0cfda851
TP
986/* mesh_nexthop_resolve - lookup next hop for given skb and start path
987 * discovery if no forwarding information is found.
050ac52c 988 *
e32f85f7 989 * @skb: 802.11 frame to be sent
f698d856 990 * @sdata: network subif the frame will be sent through
050ac52c 991 *
0cfda851
TP
992 * Returns: 0 if the next hop was found and -ENOENT if the frame was queued.
993 * skb is freeed here if no mpath could be allocated.
050ac52c 994 */
0cfda851
TP
995int mesh_nexthop_resolve(struct sk_buff *skb,
996 struct ieee80211_sub_if_data *sdata)
050ac52c 997{
e32f85f7 998 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
0cfda851
TP
999 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1000 struct mesh_path *mpath;
1001 struct sk_buff *skb_to_free = NULL;
d19b3bf6 1002 u8 *target_addr = hdr->addr3;
050ac52c
LCC
1003 int err = 0;
1004
1005 rcu_read_lock();
0cfda851
TP
1006 err = mesh_nexthop_lookup(skb, sdata);
1007 if (!err)
1008 goto endlookup;
050ac52c 1009
0cfda851
TP
1010 /* no nexthop found, start resolving */
1011 mpath = mesh_path_lookup(target_addr, sdata);
050ac52c 1012 if (!mpath) {
d19b3bf6
RP
1013 mesh_path_add(target_addr, sdata);
1014 mpath = mesh_path_lookup(target_addr, sdata);
050ac52c 1015 if (!mpath) {
0cfda851 1016 mesh_path_discard_frame(skb, sdata);
050ac52c
LCC
1017 err = -ENOSPC;
1018 goto endlookup;
1019 }
1020 }
1021
0cfda851
TP
1022 if (!(mpath->flags & MESH_PATH_RESOLVING))
1023 mesh_queue_preq(mpath, PREQ_Q_F_START);
1024
1025 if (skb_queue_len(&mpath->frame_queue) >= MESH_FRAME_QUEUE_LEN)
1026 skb_to_free = skb_dequeue(&mpath->frame_queue);
1027
1028 info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
1029 ieee80211_set_qos_hdr(sdata, skb);
1030 skb_queue_tail(&mpath->frame_queue, skb);
1031 err = -ENOENT;
1032 if (skb_to_free)
1033 mesh_path_discard_frame(skb_to_free, sdata);
1034
1035endlookup:
1036 rcu_read_unlock();
1037 return err;
1038}
1039/**
1040 * mesh_nexthop_lookup - put the appropriate next hop on a mesh frame. Calling
1041 * this function is considered "using" the associated mpath, so preempt a path
1042 * refresh if this mpath expires soon.
1043 *
1044 * @skb: 802.11 frame to be sent
1045 * @sdata: network subif the frame will be sent through
1046 *
1047 * Returns: 0 if the next hop was found. Nonzero otherwise.
1048 */
1049int mesh_nexthop_lookup(struct sk_buff *skb,
1050 struct ieee80211_sub_if_data *sdata)
1051{
1052 struct mesh_path *mpath;
1053 struct sta_info *next_hop;
1054 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1055 u8 *target_addr = hdr->addr3;
1056 int err = -ENOENT;
050ac52c 1057
0cfda851
TP
1058 rcu_read_lock();
1059 mpath = mesh_path_lookup(target_addr, sdata);
1060
1061 if (!mpath || !(mpath->flags & MESH_PATH_ACTIVE))
1062 goto endlookup;
1063
1064 if (time_after(jiffies,
1065 mpath->exp_time -
1066 msecs_to_jiffies(sdata->u.mesh.mshcfg.path_refresh_time)) &&
1067 !memcmp(sdata->vif.addr, hdr->addr4, ETH_ALEN) &&
1068 !(mpath->flags & MESH_PATH_RESOLVING) &&
1069 !(mpath->flags & MESH_PATH_FIXED))
1070 mesh_queue_preq(mpath, PREQ_Q_F_START | PREQ_Q_F_REFRESH);
050ac52c 1071
0cfda851
TP
1072 next_hop = rcu_dereference(mpath->next_hop);
1073 if (next_hop) {
1074 memcpy(hdr->addr1, next_hop->sta.addr, ETH_ALEN);
1075 memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN);
1076 err = 0;
050ac52c
LCC
1077 }
1078
1079endlookup:
1080 rcu_read_unlock();
1081 return err;
1082}
1083
1084void mesh_path_timer(unsigned long data)
1085{
dea4096b
JB
1086 struct mesh_path *mpath = (void *) data;
1087 struct ieee80211_sub_if_data *sdata = mpath->sdata;
5ee68e5b 1088 int ret;
5bb644a0 1089
dea4096b 1090 if (sdata->local->quiescing)
5bb644a0 1091 return;
5bb644a0
JB
1092
1093 spin_lock_bh(&mpath->state_lock);
cfa22c71 1094 if (mpath->flags & MESH_PATH_RESOLVED ||
5ee68e5b 1095 (!(mpath->flags & MESH_PATH_RESOLVING))) {
050ac52c 1096 mpath->flags &= ~(MESH_PATH_RESOLVING | MESH_PATH_RESOLVED);
5ee68e5b
JC
1097 spin_unlock_bh(&mpath->state_lock);
1098 } else if (mpath->discovery_retries < max_preq_retries(sdata)) {
050ac52c
LCC
1099 ++mpath->discovery_retries;
1100 mpath->discovery_timeout *= 2;
f3011cf9 1101 mpath->flags &= ~MESH_PATH_REQ_QUEUED;
5ee68e5b 1102 spin_unlock_bh(&mpath->state_lock);
050ac52c
LCC
1103 mesh_queue_preq(mpath, 0);
1104 } else {
1105 mpath->flags = 0;
1106 mpath->exp_time = jiffies;
5ee68e5b
JC
1107 spin_unlock_bh(&mpath->state_lock);
1108 if (!mpath->is_gate && mesh_gate_num(sdata) > 0) {
1109 ret = mesh_path_send_to_gates(mpath);
1110 if (ret)
1111 mhwmp_dbg("no gate was reachable");
1112 } else
1113 mesh_path_flush_pending(mpath);
050ac52c 1114 }
050ac52c 1115}
e304bfd3
RP
1116
1117void
1118mesh_path_tx_root_frame(struct ieee80211_sub_if_data *sdata)
1119{
1120 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
0507e159 1121 u32 interval = ifmsh->mshcfg.dot11MeshHWMPRannInterval;
16dd7267 1122 u8 flags;
e304bfd3 1123
16dd7267
JC
1124 flags = (ifmsh->mshcfg.dot11MeshGateAnnouncementProtocol)
1125 ? RANN_FLAG_IS_GATE : 0;
1126 mesh_path_sel_frame_tx(MPATH_RANN, flags, sdata->vif.addr,
e304bfd3 1127 cpu_to_le32(++ifmsh->sn),
15ff6365 1128 0, NULL, 0, broadcast_addr,
45904f21 1129 0, sdata->u.mesh.mshcfg.element_ttl,
0507e159 1130 cpu_to_le32(interval), 0, 0, sdata);
e304bfd3 1131}