Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 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 | |
| 10 | #include <linux/etherdevice.h> |
| 11 | #include <linux/list.h> |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 12 | #include <linux/random.h> |
| 13 | #include <linux/spinlock.h> |
| 14 | #include <linux/string.h> |
| 15 | #include <net/mac80211.h> |
| 16 | #include "ieee80211_i.h" |
| 17 | #include "mesh.h" |
| 18 | |
| 19 | /* There will be initially 2^INIT_PATHS_SIZE_ORDER buckets */ |
| 20 | #define INIT_PATHS_SIZE_ORDER 2 |
| 21 | |
| 22 | /* Keep the mean chain length below this constant */ |
| 23 | #define MEAN_CHAIN_LEN 2 |
| 24 | |
| 25 | #define MPATH_EXPIRED(mpath) ((mpath->flags & MESH_PATH_ACTIVE) && \ |
| 26 | time_after(jiffies, mpath->exp_time) && \ |
| 27 | !(mpath->flags & MESH_PATH_FIXED)) |
| 28 | |
| 29 | struct mpath_node { |
| 30 | struct hlist_node list; |
| 31 | struct rcu_head rcu; |
| 32 | /* This indirection allows two different tables to point to the same |
| 33 | * mesh_path structure, useful when resizing |
| 34 | */ |
| 35 | struct mesh_path *mpath; |
| 36 | }; |
| 37 | |
| 38 | static struct mesh_table *mesh_paths; |
YanBo | 79617de | 2008-09-22 13:30:32 +0800 | [diff] [blame] | 39 | static struct mesh_table *mpp_paths; /* Store paths for MPP&MAP */ |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 40 | |
| 41 | /* This lock will have the grow table function as writer and add / delete nodes |
| 42 | * as readers. When reading the table (i.e. doing lookups) we are well protected |
| 43 | * by RCU |
| 44 | */ |
| 45 | static DEFINE_RWLOCK(pathtbl_resize_lock); |
| 46 | |
| 47 | /** |
| 48 | * |
| 49 | * mesh_path_assign_nexthop - update mesh path next hop |
| 50 | * |
| 51 | * @mpath: mesh path to update |
| 52 | * @sta: next hop to assign |
| 53 | * |
| 54 | * Locking: mpath->state_lock must be held when calling this function |
| 55 | */ |
| 56 | void mesh_path_assign_nexthop(struct mesh_path *mpath, struct sta_info *sta) |
| 57 | { |
Javier Cardona | 10c836d | 2009-07-09 14:42:16 -0700 | [diff] [blame^] | 58 | struct sk_buff *skb; |
| 59 | struct ieee80211_hdr *hdr; |
| 60 | struct sk_buff_head tmpq; |
| 61 | unsigned long flags; |
| 62 | |
Johannes Berg | d0709a6 | 2008-02-25 16:27:46 +0100 | [diff] [blame] | 63 | rcu_assign_pointer(mpath->next_hop, sta); |
Javier Cardona | 10c836d | 2009-07-09 14:42:16 -0700 | [diff] [blame^] | 64 | |
| 65 | __skb_queue_head_init(&tmpq); |
| 66 | |
| 67 | spin_lock_irqsave(&mpath->frame_queue.lock, flags); |
| 68 | |
| 69 | while ((skb = __skb_dequeue(&mpath->frame_queue)) != NULL) { |
| 70 | hdr = (struct ieee80211_hdr *) skb->data; |
| 71 | memcpy(hdr->addr1, sta->sta.addr, ETH_ALEN); |
| 72 | __skb_queue_tail(&tmpq, skb); |
| 73 | } |
| 74 | |
| 75 | skb_queue_splice(&tmpq, &mpath->frame_queue); |
| 76 | spin_unlock_irqrestore(&mpath->frame_queue.lock, flags); |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | |
| 80 | /** |
| 81 | * mesh_path_lookup - look up a path in the mesh path table |
| 82 | * @dst: hardware address (ETH_ALEN length) of destination |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 83 | * @sdata: local subif |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 84 | * |
| 85 | * Returns: pointer to the mesh path structure, or NULL if not found |
| 86 | * |
| 87 | * Locking: must be called within a read rcu section. |
| 88 | */ |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 89 | struct mesh_path *mesh_path_lookup(u8 *dst, struct ieee80211_sub_if_data *sdata) |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 90 | { |
| 91 | struct mesh_path *mpath; |
| 92 | struct hlist_node *n; |
| 93 | struct hlist_head *bucket; |
| 94 | struct mesh_table *tbl; |
| 95 | struct mpath_node *node; |
| 96 | |
| 97 | tbl = rcu_dereference(mesh_paths); |
| 98 | |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 99 | bucket = &tbl->hash_buckets[mesh_table_hash(dst, sdata, tbl)]; |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 100 | hlist_for_each_entry_rcu(node, n, bucket, list) { |
| 101 | mpath = node->mpath; |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 102 | if (mpath->sdata == sdata && |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 103 | memcmp(dst, mpath->dst, ETH_ALEN) == 0) { |
| 104 | if (MPATH_EXPIRED(mpath)) { |
| 105 | spin_lock_bh(&mpath->state_lock); |
| 106 | if (MPATH_EXPIRED(mpath)) |
| 107 | mpath->flags &= ~MESH_PATH_ACTIVE; |
| 108 | spin_unlock_bh(&mpath->state_lock); |
| 109 | } |
| 110 | return mpath; |
| 111 | } |
| 112 | } |
| 113 | return NULL; |
| 114 | } |
| 115 | |
YanBo | 79617de | 2008-09-22 13:30:32 +0800 | [diff] [blame] | 116 | struct mesh_path *mpp_path_lookup(u8 *dst, struct ieee80211_sub_if_data *sdata) |
| 117 | { |
| 118 | struct mesh_path *mpath; |
| 119 | struct hlist_node *n; |
| 120 | struct hlist_head *bucket; |
| 121 | struct mesh_table *tbl; |
| 122 | struct mpath_node *node; |
| 123 | |
| 124 | tbl = rcu_dereference(mpp_paths); |
| 125 | |
| 126 | bucket = &tbl->hash_buckets[mesh_table_hash(dst, sdata, tbl)]; |
| 127 | hlist_for_each_entry_rcu(node, n, bucket, list) { |
| 128 | mpath = node->mpath; |
| 129 | if (mpath->sdata == sdata && |
| 130 | memcmp(dst, mpath->dst, ETH_ALEN) == 0) { |
| 131 | if (MPATH_EXPIRED(mpath)) { |
| 132 | spin_lock_bh(&mpath->state_lock); |
| 133 | if (MPATH_EXPIRED(mpath)) |
| 134 | mpath->flags &= ~MESH_PATH_ACTIVE; |
| 135 | spin_unlock_bh(&mpath->state_lock); |
| 136 | } |
| 137 | return mpath; |
| 138 | } |
| 139 | } |
| 140 | return NULL; |
| 141 | } |
| 142 | |
| 143 | |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 144 | /** |
| 145 | * mesh_path_lookup_by_idx - look up a path in the mesh path table by its index |
| 146 | * @idx: index |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 147 | * @sdata: local subif, or NULL for all entries |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 148 | * |
| 149 | * Returns: pointer to the mesh path structure, or NULL if not found. |
| 150 | * |
| 151 | * Locking: must be called within a read rcu section. |
| 152 | */ |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 153 | struct mesh_path *mesh_path_lookup_by_idx(int idx, struct ieee80211_sub_if_data *sdata) |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 154 | { |
| 155 | struct mpath_node *node; |
| 156 | struct hlist_node *p; |
| 157 | int i; |
| 158 | int j = 0; |
| 159 | |
Luis Carlos Cobo | 2a8ca29 | 2008-02-29 17:51:25 -0800 | [diff] [blame] | 160 | for_each_mesh_entry(mesh_paths, p, node, i) { |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 161 | if (sdata && node->mpath->sdata != sdata) |
Luis Carlos Cobo | 2a8ca29 | 2008-02-29 17:51:25 -0800 | [diff] [blame] | 162 | continue; |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 163 | if (j++ == idx) { |
| 164 | if (MPATH_EXPIRED(node->mpath)) { |
| 165 | spin_lock_bh(&node->mpath->state_lock); |
| 166 | if (MPATH_EXPIRED(node->mpath)) |
| 167 | node->mpath->flags &= ~MESH_PATH_ACTIVE; |
| 168 | spin_unlock_bh(&node->mpath->state_lock); |
| 169 | } |
| 170 | return node->mpath; |
| 171 | } |
Luis Carlos Cobo | 2a8ca29 | 2008-02-29 17:51:25 -0800 | [diff] [blame] | 172 | } |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 173 | |
| 174 | return NULL; |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * mesh_path_add - allocate and add a new path to the mesh path table |
| 179 | * @addr: destination address of the path (ETH_ALEN length) |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 180 | * @sdata: local subif |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 181 | * |
| 182 | * Returns: 0 on sucess |
| 183 | * |
| 184 | * State: the initial state of the new path is set to 0 |
| 185 | */ |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 186 | int mesh_path_add(u8 *dst, struct ieee80211_sub_if_data *sdata) |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 187 | { |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 188 | struct mesh_path *mpath, *new_mpath; |
| 189 | struct mpath_node *node, *new_node; |
| 190 | struct hlist_head *bucket; |
| 191 | struct hlist_node *n; |
| 192 | int grow = 0; |
| 193 | int err = 0; |
| 194 | u32 hash_idx; |
| 195 | |
Johannes Berg | e2e414d | 2009-07-10 11:38:14 +0200 | [diff] [blame] | 196 | might_sleep(); |
| 197 | |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 198 | if (memcmp(dst, sdata->dev->dev_addr, ETH_ALEN) == 0) |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 199 | /* never add ourselves as neighbours */ |
| 200 | return -ENOTSUPP; |
| 201 | |
| 202 | if (is_multicast_ether_addr(dst)) |
| 203 | return -ENOTSUPP; |
| 204 | |
Johannes Berg | 472dbc4 | 2008-09-11 00:01:49 +0200 | [diff] [blame] | 205 | if (atomic_add_unless(&sdata->u.mesh.mpaths, 1, MESH_MAX_MPATHS) == 0) |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 206 | return -ENOSPC; |
| 207 | |
Pavel Emelyanov | 402d775 | 2008-05-06 18:53:43 +0400 | [diff] [blame] | 208 | err = -ENOMEM; |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 209 | new_mpath = kzalloc(sizeof(struct mesh_path), GFP_KERNEL); |
Pavel Emelyanov | 402d775 | 2008-05-06 18:53:43 +0400 | [diff] [blame] | 210 | if (!new_mpath) |
| 211 | goto err_path_alloc; |
| 212 | |
Pavel Emelyanov | 0eb03d5 | 2008-05-06 18:49:02 +0400 | [diff] [blame] | 213 | new_node = kmalloc(sizeof(struct mpath_node), GFP_KERNEL); |
Pavel Emelyanov | 402d775 | 2008-05-06 18:53:43 +0400 | [diff] [blame] | 214 | if (!new_node) |
| 215 | goto err_node_alloc; |
Pavel Emelyanov | f84e71a | 2008-05-06 18:46:36 +0400 | [diff] [blame] | 216 | |
| 217 | read_lock(&pathtbl_resize_lock); |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 218 | memcpy(new_mpath->dst, dst, ETH_ALEN); |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 219 | new_mpath->sdata = sdata; |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 220 | new_mpath->flags = 0; |
| 221 | skb_queue_head_init(&new_mpath->frame_queue); |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 222 | new_node->mpath = new_mpath; |
| 223 | new_mpath->timer.data = (unsigned long) new_mpath; |
| 224 | new_mpath->timer.function = mesh_path_timer; |
| 225 | new_mpath->exp_time = jiffies; |
| 226 | spin_lock_init(&new_mpath->state_lock); |
| 227 | init_timer(&new_mpath->timer); |
| 228 | |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 229 | hash_idx = mesh_table_hash(dst, sdata, mesh_paths); |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 230 | bucket = &mesh_paths->hash_buckets[hash_idx]; |
| 231 | |
| 232 | spin_lock(&mesh_paths->hashwlock[hash_idx]); |
| 233 | |
Pavel Emelyanov | 402d775 | 2008-05-06 18:53:43 +0400 | [diff] [blame] | 234 | err = -EEXIST; |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 235 | hlist_for_each_entry(node, n, bucket, list) { |
| 236 | mpath = node->mpath; |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 237 | if (mpath->sdata == sdata && memcmp(dst, mpath->dst, ETH_ALEN) == 0) |
Pavel Emelyanov | 402d775 | 2008-05-06 18:53:43 +0400 | [diff] [blame] | 238 | goto err_exists; |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | hlist_add_head_rcu(&new_node->list, bucket); |
| 242 | if (atomic_inc_return(&mesh_paths->entries) >= |
| 243 | mesh_paths->mean_chain_len * (mesh_paths->hash_mask + 1)) |
| 244 | grow = 1; |
| 245 | |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 246 | spin_unlock(&mesh_paths->hashwlock[hash_idx]); |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 247 | read_unlock(&pathtbl_resize_lock); |
Pavel Emelyanov | 402d775 | 2008-05-06 18:53:43 +0400 | [diff] [blame] | 248 | if (grow) { |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 249 | struct mesh_table *oldtbl, *newtbl; |
| 250 | |
| 251 | write_lock(&pathtbl_resize_lock); |
| 252 | oldtbl = mesh_paths; |
| 253 | newtbl = mesh_table_grow(mesh_paths); |
| 254 | if (!newtbl) { |
| 255 | write_unlock(&pathtbl_resize_lock); |
Pavel Emelyanov | 3282aea | 2008-05-07 19:54:04 +0400 | [diff] [blame] | 256 | return 0; |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 257 | } |
| 258 | rcu_assign_pointer(mesh_paths, newtbl); |
Pavel Emelyanov | 6d6936e | 2008-05-06 18:51:31 +0400 | [diff] [blame] | 259 | write_unlock(&pathtbl_resize_lock); |
| 260 | |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 261 | synchronize_rcu(); |
| 262 | mesh_table_free(oldtbl, false); |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 263 | } |
Pavel Emelyanov | 402d775 | 2008-05-06 18:53:43 +0400 | [diff] [blame] | 264 | return 0; |
| 265 | |
| 266 | err_exists: |
| 267 | spin_unlock(&mesh_paths->hashwlock[hash_idx]); |
| 268 | read_unlock(&pathtbl_resize_lock); |
| 269 | kfree(new_node); |
| 270 | err_node_alloc: |
| 271 | kfree(new_mpath); |
| 272 | err_path_alloc: |
Johannes Berg | 472dbc4 | 2008-09-11 00:01:49 +0200 | [diff] [blame] | 273 | atomic_dec(&sdata->u.mesh.mpaths); |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 274 | return err; |
| 275 | } |
| 276 | |
| 277 | |
YanBo | 79617de | 2008-09-22 13:30:32 +0800 | [diff] [blame] | 278 | int mpp_path_add(u8 *dst, u8 *mpp, struct ieee80211_sub_if_data *sdata) |
| 279 | { |
| 280 | struct mesh_path *mpath, *new_mpath; |
| 281 | struct mpath_node *node, *new_node; |
| 282 | struct hlist_head *bucket; |
| 283 | struct hlist_node *n; |
| 284 | int grow = 0; |
| 285 | int err = 0; |
| 286 | u32 hash_idx; |
| 287 | |
Johannes Berg | e2e414d | 2009-07-10 11:38:14 +0200 | [diff] [blame] | 288 | might_sleep(); |
YanBo | 79617de | 2008-09-22 13:30:32 +0800 | [diff] [blame] | 289 | |
| 290 | if (memcmp(dst, sdata->dev->dev_addr, ETH_ALEN) == 0) |
| 291 | /* never add ourselves as neighbours */ |
| 292 | return -ENOTSUPP; |
| 293 | |
| 294 | if (is_multicast_ether_addr(dst)) |
| 295 | return -ENOTSUPP; |
| 296 | |
| 297 | err = -ENOMEM; |
| 298 | new_mpath = kzalloc(sizeof(struct mesh_path), GFP_KERNEL); |
| 299 | if (!new_mpath) |
| 300 | goto err_path_alloc; |
| 301 | |
| 302 | new_node = kmalloc(sizeof(struct mpath_node), GFP_KERNEL); |
| 303 | if (!new_node) |
| 304 | goto err_node_alloc; |
| 305 | |
| 306 | read_lock(&pathtbl_resize_lock); |
| 307 | memcpy(new_mpath->dst, dst, ETH_ALEN); |
| 308 | memcpy(new_mpath->mpp, mpp, ETH_ALEN); |
| 309 | new_mpath->sdata = sdata; |
| 310 | new_mpath->flags = 0; |
| 311 | skb_queue_head_init(&new_mpath->frame_queue); |
| 312 | new_node->mpath = new_mpath; |
| 313 | new_mpath->exp_time = jiffies; |
| 314 | spin_lock_init(&new_mpath->state_lock); |
| 315 | |
| 316 | hash_idx = mesh_table_hash(dst, sdata, mpp_paths); |
| 317 | bucket = &mpp_paths->hash_buckets[hash_idx]; |
| 318 | |
| 319 | spin_lock(&mpp_paths->hashwlock[hash_idx]); |
| 320 | |
| 321 | err = -EEXIST; |
| 322 | hlist_for_each_entry(node, n, bucket, list) { |
| 323 | mpath = node->mpath; |
| 324 | if (mpath->sdata == sdata && memcmp(dst, mpath->dst, ETH_ALEN) == 0) |
| 325 | goto err_exists; |
| 326 | } |
| 327 | |
| 328 | hlist_add_head_rcu(&new_node->list, bucket); |
| 329 | if (atomic_inc_return(&mpp_paths->entries) >= |
| 330 | mpp_paths->mean_chain_len * (mpp_paths->hash_mask + 1)) |
| 331 | grow = 1; |
| 332 | |
| 333 | spin_unlock(&mpp_paths->hashwlock[hash_idx]); |
| 334 | read_unlock(&pathtbl_resize_lock); |
| 335 | if (grow) { |
| 336 | struct mesh_table *oldtbl, *newtbl; |
| 337 | |
| 338 | write_lock(&pathtbl_resize_lock); |
| 339 | oldtbl = mpp_paths; |
| 340 | newtbl = mesh_table_grow(mpp_paths); |
| 341 | if (!newtbl) { |
| 342 | write_unlock(&pathtbl_resize_lock); |
| 343 | return 0; |
| 344 | } |
| 345 | rcu_assign_pointer(mpp_paths, newtbl); |
| 346 | write_unlock(&pathtbl_resize_lock); |
| 347 | |
| 348 | synchronize_rcu(); |
| 349 | mesh_table_free(oldtbl, false); |
| 350 | } |
| 351 | return 0; |
| 352 | |
| 353 | err_exists: |
| 354 | spin_unlock(&mpp_paths->hashwlock[hash_idx]); |
| 355 | read_unlock(&pathtbl_resize_lock); |
| 356 | kfree(new_node); |
| 357 | err_node_alloc: |
| 358 | kfree(new_mpath); |
| 359 | err_path_alloc: |
| 360 | return err; |
| 361 | } |
| 362 | |
| 363 | |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 364 | /** |
| 365 | * mesh_plink_broken - deactivates paths and sends perr when a link breaks |
| 366 | * |
| 367 | * @sta: broken peer link |
| 368 | * |
| 369 | * This function must be called from the rate control algorithm if enough |
| 370 | * delivery errors suggest that a peer link is no longer usable. |
| 371 | */ |
| 372 | void mesh_plink_broken(struct sta_info *sta) |
| 373 | { |
| 374 | struct mesh_path *mpath; |
| 375 | struct mpath_node *node; |
| 376 | struct hlist_node *p; |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 377 | struct ieee80211_sub_if_data *sdata = sta->sdata; |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 378 | int i; |
| 379 | |
| 380 | rcu_read_lock(); |
| 381 | for_each_mesh_entry(mesh_paths, p, node, i) { |
| 382 | mpath = node->mpath; |
| 383 | spin_lock_bh(&mpath->state_lock); |
| 384 | if (mpath->next_hop == sta && |
| 385 | mpath->flags & MESH_PATH_ACTIVE && |
| 386 | !(mpath->flags & MESH_PATH_FIXED)) { |
| 387 | mpath->flags &= ~MESH_PATH_ACTIVE; |
| 388 | ++mpath->dsn; |
| 389 | spin_unlock_bh(&mpath->state_lock); |
| 390 | mesh_path_error_tx(mpath->dst, |
| 391 | cpu_to_le32(mpath->dsn), |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 392 | sdata->dev->broadcast, sdata); |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 393 | } else |
| 394 | spin_unlock_bh(&mpath->state_lock); |
| 395 | } |
| 396 | rcu_read_unlock(); |
| 397 | } |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 398 | |
| 399 | /** |
| 400 | * mesh_path_flush_by_nexthop - Deletes mesh paths if their next hop matches |
| 401 | * |
| 402 | * @sta - mesh peer to match |
| 403 | * |
Luis Carlos Cobo | b4e08ea | 2008-02-29 15:46:08 -0800 | [diff] [blame] | 404 | * RCU notes: this function is called when a mesh plink transitions from |
| 405 | * PLINK_ESTAB to any other state, since PLINK_ESTAB state is the only one that |
| 406 | * allows path creation. This will happen before the sta can be freed (because |
Johannes Berg | d0709a6 | 2008-02-25 16:27:46 +0100 | [diff] [blame] | 407 | * sta_info_destroy() calls this) so any reader in a rcu read block will be |
| 408 | * protected against the plink disappearing. |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 409 | */ |
| 410 | void mesh_path_flush_by_nexthop(struct sta_info *sta) |
| 411 | { |
| 412 | struct mesh_path *mpath; |
| 413 | struct mpath_node *node; |
| 414 | struct hlist_node *p; |
| 415 | int i; |
| 416 | |
| 417 | for_each_mesh_entry(mesh_paths, p, node, i) { |
| 418 | mpath = node->mpath; |
| 419 | if (mpath->next_hop == sta) |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 420 | mesh_path_del(mpath->dst, mpath->sdata); |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 421 | } |
| 422 | } |
| 423 | |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 424 | void mesh_path_flush(struct ieee80211_sub_if_data *sdata) |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 425 | { |
| 426 | struct mesh_path *mpath; |
| 427 | struct mpath_node *node; |
| 428 | struct hlist_node *p; |
| 429 | int i; |
| 430 | |
| 431 | for_each_mesh_entry(mesh_paths, p, node, i) { |
| 432 | mpath = node->mpath; |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 433 | if (mpath->sdata == sdata) |
| 434 | mesh_path_del(mpath->dst, mpath->sdata); |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 435 | } |
| 436 | } |
| 437 | |
| 438 | static void mesh_path_node_reclaim(struct rcu_head *rp) |
| 439 | { |
| 440 | struct mpath_node *node = container_of(rp, struct mpath_node, rcu); |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 441 | struct ieee80211_sub_if_data *sdata = node->mpath->sdata; |
Johannes Berg | d0709a6 | 2008-02-25 16:27:46 +0100 | [diff] [blame] | 442 | |
Luis Carlos Cobo | 89a1ad6 | 2008-02-29 14:49:37 -0800 | [diff] [blame] | 443 | del_timer_sync(&node->mpath->timer); |
Johannes Berg | 472dbc4 | 2008-09-11 00:01:49 +0200 | [diff] [blame] | 444 | atomic_dec(&sdata->u.mesh.mpaths); |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 445 | kfree(node->mpath); |
| 446 | kfree(node); |
| 447 | } |
| 448 | |
| 449 | /** |
| 450 | * mesh_path_del - delete a mesh path from the table |
| 451 | * |
| 452 | * @addr: dst address (ETH_ALEN length) |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 453 | * @sdata: local subif |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 454 | * |
| 455 | * Returns: 0 if succesful |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 456 | */ |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 457 | int mesh_path_del(u8 *addr, struct ieee80211_sub_if_data *sdata) |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 458 | { |
| 459 | struct mesh_path *mpath; |
| 460 | struct mpath_node *node; |
| 461 | struct hlist_head *bucket; |
| 462 | struct hlist_node *n; |
| 463 | int hash_idx; |
| 464 | int err = 0; |
| 465 | |
| 466 | read_lock(&pathtbl_resize_lock); |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 467 | hash_idx = mesh_table_hash(addr, sdata, mesh_paths); |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 468 | bucket = &mesh_paths->hash_buckets[hash_idx]; |
| 469 | |
| 470 | spin_lock(&mesh_paths->hashwlock[hash_idx]); |
| 471 | hlist_for_each_entry(node, n, bucket, list) { |
| 472 | mpath = node->mpath; |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 473 | if (mpath->sdata == sdata && |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 474 | memcmp(addr, mpath->dst, ETH_ALEN) == 0) { |
| 475 | spin_lock_bh(&mpath->state_lock); |
Luis Carlos Cobo | cfa22c7 | 2008-02-29 15:04:13 -0800 | [diff] [blame] | 476 | mpath->flags |= MESH_PATH_RESOLVING; |
| 477 | hlist_del_rcu(&node->list); |
| 478 | call_rcu(&node->rcu, mesh_path_node_reclaim); |
| 479 | atomic_dec(&mesh_paths->entries); |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 480 | spin_unlock_bh(&mpath->state_lock); |
| 481 | goto enddel; |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | err = -ENXIO; |
| 486 | enddel: |
| 487 | spin_unlock(&mesh_paths->hashwlock[hash_idx]); |
| 488 | read_unlock(&pathtbl_resize_lock); |
| 489 | return err; |
| 490 | } |
| 491 | |
| 492 | /** |
| 493 | * mesh_path_tx_pending - sends pending frames in a mesh path queue |
| 494 | * |
| 495 | * @mpath: mesh path to activate |
| 496 | * |
| 497 | * Locking: the state_lock of the mpath structure must NOT be held when calling |
| 498 | * this function. |
| 499 | */ |
| 500 | void mesh_path_tx_pending(struct mesh_path *mpath) |
| 501 | { |
| 502 | struct sk_buff *skb; |
| 503 | |
| 504 | while ((skb = skb_dequeue(&mpath->frame_queue)) && |
| 505 | (mpath->flags & MESH_PATH_ACTIVE)) |
| 506 | dev_queue_xmit(skb); |
| 507 | } |
| 508 | |
| 509 | /** |
| 510 | * mesh_path_discard_frame - discard a frame whose path could not be resolved |
| 511 | * |
| 512 | * @skb: frame to discard |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 513 | * @sdata: network subif the frame was to be sent through |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 514 | * |
Javier Cardona | 35946a5 | 2009-07-13 17:00:10 -0700 | [diff] [blame] | 515 | * If the frame was being forwarded from another MP, a PERR frame will be sent |
| 516 | * to the precursor. The precursor's address (i.e. the previous hop) was saved |
| 517 | * in addr1 of the frame-to-be-forwarded, and would only be overwritten once |
| 518 | * the destination is successfully resolved. |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 519 | * |
| 520 | * Locking: the function must me called within a rcu_read_lock region |
| 521 | */ |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 522 | void mesh_path_discard_frame(struct sk_buff *skb, |
| 523 | struct ieee80211_sub_if_data *sdata) |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 524 | { |
Luis Carlos Cobo | e32f85f | 2008-08-05 19:34:52 +0200 | [diff] [blame] | 525 | struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data; |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 526 | struct mesh_path *mpath; |
| 527 | u32 dsn = 0; |
| 528 | |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 529 | if (memcmp(hdr->addr4, sdata->dev->dev_addr, ETH_ALEN) != 0) { |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 530 | u8 *ra, *da; |
| 531 | |
Luis Carlos Cobo | e32f85f | 2008-08-05 19:34:52 +0200 | [diff] [blame] | 532 | da = hdr->addr3; |
Javier Cardona | 35946a5 | 2009-07-13 17:00:10 -0700 | [diff] [blame] | 533 | ra = hdr->addr1; |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 534 | mpath = mesh_path_lookup(da, sdata); |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 535 | if (mpath) |
| 536 | dsn = ++mpath->dsn; |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 537 | mesh_path_error_tx(skb->data, cpu_to_le32(dsn), ra, sdata); |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | kfree_skb(skb); |
Johannes Berg | 472dbc4 | 2008-09-11 00:01:49 +0200 | [diff] [blame] | 541 | sdata->u.mesh.mshstats.dropped_frames_no_route++; |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | /** |
| 545 | * mesh_path_flush_pending - free the pending queue of a mesh path |
| 546 | * |
| 547 | * @mpath: mesh path whose queue has to be freed |
| 548 | * |
| 549 | * Locking: the function must me called withing a rcu_read_lock region |
| 550 | */ |
| 551 | void mesh_path_flush_pending(struct mesh_path *mpath) |
| 552 | { |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 553 | struct sk_buff *skb; |
| 554 | |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 555 | while ((skb = skb_dequeue(&mpath->frame_queue)) && |
| 556 | (mpath->flags & MESH_PATH_ACTIVE)) |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 557 | mesh_path_discard_frame(skb, mpath->sdata); |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | /** |
| 561 | * mesh_path_fix_nexthop - force a specific next hop for a mesh path |
| 562 | * |
| 563 | * @mpath: the mesh path to modify |
| 564 | * @next_hop: the next hop to force |
| 565 | * |
| 566 | * Locking: this function must be called holding mpath->state_lock |
| 567 | */ |
| 568 | void mesh_path_fix_nexthop(struct mesh_path *mpath, struct sta_info *next_hop) |
| 569 | { |
| 570 | spin_lock_bh(&mpath->state_lock); |
| 571 | mesh_path_assign_nexthop(mpath, next_hop); |
| 572 | mpath->dsn = 0xffff; |
| 573 | mpath->metric = 0; |
| 574 | mpath->hop_count = 0; |
| 575 | mpath->exp_time = 0; |
| 576 | mpath->flags |= MESH_PATH_FIXED; |
| 577 | mesh_path_activate(mpath); |
| 578 | spin_unlock_bh(&mpath->state_lock); |
| 579 | mesh_path_tx_pending(mpath); |
| 580 | } |
| 581 | |
| 582 | static void mesh_path_node_free(struct hlist_node *p, bool free_leafs) |
| 583 | { |
| 584 | struct mesh_path *mpath; |
| 585 | struct mpath_node *node = hlist_entry(p, struct mpath_node, list); |
| 586 | mpath = node->mpath; |
| 587 | hlist_del_rcu(p); |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 588 | if (free_leafs) |
| 589 | kfree(mpath); |
| 590 | kfree(node); |
| 591 | } |
| 592 | |
Pavel Emelyanov | 4caf86c | 2008-05-07 19:47:01 +0400 | [diff] [blame] | 593 | static int mesh_path_node_copy(struct hlist_node *p, struct mesh_table *newtbl) |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 594 | { |
| 595 | struct mesh_path *mpath; |
| 596 | struct mpath_node *node, *new_node; |
| 597 | u32 hash_idx; |
| 598 | |
Pavel Emelyanov | 8566dc3 | 2008-05-07 19:51:51 +0400 | [diff] [blame] | 599 | new_node = kmalloc(sizeof(struct mpath_node), GFP_ATOMIC); |
Pavel Emelyanov | 00242c4 | 2008-05-07 19:48:14 +0400 | [diff] [blame] | 600 | if (new_node == NULL) |
| 601 | return -ENOMEM; |
| 602 | |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 603 | node = hlist_entry(p, struct mpath_node, list); |
| 604 | mpath = node->mpath; |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 605 | new_node->mpath = mpath; |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 606 | hash_idx = mesh_table_hash(mpath->dst, mpath->sdata, newtbl); |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 607 | hlist_add_head(&new_node->list, |
| 608 | &newtbl->hash_buckets[hash_idx]); |
Pavel Emelyanov | 4caf86c | 2008-05-07 19:47:01 +0400 | [diff] [blame] | 609 | return 0; |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | int mesh_pathtbl_init(void) |
| 613 | { |
| 614 | mesh_paths = mesh_table_alloc(INIT_PATHS_SIZE_ORDER); |
YanBo | 79617de | 2008-09-22 13:30:32 +0800 | [diff] [blame] | 615 | if (!mesh_paths) |
| 616 | return -ENOMEM; |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 617 | mesh_paths->free_node = &mesh_path_node_free; |
| 618 | mesh_paths->copy_node = &mesh_path_node_copy; |
| 619 | mesh_paths->mean_chain_len = MEAN_CHAIN_LEN; |
YanBo | 79617de | 2008-09-22 13:30:32 +0800 | [diff] [blame] | 620 | |
| 621 | mpp_paths = mesh_table_alloc(INIT_PATHS_SIZE_ORDER); |
| 622 | if (!mpp_paths) { |
| 623 | mesh_table_free(mesh_paths, true); |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 624 | return -ENOMEM; |
YanBo | 79617de | 2008-09-22 13:30:32 +0800 | [diff] [blame] | 625 | } |
| 626 | mpp_paths->free_node = &mesh_path_node_free; |
| 627 | mpp_paths->copy_node = &mesh_path_node_copy; |
| 628 | mpp_paths->mean_chain_len = MEAN_CHAIN_LEN; |
| 629 | |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 630 | return 0; |
| 631 | } |
| 632 | |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 633 | void mesh_path_expire(struct ieee80211_sub_if_data *sdata) |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 634 | { |
| 635 | struct mesh_path *mpath; |
| 636 | struct mpath_node *node; |
| 637 | struct hlist_node *p; |
| 638 | int i; |
| 639 | |
| 640 | read_lock(&pathtbl_resize_lock); |
| 641 | for_each_mesh_entry(mesh_paths, p, node, i) { |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 642 | if (node->mpath->sdata != sdata) |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 643 | continue; |
| 644 | mpath = node->mpath; |
| 645 | spin_lock_bh(&mpath->state_lock); |
| 646 | if ((!(mpath->flags & MESH_PATH_RESOLVING)) && |
| 647 | (!(mpath->flags & MESH_PATH_FIXED)) && |
| 648 | time_after(jiffies, |
| 649 | mpath->exp_time + MESH_PATH_EXPIRE)) { |
| 650 | spin_unlock_bh(&mpath->state_lock); |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 651 | mesh_path_del(mpath->dst, mpath->sdata); |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 652 | } else |
| 653 | spin_unlock_bh(&mpath->state_lock); |
| 654 | } |
| 655 | read_unlock(&pathtbl_resize_lock); |
| 656 | } |
| 657 | |
| 658 | void mesh_pathtbl_unregister(void) |
| 659 | { |
| 660 | mesh_table_free(mesh_paths, true); |
YanBo | 79617de | 2008-09-22 13:30:32 +0800 | [diff] [blame] | 661 | mesh_table_free(mpp_paths, true); |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 662 | } |