blob: 592cbda283e373204b9564356e86b037a2d0a293 [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"
Linus Lüssing60432d72014-02-15 17:47:51 +010043#include "multicast.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020044#include "network-coding.h"
45#include "routing.h"
46#include "translation-table.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000047
Antonio Quartullidec05072012-11-10 11:00:32 +010048/* hash class keys */
49static struct lock_class_key batadv_orig_hash_lock_class_key;
50
Sven Eckelmann03fc7f82012-05-12 18:34:00 +020051static void batadv_purge_orig(struct work_struct *work);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000052
Sven Eckelmann62fe7102015-09-15 19:00:48 +020053/**
Sven Eckelmann7afcbbe2015-10-31 12:29:29 +010054 * batadv_compare_orig - comparing function used in the originator hash table
55 * @node: node in the local table
56 * @data2: second object to compare the node to
Sven Eckelmann62fe7102015-09-15 19:00:48 +020057 *
Sven Eckelmann4b426b12016-02-22 21:02:39 +010058 * Return: true if they are the same originator
Sven Eckelmann62fe7102015-09-15 19:00:48 +020059 */
Sven Eckelmann4b426b12016-02-22 21:02:39 +010060bool batadv_compare_orig(const struct hlist_node *node, const void *data2)
Sven Eckelmannb8e2dd12011-06-15 15:08:59 +020061{
Sven Eckelmann56303d32012-06-05 22:31:31 +020062 const void *data1 = container_of(node, struct batadv_orig_node,
63 hash_entry);
Sven Eckelmannb8e2dd12011-06-15 15:08:59 +020064
dingtianhong323813e2013-12-26 19:40:39 +080065 return batadv_compare_eth(data1, data2);
Sven Eckelmannb8e2dd12011-06-15 15:08:59 +020066}
67
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +020068/**
69 * batadv_orig_node_vlan_get - get an orig_node_vlan object
70 * @orig_node: the originator serving the VLAN
71 * @vid: the VLAN identifier
72 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +020073 * Return: the vlan object identified by vid and belonging to orig_node or NULL
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +020074 * if it does not exist.
75 */
76struct batadv_orig_node_vlan *
77batadv_orig_node_vlan_get(struct batadv_orig_node *orig_node,
78 unsigned short vid)
79{
80 struct batadv_orig_node_vlan *vlan = NULL, *tmp;
81
82 rcu_read_lock();
Marek Lindnerd0fa4f32015-06-22 00:30:22 +080083 hlist_for_each_entry_rcu(tmp, &orig_node->vlan_list, list) {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +020084 if (tmp->vid != vid)
85 continue;
86
Sven Eckelmann161a3be2016-01-16 10:29:55 +010087 if (!kref_get_unless_zero(&tmp->refcount))
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +020088 continue;
89
90 vlan = tmp;
91
92 break;
93 }
94 rcu_read_unlock();
95
96 return vlan;
97}
98
99/**
100 * batadv_orig_node_vlan_new - search and possibly create an orig_node_vlan
101 * object
102 * @orig_node: the originator serving the VLAN
103 * @vid: the VLAN identifier
104 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200105 * Return: NULL in case of failure or the vlan object identified by vid and
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200106 * belonging to orig_node otherwise. The object is created and added to the list
107 * if it does not exist.
108 *
109 * The object is returned with refcounter increased by 1.
110 */
111struct batadv_orig_node_vlan *
112batadv_orig_node_vlan_new(struct batadv_orig_node *orig_node,
113 unsigned short vid)
114{
115 struct batadv_orig_node_vlan *vlan;
116
117 spin_lock_bh(&orig_node->vlan_list_lock);
118
119 /* first look if an object for this vid already exists */
120 vlan = batadv_orig_node_vlan_get(orig_node, vid);
121 if (vlan)
122 goto out;
123
124 vlan = kzalloc(sizeof(*vlan), GFP_ATOMIC);
125 if (!vlan)
126 goto out;
127
Sven Eckelmann161a3be2016-01-16 10:29:55 +0100128 kref_init(&vlan->refcount);
129 kref_get(&vlan->refcount);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200130 vlan->vid = vid;
131
Marek Lindnerd0fa4f32015-06-22 00:30:22 +0800132 hlist_add_head_rcu(&vlan->list, &orig_node->vlan_list);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200133
134out:
135 spin_unlock_bh(&orig_node->vlan_list_lock);
136
137 return vlan;
138}
139
140/**
Sven Eckelmann161a3be2016-01-16 10:29:55 +0100141 * batadv_orig_node_vlan_release - release originator-vlan object from lists
142 * and queue for free after rcu grace period
143 * @ref: kref pointer of the originator-vlan object
144 */
145static void batadv_orig_node_vlan_release(struct kref *ref)
146{
147 struct batadv_orig_node_vlan *orig_vlan;
148
149 orig_vlan = container_of(ref, struct batadv_orig_node_vlan, refcount);
150
151 kfree_rcu(orig_vlan, rcu);
152}
153
154/**
Sven Eckelmann21754e22016-01-17 11:01:24 +0100155 * batadv_orig_node_vlan_put - decrement the refcounter and possibly release
156 * the originator-vlan object
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200157 * @orig_vlan: the originator-vlan object to release
158 */
Sven Eckelmann21754e22016-01-17 11:01:24 +0100159void batadv_orig_node_vlan_put(struct batadv_orig_node_vlan *orig_vlan)
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200160{
Sven Eckelmann161a3be2016-01-16 10:29:55 +0100161 kref_put(&orig_vlan->refcount, batadv_orig_node_vlan_release);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200162}
163
Sven Eckelmann56303d32012-06-05 22:31:31 +0200164int batadv_originator_init(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000165{
166 if (bat_priv->orig_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200167 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000168
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +0200169 bat_priv->orig_hash = batadv_hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000170
171 if (!bat_priv->orig_hash)
172 goto err;
173
Antonio Quartullidec05072012-11-10 11:00:32 +0100174 batadv_hash_set_lock_class(bat_priv->orig_hash,
175 &batadv_orig_hash_lock_class_key);
176
Antonio Quartulli72414442012-12-25 13:14:37 +0100177 INIT_DELAYED_WORK(&bat_priv->orig_work, batadv_purge_orig);
178 queue_delayed_work(batadv_event_workqueue,
179 &bat_priv->orig_work,
180 msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD));
181
Sven Eckelmann5346c352012-05-05 13:27:28 +0200182 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000183
184err:
Sven Eckelmann5346c352012-05-05 13:27:28 +0200185 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000186}
187
Simon Wunderlich89652332013-11-13 19:14:46 +0100188/**
Sven Eckelmannae3e1e32016-01-05 12:06:24 +0100189 * batadv_neigh_ifinfo_release - release neigh_ifinfo from lists and queue for
190 * free after rcu grace period
Sven Eckelmann962c6832016-01-16 10:29:51 +0100191 * @ref: kref pointer of the neigh_ifinfo
Simon Wunderlich89652332013-11-13 19:14:46 +0100192 */
Sven Eckelmann962c6832016-01-16 10:29:51 +0100193static void batadv_neigh_ifinfo_release(struct kref *ref)
Simon Wunderlich89652332013-11-13 19:14:46 +0100194{
Sven Eckelmann962c6832016-01-16 10:29:51 +0100195 struct batadv_neigh_ifinfo *neigh_ifinfo;
196
197 neigh_ifinfo = container_of(ref, struct batadv_neigh_ifinfo, refcount);
198
Sven Eckelmannae3e1e32016-01-05 12:06:24 +0100199 if (neigh_ifinfo->if_outgoing != BATADV_IF_DEFAULT)
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100200 batadv_hardif_put(neigh_ifinfo->if_outgoing);
Sven Eckelmannae3e1e32016-01-05 12:06:24 +0100201
202 kfree_rcu(neigh_ifinfo, rcu);
Simon Wunderlich89652332013-11-13 19:14:46 +0100203}
204
205/**
Sven Eckelmann044fa3a2016-01-17 11:01:12 +0100206 * batadv_neigh_ifinfo_put - decrement the refcounter and possibly release
Simon Wunderlich89652332013-11-13 19:14:46 +0100207 * the neigh_ifinfo
208 * @neigh_ifinfo: the neigh_ifinfo object to release
209 */
Sven Eckelmann044fa3a2016-01-17 11:01:12 +0100210void batadv_neigh_ifinfo_put(struct batadv_neigh_ifinfo *neigh_ifinfo)
Simon Wunderlich89652332013-11-13 19:14:46 +0100211{
Sven Eckelmann962c6832016-01-16 10:29:51 +0100212 kref_put(&neigh_ifinfo->refcount, batadv_neigh_ifinfo_release);
Simon Wunderlich89652332013-11-13 19:14:46 +0100213}
214
215/**
Sven Eckelmannf6389692016-01-05 12:06:23 +0100216 * batadv_hardif_neigh_release - release hardif neigh node from lists and
217 * queue for free after rcu grace period
Sven Eckelmann90f564d2016-01-16 10:29:40 +0100218 * @ref: kref pointer of the neigh_node
Marek Lindnercef63412015-08-04 21:09:55 +0800219 */
Sven Eckelmann90f564d2016-01-16 10:29:40 +0100220static void batadv_hardif_neigh_release(struct kref *ref)
Marek Lindnercef63412015-08-04 21:09:55 +0800221{
Sven Eckelmann90f564d2016-01-16 10:29:40 +0100222 struct batadv_hardif_neigh_node *hardif_neigh;
223
224 hardif_neigh = container_of(ref, struct batadv_hardif_neigh_node,
225 refcount);
226
Sven Eckelmannf6389692016-01-05 12:06:23 +0100227 spin_lock_bh(&hardif_neigh->if_incoming->neigh_list_lock);
228 hlist_del_init_rcu(&hardif_neigh->list);
229 spin_unlock_bh(&hardif_neigh->if_incoming->neigh_list_lock);
Sven Eckelmannbab7c6c2016-01-05 12:06:17 +0100230
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100231 batadv_hardif_put(hardif_neigh->if_incoming);
Sven Eckelmannf6389692016-01-05 12:06:23 +0100232 kfree_rcu(hardif_neigh, rcu);
Marek Lindnercef63412015-08-04 21:09:55 +0800233}
234
235/**
Sven Eckelmannaccadc32016-01-17 11:01:14 +0100236 * batadv_hardif_neigh_put - decrement the hardif neighbors refcounter
Sven Eckelmannf6389692016-01-05 12:06:23 +0100237 * and possibly release it
Marek Lindnercef63412015-08-04 21:09:55 +0800238 * @hardif_neigh: hardif neigh neighbor to free
239 */
Sven Eckelmannaccadc32016-01-17 11:01:14 +0100240void batadv_hardif_neigh_put(struct batadv_hardif_neigh_node *hardif_neigh)
Marek Lindnercef63412015-08-04 21:09:55 +0800241{
Sven Eckelmann90f564d2016-01-16 10:29:40 +0100242 kref_put(&hardif_neigh->refcount, batadv_hardif_neigh_release);
Marek Lindnercef63412015-08-04 21:09:55 +0800243}
244
245/**
Sven Eckelmannb4d922c2016-01-05 12:06:25 +0100246 * batadv_neigh_node_release - release neigh_node from lists and queue for
247 * free after rcu grace period
Sven Eckelmann77ae32e2016-01-16 10:29:53 +0100248 * @ref: kref pointer of the neigh_node
Simon Wunderlich89652332013-11-13 19:14:46 +0100249 */
Sven Eckelmann77ae32e2016-01-16 10:29:53 +0100250static void batadv_neigh_node_release(struct kref *ref)
Simon Wunderlich89652332013-11-13 19:14:46 +0100251{
252 struct hlist_node *node_tmp;
Sven Eckelmann77ae32e2016-01-16 10:29:53 +0100253 struct batadv_neigh_node *neigh_node;
Simon Wunderlich89652332013-11-13 19:14:46 +0100254 struct batadv_neigh_ifinfo *neigh_ifinfo;
255
Sven Eckelmann77ae32e2016-01-16 10:29:53 +0100256 neigh_node = container_of(ref, struct batadv_neigh_node, refcount);
Simon Wunderlich89652332013-11-13 19:14:46 +0100257
258 hlist_for_each_entry_safe(neigh_ifinfo, node_tmp,
259 &neigh_node->ifinfo_list, list) {
Sven Eckelmann044fa3a2016-01-17 11:01:12 +0100260 batadv_neigh_ifinfo_put(neigh_ifinfo);
Simon Wunderlich89652332013-11-13 19:14:46 +0100261 }
Antonio Quartullibcef1f32015-03-01 00:50:17 +0800262
Sven Eckelmannabe59c62016-03-11 16:44:06 +0100263 batadv_hardif_neigh_put(neigh_node->hardif_neigh);
Marek Lindnercef63412015-08-04 21:09:55 +0800264
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100265 batadv_hardif_put(neigh_node->if_incoming);
Simon Wunderlich89652332013-11-13 19:14:46 +0100266
Sven Eckelmannb4d922c2016-01-05 12:06:25 +0100267 kfree_rcu(neigh_node, rcu);
Simon Wunderlich89652332013-11-13 19:14:46 +0100268}
269
270/**
Sven Eckelmann25bb2502016-01-17 11:01:11 +0100271 * batadv_neigh_node_put - decrement the neighbors refcounter and possibly
Sven Eckelmann77ae32e2016-01-16 10:29:53 +0100272 * release it
Simon Wunderlich89652332013-11-13 19:14:46 +0100273 * @neigh_node: neigh neighbor to free
274 */
Sven Eckelmann25bb2502016-01-17 11:01:11 +0100275void batadv_neigh_node_put(struct batadv_neigh_node *neigh_node)
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000276{
Sven Eckelmann77ae32e2016-01-16 10:29:53 +0100277 kref_put(&neigh_node->refcount, batadv_neigh_node_release);
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000278}
279
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100280/**
Antonio Quartulli6d030de2016-03-11 16:36:19 +0100281 * batadv_orig_router_get - router to the originator depending on iface
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100282 * @orig_node: the orig node for the router
283 * @if_outgoing: the interface where the payload packet has been received or
284 * the OGM should be sent to
285 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200286 * Return: the neighbor which should be router for this orig_node/iface.
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100287 *
288 * The object is returned with refcounter increased by 1.
289 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200290struct batadv_neigh_node *
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100291batadv_orig_router_get(struct batadv_orig_node *orig_node,
292 const struct batadv_hard_iface *if_outgoing)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000293{
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100294 struct batadv_orig_ifinfo *orig_ifinfo;
295 struct batadv_neigh_node *router = NULL;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000296
297 rcu_read_lock();
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100298 hlist_for_each_entry_rcu(orig_ifinfo, &orig_node->ifinfo_list, list) {
299 if (orig_ifinfo->if_outgoing != if_outgoing)
300 continue;
301
302 router = rcu_dereference(orig_ifinfo->router);
303 break;
304 }
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000305
Sven Eckelmann77ae32e2016-01-16 10:29:53 +0100306 if (router && !kref_get_unless_zero(&router->refcount))
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000307 router = NULL;
308
309 rcu_read_unlock();
310 return router;
311}
312
Antonio Quartulli0538f7592013-09-02 12:15:01 +0200313/**
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100314 * batadv_orig_ifinfo_get - find the ifinfo from an orig_node
315 * @orig_node: the orig node to be queried
316 * @if_outgoing: the interface for which the ifinfo should be acquired
317 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200318 * Return: the requested orig_ifinfo or NULL if not found.
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100319 *
320 * The object is returned with refcounter increased by 1.
321 */
322struct batadv_orig_ifinfo *
323batadv_orig_ifinfo_get(struct batadv_orig_node *orig_node,
324 struct batadv_hard_iface *if_outgoing)
325{
326 struct batadv_orig_ifinfo *tmp, *orig_ifinfo = NULL;
327
328 rcu_read_lock();
329 hlist_for_each_entry_rcu(tmp, &orig_node->ifinfo_list,
330 list) {
331 if (tmp->if_outgoing != if_outgoing)
332 continue;
333
Sven Eckelmanna6ba0d32016-01-16 10:29:52 +0100334 if (!kref_get_unless_zero(&tmp->refcount))
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100335 continue;
336
337 orig_ifinfo = tmp;
338 break;
339 }
340 rcu_read_unlock();
341
342 return orig_ifinfo;
343}
344
345/**
346 * batadv_orig_ifinfo_new - search and possibly create an orig_ifinfo object
347 * @orig_node: the orig node to be queried
348 * @if_outgoing: the interface for which the ifinfo should be acquired
349 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200350 * Return: NULL in case of failure or the orig_ifinfo object for the if_outgoing
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100351 * interface otherwise. The object is created and added to the list
352 * if it does not exist.
353 *
354 * The object is returned with refcounter increased by 1.
355 */
356struct batadv_orig_ifinfo *
357batadv_orig_ifinfo_new(struct batadv_orig_node *orig_node,
358 struct batadv_hard_iface *if_outgoing)
359{
360 struct batadv_orig_ifinfo *orig_ifinfo = NULL;
361 unsigned long reset_time;
362
363 spin_lock_bh(&orig_node->neigh_list_lock);
364
365 orig_ifinfo = batadv_orig_ifinfo_get(orig_node, if_outgoing);
366 if (orig_ifinfo)
367 goto out;
368
369 orig_ifinfo = kzalloc(sizeof(*orig_ifinfo), GFP_ATOMIC);
370 if (!orig_ifinfo)
371 goto out;
372
Sven Eckelmann17a86912016-04-11 13:06:40 +0200373 if (if_outgoing != BATADV_IF_DEFAULT)
374 kref_get(&if_outgoing->refcount);
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100375
376 reset_time = jiffies - 1;
377 reset_time -= msecs_to_jiffies(BATADV_RESET_PROTECTION_MS);
378 orig_ifinfo->batman_seqno_reset = reset_time;
379 orig_ifinfo->if_outgoing = if_outgoing;
380 INIT_HLIST_NODE(&orig_ifinfo->list);
Sven Eckelmanna6ba0d32016-01-16 10:29:52 +0100381 kref_init(&orig_ifinfo->refcount);
382 kref_get(&orig_ifinfo->refcount);
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100383 hlist_add_head_rcu(&orig_ifinfo->list,
384 &orig_node->ifinfo_list);
385out:
386 spin_unlock_bh(&orig_node->neigh_list_lock);
387 return orig_ifinfo;
388}
389
390/**
Simon Wunderlich89652332013-11-13 19:14:46 +0100391 * batadv_neigh_ifinfo_get - find the ifinfo from an neigh_node
Sven Eckelmanne51f0392015-09-06 21:38:51 +0200392 * @neigh: the neigh node to be queried
Simon Wunderlich89652332013-11-13 19:14:46 +0100393 * @if_outgoing: the interface for which the ifinfo should be acquired
394 *
395 * The object is returned with refcounter increased by 1.
396 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200397 * Return: the requested neigh_ifinfo or NULL if not found
Simon Wunderlich89652332013-11-13 19:14:46 +0100398 */
399struct batadv_neigh_ifinfo *
400batadv_neigh_ifinfo_get(struct batadv_neigh_node *neigh,
401 struct batadv_hard_iface *if_outgoing)
402{
403 struct batadv_neigh_ifinfo *neigh_ifinfo = NULL,
404 *tmp_neigh_ifinfo;
405
406 rcu_read_lock();
407 hlist_for_each_entry_rcu(tmp_neigh_ifinfo, &neigh->ifinfo_list,
408 list) {
409 if (tmp_neigh_ifinfo->if_outgoing != if_outgoing)
410 continue;
411
Sven Eckelmann962c6832016-01-16 10:29:51 +0100412 if (!kref_get_unless_zero(&tmp_neigh_ifinfo->refcount))
Simon Wunderlich89652332013-11-13 19:14:46 +0100413 continue;
414
415 neigh_ifinfo = tmp_neigh_ifinfo;
416 break;
417 }
418 rcu_read_unlock();
419
420 return neigh_ifinfo;
421}
422
423/**
424 * batadv_neigh_ifinfo_new - search and possibly create an neigh_ifinfo object
Sven Eckelmanne51f0392015-09-06 21:38:51 +0200425 * @neigh: the neigh node to be queried
Simon Wunderlich89652332013-11-13 19:14:46 +0100426 * @if_outgoing: the interface for which the ifinfo should be acquired
427 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200428 * Return: NULL in case of failure or the neigh_ifinfo object for the
Simon Wunderlich89652332013-11-13 19:14:46 +0100429 * if_outgoing interface otherwise. The object is created and added to the list
430 * if it does not exist.
431 *
432 * The object is returned with refcounter increased by 1.
433 */
434struct batadv_neigh_ifinfo *
435batadv_neigh_ifinfo_new(struct batadv_neigh_node *neigh,
436 struct batadv_hard_iface *if_outgoing)
437{
438 struct batadv_neigh_ifinfo *neigh_ifinfo;
439
440 spin_lock_bh(&neigh->ifinfo_lock);
441
442 neigh_ifinfo = batadv_neigh_ifinfo_get(neigh, if_outgoing);
443 if (neigh_ifinfo)
444 goto out;
445
446 neigh_ifinfo = kzalloc(sizeof(*neigh_ifinfo), GFP_ATOMIC);
447 if (!neigh_ifinfo)
448 goto out;
449
Sven Eckelmann17a86912016-04-11 13:06:40 +0200450 if (if_outgoing)
451 kref_get(&if_outgoing->refcount);
Simon Wunderlich89652332013-11-13 19:14:46 +0100452
453 INIT_HLIST_NODE(&neigh_ifinfo->list);
Sven Eckelmann962c6832016-01-16 10:29:51 +0100454 kref_init(&neigh_ifinfo->refcount);
455 kref_get(&neigh_ifinfo->refcount);
Simon Wunderlich89652332013-11-13 19:14:46 +0100456 neigh_ifinfo->if_outgoing = if_outgoing;
457
458 hlist_add_head_rcu(&neigh_ifinfo->list, &neigh->ifinfo_list);
459
460out:
461 spin_unlock_bh(&neigh->ifinfo_lock);
462
463 return neigh_ifinfo;
464}
465
466/**
Marek Lindnered292662015-08-04 23:31:44 +0800467 * batadv_neigh_node_get - retrieve a neighbour from the list
468 * @orig_node: originator which the neighbour belongs to
469 * @hard_iface: the interface where this neighbour is connected to
470 * @addr: the address of the neighbour
471 *
472 * Looks for and possibly returns a neighbour belonging to this originator list
473 * which is connected through the provided hard interface.
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200474 *
475 * Return: neighbor when found. Othwerwise NULL
Marek Lindnered292662015-08-04 23:31:44 +0800476 */
477static struct batadv_neigh_node *
478batadv_neigh_node_get(const struct batadv_orig_node *orig_node,
479 const struct batadv_hard_iface *hard_iface,
480 const u8 *addr)
481{
482 struct batadv_neigh_node *tmp_neigh_node, *res = NULL;
483
484 rcu_read_lock();
485 hlist_for_each_entry_rcu(tmp_neigh_node, &orig_node->neigh_list, list) {
486 if (!batadv_compare_eth(tmp_neigh_node->addr, addr))
487 continue;
488
489 if (tmp_neigh_node->if_incoming != hard_iface)
490 continue;
491
Sven Eckelmann77ae32e2016-01-16 10:29:53 +0100492 if (!kref_get_unless_zero(&tmp_neigh_node->refcount))
Marek Lindnered292662015-08-04 23:31:44 +0800493 continue;
494
495 res = tmp_neigh_node;
496 break;
497 }
498 rcu_read_unlock();
499
500 return res;
501}
502
503/**
Marek Lindnercef63412015-08-04 21:09:55 +0800504 * batadv_hardif_neigh_create - create a hardif neighbour node
505 * @hard_iface: the interface this neighbour is connected to
506 * @neigh_addr: the interface address of the neighbour to retrieve
507 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200508 * Return: the hardif neighbour node if found or created or NULL otherwise.
Marek Lindnercef63412015-08-04 21:09:55 +0800509 */
510static struct batadv_hardif_neigh_node *
511batadv_hardif_neigh_create(struct batadv_hard_iface *hard_iface,
512 const u8 *neigh_addr)
513{
Marek Lindner8248a4c2015-08-04 21:09:56 +0800514 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
Marek Lindnercef63412015-08-04 21:09:55 +0800515 struct batadv_hardif_neigh_node *hardif_neigh = NULL;
516
517 spin_lock_bh(&hard_iface->neigh_list_lock);
518
519 /* check if neighbor hasn't been added in the meantime */
520 hardif_neigh = batadv_hardif_neigh_get(hard_iface, neigh_addr);
521 if (hardif_neigh)
522 goto out;
523
Marek Lindnercef63412015-08-04 21:09:55 +0800524 hardif_neigh = kzalloc(sizeof(*hardif_neigh), GFP_ATOMIC);
Sven Eckelmann17a86912016-04-11 13:06:40 +0200525 if (!hardif_neigh)
Marek Lindnercef63412015-08-04 21:09:55 +0800526 goto out;
Marek Lindnercef63412015-08-04 21:09:55 +0800527
Sven Eckelmann17a86912016-04-11 13:06:40 +0200528 kref_get(&hard_iface->refcount);
Marek Lindnercef63412015-08-04 21:09:55 +0800529 INIT_HLIST_NODE(&hardif_neigh->list);
530 ether_addr_copy(hardif_neigh->addr, neigh_addr);
531 hardif_neigh->if_incoming = hard_iface;
532 hardif_neigh->last_seen = jiffies;
533
Sven Eckelmann90f564d2016-01-16 10:29:40 +0100534 kref_init(&hardif_neigh->refcount);
Marek Lindnercef63412015-08-04 21:09:55 +0800535
Marek Lindner8248a4c2015-08-04 21:09:56 +0800536 if (bat_priv->bat_algo_ops->bat_hardif_neigh_init)
537 bat_priv->bat_algo_ops->bat_hardif_neigh_init(hardif_neigh);
538
Marek Lindnercef63412015-08-04 21:09:55 +0800539 hlist_add_head(&hardif_neigh->list, &hard_iface->neigh_list);
540
541out:
542 spin_unlock_bh(&hard_iface->neigh_list_lock);
543 return hardif_neigh;
544}
545
546/**
547 * batadv_hardif_neigh_get_or_create - retrieve or create a hardif neighbour
548 * node
549 * @hard_iface: the interface this neighbour is connected to
550 * @neigh_addr: the interface address of the neighbour to retrieve
551 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200552 * Return: the hardif neighbour node if found or created or NULL otherwise.
Marek Lindnercef63412015-08-04 21:09:55 +0800553 */
554static struct batadv_hardif_neigh_node *
555batadv_hardif_neigh_get_or_create(struct batadv_hard_iface *hard_iface,
556 const u8 *neigh_addr)
557{
558 struct batadv_hardif_neigh_node *hardif_neigh = NULL;
559
560 /* first check without locking to avoid the overhead */
561 hardif_neigh = batadv_hardif_neigh_get(hard_iface, neigh_addr);
562 if (hardif_neigh)
563 return hardif_neigh;
564
565 return batadv_hardif_neigh_create(hard_iface, neigh_addr);
566}
567
568/**
569 * batadv_hardif_neigh_get - retrieve a hardif neighbour from the list
570 * @hard_iface: the interface where this neighbour is connected to
571 * @neigh_addr: the address of the neighbour
572 *
573 * Looks for and possibly returns a neighbour belonging to this hard interface.
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200574 *
575 * Return: neighbor when found. Othwerwise NULL
Marek Lindnercef63412015-08-04 21:09:55 +0800576 */
577struct batadv_hardif_neigh_node *
578batadv_hardif_neigh_get(const struct batadv_hard_iface *hard_iface,
579 const u8 *neigh_addr)
580{
581 struct batadv_hardif_neigh_node *tmp_hardif_neigh, *hardif_neigh = NULL;
582
583 rcu_read_lock();
584 hlist_for_each_entry_rcu(tmp_hardif_neigh,
585 &hard_iface->neigh_list, list) {
586 if (!batadv_compare_eth(tmp_hardif_neigh->addr, neigh_addr))
587 continue;
588
Sven Eckelmann90f564d2016-01-16 10:29:40 +0100589 if (!kref_get_unless_zero(&tmp_hardif_neigh->refcount))
Marek Lindnercef63412015-08-04 21:09:55 +0800590 continue;
591
592 hardif_neigh = tmp_hardif_neigh;
593 break;
594 }
595 rcu_read_unlock();
596
597 return hardif_neigh;
598}
599
600/**
Marek Lindner6f0a6b52016-05-03 01:52:08 +0800601 * batadv_neigh_node_create - create a neigh node object
Marek Lindner3f32f8a2015-07-26 04:59:15 +0800602 * @orig_node: originator object representing the neighbour
Antonio Quartulli0538f7592013-09-02 12:15:01 +0200603 * @hard_iface: the interface where the neighbour is connected to
604 * @neigh_addr: the mac address of the neighbour interface
Antonio Quartulli0538f7592013-09-02 12:15:01 +0200605 *
606 * Allocates a new neigh_node object and initialises all the generic fields.
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200607 *
Marek Lindner6f0a6b52016-05-03 01:52:08 +0800608 * Return: the neighbour node if found or created or NULL otherwise.
Antonio Quartulli0538f7592013-09-02 12:15:01 +0200609 */
Marek Lindner6f0a6b52016-05-03 01:52:08 +0800610static struct batadv_neigh_node *
611batadv_neigh_node_create(struct batadv_orig_node *orig_node,
612 struct batadv_hard_iface *hard_iface,
613 const u8 *neigh_addr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000614{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200615 struct batadv_neigh_node *neigh_node;
Marek Lindnercef63412015-08-04 21:09:55 +0800616 struct batadv_hardif_neigh_node *hardif_neigh = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000617
Linus Lüssinge1237052016-01-07 08:11:12 +0100618 spin_lock_bh(&orig_node->neigh_list_lock);
619
Marek Lindner741aa062015-07-26 04:57:43 +0800620 neigh_node = batadv_neigh_node_get(orig_node, hard_iface, neigh_addr);
621 if (neigh_node)
622 goto out;
623
Marek Lindnercef63412015-08-04 21:09:55 +0800624 hardif_neigh = batadv_hardif_neigh_get_or_create(hard_iface,
625 neigh_addr);
626 if (!hardif_neigh)
627 goto out;
628
Sven Eckelmann704509b2011-05-14 23:14:54 +0200629 neigh_node = kzalloc(sizeof(*neigh_node), GFP_ATOMIC);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000630 if (!neigh_node)
Marek Lindner7ae8b282012-03-01 15:35:21 +0800631 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000632
Marek Lindner9591a792010-12-12 21:57:11 +0000633 INIT_HLIST_NODE(&neigh_node->list);
Simon Wunderlich89652332013-11-13 19:14:46 +0100634 INIT_HLIST_HEAD(&neigh_node->ifinfo_list);
635 spin_lock_init(&neigh_node->ifinfo_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000636
Sven Eckelmann17a86912016-04-11 13:06:40 +0200637 kref_get(&hard_iface->refcount);
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100638 ether_addr_copy(neigh_node->addr, neigh_addr);
Antonio Quartulli0538f7592013-09-02 12:15:01 +0200639 neigh_node->if_incoming = hard_iface;
640 neigh_node->orig_node = orig_node;
Marek Lindnere48474e2016-03-11 16:01:09 +0100641 neigh_node->last_seen = jiffies;
Antonio Quartulli0538f7592013-09-02 12:15:01 +0200642
Sven Eckelmannabe59c62016-03-11 16:44:06 +0100643 /* increment unique neighbor refcount */
644 kref_get(&hardif_neigh->refcount);
645 neigh_node->hardif_neigh = hardif_neigh;
646
Marek Lindner1605d0d2011-02-18 12:28:11 +0000647 /* extra reference for return */
Sven Eckelmann77ae32e2016-01-16 10:29:53 +0100648 kref_init(&neigh_node->refcount);
649 kref_get(&neigh_node->refcount);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000650
Marek Lindner741aa062015-07-26 04:57:43 +0800651 hlist_add_head_rcu(&neigh_node->list, &orig_node->neigh_list);
Marek Lindner741aa062015-07-26 04:57:43 +0800652
653 batadv_dbg(BATADV_DBG_BATMAN, orig_node->bat_priv,
654 "Creating new neighbor %pM for orig_node %pM on interface %s\n",
655 neigh_addr, orig_node->orig, hard_iface->net_dev->name);
656
Marek Lindner7ae8b282012-03-01 15:35:21 +0800657out:
Linus Lüssinge1237052016-01-07 08:11:12 +0100658 spin_unlock_bh(&orig_node->neigh_list_lock);
659
Marek Lindnercef63412015-08-04 21:09:55 +0800660 if (hardif_neigh)
Sven Eckelmannaccadc32016-01-17 11:01:14 +0100661 batadv_hardif_neigh_put(hardif_neigh);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000662 return neigh_node;
663}
664
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100665/**
Marek Lindner6f0a6b52016-05-03 01:52:08 +0800666 * batadv_neigh_node_get_or_create - retrieve or create a neigh node object
667 * @orig_node: originator object representing the neighbour
668 * @hard_iface: the interface where the neighbour is connected to
669 * @neigh_addr: the mac address of the neighbour interface
670 *
671 * Return: the neighbour node if found or created or NULL otherwise.
672 */
673struct batadv_neigh_node *
674batadv_neigh_node_get_or_create(struct batadv_orig_node *orig_node,
675 struct batadv_hard_iface *hard_iface,
676 const u8 *neigh_addr)
677{
678 struct batadv_neigh_node *neigh_node = NULL;
679
680 /* first check without locking to avoid the overhead */
681 neigh_node = batadv_neigh_node_get(orig_node, hard_iface, neigh_addr);
682 if (neigh_node)
683 return neigh_node;
684
685 return batadv_neigh_node_create(orig_node, hard_iface, neigh_addr);
686}
687
688/**
Marek Lindner75874052015-08-04 21:09:57 +0800689 * batadv_hardif_neigh_seq_print_text - print the single hop neighbour list
690 * @seq: neighbour table seq_file struct
691 * @offset: not used
692 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200693 * Return: always 0
Marek Lindner75874052015-08-04 21:09:57 +0800694 */
695int batadv_hardif_neigh_seq_print_text(struct seq_file *seq, void *offset)
696{
697 struct net_device *net_dev = (struct net_device *)seq->private;
698 struct batadv_priv *bat_priv = netdev_priv(net_dev);
699 struct batadv_hard_iface *primary_if;
700
701 primary_if = batadv_seq_print_text_primary_if_get(seq);
702 if (!primary_if)
703 return 0;
704
705 seq_printf(seq, "[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s %s)]\n",
706 BATADV_SOURCE_VERSION, primary_if->net_dev->name,
707 primary_if->net_dev->dev_addr, net_dev->name,
708 bat_priv->bat_algo_ops->name);
709
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100710 batadv_hardif_put(primary_if);
Marek Lindner75874052015-08-04 21:09:57 +0800711
712 if (!bat_priv->bat_algo_ops->bat_neigh_print) {
713 seq_puts(seq,
714 "No printing function for this routing protocol\n");
715 return 0;
716 }
717
718 bat_priv->bat_algo_ops->bat_neigh_print(bat_priv, seq);
719 return 0;
720}
721
722/**
Sven Eckelmann2baa7532016-01-05 12:06:22 +0100723 * batadv_orig_ifinfo_release - release orig_ifinfo from lists and queue for
724 * free after rcu grace period
Sven Eckelmanna6ba0d32016-01-16 10:29:52 +0100725 * @ref: kref pointer of the orig_ifinfo
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100726 */
Sven Eckelmanna6ba0d32016-01-16 10:29:52 +0100727static void batadv_orig_ifinfo_release(struct kref *ref)
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100728{
Sven Eckelmanna6ba0d32016-01-16 10:29:52 +0100729 struct batadv_orig_ifinfo *orig_ifinfo;
Simon Wunderlich000c8df2014-03-26 15:46:22 +0100730 struct batadv_neigh_node *router;
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100731
Sven Eckelmanna6ba0d32016-01-16 10:29:52 +0100732 orig_ifinfo = container_of(ref, struct batadv_orig_ifinfo, refcount);
733
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100734 if (orig_ifinfo->if_outgoing != BATADV_IF_DEFAULT)
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100735 batadv_hardif_put(orig_ifinfo->if_outgoing);
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100736
Simon Wunderlich000c8df2014-03-26 15:46:22 +0100737 /* this is the last reference to this object */
738 router = rcu_dereference_protected(orig_ifinfo->router, true);
739 if (router)
Sven Eckelmann25bb2502016-01-17 11:01:11 +0100740 batadv_neigh_node_put(router);
Sven Eckelmann2baa7532016-01-05 12:06:22 +0100741
742 kfree_rcu(orig_ifinfo, rcu);
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100743}
744
745/**
Sven Eckelmann35f94772016-01-17 11:01:13 +0100746 * batadv_orig_ifinfo_put - decrement the refcounter and possibly release
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100747 * the orig_ifinfo
748 * @orig_ifinfo: the orig_ifinfo object to release
749 */
Sven Eckelmann35f94772016-01-17 11:01:13 +0100750void batadv_orig_ifinfo_put(struct batadv_orig_ifinfo *orig_ifinfo)
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100751{
Sven Eckelmanna6ba0d32016-01-16 10:29:52 +0100752 kref_put(&orig_ifinfo->refcount, batadv_orig_ifinfo_release);
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100753}
754
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100755/**
756 * batadv_orig_node_free_rcu - free the orig_node
757 * @rcu: rcu pointer of the orig_node
758 */
Sven Eckelmann03fc7f82012-05-12 18:34:00 +0200759static void batadv_orig_node_free_rcu(struct rcu_head *rcu)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000760{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200761 struct batadv_orig_node *orig_node;
Marek Lindner16b1aba2011-01-19 20:01:42 +0000762
Sven Eckelmann56303d32012-06-05 22:31:31 +0200763 orig_node = container_of(rcu, struct batadv_orig_node, rcu);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000764
Linus Lüssing60432d72014-02-15 17:47:51 +0100765 batadv_mcast_purge_orig(orig_node);
766
Martin Hundebøll610bfc6bc2013-05-23 16:53:02 +0200767 batadv_frag_purge_orig(orig_node, NULL);
768
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200769 if (orig_node->bat_priv->bat_algo_ops->bat_orig_free)
770 orig_node->bat_priv->bat_algo_ops->bat_orig_free(orig_node);
771
Antonio Quartullia73105b2011-04-27 14:27:44 +0200772 kfree(orig_node->tt_buff);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000773 kfree(orig_node);
774}
775
Linus Lüssing72822222013-04-15 21:43:29 +0800776/**
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100777 * batadv_orig_node_release - release orig_node from lists and queue for
778 * free after rcu grace period
Sven Eckelmann7c124392016-01-16 10:29:56 +0100779 * @ref: kref pointer of the orig_node
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100780 */
Sven Eckelmann7c124392016-01-16 10:29:56 +0100781static void batadv_orig_node_release(struct kref *ref)
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100782{
783 struct hlist_node *node_tmp;
784 struct batadv_neigh_node *neigh_node;
Sven Eckelmann7c124392016-01-16 10:29:56 +0100785 struct batadv_orig_node *orig_node;
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100786 struct batadv_orig_ifinfo *orig_ifinfo;
787
Sven Eckelmann7c124392016-01-16 10:29:56 +0100788 orig_node = container_of(ref, struct batadv_orig_node, refcount);
789
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100790 spin_lock_bh(&orig_node->neigh_list_lock);
791
792 /* for all neighbors towards this originator ... */
793 hlist_for_each_entry_safe(neigh_node, node_tmp,
794 &orig_node->neigh_list, list) {
795 hlist_del_rcu(&neigh_node->list);
Sven Eckelmann25bb2502016-01-17 11:01:11 +0100796 batadv_neigh_node_put(neigh_node);
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100797 }
798
799 hlist_for_each_entry_safe(orig_ifinfo, node_tmp,
800 &orig_node->ifinfo_list, list) {
801 hlist_del_rcu(&orig_ifinfo->list);
Sven Eckelmann35f94772016-01-17 11:01:13 +0100802 batadv_orig_ifinfo_put(orig_ifinfo);
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100803 }
804 spin_unlock_bh(&orig_node->neigh_list_lock);
805
806 /* Free nc_nodes */
807 batadv_nc_purge_orig(orig_node->bat_priv, orig_node, NULL);
808
809 call_rcu(&orig_node->rcu, batadv_orig_node_free_rcu);
810}
811
812/**
Sven Eckelmann5d967312016-01-17 11:01:09 +0100813 * batadv_orig_node_put - decrement the orig node refcounter and possibly
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100814 * release it
Linus Lüssing72822222013-04-15 21:43:29 +0800815 * @orig_node: the orig node to free
816 */
Sven Eckelmann5d967312016-01-17 11:01:09 +0100817void batadv_orig_node_put(struct batadv_orig_node *orig_node)
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000818{
Sven Eckelmann7c124392016-01-16 10:29:56 +0100819 kref_put(&orig_node->refcount, batadv_orig_node_release);
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000820}
821
Sven Eckelmann56303d32012-06-05 22:31:31 +0200822void batadv_originator_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000823{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200824 struct batadv_hashtable *hash = bat_priv->orig_hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800825 struct hlist_node *node_tmp;
Marek Lindner16b1aba2011-01-19 20:01:42 +0000826 struct hlist_head *head;
Marek Lindner16b1aba2011-01-19 20:01:42 +0000827 spinlock_t *list_lock; /* spinlock to protect write access */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200828 struct batadv_orig_node *orig_node;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200829 u32 i;
Marek Lindner16b1aba2011-01-19 20:01:42 +0000830
831 if (!hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000832 return;
833
834 cancel_delayed_work_sync(&bat_priv->orig_work);
835
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000836 bat_priv->orig_hash = NULL;
Marek Lindner16b1aba2011-01-19 20:01:42 +0000837
838 for (i = 0; i < hash->size; i++) {
839 head = &hash->table[i];
840 list_lock = &hash->list_locks[i];
841
842 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800843 hlist_for_each_entry_safe(orig_node, node_tmp,
Marek Lindner7aadf882011-02-18 12:28:09 +0000844 head, hash_entry) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800845 hlist_del_rcu(&orig_node->hash_entry);
Sven Eckelmann5d967312016-01-17 11:01:09 +0100846 batadv_orig_node_put(orig_node);
Marek Lindner16b1aba2011-01-19 20:01:42 +0000847 }
848 spin_unlock_bh(list_lock);
849 }
850
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +0200851 batadv_hash_destroy(hash);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000852}
853
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200854/**
855 * batadv_orig_node_new - creates a new orig_node
856 * @bat_priv: the bat priv with all the soft interface information
857 * @addr: the mac address of the originator
858 *
859 * Creates a new originator object and initialise all the generic fields.
860 * The new object is not added to the originator list.
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200861 *
862 * Return: the newly created object or NULL on failure.
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200863 */
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200864struct batadv_orig_node *batadv_orig_node_new(struct batadv_priv *bat_priv,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200865 const u8 *addr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000866{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200867 struct batadv_orig_node *orig_node;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200868 struct batadv_orig_node_vlan *vlan;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200869 unsigned long reset_time;
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200870 int i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000871
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200872 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
873 "Creating new originator: %pM\n", addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000874
Sven Eckelmann704509b2011-05-14 23:14:54 +0200875 orig_node = kzalloc(sizeof(*orig_node), GFP_ATOMIC);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000876 if (!orig_node)
877 return NULL;
878
Marek Lindner9591a792010-12-12 21:57:11 +0000879 INIT_HLIST_HEAD(&orig_node->neigh_list);
Marek Lindnerd0fa4f32015-06-22 00:30:22 +0800880 INIT_HLIST_HEAD(&orig_node->vlan_list);
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100881 INIT_HLIST_HEAD(&orig_node->ifinfo_list);
Marek Lindnerf3e00082011-01-25 21:52:11 +0000882 spin_lock_init(&orig_node->bcast_seqno_lock);
Marek Lindnerf987ed62010-12-12 21:57:12 +0000883 spin_lock_init(&orig_node->neigh_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200884 spin_lock_init(&orig_node->tt_buff_lock);
Antonio Quartullia70a9aa2013-07-30 22:16:24 +0200885 spin_lock_init(&orig_node->tt_lock);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200886 spin_lock_init(&orig_node->vlan_list_lock);
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000887
Martin Hundebølld56b1702013-01-25 11:12:39 +0100888 batadv_nc_init_orig(orig_node);
889
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000890 /* extra reference for return */
Sven Eckelmann7c124392016-01-16 10:29:56 +0100891 kref_init(&orig_node->refcount);
892 kref_get(&orig_node->refcount);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000893
Marek Lindner16b1aba2011-01-19 20:01:42 +0000894 orig_node->bat_priv = bat_priv;
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100895 ether_addr_copy(orig_node->orig, addr);
Antonio Quartulli785ea112011-11-23 11:35:44 +0100896 batadv_dat_init_orig_node_addr(orig_node);
Antonio Quartullic8c991b2011-07-07 01:40:57 +0200897 atomic_set(&orig_node->last_ttvn, 0);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200898 orig_node->tt_buff = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200899 orig_node->tt_buff_len = 0;
Linus Lüssing2c667a32014-10-30 06:23:40 +0100900 orig_node->last_seen = jiffies;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200901 reset_time = jiffies - 1 - msecs_to_jiffies(BATADV_RESET_PROTECTION_MS);
902 orig_node->bcast_seqno_reset = reset_time;
Linus Lüssing8a4023c2015-06-16 17:10:26 +0200903
Linus Lüssing60432d72014-02-15 17:47:51 +0100904#ifdef CONFIG_BATMAN_ADV_MCAST
905 orig_node->mcast_flags = BATADV_NO_FLAGS;
Linus Lüssing8a4023c2015-06-16 17:10:26 +0200906 INIT_HLIST_NODE(&orig_node->mcast_want_all_unsnoopables_node);
907 INIT_HLIST_NODE(&orig_node->mcast_want_all_ipv4_node);
908 INIT_HLIST_NODE(&orig_node->mcast_want_all_ipv6_node);
909 spin_lock_init(&orig_node->mcast_handler_lock);
Linus Lüssing60432d72014-02-15 17:47:51 +0100910#endif
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000911
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200912 /* create a vlan object for the "untagged" LAN */
913 vlan = batadv_orig_node_vlan_new(orig_node, BATADV_NO_FLAGS);
914 if (!vlan)
915 goto free_orig_node;
916 /* batadv_orig_node_vlan_new() increases the refcounter.
917 * Immediately release vlan since it is not needed anymore in this
918 * context
919 */
Sven Eckelmann21754e22016-01-17 11:01:24 +0100920 batadv_orig_node_vlan_put(vlan);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200921
Martin Hundebøll610bfc6bc2013-05-23 16:53:02 +0200922 for (i = 0; i < BATADV_FRAG_BUFFER_COUNT; i++) {
923 INIT_HLIST_HEAD(&orig_node->fragments[i].head);
924 spin_lock_init(&orig_node->fragments[i].lock);
925 orig_node->fragments[i].size = 0;
926 }
927
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000928 return orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000929free_orig_node:
930 kfree(orig_node);
931 return NULL;
932}
933
Simon Wunderlich89652332013-11-13 19:14:46 +0100934/**
Simon Wunderlich709de132014-03-26 15:46:24 +0100935 * batadv_purge_neigh_ifinfo - purge obsolete ifinfo entries from neighbor
936 * @bat_priv: the bat priv with all the soft interface information
937 * @neigh: orig node which is to be checked
938 */
939static void
940batadv_purge_neigh_ifinfo(struct batadv_priv *bat_priv,
941 struct batadv_neigh_node *neigh)
942{
943 struct batadv_neigh_ifinfo *neigh_ifinfo;
944 struct batadv_hard_iface *if_outgoing;
945 struct hlist_node *node_tmp;
946
947 spin_lock_bh(&neigh->ifinfo_lock);
948
949 /* for all ifinfo objects for this neighinator */
950 hlist_for_each_entry_safe(neigh_ifinfo, node_tmp,
951 &neigh->ifinfo_list, list) {
952 if_outgoing = neigh_ifinfo->if_outgoing;
953
954 /* always keep the default interface */
955 if (if_outgoing == BATADV_IF_DEFAULT)
956 continue;
957
958 /* don't purge if the interface is not (going) down */
959 if ((if_outgoing->if_status != BATADV_IF_INACTIVE) &&
960 (if_outgoing->if_status != BATADV_IF_NOT_IN_USE) &&
961 (if_outgoing->if_status != BATADV_IF_TO_BE_REMOVED))
962 continue;
963
964 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
965 "neighbor/ifinfo purge: neighbor %pM, iface: %s\n",
966 neigh->addr, if_outgoing->net_dev->name);
967
968 hlist_del_rcu(&neigh_ifinfo->list);
Sven Eckelmann044fa3a2016-01-17 11:01:12 +0100969 batadv_neigh_ifinfo_put(neigh_ifinfo);
Simon Wunderlich709de132014-03-26 15:46:24 +0100970 }
971
972 spin_unlock_bh(&neigh->ifinfo_lock);
973}
974
975/**
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100976 * batadv_purge_orig_ifinfo - purge obsolete ifinfo entries from originator
977 * @bat_priv: the bat priv with all the soft interface information
978 * @orig_node: orig node which is to be checked
979 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200980 * Return: true if any ifinfo entry was purged, false otherwise.
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100981 */
982static bool
983batadv_purge_orig_ifinfo(struct batadv_priv *bat_priv,
984 struct batadv_orig_node *orig_node)
985{
986 struct batadv_orig_ifinfo *orig_ifinfo;
987 struct batadv_hard_iface *if_outgoing;
988 struct hlist_node *node_tmp;
989 bool ifinfo_purged = false;
990
991 spin_lock_bh(&orig_node->neigh_list_lock);
992
993 /* for all ifinfo objects for this originator */
994 hlist_for_each_entry_safe(orig_ifinfo, node_tmp,
995 &orig_node->ifinfo_list, list) {
996 if_outgoing = orig_ifinfo->if_outgoing;
997
998 /* always keep the default interface */
999 if (if_outgoing == BATADV_IF_DEFAULT)
1000 continue;
1001
1002 /* don't purge if the interface is not (going) down */
1003 if ((if_outgoing->if_status != BATADV_IF_INACTIVE) &&
1004 (if_outgoing->if_status != BATADV_IF_NOT_IN_USE) &&
1005 (if_outgoing->if_status != BATADV_IF_TO_BE_REMOVED))
1006 continue;
1007
1008 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1009 "router/ifinfo purge: originator %pM, iface: %s\n",
1010 orig_node->orig, if_outgoing->net_dev->name);
1011
1012 ifinfo_purged = true;
1013
1014 hlist_del_rcu(&orig_ifinfo->list);
Sven Eckelmann35f94772016-01-17 11:01:13 +01001015 batadv_orig_ifinfo_put(orig_ifinfo);
Simon Wunderlichf3b3d902013-11-13 19:14:50 +01001016 if (orig_node->last_bonding_candidate == orig_ifinfo) {
1017 orig_node->last_bonding_candidate = NULL;
Sven Eckelmann35f94772016-01-17 11:01:13 +01001018 batadv_orig_ifinfo_put(orig_ifinfo);
Simon Wunderlichf3b3d902013-11-13 19:14:50 +01001019 }
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001020 }
1021
1022 spin_unlock_bh(&orig_node->neigh_list_lock);
1023
1024 return ifinfo_purged;
1025}
1026
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001027/**
Simon Wunderlich89652332013-11-13 19:14:46 +01001028 * batadv_purge_orig_neighbors - purges neighbors from originator
1029 * @bat_priv: the bat priv with all the soft interface information
1030 * @orig_node: orig node which is to be checked
1031 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001032 * Return: true if any neighbor was purged, false otherwise
Simon Wunderlich89652332013-11-13 19:14:46 +01001033 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001034static bool
1035batadv_purge_orig_neighbors(struct batadv_priv *bat_priv,
Simon Wunderlich89652332013-11-13 19:14:46 +01001036 struct batadv_orig_node *orig_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001037{
Sasha Levinb67bfe02013-02-27 17:06:00 -08001038 struct hlist_node *node_tmp;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001039 struct batadv_neigh_node *neigh_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001040 bool neigh_purged = false;
Marek Lindner0b0094e2012-03-01 15:35:20 +08001041 unsigned long last_seen;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001042 struct batadv_hard_iface *if_incoming;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001043
Marek Lindnerf987ed62010-12-12 21:57:12 +00001044 spin_lock_bh(&orig_node->neigh_list_lock);
1045
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001046 /* for all neighbors towards this originator ... */
Sasha Levinb67bfe02013-02-27 17:06:00 -08001047 hlist_for_each_entry_safe(neigh_node, node_tmp,
Marek Lindner9591a792010-12-12 21:57:11 +00001048 &orig_node->neigh_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001049 last_seen = neigh_node->last_seen;
1050 if_incoming = neigh_node->if_incoming;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001051
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001052 if ((batadv_has_timed_out(last_seen, BATADV_PURGE_TIMEOUT)) ||
Sven Eckelmanne9a4f292012-06-03 22:19:19 +02001053 (if_incoming->if_status == BATADV_IF_INACTIVE) ||
1054 (if_incoming->if_status == BATADV_IF_NOT_IN_USE) ||
1055 (if_incoming->if_status == BATADV_IF_TO_BE_REMOVED)) {
Sven Eckelmanne9a4f292012-06-03 22:19:19 +02001056 if ((if_incoming->if_status == BATADV_IF_INACTIVE) ||
1057 (if_incoming->if_status == BATADV_IF_NOT_IN_USE) ||
1058 (if_incoming->if_status == BATADV_IF_TO_BE_REMOVED))
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001059 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001060 "neighbor purge: originator %pM, neighbor: %pM, iface: %s\n",
1061 orig_node->orig, neigh_node->addr,
1062 if_incoming->net_dev->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001063 else
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001064 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001065 "neighbor timeout: originator %pM, neighbor: %pM, last_seen: %u\n",
1066 orig_node->orig, neigh_node->addr,
1067 jiffies_to_msecs(last_seen));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001068
1069 neigh_purged = true;
Marek Lindner9591a792010-12-12 21:57:11 +00001070
Marek Lindnerf987ed62010-12-12 21:57:12 +00001071 hlist_del_rcu(&neigh_node->list);
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001072 batadv_neigh_node_put(neigh_node);
Simon Wunderlich709de132014-03-26 15:46:24 +01001073 } else {
1074 /* only necessary if not the whole neighbor is to be
1075 * deleted, but some interface has been removed.
1076 */
1077 batadv_purge_neigh_ifinfo(bat_priv, neigh_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001078 }
1079 }
Marek Lindnerf987ed62010-12-12 21:57:12 +00001080
1081 spin_unlock_bh(&orig_node->neigh_list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001082 return neigh_purged;
1083}
1084
Simon Wunderlich89652332013-11-13 19:14:46 +01001085/**
1086 * batadv_find_best_neighbor - finds the best neighbor after purging
1087 * @bat_priv: the bat priv with all the soft interface information
1088 * @orig_node: orig node which is to be checked
1089 * @if_outgoing: the interface for which the metric should be compared
1090 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001091 * Return: the current best neighbor, with refcount increased.
Simon Wunderlich89652332013-11-13 19:14:46 +01001092 */
1093static struct batadv_neigh_node *
1094batadv_find_best_neighbor(struct batadv_priv *bat_priv,
1095 struct batadv_orig_node *orig_node,
1096 struct batadv_hard_iface *if_outgoing)
1097{
1098 struct batadv_neigh_node *best = NULL, *neigh;
1099 struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
1100
1101 rcu_read_lock();
1102 hlist_for_each_entry_rcu(neigh, &orig_node->neigh_list, list) {
1103 if (best && (bao->bat_neigh_cmp(neigh, if_outgoing,
1104 best, if_outgoing) <= 0))
1105 continue;
1106
Sven Eckelmann77ae32e2016-01-16 10:29:53 +01001107 if (!kref_get_unless_zero(&neigh->refcount))
Simon Wunderlich89652332013-11-13 19:14:46 +01001108 continue;
1109
1110 if (best)
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001111 batadv_neigh_node_put(best);
Simon Wunderlich89652332013-11-13 19:14:46 +01001112
1113 best = neigh;
1114 }
1115 rcu_read_unlock();
1116
1117 return best;
1118}
1119
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001120/**
1121 * batadv_purge_orig_node - purges obsolete information from an orig_node
1122 * @bat_priv: the bat priv with all the soft interface information
1123 * @orig_node: orig node which is to be checked
1124 *
1125 * This function checks if the orig_node or substructures of it have become
1126 * obsolete, and purges this information if that's the case.
1127 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001128 * Return: true if the orig_node is to be removed, false otherwise.
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001129 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001130static bool batadv_purge_orig_node(struct batadv_priv *bat_priv,
1131 struct batadv_orig_node *orig_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001132{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001133 struct batadv_neigh_node *best_neigh_node;
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001134 struct batadv_hard_iface *hard_iface;
Simon Wunderlich7b955a92014-03-26 15:46:23 +01001135 bool changed_ifinfo, changed_neigh;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001136
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001137 if (batadv_has_timed_out(orig_node->last_seen,
1138 2 * BATADV_PURGE_TIMEOUT)) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001139 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001140 "Originator timeout: originator %pM, last_seen %u\n",
1141 orig_node->orig,
1142 jiffies_to_msecs(orig_node->last_seen));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001143 return true;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001144 }
Simon Wunderlich7b955a92014-03-26 15:46:23 +01001145 changed_ifinfo = batadv_purge_orig_ifinfo(bat_priv, orig_node);
1146 changed_neigh = batadv_purge_orig_neighbors(bat_priv, orig_node);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001147
Simon Wunderlich7b955a92014-03-26 15:46:23 +01001148 if (!changed_ifinfo && !changed_neigh)
Simon Wunderlich89652332013-11-13 19:14:46 +01001149 return false;
1150
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001151 /* first for NULL ... */
Simon Wunderlich89652332013-11-13 19:14:46 +01001152 best_neigh_node = batadv_find_best_neighbor(bat_priv, orig_node,
1153 BATADV_IF_DEFAULT);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001154 batadv_update_route(bat_priv, orig_node, BATADV_IF_DEFAULT,
1155 best_neigh_node);
Simon Wunderlich89652332013-11-13 19:14:46 +01001156 if (best_neigh_node)
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001157 batadv_neigh_node_put(best_neigh_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001158
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001159 /* ... then for all other interfaces. */
1160 rcu_read_lock();
1161 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
1162 if (hard_iface->if_status != BATADV_IF_ACTIVE)
1163 continue;
1164
1165 if (hard_iface->soft_iface != bat_priv->soft_iface)
1166 continue;
1167
Sven Eckelmann27353442016-03-05 16:09:16 +01001168 if (!kref_get_unless_zero(&hard_iface->refcount))
1169 continue;
1170
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001171 best_neigh_node = batadv_find_best_neighbor(bat_priv,
1172 orig_node,
1173 hard_iface);
1174 batadv_update_route(bat_priv, orig_node, hard_iface,
1175 best_neigh_node);
1176 if (best_neigh_node)
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001177 batadv_neigh_node_put(best_neigh_node);
Sven Eckelmann27353442016-03-05 16:09:16 +01001178
1179 batadv_hardif_put(hard_iface);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001180 }
1181 rcu_read_unlock();
1182
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001183 return false;
1184}
1185
Sven Eckelmann56303d32012-06-05 22:31:31 +02001186static void _batadv_purge_orig(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001187{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001188 struct batadv_hashtable *hash = bat_priv->orig_hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001189 struct hlist_node *node_tmp;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001190 struct hlist_head *head;
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001191 spinlock_t *list_lock; /* spinlock to protect write access */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001192 struct batadv_orig_node *orig_node;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001193 u32 i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001194
1195 if (!hash)
1196 return;
1197
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001198 /* for all origins... */
1199 for (i = 0; i < hash->size; i++) {
1200 head = &hash->table[i];
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001201 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001202
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001203 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001204 hlist_for_each_entry_safe(orig_node, node_tmp,
Marek Lindner7aadf882011-02-18 12:28:09 +00001205 head, hash_entry) {
Sven Eckelmann03fc7f82012-05-12 18:34:00 +02001206 if (batadv_purge_orig_node(bat_priv, orig_node)) {
Marek Lindner414254e2013-04-23 21:39:58 +08001207 batadv_gw_node_delete(bat_priv, orig_node);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001208 hlist_del_rcu(&orig_node->hash_entry);
Linus Lüssing9d31b3c2014-12-13 23:32:15 +01001209 batadv_tt_global_del_orig(orig_node->bat_priv,
1210 orig_node, -1,
1211 "originator timed out");
Sven Eckelmann5d967312016-01-17 11:01:09 +01001212 batadv_orig_node_put(orig_node);
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001213 continue;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001214 }
Martin Hundebøll610bfc6bc2013-05-23 16:53:02 +02001215
1216 batadv_frag_purge_orig(orig_node,
1217 batadv_frag_check_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001218 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001219 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001220 }
1221
Sven Eckelmann7cf06bc2012-05-12 02:09:29 +02001222 batadv_gw_election(bat_priv);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001223}
1224
Sven Eckelmann03fc7f82012-05-12 18:34:00 +02001225static void batadv_purge_orig(struct work_struct *work)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001226{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001227 struct delayed_work *delayed_work;
1228 struct batadv_priv *bat_priv;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001229
Geliang Tang4ba4bc02015-12-28 23:43:37 +08001230 delayed_work = to_delayed_work(work);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001231 bat_priv = container_of(delayed_work, struct batadv_priv, orig_work);
Sven Eckelmann03fc7f82012-05-12 18:34:00 +02001232 _batadv_purge_orig(bat_priv);
Antonio Quartulli72414442012-12-25 13:14:37 +01001233 queue_delayed_work(batadv_event_workqueue,
1234 &bat_priv->orig_work,
1235 msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001236}
1237
Sven Eckelmann56303d32012-06-05 22:31:31 +02001238void batadv_purge_orig_ref(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001239{
Sven Eckelmann03fc7f82012-05-12 18:34:00 +02001240 _batadv_purge_orig(bat_priv);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001241}
1242
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001243int batadv_orig_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001244{
1245 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001246 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001247 struct batadv_hard_iface *primary_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001248
Marek Lindner30da63a2012-08-03 17:15:46 +02001249 primary_if = batadv_seq_print_text_primary_if_get(seq);
1250 if (!primary_if)
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001251 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001252
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001253 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 +02001254 BATADV_SOURCE_VERSION, primary_if->net_dev->name,
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001255 primary_if->net_dev->dev_addr, net_dev->name,
1256 bat_priv->bat_algo_ops->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001257
Sven Eckelmann82047ad2016-01-17 11:01:10 +01001258 batadv_hardif_put(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001259
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001260 if (!bat_priv->bat_algo_ops->bat_orig_print) {
1261 seq_puts(seq,
1262 "No printing function for this routing protocol\n");
1263 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001264 }
1265
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001266 bat_priv->bat_algo_ops->bat_orig_print(bat_priv, seq,
1267 BATADV_IF_DEFAULT);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001268
Marek Lindner30da63a2012-08-03 17:15:46 +02001269 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001270}
1271
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001272/**
1273 * batadv_orig_hardif_seq_print_text - writes originator infos for a specific
1274 * outgoing interface
1275 * @seq: debugfs table seq_file struct
1276 * @offset: not used
1277 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001278 * Return: 0
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001279 */
1280int batadv_orig_hardif_seq_print_text(struct seq_file *seq, void *offset)
1281{
1282 struct net_device *net_dev = (struct net_device *)seq->private;
1283 struct batadv_hard_iface *hard_iface;
1284 struct batadv_priv *bat_priv;
1285
1286 hard_iface = batadv_hardif_get_by_netdev(net_dev);
1287
1288 if (!hard_iface || !hard_iface->soft_iface) {
1289 seq_puts(seq, "Interface not known to B.A.T.M.A.N.\n");
1290 goto out;
1291 }
1292
1293 bat_priv = netdev_priv(hard_iface->soft_iface);
1294 if (!bat_priv->bat_algo_ops->bat_orig_print) {
1295 seq_puts(seq,
1296 "No printing function for this routing protocol\n");
1297 goto out;
1298 }
1299
1300 if (hard_iface->if_status != BATADV_IF_ACTIVE) {
1301 seq_puts(seq, "Interface not active\n");
1302 goto out;
1303 }
1304
1305 seq_printf(seq, "[B.A.T.M.A.N. adv %s, IF/MAC: %s/%pM (%s %s)]\n",
1306 BATADV_SOURCE_VERSION, hard_iface->net_dev->name,
1307 hard_iface->net_dev->dev_addr,
1308 hard_iface->soft_iface->name, bat_priv->bat_algo_ops->name);
1309
1310 bat_priv->bat_algo_ops->bat_orig_print(bat_priv, seq, hard_iface);
1311
1312out:
Marek Lindner16a41422014-04-24 03:44:25 +08001313 if (hard_iface)
Sven Eckelmann82047ad2016-01-17 11:01:10 +01001314 batadv_hardif_put(hard_iface);
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001315 return 0;
1316}
1317
Sven Eckelmann56303d32012-06-05 22:31:31 +02001318int batadv_orig_hash_add_if(struct batadv_hard_iface *hard_iface,
1319 int max_if_num)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001320{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001321 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
Antonio Quartullid0015fd2013-09-03 11:10:23 +02001322 struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001323 struct batadv_hashtable *hash = bat_priv->orig_hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001324 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001325 struct batadv_orig_node *orig_node;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001326 u32 i;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001327 int ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001328
1329 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001330 * if_num
1331 */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001332 for (i = 0; i < hash->size; i++) {
1333 head = &hash->table[i];
1334
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001335 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001336 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
Antonio Quartullid0015fd2013-09-03 11:10:23 +02001337 ret = 0;
1338 if (bao->bat_orig_add_if)
1339 ret = bao->bat_orig_add_if(orig_node,
1340 max_if_num);
Sven Eckelmann5346c352012-05-05 13:27:28 +02001341 if (ret == -ENOMEM)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001342 goto err;
1343 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001344 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001345 }
1346
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001347 return 0;
1348
1349err:
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001350 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001351 return -ENOMEM;
1352}
1353
Sven Eckelmann56303d32012-06-05 22:31:31 +02001354int batadv_orig_hash_del_if(struct batadv_hard_iface *hard_iface,
1355 int max_if_num)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001356{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001357 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001358 struct batadv_hashtable *hash = bat_priv->orig_hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001359 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001360 struct batadv_hard_iface *hard_iface_tmp;
1361 struct batadv_orig_node *orig_node;
Antonio Quartullid0015fd2013-09-03 11:10:23 +02001362 struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001363 u32 i;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001364 int ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001365
1366 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001367 * if_num
1368 */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001369 for (i = 0; i < hash->size; i++) {
1370 head = &hash->table[i];
1371
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001372 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001373 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
Antonio Quartullid0015fd2013-09-03 11:10:23 +02001374 ret = 0;
1375 if (bao->bat_orig_del_if)
1376 ret = bao->bat_orig_del_if(orig_node,
1377 max_if_num,
1378 hard_iface->if_num);
Sven Eckelmann5346c352012-05-05 13:27:28 +02001379 if (ret == -ENOMEM)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001380 goto err;
1381 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001382 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001383 }
1384
1385 /* renumber remaining batman interfaces _inside_ of orig_hash_lock */
1386 rcu_read_lock();
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001387 list_for_each_entry_rcu(hard_iface_tmp, &batadv_hardif_list, list) {
Sven Eckelmanne9a4f292012-06-03 22:19:19 +02001388 if (hard_iface_tmp->if_status == BATADV_IF_NOT_IN_USE)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001389 continue;
1390
Marek Lindnere6c10f42011-02-18 12:33:20 +00001391 if (hard_iface == hard_iface_tmp)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001392 continue;
1393
Marek Lindnere6c10f42011-02-18 12:33:20 +00001394 if (hard_iface->soft_iface != hard_iface_tmp->soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001395 continue;
1396
Marek Lindnere6c10f42011-02-18 12:33:20 +00001397 if (hard_iface_tmp->if_num > hard_iface->if_num)
1398 hard_iface_tmp->if_num--;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001399 }
1400 rcu_read_unlock();
1401
Marek Lindnere6c10f42011-02-18 12:33:20 +00001402 hard_iface->if_num = -1;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001403 return 0;
1404
1405err:
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001406 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001407 return -ENOMEM;
1408}