Luis Carlos Cobo | 2e3c873 | 2008-02-23 15:17:09 +0100 | [diff] [blame] | 1 | /* |
| 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 Cobo | 51cedda | 2008-04-23 12:15:29 -0700 | [diff] [blame] | 11 | #include <asm/unaligned.h> |
Luis Carlos Cobo | 2e3c873 | 2008-02-23 15:17:09 +0100 | [diff] [blame] | 12 | #include "ieee80211_i.h" |
| 13 | #include "mesh.h" |
| 14 | |
John W. Linville | 2473670 | 2008-04-08 14:15:46 -0400 | [diff] [blame] | 15 | #define PP_OFFSET 1 /* Path Selection Protocol */ |
| 16 | #define PM_OFFSET 5 /* Path Selection Metric */ |
| 17 | #define CC_OFFSET 9 /* Congestion Control Mode */ |
| 18 | #define CAPAB_OFFSET 17 |
Luis Carlos Cobo | 2e3c873 | 2008-02-23 15:17:09 +0100 | [diff] [blame] | 19 | #define ACCEPT_PLINKS 0x80 |
| 20 | |
| 21 | int mesh_allocated; |
| 22 | static struct kmem_cache *rm_cache; |
| 23 | |
| 24 | void ieee80211s_init(void) |
| 25 | { |
| 26 | mesh_pathtbl_init(); |
| 27 | mesh_allocated = 1; |
| 28 | rm_cache = kmem_cache_create("mesh_rmc", sizeof(struct rmc_entry), |
| 29 | 0, 0, NULL); |
| 30 | } |
| 31 | |
| 32 | void ieee80211s_stop(void) |
| 33 | { |
| 34 | mesh_pathtbl_unregister(); |
| 35 | kmem_cache_destroy(rm_cache); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * mesh_matches_local - check if the config of a mesh point matches ours |
| 40 | * |
| 41 | * @ie: information elements of a management frame from the mesh peer |
| 42 | * @dev: local mesh interface |
| 43 | * |
| 44 | * This function checks if the mesh configuration of a mesh point matches the |
| 45 | * local mesh configuration, i.e. if both nodes belong to the same mesh network. |
| 46 | */ |
| 47 | bool mesh_matches_local(struct ieee802_11_elems *ie, struct net_device *dev) |
| 48 | { |
| 49 | struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev); |
| 50 | struct ieee80211_if_sta *sta = &sdata->u.sta; |
| 51 | |
Luis Carlos Cobo | 2e3c873 | 2008-02-23 15:17:09 +0100 | [diff] [blame] | 52 | /* |
| 53 | * As support for each feature is added, check for matching |
| 54 | * - On mesh config capabilities |
| 55 | * - Power Save Support En |
| 56 | * - Sync support enabled |
| 57 | * - Sync support active |
| 58 | * - Sync support required from peer |
| 59 | * - MDA enabled |
| 60 | * - Power management control on fc |
| 61 | */ |
Luis Carlos Cobo | 3b091cd | 2008-02-29 12:20:39 -0800 | [diff] [blame] | 62 | if (sta->mesh_id_len == ie->mesh_id_len && |
| 63 | memcmp(sta->mesh_id, ie->mesh_id, ie->mesh_id_len) == 0 && |
| 64 | memcmp(sta->mesh_pp_id, ie->mesh_config + PP_OFFSET, 4) == 0 && |
| 65 | memcmp(sta->mesh_pm_id, ie->mesh_config + PM_OFFSET, 4) == 0 && |
| 66 | memcmp(sta->mesh_cc_id, ie->mesh_config + CC_OFFSET, 4) == 0) |
Luis Carlos Cobo | 2e3c873 | 2008-02-23 15:17:09 +0100 | [diff] [blame] | 67 | return true; |
| 68 | |
| 69 | return false; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * mesh_peer_accepts_plinks - check if an mp is willing to establish peer links |
| 74 | * |
| 75 | * @ie: information elements of a management frame from the mesh peer |
| 76 | * @dev: local mesh interface |
| 77 | */ |
| 78 | bool mesh_peer_accepts_plinks(struct ieee802_11_elems *ie, |
| 79 | struct net_device *dev) |
| 80 | { |
| 81 | return (*(ie->mesh_config + CAPAB_OFFSET) & ACCEPT_PLINKS) != 0; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * mesh_accept_plinks_update: update accepting_plink in local mesh beacons |
| 86 | * |
Johannes Berg | d0709a6 | 2008-02-25 16:27:46 +0100 | [diff] [blame] | 87 | * @sdata: mesh interface in which mesh beacons are going to be updated |
Luis Carlos Cobo | 2e3c873 | 2008-02-23 15:17:09 +0100 | [diff] [blame] | 88 | */ |
Johannes Berg | d0709a6 | 2008-02-25 16:27:46 +0100 | [diff] [blame] | 89 | void mesh_accept_plinks_update(struct ieee80211_sub_if_data *sdata) |
Luis Carlos Cobo | 2e3c873 | 2008-02-23 15:17:09 +0100 | [diff] [blame] | 90 | { |
Luis Carlos Cobo | 2e3c873 | 2008-02-23 15:17:09 +0100 | [diff] [blame] | 91 | bool free_plinks; |
| 92 | |
| 93 | /* In case mesh_plink_free_count > 0 and mesh_plinktbl_capacity == 0, |
| 94 | * the mesh interface might be able to establish plinks with peers that |
Luis Carlos Cobo | b4e08ea | 2008-02-29 15:46:08 -0800 | [diff] [blame] | 95 | * are already on the table but are not on PLINK_ESTAB state. However, |
| 96 | * in general the mesh interface is not accepting peer link requests |
| 97 | * from new peers, and that must be reflected in the beacon |
Luis Carlos Cobo | 2e3c873 | 2008-02-23 15:17:09 +0100 | [diff] [blame] | 98 | */ |
| 99 | free_plinks = mesh_plink_availables(sdata); |
| 100 | |
| 101 | if (free_plinks != sdata->u.sta.accepting_plinks) |
| 102 | ieee80211_sta_timer((unsigned long) sdata); |
| 103 | } |
| 104 | |
| 105 | void mesh_ids_set_default(struct ieee80211_if_sta *sta) |
| 106 | { |
| 107 | u8 def_id[4] = {0x00, 0x0F, 0xAC, 0xff}; |
| 108 | |
| 109 | memcpy(sta->mesh_pp_id, def_id, 4); |
| 110 | memcpy(sta->mesh_pm_id, def_id, 4); |
| 111 | memcpy(sta->mesh_cc_id, def_id, 4); |
| 112 | } |
| 113 | |
| 114 | int mesh_rmc_init(struct net_device *dev) |
| 115 | { |
| 116 | struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev); |
| 117 | int i; |
| 118 | |
| 119 | sdata->u.sta.rmc = kmalloc(sizeof(struct mesh_rmc), GFP_KERNEL); |
| 120 | if (!sdata->u.sta.rmc) |
| 121 | return -ENOMEM; |
| 122 | sdata->u.sta.rmc->idx_mask = RMC_BUCKETS - 1; |
| 123 | for (i = 0; i < RMC_BUCKETS; i++) |
| 124 | INIT_LIST_HEAD(&sdata->u.sta.rmc->bucket[i].list); |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | void mesh_rmc_free(struct net_device *dev) |
| 129 | { |
| 130 | struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev); |
| 131 | struct mesh_rmc *rmc = sdata->u.sta.rmc; |
| 132 | struct rmc_entry *p, *n; |
| 133 | int i; |
| 134 | |
| 135 | if (!sdata->u.sta.rmc) |
| 136 | return; |
| 137 | |
| 138 | for (i = 0; i < RMC_BUCKETS; i++) |
| 139 | list_for_each_entry_safe(p, n, &rmc->bucket[i].list, list) { |
| 140 | list_del(&p->list); |
| 141 | kmem_cache_free(rm_cache, p); |
| 142 | } |
| 143 | |
| 144 | kfree(rmc); |
| 145 | sdata->u.sta.rmc = NULL; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * mesh_rmc_check - Check frame in recent multicast cache and add if absent. |
| 150 | * |
| 151 | * @sa: source address |
| 152 | * @mesh_hdr: mesh_header |
| 153 | * |
| 154 | * Returns: 0 if the frame is not in the cache, nonzero otherwise. |
| 155 | * |
| 156 | * Checks using the source address and the mesh sequence number if we have |
| 157 | * received this frame lately. If the frame is not in the cache, it is added to |
| 158 | * it. |
| 159 | */ |
| 160 | int mesh_rmc_check(u8 *sa, struct ieee80211s_hdr *mesh_hdr, |
| 161 | struct net_device *dev) |
| 162 | { |
| 163 | struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev); |
| 164 | struct mesh_rmc *rmc = sdata->u.sta.rmc; |
| 165 | u32 seqnum = 0; |
| 166 | int entries = 0; |
| 167 | u8 idx; |
| 168 | struct rmc_entry *p, *n; |
| 169 | |
| 170 | /* Don't care about endianness since only match matters */ |
Luis Carlos Cobo | 51cedda | 2008-04-23 12:15:29 -0700 | [diff] [blame] | 171 | memcpy(&seqnum, &mesh_hdr->seqnum, sizeof(mesh_hdr->seqnum)); |
| 172 | idx = le32_to_cpu(mesh_hdr->seqnum) & rmc->idx_mask; |
Luis Carlos Cobo | 2e3c873 | 2008-02-23 15:17:09 +0100 | [diff] [blame] | 173 | list_for_each_entry_safe(p, n, &rmc->bucket[idx].list, list) { |
| 174 | ++entries; |
| 175 | if (time_after(jiffies, p->exp_time) || |
| 176 | (entries == RMC_QUEUE_MAX_LEN)) { |
| 177 | list_del(&p->list); |
| 178 | kmem_cache_free(rm_cache, p); |
| 179 | --entries; |
| 180 | } else if ((seqnum == p->seqnum) |
| 181 | && (memcmp(sa, p->sa, ETH_ALEN) == 0)) |
| 182 | return -1; |
| 183 | } |
| 184 | |
| 185 | p = kmem_cache_alloc(rm_cache, GFP_ATOMIC); |
| 186 | if (!p) { |
| 187 | printk(KERN_DEBUG "o11s: could not allocate RMC entry\n"); |
| 188 | return 0; |
| 189 | } |
| 190 | p->seqnum = seqnum; |
| 191 | p->exp_time = jiffies + RMC_TIMEOUT; |
| 192 | memcpy(p->sa, sa, ETH_ALEN); |
| 193 | list_add(&p->list, &rmc->bucket[idx].list); |
| 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | void mesh_mgmt_ies_add(struct sk_buff *skb, struct net_device *dev) |
| 198 | { |
| 199 | struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr); |
| 200 | struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev); |
| 201 | struct ieee80211_supported_band *sband; |
| 202 | u8 *pos; |
| 203 | int len, i, rate; |
| 204 | |
| 205 | sband = local->hw.wiphy->bands[local->hw.conf.channel->band]; |
| 206 | len = sband->n_bitrates; |
| 207 | if (len > 8) |
| 208 | len = 8; |
| 209 | pos = skb_put(skb, len + 2); |
| 210 | *pos++ = WLAN_EID_SUPP_RATES; |
| 211 | *pos++ = len; |
| 212 | for (i = 0; i < len; i++) { |
| 213 | rate = sband->bitrates[i].bitrate; |
| 214 | *pos++ = (u8) (rate / 5); |
| 215 | } |
| 216 | |
| 217 | if (sband->n_bitrates > len) { |
| 218 | pos = skb_put(skb, sband->n_bitrates - len + 2); |
| 219 | *pos++ = WLAN_EID_EXT_SUPP_RATES; |
| 220 | *pos++ = sband->n_bitrates - len; |
| 221 | for (i = len; i < sband->n_bitrates; i++) { |
| 222 | rate = sband->bitrates[i].bitrate; |
| 223 | *pos++ = (u8) (rate / 5); |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | pos = skb_put(skb, 2 + sdata->u.sta.mesh_id_len); |
| 228 | *pos++ = WLAN_EID_MESH_ID; |
| 229 | *pos++ = sdata->u.sta.mesh_id_len; |
| 230 | if (sdata->u.sta.mesh_id_len) |
| 231 | memcpy(pos, sdata->u.sta.mesh_id, sdata->u.sta.mesh_id_len); |
| 232 | |
| 233 | pos = skb_put(skb, 21); |
| 234 | *pos++ = WLAN_EID_MESH_CONFIG; |
| 235 | *pos++ = MESH_CFG_LEN; |
| 236 | /* Version */ |
| 237 | *pos++ = 1; |
| 238 | |
| 239 | /* Active path selection protocol ID */ |
| 240 | memcpy(pos, sdata->u.sta.mesh_pp_id, 4); |
| 241 | pos += 4; |
| 242 | |
| 243 | /* Active path selection metric ID */ |
| 244 | memcpy(pos, sdata->u.sta.mesh_pm_id, 4); |
| 245 | pos += 4; |
| 246 | |
| 247 | /* Congestion control mode identifier */ |
| 248 | memcpy(pos, sdata->u.sta.mesh_cc_id, 4); |
| 249 | pos += 4; |
| 250 | |
| 251 | /* Channel precedence: |
| 252 | * Not running simple channel unification protocol |
| 253 | */ |
| 254 | memset(pos, 0x00, 4); |
| 255 | pos += 4; |
| 256 | |
| 257 | /* Mesh capability */ |
| 258 | sdata->u.sta.accepting_plinks = mesh_plink_availables(sdata); |
| 259 | *pos++ = sdata->u.sta.accepting_plinks ? ACCEPT_PLINKS : 0x00; |
| 260 | *pos++ = 0x00; |
| 261 | |
| 262 | return; |
| 263 | } |
| 264 | |
| 265 | u32 mesh_table_hash(u8 *addr, struct net_device *dev, struct mesh_table *tbl) |
| 266 | { |
| 267 | /* Use last four bytes of hw addr and interface index as hash index */ |
| 268 | return jhash_2words(*(u32 *)(addr+2), dev->ifindex, tbl->hash_rnd) |
| 269 | & tbl->hash_mask; |
| 270 | } |
| 271 | |
| 272 | u8 mesh_id_hash(u8 *mesh_id, int mesh_id_len) |
| 273 | { |
| 274 | if (!mesh_id_len) |
| 275 | return 1; |
| 276 | else if (mesh_id_len == 1) |
| 277 | return (u8) mesh_id[0]; |
| 278 | else |
| 279 | return (u8) (mesh_id[0] + 2 * mesh_id[1]); |
| 280 | } |
| 281 | |
| 282 | struct mesh_table *mesh_table_alloc(int size_order) |
| 283 | { |
| 284 | int i; |
| 285 | struct mesh_table *newtbl; |
| 286 | |
| 287 | newtbl = kmalloc(sizeof(struct mesh_table), GFP_KERNEL); |
| 288 | if (!newtbl) |
| 289 | return NULL; |
| 290 | |
| 291 | newtbl->hash_buckets = kzalloc(sizeof(struct hlist_head) * |
| 292 | (1 << size_order), GFP_KERNEL); |
| 293 | |
| 294 | if (!newtbl->hash_buckets) { |
| 295 | kfree(newtbl); |
| 296 | return NULL; |
| 297 | } |
| 298 | |
| 299 | newtbl->hashwlock = kmalloc(sizeof(spinlock_t) * |
| 300 | (1 << size_order), GFP_KERNEL); |
| 301 | if (!newtbl->hashwlock) { |
| 302 | kfree(newtbl->hash_buckets); |
| 303 | kfree(newtbl); |
| 304 | return NULL; |
| 305 | } |
| 306 | |
| 307 | newtbl->size_order = size_order; |
| 308 | newtbl->hash_mask = (1 << size_order) - 1; |
| 309 | atomic_set(&newtbl->entries, 0); |
| 310 | get_random_bytes(&newtbl->hash_rnd, |
| 311 | sizeof(newtbl->hash_rnd)); |
| 312 | for (i = 0; i <= newtbl->hash_mask; i++) |
| 313 | spin_lock_init(&newtbl->hashwlock[i]); |
| 314 | |
| 315 | return newtbl; |
| 316 | } |
| 317 | |
| 318 | void mesh_table_free(struct mesh_table *tbl, bool free_leafs) |
| 319 | { |
| 320 | struct hlist_head *mesh_hash; |
| 321 | struct hlist_node *p, *q; |
| 322 | int i; |
| 323 | |
| 324 | mesh_hash = tbl->hash_buckets; |
| 325 | for (i = 0; i <= tbl->hash_mask; i++) { |
| 326 | spin_lock(&tbl->hashwlock[i]); |
| 327 | hlist_for_each_safe(p, q, &mesh_hash[i]) { |
| 328 | tbl->free_node(p, free_leafs); |
| 329 | atomic_dec(&tbl->entries); |
| 330 | } |
| 331 | spin_unlock(&tbl->hashwlock[i]); |
| 332 | } |
| 333 | kfree(tbl->hash_buckets); |
| 334 | kfree(tbl->hashwlock); |
| 335 | kfree(tbl); |
| 336 | } |
| 337 | |
| 338 | static void ieee80211_mesh_path_timer(unsigned long data) |
| 339 | { |
| 340 | struct ieee80211_sub_if_data *sdata = |
| 341 | (struct ieee80211_sub_if_data *) data; |
| 342 | struct ieee80211_if_sta *ifsta = &sdata->u.sta; |
| 343 | struct ieee80211_local *local = wdev_priv(&sdata->wdev); |
| 344 | |
| 345 | queue_work(local->hw.workqueue, &ifsta->work); |
| 346 | } |
| 347 | |
| 348 | struct mesh_table *mesh_table_grow(struct mesh_table *tbl) |
| 349 | { |
| 350 | struct mesh_table *newtbl; |
| 351 | struct hlist_head *oldhash; |
| 352 | struct hlist_node *p; |
| 353 | int err = 0; |
| 354 | int i; |
| 355 | |
| 356 | if (atomic_read(&tbl->entries) |
| 357 | < tbl->mean_chain_len * (tbl->hash_mask + 1)) { |
| 358 | err = -EPERM; |
| 359 | goto endgrow; |
| 360 | } |
| 361 | |
| 362 | newtbl = mesh_table_alloc(tbl->size_order + 1); |
| 363 | if (!newtbl) { |
| 364 | err = -ENOMEM; |
| 365 | goto endgrow; |
| 366 | } |
| 367 | |
| 368 | newtbl->free_node = tbl->free_node; |
| 369 | newtbl->mean_chain_len = tbl->mean_chain_len; |
| 370 | newtbl->copy_node = tbl->copy_node; |
| 371 | atomic_set(&newtbl->entries, atomic_read(&tbl->entries)); |
| 372 | |
| 373 | oldhash = tbl->hash_buckets; |
| 374 | for (i = 0; i <= tbl->hash_mask; i++) |
| 375 | hlist_for_each(p, &oldhash[i]) |
| 376 | tbl->copy_node(p, newtbl); |
| 377 | |
| 378 | endgrow: |
| 379 | if (err) |
| 380 | return NULL; |
| 381 | else |
| 382 | return newtbl; |
| 383 | } |
Johannes Berg | 902acc7 | 2008-02-23 15:17:19 +0100 | [diff] [blame] | 384 | |
| 385 | /** |
| 386 | * ieee80211_new_mesh_header - create a new mesh header |
| 387 | * @meshhdr: uninitialized mesh header |
| 388 | * @sdata: mesh interface to be used |
| 389 | * |
| 390 | * Return the header length. |
| 391 | */ |
| 392 | int ieee80211_new_mesh_header(struct ieee80211s_hdr *meshhdr, |
| 393 | struct ieee80211_sub_if_data *sdata) |
| 394 | { |
| 395 | meshhdr->flags = 0; |
| 396 | meshhdr->ttl = sdata->u.sta.mshcfg.dot11MeshTTL; |
Luis Carlos Cobo | 51cedda | 2008-04-23 12:15:29 -0700 | [diff] [blame] | 397 | put_unaligned(cpu_to_le32(sdata->u.sta.mesh_seqnum), &meshhdr->seqnum); |
| 398 | sdata->u.sta.mesh_seqnum++; |
Johannes Berg | 902acc7 | 2008-02-23 15:17:19 +0100 | [diff] [blame] | 399 | |
| 400 | return 5; |
| 401 | } |
| 402 | |
| 403 | void ieee80211_mesh_init_sdata(struct ieee80211_sub_if_data *sdata) |
| 404 | { |
| 405 | struct ieee80211_if_sta *ifsta = &sdata->u.sta; |
| 406 | |
| 407 | ifsta->mshcfg.dot11MeshRetryTimeout = MESH_RET_T; |
| 408 | ifsta->mshcfg.dot11MeshConfirmTimeout = MESH_CONF_T; |
| 409 | ifsta->mshcfg.dot11MeshHoldingTimeout = MESH_HOLD_T; |
| 410 | ifsta->mshcfg.dot11MeshMaxRetries = MESH_MAX_RETR; |
| 411 | ifsta->mshcfg.dot11MeshTTL = MESH_TTL; |
| 412 | ifsta->mshcfg.auto_open_plinks = true; |
| 413 | ifsta->mshcfg.dot11MeshMaxPeerLinks = |
| 414 | MESH_MAX_ESTAB_PLINKS; |
| 415 | ifsta->mshcfg.dot11MeshHWMPactivePathTimeout = |
| 416 | MESH_PATH_TIMEOUT; |
| 417 | ifsta->mshcfg.dot11MeshHWMPpreqMinInterval = |
| 418 | MESH_PREQ_MIN_INT; |
| 419 | ifsta->mshcfg.dot11MeshHWMPnetDiameterTraversalTime = |
| 420 | MESH_DIAM_TRAVERSAL_TIME; |
| 421 | ifsta->mshcfg.dot11MeshHWMPmaxPREQretries = |
| 422 | MESH_MAX_PREQ_RETRIES; |
| 423 | ifsta->mshcfg.path_refresh_time = |
| 424 | MESH_PATH_REFRESH_TIME; |
| 425 | ifsta->mshcfg.min_discovery_timeout = |
| 426 | MESH_MIN_DISCOVERY_TIMEOUT; |
| 427 | ifsta->accepting_plinks = true; |
| 428 | ifsta->preq_id = 0; |
| 429 | ifsta->dsn = 0; |
| 430 | atomic_set(&ifsta->mpaths, 0); |
| 431 | mesh_rmc_init(sdata->dev); |
| 432 | ifsta->last_preq = jiffies; |
| 433 | /* Allocate all mesh structures when creating the first mesh interface. */ |
| 434 | if (!mesh_allocated) |
| 435 | ieee80211s_init(); |
| 436 | mesh_ids_set_default(ifsta); |
| 437 | setup_timer(&ifsta->mesh_path_timer, |
| 438 | ieee80211_mesh_path_timer, |
| 439 | (unsigned long) sdata); |
| 440 | INIT_LIST_HEAD(&ifsta->preq_queue.list); |
| 441 | spin_lock_init(&ifsta->mesh_preq_queue_lock); |
| 442 | } |