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