blob: 9c3c0b86a740bfdd0bf611d3fe35d8b44d841683 [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>
Luis Carlos Cobo050ac522008-02-23 15:17:15 +010011#include "mesh.h"
12
Rui Paulo27db2e42009-11-09 23:46:45 +000013#ifdef CONFIG_MAC80211_VERBOSE_MHWMP_DEBUG
14#define mhwmp_dbg(fmt, args...) printk(KERN_DEBUG "Mesh HWMP: " fmt, ##args)
15#else
16#define mhwmp_dbg(fmt, args...) do { (void)(0); } while (0)
17#endif
18
Luis Carlos Cobo050ac522008-02-23 15:17:15 +010019#define TEST_FRAME_LEN 8192
20#define MAX_METRIC 0xffffffff
21#define ARITH_SHIFT 8
22
23/* Number of frames buffered per destination for unresolved destinations */
24#define MESH_FRAME_QUEUE_LEN 10
25#define MAX_PREQ_QUEUE_LEN 64
26
27/* Destination only */
28#define MP_F_DO 0x1
29/* Reply and forward */
30#define MP_F_RF 0x2
Rui Paulod611f062009-11-09 23:46:50 +000031/* Unknown Sequence Number */
32#define MP_F_USN 0x01
33/* Reason code Present */
34#define MP_F_RCODE 0x02
Luis Carlos Cobo050ac522008-02-23 15:17:15 +010035
Rui Paulo90a5e162009-11-11 00:01:31 +000036static void mesh_queue_preq(struct mesh_path *, u8);
37
Luis Carlos Coboa00de5d2008-02-29 17:07:54 -080038static inline u32 u32_field_get(u8 *preq_elem, int offset, bool ae)
39{
40 if (ae)
41 offset += 6;
Harvey Harrisonae7245c2008-05-01 22:19:33 -070042 return get_unaligned_le32(preq_elem + offset);
Luis Carlos Coboa00de5d2008-02-29 17:07:54 -080043}
44
Rui Paulod611f062009-11-09 23:46:50 +000045static inline u32 u16_field_get(u8 *preq_elem, int offset, bool ae)
46{
47 if (ae)
48 offset += 6;
49 return get_unaligned_le16(preq_elem + offset);
50}
51
Luis Carlos Cobo050ac522008-02-23 15:17:15 +010052/* HWMP IE processing macros */
Luis Carlos Coboa00de5d2008-02-29 17:07:54 -080053#define AE_F (1<<6)
54#define AE_F_SET(x) (*x & AE_F)
55#define PREQ_IE_FLAGS(x) (*(x))
56#define PREQ_IE_HOPCOUNT(x) (*(x + 1))
57#define PREQ_IE_TTL(x) (*(x + 2))
58#define PREQ_IE_PREQ_ID(x) u32_field_get(x, 3, 0)
59#define PREQ_IE_ORIG_ADDR(x) (x + 7)
Phil Carmody497888c2011-07-14 15:07:13 +030060#define PREQ_IE_ORIG_SN(x) u32_field_get(x, 13, 0)
61#define PREQ_IE_LIFETIME(x) u32_field_get(x, 17, AE_F_SET(x))
62#define PREQ_IE_METRIC(x) u32_field_get(x, 21, AE_F_SET(x))
Rui Paulod19b3bf2009-11-09 23:46:55 +000063#define PREQ_IE_TARGET_F(x) (*(AE_F_SET(x) ? x + 32 : x + 26))
64#define PREQ_IE_TARGET_ADDR(x) (AE_F_SET(x) ? x + 33 : x + 27)
Phil Carmody497888c2011-07-14 15:07:13 +030065#define PREQ_IE_TARGET_SN(x) u32_field_get(x, 33, AE_F_SET(x))
Luis Carlos Cobo050ac522008-02-23 15:17:15 +010066
67
Luis Carlos Coboa00de5d2008-02-29 17:07:54 -080068#define PREP_IE_FLAGS(x) PREQ_IE_FLAGS(x)
69#define PREP_IE_HOPCOUNT(x) PREQ_IE_HOPCOUNT(x)
70#define PREP_IE_TTL(x) PREQ_IE_TTL(x)
Thomas Pedersen25d49e42011-08-11 19:35:15 -070071#define PREP_IE_ORIG_ADDR(x) (AE_F_SET(x) ? x + 27 : x + 21)
72#define PREP_IE_ORIG_SN(x) u32_field_get(x, 27, AE_F_SET(x))
Phil Carmody497888c2011-07-14 15:07:13 +030073#define PREP_IE_LIFETIME(x) u32_field_get(x, 13, AE_F_SET(x))
74#define PREP_IE_METRIC(x) u32_field_get(x, 17, AE_F_SET(x))
Thomas Pedersen25d49e42011-08-11 19:35:15 -070075#define PREP_IE_TARGET_ADDR(x) (x + 3)
76#define PREP_IE_TARGET_SN(x) u32_field_get(x, 9, 0)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +010077
Rui Paulod611f062009-11-09 23:46:50 +000078#define PERR_IE_TTL(x) (*(x))
Rui Paulod19b3bf2009-11-09 23:46:55 +000079#define PERR_IE_TARGET_FLAGS(x) (*(x + 2))
80#define PERR_IE_TARGET_ADDR(x) (x + 3)
Phil Carmody497888c2011-07-14 15:07:13 +030081#define PERR_IE_TARGET_SN(x) u32_field_get(x, 9, 0)
82#define PERR_IE_TARGET_RCODE(x) u16_field_get(x, 13, 0)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +010083
Luis Carlos Cobo050ac522008-02-23 15:17:15 +010084#define MSEC_TO_TU(x) (x*1000/1024)
Rui Paulod19b3bf2009-11-09 23:46:55 +000085#define SN_GT(x, y) ((long) (y) - (long) (x) < 0)
86#define SN_LT(x, y) ((long) (x) - (long) (y) < 0)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +010087
88#define net_traversal_jiffies(s) \
Johannes Berg472dbc42008-09-11 00:01:49 +020089 msecs_to_jiffies(s->u.mesh.mshcfg.dot11MeshHWMPnetDiameterTraversalTime)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +010090#define default_lifetime(s) \
Johannes Berg472dbc42008-09-11 00:01:49 +020091 MSEC_TO_TU(s->u.mesh.mshcfg.dot11MeshHWMPactivePathTimeout)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +010092#define min_preq_int_jiff(s) \
Johannes Berg472dbc42008-09-11 00:01:49 +020093 (msecs_to_jiffies(s->u.mesh.mshcfg.dot11MeshHWMPpreqMinInterval))
94#define max_preq_retries(s) (s->u.mesh.mshcfg.dot11MeshHWMPmaxPREQretries)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +010095#define disc_timeout_jiff(s) \
Johannes Berg472dbc42008-09-11 00:01:49 +020096 msecs_to_jiffies(sdata->u.mesh.mshcfg.min_discovery_timeout)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +010097
98enum mpath_frame_type {
99 MPATH_PREQ = 0,
100 MPATH_PREP,
Rui Paulo90a5e162009-11-11 00:01:31 +0000101 MPATH_PERR,
102 MPATH_RANN
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100103};
104
Johannes Berg15ff6362009-11-17 13:34:04 +0100105static const u8 broadcast_addr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
106
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100107static int mesh_path_sel_frame_tx(enum mpath_frame_type action, u8 flags,
Rui Paulod19b3bf2009-11-09 23:46:55 +0000108 u8 *orig_addr, __le32 orig_sn, u8 target_flags, u8 *target,
Johannes Berg15ff6362009-11-17 13:34:04 +0100109 __le32 target_sn, const u8 *da, u8 hop_count, u8 ttl,
110 __le32 lifetime, __le32 metric, __le32 preq_id,
Rui Paulod19b3bf2009-11-09 23:46:55 +0000111 struct ieee80211_sub_if_data *sdata)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100112{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200113 struct ieee80211_local *local = sdata->local;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100114 struct sk_buff *skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400);
115 struct ieee80211_mgmt *mgmt;
116 u8 *pos;
117 int ie_len;
118
119 if (!skb)
120 return -1;
121 skb_reserve(skb, local->hw.extra_tx_headroom);
122 /* 25 is the size of the common mgmt part (24) plus the size of the
123 * common action part (1)
124 */
125 mgmt = (struct ieee80211_mgmt *)
126 skb_put(skb, 25 + sizeof(mgmt->u.action.u.mesh_action));
127 memset(mgmt, 0, 25 + sizeof(mgmt->u.action.u.mesh_action));
Harvey Harrisone7827a72008-07-15 18:44:13 -0700128 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
129 IEEE80211_STYPE_ACTION);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100130
131 memcpy(mgmt->da, da, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +0100132 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
Rui Paulo90a5e162009-11-11 00:01:31 +0000133 /* BSSID == SA */
Johannes Berg47846c92009-11-25 17:46:19 +0100134 memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
Thomas Pedersen25d49e42011-08-11 19:35:15 -0700135 mgmt->u.action.category = WLAN_CATEGORY_MESH_ACTION;
136 mgmt->u.action.u.mesh_action.action_code =
137 WLAN_MESH_ACTION_HWMP_PATH_SELECTION;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100138
139 switch (action) {
140 case MPATH_PREQ:
Rui Paulod19b3bf2009-11-09 23:46:55 +0000141 mhwmp_dbg("sending PREQ to %pM\n", target);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100142 ie_len = 37;
143 pos = skb_put(skb, 2 + ie_len);
144 *pos++ = WLAN_EID_PREQ;
145 break;
146 case MPATH_PREP:
Rui Paulod19b3bf2009-11-09 23:46:55 +0000147 mhwmp_dbg("sending PREP to %pM\n", target);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100148 ie_len = 31;
149 pos = skb_put(skb, 2 + ie_len);
150 *pos++ = WLAN_EID_PREP;
151 break;
Rui Paulo90a5e162009-11-11 00:01:31 +0000152 case MPATH_RANN:
153 mhwmp_dbg("sending RANN from %pM\n", orig_addr);
154 ie_len = sizeof(struct ieee80211_rann_ie);
155 pos = skb_put(skb, 2 + ie_len);
156 *pos++ = WLAN_EID_RANN;
157 break;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100158 default:
Patrick McHardy812714d2008-05-06 12:52:07 +0200159 kfree_skb(skb);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100160 return -ENOTSUPP;
161 break;
162 }
163 *pos++ = ie_len;
164 *pos++ = flags;
165 *pos++ = hop_count;
166 *pos++ = ttl;
Thomas Pedersen25d49e42011-08-11 19:35:15 -0700167 if (action == MPATH_PREP) {
Rui Paulod19b3bf2009-11-09 23:46:55 +0000168 memcpy(pos, target, ETH_ALEN);
Rui Paulo90a5e162009-11-11 00:01:31 +0000169 pos += ETH_ALEN;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000170 memcpy(pos, &target_sn, 4);
Thomas Pedersen25d49e42011-08-11 19:35:15 -0700171 pos += 4;
172 } else {
173 if (action == MPATH_PREQ) {
174 memcpy(pos, &preq_id, 4);
175 pos += 4;
176 }
177 memcpy(pos, orig_addr, ETH_ALEN);
178 pos += ETH_ALEN;
179 memcpy(pos, &orig_sn, 4);
180 pos += 4;
181 }
182 memcpy(pos, &lifetime, 4); /* interval for RANN */
183 pos += 4;
184 memcpy(pos, &metric, 4);
185 pos += 4;
186 if (action == MPATH_PREQ) {
187 *pos++ = 1; /* destination count */
188 *pos++ = target_flags;
189 memcpy(pos, target, ETH_ALEN);
190 pos += ETH_ALEN;
191 memcpy(pos, &target_sn, 4);
192 pos += 4;
193 } else if (action == MPATH_PREP) {
194 memcpy(pos, orig_addr, ETH_ALEN);
195 pos += ETH_ALEN;
196 memcpy(pos, &orig_sn, 4);
197 pos += 4;
Rui Paulo90a5e162009-11-11 00:01:31 +0000198 }
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100199
Johannes Berg62ae67b2009-11-18 18:42:05 +0100200 ieee80211_tx_skb(sdata, skb);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100201 return 0;
202}
203
204/**
205 * mesh_send_path error - Sends a PERR mesh management frame
206 *
Rui Paulod19b3bf2009-11-09 23:46:55 +0000207 * @target: broken destination
208 * @target_sn: SN of the broken destination
209 * @target_rcode: reason code for this PERR
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100210 * @ra: node this frame is addressed to
211 */
Rui Paulod19b3bf2009-11-09 23:46:55 +0000212int mesh_path_error_tx(u8 ttl, u8 *target, __le32 target_sn,
Johannes Berg15ff6362009-11-17 13:34:04 +0100213 __le16 target_rcode, const u8 *ra,
214 struct ieee80211_sub_if_data *sdata)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100215{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200216 struct ieee80211_local *local = sdata->local;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100217 struct sk_buff *skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400);
218 struct ieee80211_mgmt *mgmt;
219 u8 *pos;
220 int ie_len;
221
222 if (!skb)
223 return -1;
224 skb_reserve(skb, local->hw.extra_tx_headroom);
225 /* 25 is the size of the common mgmt part (24) plus the size of the
226 * common action part (1)
227 */
228 mgmt = (struct ieee80211_mgmt *)
229 skb_put(skb, 25 + sizeof(mgmt->u.action.u.mesh_action));
230 memset(mgmt, 0, 25 + sizeof(mgmt->u.action.u.mesh_action));
Harvey Harrisone7827a72008-07-15 18:44:13 -0700231 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
232 IEEE80211_STYPE_ACTION);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100233
234 memcpy(mgmt->da, ra, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +0100235 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
Thomas Pedersen25d49e42011-08-11 19:35:15 -0700236 /* BSSID == SA */
237 memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
238 mgmt->u.action.category = WLAN_CATEGORY_MESH_ACTION;
239 mgmt->u.action.u.mesh_action.action_code =
240 WLAN_MESH_ACTION_HWMP_PATH_SELECTION;
Rui Paulod611f062009-11-09 23:46:50 +0000241 ie_len = 15;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100242 pos = skb_put(skb, 2 + ie_len);
243 *pos++ = WLAN_EID_PERR;
244 *pos++ = ie_len;
Rui Paulod611f062009-11-09 23:46:50 +0000245 /* ttl */
Javier Cardona45904f22010-12-03 09:20:40 +0100246 *pos++ = ttl;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100247 /* number of destinations */
248 *pos++ = 1;
Rui Paulod611f062009-11-09 23:46:50 +0000249 /*
250 * flags bit, bit 1 is unset if we know the sequence number and
251 * bit 2 is set if we have a reason code
252 */
253 *pos = 0;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000254 if (!target_sn)
Rui Paulod611f062009-11-09 23:46:50 +0000255 *pos |= MP_F_USN;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000256 if (target_rcode)
Rui Paulod611f062009-11-09 23:46:50 +0000257 *pos |= MP_F_RCODE;
258 pos++;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000259 memcpy(pos, target, ETH_ALEN);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100260 pos += ETH_ALEN;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000261 memcpy(pos, &target_sn, 4);
Rui Paulod611f062009-11-09 23:46:50 +0000262 pos += 4;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000263 memcpy(pos, &target_rcode, 2);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100264
Johannes Berg62ae67b2009-11-18 18:42:05 +0100265 ieee80211_tx_skb(sdata, skb);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100266 return 0;
267}
268
Javier Cardonabfc32e62009-08-17 17:15:55 -0700269void ieee80211s_update_metric(struct ieee80211_local *local,
270 struct sta_info *stainfo, struct sk_buff *skb)
271{
272 struct ieee80211_tx_info *txinfo = IEEE80211_SKB_CB(skb);
273 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
274 int failed;
275
276 if (!ieee80211_is_data(hdr->frame_control))
277 return;
278
279 failed = !(txinfo->flags & IEEE80211_TX_STAT_ACK);
280
281 /* moving average, scaled to 100 */
282 stainfo->fail_avg = ((80 * stainfo->fail_avg + 5) / 100 + 20 * failed);
283 if (stainfo->fail_avg > 95)
284 mesh_plink_broken(stainfo);
285}
286
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100287static u32 airtime_link_metric_get(struct ieee80211_local *local,
288 struct sta_info *sta)
289{
290 struct ieee80211_supported_band *sband;
291 /* This should be adjusted for each device */
292 int device_constant = 1 << ARITH_SHIFT;
293 int test_frame_len = TEST_FRAME_LEN << ARITH_SHIFT;
294 int s_unit = 1 << ARITH_SHIFT;
295 int rate, err;
296 u32 tx_time, estimated_retx;
297 u64 result;
298
299 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
300
301 if (sta->fail_avg >= 100)
302 return MAX_METRIC;
Johannes Berge6a98542008-10-21 12:40:02 +0200303
304 if (sta->last_tx_rate.flags & IEEE80211_TX_RC_MCS)
305 return MAX_METRIC;
306
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100307 err = (sta->fail_avg << ARITH_SHIFT) / 100;
308
309 /* bitrate is in units of 100 Kbps, while we need rate in units of
310 * 1Mbps. This will be corrected on tx_time computation.
311 */
Johannes Berge6a98542008-10-21 12:40:02 +0200312 rate = sband->bitrates[sta->last_tx_rate.idx].bitrate;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100313 tx_time = (device_constant + 10 * test_frame_len / rate);
314 estimated_retx = ((1 << (2 * ARITH_SHIFT)) / (s_unit - err));
315 result = (tx_time * estimated_retx) >> (2 * ARITH_SHIFT) ;
316 return (u32)result;
317}
318
319/**
320 * hwmp_route_info_get - Update routing info to originator and transmitter
321 *
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200322 * @sdata: local mesh subif
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100323 * @mgmt: mesh management frame
324 * @hwmp_ie: hwmp information element (PREP or PREQ)
325 *
326 * This function updates the path routing information to the originator and the
Andrey Yurovskyf99288d2009-10-20 12:17:34 -0700327 * transmitter of a HWMP PREQ or PREP frame.
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100328 *
329 * Returns: metric to frame originator or 0 if the frame should not be further
330 * processed
331 *
332 * Notes: this function is the only place (besides user-provided info) where
333 * path routing information is updated.
334 */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200335static u32 hwmp_route_info_get(struct ieee80211_sub_if_data *sdata,
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100336 struct ieee80211_mgmt *mgmt,
Rui Paulodbb81c42009-11-09 23:46:46 +0000337 u8 *hwmp_ie, enum mpath_frame_type action)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100338{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200339 struct ieee80211_local *local = sdata->local;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100340 struct mesh_path *mpath;
341 struct sta_info *sta;
342 bool fresh_info;
343 u8 *orig_addr, *ta;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000344 u32 orig_sn, orig_metric;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100345 unsigned long orig_lifetime, exp_time;
346 u32 last_hop_metric, new_metric;
347 bool process = true;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100348
349 rcu_read_lock();
Johannes Bergabe60632009-11-25 17:46:18 +0100350 sta = sta_info_get(sdata, mgmt->sa);
Johannes Bergdc0b0f72008-02-23 15:17:20 +0100351 if (!sta) {
352 rcu_read_unlock();
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100353 return 0;
Johannes Bergdc0b0f72008-02-23 15:17:20 +0100354 }
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100355
356 last_hop_metric = airtime_link_metric_get(local, sta);
357 /* Update and check originator routing info */
358 fresh_info = true;
359
360 switch (action) {
361 case MPATH_PREQ:
362 orig_addr = PREQ_IE_ORIG_ADDR(hwmp_ie);
Rui Paulod19b3bf2009-11-09 23:46:55 +0000363 orig_sn = PREQ_IE_ORIG_SN(hwmp_ie);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100364 orig_lifetime = PREQ_IE_LIFETIME(hwmp_ie);
365 orig_metric = PREQ_IE_METRIC(hwmp_ie);
366 break;
367 case MPATH_PREP:
368 /* Originator here refers to the MP that was the destination in
369 * the Path Request. The draft refers to that MP as the
370 * destination address, even though usually it is the origin of
371 * the PREP frame. We divert from the nomenclature in the draft
372 * so that we can easily use a single function to gather path
373 * information from both PREQ and PREP frames.
374 */
375 orig_addr = PREP_IE_ORIG_ADDR(hwmp_ie);
Rui Paulod19b3bf2009-11-09 23:46:55 +0000376 orig_sn = PREP_IE_ORIG_SN(hwmp_ie);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100377 orig_lifetime = PREP_IE_LIFETIME(hwmp_ie);
378 orig_metric = PREP_IE_METRIC(hwmp_ie);
379 break;
380 default:
Johannes Bergdc0b0f72008-02-23 15:17:20 +0100381 rcu_read_unlock();
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100382 return 0;
383 }
384 new_metric = orig_metric + last_hop_metric;
385 if (new_metric < orig_metric)
386 new_metric = MAX_METRIC;
387 exp_time = TU_TO_EXP_TIME(orig_lifetime);
388
Johannes Berg47846c92009-11-25 17:46:19 +0100389 if (memcmp(orig_addr, sdata->vif.addr, ETH_ALEN) == 0) {
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100390 /* This MP is the originator, we are not interested in this
391 * frame, except for updating transmitter's path info.
392 */
393 process = false;
394 fresh_info = false;
395 } else {
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200396 mpath = mesh_path_lookup(orig_addr, sdata);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100397 if (mpath) {
398 spin_lock_bh(&mpath->state_lock);
399 if (mpath->flags & MESH_PATH_FIXED)
400 fresh_info = false;
401 else if ((mpath->flags & MESH_PATH_ACTIVE) &&
Rui Paulod19b3bf2009-11-09 23:46:55 +0000402 (mpath->flags & MESH_PATH_SN_VALID)) {
403 if (SN_GT(mpath->sn, orig_sn) ||
404 (mpath->sn == orig_sn &&
Porsch, Marco533866b2010-02-24 09:53:13 +0100405 new_metric >= mpath->metric)) {
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100406 process = false;
407 fresh_info = false;
408 }
409 }
410 } else {
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200411 mesh_path_add(orig_addr, sdata);
412 mpath = mesh_path_lookup(orig_addr, sdata);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100413 if (!mpath) {
414 rcu_read_unlock();
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100415 return 0;
416 }
417 spin_lock_bh(&mpath->state_lock);
418 }
419
420 if (fresh_info) {
421 mesh_path_assign_nexthop(mpath, sta);
Rui Paulod19b3bf2009-11-09 23:46:55 +0000422 mpath->flags |= MESH_PATH_SN_VALID;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100423 mpath->metric = new_metric;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000424 mpath->sn = orig_sn;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100425 mpath->exp_time = time_after(mpath->exp_time, exp_time)
426 ? mpath->exp_time : exp_time;
427 mesh_path_activate(mpath);
428 spin_unlock_bh(&mpath->state_lock);
429 mesh_path_tx_pending(mpath);
430 /* draft says preq_id should be saved to, but there does
431 * not seem to be any use for it, skipping by now
432 */
433 } else
434 spin_unlock_bh(&mpath->state_lock);
435 }
436
437 /* Update and check transmitter routing info */
438 ta = mgmt->sa;
439 if (memcmp(orig_addr, ta, ETH_ALEN) == 0)
440 fresh_info = false;
441 else {
442 fresh_info = true;
443
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200444 mpath = mesh_path_lookup(ta, sdata);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100445 if (mpath) {
446 spin_lock_bh(&mpath->state_lock);
447 if ((mpath->flags & MESH_PATH_FIXED) ||
448 ((mpath->flags & MESH_PATH_ACTIVE) &&
449 (last_hop_metric > mpath->metric)))
450 fresh_info = false;
451 } else {
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200452 mesh_path_add(ta, sdata);
453 mpath = mesh_path_lookup(ta, sdata);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100454 if (!mpath) {
455 rcu_read_unlock();
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100456 return 0;
457 }
458 spin_lock_bh(&mpath->state_lock);
459 }
460
461 if (fresh_info) {
462 mesh_path_assign_nexthop(mpath, sta);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100463 mpath->metric = last_hop_metric;
464 mpath->exp_time = time_after(mpath->exp_time, exp_time)
465 ? mpath->exp_time : exp_time;
466 mesh_path_activate(mpath);
467 spin_unlock_bh(&mpath->state_lock);
468 mesh_path_tx_pending(mpath);
469 } else
470 spin_unlock_bh(&mpath->state_lock);
471 }
472
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100473 rcu_read_unlock();
474
475 return process ? new_metric : 0;
476}
477
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200478static void hwmp_preq_frame_process(struct ieee80211_sub_if_data *sdata,
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100479 struct ieee80211_mgmt *mgmt,
David Woo57ef5dd2009-08-12 11:03:43 -0700480 u8 *preq_elem, u32 metric)
481{
Johannes Berg472dbc42008-09-11 00:01:49 +0200482 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100483 struct mesh_path *mpath;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000484 u8 *target_addr, *orig_addr;
485 u8 target_flags, ttl;
486 u32 orig_sn, target_sn, lifetime;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100487 bool reply = false;
488 bool forward = true;
489
Rui Paulod19b3bf2009-11-09 23:46:55 +0000490 /* Update target SN, if present */
491 target_addr = PREQ_IE_TARGET_ADDR(preq_elem);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100492 orig_addr = PREQ_IE_ORIG_ADDR(preq_elem);
Rui Paulod19b3bf2009-11-09 23:46:55 +0000493 target_sn = PREQ_IE_TARGET_SN(preq_elem);
494 orig_sn = PREQ_IE_ORIG_SN(preq_elem);
495 target_flags = PREQ_IE_TARGET_F(preq_elem);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100496
Rui Paulof3c0d882009-11-09 23:46:47 +0000497 mhwmp_dbg("received PREQ from %pM\n", orig_addr);
Rui Paulo27db2e42009-11-09 23:46:45 +0000498
Johannes Berg47846c92009-11-25 17:46:19 +0100499 if (memcmp(target_addr, sdata->vif.addr, ETH_ALEN) == 0) {
Rui Paulodbb81c42009-11-09 23:46:46 +0000500 mhwmp_dbg("PREQ is for us\n");
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100501 forward = false;
502 reply = true;
503 metric = 0;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000504 if (time_after(jiffies, ifmsh->last_sn_update +
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100505 net_traversal_jiffies(sdata)) ||
Rui Paulod19b3bf2009-11-09 23:46:55 +0000506 time_before(jiffies, ifmsh->last_sn_update)) {
507 target_sn = ++ifmsh->sn;
508 ifmsh->last_sn_update = jiffies;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100509 }
510 } else {
511 rcu_read_lock();
Rui Paulod19b3bf2009-11-09 23:46:55 +0000512 mpath = mesh_path_lookup(target_addr, sdata);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100513 if (mpath) {
Rui Paulod19b3bf2009-11-09 23:46:55 +0000514 if ((!(mpath->flags & MESH_PATH_SN_VALID)) ||
515 SN_LT(mpath->sn, target_sn)) {
516 mpath->sn = target_sn;
517 mpath->flags |= MESH_PATH_SN_VALID;
518 } else if ((!(target_flags & MP_F_DO)) &&
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100519 (mpath->flags & MESH_PATH_ACTIVE)) {
520 reply = true;
521 metric = mpath->metric;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000522 target_sn = mpath->sn;
523 if (target_flags & MP_F_RF)
524 target_flags |= MP_F_DO;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100525 else
526 forward = false;
527 }
528 }
529 rcu_read_unlock();
530 }
531
532 if (reply) {
533 lifetime = PREQ_IE_LIFETIME(preq_elem);
Javier Cardona45904f22010-12-03 09:20:40 +0100534 ttl = ifmsh->mshcfg.element_ttl;
Rui Paulo27db2e42009-11-09 23:46:45 +0000535 if (ttl != 0) {
536 mhwmp_dbg("replying to the PREQ\n");
Rui Paulod19b3bf2009-11-09 23:46:55 +0000537 mesh_path_sel_frame_tx(MPATH_PREP, 0, target_addr,
538 cpu_to_le32(target_sn), 0, orig_addr,
539 cpu_to_le32(orig_sn), mgmt->sa, 0, ttl,
Luis Carlos Coboaa2b5922008-02-29 14:30:32 -0800540 cpu_to_le32(lifetime), cpu_to_le32(metric),
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200541 0, sdata);
Rui Paulo27db2e42009-11-09 23:46:45 +0000542 } else
Johannes Berg472dbc42008-09-11 00:01:49 +0200543 ifmsh->mshstats.dropped_frames_ttl++;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100544 }
545
546 if (forward) {
547 u32 preq_id;
548 u8 hopcount, flags;
549
550 ttl = PREQ_IE_TTL(preq_elem);
551 lifetime = PREQ_IE_LIFETIME(preq_elem);
552 if (ttl <= 1) {
Johannes Berg472dbc42008-09-11 00:01:49 +0200553 ifmsh->mshstats.dropped_frames_ttl++;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100554 return;
555 }
Rui Paulof3c0d882009-11-09 23:46:47 +0000556 mhwmp_dbg("forwarding the PREQ from %pM\n", orig_addr);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100557 --ttl;
558 flags = PREQ_IE_FLAGS(preq_elem);
559 preq_id = PREQ_IE_PREQ_ID(preq_elem);
560 hopcount = PREQ_IE_HOPCOUNT(preq_elem) + 1;
561 mesh_path_sel_frame_tx(MPATH_PREQ, flags, orig_addr,
Rui Paulod19b3bf2009-11-09 23:46:55 +0000562 cpu_to_le32(orig_sn), target_flags, target_addr,
Johannes Berg15ff6362009-11-17 13:34:04 +0100563 cpu_to_le32(target_sn), broadcast_addr,
Luis Carlos Coboaa2b5922008-02-29 14:30:32 -0800564 hopcount, ttl, cpu_to_le32(lifetime),
565 cpu_to_le32(metric), cpu_to_le32(preq_id),
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200566 sdata);
Daniel Walkerc8a61a72009-08-18 10:59:00 -0700567 ifmsh->mshstats.fwded_mcast++;
Johannes Berg472dbc42008-09-11 00:01:49 +0200568 ifmsh->mshstats.fwded_frames++;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100569 }
570}
571
572
Johannes Berg40b275b2011-05-13 14:15:49 +0200573static inline struct sta_info *
574next_hop_deref_protected(struct mesh_path *mpath)
575{
576 return rcu_dereference_protected(mpath->next_hop,
577 lockdep_is_held(&mpath->state_lock));
578}
579
580
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200581static void hwmp_prep_frame_process(struct ieee80211_sub_if_data *sdata,
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100582 struct ieee80211_mgmt *mgmt,
583 u8 *prep_elem, u32 metric)
584{
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100585 struct mesh_path *mpath;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000586 u8 *target_addr, *orig_addr;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100587 u8 ttl, hopcount, flags;
588 u8 next_hop[ETH_ALEN];
Rui Paulod19b3bf2009-11-09 23:46:55 +0000589 u32 target_sn, orig_sn, lifetime;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100590
Rui Paulof3c0d882009-11-09 23:46:47 +0000591 mhwmp_dbg("received PREP from %pM\n", PREP_IE_ORIG_ADDR(prep_elem));
Rui Paulodbb81c42009-11-09 23:46:46 +0000592
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100593 /* Note that we divert from the draft nomenclature and denominate
594 * destination to what the draft refers to as origininator. So in this
595 * function destnation refers to the final destination of the PREP,
596 * which corresponds with the originator of the PREQ which this PREP
597 * replies
598 */
Rui Paulod19b3bf2009-11-09 23:46:55 +0000599 target_addr = PREP_IE_TARGET_ADDR(prep_elem);
Johannes Berg47846c92009-11-25 17:46:19 +0100600 if (memcmp(target_addr, sdata->vif.addr, ETH_ALEN) == 0)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100601 /* destination, no forwarding required */
602 return;
603
604 ttl = PREP_IE_TTL(prep_elem);
605 if (ttl <= 1) {
Johannes Berg472dbc42008-09-11 00:01:49 +0200606 sdata->u.mesh.mshstats.dropped_frames_ttl++;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100607 return;
608 }
609
610 rcu_read_lock();
Rui Paulod19b3bf2009-11-09 23:46:55 +0000611 mpath = mesh_path_lookup(target_addr, sdata);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100612 if (mpath)
613 spin_lock_bh(&mpath->state_lock);
614 else
615 goto fail;
616 if (!(mpath->flags & MESH_PATH_ACTIVE)) {
617 spin_unlock_bh(&mpath->state_lock);
618 goto fail;
619 }
Johannes Berg40b275b2011-05-13 14:15:49 +0200620 memcpy(next_hop, next_hop_deref_protected(mpath)->sta.addr, ETH_ALEN);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100621 spin_unlock_bh(&mpath->state_lock);
622 --ttl;
623 flags = PREP_IE_FLAGS(prep_elem);
624 lifetime = PREP_IE_LIFETIME(prep_elem);
625 hopcount = PREP_IE_HOPCOUNT(prep_elem) + 1;
626 orig_addr = PREP_IE_ORIG_ADDR(prep_elem);
Rui Paulod19b3bf2009-11-09 23:46:55 +0000627 target_sn = PREP_IE_TARGET_SN(prep_elem);
628 orig_sn = PREP_IE_ORIG_SN(prep_elem);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100629
630 mesh_path_sel_frame_tx(MPATH_PREP, flags, orig_addr,
Rui Paulod19b3bf2009-11-09 23:46:55 +0000631 cpu_to_le32(orig_sn), 0, target_addr,
Porsch, Marco533866b2010-02-24 09:53:13 +0100632 cpu_to_le32(target_sn), next_hop, hopcount,
Rui Paulod19b3bf2009-11-09 23:46:55 +0000633 ttl, cpu_to_le32(lifetime), cpu_to_le32(metric),
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200634 0, sdata);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100635 rcu_read_unlock();
Daniel Walkerc8a61a72009-08-18 10:59:00 -0700636
637 sdata->u.mesh.mshstats.fwded_unicast++;
Johannes Berg472dbc42008-09-11 00:01:49 +0200638 sdata->u.mesh.mshstats.fwded_frames++;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100639 return;
640
641fail:
642 rcu_read_unlock();
Johannes Berg472dbc42008-09-11 00:01:49 +0200643 sdata->u.mesh.mshstats.dropped_frames_no_route++;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100644}
645
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200646static void hwmp_perr_frame_process(struct ieee80211_sub_if_data *sdata,
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100647 struct ieee80211_mgmt *mgmt, u8 *perr_elem)
648{
Rui Paulod611f062009-11-09 23:46:50 +0000649 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100650 struct mesh_path *mpath;
Rui Paulod611f062009-11-09 23:46:50 +0000651 u8 ttl;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000652 u8 *ta, *target_addr;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000653 u32 target_sn;
654 u16 target_rcode;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100655
656 ta = mgmt->sa;
Rui Paulod611f062009-11-09 23:46:50 +0000657 ttl = PERR_IE_TTL(perr_elem);
658 if (ttl <= 1) {
659 ifmsh->mshstats.dropped_frames_ttl++;
660 return;
661 }
662 ttl--;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000663 target_addr = PERR_IE_TARGET_ADDR(perr_elem);
664 target_sn = PERR_IE_TARGET_SN(perr_elem);
665 target_rcode = PERR_IE_TARGET_RCODE(perr_elem);
Rui Paulod611f062009-11-09 23:46:50 +0000666
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100667 rcu_read_lock();
Rui Paulod19b3bf2009-11-09 23:46:55 +0000668 mpath = mesh_path_lookup(target_addr, sdata);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100669 if (mpath) {
670 spin_lock_bh(&mpath->state_lock);
671 if (mpath->flags & MESH_PATH_ACTIVE &&
Johannes Berg40b275b2011-05-13 14:15:49 +0200672 memcmp(ta, next_hop_deref_protected(mpath)->sta.addr,
673 ETH_ALEN) == 0 &&
Rui Paulod19b3bf2009-11-09 23:46:55 +0000674 (!(mpath->flags & MESH_PATH_SN_VALID) ||
675 SN_GT(target_sn, mpath->sn))) {
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100676 mpath->flags &= ~MESH_PATH_ACTIVE;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000677 mpath->sn = target_sn;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100678 spin_unlock_bh(&mpath->state_lock);
Rui Paulod19b3bf2009-11-09 23:46:55 +0000679 mesh_path_error_tx(ttl, target_addr, cpu_to_le32(target_sn),
680 cpu_to_le16(target_rcode),
Johannes Berg15ff6362009-11-17 13:34:04 +0100681 broadcast_addr, sdata);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100682 } else
683 spin_unlock_bh(&mpath->state_lock);
684 }
685 rcu_read_unlock();
686}
687
Rui Paulo90a5e162009-11-11 00:01:31 +0000688static void hwmp_rann_frame_process(struct ieee80211_sub_if_data *sdata,
689 struct ieee80211_mgmt *mgmt,
690 struct ieee80211_rann_ie *rann)
691{
692 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
693 struct mesh_path *mpath;
Rui Paulo90a5e162009-11-11 00:01:31 +0000694 u8 ttl, flags, hopcount;
695 u8 *orig_addr;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000696 u32 orig_sn, metric;
Thomas Pedersen25d49e42011-08-11 19:35:15 -0700697 u32 interval = cpu_to_le32(IEEE80211_MESH_RANN_INTERVAL);
Rui Paulo90a5e162009-11-11 00:01:31 +0000698
Rui Paulo90a5e162009-11-11 00:01:31 +0000699 ttl = rann->rann_ttl;
700 if (ttl <= 1) {
701 ifmsh->mshstats.dropped_frames_ttl++;
702 return;
703 }
704 ttl--;
705 flags = rann->rann_flags;
706 orig_addr = rann->rann_addr;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000707 orig_sn = rann->rann_seq;
Rui Paulo90a5e162009-11-11 00:01:31 +0000708 hopcount = rann->rann_hopcount;
Rui Pauloa6a58b42009-11-09 23:46:51 +0000709 hopcount++;
Rui Paulo90a5e162009-11-11 00:01:31 +0000710 metric = rann->rann_metric;
711 mhwmp_dbg("received RANN from %pM\n", orig_addr);
712
713 rcu_read_lock();
714 mpath = mesh_path_lookup(orig_addr, sdata);
715 if (!mpath) {
716 mesh_path_add(orig_addr, sdata);
717 mpath = mesh_path_lookup(orig_addr, sdata);
718 if (!mpath) {
719 rcu_read_unlock();
720 sdata->u.mesh.mshstats.dropped_frames_no_route++;
721 return;
722 }
723 mesh_queue_preq(mpath,
724 PREQ_Q_F_START | PREQ_Q_F_REFRESH);
725 }
Rui Paulod19b3bf2009-11-09 23:46:55 +0000726 if (mpath->sn < orig_sn) {
Rui Paulo90a5e162009-11-11 00:01:31 +0000727 mesh_path_sel_frame_tx(MPATH_RANN, flags, orig_addr,
Rui Paulod19b3bf2009-11-09 23:46:55 +0000728 cpu_to_le32(orig_sn),
Johannes Berg15ff6362009-11-17 13:34:04 +0100729 0, NULL, 0, broadcast_addr,
Thomas Pedersen25d49e42011-08-11 19:35:15 -0700730 hopcount, ttl, interval,
Rui Pauloa6a58b42009-11-09 23:46:51 +0000731 cpu_to_le32(metric + mpath->metric),
Rui Paulo90a5e162009-11-11 00:01:31 +0000732 0, sdata);
Rui Paulod19b3bf2009-11-09 23:46:55 +0000733 mpath->sn = orig_sn;
Rui Paulo90a5e162009-11-11 00:01:31 +0000734 }
735 rcu_read_unlock();
736}
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100737
738
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200739void mesh_rx_path_sel_frame(struct ieee80211_sub_if_data *sdata,
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100740 struct ieee80211_mgmt *mgmt,
741 size_t len)
742{
743 struct ieee802_11_elems elems;
744 size_t baselen;
745 u32 last_hop_metric;
746
Johannes Berg9c80d3d2008-09-08 15:41:59 +0200747 /* need action_code */
748 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
749 return;
750
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100751 baselen = (u8 *) mgmt->u.action.u.mesh_action.variable - (u8 *) mgmt;
752 ieee802_11_parse_elems(mgmt->u.action.u.mesh_action.variable,
753 len - baselen, &elems);
754
Rui Paulodbb81c42009-11-09 23:46:46 +0000755 if (elems.preq) {
756 if (elems.preq_len != 37)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100757 /* Right now we support just 1 destination and no AE */
758 return;
Rui Paulodbb81c42009-11-09 23:46:46 +0000759 last_hop_metric = hwmp_route_info_get(sdata, mgmt, elems.preq,
760 MPATH_PREQ);
761 if (last_hop_metric)
762 hwmp_preq_frame_process(sdata, mgmt, elems.preq,
763 last_hop_metric);
764 }
765 if (elems.prep) {
766 if (elems.prep_len != 31)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100767 /* Right now we support no AE */
768 return;
Rui Paulodbb81c42009-11-09 23:46:46 +0000769 last_hop_metric = hwmp_route_info_get(sdata, mgmt, elems.prep,
770 MPATH_PREP);
771 if (last_hop_metric)
772 hwmp_prep_frame_process(sdata, mgmt, elems.prep,
773 last_hop_metric);
774 }
775 if (elems.perr) {
Rui Paulod611f062009-11-09 23:46:50 +0000776 if (elems.perr_len != 15)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100777 /* Right now we support only one destination per PERR */
778 return;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200779 hwmp_perr_frame_process(sdata, mgmt, elems.perr);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100780 }
Rui Paulo90a5e162009-11-11 00:01:31 +0000781 if (elems.rann)
782 hwmp_rann_frame_process(sdata, mgmt, elems.rann);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100783}
784
785/**
786 * mesh_queue_preq - queue a PREQ to a given destination
787 *
788 * @mpath: mesh path to discover
789 * @flags: special attributes of the PREQ to be sent
790 *
791 * Locking: the function must be called from within a rcu read lock block.
792 *
793 */
794static void mesh_queue_preq(struct mesh_path *mpath, u8 flags)
795{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200796 struct ieee80211_sub_if_data *sdata = mpath->sdata;
Johannes Berg472dbc42008-09-11 00:01:49 +0200797 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100798 struct mesh_preq_queue *preq_node;
799
Andrey Yurovsky59615b52009-06-25 16:07:42 -0700800 preq_node = kmalloc(sizeof(struct mesh_preq_queue), GFP_ATOMIC);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100801 if (!preq_node) {
Rui Paulo27db2e42009-11-09 23:46:45 +0000802 mhwmp_dbg("could not allocate PREQ node\n");
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100803 return;
804 }
805
Baruch Siach987dafa2011-07-28 08:51:05 +0300806 spin_lock_bh(&ifmsh->mesh_preq_queue_lock);
Johannes Berg472dbc42008-09-11 00:01:49 +0200807 if (ifmsh->preq_queue_len == MAX_PREQ_QUEUE_LEN) {
Baruch Siach987dafa2011-07-28 08:51:05 +0300808 spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100809 kfree(preq_node);
810 if (printk_ratelimit())
Rui Paulo27db2e42009-11-09 23:46:45 +0000811 mhwmp_dbg("PREQ node queue full\n");
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100812 return;
813 }
814
815 memcpy(preq_node->dst, mpath->dst, ETH_ALEN);
816 preq_node->flags = flags;
817
Johannes Berg472dbc42008-09-11 00:01:49 +0200818 list_add_tail(&preq_node->list, &ifmsh->preq_queue.list);
819 ++ifmsh->preq_queue_len;
Baruch Siach987dafa2011-07-28 08:51:05 +0300820 spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100821
Johannes Berg472dbc42008-09-11 00:01:49 +0200822 if (time_after(jiffies, ifmsh->last_preq + min_preq_int_jiff(sdata)))
Johannes Berg64592c82010-06-10 10:21:31 +0200823 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100824
Johannes Berg472dbc42008-09-11 00:01:49 +0200825 else if (time_before(jiffies, ifmsh->last_preq)) {
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100826 /* avoid long wait if did not send preqs for a long time
827 * and jiffies wrapped around
828 */
Johannes Berg472dbc42008-09-11 00:01:49 +0200829 ifmsh->last_preq = jiffies - min_preq_int_jiff(sdata) - 1;
Johannes Berg64592c82010-06-10 10:21:31 +0200830 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100831 } else
Johannes Berg472dbc42008-09-11 00:01:49 +0200832 mod_timer(&ifmsh->mesh_path_timer, ifmsh->last_preq +
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100833 min_preq_int_jiff(sdata));
834}
835
836/**
837 * mesh_path_start_discovery - launch a path discovery from the PREQ queue
838 *
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200839 * @sdata: local mesh subif
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100840 */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200841void mesh_path_start_discovery(struct ieee80211_sub_if_data *sdata)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100842{
Johannes Berg472dbc42008-09-11 00:01:49 +0200843 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100844 struct mesh_preq_queue *preq_node;
845 struct mesh_path *mpath;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000846 u8 ttl, target_flags;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100847 u32 lifetime;
848
Johannes Berga43816d2009-07-10 11:39:26 +0200849 spin_lock_bh(&ifmsh->mesh_preq_queue_lock);
Johannes Berg472dbc42008-09-11 00:01:49 +0200850 if (!ifmsh->preq_queue_len ||
851 time_before(jiffies, ifmsh->last_preq +
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100852 min_preq_int_jiff(sdata))) {
Johannes Berga43816d2009-07-10 11:39:26 +0200853 spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100854 return;
855 }
856
Johannes Berg472dbc42008-09-11 00:01:49 +0200857 preq_node = list_first_entry(&ifmsh->preq_queue.list,
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100858 struct mesh_preq_queue, list);
859 list_del(&preq_node->list);
Johannes Berg472dbc42008-09-11 00:01:49 +0200860 --ifmsh->preq_queue_len;
Johannes Berga43816d2009-07-10 11:39:26 +0200861 spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100862
863 rcu_read_lock();
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200864 mpath = mesh_path_lookup(preq_node->dst, sdata);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100865 if (!mpath)
866 goto enddiscovery;
867
868 spin_lock_bh(&mpath->state_lock);
869 if (preq_node->flags & PREQ_Q_F_START) {
870 if (mpath->flags & MESH_PATH_RESOLVING) {
871 spin_unlock_bh(&mpath->state_lock);
872 goto enddiscovery;
873 } else {
874 mpath->flags &= ~MESH_PATH_RESOLVED;
875 mpath->flags |= MESH_PATH_RESOLVING;
876 mpath->discovery_retries = 0;
877 mpath->discovery_timeout = disc_timeout_jiff(sdata);
878 }
879 } else if (!(mpath->flags & MESH_PATH_RESOLVING) ||
880 mpath->flags & MESH_PATH_RESOLVED) {
881 mpath->flags &= ~MESH_PATH_RESOLVING;
882 spin_unlock_bh(&mpath->state_lock);
883 goto enddiscovery;
884 }
885
Johannes Berg472dbc42008-09-11 00:01:49 +0200886 ifmsh->last_preq = jiffies;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100887
Rui Paulod19b3bf2009-11-09 23:46:55 +0000888 if (time_after(jiffies, ifmsh->last_sn_update +
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100889 net_traversal_jiffies(sdata)) ||
Rui Paulod19b3bf2009-11-09 23:46:55 +0000890 time_before(jiffies, ifmsh->last_sn_update)) {
891 ++ifmsh->sn;
892 sdata->u.mesh.last_sn_update = jiffies;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100893 }
894 lifetime = default_lifetime(sdata);
Javier Cardona45904f22010-12-03 09:20:40 +0100895 ttl = sdata->u.mesh.mshcfg.element_ttl;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100896 if (ttl == 0) {
Johannes Berg472dbc42008-09-11 00:01:49 +0200897 sdata->u.mesh.mshstats.dropped_frames_ttl++;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100898 spin_unlock_bh(&mpath->state_lock);
899 goto enddiscovery;
900 }
901
902 if (preq_node->flags & PREQ_Q_F_REFRESH)
Rui Paulod19b3bf2009-11-09 23:46:55 +0000903 target_flags = MP_F_DO;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100904 else
Rui Paulod19b3bf2009-11-09 23:46:55 +0000905 target_flags = MP_F_RF;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100906
907 spin_unlock_bh(&mpath->state_lock);
Johannes Berg47846c92009-11-25 17:46:19 +0100908 mesh_path_sel_frame_tx(MPATH_PREQ, 0, sdata->vif.addr,
Rui Paulod19b3bf2009-11-09 23:46:55 +0000909 cpu_to_le32(ifmsh->sn), target_flags, mpath->dst,
Johannes Berg15ff6362009-11-17 13:34:04 +0100910 cpu_to_le32(mpath->sn), broadcast_addr, 0,
Luis Carlos Coboaa2b5922008-02-29 14:30:32 -0800911 ttl, cpu_to_le32(lifetime), 0,
Johannes Berg472dbc42008-09-11 00:01:49 +0200912 cpu_to_le32(ifmsh->preq_id++), sdata);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100913 mod_timer(&mpath->timer, jiffies + mpath->discovery_timeout);
914
915enddiscovery:
916 rcu_read_unlock();
917 kfree(preq_node);
918}
919
920/**
Rami Rosen2182b832009-01-19 13:50:37 +0200921 * mesh_nexthop_lookup - put the appropriate next hop on a mesh frame
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100922 *
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +0200923 * @skb: 802.11 frame to be sent
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200924 * @sdata: network subif the frame will be sent through
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100925 *
926 * Returns: 0 if the next hop was found. Nonzero otherwise. If no next hop is
927 * found, the function will start a path discovery and queue the frame so it is
928 * sent when the path is resolved. This means the caller must not free the skb
929 * in this case.
930 */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200931int mesh_nexthop_lookup(struct sk_buff *skb,
932 struct ieee80211_sub_if_data *sdata)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100933{
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100934 struct sk_buff *skb_to_free = NULL;
935 struct mesh_path *mpath;
Johannes Berg40b275b2011-05-13 14:15:49 +0200936 struct sta_info *next_hop;
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +0200937 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000938 u8 *target_addr = hdr->addr3;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100939 int err = 0;
940
941 rcu_read_lock();
Rui Paulod19b3bf2009-11-09 23:46:55 +0000942 mpath = mesh_path_lookup(target_addr, sdata);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100943
944 if (!mpath) {
Rui Paulod19b3bf2009-11-09 23:46:55 +0000945 mesh_path_add(target_addr, sdata);
946 mpath = mesh_path_lookup(target_addr, sdata);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100947 if (!mpath) {
Johannes Berg472dbc42008-09-11 00:01:49 +0200948 sdata->u.mesh.mshstats.dropped_frames_no_route++;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100949 err = -ENOSPC;
950 goto endlookup;
951 }
952 }
953
954 if (mpath->flags & MESH_PATH_ACTIVE) {
Joe Perchesf64f9e72009-11-29 16:55:45 -0800955 if (time_after(jiffies,
Javier Cardona7b324d22009-12-09 18:43:01 -0800956 mpath->exp_time -
Joe Perchesf64f9e72009-11-29 16:55:45 -0800957 msecs_to_jiffies(sdata->u.mesh.mshcfg.path_refresh_time)) &&
Johannes Berg47846c92009-11-25 17:46:19 +0100958 !memcmp(sdata->vif.addr, hdr->addr4, ETH_ALEN) &&
Joe Perchesf64f9e72009-11-29 16:55:45 -0800959 !(mpath->flags & MESH_PATH_RESOLVING) &&
960 !(mpath->flags & MESH_PATH_FIXED)) {
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100961 mesh_queue_preq(mpath,
962 PREQ_Q_F_START | PREQ_Q_F_REFRESH);
963 }
Johannes Berg40b275b2011-05-13 14:15:49 +0200964 next_hop = rcu_dereference(mpath->next_hop);
965 if (next_hop)
966 memcpy(hdr->addr1, next_hop->sta.addr, ETH_ALEN);
967 else
968 err = -ENOENT;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100969 } else {
Javier Cardona249b4052009-07-07 10:55:03 -0700970 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100971 if (!(mpath->flags & MESH_PATH_RESOLVING)) {
972 /* Start discovery only if it is not running yet */
973 mesh_queue_preq(mpath, PREQ_Q_F_START);
974 }
975
976 if (skb_queue_len(&mpath->frame_queue) >=
Javier Cardonafe583432009-08-10 12:15:46 -0700977 MESH_FRAME_QUEUE_LEN)
978 skb_to_free = skb_dequeue(&mpath->frame_queue);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100979
Javier Cardona249b4052009-07-07 10:55:03 -0700980 info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100981 skb_queue_tail(&mpath->frame_queue, skb);
982 if (skb_to_free)
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200983 mesh_path_discard_frame(skb_to_free, sdata);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100984 err = -ENOENT;
985 }
986
987endlookup:
988 rcu_read_unlock();
989 return err;
990}
991
992void mesh_path_timer(unsigned long data)
993{
Johannes Bergdea40962011-05-12 15:03:32 +0200994 struct mesh_path *mpath = (void *) data;
995 struct ieee80211_sub_if_data *sdata = mpath->sdata;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100996
Johannes Bergdea40962011-05-12 15:03:32 +0200997 if (sdata->local->quiescing)
Johannes Berg5bb644a2009-05-17 11:40:42 +0200998 return;
Johannes Berg5bb644a2009-05-17 11:40:42 +0200999
1000 spin_lock_bh(&mpath->state_lock);
Luis Carlos Cobocfa22c72008-02-29 15:04:13 -08001001 if (mpath->flags & MESH_PATH_RESOLVED ||
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001002 (!(mpath->flags & MESH_PATH_RESOLVING)))
1003 mpath->flags &= ~(MESH_PATH_RESOLVING | MESH_PATH_RESOLVED);
1004 else if (mpath->discovery_retries < max_preq_retries(sdata)) {
1005 ++mpath->discovery_retries;
1006 mpath->discovery_timeout *= 2;
1007 mesh_queue_preq(mpath, 0);
1008 } else {
1009 mpath->flags = 0;
1010 mpath->exp_time = jiffies;
1011 mesh_path_flush_pending(mpath);
1012 }
1013
1014 spin_unlock_bh(&mpath->state_lock);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001015}
Rui Pauloe304bfd2009-11-09 23:46:56 +00001016
1017void
1018mesh_path_tx_root_frame(struct ieee80211_sub_if_data *sdata)
1019{
1020 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
Thomas Pedersen25d49e42011-08-11 19:35:15 -07001021 u32 interval = cpu_to_le32(IEEE80211_MESH_RANN_INTERVAL);
Rui Pauloe304bfd2009-11-09 23:46:56 +00001022
Johannes Berg47846c92009-11-25 17:46:19 +01001023 mesh_path_sel_frame_tx(MPATH_RANN, 0, sdata->vif.addr,
Rui Pauloe304bfd2009-11-09 23:46:56 +00001024 cpu_to_le32(++ifmsh->sn),
Johannes Berg15ff6362009-11-17 13:34:04 +01001025 0, NULL, 0, broadcast_addr,
Javier Cardona45904f22010-12-03 09:20:40 +01001026 0, sdata->u.mesh.mshcfg.element_ttl,
Thomas Pedersen25d49e42011-08-11 19:35:15 -07001027 interval, 0, 0, sdata);
Rui Pauloe304bfd2009-11-09 23:46:56 +00001028}