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