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