Sven Eckelmann | 7db7d9f | 2017-11-19 15:05:11 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Sven Eckelmann | 6b1aea8 | 2018-01-01 00:00:00 +0100 | [diff] [blame] | 2 | /* Copyright (C) 2009-2018 B.A.T.M.A.N. contributors: |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 3 | * |
| 4 | * Marek Lindner, Simon Wunderlich |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of version 2 of the GNU General Public |
| 8 | * License as published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
Antonio Quartulli | ebf38fb | 2013-11-03 20:40:48 +0100 | [diff] [blame] | 16 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 17 | */ |
| 18 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 19 | #include "originator.h" |
Sven Eckelmann | 1e2c2a4 | 2015-04-17 19:40:28 +0200 | [diff] [blame] | 20 | #include "main.h" |
| 21 | |
Sven Eckelmann | 7c12439 | 2016-01-16 10:29:56 +0100 | [diff] [blame] | 22 | #include <linux/atomic.h> |
Sven Eckelmann | 1e2c2a4 | 2015-04-17 19:40:28 +0200 | [diff] [blame] | 23 | #include <linux/errno.h> |
| 24 | #include <linux/etherdevice.h> |
Sven Eckelmann | b92b94a | 2017-11-19 17:12:02 +0100 | [diff] [blame] | 25 | #include <linux/gfp.h> |
Sven Eckelmann | 1e2c2a4 | 2015-04-17 19:40:28 +0200 | [diff] [blame] | 26 | #include <linux/jiffies.h> |
| 27 | #include <linux/kernel.h> |
Sven Eckelmann | 90f564d | 2016-01-16 10:29:40 +0100 | [diff] [blame] | 28 | #include <linux/kref.h> |
Sven Eckelmann | 1e2c2a4 | 2015-04-17 19:40:28 +0200 | [diff] [blame] | 29 | #include <linux/list.h> |
| 30 | #include <linux/lockdep.h> |
| 31 | #include <linux/netdevice.h> |
Matthias Schiffer | 85cf8c8 | 2016-07-03 13:31:39 +0200 | [diff] [blame] | 32 | #include <linux/netlink.h> |
Marek Lindner | d0fa4f3 | 2015-06-22 00:30:22 +0800 | [diff] [blame] | 33 | #include <linux/rculist.h> |
Denys Vlasenko | 3326afe | 2017-11-19 17:59:13 +0100 | [diff] [blame] | 34 | #include <linux/rcupdate.h> |
Sven Eckelmann | 1e2c2a4 | 2015-04-17 19:40:28 +0200 | [diff] [blame] | 35 | #include <linux/seq_file.h> |
Matthias Schiffer | 85cf8c8 | 2016-07-03 13:31:39 +0200 | [diff] [blame] | 36 | #include <linux/skbuff.h> |
Sven Eckelmann | 1e2c2a4 | 2015-04-17 19:40:28 +0200 | [diff] [blame] | 37 | #include <linux/slab.h> |
| 38 | #include <linux/spinlock.h> |
Denys Vlasenko | 3326afe | 2017-11-19 17:59:13 +0100 | [diff] [blame] | 39 | #include <linux/stddef.h> |
Sven Eckelmann | 1e2c2a4 | 2015-04-17 19:40:28 +0200 | [diff] [blame] | 40 | #include <linux/workqueue.h> |
Matthias Schiffer | 85cf8c8 | 2016-07-03 13:31:39 +0200 | [diff] [blame] | 41 | #include <net/sock.h> |
| 42 | #include <uapi/linux/batman_adv.h> |
Sven Eckelmann | 1e2c2a4 | 2015-04-17 19:40:28 +0200 | [diff] [blame] | 43 | |
Sven Eckelmann | 01d350d | 2016-05-15 11:07:44 +0200 | [diff] [blame] | 44 | #include "bat_algo.h" |
Sven Eckelmann | 1e2c2a4 | 2015-04-17 19:40:28 +0200 | [diff] [blame] | 45 | #include "distributed-arp-table.h" |
| 46 | #include "fragmentation.h" |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 47 | #include "gateway_client.h" |
| 48 | #include "hard-interface.h" |
Sven Eckelmann | 1e2c2a4 | 2015-04-17 19:40:28 +0200 | [diff] [blame] | 49 | #include "hash.h" |
Sven Eckelmann | ba41208 | 2016-05-15 23:48:31 +0200 | [diff] [blame] | 50 | #include "log.h" |
Linus Lüssing | 60432d7 | 2014-02-15 17:47:51 +0100 | [diff] [blame] | 51 | #include "multicast.h" |
Matthias Schiffer | 85cf8c8 | 2016-07-03 13:31:39 +0200 | [diff] [blame] | 52 | #include "netlink.h" |
Sven Eckelmann | 1e2c2a4 | 2015-04-17 19:40:28 +0200 | [diff] [blame] | 53 | #include "network-coding.h" |
| 54 | #include "routing.h" |
Matthias Schiffer | 85cf8c8 | 2016-07-03 13:31:39 +0200 | [diff] [blame] | 55 | #include "soft-interface.h" |
Sven Eckelmann | 1e2c2a4 | 2015-04-17 19:40:28 +0200 | [diff] [blame] | 56 | #include "translation-table.h" |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 57 | |
Antonio Quartulli | dec0507 | 2012-11-10 11:00:32 +0100 | [diff] [blame] | 58 | /* hash class keys */ |
| 59 | static struct lock_class_key batadv_orig_hash_lock_class_key; |
| 60 | |
Sven Eckelmann | ff15c27 | 2017-12-02 19:51:53 +0100 | [diff] [blame] | 61 | /** |
| 62 | * batadv_orig_hash_find() - Find and return originator from orig_hash |
| 63 | * @bat_priv: the bat priv with all the soft interface information |
| 64 | * @data: mac address of the originator |
| 65 | * |
| 66 | * Return: orig_node (with increased refcnt), NULL on errors |
| 67 | */ |
Denys Vlasenko | 3326afe | 2017-11-19 17:59:13 +0100 | [diff] [blame] | 68 | struct batadv_orig_node * |
| 69 | batadv_orig_hash_find(struct batadv_priv *bat_priv, const void *data) |
| 70 | { |
| 71 | struct batadv_hashtable *hash = bat_priv->orig_hash; |
| 72 | struct hlist_head *head; |
| 73 | struct batadv_orig_node *orig_node, *orig_node_tmp = NULL; |
| 74 | int index; |
| 75 | |
| 76 | if (!hash) |
| 77 | return NULL; |
| 78 | |
| 79 | index = batadv_choose_orig(data, hash->size); |
| 80 | head = &hash->table[index]; |
| 81 | |
| 82 | rcu_read_lock(); |
| 83 | hlist_for_each_entry_rcu(orig_node, head, hash_entry) { |
| 84 | if (!batadv_compare_eth(orig_node, data)) |
| 85 | continue; |
| 86 | |
| 87 | if (!kref_get_unless_zero(&orig_node->refcount)) |
| 88 | continue; |
| 89 | |
| 90 | orig_node_tmp = orig_node; |
| 91 | break; |
| 92 | } |
| 93 | rcu_read_unlock(); |
| 94 | |
| 95 | return orig_node_tmp; |
| 96 | } |
| 97 | |
Sven Eckelmann | 03fc7f8 | 2012-05-12 18:34:00 +0200 | [diff] [blame] | 98 | static void batadv_purge_orig(struct work_struct *work); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 99 | |
Sven Eckelmann | 62fe710 | 2015-09-15 19:00:48 +0200 | [diff] [blame] | 100 | /** |
Sven Eckelmann | 7e9a8c2 | 2017-12-02 19:51:47 +0100 | [diff] [blame] | 101 | * batadv_compare_orig() - comparing function used in the originator hash table |
Sven Eckelmann | 7afcbbe | 2015-10-31 12:29:29 +0100 | [diff] [blame] | 102 | * @node: node in the local table |
| 103 | * @data2: second object to compare the node to |
Sven Eckelmann | 62fe710 | 2015-09-15 19:00:48 +0200 | [diff] [blame] | 104 | * |
Sven Eckelmann | 4b426b1 | 2016-02-22 21:02:39 +0100 | [diff] [blame] | 105 | * Return: true if they are the same originator |
Sven Eckelmann | 62fe710 | 2015-09-15 19:00:48 +0200 | [diff] [blame] | 106 | */ |
Sven Eckelmann | 4b426b1 | 2016-02-22 21:02:39 +0100 | [diff] [blame] | 107 | bool batadv_compare_orig(const struct hlist_node *node, const void *data2) |
Sven Eckelmann | b8e2dd1 | 2011-06-15 15:08:59 +0200 | [diff] [blame] | 108 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 109 | const void *data1 = container_of(node, struct batadv_orig_node, |
| 110 | hash_entry); |
Sven Eckelmann | b8e2dd1 | 2011-06-15 15:08:59 +0200 | [diff] [blame] | 111 | |
dingtianhong | 323813e | 2013-12-26 19:40:39 +0800 | [diff] [blame] | 112 | return batadv_compare_eth(data1, data2); |
Sven Eckelmann | b8e2dd1 | 2011-06-15 15:08:59 +0200 | [diff] [blame] | 113 | } |
| 114 | |
Antonio Quartulli | 7ea7b4a | 2013-07-30 22:16:25 +0200 | [diff] [blame] | 115 | /** |
Sven Eckelmann | 7e9a8c2 | 2017-12-02 19:51:47 +0100 | [diff] [blame] | 116 | * batadv_orig_node_vlan_get() - get an orig_node_vlan object |
Antonio Quartulli | 7ea7b4a | 2013-07-30 22:16:25 +0200 | [diff] [blame] | 117 | * @orig_node: the originator serving the VLAN |
| 118 | * @vid: the VLAN identifier |
| 119 | * |
Sven Eckelmann | 62fe710 | 2015-09-15 19:00:48 +0200 | [diff] [blame] | 120 | * 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] | 121 | * if it does not exist. |
| 122 | */ |
| 123 | struct batadv_orig_node_vlan * |
| 124 | batadv_orig_node_vlan_get(struct batadv_orig_node *orig_node, |
| 125 | unsigned short vid) |
| 126 | { |
| 127 | struct batadv_orig_node_vlan *vlan = NULL, *tmp; |
| 128 | |
| 129 | rcu_read_lock(); |
Marek Lindner | d0fa4f3 | 2015-06-22 00:30:22 +0800 | [diff] [blame] | 130 | hlist_for_each_entry_rcu(tmp, &orig_node->vlan_list, list) { |
Antonio Quartulli | 7ea7b4a | 2013-07-30 22:16:25 +0200 | [diff] [blame] | 131 | if (tmp->vid != vid) |
| 132 | continue; |
| 133 | |
Sven Eckelmann | 161a3be | 2016-01-16 10:29:55 +0100 | [diff] [blame] | 134 | if (!kref_get_unless_zero(&tmp->refcount)) |
Antonio Quartulli | 7ea7b4a | 2013-07-30 22:16:25 +0200 | [diff] [blame] | 135 | continue; |
| 136 | |
| 137 | vlan = tmp; |
| 138 | |
| 139 | break; |
| 140 | } |
| 141 | rcu_read_unlock(); |
| 142 | |
| 143 | return vlan; |
| 144 | } |
| 145 | |
| 146 | /** |
Sven Eckelmann | 7e9a8c2 | 2017-12-02 19:51:47 +0100 | [diff] [blame] | 147 | * batadv_orig_node_vlan_new() - search and possibly create an orig_node_vlan |
Antonio Quartulli | 7ea7b4a | 2013-07-30 22:16:25 +0200 | [diff] [blame] | 148 | * object |
| 149 | * @orig_node: the originator serving the VLAN |
| 150 | * @vid: the VLAN identifier |
| 151 | * |
Sven Eckelmann | 62fe710 | 2015-09-15 19:00:48 +0200 | [diff] [blame] | 152 | * 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] | 153 | * belonging to orig_node otherwise. The object is created and added to the list |
| 154 | * if it does not exist. |
| 155 | * |
| 156 | * The object is returned with refcounter increased by 1. |
| 157 | */ |
| 158 | struct batadv_orig_node_vlan * |
| 159 | batadv_orig_node_vlan_new(struct batadv_orig_node *orig_node, |
| 160 | unsigned short vid) |
| 161 | { |
| 162 | struct batadv_orig_node_vlan *vlan; |
| 163 | |
| 164 | spin_lock_bh(&orig_node->vlan_list_lock); |
| 165 | |
| 166 | /* first look if an object for this vid already exists */ |
| 167 | vlan = batadv_orig_node_vlan_get(orig_node, vid); |
| 168 | if (vlan) |
| 169 | goto out; |
| 170 | |
| 171 | vlan = kzalloc(sizeof(*vlan), GFP_ATOMIC); |
| 172 | if (!vlan) |
| 173 | goto out; |
| 174 | |
Sven Eckelmann | 161a3be | 2016-01-16 10:29:55 +0100 | [diff] [blame] | 175 | kref_init(&vlan->refcount); |
Antonio Quartulli | 7ea7b4a | 2013-07-30 22:16:25 +0200 | [diff] [blame] | 176 | vlan->vid = vid; |
| 177 | |
Sven Eckelmann | 09537d1 | 2016-07-15 17:39:16 +0200 | [diff] [blame] | 178 | kref_get(&vlan->refcount); |
Marek Lindner | d0fa4f3 | 2015-06-22 00:30:22 +0800 | [diff] [blame] | 179 | hlist_add_head_rcu(&vlan->list, &orig_node->vlan_list); |
Antonio Quartulli | 7ea7b4a | 2013-07-30 22:16:25 +0200 | [diff] [blame] | 180 | |
| 181 | out: |
| 182 | spin_unlock_bh(&orig_node->vlan_list_lock); |
| 183 | |
| 184 | return vlan; |
| 185 | } |
| 186 | |
| 187 | /** |
Sven Eckelmann | 7e9a8c2 | 2017-12-02 19:51:47 +0100 | [diff] [blame] | 188 | * batadv_orig_node_vlan_release() - release originator-vlan object from lists |
Sven Eckelmann | 161a3be | 2016-01-16 10:29:55 +0100 | [diff] [blame] | 189 | * and queue for free after rcu grace period |
| 190 | * @ref: kref pointer of the originator-vlan object |
| 191 | */ |
| 192 | static void batadv_orig_node_vlan_release(struct kref *ref) |
| 193 | { |
| 194 | struct batadv_orig_node_vlan *orig_vlan; |
| 195 | |
| 196 | orig_vlan = container_of(ref, struct batadv_orig_node_vlan, refcount); |
| 197 | |
| 198 | kfree_rcu(orig_vlan, rcu); |
| 199 | } |
| 200 | |
| 201 | /** |
Sven Eckelmann | 7e9a8c2 | 2017-12-02 19:51:47 +0100 | [diff] [blame] | 202 | * batadv_orig_node_vlan_put() - decrement the refcounter and possibly release |
Sven Eckelmann | 21754e2 | 2016-01-17 11:01:24 +0100 | [diff] [blame] | 203 | * the originator-vlan object |
Antonio Quartulli | 7ea7b4a | 2013-07-30 22:16:25 +0200 | [diff] [blame] | 204 | * @orig_vlan: the originator-vlan object to release |
| 205 | */ |
Sven Eckelmann | 21754e2 | 2016-01-17 11:01:24 +0100 | [diff] [blame] | 206 | void batadv_orig_node_vlan_put(struct batadv_orig_node_vlan *orig_vlan) |
Antonio Quartulli | 7ea7b4a | 2013-07-30 22:16:25 +0200 | [diff] [blame] | 207 | { |
Sven Eckelmann | 161a3be | 2016-01-16 10:29:55 +0100 | [diff] [blame] | 208 | kref_put(&orig_vlan->refcount, batadv_orig_node_vlan_release); |
Antonio Quartulli | 7ea7b4a | 2013-07-30 22:16:25 +0200 | [diff] [blame] | 209 | } |
| 210 | |
Sven Eckelmann | ff15c27 | 2017-12-02 19:51:53 +0100 | [diff] [blame] | 211 | /** |
| 212 | * batadv_originator_init() - Initialize all originator structures |
| 213 | * @bat_priv: the bat priv with all the soft interface information |
| 214 | * |
| 215 | * Return: 0 on success or negative error number in case of failure |
| 216 | */ |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 217 | int batadv_originator_init(struct batadv_priv *bat_priv) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 218 | { |
| 219 | if (bat_priv->orig_hash) |
Sven Eckelmann | 5346c35 | 2012-05-05 13:27:28 +0200 | [diff] [blame] | 220 | return 0; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 221 | |
Sven Eckelmann | 1a8eaf0 | 2012-05-12 02:09:32 +0200 | [diff] [blame] | 222 | bat_priv->orig_hash = batadv_hash_new(1024); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 223 | |
| 224 | if (!bat_priv->orig_hash) |
| 225 | goto err; |
| 226 | |
Antonio Quartulli | dec0507 | 2012-11-10 11:00:32 +0100 | [diff] [blame] | 227 | batadv_hash_set_lock_class(bat_priv->orig_hash, |
| 228 | &batadv_orig_hash_lock_class_key); |
| 229 | |
Antonio Quartulli | 7241444 | 2012-12-25 13:14:37 +0100 | [diff] [blame] | 230 | INIT_DELAYED_WORK(&bat_priv->orig_work, batadv_purge_orig); |
| 231 | queue_delayed_work(batadv_event_workqueue, |
| 232 | &bat_priv->orig_work, |
| 233 | msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD)); |
| 234 | |
Sven Eckelmann | 5346c35 | 2012-05-05 13:27:28 +0200 | [diff] [blame] | 235 | return 0; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 236 | |
| 237 | err: |
Sven Eckelmann | 5346c35 | 2012-05-05 13:27:28 +0200 | [diff] [blame] | 238 | return -ENOMEM; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 239 | } |
| 240 | |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 241 | /** |
Sven Eckelmann | 7e9a8c2 | 2017-12-02 19:51:47 +0100 | [diff] [blame] | 242 | * batadv_neigh_ifinfo_release() - release neigh_ifinfo from lists and queue for |
Sven Eckelmann | ae3e1e3 | 2016-01-05 12:06:24 +0100 | [diff] [blame] | 243 | * free after rcu grace period |
Sven Eckelmann | 962c683 | 2016-01-16 10:29:51 +0100 | [diff] [blame] | 244 | * @ref: kref pointer of the neigh_ifinfo |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 245 | */ |
Sven Eckelmann | 962c683 | 2016-01-16 10:29:51 +0100 | [diff] [blame] | 246 | static void batadv_neigh_ifinfo_release(struct kref *ref) |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 247 | { |
Sven Eckelmann | 962c683 | 2016-01-16 10:29:51 +0100 | [diff] [blame] | 248 | struct batadv_neigh_ifinfo *neigh_ifinfo; |
| 249 | |
| 250 | neigh_ifinfo = container_of(ref, struct batadv_neigh_ifinfo, refcount); |
| 251 | |
Sven Eckelmann | ae3e1e3 | 2016-01-05 12:06:24 +0100 | [diff] [blame] | 252 | if (neigh_ifinfo->if_outgoing != BATADV_IF_DEFAULT) |
Sven Eckelmann | 82047ad | 2016-01-17 11:01:10 +0100 | [diff] [blame] | 253 | batadv_hardif_put(neigh_ifinfo->if_outgoing); |
Sven Eckelmann | ae3e1e3 | 2016-01-05 12:06:24 +0100 | [diff] [blame] | 254 | |
| 255 | kfree_rcu(neigh_ifinfo, rcu); |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | /** |
Sven Eckelmann | 7e9a8c2 | 2017-12-02 19:51:47 +0100 | [diff] [blame] | 259 | * batadv_neigh_ifinfo_put() - decrement the refcounter and possibly release |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 260 | * the neigh_ifinfo |
| 261 | * @neigh_ifinfo: the neigh_ifinfo object to release |
| 262 | */ |
Sven Eckelmann | 044fa3a | 2016-01-17 11:01:12 +0100 | [diff] [blame] | 263 | void batadv_neigh_ifinfo_put(struct batadv_neigh_ifinfo *neigh_ifinfo) |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 264 | { |
Sven Eckelmann | 962c683 | 2016-01-16 10:29:51 +0100 | [diff] [blame] | 265 | kref_put(&neigh_ifinfo->refcount, batadv_neigh_ifinfo_release); |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | /** |
Sven Eckelmann | 7e9a8c2 | 2017-12-02 19:51:47 +0100 | [diff] [blame] | 269 | * batadv_hardif_neigh_release() - release hardif neigh node from lists and |
Sven Eckelmann | f638969 | 2016-01-05 12:06:23 +0100 | [diff] [blame] | 270 | * queue for free after rcu grace period |
Sven Eckelmann | 90f564d | 2016-01-16 10:29:40 +0100 | [diff] [blame] | 271 | * @ref: kref pointer of the neigh_node |
Marek Lindner | cef6341 | 2015-08-04 21:09:55 +0800 | [diff] [blame] | 272 | */ |
Sven Eckelmann | 90f564d | 2016-01-16 10:29:40 +0100 | [diff] [blame] | 273 | static void batadv_hardif_neigh_release(struct kref *ref) |
Marek Lindner | cef6341 | 2015-08-04 21:09:55 +0800 | [diff] [blame] | 274 | { |
Sven Eckelmann | 90f564d | 2016-01-16 10:29:40 +0100 | [diff] [blame] | 275 | struct batadv_hardif_neigh_node *hardif_neigh; |
| 276 | |
| 277 | hardif_neigh = container_of(ref, struct batadv_hardif_neigh_node, |
| 278 | refcount); |
| 279 | |
Sven Eckelmann | f638969 | 2016-01-05 12:06:23 +0100 | [diff] [blame] | 280 | spin_lock_bh(&hardif_neigh->if_incoming->neigh_list_lock); |
| 281 | hlist_del_init_rcu(&hardif_neigh->list); |
| 282 | spin_unlock_bh(&hardif_neigh->if_incoming->neigh_list_lock); |
Sven Eckelmann | bab7c6c | 2016-01-05 12:06:17 +0100 | [diff] [blame] | 283 | |
Sven Eckelmann | 82047ad | 2016-01-17 11:01:10 +0100 | [diff] [blame] | 284 | batadv_hardif_put(hardif_neigh->if_incoming); |
Sven Eckelmann | f638969 | 2016-01-05 12:06:23 +0100 | [diff] [blame] | 285 | kfree_rcu(hardif_neigh, rcu); |
Marek Lindner | cef6341 | 2015-08-04 21:09:55 +0800 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | /** |
Sven Eckelmann | 7e9a8c2 | 2017-12-02 19:51:47 +0100 | [diff] [blame] | 289 | * batadv_hardif_neigh_put() - decrement the hardif neighbors refcounter |
Sven Eckelmann | f638969 | 2016-01-05 12:06:23 +0100 | [diff] [blame] | 290 | * and possibly release it |
Marek Lindner | cef6341 | 2015-08-04 21:09:55 +0800 | [diff] [blame] | 291 | * @hardif_neigh: hardif neigh neighbor to free |
| 292 | */ |
Sven Eckelmann | accadc3 | 2016-01-17 11:01:14 +0100 | [diff] [blame] | 293 | void batadv_hardif_neigh_put(struct batadv_hardif_neigh_node *hardif_neigh) |
Marek Lindner | cef6341 | 2015-08-04 21:09:55 +0800 | [diff] [blame] | 294 | { |
Sven Eckelmann | 90f564d | 2016-01-16 10:29:40 +0100 | [diff] [blame] | 295 | kref_put(&hardif_neigh->refcount, batadv_hardif_neigh_release); |
Marek Lindner | cef6341 | 2015-08-04 21:09:55 +0800 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | /** |
Sven Eckelmann | 7e9a8c2 | 2017-12-02 19:51:47 +0100 | [diff] [blame] | 299 | * batadv_neigh_node_release() - release neigh_node from lists and queue for |
Sven Eckelmann | b4d922c | 2016-01-05 12:06:25 +0100 | [diff] [blame] | 300 | * free after rcu grace period |
Sven Eckelmann | 77ae32e | 2016-01-16 10:29:53 +0100 | [diff] [blame] | 301 | * @ref: kref pointer of the neigh_node |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 302 | */ |
Sven Eckelmann | 77ae32e | 2016-01-16 10:29:53 +0100 | [diff] [blame] | 303 | static void batadv_neigh_node_release(struct kref *ref) |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 304 | { |
| 305 | struct hlist_node *node_tmp; |
Sven Eckelmann | 77ae32e | 2016-01-16 10:29:53 +0100 | [diff] [blame] | 306 | struct batadv_neigh_node *neigh_node; |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 307 | struct batadv_neigh_ifinfo *neigh_ifinfo; |
| 308 | |
Sven Eckelmann | 77ae32e | 2016-01-16 10:29:53 +0100 | [diff] [blame] | 309 | neigh_node = container_of(ref, struct batadv_neigh_node, refcount); |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 310 | |
| 311 | hlist_for_each_entry_safe(neigh_ifinfo, node_tmp, |
| 312 | &neigh_node->ifinfo_list, list) { |
Sven Eckelmann | 044fa3a | 2016-01-17 11:01:12 +0100 | [diff] [blame] | 313 | batadv_neigh_ifinfo_put(neigh_ifinfo); |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 314 | } |
Antonio Quartulli | bcef1f3 | 2015-03-01 00:50:17 +0800 | [diff] [blame] | 315 | |
Sven Eckelmann | abe59c6 | 2016-03-11 16:44:06 +0100 | [diff] [blame] | 316 | batadv_hardif_neigh_put(neigh_node->hardif_neigh); |
Marek Lindner | cef6341 | 2015-08-04 21:09:55 +0800 | [diff] [blame] | 317 | |
Sven Eckelmann | 82047ad | 2016-01-17 11:01:10 +0100 | [diff] [blame] | 318 | batadv_hardif_put(neigh_node->if_incoming); |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 319 | |
Sven Eckelmann | b4d922c | 2016-01-05 12:06:25 +0100 | [diff] [blame] | 320 | kfree_rcu(neigh_node, rcu); |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | /** |
Sven Eckelmann | 7e9a8c2 | 2017-12-02 19:51:47 +0100 | [diff] [blame] | 324 | * batadv_neigh_node_put() - decrement the neighbors refcounter and possibly |
Sven Eckelmann | 77ae32e | 2016-01-16 10:29:53 +0100 | [diff] [blame] | 325 | * release it |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 326 | * @neigh_node: neigh neighbor to free |
| 327 | */ |
Sven Eckelmann | 25bb250 | 2016-01-17 11:01:11 +0100 | [diff] [blame] | 328 | void batadv_neigh_node_put(struct batadv_neigh_node *neigh_node) |
Simon Wunderlich | a4c135c | 2011-01-19 20:01:43 +0000 | [diff] [blame] | 329 | { |
Sven Eckelmann | 77ae32e | 2016-01-16 10:29:53 +0100 | [diff] [blame] | 330 | kref_put(&neigh_node->refcount, batadv_neigh_node_release); |
Simon Wunderlich | a4c135c | 2011-01-19 20:01:43 +0000 | [diff] [blame] | 331 | } |
| 332 | |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 333 | /** |
Sven Eckelmann | 7e9a8c2 | 2017-12-02 19:51:47 +0100 | [diff] [blame] | 334 | * batadv_orig_router_get() - router to the originator depending on iface |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 335 | * @orig_node: the orig node for the router |
| 336 | * @if_outgoing: the interface where the payload packet has been received or |
| 337 | * the OGM should be sent to |
| 338 | * |
Sven Eckelmann | 62fe710 | 2015-09-15 19:00:48 +0200 | [diff] [blame] | 339 | * Return: the neighbor which should be router for this orig_node/iface. |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 340 | * |
| 341 | * The object is returned with refcounter increased by 1. |
| 342 | */ |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 343 | struct batadv_neigh_node * |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 344 | batadv_orig_router_get(struct batadv_orig_node *orig_node, |
| 345 | const struct batadv_hard_iface *if_outgoing) |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 346 | { |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 347 | struct batadv_orig_ifinfo *orig_ifinfo; |
| 348 | struct batadv_neigh_node *router = NULL; |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 349 | |
| 350 | rcu_read_lock(); |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 351 | hlist_for_each_entry_rcu(orig_ifinfo, &orig_node->ifinfo_list, list) { |
| 352 | if (orig_ifinfo->if_outgoing != if_outgoing) |
| 353 | continue; |
| 354 | |
| 355 | router = rcu_dereference(orig_ifinfo->router); |
| 356 | break; |
| 357 | } |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 358 | |
Sven Eckelmann | 77ae32e | 2016-01-16 10:29:53 +0100 | [diff] [blame] | 359 | if (router && !kref_get_unless_zero(&router->refcount)) |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 360 | router = NULL; |
| 361 | |
| 362 | rcu_read_unlock(); |
| 363 | return router; |
| 364 | } |
| 365 | |
Antonio Quartulli | 0538f75 | 2013-09-02 12:15:01 +0200 | [diff] [blame] | 366 | /** |
Sven Eckelmann | 7e9a8c2 | 2017-12-02 19:51:47 +0100 | [diff] [blame] | 367 | * batadv_orig_ifinfo_get() - find the ifinfo from an orig_node |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 368 | * @orig_node: the orig node to be queried |
| 369 | * @if_outgoing: the interface for which the ifinfo should be acquired |
| 370 | * |
Sven Eckelmann | 62fe710 | 2015-09-15 19:00:48 +0200 | [diff] [blame] | 371 | * Return: the requested orig_ifinfo or NULL if not found. |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 372 | * |
| 373 | * The object is returned with refcounter increased by 1. |
| 374 | */ |
| 375 | struct batadv_orig_ifinfo * |
| 376 | batadv_orig_ifinfo_get(struct batadv_orig_node *orig_node, |
| 377 | struct batadv_hard_iface *if_outgoing) |
| 378 | { |
| 379 | struct batadv_orig_ifinfo *tmp, *orig_ifinfo = NULL; |
| 380 | |
| 381 | rcu_read_lock(); |
| 382 | hlist_for_each_entry_rcu(tmp, &orig_node->ifinfo_list, |
| 383 | list) { |
| 384 | if (tmp->if_outgoing != if_outgoing) |
| 385 | continue; |
| 386 | |
Sven Eckelmann | a6ba0d3 | 2016-01-16 10:29:52 +0100 | [diff] [blame] | 387 | if (!kref_get_unless_zero(&tmp->refcount)) |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 388 | continue; |
| 389 | |
| 390 | orig_ifinfo = tmp; |
| 391 | break; |
| 392 | } |
| 393 | rcu_read_unlock(); |
| 394 | |
| 395 | return orig_ifinfo; |
| 396 | } |
| 397 | |
| 398 | /** |
Sven Eckelmann | 7e9a8c2 | 2017-12-02 19:51:47 +0100 | [diff] [blame] | 399 | * batadv_orig_ifinfo_new() - search and possibly create an orig_ifinfo object |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 400 | * @orig_node: the orig node to be queried |
| 401 | * @if_outgoing: the interface for which the ifinfo should be acquired |
| 402 | * |
Sven Eckelmann | 62fe710 | 2015-09-15 19:00:48 +0200 | [diff] [blame] | 403 | * 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] | 404 | * interface otherwise. The object is created and added to the list |
| 405 | * if it does not exist. |
| 406 | * |
| 407 | * The object is returned with refcounter increased by 1. |
| 408 | */ |
| 409 | struct batadv_orig_ifinfo * |
| 410 | batadv_orig_ifinfo_new(struct batadv_orig_node *orig_node, |
| 411 | struct batadv_hard_iface *if_outgoing) |
| 412 | { |
Sven Eckelmann | 422d2f7 | 2016-07-25 00:42:44 +0200 | [diff] [blame] | 413 | struct batadv_orig_ifinfo *orig_ifinfo; |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 414 | unsigned long reset_time; |
| 415 | |
| 416 | spin_lock_bh(&orig_node->neigh_list_lock); |
| 417 | |
| 418 | orig_ifinfo = batadv_orig_ifinfo_get(orig_node, if_outgoing); |
| 419 | if (orig_ifinfo) |
| 420 | goto out; |
| 421 | |
| 422 | orig_ifinfo = kzalloc(sizeof(*orig_ifinfo), GFP_ATOMIC); |
| 423 | if (!orig_ifinfo) |
| 424 | goto out; |
| 425 | |
Sven Eckelmann | 17a8691 | 2016-04-11 13:06:40 +0200 | [diff] [blame] | 426 | if (if_outgoing != BATADV_IF_DEFAULT) |
| 427 | kref_get(&if_outgoing->refcount); |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 428 | |
| 429 | reset_time = jiffies - 1; |
| 430 | reset_time -= msecs_to_jiffies(BATADV_RESET_PROTECTION_MS); |
| 431 | orig_ifinfo->batman_seqno_reset = reset_time; |
| 432 | orig_ifinfo->if_outgoing = if_outgoing; |
| 433 | INIT_HLIST_NODE(&orig_ifinfo->list); |
Sven Eckelmann | a6ba0d3 | 2016-01-16 10:29:52 +0100 | [diff] [blame] | 434 | kref_init(&orig_ifinfo->refcount); |
Sven Eckelmann | f257b99 | 2016-07-15 17:39:17 +0200 | [diff] [blame] | 435 | |
Sven Eckelmann | a6ba0d3 | 2016-01-16 10:29:52 +0100 | [diff] [blame] | 436 | kref_get(&orig_ifinfo->refcount); |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 437 | hlist_add_head_rcu(&orig_ifinfo->list, |
| 438 | &orig_node->ifinfo_list); |
| 439 | out: |
| 440 | spin_unlock_bh(&orig_node->neigh_list_lock); |
| 441 | return orig_ifinfo; |
| 442 | } |
| 443 | |
| 444 | /** |
Sven Eckelmann | 7e9a8c2 | 2017-12-02 19:51:47 +0100 | [diff] [blame] | 445 | * batadv_neigh_ifinfo_get() - find the ifinfo from an neigh_node |
Sven Eckelmann | e51f039 | 2015-09-06 21:38:51 +0200 | [diff] [blame] | 446 | * @neigh: the neigh node to be queried |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 447 | * @if_outgoing: the interface for which the ifinfo should be acquired |
| 448 | * |
| 449 | * The object is returned with refcounter increased by 1. |
| 450 | * |
Sven Eckelmann | 62fe710 | 2015-09-15 19:00:48 +0200 | [diff] [blame] | 451 | * Return: the requested neigh_ifinfo or NULL if not found |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 452 | */ |
| 453 | struct batadv_neigh_ifinfo * |
| 454 | batadv_neigh_ifinfo_get(struct batadv_neigh_node *neigh, |
| 455 | struct batadv_hard_iface *if_outgoing) |
| 456 | { |
| 457 | struct batadv_neigh_ifinfo *neigh_ifinfo = NULL, |
| 458 | *tmp_neigh_ifinfo; |
| 459 | |
| 460 | rcu_read_lock(); |
| 461 | hlist_for_each_entry_rcu(tmp_neigh_ifinfo, &neigh->ifinfo_list, |
| 462 | list) { |
| 463 | if (tmp_neigh_ifinfo->if_outgoing != if_outgoing) |
| 464 | continue; |
| 465 | |
Sven Eckelmann | 962c683 | 2016-01-16 10:29:51 +0100 | [diff] [blame] | 466 | if (!kref_get_unless_zero(&tmp_neigh_ifinfo->refcount)) |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 467 | continue; |
| 468 | |
| 469 | neigh_ifinfo = tmp_neigh_ifinfo; |
| 470 | break; |
| 471 | } |
| 472 | rcu_read_unlock(); |
| 473 | |
| 474 | return neigh_ifinfo; |
| 475 | } |
| 476 | |
| 477 | /** |
Sven Eckelmann | 7e9a8c2 | 2017-12-02 19:51:47 +0100 | [diff] [blame] | 478 | * batadv_neigh_ifinfo_new() - search and possibly create an neigh_ifinfo object |
Sven Eckelmann | e51f039 | 2015-09-06 21:38:51 +0200 | [diff] [blame] | 479 | * @neigh: the neigh node to be queried |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 480 | * @if_outgoing: the interface for which the ifinfo should be acquired |
| 481 | * |
Sven Eckelmann | 62fe710 | 2015-09-15 19:00:48 +0200 | [diff] [blame] | 482 | * 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] | 483 | * if_outgoing interface otherwise. The object is created and added to the list |
| 484 | * if it does not exist. |
| 485 | * |
| 486 | * The object is returned with refcounter increased by 1. |
| 487 | */ |
| 488 | struct batadv_neigh_ifinfo * |
| 489 | batadv_neigh_ifinfo_new(struct batadv_neigh_node *neigh, |
| 490 | struct batadv_hard_iface *if_outgoing) |
| 491 | { |
| 492 | struct batadv_neigh_ifinfo *neigh_ifinfo; |
| 493 | |
| 494 | spin_lock_bh(&neigh->ifinfo_lock); |
| 495 | |
| 496 | neigh_ifinfo = batadv_neigh_ifinfo_get(neigh, if_outgoing); |
| 497 | if (neigh_ifinfo) |
| 498 | goto out; |
| 499 | |
| 500 | neigh_ifinfo = kzalloc(sizeof(*neigh_ifinfo), GFP_ATOMIC); |
| 501 | if (!neigh_ifinfo) |
| 502 | goto out; |
| 503 | |
Sven Eckelmann | 17a8691 | 2016-04-11 13:06:40 +0200 | [diff] [blame] | 504 | if (if_outgoing) |
| 505 | kref_get(&if_outgoing->refcount); |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 506 | |
| 507 | INIT_HLIST_NODE(&neigh_ifinfo->list); |
Sven Eckelmann | 962c683 | 2016-01-16 10:29:51 +0100 | [diff] [blame] | 508 | kref_init(&neigh_ifinfo->refcount); |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 509 | neigh_ifinfo->if_outgoing = if_outgoing; |
| 510 | |
Sven Eckelmann | 2e774ac | 2016-07-15 17:39:19 +0200 | [diff] [blame] | 511 | kref_get(&neigh_ifinfo->refcount); |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 512 | hlist_add_head_rcu(&neigh_ifinfo->list, &neigh->ifinfo_list); |
| 513 | |
| 514 | out: |
| 515 | spin_unlock_bh(&neigh->ifinfo_lock); |
| 516 | |
| 517 | return neigh_ifinfo; |
| 518 | } |
| 519 | |
| 520 | /** |
Sven Eckelmann | 7e9a8c2 | 2017-12-02 19:51:47 +0100 | [diff] [blame] | 521 | * batadv_neigh_node_get() - retrieve a neighbour from the list |
Marek Lindner | ed29266 | 2015-08-04 23:31:44 +0800 | [diff] [blame] | 522 | * @orig_node: originator which the neighbour belongs to |
| 523 | * @hard_iface: the interface where this neighbour is connected to |
| 524 | * @addr: the address of the neighbour |
| 525 | * |
| 526 | * Looks for and possibly returns a neighbour belonging to this originator list |
| 527 | * which is connected through the provided hard interface. |
Sven Eckelmann | 62fe710 | 2015-09-15 19:00:48 +0200 | [diff] [blame] | 528 | * |
| 529 | * Return: neighbor when found. Othwerwise NULL |
Marek Lindner | ed29266 | 2015-08-04 23:31:44 +0800 | [diff] [blame] | 530 | */ |
| 531 | static struct batadv_neigh_node * |
| 532 | batadv_neigh_node_get(const struct batadv_orig_node *orig_node, |
| 533 | const struct batadv_hard_iface *hard_iface, |
| 534 | const u8 *addr) |
| 535 | { |
| 536 | struct batadv_neigh_node *tmp_neigh_node, *res = NULL; |
| 537 | |
| 538 | rcu_read_lock(); |
| 539 | hlist_for_each_entry_rcu(tmp_neigh_node, &orig_node->neigh_list, list) { |
| 540 | if (!batadv_compare_eth(tmp_neigh_node->addr, addr)) |
| 541 | continue; |
| 542 | |
| 543 | if (tmp_neigh_node->if_incoming != hard_iface) |
| 544 | continue; |
| 545 | |
Sven Eckelmann | 77ae32e | 2016-01-16 10:29:53 +0100 | [diff] [blame] | 546 | if (!kref_get_unless_zero(&tmp_neigh_node->refcount)) |
Marek Lindner | ed29266 | 2015-08-04 23:31:44 +0800 | [diff] [blame] | 547 | continue; |
| 548 | |
| 549 | res = tmp_neigh_node; |
| 550 | break; |
| 551 | } |
| 552 | rcu_read_unlock(); |
| 553 | |
| 554 | return res; |
| 555 | } |
| 556 | |
| 557 | /** |
Sven Eckelmann | 7e9a8c2 | 2017-12-02 19:51:47 +0100 | [diff] [blame] | 558 | * batadv_hardif_neigh_create() - create a hardif neighbour node |
Marek Lindner | cef6341 | 2015-08-04 21:09:55 +0800 | [diff] [blame] | 559 | * @hard_iface: the interface this neighbour is connected to |
| 560 | * @neigh_addr: the interface address of the neighbour to retrieve |
Linus Lüssing | 3111bee | 2016-08-07 12:34:19 +0200 | [diff] [blame] | 561 | * @orig_node: originator object representing the neighbour |
Marek Lindner | cef6341 | 2015-08-04 21:09:55 +0800 | [diff] [blame] | 562 | * |
Sven Eckelmann | 62fe710 | 2015-09-15 19:00:48 +0200 | [diff] [blame] | 563 | * Return: the hardif neighbour node if found or created or NULL otherwise. |
Marek Lindner | cef6341 | 2015-08-04 21:09:55 +0800 | [diff] [blame] | 564 | */ |
| 565 | static struct batadv_hardif_neigh_node * |
| 566 | batadv_hardif_neigh_create(struct batadv_hard_iface *hard_iface, |
Linus Lüssing | 3111bee | 2016-08-07 12:34:19 +0200 | [diff] [blame] | 567 | const u8 *neigh_addr, |
| 568 | struct batadv_orig_node *orig_node) |
Marek Lindner | cef6341 | 2015-08-04 21:09:55 +0800 | [diff] [blame] | 569 | { |
Marek Lindner | 8248a4c | 2015-08-04 21:09:56 +0800 | [diff] [blame] | 570 | struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface); |
Sven Eckelmann | 422d2f7 | 2016-07-25 00:42:44 +0200 | [diff] [blame] | 571 | struct batadv_hardif_neigh_node *hardif_neigh; |
Marek Lindner | cef6341 | 2015-08-04 21:09:55 +0800 | [diff] [blame] | 572 | |
| 573 | spin_lock_bh(&hard_iface->neigh_list_lock); |
| 574 | |
| 575 | /* check if neighbor hasn't been added in the meantime */ |
| 576 | hardif_neigh = batadv_hardif_neigh_get(hard_iface, neigh_addr); |
| 577 | if (hardif_neigh) |
| 578 | goto out; |
| 579 | |
Marek Lindner | cef6341 | 2015-08-04 21:09:55 +0800 | [diff] [blame] | 580 | hardif_neigh = kzalloc(sizeof(*hardif_neigh), GFP_ATOMIC); |
Sven Eckelmann | 17a8691 | 2016-04-11 13:06:40 +0200 | [diff] [blame] | 581 | if (!hardif_neigh) |
Marek Lindner | cef6341 | 2015-08-04 21:09:55 +0800 | [diff] [blame] | 582 | goto out; |
Marek Lindner | cef6341 | 2015-08-04 21:09:55 +0800 | [diff] [blame] | 583 | |
Sven Eckelmann | 17a8691 | 2016-04-11 13:06:40 +0200 | [diff] [blame] | 584 | kref_get(&hard_iface->refcount); |
Marek Lindner | cef6341 | 2015-08-04 21:09:55 +0800 | [diff] [blame] | 585 | INIT_HLIST_NODE(&hardif_neigh->list); |
| 586 | ether_addr_copy(hardif_neigh->addr, neigh_addr); |
Linus Lüssing | 3111bee | 2016-08-07 12:34:19 +0200 | [diff] [blame] | 587 | ether_addr_copy(hardif_neigh->orig, orig_node->orig); |
Marek Lindner | cef6341 | 2015-08-04 21:09:55 +0800 | [diff] [blame] | 588 | hardif_neigh->if_incoming = hard_iface; |
| 589 | hardif_neigh->last_seen = jiffies; |
| 590 | |
Sven Eckelmann | 90f564d | 2016-01-16 10:29:40 +0100 | [diff] [blame] | 591 | kref_init(&hardif_neigh->refcount); |
Marek Lindner | cef6341 | 2015-08-04 21:09:55 +0800 | [diff] [blame] | 592 | |
Antonio Quartulli | 29824a5 | 2016-05-25 23:27:31 +0800 | [diff] [blame] | 593 | if (bat_priv->algo_ops->neigh.hardif_init) |
| 594 | bat_priv->algo_ops->neigh.hardif_init(hardif_neigh); |
Marek Lindner | 8248a4c | 2015-08-04 21:09:56 +0800 | [diff] [blame] | 595 | |
Sven Eckelmann | 9ca488d | 2016-09-29 17:22:58 +0200 | [diff] [blame] | 596 | hlist_add_head_rcu(&hardif_neigh->list, &hard_iface->neigh_list); |
Marek Lindner | cef6341 | 2015-08-04 21:09:55 +0800 | [diff] [blame] | 597 | |
| 598 | out: |
| 599 | spin_unlock_bh(&hard_iface->neigh_list_lock); |
| 600 | return hardif_neigh; |
| 601 | } |
| 602 | |
| 603 | /** |
Sven Eckelmann | 7e9a8c2 | 2017-12-02 19:51:47 +0100 | [diff] [blame] | 604 | * batadv_hardif_neigh_get_or_create() - retrieve or create a hardif neighbour |
Marek Lindner | cef6341 | 2015-08-04 21:09:55 +0800 | [diff] [blame] | 605 | * node |
| 606 | * @hard_iface: the interface this neighbour is connected to |
| 607 | * @neigh_addr: the interface address of the neighbour to retrieve |
Linus Lüssing | 3111bee | 2016-08-07 12:34:19 +0200 | [diff] [blame] | 608 | * @orig_node: originator object representing the neighbour |
Marek Lindner | cef6341 | 2015-08-04 21:09:55 +0800 | [diff] [blame] | 609 | * |
Sven Eckelmann | 62fe710 | 2015-09-15 19:00:48 +0200 | [diff] [blame] | 610 | * Return: the hardif neighbour node if found or created or NULL otherwise. |
Marek Lindner | cef6341 | 2015-08-04 21:09:55 +0800 | [diff] [blame] | 611 | */ |
| 612 | static struct batadv_hardif_neigh_node * |
| 613 | batadv_hardif_neigh_get_or_create(struct batadv_hard_iface *hard_iface, |
Linus Lüssing | 3111bee | 2016-08-07 12:34:19 +0200 | [diff] [blame] | 614 | const u8 *neigh_addr, |
| 615 | struct batadv_orig_node *orig_node) |
Marek Lindner | cef6341 | 2015-08-04 21:09:55 +0800 | [diff] [blame] | 616 | { |
Sven Eckelmann | 422d2f7 | 2016-07-25 00:42:44 +0200 | [diff] [blame] | 617 | struct batadv_hardif_neigh_node *hardif_neigh; |
Marek Lindner | cef6341 | 2015-08-04 21:09:55 +0800 | [diff] [blame] | 618 | |
| 619 | /* first check without locking to avoid the overhead */ |
| 620 | hardif_neigh = batadv_hardif_neigh_get(hard_iface, neigh_addr); |
| 621 | if (hardif_neigh) |
| 622 | return hardif_neigh; |
| 623 | |
Linus Lüssing | 3111bee | 2016-08-07 12:34:19 +0200 | [diff] [blame] | 624 | return batadv_hardif_neigh_create(hard_iface, neigh_addr, orig_node); |
Marek Lindner | cef6341 | 2015-08-04 21:09:55 +0800 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | /** |
Sven Eckelmann | 7e9a8c2 | 2017-12-02 19:51:47 +0100 | [diff] [blame] | 628 | * batadv_hardif_neigh_get() - retrieve a hardif neighbour from the list |
Marek Lindner | cef6341 | 2015-08-04 21:09:55 +0800 | [diff] [blame] | 629 | * @hard_iface: the interface where this neighbour is connected to |
| 630 | * @neigh_addr: the address of the neighbour |
| 631 | * |
| 632 | * Looks for and possibly returns a neighbour belonging to this hard interface. |
Sven Eckelmann | 62fe710 | 2015-09-15 19:00:48 +0200 | [diff] [blame] | 633 | * |
| 634 | * Return: neighbor when found. Othwerwise NULL |
Marek Lindner | cef6341 | 2015-08-04 21:09:55 +0800 | [diff] [blame] | 635 | */ |
| 636 | struct batadv_hardif_neigh_node * |
| 637 | batadv_hardif_neigh_get(const struct batadv_hard_iface *hard_iface, |
| 638 | const u8 *neigh_addr) |
| 639 | { |
| 640 | struct batadv_hardif_neigh_node *tmp_hardif_neigh, *hardif_neigh = NULL; |
| 641 | |
| 642 | rcu_read_lock(); |
| 643 | hlist_for_each_entry_rcu(tmp_hardif_neigh, |
| 644 | &hard_iface->neigh_list, list) { |
| 645 | if (!batadv_compare_eth(tmp_hardif_neigh->addr, neigh_addr)) |
| 646 | continue; |
| 647 | |
Sven Eckelmann | 90f564d | 2016-01-16 10:29:40 +0100 | [diff] [blame] | 648 | if (!kref_get_unless_zero(&tmp_hardif_neigh->refcount)) |
Marek Lindner | cef6341 | 2015-08-04 21:09:55 +0800 | [diff] [blame] | 649 | continue; |
| 650 | |
| 651 | hardif_neigh = tmp_hardif_neigh; |
| 652 | break; |
| 653 | } |
| 654 | rcu_read_unlock(); |
| 655 | |
| 656 | return hardif_neigh; |
| 657 | } |
| 658 | |
| 659 | /** |
Sven Eckelmann | 7e9a8c2 | 2017-12-02 19:51:47 +0100 | [diff] [blame] | 660 | * batadv_neigh_node_create() - create a neigh node object |
Marek Lindner | 3f32f8a | 2015-07-26 04:59:15 +0800 | [diff] [blame] | 661 | * @orig_node: originator object representing the neighbour |
Antonio Quartulli | 0538f75 | 2013-09-02 12:15:01 +0200 | [diff] [blame] | 662 | * @hard_iface: the interface where the neighbour is connected to |
| 663 | * @neigh_addr: the mac address of the neighbour interface |
Antonio Quartulli | 0538f75 | 2013-09-02 12:15:01 +0200 | [diff] [blame] | 664 | * |
| 665 | * Allocates a new neigh_node object and initialises all the generic fields. |
Sven Eckelmann | 62fe710 | 2015-09-15 19:00:48 +0200 | [diff] [blame] | 666 | * |
Marek Lindner | 6f0a6b5 | 2016-05-03 01:52:08 +0800 | [diff] [blame] | 667 | * Return: the neighbour node if found or created or NULL otherwise. |
Antonio Quartulli | 0538f75 | 2013-09-02 12:15:01 +0200 | [diff] [blame] | 668 | */ |
Marek Lindner | 6f0a6b5 | 2016-05-03 01:52:08 +0800 | [diff] [blame] | 669 | static struct batadv_neigh_node * |
| 670 | batadv_neigh_node_create(struct batadv_orig_node *orig_node, |
| 671 | struct batadv_hard_iface *hard_iface, |
| 672 | const u8 *neigh_addr) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 673 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 674 | struct batadv_neigh_node *neigh_node; |
Marek Lindner | cef6341 | 2015-08-04 21:09:55 +0800 | [diff] [blame] | 675 | struct batadv_hardif_neigh_node *hardif_neigh = NULL; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 676 | |
Linus Lüssing | e123705 | 2016-01-07 08:11:12 +0100 | [diff] [blame] | 677 | spin_lock_bh(&orig_node->neigh_list_lock); |
| 678 | |
Marek Lindner | 741aa06 | 2015-07-26 04:57:43 +0800 | [diff] [blame] | 679 | neigh_node = batadv_neigh_node_get(orig_node, hard_iface, neigh_addr); |
| 680 | if (neigh_node) |
| 681 | goto out; |
| 682 | |
Marek Lindner | cef6341 | 2015-08-04 21:09:55 +0800 | [diff] [blame] | 683 | hardif_neigh = batadv_hardif_neigh_get_or_create(hard_iface, |
Linus Lüssing | 3111bee | 2016-08-07 12:34:19 +0200 | [diff] [blame] | 684 | neigh_addr, orig_node); |
Marek Lindner | cef6341 | 2015-08-04 21:09:55 +0800 | [diff] [blame] | 685 | if (!hardif_neigh) |
| 686 | goto out; |
| 687 | |
Sven Eckelmann | 704509b | 2011-05-14 23:14:54 +0200 | [diff] [blame] | 688 | neigh_node = kzalloc(sizeof(*neigh_node), GFP_ATOMIC); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 689 | if (!neigh_node) |
Marek Lindner | 7ae8b28 | 2012-03-01 15:35:21 +0800 | [diff] [blame] | 690 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 691 | |
Marek Lindner | 9591a79 | 2010-12-12 21:57:11 +0000 | [diff] [blame] | 692 | INIT_HLIST_NODE(&neigh_node->list); |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 693 | INIT_HLIST_HEAD(&neigh_node->ifinfo_list); |
| 694 | spin_lock_init(&neigh_node->ifinfo_lock); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 695 | |
Sven Eckelmann | 17a8691 | 2016-04-11 13:06:40 +0200 | [diff] [blame] | 696 | kref_get(&hard_iface->refcount); |
Antonio Quartulli | 8fdd015 | 2014-01-22 00:42:11 +0100 | [diff] [blame] | 697 | ether_addr_copy(neigh_node->addr, neigh_addr); |
Antonio Quartulli | 0538f75 | 2013-09-02 12:15:01 +0200 | [diff] [blame] | 698 | neigh_node->if_incoming = hard_iface; |
| 699 | neigh_node->orig_node = orig_node; |
Marek Lindner | e48474e | 2016-03-11 16:01:09 +0100 | [diff] [blame] | 700 | neigh_node->last_seen = jiffies; |
Antonio Quartulli | 0538f75 | 2013-09-02 12:15:01 +0200 | [diff] [blame] | 701 | |
Sven Eckelmann | abe59c6 | 2016-03-11 16:44:06 +0100 | [diff] [blame] | 702 | /* increment unique neighbor refcount */ |
| 703 | kref_get(&hardif_neigh->refcount); |
| 704 | neigh_node->hardif_neigh = hardif_neigh; |
| 705 | |
Marek Lindner | 1605d0d | 2011-02-18 12:28:11 +0000 | [diff] [blame] | 706 | /* extra reference for return */ |
Sven Eckelmann | 77ae32e | 2016-01-16 10:29:53 +0100 | [diff] [blame] | 707 | kref_init(&neigh_node->refcount); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 708 | |
Sven Eckelmann | 8427445 | 2016-07-15 17:39:20 +0200 | [diff] [blame] | 709 | kref_get(&neigh_node->refcount); |
Marek Lindner | 741aa06 | 2015-07-26 04:57:43 +0800 | [diff] [blame] | 710 | hlist_add_head_rcu(&neigh_node->list, &orig_node->neigh_list); |
Marek Lindner | 741aa06 | 2015-07-26 04:57:43 +0800 | [diff] [blame] | 711 | |
| 712 | batadv_dbg(BATADV_DBG_BATMAN, orig_node->bat_priv, |
| 713 | "Creating new neighbor %pM for orig_node %pM on interface %s\n", |
| 714 | neigh_addr, orig_node->orig, hard_iface->net_dev->name); |
| 715 | |
Marek Lindner | 7ae8b28 | 2012-03-01 15:35:21 +0800 | [diff] [blame] | 716 | out: |
Linus Lüssing | e123705 | 2016-01-07 08:11:12 +0100 | [diff] [blame] | 717 | spin_unlock_bh(&orig_node->neigh_list_lock); |
| 718 | |
Marek Lindner | cef6341 | 2015-08-04 21:09:55 +0800 | [diff] [blame] | 719 | if (hardif_neigh) |
Sven Eckelmann | accadc3 | 2016-01-17 11:01:14 +0100 | [diff] [blame] | 720 | batadv_hardif_neigh_put(hardif_neigh); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 721 | return neigh_node; |
| 722 | } |
| 723 | |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 724 | /** |
Sven Eckelmann | 7e9a8c2 | 2017-12-02 19:51:47 +0100 | [diff] [blame] | 725 | * batadv_neigh_node_get_or_create() - retrieve or create a neigh node object |
Marek Lindner | 6f0a6b5 | 2016-05-03 01:52:08 +0800 | [diff] [blame] | 726 | * @orig_node: originator object representing the neighbour |
| 727 | * @hard_iface: the interface where the neighbour is connected to |
| 728 | * @neigh_addr: the mac address of the neighbour interface |
| 729 | * |
| 730 | * Return: the neighbour node if found or created or NULL otherwise. |
| 731 | */ |
| 732 | struct batadv_neigh_node * |
| 733 | batadv_neigh_node_get_or_create(struct batadv_orig_node *orig_node, |
| 734 | struct batadv_hard_iface *hard_iface, |
| 735 | const u8 *neigh_addr) |
| 736 | { |
Sven Eckelmann | 422d2f7 | 2016-07-25 00:42:44 +0200 | [diff] [blame] | 737 | struct batadv_neigh_node *neigh_node; |
Marek Lindner | 6f0a6b5 | 2016-05-03 01:52:08 +0800 | [diff] [blame] | 738 | |
| 739 | /* first check without locking to avoid the overhead */ |
| 740 | neigh_node = batadv_neigh_node_get(orig_node, hard_iface, neigh_addr); |
| 741 | if (neigh_node) |
| 742 | return neigh_node; |
| 743 | |
| 744 | return batadv_neigh_node_create(orig_node, hard_iface, neigh_addr); |
| 745 | } |
| 746 | |
Sven Eckelmann | dc1cbd1 | 2016-07-16 09:31:20 +0200 | [diff] [blame] | 747 | #ifdef CONFIG_BATMAN_ADV_DEBUGFS |
Marek Lindner | 6f0a6b5 | 2016-05-03 01:52:08 +0800 | [diff] [blame] | 748 | /** |
Sven Eckelmann | 7e9a8c2 | 2017-12-02 19:51:47 +0100 | [diff] [blame] | 749 | * batadv_hardif_neigh_seq_print_text() - print the single hop neighbour list |
Marek Lindner | 7587405 | 2015-08-04 21:09:57 +0800 | [diff] [blame] | 750 | * @seq: neighbour table seq_file struct |
| 751 | * @offset: not used |
| 752 | * |
Sven Eckelmann | 62fe710 | 2015-09-15 19:00:48 +0200 | [diff] [blame] | 753 | * Return: always 0 |
Marek Lindner | 7587405 | 2015-08-04 21:09:57 +0800 | [diff] [blame] | 754 | */ |
| 755 | int batadv_hardif_neigh_seq_print_text(struct seq_file *seq, void *offset) |
| 756 | { |
| 757 | struct net_device *net_dev = (struct net_device *)seq->private; |
| 758 | struct batadv_priv *bat_priv = netdev_priv(net_dev); |
| 759 | struct batadv_hard_iface *primary_if; |
| 760 | |
| 761 | primary_if = batadv_seq_print_text_primary_if_get(seq); |
| 762 | if (!primary_if) |
| 763 | return 0; |
| 764 | |
| 765 | seq_printf(seq, "[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s %s)]\n", |
| 766 | BATADV_SOURCE_VERSION, primary_if->net_dev->name, |
| 767 | primary_if->net_dev->dev_addr, net_dev->name, |
Antonio Quartulli | 29824a5 | 2016-05-25 23:27:31 +0800 | [diff] [blame] | 768 | bat_priv->algo_ops->name); |
Marek Lindner | 7587405 | 2015-08-04 21:09:57 +0800 | [diff] [blame] | 769 | |
Sven Eckelmann | 82047ad | 2016-01-17 11:01:10 +0100 | [diff] [blame] | 770 | batadv_hardif_put(primary_if); |
Marek Lindner | 7587405 | 2015-08-04 21:09:57 +0800 | [diff] [blame] | 771 | |
Antonio Quartulli | 29824a5 | 2016-05-25 23:27:31 +0800 | [diff] [blame] | 772 | if (!bat_priv->algo_ops->neigh.print) { |
Marek Lindner | 7587405 | 2015-08-04 21:09:57 +0800 | [diff] [blame] | 773 | seq_puts(seq, |
| 774 | "No printing function for this routing protocol\n"); |
| 775 | return 0; |
| 776 | } |
| 777 | |
Antonio Quartulli | 29824a5 | 2016-05-25 23:27:31 +0800 | [diff] [blame] | 778 | bat_priv->algo_ops->neigh.print(bat_priv, seq); |
Marek Lindner | 7587405 | 2015-08-04 21:09:57 +0800 | [diff] [blame] | 779 | return 0; |
| 780 | } |
Sven Eckelmann | dc1cbd1 | 2016-07-16 09:31:20 +0200 | [diff] [blame] | 781 | #endif |
Marek Lindner | 7587405 | 2015-08-04 21:09:57 +0800 | [diff] [blame] | 782 | |
| 783 | /** |
Sven Eckelmann | 7e9a8c2 | 2017-12-02 19:51:47 +0100 | [diff] [blame] | 784 | * batadv_hardif_neigh_dump() - Dump to netlink the neighbor infos for a |
| 785 | * specific outgoing interface |
Matthias Schiffer | 85cf8c8 | 2016-07-03 13:31:39 +0200 | [diff] [blame] | 786 | * @msg: message to dump into |
| 787 | * @cb: parameters for the dump |
| 788 | * |
| 789 | * Return: 0 or error value |
| 790 | */ |
| 791 | int batadv_hardif_neigh_dump(struct sk_buff *msg, struct netlink_callback *cb) |
| 792 | { |
| 793 | struct net *net = sock_net(cb->skb->sk); |
| 794 | struct net_device *soft_iface; |
| 795 | struct net_device *hard_iface = NULL; |
| 796 | struct batadv_hard_iface *hardif = BATADV_IF_DEFAULT; |
| 797 | struct batadv_priv *bat_priv; |
| 798 | struct batadv_hard_iface *primary_if = NULL; |
| 799 | int ret; |
| 800 | int ifindex, hard_ifindex; |
| 801 | |
| 802 | ifindex = batadv_netlink_get_ifindex(cb->nlh, BATADV_ATTR_MESH_IFINDEX); |
| 803 | if (!ifindex) |
| 804 | return -EINVAL; |
| 805 | |
| 806 | soft_iface = dev_get_by_index(net, ifindex); |
| 807 | if (!soft_iface || !batadv_softif_is_valid(soft_iface)) { |
| 808 | ret = -ENODEV; |
| 809 | goto out; |
| 810 | } |
| 811 | |
| 812 | bat_priv = netdev_priv(soft_iface); |
| 813 | |
| 814 | primary_if = batadv_primary_if_get_selected(bat_priv); |
| 815 | if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) { |
| 816 | ret = -ENOENT; |
| 817 | goto out; |
| 818 | } |
| 819 | |
| 820 | hard_ifindex = batadv_netlink_get_ifindex(cb->nlh, |
| 821 | BATADV_ATTR_HARD_IFINDEX); |
| 822 | if (hard_ifindex) { |
| 823 | hard_iface = dev_get_by_index(net, hard_ifindex); |
| 824 | if (hard_iface) |
| 825 | hardif = batadv_hardif_get_by_netdev(hard_iface); |
| 826 | |
| 827 | if (!hardif) { |
| 828 | ret = -ENODEV; |
| 829 | goto out; |
| 830 | } |
| 831 | |
| 832 | if (hardif->soft_iface != soft_iface) { |
| 833 | ret = -ENOENT; |
| 834 | goto out; |
| 835 | } |
| 836 | } |
| 837 | |
| 838 | if (!bat_priv->algo_ops->neigh.dump) { |
| 839 | ret = -EOPNOTSUPP; |
| 840 | goto out; |
| 841 | } |
| 842 | |
| 843 | bat_priv->algo_ops->neigh.dump(msg, cb, bat_priv, hardif); |
| 844 | |
| 845 | ret = msg->len; |
| 846 | |
| 847 | out: |
| 848 | if (hardif) |
| 849 | batadv_hardif_put(hardif); |
| 850 | if (hard_iface) |
| 851 | dev_put(hard_iface); |
| 852 | if (primary_if) |
| 853 | batadv_hardif_put(primary_if); |
| 854 | if (soft_iface) |
| 855 | dev_put(soft_iface); |
| 856 | |
| 857 | return ret; |
| 858 | } |
| 859 | |
| 860 | /** |
Sven Eckelmann | 7e9a8c2 | 2017-12-02 19:51:47 +0100 | [diff] [blame] | 861 | * batadv_orig_ifinfo_release() - release orig_ifinfo from lists and queue for |
Sven Eckelmann | 2baa753 | 2016-01-05 12:06:22 +0100 | [diff] [blame] | 862 | * free after rcu grace period |
Sven Eckelmann | a6ba0d3 | 2016-01-16 10:29:52 +0100 | [diff] [blame] | 863 | * @ref: kref pointer of the orig_ifinfo |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 864 | */ |
Sven Eckelmann | a6ba0d3 | 2016-01-16 10:29:52 +0100 | [diff] [blame] | 865 | static void batadv_orig_ifinfo_release(struct kref *ref) |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 866 | { |
Sven Eckelmann | a6ba0d3 | 2016-01-16 10:29:52 +0100 | [diff] [blame] | 867 | struct batadv_orig_ifinfo *orig_ifinfo; |
Simon Wunderlich | 000c8df | 2014-03-26 15:46:22 +0100 | [diff] [blame] | 868 | struct batadv_neigh_node *router; |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 869 | |
Sven Eckelmann | a6ba0d3 | 2016-01-16 10:29:52 +0100 | [diff] [blame] | 870 | orig_ifinfo = container_of(ref, struct batadv_orig_ifinfo, refcount); |
| 871 | |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 872 | if (orig_ifinfo->if_outgoing != BATADV_IF_DEFAULT) |
Sven Eckelmann | 82047ad | 2016-01-17 11:01:10 +0100 | [diff] [blame] | 873 | batadv_hardif_put(orig_ifinfo->if_outgoing); |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 874 | |
Simon Wunderlich | 000c8df | 2014-03-26 15:46:22 +0100 | [diff] [blame] | 875 | /* this is the last reference to this object */ |
| 876 | router = rcu_dereference_protected(orig_ifinfo->router, true); |
| 877 | if (router) |
Sven Eckelmann | 25bb250 | 2016-01-17 11:01:11 +0100 | [diff] [blame] | 878 | batadv_neigh_node_put(router); |
Sven Eckelmann | 2baa753 | 2016-01-05 12:06:22 +0100 | [diff] [blame] | 879 | |
| 880 | kfree_rcu(orig_ifinfo, rcu); |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 881 | } |
| 882 | |
| 883 | /** |
Sven Eckelmann | 7e9a8c2 | 2017-12-02 19:51:47 +0100 | [diff] [blame] | 884 | * batadv_orig_ifinfo_put() - decrement the refcounter and possibly release |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 885 | * the orig_ifinfo |
| 886 | * @orig_ifinfo: the orig_ifinfo object to release |
| 887 | */ |
Sven Eckelmann | 35f9477 | 2016-01-17 11:01:13 +0100 | [diff] [blame] | 888 | void batadv_orig_ifinfo_put(struct batadv_orig_ifinfo *orig_ifinfo) |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 889 | { |
Sven Eckelmann | a6ba0d3 | 2016-01-16 10:29:52 +0100 | [diff] [blame] | 890 | kref_put(&orig_ifinfo->refcount, batadv_orig_ifinfo_release); |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 891 | } |
| 892 | |
Sven Eckelmann | deed966 | 2016-01-05 12:06:21 +0100 | [diff] [blame] | 893 | /** |
Sven Eckelmann | 7e9a8c2 | 2017-12-02 19:51:47 +0100 | [diff] [blame] | 894 | * batadv_orig_node_free_rcu() - free the orig_node |
Sven Eckelmann | deed966 | 2016-01-05 12:06:21 +0100 | [diff] [blame] | 895 | * @rcu: rcu pointer of the orig_node |
| 896 | */ |
Sven Eckelmann | 03fc7f8 | 2012-05-12 18:34:00 +0200 | [diff] [blame] | 897 | static void batadv_orig_node_free_rcu(struct rcu_head *rcu) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 898 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 899 | struct batadv_orig_node *orig_node; |
Marek Lindner | 16b1aba | 2011-01-19 20:01:42 +0000 | [diff] [blame] | 900 | |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 901 | orig_node = container_of(rcu, struct batadv_orig_node, rcu); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 902 | |
Linus Lüssing | 60432d7 | 2014-02-15 17:47:51 +0100 | [diff] [blame] | 903 | batadv_mcast_purge_orig(orig_node); |
| 904 | |
Martin Hundebøll | 610bfc6bc | 2013-05-23 16:53:02 +0200 | [diff] [blame] | 905 | batadv_frag_purge_orig(orig_node, NULL); |
| 906 | |
Antonio Quartulli | 29824a5 | 2016-05-25 23:27:31 +0800 | [diff] [blame] | 907 | if (orig_node->bat_priv->algo_ops->orig.free) |
| 908 | orig_node->bat_priv->algo_ops->orig.free(orig_node); |
Antonio Quartulli | d0015fd | 2013-09-03 11:10:23 +0200 | [diff] [blame] | 909 | |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 910 | kfree(orig_node->tt_buff); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 911 | kfree(orig_node); |
| 912 | } |
| 913 | |
Linus Lüssing | 7282222 | 2013-04-15 21:43:29 +0800 | [diff] [blame] | 914 | /** |
Sven Eckelmann | 7e9a8c2 | 2017-12-02 19:51:47 +0100 | [diff] [blame] | 915 | * batadv_orig_node_release() - release orig_node from lists and queue for |
Sven Eckelmann | deed966 | 2016-01-05 12:06:21 +0100 | [diff] [blame] | 916 | * free after rcu grace period |
Sven Eckelmann | 7c12439 | 2016-01-16 10:29:56 +0100 | [diff] [blame] | 917 | * @ref: kref pointer of the orig_node |
Sven Eckelmann | deed966 | 2016-01-05 12:06:21 +0100 | [diff] [blame] | 918 | */ |
Sven Eckelmann | 7c12439 | 2016-01-16 10:29:56 +0100 | [diff] [blame] | 919 | static void batadv_orig_node_release(struct kref *ref) |
Sven Eckelmann | deed966 | 2016-01-05 12:06:21 +0100 | [diff] [blame] | 920 | { |
| 921 | struct hlist_node *node_tmp; |
| 922 | struct batadv_neigh_node *neigh_node; |
Sven Eckelmann | 7c12439 | 2016-01-16 10:29:56 +0100 | [diff] [blame] | 923 | struct batadv_orig_node *orig_node; |
Sven Eckelmann | deed966 | 2016-01-05 12:06:21 +0100 | [diff] [blame] | 924 | struct batadv_orig_ifinfo *orig_ifinfo; |
Sven Eckelmann | 33fbb1f | 2016-06-30 20:10:46 +0200 | [diff] [blame] | 925 | struct batadv_orig_node_vlan *vlan; |
Sven Eckelmann | cbef1e1 | 2016-06-30 21:41:13 +0200 | [diff] [blame] | 926 | struct batadv_orig_ifinfo *last_candidate; |
Sven Eckelmann | deed966 | 2016-01-05 12:06:21 +0100 | [diff] [blame] | 927 | |
Sven Eckelmann | 7c12439 | 2016-01-16 10:29:56 +0100 | [diff] [blame] | 928 | orig_node = container_of(ref, struct batadv_orig_node, refcount); |
| 929 | |
Sven Eckelmann | deed966 | 2016-01-05 12:06:21 +0100 | [diff] [blame] | 930 | spin_lock_bh(&orig_node->neigh_list_lock); |
| 931 | |
| 932 | /* for all neighbors towards this originator ... */ |
| 933 | hlist_for_each_entry_safe(neigh_node, node_tmp, |
| 934 | &orig_node->neigh_list, list) { |
| 935 | hlist_del_rcu(&neigh_node->list); |
Sven Eckelmann | 25bb250 | 2016-01-17 11:01:11 +0100 | [diff] [blame] | 936 | batadv_neigh_node_put(neigh_node); |
Sven Eckelmann | deed966 | 2016-01-05 12:06:21 +0100 | [diff] [blame] | 937 | } |
| 938 | |
| 939 | hlist_for_each_entry_safe(orig_ifinfo, node_tmp, |
| 940 | &orig_node->ifinfo_list, list) { |
| 941 | hlist_del_rcu(&orig_ifinfo->list); |
Sven Eckelmann | 35f9477 | 2016-01-17 11:01:13 +0100 | [diff] [blame] | 942 | batadv_orig_ifinfo_put(orig_ifinfo); |
Sven Eckelmann | deed966 | 2016-01-05 12:06:21 +0100 | [diff] [blame] | 943 | } |
Sven Eckelmann | cbef1e1 | 2016-06-30 21:41:13 +0200 | [diff] [blame] | 944 | |
| 945 | last_candidate = orig_node->last_bonding_candidate; |
| 946 | orig_node->last_bonding_candidate = NULL; |
Sven Eckelmann | deed966 | 2016-01-05 12:06:21 +0100 | [diff] [blame] | 947 | spin_unlock_bh(&orig_node->neigh_list_lock); |
| 948 | |
Sven Eckelmann | cbef1e1 | 2016-06-30 21:41:13 +0200 | [diff] [blame] | 949 | if (last_candidate) |
| 950 | batadv_orig_ifinfo_put(last_candidate); |
| 951 | |
Sven Eckelmann | 33fbb1f | 2016-06-30 20:10:46 +0200 | [diff] [blame] | 952 | spin_lock_bh(&orig_node->vlan_list_lock); |
| 953 | hlist_for_each_entry_safe(vlan, node_tmp, &orig_node->vlan_list, list) { |
| 954 | hlist_del_rcu(&vlan->list); |
| 955 | batadv_orig_node_vlan_put(vlan); |
| 956 | } |
| 957 | spin_unlock_bh(&orig_node->vlan_list_lock); |
| 958 | |
Sven Eckelmann | deed966 | 2016-01-05 12:06:21 +0100 | [diff] [blame] | 959 | /* Free nc_nodes */ |
| 960 | batadv_nc_purge_orig(orig_node->bat_priv, orig_node, NULL); |
| 961 | |
| 962 | call_rcu(&orig_node->rcu, batadv_orig_node_free_rcu); |
| 963 | } |
| 964 | |
| 965 | /** |
Sven Eckelmann | 7e9a8c2 | 2017-12-02 19:51:47 +0100 | [diff] [blame] | 966 | * batadv_orig_node_put() - decrement the orig node refcounter and possibly |
Sven Eckelmann | deed966 | 2016-01-05 12:06:21 +0100 | [diff] [blame] | 967 | * release it |
Linus Lüssing | 7282222 | 2013-04-15 21:43:29 +0800 | [diff] [blame] | 968 | * @orig_node: the orig node to free |
| 969 | */ |
Sven Eckelmann | 5d96731 | 2016-01-17 11:01:09 +0100 | [diff] [blame] | 970 | void batadv_orig_node_put(struct batadv_orig_node *orig_node) |
Marek Lindner | 7b36e8e | 2011-02-18 12:28:10 +0000 | [diff] [blame] | 971 | { |
Sven Eckelmann | 7c12439 | 2016-01-16 10:29:56 +0100 | [diff] [blame] | 972 | kref_put(&orig_node->refcount, batadv_orig_node_release); |
Marek Lindner | 7b36e8e | 2011-02-18 12:28:10 +0000 | [diff] [blame] | 973 | } |
| 974 | |
Sven Eckelmann | ff15c27 | 2017-12-02 19:51:53 +0100 | [diff] [blame] | 975 | /** |
| 976 | * batadv_originator_free() - Free all originator structures |
| 977 | * @bat_priv: the bat priv with all the soft interface information |
| 978 | */ |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 979 | void batadv_originator_free(struct batadv_priv *bat_priv) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 980 | { |
Sven Eckelmann | 5bf74e9 | 2012-06-05 22:31:28 +0200 | [diff] [blame] | 981 | struct batadv_hashtable *hash = bat_priv->orig_hash; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 982 | struct hlist_node *node_tmp; |
Marek Lindner | 16b1aba | 2011-01-19 20:01:42 +0000 | [diff] [blame] | 983 | struct hlist_head *head; |
Marek Lindner | 16b1aba | 2011-01-19 20:01:42 +0000 | [diff] [blame] | 984 | spinlock_t *list_lock; /* spinlock to protect write access */ |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 985 | struct batadv_orig_node *orig_node; |
Sven Eckelmann | 6b5e971 | 2015-05-26 18:34:26 +0200 | [diff] [blame] | 986 | u32 i; |
Marek Lindner | 16b1aba | 2011-01-19 20:01:42 +0000 | [diff] [blame] | 987 | |
| 988 | if (!hash) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 989 | return; |
| 990 | |
| 991 | cancel_delayed_work_sync(&bat_priv->orig_work); |
| 992 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 993 | bat_priv->orig_hash = NULL; |
Marek Lindner | 16b1aba | 2011-01-19 20:01:42 +0000 | [diff] [blame] | 994 | |
| 995 | for (i = 0; i < hash->size; i++) { |
| 996 | head = &hash->table[i]; |
| 997 | list_lock = &hash->list_locks[i]; |
| 998 | |
| 999 | spin_lock_bh(list_lock); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1000 | hlist_for_each_entry_safe(orig_node, node_tmp, |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 1001 | head, hash_entry) { |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1002 | hlist_del_rcu(&orig_node->hash_entry); |
Sven Eckelmann | 5d96731 | 2016-01-17 11:01:09 +0100 | [diff] [blame] | 1003 | batadv_orig_node_put(orig_node); |
Marek Lindner | 16b1aba | 2011-01-19 20:01:42 +0000 | [diff] [blame] | 1004 | } |
| 1005 | spin_unlock_bh(list_lock); |
| 1006 | } |
| 1007 | |
Sven Eckelmann | 1a8eaf0 | 2012-05-12 02:09:32 +0200 | [diff] [blame] | 1008 | batadv_hash_destroy(hash); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1009 | } |
| 1010 | |
Antonio Quartulli | bbad0a5 | 2013-09-02 12:15:02 +0200 | [diff] [blame] | 1011 | /** |
Sven Eckelmann | 7e9a8c2 | 2017-12-02 19:51:47 +0100 | [diff] [blame] | 1012 | * batadv_orig_node_new() - creates a new orig_node |
Antonio Quartulli | bbad0a5 | 2013-09-02 12:15:02 +0200 | [diff] [blame] | 1013 | * @bat_priv: the bat priv with all the soft interface information |
| 1014 | * @addr: the mac address of the originator |
| 1015 | * |
| 1016 | * Creates a new originator object and initialise all the generic fields. |
| 1017 | * The new object is not added to the originator list. |
Sven Eckelmann | 62fe710 | 2015-09-15 19:00:48 +0200 | [diff] [blame] | 1018 | * |
| 1019 | * Return: the newly created object or NULL on failure. |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 1020 | */ |
Antonio Quartulli | bbad0a5 | 2013-09-02 12:15:02 +0200 | [diff] [blame] | 1021 | 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] | 1022 | const u8 *addr) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1023 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 1024 | struct batadv_orig_node *orig_node; |
Antonio Quartulli | 7ea7b4a | 2013-07-30 22:16:25 +0200 | [diff] [blame] | 1025 | struct batadv_orig_node_vlan *vlan; |
Sven Eckelmann | 42d0b04 | 2012-06-03 22:19:17 +0200 | [diff] [blame] | 1026 | unsigned long reset_time; |
Antonio Quartulli | bbad0a5 | 2013-09-02 12:15:02 +0200 | [diff] [blame] | 1027 | int i; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1028 | |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 1029 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
| 1030 | "Creating new originator: %pM\n", addr); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1031 | |
Sven Eckelmann | 704509b | 2011-05-14 23:14:54 +0200 | [diff] [blame] | 1032 | orig_node = kzalloc(sizeof(*orig_node), GFP_ATOMIC); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1033 | if (!orig_node) |
| 1034 | return NULL; |
| 1035 | |
Marek Lindner | 9591a79 | 2010-12-12 21:57:11 +0000 | [diff] [blame] | 1036 | INIT_HLIST_HEAD(&orig_node->neigh_list); |
Marek Lindner | d0fa4f3 | 2015-06-22 00:30:22 +0800 | [diff] [blame] | 1037 | INIT_HLIST_HEAD(&orig_node->vlan_list); |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 1038 | INIT_HLIST_HEAD(&orig_node->ifinfo_list); |
Marek Lindner | f3e0008 | 2011-01-25 21:52:11 +0000 | [diff] [blame] | 1039 | spin_lock_init(&orig_node->bcast_seqno_lock); |
Marek Lindner | f987ed6 | 2010-12-12 21:57:12 +0000 | [diff] [blame] | 1040 | spin_lock_init(&orig_node->neigh_list_lock); |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1041 | spin_lock_init(&orig_node->tt_buff_lock); |
Antonio Quartulli | a70a9aa | 2013-07-30 22:16:24 +0200 | [diff] [blame] | 1042 | spin_lock_init(&orig_node->tt_lock); |
Antonio Quartulli | 7ea7b4a | 2013-07-30 22:16:25 +0200 | [diff] [blame] | 1043 | spin_lock_init(&orig_node->vlan_list_lock); |
Marek Lindner | 7b36e8e | 2011-02-18 12:28:10 +0000 | [diff] [blame] | 1044 | |
Martin Hundebøll | d56b170 | 2013-01-25 11:12:39 +0100 | [diff] [blame] | 1045 | batadv_nc_init_orig(orig_node); |
| 1046 | |
Marek Lindner | 7b36e8e | 2011-02-18 12:28:10 +0000 | [diff] [blame] | 1047 | /* extra reference for return */ |
Sven Eckelmann | 7c12439 | 2016-01-16 10:29:56 +0100 | [diff] [blame] | 1048 | kref_init(&orig_node->refcount); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1049 | |
Marek Lindner | 16b1aba | 2011-01-19 20:01:42 +0000 | [diff] [blame] | 1050 | orig_node->bat_priv = bat_priv; |
Antonio Quartulli | 8fdd015 | 2014-01-22 00:42:11 +0100 | [diff] [blame] | 1051 | ether_addr_copy(orig_node->orig, addr); |
Antonio Quartulli | 785ea11 | 2011-11-23 11:35:44 +0100 | [diff] [blame] | 1052 | batadv_dat_init_orig_node_addr(orig_node); |
Antonio Quartulli | c8c991b | 2011-07-07 01:40:57 +0200 | [diff] [blame] | 1053 | atomic_set(&orig_node->last_ttvn, 0); |
Antonio Quartulli | 2dafb49 | 2011-05-05 08:42:45 +0200 | [diff] [blame] | 1054 | orig_node->tt_buff = NULL; |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1055 | orig_node->tt_buff_len = 0; |
Linus Lüssing | 2c667a3 | 2014-10-30 06:23:40 +0100 | [diff] [blame] | 1056 | orig_node->last_seen = jiffies; |
Sven Eckelmann | 42d0b04 | 2012-06-03 22:19:17 +0200 | [diff] [blame] | 1057 | reset_time = jiffies - 1 - msecs_to_jiffies(BATADV_RESET_PROTECTION_MS); |
| 1058 | orig_node->bcast_seqno_reset = reset_time; |
Linus Lüssing | 8a4023c | 2015-06-16 17:10:26 +0200 | [diff] [blame] | 1059 | |
Linus Lüssing | 60432d7 | 2014-02-15 17:47:51 +0100 | [diff] [blame] | 1060 | #ifdef CONFIG_BATMAN_ADV_MCAST |
| 1061 | orig_node->mcast_flags = BATADV_NO_FLAGS; |
Linus Lüssing | 8a4023c | 2015-06-16 17:10:26 +0200 | [diff] [blame] | 1062 | INIT_HLIST_NODE(&orig_node->mcast_want_all_unsnoopables_node); |
| 1063 | INIT_HLIST_NODE(&orig_node->mcast_want_all_ipv4_node); |
| 1064 | INIT_HLIST_NODE(&orig_node->mcast_want_all_ipv6_node); |
| 1065 | spin_lock_init(&orig_node->mcast_handler_lock); |
Linus Lüssing | 60432d7 | 2014-02-15 17:47:51 +0100 | [diff] [blame] | 1066 | #endif |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1067 | |
Antonio Quartulli | 7ea7b4a | 2013-07-30 22:16:25 +0200 | [diff] [blame] | 1068 | /* create a vlan object for the "untagged" LAN */ |
| 1069 | vlan = batadv_orig_node_vlan_new(orig_node, BATADV_NO_FLAGS); |
| 1070 | if (!vlan) |
| 1071 | goto free_orig_node; |
| 1072 | /* batadv_orig_node_vlan_new() increases the refcounter. |
| 1073 | * Immediately release vlan since it is not needed anymore in this |
| 1074 | * context |
| 1075 | */ |
Sven Eckelmann | 21754e2 | 2016-01-17 11:01:24 +0100 | [diff] [blame] | 1076 | batadv_orig_node_vlan_put(vlan); |
Antonio Quartulli | 7ea7b4a | 2013-07-30 22:16:25 +0200 | [diff] [blame] | 1077 | |
Martin Hundebøll | 610bfc6bc | 2013-05-23 16:53:02 +0200 | [diff] [blame] | 1078 | for (i = 0; i < BATADV_FRAG_BUFFER_COUNT; i++) { |
Sven Eckelmann | 176e5b7 | 2016-07-27 12:31:07 +0200 | [diff] [blame] | 1079 | INIT_HLIST_HEAD(&orig_node->fragments[i].fragment_list); |
Martin Hundebøll | 610bfc6bc | 2013-05-23 16:53:02 +0200 | [diff] [blame] | 1080 | spin_lock_init(&orig_node->fragments[i].lock); |
| 1081 | orig_node->fragments[i].size = 0; |
| 1082 | } |
| 1083 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1084 | return orig_node; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1085 | free_orig_node: |
| 1086 | kfree(orig_node); |
| 1087 | return NULL; |
| 1088 | } |
| 1089 | |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 1090 | /** |
Sven Eckelmann | 7e9a8c2 | 2017-12-02 19:51:47 +0100 | [diff] [blame] | 1091 | * batadv_purge_neigh_ifinfo() - purge obsolete ifinfo entries from neighbor |
Simon Wunderlich | 709de13 | 2014-03-26 15:46:24 +0100 | [diff] [blame] | 1092 | * @bat_priv: the bat priv with all the soft interface information |
| 1093 | * @neigh: orig node which is to be checked |
| 1094 | */ |
| 1095 | static void |
| 1096 | batadv_purge_neigh_ifinfo(struct batadv_priv *bat_priv, |
| 1097 | struct batadv_neigh_node *neigh) |
| 1098 | { |
| 1099 | struct batadv_neigh_ifinfo *neigh_ifinfo; |
| 1100 | struct batadv_hard_iface *if_outgoing; |
| 1101 | struct hlist_node *node_tmp; |
| 1102 | |
| 1103 | spin_lock_bh(&neigh->ifinfo_lock); |
| 1104 | |
| 1105 | /* for all ifinfo objects for this neighinator */ |
| 1106 | hlist_for_each_entry_safe(neigh_ifinfo, node_tmp, |
| 1107 | &neigh->ifinfo_list, list) { |
| 1108 | if_outgoing = neigh_ifinfo->if_outgoing; |
| 1109 | |
| 1110 | /* always keep the default interface */ |
| 1111 | if (if_outgoing == BATADV_IF_DEFAULT) |
| 1112 | continue; |
| 1113 | |
| 1114 | /* don't purge if the interface is not (going) down */ |
Sven Eckelmann | 825ffe1 | 2017-08-23 21:52:13 +0200 | [diff] [blame] | 1115 | if (if_outgoing->if_status != BATADV_IF_INACTIVE && |
| 1116 | if_outgoing->if_status != BATADV_IF_NOT_IN_USE && |
| 1117 | if_outgoing->if_status != BATADV_IF_TO_BE_REMOVED) |
Simon Wunderlich | 709de13 | 2014-03-26 15:46:24 +0100 | [diff] [blame] | 1118 | continue; |
| 1119 | |
| 1120 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
| 1121 | "neighbor/ifinfo purge: neighbor %pM, iface: %s\n", |
| 1122 | neigh->addr, if_outgoing->net_dev->name); |
| 1123 | |
| 1124 | hlist_del_rcu(&neigh_ifinfo->list); |
Sven Eckelmann | 044fa3a | 2016-01-17 11:01:12 +0100 | [diff] [blame] | 1125 | batadv_neigh_ifinfo_put(neigh_ifinfo); |
Simon Wunderlich | 709de13 | 2014-03-26 15:46:24 +0100 | [diff] [blame] | 1126 | } |
| 1127 | |
| 1128 | spin_unlock_bh(&neigh->ifinfo_lock); |
| 1129 | } |
| 1130 | |
| 1131 | /** |
Sven Eckelmann | 7e9a8c2 | 2017-12-02 19:51:47 +0100 | [diff] [blame] | 1132 | * batadv_purge_orig_ifinfo() - purge obsolete ifinfo entries from originator |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 1133 | * @bat_priv: the bat priv with all the soft interface information |
| 1134 | * @orig_node: orig node which is to be checked |
| 1135 | * |
Sven Eckelmann | 62fe710 | 2015-09-15 19:00:48 +0200 | [diff] [blame] | 1136 | * Return: true if any ifinfo entry was purged, false otherwise. |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 1137 | */ |
| 1138 | static bool |
| 1139 | batadv_purge_orig_ifinfo(struct batadv_priv *bat_priv, |
| 1140 | struct batadv_orig_node *orig_node) |
| 1141 | { |
| 1142 | struct batadv_orig_ifinfo *orig_ifinfo; |
| 1143 | struct batadv_hard_iface *if_outgoing; |
| 1144 | struct hlist_node *node_tmp; |
| 1145 | bool ifinfo_purged = false; |
| 1146 | |
| 1147 | spin_lock_bh(&orig_node->neigh_list_lock); |
| 1148 | |
| 1149 | /* for all ifinfo objects for this originator */ |
| 1150 | hlist_for_each_entry_safe(orig_ifinfo, node_tmp, |
| 1151 | &orig_node->ifinfo_list, list) { |
| 1152 | if_outgoing = orig_ifinfo->if_outgoing; |
| 1153 | |
| 1154 | /* always keep the default interface */ |
| 1155 | if (if_outgoing == BATADV_IF_DEFAULT) |
| 1156 | continue; |
| 1157 | |
| 1158 | /* don't purge if the interface is not (going) down */ |
Sven Eckelmann | 825ffe1 | 2017-08-23 21:52:13 +0200 | [diff] [blame] | 1159 | if (if_outgoing->if_status != BATADV_IF_INACTIVE && |
| 1160 | if_outgoing->if_status != BATADV_IF_NOT_IN_USE && |
| 1161 | if_outgoing->if_status != BATADV_IF_TO_BE_REMOVED) |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 1162 | continue; |
| 1163 | |
| 1164 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
| 1165 | "router/ifinfo purge: originator %pM, iface: %s\n", |
| 1166 | orig_node->orig, if_outgoing->net_dev->name); |
| 1167 | |
| 1168 | ifinfo_purged = true; |
| 1169 | |
| 1170 | hlist_del_rcu(&orig_ifinfo->list); |
Sven Eckelmann | 35f9477 | 2016-01-17 11:01:13 +0100 | [diff] [blame] | 1171 | batadv_orig_ifinfo_put(orig_ifinfo); |
Simon Wunderlich | f3b3d90 | 2013-11-13 19:14:50 +0100 | [diff] [blame] | 1172 | if (orig_node->last_bonding_candidate == orig_ifinfo) { |
| 1173 | orig_node->last_bonding_candidate = NULL; |
Sven Eckelmann | 35f9477 | 2016-01-17 11:01:13 +0100 | [diff] [blame] | 1174 | batadv_orig_ifinfo_put(orig_ifinfo); |
Simon Wunderlich | f3b3d90 | 2013-11-13 19:14:50 +0100 | [diff] [blame] | 1175 | } |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 1176 | } |
| 1177 | |
| 1178 | spin_unlock_bh(&orig_node->neigh_list_lock); |
| 1179 | |
| 1180 | return ifinfo_purged; |
| 1181 | } |
| 1182 | |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 1183 | /** |
Sven Eckelmann | 7e9a8c2 | 2017-12-02 19:51:47 +0100 | [diff] [blame] | 1184 | * batadv_purge_orig_neighbors() - purges neighbors from originator |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 1185 | * @bat_priv: the bat priv with all the soft interface information |
| 1186 | * @orig_node: orig node which is to be checked |
| 1187 | * |
Sven Eckelmann | 62fe710 | 2015-09-15 19:00:48 +0200 | [diff] [blame] | 1188 | * Return: true if any neighbor was purged, false otherwise |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 1189 | */ |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 1190 | static bool |
| 1191 | batadv_purge_orig_neighbors(struct batadv_priv *bat_priv, |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 1192 | struct batadv_orig_node *orig_node) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1193 | { |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1194 | struct hlist_node *node_tmp; |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 1195 | struct batadv_neigh_node *neigh_node; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1196 | bool neigh_purged = false; |
Marek Lindner | 0b0094e | 2012-03-01 15:35:20 +0800 | [diff] [blame] | 1197 | unsigned long last_seen; |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 1198 | struct batadv_hard_iface *if_incoming; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1199 | |
Marek Lindner | f987ed6 | 2010-12-12 21:57:12 +0000 | [diff] [blame] | 1200 | spin_lock_bh(&orig_node->neigh_list_lock); |
| 1201 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1202 | /* for all neighbors towards this originator ... */ |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1203 | hlist_for_each_entry_safe(neigh_node, node_tmp, |
Marek Lindner | 9591a79 | 2010-12-12 21:57:11 +0000 | [diff] [blame] | 1204 | &orig_node->neigh_list, list) { |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 1205 | last_seen = neigh_node->last_seen; |
| 1206 | if_incoming = neigh_node->if_incoming; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1207 | |
Sven Eckelmann | 825ffe1 | 2017-08-23 21:52:13 +0200 | [diff] [blame] | 1208 | if (batadv_has_timed_out(last_seen, BATADV_PURGE_TIMEOUT) || |
| 1209 | if_incoming->if_status == BATADV_IF_INACTIVE || |
| 1210 | if_incoming->if_status == BATADV_IF_NOT_IN_USE || |
| 1211 | if_incoming->if_status == BATADV_IF_TO_BE_REMOVED) { |
| 1212 | if (if_incoming->if_status == BATADV_IF_INACTIVE || |
| 1213 | if_incoming->if_status == BATADV_IF_NOT_IN_USE || |
| 1214 | if_incoming->if_status == BATADV_IF_TO_BE_REMOVED) |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 1215 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 1216 | "neighbor purge: originator %pM, neighbor: %pM, iface: %s\n", |
| 1217 | orig_node->orig, neigh_node->addr, |
| 1218 | if_incoming->net_dev->name); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1219 | else |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 1220 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 1221 | "neighbor timeout: originator %pM, neighbor: %pM, last_seen: %u\n", |
| 1222 | orig_node->orig, neigh_node->addr, |
| 1223 | jiffies_to_msecs(last_seen)); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1224 | |
| 1225 | neigh_purged = true; |
Marek Lindner | 9591a79 | 2010-12-12 21:57:11 +0000 | [diff] [blame] | 1226 | |
Marek Lindner | f987ed6 | 2010-12-12 21:57:12 +0000 | [diff] [blame] | 1227 | hlist_del_rcu(&neigh_node->list); |
Sven Eckelmann | 25bb250 | 2016-01-17 11:01:11 +0100 | [diff] [blame] | 1228 | batadv_neigh_node_put(neigh_node); |
Simon Wunderlich | 709de13 | 2014-03-26 15:46:24 +0100 | [diff] [blame] | 1229 | } else { |
| 1230 | /* only necessary if not the whole neighbor is to be |
| 1231 | * deleted, but some interface has been removed. |
| 1232 | */ |
| 1233 | batadv_purge_neigh_ifinfo(bat_priv, neigh_node); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1234 | } |
| 1235 | } |
Marek Lindner | f987ed6 | 2010-12-12 21:57:12 +0000 | [diff] [blame] | 1236 | |
| 1237 | spin_unlock_bh(&orig_node->neigh_list_lock); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1238 | return neigh_purged; |
| 1239 | } |
| 1240 | |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 1241 | /** |
Sven Eckelmann | 7e9a8c2 | 2017-12-02 19:51:47 +0100 | [diff] [blame] | 1242 | * batadv_find_best_neighbor() - finds the best neighbor after purging |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 1243 | * @bat_priv: the bat priv with all the soft interface information |
| 1244 | * @orig_node: orig node which is to be checked |
| 1245 | * @if_outgoing: the interface for which the metric should be compared |
| 1246 | * |
Sven Eckelmann | 62fe710 | 2015-09-15 19:00:48 +0200 | [diff] [blame] | 1247 | * Return: the current best neighbor, with refcount increased. |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 1248 | */ |
| 1249 | static struct batadv_neigh_node * |
| 1250 | batadv_find_best_neighbor(struct batadv_priv *bat_priv, |
| 1251 | struct batadv_orig_node *orig_node, |
| 1252 | struct batadv_hard_iface *if_outgoing) |
| 1253 | { |
| 1254 | struct batadv_neigh_node *best = NULL, *neigh; |
Antonio Quartulli | 29824a5 | 2016-05-25 23:27:31 +0800 | [diff] [blame] | 1255 | struct batadv_algo_ops *bao = bat_priv->algo_ops; |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 1256 | |
| 1257 | rcu_read_lock(); |
| 1258 | hlist_for_each_entry_rcu(neigh, &orig_node->neigh_list, list) { |
Antonio Quartulli | 29824a5 | 2016-05-25 23:27:31 +0800 | [diff] [blame] | 1259 | if (best && (bao->neigh.cmp(neigh, if_outgoing, best, |
| 1260 | if_outgoing) <= 0)) |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 1261 | continue; |
| 1262 | |
Sven Eckelmann | 77ae32e | 2016-01-16 10:29:53 +0100 | [diff] [blame] | 1263 | if (!kref_get_unless_zero(&neigh->refcount)) |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 1264 | continue; |
| 1265 | |
| 1266 | if (best) |
Sven Eckelmann | 25bb250 | 2016-01-17 11:01:11 +0100 | [diff] [blame] | 1267 | batadv_neigh_node_put(best); |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 1268 | |
| 1269 | best = neigh; |
| 1270 | } |
| 1271 | rcu_read_unlock(); |
| 1272 | |
| 1273 | return best; |
| 1274 | } |
| 1275 | |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 1276 | /** |
Sven Eckelmann | 7e9a8c2 | 2017-12-02 19:51:47 +0100 | [diff] [blame] | 1277 | * batadv_purge_orig_node() - purges obsolete information from an orig_node |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 1278 | * @bat_priv: the bat priv with all the soft interface information |
| 1279 | * @orig_node: orig node which is to be checked |
| 1280 | * |
| 1281 | * This function checks if the orig_node or substructures of it have become |
| 1282 | * obsolete, and purges this information if that's the case. |
| 1283 | * |
Sven Eckelmann | 62fe710 | 2015-09-15 19:00:48 +0200 | [diff] [blame] | 1284 | * Return: true if the orig_node is to be removed, false otherwise. |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 1285 | */ |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 1286 | static bool batadv_purge_orig_node(struct batadv_priv *bat_priv, |
| 1287 | struct batadv_orig_node *orig_node) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1288 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 1289 | struct batadv_neigh_node *best_neigh_node; |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 1290 | struct batadv_hard_iface *hard_iface; |
Simon Wunderlich | 7b955a9 | 2014-03-26 15:46:23 +0100 | [diff] [blame] | 1291 | bool changed_ifinfo, changed_neigh; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1292 | |
Sven Eckelmann | 42d0b04 | 2012-06-03 22:19:17 +0200 | [diff] [blame] | 1293 | if (batadv_has_timed_out(orig_node->last_seen, |
| 1294 | 2 * BATADV_PURGE_TIMEOUT)) { |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 1295 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 1296 | "Originator timeout: originator %pM, last_seen %u\n", |
| 1297 | orig_node->orig, |
| 1298 | jiffies_to_msecs(orig_node->last_seen)); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1299 | return true; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1300 | } |
Simon Wunderlich | 7b955a9 | 2014-03-26 15:46:23 +0100 | [diff] [blame] | 1301 | changed_ifinfo = batadv_purge_orig_ifinfo(bat_priv, orig_node); |
| 1302 | changed_neigh = batadv_purge_orig_neighbors(bat_priv, orig_node); |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 1303 | |
Simon Wunderlich | 7b955a9 | 2014-03-26 15:46:23 +0100 | [diff] [blame] | 1304 | if (!changed_ifinfo && !changed_neigh) |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 1305 | return false; |
| 1306 | |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 1307 | /* first for NULL ... */ |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 1308 | best_neigh_node = batadv_find_best_neighbor(bat_priv, orig_node, |
| 1309 | BATADV_IF_DEFAULT); |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 1310 | batadv_update_route(bat_priv, orig_node, BATADV_IF_DEFAULT, |
| 1311 | best_neigh_node); |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 1312 | if (best_neigh_node) |
Sven Eckelmann | 25bb250 | 2016-01-17 11:01:11 +0100 | [diff] [blame] | 1313 | batadv_neigh_node_put(best_neigh_node); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1314 | |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 1315 | /* ... then for all other interfaces. */ |
| 1316 | rcu_read_lock(); |
| 1317 | list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) { |
| 1318 | if (hard_iface->if_status != BATADV_IF_ACTIVE) |
| 1319 | continue; |
| 1320 | |
| 1321 | if (hard_iface->soft_iface != bat_priv->soft_iface) |
| 1322 | continue; |
| 1323 | |
Sven Eckelmann | 2735344 | 2016-03-05 16:09:16 +0100 | [diff] [blame] | 1324 | if (!kref_get_unless_zero(&hard_iface->refcount)) |
| 1325 | continue; |
| 1326 | |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 1327 | best_neigh_node = batadv_find_best_neighbor(bat_priv, |
| 1328 | orig_node, |
| 1329 | hard_iface); |
| 1330 | batadv_update_route(bat_priv, orig_node, hard_iface, |
| 1331 | best_neigh_node); |
| 1332 | if (best_neigh_node) |
Sven Eckelmann | 25bb250 | 2016-01-17 11:01:11 +0100 | [diff] [blame] | 1333 | batadv_neigh_node_put(best_neigh_node); |
Sven Eckelmann | 2735344 | 2016-03-05 16:09:16 +0100 | [diff] [blame] | 1334 | |
| 1335 | batadv_hardif_put(hard_iface); |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 1336 | } |
| 1337 | rcu_read_unlock(); |
| 1338 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1339 | return false; |
| 1340 | } |
| 1341 | |
Sven Eckelmann | 3b1709d | 2018-07-07 21:46:11 +0200 | [diff] [blame] | 1342 | /** |
| 1343 | * batadv_purge_orig_ref() - Purge all outdated originators |
| 1344 | * @bat_priv: the bat priv with all the soft interface information |
| 1345 | */ |
| 1346 | void batadv_purge_orig_ref(struct batadv_priv *bat_priv) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1347 | { |
Sven Eckelmann | 5bf74e9 | 2012-06-05 22:31:28 +0200 | [diff] [blame] | 1348 | struct batadv_hashtable *hash = bat_priv->orig_hash; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1349 | struct hlist_node *node_tmp; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1350 | struct hlist_head *head; |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame] | 1351 | spinlock_t *list_lock; /* spinlock to protect write access */ |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 1352 | struct batadv_orig_node *orig_node; |
Sven Eckelmann | 6b5e971 | 2015-05-26 18:34:26 +0200 | [diff] [blame] | 1353 | u32 i; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1354 | |
| 1355 | if (!hash) |
| 1356 | return; |
| 1357 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1358 | /* for all origins... */ |
| 1359 | for (i = 0; i < hash->size; i++) { |
| 1360 | head = &hash->table[i]; |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame] | 1361 | list_lock = &hash->list_locks[i]; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1362 | |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame] | 1363 | spin_lock_bh(list_lock); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1364 | hlist_for_each_entry_safe(orig_node, node_tmp, |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 1365 | head, hash_entry) { |
Sven Eckelmann | 03fc7f8 | 2012-05-12 18:34:00 +0200 | [diff] [blame] | 1366 | if (batadv_purge_orig_node(bat_priv, orig_node)) { |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 1367 | batadv_gw_node_delete(bat_priv, orig_node); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1368 | hlist_del_rcu(&orig_node->hash_entry); |
Linus Lüssing | 9d31b3c | 2014-12-13 23:32:15 +0100 | [diff] [blame] | 1369 | batadv_tt_global_del_orig(orig_node->bat_priv, |
| 1370 | orig_node, -1, |
| 1371 | "originator timed out"); |
Sven Eckelmann | 5d96731 | 2016-01-17 11:01:09 +0100 | [diff] [blame] | 1372 | batadv_orig_node_put(orig_node); |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame] | 1373 | continue; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1374 | } |
Martin Hundebøll | 610bfc6bc | 2013-05-23 16:53:02 +0200 | [diff] [blame] | 1375 | |
| 1376 | batadv_frag_purge_orig(orig_node, |
| 1377 | batadv_frag_check_entry); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1378 | } |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame] | 1379 | spin_unlock_bh(list_lock); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1380 | } |
| 1381 | |
Sven Eckelmann | 7cf06bc | 2012-05-12 02:09:29 +0200 | [diff] [blame] | 1382 | batadv_gw_election(bat_priv); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1383 | } |
| 1384 | |
Sven Eckelmann | 03fc7f8 | 2012-05-12 18:34:00 +0200 | [diff] [blame] | 1385 | static void batadv_purge_orig(struct work_struct *work) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1386 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 1387 | struct delayed_work *delayed_work; |
| 1388 | struct batadv_priv *bat_priv; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1389 | |
Geliang Tang | 4ba4bc0 | 2015-12-28 23:43:37 +0800 | [diff] [blame] | 1390 | delayed_work = to_delayed_work(work); |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 1391 | bat_priv = container_of(delayed_work, struct batadv_priv, orig_work); |
Sven Eckelmann | 3b1709d | 2018-07-07 21:46:11 +0200 | [diff] [blame] | 1392 | batadv_purge_orig_ref(bat_priv); |
Antonio Quartulli | 7241444 | 2012-12-25 13:14:37 +0100 | [diff] [blame] | 1393 | queue_delayed_work(batadv_event_workqueue, |
| 1394 | &bat_priv->orig_work, |
| 1395 | msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD)); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1396 | } |
| 1397 | |
Sven Eckelmann | dc1cbd1 | 2016-07-16 09:31:20 +0200 | [diff] [blame] | 1398 | #ifdef CONFIG_BATMAN_ADV_DEBUGFS |
Sven Eckelmann | ff15c27 | 2017-12-02 19:51:53 +0100 | [diff] [blame] | 1399 | |
| 1400 | /** |
| 1401 | * batadv_orig_seq_print_text() - Print the originator table in a seq file |
| 1402 | * @seq: seq file to print on |
| 1403 | * @offset: not used |
| 1404 | * |
| 1405 | * Return: always 0 |
| 1406 | */ |
Sven Eckelmann | 7d211ef | 2012-05-12 02:09:34 +0200 | [diff] [blame] | 1407 | int batadv_orig_seq_print_text(struct seq_file *seq, void *offset) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1408 | { |
| 1409 | struct net_device *net_dev = (struct net_device *)seq->private; |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 1410 | struct batadv_priv *bat_priv = netdev_priv(net_dev); |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 1411 | struct batadv_hard_iface *primary_if; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1412 | |
Marek Lindner | 30da63a | 2012-08-03 17:15:46 +0200 | [diff] [blame] | 1413 | primary_if = batadv_seq_print_text_primary_if_get(seq); |
| 1414 | if (!primary_if) |
Antonio Quartulli | 737a2a22 | 2013-09-02 12:15:03 +0200 | [diff] [blame] | 1415 | return 0; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1416 | |
Antonio Quartulli | 737a2a22 | 2013-09-02 12:15:03 +0200 | [diff] [blame] | 1417 | 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] | 1418 | BATADV_SOURCE_VERSION, primary_if->net_dev->name, |
Antonio Quartulli | 737a2a22 | 2013-09-02 12:15:03 +0200 | [diff] [blame] | 1419 | primary_if->net_dev->dev_addr, net_dev->name, |
Antonio Quartulli | 29824a5 | 2016-05-25 23:27:31 +0800 | [diff] [blame] | 1420 | bat_priv->algo_ops->name); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1421 | |
Sven Eckelmann | 82047ad | 2016-01-17 11:01:10 +0100 | [diff] [blame] | 1422 | batadv_hardif_put(primary_if); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1423 | |
Antonio Quartulli | 29824a5 | 2016-05-25 23:27:31 +0800 | [diff] [blame] | 1424 | if (!bat_priv->algo_ops->orig.print) { |
Antonio Quartulli | 737a2a22 | 2013-09-02 12:15:03 +0200 | [diff] [blame] | 1425 | seq_puts(seq, |
| 1426 | "No printing function for this routing protocol\n"); |
| 1427 | return 0; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1428 | } |
| 1429 | |
Antonio Quartulli | 29824a5 | 2016-05-25 23:27:31 +0800 | [diff] [blame] | 1430 | bat_priv->algo_ops->orig.print(bat_priv, seq, BATADV_IF_DEFAULT); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1431 | |
Marek Lindner | 30da63a | 2012-08-03 17:15:46 +0200 | [diff] [blame] | 1432 | return 0; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1433 | } |
| 1434 | |
Simon Wunderlich | cb1c92e | 2013-11-21 11:52:16 +0100 | [diff] [blame] | 1435 | /** |
Sven Eckelmann | 7e9a8c2 | 2017-12-02 19:51:47 +0100 | [diff] [blame] | 1436 | * batadv_orig_hardif_seq_print_text() - writes originator infos for a specific |
Simon Wunderlich | cb1c92e | 2013-11-21 11:52:16 +0100 | [diff] [blame] | 1437 | * outgoing interface |
| 1438 | * @seq: debugfs table seq_file struct |
| 1439 | * @offset: not used |
| 1440 | * |
Sven Eckelmann | 62fe710 | 2015-09-15 19:00:48 +0200 | [diff] [blame] | 1441 | * Return: 0 |
Simon Wunderlich | cb1c92e | 2013-11-21 11:52:16 +0100 | [diff] [blame] | 1442 | */ |
| 1443 | int batadv_orig_hardif_seq_print_text(struct seq_file *seq, void *offset) |
| 1444 | { |
| 1445 | struct net_device *net_dev = (struct net_device *)seq->private; |
| 1446 | struct batadv_hard_iface *hard_iface; |
| 1447 | struct batadv_priv *bat_priv; |
| 1448 | |
| 1449 | hard_iface = batadv_hardif_get_by_netdev(net_dev); |
| 1450 | |
| 1451 | if (!hard_iface || !hard_iface->soft_iface) { |
| 1452 | seq_puts(seq, "Interface not known to B.A.T.M.A.N.\n"); |
| 1453 | goto out; |
| 1454 | } |
| 1455 | |
| 1456 | bat_priv = netdev_priv(hard_iface->soft_iface); |
Antonio Quartulli | 29824a5 | 2016-05-25 23:27:31 +0800 | [diff] [blame] | 1457 | if (!bat_priv->algo_ops->orig.print) { |
Simon Wunderlich | cb1c92e | 2013-11-21 11:52:16 +0100 | [diff] [blame] | 1458 | seq_puts(seq, |
| 1459 | "No printing function for this routing protocol\n"); |
| 1460 | goto out; |
| 1461 | } |
| 1462 | |
| 1463 | if (hard_iface->if_status != BATADV_IF_ACTIVE) { |
| 1464 | seq_puts(seq, "Interface not active\n"); |
| 1465 | goto out; |
| 1466 | } |
| 1467 | |
| 1468 | seq_printf(seq, "[B.A.T.M.A.N. adv %s, IF/MAC: %s/%pM (%s %s)]\n", |
| 1469 | BATADV_SOURCE_VERSION, hard_iface->net_dev->name, |
| 1470 | hard_iface->net_dev->dev_addr, |
Antonio Quartulli | 29824a5 | 2016-05-25 23:27:31 +0800 | [diff] [blame] | 1471 | hard_iface->soft_iface->name, bat_priv->algo_ops->name); |
Simon Wunderlich | cb1c92e | 2013-11-21 11:52:16 +0100 | [diff] [blame] | 1472 | |
Antonio Quartulli | 29824a5 | 2016-05-25 23:27:31 +0800 | [diff] [blame] | 1473 | bat_priv->algo_ops->orig.print(bat_priv, seq, hard_iface); |
Simon Wunderlich | cb1c92e | 2013-11-21 11:52:16 +0100 | [diff] [blame] | 1474 | |
| 1475 | out: |
Marek Lindner | 16a4142 | 2014-04-24 03:44:25 +0800 | [diff] [blame] | 1476 | if (hard_iface) |
Sven Eckelmann | 82047ad | 2016-01-17 11:01:10 +0100 | [diff] [blame] | 1477 | batadv_hardif_put(hard_iface); |
Simon Wunderlich | cb1c92e | 2013-11-21 11:52:16 +0100 | [diff] [blame] | 1478 | return 0; |
| 1479 | } |
Sven Eckelmann | dc1cbd1 | 2016-07-16 09:31:20 +0200 | [diff] [blame] | 1480 | #endif |
Simon Wunderlich | cb1c92e | 2013-11-21 11:52:16 +0100 | [diff] [blame] | 1481 | |
Matthias Schiffer | 85cf8c8 | 2016-07-03 13:31:39 +0200 | [diff] [blame] | 1482 | /** |
Sven Eckelmann | 7e9a8c2 | 2017-12-02 19:51:47 +0100 | [diff] [blame] | 1483 | * batadv_orig_dump() - Dump to netlink the originator infos for a specific |
Matthias Schiffer | 85cf8c8 | 2016-07-03 13:31:39 +0200 | [diff] [blame] | 1484 | * outgoing interface |
| 1485 | * @msg: message to dump into |
| 1486 | * @cb: parameters for the dump |
| 1487 | * |
| 1488 | * Return: 0 or error value |
| 1489 | */ |
| 1490 | int batadv_orig_dump(struct sk_buff *msg, struct netlink_callback *cb) |
| 1491 | { |
| 1492 | struct net *net = sock_net(cb->skb->sk); |
| 1493 | struct net_device *soft_iface; |
| 1494 | struct net_device *hard_iface = NULL; |
| 1495 | struct batadv_hard_iface *hardif = BATADV_IF_DEFAULT; |
| 1496 | struct batadv_priv *bat_priv; |
| 1497 | struct batadv_hard_iface *primary_if = NULL; |
| 1498 | int ret; |
| 1499 | int ifindex, hard_ifindex; |
| 1500 | |
| 1501 | ifindex = batadv_netlink_get_ifindex(cb->nlh, BATADV_ATTR_MESH_IFINDEX); |
| 1502 | if (!ifindex) |
| 1503 | return -EINVAL; |
| 1504 | |
| 1505 | soft_iface = dev_get_by_index(net, ifindex); |
| 1506 | if (!soft_iface || !batadv_softif_is_valid(soft_iface)) { |
| 1507 | ret = -ENODEV; |
| 1508 | goto out; |
| 1509 | } |
| 1510 | |
| 1511 | bat_priv = netdev_priv(soft_iface); |
| 1512 | |
| 1513 | primary_if = batadv_primary_if_get_selected(bat_priv); |
| 1514 | if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) { |
| 1515 | ret = -ENOENT; |
| 1516 | goto out; |
| 1517 | } |
| 1518 | |
| 1519 | hard_ifindex = batadv_netlink_get_ifindex(cb->nlh, |
| 1520 | BATADV_ATTR_HARD_IFINDEX); |
| 1521 | if (hard_ifindex) { |
| 1522 | hard_iface = dev_get_by_index(net, hard_ifindex); |
| 1523 | if (hard_iface) |
| 1524 | hardif = batadv_hardif_get_by_netdev(hard_iface); |
| 1525 | |
| 1526 | if (!hardif) { |
| 1527 | ret = -ENODEV; |
| 1528 | goto out; |
| 1529 | } |
| 1530 | |
| 1531 | if (hardif->soft_iface != soft_iface) { |
| 1532 | ret = -ENOENT; |
| 1533 | goto out; |
| 1534 | } |
| 1535 | } |
| 1536 | |
| 1537 | if (!bat_priv->algo_ops->orig.dump) { |
| 1538 | ret = -EOPNOTSUPP; |
| 1539 | goto out; |
| 1540 | } |
| 1541 | |
| 1542 | bat_priv->algo_ops->orig.dump(msg, cb, bat_priv, hardif); |
| 1543 | |
| 1544 | ret = msg->len; |
| 1545 | |
| 1546 | out: |
| 1547 | if (hardif) |
| 1548 | batadv_hardif_put(hardif); |
| 1549 | if (hard_iface) |
| 1550 | dev_put(hard_iface); |
| 1551 | if (primary_if) |
| 1552 | batadv_hardif_put(primary_if); |
| 1553 | if (soft_iface) |
| 1554 | dev_put(soft_iface); |
| 1555 | |
| 1556 | return ret; |
| 1557 | } |
| 1558 | |
Sven Eckelmann | ff15c27 | 2017-12-02 19:51:53 +0100 | [diff] [blame] | 1559 | /** |
| 1560 | * batadv_orig_hash_add_if() - Add interface to originators in orig_hash |
| 1561 | * @hard_iface: hard interface to add (already slave of the soft interface) |
| 1562 | * @max_if_num: new number of interfaces |
| 1563 | * |
| 1564 | * Return: 0 on success or negative error number in case of failure |
| 1565 | */ |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 1566 | int batadv_orig_hash_add_if(struct batadv_hard_iface *hard_iface, |
Sven Eckelmann | f22e089 | 2017-12-26 15:14:01 +0100 | [diff] [blame] | 1567 | unsigned int max_if_num) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1568 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 1569 | struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface); |
Antonio Quartulli | 29824a5 | 2016-05-25 23:27:31 +0800 | [diff] [blame] | 1570 | struct batadv_algo_ops *bao = bat_priv->algo_ops; |
Sven Eckelmann | 5bf74e9 | 2012-06-05 22:31:28 +0200 | [diff] [blame] | 1571 | struct batadv_hashtable *hash = bat_priv->orig_hash; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1572 | struct hlist_head *head; |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 1573 | struct batadv_orig_node *orig_node; |
Sven Eckelmann | 6b5e971 | 2015-05-26 18:34:26 +0200 | [diff] [blame] | 1574 | u32 i; |
Antonio Quartulli | c90681b | 2011-10-05 17:05:25 +0200 | [diff] [blame] | 1575 | int ret; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1576 | |
| 1577 | /* resize all orig nodes because orig_node->bcast_own(_sum) depend on |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 1578 | * if_num |
| 1579 | */ |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1580 | for (i = 0; i < hash->size; i++) { |
| 1581 | head = &hash->table[i]; |
| 1582 | |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame] | 1583 | rcu_read_lock(); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1584 | hlist_for_each_entry_rcu(orig_node, head, hash_entry) { |
Antonio Quartulli | d0015fd | 2013-09-03 11:10:23 +0200 | [diff] [blame] | 1585 | ret = 0; |
Antonio Quartulli | 29824a5 | 2016-05-25 23:27:31 +0800 | [diff] [blame] | 1586 | if (bao->orig.add_if) |
| 1587 | ret = bao->orig.add_if(orig_node, max_if_num); |
Sven Eckelmann | 5346c35 | 2012-05-05 13:27:28 +0200 | [diff] [blame] | 1588 | if (ret == -ENOMEM) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1589 | goto err; |
| 1590 | } |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame] | 1591 | rcu_read_unlock(); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1592 | } |
| 1593 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1594 | return 0; |
| 1595 | |
| 1596 | err: |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame] | 1597 | rcu_read_unlock(); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1598 | return -ENOMEM; |
| 1599 | } |
| 1600 | |
Sven Eckelmann | ff15c27 | 2017-12-02 19:51:53 +0100 | [diff] [blame] | 1601 | /** |
| 1602 | * batadv_orig_hash_del_if() - Remove interface from originators in orig_hash |
| 1603 | * @hard_iface: hard interface to remove (still slave of the soft interface) |
| 1604 | * @max_if_num: new number of interfaces |
| 1605 | * |
| 1606 | * Return: 0 on success or negative error number in case of failure |
| 1607 | */ |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 1608 | int batadv_orig_hash_del_if(struct batadv_hard_iface *hard_iface, |
Sven Eckelmann | f22e089 | 2017-12-26 15:14:01 +0100 | [diff] [blame] | 1609 | unsigned int max_if_num) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1610 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 1611 | struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface); |
Sven Eckelmann | 5bf74e9 | 2012-06-05 22:31:28 +0200 | [diff] [blame] | 1612 | struct batadv_hashtable *hash = bat_priv->orig_hash; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1613 | struct hlist_head *head; |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 1614 | struct batadv_hard_iface *hard_iface_tmp; |
| 1615 | struct batadv_orig_node *orig_node; |
Antonio Quartulli | 29824a5 | 2016-05-25 23:27:31 +0800 | [diff] [blame] | 1616 | struct batadv_algo_ops *bao = bat_priv->algo_ops; |
Sven Eckelmann | 6b5e971 | 2015-05-26 18:34:26 +0200 | [diff] [blame] | 1617 | u32 i; |
Antonio Quartulli | c90681b | 2011-10-05 17:05:25 +0200 | [diff] [blame] | 1618 | int ret; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1619 | |
| 1620 | /* resize all orig nodes because orig_node->bcast_own(_sum) depend on |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 1621 | * if_num |
| 1622 | */ |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1623 | for (i = 0; i < hash->size; i++) { |
| 1624 | head = &hash->table[i]; |
| 1625 | |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame] | 1626 | rcu_read_lock(); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1627 | hlist_for_each_entry_rcu(orig_node, head, hash_entry) { |
Antonio Quartulli | d0015fd | 2013-09-03 11:10:23 +0200 | [diff] [blame] | 1628 | ret = 0; |
Antonio Quartulli | 29824a5 | 2016-05-25 23:27:31 +0800 | [diff] [blame] | 1629 | if (bao->orig.del_if) |
| 1630 | ret = bao->orig.del_if(orig_node, max_if_num, |
| 1631 | hard_iface->if_num); |
Sven Eckelmann | 5346c35 | 2012-05-05 13:27:28 +0200 | [diff] [blame] | 1632 | if (ret == -ENOMEM) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1633 | goto err; |
| 1634 | } |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame] | 1635 | rcu_read_unlock(); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1636 | } |
| 1637 | |
| 1638 | /* renumber remaining batman interfaces _inside_ of orig_hash_lock */ |
| 1639 | rcu_read_lock(); |
Sven Eckelmann | 3193e8f | 2012-05-12 02:09:42 +0200 | [diff] [blame] | 1640 | list_for_each_entry_rcu(hard_iface_tmp, &batadv_hardif_list, list) { |
Sven Eckelmann | e9a4f29 | 2012-06-03 22:19:19 +0200 | [diff] [blame] | 1641 | if (hard_iface_tmp->if_status == BATADV_IF_NOT_IN_USE) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1642 | continue; |
| 1643 | |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 1644 | if (hard_iface == hard_iface_tmp) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1645 | continue; |
| 1646 | |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 1647 | if (hard_iface->soft_iface != hard_iface_tmp->soft_iface) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1648 | continue; |
| 1649 | |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 1650 | if (hard_iface_tmp->if_num > hard_iface->if_num) |
| 1651 | hard_iface_tmp->if_num--; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1652 | } |
| 1653 | rcu_read_unlock(); |
| 1654 | |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 1655 | hard_iface->if_num = -1; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1656 | return 0; |
| 1657 | |
| 1658 | err: |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame] | 1659 | rcu_read_unlock(); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1660 | return -ENOMEM; |
| 1661 | } |