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) (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
{
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
= WLAN_CATEGORY_MESH_ACTION
;
136 mgmt
->u
.action
.u
.mesh_action
.action_code
=
137 WLAN_MESH_ACTION_HWMP_PATH_SELECTION
;
141 mhwmp_dbg("sending PREQ to %pM\n", target
);
143 pos
= skb_put(skb
, 2 + ie_len
);
144 *pos
++ = WLAN_EID_PREQ
;
147 mhwmp_dbg("sending PREP to %pM\n", target
);
149 pos
= skb_put(skb
, 2 + ie_len
);
150 *pos
++ = WLAN_EID_PREP
;
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
;
167 if (action
== MPATH_PREP
) {
168 memcpy(pos
, target
, ETH_ALEN
);
170 memcpy(pos
, &target_sn
, 4);
173 if (action
== MPATH_PREQ
) {
174 memcpy(pos
, &preq_id
, 4);
177 memcpy(pos
, orig_addr
, ETH_ALEN
);
179 memcpy(pos
, &orig_sn
, 4);
182 memcpy(pos
, &lifetime
, 4); /* interval for RANN */
184 memcpy(pos
, &metric
, 4);
186 if (action
== MPATH_PREQ
) {
187 *pos
++ = 1; /* destination count */
188 *pos
++ = target_flags
;
189 memcpy(pos
, target
, ETH_ALEN
);
191 memcpy(pos
, &target_sn
, 4);
193 } else if (action
== MPATH_PREP
) {
194 memcpy(pos
, orig_addr
, ETH_ALEN
);
196 memcpy(pos
, &orig_sn
, 4);
200 ieee80211_tx_skb(sdata
, skb
);
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
;
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
);
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
;
242 pos
= skb_put(skb
, 2 + ie_len
);
243 *pos
++ = WLAN_EID_PERR
;
247 /* number of destinations */
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
259 memcpy(pos
, target
, ETH_ALEN
);
261 memcpy(pos
, &target_sn
, 4);
263 memcpy(pos
, &target_rcode
, 2);
265 ieee80211_tx_skb(sdata
, skb
);
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
;
276 if (!ieee80211_is_data(hdr
->frame_control
))
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
;
296 u32 tx_time
, estimated_retx
;
299 sband
= local
->hw
.wiphy
->bands
[local
->hw
.conf
.channel
->band
];
301 if (sta
->fail_avg
>= 100)
304 if (sta
->last_tx_rate
.flags
& IEEE80211_TX_RC_MCS
)
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
) ;
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
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
;
344 u32 orig_sn
, orig_metric
;
345 unsigned long orig_lifetime
, exp_time
;
346 u32 last_hop_metric
, new_metric
;
350 sta
= sta_info_get(sdata
, mgmt
->sa
);
356 last_hop_metric
= airtime_link_metric_get(local
, sta
);
357 /* Update and check originator routing info */
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
);
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
);
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.
396 mpath
= mesh_path_lookup(orig_addr
, sdata
);
398 spin_lock_bh(&mpath
->state_lock
);
399 if (mpath
->flags
& MESH_PATH_FIXED
)
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
)) {
411 mesh_path_add(orig_addr
, sdata
);
412 mpath
= mesh_path_lookup(orig_addr
, sdata
);
417 spin_lock_bh(&mpath
->state_lock
);
421 mesh_path_assign_nexthop(mpath
, sta
);
422 mpath
->flags
|= MESH_PATH_SN_VALID
;
423 mpath
->metric
= new_metric
;
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
434 spin_unlock_bh(&mpath
->state_lock
);
437 /* Update and check transmitter routing info */
439 if (memcmp(orig_addr
, ta
, ETH_ALEN
) == 0)
444 mpath
= mesh_path_lookup(ta
, sdata
);
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
)))
452 mesh_path_add(ta
, sdata
);
453 mpath
= mesh_path_lookup(ta
, sdata
);
458 spin_lock_bh(&mpath
->state_lock
);
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
);
470 spin_unlock_bh(&mpath
->state_lock
);
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
;
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");
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
;
512 mpath
= mesh_path_lookup(target_addr
, sdata
);
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
)) {
521 metric
= mpath
->metric
;
522 target_sn
= mpath
->sn
;
523 if (target_flags
& MP_F_RF
)
524 target_flags
|= MP_F_DO
;
533 lifetime
= PREQ_IE_LIFETIME(preq_elem
);
534 ttl
= ifmsh
->mshcfg
.element_ttl
;
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
),
543 ifmsh
->mshstats
.dropped_frames_ttl
++;
550 ttl
= PREQ_IE_TTL(preq_elem
);
551 lifetime
= PREQ_IE_LIFETIME(preq_elem
);
553 ifmsh
->mshstats
.dropped_frames_ttl
++;
556 mhwmp_dbg("forwarding the PREQ from %pM\n", orig_addr
);
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
),
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
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 */
604 ttl
= PREP_IE_TTL(prep_elem
);
606 sdata
->u
.mesh
.mshstats
.dropped_frames_ttl
++;
611 mpath
= mesh_path_lookup(target_addr
, sdata
);
613 spin_lock_bh(&mpath
->state_lock
);
616 if (!(mpath
->flags
& MESH_PATH_ACTIVE
)) {
617 spin_unlock_bh(&mpath
->state_lock
);
620 memcpy(next_hop
, next_hop_deref_protected(mpath
)->sta
.addr
, ETH_ALEN
);
621 spin_unlock_bh(&mpath
->state_lock
);
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
),
637 sdata
->u
.mesh
.mshstats
.fwded_unicast
++;
638 sdata
->u
.mesh
.mshstats
.fwded_frames
++;
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
;
652 u8
*ta
, *target_addr
;
657 ttl
= PERR_IE_TTL(perr_elem
);
659 ifmsh
->mshstats
.dropped_frames_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
);
668 mpath
= mesh_path_lookup(target_addr
, sdata
);
670 spin_lock_bh(&mpath
->state_lock
);
671 if (mpath
->flags
& MESH_PATH_ACTIVE
&&
672 memcmp(ta
, next_hop_deref_protected(mpath
)->sta
.addr
,
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
);
683 spin_unlock_bh(&mpath
->state_lock
);
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
;
697 u32 interval
= cpu_to_le32(IEEE80211_MESH_RANN_INTERVAL
);
699 ttl
= rann
->rann_ttl
;
701 ifmsh
->mshstats
.dropped_frames_ttl
++;
705 flags
= rann
->rann_flags
;
706 orig_addr
= rann
->rann_addr
;
707 orig_sn
= rann
->rann_seq
;
708 hopcount
= rann
->rann_hopcount
;
710 metric
= rann
->rann_metric
;
711 mhwmp_dbg("received RANN from %pM\n", orig_addr
);
714 mpath
= mesh_path_lookup(orig_addr
, sdata
);
716 mesh_path_add(orig_addr
, sdata
);
717 mpath
= mesh_path_lookup(orig_addr
, sdata
);
720 sdata
->u
.mesh
.mshstats
.dropped_frames_no_route
++;
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
),
739 void mesh_rx_path_sel_frame(struct ieee80211_sub_if_data
*sdata
,
740 struct ieee80211_mgmt
*mgmt
,
743 struct ieee802_11_elems elems
;
747 /* need action_code */
748 if (len
< IEEE80211_MIN_ACTION_SIZE
+ 1)
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
);
756 if (elems
.preq_len
!= 37)
757 /* Right now we support just 1 destination and no AE */
759 last_hop_metric
= hwmp_route_info_get(sdata
, mgmt
, elems
.preq
,
762 hwmp_preq_frame_process(sdata
, mgmt
, elems
.preq
,
766 if (elems
.prep_len
!= 31)
767 /* Right now we support no AE */
769 last_hop_metric
= hwmp_route_info_get(sdata
, mgmt
, elems
.prep
,
772 hwmp_prep_frame_process(sdata
, mgmt
, elems
.prep
,
776 if (elems
.perr_len
!= 15)
777 /* Right now we support only one destination per PERR */
779 hwmp_perr_frame_process(sdata
, mgmt
, elems
.perr
);
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
);
802 mhwmp_dbg("could not allocate PREQ node\n");
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
);
810 if (printk_ratelimit())
811 mhwmp_dbg("PREQ node queue full\n");
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
);
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
;
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
);
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
);
864 mpath
= mesh_path_lookup(preq_node
->dst
, sdata
);
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
);
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
);
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
)) {
892 sdata
->u
.mesh
.last_sn_update
= jiffies
;
894 lifetime
= default_lifetime(sdata
);
895 ttl
= sdata
->u
.mesh
.mshcfg
.element_ttl
;
897 sdata
->u
.mesh
.mshstats
.dropped_frames_ttl
++;
898 spin_unlock_bh(&mpath
->state_lock
);
902 if (preq_node
->flags
& PREQ_Q_F_REFRESH
)
903 target_flags
= MP_F_DO
;
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
);
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
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
;
942 mpath
= mesh_path_lookup(target_addr
, sdata
);
945 mesh_path_add(target_addr
, sdata
);
946 mpath
= mesh_path_lookup(target_addr
, sdata
);
948 sdata
->u
.mesh
.mshstats
.dropped_frames_no_route
++;
954 if (mpath
->flags
& MESH_PATH_ACTIVE
) {
955 if (time_after(jiffies
,
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
);
966 memcpy(hdr
->addr1
, next_hop
->sta
.addr
, ETH_ALEN
);
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
);
983 mesh_path_discard_frame(skb_to_free
, sdata
);
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
)
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);
1010 mpath
->exp_time
= jiffies
;
1011 mesh_path_flush_pending(mpath
);
1014 spin_unlock_bh(&mpath
->state_lock
);
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
);