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 | |
Javier Cardona | 5ee68e5 | 2011-08-09 16:45:08 -0700 | [diff] [blame] | 69 | static int mesh_gate_add(struct mesh_table *tbl, struct mesh_path *mpath); |
| 70 | |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 71 | /* |
| 72 | * CAREFUL -- "tbl" must not be an expression, |
| 73 | * in particular not an rcu_dereference(), since |
| 74 | * it's used twice. So it is illegal to do |
| 75 | * for_each_mesh_entry(rcu_dereference(...), ...) |
| 76 | */ |
| 77 | #define for_each_mesh_entry(tbl, p, node, i) \ |
| 78 | for (i = 0; i <= tbl->hash_mask; i++) \ |
| 79 | hlist_for_each_entry_rcu(node, p, &tbl->hash_buckets[i], list) |
| 80 | |
| 81 | |
Johannes Berg | 6b86bd6 | 2011-05-12 13:38:50 +0200 | [diff] [blame] | 82 | static struct mesh_table *mesh_table_alloc(int size_order) |
| 83 | { |
| 84 | int i; |
| 85 | struct mesh_table *newtbl; |
| 86 | |
Javier Cardona | d676ff4 | 2011-05-17 16:13:34 -0700 | [diff] [blame] | 87 | newtbl = kmalloc(sizeof(struct mesh_table), GFP_ATOMIC); |
Johannes Berg | 6b86bd6 | 2011-05-12 13:38:50 +0200 | [diff] [blame] | 88 | if (!newtbl) |
| 89 | return NULL; |
| 90 | |
| 91 | newtbl->hash_buckets = kzalloc(sizeof(struct hlist_head) * |
Javier Cardona | d676ff4 | 2011-05-17 16:13:34 -0700 | [diff] [blame] | 92 | (1 << size_order), GFP_ATOMIC); |
Johannes Berg | 6b86bd6 | 2011-05-12 13:38:50 +0200 | [diff] [blame] | 93 | |
| 94 | if (!newtbl->hash_buckets) { |
| 95 | kfree(newtbl); |
| 96 | return NULL; |
| 97 | } |
| 98 | |
| 99 | newtbl->hashwlock = kmalloc(sizeof(spinlock_t) * |
Javier Cardona | d676ff4 | 2011-05-17 16:13:34 -0700 | [diff] [blame] | 100 | (1 << size_order), GFP_ATOMIC); |
Johannes Berg | 6b86bd6 | 2011-05-12 13:38:50 +0200 | [diff] [blame] | 101 | if (!newtbl->hashwlock) { |
| 102 | kfree(newtbl->hash_buckets); |
| 103 | kfree(newtbl); |
| 104 | return NULL; |
| 105 | } |
| 106 | |
| 107 | newtbl->size_order = size_order; |
| 108 | newtbl->hash_mask = (1 << size_order) - 1; |
| 109 | atomic_set(&newtbl->entries, 0); |
| 110 | get_random_bytes(&newtbl->hash_rnd, |
| 111 | sizeof(newtbl->hash_rnd)); |
| 112 | for (i = 0; i <= newtbl->hash_mask; i++) |
| 113 | spin_lock_init(&newtbl->hashwlock[i]); |
Javier Cardona | 5ee68e5 | 2011-08-09 16:45:08 -0700 | [diff] [blame] | 114 | spin_lock_init(&newtbl->gates_lock); |
Johannes Berg | 6b86bd6 | 2011-05-12 13:38:50 +0200 | [diff] [blame] | 115 | |
| 116 | return newtbl; |
| 117 | } |
| 118 | |
Javier Cardona | 1888923 | 2009-08-10 12:15:52 -0700 | [diff] [blame] | 119 | static void __mesh_table_free(struct mesh_table *tbl) |
| 120 | { |
| 121 | kfree(tbl->hash_buckets); |
| 122 | kfree(tbl->hashwlock); |
| 123 | kfree(tbl); |
| 124 | } |
| 125 | |
Johannes Berg | 6b86bd6 | 2011-05-12 13:38:50 +0200 | [diff] [blame] | 126 | static void mesh_table_free(struct mesh_table *tbl, bool free_leafs) |
Javier Cardona | 1888923 | 2009-08-10 12:15:52 -0700 | [diff] [blame] | 127 | { |
| 128 | struct hlist_head *mesh_hash; |
| 129 | struct hlist_node *p, *q; |
Javier Cardona | 5ee68e5 | 2011-08-09 16:45:08 -0700 | [diff] [blame] | 130 | struct mpath_node *gate; |
Javier Cardona | 1888923 | 2009-08-10 12:15:52 -0700 | [diff] [blame] | 131 | int i; |
| 132 | |
| 133 | mesh_hash = tbl->hash_buckets; |
| 134 | for (i = 0; i <= tbl->hash_mask; i++) { |
Javier Cardona | 9b84b808 | 2011-05-03 16:57:16 -0700 | [diff] [blame] | 135 | spin_lock_bh(&tbl->hashwlock[i]); |
Javier Cardona | 1888923 | 2009-08-10 12:15:52 -0700 | [diff] [blame] | 136 | hlist_for_each_safe(p, q, &mesh_hash[i]) { |
| 137 | tbl->free_node(p, free_leafs); |
| 138 | atomic_dec(&tbl->entries); |
| 139 | } |
Javier Cardona | 9b84b808 | 2011-05-03 16:57:16 -0700 | [diff] [blame] | 140 | spin_unlock_bh(&tbl->hashwlock[i]); |
Javier Cardona | 1888923 | 2009-08-10 12:15:52 -0700 | [diff] [blame] | 141 | } |
Javier Cardona | 5ee68e5 | 2011-08-09 16:45:08 -0700 | [diff] [blame] | 142 | if (free_leafs) { |
| 143 | spin_lock_bh(&tbl->gates_lock); |
| 144 | hlist_for_each_entry_safe(gate, p, q, |
| 145 | tbl->known_gates, list) { |
| 146 | hlist_del(&gate->list); |
| 147 | kfree(gate); |
| 148 | } |
| 149 | kfree(tbl->known_gates); |
| 150 | spin_unlock_bh(&tbl->gates_lock); |
| 151 | } |
| 152 | |
Javier Cardona | 1888923 | 2009-08-10 12:15:52 -0700 | [diff] [blame] | 153 | __mesh_table_free(tbl); |
| 154 | } |
| 155 | |
cozybit Inc | a3e6b12 | 2011-04-13 11:10:28 -0700 | [diff] [blame] | 156 | static int mesh_table_grow(struct mesh_table *oldtbl, |
Johannes Berg | 6b86bd6 | 2011-05-12 13:38:50 +0200 | [diff] [blame] | 157 | struct mesh_table *newtbl) |
Javier Cardona | 1888923 | 2009-08-10 12:15:52 -0700 | [diff] [blame] | 158 | { |
Javier Cardona | 1888923 | 2009-08-10 12:15:52 -0700 | [diff] [blame] | 159 | struct hlist_head *oldhash; |
| 160 | struct hlist_node *p, *q; |
| 161 | int i; |
| 162 | |
cozybit Inc | a3e6b12 | 2011-04-13 11:10:28 -0700 | [diff] [blame] | 163 | if (atomic_read(&oldtbl->entries) |
| 164 | < oldtbl->mean_chain_len * (oldtbl->hash_mask + 1)) |
| 165 | return -EAGAIN; |
Javier Cardona | 1888923 | 2009-08-10 12:15:52 -0700 | [diff] [blame] | 166 | |
cozybit Inc | a3e6b12 | 2011-04-13 11:10:28 -0700 | [diff] [blame] | 167 | newtbl->free_node = oldtbl->free_node; |
| 168 | newtbl->mean_chain_len = oldtbl->mean_chain_len; |
| 169 | newtbl->copy_node = oldtbl->copy_node; |
Javier Cardona | 5ee68e5 | 2011-08-09 16:45:08 -0700 | [diff] [blame] | 170 | newtbl->known_gates = oldtbl->known_gates; |
cozybit Inc | a3e6b12 | 2011-04-13 11:10:28 -0700 | [diff] [blame] | 171 | atomic_set(&newtbl->entries, atomic_read(&oldtbl->entries)); |
Javier Cardona | 1888923 | 2009-08-10 12:15:52 -0700 | [diff] [blame] | 172 | |
cozybit Inc | a3e6b12 | 2011-04-13 11:10:28 -0700 | [diff] [blame] | 173 | oldhash = oldtbl->hash_buckets; |
| 174 | for (i = 0; i <= oldtbl->hash_mask; i++) |
Javier Cardona | 1888923 | 2009-08-10 12:15:52 -0700 | [diff] [blame] | 175 | hlist_for_each(p, &oldhash[i]) |
cozybit Inc | a3e6b12 | 2011-04-13 11:10:28 -0700 | [diff] [blame] | 176 | if (oldtbl->copy_node(p, newtbl) < 0) |
Javier Cardona | 1888923 | 2009-08-10 12:15:52 -0700 | [diff] [blame] | 177 | goto errcopy; |
| 178 | |
cozybit Inc | a3e6b12 | 2011-04-13 11:10:28 -0700 | [diff] [blame] | 179 | return 0; |
Javier Cardona | 1888923 | 2009-08-10 12:15:52 -0700 | [diff] [blame] | 180 | |
| 181 | errcopy: |
| 182 | for (i = 0; i <= newtbl->hash_mask; i++) { |
| 183 | hlist_for_each_safe(p, q, &newtbl->hash_buckets[i]) |
cozybit Inc | a3e6b12 | 2011-04-13 11:10:28 -0700 | [diff] [blame] | 184 | oldtbl->free_node(p, 0); |
Javier Cardona | 1888923 | 2009-08-10 12:15:52 -0700 | [diff] [blame] | 185 | } |
cozybit Inc | a3e6b12 | 2011-04-13 11:10:28 -0700 | [diff] [blame] | 186 | return -ENOMEM; |
Javier Cardona | 1888923 | 2009-08-10 12:15:52 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Johannes Berg | 6b86bd6 | 2011-05-12 13:38:50 +0200 | [diff] [blame] | 189 | static u32 mesh_table_hash(u8 *addr, struct ieee80211_sub_if_data *sdata, |
| 190 | struct mesh_table *tbl) |
| 191 | { |
| 192 | /* Use last four bytes of hw addr and interface index as hash index */ |
| 193 | return jhash_2words(*(u32 *)(addr+2), sdata->dev->ifindex, tbl->hash_rnd) |
| 194 | & tbl->hash_mask; |
| 195 | } |
Johannes Berg | f5ea912 | 2009-08-07 16:17:38 +0200 | [diff] [blame] | 196 | |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 197 | |
| 198 | /** |
| 199 | * |
| 200 | * mesh_path_assign_nexthop - update mesh path next hop |
| 201 | * |
| 202 | * @mpath: mesh path to update |
| 203 | * @sta: next hop to assign |
| 204 | * |
| 205 | * Locking: mpath->state_lock must be held when calling this function |
| 206 | */ |
| 207 | void mesh_path_assign_nexthop(struct mesh_path *mpath, struct sta_info *sta) |
| 208 | { |
Javier Cardona | 10c836d | 2009-07-09 14:42:16 -0700 | [diff] [blame] | 209 | struct sk_buff *skb; |
| 210 | struct ieee80211_hdr *hdr; |
| 211 | struct sk_buff_head tmpq; |
| 212 | unsigned long flags; |
| 213 | |
Johannes Berg | d0709a6 | 2008-02-25 16:27:46 +0100 | [diff] [blame] | 214 | rcu_assign_pointer(mpath->next_hop, sta); |
Javier Cardona | 10c836d | 2009-07-09 14:42:16 -0700 | [diff] [blame] | 215 | |
| 216 | __skb_queue_head_init(&tmpq); |
| 217 | |
| 218 | spin_lock_irqsave(&mpath->frame_queue.lock, flags); |
| 219 | |
| 220 | while ((skb = __skb_dequeue(&mpath->frame_queue)) != NULL) { |
| 221 | hdr = (struct ieee80211_hdr *) skb->data; |
| 222 | memcpy(hdr->addr1, sta->sta.addr, ETH_ALEN); |
| 223 | __skb_queue_tail(&tmpq, skb); |
| 224 | } |
| 225 | |
| 226 | skb_queue_splice(&tmpq, &mpath->frame_queue); |
| 227 | spin_unlock_irqrestore(&mpath->frame_queue.lock, flags); |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 228 | } |
| 229 | |
Javier Cardona | 5ee68e5 | 2011-08-09 16:45:08 -0700 | [diff] [blame] | 230 | static void prepare_for_gate(struct sk_buff *skb, char *dst_addr, |
| 231 | struct mesh_path *gate_mpath) |
| 232 | { |
| 233 | struct ieee80211_hdr *hdr; |
| 234 | struct ieee80211s_hdr *mshdr; |
| 235 | int mesh_hdrlen, hdrlen; |
| 236 | char *next_hop; |
| 237 | |
| 238 | hdr = (struct ieee80211_hdr *) skb->data; |
| 239 | hdrlen = ieee80211_hdrlen(hdr->frame_control); |
| 240 | mshdr = (struct ieee80211s_hdr *) (skb->data + hdrlen); |
| 241 | |
| 242 | if (!(mshdr->flags & MESH_FLAGS_AE)) { |
| 243 | /* size of the fixed part of the mesh header */ |
| 244 | mesh_hdrlen = 6; |
| 245 | |
| 246 | /* make room for the two extended addresses */ |
| 247 | skb_push(skb, 2 * ETH_ALEN); |
| 248 | memmove(skb->data, hdr, hdrlen + mesh_hdrlen); |
| 249 | |
| 250 | hdr = (struct ieee80211_hdr *) skb->data; |
| 251 | |
| 252 | /* we preserve the previous mesh header and only add |
| 253 | * the new addreses */ |
| 254 | mshdr = (struct ieee80211s_hdr *) (skb->data + hdrlen); |
| 255 | mshdr->flags = MESH_FLAGS_AE_A5_A6; |
| 256 | memcpy(mshdr->eaddr1, hdr->addr3, ETH_ALEN); |
| 257 | memcpy(mshdr->eaddr2, hdr->addr4, ETH_ALEN); |
| 258 | } |
| 259 | |
| 260 | /* update next hop */ |
| 261 | hdr = (struct ieee80211_hdr *) skb->data; |
| 262 | rcu_read_lock(); |
| 263 | next_hop = rcu_dereference(gate_mpath->next_hop)->sta.addr; |
| 264 | memcpy(hdr->addr1, next_hop, ETH_ALEN); |
| 265 | rcu_read_unlock(); |
| 266 | memcpy(hdr->addr3, dst_addr, ETH_ALEN); |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * |
| 271 | * mesh_path_move_to_queue - Move or copy frames from one mpath queue to another |
| 272 | * |
| 273 | * This function is used to transfer or copy frames from an unresolved mpath to |
| 274 | * a gate mpath. The function also adds the Address Extension field and |
| 275 | * updates the next hop. |
| 276 | * |
| 277 | * If a frame already has an Address Extension field, only the next hop and |
| 278 | * destination addresses are updated. |
| 279 | * |
| 280 | * The gate mpath must be an active mpath with a valid mpath->next_hop. |
| 281 | * |
| 282 | * @mpath: An active mpath the frames will be sent to (i.e. the gate) |
| 283 | * @from_mpath: The failed mpath |
| 284 | * @copy: When true, copy all the frames to the new mpath queue. When false, |
| 285 | * move them. |
| 286 | */ |
| 287 | static void mesh_path_move_to_queue(struct mesh_path *gate_mpath, |
| 288 | struct mesh_path *from_mpath, |
| 289 | bool copy) |
| 290 | { |
Thomas Pedersen | c613366 | 2011-08-25 10:36:14 -0700 | [diff] [blame] | 291 | struct sk_buff *skb, *cp_skb = NULL; |
Javier Cardona | 5ee68e5 | 2011-08-09 16:45:08 -0700 | [diff] [blame] | 292 | struct sk_buff_head gateq, failq; |
| 293 | unsigned long flags; |
| 294 | int num_skbs; |
| 295 | |
| 296 | BUG_ON(gate_mpath == from_mpath); |
| 297 | BUG_ON(!gate_mpath->next_hop); |
| 298 | |
| 299 | __skb_queue_head_init(&gateq); |
| 300 | __skb_queue_head_init(&failq); |
| 301 | |
| 302 | spin_lock_irqsave(&from_mpath->frame_queue.lock, flags); |
| 303 | skb_queue_splice_init(&from_mpath->frame_queue, &failq); |
| 304 | spin_unlock_irqrestore(&from_mpath->frame_queue.lock, flags); |
| 305 | |
| 306 | num_skbs = skb_queue_len(&failq); |
| 307 | |
| 308 | while (num_skbs--) { |
| 309 | skb = __skb_dequeue(&failq); |
John W. Linville | 817a53d | 2011-08-24 15:12:41 -0400 | [diff] [blame] | 310 | if (copy) { |
Javier Cardona | 5ee68e5 | 2011-08-09 16:45:08 -0700 | [diff] [blame] | 311 | cp_skb = skb_copy(skb, GFP_ATOMIC); |
John W. Linville | 817a53d | 2011-08-24 15:12:41 -0400 | [diff] [blame] | 312 | if (cp_skb) |
| 313 | __skb_queue_tail(&failq, cp_skb); |
| 314 | } |
Javier Cardona | 5ee68e5 | 2011-08-09 16:45:08 -0700 | [diff] [blame] | 315 | |
| 316 | prepare_for_gate(skb, gate_mpath->dst, gate_mpath); |
| 317 | __skb_queue_tail(&gateq, skb); |
Javier Cardona | 5ee68e5 | 2011-08-09 16:45:08 -0700 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | spin_lock_irqsave(&gate_mpath->frame_queue.lock, flags); |
| 321 | skb_queue_splice(&gateq, &gate_mpath->frame_queue); |
| 322 | mpath_dbg("Mpath queue for gate %pM has %d frames\n", |
| 323 | gate_mpath->dst, |
| 324 | skb_queue_len(&gate_mpath->frame_queue)); |
| 325 | spin_unlock_irqrestore(&gate_mpath->frame_queue.lock, flags); |
| 326 | |
| 327 | if (!copy) |
| 328 | return; |
| 329 | |
| 330 | spin_lock_irqsave(&from_mpath->frame_queue.lock, flags); |
| 331 | skb_queue_splice(&failq, &from_mpath->frame_queue); |
| 332 | spin_unlock_irqrestore(&from_mpath->frame_queue.lock, flags); |
| 333 | } |
| 334 | |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 335 | |
| 336 | /** |
| 337 | * mesh_path_lookup - look up a path in the mesh path table |
| 338 | * @dst: hardware address (ETH_ALEN length) of destination |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 339 | * @sdata: local subif |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 340 | * |
| 341 | * Returns: pointer to the mesh path structure, or NULL if not found |
| 342 | * |
| 343 | * Locking: must be called within a read rcu section. |
| 344 | */ |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 345 | 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] | 346 | { |
| 347 | struct mesh_path *mpath; |
| 348 | struct hlist_node *n; |
| 349 | struct hlist_head *bucket; |
| 350 | struct mesh_table *tbl; |
| 351 | struct mpath_node *node; |
| 352 | |
| 353 | tbl = rcu_dereference(mesh_paths); |
| 354 | |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 355 | bucket = &tbl->hash_buckets[mesh_table_hash(dst, sdata, tbl)]; |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 356 | hlist_for_each_entry_rcu(node, n, bucket, list) { |
| 357 | mpath = node->mpath; |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 358 | if (mpath->sdata == sdata && |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 359 | memcmp(dst, mpath->dst, ETH_ALEN) == 0) { |
| 360 | if (MPATH_EXPIRED(mpath)) { |
| 361 | spin_lock_bh(&mpath->state_lock); |
Javier Cardona | ad99d14 | 2011-08-29 13:23:06 -0700 | [diff] [blame^] | 362 | mpath->flags &= ~MESH_PATH_ACTIVE; |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 363 | spin_unlock_bh(&mpath->state_lock); |
| 364 | } |
| 365 | return mpath; |
| 366 | } |
| 367 | } |
| 368 | return NULL; |
| 369 | } |
| 370 | |
YanBo | 79617de | 2008-09-22 13:30:32 +0800 | [diff] [blame] | 371 | struct mesh_path *mpp_path_lookup(u8 *dst, struct ieee80211_sub_if_data *sdata) |
| 372 | { |
| 373 | struct mesh_path *mpath; |
| 374 | struct hlist_node *n; |
| 375 | struct hlist_head *bucket; |
| 376 | struct mesh_table *tbl; |
| 377 | struct mpath_node *node; |
| 378 | |
| 379 | tbl = rcu_dereference(mpp_paths); |
| 380 | |
| 381 | bucket = &tbl->hash_buckets[mesh_table_hash(dst, sdata, tbl)]; |
| 382 | hlist_for_each_entry_rcu(node, n, bucket, list) { |
| 383 | mpath = node->mpath; |
| 384 | if (mpath->sdata == sdata && |
| 385 | memcmp(dst, mpath->dst, ETH_ALEN) == 0) { |
| 386 | if (MPATH_EXPIRED(mpath)) { |
| 387 | spin_lock_bh(&mpath->state_lock); |
Javier Cardona | ad99d14 | 2011-08-29 13:23:06 -0700 | [diff] [blame^] | 388 | mpath->flags &= ~MESH_PATH_ACTIVE; |
YanBo | 79617de | 2008-09-22 13:30:32 +0800 | [diff] [blame] | 389 | spin_unlock_bh(&mpath->state_lock); |
| 390 | } |
| 391 | return mpath; |
| 392 | } |
| 393 | } |
| 394 | return NULL; |
| 395 | } |
| 396 | |
| 397 | |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 398 | /** |
| 399 | * mesh_path_lookup_by_idx - look up a path in the mesh path table by its index |
| 400 | * @idx: index |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 401 | * @sdata: local subif, or NULL for all entries |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 402 | * |
| 403 | * Returns: pointer to the mesh path structure, or NULL if not found. |
| 404 | * |
| 405 | * Locking: must be called within a read rcu section. |
| 406 | */ |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 407 | 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] | 408 | { |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 409 | struct mesh_table *tbl = rcu_dereference(mesh_paths); |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 410 | struct mpath_node *node; |
| 411 | struct hlist_node *p; |
| 412 | int i; |
| 413 | int j = 0; |
| 414 | |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 415 | for_each_mesh_entry(tbl, p, node, i) { |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 416 | if (sdata && node->mpath->sdata != sdata) |
Luis Carlos Cobo | 2a8ca29 | 2008-02-29 17:51:25 -0800 | [diff] [blame] | 417 | continue; |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 418 | if (j++ == idx) { |
| 419 | if (MPATH_EXPIRED(node->mpath)) { |
| 420 | spin_lock_bh(&node->mpath->state_lock); |
Javier Cardona | ad99d14 | 2011-08-29 13:23:06 -0700 | [diff] [blame^] | 421 | node->mpath->flags &= ~MESH_PATH_ACTIVE; |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 422 | spin_unlock_bh(&node->mpath->state_lock); |
| 423 | } |
| 424 | return node->mpath; |
| 425 | } |
Luis Carlos Cobo | 2a8ca29 | 2008-02-29 17:51:25 -0800 | [diff] [blame] | 426 | } |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 427 | |
| 428 | return NULL; |
| 429 | } |
| 430 | |
Javier Cardona | 5ee68e5 | 2011-08-09 16:45:08 -0700 | [diff] [blame] | 431 | static void mesh_gate_node_reclaim(struct rcu_head *rp) |
| 432 | { |
| 433 | struct mpath_node *node = container_of(rp, struct mpath_node, rcu); |
| 434 | kfree(node); |
| 435 | } |
| 436 | |
| 437 | /** |
| 438 | * mesh_gate_add - mark mpath as path to a mesh gate and add to known_gates |
| 439 | * @mesh_tbl: table which contains known_gates list |
| 440 | * @mpath: mpath to known mesh gate |
| 441 | * |
| 442 | * Returns: 0 on success |
| 443 | * |
| 444 | */ |
| 445 | static int mesh_gate_add(struct mesh_table *tbl, struct mesh_path *mpath) |
| 446 | { |
| 447 | struct mpath_node *gate, *new_gate; |
| 448 | struct hlist_node *n; |
| 449 | int err; |
| 450 | |
| 451 | rcu_read_lock(); |
| 452 | tbl = rcu_dereference(tbl); |
| 453 | |
| 454 | hlist_for_each_entry_rcu(gate, n, tbl->known_gates, list) |
| 455 | if (gate->mpath == mpath) { |
| 456 | err = -EEXIST; |
| 457 | goto err_rcu; |
| 458 | } |
| 459 | |
| 460 | new_gate = kzalloc(sizeof(struct mpath_node), GFP_ATOMIC); |
| 461 | if (!new_gate) { |
| 462 | err = -ENOMEM; |
| 463 | goto err_rcu; |
| 464 | } |
| 465 | |
| 466 | mpath->is_gate = true; |
| 467 | mpath->sdata->u.mesh.num_gates++; |
| 468 | new_gate->mpath = mpath; |
| 469 | spin_lock_bh(&tbl->gates_lock); |
| 470 | hlist_add_head_rcu(&new_gate->list, tbl->known_gates); |
| 471 | spin_unlock_bh(&tbl->gates_lock); |
| 472 | rcu_read_unlock(); |
| 473 | mpath_dbg("Mesh path (%s): Recorded new gate: %pM. %d known gates\n", |
| 474 | mpath->sdata->name, mpath->dst, |
| 475 | mpath->sdata->u.mesh.num_gates); |
| 476 | return 0; |
| 477 | err_rcu: |
| 478 | rcu_read_unlock(); |
| 479 | return err; |
| 480 | } |
| 481 | |
| 482 | /** |
| 483 | * mesh_gate_del - remove a mesh gate from the list of known gates |
| 484 | * @tbl: table which holds our list of known gates |
| 485 | * @mpath: gate mpath |
| 486 | * |
| 487 | * Returns: 0 on success |
| 488 | * |
| 489 | * Locking: must be called inside rcu_read_lock() section |
| 490 | */ |
| 491 | static int mesh_gate_del(struct mesh_table *tbl, struct mesh_path *mpath) |
| 492 | { |
| 493 | struct mpath_node *gate; |
| 494 | struct hlist_node *p, *q; |
| 495 | |
| 496 | tbl = rcu_dereference(tbl); |
| 497 | |
| 498 | hlist_for_each_entry_safe(gate, p, q, tbl->known_gates, list) |
| 499 | if (gate->mpath == mpath) { |
| 500 | spin_lock_bh(&tbl->gates_lock); |
| 501 | hlist_del_rcu(&gate->list); |
| 502 | call_rcu(&gate->rcu, mesh_gate_node_reclaim); |
| 503 | spin_unlock_bh(&tbl->gates_lock); |
| 504 | mpath->sdata->u.mesh.num_gates--; |
| 505 | mpath->is_gate = false; |
| 506 | mpath_dbg("Mesh path (%s): Deleted gate: %pM. " |
| 507 | "%d known gates\n", mpath->sdata->name, |
| 508 | mpath->dst, mpath->sdata->u.mesh.num_gates); |
| 509 | break; |
| 510 | } |
| 511 | |
| 512 | return 0; |
| 513 | } |
| 514 | |
| 515 | /** |
| 516 | * |
| 517 | * mesh_path_add_gate - add the given mpath to a mesh gate to our path table |
| 518 | * @mpath: gate path to add to table |
| 519 | */ |
| 520 | int mesh_path_add_gate(struct mesh_path *mpath) |
| 521 | { |
| 522 | return mesh_gate_add(mesh_paths, mpath); |
| 523 | } |
| 524 | |
| 525 | /** |
| 526 | * mesh_gate_num - number of gates known to this interface |
| 527 | * @sdata: subif data |
| 528 | */ |
| 529 | int mesh_gate_num(struct ieee80211_sub_if_data *sdata) |
| 530 | { |
| 531 | return sdata->u.mesh.num_gates; |
| 532 | } |
| 533 | |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 534 | /** |
| 535 | * mesh_path_add - allocate and add a new path to the mesh path table |
| 536 | * @addr: destination address of the path (ETH_ALEN length) |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 537 | * @sdata: local subif |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 538 | * |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 539 | * Returns: 0 on success |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 540 | * |
| 541 | * State: the initial state of the new path is set to 0 |
| 542 | */ |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 543 | 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] | 544 | { |
Javier Cardona | 1888923 | 2009-08-10 12:15:52 -0700 | [diff] [blame] | 545 | struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh; |
| 546 | struct ieee80211_local *local = sdata->local; |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 547 | struct mesh_table *tbl; |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 548 | struct mesh_path *mpath, *new_mpath; |
| 549 | struct mpath_node *node, *new_node; |
| 550 | struct hlist_head *bucket; |
| 551 | struct hlist_node *n; |
| 552 | int grow = 0; |
| 553 | int err = 0; |
| 554 | u32 hash_idx; |
| 555 | |
Johannes Berg | 47846c9 | 2009-11-25 17:46:19 +0100 | [diff] [blame] | 556 | if (memcmp(dst, sdata->vif.addr, ETH_ALEN) == 0) |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 557 | /* never add ourselves as neighbours */ |
| 558 | return -ENOTSUPP; |
| 559 | |
| 560 | if (is_multicast_ether_addr(dst)) |
| 561 | return -ENOTSUPP; |
| 562 | |
Johannes Berg | 472dbc4 | 2008-09-11 00:01:49 +0200 | [diff] [blame] | 563 | 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] | 564 | return -ENOSPC; |
| 565 | |
Pavel Emelyanov | 402d775 | 2008-05-06 18:53:43 +0400 | [diff] [blame] | 566 | err = -ENOMEM; |
Javier Cardona | 1888923 | 2009-08-10 12:15:52 -0700 | [diff] [blame] | 567 | new_mpath = kzalloc(sizeof(struct mesh_path), GFP_ATOMIC); |
Pavel Emelyanov | 402d775 | 2008-05-06 18:53:43 +0400 | [diff] [blame] | 568 | if (!new_mpath) |
| 569 | goto err_path_alloc; |
| 570 | |
Javier Cardona | 1888923 | 2009-08-10 12:15:52 -0700 | [diff] [blame] | 571 | new_node = kmalloc(sizeof(struct mpath_node), GFP_ATOMIC); |
Pavel Emelyanov | 402d775 | 2008-05-06 18:53:43 +0400 | [diff] [blame] | 572 | if (!new_node) |
| 573 | goto err_node_alloc; |
Pavel Emelyanov | f84e71a | 2008-05-06 18:46:36 +0400 | [diff] [blame] | 574 | |
Javier Cardona | 9b84b808 | 2011-05-03 16:57:16 -0700 | [diff] [blame] | 575 | read_lock_bh(&pathtbl_resize_lock); |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 576 | memcpy(new_mpath->dst, dst, ETH_ALEN); |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 577 | new_mpath->sdata = sdata; |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 578 | new_mpath->flags = 0; |
| 579 | skb_queue_head_init(&new_mpath->frame_queue); |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 580 | new_node->mpath = new_mpath; |
| 581 | new_mpath->timer.data = (unsigned long) new_mpath; |
| 582 | new_mpath->timer.function = mesh_path_timer; |
| 583 | new_mpath->exp_time = jiffies; |
| 584 | spin_lock_init(&new_mpath->state_lock); |
| 585 | init_timer(&new_mpath->timer); |
| 586 | |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 587 | tbl = resize_dereference_mesh_paths(); |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 588 | |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 589 | hash_idx = mesh_table_hash(dst, sdata, tbl); |
| 590 | bucket = &tbl->hash_buckets[hash_idx]; |
| 591 | |
| 592 | spin_lock_bh(&tbl->hashwlock[hash_idx]); |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 593 | |
Pavel Emelyanov | 402d775 | 2008-05-06 18:53:43 +0400 | [diff] [blame] | 594 | err = -EEXIST; |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 595 | hlist_for_each_entry(node, n, bucket, list) { |
| 596 | mpath = node->mpath; |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 597 | if (mpath->sdata == sdata && memcmp(dst, mpath->dst, ETH_ALEN) == 0) |
Pavel Emelyanov | 402d775 | 2008-05-06 18:53:43 +0400 | [diff] [blame] | 598 | goto err_exists; |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | hlist_add_head_rcu(&new_node->list, bucket); |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 602 | if (atomic_inc_return(&tbl->entries) >= |
| 603 | tbl->mean_chain_len * (tbl->hash_mask + 1)) |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 604 | grow = 1; |
| 605 | |
Johannes Berg | f5ea912 | 2009-08-07 16:17:38 +0200 | [diff] [blame] | 606 | mesh_paths_generation++; |
| 607 | |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 608 | spin_unlock_bh(&tbl->hashwlock[hash_idx]); |
Javier Cardona | 9b84b808 | 2011-05-03 16:57:16 -0700 | [diff] [blame] | 609 | read_unlock_bh(&pathtbl_resize_lock); |
Pavel Emelyanov | 402d775 | 2008-05-06 18:53:43 +0400 | [diff] [blame] | 610 | if (grow) { |
Javier Cardona | 1888923 | 2009-08-10 12:15:52 -0700 | [diff] [blame] | 611 | set_bit(MESH_WORK_GROW_MPATH_TABLE, &ifmsh->wrkq_flags); |
Johannes Berg | 64592c8 | 2010-06-10 10:21:31 +0200 | [diff] [blame] | 612 | ieee80211_queue_work(&local->hw, &sdata->work); |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 613 | } |
Pavel Emelyanov | 402d775 | 2008-05-06 18:53:43 +0400 | [diff] [blame] | 614 | return 0; |
| 615 | |
| 616 | err_exists: |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 617 | spin_unlock_bh(&tbl->hashwlock[hash_idx]); |
Javier Cardona | 9b84b808 | 2011-05-03 16:57:16 -0700 | [diff] [blame] | 618 | read_unlock_bh(&pathtbl_resize_lock); |
Pavel Emelyanov | 402d775 | 2008-05-06 18:53:43 +0400 | [diff] [blame] | 619 | kfree(new_node); |
| 620 | err_node_alloc: |
| 621 | kfree(new_mpath); |
| 622 | err_path_alloc: |
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 | return err; |
| 625 | } |
| 626 | |
Johannes Berg | 1928eca | 2011-05-14 11:00:52 +0200 | [diff] [blame] | 627 | static void mesh_table_free_rcu(struct rcu_head *rcu) |
| 628 | { |
| 629 | struct mesh_table *tbl = container_of(rcu, struct mesh_table, rcu_head); |
| 630 | |
| 631 | mesh_table_free(tbl, false); |
| 632 | } |
| 633 | |
Javier Cardona | 1888923 | 2009-08-10 12:15:52 -0700 | [diff] [blame] | 634 | void mesh_mpath_table_grow(void) |
| 635 | { |
| 636 | struct mesh_table *oldtbl, *newtbl; |
| 637 | |
Javier Cardona | 9b84b808 | 2011-05-03 16:57:16 -0700 | [diff] [blame] | 638 | write_lock_bh(&pathtbl_resize_lock); |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 639 | oldtbl = resize_dereference_mesh_paths(); |
| 640 | newtbl = mesh_table_alloc(oldtbl->size_order + 1); |
Johannes Berg | 1928eca | 2011-05-14 11:00:52 +0200 | [diff] [blame] | 641 | if (!newtbl) |
| 642 | goto out; |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 643 | if (mesh_table_grow(oldtbl, newtbl) < 0) { |
cozybit Inc | a3e6b12 | 2011-04-13 11:10:28 -0700 | [diff] [blame] | 644 | __mesh_table_free(newtbl); |
Johannes Berg | 1928eca | 2011-05-14 11:00:52 +0200 | [diff] [blame] | 645 | goto out; |
Javier Cardona | 1888923 | 2009-08-10 12:15:52 -0700 | [diff] [blame] | 646 | } |
| 647 | rcu_assign_pointer(mesh_paths, newtbl); |
Javier Cardona | 1888923 | 2009-08-10 12:15:52 -0700 | [diff] [blame] | 648 | |
Johannes Berg | 1928eca | 2011-05-14 11:00:52 +0200 | [diff] [blame] | 649 | call_rcu(&oldtbl->rcu_head, mesh_table_free_rcu); |
| 650 | |
| 651 | out: |
| 652 | write_unlock_bh(&pathtbl_resize_lock); |
Javier Cardona | 1888923 | 2009-08-10 12:15:52 -0700 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | void mesh_mpp_table_grow(void) |
| 656 | { |
| 657 | struct mesh_table *oldtbl, *newtbl; |
| 658 | |
Javier Cardona | 9b84b808 | 2011-05-03 16:57:16 -0700 | [diff] [blame] | 659 | write_lock_bh(&pathtbl_resize_lock); |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 660 | oldtbl = resize_dereference_mpp_paths(); |
| 661 | newtbl = mesh_table_alloc(oldtbl->size_order + 1); |
Johannes Berg | 1928eca | 2011-05-14 11:00:52 +0200 | [diff] [blame] | 662 | if (!newtbl) |
| 663 | goto out; |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 664 | if (mesh_table_grow(oldtbl, newtbl) < 0) { |
cozybit Inc | a3e6b12 | 2011-04-13 11:10:28 -0700 | [diff] [blame] | 665 | __mesh_table_free(newtbl); |
Johannes Berg | 1928eca | 2011-05-14 11:00:52 +0200 | [diff] [blame] | 666 | goto out; |
Javier Cardona | 1888923 | 2009-08-10 12:15:52 -0700 | [diff] [blame] | 667 | } |
| 668 | rcu_assign_pointer(mpp_paths, newtbl); |
Johannes Berg | 1928eca | 2011-05-14 11:00:52 +0200 | [diff] [blame] | 669 | call_rcu(&oldtbl->rcu_head, mesh_table_free_rcu); |
Javier Cardona | 1888923 | 2009-08-10 12:15:52 -0700 | [diff] [blame] | 670 | |
Johannes Berg | 1928eca | 2011-05-14 11:00:52 +0200 | [diff] [blame] | 671 | out: |
| 672 | write_unlock_bh(&pathtbl_resize_lock); |
Javier Cardona | 1888923 | 2009-08-10 12:15:52 -0700 | [diff] [blame] | 673 | } |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 674 | |
YanBo | 79617de | 2008-09-22 13:30:32 +0800 | [diff] [blame] | 675 | int mpp_path_add(u8 *dst, u8 *mpp, struct ieee80211_sub_if_data *sdata) |
| 676 | { |
Javier Cardona | 1888923 | 2009-08-10 12:15:52 -0700 | [diff] [blame] | 677 | struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh; |
| 678 | struct ieee80211_local *local = sdata->local; |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 679 | struct mesh_table *tbl; |
YanBo | 79617de | 2008-09-22 13:30:32 +0800 | [diff] [blame] | 680 | struct mesh_path *mpath, *new_mpath; |
| 681 | struct mpath_node *node, *new_node; |
| 682 | struct hlist_head *bucket; |
| 683 | struct hlist_node *n; |
| 684 | int grow = 0; |
| 685 | int err = 0; |
| 686 | u32 hash_idx; |
| 687 | |
Johannes Berg | 47846c9 | 2009-11-25 17:46:19 +0100 | [diff] [blame] | 688 | if (memcmp(dst, sdata->vif.addr, ETH_ALEN) == 0) |
YanBo | 79617de | 2008-09-22 13:30:32 +0800 | [diff] [blame] | 689 | /* never add ourselves as neighbours */ |
| 690 | return -ENOTSUPP; |
| 691 | |
| 692 | if (is_multicast_ether_addr(dst)) |
| 693 | return -ENOTSUPP; |
| 694 | |
| 695 | err = -ENOMEM; |
Javier Cardona | 1888923 | 2009-08-10 12:15:52 -0700 | [diff] [blame] | 696 | new_mpath = kzalloc(sizeof(struct mesh_path), GFP_ATOMIC); |
YanBo | 79617de | 2008-09-22 13:30:32 +0800 | [diff] [blame] | 697 | if (!new_mpath) |
| 698 | goto err_path_alloc; |
| 699 | |
Javier Cardona | 1888923 | 2009-08-10 12:15:52 -0700 | [diff] [blame] | 700 | new_node = kmalloc(sizeof(struct mpath_node), GFP_ATOMIC); |
YanBo | 79617de | 2008-09-22 13:30:32 +0800 | [diff] [blame] | 701 | if (!new_node) |
| 702 | goto err_node_alloc; |
| 703 | |
Javier Cardona | 9b84b808 | 2011-05-03 16:57:16 -0700 | [diff] [blame] | 704 | read_lock_bh(&pathtbl_resize_lock); |
YanBo | 79617de | 2008-09-22 13:30:32 +0800 | [diff] [blame] | 705 | memcpy(new_mpath->dst, dst, ETH_ALEN); |
| 706 | memcpy(new_mpath->mpp, mpp, ETH_ALEN); |
| 707 | new_mpath->sdata = sdata; |
| 708 | new_mpath->flags = 0; |
| 709 | skb_queue_head_init(&new_mpath->frame_queue); |
| 710 | new_node->mpath = new_mpath; |
Thomas Pedersen | c613366 | 2011-08-25 10:36:14 -0700 | [diff] [blame] | 711 | init_timer(&new_mpath->timer); |
YanBo | 79617de | 2008-09-22 13:30:32 +0800 | [diff] [blame] | 712 | new_mpath->exp_time = jiffies; |
| 713 | spin_lock_init(&new_mpath->state_lock); |
| 714 | |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 715 | tbl = resize_dereference_mpp_paths(); |
YanBo | 79617de | 2008-09-22 13:30:32 +0800 | [diff] [blame] | 716 | |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 717 | hash_idx = mesh_table_hash(dst, sdata, tbl); |
| 718 | bucket = &tbl->hash_buckets[hash_idx]; |
| 719 | |
| 720 | spin_lock_bh(&tbl->hashwlock[hash_idx]); |
YanBo | 79617de | 2008-09-22 13:30:32 +0800 | [diff] [blame] | 721 | |
| 722 | err = -EEXIST; |
| 723 | hlist_for_each_entry(node, n, bucket, list) { |
| 724 | mpath = node->mpath; |
| 725 | if (mpath->sdata == sdata && memcmp(dst, mpath->dst, ETH_ALEN) == 0) |
| 726 | goto err_exists; |
| 727 | } |
| 728 | |
| 729 | hlist_add_head_rcu(&new_node->list, bucket); |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 730 | if (atomic_inc_return(&tbl->entries) >= |
| 731 | tbl->mean_chain_len * (tbl->hash_mask + 1)) |
YanBo | 79617de | 2008-09-22 13:30:32 +0800 | [diff] [blame] | 732 | grow = 1; |
| 733 | |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 734 | spin_unlock_bh(&tbl->hashwlock[hash_idx]); |
Javier Cardona | 9b84b808 | 2011-05-03 16:57:16 -0700 | [diff] [blame] | 735 | read_unlock_bh(&pathtbl_resize_lock); |
YanBo | 79617de | 2008-09-22 13:30:32 +0800 | [diff] [blame] | 736 | if (grow) { |
Javier Cardona | 1888923 | 2009-08-10 12:15:52 -0700 | [diff] [blame] | 737 | set_bit(MESH_WORK_GROW_MPP_TABLE, &ifmsh->wrkq_flags); |
Johannes Berg | 64592c8 | 2010-06-10 10:21:31 +0200 | [diff] [blame] | 738 | ieee80211_queue_work(&local->hw, &sdata->work); |
YanBo | 79617de | 2008-09-22 13:30:32 +0800 | [diff] [blame] | 739 | } |
| 740 | return 0; |
| 741 | |
| 742 | err_exists: |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 743 | spin_unlock_bh(&tbl->hashwlock[hash_idx]); |
Javier Cardona | 9b84b808 | 2011-05-03 16:57:16 -0700 | [diff] [blame] | 744 | read_unlock_bh(&pathtbl_resize_lock); |
YanBo | 79617de | 2008-09-22 13:30:32 +0800 | [diff] [blame] | 745 | kfree(new_node); |
| 746 | err_node_alloc: |
| 747 | kfree(new_mpath); |
| 748 | err_path_alloc: |
| 749 | return err; |
| 750 | } |
| 751 | |
| 752 | |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 753 | /** |
| 754 | * mesh_plink_broken - deactivates paths and sends perr when a link breaks |
| 755 | * |
| 756 | * @sta: broken peer link |
| 757 | * |
| 758 | * This function must be called from the rate control algorithm if enough |
| 759 | * delivery errors suggest that a peer link is no longer usable. |
| 760 | */ |
| 761 | void mesh_plink_broken(struct sta_info *sta) |
| 762 | { |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 763 | struct mesh_table *tbl; |
Johannes Berg | 15ff636 | 2009-11-17 13:34:04 +0100 | [diff] [blame] | 764 | 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] | 765 | struct mesh_path *mpath; |
| 766 | struct mpath_node *node; |
| 767 | struct hlist_node *p; |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 768 | struct ieee80211_sub_if_data *sdata = sta->sdata; |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 769 | int i; |
Thomas Pedersen | 25d49e4 | 2011-08-11 19:35:15 -0700 | [diff] [blame] | 770 | __le16 reason = cpu_to_le16(WLAN_REASON_MESH_PATH_DEST_UNREACHABLE); |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 771 | |
| 772 | rcu_read_lock(); |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 773 | tbl = rcu_dereference(mesh_paths); |
| 774 | for_each_mesh_entry(tbl, p, node, i) { |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 775 | mpath = node->mpath; |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 776 | if (rcu_dereference(mpath->next_hop) == sta && |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 777 | mpath->flags & MESH_PATH_ACTIVE && |
| 778 | !(mpath->flags & MESH_PATH_FIXED)) { |
Javier Cardona | f5e50cd | 2011-08-29 13:23:05 -0700 | [diff] [blame] | 779 | spin_lock_bh(&mpath->state_lock); |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 780 | mpath->flags &= ~MESH_PATH_ACTIVE; |
Rui Paulo | d19b3bf | 2009-11-09 23:46:55 +0000 | [diff] [blame] | 781 | ++mpath->sn; |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 782 | spin_unlock_bh(&mpath->state_lock); |
Javier Cardona | 45904f2 | 2010-12-03 09:20:40 +0100 | [diff] [blame] | 783 | mesh_path_error_tx(sdata->u.mesh.mshcfg.element_ttl, |
| 784 | mpath->dst, cpu_to_le32(mpath->sn), |
Thomas Pedersen | 25d49e4 | 2011-08-11 19:35:15 -0700 | [diff] [blame] | 785 | reason, bcast, sdata); |
Javier Cardona | f5e50cd | 2011-08-29 13:23:05 -0700 | [diff] [blame] | 786 | } |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 787 | } |
| 788 | rcu_read_unlock(); |
| 789 | } |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 790 | |
| 791 | /** |
| 792 | * mesh_path_flush_by_nexthop - Deletes mesh paths if their next hop matches |
| 793 | * |
| 794 | * @sta - mesh peer to match |
| 795 | * |
Luis Carlos Cobo | b4e08ea | 2008-02-29 15:46:08 -0800 | [diff] [blame] | 796 | * RCU notes: this function is called when a mesh plink transitions from |
| 797 | * PLINK_ESTAB to any other state, since PLINK_ESTAB state is the only one that |
| 798 | * 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] | 799 | * sta_info_destroy() calls this) so any reader in a rcu read block will be |
| 800 | * protected against the plink disappearing. |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 801 | */ |
| 802 | void mesh_path_flush_by_nexthop(struct sta_info *sta) |
| 803 | { |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 804 | struct mesh_table *tbl; |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 805 | struct mesh_path *mpath; |
| 806 | struct mpath_node *node; |
| 807 | struct hlist_node *p; |
| 808 | int i; |
| 809 | |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 810 | rcu_read_lock(); |
| 811 | tbl = rcu_dereference(mesh_paths); |
| 812 | for_each_mesh_entry(tbl, p, node, i) { |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 813 | mpath = node->mpath; |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 814 | if (rcu_dereference(mpath->next_hop) == sta) |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 815 | mesh_path_del(mpath->dst, mpath->sdata); |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 816 | } |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 817 | rcu_read_unlock(); |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 818 | } |
| 819 | |
Javier Cardona | ece1a2e | 2011-08-29 13:23:04 -0700 | [diff] [blame] | 820 | static void mesh_path_flush(struct ieee80211_sub_if_data *sdata) |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 821 | { |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 822 | struct mesh_table *tbl; |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 823 | struct mesh_path *mpath; |
| 824 | struct mpath_node *node; |
| 825 | struct hlist_node *p; |
| 826 | int i; |
| 827 | |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 828 | rcu_read_lock(); |
| 829 | tbl = rcu_dereference(mesh_paths); |
| 830 | for_each_mesh_entry(tbl, p, node, i) { |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 831 | mpath = node->mpath; |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 832 | if (mpath->sdata == sdata) |
| 833 | mesh_path_del(mpath->dst, mpath->sdata); |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 834 | } |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 835 | rcu_read_unlock(); |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 836 | } |
| 837 | |
| 838 | static void mesh_path_node_reclaim(struct rcu_head *rp) |
| 839 | { |
| 840 | struct mpath_node *node = container_of(rp, struct mpath_node, rcu); |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 841 | struct ieee80211_sub_if_data *sdata = node->mpath->sdata; |
Johannes Berg | d0709a6 | 2008-02-25 16:27:46 +0100 | [diff] [blame] | 842 | |
Thomas Pedersen | c613366 | 2011-08-25 10:36:14 -0700 | [diff] [blame] | 843 | del_timer_sync(&node->mpath->timer); |
Johannes Berg | 472dbc4 | 2008-09-11 00:01:49 +0200 | [diff] [blame] | 844 | atomic_dec(&sdata->u.mesh.mpaths); |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 845 | kfree(node->mpath); |
| 846 | kfree(node); |
| 847 | } |
| 848 | |
Javier Cardona | ece1a2e | 2011-08-29 13:23:04 -0700 | [diff] [blame] | 849 | static void mpp_path_flush(struct ieee80211_sub_if_data *sdata) |
| 850 | { |
| 851 | struct mesh_table *tbl; |
| 852 | struct mesh_path *mpath; |
| 853 | struct mpath_node *node; |
| 854 | struct hlist_node *p; |
| 855 | int i; |
| 856 | |
| 857 | read_lock_bh(&pathtbl_resize_lock); |
| 858 | tbl = rcu_dereference_protected(mpp_paths, |
| 859 | lockdep_is_held(pathtbl_resize_lock)); |
| 860 | for_each_mesh_entry(tbl, p, node, i) { |
| 861 | mpath = node->mpath; |
| 862 | if (mpath->sdata != sdata) |
| 863 | continue; |
| 864 | spin_lock_bh(&tbl->hashwlock[i]); |
Javier Cardona | f5e50cd | 2011-08-29 13:23:05 -0700 | [diff] [blame] | 865 | hlist_del_rcu(&node->list); |
Javier Cardona | ece1a2e | 2011-08-29 13:23:04 -0700 | [diff] [blame] | 866 | call_rcu(&node->rcu, mesh_path_node_reclaim); |
| 867 | atomic_dec(&tbl->entries); |
| 868 | spin_unlock_bh(&tbl->hashwlock[i]); |
| 869 | } |
| 870 | read_unlock_bh(&pathtbl_resize_lock); |
| 871 | } |
| 872 | |
| 873 | /** |
| 874 | * mesh_path_flush_by_iface - Deletes all mesh paths associated with a given iface |
| 875 | * |
| 876 | * This function deletes both mesh paths as well as mesh portal paths. |
| 877 | * |
| 878 | * @sdata - interface data to match |
| 879 | * |
| 880 | */ |
| 881 | void mesh_path_flush_by_iface(struct ieee80211_sub_if_data *sdata) |
| 882 | { |
| 883 | mesh_path_flush(sdata); |
| 884 | mpp_path_flush(sdata); |
| 885 | } |
| 886 | |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 887 | /** |
| 888 | * mesh_path_del - delete a mesh path from the table |
| 889 | * |
| 890 | * @addr: dst address (ETH_ALEN length) |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 891 | * @sdata: local subif |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 892 | * |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 893 | * Returns: 0 if successful |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 894 | */ |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 895 | 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] | 896 | { |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 897 | struct mesh_table *tbl; |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 898 | struct mesh_path *mpath; |
| 899 | struct mpath_node *node; |
| 900 | struct hlist_head *bucket; |
| 901 | struct hlist_node *n; |
| 902 | int hash_idx; |
| 903 | int err = 0; |
| 904 | |
Javier Cardona | 9b84b808 | 2011-05-03 16:57:16 -0700 | [diff] [blame] | 905 | read_lock_bh(&pathtbl_resize_lock); |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 906 | tbl = resize_dereference_mesh_paths(); |
| 907 | hash_idx = mesh_table_hash(addr, sdata, tbl); |
| 908 | bucket = &tbl->hash_buckets[hash_idx]; |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 909 | |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 910 | spin_lock_bh(&tbl->hashwlock[hash_idx]); |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 911 | hlist_for_each_entry(node, n, bucket, list) { |
| 912 | mpath = node->mpath; |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 913 | if (mpath->sdata == sdata && |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 914 | memcmp(addr, mpath->dst, ETH_ALEN) == 0) { |
Javier Cardona | a6965c4 | 2011-08-09 16:45:06 -0700 | [diff] [blame] | 915 | spin_lock_bh(&mpath->state_lock); |
Javier Cardona | 5ee68e5 | 2011-08-09 16:45:08 -0700 | [diff] [blame] | 916 | if (mpath->is_gate) |
| 917 | mesh_gate_del(tbl, mpath); |
Luis Carlos Cobo | cfa22c7 | 2008-02-29 15:04:13 -0800 | [diff] [blame] | 918 | mpath->flags |= MESH_PATH_RESOLVING; |
| 919 | hlist_del_rcu(&node->list); |
| 920 | call_rcu(&node->rcu, mesh_path_node_reclaim); |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 921 | atomic_dec(&tbl->entries); |
Javier Cardona | a6965c4 | 2011-08-09 16:45:06 -0700 | [diff] [blame] | 922 | spin_unlock_bh(&mpath->state_lock); |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 923 | goto enddel; |
| 924 | } |
| 925 | } |
| 926 | |
| 927 | err = -ENXIO; |
| 928 | enddel: |
Johannes Berg | f5ea912 | 2009-08-07 16:17:38 +0200 | [diff] [blame] | 929 | mesh_paths_generation++; |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 930 | spin_unlock_bh(&tbl->hashwlock[hash_idx]); |
Javier Cardona | 9b84b808 | 2011-05-03 16:57:16 -0700 | [diff] [blame] | 931 | read_unlock_bh(&pathtbl_resize_lock); |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 932 | return err; |
| 933 | } |
| 934 | |
| 935 | /** |
| 936 | * mesh_path_tx_pending - sends pending frames in a mesh path queue |
| 937 | * |
| 938 | * @mpath: mesh path to activate |
| 939 | * |
| 940 | * Locking: the state_lock of the mpath structure must NOT be held when calling |
| 941 | * this function. |
| 942 | */ |
| 943 | void mesh_path_tx_pending(struct mesh_path *mpath) |
| 944 | { |
Javier Cardona | 249b405 | 2009-07-07 10:55:03 -0700 | [diff] [blame] | 945 | if (mpath->flags & MESH_PATH_ACTIVE) |
| 946 | ieee80211_add_pending_skbs(mpath->sdata->local, |
| 947 | &mpath->frame_queue); |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 948 | } |
| 949 | |
| 950 | /** |
Javier Cardona | 5ee68e5 | 2011-08-09 16:45:08 -0700 | [diff] [blame] | 951 | * mesh_path_send_to_gates - sends pending frames to all known mesh gates |
| 952 | * |
| 953 | * @mpath: mesh path whose queue will be emptied |
| 954 | * |
| 955 | * If there is only one gate, the frames are transferred from the failed mpath |
| 956 | * queue to that gate's queue. If there are more than one gates, the frames |
| 957 | * are copied from each gate to the next. After frames are copied, the |
| 958 | * mpath queues are emptied onto the transmission queue. |
| 959 | */ |
| 960 | int mesh_path_send_to_gates(struct mesh_path *mpath) |
| 961 | { |
| 962 | struct ieee80211_sub_if_data *sdata = mpath->sdata; |
| 963 | struct hlist_node *n; |
| 964 | struct mesh_table *tbl; |
| 965 | struct mesh_path *from_mpath = mpath; |
| 966 | struct mpath_node *gate = NULL; |
| 967 | bool copy = false; |
| 968 | struct hlist_head *known_gates; |
| 969 | |
| 970 | rcu_read_lock(); |
| 971 | tbl = rcu_dereference(mesh_paths); |
| 972 | known_gates = tbl->known_gates; |
| 973 | rcu_read_unlock(); |
| 974 | |
| 975 | if (!known_gates) |
| 976 | return -EHOSTUNREACH; |
| 977 | |
| 978 | hlist_for_each_entry_rcu(gate, n, known_gates, list) { |
| 979 | if (gate->mpath->sdata != sdata) |
| 980 | continue; |
| 981 | |
| 982 | if (gate->mpath->flags & MESH_PATH_ACTIVE) { |
| 983 | mpath_dbg("Forwarding to %pM\n", gate->mpath->dst); |
| 984 | mesh_path_move_to_queue(gate->mpath, from_mpath, copy); |
| 985 | from_mpath = gate->mpath; |
| 986 | copy = true; |
| 987 | } else { |
| 988 | mpath_dbg("Not forwarding %p\n", gate->mpath); |
| 989 | mpath_dbg("flags %x\n", gate->mpath->flags); |
| 990 | } |
| 991 | } |
| 992 | |
| 993 | hlist_for_each_entry_rcu(gate, n, known_gates, list) |
| 994 | if (gate->mpath->sdata == sdata) { |
| 995 | mpath_dbg("Sending to %pM\n", gate->mpath->dst); |
| 996 | mesh_path_tx_pending(gate->mpath); |
| 997 | } |
| 998 | |
| 999 | return (from_mpath == mpath) ? -EHOSTUNREACH : 0; |
| 1000 | } |
| 1001 | |
| 1002 | /** |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 1003 | * mesh_path_discard_frame - discard a frame whose path could not be resolved |
| 1004 | * |
| 1005 | * @skb: frame to discard |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 1006 | * @sdata: network subif the frame was to be sent through |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 1007 | * |
Javier Cardona | 35946a5 | 2009-07-13 17:00:10 -0700 | [diff] [blame] | 1008 | * If the frame was being forwarded from another MP, a PERR frame will be sent |
| 1009 | * to the precursor. The precursor's address (i.e. the previous hop) was saved |
| 1010 | * in addr1 of the frame-to-be-forwarded, and would only be overwritten once |
| 1011 | * the destination is successfully resolved. |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 1012 | * |
| 1013 | * Locking: the function must me called within a rcu_read_lock region |
| 1014 | */ |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 1015 | void mesh_path_discard_frame(struct sk_buff *skb, |
| 1016 | struct ieee80211_sub_if_data *sdata) |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 1017 | { |
Luis Carlos Cobo | e32f85f | 2008-08-05 19:34:52 +0200 | [diff] [blame] | 1018 | struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data; |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 1019 | struct mesh_path *mpath; |
Rui Paulo | d19b3bf | 2009-11-09 23:46:55 +0000 | [diff] [blame] | 1020 | u32 sn = 0; |
Thomas Pedersen | 25d49e4 | 2011-08-11 19:35:15 -0700 | [diff] [blame] | 1021 | __le16 reason = cpu_to_le16(WLAN_REASON_MESH_PATH_NOFORWARD); |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 1022 | |
Johannes Berg | 47846c9 | 2009-11-25 17:46:19 +0100 | [diff] [blame] | 1023 | if (memcmp(hdr->addr4, sdata->vif.addr, ETH_ALEN) != 0) { |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 1024 | u8 *ra, *da; |
| 1025 | |
Luis Carlos Cobo | e32f85f | 2008-08-05 19:34:52 +0200 | [diff] [blame] | 1026 | da = hdr->addr3; |
Javier Cardona | 35946a5 | 2009-07-13 17:00:10 -0700 | [diff] [blame] | 1027 | ra = hdr->addr1; |
Javier Cardona | af089c1 | 2011-08-29 13:23:03 -0700 | [diff] [blame] | 1028 | rcu_read_lock(); |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 1029 | mpath = mesh_path_lookup(da, sdata); |
Javier Cardona | af089c1 | 2011-08-29 13:23:03 -0700 | [diff] [blame] | 1030 | if (mpath) { |
| 1031 | spin_lock_bh(&mpath->state_lock); |
Rui Paulo | d19b3bf | 2009-11-09 23:46:55 +0000 | [diff] [blame] | 1032 | sn = ++mpath->sn; |
Javier Cardona | af089c1 | 2011-08-29 13:23:03 -0700 | [diff] [blame] | 1033 | spin_unlock_bh(&mpath->state_lock); |
| 1034 | } |
| 1035 | rcu_read_unlock(); |
Javier Cardona | 45904f2 | 2010-12-03 09:20:40 +0100 | [diff] [blame] | 1036 | mesh_path_error_tx(sdata->u.mesh.mshcfg.element_ttl, skb->data, |
Thomas Pedersen | 25d49e4 | 2011-08-11 19:35:15 -0700 | [diff] [blame] | 1037 | cpu_to_le32(sn), reason, ra, sdata); |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 1038 | } |
| 1039 | |
| 1040 | kfree_skb(skb); |
Johannes Berg | 472dbc4 | 2008-09-11 00:01:49 +0200 | [diff] [blame] | 1041 | sdata->u.mesh.mshstats.dropped_frames_no_route++; |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 1042 | } |
| 1043 | |
| 1044 | /** |
| 1045 | * mesh_path_flush_pending - free the pending queue of a mesh path |
| 1046 | * |
| 1047 | * @mpath: mesh path whose queue has to be freed |
| 1048 | * |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1049 | * 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] | 1050 | */ |
| 1051 | void mesh_path_flush_pending(struct mesh_path *mpath) |
| 1052 | { |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 1053 | struct sk_buff *skb; |
| 1054 | |
Javier Cardona | 00e3f25 | 2011-08-09 16:45:07 -0700 | [diff] [blame] | 1055 | while ((skb = skb_dequeue(&mpath->frame_queue)) != NULL) |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 1056 | mesh_path_discard_frame(skb, mpath->sdata); |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 1057 | } |
| 1058 | |
| 1059 | /** |
| 1060 | * mesh_path_fix_nexthop - force a specific next hop for a mesh path |
| 1061 | * |
| 1062 | * @mpath: the mesh path to modify |
| 1063 | * @next_hop: the next hop to force |
| 1064 | * |
| 1065 | * Locking: this function must be called holding mpath->state_lock |
| 1066 | */ |
| 1067 | void mesh_path_fix_nexthop(struct mesh_path *mpath, struct sta_info *next_hop) |
| 1068 | { |
| 1069 | spin_lock_bh(&mpath->state_lock); |
| 1070 | mesh_path_assign_nexthop(mpath, next_hop); |
Rui Paulo | d19b3bf | 2009-11-09 23:46:55 +0000 | [diff] [blame] | 1071 | mpath->sn = 0xffff; |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 1072 | mpath->metric = 0; |
| 1073 | mpath->hop_count = 0; |
| 1074 | mpath->exp_time = 0; |
| 1075 | mpath->flags |= MESH_PATH_FIXED; |
| 1076 | mesh_path_activate(mpath); |
| 1077 | spin_unlock_bh(&mpath->state_lock); |
| 1078 | mesh_path_tx_pending(mpath); |
| 1079 | } |
| 1080 | |
| 1081 | static void mesh_path_node_free(struct hlist_node *p, bool free_leafs) |
| 1082 | { |
| 1083 | struct mesh_path *mpath; |
| 1084 | struct mpath_node *node = hlist_entry(p, struct mpath_node, list); |
| 1085 | mpath = node->mpath; |
| 1086 | hlist_del_rcu(p); |
Javier Cardona | d0df9ee | 2011-05-13 14:16:07 -0700 | [diff] [blame] | 1087 | if (free_leafs) { |
Thomas Pedersen | c613366 | 2011-08-25 10:36:14 -0700 | [diff] [blame] | 1088 | del_timer_sync(&mpath->timer); |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 1089 | kfree(mpath); |
Javier Cardona | d0df9ee | 2011-05-13 14:16:07 -0700 | [diff] [blame] | 1090 | } |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 1091 | kfree(node); |
| 1092 | } |
| 1093 | |
Pavel Emelyanov | 4caf86c | 2008-05-07 19:47:01 +0400 | [diff] [blame] | 1094 | 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] | 1095 | { |
| 1096 | struct mesh_path *mpath; |
| 1097 | struct mpath_node *node, *new_node; |
| 1098 | u32 hash_idx; |
| 1099 | |
Pavel Emelyanov | 8566dc3 | 2008-05-07 19:51:51 +0400 | [diff] [blame] | 1100 | new_node = kmalloc(sizeof(struct mpath_node), GFP_ATOMIC); |
Pavel Emelyanov | 00242c4 | 2008-05-07 19:48:14 +0400 | [diff] [blame] | 1101 | if (new_node == NULL) |
| 1102 | return -ENOMEM; |
| 1103 | |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 1104 | node = hlist_entry(p, struct mpath_node, list); |
| 1105 | mpath = node->mpath; |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 1106 | new_node->mpath = mpath; |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 1107 | hash_idx = mesh_table_hash(mpath->dst, mpath->sdata, newtbl); |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 1108 | hlist_add_head(&new_node->list, |
| 1109 | &newtbl->hash_buckets[hash_idx]); |
Pavel Emelyanov | 4caf86c | 2008-05-07 19:47:01 +0400 | [diff] [blame] | 1110 | return 0; |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 1111 | } |
| 1112 | |
| 1113 | int mesh_pathtbl_init(void) |
| 1114 | { |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 1115 | struct mesh_table *tbl_path, *tbl_mpp; |
YanBo | 79617de | 2008-09-22 13:30:32 +0800 | [diff] [blame] | 1116 | |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 1117 | tbl_path = mesh_table_alloc(INIT_PATHS_SIZE_ORDER); |
| 1118 | if (!tbl_path) |
| 1119 | return -ENOMEM; |
| 1120 | tbl_path->free_node = &mesh_path_node_free; |
| 1121 | tbl_path->copy_node = &mesh_path_node_copy; |
| 1122 | tbl_path->mean_chain_len = MEAN_CHAIN_LEN; |
Javier Cardona | 5ee68e5 | 2011-08-09 16:45:08 -0700 | [diff] [blame] | 1123 | tbl_path->known_gates = kzalloc(sizeof(struct hlist_head), GFP_ATOMIC); |
| 1124 | INIT_HLIST_HEAD(tbl_path->known_gates); |
| 1125 | |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 1126 | |
| 1127 | tbl_mpp = mesh_table_alloc(INIT_PATHS_SIZE_ORDER); |
| 1128 | if (!tbl_mpp) { |
| 1129 | mesh_table_free(tbl_path, true); |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 1130 | return -ENOMEM; |
YanBo | 79617de | 2008-09-22 13:30:32 +0800 | [diff] [blame] | 1131 | } |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 1132 | tbl_mpp->free_node = &mesh_path_node_free; |
| 1133 | tbl_mpp->copy_node = &mesh_path_node_copy; |
| 1134 | tbl_mpp->mean_chain_len = MEAN_CHAIN_LEN; |
Javier Cardona | 5ee68e5 | 2011-08-09 16:45:08 -0700 | [diff] [blame] | 1135 | tbl_mpp->known_gates = kzalloc(sizeof(struct hlist_head), GFP_ATOMIC); |
| 1136 | INIT_HLIST_HEAD(tbl_mpp->known_gates); |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 1137 | |
| 1138 | /* Need no locking since this is during init */ |
| 1139 | RCU_INIT_POINTER(mesh_paths, tbl_path); |
| 1140 | RCU_INIT_POINTER(mpp_paths, tbl_mpp); |
YanBo | 79617de | 2008-09-22 13:30:32 +0800 | [diff] [blame] | 1141 | |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 1142 | return 0; |
| 1143 | } |
| 1144 | |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 1145 | void mesh_path_expire(struct ieee80211_sub_if_data *sdata) |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 1146 | { |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 1147 | struct mesh_table *tbl; |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 1148 | struct mesh_path *mpath; |
| 1149 | struct mpath_node *node; |
| 1150 | struct hlist_node *p; |
| 1151 | int i; |
| 1152 | |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 1153 | rcu_read_lock(); |
| 1154 | tbl = rcu_dereference(mesh_paths); |
| 1155 | for_each_mesh_entry(tbl, p, node, i) { |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 1156 | if (node->mpath->sdata != sdata) |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 1157 | continue; |
| 1158 | mpath = node->mpath; |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 1159 | if ((!(mpath->flags & MESH_PATH_RESOLVING)) && |
| 1160 | (!(mpath->flags & MESH_PATH_FIXED)) && |
Javier Cardona | f5e50cd | 2011-08-29 13:23:05 -0700 | [diff] [blame] | 1161 | time_after(jiffies, mpath->exp_time + MESH_PATH_EXPIRE)) |
Jasper Bryant-Greene | f698d85 | 2008-08-03 12:04:37 +1200 | [diff] [blame] | 1162 | mesh_path_del(mpath->dst, mpath->sdata); |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 1163 | rcu_read_unlock(); |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 1164 | } |
| 1165 | |
| 1166 | void mesh_pathtbl_unregister(void) |
| 1167 | { |
Johannes Berg | 349eb8c | 2011-05-14 11:56:16 +0200 | [diff] [blame] | 1168 | /* no need for locking during exit path */ |
| 1169 | mesh_table_free(rcu_dereference_raw(mesh_paths), true); |
| 1170 | mesh_table_free(rcu_dereference_raw(mpp_paths), true); |
Luis Carlos Cobo | eb2b931 | 2008-02-23 15:17:14 +0100 | [diff] [blame] | 1171 | } |