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>
14 #ifdef CONFIG_MAC80211_VERBOSE_MHWMP_DEBUG
15 #define mhwmp_dbg(fmt, args...) \
16 printk(KERN_DEBUG "Mesh HWMP (%s): " fmt "\n", sdata->name, ##args)
18 #define mhwmp_dbg(fmt, args...) do { (void)(0); } while (0)
21 #define TEST_FRAME_LEN 8192
22 #define MAX_METRIC 0xffffffff
25 /* Number of frames buffered per destination for unresolved destinations */
26 #define MESH_FRAME_QUEUE_LEN 10
27 #define MAX_PREQ_QUEUE_LEN 64
29 /* Destination only */
31 /* Reply and forward */
33 /* Unknown Sequence Number */
35 /* Reason code Present */
36 #define MP_F_RCODE 0x02
38 static void mesh_queue_preq(struct mesh_path
*, u8
);
40 static inline u32
u32_field_get(u8
*preq_elem
, int offset
, bool ae
)
44 return get_unaligned_le32(preq_elem
+ offset
);
47 static inline u32
u16_field_get(u8
*preq_elem
, int offset
, bool ae
)
51 return get_unaligned_le16(preq_elem
+ offset
);
54 /* HWMP IE processing macros */
56 #define AE_F_SET(x) (*x & AE_F)
57 #define PREQ_IE_FLAGS(x) (*(x))
58 #define PREQ_IE_HOPCOUNT(x) (*(x + 1))
59 #define PREQ_IE_TTL(x) (*(x + 2))
60 #define PREQ_IE_PREQ_ID(x) u32_field_get(x, 3, 0)
61 #define PREQ_IE_ORIG_ADDR(x) (x + 7)
62 #define PREQ_IE_ORIG_SN(x) u32_field_get(x, 13, 0)
63 #define PREQ_IE_LIFETIME(x) u32_field_get(x, 17, AE_F_SET(x))
64 #define PREQ_IE_METRIC(x) u32_field_get(x, 21, AE_F_SET(x))
65 #define PREQ_IE_TARGET_F(x) (*(AE_F_SET(x) ? x + 32 : x + 26))
66 #define PREQ_IE_TARGET_ADDR(x) (AE_F_SET(x) ? x + 33 : x + 27)
67 #define PREQ_IE_TARGET_SN(x) u32_field_get(x, 33, AE_F_SET(x))
70 #define PREP_IE_FLAGS(x) PREQ_IE_FLAGS(x)
71 #define PREP_IE_HOPCOUNT(x) PREQ_IE_HOPCOUNT(x)
72 #define PREP_IE_TTL(x) PREQ_IE_TTL(x)
73 #define PREP_IE_ORIG_ADDR(x) (AE_F_SET(x) ? x + 27 : x + 21)
74 #define PREP_IE_ORIG_SN(x) u32_field_get(x, 27, AE_F_SET(x))
75 #define PREP_IE_LIFETIME(x) u32_field_get(x, 13, AE_F_SET(x))
76 #define PREP_IE_METRIC(x) u32_field_get(x, 17, AE_F_SET(x))
77 #define PREP_IE_TARGET_ADDR(x) (x + 3)
78 #define PREP_IE_TARGET_SN(x) u32_field_get(x, 9, 0)
80 #define PERR_IE_TTL(x) (*(x))
81 #define PERR_IE_TARGET_FLAGS(x) (*(x + 2))
82 #define PERR_IE_TARGET_ADDR(x) (x + 3)
83 #define PERR_IE_TARGET_SN(x) u32_field_get(x, 9, 0)
84 #define PERR_IE_TARGET_RCODE(x) u16_field_get(x, 13, 0)
86 #define MSEC_TO_TU(x) (x*1000/1024)
87 #define SN_GT(x, y) ((long) (y) - (long) (x) < 0)
88 #define SN_LT(x, y) ((long) (x) - (long) (y) < 0)
90 #define net_traversal_jiffies(s) \
91 msecs_to_jiffies(s->u.mesh.mshcfg.dot11MeshHWMPnetDiameterTraversalTime)
92 #define default_lifetime(s) \
93 MSEC_TO_TU(s->u.mesh.mshcfg.dot11MeshHWMPactivePathTimeout)
94 #define min_preq_int_jiff(s) \
95 (msecs_to_jiffies(s->u.mesh.mshcfg.dot11MeshHWMPpreqMinInterval))
96 #define max_preq_retries(s) (s->u.mesh.mshcfg.dot11MeshHWMPmaxPREQretries)
97 #define disc_timeout_jiff(s) \
98 msecs_to_jiffies(sdata->u.mesh.mshcfg.min_discovery_timeout)
100 enum mpath_frame_type
{
107 static const u8 broadcast_addr
[ETH_ALEN
] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
109 static int mesh_path_sel_frame_tx(enum mpath_frame_type action
, u8 flags
,
110 u8
*orig_addr
, __le32 orig_sn
, u8 target_flags
, u8
*target
,
111 __le32 target_sn
, const u8
*da
, u8 hop_count
, u8 ttl
,
112 __le32 lifetime
, __le32 metric
, __le32 preq_id
,
113 struct ieee80211_sub_if_data
*sdata
)
115 struct ieee80211_local
*local
= sdata
->local
;
116 struct sk_buff
*skb
= dev_alloc_skb(local
->hw
.extra_tx_headroom
+ 400);
117 struct ieee80211_mgmt
*mgmt
;
123 skb_reserve(skb
, local
->hw
.extra_tx_headroom
);
124 /* 25 is the size of the common mgmt part (24) plus the size of the
125 * common action part (1)
127 mgmt
= (struct ieee80211_mgmt
*)
128 skb_put(skb
, 25 + sizeof(mgmt
->u
.action
.u
.mesh_action
));
129 memset(mgmt
, 0, 25 + sizeof(mgmt
->u
.action
.u
.mesh_action
));
130 mgmt
->frame_control
= cpu_to_le16(IEEE80211_FTYPE_MGMT
|
131 IEEE80211_STYPE_ACTION
);
133 memcpy(mgmt
->da
, da
, ETH_ALEN
);
134 memcpy(mgmt
->sa
, sdata
->vif
.addr
, ETH_ALEN
);
136 memcpy(mgmt
->bssid
, sdata
->vif
.addr
, ETH_ALEN
);
137 mgmt
->u
.action
.category
= WLAN_CATEGORY_MESH_ACTION
;
138 mgmt
->u
.action
.u
.mesh_action
.action_code
=
139 WLAN_MESH_ACTION_HWMP_PATH_SELECTION
;
143 mhwmp_dbg("sending PREQ to %pM", target
);
145 pos
= skb_put(skb
, 2 + ie_len
);
146 *pos
++ = WLAN_EID_PREQ
;
149 mhwmp_dbg("sending PREP to %pM", target
);
151 pos
= skb_put(skb
, 2 + ie_len
);
152 *pos
++ = WLAN_EID_PREP
;
155 mhwmp_dbg("sending RANN from %pM", orig_addr
);
156 ie_len
= sizeof(struct ieee80211_rann_ie
);
157 pos
= skb_put(skb
, 2 + ie_len
);
158 *pos
++ = WLAN_EID_RANN
;
169 if (action
== MPATH_PREP
) {
170 memcpy(pos
, target
, ETH_ALEN
);
172 memcpy(pos
, &target_sn
, 4);
175 if (action
== MPATH_PREQ
) {
176 memcpy(pos
, &preq_id
, 4);
179 memcpy(pos
, orig_addr
, ETH_ALEN
);
181 memcpy(pos
, &orig_sn
, 4);
184 memcpy(pos
, &lifetime
, 4); /* interval for RANN */
186 memcpy(pos
, &metric
, 4);
188 if (action
== MPATH_PREQ
) {
189 *pos
++ = 1; /* destination count */
190 *pos
++ = target_flags
;
191 memcpy(pos
, target
, ETH_ALEN
);
193 memcpy(pos
, &target_sn
, 4);
195 } else if (action
== MPATH_PREP
) {
196 memcpy(pos
, orig_addr
, ETH_ALEN
);
198 memcpy(pos
, &orig_sn
, 4);
202 ieee80211_tx_skb(sdata
, skb
);
207 /* Headroom is not adjusted. Caller should ensure that skb has sufficient
208 * headroom in case the frame is encrypted. */
209 static void prepare_frame_for_deferred_tx(struct ieee80211_sub_if_data
*sdata
,
212 struct ieee80211_tx_info
*info
= IEEE80211_SKB_CB(skb
);
214 skb_set_mac_header(skb
, 0);
215 skb_set_network_header(skb
, 0);
216 skb_set_transport_header(skb
, 0);
218 /* Send all internal mgmt frames on VO. Accordingly set TID to 7. */
219 skb_set_queue_mapping(skb
, IEEE80211_AC_VO
);
222 info
->control
.vif
= &sdata
->vif
;
223 ieee80211_set_qos_hdr(sdata
, skb
);
227 * mesh_send_path error - Sends a PERR mesh management frame
229 * @target: broken destination
230 * @target_sn: SN of the broken destination
231 * @target_rcode: reason code for this PERR
232 * @ra: node this frame is addressed to
234 * Note: This function may be called with driver locks taken that the driver
235 * also acquires in the TX path. To avoid a deadlock we don't transmit the
236 * frame directly but add it to the pending queue instead.
238 int mesh_path_error_tx(u8 ttl
, u8
*target
, __le32 target_sn
,
239 __le16 target_rcode
, const u8
*ra
,
240 struct ieee80211_sub_if_data
*sdata
)
242 struct ieee80211_local
*local
= sdata
->local
;
243 struct sk_buff
*skb
= dev_alloc_skb(local
->hw
.extra_tx_headroom
+ 400);
244 struct ieee80211_mgmt
*mgmt
;
250 skb_reserve(skb
, local
->tx_headroom
+ local
->hw
.extra_tx_headroom
);
251 /* 25 is the size of the common mgmt part (24) plus the size of the
252 * common action part (1)
254 mgmt
= (struct ieee80211_mgmt
*)
255 skb_put(skb
, 25 + sizeof(mgmt
->u
.action
.u
.mesh_action
));
256 memset(mgmt
, 0, 25 + sizeof(mgmt
->u
.action
.u
.mesh_action
));
257 mgmt
->frame_control
= cpu_to_le16(IEEE80211_FTYPE_MGMT
|
258 IEEE80211_STYPE_ACTION
);
260 memcpy(mgmt
->da
, ra
, ETH_ALEN
);
261 memcpy(mgmt
->sa
, sdata
->vif
.addr
, ETH_ALEN
);
263 memcpy(mgmt
->bssid
, sdata
->vif
.addr
, ETH_ALEN
);
264 mgmt
->u
.action
.category
= WLAN_CATEGORY_MESH_ACTION
;
265 mgmt
->u
.action
.u
.mesh_action
.action_code
=
266 WLAN_MESH_ACTION_HWMP_PATH_SELECTION
;
268 pos
= skb_put(skb
, 2 + ie_len
);
269 *pos
++ = WLAN_EID_PERR
;
273 /* number of destinations */
276 * flags bit, bit 1 is unset if we know the sequence number and
277 * bit 2 is set if we have a reason code
285 memcpy(pos
, target
, ETH_ALEN
);
287 memcpy(pos
, &target_sn
, 4);
289 memcpy(pos
, &target_rcode
, 2);
291 /* see note in function header */
292 prepare_frame_for_deferred_tx(sdata
, skb
);
293 ieee80211_add_pending_skb(local
, skb
);
297 void ieee80211s_update_metric(struct ieee80211_local
*local
,
298 struct sta_info
*stainfo
, struct sk_buff
*skb
)
300 struct ieee80211_tx_info
*txinfo
= IEEE80211_SKB_CB(skb
);
301 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*) skb
->data
;
304 if (!ieee80211_is_data(hdr
->frame_control
))
307 failed
= !(txinfo
->flags
& IEEE80211_TX_STAT_ACK
);
309 /* moving average, scaled to 100 */
310 stainfo
->fail_avg
= ((80 * stainfo
->fail_avg
+ 5) / 100 + 20 * failed
);
311 if (stainfo
->fail_avg
> 95)
312 mesh_plink_broken(stainfo
);
315 static u32
airtime_link_metric_get(struct ieee80211_local
*local
,
316 struct sta_info
*sta
)
318 struct ieee80211_supported_band
*sband
;
319 /* This should be adjusted for each device */
320 int device_constant
= 1 << ARITH_SHIFT
;
321 int test_frame_len
= TEST_FRAME_LEN
<< ARITH_SHIFT
;
322 int s_unit
= 1 << ARITH_SHIFT
;
324 u32 tx_time
, estimated_retx
;
327 sband
= local
->hw
.wiphy
->bands
[local
->hw
.conf
.channel
->band
];
329 if (sta
->fail_avg
>= 100)
332 if (sta
->last_tx_rate
.flags
& IEEE80211_TX_RC_MCS
)
335 err
= (sta
->fail_avg
<< ARITH_SHIFT
) / 100;
337 /* bitrate is in units of 100 Kbps, while we need rate in units of
338 * 1Mbps. This will be corrected on tx_time computation.
340 rate
= sband
->bitrates
[sta
->last_tx_rate
.idx
].bitrate
;
341 tx_time
= (device_constant
+ 10 * test_frame_len
/ rate
);
342 estimated_retx
= ((1 << (2 * ARITH_SHIFT
)) / (s_unit
- err
));
343 result
= (tx_time
* estimated_retx
) >> (2 * ARITH_SHIFT
) ;
348 * hwmp_route_info_get - Update routing info to originator and transmitter
350 * @sdata: local mesh subif
351 * @mgmt: mesh management frame
352 * @hwmp_ie: hwmp information element (PREP or PREQ)
354 * This function updates the path routing information to the originator and the
355 * transmitter of a HWMP PREQ or PREP frame.
357 * Returns: metric to frame originator or 0 if the frame should not be further
360 * Notes: this function is the only place (besides user-provided info) where
361 * path routing information is updated.
363 static u32
hwmp_route_info_get(struct ieee80211_sub_if_data
*sdata
,
364 struct ieee80211_mgmt
*mgmt
,
365 u8
*hwmp_ie
, enum mpath_frame_type action
)
367 struct ieee80211_local
*local
= sdata
->local
;
368 struct mesh_path
*mpath
;
369 struct sta_info
*sta
;
372 u32 orig_sn
, orig_metric
;
373 unsigned long orig_lifetime
, exp_time
;
374 u32 last_hop_metric
, new_metric
;
378 sta
= sta_info_get(sdata
, mgmt
->sa
);
384 last_hop_metric
= airtime_link_metric_get(local
, sta
);
385 /* Update and check originator routing info */
390 orig_addr
= PREQ_IE_ORIG_ADDR(hwmp_ie
);
391 orig_sn
= PREQ_IE_ORIG_SN(hwmp_ie
);
392 orig_lifetime
= PREQ_IE_LIFETIME(hwmp_ie
);
393 orig_metric
= PREQ_IE_METRIC(hwmp_ie
);
396 /* Originator here refers to the MP that was the destination in
397 * the Path Request. The draft refers to that MP as the
398 * destination address, even though usually it is the origin of
399 * the PREP frame. We divert from the nomenclature in the draft
400 * so that we can easily use a single function to gather path
401 * information from both PREQ and PREP frames.
403 orig_addr
= PREP_IE_ORIG_ADDR(hwmp_ie
);
404 orig_sn
= PREP_IE_ORIG_SN(hwmp_ie
);
405 orig_lifetime
= PREP_IE_LIFETIME(hwmp_ie
);
406 orig_metric
= PREP_IE_METRIC(hwmp_ie
);
412 new_metric
= orig_metric
+ last_hop_metric
;
413 if (new_metric
< orig_metric
)
414 new_metric
= MAX_METRIC
;
415 exp_time
= TU_TO_EXP_TIME(orig_lifetime
);
417 if (memcmp(orig_addr
, sdata
->vif
.addr
, ETH_ALEN
) == 0) {
418 /* This MP is the originator, we are not interested in this
419 * frame, except for updating transmitter's path info.
424 mpath
= mesh_path_lookup(orig_addr
, sdata
);
426 spin_lock_bh(&mpath
->state_lock
);
427 if (mpath
->flags
& MESH_PATH_FIXED
)
429 else if ((mpath
->flags
& MESH_PATH_ACTIVE
) &&
430 (mpath
->flags
& MESH_PATH_SN_VALID
)) {
431 if (SN_GT(mpath
->sn
, orig_sn
) ||
432 (mpath
->sn
== orig_sn
&&
433 new_metric
>= mpath
->metric
)) {
439 mesh_path_add(orig_addr
, sdata
);
440 mpath
= mesh_path_lookup(orig_addr
, sdata
);
445 spin_lock_bh(&mpath
->state_lock
);
449 mesh_path_assign_nexthop(mpath
, sta
);
450 mpath
->flags
|= MESH_PATH_SN_VALID
;
451 mpath
->metric
= new_metric
;
453 mpath
->exp_time
= time_after(mpath
->exp_time
, exp_time
)
454 ? mpath
->exp_time
: exp_time
;
455 mesh_path_activate(mpath
);
456 spin_unlock_bh(&mpath
->state_lock
);
457 mesh_path_tx_pending(mpath
);
458 /* draft says preq_id should be saved to, but there does
459 * not seem to be any use for it, skipping by now
462 spin_unlock_bh(&mpath
->state_lock
);
465 /* Update and check transmitter routing info */
467 if (memcmp(orig_addr
, ta
, ETH_ALEN
) == 0)
472 mpath
= mesh_path_lookup(ta
, sdata
);
474 spin_lock_bh(&mpath
->state_lock
);
475 if ((mpath
->flags
& MESH_PATH_FIXED
) ||
476 ((mpath
->flags
& MESH_PATH_ACTIVE
) &&
477 (last_hop_metric
> mpath
->metric
)))
480 mesh_path_add(ta
, sdata
);
481 mpath
= mesh_path_lookup(ta
, sdata
);
486 spin_lock_bh(&mpath
->state_lock
);
490 mesh_path_assign_nexthop(mpath
, sta
);
491 mpath
->metric
= last_hop_metric
;
492 mpath
->exp_time
= time_after(mpath
->exp_time
, exp_time
)
493 ? mpath
->exp_time
: exp_time
;
494 mesh_path_activate(mpath
);
495 spin_unlock_bh(&mpath
->state_lock
);
496 mesh_path_tx_pending(mpath
);
498 spin_unlock_bh(&mpath
->state_lock
);
503 return process
? new_metric
: 0;
506 static void hwmp_preq_frame_process(struct ieee80211_sub_if_data
*sdata
,
507 struct ieee80211_mgmt
*mgmt
,
508 u8
*preq_elem
, u32 metric
)
510 struct ieee80211_if_mesh
*ifmsh
= &sdata
->u
.mesh
;
511 struct mesh_path
*mpath
;
512 u8
*target_addr
, *orig_addr
;
513 u8 target_flags
, ttl
;
514 u32 orig_sn
, target_sn
, lifetime
;
518 /* Update target SN, if present */
519 target_addr
= PREQ_IE_TARGET_ADDR(preq_elem
);
520 orig_addr
= PREQ_IE_ORIG_ADDR(preq_elem
);
521 target_sn
= PREQ_IE_TARGET_SN(preq_elem
);
522 orig_sn
= PREQ_IE_ORIG_SN(preq_elem
);
523 target_flags
= PREQ_IE_TARGET_F(preq_elem
);
525 mhwmp_dbg("received PREQ from %pM", orig_addr
);
527 if (memcmp(target_addr
, sdata
->vif
.addr
, ETH_ALEN
) == 0) {
528 mhwmp_dbg("PREQ is for us");
532 if (time_after(jiffies
, ifmsh
->last_sn_update
+
533 net_traversal_jiffies(sdata
)) ||
534 time_before(jiffies
, ifmsh
->last_sn_update
)) {
535 target_sn
= ++ifmsh
->sn
;
536 ifmsh
->last_sn_update
= jiffies
;
540 mpath
= mesh_path_lookup(target_addr
, sdata
);
542 if ((!(mpath
->flags
& MESH_PATH_SN_VALID
)) ||
543 SN_LT(mpath
->sn
, target_sn
)) {
544 mpath
->sn
= target_sn
;
545 mpath
->flags
|= MESH_PATH_SN_VALID
;
546 } else if ((!(target_flags
& MP_F_DO
)) &&
547 (mpath
->flags
& MESH_PATH_ACTIVE
)) {
549 metric
= mpath
->metric
;
550 target_sn
= mpath
->sn
;
551 if (target_flags
& MP_F_RF
)
552 target_flags
|= MP_F_DO
;
561 lifetime
= PREQ_IE_LIFETIME(preq_elem
);
562 ttl
= ifmsh
->mshcfg
.element_ttl
;
564 mhwmp_dbg("replying to the PREQ");
565 mesh_path_sel_frame_tx(MPATH_PREP
, 0, target_addr
,
566 cpu_to_le32(target_sn
), 0, orig_addr
,
567 cpu_to_le32(orig_sn
), mgmt
->sa
, 0, ttl
,
568 cpu_to_le32(lifetime
), cpu_to_le32(metric
),
571 ifmsh
->mshstats
.dropped_frames_ttl
++;
578 ttl
= PREQ_IE_TTL(preq_elem
);
579 lifetime
= PREQ_IE_LIFETIME(preq_elem
);
581 ifmsh
->mshstats
.dropped_frames_ttl
++;
584 mhwmp_dbg("forwarding the PREQ from %pM", orig_addr
);
586 flags
= PREQ_IE_FLAGS(preq_elem
);
587 preq_id
= PREQ_IE_PREQ_ID(preq_elem
);
588 hopcount
= PREQ_IE_HOPCOUNT(preq_elem
) + 1;
589 mesh_path_sel_frame_tx(MPATH_PREQ
, flags
, orig_addr
,
590 cpu_to_le32(orig_sn
), target_flags
, target_addr
,
591 cpu_to_le32(target_sn
), broadcast_addr
,
592 hopcount
, ttl
, cpu_to_le32(lifetime
),
593 cpu_to_le32(metric
), cpu_to_le32(preq_id
),
595 ifmsh
->mshstats
.fwded_mcast
++;
596 ifmsh
->mshstats
.fwded_frames
++;
601 static inline struct sta_info
*
602 next_hop_deref_protected(struct mesh_path
*mpath
)
604 return rcu_dereference_protected(mpath
->next_hop
,
605 lockdep_is_held(&mpath
->state_lock
));
609 static void hwmp_prep_frame_process(struct ieee80211_sub_if_data
*sdata
,
610 struct ieee80211_mgmt
*mgmt
,
611 u8
*prep_elem
, u32 metric
)
613 struct mesh_path
*mpath
;
614 u8
*target_addr
, *orig_addr
;
615 u8 ttl
, hopcount
, flags
;
616 u8 next_hop
[ETH_ALEN
];
617 u32 target_sn
, orig_sn
, lifetime
;
619 mhwmp_dbg("received PREP from %pM", PREP_IE_ORIG_ADDR(prep_elem
));
621 /* Note that we divert from the draft nomenclature and denominate
622 * destination to what the draft refers to as origininator. So in this
623 * function destnation refers to the final destination of the PREP,
624 * which corresponds with the originator of the PREQ which this PREP
627 target_addr
= PREP_IE_TARGET_ADDR(prep_elem
);
628 if (memcmp(target_addr
, sdata
->vif
.addr
, ETH_ALEN
) == 0)
629 /* destination, no forwarding required */
632 ttl
= PREP_IE_TTL(prep_elem
);
634 sdata
->u
.mesh
.mshstats
.dropped_frames_ttl
++;
639 mpath
= mesh_path_lookup(target_addr
, sdata
);
641 spin_lock_bh(&mpath
->state_lock
);
644 if (!(mpath
->flags
& MESH_PATH_ACTIVE
)) {
645 spin_unlock_bh(&mpath
->state_lock
);
648 memcpy(next_hop
, next_hop_deref_protected(mpath
)->sta
.addr
, ETH_ALEN
);
649 spin_unlock_bh(&mpath
->state_lock
);
651 flags
= PREP_IE_FLAGS(prep_elem
);
652 lifetime
= PREP_IE_LIFETIME(prep_elem
);
653 hopcount
= PREP_IE_HOPCOUNT(prep_elem
) + 1;
654 orig_addr
= PREP_IE_ORIG_ADDR(prep_elem
);
655 target_sn
= PREP_IE_TARGET_SN(prep_elem
);
656 orig_sn
= PREP_IE_ORIG_SN(prep_elem
);
658 mesh_path_sel_frame_tx(MPATH_PREP
, flags
, orig_addr
,
659 cpu_to_le32(orig_sn
), 0, target_addr
,
660 cpu_to_le32(target_sn
), next_hop
, hopcount
,
661 ttl
, cpu_to_le32(lifetime
), cpu_to_le32(metric
),
665 sdata
->u
.mesh
.mshstats
.fwded_unicast
++;
666 sdata
->u
.mesh
.mshstats
.fwded_frames
++;
671 sdata
->u
.mesh
.mshstats
.dropped_frames_no_route
++;
674 static void hwmp_perr_frame_process(struct ieee80211_sub_if_data
*sdata
,
675 struct ieee80211_mgmt
*mgmt
, u8
*perr_elem
)
677 struct ieee80211_if_mesh
*ifmsh
= &sdata
->u
.mesh
;
678 struct mesh_path
*mpath
;
680 u8
*ta
, *target_addr
;
685 ttl
= PERR_IE_TTL(perr_elem
);
687 ifmsh
->mshstats
.dropped_frames_ttl
++;
691 target_addr
= PERR_IE_TARGET_ADDR(perr_elem
);
692 target_sn
= PERR_IE_TARGET_SN(perr_elem
);
693 target_rcode
= PERR_IE_TARGET_RCODE(perr_elem
);
696 mpath
= mesh_path_lookup(target_addr
, sdata
);
698 spin_lock_bh(&mpath
->state_lock
);
699 if (mpath
->flags
& MESH_PATH_ACTIVE
&&
700 memcmp(ta
, next_hop_deref_protected(mpath
)->sta
.addr
,
702 (!(mpath
->flags
& MESH_PATH_SN_VALID
) ||
703 SN_GT(target_sn
, mpath
->sn
))) {
704 mpath
->flags
&= ~MESH_PATH_ACTIVE
;
705 mpath
->sn
= target_sn
;
706 spin_unlock_bh(&mpath
->state_lock
);
707 mesh_path_error_tx(ttl
, target_addr
, cpu_to_le32(target_sn
),
708 cpu_to_le16(target_rcode
),
709 broadcast_addr
, sdata
);
711 spin_unlock_bh(&mpath
->state_lock
);
716 static void hwmp_rann_frame_process(struct ieee80211_sub_if_data
*sdata
,
717 struct ieee80211_mgmt
*mgmt
,
718 struct ieee80211_rann_ie
*rann
)
720 struct ieee80211_if_mesh
*ifmsh
= &sdata
->u
.mesh
;
721 struct mesh_path
*mpath
;
722 u8 ttl
, flags
, hopcount
;
725 u32 interval
= ifmsh
->mshcfg
.dot11MeshHWMPRannInterval
;
728 ttl
= rann
->rann_ttl
;
730 ifmsh
->mshstats
.dropped_frames_ttl
++;
734 flags
= rann
->rann_flags
;
735 root_is_gate
= !!(flags
& RANN_FLAG_IS_GATE
);
736 orig_addr
= rann
->rann_addr
;
737 orig_sn
= rann
->rann_seq
;
738 hopcount
= rann
->rann_hopcount
;
740 metric
= rann
->rann_metric
;
742 /* Ignore our own RANNs */
743 if (memcmp(orig_addr
, sdata
->vif
.addr
, ETH_ALEN
) == 0)
746 mhwmp_dbg("received RANN from %pM (is_gate=%d)", orig_addr
,
750 mpath
= mesh_path_lookup(orig_addr
, sdata
);
752 mesh_path_add(orig_addr
, sdata
);
753 mpath
= mesh_path_lookup(orig_addr
, sdata
);
756 sdata
->u
.mesh
.mshstats
.dropped_frames_no_route
++;
761 if ((!(mpath
->flags
& (MESH_PATH_ACTIVE
| MESH_PATH_RESOLVING
)) ||
762 time_after(jiffies
, mpath
->exp_time
- 1*HZ
)) &&
763 !(mpath
->flags
& MESH_PATH_FIXED
)) {
764 mhwmp_dbg("%s time to refresh root mpath %pM", sdata
->name
,
766 mesh_queue_preq(mpath
, PREQ_Q_F_START
| PREQ_Q_F_REFRESH
);
769 if (mpath
->sn
< orig_sn
) {
770 mesh_path_sel_frame_tx(MPATH_RANN
, flags
, orig_addr
,
771 cpu_to_le32(orig_sn
),
772 0, NULL
, 0, broadcast_addr
,
773 hopcount
, ttl
, cpu_to_le32(interval
),
774 cpu_to_le32(metric
+ mpath
->metric
),
779 mesh_path_add_gate(mpath
);
785 void mesh_rx_path_sel_frame(struct ieee80211_sub_if_data
*sdata
,
786 struct ieee80211_mgmt
*mgmt
,
789 struct ieee802_11_elems elems
;
792 struct sta_info
*sta
;
794 /* need action_code */
795 if (len
< IEEE80211_MIN_ACTION_SIZE
+ 1)
799 sta
= sta_info_get(sdata
, mgmt
->sa
);
800 if (!sta
|| sta
->plink_state
!= NL80211_PLINK_ESTAB
) {
806 baselen
= (u8
*) mgmt
->u
.action
.u
.mesh_action
.variable
- (u8
*) mgmt
;
807 ieee802_11_parse_elems(mgmt
->u
.action
.u
.mesh_action
.variable
,
808 len
- baselen
, &elems
);
811 if (elems
.preq_len
!= 37)
812 /* Right now we support just 1 destination and no AE */
814 last_hop_metric
= hwmp_route_info_get(sdata
, mgmt
, elems
.preq
,
817 hwmp_preq_frame_process(sdata
, mgmt
, elems
.preq
,
821 if (elems
.prep_len
!= 31)
822 /* Right now we support no AE */
824 last_hop_metric
= hwmp_route_info_get(sdata
, mgmt
, elems
.prep
,
827 hwmp_prep_frame_process(sdata
, mgmt
, elems
.prep
,
831 if (elems
.perr_len
!= 15)
832 /* Right now we support only one destination per PERR */
834 hwmp_perr_frame_process(sdata
, mgmt
, elems
.perr
);
837 hwmp_rann_frame_process(sdata
, mgmt
, elems
.rann
);
841 * mesh_queue_preq - queue a PREQ to a given destination
843 * @mpath: mesh path to discover
844 * @flags: special attributes of the PREQ to be sent
846 * Locking: the function must be called from within a rcu read lock block.
849 static void mesh_queue_preq(struct mesh_path
*mpath
, u8 flags
)
851 struct ieee80211_sub_if_data
*sdata
= mpath
->sdata
;
852 struct ieee80211_if_mesh
*ifmsh
= &sdata
->u
.mesh
;
853 struct mesh_preq_queue
*preq_node
;
855 preq_node
= kmalloc(sizeof(struct mesh_preq_queue
), GFP_ATOMIC
);
857 mhwmp_dbg("could not allocate PREQ node");
861 spin_lock_bh(&ifmsh
->mesh_preq_queue_lock
);
862 if (ifmsh
->preq_queue_len
== MAX_PREQ_QUEUE_LEN
) {
863 spin_unlock_bh(&ifmsh
->mesh_preq_queue_lock
);
865 if (printk_ratelimit())
866 mhwmp_dbg("PREQ node queue full");
870 memcpy(preq_node
->dst
, mpath
->dst
, ETH_ALEN
);
871 preq_node
->flags
= flags
;
873 list_add_tail(&preq_node
->list
, &ifmsh
->preq_queue
.list
);
874 ++ifmsh
->preq_queue_len
;
875 spin_unlock_bh(&ifmsh
->mesh_preq_queue_lock
);
877 if (time_after(jiffies
, ifmsh
->last_preq
+ min_preq_int_jiff(sdata
)))
878 ieee80211_queue_work(&sdata
->local
->hw
, &sdata
->work
);
880 else if (time_before(jiffies
, ifmsh
->last_preq
)) {
881 /* avoid long wait if did not send preqs for a long time
882 * and jiffies wrapped around
884 ifmsh
->last_preq
= jiffies
- min_preq_int_jiff(sdata
) - 1;
885 ieee80211_queue_work(&sdata
->local
->hw
, &sdata
->work
);
887 mod_timer(&ifmsh
->mesh_path_timer
, ifmsh
->last_preq
+
888 min_preq_int_jiff(sdata
));
892 * mesh_path_start_discovery - launch a path discovery from the PREQ queue
894 * @sdata: local mesh subif
896 void mesh_path_start_discovery(struct ieee80211_sub_if_data
*sdata
)
898 struct ieee80211_if_mesh
*ifmsh
= &sdata
->u
.mesh
;
899 struct mesh_preq_queue
*preq_node
;
900 struct mesh_path
*mpath
;
901 u8 ttl
, target_flags
;
904 spin_lock_bh(&ifmsh
->mesh_preq_queue_lock
);
905 if (!ifmsh
->preq_queue_len
||
906 time_before(jiffies
, ifmsh
->last_preq
+
907 min_preq_int_jiff(sdata
))) {
908 spin_unlock_bh(&ifmsh
->mesh_preq_queue_lock
);
912 preq_node
= list_first_entry(&ifmsh
->preq_queue
.list
,
913 struct mesh_preq_queue
, list
);
914 list_del(&preq_node
->list
);
915 --ifmsh
->preq_queue_len
;
916 spin_unlock_bh(&ifmsh
->mesh_preq_queue_lock
);
919 mpath
= mesh_path_lookup(preq_node
->dst
, sdata
);
923 spin_lock_bh(&mpath
->state_lock
);
924 if (preq_node
->flags
& PREQ_Q_F_START
) {
925 if (mpath
->flags
& MESH_PATH_RESOLVING
) {
926 spin_unlock_bh(&mpath
->state_lock
);
929 mpath
->flags
&= ~MESH_PATH_RESOLVED
;
930 mpath
->flags
|= MESH_PATH_RESOLVING
;
931 mpath
->discovery_retries
= 0;
932 mpath
->discovery_timeout
= disc_timeout_jiff(sdata
);
934 } else if (!(mpath
->flags
& MESH_PATH_RESOLVING
) ||
935 mpath
->flags
& MESH_PATH_RESOLVED
) {
936 mpath
->flags
&= ~MESH_PATH_RESOLVING
;
937 spin_unlock_bh(&mpath
->state_lock
);
941 ifmsh
->last_preq
= jiffies
;
943 if (time_after(jiffies
, ifmsh
->last_sn_update
+
944 net_traversal_jiffies(sdata
)) ||
945 time_before(jiffies
, ifmsh
->last_sn_update
)) {
947 sdata
->u
.mesh
.last_sn_update
= jiffies
;
949 lifetime
= default_lifetime(sdata
);
950 ttl
= sdata
->u
.mesh
.mshcfg
.element_ttl
;
952 sdata
->u
.mesh
.mshstats
.dropped_frames_ttl
++;
953 spin_unlock_bh(&mpath
->state_lock
);
957 if (preq_node
->flags
& PREQ_Q_F_REFRESH
)
958 target_flags
= MP_F_DO
;
960 target_flags
= MP_F_RF
;
962 spin_unlock_bh(&mpath
->state_lock
);
963 mesh_path_sel_frame_tx(MPATH_PREQ
, 0, sdata
->vif
.addr
,
964 cpu_to_le32(ifmsh
->sn
), target_flags
, mpath
->dst
,
965 cpu_to_le32(mpath
->sn
), broadcast_addr
, 0,
966 ttl
, cpu_to_le32(lifetime
), 0,
967 cpu_to_le32(ifmsh
->preq_id
++), sdata
);
968 mod_timer(&mpath
->timer
, jiffies
+ mpath
->discovery_timeout
);
976 * mesh_nexthop_lookup - put the appropriate next hop on a mesh frame
978 * @skb: 802.11 frame to be sent
979 * @sdata: network subif the frame will be sent through
981 * Returns: 0 if the next hop was found. Nonzero otherwise. If no next hop is
982 * found, the function will start a path discovery and queue the frame so it is
983 * sent when the path is resolved. This means the caller must not free the skb
986 int mesh_nexthop_lookup(struct sk_buff
*skb
,
987 struct ieee80211_sub_if_data
*sdata
)
989 struct sk_buff
*skb_to_free
= NULL
;
990 struct mesh_path
*mpath
;
991 struct sta_info
*next_hop
;
992 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*) skb
->data
;
993 u8
*target_addr
= hdr
->addr3
;
997 mpath
= mesh_path_lookup(target_addr
, sdata
);
1000 mesh_path_add(target_addr
, sdata
);
1001 mpath
= mesh_path_lookup(target_addr
, sdata
);
1003 sdata
->u
.mesh
.mshstats
.dropped_frames_no_route
++;
1009 if (mpath
->flags
& MESH_PATH_ACTIVE
) {
1010 if (time_after(jiffies
,
1012 msecs_to_jiffies(sdata
->u
.mesh
.mshcfg
.path_refresh_time
)) &&
1013 !memcmp(sdata
->vif
.addr
, hdr
->addr4
, ETH_ALEN
) &&
1014 !(mpath
->flags
& MESH_PATH_RESOLVING
) &&
1015 !(mpath
->flags
& MESH_PATH_FIXED
)) {
1016 mesh_queue_preq(mpath
,
1017 PREQ_Q_F_START
| PREQ_Q_F_REFRESH
);
1019 next_hop
= rcu_dereference(mpath
->next_hop
);
1021 memcpy(hdr
->addr1
, next_hop
->sta
.addr
, ETH_ALEN
);
1025 struct ieee80211_tx_info
*info
= IEEE80211_SKB_CB(skb
);
1026 if (!(mpath
->flags
& MESH_PATH_RESOLVING
)) {
1027 /* Start discovery only if it is not running yet */
1028 mesh_queue_preq(mpath
, PREQ_Q_F_START
);
1031 if (skb_queue_len(&mpath
->frame_queue
) >=
1032 MESH_FRAME_QUEUE_LEN
)
1033 skb_to_free
= skb_dequeue(&mpath
->frame_queue
);
1035 info
->flags
|= IEEE80211_TX_INTFL_NEED_TXPROCESSING
;
1036 skb_queue_tail(&mpath
->frame_queue
, skb
);
1038 mesh_path_discard_frame(skb_to_free
, sdata
);
1047 void mesh_path_timer(unsigned long data
)
1049 struct mesh_path
*mpath
= (void *) data
;
1050 struct ieee80211_sub_if_data
*sdata
= mpath
->sdata
;
1053 if (sdata
->local
->quiescing
)
1056 spin_lock_bh(&mpath
->state_lock
);
1057 if (mpath
->flags
& MESH_PATH_RESOLVED
||
1058 (!(mpath
->flags
& MESH_PATH_RESOLVING
))) {
1059 mpath
->flags
&= ~(MESH_PATH_RESOLVING
| MESH_PATH_RESOLVED
);
1060 spin_unlock_bh(&mpath
->state_lock
);
1061 } else if (mpath
->discovery_retries
< max_preq_retries(sdata
)) {
1062 ++mpath
->discovery_retries
;
1063 mpath
->discovery_timeout
*= 2;
1064 spin_unlock_bh(&mpath
->state_lock
);
1065 mesh_queue_preq(mpath
, 0);
1068 mpath
->exp_time
= jiffies
;
1069 spin_unlock_bh(&mpath
->state_lock
);
1070 if (!mpath
->is_gate
&& mesh_gate_num(sdata
) > 0) {
1071 ret
= mesh_path_send_to_gates(mpath
);
1073 mhwmp_dbg("no gate was reachable");
1075 mesh_path_flush_pending(mpath
);
1080 mesh_path_tx_root_frame(struct ieee80211_sub_if_data
*sdata
)
1082 struct ieee80211_if_mesh
*ifmsh
= &sdata
->u
.mesh
;
1083 u32 interval
= ifmsh
->mshcfg
.dot11MeshHWMPRannInterval
;
1086 flags
= (ifmsh
->mshcfg
.dot11MeshGateAnnouncementProtocol
)
1087 ? RANN_FLAG_IS_GATE
: 0;
1088 mesh_path_sel_frame_tx(MPATH_RANN
, flags
, sdata
->vif
.addr
,
1089 cpu_to_le32(++ifmsh
->sn
),
1090 0, NULL
, 0, broadcast_addr
,
1091 0, sdata
->u
.mesh
.mshcfg
.element_ttl
,
1092 cpu_to_le32(interval
), 0, 0, sdata
);