blob: 7aeba00ac502d0ca1b21993f4874ffd8f3c07f9a [file] [log] [blame]
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001/*
2 * Copyright (c) 2008 open80211s Ltd.
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
10#include "mesh.h"
11
Luis Carlos Cobo050ac522008-02-23 15:17:15 +010012#define TEST_FRAME_LEN 8192
13#define MAX_METRIC 0xffffffff
14#define ARITH_SHIFT 8
15
16/* Number of frames buffered per destination for unresolved destinations */
17#define MESH_FRAME_QUEUE_LEN 10
18#define MAX_PREQ_QUEUE_LEN 64
19
20/* Destination only */
21#define MP_F_DO 0x1
22/* Reply and forward */
23#define MP_F_RF 0x2
24
Luis Carlos Coboa00de5d2008-02-29 17:07:54 -080025static inline u32 u32_field_get(u8 *preq_elem, int offset, bool ae)
26{
27 if (ae)
28 offset += 6;
Harvey Harrisonae7245c2008-05-01 22:19:33 -070029 return get_unaligned_le32(preq_elem + offset);
Luis Carlos Coboa00de5d2008-02-29 17:07:54 -080030}
31
Luis Carlos Cobo050ac522008-02-23 15:17:15 +010032/* HWMP IE processing macros */
Luis Carlos Coboa00de5d2008-02-29 17:07:54 -080033#define AE_F (1<<6)
34#define AE_F_SET(x) (*x & AE_F)
35#define PREQ_IE_FLAGS(x) (*(x))
36#define PREQ_IE_HOPCOUNT(x) (*(x + 1))
37#define PREQ_IE_TTL(x) (*(x + 2))
38#define PREQ_IE_PREQ_ID(x) u32_field_get(x, 3, 0)
39#define PREQ_IE_ORIG_ADDR(x) (x + 7)
40#define PREQ_IE_ORIG_DSN(x) u32_field_get(x, 13, 0);
41#define PREQ_IE_LIFETIME(x) u32_field_get(x, 17, AE_F_SET(x));
42#define PREQ_IE_METRIC(x) u32_field_get(x, 21, AE_F_SET(x));
43#define PREQ_IE_DST_F(x) (*(AE_F_SET(x) ? x + 32 : x + 26))
44#define PREQ_IE_DST_ADDR(x) (AE_F_SET(x) ? x + 33 : x + 27)
45#define PREQ_IE_DST_DSN(x) u32_field_get(x, 33, AE_F_SET(x));
Luis Carlos Cobo050ac522008-02-23 15:17:15 +010046
47
Luis Carlos Coboa00de5d2008-02-29 17:07:54 -080048#define PREP_IE_FLAGS(x) PREQ_IE_FLAGS(x)
49#define PREP_IE_HOPCOUNT(x) PREQ_IE_HOPCOUNT(x)
50#define PREP_IE_TTL(x) PREQ_IE_TTL(x)
51#define PREP_IE_ORIG_ADDR(x) (x + 3)
52#define PREP_IE_ORIG_DSN(x) u32_field_get(x, 9, 0);
53#define PREP_IE_LIFETIME(x) u32_field_get(x, 13, AE_F_SET(x));
54#define PREP_IE_METRIC(x) u32_field_get(x, 17, AE_F_SET(x));
55#define PREP_IE_DST_ADDR(x) (AE_F_SET(x) ? x + 27 : x + 21)
56#define PREP_IE_DST_DSN(x) u32_field_get(x, 27, AE_F_SET(x));
Luis Carlos Cobo050ac522008-02-23 15:17:15 +010057
Luis Carlos Coboa00de5d2008-02-29 17:07:54 -080058#define PERR_IE_DST_ADDR(x) (x + 2)
59#define PERR_IE_DST_DSN(x) u32_field_get(x, 8, 0);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +010060
Luis Carlos Cobo050ac522008-02-23 15:17:15 +010061#define MSEC_TO_TU(x) (x*1000/1024)
62#define DSN_GT(x, y) ((long) (y) - (long) (x) < 0)
63#define DSN_LT(x, y) ((long) (x) - (long) (y) < 0)
64
65#define net_traversal_jiffies(s) \
Johannes Berg472dbc42008-09-11 00:01:49 +020066 msecs_to_jiffies(s->u.mesh.mshcfg.dot11MeshHWMPnetDiameterTraversalTime)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +010067#define default_lifetime(s) \
Johannes Berg472dbc42008-09-11 00:01:49 +020068 MSEC_TO_TU(s->u.mesh.mshcfg.dot11MeshHWMPactivePathTimeout)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +010069#define min_preq_int_jiff(s) \
Johannes Berg472dbc42008-09-11 00:01:49 +020070 (msecs_to_jiffies(s->u.mesh.mshcfg.dot11MeshHWMPpreqMinInterval))
71#define max_preq_retries(s) (s->u.mesh.mshcfg.dot11MeshHWMPmaxPREQretries)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +010072#define disc_timeout_jiff(s) \
Johannes Berg472dbc42008-09-11 00:01:49 +020073 msecs_to_jiffies(sdata->u.mesh.mshcfg.min_discovery_timeout)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +010074
75enum mpath_frame_type {
76 MPATH_PREQ = 0,
77 MPATH_PREP,
78 MPATH_PERR
79};
80
81static int mesh_path_sel_frame_tx(enum mpath_frame_type action, u8 flags,
82 u8 *orig_addr, __le32 orig_dsn, u8 dst_flags, u8 *dst,
83 __le32 dst_dsn, u8 *da, u8 hop_count, u8 ttl, __le32 lifetime,
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +120084 __le32 metric, __le32 preq_id, struct ieee80211_sub_if_data *sdata)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +010085{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +120086 struct ieee80211_local *local = sdata->local;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +010087 struct sk_buff *skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400);
88 struct ieee80211_mgmt *mgmt;
89 u8 *pos;
90 int ie_len;
91
92 if (!skb)
93 return -1;
94 skb_reserve(skb, local->hw.extra_tx_headroom);
95 /* 25 is the size of the common mgmt part (24) plus the size of the
96 * common action part (1)
97 */
98 mgmt = (struct ieee80211_mgmt *)
99 skb_put(skb, 25 + sizeof(mgmt->u.action.u.mesh_action));
100 memset(mgmt, 0, 25 + sizeof(mgmt->u.action.u.mesh_action));
Harvey Harrisone7827a72008-07-15 18:44:13 -0700101 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
102 IEEE80211_STYPE_ACTION);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100103
104 memcpy(mgmt->da, da, ETH_ALEN);
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200105 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100106 /* BSSID is left zeroed, wildcard value */
107 mgmt->u.action.category = MESH_PATH_SEL_CATEGORY;
108 mgmt->u.action.u.mesh_action.action_code = action;
109
110 switch (action) {
111 case MPATH_PREQ:
112 ie_len = 37;
113 pos = skb_put(skb, 2 + ie_len);
114 *pos++ = WLAN_EID_PREQ;
115 break;
116 case MPATH_PREP:
117 ie_len = 31;
118 pos = skb_put(skb, 2 + ie_len);
119 *pos++ = WLAN_EID_PREP;
120 break;
121 default:
Patrick McHardy812714d2008-05-06 12:52:07 +0200122 kfree_skb(skb);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100123 return -ENOTSUPP;
124 break;
125 }
126 *pos++ = ie_len;
127 *pos++ = flags;
128 *pos++ = hop_count;
129 *pos++ = ttl;
130 if (action == MPATH_PREQ) {
131 memcpy(pos, &preq_id, 4);
132 pos += 4;
133 }
134 memcpy(pos, orig_addr, ETH_ALEN);
135 pos += ETH_ALEN;
136 memcpy(pos, &orig_dsn, 4);
137 pos += 4;
138 memcpy(pos, &lifetime, 4);
139 pos += 4;
140 memcpy(pos, &metric, 4);
141 pos += 4;
142 if (action == MPATH_PREQ) {
143 /* destination count */
144 *pos++ = 1;
145 *pos++ = dst_flags;
146 }
147 memcpy(pos, dst, ETH_ALEN);
148 pos += ETH_ALEN;
149 memcpy(pos, &dst_dsn, 4);
150
Jouni Malinen1acc97b2009-01-08 13:32:07 +0200151 ieee80211_tx_skb(sdata, skb, 1);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100152 return 0;
153}
154
155/**
156 * mesh_send_path error - Sends a PERR mesh management frame
157 *
158 * @dst: broken destination
159 * @dst_dsn: dsn of the broken destination
160 * @ra: node this frame is addressed to
161 */
162int mesh_path_error_tx(u8 *dst, __le32 dst_dsn, u8 *ra,
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200163 struct ieee80211_sub_if_data *sdata)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100164{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200165 struct ieee80211_local *local = sdata->local;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100166 struct sk_buff *skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400);
167 struct ieee80211_mgmt *mgmt;
168 u8 *pos;
169 int ie_len;
170
171 if (!skb)
172 return -1;
173 skb_reserve(skb, local->hw.extra_tx_headroom);
174 /* 25 is the size of the common mgmt part (24) plus the size of the
175 * common action part (1)
176 */
177 mgmt = (struct ieee80211_mgmt *)
178 skb_put(skb, 25 + sizeof(mgmt->u.action.u.mesh_action));
179 memset(mgmt, 0, 25 + sizeof(mgmt->u.action.u.mesh_action));
Harvey Harrisone7827a72008-07-15 18:44:13 -0700180 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
181 IEEE80211_STYPE_ACTION);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100182
183 memcpy(mgmt->da, ra, ETH_ALEN);
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200184 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100185 /* BSSID is left zeroed, wildcard value */
186 mgmt->u.action.category = MESH_PATH_SEL_CATEGORY;
187 mgmt->u.action.u.mesh_action.action_code = MPATH_PERR;
188 ie_len = 12;
189 pos = skb_put(skb, 2 + ie_len);
190 *pos++ = WLAN_EID_PERR;
191 *pos++ = ie_len;
192 /* mode flags, reserved */
193 *pos++ = 0;
194 /* number of destinations */
195 *pos++ = 1;
196 memcpy(pos, dst, ETH_ALEN);
197 pos += ETH_ALEN;
198 memcpy(pos, &dst_dsn, 4);
199
Jouni Malinen1acc97b2009-01-08 13:32:07 +0200200 ieee80211_tx_skb(sdata, skb, 1);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100201 return 0;
202}
203
Javier Cardonabfc32e62009-08-17 17:15:55 -0700204void ieee80211s_update_metric(struct ieee80211_local *local,
205 struct sta_info *stainfo, struct sk_buff *skb)
206{
207 struct ieee80211_tx_info *txinfo = IEEE80211_SKB_CB(skb);
208 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
209 int failed;
210
211 if (!ieee80211_is_data(hdr->frame_control))
212 return;
213
214 failed = !(txinfo->flags & IEEE80211_TX_STAT_ACK);
215
216 /* moving average, scaled to 100 */
217 stainfo->fail_avg = ((80 * stainfo->fail_avg + 5) / 100 + 20 * failed);
218 if (stainfo->fail_avg > 95)
219 mesh_plink_broken(stainfo);
220}
221
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100222static u32 airtime_link_metric_get(struct ieee80211_local *local,
223 struct sta_info *sta)
224{
225 struct ieee80211_supported_band *sband;
226 /* This should be adjusted for each device */
227 int device_constant = 1 << ARITH_SHIFT;
228 int test_frame_len = TEST_FRAME_LEN << ARITH_SHIFT;
229 int s_unit = 1 << ARITH_SHIFT;
230 int rate, err;
231 u32 tx_time, estimated_retx;
232 u64 result;
233
234 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
235
236 if (sta->fail_avg >= 100)
237 return MAX_METRIC;
Johannes Berge6a98542008-10-21 12:40:02 +0200238
239 if (sta->last_tx_rate.flags & IEEE80211_TX_RC_MCS)
240 return MAX_METRIC;
241
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100242 err = (sta->fail_avg << ARITH_SHIFT) / 100;
243
244 /* bitrate is in units of 100 Kbps, while we need rate in units of
245 * 1Mbps. This will be corrected on tx_time computation.
246 */
Johannes Berge6a98542008-10-21 12:40:02 +0200247 rate = sband->bitrates[sta->last_tx_rate.idx].bitrate;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100248 tx_time = (device_constant + 10 * test_frame_len / rate);
249 estimated_retx = ((1 << (2 * ARITH_SHIFT)) / (s_unit - err));
250 result = (tx_time * estimated_retx) >> (2 * ARITH_SHIFT) ;
251 return (u32)result;
252}
253
254/**
255 * hwmp_route_info_get - Update routing info to originator and transmitter
256 *
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200257 * @sdata: local mesh subif
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100258 * @mgmt: mesh management frame
259 * @hwmp_ie: hwmp information element (PREP or PREQ)
260 *
261 * This function updates the path routing information to the originator and the
262 * transmitter of a HWMP PREQ or PREP fram.
263 *
264 * Returns: metric to frame originator or 0 if the frame should not be further
265 * processed
266 *
267 * Notes: this function is the only place (besides user-provided info) where
268 * path routing information is updated.
269 */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200270static u32 hwmp_route_info_get(struct ieee80211_sub_if_data *sdata,
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100271 struct ieee80211_mgmt *mgmt,
272 u8 *hwmp_ie)
273{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200274 struct ieee80211_local *local = sdata->local;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100275 struct mesh_path *mpath;
276 struct sta_info *sta;
277 bool fresh_info;
278 u8 *orig_addr, *ta;
279 u32 orig_dsn, orig_metric;
280 unsigned long orig_lifetime, exp_time;
281 u32 last_hop_metric, new_metric;
282 bool process = true;
283 u8 action = mgmt->u.action.u.mesh_action.action_code;
284
285 rcu_read_lock();
286 sta = sta_info_get(local, mgmt->sa);
Johannes Bergdc0b0f72008-02-23 15:17:20 +0100287 if (!sta) {
288 rcu_read_unlock();
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100289 return 0;
Johannes Bergdc0b0f72008-02-23 15:17:20 +0100290 }
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100291
292 last_hop_metric = airtime_link_metric_get(local, sta);
293 /* Update and check originator routing info */
294 fresh_info = true;
295
296 switch (action) {
297 case MPATH_PREQ:
298 orig_addr = PREQ_IE_ORIG_ADDR(hwmp_ie);
299 orig_dsn = PREQ_IE_ORIG_DSN(hwmp_ie);
300 orig_lifetime = PREQ_IE_LIFETIME(hwmp_ie);
301 orig_metric = PREQ_IE_METRIC(hwmp_ie);
302 break;
303 case MPATH_PREP:
304 /* Originator here refers to the MP that was the destination in
305 * the Path Request. The draft refers to that MP as the
306 * destination address, even though usually it is the origin of
307 * the PREP frame. We divert from the nomenclature in the draft
308 * so that we can easily use a single function to gather path
309 * information from both PREQ and PREP frames.
310 */
311 orig_addr = PREP_IE_ORIG_ADDR(hwmp_ie);
312 orig_dsn = PREP_IE_ORIG_DSN(hwmp_ie);
313 orig_lifetime = PREP_IE_LIFETIME(hwmp_ie);
314 orig_metric = PREP_IE_METRIC(hwmp_ie);
315 break;
316 default:
Johannes Bergdc0b0f72008-02-23 15:17:20 +0100317 rcu_read_unlock();
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100318 return 0;
319 }
320 new_metric = orig_metric + last_hop_metric;
321 if (new_metric < orig_metric)
322 new_metric = MAX_METRIC;
323 exp_time = TU_TO_EXP_TIME(orig_lifetime);
324
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200325 if (memcmp(orig_addr, sdata->dev->dev_addr, ETH_ALEN) == 0) {
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100326 /* This MP is the originator, we are not interested in this
327 * frame, except for updating transmitter's path info.
328 */
329 process = false;
330 fresh_info = false;
331 } else {
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200332 mpath = mesh_path_lookup(orig_addr, sdata);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100333 if (mpath) {
334 spin_lock_bh(&mpath->state_lock);
335 if (mpath->flags & MESH_PATH_FIXED)
336 fresh_info = false;
337 else if ((mpath->flags & MESH_PATH_ACTIVE) &&
338 (mpath->flags & MESH_PATH_DSN_VALID)) {
339 if (DSN_GT(mpath->dsn, orig_dsn) ||
340 (mpath->dsn == orig_dsn &&
341 action == MPATH_PREQ &&
342 new_metric > mpath->metric)) {
343 process = false;
344 fresh_info = false;
345 }
346 }
347 } else {
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200348 mesh_path_add(orig_addr, sdata);
349 mpath = mesh_path_lookup(orig_addr, sdata);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100350 if (!mpath) {
351 rcu_read_unlock();
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100352 return 0;
353 }
354 spin_lock_bh(&mpath->state_lock);
355 }
356
357 if (fresh_info) {
358 mesh_path_assign_nexthop(mpath, sta);
359 mpath->flags |= MESH_PATH_DSN_VALID;
360 mpath->metric = new_metric;
361 mpath->dsn = orig_dsn;
362 mpath->exp_time = time_after(mpath->exp_time, exp_time)
363 ? mpath->exp_time : exp_time;
364 mesh_path_activate(mpath);
365 spin_unlock_bh(&mpath->state_lock);
366 mesh_path_tx_pending(mpath);
367 /* draft says preq_id should be saved to, but there does
368 * not seem to be any use for it, skipping by now
369 */
370 } else
371 spin_unlock_bh(&mpath->state_lock);
372 }
373
374 /* Update and check transmitter routing info */
375 ta = mgmt->sa;
376 if (memcmp(orig_addr, ta, ETH_ALEN) == 0)
377 fresh_info = false;
378 else {
379 fresh_info = true;
380
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200381 mpath = mesh_path_lookup(ta, sdata);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100382 if (mpath) {
383 spin_lock_bh(&mpath->state_lock);
384 if ((mpath->flags & MESH_PATH_FIXED) ||
385 ((mpath->flags & MESH_PATH_ACTIVE) &&
386 (last_hop_metric > mpath->metric)))
387 fresh_info = false;
388 } else {
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200389 mesh_path_add(ta, sdata);
390 mpath = mesh_path_lookup(ta, sdata);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100391 if (!mpath) {
392 rcu_read_unlock();
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100393 return 0;
394 }
395 spin_lock_bh(&mpath->state_lock);
396 }
397
398 if (fresh_info) {
399 mesh_path_assign_nexthop(mpath, sta);
400 mpath->flags &= ~MESH_PATH_DSN_VALID;
401 mpath->metric = last_hop_metric;
402 mpath->exp_time = time_after(mpath->exp_time, exp_time)
403 ? mpath->exp_time : exp_time;
404 mesh_path_activate(mpath);
405 spin_unlock_bh(&mpath->state_lock);
406 mesh_path_tx_pending(mpath);
407 } else
408 spin_unlock_bh(&mpath->state_lock);
409 }
410
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100411 rcu_read_unlock();
412
413 return process ? new_metric : 0;
414}
415
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200416static void hwmp_preq_frame_process(struct ieee80211_sub_if_data *sdata,
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100417 struct ieee80211_mgmt *mgmt,
David Woo57ef5dd2009-08-12 11:03:43 -0700418 u8 *preq_elem, u32 metric)
419{
Johannes Berg472dbc42008-09-11 00:01:49 +0200420 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100421 struct mesh_path *mpath;
422 u8 *dst_addr, *orig_addr;
423 u8 dst_flags, ttl;
424 u32 orig_dsn, dst_dsn, lifetime;
425 bool reply = false;
426 bool forward = true;
427
428 /* Update destination DSN, if present */
429 dst_addr = PREQ_IE_DST_ADDR(preq_elem);
430 orig_addr = PREQ_IE_ORIG_ADDR(preq_elem);
431 dst_dsn = PREQ_IE_DST_DSN(preq_elem);
432 orig_dsn = PREQ_IE_ORIG_DSN(preq_elem);
433 dst_flags = PREQ_IE_DST_F(preq_elem);
434
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200435 if (memcmp(dst_addr, sdata->dev->dev_addr, ETH_ALEN) == 0) {
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100436 forward = false;
437 reply = true;
438 metric = 0;
Johannes Berg472dbc42008-09-11 00:01:49 +0200439 if (time_after(jiffies, ifmsh->last_dsn_update +
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100440 net_traversal_jiffies(sdata)) ||
Johannes Berg472dbc42008-09-11 00:01:49 +0200441 time_before(jiffies, ifmsh->last_dsn_update)) {
442 dst_dsn = ++ifmsh->dsn;
443 ifmsh->last_dsn_update = jiffies;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100444 }
445 } else {
446 rcu_read_lock();
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200447 mpath = mesh_path_lookup(dst_addr, sdata);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100448 if (mpath) {
449 if ((!(mpath->flags & MESH_PATH_DSN_VALID)) ||
450 DSN_LT(mpath->dsn, dst_dsn)) {
451 mpath->dsn = dst_dsn;
David Woo57ef5dd2009-08-12 11:03:43 -0700452 mpath->flags |= MESH_PATH_DSN_VALID;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100453 } else if ((!(dst_flags & MP_F_DO)) &&
454 (mpath->flags & MESH_PATH_ACTIVE)) {
455 reply = true;
456 metric = mpath->metric;
457 dst_dsn = mpath->dsn;
458 if (dst_flags & MP_F_RF)
459 dst_flags |= MP_F_DO;
460 else
461 forward = false;
462 }
463 }
464 rcu_read_unlock();
465 }
466
467 if (reply) {
468 lifetime = PREQ_IE_LIFETIME(preq_elem);
Johannes Berg472dbc42008-09-11 00:01:49 +0200469 ttl = ifmsh->mshcfg.dot11MeshTTL;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100470 if (ttl != 0)
471 mesh_path_sel_frame_tx(MPATH_PREP, 0, dst_addr,
Luis Carlos Coboaa2b5922008-02-29 14:30:32 -0800472 cpu_to_le32(dst_dsn), 0, orig_addr,
473 cpu_to_le32(orig_dsn), mgmt->sa, 0, ttl,
474 cpu_to_le32(lifetime), cpu_to_le32(metric),
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200475 0, sdata);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100476 else
Johannes Berg472dbc42008-09-11 00:01:49 +0200477 ifmsh->mshstats.dropped_frames_ttl++;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100478 }
479
480 if (forward) {
481 u32 preq_id;
482 u8 hopcount, flags;
483
484 ttl = PREQ_IE_TTL(preq_elem);
485 lifetime = PREQ_IE_LIFETIME(preq_elem);
486 if (ttl <= 1) {
Johannes Berg472dbc42008-09-11 00:01:49 +0200487 ifmsh->mshstats.dropped_frames_ttl++;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100488 return;
489 }
490 --ttl;
491 flags = PREQ_IE_FLAGS(preq_elem);
492 preq_id = PREQ_IE_PREQ_ID(preq_elem);
493 hopcount = PREQ_IE_HOPCOUNT(preq_elem) + 1;
494 mesh_path_sel_frame_tx(MPATH_PREQ, flags, orig_addr,
Luis Carlos Coboaa2b5922008-02-29 14:30:32 -0800495 cpu_to_le32(orig_dsn), dst_flags, dst_addr,
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200496 cpu_to_le32(dst_dsn), sdata->dev->broadcast,
Luis Carlos Coboaa2b5922008-02-29 14:30:32 -0800497 hopcount, ttl, cpu_to_le32(lifetime),
498 cpu_to_le32(metric), cpu_to_le32(preq_id),
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200499 sdata);
Johannes Berg472dbc42008-09-11 00:01:49 +0200500 ifmsh->mshstats.fwded_frames++;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100501 }
502}
503
504
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200505static void hwmp_prep_frame_process(struct ieee80211_sub_if_data *sdata,
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100506 struct ieee80211_mgmt *mgmt,
507 u8 *prep_elem, u32 metric)
508{
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100509 struct mesh_path *mpath;
510 u8 *dst_addr, *orig_addr;
511 u8 ttl, hopcount, flags;
512 u8 next_hop[ETH_ALEN];
513 u32 dst_dsn, orig_dsn, lifetime;
514
515 /* Note that we divert from the draft nomenclature and denominate
516 * destination to what the draft refers to as origininator. So in this
517 * function destnation refers to the final destination of the PREP,
518 * which corresponds with the originator of the PREQ which this PREP
519 * replies
520 */
521 dst_addr = PREP_IE_DST_ADDR(prep_elem);
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200522 if (memcmp(dst_addr, sdata->dev->dev_addr, ETH_ALEN) == 0)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100523 /* destination, no forwarding required */
524 return;
525
526 ttl = PREP_IE_TTL(prep_elem);
527 if (ttl <= 1) {
Johannes Berg472dbc42008-09-11 00:01:49 +0200528 sdata->u.mesh.mshstats.dropped_frames_ttl++;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100529 return;
530 }
531
532 rcu_read_lock();
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200533 mpath = mesh_path_lookup(dst_addr, sdata);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100534 if (mpath)
535 spin_lock_bh(&mpath->state_lock);
536 else
537 goto fail;
538 if (!(mpath->flags & MESH_PATH_ACTIVE)) {
539 spin_unlock_bh(&mpath->state_lock);
540 goto fail;
541 }
Johannes Berg17741cd2008-09-11 00:02:02 +0200542 memcpy(next_hop, mpath->next_hop->sta.addr, ETH_ALEN);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100543 spin_unlock_bh(&mpath->state_lock);
544 --ttl;
545 flags = PREP_IE_FLAGS(prep_elem);
546 lifetime = PREP_IE_LIFETIME(prep_elem);
547 hopcount = PREP_IE_HOPCOUNT(prep_elem) + 1;
548 orig_addr = PREP_IE_ORIG_ADDR(prep_elem);
549 dst_dsn = PREP_IE_DST_DSN(prep_elem);
550 orig_dsn = PREP_IE_ORIG_DSN(prep_elem);
551
552 mesh_path_sel_frame_tx(MPATH_PREP, flags, orig_addr,
Luis Carlos Coboaa2b5922008-02-29 14:30:32 -0800553 cpu_to_le32(orig_dsn), 0, dst_addr,
Johannes Berg17741cd2008-09-11 00:02:02 +0200554 cpu_to_le32(dst_dsn), mpath->next_hop->sta.addr, hopcount, ttl,
Luis Carlos Coboaa2b5922008-02-29 14:30:32 -0800555 cpu_to_le32(lifetime), cpu_to_le32(metric),
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200556 0, sdata);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100557 rcu_read_unlock();
Johannes Berg472dbc42008-09-11 00:01:49 +0200558 sdata->u.mesh.mshstats.fwded_frames++;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100559 return;
560
561fail:
562 rcu_read_unlock();
Johannes Berg472dbc42008-09-11 00:01:49 +0200563 sdata->u.mesh.mshstats.dropped_frames_no_route++;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100564 return;
565}
566
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200567static void hwmp_perr_frame_process(struct ieee80211_sub_if_data *sdata,
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100568 struct ieee80211_mgmt *mgmt, u8 *perr_elem)
569{
570 struct mesh_path *mpath;
571 u8 *ta, *dst_addr;
572 u32 dst_dsn;
573
574 ta = mgmt->sa;
575 dst_addr = PERR_IE_DST_ADDR(perr_elem);
576 dst_dsn = PERR_IE_DST_DSN(perr_elem);
577 rcu_read_lock();
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200578 mpath = mesh_path_lookup(dst_addr, sdata);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100579 if (mpath) {
580 spin_lock_bh(&mpath->state_lock);
581 if (mpath->flags & MESH_PATH_ACTIVE &&
Johannes Berg17741cd2008-09-11 00:02:02 +0200582 memcmp(ta, mpath->next_hop->sta.addr, ETH_ALEN) == 0 &&
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100583 (!(mpath->flags & MESH_PATH_DSN_VALID) ||
584 DSN_GT(dst_dsn, mpath->dsn))) {
585 mpath->flags &= ~MESH_PATH_ACTIVE;
586 mpath->dsn = dst_dsn;
587 spin_unlock_bh(&mpath->state_lock);
Luis Carlos Coboaa2b5922008-02-29 14:30:32 -0800588 mesh_path_error_tx(dst_addr, cpu_to_le32(dst_dsn),
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200589 sdata->dev->broadcast, sdata);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100590 } else
591 spin_unlock_bh(&mpath->state_lock);
592 }
593 rcu_read_unlock();
594}
595
596
597
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200598void mesh_rx_path_sel_frame(struct ieee80211_sub_if_data *sdata,
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100599 struct ieee80211_mgmt *mgmt,
600 size_t len)
601{
602 struct ieee802_11_elems elems;
603 size_t baselen;
604 u32 last_hop_metric;
605
Johannes Berg9c80d3d2008-09-08 15:41:59 +0200606 /* need action_code */
607 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
608 return;
609
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100610 baselen = (u8 *) mgmt->u.action.u.mesh_action.variable - (u8 *) mgmt;
611 ieee802_11_parse_elems(mgmt->u.action.u.mesh_action.variable,
612 len - baselen, &elems);
613
614 switch (mgmt->u.action.u.mesh_action.action_code) {
615 case MPATH_PREQ:
616 if (!elems.preq || elems.preq_len != 37)
617 /* Right now we support just 1 destination and no AE */
618 return;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200619 last_hop_metric = hwmp_route_info_get(sdata, mgmt, elems.preq);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100620 if (!last_hop_metric)
621 return;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200622 hwmp_preq_frame_process(sdata, mgmt, elems.preq, last_hop_metric);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100623 break;
624 case MPATH_PREP:
625 if (!elems.prep || elems.prep_len != 31)
626 /* Right now we support no AE */
627 return;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200628 last_hop_metric = hwmp_route_info_get(sdata, mgmt, elems.prep);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100629 if (!last_hop_metric)
630 return;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200631 hwmp_prep_frame_process(sdata, mgmt, elems.prep, last_hop_metric);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100632 break;
633 case MPATH_PERR:
634 if (!elems.perr || elems.perr_len != 12)
635 /* Right now we support only one destination per PERR */
636 return;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200637 hwmp_perr_frame_process(sdata, mgmt, elems.perr);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100638 default:
639 return;
640 }
641
642}
643
644/**
645 * mesh_queue_preq - queue a PREQ to a given destination
646 *
647 * @mpath: mesh path to discover
648 * @flags: special attributes of the PREQ to be sent
649 *
650 * Locking: the function must be called from within a rcu read lock block.
651 *
652 */
653static void mesh_queue_preq(struct mesh_path *mpath, u8 flags)
654{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200655 struct ieee80211_sub_if_data *sdata = mpath->sdata;
Johannes Berg472dbc42008-09-11 00:01:49 +0200656 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100657 struct mesh_preq_queue *preq_node;
658
Andrey Yurovsky59615b52009-06-25 16:07:42 -0700659 preq_node = kmalloc(sizeof(struct mesh_preq_queue), GFP_ATOMIC);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100660 if (!preq_node) {
661 printk(KERN_DEBUG "Mesh HWMP: could not allocate PREQ node\n");
662 return;
663 }
664
Johannes Berg472dbc42008-09-11 00:01:49 +0200665 spin_lock(&ifmsh->mesh_preq_queue_lock);
666 if (ifmsh->preq_queue_len == MAX_PREQ_QUEUE_LEN) {
667 spin_unlock(&ifmsh->mesh_preq_queue_lock);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100668 kfree(preq_node);
669 if (printk_ratelimit())
670 printk(KERN_DEBUG "Mesh HWMP: PREQ node queue full\n");
671 return;
672 }
673
674 memcpy(preq_node->dst, mpath->dst, ETH_ALEN);
675 preq_node->flags = flags;
676
Johannes Berg472dbc42008-09-11 00:01:49 +0200677 list_add_tail(&preq_node->list, &ifmsh->preq_queue.list);
678 ++ifmsh->preq_queue_len;
679 spin_unlock(&ifmsh->mesh_preq_queue_lock);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100680
Johannes Berg472dbc42008-09-11 00:01:49 +0200681 if (time_after(jiffies, ifmsh->last_preq + min_preq_int_jiff(sdata)))
Luis R. Rodriguezc03e20f2009-08-04 15:06:26 -0700682 ieee80211_queue_work(&sdata->local->hw, &ifmsh->work);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100683
Johannes Berg472dbc42008-09-11 00:01:49 +0200684 else if (time_before(jiffies, ifmsh->last_preq)) {
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100685 /* avoid long wait if did not send preqs for a long time
686 * and jiffies wrapped around
687 */
Johannes Berg472dbc42008-09-11 00:01:49 +0200688 ifmsh->last_preq = jiffies - min_preq_int_jiff(sdata) - 1;
Luis R. Rodriguezc03e20f2009-08-04 15:06:26 -0700689 ieee80211_queue_work(&sdata->local->hw, &ifmsh->work);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100690 } else
Johannes Berg472dbc42008-09-11 00:01:49 +0200691 mod_timer(&ifmsh->mesh_path_timer, ifmsh->last_preq +
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100692 min_preq_int_jiff(sdata));
693}
694
695/**
696 * mesh_path_start_discovery - launch a path discovery from the PREQ queue
697 *
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200698 * @sdata: local mesh subif
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100699 */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200700void mesh_path_start_discovery(struct ieee80211_sub_if_data *sdata)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100701{
Johannes Berg472dbc42008-09-11 00:01:49 +0200702 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100703 struct mesh_preq_queue *preq_node;
704 struct mesh_path *mpath;
705 u8 ttl, dst_flags;
706 u32 lifetime;
707
Johannes Berga43816d2009-07-10 11:39:26 +0200708 spin_lock_bh(&ifmsh->mesh_preq_queue_lock);
Johannes Berg472dbc42008-09-11 00:01:49 +0200709 if (!ifmsh->preq_queue_len ||
710 time_before(jiffies, ifmsh->last_preq +
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100711 min_preq_int_jiff(sdata))) {
Johannes Berga43816d2009-07-10 11:39:26 +0200712 spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100713 return;
714 }
715
Johannes Berg472dbc42008-09-11 00:01:49 +0200716 preq_node = list_first_entry(&ifmsh->preq_queue.list,
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100717 struct mesh_preq_queue, list);
718 list_del(&preq_node->list);
Johannes Berg472dbc42008-09-11 00:01:49 +0200719 --ifmsh->preq_queue_len;
Johannes Berga43816d2009-07-10 11:39:26 +0200720 spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100721
722 rcu_read_lock();
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200723 mpath = mesh_path_lookup(preq_node->dst, sdata);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100724 if (!mpath)
725 goto enddiscovery;
726
727 spin_lock_bh(&mpath->state_lock);
728 if (preq_node->flags & PREQ_Q_F_START) {
729 if (mpath->flags & MESH_PATH_RESOLVING) {
730 spin_unlock_bh(&mpath->state_lock);
731 goto enddiscovery;
732 } else {
733 mpath->flags &= ~MESH_PATH_RESOLVED;
734 mpath->flags |= MESH_PATH_RESOLVING;
735 mpath->discovery_retries = 0;
736 mpath->discovery_timeout = disc_timeout_jiff(sdata);
737 }
738 } else if (!(mpath->flags & MESH_PATH_RESOLVING) ||
739 mpath->flags & MESH_PATH_RESOLVED) {
740 mpath->flags &= ~MESH_PATH_RESOLVING;
741 spin_unlock_bh(&mpath->state_lock);
742 goto enddiscovery;
743 }
744
Johannes Berg472dbc42008-09-11 00:01:49 +0200745 ifmsh->last_preq = jiffies;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100746
Johannes Berg472dbc42008-09-11 00:01:49 +0200747 if (time_after(jiffies, ifmsh->last_dsn_update +
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100748 net_traversal_jiffies(sdata)) ||
Johannes Berg472dbc42008-09-11 00:01:49 +0200749 time_before(jiffies, ifmsh->last_dsn_update)) {
750 ++ifmsh->dsn;
751 sdata->u.mesh.last_dsn_update = jiffies;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100752 }
753 lifetime = default_lifetime(sdata);
Johannes Berg472dbc42008-09-11 00:01:49 +0200754 ttl = sdata->u.mesh.mshcfg.dot11MeshTTL;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100755 if (ttl == 0) {
Johannes Berg472dbc42008-09-11 00:01:49 +0200756 sdata->u.mesh.mshstats.dropped_frames_ttl++;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100757 spin_unlock_bh(&mpath->state_lock);
758 goto enddiscovery;
759 }
760
761 if (preq_node->flags & PREQ_Q_F_REFRESH)
762 dst_flags = MP_F_DO;
763 else
764 dst_flags = MP_F_RF;
765
766 spin_unlock_bh(&mpath->state_lock);
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200767 mesh_path_sel_frame_tx(MPATH_PREQ, 0, sdata->dev->dev_addr,
Johannes Berg472dbc42008-09-11 00:01:49 +0200768 cpu_to_le32(ifmsh->dsn), dst_flags, mpath->dst,
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200769 cpu_to_le32(mpath->dsn), sdata->dev->broadcast, 0,
Luis Carlos Coboaa2b5922008-02-29 14:30:32 -0800770 ttl, cpu_to_le32(lifetime), 0,
Johannes Berg472dbc42008-09-11 00:01:49 +0200771 cpu_to_le32(ifmsh->preq_id++), sdata);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100772 mod_timer(&mpath->timer, jiffies + mpath->discovery_timeout);
773
774enddiscovery:
775 rcu_read_unlock();
776 kfree(preq_node);
777}
778
779/**
Rami Rosen2182b832009-01-19 13:50:37 +0200780 * mesh_nexthop_lookup - put the appropriate next hop on a mesh frame
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100781 *
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +0200782 * @skb: 802.11 frame to be sent
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200783 * @sdata: network subif the frame will be sent through
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100784 *
785 * Returns: 0 if the next hop was found. Nonzero otherwise. If no next hop is
786 * found, the function will start a path discovery and queue the frame so it is
787 * sent when the path is resolved. This means the caller must not free the skb
788 * in this case.
789 */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200790int mesh_nexthop_lookup(struct sk_buff *skb,
791 struct ieee80211_sub_if_data *sdata)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100792{
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100793 struct sk_buff *skb_to_free = NULL;
794 struct mesh_path *mpath;
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +0200795 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
796 u8 *dst_addr = hdr->addr3;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100797 int err = 0;
798
799 rcu_read_lock();
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200800 mpath = mesh_path_lookup(dst_addr, sdata);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100801
802 if (!mpath) {
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200803 mesh_path_add(dst_addr, sdata);
804 mpath = mesh_path_lookup(dst_addr, sdata);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100805 if (!mpath) {
Johannes Berg472dbc42008-09-11 00:01:49 +0200806 sdata->u.mesh.mshstats.dropped_frames_no_route++;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100807 err = -ENOSPC;
808 goto endlookup;
809 }
810 }
811
812 if (mpath->flags & MESH_PATH_ACTIVE) {
Andrey Yurovskya9e30912009-08-10 12:15:47 -0700813 if (time_after(jiffies, mpath->exp_time +
Johannes Berg472dbc42008-09-11 00:01:49 +0200814 msecs_to_jiffies(sdata->u.mesh.mshcfg.path_refresh_time))
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200815 && !memcmp(sdata->dev->dev_addr, hdr->addr4,
816 ETH_ALEN)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100817 && !(mpath->flags & MESH_PATH_RESOLVING)
818 && !(mpath->flags & MESH_PATH_FIXED)) {
819 mesh_queue_preq(mpath,
820 PREQ_Q_F_START | PREQ_Q_F_REFRESH);
821 }
Johannes Berg17741cd2008-09-11 00:02:02 +0200822 memcpy(hdr->addr1, mpath->next_hop->sta.addr,
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100823 ETH_ALEN);
824 } else {
Javier Cardona249b4052009-07-07 10:55:03 -0700825 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100826 if (!(mpath->flags & MESH_PATH_RESOLVING)) {
827 /* Start discovery only if it is not running yet */
828 mesh_queue_preq(mpath, PREQ_Q_F_START);
829 }
830
831 if (skb_queue_len(&mpath->frame_queue) >=
Javier Cardonafe583432009-08-10 12:15:46 -0700832 MESH_FRAME_QUEUE_LEN)
833 skb_to_free = skb_dequeue(&mpath->frame_queue);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100834
Javier Cardona249b4052009-07-07 10:55:03 -0700835 info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100836 skb_queue_tail(&mpath->frame_queue, skb);
837 if (skb_to_free)
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200838 mesh_path_discard_frame(skb_to_free, sdata);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100839 err = -ENOENT;
840 }
841
842endlookup:
843 rcu_read_unlock();
844 return err;
845}
846
847void mesh_path_timer(unsigned long data)
848{
849 struct ieee80211_sub_if_data *sdata;
850 struct mesh_path *mpath;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100851
852 rcu_read_lock();
853 mpath = (struct mesh_path *) data;
854 mpath = rcu_dereference(mpath);
855 if (!mpath)
856 goto endmpathtimer;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200857 sdata = mpath->sdata;
Johannes Berg5bb644a2009-05-17 11:40:42 +0200858
859 if (sdata->local->quiescing) {
860 rcu_read_unlock();
861 return;
862 }
863
864 spin_lock_bh(&mpath->state_lock);
Luis Carlos Cobocfa22c72008-02-29 15:04:13 -0800865 if (mpath->flags & MESH_PATH_RESOLVED ||
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100866 (!(mpath->flags & MESH_PATH_RESOLVING)))
867 mpath->flags &= ~(MESH_PATH_RESOLVING | MESH_PATH_RESOLVED);
868 else if (mpath->discovery_retries < max_preq_retries(sdata)) {
869 ++mpath->discovery_retries;
870 mpath->discovery_timeout *= 2;
871 mesh_queue_preq(mpath, 0);
872 } else {
873 mpath->flags = 0;
874 mpath->exp_time = jiffies;
875 mesh_path_flush_pending(mpath);
876 }
877
878 spin_unlock_bh(&mpath->state_lock);
879endmpathtimer:
880 rcu_read_unlock();
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100881}