Commit | Line | Data |
---|---|---|
050ac52c | 1 | /* |
264d9b7d | 2 | * Copyright (c) 2008, 2009 open80211s Ltd. |
050ac52c LCC |
3 | * 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 | ||
5a0e3ad6 | 10 | #include <linux/slab.h> |
050ac52c LCC |
11 | #include "mesh.h" |
12 | ||
27db2e42 | 13 | #ifdef CONFIG_MAC80211_VERBOSE_MHWMP_DEBUG |
7646887a JC |
14 | #define mhwmp_dbg(fmt, args...) \ |
15 | printk(KERN_DEBUG "Mesh HWMP (%s): " fmt "\n", sdata->name, ##args) | |
27db2e42 RP |
16 | #else |
17 | #define mhwmp_dbg(fmt, args...) do { (void)(0); } while (0) | |
18 | #endif | |
19 | ||
050ac52c LCC |
20 | #define TEST_FRAME_LEN 8192 |
21 | #define MAX_METRIC 0xffffffff | |
22 | #define ARITH_SHIFT 8 | |
23 | ||
24 | /* Number of frames buffered per destination for unresolved destinations */ | |
25 | #define MESH_FRAME_QUEUE_LEN 10 | |
26 | #define MAX_PREQ_QUEUE_LEN 64 | |
27 | ||
28 | /* Destination only */ | |
29 | #define MP_F_DO 0x1 | |
30 | /* Reply and forward */ | |
31 | #define MP_F_RF 0x2 | |
d611f062 RP |
32 | /* Unknown Sequence Number */ |
33 | #define MP_F_USN 0x01 | |
34 | /* Reason code Present */ | |
35 | #define MP_F_RCODE 0x02 | |
050ac52c | 36 | |
90a5e169 RP |
37 | static void mesh_queue_preq(struct mesh_path *, u8); |
38 | ||
a00de5d0 LCC |
39 | static inline u32 u32_field_get(u8 *preq_elem, int offset, bool ae) |
40 | { | |
41 | if (ae) | |
42 | offset += 6; | |
ae7245cb | 43 | return get_unaligned_le32(preq_elem + offset); |
a00de5d0 LCC |
44 | } |
45 | ||
d611f062 RP |
46 | static inline u32 u16_field_get(u8 *preq_elem, int offset, bool ae) |
47 | { | |
48 | if (ae) | |
49 | offset += 6; | |
50 | return get_unaligned_le16(preq_elem + offset); | |
51 | } | |
52 | ||
050ac52c | 53 | /* HWMP IE processing macros */ |
a00de5d0 LCC |
54 | #define AE_F (1<<6) |
55 | #define AE_F_SET(x) (*x & AE_F) | |
56 | #define PREQ_IE_FLAGS(x) (*(x)) | |
57 | #define PREQ_IE_HOPCOUNT(x) (*(x + 1)) | |
58 | #define PREQ_IE_TTL(x) (*(x + 2)) | |
59 | #define PREQ_IE_PREQ_ID(x) u32_field_get(x, 3, 0) | |
60 | #define PREQ_IE_ORIG_ADDR(x) (x + 7) | |
497888cf PC |
61 | #define PREQ_IE_ORIG_SN(x) u32_field_get(x, 13, 0) |
62 | #define PREQ_IE_LIFETIME(x) u32_field_get(x, 17, AE_F_SET(x)) | |
63 | #define PREQ_IE_METRIC(x) u32_field_get(x, 21, AE_F_SET(x)) | |
d19b3bf6 RP |
64 | #define PREQ_IE_TARGET_F(x) (*(AE_F_SET(x) ? x + 32 : x + 26)) |
65 | #define PREQ_IE_TARGET_ADDR(x) (AE_F_SET(x) ? x + 33 : x + 27) | |
497888cf | 66 | #define PREQ_IE_TARGET_SN(x) u32_field_get(x, 33, AE_F_SET(x)) |
a00de5d0 LCC |
67 | |
68 | ||
69 | #define PREP_IE_FLAGS(x) PREQ_IE_FLAGS(x) | |
70 | #define PREP_IE_HOPCOUNT(x) PREQ_IE_HOPCOUNT(x) | |
71 | #define PREP_IE_TTL(x) PREQ_IE_TTL(x) | |
25d49e4d TP |
72 | #define PREP_IE_ORIG_ADDR(x) (AE_F_SET(x) ? x + 27 : x + 21) |
73 | #define PREP_IE_ORIG_SN(x) u32_field_get(x, 27, AE_F_SET(x)) | |
497888cf PC |
74 | #define PREP_IE_LIFETIME(x) u32_field_get(x, 13, AE_F_SET(x)) |
75 | #define PREP_IE_METRIC(x) u32_field_get(x, 17, AE_F_SET(x)) | |
25d49e4d TP |
76 | #define PREP_IE_TARGET_ADDR(x) (x + 3) |
77 | #define PREP_IE_TARGET_SN(x) u32_field_get(x, 9, 0) | |
a00de5d0 | 78 | |
d611f062 | 79 | #define PERR_IE_TTL(x) (*(x)) |
d19b3bf6 RP |
80 | #define PERR_IE_TARGET_FLAGS(x) (*(x + 2)) |
81 | #define PERR_IE_TARGET_ADDR(x) (x + 3) | |
497888cf PC |
82 | #define PERR_IE_TARGET_SN(x) u32_field_get(x, 9, 0) |
83 | #define PERR_IE_TARGET_RCODE(x) u16_field_get(x, 13, 0) | |
050ac52c | 84 | |
050ac52c | 85 | #define MSEC_TO_TU(x) (x*1000/1024) |
d19b3bf6 RP |
86 | #define SN_GT(x, y) ((long) (y) - (long) (x) < 0) |
87 | #define SN_LT(x, y) ((long) (x) - (long) (y) < 0) | |
050ac52c LCC |
88 | |
89 | #define net_traversal_jiffies(s) \ | |
472dbc45 | 90 | msecs_to_jiffies(s->u.mesh.mshcfg.dot11MeshHWMPnetDiameterTraversalTime) |
050ac52c | 91 | #define default_lifetime(s) \ |
472dbc45 | 92 | MSEC_TO_TU(s->u.mesh.mshcfg.dot11MeshHWMPactivePathTimeout) |
050ac52c | 93 | #define min_preq_int_jiff(s) \ |
472dbc45 JB |
94 | (msecs_to_jiffies(s->u.mesh.mshcfg.dot11MeshHWMPpreqMinInterval)) |
95 | #define max_preq_retries(s) (s->u.mesh.mshcfg.dot11MeshHWMPmaxPREQretries) | |
050ac52c | 96 | #define disc_timeout_jiff(s) \ |
472dbc45 | 97 | msecs_to_jiffies(sdata->u.mesh.mshcfg.min_discovery_timeout) |
050ac52c LCC |
98 | |
99 | enum mpath_frame_type { | |
100 | MPATH_PREQ = 0, | |
101 | MPATH_PREP, | |
90a5e169 RP |
102 | MPATH_PERR, |
103 | MPATH_RANN | |
050ac52c LCC |
104 | }; |
105 | ||
15ff6365 JB |
106 | static const u8 broadcast_addr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; |
107 | ||
050ac52c | 108 | static int mesh_path_sel_frame_tx(enum mpath_frame_type action, u8 flags, |
d19b3bf6 | 109 | u8 *orig_addr, __le32 orig_sn, u8 target_flags, u8 *target, |
15ff6365 JB |
110 | __le32 target_sn, const u8 *da, u8 hop_count, u8 ttl, |
111 | __le32 lifetime, __le32 metric, __le32 preq_id, | |
d19b3bf6 | 112 | struct ieee80211_sub_if_data *sdata) |
050ac52c | 113 | { |
f698d856 | 114 | struct ieee80211_local *local = sdata->local; |
050ac52c LCC |
115 | struct sk_buff *skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400); |
116 | struct ieee80211_mgmt *mgmt; | |
117 | u8 *pos; | |
118 | int ie_len; | |
119 | ||
120 | if (!skb) | |
121 | return -1; | |
122 | skb_reserve(skb, local->hw.extra_tx_headroom); | |
123 | /* 25 is the size of the common mgmt part (24) plus the size of the | |
124 | * common action part (1) | |
125 | */ | |
126 | mgmt = (struct ieee80211_mgmt *) | |
127 | skb_put(skb, 25 + sizeof(mgmt->u.action.u.mesh_action)); | |
128 | memset(mgmt, 0, 25 + sizeof(mgmt->u.action.u.mesh_action)); | |
e7827a70 HH |
129 | mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | |
130 | IEEE80211_STYPE_ACTION); | |
050ac52c LCC |
131 | |
132 | memcpy(mgmt->da, da, ETH_ALEN); | |
47846c9b | 133 | memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN); |
90a5e169 | 134 | /* BSSID == SA */ |
47846c9b | 135 | memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN); |
25d49e4d TP |
136 | mgmt->u.action.category = WLAN_CATEGORY_MESH_ACTION; |
137 | mgmt->u.action.u.mesh_action.action_code = | |
138 | WLAN_MESH_ACTION_HWMP_PATH_SELECTION; | |
050ac52c LCC |
139 | |
140 | switch (action) { | |
141 | case MPATH_PREQ: | |
7646887a | 142 | mhwmp_dbg("sending PREQ to %pM", target); |
050ac52c LCC |
143 | ie_len = 37; |
144 | pos = skb_put(skb, 2 + ie_len); | |
145 | *pos++ = WLAN_EID_PREQ; | |
146 | break; | |
147 | case MPATH_PREP: | |
7646887a | 148 | mhwmp_dbg("sending PREP to %pM", target); |
050ac52c LCC |
149 | ie_len = 31; |
150 | pos = skb_put(skb, 2 + ie_len); | |
151 | *pos++ = WLAN_EID_PREP; | |
152 | break; | |
90a5e169 | 153 | case MPATH_RANN: |
7646887a | 154 | mhwmp_dbg("sending RANN from %pM", orig_addr); |
90a5e169 RP |
155 | ie_len = sizeof(struct ieee80211_rann_ie); |
156 | pos = skb_put(skb, 2 + ie_len); | |
157 | *pos++ = WLAN_EID_RANN; | |
158 | break; | |
050ac52c | 159 | default: |
812714d7 | 160 | kfree_skb(skb); |
050ac52c LCC |
161 | return -ENOTSUPP; |
162 | break; | |
163 | } | |
164 | *pos++ = ie_len; | |
165 | *pos++ = flags; | |
166 | *pos++ = hop_count; | |
167 | *pos++ = ttl; | |
25d49e4d TP |
168 | if (action == MPATH_PREP) { |
169 | memcpy(pos, target, ETH_ALEN); | |
170 | pos += ETH_ALEN; | |
171 | memcpy(pos, &target_sn, 4); | |
050ac52c | 172 | pos += 4; |
25d49e4d TP |
173 | } else { |
174 | if (action == MPATH_PREQ) { | |
175 | memcpy(pos, &preq_id, 4); | |
176 | pos += 4; | |
177 | } | |
178 | memcpy(pos, orig_addr, ETH_ALEN); | |
179 | pos += ETH_ALEN; | |
180 | memcpy(pos, &orig_sn, 4); | |
90a5e169 RP |
181 | pos += 4; |
182 | } | |
25d49e4d TP |
183 | memcpy(pos, &lifetime, 4); /* interval for RANN */ |
184 | pos += 4; | |
050ac52c LCC |
185 | memcpy(pos, &metric, 4); |
186 | pos += 4; | |
187 | if (action == MPATH_PREQ) { | |
25d49e4d | 188 | *pos++ = 1; /* destination count */ |
d19b3bf6 | 189 | *pos++ = target_flags; |
d19b3bf6 | 190 | memcpy(pos, target, ETH_ALEN); |
90a5e169 | 191 | pos += ETH_ALEN; |
d19b3bf6 | 192 | memcpy(pos, &target_sn, 4); |
25d49e4d TP |
193 | pos += 4; |
194 | } else if (action == MPATH_PREP) { | |
195 | memcpy(pos, orig_addr, ETH_ALEN); | |
196 | pos += ETH_ALEN; | |
197 | memcpy(pos, &orig_sn, 4); | |
198 | pos += 4; | |
90a5e169 | 199 | } |
050ac52c | 200 | |
62ae67be | 201 | ieee80211_tx_skb(sdata, skb); |
050ac52c LCC |
202 | return 0; |
203 | } | |
204 | ||
205 | /** | |
206 | * mesh_send_path error - Sends a PERR mesh management frame | |
207 | * | |
d19b3bf6 RP |
208 | * @target: broken destination |
209 | * @target_sn: SN of the broken destination | |
210 | * @target_rcode: reason code for this PERR | |
050ac52c LCC |
211 | * @ra: node this frame is addressed to |
212 | */ | |
d19b3bf6 | 213 | int mesh_path_error_tx(u8 ttl, u8 *target, __le32 target_sn, |
15ff6365 JB |
214 | __le16 target_rcode, const u8 *ra, |
215 | struct ieee80211_sub_if_data *sdata) | |
050ac52c | 216 | { |
f698d856 | 217 | struct ieee80211_local *local = sdata->local; |
050ac52c LCC |
218 | struct sk_buff *skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400); |
219 | struct ieee80211_mgmt *mgmt; | |
220 | u8 *pos; | |
221 | int ie_len; | |
222 | ||
223 | if (!skb) | |
224 | return -1; | |
225 | skb_reserve(skb, local->hw.extra_tx_headroom); | |
226 | /* 25 is the size of the common mgmt part (24) plus the size of the | |
227 | * common action part (1) | |
228 | */ | |
229 | mgmt = (struct ieee80211_mgmt *) | |
230 | skb_put(skb, 25 + sizeof(mgmt->u.action.u.mesh_action)); | |
231 | memset(mgmt, 0, 25 + sizeof(mgmt->u.action.u.mesh_action)); | |
e7827a70 HH |
232 | mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | |
233 | IEEE80211_STYPE_ACTION); | |
050ac52c LCC |
234 | |
235 | memcpy(mgmt->da, ra, ETH_ALEN); | |
47846c9b | 236 | memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN); |
25d49e4d TP |
237 | /* BSSID == SA */ |
238 | memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN); | |
239 | mgmt->u.action.category = WLAN_CATEGORY_MESH_ACTION; | |
240 | mgmt->u.action.u.mesh_action.action_code = | |
241 | WLAN_MESH_ACTION_HWMP_PATH_SELECTION; | |
d611f062 | 242 | ie_len = 15; |
050ac52c LCC |
243 | pos = skb_put(skb, 2 + ie_len); |
244 | *pos++ = WLAN_EID_PERR; | |
245 | *pos++ = ie_len; | |
d611f062 | 246 | /* ttl */ |
45904f21 | 247 | *pos++ = ttl; |
050ac52c LCC |
248 | /* number of destinations */ |
249 | *pos++ = 1; | |
d611f062 RP |
250 | /* |
251 | * flags bit, bit 1 is unset if we know the sequence number and | |
252 | * bit 2 is set if we have a reason code | |
253 | */ | |
254 | *pos = 0; | |
d19b3bf6 | 255 | if (!target_sn) |
d611f062 | 256 | *pos |= MP_F_USN; |
d19b3bf6 | 257 | if (target_rcode) |
d611f062 RP |
258 | *pos |= MP_F_RCODE; |
259 | pos++; | |
d19b3bf6 | 260 | memcpy(pos, target, ETH_ALEN); |
050ac52c | 261 | pos += ETH_ALEN; |
d19b3bf6 | 262 | memcpy(pos, &target_sn, 4); |
d611f062 | 263 | pos += 4; |
d19b3bf6 | 264 | memcpy(pos, &target_rcode, 2); |
050ac52c | 265 | |
62ae67be | 266 | ieee80211_tx_skb(sdata, skb); |
050ac52c LCC |
267 | return 0; |
268 | } | |
269 | ||
bfc32e6a JC |
270 | void ieee80211s_update_metric(struct ieee80211_local *local, |
271 | struct sta_info *stainfo, struct sk_buff *skb) | |
272 | { | |
273 | struct ieee80211_tx_info *txinfo = IEEE80211_SKB_CB(skb); | |
274 | struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data; | |
275 | int failed; | |
276 | ||
277 | if (!ieee80211_is_data(hdr->frame_control)) | |
278 | return; | |
279 | ||
280 | failed = !(txinfo->flags & IEEE80211_TX_STAT_ACK); | |
281 | ||
282 | /* moving average, scaled to 100 */ | |
283 | stainfo->fail_avg = ((80 * stainfo->fail_avg + 5) / 100 + 20 * failed); | |
284 | if (stainfo->fail_avg > 95) | |
285 | mesh_plink_broken(stainfo); | |
286 | } | |
287 | ||
050ac52c LCC |
288 | static u32 airtime_link_metric_get(struct ieee80211_local *local, |
289 | struct sta_info *sta) | |
290 | { | |
291 | struct ieee80211_supported_band *sband; | |
292 | /* This should be adjusted for each device */ | |
293 | int device_constant = 1 << ARITH_SHIFT; | |
294 | int test_frame_len = TEST_FRAME_LEN << ARITH_SHIFT; | |
295 | int s_unit = 1 << ARITH_SHIFT; | |
296 | int rate, err; | |
297 | u32 tx_time, estimated_retx; | |
298 | u64 result; | |
299 | ||
300 | sband = local->hw.wiphy->bands[local->hw.conf.channel->band]; | |
301 | ||
302 | if (sta->fail_avg >= 100) | |
303 | return MAX_METRIC; | |
e6a9854b JB |
304 | |
305 | if (sta->last_tx_rate.flags & IEEE80211_TX_RC_MCS) | |
306 | return MAX_METRIC; | |
307 | ||
050ac52c LCC |
308 | err = (sta->fail_avg << ARITH_SHIFT) / 100; |
309 | ||
310 | /* bitrate is in units of 100 Kbps, while we need rate in units of | |
311 | * 1Mbps. This will be corrected on tx_time computation. | |
312 | */ | |
e6a9854b | 313 | rate = sband->bitrates[sta->last_tx_rate.idx].bitrate; |
050ac52c LCC |
314 | tx_time = (device_constant + 10 * test_frame_len / rate); |
315 | estimated_retx = ((1 << (2 * ARITH_SHIFT)) / (s_unit - err)); | |
316 | result = (tx_time * estimated_retx) >> (2 * ARITH_SHIFT) ; | |
317 | return (u32)result; | |
318 | } | |
319 | ||
320 | /** | |
321 | * hwmp_route_info_get - Update routing info to originator and transmitter | |
322 | * | |
f698d856 | 323 | * @sdata: local mesh subif |
050ac52c LCC |
324 | * @mgmt: mesh management frame |
325 | * @hwmp_ie: hwmp information element (PREP or PREQ) | |
326 | * | |
327 | * This function updates the path routing information to the originator and the | |
f99288d1 | 328 | * transmitter of a HWMP PREQ or PREP frame. |
050ac52c LCC |
329 | * |
330 | * Returns: metric to frame originator or 0 if the frame should not be further | |
331 | * processed | |
332 | * | |
333 | * Notes: this function is the only place (besides user-provided info) where | |
334 | * path routing information is updated. | |
335 | */ | |
f698d856 | 336 | static u32 hwmp_route_info_get(struct ieee80211_sub_if_data *sdata, |
050ac52c | 337 | struct ieee80211_mgmt *mgmt, |
dbb81c42 | 338 | u8 *hwmp_ie, enum mpath_frame_type action) |
050ac52c | 339 | { |
f698d856 | 340 | struct ieee80211_local *local = sdata->local; |
050ac52c LCC |
341 | struct mesh_path *mpath; |
342 | struct sta_info *sta; | |
343 | bool fresh_info; | |
344 | u8 *orig_addr, *ta; | |
d19b3bf6 | 345 | u32 orig_sn, orig_metric; |
050ac52c LCC |
346 | unsigned long orig_lifetime, exp_time; |
347 | u32 last_hop_metric, new_metric; | |
348 | bool process = true; | |
050ac52c LCC |
349 | |
350 | rcu_read_lock(); | |
abe60632 | 351 | sta = sta_info_get(sdata, mgmt->sa); |
dc0b0f7d JB |
352 | if (!sta) { |
353 | rcu_read_unlock(); | |
050ac52c | 354 | return 0; |
dc0b0f7d | 355 | } |
050ac52c LCC |
356 | |
357 | last_hop_metric = airtime_link_metric_get(local, sta); | |
358 | /* Update and check originator routing info */ | |
359 | fresh_info = true; | |
360 | ||
361 | switch (action) { | |
362 | case MPATH_PREQ: | |
363 | orig_addr = PREQ_IE_ORIG_ADDR(hwmp_ie); | |
d19b3bf6 | 364 | orig_sn = PREQ_IE_ORIG_SN(hwmp_ie); |
050ac52c LCC |
365 | orig_lifetime = PREQ_IE_LIFETIME(hwmp_ie); |
366 | orig_metric = PREQ_IE_METRIC(hwmp_ie); | |
367 | break; | |
368 | case MPATH_PREP: | |
369 | /* Originator here refers to the MP that was the destination in | |
370 | * the Path Request. The draft refers to that MP as the | |
371 | * destination address, even though usually it is the origin of | |
372 | * the PREP frame. We divert from the nomenclature in the draft | |
373 | * so that we can easily use a single function to gather path | |
374 | * information from both PREQ and PREP frames. | |
375 | */ | |
376 | orig_addr = PREP_IE_ORIG_ADDR(hwmp_ie); | |
d19b3bf6 | 377 | orig_sn = PREP_IE_ORIG_SN(hwmp_ie); |
050ac52c LCC |
378 | orig_lifetime = PREP_IE_LIFETIME(hwmp_ie); |
379 | orig_metric = PREP_IE_METRIC(hwmp_ie); | |
380 | break; | |
381 | default: | |
dc0b0f7d | 382 | rcu_read_unlock(); |
050ac52c LCC |
383 | return 0; |
384 | } | |
385 | new_metric = orig_metric + last_hop_metric; | |
386 | if (new_metric < orig_metric) | |
387 | new_metric = MAX_METRIC; | |
388 | exp_time = TU_TO_EXP_TIME(orig_lifetime); | |
389 | ||
47846c9b | 390 | if (memcmp(orig_addr, sdata->vif.addr, ETH_ALEN) == 0) { |
050ac52c LCC |
391 | /* This MP is the originator, we are not interested in this |
392 | * frame, except for updating transmitter's path info. | |
393 | */ | |
394 | process = false; | |
395 | fresh_info = false; | |
396 | } else { | |
f698d856 | 397 | mpath = mesh_path_lookup(orig_addr, sdata); |
050ac52c LCC |
398 | if (mpath) { |
399 | spin_lock_bh(&mpath->state_lock); | |
400 | if (mpath->flags & MESH_PATH_FIXED) | |
401 | fresh_info = false; | |
402 | else if ((mpath->flags & MESH_PATH_ACTIVE) && | |
d19b3bf6 RP |
403 | (mpath->flags & MESH_PATH_SN_VALID)) { |
404 | if (SN_GT(mpath->sn, orig_sn) || | |
405 | (mpath->sn == orig_sn && | |
533866b1 | 406 | new_metric >= mpath->metric)) { |
050ac52c LCC |
407 | process = false; |
408 | fresh_info = false; | |
409 | } | |
410 | } | |
411 | } else { | |
f698d856 JBG |
412 | mesh_path_add(orig_addr, sdata); |
413 | mpath = mesh_path_lookup(orig_addr, sdata); | |
050ac52c LCC |
414 | if (!mpath) { |
415 | rcu_read_unlock(); | |
050ac52c LCC |
416 | return 0; |
417 | } | |
418 | spin_lock_bh(&mpath->state_lock); | |
419 | } | |
420 | ||
421 | if (fresh_info) { | |
422 | mesh_path_assign_nexthop(mpath, sta); | |
d19b3bf6 | 423 | mpath->flags |= MESH_PATH_SN_VALID; |
050ac52c | 424 | mpath->metric = new_metric; |
d19b3bf6 | 425 | mpath->sn = orig_sn; |
050ac52c LCC |
426 | mpath->exp_time = time_after(mpath->exp_time, exp_time) |
427 | ? mpath->exp_time : exp_time; | |
428 | mesh_path_activate(mpath); | |
429 | spin_unlock_bh(&mpath->state_lock); | |
430 | mesh_path_tx_pending(mpath); | |
431 | /* draft says preq_id should be saved to, but there does | |
432 | * not seem to be any use for it, skipping by now | |
433 | */ | |
434 | } else | |
435 | spin_unlock_bh(&mpath->state_lock); | |
436 | } | |
437 | ||
438 | /* Update and check transmitter routing info */ | |
439 | ta = mgmt->sa; | |
440 | if (memcmp(orig_addr, ta, ETH_ALEN) == 0) | |
441 | fresh_info = false; | |
442 | else { | |
443 | fresh_info = true; | |
444 | ||
f698d856 | 445 | mpath = mesh_path_lookup(ta, sdata); |
050ac52c LCC |
446 | if (mpath) { |
447 | spin_lock_bh(&mpath->state_lock); | |
448 | if ((mpath->flags & MESH_PATH_FIXED) || | |
449 | ((mpath->flags & MESH_PATH_ACTIVE) && | |
450 | (last_hop_metric > mpath->metric))) | |
451 | fresh_info = false; | |
452 | } else { | |
f698d856 JBG |
453 | mesh_path_add(ta, sdata); |
454 | mpath = mesh_path_lookup(ta, sdata); | |
050ac52c LCC |
455 | if (!mpath) { |
456 | rcu_read_unlock(); | |
050ac52c LCC |
457 | return 0; |
458 | } | |
459 | spin_lock_bh(&mpath->state_lock); | |
460 | } | |
461 | ||
462 | if (fresh_info) { | |
463 | mesh_path_assign_nexthop(mpath, sta); | |
050ac52c LCC |
464 | mpath->metric = last_hop_metric; |
465 | mpath->exp_time = time_after(mpath->exp_time, exp_time) | |
466 | ? mpath->exp_time : exp_time; | |
467 | mesh_path_activate(mpath); | |
468 | spin_unlock_bh(&mpath->state_lock); | |
469 | mesh_path_tx_pending(mpath); | |
470 | } else | |
471 | spin_unlock_bh(&mpath->state_lock); | |
472 | } | |
473 | ||
050ac52c LCC |
474 | rcu_read_unlock(); |
475 | ||
476 | return process ? new_metric : 0; | |
477 | } | |
478 | ||
f698d856 | 479 | static void hwmp_preq_frame_process(struct ieee80211_sub_if_data *sdata, |
050ac52c | 480 | struct ieee80211_mgmt *mgmt, |
57ef5ddb DW |
481 | u8 *preq_elem, u32 metric) |
482 | { | |
472dbc45 | 483 | struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh; |
050ac52c | 484 | struct mesh_path *mpath; |
d19b3bf6 RP |
485 | u8 *target_addr, *orig_addr; |
486 | u8 target_flags, ttl; | |
487 | u32 orig_sn, target_sn, lifetime; | |
050ac52c LCC |
488 | bool reply = false; |
489 | bool forward = true; | |
490 | ||
d19b3bf6 RP |
491 | /* Update target SN, if present */ |
492 | target_addr = PREQ_IE_TARGET_ADDR(preq_elem); | |
050ac52c | 493 | orig_addr = PREQ_IE_ORIG_ADDR(preq_elem); |
d19b3bf6 RP |
494 | target_sn = PREQ_IE_TARGET_SN(preq_elem); |
495 | orig_sn = PREQ_IE_ORIG_SN(preq_elem); | |
496 | target_flags = PREQ_IE_TARGET_F(preq_elem); | |
050ac52c | 497 | |
7646887a | 498 | mhwmp_dbg("received PREQ from %pM", orig_addr); |
27db2e42 | 499 | |
47846c9b | 500 | if (memcmp(target_addr, sdata->vif.addr, ETH_ALEN) == 0) { |
7646887a | 501 | mhwmp_dbg("PREQ is for us"); |
050ac52c LCC |
502 | forward = false; |
503 | reply = true; | |
504 | metric = 0; | |
d19b3bf6 | 505 | if (time_after(jiffies, ifmsh->last_sn_update + |
050ac52c | 506 | net_traversal_jiffies(sdata)) || |
d19b3bf6 RP |
507 | time_before(jiffies, ifmsh->last_sn_update)) { |
508 | target_sn = ++ifmsh->sn; | |
509 | ifmsh->last_sn_update = jiffies; | |
050ac52c LCC |
510 | } |
511 | } else { | |
512 | rcu_read_lock(); | |
d19b3bf6 | 513 | mpath = mesh_path_lookup(target_addr, sdata); |
050ac52c | 514 | if (mpath) { |
d19b3bf6 RP |
515 | if ((!(mpath->flags & MESH_PATH_SN_VALID)) || |
516 | SN_LT(mpath->sn, target_sn)) { | |
517 | mpath->sn = target_sn; | |
518 | mpath->flags |= MESH_PATH_SN_VALID; | |
519 | } else if ((!(target_flags & MP_F_DO)) && | |
050ac52c LCC |
520 | (mpath->flags & MESH_PATH_ACTIVE)) { |
521 | reply = true; | |
522 | metric = mpath->metric; | |
d19b3bf6 RP |
523 | target_sn = mpath->sn; |
524 | if (target_flags & MP_F_RF) | |
525 | target_flags |= MP_F_DO; | |
050ac52c LCC |
526 | else |
527 | forward = false; | |
528 | } | |
529 | } | |
530 | rcu_read_unlock(); | |
531 | } | |
532 | ||
533 | if (reply) { | |
534 | lifetime = PREQ_IE_LIFETIME(preq_elem); | |
45904f21 | 535 | ttl = ifmsh->mshcfg.element_ttl; |
27db2e42 | 536 | if (ttl != 0) { |
7646887a | 537 | mhwmp_dbg("replying to the PREQ"); |
d19b3bf6 RP |
538 | mesh_path_sel_frame_tx(MPATH_PREP, 0, target_addr, |
539 | cpu_to_le32(target_sn), 0, orig_addr, | |
540 | cpu_to_le32(orig_sn), mgmt->sa, 0, ttl, | |
aa2b5928 | 541 | cpu_to_le32(lifetime), cpu_to_le32(metric), |
f698d856 | 542 | 0, sdata); |
27db2e42 | 543 | } else |
472dbc45 | 544 | ifmsh->mshstats.dropped_frames_ttl++; |
050ac52c LCC |
545 | } |
546 | ||
547 | if (forward) { | |
548 | u32 preq_id; | |
549 | u8 hopcount, flags; | |
550 | ||
551 | ttl = PREQ_IE_TTL(preq_elem); | |
552 | lifetime = PREQ_IE_LIFETIME(preq_elem); | |
553 | if (ttl <= 1) { | |
472dbc45 | 554 | ifmsh->mshstats.dropped_frames_ttl++; |
050ac52c LCC |
555 | return; |
556 | } | |
7646887a | 557 | mhwmp_dbg("forwarding the PREQ from %pM", orig_addr); |
050ac52c LCC |
558 | --ttl; |
559 | flags = PREQ_IE_FLAGS(preq_elem); | |
560 | preq_id = PREQ_IE_PREQ_ID(preq_elem); | |
561 | hopcount = PREQ_IE_HOPCOUNT(preq_elem) + 1; | |
562 | mesh_path_sel_frame_tx(MPATH_PREQ, flags, orig_addr, | |
d19b3bf6 | 563 | cpu_to_le32(orig_sn), target_flags, target_addr, |
15ff6365 | 564 | cpu_to_le32(target_sn), broadcast_addr, |
aa2b5928 LCC |
565 | hopcount, ttl, cpu_to_le32(lifetime), |
566 | cpu_to_le32(metric), cpu_to_le32(preq_id), | |
f698d856 | 567 | sdata); |
c8a61a7d | 568 | ifmsh->mshstats.fwded_mcast++; |
472dbc45 | 569 | ifmsh->mshstats.fwded_frames++; |
050ac52c LCC |
570 | } |
571 | } | |
572 | ||
573 | ||
40b275b6 JB |
574 | static inline struct sta_info * |
575 | next_hop_deref_protected(struct mesh_path *mpath) | |
576 | { | |
577 | return rcu_dereference_protected(mpath->next_hop, | |
578 | lockdep_is_held(&mpath->state_lock)); | |
579 | } | |
580 | ||
581 | ||
f698d856 | 582 | static void hwmp_prep_frame_process(struct ieee80211_sub_if_data *sdata, |
050ac52c LCC |
583 | struct ieee80211_mgmt *mgmt, |
584 | u8 *prep_elem, u32 metric) | |
585 | { | |
050ac52c | 586 | struct mesh_path *mpath; |
d19b3bf6 | 587 | u8 *target_addr, *orig_addr; |
050ac52c LCC |
588 | u8 ttl, hopcount, flags; |
589 | u8 next_hop[ETH_ALEN]; | |
d19b3bf6 | 590 | u32 target_sn, orig_sn, lifetime; |
050ac52c | 591 | |
7646887a | 592 | mhwmp_dbg("received PREP from %pM", PREP_IE_ORIG_ADDR(prep_elem)); |
dbb81c42 | 593 | |
050ac52c LCC |
594 | /* Note that we divert from the draft nomenclature and denominate |
595 | * destination to what the draft refers to as origininator. So in this | |
596 | * function destnation refers to the final destination of the PREP, | |
597 | * which corresponds with the originator of the PREQ which this PREP | |
598 | * replies | |
599 | */ | |
d19b3bf6 | 600 | target_addr = PREP_IE_TARGET_ADDR(prep_elem); |
47846c9b | 601 | if (memcmp(target_addr, sdata->vif.addr, ETH_ALEN) == 0) |
050ac52c LCC |
602 | /* destination, no forwarding required */ |
603 | return; | |
604 | ||
605 | ttl = PREP_IE_TTL(prep_elem); | |
606 | if (ttl <= 1) { | |
472dbc45 | 607 | sdata->u.mesh.mshstats.dropped_frames_ttl++; |
050ac52c LCC |
608 | return; |
609 | } | |
610 | ||
611 | rcu_read_lock(); | |
d19b3bf6 | 612 | mpath = mesh_path_lookup(target_addr, sdata); |
050ac52c LCC |
613 | if (mpath) |
614 | spin_lock_bh(&mpath->state_lock); | |
615 | else | |
616 | goto fail; | |
617 | if (!(mpath->flags & MESH_PATH_ACTIVE)) { | |
618 | spin_unlock_bh(&mpath->state_lock); | |
619 | goto fail; | |
620 | } | |
40b275b6 | 621 | memcpy(next_hop, next_hop_deref_protected(mpath)->sta.addr, ETH_ALEN); |
050ac52c LCC |
622 | spin_unlock_bh(&mpath->state_lock); |
623 | --ttl; | |
624 | flags = PREP_IE_FLAGS(prep_elem); | |
625 | lifetime = PREP_IE_LIFETIME(prep_elem); | |
626 | hopcount = PREP_IE_HOPCOUNT(prep_elem) + 1; | |
627 | orig_addr = PREP_IE_ORIG_ADDR(prep_elem); | |
d19b3bf6 RP |
628 | target_sn = PREP_IE_TARGET_SN(prep_elem); |
629 | orig_sn = PREP_IE_ORIG_SN(prep_elem); | |
050ac52c LCC |
630 | |
631 | mesh_path_sel_frame_tx(MPATH_PREP, flags, orig_addr, | |
d19b3bf6 | 632 | cpu_to_le32(orig_sn), 0, target_addr, |
533866b1 | 633 | cpu_to_le32(target_sn), next_hop, hopcount, |
d19b3bf6 | 634 | ttl, cpu_to_le32(lifetime), cpu_to_le32(metric), |
f698d856 | 635 | 0, sdata); |
050ac52c | 636 | rcu_read_unlock(); |
c8a61a7d DW |
637 | |
638 | sdata->u.mesh.mshstats.fwded_unicast++; | |
472dbc45 | 639 | sdata->u.mesh.mshstats.fwded_frames++; |
050ac52c LCC |
640 | return; |
641 | ||
642 | fail: | |
643 | rcu_read_unlock(); | |
472dbc45 | 644 | sdata->u.mesh.mshstats.dropped_frames_no_route++; |
050ac52c LCC |
645 | } |
646 | ||
f698d856 | 647 | static void hwmp_perr_frame_process(struct ieee80211_sub_if_data *sdata, |
050ac52c LCC |
648 | struct ieee80211_mgmt *mgmt, u8 *perr_elem) |
649 | { | |
d611f062 | 650 | struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh; |
050ac52c | 651 | struct mesh_path *mpath; |
d611f062 | 652 | u8 ttl; |
d19b3bf6 | 653 | u8 *ta, *target_addr; |
d19b3bf6 RP |
654 | u32 target_sn; |
655 | u16 target_rcode; | |
050ac52c LCC |
656 | |
657 | ta = mgmt->sa; | |
d611f062 RP |
658 | ttl = PERR_IE_TTL(perr_elem); |
659 | if (ttl <= 1) { | |
660 | ifmsh->mshstats.dropped_frames_ttl++; | |
661 | return; | |
662 | } | |
663 | ttl--; | |
d19b3bf6 RP |
664 | target_addr = PERR_IE_TARGET_ADDR(perr_elem); |
665 | target_sn = PERR_IE_TARGET_SN(perr_elem); | |
666 | target_rcode = PERR_IE_TARGET_RCODE(perr_elem); | |
d611f062 | 667 | |
050ac52c | 668 | rcu_read_lock(); |
d19b3bf6 | 669 | mpath = mesh_path_lookup(target_addr, sdata); |
050ac52c LCC |
670 | if (mpath) { |
671 | spin_lock_bh(&mpath->state_lock); | |
672 | if (mpath->flags & MESH_PATH_ACTIVE && | |
40b275b6 JB |
673 | memcmp(ta, next_hop_deref_protected(mpath)->sta.addr, |
674 | ETH_ALEN) == 0 && | |
d19b3bf6 RP |
675 | (!(mpath->flags & MESH_PATH_SN_VALID) || |
676 | SN_GT(target_sn, mpath->sn))) { | |
050ac52c | 677 | mpath->flags &= ~MESH_PATH_ACTIVE; |
d19b3bf6 | 678 | mpath->sn = target_sn; |
050ac52c | 679 | spin_unlock_bh(&mpath->state_lock); |
d19b3bf6 RP |
680 | mesh_path_error_tx(ttl, target_addr, cpu_to_le32(target_sn), |
681 | cpu_to_le16(target_rcode), | |
15ff6365 | 682 | broadcast_addr, sdata); |
050ac52c LCC |
683 | } else |
684 | spin_unlock_bh(&mpath->state_lock); | |
685 | } | |
686 | rcu_read_unlock(); | |
687 | } | |
688 | ||
90a5e169 RP |
689 | static void hwmp_rann_frame_process(struct ieee80211_sub_if_data *sdata, |
690 | struct ieee80211_mgmt *mgmt, | |
691 | struct ieee80211_rann_ie *rann) | |
692 | { | |
693 | struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh; | |
694 | struct mesh_path *mpath; | |
90a5e169 RP |
695 | u8 ttl, flags, hopcount; |
696 | u8 *orig_addr; | |
d19b3bf6 | 697 | u32 orig_sn, metric; |
25d49e4d | 698 | u32 interval = cpu_to_le32(IEEE80211_MESH_RANN_INTERVAL); |
90a5e169 | 699 | |
90a5e169 RP |
700 | ttl = rann->rann_ttl; |
701 | if (ttl <= 1) { | |
702 | ifmsh->mshstats.dropped_frames_ttl++; | |
703 | return; | |
704 | } | |
705 | ttl--; | |
706 | flags = rann->rann_flags; | |
707 | orig_addr = rann->rann_addr; | |
d19b3bf6 | 708 | orig_sn = rann->rann_seq; |
90a5e169 | 709 | hopcount = rann->rann_hopcount; |
a6a58b4f | 710 | hopcount++; |
90a5e169 RP |
711 | metric = rann->rann_metric; |
712 | mhwmp_dbg("received RANN from %pM\n", orig_addr); | |
713 | ||
714 | rcu_read_lock(); | |
715 | mpath = mesh_path_lookup(orig_addr, sdata); | |
716 | if (!mpath) { | |
717 | mesh_path_add(orig_addr, sdata); | |
718 | mpath = mesh_path_lookup(orig_addr, sdata); | |
719 | if (!mpath) { | |
720 | rcu_read_unlock(); | |
721 | sdata->u.mesh.mshstats.dropped_frames_no_route++; | |
722 | return; | |
723 | } | |
724 | mesh_queue_preq(mpath, | |
725 | PREQ_Q_F_START | PREQ_Q_F_REFRESH); | |
726 | } | |
d19b3bf6 | 727 | if (mpath->sn < orig_sn) { |
90a5e169 | 728 | mesh_path_sel_frame_tx(MPATH_RANN, flags, orig_addr, |
d19b3bf6 | 729 | cpu_to_le32(orig_sn), |
15ff6365 | 730 | 0, NULL, 0, broadcast_addr, |
25d49e4d | 731 | hopcount, ttl, interval, |
a6a58b4f | 732 | cpu_to_le32(metric + mpath->metric), |
90a5e169 | 733 | 0, sdata); |
d19b3bf6 | 734 | mpath->sn = orig_sn; |
90a5e169 RP |
735 | } |
736 | rcu_read_unlock(); | |
737 | } | |
050ac52c LCC |
738 | |
739 | ||
f698d856 | 740 | void mesh_rx_path_sel_frame(struct ieee80211_sub_if_data *sdata, |
050ac52c LCC |
741 | struct ieee80211_mgmt *mgmt, |
742 | size_t len) | |
743 | { | |
744 | struct ieee802_11_elems elems; | |
745 | size_t baselen; | |
746 | u32 last_hop_metric; | |
747 | ||
9c80d3dc JB |
748 | /* need action_code */ |
749 | if (len < IEEE80211_MIN_ACTION_SIZE + 1) | |
750 | return; | |
751 | ||
050ac52c LCC |
752 | baselen = (u8 *) mgmt->u.action.u.mesh_action.variable - (u8 *) mgmt; |
753 | ieee802_11_parse_elems(mgmt->u.action.u.mesh_action.variable, | |
754 | len - baselen, &elems); | |
755 | ||
dbb81c42 RP |
756 | if (elems.preq) { |
757 | if (elems.preq_len != 37) | |
050ac52c LCC |
758 | /* Right now we support just 1 destination and no AE */ |
759 | return; | |
dbb81c42 RP |
760 | last_hop_metric = hwmp_route_info_get(sdata, mgmt, elems.preq, |
761 | MPATH_PREQ); | |
762 | if (last_hop_metric) | |
763 | hwmp_preq_frame_process(sdata, mgmt, elems.preq, | |
764 | last_hop_metric); | |
765 | } | |
766 | if (elems.prep) { | |
767 | if (elems.prep_len != 31) | |
050ac52c LCC |
768 | /* Right now we support no AE */ |
769 | return; | |
dbb81c42 RP |
770 | last_hop_metric = hwmp_route_info_get(sdata, mgmt, elems.prep, |
771 | MPATH_PREP); | |
772 | if (last_hop_metric) | |
773 | hwmp_prep_frame_process(sdata, mgmt, elems.prep, | |
774 | last_hop_metric); | |
775 | } | |
776 | if (elems.perr) { | |
d611f062 | 777 | if (elems.perr_len != 15) |
050ac52c LCC |
778 | /* Right now we support only one destination per PERR */ |
779 | return; | |
f698d856 | 780 | hwmp_perr_frame_process(sdata, mgmt, elems.perr); |
050ac52c | 781 | } |
90a5e169 RP |
782 | if (elems.rann) |
783 | hwmp_rann_frame_process(sdata, mgmt, elems.rann); | |
050ac52c LCC |
784 | } |
785 | ||
786 | /** | |
787 | * mesh_queue_preq - queue a PREQ to a given destination | |
788 | * | |
789 | * @mpath: mesh path to discover | |
790 | * @flags: special attributes of the PREQ to be sent | |
791 | * | |
792 | * Locking: the function must be called from within a rcu read lock block. | |
793 | * | |
794 | */ | |
795 | static void mesh_queue_preq(struct mesh_path *mpath, u8 flags) | |
796 | { | |
f698d856 | 797 | struct ieee80211_sub_if_data *sdata = mpath->sdata; |
472dbc45 | 798 | struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh; |
050ac52c LCC |
799 | struct mesh_preq_queue *preq_node; |
800 | ||
59615b5f | 801 | preq_node = kmalloc(sizeof(struct mesh_preq_queue), GFP_ATOMIC); |
050ac52c | 802 | if (!preq_node) { |
7646887a | 803 | mhwmp_dbg("could not allocate PREQ node"); |
050ac52c LCC |
804 | return; |
805 | } | |
806 | ||
987dafad | 807 | spin_lock_bh(&ifmsh->mesh_preq_queue_lock); |
472dbc45 | 808 | if (ifmsh->preq_queue_len == MAX_PREQ_QUEUE_LEN) { |
987dafad | 809 | spin_unlock_bh(&ifmsh->mesh_preq_queue_lock); |
050ac52c LCC |
810 | kfree(preq_node); |
811 | if (printk_ratelimit()) | |
7646887a | 812 | mhwmp_dbg("PREQ node queue full"); |
050ac52c LCC |
813 | return; |
814 | } | |
815 | ||
816 | memcpy(preq_node->dst, mpath->dst, ETH_ALEN); | |
817 | preq_node->flags = flags; | |
818 | ||
472dbc45 JB |
819 | list_add_tail(&preq_node->list, &ifmsh->preq_queue.list); |
820 | ++ifmsh->preq_queue_len; | |
987dafad | 821 | spin_unlock_bh(&ifmsh->mesh_preq_queue_lock); |
050ac52c | 822 | |
472dbc45 | 823 | if (time_after(jiffies, ifmsh->last_preq + min_preq_int_jiff(sdata))) |
64592c8f | 824 | ieee80211_queue_work(&sdata->local->hw, &sdata->work); |
050ac52c | 825 | |
472dbc45 | 826 | else if (time_before(jiffies, ifmsh->last_preq)) { |
050ac52c LCC |
827 | /* avoid long wait if did not send preqs for a long time |
828 | * and jiffies wrapped around | |
829 | */ | |
472dbc45 | 830 | ifmsh->last_preq = jiffies - min_preq_int_jiff(sdata) - 1; |
64592c8f | 831 | ieee80211_queue_work(&sdata->local->hw, &sdata->work); |
050ac52c | 832 | } else |
472dbc45 | 833 | mod_timer(&ifmsh->mesh_path_timer, ifmsh->last_preq + |
050ac52c LCC |
834 | min_preq_int_jiff(sdata)); |
835 | } | |
836 | ||
837 | /** | |
838 | * mesh_path_start_discovery - launch a path discovery from the PREQ queue | |
839 | * | |
f698d856 | 840 | * @sdata: local mesh subif |
050ac52c | 841 | */ |
f698d856 | 842 | void mesh_path_start_discovery(struct ieee80211_sub_if_data *sdata) |
050ac52c | 843 | { |
472dbc45 | 844 | struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh; |
050ac52c LCC |
845 | struct mesh_preq_queue *preq_node; |
846 | struct mesh_path *mpath; | |
d19b3bf6 | 847 | u8 ttl, target_flags; |
050ac52c LCC |
848 | u32 lifetime; |
849 | ||
a43816df | 850 | spin_lock_bh(&ifmsh->mesh_preq_queue_lock); |
472dbc45 JB |
851 | if (!ifmsh->preq_queue_len || |
852 | time_before(jiffies, ifmsh->last_preq + | |
050ac52c | 853 | min_preq_int_jiff(sdata))) { |
a43816df | 854 | spin_unlock_bh(&ifmsh->mesh_preq_queue_lock); |
050ac52c LCC |
855 | return; |
856 | } | |
857 | ||
472dbc45 | 858 | preq_node = list_first_entry(&ifmsh->preq_queue.list, |
050ac52c LCC |
859 | struct mesh_preq_queue, list); |
860 | list_del(&preq_node->list); | |
472dbc45 | 861 | --ifmsh->preq_queue_len; |
a43816df | 862 | spin_unlock_bh(&ifmsh->mesh_preq_queue_lock); |
050ac52c LCC |
863 | |
864 | rcu_read_lock(); | |
f698d856 | 865 | mpath = mesh_path_lookup(preq_node->dst, sdata); |
050ac52c LCC |
866 | if (!mpath) |
867 | goto enddiscovery; | |
868 | ||
869 | spin_lock_bh(&mpath->state_lock); | |
870 | if (preq_node->flags & PREQ_Q_F_START) { | |
871 | if (mpath->flags & MESH_PATH_RESOLVING) { | |
872 | spin_unlock_bh(&mpath->state_lock); | |
873 | goto enddiscovery; | |
874 | } else { | |
875 | mpath->flags &= ~MESH_PATH_RESOLVED; | |
876 | mpath->flags |= MESH_PATH_RESOLVING; | |
877 | mpath->discovery_retries = 0; | |
878 | mpath->discovery_timeout = disc_timeout_jiff(sdata); | |
879 | } | |
880 | } else if (!(mpath->flags & MESH_PATH_RESOLVING) || | |
881 | mpath->flags & MESH_PATH_RESOLVED) { | |
882 | mpath->flags &= ~MESH_PATH_RESOLVING; | |
883 | spin_unlock_bh(&mpath->state_lock); | |
884 | goto enddiscovery; | |
885 | } | |
886 | ||
472dbc45 | 887 | ifmsh->last_preq = jiffies; |
050ac52c | 888 | |
d19b3bf6 | 889 | if (time_after(jiffies, ifmsh->last_sn_update + |
050ac52c | 890 | net_traversal_jiffies(sdata)) || |
d19b3bf6 RP |
891 | time_before(jiffies, ifmsh->last_sn_update)) { |
892 | ++ifmsh->sn; | |
893 | sdata->u.mesh.last_sn_update = jiffies; | |
050ac52c LCC |
894 | } |
895 | lifetime = default_lifetime(sdata); | |
45904f21 | 896 | ttl = sdata->u.mesh.mshcfg.element_ttl; |
050ac52c | 897 | if (ttl == 0) { |
472dbc45 | 898 | sdata->u.mesh.mshstats.dropped_frames_ttl++; |
050ac52c LCC |
899 | spin_unlock_bh(&mpath->state_lock); |
900 | goto enddiscovery; | |
901 | } | |
902 | ||
903 | if (preq_node->flags & PREQ_Q_F_REFRESH) | |
d19b3bf6 | 904 | target_flags = MP_F_DO; |
050ac52c | 905 | else |
d19b3bf6 | 906 | target_flags = MP_F_RF; |
050ac52c LCC |
907 | |
908 | spin_unlock_bh(&mpath->state_lock); | |
47846c9b | 909 | mesh_path_sel_frame_tx(MPATH_PREQ, 0, sdata->vif.addr, |
d19b3bf6 | 910 | cpu_to_le32(ifmsh->sn), target_flags, mpath->dst, |
15ff6365 | 911 | cpu_to_le32(mpath->sn), broadcast_addr, 0, |
aa2b5928 | 912 | ttl, cpu_to_le32(lifetime), 0, |
472dbc45 | 913 | cpu_to_le32(ifmsh->preq_id++), sdata); |
050ac52c LCC |
914 | mod_timer(&mpath->timer, jiffies + mpath->discovery_timeout); |
915 | ||
916 | enddiscovery: | |
917 | rcu_read_unlock(); | |
918 | kfree(preq_node); | |
919 | } | |
920 | ||
921 | /** | |
2182b830 | 922 | * mesh_nexthop_lookup - put the appropriate next hop on a mesh frame |
050ac52c | 923 | * |
e32f85f7 | 924 | * @skb: 802.11 frame to be sent |
f698d856 | 925 | * @sdata: network subif the frame will be sent through |
050ac52c LCC |
926 | * |
927 | * Returns: 0 if the next hop was found. Nonzero otherwise. If no next hop is | |
928 | * found, the function will start a path discovery and queue the frame so it is | |
929 | * sent when the path is resolved. This means the caller must not free the skb | |
930 | * in this case. | |
931 | */ | |
f698d856 JBG |
932 | int mesh_nexthop_lookup(struct sk_buff *skb, |
933 | struct ieee80211_sub_if_data *sdata) | |
050ac52c | 934 | { |
050ac52c LCC |
935 | struct sk_buff *skb_to_free = NULL; |
936 | struct mesh_path *mpath; | |
40b275b6 | 937 | struct sta_info *next_hop; |
e32f85f7 | 938 | struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data; |
d19b3bf6 | 939 | u8 *target_addr = hdr->addr3; |
050ac52c LCC |
940 | int err = 0; |
941 | ||
942 | rcu_read_lock(); | |
d19b3bf6 | 943 | mpath = mesh_path_lookup(target_addr, sdata); |
050ac52c LCC |
944 | |
945 | if (!mpath) { | |
d19b3bf6 RP |
946 | mesh_path_add(target_addr, sdata); |
947 | mpath = mesh_path_lookup(target_addr, sdata); | |
050ac52c | 948 | if (!mpath) { |
472dbc45 | 949 | sdata->u.mesh.mshstats.dropped_frames_no_route++; |
050ac52c LCC |
950 | err = -ENOSPC; |
951 | goto endlookup; | |
952 | } | |
953 | } | |
954 | ||
955 | if (mpath->flags & MESH_PATH_ACTIVE) { | |
f64f9e71 | 956 | if (time_after(jiffies, |
7b324d28 | 957 | mpath->exp_time - |
f64f9e71 | 958 | msecs_to_jiffies(sdata->u.mesh.mshcfg.path_refresh_time)) && |
47846c9b | 959 | !memcmp(sdata->vif.addr, hdr->addr4, ETH_ALEN) && |
f64f9e71 JP |
960 | !(mpath->flags & MESH_PATH_RESOLVING) && |
961 | !(mpath->flags & MESH_PATH_FIXED)) { | |
050ac52c LCC |
962 | mesh_queue_preq(mpath, |
963 | PREQ_Q_F_START | PREQ_Q_F_REFRESH); | |
964 | } | |
40b275b6 JB |
965 | next_hop = rcu_dereference(mpath->next_hop); |
966 | if (next_hop) | |
967 | memcpy(hdr->addr1, next_hop->sta.addr, ETH_ALEN); | |
968 | else | |
969 | err = -ENOENT; | |
050ac52c | 970 | } else { |
249b405c | 971 | struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); |
050ac52c LCC |
972 | if (!(mpath->flags & MESH_PATH_RESOLVING)) { |
973 | /* Start discovery only if it is not running yet */ | |
974 | mesh_queue_preq(mpath, PREQ_Q_F_START); | |
975 | } | |
976 | ||
977 | if (skb_queue_len(&mpath->frame_queue) >= | |
fe583434 JC |
978 | MESH_FRAME_QUEUE_LEN) |
979 | skb_to_free = skb_dequeue(&mpath->frame_queue); | |
050ac52c | 980 | |
249b405c | 981 | info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING; |
050ac52c LCC |
982 | skb_queue_tail(&mpath->frame_queue, skb); |
983 | if (skb_to_free) | |
f698d856 | 984 | mesh_path_discard_frame(skb_to_free, sdata); |
050ac52c LCC |
985 | err = -ENOENT; |
986 | } | |
987 | ||
988 | endlookup: | |
989 | rcu_read_unlock(); | |
990 | return err; | |
991 | } | |
992 | ||
993 | void mesh_path_timer(unsigned long data) | |
994 | { | |
dea4096b JB |
995 | struct mesh_path *mpath = (void *) data; |
996 | struct ieee80211_sub_if_data *sdata = mpath->sdata; | |
5bb644a0 | 997 | |
dea4096b | 998 | if (sdata->local->quiescing) |
5bb644a0 | 999 | return; |
5bb644a0 JB |
1000 | |
1001 | spin_lock_bh(&mpath->state_lock); | |
cfa22c71 | 1002 | if (mpath->flags & MESH_PATH_RESOLVED || |
050ac52c LCC |
1003 | (!(mpath->flags & MESH_PATH_RESOLVING))) |
1004 | mpath->flags &= ~(MESH_PATH_RESOLVING | MESH_PATH_RESOLVED); | |
1005 | else if (mpath->discovery_retries < max_preq_retries(sdata)) { | |
1006 | ++mpath->discovery_retries; | |
1007 | mpath->discovery_timeout *= 2; | |
1008 | mesh_queue_preq(mpath, 0); | |
1009 | } else { | |
1010 | mpath->flags = 0; | |
1011 | mpath->exp_time = jiffies; | |
1012 | mesh_path_flush_pending(mpath); | |
1013 | } | |
1014 | ||
1015 | spin_unlock_bh(&mpath->state_lock); | |
050ac52c | 1016 | } |
e304bfd3 RP |
1017 | |
1018 | void | |
1019 | mesh_path_tx_root_frame(struct ieee80211_sub_if_data *sdata) | |
1020 | { | |
1021 | struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh; | |
25d49e4d | 1022 | u32 interval = cpu_to_le32(IEEE80211_MESH_RANN_INTERVAL); |
e304bfd3 | 1023 | |
47846c9b | 1024 | mesh_path_sel_frame_tx(MPATH_RANN, 0, sdata->vif.addr, |
e304bfd3 | 1025 | cpu_to_le32(++ifmsh->sn), |
15ff6365 | 1026 | 0, NULL, 0, broadcast_addr, |
45904f21 | 1027 | 0, sdata->u.mesh.mshcfg.element_ttl, |
25d49e4d | 1028 | interval, 0, 0, sdata); |
e304bfd3 | 1029 | } |