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 | f987ed6 | 2010-12-12 21:57:12 +0000 | [diff] [blame] | 62 | static void neigh_node_free_rcu(struct rcu_head *rcu) |
| 63 | { |
| 64 | struct neigh_node *neigh_node; |
| 65 | |
| 66 | neigh_node = container_of(rcu, struct neigh_node, rcu); |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 67 | kfree(neigh_node); |
Marek Lindner | f987ed6 | 2010-12-12 21:57:12 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 70 | void neigh_node_free_ref(struct neigh_node *neigh_node) |
Simon Wunderlich | a4c135c | 2011-01-19 20:01:43 +0000 | [diff] [blame] | 71 | { |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 72 | if (atomic_dec_and_test(&neigh_node->refcount)) |
| 73 | call_rcu(&neigh_node->rcu, neigh_node_free_rcu); |
Simon Wunderlich | a4c135c | 2011-01-19 20:01:43 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Marek Lindner | a8e7f4b | 2010-12-12 21:57:10 +0000 | [diff] [blame] | 76 | struct neigh_node *create_neighbor(struct orig_node *orig_node, |
| 77 | struct orig_node *orig_neigh_node, |
| 78 | uint8_t *neigh, |
| 79 | struct batman_if *if_incoming) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 80 | { |
| 81 | struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface); |
| 82 | struct neigh_node *neigh_node; |
| 83 | |
| 84 | bat_dbg(DBG_BATMAN, bat_priv, |
| 85 | "Creating new last-hop neighbor of originator\n"); |
| 86 | |
| 87 | neigh_node = kzalloc(sizeof(struct neigh_node), GFP_ATOMIC); |
| 88 | if (!neigh_node) |
| 89 | return NULL; |
| 90 | |
Marek Lindner | 9591a79 | 2010-12-12 21:57:11 +0000 | [diff] [blame] | 91 | INIT_HLIST_NODE(&neigh_node->list); |
Simon Wunderlich | a4c135c | 2011-01-19 20:01:43 +0000 | [diff] [blame] | 92 | INIT_LIST_HEAD(&neigh_node->bonding_list); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 93 | |
| 94 | memcpy(neigh_node->addr, neigh, ETH_ALEN); |
| 95 | neigh_node->orig_node = orig_neigh_node; |
| 96 | neigh_node->if_incoming = if_incoming; |
Marek Lindner | 1605d0d | 2011-02-18 12:28:11 +0000 | [diff] [blame^] | 97 | |
| 98 | /* extra reference for return */ |
| 99 | atomic_set(&neigh_node->refcount, 2); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 100 | |
Marek Lindner | f987ed6 | 2010-12-12 21:57:12 +0000 | [diff] [blame] | 101 | spin_lock_bh(&orig_node->neigh_list_lock); |
| 102 | hlist_add_head_rcu(&neigh_node->list, &orig_node->neigh_list); |
| 103 | spin_unlock_bh(&orig_node->neigh_list_lock); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 104 | return neigh_node; |
| 105 | } |
| 106 | |
Marek Lindner | 7b36e8e | 2011-02-18 12:28:10 +0000 | [diff] [blame] | 107 | static void orig_node_free_rcu(struct rcu_head *rcu) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 108 | { |
Marek Lindner | 9591a79 | 2010-12-12 21:57:11 +0000 | [diff] [blame] | 109 | struct hlist_node *node, *node_tmp; |
Simon Wunderlich | a4c135c | 2011-01-19 20:01:43 +0000 | [diff] [blame] | 110 | struct neigh_node *neigh_node, *tmp_neigh_node; |
Marek Lindner | 16b1aba | 2011-01-19 20:01:42 +0000 | [diff] [blame] | 111 | struct orig_node *orig_node; |
| 112 | |
Marek Lindner | 7b36e8e | 2011-02-18 12:28:10 +0000 | [diff] [blame] | 113 | orig_node = container_of(rcu, struct orig_node, rcu); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 114 | |
Marek Lindner | f987ed6 | 2010-12-12 21:57:12 +0000 | [diff] [blame] | 115 | spin_lock_bh(&orig_node->neigh_list_lock); |
| 116 | |
Simon Wunderlich | a4c135c | 2011-01-19 20:01:43 +0000 | [diff] [blame] | 117 | /* for all bonding members ... */ |
| 118 | list_for_each_entry_safe(neigh_node, tmp_neigh_node, |
| 119 | &orig_node->bond_list, bonding_list) { |
| 120 | list_del_rcu(&neigh_node->bonding_list); |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 121 | neigh_node_free_ref(neigh_node); |
Simon Wunderlich | a4c135c | 2011-01-19 20:01:43 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 124 | /* for all neighbors towards this originator ... */ |
Marek Lindner | 9591a79 | 2010-12-12 21:57:11 +0000 | [diff] [blame] | 125 | hlist_for_each_entry_safe(neigh_node, node, node_tmp, |
| 126 | &orig_node->neigh_list, list) { |
Marek Lindner | f987ed6 | 2010-12-12 21:57:12 +0000 | [diff] [blame] | 127 | hlist_del_rcu(&neigh_node->list); |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 128 | neigh_node_free_ref(neigh_node); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Marek Lindner | f987ed6 | 2010-12-12 21:57:12 +0000 | [diff] [blame] | 131 | spin_unlock_bh(&orig_node->neigh_list_lock); |
| 132 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 133 | frag_list_free(&orig_node->frag_list); |
Marek Lindner | 16b1aba | 2011-01-19 20:01:42 +0000 | [diff] [blame] | 134 | hna_global_del_orig(orig_node->bat_priv, orig_node, |
| 135 | "originator timed out"); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 136 | |
| 137 | kfree(orig_node->bcast_own); |
| 138 | kfree(orig_node->bcast_own_sum); |
| 139 | kfree(orig_node); |
| 140 | } |
| 141 | |
Marek Lindner | 7b36e8e | 2011-02-18 12:28:10 +0000 | [diff] [blame] | 142 | void orig_node_free_ref(struct orig_node *orig_node) |
| 143 | { |
| 144 | if (atomic_dec_and_test(&orig_node->refcount)) |
| 145 | call_rcu(&orig_node->rcu, orig_node_free_rcu); |
| 146 | } |
| 147 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 148 | void originator_free(struct bat_priv *bat_priv) |
| 149 | { |
Marek Lindner | 16b1aba | 2011-01-19 20:01:42 +0000 | [diff] [blame] | 150 | struct hashtable_t *hash = bat_priv->orig_hash; |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 151 | struct hlist_node *node, *node_tmp; |
Marek Lindner | 16b1aba | 2011-01-19 20:01:42 +0000 | [diff] [blame] | 152 | struct hlist_head *head; |
Marek Lindner | 16b1aba | 2011-01-19 20:01:42 +0000 | [diff] [blame] | 153 | spinlock_t *list_lock; /* spinlock to protect write access */ |
| 154 | struct orig_node *orig_node; |
| 155 | int i; |
| 156 | |
| 157 | if (!hash) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 158 | return; |
| 159 | |
| 160 | cancel_delayed_work_sync(&bat_priv->orig_work); |
| 161 | |
| 162 | spin_lock_bh(&bat_priv->orig_hash_lock); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 163 | bat_priv->orig_hash = NULL; |
Marek Lindner | 16b1aba | 2011-01-19 20:01:42 +0000 | [diff] [blame] | 164 | |
| 165 | for (i = 0; i < hash->size; i++) { |
| 166 | head = &hash->table[i]; |
| 167 | list_lock = &hash->list_locks[i]; |
| 168 | |
| 169 | spin_lock_bh(list_lock); |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 170 | hlist_for_each_entry_safe(orig_node, node, node_tmp, |
| 171 | head, hash_entry) { |
Marek Lindner | 16b1aba | 2011-01-19 20:01:42 +0000 | [diff] [blame] | 172 | |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 173 | hlist_del_rcu(node); |
Marek Lindner | 7b36e8e | 2011-02-18 12:28:10 +0000 | [diff] [blame] | 174 | orig_node_free_ref(orig_node); |
Marek Lindner | 16b1aba | 2011-01-19 20:01:42 +0000 | [diff] [blame] | 175 | } |
| 176 | spin_unlock_bh(list_lock); |
| 177 | } |
| 178 | |
| 179 | hash_destroy(hash); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 180 | spin_unlock_bh(&bat_priv->orig_hash_lock); |
| 181 | } |
| 182 | |
| 183 | /* this function finds or creates an originator entry for the given |
| 184 | * address if it does not exits */ |
| 185 | struct orig_node *get_orig_node(struct bat_priv *bat_priv, uint8_t *addr) |
| 186 | { |
| 187 | struct orig_node *orig_node; |
| 188 | int size; |
| 189 | int hash_added; |
| 190 | |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 191 | orig_node = orig_hash_find(bat_priv, addr); |
| 192 | if (orig_node) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 193 | return orig_node; |
| 194 | |
| 195 | bat_dbg(DBG_BATMAN, bat_priv, |
| 196 | "Creating new originator: %pM\n", addr); |
| 197 | |
| 198 | orig_node = kzalloc(sizeof(struct orig_node), GFP_ATOMIC); |
| 199 | if (!orig_node) |
| 200 | return NULL; |
| 201 | |
Marek Lindner | 9591a79 | 2010-12-12 21:57:11 +0000 | [diff] [blame] | 202 | INIT_HLIST_HEAD(&orig_node->neigh_list); |
Simon Wunderlich | a4c135c | 2011-01-19 20:01:43 +0000 | [diff] [blame] | 203 | INIT_LIST_HEAD(&orig_node->bond_list); |
Marek Lindner | 2ae2daf | 2011-01-19 20:01:42 +0000 | [diff] [blame] | 204 | spin_lock_init(&orig_node->ogm_cnt_lock); |
Marek Lindner | f3e0008 | 2011-01-25 21:52:11 +0000 | [diff] [blame] | 205 | spin_lock_init(&orig_node->bcast_seqno_lock); |
Marek Lindner | f987ed6 | 2010-12-12 21:57:12 +0000 | [diff] [blame] | 206 | spin_lock_init(&orig_node->neigh_list_lock); |
Marek Lindner | 7b36e8e | 2011-02-18 12:28:10 +0000 | [diff] [blame] | 207 | |
| 208 | /* extra reference for return */ |
| 209 | atomic_set(&orig_node->refcount, 2); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 210 | |
Marek Lindner | 16b1aba | 2011-01-19 20:01:42 +0000 | [diff] [blame] | 211 | orig_node->bat_priv = bat_priv; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 212 | memcpy(orig_node->orig, addr, ETH_ALEN); |
| 213 | orig_node->router = NULL; |
| 214 | orig_node->hna_buff = NULL; |
| 215 | orig_node->bcast_seqno_reset = jiffies - 1 |
| 216 | - msecs_to_jiffies(RESET_PROTECTION_MS); |
| 217 | orig_node->batman_seqno_reset = jiffies - 1 |
| 218 | - msecs_to_jiffies(RESET_PROTECTION_MS); |
| 219 | |
Simon Wunderlich | a4c135c | 2011-01-19 20:01:43 +0000 | [diff] [blame] | 220 | atomic_set(&orig_node->bond_candidates, 0); |
| 221 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 222 | size = bat_priv->num_ifaces * sizeof(unsigned long) * NUM_WORDS; |
| 223 | |
| 224 | orig_node->bcast_own = kzalloc(size, GFP_ATOMIC); |
| 225 | if (!orig_node->bcast_own) |
| 226 | goto free_orig_node; |
| 227 | |
| 228 | size = bat_priv->num_ifaces * sizeof(uint8_t); |
| 229 | orig_node->bcast_own_sum = kzalloc(size, GFP_ATOMIC); |
| 230 | |
| 231 | INIT_LIST_HEAD(&orig_node->frag_list); |
| 232 | orig_node->last_frag_packet = 0; |
| 233 | |
| 234 | if (!orig_node->bcast_own_sum) |
| 235 | goto free_bcast_own; |
| 236 | |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 237 | hash_added = hash_add(bat_priv->orig_hash, compare_orig, |
| 238 | choose_orig, orig_node, &orig_node->hash_entry); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 239 | if (hash_added < 0) |
| 240 | goto free_bcast_own_sum; |
| 241 | |
| 242 | return orig_node; |
| 243 | free_bcast_own_sum: |
| 244 | kfree(orig_node->bcast_own_sum); |
| 245 | free_bcast_own: |
| 246 | kfree(orig_node->bcast_own); |
| 247 | free_orig_node: |
| 248 | kfree(orig_node); |
| 249 | return NULL; |
| 250 | } |
| 251 | |
| 252 | static bool purge_orig_neighbors(struct bat_priv *bat_priv, |
| 253 | struct orig_node *orig_node, |
| 254 | struct neigh_node **best_neigh_node) |
| 255 | { |
Marek Lindner | 9591a79 | 2010-12-12 21:57:11 +0000 | [diff] [blame] | 256 | struct hlist_node *node, *node_tmp; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 257 | struct neigh_node *neigh_node; |
| 258 | bool neigh_purged = false; |
| 259 | |
| 260 | *best_neigh_node = NULL; |
| 261 | |
Marek Lindner | f987ed6 | 2010-12-12 21:57:12 +0000 | [diff] [blame] | 262 | spin_lock_bh(&orig_node->neigh_list_lock); |
| 263 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 264 | /* for all neighbors towards this originator ... */ |
Marek Lindner | 9591a79 | 2010-12-12 21:57:11 +0000 | [diff] [blame] | 265 | hlist_for_each_entry_safe(neigh_node, node, node_tmp, |
| 266 | &orig_node->neigh_list, list) { |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 267 | |
| 268 | if ((time_after(jiffies, |
| 269 | neigh_node->last_valid + PURGE_TIMEOUT * HZ)) || |
| 270 | (neigh_node->if_incoming->if_status == IF_INACTIVE) || |
Marek Lindner | 1a241a5 | 2011-01-19 19:16:10 +0000 | [diff] [blame] | 271 | (neigh_node->if_incoming->if_status == IF_NOT_IN_USE) || |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 272 | (neigh_node->if_incoming->if_status == IF_TO_BE_REMOVED)) { |
| 273 | |
Marek Lindner | 1a241a5 | 2011-01-19 19:16:10 +0000 | [diff] [blame] | 274 | if ((neigh_node->if_incoming->if_status == |
| 275 | IF_INACTIVE) || |
| 276 | (neigh_node->if_incoming->if_status == |
| 277 | IF_NOT_IN_USE) || |
| 278 | (neigh_node->if_incoming->if_status == |
| 279 | IF_TO_BE_REMOVED)) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 280 | bat_dbg(DBG_BATMAN, bat_priv, |
| 281 | "neighbor purge: originator %pM, " |
| 282 | "neighbor: %pM, iface: %s\n", |
| 283 | orig_node->orig, neigh_node->addr, |
| 284 | neigh_node->if_incoming->net_dev->name); |
| 285 | else |
| 286 | bat_dbg(DBG_BATMAN, bat_priv, |
| 287 | "neighbor timeout: originator %pM, " |
| 288 | "neighbor: %pM, last_valid: %lu\n", |
| 289 | orig_node->orig, neigh_node->addr, |
| 290 | (neigh_node->last_valid / HZ)); |
| 291 | |
| 292 | neigh_purged = true; |
Marek Lindner | 9591a79 | 2010-12-12 21:57:11 +0000 | [diff] [blame] | 293 | |
Marek Lindner | f987ed6 | 2010-12-12 21:57:12 +0000 | [diff] [blame] | 294 | hlist_del_rcu(&neigh_node->list); |
Simon Wunderlich | a4c135c | 2011-01-19 20:01:43 +0000 | [diff] [blame] | 295 | bonding_candidate_del(orig_node, neigh_node); |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 296 | neigh_node_free_ref(neigh_node); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 297 | } else { |
| 298 | if ((!*best_neigh_node) || |
| 299 | (neigh_node->tq_avg > (*best_neigh_node)->tq_avg)) |
| 300 | *best_neigh_node = neigh_node; |
| 301 | } |
| 302 | } |
Marek Lindner | f987ed6 | 2010-12-12 21:57:12 +0000 | [diff] [blame] | 303 | |
| 304 | spin_unlock_bh(&orig_node->neigh_list_lock); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 305 | return neigh_purged; |
| 306 | } |
| 307 | |
| 308 | static bool purge_orig_node(struct bat_priv *bat_priv, |
| 309 | struct orig_node *orig_node) |
| 310 | { |
| 311 | struct neigh_node *best_neigh_node; |
| 312 | |
| 313 | if (time_after(jiffies, |
| 314 | orig_node->last_valid + 2 * PURGE_TIMEOUT * HZ)) { |
| 315 | |
| 316 | bat_dbg(DBG_BATMAN, bat_priv, |
| 317 | "Originator timeout: originator %pM, last_valid %lu\n", |
| 318 | orig_node->orig, (orig_node->last_valid / HZ)); |
| 319 | return true; |
| 320 | } else { |
| 321 | if (purge_orig_neighbors(bat_priv, orig_node, |
| 322 | &best_neigh_node)) { |
| 323 | update_routes(bat_priv, orig_node, |
| 324 | best_neigh_node, |
| 325 | orig_node->hna_buff, |
| 326 | orig_node->hna_buff_len); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 327 | } |
| 328 | } |
| 329 | |
| 330 | return false; |
| 331 | } |
| 332 | |
| 333 | static void _purge_orig(struct bat_priv *bat_priv) |
| 334 | { |
| 335 | struct hashtable_t *hash = bat_priv->orig_hash; |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 336 | struct hlist_node *node, *node_tmp; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 337 | struct hlist_head *head; |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame] | 338 | spinlock_t *list_lock; /* spinlock to protect write access */ |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 339 | struct orig_node *orig_node; |
| 340 | int i; |
| 341 | |
| 342 | if (!hash) |
| 343 | return; |
| 344 | |
| 345 | spin_lock_bh(&bat_priv->orig_hash_lock); |
| 346 | |
| 347 | /* for all origins... */ |
| 348 | for (i = 0; i < hash->size; i++) { |
| 349 | head = &hash->table[i]; |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame] | 350 | list_lock = &hash->list_locks[i]; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 351 | |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame] | 352 | spin_lock_bh(list_lock); |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 353 | hlist_for_each_entry_safe(orig_node, node, node_tmp, |
| 354 | head, hash_entry) { |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 355 | if (purge_orig_node(bat_priv, orig_node)) { |
| 356 | if (orig_node->gw_flags) |
| 357 | gw_node_delete(bat_priv, orig_node); |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 358 | hlist_del_rcu(node); |
Marek Lindner | 7b36e8e | 2011-02-18 12:28:10 +0000 | [diff] [blame] | 359 | orig_node_free_ref(orig_node); |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame] | 360 | continue; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | if (time_after(jiffies, orig_node->last_frag_packet + |
| 364 | msecs_to_jiffies(FRAG_TIMEOUT))) |
| 365 | frag_list_free(&orig_node->frag_list); |
| 366 | } |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame] | 367 | spin_unlock_bh(list_lock); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | spin_unlock_bh(&bat_priv->orig_hash_lock); |
| 371 | |
| 372 | gw_node_purge(bat_priv); |
| 373 | gw_election(bat_priv); |
| 374 | |
| 375 | softif_neigh_purge(bat_priv); |
| 376 | } |
| 377 | |
| 378 | static void purge_orig(struct work_struct *work) |
| 379 | { |
| 380 | struct delayed_work *delayed_work = |
| 381 | container_of(work, struct delayed_work, work); |
| 382 | struct bat_priv *bat_priv = |
| 383 | container_of(delayed_work, struct bat_priv, orig_work); |
| 384 | |
| 385 | _purge_orig(bat_priv); |
| 386 | start_purge_timer(bat_priv); |
| 387 | } |
| 388 | |
| 389 | void purge_orig_ref(struct bat_priv *bat_priv) |
| 390 | { |
| 391 | _purge_orig(bat_priv); |
| 392 | } |
| 393 | |
| 394 | int orig_seq_print_text(struct seq_file *seq, void *offset) |
| 395 | { |
| 396 | struct net_device *net_dev = (struct net_device *)seq->private; |
| 397 | struct bat_priv *bat_priv = netdev_priv(net_dev); |
| 398 | struct hashtable_t *hash = bat_priv->orig_hash; |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 399 | struct hlist_node *node, *node_tmp; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 400 | struct hlist_head *head; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 401 | struct orig_node *orig_node; |
| 402 | struct neigh_node *neigh_node; |
| 403 | int batman_count = 0; |
| 404 | int last_seen_secs; |
| 405 | int last_seen_msecs; |
| 406 | int i; |
| 407 | |
| 408 | if ((!bat_priv->primary_if) || |
| 409 | (bat_priv->primary_if->if_status != IF_ACTIVE)) { |
| 410 | if (!bat_priv->primary_if) |
| 411 | return seq_printf(seq, "BATMAN mesh %s disabled - " |
| 412 | "please specify interfaces to enable it\n", |
| 413 | net_dev->name); |
| 414 | |
| 415 | return seq_printf(seq, "BATMAN mesh %s " |
| 416 | "disabled - primary interface not active\n", |
| 417 | net_dev->name); |
| 418 | } |
| 419 | |
| 420 | seq_printf(seq, "[B.A.T.M.A.N. adv %s%s, MainIF/MAC: %s/%pM (%s)]\n", |
| 421 | SOURCE_VERSION, REVISION_VERSION_STR, |
| 422 | bat_priv->primary_if->net_dev->name, |
| 423 | bat_priv->primary_if->net_dev->dev_addr, net_dev->name); |
| 424 | seq_printf(seq, " %-15s %s (%s/%i) %17s [%10s]: %20s ...\n", |
| 425 | "Originator", "last-seen", "#", TQ_MAX_VALUE, "Nexthop", |
| 426 | "outgoingIF", "Potential nexthops"); |
| 427 | |
| 428 | spin_lock_bh(&bat_priv->orig_hash_lock); |
| 429 | |
| 430 | for (i = 0; i < hash->size; i++) { |
| 431 | head = &hash->table[i]; |
| 432 | |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame] | 433 | rcu_read_lock(); |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 434 | hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) { |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 435 | if (!orig_node->router) |
| 436 | continue; |
| 437 | |
| 438 | if (orig_node->router->tq_avg == 0) |
| 439 | continue; |
| 440 | |
| 441 | last_seen_secs = jiffies_to_msecs(jiffies - |
| 442 | orig_node->last_valid) / 1000; |
| 443 | last_seen_msecs = jiffies_to_msecs(jiffies - |
| 444 | orig_node->last_valid) % 1000; |
| 445 | |
| 446 | neigh_node = orig_node->router; |
| 447 | seq_printf(seq, "%pM %4i.%03is (%3i) %pM [%10s]:", |
| 448 | orig_node->orig, last_seen_secs, |
| 449 | last_seen_msecs, neigh_node->tq_avg, |
| 450 | neigh_node->addr, |
| 451 | neigh_node->if_incoming->net_dev->name); |
| 452 | |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 453 | hlist_for_each_entry_rcu(neigh_node, node_tmp, |
Marek Lindner | f987ed6 | 2010-12-12 21:57:12 +0000 | [diff] [blame] | 454 | &orig_node->neigh_list, list) { |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 455 | seq_printf(seq, " %pM (%3i)", neigh_node->addr, |
| 456 | neigh_node->tq_avg); |
| 457 | } |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 458 | |
| 459 | seq_printf(seq, "\n"); |
| 460 | batman_count++; |
| 461 | } |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame] | 462 | rcu_read_unlock(); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | spin_unlock_bh(&bat_priv->orig_hash_lock); |
| 466 | |
| 467 | if ((batman_count == 0)) |
| 468 | seq_printf(seq, "No batman nodes in range ...\n"); |
| 469 | |
| 470 | return 0; |
| 471 | } |
| 472 | |
| 473 | static int orig_node_add_if(struct orig_node *orig_node, int max_if_num) |
| 474 | { |
| 475 | void *data_ptr; |
| 476 | |
| 477 | data_ptr = kmalloc(max_if_num * sizeof(unsigned long) * NUM_WORDS, |
| 478 | GFP_ATOMIC); |
| 479 | if (!data_ptr) { |
| 480 | pr_err("Can't resize orig: out of memory\n"); |
| 481 | return -1; |
| 482 | } |
| 483 | |
| 484 | memcpy(data_ptr, orig_node->bcast_own, |
| 485 | (max_if_num - 1) * sizeof(unsigned long) * NUM_WORDS); |
| 486 | kfree(orig_node->bcast_own); |
| 487 | orig_node->bcast_own = data_ptr; |
| 488 | |
| 489 | data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC); |
| 490 | if (!data_ptr) { |
| 491 | pr_err("Can't resize orig: out of memory\n"); |
| 492 | return -1; |
| 493 | } |
| 494 | |
| 495 | memcpy(data_ptr, orig_node->bcast_own_sum, |
| 496 | (max_if_num - 1) * sizeof(uint8_t)); |
| 497 | kfree(orig_node->bcast_own_sum); |
| 498 | orig_node->bcast_own_sum = data_ptr; |
| 499 | |
| 500 | return 0; |
| 501 | } |
| 502 | |
| 503 | int orig_hash_add_if(struct batman_if *batman_if, int max_if_num) |
| 504 | { |
| 505 | struct bat_priv *bat_priv = netdev_priv(batman_if->soft_iface); |
| 506 | struct hashtable_t *hash = bat_priv->orig_hash; |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 507 | struct hlist_node *node; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 508 | struct hlist_head *head; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 509 | struct orig_node *orig_node; |
Marek Lindner | 2ae2daf | 2011-01-19 20:01:42 +0000 | [diff] [blame] | 510 | int i, ret; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 511 | |
| 512 | /* resize all orig nodes because orig_node->bcast_own(_sum) depend on |
| 513 | * if_num */ |
| 514 | spin_lock_bh(&bat_priv->orig_hash_lock); |
| 515 | |
| 516 | for (i = 0; i < hash->size; i++) { |
| 517 | head = &hash->table[i]; |
| 518 | |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame] | 519 | rcu_read_lock(); |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 520 | hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) { |
Marek Lindner | 2ae2daf | 2011-01-19 20:01:42 +0000 | [diff] [blame] | 521 | spin_lock_bh(&orig_node->ogm_cnt_lock); |
| 522 | ret = orig_node_add_if(orig_node, max_if_num); |
| 523 | spin_unlock_bh(&orig_node->ogm_cnt_lock); |
| 524 | |
| 525 | if (ret == -1) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 526 | goto err; |
| 527 | } |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame] | 528 | rcu_read_unlock(); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | spin_unlock_bh(&bat_priv->orig_hash_lock); |
| 532 | return 0; |
| 533 | |
| 534 | err: |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame] | 535 | rcu_read_unlock(); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 536 | spin_unlock_bh(&bat_priv->orig_hash_lock); |
| 537 | return -ENOMEM; |
| 538 | } |
| 539 | |
| 540 | static int orig_node_del_if(struct orig_node *orig_node, |
| 541 | int max_if_num, int del_if_num) |
| 542 | { |
| 543 | void *data_ptr = NULL; |
| 544 | int chunk_size; |
| 545 | |
| 546 | /* last interface was removed */ |
| 547 | if (max_if_num == 0) |
| 548 | goto free_bcast_own; |
| 549 | |
| 550 | chunk_size = sizeof(unsigned long) * NUM_WORDS; |
| 551 | data_ptr = kmalloc(max_if_num * chunk_size, GFP_ATOMIC); |
| 552 | if (!data_ptr) { |
| 553 | pr_err("Can't resize orig: out of memory\n"); |
| 554 | return -1; |
| 555 | } |
| 556 | |
| 557 | /* copy first part */ |
| 558 | memcpy(data_ptr, orig_node->bcast_own, del_if_num * chunk_size); |
| 559 | |
| 560 | /* copy second part */ |
| 561 | memcpy(data_ptr + del_if_num * chunk_size, |
| 562 | orig_node->bcast_own + ((del_if_num + 1) * chunk_size), |
| 563 | (max_if_num - del_if_num) * chunk_size); |
| 564 | |
| 565 | free_bcast_own: |
| 566 | kfree(orig_node->bcast_own); |
| 567 | orig_node->bcast_own = data_ptr; |
| 568 | |
| 569 | if (max_if_num == 0) |
| 570 | goto free_own_sum; |
| 571 | |
| 572 | data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC); |
| 573 | if (!data_ptr) { |
| 574 | pr_err("Can't resize orig: out of memory\n"); |
| 575 | return -1; |
| 576 | } |
| 577 | |
| 578 | memcpy(data_ptr, orig_node->bcast_own_sum, |
| 579 | del_if_num * sizeof(uint8_t)); |
| 580 | |
| 581 | memcpy(data_ptr + del_if_num * sizeof(uint8_t), |
| 582 | orig_node->bcast_own_sum + ((del_if_num + 1) * sizeof(uint8_t)), |
| 583 | (max_if_num - del_if_num) * sizeof(uint8_t)); |
| 584 | |
| 585 | free_own_sum: |
| 586 | kfree(orig_node->bcast_own_sum); |
| 587 | orig_node->bcast_own_sum = data_ptr; |
| 588 | |
| 589 | return 0; |
| 590 | } |
| 591 | |
| 592 | int orig_hash_del_if(struct batman_if *batman_if, int max_if_num) |
| 593 | { |
| 594 | struct bat_priv *bat_priv = netdev_priv(batman_if->soft_iface); |
| 595 | struct hashtable_t *hash = bat_priv->orig_hash; |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 596 | struct hlist_node *node; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 597 | struct hlist_head *head; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 598 | struct batman_if *batman_if_tmp; |
| 599 | struct orig_node *orig_node; |
| 600 | int i, ret; |
| 601 | |
| 602 | /* resize all orig nodes because orig_node->bcast_own(_sum) depend on |
| 603 | * if_num */ |
| 604 | spin_lock_bh(&bat_priv->orig_hash_lock); |
| 605 | |
| 606 | for (i = 0; i < hash->size; i++) { |
| 607 | head = &hash->table[i]; |
| 608 | |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame] | 609 | rcu_read_lock(); |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 610 | hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) { |
Marek Lindner | 2ae2daf | 2011-01-19 20:01:42 +0000 | [diff] [blame] | 611 | spin_lock_bh(&orig_node->ogm_cnt_lock); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 612 | ret = orig_node_del_if(orig_node, max_if_num, |
| 613 | batman_if->if_num); |
Marek Lindner | 2ae2daf | 2011-01-19 20:01:42 +0000 | [diff] [blame] | 614 | spin_unlock_bh(&orig_node->ogm_cnt_lock); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 615 | |
| 616 | if (ret == -1) |
| 617 | goto err; |
| 618 | } |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame] | 619 | rcu_read_unlock(); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | /* renumber remaining batman interfaces _inside_ of orig_hash_lock */ |
| 623 | rcu_read_lock(); |
| 624 | list_for_each_entry_rcu(batman_if_tmp, &if_list, list) { |
| 625 | if (batman_if_tmp->if_status == IF_NOT_IN_USE) |
| 626 | continue; |
| 627 | |
| 628 | if (batman_if == batman_if_tmp) |
| 629 | continue; |
| 630 | |
| 631 | if (batman_if->soft_iface != batman_if_tmp->soft_iface) |
| 632 | continue; |
| 633 | |
| 634 | if (batman_if_tmp->if_num > batman_if->if_num) |
| 635 | batman_if_tmp->if_num--; |
| 636 | } |
| 637 | rcu_read_unlock(); |
| 638 | |
| 639 | batman_if->if_num = -1; |
| 640 | spin_unlock_bh(&bat_priv->orig_hash_lock); |
| 641 | return 0; |
| 642 | |
| 643 | err: |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame] | 644 | rcu_read_unlock(); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 645 | spin_unlock_bh(&bat_priv->orig_hash_lock); |
| 646 | return -ENOMEM; |
| 647 | } |