blob: 8013277924f2d75fca8acca678cbebb079c0f8a4 [file] [log] [blame]
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +01001/*
2 * Copyright (c) 2008 open80211s Ltd.
3 * Authors: Luis Carlos Cobo <luisca@cozybit.com>
4 * Javier Cardona <javier@cozybit.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
Luis Carlos Cobo51cedda2008-04-23 12:15:29 -070011#include <asm/unaligned.h>
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +010012#include "ieee80211_i.h"
13#include "mesh.h"
14
Johannes Berg472dbc42008-09-11 00:01:49 +020015#define IEEE80211_MESH_PEER_INACTIVITY_LIMIT (1800 * HZ)
16#define IEEE80211_MESH_HOUSEKEEPING_INTERVAL (60 * HZ)
17
John W. Linville24736702008-04-08 14:15:46 -040018#define PP_OFFSET 1 /* Path Selection Protocol */
19#define PM_OFFSET 5 /* Path Selection Metric */
20#define CC_OFFSET 9 /* Congestion Control Mode */
21#define CAPAB_OFFSET 17
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +010022#define ACCEPT_PLINKS 0x80
23
24int mesh_allocated;
25static struct kmem_cache *rm_cache;
26
27void ieee80211s_init(void)
28{
29 mesh_pathtbl_init();
30 mesh_allocated = 1;
31 rm_cache = kmem_cache_create("mesh_rmc", sizeof(struct rmc_entry),
32 0, 0, NULL);
33}
34
35void ieee80211s_stop(void)
36{
37 mesh_pathtbl_unregister();
38 kmem_cache_destroy(rm_cache);
39}
40
Johannes Berg472dbc42008-09-11 00:01:49 +020041static void ieee80211_mesh_housekeeping_timer(unsigned long data)
42{
43 struct ieee80211_sub_if_data *sdata = (void *) data;
44 struct ieee80211_local *local = sdata->local;
45 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
46
47 ifmsh->housekeeping = true;
48 queue_work(local->hw.workqueue, &ifmsh->work);
49}
50
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +010051/**
52 * mesh_matches_local - check if the config of a mesh point matches ours
53 *
54 * @ie: information elements of a management frame from the mesh peer
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +120055 * @sdata: local mesh subif
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +010056 *
57 * This function checks if the mesh configuration of a mesh point matches the
58 * local mesh configuration, i.e. if both nodes belong to the same mesh network.
59 */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +120060bool mesh_matches_local(struct ieee802_11_elems *ie, struct ieee80211_sub_if_data *sdata)
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +010061{
Johannes Berg472dbc42008-09-11 00:01:49 +020062 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +010063
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +010064 /*
65 * As support for each feature is added, check for matching
66 * - On mesh config capabilities
67 * - Power Save Support En
68 * - Sync support enabled
69 * - Sync support active
70 * - Sync support required from peer
71 * - MDA enabled
72 * - Power management control on fc
73 */
Johannes Berg472dbc42008-09-11 00:01:49 +020074 if (ifmsh->mesh_id_len == ie->mesh_id_len &&
75 memcmp(ifmsh->mesh_id, ie->mesh_id, ie->mesh_id_len) == 0 &&
76 memcmp(ifmsh->mesh_pp_id, ie->mesh_config + PP_OFFSET, 4) == 0 &&
77 memcmp(ifmsh->mesh_pm_id, ie->mesh_config + PM_OFFSET, 4) == 0 &&
78 memcmp(ifmsh->mesh_cc_id, ie->mesh_config + CC_OFFSET, 4) == 0)
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +010079 return true;
80
81 return false;
82}
83
84/**
85 * mesh_peer_accepts_plinks - check if an mp is willing to establish peer links
86 *
87 * @ie: information elements of a management frame from the mesh peer
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +010088 */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +120089bool mesh_peer_accepts_plinks(struct ieee802_11_elems *ie)
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +010090{
91 return (*(ie->mesh_config + CAPAB_OFFSET) & ACCEPT_PLINKS) != 0;
92}
93
94/**
95 * mesh_accept_plinks_update: update accepting_plink in local mesh beacons
96 *
Johannes Bergd0709a62008-02-25 16:27:46 +010097 * @sdata: mesh interface in which mesh beacons are going to be updated
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +010098 */
Johannes Bergd0709a62008-02-25 16:27:46 +010099void mesh_accept_plinks_update(struct ieee80211_sub_if_data *sdata)
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100100{
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100101 bool free_plinks;
102
103 /* In case mesh_plink_free_count > 0 and mesh_plinktbl_capacity == 0,
104 * the mesh interface might be able to establish plinks with peers that
Luis Carlos Cobob4e08ea2008-02-29 15:46:08 -0800105 * are already on the table but are not on PLINK_ESTAB state. However,
106 * in general the mesh interface is not accepting peer link requests
107 * from new peers, and that must be reflected in the beacon
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100108 */
109 free_plinks = mesh_plink_availables(sdata);
110
Johannes Berg472dbc42008-09-11 00:01:49 +0200111 if (free_plinks != sdata->u.mesh.accepting_plinks)
112 ieee80211_mesh_housekeeping_timer((unsigned long) sdata);
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100113}
114
Johannes Berg472dbc42008-09-11 00:01:49 +0200115void mesh_ids_set_default(struct ieee80211_if_mesh *sta)
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100116{
117 u8 def_id[4] = {0x00, 0x0F, 0xAC, 0xff};
118
119 memcpy(sta->mesh_pp_id, def_id, 4);
120 memcpy(sta->mesh_pm_id, def_id, 4);
121 memcpy(sta->mesh_cc_id, def_id, 4);
122}
123
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200124int mesh_rmc_init(struct ieee80211_sub_if_data *sdata)
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100125{
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100126 int i;
127
Johannes Berg472dbc42008-09-11 00:01:49 +0200128 sdata->u.mesh.rmc = kmalloc(sizeof(struct mesh_rmc), GFP_KERNEL);
129 if (!sdata->u.mesh.rmc)
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100130 return -ENOMEM;
Johannes Berg472dbc42008-09-11 00:01:49 +0200131 sdata->u.mesh.rmc->idx_mask = RMC_BUCKETS - 1;
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100132 for (i = 0; i < RMC_BUCKETS; i++)
Johannes Berg472dbc42008-09-11 00:01:49 +0200133 INIT_LIST_HEAD(&sdata->u.mesh.rmc->bucket[i].list);
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100134 return 0;
135}
136
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200137void mesh_rmc_free(struct ieee80211_sub_if_data *sdata)
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100138{
Johannes Berg472dbc42008-09-11 00:01:49 +0200139 struct mesh_rmc *rmc = sdata->u.mesh.rmc;
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100140 struct rmc_entry *p, *n;
141 int i;
142
Johannes Berg472dbc42008-09-11 00:01:49 +0200143 if (!sdata->u.mesh.rmc)
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100144 return;
145
146 for (i = 0; i < RMC_BUCKETS; i++)
147 list_for_each_entry_safe(p, n, &rmc->bucket[i].list, list) {
148 list_del(&p->list);
149 kmem_cache_free(rm_cache, p);
150 }
151
152 kfree(rmc);
Johannes Berg472dbc42008-09-11 00:01:49 +0200153 sdata->u.mesh.rmc = NULL;
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100154}
155
156/**
157 * mesh_rmc_check - Check frame in recent multicast cache and add if absent.
158 *
159 * @sa: source address
160 * @mesh_hdr: mesh_header
161 *
162 * Returns: 0 if the frame is not in the cache, nonzero otherwise.
163 *
164 * Checks using the source address and the mesh sequence number if we have
165 * received this frame lately. If the frame is not in the cache, it is added to
166 * it.
167 */
168int mesh_rmc_check(u8 *sa, struct ieee80211s_hdr *mesh_hdr,
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200169 struct ieee80211_sub_if_data *sdata)
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100170{
Johannes Berg472dbc42008-09-11 00:01:49 +0200171 struct mesh_rmc *rmc = sdata->u.mesh.rmc;
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100172 u32 seqnum = 0;
173 int entries = 0;
174 u8 idx;
175 struct rmc_entry *p, *n;
176
177 /* Don't care about endianness since only match matters */
Luis Carlos Cobo51cedda2008-04-23 12:15:29 -0700178 memcpy(&seqnum, &mesh_hdr->seqnum, sizeof(mesh_hdr->seqnum));
179 idx = le32_to_cpu(mesh_hdr->seqnum) & rmc->idx_mask;
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100180 list_for_each_entry_safe(p, n, &rmc->bucket[idx].list, list) {
181 ++entries;
182 if (time_after(jiffies, p->exp_time) ||
183 (entries == RMC_QUEUE_MAX_LEN)) {
184 list_del(&p->list);
185 kmem_cache_free(rm_cache, p);
186 --entries;
187 } else if ((seqnum == p->seqnum)
188 && (memcmp(sa, p->sa, ETH_ALEN) == 0))
189 return -1;
190 }
191
192 p = kmem_cache_alloc(rm_cache, GFP_ATOMIC);
193 if (!p) {
194 printk(KERN_DEBUG "o11s: could not allocate RMC entry\n");
195 return 0;
196 }
197 p->seqnum = seqnum;
198 p->exp_time = jiffies + RMC_TIMEOUT;
199 memcpy(p->sa, sa, ETH_ALEN);
200 list_add(&p->list, &rmc->bucket[idx].list);
201 return 0;
202}
203
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200204void mesh_mgmt_ies_add(struct sk_buff *skb, struct ieee80211_sub_if_data *sdata)
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100205{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200206 struct ieee80211_local *local = sdata->local;
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100207 struct ieee80211_supported_band *sband;
208 u8 *pos;
209 int len, i, rate;
210
211 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
212 len = sband->n_bitrates;
213 if (len > 8)
214 len = 8;
215 pos = skb_put(skb, len + 2);
216 *pos++ = WLAN_EID_SUPP_RATES;
217 *pos++ = len;
218 for (i = 0; i < len; i++) {
219 rate = sband->bitrates[i].bitrate;
220 *pos++ = (u8) (rate / 5);
221 }
222
223 if (sband->n_bitrates > len) {
224 pos = skb_put(skb, sband->n_bitrates - len + 2);
225 *pos++ = WLAN_EID_EXT_SUPP_RATES;
226 *pos++ = sband->n_bitrates - len;
227 for (i = len; i < sband->n_bitrates; i++) {
228 rate = sband->bitrates[i].bitrate;
229 *pos++ = (u8) (rate / 5);
230 }
231 }
232
Johannes Berg472dbc42008-09-11 00:01:49 +0200233 pos = skb_put(skb, 2 + sdata->u.mesh.mesh_id_len);
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100234 *pos++ = WLAN_EID_MESH_ID;
Johannes Berg472dbc42008-09-11 00:01:49 +0200235 *pos++ = sdata->u.mesh.mesh_id_len;
236 if (sdata->u.mesh.mesh_id_len)
237 memcpy(pos, sdata->u.mesh.mesh_id, sdata->u.mesh.mesh_id_len);
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100238
239 pos = skb_put(skb, 21);
240 *pos++ = WLAN_EID_MESH_CONFIG;
241 *pos++ = MESH_CFG_LEN;
242 /* Version */
243 *pos++ = 1;
244
245 /* Active path selection protocol ID */
Johannes Berg472dbc42008-09-11 00:01:49 +0200246 memcpy(pos, sdata->u.mesh.mesh_pp_id, 4);
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100247 pos += 4;
248
249 /* Active path selection metric ID */
Johannes Berg472dbc42008-09-11 00:01:49 +0200250 memcpy(pos, sdata->u.mesh.mesh_pm_id, 4);
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100251 pos += 4;
252
253 /* Congestion control mode identifier */
Johannes Berg472dbc42008-09-11 00:01:49 +0200254 memcpy(pos, sdata->u.mesh.mesh_cc_id, 4);
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100255 pos += 4;
256
257 /* Channel precedence:
258 * Not running simple channel unification protocol
259 */
260 memset(pos, 0x00, 4);
261 pos += 4;
262
263 /* Mesh capability */
Johannes Berg472dbc42008-09-11 00:01:49 +0200264 sdata->u.mesh.accepting_plinks = mesh_plink_availables(sdata);
265 *pos++ = sdata->u.mesh.accepting_plinks ? ACCEPT_PLINKS : 0x00;
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100266 *pos++ = 0x00;
267
268 return;
269}
270
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200271u32 mesh_table_hash(u8 *addr, struct ieee80211_sub_if_data *sdata, struct mesh_table *tbl)
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100272{
273 /* Use last four bytes of hw addr and interface index as hash index */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200274 return jhash_2words(*(u32 *)(addr+2), sdata->dev->ifindex, tbl->hash_rnd)
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100275 & tbl->hash_mask;
276}
277
278u8 mesh_id_hash(u8 *mesh_id, int mesh_id_len)
279{
280 if (!mesh_id_len)
281 return 1;
282 else if (mesh_id_len == 1)
283 return (u8) mesh_id[0];
284 else
285 return (u8) (mesh_id[0] + 2 * mesh_id[1]);
286}
287
288struct mesh_table *mesh_table_alloc(int size_order)
289{
290 int i;
291 struct mesh_table *newtbl;
292
293 newtbl = kmalloc(sizeof(struct mesh_table), GFP_KERNEL);
294 if (!newtbl)
295 return NULL;
296
297 newtbl->hash_buckets = kzalloc(sizeof(struct hlist_head) *
298 (1 << size_order), GFP_KERNEL);
299
300 if (!newtbl->hash_buckets) {
301 kfree(newtbl);
302 return NULL;
303 }
304
305 newtbl->hashwlock = kmalloc(sizeof(spinlock_t) *
306 (1 << size_order), GFP_KERNEL);
307 if (!newtbl->hashwlock) {
308 kfree(newtbl->hash_buckets);
309 kfree(newtbl);
310 return NULL;
311 }
312
313 newtbl->size_order = size_order;
314 newtbl->hash_mask = (1 << size_order) - 1;
315 atomic_set(&newtbl->entries, 0);
316 get_random_bytes(&newtbl->hash_rnd,
317 sizeof(newtbl->hash_rnd));
318 for (i = 0; i <= newtbl->hash_mask; i++)
319 spin_lock_init(&newtbl->hashwlock[i]);
320
321 return newtbl;
322}
323
Pavel Emelyanovbd9b4482008-05-07 19:57:11 +0400324static void __mesh_table_free(struct mesh_table *tbl)
325{
326 kfree(tbl->hash_buckets);
327 kfree(tbl->hashwlock);
328 kfree(tbl);
329}
330
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100331void mesh_table_free(struct mesh_table *tbl, bool free_leafs)
332{
333 struct hlist_head *mesh_hash;
334 struct hlist_node *p, *q;
335 int i;
336
337 mesh_hash = tbl->hash_buckets;
338 for (i = 0; i <= tbl->hash_mask; i++) {
339 spin_lock(&tbl->hashwlock[i]);
340 hlist_for_each_safe(p, q, &mesh_hash[i]) {
341 tbl->free_node(p, free_leafs);
342 atomic_dec(&tbl->entries);
343 }
344 spin_unlock(&tbl->hashwlock[i]);
345 }
Pavel Emelyanovbd9b4482008-05-07 19:57:11 +0400346 __mesh_table_free(tbl);
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100347}
348
349static void ieee80211_mesh_path_timer(unsigned long data)
350{
351 struct ieee80211_sub_if_data *sdata =
352 (struct ieee80211_sub_if_data *) data;
Johannes Berg472dbc42008-09-11 00:01:49 +0200353 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
Johannes Berg133b8222008-09-16 14:18:59 +0200354 struct ieee80211_local *local = sdata->local;
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100355
Johannes Berg472dbc42008-09-11 00:01:49 +0200356 queue_work(local->hw.workqueue, &ifmsh->work);
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100357}
358
359struct mesh_table *mesh_table_grow(struct mesh_table *tbl)
360{
361 struct mesh_table *newtbl;
362 struct hlist_head *oldhash;
Pavel Emelyanov4caf86c2008-05-07 19:47:01 +0400363 struct hlist_node *p, *q;
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100364 int i;
365
366 if (atomic_read(&tbl->entries)
Pavel Emelyanova3538b12008-05-07 19:55:59 +0400367 < tbl->mean_chain_len * (tbl->hash_mask + 1))
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100368 goto endgrow;
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100369
370 newtbl = mesh_table_alloc(tbl->size_order + 1);
Pavel Emelyanova3538b12008-05-07 19:55:59 +0400371 if (!newtbl)
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100372 goto endgrow;
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100373
374 newtbl->free_node = tbl->free_node;
375 newtbl->mean_chain_len = tbl->mean_chain_len;
376 newtbl->copy_node = tbl->copy_node;
377 atomic_set(&newtbl->entries, atomic_read(&tbl->entries));
378
379 oldhash = tbl->hash_buckets;
380 for (i = 0; i <= tbl->hash_mask; i++)
381 hlist_for_each(p, &oldhash[i])
Pavel Emelyanov4caf86c2008-05-07 19:47:01 +0400382 if (tbl->copy_node(p, newtbl) < 0)
383 goto errcopy;
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100384
Pavel Emelyanova3538b12008-05-07 19:55:59 +0400385 return newtbl;
Pavel Emelyanov4caf86c2008-05-07 19:47:01 +0400386
387errcopy:
388 for (i = 0; i <= newtbl->hash_mask; i++) {
389 hlist_for_each_safe(p, q, &newtbl->hash_buckets[i])
390 tbl->free_node(p, 0);
391 }
Julia Lawall667d8af2008-08-23 18:27:38 +0200392 __mesh_table_free(newtbl);
Pavel Emelyanova3538b12008-05-07 19:55:59 +0400393endgrow:
Pavel Emelyanov4caf86c2008-05-07 19:47:01 +0400394 return NULL;
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100395}
Johannes Berg902acc72008-02-23 15:17:19 +0100396
397/**
398 * ieee80211_new_mesh_header - create a new mesh header
399 * @meshhdr: uninitialized mesh header
400 * @sdata: mesh interface to be used
401 *
402 * Return the header length.
403 */
404int ieee80211_new_mesh_header(struct ieee80211s_hdr *meshhdr,
405 struct ieee80211_sub_if_data *sdata)
406{
407 meshhdr->flags = 0;
Johannes Berg472dbc42008-09-11 00:01:49 +0200408 meshhdr->ttl = sdata->u.mesh.mshcfg.dot11MeshTTL;
409 put_unaligned(cpu_to_le32(sdata->u.mesh.mesh_seqnum), &meshhdr->seqnum);
410 sdata->u.mesh.mesh_seqnum++;
Johannes Berg902acc72008-02-23 15:17:19 +0100411
Luis Carlos Coboef269252008-05-05 12:02:35 -0700412 return 6;
Johannes Berg902acc72008-02-23 15:17:19 +0100413}
414
Johannes Berg472dbc42008-09-11 00:01:49 +0200415static void ieee80211_mesh_housekeeping(struct ieee80211_sub_if_data *sdata,
416 struct ieee80211_if_mesh *ifmsh)
417{
418 bool free_plinks;
419
420#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
421 printk(KERN_DEBUG "%s: running mesh housekeeping\n",
422 sdata->dev->name);
423#endif
424
425 ieee80211_sta_expire(sdata, IEEE80211_MESH_PEER_INACTIVITY_LIMIT);
426 mesh_path_expire(sdata);
427
428 free_plinks = mesh_plink_availables(sdata);
429 if (free_plinks != sdata->u.mesh.accepting_plinks)
430 ieee80211_if_config(sdata, IEEE80211_IFCC_BEACON);
431
432 ifmsh->housekeeping = false;
433 mod_timer(&ifmsh->housekeeping_timer,
434 round_jiffies(jiffies + IEEE80211_MESH_HOUSEKEEPING_INTERVAL));
435}
436
437
438void ieee80211_start_mesh(struct ieee80211_sub_if_data *sdata)
439{
440 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
441 struct ieee80211_local *local = sdata->local;
442
443 ifmsh->housekeeping = true;
444 queue_work(local->hw.workqueue, &ifmsh->work);
445 ieee80211_if_config(sdata, IEEE80211_IFCC_BEACON);
446}
447
448void ieee80211_stop_mesh(struct ieee80211_sub_if_data *sdata)
449{
450 del_timer_sync(&sdata->u.mesh.housekeeping_timer);
451 /*
Johannes Bergb7413432008-09-11 00:01:50 +0200452 * If the timer fired while we waited for it, it will have
453 * requeued the work. Now the work will be running again
454 * but will not rearm the timer again because it checks
455 * whether the interface is running, which, at this point,
456 * it no longer is.
457 */
458 cancel_work_sync(&sdata->u.mesh.work);
459
460 /*
Johannes Berg472dbc42008-09-11 00:01:49 +0200461 * When we get here, the interface is marked down.
462 * Call synchronize_rcu() to wait for the RX path
463 * should it be using the interface and enqueuing
464 * frames at this very time on another CPU.
465 */
466 synchronize_rcu();
467 skb_queue_purge(&sdata->u.mesh.skb_queue);
468}
469
470static void ieee80211_mesh_rx_bcn_presp(struct ieee80211_sub_if_data *sdata,
471 u16 stype,
472 struct ieee80211_mgmt *mgmt,
473 size_t len,
474 struct ieee80211_rx_status *rx_status)
475{
476 struct ieee80211_local *local= sdata->local;
477 struct ieee802_11_elems elems;
478 struct ieee80211_channel *channel;
479 u64 supp_rates = 0;
480 size_t baselen;
481 int freq;
482 enum ieee80211_band band = rx_status->band;
483
484 /* ignore ProbeResp to foreign address */
485 if (stype == IEEE80211_STYPE_PROBE_RESP &&
486 compare_ether_addr(mgmt->da, sdata->dev->dev_addr))
487 return;
488
489 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
490 if (baselen > len)
491 return;
492
493 ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
494 &elems);
495
496 if (elems.ds_params && elems.ds_params_len == 1)
497 freq = ieee80211_channel_to_frequency(elems.ds_params[0]);
498 else
499 freq = rx_status->freq;
500
501 channel = ieee80211_get_channel(local->hw.wiphy, freq);
502
503 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
504 return;
505
506 if (elems.mesh_id && elems.mesh_config &&
507 mesh_matches_local(&elems, sdata)) {
508 supp_rates = ieee80211_sta_get_rates(local, &elems, band);
509
510 mesh_neighbour_update(mgmt->sa, supp_rates, sdata,
511 mesh_peer_accepts_plinks(&elems));
512 }
513}
514
515static void ieee80211_mesh_rx_mgmt_action(struct ieee80211_sub_if_data *sdata,
516 struct ieee80211_mgmt *mgmt,
517 size_t len,
518 struct ieee80211_rx_status *rx_status)
519{
520 switch (mgmt->u.action.category) {
521 case PLINK_CATEGORY:
522 mesh_rx_plink_frame(sdata, mgmt, len, rx_status);
523 break;
524 case MESH_PATH_SEL_CATEGORY:
525 mesh_rx_path_sel_frame(sdata, mgmt, len);
526 break;
527 }
528}
529
530static void ieee80211_mesh_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
531 struct sk_buff *skb)
532{
533 struct ieee80211_rx_status *rx_status;
534 struct ieee80211_if_mesh *ifmsh;
535 struct ieee80211_mgmt *mgmt;
536 u16 stype;
537
538 ifmsh = &sdata->u.mesh;
539
540 rx_status = (struct ieee80211_rx_status *) skb->cb;
541 mgmt = (struct ieee80211_mgmt *) skb->data;
542 stype = le16_to_cpu(mgmt->frame_control) & IEEE80211_FCTL_STYPE;
543
544 switch (stype) {
545 case IEEE80211_STYPE_PROBE_RESP:
546 case IEEE80211_STYPE_BEACON:
547 ieee80211_mesh_rx_bcn_presp(sdata, stype, mgmt, skb->len,
548 rx_status);
549 break;
550 case IEEE80211_STYPE_ACTION:
551 ieee80211_mesh_rx_mgmt_action(sdata, mgmt, skb->len, rx_status);
552 break;
553 }
554
555 kfree_skb(skb);
556}
557
558static void ieee80211_mesh_work(struct work_struct *work)
559{
560 struct ieee80211_sub_if_data *sdata =
561 container_of(work, struct ieee80211_sub_if_data, u.mesh.work);
562 struct ieee80211_local *local = sdata->local;
563 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
564 struct sk_buff *skb;
565
566 if (!netif_running(sdata->dev))
567 return;
568
Johannes Bergc2b13452008-09-11 00:01:55 +0200569 if (local->sw_scanning || local->hw_scanning)
Johannes Berg472dbc42008-09-11 00:01:49 +0200570 return;
571
572 while ((skb = skb_dequeue(&ifmsh->skb_queue)))
573 ieee80211_mesh_rx_queued_mgmt(sdata, skb);
574
575 if (ifmsh->preq_queue_len &&
576 time_after(jiffies,
577 ifmsh->last_preq + msecs_to_jiffies(ifmsh->mshcfg.dot11MeshHWMPpreqMinInterval)))
578 mesh_path_start_discovery(sdata);
579
580 if (ifmsh->housekeeping)
581 ieee80211_mesh_housekeeping(sdata, ifmsh);
582}
583
584void ieee80211_mesh_notify_scan_completed(struct ieee80211_local *local)
585{
586 struct ieee80211_sub_if_data *sdata;
587
588 rcu_read_lock();
589 list_for_each_entry_rcu(sdata, &local->interfaces, list)
590 if (ieee80211_vif_is_mesh(&sdata->vif))
591 queue_work(local->hw.workqueue, &sdata->u.mesh.work);
592 rcu_read_unlock();
593}
594
Johannes Berg902acc72008-02-23 15:17:19 +0100595void ieee80211_mesh_init_sdata(struct ieee80211_sub_if_data *sdata)
596{
Johannes Berg472dbc42008-09-11 00:01:49 +0200597 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
Johannes Berg902acc72008-02-23 15:17:19 +0100598
Johannes Berg472dbc42008-09-11 00:01:49 +0200599 INIT_WORK(&ifmsh->work, ieee80211_mesh_work);
600 setup_timer(&ifmsh->housekeeping_timer,
601 ieee80211_mesh_housekeeping_timer,
602 (unsigned long) sdata);
603 skb_queue_head_init(&sdata->u.mesh.skb_queue);
604
605 ifmsh->mshcfg.dot11MeshRetryTimeout = MESH_RET_T;
606 ifmsh->mshcfg.dot11MeshConfirmTimeout = MESH_CONF_T;
607 ifmsh->mshcfg.dot11MeshHoldingTimeout = MESH_HOLD_T;
608 ifmsh->mshcfg.dot11MeshMaxRetries = MESH_MAX_RETR;
609 ifmsh->mshcfg.dot11MeshTTL = MESH_TTL;
610 ifmsh->mshcfg.auto_open_plinks = true;
611 ifmsh->mshcfg.dot11MeshMaxPeerLinks =
Johannes Berg902acc72008-02-23 15:17:19 +0100612 MESH_MAX_ESTAB_PLINKS;
Johannes Berg472dbc42008-09-11 00:01:49 +0200613 ifmsh->mshcfg.dot11MeshHWMPactivePathTimeout =
Johannes Berg902acc72008-02-23 15:17:19 +0100614 MESH_PATH_TIMEOUT;
Johannes Berg472dbc42008-09-11 00:01:49 +0200615 ifmsh->mshcfg.dot11MeshHWMPpreqMinInterval =
Johannes Berg902acc72008-02-23 15:17:19 +0100616 MESH_PREQ_MIN_INT;
Johannes Berg472dbc42008-09-11 00:01:49 +0200617 ifmsh->mshcfg.dot11MeshHWMPnetDiameterTraversalTime =
Johannes Berg902acc72008-02-23 15:17:19 +0100618 MESH_DIAM_TRAVERSAL_TIME;
Johannes Berg472dbc42008-09-11 00:01:49 +0200619 ifmsh->mshcfg.dot11MeshHWMPmaxPREQretries =
Johannes Berg902acc72008-02-23 15:17:19 +0100620 MESH_MAX_PREQ_RETRIES;
Johannes Berg472dbc42008-09-11 00:01:49 +0200621 ifmsh->mshcfg.path_refresh_time =
Johannes Berg902acc72008-02-23 15:17:19 +0100622 MESH_PATH_REFRESH_TIME;
Johannes Berg472dbc42008-09-11 00:01:49 +0200623 ifmsh->mshcfg.min_discovery_timeout =
Johannes Berg902acc72008-02-23 15:17:19 +0100624 MESH_MIN_DISCOVERY_TIMEOUT;
Johannes Berg472dbc42008-09-11 00:01:49 +0200625 ifmsh->accepting_plinks = true;
626 ifmsh->preq_id = 0;
627 ifmsh->dsn = 0;
628 atomic_set(&ifmsh->mpaths, 0);
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200629 mesh_rmc_init(sdata);
Johannes Berg472dbc42008-09-11 00:01:49 +0200630 ifmsh->last_preq = jiffies;
Johannes Berg902acc72008-02-23 15:17:19 +0100631 /* Allocate all mesh structures when creating the first mesh interface. */
632 if (!mesh_allocated)
633 ieee80211s_init();
Johannes Berg472dbc42008-09-11 00:01:49 +0200634 mesh_ids_set_default(ifmsh);
635 setup_timer(&ifmsh->mesh_path_timer,
Johannes Berg902acc72008-02-23 15:17:19 +0100636 ieee80211_mesh_path_timer,
637 (unsigned long) sdata);
Johannes Berg472dbc42008-09-11 00:01:49 +0200638 INIT_LIST_HEAD(&ifmsh->preq_queue.list);
639 spin_lock_init(&ifmsh->mesh_preq_queue_lock);
640}
641
642ieee80211_rx_result
643ieee80211_mesh_rx_mgmt(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb,
644 struct ieee80211_rx_status *rx_status)
645{
646 struct ieee80211_local *local = sdata->local;
647 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
648 struct ieee80211_mgmt *mgmt;
649 u16 fc;
650
651 if (skb->len < 24)
652 return RX_DROP_MONITOR;
653
654 mgmt = (struct ieee80211_mgmt *) skb->data;
655 fc = le16_to_cpu(mgmt->frame_control);
656
657 switch (fc & IEEE80211_FCTL_STYPE) {
658 case IEEE80211_STYPE_PROBE_RESP:
659 case IEEE80211_STYPE_BEACON:
660 case IEEE80211_STYPE_ACTION:
661 memcpy(skb->cb, rx_status, sizeof(*rx_status));
662 skb_queue_tail(&ifmsh->skb_queue, skb);
663 queue_work(local->hw.workqueue, &ifmsh->work);
664 return RX_QUEUED;
665 }
666
667 return RX_CONTINUE;
Johannes Berg902acc72008-02-23 15:17:19 +0100668}