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