blob: 639db14f43d241d0e3e5b1d719c7ffece0d603af [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;
Chun-Yeow Yeoh3d045a52012-02-28 22:00:06 +0800516 struct mesh_path *mpath = NULL;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000517 u8 *target_addr, *orig_addr;
Chun-Yeow Yeoh3d045a52012-02-28 22:00:06 +0800518 const u8 *da;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000519 u8 target_flags, ttl;
520 u32 orig_sn, target_sn, lifetime;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100521 bool reply = false;
522 bool forward = true;
523
Rui Paulod19b3bf2009-11-09 23:46:55 +0000524 /* Update target SN, if present */
525 target_addr = PREQ_IE_TARGET_ADDR(preq_elem);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100526 orig_addr = PREQ_IE_ORIG_ADDR(preq_elem);
Rui Paulod19b3bf2009-11-09 23:46:55 +0000527 target_sn = PREQ_IE_TARGET_SN(preq_elem);
528 orig_sn = PREQ_IE_ORIG_SN(preq_elem);
529 target_flags = PREQ_IE_TARGET_F(preq_elem);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100530
Javier Cardona76468872011-08-09 16:45:04 -0700531 mhwmp_dbg("received PREQ from %pM", orig_addr);
Rui Paulo27db2e42009-11-09 23:46:45 +0000532
Johannes Berg47846c92009-11-25 17:46:19 +0100533 if (memcmp(target_addr, sdata->vif.addr, ETH_ALEN) == 0) {
Javier Cardona76468872011-08-09 16:45:04 -0700534 mhwmp_dbg("PREQ is for us");
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100535 forward = false;
536 reply = true;
537 metric = 0;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000538 if (time_after(jiffies, ifmsh->last_sn_update +
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100539 net_traversal_jiffies(sdata)) ||
Rui Paulod19b3bf2009-11-09 23:46:55 +0000540 time_before(jiffies, ifmsh->last_sn_update)) {
541 target_sn = ++ifmsh->sn;
542 ifmsh->last_sn_update = jiffies;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100543 }
544 } else {
545 rcu_read_lock();
Rui Paulod19b3bf2009-11-09 23:46:55 +0000546 mpath = mesh_path_lookup(target_addr, sdata);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100547 if (mpath) {
Rui Paulod19b3bf2009-11-09 23:46:55 +0000548 if ((!(mpath->flags & MESH_PATH_SN_VALID)) ||
549 SN_LT(mpath->sn, target_sn)) {
550 mpath->sn = target_sn;
551 mpath->flags |= MESH_PATH_SN_VALID;
552 } else if ((!(target_flags & MP_F_DO)) &&
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100553 (mpath->flags & MESH_PATH_ACTIVE)) {
554 reply = true;
555 metric = mpath->metric;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000556 target_sn = mpath->sn;
557 if (target_flags & MP_F_RF)
558 target_flags |= MP_F_DO;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100559 else
560 forward = false;
561 }
562 }
563 rcu_read_unlock();
564 }
565
566 if (reply) {
567 lifetime = PREQ_IE_LIFETIME(preq_elem);
Javier Cardona45904f22010-12-03 09:20:40 +0100568 ttl = ifmsh->mshcfg.element_ttl;
Rui Paulo27db2e42009-11-09 23:46:45 +0000569 if (ttl != 0) {
Javier Cardona76468872011-08-09 16:45:04 -0700570 mhwmp_dbg("replying to the PREQ");
Thomas Pedersen3c26f1f2011-11-24 17:15:22 -0800571 mesh_path_sel_frame_tx(MPATH_PREP, 0, orig_addr,
572 cpu_to_le32(orig_sn), 0, target_addr,
573 cpu_to_le32(target_sn), mgmt->sa, 0, ttl,
Luis Carlos Coboaa2b5922008-02-29 14:30:32 -0800574 cpu_to_le32(lifetime), cpu_to_le32(metric),
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200575 0, sdata);
Rui Paulo27db2e42009-11-09 23:46:45 +0000576 } else
Johannes Berg472dbc42008-09-11 00:01:49 +0200577 ifmsh->mshstats.dropped_frames_ttl++;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100578 }
579
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +0800580 if (forward && ifmsh->mshcfg.dot11MeshForwarding) {
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100581 u32 preq_id;
582 u8 hopcount, flags;
583
584 ttl = PREQ_IE_TTL(preq_elem);
585 lifetime = PREQ_IE_LIFETIME(preq_elem);
586 if (ttl <= 1) {
Johannes Berg472dbc42008-09-11 00:01:49 +0200587 ifmsh->mshstats.dropped_frames_ttl++;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100588 return;
589 }
Javier Cardona76468872011-08-09 16:45:04 -0700590 mhwmp_dbg("forwarding the PREQ from %pM", orig_addr);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100591 --ttl;
592 flags = PREQ_IE_FLAGS(preq_elem);
593 preq_id = PREQ_IE_PREQ_ID(preq_elem);
594 hopcount = PREQ_IE_HOPCOUNT(preq_elem) + 1;
Chun-Yeow Yeoh3d045a52012-02-28 22:00:06 +0800595 da = (mpath && mpath->is_root) ?
596 mpath->rann_snd_addr : broadcast_addr;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100597 mesh_path_sel_frame_tx(MPATH_PREQ, flags, orig_addr,
Rui Paulod19b3bf2009-11-09 23:46:55 +0000598 cpu_to_le32(orig_sn), target_flags, target_addr,
Chun-Yeow Yeoh3d045a52012-02-28 22:00:06 +0800599 cpu_to_le32(target_sn), da,
Luis Carlos Coboaa2b5922008-02-29 14:30:32 -0800600 hopcount, ttl, cpu_to_le32(lifetime),
601 cpu_to_le32(metric), cpu_to_le32(preq_id),
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200602 sdata);
Daniel Walkerc8a61a72009-08-18 10:59:00 -0700603 ifmsh->mshstats.fwded_mcast++;
Johannes Berg472dbc42008-09-11 00:01:49 +0200604 ifmsh->mshstats.fwded_frames++;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100605 }
606}
607
608
Johannes Berg40b275b2011-05-13 14:15:49 +0200609static inline struct sta_info *
610next_hop_deref_protected(struct mesh_path *mpath)
611{
612 return rcu_dereference_protected(mpath->next_hop,
613 lockdep_is_held(&mpath->state_lock));
614}
615
616
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200617static void hwmp_prep_frame_process(struct ieee80211_sub_if_data *sdata,
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100618 struct ieee80211_mgmt *mgmt,
619 u8 *prep_elem, u32 metric)
620{
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100621 struct mesh_path *mpath;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000622 u8 *target_addr, *orig_addr;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100623 u8 ttl, hopcount, flags;
624 u8 next_hop[ETH_ALEN];
Rui Paulod19b3bf2009-11-09 23:46:55 +0000625 u32 target_sn, orig_sn, lifetime;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100626
Javier Cardona76468872011-08-09 16:45:04 -0700627 mhwmp_dbg("received PREP from %pM", PREP_IE_ORIG_ADDR(prep_elem));
Rui Paulodbb81c42009-11-09 23:46:46 +0000628
Thomas Pedersen3c26f1f2011-11-24 17:15:22 -0800629 orig_addr = PREP_IE_ORIG_ADDR(prep_elem);
630 if (memcmp(orig_addr, sdata->vif.addr, ETH_ALEN) == 0)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100631 /* destination, no forwarding required */
632 return;
633
634 ttl = PREP_IE_TTL(prep_elem);
635 if (ttl <= 1) {
Johannes Berg472dbc42008-09-11 00:01:49 +0200636 sdata->u.mesh.mshstats.dropped_frames_ttl++;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100637 return;
638 }
639
640 rcu_read_lock();
Thomas Pedersen3c26f1f2011-11-24 17:15:22 -0800641 mpath = mesh_path_lookup(orig_addr, sdata);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100642 if (mpath)
643 spin_lock_bh(&mpath->state_lock);
644 else
645 goto fail;
646 if (!(mpath->flags & MESH_PATH_ACTIVE)) {
647 spin_unlock_bh(&mpath->state_lock);
648 goto fail;
649 }
Johannes Berg40b275b2011-05-13 14:15:49 +0200650 memcpy(next_hop, next_hop_deref_protected(mpath)->sta.addr, ETH_ALEN);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100651 spin_unlock_bh(&mpath->state_lock);
652 --ttl;
653 flags = PREP_IE_FLAGS(prep_elem);
654 lifetime = PREP_IE_LIFETIME(prep_elem);
655 hopcount = PREP_IE_HOPCOUNT(prep_elem) + 1;
Thomas Pedersen3c26f1f2011-11-24 17:15:22 -0800656 target_addr = PREP_IE_TARGET_ADDR(prep_elem);
Rui Paulod19b3bf2009-11-09 23:46:55 +0000657 target_sn = PREP_IE_TARGET_SN(prep_elem);
658 orig_sn = PREP_IE_ORIG_SN(prep_elem);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100659
660 mesh_path_sel_frame_tx(MPATH_PREP, flags, orig_addr,
Rui Paulod19b3bf2009-11-09 23:46:55 +0000661 cpu_to_le32(orig_sn), 0, target_addr,
Porsch, Marco533866b2010-02-24 09:53:13 +0100662 cpu_to_le32(target_sn), next_hop, hopcount,
Rui Paulod19b3bf2009-11-09 23:46:55 +0000663 ttl, cpu_to_le32(lifetime), cpu_to_le32(metric),
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200664 0, sdata);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100665 rcu_read_unlock();
Daniel Walkerc8a61a72009-08-18 10:59:00 -0700666
667 sdata->u.mesh.mshstats.fwded_unicast++;
Johannes Berg472dbc42008-09-11 00:01:49 +0200668 sdata->u.mesh.mshstats.fwded_frames++;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100669 return;
670
671fail:
672 rcu_read_unlock();
Johannes Berg472dbc42008-09-11 00:01:49 +0200673 sdata->u.mesh.mshstats.dropped_frames_no_route++;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100674}
675
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200676static void hwmp_perr_frame_process(struct ieee80211_sub_if_data *sdata,
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100677 struct ieee80211_mgmt *mgmt, u8 *perr_elem)
678{
Rui Paulod611f062009-11-09 23:46:50 +0000679 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100680 struct mesh_path *mpath;
Rui Paulod611f062009-11-09 23:46:50 +0000681 u8 ttl;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000682 u8 *ta, *target_addr;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000683 u32 target_sn;
684 u16 target_rcode;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100685
686 ta = mgmt->sa;
Rui Paulod611f062009-11-09 23:46:50 +0000687 ttl = PERR_IE_TTL(perr_elem);
688 if (ttl <= 1) {
689 ifmsh->mshstats.dropped_frames_ttl++;
690 return;
691 }
692 ttl--;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000693 target_addr = PERR_IE_TARGET_ADDR(perr_elem);
694 target_sn = PERR_IE_TARGET_SN(perr_elem);
695 target_rcode = PERR_IE_TARGET_RCODE(perr_elem);
Rui Paulod611f062009-11-09 23:46:50 +0000696
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100697 rcu_read_lock();
Rui Paulod19b3bf2009-11-09 23:46:55 +0000698 mpath = mesh_path_lookup(target_addr, sdata);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100699 if (mpath) {
700 spin_lock_bh(&mpath->state_lock);
701 if (mpath->flags & MESH_PATH_ACTIVE &&
Johannes Berg40b275b2011-05-13 14:15:49 +0200702 memcmp(ta, next_hop_deref_protected(mpath)->sta.addr,
703 ETH_ALEN) == 0 &&
Rui Paulod19b3bf2009-11-09 23:46:55 +0000704 (!(mpath->flags & MESH_PATH_SN_VALID) ||
705 SN_GT(target_sn, mpath->sn))) {
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100706 mpath->flags &= ~MESH_PATH_ACTIVE;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000707 mpath->sn = target_sn;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100708 spin_unlock_bh(&mpath->state_lock);
Rui Paulod19b3bf2009-11-09 23:46:55 +0000709 mesh_path_error_tx(ttl, target_addr, cpu_to_le32(target_sn),
710 cpu_to_le16(target_rcode),
Johannes Berg15ff6362009-11-17 13:34:04 +0100711 broadcast_addr, sdata);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100712 } else
713 spin_unlock_bh(&mpath->state_lock);
714 }
715 rcu_read_unlock();
716}
717
Rui Paulo90a5e162009-11-11 00:01:31 +0000718static void hwmp_rann_frame_process(struct ieee80211_sub_if_data *sdata,
719 struct ieee80211_mgmt *mgmt,
720 struct ieee80211_rann_ie *rann)
721{
722 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
723 struct mesh_path *mpath;
Rui Paulo90a5e162009-11-11 00:01:31 +0000724 u8 ttl, flags, hopcount;
725 u8 *orig_addr;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000726 u32 orig_sn, metric;
Javier Cardona0507e152011-08-09 16:45:10 -0700727 u32 interval = ifmsh->mshcfg.dot11MeshHWMPRannInterval;
Javier Cardona5ee68e52011-08-09 16:45:08 -0700728 bool root_is_gate;
Rui Paulo90a5e162009-11-11 00:01:31 +0000729
Rui Paulo90a5e162009-11-11 00:01:31 +0000730 ttl = rann->rann_ttl;
731 if (ttl <= 1) {
732 ifmsh->mshstats.dropped_frames_ttl++;
733 return;
734 }
735 ttl--;
736 flags = rann->rann_flags;
Javier Cardona5ee68e52011-08-09 16:45:08 -0700737 root_is_gate = !!(flags & RANN_FLAG_IS_GATE);
Rui Paulo90a5e162009-11-11 00:01:31 +0000738 orig_addr = rann->rann_addr;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000739 orig_sn = rann->rann_seq;
Rui Paulo90a5e162009-11-11 00:01:31 +0000740 hopcount = rann->rann_hopcount;
Rui Pauloa6a58b42009-11-09 23:46:51 +0000741 hopcount++;
Rui Paulo90a5e162009-11-11 00:01:31 +0000742 metric = rann->rann_metric;
Javier Cardona5ee68e52011-08-09 16:45:08 -0700743
744 /* Ignore our own RANNs */
745 if (memcmp(orig_addr, sdata->vif.addr, ETH_ALEN) == 0)
746 return;
747
Chun-Yeow Yeoh3d045a52012-02-28 22:00:06 +0800748 mhwmp_dbg("received RANN from %pM via neighbour %pM (is_gate=%d)",
749 orig_addr, mgmt->sa, root_is_gate);
Rui Paulo90a5e162009-11-11 00:01:31 +0000750
751 rcu_read_lock();
752 mpath = mesh_path_lookup(orig_addr, sdata);
753 if (!mpath) {
754 mesh_path_add(orig_addr, sdata);
755 mpath = mesh_path_lookup(orig_addr, sdata);
756 if (!mpath) {
757 rcu_read_unlock();
758 sdata->u.mesh.mshstats.dropped_frames_no_route++;
759 return;
760 }
Rui Paulo90a5e162009-11-11 00:01:31 +0000761 }
Javier Cardona5ee68e52011-08-09 16:45:08 -0700762
763 if ((!(mpath->flags & (MESH_PATH_ACTIVE | MESH_PATH_RESOLVING)) ||
764 time_after(jiffies, mpath->exp_time - 1*HZ)) &&
765 !(mpath->flags & MESH_PATH_FIXED)) {
766 mhwmp_dbg("%s time to refresh root mpath %pM", sdata->name,
767 orig_addr);
768 mesh_queue_preq(mpath, PREQ_Q_F_START | PREQ_Q_F_REFRESH);
769 }
770
Rui Paulod19b3bf2009-11-09 23:46:55 +0000771 if (mpath->sn < orig_sn) {
Rui Paulo90a5e162009-11-11 00:01:31 +0000772 mesh_path_sel_frame_tx(MPATH_RANN, flags, orig_addr,
Rui Paulod19b3bf2009-11-09 23:46:55 +0000773 cpu_to_le32(orig_sn),
Johannes Berg15ff6362009-11-17 13:34:04 +0100774 0, NULL, 0, broadcast_addr,
Javier Cardona0507e152011-08-09 16:45:10 -0700775 hopcount, ttl, cpu_to_le32(interval),
Rui Pauloa6a58b42009-11-09 23:46:51 +0000776 cpu_to_le32(metric + mpath->metric),
Rui Paulo90a5e162009-11-11 00:01:31 +0000777 0, sdata);
Rui Paulod19b3bf2009-11-09 23:46:55 +0000778 mpath->sn = orig_sn;
Rui Paulo90a5e162009-11-11 00:01:31 +0000779 }
Chun-Yeow Yeoh3d045a52012-02-28 22:00:06 +0800780
781 /* Using individually addressed PREQ for root node */
782 memcpy(mpath->rann_snd_addr, mgmt->sa, ETH_ALEN);
783 mpath->is_root = true;
784
Javier Cardona5ee68e52011-08-09 16:45:08 -0700785 if (root_is_gate)
786 mesh_path_add_gate(mpath);
787
Rui Paulo90a5e162009-11-11 00:01:31 +0000788 rcu_read_unlock();
789}
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100790
791
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200792void mesh_rx_path_sel_frame(struct ieee80211_sub_if_data *sdata,
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100793 struct ieee80211_mgmt *mgmt,
794 size_t len)
795{
796 struct ieee802_11_elems elems;
797 size_t baselen;
798 u32 last_hop_metric;
Javier Cardona97091312011-10-06 14:54:22 -0700799 struct sta_info *sta;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100800
Johannes Berg9c80d3d2008-09-08 15:41:59 +0200801 /* need action_code */
802 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
803 return;
804
Javier Cardona97091312011-10-06 14:54:22 -0700805 rcu_read_lock();
806 sta = sta_info_get(sdata, mgmt->sa);
807 if (!sta || sta->plink_state != NL80211_PLINK_ESTAB) {
808 rcu_read_unlock();
809 return;
810 }
811 rcu_read_unlock();
812
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100813 baselen = (u8 *) mgmt->u.action.u.mesh_action.variable - (u8 *) mgmt;
814 ieee802_11_parse_elems(mgmt->u.action.u.mesh_action.variable,
815 len - baselen, &elems);
816
Rui Paulodbb81c42009-11-09 23:46:46 +0000817 if (elems.preq) {
818 if (elems.preq_len != 37)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100819 /* Right now we support just 1 destination and no AE */
820 return;
Rui Paulodbb81c42009-11-09 23:46:46 +0000821 last_hop_metric = hwmp_route_info_get(sdata, mgmt, elems.preq,
822 MPATH_PREQ);
823 if (last_hop_metric)
824 hwmp_preq_frame_process(sdata, mgmt, elems.preq,
825 last_hop_metric);
826 }
827 if (elems.prep) {
828 if (elems.prep_len != 31)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100829 /* Right now we support no AE */
830 return;
Rui Paulodbb81c42009-11-09 23:46:46 +0000831 last_hop_metric = hwmp_route_info_get(sdata, mgmt, elems.prep,
832 MPATH_PREP);
833 if (last_hop_metric)
834 hwmp_prep_frame_process(sdata, mgmt, elems.prep,
835 last_hop_metric);
836 }
837 if (elems.perr) {
Rui Paulod611f062009-11-09 23:46:50 +0000838 if (elems.perr_len != 15)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100839 /* Right now we support only one destination per PERR */
840 return;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200841 hwmp_perr_frame_process(sdata, mgmt, elems.perr);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100842 }
Rui Paulo90a5e162009-11-11 00:01:31 +0000843 if (elems.rann)
844 hwmp_rann_frame_process(sdata, mgmt, elems.rann);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100845}
846
847/**
848 * mesh_queue_preq - queue a PREQ to a given destination
849 *
850 * @mpath: mesh path to discover
851 * @flags: special attributes of the PREQ to be sent
852 *
853 * Locking: the function must be called from within a rcu read lock block.
854 *
855 */
856static void mesh_queue_preq(struct mesh_path *mpath, u8 flags)
857{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200858 struct ieee80211_sub_if_data *sdata = mpath->sdata;
Johannes Berg472dbc42008-09-11 00:01:49 +0200859 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100860 struct mesh_preq_queue *preq_node;
861
Andrey Yurovsky59615b52009-06-25 16:07:42 -0700862 preq_node = kmalloc(sizeof(struct mesh_preq_queue), GFP_ATOMIC);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100863 if (!preq_node) {
Javier Cardona76468872011-08-09 16:45:04 -0700864 mhwmp_dbg("could not allocate PREQ node");
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100865 return;
866 }
867
Baruch Siach987dafa2011-07-28 08:51:05 +0300868 spin_lock_bh(&ifmsh->mesh_preq_queue_lock);
Johannes Berg472dbc42008-09-11 00:01:49 +0200869 if (ifmsh->preq_queue_len == MAX_PREQ_QUEUE_LEN) {
Baruch Siach987dafa2011-07-28 08:51:05 +0300870 spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100871 kfree(preq_node);
872 if (printk_ratelimit())
Javier Cardona76468872011-08-09 16:45:04 -0700873 mhwmp_dbg("PREQ node queue full");
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100874 return;
875 }
876
Johannes Bergf2dc7982011-11-18 15:27:31 +0100877 spin_lock(&mpath->state_lock);
Javier Cardonaf3011cf2011-11-03 21:11:10 -0700878 if (mpath->flags & MESH_PATH_REQ_QUEUED) {
Johannes Bergf2dc7982011-11-18 15:27:31 +0100879 spin_unlock(&mpath->state_lock);
Javier Cardonaf3011cf2011-11-03 21:11:10 -0700880 spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
Dan Carpenter88d53462011-11-15 09:33:31 +0300881 kfree(preq_node);
Javier Cardonaf3011cf2011-11-03 21:11:10 -0700882 return;
883 }
884
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100885 memcpy(preq_node->dst, mpath->dst, ETH_ALEN);
886 preq_node->flags = flags;
887
Javier Cardonaf3011cf2011-11-03 21:11:10 -0700888 mpath->flags |= MESH_PATH_REQ_QUEUED;
Johannes Bergf2dc7982011-11-18 15:27:31 +0100889 spin_unlock(&mpath->state_lock);
Javier Cardonaf3011cf2011-11-03 21:11:10 -0700890
Johannes Berg472dbc42008-09-11 00:01:49 +0200891 list_add_tail(&preq_node->list, &ifmsh->preq_queue.list);
892 ++ifmsh->preq_queue_len;
Baruch Siach987dafa2011-07-28 08:51:05 +0300893 spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100894
Johannes Berg472dbc42008-09-11 00:01:49 +0200895 if (time_after(jiffies, ifmsh->last_preq + min_preq_int_jiff(sdata)))
Johannes Berg64592c82010-06-10 10:21:31 +0200896 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100897
Johannes Berg472dbc42008-09-11 00:01:49 +0200898 else if (time_before(jiffies, ifmsh->last_preq)) {
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100899 /* avoid long wait if did not send preqs for a long time
900 * and jiffies wrapped around
901 */
Johannes Berg472dbc42008-09-11 00:01:49 +0200902 ifmsh->last_preq = jiffies - min_preq_int_jiff(sdata) - 1;
Johannes Berg64592c82010-06-10 10:21:31 +0200903 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100904 } else
Johannes Berg472dbc42008-09-11 00:01:49 +0200905 mod_timer(&ifmsh->mesh_path_timer, ifmsh->last_preq +
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100906 min_preq_int_jiff(sdata));
907}
908
909/**
910 * mesh_path_start_discovery - launch a path discovery from the PREQ queue
911 *
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200912 * @sdata: local mesh subif
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100913 */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200914void mesh_path_start_discovery(struct ieee80211_sub_if_data *sdata)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100915{
Johannes Berg472dbc42008-09-11 00:01:49 +0200916 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100917 struct mesh_preq_queue *preq_node;
918 struct mesh_path *mpath;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000919 u8 ttl, target_flags;
Chun-Yeow Yeoh3d045a52012-02-28 22:00:06 +0800920 const u8 *da;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100921 u32 lifetime;
922
Johannes Berga43816d2009-07-10 11:39:26 +0200923 spin_lock_bh(&ifmsh->mesh_preq_queue_lock);
Johannes Berg472dbc42008-09-11 00:01:49 +0200924 if (!ifmsh->preq_queue_len ||
925 time_before(jiffies, ifmsh->last_preq +
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100926 min_preq_int_jiff(sdata))) {
Johannes Berga43816d2009-07-10 11:39:26 +0200927 spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100928 return;
929 }
930
Johannes Berg472dbc42008-09-11 00:01:49 +0200931 preq_node = list_first_entry(&ifmsh->preq_queue.list,
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100932 struct mesh_preq_queue, list);
933 list_del(&preq_node->list);
Johannes Berg472dbc42008-09-11 00:01:49 +0200934 --ifmsh->preq_queue_len;
Johannes Berga43816d2009-07-10 11:39:26 +0200935 spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100936
937 rcu_read_lock();
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200938 mpath = mesh_path_lookup(preq_node->dst, sdata);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100939 if (!mpath)
940 goto enddiscovery;
941
942 spin_lock_bh(&mpath->state_lock);
Javier Cardonaf3011cf2011-11-03 21:11:10 -0700943 mpath->flags &= ~MESH_PATH_REQ_QUEUED;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100944 if (preq_node->flags & PREQ_Q_F_START) {
945 if (mpath->flags & MESH_PATH_RESOLVING) {
946 spin_unlock_bh(&mpath->state_lock);
947 goto enddiscovery;
948 } else {
949 mpath->flags &= ~MESH_PATH_RESOLVED;
950 mpath->flags |= MESH_PATH_RESOLVING;
951 mpath->discovery_retries = 0;
952 mpath->discovery_timeout = disc_timeout_jiff(sdata);
953 }
954 } else if (!(mpath->flags & MESH_PATH_RESOLVING) ||
955 mpath->flags & MESH_PATH_RESOLVED) {
956 mpath->flags &= ~MESH_PATH_RESOLVING;
957 spin_unlock_bh(&mpath->state_lock);
958 goto enddiscovery;
959 }
960
Johannes Berg472dbc42008-09-11 00:01:49 +0200961 ifmsh->last_preq = jiffies;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100962
Rui Paulod19b3bf2009-11-09 23:46:55 +0000963 if (time_after(jiffies, ifmsh->last_sn_update +
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100964 net_traversal_jiffies(sdata)) ||
Rui Paulod19b3bf2009-11-09 23:46:55 +0000965 time_before(jiffies, ifmsh->last_sn_update)) {
966 ++ifmsh->sn;
967 sdata->u.mesh.last_sn_update = jiffies;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100968 }
969 lifetime = default_lifetime(sdata);
Javier Cardona45904f22010-12-03 09:20:40 +0100970 ttl = sdata->u.mesh.mshcfg.element_ttl;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100971 if (ttl == 0) {
Johannes Berg472dbc42008-09-11 00:01:49 +0200972 sdata->u.mesh.mshstats.dropped_frames_ttl++;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100973 spin_unlock_bh(&mpath->state_lock);
974 goto enddiscovery;
975 }
976
977 if (preq_node->flags & PREQ_Q_F_REFRESH)
Rui Paulod19b3bf2009-11-09 23:46:55 +0000978 target_flags = MP_F_DO;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100979 else
Rui Paulod19b3bf2009-11-09 23:46:55 +0000980 target_flags = MP_F_RF;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100981
982 spin_unlock_bh(&mpath->state_lock);
Chun-Yeow Yeoh3d045a52012-02-28 22:00:06 +0800983 da = (mpath->is_root) ? mpath->rann_snd_addr : broadcast_addr;
Johannes Berg47846c92009-11-25 17:46:19 +0100984 mesh_path_sel_frame_tx(MPATH_PREQ, 0, sdata->vif.addr,
Rui Paulod19b3bf2009-11-09 23:46:55 +0000985 cpu_to_le32(ifmsh->sn), target_flags, mpath->dst,
Chun-Yeow Yeoh3d045a52012-02-28 22:00:06 +0800986 cpu_to_le32(mpath->sn), da, 0,
Luis Carlos Coboaa2b5922008-02-29 14:30:32 -0800987 ttl, cpu_to_le32(lifetime), 0,
Johannes Berg472dbc42008-09-11 00:01:49 +0200988 cpu_to_le32(ifmsh->preq_id++), sdata);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100989 mod_timer(&mpath->timer, jiffies + mpath->discovery_timeout);
990
991enddiscovery:
992 rcu_read_unlock();
993 kfree(preq_node);
994}
995
Thomas Pedersen0cfda852011-11-24 17:15:25 -0800996/* mesh_nexthop_resolve - lookup next hop for given skb and start path
997 * discovery if no forwarding information is found.
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100998 *
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +0200999 * @skb: 802.11 frame to be sent
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001000 * @sdata: network subif the frame will be sent through
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001001 *
Thomas Pedersen0cfda852011-11-24 17:15:25 -08001002 * Returns: 0 if the next hop was found and -ENOENT if the frame was queued.
1003 * skb is freeed here if no mpath could be allocated.
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001004 */
Thomas Pedersen0cfda852011-11-24 17:15:25 -08001005int mesh_nexthop_resolve(struct sk_buff *skb,
1006 struct ieee80211_sub_if_data *sdata)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001007{
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02001008 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
Thomas Pedersen0cfda852011-11-24 17:15:25 -08001009 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1010 struct mesh_path *mpath;
1011 struct sk_buff *skb_to_free = NULL;
Rui Paulod19b3bf2009-11-09 23:46:55 +00001012 u8 *target_addr = hdr->addr3;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001013 int err = 0;
1014
1015 rcu_read_lock();
Thomas Pedersen0cfda852011-11-24 17:15:25 -08001016 err = mesh_nexthop_lookup(skb, sdata);
1017 if (!err)
1018 goto endlookup;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001019
Thomas Pedersen0cfda852011-11-24 17:15:25 -08001020 /* no nexthop found, start resolving */
1021 mpath = mesh_path_lookup(target_addr, sdata);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001022 if (!mpath) {
Rui Paulod19b3bf2009-11-09 23:46:55 +00001023 mesh_path_add(target_addr, sdata);
1024 mpath = mesh_path_lookup(target_addr, sdata);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001025 if (!mpath) {
Thomas Pedersen0cfda852011-11-24 17:15:25 -08001026 mesh_path_discard_frame(skb, sdata);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001027 err = -ENOSPC;
1028 goto endlookup;
1029 }
1030 }
1031
Thomas Pedersen0cfda852011-11-24 17:15:25 -08001032 if (!(mpath->flags & MESH_PATH_RESOLVING))
1033 mesh_queue_preq(mpath, PREQ_Q_F_START);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001034
Thomas Pedersen0cfda852011-11-24 17:15:25 -08001035 if (skb_queue_len(&mpath->frame_queue) >= MESH_FRAME_QUEUE_LEN)
1036 skb_to_free = skb_dequeue(&mpath->frame_queue);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001037
Thomas Pedersen0cfda852011-11-24 17:15:25 -08001038 info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
1039 ieee80211_set_qos_hdr(sdata, skb);
1040 skb_queue_tail(&mpath->frame_queue, skb);
1041 err = -ENOENT;
1042 if (skb_to_free)
1043 mesh_path_discard_frame(skb_to_free, sdata);
1044
1045endlookup:
1046 rcu_read_unlock();
1047 return err;
1048}
1049/**
1050 * mesh_nexthop_lookup - put the appropriate next hop on a mesh frame. Calling
1051 * this function is considered "using" the associated mpath, so preempt a path
1052 * refresh if this mpath expires soon.
1053 *
1054 * @skb: 802.11 frame to be sent
1055 * @sdata: network subif the frame will be sent through
1056 *
1057 * Returns: 0 if the next hop was found. Nonzero otherwise.
1058 */
1059int mesh_nexthop_lookup(struct sk_buff *skb,
1060 struct ieee80211_sub_if_data *sdata)
1061{
1062 struct mesh_path *mpath;
1063 struct sta_info *next_hop;
1064 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1065 u8 *target_addr = hdr->addr3;
1066 int err = -ENOENT;
1067
1068 rcu_read_lock();
1069 mpath = mesh_path_lookup(target_addr, sdata);
1070
1071 if (!mpath || !(mpath->flags & MESH_PATH_ACTIVE))
1072 goto endlookup;
1073
1074 if (time_after(jiffies,
1075 mpath->exp_time -
1076 msecs_to_jiffies(sdata->u.mesh.mshcfg.path_refresh_time)) &&
1077 !memcmp(sdata->vif.addr, hdr->addr4, ETH_ALEN) &&
1078 !(mpath->flags & MESH_PATH_RESOLVING) &&
1079 !(mpath->flags & MESH_PATH_FIXED))
1080 mesh_queue_preq(mpath, PREQ_Q_F_START | PREQ_Q_F_REFRESH);
1081
1082 next_hop = rcu_dereference(mpath->next_hop);
1083 if (next_hop) {
1084 memcpy(hdr->addr1, next_hop->sta.addr, ETH_ALEN);
1085 memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN);
1086 err = 0;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001087 }
1088
1089endlookup:
1090 rcu_read_unlock();
1091 return err;
1092}
1093
1094void mesh_path_timer(unsigned long data)
1095{
Johannes Bergdea40962011-05-12 15:03:32 +02001096 struct mesh_path *mpath = (void *) data;
1097 struct ieee80211_sub_if_data *sdata = mpath->sdata;
Javier Cardona5ee68e52011-08-09 16:45:08 -07001098 int ret;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001099
Johannes Bergdea40962011-05-12 15:03:32 +02001100 if (sdata->local->quiescing)
Johannes Berg5bb644a2009-05-17 11:40:42 +02001101 return;
Johannes Berg5bb644a2009-05-17 11:40:42 +02001102
1103 spin_lock_bh(&mpath->state_lock);
Luis Carlos Cobocfa22c72008-02-29 15:04:13 -08001104 if (mpath->flags & MESH_PATH_RESOLVED ||
Javier Cardona5ee68e52011-08-09 16:45:08 -07001105 (!(mpath->flags & MESH_PATH_RESOLVING))) {
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001106 mpath->flags &= ~(MESH_PATH_RESOLVING | MESH_PATH_RESOLVED);
Javier Cardona5ee68e52011-08-09 16:45:08 -07001107 spin_unlock_bh(&mpath->state_lock);
1108 } else if (mpath->discovery_retries < max_preq_retries(sdata)) {
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001109 ++mpath->discovery_retries;
1110 mpath->discovery_timeout *= 2;
Javier Cardonaf3011cf2011-11-03 21:11:10 -07001111 mpath->flags &= ~MESH_PATH_REQ_QUEUED;
Javier Cardona5ee68e52011-08-09 16:45:08 -07001112 spin_unlock_bh(&mpath->state_lock);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001113 mesh_queue_preq(mpath, 0);
1114 } else {
1115 mpath->flags = 0;
1116 mpath->exp_time = jiffies;
Javier Cardona5ee68e52011-08-09 16:45:08 -07001117 spin_unlock_bh(&mpath->state_lock);
1118 if (!mpath->is_gate && mesh_gate_num(sdata) > 0) {
1119 ret = mesh_path_send_to_gates(mpath);
1120 if (ret)
1121 mhwmp_dbg("no gate was reachable");
1122 } else
1123 mesh_path_flush_pending(mpath);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001124 }
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001125}
Rui Pauloe304bfd2009-11-09 23:46:56 +00001126
1127void
1128mesh_path_tx_root_frame(struct ieee80211_sub_if_data *sdata)
1129{
1130 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
Javier Cardona0507e152011-08-09 16:45:10 -07001131 u32 interval = ifmsh->mshcfg.dot11MeshHWMPRannInterval;
Javier Cardona16dd7262011-08-09 16:45:11 -07001132 u8 flags;
Rui Pauloe304bfd2009-11-09 23:46:56 +00001133
Javier Cardona16dd7262011-08-09 16:45:11 -07001134 flags = (ifmsh->mshcfg.dot11MeshGateAnnouncementProtocol)
1135 ? RANN_FLAG_IS_GATE : 0;
1136 mesh_path_sel_frame_tx(MPATH_RANN, flags, sdata->vif.addr,
Rui Pauloe304bfd2009-11-09 23:46:56 +00001137 cpu_to_le32(++ifmsh->sn),
Johannes Berg15ff6362009-11-17 13:34:04 +01001138 0, NULL, 0, broadcast_addr,
Javier Cardona45904f22010-12-03 09:20:40 +01001139 0, sdata->u.mesh.mshcfg.element_ttl,
Javier Cardona0507e152011-08-09 16:45:10 -07001140 cpu_to_le32(interval), 0, 0, sdata);
Rui Pauloe304bfd2009-11-09 23:46:56 +00001141}