Commit | Line | Data |
---|---|---|
050ac52c LCC |
1 | /* |
2 | * Copyright (c) 2008 open80211s Ltd. | |
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 | ||
a00de5d0 | 10 | #include <asm/unaligned.h> |
050ac52c LCC |
11 | #include "mesh.h" |
12 | ||
13 | #define IEEE80211_FC(type, stype) cpu_to_le16(type | stype) | |
14 | ||
15 | #define TEST_FRAME_LEN 8192 | |
16 | #define MAX_METRIC 0xffffffff | |
17 | #define ARITH_SHIFT 8 | |
18 | ||
19 | /* Number of frames buffered per destination for unresolved destinations */ | |
20 | #define MESH_FRAME_QUEUE_LEN 10 | |
21 | #define MAX_PREQ_QUEUE_LEN 64 | |
22 | ||
23 | /* Destination only */ | |
24 | #define MP_F_DO 0x1 | |
25 | /* Reply and forward */ | |
26 | #define MP_F_RF 0x2 | |
27 | ||
a00de5d0 LCC |
28 | static inline u32 u32_field_get(u8 *preq_elem, int offset, bool ae) |
29 | { | |
30 | if (ae) | |
31 | offset += 6; | |
32 | return le32_to_cpu(get_unaligned((__le32 *) (preq_elem + offset))); | |
33 | } | |
34 | ||
050ac52c | 35 | /* HWMP IE processing macros */ |
a00de5d0 LCC |
36 | #define AE_F (1<<6) |
37 | #define AE_F_SET(x) (*x & AE_F) | |
38 | #define PREQ_IE_FLAGS(x) (*(x)) | |
39 | #define PREQ_IE_HOPCOUNT(x) (*(x + 1)) | |
40 | #define PREQ_IE_TTL(x) (*(x + 2)) | |
41 | #define PREQ_IE_PREQ_ID(x) u32_field_get(x, 3, 0) | |
42 | #define PREQ_IE_ORIG_ADDR(x) (x + 7) | |
43 | #define PREQ_IE_ORIG_DSN(x) u32_field_get(x, 13, 0); | |
44 | #define PREQ_IE_LIFETIME(x) u32_field_get(x, 17, AE_F_SET(x)); | |
45 | #define PREQ_IE_METRIC(x) u32_field_get(x, 21, AE_F_SET(x)); | |
46 | #define PREQ_IE_DST_F(x) (*(AE_F_SET(x) ? x + 32 : x + 26)) | |
47 | #define PREQ_IE_DST_ADDR(x) (AE_F_SET(x) ? x + 33 : x + 27) | |
48 | #define PREQ_IE_DST_DSN(x) u32_field_get(x, 33, AE_F_SET(x)); | |
49 | ||
50 | ||
51 | #define PREP_IE_FLAGS(x) PREQ_IE_FLAGS(x) | |
52 | #define PREP_IE_HOPCOUNT(x) PREQ_IE_HOPCOUNT(x) | |
53 | #define PREP_IE_TTL(x) PREQ_IE_TTL(x) | |
54 | #define PREP_IE_ORIG_ADDR(x) (x + 3) | |
55 | #define PREP_IE_ORIG_DSN(x) u32_field_get(x, 9, 0); | |
56 | #define PREP_IE_LIFETIME(x) u32_field_get(x, 13, AE_F_SET(x)); | |
57 | #define PREP_IE_METRIC(x) u32_field_get(x, 17, AE_F_SET(x)); | |
58 | #define PREP_IE_DST_ADDR(x) (AE_F_SET(x) ? x + 27 : x + 21) | |
59 | #define PREP_IE_DST_DSN(x) u32_field_get(x, 27, AE_F_SET(x)); | |
60 | ||
61 | #define PERR_IE_DST_ADDR(x) (x + 2) | |
62 | #define PERR_IE_DST_DSN(x) u32_field_get(x, 8, 0); | |
050ac52c LCC |
63 | |
64 | #define TU_TO_EXP_TIME(x) (jiffies + msecs_to_jiffies(x * 1024 / 1000)) | |
65 | #define MSEC_TO_TU(x) (x*1000/1024) | |
66 | #define DSN_GT(x, y) ((long) (y) - (long) (x) < 0) | |
67 | #define DSN_LT(x, y) ((long) (x) - (long) (y) < 0) | |
68 | ||
69 | #define net_traversal_jiffies(s) \ | |
70 | msecs_to_jiffies(s->u.sta.mshcfg.dot11MeshHWMPnetDiameterTraversalTime) | |
71 | #define default_lifetime(s) \ | |
72 | MSEC_TO_TU(s->u.sta.mshcfg.dot11MeshHWMPactivePathTimeout) | |
73 | #define min_preq_int_jiff(s) \ | |
74 | (msecs_to_jiffies(s->u.sta.mshcfg.dot11MeshHWMPpreqMinInterval)) | |
75 | #define max_preq_retries(s) (s->u.sta.mshcfg.dot11MeshHWMPmaxPREQretries) | |
76 | #define disc_timeout_jiff(s) \ | |
77 | msecs_to_jiffies(sdata->u.sta.mshcfg.min_discovery_timeout) | |
78 | ||
79 | enum mpath_frame_type { | |
80 | MPATH_PREQ = 0, | |
81 | MPATH_PREP, | |
82 | MPATH_PERR | |
83 | }; | |
84 | ||
85 | static int mesh_path_sel_frame_tx(enum mpath_frame_type action, u8 flags, | |
86 | u8 *orig_addr, __le32 orig_dsn, u8 dst_flags, u8 *dst, | |
87 | __le32 dst_dsn, u8 *da, u8 hop_count, u8 ttl, __le32 lifetime, | |
88 | __le32 metric, __le32 preq_id, struct net_device *dev) | |
89 | { | |
90 | struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr); | |
91 | struct sk_buff *skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400); | |
92 | struct ieee80211_mgmt *mgmt; | |
93 | u8 *pos; | |
94 | int ie_len; | |
95 | ||
96 | if (!skb) | |
97 | return -1; | |
98 | skb_reserve(skb, local->hw.extra_tx_headroom); | |
99 | /* 25 is the size of the common mgmt part (24) plus the size of the | |
100 | * common action part (1) | |
101 | */ | |
102 | mgmt = (struct ieee80211_mgmt *) | |
103 | skb_put(skb, 25 + sizeof(mgmt->u.action.u.mesh_action)); | |
104 | memset(mgmt, 0, 25 + sizeof(mgmt->u.action.u.mesh_action)); | |
105 | mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT, | |
106 | IEEE80211_STYPE_ACTION); | |
107 | ||
108 | memcpy(mgmt->da, da, ETH_ALEN); | |
109 | memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN); | |
110 | /* BSSID is left zeroed, wildcard value */ | |
111 | mgmt->u.action.category = MESH_PATH_SEL_CATEGORY; | |
112 | mgmt->u.action.u.mesh_action.action_code = action; | |
113 | ||
114 | switch (action) { | |
115 | case MPATH_PREQ: | |
116 | ie_len = 37; | |
117 | pos = skb_put(skb, 2 + ie_len); | |
118 | *pos++ = WLAN_EID_PREQ; | |
119 | break; | |
120 | case MPATH_PREP: | |
121 | ie_len = 31; | |
122 | pos = skb_put(skb, 2 + ie_len); | |
123 | *pos++ = WLAN_EID_PREP; | |
124 | break; | |
125 | default: | |
126 | kfree(skb); | |
127 | return -ENOTSUPP; | |
128 | break; | |
129 | } | |
130 | *pos++ = ie_len; | |
131 | *pos++ = flags; | |
132 | *pos++ = hop_count; | |
133 | *pos++ = ttl; | |
134 | if (action == MPATH_PREQ) { | |
135 | memcpy(pos, &preq_id, 4); | |
136 | pos += 4; | |
137 | } | |
138 | memcpy(pos, orig_addr, ETH_ALEN); | |
139 | pos += ETH_ALEN; | |
140 | memcpy(pos, &orig_dsn, 4); | |
141 | pos += 4; | |
142 | memcpy(pos, &lifetime, 4); | |
143 | pos += 4; | |
144 | memcpy(pos, &metric, 4); | |
145 | pos += 4; | |
146 | if (action == MPATH_PREQ) { | |
147 | /* destination count */ | |
148 | *pos++ = 1; | |
149 | *pos++ = dst_flags; | |
150 | } | |
151 | memcpy(pos, dst, ETH_ALEN); | |
152 | pos += ETH_ALEN; | |
153 | memcpy(pos, &dst_dsn, 4); | |
154 | ||
155 | ieee80211_sta_tx(dev, skb, 0); | |
156 | return 0; | |
157 | } | |
158 | ||
159 | /** | |
160 | * mesh_send_path error - Sends a PERR mesh management frame | |
161 | * | |
162 | * @dst: broken destination | |
163 | * @dst_dsn: dsn of the broken destination | |
164 | * @ra: node this frame is addressed to | |
165 | */ | |
166 | int mesh_path_error_tx(u8 *dst, __le32 dst_dsn, u8 *ra, | |
167 | struct net_device *dev) | |
168 | { | |
169 | struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr); | |
170 | struct sk_buff *skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400); | |
171 | struct ieee80211_mgmt *mgmt; | |
172 | u8 *pos; | |
173 | int ie_len; | |
174 | ||
175 | if (!skb) | |
176 | return -1; | |
177 | skb_reserve(skb, local->hw.extra_tx_headroom); | |
178 | /* 25 is the size of the common mgmt part (24) plus the size of the | |
179 | * common action part (1) | |
180 | */ | |
181 | mgmt = (struct ieee80211_mgmt *) | |
182 | skb_put(skb, 25 + sizeof(mgmt->u.action.u.mesh_action)); | |
183 | memset(mgmt, 0, 25 + sizeof(mgmt->u.action.u.mesh_action)); | |
184 | mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT, | |
185 | IEEE80211_STYPE_ACTION); | |
186 | ||
187 | memcpy(mgmt->da, ra, ETH_ALEN); | |
188 | memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN); | |
189 | /* BSSID is left zeroed, wildcard value */ | |
190 | mgmt->u.action.category = MESH_PATH_SEL_CATEGORY; | |
191 | mgmt->u.action.u.mesh_action.action_code = MPATH_PERR; | |
192 | ie_len = 12; | |
193 | pos = skb_put(skb, 2 + ie_len); | |
194 | *pos++ = WLAN_EID_PERR; | |
195 | *pos++ = ie_len; | |
196 | /* mode flags, reserved */ | |
197 | *pos++ = 0; | |
198 | /* number of destinations */ | |
199 | *pos++ = 1; | |
200 | memcpy(pos, dst, ETH_ALEN); | |
201 | pos += ETH_ALEN; | |
202 | memcpy(pos, &dst_dsn, 4); | |
203 | ||
204 | ieee80211_sta_tx(dev, skb, 0); | |
205 | return 0; | |
206 | } | |
207 | ||
208 | static u32 airtime_link_metric_get(struct ieee80211_local *local, | |
209 | struct sta_info *sta) | |
210 | { | |
211 | struct ieee80211_supported_band *sband; | |
212 | /* This should be adjusted for each device */ | |
213 | int device_constant = 1 << ARITH_SHIFT; | |
214 | int test_frame_len = TEST_FRAME_LEN << ARITH_SHIFT; | |
215 | int s_unit = 1 << ARITH_SHIFT; | |
216 | int rate, err; | |
217 | u32 tx_time, estimated_retx; | |
218 | u64 result; | |
219 | ||
220 | sband = local->hw.wiphy->bands[local->hw.conf.channel->band]; | |
221 | ||
222 | if (sta->fail_avg >= 100) | |
223 | return MAX_METRIC; | |
224 | err = (sta->fail_avg << ARITH_SHIFT) / 100; | |
225 | ||
226 | /* bitrate is in units of 100 Kbps, while we need rate in units of | |
227 | * 1Mbps. This will be corrected on tx_time computation. | |
228 | */ | |
229 | rate = sband->bitrates[sta->txrate_idx].bitrate; | |
230 | tx_time = (device_constant + 10 * test_frame_len / rate); | |
231 | estimated_retx = ((1 << (2 * ARITH_SHIFT)) / (s_unit - err)); | |
232 | result = (tx_time * estimated_retx) >> (2 * ARITH_SHIFT) ; | |
233 | return (u32)result; | |
234 | } | |
235 | ||
236 | /** | |
237 | * hwmp_route_info_get - Update routing info to originator and transmitter | |
238 | * | |
239 | * @dev: local mesh interface | |
240 | * @mgmt: mesh management frame | |
241 | * @hwmp_ie: hwmp information element (PREP or PREQ) | |
242 | * | |
243 | * This function updates the path routing information to the originator and the | |
244 | * transmitter of a HWMP PREQ or PREP fram. | |
245 | * | |
246 | * Returns: metric to frame originator or 0 if the frame should not be further | |
247 | * processed | |
248 | * | |
249 | * Notes: this function is the only place (besides user-provided info) where | |
250 | * path routing information is updated. | |
251 | */ | |
252 | static u32 hwmp_route_info_get(struct net_device *dev, | |
253 | struct ieee80211_mgmt *mgmt, | |
254 | u8 *hwmp_ie) | |
255 | { | |
256 | struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr); | |
257 | struct mesh_path *mpath; | |
258 | struct sta_info *sta; | |
259 | bool fresh_info; | |
260 | u8 *orig_addr, *ta; | |
261 | u32 orig_dsn, orig_metric; | |
262 | unsigned long orig_lifetime, exp_time; | |
263 | u32 last_hop_metric, new_metric; | |
264 | bool process = true; | |
265 | u8 action = mgmt->u.action.u.mesh_action.action_code; | |
266 | ||
267 | rcu_read_lock(); | |
268 | sta = sta_info_get(local, mgmt->sa); | |
dc0b0f7d JB |
269 | if (!sta) { |
270 | rcu_read_unlock(); | |
050ac52c | 271 | return 0; |
dc0b0f7d | 272 | } |
050ac52c LCC |
273 | |
274 | last_hop_metric = airtime_link_metric_get(local, sta); | |
275 | /* Update and check originator routing info */ | |
276 | fresh_info = true; | |
277 | ||
278 | switch (action) { | |
279 | case MPATH_PREQ: | |
280 | orig_addr = PREQ_IE_ORIG_ADDR(hwmp_ie); | |
281 | orig_dsn = PREQ_IE_ORIG_DSN(hwmp_ie); | |
282 | orig_lifetime = PREQ_IE_LIFETIME(hwmp_ie); | |
283 | orig_metric = PREQ_IE_METRIC(hwmp_ie); | |
284 | break; | |
285 | case MPATH_PREP: | |
286 | /* Originator here refers to the MP that was the destination in | |
287 | * the Path Request. The draft refers to that MP as the | |
288 | * destination address, even though usually it is the origin of | |
289 | * the PREP frame. We divert from the nomenclature in the draft | |
290 | * so that we can easily use a single function to gather path | |
291 | * information from both PREQ and PREP frames. | |
292 | */ | |
293 | orig_addr = PREP_IE_ORIG_ADDR(hwmp_ie); | |
294 | orig_dsn = PREP_IE_ORIG_DSN(hwmp_ie); | |
295 | orig_lifetime = PREP_IE_LIFETIME(hwmp_ie); | |
296 | orig_metric = PREP_IE_METRIC(hwmp_ie); | |
297 | break; | |
298 | default: | |
dc0b0f7d | 299 | rcu_read_unlock(); |
050ac52c LCC |
300 | return 0; |
301 | } | |
302 | new_metric = orig_metric + last_hop_metric; | |
303 | if (new_metric < orig_metric) | |
304 | new_metric = MAX_METRIC; | |
305 | exp_time = TU_TO_EXP_TIME(orig_lifetime); | |
306 | ||
307 | if (memcmp(orig_addr, dev->dev_addr, ETH_ALEN) == 0) { | |
308 | /* This MP is the originator, we are not interested in this | |
309 | * frame, except for updating transmitter's path info. | |
310 | */ | |
311 | process = false; | |
312 | fresh_info = false; | |
313 | } else { | |
314 | mpath = mesh_path_lookup(orig_addr, dev); | |
315 | if (mpath) { | |
316 | spin_lock_bh(&mpath->state_lock); | |
317 | if (mpath->flags & MESH_PATH_FIXED) | |
318 | fresh_info = false; | |
319 | else if ((mpath->flags & MESH_PATH_ACTIVE) && | |
320 | (mpath->flags & MESH_PATH_DSN_VALID)) { | |
321 | if (DSN_GT(mpath->dsn, orig_dsn) || | |
322 | (mpath->dsn == orig_dsn && | |
323 | action == MPATH_PREQ && | |
324 | new_metric > mpath->metric)) { | |
325 | process = false; | |
326 | fresh_info = false; | |
327 | } | |
328 | } | |
329 | } else { | |
330 | mesh_path_add(orig_addr, dev); | |
331 | mpath = mesh_path_lookup(orig_addr, dev); | |
332 | if (!mpath) { | |
333 | rcu_read_unlock(); | |
050ac52c LCC |
334 | return 0; |
335 | } | |
336 | spin_lock_bh(&mpath->state_lock); | |
337 | } | |
338 | ||
339 | if (fresh_info) { | |
340 | mesh_path_assign_nexthop(mpath, sta); | |
341 | mpath->flags |= MESH_PATH_DSN_VALID; | |
342 | mpath->metric = new_metric; | |
343 | mpath->dsn = orig_dsn; | |
344 | mpath->exp_time = time_after(mpath->exp_time, exp_time) | |
345 | ? mpath->exp_time : exp_time; | |
346 | mesh_path_activate(mpath); | |
347 | spin_unlock_bh(&mpath->state_lock); | |
348 | mesh_path_tx_pending(mpath); | |
349 | /* draft says preq_id should be saved to, but there does | |
350 | * not seem to be any use for it, skipping by now | |
351 | */ | |
352 | } else | |
353 | spin_unlock_bh(&mpath->state_lock); | |
354 | } | |
355 | ||
356 | /* Update and check transmitter routing info */ | |
357 | ta = mgmt->sa; | |
358 | if (memcmp(orig_addr, ta, ETH_ALEN) == 0) | |
359 | fresh_info = false; | |
360 | else { | |
361 | fresh_info = true; | |
362 | ||
363 | mpath = mesh_path_lookup(ta, dev); | |
364 | if (mpath) { | |
365 | spin_lock_bh(&mpath->state_lock); | |
366 | if ((mpath->flags & MESH_PATH_FIXED) || | |
367 | ((mpath->flags & MESH_PATH_ACTIVE) && | |
368 | (last_hop_metric > mpath->metric))) | |
369 | fresh_info = false; | |
370 | } else { | |
371 | mesh_path_add(ta, dev); | |
372 | mpath = mesh_path_lookup(ta, dev); | |
373 | if (!mpath) { | |
374 | rcu_read_unlock(); | |
050ac52c LCC |
375 | return 0; |
376 | } | |
377 | spin_lock_bh(&mpath->state_lock); | |
378 | } | |
379 | ||
380 | if (fresh_info) { | |
381 | mesh_path_assign_nexthop(mpath, sta); | |
382 | mpath->flags &= ~MESH_PATH_DSN_VALID; | |
383 | mpath->metric = last_hop_metric; | |
384 | mpath->exp_time = time_after(mpath->exp_time, exp_time) | |
385 | ? mpath->exp_time : exp_time; | |
386 | mesh_path_activate(mpath); | |
387 | spin_unlock_bh(&mpath->state_lock); | |
388 | mesh_path_tx_pending(mpath); | |
389 | } else | |
390 | spin_unlock_bh(&mpath->state_lock); | |
391 | } | |
392 | ||
050ac52c LCC |
393 | rcu_read_unlock(); |
394 | ||
395 | return process ? new_metric : 0; | |
396 | } | |
397 | ||
398 | static void hwmp_preq_frame_process(struct net_device *dev, | |
399 | struct ieee80211_mgmt *mgmt, | |
400 | u8 *preq_elem, u32 metric) { | |
401 | struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev); | |
402 | struct ieee80211_if_sta *ifsta = &sdata->u.sta; | |
403 | struct mesh_path *mpath; | |
404 | u8 *dst_addr, *orig_addr; | |
405 | u8 dst_flags, ttl; | |
406 | u32 orig_dsn, dst_dsn, lifetime; | |
407 | bool reply = false; | |
408 | bool forward = true; | |
409 | ||
410 | /* Update destination DSN, if present */ | |
411 | dst_addr = PREQ_IE_DST_ADDR(preq_elem); | |
412 | orig_addr = PREQ_IE_ORIG_ADDR(preq_elem); | |
413 | dst_dsn = PREQ_IE_DST_DSN(preq_elem); | |
414 | orig_dsn = PREQ_IE_ORIG_DSN(preq_elem); | |
415 | dst_flags = PREQ_IE_DST_F(preq_elem); | |
416 | ||
417 | if (memcmp(dst_addr, dev->dev_addr, ETH_ALEN) == 0) { | |
418 | forward = false; | |
419 | reply = true; | |
420 | metric = 0; | |
421 | if (time_after(jiffies, ifsta->last_dsn_update + | |
422 | net_traversal_jiffies(sdata)) || | |
423 | time_before(jiffies, ifsta->last_dsn_update)) { | |
424 | dst_dsn = ++ifsta->dsn; | |
425 | ifsta->last_dsn_update = jiffies; | |
426 | } | |
427 | } else { | |
428 | rcu_read_lock(); | |
429 | mpath = mesh_path_lookup(dst_addr, dev); | |
430 | if (mpath) { | |
431 | if ((!(mpath->flags & MESH_PATH_DSN_VALID)) || | |
432 | DSN_LT(mpath->dsn, dst_dsn)) { | |
433 | mpath->dsn = dst_dsn; | |
434 | mpath->flags &= MESH_PATH_DSN_VALID; | |
435 | } else if ((!(dst_flags & MP_F_DO)) && | |
436 | (mpath->flags & MESH_PATH_ACTIVE)) { | |
437 | reply = true; | |
438 | metric = mpath->metric; | |
439 | dst_dsn = mpath->dsn; | |
440 | if (dst_flags & MP_F_RF) | |
441 | dst_flags |= MP_F_DO; | |
442 | else | |
443 | forward = false; | |
444 | } | |
445 | } | |
446 | rcu_read_unlock(); | |
447 | } | |
448 | ||
449 | if (reply) { | |
450 | lifetime = PREQ_IE_LIFETIME(preq_elem); | |
451 | ttl = ifsta->mshcfg.dot11MeshTTL; | |
452 | if (ttl != 0) | |
453 | mesh_path_sel_frame_tx(MPATH_PREP, 0, dst_addr, | |
aa2b5928 LCC |
454 | cpu_to_le32(dst_dsn), 0, orig_addr, |
455 | cpu_to_le32(orig_dsn), mgmt->sa, 0, ttl, | |
456 | cpu_to_le32(lifetime), cpu_to_le32(metric), | |
050ac52c LCC |
457 | 0, dev); |
458 | else | |
459 | ifsta->mshstats.dropped_frames_ttl++; | |
460 | } | |
461 | ||
462 | if (forward) { | |
463 | u32 preq_id; | |
464 | u8 hopcount, flags; | |
465 | ||
466 | ttl = PREQ_IE_TTL(preq_elem); | |
467 | lifetime = PREQ_IE_LIFETIME(preq_elem); | |
468 | if (ttl <= 1) { | |
469 | ifsta->mshstats.dropped_frames_ttl++; | |
470 | return; | |
471 | } | |
472 | --ttl; | |
473 | flags = PREQ_IE_FLAGS(preq_elem); | |
474 | preq_id = PREQ_IE_PREQ_ID(preq_elem); | |
475 | hopcount = PREQ_IE_HOPCOUNT(preq_elem) + 1; | |
476 | mesh_path_sel_frame_tx(MPATH_PREQ, flags, orig_addr, | |
aa2b5928 LCC |
477 | cpu_to_le32(orig_dsn), dst_flags, dst_addr, |
478 | cpu_to_le32(dst_dsn), dev->broadcast, | |
479 | hopcount, ttl, cpu_to_le32(lifetime), | |
480 | cpu_to_le32(metric), cpu_to_le32(preq_id), | |
050ac52c LCC |
481 | dev); |
482 | ifsta->mshstats.fwded_frames++; | |
483 | } | |
484 | } | |
485 | ||
486 | ||
487 | static void hwmp_prep_frame_process(struct net_device *dev, | |
488 | struct ieee80211_mgmt *mgmt, | |
489 | u8 *prep_elem, u32 metric) | |
490 | { | |
491 | struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev); | |
492 | struct mesh_path *mpath; | |
493 | u8 *dst_addr, *orig_addr; | |
494 | u8 ttl, hopcount, flags; | |
495 | u8 next_hop[ETH_ALEN]; | |
496 | u32 dst_dsn, orig_dsn, lifetime; | |
497 | ||
498 | /* Note that we divert from the draft nomenclature and denominate | |
499 | * destination to what the draft refers to as origininator. So in this | |
500 | * function destnation refers to the final destination of the PREP, | |
501 | * which corresponds with the originator of the PREQ which this PREP | |
502 | * replies | |
503 | */ | |
504 | dst_addr = PREP_IE_DST_ADDR(prep_elem); | |
505 | if (memcmp(dst_addr, dev->dev_addr, ETH_ALEN) == 0) | |
506 | /* destination, no forwarding required */ | |
507 | return; | |
508 | ||
509 | ttl = PREP_IE_TTL(prep_elem); | |
510 | if (ttl <= 1) { | |
511 | sdata->u.sta.mshstats.dropped_frames_ttl++; | |
512 | return; | |
513 | } | |
514 | ||
515 | rcu_read_lock(); | |
516 | mpath = mesh_path_lookup(dst_addr, dev); | |
517 | if (mpath) | |
518 | spin_lock_bh(&mpath->state_lock); | |
519 | else | |
520 | goto fail; | |
521 | if (!(mpath->flags & MESH_PATH_ACTIVE)) { | |
522 | spin_unlock_bh(&mpath->state_lock); | |
523 | goto fail; | |
524 | } | |
525 | memcpy(next_hop, mpath->next_hop->addr, ETH_ALEN); | |
526 | spin_unlock_bh(&mpath->state_lock); | |
527 | --ttl; | |
528 | flags = PREP_IE_FLAGS(prep_elem); | |
529 | lifetime = PREP_IE_LIFETIME(prep_elem); | |
530 | hopcount = PREP_IE_HOPCOUNT(prep_elem) + 1; | |
531 | orig_addr = PREP_IE_ORIG_ADDR(prep_elem); | |
532 | dst_dsn = PREP_IE_DST_DSN(prep_elem); | |
533 | orig_dsn = PREP_IE_ORIG_DSN(prep_elem); | |
534 | ||
535 | mesh_path_sel_frame_tx(MPATH_PREP, flags, orig_addr, | |
aa2b5928 LCC |
536 | cpu_to_le32(orig_dsn), 0, dst_addr, |
537 | cpu_to_le32(dst_dsn), mpath->next_hop->addr, hopcount, ttl, | |
538 | cpu_to_le32(lifetime), cpu_to_le32(metric), | |
050ac52c LCC |
539 | 0, dev); |
540 | rcu_read_unlock(); | |
541 | sdata->u.sta.mshstats.fwded_frames++; | |
542 | return; | |
543 | ||
544 | fail: | |
545 | rcu_read_unlock(); | |
546 | sdata->u.sta.mshstats.dropped_frames_no_route++; | |
547 | return; | |
548 | } | |
549 | ||
550 | static void hwmp_perr_frame_process(struct net_device *dev, | |
551 | struct ieee80211_mgmt *mgmt, u8 *perr_elem) | |
552 | { | |
553 | struct mesh_path *mpath; | |
554 | u8 *ta, *dst_addr; | |
555 | u32 dst_dsn; | |
556 | ||
557 | ta = mgmt->sa; | |
558 | dst_addr = PERR_IE_DST_ADDR(perr_elem); | |
559 | dst_dsn = PERR_IE_DST_DSN(perr_elem); | |
560 | rcu_read_lock(); | |
561 | mpath = mesh_path_lookup(dst_addr, dev); | |
562 | if (mpath) { | |
563 | spin_lock_bh(&mpath->state_lock); | |
564 | if (mpath->flags & MESH_PATH_ACTIVE && | |
565 | memcmp(ta, mpath->next_hop->addr, ETH_ALEN) == 0 && | |
566 | (!(mpath->flags & MESH_PATH_DSN_VALID) || | |
567 | DSN_GT(dst_dsn, mpath->dsn))) { | |
568 | mpath->flags &= ~MESH_PATH_ACTIVE; | |
569 | mpath->dsn = dst_dsn; | |
570 | spin_unlock_bh(&mpath->state_lock); | |
aa2b5928 | 571 | mesh_path_error_tx(dst_addr, cpu_to_le32(dst_dsn), |
37659ff8 | 572 | dev->broadcast, dev); |
050ac52c LCC |
573 | } else |
574 | spin_unlock_bh(&mpath->state_lock); | |
575 | } | |
576 | rcu_read_unlock(); | |
577 | } | |
578 | ||
579 | ||
580 | ||
581 | void mesh_rx_path_sel_frame(struct net_device *dev, | |
582 | struct ieee80211_mgmt *mgmt, | |
583 | size_t len) | |
584 | { | |
585 | struct ieee802_11_elems elems; | |
586 | size_t baselen; | |
587 | u32 last_hop_metric; | |
588 | ||
589 | baselen = (u8 *) mgmt->u.action.u.mesh_action.variable - (u8 *) mgmt; | |
590 | ieee802_11_parse_elems(mgmt->u.action.u.mesh_action.variable, | |
591 | len - baselen, &elems); | |
592 | ||
593 | switch (mgmt->u.action.u.mesh_action.action_code) { | |
594 | case MPATH_PREQ: | |
595 | if (!elems.preq || elems.preq_len != 37) | |
596 | /* Right now we support just 1 destination and no AE */ | |
597 | return; | |
598 | last_hop_metric = hwmp_route_info_get(dev, mgmt, elems.preq); | |
599 | if (!last_hop_metric) | |
600 | return; | |
601 | hwmp_preq_frame_process(dev, mgmt, elems.preq, last_hop_metric); | |
602 | break; | |
603 | case MPATH_PREP: | |
604 | if (!elems.prep || elems.prep_len != 31) | |
605 | /* Right now we support no AE */ | |
606 | return; | |
607 | last_hop_metric = hwmp_route_info_get(dev, mgmt, elems.prep); | |
608 | if (!last_hop_metric) | |
609 | return; | |
610 | hwmp_prep_frame_process(dev, mgmt, elems.prep, last_hop_metric); | |
611 | break; | |
612 | case MPATH_PERR: | |
613 | if (!elems.perr || elems.perr_len != 12) | |
614 | /* Right now we support only one destination per PERR */ | |
615 | return; | |
616 | hwmp_perr_frame_process(dev, mgmt, elems.perr); | |
617 | default: | |
618 | return; | |
619 | } | |
620 | ||
621 | } | |
622 | ||
623 | /** | |
624 | * mesh_queue_preq - queue a PREQ to a given destination | |
625 | * | |
626 | * @mpath: mesh path to discover | |
627 | * @flags: special attributes of the PREQ to be sent | |
628 | * | |
629 | * Locking: the function must be called from within a rcu read lock block. | |
630 | * | |
631 | */ | |
632 | static void mesh_queue_preq(struct mesh_path *mpath, u8 flags) | |
633 | { | |
634 | struct ieee80211_sub_if_data *sdata = | |
635 | IEEE80211_DEV_TO_SUB_IF(mpath->dev); | |
636 | struct ieee80211_if_sta *ifsta = &sdata->u.sta; | |
637 | struct mesh_preq_queue *preq_node; | |
638 | ||
639 | preq_node = kmalloc(sizeof(struct mesh_preq_queue), GFP_KERNEL); | |
640 | if (!preq_node) { | |
641 | printk(KERN_DEBUG "Mesh HWMP: could not allocate PREQ node\n"); | |
642 | return; | |
643 | } | |
644 | ||
645 | spin_lock(&ifsta->mesh_preq_queue_lock); | |
646 | if (ifsta->preq_queue_len == MAX_PREQ_QUEUE_LEN) { | |
647 | spin_unlock(&ifsta->mesh_preq_queue_lock); | |
648 | kfree(preq_node); | |
649 | if (printk_ratelimit()) | |
650 | printk(KERN_DEBUG "Mesh HWMP: PREQ node queue full\n"); | |
651 | return; | |
652 | } | |
653 | ||
654 | memcpy(preq_node->dst, mpath->dst, ETH_ALEN); | |
655 | preq_node->flags = flags; | |
656 | ||
657 | list_add_tail(&preq_node->list, &ifsta->preq_queue.list); | |
658 | ++ifsta->preq_queue_len; | |
659 | spin_unlock(&ifsta->mesh_preq_queue_lock); | |
660 | ||
661 | if (time_after(jiffies, ifsta->last_preq + min_preq_int_jiff(sdata))) | |
662 | queue_work(sdata->local->hw.workqueue, &ifsta->work); | |
663 | ||
664 | else if (time_before(jiffies, ifsta->last_preq)) { | |
665 | /* avoid long wait if did not send preqs for a long time | |
666 | * and jiffies wrapped around | |
667 | */ | |
668 | ifsta->last_preq = jiffies - min_preq_int_jiff(sdata) - 1; | |
669 | queue_work(sdata->local->hw.workqueue, &ifsta->work); | |
670 | } else | |
671 | mod_timer(&ifsta->mesh_path_timer, ifsta->last_preq + | |
672 | min_preq_int_jiff(sdata)); | |
673 | } | |
674 | ||
675 | /** | |
676 | * mesh_path_start_discovery - launch a path discovery from the PREQ queue | |
677 | * | |
678 | * @dev: local mesh interface | |
679 | */ | |
680 | void mesh_path_start_discovery(struct net_device *dev) | |
681 | { | |
682 | struct ieee80211_sub_if_data *sdata = | |
683 | IEEE80211_DEV_TO_SUB_IF(dev); | |
684 | struct ieee80211_if_sta *ifsta = &sdata->u.sta; | |
685 | struct mesh_preq_queue *preq_node; | |
686 | struct mesh_path *mpath; | |
687 | u8 ttl, dst_flags; | |
688 | u32 lifetime; | |
689 | ||
690 | spin_lock(&ifsta->mesh_preq_queue_lock); | |
691 | if (!ifsta->preq_queue_len || | |
692 | time_before(jiffies, ifsta->last_preq + | |
693 | min_preq_int_jiff(sdata))) { | |
694 | spin_unlock(&ifsta->mesh_preq_queue_lock); | |
695 | return; | |
696 | } | |
697 | ||
698 | preq_node = list_first_entry(&ifsta->preq_queue.list, | |
699 | struct mesh_preq_queue, list); | |
700 | list_del(&preq_node->list); | |
701 | --ifsta->preq_queue_len; | |
702 | spin_unlock(&ifsta->mesh_preq_queue_lock); | |
703 | ||
704 | rcu_read_lock(); | |
705 | mpath = mesh_path_lookup(preq_node->dst, dev); | |
706 | if (!mpath) | |
707 | goto enddiscovery; | |
708 | ||
709 | spin_lock_bh(&mpath->state_lock); | |
710 | if (preq_node->flags & PREQ_Q_F_START) { | |
711 | if (mpath->flags & MESH_PATH_RESOLVING) { | |
712 | spin_unlock_bh(&mpath->state_lock); | |
713 | goto enddiscovery; | |
714 | } else { | |
715 | mpath->flags &= ~MESH_PATH_RESOLVED; | |
716 | mpath->flags |= MESH_PATH_RESOLVING; | |
717 | mpath->discovery_retries = 0; | |
718 | mpath->discovery_timeout = disc_timeout_jiff(sdata); | |
719 | } | |
720 | } else if (!(mpath->flags & MESH_PATH_RESOLVING) || | |
721 | mpath->flags & MESH_PATH_RESOLVED) { | |
722 | mpath->flags &= ~MESH_PATH_RESOLVING; | |
723 | spin_unlock_bh(&mpath->state_lock); | |
724 | goto enddiscovery; | |
725 | } | |
726 | ||
727 | ifsta->last_preq = jiffies; | |
728 | ||
729 | if (time_after(jiffies, ifsta->last_dsn_update + | |
730 | net_traversal_jiffies(sdata)) || | |
731 | time_before(jiffies, ifsta->last_dsn_update)) { | |
732 | ++ifsta->dsn; | |
733 | sdata->u.sta.last_dsn_update = jiffies; | |
734 | } | |
735 | lifetime = default_lifetime(sdata); | |
736 | ttl = sdata->u.sta.mshcfg.dot11MeshTTL; | |
737 | if (ttl == 0) { | |
738 | sdata->u.sta.mshstats.dropped_frames_ttl++; | |
739 | spin_unlock_bh(&mpath->state_lock); | |
740 | goto enddiscovery; | |
741 | } | |
742 | ||
743 | if (preq_node->flags & PREQ_Q_F_REFRESH) | |
744 | dst_flags = MP_F_DO; | |
745 | else | |
746 | dst_flags = MP_F_RF; | |
747 | ||
748 | spin_unlock_bh(&mpath->state_lock); | |
749 | mesh_path_sel_frame_tx(MPATH_PREQ, 0, dev->dev_addr, | |
aa2b5928 LCC |
750 | cpu_to_le32(ifsta->dsn), dst_flags, mpath->dst, |
751 | cpu_to_le32(mpath->dsn), dev->broadcast, 0, | |
752 | ttl, cpu_to_le32(lifetime), 0, | |
753 | cpu_to_le32(ifsta->preq_id++), dev); | |
050ac52c LCC |
754 | mod_timer(&mpath->timer, jiffies + mpath->discovery_timeout); |
755 | ||
756 | enddiscovery: | |
757 | rcu_read_unlock(); | |
758 | kfree(preq_node); | |
759 | } | |
760 | ||
761 | /** | |
762 | * ieee80211s_lookup_nexthop - put the appropriate next hop on a mesh frame | |
763 | * | |
764 | * @next_hop: output argument for next hop address | |
765 | * @skb: frame to be sent | |
766 | * @dev: network device the frame will be sent through | |
767 | * | |
768 | * Returns: 0 if the next hop was found. Nonzero otherwise. If no next hop is | |
769 | * found, the function will start a path discovery and queue the frame so it is | |
770 | * sent when the path is resolved. This means the caller must not free the skb | |
771 | * in this case. | |
772 | */ | |
773 | int mesh_nexthop_lookup(u8 *next_hop, struct sk_buff *skb, | |
774 | struct net_device *dev) | |
775 | { | |
776 | struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev); | |
777 | struct sk_buff *skb_to_free = NULL; | |
778 | struct mesh_path *mpath; | |
779 | int err = 0; | |
780 | ||
781 | rcu_read_lock(); | |
782 | mpath = mesh_path_lookup(skb->data, dev); | |
783 | ||
784 | if (!mpath) { | |
785 | mesh_path_add(skb->data, dev); | |
786 | mpath = mesh_path_lookup(skb->data, dev); | |
787 | if (!mpath) { | |
788 | dev_kfree_skb(skb); | |
789 | sdata->u.sta.mshstats.dropped_frames_no_route++; | |
790 | err = -ENOSPC; | |
791 | goto endlookup; | |
792 | } | |
793 | } | |
794 | ||
795 | if (mpath->flags & MESH_PATH_ACTIVE) { | |
796 | if (time_after(jiffies, mpath->exp_time - | |
797 | msecs_to_jiffies(sdata->u.sta.mshcfg.path_refresh_time)) | |
798 | && skb->pkt_type != PACKET_OTHERHOST | |
799 | && !(mpath->flags & MESH_PATH_RESOLVING) | |
800 | && !(mpath->flags & MESH_PATH_FIXED)) { | |
801 | mesh_queue_preq(mpath, | |
802 | PREQ_Q_F_START | PREQ_Q_F_REFRESH); | |
803 | } | |
804 | memcpy(next_hop, mpath->next_hop->addr, | |
805 | ETH_ALEN); | |
806 | } else { | |
807 | if (!(mpath->flags & MESH_PATH_RESOLVING)) { | |
808 | /* Start discovery only if it is not running yet */ | |
809 | mesh_queue_preq(mpath, PREQ_Q_F_START); | |
810 | } | |
811 | ||
812 | if (skb_queue_len(&mpath->frame_queue) >= | |
813 | MESH_FRAME_QUEUE_LEN) { | |
814 | skb_to_free = mpath->frame_queue.next; | |
815 | skb_unlink(skb_to_free, &mpath->frame_queue); | |
816 | } | |
817 | ||
818 | skb_queue_tail(&mpath->frame_queue, skb); | |
819 | if (skb_to_free) | |
820 | mesh_path_discard_frame(skb_to_free, dev); | |
821 | err = -ENOENT; | |
822 | } | |
823 | ||
824 | endlookup: | |
825 | rcu_read_unlock(); | |
826 | return err; | |
827 | } | |
828 | ||
829 | void mesh_path_timer(unsigned long data) | |
830 | { | |
831 | struct ieee80211_sub_if_data *sdata; | |
832 | struct mesh_path *mpath; | |
050ac52c LCC |
833 | |
834 | rcu_read_lock(); | |
835 | mpath = (struct mesh_path *) data; | |
836 | mpath = rcu_dereference(mpath); | |
837 | if (!mpath) | |
838 | goto endmpathtimer; | |
839 | spin_lock_bh(&mpath->state_lock); | |
840 | sdata = IEEE80211_DEV_TO_SUB_IF(mpath->dev); | |
cfa22c71 | 841 | if (mpath->flags & MESH_PATH_RESOLVED || |
050ac52c LCC |
842 | (!(mpath->flags & MESH_PATH_RESOLVING))) |
843 | mpath->flags &= ~(MESH_PATH_RESOLVING | MESH_PATH_RESOLVED); | |
844 | else if (mpath->discovery_retries < max_preq_retries(sdata)) { | |
845 | ++mpath->discovery_retries; | |
846 | mpath->discovery_timeout *= 2; | |
847 | mesh_queue_preq(mpath, 0); | |
848 | } else { | |
849 | mpath->flags = 0; | |
850 | mpath->exp_time = jiffies; | |
851 | mesh_path_flush_pending(mpath); | |
852 | } | |
853 | ||
854 | spin_unlock_bh(&mpath->state_lock); | |
855 | endmpathtimer: | |
856 | rcu_read_unlock(); | |
050ac52c | 857 | } |