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