blob: 0225616d5771d0986127322142fc591780fc25b0 [file] [log] [blame]
Sven Eckelmann7db7d9f2017-11-19 15:05:11 +01001// SPDX-License-Identifier: GPL-2.0
Sven Eckelmann6b1aea82018-01-01 00:00:00 +01002/* Copyright (C) 2007-2018 B.A.T.M.A.N. contributors:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00003 *
Antonio Quartulli35c133a2012-03-14 13:03:01 +01004 * Marek Lindner, Simon Wunderlich, Antonio Quartulli
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2 of the GNU General Public
8 * License as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
Antonio Quartulliebf38fb2013-11-03 20:40:48 +010016 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000017 */
18
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000019#include "translation-table.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020020#include "main.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000021
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020022#include <linux/atomic.h>
Linus Lüssingac4eebd2015-06-16 17:10:24 +020023#include <linux/bitops.h>
Sven Eckelmannecc36f52017-11-19 17:12:03 +010024#include <linux/build_bug.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020025#include <linux/byteorder/generic.h>
Sven Eckelmann86452f82016-06-25 16:44:06 +020026#include <linux/cache.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020027#include <linux/compiler.h>
Antonio Quartulliced72932013-04-24 16:37:51 +020028#include <linux/crc32c.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020029#include <linux/errno.h>
30#include <linux/etherdevice.h>
Sven Eckelmannb92b94a2017-11-19 17:12:02 +010031#include <linux/gfp.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020032#include <linux/if_ether.h>
Sven Eckelmann86452f82016-06-25 16:44:06 +020033#include <linux/init.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020034#include <linux/jhash.h>
35#include <linux/jiffies.h>
36#include <linux/kernel.h>
Sven Eckelmann6e8ef692016-01-16 10:29:50 +010037#include <linux/kref.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020038#include <linux/list.h>
39#include <linux/lockdep.h>
Sven Eckelmann3a644692017-11-19 17:12:04 +010040#include <linux/net.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020041#include <linux/netdevice.h>
Matthias Schifferd34f0552016-07-03 13:31:37 +020042#include <linux/netlink.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020043#include <linux/rculist.h>
44#include <linux/rcupdate.h>
45#include <linux/seq_file.h>
Matthias Schifferd34f0552016-07-03 13:31:37 +020046#include <linux/skbuff.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020047#include <linux/slab.h>
48#include <linux/spinlock.h>
49#include <linux/stddef.h>
50#include <linux/string.h>
51#include <linux/workqueue.h>
Matthias Schifferd34f0552016-07-03 13:31:37 +020052#include <net/genetlink.h>
53#include <net/netlink.h>
54#include <net/sock.h>
Sven Eckelmannfec149f2017-12-21 10:17:41 +010055#include <uapi/linux/batadv_packet.h>
Matthias Schifferd34f0552016-07-03 13:31:37 +020056#include <uapi/linux/batman_adv.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020057
58#include "bridge_loop_avoidance.h"
59#include "hard-interface.h"
60#include "hash.h"
Sven Eckelmannba412082016-05-15 23:48:31 +020061#include "log.h"
Matthias Schifferd34f0552016-07-03 13:31:37 +020062#include "netlink.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020063#include "originator.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020064#include "soft-interface.h"
Markus Pargmann1f8dce42016-05-15 11:07:43 +020065#include "tvlv.h"
Antonio Quartullia73105b2011-04-27 14:27:44 +020066
Sven Eckelmann86452f82016-06-25 16:44:06 +020067static struct kmem_cache *batadv_tl_cache __read_mostly;
68static struct kmem_cache *batadv_tg_cache __read_mostly;
69static struct kmem_cache *batadv_tt_orig_cache __read_mostly;
70static struct kmem_cache *batadv_tt_change_cache __read_mostly;
71static struct kmem_cache *batadv_tt_req_cache __read_mostly;
72static struct kmem_cache *batadv_tt_roam_cache __read_mostly;
73
Antonio Quartullidec05072012-11-10 11:00:32 +010074/* hash class keys */
75static struct lock_class_key batadv_tt_local_hash_lock_class_key;
76static struct lock_class_key batadv_tt_global_hash_lock_class_key;
77
Sven Eckelmann6b5e9712015-05-26 18:34:26 +020078static void batadv_send_roam_adv(struct batadv_priv *bat_priv, u8 *client,
Antonio Quartullic018ad32013-06-04 12:11:39 +020079 unsigned short vid,
Sven Eckelmann56303d32012-06-05 22:31:31 +020080 struct batadv_orig_node *orig_node);
Sven Eckelmanna5130882012-05-16 20:23:16 +020081static void batadv_tt_purge(struct work_struct *work);
82static void
Sven Eckelmann56303d32012-06-05 22:31:31 +020083batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry);
Antonio Quartulli30cfd022012-07-05 23:38:29 +020084static void batadv_tt_global_del(struct batadv_priv *bat_priv,
85 struct batadv_orig_node *orig_node,
86 const unsigned char *addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +020087 unsigned short vid, const char *message,
88 bool roaming);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000089
Sven Eckelmann62fe7102015-09-15 19:00:48 +020090/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +010091 * batadv_compare_tt() - check if two TT entries are the same
Antonio Quartullid15cd622015-11-17 16:40:52 +080092 * @node: the list element pointer of the first TT entry
93 * @data2: pointer to the tt_common_entry of the second TT entry
Sven Eckelmann62fe7102015-09-15 19:00:48 +020094 *
Antonio Quartullid15cd622015-11-17 16:40:52 +080095 * Compare the MAC address and the VLAN ID of the two TT entries and check if
96 * they are the same TT client.
Sven Eckelmann4b426b12016-02-22 21:02:39 +010097 * Return: true if the two TT clients are the same, false otherwise
Sven Eckelmann62fe7102015-09-15 19:00:48 +020098 */
Sven Eckelmann4b426b12016-02-22 21:02:39 +010099static bool batadv_compare_tt(const struct hlist_node *node, const void *data2)
Marek Lindner7aadf882011-02-18 12:28:09 +0000100{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200101 const void *data1 = container_of(node, struct batadv_tt_common_entry,
Sven Eckelmann747e4222011-05-14 23:14:50 +0200102 hash_entry);
Marek Lindner4c718952015-08-06 10:38:54 +0200103 const struct batadv_tt_common_entry *tt1 = data1;
104 const struct batadv_tt_common_entry *tt2 = data2;
Marek Lindner7aadf882011-02-18 12:28:09 +0000105
Marek Lindner4c718952015-08-06 10:38:54 +0200106 return (tt1->vid == tt2->vid) && batadv_compare_eth(data1, data2);
Marek Lindner7aadf882011-02-18 12:28:09 +0000107}
108
Antonio Quartullic018ad32013-06-04 12:11:39 +0200109/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100110 * batadv_choose_tt() - return the index of the tt entry in the hash table
Antonio Quartullic018ad32013-06-04 12:11:39 +0200111 * @data: pointer to the tt_common_entry object to map
112 * @size: the size of the hash table
113 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200114 * Return: the hash index where the object represented by 'data' should be
Antonio Quartullic018ad32013-06-04 12:11:39 +0200115 * stored at.
116 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200117static inline u32 batadv_choose_tt(const void *data, u32 size)
Antonio Quartullic018ad32013-06-04 12:11:39 +0200118{
119 struct batadv_tt_common_entry *tt;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200120 u32 hash = 0;
Antonio Quartullic018ad32013-06-04 12:11:39 +0200121
122 tt = (struct batadv_tt_common_entry *)data;
Sven Eckelmann36fd61c2015-03-01 09:46:18 +0100123 hash = jhash(&tt->addr, ETH_ALEN, hash);
124 hash = jhash(&tt->vid, sizeof(tt->vid), hash);
Antonio Quartullic018ad32013-06-04 12:11:39 +0200125
126 return hash % size;
127}
128
129/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100130 * batadv_tt_hash_find() - look for a client in the given hash table
Antonio Quartullic018ad32013-06-04 12:11:39 +0200131 * @hash: the hash table to search
132 * @addr: the mac address of the client to look for
133 * @vid: VLAN identifier
134 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200135 * Return: a pointer to the tt_common struct belonging to the searched client if
Antonio Quartullic018ad32013-06-04 12:11:39 +0200136 * found, NULL otherwise.
137 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200138static struct batadv_tt_common_entry *
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200139batadv_tt_hash_find(struct batadv_hashtable *hash, const u8 *addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200140 unsigned short vid)
Marek Lindner7aadf882011-02-18 12:28:09 +0000141{
Marek Lindner7aadf882011-02-18 12:28:09 +0000142 struct hlist_head *head;
Antonio Quartullic018ad32013-06-04 12:11:39 +0200143 struct batadv_tt_common_entry to_search, *tt, *tt_tmp = NULL;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200144 u32 index;
Marek Lindner7aadf882011-02-18 12:28:09 +0000145
146 if (!hash)
147 return NULL;
148
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100149 ether_addr_copy(to_search.addr, addr);
Antonio Quartullic018ad32013-06-04 12:11:39 +0200150 to_search.vid = vid;
151
152 index = batadv_choose_tt(&to_search, hash->size);
Marek Lindner7aadf882011-02-18 12:28:09 +0000153 head = &hash->table[index];
154
155 rcu_read_lock();
Antonio Quartullic018ad32013-06-04 12:11:39 +0200156 hlist_for_each_entry_rcu(tt, head, hash_entry) {
157 if (!batadv_compare_eth(tt, addr))
Marek Lindner7aadf882011-02-18 12:28:09 +0000158 continue;
159
Antonio Quartullic018ad32013-06-04 12:11:39 +0200160 if (tt->vid != vid)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200161 continue;
162
Sven Eckelmann92dcdf02016-01-16 10:29:57 +0100163 if (!kref_get_unless_zero(&tt->refcount))
Antonio Quartullic018ad32013-06-04 12:11:39 +0200164 continue;
165
166 tt_tmp = tt;
Marek Lindner7aadf882011-02-18 12:28:09 +0000167 break;
168 }
169 rcu_read_unlock();
170
Antonio Quartullic018ad32013-06-04 12:11:39 +0200171 return tt_tmp;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100172}
173
Antonio Quartullic018ad32013-06-04 12:11:39 +0200174/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100175 * batadv_tt_local_hash_find() - search the local table for a given client
Antonio Quartullic018ad32013-06-04 12:11:39 +0200176 * @bat_priv: the bat priv with all the soft interface information
177 * @addr: the mac address of the client to look for
178 * @vid: VLAN identifier
179 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200180 * Return: a pointer to the corresponding tt_local_entry struct if the client is
Antonio Quartullic018ad32013-06-04 12:11:39 +0200181 * found, NULL otherwise.
182 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200183static struct batadv_tt_local_entry *
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200184batadv_tt_local_hash_find(struct batadv_priv *bat_priv, const u8 *addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200185 unsigned short vid)
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100186{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200187 struct batadv_tt_common_entry *tt_common_entry;
188 struct batadv_tt_local_entry *tt_local_entry = NULL;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100189
Antonio Quartullic018ad32013-06-04 12:11:39 +0200190 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.local_hash, addr,
191 vid);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100192 if (tt_common_entry)
193 tt_local_entry = container_of(tt_common_entry,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200194 struct batadv_tt_local_entry,
195 common);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100196 return tt_local_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +0000197}
198
Antonio Quartullic018ad32013-06-04 12:11:39 +0200199/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100200 * batadv_tt_global_hash_find() - search the global table for a given client
Antonio Quartullic018ad32013-06-04 12:11:39 +0200201 * @bat_priv: the bat priv with all the soft interface information
202 * @addr: the mac address of the client to look for
203 * @vid: VLAN identifier
204 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200205 * Return: a pointer to the corresponding tt_global_entry struct if the client
Antonio Quartullic018ad32013-06-04 12:11:39 +0200206 * is found, NULL otherwise.
207 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200208static struct batadv_tt_global_entry *
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200209batadv_tt_global_hash_find(struct batadv_priv *bat_priv, const u8 *addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200210 unsigned short vid)
Marek Lindner7aadf882011-02-18 12:28:09 +0000211{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200212 struct batadv_tt_common_entry *tt_common_entry;
213 struct batadv_tt_global_entry *tt_global_entry = NULL;
Marek Lindner7aadf882011-02-18 12:28:09 +0000214
Antonio Quartullic018ad32013-06-04 12:11:39 +0200215 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.global_hash, addr,
216 vid);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100217 if (tt_common_entry)
218 tt_global_entry = container_of(tt_common_entry,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200219 struct batadv_tt_global_entry,
220 common);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100221 return tt_global_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +0000222}
223
Sven Eckelmann92dcdf02016-01-16 10:29:57 +0100224/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100225 * batadv_tt_local_entry_free_rcu() - free the tt_local_entry
Sven Eckelmann86452f82016-06-25 16:44:06 +0200226 * @rcu: rcu pointer of the tt_local_entry
227 */
228static void batadv_tt_local_entry_free_rcu(struct rcu_head *rcu)
229{
230 struct batadv_tt_local_entry *tt_local_entry;
231
232 tt_local_entry = container_of(rcu, struct batadv_tt_local_entry,
233 common.rcu);
234
235 kmem_cache_free(batadv_tl_cache, tt_local_entry);
236}
237
238/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100239 * batadv_tt_local_entry_release() - release tt_local_entry from lists and queue
Sven Eckelmann92dcdf02016-01-16 10:29:57 +0100240 * for free after rcu grace period
241 * @ref: kref pointer of the nc_node
242 */
243static void batadv_tt_local_entry_release(struct kref *ref)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200244{
Sven Eckelmann92dcdf02016-01-16 10:29:57 +0100245 struct batadv_tt_local_entry *tt_local_entry;
246
247 tt_local_entry = container_of(ref, struct batadv_tt_local_entry,
248 common.refcount);
249
Sven Eckelmanna33d9702016-03-11 16:44:05 +0100250 batadv_softif_vlan_put(tt_local_entry->vlan);
251
Sven Eckelmann86452f82016-06-25 16:44:06 +0200252 call_rcu(&tt_local_entry->common.rcu, batadv_tt_local_entry_free_rcu);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200253}
254
Antonio Quartulli21026052013-05-07 00:29:22 +0200255/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100256 * batadv_tt_local_entry_put() - decrement the tt_local_entry refcounter and
Sven Eckelmann92dcdf02016-01-16 10:29:57 +0100257 * possibly release it
258 * @tt_local_entry: tt_local_entry to be free'd
259 */
260static void
Sven Eckelmann95c0db92016-01-17 11:01:25 +0100261batadv_tt_local_entry_put(struct batadv_tt_local_entry *tt_local_entry)
Sven Eckelmann92dcdf02016-01-16 10:29:57 +0100262{
263 kref_put(&tt_local_entry->common.refcount,
264 batadv_tt_local_entry_release);
265}
266
267/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100268 * batadv_tt_global_entry_free_rcu() - free the tt_global_entry
Sven Eckelmann86452f82016-06-25 16:44:06 +0200269 * @rcu: rcu pointer of the tt_global_entry
270 */
271static void batadv_tt_global_entry_free_rcu(struct rcu_head *rcu)
272{
273 struct batadv_tt_global_entry *tt_global_entry;
274
275 tt_global_entry = container_of(rcu, struct batadv_tt_global_entry,
276 common.rcu);
277
278 kmem_cache_free(batadv_tg_cache, tt_global_entry);
279}
280
281/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100282 * batadv_tt_global_entry_release() - release tt_global_entry from lists and
283 * queue for free after rcu grace period
Sven Eckelmann92dcdf02016-01-16 10:29:57 +0100284 * @ref: kref pointer of the nc_node
285 */
286static void batadv_tt_global_entry_release(struct kref *ref)
287{
288 struct batadv_tt_global_entry *tt_global_entry;
289
290 tt_global_entry = container_of(ref, struct batadv_tt_global_entry,
291 common.refcount);
292
293 batadv_tt_global_del_orig_list(tt_global_entry);
Sven Eckelmann86452f82016-06-25 16:44:06 +0200294
295 call_rcu(&tt_global_entry->common.rcu, batadv_tt_global_entry_free_rcu);
Sven Eckelmann92dcdf02016-01-16 10:29:57 +0100296}
297
298/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100299 * batadv_tt_global_entry_put() - decrement the tt_global_entry refcounter and
Sven Eckelmann5dafd8a2016-01-17 11:01:26 +0100300 * possibly release it
Sven Eckelmann92dcdf02016-01-16 10:29:57 +0100301 * @tt_global_entry: tt_global_entry to be free'd
Antonio Quartulli21026052013-05-07 00:29:22 +0200302 */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200303static void
Sven Eckelmann5dafd8a2016-01-17 11:01:26 +0100304batadv_tt_global_entry_put(struct batadv_tt_global_entry *tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200305{
Sven Eckelmann92dcdf02016-01-16 10:29:57 +0100306 kref_put(&tt_global_entry->common.refcount,
307 batadv_tt_global_entry_release);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200308}
309
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100310/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100311 * batadv_tt_global_hash_count() - count the number of orig entries
Antonio Quartullid15cd622015-11-17 16:40:52 +0800312 * @bat_priv: the bat priv with all the soft interface information
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100313 * @addr: the mac address of the client to count entries for
314 * @vid: VLAN identifier
315 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200316 * Return: the number of originators advertising the given address/data
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100317 * (excluding ourself).
318 */
319int batadv_tt_global_hash_count(struct batadv_priv *bat_priv,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200320 const u8 *addr, unsigned short vid)
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100321{
322 struct batadv_tt_global_entry *tt_global_entry;
323 int count;
324
325 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
326 if (!tt_global_entry)
327 return 0;
328
329 count = atomic_read(&tt_global_entry->orig_list_count);
Sven Eckelmann5dafd8a2016-01-17 11:01:26 +0100330 batadv_tt_global_entry_put(tt_global_entry);
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100331
332 return count;
333}
334
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200335/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100336 * batadv_tt_local_size_mod() - change the size by v of the local table
337 * identified by vid
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200338 * @bat_priv: the bat priv with all the soft interface information
339 * @vid: the VLAN identifier of the sub-table to change
340 * @v: the amount to sum to the local table size
341 */
342static void batadv_tt_local_size_mod(struct batadv_priv *bat_priv,
343 unsigned short vid, int v)
344{
345 struct batadv_softif_vlan *vlan;
346
347 vlan = batadv_softif_vlan_get(bat_priv, vid);
348 if (!vlan)
349 return;
350
351 atomic_add(v, &vlan->tt.num_entries);
352
Sven Eckelmann9c3bf082016-01-17 11:01:21 +0100353 batadv_softif_vlan_put(vlan);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200354}
355
356/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100357 * batadv_tt_local_size_inc() - increase by one the local table size for the
358 * given vid
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200359 * @bat_priv: the bat priv with all the soft interface information
360 * @vid: the VLAN identifier
361 */
362static void batadv_tt_local_size_inc(struct batadv_priv *bat_priv,
363 unsigned short vid)
364{
365 batadv_tt_local_size_mod(bat_priv, vid, 1);
366}
367
368/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100369 * batadv_tt_local_size_dec() - decrease by one the local table size for the
370 * given vid
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200371 * @bat_priv: the bat priv with all the soft interface information
372 * @vid: the VLAN identifier
373 */
374static void batadv_tt_local_size_dec(struct batadv_priv *bat_priv,
375 unsigned short vid)
376{
377 batadv_tt_local_size_mod(bat_priv, vid, -1);
378}
379
380/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100381 * batadv_tt_global_size_mod() - change the size by v of the global table
Antonio Quartullid15cd622015-11-17 16:40:52 +0800382 * for orig_node identified by vid
383 * @orig_node: the originator for which the table has to be modified
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200384 * @vid: the VLAN identifier
385 * @v: the amount to sum to the global table size
386 */
387static void batadv_tt_global_size_mod(struct batadv_orig_node *orig_node,
388 unsigned short vid, int v)
389{
390 struct batadv_orig_node_vlan *vlan;
391
392 vlan = batadv_orig_node_vlan_new(orig_node, vid);
393 if (!vlan)
394 return;
395
396 if (atomic_add_return(v, &vlan->tt.num_entries) == 0) {
397 spin_lock_bh(&orig_node->vlan_list_lock);
Sven Eckelmann3db15202016-01-31 13:28:00 +0100398 if (!hlist_unhashed(&vlan->list)) {
399 hlist_del_init_rcu(&vlan->list);
Sven Eckelmann21754e22016-01-17 11:01:24 +0100400 batadv_orig_node_vlan_put(vlan);
Sven Eckelmann3db15202016-01-31 13:28:00 +0100401 }
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200402 spin_unlock_bh(&orig_node->vlan_list_lock);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200403 }
404
Sven Eckelmann21754e22016-01-17 11:01:24 +0100405 batadv_orig_node_vlan_put(vlan);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200406}
407
408/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100409 * batadv_tt_global_size_inc() - increase by one the global table size for the
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200410 * given vid
411 * @orig_node: the originator which global table size has to be decreased
412 * @vid: the vlan identifier
413 */
414static void batadv_tt_global_size_inc(struct batadv_orig_node *orig_node,
415 unsigned short vid)
416{
417 batadv_tt_global_size_mod(orig_node, vid, 1);
418}
419
420/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100421 * batadv_tt_global_size_dec() - decrease by one the global table size for the
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200422 * given vid
423 * @orig_node: the originator which global table size has to be decreased
424 * @vid: the vlan identifier
425 */
426static void batadv_tt_global_size_dec(struct batadv_orig_node *orig_node,
427 unsigned short vid)
428{
429 batadv_tt_global_size_mod(orig_node, vid, -1);
430}
431
Sven Eckelmann42eff6a2016-01-05 12:06:20 +0100432/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100433 * batadv_tt_orig_list_entry_free_rcu() - free the orig_entry
Sven Eckelmann86452f82016-06-25 16:44:06 +0200434 * @rcu: rcu pointer of the orig_entry
435 */
436static void batadv_tt_orig_list_entry_free_rcu(struct rcu_head *rcu)
437{
438 struct batadv_tt_orig_list_entry *orig_entry;
439
440 orig_entry = container_of(rcu, struct batadv_tt_orig_list_entry, rcu);
441
442 kmem_cache_free(batadv_tt_orig_cache, orig_entry);
443}
444
445/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100446 * batadv_tt_orig_list_entry_release() - release tt orig entry from lists and
Sven Eckelmann42eff6a2016-01-05 12:06:20 +0100447 * queue for free after rcu grace period
Sven Eckelmann6e8ef692016-01-16 10:29:50 +0100448 * @ref: kref pointer of the tt orig entry
Sven Eckelmann42eff6a2016-01-05 12:06:20 +0100449 */
Sven Eckelmann6e8ef692016-01-16 10:29:50 +0100450static void batadv_tt_orig_list_entry_release(struct kref *ref)
Sven Eckelmann42eff6a2016-01-05 12:06:20 +0100451{
Sven Eckelmann6e8ef692016-01-16 10:29:50 +0100452 struct batadv_tt_orig_list_entry *orig_entry;
453
454 orig_entry = container_of(ref, struct batadv_tt_orig_list_entry,
455 refcount);
456
Sven Eckelmann5d967312016-01-17 11:01:09 +0100457 batadv_orig_node_put(orig_entry->orig_node);
Sven Eckelmann86452f82016-06-25 16:44:06 +0200458 call_rcu(&orig_entry->rcu, batadv_tt_orig_list_entry_free_rcu);
Sven Eckelmann42eff6a2016-01-05 12:06:20 +0100459}
460
Sven Eckelmann6e8ef692016-01-16 10:29:50 +0100461/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100462 * batadv_tt_orig_list_entry_put() - decrement the tt orig entry refcounter and
Sven Eckelmann7e2366c2016-01-17 11:01:27 +0100463 * possibly release it
Sven Eckelmann6e8ef692016-01-16 10:29:50 +0100464 * @orig_entry: tt orig entry to be free'd
465 */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200466static void
Sven Eckelmann7e2366c2016-01-17 11:01:27 +0100467batadv_tt_orig_list_entry_put(struct batadv_tt_orig_list_entry *orig_entry)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200468{
Sven Eckelmann6e8ef692016-01-16 10:29:50 +0100469 kref_put(&orig_entry->refcount, batadv_tt_orig_list_entry_release);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200470}
471
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200472/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100473 * batadv_tt_local_event() - store a local TT event (ADD/DEL)
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200474 * @bat_priv: the bat priv with all the soft interface information
475 * @tt_local_entry: the TT entry involved in the event
476 * @event_flags: flags to store in the event structure
477 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200478static void batadv_tt_local_event(struct batadv_priv *bat_priv,
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200479 struct batadv_tt_local_entry *tt_local_entry,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200480 u8 event_flags)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200481{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200482 struct batadv_tt_change_node *tt_change_node, *entry, *safe;
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200483 struct batadv_tt_common_entry *common = &tt_local_entry->common;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200484 u8 flags = common->flags | event_flags;
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200485 bool event_removed = false;
486 bool del_op_requested, del_op_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200487
Sven Eckelmann86452f82016-06-25 16:44:06 +0200488 tt_change_node = kmem_cache_alloc(batadv_tt_change_cache, GFP_ATOMIC);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200489 if (!tt_change_node)
490 return;
491
Antonio Quartulliff66c972011-06-30 01:14:00 +0200492 tt_change_node->change.flags = flags;
Antonio Quartullica663042013-12-15 13:26:55 +0100493 memset(tt_change_node->change.reserved, 0,
494 sizeof(tt_change_node->change.reserved));
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100495 ether_addr_copy(tt_change_node->change.addr, common->addr);
Antonio Quartullic018ad32013-06-04 12:11:39 +0200496 tt_change_node->change.vid = htons(common->vid);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200497
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200498 del_op_requested = flags & BATADV_TT_CLIENT_DEL;
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200499
500 /* check for ADD+DEL or DEL+ADD events */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200501 spin_lock_bh(&bat_priv->tt.changes_list_lock);
502 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200503 list) {
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200504 if (!batadv_compare_eth(entry->change.addr, common->addr))
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200505 continue;
506
507 /* DEL+ADD in the same orig interval have no effect and can be
508 * removed to avoid silly behaviour on the receiver side. The
509 * other way around (ADD+DEL) can happen in case of roaming of
510 * a client still in the NEW state. Roaming of NEW clients is
511 * now possible due to automatically recognition of "temporary"
512 * clients
513 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200514 del_op_entry = entry->change.flags & BATADV_TT_CLIENT_DEL;
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200515 if (!del_op_requested && del_op_entry)
516 goto del;
517 if (del_op_requested && !del_op_entry)
518 goto del;
Antonio Quartulli3c4f7ab2013-10-13 02:50:19 +0200519
520 /* this is a second add in the same originator interval. It
521 * means that flags have been changed: update them!
522 */
523 if (!del_op_requested && !del_op_entry)
524 entry->change.flags = flags;
525
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200526 continue;
527del:
528 list_del(&entry->list);
Sven Eckelmann86452f82016-06-25 16:44:06 +0200529 kmem_cache_free(batadv_tt_change_cache, entry);
530 kmem_cache_free(batadv_tt_change_cache, tt_change_node);
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200531 event_removed = true;
532 goto unlock;
533 }
534
Antonio Quartullia73105b2011-04-27 14:27:44 +0200535 /* track the change in the OGMinterval list */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200536 list_add_tail(&tt_change_node->list, &bat_priv->tt.changes_list);
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200537
538unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +0200539 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200540
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200541 if (event_removed)
Sven Eckelmann807736f2012-07-15 22:26:51 +0200542 atomic_dec(&bat_priv->tt.local_changes);
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200543 else
Sven Eckelmann807736f2012-07-15 22:26:51 +0200544 atomic_inc(&bat_priv->tt.local_changes);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200545}
546
Marek Lindner335fbe02013-04-23 21:40:02 +0800547/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100548 * batadv_tt_len() - compute length in bytes of given number of tt changes
Marek Lindner335fbe02013-04-23 21:40:02 +0800549 * @changes_num: number of tt changes
550 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200551 * Return: computed length in bytes.
Marek Lindner335fbe02013-04-23 21:40:02 +0800552 */
553static int batadv_tt_len(int changes_num)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200554{
Marek Lindner335fbe02013-04-23 21:40:02 +0800555 return changes_num * sizeof(struct batadv_tvlv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200556}
557
Antonio Quartulli298e6e62013-05-28 13:14:27 +0200558/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100559 * batadv_tt_entries() - compute the number of entries fitting in tt_len bytes
Antonio Quartulli298e6e62013-05-28 13:14:27 +0200560 * @tt_len: available space
561 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200562 * Return: the number of entries.
Antonio Quartulli298e6e62013-05-28 13:14:27 +0200563 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200564static u16 batadv_tt_entries(u16 tt_len)
Antonio Quartulli298e6e62013-05-28 13:14:27 +0200565{
566 return tt_len / batadv_tt_len(1);
567}
568
Marek Lindnera19d3d82013-05-27 15:33:25 +0800569/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100570 * batadv_tt_local_table_transmit_size() - calculates the local translation
571 * table size when transmitted over the air
Marek Lindnera19d3d82013-05-27 15:33:25 +0800572 * @bat_priv: the bat priv with all the soft interface information
573 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200574 * Return: local translation table size in bytes.
Marek Lindnera19d3d82013-05-27 15:33:25 +0800575 */
576static int batadv_tt_local_table_transmit_size(struct batadv_priv *bat_priv)
577{
Sven Eckelmann4f248cf2015-06-09 20:50:49 +0200578 u16 num_vlan = 0;
579 u16 tt_local_entries = 0;
Marek Lindnera19d3d82013-05-27 15:33:25 +0800580 struct batadv_softif_vlan *vlan;
581 int hdr_size;
582
583 rcu_read_lock();
584 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
585 num_vlan++;
586 tt_local_entries += atomic_read(&vlan->tt.num_entries);
587 }
588 rcu_read_unlock();
589
590 /* header size of tvlv encapsulated tt response payload */
591 hdr_size = sizeof(struct batadv_unicast_tvlv_packet);
592 hdr_size += sizeof(struct batadv_tvlv_hdr);
593 hdr_size += sizeof(struct batadv_tvlv_tt_data);
594 hdr_size += num_vlan * sizeof(struct batadv_tvlv_tt_vlan_data);
595
596 return hdr_size + batadv_tt_len(tt_local_entries);
597}
598
Sven Eckelmann56303d32012-06-05 22:31:31 +0200599static int batadv_tt_local_init(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000600{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200601 if (bat_priv->tt.local_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200602 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000603
Sven Eckelmann807736f2012-07-15 22:26:51 +0200604 bat_priv->tt.local_hash = batadv_hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000605
Sven Eckelmann807736f2012-07-15 22:26:51 +0200606 if (!bat_priv->tt.local_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200607 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000608
Antonio Quartullidec05072012-11-10 11:00:32 +0100609 batadv_hash_set_lock_class(bat_priv->tt.local_hash,
610 &batadv_tt_local_hash_lock_class_key);
611
Sven Eckelmann5346c352012-05-05 13:27:28 +0200612 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000613}
614
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200615static void batadv_tt_global_free(struct batadv_priv *bat_priv,
616 struct batadv_tt_global_entry *tt_global,
617 const char *message)
618{
619 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +0200620 "Deleting global tt entry %pM (vid: %d): %s\n",
621 tt_global->common.addr,
Sven Eckelmannf7a2bd62017-02-22 17:16:42 +0100622 batadv_print_vid(tt_global->common.vid), message);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200623
624 batadv_hash_remove(bat_priv->tt.global_hash, batadv_compare_tt,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200625 batadv_choose_tt, &tt_global->common);
Sven Eckelmann5dafd8a2016-01-17 11:01:26 +0100626 batadv_tt_global_entry_put(tt_global);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200627}
628
Antonio Quartullic018ad32013-06-04 12:11:39 +0200629/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100630 * batadv_tt_local_add() - add a new client to the local table or update an
Antonio Quartullic018ad32013-06-04 12:11:39 +0200631 * existing client
632 * @soft_iface: netdev struct of the mesh interface
633 * @addr: the mac address of the client to add
634 * @vid: VLAN identifier
635 * @ifindex: index of the interface where the client is connected to (useful to
636 * identify wireless clients)
Antonio Quartulli9464d072013-11-16 12:03:48 +0100637 * @mark: the value contained in the skb->mark field of the received packet (if
638 * any)
Marek Lindnera19d3d82013-05-27 15:33:25 +0800639 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200640 * Return: true if the client was successfully added, false otherwise.
Antonio Quartullic018ad32013-06-04 12:11:39 +0200641 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200642bool batadv_tt_local_add(struct net_device *soft_iface, const u8 *addr,
643 unsigned short vid, int ifindex, u32 mark)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000644{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200645 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
Sven Eckelmann170173b2012-10-07 12:02:22 +0200646 struct batadv_tt_local_entry *tt_local;
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100647 struct batadv_tt_global_entry *tt_global = NULL;
Andrew Lunn2cd45a02016-04-21 12:57:27 +0200648 struct net *net = dev_net(soft_iface);
Antonio Quartulli35df3b22014-05-08 17:13:15 +0200649 struct batadv_softif_vlan *vlan;
Antonio Quartulli0c69aec2013-10-13 02:50:18 +0200650 struct net_device *in_dev = NULL;
Sven Eckelmann10b1bbb2016-09-30 15:21:03 +0200651 struct batadv_hard_iface *in_hardif = NULL;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200652 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200653 struct batadv_tt_orig_list_entry *orig_entry;
Marek Lindnera19d3d82013-05-27 15:33:25 +0800654 int hash_added, table_size, packet_size_max;
Sven Eckelmann4f248cf2015-06-09 20:50:49 +0200655 bool ret = false;
656 bool roamed_back = false;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200657 u8 remote_flags;
658 u32 match_mark;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000659
Antonio Quartulli0c69aec2013-10-13 02:50:18 +0200660 if (ifindex != BATADV_NULL_IFINDEX)
Andrew Lunn2cd45a02016-04-21 12:57:27 +0200661 in_dev = dev_get_by_index(net, ifindex);
Antonio Quartulli0c69aec2013-10-13 02:50:18 +0200662
Sven Eckelmann10b1bbb2016-09-30 15:21:03 +0200663 if (in_dev)
664 in_hardif = batadv_hardif_get_by_netdev(in_dev);
665
Antonio Quartullic018ad32013-06-04 12:11:39 +0200666 tt_local = batadv_tt_local_hash_find(bat_priv, addr, vid);
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100667
668 if (!is_multicast_ether_addr(addr))
669 tt_global = batadv_tt_global_hash_find(bat_priv, addr, vid);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000670
Antonio Quartulli47c94652012-09-23 22:38:35 +0200671 if (tt_local) {
672 tt_local->last_seen = jiffies;
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200673 if (tt_local->common.flags & BATADV_TT_CLIENT_PENDING) {
674 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +0200675 "Re-adding pending client %pM (vid: %d)\n",
Sven Eckelmannf7a2bd62017-02-22 17:16:42 +0100676 addr, batadv_print_vid(vid));
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200677 /* whatever the reason why the PENDING flag was set,
678 * this is a client which was enqueued to be removed in
679 * this orig_interval. Since it popped up again, the
680 * flag can be reset like it was never enqueued
681 */
682 tt_local->common.flags &= ~BATADV_TT_CLIENT_PENDING;
683 goto add_event;
684 }
685
686 if (tt_local->common.flags & BATADV_TT_CLIENT_ROAM) {
687 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +0200688 "Roaming client %pM (vid: %d) came back to its original location\n",
Sven Eckelmannf7a2bd62017-02-22 17:16:42 +0100689 addr, batadv_print_vid(vid));
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200690 /* the ROAM flag is set because this client roamed away
691 * and the node got a roaming_advertisement message. Now
692 * that the client popped up again at its original
693 * location such flag can be unset
694 */
695 tt_local->common.flags &= ~BATADV_TT_CLIENT_ROAM;
696 roamed_back = true;
697 }
698 goto check_roaming;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000699 }
700
Marek Lindnera19d3d82013-05-27 15:33:25 +0800701 /* Ignore the client if we cannot send it in a full table response. */
702 table_size = batadv_tt_local_table_transmit_size(bat_priv);
703 table_size += batadv_tt_len(1);
704 packet_size_max = atomic_read(&bat_priv->packet_size_max);
705 if (table_size > packet_size_max) {
706 net_ratelimited_function(batadv_info, soft_iface,
707 "Local translation table size (%i) exceeds maximum packet size (%i); Ignoring new local tt entry: %pM\n",
708 table_size, packet_size_max, addr);
709 goto out;
710 }
711
Sven Eckelmann86452f82016-06-25 16:44:06 +0200712 tt_local = kmem_cache_alloc(batadv_tl_cache, GFP_ATOMIC);
Antonio Quartulli47c94652012-09-23 22:38:35 +0200713 if (!tt_local)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200714 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200715
Antonio Quartulli35df3b22014-05-08 17:13:15 +0200716 /* increase the refcounter of the related vlan */
717 vlan = batadv_softif_vlan_get(bat_priv, vid);
Simon Wunderlich0b3dd7d2016-06-26 11:16:09 +0200718 if (!vlan) {
719 net_ratelimited_function(batadv_info, soft_iface,
720 "adding TT local entry %pM to non-existent VLAN %d\n",
Sven Eckelmannf7a2bd62017-02-22 17:16:42 +0100721 addr, batadv_print_vid(vid));
Sven Eckelmann86452f82016-06-25 16:44:06 +0200722 kmem_cache_free(batadv_tl_cache, tt_local);
Sven Eckelmannfd7dec22015-08-18 13:37:01 +0200723 tt_local = NULL;
Marek Lindner354136b2015-06-09 21:24:36 +0800724 goto out;
Sven Eckelmannfd7dec22015-08-18 13:37:01 +0200725 }
Antonio Quartulli35df3b22014-05-08 17:13:15 +0200726
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200727 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +0200728 "Creating new local tt entry: %pM (vid: %d, ttvn: %d)\n",
Sven Eckelmannf7a2bd62017-02-22 17:16:42 +0100729 addr, batadv_print_vid(vid),
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200730 (u8)atomic_read(&bat_priv->tt.vn));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000731
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100732 ether_addr_copy(tt_local->common.addr, addr);
Antonio Quartulli8425ec62012-11-19 09:01:44 +0100733 /* The local entry has to be marked as NEW to avoid to send it in
734 * a full table response going out before the next ttvn increment
735 * (consistency check)
736 */
737 tt_local->common.flags = BATADV_TT_CLIENT_NEW;
Antonio Quartullic018ad32013-06-04 12:11:39 +0200738 tt_local->common.vid = vid;
Sven Eckelmann10b1bbb2016-09-30 15:21:03 +0200739 if (batadv_is_wifi_hardif(in_hardif))
Antonio Quartulli47c94652012-09-23 22:38:35 +0200740 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
Sven Eckelmann92dcdf02016-01-16 10:29:57 +0100741 kref_init(&tt_local->common.refcount);
Antonio Quartulli47c94652012-09-23 22:38:35 +0200742 tt_local->last_seen = jiffies;
743 tt_local->common.added_at = tt_local->last_seen;
Sven Eckelmanna33d9702016-03-11 16:44:05 +0100744 tt_local->vlan = vlan;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000745
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100746 /* the batman interface mac and multicast addresses should never be
747 * purged
748 */
749 if (batadv_compare_eth(addr, soft_iface->dev_addr) ||
750 is_multicast_ether_addr(addr))
Antonio Quartulli47c94652012-09-23 22:38:35 +0200751 tt_local->common.flags |= BATADV_TT_CLIENT_NOPURGE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000752
Sven Eckelmanne3387b22016-07-15 17:39:22 +0200753 kref_get(&tt_local->common.refcount);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200754 hash_added = batadv_hash_add(bat_priv->tt.local_hash, batadv_compare_tt,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200755 batadv_choose_tt, &tt_local->common,
Antonio Quartulli47c94652012-09-23 22:38:35 +0200756 &tt_local->common.hash_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100757
758 if (unlikely(hash_added != 0)) {
759 /* remove the reference for the hash */
Sven Eckelmann95c0db92016-01-17 11:01:25 +0100760 batadv_tt_local_entry_put(tt_local);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100761 goto out;
762 }
763
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200764add_event:
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200765 batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
Antonio Quartulliff66c972011-06-30 01:14:00 +0200766
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200767check_roaming:
768 /* Check whether it is a roaming, but don't do anything if the roaming
769 * process has already been handled
770 */
771 if (tt_global && !(tt_global->common.flags & BATADV_TT_CLIENT_ROAM)) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200772 /* These node are probably going to update their tt table */
Antonio Quartulli47c94652012-09-23 22:38:35 +0200773 head = &tt_global->orig_list;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200774 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800775 hlist_for_each_entry_rcu(orig_entry, head, list) {
Antonio Quartulli47c94652012-09-23 22:38:35 +0200776 batadv_send_roam_adv(bat_priv, tt_global->common.addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200777 tt_global->common.vid,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200778 orig_entry->orig_node);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200779 }
780 rcu_read_unlock();
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200781 if (roamed_back) {
782 batadv_tt_global_free(bat_priv, tt_global,
783 "Roaming canceled");
784 tt_global = NULL;
785 } else {
786 /* The global entry has to be marked as ROAMING and
787 * has to be kept for consistency purpose
788 */
789 tt_global->common.flags |= BATADV_TT_CLIENT_ROAM;
790 tt_global->roam_at = jiffies;
791 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200792 }
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200793
Antonio Quartulli3c4f7ab2013-10-13 02:50:19 +0200794 /* store the current remote flags before altering them. This helps
795 * understanding is flags are changing or not
796 */
797 remote_flags = tt_local->common.flags & BATADV_TT_REMOTE_MASK;
Marek Lindnera19d3d82013-05-27 15:33:25 +0800798
Sven Eckelmann10b1bbb2016-09-30 15:21:03 +0200799 if (batadv_is_wifi_hardif(in_hardif))
Antonio Quartulli3c4f7ab2013-10-13 02:50:19 +0200800 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
801 else
802 tt_local->common.flags &= ~BATADV_TT_CLIENT_WIFI;
803
Antonio Quartulli9464d072013-11-16 12:03:48 +0100804 /* check the mark in the skb: if it's equal to the configured
805 * isolation_mark, it means the packet is coming from an isolated
806 * non-mesh client
807 */
808 match_mark = (mark & bat_priv->isolation_mark_mask);
809 if (bat_priv->isolation_mark_mask &&
810 match_mark == bat_priv->isolation_mark)
811 tt_local->common.flags |= BATADV_TT_CLIENT_ISOLA;
812 else
813 tt_local->common.flags &= ~BATADV_TT_CLIENT_ISOLA;
814
Antonio Quartulli3c4f7ab2013-10-13 02:50:19 +0200815 /* if any "dynamic" flag has been modified, resend an ADD event for this
816 * entry so that all the nodes can get the new flags
817 */
818 if (remote_flags ^ (tt_local->common.flags & BATADV_TT_REMOTE_MASK))
819 batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
820
821 ret = true;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200822out:
Sven Eckelmann10b1bbb2016-09-30 15:21:03 +0200823 if (in_hardif)
824 batadv_hardif_put(in_hardif);
Antonio Quartulli0c69aec2013-10-13 02:50:18 +0200825 if (in_dev)
826 dev_put(in_dev);
Antonio Quartulli47c94652012-09-23 22:38:35 +0200827 if (tt_local)
Sven Eckelmann95c0db92016-01-17 11:01:25 +0100828 batadv_tt_local_entry_put(tt_local);
Antonio Quartulli47c94652012-09-23 22:38:35 +0200829 if (tt_global)
Sven Eckelmann5dafd8a2016-01-17 11:01:26 +0100830 batadv_tt_global_entry_put(tt_global);
Marek Lindnera19d3d82013-05-27 15:33:25 +0800831 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000832}
833
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800834/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100835 * batadv_tt_prepare_tvlv_global_data() - prepare the TVLV TT header to send
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200836 * within a TT Response directed to another node
837 * @orig_node: originator for which the TT data has to be prepared
838 * @tt_data: uninitialised pointer to the address of the TVLV buffer
839 * @tt_change: uninitialised pointer to the address of the area where the TT
840 * changed can be stored
841 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
842 * function reserves the amount of space needed to send the entire global TT
843 * table. In case of success the value is updated with the real amount of
844 * reserved bytes
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200845 * Allocate the needed amount of memory for the entire TT TVLV and write its
846 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
847 * objects, one per active VLAN served by the originator node.
848 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200849 * Return: the size of the allocated buffer or 0 in case of failure.
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200850 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200851static u16
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200852batadv_tt_prepare_tvlv_global_data(struct batadv_orig_node *orig_node,
853 struct batadv_tvlv_tt_data **tt_data,
854 struct batadv_tvlv_tt_change **tt_change,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200855 s32 *tt_len)
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200856{
Sven Eckelmann4f248cf2015-06-09 20:50:49 +0200857 u16 num_vlan = 0;
858 u16 num_entries = 0;
859 u16 change_offset;
860 u16 tvlv_len;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200861 struct batadv_tvlv_tt_vlan_data *tt_vlan;
862 struct batadv_orig_node_vlan *vlan;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200863 u8 *tt_change_ptr;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200864
865 rcu_read_lock();
Marek Lindnerd0fa4f32015-06-22 00:30:22 +0800866 hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200867 num_vlan++;
868 num_entries += atomic_read(&vlan->tt.num_entries);
869 }
870
871 change_offset = sizeof(**tt_data);
872 change_offset += num_vlan * sizeof(*tt_vlan);
873
874 /* if tt_len is negative, allocate the space needed by the full table */
875 if (*tt_len < 0)
876 *tt_len = batadv_tt_len(num_entries);
877
878 tvlv_len = *tt_len;
879 tvlv_len += change_offset;
880
881 *tt_data = kmalloc(tvlv_len, GFP_ATOMIC);
882 if (!*tt_data) {
883 *tt_len = 0;
884 goto out;
885 }
886
887 (*tt_data)->flags = BATADV_NO_FLAGS;
888 (*tt_data)->ttvn = atomic_read(&orig_node->last_ttvn);
889 (*tt_data)->num_vlan = htons(num_vlan);
890
891 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(*tt_data + 1);
Marek Lindnerd0fa4f32015-06-22 00:30:22 +0800892 hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200893 tt_vlan->vid = htons(vlan->vid);
894 tt_vlan->crc = htonl(vlan->tt.crc);
895
896 tt_vlan++;
897 }
898
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200899 tt_change_ptr = (u8 *)*tt_data + change_offset;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200900 *tt_change = (struct batadv_tvlv_tt_change *)tt_change_ptr;
901
902out:
903 rcu_read_unlock();
904 return tvlv_len;
905}
906
907/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100908 * batadv_tt_prepare_tvlv_local_data() - allocate and prepare the TT TVLV for
909 * this node
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200910 * @bat_priv: the bat priv with all the soft interface information
911 * @tt_data: uninitialised pointer to the address of the TVLV buffer
912 * @tt_change: uninitialised pointer to the address of the area where the TT
913 * changes can be stored
914 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
915 * function reserves the amount of space needed to send the entire local TT
916 * table. In case of success the value is updated with the real amount of
917 * reserved bytes
918 *
919 * Allocate the needed amount of memory for the entire TT TVLV and write its
920 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
921 * objects, one per active VLAN.
922 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200923 * Return: the size of the allocated buffer or 0 in case of failure.
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200924 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200925static u16
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200926batadv_tt_prepare_tvlv_local_data(struct batadv_priv *bat_priv,
927 struct batadv_tvlv_tt_data **tt_data,
928 struct batadv_tvlv_tt_change **tt_change,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200929 s32 *tt_len)
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200930{
931 struct batadv_tvlv_tt_vlan_data *tt_vlan;
932 struct batadv_softif_vlan *vlan;
Sven Eckelmann4f248cf2015-06-09 20:50:49 +0200933 u16 num_vlan = 0;
934 u16 num_entries = 0;
935 u16 tvlv_len;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200936 u8 *tt_change_ptr;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200937 int change_offset;
938
939 rcu_read_lock();
940 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
941 num_vlan++;
942 num_entries += atomic_read(&vlan->tt.num_entries);
943 }
944
945 change_offset = sizeof(**tt_data);
946 change_offset += num_vlan * sizeof(*tt_vlan);
947
948 /* if tt_len is negative, allocate the space needed by the full table */
949 if (*tt_len < 0)
950 *tt_len = batadv_tt_len(num_entries);
951
952 tvlv_len = *tt_len;
953 tvlv_len += change_offset;
954
955 *tt_data = kmalloc(tvlv_len, GFP_ATOMIC);
956 if (!*tt_data) {
957 tvlv_len = 0;
958 goto out;
959 }
960
961 (*tt_data)->flags = BATADV_NO_FLAGS;
962 (*tt_data)->ttvn = atomic_read(&bat_priv->tt.vn);
963 (*tt_data)->num_vlan = htons(num_vlan);
964
965 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(*tt_data + 1);
966 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
967 tt_vlan->vid = htons(vlan->vid);
968 tt_vlan->crc = htonl(vlan->tt.crc);
969
970 tt_vlan++;
971 }
972
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200973 tt_change_ptr = (u8 *)*tt_data + change_offset;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200974 *tt_change = (struct batadv_tvlv_tt_change *)tt_change_ptr;
975
976out:
977 rcu_read_unlock();
978 return tvlv_len;
979}
980
981/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100982 * batadv_tt_tvlv_container_update() - update the translation table tvlv
983 * container after local tt changes have been committed
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800984 * @bat_priv: the bat priv with all the soft interface information
985 */
986static void batadv_tt_tvlv_container_update(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000987{
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800988 struct batadv_tt_change_node *entry, *safe;
989 struct batadv_tvlv_tt_data *tt_data;
990 struct batadv_tvlv_tt_change *tt_change;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200991 int tt_diff_len, tt_change_len = 0;
Sven Eckelmann4f248cf2015-06-09 20:50:49 +0200992 int tt_diff_entries_num = 0;
993 int tt_diff_entries_count = 0;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200994 u16 tvlv_len;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000995
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200996 tt_diff_entries_num = atomic_read(&bat_priv->tt.local_changes);
997 tt_diff_len = batadv_tt_len(tt_diff_entries_num);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800998
999 /* if we have too many changes for one packet don't send any
1000 * and wait for the tt table request which will be fragmented
1001 */
Marek Lindnere1bf0c12013-04-23 21:40:01 +08001002 if (tt_diff_len > bat_priv->soft_iface->mtu)
1003 tt_diff_len = 0;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08001004
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001005 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv, &tt_data,
1006 &tt_change, &tt_diff_len);
1007 if (!tvlv_len)
Marek Lindnere1bf0c12013-04-23 21:40:01 +08001008 return;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08001009
Marek Lindnere1bf0c12013-04-23 21:40:01 +08001010 tt_data->flags = BATADV_TT_OGM_DIFF;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08001011
Marek Lindnere1bf0c12013-04-23 21:40:01 +08001012 if (tt_diff_len == 0)
1013 goto container_register;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08001014
Sven Eckelmann807736f2012-07-15 22:26:51 +02001015 spin_lock_bh(&bat_priv->tt.changes_list_lock);
1016 atomic_set(&bat_priv->tt.local_changes, 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001017
Sven Eckelmann807736f2012-07-15 22:26:51 +02001018 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +01001019 list) {
Marek Lindnere1bf0c12013-04-23 21:40:01 +08001020 if (tt_diff_entries_count < tt_diff_entries_num) {
1021 memcpy(tt_change + tt_diff_entries_count,
1022 &entry->change,
1023 sizeof(struct batadv_tvlv_tt_change));
1024 tt_diff_entries_count++;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001025 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001026 list_del(&entry->list);
Sven Eckelmann86452f82016-06-25 16:44:06 +02001027 kmem_cache_free(batadv_tt_change_cache, entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001028 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02001029 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001030
Antonio Quartullia73105b2011-04-27 14:27:44 +02001031 /* Keep the buffer for possible tt_request */
Sven Eckelmann807736f2012-07-15 22:26:51 +02001032 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
1033 kfree(bat_priv->tt.last_changeset);
1034 bat_priv->tt.last_changeset_len = 0;
1035 bat_priv->tt.last_changeset = NULL;
Marek Lindnere1bf0c12013-04-23 21:40:01 +08001036 tt_change_len = batadv_tt_len(tt_diff_entries_count);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08001037 /* check whether this new OGM has no changes due to size problems */
Marek Lindnere1bf0c12013-04-23 21:40:01 +08001038 if (tt_diff_entries_count > 0) {
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08001039 /* if kmalloc() fails we will reply with the full table
Antonio Quartullia73105b2011-04-27 14:27:44 +02001040 * instead of providing the diff
1041 */
Marek Lindnere1bf0c12013-04-23 21:40:01 +08001042 bat_priv->tt.last_changeset = kzalloc(tt_diff_len, GFP_ATOMIC);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001043 if (bat_priv->tt.last_changeset) {
Marek Lindnere1bf0c12013-04-23 21:40:01 +08001044 memcpy(bat_priv->tt.last_changeset,
1045 tt_change, tt_change_len);
1046 bat_priv->tt.last_changeset_len = tt_diff_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001047 }
1048 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02001049 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001050
Marek Lindnere1bf0c12013-04-23 21:40:01 +08001051container_register:
1052 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_TT, 1, tt_data,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001053 tvlv_len);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08001054 kfree(tt_data);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001055}
1056
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +02001057#ifdef CONFIG_BATMAN_ADV_DEBUGFS
Sven Eckelmannff15c272017-12-02 19:51:53 +01001058
1059/**
1060 * batadv_tt_local_seq_print_text() - Print the local tt table in a seq file
1061 * @seq: seq file to print on
1062 * @offset: not used
1063 *
1064 * Return: always 0
1065 */
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001066int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001067{
1068 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001069 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001070 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001071 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartulli85766a82012-11-08 22:16:16 +01001072 struct batadv_tt_local_entry *tt_local;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001073 struct batadv_hard_iface *primary_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001074 struct hlist_head *head;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001075 u32 i;
Antonio Quartulli85766a82012-11-08 22:16:16 +01001076 int last_seen_secs;
1077 int last_seen_msecs;
1078 unsigned long last_seen_jiffies;
1079 bool no_purge;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001080 u16 np_flag = BATADV_TT_CLIENT_NOPURGE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001081
Marek Lindner30da63a2012-08-03 17:15:46 +02001082 primary_if = batadv_seq_print_text_primary_if_get(seq);
1083 if (!primary_if)
Marek Lindner32ae9b22011-04-20 15:40:58 +02001084 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001085
Sven Eckelmann86ceb362012-03-07 09:07:45 +01001086 seq_printf(seq,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001087 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u):\n",
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001088 net_dev->name, (u8)atomic_read(&bat_priv->tt.vn));
Antonio Quartulli925a6f32016-03-12 10:30:18 +01001089 seq_puts(seq,
1090 " Client VID Flags Last seen (CRC )\n");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001091
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001092 for (i = 0; i < hash->size; i++) {
1093 head = &hash->table[i];
1094
Marek Lindner7aadf882011-02-18 12:28:09 +00001095 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001096 hlist_for_each_entry_rcu(tt_common_entry,
Marek Lindner7aadf882011-02-18 12:28:09 +00001097 head, hash_entry) {
Antonio Quartulli85766a82012-11-08 22:16:16 +01001098 tt_local = container_of(tt_common_entry,
1099 struct batadv_tt_local_entry,
1100 common);
1101 last_seen_jiffies = jiffies - tt_local->last_seen;
1102 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
1103 last_seen_secs = last_seen_msecs / 1000;
1104 last_seen_msecs = last_seen_msecs % 1000;
1105
1106 no_purge = tt_common_entry->flags & np_flag;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001107 seq_printf(seq,
Antonio Quartullidd24ddb2013-11-16 12:03:49 +01001108 " * %pM %4i [%c%c%c%c%c%c] %3u.%03u (%#.8x)\n",
Sven Eckelmann7c64fd92012-02-28 10:55:36 +01001109 tt_common_entry->addr,
Sven Eckelmannf7a2bd62017-02-22 17:16:42 +01001110 batadv_print_vid(tt_common_entry->vid),
Sven Eckelmanna2f2b6c2015-04-23 18:22:24 +02001111 ((tt_common_entry->flags &
1112 BATADV_TT_CLIENT_ROAM) ? 'R' : '.'),
Antonio Quartulli85766a82012-11-08 22:16:16 +01001113 no_purge ? 'P' : '.',
Sven Eckelmanna2f2b6c2015-04-23 18:22:24 +02001114 ((tt_common_entry->flags &
1115 BATADV_TT_CLIENT_NEW) ? 'N' : '.'),
1116 ((tt_common_entry->flags &
1117 BATADV_TT_CLIENT_PENDING) ? 'X' : '.'),
1118 ((tt_common_entry->flags &
1119 BATADV_TT_CLIENT_WIFI) ? 'W' : '.'),
1120 ((tt_common_entry->flags &
1121 BATADV_TT_CLIENT_ISOLA) ? 'I' : '.'),
Antonio Quartullia7966d92013-01-24 11:41:39 +01001122 no_purge ? 0 : last_seen_secs,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001123 no_purge ? 0 : last_seen_msecs,
Sven Eckelmanna33d9702016-03-11 16:44:05 +01001124 tt_local->vlan->tt.crc);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001125 }
Marek Lindner7aadf882011-02-18 12:28:09 +00001126 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001127 }
Marek Lindner32ae9b22011-04-20 15:40:58 +02001128out:
1129 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +01001130 batadv_hardif_put(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +02001131 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001132}
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +02001133#endif
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001134
Matthias Schifferd34f0552016-07-03 13:31:37 +02001135/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001136 * batadv_tt_local_dump_entry() - Dump one TT local entry into a message
Matthias Schifferd34f0552016-07-03 13:31:37 +02001137 * @msg :Netlink message to dump into
1138 * @portid: Port making netlink request
1139 * @seq: Sequence number of netlink message
1140 * @bat_priv: The bat priv with all the soft interface information
1141 * @common: tt local & tt global common data
1142 *
1143 * Return: Error code, or 0 on success
1144 */
1145static int
1146batadv_tt_local_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
1147 struct batadv_priv *bat_priv,
1148 struct batadv_tt_common_entry *common)
1149{
1150 void *hdr;
1151 struct batadv_softif_vlan *vlan;
1152 struct batadv_tt_local_entry *local;
1153 unsigned int last_seen_msecs;
1154 u32 crc;
1155
1156 local = container_of(common, struct batadv_tt_local_entry, common);
1157 last_seen_msecs = jiffies_to_msecs(jiffies - local->last_seen);
1158
1159 vlan = batadv_softif_vlan_get(bat_priv, common->vid);
1160 if (!vlan)
1161 return 0;
1162
1163 crc = vlan->tt.crc;
1164
1165 batadv_softif_vlan_put(vlan);
1166
1167 hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
1168 NLM_F_MULTI,
1169 BATADV_CMD_GET_TRANSTABLE_LOCAL);
1170 if (!hdr)
1171 return -ENOBUFS;
1172
1173 if (nla_put(msg, BATADV_ATTR_TT_ADDRESS, ETH_ALEN, common->addr) ||
1174 nla_put_u32(msg, BATADV_ATTR_TT_CRC32, crc) ||
1175 nla_put_u16(msg, BATADV_ATTR_TT_VID, common->vid) ||
1176 nla_put_u32(msg, BATADV_ATTR_TT_FLAGS, common->flags))
1177 goto nla_put_failure;
1178
1179 if (!(common->flags & BATADV_TT_CLIENT_NOPURGE) &&
1180 nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS, last_seen_msecs))
1181 goto nla_put_failure;
1182
1183 genlmsg_end(msg, hdr);
1184 return 0;
1185
1186 nla_put_failure:
1187 genlmsg_cancel(msg, hdr);
1188 return -EMSGSIZE;
1189}
1190
1191/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001192 * batadv_tt_local_dump_bucket() - Dump one TT local bucket into a message
Matthias Schifferd34f0552016-07-03 13:31:37 +02001193 * @msg: Netlink message to dump into
1194 * @portid: Port making netlink request
1195 * @seq: Sequence number of netlink message
1196 * @bat_priv: The bat priv with all the soft interface information
1197 * @head: Pointer to the list containing the local tt entries
1198 * @idx_s: Number of entries to skip
1199 *
1200 * Return: Error code, or 0 on success
1201 */
1202static int
1203batadv_tt_local_dump_bucket(struct sk_buff *msg, u32 portid, u32 seq,
1204 struct batadv_priv *bat_priv,
1205 struct hlist_head *head, int *idx_s)
1206{
1207 struct batadv_tt_common_entry *common;
1208 int idx = 0;
1209
1210 rcu_read_lock();
1211 hlist_for_each_entry_rcu(common, head, hash_entry) {
1212 if (idx++ < *idx_s)
1213 continue;
1214
1215 if (batadv_tt_local_dump_entry(msg, portid, seq, bat_priv,
1216 common)) {
1217 rcu_read_unlock();
1218 *idx_s = idx - 1;
1219 return -EMSGSIZE;
1220 }
1221 }
1222 rcu_read_unlock();
1223
1224 *idx_s = 0;
1225 return 0;
1226}
1227
1228/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001229 * batadv_tt_local_dump() - Dump TT local entries into a message
Matthias Schifferd34f0552016-07-03 13:31:37 +02001230 * @msg: Netlink message to dump into
1231 * @cb: Parameters from query
1232 *
1233 * Return: Error code, or 0 on success
1234 */
1235int batadv_tt_local_dump(struct sk_buff *msg, struct netlink_callback *cb)
1236{
1237 struct net *net = sock_net(cb->skb->sk);
1238 struct net_device *soft_iface;
1239 struct batadv_priv *bat_priv;
1240 struct batadv_hard_iface *primary_if = NULL;
1241 struct batadv_hashtable *hash;
1242 struct hlist_head *head;
1243 int ret;
1244 int ifindex;
1245 int bucket = cb->args[0];
1246 int idx = cb->args[1];
1247 int portid = NETLINK_CB(cb->skb).portid;
1248
1249 ifindex = batadv_netlink_get_ifindex(cb->nlh, BATADV_ATTR_MESH_IFINDEX);
1250 if (!ifindex)
1251 return -EINVAL;
1252
1253 soft_iface = dev_get_by_index(net, ifindex);
1254 if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
1255 ret = -ENODEV;
1256 goto out;
1257 }
1258
1259 bat_priv = netdev_priv(soft_iface);
1260
1261 primary_if = batadv_primary_if_get_selected(bat_priv);
1262 if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
1263 ret = -ENOENT;
1264 goto out;
1265 }
1266
1267 hash = bat_priv->tt.local_hash;
1268
1269 while (bucket < hash->size) {
1270 head = &hash->table[bucket];
1271
1272 if (batadv_tt_local_dump_bucket(msg, portid, cb->nlh->nlmsg_seq,
1273 bat_priv, head, &idx))
1274 break;
1275
1276 bucket++;
1277 }
1278
1279 ret = msg->len;
1280
1281 out:
1282 if (primary_if)
1283 batadv_hardif_put(primary_if);
1284 if (soft_iface)
1285 dev_put(soft_iface);
1286
1287 cb->args[0] = bucket;
1288 cb->args[1] = idx;
1289
1290 return ret;
1291}
1292
Sven Eckelmann56303d32012-06-05 22:31:31 +02001293static void
1294batadv_tt_local_set_pending(struct batadv_priv *bat_priv,
1295 struct batadv_tt_local_entry *tt_local_entry,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001296 u16 flags, const char *message)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001297{
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +02001298 batadv_tt_local_event(bat_priv, tt_local_entry, flags);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001299
Antonio Quartulli015758d2011-07-09 17:52:13 +02001300 /* The local client has to be marked as "pending to be removed" but has
1301 * to be kept in the table in order to send it in a full table
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001302 * response issued before the net ttvn increment (consistency check)
1303 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001304 tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING;
Antonio Quartullic566dbb2012-01-06 21:31:34 +01001305
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001306 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02001307 "Local tt entry (%pM, vid: %d) pending to be removed: %s\n",
1308 tt_local_entry->common.addr,
Sven Eckelmannf7a2bd62017-02-22 17:16:42 +01001309 batadv_print_vid(tt_local_entry->common.vid), message);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001310}
1311
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001312/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001313 * batadv_tt_local_remove() - logically remove an entry from the local table
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001314 * @bat_priv: the bat priv with all the soft interface information
1315 * @addr: the MAC address of the client to remove
Antonio Quartullic018ad32013-06-04 12:11:39 +02001316 * @vid: VLAN identifier
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001317 * @message: message to append to the log on deletion
1318 * @roaming: true if the deletion is due to a roaming event
1319 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001320 * Return: the flags assigned to the local entry before being deleted
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001321 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001322u16 batadv_tt_local_remove(struct batadv_priv *bat_priv, const u8 *addr,
1323 unsigned short vid, const char *message,
1324 bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001325{
Sven Eckelmann170173b2012-10-07 12:02:22 +02001326 struct batadv_tt_local_entry *tt_local_entry;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001327 u16 flags, curr_flags = BATADV_NO_FLAGS;
Marek Lindneref727062015-06-17 20:01:36 +08001328 void *tt_entry_exists;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001329
Antonio Quartullic018ad32013-06-04 12:11:39 +02001330 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001331 if (!tt_local_entry)
1332 goto out;
1333
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001334 curr_flags = tt_local_entry->common.flags;
1335
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001336 flags = BATADV_TT_CLIENT_DEL;
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001337 /* if this global entry addition is due to a roaming, the node has to
1338 * mark the local entry as "roamed" in order to correctly reroute
1339 * packets later
1340 */
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02001341 if (roaming) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001342 flags |= BATADV_TT_CLIENT_ROAM;
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02001343 /* mark the local client as ROAMed */
1344 tt_local_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
1345 }
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001346
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001347 if (!(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW)) {
1348 batadv_tt_local_set_pending(bat_priv, tt_local_entry, flags,
1349 message);
1350 goto out;
1351 }
1352 /* if this client has been added right now, it is possible to
1353 * immediately purge it
1354 */
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +02001355 batadv_tt_local_event(bat_priv, tt_local_entry, BATADV_TT_CLIENT_DEL);
Marek Lindneref727062015-06-17 20:01:36 +08001356
1357 tt_entry_exists = batadv_hash_remove(bat_priv->tt.local_hash,
1358 batadv_compare_tt,
1359 batadv_choose_tt,
1360 &tt_local_entry->common);
1361 if (!tt_entry_exists)
1362 goto out;
1363
1364 /* extra call to free the local tt entry */
Sven Eckelmann95c0db92016-01-17 11:01:25 +01001365 batadv_tt_local_entry_put(tt_local_entry);
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001366
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001367out:
1368 if (tt_local_entry)
Sven Eckelmann95c0db92016-01-17 11:01:25 +01001369 batadv_tt_local_entry_put(tt_local_entry);
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001370
1371 return curr_flags;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001372}
1373
Marek Lindnera19d3d82013-05-27 15:33:25 +08001374/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001375 * batadv_tt_local_purge_list() - purge inactive tt local entries
Marek Lindnera19d3d82013-05-27 15:33:25 +08001376 * @bat_priv: the bat priv with all the soft interface information
1377 * @head: pointer to the list containing the local tt entries
1378 * @timeout: parameter deciding whether a given tt local entry is considered
1379 * inactive or not
1380 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001381static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
Marek Lindnera19d3d82013-05-27 15:33:25 +08001382 struct hlist_head *head,
1383 int timeout)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001384{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001385 struct batadv_tt_local_entry *tt_local_entry;
1386 struct batadv_tt_common_entry *tt_common_entry;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001387 struct hlist_node *node_tmp;
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001388
Sasha Levinb67bfe02013-02-27 17:06:00 -08001389 hlist_for_each_entry_safe(tt_common_entry, node_tmp, head,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001390 hash_entry) {
1391 tt_local_entry = container_of(tt_common_entry,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001392 struct batadv_tt_local_entry,
1393 common);
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001394 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_NOPURGE)
1395 continue;
1396
1397 /* entry already marked for deletion */
1398 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)
1399 continue;
1400
Marek Lindnera19d3d82013-05-27 15:33:25 +08001401 if (!batadv_has_timed_out(tt_local_entry->last_seen, timeout))
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001402 continue;
1403
1404 batadv_tt_local_set_pending(bat_priv, tt_local_entry,
1405 BATADV_TT_CLIENT_DEL, "timed out");
1406 }
1407}
1408
Marek Lindnera19d3d82013-05-27 15:33:25 +08001409/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001410 * batadv_tt_local_purge() - purge inactive tt local entries
Marek Lindnera19d3d82013-05-27 15:33:25 +08001411 * @bat_priv: the bat priv with all the soft interface information
1412 * @timeout: parameter deciding whether a given tt local entry is considered
1413 * inactive or not
1414 */
1415static void batadv_tt_local_purge(struct batadv_priv *bat_priv,
1416 int timeout)
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001417{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001418 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001419 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001420 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001421 u32 i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001422
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001423 for (i = 0; i < hash->size; i++) {
1424 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001425 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001426
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001427 spin_lock_bh(list_lock);
Marek Lindnera19d3d82013-05-27 15:33:25 +08001428 batadv_tt_local_purge_list(bat_priv, head, timeout);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001429 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001430 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001431}
1432
Sven Eckelmann56303d32012-06-05 22:31:31 +02001433static void batadv_tt_local_table_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001434{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001435 struct batadv_hashtable *hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001436 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001437 struct batadv_tt_common_entry *tt_common_entry;
1438 struct batadv_tt_local_entry *tt_local;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001439 struct hlist_node *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001440 struct hlist_head *head;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001441 u32 i;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001442
Sven Eckelmann807736f2012-07-15 22:26:51 +02001443 if (!bat_priv->tt.local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001444 return;
1445
Sven Eckelmann807736f2012-07-15 22:26:51 +02001446 hash = bat_priv->tt.local_hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001447
1448 for (i = 0; i < hash->size; i++) {
1449 head = &hash->table[i];
1450 list_lock = &hash->list_locks[i];
1451
1452 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001453 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001454 head, hash_entry) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001455 hlist_del_rcu(&tt_common_entry->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001456 tt_local = container_of(tt_common_entry,
1457 struct batadv_tt_local_entry,
1458 common);
Antonio Quartulli35df3b22014-05-08 17:13:15 +02001459
Sven Eckelmann95c0db92016-01-17 11:01:25 +01001460 batadv_tt_local_entry_put(tt_local);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001461 }
1462 spin_unlock_bh(list_lock);
1463 }
1464
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +02001465 batadv_hash_destroy(hash);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001466
Sven Eckelmann807736f2012-07-15 22:26:51 +02001467 bat_priv->tt.local_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001468}
1469
Sven Eckelmann56303d32012-06-05 22:31:31 +02001470static int batadv_tt_global_init(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001471{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001472 if (bat_priv->tt.global_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +02001473 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001474
Sven Eckelmann807736f2012-07-15 22:26:51 +02001475 bat_priv->tt.global_hash = batadv_hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001476
Sven Eckelmann807736f2012-07-15 22:26:51 +02001477 if (!bat_priv->tt.global_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +02001478 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001479
Antonio Quartullidec05072012-11-10 11:00:32 +01001480 batadv_hash_set_lock_class(bat_priv->tt.global_hash,
1481 &batadv_tt_global_hash_lock_class_key);
1482
Sven Eckelmann5346c352012-05-05 13:27:28 +02001483 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001484}
1485
Sven Eckelmann56303d32012-06-05 22:31:31 +02001486static void batadv_tt_changes_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001487{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001488 struct batadv_tt_change_node *entry, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001489
Sven Eckelmann807736f2012-07-15 22:26:51 +02001490 spin_lock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001491
Sven Eckelmann807736f2012-07-15 22:26:51 +02001492 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001493 list) {
1494 list_del(&entry->list);
Sven Eckelmann86452f82016-06-25 16:44:06 +02001495 kmem_cache_free(batadv_tt_change_cache, entry);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001496 }
1497
Sven Eckelmann807736f2012-07-15 22:26:51 +02001498 atomic_set(&bat_priv->tt.local_changes, 0);
1499 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001500}
1501
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001502/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001503 * batadv_tt_global_orig_entry_find() - find a TT orig_list_entry
Antonio Quartullid15cd622015-11-17 16:40:52 +08001504 * @entry: the TT global entry where the orig_list_entry has to be
1505 * extracted from
1506 * @orig_node: the originator for which the orig_list_entry has to be found
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001507 *
Antonio Quartullid15cd622015-11-17 16:40:52 +08001508 * retrieve the orig_tt_list_entry belonging to orig_node from the
Antonio Quartullid657e622012-07-01 14:09:12 +02001509 * batadv_tt_global_entry list
1510 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001511 * Return: it with an increased refcounter, NULL if not found
Antonio Quartullid657e622012-07-01 14:09:12 +02001512 */
1513static struct batadv_tt_orig_list_entry *
1514batadv_tt_global_orig_entry_find(const struct batadv_tt_global_entry *entry,
1515 const struct batadv_orig_node *orig_node)
1516{
1517 struct batadv_tt_orig_list_entry *tmp_orig_entry, *orig_entry = NULL;
1518 const struct hlist_head *head;
Antonio Quartullid657e622012-07-01 14:09:12 +02001519
1520 rcu_read_lock();
1521 head = &entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001522 hlist_for_each_entry_rcu(tmp_orig_entry, head, list) {
Antonio Quartullid657e622012-07-01 14:09:12 +02001523 if (tmp_orig_entry->orig_node != orig_node)
1524 continue;
Sven Eckelmann6e8ef692016-01-16 10:29:50 +01001525 if (!kref_get_unless_zero(&tmp_orig_entry->refcount))
Antonio Quartullid657e622012-07-01 14:09:12 +02001526 continue;
1527
1528 orig_entry = tmp_orig_entry;
1529 break;
1530 }
1531 rcu_read_unlock();
1532
1533 return orig_entry;
1534}
1535
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001536/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001537 * batadv_tt_global_entry_has_orig() - check if a TT global entry is also
1538 * handled by a given originator
Antonio Quartullid15cd622015-11-17 16:40:52 +08001539 * @entry: the TT global entry to check
1540 * @orig_node: the originator to search in the list
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001541 *
1542 * find out if an orig_node is already in the list of a tt_global_entry.
1543 *
1544 * Return: true if found, false otherwise
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001545 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001546static bool
1547batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry *entry,
1548 const struct batadv_orig_node *orig_node)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001549{
Antonio Quartullid657e622012-07-01 14:09:12 +02001550 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001551 bool found = false;
1552
Antonio Quartullid657e622012-07-01 14:09:12 +02001553 orig_entry = batadv_tt_global_orig_entry_find(entry, orig_node);
1554 if (orig_entry) {
1555 found = true;
Sven Eckelmann7e2366c2016-01-17 11:01:27 +01001556 batadv_tt_orig_list_entry_put(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001557 }
Antonio Quartullid657e622012-07-01 14:09:12 +02001558
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001559 return found;
1560}
1561
Linus Lüssing54e22f22017-07-06 07:02:25 +02001562/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001563 * batadv_tt_global_sync_flags() - update TT sync flags
Linus Lüssing54e22f22017-07-06 07:02:25 +02001564 * @tt_global: the TT global entry to update sync flags in
1565 *
1566 * Updates the sync flag bits in the tt_global flag attribute with a logical
1567 * OR of all sync flags from any of its TT orig entries.
1568 */
1569static void
1570batadv_tt_global_sync_flags(struct batadv_tt_global_entry *tt_global)
1571{
1572 struct batadv_tt_orig_list_entry *orig_entry;
1573 const struct hlist_head *head;
1574 u16 flags = BATADV_NO_FLAGS;
1575
1576 rcu_read_lock();
1577 head = &tt_global->orig_list;
1578 hlist_for_each_entry_rcu(orig_entry, head, list)
1579 flags |= orig_entry->flags;
1580 rcu_read_unlock();
1581
1582 flags |= tt_global->common.flags & (~BATADV_TT_SYNC_MASK);
1583 tt_global->common.flags = flags;
1584}
1585
1586/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001587 * batadv_tt_global_orig_entry_add() - add or update a TT orig entry
Linus Lüssing54e22f22017-07-06 07:02:25 +02001588 * @tt_global: the TT global entry to add an orig entry in
1589 * @orig_node: the originator to add an orig entry for
1590 * @ttvn: translation table version number of this changeset
1591 * @flags: TT sync flags
1592 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001593static void
Antonio Quartullid657e622012-07-01 14:09:12 +02001594batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry *tt_global,
Linus Lüssing54e22f22017-07-06 07:02:25 +02001595 struct batadv_orig_node *orig_node, int ttvn,
1596 u8 flags)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001597{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001598 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001599
Antonio Quartullid657e622012-07-01 14:09:12 +02001600 orig_entry = batadv_tt_global_orig_entry_find(tt_global, orig_node);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001601 if (orig_entry) {
1602 /* refresh the ttvn: the current value could be a bogus one that
1603 * was added during a "temporary client detection"
1604 */
1605 orig_entry->ttvn = ttvn;
Linus Lüssing54e22f22017-07-06 07:02:25 +02001606 orig_entry->flags = flags;
1607 goto sync_flags;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001608 }
Antonio Quartullid657e622012-07-01 14:09:12 +02001609
Sven Eckelmann86452f82016-06-25 16:44:06 +02001610 orig_entry = kmem_cache_zalloc(batadv_tt_orig_cache, GFP_ATOMIC);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001611 if (!orig_entry)
Antonio Quartullid657e622012-07-01 14:09:12 +02001612 goto out;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001613
1614 INIT_HLIST_NODE(&orig_entry->list);
Sven Eckelmann7c124392016-01-16 10:29:56 +01001615 kref_get(&orig_node->refcount);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001616 batadv_tt_global_size_inc(orig_node, tt_global->common.vid);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001617 orig_entry->orig_node = orig_node;
1618 orig_entry->ttvn = ttvn;
Linus Lüssing54e22f22017-07-06 07:02:25 +02001619 orig_entry->flags = flags;
Sven Eckelmann6e8ef692016-01-16 10:29:50 +01001620 kref_init(&orig_entry->refcount);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001621
Antonio Quartullid657e622012-07-01 14:09:12 +02001622 spin_lock_bh(&tt_global->list_lock);
Sven Eckelmann23f55482016-07-15 17:39:18 +02001623 kref_get(&orig_entry->refcount);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001624 hlist_add_head_rcu(&orig_entry->list,
Antonio Quartullid657e622012-07-01 14:09:12 +02001625 &tt_global->orig_list);
1626 spin_unlock_bh(&tt_global->list_lock);
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001627 atomic_inc(&tt_global->orig_list_count);
1628
Linus Lüssing54e22f22017-07-06 07:02:25 +02001629sync_flags:
1630 batadv_tt_global_sync_flags(tt_global);
Antonio Quartullid657e622012-07-01 14:09:12 +02001631out:
1632 if (orig_entry)
Sven Eckelmann7e2366c2016-01-17 11:01:27 +01001633 batadv_tt_orig_list_entry_put(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001634}
1635
Antonio Quartullid4ff40f2013-04-18 15:13:01 +02001636/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001637 * batadv_tt_global_add() - add a new TT global entry or update an existing one
Antonio Quartullid4ff40f2013-04-18 15:13:01 +02001638 * @bat_priv: the bat priv with all the soft interface information
1639 * @orig_node: the originator announcing the client
1640 * @tt_addr: the mac address of the non-mesh client
Antonio Quartullic018ad32013-06-04 12:11:39 +02001641 * @vid: VLAN identifier
Antonio Quartullid4ff40f2013-04-18 15:13:01 +02001642 * @flags: TT flags that have to be set for this non-mesh client
1643 * @ttvn: the tt version number ever announcing this non-mesh client
1644 *
1645 * Add a new TT global entry for the given originator. If the entry already
1646 * exists add a new reference to the given originator (a global entry can have
1647 * references to multiple originators) and adjust the flags attribute to reflect
1648 * the function argument.
1649 * If a TT local entry exists for this non-mesh client remove it.
1650 *
1651 * The caller must hold orig_node refcount.
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +02001652 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001653 * Return: true if the new entry has been added, false otherwise
Antonio Quartullid4ff40f2013-04-18 15:13:01 +02001654 */
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +02001655static bool batadv_tt_global_add(struct batadv_priv *bat_priv,
1656 struct batadv_orig_node *orig_node,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001657 const unsigned char *tt_addr,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001658 unsigned short vid, u16 flags, u8 ttvn)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001659{
Sven Eckelmann170173b2012-10-07 12:02:22 +02001660 struct batadv_tt_global_entry *tt_global_entry;
1661 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +02001662 bool ret = false;
Simon Wunderlich80b3f582011-11-02 20:26:45 +01001663 int hash_added;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001664 struct batadv_tt_common_entry *common;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001665 u16 local_flags;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001666
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001667 /* ignore global entries from backbone nodes */
1668 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig, vid))
1669 return true;
1670
Antonio Quartullic018ad32013-06-04 12:11:39 +02001671 tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr, vid);
1672 tt_local_entry = batadv_tt_local_hash_find(bat_priv, tt_addr, vid);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001673
1674 /* if the node already has a local client for this entry, it has to wait
1675 * for a roaming advertisement instead of manually messing up the global
1676 * table
1677 */
1678 if ((flags & BATADV_TT_CLIENT_TEMP) && tt_local_entry &&
1679 !(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW))
1680 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001681
Antonio Quartullia73105b2011-04-27 14:27:44 +02001682 if (!tt_global_entry) {
Sven Eckelmann86452f82016-06-25 16:44:06 +02001683 tt_global_entry = kmem_cache_zalloc(batadv_tg_cache,
1684 GFP_ATOMIC);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001685 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001686 goto out;
1687
Sven Eckelmannc0a55922012-05-12 13:48:55 +02001688 common = &tt_global_entry->common;
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01001689 ether_addr_copy(common->addr, tt_addr);
Antonio Quartullic018ad32013-06-04 12:11:39 +02001690 common->vid = vid;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001691
Antonio Quartullid4f44692012-05-25 00:00:54 +02001692 common->flags = flags;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001693 tt_global_entry->roam_at = 0;
Antonio Quartullifdf79322012-08-24 17:54:07 +02001694 /* node must store current time in case of roaming. This is
1695 * needed to purge this entry out on timeout (if nobody claims
1696 * it)
1697 */
1698 if (flags & BATADV_TT_CLIENT_ROAM)
1699 tt_global_entry->roam_at = jiffies;
Sven Eckelmann92dcdf02016-01-16 10:29:57 +01001700 kref_init(&common->refcount);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001701 common->added_at = jiffies;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001702
1703 INIT_HLIST_HEAD(&tt_global_entry->orig_list);
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001704 atomic_set(&tt_global_entry->orig_list_count, 0);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001705 spin_lock_init(&tt_global_entry->list_lock);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001706
Sven Eckelmann15d5ffd2016-07-15 17:39:23 +02001707 kref_get(&common->refcount);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001708 hash_added = batadv_hash_add(bat_priv->tt.global_hash,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001709 batadv_compare_tt,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001710 batadv_choose_tt, common,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001711 &common->hash_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +01001712
1713 if (unlikely(hash_added != 0)) {
1714 /* remove the reference for the hash */
Sven Eckelmann5dafd8a2016-01-17 11:01:26 +01001715 batadv_tt_global_entry_put(tt_global_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +01001716 goto out_remove;
1717 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001718 } else {
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001719 common = &tt_global_entry->common;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001720 /* If there is already a global entry, we can use this one for
1721 * our processing.
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001722 * But if we are trying to add a temporary client then here are
1723 * two options at this point:
1724 * 1) the global client is not a temporary client: the global
1725 * client has to be left as it is, temporary information
1726 * should never override any already known client state
1727 * 2) the global client is a temporary client: purge the
1728 * originator list and add the new one orig_entry
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001729 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001730 if (flags & BATADV_TT_CLIENT_TEMP) {
1731 if (!(common->flags & BATADV_TT_CLIENT_TEMP))
1732 goto out;
1733 if (batadv_tt_global_entry_has_orig(tt_global_entry,
1734 orig_node))
1735 goto out_remove;
1736 batadv_tt_global_del_orig_list(tt_global_entry);
1737 goto add_orig_entry;
1738 }
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001739
1740 /* if the client was temporary added before receiving the first
Simon Wunderlicha6cb3902015-09-02 20:09:55 +02001741 * OGM announcing it, we have to clear the TEMP flag. Also,
1742 * remove the previous temporary orig node and re-add it
1743 * if required. If the orig entry changed, the new one which
1744 * is a non-temporary entry is preferred.
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001745 */
Simon Wunderlicha6cb3902015-09-02 20:09:55 +02001746 if (common->flags & BATADV_TT_CLIENT_TEMP) {
1747 batadv_tt_global_del_orig_list(tt_global_entry);
1748 common->flags &= ~BATADV_TT_CLIENT_TEMP;
1749 }
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001750
Antonio Quartullie9c001362012-11-07 15:05:33 +01001751 /* the change can carry possible "attribute" flags like the
Linus Lüssing54e22f22017-07-06 07:02:25 +02001752 * TT_CLIENT_TEMP, therefore they have to be copied in the
Antonio Quartullie9c001362012-11-07 15:05:33 +01001753 * client entry
1754 */
Linus Lüssing54e22f22017-07-06 07:02:25 +02001755 common->flags |= flags & (~BATADV_TT_SYNC_MASK);
Antonio Quartullie9c001362012-11-07 15:05:33 +01001756
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001757 /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
1758 * one originator left in the list and we previously received a
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001759 * delete + roaming change for this originator.
1760 *
1761 * We should first delete the old originator before adding the
1762 * new one.
1763 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001764 if (common->flags & BATADV_TT_CLIENT_ROAM) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001765 batadv_tt_global_del_orig_list(tt_global_entry);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001766 common->flags &= ~BATADV_TT_CLIENT_ROAM;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001767 tt_global_entry->roam_at = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001768 }
1769 }
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001770add_orig_entry:
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001771 /* add the new orig_entry (if needed) or update it */
Linus Lüssing54e22f22017-07-06 07:02:25 +02001772 batadv_tt_global_orig_entry_add(tt_global_entry, orig_node, ttvn,
1773 flags & BATADV_TT_SYNC_MASK);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001774
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001775 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02001776 "Creating new global tt entry: %pM (vid: %d, via %pM)\n",
Sven Eckelmannf7a2bd62017-02-22 17:16:42 +01001777 common->addr, batadv_print_vid(common->vid),
Antonio Quartulli16052782013-06-04 12:11:41 +02001778 orig_node->orig);
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +02001779 ret = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001780
Simon Wunderlich80b3f582011-11-02 20:26:45 +01001781out_remove:
Linus Lüssingc5caf4e2014-02-15 17:47:49 +01001782 /* Do not remove multicast addresses from the local hash on
1783 * global additions
1784 */
1785 if (is_multicast_ether_addr(tt_addr))
1786 goto out;
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001787
Antonio Quartullia73105b2011-04-27 14:27:44 +02001788 /* remove address from local hash if present */
Antonio Quartullic018ad32013-06-04 12:11:39 +02001789 local_flags = batadv_tt_local_remove(bat_priv, tt_addr, vid,
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001790 "global tt received",
Antonio Quartullic1d07432013-01-15 22:17:19 +10001791 flags & BATADV_TT_CLIENT_ROAM);
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001792 tt_global_entry->common.flags |= local_flags & BATADV_TT_CLIENT_WIFI;
1793
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001794 if (!(flags & BATADV_TT_CLIENT_ROAM))
1795 /* this is a normal global add. Therefore the client is not in a
1796 * roaming state anymore.
1797 */
1798 tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_ROAM;
1799
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001800out:
1801 if (tt_global_entry)
Sven Eckelmann5dafd8a2016-01-17 11:01:26 +01001802 batadv_tt_global_entry_put(tt_global_entry);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001803 if (tt_local_entry)
Sven Eckelmann95c0db92016-01-17 11:01:25 +01001804 batadv_tt_local_entry_put(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001805 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001806}
1807
Simon Wunderlich1b371d12014-01-15 21:17:54 +01001808/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001809 * batadv_transtable_best_orig() - Get best originator list entry from tt entry
Antonio Quartulli46274562013-09-03 11:10:24 +02001810 * @bat_priv: the bat priv with all the soft interface information
Sven Eckelmann981d8902012-10-07 13:34:15 +02001811 * @tt_global_entry: global translation table entry to be analyzed
1812 *
1813 * This functon assumes the caller holds rcu_read_lock().
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001814 * Return: best originator list entry or NULL on errors.
Sven Eckelmann981d8902012-10-07 13:34:15 +02001815 */
1816static struct batadv_tt_orig_list_entry *
Antonio Quartulli46274562013-09-03 11:10:24 +02001817batadv_transtable_best_orig(struct batadv_priv *bat_priv,
1818 struct batadv_tt_global_entry *tt_global_entry)
Sven Eckelmann981d8902012-10-07 13:34:15 +02001819{
Antonio Quartulli46274562013-09-03 11:10:24 +02001820 struct batadv_neigh_node *router, *best_router = NULL;
Antonio Quartulli29824a52016-05-25 23:27:31 +08001821 struct batadv_algo_ops *bao = bat_priv->algo_ops;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001822 struct hlist_head *head;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001823 struct batadv_tt_orig_list_entry *orig_entry, *best_entry = NULL;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001824
1825 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001826 hlist_for_each_entry_rcu(orig_entry, head, list) {
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001827 router = batadv_orig_router_get(orig_entry->orig_node,
1828 BATADV_IF_DEFAULT);
Sven Eckelmann981d8902012-10-07 13:34:15 +02001829 if (!router)
1830 continue;
1831
Antonio Quartulli46274562013-09-03 11:10:24 +02001832 if (best_router &&
Antonio Quartulli29824a52016-05-25 23:27:31 +08001833 bao->neigh.cmp(router, BATADV_IF_DEFAULT, best_router,
1834 BATADV_IF_DEFAULT) <= 0) {
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001835 batadv_neigh_node_put(router);
Antonio Quartulli46274562013-09-03 11:10:24 +02001836 continue;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001837 }
1838
Antonio Quartulli46274562013-09-03 11:10:24 +02001839 /* release the refcount for the "old" best */
1840 if (best_router)
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001841 batadv_neigh_node_put(best_router);
Antonio Quartulli46274562013-09-03 11:10:24 +02001842
1843 best_entry = orig_entry;
1844 best_router = router;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001845 }
1846
Antonio Quartulli46274562013-09-03 11:10:24 +02001847 if (best_router)
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001848 batadv_neigh_node_put(best_router);
Antonio Quartulli46274562013-09-03 11:10:24 +02001849
Sven Eckelmann981d8902012-10-07 13:34:15 +02001850 return best_entry;
1851}
1852
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +02001853#ifdef CONFIG_BATMAN_ADV_DEBUGFS
Simon Wunderlich1b371d12014-01-15 21:17:54 +01001854/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001855 * batadv_tt_global_print_entry() - print all orig nodes who announce the
1856 * address for this global entry
Antonio Quartulli46274562013-09-03 11:10:24 +02001857 * @bat_priv: the bat priv with all the soft interface information
Sven Eckelmann981d8902012-10-07 13:34:15 +02001858 * @tt_global_entry: global translation table entry to be printed
1859 * @seq: debugfs table seq_file struct
1860 *
1861 * This functon assumes the caller holds rcu_read_lock().
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001862 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001863static void
Antonio Quartulli46274562013-09-03 11:10:24 +02001864batadv_tt_global_print_entry(struct batadv_priv *bat_priv,
1865 struct batadv_tt_global_entry *tt_global_entry,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001866 struct seq_file *seq)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001867{
Sven Eckelmann981d8902012-10-07 13:34:15 +02001868 struct batadv_tt_orig_list_entry *orig_entry, *best_entry;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001869 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001870 struct batadv_orig_node_vlan *vlan;
1871 struct hlist_head *head;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001872 u8 last_ttvn;
1873 u16 flags;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001874
1875 tt_common_entry = &tt_global_entry->common;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001876 flags = tt_common_entry->flags;
1877
Antonio Quartulli46274562013-09-03 11:10:24 +02001878 best_entry = batadv_transtable_best_orig(bat_priv, tt_global_entry);
Sven Eckelmann981d8902012-10-07 13:34:15 +02001879 if (best_entry) {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001880 vlan = batadv_orig_node_vlan_get(best_entry->orig_node,
1881 tt_common_entry->vid);
1882 if (!vlan) {
1883 seq_printf(seq,
1884 " * Cannot retrieve VLAN %d for originator %pM\n",
Sven Eckelmannf7a2bd62017-02-22 17:16:42 +01001885 batadv_print_vid(tt_common_entry->vid),
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001886 best_entry->orig_node->orig);
1887 goto print_list;
1888 }
1889
Sven Eckelmann981d8902012-10-07 13:34:15 +02001890 last_ttvn = atomic_read(&best_entry->orig_node->last_ttvn);
Antonio Quartullif9d8a532012-11-19 09:01:42 +01001891 seq_printf(seq,
Antonio Quartullidd24ddb2013-11-16 12:03:49 +01001892 " %c %pM %4i (%3u) via %pM (%3u) (%#.8x) [%c%c%c%c]\n",
Sven Eckelmann981d8902012-10-07 13:34:15 +02001893 '*', tt_global_entry->common.addr,
Sven Eckelmannf7a2bd62017-02-22 17:16:42 +01001894 batadv_print_vid(tt_global_entry->common.vid),
Sven Eckelmann981d8902012-10-07 13:34:15 +02001895 best_entry->ttvn, best_entry->orig_node->orig,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001896 last_ttvn, vlan->tt.crc,
Sven Eckelmanna2f2b6c2015-04-23 18:22:24 +02001897 ((flags & BATADV_TT_CLIENT_ROAM) ? 'R' : '.'),
1898 ((flags & BATADV_TT_CLIENT_WIFI) ? 'W' : '.'),
1899 ((flags & BATADV_TT_CLIENT_ISOLA) ? 'I' : '.'),
1900 ((flags & BATADV_TT_CLIENT_TEMP) ? 'T' : '.'));
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001901
Sven Eckelmann21754e22016-01-17 11:01:24 +01001902 batadv_orig_node_vlan_put(vlan);
Sven Eckelmann981d8902012-10-07 13:34:15 +02001903 }
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001904
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001905print_list:
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001906 head = &tt_global_entry->orig_list;
1907
Sasha Levinb67bfe02013-02-27 17:06:00 -08001908 hlist_for_each_entry_rcu(orig_entry, head, list) {
Sven Eckelmann981d8902012-10-07 13:34:15 +02001909 if (best_entry == orig_entry)
1910 continue;
1911
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001912 vlan = batadv_orig_node_vlan_get(orig_entry->orig_node,
1913 tt_common_entry->vid);
1914 if (!vlan) {
1915 seq_printf(seq,
1916 " + Cannot retrieve VLAN %d for originator %pM\n",
Sven Eckelmannf7a2bd62017-02-22 17:16:42 +01001917 batadv_print_vid(tt_common_entry->vid),
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001918 orig_entry->orig_node->orig);
1919 continue;
1920 }
1921
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001922 last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
Antonio Quartulli16052782013-06-04 12:11:41 +02001923 seq_printf(seq,
Antonio Quartullidd24ddb2013-11-16 12:03:49 +01001924 " %c %pM %4d (%3u) via %pM (%3u) (%#.8x) [%c%c%c%c]\n",
Sven Eckelmann981d8902012-10-07 13:34:15 +02001925 '+', tt_global_entry->common.addr,
Sven Eckelmannf7a2bd62017-02-22 17:16:42 +01001926 batadv_print_vid(tt_global_entry->common.vid),
Sven Eckelmann981d8902012-10-07 13:34:15 +02001927 orig_entry->ttvn, orig_entry->orig_node->orig,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001928 last_ttvn, vlan->tt.crc,
Sven Eckelmanna2f2b6c2015-04-23 18:22:24 +02001929 ((flags & BATADV_TT_CLIENT_ROAM) ? 'R' : '.'),
1930 ((flags & BATADV_TT_CLIENT_WIFI) ? 'W' : '.'),
1931 ((flags & BATADV_TT_CLIENT_ISOLA) ? 'I' : '.'),
1932 ((flags & BATADV_TT_CLIENT_TEMP) ? 'T' : '.'));
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001933
Sven Eckelmann21754e22016-01-17 11:01:24 +01001934 batadv_orig_node_vlan_put(vlan);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001935 }
1936}
1937
Sven Eckelmannff15c272017-12-02 19:51:53 +01001938/**
1939 * batadv_tt_global_seq_print_text() - Print the global tt table in a seq file
1940 * @seq: seq file to print on
1941 * @offset: not used
1942 *
1943 * Return: always 0
1944 */
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001945int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001946{
1947 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001948 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001949 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001950 struct batadv_tt_common_entry *tt_common_entry;
1951 struct batadv_tt_global_entry *tt_global;
1952 struct batadv_hard_iface *primary_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001953 struct hlist_head *head;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001954 u32 i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001955
Marek Lindner30da63a2012-08-03 17:15:46 +02001956 primary_if = batadv_seq_print_text_primary_if_get(seq);
1957 if (!primary_if)
Marek Lindner32ae9b22011-04-20 15:40:58 +02001958 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001959
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001960 seq_printf(seq,
1961 "Globally announced TT entries received via the mesh %s\n",
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001962 net_dev->name);
Antonio Quartulli925a6f32016-03-12 10:30:18 +01001963 seq_puts(seq,
1964 " Client VID (TTVN) Originator (Curr TTVN) (CRC ) Flags\n");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001965
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001966 for (i = 0; i < hash->size; i++) {
1967 head = &hash->table[i];
1968
Marek Lindner7aadf882011-02-18 12:28:09 +00001969 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001970 hlist_for_each_entry_rcu(tt_common_entry,
Marek Lindner7aadf882011-02-18 12:28:09 +00001971 head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001972 tt_global = container_of(tt_common_entry,
1973 struct batadv_tt_global_entry,
1974 common);
Antonio Quartulli46274562013-09-03 11:10:24 +02001975 batadv_tt_global_print_entry(bat_priv, tt_global, seq);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001976 }
Marek Lindner7aadf882011-02-18 12:28:09 +00001977 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001978 }
Marek Lindner32ae9b22011-04-20 15:40:58 +02001979out:
1980 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +01001981 batadv_hardif_put(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +02001982 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001983}
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +02001984#endif
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001985
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001986/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001987 * batadv_tt_global_dump_subentry() - Dump all TT local entries into a message
Matthias Schifferd34f0552016-07-03 13:31:37 +02001988 * @msg: Netlink message to dump into
1989 * @portid: Port making netlink request
1990 * @seq: Sequence number of netlink message
1991 * @common: tt local & tt global common data
1992 * @orig: Originator node announcing a non-mesh client
1993 * @best: Is the best originator for the TT entry
1994 *
1995 * Return: Error code, or 0 on success
1996 */
1997static int
1998batadv_tt_global_dump_subentry(struct sk_buff *msg, u32 portid, u32 seq,
1999 struct batadv_tt_common_entry *common,
2000 struct batadv_tt_orig_list_entry *orig,
2001 bool best)
2002{
Linus Lüssing54e22f22017-07-06 07:02:25 +02002003 u16 flags = (common->flags & (~BATADV_TT_SYNC_MASK)) | orig->flags;
Matthias Schifferd34f0552016-07-03 13:31:37 +02002004 void *hdr;
2005 struct batadv_orig_node_vlan *vlan;
2006 u8 last_ttvn;
2007 u32 crc;
2008
2009 vlan = batadv_orig_node_vlan_get(orig->orig_node,
2010 common->vid);
2011 if (!vlan)
2012 return 0;
2013
2014 crc = vlan->tt.crc;
2015
2016 batadv_orig_node_vlan_put(vlan);
2017
2018 hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
2019 NLM_F_MULTI,
2020 BATADV_CMD_GET_TRANSTABLE_GLOBAL);
2021 if (!hdr)
2022 return -ENOBUFS;
2023
2024 last_ttvn = atomic_read(&orig->orig_node->last_ttvn);
2025
2026 if (nla_put(msg, BATADV_ATTR_TT_ADDRESS, ETH_ALEN, common->addr) ||
2027 nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN,
2028 orig->orig_node->orig) ||
2029 nla_put_u8(msg, BATADV_ATTR_TT_TTVN, orig->ttvn) ||
2030 nla_put_u8(msg, BATADV_ATTR_TT_LAST_TTVN, last_ttvn) ||
2031 nla_put_u32(msg, BATADV_ATTR_TT_CRC32, crc) ||
2032 nla_put_u16(msg, BATADV_ATTR_TT_VID, common->vid) ||
Linus Lüssing54e22f22017-07-06 07:02:25 +02002033 nla_put_u32(msg, BATADV_ATTR_TT_FLAGS, flags))
Matthias Schifferd34f0552016-07-03 13:31:37 +02002034 goto nla_put_failure;
2035
2036 if (best && nla_put_flag(msg, BATADV_ATTR_FLAG_BEST))
2037 goto nla_put_failure;
2038
2039 genlmsg_end(msg, hdr);
2040 return 0;
2041
2042 nla_put_failure:
2043 genlmsg_cancel(msg, hdr);
2044 return -EMSGSIZE;
2045}
2046
2047/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01002048 * batadv_tt_global_dump_entry() - Dump one TT global entry into a message
Matthias Schifferd34f0552016-07-03 13:31:37 +02002049 * @msg: Netlink message to dump into
2050 * @portid: Port making netlink request
2051 * @seq: Sequence number of netlink message
2052 * @bat_priv: The bat priv with all the soft interface information
2053 * @common: tt local & tt global common data
2054 * @sub_s: Number of entries to skip
2055 *
2056 * This function assumes the caller holds rcu_read_lock().
2057 *
2058 * Return: Error code, or 0 on success
2059 */
2060static int
2061batadv_tt_global_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
2062 struct batadv_priv *bat_priv,
2063 struct batadv_tt_common_entry *common, int *sub_s)
2064{
2065 struct batadv_tt_orig_list_entry *orig_entry, *best_entry;
2066 struct batadv_tt_global_entry *global;
2067 struct hlist_head *head;
2068 int sub = 0;
2069 bool best;
2070
2071 global = container_of(common, struct batadv_tt_global_entry, common);
2072 best_entry = batadv_transtable_best_orig(bat_priv, global);
2073 head = &global->orig_list;
2074
2075 hlist_for_each_entry_rcu(orig_entry, head, list) {
2076 if (sub++ < *sub_s)
2077 continue;
2078
2079 best = (orig_entry == best_entry);
2080
2081 if (batadv_tt_global_dump_subentry(msg, portid, seq, common,
2082 orig_entry, best)) {
2083 *sub_s = sub - 1;
2084 return -EMSGSIZE;
2085 }
2086 }
2087
2088 *sub_s = 0;
2089 return 0;
2090}
2091
2092/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01002093 * batadv_tt_global_dump_bucket() - Dump one TT local bucket into a message
Matthias Schifferd34f0552016-07-03 13:31:37 +02002094 * @msg: Netlink message to dump into
2095 * @portid: Port making netlink request
2096 * @seq: Sequence number of netlink message
2097 * @bat_priv: The bat priv with all the soft interface information
2098 * @head: Pointer to the list containing the global tt entries
2099 * @idx_s: Number of entries to skip
2100 * @sub: Number of entries to skip
2101 *
2102 * Return: Error code, or 0 on success
2103 */
2104static int
2105batadv_tt_global_dump_bucket(struct sk_buff *msg, u32 portid, u32 seq,
2106 struct batadv_priv *bat_priv,
2107 struct hlist_head *head, int *idx_s, int *sub)
2108{
2109 struct batadv_tt_common_entry *common;
2110 int idx = 0;
2111
2112 rcu_read_lock();
2113 hlist_for_each_entry_rcu(common, head, hash_entry) {
2114 if (idx++ < *idx_s)
2115 continue;
2116
2117 if (batadv_tt_global_dump_entry(msg, portid, seq, bat_priv,
2118 common, sub)) {
2119 rcu_read_unlock();
2120 *idx_s = idx - 1;
2121 return -EMSGSIZE;
2122 }
2123 }
2124 rcu_read_unlock();
2125
2126 *idx_s = 0;
2127 *sub = 0;
2128 return 0;
2129}
2130
2131/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01002132 * batadv_tt_global_dump() - Dump TT global entries into a message
Matthias Schifferd34f0552016-07-03 13:31:37 +02002133 * @msg: Netlink message to dump into
2134 * @cb: Parameters from query
2135 *
2136 * Return: Error code, or length of message on success
2137 */
2138int batadv_tt_global_dump(struct sk_buff *msg, struct netlink_callback *cb)
2139{
2140 struct net *net = sock_net(cb->skb->sk);
2141 struct net_device *soft_iface;
2142 struct batadv_priv *bat_priv;
2143 struct batadv_hard_iface *primary_if = NULL;
2144 struct batadv_hashtable *hash;
2145 struct hlist_head *head;
2146 int ret;
2147 int ifindex;
2148 int bucket = cb->args[0];
2149 int idx = cb->args[1];
2150 int sub = cb->args[2];
2151 int portid = NETLINK_CB(cb->skb).portid;
2152
2153 ifindex = batadv_netlink_get_ifindex(cb->nlh, BATADV_ATTR_MESH_IFINDEX);
2154 if (!ifindex)
2155 return -EINVAL;
2156
2157 soft_iface = dev_get_by_index(net, ifindex);
2158 if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
2159 ret = -ENODEV;
2160 goto out;
2161 }
2162
2163 bat_priv = netdev_priv(soft_iface);
2164
2165 primary_if = batadv_primary_if_get_selected(bat_priv);
2166 if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
2167 ret = -ENOENT;
2168 goto out;
2169 }
2170
2171 hash = bat_priv->tt.global_hash;
2172
2173 while (bucket < hash->size) {
2174 head = &hash->table[bucket];
2175
2176 if (batadv_tt_global_dump_bucket(msg, portid,
2177 cb->nlh->nlmsg_seq, bat_priv,
2178 head, &idx, &sub))
2179 break;
2180
2181 bucket++;
2182 }
2183
2184 ret = msg->len;
2185
2186 out:
2187 if (primary_if)
2188 batadv_hardif_put(primary_if);
2189 if (soft_iface)
2190 dev_put(soft_iface);
2191
2192 cb->args[0] = bucket;
2193 cb->args[1] = idx;
2194 cb->args[2] = sub;
2195
2196 return ret;
2197}
2198
2199/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01002200 * _batadv_tt_global_del_orig_entry() - remove and free an orig_entry
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01002201 * @tt_global_entry: the global entry to remove the orig_entry from
2202 * @orig_entry: the orig entry to remove and free
2203 *
2204 * Remove an orig_entry from its list in the given tt_global_entry and
2205 * free this orig_entry afterwards.
Marek Lindner433ff98f2015-06-18 16:11:07 +08002206 *
2207 * Caller must hold tt_global_entry->list_lock and ensure orig_entry->list is
2208 * part of a list.
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01002209 */
2210static void
Marek Lindner433ff98f2015-06-18 16:11:07 +08002211_batadv_tt_global_del_orig_entry(struct batadv_tt_global_entry *tt_global_entry,
2212 struct batadv_tt_orig_list_entry *orig_entry)
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01002213{
Sven Eckelmann2c72d652015-06-21 14:45:14 +02002214 lockdep_assert_held(&tt_global_entry->list_lock);
2215
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01002216 batadv_tt_global_size_dec(orig_entry->orig_node,
2217 tt_global_entry->common.vid);
2218 atomic_dec(&tt_global_entry->orig_list_count);
Marek Lindner433ff98f2015-06-18 16:11:07 +08002219 /* requires holding tt_global_entry->list_lock and orig_entry->list
2220 * being part of a list
2221 */
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01002222 hlist_del_rcu(&orig_entry->list);
Sven Eckelmann7e2366c2016-01-17 11:01:27 +01002223 batadv_tt_orig_list_entry_put(orig_entry);
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01002224}
2225
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002226/* deletes the orig list of a tt_global_entry */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002227static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02002228batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002229{
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002230 struct hlist_head *head;
Sasha Levinb67bfe02013-02-27 17:06:00 -08002231 struct hlist_node *safe;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002232 struct batadv_tt_orig_list_entry *orig_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002233
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002234 spin_lock_bh(&tt_global_entry->list_lock);
2235 head = &tt_global_entry->orig_list;
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01002236 hlist_for_each_entry_safe(orig_entry, safe, head, list)
Marek Lindner433ff98f2015-06-18 16:11:07 +08002237 _batadv_tt_global_del_orig_entry(tt_global_entry, orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002238 spin_unlock_bh(&tt_global_entry->list_lock);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002239}
2240
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01002241/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01002242 * batadv_tt_global_del_orig_node() - remove orig_node from a global tt entry
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01002243 * @bat_priv: the bat priv with all the soft interface information
2244 * @tt_global_entry: the global entry to remove the orig_node from
2245 * @orig_node: the originator announcing the client
2246 * @message: message to append to the log on deletion
2247 *
2248 * Remove the given orig_node and its according orig_entry from the given
2249 * global tt entry.
2250 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002251static void
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01002252batadv_tt_global_del_orig_node(struct batadv_priv *bat_priv,
2253 struct batadv_tt_global_entry *tt_global_entry,
2254 struct batadv_orig_node *orig_node,
2255 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002256{
2257 struct hlist_head *head;
Sasha Levinb67bfe02013-02-27 17:06:00 -08002258 struct hlist_node *safe;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002259 struct batadv_tt_orig_list_entry *orig_entry;
Antonio Quartulli16052782013-06-04 12:11:41 +02002260 unsigned short vid;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002261
2262 spin_lock_bh(&tt_global_entry->list_lock);
2263 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08002264 hlist_for_each_entry_safe(orig_entry, safe, head, list) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002265 if (orig_entry->orig_node == orig_node) {
Antonio Quartulli16052782013-06-04 12:11:41 +02002266 vid = tt_global_entry->common.vid;
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002267 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02002268 "Deleting %pM from global tt entry %pM (vid: %d): %s\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002269 orig_node->orig,
Antonio Quartulli16052782013-06-04 12:11:41 +02002270 tt_global_entry->common.addr,
Sven Eckelmannf7a2bd62017-02-22 17:16:42 +01002271 batadv_print_vid(vid), message);
Marek Lindner433ff98f2015-06-18 16:11:07 +08002272 _batadv_tt_global_del_orig_entry(tt_global_entry,
2273 orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002274 }
2275 }
2276 spin_unlock_bh(&tt_global_entry->list_lock);
2277}
2278
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002279/* If the client is to be deleted, we check if it is the last origantor entry
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002280 * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
2281 * timer, otherwise we simply remove the originator scheduled for deletion.
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002282 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002283static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02002284batadv_tt_global_del_roaming(struct batadv_priv *bat_priv,
2285 struct batadv_tt_global_entry *tt_global_entry,
2286 struct batadv_orig_node *orig_node,
2287 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002288{
2289 bool last_entry = true;
2290 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002291 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002292
2293 /* no local entry exists, case 1:
2294 * Check if this is the last one or if other entries exist.
2295 */
2296
2297 rcu_read_lock();
2298 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08002299 hlist_for_each_entry_rcu(orig_entry, head, list) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002300 if (orig_entry->orig_node != orig_node) {
2301 last_entry = false;
2302 break;
2303 }
2304 }
2305 rcu_read_unlock();
2306
2307 if (last_entry) {
2308 /* its the last one, mark for roaming. */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002309 tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002310 tt_global_entry->roam_at = jiffies;
Sven Eckelmann1fda4c02017-02-22 17:16:40 +01002311 } else {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002312 /* there is another entry, we can simply delete this
2313 * one and can still use the other one.
2314 */
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01002315 batadv_tt_global_del_orig_node(bat_priv, tt_global_entry,
2316 orig_node, message);
Sven Eckelmann1fda4c02017-02-22 17:16:40 +01002317 }
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002318}
2319
Antonio Quartullic018ad32013-06-04 12:11:39 +02002320/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01002321 * batadv_tt_global_del() - remove a client from the global table
Antonio Quartullic018ad32013-06-04 12:11:39 +02002322 * @bat_priv: the bat priv with all the soft interface information
2323 * @orig_node: an originator serving this client
2324 * @addr: the mac address of the client
2325 * @vid: VLAN identifier
2326 * @message: a message explaining the reason for deleting the client to print
2327 * for debugging purpose
2328 * @roaming: true if the deletion has been triggered by a roaming event
2329 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002330static void batadv_tt_global_del(struct batadv_priv *bat_priv,
2331 struct batadv_orig_node *orig_node,
Antonio Quartullic018ad32013-06-04 12:11:39 +02002332 const unsigned char *addr, unsigned short vid,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002333 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002334{
Sven Eckelmann170173b2012-10-07 12:02:22 +02002335 struct batadv_tt_global_entry *tt_global_entry;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002336 struct batadv_tt_local_entry *local_entry = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002337
Antonio Quartullic018ad32013-06-04 12:11:39 +02002338 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002339 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002340 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002341
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002342 if (!roaming) {
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01002343 batadv_tt_global_del_orig_node(bat_priv, tt_global_entry,
2344 orig_node, message);
Sven Eckelmann92f90f52011-12-22 20:31:12 +08002345
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002346 if (hlist_empty(&tt_global_entry->orig_list))
Antonio Quartullibe73b482012-09-23 22:38:36 +02002347 batadv_tt_global_free(bat_priv, tt_global_entry,
2348 message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002349
Sven Eckelmann92f90f52011-12-22 20:31:12 +08002350 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002351 }
Sven Eckelmann92f90f52011-12-22 20:31:12 +08002352
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002353 /* if we are deleting a global entry due to a roam
2354 * event, there are two possibilities:
2355 * 1) the client roamed from node A to node B => if there
2356 * is only one originator left for this client, we mark
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002357 * it with BATADV_TT_CLIENT_ROAM, we start a timer and we
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002358 * wait for node B to claim it. In case of timeout
2359 * the entry is purged.
2360 *
2361 * If there are other originators left, we directly delete
2362 * the originator.
2363 * 2) the client roamed to us => we can directly delete
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002364 * the global entry, since it is useless now.
2365 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002366 local_entry = batadv_tt_local_hash_find(bat_priv,
Antonio Quartullic018ad32013-06-04 12:11:39 +02002367 tt_global_entry->common.addr,
2368 vid);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002369 if (local_entry) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002370 /* local entry exists, case 2: client roamed to us. */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002371 batadv_tt_global_del_orig_list(tt_global_entry);
Antonio Quartullibe73b482012-09-23 22:38:36 +02002372 batadv_tt_global_free(bat_priv, tt_global_entry, message);
Sven Eckelmann1fda4c02017-02-22 17:16:40 +01002373 } else {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002374 /* no local entry exists, case 1: check for roaming */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002375 batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
2376 orig_node, message);
Sven Eckelmann1fda4c02017-02-22 17:16:40 +01002377 }
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002378
Antonio Quartullicc47f662011-04-27 14:27:57 +02002379out:
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002380 if (tt_global_entry)
Sven Eckelmann5dafd8a2016-01-17 11:01:26 +01002381 batadv_tt_global_entry_put(tt_global_entry);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002382 if (local_entry)
Sven Eckelmann95c0db92016-01-17 11:01:25 +01002383 batadv_tt_local_entry_put(local_entry);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002384}
2385
Antonio Quartulli95fb1302013-08-07 18:28:55 +02002386/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01002387 * batadv_tt_global_del_orig() - remove all the TT global entries belonging to
2388 * the given originator matching the provided vid
Antonio Quartulli95fb1302013-08-07 18:28:55 +02002389 * @bat_priv: the bat priv with all the soft interface information
2390 * @orig_node: the originator owning the entries to remove
2391 * @match_vid: the VLAN identifier to match. If negative all the entries will be
2392 * removed
2393 * @message: debug message to print as "reason"
2394 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002395void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
2396 struct batadv_orig_node *orig_node,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002397 s32 match_vid,
Sven Eckelmann56303d32012-06-05 22:31:31 +02002398 const char *message)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002399{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002400 struct batadv_tt_global_entry *tt_global;
2401 struct batadv_tt_common_entry *tt_common_entry;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002402 u32 i;
Sven Eckelmann807736f2012-07-15 22:26:51 +02002403 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08002404 struct hlist_node *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002405 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002406 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartulli16052782013-06-04 12:11:41 +02002407 unsigned short vid;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002408
Simon Wunderlich6e801492011-10-19 10:28:26 +02002409 if (!hash)
2410 return;
2411
Antonio Quartullia73105b2011-04-27 14:27:44 +02002412 for (i = 0; i < hash->size; i++) {
2413 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002414 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002415
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002416 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08002417 hlist_for_each_entry_safe(tt_common_entry, safe,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +01002418 head, hash_entry) {
Antonio Quartulli95fb1302013-08-07 18:28:55 +02002419 /* remove only matching entries */
2420 if (match_vid >= 0 && tt_common_entry->vid != match_vid)
2421 continue;
2422
Sven Eckelmann56303d32012-06-05 22:31:31 +02002423 tt_global = container_of(tt_common_entry,
2424 struct batadv_tt_global_entry,
2425 common);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002426
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01002427 batadv_tt_global_del_orig_node(bat_priv, tt_global,
2428 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002429
Sven Eckelmann56303d32012-06-05 22:31:31 +02002430 if (hlist_empty(&tt_global->orig_list)) {
Antonio Quartulli16052782013-06-04 12:11:41 +02002431 vid = tt_global->common.vid;
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002432 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02002433 "Deleting global tt entry %pM (vid: %d): %s\n",
2434 tt_global->common.addr,
Sven Eckelmannf7a2bd62017-02-22 17:16:42 +01002435 batadv_print_vid(vid), message);
Sasha Levinb67bfe02013-02-27 17:06:00 -08002436 hlist_del_rcu(&tt_common_entry->hash_entry);
Sven Eckelmann5dafd8a2016-01-17 11:01:26 +01002437 batadv_tt_global_entry_put(tt_global);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002438 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002439 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002440 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002441 }
Linus Lüssingac4eebd2015-06-16 17:10:24 +02002442 clear_bit(BATADV_ORIG_CAPA_HAS_TT, &orig_node->capa_initialized);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002443}
2444
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002445static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry *tt_global,
2446 char **msg)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002447{
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002448 bool purge = false;
2449 unsigned long roam_timeout = BATADV_TT_CLIENT_ROAM_TIMEOUT;
2450 unsigned long temp_timeout = BATADV_TT_CLIENT_TEMP_TIMEOUT;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002451
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002452 if ((tt_global->common.flags & BATADV_TT_CLIENT_ROAM) &&
2453 batadv_has_timed_out(tt_global->roam_at, roam_timeout)) {
2454 purge = true;
2455 *msg = "Roaming timeout\n";
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002456 }
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002457
2458 if ((tt_global->common.flags & BATADV_TT_CLIENT_TEMP) &&
2459 batadv_has_timed_out(tt_global->common.added_at, temp_timeout)) {
2460 purge = true;
2461 *msg = "Temporary client timeout\n";
2462 }
2463
2464 return purge;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002465}
2466
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002467static void batadv_tt_global_purge(struct batadv_priv *bat_priv)
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002468{
Sven Eckelmann807736f2012-07-15 22:26:51 +02002469 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002470 struct hlist_head *head;
Sasha Levinb67bfe02013-02-27 17:06:00 -08002471 struct hlist_node *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002472 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002473 u32 i;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002474 char *msg = NULL;
2475 struct batadv_tt_common_entry *tt_common;
2476 struct batadv_tt_global_entry *tt_global;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002477
Antonio Quartullicc47f662011-04-27 14:27:57 +02002478 for (i = 0; i < hash->size; i++) {
2479 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002480 list_lock = &hash->list_locks[i];
Antonio Quartullicc47f662011-04-27 14:27:57 +02002481
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002482 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08002483 hlist_for_each_entry_safe(tt_common, node_tmp, head,
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002484 hash_entry) {
2485 tt_global = container_of(tt_common,
2486 struct batadv_tt_global_entry,
2487 common);
2488
2489 if (!batadv_tt_global_to_purge(tt_global, &msg))
2490 continue;
2491
2492 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02002493 "Deleting global tt entry %pM (vid: %d): %s\n",
2494 tt_global->common.addr,
Sven Eckelmannf7a2bd62017-02-22 17:16:42 +01002495 batadv_print_vid(tt_global->common.vid),
Antonio Quartulli16052782013-06-04 12:11:41 +02002496 msg);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002497
Sasha Levinb67bfe02013-02-27 17:06:00 -08002498 hlist_del_rcu(&tt_common->hash_entry);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002499
Sven Eckelmann5dafd8a2016-01-17 11:01:26 +01002500 batadv_tt_global_entry_put(tt_global);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002501 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002502 spin_unlock_bh(list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002503 }
Antonio Quartullicc47f662011-04-27 14:27:57 +02002504}
2505
Sven Eckelmann56303d32012-06-05 22:31:31 +02002506static void batadv_tt_global_table_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002507{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02002508 struct batadv_hashtable *hash;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002509 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002510 struct batadv_tt_common_entry *tt_common_entry;
2511 struct batadv_tt_global_entry *tt_global;
Sasha Levinb67bfe02013-02-27 17:06:00 -08002512 struct hlist_node *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002513 struct hlist_head *head;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002514 u32 i;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002515
Sven Eckelmann807736f2012-07-15 22:26:51 +02002516 if (!bat_priv->tt.global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002517 return;
2518
Sven Eckelmann807736f2012-07-15 22:26:51 +02002519 hash = bat_priv->tt.global_hash;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002520
2521 for (i = 0; i < hash->size; i++) {
2522 head = &hash->table[i];
2523 list_lock = &hash->list_locks[i];
2524
2525 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08002526 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002527 head, hash_entry) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08002528 hlist_del_rcu(&tt_common_entry->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02002529 tt_global = container_of(tt_common_entry,
2530 struct batadv_tt_global_entry,
2531 common);
Sven Eckelmann5dafd8a2016-01-17 11:01:26 +01002532 batadv_tt_global_entry_put(tt_global);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002533 }
2534 spin_unlock_bh(list_lock);
2535 }
2536
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +02002537 batadv_hash_destroy(hash);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002538
Sven Eckelmann807736f2012-07-15 22:26:51 +02002539 bat_priv->tt.global_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002540}
2541
Sven Eckelmann56303d32012-06-05 22:31:31 +02002542static bool
2543_batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
2544 struct batadv_tt_global_entry *tt_global_entry)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002545{
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002546 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_WIFI &&
2547 tt_global_entry->common.flags & BATADV_TT_CLIENT_WIFI)
Simon Wunderlich75ae84a2017-06-01 17:11:26 +02002548 return true;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002549
Antonio Quartulli2d2fcc2a2013-11-16 12:03:50 +01002550 /* check if the two clients are marked as isolated */
2551 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_ISOLA &&
2552 tt_global_entry->common.flags & BATADV_TT_CLIENT_ISOLA)
Simon Wunderlich75ae84a2017-06-01 17:11:26 +02002553 return true;
Antonio Quartulli2d2fcc2a2013-11-16 12:03:50 +01002554
Simon Wunderlich75ae84a2017-06-01 17:11:26 +02002555 return false;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002556}
2557
Antonio Quartullic018ad32013-06-04 12:11:39 +02002558/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01002559 * batadv_transtable_search() - get the mesh destination for a given client
Antonio Quartullic018ad32013-06-04 12:11:39 +02002560 * @bat_priv: the bat priv with all the soft interface information
2561 * @src: mac address of the source client
2562 * @addr: mac address of the destination client
2563 * @vid: VLAN identifier
2564 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02002565 * Return: a pointer to the originator that was selected as destination in the
Antonio Quartullic018ad32013-06-04 12:11:39 +02002566 * mesh for contacting the client 'addr', NULL otherwise.
2567 * In case of multiple originators serving the same client, the function returns
2568 * the best one (best in terms of metric towards the destination node).
2569 *
2570 * If the two clients are AP isolated the function returns NULL.
2571 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002572struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002573 const u8 *src,
2574 const u8 *addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +02002575 unsigned short vid)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002576{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002577 struct batadv_tt_local_entry *tt_local_entry = NULL;
2578 struct batadv_tt_global_entry *tt_global_entry = NULL;
2579 struct batadv_orig_node *orig_node = NULL;
Sven Eckelmann981d8902012-10-07 13:34:15 +02002580 struct batadv_tt_orig_list_entry *best_entry;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002581
Antonio Quartullieceb22a2013-11-16 12:03:51 +01002582 if (src && batadv_vlan_ap_isola_get(bat_priv, vid)) {
Antonio Quartullic018ad32013-06-04 12:11:39 +02002583 tt_local_entry = batadv_tt_local_hash_find(bat_priv, src, vid);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02002584 if (!tt_local_entry ||
2585 (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING))
Antonio Quartulli3d393e42011-07-07 15:35:37 +02002586 goto out;
2587 }
Marek Lindner7aadf882011-02-18 12:28:09 +00002588
Antonio Quartullic018ad32013-06-04 12:11:39 +02002589 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
Antonio Quartulli2dafb492011-05-05 08:42:45 +02002590 if (!tt_global_entry)
Marek Lindner7b36e8e2011-02-18 12:28:10 +00002591 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002592
Antonio Quartulli3d393e42011-07-07 15:35:37 +02002593 /* check whether the clients should not communicate due to AP
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002594 * isolation
2595 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002596 if (tt_local_entry &&
2597 _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli3d393e42011-07-07 15:35:37 +02002598 goto out;
2599
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002600 rcu_read_lock();
Antonio Quartulli46274562013-09-03 11:10:24 +02002601 best_entry = batadv_transtable_best_orig(bat_priv, tt_global_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002602 /* found anything? */
Sven Eckelmann981d8902012-10-07 13:34:15 +02002603 if (best_entry)
2604 orig_node = best_entry->orig_node;
Sven Eckelmann7c124392016-01-16 10:29:56 +01002605 if (orig_node && !kref_get_unless_zero(&orig_node->refcount))
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002606 orig_node = NULL;
2607 rcu_read_unlock();
Sven Eckelmann981d8902012-10-07 13:34:15 +02002608
Marek Lindner7b36e8e2011-02-18 12:28:10 +00002609out:
Antonio Quartulli3d393e42011-07-07 15:35:37 +02002610 if (tt_global_entry)
Sven Eckelmann5dafd8a2016-01-17 11:01:26 +01002611 batadv_tt_global_entry_put(tt_global_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02002612 if (tt_local_entry)
Sven Eckelmann95c0db92016-01-17 11:01:25 +01002613 batadv_tt_local_entry_put(tt_local_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02002614
Marek Lindner7b36e8e2011-02-18 12:28:10 +00002615 return orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002616}
Antonio Quartullia73105b2011-04-27 14:27:44 +02002617
Antonio Quartulliced72932013-04-24 16:37:51 +02002618/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01002619 * batadv_tt_global_crc() - calculates the checksum of the local table belonging
Antonio Quartulliced72932013-04-24 16:37:51 +02002620 * to the given orig_node
2621 * @bat_priv: the bat priv with all the soft interface information
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02002622 * @orig_node: originator for which the CRC should be computed
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002623 * @vid: VLAN identifier for which the CRC32 has to be computed
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02002624 *
2625 * This function computes the checksum for the global table corresponding to a
2626 * specific originator. In particular, the checksum is computed as follows: For
2627 * each client connected to the originator the CRC32C of the MAC address and the
2628 * VID is computed and then all the CRC32Cs of the various clients are xor'ed
2629 * together.
2630 *
2631 * The idea behind is that CRC32C should be used as much as possible in order to
2632 * produce a unique hash of the table, but since the order which is used to feed
2633 * the CRC32C function affects the result and since every node in the network
2634 * probably sorts the clients differently, the hash function cannot be directly
2635 * computed over the entire table. Hence the CRC32C is used only on
2636 * the single client entry, while all the results are then xor'ed together
2637 * because the XOR operation can combine them all while trying to reduce the
2638 * noise as much as possible.
2639 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02002640 * Return: the checksum of the global table of a given originator.
Antonio Quartulliced72932013-04-24 16:37:51 +02002641 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002642static u32 batadv_tt_global_crc(struct batadv_priv *bat_priv,
2643 struct batadv_orig_node *orig_node,
2644 unsigned short vid)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002645{
Sven Eckelmann807736f2012-07-15 22:26:51 +02002646 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Linus Lüssing54e22f22017-07-06 07:02:25 +02002647 struct batadv_tt_orig_list_entry *tt_orig;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002648 struct batadv_tt_common_entry *tt_common;
2649 struct batadv_tt_global_entry *tt_global;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002650 struct hlist_head *head;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002651 u32 i, crc_tmp, crc = 0;
2652 u8 flags;
Antonio Quartullia30e22c2014-02-11 17:05:06 +01002653 __be16 tmp_vid;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002654
2655 for (i = 0; i < hash->size; i++) {
2656 head = &hash->table[i];
2657
2658 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08002659 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02002660 tt_global = container_of(tt_common,
2661 struct batadv_tt_global_entry,
2662 common);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002663 /* compute the CRC only for entries belonging to the
2664 * VLAN identified by the vid passed as parameter
2665 */
2666 if (tt_common->vid != vid)
2667 continue;
2668
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002669 /* Roaming clients are in the global table for
2670 * consistency only. They don't have to be
2671 * taken into account while computing the
2672 * global crc
2673 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002674 if (tt_common->flags & BATADV_TT_CLIENT_ROAM)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002675 continue;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002676 /* Temporary clients have not been announced yet, so
2677 * they have to be skipped while computing the global
2678 * crc
2679 */
2680 if (tt_common->flags & BATADV_TT_CLIENT_TEMP)
2681 continue;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002682
2683 /* find out if this global entry is announced by this
2684 * originator
2685 */
Linus Lüssing54e22f22017-07-06 07:02:25 +02002686 tt_orig = batadv_tt_global_orig_entry_find(tt_global,
2687 orig_node);
2688 if (!tt_orig)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002689 continue;
2690
Antonio Quartullia30e22c2014-02-11 17:05:06 +01002691 /* use network order to read the VID: this ensures that
2692 * every node reads the bytes in the same order.
2693 */
2694 tmp_vid = htons(tt_common->vid);
2695 crc_tmp = crc32c(0, &tmp_vid, sizeof(tmp_vid));
Antonio Quartulli0eb015682013-10-13 02:50:20 +02002696
2697 /* compute the CRC on flags that have to be kept in sync
2698 * among nodes
2699 */
Linus Lüssing54e22f22017-07-06 07:02:25 +02002700 flags = tt_orig->flags;
Antonio Quartulli0eb015682013-10-13 02:50:20 +02002701 crc_tmp = crc32c(crc_tmp, &flags, sizeof(flags));
2702
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02002703 crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
Linus Lüssing54e22f22017-07-06 07:02:25 +02002704
2705 batadv_tt_orig_list_entry_put(tt_orig);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002706 }
2707 rcu_read_unlock();
2708 }
2709
Antonio Quartulliced72932013-04-24 16:37:51 +02002710 return crc;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002711}
2712
Antonio Quartulliced72932013-04-24 16:37:51 +02002713/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01002714 * batadv_tt_local_crc() - calculates the checksum of the local table
Antonio Quartulliced72932013-04-24 16:37:51 +02002715 * @bat_priv: the bat priv with all the soft interface information
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002716 * @vid: VLAN identifier for which the CRC32 has to be computed
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02002717 *
2718 * For details about the computation, please refer to the documentation for
2719 * batadv_tt_global_crc().
2720 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02002721 * Return: the checksum of the local table
Antonio Quartulliced72932013-04-24 16:37:51 +02002722 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002723static u32 batadv_tt_local_crc(struct batadv_priv *bat_priv,
2724 unsigned short vid)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002725{
Sven Eckelmann807736f2012-07-15 22:26:51 +02002726 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002727 struct batadv_tt_common_entry *tt_common;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002728 struct hlist_head *head;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002729 u32 i, crc_tmp, crc = 0;
2730 u8 flags;
Antonio Quartullia30e22c2014-02-11 17:05:06 +01002731 __be16 tmp_vid;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002732
2733 for (i = 0; i < hash->size; i++) {
2734 head = &hash->table[i];
2735
2736 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08002737 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002738 /* compute the CRC only for entries belonging to the
2739 * VLAN identified by vid
2740 */
2741 if (tt_common->vid != vid)
2742 continue;
2743
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002744 /* not yet committed clients have not to be taken into
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002745 * account while computing the CRC
2746 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002747 if (tt_common->flags & BATADV_TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002748 continue;
Antonio Quartulliced72932013-04-24 16:37:51 +02002749
Antonio Quartullia30e22c2014-02-11 17:05:06 +01002750 /* use network order to read the VID: this ensures that
2751 * every node reads the bytes in the same order.
2752 */
2753 tmp_vid = htons(tt_common->vid);
2754 crc_tmp = crc32c(0, &tmp_vid, sizeof(tmp_vid));
Antonio Quartulli0eb015682013-10-13 02:50:20 +02002755
2756 /* compute the CRC on flags that have to be kept in sync
2757 * among nodes
2758 */
2759 flags = tt_common->flags & BATADV_TT_SYNC_MASK;
2760 crc_tmp = crc32c(crc_tmp, &flags, sizeof(flags));
2761
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02002762 crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002763 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002764 rcu_read_unlock();
2765 }
2766
Antonio Quartulliced72932013-04-24 16:37:51 +02002767 return crc;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002768}
2769
Sven Eckelmann9c4604a2016-06-26 11:16:10 +02002770/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01002771 * batadv_tt_req_node_release() - free tt_req node entry
Sven Eckelmann9c4604a2016-06-26 11:16:10 +02002772 * @ref: kref pointer of the tt req_node entry
2773 */
2774static void batadv_tt_req_node_release(struct kref *ref)
2775{
2776 struct batadv_tt_req_node *tt_req_node;
2777
2778 tt_req_node = container_of(ref, struct batadv_tt_req_node, refcount);
2779
Sven Eckelmann86452f82016-06-25 16:44:06 +02002780 kmem_cache_free(batadv_tt_req_cache, tt_req_node);
Sven Eckelmann9c4604a2016-06-26 11:16:10 +02002781}
2782
2783/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01002784 * batadv_tt_req_node_put() - decrement the tt_req_node refcounter and
Sven Eckelmann9c4604a2016-06-26 11:16:10 +02002785 * possibly release it
2786 * @tt_req_node: tt_req_node to be free'd
2787 */
2788static void batadv_tt_req_node_put(struct batadv_tt_req_node *tt_req_node)
2789{
2790 kref_put(&tt_req_node->refcount, batadv_tt_req_node_release);
2791}
2792
Sven Eckelmann56303d32012-06-05 22:31:31 +02002793static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002794{
Marek Lindner7c26a532015-06-28 22:16:06 +08002795 struct batadv_tt_req_node *node;
2796 struct hlist_node *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002797
Sven Eckelmann807736f2012-07-15 22:26:51 +02002798 spin_lock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002799
Marek Lindner7c26a532015-06-28 22:16:06 +08002800 hlist_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
2801 hlist_del_init(&node->list);
Sven Eckelmann9c4604a2016-06-26 11:16:10 +02002802 batadv_tt_req_node_put(node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002803 }
2804
Sven Eckelmann807736f2012-07-15 22:26:51 +02002805 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002806}
2807
Sven Eckelmann56303d32012-06-05 22:31:31 +02002808static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv,
2809 struct batadv_orig_node *orig_node,
Antonio Quartullie8cf2342013-05-28 13:14:28 +02002810 const void *tt_buff,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002811 u16 tt_buff_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002812{
Antonio Quartullia73105b2011-04-27 14:27:44 +02002813 /* Replace the old buffer only if I received something in the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002814 * last OGM (the OGM could carry no changes)
2815 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02002816 spin_lock_bh(&orig_node->tt_buff_lock);
2817 if (tt_buff_len > 0) {
2818 kfree(orig_node->tt_buff);
2819 orig_node->tt_buff_len = 0;
2820 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
2821 if (orig_node->tt_buff) {
2822 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
2823 orig_node->tt_buff_len = tt_buff_len;
2824 }
2825 }
2826 spin_unlock_bh(&orig_node->tt_buff_lock);
2827}
2828
Sven Eckelmann56303d32012-06-05 22:31:31 +02002829static void batadv_tt_req_purge(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002830{
Marek Lindner7c26a532015-06-28 22:16:06 +08002831 struct batadv_tt_req_node *node;
2832 struct hlist_node *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002833
Sven Eckelmann807736f2012-07-15 22:26:51 +02002834 spin_lock_bh(&bat_priv->tt.req_list_lock);
Marek Lindner7c26a532015-06-28 22:16:06 +08002835 hlist_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002836 if (batadv_has_timed_out(node->issued_at,
2837 BATADV_TT_REQUEST_TIMEOUT)) {
Marek Lindner7c26a532015-06-28 22:16:06 +08002838 hlist_del_init(&node->list);
Sven Eckelmann9c4604a2016-06-26 11:16:10 +02002839 batadv_tt_req_node_put(node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002840 }
2841 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02002842 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002843}
2844
Marek Lindner383b8632015-06-18 16:24:24 +08002845/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01002846 * batadv_tt_req_node_new() - search and possibly create a tt_req_node object
Marek Lindner383b8632015-06-18 16:24:24 +08002847 * @bat_priv: the bat priv with all the soft interface information
2848 * @orig_node: orig node this request is being issued for
2849 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02002850 * Return: the pointer to the new tt_req_node struct if no request
Marek Lindner383b8632015-06-18 16:24:24 +08002851 * has already been issued for this orig_node, NULL otherwise.
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002852 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002853static struct batadv_tt_req_node *
Marek Lindner383b8632015-06-18 16:24:24 +08002854batadv_tt_req_node_new(struct batadv_priv *bat_priv,
Sven Eckelmann56303d32012-06-05 22:31:31 +02002855 struct batadv_orig_node *orig_node)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002856{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002857 struct batadv_tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002858
Sven Eckelmann807736f2012-07-15 22:26:51 +02002859 spin_lock_bh(&bat_priv->tt.req_list_lock);
Marek Lindner7c26a532015-06-28 22:16:06 +08002860 hlist_for_each_entry(tt_req_node_tmp, &bat_priv->tt.req_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002861 if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
2862 !batadv_has_timed_out(tt_req_node_tmp->issued_at,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002863 BATADV_TT_REQUEST_TIMEOUT))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002864 goto unlock;
2865 }
2866
Sven Eckelmann86452f82016-06-25 16:44:06 +02002867 tt_req_node = kmem_cache_alloc(batadv_tt_req_cache, GFP_ATOMIC);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002868 if (!tt_req_node)
2869 goto unlock;
2870
Sven Eckelmann9c4604a2016-06-26 11:16:10 +02002871 kref_init(&tt_req_node->refcount);
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01002872 ether_addr_copy(tt_req_node->addr, orig_node->orig);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002873 tt_req_node->issued_at = jiffies;
2874
Sven Eckelmann9c4604a2016-06-26 11:16:10 +02002875 kref_get(&tt_req_node->refcount);
Marek Lindner7c26a532015-06-28 22:16:06 +08002876 hlist_add_head(&tt_req_node->list, &bat_priv->tt.req_list);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002877unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02002878 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002879 return tt_req_node;
2880}
2881
Marek Lindner335fbe02013-04-23 21:40:02 +08002882/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01002883 * batadv_tt_local_valid() - verify that given tt entry is a valid one
Marek Lindner335fbe02013-04-23 21:40:02 +08002884 * @entry_ptr: to be checked local tt entry
2885 * @data_ptr: not used but definition required to satisfy the callback prototype
2886 *
Sven Eckelmann4b426b12016-02-22 21:02:39 +01002887 * Return: true if the entry is a valid, false otherwise.
Marek Lindner335fbe02013-04-23 21:40:02 +08002888 */
Sven Eckelmann4b426b12016-02-22 21:02:39 +01002889static bool batadv_tt_local_valid(const void *entry_ptr, const void *data_ptr)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002890{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002891 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002892
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002893 if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
Sven Eckelmann4b426b12016-02-22 21:02:39 +01002894 return false;
2895 return true;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002896}
2897
Sven Eckelmann4b426b12016-02-22 21:02:39 +01002898static bool batadv_tt_global_valid(const void *entry_ptr,
2899 const void *data_ptr)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002900{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002901 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
2902 const struct batadv_tt_global_entry *tt_global_entry;
2903 const struct batadv_orig_node *orig_node = data_ptr;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002904
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002905 if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM ||
2906 tt_common_entry->flags & BATADV_TT_CLIENT_TEMP)
Sven Eckelmann4b426b12016-02-22 21:02:39 +01002907 return false;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002908
Sven Eckelmann56303d32012-06-05 22:31:31 +02002909 tt_global_entry = container_of(tt_common_entry,
2910 struct batadv_tt_global_entry,
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002911 common);
2912
Sven Eckelmanna5130882012-05-16 20:23:16 +02002913 return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002914}
2915
Marek Lindner335fbe02013-04-23 21:40:02 +08002916/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01002917 * batadv_tt_tvlv_generate() - fill the tvlv buff with the tt entries from the
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002918 * specified tt hash
Marek Lindner335fbe02013-04-23 21:40:02 +08002919 * @bat_priv: the bat priv with all the soft interface information
2920 * @hash: hash table containing the tt entries
2921 * @tt_len: expected tvlv tt data buffer length in number of bytes
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002922 * @tvlv_buff: pointer to the buffer to fill with the TT data
Marek Lindner335fbe02013-04-23 21:40:02 +08002923 * @valid_cb: function to filter tt change entries
2924 * @cb_data: data passed to the filter function as argument
Marek Lindner335fbe02013-04-23 21:40:02 +08002925 */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002926static void batadv_tt_tvlv_generate(struct batadv_priv *bat_priv,
2927 struct batadv_hashtable *hash,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002928 void *tvlv_buff, u16 tt_len,
Sven Eckelmann4b426b12016-02-22 21:02:39 +01002929 bool (*valid_cb)(const void *,
2930 const void *),
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002931 void *cb_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002932{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002933 struct batadv_tt_common_entry *tt_common_entry;
Marek Lindner335fbe02013-04-23 21:40:02 +08002934 struct batadv_tvlv_tt_change *tt_change;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002935 struct hlist_head *head;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002936 u16 tt_tot, tt_num_entries = 0;
2937 u32 i;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002938
Antonio Quartulli298e6e62013-05-28 13:14:27 +02002939 tt_tot = batadv_tt_entries(tt_len);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002940 tt_change = (struct batadv_tvlv_tt_change *)tvlv_buff;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002941
2942 rcu_read_lock();
2943 for (i = 0; i < hash->size; i++) {
2944 head = &hash->table[i];
2945
Sasha Levinb67bfe02013-02-27 17:06:00 -08002946 hlist_for_each_entry_rcu(tt_common_entry,
Antonio Quartullia73105b2011-04-27 14:27:44 +02002947 head, hash_entry) {
Marek Lindner335fbe02013-04-23 21:40:02 +08002948 if (tt_tot == tt_num_entries)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002949 break;
2950
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002951 if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002952 continue;
2953
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01002954 ether_addr_copy(tt_change->addr, tt_common_entry->addr);
Antonio Quartulli27b37eb2012-11-08 14:21:11 +01002955 tt_change->flags = tt_common_entry->flags;
Antonio Quartullic018ad32013-06-04 12:11:39 +02002956 tt_change->vid = htons(tt_common_entry->vid);
Antonio Quartullica663042013-12-15 13:26:55 +01002957 memset(tt_change->reserved, 0,
2958 sizeof(tt_change->reserved));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002959
Marek Lindner335fbe02013-04-23 21:40:02 +08002960 tt_num_entries++;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002961 tt_change++;
2962 }
2963 }
2964 rcu_read_unlock();
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002965}
Antonio Quartullia73105b2011-04-27 14:27:44 +02002966
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002967/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01002968 * batadv_tt_global_check_crc() - check if all the CRCs are correct
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002969 * @orig_node: originator for which the CRCs have to be checked
2970 * @tt_vlan: pointer to the first tvlv VLAN entry
2971 * @num_vlan: number of tvlv VLAN entries
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002972 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02002973 * Return: true if all the received CRCs match the locally stored ones, false
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002974 * otherwise
2975 */
2976static bool batadv_tt_global_check_crc(struct batadv_orig_node *orig_node,
2977 struct batadv_tvlv_tt_vlan_data *tt_vlan,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002978 u16 num_vlan)
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002979{
2980 struct batadv_tvlv_tt_vlan_data *tt_vlan_tmp;
2981 struct batadv_orig_node_vlan *vlan;
Simon Wunderlichc169c592015-09-02 20:09:56 +02002982 int i, orig_num_vlan;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002983 u32 crc;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002984
2985 /* check if each received CRC matches the locally stored one */
2986 for (i = 0; i < num_vlan; i++) {
2987 tt_vlan_tmp = tt_vlan + i;
2988
2989 /* if orig_node is a backbone node for this VLAN, don't check
2990 * the CRC as we ignore all the global entries over it
2991 */
2992 if (batadv_bla_is_backbone_gw_orig(orig_node->bat_priv,
Antonio Quartullicfd4f752013-08-07 18:28:56 +02002993 orig_node->orig,
2994 ntohs(tt_vlan_tmp->vid)))
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002995 continue;
2996
2997 vlan = batadv_orig_node_vlan_get(orig_node,
2998 ntohs(tt_vlan_tmp->vid));
2999 if (!vlan)
3000 return false;
3001
Antonio Quartulli91c2b1a2014-01-28 02:06:47 +01003002 crc = vlan->tt.crc;
Sven Eckelmann21754e22016-01-17 11:01:24 +01003003 batadv_orig_node_vlan_put(vlan);
Antonio Quartulli91c2b1a2014-01-28 02:06:47 +01003004
3005 if (crc != ntohl(tt_vlan_tmp->crc))
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003006 return false;
3007 }
3008
Simon Wunderlichc169c592015-09-02 20:09:56 +02003009 /* check if any excess VLANs exist locally for the originator
3010 * which are not mentioned in the TVLV from the originator.
3011 */
3012 rcu_read_lock();
3013 orig_num_vlan = 0;
3014 hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list)
3015 orig_num_vlan++;
3016 rcu_read_unlock();
3017
3018 if (orig_num_vlan > num_vlan)
3019 return false;
3020
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003021 return true;
3022}
3023
3024/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01003025 * batadv_tt_local_update_crc() - update all the local CRCs
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003026 * @bat_priv: the bat priv with all the soft interface information
3027 */
3028static void batadv_tt_local_update_crc(struct batadv_priv *bat_priv)
3029{
3030 struct batadv_softif_vlan *vlan;
3031
3032 /* recompute the global CRC for each VLAN */
3033 rcu_read_lock();
3034 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
3035 vlan->tt.crc = batadv_tt_local_crc(bat_priv, vlan->vid);
3036 }
3037 rcu_read_unlock();
3038}
3039
3040/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01003041 * batadv_tt_global_update_crc() - update all the global CRCs for this orig_node
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003042 * @bat_priv: the bat priv with all the soft interface information
3043 * @orig_node: the orig_node for which the CRCs have to be updated
3044 */
3045static void batadv_tt_global_update_crc(struct batadv_priv *bat_priv,
3046 struct batadv_orig_node *orig_node)
3047{
3048 struct batadv_orig_node_vlan *vlan;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003049 u32 crc;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003050
3051 /* recompute the global CRC for each VLAN */
3052 rcu_read_lock();
Marek Lindnerd0fa4f32015-06-22 00:30:22 +08003053 hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003054 /* if orig_node is a backbone node for this VLAN, don't compute
3055 * the CRC as we ignore all the global entries over it
3056 */
Antonio Quartullicfd4f752013-08-07 18:28:56 +02003057 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig,
3058 vlan->vid))
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003059 continue;
3060
3061 crc = batadv_tt_global_crc(bat_priv, orig_node, vlan->vid);
3062 vlan->tt.crc = crc;
3063 }
3064 rcu_read_unlock();
Antonio Quartullia73105b2011-04-27 14:27:44 +02003065}
3066
Antonio Quartulliced72932013-04-24 16:37:51 +02003067/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01003068 * batadv_send_tt_request() - send a TT Request message to a given node
Antonio Quartulliced72932013-04-24 16:37:51 +02003069 * @bat_priv: the bat priv with all the soft interface information
3070 * @dst_orig_node: the destination of the message
3071 * @ttvn: the version number that the source of the message is looking for
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003072 * @tt_vlan: pointer to the first tvlv VLAN object to request
3073 * @num_vlan: number of tvlv VLAN entries
Antonio Quartulliced72932013-04-24 16:37:51 +02003074 * @full_table: ask for the entire translation table if true, while only for the
3075 * last TT diff otherwise
Antonio Quartullid15cd622015-11-17 16:40:52 +08003076 *
3077 * Return: true if the TT Request was sent, false otherwise
Antonio Quartulliced72932013-04-24 16:37:51 +02003078 */
Sven Eckelmann4b426b12016-02-22 21:02:39 +01003079static bool batadv_send_tt_request(struct batadv_priv *bat_priv,
3080 struct batadv_orig_node *dst_orig_node,
3081 u8 ttvn,
3082 struct batadv_tvlv_tt_vlan_data *tt_vlan,
3083 u16 num_vlan, bool full_table)
Antonio Quartullia73105b2011-04-27 14:27:44 +02003084{
Marek Lindner335fbe02013-04-23 21:40:02 +08003085 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +02003086 struct batadv_tt_req_node *tt_req_node = NULL;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003087 struct batadv_tvlv_tt_vlan_data *tt_vlan_req;
3088 struct batadv_hard_iface *primary_if;
Marek Lindner335fbe02013-04-23 21:40:02 +08003089 bool ret = false;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003090 int i, size;
Antonio Quartullia73105b2011-04-27 14:27:44 +02003091
Sven Eckelmanne5d89252012-05-12 13:48:54 +02003092 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003093 if (!primary_if)
3094 goto out;
3095
3096 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003097 * reply from the same orig_node yet
3098 */
Marek Lindner383b8632015-06-18 16:24:24 +08003099 tt_req_node = batadv_tt_req_node_new(bat_priv, dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003100 if (!tt_req_node)
3101 goto out;
3102
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003103 size = sizeof(*tvlv_tt_data) + sizeof(*tt_vlan_req) * num_vlan;
3104 tvlv_tt_data = kzalloc(size, GFP_ATOMIC);
Marek Lindner335fbe02013-04-23 21:40:02 +08003105 if (!tvlv_tt_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02003106 goto out;
3107
Marek Lindner335fbe02013-04-23 21:40:02 +08003108 tvlv_tt_data->flags = BATADV_TT_REQUEST;
3109 tvlv_tt_data->ttvn = ttvn;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003110 tvlv_tt_data->num_vlan = htons(num_vlan);
3111
3112 /* send all the CRCs within the request. This is needed by intermediate
3113 * nodes to ensure they have the correct table before replying
3114 */
3115 tt_vlan_req = (struct batadv_tvlv_tt_vlan_data *)(tvlv_tt_data + 1);
3116 for (i = 0; i < num_vlan; i++) {
3117 tt_vlan_req->vid = tt_vlan->vid;
3118 tt_vlan_req->crc = tt_vlan->crc;
3119
3120 tt_vlan_req++;
3121 tt_vlan++;
3122 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02003123
3124 if (full_table)
Marek Lindner335fbe02013-04-23 21:40:02 +08003125 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02003126
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02003127 batadv_dbg(BATADV_DBG_TT, bat_priv, "Sending TT_REQUEST to %pM [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08003128 dst_orig_node->orig, full_table ? 'F' : '.');
Antonio Quartullia73105b2011-04-27 14:27:44 +02003129
Sven Eckelmannd69909d2012-06-03 22:19:20 +02003130 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
Marek Lindner335fbe02013-04-23 21:40:02 +08003131 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
3132 dst_orig_node->orig, BATADV_TVLV_TT, 1,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003133 tvlv_tt_data, size);
Marek Lindner335fbe02013-04-23 21:40:02 +08003134 ret = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02003135
3136out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02003137 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +01003138 batadv_hardif_put(primary_if);
Sven Eckelmann9c4604a2016-06-26 11:16:10 +02003139
Antonio Quartullia73105b2011-04-27 14:27:44 +02003140 if (ret && tt_req_node) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02003141 spin_lock_bh(&bat_priv->tt.req_list_lock);
Sven Eckelmann9c4604a2016-06-26 11:16:10 +02003142 if (!hlist_unhashed(&tt_req_node->list)) {
3143 hlist_del_init(&tt_req_node->list);
3144 batadv_tt_req_node_put(tt_req_node);
3145 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02003146 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003147 }
Sven Eckelmann9c4604a2016-06-26 11:16:10 +02003148
3149 if (tt_req_node)
3150 batadv_tt_req_node_put(tt_req_node);
3151
Marek Lindner335fbe02013-04-23 21:40:02 +08003152 kfree(tvlv_tt_data);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003153 return ret;
3154}
3155
Marek Lindner335fbe02013-04-23 21:40:02 +08003156/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01003157 * batadv_send_other_tt_response() - send reply to tt request concerning another
Marek Lindner335fbe02013-04-23 21:40:02 +08003158 * node's translation table
3159 * @bat_priv: the bat priv with all the soft interface information
3160 * @tt_data: tt data containing the tt request information
3161 * @req_src: mac address of tt request sender
3162 * @req_dst: mac address of tt request recipient
3163 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02003164 * Return: true if tt request reply was sent, false otherwise.
Marek Lindner335fbe02013-04-23 21:40:02 +08003165 */
3166static bool batadv_send_other_tt_response(struct batadv_priv *bat_priv,
3167 struct batadv_tvlv_tt_data *tt_data,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003168 u8 *req_src, u8 *req_dst)
Antonio Quartullia73105b2011-04-27 14:27:44 +02003169{
Sven Eckelmann170173b2012-10-07 12:02:22 +02003170 struct batadv_orig_node *req_dst_orig_node;
Sven Eckelmann56303d32012-06-05 22:31:31 +02003171 struct batadv_orig_node *res_dst_orig_node = NULL;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003172 struct batadv_tvlv_tt_change *tt_change;
Marek Lindner335fbe02013-04-23 21:40:02 +08003173 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003174 struct batadv_tvlv_tt_vlan_data *tt_vlan;
Marek Lindner335fbe02013-04-23 21:40:02 +08003175 bool ret = false, full_table;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003176 u8 orig_ttvn, req_ttvn;
3177 u16 tvlv_len;
3178 s32 tt_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02003179
Sven Eckelmann39c75a52012-06-03 22:19:22 +02003180 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02003181 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08003182 req_src, tt_data->ttvn, req_dst,
Sven Eckelmanna2f2b6c2015-04-23 18:22:24 +02003183 ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02003184
3185 /* Let's get the orig node of the REAL destination */
Marek Lindner335fbe02013-04-23 21:40:02 +08003186 req_dst_orig_node = batadv_orig_hash_find(bat_priv, req_dst);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003187 if (!req_dst_orig_node)
3188 goto out;
3189
Marek Lindner335fbe02013-04-23 21:40:02 +08003190 res_dst_orig_node = batadv_orig_hash_find(bat_priv, req_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003191 if (!res_dst_orig_node)
3192 goto out;
3193
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003194 orig_ttvn = (u8)atomic_read(&req_dst_orig_node->last_ttvn);
Marek Lindner335fbe02013-04-23 21:40:02 +08003195 req_ttvn = tt_data->ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02003196
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003197 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
Marek Lindner335fbe02013-04-23 21:40:02 +08003198 /* this node doesn't have the requested data */
Antonio Quartullia73105b2011-04-27 14:27:44 +02003199 if (orig_ttvn != req_ttvn ||
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003200 !batadv_tt_global_check_crc(req_dst_orig_node, tt_vlan,
3201 ntohs(tt_data->num_vlan)))
Antonio Quartullia73105b2011-04-27 14:27:44 +02003202 goto out;
3203
Antonio Quartulli015758d2011-07-09 17:52:13 +02003204 /* If the full table has been explicitly requested */
Marek Lindner335fbe02013-04-23 21:40:02 +08003205 if (tt_data->flags & BATADV_TT_FULL_TABLE ||
Antonio Quartullia73105b2011-04-27 14:27:44 +02003206 !req_dst_orig_node->tt_buff)
3207 full_table = true;
3208 else
3209 full_table = false;
3210
Marek Lindner335fbe02013-04-23 21:40:02 +08003211 /* TT fragmentation hasn't been implemented yet, so send as many
3212 * TT entries fit a single packet as possible only
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003213 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02003214 if (!full_table) {
3215 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
3216 tt_len = req_dst_orig_node->tt_buff_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02003217
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003218 tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
3219 &tvlv_tt_data,
3220 &tt_change,
3221 &tt_len);
3222 if (!tt_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02003223 goto unlock;
3224
Antonio Quartullia73105b2011-04-27 14:27:44 +02003225 /* Copy the last orig_node's OGM buffer */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003226 memcpy(tt_change, req_dst_orig_node->tt_buff,
Antonio Quartullia73105b2011-04-27 14:27:44 +02003227 req_dst_orig_node->tt_buff_len);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003228 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
3229 } else {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003230 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
3231 * in the initial part
3232 */
3233 tt_len = -1;
3234 tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
3235 &tvlv_tt_data,
3236 &tt_change,
3237 &tt_len);
3238 if (!tt_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02003239 goto out;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003240
3241 /* fill the rest of the tvlv with the real TT entries */
3242 batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.global_hash,
3243 tt_change, tt_len,
3244 batadv_tt_global_valid,
3245 req_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003246 }
3247
Marek Lindnera19d3d82013-05-27 15:33:25 +08003248 /* Don't send the response, if larger than fragmented packet. */
3249 tt_len = sizeof(struct batadv_unicast_tvlv_packet) + tvlv_len;
3250 if (tt_len > atomic_read(&bat_priv->packet_size_max)) {
3251 net_ratelimited_function(batadv_info, bat_priv->soft_iface,
3252 "Ignoring TT_REQUEST from %pM; Response size exceeds max packet size.\n",
3253 res_dst_orig_node->orig);
3254 goto out;
3255 }
3256
Marek Lindner335fbe02013-04-23 21:40:02 +08003257 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
3258 tvlv_tt_data->ttvn = req_ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02003259
3260 if (full_table)
Marek Lindner335fbe02013-04-23 21:40:02 +08003261 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02003262
Sven Eckelmann39c75a52012-06-03 22:19:22 +02003263 batadv_dbg(BATADV_DBG_TT, bat_priv,
Marek Lindner335fbe02013-04-23 21:40:02 +08003264 "Sending TT_RESPONSE %pM for %pM [%c] (ttvn: %u)\n",
3265 res_dst_orig_node->orig, req_dst_orig_node->orig,
3266 full_table ? 'F' : '.', req_ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003267
Sven Eckelmannd69909d2012-06-03 22:19:20 +02003268 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02003269
Marek Lindner335fbe02013-04-23 21:40:02 +08003270 batadv_tvlv_unicast_send(bat_priv, req_dst_orig_node->orig,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003271 req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
3272 tvlv_len);
Martin Hundebølle91ecfc2013-04-20 13:54:39 +02003273
Marek Lindner335fbe02013-04-23 21:40:02 +08003274 ret = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02003275 goto out;
3276
3277unlock:
3278 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
3279
3280out:
3281 if (res_dst_orig_node)
Sven Eckelmann5d967312016-01-17 11:01:09 +01003282 batadv_orig_node_put(res_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003283 if (req_dst_orig_node)
Sven Eckelmann5d967312016-01-17 11:01:09 +01003284 batadv_orig_node_put(req_dst_orig_node);
Marek Lindner335fbe02013-04-23 21:40:02 +08003285 kfree(tvlv_tt_data);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003286 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02003287}
Sven Eckelmann96412692012-06-05 22:31:30 +02003288
Marek Lindner335fbe02013-04-23 21:40:02 +08003289/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01003290 * batadv_send_my_tt_response() - send reply to tt request concerning this
3291 * node's translation table
Marek Lindner335fbe02013-04-23 21:40:02 +08003292 * @bat_priv: the bat priv with all the soft interface information
3293 * @tt_data: tt data containing the tt request information
3294 * @req_src: mac address of tt request sender
3295 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02003296 * Return: true if tt request reply was sent, false otherwise.
Marek Lindner335fbe02013-04-23 21:40:02 +08003297 */
3298static bool batadv_send_my_tt_response(struct batadv_priv *bat_priv,
3299 struct batadv_tvlv_tt_data *tt_data,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003300 u8 *req_src)
Antonio Quartullia73105b2011-04-27 14:27:44 +02003301{
Marek Lindner335fbe02013-04-23 21:40:02 +08003302 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +02003303 struct batadv_hard_iface *primary_if = NULL;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003304 struct batadv_tvlv_tt_change *tt_change;
3305 struct batadv_orig_node *orig_node;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003306 u8 my_ttvn, req_ttvn;
3307 u16 tvlv_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02003308 bool full_table;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003309 s32 tt_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02003310
Sven Eckelmann39c75a52012-06-03 22:19:22 +02003311 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02003312 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08003313 req_src, tt_data->ttvn,
Sven Eckelmanna2f2b6c2015-04-23 18:22:24 +02003314 ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02003315
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003316 spin_lock_bh(&bat_priv->tt.commit_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003317
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003318 my_ttvn = (u8)atomic_read(&bat_priv->tt.vn);
Marek Lindner335fbe02013-04-23 21:40:02 +08003319 req_ttvn = tt_data->ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02003320
Marek Lindner335fbe02013-04-23 21:40:02 +08003321 orig_node = batadv_orig_hash_find(bat_priv, req_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003322 if (!orig_node)
3323 goto out;
3324
Sven Eckelmanne5d89252012-05-12 13:48:54 +02003325 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003326 if (!primary_if)
3327 goto out;
3328
3329 /* If the full table has been explicitly requested or the gap
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003330 * is too big send the whole local translation table
3331 */
Marek Lindner335fbe02013-04-23 21:40:02 +08003332 if (tt_data->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
Sven Eckelmann807736f2012-07-15 22:26:51 +02003333 !bat_priv->tt.last_changeset)
Antonio Quartullia73105b2011-04-27 14:27:44 +02003334 full_table = true;
3335 else
3336 full_table = false;
3337
Marek Lindner335fbe02013-04-23 21:40:02 +08003338 /* TT fragmentation hasn't been implemented yet, so send as many
3339 * TT entries fit a single packet as possible only
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003340 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02003341 if (!full_table) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02003342 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003343
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003344 tt_len = bat_priv->tt.last_changeset_len;
3345 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
3346 &tvlv_tt_data,
3347 &tt_change,
3348 &tt_len);
Sven Eckelmannc2d0f482016-11-30 21:47:09 +01003349 if (!tt_len || !tvlv_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02003350 goto unlock;
3351
Marek Lindner335fbe02013-04-23 21:40:02 +08003352 /* Copy the last orig_node's OGM buffer */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003353 memcpy(tt_change, bat_priv->tt.last_changeset,
Sven Eckelmann807736f2012-07-15 22:26:51 +02003354 bat_priv->tt.last_changeset_len);
3355 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003356 } else {
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003357 req_ttvn = (u8)atomic_read(&bat_priv->tt.vn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003358
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003359 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
3360 * in the initial part
3361 */
3362 tt_len = -1;
3363 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
3364 &tvlv_tt_data,
3365 &tt_change,
3366 &tt_len);
Sven Eckelmannc2d0f482016-11-30 21:47:09 +01003367 if (!tt_len || !tvlv_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02003368 goto out;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003369
3370 /* fill the rest of the tvlv with the real TT entries */
3371 batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.local_hash,
3372 tt_change, tt_len,
3373 batadv_tt_local_valid, NULL);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003374 }
3375
Marek Lindner335fbe02013-04-23 21:40:02 +08003376 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
3377 tvlv_tt_data->ttvn = req_ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02003378
3379 if (full_table)
Marek Lindner335fbe02013-04-23 21:40:02 +08003380 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02003381
Sven Eckelmann39c75a52012-06-03 22:19:22 +02003382 batadv_dbg(BATADV_DBG_TT, bat_priv,
Marek Lindner335fbe02013-04-23 21:40:02 +08003383 "Sending TT_RESPONSE to %pM [%c] (ttvn: %u)\n",
3384 orig_node->orig, full_table ? 'F' : '.', req_ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003385
Sven Eckelmannd69909d2012-06-03 22:19:20 +02003386 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02003387
Marek Lindner335fbe02013-04-23 21:40:02 +08003388 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003389 req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
3390 tvlv_len);
Marek Lindner335fbe02013-04-23 21:40:02 +08003391
Antonio Quartullia73105b2011-04-27 14:27:44 +02003392 goto out;
3393
3394unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02003395 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003396out:
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003397 spin_unlock_bh(&bat_priv->tt.commit_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003398 if (orig_node)
Sven Eckelmann5d967312016-01-17 11:01:09 +01003399 batadv_orig_node_put(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003400 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +01003401 batadv_hardif_put(primary_if);
Marek Lindner335fbe02013-04-23 21:40:02 +08003402 kfree(tvlv_tt_data);
3403 /* The packet was for this host, so it doesn't need to be re-routed */
Antonio Quartullia73105b2011-04-27 14:27:44 +02003404 return true;
3405}
3406
Marek Lindner335fbe02013-04-23 21:40:02 +08003407/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01003408 * batadv_send_tt_response() - send reply to tt request
Marek Lindner335fbe02013-04-23 21:40:02 +08003409 * @bat_priv: the bat priv with all the soft interface information
3410 * @tt_data: tt data containing the tt request information
3411 * @req_src: mac address of tt request sender
3412 * @req_dst: mac address of tt request recipient
3413 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02003414 * Return: true if tt request reply was sent, false otherwise.
Marek Lindner335fbe02013-04-23 21:40:02 +08003415 */
3416static bool batadv_send_tt_response(struct batadv_priv *bat_priv,
3417 struct batadv_tvlv_tt_data *tt_data,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003418 u8 *req_src, u8 *req_dst)
Antonio Quartullia73105b2011-04-27 14:27:44 +02003419{
Antonio Quartullicfd4f752013-08-07 18:28:56 +02003420 if (batadv_is_my_mac(bat_priv, req_dst))
Marek Lindner335fbe02013-04-23 21:40:02 +08003421 return batadv_send_my_tt_response(bat_priv, tt_data, req_src);
Antonio Quartulli24820df2014-09-01 14:37:25 +02003422 return batadv_send_other_tt_response(bat_priv, tt_data, req_src,
3423 req_dst);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003424}
3425
Sven Eckelmann56303d32012-06-05 22:31:31 +02003426static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
3427 struct batadv_orig_node *orig_node,
Marek Lindner335fbe02013-04-23 21:40:02 +08003428 struct batadv_tvlv_tt_change *tt_change,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003429 u16 tt_num_changes, u8 ttvn)
Antonio Quartullia73105b2011-04-27 14:27:44 +02003430{
3431 int i;
Sven Eckelmanna5130882012-05-16 20:23:16 +02003432 int roams;
Antonio Quartullia73105b2011-04-27 14:27:44 +02003433
3434 for (i = 0; i < tt_num_changes; i++) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +02003435 if ((tt_change + i)->flags & BATADV_TT_CLIENT_DEL) {
3436 roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM;
Sven Eckelmanna5130882012-05-16 20:23:16 +02003437 batadv_tt_global_del(bat_priv, orig_node,
3438 (tt_change + i)->addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +02003439 ntohs((tt_change + i)->vid),
Antonio Quartullid4f44692012-05-25 00:00:54 +02003440 "tt removed by changes",
3441 roams);
Sven Eckelmann08c36d32012-05-12 02:09:39 +02003442 } else {
Sven Eckelmann08c36d32012-05-12 02:09:39 +02003443 if (!batadv_tt_global_add(bat_priv, orig_node,
Antonio Quartullid4f44692012-05-25 00:00:54 +02003444 (tt_change + i)->addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +02003445 ntohs((tt_change + i)->vid),
Antonio Quartullid4f44692012-05-25 00:00:54 +02003446 (tt_change + i)->flags, ttvn))
Antonio Quartullia73105b2011-04-27 14:27:44 +02003447 /* In case of problem while storing a
3448 * global_entry, we stop the updating
3449 * procedure without committing the
3450 * ttvn change. This will avoid to send
3451 * corrupted data on tt_request
3452 */
3453 return;
Sven Eckelmann08c36d32012-05-12 02:09:39 +02003454 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02003455 }
Linus Lüssingac4eebd2015-06-16 17:10:24 +02003456 set_bit(BATADV_ORIG_CAPA_HAS_TT, &orig_node->capa_initialized);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003457}
3458
Sven Eckelmann56303d32012-06-05 22:31:31 +02003459static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003460 struct batadv_tvlv_tt_change *tt_change,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003461 u8 ttvn, u8 *resp_src,
3462 u16 num_entries)
Antonio Quartullia73105b2011-04-27 14:27:44 +02003463{
Sven Eckelmann170173b2012-10-07 12:02:22 +02003464 struct batadv_orig_node *orig_node;
Antonio Quartullia73105b2011-04-27 14:27:44 +02003465
Marek Lindner335fbe02013-04-23 21:40:02 +08003466 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003467 if (!orig_node)
3468 goto out;
3469
3470 /* Purge the old table first.. */
Antonio Quartulli95fb1302013-08-07 18:28:55 +02003471 batadv_tt_global_del_orig(bat_priv, orig_node, -1,
3472 "Received full table");
Antonio Quartullia73105b2011-04-27 14:27:44 +02003473
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003474 _batadv_tt_update_changes(bat_priv, orig_node, tt_change, num_entries,
3475 ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003476
3477 spin_lock_bh(&orig_node->tt_buff_lock);
3478 kfree(orig_node->tt_buff);
3479 orig_node->tt_buff_len = 0;
3480 orig_node->tt_buff = NULL;
3481 spin_unlock_bh(&orig_node->tt_buff_lock);
3482
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003483 atomic_set(&orig_node->last_ttvn, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003484
3485out:
3486 if (orig_node)
Sven Eckelmann5d967312016-01-17 11:01:09 +01003487 batadv_orig_node_put(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003488}
3489
Sven Eckelmann56303d32012-06-05 22:31:31 +02003490static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
3491 struct batadv_orig_node *orig_node,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003492 u16 tt_num_changes, u8 ttvn,
Marek Lindner335fbe02013-04-23 21:40:02 +08003493 struct batadv_tvlv_tt_change *tt_change)
Antonio Quartullia73105b2011-04-27 14:27:44 +02003494{
Sven Eckelmanna5130882012-05-16 20:23:16 +02003495 _batadv_tt_update_changes(bat_priv, orig_node, tt_change,
3496 tt_num_changes, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003497
Antonio Quartullie8cf2342013-05-28 13:14:28 +02003498 batadv_tt_save_orig_buffer(bat_priv, orig_node, tt_change,
3499 batadv_tt_len(tt_num_changes));
Antonio Quartullia73105b2011-04-27 14:27:44 +02003500 atomic_set(&orig_node->last_ttvn, ttvn);
3501}
3502
Antonio Quartullic018ad32013-06-04 12:11:39 +02003503/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01003504 * batadv_is_my_client() - check if a client is served by the local node
Antonio Quartullic018ad32013-06-04 12:11:39 +02003505 * @bat_priv: the bat priv with all the soft interface information
Antonio Quartulli3f687852014-11-02 11:29:56 +01003506 * @addr: the mac address of the client to check
Antonio Quartullic018ad32013-06-04 12:11:39 +02003507 * @vid: VLAN identifier
3508 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02003509 * Return: true if the client is served by this node, false otherwise.
Antonio Quartullic018ad32013-06-04 12:11:39 +02003510 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003511bool batadv_is_my_client(struct batadv_priv *bat_priv, const u8 *addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +02003512 unsigned short vid)
Antonio Quartullia73105b2011-04-27 14:27:44 +02003513{
Sven Eckelmann170173b2012-10-07 12:02:22 +02003514 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02003515 bool ret = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02003516
Antonio Quartullic018ad32013-06-04 12:11:39 +02003517 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02003518 if (!tt_local_entry)
3519 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003520 /* Check if the client has been logically deleted (but is kept for
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003521 * consistency purpose)
3522 */
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003523 if ((tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING) ||
3524 (tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003525 goto out;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02003526 ret = true;
3527out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02003528 if (tt_local_entry)
Sven Eckelmann95c0db92016-01-17 11:01:25 +01003529 batadv_tt_local_entry_put(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02003530 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02003531}
3532
Marek Lindner335fbe02013-04-23 21:40:02 +08003533/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01003534 * batadv_handle_tt_response() - process incoming tt reply
Marek Lindner335fbe02013-04-23 21:40:02 +08003535 * @bat_priv: the bat priv with all the soft interface information
3536 * @tt_data: tt data containing the tt request information
3537 * @resp_src: mac address of tt reply sender
3538 * @num_entries: number of tt change entries appended to the tt data
3539 */
3540static void batadv_handle_tt_response(struct batadv_priv *bat_priv,
3541 struct batadv_tvlv_tt_data *tt_data,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003542 u8 *resp_src, u16 num_entries)
Antonio Quartullia73105b2011-04-27 14:27:44 +02003543{
Marek Lindner7c26a532015-06-28 22:16:06 +08003544 struct batadv_tt_req_node *node;
3545 struct hlist_node *safe;
Sven Eckelmann56303d32012-06-05 22:31:31 +02003546 struct batadv_orig_node *orig_node = NULL;
Marek Lindner335fbe02013-04-23 21:40:02 +08003547 struct batadv_tvlv_tt_change *tt_change;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003548 u8 *tvlv_ptr = (u8 *)tt_data;
3549 u16 change_offset;
Antonio Quartullia73105b2011-04-27 14:27:44 +02003550
Sven Eckelmann39c75a52012-06-03 22:19:22 +02003551 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02003552 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08003553 resp_src, tt_data->ttvn, num_entries,
Sven Eckelmanna2f2b6c2015-04-23 18:22:24 +02003554 ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02003555
Marek Lindner335fbe02013-04-23 21:40:02 +08003556 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003557 if (!orig_node)
3558 goto out;
3559
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003560 spin_lock_bh(&orig_node->tt_lock);
3561
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003562 change_offset = sizeof(struct batadv_tvlv_tt_vlan_data);
3563 change_offset *= ntohs(tt_data->num_vlan);
3564 change_offset += sizeof(*tt_data);
3565 tvlv_ptr += change_offset;
3566
3567 tt_change = (struct batadv_tvlv_tt_change *)tvlv_ptr;
Marek Lindner335fbe02013-04-23 21:40:02 +08003568 if (tt_data->flags & BATADV_TT_FULL_TABLE) {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003569 batadv_tt_fill_gtable(bat_priv, tt_change, tt_data->ttvn,
3570 resp_src, num_entries);
Sven Eckelmann96412692012-06-05 22:31:30 +02003571 } else {
Marek Lindner335fbe02013-04-23 21:40:02 +08003572 batadv_tt_update_changes(bat_priv, orig_node, num_entries,
3573 tt_data->ttvn, tt_change);
Sven Eckelmann96412692012-06-05 22:31:30 +02003574 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02003575
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003576 /* Recalculate the CRC for this orig_node and store it */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003577 batadv_tt_global_update_crc(bat_priv, orig_node);
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003578
3579 spin_unlock_bh(&orig_node->tt_lock);
3580
Antonio Quartullia73105b2011-04-27 14:27:44 +02003581 /* Delete the tt_req_node from pending tt_requests list */
Sven Eckelmann807736f2012-07-15 22:26:51 +02003582 spin_lock_bh(&bat_priv->tt.req_list_lock);
Marek Lindner7c26a532015-06-28 22:16:06 +08003583 hlist_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Marek Lindner335fbe02013-04-23 21:40:02 +08003584 if (!batadv_compare_eth(node->addr, resp_src))
Antonio Quartullia73105b2011-04-27 14:27:44 +02003585 continue;
Marek Lindner7c26a532015-06-28 22:16:06 +08003586 hlist_del_init(&node->list);
Sven Eckelmann9c4604a2016-06-26 11:16:10 +02003587 batadv_tt_req_node_put(node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003588 }
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003589
Sven Eckelmann807736f2012-07-15 22:26:51 +02003590 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003591out:
3592 if (orig_node)
Sven Eckelmann5d967312016-01-17 11:01:09 +01003593 batadv_orig_node_put(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003594}
3595
Sven Eckelmann56303d32012-06-05 22:31:31 +02003596static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02003597{
Sven Eckelmann56303d32012-06-05 22:31:31 +02003598 struct batadv_tt_roam_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02003599
Sven Eckelmann807736f2012-07-15 22:26:51 +02003600 spin_lock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003601
Sven Eckelmann807736f2012-07-15 22:26:51 +02003602 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
Antonio Quartullicc47f662011-04-27 14:27:57 +02003603 list_del(&node->list);
Sven Eckelmann86452f82016-06-25 16:44:06 +02003604 kmem_cache_free(batadv_tt_roam_cache, node);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003605 }
3606
Sven Eckelmann807736f2012-07-15 22:26:51 +02003607 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003608}
3609
Sven Eckelmann56303d32012-06-05 22:31:31 +02003610static void batadv_tt_roam_purge(struct batadv_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02003611{
Sven Eckelmann56303d32012-06-05 22:31:31 +02003612 struct batadv_tt_roam_node *node, *safe;
Antonio Quartullicc47f662011-04-27 14:27:57 +02003613
Sven Eckelmann807736f2012-07-15 22:26:51 +02003614 spin_lock_bh(&bat_priv->tt.roam_list_lock);
3615 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02003616 if (!batadv_has_timed_out(node->first_time,
3617 BATADV_ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02003618 continue;
3619
3620 list_del(&node->list);
Sven Eckelmann86452f82016-06-25 16:44:06 +02003621 kmem_cache_free(batadv_tt_roam_cache, node);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003622 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02003623 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003624}
3625
Sven Eckelmann62fe7102015-09-15 19:00:48 +02003626/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01003627 * batadv_tt_check_roam_count() - check if a client has roamed too frequently
Antonio Quartullid15cd622015-11-17 16:40:52 +08003628 * @bat_priv: the bat priv with all the soft interface information
3629 * @client: mac address of the roaming client
Sven Eckelmann62fe7102015-09-15 19:00:48 +02003630 *
3631 * This function checks whether the client already reached the
Antonio Quartullicc47f662011-04-27 14:27:57 +02003632 * maximum number of possible roaming phases. In this case the ROAMING_ADV
3633 * will not be sent.
3634 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02003635 * Return: true if the ROAMING_ADV can be sent, false otherwise
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003636 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003637static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv, u8 *client)
Antonio Quartullicc47f662011-04-27 14:27:57 +02003638{
Sven Eckelmann56303d32012-06-05 22:31:31 +02003639 struct batadv_tt_roam_node *tt_roam_node;
Antonio Quartullicc47f662011-04-27 14:27:57 +02003640 bool ret = false;
3641
Sven Eckelmann807736f2012-07-15 22:26:51 +02003642 spin_lock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003643 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003644 * reply from the same orig_node yet
3645 */
Sven Eckelmann807736f2012-07-15 22:26:51 +02003646 list_for_each_entry(tt_roam_node, &bat_priv->tt.roam_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02003647 if (!batadv_compare_eth(tt_roam_node->addr, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02003648 continue;
3649
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02003650 if (batadv_has_timed_out(tt_roam_node->first_time,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02003651 BATADV_ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02003652 continue;
3653
Sven Eckelmann3e348192012-05-16 20:23:22 +02003654 if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
Antonio Quartullicc47f662011-04-27 14:27:57 +02003655 /* Sorry, you roamed too many times! */
3656 goto unlock;
3657 ret = true;
3658 break;
3659 }
3660
3661 if (!ret) {
Sven Eckelmann86452f82016-06-25 16:44:06 +02003662 tt_roam_node = kmem_cache_alloc(batadv_tt_roam_cache,
3663 GFP_ATOMIC);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003664 if (!tt_roam_node)
3665 goto unlock;
3666
3667 tt_roam_node->first_time = jiffies;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02003668 atomic_set(&tt_roam_node->counter,
3669 BATADV_ROAMING_MAX_COUNT - 1);
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01003670 ether_addr_copy(tt_roam_node->addr, client);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003671
Sven Eckelmann807736f2012-07-15 22:26:51 +02003672 list_add(&tt_roam_node->list, &bat_priv->tt.roam_list);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003673 ret = true;
3674 }
3675
3676unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02003677 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003678 return ret;
3679}
3680
Antonio Quartullic018ad32013-06-04 12:11:39 +02003681/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01003682 * batadv_send_roam_adv() - send a roaming advertisement message
Antonio Quartullic018ad32013-06-04 12:11:39 +02003683 * @bat_priv: the bat priv with all the soft interface information
3684 * @client: mac address of the roaming client
3685 * @vid: VLAN identifier
3686 * @orig_node: message destination
3687 *
3688 * Send a ROAMING_ADV message to the node which was previously serving this
3689 * client. This is done to inform the node that from now on all traffic destined
3690 * for this particular roamed client has to be forwarded to the sender of the
3691 * roaming message.
3692 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003693static void batadv_send_roam_adv(struct batadv_priv *bat_priv, u8 *client,
Antonio Quartullic018ad32013-06-04 12:11:39 +02003694 unsigned short vid,
Sven Eckelmann56303d32012-06-05 22:31:31 +02003695 struct batadv_orig_node *orig_node)
Antonio Quartullicc47f662011-04-27 14:27:57 +02003696{
Sven Eckelmann56303d32012-06-05 22:31:31 +02003697 struct batadv_hard_iface *primary_if;
Marek Lindner122edaa2013-04-23 21:40:03 +08003698 struct batadv_tvlv_roam_adv tvlv_roam;
3699
3700 primary_if = batadv_primary_if_get_selected(bat_priv);
3701 if (!primary_if)
3702 goto out;
Antonio Quartullicc47f662011-04-27 14:27:57 +02003703
3704 /* before going on we have to check whether the client has
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003705 * already roamed to us too many times
3706 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02003707 if (!batadv_tt_check_roam_count(bat_priv, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02003708 goto out;
3709
Sven Eckelmann39c75a52012-06-03 22:19:22 +02003710 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02003711 "Sending ROAMING_ADV to %pM (client %pM, vid: %d)\n",
Sven Eckelmannf7a2bd62017-02-22 17:16:42 +01003712 orig_node->orig, client, batadv_print_vid(vid));
Antonio Quartullicc47f662011-04-27 14:27:57 +02003713
Sven Eckelmannd69909d2012-06-03 22:19:20 +02003714 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02003715
Marek Lindner122edaa2013-04-23 21:40:03 +08003716 memcpy(tvlv_roam.client, client, sizeof(tvlv_roam.client));
Antonio Quartullic018ad32013-06-04 12:11:39 +02003717 tvlv_roam.vid = htons(vid);
Marek Lindner122edaa2013-04-23 21:40:03 +08003718
3719 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
3720 orig_node->orig, BATADV_TVLV_ROAM, 1,
3721 &tvlv_roam, sizeof(tvlv_roam));
Antonio Quartullicc47f662011-04-27 14:27:57 +02003722
3723out:
Marek Lindner122edaa2013-04-23 21:40:03 +08003724 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +01003725 batadv_hardif_put(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003726}
3727
Sven Eckelmanna5130882012-05-16 20:23:16 +02003728static void batadv_tt_purge(struct work_struct *work)
Antonio Quartullia73105b2011-04-27 14:27:44 +02003729{
Sven Eckelmann56303d32012-06-05 22:31:31 +02003730 struct delayed_work *delayed_work;
Sven Eckelmann807736f2012-07-15 22:26:51 +02003731 struct batadv_priv_tt *priv_tt;
Sven Eckelmann56303d32012-06-05 22:31:31 +02003732 struct batadv_priv *bat_priv;
3733
Geliang Tang4ba4bc02015-12-28 23:43:37 +08003734 delayed_work = to_delayed_work(work);
Sven Eckelmann807736f2012-07-15 22:26:51 +02003735 priv_tt = container_of(delayed_work, struct batadv_priv_tt, work);
3736 bat_priv = container_of(priv_tt, struct batadv_priv, tt);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003737
Marek Lindnera19d3d82013-05-27 15:33:25 +08003738 batadv_tt_local_purge(bat_priv, BATADV_TT_LOCAL_TIMEOUT);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003739 batadv_tt_global_purge(bat_priv);
Sven Eckelmanna5130882012-05-16 20:23:16 +02003740 batadv_tt_req_purge(bat_priv);
3741 batadv_tt_roam_purge(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003742
Antonio Quartulli72414442012-12-25 13:14:37 +01003743 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
3744 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
Antonio Quartullia73105b2011-04-27 14:27:44 +02003745}
Antonio Quartullicc47f662011-04-27 14:27:57 +02003746
Sven Eckelmannff15c272017-12-02 19:51:53 +01003747/**
3748 * batadv_tt_free() - Free translation table of soft interface
3749 * @bat_priv: the bat priv with all the soft interface information
3750 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02003751void batadv_tt_free(struct batadv_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02003752{
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003753 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_TT, 1);
3754 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_TT, 1);
3755
Sven Eckelmann807736f2012-07-15 22:26:51 +02003756 cancel_delayed_work_sync(&bat_priv->tt.work);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003757
Sven Eckelmanna5130882012-05-16 20:23:16 +02003758 batadv_tt_local_table_free(bat_priv);
3759 batadv_tt_global_table_free(bat_priv);
3760 batadv_tt_req_list_free(bat_priv);
3761 batadv_tt_changes_list_free(bat_priv);
3762 batadv_tt_roam_list_free(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003763
Sven Eckelmann807736f2012-07-15 22:26:51 +02003764 kfree(bat_priv->tt.last_changeset);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003765}
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003766
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003767/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01003768 * batadv_tt_local_set_flags() - set or unset the specified flags on the local
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003769 * table and possibly count them in the TT size
3770 * @bat_priv: the bat priv with all the soft interface information
3771 * @flags: the flag to switch
3772 * @enable: whether to set or unset the flag
3773 * @count: whether to increase the TT size by the number of changed entries
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003774 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003775static void batadv_tt_local_set_flags(struct batadv_priv *bat_priv, u16 flags,
3776 bool enable, bool count)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003777{
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003778 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
3779 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003780 struct hlist_head *head;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003781 u32 i;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003782
3783 if (!hash)
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003784 return;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003785
3786 for (i = 0; i < hash->size; i++) {
3787 head = &hash->table[i];
3788
3789 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08003790 hlist_for_each_entry_rcu(tt_common_entry,
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003791 head, hash_entry) {
Antonio Quartulli697f2532011-11-07 16:47:01 +01003792 if (enable) {
3793 if ((tt_common_entry->flags & flags) == flags)
3794 continue;
3795 tt_common_entry->flags |= flags;
3796 } else {
3797 if (!(tt_common_entry->flags & flags))
3798 continue;
3799 tt_common_entry->flags &= ~flags;
3800 }
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003801
3802 if (!count)
3803 continue;
3804
3805 batadv_tt_local_size_inc(bat_priv,
3806 tt_common_entry->vid);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003807 }
3808 rcu_read_unlock();
3809 }
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003810}
3811
Sven Eckelmannacd34af2012-06-03 22:19:21 +02003812/* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
Sven Eckelmann56303d32012-06-05 22:31:31 +02003813static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003814{
Sven Eckelmann807736f2012-07-15 22:26:51 +02003815 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02003816 struct batadv_tt_common_entry *tt_common;
3817 struct batadv_tt_local_entry *tt_local;
Sasha Levinb67bfe02013-02-27 17:06:00 -08003818 struct hlist_node *node_tmp;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003819 struct hlist_head *head;
3820 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003821 u32 i;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003822
3823 if (!hash)
3824 return;
3825
3826 for (i = 0; i < hash->size; i++) {
3827 head = &hash->table[i];
3828 list_lock = &hash->list_locks[i];
3829
3830 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08003831 hlist_for_each_entry_safe(tt_common, node_tmp, head,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02003832 hash_entry) {
3833 if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003834 continue;
3835
Sven Eckelmann39c75a52012-06-03 22:19:22 +02003836 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02003837 "Deleting local tt entry (%pM, vid: %d): pending\n",
3838 tt_common->addr,
Sven Eckelmannf7a2bd62017-02-22 17:16:42 +01003839 batadv_print_vid(tt_common->vid));
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003840
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003841 batadv_tt_local_size_dec(bat_priv, tt_common->vid);
Sasha Levinb67bfe02013-02-27 17:06:00 -08003842 hlist_del_rcu(&tt_common->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02003843 tt_local = container_of(tt_common,
3844 struct batadv_tt_local_entry,
3845 common);
Antonio Quartulli35df3b22014-05-08 17:13:15 +02003846
Sven Eckelmann95c0db92016-01-17 11:01:25 +01003847 batadv_tt_local_entry_put(tt_local);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003848 }
3849 spin_unlock_bh(list_lock);
3850 }
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003851}
3852
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003853/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01003854 * batadv_tt_local_commit_changes_nolock() - commit all pending local tt changes
Marek Lindnera19d3d82013-05-27 15:33:25 +08003855 * which have been queued in the time since the last commit
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003856 * @bat_priv: the bat priv with all the soft interface information
Marek Lindnera19d3d82013-05-27 15:33:25 +08003857 *
3858 * Caller must hold tt->commit_lock.
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003859 */
Marek Lindnera19d3d82013-05-27 15:33:25 +08003860static void batadv_tt_local_commit_changes_nolock(struct batadv_priv *bat_priv)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003861{
Sven Eckelmann5274cd62015-06-21 14:45:15 +02003862 lockdep_assert_held(&bat_priv->tt.commit_lock);
3863
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003864 if (atomic_read(&bat_priv->tt.local_changes) < 1) {
3865 if (!batadv_atomic_dec_not_zero(&bat_priv->tt.ogm_append_cnt))
3866 batadv_tt_tvlv_container_update(bat_priv);
Marek Lindnera19d3d82013-05-27 15:33:25 +08003867 return;
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003868 }
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08003869
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003870 batadv_tt_local_set_flags(bat_priv, BATADV_TT_CLIENT_NEW, false, true);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08003871
Sven Eckelmanna5130882012-05-16 20:23:16 +02003872 batadv_tt_local_purge_pending_clients(bat_priv);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003873 batadv_tt_local_update_crc(bat_priv);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003874
3875 /* Increment the TTVN only once per OGM interval */
Sven Eckelmann807736f2012-07-15 22:26:51 +02003876 atomic_inc(&bat_priv->tt.vn);
Sven Eckelmann39c75a52012-06-03 22:19:22 +02003877 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02003878 "Local changes committed, updating to ttvn %u\n",
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003879 (u8)atomic_read(&bat_priv->tt.vn));
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08003880
3881 /* reset the sending counter */
Sven Eckelmann807736f2012-07-15 22:26:51 +02003882 atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003883 batadv_tt_tvlv_container_update(bat_priv);
Marek Lindnera19d3d82013-05-27 15:33:25 +08003884}
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003885
Marek Lindnera19d3d82013-05-27 15:33:25 +08003886/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01003887 * batadv_tt_local_commit_changes() - commit all pending local tt changes which
Marek Lindnera19d3d82013-05-27 15:33:25 +08003888 * have been queued in the time since the last commit
3889 * @bat_priv: the bat priv with all the soft interface information
3890 */
3891void batadv_tt_local_commit_changes(struct batadv_priv *bat_priv)
3892{
3893 spin_lock_bh(&bat_priv->tt.commit_lock);
3894 batadv_tt_local_commit_changes_nolock(bat_priv);
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003895 spin_unlock_bh(&bat_priv->tt.commit_lock);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003896}
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003897
Sven Eckelmannff15c272017-12-02 19:51:53 +01003898/**
3899 * batadv_is_ap_isolated() - Check if packet from upper layer should be dropped
3900 * @bat_priv: the bat priv with all the soft interface information
3901 * @src: source mac address of packet
3902 * @dst: destination mac address of packet
3903 * @vid: vlan id of packet
3904 *
3905 * Return: true when src+dst(+vid) pair should be isolated, false otherwise
3906 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003907bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, u8 *src, u8 *dst,
3908 unsigned short vid)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003909{
Markus Elfring393b2992016-03-11 13:10:20 +01003910 struct batadv_tt_local_entry *tt_local_entry;
3911 struct batadv_tt_global_entry *tt_global_entry;
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003912 struct batadv_softif_vlan *vlan;
Marek Lindner5870adc2012-06-20 17:16:05 +02003913 bool ret = false;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003914
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003915 vlan = batadv_softif_vlan_get(bat_priv, vid);
Markus Elfringe087f342015-11-03 19:20:34 +01003916 if (!vlan)
3917 return false;
3918
3919 if (!atomic_read(&vlan->ap_isolation))
Markus Elfring393b2992016-03-11 13:10:20 +01003920 goto vlan_put;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003921
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003922 tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst, vid);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003923 if (!tt_local_entry)
Markus Elfring393b2992016-03-11 13:10:20 +01003924 goto vlan_put;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003925
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003926 tt_global_entry = batadv_tt_global_hash_find(bat_priv, src, vid);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003927 if (!tt_global_entry)
Markus Elfring393b2992016-03-11 13:10:20 +01003928 goto local_entry_put;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003929
Markus Elfring393b2992016-03-11 13:10:20 +01003930 if (_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
3931 ret = true;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003932
Markus Elfring393b2992016-03-11 13:10:20 +01003933 batadv_tt_global_entry_put(tt_global_entry);
3934local_entry_put:
3935 batadv_tt_local_entry_put(tt_local_entry);
3936vlan_put:
Sven Eckelmann9c3bf082016-01-17 11:01:21 +01003937 batadv_softif_vlan_put(vlan);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003938 return ret;
3939}
Marek Lindnera943cac2011-07-30 13:10:18 +02003940
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003941/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01003942 * batadv_tt_update_orig() - update global translation table with new tt
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003943 * information received via ogms
3944 * @bat_priv: the bat priv with all the soft interface information
Sven Eckelmanne51f0392015-09-06 21:38:51 +02003945 * @orig_node: the orig_node of the ogm
3946 * @tt_buff: pointer to the first tvlv VLAN entry
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003947 * @tt_num_vlan: number of tvlv VLAN entries
3948 * @tt_change: pointer to the first entry in the TT buffer
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003949 * @tt_num_changes: number of tt changes inside the tt buffer
3950 * @ttvn: translation table version number of this changeset
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003951 */
3952static void batadv_tt_update_orig(struct batadv_priv *bat_priv,
3953 struct batadv_orig_node *orig_node,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003954 const void *tt_buff, u16 tt_num_vlan,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003955 struct batadv_tvlv_tt_change *tt_change,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003956 u16 tt_num_changes, u8 ttvn)
Marek Lindnera943cac2011-07-30 13:10:18 +02003957{
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003958 u8 orig_ttvn = (u8)atomic_read(&orig_node->last_ttvn);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003959 struct batadv_tvlv_tt_vlan_data *tt_vlan;
Marek Lindnera943cac2011-07-30 13:10:18 +02003960 bool full_table = true;
Linus Lüssinge17931d2014-02-15 17:47:50 +01003961 bool has_tt_init;
Marek Lindnera943cac2011-07-30 13:10:18 +02003962
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003963 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)tt_buff;
Linus Lüssingac4eebd2015-06-16 17:10:24 +02003964 has_tt_init = test_bit(BATADV_ORIG_CAPA_HAS_TT,
3965 &orig_node->capa_initialized);
Linus Lüssinge17931d2014-02-15 17:47:50 +01003966
Antonio Quartulli17071572011-11-07 16:36:40 +01003967 /* orig table not initialised AND first diff is in the OGM OR the ttvn
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003968 * increased by one -> we can apply the attached changes
3969 */
Linus Lüssinge17931d2014-02-15 17:47:50 +01003970 if ((!has_tt_init && ttvn == 1) || ttvn - orig_ttvn == 1) {
Marek Lindnera943cac2011-07-30 13:10:18 +02003971 /* the OGM could not contain the changes due to their size or
Sven Eckelmann42d0b042012-06-03 22:19:17 +02003972 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
3973 * times.
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003974 * In this case send a tt request
3975 */
Marek Lindnera943cac2011-07-30 13:10:18 +02003976 if (!tt_num_changes) {
3977 full_table = false;
3978 goto request_table;
3979 }
3980
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003981 spin_lock_bh(&orig_node->tt_lock);
3982
Sven Eckelmanna5130882012-05-16 20:23:16 +02003983 batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
Sven Eckelmann96412692012-06-05 22:31:30 +02003984 ttvn, tt_change);
Marek Lindnera943cac2011-07-30 13:10:18 +02003985
3986 /* Even if we received the precomputed crc with the OGM, we
3987 * prefer to recompute it to spot any possible inconsistency
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003988 * in the global table
3989 */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003990 batadv_tt_global_update_crc(bat_priv, orig_node);
Marek Lindnera943cac2011-07-30 13:10:18 +02003991
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003992 spin_unlock_bh(&orig_node->tt_lock);
3993
Marek Lindnera943cac2011-07-30 13:10:18 +02003994 /* The ttvn alone is not enough to guarantee consistency
3995 * because a single value could represent different states
3996 * (due to the wrap around). Thus a node has to check whether
3997 * the resulting table (after applying the changes) is still
3998 * consistent or not. E.g. a node could disconnect while its
3999 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
4000 * checking the CRC value is mandatory to detect the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02004001 * inconsistency
4002 */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02004003 if (!batadv_tt_global_check_crc(orig_node, tt_vlan,
4004 tt_num_vlan))
Marek Lindnera943cac2011-07-30 13:10:18 +02004005 goto request_table;
Marek Lindnera943cac2011-07-30 13:10:18 +02004006 } else {
4007 /* if we missed more than one change or our tables are not
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02004008 * in sync anymore -> request fresh tt data
4009 */
Linus Lüssinge17931d2014-02-15 17:47:50 +01004010 if (!has_tt_init || ttvn != orig_ttvn ||
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02004011 !batadv_tt_global_check_crc(orig_node, tt_vlan,
4012 tt_num_vlan)) {
Marek Lindnera943cac2011-07-30 13:10:18 +02004013request_table:
Sven Eckelmann39c75a52012-06-03 22:19:22 +02004014 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02004015 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u num_changes: %u)\n",
4016 orig_node->orig, ttvn, orig_ttvn,
4017 tt_num_changes);
Sven Eckelmanna5130882012-05-16 20:23:16 +02004018 batadv_send_tt_request(bat_priv, orig_node, ttvn,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02004019 tt_vlan, tt_num_vlan,
4020 full_table);
Marek Lindnera943cac2011-07-30 13:10:18 +02004021 return;
4022 }
4023 }
4024}
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01004025
Antonio Quartullic018ad32013-06-04 12:11:39 +02004026/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01004027 * batadv_tt_global_client_is_roaming() - check if a client is marked as roaming
Antonio Quartullic018ad32013-06-04 12:11:39 +02004028 * @bat_priv: the bat priv with all the soft interface information
4029 * @addr: the mac address of the client to check
4030 * @vid: VLAN identifier
4031 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02004032 * Return: true if we know that the client has moved from its old originator
Antonio Quartullic018ad32013-06-04 12:11:39 +02004033 * to another one. This entry is still kept for consistency purposes and will be
4034 * deleted later by a DEL or because of timeout
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01004035 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02004036bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02004037 u8 *addr, unsigned short vid)
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01004038{
Sven Eckelmann56303d32012-06-05 22:31:31 +02004039 struct batadv_tt_global_entry *tt_global_entry;
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01004040 bool ret = false;
4041
Antonio Quartullic018ad32013-06-04 12:11:39 +02004042 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01004043 if (!tt_global_entry)
4044 goto out;
4045
Antonio Quartullic1d07432013-01-15 22:17:19 +10004046 ret = tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM;
Sven Eckelmann5dafd8a2016-01-17 11:01:26 +01004047 batadv_tt_global_entry_put(tt_global_entry);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01004048out:
4049 return ret;
4050}
Antonio Quartulli30cfd022012-07-05 23:38:29 +02004051
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02004052/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01004053 * batadv_tt_local_client_is_roaming() - tells whether the client is roaming
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02004054 * @bat_priv: the bat priv with all the soft interface information
Antonio Quartullic018ad32013-06-04 12:11:39 +02004055 * @addr: the mac address of the local client to query
4056 * @vid: VLAN identifier
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02004057 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02004058 * Return: true if the local client is known to be roaming (it is not served by
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02004059 * this node anymore) or not. If yes, the client is still present in the table
4060 * to keep the latter consistent with the node TTVN
4061 */
4062bool batadv_tt_local_client_is_roaming(struct batadv_priv *bat_priv,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02004063 u8 *addr, unsigned short vid)
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02004064{
4065 struct batadv_tt_local_entry *tt_local_entry;
4066 bool ret = false;
4067
Antonio Quartullic018ad32013-06-04 12:11:39 +02004068 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02004069 if (!tt_local_entry)
4070 goto out;
4071
4072 ret = tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM;
Sven Eckelmann95c0db92016-01-17 11:01:25 +01004073 batadv_tt_local_entry_put(tt_local_entry);
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02004074out:
4075 return ret;
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02004076}
4077
Sven Eckelmannff15c272017-12-02 19:51:53 +01004078/**
4079 * batadv_tt_add_temporary_global_entry() - Add temporary entry to global TT
4080 * @bat_priv: the bat priv with all the soft interface information
4081 * @orig_node: orig node which the temporary entry should be associated with
4082 * @addr: mac address of the client
4083 * @vid: VLAN id of the new temporary global translation table
4084 *
4085 * Return: true when temporary tt entry could be added, false otherwise
4086 */
Antonio Quartulli30cfd022012-07-05 23:38:29 +02004087bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv,
4088 struct batadv_orig_node *orig_node,
Antonio Quartullic018ad32013-06-04 12:11:39 +02004089 const unsigned char *addr,
Antonio Quartulli16052782013-06-04 12:11:41 +02004090 unsigned short vid)
Antonio Quartulli30cfd022012-07-05 23:38:29 +02004091{
Simon Wunderlich1227c9a2017-06-01 17:11:24 +02004092 /* ignore loop detect macs, they are not supposed to be in the tt local
4093 * data as well.
4094 */
4095 if (batadv_bla_is_loopdetect_mac(addr))
4096 return false;
4097
Antonio Quartulli16052782013-06-04 12:11:41 +02004098 if (!batadv_tt_global_add(bat_priv, orig_node, addr, vid,
Antonio Quartulli30cfd022012-07-05 23:38:29 +02004099 BATADV_TT_CLIENT_TEMP,
4100 atomic_read(&orig_node->last_ttvn)))
Simon Wunderlich75ae84a2017-06-01 17:11:26 +02004101 return false;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02004102
4103 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02004104 "Added temporary global client (addr: %pM, vid: %d, orig: %pM)\n",
Sven Eckelmannf7a2bd62017-02-22 17:16:42 +01004105 addr, batadv_print_vid(vid), orig_node->orig);
Simon Wunderlich75ae84a2017-06-01 17:11:26 +02004106
4107 return true;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02004108}
Marek Lindnere1bf0c12013-04-23 21:40:01 +08004109
4110/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01004111 * batadv_tt_local_resize_to_mtu() - resize the local translation table fit the
Marek Lindnera19d3d82013-05-27 15:33:25 +08004112 * maximum packet size that can be transported through the mesh
4113 * @soft_iface: netdev struct of the mesh interface
4114 *
4115 * Remove entries older than 'timeout' and half timeout if more entries need
4116 * to be removed.
4117 */
4118void batadv_tt_local_resize_to_mtu(struct net_device *soft_iface)
4119{
4120 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
4121 int packet_size_max = atomic_read(&bat_priv->packet_size_max);
4122 int table_size, timeout = BATADV_TT_LOCAL_TIMEOUT / 2;
4123 bool reduced = false;
4124
4125 spin_lock_bh(&bat_priv->tt.commit_lock);
4126
4127 while (true) {
4128 table_size = batadv_tt_local_table_transmit_size(bat_priv);
4129 if (packet_size_max >= table_size)
4130 break;
4131
4132 batadv_tt_local_purge(bat_priv, timeout);
4133 batadv_tt_local_purge_pending_clients(bat_priv);
4134
4135 timeout /= 2;
4136 reduced = true;
4137 net_ratelimited_function(batadv_info, soft_iface,
4138 "Forced to purge local tt entries to fit new maximum fragment MTU (%i)\n",
4139 packet_size_max);
4140 }
4141
4142 /* commit these changes immediately, to avoid synchronization problem
4143 * with the TTVN
4144 */
4145 if (reduced)
4146 batadv_tt_local_commit_changes_nolock(bat_priv);
4147
4148 spin_unlock_bh(&bat_priv->tt.commit_lock);
4149}
4150
4151/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01004152 * batadv_tt_tvlv_ogm_handler_v1() - process incoming tt tvlv container
Marek Lindnere1bf0c12013-04-23 21:40:01 +08004153 * @bat_priv: the bat priv with all the soft interface information
4154 * @orig: the orig_node of the ogm
4155 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
4156 * @tvlv_value: tvlv buffer containing the gateway data
4157 * @tvlv_value_len: tvlv buffer length
4158 */
4159static void batadv_tt_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
4160 struct batadv_orig_node *orig,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02004161 u8 flags, void *tvlv_value,
4162 u16 tvlv_value_len)
Marek Lindnere1bf0c12013-04-23 21:40:01 +08004163{
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02004164 struct batadv_tvlv_tt_vlan_data *tt_vlan;
4165 struct batadv_tvlv_tt_change *tt_change;
Marek Lindnere1bf0c12013-04-23 21:40:01 +08004166 struct batadv_tvlv_tt_data *tt_data;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02004167 u16 num_entries, num_vlan;
Marek Lindnere1bf0c12013-04-23 21:40:01 +08004168
4169 if (tvlv_value_len < sizeof(*tt_data))
4170 return;
4171
4172 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
4173 tvlv_value_len -= sizeof(*tt_data);
4174
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02004175 num_vlan = ntohs(tt_data->num_vlan);
4176
4177 if (tvlv_value_len < sizeof(*tt_vlan) * num_vlan)
4178 return;
4179
4180 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
4181 tt_change = (struct batadv_tvlv_tt_change *)(tt_vlan + num_vlan);
4182 tvlv_value_len -= sizeof(*tt_vlan) * num_vlan;
4183
Antonio Quartulli298e6e62013-05-28 13:14:27 +02004184 num_entries = batadv_tt_entries(tvlv_value_len);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08004185
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02004186 batadv_tt_update_orig(bat_priv, orig, tt_vlan, num_vlan, tt_change,
4187 num_entries, tt_data->ttvn);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08004188}
4189
4190/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01004191 * batadv_tt_tvlv_unicast_handler_v1() - process incoming (unicast) tt tvlv
Marek Lindner335fbe02013-04-23 21:40:02 +08004192 * container
4193 * @bat_priv: the bat priv with all the soft interface information
4194 * @src: mac address of tt tvlv sender
4195 * @dst: mac address of tt tvlv recipient
4196 * @tvlv_value: tvlv buffer containing the tt data
4197 * @tvlv_value_len: tvlv buffer length
4198 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02004199 * Return: NET_RX_DROP if the tt tvlv is to be re-routed, NET_RX_SUCCESS
Marek Lindner335fbe02013-04-23 21:40:02 +08004200 * otherwise.
4201 */
4202static int batadv_tt_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02004203 u8 *src, u8 *dst,
Marek Lindner335fbe02013-04-23 21:40:02 +08004204 void *tvlv_value,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02004205 u16 tvlv_value_len)
Marek Lindner335fbe02013-04-23 21:40:02 +08004206{
4207 struct batadv_tvlv_tt_data *tt_data;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02004208 u16 tt_vlan_len, tt_num_entries;
Marek Lindner335fbe02013-04-23 21:40:02 +08004209 char tt_flag;
4210 bool ret;
4211
4212 if (tvlv_value_len < sizeof(*tt_data))
4213 return NET_RX_SUCCESS;
4214
4215 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
4216 tvlv_value_len -= sizeof(*tt_data);
4217
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02004218 tt_vlan_len = sizeof(struct batadv_tvlv_tt_vlan_data);
4219 tt_vlan_len *= ntohs(tt_data->num_vlan);
4220
4221 if (tvlv_value_len < tt_vlan_len)
4222 return NET_RX_SUCCESS;
4223
4224 tvlv_value_len -= tt_vlan_len;
4225 tt_num_entries = batadv_tt_entries(tvlv_value_len);
Marek Lindner335fbe02013-04-23 21:40:02 +08004226
4227 switch (tt_data->flags & BATADV_TT_DATA_TYPE_MASK) {
4228 case BATADV_TT_REQUEST:
4229 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_RX);
4230
4231 /* If this node cannot provide a TT response the tt_request is
4232 * forwarded
4233 */
4234 ret = batadv_send_tt_response(bat_priv, tt_data, src, dst);
4235 if (!ret) {
4236 if (tt_data->flags & BATADV_TT_FULL_TABLE)
4237 tt_flag = 'F';
4238 else
4239 tt_flag = '.';
4240
4241 batadv_dbg(BATADV_DBG_TT, bat_priv,
4242 "Routing TT_REQUEST to %pM [%c]\n",
4243 dst, tt_flag);
4244 /* tvlv API will re-route the packet */
4245 return NET_RX_DROP;
4246 }
4247 break;
4248 case BATADV_TT_RESPONSE:
4249 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_RX);
4250
4251 if (batadv_is_my_mac(bat_priv, dst)) {
4252 batadv_handle_tt_response(bat_priv, tt_data,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02004253 src, tt_num_entries);
Marek Lindner335fbe02013-04-23 21:40:02 +08004254 return NET_RX_SUCCESS;
4255 }
4256
4257 if (tt_data->flags & BATADV_TT_FULL_TABLE)
4258 tt_flag = 'F';
4259 else
4260 tt_flag = '.';
4261
4262 batadv_dbg(BATADV_DBG_TT, bat_priv,
4263 "Routing TT_RESPONSE to %pM [%c]\n", dst, tt_flag);
4264
4265 /* tvlv API will re-route the packet */
4266 return NET_RX_DROP;
4267 }
4268
4269 return NET_RX_SUCCESS;
4270}
4271
4272/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01004273 * batadv_roam_tvlv_unicast_handler_v1() - process incoming tt roam tvlv
4274 * container
Marek Lindner122edaa2013-04-23 21:40:03 +08004275 * @bat_priv: the bat priv with all the soft interface information
4276 * @src: mac address of tt tvlv sender
4277 * @dst: mac address of tt tvlv recipient
4278 * @tvlv_value: tvlv buffer containing the tt data
4279 * @tvlv_value_len: tvlv buffer length
4280 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02004281 * Return: NET_RX_DROP if the tt roam tvlv is to be re-routed, NET_RX_SUCCESS
Marek Lindner122edaa2013-04-23 21:40:03 +08004282 * otherwise.
4283 */
4284static int batadv_roam_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02004285 u8 *src, u8 *dst,
Marek Lindner122edaa2013-04-23 21:40:03 +08004286 void *tvlv_value,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02004287 u16 tvlv_value_len)
Marek Lindner122edaa2013-04-23 21:40:03 +08004288{
4289 struct batadv_tvlv_roam_adv *roaming_adv;
4290 struct batadv_orig_node *orig_node = NULL;
4291
4292 /* If this node is not the intended recipient of the
4293 * roaming advertisement the packet is forwarded
4294 * (the tvlv API will re-route the packet).
4295 */
4296 if (!batadv_is_my_mac(bat_priv, dst))
4297 return NET_RX_DROP;
4298
Marek Lindner122edaa2013-04-23 21:40:03 +08004299 if (tvlv_value_len < sizeof(*roaming_adv))
4300 goto out;
4301
4302 orig_node = batadv_orig_hash_find(bat_priv, src);
4303 if (!orig_node)
4304 goto out;
4305
4306 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_RX);
4307 roaming_adv = (struct batadv_tvlv_roam_adv *)tvlv_value;
4308
4309 batadv_dbg(BATADV_DBG_TT, bat_priv,
4310 "Received ROAMING_ADV from %pM (client %pM)\n",
4311 src, roaming_adv->client);
4312
4313 batadv_tt_global_add(bat_priv, orig_node, roaming_adv->client,
Antonio Quartullic018ad32013-06-04 12:11:39 +02004314 ntohs(roaming_adv->vid), BATADV_TT_CLIENT_ROAM,
Marek Lindner122edaa2013-04-23 21:40:03 +08004315 atomic_read(&orig_node->last_ttvn) + 1);
4316
4317out:
4318 if (orig_node)
Sven Eckelmann5d967312016-01-17 11:01:09 +01004319 batadv_orig_node_put(orig_node);
Marek Lindner122edaa2013-04-23 21:40:03 +08004320 return NET_RX_SUCCESS;
4321}
4322
4323/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01004324 * batadv_tt_init() - initialise the translation table internals
Marek Lindnere1bf0c12013-04-23 21:40:01 +08004325 * @bat_priv: the bat priv with all the soft interface information
4326 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02004327 * Return: 0 on success or negative error number in case of failure.
Marek Lindnere1bf0c12013-04-23 21:40:01 +08004328 */
4329int batadv_tt_init(struct batadv_priv *bat_priv)
4330{
4331 int ret;
4332
Antonio Quartulli0eb015682013-10-13 02:50:20 +02004333 /* synchronized flags must be remote */
4334 BUILD_BUG_ON(!(BATADV_TT_SYNC_MASK & BATADV_TT_REMOTE_MASK));
4335
Marek Lindnere1bf0c12013-04-23 21:40:01 +08004336 ret = batadv_tt_local_init(bat_priv);
4337 if (ret < 0)
4338 return ret;
4339
4340 ret = batadv_tt_global_init(bat_priv);
4341 if (ret < 0)
4342 return ret;
4343
4344 batadv_tvlv_handler_register(bat_priv, batadv_tt_tvlv_ogm_handler_v1,
Marek Lindner335fbe02013-04-23 21:40:02 +08004345 batadv_tt_tvlv_unicast_handler_v1,
4346 BATADV_TVLV_TT, 1, BATADV_NO_FLAGS);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08004347
Marek Lindner122edaa2013-04-23 21:40:03 +08004348 batadv_tvlv_handler_register(bat_priv, NULL,
4349 batadv_roam_tvlv_unicast_handler_v1,
4350 BATADV_TVLV_ROAM, 1, BATADV_NO_FLAGS);
4351
Marek Lindnere1bf0c12013-04-23 21:40:01 +08004352 INIT_DELAYED_WORK(&bat_priv->tt.work, batadv_tt_purge);
4353 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
4354 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
4355
4356 return 1;
4357}
Antonio Quartulli42cb0be2013-11-16 12:03:52 +01004358
4359/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01004360 * batadv_tt_global_is_isolated() - check if a client is marked as isolated
Antonio Quartulli42cb0be2013-11-16 12:03:52 +01004361 * @bat_priv: the bat priv with all the soft interface information
4362 * @addr: the mac address of the client
4363 * @vid: the identifier of the VLAN where this client is connected
4364 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02004365 * Return: true if the client is marked with the TT_CLIENT_ISOLA flag, false
Antonio Quartulli42cb0be2013-11-16 12:03:52 +01004366 * otherwise
4367 */
4368bool batadv_tt_global_is_isolated(struct batadv_priv *bat_priv,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02004369 const u8 *addr, unsigned short vid)
Antonio Quartulli42cb0be2013-11-16 12:03:52 +01004370{
4371 struct batadv_tt_global_entry *tt;
4372 bool ret;
4373
4374 tt = batadv_tt_global_hash_find(bat_priv, addr, vid);
4375 if (!tt)
4376 return false;
4377
4378 ret = tt->common.flags & BATADV_TT_CLIENT_ISOLA;
4379
Sven Eckelmann5dafd8a2016-01-17 11:01:26 +01004380 batadv_tt_global_entry_put(tt);
Antonio Quartulli42cb0be2013-11-16 12:03:52 +01004381
4382 return ret;
4383}
Sven Eckelmann86452f82016-06-25 16:44:06 +02004384
4385/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01004386 * batadv_tt_cache_init() - Initialize tt memory object cache
Sven Eckelmann86452f82016-06-25 16:44:06 +02004387 *
4388 * Return: 0 on success or negative error number in case of failure.
4389 */
4390int __init batadv_tt_cache_init(void)
4391{
4392 size_t tl_size = sizeof(struct batadv_tt_local_entry);
4393 size_t tg_size = sizeof(struct batadv_tt_global_entry);
4394 size_t tt_orig_size = sizeof(struct batadv_tt_orig_list_entry);
4395 size_t tt_change_size = sizeof(struct batadv_tt_change_node);
4396 size_t tt_req_size = sizeof(struct batadv_tt_req_node);
4397 size_t tt_roam_size = sizeof(struct batadv_tt_roam_node);
4398
4399 batadv_tl_cache = kmem_cache_create("batadv_tl_cache", tl_size, 0,
4400 SLAB_HWCACHE_ALIGN, NULL);
4401 if (!batadv_tl_cache)
4402 return -ENOMEM;
4403
4404 batadv_tg_cache = kmem_cache_create("batadv_tg_cache", tg_size, 0,
4405 SLAB_HWCACHE_ALIGN, NULL);
4406 if (!batadv_tg_cache)
4407 goto err_tt_tl_destroy;
4408
4409 batadv_tt_orig_cache = kmem_cache_create("batadv_tt_orig_cache",
4410 tt_orig_size, 0,
4411 SLAB_HWCACHE_ALIGN, NULL);
4412 if (!batadv_tt_orig_cache)
4413 goto err_tt_tg_destroy;
4414
4415 batadv_tt_change_cache = kmem_cache_create("batadv_tt_change_cache",
4416 tt_change_size, 0,
4417 SLAB_HWCACHE_ALIGN, NULL);
4418 if (!batadv_tt_change_cache)
4419 goto err_tt_orig_destroy;
4420
4421 batadv_tt_req_cache = kmem_cache_create("batadv_tt_req_cache",
4422 tt_req_size, 0,
4423 SLAB_HWCACHE_ALIGN, NULL);
4424 if (!batadv_tt_req_cache)
4425 goto err_tt_change_destroy;
4426
4427 batadv_tt_roam_cache = kmem_cache_create("batadv_tt_roam_cache",
4428 tt_roam_size, 0,
4429 SLAB_HWCACHE_ALIGN, NULL);
4430 if (!batadv_tt_roam_cache)
4431 goto err_tt_req_destroy;
4432
4433 return 0;
4434
4435err_tt_req_destroy:
4436 kmem_cache_destroy(batadv_tt_req_cache);
4437 batadv_tt_req_cache = NULL;
4438err_tt_change_destroy:
4439 kmem_cache_destroy(batadv_tt_change_cache);
4440 batadv_tt_change_cache = NULL;
4441err_tt_orig_destroy:
4442 kmem_cache_destroy(batadv_tt_orig_cache);
4443 batadv_tt_orig_cache = NULL;
4444err_tt_tg_destroy:
4445 kmem_cache_destroy(batadv_tg_cache);
4446 batadv_tg_cache = NULL;
4447err_tt_tl_destroy:
4448 kmem_cache_destroy(batadv_tl_cache);
4449 batadv_tl_cache = NULL;
4450
4451 return -ENOMEM;
4452}
4453
4454/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01004455 * batadv_tt_cache_destroy() - Destroy tt memory object cache
Sven Eckelmann86452f82016-06-25 16:44:06 +02004456 */
4457void batadv_tt_cache_destroy(void)
4458{
4459 kmem_cache_destroy(batadv_tl_cache);
4460 kmem_cache_destroy(batadv_tg_cache);
4461 kmem_cache_destroy(batadv_tt_orig_cache);
4462 kmem_cache_destroy(batadv_tt_change_cache);
4463 kmem_cache_destroy(batadv_tt_req_cache);
4464 kmem_cache_destroy(batadv_tt_roam_cache);
4465}