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>
11 #include <linux/etherdevice.h>
12 #include <asm/unaligned.h>
16 #ifdef CONFIG_MAC80211_VERBOSE_MHWMP_DEBUG
17 #define mhwmp_dbg(fmt, args...) \
18 pr_debug("Mesh HWMP (%s): " fmt "\n", sdata->name, ##args)
20 #define mhwmp_dbg(fmt, args...) do { (void)(0); } while (0)
23 #define TEST_FRAME_LEN 8192
24 #define MAX_METRIC 0xffffffff
27 /* Number of frames buffered per destination for unresolved destinations */
28 #define MESH_FRAME_QUEUE_LEN 10
29 #define MAX_PREQ_QUEUE_LEN 64
31 /* Destination only */
33 /* Reply and forward */
35 /* Unknown Sequence Number */
37 /* Reason code Present */
38 #define MP_F_RCODE 0x02
40 static void mesh_queue_preq(struct mesh_path *, u8);
42 static inline u32 u32_field_get(u8 *preq_elem, int offset, bool ae)
46 return get_unaligned_le32(preq_elem + offset);
49 static inline u32 u16_field_get(u8 *preq_elem, int offset, bool ae)
53 return get_unaligned_le16(preq_elem + offset);
56 /* HWMP IE processing macros */
58 #define AE_F_SET(x) (*x & AE_F)
59 #define PREQ_IE_FLAGS(x) (*(x))
60 #define PREQ_IE_HOPCOUNT(x) (*(x + 1))
61 #define PREQ_IE_TTL(x) (*(x + 2))
62 #define PREQ_IE_PREQ_ID(x) u32_field_get(x, 3, 0)
63 #define PREQ_IE_ORIG_ADDR(x) (x + 7)
64 #define PREQ_IE_ORIG_SN(x) u32_field_get(x, 13, 0)
65 #define PREQ_IE_LIFETIME(x) u32_field_get(x, 17, AE_F_SET(x))
66 #define PREQ_IE_METRIC(x) u32_field_get(x, 21, AE_F_SET(x))
67 #define PREQ_IE_TARGET_F(x) (*(AE_F_SET(x) ? x + 32 : x + 26))
68 #define PREQ_IE_TARGET_ADDR(x) (AE_F_SET(x) ? x + 33 : x + 27)
69 #define PREQ_IE_TARGET_SN(x) u32_field_get(x, 33, AE_F_SET(x))
72 #define PREP_IE_FLAGS(x) PREQ_IE_FLAGS(x)
73 #define PREP_IE_HOPCOUNT(x) PREQ_IE_HOPCOUNT(x)
74 #define PREP_IE_TTL(x) PREQ_IE_TTL(x)
75 #define PREP_IE_ORIG_ADDR(x) (AE_F_SET(x) ? x + 27 : x + 21)
76 #define PREP_IE_ORIG_SN(x) u32_field_get(x, 27, AE_F_SET(x))
77 #define PREP_IE_LIFETIME(x) u32_field_get(x, 13, AE_F_SET(x))
78 #define PREP_IE_METRIC(x) u32_field_get(x, 17, AE_F_SET(x))
79 #define PREP_IE_TARGET_ADDR(x) (x + 3)
80 #define PREP_IE_TARGET_SN(x) u32_field_get(x, 9, 0)
82 #define PERR_IE_TTL(x) (*(x))
83 #define PERR_IE_TARGET_FLAGS(x) (*(x + 2))
84 #define PERR_IE_TARGET_ADDR(x) (x + 3)
85 #define PERR_IE_TARGET_SN(x) u32_field_get(x, 9, 0)
86 #define PERR_IE_TARGET_RCODE(x) u16_field_get(x, 13, 0)
88 #define MSEC_TO_TU(x) (x*1000/1024)
89 #define SN_GT(x, y) ((s32)(y - x) < 0)
90 #define SN_LT(x, y) ((s32)(x - y) < 0)
92 #define net_traversal_jiffies(s) \
93 msecs_to_jiffies(s->u.mesh.mshcfg.dot11MeshHWMPnetDiameterTraversalTime)
94 #define default_lifetime(s) \
95 MSEC_TO_TU(s->u.mesh.mshcfg.dot11MeshHWMPactivePathTimeout)
96 #define min_preq_int_jiff(s) \
97 (msecs_to_jiffies(s->u.mesh.mshcfg.dot11MeshHWMPpreqMinInterval))
98 #define max_preq_retries(s) (s->u.mesh.mshcfg.dot11MeshHWMPmaxPREQretries)
99 #define disc_timeout_jiff(s) \
100 msecs_to_jiffies(sdata->u.mesh.mshcfg.min_discovery_timeout)
102 enum mpath_frame_type {
109 static const u8 broadcast_addr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
111 static int mesh_path_sel_frame_tx(enum mpath_frame_type action, u8 flags,
112 u8 *orig_addr, __le32 orig_sn, u8 target_flags, u8 *target,
113 __le32 target_sn, const u8 *da, u8 hop_count, u8 ttl,
114 __le32 lifetime, __le32 metric, __le32 preq_id,
115 struct ieee80211_sub_if_data *sdata)
117 struct ieee80211_local *local = sdata->local;
119 struct ieee80211_mgmt *mgmt;
121 int hdr_len = offsetof(struct ieee80211_mgmt, u.action.u.mesh_action) +
122 sizeof(mgmt->u.action.u.mesh_action);
124 skb = dev_alloc_skb(local->tx_headroom +
126 2 + 37); /* max HWMP IE */
129 skb_reserve(skb, local->tx_headroom);
130 mgmt = (struct ieee80211_mgmt *) skb_put(skb, hdr_len);
131 memset(mgmt, 0, hdr_len);
132 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
133 IEEE80211_STYPE_ACTION);
135 memcpy(mgmt->da, da, ETH_ALEN);
136 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
138 memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
139 mgmt->u.action.category = WLAN_CATEGORY_MESH_ACTION;
140 mgmt->u.action.u.mesh_action.action_code =
141 WLAN_MESH_ACTION_HWMP_PATH_SELECTION;
145 mhwmp_dbg("sending PREQ to %pM", target);
147 pos = skb_put(skb, 2 + ie_len);
148 *pos++ = WLAN_EID_PREQ;
151 mhwmp_dbg("sending PREP to %pM", target);
153 pos = skb_put(skb, 2 + ie_len);
154 *pos++ = WLAN_EID_PREP;
157 mhwmp_dbg("sending RANN from %pM", orig_addr);
158 ie_len = sizeof(struct ieee80211_rann_ie);
159 pos = skb_put(skb, 2 + ie_len);
160 *pos++ = WLAN_EID_RANN;
171 if (action == MPATH_PREP) {
172 memcpy(pos, target, ETH_ALEN);
174 memcpy(pos, &target_sn, 4);
177 if (action == MPATH_PREQ) {
178 memcpy(pos, &preq_id, 4);
181 memcpy(pos, orig_addr, ETH_ALEN);
183 memcpy(pos, &orig_sn, 4);
186 memcpy(pos, &lifetime, 4); /* interval for RANN */
188 memcpy(pos, &metric, 4);
190 if (action == MPATH_PREQ) {
191 *pos++ = 1; /* destination count */
192 *pos++ = target_flags;
193 memcpy(pos, target, ETH_ALEN);
195 memcpy(pos, &target_sn, 4);
197 } else if (action == MPATH_PREP) {
198 memcpy(pos, orig_addr, ETH_ALEN);
200 memcpy(pos, &orig_sn, 4);
204 ieee80211_tx_skb(sdata, skb);
209 /* Headroom is not adjusted. Caller should ensure that skb has sufficient
210 * headroom in case the frame is encrypted. */
211 static void prepare_frame_for_deferred_tx(struct ieee80211_sub_if_data *sdata,
214 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
216 skb_set_mac_header(skb, 0);
217 skb_set_network_header(skb, 0);
218 skb_set_transport_header(skb, 0);
220 /* Send all internal mgmt frames on VO. Accordingly set TID to 7. */
221 skb_set_queue_mapping(skb, IEEE80211_AC_VO);
224 info->control.vif = &sdata->vif;
225 ieee80211_set_qos_hdr(sdata, skb);
229 * mesh_send_path error - Sends a PERR mesh management frame
231 * @target: broken destination
232 * @target_sn: SN of the broken destination
233 * @target_rcode: reason code for this PERR
234 * @ra: node this frame is addressed to
236 * Note: This function may be called with driver locks taken that the driver
237 * also acquires in the TX path. To avoid a deadlock we don't transmit the
238 * frame directly but add it to the pending queue instead.
240 int mesh_path_error_tx(u8 ttl, u8 *target, __le32 target_sn,
241 __le16 target_rcode, const u8 *ra,
242 struct ieee80211_sub_if_data *sdata)
244 struct ieee80211_local *local = sdata->local;
246 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
247 struct ieee80211_mgmt *mgmt;
249 int hdr_len = offsetof(struct ieee80211_mgmt, u.action.u.mesh_action) +
250 sizeof(mgmt->u.action.u.mesh_action);
252 if (time_before(jiffies, ifmsh->next_perr))
255 skb = dev_alloc_skb(local->tx_headroom +
257 2 + 15 /* PERR IE */);
260 skb_reserve(skb, local->tx_headroom);
261 mgmt = (struct ieee80211_mgmt *) skb_put(skb, hdr_len);
262 memset(mgmt, 0, hdr_len);
263 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
264 IEEE80211_STYPE_ACTION);
266 memcpy(mgmt->da, ra, ETH_ALEN);
267 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
269 memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
270 mgmt->u.action.category = WLAN_CATEGORY_MESH_ACTION;
271 mgmt->u.action.u.mesh_action.action_code =
272 WLAN_MESH_ACTION_HWMP_PATH_SELECTION;
274 pos = skb_put(skb, 2 + ie_len);
275 *pos++ = WLAN_EID_PERR;
279 /* number of destinations */
282 * flags bit, bit 1 is unset if we know the sequence number and
283 * bit 2 is set if we have a reason code
291 memcpy(pos, target, ETH_ALEN);
293 memcpy(pos, &target_sn, 4);
295 memcpy(pos, &target_rcode, 2);
297 /* see note in function header */
298 prepare_frame_for_deferred_tx(sdata, skb);
299 ifmsh->next_perr = TU_TO_EXP_TIME(
300 ifmsh->mshcfg.dot11MeshHWMPperrMinInterval);
301 ieee80211_add_pending_skb(local, skb);
305 void ieee80211s_update_metric(struct ieee80211_local *local,
306 struct sta_info *stainfo, struct sk_buff *skb)
308 struct ieee80211_tx_info *txinfo = IEEE80211_SKB_CB(skb);
309 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
312 if (!ieee80211_is_data(hdr->frame_control))
315 failed = !(txinfo->flags & IEEE80211_TX_STAT_ACK);
317 /* moving average, scaled to 100 */
318 stainfo->fail_avg = ((80 * stainfo->fail_avg + 5) / 100 + 20 * failed);
319 if (stainfo->fail_avg > 95)
320 mesh_plink_broken(stainfo);
323 static u32 airtime_link_metric_get(struct ieee80211_local *local,
324 struct sta_info *sta)
326 struct ieee80211_supported_band *sband;
327 struct rate_info rinfo;
328 /* This should be adjusted for each device */
329 int device_constant = 1 << ARITH_SHIFT;
330 int test_frame_len = TEST_FRAME_LEN << ARITH_SHIFT;
331 int s_unit = 1 << ARITH_SHIFT;
333 u32 tx_time, estimated_retx;
336 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
338 if (sta->fail_avg >= 100)
341 sta_set_rate_info_tx(sta, &sta->last_tx_rate, &rinfo);
342 rate = cfg80211_calculate_bitrate(&rinfo);
346 err = (sta->fail_avg << ARITH_SHIFT) / 100;
348 /* bitrate is in units of 100 Kbps, while we need rate in units of
349 * 1Mbps. This will be corrected on tx_time computation.
351 tx_time = (device_constant + 10 * test_frame_len / rate);
352 estimated_retx = ((1 << (2 * ARITH_SHIFT)) / (s_unit - err));
353 result = (tx_time * estimated_retx) >> (2 * ARITH_SHIFT) ;
358 * hwmp_route_info_get - Update routing info to originator and transmitter
360 * @sdata: local mesh subif
361 * @mgmt: mesh management frame
362 * @hwmp_ie: hwmp information element (PREP or PREQ)
364 * This function updates the path routing information to the originator and the
365 * transmitter of a HWMP PREQ or PREP frame.
367 * Returns: metric to frame originator or 0 if the frame should not be further
370 * Notes: this function is the only place (besides user-provided info) where
371 * path routing information is updated.
373 static u32 hwmp_route_info_get(struct ieee80211_sub_if_data *sdata,
374 struct ieee80211_mgmt *mgmt,
375 u8 *hwmp_ie, enum mpath_frame_type action)
377 struct ieee80211_local *local = sdata->local;
378 struct mesh_path *mpath;
379 struct sta_info *sta;
382 u32 orig_sn, orig_metric;
383 unsigned long orig_lifetime, exp_time;
384 u32 last_hop_metric, new_metric;
388 sta = sta_info_get(sdata, mgmt->sa);
394 last_hop_metric = airtime_link_metric_get(local, sta);
395 /* Update and check originator routing info */
400 orig_addr = PREQ_IE_ORIG_ADDR(hwmp_ie);
401 orig_sn = PREQ_IE_ORIG_SN(hwmp_ie);
402 orig_lifetime = PREQ_IE_LIFETIME(hwmp_ie);
403 orig_metric = PREQ_IE_METRIC(hwmp_ie);
406 /* Originator here refers to the MP that was the target in the
407 * Path Request. We divert from the nomenclature in the draft
408 * so that we can easily use a single function to gather path
409 * information from both PREQ and PREP frames.
411 orig_addr = PREP_IE_TARGET_ADDR(hwmp_ie);
412 orig_sn = PREP_IE_TARGET_SN(hwmp_ie);
413 orig_lifetime = PREP_IE_LIFETIME(hwmp_ie);
414 orig_metric = PREP_IE_METRIC(hwmp_ie);
420 new_metric = orig_metric + last_hop_metric;
421 if (new_metric < orig_metric)
422 new_metric = MAX_METRIC;
423 exp_time = TU_TO_EXP_TIME(orig_lifetime);
425 if (ether_addr_equal(orig_addr, sdata->vif.addr)) {
426 /* This MP is the originator, we are not interested in this
427 * frame, except for updating transmitter's path info.
432 mpath = mesh_path_lookup(orig_addr, sdata);
434 spin_lock_bh(&mpath->state_lock);
435 if (mpath->flags & MESH_PATH_FIXED)
437 else if ((mpath->flags & MESH_PATH_ACTIVE) &&
438 (mpath->flags & MESH_PATH_SN_VALID)) {
439 if (SN_GT(mpath->sn, orig_sn) ||
440 (mpath->sn == orig_sn &&
441 new_metric >= mpath->metric)) {
447 mesh_path_add(orig_addr, sdata);
448 mpath = mesh_path_lookup(orig_addr, sdata);
453 spin_lock_bh(&mpath->state_lock);
457 mesh_path_assign_nexthop(mpath, sta);
458 mpath->flags |= MESH_PATH_SN_VALID;
459 mpath->metric = new_metric;
461 mpath->exp_time = time_after(mpath->exp_time, exp_time)
462 ? mpath->exp_time : exp_time;
463 mesh_path_activate(mpath);
464 spin_unlock_bh(&mpath->state_lock);
465 mesh_path_tx_pending(mpath);
466 /* draft says preq_id should be saved to, but there does
467 * not seem to be any use for it, skipping by now
470 spin_unlock_bh(&mpath->state_lock);
473 /* Update and check transmitter routing info */
475 if (ether_addr_equal(orig_addr, ta))
480 mpath = mesh_path_lookup(ta, sdata);
482 spin_lock_bh(&mpath->state_lock);
483 if ((mpath->flags & MESH_PATH_FIXED) ||
484 ((mpath->flags & MESH_PATH_ACTIVE) &&
485 (last_hop_metric > mpath->metric)))
488 mesh_path_add(ta, sdata);
489 mpath = mesh_path_lookup(ta, sdata);
494 spin_lock_bh(&mpath->state_lock);
498 mesh_path_assign_nexthop(mpath, sta);
499 mpath->metric = last_hop_metric;
500 mpath->exp_time = time_after(mpath->exp_time, exp_time)
501 ? mpath->exp_time : exp_time;
502 mesh_path_activate(mpath);
503 spin_unlock_bh(&mpath->state_lock);
504 mesh_path_tx_pending(mpath);
506 spin_unlock_bh(&mpath->state_lock);
511 return process ? new_metric : 0;
514 static void hwmp_preq_frame_process(struct ieee80211_sub_if_data *sdata,
515 struct ieee80211_mgmt *mgmt,
516 u8 *preq_elem, u32 metric)
518 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
519 struct mesh_path *mpath = NULL;
520 u8 *target_addr, *orig_addr;
522 u8 target_flags, ttl;
523 u32 orig_sn, target_sn, lifetime;
527 /* Update target SN, if present */
528 target_addr = PREQ_IE_TARGET_ADDR(preq_elem);
529 orig_addr = PREQ_IE_ORIG_ADDR(preq_elem);
530 target_sn = PREQ_IE_TARGET_SN(preq_elem);
531 orig_sn = PREQ_IE_ORIG_SN(preq_elem);
532 target_flags = PREQ_IE_TARGET_F(preq_elem);
534 mhwmp_dbg("received PREQ from %pM", orig_addr);
536 if (ether_addr_equal(target_addr, sdata->vif.addr)) {
537 mhwmp_dbg("PREQ is for us");
541 if (time_after(jiffies, ifmsh->last_sn_update +
542 net_traversal_jiffies(sdata)) ||
543 time_before(jiffies, ifmsh->last_sn_update)) {
544 target_sn = ++ifmsh->sn;
545 ifmsh->last_sn_update = jiffies;
549 mpath = mesh_path_lookup(target_addr, sdata);
551 if ((!(mpath->flags & MESH_PATH_SN_VALID)) ||
552 SN_LT(mpath->sn, target_sn)) {
553 mpath->sn = target_sn;
554 mpath->flags |= MESH_PATH_SN_VALID;
555 } else if ((!(target_flags & MP_F_DO)) &&
556 (mpath->flags & MESH_PATH_ACTIVE)) {
558 metric = mpath->metric;
559 target_sn = mpath->sn;
560 if (target_flags & MP_F_RF)
561 target_flags |= MP_F_DO;
570 lifetime = PREQ_IE_LIFETIME(preq_elem);
571 ttl = ifmsh->mshcfg.element_ttl;
573 mhwmp_dbg("replying to the PREQ");
574 mesh_path_sel_frame_tx(MPATH_PREP, 0, orig_addr,
575 cpu_to_le32(orig_sn), 0, target_addr,
576 cpu_to_le32(target_sn), mgmt->sa, 0, ttl,
577 cpu_to_le32(lifetime), cpu_to_le32(metric),
580 ifmsh->mshstats.dropped_frames_ttl++;
583 if (forward && ifmsh->mshcfg.dot11MeshForwarding) {
587 ttl = PREQ_IE_TTL(preq_elem);
588 lifetime = PREQ_IE_LIFETIME(preq_elem);
590 ifmsh->mshstats.dropped_frames_ttl++;
593 mhwmp_dbg("forwarding the PREQ from %pM", orig_addr);
595 flags = PREQ_IE_FLAGS(preq_elem);
596 preq_id = PREQ_IE_PREQ_ID(preq_elem);
597 hopcount = PREQ_IE_HOPCOUNT(preq_elem) + 1;
598 da = (mpath && mpath->is_root) ?
599 mpath->rann_snd_addr : broadcast_addr;
600 mesh_path_sel_frame_tx(MPATH_PREQ, flags, orig_addr,
601 cpu_to_le32(orig_sn), target_flags, target_addr,
602 cpu_to_le32(target_sn), da,
603 hopcount, ttl, cpu_to_le32(lifetime),
604 cpu_to_le32(metric), cpu_to_le32(preq_id),
606 if (!is_multicast_ether_addr(da))
607 ifmsh->mshstats.fwded_unicast++;
609 ifmsh->mshstats.fwded_mcast++;
610 ifmsh->mshstats.fwded_frames++;
615 static inline struct sta_info *
616 next_hop_deref_protected(struct mesh_path *mpath)
618 return rcu_dereference_protected(mpath->next_hop,
619 lockdep_is_held(&mpath->state_lock));
623 static void hwmp_prep_frame_process(struct ieee80211_sub_if_data *sdata,
624 struct ieee80211_mgmt *mgmt,
625 u8 *prep_elem, u32 metric)
627 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
628 struct mesh_path *mpath;
629 u8 *target_addr, *orig_addr;
630 u8 ttl, hopcount, flags;
631 u8 next_hop[ETH_ALEN];
632 u32 target_sn, orig_sn, lifetime;
634 mhwmp_dbg("received PREP from %pM", PREP_IE_ORIG_ADDR(prep_elem));
636 orig_addr = PREP_IE_ORIG_ADDR(prep_elem);
637 if (ether_addr_equal(orig_addr, sdata->vif.addr))
638 /* destination, no forwarding required */
641 if (!ifmsh->mshcfg.dot11MeshForwarding)
644 ttl = PREP_IE_TTL(prep_elem);
646 sdata->u.mesh.mshstats.dropped_frames_ttl++;
651 mpath = mesh_path_lookup(orig_addr, sdata);
653 spin_lock_bh(&mpath->state_lock);
656 if (!(mpath->flags & MESH_PATH_ACTIVE)) {
657 spin_unlock_bh(&mpath->state_lock);
660 memcpy(next_hop, next_hop_deref_protected(mpath)->sta.addr, ETH_ALEN);
661 spin_unlock_bh(&mpath->state_lock);
663 flags = PREP_IE_FLAGS(prep_elem);
664 lifetime = PREP_IE_LIFETIME(prep_elem);
665 hopcount = PREP_IE_HOPCOUNT(prep_elem) + 1;
666 target_addr = PREP_IE_TARGET_ADDR(prep_elem);
667 target_sn = PREP_IE_TARGET_SN(prep_elem);
668 orig_sn = PREP_IE_ORIG_SN(prep_elem);
670 mesh_path_sel_frame_tx(MPATH_PREP, flags, orig_addr,
671 cpu_to_le32(orig_sn), 0, target_addr,
672 cpu_to_le32(target_sn), next_hop, hopcount,
673 ttl, cpu_to_le32(lifetime), cpu_to_le32(metric),
677 sdata->u.mesh.mshstats.fwded_unicast++;
678 sdata->u.mesh.mshstats.fwded_frames++;
683 sdata->u.mesh.mshstats.dropped_frames_no_route++;
686 static void hwmp_perr_frame_process(struct ieee80211_sub_if_data *sdata,
687 struct ieee80211_mgmt *mgmt, u8 *perr_elem)
689 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
690 struct mesh_path *mpath;
692 u8 *ta, *target_addr;
697 ttl = PERR_IE_TTL(perr_elem);
699 ifmsh->mshstats.dropped_frames_ttl++;
703 target_addr = PERR_IE_TARGET_ADDR(perr_elem);
704 target_sn = PERR_IE_TARGET_SN(perr_elem);
705 target_rcode = PERR_IE_TARGET_RCODE(perr_elem);
708 mpath = mesh_path_lookup(target_addr, sdata);
710 struct sta_info *sta;
712 spin_lock_bh(&mpath->state_lock);
713 sta = next_hop_deref_protected(mpath);
714 if (mpath->flags & MESH_PATH_ACTIVE &&
715 ether_addr_equal(ta, sta->sta.addr) &&
716 (!(mpath->flags & MESH_PATH_SN_VALID) ||
717 SN_GT(target_sn, mpath->sn))) {
718 mpath->flags &= ~MESH_PATH_ACTIVE;
719 mpath->sn = target_sn;
720 spin_unlock_bh(&mpath->state_lock);
721 if (!ifmsh->mshcfg.dot11MeshForwarding)
723 mesh_path_error_tx(ttl, target_addr, cpu_to_le32(target_sn),
724 cpu_to_le16(target_rcode),
725 broadcast_addr, sdata);
727 spin_unlock_bh(&mpath->state_lock);
733 static void hwmp_rann_frame_process(struct ieee80211_sub_if_data *sdata,
734 struct ieee80211_mgmt *mgmt,
735 struct ieee80211_rann_ie *rann)
737 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
738 struct ieee80211_local *local = sdata->local;
739 struct sta_info *sta;
740 struct mesh_path *mpath;
741 u8 ttl, flags, hopcount;
743 u32 orig_sn, metric, metric_txsta, interval;
746 ttl = rann->rann_ttl;
748 ifmsh->mshstats.dropped_frames_ttl++;
752 flags = rann->rann_flags;
753 root_is_gate = !!(flags & RANN_FLAG_IS_GATE);
754 orig_addr = rann->rann_addr;
755 orig_sn = le32_to_cpu(rann->rann_seq);
756 interval = le32_to_cpu(rann->rann_interval);
757 hopcount = rann->rann_hopcount;
759 metric = le32_to_cpu(rann->rann_metric);
761 /* Ignore our own RANNs */
762 if (ether_addr_equal(orig_addr, sdata->vif.addr))
765 mhwmp_dbg("received RANN from %pM via neighbour %pM (is_gate=%d)",
766 orig_addr, mgmt->sa, root_is_gate);
769 sta = sta_info_get(sdata, mgmt->sa);
775 metric_txsta = airtime_link_metric_get(local, sta);
777 mpath = mesh_path_lookup(orig_addr, sdata);
779 mesh_path_add(orig_addr, sdata);
780 mpath = mesh_path_lookup(orig_addr, sdata);
783 sdata->u.mesh.mshstats.dropped_frames_no_route++;
788 if ((!(mpath->flags & (MESH_PATH_ACTIVE | MESH_PATH_RESOLVING)) ||
789 time_after(jiffies, mpath->exp_time - 1*HZ)) &&
790 !(mpath->flags & MESH_PATH_FIXED)) {
791 mhwmp_dbg("%s time to refresh root mpath %pM", sdata->name,
793 mesh_queue_preq(mpath, PREQ_Q_F_START | PREQ_Q_F_REFRESH);
796 if ((SN_LT(mpath->sn, orig_sn) || (mpath->sn == orig_sn &&
797 metric < mpath->rann_metric)) && ifmsh->mshcfg.dot11MeshForwarding) {
798 mesh_path_sel_frame_tx(MPATH_RANN, flags, orig_addr,
799 cpu_to_le32(orig_sn),
800 0, NULL, 0, broadcast_addr,
801 hopcount, ttl, cpu_to_le32(interval),
802 cpu_to_le32(metric + metric_txsta),
805 mpath->rann_metric = metric + metric_txsta;
806 /* Recording RANNs sender address to send individually
807 * addressed PREQs destined for root mesh STA */
808 memcpy(mpath->rann_snd_addr, mgmt->sa, ETH_ALEN);
811 mpath->is_root = true;
814 mesh_path_add_gate(mpath);
820 void mesh_rx_path_sel_frame(struct ieee80211_sub_if_data *sdata,
821 struct ieee80211_mgmt *mgmt,
824 struct ieee802_11_elems elems;
827 struct sta_info *sta;
829 /* need action_code */
830 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
834 sta = sta_info_get(sdata, mgmt->sa);
835 if (!sta || sta->plink_state != NL80211_PLINK_ESTAB) {
841 baselen = (u8 *) mgmt->u.action.u.mesh_action.variable - (u8 *) mgmt;
842 ieee802_11_parse_elems(mgmt->u.action.u.mesh_action.variable,
843 len - baselen, &elems);
846 if (elems.preq_len != 37)
847 /* Right now we support just 1 destination and no AE */
849 last_hop_metric = hwmp_route_info_get(sdata, mgmt, elems.preq,
852 hwmp_preq_frame_process(sdata, mgmt, elems.preq,
856 if (elems.prep_len != 31)
857 /* Right now we support no AE */
859 last_hop_metric = hwmp_route_info_get(sdata, mgmt, elems.prep,
862 hwmp_prep_frame_process(sdata, mgmt, elems.prep,
866 if (elems.perr_len != 15)
867 /* Right now we support only one destination per PERR */
869 hwmp_perr_frame_process(sdata, mgmt, elems.perr);
872 hwmp_rann_frame_process(sdata, mgmt, elems.rann);
876 * mesh_queue_preq - queue a PREQ to a given destination
878 * @mpath: mesh path to discover
879 * @flags: special attributes of the PREQ to be sent
881 * Locking: the function must be called from within a rcu read lock block.
884 static void mesh_queue_preq(struct mesh_path *mpath, u8 flags)
886 struct ieee80211_sub_if_data *sdata = mpath->sdata;
887 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
888 struct mesh_preq_queue *preq_node;
890 preq_node = kmalloc(sizeof(struct mesh_preq_queue), GFP_ATOMIC);
892 mhwmp_dbg("could not allocate PREQ node");
896 spin_lock_bh(&ifmsh->mesh_preq_queue_lock);
897 if (ifmsh->preq_queue_len == MAX_PREQ_QUEUE_LEN) {
898 spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
900 if (printk_ratelimit())
901 mhwmp_dbg("PREQ node queue full");
905 spin_lock(&mpath->state_lock);
906 if (mpath->flags & MESH_PATH_REQ_QUEUED) {
907 spin_unlock(&mpath->state_lock);
908 spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
913 memcpy(preq_node->dst, mpath->dst, ETH_ALEN);
914 preq_node->flags = flags;
916 mpath->flags |= MESH_PATH_REQ_QUEUED;
917 spin_unlock(&mpath->state_lock);
919 list_add_tail(&preq_node->list, &ifmsh->preq_queue.list);
920 ++ifmsh->preq_queue_len;
921 spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
923 if (time_after(jiffies, ifmsh->last_preq + min_preq_int_jiff(sdata)))
924 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
926 else if (time_before(jiffies, ifmsh->last_preq)) {
927 /* avoid long wait if did not send preqs for a long time
928 * and jiffies wrapped around
930 ifmsh->last_preq = jiffies - min_preq_int_jiff(sdata) - 1;
931 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
933 mod_timer(&ifmsh->mesh_path_timer, ifmsh->last_preq +
934 min_preq_int_jiff(sdata));
938 * mesh_path_start_discovery - launch a path discovery from the PREQ queue
940 * @sdata: local mesh subif
942 void mesh_path_start_discovery(struct ieee80211_sub_if_data *sdata)
944 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
945 struct mesh_preq_queue *preq_node;
946 struct mesh_path *mpath;
947 u8 ttl, target_flags;
951 spin_lock_bh(&ifmsh->mesh_preq_queue_lock);
952 if (!ifmsh->preq_queue_len ||
953 time_before(jiffies, ifmsh->last_preq +
954 min_preq_int_jiff(sdata))) {
955 spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
959 preq_node = list_first_entry(&ifmsh->preq_queue.list,
960 struct mesh_preq_queue, list);
961 list_del(&preq_node->list);
962 --ifmsh->preq_queue_len;
963 spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
966 mpath = mesh_path_lookup(preq_node->dst, sdata);
970 spin_lock_bh(&mpath->state_lock);
971 mpath->flags &= ~MESH_PATH_REQ_QUEUED;
972 if (preq_node->flags & PREQ_Q_F_START) {
973 if (mpath->flags & MESH_PATH_RESOLVING) {
974 spin_unlock_bh(&mpath->state_lock);
977 mpath->flags &= ~MESH_PATH_RESOLVED;
978 mpath->flags |= MESH_PATH_RESOLVING;
979 mpath->discovery_retries = 0;
980 mpath->discovery_timeout = disc_timeout_jiff(sdata);
982 } else if (!(mpath->flags & MESH_PATH_RESOLVING) ||
983 mpath->flags & MESH_PATH_RESOLVED) {
984 mpath->flags &= ~MESH_PATH_RESOLVING;
985 spin_unlock_bh(&mpath->state_lock);
989 ifmsh->last_preq = jiffies;
991 if (time_after(jiffies, ifmsh->last_sn_update +
992 net_traversal_jiffies(sdata)) ||
993 time_before(jiffies, ifmsh->last_sn_update)) {
995 sdata->u.mesh.last_sn_update = jiffies;
997 lifetime = default_lifetime(sdata);
998 ttl = sdata->u.mesh.mshcfg.element_ttl;
1000 sdata->u.mesh.mshstats.dropped_frames_ttl++;
1001 spin_unlock_bh(&mpath->state_lock);
1005 if (preq_node->flags & PREQ_Q_F_REFRESH)
1006 target_flags = MP_F_DO;
1008 target_flags = MP_F_RF;
1010 spin_unlock_bh(&mpath->state_lock);
1011 da = (mpath->is_root) ? mpath->rann_snd_addr : broadcast_addr;
1012 mesh_path_sel_frame_tx(MPATH_PREQ, 0, sdata->vif.addr,
1013 cpu_to_le32(ifmsh->sn), target_flags, mpath->dst,
1014 cpu_to_le32(mpath->sn), da, 0,
1015 ttl, cpu_to_le32(lifetime), 0,
1016 cpu_to_le32(ifmsh->preq_id++), sdata);
1017 mod_timer(&mpath->timer, jiffies + mpath->discovery_timeout);
1024 /* mesh_nexthop_resolve - lookup next hop for given skb and start path
1025 * discovery if no forwarding information is found.
1027 * @skb: 802.11 frame to be sent
1028 * @sdata: network subif the frame will be sent through
1030 * Returns: 0 if the next hop was found and -ENOENT if the frame was queued.
1031 * skb is freeed here if no mpath could be allocated.
1033 int mesh_nexthop_resolve(struct sk_buff *skb,
1034 struct ieee80211_sub_if_data *sdata)
1036 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1037 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1038 struct mesh_path *mpath;
1039 struct sk_buff *skb_to_free = NULL;
1040 u8 *target_addr = hdr->addr3;
1044 err = mesh_nexthop_lookup(skb, sdata);
1048 /* no nexthop found, start resolving */
1049 mpath = mesh_path_lookup(target_addr, sdata);
1051 mesh_path_add(target_addr, sdata);
1052 mpath = mesh_path_lookup(target_addr, sdata);
1054 mesh_path_discard_frame(skb, sdata);
1060 if (!(mpath->flags & MESH_PATH_RESOLVING))
1061 mesh_queue_preq(mpath, PREQ_Q_F_START);
1063 if (skb_queue_len(&mpath->frame_queue) >= MESH_FRAME_QUEUE_LEN)
1064 skb_to_free = skb_dequeue(&mpath->frame_queue);
1066 info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
1067 ieee80211_set_qos_hdr(sdata, skb);
1068 skb_queue_tail(&mpath->frame_queue, skb);
1071 mesh_path_discard_frame(skb_to_free, sdata);
1078 * mesh_nexthop_lookup - put the appropriate next hop on a mesh frame. Calling
1079 * this function is considered "using" the associated mpath, so preempt a path
1080 * refresh if this mpath expires soon.
1082 * @skb: 802.11 frame to be sent
1083 * @sdata: network subif the frame will be sent through
1085 * Returns: 0 if the next hop was found. Nonzero otherwise.
1087 int mesh_nexthop_lookup(struct sk_buff *skb,
1088 struct ieee80211_sub_if_data *sdata)
1090 struct mesh_path *mpath;
1091 struct sta_info *next_hop;
1092 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1093 u8 *target_addr = hdr->addr3;
1097 mpath = mesh_path_lookup(target_addr, sdata);
1099 if (!mpath || !(mpath->flags & MESH_PATH_ACTIVE))
1102 if (time_after(jiffies,
1104 msecs_to_jiffies(sdata->u.mesh.mshcfg.path_refresh_time)) &&
1105 ether_addr_equal(sdata->vif.addr, hdr->addr4) &&
1106 !(mpath->flags & MESH_PATH_RESOLVING) &&
1107 !(mpath->flags & MESH_PATH_FIXED))
1108 mesh_queue_preq(mpath, PREQ_Q_F_START | PREQ_Q_F_REFRESH);
1110 next_hop = rcu_dereference(mpath->next_hop);
1112 memcpy(hdr->addr1, next_hop->sta.addr, ETH_ALEN);
1113 memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN);
1122 void mesh_path_timer(unsigned long data)
1124 struct mesh_path *mpath = (void *) data;
1125 struct ieee80211_sub_if_data *sdata = mpath->sdata;
1128 if (sdata->local->quiescing)
1131 spin_lock_bh(&mpath->state_lock);
1132 if (mpath->flags & MESH_PATH_RESOLVED ||
1133 (!(mpath->flags & MESH_PATH_RESOLVING))) {
1134 mpath->flags &= ~(MESH_PATH_RESOLVING | MESH_PATH_RESOLVED);
1135 spin_unlock_bh(&mpath->state_lock);
1136 } else if (mpath->discovery_retries < max_preq_retries(sdata)) {
1137 ++mpath->discovery_retries;
1138 mpath->discovery_timeout *= 2;
1139 mpath->flags &= ~MESH_PATH_REQ_QUEUED;
1140 spin_unlock_bh(&mpath->state_lock);
1141 mesh_queue_preq(mpath, 0);
1144 mpath->exp_time = jiffies;
1145 spin_unlock_bh(&mpath->state_lock);
1146 if (!mpath->is_gate && mesh_gate_num(sdata) > 0) {
1147 ret = mesh_path_send_to_gates(mpath);
1149 mhwmp_dbg("no gate was reachable");
1151 mesh_path_flush_pending(mpath);
1156 mesh_path_tx_root_frame(struct ieee80211_sub_if_data *sdata)
1158 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
1159 u32 interval = ifmsh->mshcfg.dot11MeshHWMPRannInterval;
1162 flags = (ifmsh->mshcfg.dot11MeshGateAnnouncementProtocol)
1163 ? RANN_FLAG_IS_GATE : 0;
1164 mesh_path_sel_frame_tx(MPATH_RANN, flags, sdata->vif.addr,
1165 cpu_to_le32(++ifmsh->sn),
1166 0, NULL, 0, broadcast_addr,
1167 0, sdata->u.mesh.mshcfg.element_ttl,
1168 cpu_to_le32(interval), 0, 0, sdata);