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