Sven Eckelmann | 9f6446c | 2015-04-23 13:16:35 +0200 | [diff] [blame] | 1 | /* Copyright (C) 2009-2015 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 "originator.h" |
Sven Eckelmann | 1e2c2a4 | 2015-04-17 19:40:28 +0200 | [diff] [blame^] | 19 | #include "main.h" |
| 20 | |
| 21 | #include <linux/errno.h> |
| 22 | #include <linux/etherdevice.h> |
| 23 | #include <linux/fs.h> |
| 24 | #include <linux/jiffies.h> |
| 25 | #include <linux/kernel.h> |
| 26 | #include <linux/list.h> |
| 27 | #include <linux/lockdep.h> |
| 28 | #include <linux/netdevice.h> |
| 29 | #include <linux/seq_file.h> |
| 30 | #include <linux/slab.h> |
| 31 | #include <linux/spinlock.h> |
| 32 | #include <linux/workqueue.h> |
| 33 | |
| 34 | #include "distributed-arp-table.h" |
| 35 | #include "fragmentation.h" |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 36 | #include "gateway_client.h" |
| 37 | #include "hard-interface.h" |
Sven Eckelmann | 1e2c2a4 | 2015-04-17 19:40:28 +0200 | [diff] [blame^] | 38 | #include "hash.h" |
Linus Lüssing | 60432d7 | 2014-02-15 17:47:51 +0100 | [diff] [blame] | 39 | #include "multicast.h" |
Sven Eckelmann | 1e2c2a4 | 2015-04-17 19:40:28 +0200 | [diff] [blame^] | 40 | #include "network-coding.h" |
| 41 | #include "routing.h" |
| 42 | #include "translation-table.h" |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 43 | |
Antonio Quartulli | dec0507 | 2012-11-10 11:00:32 +0100 | [diff] [blame] | 44 | /* hash class keys */ |
| 45 | static struct lock_class_key batadv_orig_hash_lock_class_key; |
| 46 | |
Sven Eckelmann | 03fc7f8 | 2012-05-12 18:34:00 +0200 | [diff] [blame] | 47 | static void batadv_purge_orig(struct work_struct *work); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 48 | |
Sven Eckelmann | b8e2dd1 | 2011-06-15 15:08:59 +0200 | [diff] [blame] | 49 | /* returns 1 if they are the same originator */ |
Antonio Quartulli | bbad0a5 | 2013-09-02 12:15:02 +0200 | [diff] [blame] | 50 | int batadv_compare_orig(const struct hlist_node *node, const void *data2) |
Sven Eckelmann | b8e2dd1 | 2011-06-15 15:08:59 +0200 | [diff] [blame] | 51 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 52 | const void *data1 = container_of(node, struct batadv_orig_node, |
| 53 | hash_entry); |
Sven Eckelmann | b8e2dd1 | 2011-06-15 15:08:59 +0200 | [diff] [blame] | 54 | |
dingtianhong | 323813e | 2013-12-26 19:40:39 +0800 | [diff] [blame] | 55 | return batadv_compare_eth(data1, data2); |
Sven Eckelmann | b8e2dd1 | 2011-06-15 15:08:59 +0200 | [diff] [blame] | 56 | } |
| 57 | |
Antonio Quartulli | 7ea7b4a | 2013-07-30 22:16:25 +0200 | [diff] [blame] | 58 | /** |
| 59 | * batadv_orig_node_vlan_get - get an orig_node_vlan object |
| 60 | * @orig_node: the originator serving the VLAN |
| 61 | * @vid: the VLAN identifier |
| 62 | * |
| 63 | * Returns the vlan object identified by vid and belonging to orig_node or NULL |
| 64 | * if it does not exist. |
| 65 | */ |
| 66 | struct batadv_orig_node_vlan * |
| 67 | batadv_orig_node_vlan_get(struct batadv_orig_node *orig_node, |
| 68 | unsigned short vid) |
| 69 | { |
| 70 | struct batadv_orig_node_vlan *vlan = NULL, *tmp; |
| 71 | |
| 72 | rcu_read_lock(); |
| 73 | list_for_each_entry_rcu(tmp, &orig_node->vlan_list, list) { |
| 74 | if (tmp->vid != vid) |
| 75 | continue; |
| 76 | |
| 77 | if (!atomic_inc_not_zero(&tmp->refcount)) |
| 78 | continue; |
| 79 | |
| 80 | vlan = tmp; |
| 81 | |
| 82 | break; |
| 83 | } |
| 84 | rcu_read_unlock(); |
| 85 | |
| 86 | return vlan; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * batadv_orig_node_vlan_new - search and possibly create an orig_node_vlan |
| 91 | * object |
| 92 | * @orig_node: the originator serving the VLAN |
| 93 | * @vid: the VLAN identifier |
| 94 | * |
| 95 | * Returns NULL in case of failure or the vlan object identified by vid and |
| 96 | * belonging to orig_node otherwise. The object is created and added to the list |
| 97 | * if it does not exist. |
| 98 | * |
| 99 | * The object is returned with refcounter increased by 1. |
| 100 | */ |
| 101 | struct batadv_orig_node_vlan * |
| 102 | batadv_orig_node_vlan_new(struct batadv_orig_node *orig_node, |
| 103 | unsigned short vid) |
| 104 | { |
| 105 | struct batadv_orig_node_vlan *vlan; |
| 106 | |
| 107 | spin_lock_bh(&orig_node->vlan_list_lock); |
| 108 | |
| 109 | /* first look if an object for this vid already exists */ |
| 110 | vlan = batadv_orig_node_vlan_get(orig_node, vid); |
| 111 | if (vlan) |
| 112 | goto out; |
| 113 | |
| 114 | vlan = kzalloc(sizeof(*vlan), GFP_ATOMIC); |
| 115 | if (!vlan) |
| 116 | goto out; |
| 117 | |
| 118 | atomic_set(&vlan->refcount, 2); |
| 119 | vlan->vid = vid; |
| 120 | |
| 121 | list_add_rcu(&vlan->list, &orig_node->vlan_list); |
| 122 | |
| 123 | out: |
| 124 | spin_unlock_bh(&orig_node->vlan_list_lock); |
| 125 | |
| 126 | return vlan; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * batadv_orig_node_vlan_free_ref - decrement the refcounter and possibly free |
| 131 | * the originator-vlan object |
| 132 | * @orig_vlan: the originator-vlan object to release |
| 133 | */ |
| 134 | void batadv_orig_node_vlan_free_ref(struct batadv_orig_node_vlan *orig_vlan) |
| 135 | { |
| 136 | if (atomic_dec_and_test(&orig_vlan->refcount)) |
| 137 | kfree_rcu(orig_vlan, rcu); |
| 138 | } |
| 139 | |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 140 | int batadv_originator_init(struct batadv_priv *bat_priv) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 141 | { |
| 142 | if (bat_priv->orig_hash) |
Sven Eckelmann | 5346c35 | 2012-05-05 13:27:28 +0200 | [diff] [blame] | 143 | return 0; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 144 | |
Sven Eckelmann | 1a8eaf0 | 2012-05-12 02:09:32 +0200 | [diff] [blame] | 145 | bat_priv->orig_hash = batadv_hash_new(1024); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 146 | |
| 147 | if (!bat_priv->orig_hash) |
| 148 | goto err; |
| 149 | |
Antonio Quartulli | dec0507 | 2012-11-10 11:00:32 +0100 | [diff] [blame] | 150 | batadv_hash_set_lock_class(bat_priv->orig_hash, |
| 151 | &batadv_orig_hash_lock_class_key); |
| 152 | |
Antonio Quartulli | 7241444 | 2012-12-25 13:14:37 +0100 | [diff] [blame] | 153 | INIT_DELAYED_WORK(&bat_priv->orig_work, batadv_purge_orig); |
| 154 | queue_delayed_work(batadv_event_workqueue, |
| 155 | &bat_priv->orig_work, |
| 156 | msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD)); |
| 157 | |
Sven Eckelmann | 5346c35 | 2012-05-05 13:27:28 +0200 | [diff] [blame] | 158 | return 0; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 159 | |
| 160 | err: |
Sven Eckelmann | 5346c35 | 2012-05-05 13:27:28 +0200 | [diff] [blame] | 161 | return -ENOMEM; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 164 | /** |
| 165 | * batadv_neigh_ifinfo_free_rcu - free the neigh_ifinfo object |
| 166 | * @rcu: rcu pointer of the neigh_ifinfo object |
| 167 | */ |
| 168 | static void batadv_neigh_ifinfo_free_rcu(struct rcu_head *rcu) |
| 169 | { |
| 170 | struct batadv_neigh_ifinfo *neigh_ifinfo; |
| 171 | |
| 172 | neigh_ifinfo = container_of(rcu, struct batadv_neigh_ifinfo, rcu); |
| 173 | |
| 174 | if (neigh_ifinfo->if_outgoing != BATADV_IF_DEFAULT) |
| 175 | batadv_hardif_free_ref_now(neigh_ifinfo->if_outgoing); |
| 176 | |
| 177 | kfree(neigh_ifinfo); |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * batadv_neigh_ifinfo_free_now - decrement the refcounter and possibly free |
| 182 | * the neigh_ifinfo (without rcu callback) |
| 183 | * @neigh_ifinfo: the neigh_ifinfo object to release |
| 184 | */ |
| 185 | static void |
| 186 | batadv_neigh_ifinfo_free_ref_now(struct batadv_neigh_ifinfo *neigh_ifinfo) |
| 187 | { |
| 188 | if (atomic_dec_and_test(&neigh_ifinfo->refcount)) |
| 189 | batadv_neigh_ifinfo_free_rcu(&neigh_ifinfo->rcu); |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * batadv_neigh_ifinfo_free_ref - decrement the refcounter and possibly free |
| 194 | * the neigh_ifinfo |
| 195 | * @neigh_ifinfo: the neigh_ifinfo object to release |
| 196 | */ |
| 197 | void batadv_neigh_ifinfo_free_ref(struct batadv_neigh_ifinfo *neigh_ifinfo) |
| 198 | { |
| 199 | if (atomic_dec_and_test(&neigh_ifinfo->refcount)) |
| 200 | call_rcu(&neigh_ifinfo->rcu, batadv_neigh_ifinfo_free_rcu); |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * batadv_neigh_node_free_rcu - free the neigh_node |
| 205 | * @rcu: rcu pointer of the neigh_node |
| 206 | */ |
| 207 | static void batadv_neigh_node_free_rcu(struct rcu_head *rcu) |
| 208 | { |
| 209 | struct hlist_node *node_tmp; |
| 210 | struct batadv_neigh_node *neigh_node; |
| 211 | struct batadv_neigh_ifinfo *neigh_ifinfo; |
Antonio Quartulli | bcef1f3 | 2015-03-01 00:50:17 +0800 | [diff] [blame] | 212 | struct batadv_algo_ops *bao; |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 213 | |
| 214 | neigh_node = container_of(rcu, struct batadv_neigh_node, rcu); |
Antonio Quartulli | bcef1f3 | 2015-03-01 00:50:17 +0800 | [diff] [blame] | 215 | bao = neigh_node->orig_node->bat_priv->bat_algo_ops; |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 216 | |
| 217 | hlist_for_each_entry_safe(neigh_ifinfo, node_tmp, |
| 218 | &neigh_node->ifinfo_list, list) { |
| 219 | batadv_neigh_ifinfo_free_ref_now(neigh_ifinfo); |
| 220 | } |
Antonio Quartulli | bcef1f3 | 2015-03-01 00:50:17 +0800 | [diff] [blame] | 221 | |
| 222 | if (bao->bat_neigh_free) |
| 223 | bao->bat_neigh_free(neigh_node); |
| 224 | |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 225 | batadv_hardif_free_ref_now(neigh_node->if_incoming); |
| 226 | |
| 227 | kfree(neigh_node); |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * batadv_neigh_node_free_ref_now - decrement the neighbors refcounter |
| 232 | * and possibly free it (without rcu callback) |
| 233 | * @neigh_node: neigh neighbor to free |
| 234 | */ |
| 235 | static void |
| 236 | batadv_neigh_node_free_ref_now(struct batadv_neigh_node *neigh_node) |
| 237 | { |
| 238 | if (atomic_dec_and_test(&neigh_node->refcount)) |
| 239 | batadv_neigh_node_free_rcu(&neigh_node->rcu); |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * batadv_neigh_node_free_ref - decrement the neighbors refcounter |
| 244 | * and possibly free it |
| 245 | * @neigh_node: neigh neighbor to free |
| 246 | */ |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 247 | void batadv_neigh_node_free_ref(struct batadv_neigh_node *neigh_node) |
Simon Wunderlich | a4c135c | 2011-01-19 20:01:43 +0000 | [diff] [blame] | 248 | { |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 249 | if (atomic_dec_and_test(&neigh_node->refcount)) |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 250 | call_rcu(&neigh_node->rcu, batadv_neigh_node_free_rcu); |
Simon Wunderlich | a4c135c | 2011-01-19 20:01:43 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 253 | /** |
| 254 | * batadv_orig_node_get_router - router to the originator depending on iface |
| 255 | * @orig_node: the orig node for the router |
| 256 | * @if_outgoing: the interface where the payload packet has been received or |
| 257 | * the OGM should be sent to |
| 258 | * |
| 259 | * Returns the neighbor which should be router for this orig_node/iface. |
| 260 | * |
| 261 | * The object is returned with refcounter increased by 1. |
| 262 | */ |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 263 | struct batadv_neigh_node * |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 264 | batadv_orig_router_get(struct batadv_orig_node *orig_node, |
| 265 | const struct batadv_hard_iface *if_outgoing) |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 266 | { |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 267 | struct batadv_orig_ifinfo *orig_ifinfo; |
| 268 | struct batadv_neigh_node *router = NULL; |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 269 | |
| 270 | rcu_read_lock(); |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 271 | hlist_for_each_entry_rcu(orig_ifinfo, &orig_node->ifinfo_list, list) { |
| 272 | if (orig_ifinfo->if_outgoing != if_outgoing) |
| 273 | continue; |
| 274 | |
| 275 | router = rcu_dereference(orig_ifinfo->router); |
| 276 | break; |
| 277 | } |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 278 | |
| 279 | if (router && !atomic_inc_not_zero(&router->refcount)) |
| 280 | router = NULL; |
| 281 | |
| 282 | rcu_read_unlock(); |
| 283 | return router; |
| 284 | } |
| 285 | |
Antonio Quartulli | 0538f759 | 2013-09-02 12:15:01 +0200 | [diff] [blame] | 286 | /** |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 287 | * batadv_orig_ifinfo_get - find the ifinfo from an orig_node |
| 288 | * @orig_node: the orig node to be queried |
| 289 | * @if_outgoing: the interface for which the ifinfo should be acquired |
| 290 | * |
| 291 | * Returns the requested orig_ifinfo or NULL if not found. |
| 292 | * |
| 293 | * The object is returned with refcounter increased by 1. |
| 294 | */ |
| 295 | struct batadv_orig_ifinfo * |
| 296 | batadv_orig_ifinfo_get(struct batadv_orig_node *orig_node, |
| 297 | struct batadv_hard_iface *if_outgoing) |
| 298 | { |
| 299 | struct batadv_orig_ifinfo *tmp, *orig_ifinfo = NULL; |
| 300 | |
| 301 | rcu_read_lock(); |
| 302 | hlist_for_each_entry_rcu(tmp, &orig_node->ifinfo_list, |
| 303 | list) { |
| 304 | if (tmp->if_outgoing != if_outgoing) |
| 305 | continue; |
| 306 | |
| 307 | if (!atomic_inc_not_zero(&tmp->refcount)) |
| 308 | continue; |
| 309 | |
| 310 | orig_ifinfo = tmp; |
| 311 | break; |
| 312 | } |
| 313 | rcu_read_unlock(); |
| 314 | |
| 315 | return orig_ifinfo; |
| 316 | } |
| 317 | |
| 318 | /** |
| 319 | * batadv_orig_ifinfo_new - search and possibly create an orig_ifinfo object |
| 320 | * @orig_node: the orig node to be queried |
| 321 | * @if_outgoing: the interface for which the ifinfo should be acquired |
| 322 | * |
| 323 | * Returns NULL in case of failure or the orig_ifinfo object for the if_outgoing |
| 324 | * interface otherwise. The object is created and added to the list |
| 325 | * if it does not exist. |
| 326 | * |
| 327 | * The object is returned with refcounter increased by 1. |
| 328 | */ |
| 329 | struct batadv_orig_ifinfo * |
| 330 | batadv_orig_ifinfo_new(struct batadv_orig_node *orig_node, |
| 331 | struct batadv_hard_iface *if_outgoing) |
| 332 | { |
| 333 | struct batadv_orig_ifinfo *orig_ifinfo = NULL; |
| 334 | unsigned long reset_time; |
| 335 | |
| 336 | spin_lock_bh(&orig_node->neigh_list_lock); |
| 337 | |
| 338 | orig_ifinfo = batadv_orig_ifinfo_get(orig_node, if_outgoing); |
| 339 | if (orig_ifinfo) |
| 340 | goto out; |
| 341 | |
| 342 | orig_ifinfo = kzalloc(sizeof(*orig_ifinfo), GFP_ATOMIC); |
| 343 | if (!orig_ifinfo) |
| 344 | goto out; |
| 345 | |
| 346 | if (if_outgoing != BATADV_IF_DEFAULT && |
| 347 | !atomic_inc_not_zero(&if_outgoing->refcount)) { |
| 348 | kfree(orig_ifinfo); |
| 349 | orig_ifinfo = NULL; |
| 350 | goto out; |
| 351 | } |
| 352 | |
| 353 | reset_time = jiffies - 1; |
| 354 | reset_time -= msecs_to_jiffies(BATADV_RESET_PROTECTION_MS); |
| 355 | orig_ifinfo->batman_seqno_reset = reset_time; |
| 356 | orig_ifinfo->if_outgoing = if_outgoing; |
| 357 | INIT_HLIST_NODE(&orig_ifinfo->list); |
| 358 | atomic_set(&orig_ifinfo->refcount, 2); |
| 359 | hlist_add_head_rcu(&orig_ifinfo->list, |
| 360 | &orig_node->ifinfo_list); |
| 361 | out: |
| 362 | spin_unlock_bh(&orig_node->neigh_list_lock); |
| 363 | return orig_ifinfo; |
| 364 | } |
| 365 | |
| 366 | /** |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 367 | * batadv_neigh_ifinfo_get - find the ifinfo from an neigh_node |
| 368 | * @neigh_node: the neigh node to be queried |
| 369 | * @if_outgoing: the interface for which the ifinfo should be acquired |
| 370 | * |
| 371 | * The object is returned with refcounter increased by 1. |
| 372 | * |
| 373 | * Returns the requested neigh_ifinfo or NULL if not found |
| 374 | */ |
| 375 | struct batadv_neigh_ifinfo * |
| 376 | batadv_neigh_ifinfo_get(struct batadv_neigh_node *neigh, |
| 377 | struct batadv_hard_iface *if_outgoing) |
| 378 | { |
| 379 | struct batadv_neigh_ifinfo *neigh_ifinfo = NULL, |
| 380 | *tmp_neigh_ifinfo; |
| 381 | |
| 382 | rcu_read_lock(); |
| 383 | hlist_for_each_entry_rcu(tmp_neigh_ifinfo, &neigh->ifinfo_list, |
| 384 | list) { |
| 385 | if (tmp_neigh_ifinfo->if_outgoing != if_outgoing) |
| 386 | continue; |
| 387 | |
| 388 | if (!atomic_inc_not_zero(&tmp_neigh_ifinfo->refcount)) |
| 389 | continue; |
| 390 | |
| 391 | neigh_ifinfo = tmp_neigh_ifinfo; |
| 392 | break; |
| 393 | } |
| 394 | rcu_read_unlock(); |
| 395 | |
| 396 | return neigh_ifinfo; |
| 397 | } |
| 398 | |
| 399 | /** |
| 400 | * batadv_neigh_ifinfo_new - search and possibly create an neigh_ifinfo object |
| 401 | * @neigh_node: the neigh node to be queried |
| 402 | * @if_outgoing: the interface for which the ifinfo should be acquired |
| 403 | * |
| 404 | * Returns NULL in case of failure or the neigh_ifinfo object for the |
| 405 | * if_outgoing interface otherwise. The object is created and added to the list |
| 406 | * if it does not exist. |
| 407 | * |
| 408 | * The object is returned with refcounter increased by 1. |
| 409 | */ |
| 410 | struct batadv_neigh_ifinfo * |
| 411 | batadv_neigh_ifinfo_new(struct batadv_neigh_node *neigh, |
| 412 | struct batadv_hard_iface *if_outgoing) |
| 413 | { |
| 414 | struct batadv_neigh_ifinfo *neigh_ifinfo; |
| 415 | |
| 416 | spin_lock_bh(&neigh->ifinfo_lock); |
| 417 | |
| 418 | neigh_ifinfo = batadv_neigh_ifinfo_get(neigh, if_outgoing); |
| 419 | if (neigh_ifinfo) |
| 420 | goto out; |
| 421 | |
| 422 | neigh_ifinfo = kzalloc(sizeof(*neigh_ifinfo), GFP_ATOMIC); |
| 423 | if (!neigh_ifinfo) |
| 424 | goto out; |
| 425 | |
| 426 | if (if_outgoing && !atomic_inc_not_zero(&if_outgoing->refcount)) { |
| 427 | kfree(neigh_ifinfo); |
| 428 | neigh_ifinfo = NULL; |
| 429 | goto out; |
| 430 | } |
| 431 | |
| 432 | INIT_HLIST_NODE(&neigh_ifinfo->list); |
| 433 | atomic_set(&neigh_ifinfo->refcount, 2); |
| 434 | neigh_ifinfo->if_outgoing = if_outgoing; |
| 435 | |
| 436 | hlist_add_head_rcu(&neigh_ifinfo->list, &neigh->ifinfo_list); |
| 437 | |
| 438 | out: |
| 439 | spin_unlock_bh(&neigh->ifinfo_lock); |
| 440 | |
| 441 | return neigh_ifinfo; |
| 442 | } |
| 443 | |
| 444 | /** |
Antonio Quartulli | 0538f759 | 2013-09-02 12:15:01 +0200 | [diff] [blame] | 445 | * batadv_neigh_node_new - create and init a new neigh_node object |
| 446 | * @hard_iface: the interface where the neighbour is connected to |
| 447 | * @neigh_addr: the mac address of the neighbour interface |
| 448 | * @orig_node: originator object representing the neighbour |
| 449 | * |
| 450 | * Allocates a new neigh_node object and initialises all the generic fields. |
| 451 | * Returns the new object or NULL on failure. |
| 452 | */ |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 453 | struct batadv_neigh_node * |
| 454 | batadv_neigh_node_new(struct batadv_hard_iface *hard_iface, |
Antonio Quartulli | 0538f759 | 2013-09-02 12:15:01 +0200 | [diff] [blame] | 455 | const uint8_t *neigh_addr, |
| 456 | struct batadv_orig_node *orig_node) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 457 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 458 | struct batadv_neigh_node *neigh_node; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 459 | |
Sven Eckelmann | 704509b | 2011-05-14 23:14:54 +0200 | [diff] [blame] | 460 | neigh_node = kzalloc(sizeof(*neigh_node), GFP_ATOMIC); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 461 | if (!neigh_node) |
Marek Lindner | 7ae8b28 | 2012-03-01 15:35:21 +0800 | [diff] [blame] | 462 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 463 | |
Marek Lindner | 9591a79 | 2010-12-12 21:57:11 +0000 | [diff] [blame] | 464 | INIT_HLIST_NODE(&neigh_node->list); |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 465 | INIT_HLIST_HEAD(&neigh_node->ifinfo_list); |
| 466 | spin_lock_init(&neigh_node->ifinfo_lock); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 467 | |
Antonio Quartulli | 8fdd015 | 2014-01-22 00:42:11 +0100 | [diff] [blame] | 468 | ether_addr_copy(neigh_node->addr, neigh_addr); |
Antonio Quartulli | 0538f759 | 2013-09-02 12:15:01 +0200 | [diff] [blame] | 469 | neigh_node->if_incoming = hard_iface; |
| 470 | neigh_node->orig_node = orig_node; |
| 471 | |
Marek Lindner | 1605d0d | 2011-02-18 12:28:11 +0000 | [diff] [blame] | 472 | /* extra reference for return */ |
| 473 | atomic_set(&neigh_node->refcount, 2); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 474 | |
Marek Lindner | 7ae8b28 | 2012-03-01 15:35:21 +0800 | [diff] [blame] | 475 | out: |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 476 | return neigh_node; |
| 477 | } |
| 478 | |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 479 | /** |
Antonio Quartulli | 08bf0ed | 2014-01-29 11:25:12 +0100 | [diff] [blame] | 480 | * batadv_neigh_node_get - retrieve a neighbour from the list |
| 481 | * @orig_node: originator which the neighbour belongs to |
| 482 | * @hard_iface: the interface where this neighbour is connected to |
| 483 | * @addr: the address of the neighbour |
| 484 | * |
| 485 | * Looks for and possibly returns a neighbour belonging to this originator list |
| 486 | * which is connected through the provided hard interface. |
| 487 | * Returns NULL if the neighbour is not found. |
| 488 | */ |
| 489 | struct batadv_neigh_node * |
| 490 | batadv_neigh_node_get(const struct batadv_orig_node *orig_node, |
| 491 | const struct batadv_hard_iface *hard_iface, |
| 492 | const uint8_t *addr) |
| 493 | { |
| 494 | struct batadv_neigh_node *tmp_neigh_node, *res = NULL; |
| 495 | |
| 496 | rcu_read_lock(); |
| 497 | hlist_for_each_entry_rcu(tmp_neigh_node, &orig_node->neigh_list, list) { |
| 498 | if (!batadv_compare_eth(tmp_neigh_node->addr, addr)) |
| 499 | continue; |
| 500 | |
| 501 | if (tmp_neigh_node->if_incoming != hard_iface) |
| 502 | continue; |
| 503 | |
| 504 | if (!atomic_inc_not_zero(&tmp_neigh_node->refcount)) |
| 505 | continue; |
| 506 | |
| 507 | res = tmp_neigh_node; |
| 508 | break; |
| 509 | } |
| 510 | rcu_read_unlock(); |
| 511 | |
| 512 | return res; |
| 513 | } |
| 514 | |
| 515 | /** |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 516 | * batadv_orig_ifinfo_free_rcu - free the orig_ifinfo object |
| 517 | * @rcu: rcu pointer of the orig_ifinfo object |
| 518 | */ |
| 519 | static void batadv_orig_ifinfo_free_rcu(struct rcu_head *rcu) |
| 520 | { |
| 521 | struct batadv_orig_ifinfo *orig_ifinfo; |
Simon Wunderlich | 000c8df | 2014-03-26 15:46:22 +0100 | [diff] [blame] | 522 | struct batadv_neigh_node *router; |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 523 | |
| 524 | orig_ifinfo = container_of(rcu, struct batadv_orig_ifinfo, rcu); |
| 525 | |
| 526 | if (orig_ifinfo->if_outgoing != BATADV_IF_DEFAULT) |
| 527 | batadv_hardif_free_ref_now(orig_ifinfo->if_outgoing); |
| 528 | |
Simon Wunderlich | 000c8df | 2014-03-26 15:46:22 +0100 | [diff] [blame] | 529 | /* this is the last reference to this object */ |
| 530 | router = rcu_dereference_protected(orig_ifinfo->router, true); |
| 531 | if (router) |
| 532 | batadv_neigh_node_free_ref_now(router); |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 533 | kfree(orig_ifinfo); |
| 534 | } |
| 535 | |
| 536 | /** |
| 537 | * batadv_orig_ifinfo_free_ref - decrement the refcounter and possibly free |
| 538 | * the orig_ifinfo (without rcu callback) |
| 539 | * @orig_ifinfo: the orig_ifinfo object to release |
| 540 | */ |
| 541 | static void |
| 542 | batadv_orig_ifinfo_free_ref_now(struct batadv_orig_ifinfo *orig_ifinfo) |
| 543 | { |
| 544 | if (atomic_dec_and_test(&orig_ifinfo->refcount)) |
| 545 | batadv_orig_ifinfo_free_rcu(&orig_ifinfo->rcu); |
| 546 | } |
| 547 | |
| 548 | /** |
| 549 | * batadv_orig_ifinfo_free_ref - decrement the refcounter and possibly free |
| 550 | * the orig_ifinfo |
| 551 | * @orig_ifinfo: the orig_ifinfo object to release |
| 552 | */ |
| 553 | void batadv_orig_ifinfo_free_ref(struct batadv_orig_ifinfo *orig_ifinfo) |
| 554 | { |
| 555 | if (atomic_dec_and_test(&orig_ifinfo->refcount)) |
| 556 | call_rcu(&orig_ifinfo->rcu, batadv_orig_ifinfo_free_rcu); |
| 557 | } |
| 558 | |
Sven Eckelmann | 03fc7f8 | 2012-05-12 18:34:00 +0200 | [diff] [blame] | 559 | static void batadv_orig_node_free_rcu(struct rcu_head *rcu) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 560 | { |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 561 | struct hlist_node *node_tmp; |
Simon Wunderlich | f6c8b71 | 2013-11-13 19:14:45 +0100 | [diff] [blame] | 562 | struct batadv_neigh_node *neigh_node; |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 563 | struct batadv_orig_node *orig_node; |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 564 | struct batadv_orig_ifinfo *orig_ifinfo; |
Marek Lindner | 16b1aba | 2011-01-19 20:01:42 +0000 | [diff] [blame] | 565 | |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 566 | orig_node = container_of(rcu, struct batadv_orig_node, rcu); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 567 | |
Marek Lindner | f987ed6 | 2010-12-12 21:57:12 +0000 | [diff] [blame] | 568 | spin_lock_bh(&orig_node->neigh_list_lock); |
| 569 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 570 | /* for all neighbors towards this originator ... */ |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 571 | hlist_for_each_entry_safe(neigh_node, node_tmp, |
Marek Lindner | 9591a79 | 2010-12-12 21:57:11 +0000 | [diff] [blame] | 572 | &orig_node->neigh_list, list) { |
Marek Lindner | f987ed6 | 2010-12-12 21:57:12 +0000 | [diff] [blame] | 573 | hlist_del_rcu(&neigh_node->list); |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 574 | batadv_neigh_node_free_ref_now(neigh_node); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 575 | } |
| 576 | |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 577 | hlist_for_each_entry_safe(orig_ifinfo, node_tmp, |
| 578 | &orig_node->ifinfo_list, list) { |
| 579 | hlist_del_rcu(&orig_ifinfo->list); |
| 580 | batadv_orig_ifinfo_free_ref_now(orig_ifinfo); |
| 581 | } |
Marek Lindner | f987ed6 | 2010-12-12 21:57:12 +0000 | [diff] [blame] | 582 | spin_unlock_bh(&orig_node->neigh_list_lock); |
| 583 | |
Linus Lüssing | 60432d7 | 2014-02-15 17:47:51 +0100 | [diff] [blame] | 584 | batadv_mcast_purge_orig(orig_node); |
| 585 | |
Martin Hundebøll | d56b170 | 2013-01-25 11:12:39 +0100 | [diff] [blame] | 586 | /* Free nc_nodes */ |
| 587 | batadv_nc_purge_orig(orig_node->bat_priv, orig_node, NULL); |
| 588 | |
Martin Hundebøll | 610bfc6bc | 2013-05-23 16:53:02 +0200 | [diff] [blame] | 589 | batadv_frag_purge_orig(orig_node, NULL); |
| 590 | |
Antonio Quartulli | d0015fd | 2013-09-03 11:10:23 +0200 | [diff] [blame] | 591 | if (orig_node->bat_priv->bat_algo_ops->bat_orig_free) |
| 592 | orig_node->bat_priv->bat_algo_ops->bat_orig_free(orig_node); |
| 593 | |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 594 | kfree(orig_node->tt_buff); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 595 | kfree(orig_node); |
| 596 | } |
| 597 | |
Linus Lüssing | 7282222 | 2013-04-15 21:43:29 +0800 | [diff] [blame] | 598 | /** |
| 599 | * batadv_orig_node_free_ref - decrement the orig node refcounter and possibly |
| 600 | * schedule an rcu callback for freeing it |
| 601 | * @orig_node: the orig node to free |
| 602 | */ |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 603 | void batadv_orig_node_free_ref(struct batadv_orig_node *orig_node) |
Marek Lindner | 7b36e8e | 2011-02-18 12:28:10 +0000 | [diff] [blame] | 604 | { |
| 605 | if (atomic_dec_and_test(&orig_node->refcount)) |
Sven Eckelmann | 03fc7f8 | 2012-05-12 18:34:00 +0200 | [diff] [blame] | 606 | call_rcu(&orig_node->rcu, batadv_orig_node_free_rcu); |
Marek Lindner | 7b36e8e | 2011-02-18 12:28:10 +0000 | [diff] [blame] | 607 | } |
| 608 | |
Linus Lüssing | 7282222 | 2013-04-15 21:43:29 +0800 | [diff] [blame] | 609 | /** |
| 610 | * batadv_orig_node_free_ref_now - decrement the orig node refcounter and |
| 611 | * possibly free it (without rcu callback) |
| 612 | * @orig_node: the orig node to free |
| 613 | */ |
| 614 | void batadv_orig_node_free_ref_now(struct batadv_orig_node *orig_node) |
| 615 | { |
| 616 | if (atomic_dec_and_test(&orig_node->refcount)) |
| 617 | batadv_orig_node_free_rcu(&orig_node->rcu); |
| 618 | } |
| 619 | |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 620 | void batadv_originator_free(struct batadv_priv *bat_priv) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 621 | { |
Sven Eckelmann | 5bf74e9 | 2012-06-05 22:31:28 +0200 | [diff] [blame] | 622 | struct batadv_hashtable *hash = bat_priv->orig_hash; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 623 | struct hlist_node *node_tmp; |
Marek Lindner | 16b1aba | 2011-01-19 20:01:42 +0000 | [diff] [blame] | 624 | struct hlist_head *head; |
Marek Lindner | 16b1aba | 2011-01-19 20:01:42 +0000 | [diff] [blame] | 625 | spinlock_t *list_lock; /* spinlock to protect write access */ |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 626 | struct batadv_orig_node *orig_node; |
Antonio Quartulli | c90681b | 2011-10-05 17:05:25 +0200 | [diff] [blame] | 627 | uint32_t i; |
Marek Lindner | 16b1aba | 2011-01-19 20:01:42 +0000 | [diff] [blame] | 628 | |
| 629 | if (!hash) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 630 | return; |
| 631 | |
| 632 | cancel_delayed_work_sync(&bat_priv->orig_work); |
| 633 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 634 | bat_priv->orig_hash = NULL; |
Marek Lindner | 16b1aba | 2011-01-19 20:01:42 +0000 | [diff] [blame] | 635 | |
| 636 | for (i = 0; i < hash->size; i++) { |
| 637 | head = &hash->table[i]; |
| 638 | list_lock = &hash->list_locks[i]; |
| 639 | |
| 640 | spin_lock_bh(list_lock); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 641 | hlist_for_each_entry_safe(orig_node, node_tmp, |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 642 | head, hash_entry) { |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 643 | hlist_del_rcu(&orig_node->hash_entry); |
Sven Eckelmann | 7d211ef | 2012-05-12 02:09:34 +0200 | [diff] [blame] | 644 | batadv_orig_node_free_ref(orig_node); |
Marek Lindner | 16b1aba | 2011-01-19 20:01:42 +0000 | [diff] [blame] | 645 | } |
| 646 | spin_unlock_bh(list_lock); |
| 647 | } |
| 648 | |
Sven Eckelmann | 1a8eaf0 | 2012-05-12 02:09:32 +0200 | [diff] [blame] | 649 | batadv_hash_destroy(hash); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 650 | } |
| 651 | |
Antonio Quartulli | bbad0a5 | 2013-09-02 12:15:02 +0200 | [diff] [blame] | 652 | /** |
| 653 | * batadv_orig_node_new - creates a new orig_node |
| 654 | * @bat_priv: the bat priv with all the soft interface information |
| 655 | * @addr: the mac address of the originator |
| 656 | * |
| 657 | * Creates a new originator object and initialise all the generic fields. |
| 658 | * The new object is not added to the originator list. |
| 659 | * Returns the newly created object or NULL on failure. |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 660 | */ |
Antonio Quartulli | bbad0a5 | 2013-09-02 12:15:02 +0200 | [diff] [blame] | 661 | 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] | 662 | const uint8_t *addr) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 663 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 664 | struct batadv_orig_node *orig_node; |
Antonio Quartulli | 7ea7b4a | 2013-07-30 22:16:25 +0200 | [diff] [blame] | 665 | struct batadv_orig_node_vlan *vlan; |
Sven Eckelmann | 42d0b04 | 2012-06-03 22:19:17 +0200 | [diff] [blame] | 666 | unsigned long reset_time; |
Antonio Quartulli | bbad0a5 | 2013-09-02 12:15:02 +0200 | [diff] [blame] | 667 | int i; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 668 | |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 669 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
| 670 | "Creating new originator: %pM\n", addr); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 671 | |
Sven Eckelmann | 704509b | 2011-05-14 23:14:54 +0200 | [diff] [blame] | 672 | orig_node = kzalloc(sizeof(*orig_node), GFP_ATOMIC); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 673 | if (!orig_node) |
| 674 | return NULL; |
| 675 | |
Marek Lindner | 9591a79 | 2010-12-12 21:57:11 +0000 | [diff] [blame] | 676 | INIT_HLIST_HEAD(&orig_node->neigh_list); |
Antonio Quartulli | 7ea7b4a | 2013-07-30 22:16:25 +0200 | [diff] [blame] | 677 | INIT_LIST_HEAD(&orig_node->vlan_list); |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 678 | INIT_HLIST_HEAD(&orig_node->ifinfo_list); |
Marek Lindner | f3e0008 | 2011-01-25 21:52:11 +0000 | [diff] [blame] | 679 | spin_lock_init(&orig_node->bcast_seqno_lock); |
Marek Lindner | f987ed6 | 2010-12-12 21:57:12 +0000 | [diff] [blame] | 680 | spin_lock_init(&orig_node->neigh_list_lock); |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 681 | spin_lock_init(&orig_node->tt_buff_lock); |
Antonio Quartulli | a70a9aa | 2013-07-30 22:16:24 +0200 | [diff] [blame] | 682 | spin_lock_init(&orig_node->tt_lock); |
Antonio Quartulli | 7ea7b4a | 2013-07-30 22:16:25 +0200 | [diff] [blame] | 683 | spin_lock_init(&orig_node->vlan_list_lock); |
Marek Lindner | 7b36e8e | 2011-02-18 12:28:10 +0000 | [diff] [blame] | 684 | |
Martin Hundebøll | d56b170 | 2013-01-25 11:12:39 +0100 | [diff] [blame] | 685 | batadv_nc_init_orig(orig_node); |
| 686 | |
Marek Lindner | 7b36e8e | 2011-02-18 12:28:10 +0000 | [diff] [blame] | 687 | /* extra reference for return */ |
| 688 | atomic_set(&orig_node->refcount, 2); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 689 | |
Marek Lindner | 16b1aba | 2011-01-19 20:01:42 +0000 | [diff] [blame] | 690 | orig_node->bat_priv = bat_priv; |
Antonio Quartulli | 8fdd015 | 2014-01-22 00:42:11 +0100 | [diff] [blame] | 691 | ether_addr_copy(orig_node->orig, addr); |
Antonio Quartulli | 785ea11 | 2011-11-23 11:35:44 +0100 | [diff] [blame] | 692 | batadv_dat_init_orig_node_addr(orig_node); |
Antonio Quartulli | c8c991b | 2011-07-07 01:40:57 +0200 | [diff] [blame] | 693 | atomic_set(&orig_node->last_ttvn, 0); |
Antonio Quartulli | 2dafb49 | 2011-05-05 08:42:45 +0200 | [diff] [blame] | 694 | orig_node->tt_buff = NULL; |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 695 | orig_node->tt_buff_len = 0; |
Linus Lüssing | 2c667a3 | 2014-10-30 06:23:40 +0100 | [diff] [blame] | 696 | orig_node->last_seen = jiffies; |
Sven Eckelmann | 42d0b04 | 2012-06-03 22:19:17 +0200 | [diff] [blame] | 697 | reset_time = jiffies - 1 - msecs_to_jiffies(BATADV_RESET_PROTECTION_MS); |
| 698 | orig_node->bcast_seqno_reset = reset_time; |
Linus Lüssing | 60432d7 | 2014-02-15 17:47:51 +0100 | [diff] [blame] | 699 | #ifdef CONFIG_BATMAN_ADV_MCAST |
| 700 | orig_node->mcast_flags = BATADV_NO_FLAGS; |
| 701 | #endif |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 702 | |
Antonio Quartulli | 7ea7b4a | 2013-07-30 22:16:25 +0200 | [diff] [blame] | 703 | /* create a vlan object for the "untagged" LAN */ |
| 704 | vlan = batadv_orig_node_vlan_new(orig_node, BATADV_NO_FLAGS); |
| 705 | if (!vlan) |
| 706 | goto free_orig_node; |
| 707 | /* batadv_orig_node_vlan_new() increases the refcounter. |
| 708 | * Immediately release vlan since it is not needed anymore in this |
| 709 | * context |
| 710 | */ |
| 711 | batadv_orig_node_vlan_free_ref(vlan); |
| 712 | |
Martin Hundebøll | 610bfc6bc | 2013-05-23 16:53:02 +0200 | [diff] [blame] | 713 | for (i = 0; i < BATADV_FRAG_BUFFER_COUNT; i++) { |
| 714 | INIT_HLIST_HEAD(&orig_node->fragments[i].head); |
| 715 | spin_lock_init(&orig_node->fragments[i].lock); |
| 716 | orig_node->fragments[i].size = 0; |
| 717 | } |
| 718 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 719 | return orig_node; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 720 | free_orig_node: |
| 721 | kfree(orig_node); |
| 722 | return NULL; |
| 723 | } |
| 724 | |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 725 | /** |
Simon Wunderlich | 709de13 | 2014-03-26 15:46:24 +0100 | [diff] [blame] | 726 | * batadv_purge_neigh_ifinfo - purge obsolete ifinfo entries from neighbor |
| 727 | * @bat_priv: the bat priv with all the soft interface information |
| 728 | * @neigh: orig node which is to be checked |
| 729 | */ |
| 730 | static void |
| 731 | batadv_purge_neigh_ifinfo(struct batadv_priv *bat_priv, |
| 732 | struct batadv_neigh_node *neigh) |
| 733 | { |
| 734 | struct batadv_neigh_ifinfo *neigh_ifinfo; |
| 735 | struct batadv_hard_iface *if_outgoing; |
| 736 | struct hlist_node *node_tmp; |
| 737 | |
| 738 | spin_lock_bh(&neigh->ifinfo_lock); |
| 739 | |
| 740 | /* for all ifinfo objects for this neighinator */ |
| 741 | hlist_for_each_entry_safe(neigh_ifinfo, node_tmp, |
| 742 | &neigh->ifinfo_list, list) { |
| 743 | if_outgoing = neigh_ifinfo->if_outgoing; |
| 744 | |
| 745 | /* always keep the default interface */ |
| 746 | if (if_outgoing == BATADV_IF_DEFAULT) |
| 747 | continue; |
| 748 | |
| 749 | /* don't purge if the interface is not (going) down */ |
| 750 | if ((if_outgoing->if_status != BATADV_IF_INACTIVE) && |
| 751 | (if_outgoing->if_status != BATADV_IF_NOT_IN_USE) && |
| 752 | (if_outgoing->if_status != BATADV_IF_TO_BE_REMOVED)) |
| 753 | continue; |
| 754 | |
| 755 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
| 756 | "neighbor/ifinfo purge: neighbor %pM, iface: %s\n", |
| 757 | neigh->addr, if_outgoing->net_dev->name); |
| 758 | |
| 759 | hlist_del_rcu(&neigh_ifinfo->list); |
| 760 | batadv_neigh_ifinfo_free_ref(neigh_ifinfo); |
| 761 | } |
| 762 | |
| 763 | spin_unlock_bh(&neigh->ifinfo_lock); |
| 764 | } |
| 765 | |
| 766 | /** |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 767 | * batadv_purge_orig_ifinfo - purge obsolete ifinfo entries from originator |
| 768 | * @bat_priv: the bat priv with all the soft interface information |
| 769 | * @orig_node: orig node which is to be checked |
| 770 | * |
| 771 | * Returns true if any ifinfo entry was purged, false otherwise. |
| 772 | */ |
| 773 | static bool |
| 774 | batadv_purge_orig_ifinfo(struct batadv_priv *bat_priv, |
| 775 | struct batadv_orig_node *orig_node) |
| 776 | { |
| 777 | struct batadv_orig_ifinfo *orig_ifinfo; |
| 778 | struct batadv_hard_iface *if_outgoing; |
| 779 | struct hlist_node *node_tmp; |
| 780 | bool ifinfo_purged = false; |
| 781 | |
| 782 | spin_lock_bh(&orig_node->neigh_list_lock); |
| 783 | |
| 784 | /* for all ifinfo objects for this originator */ |
| 785 | hlist_for_each_entry_safe(orig_ifinfo, node_tmp, |
| 786 | &orig_node->ifinfo_list, list) { |
| 787 | if_outgoing = orig_ifinfo->if_outgoing; |
| 788 | |
| 789 | /* always keep the default interface */ |
| 790 | if (if_outgoing == BATADV_IF_DEFAULT) |
| 791 | continue; |
| 792 | |
| 793 | /* don't purge if the interface is not (going) down */ |
| 794 | if ((if_outgoing->if_status != BATADV_IF_INACTIVE) && |
| 795 | (if_outgoing->if_status != BATADV_IF_NOT_IN_USE) && |
| 796 | (if_outgoing->if_status != BATADV_IF_TO_BE_REMOVED)) |
| 797 | continue; |
| 798 | |
| 799 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
| 800 | "router/ifinfo purge: originator %pM, iface: %s\n", |
| 801 | orig_node->orig, if_outgoing->net_dev->name); |
| 802 | |
| 803 | ifinfo_purged = true; |
| 804 | |
| 805 | hlist_del_rcu(&orig_ifinfo->list); |
| 806 | batadv_orig_ifinfo_free_ref(orig_ifinfo); |
Simon Wunderlich | f3b3d90 | 2013-11-13 19:14:50 +0100 | [diff] [blame] | 807 | if (orig_node->last_bonding_candidate == orig_ifinfo) { |
| 808 | orig_node->last_bonding_candidate = NULL; |
| 809 | batadv_orig_ifinfo_free_ref(orig_ifinfo); |
| 810 | } |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 811 | } |
| 812 | |
| 813 | spin_unlock_bh(&orig_node->neigh_list_lock); |
| 814 | |
| 815 | return ifinfo_purged; |
| 816 | } |
| 817 | |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 818 | /** |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 819 | * batadv_purge_orig_neighbors - purges neighbors from originator |
| 820 | * @bat_priv: the bat priv with all the soft interface information |
| 821 | * @orig_node: orig node which is to be checked |
| 822 | * |
| 823 | * Returns true if any neighbor was purged, false otherwise |
| 824 | */ |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 825 | static bool |
| 826 | batadv_purge_orig_neighbors(struct batadv_priv *bat_priv, |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 827 | struct batadv_orig_node *orig_node) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 828 | { |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 829 | struct hlist_node *node_tmp; |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 830 | struct batadv_neigh_node *neigh_node; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 831 | bool neigh_purged = false; |
Marek Lindner | 0b0094e | 2012-03-01 15:35:20 +0800 | [diff] [blame] | 832 | unsigned long last_seen; |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 833 | struct batadv_hard_iface *if_incoming; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 834 | |
Marek Lindner | f987ed6 | 2010-12-12 21:57:12 +0000 | [diff] [blame] | 835 | spin_lock_bh(&orig_node->neigh_list_lock); |
| 836 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 837 | /* for all neighbors towards this originator ... */ |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 838 | hlist_for_each_entry_safe(neigh_node, node_tmp, |
Marek Lindner | 9591a79 | 2010-12-12 21:57:11 +0000 | [diff] [blame] | 839 | &orig_node->neigh_list, list) { |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 840 | last_seen = neigh_node->last_seen; |
| 841 | if_incoming = neigh_node->if_incoming; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 842 | |
Sven Eckelmann | 42d0b04 | 2012-06-03 22:19:17 +0200 | [diff] [blame] | 843 | if ((batadv_has_timed_out(last_seen, BATADV_PURGE_TIMEOUT)) || |
Sven Eckelmann | e9a4f29 | 2012-06-03 22:19:19 +0200 | [diff] [blame] | 844 | (if_incoming->if_status == BATADV_IF_INACTIVE) || |
| 845 | (if_incoming->if_status == BATADV_IF_NOT_IN_USE) || |
| 846 | (if_incoming->if_status == BATADV_IF_TO_BE_REMOVED)) { |
Sven Eckelmann | e9a4f29 | 2012-06-03 22:19:19 +0200 | [diff] [blame] | 847 | if ((if_incoming->if_status == BATADV_IF_INACTIVE) || |
| 848 | (if_incoming->if_status == BATADV_IF_NOT_IN_USE) || |
| 849 | (if_incoming->if_status == BATADV_IF_TO_BE_REMOVED)) |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 850 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 851 | "neighbor purge: originator %pM, neighbor: %pM, iface: %s\n", |
| 852 | orig_node->orig, neigh_node->addr, |
| 853 | if_incoming->net_dev->name); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 854 | else |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 855 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 856 | "neighbor timeout: originator %pM, neighbor: %pM, last_seen: %u\n", |
| 857 | orig_node->orig, neigh_node->addr, |
| 858 | jiffies_to_msecs(last_seen)); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 859 | |
| 860 | neigh_purged = true; |
Marek Lindner | 9591a79 | 2010-12-12 21:57:11 +0000 | [diff] [blame] | 861 | |
Marek Lindner | f987ed6 | 2010-12-12 21:57:12 +0000 | [diff] [blame] | 862 | hlist_del_rcu(&neigh_node->list); |
Sven Eckelmann | 7d211ef | 2012-05-12 02:09:34 +0200 | [diff] [blame] | 863 | batadv_neigh_node_free_ref(neigh_node); |
Simon Wunderlich | 709de13 | 2014-03-26 15:46:24 +0100 | [diff] [blame] | 864 | } else { |
| 865 | /* only necessary if not the whole neighbor is to be |
| 866 | * deleted, but some interface has been removed. |
| 867 | */ |
| 868 | batadv_purge_neigh_ifinfo(bat_priv, neigh_node); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 869 | } |
| 870 | } |
Marek Lindner | f987ed6 | 2010-12-12 21:57:12 +0000 | [diff] [blame] | 871 | |
| 872 | spin_unlock_bh(&orig_node->neigh_list_lock); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 873 | return neigh_purged; |
| 874 | } |
| 875 | |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 876 | /** |
| 877 | * batadv_find_best_neighbor - finds the best neighbor after purging |
| 878 | * @bat_priv: the bat priv with all the soft interface information |
| 879 | * @orig_node: orig node which is to be checked |
| 880 | * @if_outgoing: the interface for which the metric should be compared |
| 881 | * |
| 882 | * Returns the current best neighbor, with refcount increased. |
| 883 | */ |
| 884 | static struct batadv_neigh_node * |
| 885 | batadv_find_best_neighbor(struct batadv_priv *bat_priv, |
| 886 | struct batadv_orig_node *orig_node, |
| 887 | struct batadv_hard_iface *if_outgoing) |
| 888 | { |
| 889 | struct batadv_neigh_node *best = NULL, *neigh; |
| 890 | struct batadv_algo_ops *bao = bat_priv->bat_algo_ops; |
| 891 | |
| 892 | rcu_read_lock(); |
| 893 | hlist_for_each_entry_rcu(neigh, &orig_node->neigh_list, list) { |
| 894 | if (best && (bao->bat_neigh_cmp(neigh, if_outgoing, |
| 895 | best, if_outgoing) <= 0)) |
| 896 | continue; |
| 897 | |
| 898 | if (!atomic_inc_not_zero(&neigh->refcount)) |
| 899 | continue; |
| 900 | |
| 901 | if (best) |
| 902 | batadv_neigh_node_free_ref(best); |
| 903 | |
| 904 | best = neigh; |
| 905 | } |
| 906 | rcu_read_unlock(); |
| 907 | |
| 908 | return best; |
| 909 | } |
| 910 | |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 911 | /** |
| 912 | * batadv_purge_orig_node - purges obsolete information from an orig_node |
| 913 | * @bat_priv: the bat priv with all the soft interface information |
| 914 | * @orig_node: orig node which is to be checked |
| 915 | * |
| 916 | * This function checks if the orig_node or substructures of it have become |
| 917 | * obsolete, and purges this information if that's the case. |
| 918 | * |
| 919 | * Returns true if the orig_node is to be removed, false otherwise. |
| 920 | */ |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 921 | static bool batadv_purge_orig_node(struct batadv_priv *bat_priv, |
| 922 | struct batadv_orig_node *orig_node) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 923 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 924 | struct batadv_neigh_node *best_neigh_node; |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 925 | struct batadv_hard_iface *hard_iface; |
Simon Wunderlich | 7b955a9 | 2014-03-26 15:46:23 +0100 | [diff] [blame] | 926 | bool changed_ifinfo, changed_neigh; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 927 | |
Sven Eckelmann | 42d0b04 | 2012-06-03 22:19:17 +0200 | [diff] [blame] | 928 | if (batadv_has_timed_out(orig_node->last_seen, |
| 929 | 2 * BATADV_PURGE_TIMEOUT)) { |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 930 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 931 | "Originator timeout: originator %pM, last_seen %u\n", |
| 932 | orig_node->orig, |
| 933 | jiffies_to_msecs(orig_node->last_seen)); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 934 | return true; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 935 | } |
Simon Wunderlich | 7b955a9 | 2014-03-26 15:46:23 +0100 | [diff] [blame] | 936 | changed_ifinfo = batadv_purge_orig_ifinfo(bat_priv, orig_node); |
| 937 | changed_neigh = batadv_purge_orig_neighbors(bat_priv, orig_node); |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 938 | |
Simon Wunderlich | 7b955a9 | 2014-03-26 15:46:23 +0100 | [diff] [blame] | 939 | if (!changed_ifinfo && !changed_neigh) |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 940 | return false; |
| 941 | |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 942 | /* first for NULL ... */ |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 943 | best_neigh_node = batadv_find_best_neighbor(bat_priv, orig_node, |
| 944 | BATADV_IF_DEFAULT); |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 945 | batadv_update_route(bat_priv, orig_node, BATADV_IF_DEFAULT, |
| 946 | best_neigh_node); |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 947 | if (best_neigh_node) |
| 948 | batadv_neigh_node_free_ref(best_neigh_node); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 949 | |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 950 | /* ... then for all other interfaces. */ |
| 951 | rcu_read_lock(); |
| 952 | list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) { |
| 953 | if (hard_iface->if_status != BATADV_IF_ACTIVE) |
| 954 | continue; |
| 955 | |
| 956 | if (hard_iface->soft_iface != bat_priv->soft_iface) |
| 957 | continue; |
| 958 | |
| 959 | best_neigh_node = batadv_find_best_neighbor(bat_priv, |
| 960 | orig_node, |
| 961 | hard_iface); |
| 962 | batadv_update_route(bat_priv, orig_node, hard_iface, |
| 963 | best_neigh_node); |
| 964 | if (best_neigh_node) |
| 965 | batadv_neigh_node_free_ref(best_neigh_node); |
| 966 | } |
| 967 | rcu_read_unlock(); |
| 968 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 969 | return false; |
| 970 | } |
| 971 | |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 972 | static void _batadv_purge_orig(struct batadv_priv *bat_priv) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 973 | { |
Sven Eckelmann | 5bf74e9 | 2012-06-05 22:31:28 +0200 | [diff] [blame] | 974 | struct batadv_hashtable *hash = bat_priv->orig_hash; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 975 | struct hlist_node *node_tmp; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 976 | struct hlist_head *head; |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame] | 977 | spinlock_t *list_lock; /* spinlock to protect write access */ |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 978 | struct batadv_orig_node *orig_node; |
Antonio Quartulli | c90681b | 2011-10-05 17:05:25 +0200 | [diff] [blame] | 979 | uint32_t i; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 980 | |
| 981 | if (!hash) |
| 982 | return; |
| 983 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 984 | /* for all origins... */ |
| 985 | for (i = 0; i < hash->size; i++) { |
| 986 | head = &hash->table[i]; |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame] | 987 | list_lock = &hash->list_locks[i]; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 988 | |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame] | 989 | spin_lock_bh(list_lock); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 990 | hlist_for_each_entry_safe(orig_node, node_tmp, |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 991 | head, hash_entry) { |
Sven Eckelmann | 03fc7f8 | 2012-05-12 18:34:00 +0200 | [diff] [blame] | 992 | if (batadv_purge_orig_node(bat_priv, orig_node)) { |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 993 | batadv_gw_node_delete(bat_priv, orig_node); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 994 | hlist_del_rcu(&orig_node->hash_entry); |
Linus Lüssing | 9d31b3c | 2014-12-13 23:32:15 +0100 | [diff] [blame] | 995 | batadv_tt_global_del_orig(orig_node->bat_priv, |
| 996 | orig_node, -1, |
| 997 | "originator timed out"); |
Sven Eckelmann | 7d211ef | 2012-05-12 02:09:34 +0200 | [diff] [blame] | 998 | batadv_orig_node_free_ref(orig_node); |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame] | 999 | continue; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1000 | } |
Martin Hundebøll | 610bfc6bc | 2013-05-23 16:53:02 +0200 | [diff] [blame] | 1001 | |
| 1002 | batadv_frag_purge_orig(orig_node, |
| 1003 | batadv_frag_check_entry); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1004 | } |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame] | 1005 | spin_unlock_bh(list_lock); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1006 | } |
| 1007 | |
Sven Eckelmann | 7cf06bc | 2012-05-12 02:09:29 +0200 | [diff] [blame] | 1008 | batadv_gw_node_purge(bat_priv); |
| 1009 | batadv_gw_election(bat_priv); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1010 | } |
| 1011 | |
Sven Eckelmann | 03fc7f8 | 2012-05-12 18:34:00 +0200 | [diff] [blame] | 1012 | static void batadv_purge_orig(struct work_struct *work) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1013 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 1014 | struct delayed_work *delayed_work; |
| 1015 | struct batadv_priv *bat_priv; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1016 | |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 1017 | delayed_work = container_of(work, struct delayed_work, work); |
| 1018 | bat_priv = container_of(delayed_work, struct batadv_priv, orig_work); |
Sven Eckelmann | 03fc7f8 | 2012-05-12 18:34:00 +0200 | [diff] [blame] | 1019 | _batadv_purge_orig(bat_priv); |
Antonio Quartulli | 7241444 | 2012-12-25 13:14:37 +0100 | [diff] [blame] | 1020 | queue_delayed_work(batadv_event_workqueue, |
| 1021 | &bat_priv->orig_work, |
| 1022 | msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD)); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1023 | } |
| 1024 | |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 1025 | void batadv_purge_orig_ref(struct batadv_priv *bat_priv) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1026 | { |
Sven Eckelmann | 03fc7f8 | 2012-05-12 18:34:00 +0200 | [diff] [blame] | 1027 | _batadv_purge_orig(bat_priv); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1028 | } |
| 1029 | |
Sven Eckelmann | 7d211ef | 2012-05-12 02:09:34 +0200 | [diff] [blame] | 1030 | int batadv_orig_seq_print_text(struct seq_file *seq, void *offset) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1031 | { |
| 1032 | struct net_device *net_dev = (struct net_device *)seq->private; |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 1033 | struct batadv_priv *bat_priv = netdev_priv(net_dev); |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 1034 | struct batadv_hard_iface *primary_if; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1035 | |
Marek Lindner | 30da63a | 2012-08-03 17:15:46 +0200 | [diff] [blame] | 1036 | primary_if = batadv_seq_print_text_primary_if_get(seq); |
| 1037 | if (!primary_if) |
Antonio Quartulli | 737a2a22 | 2013-09-02 12:15:03 +0200 | [diff] [blame] | 1038 | return 0; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1039 | |
Antonio Quartulli | 737a2a22 | 2013-09-02 12:15:03 +0200 | [diff] [blame] | 1040 | 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] | 1041 | BATADV_SOURCE_VERSION, primary_if->net_dev->name, |
Antonio Quartulli | 737a2a22 | 2013-09-02 12:15:03 +0200 | [diff] [blame] | 1042 | primary_if->net_dev->dev_addr, net_dev->name, |
| 1043 | bat_priv->bat_algo_ops->name); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1044 | |
Antonio Quartulli | 737a2a22 | 2013-09-02 12:15:03 +0200 | [diff] [blame] | 1045 | batadv_hardif_free_ref(primary_if); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1046 | |
Antonio Quartulli | 737a2a22 | 2013-09-02 12:15:03 +0200 | [diff] [blame] | 1047 | if (!bat_priv->bat_algo_ops->bat_orig_print) { |
| 1048 | seq_puts(seq, |
| 1049 | "No printing function for this routing protocol\n"); |
| 1050 | return 0; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1051 | } |
| 1052 | |
Simon Wunderlich | cb1c92e | 2013-11-21 11:52:16 +0100 | [diff] [blame] | 1053 | bat_priv->bat_algo_ops->bat_orig_print(bat_priv, seq, |
| 1054 | BATADV_IF_DEFAULT); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1055 | |
Marek Lindner | 30da63a | 2012-08-03 17:15:46 +0200 | [diff] [blame] | 1056 | return 0; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1057 | } |
| 1058 | |
Simon Wunderlich | cb1c92e | 2013-11-21 11:52:16 +0100 | [diff] [blame] | 1059 | /** |
| 1060 | * batadv_orig_hardif_seq_print_text - writes originator infos for a specific |
| 1061 | * outgoing interface |
| 1062 | * @seq: debugfs table seq_file struct |
| 1063 | * @offset: not used |
| 1064 | * |
| 1065 | * Returns 0 |
| 1066 | */ |
| 1067 | int batadv_orig_hardif_seq_print_text(struct seq_file *seq, void *offset) |
| 1068 | { |
| 1069 | struct net_device *net_dev = (struct net_device *)seq->private; |
| 1070 | struct batadv_hard_iface *hard_iface; |
| 1071 | struct batadv_priv *bat_priv; |
| 1072 | |
| 1073 | hard_iface = batadv_hardif_get_by_netdev(net_dev); |
| 1074 | |
| 1075 | if (!hard_iface || !hard_iface->soft_iface) { |
| 1076 | seq_puts(seq, "Interface not known to B.A.T.M.A.N.\n"); |
| 1077 | goto out; |
| 1078 | } |
| 1079 | |
| 1080 | bat_priv = netdev_priv(hard_iface->soft_iface); |
| 1081 | if (!bat_priv->bat_algo_ops->bat_orig_print) { |
| 1082 | seq_puts(seq, |
| 1083 | "No printing function for this routing protocol\n"); |
| 1084 | goto out; |
| 1085 | } |
| 1086 | |
| 1087 | if (hard_iface->if_status != BATADV_IF_ACTIVE) { |
| 1088 | seq_puts(seq, "Interface not active\n"); |
| 1089 | goto out; |
| 1090 | } |
| 1091 | |
| 1092 | seq_printf(seq, "[B.A.T.M.A.N. adv %s, IF/MAC: %s/%pM (%s %s)]\n", |
| 1093 | BATADV_SOURCE_VERSION, hard_iface->net_dev->name, |
| 1094 | hard_iface->net_dev->dev_addr, |
| 1095 | hard_iface->soft_iface->name, bat_priv->bat_algo_ops->name); |
| 1096 | |
| 1097 | bat_priv->bat_algo_ops->bat_orig_print(bat_priv, seq, hard_iface); |
| 1098 | |
| 1099 | out: |
Marek Lindner | 16a4142 | 2014-04-24 03:44:25 +0800 | [diff] [blame] | 1100 | if (hard_iface) |
| 1101 | batadv_hardif_free_ref(hard_iface); |
Simon Wunderlich | cb1c92e | 2013-11-21 11:52:16 +0100 | [diff] [blame] | 1102 | return 0; |
| 1103 | } |
| 1104 | |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 1105 | int batadv_orig_hash_add_if(struct batadv_hard_iface *hard_iface, |
| 1106 | int max_if_num) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1107 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 1108 | struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface); |
Antonio Quartulli | d0015fd | 2013-09-03 11:10:23 +0200 | [diff] [blame] | 1109 | struct batadv_algo_ops *bao = bat_priv->bat_algo_ops; |
Sven Eckelmann | 5bf74e9 | 2012-06-05 22:31:28 +0200 | [diff] [blame] | 1110 | struct batadv_hashtable *hash = bat_priv->orig_hash; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1111 | struct hlist_head *head; |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 1112 | struct batadv_orig_node *orig_node; |
Antonio Quartulli | c90681b | 2011-10-05 17:05:25 +0200 | [diff] [blame] | 1113 | uint32_t i; |
| 1114 | int ret; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1115 | |
| 1116 | /* resize all orig nodes because orig_node->bcast_own(_sum) depend on |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 1117 | * if_num |
| 1118 | */ |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1119 | for (i = 0; i < hash->size; i++) { |
| 1120 | head = &hash->table[i]; |
| 1121 | |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame] | 1122 | rcu_read_lock(); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1123 | hlist_for_each_entry_rcu(orig_node, head, hash_entry) { |
Antonio Quartulli | d0015fd | 2013-09-03 11:10:23 +0200 | [diff] [blame] | 1124 | ret = 0; |
| 1125 | if (bao->bat_orig_add_if) |
| 1126 | ret = bao->bat_orig_add_if(orig_node, |
| 1127 | max_if_num); |
Sven Eckelmann | 5346c35 | 2012-05-05 13:27:28 +0200 | [diff] [blame] | 1128 | if (ret == -ENOMEM) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1129 | goto err; |
| 1130 | } |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame] | 1131 | rcu_read_unlock(); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1132 | } |
| 1133 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1134 | return 0; |
| 1135 | |
| 1136 | err: |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame] | 1137 | rcu_read_unlock(); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1138 | return -ENOMEM; |
| 1139 | } |
| 1140 | |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 1141 | int batadv_orig_hash_del_if(struct batadv_hard_iface *hard_iface, |
| 1142 | int max_if_num) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1143 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 1144 | struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface); |
Sven Eckelmann | 5bf74e9 | 2012-06-05 22:31:28 +0200 | [diff] [blame] | 1145 | struct batadv_hashtable *hash = bat_priv->orig_hash; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1146 | struct hlist_head *head; |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 1147 | struct batadv_hard_iface *hard_iface_tmp; |
| 1148 | struct batadv_orig_node *orig_node; |
Antonio Quartulli | d0015fd | 2013-09-03 11:10:23 +0200 | [diff] [blame] | 1149 | struct batadv_algo_ops *bao = bat_priv->bat_algo_ops; |
Antonio Quartulli | c90681b | 2011-10-05 17:05:25 +0200 | [diff] [blame] | 1150 | uint32_t i; |
| 1151 | int ret; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1152 | |
| 1153 | /* resize all orig nodes because orig_node->bcast_own(_sum) depend on |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 1154 | * if_num |
| 1155 | */ |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1156 | for (i = 0; i < hash->size; i++) { |
| 1157 | head = &hash->table[i]; |
| 1158 | |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame] | 1159 | rcu_read_lock(); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1160 | hlist_for_each_entry_rcu(orig_node, head, hash_entry) { |
Antonio Quartulli | d0015fd | 2013-09-03 11:10:23 +0200 | [diff] [blame] | 1161 | ret = 0; |
| 1162 | if (bao->bat_orig_del_if) |
| 1163 | ret = bao->bat_orig_del_if(orig_node, |
| 1164 | max_if_num, |
| 1165 | hard_iface->if_num); |
Sven Eckelmann | 5346c35 | 2012-05-05 13:27:28 +0200 | [diff] [blame] | 1166 | if (ret == -ENOMEM) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1167 | goto err; |
| 1168 | } |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame] | 1169 | rcu_read_unlock(); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1170 | } |
| 1171 | |
| 1172 | /* renumber remaining batman interfaces _inside_ of orig_hash_lock */ |
| 1173 | rcu_read_lock(); |
Sven Eckelmann | 3193e8f | 2012-05-12 02:09:42 +0200 | [diff] [blame] | 1174 | list_for_each_entry_rcu(hard_iface_tmp, &batadv_hardif_list, list) { |
Sven Eckelmann | e9a4f29 | 2012-06-03 22:19:19 +0200 | [diff] [blame] | 1175 | if (hard_iface_tmp->if_status == BATADV_IF_NOT_IN_USE) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1176 | continue; |
| 1177 | |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 1178 | if (hard_iface == hard_iface_tmp) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1179 | continue; |
| 1180 | |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 1181 | if (hard_iface->soft_iface != hard_iface_tmp->soft_iface) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1182 | continue; |
| 1183 | |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 1184 | if (hard_iface_tmp->if_num > hard_iface->if_num) |
| 1185 | hard_iface_tmp->if_num--; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1186 | } |
| 1187 | rcu_read_unlock(); |
| 1188 | |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 1189 | hard_iface->if_num = -1; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1190 | return 0; |
| 1191 | |
| 1192 | err: |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame] | 1193 | rcu_read_unlock(); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1194 | return -ENOMEM; |
| 1195 | } |