mac80211: mesh hwmp locking fixes
[linux-2.6/x86.git] / net / mac80211 / mesh_hwmp.c
blobc2f40ef418cfd42207df28805e9ea41d68080d07
1 /*
2 * Copyright (c) 2008 open80211s Ltd.
3 * Author: Luis Carlos Cobo <luisca@cozybit.com>
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 */
10 #include "mesh.h"
12 #define IEEE80211_FC(type, stype) cpu_to_le16(type | stype)
14 #define TEST_FRAME_LEN 8192
15 #define MAX_METRIC 0xffffffff
16 #define ARITH_SHIFT 8
18 /* Number of frames buffered per destination for unresolved destinations */
19 #define MESH_FRAME_QUEUE_LEN 10
20 #define MAX_PREQ_QUEUE_LEN 64
22 /* Destination only */
23 #define MP_F_DO 0x1
24 /* Reply and forward */
25 #define MP_F_RF 0x2
27 /* HWMP IE processing macros */
28 #define AE_F (1<<6)
29 #define AE_F_SET(x) (*x & AE_F)
30 #define PREQ_IE_FLAGS(x) (*(x))
31 #define PREQ_IE_HOPCOUNT(x) (*(x + 1))
32 #define PREQ_IE_TTL(x) (*(x + 2))
33 #define PREQ_IE_PREQ_ID(x) le32_to_cpu(*((u32 *) (x + 3)))
34 #define PREQ_IE_ORIG_ADDR(x) (x + 7)
35 #define PREQ_IE_ORIG_DSN(x) le32_to_cpu(*((u32 *) (x + 13)))
36 #define PREQ_IE_LIFETIME(x) le32_to_cpu(*((u32 *) \
37 (AE_F_SET(x) ? x + 23 : x + 17)))
38 #define PREQ_IE_METRIC(x) le32_to_cpu(*((u32 *) \
39 (AE_F_SET(x) ? x + 27 : x + 21)))
40 #define PREQ_IE_DST_F(x) (*(AE_F_SET(x) ? x + 32 : x + 26))
41 #define PREQ_IE_DST_ADDR(x) (AE_F_SET(x) ? x + 33 : x + 27)
42 #define PREQ_IE_DST_DSN(x) le32_to_cpu(*((u32 *) \
43 (AE_F_SET(x) ? x + 39 : x + 33)))
46 #define PREP_IE_FLAGS(x) PREQ_IE_FLAGS(x)
47 #define PREP_IE_HOPCOUNT(x) PREQ_IE_HOPCOUNT(x)
48 #define PREP_IE_TTL(x) PREQ_IE_TTL(x)
49 #define PREP_IE_ORIG_ADDR(x) (x + 3)
50 #define PREP_IE_ORIG_DSN(x) le32_to_cpu(*((u32 *) (x + 9)))
51 #define PREP_IE_LIFETIME(x) le32_to_cpu(*((u32 *) \
52 (AE_F_SET(x) ? x + 19 : x + 13)))
53 #define PREP_IE_METRIC(x) le32_to_cpu(*((u32 *) \
54 (AE_F_SET(x) ? x + 23 : x + 17)))
55 #define PREP_IE_DST_ADDR(x) (AE_F_SET(x) ? x + 27 : x + 21)
56 #define PREP_IE_DST_DSN(x) le32_to_cpu(*((u32 *) \
57 (AE_F_SET(x) ? x + 33 : x + 27)))
59 #define PERR_IE_DST_ADDR(x) (x + 2)
60 #define PERR_IE_DST_DSN(x) le32_to_cpu(*((u32 *) (x + 8)))
62 #define TU_TO_EXP_TIME(x) (jiffies + msecs_to_jiffies(x * 1024 / 1000))
63 #define MSEC_TO_TU(x) (x*1000/1024)
64 #define DSN_GT(x, y) ((long) (y) - (long) (x) < 0)
65 #define DSN_LT(x, y) ((long) (x) - (long) (y) < 0)
67 #define net_traversal_jiffies(s) \
68 msecs_to_jiffies(s->u.sta.mshcfg.dot11MeshHWMPnetDiameterTraversalTime)
69 #define default_lifetime(s) \
70 MSEC_TO_TU(s->u.sta.mshcfg.dot11MeshHWMPactivePathTimeout)
71 #define min_preq_int_jiff(s) \
72 (msecs_to_jiffies(s->u.sta.mshcfg.dot11MeshHWMPpreqMinInterval))
73 #define max_preq_retries(s) (s->u.sta.mshcfg.dot11MeshHWMPmaxPREQretries)
74 #define disc_timeout_jiff(s) \
75 msecs_to_jiffies(sdata->u.sta.mshcfg.min_discovery_timeout)
77 enum mpath_frame_type {
78 MPATH_PREQ = 0,
79 MPATH_PREP,
80 MPATH_PERR
83 static int mesh_path_sel_frame_tx(enum mpath_frame_type action, u8 flags,
84 u8 *orig_addr, __le32 orig_dsn, u8 dst_flags, u8 *dst,
85 __le32 dst_dsn, u8 *da, u8 hop_count, u8 ttl, __le32 lifetime,
86 __le32 metric, __le32 preq_id, struct net_device *dev)
88 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
89 struct sk_buff *skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400);
90 struct ieee80211_mgmt *mgmt;
91 u8 *pos;
92 int ie_len;
94 if (!skb)
95 return -1;
96 skb_reserve(skb, local->hw.extra_tx_headroom);
97 /* 25 is the size of the common mgmt part (24) plus the size of the
98 * common action part (1)
100 mgmt = (struct ieee80211_mgmt *)
101 skb_put(skb, 25 + sizeof(mgmt->u.action.u.mesh_action));
102 memset(mgmt, 0, 25 + sizeof(mgmt->u.action.u.mesh_action));
103 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
104 IEEE80211_STYPE_ACTION);
106 memcpy(mgmt->da, da, ETH_ALEN);
107 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
108 /* BSSID is left zeroed, wildcard value */
109 mgmt->u.action.category = MESH_PATH_SEL_CATEGORY;
110 mgmt->u.action.u.mesh_action.action_code = action;
112 switch (action) {
113 case MPATH_PREQ:
114 ie_len = 37;
115 pos = skb_put(skb, 2 + ie_len);
116 *pos++ = WLAN_EID_PREQ;
117 break;
118 case MPATH_PREP:
119 ie_len = 31;
120 pos = skb_put(skb, 2 + ie_len);
121 *pos++ = WLAN_EID_PREP;
122 break;
123 default:
124 kfree(skb);
125 return -ENOTSUPP;
126 break;
128 *pos++ = ie_len;
129 *pos++ = flags;
130 *pos++ = hop_count;
131 *pos++ = ttl;
132 if (action == MPATH_PREQ) {
133 memcpy(pos, &preq_id, 4);
134 pos += 4;
136 memcpy(pos, orig_addr, ETH_ALEN);
137 pos += ETH_ALEN;
138 memcpy(pos, &orig_dsn, 4);
139 pos += 4;
140 memcpy(pos, &lifetime, 4);
141 pos += 4;
142 memcpy(pos, &metric, 4);
143 pos += 4;
144 if (action == MPATH_PREQ) {
145 /* destination count */
146 *pos++ = 1;
147 *pos++ = dst_flags;
149 memcpy(pos, dst, ETH_ALEN);
150 pos += ETH_ALEN;
151 memcpy(pos, &dst_dsn, 4);
153 ieee80211_sta_tx(dev, skb, 0);
154 return 0;
158 * mesh_send_path error - Sends a PERR mesh management frame
160 * @dst: broken destination
161 * @dst_dsn: dsn of the broken destination
162 * @ra: node this frame is addressed to
164 int mesh_path_error_tx(u8 *dst, __le32 dst_dsn, u8 *ra,
165 struct net_device *dev)
167 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
168 struct sk_buff *skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400);
169 struct ieee80211_mgmt *mgmt;
170 u8 *pos;
171 int ie_len;
173 if (!skb)
174 return -1;
175 skb_reserve(skb, local->hw.extra_tx_headroom);
176 /* 25 is the size of the common mgmt part (24) plus the size of the
177 * common action part (1)
179 mgmt = (struct ieee80211_mgmt *)
180 skb_put(skb, 25 + sizeof(mgmt->u.action.u.mesh_action));
181 memset(mgmt, 0, 25 + sizeof(mgmt->u.action.u.mesh_action));
182 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
183 IEEE80211_STYPE_ACTION);
185 memcpy(mgmt->da, ra, ETH_ALEN);
186 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
187 /* BSSID is left zeroed, wildcard value */
188 mgmt->u.action.category = MESH_PATH_SEL_CATEGORY;
189 mgmt->u.action.u.mesh_action.action_code = MPATH_PERR;
190 ie_len = 12;
191 pos = skb_put(skb, 2 + ie_len);
192 *pos++ = WLAN_EID_PERR;
193 *pos++ = ie_len;
194 /* mode flags, reserved */
195 *pos++ = 0;
196 /* number of destinations */
197 *pos++ = 1;
198 memcpy(pos, dst, ETH_ALEN);
199 pos += ETH_ALEN;
200 memcpy(pos, &dst_dsn, 4);
202 ieee80211_sta_tx(dev, skb, 0);
203 return 0;
206 static u32 airtime_link_metric_get(struct ieee80211_local *local,
207 struct sta_info *sta)
209 struct ieee80211_supported_band *sband;
210 /* This should be adjusted for each device */
211 int device_constant = 1 << ARITH_SHIFT;
212 int test_frame_len = TEST_FRAME_LEN << ARITH_SHIFT;
213 int s_unit = 1 << ARITH_SHIFT;
214 int rate, err;
215 u32 tx_time, estimated_retx;
216 u64 result;
218 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
220 if (sta->fail_avg >= 100)
221 return MAX_METRIC;
222 err = (sta->fail_avg << ARITH_SHIFT) / 100;
224 /* bitrate is in units of 100 Kbps, while we need rate in units of
225 * 1Mbps. This will be corrected on tx_time computation.
227 rate = sband->bitrates[sta->txrate_idx].bitrate;
228 tx_time = (device_constant + 10 * test_frame_len / rate);
229 estimated_retx = ((1 << (2 * ARITH_SHIFT)) / (s_unit - err));
230 result = (tx_time * estimated_retx) >> (2 * ARITH_SHIFT) ;
231 return (u32)result;
235 * hwmp_route_info_get - Update routing info to originator and transmitter
237 * @dev: local mesh interface
238 * @mgmt: mesh management frame
239 * @hwmp_ie: hwmp information element (PREP or PREQ)
241 * This function updates the path routing information to the originator and the
242 * transmitter of a HWMP PREQ or PREP fram.
244 * Returns: metric to frame originator or 0 if the frame should not be further
245 * processed
247 * Notes: this function is the only place (besides user-provided info) where
248 * path routing information is updated.
250 static u32 hwmp_route_info_get(struct net_device *dev,
251 struct ieee80211_mgmt *mgmt,
252 u8 *hwmp_ie)
254 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
255 struct mesh_path *mpath;
256 struct sta_info *sta;
257 bool fresh_info;
258 u8 *orig_addr, *ta;
259 u32 orig_dsn, orig_metric;
260 unsigned long orig_lifetime, exp_time;
261 u32 last_hop_metric, new_metric;
262 bool process = true;
263 u8 action = mgmt->u.action.u.mesh_action.action_code;
265 rcu_read_lock();
266 sta = sta_info_get(local, mgmt->sa);
267 if (!sta) {
268 rcu_read_unlock();
269 return 0;
272 last_hop_metric = airtime_link_metric_get(local, sta);
273 /* Update and check originator routing info */
274 fresh_info = true;
276 switch (action) {
277 case MPATH_PREQ:
278 orig_addr = PREQ_IE_ORIG_ADDR(hwmp_ie);
279 orig_dsn = PREQ_IE_ORIG_DSN(hwmp_ie);
280 orig_lifetime = PREQ_IE_LIFETIME(hwmp_ie);
281 orig_metric = PREQ_IE_METRIC(hwmp_ie);
282 break;
283 case MPATH_PREP:
284 /* Originator here refers to the MP that was the destination in
285 * the Path Request. The draft refers to that MP as the
286 * destination address, even though usually it is the origin of
287 * the PREP frame. We divert from the nomenclature in the draft
288 * so that we can easily use a single function to gather path
289 * information from both PREQ and PREP frames.
291 orig_addr = PREP_IE_ORIG_ADDR(hwmp_ie);
292 orig_dsn = PREP_IE_ORIG_DSN(hwmp_ie);
293 orig_lifetime = PREP_IE_LIFETIME(hwmp_ie);
294 orig_metric = PREP_IE_METRIC(hwmp_ie);
295 break;
296 default:
297 sta_info_put(sta);
298 rcu_read_unlock();
299 return 0;
301 new_metric = orig_metric + last_hop_metric;
302 if (new_metric < orig_metric)
303 new_metric = MAX_METRIC;
304 exp_time = TU_TO_EXP_TIME(orig_lifetime);
306 if (memcmp(orig_addr, dev->dev_addr, ETH_ALEN) == 0) {
307 /* This MP is the originator, we are not interested in this
308 * frame, except for updating transmitter's path info.
310 process = false;
311 fresh_info = false;
312 } else {
313 mpath = mesh_path_lookup(orig_addr, dev);
314 if (mpath) {
315 spin_lock_bh(&mpath->state_lock);
316 if (mpath->flags & MESH_PATH_FIXED)
317 fresh_info = false;
318 else if ((mpath->flags & MESH_PATH_ACTIVE) &&
319 (mpath->flags & MESH_PATH_DSN_VALID)) {
320 if (DSN_GT(mpath->dsn, orig_dsn) ||
321 (mpath->dsn == orig_dsn &&
322 action == MPATH_PREQ &&
323 new_metric > mpath->metric)) {
324 process = false;
325 fresh_info = false;
328 } else {
329 mesh_path_add(orig_addr, dev);
330 mpath = mesh_path_lookup(orig_addr, dev);
331 if (!mpath) {
332 rcu_read_unlock();
333 sta_info_put(sta);
334 return 0;
336 spin_lock_bh(&mpath->state_lock);
339 if (fresh_info) {
340 mesh_path_assign_nexthop(mpath, sta);
341 mpath->flags |= MESH_PATH_DSN_VALID;
342 mpath->metric = new_metric;
343 mpath->dsn = orig_dsn;
344 mpath->exp_time = time_after(mpath->exp_time, exp_time)
345 ? mpath->exp_time : exp_time;
346 mesh_path_activate(mpath);
347 spin_unlock_bh(&mpath->state_lock);
348 mesh_path_tx_pending(mpath);
349 /* draft says preq_id should be saved to, but there does
350 * not seem to be any use for it, skipping by now
352 } else
353 spin_unlock_bh(&mpath->state_lock);
356 /* Update and check transmitter routing info */
357 ta = mgmt->sa;
358 if (memcmp(orig_addr, ta, ETH_ALEN) == 0)
359 fresh_info = false;
360 else {
361 fresh_info = true;
363 mpath = mesh_path_lookup(ta, dev);
364 if (mpath) {
365 spin_lock_bh(&mpath->state_lock);
366 if ((mpath->flags & MESH_PATH_FIXED) ||
367 ((mpath->flags & MESH_PATH_ACTIVE) &&
368 (last_hop_metric > mpath->metric)))
369 fresh_info = false;
370 } else {
371 mesh_path_add(ta, dev);
372 mpath = mesh_path_lookup(ta, dev);
373 if (!mpath) {
374 rcu_read_unlock();
375 sta_info_put(sta);
376 return 0;
378 spin_lock_bh(&mpath->state_lock);
381 if (fresh_info) {
382 mesh_path_assign_nexthop(mpath, sta);
383 mpath->flags &= ~MESH_PATH_DSN_VALID;
384 mpath->metric = last_hop_metric;
385 mpath->exp_time = time_after(mpath->exp_time, exp_time)
386 ? mpath->exp_time : exp_time;
387 mesh_path_activate(mpath);
388 spin_unlock_bh(&mpath->state_lock);
389 mesh_path_tx_pending(mpath);
390 } else
391 spin_unlock_bh(&mpath->state_lock);
394 sta_info_put(sta);
395 rcu_read_unlock();
397 return process ? new_metric : 0;
400 static void hwmp_preq_frame_process(struct net_device *dev,
401 struct ieee80211_mgmt *mgmt,
402 u8 *preq_elem, u32 metric) {
403 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
404 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
405 struct mesh_path *mpath;
406 u8 *dst_addr, *orig_addr;
407 u8 dst_flags, ttl;
408 u32 orig_dsn, dst_dsn, lifetime;
409 bool reply = false;
410 bool forward = true;
412 /* Update destination DSN, if present */
413 dst_addr = PREQ_IE_DST_ADDR(preq_elem);
414 orig_addr = PREQ_IE_ORIG_ADDR(preq_elem);
415 dst_dsn = PREQ_IE_DST_DSN(preq_elem);
416 orig_dsn = PREQ_IE_ORIG_DSN(preq_elem);
417 dst_flags = PREQ_IE_DST_F(preq_elem);
419 if (memcmp(dst_addr, dev->dev_addr, ETH_ALEN) == 0) {
420 forward = false;
421 reply = true;
422 metric = 0;
423 if (time_after(jiffies, ifsta->last_dsn_update +
424 net_traversal_jiffies(sdata)) ||
425 time_before(jiffies, ifsta->last_dsn_update)) {
426 dst_dsn = ++ifsta->dsn;
427 ifsta->last_dsn_update = jiffies;
429 } else {
430 rcu_read_lock();
431 mpath = mesh_path_lookup(dst_addr, dev);
432 if (mpath) {
433 if ((!(mpath->flags & MESH_PATH_DSN_VALID)) ||
434 DSN_LT(mpath->dsn, dst_dsn)) {
435 mpath->dsn = dst_dsn;
436 mpath->flags &= MESH_PATH_DSN_VALID;
437 } else if ((!(dst_flags & MP_F_DO)) &&
438 (mpath->flags & MESH_PATH_ACTIVE)) {
439 reply = true;
440 metric = mpath->metric;
441 dst_dsn = mpath->dsn;
442 if (dst_flags & MP_F_RF)
443 dst_flags |= MP_F_DO;
444 else
445 forward = false;
448 rcu_read_unlock();
451 if (reply) {
452 lifetime = PREQ_IE_LIFETIME(preq_elem);
453 ttl = ifsta->mshcfg.dot11MeshTTL;
454 if (ttl != 0)
455 mesh_path_sel_frame_tx(MPATH_PREP, 0, dst_addr,
456 __cpu_to_le32(dst_dsn), 0, orig_addr,
457 __cpu_to_le32(orig_dsn), mgmt->sa, 0, ttl,
458 __cpu_to_le32(lifetime), __cpu_to_le32(metric),
459 0, dev);
460 else
461 ifsta->mshstats.dropped_frames_ttl++;
464 if (forward) {
465 u32 preq_id;
466 u8 hopcount, flags;
468 ttl = PREQ_IE_TTL(preq_elem);
469 lifetime = PREQ_IE_LIFETIME(preq_elem);
470 if (ttl <= 1) {
471 ifsta->mshstats.dropped_frames_ttl++;
472 return;
474 --ttl;
475 flags = PREQ_IE_FLAGS(preq_elem);
476 preq_id = PREQ_IE_PREQ_ID(preq_elem);
477 hopcount = PREQ_IE_HOPCOUNT(preq_elem) + 1;
478 mesh_path_sel_frame_tx(MPATH_PREQ, flags, orig_addr,
479 __cpu_to_le32(orig_dsn), dst_flags, dst_addr,
480 __cpu_to_le32(dst_dsn), dev->broadcast,
481 hopcount, ttl, __cpu_to_le32(lifetime),
482 __cpu_to_le32(metric), __cpu_to_le32(preq_id),
483 dev);
484 ifsta->mshstats.fwded_frames++;
489 static void hwmp_prep_frame_process(struct net_device *dev,
490 struct ieee80211_mgmt *mgmt,
491 u8 *prep_elem, u32 metric)
493 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
494 struct mesh_path *mpath;
495 u8 *dst_addr, *orig_addr;
496 u8 ttl, hopcount, flags;
497 u8 next_hop[ETH_ALEN];
498 u32 dst_dsn, orig_dsn, lifetime;
500 /* Note that we divert from the draft nomenclature and denominate
501 * destination to what the draft refers to as origininator. So in this
502 * function destnation refers to the final destination of the PREP,
503 * which corresponds with the originator of the PREQ which this PREP
504 * replies
506 dst_addr = PREP_IE_DST_ADDR(prep_elem);
507 if (memcmp(dst_addr, dev->dev_addr, ETH_ALEN) == 0)
508 /* destination, no forwarding required */
509 return;
511 ttl = PREP_IE_TTL(prep_elem);
512 if (ttl <= 1) {
513 sdata->u.sta.mshstats.dropped_frames_ttl++;
514 return;
517 rcu_read_lock();
518 mpath = mesh_path_lookup(dst_addr, dev);
519 if (mpath)
520 spin_lock_bh(&mpath->state_lock);
521 else
522 goto fail;
523 if (!(mpath->flags & MESH_PATH_ACTIVE)) {
524 spin_unlock_bh(&mpath->state_lock);
525 goto fail;
527 memcpy(next_hop, mpath->next_hop->addr, ETH_ALEN);
528 spin_unlock_bh(&mpath->state_lock);
529 --ttl;
530 flags = PREP_IE_FLAGS(prep_elem);
531 lifetime = PREP_IE_LIFETIME(prep_elem);
532 hopcount = PREP_IE_HOPCOUNT(prep_elem) + 1;
533 orig_addr = PREP_IE_ORIG_ADDR(prep_elem);
534 dst_dsn = PREP_IE_DST_DSN(prep_elem);
535 orig_dsn = PREP_IE_ORIG_DSN(prep_elem);
537 mesh_path_sel_frame_tx(MPATH_PREP, flags, orig_addr,
538 __cpu_to_le32(orig_dsn), 0, dst_addr,
539 __cpu_to_le32(dst_dsn), mpath->next_hop->addr, hopcount, ttl,
540 __cpu_to_le32(lifetime), __cpu_to_le32(metric),
541 0, dev);
542 rcu_read_unlock();
543 sdata->u.sta.mshstats.fwded_frames++;
544 return;
546 fail:
547 rcu_read_unlock();
548 sdata->u.sta.mshstats.dropped_frames_no_route++;
549 return;
552 static void hwmp_perr_frame_process(struct net_device *dev,
553 struct ieee80211_mgmt *mgmt, u8 *perr_elem)
555 struct mesh_path *mpath;
556 u8 *ta, *dst_addr;
557 u32 dst_dsn;
559 ta = mgmt->sa;
560 dst_addr = PERR_IE_DST_ADDR(perr_elem);
561 dst_dsn = PERR_IE_DST_DSN(perr_elem);
562 rcu_read_lock();
563 mpath = mesh_path_lookup(dst_addr, dev);
564 if (mpath) {
565 spin_lock_bh(&mpath->state_lock);
566 if (mpath->flags & MESH_PATH_ACTIVE &&
567 memcmp(ta, mpath->next_hop->addr, ETH_ALEN) == 0 &&
568 (!(mpath->flags & MESH_PATH_DSN_VALID) ||
569 DSN_GT(dst_dsn, mpath->dsn))) {
570 mpath->flags &= ~MESH_PATH_ACTIVE;
571 mpath->dsn = dst_dsn;
572 spin_unlock_bh(&mpath->state_lock);
573 mesh_path_error_tx(dst_addr, dst_dsn, dev->broadcast,
574 dev);
575 } else
576 spin_unlock_bh(&mpath->state_lock);
578 rcu_read_unlock();
583 void mesh_rx_path_sel_frame(struct net_device *dev,
584 struct ieee80211_mgmt *mgmt,
585 size_t len)
587 struct ieee802_11_elems elems;
588 size_t baselen;
589 u32 last_hop_metric;
591 baselen = (u8 *) mgmt->u.action.u.mesh_action.variable - (u8 *) mgmt;
592 ieee802_11_parse_elems(mgmt->u.action.u.mesh_action.variable,
593 len - baselen, &elems);
595 switch (mgmt->u.action.u.mesh_action.action_code) {
596 case MPATH_PREQ:
597 if (!elems.preq || elems.preq_len != 37)
598 /* Right now we support just 1 destination and no AE */
599 return;
600 last_hop_metric = hwmp_route_info_get(dev, mgmt, elems.preq);
601 if (!last_hop_metric)
602 return;
603 hwmp_preq_frame_process(dev, mgmt, elems.preq, last_hop_metric);
604 break;
605 case MPATH_PREP:
606 if (!elems.prep || elems.prep_len != 31)
607 /* Right now we support no AE */
608 return;
609 last_hop_metric = hwmp_route_info_get(dev, mgmt, elems.prep);
610 if (!last_hop_metric)
611 return;
612 hwmp_prep_frame_process(dev, mgmt, elems.prep, last_hop_metric);
613 break;
614 case MPATH_PERR:
615 if (!elems.perr || elems.perr_len != 12)
616 /* Right now we support only one destination per PERR */
617 return;
618 hwmp_perr_frame_process(dev, mgmt, elems.perr);
619 default:
620 return;
626 * mesh_queue_preq - queue a PREQ to a given destination
628 * @mpath: mesh path to discover
629 * @flags: special attributes of the PREQ to be sent
631 * Locking: the function must be called from within a rcu read lock block.
634 static void mesh_queue_preq(struct mesh_path *mpath, u8 flags)
636 struct ieee80211_sub_if_data *sdata =
637 IEEE80211_DEV_TO_SUB_IF(mpath->dev);
638 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
639 struct mesh_preq_queue *preq_node;
641 preq_node = kmalloc(sizeof(struct mesh_preq_queue), GFP_KERNEL);
642 if (!preq_node) {
643 printk(KERN_DEBUG "Mesh HWMP: could not allocate PREQ node\n");
644 return;
647 spin_lock(&ifsta->mesh_preq_queue_lock);
648 if (ifsta->preq_queue_len == MAX_PREQ_QUEUE_LEN) {
649 spin_unlock(&ifsta->mesh_preq_queue_lock);
650 kfree(preq_node);
651 if (printk_ratelimit())
652 printk(KERN_DEBUG "Mesh HWMP: PREQ node queue full\n");
653 return;
656 memcpy(preq_node->dst, mpath->dst, ETH_ALEN);
657 preq_node->flags = flags;
659 list_add_tail(&preq_node->list, &ifsta->preq_queue.list);
660 ++ifsta->preq_queue_len;
661 spin_unlock(&ifsta->mesh_preq_queue_lock);
663 if (time_after(jiffies, ifsta->last_preq + min_preq_int_jiff(sdata)))
664 queue_work(sdata->local->hw.workqueue, &ifsta->work);
666 else if (time_before(jiffies, ifsta->last_preq)) {
667 /* avoid long wait if did not send preqs for a long time
668 * and jiffies wrapped around
670 ifsta->last_preq = jiffies - min_preq_int_jiff(sdata) - 1;
671 queue_work(sdata->local->hw.workqueue, &ifsta->work);
672 } else
673 mod_timer(&ifsta->mesh_path_timer, ifsta->last_preq +
674 min_preq_int_jiff(sdata));
678 * mesh_path_start_discovery - launch a path discovery from the PREQ queue
680 * @dev: local mesh interface
682 void mesh_path_start_discovery(struct net_device *dev)
684 struct ieee80211_sub_if_data *sdata =
685 IEEE80211_DEV_TO_SUB_IF(dev);
686 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
687 struct mesh_preq_queue *preq_node;
688 struct mesh_path *mpath;
689 u8 ttl, dst_flags;
690 u32 lifetime;
692 spin_lock(&ifsta->mesh_preq_queue_lock);
693 if (!ifsta->preq_queue_len ||
694 time_before(jiffies, ifsta->last_preq +
695 min_preq_int_jiff(sdata))) {
696 spin_unlock(&ifsta->mesh_preq_queue_lock);
697 return;
700 preq_node = list_first_entry(&ifsta->preq_queue.list,
701 struct mesh_preq_queue, list);
702 list_del(&preq_node->list);
703 --ifsta->preq_queue_len;
704 spin_unlock(&ifsta->mesh_preq_queue_lock);
706 rcu_read_lock();
707 mpath = mesh_path_lookup(preq_node->dst, dev);
708 if (!mpath)
709 goto enddiscovery;
711 spin_lock_bh(&mpath->state_lock);
712 if (preq_node->flags & PREQ_Q_F_START) {
713 if (mpath->flags & MESH_PATH_RESOLVING) {
714 spin_unlock_bh(&mpath->state_lock);
715 goto enddiscovery;
716 } else {
717 mpath->flags &= ~MESH_PATH_RESOLVED;
718 mpath->flags |= MESH_PATH_RESOLVING;
719 mpath->discovery_retries = 0;
720 mpath->discovery_timeout = disc_timeout_jiff(sdata);
722 } else if (!(mpath->flags & MESH_PATH_RESOLVING) ||
723 mpath->flags & MESH_PATH_RESOLVED) {
724 mpath->flags &= ~MESH_PATH_RESOLVING;
725 spin_unlock_bh(&mpath->state_lock);
726 goto enddiscovery;
729 ifsta->last_preq = jiffies;
731 if (time_after(jiffies, ifsta->last_dsn_update +
732 net_traversal_jiffies(sdata)) ||
733 time_before(jiffies, ifsta->last_dsn_update)) {
734 ++ifsta->dsn;
735 sdata->u.sta.last_dsn_update = jiffies;
737 lifetime = default_lifetime(sdata);
738 ttl = sdata->u.sta.mshcfg.dot11MeshTTL;
739 if (ttl == 0) {
740 sdata->u.sta.mshstats.dropped_frames_ttl++;
741 spin_unlock_bh(&mpath->state_lock);
742 goto enddiscovery;
745 if (preq_node->flags & PREQ_Q_F_REFRESH)
746 dst_flags = MP_F_DO;
747 else
748 dst_flags = MP_F_RF;
750 spin_unlock_bh(&mpath->state_lock);
751 mesh_path_sel_frame_tx(MPATH_PREQ, 0, dev->dev_addr,
752 __cpu_to_le32(ifsta->dsn), dst_flags, mpath->dst,
753 __cpu_to_le32(mpath->dsn), dev->broadcast, 0,
754 ttl, __cpu_to_le32(lifetime), 0,
755 __cpu_to_le32(ifsta->preq_id++), dev);
756 mod_timer(&mpath->timer, jiffies + mpath->discovery_timeout);
758 enddiscovery:
759 rcu_read_unlock();
760 kfree(preq_node);
764 * ieee80211s_lookup_nexthop - put the appropriate next hop on a mesh frame
766 * @next_hop: output argument for next hop address
767 * @skb: frame to be sent
768 * @dev: network device the frame will be sent through
770 * Returns: 0 if the next hop was found. Nonzero otherwise. If no next hop is
771 * found, the function will start a path discovery and queue the frame so it is
772 * sent when the path is resolved. This means the caller must not free the skb
773 * in this case.
775 int mesh_nexthop_lookup(u8 *next_hop, struct sk_buff *skb,
776 struct net_device *dev)
778 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
779 struct sk_buff *skb_to_free = NULL;
780 struct mesh_path *mpath;
781 int err = 0;
783 rcu_read_lock();
784 mpath = mesh_path_lookup(skb->data, dev);
786 if (!mpath) {
787 mesh_path_add(skb->data, dev);
788 mpath = mesh_path_lookup(skb->data, dev);
789 if (!mpath) {
790 dev_kfree_skb(skb);
791 sdata->u.sta.mshstats.dropped_frames_no_route++;
792 err = -ENOSPC;
793 goto endlookup;
797 if (mpath->flags & MESH_PATH_ACTIVE) {
798 if (time_after(jiffies, mpath->exp_time -
799 msecs_to_jiffies(sdata->u.sta.mshcfg.path_refresh_time))
800 && skb->pkt_type != PACKET_OTHERHOST
801 && !(mpath->flags & MESH_PATH_RESOLVING)
802 && !(mpath->flags & MESH_PATH_FIXED)) {
803 mesh_queue_preq(mpath,
804 PREQ_Q_F_START | PREQ_Q_F_REFRESH);
806 memcpy(next_hop, mpath->next_hop->addr,
807 ETH_ALEN);
808 } else {
809 if (!(mpath->flags & MESH_PATH_RESOLVING)) {
810 /* Start discovery only if it is not running yet */
811 mesh_queue_preq(mpath, PREQ_Q_F_START);
814 if (skb_queue_len(&mpath->frame_queue) >=
815 MESH_FRAME_QUEUE_LEN) {
816 skb_to_free = mpath->frame_queue.next;
817 skb_unlink(skb_to_free, &mpath->frame_queue);
820 skb_queue_tail(&mpath->frame_queue, skb);
821 if (skb_to_free)
822 mesh_path_discard_frame(skb_to_free, dev);
823 err = -ENOENT;
826 endlookup:
827 rcu_read_unlock();
828 return err;
831 void mesh_path_timer(unsigned long data)
833 struct ieee80211_sub_if_data *sdata;
834 struct mesh_path *mpath;
835 bool delete = false;
837 rcu_read_lock();
838 mpath = (struct mesh_path *) data;
839 mpath = rcu_dereference(mpath);
840 if (!mpath)
841 goto endmpathtimer;
842 spin_lock_bh(&mpath->state_lock);
843 sdata = IEEE80211_DEV_TO_SUB_IF(mpath->dev);
844 if (mpath->flags & MESH_PATH_DELETE) {
845 mpath->flags = 0;
846 delete = true;
847 } else if (mpath->flags & MESH_PATH_RESOLVED ||
848 (!(mpath->flags & MESH_PATH_RESOLVING)))
849 mpath->flags &= ~(MESH_PATH_RESOLVING | MESH_PATH_RESOLVED);
850 else if (mpath->discovery_retries < max_preq_retries(sdata)) {
851 ++mpath->discovery_retries;
852 mpath->discovery_timeout *= 2;
853 mesh_queue_preq(mpath, 0);
854 } else {
855 mpath->flags = 0;
856 mpath->exp_time = jiffies;
857 mesh_path_flush_pending(mpath);
860 spin_unlock_bh(&mpath->state_lock);
861 endmpathtimer:
862 rcu_read_unlock();
863 if (delete)
864 mesh_path_del(mpath->dst, mpath->dev);