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.
10 #include <linux/slab.h>
13 #ifdef CONFIG_MAC80211_VERBOSE_MHWMP_DEBUG
14 #define mhwmp_dbg(fmt, args...) printk(KERN_DEBUG "Mesh HWMP: " fmt, ##args)
16 #define mhwmp_dbg(fmt, args...) do { (void)(0); } while (0)
19 #define TEST_FRAME_LEN 8192
20 #define MAX_METRIC 0xffffffff
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 */
29 /* Reply and forward */
31 /* Unknown Sequence Number */
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
)
42 return get_unaligned_le32(preq_elem
+ offset
);
45 static inline u32
u16_field_get(u8
*preq_elem
, int offset
, bool ae
)
49 return get_unaligned_le16(preq_elem
+ offset
);
52 /* HWMP IE processing macros */
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) (x + 3)
72 #define PREP_IE_ORIG_SN(x) u32_field_get(x, 9, 0);
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) (AE_F_SET(x) ? x + 27 : x + 21)
76 #define PREP_IE_TARGET_SN(x) u32_field_get(x, 27, AE_F_SET(x));
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
{
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
;
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
);
134 memcpy(mgmt
->bssid
, sdata
->vif
.addr
, ETH_ALEN
);
135 mgmt
->u
.action
.category
= MESH_PATH_SEL_CATEGORY
;
136 mgmt
->u
.action
.u
.mesh_action
.action_code
= MESH_PATH_SEL_ACTION
;
140 mhwmp_dbg("sending PREQ to %pM\n", target
);
142 pos
= skb_put(skb
, 2 + ie_len
);
143 *pos
++ = WLAN_EID_PREQ
;
146 mhwmp_dbg("sending PREP to %pM\n", target
);
148 pos
= skb_put(skb
, 2 + ie_len
);
149 *pos
++ = WLAN_EID_PREP
;
152 mhwmp_dbg("sending RANN from %pM\n", orig_addr
);
153 ie_len
= sizeof(struct ieee80211_rann_ie
);
154 pos
= skb_put(skb
, 2 + ie_len
);
155 *pos
++ = WLAN_EID_RANN
;
166 if (action
== MPATH_PREQ
) {
167 memcpy(pos
, &preq_id
, 4);
170 memcpy(pos
, orig_addr
, ETH_ALEN
);
172 memcpy(pos
, &orig_sn
, 4);
174 if (action
!= MPATH_RANN
) {
175 memcpy(pos
, &lifetime
, 4);
178 memcpy(pos
, &metric
, 4);
180 if (action
== MPATH_PREQ
) {
181 /* destination count */
183 *pos
++ = target_flags
;
185 if (action
!= MPATH_RANN
) {
186 memcpy(pos
, target
, ETH_ALEN
);
188 memcpy(pos
, &target_sn
, 4);
191 ieee80211_tx_skb(sdata
, skb
);
196 * mesh_send_path error - Sends a PERR mesh management frame
198 * @target: broken destination
199 * @target_sn: SN of the broken destination
200 * @target_rcode: reason code for this PERR
201 * @ra: node this frame is addressed to
203 int mesh_path_error_tx(u8 ttl
, u8
*target
, __le32 target_sn
,
204 __le16 target_rcode
, const u8
*ra
,
205 struct ieee80211_sub_if_data
*sdata
)
207 struct ieee80211_local
*local
= sdata
->local
;
208 struct sk_buff
*skb
= dev_alloc_skb(local
->hw
.extra_tx_headroom
+ 400);
209 struct ieee80211_mgmt
*mgmt
;
215 skb_reserve(skb
, local
->hw
.extra_tx_headroom
);
216 /* 25 is the size of the common mgmt part (24) plus the size of the
217 * common action part (1)
219 mgmt
= (struct ieee80211_mgmt
*)
220 skb_put(skb
, 25 + sizeof(mgmt
->u
.action
.u
.mesh_action
));
221 memset(mgmt
, 0, 25 + sizeof(mgmt
->u
.action
.u
.mesh_action
));
222 mgmt
->frame_control
= cpu_to_le16(IEEE80211_FTYPE_MGMT
|
223 IEEE80211_STYPE_ACTION
);
225 memcpy(mgmt
->da
, ra
, ETH_ALEN
);
226 memcpy(mgmt
->sa
, sdata
->vif
.addr
, ETH_ALEN
);
227 /* BSSID is left zeroed, wildcard value */
228 mgmt
->u
.action
.category
= MESH_PATH_SEL_CATEGORY
;
229 mgmt
->u
.action
.u
.mesh_action
.action_code
= MESH_PATH_SEL_ACTION
;
231 pos
= skb_put(skb
, 2 + ie_len
);
232 *pos
++ = WLAN_EID_PERR
;
236 /* number of destinations */
239 * flags bit, bit 1 is unset if we know the sequence number and
240 * bit 2 is set if we have a reason code
248 memcpy(pos
, target
, ETH_ALEN
);
250 memcpy(pos
, &target_sn
, 4);
252 memcpy(pos
, &target_rcode
, 2);
254 ieee80211_tx_skb(sdata
, skb
);
258 void ieee80211s_update_metric(struct ieee80211_local
*local
,
259 struct sta_info
*stainfo
, struct sk_buff
*skb
)
261 struct ieee80211_tx_info
*txinfo
= IEEE80211_SKB_CB(skb
);
262 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*) skb
->data
;
265 if (!ieee80211_is_data(hdr
->frame_control
))
268 failed
= !(txinfo
->flags
& IEEE80211_TX_STAT_ACK
);
270 /* moving average, scaled to 100 */
271 stainfo
->fail_avg
= ((80 * stainfo
->fail_avg
+ 5) / 100 + 20 * failed
);
272 if (stainfo
->fail_avg
> 95)
273 mesh_plink_broken(stainfo
);
276 static u32
airtime_link_metric_get(struct ieee80211_local
*local
,
277 struct sta_info
*sta
)
279 struct ieee80211_supported_band
*sband
;
280 /* This should be adjusted for each device */
281 int device_constant
= 1 << ARITH_SHIFT
;
282 int test_frame_len
= TEST_FRAME_LEN
<< ARITH_SHIFT
;
283 int s_unit
= 1 << ARITH_SHIFT
;
285 u32 tx_time
, estimated_retx
;
288 sband
= local
->hw
.wiphy
->bands
[local
->hw
.conf
.channel
->band
];
290 if (sta
->fail_avg
>= 100)
293 if (sta
->last_tx_rate
.flags
& IEEE80211_TX_RC_MCS
)
296 err
= (sta
->fail_avg
<< ARITH_SHIFT
) / 100;
298 /* bitrate is in units of 100 Kbps, while we need rate in units of
299 * 1Mbps. This will be corrected on tx_time computation.
301 rate
= sband
->bitrates
[sta
->last_tx_rate
.idx
].bitrate
;
302 tx_time
= (device_constant
+ 10 * test_frame_len
/ rate
);
303 estimated_retx
= ((1 << (2 * ARITH_SHIFT
)) / (s_unit
- err
));
304 result
= (tx_time
* estimated_retx
) >> (2 * ARITH_SHIFT
) ;
309 * hwmp_route_info_get - Update routing info to originator and transmitter
311 * @sdata: local mesh subif
312 * @mgmt: mesh management frame
313 * @hwmp_ie: hwmp information element (PREP or PREQ)
315 * This function updates the path routing information to the originator and the
316 * transmitter of a HWMP PREQ or PREP frame.
318 * Returns: metric to frame originator or 0 if the frame should not be further
321 * Notes: this function is the only place (besides user-provided info) where
322 * path routing information is updated.
324 static u32
hwmp_route_info_get(struct ieee80211_sub_if_data
*sdata
,
325 struct ieee80211_mgmt
*mgmt
,
326 u8
*hwmp_ie
, enum mpath_frame_type action
)
328 struct ieee80211_local
*local
= sdata
->local
;
329 struct mesh_path
*mpath
;
330 struct sta_info
*sta
;
333 u32 orig_sn
, orig_metric
;
334 unsigned long orig_lifetime
, exp_time
;
335 u32 last_hop_metric
, new_metric
;
339 sta
= sta_info_get(sdata
, mgmt
->sa
);
345 last_hop_metric
= airtime_link_metric_get(local
, sta
);
346 /* Update and check originator routing info */
351 orig_addr
= PREQ_IE_ORIG_ADDR(hwmp_ie
);
352 orig_sn
= PREQ_IE_ORIG_SN(hwmp_ie
);
353 orig_lifetime
= PREQ_IE_LIFETIME(hwmp_ie
);
354 orig_metric
= PREQ_IE_METRIC(hwmp_ie
);
357 /* Originator here refers to the MP that was the destination in
358 * the Path Request. The draft refers to that MP as the
359 * destination address, even though usually it is the origin of
360 * the PREP frame. We divert from the nomenclature in the draft
361 * so that we can easily use a single function to gather path
362 * information from both PREQ and PREP frames.
364 orig_addr
= PREP_IE_ORIG_ADDR(hwmp_ie
);
365 orig_sn
= PREP_IE_ORIG_SN(hwmp_ie
);
366 orig_lifetime
= PREP_IE_LIFETIME(hwmp_ie
);
367 orig_metric
= PREP_IE_METRIC(hwmp_ie
);
373 new_metric
= orig_metric
+ last_hop_metric
;
374 if (new_metric
< orig_metric
)
375 new_metric
= MAX_METRIC
;
376 exp_time
= TU_TO_EXP_TIME(orig_lifetime
);
378 if (memcmp(orig_addr
, sdata
->vif
.addr
, ETH_ALEN
) == 0) {
379 /* This MP is the originator, we are not interested in this
380 * frame, except for updating transmitter's path info.
385 mpath
= mesh_path_lookup(orig_addr
, sdata
);
387 spin_lock_bh(&mpath
->state_lock
);
388 if (mpath
->flags
& MESH_PATH_FIXED
)
390 else if ((mpath
->flags
& MESH_PATH_ACTIVE
) &&
391 (mpath
->flags
& MESH_PATH_SN_VALID
)) {
392 if (SN_GT(mpath
->sn
, orig_sn
) ||
393 (mpath
->sn
== orig_sn
&&
394 action
== MPATH_PREQ
&&
395 new_metric
>= mpath
->metric
)) {
401 mesh_path_add(orig_addr
, sdata
);
402 mpath
= mesh_path_lookup(orig_addr
, sdata
);
407 spin_lock_bh(&mpath
->state_lock
);
411 mesh_path_assign_nexthop(mpath
, sta
);
412 mpath
->flags
|= MESH_PATH_SN_VALID
;
413 mpath
->metric
= new_metric
;
415 mpath
->exp_time
= time_after(mpath
->exp_time
, exp_time
)
416 ? mpath
->exp_time
: exp_time
;
417 mesh_path_activate(mpath
);
418 spin_unlock_bh(&mpath
->state_lock
);
419 mesh_path_tx_pending(mpath
);
420 /* draft says preq_id should be saved to, but there does
421 * not seem to be any use for it, skipping by now
424 spin_unlock_bh(&mpath
->state_lock
);
427 /* Update and check transmitter routing info */
429 if (memcmp(orig_addr
, ta
, ETH_ALEN
) == 0)
434 mpath
= mesh_path_lookup(ta
, sdata
);
436 spin_lock_bh(&mpath
->state_lock
);
437 if ((mpath
->flags
& MESH_PATH_FIXED
) ||
438 ((mpath
->flags
& MESH_PATH_ACTIVE
) &&
439 (last_hop_metric
> mpath
->metric
)))
442 mesh_path_add(ta
, sdata
);
443 mpath
= mesh_path_lookup(ta
, sdata
);
448 spin_lock_bh(&mpath
->state_lock
);
452 mesh_path_assign_nexthop(mpath
, sta
);
453 mpath
->flags
&= ~MESH_PATH_SN_VALID
;
454 mpath
->metric
= last_hop_metric
;
455 mpath
->exp_time
= time_after(mpath
->exp_time
, exp_time
)
456 ? mpath
->exp_time
: exp_time
;
457 mesh_path_activate(mpath
);
458 spin_unlock_bh(&mpath
->state_lock
);
459 mesh_path_tx_pending(mpath
);
461 spin_unlock_bh(&mpath
->state_lock
);
466 return process
? new_metric
: 0;
469 static void hwmp_preq_frame_process(struct ieee80211_sub_if_data
*sdata
,
470 struct ieee80211_mgmt
*mgmt
,
471 u8
*preq_elem
, u32 metric
)
473 struct ieee80211_if_mesh
*ifmsh
= &sdata
->u
.mesh
;
474 struct mesh_path
*mpath
;
475 u8
*target_addr
, *orig_addr
;
476 u8 target_flags
, ttl
;
477 u32 orig_sn
, target_sn
, lifetime
;
481 /* Update target SN, if present */
482 target_addr
= PREQ_IE_TARGET_ADDR(preq_elem
);
483 orig_addr
= PREQ_IE_ORIG_ADDR(preq_elem
);
484 target_sn
= PREQ_IE_TARGET_SN(preq_elem
);
485 orig_sn
= PREQ_IE_ORIG_SN(preq_elem
);
486 target_flags
= PREQ_IE_TARGET_F(preq_elem
);
488 mhwmp_dbg("received PREQ from %pM\n", orig_addr
);
490 if (memcmp(target_addr
, sdata
->vif
.addr
, ETH_ALEN
) == 0) {
491 mhwmp_dbg("PREQ is for us\n");
495 if (time_after(jiffies
, ifmsh
->last_sn_update
+
496 net_traversal_jiffies(sdata
)) ||
497 time_before(jiffies
, ifmsh
->last_sn_update
)) {
498 target_sn
= ++ifmsh
->sn
;
499 ifmsh
->last_sn_update
= jiffies
;
503 mpath
= mesh_path_lookup(target_addr
, sdata
);
505 if ((!(mpath
->flags
& MESH_PATH_SN_VALID
)) ||
506 SN_LT(mpath
->sn
, target_sn
)) {
507 mpath
->sn
= target_sn
;
508 mpath
->flags
|= MESH_PATH_SN_VALID
;
509 } else if ((!(target_flags
& MP_F_DO
)) &&
510 (mpath
->flags
& MESH_PATH_ACTIVE
)) {
512 metric
= mpath
->metric
;
513 target_sn
= mpath
->sn
;
514 if (target_flags
& MP_F_RF
)
515 target_flags
|= MP_F_DO
;
524 lifetime
= PREQ_IE_LIFETIME(preq_elem
);
525 ttl
= ifmsh
->mshcfg
.dot11MeshTTL
;
527 mhwmp_dbg("replying to the PREQ\n");
528 mesh_path_sel_frame_tx(MPATH_PREP
, 0, target_addr
,
529 cpu_to_le32(target_sn
), 0, orig_addr
,
530 cpu_to_le32(orig_sn
), mgmt
->sa
, 0, ttl
,
531 cpu_to_le32(lifetime
), cpu_to_le32(metric
),
534 ifmsh
->mshstats
.dropped_frames_ttl
++;
541 ttl
= PREQ_IE_TTL(preq_elem
);
542 lifetime
= PREQ_IE_LIFETIME(preq_elem
);
544 ifmsh
->mshstats
.dropped_frames_ttl
++;
547 mhwmp_dbg("forwarding the PREQ from %pM\n", orig_addr
);
549 flags
= PREQ_IE_FLAGS(preq_elem
);
550 preq_id
= PREQ_IE_PREQ_ID(preq_elem
);
551 hopcount
= PREQ_IE_HOPCOUNT(preq_elem
) + 1;
552 mesh_path_sel_frame_tx(MPATH_PREQ
, flags
, orig_addr
,
553 cpu_to_le32(orig_sn
), target_flags
, target_addr
,
554 cpu_to_le32(target_sn
), broadcast_addr
,
555 hopcount
, ttl
, cpu_to_le32(lifetime
),
556 cpu_to_le32(metric
), cpu_to_le32(preq_id
),
558 ifmsh
->mshstats
.fwded_mcast
++;
559 ifmsh
->mshstats
.fwded_frames
++;
564 static void hwmp_prep_frame_process(struct ieee80211_sub_if_data
*sdata
,
565 struct ieee80211_mgmt
*mgmt
,
566 u8
*prep_elem
, u32 metric
)
568 struct mesh_path
*mpath
;
569 u8
*target_addr
, *orig_addr
;
570 u8 ttl
, hopcount
, flags
;
571 u8 next_hop
[ETH_ALEN
];
572 u32 target_sn
, orig_sn
, lifetime
;
574 mhwmp_dbg("received PREP from %pM\n", PREP_IE_ORIG_ADDR(prep_elem
));
576 /* Note that we divert from the draft nomenclature and denominate
577 * destination to what the draft refers to as origininator. So in this
578 * function destnation refers to the final destination of the PREP,
579 * which corresponds with the originator of the PREQ which this PREP
582 target_addr
= PREP_IE_TARGET_ADDR(prep_elem
);
583 if (memcmp(target_addr
, sdata
->vif
.addr
, ETH_ALEN
) == 0)
584 /* destination, no forwarding required */
587 ttl
= PREP_IE_TTL(prep_elem
);
589 sdata
->u
.mesh
.mshstats
.dropped_frames_ttl
++;
594 mpath
= mesh_path_lookup(target_addr
, sdata
);
596 spin_lock_bh(&mpath
->state_lock
);
599 if (!(mpath
->flags
& MESH_PATH_ACTIVE
)) {
600 spin_unlock_bh(&mpath
->state_lock
);
603 memcpy(next_hop
, mpath
->next_hop
->sta
.addr
, ETH_ALEN
);
604 spin_unlock_bh(&mpath
->state_lock
);
606 flags
= PREP_IE_FLAGS(prep_elem
);
607 lifetime
= PREP_IE_LIFETIME(prep_elem
);
608 hopcount
= PREP_IE_HOPCOUNT(prep_elem
) + 1;
609 orig_addr
= PREP_IE_ORIG_ADDR(prep_elem
);
610 target_sn
= PREP_IE_TARGET_SN(prep_elem
);
611 orig_sn
= PREP_IE_ORIG_SN(prep_elem
);
613 mesh_path_sel_frame_tx(MPATH_PREP
, flags
, orig_addr
,
614 cpu_to_le32(orig_sn
), 0, target_addr
,
615 cpu_to_le32(target_sn
), next_hop
, hopcount
,
616 ttl
, cpu_to_le32(lifetime
), cpu_to_le32(metric
),
620 sdata
->u
.mesh
.mshstats
.fwded_unicast
++;
621 sdata
->u
.mesh
.mshstats
.fwded_frames
++;
626 sdata
->u
.mesh
.mshstats
.dropped_frames_no_route
++;
630 static void hwmp_perr_frame_process(struct ieee80211_sub_if_data
*sdata
,
631 struct ieee80211_mgmt
*mgmt
, u8
*perr_elem
)
633 struct ieee80211_if_mesh
*ifmsh
= &sdata
->u
.mesh
;
634 struct mesh_path
*mpath
;
636 u8
*ta
, *target_addr
;
642 ttl
= PERR_IE_TTL(perr_elem
);
644 ifmsh
->mshstats
.dropped_frames_ttl
++;
648 target_flags
= PERR_IE_TARGET_FLAGS(perr_elem
);
649 target_addr
= PERR_IE_TARGET_ADDR(perr_elem
);
650 target_sn
= PERR_IE_TARGET_SN(perr_elem
);
651 target_rcode
= PERR_IE_TARGET_RCODE(perr_elem
);
654 mpath
= mesh_path_lookup(target_addr
, sdata
);
656 spin_lock_bh(&mpath
->state_lock
);
657 if (mpath
->flags
& MESH_PATH_ACTIVE
&&
658 memcmp(ta
, mpath
->next_hop
->sta
.addr
, ETH_ALEN
) == 0 &&
659 (!(mpath
->flags
& MESH_PATH_SN_VALID
) ||
660 SN_GT(target_sn
, mpath
->sn
))) {
661 mpath
->flags
&= ~MESH_PATH_ACTIVE
;
662 mpath
->sn
= target_sn
;
663 spin_unlock_bh(&mpath
->state_lock
);
664 mesh_path_error_tx(ttl
, target_addr
, cpu_to_le32(target_sn
),
665 cpu_to_le16(target_rcode
),
666 broadcast_addr
, sdata
);
668 spin_unlock_bh(&mpath
->state_lock
);
673 static void hwmp_rann_frame_process(struct ieee80211_sub_if_data
*sdata
,
674 struct ieee80211_mgmt
*mgmt
,
675 struct ieee80211_rann_ie
*rann
)
677 struct ieee80211_if_mesh
*ifmsh
= &sdata
->u
.mesh
;
678 struct mesh_path
*mpath
;
680 u8 ttl
, flags
, hopcount
;
685 ttl
= rann
->rann_ttl
;
687 ifmsh
->mshstats
.dropped_frames_ttl
++;
691 flags
= rann
->rann_flags
;
692 orig_addr
= rann
->rann_addr
;
693 orig_sn
= rann
->rann_seq
;
694 hopcount
= rann
->rann_hopcount
;
696 metric
= rann
->rann_metric
;
697 mhwmp_dbg("received RANN from %pM\n", orig_addr
);
700 mpath
= mesh_path_lookup(orig_addr
, sdata
);
702 mesh_path_add(orig_addr
, sdata
);
703 mpath
= mesh_path_lookup(orig_addr
, sdata
);
706 sdata
->u
.mesh
.mshstats
.dropped_frames_no_route
++;
709 mesh_queue_preq(mpath
,
710 PREQ_Q_F_START
| PREQ_Q_F_REFRESH
);
712 if (mpath
->sn
< orig_sn
) {
713 mesh_path_sel_frame_tx(MPATH_RANN
, flags
, orig_addr
,
714 cpu_to_le32(orig_sn
),
715 0, NULL
, 0, broadcast_addr
,
717 cpu_to_le32(metric
+ mpath
->metric
),
725 void mesh_rx_path_sel_frame(struct ieee80211_sub_if_data
*sdata
,
726 struct ieee80211_mgmt
*mgmt
,
729 struct ieee802_11_elems elems
;
733 /* need action_code */
734 if (len
< IEEE80211_MIN_ACTION_SIZE
+ 1)
737 baselen
= (u8
*) mgmt
->u
.action
.u
.mesh_action
.variable
- (u8
*) mgmt
;
738 ieee802_11_parse_elems(mgmt
->u
.action
.u
.mesh_action
.variable
,
739 len
- baselen
, &elems
);
742 if (elems
.preq_len
!= 37)
743 /* Right now we support just 1 destination and no AE */
745 last_hop_metric
= hwmp_route_info_get(sdata
, mgmt
, elems
.preq
,
748 hwmp_preq_frame_process(sdata
, mgmt
, elems
.preq
,
752 if (elems
.prep_len
!= 31)
753 /* Right now we support no AE */
755 last_hop_metric
= hwmp_route_info_get(sdata
, mgmt
, elems
.prep
,
758 hwmp_prep_frame_process(sdata
, mgmt
, elems
.prep
,
762 if (elems
.perr_len
!= 15)
763 /* Right now we support only one destination per PERR */
765 hwmp_perr_frame_process(sdata
, mgmt
, elems
.perr
);
768 hwmp_rann_frame_process(sdata
, mgmt
, elems
.rann
);
772 * mesh_queue_preq - queue a PREQ to a given destination
774 * @mpath: mesh path to discover
775 * @flags: special attributes of the PREQ to be sent
777 * Locking: the function must be called from within a rcu read lock block.
780 static void mesh_queue_preq(struct mesh_path
*mpath
, u8 flags
)
782 struct ieee80211_sub_if_data
*sdata
= mpath
->sdata
;
783 struct ieee80211_if_mesh
*ifmsh
= &sdata
->u
.mesh
;
784 struct mesh_preq_queue
*preq_node
;
786 preq_node
= kmalloc(sizeof(struct mesh_preq_queue
), GFP_ATOMIC
);
788 mhwmp_dbg("could not allocate PREQ node\n");
792 spin_lock(&ifmsh
->mesh_preq_queue_lock
);
793 if (ifmsh
->preq_queue_len
== MAX_PREQ_QUEUE_LEN
) {
794 spin_unlock(&ifmsh
->mesh_preq_queue_lock
);
796 if (printk_ratelimit())
797 mhwmp_dbg("PREQ node queue full\n");
801 memcpy(preq_node
->dst
, mpath
->dst
, ETH_ALEN
);
802 preq_node
->flags
= flags
;
804 list_add_tail(&preq_node
->list
, &ifmsh
->preq_queue
.list
);
805 ++ifmsh
->preq_queue_len
;
806 spin_unlock(&ifmsh
->mesh_preq_queue_lock
);
808 if (time_after(jiffies
, ifmsh
->last_preq
+ min_preq_int_jiff(sdata
)))
809 ieee80211_queue_work(&sdata
->local
->hw
, &ifmsh
->work
);
811 else if (time_before(jiffies
, ifmsh
->last_preq
)) {
812 /* avoid long wait if did not send preqs for a long time
813 * and jiffies wrapped around
815 ifmsh
->last_preq
= jiffies
- min_preq_int_jiff(sdata
) - 1;
816 ieee80211_queue_work(&sdata
->local
->hw
, &ifmsh
->work
);
818 mod_timer(&ifmsh
->mesh_path_timer
, ifmsh
->last_preq
+
819 min_preq_int_jiff(sdata
));
823 * mesh_path_start_discovery - launch a path discovery from the PREQ queue
825 * @sdata: local mesh subif
827 void mesh_path_start_discovery(struct ieee80211_sub_if_data
*sdata
)
829 struct ieee80211_if_mesh
*ifmsh
= &sdata
->u
.mesh
;
830 struct mesh_preq_queue
*preq_node
;
831 struct mesh_path
*mpath
;
832 u8 ttl
, target_flags
;
835 spin_lock_bh(&ifmsh
->mesh_preq_queue_lock
);
836 if (!ifmsh
->preq_queue_len
||
837 time_before(jiffies
, ifmsh
->last_preq
+
838 min_preq_int_jiff(sdata
))) {
839 spin_unlock_bh(&ifmsh
->mesh_preq_queue_lock
);
843 preq_node
= list_first_entry(&ifmsh
->preq_queue
.list
,
844 struct mesh_preq_queue
, list
);
845 list_del(&preq_node
->list
);
846 --ifmsh
->preq_queue_len
;
847 spin_unlock_bh(&ifmsh
->mesh_preq_queue_lock
);
850 mpath
= mesh_path_lookup(preq_node
->dst
, sdata
);
854 spin_lock_bh(&mpath
->state_lock
);
855 if (preq_node
->flags
& PREQ_Q_F_START
) {
856 if (mpath
->flags
& MESH_PATH_RESOLVING
) {
857 spin_unlock_bh(&mpath
->state_lock
);
860 mpath
->flags
&= ~MESH_PATH_RESOLVED
;
861 mpath
->flags
|= MESH_PATH_RESOLVING
;
862 mpath
->discovery_retries
= 0;
863 mpath
->discovery_timeout
= disc_timeout_jiff(sdata
);
865 } else if (!(mpath
->flags
& MESH_PATH_RESOLVING
) ||
866 mpath
->flags
& MESH_PATH_RESOLVED
) {
867 mpath
->flags
&= ~MESH_PATH_RESOLVING
;
868 spin_unlock_bh(&mpath
->state_lock
);
872 ifmsh
->last_preq
= jiffies
;
874 if (time_after(jiffies
, ifmsh
->last_sn_update
+
875 net_traversal_jiffies(sdata
)) ||
876 time_before(jiffies
, ifmsh
->last_sn_update
)) {
878 sdata
->u
.mesh
.last_sn_update
= jiffies
;
880 lifetime
= default_lifetime(sdata
);
881 ttl
= sdata
->u
.mesh
.mshcfg
.dot11MeshTTL
;
883 sdata
->u
.mesh
.mshstats
.dropped_frames_ttl
++;
884 spin_unlock_bh(&mpath
->state_lock
);
888 if (preq_node
->flags
& PREQ_Q_F_REFRESH
)
889 target_flags
= MP_F_DO
;
891 target_flags
= MP_F_RF
;
893 spin_unlock_bh(&mpath
->state_lock
);
894 mesh_path_sel_frame_tx(MPATH_PREQ
, 0, sdata
->vif
.addr
,
895 cpu_to_le32(ifmsh
->sn
), target_flags
, mpath
->dst
,
896 cpu_to_le32(mpath
->sn
), broadcast_addr
, 0,
897 ttl
, cpu_to_le32(lifetime
), 0,
898 cpu_to_le32(ifmsh
->preq_id
++), sdata
);
899 mod_timer(&mpath
->timer
, jiffies
+ mpath
->discovery_timeout
);
907 * mesh_nexthop_lookup - put the appropriate next hop on a mesh frame
909 * @skb: 802.11 frame to be sent
910 * @sdata: network subif the frame will be sent through
912 * Returns: 0 if the next hop was found. Nonzero otherwise. If no next hop is
913 * found, the function will start a path discovery and queue the frame so it is
914 * sent when the path is resolved. This means the caller must not free the skb
917 int mesh_nexthop_lookup(struct sk_buff
*skb
,
918 struct ieee80211_sub_if_data
*sdata
)
920 struct sk_buff
*skb_to_free
= NULL
;
921 struct mesh_path
*mpath
;
922 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*) skb
->data
;
923 u8
*target_addr
= hdr
->addr3
;
927 mpath
= mesh_path_lookup(target_addr
, sdata
);
930 mesh_path_add(target_addr
, sdata
);
931 mpath
= mesh_path_lookup(target_addr
, sdata
);
933 sdata
->u
.mesh
.mshstats
.dropped_frames_no_route
++;
939 if (mpath
->flags
& MESH_PATH_ACTIVE
) {
940 if (time_after(jiffies
,
942 msecs_to_jiffies(sdata
->u
.mesh
.mshcfg
.path_refresh_time
)) &&
943 !memcmp(sdata
->vif
.addr
, hdr
->addr4
, ETH_ALEN
) &&
944 !(mpath
->flags
& MESH_PATH_RESOLVING
) &&
945 !(mpath
->flags
& MESH_PATH_FIXED
)) {
946 mesh_queue_preq(mpath
,
947 PREQ_Q_F_START
| PREQ_Q_F_REFRESH
);
949 memcpy(hdr
->addr1
, mpath
->next_hop
->sta
.addr
, ETH_ALEN
);
951 struct ieee80211_tx_info
*info
= IEEE80211_SKB_CB(skb
);
952 if (!(mpath
->flags
& MESH_PATH_RESOLVING
)) {
953 /* Start discovery only if it is not running yet */
954 mesh_queue_preq(mpath
, PREQ_Q_F_START
);
957 if (skb_queue_len(&mpath
->frame_queue
) >=
958 MESH_FRAME_QUEUE_LEN
)
959 skb_to_free
= skb_dequeue(&mpath
->frame_queue
);
961 info
->flags
|= IEEE80211_TX_INTFL_NEED_TXPROCESSING
;
962 skb_queue_tail(&mpath
->frame_queue
, skb
);
964 mesh_path_discard_frame(skb_to_free
, sdata
);
973 void mesh_path_timer(unsigned long data
)
975 struct ieee80211_sub_if_data
*sdata
;
976 struct mesh_path
*mpath
;
979 mpath
= (struct mesh_path
*) data
;
980 mpath
= rcu_dereference(mpath
);
983 sdata
= mpath
->sdata
;
985 if (sdata
->local
->quiescing
) {
990 spin_lock_bh(&mpath
->state_lock
);
991 if (mpath
->flags
& MESH_PATH_RESOLVED
||
992 (!(mpath
->flags
& MESH_PATH_RESOLVING
)))
993 mpath
->flags
&= ~(MESH_PATH_RESOLVING
| MESH_PATH_RESOLVED
);
994 else if (mpath
->discovery_retries
< max_preq_retries(sdata
)) {
995 ++mpath
->discovery_retries
;
996 mpath
->discovery_timeout
*= 2;
997 mesh_queue_preq(mpath
, 0);
1000 mpath
->exp_time
= jiffies
;
1001 mesh_path_flush_pending(mpath
);
1004 spin_unlock_bh(&mpath
->state_lock
);
1010 mesh_path_tx_root_frame(struct ieee80211_sub_if_data
*sdata
)
1012 struct ieee80211_if_mesh
*ifmsh
= &sdata
->u
.mesh
;
1014 mesh_path_sel_frame_tx(MPATH_RANN
, 0, sdata
->vif
.addr
,
1015 cpu_to_le32(++ifmsh
->sn
),
1016 0, NULL
, 0, broadcast_addr
,
1017 0, MESH_TTL
, 0, 0, 0, sdata
);