blob: 35e3acbe22624f3874f3cfbde8741dcbb9a40bc3 [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>
Felix Fietkau888d04d2012-03-01 15:22:09 +010011#include <linux/etherdevice.h>
Johannes Bergd26ad372012-02-20 11:38:41 +010012#include <asm/unaligned.h>
Javier Cardona2cca3972011-09-06 12:10:43 -070013#include "wme.h"
Luis Carlos Cobo050ac522008-02-23 15:17:15 +010014#include "mesh.h"
15
Rui Paulo27db2e42009-11-09 23:46:45 +000016#ifdef CONFIG_MAC80211_VERBOSE_MHWMP_DEBUG
Javier Cardona76468872011-08-09 16:45:04 -070017#define mhwmp_dbg(fmt, args...) \
Joe Perchesf0d23202012-05-15 14:20:30 -070018 pr_debug("Mesh HWMP (%s): " fmt "\n", sdata->name, ##args)
Rui Paulo27db2e42009-11-09 23:46:45 +000019#else
20#define mhwmp_dbg(fmt, args...) do { (void)(0); } while (0)
21#endif
22
Luis Carlos Cobo050ac522008-02-23 15:17:15 +010023#define TEST_FRAME_LEN 8192
24#define MAX_METRIC 0xffffffff
25#define ARITH_SHIFT 8
26
27/* Number of frames buffered per destination for unresolved destinations */
28#define MESH_FRAME_QUEUE_LEN 10
29#define MAX_PREQ_QUEUE_LEN 64
30
31/* Destination only */
32#define MP_F_DO 0x1
33/* Reply and forward */
34#define MP_F_RF 0x2
Rui Paulod611f062009-11-09 23:46:50 +000035/* Unknown Sequence Number */
36#define MP_F_USN 0x01
37/* Reason code Present */
38#define MP_F_RCODE 0x02
Luis Carlos Cobo050ac522008-02-23 15:17:15 +010039
Rui Paulo90a5e162009-11-11 00:01:31 +000040static void mesh_queue_preq(struct mesh_path *, u8);
41
Luis Carlos Coboa00de5d2008-02-29 17:07:54 -080042static inline u32 u32_field_get(u8 *preq_elem, int offset, bool ae)
43{
44 if (ae)
45 offset += 6;
Harvey Harrisonae7245c2008-05-01 22:19:33 -070046 return get_unaligned_le32(preq_elem + offset);
Luis Carlos Coboa00de5d2008-02-29 17:07:54 -080047}
48
Rui Paulod611f062009-11-09 23:46:50 +000049static inline u32 u16_field_get(u8 *preq_elem, int offset, bool ae)
50{
51 if (ae)
52 offset += 6;
53 return get_unaligned_le16(preq_elem + offset);
54}
55
Luis Carlos Cobo050ac522008-02-23 15:17:15 +010056/* HWMP IE processing macros */
Luis Carlos Coboa00de5d2008-02-29 17:07:54 -080057#define AE_F (1<<6)
58#define AE_F_SET(x) (*x & AE_F)
59#define PREQ_IE_FLAGS(x) (*(x))
60#define PREQ_IE_HOPCOUNT(x) (*(x + 1))
61#define PREQ_IE_TTL(x) (*(x + 2))
62#define PREQ_IE_PREQ_ID(x) u32_field_get(x, 3, 0)
63#define PREQ_IE_ORIG_ADDR(x) (x + 7)
Phil Carmody497888c2011-07-14 15:07:13 +030064#define PREQ_IE_ORIG_SN(x) u32_field_get(x, 13, 0)
65#define PREQ_IE_LIFETIME(x) u32_field_get(x, 17, AE_F_SET(x))
66#define PREQ_IE_METRIC(x) u32_field_get(x, 21, AE_F_SET(x))
Rui Paulod19b3bf2009-11-09 23:46:55 +000067#define PREQ_IE_TARGET_F(x) (*(AE_F_SET(x) ? x + 32 : x + 26))
68#define PREQ_IE_TARGET_ADDR(x) (AE_F_SET(x) ? x + 33 : x + 27)
Phil Carmody497888c2011-07-14 15:07:13 +030069#define PREQ_IE_TARGET_SN(x) u32_field_get(x, 33, AE_F_SET(x))
Luis Carlos Cobo050ac522008-02-23 15:17:15 +010070
71
Luis Carlos Coboa00de5d2008-02-29 17:07:54 -080072#define PREP_IE_FLAGS(x) PREQ_IE_FLAGS(x)
73#define PREP_IE_HOPCOUNT(x) PREQ_IE_HOPCOUNT(x)
74#define PREP_IE_TTL(x) PREQ_IE_TTL(x)
Thomas Pedersen25d49e42011-08-11 19:35:15 -070075#define PREP_IE_ORIG_ADDR(x) (AE_F_SET(x) ? x + 27 : x + 21)
76#define PREP_IE_ORIG_SN(x) u32_field_get(x, 27, AE_F_SET(x))
Phil Carmody497888c2011-07-14 15:07:13 +030077#define PREP_IE_LIFETIME(x) u32_field_get(x, 13, AE_F_SET(x))
78#define PREP_IE_METRIC(x) u32_field_get(x, 17, AE_F_SET(x))
Thomas Pedersen25d49e42011-08-11 19:35:15 -070079#define PREP_IE_TARGET_ADDR(x) (x + 3)
80#define PREP_IE_TARGET_SN(x) u32_field_get(x, 9, 0)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +010081
Rui Paulod611f062009-11-09 23:46:50 +000082#define PERR_IE_TTL(x) (*(x))
Rui Paulod19b3bf2009-11-09 23:46:55 +000083#define PERR_IE_TARGET_FLAGS(x) (*(x + 2))
84#define PERR_IE_TARGET_ADDR(x) (x + 3)
Phil Carmody497888c2011-07-14 15:07:13 +030085#define PERR_IE_TARGET_SN(x) u32_field_get(x, 9, 0)
86#define PERR_IE_TARGET_RCODE(x) u16_field_get(x, 13, 0)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +010087
Luis Carlos Cobo050ac522008-02-23 15:17:15 +010088#define MSEC_TO_TU(x) (x*1000/1024)
Chun-Yeow Yeohd2a079f2012-03-23 18:48:51 +080089#define SN_GT(x, y) ((s32)(y - x) < 0)
90#define SN_LT(x, y) ((s32)(x - y) < 0)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +010091
92#define net_traversal_jiffies(s) \
Johannes Berg472dbc42008-09-11 00:01:49 +020093 msecs_to_jiffies(s->u.mesh.mshcfg.dot11MeshHWMPnetDiameterTraversalTime)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +010094#define default_lifetime(s) \
Johannes Berg472dbc42008-09-11 00:01:49 +020095 MSEC_TO_TU(s->u.mesh.mshcfg.dot11MeshHWMPactivePathTimeout)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +010096#define min_preq_int_jiff(s) \
Johannes Berg472dbc42008-09-11 00:01:49 +020097 (msecs_to_jiffies(s->u.mesh.mshcfg.dot11MeshHWMPpreqMinInterval))
98#define max_preq_retries(s) (s->u.mesh.mshcfg.dot11MeshHWMPmaxPREQretries)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +010099#define disc_timeout_jiff(s) \
Johannes Berg472dbc42008-09-11 00:01:49 +0200100 msecs_to_jiffies(sdata->u.mesh.mshcfg.min_discovery_timeout)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100101
102enum mpath_frame_type {
103 MPATH_PREQ = 0,
104 MPATH_PREP,
Rui Paulo90a5e162009-11-11 00:01:31 +0000105 MPATH_PERR,
106 MPATH_RANN
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100107};
108
Johannes Berg15ff6362009-11-17 13:34:04 +0100109static const u8 broadcast_addr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
110
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100111static int mesh_path_sel_frame_tx(enum mpath_frame_type action, u8 flags,
Rui Paulod19b3bf2009-11-09 23:46:55 +0000112 u8 *orig_addr, __le32 orig_sn, u8 target_flags, u8 *target,
Johannes Berg15ff6362009-11-17 13:34:04 +0100113 __le32 target_sn, const u8 *da, u8 hop_count, u8 ttl,
114 __le32 lifetime, __le32 metric, __le32 preq_id,
Rui Paulod19b3bf2009-11-09 23:46:55 +0000115 struct ieee80211_sub_if_data *sdata)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100116{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200117 struct ieee80211_local *local = sdata->local;
Thomas Pedersen3b69a9c2011-10-26 14:47:25 -0700118 struct sk_buff *skb;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100119 struct ieee80211_mgmt *mgmt;
Thomas Pedersen3b69a9c2011-10-26 14:47:25 -0700120 u8 *pos, ie_len;
121 int hdr_len = offsetof(struct ieee80211_mgmt, u.action.u.mesh_action) +
122 sizeof(mgmt->u.action.u.mesh_action);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100123
Javier Cardona65e8b0c2012-01-17 18:17:46 -0800124 skb = dev_alloc_skb(local->tx_headroom +
Thomas Pedersen3b69a9c2011-10-26 14:47:25 -0700125 hdr_len +
126 2 + 37); /* max HWMP IE */
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100127 if (!skb)
128 return -1;
Javier Cardona65e8b0c2012-01-17 18:17:46 -0800129 skb_reserve(skb, local->tx_headroom);
Thomas Pedersen3b69a9c2011-10-26 14:47:25 -0700130 mgmt = (struct ieee80211_mgmt *) skb_put(skb, hdr_len);
131 memset(mgmt, 0, hdr_len);
Harvey Harrisone7827a72008-07-15 18:44:13 -0700132 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
133 IEEE80211_STYPE_ACTION);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100134
135 memcpy(mgmt->da, da, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +0100136 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
Rui Paulo90a5e162009-11-11 00:01:31 +0000137 /* BSSID == SA */
Johannes Berg47846c92009-11-25 17:46:19 +0100138 memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
Thomas Pedersen25d49e42011-08-11 19:35:15 -0700139 mgmt->u.action.category = WLAN_CATEGORY_MESH_ACTION;
140 mgmt->u.action.u.mesh_action.action_code =
141 WLAN_MESH_ACTION_HWMP_PATH_SELECTION;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100142
143 switch (action) {
144 case MPATH_PREQ:
Javier Cardona76468872011-08-09 16:45:04 -0700145 mhwmp_dbg("sending PREQ to %pM", target);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100146 ie_len = 37;
147 pos = skb_put(skb, 2 + ie_len);
148 *pos++ = WLAN_EID_PREQ;
149 break;
150 case MPATH_PREP:
Javier Cardona76468872011-08-09 16:45:04 -0700151 mhwmp_dbg("sending PREP to %pM", target);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100152 ie_len = 31;
153 pos = skb_put(skb, 2 + ie_len);
154 *pos++ = WLAN_EID_PREP;
155 break;
Rui Paulo90a5e162009-11-11 00:01:31 +0000156 case MPATH_RANN:
Javier Cardona76468872011-08-09 16:45:04 -0700157 mhwmp_dbg("sending RANN from %pM", orig_addr);
Rui Paulo90a5e162009-11-11 00:01:31 +0000158 ie_len = sizeof(struct ieee80211_rann_ie);
159 pos = skb_put(skb, 2 + ie_len);
160 *pos++ = WLAN_EID_RANN;
161 break;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100162 default:
Patrick McHardy812714d2008-05-06 12:52:07 +0200163 kfree_skb(skb);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100164 return -ENOTSUPP;
165 break;
166 }
167 *pos++ = ie_len;
168 *pos++ = flags;
169 *pos++ = hop_count;
170 *pos++ = ttl;
Thomas Pedersen25d49e42011-08-11 19:35:15 -0700171 if (action == MPATH_PREP) {
Rui Paulod19b3bf2009-11-09 23:46:55 +0000172 memcpy(pos, target, ETH_ALEN);
Rui Paulo90a5e162009-11-11 00:01:31 +0000173 pos += ETH_ALEN;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000174 memcpy(pos, &target_sn, 4);
Thomas Pedersen25d49e42011-08-11 19:35:15 -0700175 pos += 4;
176 } else {
177 if (action == MPATH_PREQ) {
178 memcpy(pos, &preq_id, 4);
179 pos += 4;
180 }
181 memcpy(pos, orig_addr, ETH_ALEN);
182 pos += ETH_ALEN;
183 memcpy(pos, &orig_sn, 4);
184 pos += 4;
185 }
186 memcpy(pos, &lifetime, 4); /* interval for RANN */
187 pos += 4;
188 memcpy(pos, &metric, 4);
189 pos += 4;
190 if (action == MPATH_PREQ) {
191 *pos++ = 1; /* destination count */
192 *pos++ = target_flags;
193 memcpy(pos, target, ETH_ALEN);
194 pos += ETH_ALEN;
195 memcpy(pos, &target_sn, 4);
196 pos += 4;
197 } else if (action == MPATH_PREP) {
198 memcpy(pos, orig_addr, ETH_ALEN);
199 pos += ETH_ALEN;
200 memcpy(pos, &orig_sn, 4);
201 pos += 4;
Rui Paulo90a5e162009-11-11 00:01:31 +0000202 }
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100203
Johannes Berg62ae67b2009-11-18 18:42:05 +0100204 ieee80211_tx_skb(sdata, skb);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100205 return 0;
206}
207
Javier Cardona2cca3972011-09-06 12:10:43 -0700208
209/* Headroom is not adjusted. Caller should ensure that skb has sufficient
210 * headroom in case the frame is encrypted. */
211static void prepare_frame_for_deferred_tx(struct ieee80211_sub_if_data *sdata,
212 struct sk_buff *skb)
213{
Javier Cardona2cca3972011-09-06 12:10:43 -0700214 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
215
216 skb_set_mac_header(skb, 0);
217 skb_set_network_header(skb, 0);
218 skb_set_transport_header(skb, 0);
219
220 /* Send all internal mgmt frames on VO. Accordingly set TID to 7. */
221 skb_set_queue_mapping(skb, IEEE80211_AC_VO);
222 skb->priority = 7;
223
224 info->control.vif = &sdata->vif;
Javier Cardona2154c812011-09-07 17:49:53 -0700225 ieee80211_set_qos_hdr(sdata, skb);
Javier Cardona2cca3972011-09-06 12:10:43 -0700226}
227
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100228/**
229 * mesh_send_path error - Sends a PERR mesh management frame
230 *
Rui Paulod19b3bf2009-11-09 23:46:55 +0000231 * @target: broken destination
232 * @target_sn: SN of the broken destination
233 * @target_rcode: reason code for this PERR
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100234 * @ra: node this frame is addressed to
Javier Cardona2cca3972011-09-06 12:10:43 -0700235 *
236 * Note: This function may be called with driver locks taken that the driver
237 * also acquires in the TX path. To avoid a deadlock we don't transmit the
238 * frame directly but add it to the pending queue instead.
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100239 */
Rui Paulod19b3bf2009-11-09 23:46:55 +0000240int mesh_path_error_tx(u8 ttl, u8 *target, __le32 target_sn,
Johannes Berg15ff6362009-11-17 13:34:04 +0100241 __le16 target_rcode, const u8 *ra,
242 struct ieee80211_sub_if_data *sdata)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100243{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200244 struct ieee80211_local *local = sdata->local;
Thomas Pedersen3b69a9c2011-10-26 14:47:25 -0700245 struct sk_buff *skb;
Thomas Pedersendca7e942011-11-24 17:15:24 -0800246 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100247 struct ieee80211_mgmt *mgmt;
Thomas Pedersen3b69a9c2011-10-26 14:47:25 -0700248 u8 *pos, ie_len;
249 int hdr_len = offsetof(struct ieee80211_mgmt, u.action.u.mesh_action) +
250 sizeof(mgmt->u.action.u.mesh_action);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100251
Thomas Pedersendca7e942011-11-24 17:15:24 -0800252 if (time_before(jiffies, ifmsh->next_perr))
253 return -EAGAIN;
254
Javier Cardona65e8b0c2012-01-17 18:17:46 -0800255 skb = dev_alloc_skb(local->tx_headroom +
Thomas Pedersen3b69a9c2011-10-26 14:47:25 -0700256 hdr_len +
257 2 + 15 /* PERR IE */);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100258 if (!skb)
259 return -1;
Javier Cardona65e8b0c2012-01-17 18:17:46 -0800260 skb_reserve(skb, local->tx_headroom);
Thomas Pedersen3b69a9c2011-10-26 14:47:25 -0700261 mgmt = (struct ieee80211_mgmt *) skb_put(skb, hdr_len);
262 memset(mgmt, 0, hdr_len);
Harvey Harrisone7827a72008-07-15 18:44:13 -0700263 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
264 IEEE80211_STYPE_ACTION);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100265
266 memcpy(mgmt->da, ra, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +0100267 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
Thomas Pedersen25d49e42011-08-11 19:35:15 -0700268 /* BSSID == SA */
269 memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
270 mgmt->u.action.category = WLAN_CATEGORY_MESH_ACTION;
271 mgmt->u.action.u.mesh_action.action_code =
272 WLAN_MESH_ACTION_HWMP_PATH_SELECTION;
Rui Paulod611f062009-11-09 23:46:50 +0000273 ie_len = 15;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100274 pos = skb_put(skb, 2 + ie_len);
275 *pos++ = WLAN_EID_PERR;
276 *pos++ = ie_len;
Rui Paulod611f062009-11-09 23:46:50 +0000277 /* ttl */
Javier Cardona45904f22010-12-03 09:20:40 +0100278 *pos++ = ttl;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100279 /* number of destinations */
280 *pos++ = 1;
Rui Paulod611f062009-11-09 23:46:50 +0000281 /*
282 * flags bit, bit 1 is unset if we know the sequence number and
283 * bit 2 is set if we have a reason code
284 */
285 *pos = 0;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000286 if (!target_sn)
Rui Paulod611f062009-11-09 23:46:50 +0000287 *pos |= MP_F_USN;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000288 if (target_rcode)
Rui Paulod611f062009-11-09 23:46:50 +0000289 *pos |= MP_F_RCODE;
290 pos++;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000291 memcpy(pos, target, ETH_ALEN);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100292 pos += ETH_ALEN;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000293 memcpy(pos, &target_sn, 4);
Rui Paulod611f062009-11-09 23:46:50 +0000294 pos += 4;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000295 memcpy(pos, &target_rcode, 2);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100296
Javier Cardona2cca3972011-09-06 12:10:43 -0700297 /* see note in function header */
298 prepare_frame_for_deferred_tx(sdata, skb);
Thomas Pedersendca7e942011-11-24 17:15:24 -0800299 ifmsh->next_perr = TU_TO_EXP_TIME(
300 ifmsh->mshcfg.dot11MeshHWMPperrMinInterval);
Javier Cardona2cca3972011-09-06 12:10:43 -0700301 ieee80211_add_pending_skb(local, skb);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100302 return 0;
303}
304
Javier Cardonabfc32e62009-08-17 17:15:55 -0700305void ieee80211s_update_metric(struct ieee80211_local *local,
Javier Cardona35b3fe1c2012-06-08 13:30:25 -0700306 struct sta_info *sta, struct sk_buff *skb)
Javier Cardonabfc32e62009-08-17 17:15:55 -0700307{
308 struct ieee80211_tx_info *txinfo = IEEE80211_SKB_CB(skb);
309 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
310 int failed;
311
312 if (!ieee80211_is_data(hdr->frame_control))
313 return;
314
315 failed = !(txinfo->flags & IEEE80211_TX_STAT_ACK);
316
317 /* moving average, scaled to 100 */
Javier Cardona35b3fe1c2012-06-08 13:30:25 -0700318 sta->fail_avg = ((80 * sta->fail_avg + 5) / 100 + 20 * failed);
319 if (sta->fail_avg > 95)
320 mesh_plink_broken(sta);
Javier Cardonabfc32e62009-08-17 17:15:55 -0700321}
322
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100323static u32 airtime_link_metric_get(struct ieee80211_local *local,
324 struct sta_info *sta)
325{
Thomas Pedersen6b62bf32012-03-05 15:31:48 -0800326 struct rate_info rinfo;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100327 /* This should be adjusted for each device */
328 int device_constant = 1 << ARITH_SHIFT;
329 int test_frame_len = TEST_FRAME_LEN << ARITH_SHIFT;
330 int s_unit = 1 << ARITH_SHIFT;
331 int rate, err;
332 u32 tx_time, estimated_retx;
333 u64 result;
334
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100335 if (sta->fail_avg >= 100)
336 return MAX_METRIC;
Johannes Berge6a98542008-10-21 12:40:02 +0200337
Thomas Pedersen6b62bf32012-03-05 15:31:48 -0800338 sta_set_rate_info_tx(sta, &sta->last_tx_rate, &rinfo);
339 rate = cfg80211_calculate_bitrate(&rinfo);
340 if (WARN_ON(!rate))
Johannes Berge6a98542008-10-21 12:40:02 +0200341 return MAX_METRIC;
342
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100343 err = (sta->fail_avg << ARITH_SHIFT) / 100;
344
345 /* bitrate is in units of 100 Kbps, while we need rate in units of
346 * 1Mbps. This will be corrected on tx_time computation.
347 */
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
Joe Perchesb203ca32012-05-08 18:56:52 +0000422 if (ether_addr_equal(orig_addr, sdata->vif.addr)) {
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;
Joe Perchesb203ca32012-05-08 18:56:52 +0000472 if (ether_addr_equal(orig_addr, ta))
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100473 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;
Chun-Yeow Yeoh3fbf4b71b2012-06-17 02:27:40 +0800519 u8 target_flags, ttl, flags;
520 u32 orig_sn, target_sn, lifetime, orig_metric;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100521 bool reply = false;
522 bool forward = true;
Chun-Yeow Yeoh3fbf4b71b2012-06-17 02:27:40 +0800523 bool root_is_gate;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100524
Rui Paulod19b3bf2009-11-09 23:46:55 +0000525 /* Update target SN, if present */
526 target_addr = PREQ_IE_TARGET_ADDR(preq_elem);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100527 orig_addr = PREQ_IE_ORIG_ADDR(preq_elem);
Rui Paulod19b3bf2009-11-09 23:46:55 +0000528 target_sn = PREQ_IE_TARGET_SN(preq_elem);
529 orig_sn = PREQ_IE_ORIG_SN(preq_elem);
530 target_flags = PREQ_IE_TARGET_F(preq_elem);
Chun-Yeow Yeoh3fbf4b71b2012-06-17 02:27:40 +0800531 orig_metric = metric;
532 /* Proactive PREQ gate announcements */
533 flags = PREQ_IE_FLAGS(preq_elem);
534 root_is_gate = !!(flags & RANN_FLAG_IS_GATE);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100535
Javier Cardona76468872011-08-09 16:45:04 -0700536 mhwmp_dbg("received PREQ from %pM", orig_addr);
Rui Paulo27db2e42009-11-09 23:46:45 +0000537
Joe Perchesb203ca32012-05-08 18:56:52 +0000538 if (ether_addr_equal(target_addr, sdata->vif.addr)) {
Javier Cardona76468872011-08-09 16:45:04 -0700539 mhwmp_dbg("PREQ is for us");
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100540 forward = false;
541 reply = true;
542 metric = 0;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000543 if (time_after(jiffies, ifmsh->last_sn_update +
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100544 net_traversal_jiffies(sdata)) ||
Rui Paulod19b3bf2009-11-09 23:46:55 +0000545 time_before(jiffies, ifmsh->last_sn_update)) {
546 target_sn = ++ifmsh->sn;
547 ifmsh->last_sn_update = jiffies;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100548 }
Chun-Yeow Yeoh3fbf4b71b2012-06-17 02:27:40 +0800549 } else if (is_broadcast_ether_addr(target_addr) &&
550 (target_flags & IEEE80211_PREQ_TO_FLAG)) {
551 rcu_read_lock();
552 mpath = mesh_path_lookup(orig_addr, sdata);
553 if (mpath) {
554 if (flags & IEEE80211_PREQ_PROACTIVE_PREP_FLAG) {
555 reply = true;
556 target_addr = sdata->vif.addr;
557 target_sn = ++ifmsh->sn;
558 metric = 0;
559 ifmsh->last_sn_update = jiffies;
560 }
561 if (root_is_gate)
562 mesh_path_add_gate(mpath);
563 }
564 rcu_read_unlock();
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100565 } else {
566 rcu_read_lock();
Rui Paulod19b3bf2009-11-09 23:46:55 +0000567 mpath = mesh_path_lookup(target_addr, sdata);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100568 if (mpath) {
Rui Paulod19b3bf2009-11-09 23:46:55 +0000569 if ((!(mpath->flags & MESH_PATH_SN_VALID)) ||
570 SN_LT(mpath->sn, target_sn)) {
571 mpath->sn = target_sn;
572 mpath->flags |= MESH_PATH_SN_VALID;
573 } else if ((!(target_flags & MP_F_DO)) &&
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100574 (mpath->flags & MESH_PATH_ACTIVE)) {
575 reply = true;
576 metric = mpath->metric;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000577 target_sn = mpath->sn;
578 if (target_flags & MP_F_RF)
579 target_flags |= MP_F_DO;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100580 else
581 forward = false;
582 }
583 }
584 rcu_read_unlock();
585 }
586
587 if (reply) {
588 lifetime = PREQ_IE_LIFETIME(preq_elem);
Javier Cardona45904f22010-12-03 09:20:40 +0100589 ttl = ifmsh->mshcfg.element_ttl;
Rui Paulo27db2e42009-11-09 23:46:45 +0000590 if (ttl != 0) {
Javier Cardona76468872011-08-09 16:45:04 -0700591 mhwmp_dbg("replying to the PREQ");
Thomas Pedersen3c26f1f2011-11-24 17:15:22 -0800592 mesh_path_sel_frame_tx(MPATH_PREP, 0, orig_addr,
593 cpu_to_le32(orig_sn), 0, target_addr,
594 cpu_to_le32(target_sn), mgmt->sa, 0, ttl,
Luis Carlos Coboaa2b5922008-02-29 14:30:32 -0800595 cpu_to_le32(lifetime), cpu_to_le32(metric),
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200596 0, sdata);
Chun-Yeow Yeoh3fbf4b71b2012-06-17 02:27:40 +0800597 } else {
Johannes Berg472dbc42008-09-11 00:01:49 +0200598 ifmsh->mshstats.dropped_frames_ttl++;
Chun-Yeow Yeoh3fbf4b71b2012-06-17 02:27:40 +0800599 }
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100600 }
601
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +0800602 if (forward && ifmsh->mshcfg.dot11MeshForwarding) {
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100603 u32 preq_id;
Chun-Yeow Yeoh3fbf4b71b2012-06-17 02:27:40 +0800604 u8 hopcount;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100605
606 ttl = PREQ_IE_TTL(preq_elem);
607 lifetime = PREQ_IE_LIFETIME(preq_elem);
608 if (ttl <= 1) {
Johannes Berg472dbc42008-09-11 00:01:49 +0200609 ifmsh->mshstats.dropped_frames_ttl++;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100610 return;
611 }
Javier Cardona76468872011-08-09 16:45:04 -0700612 mhwmp_dbg("forwarding the PREQ from %pM", orig_addr);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100613 --ttl;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100614 preq_id = PREQ_IE_PREQ_ID(preq_elem);
615 hopcount = PREQ_IE_HOPCOUNT(preq_elem) + 1;
Chun-Yeow Yeoh3d045a52012-02-28 22:00:06 +0800616 da = (mpath && mpath->is_root) ?
617 mpath->rann_snd_addr : broadcast_addr;
Chun-Yeow Yeoh3fbf4b71b2012-06-17 02:27:40 +0800618
619 if (flags & IEEE80211_PREQ_PROACTIVE_PREP_FLAG) {
620 target_addr = PREQ_IE_TARGET_ADDR(preq_elem);
621 target_sn = PREQ_IE_TARGET_SN(preq_elem);
622 metric = orig_metric;
623 }
624
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100625 mesh_path_sel_frame_tx(MPATH_PREQ, flags, orig_addr,
Rui Paulod19b3bf2009-11-09 23:46:55 +0000626 cpu_to_le32(orig_sn), target_flags, target_addr,
Chun-Yeow Yeoh3d045a52012-02-28 22:00:06 +0800627 cpu_to_le32(target_sn), da,
Luis Carlos Coboaa2b5922008-02-29 14:30:32 -0800628 hopcount, ttl, cpu_to_le32(lifetime),
629 cpu_to_le32(metric), cpu_to_le32(preq_id),
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200630 sdata);
Chun-Yeow Yeoh7d4e15b2012-05-04 14:57:50 +0800631 if (!is_multicast_ether_addr(da))
632 ifmsh->mshstats.fwded_unicast++;
633 else
634 ifmsh->mshstats.fwded_mcast++;
Johannes Berg472dbc42008-09-11 00:01:49 +0200635 ifmsh->mshstats.fwded_frames++;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100636 }
637}
638
639
Johannes Berg40b275b2011-05-13 14:15:49 +0200640static inline struct sta_info *
641next_hop_deref_protected(struct mesh_path *mpath)
642{
643 return rcu_dereference_protected(mpath->next_hop,
644 lockdep_is_held(&mpath->state_lock));
645}
646
647
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200648static void hwmp_prep_frame_process(struct ieee80211_sub_if_data *sdata,
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100649 struct ieee80211_mgmt *mgmt,
650 u8 *prep_elem, u32 metric)
651{
Chun-Yeow Yeohd6655082012-03-02 02:03:19 +0800652 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100653 struct mesh_path *mpath;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000654 u8 *target_addr, *orig_addr;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100655 u8 ttl, hopcount, flags;
656 u8 next_hop[ETH_ALEN];
Rui Paulod19b3bf2009-11-09 23:46:55 +0000657 u32 target_sn, orig_sn, lifetime;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100658
Javier Cardona76468872011-08-09 16:45:04 -0700659 mhwmp_dbg("received PREP from %pM", PREP_IE_ORIG_ADDR(prep_elem));
Rui Paulodbb81c42009-11-09 23:46:46 +0000660
Thomas Pedersen3c26f1f2011-11-24 17:15:22 -0800661 orig_addr = PREP_IE_ORIG_ADDR(prep_elem);
Joe Perchesb203ca32012-05-08 18:56:52 +0000662 if (ether_addr_equal(orig_addr, sdata->vif.addr))
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100663 /* destination, no forwarding required */
664 return;
665
Chun-Yeow Yeohd6655082012-03-02 02:03:19 +0800666 if (!ifmsh->mshcfg.dot11MeshForwarding)
667 return;
668
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100669 ttl = PREP_IE_TTL(prep_elem);
670 if (ttl <= 1) {
Johannes Berg472dbc42008-09-11 00:01:49 +0200671 sdata->u.mesh.mshstats.dropped_frames_ttl++;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100672 return;
673 }
674
675 rcu_read_lock();
Thomas Pedersen3c26f1f2011-11-24 17:15:22 -0800676 mpath = mesh_path_lookup(orig_addr, sdata);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100677 if (mpath)
678 spin_lock_bh(&mpath->state_lock);
679 else
680 goto fail;
681 if (!(mpath->flags & MESH_PATH_ACTIVE)) {
682 spin_unlock_bh(&mpath->state_lock);
683 goto fail;
684 }
Johannes Berg40b275b2011-05-13 14:15:49 +0200685 memcpy(next_hop, next_hop_deref_protected(mpath)->sta.addr, ETH_ALEN);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100686 spin_unlock_bh(&mpath->state_lock);
687 --ttl;
688 flags = PREP_IE_FLAGS(prep_elem);
689 lifetime = PREP_IE_LIFETIME(prep_elem);
690 hopcount = PREP_IE_HOPCOUNT(prep_elem) + 1;
Thomas Pedersen3c26f1f2011-11-24 17:15:22 -0800691 target_addr = PREP_IE_TARGET_ADDR(prep_elem);
Rui Paulod19b3bf2009-11-09 23:46:55 +0000692 target_sn = PREP_IE_TARGET_SN(prep_elem);
693 orig_sn = PREP_IE_ORIG_SN(prep_elem);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100694
695 mesh_path_sel_frame_tx(MPATH_PREP, flags, orig_addr,
Rui Paulod19b3bf2009-11-09 23:46:55 +0000696 cpu_to_le32(orig_sn), 0, target_addr,
Porsch, Marco533866b2010-02-24 09:53:13 +0100697 cpu_to_le32(target_sn), next_hop, hopcount,
Rui Paulod19b3bf2009-11-09 23:46:55 +0000698 ttl, cpu_to_le32(lifetime), cpu_to_le32(metric),
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200699 0, sdata);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100700 rcu_read_unlock();
Daniel Walkerc8a61a72009-08-18 10:59:00 -0700701
702 sdata->u.mesh.mshstats.fwded_unicast++;
Johannes Berg472dbc42008-09-11 00:01:49 +0200703 sdata->u.mesh.mshstats.fwded_frames++;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100704 return;
705
706fail:
707 rcu_read_unlock();
Johannes Berg472dbc42008-09-11 00:01:49 +0200708 sdata->u.mesh.mshstats.dropped_frames_no_route++;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100709}
710
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200711static void hwmp_perr_frame_process(struct ieee80211_sub_if_data *sdata,
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100712 struct ieee80211_mgmt *mgmt, u8 *perr_elem)
713{
Rui Paulod611f062009-11-09 23:46:50 +0000714 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100715 struct mesh_path *mpath;
Rui Paulod611f062009-11-09 23:46:50 +0000716 u8 ttl;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000717 u8 *ta, *target_addr;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000718 u32 target_sn;
719 u16 target_rcode;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100720
721 ta = mgmt->sa;
Rui Paulod611f062009-11-09 23:46:50 +0000722 ttl = PERR_IE_TTL(perr_elem);
723 if (ttl <= 1) {
724 ifmsh->mshstats.dropped_frames_ttl++;
725 return;
726 }
727 ttl--;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000728 target_addr = PERR_IE_TARGET_ADDR(perr_elem);
729 target_sn = PERR_IE_TARGET_SN(perr_elem);
730 target_rcode = PERR_IE_TARGET_RCODE(perr_elem);
Rui Paulod611f062009-11-09 23:46:50 +0000731
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100732 rcu_read_lock();
Rui Paulod19b3bf2009-11-09 23:46:55 +0000733 mpath = mesh_path_lookup(target_addr, sdata);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100734 if (mpath) {
Felix Fietkau888d04d2012-03-01 15:22:09 +0100735 struct sta_info *sta;
736
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100737 spin_lock_bh(&mpath->state_lock);
Felix Fietkau888d04d2012-03-01 15:22:09 +0100738 sta = next_hop_deref_protected(mpath);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100739 if (mpath->flags & MESH_PATH_ACTIVE &&
Joe Perchesb203ca32012-05-08 18:56:52 +0000740 ether_addr_equal(ta, sta->sta.addr) &&
Rui Paulod19b3bf2009-11-09 23:46:55 +0000741 (!(mpath->flags & MESH_PATH_SN_VALID) ||
742 SN_GT(target_sn, mpath->sn))) {
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100743 mpath->flags &= ~MESH_PATH_ACTIVE;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000744 mpath->sn = target_sn;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100745 spin_unlock_bh(&mpath->state_lock);
Chun-Yeow Yeohd6655082012-03-02 02:03:19 +0800746 if (!ifmsh->mshcfg.dot11MeshForwarding)
747 goto endperr;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000748 mesh_path_error_tx(ttl, target_addr, cpu_to_le32(target_sn),
749 cpu_to_le16(target_rcode),
Johannes Berg15ff6362009-11-17 13:34:04 +0100750 broadcast_addr, sdata);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100751 } else
752 spin_unlock_bh(&mpath->state_lock);
753 }
Chun-Yeow Yeohd6655082012-03-02 02:03:19 +0800754endperr:
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100755 rcu_read_unlock();
756}
757
Rui Paulo90a5e162009-11-11 00:01:31 +0000758static void hwmp_rann_frame_process(struct ieee80211_sub_if_data *sdata,
759 struct ieee80211_mgmt *mgmt,
760 struct ieee80211_rann_ie *rann)
761{
762 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
Chun-Yeow Yeohd2a079f2012-03-23 18:48:51 +0800763 struct ieee80211_local *local = sdata->local;
764 struct sta_info *sta;
Rui Paulo90a5e162009-11-11 00:01:31 +0000765 struct mesh_path *mpath;
Rui Paulo90a5e162009-11-11 00:01:31 +0000766 u8 ttl, flags, hopcount;
767 u8 *orig_addr;
Chun-Yeow Yeohd2a079f2012-03-23 18:48:51 +0800768 u32 orig_sn, metric, metric_txsta, interval;
Javier Cardona5ee68e52011-08-09 16:45:08 -0700769 bool root_is_gate;
Rui Paulo90a5e162009-11-11 00:01:31 +0000770
Rui Paulo90a5e162009-11-11 00:01:31 +0000771 ttl = rann->rann_ttl;
772 if (ttl <= 1) {
773 ifmsh->mshstats.dropped_frames_ttl++;
774 return;
775 }
776 ttl--;
777 flags = rann->rann_flags;
Javier Cardona5ee68e52011-08-09 16:45:08 -0700778 root_is_gate = !!(flags & RANN_FLAG_IS_GATE);
Rui Paulo90a5e162009-11-11 00:01:31 +0000779 orig_addr = rann->rann_addr;
Chun-Yeow Yeoh292c41a2012-03-19 21:38:46 +0800780 orig_sn = le32_to_cpu(rann->rann_seq);
Chun-Yeow Yeohd2a079f2012-03-23 18:48:51 +0800781 interval = le32_to_cpu(rann->rann_interval);
Rui Paulo90a5e162009-11-11 00:01:31 +0000782 hopcount = rann->rann_hopcount;
Rui Pauloa6a58b42009-11-09 23:46:51 +0000783 hopcount++;
Chun-Yeow Yeoh292c41a2012-03-19 21:38:46 +0800784 metric = le32_to_cpu(rann->rann_metric);
Javier Cardona5ee68e52011-08-09 16:45:08 -0700785
786 /* Ignore our own RANNs */
Joe Perchesb203ca32012-05-08 18:56:52 +0000787 if (ether_addr_equal(orig_addr, sdata->vif.addr))
Javier Cardona5ee68e52011-08-09 16:45:08 -0700788 return;
789
Chun-Yeow Yeoh3d045a52012-02-28 22:00:06 +0800790 mhwmp_dbg("received RANN from %pM via neighbour %pM (is_gate=%d)",
791 orig_addr, mgmt->sa, root_is_gate);
Rui Paulo90a5e162009-11-11 00:01:31 +0000792
793 rcu_read_lock();
Chun-Yeow Yeohd2a079f2012-03-23 18:48:51 +0800794 sta = sta_info_get(sdata, mgmt->sa);
795 if (!sta) {
796 rcu_read_unlock();
797 return;
798 }
799
800 metric_txsta = airtime_link_metric_get(local, sta);
801
Rui Paulo90a5e162009-11-11 00:01:31 +0000802 mpath = mesh_path_lookup(orig_addr, sdata);
803 if (!mpath) {
804 mesh_path_add(orig_addr, sdata);
805 mpath = mesh_path_lookup(orig_addr, sdata);
806 if (!mpath) {
807 rcu_read_unlock();
808 sdata->u.mesh.mshstats.dropped_frames_no_route++;
809 return;
810 }
Rui Paulo90a5e162009-11-11 00:01:31 +0000811 }
Javier Cardona5ee68e52011-08-09 16:45:08 -0700812
813 if ((!(mpath->flags & (MESH_PATH_ACTIVE | MESH_PATH_RESOLVING)) ||
814 time_after(jiffies, mpath->exp_time - 1*HZ)) &&
815 !(mpath->flags & MESH_PATH_FIXED)) {
816 mhwmp_dbg("%s time to refresh root mpath %pM", sdata->name,
817 orig_addr);
818 mesh_queue_preq(mpath, PREQ_Q_F_START | PREQ_Q_F_REFRESH);
819 }
820
Chun-Yeow Yeohd2a079f2012-03-23 18:48:51 +0800821 if ((SN_LT(mpath->sn, orig_sn) || (mpath->sn == orig_sn &&
822 metric < mpath->rann_metric)) && ifmsh->mshcfg.dot11MeshForwarding) {
Rui Paulo90a5e162009-11-11 00:01:31 +0000823 mesh_path_sel_frame_tx(MPATH_RANN, flags, orig_addr,
Rui Paulod19b3bf2009-11-09 23:46:55 +0000824 cpu_to_le32(orig_sn),
Johannes Berg15ff6362009-11-17 13:34:04 +0100825 0, NULL, 0, broadcast_addr,
Javier Cardona0507e152011-08-09 16:45:10 -0700826 hopcount, ttl, cpu_to_le32(interval),
Chun-Yeow Yeohd2a079f2012-03-23 18:48:51 +0800827 cpu_to_le32(metric + metric_txsta),
Rui Paulo90a5e162009-11-11 00:01:31 +0000828 0, sdata);
Rui Paulod19b3bf2009-11-09 23:46:55 +0000829 mpath->sn = orig_sn;
Chun-Yeow Yeohd2a079f2012-03-23 18:48:51 +0800830 mpath->rann_metric = metric + metric_txsta;
Chun-Yeow Yeoh35bcd592012-04-10 12:31:56 +0800831 /* Recording RANNs sender address to send individually
832 * addressed PREQs destined for root mesh STA */
833 memcpy(mpath->rann_snd_addr, mgmt->sa, ETH_ALEN);
Rui Paulo90a5e162009-11-11 00:01:31 +0000834 }
Chun-Yeow Yeoh3d045a52012-02-28 22:00:06 +0800835
Chun-Yeow Yeoh3d045a52012-02-28 22:00:06 +0800836 mpath->is_root = true;
837
Javier Cardona5ee68e52011-08-09 16:45:08 -0700838 if (root_is_gate)
839 mesh_path_add_gate(mpath);
840
Rui Paulo90a5e162009-11-11 00:01:31 +0000841 rcu_read_unlock();
842}
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100843
844
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200845void mesh_rx_path_sel_frame(struct ieee80211_sub_if_data *sdata,
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100846 struct ieee80211_mgmt *mgmt,
847 size_t len)
848{
849 struct ieee802_11_elems elems;
850 size_t baselen;
851 u32 last_hop_metric;
Javier Cardona97091312011-10-06 14:54:22 -0700852 struct sta_info *sta;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100853
Johannes Berg9c80d3d2008-09-08 15:41:59 +0200854 /* need action_code */
855 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
856 return;
857
Javier Cardona97091312011-10-06 14:54:22 -0700858 rcu_read_lock();
859 sta = sta_info_get(sdata, mgmt->sa);
860 if (!sta || sta->plink_state != NL80211_PLINK_ESTAB) {
861 rcu_read_unlock();
862 return;
863 }
864 rcu_read_unlock();
865
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100866 baselen = (u8 *) mgmt->u.action.u.mesh_action.variable - (u8 *) mgmt;
867 ieee802_11_parse_elems(mgmt->u.action.u.mesh_action.variable,
868 len - baselen, &elems);
869
Rui Paulodbb81c42009-11-09 23:46:46 +0000870 if (elems.preq) {
871 if (elems.preq_len != 37)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100872 /* Right now we support just 1 destination and no AE */
873 return;
Rui Paulodbb81c42009-11-09 23:46:46 +0000874 last_hop_metric = hwmp_route_info_get(sdata, mgmt, elems.preq,
875 MPATH_PREQ);
876 if (last_hop_metric)
877 hwmp_preq_frame_process(sdata, mgmt, elems.preq,
878 last_hop_metric);
879 }
880 if (elems.prep) {
881 if (elems.prep_len != 31)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100882 /* Right now we support no AE */
883 return;
Rui Paulodbb81c42009-11-09 23:46:46 +0000884 last_hop_metric = hwmp_route_info_get(sdata, mgmt, elems.prep,
885 MPATH_PREP);
886 if (last_hop_metric)
887 hwmp_prep_frame_process(sdata, mgmt, elems.prep,
888 last_hop_metric);
889 }
890 if (elems.perr) {
Rui Paulod611f062009-11-09 23:46:50 +0000891 if (elems.perr_len != 15)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100892 /* Right now we support only one destination per PERR */
893 return;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200894 hwmp_perr_frame_process(sdata, mgmt, elems.perr);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100895 }
Rui Paulo90a5e162009-11-11 00:01:31 +0000896 if (elems.rann)
897 hwmp_rann_frame_process(sdata, mgmt, elems.rann);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100898}
899
900/**
901 * mesh_queue_preq - queue a PREQ to a given destination
902 *
903 * @mpath: mesh path to discover
904 * @flags: special attributes of the PREQ to be sent
905 *
906 * Locking: the function must be called from within a rcu read lock block.
907 *
908 */
909static void mesh_queue_preq(struct mesh_path *mpath, u8 flags)
910{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200911 struct ieee80211_sub_if_data *sdata = mpath->sdata;
Johannes Berg472dbc42008-09-11 00:01:49 +0200912 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100913 struct mesh_preq_queue *preq_node;
914
Andrey Yurovsky59615b52009-06-25 16:07:42 -0700915 preq_node = kmalloc(sizeof(struct mesh_preq_queue), GFP_ATOMIC);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100916 if (!preq_node) {
Javier Cardona76468872011-08-09 16:45:04 -0700917 mhwmp_dbg("could not allocate PREQ node");
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100918 return;
919 }
920
Baruch Siach987dafa2011-07-28 08:51:05 +0300921 spin_lock_bh(&ifmsh->mesh_preq_queue_lock);
Johannes Berg472dbc42008-09-11 00:01:49 +0200922 if (ifmsh->preq_queue_len == MAX_PREQ_QUEUE_LEN) {
Baruch Siach987dafa2011-07-28 08:51:05 +0300923 spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100924 kfree(preq_node);
925 if (printk_ratelimit())
Javier Cardona76468872011-08-09 16:45:04 -0700926 mhwmp_dbg("PREQ node queue full");
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100927 return;
928 }
929
Johannes Bergf2dc7982011-11-18 15:27:31 +0100930 spin_lock(&mpath->state_lock);
Javier Cardonaf3011cf2011-11-03 21:11:10 -0700931 if (mpath->flags & MESH_PATH_REQ_QUEUED) {
Johannes Bergf2dc7982011-11-18 15:27:31 +0100932 spin_unlock(&mpath->state_lock);
Javier Cardonaf3011cf2011-11-03 21:11:10 -0700933 spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
Dan Carpenter88d53462011-11-15 09:33:31 +0300934 kfree(preq_node);
Javier Cardonaf3011cf2011-11-03 21:11:10 -0700935 return;
936 }
937
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100938 memcpy(preq_node->dst, mpath->dst, ETH_ALEN);
939 preq_node->flags = flags;
940
Javier Cardonaf3011cf2011-11-03 21:11:10 -0700941 mpath->flags |= MESH_PATH_REQ_QUEUED;
Johannes Bergf2dc7982011-11-18 15:27:31 +0100942 spin_unlock(&mpath->state_lock);
Javier Cardonaf3011cf2011-11-03 21:11:10 -0700943
Johannes Berg472dbc42008-09-11 00:01:49 +0200944 list_add_tail(&preq_node->list, &ifmsh->preq_queue.list);
945 ++ifmsh->preq_queue_len;
Baruch Siach987dafa2011-07-28 08:51:05 +0300946 spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100947
Johannes Berg472dbc42008-09-11 00:01:49 +0200948 if (time_after(jiffies, ifmsh->last_preq + min_preq_int_jiff(sdata)))
Johannes Berg64592c82010-06-10 10:21:31 +0200949 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100950
Johannes Berg472dbc42008-09-11 00:01:49 +0200951 else if (time_before(jiffies, ifmsh->last_preq)) {
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100952 /* avoid long wait if did not send preqs for a long time
953 * and jiffies wrapped around
954 */
Johannes Berg472dbc42008-09-11 00:01:49 +0200955 ifmsh->last_preq = jiffies - min_preq_int_jiff(sdata) - 1;
Johannes Berg64592c82010-06-10 10:21:31 +0200956 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100957 } else
Johannes Berg472dbc42008-09-11 00:01:49 +0200958 mod_timer(&ifmsh->mesh_path_timer, ifmsh->last_preq +
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100959 min_preq_int_jiff(sdata));
960}
961
962/**
963 * mesh_path_start_discovery - launch a path discovery from the PREQ queue
964 *
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200965 * @sdata: local mesh subif
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100966 */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200967void mesh_path_start_discovery(struct ieee80211_sub_if_data *sdata)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100968{
Johannes Berg472dbc42008-09-11 00:01:49 +0200969 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100970 struct mesh_preq_queue *preq_node;
971 struct mesh_path *mpath;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000972 u8 ttl, target_flags;
Chun-Yeow Yeoh3d045a52012-02-28 22:00:06 +0800973 const u8 *da;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100974 u32 lifetime;
975
Johannes Berga43816d2009-07-10 11:39:26 +0200976 spin_lock_bh(&ifmsh->mesh_preq_queue_lock);
Johannes Berg472dbc42008-09-11 00:01:49 +0200977 if (!ifmsh->preq_queue_len ||
978 time_before(jiffies, ifmsh->last_preq +
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100979 min_preq_int_jiff(sdata))) {
Johannes Berga43816d2009-07-10 11:39:26 +0200980 spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100981 return;
982 }
983
Johannes Berg472dbc42008-09-11 00:01:49 +0200984 preq_node = list_first_entry(&ifmsh->preq_queue.list,
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100985 struct mesh_preq_queue, list);
986 list_del(&preq_node->list);
Johannes Berg472dbc42008-09-11 00:01:49 +0200987 --ifmsh->preq_queue_len;
Johannes Berga43816d2009-07-10 11:39:26 +0200988 spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100989
990 rcu_read_lock();
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200991 mpath = mesh_path_lookup(preq_node->dst, sdata);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100992 if (!mpath)
993 goto enddiscovery;
994
995 spin_lock_bh(&mpath->state_lock);
Javier Cardonaf3011cf2011-11-03 21:11:10 -0700996 mpath->flags &= ~MESH_PATH_REQ_QUEUED;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100997 if (preq_node->flags & PREQ_Q_F_START) {
998 if (mpath->flags & MESH_PATH_RESOLVING) {
999 spin_unlock_bh(&mpath->state_lock);
1000 goto enddiscovery;
1001 } else {
1002 mpath->flags &= ~MESH_PATH_RESOLVED;
1003 mpath->flags |= MESH_PATH_RESOLVING;
1004 mpath->discovery_retries = 0;
1005 mpath->discovery_timeout = disc_timeout_jiff(sdata);
1006 }
1007 } else if (!(mpath->flags & MESH_PATH_RESOLVING) ||
1008 mpath->flags & MESH_PATH_RESOLVED) {
1009 mpath->flags &= ~MESH_PATH_RESOLVING;
1010 spin_unlock_bh(&mpath->state_lock);
1011 goto enddiscovery;
1012 }
1013
Johannes Berg472dbc42008-09-11 00:01:49 +02001014 ifmsh->last_preq = jiffies;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001015
Rui Paulod19b3bf2009-11-09 23:46:55 +00001016 if (time_after(jiffies, ifmsh->last_sn_update +
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001017 net_traversal_jiffies(sdata)) ||
Rui Paulod19b3bf2009-11-09 23:46:55 +00001018 time_before(jiffies, ifmsh->last_sn_update)) {
1019 ++ifmsh->sn;
1020 sdata->u.mesh.last_sn_update = jiffies;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001021 }
1022 lifetime = default_lifetime(sdata);
Javier Cardona45904f22010-12-03 09:20:40 +01001023 ttl = sdata->u.mesh.mshcfg.element_ttl;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001024 if (ttl == 0) {
Johannes Berg472dbc42008-09-11 00:01:49 +02001025 sdata->u.mesh.mshstats.dropped_frames_ttl++;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001026 spin_unlock_bh(&mpath->state_lock);
1027 goto enddiscovery;
1028 }
1029
1030 if (preq_node->flags & PREQ_Q_F_REFRESH)
Rui Paulod19b3bf2009-11-09 23:46:55 +00001031 target_flags = MP_F_DO;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001032 else
Rui Paulod19b3bf2009-11-09 23:46:55 +00001033 target_flags = MP_F_RF;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001034
1035 spin_unlock_bh(&mpath->state_lock);
Chun-Yeow Yeoh3d045a52012-02-28 22:00:06 +08001036 da = (mpath->is_root) ? mpath->rann_snd_addr : broadcast_addr;
Johannes Berg47846c92009-11-25 17:46:19 +01001037 mesh_path_sel_frame_tx(MPATH_PREQ, 0, sdata->vif.addr,
Rui Paulod19b3bf2009-11-09 23:46:55 +00001038 cpu_to_le32(ifmsh->sn), target_flags, mpath->dst,
Chun-Yeow Yeoh3d045a52012-02-28 22:00:06 +08001039 cpu_to_le32(mpath->sn), da, 0,
Luis Carlos Coboaa2b5922008-02-29 14:30:32 -08001040 ttl, cpu_to_le32(lifetime), 0,
Johannes Berg472dbc42008-09-11 00:01:49 +02001041 cpu_to_le32(ifmsh->preq_id++), sdata);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001042 mod_timer(&mpath->timer, jiffies + mpath->discovery_timeout);
1043
1044enddiscovery:
1045 rcu_read_unlock();
1046 kfree(preq_node);
1047}
1048
Thomas Pedersen0cfda852011-11-24 17:15:25 -08001049/* mesh_nexthop_resolve - lookup next hop for given skb and start path
1050 * discovery if no forwarding information is found.
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001051 *
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02001052 * @skb: 802.11 frame to be sent
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001053 * @sdata: network subif the frame will be sent through
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001054 *
Thomas Pedersen0cfda852011-11-24 17:15:25 -08001055 * Returns: 0 if the next hop was found and -ENOENT if the frame was queued.
1056 * skb is freeed here if no mpath could be allocated.
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001057 */
Thomas Pedersen0cfda852011-11-24 17:15:25 -08001058int mesh_nexthop_resolve(struct sk_buff *skb,
1059 struct ieee80211_sub_if_data *sdata)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001060{
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02001061 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
Thomas Pedersen0cfda852011-11-24 17:15:25 -08001062 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1063 struct mesh_path *mpath;
1064 struct sk_buff *skb_to_free = NULL;
Rui Paulod19b3bf2009-11-09 23:46:55 +00001065 u8 *target_addr = hdr->addr3;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001066 int err = 0;
1067
1068 rcu_read_lock();
Thomas Pedersen0cfda852011-11-24 17:15:25 -08001069 err = mesh_nexthop_lookup(skb, sdata);
1070 if (!err)
1071 goto endlookup;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001072
Thomas Pedersen0cfda852011-11-24 17:15:25 -08001073 /* no nexthop found, start resolving */
1074 mpath = mesh_path_lookup(target_addr, sdata);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001075 if (!mpath) {
Rui Paulod19b3bf2009-11-09 23:46:55 +00001076 mesh_path_add(target_addr, sdata);
1077 mpath = mesh_path_lookup(target_addr, sdata);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001078 if (!mpath) {
Thomas Pedersen0cfda852011-11-24 17:15:25 -08001079 mesh_path_discard_frame(skb, sdata);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001080 err = -ENOSPC;
1081 goto endlookup;
1082 }
1083 }
1084
Thomas Pedersen0cfda852011-11-24 17:15:25 -08001085 if (!(mpath->flags & MESH_PATH_RESOLVING))
1086 mesh_queue_preq(mpath, PREQ_Q_F_START);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001087
Thomas Pedersen0cfda852011-11-24 17:15:25 -08001088 if (skb_queue_len(&mpath->frame_queue) >= MESH_FRAME_QUEUE_LEN)
1089 skb_to_free = skb_dequeue(&mpath->frame_queue);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001090
Thomas Pedersen0cfda852011-11-24 17:15:25 -08001091 info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
1092 ieee80211_set_qos_hdr(sdata, skb);
1093 skb_queue_tail(&mpath->frame_queue, skb);
1094 err = -ENOENT;
1095 if (skb_to_free)
1096 mesh_path_discard_frame(skb_to_free, sdata);
1097
1098endlookup:
1099 rcu_read_unlock();
1100 return err;
1101}
1102/**
1103 * mesh_nexthop_lookup - put the appropriate next hop on a mesh frame. Calling
1104 * this function is considered "using" the associated mpath, so preempt a path
1105 * refresh if this mpath expires soon.
1106 *
1107 * @skb: 802.11 frame to be sent
1108 * @sdata: network subif the frame will be sent through
1109 *
1110 * Returns: 0 if the next hop was found. Nonzero otherwise.
1111 */
1112int mesh_nexthop_lookup(struct sk_buff *skb,
1113 struct ieee80211_sub_if_data *sdata)
1114{
1115 struct mesh_path *mpath;
1116 struct sta_info *next_hop;
1117 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1118 u8 *target_addr = hdr->addr3;
1119 int err = -ENOENT;
1120
1121 rcu_read_lock();
1122 mpath = mesh_path_lookup(target_addr, sdata);
1123
1124 if (!mpath || !(mpath->flags & MESH_PATH_ACTIVE))
1125 goto endlookup;
1126
1127 if (time_after(jiffies,
1128 mpath->exp_time -
1129 msecs_to_jiffies(sdata->u.mesh.mshcfg.path_refresh_time)) &&
Joe Perchesb203ca32012-05-08 18:56:52 +00001130 ether_addr_equal(sdata->vif.addr, hdr->addr4) &&
Thomas Pedersen0cfda852011-11-24 17:15:25 -08001131 !(mpath->flags & MESH_PATH_RESOLVING) &&
1132 !(mpath->flags & MESH_PATH_FIXED))
1133 mesh_queue_preq(mpath, PREQ_Q_F_START | PREQ_Q_F_REFRESH);
1134
1135 next_hop = rcu_dereference(mpath->next_hop);
1136 if (next_hop) {
1137 memcpy(hdr->addr1, next_hop->sta.addr, ETH_ALEN);
1138 memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN);
1139 err = 0;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001140 }
1141
1142endlookup:
1143 rcu_read_unlock();
1144 return err;
1145}
1146
1147void mesh_path_timer(unsigned long data)
1148{
Johannes Bergdea40962011-05-12 15:03:32 +02001149 struct mesh_path *mpath = (void *) data;
1150 struct ieee80211_sub_if_data *sdata = mpath->sdata;
Javier Cardona5ee68e52011-08-09 16:45:08 -07001151 int ret;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001152
Johannes Bergdea40962011-05-12 15:03:32 +02001153 if (sdata->local->quiescing)
Johannes Berg5bb644a2009-05-17 11:40:42 +02001154 return;
Johannes Berg5bb644a2009-05-17 11:40:42 +02001155
1156 spin_lock_bh(&mpath->state_lock);
Luis Carlos Cobocfa22c72008-02-29 15:04:13 -08001157 if (mpath->flags & MESH_PATH_RESOLVED ||
Javier Cardona5ee68e52011-08-09 16:45:08 -07001158 (!(mpath->flags & MESH_PATH_RESOLVING))) {
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001159 mpath->flags &= ~(MESH_PATH_RESOLVING | MESH_PATH_RESOLVED);
Javier Cardona5ee68e52011-08-09 16:45:08 -07001160 spin_unlock_bh(&mpath->state_lock);
1161 } else if (mpath->discovery_retries < max_preq_retries(sdata)) {
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001162 ++mpath->discovery_retries;
1163 mpath->discovery_timeout *= 2;
Javier Cardonaf3011cf2011-11-03 21:11:10 -07001164 mpath->flags &= ~MESH_PATH_REQ_QUEUED;
Javier Cardona5ee68e52011-08-09 16:45:08 -07001165 spin_unlock_bh(&mpath->state_lock);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001166 mesh_queue_preq(mpath, 0);
1167 } else {
1168 mpath->flags = 0;
1169 mpath->exp_time = jiffies;
Javier Cardona5ee68e52011-08-09 16:45:08 -07001170 spin_unlock_bh(&mpath->state_lock);
1171 if (!mpath->is_gate && mesh_gate_num(sdata) > 0) {
1172 ret = mesh_path_send_to_gates(mpath);
1173 if (ret)
1174 mhwmp_dbg("no gate was reachable");
1175 } else
1176 mesh_path_flush_pending(mpath);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001177 }
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001178}
Rui Pauloe304bfd2009-11-09 23:46:56 +00001179
1180void
1181mesh_path_tx_root_frame(struct ieee80211_sub_if_data *sdata)
1182{
1183 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
Javier Cardona0507e152011-08-09 16:45:10 -07001184 u32 interval = ifmsh->mshcfg.dot11MeshHWMPRannInterval;
Chun-Yeow Yeoha69cc442012-06-14 02:06:07 +08001185 u8 flags, target_flags = 0;
Rui Pauloe304bfd2009-11-09 23:46:56 +00001186
Javier Cardona16dd7262011-08-09 16:45:11 -07001187 flags = (ifmsh->mshcfg.dot11MeshGateAnnouncementProtocol)
1188 ? RANN_FLAG_IS_GATE : 0;
Chun-Yeow Yeoha69cc442012-06-14 02:06:07 +08001189
1190 switch (ifmsh->mshcfg.dot11MeshHWMPRootMode) {
1191 case IEEE80211_PROACTIVE_RANN:
1192 mesh_path_sel_frame_tx(MPATH_RANN, flags, sdata->vif.addr,
Rui Pauloe304bfd2009-11-09 23:46:56 +00001193 cpu_to_le32(++ifmsh->sn),
Johannes Berg15ff6362009-11-17 13:34:04 +01001194 0, NULL, 0, broadcast_addr,
Chun-Yeow Yeoha69cc442012-06-14 02:06:07 +08001195 0, ifmsh->mshcfg.element_ttl,
Javier Cardona0507e152011-08-09 16:45:10 -07001196 cpu_to_le32(interval), 0, 0, sdata);
Chun-Yeow Yeoha69cc442012-06-14 02:06:07 +08001197 break;
1198 case IEEE80211_PROACTIVE_PREQ_WITH_PREP:
1199 flags |= IEEE80211_PREQ_PROACTIVE_PREP_FLAG;
1200 case IEEE80211_PROACTIVE_PREQ_NO_PREP:
1201 interval = ifmsh->mshcfg.dot11MeshHWMPactivePathToRootTimeout;
1202 target_flags |= IEEE80211_PREQ_TO_FLAG |
1203 IEEE80211_PREQ_USN_FLAG;
1204 mesh_path_sel_frame_tx(MPATH_PREQ, flags, sdata->vif.addr,
1205 cpu_to_le32(++ifmsh->sn), target_flags,
1206 (u8 *) broadcast_addr, 0, broadcast_addr,
1207 0, ifmsh->mshcfg.element_ttl,
1208 cpu_to_le32(interval),
1209 0, cpu_to_le32(ifmsh->preq_id++), sdata);
1210 break;
1211 default:
1212 mhwmp_dbg("Proactive mechanism not supported");
1213 return;
1214 }
Rui Pauloe304bfd2009-11-09 23:46:56 +00001215}