blob: 0705018d8d1e7ac9a185f3bea2fffe43aaaaed24 [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)
Rui Paulod19b3bf2009-11-09 23:46:55 +000060#define PREQ_IE_ORIG_SN(x) u32_field_get(x, 13, 0);
Luis Carlos Coboa00de5d2008-02-29 17:07:54 -080061#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)
65#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)
71#define PREP_IE_ORIG_ADDR(x) (x + 3)
Rui Paulod19b3bf2009-11-09 23:46:55 +000072#define PREP_IE_ORIG_SN(x) u32_field_get(x, 9, 0);
Luis Carlos Coboa00de5d2008-02-29 17:07:54 -080073#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));
Rui Paulod19b3bf2009-11-09 23:46:55 +000075#define PREP_IE_TARGET_ADDR(x) (AE_F_SET(x) ? x + 27 : x + 21)
76#define PREP_IE_TARGET_SN(x) u32_field_get(x, 27, AE_F_SET(x));
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)
81#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);
Javier Cardona97ad9132010-03-29 11:00:21 -0700135 mgmt->u.action.category = WLAN_CATEGORY_MESH_PATH_SEL;
Rui Paulo095de012009-11-09 23:46:44 +0000136 mgmt->u.action.u.mesh_action.action_code = MESH_PATH_SEL_ACTION;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100137
138 switch (action) {
139 case MPATH_PREQ:
Rui Paulod19b3bf2009-11-09 23:46:55 +0000140 mhwmp_dbg("sending PREQ to %pM\n", target);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100141 ie_len = 37;
142 pos = skb_put(skb, 2 + ie_len);
143 *pos++ = WLAN_EID_PREQ;
144 break;
145 case MPATH_PREP:
Rui Paulod19b3bf2009-11-09 23:46:55 +0000146 mhwmp_dbg("sending PREP to %pM\n", target);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100147 ie_len = 31;
148 pos = skb_put(skb, 2 + ie_len);
149 *pos++ = WLAN_EID_PREP;
150 break;
Rui Paulo90a5e162009-11-11 00:01:31 +0000151 case MPATH_RANN:
152 mhwmp_dbg("sending RANN from %pM\n", orig_addr);
153 ie_len = sizeof(struct ieee80211_rann_ie);
154 pos = skb_put(skb, 2 + ie_len);
155 *pos++ = WLAN_EID_RANN;
156 break;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100157 default:
Patrick McHardy812714d2008-05-06 12:52:07 +0200158 kfree_skb(skb);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100159 return -ENOTSUPP;
160 break;
161 }
162 *pos++ = ie_len;
163 *pos++ = flags;
164 *pos++ = hop_count;
165 *pos++ = ttl;
166 if (action == MPATH_PREQ) {
167 memcpy(pos, &preq_id, 4);
168 pos += 4;
169 }
170 memcpy(pos, orig_addr, ETH_ALEN);
171 pos += ETH_ALEN;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000172 memcpy(pos, &orig_sn, 4);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100173 pos += 4;
Rui Paulo90a5e162009-11-11 00:01:31 +0000174 if (action != MPATH_RANN) {
175 memcpy(pos, &lifetime, 4);
176 pos += 4;
177 }
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100178 memcpy(pos, &metric, 4);
179 pos += 4;
180 if (action == MPATH_PREQ) {
181 /* destination count */
182 *pos++ = 1;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000183 *pos++ = target_flags;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100184 }
Rui Paulo90a5e162009-11-11 00:01:31 +0000185 if (action != MPATH_RANN) {
Rui Paulod19b3bf2009-11-09 23:46:55 +0000186 memcpy(pos, target, ETH_ALEN);
Rui Paulo90a5e162009-11-11 00:01:31 +0000187 pos += ETH_ALEN;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000188 memcpy(pos, &target_sn, 4);
Rui Paulo90a5e162009-11-11 00:01:31 +0000189 }
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100190
Johannes Berg62ae67b2009-11-18 18:42:05 +0100191 ieee80211_tx_skb(sdata, skb);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100192 return 0;
193}
194
195/**
196 * mesh_send_path error - Sends a PERR mesh management frame
197 *
Rui Paulod19b3bf2009-11-09 23:46:55 +0000198 * @target: broken destination
199 * @target_sn: SN of the broken destination
200 * @target_rcode: reason code for this PERR
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100201 * @ra: node this frame is addressed to
202 */
Rui Paulod19b3bf2009-11-09 23:46:55 +0000203int mesh_path_error_tx(u8 ttl, u8 *target, __le32 target_sn,
Johannes Berg15ff6362009-11-17 13:34:04 +0100204 __le16 target_rcode, const u8 *ra,
205 struct ieee80211_sub_if_data *sdata)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100206{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200207 struct ieee80211_local *local = sdata->local;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100208 struct sk_buff *skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400);
209 struct ieee80211_mgmt *mgmt;
210 u8 *pos;
211 int ie_len;
212
213 if (!skb)
214 return -1;
215 skb_reserve(skb, local->hw.extra_tx_headroom);
216 /* 25 is the size of the common mgmt part (24) plus the size of the
217 * common action part (1)
218 */
219 mgmt = (struct ieee80211_mgmt *)
220 skb_put(skb, 25 + sizeof(mgmt->u.action.u.mesh_action));
221 memset(mgmt, 0, 25 + sizeof(mgmt->u.action.u.mesh_action));
Harvey Harrisone7827a72008-07-15 18:44:13 -0700222 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
223 IEEE80211_STYPE_ACTION);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100224
225 memcpy(mgmt->da, ra, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +0100226 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100227 /* BSSID is left zeroed, wildcard value */
Javier Cardona97ad9132010-03-29 11:00:21 -0700228 mgmt->u.action.category = WLAN_CATEGORY_MESH_PATH_SEL;
Rui Paulodbb81c42009-11-09 23:46:46 +0000229 mgmt->u.action.u.mesh_action.action_code = MESH_PATH_SEL_ACTION;
Rui Paulod611f062009-11-09 23:46:50 +0000230 ie_len = 15;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100231 pos = skb_put(skb, 2 + ie_len);
232 *pos++ = WLAN_EID_PERR;
233 *pos++ = ie_len;
Rui Paulod611f062009-11-09 23:46:50 +0000234 /* ttl */
235 *pos++ = MESH_TTL;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100236 /* number of destinations */
237 *pos++ = 1;
Rui Paulod611f062009-11-09 23:46:50 +0000238 /*
239 * flags bit, bit 1 is unset if we know the sequence number and
240 * bit 2 is set if we have a reason code
241 */
242 *pos = 0;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000243 if (!target_sn)
Rui Paulod611f062009-11-09 23:46:50 +0000244 *pos |= MP_F_USN;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000245 if (target_rcode)
Rui Paulod611f062009-11-09 23:46:50 +0000246 *pos |= MP_F_RCODE;
247 pos++;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000248 memcpy(pos, target, ETH_ALEN);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100249 pos += ETH_ALEN;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000250 memcpy(pos, &target_sn, 4);
Rui Paulod611f062009-11-09 23:46:50 +0000251 pos += 4;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000252 memcpy(pos, &target_rcode, 2);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100253
Johannes Berg62ae67b2009-11-18 18:42:05 +0100254 ieee80211_tx_skb(sdata, skb);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100255 return 0;
256}
257
Javier Cardonabfc32e62009-08-17 17:15:55 -0700258void ieee80211s_update_metric(struct ieee80211_local *local,
259 struct sta_info *stainfo, struct sk_buff *skb)
260{
261 struct ieee80211_tx_info *txinfo = IEEE80211_SKB_CB(skb);
262 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
263 int failed;
264
265 if (!ieee80211_is_data(hdr->frame_control))
266 return;
267
268 failed = !(txinfo->flags & IEEE80211_TX_STAT_ACK);
269
270 /* moving average, scaled to 100 */
271 stainfo->fail_avg = ((80 * stainfo->fail_avg + 5) / 100 + 20 * failed);
272 if (stainfo->fail_avg > 95)
273 mesh_plink_broken(stainfo);
274}
275
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100276static u32 airtime_link_metric_get(struct ieee80211_local *local,
277 struct sta_info *sta)
278{
279 struct ieee80211_supported_band *sband;
280 /* This should be adjusted for each device */
281 int device_constant = 1 << ARITH_SHIFT;
282 int test_frame_len = TEST_FRAME_LEN << ARITH_SHIFT;
283 int s_unit = 1 << ARITH_SHIFT;
284 int rate, err;
285 u32 tx_time, estimated_retx;
286 u64 result;
287
288 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
289
290 if (sta->fail_avg >= 100)
291 return MAX_METRIC;
Johannes Berge6a98542008-10-21 12:40:02 +0200292
293 if (sta->last_tx_rate.flags & IEEE80211_TX_RC_MCS)
294 return MAX_METRIC;
295
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100296 err = (sta->fail_avg << ARITH_SHIFT) / 100;
297
298 /* bitrate is in units of 100 Kbps, while we need rate in units of
299 * 1Mbps. This will be corrected on tx_time computation.
300 */
Johannes Berge6a98542008-10-21 12:40:02 +0200301 rate = sband->bitrates[sta->last_tx_rate.idx].bitrate;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100302 tx_time = (device_constant + 10 * test_frame_len / rate);
303 estimated_retx = ((1 << (2 * ARITH_SHIFT)) / (s_unit - err));
304 result = (tx_time * estimated_retx) >> (2 * ARITH_SHIFT) ;
305 return (u32)result;
306}
307
308/**
309 * hwmp_route_info_get - Update routing info to originator and transmitter
310 *
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200311 * @sdata: local mesh subif
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100312 * @mgmt: mesh management frame
313 * @hwmp_ie: hwmp information element (PREP or PREQ)
314 *
315 * This function updates the path routing information to the originator and the
Andrey Yurovskyf99288d2009-10-20 12:17:34 -0700316 * transmitter of a HWMP PREQ or PREP frame.
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100317 *
318 * Returns: metric to frame originator or 0 if the frame should not be further
319 * processed
320 *
321 * Notes: this function is the only place (besides user-provided info) where
322 * path routing information is updated.
323 */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200324static u32 hwmp_route_info_get(struct ieee80211_sub_if_data *sdata,
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100325 struct ieee80211_mgmt *mgmt,
Rui Paulodbb81c42009-11-09 23:46:46 +0000326 u8 *hwmp_ie, enum mpath_frame_type action)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100327{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200328 struct ieee80211_local *local = sdata->local;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100329 struct mesh_path *mpath;
330 struct sta_info *sta;
331 bool fresh_info;
332 u8 *orig_addr, *ta;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000333 u32 orig_sn, orig_metric;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100334 unsigned long orig_lifetime, exp_time;
335 u32 last_hop_metric, new_metric;
336 bool process = true;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100337
338 rcu_read_lock();
Johannes Bergabe60632009-11-25 17:46:18 +0100339 sta = sta_info_get(sdata, mgmt->sa);
Johannes Bergdc0b0f72008-02-23 15:17:20 +0100340 if (!sta) {
341 rcu_read_unlock();
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100342 return 0;
Johannes Bergdc0b0f72008-02-23 15:17:20 +0100343 }
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100344
345 last_hop_metric = airtime_link_metric_get(local, sta);
346 /* Update and check originator routing info */
347 fresh_info = true;
348
349 switch (action) {
350 case MPATH_PREQ:
351 orig_addr = PREQ_IE_ORIG_ADDR(hwmp_ie);
Rui Paulod19b3bf2009-11-09 23:46:55 +0000352 orig_sn = PREQ_IE_ORIG_SN(hwmp_ie);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100353 orig_lifetime = PREQ_IE_LIFETIME(hwmp_ie);
354 orig_metric = PREQ_IE_METRIC(hwmp_ie);
355 break;
356 case MPATH_PREP:
357 /* Originator here refers to the MP that was the destination in
358 * the Path Request. The draft refers to that MP as the
359 * destination address, even though usually it is the origin of
360 * the PREP frame. We divert from the nomenclature in the draft
361 * so that we can easily use a single function to gather path
362 * information from both PREQ and PREP frames.
363 */
364 orig_addr = PREP_IE_ORIG_ADDR(hwmp_ie);
Rui Paulod19b3bf2009-11-09 23:46:55 +0000365 orig_sn = PREP_IE_ORIG_SN(hwmp_ie);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100366 orig_lifetime = PREP_IE_LIFETIME(hwmp_ie);
367 orig_metric = PREP_IE_METRIC(hwmp_ie);
368 break;
369 default:
Johannes Bergdc0b0f72008-02-23 15:17:20 +0100370 rcu_read_unlock();
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100371 return 0;
372 }
373 new_metric = orig_metric + last_hop_metric;
374 if (new_metric < orig_metric)
375 new_metric = MAX_METRIC;
376 exp_time = TU_TO_EXP_TIME(orig_lifetime);
377
Johannes Berg47846c92009-11-25 17:46:19 +0100378 if (memcmp(orig_addr, sdata->vif.addr, ETH_ALEN) == 0) {
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100379 /* This MP is the originator, we are not interested in this
380 * frame, except for updating transmitter's path info.
381 */
382 process = false;
383 fresh_info = false;
384 } else {
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200385 mpath = mesh_path_lookup(orig_addr, sdata);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100386 if (mpath) {
387 spin_lock_bh(&mpath->state_lock);
388 if (mpath->flags & MESH_PATH_FIXED)
389 fresh_info = false;
390 else if ((mpath->flags & MESH_PATH_ACTIVE) &&
Rui Paulod19b3bf2009-11-09 23:46:55 +0000391 (mpath->flags & MESH_PATH_SN_VALID)) {
392 if (SN_GT(mpath->sn, orig_sn) ||
393 (mpath->sn == orig_sn &&
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100394 action == MPATH_PREQ &&
Porsch, Marco533866b2010-02-24 09:53:13 +0100395 new_metric >= mpath->metric)) {
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100396 process = false;
397 fresh_info = false;
398 }
399 }
400 } else {
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200401 mesh_path_add(orig_addr, sdata);
402 mpath = mesh_path_lookup(orig_addr, sdata);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100403 if (!mpath) {
404 rcu_read_unlock();
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100405 return 0;
406 }
407 spin_lock_bh(&mpath->state_lock);
408 }
409
410 if (fresh_info) {
411 mesh_path_assign_nexthop(mpath, sta);
Rui Paulod19b3bf2009-11-09 23:46:55 +0000412 mpath->flags |= MESH_PATH_SN_VALID;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100413 mpath->metric = new_metric;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000414 mpath->sn = orig_sn;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100415 mpath->exp_time = time_after(mpath->exp_time, exp_time)
416 ? mpath->exp_time : exp_time;
417 mesh_path_activate(mpath);
418 spin_unlock_bh(&mpath->state_lock);
419 mesh_path_tx_pending(mpath);
420 /* draft says preq_id should be saved to, but there does
421 * not seem to be any use for it, skipping by now
422 */
423 } else
424 spin_unlock_bh(&mpath->state_lock);
425 }
426
427 /* Update and check transmitter routing info */
428 ta = mgmt->sa;
429 if (memcmp(orig_addr, ta, ETH_ALEN) == 0)
430 fresh_info = false;
431 else {
432 fresh_info = true;
433
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200434 mpath = mesh_path_lookup(ta, sdata);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100435 if (mpath) {
436 spin_lock_bh(&mpath->state_lock);
437 if ((mpath->flags & MESH_PATH_FIXED) ||
438 ((mpath->flags & MESH_PATH_ACTIVE) &&
439 (last_hop_metric > mpath->metric)))
440 fresh_info = false;
441 } else {
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200442 mesh_path_add(ta, sdata);
443 mpath = mesh_path_lookup(ta, sdata);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100444 if (!mpath) {
445 rcu_read_unlock();
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100446 return 0;
447 }
448 spin_lock_bh(&mpath->state_lock);
449 }
450
451 if (fresh_info) {
452 mesh_path_assign_nexthop(mpath, sta);
Rui Paulod19b3bf2009-11-09 23:46:55 +0000453 mpath->flags &= ~MESH_PATH_SN_VALID;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100454 mpath->metric = last_hop_metric;
455 mpath->exp_time = time_after(mpath->exp_time, exp_time)
456 ? mpath->exp_time : exp_time;
457 mesh_path_activate(mpath);
458 spin_unlock_bh(&mpath->state_lock);
459 mesh_path_tx_pending(mpath);
460 } else
461 spin_unlock_bh(&mpath->state_lock);
462 }
463
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100464 rcu_read_unlock();
465
466 return process ? new_metric : 0;
467}
468
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200469static void hwmp_preq_frame_process(struct ieee80211_sub_if_data *sdata,
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100470 struct ieee80211_mgmt *mgmt,
David Woo57ef5dd2009-08-12 11:03:43 -0700471 u8 *preq_elem, u32 metric)
472{
Johannes Berg472dbc42008-09-11 00:01:49 +0200473 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100474 struct mesh_path *mpath;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000475 u8 *target_addr, *orig_addr;
476 u8 target_flags, ttl;
477 u32 orig_sn, target_sn, lifetime;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100478 bool reply = false;
479 bool forward = true;
480
Rui Paulod19b3bf2009-11-09 23:46:55 +0000481 /* Update target SN, if present */
482 target_addr = PREQ_IE_TARGET_ADDR(preq_elem);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100483 orig_addr = PREQ_IE_ORIG_ADDR(preq_elem);
Rui Paulod19b3bf2009-11-09 23:46:55 +0000484 target_sn = PREQ_IE_TARGET_SN(preq_elem);
485 orig_sn = PREQ_IE_ORIG_SN(preq_elem);
486 target_flags = PREQ_IE_TARGET_F(preq_elem);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100487
Rui Paulof3c0d882009-11-09 23:46:47 +0000488 mhwmp_dbg("received PREQ from %pM\n", orig_addr);
Rui Paulo27db2e42009-11-09 23:46:45 +0000489
Johannes Berg47846c92009-11-25 17:46:19 +0100490 if (memcmp(target_addr, sdata->vif.addr, ETH_ALEN) == 0) {
Rui Paulodbb81c42009-11-09 23:46:46 +0000491 mhwmp_dbg("PREQ is for us\n");
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100492 forward = false;
493 reply = true;
494 metric = 0;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000495 if (time_after(jiffies, ifmsh->last_sn_update +
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100496 net_traversal_jiffies(sdata)) ||
Rui Paulod19b3bf2009-11-09 23:46:55 +0000497 time_before(jiffies, ifmsh->last_sn_update)) {
498 target_sn = ++ifmsh->sn;
499 ifmsh->last_sn_update = jiffies;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100500 }
501 } else {
502 rcu_read_lock();
Rui Paulod19b3bf2009-11-09 23:46:55 +0000503 mpath = mesh_path_lookup(target_addr, sdata);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100504 if (mpath) {
Rui Paulod19b3bf2009-11-09 23:46:55 +0000505 if ((!(mpath->flags & MESH_PATH_SN_VALID)) ||
506 SN_LT(mpath->sn, target_sn)) {
507 mpath->sn = target_sn;
508 mpath->flags |= MESH_PATH_SN_VALID;
509 } else if ((!(target_flags & MP_F_DO)) &&
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100510 (mpath->flags & MESH_PATH_ACTIVE)) {
511 reply = true;
512 metric = mpath->metric;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000513 target_sn = mpath->sn;
514 if (target_flags & MP_F_RF)
515 target_flags |= MP_F_DO;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100516 else
517 forward = false;
518 }
519 }
520 rcu_read_unlock();
521 }
522
523 if (reply) {
524 lifetime = PREQ_IE_LIFETIME(preq_elem);
Johannes Berg472dbc42008-09-11 00:01:49 +0200525 ttl = ifmsh->mshcfg.dot11MeshTTL;
Rui Paulo27db2e42009-11-09 23:46:45 +0000526 if (ttl != 0) {
527 mhwmp_dbg("replying to the PREQ\n");
Rui Paulod19b3bf2009-11-09 23:46:55 +0000528 mesh_path_sel_frame_tx(MPATH_PREP, 0, target_addr,
529 cpu_to_le32(target_sn), 0, orig_addr,
530 cpu_to_le32(orig_sn), mgmt->sa, 0, ttl,
Luis Carlos Coboaa2b5922008-02-29 14:30:32 -0800531 cpu_to_le32(lifetime), cpu_to_le32(metric),
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200532 0, sdata);
Rui Paulo27db2e42009-11-09 23:46:45 +0000533 } else
Johannes Berg472dbc42008-09-11 00:01:49 +0200534 ifmsh->mshstats.dropped_frames_ttl++;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100535 }
536
537 if (forward) {
538 u32 preq_id;
539 u8 hopcount, flags;
540
541 ttl = PREQ_IE_TTL(preq_elem);
542 lifetime = PREQ_IE_LIFETIME(preq_elem);
543 if (ttl <= 1) {
Johannes Berg472dbc42008-09-11 00:01:49 +0200544 ifmsh->mshstats.dropped_frames_ttl++;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100545 return;
546 }
Rui Paulof3c0d882009-11-09 23:46:47 +0000547 mhwmp_dbg("forwarding the PREQ from %pM\n", orig_addr);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100548 --ttl;
549 flags = PREQ_IE_FLAGS(preq_elem);
550 preq_id = PREQ_IE_PREQ_ID(preq_elem);
551 hopcount = PREQ_IE_HOPCOUNT(preq_elem) + 1;
552 mesh_path_sel_frame_tx(MPATH_PREQ, flags, orig_addr,
Rui Paulod19b3bf2009-11-09 23:46:55 +0000553 cpu_to_le32(orig_sn), target_flags, target_addr,
Johannes Berg15ff6362009-11-17 13:34:04 +0100554 cpu_to_le32(target_sn), broadcast_addr,
Luis Carlos Coboaa2b5922008-02-29 14:30:32 -0800555 hopcount, ttl, cpu_to_le32(lifetime),
556 cpu_to_le32(metric), cpu_to_le32(preq_id),
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200557 sdata);
Daniel Walkerc8a61a72009-08-18 10:59:00 -0700558 ifmsh->mshstats.fwded_mcast++;
Johannes Berg472dbc42008-09-11 00:01:49 +0200559 ifmsh->mshstats.fwded_frames++;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100560 }
561}
562
563
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200564static void hwmp_prep_frame_process(struct ieee80211_sub_if_data *sdata,
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100565 struct ieee80211_mgmt *mgmt,
566 u8 *prep_elem, u32 metric)
567{
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100568 struct mesh_path *mpath;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000569 u8 *target_addr, *orig_addr;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100570 u8 ttl, hopcount, flags;
571 u8 next_hop[ETH_ALEN];
Rui Paulod19b3bf2009-11-09 23:46:55 +0000572 u32 target_sn, orig_sn, lifetime;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100573
Rui Paulof3c0d882009-11-09 23:46:47 +0000574 mhwmp_dbg("received PREP from %pM\n", PREP_IE_ORIG_ADDR(prep_elem));
Rui Paulodbb81c42009-11-09 23:46:46 +0000575
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100576 /* Note that we divert from the draft nomenclature and denominate
577 * destination to what the draft refers to as origininator. So in this
578 * function destnation refers to the final destination of the PREP,
579 * which corresponds with the originator of the PREQ which this PREP
580 * replies
581 */
Rui Paulod19b3bf2009-11-09 23:46:55 +0000582 target_addr = PREP_IE_TARGET_ADDR(prep_elem);
Johannes Berg47846c92009-11-25 17:46:19 +0100583 if (memcmp(target_addr, sdata->vif.addr, ETH_ALEN) == 0)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100584 /* destination, no forwarding required */
585 return;
586
587 ttl = PREP_IE_TTL(prep_elem);
588 if (ttl <= 1) {
Johannes Berg472dbc42008-09-11 00:01:49 +0200589 sdata->u.mesh.mshstats.dropped_frames_ttl++;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100590 return;
591 }
592
593 rcu_read_lock();
Rui Paulod19b3bf2009-11-09 23:46:55 +0000594 mpath = mesh_path_lookup(target_addr, sdata);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100595 if (mpath)
596 spin_lock_bh(&mpath->state_lock);
597 else
598 goto fail;
599 if (!(mpath->flags & MESH_PATH_ACTIVE)) {
600 spin_unlock_bh(&mpath->state_lock);
601 goto fail;
602 }
Johannes Berg17741cd2008-09-11 00:02:02 +0200603 memcpy(next_hop, mpath->next_hop->sta.addr, ETH_ALEN);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100604 spin_unlock_bh(&mpath->state_lock);
605 --ttl;
606 flags = PREP_IE_FLAGS(prep_elem);
607 lifetime = PREP_IE_LIFETIME(prep_elem);
608 hopcount = PREP_IE_HOPCOUNT(prep_elem) + 1;
609 orig_addr = PREP_IE_ORIG_ADDR(prep_elem);
Rui Paulod19b3bf2009-11-09 23:46:55 +0000610 target_sn = PREP_IE_TARGET_SN(prep_elem);
611 orig_sn = PREP_IE_ORIG_SN(prep_elem);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100612
613 mesh_path_sel_frame_tx(MPATH_PREP, flags, orig_addr,
Rui Paulod19b3bf2009-11-09 23:46:55 +0000614 cpu_to_le32(orig_sn), 0, target_addr,
Porsch, Marco533866b2010-02-24 09:53:13 +0100615 cpu_to_le32(target_sn), next_hop, hopcount,
Rui Paulod19b3bf2009-11-09 23:46:55 +0000616 ttl, cpu_to_le32(lifetime), cpu_to_le32(metric),
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200617 0, sdata);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100618 rcu_read_unlock();
Daniel Walkerc8a61a72009-08-18 10:59:00 -0700619
620 sdata->u.mesh.mshstats.fwded_unicast++;
Johannes Berg472dbc42008-09-11 00:01:49 +0200621 sdata->u.mesh.mshstats.fwded_frames++;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100622 return;
623
624fail:
625 rcu_read_unlock();
Johannes Berg472dbc42008-09-11 00:01:49 +0200626 sdata->u.mesh.mshstats.dropped_frames_no_route++;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100627}
628
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200629static void hwmp_perr_frame_process(struct ieee80211_sub_if_data *sdata,
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100630 struct ieee80211_mgmt *mgmt, u8 *perr_elem)
631{
Rui Paulod611f062009-11-09 23:46:50 +0000632 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100633 struct mesh_path *mpath;
Rui Paulod611f062009-11-09 23:46:50 +0000634 u8 ttl;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000635 u8 *ta, *target_addr;
636 u8 target_flags;
637 u32 target_sn;
638 u16 target_rcode;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100639
640 ta = mgmt->sa;
Rui Paulod611f062009-11-09 23:46:50 +0000641 ttl = PERR_IE_TTL(perr_elem);
642 if (ttl <= 1) {
643 ifmsh->mshstats.dropped_frames_ttl++;
644 return;
645 }
646 ttl--;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000647 target_flags = PERR_IE_TARGET_FLAGS(perr_elem);
648 target_addr = PERR_IE_TARGET_ADDR(perr_elem);
649 target_sn = PERR_IE_TARGET_SN(perr_elem);
650 target_rcode = PERR_IE_TARGET_RCODE(perr_elem);
Rui Paulod611f062009-11-09 23:46:50 +0000651
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100652 rcu_read_lock();
Rui Paulod19b3bf2009-11-09 23:46:55 +0000653 mpath = mesh_path_lookup(target_addr, sdata);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100654 if (mpath) {
655 spin_lock_bh(&mpath->state_lock);
656 if (mpath->flags & MESH_PATH_ACTIVE &&
Johannes Berg17741cd2008-09-11 00:02:02 +0200657 memcmp(ta, mpath->next_hop->sta.addr, ETH_ALEN) == 0 &&
Rui Paulod19b3bf2009-11-09 23:46:55 +0000658 (!(mpath->flags & MESH_PATH_SN_VALID) ||
659 SN_GT(target_sn, mpath->sn))) {
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100660 mpath->flags &= ~MESH_PATH_ACTIVE;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000661 mpath->sn = target_sn;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100662 spin_unlock_bh(&mpath->state_lock);
Rui Paulod19b3bf2009-11-09 23:46:55 +0000663 mesh_path_error_tx(ttl, target_addr, cpu_to_le32(target_sn),
664 cpu_to_le16(target_rcode),
Johannes Berg15ff6362009-11-17 13:34:04 +0100665 broadcast_addr, sdata);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100666 } else
667 spin_unlock_bh(&mpath->state_lock);
668 }
669 rcu_read_unlock();
670}
671
Rui Paulo90a5e162009-11-11 00:01:31 +0000672static void hwmp_rann_frame_process(struct ieee80211_sub_if_data *sdata,
673 struct ieee80211_mgmt *mgmt,
674 struct ieee80211_rann_ie *rann)
675{
676 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
677 struct mesh_path *mpath;
678 u8 *ta;
679 u8 ttl, flags, hopcount;
680 u8 *orig_addr;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000681 u32 orig_sn, metric;
Rui Paulo90a5e162009-11-11 00:01:31 +0000682
683 ta = mgmt->sa;
684 ttl = rann->rann_ttl;
685 if (ttl <= 1) {
686 ifmsh->mshstats.dropped_frames_ttl++;
687 return;
688 }
689 ttl--;
690 flags = rann->rann_flags;
691 orig_addr = rann->rann_addr;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000692 orig_sn = rann->rann_seq;
Rui Paulo90a5e162009-11-11 00:01:31 +0000693 hopcount = rann->rann_hopcount;
Rui Pauloa6a58b42009-11-09 23:46:51 +0000694 hopcount++;
Rui Paulo90a5e162009-11-11 00:01:31 +0000695 metric = rann->rann_metric;
696 mhwmp_dbg("received RANN from %pM\n", orig_addr);
697
698 rcu_read_lock();
699 mpath = mesh_path_lookup(orig_addr, sdata);
700 if (!mpath) {
701 mesh_path_add(orig_addr, sdata);
702 mpath = mesh_path_lookup(orig_addr, sdata);
703 if (!mpath) {
704 rcu_read_unlock();
705 sdata->u.mesh.mshstats.dropped_frames_no_route++;
706 return;
707 }
708 mesh_queue_preq(mpath,
709 PREQ_Q_F_START | PREQ_Q_F_REFRESH);
710 }
Rui Paulod19b3bf2009-11-09 23:46:55 +0000711 if (mpath->sn < orig_sn) {
Rui Paulo90a5e162009-11-11 00:01:31 +0000712 mesh_path_sel_frame_tx(MPATH_RANN, flags, orig_addr,
Rui Paulod19b3bf2009-11-09 23:46:55 +0000713 cpu_to_le32(orig_sn),
Johannes Berg15ff6362009-11-17 13:34:04 +0100714 0, NULL, 0, broadcast_addr,
Rui Pauloa6a58b42009-11-09 23:46:51 +0000715 hopcount, ttl, 0,
716 cpu_to_le32(metric + mpath->metric),
Rui Paulo90a5e162009-11-11 00:01:31 +0000717 0, sdata);
Rui Paulod19b3bf2009-11-09 23:46:55 +0000718 mpath->sn = orig_sn;
Rui Paulo90a5e162009-11-11 00:01:31 +0000719 }
720 rcu_read_unlock();
721}
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100722
723
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200724void mesh_rx_path_sel_frame(struct ieee80211_sub_if_data *sdata,
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100725 struct ieee80211_mgmt *mgmt,
726 size_t len)
727{
728 struct ieee802_11_elems elems;
729 size_t baselen;
730 u32 last_hop_metric;
731
Johannes Berg9c80d3d2008-09-08 15:41:59 +0200732 /* need action_code */
733 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
734 return;
735
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100736 baselen = (u8 *) mgmt->u.action.u.mesh_action.variable - (u8 *) mgmt;
737 ieee802_11_parse_elems(mgmt->u.action.u.mesh_action.variable,
738 len - baselen, &elems);
739
Rui Paulodbb81c42009-11-09 23:46:46 +0000740 if (elems.preq) {
741 if (elems.preq_len != 37)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100742 /* Right now we support just 1 destination and no AE */
743 return;
Rui Paulodbb81c42009-11-09 23:46:46 +0000744 last_hop_metric = hwmp_route_info_get(sdata, mgmt, elems.preq,
745 MPATH_PREQ);
746 if (last_hop_metric)
747 hwmp_preq_frame_process(sdata, mgmt, elems.preq,
748 last_hop_metric);
749 }
750 if (elems.prep) {
751 if (elems.prep_len != 31)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100752 /* Right now we support no AE */
753 return;
Rui Paulodbb81c42009-11-09 23:46:46 +0000754 last_hop_metric = hwmp_route_info_get(sdata, mgmt, elems.prep,
755 MPATH_PREP);
756 if (last_hop_metric)
757 hwmp_prep_frame_process(sdata, mgmt, elems.prep,
758 last_hop_metric);
759 }
760 if (elems.perr) {
Rui Paulod611f062009-11-09 23:46:50 +0000761 if (elems.perr_len != 15)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100762 /* Right now we support only one destination per PERR */
763 return;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200764 hwmp_perr_frame_process(sdata, mgmt, elems.perr);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100765 }
Rui Paulo90a5e162009-11-11 00:01:31 +0000766 if (elems.rann)
767 hwmp_rann_frame_process(sdata, mgmt, elems.rann);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100768}
769
770/**
771 * mesh_queue_preq - queue a PREQ to a given destination
772 *
773 * @mpath: mesh path to discover
774 * @flags: special attributes of the PREQ to be sent
775 *
776 * Locking: the function must be called from within a rcu read lock block.
777 *
778 */
779static void mesh_queue_preq(struct mesh_path *mpath, u8 flags)
780{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200781 struct ieee80211_sub_if_data *sdata = mpath->sdata;
Johannes Berg472dbc42008-09-11 00:01:49 +0200782 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100783 struct mesh_preq_queue *preq_node;
784
Andrey Yurovsky59615b52009-06-25 16:07:42 -0700785 preq_node = kmalloc(sizeof(struct mesh_preq_queue), GFP_ATOMIC);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100786 if (!preq_node) {
Rui Paulo27db2e42009-11-09 23:46:45 +0000787 mhwmp_dbg("could not allocate PREQ node\n");
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100788 return;
789 }
790
Johannes Berg472dbc42008-09-11 00:01:49 +0200791 spin_lock(&ifmsh->mesh_preq_queue_lock);
792 if (ifmsh->preq_queue_len == MAX_PREQ_QUEUE_LEN) {
793 spin_unlock(&ifmsh->mesh_preq_queue_lock);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100794 kfree(preq_node);
795 if (printk_ratelimit())
Rui Paulo27db2e42009-11-09 23:46:45 +0000796 mhwmp_dbg("PREQ node queue full\n");
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100797 return;
798 }
799
800 memcpy(preq_node->dst, mpath->dst, ETH_ALEN);
801 preq_node->flags = flags;
802
Johannes Berg472dbc42008-09-11 00:01:49 +0200803 list_add_tail(&preq_node->list, &ifmsh->preq_queue.list);
804 ++ifmsh->preq_queue_len;
805 spin_unlock(&ifmsh->mesh_preq_queue_lock);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100806
Johannes Berg472dbc42008-09-11 00:01:49 +0200807 if (time_after(jiffies, ifmsh->last_preq + min_preq_int_jiff(sdata)))
Luis R. Rodriguezc03e20f2009-08-04 15:06:26 -0700808 ieee80211_queue_work(&sdata->local->hw, &ifmsh->work);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100809
Johannes Berg472dbc42008-09-11 00:01:49 +0200810 else if (time_before(jiffies, ifmsh->last_preq)) {
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100811 /* avoid long wait if did not send preqs for a long time
812 * and jiffies wrapped around
813 */
Johannes Berg472dbc42008-09-11 00:01:49 +0200814 ifmsh->last_preq = jiffies - min_preq_int_jiff(sdata) - 1;
Luis R. Rodriguezc03e20f2009-08-04 15:06:26 -0700815 ieee80211_queue_work(&sdata->local->hw, &ifmsh->work);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100816 } else
Johannes Berg472dbc42008-09-11 00:01:49 +0200817 mod_timer(&ifmsh->mesh_path_timer, ifmsh->last_preq +
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100818 min_preq_int_jiff(sdata));
819}
820
821/**
822 * mesh_path_start_discovery - launch a path discovery from the PREQ queue
823 *
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200824 * @sdata: local mesh subif
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100825 */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200826void mesh_path_start_discovery(struct ieee80211_sub_if_data *sdata)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100827{
Johannes Berg472dbc42008-09-11 00:01:49 +0200828 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100829 struct mesh_preq_queue *preq_node;
830 struct mesh_path *mpath;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000831 u8 ttl, target_flags;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100832 u32 lifetime;
833
Johannes Berga43816d2009-07-10 11:39:26 +0200834 spin_lock_bh(&ifmsh->mesh_preq_queue_lock);
Johannes Berg472dbc42008-09-11 00:01:49 +0200835 if (!ifmsh->preq_queue_len ||
836 time_before(jiffies, ifmsh->last_preq +
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100837 min_preq_int_jiff(sdata))) {
Johannes Berga43816d2009-07-10 11:39:26 +0200838 spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100839 return;
840 }
841
Johannes Berg472dbc42008-09-11 00:01:49 +0200842 preq_node = list_first_entry(&ifmsh->preq_queue.list,
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100843 struct mesh_preq_queue, list);
844 list_del(&preq_node->list);
Johannes Berg472dbc42008-09-11 00:01:49 +0200845 --ifmsh->preq_queue_len;
Johannes Berga43816d2009-07-10 11:39:26 +0200846 spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100847
848 rcu_read_lock();
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200849 mpath = mesh_path_lookup(preq_node->dst, sdata);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100850 if (!mpath)
851 goto enddiscovery;
852
853 spin_lock_bh(&mpath->state_lock);
854 if (preq_node->flags & PREQ_Q_F_START) {
855 if (mpath->flags & MESH_PATH_RESOLVING) {
856 spin_unlock_bh(&mpath->state_lock);
857 goto enddiscovery;
858 } else {
859 mpath->flags &= ~MESH_PATH_RESOLVED;
860 mpath->flags |= MESH_PATH_RESOLVING;
861 mpath->discovery_retries = 0;
862 mpath->discovery_timeout = disc_timeout_jiff(sdata);
863 }
864 } else if (!(mpath->flags & MESH_PATH_RESOLVING) ||
865 mpath->flags & MESH_PATH_RESOLVED) {
866 mpath->flags &= ~MESH_PATH_RESOLVING;
867 spin_unlock_bh(&mpath->state_lock);
868 goto enddiscovery;
869 }
870
Johannes Berg472dbc42008-09-11 00:01:49 +0200871 ifmsh->last_preq = jiffies;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100872
Rui Paulod19b3bf2009-11-09 23:46:55 +0000873 if (time_after(jiffies, ifmsh->last_sn_update +
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100874 net_traversal_jiffies(sdata)) ||
Rui Paulod19b3bf2009-11-09 23:46:55 +0000875 time_before(jiffies, ifmsh->last_sn_update)) {
876 ++ifmsh->sn;
877 sdata->u.mesh.last_sn_update = jiffies;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100878 }
879 lifetime = default_lifetime(sdata);
Johannes Berg472dbc42008-09-11 00:01:49 +0200880 ttl = sdata->u.mesh.mshcfg.dot11MeshTTL;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100881 if (ttl == 0) {
Johannes Berg472dbc42008-09-11 00:01:49 +0200882 sdata->u.mesh.mshstats.dropped_frames_ttl++;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100883 spin_unlock_bh(&mpath->state_lock);
884 goto enddiscovery;
885 }
886
887 if (preq_node->flags & PREQ_Q_F_REFRESH)
Rui Paulod19b3bf2009-11-09 23:46:55 +0000888 target_flags = MP_F_DO;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100889 else
Rui Paulod19b3bf2009-11-09 23:46:55 +0000890 target_flags = MP_F_RF;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100891
892 spin_unlock_bh(&mpath->state_lock);
Johannes Berg47846c92009-11-25 17:46:19 +0100893 mesh_path_sel_frame_tx(MPATH_PREQ, 0, sdata->vif.addr,
Rui Paulod19b3bf2009-11-09 23:46:55 +0000894 cpu_to_le32(ifmsh->sn), target_flags, mpath->dst,
Johannes Berg15ff6362009-11-17 13:34:04 +0100895 cpu_to_le32(mpath->sn), broadcast_addr, 0,
Luis Carlos Coboaa2b5922008-02-29 14:30:32 -0800896 ttl, cpu_to_le32(lifetime), 0,
Johannes Berg472dbc42008-09-11 00:01:49 +0200897 cpu_to_le32(ifmsh->preq_id++), sdata);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100898 mod_timer(&mpath->timer, jiffies + mpath->discovery_timeout);
899
900enddiscovery:
901 rcu_read_unlock();
902 kfree(preq_node);
903}
904
905/**
Rami Rosen2182b832009-01-19 13:50:37 +0200906 * mesh_nexthop_lookup - put the appropriate next hop on a mesh frame
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100907 *
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +0200908 * @skb: 802.11 frame to be sent
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200909 * @sdata: network subif the frame will be sent through
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100910 *
911 * Returns: 0 if the next hop was found. Nonzero otherwise. If no next hop is
912 * found, the function will start a path discovery and queue the frame so it is
913 * sent when the path is resolved. This means the caller must not free the skb
914 * in this case.
915 */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200916int mesh_nexthop_lookup(struct sk_buff *skb,
917 struct ieee80211_sub_if_data *sdata)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100918{
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100919 struct sk_buff *skb_to_free = NULL;
920 struct mesh_path *mpath;
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +0200921 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000922 u8 *target_addr = hdr->addr3;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100923 int err = 0;
924
925 rcu_read_lock();
Rui Paulod19b3bf2009-11-09 23:46:55 +0000926 mpath = mesh_path_lookup(target_addr, sdata);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100927
928 if (!mpath) {
Rui Paulod19b3bf2009-11-09 23:46:55 +0000929 mesh_path_add(target_addr, sdata);
930 mpath = mesh_path_lookup(target_addr, sdata);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100931 if (!mpath) {
Johannes Berg472dbc42008-09-11 00:01:49 +0200932 sdata->u.mesh.mshstats.dropped_frames_no_route++;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100933 err = -ENOSPC;
934 goto endlookup;
935 }
936 }
937
938 if (mpath->flags & MESH_PATH_ACTIVE) {
Joe Perchesf64f9e72009-11-29 16:55:45 -0800939 if (time_after(jiffies,
Javier Cardona7b324d22009-12-09 18:43:01 -0800940 mpath->exp_time -
Joe Perchesf64f9e72009-11-29 16:55:45 -0800941 msecs_to_jiffies(sdata->u.mesh.mshcfg.path_refresh_time)) &&
Johannes Berg47846c92009-11-25 17:46:19 +0100942 !memcmp(sdata->vif.addr, hdr->addr4, ETH_ALEN) &&
Joe Perchesf64f9e72009-11-29 16:55:45 -0800943 !(mpath->flags & MESH_PATH_RESOLVING) &&
944 !(mpath->flags & MESH_PATH_FIXED)) {
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100945 mesh_queue_preq(mpath,
946 PREQ_Q_F_START | PREQ_Q_F_REFRESH);
947 }
Joe Perchesf64f9e72009-11-29 16:55:45 -0800948 memcpy(hdr->addr1, mpath->next_hop->sta.addr, ETH_ALEN);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100949 } else {
Javier Cardona249b4052009-07-07 10:55:03 -0700950 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100951 if (!(mpath->flags & MESH_PATH_RESOLVING)) {
952 /* Start discovery only if it is not running yet */
953 mesh_queue_preq(mpath, PREQ_Q_F_START);
954 }
955
956 if (skb_queue_len(&mpath->frame_queue) >=
Javier Cardonafe583432009-08-10 12:15:46 -0700957 MESH_FRAME_QUEUE_LEN)
958 skb_to_free = skb_dequeue(&mpath->frame_queue);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100959
Javier Cardona249b4052009-07-07 10:55:03 -0700960 info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100961 skb_queue_tail(&mpath->frame_queue, skb);
962 if (skb_to_free)
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200963 mesh_path_discard_frame(skb_to_free, sdata);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100964 err = -ENOENT;
965 }
966
967endlookup:
968 rcu_read_unlock();
969 return err;
970}
971
972void mesh_path_timer(unsigned long data)
973{
974 struct ieee80211_sub_if_data *sdata;
975 struct mesh_path *mpath;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100976
977 rcu_read_lock();
978 mpath = (struct mesh_path *) data;
979 mpath = rcu_dereference(mpath);
980 if (!mpath)
981 goto endmpathtimer;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200982 sdata = mpath->sdata;
Johannes Berg5bb644a2009-05-17 11:40:42 +0200983
984 if (sdata->local->quiescing) {
985 rcu_read_unlock();
986 return;
987 }
988
989 spin_lock_bh(&mpath->state_lock);
Luis Carlos Cobocfa22c72008-02-29 15:04:13 -0800990 if (mpath->flags & MESH_PATH_RESOLVED ||
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100991 (!(mpath->flags & MESH_PATH_RESOLVING)))
992 mpath->flags &= ~(MESH_PATH_RESOLVING | MESH_PATH_RESOLVED);
993 else if (mpath->discovery_retries < max_preq_retries(sdata)) {
994 ++mpath->discovery_retries;
995 mpath->discovery_timeout *= 2;
996 mesh_queue_preq(mpath, 0);
997 } else {
998 mpath->flags = 0;
999 mpath->exp_time = jiffies;
1000 mesh_path_flush_pending(mpath);
1001 }
1002
1003 spin_unlock_bh(&mpath->state_lock);
1004endmpathtimer:
1005 rcu_read_unlock();
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001006}
Rui Pauloe304bfd2009-11-09 23:46:56 +00001007
1008void
1009mesh_path_tx_root_frame(struct ieee80211_sub_if_data *sdata)
1010{
1011 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
1012
Johannes Berg47846c92009-11-25 17:46:19 +01001013 mesh_path_sel_frame_tx(MPATH_RANN, 0, sdata->vif.addr,
Rui Pauloe304bfd2009-11-09 23:46:56 +00001014 cpu_to_le32(++ifmsh->sn),
Johannes Berg15ff6362009-11-17 13:34:04 +01001015 0, NULL, 0, broadcast_addr,
Rui Pauloe304bfd2009-11-09 23:46:56 +00001016 0, MESH_TTL, 0, 0, 0, sdata);
1017}