Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1 | /* |
Sven Eckelmann | 64afe35 | 2011-01-27 10:38:15 +0100 | [diff] [blame] | 2 | * Copyright (C) 2009-2011 B.A.T.M.A.N. contributors: |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 3 | * |
| 4 | * Marek Lindner, Simon Wunderlich |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of version 2 of the GNU General Public |
| 8 | * License as published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 18 | * 02110-1301, USA |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | /* increase the reference counter for this originator */ |
| 23 | |
| 24 | #include "main.h" |
| 25 | #include "originator.h" |
| 26 | #include "hash.h" |
| 27 | #include "translation-table.h" |
| 28 | #include "routing.h" |
| 29 | #include "gateway_client.h" |
| 30 | #include "hard-interface.h" |
| 31 | #include "unicast.h" |
| 32 | #include "soft-interface.h" |
| 33 | |
| 34 | static void purge_orig(struct work_struct *work); |
| 35 | |
| 36 | static void start_purge_timer(struct bat_priv *bat_priv) |
| 37 | { |
| 38 | INIT_DELAYED_WORK(&bat_priv->orig_work, purge_orig); |
| 39 | queue_delayed_work(bat_event_workqueue, &bat_priv->orig_work, 1 * HZ); |
| 40 | } |
| 41 | |
| 42 | int originator_init(struct bat_priv *bat_priv) |
| 43 | { |
| 44 | if (bat_priv->orig_hash) |
| 45 | return 1; |
| 46 | |
| 47 | spin_lock_bh(&bat_priv->orig_hash_lock); |
| 48 | bat_priv->orig_hash = hash_new(1024); |
| 49 | |
| 50 | if (!bat_priv->orig_hash) |
| 51 | goto err; |
| 52 | |
| 53 | spin_unlock_bh(&bat_priv->orig_hash_lock); |
| 54 | start_purge_timer(bat_priv); |
| 55 | return 1; |
| 56 | |
| 57 | err: |
| 58 | spin_unlock_bh(&bat_priv->orig_hash_lock); |
| 59 | return 0; |
| 60 | } |
| 61 | |
Marek Lindner | a8e7f4b | 2010-12-12 21:57:10 +0000 | [diff] [blame] | 62 | void neigh_node_free_ref(struct kref *refcount) |
| 63 | { |
| 64 | struct neigh_node *neigh_node; |
| 65 | |
| 66 | neigh_node = container_of(refcount, struct neigh_node, refcount); |
| 67 | kfree(neigh_node); |
| 68 | } |
| 69 | |
Marek Lindner | f987ed6 | 2010-12-12 21:57:12 +0000 | [diff] [blame^] | 70 | static void neigh_node_free_rcu(struct rcu_head *rcu) |
| 71 | { |
| 72 | struct neigh_node *neigh_node; |
| 73 | |
| 74 | neigh_node = container_of(rcu, struct neigh_node, rcu); |
| 75 | kref_put(&neigh_node->refcount, neigh_node_free_ref); |
| 76 | } |
| 77 | |
Marek Lindner | a8e7f4b | 2010-12-12 21:57:10 +0000 | [diff] [blame] | 78 | struct neigh_node *create_neighbor(struct orig_node *orig_node, |
| 79 | struct orig_node *orig_neigh_node, |
| 80 | uint8_t *neigh, |
| 81 | struct batman_if *if_incoming) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 82 | { |
| 83 | struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface); |
| 84 | struct neigh_node *neigh_node; |
| 85 | |
| 86 | bat_dbg(DBG_BATMAN, bat_priv, |
| 87 | "Creating new last-hop neighbor of originator\n"); |
| 88 | |
| 89 | neigh_node = kzalloc(sizeof(struct neigh_node), GFP_ATOMIC); |
| 90 | if (!neigh_node) |
| 91 | return NULL; |
| 92 | |
Marek Lindner | 9591a79 | 2010-12-12 21:57:11 +0000 | [diff] [blame] | 93 | INIT_HLIST_NODE(&neigh_node->list); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 94 | |
| 95 | memcpy(neigh_node->addr, neigh, ETH_ALEN); |
| 96 | neigh_node->orig_node = orig_neigh_node; |
| 97 | neigh_node->if_incoming = if_incoming; |
Marek Lindner | a8e7f4b | 2010-12-12 21:57:10 +0000 | [diff] [blame] | 98 | kref_init(&neigh_node->refcount); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 99 | |
Marek Lindner | f987ed6 | 2010-12-12 21:57:12 +0000 | [diff] [blame^] | 100 | spin_lock_bh(&orig_node->neigh_list_lock); |
| 101 | hlist_add_head_rcu(&neigh_node->list, &orig_node->neigh_list); |
| 102 | spin_unlock_bh(&orig_node->neigh_list_lock); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 103 | return neigh_node; |
| 104 | } |
| 105 | |
| 106 | static void free_orig_node(void *data, void *arg) |
| 107 | { |
Marek Lindner | 9591a79 | 2010-12-12 21:57:11 +0000 | [diff] [blame] | 108 | struct hlist_node *node, *node_tmp; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 109 | struct neigh_node *neigh_node; |
| 110 | struct orig_node *orig_node = (struct orig_node *)data; |
| 111 | struct bat_priv *bat_priv = (struct bat_priv *)arg; |
| 112 | |
Marek Lindner | f987ed6 | 2010-12-12 21:57:12 +0000 | [diff] [blame^] | 113 | spin_lock_bh(&orig_node->neigh_list_lock); |
| 114 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 115 | /* for all neighbors towards this originator ... */ |
Marek Lindner | 9591a79 | 2010-12-12 21:57:11 +0000 | [diff] [blame] | 116 | hlist_for_each_entry_safe(neigh_node, node, node_tmp, |
| 117 | &orig_node->neigh_list, list) { |
Marek Lindner | f987ed6 | 2010-12-12 21:57:12 +0000 | [diff] [blame^] | 118 | hlist_del_rcu(&neigh_node->list); |
| 119 | call_rcu(&neigh_node->rcu, neigh_node_free_rcu); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Marek Lindner | f987ed6 | 2010-12-12 21:57:12 +0000 | [diff] [blame^] | 122 | spin_unlock_bh(&orig_node->neigh_list_lock); |
| 123 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 124 | frag_list_free(&orig_node->frag_list); |
| 125 | hna_global_del_orig(bat_priv, orig_node, "originator timed out"); |
| 126 | |
| 127 | kfree(orig_node->bcast_own); |
| 128 | kfree(orig_node->bcast_own_sum); |
| 129 | kfree(orig_node); |
| 130 | } |
| 131 | |
| 132 | void originator_free(struct bat_priv *bat_priv) |
| 133 | { |
| 134 | if (!bat_priv->orig_hash) |
| 135 | return; |
| 136 | |
| 137 | cancel_delayed_work_sync(&bat_priv->orig_work); |
| 138 | |
| 139 | spin_lock_bh(&bat_priv->orig_hash_lock); |
| 140 | hash_delete(bat_priv->orig_hash, free_orig_node, bat_priv); |
| 141 | bat_priv->orig_hash = NULL; |
| 142 | spin_unlock_bh(&bat_priv->orig_hash_lock); |
| 143 | } |
| 144 | |
| 145 | /* this function finds or creates an originator entry for the given |
| 146 | * address if it does not exits */ |
| 147 | struct orig_node *get_orig_node(struct bat_priv *bat_priv, uint8_t *addr) |
| 148 | { |
| 149 | struct orig_node *orig_node; |
| 150 | int size; |
| 151 | int hash_added; |
| 152 | |
| 153 | orig_node = ((struct orig_node *)hash_find(bat_priv->orig_hash, |
| 154 | compare_orig, choose_orig, |
| 155 | addr)); |
| 156 | |
| 157 | if (orig_node) |
| 158 | return orig_node; |
| 159 | |
| 160 | bat_dbg(DBG_BATMAN, bat_priv, |
| 161 | "Creating new originator: %pM\n", addr); |
| 162 | |
| 163 | orig_node = kzalloc(sizeof(struct orig_node), GFP_ATOMIC); |
| 164 | if (!orig_node) |
| 165 | return NULL; |
| 166 | |
Marek Lindner | 9591a79 | 2010-12-12 21:57:11 +0000 | [diff] [blame] | 167 | INIT_HLIST_HEAD(&orig_node->neigh_list); |
Marek Lindner | f987ed6 | 2010-12-12 21:57:12 +0000 | [diff] [blame^] | 168 | spin_lock_init(&orig_node->neigh_list_lock); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 169 | |
| 170 | memcpy(orig_node->orig, addr, ETH_ALEN); |
| 171 | orig_node->router = NULL; |
| 172 | orig_node->hna_buff = NULL; |
| 173 | orig_node->bcast_seqno_reset = jiffies - 1 |
| 174 | - msecs_to_jiffies(RESET_PROTECTION_MS); |
| 175 | orig_node->batman_seqno_reset = jiffies - 1 |
| 176 | - msecs_to_jiffies(RESET_PROTECTION_MS); |
| 177 | |
| 178 | size = bat_priv->num_ifaces * sizeof(unsigned long) * NUM_WORDS; |
| 179 | |
| 180 | orig_node->bcast_own = kzalloc(size, GFP_ATOMIC); |
| 181 | if (!orig_node->bcast_own) |
| 182 | goto free_orig_node; |
| 183 | |
| 184 | size = bat_priv->num_ifaces * sizeof(uint8_t); |
| 185 | orig_node->bcast_own_sum = kzalloc(size, GFP_ATOMIC); |
| 186 | |
| 187 | INIT_LIST_HEAD(&orig_node->frag_list); |
| 188 | orig_node->last_frag_packet = 0; |
| 189 | |
| 190 | if (!orig_node->bcast_own_sum) |
| 191 | goto free_bcast_own; |
| 192 | |
| 193 | hash_added = hash_add(bat_priv->orig_hash, compare_orig, choose_orig, |
| 194 | orig_node); |
| 195 | if (hash_added < 0) |
| 196 | goto free_bcast_own_sum; |
| 197 | |
| 198 | return orig_node; |
| 199 | free_bcast_own_sum: |
| 200 | kfree(orig_node->bcast_own_sum); |
| 201 | free_bcast_own: |
| 202 | kfree(orig_node->bcast_own); |
| 203 | free_orig_node: |
| 204 | kfree(orig_node); |
| 205 | return NULL; |
| 206 | } |
| 207 | |
| 208 | static bool purge_orig_neighbors(struct bat_priv *bat_priv, |
| 209 | struct orig_node *orig_node, |
| 210 | struct neigh_node **best_neigh_node) |
| 211 | { |
Marek Lindner | 9591a79 | 2010-12-12 21:57:11 +0000 | [diff] [blame] | 212 | struct hlist_node *node, *node_tmp; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 213 | struct neigh_node *neigh_node; |
| 214 | bool neigh_purged = false; |
| 215 | |
| 216 | *best_neigh_node = NULL; |
| 217 | |
Marek Lindner | f987ed6 | 2010-12-12 21:57:12 +0000 | [diff] [blame^] | 218 | spin_lock_bh(&orig_node->neigh_list_lock); |
| 219 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 220 | /* for all neighbors towards this originator ... */ |
Marek Lindner | 9591a79 | 2010-12-12 21:57:11 +0000 | [diff] [blame] | 221 | hlist_for_each_entry_safe(neigh_node, node, node_tmp, |
| 222 | &orig_node->neigh_list, list) { |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 223 | |
| 224 | if ((time_after(jiffies, |
| 225 | neigh_node->last_valid + PURGE_TIMEOUT * HZ)) || |
| 226 | (neigh_node->if_incoming->if_status == IF_INACTIVE) || |
| 227 | (neigh_node->if_incoming->if_status == IF_TO_BE_REMOVED)) { |
| 228 | |
| 229 | if (neigh_node->if_incoming->if_status == |
| 230 | IF_TO_BE_REMOVED) |
| 231 | bat_dbg(DBG_BATMAN, bat_priv, |
| 232 | "neighbor purge: originator %pM, " |
| 233 | "neighbor: %pM, iface: %s\n", |
| 234 | orig_node->orig, neigh_node->addr, |
| 235 | neigh_node->if_incoming->net_dev->name); |
| 236 | else |
| 237 | bat_dbg(DBG_BATMAN, bat_priv, |
| 238 | "neighbor timeout: originator %pM, " |
| 239 | "neighbor: %pM, last_valid: %lu\n", |
| 240 | orig_node->orig, neigh_node->addr, |
| 241 | (neigh_node->last_valid / HZ)); |
| 242 | |
| 243 | neigh_purged = true; |
Marek Lindner | 9591a79 | 2010-12-12 21:57:11 +0000 | [diff] [blame] | 244 | |
Marek Lindner | f987ed6 | 2010-12-12 21:57:12 +0000 | [diff] [blame^] | 245 | hlist_del_rcu(&neigh_node->list); |
| 246 | call_rcu(&neigh_node->rcu, neigh_node_free_rcu); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 247 | } else { |
| 248 | if ((!*best_neigh_node) || |
| 249 | (neigh_node->tq_avg > (*best_neigh_node)->tq_avg)) |
| 250 | *best_neigh_node = neigh_node; |
| 251 | } |
| 252 | } |
Marek Lindner | f987ed6 | 2010-12-12 21:57:12 +0000 | [diff] [blame^] | 253 | |
| 254 | spin_unlock_bh(&orig_node->neigh_list_lock); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 255 | return neigh_purged; |
| 256 | } |
| 257 | |
| 258 | static bool purge_orig_node(struct bat_priv *bat_priv, |
| 259 | struct orig_node *orig_node) |
| 260 | { |
| 261 | struct neigh_node *best_neigh_node; |
| 262 | |
| 263 | if (time_after(jiffies, |
| 264 | orig_node->last_valid + 2 * PURGE_TIMEOUT * HZ)) { |
| 265 | |
| 266 | bat_dbg(DBG_BATMAN, bat_priv, |
| 267 | "Originator timeout: originator %pM, last_valid %lu\n", |
| 268 | orig_node->orig, (orig_node->last_valid / HZ)); |
| 269 | return true; |
| 270 | } else { |
| 271 | if (purge_orig_neighbors(bat_priv, orig_node, |
| 272 | &best_neigh_node)) { |
| 273 | update_routes(bat_priv, orig_node, |
| 274 | best_neigh_node, |
| 275 | orig_node->hna_buff, |
| 276 | orig_node->hna_buff_len); |
| 277 | /* update bonding candidates, we could have lost |
| 278 | * some candidates. */ |
Simon Wunderlich | 74ef115 | 2010-12-29 16:15:19 +0000 | [diff] [blame] | 279 | update_bonding_candidates(orig_node); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 280 | } |
| 281 | } |
| 282 | |
| 283 | return false; |
| 284 | } |
| 285 | |
| 286 | static void _purge_orig(struct bat_priv *bat_priv) |
| 287 | { |
| 288 | struct hashtable_t *hash = bat_priv->orig_hash; |
| 289 | struct hlist_node *walk, *safe; |
| 290 | struct hlist_head *head; |
| 291 | struct element_t *bucket; |
| 292 | struct orig_node *orig_node; |
| 293 | int i; |
| 294 | |
| 295 | if (!hash) |
| 296 | return; |
| 297 | |
| 298 | spin_lock_bh(&bat_priv->orig_hash_lock); |
| 299 | |
| 300 | /* for all origins... */ |
| 301 | for (i = 0; i < hash->size; i++) { |
| 302 | head = &hash->table[i]; |
| 303 | |
| 304 | hlist_for_each_entry_safe(bucket, walk, safe, head, hlist) { |
| 305 | orig_node = bucket->data; |
| 306 | |
| 307 | if (purge_orig_node(bat_priv, orig_node)) { |
| 308 | if (orig_node->gw_flags) |
| 309 | gw_node_delete(bat_priv, orig_node); |
| 310 | hlist_del(walk); |
| 311 | kfree(bucket); |
| 312 | free_orig_node(orig_node, bat_priv); |
| 313 | } |
| 314 | |
| 315 | if (time_after(jiffies, orig_node->last_frag_packet + |
| 316 | msecs_to_jiffies(FRAG_TIMEOUT))) |
| 317 | frag_list_free(&orig_node->frag_list); |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | spin_unlock_bh(&bat_priv->orig_hash_lock); |
| 322 | |
| 323 | gw_node_purge(bat_priv); |
| 324 | gw_election(bat_priv); |
| 325 | |
| 326 | softif_neigh_purge(bat_priv); |
| 327 | } |
| 328 | |
| 329 | static void purge_orig(struct work_struct *work) |
| 330 | { |
| 331 | struct delayed_work *delayed_work = |
| 332 | container_of(work, struct delayed_work, work); |
| 333 | struct bat_priv *bat_priv = |
| 334 | container_of(delayed_work, struct bat_priv, orig_work); |
| 335 | |
| 336 | _purge_orig(bat_priv); |
| 337 | start_purge_timer(bat_priv); |
| 338 | } |
| 339 | |
| 340 | void purge_orig_ref(struct bat_priv *bat_priv) |
| 341 | { |
| 342 | _purge_orig(bat_priv); |
| 343 | } |
| 344 | |
| 345 | int orig_seq_print_text(struct seq_file *seq, void *offset) |
| 346 | { |
| 347 | struct net_device *net_dev = (struct net_device *)seq->private; |
| 348 | struct bat_priv *bat_priv = netdev_priv(net_dev); |
| 349 | struct hashtable_t *hash = bat_priv->orig_hash; |
Marek Lindner | 9591a79 | 2010-12-12 21:57:11 +0000 | [diff] [blame] | 350 | struct hlist_node *walk, *node; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 351 | struct hlist_head *head; |
| 352 | struct element_t *bucket; |
| 353 | struct orig_node *orig_node; |
| 354 | struct neigh_node *neigh_node; |
| 355 | int batman_count = 0; |
| 356 | int last_seen_secs; |
| 357 | int last_seen_msecs; |
| 358 | int i; |
| 359 | |
| 360 | if ((!bat_priv->primary_if) || |
| 361 | (bat_priv->primary_if->if_status != IF_ACTIVE)) { |
| 362 | if (!bat_priv->primary_if) |
| 363 | return seq_printf(seq, "BATMAN mesh %s disabled - " |
| 364 | "please specify interfaces to enable it\n", |
| 365 | net_dev->name); |
| 366 | |
| 367 | return seq_printf(seq, "BATMAN mesh %s " |
| 368 | "disabled - primary interface not active\n", |
| 369 | net_dev->name); |
| 370 | } |
| 371 | |
| 372 | seq_printf(seq, "[B.A.T.M.A.N. adv %s%s, MainIF/MAC: %s/%pM (%s)]\n", |
| 373 | SOURCE_VERSION, REVISION_VERSION_STR, |
| 374 | bat_priv->primary_if->net_dev->name, |
| 375 | bat_priv->primary_if->net_dev->dev_addr, net_dev->name); |
| 376 | seq_printf(seq, " %-15s %s (%s/%i) %17s [%10s]: %20s ...\n", |
| 377 | "Originator", "last-seen", "#", TQ_MAX_VALUE, "Nexthop", |
| 378 | "outgoingIF", "Potential nexthops"); |
| 379 | |
| 380 | spin_lock_bh(&bat_priv->orig_hash_lock); |
| 381 | |
| 382 | for (i = 0; i < hash->size; i++) { |
| 383 | head = &hash->table[i]; |
| 384 | |
| 385 | hlist_for_each_entry(bucket, walk, head, hlist) { |
| 386 | orig_node = bucket->data; |
| 387 | |
| 388 | if (!orig_node->router) |
| 389 | continue; |
| 390 | |
| 391 | if (orig_node->router->tq_avg == 0) |
| 392 | continue; |
| 393 | |
| 394 | last_seen_secs = jiffies_to_msecs(jiffies - |
| 395 | orig_node->last_valid) / 1000; |
| 396 | last_seen_msecs = jiffies_to_msecs(jiffies - |
| 397 | orig_node->last_valid) % 1000; |
| 398 | |
| 399 | neigh_node = orig_node->router; |
| 400 | seq_printf(seq, "%pM %4i.%03is (%3i) %pM [%10s]:", |
| 401 | orig_node->orig, last_seen_secs, |
| 402 | last_seen_msecs, neigh_node->tq_avg, |
| 403 | neigh_node->addr, |
| 404 | neigh_node->if_incoming->net_dev->name); |
| 405 | |
Marek Lindner | f987ed6 | 2010-12-12 21:57:12 +0000 | [diff] [blame^] | 406 | rcu_read_lock(); |
| 407 | hlist_for_each_entry_rcu(neigh_node, node, |
| 408 | &orig_node->neigh_list, list) { |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 409 | seq_printf(seq, " %pM (%3i)", neigh_node->addr, |
| 410 | neigh_node->tq_avg); |
| 411 | } |
Marek Lindner | f987ed6 | 2010-12-12 21:57:12 +0000 | [diff] [blame^] | 412 | rcu_read_unlock(); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 413 | |
| 414 | seq_printf(seq, "\n"); |
| 415 | batman_count++; |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | spin_unlock_bh(&bat_priv->orig_hash_lock); |
| 420 | |
| 421 | if ((batman_count == 0)) |
| 422 | seq_printf(seq, "No batman nodes in range ...\n"); |
| 423 | |
| 424 | return 0; |
| 425 | } |
| 426 | |
| 427 | static int orig_node_add_if(struct orig_node *orig_node, int max_if_num) |
| 428 | { |
| 429 | void *data_ptr; |
| 430 | |
| 431 | data_ptr = kmalloc(max_if_num * sizeof(unsigned long) * NUM_WORDS, |
| 432 | GFP_ATOMIC); |
| 433 | if (!data_ptr) { |
| 434 | pr_err("Can't resize orig: out of memory\n"); |
| 435 | return -1; |
| 436 | } |
| 437 | |
| 438 | memcpy(data_ptr, orig_node->bcast_own, |
| 439 | (max_if_num - 1) * sizeof(unsigned long) * NUM_WORDS); |
| 440 | kfree(orig_node->bcast_own); |
| 441 | orig_node->bcast_own = data_ptr; |
| 442 | |
| 443 | data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC); |
| 444 | if (!data_ptr) { |
| 445 | pr_err("Can't resize orig: out of memory\n"); |
| 446 | return -1; |
| 447 | } |
| 448 | |
| 449 | memcpy(data_ptr, orig_node->bcast_own_sum, |
| 450 | (max_if_num - 1) * sizeof(uint8_t)); |
| 451 | kfree(orig_node->bcast_own_sum); |
| 452 | orig_node->bcast_own_sum = data_ptr; |
| 453 | |
| 454 | return 0; |
| 455 | } |
| 456 | |
| 457 | int orig_hash_add_if(struct batman_if *batman_if, int max_if_num) |
| 458 | { |
| 459 | struct bat_priv *bat_priv = netdev_priv(batman_if->soft_iface); |
| 460 | struct hashtable_t *hash = bat_priv->orig_hash; |
| 461 | struct hlist_node *walk; |
| 462 | struct hlist_head *head; |
| 463 | struct element_t *bucket; |
| 464 | struct orig_node *orig_node; |
| 465 | int i; |
| 466 | |
| 467 | /* resize all orig nodes because orig_node->bcast_own(_sum) depend on |
| 468 | * if_num */ |
| 469 | spin_lock_bh(&bat_priv->orig_hash_lock); |
| 470 | |
| 471 | for (i = 0; i < hash->size; i++) { |
| 472 | head = &hash->table[i]; |
| 473 | |
| 474 | hlist_for_each_entry(bucket, walk, head, hlist) { |
| 475 | orig_node = bucket->data; |
| 476 | |
| 477 | if (orig_node_add_if(orig_node, max_if_num) == -1) |
| 478 | goto err; |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | spin_unlock_bh(&bat_priv->orig_hash_lock); |
| 483 | return 0; |
| 484 | |
| 485 | err: |
| 486 | spin_unlock_bh(&bat_priv->orig_hash_lock); |
| 487 | return -ENOMEM; |
| 488 | } |
| 489 | |
| 490 | static int orig_node_del_if(struct orig_node *orig_node, |
| 491 | int max_if_num, int del_if_num) |
| 492 | { |
| 493 | void *data_ptr = NULL; |
| 494 | int chunk_size; |
| 495 | |
| 496 | /* last interface was removed */ |
| 497 | if (max_if_num == 0) |
| 498 | goto free_bcast_own; |
| 499 | |
| 500 | chunk_size = sizeof(unsigned long) * NUM_WORDS; |
| 501 | data_ptr = kmalloc(max_if_num * chunk_size, GFP_ATOMIC); |
| 502 | if (!data_ptr) { |
| 503 | pr_err("Can't resize orig: out of memory\n"); |
| 504 | return -1; |
| 505 | } |
| 506 | |
| 507 | /* copy first part */ |
| 508 | memcpy(data_ptr, orig_node->bcast_own, del_if_num * chunk_size); |
| 509 | |
| 510 | /* copy second part */ |
| 511 | memcpy(data_ptr + del_if_num * chunk_size, |
| 512 | orig_node->bcast_own + ((del_if_num + 1) * chunk_size), |
| 513 | (max_if_num - del_if_num) * chunk_size); |
| 514 | |
| 515 | free_bcast_own: |
| 516 | kfree(orig_node->bcast_own); |
| 517 | orig_node->bcast_own = data_ptr; |
| 518 | |
| 519 | if (max_if_num == 0) |
| 520 | goto free_own_sum; |
| 521 | |
| 522 | data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC); |
| 523 | if (!data_ptr) { |
| 524 | pr_err("Can't resize orig: out of memory\n"); |
| 525 | return -1; |
| 526 | } |
| 527 | |
| 528 | memcpy(data_ptr, orig_node->bcast_own_sum, |
| 529 | del_if_num * sizeof(uint8_t)); |
| 530 | |
| 531 | memcpy(data_ptr + del_if_num * sizeof(uint8_t), |
| 532 | orig_node->bcast_own_sum + ((del_if_num + 1) * sizeof(uint8_t)), |
| 533 | (max_if_num - del_if_num) * sizeof(uint8_t)); |
| 534 | |
| 535 | free_own_sum: |
| 536 | kfree(orig_node->bcast_own_sum); |
| 537 | orig_node->bcast_own_sum = data_ptr; |
| 538 | |
| 539 | return 0; |
| 540 | } |
| 541 | |
| 542 | int orig_hash_del_if(struct batman_if *batman_if, int max_if_num) |
| 543 | { |
| 544 | struct bat_priv *bat_priv = netdev_priv(batman_if->soft_iface); |
| 545 | struct hashtable_t *hash = bat_priv->orig_hash; |
| 546 | struct hlist_node *walk; |
| 547 | struct hlist_head *head; |
| 548 | struct element_t *bucket; |
| 549 | struct batman_if *batman_if_tmp; |
| 550 | struct orig_node *orig_node; |
| 551 | int i, ret; |
| 552 | |
| 553 | /* resize all orig nodes because orig_node->bcast_own(_sum) depend on |
| 554 | * if_num */ |
| 555 | spin_lock_bh(&bat_priv->orig_hash_lock); |
| 556 | |
| 557 | for (i = 0; i < hash->size; i++) { |
| 558 | head = &hash->table[i]; |
| 559 | |
| 560 | hlist_for_each_entry(bucket, walk, head, hlist) { |
| 561 | orig_node = bucket->data; |
| 562 | |
| 563 | ret = orig_node_del_if(orig_node, max_if_num, |
| 564 | batman_if->if_num); |
| 565 | |
| 566 | if (ret == -1) |
| 567 | goto err; |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | /* renumber remaining batman interfaces _inside_ of orig_hash_lock */ |
| 572 | rcu_read_lock(); |
| 573 | list_for_each_entry_rcu(batman_if_tmp, &if_list, list) { |
| 574 | if (batman_if_tmp->if_status == IF_NOT_IN_USE) |
| 575 | continue; |
| 576 | |
| 577 | if (batman_if == batman_if_tmp) |
| 578 | continue; |
| 579 | |
| 580 | if (batman_if->soft_iface != batman_if_tmp->soft_iface) |
| 581 | continue; |
| 582 | |
| 583 | if (batman_if_tmp->if_num > batman_if->if_num) |
| 584 | batman_if_tmp->if_num--; |
| 585 | } |
| 586 | rcu_read_unlock(); |
| 587 | |
| 588 | batman_if->if_num = -1; |
| 589 | spin_unlock_bh(&bat_priv->orig_hash_lock); |
| 590 | return 0; |
| 591 | |
| 592 | err: |
| 593 | spin_unlock_bh(&bat_priv->orig_hash_lock); |
| 594 | return -ENOMEM; |
| 595 | } |