blob: 5b0c2fffc21439de052e2dbc6755b6450f6b406f [file] [log] [blame]
Sven Eckelmann7db7d9f2017-11-19 15:05:11 +01001// SPDX-License-Identifier: GPL-2.0
Sven Eckelmann68e039f2020-01-01 00:00:01 +01002/* Copyright (C) 2009-2020 B.A.T.M.A.N. contributors:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00003 *
4 * Marek Lindner, Simon Wunderlich
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00005 */
6
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00007#include "originator.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +02008#include "main.h"
9
Sven Eckelmann7c124392016-01-16 10:29:56 +010010#include <linux/atomic.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020011#include <linux/errno.h>
12#include <linux/etherdevice.h>
Sven Eckelmannb92b94a2017-11-19 17:12:02 +010013#include <linux/gfp.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020014#include <linux/jiffies.h>
15#include <linux/kernel.h>
Sven Eckelmann90f564d2016-01-16 10:29:40 +010016#include <linux/kref.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020017#include <linux/list.h>
18#include <linux/lockdep.h>
19#include <linux/netdevice.h>
Matthias Schiffer85cf8c82016-07-03 13:31:39 +020020#include <linux/netlink.h>
Marek Lindnerd0fa4f32015-06-22 00:30:22 +080021#include <linux/rculist.h>
Denys Vlasenko3326afe2017-11-19 17:59:13 +010022#include <linux/rcupdate.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020023#include <linux/seq_file.h>
Matthias Schiffer85cf8c82016-07-03 13:31:39 +020024#include <linux/skbuff.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020025#include <linux/slab.h>
26#include <linux/spinlock.h>
Denys Vlasenko3326afe2017-11-19 17:59:13 +010027#include <linux/stddef.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020028#include <linux/workqueue.h>
Matthias Schiffer85cf8c82016-07-03 13:31:39 +020029#include <net/sock.h>
Linus Lüssing61caf3d2019-06-11 22:58:40 +020030#include <uapi/linux/batadv_packet.h>
Matthias Schiffer85cf8c82016-07-03 13:31:39 +020031#include <uapi/linux/batman_adv.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020032
Sven Eckelmann01d350d2016-05-15 11:07:44 +020033#include "bat_algo.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020034#include "distributed-arp-table.h"
35#include "fragmentation.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000036#include "gateway_client.h"
37#include "hard-interface.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020038#include "hash.h"
Sven Eckelmannba412082016-05-15 23:48:31 +020039#include "log.h"
Linus Lüssing60432d72014-02-15 17:47:51 +010040#include "multicast.h"
Matthias Schiffer85cf8c82016-07-03 13:31:39 +020041#include "netlink.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020042#include "network-coding.h"
43#include "routing.h"
Matthias Schiffer85cf8c82016-07-03 13:31:39 +020044#include "soft-interface.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020045#include "translation-table.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000046
Antonio Quartullidec05072012-11-10 11:00:32 +010047/* hash class keys */
48static struct lock_class_key batadv_orig_hash_lock_class_key;
49
Sven Eckelmannff15c272017-12-02 19:51:53 +010050/**
51 * batadv_orig_hash_find() - Find and return originator from orig_hash
52 * @bat_priv: the bat priv with all the soft interface information
53 * @data: mac address of the originator
54 *
55 * Return: orig_node (with increased refcnt), NULL on errors
56 */
Denys Vlasenko3326afe2017-11-19 17:59:13 +010057struct batadv_orig_node *
58batadv_orig_hash_find(struct batadv_priv *bat_priv, const void *data)
59{
60 struct batadv_hashtable *hash = bat_priv->orig_hash;
61 struct hlist_head *head;
62 struct batadv_orig_node *orig_node, *orig_node_tmp = NULL;
63 int index;
64
65 if (!hash)
66 return NULL;
67
68 index = batadv_choose_orig(data, hash->size);
69 head = &hash->table[index];
70
71 rcu_read_lock();
72 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
73 if (!batadv_compare_eth(orig_node, data))
74 continue;
75
76 if (!kref_get_unless_zero(&orig_node->refcount))
77 continue;
78
79 orig_node_tmp = orig_node;
80 break;
81 }
82 rcu_read_unlock();
83
84 return orig_node_tmp;
85}
86
Sven Eckelmann03fc7f82012-05-12 18:34:00 +020087static void batadv_purge_orig(struct work_struct *work);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000088
Sven Eckelmann62fe7102015-09-15 19:00:48 +020089/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +010090 * batadv_compare_orig() - comparing function used in the originator hash table
Sven Eckelmann7afcbbe2015-10-31 12:29:29 +010091 * @node: node in the local table
92 * @data2: second object to compare the node to
Sven Eckelmann62fe7102015-09-15 19:00:48 +020093 *
Sven Eckelmann4b426b12016-02-22 21:02:39 +010094 * Return: true if they are the same originator
Sven Eckelmann62fe7102015-09-15 19:00:48 +020095 */
Sven Eckelmann4b426b12016-02-22 21:02:39 +010096bool batadv_compare_orig(const struct hlist_node *node, const void *data2)
Sven Eckelmannb8e2dd12011-06-15 15:08:59 +020097{
Sven Eckelmann56303d32012-06-05 22:31:31 +020098 const void *data1 = container_of(node, struct batadv_orig_node,
99 hash_entry);
Sven Eckelmannb8e2dd12011-06-15 15:08:59 +0200100
dingtianhong323813e2013-12-26 19:40:39 +0800101 return batadv_compare_eth(data1, data2);
Sven Eckelmannb8e2dd12011-06-15 15:08:59 +0200102}
103
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200104/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100105 * batadv_orig_node_vlan_get() - get an orig_node_vlan object
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200106 * @orig_node: the originator serving the VLAN
107 * @vid: the VLAN identifier
108 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200109 * Return: the vlan object identified by vid and belonging to orig_node or NULL
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200110 * if it does not exist.
111 */
112struct batadv_orig_node_vlan *
113batadv_orig_node_vlan_get(struct batadv_orig_node *orig_node,
114 unsigned short vid)
115{
116 struct batadv_orig_node_vlan *vlan = NULL, *tmp;
117
118 rcu_read_lock();
Marek Lindnerd0fa4f32015-06-22 00:30:22 +0800119 hlist_for_each_entry_rcu(tmp, &orig_node->vlan_list, list) {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200120 if (tmp->vid != vid)
121 continue;
122
Sven Eckelmann161a3be2016-01-16 10:29:55 +0100123 if (!kref_get_unless_zero(&tmp->refcount))
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200124 continue;
125
126 vlan = tmp;
127
128 break;
129 }
130 rcu_read_unlock();
131
132 return vlan;
133}
134
135/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100136 * batadv_orig_node_vlan_new() - search and possibly create an orig_node_vlan
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200137 * object
138 * @orig_node: the originator serving the VLAN
139 * @vid: the VLAN identifier
140 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200141 * Return: NULL in case of failure or the vlan object identified by vid and
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200142 * belonging to orig_node otherwise. The object is created and added to the list
143 * if it does not exist.
144 *
145 * The object is returned with refcounter increased by 1.
146 */
147struct batadv_orig_node_vlan *
148batadv_orig_node_vlan_new(struct batadv_orig_node *orig_node,
149 unsigned short vid)
150{
151 struct batadv_orig_node_vlan *vlan;
152
153 spin_lock_bh(&orig_node->vlan_list_lock);
154
155 /* first look if an object for this vid already exists */
156 vlan = batadv_orig_node_vlan_get(orig_node, vid);
157 if (vlan)
158 goto out;
159
160 vlan = kzalloc(sizeof(*vlan), GFP_ATOMIC);
161 if (!vlan)
162 goto out;
163
Sven Eckelmann161a3be2016-01-16 10:29:55 +0100164 kref_init(&vlan->refcount);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200165 vlan->vid = vid;
166
Sven Eckelmann09537d12016-07-15 17:39:16 +0200167 kref_get(&vlan->refcount);
Marek Lindnerd0fa4f32015-06-22 00:30:22 +0800168 hlist_add_head_rcu(&vlan->list, &orig_node->vlan_list);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200169
170out:
171 spin_unlock_bh(&orig_node->vlan_list_lock);
172
173 return vlan;
174}
175
176/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100177 * batadv_orig_node_vlan_release() - release originator-vlan object from lists
Sven Eckelmann161a3be2016-01-16 10:29:55 +0100178 * and queue for free after rcu grace period
179 * @ref: kref pointer of the originator-vlan object
180 */
181static void batadv_orig_node_vlan_release(struct kref *ref)
182{
183 struct batadv_orig_node_vlan *orig_vlan;
184
185 orig_vlan = container_of(ref, struct batadv_orig_node_vlan, refcount);
186
187 kfree_rcu(orig_vlan, rcu);
188}
189
190/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100191 * batadv_orig_node_vlan_put() - decrement the refcounter and possibly release
Sven Eckelmann21754e22016-01-17 11:01:24 +0100192 * the originator-vlan object
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200193 * @orig_vlan: the originator-vlan object to release
194 */
Sven Eckelmann21754e22016-01-17 11:01:24 +0100195void batadv_orig_node_vlan_put(struct batadv_orig_node_vlan *orig_vlan)
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200196{
Sven Eckelmann161a3be2016-01-16 10:29:55 +0100197 kref_put(&orig_vlan->refcount, batadv_orig_node_vlan_release);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200198}
199
Sven Eckelmannff15c272017-12-02 19:51:53 +0100200/**
201 * batadv_originator_init() - Initialize all originator structures
202 * @bat_priv: the bat priv with all the soft interface information
203 *
204 * Return: 0 on success or negative error number in case of failure
205 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200206int batadv_originator_init(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000207{
208 if (bat_priv->orig_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200209 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000210
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +0200211 bat_priv->orig_hash = batadv_hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000212
213 if (!bat_priv->orig_hash)
214 goto err;
215
Antonio Quartullidec05072012-11-10 11:00:32 +0100216 batadv_hash_set_lock_class(bat_priv->orig_hash,
217 &batadv_orig_hash_lock_class_key);
218
Antonio Quartulli72414442012-12-25 13:14:37 +0100219 INIT_DELAYED_WORK(&bat_priv->orig_work, batadv_purge_orig);
220 queue_delayed_work(batadv_event_workqueue,
221 &bat_priv->orig_work,
222 msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD));
223
Sven Eckelmann5346c352012-05-05 13:27:28 +0200224 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000225
226err:
Sven Eckelmann5346c352012-05-05 13:27:28 +0200227 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000228}
229
Simon Wunderlich89652332013-11-13 19:14:46 +0100230/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100231 * batadv_neigh_ifinfo_release() - release neigh_ifinfo from lists and queue for
Sven Eckelmannae3e1e32016-01-05 12:06:24 +0100232 * free after rcu grace period
Sven Eckelmann962c6832016-01-16 10:29:51 +0100233 * @ref: kref pointer of the neigh_ifinfo
Simon Wunderlich89652332013-11-13 19:14:46 +0100234 */
Sven Eckelmann962c6832016-01-16 10:29:51 +0100235static void batadv_neigh_ifinfo_release(struct kref *ref)
Simon Wunderlich89652332013-11-13 19:14:46 +0100236{
Sven Eckelmann962c6832016-01-16 10:29:51 +0100237 struct batadv_neigh_ifinfo *neigh_ifinfo;
238
239 neigh_ifinfo = container_of(ref, struct batadv_neigh_ifinfo, refcount);
240
Sven Eckelmannae3e1e32016-01-05 12:06:24 +0100241 if (neigh_ifinfo->if_outgoing != BATADV_IF_DEFAULT)
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100242 batadv_hardif_put(neigh_ifinfo->if_outgoing);
Sven Eckelmannae3e1e32016-01-05 12:06:24 +0100243
244 kfree_rcu(neigh_ifinfo, rcu);
Simon Wunderlich89652332013-11-13 19:14:46 +0100245}
246
247/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100248 * batadv_neigh_ifinfo_put() - decrement the refcounter and possibly release
Simon Wunderlich89652332013-11-13 19:14:46 +0100249 * the neigh_ifinfo
250 * @neigh_ifinfo: the neigh_ifinfo object to release
251 */
Sven Eckelmann044fa3a2016-01-17 11:01:12 +0100252void batadv_neigh_ifinfo_put(struct batadv_neigh_ifinfo *neigh_ifinfo)
Simon Wunderlich89652332013-11-13 19:14:46 +0100253{
Sven Eckelmann962c6832016-01-16 10:29:51 +0100254 kref_put(&neigh_ifinfo->refcount, batadv_neigh_ifinfo_release);
Simon Wunderlich89652332013-11-13 19:14:46 +0100255}
256
257/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100258 * batadv_hardif_neigh_release() - release hardif neigh node from lists and
Sven Eckelmannf6389692016-01-05 12:06:23 +0100259 * queue for free after rcu grace period
Sven Eckelmann90f564d2016-01-16 10:29:40 +0100260 * @ref: kref pointer of the neigh_node
Marek Lindnercef63412015-08-04 21:09:55 +0800261 */
Sven Eckelmann90f564d2016-01-16 10:29:40 +0100262static void batadv_hardif_neigh_release(struct kref *ref)
Marek Lindnercef63412015-08-04 21:09:55 +0800263{
Sven Eckelmann90f564d2016-01-16 10:29:40 +0100264 struct batadv_hardif_neigh_node *hardif_neigh;
265
266 hardif_neigh = container_of(ref, struct batadv_hardif_neigh_node,
267 refcount);
268
Sven Eckelmannf6389692016-01-05 12:06:23 +0100269 spin_lock_bh(&hardif_neigh->if_incoming->neigh_list_lock);
270 hlist_del_init_rcu(&hardif_neigh->list);
271 spin_unlock_bh(&hardif_neigh->if_incoming->neigh_list_lock);
Sven Eckelmannbab7c6c2016-01-05 12:06:17 +0100272
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100273 batadv_hardif_put(hardif_neigh->if_incoming);
Sven Eckelmannf6389692016-01-05 12:06:23 +0100274 kfree_rcu(hardif_neigh, rcu);
Marek Lindnercef63412015-08-04 21:09:55 +0800275}
276
277/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100278 * batadv_hardif_neigh_put() - decrement the hardif neighbors refcounter
Sven Eckelmannf6389692016-01-05 12:06:23 +0100279 * and possibly release it
Marek Lindnercef63412015-08-04 21:09:55 +0800280 * @hardif_neigh: hardif neigh neighbor to free
281 */
Sven Eckelmannaccadc32016-01-17 11:01:14 +0100282void batadv_hardif_neigh_put(struct batadv_hardif_neigh_node *hardif_neigh)
Marek Lindnercef63412015-08-04 21:09:55 +0800283{
Sven Eckelmann90f564d2016-01-16 10:29:40 +0100284 kref_put(&hardif_neigh->refcount, batadv_hardif_neigh_release);
Marek Lindnercef63412015-08-04 21:09:55 +0800285}
286
287/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100288 * batadv_neigh_node_release() - release neigh_node from lists and queue for
Sven Eckelmannb4d922c2016-01-05 12:06:25 +0100289 * free after rcu grace period
Sven Eckelmann77ae32e2016-01-16 10:29:53 +0100290 * @ref: kref pointer of the neigh_node
Simon Wunderlich89652332013-11-13 19:14:46 +0100291 */
Sven Eckelmann77ae32e2016-01-16 10:29:53 +0100292static void batadv_neigh_node_release(struct kref *ref)
Simon Wunderlich89652332013-11-13 19:14:46 +0100293{
294 struct hlist_node *node_tmp;
Sven Eckelmann77ae32e2016-01-16 10:29:53 +0100295 struct batadv_neigh_node *neigh_node;
Simon Wunderlich89652332013-11-13 19:14:46 +0100296 struct batadv_neigh_ifinfo *neigh_ifinfo;
297
Sven Eckelmann77ae32e2016-01-16 10:29:53 +0100298 neigh_node = container_of(ref, struct batadv_neigh_node, refcount);
Simon Wunderlich89652332013-11-13 19:14:46 +0100299
300 hlist_for_each_entry_safe(neigh_ifinfo, node_tmp,
301 &neigh_node->ifinfo_list, list) {
Sven Eckelmann044fa3a2016-01-17 11:01:12 +0100302 batadv_neigh_ifinfo_put(neigh_ifinfo);
Simon Wunderlich89652332013-11-13 19:14:46 +0100303 }
Antonio Quartullibcef1f32015-03-01 00:50:17 +0800304
Sven Eckelmannabe59c62016-03-11 16:44:06 +0100305 batadv_hardif_neigh_put(neigh_node->hardif_neigh);
Marek Lindnercef63412015-08-04 21:09:55 +0800306
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100307 batadv_hardif_put(neigh_node->if_incoming);
Simon Wunderlich89652332013-11-13 19:14:46 +0100308
Sven Eckelmannb4d922c2016-01-05 12:06:25 +0100309 kfree_rcu(neigh_node, rcu);
Simon Wunderlich89652332013-11-13 19:14:46 +0100310}
311
312/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100313 * batadv_neigh_node_put() - decrement the neighbors refcounter and possibly
Sven Eckelmann77ae32e2016-01-16 10:29:53 +0100314 * release it
Simon Wunderlich89652332013-11-13 19:14:46 +0100315 * @neigh_node: neigh neighbor to free
316 */
Sven Eckelmann25bb2502016-01-17 11:01:11 +0100317void batadv_neigh_node_put(struct batadv_neigh_node *neigh_node)
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000318{
Sven Eckelmann77ae32e2016-01-16 10:29:53 +0100319 kref_put(&neigh_node->refcount, batadv_neigh_node_release);
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000320}
321
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100322/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100323 * batadv_orig_router_get() - router to the originator depending on iface
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100324 * @orig_node: the orig node for the router
325 * @if_outgoing: the interface where the payload packet has been received or
326 * the OGM should be sent to
327 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200328 * Return: the neighbor which should be router for this orig_node/iface.
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100329 *
330 * The object is returned with refcounter increased by 1.
331 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200332struct batadv_neigh_node *
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100333batadv_orig_router_get(struct batadv_orig_node *orig_node,
334 const struct batadv_hard_iface *if_outgoing)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000335{
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100336 struct batadv_orig_ifinfo *orig_ifinfo;
337 struct batadv_neigh_node *router = NULL;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000338
339 rcu_read_lock();
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100340 hlist_for_each_entry_rcu(orig_ifinfo, &orig_node->ifinfo_list, list) {
341 if (orig_ifinfo->if_outgoing != if_outgoing)
342 continue;
343
344 router = rcu_dereference(orig_ifinfo->router);
345 break;
346 }
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000347
Sven Eckelmann77ae32e2016-01-16 10:29:53 +0100348 if (router && !kref_get_unless_zero(&router->refcount))
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000349 router = NULL;
350
351 rcu_read_unlock();
352 return router;
353}
354
Antonio Quartulli0538f7592013-09-02 12:15:01 +0200355/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100356 * batadv_orig_ifinfo_get() - find the ifinfo from an orig_node
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100357 * @orig_node: the orig node to be queried
358 * @if_outgoing: the interface for which the ifinfo should be acquired
359 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200360 * Return: the requested orig_ifinfo or NULL if not found.
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100361 *
362 * The object is returned with refcounter increased by 1.
363 */
364struct batadv_orig_ifinfo *
365batadv_orig_ifinfo_get(struct batadv_orig_node *orig_node,
366 struct batadv_hard_iface *if_outgoing)
367{
368 struct batadv_orig_ifinfo *tmp, *orig_ifinfo = NULL;
369
370 rcu_read_lock();
371 hlist_for_each_entry_rcu(tmp, &orig_node->ifinfo_list,
372 list) {
373 if (tmp->if_outgoing != if_outgoing)
374 continue;
375
Sven Eckelmanna6ba0d32016-01-16 10:29:52 +0100376 if (!kref_get_unless_zero(&tmp->refcount))
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100377 continue;
378
379 orig_ifinfo = tmp;
380 break;
381 }
382 rcu_read_unlock();
383
384 return orig_ifinfo;
385}
386
387/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100388 * batadv_orig_ifinfo_new() - search and possibly create an orig_ifinfo object
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100389 * @orig_node: the orig node to be queried
390 * @if_outgoing: the interface for which the ifinfo should be acquired
391 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200392 * Return: NULL in case of failure or the orig_ifinfo object for the if_outgoing
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100393 * interface otherwise. The object is created and added to the list
394 * if it does not exist.
395 *
396 * The object is returned with refcounter increased by 1.
397 */
398struct batadv_orig_ifinfo *
399batadv_orig_ifinfo_new(struct batadv_orig_node *orig_node,
400 struct batadv_hard_iface *if_outgoing)
401{
Sven Eckelmann422d2f72016-07-25 00:42:44 +0200402 struct batadv_orig_ifinfo *orig_ifinfo;
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100403 unsigned long reset_time;
404
405 spin_lock_bh(&orig_node->neigh_list_lock);
406
407 orig_ifinfo = batadv_orig_ifinfo_get(orig_node, if_outgoing);
408 if (orig_ifinfo)
409 goto out;
410
411 orig_ifinfo = kzalloc(sizeof(*orig_ifinfo), GFP_ATOMIC);
412 if (!orig_ifinfo)
413 goto out;
414
Sven Eckelmann17a86912016-04-11 13:06:40 +0200415 if (if_outgoing != BATADV_IF_DEFAULT)
416 kref_get(&if_outgoing->refcount);
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100417
418 reset_time = jiffies - 1;
419 reset_time -= msecs_to_jiffies(BATADV_RESET_PROTECTION_MS);
420 orig_ifinfo->batman_seqno_reset = reset_time;
421 orig_ifinfo->if_outgoing = if_outgoing;
422 INIT_HLIST_NODE(&orig_ifinfo->list);
Sven Eckelmanna6ba0d32016-01-16 10:29:52 +0100423 kref_init(&orig_ifinfo->refcount);
Sven Eckelmannf257b992016-07-15 17:39:17 +0200424
Sven Eckelmanna6ba0d32016-01-16 10:29:52 +0100425 kref_get(&orig_ifinfo->refcount);
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100426 hlist_add_head_rcu(&orig_ifinfo->list,
427 &orig_node->ifinfo_list);
428out:
429 spin_unlock_bh(&orig_node->neigh_list_lock);
430 return orig_ifinfo;
431}
432
433/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100434 * batadv_neigh_ifinfo_get() - find the ifinfo from an neigh_node
Sven Eckelmanne51f0392015-09-06 21:38:51 +0200435 * @neigh: the neigh node to be queried
Simon Wunderlich89652332013-11-13 19:14:46 +0100436 * @if_outgoing: the interface for which the ifinfo should be acquired
437 *
438 * The object is returned with refcounter increased by 1.
439 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200440 * Return: the requested neigh_ifinfo or NULL if not found
Simon Wunderlich89652332013-11-13 19:14:46 +0100441 */
442struct batadv_neigh_ifinfo *
443batadv_neigh_ifinfo_get(struct batadv_neigh_node *neigh,
444 struct batadv_hard_iface *if_outgoing)
445{
446 struct batadv_neigh_ifinfo *neigh_ifinfo = NULL,
447 *tmp_neigh_ifinfo;
448
449 rcu_read_lock();
450 hlist_for_each_entry_rcu(tmp_neigh_ifinfo, &neigh->ifinfo_list,
451 list) {
452 if (tmp_neigh_ifinfo->if_outgoing != if_outgoing)
453 continue;
454
Sven Eckelmann962c6832016-01-16 10:29:51 +0100455 if (!kref_get_unless_zero(&tmp_neigh_ifinfo->refcount))
Simon Wunderlich89652332013-11-13 19:14:46 +0100456 continue;
457
458 neigh_ifinfo = tmp_neigh_ifinfo;
459 break;
460 }
461 rcu_read_unlock();
462
463 return neigh_ifinfo;
464}
465
466/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100467 * batadv_neigh_ifinfo_new() - search and possibly create an neigh_ifinfo object
Sven Eckelmanne51f0392015-09-06 21:38:51 +0200468 * @neigh: the neigh node to be queried
Simon Wunderlich89652332013-11-13 19:14:46 +0100469 * @if_outgoing: the interface for which the ifinfo should be acquired
470 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200471 * Return: NULL in case of failure or the neigh_ifinfo object for the
Simon Wunderlich89652332013-11-13 19:14:46 +0100472 * if_outgoing interface otherwise. The object is created and added to the list
473 * if it does not exist.
474 *
475 * The object is returned with refcounter increased by 1.
476 */
477struct batadv_neigh_ifinfo *
478batadv_neigh_ifinfo_new(struct batadv_neigh_node *neigh,
479 struct batadv_hard_iface *if_outgoing)
480{
481 struct batadv_neigh_ifinfo *neigh_ifinfo;
482
483 spin_lock_bh(&neigh->ifinfo_lock);
484
485 neigh_ifinfo = batadv_neigh_ifinfo_get(neigh, if_outgoing);
486 if (neigh_ifinfo)
487 goto out;
488
489 neigh_ifinfo = kzalloc(sizeof(*neigh_ifinfo), GFP_ATOMIC);
490 if (!neigh_ifinfo)
491 goto out;
492
Sven Eckelmann17a86912016-04-11 13:06:40 +0200493 if (if_outgoing)
494 kref_get(&if_outgoing->refcount);
Simon Wunderlich89652332013-11-13 19:14:46 +0100495
496 INIT_HLIST_NODE(&neigh_ifinfo->list);
Sven Eckelmann962c6832016-01-16 10:29:51 +0100497 kref_init(&neigh_ifinfo->refcount);
Simon Wunderlich89652332013-11-13 19:14:46 +0100498 neigh_ifinfo->if_outgoing = if_outgoing;
499
Sven Eckelmann2e774ac2016-07-15 17:39:19 +0200500 kref_get(&neigh_ifinfo->refcount);
Simon Wunderlich89652332013-11-13 19:14:46 +0100501 hlist_add_head_rcu(&neigh_ifinfo->list, &neigh->ifinfo_list);
502
503out:
504 spin_unlock_bh(&neigh->ifinfo_lock);
505
506 return neigh_ifinfo;
507}
508
509/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100510 * batadv_neigh_node_get() - retrieve a neighbour from the list
Marek Lindnered292662015-08-04 23:31:44 +0800511 * @orig_node: originator which the neighbour belongs to
512 * @hard_iface: the interface where this neighbour is connected to
513 * @addr: the address of the neighbour
514 *
515 * Looks for and possibly returns a neighbour belonging to this originator list
516 * which is connected through the provided hard interface.
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200517 *
518 * Return: neighbor when found. Othwerwise NULL
Marek Lindnered292662015-08-04 23:31:44 +0800519 */
520static struct batadv_neigh_node *
521batadv_neigh_node_get(const struct batadv_orig_node *orig_node,
522 const struct batadv_hard_iface *hard_iface,
523 const u8 *addr)
524{
525 struct batadv_neigh_node *tmp_neigh_node, *res = NULL;
526
527 rcu_read_lock();
528 hlist_for_each_entry_rcu(tmp_neigh_node, &orig_node->neigh_list, list) {
529 if (!batadv_compare_eth(tmp_neigh_node->addr, addr))
530 continue;
531
532 if (tmp_neigh_node->if_incoming != hard_iface)
533 continue;
534
Sven Eckelmann77ae32e2016-01-16 10:29:53 +0100535 if (!kref_get_unless_zero(&tmp_neigh_node->refcount))
Marek Lindnered292662015-08-04 23:31:44 +0800536 continue;
537
538 res = tmp_neigh_node;
539 break;
540 }
541 rcu_read_unlock();
542
543 return res;
544}
545
546/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100547 * batadv_hardif_neigh_create() - create a hardif neighbour node
Marek Lindnercef63412015-08-04 21:09:55 +0800548 * @hard_iface: the interface this neighbour is connected to
549 * @neigh_addr: the interface address of the neighbour to retrieve
Linus Lüssing3111bee2016-08-07 12:34:19 +0200550 * @orig_node: originator object representing the neighbour
Marek Lindnercef63412015-08-04 21:09:55 +0800551 *
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_create(struct batadv_hard_iface *hard_iface,
Linus Lüssing3111bee2016-08-07 12:34:19 +0200556 const u8 *neigh_addr,
557 struct batadv_orig_node *orig_node)
Marek Lindnercef63412015-08-04 21:09:55 +0800558{
Marek Lindner8248a4c2015-08-04 21:09:56 +0800559 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
Sven Eckelmann422d2f72016-07-25 00:42:44 +0200560 struct batadv_hardif_neigh_node *hardif_neigh;
Marek Lindnercef63412015-08-04 21:09:55 +0800561
562 spin_lock_bh(&hard_iface->neigh_list_lock);
563
564 /* check if neighbor hasn't been added in the meantime */
565 hardif_neigh = batadv_hardif_neigh_get(hard_iface, neigh_addr);
566 if (hardif_neigh)
567 goto out;
568
Marek Lindnercef63412015-08-04 21:09:55 +0800569 hardif_neigh = kzalloc(sizeof(*hardif_neigh), GFP_ATOMIC);
Sven Eckelmann17a86912016-04-11 13:06:40 +0200570 if (!hardif_neigh)
Marek Lindnercef63412015-08-04 21:09:55 +0800571 goto out;
Marek Lindnercef63412015-08-04 21:09:55 +0800572
Sven Eckelmann17a86912016-04-11 13:06:40 +0200573 kref_get(&hard_iface->refcount);
Marek Lindnercef63412015-08-04 21:09:55 +0800574 INIT_HLIST_NODE(&hardif_neigh->list);
575 ether_addr_copy(hardif_neigh->addr, neigh_addr);
Linus Lüssing3111bee2016-08-07 12:34:19 +0200576 ether_addr_copy(hardif_neigh->orig, orig_node->orig);
Marek Lindnercef63412015-08-04 21:09:55 +0800577 hardif_neigh->if_incoming = hard_iface;
578 hardif_neigh->last_seen = jiffies;
579
Sven Eckelmann90f564d2016-01-16 10:29:40 +0100580 kref_init(&hardif_neigh->refcount);
Marek Lindnercef63412015-08-04 21:09:55 +0800581
Antonio Quartulli29824a52016-05-25 23:27:31 +0800582 if (bat_priv->algo_ops->neigh.hardif_init)
583 bat_priv->algo_ops->neigh.hardif_init(hardif_neigh);
Marek Lindner8248a4c2015-08-04 21:09:56 +0800584
Sven Eckelmann9ca488d2016-09-29 17:22:58 +0200585 hlist_add_head_rcu(&hardif_neigh->list, &hard_iface->neigh_list);
Marek Lindnercef63412015-08-04 21:09:55 +0800586
587out:
588 spin_unlock_bh(&hard_iface->neigh_list_lock);
589 return hardif_neigh;
590}
591
592/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100593 * batadv_hardif_neigh_get_or_create() - retrieve or create a hardif neighbour
Marek Lindnercef63412015-08-04 21:09:55 +0800594 * node
595 * @hard_iface: the interface this neighbour is connected to
596 * @neigh_addr: the interface address of the neighbour to retrieve
Linus Lüssing3111bee2016-08-07 12:34:19 +0200597 * @orig_node: originator object representing the neighbour
Marek Lindnercef63412015-08-04 21:09:55 +0800598 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200599 * Return: the hardif neighbour node if found or created or NULL otherwise.
Marek Lindnercef63412015-08-04 21:09:55 +0800600 */
601static struct batadv_hardif_neigh_node *
602batadv_hardif_neigh_get_or_create(struct batadv_hard_iface *hard_iface,
Linus Lüssing3111bee2016-08-07 12:34:19 +0200603 const u8 *neigh_addr,
604 struct batadv_orig_node *orig_node)
Marek Lindnercef63412015-08-04 21:09:55 +0800605{
Sven Eckelmann422d2f72016-07-25 00:42:44 +0200606 struct batadv_hardif_neigh_node *hardif_neigh;
Marek Lindnercef63412015-08-04 21:09:55 +0800607
608 /* first check without locking to avoid the overhead */
609 hardif_neigh = batadv_hardif_neigh_get(hard_iface, neigh_addr);
610 if (hardif_neigh)
611 return hardif_neigh;
612
Linus Lüssing3111bee2016-08-07 12:34:19 +0200613 return batadv_hardif_neigh_create(hard_iface, neigh_addr, orig_node);
Marek Lindnercef63412015-08-04 21:09:55 +0800614}
615
616/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100617 * batadv_hardif_neigh_get() - retrieve a hardif neighbour from the list
Marek Lindnercef63412015-08-04 21:09:55 +0800618 * @hard_iface: the interface where this neighbour is connected to
619 * @neigh_addr: the address of the neighbour
620 *
621 * Looks for and possibly returns a neighbour belonging to this hard interface.
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200622 *
623 * Return: neighbor when found. Othwerwise NULL
Marek Lindnercef63412015-08-04 21:09:55 +0800624 */
625struct batadv_hardif_neigh_node *
626batadv_hardif_neigh_get(const struct batadv_hard_iface *hard_iface,
627 const u8 *neigh_addr)
628{
629 struct batadv_hardif_neigh_node *tmp_hardif_neigh, *hardif_neigh = NULL;
630
631 rcu_read_lock();
632 hlist_for_each_entry_rcu(tmp_hardif_neigh,
633 &hard_iface->neigh_list, list) {
634 if (!batadv_compare_eth(tmp_hardif_neigh->addr, neigh_addr))
635 continue;
636
Sven Eckelmann90f564d2016-01-16 10:29:40 +0100637 if (!kref_get_unless_zero(&tmp_hardif_neigh->refcount))
Marek Lindnercef63412015-08-04 21:09:55 +0800638 continue;
639
640 hardif_neigh = tmp_hardif_neigh;
641 break;
642 }
643 rcu_read_unlock();
644
645 return hardif_neigh;
646}
647
648/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100649 * batadv_neigh_node_create() - create a neigh node object
Marek Lindner3f32f8a2015-07-26 04:59:15 +0800650 * @orig_node: originator object representing the neighbour
Antonio Quartulli0538f7592013-09-02 12:15:01 +0200651 * @hard_iface: the interface where the neighbour is connected to
652 * @neigh_addr: the mac address of the neighbour interface
Antonio Quartulli0538f7592013-09-02 12:15:01 +0200653 *
654 * Allocates a new neigh_node object and initialises all the generic fields.
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200655 *
Marek Lindner6f0a6b52016-05-03 01:52:08 +0800656 * Return: the neighbour node if found or created or NULL otherwise.
Antonio Quartulli0538f7592013-09-02 12:15:01 +0200657 */
Marek Lindner6f0a6b52016-05-03 01:52:08 +0800658static struct batadv_neigh_node *
659batadv_neigh_node_create(struct batadv_orig_node *orig_node,
660 struct batadv_hard_iface *hard_iface,
661 const u8 *neigh_addr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000662{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200663 struct batadv_neigh_node *neigh_node;
Marek Lindnercef63412015-08-04 21:09:55 +0800664 struct batadv_hardif_neigh_node *hardif_neigh = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000665
Linus Lüssinge1237052016-01-07 08:11:12 +0100666 spin_lock_bh(&orig_node->neigh_list_lock);
667
Marek Lindner741aa062015-07-26 04:57:43 +0800668 neigh_node = batadv_neigh_node_get(orig_node, hard_iface, neigh_addr);
669 if (neigh_node)
670 goto out;
671
Marek Lindnercef63412015-08-04 21:09:55 +0800672 hardif_neigh = batadv_hardif_neigh_get_or_create(hard_iface,
Linus Lüssing3111bee2016-08-07 12:34:19 +0200673 neigh_addr, orig_node);
Marek Lindnercef63412015-08-04 21:09:55 +0800674 if (!hardif_neigh)
675 goto out;
676
Sven Eckelmann704509b2011-05-14 23:14:54 +0200677 neigh_node = kzalloc(sizeof(*neigh_node), GFP_ATOMIC);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000678 if (!neigh_node)
Marek Lindner7ae8b282012-03-01 15:35:21 +0800679 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000680
Marek Lindner9591a792010-12-12 21:57:11 +0000681 INIT_HLIST_NODE(&neigh_node->list);
Simon Wunderlich89652332013-11-13 19:14:46 +0100682 INIT_HLIST_HEAD(&neigh_node->ifinfo_list);
683 spin_lock_init(&neigh_node->ifinfo_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000684
Sven Eckelmann17a86912016-04-11 13:06:40 +0200685 kref_get(&hard_iface->refcount);
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100686 ether_addr_copy(neigh_node->addr, neigh_addr);
Antonio Quartulli0538f7592013-09-02 12:15:01 +0200687 neigh_node->if_incoming = hard_iface;
688 neigh_node->orig_node = orig_node;
Marek Lindnere48474e2016-03-11 16:01:09 +0100689 neigh_node->last_seen = jiffies;
Antonio Quartulli0538f7592013-09-02 12:15:01 +0200690
Sven Eckelmannabe59c62016-03-11 16:44:06 +0100691 /* increment unique neighbor refcount */
692 kref_get(&hardif_neigh->refcount);
693 neigh_node->hardif_neigh = hardif_neigh;
694
Marek Lindner1605d0d2011-02-18 12:28:11 +0000695 /* extra reference for return */
Sven Eckelmann77ae32e2016-01-16 10:29:53 +0100696 kref_init(&neigh_node->refcount);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000697
Sven Eckelmann84274452016-07-15 17:39:20 +0200698 kref_get(&neigh_node->refcount);
Marek Lindner741aa062015-07-26 04:57:43 +0800699 hlist_add_head_rcu(&neigh_node->list, &orig_node->neigh_list);
Marek Lindner741aa062015-07-26 04:57:43 +0800700
701 batadv_dbg(BATADV_DBG_BATMAN, orig_node->bat_priv,
702 "Creating new neighbor %pM for orig_node %pM on interface %s\n",
703 neigh_addr, orig_node->orig, hard_iface->net_dev->name);
704
Marek Lindner7ae8b282012-03-01 15:35:21 +0800705out:
Linus Lüssinge1237052016-01-07 08:11:12 +0100706 spin_unlock_bh(&orig_node->neigh_list_lock);
707
Marek Lindnercef63412015-08-04 21:09:55 +0800708 if (hardif_neigh)
Sven Eckelmannaccadc32016-01-17 11:01:14 +0100709 batadv_hardif_neigh_put(hardif_neigh);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000710 return neigh_node;
711}
712
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100713/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100714 * batadv_neigh_node_get_or_create() - retrieve or create a neigh node object
Marek Lindner6f0a6b52016-05-03 01:52:08 +0800715 * @orig_node: originator object representing the neighbour
716 * @hard_iface: the interface where the neighbour is connected to
717 * @neigh_addr: the mac address of the neighbour interface
718 *
719 * Return: the neighbour node if found or created or NULL otherwise.
720 */
721struct batadv_neigh_node *
722batadv_neigh_node_get_or_create(struct batadv_orig_node *orig_node,
723 struct batadv_hard_iface *hard_iface,
724 const u8 *neigh_addr)
725{
Sven Eckelmann422d2f72016-07-25 00:42:44 +0200726 struct batadv_neigh_node *neigh_node;
Marek Lindner6f0a6b52016-05-03 01:52:08 +0800727
728 /* first check without locking to avoid the overhead */
729 neigh_node = batadv_neigh_node_get(orig_node, hard_iface, neigh_addr);
730 if (neigh_node)
731 return neigh_node;
732
733 return batadv_neigh_node_create(orig_node, hard_iface, neigh_addr);
734}
735
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +0200736#ifdef CONFIG_BATMAN_ADV_DEBUGFS
Marek Lindner6f0a6b52016-05-03 01:52:08 +0800737/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100738 * batadv_hardif_neigh_seq_print_text() - print the single hop neighbour list
Marek Lindner75874052015-08-04 21:09:57 +0800739 * @seq: neighbour table seq_file struct
740 * @offset: not used
741 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200742 * Return: always 0
Marek Lindner75874052015-08-04 21:09:57 +0800743 */
744int batadv_hardif_neigh_seq_print_text(struct seq_file *seq, void *offset)
745{
746 struct net_device *net_dev = (struct net_device *)seq->private;
747 struct batadv_priv *bat_priv = netdev_priv(net_dev);
748 struct batadv_hard_iface *primary_if;
749
750 primary_if = batadv_seq_print_text_primary_if_get(seq);
751 if (!primary_if)
752 return 0;
753
754 seq_printf(seq, "[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s %s)]\n",
755 BATADV_SOURCE_VERSION, primary_if->net_dev->name,
756 primary_if->net_dev->dev_addr, net_dev->name,
Antonio Quartulli29824a52016-05-25 23:27:31 +0800757 bat_priv->algo_ops->name);
Marek Lindner75874052015-08-04 21:09:57 +0800758
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100759 batadv_hardif_put(primary_if);
Marek Lindner75874052015-08-04 21:09:57 +0800760
Antonio Quartulli29824a52016-05-25 23:27:31 +0800761 if (!bat_priv->algo_ops->neigh.print) {
Marek Lindner75874052015-08-04 21:09:57 +0800762 seq_puts(seq,
763 "No printing function for this routing protocol\n");
764 return 0;
765 }
766
Antonio Quartulli29824a52016-05-25 23:27:31 +0800767 bat_priv->algo_ops->neigh.print(bat_priv, seq);
Marek Lindner75874052015-08-04 21:09:57 +0800768 return 0;
769}
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +0200770#endif
Marek Lindner75874052015-08-04 21:09:57 +0800771
772/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100773 * batadv_hardif_neigh_dump() - Dump to netlink the neighbor infos for a
774 * specific outgoing interface
Matthias Schiffer85cf8c82016-07-03 13:31:39 +0200775 * @msg: message to dump into
776 * @cb: parameters for the dump
777 *
778 * Return: 0 or error value
779 */
780int batadv_hardif_neigh_dump(struct sk_buff *msg, struct netlink_callback *cb)
781{
782 struct net *net = sock_net(cb->skb->sk);
783 struct net_device *soft_iface;
784 struct net_device *hard_iface = NULL;
785 struct batadv_hard_iface *hardif = BATADV_IF_DEFAULT;
786 struct batadv_priv *bat_priv;
787 struct batadv_hard_iface *primary_if = NULL;
788 int ret;
789 int ifindex, hard_ifindex;
790
791 ifindex = batadv_netlink_get_ifindex(cb->nlh, BATADV_ATTR_MESH_IFINDEX);
792 if (!ifindex)
793 return -EINVAL;
794
795 soft_iface = dev_get_by_index(net, ifindex);
796 if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
797 ret = -ENODEV;
798 goto out;
799 }
800
801 bat_priv = netdev_priv(soft_iface);
802
803 primary_if = batadv_primary_if_get_selected(bat_priv);
804 if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
805 ret = -ENOENT;
806 goto out;
807 }
808
809 hard_ifindex = batadv_netlink_get_ifindex(cb->nlh,
810 BATADV_ATTR_HARD_IFINDEX);
811 if (hard_ifindex) {
812 hard_iface = dev_get_by_index(net, hard_ifindex);
813 if (hard_iface)
814 hardif = batadv_hardif_get_by_netdev(hard_iface);
815
816 if (!hardif) {
817 ret = -ENODEV;
818 goto out;
819 }
820
821 if (hardif->soft_iface != soft_iface) {
822 ret = -ENOENT;
823 goto out;
824 }
825 }
826
827 if (!bat_priv->algo_ops->neigh.dump) {
828 ret = -EOPNOTSUPP;
829 goto out;
830 }
831
832 bat_priv->algo_ops->neigh.dump(msg, cb, bat_priv, hardif);
833
834 ret = msg->len;
835
836 out:
837 if (hardif)
838 batadv_hardif_put(hardif);
839 if (hard_iface)
840 dev_put(hard_iface);
841 if (primary_if)
842 batadv_hardif_put(primary_if);
843 if (soft_iface)
844 dev_put(soft_iface);
845
846 return ret;
847}
848
849/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100850 * batadv_orig_ifinfo_release() - release orig_ifinfo from lists and queue for
Sven Eckelmann2baa7532016-01-05 12:06:22 +0100851 * free after rcu grace period
Sven Eckelmanna6ba0d32016-01-16 10:29:52 +0100852 * @ref: kref pointer of the orig_ifinfo
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100853 */
Sven Eckelmanna6ba0d32016-01-16 10:29:52 +0100854static void batadv_orig_ifinfo_release(struct kref *ref)
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100855{
Sven Eckelmanna6ba0d32016-01-16 10:29:52 +0100856 struct batadv_orig_ifinfo *orig_ifinfo;
Simon Wunderlich000c8df2014-03-26 15:46:22 +0100857 struct batadv_neigh_node *router;
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100858
Sven Eckelmanna6ba0d32016-01-16 10:29:52 +0100859 orig_ifinfo = container_of(ref, struct batadv_orig_ifinfo, refcount);
860
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100861 if (orig_ifinfo->if_outgoing != BATADV_IF_DEFAULT)
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100862 batadv_hardif_put(orig_ifinfo->if_outgoing);
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100863
Simon Wunderlich000c8df2014-03-26 15:46:22 +0100864 /* this is the last reference to this object */
865 router = rcu_dereference_protected(orig_ifinfo->router, true);
866 if (router)
Sven Eckelmann25bb2502016-01-17 11:01:11 +0100867 batadv_neigh_node_put(router);
Sven Eckelmann2baa7532016-01-05 12:06:22 +0100868
869 kfree_rcu(orig_ifinfo, rcu);
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100870}
871
872/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100873 * batadv_orig_ifinfo_put() - decrement the refcounter and possibly release
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100874 * the orig_ifinfo
875 * @orig_ifinfo: the orig_ifinfo object to release
876 */
Sven Eckelmann35f94772016-01-17 11:01:13 +0100877void batadv_orig_ifinfo_put(struct batadv_orig_ifinfo *orig_ifinfo)
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100878{
Sven Eckelmanna6ba0d32016-01-16 10:29:52 +0100879 kref_put(&orig_ifinfo->refcount, batadv_orig_ifinfo_release);
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100880}
881
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100882/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100883 * batadv_orig_node_free_rcu() - free the orig_node
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100884 * @rcu: rcu pointer of the orig_node
885 */
Sven Eckelmann03fc7f82012-05-12 18:34:00 +0200886static void batadv_orig_node_free_rcu(struct rcu_head *rcu)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000887{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200888 struct batadv_orig_node *orig_node;
Marek Lindner16b1aba2011-01-19 20:01:42 +0000889
Sven Eckelmann56303d32012-06-05 22:31:31 +0200890 orig_node = container_of(rcu, struct batadv_orig_node, rcu);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000891
Linus Lüssing60432d72014-02-15 17:47:51 +0100892 batadv_mcast_purge_orig(orig_node);
893
Martin Hundebøll610bfc6bc2013-05-23 16:53:02 +0200894 batadv_frag_purge_orig(orig_node, NULL);
895
Antonio Quartullia73105b2011-04-27 14:27:44 +0200896 kfree(orig_node->tt_buff);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000897 kfree(orig_node);
898}
899
Linus Lüssing72822222013-04-15 21:43:29 +0800900/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100901 * batadv_orig_node_release() - release orig_node from lists and queue for
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100902 * free after rcu grace period
Sven Eckelmann7c124392016-01-16 10:29:56 +0100903 * @ref: kref pointer of the orig_node
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100904 */
Sven Eckelmann7c124392016-01-16 10:29:56 +0100905static void batadv_orig_node_release(struct kref *ref)
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100906{
907 struct hlist_node *node_tmp;
908 struct batadv_neigh_node *neigh_node;
Sven Eckelmann7c124392016-01-16 10:29:56 +0100909 struct batadv_orig_node *orig_node;
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100910 struct batadv_orig_ifinfo *orig_ifinfo;
Sven Eckelmann33fbb1f2016-06-30 20:10:46 +0200911 struct batadv_orig_node_vlan *vlan;
Sven Eckelmanncbef1e12016-06-30 21:41:13 +0200912 struct batadv_orig_ifinfo *last_candidate;
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100913
Sven Eckelmann7c124392016-01-16 10:29:56 +0100914 orig_node = container_of(ref, struct batadv_orig_node, refcount);
915
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100916 spin_lock_bh(&orig_node->neigh_list_lock);
917
918 /* for all neighbors towards this originator ... */
919 hlist_for_each_entry_safe(neigh_node, node_tmp,
920 &orig_node->neigh_list, list) {
921 hlist_del_rcu(&neigh_node->list);
Sven Eckelmann25bb2502016-01-17 11:01:11 +0100922 batadv_neigh_node_put(neigh_node);
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100923 }
924
925 hlist_for_each_entry_safe(orig_ifinfo, node_tmp,
926 &orig_node->ifinfo_list, list) {
927 hlist_del_rcu(&orig_ifinfo->list);
Sven Eckelmann35f94772016-01-17 11:01:13 +0100928 batadv_orig_ifinfo_put(orig_ifinfo);
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100929 }
Sven Eckelmanncbef1e12016-06-30 21:41:13 +0200930
931 last_candidate = orig_node->last_bonding_candidate;
932 orig_node->last_bonding_candidate = NULL;
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100933 spin_unlock_bh(&orig_node->neigh_list_lock);
934
Sven Eckelmanncbef1e12016-06-30 21:41:13 +0200935 if (last_candidate)
936 batadv_orig_ifinfo_put(last_candidate);
937
Sven Eckelmann33fbb1f2016-06-30 20:10:46 +0200938 spin_lock_bh(&orig_node->vlan_list_lock);
939 hlist_for_each_entry_safe(vlan, node_tmp, &orig_node->vlan_list, list) {
940 hlist_del_rcu(&vlan->list);
941 batadv_orig_node_vlan_put(vlan);
942 }
943 spin_unlock_bh(&orig_node->vlan_list_lock);
944
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100945 /* Free nc_nodes */
946 batadv_nc_purge_orig(orig_node->bat_priv, orig_node, NULL);
947
948 call_rcu(&orig_node->rcu, batadv_orig_node_free_rcu);
949}
950
951/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100952 * batadv_orig_node_put() - decrement the orig node refcounter and possibly
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100953 * release it
Linus Lüssing72822222013-04-15 21:43:29 +0800954 * @orig_node: the orig node to free
955 */
Sven Eckelmann5d967312016-01-17 11:01:09 +0100956void batadv_orig_node_put(struct batadv_orig_node *orig_node)
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000957{
Sven Eckelmann7c124392016-01-16 10:29:56 +0100958 kref_put(&orig_node->refcount, batadv_orig_node_release);
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000959}
960
Sven Eckelmannff15c272017-12-02 19:51:53 +0100961/**
962 * batadv_originator_free() - Free all originator structures
963 * @bat_priv: the bat priv with all the soft interface information
964 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200965void batadv_originator_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000966{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200967 struct batadv_hashtable *hash = bat_priv->orig_hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800968 struct hlist_node *node_tmp;
Marek Lindner16b1aba2011-01-19 20:01:42 +0000969 struct hlist_head *head;
Marek Lindner16b1aba2011-01-19 20:01:42 +0000970 spinlock_t *list_lock; /* spinlock to protect write access */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200971 struct batadv_orig_node *orig_node;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200972 u32 i;
Marek Lindner16b1aba2011-01-19 20:01:42 +0000973
974 if (!hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000975 return;
976
977 cancel_delayed_work_sync(&bat_priv->orig_work);
978
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000979 bat_priv->orig_hash = NULL;
Marek Lindner16b1aba2011-01-19 20:01:42 +0000980
981 for (i = 0; i < hash->size; i++) {
982 head = &hash->table[i];
983 list_lock = &hash->list_locks[i];
984
985 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800986 hlist_for_each_entry_safe(orig_node, node_tmp,
Marek Lindner7aadf882011-02-18 12:28:09 +0000987 head, hash_entry) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800988 hlist_del_rcu(&orig_node->hash_entry);
Sven Eckelmann5d967312016-01-17 11:01:09 +0100989 batadv_orig_node_put(orig_node);
Marek Lindner16b1aba2011-01-19 20:01:42 +0000990 }
991 spin_unlock_bh(list_lock);
992 }
993
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +0200994 batadv_hash_destroy(hash);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000995}
996
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200997/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100998 * batadv_orig_node_new() - creates a new orig_node
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200999 * @bat_priv: the bat priv with all the soft interface information
1000 * @addr: the mac address of the originator
1001 *
1002 * Creates a new originator object and initialise all the generic fields.
1003 * The new object is not added to the originator list.
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001004 *
1005 * Return: the newly created object or NULL on failure.
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001006 */
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001007struct batadv_orig_node *batadv_orig_node_new(struct batadv_priv *bat_priv,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001008 const u8 *addr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001009{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001010 struct batadv_orig_node *orig_node;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001011 struct batadv_orig_node_vlan *vlan;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001012 unsigned long reset_time;
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001013 int i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001014
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001015 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1016 "Creating new originator: %pM\n", addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001017
Sven Eckelmann704509b2011-05-14 23:14:54 +02001018 orig_node = kzalloc(sizeof(*orig_node), GFP_ATOMIC);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001019 if (!orig_node)
1020 return NULL;
1021
Marek Lindner9591a792010-12-12 21:57:11 +00001022 INIT_HLIST_HEAD(&orig_node->neigh_list);
Marek Lindnerd0fa4f32015-06-22 00:30:22 +08001023 INIT_HLIST_HEAD(&orig_node->vlan_list);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001024 INIT_HLIST_HEAD(&orig_node->ifinfo_list);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001025 spin_lock_init(&orig_node->bcast_seqno_lock);
Marek Lindnerf987ed62010-12-12 21:57:12 +00001026 spin_lock_init(&orig_node->neigh_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001027 spin_lock_init(&orig_node->tt_buff_lock);
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02001028 spin_lock_init(&orig_node->tt_lock);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001029 spin_lock_init(&orig_node->vlan_list_lock);
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001030
Martin Hundebølld56b1702013-01-25 11:12:39 +01001031 batadv_nc_init_orig(orig_node);
1032
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001033 /* extra reference for return */
Sven Eckelmann7c124392016-01-16 10:29:56 +01001034 kref_init(&orig_node->refcount);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001035
Marek Lindner16b1aba2011-01-19 20:01:42 +00001036 orig_node->bat_priv = bat_priv;
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01001037 ether_addr_copy(orig_node->orig, addr);
Antonio Quartulli785ea112011-11-23 11:35:44 +01001038 batadv_dat_init_orig_node_addr(orig_node);
Antonio Quartullic8c991b2011-07-07 01:40:57 +02001039 atomic_set(&orig_node->last_ttvn, 0);
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001040 orig_node->tt_buff = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001041 orig_node->tt_buff_len = 0;
Linus Lüssing2c667a32014-10-30 06:23:40 +01001042 orig_node->last_seen = jiffies;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001043 reset_time = jiffies - 1 - msecs_to_jiffies(BATADV_RESET_PROTECTION_MS);
1044 orig_node->bcast_seqno_reset = reset_time;
Linus Lüssing8a4023c2015-06-16 17:10:26 +02001045
Linus Lüssing60432d72014-02-15 17:47:51 +01001046#ifdef CONFIG_BATMAN_ADV_MCAST
Linus Lüssing61caf3d2019-06-11 22:58:40 +02001047 orig_node->mcast_flags = BATADV_MCAST_WANT_NO_RTR4;
1048 orig_node->mcast_flags |= BATADV_MCAST_WANT_NO_RTR6;
Linus Lüssing8a4023c2015-06-16 17:10:26 +02001049 INIT_HLIST_NODE(&orig_node->mcast_want_all_unsnoopables_node);
1050 INIT_HLIST_NODE(&orig_node->mcast_want_all_ipv4_node);
1051 INIT_HLIST_NODE(&orig_node->mcast_want_all_ipv6_node);
1052 spin_lock_init(&orig_node->mcast_handler_lock);
Linus Lüssing60432d72014-02-15 17:47:51 +01001053#endif
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001054
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001055 /* create a vlan object for the "untagged" LAN */
1056 vlan = batadv_orig_node_vlan_new(orig_node, BATADV_NO_FLAGS);
1057 if (!vlan)
1058 goto free_orig_node;
1059 /* batadv_orig_node_vlan_new() increases the refcounter.
1060 * Immediately release vlan since it is not needed anymore in this
1061 * context
1062 */
Sven Eckelmann21754e22016-01-17 11:01:24 +01001063 batadv_orig_node_vlan_put(vlan);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001064
Martin Hundebøll610bfc6bc2013-05-23 16:53:02 +02001065 for (i = 0; i < BATADV_FRAG_BUFFER_COUNT; i++) {
Sven Eckelmann176e5b72016-07-27 12:31:07 +02001066 INIT_HLIST_HEAD(&orig_node->fragments[i].fragment_list);
Martin Hundebøll610bfc6bc2013-05-23 16:53:02 +02001067 spin_lock_init(&orig_node->fragments[i].lock);
1068 orig_node->fragments[i].size = 0;
1069 }
1070
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001071 return orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001072free_orig_node:
1073 kfree(orig_node);
1074 return NULL;
1075}
1076
Simon Wunderlich89652332013-11-13 19:14:46 +01001077/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001078 * batadv_purge_neigh_ifinfo() - purge obsolete ifinfo entries from neighbor
Simon Wunderlich709de132014-03-26 15:46:24 +01001079 * @bat_priv: the bat priv with all the soft interface information
1080 * @neigh: orig node which is to be checked
1081 */
1082static void
1083batadv_purge_neigh_ifinfo(struct batadv_priv *bat_priv,
1084 struct batadv_neigh_node *neigh)
1085{
1086 struct batadv_neigh_ifinfo *neigh_ifinfo;
1087 struct batadv_hard_iface *if_outgoing;
1088 struct hlist_node *node_tmp;
1089
1090 spin_lock_bh(&neigh->ifinfo_lock);
1091
1092 /* for all ifinfo objects for this neighinator */
1093 hlist_for_each_entry_safe(neigh_ifinfo, node_tmp,
1094 &neigh->ifinfo_list, list) {
1095 if_outgoing = neigh_ifinfo->if_outgoing;
1096
1097 /* always keep the default interface */
1098 if (if_outgoing == BATADV_IF_DEFAULT)
1099 continue;
1100
1101 /* don't purge if the interface is not (going) down */
Sven Eckelmann825ffe12017-08-23 21:52:13 +02001102 if (if_outgoing->if_status != BATADV_IF_INACTIVE &&
1103 if_outgoing->if_status != BATADV_IF_NOT_IN_USE &&
1104 if_outgoing->if_status != BATADV_IF_TO_BE_REMOVED)
Simon Wunderlich709de132014-03-26 15:46:24 +01001105 continue;
1106
1107 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1108 "neighbor/ifinfo purge: neighbor %pM, iface: %s\n",
1109 neigh->addr, if_outgoing->net_dev->name);
1110
1111 hlist_del_rcu(&neigh_ifinfo->list);
Sven Eckelmann044fa3a2016-01-17 11:01:12 +01001112 batadv_neigh_ifinfo_put(neigh_ifinfo);
Simon Wunderlich709de132014-03-26 15:46:24 +01001113 }
1114
1115 spin_unlock_bh(&neigh->ifinfo_lock);
1116}
1117
1118/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001119 * batadv_purge_orig_ifinfo() - purge obsolete ifinfo entries from originator
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001120 * @bat_priv: the bat priv with all the soft interface information
1121 * @orig_node: orig node which is to be checked
1122 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001123 * Return: true if any ifinfo entry was purged, false otherwise.
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001124 */
1125static bool
1126batadv_purge_orig_ifinfo(struct batadv_priv *bat_priv,
1127 struct batadv_orig_node *orig_node)
1128{
1129 struct batadv_orig_ifinfo *orig_ifinfo;
1130 struct batadv_hard_iface *if_outgoing;
1131 struct hlist_node *node_tmp;
1132 bool ifinfo_purged = false;
1133
1134 spin_lock_bh(&orig_node->neigh_list_lock);
1135
1136 /* for all ifinfo objects for this originator */
1137 hlist_for_each_entry_safe(orig_ifinfo, node_tmp,
1138 &orig_node->ifinfo_list, list) {
1139 if_outgoing = orig_ifinfo->if_outgoing;
1140
1141 /* always keep the default interface */
1142 if (if_outgoing == BATADV_IF_DEFAULT)
1143 continue;
1144
1145 /* don't purge if the interface is not (going) down */
Sven Eckelmann825ffe12017-08-23 21:52:13 +02001146 if (if_outgoing->if_status != BATADV_IF_INACTIVE &&
1147 if_outgoing->if_status != BATADV_IF_NOT_IN_USE &&
1148 if_outgoing->if_status != BATADV_IF_TO_BE_REMOVED)
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001149 continue;
1150
1151 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1152 "router/ifinfo purge: originator %pM, iface: %s\n",
1153 orig_node->orig, if_outgoing->net_dev->name);
1154
1155 ifinfo_purged = true;
1156
1157 hlist_del_rcu(&orig_ifinfo->list);
Sven Eckelmann35f94772016-01-17 11:01:13 +01001158 batadv_orig_ifinfo_put(orig_ifinfo);
Simon Wunderlichf3b3d902013-11-13 19:14:50 +01001159 if (orig_node->last_bonding_candidate == orig_ifinfo) {
1160 orig_node->last_bonding_candidate = NULL;
Sven Eckelmann35f94772016-01-17 11:01:13 +01001161 batadv_orig_ifinfo_put(orig_ifinfo);
Simon Wunderlichf3b3d902013-11-13 19:14:50 +01001162 }
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001163 }
1164
1165 spin_unlock_bh(&orig_node->neigh_list_lock);
1166
1167 return ifinfo_purged;
1168}
1169
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001170/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001171 * batadv_purge_orig_neighbors() - purges neighbors from originator
Simon Wunderlich89652332013-11-13 19:14:46 +01001172 * @bat_priv: the bat priv with all the soft interface information
1173 * @orig_node: orig node which is to be checked
1174 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001175 * Return: true if any neighbor was purged, false otherwise
Simon Wunderlich89652332013-11-13 19:14:46 +01001176 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001177static bool
1178batadv_purge_orig_neighbors(struct batadv_priv *bat_priv,
Simon Wunderlich89652332013-11-13 19:14:46 +01001179 struct batadv_orig_node *orig_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001180{
Sasha Levinb67bfe02013-02-27 17:06:00 -08001181 struct hlist_node *node_tmp;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001182 struct batadv_neigh_node *neigh_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001183 bool neigh_purged = false;
Marek Lindner0b0094e2012-03-01 15:35:20 +08001184 unsigned long last_seen;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001185 struct batadv_hard_iface *if_incoming;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001186
Marek Lindnerf987ed62010-12-12 21:57:12 +00001187 spin_lock_bh(&orig_node->neigh_list_lock);
1188
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001189 /* for all neighbors towards this originator ... */
Sasha Levinb67bfe02013-02-27 17:06:00 -08001190 hlist_for_each_entry_safe(neigh_node, node_tmp,
Marek Lindner9591a792010-12-12 21:57:11 +00001191 &orig_node->neigh_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001192 last_seen = neigh_node->last_seen;
1193 if_incoming = neigh_node->if_incoming;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001194
Sven Eckelmann825ffe12017-08-23 21:52:13 +02001195 if (batadv_has_timed_out(last_seen, BATADV_PURGE_TIMEOUT) ||
1196 if_incoming->if_status == BATADV_IF_INACTIVE ||
1197 if_incoming->if_status == BATADV_IF_NOT_IN_USE ||
1198 if_incoming->if_status == BATADV_IF_TO_BE_REMOVED) {
1199 if (if_incoming->if_status == BATADV_IF_INACTIVE ||
1200 if_incoming->if_status == BATADV_IF_NOT_IN_USE ||
1201 if_incoming->if_status == BATADV_IF_TO_BE_REMOVED)
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001202 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001203 "neighbor purge: originator %pM, neighbor: %pM, iface: %s\n",
1204 orig_node->orig, neigh_node->addr,
1205 if_incoming->net_dev->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001206 else
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001207 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001208 "neighbor timeout: originator %pM, neighbor: %pM, last_seen: %u\n",
1209 orig_node->orig, neigh_node->addr,
1210 jiffies_to_msecs(last_seen));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001211
1212 neigh_purged = true;
Marek Lindner9591a792010-12-12 21:57:11 +00001213
Marek Lindnerf987ed62010-12-12 21:57:12 +00001214 hlist_del_rcu(&neigh_node->list);
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001215 batadv_neigh_node_put(neigh_node);
Simon Wunderlich709de132014-03-26 15:46:24 +01001216 } else {
1217 /* only necessary if not the whole neighbor is to be
1218 * deleted, but some interface has been removed.
1219 */
1220 batadv_purge_neigh_ifinfo(bat_priv, neigh_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001221 }
1222 }
Marek Lindnerf987ed62010-12-12 21:57:12 +00001223
1224 spin_unlock_bh(&orig_node->neigh_list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001225 return neigh_purged;
1226}
1227
Simon Wunderlich89652332013-11-13 19:14:46 +01001228/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001229 * batadv_find_best_neighbor() - finds the best neighbor after purging
Simon Wunderlich89652332013-11-13 19:14:46 +01001230 * @bat_priv: the bat priv with all the soft interface information
1231 * @orig_node: orig node which is to be checked
1232 * @if_outgoing: the interface for which the metric should be compared
1233 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001234 * Return: the current best neighbor, with refcount increased.
Simon Wunderlich89652332013-11-13 19:14:46 +01001235 */
1236static struct batadv_neigh_node *
1237batadv_find_best_neighbor(struct batadv_priv *bat_priv,
1238 struct batadv_orig_node *orig_node,
1239 struct batadv_hard_iface *if_outgoing)
1240{
1241 struct batadv_neigh_node *best = NULL, *neigh;
Antonio Quartulli29824a52016-05-25 23:27:31 +08001242 struct batadv_algo_ops *bao = bat_priv->algo_ops;
Simon Wunderlich89652332013-11-13 19:14:46 +01001243
1244 rcu_read_lock();
1245 hlist_for_each_entry_rcu(neigh, &orig_node->neigh_list, list) {
Antonio Quartulli29824a52016-05-25 23:27:31 +08001246 if (best && (bao->neigh.cmp(neigh, if_outgoing, best,
1247 if_outgoing) <= 0))
Simon Wunderlich89652332013-11-13 19:14:46 +01001248 continue;
1249
Sven Eckelmann77ae32e2016-01-16 10:29:53 +01001250 if (!kref_get_unless_zero(&neigh->refcount))
Simon Wunderlich89652332013-11-13 19:14:46 +01001251 continue;
1252
1253 if (best)
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001254 batadv_neigh_node_put(best);
Simon Wunderlich89652332013-11-13 19:14:46 +01001255
1256 best = neigh;
1257 }
1258 rcu_read_unlock();
1259
1260 return best;
1261}
1262
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001263/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001264 * batadv_purge_orig_node() - purges obsolete information from an orig_node
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001265 * @bat_priv: the bat priv with all the soft interface information
1266 * @orig_node: orig node which is to be checked
1267 *
1268 * This function checks if the orig_node or substructures of it have become
1269 * obsolete, and purges this information if that's the case.
1270 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001271 * Return: true if the orig_node is to be removed, false otherwise.
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001272 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001273static bool batadv_purge_orig_node(struct batadv_priv *bat_priv,
1274 struct batadv_orig_node *orig_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001275{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001276 struct batadv_neigh_node *best_neigh_node;
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001277 struct batadv_hard_iface *hard_iface;
Simon Wunderlich7b955a92014-03-26 15:46:23 +01001278 bool changed_ifinfo, changed_neigh;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001279
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001280 if (batadv_has_timed_out(orig_node->last_seen,
1281 2 * BATADV_PURGE_TIMEOUT)) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001282 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001283 "Originator timeout: originator %pM, last_seen %u\n",
1284 orig_node->orig,
1285 jiffies_to_msecs(orig_node->last_seen));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001286 return true;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001287 }
Simon Wunderlich7b955a92014-03-26 15:46:23 +01001288 changed_ifinfo = batadv_purge_orig_ifinfo(bat_priv, orig_node);
1289 changed_neigh = batadv_purge_orig_neighbors(bat_priv, orig_node);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001290
Simon Wunderlich7b955a92014-03-26 15:46:23 +01001291 if (!changed_ifinfo && !changed_neigh)
Simon Wunderlich89652332013-11-13 19:14:46 +01001292 return false;
1293
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001294 /* first for NULL ... */
Simon Wunderlich89652332013-11-13 19:14:46 +01001295 best_neigh_node = batadv_find_best_neighbor(bat_priv, orig_node,
1296 BATADV_IF_DEFAULT);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001297 batadv_update_route(bat_priv, orig_node, BATADV_IF_DEFAULT,
1298 best_neigh_node);
Simon Wunderlich89652332013-11-13 19:14:46 +01001299 if (best_neigh_node)
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001300 batadv_neigh_node_put(best_neigh_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001301
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001302 /* ... then for all other interfaces. */
1303 rcu_read_lock();
1304 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
1305 if (hard_iface->if_status != BATADV_IF_ACTIVE)
1306 continue;
1307
1308 if (hard_iface->soft_iface != bat_priv->soft_iface)
1309 continue;
1310
Sven Eckelmann27353442016-03-05 16:09:16 +01001311 if (!kref_get_unless_zero(&hard_iface->refcount))
1312 continue;
1313
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001314 best_neigh_node = batadv_find_best_neighbor(bat_priv,
1315 orig_node,
1316 hard_iface);
1317 batadv_update_route(bat_priv, orig_node, hard_iface,
1318 best_neigh_node);
1319 if (best_neigh_node)
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001320 batadv_neigh_node_put(best_neigh_node);
Sven Eckelmann27353442016-03-05 16:09:16 +01001321
1322 batadv_hardif_put(hard_iface);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001323 }
1324 rcu_read_unlock();
1325
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001326 return false;
1327}
1328
Sven Eckelmann3b1709d2018-07-07 21:46:11 +02001329/**
1330 * batadv_purge_orig_ref() - Purge all outdated originators
1331 * @bat_priv: the bat priv with all the soft interface information
1332 */
1333void batadv_purge_orig_ref(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001334{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001335 struct batadv_hashtable *hash = bat_priv->orig_hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001336 struct hlist_node *node_tmp;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001337 struct hlist_head *head;
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001338 spinlock_t *list_lock; /* spinlock to protect write access */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001339 struct batadv_orig_node *orig_node;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001340 u32 i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001341
1342 if (!hash)
1343 return;
1344
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001345 /* for all origins... */
1346 for (i = 0; i < hash->size; i++) {
1347 head = &hash->table[i];
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001348 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001349
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001350 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001351 hlist_for_each_entry_safe(orig_node, node_tmp,
Marek Lindner7aadf882011-02-18 12:28:09 +00001352 head, hash_entry) {
Sven Eckelmann03fc7f82012-05-12 18:34:00 +02001353 if (batadv_purge_orig_node(bat_priv, orig_node)) {
Marek Lindner414254e2013-04-23 21:39:58 +08001354 batadv_gw_node_delete(bat_priv, orig_node);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001355 hlist_del_rcu(&orig_node->hash_entry);
Linus Lüssing9d31b3c2014-12-13 23:32:15 +01001356 batadv_tt_global_del_orig(orig_node->bat_priv,
1357 orig_node, -1,
1358 "originator timed out");
Sven Eckelmann5d967312016-01-17 11:01:09 +01001359 batadv_orig_node_put(orig_node);
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001360 continue;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001361 }
Martin Hundebøll610bfc6bc2013-05-23 16:53:02 +02001362
1363 batadv_frag_purge_orig(orig_node,
1364 batadv_frag_check_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001365 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001366 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001367 }
1368
Sven Eckelmann7cf06bc2012-05-12 02:09:29 +02001369 batadv_gw_election(bat_priv);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001370}
1371
Sven Eckelmann03fc7f82012-05-12 18:34:00 +02001372static void batadv_purge_orig(struct work_struct *work)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001373{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001374 struct delayed_work *delayed_work;
1375 struct batadv_priv *bat_priv;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001376
Geliang Tang4ba4bc02015-12-28 23:43:37 +08001377 delayed_work = to_delayed_work(work);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001378 bat_priv = container_of(delayed_work, struct batadv_priv, orig_work);
Sven Eckelmann3b1709d2018-07-07 21:46:11 +02001379 batadv_purge_orig_ref(bat_priv);
Antonio Quartulli72414442012-12-25 13:14:37 +01001380 queue_delayed_work(batadv_event_workqueue,
1381 &bat_priv->orig_work,
1382 msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001383}
1384
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +02001385#ifdef CONFIG_BATMAN_ADV_DEBUGFS
Sven Eckelmannff15c272017-12-02 19:51:53 +01001386
1387/**
1388 * batadv_orig_seq_print_text() - Print the originator table in a seq file
1389 * @seq: seq file to print on
1390 * @offset: not used
1391 *
1392 * Return: always 0
1393 */
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001394int batadv_orig_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001395{
1396 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001397 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001398 struct batadv_hard_iface *primary_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001399
Marek Lindner30da63a2012-08-03 17:15:46 +02001400 primary_if = batadv_seq_print_text_primary_if_get(seq);
1401 if (!primary_if)
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001402 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001403
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001404 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 +02001405 BATADV_SOURCE_VERSION, primary_if->net_dev->name,
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001406 primary_if->net_dev->dev_addr, net_dev->name,
Antonio Quartulli29824a52016-05-25 23:27:31 +08001407 bat_priv->algo_ops->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001408
Sven Eckelmann82047ad2016-01-17 11:01:10 +01001409 batadv_hardif_put(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001410
Antonio Quartulli29824a52016-05-25 23:27:31 +08001411 if (!bat_priv->algo_ops->orig.print) {
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001412 seq_puts(seq,
1413 "No printing function for this routing protocol\n");
1414 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001415 }
1416
Antonio Quartulli29824a52016-05-25 23:27:31 +08001417 bat_priv->algo_ops->orig.print(bat_priv, seq, BATADV_IF_DEFAULT);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001418
Marek Lindner30da63a2012-08-03 17:15:46 +02001419 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001420}
1421
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001422/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001423 * batadv_orig_hardif_seq_print_text() - writes originator infos for a specific
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001424 * outgoing interface
1425 * @seq: debugfs table seq_file struct
1426 * @offset: not used
1427 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001428 * Return: 0
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001429 */
1430int batadv_orig_hardif_seq_print_text(struct seq_file *seq, void *offset)
1431{
1432 struct net_device *net_dev = (struct net_device *)seq->private;
1433 struct batadv_hard_iface *hard_iface;
1434 struct batadv_priv *bat_priv;
1435
1436 hard_iface = batadv_hardif_get_by_netdev(net_dev);
1437
1438 if (!hard_iface || !hard_iface->soft_iface) {
1439 seq_puts(seq, "Interface not known to B.A.T.M.A.N.\n");
1440 goto out;
1441 }
1442
1443 bat_priv = netdev_priv(hard_iface->soft_iface);
Antonio Quartulli29824a52016-05-25 23:27:31 +08001444 if (!bat_priv->algo_ops->orig.print) {
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001445 seq_puts(seq,
1446 "No printing function for this routing protocol\n");
1447 goto out;
1448 }
1449
1450 if (hard_iface->if_status != BATADV_IF_ACTIVE) {
1451 seq_puts(seq, "Interface not active\n");
1452 goto out;
1453 }
1454
1455 seq_printf(seq, "[B.A.T.M.A.N. adv %s, IF/MAC: %s/%pM (%s %s)]\n",
1456 BATADV_SOURCE_VERSION, hard_iface->net_dev->name,
1457 hard_iface->net_dev->dev_addr,
Antonio Quartulli29824a52016-05-25 23:27:31 +08001458 hard_iface->soft_iface->name, bat_priv->algo_ops->name);
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001459
Antonio Quartulli29824a52016-05-25 23:27:31 +08001460 bat_priv->algo_ops->orig.print(bat_priv, seq, hard_iface);
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001461
1462out:
Marek Lindner16a41422014-04-24 03:44:25 +08001463 if (hard_iface)
Sven Eckelmann82047ad2016-01-17 11:01:10 +01001464 batadv_hardif_put(hard_iface);
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001465 return 0;
1466}
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +02001467#endif
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001468
Matthias Schiffer85cf8c82016-07-03 13:31:39 +02001469/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001470 * batadv_orig_dump() - Dump to netlink the originator infos for a specific
Matthias Schiffer85cf8c82016-07-03 13:31:39 +02001471 * outgoing interface
1472 * @msg: message to dump into
1473 * @cb: parameters for the dump
1474 *
1475 * Return: 0 or error value
1476 */
1477int batadv_orig_dump(struct sk_buff *msg, struct netlink_callback *cb)
1478{
1479 struct net *net = sock_net(cb->skb->sk);
1480 struct net_device *soft_iface;
1481 struct net_device *hard_iface = NULL;
1482 struct batadv_hard_iface *hardif = BATADV_IF_DEFAULT;
1483 struct batadv_priv *bat_priv;
1484 struct batadv_hard_iface *primary_if = NULL;
1485 int ret;
1486 int ifindex, hard_ifindex;
1487
1488 ifindex = batadv_netlink_get_ifindex(cb->nlh, BATADV_ATTR_MESH_IFINDEX);
1489 if (!ifindex)
1490 return -EINVAL;
1491
1492 soft_iface = dev_get_by_index(net, ifindex);
1493 if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
1494 ret = -ENODEV;
1495 goto out;
1496 }
1497
1498 bat_priv = netdev_priv(soft_iface);
1499
1500 primary_if = batadv_primary_if_get_selected(bat_priv);
1501 if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
1502 ret = -ENOENT;
1503 goto out;
1504 }
1505
1506 hard_ifindex = batadv_netlink_get_ifindex(cb->nlh,
1507 BATADV_ATTR_HARD_IFINDEX);
1508 if (hard_ifindex) {
1509 hard_iface = dev_get_by_index(net, hard_ifindex);
1510 if (hard_iface)
1511 hardif = batadv_hardif_get_by_netdev(hard_iface);
1512
1513 if (!hardif) {
1514 ret = -ENODEV;
1515 goto out;
1516 }
1517
1518 if (hardif->soft_iface != soft_iface) {
1519 ret = -ENOENT;
1520 goto out;
1521 }
1522 }
1523
1524 if (!bat_priv->algo_ops->orig.dump) {
1525 ret = -EOPNOTSUPP;
1526 goto out;
1527 }
1528
1529 bat_priv->algo_ops->orig.dump(msg, cb, bat_priv, hardif);
1530
1531 ret = msg->len;
1532
1533 out:
1534 if (hardif)
1535 batadv_hardif_put(hardif);
1536 if (hard_iface)
1537 dev_put(hard_iface);
1538 if (primary_if)
1539 batadv_hardif_put(primary_if);
1540 if (soft_iface)
1541 dev_put(soft_iface);
1542
1543 return ret;
1544}