Antonio Quartulli | 0b87393 | 2013-01-04 03:05:31 +0100 | [diff] [blame] | 1 | /* Copyright (C) 2009-2013 B.A.T.M.A.N. contributors: |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 2 | * |
| 3 | * Marek Lindner, Simon Wunderlich |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of version 2 of the GNU General Public |
| 7 | * License as published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, but |
| 10 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
Antonio Quartulli | ebf38fb | 2013-11-03 20:40:48 +0100 | [diff] [blame] | 15 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 16 | */ |
| 17 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 18 | #include "main.h" |
Antonio Quartulli | 785ea11 | 2011-11-23 11:35:44 +0100 | [diff] [blame] | 19 | #include "distributed-arp-table.h" |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 20 | #include "originator.h" |
| 21 | #include "hash.h" |
| 22 | #include "translation-table.h" |
| 23 | #include "routing.h" |
| 24 | #include "gateway_client.h" |
| 25 | #include "hard-interface.h" |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 26 | #include "soft-interface.h" |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 27 | #include "bridge_loop_avoidance.h" |
Martin Hundebøll | d56b170 | 2013-01-25 11:12:39 +0100 | [diff] [blame] | 28 | #include "network-coding.h" |
Martin Hundebøll | 610bfc6bc | 2013-05-23 16:53:02 +0200 | [diff] [blame] | 29 | #include "fragmentation.h" |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 30 | |
Antonio Quartulli | dec0507 | 2012-11-10 11:00:32 +0100 | [diff] [blame] | 31 | /* hash class keys */ |
| 32 | static struct lock_class_key batadv_orig_hash_lock_class_key; |
| 33 | |
Sven Eckelmann | 03fc7f8 | 2012-05-12 18:34:00 +0200 | [diff] [blame] | 34 | static void batadv_purge_orig(struct work_struct *work); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 35 | |
Sven Eckelmann | b8e2dd1 | 2011-06-15 15:08:59 +0200 | [diff] [blame] | 36 | /* returns 1 if they are the same originator */ |
Antonio Quartulli | bbad0a5 | 2013-09-02 12:15:02 +0200 | [diff] [blame] | 37 | int batadv_compare_orig(const struct hlist_node *node, const void *data2) |
Sven Eckelmann | b8e2dd1 | 2011-06-15 15:08:59 +0200 | [diff] [blame] | 38 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 39 | const void *data1 = container_of(node, struct batadv_orig_node, |
| 40 | hash_entry); |
Sven Eckelmann | b8e2dd1 | 2011-06-15 15:08:59 +0200 | [diff] [blame] | 41 | |
dingtianhong | 323813e | 2013-12-26 19:40:39 +0800 | [diff] [blame] | 42 | return batadv_compare_eth(data1, data2); |
Sven Eckelmann | b8e2dd1 | 2011-06-15 15:08:59 +0200 | [diff] [blame] | 43 | } |
| 44 | |
Antonio Quartulli | 7ea7b4a | 2013-07-30 22:16:25 +0200 | [diff] [blame] | 45 | /** |
| 46 | * batadv_orig_node_vlan_get - get an orig_node_vlan object |
| 47 | * @orig_node: the originator serving the VLAN |
| 48 | * @vid: the VLAN identifier |
| 49 | * |
| 50 | * Returns the vlan object identified by vid and belonging to orig_node or NULL |
| 51 | * if it does not exist. |
| 52 | */ |
| 53 | struct batadv_orig_node_vlan * |
| 54 | batadv_orig_node_vlan_get(struct batadv_orig_node *orig_node, |
| 55 | unsigned short vid) |
| 56 | { |
| 57 | struct batadv_orig_node_vlan *vlan = NULL, *tmp; |
| 58 | |
| 59 | rcu_read_lock(); |
| 60 | list_for_each_entry_rcu(tmp, &orig_node->vlan_list, list) { |
| 61 | if (tmp->vid != vid) |
| 62 | continue; |
| 63 | |
| 64 | if (!atomic_inc_not_zero(&tmp->refcount)) |
| 65 | continue; |
| 66 | |
| 67 | vlan = tmp; |
| 68 | |
| 69 | break; |
| 70 | } |
| 71 | rcu_read_unlock(); |
| 72 | |
| 73 | return vlan; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * batadv_orig_node_vlan_new - search and possibly create an orig_node_vlan |
| 78 | * object |
| 79 | * @orig_node: the originator serving the VLAN |
| 80 | * @vid: the VLAN identifier |
| 81 | * |
| 82 | * Returns NULL in case of failure or the vlan object identified by vid and |
| 83 | * belonging to orig_node otherwise. The object is created and added to the list |
| 84 | * if it does not exist. |
| 85 | * |
| 86 | * The object is returned with refcounter increased by 1. |
| 87 | */ |
| 88 | struct batadv_orig_node_vlan * |
| 89 | batadv_orig_node_vlan_new(struct batadv_orig_node *orig_node, |
| 90 | unsigned short vid) |
| 91 | { |
| 92 | struct batadv_orig_node_vlan *vlan; |
| 93 | |
| 94 | spin_lock_bh(&orig_node->vlan_list_lock); |
| 95 | |
| 96 | /* first look if an object for this vid already exists */ |
| 97 | vlan = batadv_orig_node_vlan_get(orig_node, vid); |
| 98 | if (vlan) |
| 99 | goto out; |
| 100 | |
| 101 | vlan = kzalloc(sizeof(*vlan), GFP_ATOMIC); |
| 102 | if (!vlan) |
| 103 | goto out; |
| 104 | |
| 105 | atomic_set(&vlan->refcount, 2); |
| 106 | vlan->vid = vid; |
| 107 | |
| 108 | list_add_rcu(&vlan->list, &orig_node->vlan_list); |
| 109 | |
| 110 | out: |
| 111 | spin_unlock_bh(&orig_node->vlan_list_lock); |
| 112 | |
| 113 | return vlan; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * batadv_orig_node_vlan_free_ref - decrement the refcounter and possibly free |
| 118 | * the originator-vlan object |
| 119 | * @orig_vlan: the originator-vlan object to release |
| 120 | */ |
| 121 | void batadv_orig_node_vlan_free_ref(struct batadv_orig_node_vlan *orig_vlan) |
| 122 | { |
| 123 | if (atomic_dec_and_test(&orig_vlan->refcount)) |
| 124 | kfree_rcu(orig_vlan, rcu); |
| 125 | } |
| 126 | |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 127 | int batadv_originator_init(struct batadv_priv *bat_priv) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 128 | { |
| 129 | if (bat_priv->orig_hash) |
Sven Eckelmann | 5346c35 | 2012-05-05 13:27:28 +0200 | [diff] [blame] | 130 | return 0; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 131 | |
Sven Eckelmann | 1a8eaf0 | 2012-05-12 02:09:32 +0200 | [diff] [blame] | 132 | bat_priv->orig_hash = batadv_hash_new(1024); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 133 | |
| 134 | if (!bat_priv->orig_hash) |
| 135 | goto err; |
| 136 | |
Antonio Quartulli | dec0507 | 2012-11-10 11:00:32 +0100 | [diff] [blame] | 137 | batadv_hash_set_lock_class(bat_priv->orig_hash, |
| 138 | &batadv_orig_hash_lock_class_key); |
| 139 | |
Antonio Quartulli | 7241444 | 2012-12-25 13:14:37 +0100 | [diff] [blame] | 140 | INIT_DELAYED_WORK(&bat_priv->orig_work, batadv_purge_orig); |
| 141 | queue_delayed_work(batadv_event_workqueue, |
| 142 | &bat_priv->orig_work, |
| 143 | msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD)); |
| 144 | |
Sven Eckelmann | 5346c35 | 2012-05-05 13:27:28 +0200 | [diff] [blame] | 145 | return 0; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 146 | |
| 147 | err: |
Sven Eckelmann | 5346c35 | 2012-05-05 13:27:28 +0200 | [diff] [blame] | 148 | return -ENOMEM; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame^] | 151 | /** |
| 152 | * batadv_neigh_ifinfo_free_rcu - free the neigh_ifinfo object |
| 153 | * @rcu: rcu pointer of the neigh_ifinfo object |
| 154 | */ |
| 155 | static void batadv_neigh_ifinfo_free_rcu(struct rcu_head *rcu) |
| 156 | { |
| 157 | struct batadv_neigh_ifinfo *neigh_ifinfo; |
| 158 | |
| 159 | neigh_ifinfo = container_of(rcu, struct batadv_neigh_ifinfo, rcu); |
| 160 | |
| 161 | if (neigh_ifinfo->if_outgoing != BATADV_IF_DEFAULT) |
| 162 | batadv_hardif_free_ref_now(neigh_ifinfo->if_outgoing); |
| 163 | |
| 164 | kfree(neigh_ifinfo); |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * batadv_neigh_ifinfo_free_now - decrement the refcounter and possibly free |
| 169 | * the neigh_ifinfo (without rcu callback) |
| 170 | * @neigh_ifinfo: the neigh_ifinfo object to release |
| 171 | */ |
| 172 | static void |
| 173 | batadv_neigh_ifinfo_free_ref_now(struct batadv_neigh_ifinfo *neigh_ifinfo) |
| 174 | { |
| 175 | if (atomic_dec_and_test(&neigh_ifinfo->refcount)) |
| 176 | batadv_neigh_ifinfo_free_rcu(&neigh_ifinfo->rcu); |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * batadv_neigh_ifinfo_free_ref - decrement the refcounter and possibly free |
| 181 | * the neigh_ifinfo |
| 182 | * @neigh_ifinfo: the neigh_ifinfo object to release |
| 183 | */ |
| 184 | void batadv_neigh_ifinfo_free_ref(struct batadv_neigh_ifinfo *neigh_ifinfo) |
| 185 | { |
| 186 | if (atomic_dec_and_test(&neigh_ifinfo->refcount)) |
| 187 | call_rcu(&neigh_ifinfo->rcu, batadv_neigh_ifinfo_free_rcu); |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * batadv_neigh_node_free_rcu - free the neigh_node |
| 192 | * @rcu: rcu pointer of the neigh_node |
| 193 | */ |
| 194 | static void batadv_neigh_node_free_rcu(struct rcu_head *rcu) |
| 195 | { |
| 196 | struct hlist_node *node_tmp; |
| 197 | struct batadv_neigh_node *neigh_node; |
| 198 | struct batadv_neigh_ifinfo *neigh_ifinfo; |
| 199 | |
| 200 | neigh_node = container_of(rcu, struct batadv_neigh_node, rcu); |
| 201 | |
| 202 | hlist_for_each_entry_safe(neigh_ifinfo, node_tmp, |
| 203 | &neigh_node->ifinfo_list, list) { |
| 204 | batadv_neigh_ifinfo_free_ref_now(neigh_ifinfo); |
| 205 | } |
| 206 | batadv_hardif_free_ref_now(neigh_node->if_incoming); |
| 207 | |
| 208 | kfree(neigh_node); |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * batadv_neigh_node_free_ref_now - decrement the neighbors refcounter |
| 213 | * and possibly free it (without rcu callback) |
| 214 | * @neigh_node: neigh neighbor to free |
| 215 | */ |
| 216 | static void |
| 217 | batadv_neigh_node_free_ref_now(struct batadv_neigh_node *neigh_node) |
| 218 | { |
| 219 | if (atomic_dec_and_test(&neigh_node->refcount)) |
| 220 | batadv_neigh_node_free_rcu(&neigh_node->rcu); |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * batadv_neigh_node_free_ref - decrement the neighbors refcounter |
| 225 | * and possibly free it |
| 226 | * @neigh_node: neigh neighbor to free |
| 227 | */ |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 228 | void batadv_neigh_node_free_ref(struct batadv_neigh_node *neigh_node) |
Simon Wunderlich | a4c135c | 2011-01-19 20:01:43 +0000 | [diff] [blame] | 229 | { |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 230 | if (atomic_dec_and_test(&neigh_node->refcount)) |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame^] | 231 | call_rcu(&neigh_node->rcu, batadv_neigh_node_free_rcu); |
Simon Wunderlich | a4c135c | 2011-01-19 20:01:43 +0000 | [diff] [blame] | 232 | } |
| 233 | |
Linus LĂĽssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 234 | /* increases the refcounter of a found router */ |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 235 | struct batadv_neigh_node * |
| 236 | batadv_orig_node_get_router(struct batadv_orig_node *orig_node) |
Linus LĂĽssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 237 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 238 | struct batadv_neigh_node *router; |
Linus LĂĽssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 239 | |
| 240 | rcu_read_lock(); |
| 241 | router = rcu_dereference(orig_node->router); |
| 242 | |
| 243 | if (router && !atomic_inc_not_zero(&router->refcount)) |
| 244 | router = NULL; |
| 245 | |
| 246 | rcu_read_unlock(); |
| 247 | return router; |
| 248 | } |
| 249 | |
Antonio Quartulli | 0538f75 | 2013-09-02 12:15:01 +0200 | [diff] [blame] | 250 | /** |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame^] | 251 | * batadv_neigh_ifinfo_get - find the ifinfo from an neigh_node |
| 252 | * @neigh_node: the neigh node to be queried |
| 253 | * @if_outgoing: the interface for which the ifinfo should be acquired |
| 254 | * |
| 255 | * The object is returned with refcounter increased by 1. |
| 256 | * |
| 257 | * Returns the requested neigh_ifinfo or NULL if not found |
| 258 | */ |
| 259 | struct batadv_neigh_ifinfo * |
| 260 | batadv_neigh_ifinfo_get(struct batadv_neigh_node *neigh, |
| 261 | struct batadv_hard_iface *if_outgoing) |
| 262 | { |
| 263 | struct batadv_neigh_ifinfo *neigh_ifinfo = NULL, |
| 264 | *tmp_neigh_ifinfo; |
| 265 | |
| 266 | rcu_read_lock(); |
| 267 | hlist_for_each_entry_rcu(tmp_neigh_ifinfo, &neigh->ifinfo_list, |
| 268 | list) { |
| 269 | if (tmp_neigh_ifinfo->if_outgoing != if_outgoing) |
| 270 | continue; |
| 271 | |
| 272 | if (!atomic_inc_not_zero(&tmp_neigh_ifinfo->refcount)) |
| 273 | continue; |
| 274 | |
| 275 | neigh_ifinfo = tmp_neigh_ifinfo; |
| 276 | break; |
| 277 | } |
| 278 | rcu_read_unlock(); |
| 279 | |
| 280 | return neigh_ifinfo; |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * batadv_neigh_ifinfo_new - search and possibly create an neigh_ifinfo object |
| 285 | * @neigh_node: the neigh node to be queried |
| 286 | * @if_outgoing: the interface for which the ifinfo should be acquired |
| 287 | * |
| 288 | * Returns NULL in case of failure or the neigh_ifinfo object for the |
| 289 | * if_outgoing interface otherwise. The object is created and added to the list |
| 290 | * if it does not exist. |
| 291 | * |
| 292 | * The object is returned with refcounter increased by 1. |
| 293 | */ |
| 294 | struct batadv_neigh_ifinfo * |
| 295 | batadv_neigh_ifinfo_new(struct batadv_neigh_node *neigh, |
| 296 | struct batadv_hard_iface *if_outgoing) |
| 297 | { |
| 298 | struct batadv_neigh_ifinfo *neigh_ifinfo; |
| 299 | |
| 300 | spin_lock_bh(&neigh->ifinfo_lock); |
| 301 | |
| 302 | neigh_ifinfo = batadv_neigh_ifinfo_get(neigh, if_outgoing); |
| 303 | if (neigh_ifinfo) |
| 304 | goto out; |
| 305 | |
| 306 | neigh_ifinfo = kzalloc(sizeof(*neigh_ifinfo), GFP_ATOMIC); |
| 307 | if (!neigh_ifinfo) |
| 308 | goto out; |
| 309 | |
| 310 | if (if_outgoing && !atomic_inc_not_zero(&if_outgoing->refcount)) { |
| 311 | kfree(neigh_ifinfo); |
| 312 | neigh_ifinfo = NULL; |
| 313 | goto out; |
| 314 | } |
| 315 | |
| 316 | INIT_HLIST_NODE(&neigh_ifinfo->list); |
| 317 | atomic_set(&neigh_ifinfo->refcount, 2); |
| 318 | neigh_ifinfo->if_outgoing = if_outgoing; |
| 319 | |
| 320 | hlist_add_head_rcu(&neigh_ifinfo->list, &neigh->ifinfo_list); |
| 321 | |
| 322 | out: |
| 323 | spin_unlock_bh(&neigh->ifinfo_lock); |
| 324 | |
| 325 | return neigh_ifinfo; |
| 326 | } |
| 327 | |
| 328 | /** |
Antonio Quartulli | 0538f75 | 2013-09-02 12:15:01 +0200 | [diff] [blame] | 329 | * batadv_neigh_node_new - create and init a new neigh_node object |
| 330 | * @hard_iface: the interface where the neighbour is connected to |
| 331 | * @neigh_addr: the mac address of the neighbour interface |
| 332 | * @orig_node: originator object representing the neighbour |
| 333 | * |
| 334 | * Allocates a new neigh_node object and initialises all the generic fields. |
| 335 | * Returns the new object or NULL on failure. |
| 336 | */ |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 337 | struct batadv_neigh_node * |
| 338 | batadv_neigh_node_new(struct batadv_hard_iface *hard_iface, |
Antonio Quartulli | 0538f75 | 2013-09-02 12:15:01 +0200 | [diff] [blame] | 339 | const uint8_t *neigh_addr, |
| 340 | struct batadv_orig_node *orig_node) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 341 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 342 | struct batadv_neigh_node *neigh_node; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 343 | |
Sven Eckelmann | 704509b | 2011-05-14 23:14:54 +0200 | [diff] [blame] | 344 | neigh_node = kzalloc(sizeof(*neigh_node), GFP_ATOMIC); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 345 | if (!neigh_node) |
Marek Lindner | 7ae8b28 | 2012-03-01 15:35:21 +0800 | [diff] [blame] | 346 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 347 | |
Marek Lindner | 9591a79 | 2010-12-12 21:57:11 +0000 | [diff] [blame] | 348 | INIT_HLIST_NODE(&neigh_node->list); |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame^] | 349 | INIT_HLIST_HEAD(&neigh_node->ifinfo_list); |
| 350 | spin_lock_init(&neigh_node->ifinfo_lock); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 351 | |
Marek Lindner | 7ae8b28 | 2012-03-01 15:35:21 +0800 | [diff] [blame] | 352 | memcpy(neigh_node->addr, neigh_addr, ETH_ALEN); |
Antonio Quartulli | 0538f75 | 2013-09-02 12:15:01 +0200 | [diff] [blame] | 353 | neigh_node->if_incoming = hard_iface; |
| 354 | neigh_node->orig_node = orig_node; |
| 355 | |
Marek Lindner | 1605d0d | 2011-02-18 12:28:11 +0000 | [diff] [blame] | 356 | /* extra reference for return */ |
| 357 | atomic_set(&neigh_node->refcount, 2); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 358 | |
Marek Lindner | 7ae8b28 | 2012-03-01 15:35:21 +0800 | [diff] [blame] | 359 | out: |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 360 | return neigh_node; |
| 361 | } |
| 362 | |
Sven Eckelmann | 03fc7f8 | 2012-05-12 18:34:00 +0200 | [diff] [blame] | 363 | static void batadv_orig_node_free_rcu(struct rcu_head *rcu) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 364 | { |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 365 | struct hlist_node *node_tmp; |
Simon Wunderlich | f6c8b71 | 2013-11-13 19:14:45 +0100 | [diff] [blame] | 366 | struct batadv_neigh_node *neigh_node; |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 367 | struct batadv_orig_node *orig_node; |
Marek Lindner | 16b1aba | 2011-01-19 20:01:42 +0000 | [diff] [blame] | 368 | |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 369 | orig_node = container_of(rcu, struct batadv_orig_node, rcu); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 370 | |
Marek Lindner | f987ed6 | 2010-12-12 21:57:12 +0000 | [diff] [blame] | 371 | spin_lock_bh(&orig_node->neigh_list_lock); |
| 372 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 373 | /* for all neighbors towards this originator ... */ |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 374 | hlist_for_each_entry_safe(neigh_node, node_tmp, |
Marek Lindner | 9591a79 | 2010-12-12 21:57:11 +0000 | [diff] [blame] | 375 | &orig_node->neigh_list, list) { |
Marek Lindner | f987ed6 | 2010-12-12 21:57:12 +0000 | [diff] [blame] | 376 | hlist_del_rcu(&neigh_node->list); |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame^] | 377 | batadv_neigh_node_free_ref_now(neigh_node); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 378 | } |
| 379 | |
Marek Lindner | f987ed6 | 2010-12-12 21:57:12 +0000 | [diff] [blame] | 380 | spin_unlock_bh(&orig_node->neigh_list_lock); |
| 381 | |
Martin Hundebøll | d56b170 | 2013-01-25 11:12:39 +0100 | [diff] [blame] | 382 | /* Free nc_nodes */ |
| 383 | batadv_nc_purge_orig(orig_node->bat_priv, orig_node, NULL); |
| 384 | |
Martin Hundebøll | 610bfc6bc | 2013-05-23 16:53:02 +0200 | [diff] [blame] | 385 | batadv_frag_purge_orig(orig_node, NULL); |
| 386 | |
Antonio Quartulli | 95fb130 | 2013-08-07 18:28:55 +0200 | [diff] [blame] | 387 | batadv_tt_global_del_orig(orig_node->bat_priv, orig_node, -1, |
Sven Eckelmann | 08c36d3 | 2012-05-12 02:09:39 +0200 | [diff] [blame] | 388 | "originator timed out"); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 389 | |
Antonio Quartulli | d0015fd | 2013-09-03 11:10:23 +0200 | [diff] [blame] | 390 | if (orig_node->bat_priv->bat_algo_ops->bat_orig_free) |
| 391 | orig_node->bat_priv->bat_algo_ops->bat_orig_free(orig_node); |
| 392 | |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 393 | kfree(orig_node->tt_buff); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 394 | kfree(orig_node); |
| 395 | } |
| 396 | |
Linus LĂĽssing | 7282222 | 2013-04-15 21:43:29 +0800 | [diff] [blame] | 397 | /** |
| 398 | * batadv_orig_node_free_ref - decrement the orig node refcounter and possibly |
| 399 | * schedule an rcu callback for freeing it |
| 400 | * @orig_node: the orig node to free |
| 401 | */ |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 402 | void batadv_orig_node_free_ref(struct batadv_orig_node *orig_node) |
Marek Lindner | 7b36e8e | 2011-02-18 12:28:10 +0000 | [diff] [blame] | 403 | { |
| 404 | if (atomic_dec_and_test(&orig_node->refcount)) |
Sven Eckelmann | 03fc7f8 | 2012-05-12 18:34:00 +0200 | [diff] [blame] | 405 | call_rcu(&orig_node->rcu, batadv_orig_node_free_rcu); |
Marek Lindner | 7b36e8e | 2011-02-18 12:28:10 +0000 | [diff] [blame] | 406 | } |
| 407 | |
Linus LĂĽssing | 7282222 | 2013-04-15 21:43:29 +0800 | [diff] [blame] | 408 | /** |
| 409 | * batadv_orig_node_free_ref_now - decrement the orig node refcounter and |
| 410 | * possibly free it (without rcu callback) |
| 411 | * @orig_node: the orig node to free |
| 412 | */ |
| 413 | void batadv_orig_node_free_ref_now(struct batadv_orig_node *orig_node) |
| 414 | { |
| 415 | if (atomic_dec_and_test(&orig_node->refcount)) |
| 416 | batadv_orig_node_free_rcu(&orig_node->rcu); |
| 417 | } |
| 418 | |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 419 | void batadv_originator_free(struct batadv_priv *bat_priv) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 420 | { |
Sven Eckelmann | 5bf74e9 | 2012-06-05 22:31:28 +0200 | [diff] [blame] | 421 | struct batadv_hashtable *hash = bat_priv->orig_hash; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 422 | struct hlist_node *node_tmp; |
Marek Lindner | 16b1aba | 2011-01-19 20:01:42 +0000 | [diff] [blame] | 423 | struct hlist_head *head; |
Marek Lindner | 16b1aba | 2011-01-19 20:01:42 +0000 | [diff] [blame] | 424 | spinlock_t *list_lock; /* spinlock to protect write access */ |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 425 | struct batadv_orig_node *orig_node; |
Antonio Quartulli | c90681b | 2011-10-05 17:05:25 +0200 | [diff] [blame] | 426 | uint32_t i; |
Marek Lindner | 16b1aba | 2011-01-19 20:01:42 +0000 | [diff] [blame] | 427 | |
| 428 | if (!hash) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 429 | return; |
| 430 | |
| 431 | cancel_delayed_work_sync(&bat_priv->orig_work); |
| 432 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 433 | bat_priv->orig_hash = NULL; |
Marek Lindner | 16b1aba | 2011-01-19 20:01:42 +0000 | [diff] [blame] | 434 | |
| 435 | for (i = 0; i < hash->size; i++) { |
| 436 | head = &hash->table[i]; |
| 437 | list_lock = &hash->list_locks[i]; |
| 438 | |
| 439 | spin_lock_bh(list_lock); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 440 | hlist_for_each_entry_safe(orig_node, node_tmp, |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 441 | head, hash_entry) { |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 442 | hlist_del_rcu(&orig_node->hash_entry); |
Sven Eckelmann | 7d211ef | 2012-05-12 02:09:34 +0200 | [diff] [blame] | 443 | batadv_orig_node_free_ref(orig_node); |
Marek Lindner | 16b1aba | 2011-01-19 20:01:42 +0000 | [diff] [blame] | 444 | } |
| 445 | spin_unlock_bh(list_lock); |
| 446 | } |
| 447 | |
Sven Eckelmann | 1a8eaf0 | 2012-05-12 02:09:32 +0200 | [diff] [blame] | 448 | batadv_hash_destroy(hash); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 449 | } |
| 450 | |
Antonio Quartulli | bbad0a5 | 2013-09-02 12:15:02 +0200 | [diff] [blame] | 451 | /** |
| 452 | * batadv_orig_node_new - creates a new orig_node |
| 453 | * @bat_priv: the bat priv with all the soft interface information |
| 454 | * @addr: the mac address of the originator |
| 455 | * |
| 456 | * Creates a new originator object and initialise all the generic fields. |
| 457 | * The new object is not added to the originator list. |
| 458 | * Returns the newly created object or NULL on failure. |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 459 | */ |
Antonio Quartulli | bbad0a5 | 2013-09-02 12:15:02 +0200 | [diff] [blame] | 460 | struct batadv_orig_node *batadv_orig_node_new(struct batadv_priv *bat_priv, |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 461 | const uint8_t *addr) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 462 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 463 | struct batadv_orig_node *orig_node; |
Antonio Quartulli | 7ea7b4a | 2013-07-30 22:16:25 +0200 | [diff] [blame] | 464 | struct batadv_orig_node_vlan *vlan; |
Sven Eckelmann | 42d0b04 | 2012-06-03 22:19:17 +0200 | [diff] [blame] | 465 | unsigned long reset_time; |
Antonio Quartulli | bbad0a5 | 2013-09-02 12:15:02 +0200 | [diff] [blame] | 466 | int i; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 467 | |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 468 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
| 469 | "Creating new originator: %pM\n", addr); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 470 | |
Sven Eckelmann | 704509b | 2011-05-14 23:14:54 +0200 | [diff] [blame] | 471 | orig_node = kzalloc(sizeof(*orig_node), GFP_ATOMIC); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 472 | if (!orig_node) |
| 473 | return NULL; |
| 474 | |
Marek Lindner | 9591a79 | 2010-12-12 21:57:11 +0000 | [diff] [blame] | 475 | INIT_HLIST_HEAD(&orig_node->neigh_list); |
Antonio Quartulli | 7ea7b4a | 2013-07-30 22:16:25 +0200 | [diff] [blame] | 476 | INIT_LIST_HEAD(&orig_node->vlan_list); |
Marek Lindner | f3e0008 | 2011-01-25 21:52:11 +0000 | [diff] [blame] | 477 | spin_lock_init(&orig_node->bcast_seqno_lock); |
Marek Lindner | f987ed6 | 2010-12-12 21:57:12 +0000 | [diff] [blame] | 478 | spin_lock_init(&orig_node->neigh_list_lock); |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 479 | spin_lock_init(&orig_node->tt_buff_lock); |
Antonio Quartulli | a70a9aa | 2013-07-30 22:16:24 +0200 | [diff] [blame] | 480 | spin_lock_init(&orig_node->tt_lock); |
Antonio Quartulli | 7ea7b4a | 2013-07-30 22:16:25 +0200 | [diff] [blame] | 481 | spin_lock_init(&orig_node->vlan_list_lock); |
Marek Lindner | 7b36e8e | 2011-02-18 12:28:10 +0000 | [diff] [blame] | 482 | |
Martin Hundebøll | d56b170 | 2013-01-25 11:12:39 +0100 | [diff] [blame] | 483 | batadv_nc_init_orig(orig_node); |
| 484 | |
Marek Lindner | 7b36e8e | 2011-02-18 12:28:10 +0000 | [diff] [blame] | 485 | /* extra reference for return */ |
| 486 | atomic_set(&orig_node->refcount, 2); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 487 | |
Antonio Quartulli | 1707157 | 2011-11-07 16:36:40 +0100 | [diff] [blame] | 488 | orig_node->tt_initialised = false; |
Marek Lindner | 16b1aba | 2011-01-19 20:01:42 +0000 | [diff] [blame] | 489 | orig_node->bat_priv = bat_priv; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 490 | memcpy(orig_node->orig, addr, ETH_ALEN); |
Antonio Quartulli | 785ea11 | 2011-11-23 11:35:44 +0100 | [diff] [blame] | 491 | batadv_dat_init_orig_node_addr(orig_node); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 492 | orig_node->router = NULL; |
Antonio Quartulli | c8c991b | 2011-07-07 01:40:57 +0200 | [diff] [blame] | 493 | atomic_set(&orig_node->last_ttvn, 0); |
Antonio Quartulli | 2dafb49 | 2011-05-05 08:42:45 +0200 | [diff] [blame] | 494 | orig_node->tt_buff = NULL; |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 495 | orig_node->tt_buff_len = 0; |
Sven Eckelmann | 42d0b04 | 2012-06-03 22:19:17 +0200 | [diff] [blame] | 496 | reset_time = jiffies - 1 - msecs_to_jiffies(BATADV_RESET_PROTECTION_MS); |
| 497 | orig_node->bcast_seqno_reset = reset_time; |
| 498 | orig_node->batman_seqno_reset = reset_time; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 499 | |
Antonio Quartulli | 7ea7b4a | 2013-07-30 22:16:25 +0200 | [diff] [blame] | 500 | /* create a vlan object for the "untagged" LAN */ |
| 501 | vlan = batadv_orig_node_vlan_new(orig_node, BATADV_NO_FLAGS); |
| 502 | if (!vlan) |
| 503 | goto free_orig_node; |
| 504 | /* batadv_orig_node_vlan_new() increases the refcounter. |
| 505 | * Immediately release vlan since it is not needed anymore in this |
| 506 | * context |
| 507 | */ |
| 508 | batadv_orig_node_vlan_free_ref(vlan); |
| 509 | |
Martin Hundebøll | 610bfc6bc | 2013-05-23 16:53:02 +0200 | [diff] [blame] | 510 | for (i = 0; i < BATADV_FRAG_BUFFER_COUNT; i++) { |
| 511 | INIT_HLIST_HEAD(&orig_node->fragments[i].head); |
| 512 | spin_lock_init(&orig_node->fragments[i].lock); |
| 513 | orig_node->fragments[i].size = 0; |
| 514 | } |
| 515 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 516 | return orig_node; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 517 | free_orig_node: |
| 518 | kfree(orig_node); |
| 519 | return NULL; |
| 520 | } |
| 521 | |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame^] | 522 | /** |
| 523 | * batadv_purge_orig_neighbors - purges neighbors from originator |
| 524 | * @bat_priv: the bat priv with all the soft interface information |
| 525 | * @orig_node: orig node which is to be checked |
| 526 | * |
| 527 | * Returns true if any neighbor was purged, false otherwise |
| 528 | */ |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 529 | static bool |
| 530 | batadv_purge_orig_neighbors(struct batadv_priv *bat_priv, |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame^] | 531 | struct batadv_orig_node *orig_node) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 532 | { |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 533 | struct hlist_node *node_tmp; |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 534 | struct batadv_neigh_node *neigh_node; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 535 | bool neigh_purged = false; |
Marek Lindner | 0b0094e | 2012-03-01 15:35:20 +0800 | [diff] [blame] | 536 | unsigned long last_seen; |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 537 | struct batadv_hard_iface *if_incoming; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 538 | |
Marek Lindner | f987ed6 | 2010-12-12 21:57:12 +0000 | [diff] [blame] | 539 | spin_lock_bh(&orig_node->neigh_list_lock); |
| 540 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 541 | /* for all neighbors towards this originator ... */ |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 542 | hlist_for_each_entry_safe(neigh_node, node_tmp, |
Marek Lindner | 9591a79 | 2010-12-12 21:57:11 +0000 | [diff] [blame] | 543 | &orig_node->neigh_list, list) { |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 544 | last_seen = neigh_node->last_seen; |
| 545 | if_incoming = neigh_node->if_incoming; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 546 | |
Sven Eckelmann | 42d0b04 | 2012-06-03 22:19:17 +0200 | [diff] [blame] | 547 | if ((batadv_has_timed_out(last_seen, BATADV_PURGE_TIMEOUT)) || |
Sven Eckelmann | e9a4f29 | 2012-06-03 22:19:19 +0200 | [diff] [blame] | 548 | (if_incoming->if_status == BATADV_IF_INACTIVE) || |
| 549 | (if_incoming->if_status == BATADV_IF_NOT_IN_USE) || |
| 550 | (if_incoming->if_status == BATADV_IF_TO_BE_REMOVED)) { |
Sven Eckelmann | e9a4f29 | 2012-06-03 22:19:19 +0200 | [diff] [blame] | 551 | if ((if_incoming->if_status == BATADV_IF_INACTIVE) || |
| 552 | (if_incoming->if_status == BATADV_IF_NOT_IN_USE) || |
| 553 | (if_incoming->if_status == BATADV_IF_TO_BE_REMOVED)) |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 554 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 555 | "neighbor purge: originator %pM, neighbor: %pM, iface: %s\n", |
| 556 | orig_node->orig, neigh_node->addr, |
| 557 | if_incoming->net_dev->name); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 558 | else |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 559 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 560 | "neighbor timeout: originator %pM, neighbor: %pM, last_seen: %u\n", |
| 561 | orig_node->orig, neigh_node->addr, |
| 562 | jiffies_to_msecs(last_seen)); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 563 | |
| 564 | neigh_purged = true; |
Marek Lindner | 9591a79 | 2010-12-12 21:57:11 +0000 | [diff] [blame] | 565 | |
Marek Lindner | f987ed6 | 2010-12-12 21:57:12 +0000 | [diff] [blame] | 566 | hlist_del_rcu(&neigh_node->list); |
Sven Eckelmann | 7d211ef | 2012-05-12 02:09:34 +0200 | [diff] [blame] | 567 | batadv_neigh_node_free_ref(neigh_node); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 568 | } |
| 569 | } |
Marek Lindner | f987ed6 | 2010-12-12 21:57:12 +0000 | [diff] [blame] | 570 | |
| 571 | spin_unlock_bh(&orig_node->neigh_list_lock); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 572 | return neigh_purged; |
| 573 | } |
| 574 | |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame^] | 575 | /** |
| 576 | * batadv_find_best_neighbor - finds the best neighbor after purging |
| 577 | * @bat_priv: the bat priv with all the soft interface information |
| 578 | * @orig_node: orig node which is to be checked |
| 579 | * @if_outgoing: the interface for which the metric should be compared |
| 580 | * |
| 581 | * Returns the current best neighbor, with refcount increased. |
| 582 | */ |
| 583 | static struct batadv_neigh_node * |
| 584 | batadv_find_best_neighbor(struct batadv_priv *bat_priv, |
| 585 | struct batadv_orig_node *orig_node, |
| 586 | struct batadv_hard_iface *if_outgoing) |
| 587 | { |
| 588 | struct batadv_neigh_node *best = NULL, *neigh; |
| 589 | struct batadv_algo_ops *bao = bat_priv->bat_algo_ops; |
| 590 | |
| 591 | rcu_read_lock(); |
| 592 | hlist_for_each_entry_rcu(neigh, &orig_node->neigh_list, list) { |
| 593 | if (best && (bao->bat_neigh_cmp(neigh, if_outgoing, |
| 594 | best, if_outgoing) <= 0)) |
| 595 | continue; |
| 596 | |
| 597 | if (!atomic_inc_not_zero(&neigh->refcount)) |
| 598 | continue; |
| 599 | |
| 600 | if (best) |
| 601 | batadv_neigh_node_free_ref(best); |
| 602 | |
| 603 | best = neigh; |
| 604 | } |
| 605 | rcu_read_unlock(); |
| 606 | |
| 607 | return best; |
| 608 | } |
| 609 | |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 610 | static bool batadv_purge_orig_node(struct batadv_priv *bat_priv, |
| 611 | struct batadv_orig_node *orig_node) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 612 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 613 | struct batadv_neigh_node *best_neigh_node; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 614 | |
Sven Eckelmann | 42d0b04 | 2012-06-03 22:19:17 +0200 | [diff] [blame] | 615 | if (batadv_has_timed_out(orig_node->last_seen, |
| 616 | 2 * BATADV_PURGE_TIMEOUT)) { |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 617 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 618 | "Originator timeout: originator %pM, last_seen %u\n", |
| 619 | orig_node->orig, |
| 620 | jiffies_to_msecs(orig_node->last_seen)); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 621 | return true; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 622 | } |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame^] | 623 | if (!batadv_purge_orig_neighbors(bat_priv, orig_node)) |
| 624 | return false; |
| 625 | |
| 626 | best_neigh_node = batadv_find_best_neighbor(bat_priv, orig_node, |
| 627 | BATADV_IF_DEFAULT); |
| 628 | batadv_update_route(bat_priv, orig_node, best_neigh_node); |
| 629 | if (best_neigh_node) |
| 630 | batadv_neigh_node_free_ref(best_neigh_node); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 631 | |
| 632 | return false; |
| 633 | } |
| 634 | |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 635 | static void _batadv_purge_orig(struct batadv_priv *bat_priv) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 636 | { |
Sven Eckelmann | 5bf74e9 | 2012-06-05 22:31:28 +0200 | [diff] [blame] | 637 | struct batadv_hashtable *hash = bat_priv->orig_hash; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 638 | struct hlist_node *node_tmp; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 639 | struct hlist_head *head; |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame] | 640 | spinlock_t *list_lock; /* spinlock to protect write access */ |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 641 | struct batadv_orig_node *orig_node; |
Antonio Quartulli | c90681b | 2011-10-05 17:05:25 +0200 | [diff] [blame] | 642 | uint32_t i; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 643 | |
| 644 | if (!hash) |
| 645 | return; |
| 646 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 647 | /* for all origins... */ |
| 648 | for (i = 0; i < hash->size; i++) { |
| 649 | head = &hash->table[i]; |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame] | 650 | list_lock = &hash->list_locks[i]; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 651 | |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame] | 652 | spin_lock_bh(list_lock); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 653 | hlist_for_each_entry_safe(orig_node, node_tmp, |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 654 | head, hash_entry) { |
Sven Eckelmann | 03fc7f8 | 2012-05-12 18:34:00 +0200 | [diff] [blame] | 655 | if (batadv_purge_orig_node(bat_priv, orig_node)) { |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 656 | batadv_gw_node_delete(bat_priv, orig_node); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 657 | hlist_del_rcu(&orig_node->hash_entry); |
Sven Eckelmann | 7d211ef | 2012-05-12 02:09:34 +0200 | [diff] [blame] | 658 | batadv_orig_node_free_ref(orig_node); |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame] | 659 | continue; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 660 | } |
Martin Hundebøll | 610bfc6bc | 2013-05-23 16:53:02 +0200 | [diff] [blame] | 661 | |
| 662 | batadv_frag_purge_orig(orig_node, |
| 663 | batadv_frag_check_entry); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 664 | } |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame] | 665 | spin_unlock_bh(list_lock); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 666 | } |
| 667 | |
Sven Eckelmann | 7cf06bc | 2012-05-12 02:09:29 +0200 | [diff] [blame] | 668 | batadv_gw_node_purge(bat_priv); |
| 669 | batadv_gw_election(bat_priv); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 670 | } |
| 671 | |
Sven Eckelmann | 03fc7f8 | 2012-05-12 18:34:00 +0200 | [diff] [blame] | 672 | static void batadv_purge_orig(struct work_struct *work) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 673 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 674 | struct delayed_work *delayed_work; |
| 675 | struct batadv_priv *bat_priv; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 676 | |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 677 | delayed_work = container_of(work, struct delayed_work, work); |
| 678 | bat_priv = container_of(delayed_work, struct batadv_priv, orig_work); |
Sven Eckelmann | 03fc7f8 | 2012-05-12 18:34:00 +0200 | [diff] [blame] | 679 | _batadv_purge_orig(bat_priv); |
Antonio Quartulli | 7241444 | 2012-12-25 13:14:37 +0100 | [diff] [blame] | 680 | queue_delayed_work(batadv_event_workqueue, |
| 681 | &bat_priv->orig_work, |
| 682 | msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD)); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 683 | } |
| 684 | |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 685 | void batadv_purge_orig_ref(struct batadv_priv *bat_priv) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 686 | { |
Sven Eckelmann | 03fc7f8 | 2012-05-12 18:34:00 +0200 | [diff] [blame] | 687 | _batadv_purge_orig(bat_priv); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 688 | } |
| 689 | |
Sven Eckelmann | 7d211ef | 2012-05-12 02:09:34 +0200 | [diff] [blame] | 690 | int batadv_orig_seq_print_text(struct seq_file *seq, void *offset) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 691 | { |
| 692 | struct net_device *net_dev = (struct net_device *)seq->private; |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 693 | struct batadv_priv *bat_priv = netdev_priv(net_dev); |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 694 | struct batadv_hard_iface *primary_if; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 695 | |
Marek Lindner | 30da63a | 2012-08-03 17:15:46 +0200 | [diff] [blame] | 696 | primary_if = batadv_seq_print_text_primary_if_get(seq); |
| 697 | if (!primary_if) |
Antonio Quartulli | 737a2a22 | 2013-09-02 12:15:03 +0200 | [diff] [blame] | 698 | return 0; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 699 | |
Antonio Quartulli | 737a2a22 | 2013-09-02 12:15:03 +0200 | [diff] [blame] | 700 | seq_printf(seq, "[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s %s)]\n", |
Sven Eckelmann | 42d0b04 | 2012-06-03 22:19:17 +0200 | [diff] [blame] | 701 | BATADV_SOURCE_VERSION, primary_if->net_dev->name, |
Antonio Quartulli | 737a2a22 | 2013-09-02 12:15:03 +0200 | [diff] [blame] | 702 | primary_if->net_dev->dev_addr, net_dev->name, |
| 703 | bat_priv->bat_algo_ops->name); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 704 | |
Antonio Quartulli | 737a2a22 | 2013-09-02 12:15:03 +0200 | [diff] [blame] | 705 | batadv_hardif_free_ref(primary_if); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 706 | |
Antonio Quartulli | 737a2a22 | 2013-09-02 12:15:03 +0200 | [diff] [blame] | 707 | if (!bat_priv->bat_algo_ops->bat_orig_print) { |
| 708 | seq_puts(seq, |
| 709 | "No printing function for this routing protocol\n"); |
| 710 | return 0; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 711 | } |
| 712 | |
Antonio Quartulli | 737a2a22 | 2013-09-02 12:15:03 +0200 | [diff] [blame] | 713 | bat_priv->bat_algo_ops->bat_orig_print(bat_priv, seq); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 714 | |
Marek Lindner | 30da63a | 2012-08-03 17:15:46 +0200 | [diff] [blame] | 715 | return 0; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 716 | } |
| 717 | |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 718 | int batadv_orig_hash_add_if(struct batadv_hard_iface *hard_iface, |
| 719 | int max_if_num) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 720 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 721 | struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface); |
Antonio Quartulli | d0015fd | 2013-09-03 11:10:23 +0200 | [diff] [blame] | 722 | struct batadv_algo_ops *bao = bat_priv->bat_algo_ops; |
Sven Eckelmann | 5bf74e9 | 2012-06-05 22:31:28 +0200 | [diff] [blame] | 723 | struct batadv_hashtable *hash = bat_priv->orig_hash; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 724 | struct hlist_head *head; |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 725 | struct batadv_orig_node *orig_node; |
Antonio Quartulli | c90681b | 2011-10-05 17:05:25 +0200 | [diff] [blame] | 726 | uint32_t i; |
| 727 | int ret; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 728 | |
| 729 | /* resize all orig nodes because orig_node->bcast_own(_sum) depend on |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 730 | * if_num |
| 731 | */ |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 732 | for (i = 0; i < hash->size; i++) { |
| 733 | head = &hash->table[i]; |
| 734 | |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame] | 735 | rcu_read_lock(); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 736 | hlist_for_each_entry_rcu(orig_node, head, hash_entry) { |
Antonio Quartulli | d0015fd | 2013-09-03 11:10:23 +0200 | [diff] [blame] | 737 | ret = 0; |
| 738 | if (bao->bat_orig_add_if) |
| 739 | ret = bao->bat_orig_add_if(orig_node, |
| 740 | max_if_num); |
Sven Eckelmann | 5346c35 | 2012-05-05 13:27:28 +0200 | [diff] [blame] | 741 | if (ret == -ENOMEM) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 742 | goto err; |
| 743 | } |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame] | 744 | rcu_read_unlock(); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 745 | } |
| 746 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 747 | return 0; |
| 748 | |
| 749 | err: |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame] | 750 | rcu_read_unlock(); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 751 | return -ENOMEM; |
| 752 | } |
| 753 | |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 754 | int batadv_orig_hash_del_if(struct batadv_hard_iface *hard_iface, |
| 755 | int max_if_num) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 756 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 757 | struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface); |
Sven Eckelmann | 5bf74e9 | 2012-06-05 22:31:28 +0200 | [diff] [blame] | 758 | struct batadv_hashtable *hash = bat_priv->orig_hash; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 759 | struct hlist_head *head; |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 760 | struct batadv_hard_iface *hard_iface_tmp; |
| 761 | struct batadv_orig_node *orig_node; |
Antonio Quartulli | d0015fd | 2013-09-03 11:10:23 +0200 | [diff] [blame] | 762 | struct batadv_algo_ops *bao = bat_priv->bat_algo_ops; |
Antonio Quartulli | c90681b | 2011-10-05 17:05:25 +0200 | [diff] [blame] | 763 | uint32_t i; |
| 764 | int ret; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 765 | |
| 766 | /* resize all orig nodes because orig_node->bcast_own(_sum) depend on |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 767 | * if_num |
| 768 | */ |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 769 | for (i = 0; i < hash->size; i++) { |
| 770 | head = &hash->table[i]; |
| 771 | |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame] | 772 | rcu_read_lock(); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 773 | hlist_for_each_entry_rcu(orig_node, head, hash_entry) { |
Antonio Quartulli | d0015fd | 2013-09-03 11:10:23 +0200 | [diff] [blame] | 774 | ret = 0; |
| 775 | if (bao->bat_orig_del_if) |
| 776 | ret = bao->bat_orig_del_if(orig_node, |
| 777 | max_if_num, |
| 778 | hard_iface->if_num); |
Sven Eckelmann | 5346c35 | 2012-05-05 13:27:28 +0200 | [diff] [blame] | 779 | if (ret == -ENOMEM) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 780 | goto err; |
| 781 | } |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame] | 782 | rcu_read_unlock(); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 783 | } |
| 784 | |
| 785 | /* renumber remaining batman interfaces _inside_ of orig_hash_lock */ |
| 786 | rcu_read_lock(); |
Sven Eckelmann | 3193e8f | 2012-05-12 02:09:42 +0200 | [diff] [blame] | 787 | list_for_each_entry_rcu(hard_iface_tmp, &batadv_hardif_list, list) { |
Sven Eckelmann | e9a4f29 | 2012-06-03 22:19:19 +0200 | [diff] [blame] | 788 | if (hard_iface_tmp->if_status == BATADV_IF_NOT_IN_USE) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 789 | continue; |
| 790 | |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 791 | if (hard_iface == hard_iface_tmp) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 792 | continue; |
| 793 | |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 794 | if (hard_iface->soft_iface != hard_iface_tmp->soft_iface) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 795 | continue; |
| 796 | |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 797 | if (hard_iface_tmp->if_num > hard_iface->if_num) |
| 798 | hard_iface_tmp->if_num--; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 799 | } |
| 800 | rcu_read_unlock(); |
| 801 | |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 802 | hard_iface->if_num = -1; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 803 | return 0; |
| 804 | |
| 805 | err: |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame] | 806 | rcu_read_unlock(); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 807 | return -ENOMEM; |
| 808 | } |