blob: 31bc762f209df80841fbc2956b3fb3bc9c352ff6 [file] [log] [blame]
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001/*
Rui Paulo264d9b72009-11-09 23:46:58 +00002 * Copyright (c) 2008, 2009 open80211s Ltd.
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01003 * Author: Luis Carlos Cobo <luisca@cozybit.com>
4 *
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.
8 */
9
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Johannes Bergd26ad372012-02-20 11:38:41 +010011#include <asm/unaligned.h>
Javier Cardona2cca3972011-09-06 12:10:43 -070012#include "wme.h"
Luis Carlos Cobo050ac522008-02-23 15:17:15 +010013#include "mesh.h"
14
Rui Paulo27db2e42009-11-09 23:46:45 +000015#ifdef CONFIG_MAC80211_VERBOSE_MHWMP_DEBUG
Javier Cardona76468872011-08-09 16:45:04 -070016#define mhwmp_dbg(fmt, args...) \
17 printk(KERN_DEBUG "Mesh HWMP (%s): " fmt "\n", sdata->name, ##args)
Rui Paulo27db2e42009-11-09 23:46:45 +000018#else
19#define mhwmp_dbg(fmt, args...) do { (void)(0); } while (0)
20#endif
21
Luis Carlos Cobo050ac522008-02-23 15:17:15 +010022#define TEST_FRAME_LEN 8192
23#define MAX_METRIC 0xffffffff
24#define ARITH_SHIFT 8
25
26/* Number of frames buffered per destination for unresolved destinations */
27#define MESH_FRAME_QUEUE_LEN 10
28#define MAX_PREQ_QUEUE_LEN 64
29
30/* Destination only */
31#define MP_F_DO 0x1
32/* Reply and forward */
33#define MP_F_RF 0x2
Rui Paulod611f062009-11-09 23:46:50 +000034/* Unknown Sequence Number */
35#define MP_F_USN 0x01
36/* Reason code Present */
37#define MP_F_RCODE 0x02
Luis Carlos Cobo050ac522008-02-23 15:17:15 +010038
Rui Paulo90a5e162009-11-11 00:01:31 +000039static void mesh_queue_preq(struct mesh_path *, u8);
40
Luis Carlos Coboa00de5d2008-02-29 17:07:54 -080041static inline u32 u32_field_get(u8 *preq_elem, int offset, bool ae)
42{
43 if (ae)
44 offset += 6;
Harvey Harrisonae7245c2008-05-01 22:19:33 -070045 return get_unaligned_le32(preq_elem + offset);
Luis Carlos Coboa00de5d2008-02-29 17:07:54 -080046}
47
Rui Paulod611f062009-11-09 23:46:50 +000048static inline u32 u16_field_get(u8 *preq_elem, int offset, bool ae)
49{
50 if (ae)
51 offset += 6;
52 return get_unaligned_le16(preq_elem + offset);
53}
54
Luis Carlos Cobo050ac522008-02-23 15:17:15 +010055/* HWMP IE processing macros */
Luis Carlos Coboa00de5d2008-02-29 17:07:54 -080056#define AE_F (1<<6)
57#define AE_F_SET(x) (*x & AE_F)
58#define PREQ_IE_FLAGS(x) (*(x))
59#define PREQ_IE_HOPCOUNT(x) (*(x + 1))
60#define PREQ_IE_TTL(x) (*(x + 2))
61#define PREQ_IE_PREQ_ID(x) u32_field_get(x, 3, 0)
62#define PREQ_IE_ORIG_ADDR(x) (x + 7)
Phil Carmody497888c2011-07-14 15:07:13 +030063#define PREQ_IE_ORIG_SN(x) u32_field_get(x, 13, 0)
64#define PREQ_IE_LIFETIME(x) u32_field_get(x, 17, AE_F_SET(x))
65#define PREQ_IE_METRIC(x) u32_field_get(x, 21, AE_F_SET(x))
Rui Paulod19b3bf2009-11-09 23:46:55 +000066#define PREQ_IE_TARGET_F(x) (*(AE_F_SET(x) ? x + 32 : x + 26))
67#define PREQ_IE_TARGET_ADDR(x) (AE_F_SET(x) ? x + 33 : x + 27)
Phil Carmody497888c2011-07-14 15:07:13 +030068#define PREQ_IE_TARGET_SN(x) u32_field_get(x, 33, AE_F_SET(x))
Luis Carlos Cobo050ac522008-02-23 15:17:15 +010069
70
Luis Carlos Coboa00de5d2008-02-29 17:07:54 -080071#define PREP_IE_FLAGS(x) PREQ_IE_FLAGS(x)
72#define PREP_IE_HOPCOUNT(x) PREQ_IE_HOPCOUNT(x)
73#define PREP_IE_TTL(x) PREQ_IE_TTL(x)
Thomas Pedersen25d49e42011-08-11 19:35:15 -070074#define PREP_IE_ORIG_ADDR(x) (AE_F_SET(x) ? x + 27 : x + 21)
75#define PREP_IE_ORIG_SN(x) u32_field_get(x, 27, AE_F_SET(x))
Phil Carmody497888c2011-07-14 15:07:13 +030076#define PREP_IE_LIFETIME(x) u32_field_get(x, 13, AE_F_SET(x))
77#define PREP_IE_METRIC(x) u32_field_get(x, 17, AE_F_SET(x))
Thomas Pedersen25d49e42011-08-11 19:35:15 -070078#define PREP_IE_TARGET_ADDR(x) (x + 3)
79#define PREP_IE_TARGET_SN(x) u32_field_get(x, 9, 0)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +010080
Rui Paulod611f062009-11-09 23:46:50 +000081#define PERR_IE_TTL(x) (*(x))
Rui Paulod19b3bf2009-11-09 23:46:55 +000082#define PERR_IE_TARGET_FLAGS(x) (*(x + 2))
83#define PERR_IE_TARGET_ADDR(x) (x + 3)
Phil Carmody497888c2011-07-14 15:07:13 +030084#define PERR_IE_TARGET_SN(x) u32_field_get(x, 9, 0)
85#define PERR_IE_TARGET_RCODE(x) u16_field_get(x, 13, 0)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +010086
Luis Carlos Cobo050ac522008-02-23 15:17:15 +010087#define MSEC_TO_TU(x) (x*1000/1024)
Rui Paulod19b3bf2009-11-09 23:46:55 +000088#define SN_GT(x, y) ((long) (y) - (long) (x) < 0)
89#define SN_LT(x, y) ((long) (x) - (long) (y) < 0)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +010090
91#define net_traversal_jiffies(s) \
Johannes Berg472dbc42008-09-11 00:01:49 +020092 msecs_to_jiffies(s->u.mesh.mshcfg.dot11MeshHWMPnetDiameterTraversalTime)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +010093#define default_lifetime(s) \
Johannes Berg472dbc42008-09-11 00:01:49 +020094 MSEC_TO_TU(s->u.mesh.mshcfg.dot11MeshHWMPactivePathTimeout)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +010095#define min_preq_int_jiff(s) \
Johannes Berg472dbc42008-09-11 00:01:49 +020096 (msecs_to_jiffies(s->u.mesh.mshcfg.dot11MeshHWMPpreqMinInterval))
97#define max_preq_retries(s) (s->u.mesh.mshcfg.dot11MeshHWMPmaxPREQretries)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +010098#define disc_timeout_jiff(s) \
Johannes Berg472dbc42008-09-11 00:01:49 +020099 msecs_to_jiffies(sdata->u.mesh.mshcfg.min_discovery_timeout)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100100
101enum mpath_frame_type {
102 MPATH_PREQ = 0,
103 MPATH_PREP,
Rui Paulo90a5e162009-11-11 00:01:31 +0000104 MPATH_PERR,
105 MPATH_RANN
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100106};
107
Johannes Berg15ff6362009-11-17 13:34:04 +0100108static const u8 broadcast_addr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
109
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100110static int mesh_path_sel_frame_tx(enum mpath_frame_type action, u8 flags,
Rui Paulod19b3bf2009-11-09 23:46:55 +0000111 u8 *orig_addr, __le32 orig_sn, u8 target_flags, u8 *target,
Johannes Berg15ff6362009-11-17 13:34:04 +0100112 __le32 target_sn, const u8 *da, u8 hop_count, u8 ttl,
113 __le32 lifetime, __le32 metric, __le32 preq_id,
Rui Paulod19b3bf2009-11-09 23:46:55 +0000114 struct ieee80211_sub_if_data *sdata)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100115{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200116 struct ieee80211_local *local = sdata->local;
Thomas Pedersen3b69a9c2011-10-26 14:47:25 -0700117 struct sk_buff *skb;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100118 struct ieee80211_mgmt *mgmt;
Thomas Pedersen3b69a9c2011-10-26 14:47:25 -0700119 u8 *pos, ie_len;
120 int hdr_len = offsetof(struct ieee80211_mgmt, u.action.u.mesh_action) +
121 sizeof(mgmt->u.action.u.mesh_action);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100122
Javier Cardona65e8b0c2012-01-17 18:17:46 -0800123 skb = dev_alloc_skb(local->tx_headroom +
Thomas Pedersen3b69a9c2011-10-26 14:47:25 -0700124 hdr_len +
125 2 + 37); /* max HWMP IE */
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100126 if (!skb)
127 return -1;
Javier Cardona65e8b0c2012-01-17 18:17:46 -0800128 skb_reserve(skb, local->tx_headroom);
Thomas Pedersen3b69a9c2011-10-26 14:47:25 -0700129 mgmt = (struct ieee80211_mgmt *) skb_put(skb, hdr_len);
130 memset(mgmt, 0, hdr_len);
Harvey Harrisone7827a72008-07-15 18:44:13 -0700131 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
132 IEEE80211_STYPE_ACTION);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100133
134 memcpy(mgmt->da, da, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +0100135 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
Rui Paulo90a5e162009-11-11 00:01:31 +0000136 /* BSSID == SA */
Johannes Berg47846c92009-11-25 17:46:19 +0100137 memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
Thomas Pedersen25d49e42011-08-11 19:35:15 -0700138 mgmt->u.action.category = WLAN_CATEGORY_MESH_ACTION;
139 mgmt->u.action.u.mesh_action.action_code =
140 WLAN_MESH_ACTION_HWMP_PATH_SELECTION;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100141
142 switch (action) {
143 case MPATH_PREQ:
Javier Cardona76468872011-08-09 16:45:04 -0700144 mhwmp_dbg("sending PREQ to %pM", target);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100145 ie_len = 37;
146 pos = skb_put(skb, 2 + ie_len);
147 *pos++ = WLAN_EID_PREQ;
148 break;
149 case MPATH_PREP:
Javier Cardona76468872011-08-09 16:45:04 -0700150 mhwmp_dbg("sending PREP to %pM", target);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100151 ie_len = 31;
152 pos = skb_put(skb, 2 + ie_len);
153 *pos++ = WLAN_EID_PREP;
154 break;
Rui Paulo90a5e162009-11-11 00:01:31 +0000155 case MPATH_RANN:
Javier Cardona76468872011-08-09 16:45:04 -0700156 mhwmp_dbg("sending RANN from %pM", orig_addr);
Rui Paulo90a5e162009-11-11 00:01:31 +0000157 ie_len = sizeof(struct ieee80211_rann_ie);
158 pos = skb_put(skb, 2 + ie_len);
159 *pos++ = WLAN_EID_RANN;
160 break;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100161 default:
Patrick McHardy812714d2008-05-06 12:52:07 +0200162 kfree_skb(skb);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100163 return -ENOTSUPP;
164 break;
165 }
166 *pos++ = ie_len;
167 *pos++ = flags;
168 *pos++ = hop_count;
169 *pos++ = ttl;
Thomas Pedersen25d49e42011-08-11 19:35:15 -0700170 if (action == MPATH_PREP) {
Rui Paulod19b3bf2009-11-09 23:46:55 +0000171 memcpy(pos, target, ETH_ALEN);
Rui Paulo90a5e162009-11-11 00:01:31 +0000172 pos += ETH_ALEN;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000173 memcpy(pos, &target_sn, 4);
Thomas Pedersen25d49e42011-08-11 19:35:15 -0700174 pos += 4;
175 } else {
176 if (action == MPATH_PREQ) {
177 memcpy(pos, &preq_id, 4);
178 pos += 4;
179 }
180 memcpy(pos, orig_addr, ETH_ALEN);
181 pos += ETH_ALEN;
182 memcpy(pos, &orig_sn, 4);
183 pos += 4;
184 }
185 memcpy(pos, &lifetime, 4); /* interval for RANN */
186 pos += 4;
187 memcpy(pos, &metric, 4);
188 pos += 4;
189 if (action == MPATH_PREQ) {
190 *pos++ = 1; /* destination count */
191 *pos++ = target_flags;
192 memcpy(pos, target, ETH_ALEN);
193 pos += ETH_ALEN;
194 memcpy(pos, &target_sn, 4);
195 pos += 4;
196 } else if (action == MPATH_PREP) {
197 memcpy(pos, orig_addr, ETH_ALEN);
198 pos += ETH_ALEN;
199 memcpy(pos, &orig_sn, 4);
200 pos += 4;
Rui Paulo90a5e162009-11-11 00:01:31 +0000201 }
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100202
Johannes Berg62ae67b2009-11-18 18:42:05 +0100203 ieee80211_tx_skb(sdata, skb);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100204 return 0;
205}
206
Javier Cardona2cca3972011-09-06 12:10:43 -0700207
208/* Headroom is not adjusted. Caller should ensure that skb has sufficient
209 * headroom in case the frame is encrypted. */
210static void prepare_frame_for_deferred_tx(struct ieee80211_sub_if_data *sdata,
211 struct sk_buff *skb)
212{
Javier Cardona2cca3972011-09-06 12:10:43 -0700213 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
214
215 skb_set_mac_header(skb, 0);
216 skb_set_network_header(skb, 0);
217 skb_set_transport_header(skb, 0);
218
219 /* Send all internal mgmt frames on VO. Accordingly set TID to 7. */
220 skb_set_queue_mapping(skb, IEEE80211_AC_VO);
221 skb->priority = 7;
222
223 info->control.vif = &sdata->vif;
Javier Cardona2154c812011-09-07 17:49:53 -0700224 ieee80211_set_qos_hdr(sdata, skb);
Javier Cardona2cca3972011-09-06 12:10:43 -0700225}
226
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100227/**
228 * mesh_send_path error - Sends a PERR mesh management frame
229 *
Rui Paulod19b3bf2009-11-09 23:46:55 +0000230 * @target: broken destination
231 * @target_sn: SN of the broken destination
232 * @target_rcode: reason code for this PERR
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100233 * @ra: node this frame is addressed to
Javier Cardona2cca3972011-09-06 12:10:43 -0700234 *
235 * Note: This function may be called with driver locks taken that the driver
236 * also acquires in the TX path. To avoid a deadlock we don't transmit the
237 * frame directly but add it to the pending queue instead.
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100238 */
Rui Paulod19b3bf2009-11-09 23:46:55 +0000239int mesh_path_error_tx(u8 ttl, u8 *target, __le32 target_sn,
Johannes Berg15ff6362009-11-17 13:34:04 +0100240 __le16 target_rcode, const u8 *ra,
241 struct ieee80211_sub_if_data *sdata)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100242{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200243 struct ieee80211_local *local = sdata->local;
Thomas Pedersen3b69a9c2011-10-26 14:47:25 -0700244 struct sk_buff *skb;
Thomas Pedersendca7e942011-11-24 17:15:24 -0800245 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100246 struct ieee80211_mgmt *mgmt;
Thomas Pedersen3b69a9c2011-10-26 14:47:25 -0700247 u8 *pos, ie_len;
248 int hdr_len = offsetof(struct ieee80211_mgmt, u.action.u.mesh_action) +
249 sizeof(mgmt->u.action.u.mesh_action);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100250
Thomas Pedersendca7e942011-11-24 17:15:24 -0800251 if (time_before(jiffies, ifmsh->next_perr))
252 return -EAGAIN;
253
Javier Cardona65e8b0c2012-01-17 18:17:46 -0800254 skb = dev_alloc_skb(local->tx_headroom +
Thomas Pedersen3b69a9c2011-10-26 14:47:25 -0700255 hdr_len +
256 2 + 15 /* PERR IE */);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100257 if (!skb)
258 return -1;
Javier Cardona65e8b0c2012-01-17 18:17:46 -0800259 skb_reserve(skb, local->tx_headroom);
Thomas Pedersen3b69a9c2011-10-26 14:47:25 -0700260 mgmt = (struct ieee80211_mgmt *) skb_put(skb, hdr_len);
261 memset(mgmt, 0, hdr_len);
Harvey Harrisone7827a72008-07-15 18:44:13 -0700262 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
263 IEEE80211_STYPE_ACTION);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100264
265 memcpy(mgmt->da, ra, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +0100266 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
Thomas Pedersen25d49e42011-08-11 19:35:15 -0700267 /* BSSID == SA */
268 memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
269 mgmt->u.action.category = WLAN_CATEGORY_MESH_ACTION;
270 mgmt->u.action.u.mesh_action.action_code =
271 WLAN_MESH_ACTION_HWMP_PATH_SELECTION;
Rui Paulod611f062009-11-09 23:46:50 +0000272 ie_len = 15;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100273 pos = skb_put(skb, 2 + ie_len);
274 *pos++ = WLAN_EID_PERR;
275 *pos++ = ie_len;
Rui Paulod611f062009-11-09 23:46:50 +0000276 /* ttl */
Javier Cardona45904f22010-12-03 09:20:40 +0100277 *pos++ = ttl;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100278 /* number of destinations */
279 *pos++ = 1;
Rui Paulod611f062009-11-09 23:46:50 +0000280 /*
281 * flags bit, bit 1 is unset if we know the sequence number and
282 * bit 2 is set if we have a reason code
283 */
284 *pos = 0;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000285 if (!target_sn)
Rui Paulod611f062009-11-09 23:46:50 +0000286 *pos |= MP_F_USN;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000287 if (target_rcode)
Rui Paulod611f062009-11-09 23:46:50 +0000288 *pos |= MP_F_RCODE;
289 pos++;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000290 memcpy(pos, target, ETH_ALEN);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100291 pos += ETH_ALEN;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000292 memcpy(pos, &target_sn, 4);
Rui Paulod611f062009-11-09 23:46:50 +0000293 pos += 4;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000294 memcpy(pos, &target_rcode, 2);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100295
Javier Cardona2cca3972011-09-06 12:10:43 -0700296 /* see note in function header */
297 prepare_frame_for_deferred_tx(sdata, skb);
Thomas Pedersendca7e942011-11-24 17:15:24 -0800298 ifmsh->next_perr = TU_TO_EXP_TIME(
299 ifmsh->mshcfg.dot11MeshHWMPperrMinInterval);
Javier Cardona2cca3972011-09-06 12:10:43 -0700300 ieee80211_add_pending_skb(local, skb);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100301 return 0;
302}
303
Javier Cardonabfc32e62009-08-17 17:15:55 -0700304void ieee80211s_update_metric(struct ieee80211_local *local,
305 struct sta_info *stainfo, struct sk_buff *skb)
306{
307 struct ieee80211_tx_info *txinfo = IEEE80211_SKB_CB(skb);
308 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
309 int failed;
310
311 if (!ieee80211_is_data(hdr->frame_control))
312 return;
313
314 failed = !(txinfo->flags & IEEE80211_TX_STAT_ACK);
315
316 /* moving average, scaled to 100 */
317 stainfo->fail_avg = ((80 * stainfo->fail_avg + 5) / 100 + 20 * failed);
318 if (stainfo->fail_avg > 95)
319 mesh_plink_broken(stainfo);
320}
321
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100322static u32 airtime_link_metric_get(struct ieee80211_local *local,
323 struct sta_info *sta)
324{
325 struct ieee80211_supported_band *sband;
326 /* This should be adjusted for each device */
327 int device_constant = 1 << ARITH_SHIFT;
328 int test_frame_len = TEST_FRAME_LEN << ARITH_SHIFT;
329 int s_unit = 1 << ARITH_SHIFT;
330 int rate, err;
331 u32 tx_time, estimated_retx;
332 u64 result;
333
334 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
335
336 if (sta->fail_avg >= 100)
337 return MAX_METRIC;
Johannes Berge6a98542008-10-21 12:40:02 +0200338
339 if (sta->last_tx_rate.flags & IEEE80211_TX_RC_MCS)
340 return MAX_METRIC;
341
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100342 err = (sta->fail_avg << ARITH_SHIFT) / 100;
343
344 /* bitrate is in units of 100 Kbps, while we need rate in units of
345 * 1Mbps. This will be corrected on tx_time computation.
346 */
Johannes Berge6a98542008-10-21 12:40:02 +0200347 rate = sband->bitrates[sta->last_tx_rate.idx].bitrate;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100348 tx_time = (device_constant + 10 * test_frame_len / rate);
349 estimated_retx = ((1 << (2 * ARITH_SHIFT)) / (s_unit - err));
350 result = (tx_time * estimated_retx) >> (2 * ARITH_SHIFT) ;
351 return (u32)result;
352}
353
354/**
355 * hwmp_route_info_get - Update routing info to originator and transmitter
356 *
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200357 * @sdata: local mesh subif
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100358 * @mgmt: mesh management frame
359 * @hwmp_ie: hwmp information element (PREP or PREQ)
360 *
361 * This function updates the path routing information to the originator and the
Andrey Yurovskyf99288d2009-10-20 12:17:34 -0700362 * transmitter of a HWMP PREQ or PREP frame.
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100363 *
364 * Returns: metric to frame originator or 0 if the frame should not be further
365 * processed
366 *
367 * Notes: this function is the only place (besides user-provided info) where
368 * path routing information is updated.
369 */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200370static u32 hwmp_route_info_get(struct ieee80211_sub_if_data *sdata,
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100371 struct ieee80211_mgmt *mgmt,
Rui Paulodbb81c42009-11-09 23:46:46 +0000372 u8 *hwmp_ie, enum mpath_frame_type action)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100373{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200374 struct ieee80211_local *local = sdata->local;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100375 struct mesh_path *mpath;
376 struct sta_info *sta;
377 bool fresh_info;
378 u8 *orig_addr, *ta;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000379 u32 orig_sn, orig_metric;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100380 unsigned long orig_lifetime, exp_time;
381 u32 last_hop_metric, new_metric;
382 bool process = true;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100383
384 rcu_read_lock();
Johannes Bergabe60632009-11-25 17:46:18 +0100385 sta = sta_info_get(sdata, mgmt->sa);
Johannes Bergdc0b0f72008-02-23 15:17:20 +0100386 if (!sta) {
387 rcu_read_unlock();
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100388 return 0;
Johannes Bergdc0b0f72008-02-23 15:17:20 +0100389 }
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100390
391 last_hop_metric = airtime_link_metric_get(local, sta);
392 /* Update and check originator routing info */
393 fresh_info = true;
394
395 switch (action) {
396 case MPATH_PREQ:
397 orig_addr = PREQ_IE_ORIG_ADDR(hwmp_ie);
Rui Paulod19b3bf2009-11-09 23:46:55 +0000398 orig_sn = PREQ_IE_ORIG_SN(hwmp_ie);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100399 orig_lifetime = PREQ_IE_LIFETIME(hwmp_ie);
400 orig_metric = PREQ_IE_METRIC(hwmp_ie);
401 break;
402 case MPATH_PREP:
Thomas Pedersen3c26f1f2011-11-24 17:15:22 -0800403 /* Originator here refers to the MP that was the target in the
404 * Path Request. We divert from the nomenclature in the draft
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100405 * so that we can easily use a single function to gather path
406 * information from both PREQ and PREP frames.
407 */
Thomas Pedersen3c26f1f2011-11-24 17:15:22 -0800408 orig_addr = PREP_IE_TARGET_ADDR(hwmp_ie);
409 orig_sn = PREP_IE_TARGET_SN(hwmp_ie);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100410 orig_lifetime = PREP_IE_LIFETIME(hwmp_ie);
411 orig_metric = PREP_IE_METRIC(hwmp_ie);
412 break;
413 default:
Johannes Bergdc0b0f72008-02-23 15:17:20 +0100414 rcu_read_unlock();
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100415 return 0;
416 }
417 new_metric = orig_metric + last_hop_metric;
418 if (new_metric < orig_metric)
419 new_metric = MAX_METRIC;
420 exp_time = TU_TO_EXP_TIME(orig_lifetime);
421
Johannes Berg47846c92009-11-25 17:46:19 +0100422 if (memcmp(orig_addr, sdata->vif.addr, ETH_ALEN) == 0) {
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100423 /* This MP is the originator, we are not interested in this
424 * frame, except for updating transmitter's path info.
425 */
426 process = false;
427 fresh_info = false;
428 } else {
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200429 mpath = mesh_path_lookup(orig_addr, sdata);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100430 if (mpath) {
431 spin_lock_bh(&mpath->state_lock);
432 if (mpath->flags & MESH_PATH_FIXED)
433 fresh_info = false;
434 else if ((mpath->flags & MESH_PATH_ACTIVE) &&
Rui Paulod19b3bf2009-11-09 23:46:55 +0000435 (mpath->flags & MESH_PATH_SN_VALID)) {
436 if (SN_GT(mpath->sn, orig_sn) ||
437 (mpath->sn == orig_sn &&
Porsch, Marco533866b2010-02-24 09:53:13 +0100438 new_metric >= mpath->metric)) {
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100439 process = false;
440 fresh_info = false;
441 }
442 }
443 } else {
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200444 mesh_path_add(orig_addr, sdata);
445 mpath = mesh_path_lookup(orig_addr, sdata);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100446 if (!mpath) {
447 rcu_read_unlock();
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100448 return 0;
449 }
450 spin_lock_bh(&mpath->state_lock);
451 }
452
453 if (fresh_info) {
454 mesh_path_assign_nexthop(mpath, sta);
Rui Paulod19b3bf2009-11-09 23:46:55 +0000455 mpath->flags |= MESH_PATH_SN_VALID;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100456 mpath->metric = new_metric;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000457 mpath->sn = orig_sn;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100458 mpath->exp_time = time_after(mpath->exp_time, exp_time)
459 ? mpath->exp_time : exp_time;
460 mesh_path_activate(mpath);
461 spin_unlock_bh(&mpath->state_lock);
462 mesh_path_tx_pending(mpath);
463 /* draft says preq_id should be saved to, but there does
464 * not seem to be any use for it, skipping by now
465 */
466 } else
467 spin_unlock_bh(&mpath->state_lock);
468 }
469
470 /* Update and check transmitter routing info */
471 ta = mgmt->sa;
472 if (memcmp(orig_addr, ta, ETH_ALEN) == 0)
473 fresh_info = false;
474 else {
475 fresh_info = true;
476
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200477 mpath = mesh_path_lookup(ta, sdata);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100478 if (mpath) {
479 spin_lock_bh(&mpath->state_lock);
480 if ((mpath->flags & MESH_PATH_FIXED) ||
481 ((mpath->flags & MESH_PATH_ACTIVE) &&
482 (last_hop_metric > mpath->metric)))
483 fresh_info = false;
484 } else {
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200485 mesh_path_add(ta, sdata);
486 mpath = mesh_path_lookup(ta, sdata);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100487 if (!mpath) {
488 rcu_read_unlock();
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100489 return 0;
490 }
491 spin_lock_bh(&mpath->state_lock);
492 }
493
494 if (fresh_info) {
495 mesh_path_assign_nexthop(mpath, sta);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100496 mpath->metric = last_hop_metric;
497 mpath->exp_time = time_after(mpath->exp_time, exp_time)
498 ? mpath->exp_time : exp_time;
499 mesh_path_activate(mpath);
500 spin_unlock_bh(&mpath->state_lock);
501 mesh_path_tx_pending(mpath);
502 } else
503 spin_unlock_bh(&mpath->state_lock);
504 }
505
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100506 rcu_read_unlock();
507
508 return process ? new_metric : 0;
509}
510
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200511static void hwmp_preq_frame_process(struct ieee80211_sub_if_data *sdata,
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100512 struct ieee80211_mgmt *mgmt,
David Woo57ef5dd2009-08-12 11:03:43 -0700513 u8 *preq_elem, u32 metric)
514{
Johannes Berg472dbc42008-09-11 00:01:49 +0200515 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100516 struct mesh_path *mpath;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000517 u8 *target_addr, *orig_addr;
518 u8 target_flags, ttl;
519 u32 orig_sn, target_sn, lifetime;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100520 bool reply = false;
521 bool forward = true;
522
Rui Paulod19b3bf2009-11-09 23:46:55 +0000523 /* Update target SN, if present */
524 target_addr = PREQ_IE_TARGET_ADDR(preq_elem);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100525 orig_addr = PREQ_IE_ORIG_ADDR(preq_elem);
Rui Paulod19b3bf2009-11-09 23:46:55 +0000526 target_sn = PREQ_IE_TARGET_SN(preq_elem);
527 orig_sn = PREQ_IE_ORIG_SN(preq_elem);
528 target_flags = PREQ_IE_TARGET_F(preq_elem);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100529
Javier Cardona76468872011-08-09 16:45:04 -0700530 mhwmp_dbg("received PREQ from %pM", orig_addr);
Rui Paulo27db2e42009-11-09 23:46:45 +0000531
Johannes Berg47846c92009-11-25 17:46:19 +0100532 if (memcmp(target_addr, sdata->vif.addr, ETH_ALEN) == 0) {
Javier Cardona76468872011-08-09 16:45:04 -0700533 mhwmp_dbg("PREQ is for us");
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100534 forward = false;
535 reply = true;
536 metric = 0;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000537 if (time_after(jiffies, ifmsh->last_sn_update +
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100538 net_traversal_jiffies(sdata)) ||
Rui Paulod19b3bf2009-11-09 23:46:55 +0000539 time_before(jiffies, ifmsh->last_sn_update)) {
540 target_sn = ++ifmsh->sn;
541 ifmsh->last_sn_update = jiffies;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100542 }
543 } else {
544 rcu_read_lock();
Rui Paulod19b3bf2009-11-09 23:46:55 +0000545 mpath = mesh_path_lookup(target_addr, sdata);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100546 if (mpath) {
Rui Paulod19b3bf2009-11-09 23:46:55 +0000547 if ((!(mpath->flags & MESH_PATH_SN_VALID)) ||
548 SN_LT(mpath->sn, target_sn)) {
549 mpath->sn = target_sn;
550 mpath->flags |= MESH_PATH_SN_VALID;
551 } else if ((!(target_flags & MP_F_DO)) &&
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100552 (mpath->flags & MESH_PATH_ACTIVE)) {
553 reply = true;
554 metric = mpath->metric;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000555 target_sn = mpath->sn;
556 if (target_flags & MP_F_RF)
557 target_flags |= MP_F_DO;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100558 else
559 forward = false;
560 }
561 }
562 rcu_read_unlock();
563 }
564
565 if (reply) {
566 lifetime = PREQ_IE_LIFETIME(preq_elem);
Javier Cardona45904f22010-12-03 09:20:40 +0100567 ttl = ifmsh->mshcfg.element_ttl;
Rui Paulo27db2e42009-11-09 23:46:45 +0000568 if (ttl != 0) {
Javier Cardona76468872011-08-09 16:45:04 -0700569 mhwmp_dbg("replying to the PREQ");
Thomas Pedersen3c26f1f2011-11-24 17:15:22 -0800570 mesh_path_sel_frame_tx(MPATH_PREP, 0, orig_addr,
571 cpu_to_le32(orig_sn), 0, target_addr,
572 cpu_to_le32(target_sn), mgmt->sa, 0, ttl,
Luis Carlos Coboaa2b5922008-02-29 14:30:32 -0800573 cpu_to_le32(lifetime), cpu_to_le32(metric),
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200574 0, sdata);
Rui Paulo27db2e42009-11-09 23:46:45 +0000575 } else
Johannes Berg472dbc42008-09-11 00:01:49 +0200576 ifmsh->mshstats.dropped_frames_ttl++;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100577 }
578
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +0800579 if (forward && ifmsh->mshcfg.dot11MeshForwarding) {
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100580 u32 preq_id;
581 u8 hopcount, flags;
582
583 ttl = PREQ_IE_TTL(preq_elem);
584 lifetime = PREQ_IE_LIFETIME(preq_elem);
585 if (ttl <= 1) {
Johannes Berg472dbc42008-09-11 00:01:49 +0200586 ifmsh->mshstats.dropped_frames_ttl++;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100587 return;
588 }
Javier Cardona76468872011-08-09 16:45:04 -0700589 mhwmp_dbg("forwarding the PREQ from %pM", orig_addr);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100590 --ttl;
591 flags = PREQ_IE_FLAGS(preq_elem);
592 preq_id = PREQ_IE_PREQ_ID(preq_elem);
593 hopcount = PREQ_IE_HOPCOUNT(preq_elem) + 1;
594 mesh_path_sel_frame_tx(MPATH_PREQ, flags, orig_addr,
Rui Paulod19b3bf2009-11-09 23:46:55 +0000595 cpu_to_le32(orig_sn), target_flags, target_addr,
Johannes Berg15ff6362009-11-17 13:34:04 +0100596 cpu_to_le32(target_sn), broadcast_addr,
Luis Carlos Coboaa2b5922008-02-29 14:30:32 -0800597 hopcount, ttl, cpu_to_le32(lifetime),
598 cpu_to_le32(metric), cpu_to_le32(preq_id),
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200599 sdata);
Daniel Walkerc8a61a72009-08-18 10:59:00 -0700600 ifmsh->mshstats.fwded_mcast++;
Johannes Berg472dbc42008-09-11 00:01:49 +0200601 ifmsh->mshstats.fwded_frames++;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100602 }
603}
604
605
Johannes Berg40b275b2011-05-13 14:15:49 +0200606static inline struct sta_info *
607next_hop_deref_protected(struct mesh_path *mpath)
608{
609 return rcu_dereference_protected(mpath->next_hop,
610 lockdep_is_held(&mpath->state_lock));
611}
612
613
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200614static void hwmp_prep_frame_process(struct ieee80211_sub_if_data *sdata,
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100615 struct ieee80211_mgmt *mgmt,
616 u8 *prep_elem, u32 metric)
617{
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100618 struct mesh_path *mpath;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000619 u8 *target_addr, *orig_addr;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100620 u8 ttl, hopcount, flags;
621 u8 next_hop[ETH_ALEN];
Rui Paulod19b3bf2009-11-09 23:46:55 +0000622 u32 target_sn, orig_sn, lifetime;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100623
Javier Cardona76468872011-08-09 16:45:04 -0700624 mhwmp_dbg("received PREP from %pM", PREP_IE_ORIG_ADDR(prep_elem));
Rui Paulodbb81c42009-11-09 23:46:46 +0000625
Thomas Pedersen3c26f1f2011-11-24 17:15:22 -0800626 orig_addr = PREP_IE_ORIG_ADDR(prep_elem);
627 if (memcmp(orig_addr, sdata->vif.addr, ETH_ALEN) == 0)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100628 /* destination, no forwarding required */
629 return;
630
631 ttl = PREP_IE_TTL(prep_elem);
632 if (ttl <= 1) {
Johannes Berg472dbc42008-09-11 00:01:49 +0200633 sdata->u.mesh.mshstats.dropped_frames_ttl++;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100634 return;
635 }
636
637 rcu_read_lock();
Thomas Pedersen3c26f1f2011-11-24 17:15:22 -0800638 mpath = mesh_path_lookup(orig_addr, sdata);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100639 if (mpath)
640 spin_lock_bh(&mpath->state_lock);
641 else
642 goto fail;
643 if (!(mpath->flags & MESH_PATH_ACTIVE)) {
644 spin_unlock_bh(&mpath->state_lock);
645 goto fail;
646 }
Johannes Berg40b275b2011-05-13 14:15:49 +0200647 memcpy(next_hop, next_hop_deref_protected(mpath)->sta.addr, ETH_ALEN);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100648 spin_unlock_bh(&mpath->state_lock);
649 --ttl;
650 flags = PREP_IE_FLAGS(prep_elem);
651 lifetime = PREP_IE_LIFETIME(prep_elem);
652 hopcount = PREP_IE_HOPCOUNT(prep_elem) + 1;
Thomas Pedersen3c26f1f2011-11-24 17:15:22 -0800653 target_addr = PREP_IE_TARGET_ADDR(prep_elem);
Rui Paulod19b3bf2009-11-09 23:46:55 +0000654 target_sn = PREP_IE_TARGET_SN(prep_elem);
655 orig_sn = PREP_IE_ORIG_SN(prep_elem);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100656
657 mesh_path_sel_frame_tx(MPATH_PREP, flags, orig_addr,
Rui Paulod19b3bf2009-11-09 23:46:55 +0000658 cpu_to_le32(orig_sn), 0, target_addr,
Porsch, Marco533866b2010-02-24 09:53:13 +0100659 cpu_to_le32(target_sn), next_hop, hopcount,
Rui Paulod19b3bf2009-11-09 23:46:55 +0000660 ttl, cpu_to_le32(lifetime), cpu_to_le32(metric),
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200661 0, sdata);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100662 rcu_read_unlock();
Daniel Walkerc8a61a72009-08-18 10:59:00 -0700663
664 sdata->u.mesh.mshstats.fwded_unicast++;
Johannes Berg472dbc42008-09-11 00:01:49 +0200665 sdata->u.mesh.mshstats.fwded_frames++;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100666 return;
667
668fail:
669 rcu_read_unlock();
Johannes Berg472dbc42008-09-11 00:01:49 +0200670 sdata->u.mesh.mshstats.dropped_frames_no_route++;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100671}
672
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200673static void hwmp_perr_frame_process(struct ieee80211_sub_if_data *sdata,
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100674 struct ieee80211_mgmt *mgmt, u8 *perr_elem)
675{
Rui Paulod611f062009-11-09 23:46:50 +0000676 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100677 struct mesh_path *mpath;
Rui Paulod611f062009-11-09 23:46:50 +0000678 u8 ttl;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000679 u8 *ta, *target_addr;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000680 u32 target_sn;
681 u16 target_rcode;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100682
683 ta = mgmt->sa;
Rui Paulod611f062009-11-09 23:46:50 +0000684 ttl = PERR_IE_TTL(perr_elem);
685 if (ttl <= 1) {
686 ifmsh->mshstats.dropped_frames_ttl++;
687 return;
688 }
689 ttl--;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000690 target_addr = PERR_IE_TARGET_ADDR(perr_elem);
691 target_sn = PERR_IE_TARGET_SN(perr_elem);
692 target_rcode = PERR_IE_TARGET_RCODE(perr_elem);
Rui Paulod611f062009-11-09 23:46:50 +0000693
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100694 rcu_read_lock();
Rui Paulod19b3bf2009-11-09 23:46:55 +0000695 mpath = mesh_path_lookup(target_addr, sdata);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100696 if (mpath) {
697 spin_lock_bh(&mpath->state_lock);
698 if (mpath->flags & MESH_PATH_ACTIVE &&
Johannes Berg40b275b2011-05-13 14:15:49 +0200699 memcmp(ta, next_hop_deref_protected(mpath)->sta.addr,
700 ETH_ALEN) == 0 &&
Rui Paulod19b3bf2009-11-09 23:46:55 +0000701 (!(mpath->flags & MESH_PATH_SN_VALID) ||
702 SN_GT(target_sn, mpath->sn))) {
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100703 mpath->flags &= ~MESH_PATH_ACTIVE;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000704 mpath->sn = target_sn;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100705 spin_unlock_bh(&mpath->state_lock);
Rui Paulod19b3bf2009-11-09 23:46:55 +0000706 mesh_path_error_tx(ttl, target_addr, cpu_to_le32(target_sn),
707 cpu_to_le16(target_rcode),
Johannes Berg15ff6362009-11-17 13:34:04 +0100708 broadcast_addr, sdata);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100709 } else
710 spin_unlock_bh(&mpath->state_lock);
711 }
712 rcu_read_unlock();
713}
714
Rui Paulo90a5e162009-11-11 00:01:31 +0000715static void hwmp_rann_frame_process(struct ieee80211_sub_if_data *sdata,
716 struct ieee80211_mgmt *mgmt,
717 struct ieee80211_rann_ie *rann)
718{
719 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
720 struct mesh_path *mpath;
Rui Paulo90a5e162009-11-11 00:01:31 +0000721 u8 ttl, flags, hopcount;
722 u8 *orig_addr;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000723 u32 orig_sn, metric;
Javier Cardona0507e152011-08-09 16:45:10 -0700724 u32 interval = ifmsh->mshcfg.dot11MeshHWMPRannInterval;
Javier Cardona5ee68e52011-08-09 16:45:08 -0700725 bool root_is_gate;
Rui Paulo90a5e162009-11-11 00:01:31 +0000726
Rui Paulo90a5e162009-11-11 00:01:31 +0000727 ttl = rann->rann_ttl;
728 if (ttl <= 1) {
729 ifmsh->mshstats.dropped_frames_ttl++;
730 return;
731 }
732 ttl--;
733 flags = rann->rann_flags;
Javier Cardona5ee68e52011-08-09 16:45:08 -0700734 root_is_gate = !!(flags & RANN_FLAG_IS_GATE);
Rui Paulo90a5e162009-11-11 00:01:31 +0000735 orig_addr = rann->rann_addr;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000736 orig_sn = rann->rann_seq;
Rui Paulo90a5e162009-11-11 00:01:31 +0000737 hopcount = rann->rann_hopcount;
Rui Pauloa6a58b42009-11-09 23:46:51 +0000738 hopcount++;
Rui Paulo90a5e162009-11-11 00:01:31 +0000739 metric = rann->rann_metric;
Javier Cardona5ee68e52011-08-09 16:45:08 -0700740
741 /* Ignore our own RANNs */
742 if (memcmp(orig_addr, sdata->vif.addr, ETH_ALEN) == 0)
743 return;
744
745 mhwmp_dbg("received RANN from %pM (is_gate=%d)", orig_addr,
746 root_is_gate);
Rui Paulo90a5e162009-11-11 00:01:31 +0000747
748 rcu_read_lock();
749 mpath = mesh_path_lookup(orig_addr, sdata);
750 if (!mpath) {
751 mesh_path_add(orig_addr, sdata);
752 mpath = mesh_path_lookup(orig_addr, sdata);
753 if (!mpath) {
754 rcu_read_unlock();
755 sdata->u.mesh.mshstats.dropped_frames_no_route++;
756 return;
757 }
Rui Paulo90a5e162009-11-11 00:01:31 +0000758 }
Javier Cardona5ee68e52011-08-09 16:45:08 -0700759
760 if ((!(mpath->flags & (MESH_PATH_ACTIVE | MESH_PATH_RESOLVING)) ||
761 time_after(jiffies, mpath->exp_time - 1*HZ)) &&
762 !(mpath->flags & MESH_PATH_FIXED)) {
763 mhwmp_dbg("%s time to refresh root mpath %pM", sdata->name,
764 orig_addr);
765 mesh_queue_preq(mpath, PREQ_Q_F_START | PREQ_Q_F_REFRESH);
766 }
767
Rui Paulod19b3bf2009-11-09 23:46:55 +0000768 if (mpath->sn < orig_sn) {
Rui Paulo90a5e162009-11-11 00:01:31 +0000769 mesh_path_sel_frame_tx(MPATH_RANN, flags, orig_addr,
Rui Paulod19b3bf2009-11-09 23:46:55 +0000770 cpu_to_le32(orig_sn),
Johannes Berg15ff6362009-11-17 13:34:04 +0100771 0, NULL, 0, broadcast_addr,
Javier Cardona0507e152011-08-09 16:45:10 -0700772 hopcount, ttl, cpu_to_le32(interval),
Rui Pauloa6a58b42009-11-09 23:46:51 +0000773 cpu_to_le32(metric + mpath->metric),
Rui Paulo90a5e162009-11-11 00:01:31 +0000774 0, sdata);
Rui Paulod19b3bf2009-11-09 23:46:55 +0000775 mpath->sn = orig_sn;
Rui Paulo90a5e162009-11-11 00:01:31 +0000776 }
Javier Cardona5ee68e52011-08-09 16:45:08 -0700777 if (root_is_gate)
778 mesh_path_add_gate(mpath);
779
Rui Paulo90a5e162009-11-11 00:01:31 +0000780 rcu_read_unlock();
781}
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100782
783
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200784void mesh_rx_path_sel_frame(struct ieee80211_sub_if_data *sdata,
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100785 struct ieee80211_mgmt *mgmt,
786 size_t len)
787{
788 struct ieee802_11_elems elems;
789 size_t baselen;
790 u32 last_hop_metric;
Javier Cardona97091312011-10-06 14:54:22 -0700791 struct sta_info *sta;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100792
Johannes Berg9c80d3d2008-09-08 15:41:59 +0200793 /* need action_code */
794 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
795 return;
796
Javier Cardona97091312011-10-06 14:54:22 -0700797 rcu_read_lock();
798 sta = sta_info_get(sdata, mgmt->sa);
799 if (!sta || sta->plink_state != NL80211_PLINK_ESTAB) {
800 rcu_read_unlock();
801 return;
802 }
803 rcu_read_unlock();
804
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100805 baselen = (u8 *) mgmt->u.action.u.mesh_action.variable - (u8 *) mgmt;
806 ieee802_11_parse_elems(mgmt->u.action.u.mesh_action.variable,
807 len - baselen, &elems);
808
Rui Paulodbb81c42009-11-09 23:46:46 +0000809 if (elems.preq) {
810 if (elems.preq_len != 37)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100811 /* Right now we support just 1 destination and no AE */
812 return;
Rui Paulodbb81c42009-11-09 23:46:46 +0000813 last_hop_metric = hwmp_route_info_get(sdata, mgmt, elems.preq,
814 MPATH_PREQ);
815 if (last_hop_metric)
816 hwmp_preq_frame_process(sdata, mgmt, elems.preq,
817 last_hop_metric);
818 }
819 if (elems.prep) {
820 if (elems.prep_len != 31)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100821 /* Right now we support no AE */
822 return;
Rui Paulodbb81c42009-11-09 23:46:46 +0000823 last_hop_metric = hwmp_route_info_get(sdata, mgmt, elems.prep,
824 MPATH_PREP);
825 if (last_hop_metric)
826 hwmp_prep_frame_process(sdata, mgmt, elems.prep,
827 last_hop_metric);
828 }
829 if (elems.perr) {
Rui Paulod611f062009-11-09 23:46:50 +0000830 if (elems.perr_len != 15)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100831 /* Right now we support only one destination per PERR */
832 return;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200833 hwmp_perr_frame_process(sdata, mgmt, elems.perr);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100834 }
Rui Paulo90a5e162009-11-11 00:01:31 +0000835 if (elems.rann)
836 hwmp_rann_frame_process(sdata, mgmt, elems.rann);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100837}
838
839/**
840 * mesh_queue_preq - queue a PREQ to a given destination
841 *
842 * @mpath: mesh path to discover
843 * @flags: special attributes of the PREQ to be sent
844 *
845 * Locking: the function must be called from within a rcu read lock block.
846 *
847 */
848static void mesh_queue_preq(struct mesh_path *mpath, u8 flags)
849{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200850 struct ieee80211_sub_if_data *sdata = mpath->sdata;
Johannes Berg472dbc42008-09-11 00:01:49 +0200851 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100852 struct mesh_preq_queue *preq_node;
853
Andrey Yurovsky59615b52009-06-25 16:07:42 -0700854 preq_node = kmalloc(sizeof(struct mesh_preq_queue), GFP_ATOMIC);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100855 if (!preq_node) {
Javier Cardona76468872011-08-09 16:45:04 -0700856 mhwmp_dbg("could not allocate PREQ node");
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100857 return;
858 }
859
Baruch Siach987dafa2011-07-28 08:51:05 +0300860 spin_lock_bh(&ifmsh->mesh_preq_queue_lock);
Johannes Berg472dbc42008-09-11 00:01:49 +0200861 if (ifmsh->preq_queue_len == MAX_PREQ_QUEUE_LEN) {
Baruch Siach987dafa2011-07-28 08:51:05 +0300862 spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100863 kfree(preq_node);
864 if (printk_ratelimit())
Javier Cardona76468872011-08-09 16:45:04 -0700865 mhwmp_dbg("PREQ node queue full");
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100866 return;
867 }
868
Johannes Bergf2dc7982011-11-18 15:27:31 +0100869 spin_lock(&mpath->state_lock);
Javier Cardonaf3011cf2011-11-03 21:11:10 -0700870 if (mpath->flags & MESH_PATH_REQ_QUEUED) {
Johannes Bergf2dc7982011-11-18 15:27:31 +0100871 spin_unlock(&mpath->state_lock);
Javier Cardonaf3011cf2011-11-03 21:11:10 -0700872 spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
Dan Carpenter88d53462011-11-15 09:33:31 +0300873 kfree(preq_node);
Javier Cardonaf3011cf2011-11-03 21:11:10 -0700874 return;
875 }
876
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100877 memcpy(preq_node->dst, mpath->dst, ETH_ALEN);
878 preq_node->flags = flags;
879
Javier Cardonaf3011cf2011-11-03 21:11:10 -0700880 mpath->flags |= MESH_PATH_REQ_QUEUED;
Johannes Bergf2dc7982011-11-18 15:27:31 +0100881 spin_unlock(&mpath->state_lock);
Javier Cardonaf3011cf2011-11-03 21:11:10 -0700882
Johannes Berg472dbc42008-09-11 00:01:49 +0200883 list_add_tail(&preq_node->list, &ifmsh->preq_queue.list);
884 ++ifmsh->preq_queue_len;
Baruch Siach987dafa2011-07-28 08:51:05 +0300885 spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100886
Johannes Berg472dbc42008-09-11 00:01:49 +0200887 if (time_after(jiffies, ifmsh->last_preq + min_preq_int_jiff(sdata)))
Johannes Berg64592c82010-06-10 10:21:31 +0200888 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100889
Johannes Berg472dbc42008-09-11 00:01:49 +0200890 else if (time_before(jiffies, ifmsh->last_preq)) {
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100891 /* avoid long wait if did not send preqs for a long time
892 * and jiffies wrapped around
893 */
Johannes Berg472dbc42008-09-11 00:01:49 +0200894 ifmsh->last_preq = jiffies - min_preq_int_jiff(sdata) - 1;
Johannes Berg64592c82010-06-10 10:21:31 +0200895 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100896 } else
Johannes Berg472dbc42008-09-11 00:01:49 +0200897 mod_timer(&ifmsh->mesh_path_timer, ifmsh->last_preq +
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100898 min_preq_int_jiff(sdata));
899}
900
901/**
902 * mesh_path_start_discovery - launch a path discovery from the PREQ queue
903 *
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200904 * @sdata: local mesh subif
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100905 */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200906void mesh_path_start_discovery(struct ieee80211_sub_if_data *sdata)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100907{
Johannes Berg472dbc42008-09-11 00:01:49 +0200908 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100909 struct mesh_preq_queue *preq_node;
910 struct mesh_path *mpath;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000911 u8 ttl, target_flags;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100912 u32 lifetime;
913
Johannes Berga43816d2009-07-10 11:39:26 +0200914 spin_lock_bh(&ifmsh->mesh_preq_queue_lock);
Johannes Berg472dbc42008-09-11 00:01:49 +0200915 if (!ifmsh->preq_queue_len ||
916 time_before(jiffies, ifmsh->last_preq +
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100917 min_preq_int_jiff(sdata))) {
Johannes Berga43816d2009-07-10 11:39:26 +0200918 spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100919 return;
920 }
921
Johannes Berg472dbc42008-09-11 00:01:49 +0200922 preq_node = list_first_entry(&ifmsh->preq_queue.list,
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100923 struct mesh_preq_queue, list);
924 list_del(&preq_node->list);
Johannes Berg472dbc42008-09-11 00:01:49 +0200925 --ifmsh->preq_queue_len;
Johannes Berga43816d2009-07-10 11:39:26 +0200926 spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100927
928 rcu_read_lock();
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200929 mpath = mesh_path_lookup(preq_node->dst, sdata);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100930 if (!mpath)
931 goto enddiscovery;
932
933 spin_lock_bh(&mpath->state_lock);
Javier Cardonaf3011cf2011-11-03 21:11:10 -0700934 mpath->flags &= ~MESH_PATH_REQ_QUEUED;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100935 if (preq_node->flags & PREQ_Q_F_START) {
936 if (mpath->flags & MESH_PATH_RESOLVING) {
937 spin_unlock_bh(&mpath->state_lock);
938 goto enddiscovery;
939 } else {
940 mpath->flags &= ~MESH_PATH_RESOLVED;
941 mpath->flags |= MESH_PATH_RESOLVING;
942 mpath->discovery_retries = 0;
943 mpath->discovery_timeout = disc_timeout_jiff(sdata);
944 }
945 } else if (!(mpath->flags & MESH_PATH_RESOLVING) ||
946 mpath->flags & MESH_PATH_RESOLVED) {
947 mpath->flags &= ~MESH_PATH_RESOLVING;
948 spin_unlock_bh(&mpath->state_lock);
949 goto enddiscovery;
950 }
951
Johannes Berg472dbc42008-09-11 00:01:49 +0200952 ifmsh->last_preq = jiffies;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100953
Rui Paulod19b3bf2009-11-09 23:46:55 +0000954 if (time_after(jiffies, ifmsh->last_sn_update +
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100955 net_traversal_jiffies(sdata)) ||
Rui Paulod19b3bf2009-11-09 23:46:55 +0000956 time_before(jiffies, ifmsh->last_sn_update)) {
957 ++ifmsh->sn;
958 sdata->u.mesh.last_sn_update = jiffies;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100959 }
960 lifetime = default_lifetime(sdata);
Javier Cardona45904f22010-12-03 09:20:40 +0100961 ttl = sdata->u.mesh.mshcfg.element_ttl;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100962 if (ttl == 0) {
Johannes Berg472dbc42008-09-11 00:01:49 +0200963 sdata->u.mesh.mshstats.dropped_frames_ttl++;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100964 spin_unlock_bh(&mpath->state_lock);
965 goto enddiscovery;
966 }
967
968 if (preq_node->flags & PREQ_Q_F_REFRESH)
Rui Paulod19b3bf2009-11-09 23:46:55 +0000969 target_flags = MP_F_DO;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100970 else
Rui Paulod19b3bf2009-11-09 23:46:55 +0000971 target_flags = MP_F_RF;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100972
973 spin_unlock_bh(&mpath->state_lock);
Johannes Berg47846c92009-11-25 17:46:19 +0100974 mesh_path_sel_frame_tx(MPATH_PREQ, 0, sdata->vif.addr,
Rui Paulod19b3bf2009-11-09 23:46:55 +0000975 cpu_to_le32(ifmsh->sn), target_flags, mpath->dst,
Johannes Berg15ff6362009-11-17 13:34:04 +0100976 cpu_to_le32(mpath->sn), broadcast_addr, 0,
Luis Carlos Coboaa2b5922008-02-29 14:30:32 -0800977 ttl, cpu_to_le32(lifetime), 0,
Johannes Berg472dbc42008-09-11 00:01:49 +0200978 cpu_to_le32(ifmsh->preq_id++), sdata);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100979 mod_timer(&mpath->timer, jiffies + mpath->discovery_timeout);
980
981enddiscovery:
982 rcu_read_unlock();
983 kfree(preq_node);
984}
985
Thomas Pedersen0cfda852011-11-24 17:15:25 -0800986/* mesh_nexthop_resolve - lookup next hop for given skb and start path
987 * discovery if no forwarding information is found.
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100988 *
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +0200989 * @skb: 802.11 frame to be sent
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200990 * @sdata: network subif the frame will be sent through
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100991 *
Thomas Pedersen0cfda852011-11-24 17:15:25 -0800992 * Returns: 0 if the next hop was found and -ENOENT if the frame was queued.
993 * skb is freeed here if no mpath could be allocated.
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100994 */
Thomas Pedersen0cfda852011-11-24 17:15:25 -0800995int mesh_nexthop_resolve(struct sk_buff *skb,
996 struct ieee80211_sub_if_data *sdata)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100997{
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +0200998 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
Thomas Pedersen0cfda852011-11-24 17:15:25 -0800999 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1000 struct mesh_path *mpath;
1001 struct sk_buff *skb_to_free = NULL;
Rui Paulod19b3bf2009-11-09 23:46:55 +00001002 u8 *target_addr = hdr->addr3;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001003 int err = 0;
1004
1005 rcu_read_lock();
Thomas Pedersen0cfda852011-11-24 17:15:25 -08001006 err = mesh_nexthop_lookup(skb, sdata);
1007 if (!err)
1008 goto endlookup;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001009
Thomas Pedersen0cfda852011-11-24 17:15:25 -08001010 /* no nexthop found, start resolving */
1011 mpath = mesh_path_lookup(target_addr, sdata);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001012 if (!mpath) {
Rui Paulod19b3bf2009-11-09 23:46:55 +00001013 mesh_path_add(target_addr, sdata);
1014 mpath = mesh_path_lookup(target_addr, sdata);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001015 if (!mpath) {
Thomas Pedersen0cfda852011-11-24 17:15:25 -08001016 mesh_path_discard_frame(skb, sdata);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001017 err = -ENOSPC;
1018 goto endlookup;
1019 }
1020 }
1021
Thomas Pedersen0cfda852011-11-24 17:15:25 -08001022 if (!(mpath->flags & MESH_PATH_RESOLVING))
1023 mesh_queue_preq(mpath, PREQ_Q_F_START);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001024
Thomas Pedersen0cfda852011-11-24 17:15:25 -08001025 if (skb_queue_len(&mpath->frame_queue) >= MESH_FRAME_QUEUE_LEN)
1026 skb_to_free = skb_dequeue(&mpath->frame_queue);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001027
Thomas Pedersen0cfda852011-11-24 17:15:25 -08001028 info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
1029 ieee80211_set_qos_hdr(sdata, skb);
1030 skb_queue_tail(&mpath->frame_queue, skb);
1031 err = -ENOENT;
1032 if (skb_to_free)
1033 mesh_path_discard_frame(skb_to_free, sdata);
1034
1035endlookup:
1036 rcu_read_unlock();
1037 return err;
1038}
1039/**
1040 * mesh_nexthop_lookup - put the appropriate next hop on a mesh frame. Calling
1041 * this function is considered "using" the associated mpath, so preempt a path
1042 * refresh if this mpath expires soon.
1043 *
1044 * @skb: 802.11 frame to be sent
1045 * @sdata: network subif the frame will be sent through
1046 *
1047 * Returns: 0 if the next hop was found. Nonzero otherwise.
1048 */
1049int mesh_nexthop_lookup(struct sk_buff *skb,
1050 struct ieee80211_sub_if_data *sdata)
1051{
1052 struct mesh_path *mpath;
1053 struct sta_info *next_hop;
1054 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1055 u8 *target_addr = hdr->addr3;
1056 int err = -ENOENT;
1057
1058 rcu_read_lock();
1059 mpath = mesh_path_lookup(target_addr, sdata);
1060
1061 if (!mpath || !(mpath->flags & MESH_PATH_ACTIVE))
1062 goto endlookup;
1063
1064 if (time_after(jiffies,
1065 mpath->exp_time -
1066 msecs_to_jiffies(sdata->u.mesh.mshcfg.path_refresh_time)) &&
1067 !memcmp(sdata->vif.addr, hdr->addr4, ETH_ALEN) &&
1068 !(mpath->flags & MESH_PATH_RESOLVING) &&
1069 !(mpath->flags & MESH_PATH_FIXED))
1070 mesh_queue_preq(mpath, PREQ_Q_F_START | PREQ_Q_F_REFRESH);
1071
1072 next_hop = rcu_dereference(mpath->next_hop);
1073 if (next_hop) {
1074 memcpy(hdr->addr1, next_hop->sta.addr, ETH_ALEN);
1075 memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN);
1076 err = 0;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001077 }
1078
1079endlookup:
1080 rcu_read_unlock();
1081 return err;
1082}
1083
1084void mesh_path_timer(unsigned long data)
1085{
Johannes Bergdea40962011-05-12 15:03:32 +02001086 struct mesh_path *mpath = (void *) data;
1087 struct ieee80211_sub_if_data *sdata = mpath->sdata;
Javier Cardona5ee68e52011-08-09 16:45:08 -07001088 int ret;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001089
Johannes Bergdea40962011-05-12 15:03:32 +02001090 if (sdata->local->quiescing)
Johannes Berg5bb644a2009-05-17 11:40:42 +02001091 return;
Johannes Berg5bb644a2009-05-17 11:40:42 +02001092
1093 spin_lock_bh(&mpath->state_lock);
Luis Carlos Cobocfa22c72008-02-29 15:04:13 -08001094 if (mpath->flags & MESH_PATH_RESOLVED ||
Javier Cardona5ee68e52011-08-09 16:45:08 -07001095 (!(mpath->flags & MESH_PATH_RESOLVING))) {
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001096 mpath->flags &= ~(MESH_PATH_RESOLVING | MESH_PATH_RESOLVED);
Javier Cardona5ee68e52011-08-09 16:45:08 -07001097 spin_unlock_bh(&mpath->state_lock);
1098 } else if (mpath->discovery_retries < max_preq_retries(sdata)) {
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001099 ++mpath->discovery_retries;
1100 mpath->discovery_timeout *= 2;
Javier Cardonaf3011cf2011-11-03 21:11:10 -07001101 mpath->flags &= ~MESH_PATH_REQ_QUEUED;
Javier Cardona5ee68e52011-08-09 16:45:08 -07001102 spin_unlock_bh(&mpath->state_lock);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001103 mesh_queue_preq(mpath, 0);
1104 } else {
1105 mpath->flags = 0;
1106 mpath->exp_time = jiffies;
Javier Cardona5ee68e52011-08-09 16:45:08 -07001107 spin_unlock_bh(&mpath->state_lock);
1108 if (!mpath->is_gate && mesh_gate_num(sdata) > 0) {
1109 ret = mesh_path_send_to_gates(mpath);
1110 if (ret)
1111 mhwmp_dbg("no gate was reachable");
1112 } else
1113 mesh_path_flush_pending(mpath);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001114 }
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001115}
Rui Pauloe304bfd2009-11-09 23:46:56 +00001116
1117void
1118mesh_path_tx_root_frame(struct ieee80211_sub_if_data *sdata)
1119{
1120 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
Javier Cardona0507e152011-08-09 16:45:10 -07001121 u32 interval = ifmsh->mshcfg.dot11MeshHWMPRannInterval;
Javier Cardona16dd7262011-08-09 16:45:11 -07001122 u8 flags;
Rui Pauloe304bfd2009-11-09 23:46:56 +00001123
Javier Cardona16dd7262011-08-09 16:45:11 -07001124 flags = (ifmsh->mshcfg.dot11MeshGateAnnouncementProtocol)
1125 ? RANN_FLAG_IS_GATE : 0;
1126 mesh_path_sel_frame_tx(MPATH_RANN, flags, sdata->vif.addr,
Rui Pauloe304bfd2009-11-09 23:46:56 +00001127 cpu_to_le32(++ifmsh->sn),
Johannes Berg15ff6362009-11-17 13:34:04 +01001128 0, NULL, 0, broadcast_addr,
Javier Cardona45904f22010-12-03 09:20:40 +01001129 0, sdata->u.mesh.mshcfg.element_ttl,
Javier Cardona0507e152011-08-09 16:45:10 -07001130 cpu_to_le32(interval), 0, 0, sdata);
Rui Pauloe304bfd2009-11-09 23:46:56 +00001131}