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