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