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