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.
12 #ifdef CONFIG_MAC80211_VERBOSE_MHWMP_DEBUG
13 #define mhwmp_dbg(fmt, args...) printk(KERN_DEBUG "Mesh HWMP: " fmt, ##args)
15 #define mhwmp_dbg(fmt, args...) do { (void)(0); } while (0)
18 #define TEST_FRAME_LEN 8192
19 #define MAX_METRIC 0xffffffff
22 /* Number of frames buffered per destination for unresolved destinations */
23 #define MESH_FRAME_QUEUE_LEN 10
24 #define MAX_PREQ_QUEUE_LEN 64
26 /* Destination only */
28 /* Reply and forward */
30 /* Unknown Sequence Number */
32 /* Reason code Present */
33 #define MP_F_RCODE 0x02
35 static void mesh_queue_preq(struct mesh_path *, u8);
37 static inline u32 u32_field_get(u8 *preq_elem, int offset, bool ae)
41 return get_unaligned_le32(preq_elem + offset);
44 static inline u32 u16_field_get(u8 *preq_elem, int offset, bool ae)
48 return get_unaligned_le16(preq_elem + offset);
51 /* HWMP IE processing macros */
53 #define AE_F_SET(x) (*x & AE_F)
54 #define PREQ_IE_FLAGS(x) (*(x))
55 #define PREQ_IE_HOPCOUNT(x) (*(x + 1))
56 #define PREQ_IE_TTL(x) (*(x + 2))
57 #define PREQ_IE_PREQ_ID(x) u32_field_get(x, 3, 0)
58 #define PREQ_IE_ORIG_ADDR(x) (x + 7)
59 #define PREQ_IE_ORIG_SN(x) u32_field_get(x, 13, 0);
60 #define PREQ_IE_LIFETIME(x) u32_field_get(x, 17, AE_F_SET(x));
61 #define PREQ_IE_METRIC(x) u32_field_get(x, 21, AE_F_SET(x));
62 #define PREQ_IE_TARGET_F(x) (*(AE_F_SET(x) ? x + 32 : x + 26))
63 #define PREQ_IE_TARGET_ADDR(x) (AE_F_SET(x) ? x + 33 : x + 27)
64 #define PREQ_IE_TARGET_SN(x) u32_field_get(x, 33, AE_F_SET(x));
67 #define PREP_IE_FLAGS(x) PREQ_IE_FLAGS(x)
68 #define PREP_IE_HOPCOUNT(x) PREQ_IE_HOPCOUNT(x)
69 #define PREP_IE_TTL(x) PREQ_IE_TTL(x)
70 #define PREP_IE_ORIG_ADDR(x) (x + 3)
71 #define PREP_IE_ORIG_SN(x) u32_field_get(x, 9, 0);
72 #define PREP_IE_LIFETIME(x) u32_field_get(x, 13, AE_F_SET(x));
73 #define PREP_IE_METRIC(x) u32_field_get(x, 17, AE_F_SET(x));
74 #define PREP_IE_TARGET_ADDR(x) (AE_F_SET(x) ? x + 27 : x + 21)
75 #define PREP_IE_TARGET_SN(x) u32_field_get(x, 27, AE_F_SET(x));
77 #define PERR_IE_TTL(x) (*(x))
78 #define PERR_IE_TARGET_FLAGS(x) (*(x + 2))
79 #define PERR_IE_TARGET_ADDR(x) (x + 3)
80 #define PERR_IE_TARGET_SN(x) u32_field_get(x, 9, 0);
81 #define PERR_IE_TARGET_RCODE(x) u16_field_get(x, 13, 0);
83 #define MSEC_TO_TU(x) (x*1000/1024)
84 #define SN_GT(x, y) ((long) (y) - (long) (x) < 0)
85 #define SN_LT(x, y) ((long) (x) - (long) (y) < 0)
87 #define net_traversal_jiffies(s) \
88 msecs_to_jiffies(s->u.mesh.mshcfg.dot11MeshHWMPnetDiameterTraversalTime)
89 #define default_lifetime(s) \
90 MSEC_TO_TU(s->u.mesh.mshcfg.dot11MeshHWMPactivePathTimeout)
91 #define min_preq_int_jiff(s) \
92 (msecs_to_jiffies(s->u.mesh.mshcfg.dot11MeshHWMPpreqMinInterval))
93 #define max_preq_retries(s) (s->u.mesh.mshcfg.dot11MeshHWMPmaxPREQretries)
94 #define disc_timeout_jiff(s) \
95 msecs_to_jiffies(sdata->u.mesh.mshcfg.min_discovery_timeout)
97 enum mpath_frame_type {
104 static int mesh_path_sel_frame_tx(enum mpath_frame_type action, u8 flags,
105 u8 *orig_addr, __le32 orig_sn, u8 target_flags, u8 *target,
106 __le32 target_sn, u8 *da, u8 hop_count, u8 ttl,__le32 lifetime,
107 __le32 metric, __le32 preq_id,
108 struct ieee80211_sub_if_data *sdata)
110 struct ieee80211_local *local = sdata->local;
111 struct sk_buff *skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400);
112 struct ieee80211_mgmt *mgmt;
118 skb_reserve(skb, local->hw.extra_tx_headroom);
119 /* 25 is the size of the common mgmt part (24) plus the size of the
120 * common action part (1)
122 mgmt = (struct ieee80211_mgmt *)
123 skb_put(skb, 25 + sizeof(mgmt->u.action.u.mesh_action));
124 memset(mgmt, 0, 25 + sizeof(mgmt->u.action.u.mesh_action));
125 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
126 IEEE80211_STYPE_ACTION);
128 memcpy(mgmt->da, da, ETH_ALEN);
129 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
131 memcpy(mgmt->bssid, sdata->dev->dev_addr, ETH_ALEN);
132 mgmt->u.action.category = MESH_PATH_SEL_CATEGORY;
133 mgmt->u.action.u.mesh_action.action_code = MESH_PATH_SEL_ACTION;
137 mhwmp_dbg("sending PREQ to %pM\n", target);
139 pos = skb_put(skb, 2 + ie_len);
140 *pos++ = WLAN_EID_PREQ;
143 mhwmp_dbg("sending PREP to %pM\n", target);
145 pos = skb_put(skb, 2 + ie_len);
146 *pos++ = WLAN_EID_PREP;
149 mhwmp_dbg("sending RANN from %pM\n", orig_addr);
150 ie_len = sizeof(struct ieee80211_rann_ie);
151 pos = skb_put(skb, 2 + ie_len);
152 *pos++ = WLAN_EID_RANN;
163 if (action == MPATH_PREQ) {
164 memcpy(pos, &preq_id, 4);
167 memcpy(pos, orig_addr, ETH_ALEN);
169 memcpy(pos, &orig_sn, 4);
171 if (action != MPATH_RANN) {
172 memcpy(pos, &lifetime, 4);
175 memcpy(pos, &metric, 4);
177 if (action == MPATH_PREQ) {
178 /* destination count */
180 *pos++ = target_flags;
182 if (action != MPATH_RANN) {
183 memcpy(pos, target, ETH_ALEN);
185 memcpy(pos, &target_sn, 4);
188 ieee80211_tx_skb(sdata, skb, 1);
193 * mesh_send_path error - Sends a PERR mesh management frame
195 * @target: broken destination
196 * @target_sn: SN of the broken destination
197 * @target_rcode: reason code for this PERR
198 * @ra: node this frame is addressed to
200 int mesh_path_error_tx(u8 ttl, u8 *target, __le32 target_sn,
201 __le16 target_rcode, u8 *ra,
202 struct ieee80211_sub_if_data *sdata)
204 struct ieee80211_local *local = sdata->local;
205 struct sk_buff *skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400);
206 struct ieee80211_mgmt *mgmt;
212 skb_reserve(skb, local->hw.extra_tx_headroom);
213 /* 25 is the size of the common mgmt part (24) plus the size of the
214 * common action part (1)
216 mgmt = (struct ieee80211_mgmt *)
217 skb_put(skb, 25 + sizeof(mgmt->u.action.u.mesh_action));
218 memset(mgmt, 0, 25 + sizeof(mgmt->u.action.u.mesh_action));
219 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
220 IEEE80211_STYPE_ACTION);
222 memcpy(mgmt->da, ra, ETH_ALEN);
223 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
224 /* BSSID is left zeroed, wildcard value */
225 mgmt->u.action.category = MESH_PATH_SEL_CATEGORY;
226 mgmt->u.action.u.mesh_action.action_code = MESH_PATH_SEL_ACTION;
228 pos = skb_put(skb, 2 + ie_len);
229 *pos++ = WLAN_EID_PERR;
233 /* number of destinations */
236 * flags bit, bit 1 is unset if we know the sequence number and
237 * bit 2 is set if we have a reason code
245 memcpy(pos, target, ETH_ALEN);
247 memcpy(pos, &target_sn, 4);
249 memcpy(pos, &target_rcode, 2);
251 ieee80211_tx_skb(sdata, skb, 1);
255 void ieee80211s_update_metric(struct ieee80211_local *local,
256 struct sta_info *stainfo, struct sk_buff *skb)
258 struct ieee80211_tx_info *txinfo = IEEE80211_SKB_CB(skb);
259 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
262 if (!ieee80211_is_data(hdr->frame_control))
265 failed = !(txinfo->flags & IEEE80211_TX_STAT_ACK);
267 /* moving average, scaled to 100 */
268 stainfo->fail_avg = ((80 * stainfo->fail_avg + 5) / 100 + 20 * failed);
269 if (stainfo->fail_avg > 95)
270 mesh_plink_broken(stainfo);
273 static u32 airtime_link_metric_get(struct ieee80211_local *local,
274 struct sta_info *sta)
276 struct ieee80211_supported_band *sband;
277 /* This should be adjusted for each device */
278 int device_constant = 1 << ARITH_SHIFT;
279 int test_frame_len = TEST_FRAME_LEN << ARITH_SHIFT;
280 int s_unit = 1 << ARITH_SHIFT;
282 u32 tx_time, estimated_retx;
285 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
287 if (sta->fail_avg >= 100)
290 if (sta->last_tx_rate.flags & IEEE80211_TX_RC_MCS)
293 err = (sta->fail_avg << ARITH_SHIFT) / 100;
295 /* bitrate is in units of 100 Kbps, while we need rate in units of
296 * 1Mbps. This will be corrected on tx_time computation.
298 rate = sband->bitrates[sta->last_tx_rate.idx].bitrate;
299 tx_time = (device_constant + 10 * test_frame_len / rate);
300 estimated_retx = ((1 << (2 * ARITH_SHIFT)) / (s_unit - err));
301 result = (tx_time * estimated_retx) >> (2 * ARITH_SHIFT) ;
306 * hwmp_route_info_get - Update routing info to originator and transmitter
308 * @sdata: local mesh subif
309 * @mgmt: mesh management frame
310 * @hwmp_ie: hwmp information element (PREP or PREQ)
312 * This function updates the path routing information to the originator and the
313 * transmitter of a HWMP PREQ or PREP frame.
315 * Returns: metric to frame originator or 0 if the frame should not be further
318 * Notes: this function is the only place (besides user-provided info) where
319 * path routing information is updated.
321 static u32 hwmp_route_info_get(struct ieee80211_sub_if_data *sdata,
322 struct ieee80211_mgmt *mgmt,
323 u8 *hwmp_ie, enum mpath_frame_type action)
325 struct ieee80211_local *local = sdata->local;
326 struct mesh_path *mpath;
327 struct sta_info *sta;
330 u32 orig_sn, orig_metric;
331 unsigned long orig_lifetime, exp_time;
332 u32 last_hop_metric, new_metric;
336 sta = sta_info_get(local, mgmt->sa);
342 last_hop_metric = airtime_link_metric_get(local, sta);
343 /* Update and check originator routing info */
348 orig_addr = PREQ_IE_ORIG_ADDR(hwmp_ie);
349 orig_sn = PREQ_IE_ORIG_SN(hwmp_ie);
350 orig_lifetime = PREQ_IE_LIFETIME(hwmp_ie);
351 orig_metric = PREQ_IE_METRIC(hwmp_ie);
354 /* Originator here refers to the MP that was the destination in
355 * the Path Request. The draft refers to that MP as the
356 * destination address, even though usually it is the origin of
357 * the PREP frame. We divert from the nomenclature in the draft
358 * so that we can easily use a single function to gather path
359 * information from both PREQ and PREP frames.
361 orig_addr = PREP_IE_ORIG_ADDR(hwmp_ie);
362 orig_sn = PREP_IE_ORIG_SN(hwmp_ie);
363 orig_lifetime = PREP_IE_LIFETIME(hwmp_ie);
364 orig_metric = PREP_IE_METRIC(hwmp_ie);
370 new_metric = orig_metric + last_hop_metric;
371 if (new_metric < orig_metric)
372 new_metric = MAX_METRIC;
373 exp_time = TU_TO_EXP_TIME(orig_lifetime);
375 if (memcmp(orig_addr, sdata->dev->dev_addr, ETH_ALEN) == 0) {
376 /* This MP is the originator, we are not interested in this
377 * frame, except for updating transmitter's path info.
382 mpath = mesh_path_lookup(orig_addr, sdata);
384 spin_lock_bh(&mpath->state_lock);
385 if (mpath->flags & MESH_PATH_FIXED)
387 else if ((mpath->flags & MESH_PATH_ACTIVE) &&
388 (mpath->flags & MESH_PATH_SN_VALID)) {
389 if (SN_GT(mpath->sn, orig_sn) ||
390 (mpath->sn == orig_sn &&
391 action == MPATH_PREQ &&
392 new_metric > mpath->metric)) {
398 mesh_path_add(orig_addr, sdata);
399 mpath = mesh_path_lookup(orig_addr, sdata);
404 spin_lock_bh(&mpath->state_lock);
408 mesh_path_assign_nexthop(mpath, sta);
409 mpath->flags |= MESH_PATH_SN_VALID;
410 mpath->metric = new_metric;
412 mpath->exp_time = time_after(mpath->exp_time, exp_time)
413 ? mpath->exp_time : exp_time;
414 mesh_path_activate(mpath);
415 spin_unlock_bh(&mpath->state_lock);
416 mesh_path_tx_pending(mpath);
417 /* draft says preq_id should be saved to, but there does
418 * not seem to be any use for it, skipping by now
421 spin_unlock_bh(&mpath->state_lock);
424 /* Update and check transmitter routing info */
426 if (memcmp(orig_addr, ta, ETH_ALEN) == 0)
431 mpath = mesh_path_lookup(ta, sdata);
433 spin_lock_bh(&mpath->state_lock);
434 if ((mpath->flags & MESH_PATH_FIXED) ||
435 ((mpath->flags & MESH_PATH_ACTIVE) &&
436 (last_hop_metric > mpath->metric)))
439 mesh_path_add(ta, sdata);
440 mpath = mesh_path_lookup(ta, 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 = last_hop_metric;
452 mpath->exp_time = time_after(mpath->exp_time, exp_time)
453 ? mpath->exp_time : exp_time;
454 mesh_path_activate(mpath);
455 spin_unlock_bh(&mpath->state_lock);
456 mesh_path_tx_pending(mpath);
458 spin_unlock_bh(&mpath->state_lock);
463 return process ? new_metric : 0;
466 static void hwmp_preq_frame_process(struct ieee80211_sub_if_data *sdata,
467 struct ieee80211_mgmt *mgmt,
468 u8 *preq_elem, u32 metric)
470 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
471 struct mesh_path *mpath;
472 u8 *target_addr, *orig_addr;
473 u8 target_flags, ttl;
474 u32 orig_sn, target_sn, lifetime;
478 /* Update target SN, if present */
479 target_addr = PREQ_IE_TARGET_ADDR(preq_elem);
480 orig_addr = PREQ_IE_ORIG_ADDR(preq_elem);
481 target_sn = PREQ_IE_TARGET_SN(preq_elem);
482 orig_sn = PREQ_IE_ORIG_SN(preq_elem);
483 target_flags = PREQ_IE_TARGET_F(preq_elem);
485 mhwmp_dbg("received PREQ from %pM\n", orig_addr);
487 if (memcmp(target_addr, sdata->dev->dev_addr, ETH_ALEN) == 0) {
488 mhwmp_dbg("PREQ is for us\n");
492 if (time_after(jiffies, ifmsh->last_sn_update +
493 net_traversal_jiffies(sdata)) ||
494 time_before(jiffies, ifmsh->last_sn_update)) {
495 target_sn = ++ifmsh->sn;
496 ifmsh->last_sn_update = jiffies;
500 mpath = mesh_path_lookup(target_addr, sdata);
502 if ((!(mpath->flags & MESH_PATH_SN_VALID)) ||
503 SN_LT(mpath->sn, target_sn)) {
504 mpath->sn = target_sn;
505 mpath->flags |= MESH_PATH_SN_VALID;
506 } else if ((!(target_flags & MP_F_DO)) &&
507 (mpath->flags & MESH_PATH_ACTIVE)) {
509 metric = mpath->metric;
510 target_sn = mpath->sn;
511 if (target_flags & MP_F_RF)
512 target_flags |= MP_F_DO;
521 lifetime = PREQ_IE_LIFETIME(preq_elem);
522 ttl = ifmsh->mshcfg.dot11MeshTTL;
524 mhwmp_dbg("replying to the PREQ\n");
525 mesh_path_sel_frame_tx(MPATH_PREP, 0, target_addr,
526 cpu_to_le32(target_sn), 0, orig_addr,
527 cpu_to_le32(orig_sn), mgmt->sa, 0, ttl,
528 cpu_to_le32(lifetime), cpu_to_le32(metric),
531 ifmsh->mshstats.dropped_frames_ttl++;
538 ttl = PREQ_IE_TTL(preq_elem);
539 lifetime = PREQ_IE_LIFETIME(preq_elem);
541 ifmsh->mshstats.dropped_frames_ttl++;
544 mhwmp_dbg("forwarding the PREQ from %pM\n", orig_addr);
546 flags = PREQ_IE_FLAGS(preq_elem);
547 preq_id = PREQ_IE_PREQ_ID(preq_elem);
548 hopcount = PREQ_IE_HOPCOUNT(preq_elem) + 1;
549 mesh_path_sel_frame_tx(MPATH_PREQ, flags, orig_addr,
550 cpu_to_le32(orig_sn), target_flags, target_addr,
551 cpu_to_le32(target_sn), sdata->dev->broadcast,
552 hopcount, ttl, cpu_to_le32(lifetime),
553 cpu_to_le32(metric), cpu_to_le32(preq_id),
555 ifmsh->mshstats.fwded_mcast++;
556 ifmsh->mshstats.fwded_frames++;
561 static void hwmp_prep_frame_process(struct ieee80211_sub_if_data *sdata,
562 struct ieee80211_mgmt *mgmt,
563 u8 *prep_elem, u32 metric)
565 struct mesh_path *mpath;
566 u8 *target_addr, *orig_addr;
567 u8 ttl, hopcount, flags;
568 u8 next_hop[ETH_ALEN];
569 u32 target_sn, orig_sn, lifetime;
571 mhwmp_dbg("received PREP from %pM\n", PREP_IE_ORIG_ADDR(prep_elem));
573 /* Note that we divert from the draft nomenclature and denominate
574 * destination to what the draft refers to as origininator. So in this
575 * function destnation refers to the final destination of the PREP,
576 * which corresponds with the originator of the PREQ which this PREP
579 target_addr = PREP_IE_TARGET_ADDR(prep_elem);
580 if (memcmp(target_addr, sdata->dev->dev_addr, ETH_ALEN) == 0)
581 /* destination, no forwarding required */
584 ttl = PREP_IE_TTL(prep_elem);
586 sdata->u.mesh.mshstats.dropped_frames_ttl++;
591 mpath = mesh_path_lookup(target_addr, sdata);
593 spin_lock_bh(&mpath->state_lock);
596 if (!(mpath->flags & MESH_PATH_ACTIVE)) {
597 spin_unlock_bh(&mpath->state_lock);
600 memcpy(next_hop, mpath->next_hop->sta.addr, ETH_ALEN);
601 spin_unlock_bh(&mpath->state_lock);
603 flags = PREP_IE_FLAGS(prep_elem);
604 lifetime = PREP_IE_LIFETIME(prep_elem);
605 hopcount = PREP_IE_HOPCOUNT(prep_elem) + 1;
606 orig_addr = PREP_IE_ORIG_ADDR(prep_elem);
607 target_sn = PREP_IE_TARGET_SN(prep_elem);
608 orig_sn = PREP_IE_ORIG_SN(prep_elem);
610 mesh_path_sel_frame_tx(MPATH_PREP, flags, orig_addr,
611 cpu_to_le32(orig_sn), 0, target_addr,
612 cpu_to_le32(target_sn), mpath->next_hop->sta.addr, hopcount,
613 ttl, cpu_to_le32(lifetime), cpu_to_le32(metric),
617 sdata->u.mesh.mshstats.fwded_unicast++;
618 sdata->u.mesh.mshstats.fwded_frames++;
623 sdata->u.mesh.mshstats.dropped_frames_no_route++;
627 static void hwmp_perr_frame_process(struct ieee80211_sub_if_data *sdata,
628 struct ieee80211_mgmt *mgmt, u8 *perr_elem)
630 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
631 struct mesh_path *mpath;
633 u8 *ta, *target_addr;
639 ttl = PERR_IE_TTL(perr_elem);
641 ifmsh->mshstats.dropped_frames_ttl++;
645 target_flags = PERR_IE_TARGET_FLAGS(perr_elem);
646 target_addr = PERR_IE_TARGET_ADDR(perr_elem);
647 target_sn = PERR_IE_TARGET_SN(perr_elem);
648 target_rcode = PERR_IE_TARGET_RCODE(perr_elem);
651 mpath = mesh_path_lookup(target_addr, sdata);
653 spin_lock_bh(&mpath->state_lock);
654 if (mpath->flags & MESH_PATH_ACTIVE &&
655 memcmp(ta, mpath->next_hop->sta.addr, ETH_ALEN) == 0 &&
656 (!(mpath->flags & MESH_PATH_SN_VALID) ||
657 SN_GT(target_sn, mpath->sn))) {
658 mpath->flags &= ~MESH_PATH_ACTIVE;
659 mpath->sn = target_sn;
660 spin_unlock_bh(&mpath->state_lock);
661 mesh_path_error_tx(ttl, target_addr, cpu_to_le32(target_sn),
662 cpu_to_le16(target_rcode),
663 sdata->dev->broadcast, sdata);
665 spin_unlock_bh(&mpath->state_lock);
670 static void hwmp_rann_frame_process(struct ieee80211_sub_if_data *sdata,
671 struct ieee80211_mgmt *mgmt,
672 struct ieee80211_rann_ie *rann)
674 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
675 struct mesh_path *mpath;
677 u8 ttl, flags, hopcount;
682 ttl = rann->rann_ttl;
684 ifmsh->mshstats.dropped_frames_ttl++;
688 flags = rann->rann_flags;
689 orig_addr = rann->rann_addr;
690 orig_sn = rann->rann_seq;
691 hopcount = rann->rann_hopcount;
693 metric = rann->rann_metric;
694 mhwmp_dbg("received RANN from %pM\n", orig_addr);
697 mpath = mesh_path_lookup(orig_addr, sdata);
699 mesh_path_add(orig_addr, sdata);
700 mpath = mesh_path_lookup(orig_addr, sdata);
703 sdata->u.mesh.mshstats.dropped_frames_no_route++;
706 mesh_queue_preq(mpath,
707 PREQ_Q_F_START | PREQ_Q_F_REFRESH);
709 if (mpath->sn < orig_sn) {
710 mesh_path_sel_frame_tx(MPATH_RANN, flags, orig_addr,
711 cpu_to_le32(orig_sn),
712 0, NULL, 0, sdata->dev->broadcast,
714 cpu_to_le32(metric + mpath->metric),
722 void mesh_rx_path_sel_frame(struct ieee80211_sub_if_data *sdata,
723 struct ieee80211_mgmt *mgmt,
726 struct ieee802_11_elems elems;
730 /* need action_code */
731 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
734 baselen = (u8 *) mgmt->u.action.u.mesh_action.variable - (u8 *) mgmt;
735 ieee802_11_parse_elems(mgmt->u.action.u.mesh_action.variable,
736 len - baselen, &elems);
739 if (elems.preq_len != 37)
740 /* Right now we support just 1 destination and no AE */
742 last_hop_metric = hwmp_route_info_get(sdata, mgmt, elems.preq,
745 hwmp_preq_frame_process(sdata, mgmt, elems.preq,
749 if (elems.prep_len != 31)
750 /* Right now we support no AE */
752 last_hop_metric = hwmp_route_info_get(sdata, mgmt, elems.prep,
755 hwmp_prep_frame_process(sdata, mgmt, elems.prep,
759 if (elems.perr_len != 15)
760 /* Right now we support only one destination per PERR */
762 hwmp_perr_frame_process(sdata, mgmt, elems.perr);
765 hwmp_rann_frame_process(sdata, mgmt, elems.rann);
769 * mesh_queue_preq - queue a PREQ to a given destination
771 * @mpath: mesh path to discover
772 * @flags: special attributes of the PREQ to be sent
774 * Locking: the function must be called from within a rcu read lock block.
777 static void mesh_queue_preq(struct mesh_path *mpath, u8 flags)
779 struct ieee80211_sub_if_data *sdata = mpath->sdata;
780 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
781 struct mesh_preq_queue *preq_node;
783 preq_node = kmalloc(sizeof(struct mesh_preq_queue), GFP_ATOMIC);
785 mhwmp_dbg("could not allocate PREQ node\n");
789 spin_lock(&ifmsh->mesh_preq_queue_lock);
790 if (ifmsh->preq_queue_len == MAX_PREQ_QUEUE_LEN) {
791 spin_unlock(&ifmsh->mesh_preq_queue_lock);
793 if (printk_ratelimit())
794 mhwmp_dbg("PREQ node queue full\n");
798 memcpy(preq_node->dst, mpath->dst, ETH_ALEN);
799 preq_node->flags = flags;
801 list_add_tail(&preq_node->list, &ifmsh->preq_queue.list);
802 ++ifmsh->preq_queue_len;
803 spin_unlock(&ifmsh->mesh_preq_queue_lock);
805 if (time_after(jiffies, ifmsh->last_preq + min_preq_int_jiff(sdata)))
806 ieee80211_queue_work(&sdata->local->hw, &ifmsh->work);
808 else if (time_before(jiffies, ifmsh->last_preq)) {
809 /* avoid long wait if did not send preqs for a long time
810 * and jiffies wrapped around
812 ifmsh->last_preq = jiffies - min_preq_int_jiff(sdata) - 1;
813 ieee80211_queue_work(&sdata->local->hw, &ifmsh->work);
815 mod_timer(&ifmsh->mesh_path_timer, ifmsh->last_preq +
816 min_preq_int_jiff(sdata));
820 * mesh_path_start_discovery - launch a path discovery from the PREQ queue
822 * @sdata: local mesh subif
824 void mesh_path_start_discovery(struct ieee80211_sub_if_data *sdata)
826 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
827 struct mesh_preq_queue *preq_node;
828 struct mesh_path *mpath;
829 u8 ttl, target_flags;
832 spin_lock_bh(&ifmsh->mesh_preq_queue_lock);
833 if (!ifmsh->preq_queue_len ||
834 time_before(jiffies, ifmsh->last_preq +
835 min_preq_int_jiff(sdata))) {
836 spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
840 preq_node = list_first_entry(&ifmsh->preq_queue.list,
841 struct mesh_preq_queue, list);
842 list_del(&preq_node->list);
843 --ifmsh->preq_queue_len;
844 spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
847 mpath = mesh_path_lookup(preq_node->dst, sdata);
851 spin_lock_bh(&mpath->state_lock);
852 if (preq_node->flags & PREQ_Q_F_START) {
853 if (mpath->flags & MESH_PATH_RESOLVING) {
854 spin_unlock_bh(&mpath->state_lock);
857 mpath->flags &= ~MESH_PATH_RESOLVED;
858 mpath->flags |= MESH_PATH_RESOLVING;
859 mpath->discovery_retries = 0;
860 mpath->discovery_timeout = disc_timeout_jiff(sdata);
862 } else if (!(mpath->flags & MESH_PATH_RESOLVING) ||
863 mpath->flags & MESH_PATH_RESOLVED) {
864 mpath->flags &= ~MESH_PATH_RESOLVING;
865 spin_unlock_bh(&mpath->state_lock);
869 ifmsh->last_preq = jiffies;
871 if (time_after(jiffies, ifmsh->last_sn_update +
872 net_traversal_jiffies(sdata)) ||
873 time_before(jiffies, ifmsh->last_sn_update)) {
875 sdata->u.mesh.last_sn_update = jiffies;
877 lifetime = default_lifetime(sdata);
878 ttl = sdata->u.mesh.mshcfg.dot11MeshTTL;
880 sdata->u.mesh.mshstats.dropped_frames_ttl++;
881 spin_unlock_bh(&mpath->state_lock);
885 if (preq_node->flags & PREQ_Q_F_REFRESH)
886 target_flags = MP_F_DO;
888 target_flags = MP_F_RF;
890 spin_unlock_bh(&mpath->state_lock);
891 mesh_path_sel_frame_tx(MPATH_PREQ, 0, sdata->dev->dev_addr,
892 cpu_to_le32(ifmsh->sn), target_flags, mpath->dst,
893 cpu_to_le32(mpath->sn), sdata->dev->broadcast, 0,
894 ttl, cpu_to_le32(lifetime), 0,
895 cpu_to_le32(ifmsh->preq_id++), sdata);
896 mod_timer(&mpath->timer, jiffies + mpath->discovery_timeout);
904 * mesh_nexthop_lookup - put the appropriate next hop on a mesh frame
906 * @skb: 802.11 frame to be sent
907 * @sdata: network subif the frame will be sent through
909 * Returns: 0 if the next hop was found. Nonzero otherwise. If no next hop is
910 * found, the function will start a path discovery and queue the frame so it is
911 * sent when the path is resolved. This means the caller must not free the skb
914 int mesh_nexthop_lookup(struct sk_buff *skb,
915 struct ieee80211_sub_if_data *sdata)
917 struct sk_buff *skb_to_free = NULL;
918 struct mesh_path *mpath;
919 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
920 u8 *target_addr = hdr->addr3;
924 mpath = mesh_path_lookup(target_addr, sdata);
927 mesh_path_add(target_addr, sdata);
928 mpath = mesh_path_lookup(target_addr, sdata);
930 sdata->u.mesh.mshstats.dropped_frames_no_route++;
936 if (mpath->flags & MESH_PATH_ACTIVE) {
937 if (time_after(jiffies, mpath->exp_time +
938 msecs_to_jiffies(sdata->u.mesh.mshcfg.path_refresh_time))
939 && !memcmp(sdata->dev->dev_addr, hdr->addr4,
941 && !(mpath->flags & MESH_PATH_RESOLVING)
942 && !(mpath->flags & MESH_PATH_FIXED)) {
943 mesh_queue_preq(mpath,
944 PREQ_Q_F_START | PREQ_Q_F_REFRESH);
946 memcpy(hdr->addr1, mpath->next_hop->sta.addr,
949 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
950 if (!(mpath->flags & MESH_PATH_RESOLVING)) {
951 /* Start discovery only if it is not running yet */
952 mesh_queue_preq(mpath, PREQ_Q_F_START);
955 if (skb_queue_len(&mpath->frame_queue) >=
956 MESH_FRAME_QUEUE_LEN)
957 skb_to_free = skb_dequeue(&mpath->frame_queue);
959 info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
960 skb_queue_tail(&mpath->frame_queue, skb);
962 mesh_path_discard_frame(skb_to_free, sdata);
971 void mesh_path_timer(unsigned long data)
973 struct ieee80211_sub_if_data *sdata;
974 struct mesh_path *mpath;
977 mpath = (struct mesh_path *) data;
978 mpath = rcu_dereference(mpath);
981 sdata = mpath->sdata;
983 if (sdata->local->quiescing) {
988 spin_lock_bh(&mpath->state_lock);
989 if (mpath->flags & MESH_PATH_RESOLVED ||
990 (!(mpath->flags & MESH_PATH_RESOLVING)))
991 mpath->flags &= ~(MESH_PATH_RESOLVING | MESH_PATH_RESOLVED);
992 else if (mpath->discovery_retries < max_preq_retries(sdata)) {
993 ++mpath->discovery_retries;
994 mpath->discovery_timeout *= 2;
995 mesh_queue_preq(mpath, 0);
998 mpath->exp_time = jiffies;
999 mesh_path_flush_pending(mpath);
1002 spin_unlock_bh(&mpath->state_lock);
1008 mesh_path_tx_root_frame(struct ieee80211_sub_if_data *sdata)
1010 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
1012 mesh_path_sel_frame_tx(MPATH_RANN, 0, sdata->dev->dev_addr,
1013 cpu_to_le32(++ifmsh->sn),
1014 0, NULL, 0, sdata->dev->broadcast,
1015 0, MESH_TTL, 0, 0, 0, sdata);