Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 1 | /* |
Rui Paulo | 264d9b7 | 2009-11-09 23:46:58 +0000 | [diff] [blame] | 2 | * Copyright (c) 2008, 2009 open80211s Ltd. |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 13 | #include <linux/slab.h> |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 14 | #include <linux/spinlock.h> |
| 15 | #include <linux/string.h> |
| 16 | #include <net/mac80211.h> |
| 17 | #include "ieee80211_i.h" |
| 18 | #include "mesh.h" |
| 19 | |
Javier Cardona | 7646887 | 2011-08-09 16:45:04 -0700 | [diff] [blame^] | 20 | #ifdef CONFIG_MAC80211_VERBOSE_MPATH_DEBUG |
| 21 | #define mpath_dbg(fmt, args...) printk(KERN_DEBUG fmt, ##args) |
| 22 | #else |
| 23 | #define mpath_dbg(fmt, args...) do { (void)(0); } while (0) |
| 24 | #endif |
| 25 | |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 26 | /* There will be initially 2^INIT_PATHS_SIZE_ORDER buckets */ |
| 27 | #define INIT_PATHS_SIZE_ORDER 2 |
| 28 | |
| 29 | /* Keep the mean chain length below this constant */ |
| 30 | #define MEAN_CHAIN_LEN 2 |
| 31 | |
| 32 | #define MPATH_EXPIRED(mpath) ((mpath->flags & MESH_PATH_ACTIVE) && \ |
| 33 | time_after(jiffies, mpath->exp_time) && \ |
| 34 | !(mpath->flags & MESH_PATH_FIXED)) |
| 35 | |
| 36 | struct mpath_node { |
| 37 | struct hlist_node list; |
| 38 | struct rcu_head rcu; |
| 39 | /* This indirection allows two different tables to point to the same |
| 40 | * mesh_path structure, useful when resizing |
| 41 | */ |
| 42 | struct mesh_path *mpath; |
| 43 | }; |
| 44 | |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 45 | static struct mesh_table __rcu *mesh_paths; |
| 46 | static struct mesh_table __rcu *mpp_paths; /* Store paths for MPP&MAP */ |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 47 | |
Johannes Berg | f5ea912 | 2009-08-07 16:17:38 +0200 | [diff] [blame] | 48 | int mesh_paths_generation; |
Johannes Berg | 6b86bd6 | 2011-05-12 13:38:50 +0200 | [diff] [blame] | 49 | |
| 50 | /* This lock will have the grow table function as writer and add / delete nodes |
| 51 | * as readers. When reading the table (i.e. doing lookups) we are well protected |
| 52 | * by RCU |
| 53 | */ |
| 54 | static DEFINE_RWLOCK(pathtbl_resize_lock); |
| 55 | |
| 56 | |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 57 | static inline struct mesh_table *resize_dereference_mesh_paths(void) |
| 58 | { |
| 59 | return rcu_dereference_protected(mesh_paths, |
| 60 | lockdep_is_held(&pathtbl_resize_lock)); |
| 61 | } |
| 62 | |
| 63 | static inline struct mesh_table *resize_dereference_mpp_paths(void) |
| 64 | { |
| 65 | return rcu_dereference_protected(mpp_paths, |
| 66 | lockdep_is_held(&pathtbl_resize_lock)); |
| 67 | } |
| 68 | |
| 69 | /* |
| 70 | * CAREFUL -- "tbl" must not be an expression, |
| 71 | * in particular not an rcu_dereference(), since |
| 72 | * it's used twice. So it is illegal to do |
| 73 | * for_each_mesh_entry(rcu_dereference(...), ...) |
| 74 | */ |
| 75 | #define for_each_mesh_entry(tbl, p, node, i) \ |
| 76 | for (i = 0; i <= tbl->hash_mask; i++) \ |
| 77 | hlist_for_each_entry_rcu(node, p, &tbl->hash_buckets[i], list) |
| 78 | |
| 79 | |
Johannes Berg | 6b86bd6 | 2011-05-12 13:38:50 +0200 | [diff] [blame] | 80 | static struct mesh_table *mesh_table_alloc(int size_order) |
| 81 | { |
| 82 | int i; |
| 83 | struct mesh_table *newtbl; |
| 84 | |
Javier Cardona | d676ff4 | 2011-05-17 16:13:34 -0700 | [diff] [blame] | 85 | newtbl = kmalloc(sizeof(struct mesh_table), GFP_ATOMIC); |
Johannes Berg | 6b86bd6 | 2011-05-12 13:38:50 +0200 | [diff] [blame] | 86 | if (!newtbl) |
| 87 | return NULL; |
| 88 | |
| 89 | newtbl->hash_buckets = kzalloc(sizeof(struct hlist_head) * |
Javier Cardona | d676ff4 | 2011-05-17 16:13:34 -0700 | [diff] [blame] | 90 | (1 << size_order), GFP_ATOMIC); |
Johannes Berg | 6b86bd6 | 2011-05-12 13:38:50 +0200 | [diff] [blame] | 91 | |
| 92 | if (!newtbl->hash_buckets) { |
| 93 | kfree(newtbl); |
| 94 | return NULL; |
| 95 | } |
| 96 | |
| 97 | newtbl->hashwlock = kmalloc(sizeof(spinlock_t) * |
Javier Cardona | d676ff4 | 2011-05-17 16:13:34 -0700 | [diff] [blame] | 98 | (1 << size_order), GFP_ATOMIC); |
Johannes Berg | 6b86bd6 | 2011-05-12 13:38:50 +0200 | [diff] [blame] | 99 | if (!newtbl->hashwlock) { |
| 100 | kfree(newtbl->hash_buckets); |
| 101 | kfree(newtbl); |
| 102 | return NULL; |
| 103 | } |
| 104 | |
| 105 | newtbl->size_order = size_order; |
| 106 | newtbl->hash_mask = (1 << size_order) - 1; |
| 107 | atomic_set(&newtbl->entries, 0); |
| 108 | get_random_bytes(&newtbl->hash_rnd, |
| 109 | sizeof(newtbl->hash_rnd)); |
| 110 | for (i = 0; i <= newtbl->hash_mask; i++) |
| 111 | spin_lock_init(&newtbl->hashwlock[i]); |
| 112 | |
| 113 | return newtbl; |
| 114 | } |
| 115 | |
Javier Cardona | 1888923 | 2009-08-10 12:15:52 -0700 | [diff] [blame] | 116 | static void __mesh_table_free(struct mesh_table *tbl) |
| 117 | { |
| 118 | kfree(tbl->hash_buckets); |
| 119 | kfree(tbl->hashwlock); |
| 120 | kfree(tbl); |
| 121 | } |
| 122 | |
Johannes Berg | 6b86bd6 | 2011-05-12 13:38:50 +0200 | [diff] [blame] | 123 | static void mesh_table_free(struct mesh_table *tbl, bool free_leafs) |
Javier Cardona | 1888923 | 2009-08-10 12:15:52 -0700 | [diff] [blame] | 124 | { |
| 125 | struct hlist_head *mesh_hash; |
| 126 | struct hlist_node *p, *q; |
| 127 | int i; |
| 128 | |
| 129 | mesh_hash = tbl->hash_buckets; |
| 130 | for (i = 0; i <= tbl->hash_mask; i++) { |
Javier Cardona | 9b84b808 | 2011-05-03 16:57:16 -0700 | [diff] [blame] | 131 | spin_lock_bh(&tbl->hashwlock[i]); |
Javier Cardona | 1888923 | 2009-08-10 12:15:52 -0700 | [diff] [blame] | 132 | hlist_for_each_safe(p, q, &mesh_hash[i]) { |
| 133 | tbl->free_node(p, free_leafs); |
| 134 | atomic_dec(&tbl->entries); |
| 135 | } |
Javier Cardona | 9b84b808 | 2011-05-03 16:57:16 -0700 | [diff] [blame] | 136 | spin_unlock_bh(&tbl->hashwlock[i]); |
Javier Cardona | 1888923 | 2009-08-10 12:15:52 -0700 | [diff] [blame] | 137 | } |
| 138 | __mesh_table_free(tbl); |
| 139 | } |
| 140 | |
cozybit Inc | a3e6b12 | 2011-04-13 11:10:28 -0700 | [diff] [blame] | 141 | static int mesh_table_grow(struct mesh_table *oldtbl, |
Johannes Berg | 6b86bd6 | 2011-05-12 13:38:50 +0200 | [diff] [blame] | 142 | struct mesh_table *newtbl) |
Javier Cardona | 1888923 | 2009-08-10 12:15:52 -0700 | [diff] [blame] | 143 | { |
Javier Cardona | 1888923 | 2009-08-10 12:15:52 -0700 | [diff] [blame] | 144 | struct hlist_head *oldhash; |
| 145 | struct hlist_node *p, *q; |
| 146 | int i; |
| 147 | |
cozybit Inc | a3e6b12 | 2011-04-13 11:10:28 -0700 | [diff] [blame] | 148 | if (atomic_read(&oldtbl->entries) |
| 149 | < oldtbl->mean_chain_len * (oldtbl->hash_mask + 1)) |
| 150 | return -EAGAIN; |
Javier Cardona | 1888923 | 2009-08-10 12:15:52 -0700 | [diff] [blame] | 151 | |
cozybit Inc | a3e6b12 | 2011-04-13 11:10:28 -0700 | [diff] [blame] | 152 | newtbl->free_node = oldtbl->free_node; |
| 153 | newtbl->mean_chain_len = oldtbl->mean_chain_len; |
| 154 | newtbl->copy_node = oldtbl->copy_node; |
| 155 | atomic_set(&newtbl->entries, atomic_read(&oldtbl->entries)); |
Javier Cardona | 1888923 | 2009-08-10 12:15:52 -0700 | [diff] [blame] | 156 | |
cozybit Inc | a3e6b12 | 2011-04-13 11:10:28 -0700 | [diff] [blame] | 157 | oldhash = oldtbl->hash_buckets; |
| 158 | for (i = 0; i <= oldtbl->hash_mask; i++) |
Javier Cardona | 1888923 | 2009-08-10 12:15:52 -0700 | [diff] [blame] | 159 | hlist_for_each(p, &oldhash[i]) |
cozybit Inc | a3e6b12 | 2011-04-13 11:10:28 -0700 | [diff] [blame] | 160 | if (oldtbl->copy_node(p, newtbl) < 0) |
Javier Cardona | 1888923 | 2009-08-10 12:15:52 -0700 | [diff] [blame] | 161 | goto errcopy; |
| 162 | |
cozybit Inc | a3e6b12 | 2011-04-13 11:10:28 -0700 | [diff] [blame] | 163 | return 0; |
Javier Cardona | 1888923 | 2009-08-10 12:15:52 -0700 | [diff] [blame] | 164 | |
| 165 | errcopy: |
| 166 | for (i = 0; i <= newtbl->hash_mask; i++) { |
| 167 | hlist_for_each_safe(p, q, &newtbl->hash_buckets[i]) |
cozybit Inc | a3e6b12 | 2011-04-13 11:10:28 -0700 | [diff] [blame] | 168 | oldtbl->free_node(p, 0); |
Javier Cardona | 1888923 | 2009-08-10 12:15:52 -0700 | [diff] [blame] | 169 | } |
cozybit Inc | a3e6b12 | 2011-04-13 11:10:28 -0700 | [diff] [blame] | 170 | return -ENOMEM; |
Javier Cardona | 1888923 | 2009-08-10 12:15:52 -0700 | [diff] [blame] | 171 | } |
| 172 | |
Johannes Berg | 6b86bd6 | 2011-05-12 13:38:50 +0200 | [diff] [blame] | 173 | static u32 mesh_table_hash(u8 *addr, struct ieee80211_sub_if_data *sdata, |
| 174 | struct mesh_table *tbl) |
| 175 | { |
| 176 | /* Use last four bytes of hw addr and interface index as hash index */ |
| 177 | return jhash_2words(*(u32 *)(addr+2), sdata->dev->ifindex, tbl->hash_rnd) |
| 178 | & tbl->hash_mask; |
| 179 | } |
Johannes Berg | f5ea912 | 2009-08-07 16:17:38 +0200 | [diff] [blame] | 180 | |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 181 | |
| 182 | /** |
| 183 | * |
| 184 | * mesh_path_assign_nexthop - update mesh path next hop |
| 185 | * |
| 186 | * @mpath: mesh path to update |
| 187 | * @sta: next hop to assign |
| 188 | * |
| 189 | * Locking: mpath->state_lock must be held when calling this function |
| 190 | */ |
| 191 | void mesh_path_assign_nexthop(struct mesh_path *mpath, struct sta_info *sta) |
| 192 | { |
Javier Cardona | 10c836d | 2009-07-09 14:42:16 -0700 | [diff] [blame] | 193 | struct sk_buff *skb; |
| 194 | struct ieee80211_hdr *hdr; |
| 195 | struct sk_buff_head tmpq; |
| 196 | unsigned long flags; |
| 197 | |
Johannes Berg | d0709a6 | 2008-02-25 16:27:46 +0100 | [diff] [blame] | 198 | rcu_assign_pointer(mpath->next_hop, sta); |
Javier Cardona | 10c836d | 2009-07-09 14:42:16 -0700 | [diff] [blame] | 199 | |
| 200 | __skb_queue_head_init(&tmpq); |
| 201 | |
| 202 | spin_lock_irqsave(&mpath->frame_queue.lock, flags); |
| 203 | |
| 204 | while ((skb = __skb_dequeue(&mpath->frame_queue)) != NULL) { |
| 205 | hdr = (struct ieee80211_hdr *) skb->data; |
| 206 | memcpy(hdr->addr1, sta->sta.addr, ETH_ALEN); |
| 207 | __skb_queue_tail(&tmpq, skb); |
| 208 | } |
| 209 | |
| 210 | skb_queue_splice(&tmpq, &mpath->frame_queue); |
| 211 | spin_unlock_irqrestore(&mpath->frame_queue.lock, flags); |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | |
| 215 | /** |
| 216 | * mesh_path_lookup - look up a path in the mesh path table |
| 217 | * @dst: hardware address (ETH_ALEN length) of destination |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 218 | * @sdata: local subif |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 219 | * |
| 220 | * Returns: pointer to the mesh path structure, or NULL if not found |
| 221 | * |
| 222 | * Locking: must be called within a read rcu section. |
| 223 | */ |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 224 | 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] | 225 | { |
| 226 | struct mesh_path *mpath; |
| 227 | struct hlist_node *n; |
| 228 | struct hlist_head *bucket; |
| 229 | struct mesh_table *tbl; |
| 230 | struct mpath_node *node; |
| 231 | |
| 232 | tbl = rcu_dereference(mesh_paths); |
| 233 | |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 234 | bucket = &tbl->hash_buckets[mesh_table_hash(dst, sdata, tbl)]; |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 235 | hlist_for_each_entry_rcu(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 && |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 238 | memcmp(dst, mpath->dst, ETH_ALEN) == 0) { |
| 239 | if (MPATH_EXPIRED(mpath)) { |
| 240 | spin_lock_bh(&mpath->state_lock); |
| 241 | if (MPATH_EXPIRED(mpath)) |
| 242 | mpath->flags &= ~MESH_PATH_ACTIVE; |
| 243 | spin_unlock_bh(&mpath->state_lock); |
| 244 | } |
| 245 | return mpath; |
| 246 | } |
| 247 | } |
| 248 | return NULL; |
| 249 | } |
| 250 | |
YanBo | 79617de | 2008-09-22 13:30:32 +0800 | [diff] [blame] | 251 | struct mesh_path *mpp_path_lookup(u8 *dst, struct ieee80211_sub_if_data *sdata) |
| 252 | { |
| 253 | struct mesh_path *mpath; |
| 254 | struct hlist_node *n; |
| 255 | struct hlist_head *bucket; |
| 256 | struct mesh_table *tbl; |
| 257 | struct mpath_node *node; |
| 258 | |
| 259 | tbl = rcu_dereference(mpp_paths); |
| 260 | |
| 261 | bucket = &tbl->hash_buckets[mesh_table_hash(dst, sdata, tbl)]; |
| 262 | hlist_for_each_entry_rcu(node, n, bucket, list) { |
| 263 | mpath = node->mpath; |
| 264 | if (mpath->sdata == sdata && |
| 265 | memcmp(dst, mpath->dst, ETH_ALEN) == 0) { |
| 266 | if (MPATH_EXPIRED(mpath)) { |
| 267 | spin_lock_bh(&mpath->state_lock); |
| 268 | if (MPATH_EXPIRED(mpath)) |
| 269 | mpath->flags &= ~MESH_PATH_ACTIVE; |
| 270 | spin_unlock_bh(&mpath->state_lock); |
| 271 | } |
| 272 | return mpath; |
| 273 | } |
| 274 | } |
| 275 | return NULL; |
| 276 | } |
| 277 | |
| 278 | |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 279 | /** |
| 280 | * mesh_path_lookup_by_idx - look up a path in the mesh path table by its index |
| 281 | * @idx: index |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 282 | * @sdata: local subif, or NULL for all entries |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 283 | * |
| 284 | * Returns: pointer to the mesh path structure, or NULL if not found. |
| 285 | * |
| 286 | * Locking: must be called within a read rcu section. |
| 287 | */ |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 288 | 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] | 289 | { |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 290 | struct mesh_table *tbl = rcu_dereference(mesh_paths); |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 291 | struct mpath_node *node; |
| 292 | struct hlist_node *p; |
| 293 | int i; |
| 294 | int j = 0; |
| 295 | |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 296 | for_each_mesh_entry(tbl, p, node, i) { |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 297 | if (sdata && node->mpath->sdata != sdata) |
Luis Carlos Cobo | 2a8ca29 | 2008-02-29 17:51:25 -0800 | [diff] [blame] | 298 | continue; |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 299 | if (j++ == idx) { |
| 300 | if (MPATH_EXPIRED(node->mpath)) { |
| 301 | spin_lock_bh(&node->mpath->state_lock); |
| 302 | if (MPATH_EXPIRED(node->mpath)) |
| 303 | node->mpath->flags &= ~MESH_PATH_ACTIVE; |
| 304 | spin_unlock_bh(&node->mpath->state_lock); |
| 305 | } |
| 306 | return node->mpath; |
| 307 | } |
Luis Carlos Cobo | 2a8ca29 | 2008-02-29 17:51:25 -0800 | [diff] [blame] | 308 | } |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 309 | |
| 310 | return NULL; |
| 311 | } |
| 312 | |
| 313 | /** |
| 314 | * mesh_path_add - allocate and add a new path to the mesh path table |
| 315 | * @addr: destination address of the path (ETH_ALEN length) |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 316 | * @sdata: local subif |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 317 | * |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 318 | * Returns: 0 on success |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 319 | * |
| 320 | * State: the initial state of the new path is set to 0 |
| 321 | */ |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 322 | 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] | 323 | { |
Javier Cardona | 1888923 | 2009-08-10 12:15:52 -0700 | [diff] [blame] | 324 | struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh; |
| 325 | struct ieee80211_local *local = sdata->local; |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 326 | struct mesh_table *tbl; |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 327 | struct mesh_path *mpath, *new_mpath; |
| 328 | struct mpath_node *node, *new_node; |
| 329 | struct hlist_head *bucket; |
| 330 | struct hlist_node *n; |
| 331 | int grow = 0; |
| 332 | int err = 0; |
| 333 | u32 hash_idx; |
| 334 | |
Johannes Berg | 47846c9 | 2009-11-25 17:46:19 +0100 | [diff] [blame] | 335 | if (memcmp(dst, sdata->vif.addr, ETH_ALEN) == 0) |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 336 | /* never add ourselves as neighbours */ |
| 337 | return -ENOTSUPP; |
| 338 | |
| 339 | if (is_multicast_ether_addr(dst)) |
| 340 | return -ENOTSUPP; |
| 341 | |
Johannes Berg | 472dbc4 | 2008-09-11 00:01:49 +0200 | [diff] [blame] | 342 | 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] | 343 | return -ENOSPC; |
| 344 | |
Pavel Emelyanov | 402d775 | 2008-05-06 18:53:43 +0400 | [diff] [blame] | 345 | err = -ENOMEM; |
Javier Cardona | 1888923 | 2009-08-10 12:15:52 -0700 | [diff] [blame] | 346 | new_mpath = kzalloc(sizeof(struct mesh_path), GFP_ATOMIC); |
Pavel Emelyanov | 402d775 | 2008-05-06 18:53:43 +0400 | [diff] [blame] | 347 | if (!new_mpath) |
| 348 | goto err_path_alloc; |
| 349 | |
Javier Cardona | 1888923 | 2009-08-10 12:15:52 -0700 | [diff] [blame] | 350 | new_node = kmalloc(sizeof(struct mpath_node), GFP_ATOMIC); |
Pavel Emelyanov | 402d775 | 2008-05-06 18:53:43 +0400 | [diff] [blame] | 351 | if (!new_node) |
| 352 | goto err_node_alloc; |
Pavel Emelyanov | f84e71a | 2008-05-06 18:46:36 +0400 | [diff] [blame] | 353 | |
Javier Cardona | 9b84b808 | 2011-05-03 16:57:16 -0700 | [diff] [blame] | 354 | read_lock_bh(&pathtbl_resize_lock); |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 355 | memcpy(new_mpath->dst, dst, ETH_ALEN); |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 356 | new_mpath->sdata = sdata; |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 357 | new_mpath->flags = 0; |
| 358 | skb_queue_head_init(&new_mpath->frame_queue); |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 359 | new_node->mpath = new_mpath; |
| 360 | new_mpath->timer.data = (unsigned long) new_mpath; |
| 361 | new_mpath->timer.function = mesh_path_timer; |
| 362 | new_mpath->exp_time = jiffies; |
| 363 | spin_lock_init(&new_mpath->state_lock); |
| 364 | init_timer(&new_mpath->timer); |
| 365 | |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 366 | tbl = resize_dereference_mesh_paths(); |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 367 | |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 368 | hash_idx = mesh_table_hash(dst, sdata, tbl); |
| 369 | bucket = &tbl->hash_buckets[hash_idx]; |
| 370 | |
| 371 | spin_lock_bh(&tbl->hashwlock[hash_idx]); |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 372 | |
Pavel Emelyanov | 402d775 | 2008-05-06 18:53:43 +0400 | [diff] [blame] | 373 | err = -EEXIST; |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 374 | hlist_for_each_entry(node, n, bucket, list) { |
| 375 | mpath = node->mpath; |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 376 | if (mpath->sdata == sdata && memcmp(dst, mpath->dst, ETH_ALEN) == 0) |
Pavel Emelyanov | 402d775 | 2008-05-06 18:53:43 +0400 | [diff] [blame] | 377 | goto err_exists; |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | hlist_add_head_rcu(&new_node->list, bucket); |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 381 | if (atomic_inc_return(&tbl->entries) >= |
| 382 | tbl->mean_chain_len * (tbl->hash_mask + 1)) |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 383 | grow = 1; |
| 384 | |
Johannes Berg | f5ea912 | 2009-08-07 16:17:38 +0200 | [diff] [blame] | 385 | mesh_paths_generation++; |
| 386 | |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 387 | spin_unlock_bh(&tbl->hashwlock[hash_idx]); |
Javier Cardona | 9b84b808 | 2011-05-03 16:57:16 -0700 | [diff] [blame] | 388 | read_unlock_bh(&pathtbl_resize_lock); |
Pavel Emelyanov | 402d775 | 2008-05-06 18:53:43 +0400 | [diff] [blame] | 389 | if (grow) { |
Javier Cardona | 1888923 | 2009-08-10 12:15:52 -0700 | [diff] [blame] | 390 | set_bit(MESH_WORK_GROW_MPATH_TABLE, &ifmsh->wrkq_flags); |
Johannes Berg | 64592c8 | 2010-06-10 10:21:31 +0200 | [diff] [blame] | 391 | ieee80211_queue_work(&local->hw, &sdata->work); |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 392 | } |
Pavel Emelyanov | 402d775 | 2008-05-06 18:53:43 +0400 | [diff] [blame] | 393 | return 0; |
| 394 | |
| 395 | err_exists: |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 396 | spin_unlock_bh(&tbl->hashwlock[hash_idx]); |
Javier Cardona | 9b84b808 | 2011-05-03 16:57:16 -0700 | [diff] [blame] | 397 | read_unlock_bh(&pathtbl_resize_lock); |
Pavel Emelyanov | 402d775 | 2008-05-06 18:53:43 +0400 | [diff] [blame] | 398 | kfree(new_node); |
| 399 | err_node_alloc: |
| 400 | kfree(new_mpath); |
| 401 | err_path_alloc: |
Johannes Berg | 472dbc4 | 2008-09-11 00:01:49 +0200 | [diff] [blame] | 402 | atomic_dec(&sdata->u.mesh.mpaths); |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 403 | return err; |
| 404 | } |
| 405 | |
Johannes Berg | 1928eca | 2011-05-14 11:00:52 +0200 | [diff] [blame] | 406 | static void mesh_table_free_rcu(struct rcu_head *rcu) |
| 407 | { |
| 408 | struct mesh_table *tbl = container_of(rcu, struct mesh_table, rcu_head); |
| 409 | |
| 410 | mesh_table_free(tbl, false); |
| 411 | } |
| 412 | |
Javier Cardona | 1888923 | 2009-08-10 12:15:52 -0700 | [diff] [blame] | 413 | void mesh_mpath_table_grow(void) |
| 414 | { |
| 415 | struct mesh_table *oldtbl, *newtbl; |
| 416 | |
Javier Cardona | 9b84b808 | 2011-05-03 16:57:16 -0700 | [diff] [blame] | 417 | write_lock_bh(&pathtbl_resize_lock); |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 418 | oldtbl = resize_dereference_mesh_paths(); |
| 419 | newtbl = mesh_table_alloc(oldtbl->size_order + 1); |
Johannes Berg | 1928eca | 2011-05-14 11:00:52 +0200 | [diff] [blame] | 420 | if (!newtbl) |
| 421 | goto out; |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 422 | if (mesh_table_grow(oldtbl, newtbl) < 0) { |
cozybit Inc | a3e6b12 | 2011-04-13 11:10:28 -0700 | [diff] [blame] | 423 | __mesh_table_free(newtbl); |
Johannes Berg | 1928eca | 2011-05-14 11:00:52 +0200 | [diff] [blame] | 424 | goto out; |
Javier Cardona | 1888923 | 2009-08-10 12:15:52 -0700 | [diff] [blame] | 425 | } |
| 426 | rcu_assign_pointer(mesh_paths, newtbl); |
Javier Cardona | 1888923 | 2009-08-10 12:15:52 -0700 | [diff] [blame] | 427 | |
Johannes Berg | 1928eca | 2011-05-14 11:00:52 +0200 | [diff] [blame] | 428 | call_rcu(&oldtbl->rcu_head, mesh_table_free_rcu); |
| 429 | |
| 430 | out: |
| 431 | write_unlock_bh(&pathtbl_resize_lock); |
Javier Cardona | 1888923 | 2009-08-10 12:15:52 -0700 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | void mesh_mpp_table_grow(void) |
| 435 | { |
| 436 | struct mesh_table *oldtbl, *newtbl; |
| 437 | |
Javier Cardona | 9b84b808 | 2011-05-03 16:57:16 -0700 | [diff] [blame] | 438 | write_lock_bh(&pathtbl_resize_lock); |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 439 | oldtbl = resize_dereference_mpp_paths(); |
| 440 | newtbl = mesh_table_alloc(oldtbl->size_order + 1); |
Johannes Berg | 1928eca | 2011-05-14 11:00:52 +0200 | [diff] [blame] | 441 | if (!newtbl) |
| 442 | goto out; |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 443 | if (mesh_table_grow(oldtbl, newtbl) < 0) { |
cozybit Inc | a3e6b12 | 2011-04-13 11:10:28 -0700 | [diff] [blame] | 444 | __mesh_table_free(newtbl); |
Johannes Berg | 1928eca | 2011-05-14 11:00:52 +0200 | [diff] [blame] | 445 | goto out; |
Javier Cardona | 1888923 | 2009-08-10 12:15:52 -0700 | [diff] [blame] | 446 | } |
| 447 | rcu_assign_pointer(mpp_paths, newtbl); |
Johannes Berg | 1928eca | 2011-05-14 11:00:52 +0200 | [diff] [blame] | 448 | call_rcu(&oldtbl->rcu_head, mesh_table_free_rcu); |
Javier Cardona | 1888923 | 2009-08-10 12:15:52 -0700 | [diff] [blame] | 449 | |
Johannes Berg | 1928eca | 2011-05-14 11:00:52 +0200 | [diff] [blame] | 450 | out: |
| 451 | write_unlock_bh(&pathtbl_resize_lock); |
Javier Cardona | 1888923 | 2009-08-10 12:15:52 -0700 | [diff] [blame] | 452 | } |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 453 | |
YanBo | 79617de | 2008-09-22 13:30:32 +0800 | [diff] [blame] | 454 | int mpp_path_add(u8 *dst, u8 *mpp, struct ieee80211_sub_if_data *sdata) |
| 455 | { |
Javier Cardona | 1888923 | 2009-08-10 12:15:52 -0700 | [diff] [blame] | 456 | struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh; |
| 457 | struct ieee80211_local *local = sdata->local; |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 458 | struct mesh_table *tbl; |
YanBo | 79617de | 2008-09-22 13:30:32 +0800 | [diff] [blame] | 459 | struct mesh_path *mpath, *new_mpath; |
| 460 | struct mpath_node *node, *new_node; |
| 461 | struct hlist_head *bucket; |
| 462 | struct hlist_node *n; |
| 463 | int grow = 0; |
| 464 | int err = 0; |
| 465 | u32 hash_idx; |
| 466 | |
Johannes Berg | 47846c9 | 2009-11-25 17:46:19 +0100 | [diff] [blame] | 467 | if (memcmp(dst, sdata->vif.addr, ETH_ALEN) == 0) |
YanBo | 79617de | 2008-09-22 13:30:32 +0800 | [diff] [blame] | 468 | /* never add ourselves as neighbours */ |
| 469 | return -ENOTSUPP; |
| 470 | |
| 471 | if (is_multicast_ether_addr(dst)) |
| 472 | return -ENOTSUPP; |
| 473 | |
| 474 | err = -ENOMEM; |
Javier Cardona | 1888923 | 2009-08-10 12:15:52 -0700 | [diff] [blame] | 475 | new_mpath = kzalloc(sizeof(struct mesh_path), GFP_ATOMIC); |
YanBo | 79617de | 2008-09-22 13:30:32 +0800 | [diff] [blame] | 476 | if (!new_mpath) |
| 477 | goto err_path_alloc; |
| 478 | |
Javier Cardona | 1888923 | 2009-08-10 12:15:52 -0700 | [diff] [blame] | 479 | new_node = kmalloc(sizeof(struct mpath_node), GFP_ATOMIC); |
YanBo | 79617de | 2008-09-22 13:30:32 +0800 | [diff] [blame] | 480 | if (!new_node) |
| 481 | goto err_node_alloc; |
| 482 | |
Javier Cardona | 9b84b808 | 2011-05-03 16:57:16 -0700 | [diff] [blame] | 483 | read_lock_bh(&pathtbl_resize_lock); |
YanBo | 79617de | 2008-09-22 13:30:32 +0800 | [diff] [blame] | 484 | memcpy(new_mpath->dst, dst, ETH_ALEN); |
| 485 | memcpy(new_mpath->mpp, mpp, ETH_ALEN); |
| 486 | new_mpath->sdata = sdata; |
| 487 | new_mpath->flags = 0; |
| 488 | skb_queue_head_init(&new_mpath->frame_queue); |
| 489 | new_node->mpath = new_mpath; |
| 490 | new_mpath->exp_time = jiffies; |
| 491 | spin_lock_init(&new_mpath->state_lock); |
| 492 | |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 493 | tbl = resize_dereference_mpp_paths(); |
YanBo | 79617de | 2008-09-22 13:30:32 +0800 | [diff] [blame] | 494 | |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 495 | hash_idx = mesh_table_hash(dst, sdata, tbl); |
| 496 | bucket = &tbl->hash_buckets[hash_idx]; |
| 497 | |
| 498 | spin_lock_bh(&tbl->hashwlock[hash_idx]); |
YanBo | 79617de | 2008-09-22 13:30:32 +0800 | [diff] [blame] | 499 | |
| 500 | err = -EEXIST; |
| 501 | hlist_for_each_entry(node, n, bucket, list) { |
| 502 | mpath = node->mpath; |
| 503 | if (mpath->sdata == sdata && memcmp(dst, mpath->dst, ETH_ALEN) == 0) |
| 504 | goto err_exists; |
| 505 | } |
| 506 | |
| 507 | hlist_add_head_rcu(&new_node->list, bucket); |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 508 | if (atomic_inc_return(&tbl->entries) >= |
| 509 | tbl->mean_chain_len * (tbl->hash_mask + 1)) |
YanBo | 79617de | 2008-09-22 13:30:32 +0800 | [diff] [blame] | 510 | grow = 1; |
| 511 | |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 512 | spin_unlock_bh(&tbl->hashwlock[hash_idx]); |
Javier Cardona | 9b84b808 | 2011-05-03 16:57:16 -0700 | [diff] [blame] | 513 | read_unlock_bh(&pathtbl_resize_lock); |
YanBo | 79617de | 2008-09-22 13:30:32 +0800 | [diff] [blame] | 514 | if (grow) { |
Javier Cardona | 1888923 | 2009-08-10 12:15:52 -0700 | [diff] [blame] | 515 | set_bit(MESH_WORK_GROW_MPP_TABLE, &ifmsh->wrkq_flags); |
Johannes Berg | 64592c8 | 2010-06-10 10:21:31 +0200 | [diff] [blame] | 516 | ieee80211_queue_work(&local->hw, &sdata->work); |
YanBo | 79617de | 2008-09-22 13:30:32 +0800 | [diff] [blame] | 517 | } |
| 518 | return 0; |
| 519 | |
| 520 | err_exists: |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 521 | spin_unlock_bh(&tbl->hashwlock[hash_idx]); |
Javier Cardona | 9b84b808 | 2011-05-03 16:57:16 -0700 | [diff] [blame] | 522 | read_unlock_bh(&pathtbl_resize_lock); |
YanBo | 79617de | 2008-09-22 13:30:32 +0800 | [diff] [blame] | 523 | kfree(new_node); |
| 524 | err_node_alloc: |
| 525 | kfree(new_mpath); |
| 526 | err_path_alloc: |
| 527 | return err; |
| 528 | } |
| 529 | |
| 530 | |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 531 | /** |
| 532 | * mesh_plink_broken - deactivates paths and sends perr when a link breaks |
| 533 | * |
| 534 | * @sta: broken peer link |
| 535 | * |
| 536 | * This function must be called from the rate control algorithm if enough |
| 537 | * delivery errors suggest that a peer link is no longer usable. |
| 538 | */ |
| 539 | void mesh_plink_broken(struct sta_info *sta) |
| 540 | { |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 541 | struct mesh_table *tbl; |
Johannes Berg | 15ff636 | 2009-11-17 13:34:04 +0100 | [diff] [blame] | 542 | static const u8 bcast[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 543 | struct mesh_path *mpath; |
| 544 | struct mpath_node *node; |
| 545 | struct hlist_node *p; |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 546 | struct ieee80211_sub_if_data *sdata = sta->sdata; |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 547 | int i; |
Thomas Pedersen | 25d49e4 | 2011-08-11 19:35:15 -0700 | [diff] [blame] | 548 | __le16 reason = cpu_to_le16(WLAN_REASON_MESH_PATH_DEST_UNREACHABLE); |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 549 | |
| 550 | rcu_read_lock(); |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 551 | tbl = rcu_dereference(mesh_paths); |
| 552 | for_each_mesh_entry(tbl, p, node, i) { |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 553 | mpath = node->mpath; |
| 554 | spin_lock_bh(&mpath->state_lock); |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 555 | if (rcu_dereference(mpath->next_hop) == sta && |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 556 | mpath->flags & MESH_PATH_ACTIVE && |
| 557 | !(mpath->flags & MESH_PATH_FIXED)) { |
| 558 | mpath->flags &= ~MESH_PATH_ACTIVE; |
Rui Paulo | d19b3bf | 2009-11-09 23:46:55 +0000 | [diff] [blame] | 559 | ++mpath->sn; |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 560 | spin_unlock_bh(&mpath->state_lock); |
Javier Cardona | 45904f2 | 2010-12-03 09:20:40 +0100 | [diff] [blame] | 561 | mesh_path_error_tx(sdata->u.mesh.mshcfg.element_ttl, |
| 562 | mpath->dst, cpu_to_le32(mpath->sn), |
Thomas Pedersen | 25d49e4 | 2011-08-11 19:35:15 -0700 | [diff] [blame] | 563 | reason, bcast, sdata); |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 564 | } else |
| 565 | spin_unlock_bh(&mpath->state_lock); |
| 566 | } |
| 567 | rcu_read_unlock(); |
| 568 | } |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 569 | |
| 570 | /** |
| 571 | * mesh_path_flush_by_nexthop - Deletes mesh paths if their next hop matches |
| 572 | * |
| 573 | * @sta - mesh peer to match |
| 574 | * |
Luis Carlos Cobo | b4e08ea | 2008-02-29 15:46:08 -0800 | [diff] [blame] | 575 | * RCU notes: this function is called when a mesh plink transitions from |
| 576 | * PLINK_ESTAB to any other state, since PLINK_ESTAB state is the only one that |
| 577 | * 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] | 578 | * sta_info_destroy() calls this) so any reader in a rcu read block will be |
| 579 | * protected against the plink disappearing. |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 580 | */ |
| 581 | void mesh_path_flush_by_nexthop(struct sta_info *sta) |
| 582 | { |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 583 | struct mesh_table *tbl; |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 584 | struct mesh_path *mpath; |
| 585 | struct mpath_node *node; |
| 586 | struct hlist_node *p; |
| 587 | int i; |
| 588 | |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 589 | rcu_read_lock(); |
| 590 | tbl = rcu_dereference(mesh_paths); |
| 591 | for_each_mesh_entry(tbl, p, node, i) { |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 592 | mpath = node->mpath; |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 593 | if (rcu_dereference(mpath->next_hop) == sta) |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 594 | mesh_path_del(mpath->dst, mpath->sdata); |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 595 | } |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 596 | rcu_read_unlock(); |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 597 | } |
| 598 | |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 599 | void mesh_path_flush(struct ieee80211_sub_if_data *sdata) |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 600 | { |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 601 | struct mesh_table *tbl; |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 602 | struct mesh_path *mpath; |
| 603 | struct mpath_node *node; |
| 604 | struct hlist_node *p; |
| 605 | int i; |
| 606 | |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 607 | rcu_read_lock(); |
| 608 | tbl = rcu_dereference(mesh_paths); |
| 609 | for_each_mesh_entry(tbl, p, node, i) { |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 610 | mpath = node->mpath; |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 611 | if (mpath->sdata == sdata) |
| 612 | mesh_path_del(mpath->dst, mpath->sdata); |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 613 | } |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 614 | rcu_read_unlock(); |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | static void mesh_path_node_reclaim(struct rcu_head *rp) |
| 618 | { |
| 619 | struct mpath_node *node = container_of(rp, struct mpath_node, rcu); |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 620 | struct ieee80211_sub_if_data *sdata = node->mpath->sdata; |
Johannes Berg | d0709a6 | 2008-02-25 16:27:46 +0100 | [diff] [blame] | 621 | |
Luis Carlos Cobo | 89a1ad6 | 2008-02-29 14:49:37 -0800 | [diff] [blame] | 622 | del_timer_sync(&node->mpath->timer); |
Johannes Berg | 472dbc4 | 2008-09-11 00:01:49 +0200 | [diff] [blame] | 623 | atomic_dec(&sdata->u.mesh.mpaths); |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 624 | kfree(node->mpath); |
| 625 | kfree(node); |
| 626 | } |
| 627 | |
| 628 | /** |
| 629 | * mesh_path_del - delete a mesh path from the table |
| 630 | * |
| 631 | * @addr: dst address (ETH_ALEN length) |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 632 | * @sdata: local subif |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 633 | * |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 634 | * Returns: 0 if successful |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 635 | */ |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 636 | 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] | 637 | { |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 638 | struct mesh_table *tbl; |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 639 | struct mesh_path *mpath; |
| 640 | struct mpath_node *node; |
| 641 | struct hlist_head *bucket; |
| 642 | struct hlist_node *n; |
| 643 | int hash_idx; |
| 644 | int err = 0; |
| 645 | |
Javier Cardona | 9b84b808 | 2011-05-03 16:57:16 -0700 | [diff] [blame] | 646 | read_lock_bh(&pathtbl_resize_lock); |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 647 | tbl = resize_dereference_mesh_paths(); |
| 648 | hash_idx = mesh_table_hash(addr, sdata, tbl); |
| 649 | bucket = &tbl->hash_buckets[hash_idx]; |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 650 | |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 651 | spin_lock_bh(&tbl->hashwlock[hash_idx]); |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 652 | hlist_for_each_entry(node, n, bucket, list) { |
| 653 | mpath = node->mpath; |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 654 | if (mpath->sdata == sdata && |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 655 | memcmp(addr, mpath->dst, ETH_ALEN) == 0) { |
Christian Lamparter | 5e34069 | 2011-06-30 21:08:43 +0200 | [diff] [blame] | 656 | spin_lock(&mpath->state_lock); |
Luis Carlos Cobo | cfa22c7 | 2008-02-29 15:04:13 -0800 | [diff] [blame] | 657 | mpath->flags |= MESH_PATH_RESOLVING; |
| 658 | hlist_del_rcu(&node->list); |
| 659 | call_rcu(&node->rcu, mesh_path_node_reclaim); |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 660 | atomic_dec(&tbl->entries); |
Christian Lamparter | 5e34069 | 2011-06-30 21:08:43 +0200 | [diff] [blame] | 661 | spin_unlock(&mpath->state_lock); |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 662 | goto enddel; |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | err = -ENXIO; |
| 667 | enddel: |
Johannes Berg | f5ea912 | 2009-08-07 16:17:38 +0200 | [diff] [blame] | 668 | mesh_paths_generation++; |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 669 | spin_unlock_bh(&tbl->hashwlock[hash_idx]); |
Javier Cardona | 9b84b808 | 2011-05-03 16:57:16 -0700 | [diff] [blame] | 670 | read_unlock_bh(&pathtbl_resize_lock); |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 671 | return err; |
| 672 | } |
| 673 | |
| 674 | /** |
| 675 | * mesh_path_tx_pending - sends pending frames in a mesh path queue |
| 676 | * |
| 677 | * @mpath: mesh path to activate |
| 678 | * |
| 679 | * Locking: the state_lock of the mpath structure must NOT be held when calling |
| 680 | * this function. |
| 681 | */ |
| 682 | void mesh_path_tx_pending(struct mesh_path *mpath) |
| 683 | { |
Javier Cardona | 249b405 | 2009-07-07 10:55:03 -0700 | [diff] [blame] | 684 | if (mpath->flags & MESH_PATH_ACTIVE) |
| 685 | ieee80211_add_pending_skbs(mpath->sdata->local, |
| 686 | &mpath->frame_queue); |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | /** |
| 690 | * mesh_path_discard_frame - discard a frame whose path could not be resolved |
| 691 | * |
| 692 | * @skb: frame to discard |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 693 | * @sdata: network subif the frame was to be sent through |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 694 | * |
Javier Cardona | 35946a5 | 2009-07-13 17:00:10 -0700 | [diff] [blame] | 695 | * If the frame was being forwarded from another MP, a PERR frame will be sent |
| 696 | * to the precursor. The precursor's address (i.e. the previous hop) was saved |
| 697 | * in addr1 of the frame-to-be-forwarded, and would only be overwritten once |
| 698 | * the destination is successfully resolved. |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 699 | * |
| 700 | * Locking: the function must me called within a rcu_read_lock region |
| 701 | */ |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 702 | void mesh_path_discard_frame(struct sk_buff *skb, |
| 703 | struct ieee80211_sub_if_data *sdata) |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 704 | { |
Luis Carlos Cobo | e32f85f | 2008-08-05 19:34:52 +0200 | [diff] [blame] | 705 | struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data; |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 706 | struct mesh_path *mpath; |
Rui Paulo | d19b3bf | 2009-11-09 23:46:55 +0000 | [diff] [blame] | 707 | u32 sn = 0; |
Thomas Pedersen | 25d49e4 | 2011-08-11 19:35:15 -0700 | [diff] [blame] | 708 | __le16 reason = cpu_to_le16(WLAN_REASON_MESH_PATH_NOFORWARD); |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 709 | |
Johannes Berg | 47846c9 | 2009-11-25 17:46:19 +0100 | [diff] [blame] | 710 | if (memcmp(hdr->addr4, sdata->vif.addr, ETH_ALEN) != 0) { |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 711 | u8 *ra, *da; |
| 712 | |
Luis Carlos Cobo | e32f85f | 2008-08-05 19:34:52 +0200 | [diff] [blame] | 713 | da = hdr->addr3; |
Javier Cardona | 35946a5 | 2009-07-13 17:00:10 -0700 | [diff] [blame] | 714 | ra = hdr->addr1; |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 715 | mpath = mesh_path_lookup(da, sdata); |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 716 | if (mpath) |
Rui Paulo | d19b3bf | 2009-11-09 23:46:55 +0000 | [diff] [blame] | 717 | sn = ++mpath->sn; |
Javier Cardona | 45904f2 | 2010-12-03 09:20:40 +0100 | [diff] [blame] | 718 | mesh_path_error_tx(sdata->u.mesh.mshcfg.element_ttl, skb->data, |
Thomas Pedersen | 25d49e4 | 2011-08-11 19:35:15 -0700 | [diff] [blame] | 719 | cpu_to_le32(sn), reason, ra, sdata); |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | kfree_skb(skb); |
Johannes Berg | 472dbc4 | 2008-09-11 00:01:49 +0200 | [diff] [blame] | 723 | sdata->u.mesh.mshstats.dropped_frames_no_route++; |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | /** |
| 727 | * mesh_path_flush_pending - free the pending queue of a mesh path |
| 728 | * |
| 729 | * @mpath: mesh path whose queue has to be freed |
| 730 | * |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 731 | * Locking: the function must me called within a rcu_read_lock region |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 732 | */ |
| 733 | void mesh_path_flush_pending(struct mesh_path *mpath) |
| 734 | { |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 735 | struct sk_buff *skb; |
| 736 | |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 737 | while ((skb = skb_dequeue(&mpath->frame_queue)) && |
| 738 | (mpath->flags & MESH_PATH_ACTIVE)) |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 739 | mesh_path_discard_frame(skb, mpath->sdata); |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 740 | } |
| 741 | |
| 742 | /** |
| 743 | * mesh_path_fix_nexthop - force a specific next hop for a mesh path |
| 744 | * |
| 745 | * @mpath: the mesh path to modify |
| 746 | * @next_hop: the next hop to force |
| 747 | * |
| 748 | * Locking: this function must be called holding mpath->state_lock |
| 749 | */ |
| 750 | void mesh_path_fix_nexthop(struct mesh_path *mpath, struct sta_info *next_hop) |
| 751 | { |
| 752 | spin_lock_bh(&mpath->state_lock); |
| 753 | mesh_path_assign_nexthop(mpath, next_hop); |
Rui Paulo | d19b3bf | 2009-11-09 23:46:55 +0000 | [diff] [blame] | 754 | mpath->sn = 0xffff; |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 755 | mpath->metric = 0; |
| 756 | mpath->hop_count = 0; |
| 757 | mpath->exp_time = 0; |
| 758 | mpath->flags |= MESH_PATH_FIXED; |
| 759 | mesh_path_activate(mpath); |
| 760 | spin_unlock_bh(&mpath->state_lock); |
| 761 | mesh_path_tx_pending(mpath); |
| 762 | } |
| 763 | |
| 764 | static void mesh_path_node_free(struct hlist_node *p, bool free_leafs) |
| 765 | { |
| 766 | struct mesh_path *mpath; |
| 767 | struct mpath_node *node = hlist_entry(p, struct mpath_node, list); |
| 768 | mpath = node->mpath; |
| 769 | hlist_del_rcu(p); |
Javier Cardona | d0df9ee | 2011-05-13 14:16:07 -0700 | [diff] [blame] | 770 | if (free_leafs) { |
| 771 | del_timer_sync(&mpath->timer); |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 772 | kfree(mpath); |
Javier Cardona | d0df9ee | 2011-05-13 14:16:07 -0700 | [diff] [blame] | 773 | } |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 774 | kfree(node); |
| 775 | } |
| 776 | |
Pavel Emelyanov | 4caf86c | 2008-05-07 19:47:01 +0400 | [diff] [blame] | 777 | 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] | 778 | { |
| 779 | struct mesh_path *mpath; |
| 780 | struct mpath_node *node, *new_node; |
| 781 | u32 hash_idx; |
| 782 | |
Pavel Emelyanov | 8566dc3 | 2008-05-07 19:51:51 +0400 | [diff] [blame] | 783 | new_node = kmalloc(sizeof(struct mpath_node), GFP_ATOMIC); |
Pavel Emelyanov | 00242c4 | 2008-05-07 19:48:14 +0400 | [diff] [blame] | 784 | if (new_node == NULL) |
| 785 | return -ENOMEM; |
| 786 | |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 787 | node = hlist_entry(p, struct mpath_node, list); |
| 788 | mpath = node->mpath; |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 789 | new_node->mpath = mpath; |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 790 | hash_idx = mesh_table_hash(mpath->dst, mpath->sdata, newtbl); |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 791 | hlist_add_head(&new_node->list, |
| 792 | &newtbl->hash_buckets[hash_idx]); |
Pavel Emelyanov | 4caf86c | 2008-05-07 19:47:01 +0400 | [diff] [blame] | 793 | return 0; |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 794 | } |
| 795 | |
| 796 | int mesh_pathtbl_init(void) |
| 797 | { |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 798 | struct mesh_table *tbl_path, *tbl_mpp; |
YanBo | 79617de | 2008-09-22 13:30:32 +0800 | [diff] [blame] | 799 | |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 800 | tbl_path = mesh_table_alloc(INIT_PATHS_SIZE_ORDER); |
| 801 | if (!tbl_path) |
| 802 | return -ENOMEM; |
| 803 | tbl_path->free_node = &mesh_path_node_free; |
| 804 | tbl_path->copy_node = &mesh_path_node_copy; |
| 805 | tbl_path->mean_chain_len = MEAN_CHAIN_LEN; |
| 806 | |
| 807 | tbl_mpp = mesh_table_alloc(INIT_PATHS_SIZE_ORDER); |
| 808 | if (!tbl_mpp) { |
| 809 | mesh_table_free(tbl_path, true); |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 810 | return -ENOMEM; |
YanBo | 79617de | 2008-09-22 13:30:32 +0800 | [diff] [blame] | 811 | } |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 812 | tbl_mpp->free_node = &mesh_path_node_free; |
| 813 | tbl_mpp->copy_node = &mesh_path_node_copy; |
| 814 | tbl_mpp->mean_chain_len = MEAN_CHAIN_LEN; |
| 815 | |
| 816 | /* Need no locking since this is during init */ |
| 817 | RCU_INIT_POINTER(mesh_paths, tbl_path); |
| 818 | RCU_INIT_POINTER(mpp_paths, tbl_mpp); |
YanBo | 79617de | 2008-09-22 13:30:32 +0800 | [diff] [blame] | 819 | |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 820 | return 0; |
| 821 | } |
| 822 | |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 823 | void mesh_path_expire(struct ieee80211_sub_if_data *sdata) |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 824 | { |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 825 | struct mesh_table *tbl; |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 826 | struct mesh_path *mpath; |
| 827 | struct mpath_node *node; |
| 828 | struct hlist_node *p; |
| 829 | int i; |
| 830 | |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 831 | rcu_read_lock(); |
| 832 | tbl = rcu_dereference(mesh_paths); |
| 833 | for_each_mesh_entry(tbl, p, node, i) { |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 834 | if (node->mpath->sdata != sdata) |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 835 | continue; |
| 836 | mpath = node->mpath; |
| 837 | spin_lock_bh(&mpath->state_lock); |
| 838 | if ((!(mpath->flags & MESH_PATH_RESOLVING)) && |
| 839 | (!(mpath->flags & MESH_PATH_FIXED)) && |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 840 | time_after(jiffies, mpath->exp_time + MESH_PATH_EXPIRE)) { |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 841 | spin_unlock_bh(&mpath->state_lock); |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 842 | mesh_path_del(mpath->dst, mpath->sdata); |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 843 | } else |
| 844 | spin_unlock_bh(&mpath->state_lock); |
| 845 | } |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 846 | rcu_read_unlock(); |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 847 | } |
| 848 | |
| 849 | void mesh_pathtbl_unregister(void) |
| 850 | { |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 851 | /* no need for locking during exit path */ |
| 852 | mesh_table_free(rcu_dereference_raw(mesh_paths), true); |
| 853 | mesh_table_free(rcu_dereference_raw(mpp_paths), true); |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 854 | } |