Merge remote-tracking branch 'sysctl/master'
[linux-2.6/next.git] / net / mac80211 / mesh_hwmp.c
blob9c3c0b86a740bfdd0bf611d3fe35d8b44d841683
1 /*
2 * Copyright (c) 2008, 2009 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 <linux/slab.h>
11 #include "mesh.h"
13 #ifdef CONFIG_MAC80211_VERBOSE_MHWMP_DEBUG
14 #define mhwmp_dbg(fmt, args...) printk(KERN_DEBUG "Mesh HWMP: " fmt, ##args)
15 #else
16 #define mhwmp_dbg(fmt, args...) do { (void)(0); } while (0)
17 #endif
19 #define TEST_FRAME_LEN 8192
20 #define MAX_METRIC 0xffffffff
21 #define ARITH_SHIFT 8
23 /* Number of frames buffered per destination for unresolved destinations */
24 #define MESH_FRAME_QUEUE_LEN 10
25 #define MAX_PREQ_QUEUE_LEN 64
27 /* Destination only */
28 #define MP_F_DO 0x1
29 /* Reply and forward */
30 #define MP_F_RF 0x2
31 /* Unknown Sequence Number */
32 #define MP_F_USN 0x01
33 /* Reason code Present */
34 #define MP_F_RCODE 0x02
36 static void mesh_queue_preq(struct mesh_path *, u8);
38 static inline u32 u32_field_get(u8 *preq_elem, int offset, bool ae)
40 if (ae)
41 offset += 6;
42 return get_unaligned_le32(preq_elem + offset);
45 static inline u32 u16_field_get(u8 *preq_elem, int offset, bool ae)
47 if (ae)
48 offset += 6;
49 return get_unaligned_le16(preq_elem + offset);
52 /* HWMP IE processing macros */
53 #define AE_F (1<<6)
54 #define AE_F_SET(x) (*x & AE_F)
55 #define PREQ_IE_FLAGS(x) (*(x))
56 #define PREQ_IE_HOPCOUNT(x) (*(x + 1))
57 #define PREQ_IE_TTL(x) (*(x + 2))
58 #define PREQ_IE_PREQ_ID(x) u32_field_get(x, 3, 0)
59 #define PREQ_IE_ORIG_ADDR(x) (x + 7)
60 #define PREQ_IE_ORIG_SN(x) u32_field_get(x, 13, 0)
61 #define PREQ_IE_LIFETIME(x) u32_field_get(x, 17, AE_F_SET(x))
62 #define PREQ_IE_METRIC(x) u32_field_get(x, 21, AE_F_SET(x))
63 #define PREQ_IE_TARGET_F(x) (*(AE_F_SET(x) ? x + 32 : x + 26))
64 #define PREQ_IE_TARGET_ADDR(x) (AE_F_SET(x) ? x + 33 : x + 27)
65 #define PREQ_IE_TARGET_SN(x) u32_field_get(x, 33, AE_F_SET(x))
68 #define PREP_IE_FLAGS(x) PREQ_IE_FLAGS(x)
69 #define PREP_IE_HOPCOUNT(x) PREQ_IE_HOPCOUNT(x)
70 #define PREP_IE_TTL(x) PREQ_IE_TTL(x)
71 #define PREP_IE_ORIG_ADDR(x) (AE_F_SET(x) ? x + 27 : x + 21)
72 #define PREP_IE_ORIG_SN(x) u32_field_get(x, 27, AE_F_SET(x))
73 #define PREP_IE_LIFETIME(x) u32_field_get(x, 13, AE_F_SET(x))
74 #define PREP_IE_METRIC(x) u32_field_get(x, 17, AE_F_SET(x))
75 #define PREP_IE_TARGET_ADDR(x) (x + 3)
76 #define PREP_IE_TARGET_SN(x) u32_field_get(x, 9, 0)
78 #define PERR_IE_TTL(x) (*(x))
79 #define PERR_IE_TARGET_FLAGS(x) (*(x + 2))
80 #define PERR_IE_TARGET_ADDR(x) (x + 3)
81 #define PERR_IE_TARGET_SN(x) u32_field_get(x, 9, 0)
82 #define PERR_IE_TARGET_RCODE(x) u16_field_get(x, 13, 0)
84 #define MSEC_TO_TU(x) (x*1000/1024)
85 #define SN_GT(x, y) ((long) (y) - (long) (x) < 0)
86 #define SN_LT(x, y) ((long) (x) - (long) (y) < 0)
88 #define net_traversal_jiffies(s) \
89 msecs_to_jiffies(s->u.mesh.mshcfg.dot11MeshHWMPnetDiameterTraversalTime)
90 #define default_lifetime(s) \
91 MSEC_TO_TU(s->u.mesh.mshcfg.dot11MeshHWMPactivePathTimeout)
92 #define min_preq_int_jiff(s) \
93 (msecs_to_jiffies(s->u.mesh.mshcfg.dot11MeshHWMPpreqMinInterval))
94 #define max_preq_retries(s) (s->u.mesh.mshcfg.dot11MeshHWMPmaxPREQretries)
95 #define disc_timeout_jiff(s) \
96 msecs_to_jiffies(sdata->u.mesh.mshcfg.min_discovery_timeout)
98 enum mpath_frame_type {
99 MPATH_PREQ = 0,
100 MPATH_PREP,
101 MPATH_PERR,
102 MPATH_RANN
105 static const u8 broadcast_addr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
107 static int mesh_path_sel_frame_tx(enum mpath_frame_type action, u8 flags,
108 u8 *orig_addr, __le32 orig_sn, u8 target_flags, u8 *target,
109 __le32 target_sn, const u8 *da, u8 hop_count, u8 ttl,
110 __le32 lifetime, __le32 metric, __le32 preq_id,
111 struct ieee80211_sub_if_data *sdata)
113 struct ieee80211_local *local = sdata->local;
114 struct sk_buff *skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400);
115 struct ieee80211_mgmt *mgmt;
116 u8 *pos;
117 int ie_len;
119 if (!skb)
120 return -1;
121 skb_reserve(skb, local->hw.extra_tx_headroom);
122 /* 25 is the size of the common mgmt part (24) plus the size of the
123 * common action part (1)
125 mgmt = (struct ieee80211_mgmt *)
126 skb_put(skb, 25 + sizeof(mgmt->u.action.u.mesh_action));
127 memset(mgmt, 0, 25 + sizeof(mgmt->u.action.u.mesh_action));
128 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
129 IEEE80211_STYPE_ACTION);
131 memcpy(mgmt->da, da, ETH_ALEN);
132 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
133 /* BSSID == SA */
134 memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
135 mgmt->u.action.category = WLAN_CATEGORY_MESH_ACTION;
136 mgmt->u.action.u.mesh_action.action_code =
137 WLAN_MESH_ACTION_HWMP_PATH_SELECTION;
139 switch (action) {
140 case MPATH_PREQ:
141 mhwmp_dbg("sending PREQ to %pM\n", target);
142 ie_len = 37;
143 pos = skb_put(skb, 2 + ie_len);
144 *pos++ = WLAN_EID_PREQ;
145 break;
146 case MPATH_PREP:
147 mhwmp_dbg("sending PREP to %pM\n", target);
148 ie_len = 31;
149 pos = skb_put(skb, 2 + ie_len);
150 *pos++ = WLAN_EID_PREP;
151 break;
152 case MPATH_RANN:
153 mhwmp_dbg("sending RANN from %pM\n", orig_addr);
154 ie_len = sizeof(struct ieee80211_rann_ie);
155 pos = skb_put(skb, 2 + ie_len);
156 *pos++ = WLAN_EID_RANN;
157 break;
158 default:
159 kfree_skb(skb);
160 return -ENOTSUPP;
161 break;
163 *pos++ = ie_len;
164 *pos++ = flags;
165 *pos++ = hop_count;
166 *pos++ = ttl;
167 if (action == MPATH_PREP) {
168 memcpy(pos, target, ETH_ALEN);
169 pos += ETH_ALEN;
170 memcpy(pos, &target_sn, 4);
171 pos += 4;
172 } else {
173 if (action == MPATH_PREQ) {
174 memcpy(pos, &preq_id, 4);
175 pos += 4;
177 memcpy(pos, orig_addr, ETH_ALEN);
178 pos += ETH_ALEN;
179 memcpy(pos, &orig_sn, 4);
180 pos += 4;
182 memcpy(pos, &lifetime, 4); /* interval for RANN */
183 pos += 4;
184 memcpy(pos, &metric, 4);
185 pos += 4;
186 if (action == MPATH_PREQ) {
187 *pos++ = 1; /* destination count */
188 *pos++ = target_flags;
189 memcpy(pos, target, ETH_ALEN);
190 pos += ETH_ALEN;
191 memcpy(pos, &target_sn, 4);
192 pos += 4;
193 } else if (action == MPATH_PREP) {
194 memcpy(pos, orig_addr, ETH_ALEN);
195 pos += ETH_ALEN;
196 memcpy(pos, &orig_sn, 4);
197 pos += 4;
200 ieee80211_tx_skb(sdata, skb);
201 return 0;
205 * mesh_send_path error - Sends a PERR mesh management frame
207 * @target: broken destination
208 * @target_sn: SN of the broken destination
209 * @target_rcode: reason code for this PERR
210 * @ra: node this frame is addressed to
212 int mesh_path_error_tx(u8 ttl, u8 *target, __le32 target_sn,
213 __le16 target_rcode, const u8 *ra,
214 struct ieee80211_sub_if_data *sdata)
216 struct ieee80211_local *local = sdata->local;
217 struct sk_buff *skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400);
218 struct ieee80211_mgmt *mgmt;
219 u8 *pos;
220 int ie_len;
222 if (!skb)
223 return -1;
224 skb_reserve(skb, local->hw.extra_tx_headroom);
225 /* 25 is the size of the common mgmt part (24) plus the size of the
226 * common action part (1)
228 mgmt = (struct ieee80211_mgmt *)
229 skb_put(skb, 25 + sizeof(mgmt->u.action.u.mesh_action));
230 memset(mgmt, 0, 25 + sizeof(mgmt->u.action.u.mesh_action));
231 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
232 IEEE80211_STYPE_ACTION);
234 memcpy(mgmt->da, ra, ETH_ALEN);
235 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
236 /* BSSID == SA */
237 memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
238 mgmt->u.action.category = WLAN_CATEGORY_MESH_ACTION;
239 mgmt->u.action.u.mesh_action.action_code =
240 WLAN_MESH_ACTION_HWMP_PATH_SELECTION;
241 ie_len = 15;
242 pos = skb_put(skb, 2 + ie_len);
243 *pos++ = WLAN_EID_PERR;
244 *pos++ = ie_len;
245 /* ttl */
246 *pos++ = ttl;
247 /* number of destinations */
248 *pos++ = 1;
250 * flags bit, bit 1 is unset if we know the sequence number and
251 * bit 2 is set if we have a reason code
253 *pos = 0;
254 if (!target_sn)
255 *pos |= MP_F_USN;
256 if (target_rcode)
257 *pos |= MP_F_RCODE;
258 pos++;
259 memcpy(pos, target, ETH_ALEN);
260 pos += ETH_ALEN;
261 memcpy(pos, &target_sn, 4);
262 pos += 4;
263 memcpy(pos, &target_rcode, 2);
265 ieee80211_tx_skb(sdata, skb);
266 return 0;
269 void ieee80211s_update_metric(struct ieee80211_local *local,
270 struct sta_info *stainfo, struct sk_buff *skb)
272 struct ieee80211_tx_info *txinfo = IEEE80211_SKB_CB(skb);
273 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
274 int failed;
276 if (!ieee80211_is_data(hdr->frame_control))
277 return;
279 failed = !(txinfo->flags & IEEE80211_TX_STAT_ACK);
281 /* moving average, scaled to 100 */
282 stainfo->fail_avg = ((80 * stainfo->fail_avg + 5) / 100 + 20 * failed);
283 if (stainfo->fail_avg > 95)
284 mesh_plink_broken(stainfo);
287 static u32 airtime_link_metric_get(struct ieee80211_local *local,
288 struct sta_info *sta)
290 struct ieee80211_supported_band *sband;
291 /* This should be adjusted for each device */
292 int device_constant = 1 << ARITH_SHIFT;
293 int test_frame_len = TEST_FRAME_LEN << ARITH_SHIFT;
294 int s_unit = 1 << ARITH_SHIFT;
295 int rate, err;
296 u32 tx_time, estimated_retx;
297 u64 result;
299 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
301 if (sta->fail_avg >= 100)
302 return MAX_METRIC;
304 if (sta->last_tx_rate.flags & IEEE80211_TX_RC_MCS)
305 return MAX_METRIC;
307 err = (sta->fail_avg << ARITH_SHIFT) / 100;
309 /* bitrate is in units of 100 Kbps, while we need rate in units of
310 * 1Mbps. This will be corrected on tx_time computation.
312 rate = sband->bitrates[sta->last_tx_rate.idx].bitrate;
313 tx_time = (device_constant + 10 * test_frame_len / rate);
314 estimated_retx = ((1 << (2 * ARITH_SHIFT)) / (s_unit - err));
315 result = (tx_time * estimated_retx) >> (2 * ARITH_SHIFT) ;
316 return (u32)result;
320 * hwmp_route_info_get - Update routing info to originator and transmitter
322 * @sdata: local mesh subif
323 * @mgmt: mesh management frame
324 * @hwmp_ie: hwmp information element (PREP or PREQ)
326 * This function updates the path routing information to the originator and the
327 * transmitter of a HWMP PREQ or PREP frame.
329 * Returns: metric to frame originator or 0 if the frame should not be further
330 * processed
332 * Notes: this function is the only place (besides user-provided info) where
333 * path routing information is updated.
335 static u32 hwmp_route_info_get(struct ieee80211_sub_if_data *sdata,
336 struct ieee80211_mgmt *mgmt,
337 u8 *hwmp_ie, enum mpath_frame_type action)
339 struct ieee80211_local *local = sdata->local;
340 struct mesh_path *mpath;
341 struct sta_info *sta;
342 bool fresh_info;
343 u8 *orig_addr, *ta;
344 u32 orig_sn, orig_metric;
345 unsigned long orig_lifetime, exp_time;
346 u32 last_hop_metric, new_metric;
347 bool process = true;
349 rcu_read_lock();
350 sta = sta_info_get(sdata, mgmt->sa);
351 if (!sta) {
352 rcu_read_unlock();
353 return 0;
356 last_hop_metric = airtime_link_metric_get(local, sta);
357 /* Update and check originator routing info */
358 fresh_info = true;
360 switch (action) {
361 case MPATH_PREQ:
362 orig_addr = PREQ_IE_ORIG_ADDR(hwmp_ie);
363 orig_sn = PREQ_IE_ORIG_SN(hwmp_ie);
364 orig_lifetime = PREQ_IE_LIFETIME(hwmp_ie);
365 orig_metric = PREQ_IE_METRIC(hwmp_ie);
366 break;
367 case MPATH_PREP:
368 /* Originator here refers to the MP that was the destination in
369 * the Path Request. The draft refers to that MP as the
370 * destination address, even though usually it is the origin of
371 * the PREP frame. We divert from the nomenclature in the draft
372 * so that we can easily use a single function to gather path
373 * information from both PREQ and PREP frames.
375 orig_addr = PREP_IE_ORIG_ADDR(hwmp_ie);
376 orig_sn = PREP_IE_ORIG_SN(hwmp_ie);
377 orig_lifetime = PREP_IE_LIFETIME(hwmp_ie);
378 orig_metric = PREP_IE_METRIC(hwmp_ie);
379 break;
380 default:
381 rcu_read_unlock();
382 return 0;
384 new_metric = orig_metric + last_hop_metric;
385 if (new_metric < orig_metric)
386 new_metric = MAX_METRIC;
387 exp_time = TU_TO_EXP_TIME(orig_lifetime);
389 if (memcmp(orig_addr, sdata->vif.addr, ETH_ALEN) == 0) {
390 /* This MP is the originator, we are not interested in this
391 * frame, except for updating transmitter's path info.
393 process = false;
394 fresh_info = false;
395 } else {
396 mpath = mesh_path_lookup(orig_addr, sdata);
397 if (mpath) {
398 spin_lock_bh(&mpath->state_lock);
399 if (mpath->flags & MESH_PATH_FIXED)
400 fresh_info = false;
401 else if ((mpath->flags & MESH_PATH_ACTIVE) &&
402 (mpath->flags & MESH_PATH_SN_VALID)) {
403 if (SN_GT(mpath->sn, orig_sn) ||
404 (mpath->sn == orig_sn &&
405 new_metric >= mpath->metric)) {
406 process = false;
407 fresh_info = false;
410 } else {
411 mesh_path_add(orig_addr, sdata);
412 mpath = mesh_path_lookup(orig_addr, sdata);
413 if (!mpath) {
414 rcu_read_unlock();
415 return 0;
417 spin_lock_bh(&mpath->state_lock);
420 if (fresh_info) {
421 mesh_path_assign_nexthop(mpath, sta);
422 mpath->flags |= MESH_PATH_SN_VALID;
423 mpath->metric = new_metric;
424 mpath->sn = orig_sn;
425 mpath->exp_time = time_after(mpath->exp_time, exp_time)
426 ? mpath->exp_time : exp_time;
427 mesh_path_activate(mpath);
428 spin_unlock_bh(&mpath->state_lock);
429 mesh_path_tx_pending(mpath);
430 /* draft says preq_id should be saved to, but there does
431 * not seem to be any use for it, skipping by now
433 } else
434 spin_unlock_bh(&mpath->state_lock);
437 /* Update and check transmitter routing info */
438 ta = mgmt->sa;
439 if (memcmp(orig_addr, ta, ETH_ALEN) == 0)
440 fresh_info = false;
441 else {
442 fresh_info = true;
444 mpath = mesh_path_lookup(ta, sdata);
445 if (mpath) {
446 spin_lock_bh(&mpath->state_lock);
447 if ((mpath->flags & MESH_PATH_FIXED) ||
448 ((mpath->flags & MESH_PATH_ACTIVE) &&
449 (last_hop_metric > mpath->metric)))
450 fresh_info = false;
451 } else {
452 mesh_path_add(ta, sdata);
453 mpath = mesh_path_lookup(ta, sdata);
454 if (!mpath) {
455 rcu_read_unlock();
456 return 0;
458 spin_lock_bh(&mpath->state_lock);
461 if (fresh_info) {
462 mesh_path_assign_nexthop(mpath, sta);
463 mpath->metric = last_hop_metric;
464 mpath->exp_time = time_after(mpath->exp_time, exp_time)
465 ? mpath->exp_time : exp_time;
466 mesh_path_activate(mpath);
467 spin_unlock_bh(&mpath->state_lock);
468 mesh_path_tx_pending(mpath);
469 } else
470 spin_unlock_bh(&mpath->state_lock);
473 rcu_read_unlock();
475 return process ? new_metric : 0;
478 static void hwmp_preq_frame_process(struct ieee80211_sub_if_data *sdata,
479 struct ieee80211_mgmt *mgmt,
480 u8 *preq_elem, u32 metric)
482 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
483 struct mesh_path *mpath;
484 u8 *target_addr, *orig_addr;
485 u8 target_flags, ttl;
486 u32 orig_sn, target_sn, lifetime;
487 bool reply = false;
488 bool forward = true;
490 /* Update target SN, if present */
491 target_addr = PREQ_IE_TARGET_ADDR(preq_elem);
492 orig_addr = PREQ_IE_ORIG_ADDR(preq_elem);
493 target_sn = PREQ_IE_TARGET_SN(preq_elem);
494 orig_sn = PREQ_IE_ORIG_SN(preq_elem);
495 target_flags = PREQ_IE_TARGET_F(preq_elem);
497 mhwmp_dbg("received PREQ from %pM\n", orig_addr);
499 if (memcmp(target_addr, sdata->vif.addr, ETH_ALEN) == 0) {
500 mhwmp_dbg("PREQ is for us\n");
501 forward = false;
502 reply = true;
503 metric = 0;
504 if (time_after(jiffies, ifmsh->last_sn_update +
505 net_traversal_jiffies(sdata)) ||
506 time_before(jiffies, ifmsh->last_sn_update)) {
507 target_sn = ++ifmsh->sn;
508 ifmsh->last_sn_update = jiffies;
510 } else {
511 rcu_read_lock();
512 mpath = mesh_path_lookup(target_addr, sdata);
513 if (mpath) {
514 if ((!(mpath->flags & MESH_PATH_SN_VALID)) ||
515 SN_LT(mpath->sn, target_sn)) {
516 mpath->sn = target_sn;
517 mpath->flags |= MESH_PATH_SN_VALID;
518 } else if ((!(target_flags & MP_F_DO)) &&
519 (mpath->flags & MESH_PATH_ACTIVE)) {
520 reply = true;
521 metric = mpath->metric;
522 target_sn = mpath->sn;
523 if (target_flags & MP_F_RF)
524 target_flags |= MP_F_DO;
525 else
526 forward = false;
529 rcu_read_unlock();
532 if (reply) {
533 lifetime = PREQ_IE_LIFETIME(preq_elem);
534 ttl = ifmsh->mshcfg.element_ttl;
535 if (ttl != 0) {
536 mhwmp_dbg("replying to the PREQ\n");
537 mesh_path_sel_frame_tx(MPATH_PREP, 0, target_addr,
538 cpu_to_le32(target_sn), 0, orig_addr,
539 cpu_to_le32(orig_sn), mgmt->sa, 0, ttl,
540 cpu_to_le32(lifetime), cpu_to_le32(metric),
541 0, sdata);
542 } else
543 ifmsh->mshstats.dropped_frames_ttl++;
546 if (forward) {
547 u32 preq_id;
548 u8 hopcount, flags;
550 ttl = PREQ_IE_TTL(preq_elem);
551 lifetime = PREQ_IE_LIFETIME(preq_elem);
552 if (ttl <= 1) {
553 ifmsh->mshstats.dropped_frames_ttl++;
554 return;
556 mhwmp_dbg("forwarding the PREQ from %pM\n", orig_addr);
557 --ttl;
558 flags = PREQ_IE_FLAGS(preq_elem);
559 preq_id = PREQ_IE_PREQ_ID(preq_elem);
560 hopcount = PREQ_IE_HOPCOUNT(preq_elem) + 1;
561 mesh_path_sel_frame_tx(MPATH_PREQ, flags, orig_addr,
562 cpu_to_le32(orig_sn), target_flags, target_addr,
563 cpu_to_le32(target_sn), broadcast_addr,
564 hopcount, ttl, cpu_to_le32(lifetime),
565 cpu_to_le32(metric), cpu_to_le32(preq_id),
566 sdata);
567 ifmsh->mshstats.fwded_mcast++;
568 ifmsh->mshstats.fwded_frames++;
573 static inline struct sta_info *
574 next_hop_deref_protected(struct mesh_path *mpath)
576 return rcu_dereference_protected(mpath->next_hop,
577 lockdep_is_held(&mpath->state_lock));
581 static void hwmp_prep_frame_process(struct ieee80211_sub_if_data *sdata,
582 struct ieee80211_mgmt *mgmt,
583 u8 *prep_elem, u32 metric)
585 struct mesh_path *mpath;
586 u8 *target_addr, *orig_addr;
587 u8 ttl, hopcount, flags;
588 u8 next_hop[ETH_ALEN];
589 u32 target_sn, orig_sn, lifetime;
591 mhwmp_dbg("received PREP from %pM\n", PREP_IE_ORIG_ADDR(prep_elem));
593 /* Note that we divert from the draft nomenclature and denominate
594 * destination to what the draft refers to as origininator. So in this
595 * function destnation refers to the final destination of the PREP,
596 * which corresponds with the originator of the PREQ which this PREP
597 * replies
599 target_addr = PREP_IE_TARGET_ADDR(prep_elem);
600 if (memcmp(target_addr, sdata->vif.addr, ETH_ALEN) == 0)
601 /* destination, no forwarding required */
602 return;
604 ttl = PREP_IE_TTL(prep_elem);
605 if (ttl <= 1) {
606 sdata->u.mesh.mshstats.dropped_frames_ttl++;
607 return;
610 rcu_read_lock();
611 mpath = mesh_path_lookup(target_addr, sdata);
612 if (mpath)
613 spin_lock_bh(&mpath->state_lock);
614 else
615 goto fail;
616 if (!(mpath->flags & MESH_PATH_ACTIVE)) {
617 spin_unlock_bh(&mpath->state_lock);
618 goto fail;
620 memcpy(next_hop, next_hop_deref_protected(mpath)->sta.addr, ETH_ALEN);
621 spin_unlock_bh(&mpath->state_lock);
622 --ttl;
623 flags = PREP_IE_FLAGS(prep_elem);
624 lifetime = PREP_IE_LIFETIME(prep_elem);
625 hopcount = PREP_IE_HOPCOUNT(prep_elem) + 1;
626 orig_addr = PREP_IE_ORIG_ADDR(prep_elem);
627 target_sn = PREP_IE_TARGET_SN(prep_elem);
628 orig_sn = PREP_IE_ORIG_SN(prep_elem);
630 mesh_path_sel_frame_tx(MPATH_PREP, flags, orig_addr,
631 cpu_to_le32(orig_sn), 0, target_addr,
632 cpu_to_le32(target_sn), next_hop, hopcount,
633 ttl, cpu_to_le32(lifetime), cpu_to_le32(metric),
634 0, sdata);
635 rcu_read_unlock();
637 sdata->u.mesh.mshstats.fwded_unicast++;
638 sdata->u.mesh.mshstats.fwded_frames++;
639 return;
641 fail:
642 rcu_read_unlock();
643 sdata->u.mesh.mshstats.dropped_frames_no_route++;
646 static void hwmp_perr_frame_process(struct ieee80211_sub_if_data *sdata,
647 struct ieee80211_mgmt *mgmt, u8 *perr_elem)
649 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
650 struct mesh_path *mpath;
651 u8 ttl;
652 u8 *ta, *target_addr;
653 u32 target_sn;
654 u16 target_rcode;
656 ta = mgmt->sa;
657 ttl = PERR_IE_TTL(perr_elem);
658 if (ttl <= 1) {
659 ifmsh->mshstats.dropped_frames_ttl++;
660 return;
662 ttl--;
663 target_addr = PERR_IE_TARGET_ADDR(perr_elem);
664 target_sn = PERR_IE_TARGET_SN(perr_elem);
665 target_rcode = PERR_IE_TARGET_RCODE(perr_elem);
667 rcu_read_lock();
668 mpath = mesh_path_lookup(target_addr, sdata);
669 if (mpath) {
670 spin_lock_bh(&mpath->state_lock);
671 if (mpath->flags & MESH_PATH_ACTIVE &&
672 memcmp(ta, next_hop_deref_protected(mpath)->sta.addr,
673 ETH_ALEN) == 0 &&
674 (!(mpath->flags & MESH_PATH_SN_VALID) ||
675 SN_GT(target_sn, mpath->sn))) {
676 mpath->flags &= ~MESH_PATH_ACTIVE;
677 mpath->sn = target_sn;
678 spin_unlock_bh(&mpath->state_lock);
679 mesh_path_error_tx(ttl, target_addr, cpu_to_le32(target_sn),
680 cpu_to_le16(target_rcode),
681 broadcast_addr, sdata);
682 } else
683 spin_unlock_bh(&mpath->state_lock);
685 rcu_read_unlock();
688 static void hwmp_rann_frame_process(struct ieee80211_sub_if_data *sdata,
689 struct ieee80211_mgmt *mgmt,
690 struct ieee80211_rann_ie *rann)
692 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
693 struct mesh_path *mpath;
694 u8 ttl, flags, hopcount;
695 u8 *orig_addr;
696 u32 orig_sn, metric;
697 u32 interval = cpu_to_le32(IEEE80211_MESH_RANN_INTERVAL);
699 ttl = rann->rann_ttl;
700 if (ttl <= 1) {
701 ifmsh->mshstats.dropped_frames_ttl++;
702 return;
704 ttl--;
705 flags = rann->rann_flags;
706 orig_addr = rann->rann_addr;
707 orig_sn = rann->rann_seq;
708 hopcount = rann->rann_hopcount;
709 hopcount++;
710 metric = rann->rann_metric;
711 mhwmp_dbg("received RANN from %pM\n", orig_addr);
713 rcu_read_lock();
714 mpath = mesh_path_lookup(orig_addr, sdata);
715 if (!mpath) {
716 mesh_path_add(orig_addr, sdata);
717 mpath = mesh_path_lookup(orig_addr, sdata);
718 if (!mpath) {
719 rcu_read_unlock();
720 sdata->u.mesh.mshstats.dropped_frames_no_route++;
721 return;
723 mesh_queue_preq(mpath,
724 PREQ_Q_F_START | PREQ_Q_F_REFRESH);
726 if (mpath->sn < orig_sn) {
727 mesh_path_sel_frame_tx(MPATH_RANN, flags, orig_addr,
728 cpu_to_le32(orig_sn),
729 0, NULL, 0, broadcast_addr,
730 hopcount, ttl, interval,
731 cpu_to_le32(metric + mpath->metric),
732 0, sdata);
733 mpath->sn = orig_sn;
735 rcu_read_unlock();
739 void mesh_rx_path_sel_frame(struct ieee80211_sub_if_data *sdata,
740 struct ieee80211_mgmt *mgmt,
741 size_t len)
743 struct ieee802_11_elems elems;
744 size_t baselen;
745 u32 last_hop_metric;
747 /* need action_code */
748 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
749 return;
751 baselen = (u8 *) mgmt->u.action.u.mesh_action.variable - (u8 *) mgmt;
752 ieee802_11_parse_elems(mgmt->u.action.u.mesh_action.variable,
753 len - baselen, &elems);
755 if (elems.preq) {
756 if (elems.preq_len != 37)
757 /* Right now we support just 1 destination and no AE */
758 return;
759 last_hop_metric = hwmp_route_info_get(sdata, mgmt, elems.preq,
760 MPATH_PREQ);
761 if (last_hop_metric)
762 hwmp_preq_frame_process(sdata, mgmt, elems.preq,
763 last_hop_metric);
765 if (elems.prep) {
766 if (elems.prep_len != 31)
767 /* Right now we support no AE */
768 return;
769 last_hop_metric = hwmp_route_info_get(sdata, mgmt, elems.prep,
770 MPATH_PREP);
771 if (last_hop_metric)
772 hwmp_prep_frame_process(sdata, mgmt, elems.prep,
773 last_hop_metric);
775 if (elems.perr) {
776 if (elems.perr_len != 15)
777 /* Right now we support only one destination per PERR */
778 return;
779 hwmp_perr_frame_process(sdata, mgmt, elems.perr);
781 if (elems.rann)
782 hwmp_rann_frame_process(sdata, mgmt, elems.rann);
786 * mesh_queue_preq - queue a PREQ to a given destination
788 * @mpath: mesh path to discover
789 * @flags: special attributes of the PREQ to be sent
791 * Locking: the function must be called from within a rcu read lock block.
794 static void mesh_queue_preq(struct mesh_path *mpath, u8 flags)
796 struct ieee80211_sub_if_data *sdata = mpath->sdata;
797 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
798 struct mesh_preq_queue *preq_node;
800 preq_node = kmalloc(sizeof(struct mesh_preq_queue), GFP_ATOMIC);
801 if (!preq_node) {
802 mhwmp_dbg("could not allocate PREQ node\n");
803 return;
806 spin_lock_bh(&ifmsh->mesh_preq_queue_lock);
807 if (ifmsh->preq_queue_len == MAX_PREQ_QUEUE_LEN) {
808 spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
809 kfree(preq_node);
810 if (printk_ratelimit())
811 mhwmp_dbg("PREQ node queue full\n");
812 return;
815 memcpy(preq_node->dst, mpath->dst, ETH_ALEN);
816 preq_node->flags = flags;
818 list_add_tail(&preq_node->list, &ifmsh->preq_queue.list);
819 ++ifmsh->preq_queue_len;
820 spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
822 if (time_after(jiffies, ifmsh->last_preq + min_preq_int_jiff(sdata)))
823 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
825 else if (time_before(jiffies, ifmsh->last_preq)) {
826 /* avoid long wait if did not send preqs for a long time
827 * and jiffies wrapped around
829 ifmsh->last_preq = jiffies - min_preq_int_jiff(sdata) - 1;
830 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
831 } else
832 mod_timer(&ifmsh->mesh_path_timer, ifmsh->last_preq +
833 min_preq_int_jiff(sdata));
837 * mesh_path_start_discovery - launch a path discovery from the PREQ queue
839 * @sdata: local mesh subif
841 void mesh_path_start_discovery(struct ieee80211_sub_if_data *sdata)
843 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
844 struct mesh_preq_queue *preq_node;
845 struct mesh_path *mpath;
846 u8 ttl, target_flags;
847 u32 lifetime;
849 spin_lock_bh(&ifmsh->mesh_preq_queue_lock);
850 if (!ifmsh->preq_queue_len ||
851 time_before(jiffies, ifmsh->last_preq +
852 min_preq_int_jiff(sdata))) {
853 spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
854 return;
857 preq_node = list_first_entry(&ifmsh->preq_queue.list,
858 struct mesh_preq_queue, list);
859 list_del(&preq_node->list);
860 --ifmsh->preq_queue_len;
861 spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
863 rcu_read_lock();
864 mpath = mesh_path_lookup(preq_node->dst, sdata);
865 if (!mpath)
866 goto enddiscovery;
868 spin_lock_bh(&mpath->state_lock);
869 if (preq_node->flags & PREQ_Q_F_START) {
870 if (mpath->flags & MESH_PATH_RESOLVING) {
871 spin_unlock_bh(&mpath->state_lock);
872 goto enddiscovery;
873 } else {
874 mpath->flags &= ~MESH_PATH_RESOLVED;
875 mpath->flags |= MESH_PATH_RESOLVING;
876 mpath->discovery_retries = 0;
877 mpath->discovery_timeout = disc_timeout_jiff(sdata);
879 } else if (!(mpath->flags & MESH_PATH_RESOLVING) ||
880 mpath->flags & MESH_PATH_RESOLVED) {
881 mpath->flags &= ~MESH_PATH_RESOLVING;
882 spin_unlock_bh(&mpath->state_lock);
883 goto enddiscovery;
886 ifmsh->last_preq = jiffies;
888 if (time_after(jiffies, ifmsh->last_sn_update +
889 net_traversal_jiffies(sdata)) ||
890 time_before(jiffies, ifmsh->last_sn_update)) {
891 ++ifmsh->sn;
892 sdata->u.mesh.last_sn_update = jiffies;
894 lifetime = default_lifetime(sdata);
895 ttl = sdata->u.mesh.mshcfg.element_ttl;
896 if (ttl == 0) {
897 sdata->u.mesh.mshstats.dropped_frames_ttl++;
898 spin_unlock_bh(&mpath->state_lock);
899 goto enddiscovery;
902 if (preq_node->flags & PREQ_Q_F_REFRESH)
903 target_flags = MP_F_DO;
904 else
905 target_flags = MP_F_RF;
907 spin_unlock_bh(&mpath->state_lock);
908 mesh_path_sel_frame_tx(MPATH_PREQ, 0, sdata->vif.addr,
909 cpu_to_le32(ifmsh->sn), target_flags, mpath->dst,
910 cpu_to_le32(mpath->sn), broadcast_addr, 0,
911 ttl, cpu_to_le32(lifetime), 0,
912 cpu_to_le32(ifmsh->preq_id++), sdata);
913 mod_timer(&mpath->timer, jiffies + mpath->discovery_timeout);
915 enddiscovery:
916 rcu_read_unlock();
917 kfree(preq_node);
921 * mesh_nexthop_lookup - put the appropriate next hop on a mesh frame
923 * @skb: 802.11 frame to be sent
924 * @sdata: network subif the frame will be sent through
926 * Returns: 0 if the next hop was found. Nonzero otherwise. If no next hop is
927 * found, the function will start a path discovery and queue the frame so it is
928 * sent when the path is resolved. This means the caller must not free the skb
929 * in this case.
931 int mesh_nexthop_lookup(struct sk_buff *skb,
932 struct ieee80211_sub_if_data *sdata)
934 struct sk_buff *skb_to_free = NULL;
935 struct mesh_path *mpath;
936 struct sta_info *next_hop;
937 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
938 u8 *target_addr = hdr->addr3;
939 int err = 0;
941 rcu_read_lock();
942 mpath = mesh_path_lookup(target_addr, sdata);
944 if (!mpath) {
945 mesh_path_add(target_addr, sdata);
946 mpath = mesh_path_lookup(target_addr, sdata);
947 if (!mpath) {
948 sdata->u.mesh.mshstats.dropped_frames_no_route++;
949 err = -ENOSPC;
950 goto endlookup;
954 if (mpath->flags & MESH_PATH_ACTIVE) {
955 if (time_after(jiffies,
956 mpath->exp_time -
957 msecs_to_jiffies(sdata->u.mesh.mshcfg.path_refresh_time)) &&
958 !memcmp(sdata->vif.addr, hdr->addr4, ETH_ALEN) &&
959 !(mpath->flags & MESH_PATH_RESOLVING) &&
960 !(mpath->flags & MESH_PATH_FIXED)) {
961 mesh_queue_preq(mpath,
962 PREQ_Q_F_START | PREQ_Q_F_REFRESH);
964 next_hop = rcu_dereference(mpath->next_hop);
965 if (next_hop)
966 memcpy(hdr->addr1, next_hop->sta.addr, ETH_ALEN);
967 else
968 err = -ENOENT;
969 } else {
970 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
971 if (!(mpath->flags & MESH_PATH_RESOLVING)) {
972 /* Start discovery only if it is not running yet */
973 mesh_queue_preq(mpath, PREQ_Q_F_START);
976 if (skb_queue_len(&mpath->frame_queue) >=
977 MESH_FRAME_QUEUE_LEN)
978 skb_to_free = skb_dequeue(&mpath->frame_queue);
980 info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
981 skb_queue_tail(&mpath->frame_queue, skb);
982 if (skb_to_free)
983 mesh_path_discard_frame(skb_to_free, sdata);
984 err = -ENOENT;
987 endlookup:
988 rcu_read_unlock();
989 return err;
992 void mesh_path_timer(unsigned long data)
994 struct mesh_path *mpath = (void *) data;
995 struct ieee80211_sub_if_data *sdata = mpath->sdata;
997 if (sdata->local->quiescing)
998 return;
1000 spin_lock_bh(&mpath->state_lock);
1001 if (mpath->flags & MESH_PATH_RESOLVED ||
1002 (!(mpath->flags & MESH_PATH_RESOLVING)))
1003 mpath->flags &= ~(MESH_PATH_RESOLVING | MESH_PATH_RESOLVED);
1004 else if (mpath->discovery_retries < max_preq_retries(sdata)) {
1005 ++mpath->discovery_retries;
1006 mpath->discovery_timeout *= 2;
1007 mesh_queue_preq(mpath, 0);
1008 } else {
1009 mpath->flags = 0;
1010 mpath->exp_time = jiffies;
1011 mesh_path_flush_pending(mpath);
1014 spin_unlock_bh(&mpath->state_lock);
1017 void
1018 mesh_path_tx_root_frame(struct ieee80211_sub_if_data *sdata)
1020 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
1021 u32 interval = cpu_to_le32(IEEE80211_MESH_RANN_INTERVAL);
1023 mesh_path_sel_frame_tx(MPATH_RANN, 0, sdata->vif.addr,
1024 cpu_to_le32(++ifmsh->sn),
1025 0, NULL, 0, broadcast_addr,
1026 0, sdata->u.mesh.mshcfg.element_ttl,
1027 interval, 0, 0, sdata);