blob: 8ad17ad477e41d765c4dc2bb555309d2bcb9ecf7 [file] [log] [blame]
Sven Eckelmann0046b042016-01-01 00:01:03 +01001/* Copyright (C) 2009-2016 B.A.T.M.A.N. contributors:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002 *
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 Quartulliebf38fb2013-11-03 20:40:48 +010015 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000016 */
17
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000018#include "originator.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020019#include "main.h"
20
Sven Eckelmann7c124392016-01-16 10:29:56 +010021#include <linux/atomic.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020022#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 Eckelmann90f564d2016-01-16 10:29:40 +010027#include <linux/kref.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020028#include <linux/list.h>
29#include <linux/lockdep.h>
30#include <linux/netdevice.h>
Marek Lindnerd0fa4f32015-06-22 00:30:22 +080031#include <linux/rculist.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020032#include <linux/seq_file.h>
33#include <linux/slab.h>
34#include <linux/spinlock.h>
35#include <linux/workqueue.h>
36
Sven Eckelmann01d350d2016-05-15 11:07:44 +020037#include "bat_algo.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020038#include "distributed-arp-table.h"
39#include "fragmentation.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000040#include "gateway_client.h"
41#include "hard-interface.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020042#include "hash.h"
Sven Eckelmannba412082016-05-15 23:48:31 +020043#include "log.h"
Linus Lüssing60432d72014-02-15 17:47:51 +010044#include "multicast.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020045#include "network-coding.h"
46#include "routing.h"
47#include "translation-table.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000048
Antonio Quartullidec05072012-11-10 11:00:32 +010049/* hash class keys */
50static struct lock_class_key batadv_orig_hash_lock_class_key;
51
Sven Eckelmann03fc7f82012-05-12 18:34:00 +020052static void batadv_purge_orig(struct work_struct *work);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000053
Sven Eckelmann62fe7102015-09-15 19:00:48 +020054/**
Sven Eckelmann7afcbbe2015-10-31 12:29:29 +010055 * batadv_compare_orig - comparing function used in the originator hash table
56 * @node: node in the local table
57 * @data2: second object to compare the node to
Sven Eckelmann62fe7102015-09-15 19:00:48 +020058 *
Sven Eckelmann4b426b12016-02-22 21:02:39 +010059 * Return: true if they are the same originator
Sven Eckelmann62fe7102015-09-15 19:00:48 +020060 */
Sven Eckelmann4b426b12016-02-22 21:02:39 +010061bool batadv_compare_orig(const struct hlist_node *node, const void *data2)
Sven Eckelmannb8e2dd12011-06-15 15:08:59 +020062{
Sven Eckelmann56303d32012-06-05 22:31:31 +020063 const void *data1 = container_of(node, struct batadv_orig_node,
64 hash_entry);
Sven Eckelmannb8e2dd12011-06-15 15:08:59 +020065
dingtianhong323813e2013-12-26 19:40:39 +080066 return batadv_compare_eth(data1, data2);
Sven Eckelmannb8e2dd12011-06-15 15:08:59 +020067}
68
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +020069/**
70 * batadv_orig_node_vlan_get - get an orig_node_vlan object
71 * @orig_node: the originator serving the VLAN
72 * @vid: the VLAN identifier
73 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +020074 * Return: the vlan object identified by vid and belonging to orig_node or NULL
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +020075 * if it does not exist.
76 */
77struct batadv_orig_node_vlan *
78batadv_orig_node_vlan_get(struct batadv_orig_node *orig_node,
79 unsigned short vid)
80{
81 struct batadv_orig_node_vlan *vlan = NULL, *tmp;
82
83 rcu_read_lock();
Marek Lindnerd0fa4f32015-06-22 00:30:22 +080084 hlist_for_each_entry_rcu(tmp, &orig_node->vlan_list, list) {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +020085 if (tmp->vid != vid)
86 continue;
87
Sven Eckelmann161a3be2016-01-16 10:29:55 +010088 if (!kref_get_unless_zero(&tmp->refcount))
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +020089 continue;
90
91 vlan = tmp;
92
93 break;
94 }
95 rcu_read_unlock();
96
97 return vlan;
98}
99
100/**
101 * batadv_orig_node_vlan_new - search and possibly create an orig_node_vlan
102 * object
103 * @orig_node: the originator serving the VLAN
104 * @vid: the VLAN identifier
105 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200106 * Return: NULL in case of failure or the vlan object identified by vid and
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200107 * belonging to orig_node otherwise. The object is created and added to the list
108 * if it does not exist.
109 *
110 * The object is returned with refcounter increased by 1.
111 */
112struct batadv_orig_node_vlan *
113batadv_orig_node_vlan_new(struct batadv_orig_node *orig_node,
114 unsigned short vid)
115{
116 struct batadv_orig_node_vlan *vlan;
117
118 spin_lock_bh(&orig_node->vlan_list_lock);
119
120 /* first look if an object for this vid already exists */
121 vlan = batadv_orig_node_vlan_get(orig_node, vid);
122 if (vlan)
123 goto out;
124
125 vlan = kzalloc(sizeof(*vlan), GFP_ATOMIC);
126 if (!vlan)
127 goto out;
128
Sven Eckelmann161a3be2016-01-16 10:29:55 +0100129 kref_init(&vlan->refcount);
130 kref_get(&vlan->refcount);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200131 vlan->vid = vid;
132
Marek Lindnerd0fa4f32015-06-22 00:30:22 +0800133 hlist_add_head_rcu(&vlan->list, &orig_node->vlan_list);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200134
135out:
136 spin_unlock_bh(&orig_node->vlan_list_lock);
137
138 return vlan;
139}
140
141/**
Sven Eckelmann161a3be2016-01-16 10:29:55 +0100142 * batadv_orig_node_vlan_release - release originator-vlan object from lists
143 * and queue for free after rcu grace period
144 * @ref: kref pointer of the originator-vlan object
145 */
146static void batadv_orig_node_vlan_release(struct kref *ref)
147{
148 struct batadv_orig_node_vlan *orig_vlan;
149
150 orig_vlan = container_of(ref, struct batadv_orig_node_vlan, refcount);
151
152 kfree_rcu(orig_vlan, rcu);
153}
154
155/**
Sven Eckelmann21754e22016-01-17 11:01:24 +0100156 * batadv_orig_node_vlan_put - decrement the refcounter and possibly release
157 * the originator-vlan object
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200158 * @orig_vlan: the originator-vlan object to release
159 */
Sven Eckelmann21754e22016-01-17 11:01:24 +0100160void batadv_orig_node_vlan_put(struct batadv_orig_node_vlan *orig_vlan)
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200161{
Sven Eckelmann161a3be2016-01-16 10:29:55 +0100162 kref_put(&orig_vlan->refcount, batadv_orig_node_vlan_release);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200163}
164
Sven Eckelmann56303d32012-06-05 22:31:31 +0200165int batadv_originator_init(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000166{
167 if (bat_priv->orig_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200168 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000169
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +0200170 bat_priv->orig_hash = batadv_hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000171
172 if (!bat_priv->orig_hash)
173 goto err;
174
Antonio Quartullidec05072012-11-10 11:00:32 +0100175 batadv_hash_set_lock_class(bat_priv->orig_hash,
176 &batadv_orig_hash_lock_class_key);
177
Antonio Quartulli72414442012-12-25 13:14:37 +0100178 INIT_DELAYED_WORK(&bat_priv->orig_work, batadv_purge_orig);
179 queue_delayed_work(batadv_event_workqueue,
180 &bat_priv->orig_work,
181 msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD));
182
Sven Eckelmann5346c352012-05-05 13:27:28 +0200183 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000184
185err:
Sven Eckelmann5346c352012-05-05 13:27:28 +0200186 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000187}
188
Simon Wunderlich89652332013-11-13 19:14:46 +0100189/**
Sven Eckelmannae3e1e32016-01-05 12:06:24 +0100190 * batadv_neigh_ifinfo_release - release neigh_ifinfo from lists and queue for
191 * free after rcu grace period
Sven Eckelmann962c6832016-01-16 10:29:51 +0100192 * @ref: kref pointer of the neigh_ifinfo
Simon Wunderlich89652332013-11-13 19:14:46 +0100193 */
Sven Eckelmann962c6832016-01-16 10:29:51 +0100194static void batadv_neigh_ifinfo_release(struct kref *ref)
Simon Wunderlich89652332013-11-13 19:14:46 +0100195{
Sven Eckelmann962c6832016-01-16 10:29:51 +0100196 struct batadv_neigh_ifinfo *neigh_ifinfo;
197
198 neigh_ifinfo = container_of(ref, struct batadv_neigh_ifinfo, refcount);
199
Sven Eckelmannae3e1e32016-01-05 12:06:24 +0100200 if (neigh_ifinfo->if_outgoing != BATADV_IF_DEFAULT)
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100201 batadv_hardif_put(neigh_ifinfo->if_outgoing);
Sven Eckelmannae3e1e32016-01-05 12:06:24 +0100202
203 kfree_rcu(neigh_ifinfo, rcu);
Simon Wunderlich89652332013-11-13 19:14:46 +0100204}
205
206/**
Sven Eckelmann044fa3a2016-01-17 11:01:12 +0100207 * batadv_neigh_ifinfo_put - decrement the refcounter and possibly release
Simon Wunderlich89652332013-11-13 19:14:46 +0100208 * the neigh_ifinfo
209 * @neigh_ifinfo: the neigh_ifinfo object to release
210 */
Sven Eckelmann044fa3a2016-01-17 11:01:12 +0100211void batadv_neigh_ifinfo_put(struct batadv_neigh_ifinfo *neigh_ifinfo)
Simon Wunderlich89652332013-11-13 19:14:46 +0100212{
Sven Eckelmann962c6832016-01-16 10:29:51 +0100213 kref_put(&neigh_ifinfo->refcount, batadv_neigh_ifinfo_release);
Simon Wunderlich89652332013-11-13 19:14:46 +0100214}
215
216/**
Sven Eckelmannf6389692016-01-05 12:06:23 +0100217 * batadv_hardif_neigh_release - release hardif neigh node from lists and
218 * queue for free after rcu grace period
Sven Eckelmann90f564d2016-01-16 10:29:40 +0100219 * @ref: kref pointer of the neigh_node
Marek Lindnercef63412015-08-04 21:09:55 +0800220 */
Sven Eckelmann90f564d2016-01-16 10:29:40 +0100221static void batadv_hardif_neigh_release(struct kref *ref)
Marek Lindnercef63412015-08-04 21:09:55 +0800222{
Sven Eckelmann90f564d2016-01-16 10:29:40 +0100223 struct batadv_hardif_neigh_node *hardif_neigh;
224
225 hardif_neigh = container_of(ref, struct batadv_hardif_neigh_node,
226 refcount);
227
Sven Eckelmannf6389692016-01-05 12:06:23 +0100228 spin_lock_bh(&hardif_neigh->if_incoming->neigh_list_lock);
229 hlist_del_init_rcu(&hardif_neigh->list);
230 spin_unlock_bh(&hardif_neigh->if_incoming->neigh_list_lock);
Sven Eckelmannbab7c6c2016-01-05 12:06:17 +0100231
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100232 batadv_hardif_put(hardif_neigh->if_incoming);
Sven Eckelmannf6389692016-01-05 12:06:23 +0100233 kfree_rcu(hardif_neigh, rcu);
Marek Lindnercef63412015-08-04 21:09:55 +0800234}
235
236/**
Sven Eckelmannaccadc32016-01-17 11:01:14 +0100237 * batadv_hardif_neigh_put - decrement the hardif neighbors refcounter
Sven Eckelmannf6389692016-01-05 12:06:23 +0100238 * and possibly release it
Marek Lindnercef63412015-08-04 21:09:55 +0800239 * @hardif_neigh: hardif neigh neighbor to free
240 */
Sven Eckelmannaccadc32016-01-17 11:01:14 +0100241void batadv_hardif_neigh_put(struct batadv_hardif_neigh_node *hardif_neigh)
Marek Lindnercef63412015-08-04 21:09:55 +0800242{
Sven Eckelmann90f564d2016-01-16 10:29:40 +0100243 kref_put(&hardif_neigh->refcount, batadv_hardif_neigh_release);
Marek Lindnercef63412015-08-04 21:09:55 +0800244}
245
246/**
Sven Eckelmannb4d922c2016-01-05 12:06:25 +0100247 * batadv_neigh_node_release - release neigh_node from lists and queue for
248 * free after rcu grace period
Sven Eckelmann77ae32e2016-01-16 10:29:53 +0100249 * @ref: kref pointer of the neigh_node
Simon Wunderlich89652332013-11-13 19:14:46 +0100250 */
Sven Eckelmann77ae32e2016-01-16 10:29:53 +0100251static void batadv_neigh_node_release(struct kref *ref)
Simon Wunderlich89652332013-11-13 19:14:46 +0100252{
253 struct hlist_node *node_tmp;
Sven Eckelmann77ae32e2016-01-16 10:29:53 +0100254 struct batadv_neigh_node *neigh_node;
Simon Wunderlich89652332013-11-13 19:14:46 +0100255 struct batadv_neigh_ifinfo *neigh_ifinfo;
256
Sven Eckelmann77ae32e2016-01-16 10:29:53 +0100257 neigh_node = container_of(ref, struct batadv_neigh_node, refcount);
Simon Wunderlich89652332013-11-13 19:14:46 +0100258
259 hlist_for_each_entry_safe(neigh_ifinfo, node_tmp,
260 &neigh_node->ifinfo_list, list) {
Sven Eckelmann044fa3a2016-01-17 11:01:12 +0100261 batadv_neigh_ifinfo_put(neigh_ifinfo);
Simon Wunderlich89652332013-11-13 19:14:46 +0100262 }
Antonio Quartullibcef1f32015-03-01 00:50:17 +0800263
Sven Eckelmannabe59c62016-03-11 16:44:06 +0100264 batadv_hardif_neigh_put(neigh_node->hardif_neigh);
Marek Lindnercef63412015-08-04 21:09:55 +0800265
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100266 batadv_hardif_put(neigh_node->if_incoming);
Simon Wunderlich89652332013-11-13 19:14:46 +0100267
Sven Eckelmannb4d922c2016-01-05 12:06:25 +0100268 kfree_rcu(neigh_node, rcu);
Simon Wunderlich89652332013-11-13 19:14:46 +0100269}
270
271/**
Sven Eckelmann25bb2502016-01-17 11:01:11 +0100272 * batadv_neigh_node_put - decrement the neighbors refcounter and possibly
Sven Eckelmann77ae32e2016-01-16 10:29:53 +0100273 * release it
Simon Wunderlich89652332013-11-13 19:14:46 +0100274 * @neigh_node: neigh neighbor to free
275 */
Sven Eckelmann25bb2502016-01-17 11:01:11 +0100276void batadv_neigh_node_put(struct batadv_neigh_node *neigh_node)
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000277{
Sven Eckelmann77ae32e2016-01-16 10:29:53 +0100278 kref_put(&neigh_node->refcount, batadv_neigh_node_release);
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000279}
280
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100281/**
Antonio Quartulli6d030de2016-03-11 16:36:19 +0100282 * batadv_orig_router_get - router to the originator depending on iface
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100283 * @orig_node: the orig node for the router
284 * @if_outgoing: the interface where the payload packet has been received or
285 * the OGM should be sent to
286 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200287 * Return: the neighbor which should be router for this orig_node/iface.
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100288 *
289 * The object is returned with refcounter increased by 1.
290 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200291struct batadv_neigh_node *
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100292batadv_orig_router_get(struct batadv_orig_node *orig_node,
293 const struct batadv_hard_iface *if_outgoing)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000294{
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100295 struct batadv_orig_ifinfo *orig_ifinfo;
296 struct batadv_neigh_node *router = NULL;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000297
298 rcu_read_lock();
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100299 hlist_for_each_entry_rcu(orig_ifinfo, &orig_node->ifinfo_list, list) {
300 if (orig_ifinfo->if_outgoing != if_outgoing)
301 continue;
302
303 router = rcu_dereference(orig_ifinfo->router);
304 break;
305 }
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000306
Sven Eckelmann77ae32e2016-01-16 10:29:53 +0100307 if (router && !kref_get_unless_zero(&router->refcount))
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000308 router = NULL;
309
310 rcu_read_unlock();
311 return router;
312}
313
Antonio Quartulli0538f752013-09-02 12:15:01 +0200314/**
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100315 * batadv_orig_ifinfo_get - find the ifinfo from an orig_node
316 * @orig_node: the orig node to be queried
317 * @if_outgoing: the interface for which the ifinfo should be acquired
318 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200319 * Return: the requested orig_ifinfo or NULL if not found.
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100320 *
321 * The object is returned with refcounter increased by 1.
322 */
323struct batadv_orig_ifinfo *
324batadv_orig_ifinfo_get(struct batadv_orig_node *orig_node,
325 struct batadv_hard_iface *if_outgoing)
326{
327 struct batadv_orig_ifinfo *tmp, *orig_ifinfo = NULL;
328
329 rcu_read_lock();
330 hlist_for_each_entry_rcu(tmp, &orig_node->ifinfo_list,
331 list) {
332 if (tmp->if_outgoing != if_outgoing)
333 continue;
334
Sven Eckelmanna6ba0d32016-01-16 10:29:52 +0100335 if (!kref_get_unless_zero(&tmp->refcount))
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100336 continue;
337
338 orig_ifinfo = tmp;
339 break;
340 }
341 rcu_read_unlock();
342
343 return orig_ifinfo;
344}
345
346/**
347 * batadv_orig_ifinfo_new - search and possibly create an orig_ifinfo object
348 * @orig_node: the orig node to be queried
349 * @if_outgoing: the interface for which the ifinfo should be acquired
350 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200351 * Return: NULL in case of failure or the orig_ifinfo object for the if_outgoing
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100352 * interface otherwise. The object is created and added to the list
353 * if it does not exist.
354 *
355 * The object is returned with refcounter increased by 1.
356 */
357struct batadv_orig_ifinfo *
358batadv_orig_ifinfo_new(struct batadv_orig_node *orig_node,
359 struct batadv_hard_iface *if_outgoing)
360{
361 struct batadv_orig_ifinfo *orig_ifinfo = NULL;
362 unsigned long reset_time;
363
364 spin_lock_bh(&orig_node->neigh_list_lock);
365
366 orig_ifinfo = batadv_orig_ifinfo_get(orig_node, if_outgoing);
367 if (orig_ifinfo)
368 goto out;
369
370 orig_ifinfo = kzalloc(sizeof(*orig_ifinfo), GFP_ATOMIC);
371 if (!orig_ifinfo)
372 goto out;
373
Sven Eckelmann17a86912016-04-11 13:06:40 +0200374 if (if_outgoing != BATADV_IF_DEFAULT)
375 kref_get(&if_outgoing->refcount);
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100376
377 reset_time = jiffies - 1;
378 reset_time -= msecs_to_jiffies(BATADV_RESET_PROTECTION_MS);
379 orig_ifinfo->batman_seqno_reset = reset_time;
380 orig_ifinfo->if_outgoing = if_outgoing;
381 INIT_HLIST_NODE(&orig_ifinfo->list);
Sven Eckelmanna6ba0d32016-01-16 10:29:52 +0100382 kref_init(&orig_ifinfo->refcount);
383 kref_get(&orig_ifinfo->refcount);
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100384 hlist_add_head_rcu(&orig_ifinfo->list,
385 &orig_node->ifinfo_list);
386out:
387 spin_unlock_bh(&orig_node->neigh_list_lock);
388 return orig_ifinfo;
389}
390
391/**
Simon Wunderlich89652332013-11-13 19:14:46 +0100392 * batadv_neigh_ifinfo_get - find the ifinfo from an neigh_node
Sven Eckelmanne51f0392015-09-06 21:38:51 +0200393 * @neigh: the neigh node to be queried
Simon Wunderlich89652332013-11-13 19:14:46 +0100394 * @if_outgoing: the interface for which the ifinfo should be acquired
395 *
396 * The object is returned with refcounter increased by 1.
397 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200398 * Return: the requested neigh_ifinfo or NULL if not found
Simon Wunderlich89652332013-11-13 19:14:46 +0100399 */
400struct batadv_neigh_ifinfo *
401batadv_neigh_ifinfo_get(struct batadv_neigh_node *neigh,
402 struct batadv_hard_iface *if_outgoing)
403{
404 struct batadv_neigh_ifinfo *neigh_ifinfo = NULL,
405 *tmp_neigh_ifinfo;
406
407 rcu_read_lock();
408 hlist_for_each_entry_rcu(tmp_neigh_ifinfo, &neigh->ifinfo_list,
409 list) {
410 if (tmp_neigh_ifinfo->if_outgoing != if_outgoing)
411 continue;
412
Sven Eckelmann962c6832016-01-16 10:29:51 +0100413 if (!kref_get_unless_zero(&tmp_neigh_ifinfo->refcount))
Simon Wunderlich89652332013-11-13 19:14:46 +0100414 continue;
415
416 neigh_ifinfo = tmp_neigh_ifinfo;
417 break;
418 }
419 rcu_read_unlock();
420
421 return neigh_ifinfo;
422}
423
424/**
425 * batadv_neigh_ifinfo_new - search and possibly create an neigh_ifinfo object
Sven Eckelmanne51f0392015-09-06 21:38:51 +0200426 * @neigh: the neigh node to be queried
Simon Wunderlich89652332013-11-13 19:14:46 +0100427 * @if_outgoing: the interface for which the ifinfo should be acquired
428 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200429 * Return: NULL in case of failure or the neigh_ifinfo object for the
Simon Wunderlich89652332013-11-13 19:14:46 +0100430 * if_outgoing interface otherwise. The object is created and added to the list
431 * if it does not exist.
432 *
433 * The object is returned with refcounter increased by 1.
434 */
435struct batadv_neigh_ifinfo *
436batadv_neigh_ifinfo_new(struct batadv_neigh_node *neigh,
437 struct batadv_hard_iface *if_outgoing)
438{
439 struct batadv_neigh_ifinfo *neigh_ifinfo;
440
441 spin_lock_bh(&neigh->ifinfo_lock);
442
443 neigh_ifinfo = batadv_neigh_ifinfo_get(neigh, if_outgoing);
444 if (neigh_ifinfo)
445 goto out;
446
447 neigh_ifinfo = kzalloc(sizeof(*neigh_ifinfo), GFP_ATOMIC);
448 if (!neigh_ifinfo)
449 goto out;
450
Sven Eckelmann17a86912016-04-11 13:06:40 +0200451 if (if_outgoing)
452 kref_get(&if_outgoing->refcount);
Simon Wunderlich89652332013-11-13 19:14:46 +0100453
454 INIT_HLIST_NODE(&neigh_ifinfo->list);
Sven Eckelmann962c6832016-01-16 10:29:51 +0100455 kref_init(&neigh_ifinfo->refcount);
456 kref_get(&neigh_ifinfo->refcount);
Simon Wunderlich89652332013-11-13 19:14:46 +0100457 neigh_ifinfo->if_outgoing = if_outgoing;
458
459 hlist_add_head_rcu(&neigh_ifinfo->list, &neigh->ifinfo_list);
460
461out:
462 spin_unlock_bh(&neigh->ifinfo_lock);
463
464 return neigh_ifinfo;
465}
466
467/**
Marek Lindnered292662015-08-04 23:31:44 +0800468 * batadv_neigh_node_get - retrieve a neighbour from the list
469 * @orig_node: originator which the neighbour belongs to
470 * @hard_iface: the interface where this neighbour is connected to
471 * @addr: the address of the neighbour
472 *
473 * Looks for and possibly returns a neighbour belonging to this originator list
474 * which is connected through the provided hard interface.
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200475 *
476 * Return: neighbor when found. Othwerwise NULL
Marek Lindnered292662015-08-04 23:31:44 +0800477 */
478static struct batadv_neigh_node *
479batadv_neigh_node_get(const struct batadv_orig_node *orig_node,
480 const struct batadv_hard_iface *hard_iface,
481 const u8 *addr)
482{
483 struct batadv_neigh_node *tmp_neigh_node, *res = NULL;
484
485 rcu_read_lock();
486 hlist_for_each_entry_rcu(tmp_neigh_node, &orig_node->neigh_list, list) {
487 if (!batadv_compare_eth(tmp_neigh_node->addr, addr))
488 continue;
489
490 if (tmp_neigh_node->if_incoming != hard_iface)
491 continue;
492
Sven Eckelmann77ae32e2016-01-16 10:29:53 +0100493 if (!kref_get_unless_zero(&tmp_neigh_node->refcount))
Marek Lindnered292662015-08-04 23:31:44 +0800494 continue;
495
496 res = tmp_neigh_node;
497 break;
498 }
499 rcu_read_unlock();
500
501 return res;
502}
503
504/**
Marek Lindnercef63412015-08-04 21:09:55 +0800505 * batadv_hardif_neigh_create - create a hardif neighbour node
506 * @hard_iface: the interface this neighbour is connected to
507 * @neigh_addr: the interface address of the neighbour to retrieve
508 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200509 * Return: the hardif neighbour node if found or created or NULL otherwise.
Marek Lindnercef63412015-08-04 21:09:55 +0800510 */
511static struct batadv_hardif_neigh_node *
512batadv_hardif_neigh_create(struct batadv_hard_iface *hard_iface,
513 const u8 *neigh_addr)
514{
Marek Lindner8248a4c2015-08-04 21:09:56 +0800515 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
Marek Lindnercef63412015-08-04 21:09:55 +0800516 struct batadv_hardif_neigh_node *hardif_neigh = NULL;
517
518 spin_lock_bh(&hard_iface->neigh_list_lock);
519
520 /* check if neighbor hasn't been added in the meantime */
521 hardif_neigh = batadv_hardif_neigh_get(hard_iface, neigh_addr);
522 if (hardif_neigh)
523 goto out;
524
Marek Lindnercef63412015-08-04 21:09:55 +0800525 hardif_neigh = kzalloc(sizeof(*hardif_neigh), GFP_ATOMIC);
Sven Eckelmann17a86912016-04-11 13:06:40 +0200526 if (!hardif_neigh)
Marek Lindnercef63412015-08-04 21:09:55 +0800527 goto out;
Marek Lindnercef63412015-08-04 21:09:55 +0800528
Sven Eckelmann17a86912016-04-11 13:06:40 +0200529 kref_get(&hard_iface->refcount);
Marek Lindnercef63412015-08-04 21:09:55 +0800530 INIT_HLIST_NODE(&hardif_neigh->list);
531 ether_addr_copy(hardif_neigh->addr, neigh_addr);
532 hardif_neigh->if_incoming = hard_iface;
533 hardif_neigh->last_seen = jiffies;
534
Sven Eckelmann90f564d2016-01-16 10:29:40 +0100535 kref_init(&hardif_neigh->refcount);
Marek Lindnercef63412015-08-04 21:09:55 +0800536
Marek Lindner8248a4c2015-08-04 21:09:56 +0800537 if (bat_priv->bat_algo_ops->bat_hardif_neigh_init)
538 bat_priv->bat_algo_ops->bat_hardif_neigh_init(hardif_neigh);
539
Marek Lindnercef63412015-08-04 21:09:55 +0800540 hlist_add_head(&hardif_neigh->list, &hard_iface->neigh_list);
541
542out:
543 spin_unlock_bh(&hard_iface->neigh_list_lock);
544 return hardif_neigh;
545}
546
547/**
548 * batadv_hardif_neigh_get_or_create - retrieve or create a hardif neighbour
549 * node
550 * @hard_iface: the interface this neighbour is connected to
551 * @neigh_addr: the interface address of the neighbour to retrieve
552 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200553 * Return: the hardif neighbour node if found or created or NULL otherwise.
Marek Lindnercef63412015-08-04 21:09:55 +0800554 */
555static struct batadv_hardif_neigh_node *
556batadv_hardif_neigh_get_or_create(struct batadv_hard_iface *hard_iface,
557 const u8 *neigh_addr)
558{
559 struct batadv_hardif_neigh_node *hardif_neigh = NULL;
560
561 /* first check without locking to avoid the overhead */
562 hardif_neigh = batadv_hardif_neigh_get(hard_iface, neigh_addr);
563 if (hardif_neigh)
564 return hardif_neigh;
565
566 return batadv_hardif_neigh_create(hard_iface, neigh_addr);
567}
568
569/**
570 * batadv_hardif_neigh_get - retrieve a hardif neighbour from the list
571 * @hard_iface: the interface where this neighbour is connected to
572 * @neigh_addr: the address of the neighbour
573 *
574 * Looks for and possibly returns a neighbour belonging to this hard interface.
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200575 *
576 * Return: neighbor when found. Othwerwise NULL
Marek Lindnercef63412015-08-04 21:09:55 +0800577 */
578struct batadv_hardif_neigh_node *
579batadv_hardif_neigh_get(const struct batadv_hard_iface *hard_iface,
580 const u8 *neigh_addr)
581{
582 struct batadv_hardif_neigh_node *tmp_hardif_neigh, *hardif_neigh = NULL;
583
584 rcu_read_lock();
585 hlist_for_each_entry_rcu(tmp_hardif_neigh,
586 &hard_iface->neigh_list, list) {
587 if (!batadv_compare_eth(tmp_hardif_neigh->addr, neigh_addr))
588 continue;
589
Sven Eckelmann90f564d2016-01-16 10:29:40 +0100590 if (!kref_get_unless_zero(&tmp_hardif_neigh->refcount))
Marek Lindnercef63412015-08-04 21:09:55 +0800591 continue;
592
593 hardif_neigh = tmp_hardif_neigh;
594 break;
595 }
596 rcu_read_unlock();
597
598 return hardif_neigh;
599}
600
601/**
Marek Lindner6f0a6b52016-05-03 01:52:08 +0800602 * batadv_neigh_node_create - create a neigh node object
Marek Lindner3f32f8a2015-07-26 04:59:15 +0800603 * @orig_node: originator object representing the neighbour
Antonio Quartulli0538f752013-09-02 12:15:01 +0200604 * @hard_iface: the interface where the neighbour is connected to
605 * @neigh_addr: the mac address of the neighbour interface
Antonio Quartulli0538f752013-09-02 12:15:01 +0200606 *
607 * Allocates a new neigh_node object and initialises all the generic fields.
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200608 *
Marek Lindner6f0a6b52016-05-03 01:52:08 +0800609 * Return: the neighbour node if found or created or NULL otherwise.
Antonio Quartulli0538f752013-09-02 12:15:01 +0200610 */
Marek Lindner6f0a6b52016-05-03 01:52:08 +0800611static struct batadv_neigh_node *
612batadv_neigh_node_create(struct batadv_orig_node *orig_node,
613 struct batadv_hard_iface *hard_iface,
614 const u8 *neigh_addr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000615{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200616 struct batadv_neigh_node *neigh_node;
Marek Lindnercef63412015-08-04 21:09:55 +0800617 struct batadv_hardif_neigh_node *hardif_neigh = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000618
Linus Lüssinge1237052016-01-07 08:11:12 +0100619 spin_lock_bh(&orig_node->neigh_list_lock);
620
Marek Lindner741aa062015-07-26 04:57:43 +0800621 neigh_node = batadv_neigh_node_get(orig_node, hard_iface, neigh_addr);
622 if (neigh_node)
623 goto out;
624
Marek Lindnercef63412015-08-04 21:09:55 +0800625 hardif_neigh = batadv_hardif_neigh_get_or_create(hard_iface,
626 neigh_addr);
627 if (!hardif_neigh)
628 goto out;
629
Sven Eckelmann704509b2011-05-14 23:14:54 +0200630 neigh_node = kzalloc(sizeof(*neigh_node), GFP_ATOMIC);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000631 if (!neigh_node)
Marek Lindner7ae8b282012-03-01 15:35:21 +0800632 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000633
Marek Lindner9591a792010-12-12 21:57:11 +0000634 INIT_HLIST_NODE(&neigh_node->list);
Simon Wunderlich89652332013-11-13 19:14:46 +0100635 INIT_HLIST_HEAD(&neigh_node->ifinfo_list);
636 spin_lock_init(&neigh_node->ifinfo_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000637
Sven Eckelmann17a86912016-04-11 13:06:40 +0200638 kref_get(&hard_iface->refcount);
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100639 ether_addr_copy(neigh_node->addr, neigh_addr);
Antonio Quartulli0538f752013-09-02 12:15:01 +0200640 neigh_node->if_incoming = hard_iface;
641 neigh_node->orig_node = orig_node;
Marek Lindnere48474e2016-03-11 16:01:09 +0100642 neigh_node->last_seen = jiffies;
Antonio Quartulli0538f752013-09-02 12:15:01 +0200643
Sven Eckelmannabe59c62016-03-11 16:44:06 +0100644 /* increment unique neighbor refcount */
645 kref_get(&hardif_neigh->refcount);
646 neigh_node->hardif_neigh = hardif_neigh;
647
Marek Lindner1605d0d2011-02-18 12:28:11 +0000648 /* extra reference for return */
Sven Eckelmann77ae32e2016-01-16 10:29:53 +0100649 kref_init(&neigh_node->refcount);
650 kref_get(&neigh_node->refcount);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000651
Marek Lindner741aa062015-07-26 04:57:43 +0800652 hlist_add_head_rcu(&neigh_node->list, &orig_node->neigh_list);
Marek Lindner741aa062015-07-26 04:57:43 +0800653
654 batadv_dbg(BATADV_DBG_BATMAN, orig_node->bat_priv,
655 "Creating new neighbor %pM for orig_node %pM on interface %s\n",
656 neigh_addr, orig_node->orig, hard_iface->net_dev->name);
657
Marek Lindner7ae8b282012-03-01 15:35:21 +0800658out:
Linus Lüssinge1237052016-01-07 08:11:12 +0100659 spin_unlock_bh(&orig_node->neigh_list_lock);
660
Marek Lindnercef63412015-08-04 21:09:55 +0800661 if (hardif_neigh)
Sven Eckelmannaccadc32016-01-17 11:01:14 +0100662 batadv_hardif_neigh_put(hardif_neigh);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000663 return neigh_node;
664}
665
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100666/**
Marek Lindner6f0a6b52016-05-03 01:52:08 +0800667 * batadv_neigh_node_get_or_create - retrieve or create a neigh node object
668 * @orig_node: originator object representing the neighbour
669 * @hard_iface: the interface where the neighbour is connected to
670 * @neigh_addr: the mac address of the neighbour interface
671 *
672 * Return: the neighbour node if found or created or NULL otherwise.
673 */
674struct batadv_neigh_node *
675batadv_neigh_node_get_or_create(struct batadv_orig_node *orig_node,
676 struct batadv_hard_iface *hard_iface,
677 const u8 *neigh_addr)
678{
679 struct batadv_neigh_node *neigh_node = NULL;
680
681 /* first check without locking to avoid the overhead */
682 neigh_node = batadv_neigh_node_get(orig_node, hard_iface, neigh_addr);
683 if (neigh_node)
684 return neigh_node;
685
686 return batadv_neigh_node_create(orig_node, hard_iface, neigh_addr);
687}
688
689/**
Marek Lindner75874052015-08-04 21:09:57 +0800690 * batadv_hardif_neigh_seq_print_text - print the single hop neighbour list
691 * @seq: neighbour table seq_file struct
692 * @offset: not used
693 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200694 * Return: always 0
Marek Lindner75874052015-08-04 21:09:57 +0800695 */
696int batadv_hardif_neigh_seq_print_text(struct seq_file *seq, void *offset)
697{
698 struct net_device *net_dev = (struct net_device *)seq->private;
699 struct batadv_priv *bat_priv = netdev_priv(net_dev);
700 struct batadv_hard_iface *primary_if;
701
702 primary_if = batadv_seq_print_text_primary_if_get(seq);
703 if (!primary_if)
704 return 0;
705
706 seq_printf(seq, "[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s %s)]\n",
707 BATADV_SOURCE_VERSION, primary_if->net_dev->name,
708 primary_if->net_dev->dev_addr, net_dev->name,
709 bat_priv->bat_algo_ops->name);
710
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100711 batadv_hardif_put(primary_if);
Marek Lindner75874052015-08-04 21:09:57 +0800712
713 if (!bat_priv->bat_algo_ops->bat_neigh_print) {
714 seq_puts(seq,
715 "No printing function for this routing protocol\n");
716 return 0;
717 }
718
719 bat_priv->bat_algo_ops->bat_neigh_print(bat_priv, seq);
720 return 0;
721}
722
723/**
Sven Eckelmann2baa7532016-01-05 12:06:22 +0100724 * batadv_orig_ifinfo_release - release orig_ifinfo from lists and queue for
725 * free after rcu grace period
Sven Eckelmanna6ba0d32016-01-16 10:29:52 +0100726 * @ref: kref pointer of the orig_ifinfo
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100727 */
Sven Eckelmanna6ba0d32016-01-16 10:29:52 +0100728static void batadv_orig_ifinfo_release(struct kref *ref)
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100729{
Sven Eckelmanna6ba0d32016-01-16 10:29:52 +0100730 struct batadv_orig_ifinfo *orig_ifinfo;
Simon Wunderlich000c8df2014-03-26 15:46:22 +0100731 struct batadv_neigh_node *router;
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100732
Sven Eckelmanna6ba0d32016-01-16 10:29:52 +0100733 orig_ifinfo = container_of(ref, struct batadv_orig_ifinfo, refcount);
734
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100735 if (orig_ifinfo->if_outgoing != BATADV_IF_DEFAULT)
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100736 batadv_hardif_put(orig_ifinfo->if_outgoing);
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100737
Simon Wunderlich000c8df2014-03-26 15:46:22 +0100738 /* this is the last reference to this object */
739 router = rcu_dereference_protected(orig_ifinfo->router, true);
740 if (router)
Sven Eckelmann25bb2502016-01-17 11:01:11 +0100741 batadv_neigh_node_put(router);
Sven Eckelmann2baa7532016-01-05 12:06:22 +0100742
743 kfree_rcu(orig_ifinfo, rcu);
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100744}
745
746/**
Sven Eckelmann35f94772016-01-17 11:01:13 +0100747 * batadv_orig_ifinfo_put - decrement the refcounter and possibly release
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100748 * the orig_ifinfo
749 * @orig_ifinfo: the orig_ifinfo object to release
750 */
Sven Eckelmann35f94772016-01-17 11:01:13 +0100751void batadv_orig_ifinfo_put(struct batadv_orig_ifinfo *orig_ifinfo)
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100752{
Sven Eckelmanna6ba0d32016-01-16 10:29:52 +0100753 kref_put(&orig_ifinfo->refcount, batadv_orig_ifinfo_release);
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100754}
755
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100756/**
757 * batadv_orig_node_free_rcu - free the orig_node
758 * @rcu: rcu pointer of the orig_node
759 */
Sven Eckelmann03fc7f82012-05-12 18:34:00 +0200760static void batadv_orig_node_free_rcu(struct rcu_head *rcu)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000761{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200762 struct batadv_orig_node *orig_node;
Marek Lindner16b1aba2011-01-19 20:01:42 +0000763
Sven Eckelmann56303d32012-06-05 22:31:31 +0200764 orig_node = container_of(rcu, struct batadv_orig_node, rcu);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000765
Linus Lüssing60432d72014-02-15 17:47:51 +0100766 batadv_mcast_purge_orig(orig_node);
767
Martin Hundebøll610bfc6bc2013-05-23 16:53:02 +0200768 batadv_frag_purge_orig(orig_node, NULL);
769
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200770 if (orig_node->bat_priv->bat_algo_ops->bat_orig_free)
771 orig_node->bat_priv->bat_algo_ops->bat_orig_free(orig_node);
772
Antonio Quartullia73105b2011-04-27 14:27:44 +0200773 kfree(orig_node->tt_buff);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000774 kfree(orig_node);
775}
776
Linus Lüssing72822222013-04-15 21:43:29 +0800777/**
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100778 * batadv_orig_node_release - release orig_node from lists and queue for
779 * free after rcu grace period
Sven Eckelmann7c124392016-01-16 10:29:56 +0100780 * @ref: kref pointer of the orig_node
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100781 */
Sven Eckelmann7c124392016-01-16 10:29:56 +0100782static void batadv_orig_node_release(struct kref *ref)
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100783{
784 struct hlist_node *node_tmp;
785 struct batadv_neigh_node *neigh_node;
Sven Eckelmann7c124392016-01-16 10:29:56 +0100786 struct batadv_orig_node *orig_node;
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100787 struct batadv_orig_ifinfo *orig_ifinfo;
788
Sven Eckelmann7c124392016-01-16 10:29:56 +0100789 orig_node = container_of(ref, struct batadv_orig_node, refcount);
790
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100791 spin_lock_bh(&orig_node->neigh_list_lock);
792
793 /* for all neighbors towards this originator ... */
794 hlist_for_each_entry_safe(neigh_node, node_tmp,
795 &orig_node->neigh_list, list) {
796 hlist_del_rcu(&neigh_node->list);
Sven Eckelmann25bb2502016-01-17 11:01:11 +0100797 batadv_neigh_node_put(neigh_node);
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100798 }
799
800 hlist_for_each_entry_safe(orig_ifinfo, node_tmp,
801 &orig_node->ifinfo_list, list) {
802 hlist_del_rcu(&orig_ifinfo->list);
Sven Eckelmann35f94772016-01-17 11:01:13 +0100803 batadv_orig_ifinfo_put(orig_ifinfo);
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100804 }
805 spin_unlock_bh(&orig_node->neigh_list_lock);
806
807 /* Free nc_nodes */
808 batadv_nc_purge_orig(orig_node->bat_priv, orig_node, NULL);
809
810 call_rcu(&orig_node->rcu, batadv_orig_node_free_rcu);
811}
812
813/**
Sven Eckelmann5d967312016-01-17 11:01:09 +0100814 * batadv_orig_node_put - decrement the orig node refcounter and possibly
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100815 * release it
Linus Lüssing72822222013-04-15 21:43:29 +0800816 * @orig_node: the orig node to free
817 */
Sven Eckelmann5d967312016-01-17 11:01:09 +0100818void batadv_orig_node_put(struct batadv_orig_node *orig_node)
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000819{
Sven Eckelmann7c124392016-01-16 10:29:56 +0100820 kref_put(&orig_node->refcount, batadv_orig_node_release);
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000821}
822
Sven Eckelmann56303d32012-06-05 22:31:31 +0200823void batadv_originator_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000824{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200825 struct batadv_hashtable *hash = bat_priv->orig_hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800826 struct hlist_node *node_tmp;
Marek Lindner16b1aba2011-01-19 20:01:42 +0000827 struct hlist_head *head;
Marek Lindner16b1aba2011-01-19 20:01:42 +0000828 spinlock_t *list_lock; /* spinlock to protect write access */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200829 struct batadv_orig_node *orig_node;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200830 u32 i;
Marek Lindner16b1aba2011-01-19 20:01:42 +0000831
832 if (!hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000833 return;
834
835 cancel_delayed_work_sync(&bat_priv->orig_work);
836
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000837 bat_priv->orig_hash = NULL;
Marek Lindner16b1aba2011-01-19 20:01:42 +0000838
839 for (i = 0; i < hash->size; i++) {
840 head = &hash->table[i];
841 list_lock = &hash->list_locks[i];
842
843 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800844 hlist_for_each_entry_safe(orig_node, node_tmp,
Marek Lindner7aadf882011-02-18 12:28:09 +0000845 head, hash_entry) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800846 hlist_del_rcu(&orig_node->hash_entry);
Sven Eckelmann5d967312016-01-17 11:01:09 +0100847 batadv_orig_node_put(orig_node);
Marek Lindner16b1aba2011-01-19 20:01:42 +0000848 }
849 spin_unlock_bh(list_lock);
850 }
851
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +0200852 batadv_hash_destroy(hash);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000853}
854
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200855/**
856 * batadv_orig_node_new - creates a new orig_node
857 * @bat_priv: the bat priv with all the soft interface information
858 * @addr: the mac address of the originator
859 *
860 * Creates a new originator object and initialise all the generic fields.
861 * The new object is not added to the originator list.
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200862 *
863 * Return: the newly created object or NULL on failure.
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200864 */
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200865struct batadv_orig_node *batadv_orig_node_new(struct batadv_priv *bat_priv,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200866 const u8 *addr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000867{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200868 struct batadv_orig_node *orig_node;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200869 struct batadv_orig_node_vlan *vlan;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200870 unsigned long reset_time;
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200871 int i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000872
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200873 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
874 "Creating new originator: %pM\n", addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000875
Sven Eckelmann704509b2011-05-14 23:14:54 +0200876 orig_node = kzalloc(sizeof(*orig_node), GFP_ATOMIC);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000877 if (!orig_node)
878 return NULL;
879
Marek Lindner9591a792010-12-12 21:57:11 +0000880 INIT_HLIST_HEAD(&orig_node->neigh_list);
Marek Lindnerd0fa4f32015-06-22 00:30:22 +0800881 INIT_HLIST_HEAD(&orig_node->vlan_list);
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100882 INIT_HLIST_HEAD(&orig_node->ifinfo_list);
Marek Lindnerf3e00082011-01-25 21:52:11 +0000883 spin_lock_init(&orig_node->bcast_seqno_lock);
Marek Lindnerf987ed62010-12-12 21:57:12 +0000884 spin_lock_init(&orig_node->neigh_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200885 spin_lock_init(&orig_node->tt_buff_lock);
Antonio Quartullia70a9aa2013-07-30 22:16:24 +0200886 spin_lock_init(&orig_node->tt_lock);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200887 spin_lock_init(&orig_node->vlan_list_lock);
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000888
Martin Hundebølld56b1702013-01-25 11:12:39 +0100889 batadv_nc_init_orig(orig_node);
890
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000891 /* extra reference for return */
Sven Eckelmann7c124392016-01-16 10:29:56 +0100892 kref_init(&orig_node->refcount);
893 kref_get(&orig_node->refcount);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000894
Marek Lindner16b1aba2011-01-19 20:01:42 +0000895 orig_node->bat_priv = bat_priv;
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100896 ether_addr_copy(orig_node->orig, addr);
Antonio Quartulli785ea112011-11-23 11:35:44 +0100897 batadv_dat_init_orig_node_addr(orig_node);
Antonio Quartullic8c991b2011-07-07 01:40:57 +0200898 atomic_set(&orig_node->last_ttvn, 0);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200899 orig_node->tt_buff = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200900 orig_node->tt_buff_len = 0;
Linus Lüssing2c667a32014-10-30 06:23:40 +0100901 orig_node->last_seen = jiffies;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200902 reset_time = jiffies - 1 - msecs_to_jiffies(BATADV_RESET_PROTECTION_MS);
903 orig_node->bcast_seqno_reset = reset_time;
Linus Lüssing8a4023c2015-06-16 17:10:26 +0200904
Linus Lüssing60432d72014-02-15 17:47:51 +0100905#ifdef CONFIG_BATMAN_ADV_MCAST
906 orig_node->mcast_flags = BATADV_NO_FLAGS;
Linus Lüssing8a4023c2015-06-16 17:10:26 +0200907 INIT_HLIST_NODE(&orig_node->mcast_want_all_unsnoopables_node);
908 INIT_HLIST_NODE(&orig_node->mcast_want_all_ipv4_node);
909 INIT_HLIST_NODE(&orig_node->mcast_want_all_ipv6_node);
910 spin_lock_init(&orig_node->mcast_handler_lock);
Linus Lüssing60432d72014-02-15 17:47:51 +0100911#endif
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000912
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200913 /* create a vlan object for the "untagged" LAN */
914 vlan = batadv_orig_node_vlan_new(orig_node, BATADV_NO_FLAGS);
915 if (!vlan)
916 goto free_orig_node;
917 /* batadv_orig_node_vlan_new() increases the refcounter.
918 * Immediately release vlan since it is not needed anymore in this
919 * context
920 */
Sven Eckelmann21754e22016-01-17 11:01:24 +0100921 batadv_orig_node_vlan_put(vlan);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200922
Martin Hundebøll610bfc6bc2013-05-23 16:53:02 +0200923 for (i = 0; i < BATADV_FRAG_BUFFER_COUNT; i++) {
924 INIT_HLIST_HEAD(&orig_node->fragments[i].head);
925 spin_lock_init(&orig_node->fragments[i].lock);
926 orig_node->fragments[i].size = 0;
927 }
928
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000929 return orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000930free_orig_node:
931 kfree(orig_node);
932 return NULL;
933}
934
Simon Wunderlich89652332013-11-13 19:14:46 +0100935/**
Simon Wunderlich709de132014-03-26 15:46:24 +0100936 * batadv_purge_neigh_ifinfo - purge obsolete ifinfo entries from neighbor
937 * @bat_priv: the bat priv with all the soft interface information
938 * @neigh: orig node which is to be checked
939 */
940static void
941batadv_purge_neigh_ifinfo(struct batadv_priv *bat_priv,
942 struct batadv_neigh_node *neigh)
943{
944 struct batadv_neigh_ifinfo *neigh_ifinfo;
945 struct batadv_hard_iface *if_outgoing;
946 struct hlist_node *node_tmp;
947
948 spin_lock_bh(&neigh->ifinfo_lock);
949
950 /* for all ifinfo objects for this neighinator */
951 hlist_for_each_entry_safe(neigh_ifinfo, node_tmp,
952 &neigh->ifinfo_list, list) {
953 if_outgoing = neigh_ifinfo->if_outgoing;
954
955 /* always keep the default interface */
956 if (if_outgoing == BATADV_IF_DEFAULT)
957 continue;
958
959 /* don't purge if the interface is not (going) down */
960 if ((if_outgoing->if_status != BATADV_IF_INACTIVE) &&
961 (if_outgoing->if_status != BATADV_IF_NOT_IN_USE) &&
962 (if_outgoing->if_status != BATADV_IF_TO_BE_REMOVED))
963 continue;
964
965 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
966 "neighbor/ifinfo purge: neighbor %pM, iface: %s\n",
967 neigh->addr, if_outgoing->net_dev->name);
968
969 hlist_del_rcu(&neigh_ifinfo->list);
Sven Eckelmann044fa3a2016-01-17 11:01:12 +0100970 batadv_neigh_ifinfo_put(neigh_ifinfo);
Simon Wunderlich709de132014-03-26 15:46:24 +0100971 }
972
973 spin_unlock_bh(&neigh->ifinfo_lock);
974}
975
976/**
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100977 * batadv_purge_orig_ifinfo - purge obsolete ifinfo entries from originator
978 * @bat_priv: the bat priv with all the soft interface information
979 * @orig_node: orig node which is to be checked
980 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200981 * Return: true if any ifinfo entry was purged, false otherwise.
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100982 */
983static bool
984batadv_purge_orig_ifinfo(struct batadv_priv *bat_priv,
985 struct batadv_orig_node *orig_node)
986{
987 struct batadv_orig_ifinfo *orig_ifinfo;
988 struct batadv_hard_iface *if_outgoing;
989 struct hlist_node *node_tmp;
990 bool ifinfo_purged = false;
991
992 spin_lock_bh(&orig_node->neigh_list_lock);
993
994 /* for all ifinfo objects for this originator */
995 hlist_for_each_entry_safe(orig_ifinfo, node_tmp,
996 &orig_node->ifinfo_list, list) {
997 if_outgoing = orig_ifinfo->if_outgoing;
998
999 /* always keep the default interface */
1000 if (if_outgoing == BATADV_IF_DEFAULT)
1001 continue;
1002
1003 /* don't purge if the interface is not (going) down */
1004 if ((if_outgoing->if_status != BATADV_IF_INACTIVE) &&
1005 (if_outgoing->if_status != BATADV_IF_NOT_IN_USE) &&
1006 (if_outgoing->if_status != BATADV_IF_TO_BE_REMOVED))
1007 continue;
1008
1009 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1010 "router/ifinfo purge: originator %pM, iface: %s\n",
1011 orig_node->orig, if_outgoing->net_dev->name);
1012
1013 ifinfo_purged = true;
1014
1015 hlist_del_rcu(&orig_ifinfo->list);
Sven Eckelmann35f94772016-01-17 11:01:13 +01001016 batadv_orig_ifinfo_put(orig_ifinfo);
Simon Wunderlichf3b3d902013-11-13 19:14:50 +01001017 if (orig_node->last_bonding_candidate == orig_ifinfo) {
1018 orig_node->last_bonding_candidate = NULL;
Sven Eckelmann35f94772016-01-17 11:01:13 +01001019 batadv_orig_ifinfo_put(orig_ifinfo);
Simon Wunderlichf3b3d902013-11-13 19:14:50 +01001020 }
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001021 }
1022
1023 spin_unlock_bh(&orig_node->neigh_list_lock);
1024
1025 return ifinfo_purged;
1026}
1027
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001028/**
Simon Wunderlich89652332013-11-13 19:14:46 +01001029 * batadv_purge_orig_neighbors - purges neighbors from originator
1030 * @bat_priv: the bat priv with all the soft interface information
1031 * @orig_node: orig node which is to be checked
1032 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001033 * Return: true if any neighbor was purged, false otherwise
Simon Wunderlich89652332013-11-13 19:14:46 +01001034 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001035static bool
1036batadv_purge_orig_neighbors(struct batadv_priv *bat_priv,
Simon Wunderlich89652332013-11-13 19:14:46 +01001037 struct batadv_orig_node *orig_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001038{
Sasha Levinb67bfe02013-02-27 17:06:00 -08001039 struct hlist_node *node_tmp;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001040 struct batadv_neigh_node *neigh_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001041 bool neigh_purged = false;
Marek Lindner0b0094e2012-03-01 15:35:20 +08001042 unsigned long last_seen;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001043 struct batadv_hard_iface *if_incoming;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001044
Marek Lindnerf987ed62010-12-12 21:57:12 +00001045 spin_lock_bh(&orig_node->neigh_list_lock);
1046
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001047 /* for all neighbors towards this originator ... */
Sasha Levinb67bfe02013-02-27 17:06:00 -08001048 hlist_for_each_entry_safe(neigh_node, node_tmp,
Marek Lindner9591a792010-12-12 21:57:11 +00001049 &orig_node->neigh_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001050 last_seen = neigh_node->last_seen;
1051 if_incoming = neigh_node->if_incoming;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001052
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001053 if ((batadv_has_timed_out(last_seen, BATADV_PURGE_TIMEOUT)) ||
Sven Eckelmanne9a4f292012-06-03 22:19:19 +02001054 (if_incoming->if_status == BATADV_IF_INACTIVE) ||
1055 (if_incoming->if_status == BATADV_IF_NOT_IN_USE) ||
1056 (if_incoming->if_status == BATADV_IF_TO_BE_REMOVED)) {
Sven Eckelmanne9a4f292012-06-03 22:19:19 +02001057 if ((if_incoming->if_status == BATADV_IF_INACTIVE) ||
1058 (if_incoming->if_status == BATADV_IF_NOT_IN_USE) ||
1059 (if_incoming->if_status == BATADV_IF_TO_BE_REMOVED))
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001060 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001061 "neighbor purge: originator %pM, neighbor: %pM, iface: %s\n",
1062 orig_node->orig, neigh_node->addr,
1063 if_incoming->net_dev->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001064 else
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001065 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001066 "neighbor timeout: originator %pM, neighbor: %pM, last_seen: %u\n",
1067 orig_node->orig, neigh_node->addr,
1068 jiffies_to_msecs(last_seen));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001069
1070 neigh_purged = true;
Marek Lindner9591a792010-12-12 21:57:11 +00001071
Marek Lindnerf987ed62010-12-12 21:57:12 +00001072 hlist_del_rcu(&neigh_node->list);
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001073 batadv_neigh_node_put(neigh_node);
Simon Wunderlich709de132014-03-26 15:46:24 +01001074 } else {
1075 /* only necessary if not the whole neighbor is to be
1076 * deleted, but some interface has been removed.
1077 */
1078 batadv_purge_neigh_ifinfo(bat_priv, neigh_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001079 }
1080 }
Marek Lindnerf987ed62010-12-12 21:57:12 +00001081
1082 spin_unlock_bh(&orig_node->neigh_list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001083 return neigh_purged;
1084}
1085
Simon Wunderlich89652332013-11-13 19:14:46 +01001086/**
1087 * batadv_find_best_neighbor - finds the best neighbor after purging
1088 * @bat_priv: the bat priv with all the soft interface information
1089 * @orig_node: orig node which is to be checked
1090 * @if_outgoing: the interface for which the metric should be compared
1091 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001092 * Return: the current best neighbor, with refcount increased.
Simon Wunderlich89652332013-11-13 19:14:46 +01001093 */
1094static struct batadv_neigh_node *
1095batadv_find_best_neighbor(struct batadv_priv *bat_priv,
1096 struct batadv_orig_node *orig_node,
1097 struct batadv_hard_iface *if_outgoing)
1098{
1099 struct batadv_neigh_node *best = NULL, *neigh;
1100 struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
1101
1102 rcu_read_lock();
1103 hlist_for_each_entry_rcu(neigh, &orig_node->neigh_list, list) {
1104 if (best && (bao->bat_neigh_cmp(neigh, if_outgoing,
1105 best, if_outgoing) <= 0))
1106 continue;
1107
Sven Eckelmann77ae32e2016-01-16 10:29:53 +01001108 if (!kref_get_unless_zero(&neigh->refcount))
Simon Wunderlich89652332013-11-13 19:14:46 +01001109 continue;
1110
1111 if (best)
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001112 batadv_neigh_node_put(best);
Simon Wunderlich89652332013-11-13 19:14:46 +01001113
1114 best = neigh;
1115 }
1116 rcu_read_unlock();
1117
1118 return best;
1119}
1120
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001121/**
1122 * batadv_purge_orig_node - purges obsolete information from an orig_node
1123 * @bat_priv: the bat priv with all the soft interface information
1124 * @orig_node: orig node which is to be checked
1125 *
1126 * This function checks if the orig_node or substructures of it have become
1127 * obsolete, and purges this information if that's the case.
1128 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001129 * Return: true if the orig_node is to be removed, false otherwise.
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001130 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001131static bool batadv_purge_orig_node(struct batadv_priv *bat_priv,
1132 struct batadv_orig_node *orig_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001133{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001134 struct batadv_neigh_node *best_neigh_node;
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001135 struct batadv_hard_iface *hard_iface;
Simon Wunderlich7b955a92014-03-26 15:46:23 +01001136 bool changed_ifinfo, changed_neigh;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001137
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001138 if (batadv_has_timed_out(orig_node->last_seen,
1139 2 * BATADV_PURGE_TIMEOUT)) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001140 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001141 "Originator timeout: originator %pM, last_seen %u\n",
1142 orig_node->orig,
1143 jiffies_to_msecs(orig_node->last_seen));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001144 return true;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001145 }
Simon Wunderlich7b955a92014-03-26 15:46:23 +01001146 changed_ifinfo = batadv_purge_orig_ifinfo(bat_priv, orig_node);
1147 changed_neigh = batadv_purge_orig_neighbors(bat_priv, orig_node);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001148
Simon Wunderlich7b955a92014-03-26 15:46:23 +01001149 if (!changed_ifinfo && !changed_neigh)
Simon Wunderlich89652332013-11-13 19:14:46 +01001150 return false;
1151
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001152 /* first for NULL ... */
Simon Wunderlich89652332013-11-13 19:14:46 +01001153 best_neigh_node = batadv_find_best_neighbor(bat_priv, orig_node,
1154 BATADV_IF_DEFAULT);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001155 batadv_update_route(bat_priv, orig_node, BATADV_IF_DEFAULT,
1156 best_neigh_node);
Simon Wunderlich89652332013-11-13 19:14:46 +01001157 if (best_neigh_node)
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001158 batadv_neigh_node_put(best_neigh_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001159
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001160 /* ... then for all other interfaces. */
1161 rcu_read_lock();
1162 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
1163 if (hard_iface->if_status != BATADV_IF_ACTIVE)
1164 continue;
1165
1166 if (hard_iface->soft_iface != bat_priv->soft_iface)
1167 continue;
1168
Sven Eckelmann27353442016-03-05 16:09:16 +01001169 if (!kref_get_unless_zero(&hard_iface->refcount))
1170 continue;
1171
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001172 best_neigh_node = batadv_find_best_neighbor(bat_priv,
1173 orig_node,
1174 hard_iface);
1175 batadv_update_route(bat_priv, orig_node, hard_iface,
1176 best_neigh_node);
1177 if (best_neigh_node)
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001178 batadv_neigh_node_put(best_neigh_node);
Sven Eckelmann27353442016-03-05 16:09:16 +01001179
1180 batadv_hardif_put(hard_iface);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001181 }
1182 rcu_read_unlock();
1183
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001184 return false;
1185}
1186
Sven Eckelmann56303d32012-06-05 22:31:31 +02001187static void _batadv_purge_orig(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001188{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001189 struct batadv_hashtable *hash = bat_priv->orig_hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001190 struct hlist_node *node_tmp;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001191 struct hlist_head *head;
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001192 spinlock_t *list_lock; /* spinlock to protect write access */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001193 struct batadv_orig_node *orig_node;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001194 u32 i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001195
1196 if (!hash)
1197 return;
1198
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001199 /* for all origins... */
1200 for (i = 0; i < hash->size; i++) {
1201 head = &hash->table[i];
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001202 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001203
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001204 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001205 hlist_for_each_entry_safe(orig_node, node_tmp,
Marek Lindner7aadf882011-02-18 12:28:09 +00001206 head, hash_entry) {
Sven Eckelmann03fc7f82012-05-12 18:34:00 +02001207 if (batadv_purge_orig_node(bat_priv, orig_node)) {
Marek Lindner414254e2013-04-23 21:39:58 +08001208 batadv_gw_node_delete(bat_priv, orig_node);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001209 hlist_del_rcu(&orig_node->hash_entry);
Linus Lüssing9d31b3c2014-12-13 23:32:15 +01001210 batadv_tt_global_del_orig(orig_node->bat_priv,
1211 orig_node, -1,
1212 "originator timed out");
Sven Eckelmann5d967312016-01-17 11:01:09 +01001213 batadv_orig_node_put(orig_node);
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001214 continue;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001215 }
Martin Hundebøll610bfc6bc2013-05-23 16:53:02 +02001216
1217 batadv_frag_purge_orig(orig_node,
1218 batadv_frag_check_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001219 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001220 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001221 }
1222
Sven Eckelmann7cf06bc2012-05-12 02:09:29 +02001223 batadv_gw_election(bat_priv);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001224}
1225
Sven Eckelmann03fc7f82012-05-12 18:34:00 +02001226static void batadv_purge_orig(struct work_struct *work)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001227{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001228 struct delayed_work *delayed_work;
1229 struct batadv_priv *bat_priv;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001230
Geliang Tang4ba4bc02015-12-28 23:43:37 +08001231 delayed_work = to_delayed_work(work);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001232 bat_priv = container_of(delayed_work, struct batadv_priv, orig_work);
Sven Eckelmann03fc7f82012-05-12 18:34:00 +02001233 _batadv_purge_orig(bat_priv);
Antonio Quartulli72414442012-12-25 13:14:37 +01001234 queue_delayed_work(batadv_event_workqueue,
1235 &bat_priv->orig_work,
1236 msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001237}
1238
Sven Eckelmann56303d32012-06-05 22:31:31 +02001239void batadv_purge_orig_ref(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001240{
Sven Eckelmann03fc7f82012-05-12 18:34:00 +02001241 _batadv_purge_orig(bat_priv);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001242}
1243
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001244int batadv_orig_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001245{
1246 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001247 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001248 struct batadv_hard_iface *primary_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001249
Marek Lindner30da63a2012-08-03 17:15:46 +02001250 primary_if = batadv_seq_print_text_primary_if_get(seq);
1251 if (!primary_if)
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001252 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001253
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001254 seq_printf(seq, "[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s %s)]\n",
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001255 BATADV_SOURCE_VERSION, primary_if->net_dev->name,
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001256 primary_if->net_dev->dev_addr, net_dev->name,
1257 bat_priv->bat_algo_ops->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001258
Sven Eckelmann82047ad2016-01-17 11:01:10 +01001259 batadv_hardif_put(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001260
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001261 if (!bat_priv->bat_algo_ops->bat_orig_print) {
1262 seq_puts(seq,
1263 "No printing function for this routing protocol\n");
1264 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001265 }
1266
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001267 bat_priv->bat_algo_ops->bat_orig_print(bat_priv, seq,
1268 BATADV_IF_DEFAULT);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001269
Marek Lindner30da63a2012-08-03 17:15:46 +02001270 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001271}
1272
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001273/**
1274 * batadv_orig_hardif_seq_print_text - writes originator infos for a specific
1275 * outgoing interface
1276 * @seq: debugfs table seq_file struct
1277 * @offset: not used
1278 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001279 * Return: 0
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001280 */
1281int batadv_orig_hardif_seq_print_text(struct seq_file *seq, void *offset)
1282{
1283 struct net_device *net_dev = (struct net_device *)seq->private;
1284 struct batadv_hard_iface *hard_iface;
1285 struct batadv_priv *bat_priv;
1286
1287 hard_iface = batadv_hardif_get_by_netdev(net_dev);
1288
1289 if (!hard_iface || !hard_iface->soft_iface) {
1290 seq_puts(seq, "Interface not known to B.A.T.M.A.N.\n");
1291 goto out;
1292 }
1293
1294 bat_priv = netdev_priv(hard_iface->soft_iface);
1295 if (!bat_priv->bat_algo_ops->bat_orig_print) {
1296 seq_puts(seq,
1297 "No printing function for this routing protocol\n");
1298 goto out;
1299 }
1300
1301 if (hard_iface->if_status != BATADV_IF_ACTIVE) {
1302 seq_puts(seq, "Interface not active\n");
1303 goto out;
1304 }
1305
1306 seq_printf(seq, "[B.A.T.M.A.N. adv %s, IF/MAC: %s/%pM (%s %s)]\n",
1307 BATADV_SOURCE_VERSION, hard_iface->net_dev->name,
1308 hard_iface->net_dev->dev_addr,
1309 hard_iface->soft_iface->name, bat_priv->bat_algo_ops->name);
1310
1311 bat_priv->bat_algo_ops->bat_orig_print(bat_priv, seq, hard_iface);
1312
1313out:
Marek Lindner16a41422014-04-24 03:44:25 +08001314 if (hard_iface)
Sven Eckelmann82047ad2016-01-17 11:01:10 +01001315 batadv_hardif_put(hard_iface);
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001316 return 0;
1317}
1318
Sven Eckelmann56303d32012-06-05 22:31:31 +02001319int batadv_orig_hash_add_if(struct batadv_hard_iface *hard_iface,
1320 int max_if_num)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001321{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001322 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
Antonio Quartullid0015fd2013-09-03 11:10:23 +02001323 struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001324 struct batadv_hashtable *hash = bat_priv->orig_hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001325 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001326 struct batadv_orig_node *orig_node;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001327 u32 i;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001328 int ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001329
1330 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001331 * if_num
1332 */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001333 for (i = 0; i < hash->size; i++) {
1334 head = &hash->table[i];
1335
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001336 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001337 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
Antonio Quartullid0015fd2013-09-03 11:10:23 +02001338 ret = 0;
1339 if (bao->bat_orig_add_if)
1340 ret = bao->bat_orig_add_if(orig_node,
1341 max_if_num);
Sven Eckelmann5346c352012-05-05 13:27:28 +02001342 if (ret == -ENOMEM)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001343 goto err;
1344 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001345 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001346 }
1347
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001348 return 0;
1349
1350err:
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001351 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001352 return -ENOMEM;
1353}
1354
Sven Eckelmann56303d32012-06-05 22:31:31 +02001355int batadv_orig_hash_del_if(struct batadv_hard_iface *hard_iface,
1356 int max_if_num)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001357{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001358 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001359 struct batadv_hashtable *hash = bat_priv->orig_hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001360 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001361 struct batadv_hard_iface *hard_iface_tmp;
1362 struct batadv_orig_node *orig_node;
Antonio Quartullid0015fd2013-09-03 11:10:23 +02001363 struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001364 u32 i;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001365 int ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001366
1367 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001368 * if_num
1369 */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001370 for (i = 0; i < hash->size; i++) {
1371 head = &hash->table[i];
1372
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001373 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001374 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
Antonio Quartullid0015fd2013-09-03 11:10:23 +02001375 ret = 0;
1376 if (bao->bat_orig_del_if)
1377 ret = bao->bat_orig_del_if(orig_node,
1378 max_if_num,
1379 hard_iface->if_num);
Sven Eckelmann5346c352012-05-05 13:27:28 +02001380 if (ret == -ENOMEM)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001381 goto err;
1382 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001383 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001384 }
1385
1386 /* renumber remaining batman interfaces _inside_ of orig_hash_lock */
1387 rcu_read_lock();
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001388 list_for_each_entry_rcu(hard_iface_tmp, &batadv_hardif_list, list) {
Sven Eckelmanne9a4f292012-06-03 22:19:19 +02001389 if (hard_iface_tmp->if_status == BATADV_IF_NOT_IN_USE)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001390 continue;
1391
Marek Lindnere6c10f42011-02-18 12:33:20 +00001392 if (hard_iface == hard_iface_tmp)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001393 continue;
1394
Marek Lindnere6c10f42011-02-18 12:33:20 +00001395 if (hard_iface->soft_iface != hard_iface_tmp->soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001396 continue;
1397
Marek Lindnere6c10f42011-02-18 12:33:20 +00001398 if (hard_iface_tmp->if_num > hard_iface->if_num)
1399 hard_iface_tmp->if_num--;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001400 }
1401 rcu_read_unlock();
1402
Marek Lindnere6c10f42011-02-18 12:33:20 +00001403 hard_iface->if_num = -1;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001404 return 0;
1405
1406err:
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001407 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001408 return -ENOMEM;
1409}