2 * Copyright (c) 2008 open80211s Ltd.
3 * Authors: Luis Carlos Cobo <luisca@cozybit.com>
4 * Javier Cardona <javier@cozybit.com>
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.
11 #include "ieee80211_i.h"
14 #define ACCEPT_PLINKS 0x80
17 static struct kmem_cache *rm_cache;
19 void ieee80211s_init(void)
23 rm_cache = kmem_cache_create("mesh_rmc", sizeof(struct rmc_entry),
27 void ieee80211s_stop(void)
29 mesh_pathtbl_unregister();
30 kmem_cache_destroy(rm_cache);
34 * mesh_matches_local - check if the config of a mesh point matches ours
36 * @ie: information elements of a management frame from the mesh peer
37 * @dev: local mesh interface
39 * This function checks if the mesh configuration of a mesh point matches the
40 * local mesh configuration, i.e. if both nodes belong to the same mesh network.
42 bool mesh_matches_local(struct ieee802_11_elems *ie, struct net_device *dev)
44 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
45 struct ieee80211_if_sta *sta = &sdata->u.sta;
48 * As support for each feature is added, check for matching
49 * - On mesh config capabilities
50 * - Power Save Support En
51 * - Sync support enabled
52 * - Sync support active
53 * - Sync support required from peer
55 * - Power management control on fc
57 if (sta->mesh_id_len == ie->mesh_id_len &&
58 memcmp(sta->mesh_id, ie->mesh_id, ie->mesh_id_len) == 0 &&
59 memcmp(sta->mesh_pp_id, ie->mesh_config + PP_OFFSET, 4) == 0 &&
60 memcmp(sta->mesh_pm_id, ie->mesh_config + PM_OFFSET, 4) == 0 &&
61 memcmp(sta->mesh_cc_id, ie->mesh_config + CC_OFFSET, 4) == 0)
68 * mesh_peer_accepts_plinks - check if an mp is willing to establish peer links
70 * @ie: information elements of a management frame from the mesh peer
71 * @dev: local mesh interface
73 bool mesh_peer_accepts_plinks(struct ieee802_11_elems *ie,
74 struct net_device *dev)
76 return (*(ie->mesh_config + CAPAB_OFFSET) & ACCEPT_PLINKS) != 0;
80 * mesh_accept_plinks_update: update accepting_plink in local mesh beacons
82 * @sdata: mesh interface in which mesh beacons are going to be updated
84 void mesh_accept_plinks_update(struct ieee80211_sub_if_data *sdata)
88 /* In case mesh_plink_free_count > 0 and mesh_plinktbl_capacity == 0,
89 * the mesh interface might be able to establish plinks with peers that
90 * are already on the table but are not on PLINK_ESTAB state. However,
91 * in general the mesh interface is not accepting peer link requests
92 * from new peers, and that must be reflected in the beacon
94 free_plinks = mesh_plink_availables(sdata);
96 if (free_plinks != sdata->u.sta.accepting_plinks)
97 ieee80211_sta_timer((unsigned long) sdata);
100 void mesh_ids_set_default(struct ieee80211_if_sta *sta)
102 u8 def_id[4] = {0x00, 0x0F, 0xAC, 0xff};
104 memcpy(sta->mesh_pp_id, def_id, 4);
105 memcpy(sta->mesh_pm_id, def_id, 4);
106 memcpy(sta->mesh_cc_id, def_id, 4);
109 int mesh_rmc_init(struct net_device *dev)
111 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
114 sdata->u.sta.rmc = kmalloc(sizeof(struct mesh_rmc), GFP_KERNEL);
115 if (!sdata->u.sta.rmc)
117 sdata->u.sta.rmc->idx_mask = RMC_BUCKETS - 1;
118 for (i = 0; i < RMC_BUCKETS; i++)
119 INIT_LIST_HEAD(&sdata->u.sta.rmc->bucket[i].list);
123 void mesh_rmc_free(struct net_device *dev)
125 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
126 struct mesh_rmc *rmc = sdata->u.sta.rmc;
127 struct rmc_entry *p, *n;
130 if (!sdata->u.sta.rmc)
133 for (i = 0; i < RMC_BUCKETS; i++)
134 list_for_each_entry_safe(p, n, &rmc->bucket[i].list, list) {
136 kmem_cache_free(rm_cache, p);
140 sdata->u.sta.rmc = NULL;
144 * mesh_rmc_check - Check frame in recent multicast cache and add if absent.
146 * @sa: source address
147 * @mesh_hdr: mesh_header
149 * Returns: 0 if the frame is not in the cache, nonzero otherwise.
151 * Checks using the source address and the mesh sequence number if we have
152 * received this frame lately. If the frame is not in the cache, it is added to
155 int mesh_rmc_check(u8 *sa, struct ieee80211s_hdr *mesh_hdr,
156 struct net_device *dev)
158 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
159 struct mesh_rmc *rmc = sdata->u.sta.rmc;
163 struct rmc_entry *p, *n;
165 /* Don't care about endianness since only match matters */
166 memcpy(&seqnum, mesh_hdr->seqnum, sizeof(mesh_hdr->seqnum));
167 idx = mesh_hdr->seqnum[0] & rmc->idx_mask;
168 list_for_each_entry_safe(p, n, &rmc->bucket[idx].list, list) {
170 if (time_after(jiffies, p->exp_time) ||
171 (entries == RMC_QUEUE_MAX_LEN)) {
173 kmem_cache_free(rm_cache, p);
175 } else if ((seqnum == p->seqnum)
176 && (memcmp(sa, p->sa, ETH_ALEN) == 0))
180 p = kmem_cache_alloc(rm_cache, GFP_ATOMIC);
182 printk(KERN_DEBUG "o11s: could not allocate RMC entry\n");
186 p->exp_time = jiffies + RMC_TIMEOUT;
187 memcpy(p->sa, sa, ETH_ALEN);
188 list_add(&p->list, &rmc->bucket[idx].list);
192 void mesh_mgmt_ies_add(struct sk_buff *skb, struct net_device *dev)
194 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
195 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
196 struct ieee80211_supported_band *sband;
200 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
201 len = sband->n_bitrates;
204 pos = skb_put(skb, len + 2);
205 *pos++ = WLAN_EID_SUPP_RATES;
207 for (i = 0; i < len; i++) {
208 rate = sband->bitrates[i].bitrate;
209 *pos++ = (u8) (rate / 5);
212 if (sband->n_bitrates > len) {
213 pos = skb_put(skb, sband->n_bitrates - len + 2);
214 *pos++ = WLAN_EID_EXT_SUPP_RATES;
215 *pos++ = sband->n_bitrates - len;
216 for (i = len; i < sband->n_bitrates; i++) {
217 rate = sband->bitrates[i].bitrate;
218 *pos++ = (u8) (rate / 5);
222 pos = skb_put(skb, 2 + sdata->u.sta.mesh_id_len);
223 *pos++ = WLAN_EID_MESH_ID;
224 *pos++ = sdata->u.sta.mesh_id_len;
225 if (sdata->u.sta.mesh_id_len)
226 memcpy(pos, sdata->u.sta.mesh_id, sdata->u.sta.mesh_id_len);
228 pos = skb_put(skb, 21);
229 *pos++ = WLAN_EID_MESH_CONFIG;
230 *pos++ = MESH_CFG_LEN;
234 /* Active path selection protocol ID */
235 memcpy(pos, sdata->u.sta.mesh_pp_id, 4);
238 /* Active path selection metric ID */
239 memcpy(pos, sdata->u.sta.mesh_pm_id, 4);
242 /* Congestion control mode identifier */
243 memcpy(pos, sdata->u.sta.mesh_cc_id, 4);
246 /* Channel precedence:
247 * Not running simple channel unification protocol
249 memset(pos, 0x00, 4);
252 /* Mesh capability */
253 sdata->u.sta.accepting_plinks = mesh_plink_availables(sdata);
254 *pos++ = sdata->u.sta.accepting_plinks ? ACCEPT_PLINKS : 0x00;
260 u32 mesh_table_hash(u8 *addr, struct net_device *dev, struct mesh_table *tbl)
262 /* Use last four bytes of hw addr and interface index as hash index */
263 return jhash_2words(*(u32 *)(addr+2), dev->ifindex, tbl->hash_rnd)
267 u8 mesh_id_hash(u8 *mesh_id, int mesh_id_len)
271 else if (mesh_id_len == 1)
272 return (u8) mesh_id[0];
274 return (u8) (mesh_id[0] + 2 * mesh_id[1]);
277 struct mesh_table *mesh_table_alloc(int size_order)
280 struct mesh_table *newtbl;
282 newtbl = kmalloc(sizeof(struct mesh_table), GFP_KERNEL);
286 newtbl->hash_buckets = kzalloc(sizeof(struct hlist_head) *
287 (1 << size_order), GFP_KERNEL);
289 if (!newtbl->hash_buckets) {
294 newtbl->hashwlock = kmalloc(sizeof(spinlock_t) *
295 (1 << size_order), GFP_KERNEL);
296 if (!newtbl->hashwlock) {
297 kfree(newtbl->hash_buckets);
302 newtbl->size_order = size_order;
303 newtbl->hash_mask = (1 << size_order) - 1;
304 atomic_set(&newtbl->entries, 0);
305 get_random_bytes(&newtbl->hash_rnd,
306 sizeof(newtbl->hash_rnd));
307 for (i = 0; i <= newtbl->hash_mask; i++)
308 spin_lock_init(&newtbl->hashwlock[i]);
313 void mesh_table_free(struct mesh_table *tbl, bool free_leafs)
315 struct hlist_head *mesh_hash;
316 struct hlist_node *p, *q;
319 mesh_hash = tbl->hash_buckets;
320 for (i = 0; i <= tbl->hash_mask; i++) {
321 spin_lock(&tbl->hashwlock[i]);
322 hlist_for_each_safe(p, q, &mesh_hash[i]) {
323 tbl->free_node(p, free_leafs);
324 atomic_dec(&tbl->entries);
326 spin_unlock(&tbl->hashwlock[i]);
328 kfree(tbl->hash_buckets);
329 kfree(tbl->hashwlock);
333 static void ieee80211_mesh_path_timer(unsigned long data)
335 struct ieee80211_sub_if_data *sdata =
336 (struct ieee80211_sub_if_data *) data;
337 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
338 struct ieee80211_local *local = wdev_priv(&sdata->wdev);
340 queue_work(local->hw.workqueue, &ifsta->work);
343 struct mesh_table *mesh_table_grow(struct mesh_table *tbl)
345 struct mesh_table *newtbl;
346 struct hlist_head *oldhash;
347 struct hlist_node *p;
351 if (atomic_read(&tbl->entries)
352 < tbl->mean_chain_len * (tbl->hash_mask + 1)) {
357 newtbl = mesh_table_alloc(tbl->size_order + 1);
363 newtbl->free_node = tbl->free_node;
364 newtbl->mean_chain_len = tbl->mean_chain_len;
365 newtbl->copy_node = tbl->copy_node;
366 atomic_set(&newtbl->entries, atomic_read(&tbl->entries));
368 oldhash = tbl->hash_buckets;
369 for (i = 0; i <= tbl->hash_mask; i++)
370 hlist_for_each(p, &oldhash[i])
371 tbl->copy_node(p, newtbl);
381 * ieee80211_new_mesh_header - create a new mesh header
382 * @meshhdr: uninitialized mesh header
383 * @sdata: mesh interface to be used
385 * Return the header length.
387 int ieee80211_new_mesh_header(struct ieee80211s_hdr *meshhdr,
388 struct ieee80211_sub_if_data *sdata)
391 meshhdr->ttl = sdata->u.sta.mshcfg.dot11MeshTTL;
393 meshhdr->seqnum[0] = sdata->u.sta.mesh_seqnum[0]++;
394 meshhdr->seqnum[1] = sdata->u.sta.mesh_seqnum[1];
395 meshhdr->seqnum[2] = sdata->u.sta.mesh_seqnum[2];
397 if (sdata->u.sta.mesh_seqnum[0] == 0) {
398 sdata->u.sta.mesh_seqnum[1]++;
399 if (sdata->u.sta.mesh_seqnum[1] == 0)
400 sdata->u.sta.mesh_seqnum[2]++;
406 void ieee80211_mesh_init_sdata(struct ieee80211_sub_if_data *sdata)
408 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
410 ifsta->mshcfg.dot11MeshRetryTimeout = MESH_RET_T;
411 ifsta->mshcfg.dot11MeshConfirmTimeout = MESH_CONF_T;
412 ifsta->mshcfg.dot11MeshHoldingTimeout = MESH_HOLD_T;
413 ifsta->mshcfg.dot11MeshMaxRetries = MESH_MAX_RETR;
414 ifsta->mshcfg.dot11MeshTTL = MESH_TTL;
415 ifsta->mshcfg.auto_open_plinks = true;
416 ifsta->mshcfg.dot11MeshMaxPeerLinks =
417 MESH_MAX_ESTAB_PLINKS;
418 ifsta->mshcfg.dot11MeshHWMPactivePathTimeout =
420 ifsta->mshcfg.dot11MeshHWMPpreqMinInterval =
422 ifsta->mshcfg.dot11MeshHWMPnetDiameterTraversalTime =
423 MESH_DIAM_TRAVERSAL_TIME;
424 ifsta->mshcfg.dot11MeshHWMPmaxPREQretries =
425 MESH_MAX_PREQ_RETRIES;
426 ifsta->mshcfg.path_refresh_time =
427 MESH_PATH_REFRESH_TIME;
428 ifsta->mshcfg.min_discovery_timeout =
429 MESH_MIN_DISCOVERY_TIMEOUT;
430 ifsta->accepting_plinks = true;
433 atomic_set(&ifsta->mpaths, 0);
434 mesh_rmc_init(sdata->dev);
435 ifsta->last_preq = jiffies;
436 /* Allocate all mesh structures when creating the first mesh interface. */
439 mesh_ids_set_default(ifsta);
440 setup_timer(&ifsta->mesh_path_timer,
441 ieee80211_mesh_path_timer,
442 (unsigned long) sdata);
443 INIT_LIST_HEAD(&ifsta->preq_queue.list);
444 spin_lock_init(&ifsta->mesh_preq_queue_lock);